blob: 197179221de0e198a24331da239e4e0a482907d6 [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 if (h2c->st0 >= H2_CS_ERROR)
1924 goto strm_err;
1925
1926 if (h2s->st >= H2_SS_ERROR) {
1927 /* stream error : send RST_STREAM */
1928 h2c->st0 = H2_CS_FRAME_E;
1929 }
1930 }
1931 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001932
Willy Tarreaud7901432017-12-29 11:34:40 +01001933 if (h2c->st0 == H2_CS_FRAME_E)
1934 goto strm_err;
1935
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001936 if (h2s->st == H2_SS_IDLE &&
1937 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1938 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1939 * this state MUST be treated as a connection error
1940 */
1941 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1942 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001943 if (!h2c->nb_streams) {
1944 /* only log if no other stream can report the error */
1945 sess_log(h2c->conn->owner);
1946 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001947 break;
1948 }
1949
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001950 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1951 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1952 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1953 * this state MUST be treated as a stream error
1954 */
1955 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1956 goto strm_err;
1957 }
1958
Willy Tarreauab837502017-12-27 15:07:30 +01001959 /* Below the management of frames received in closed state is a
1960 * bit hackish because the spec makes strong differences between
1961 * streams closed by receiving RST, sending RST, and seeing ES
1962 * in both directions. In addition to this, the creation of a
1963 * new stream reusing the identifier of a closed one will be
1964 * detected here. Given that we cannot keep track of all closed
1965 * streams forever, we consider that unknown closed streams were
1966 * closed on RST received, which allows us to respond with an
1967 * RST without breaking the connection (eg: to abort a transfer).
1968 * Some frames have to be silently ignored as well.
1969 */
1970 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1971 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1972 /* #5.1.1: The identifier of a newly
1973 * established stream MUST be numerically
1974 * greater than all streams that the initiating
1975 * endpoint has opened or reserved. This
1976 * governs streams that are opened using a
1977 * HEADERS frame and streams that are reserved
1978 * using PUSH_PROMISE. An endpoint that
1979 * receives an unexpected stream identifier
1980 * MUST respond with a connection error.
1981 */
1982 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1983 goto strm_err;
1984 }
1985
1986 if (h2s->flags & H2_SF_RST_RCVD) {
1987 /* RFC7540#5.1:closed: an endpoint that
1988 * receives any frame other than PRIORITY after
1989 * receiving a RST_STREAM MUST treat that as a
1990 * stream error of type STREAM_CLOSED.
1991 *
1992 * Note that old streams fall into this category
1993 * and will lead to an RST being sent.
1994 */
1995 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1996 h2c->st0 = H2_CS_FRAME_E;
1997 goto strm_err;
1998 }
1999
2000 /* RFC7540#5.1:closed: if this state is reached as a
2001 * result of sending a RST_STREAM frame, the peer that
2002 * receives the RST_STREAM might have already sent
2003 * frames on the stream that cannot be withdrawn. An
2004 * endpoint MUST ignore frames that it receives on
2005 * closed streams after it has sent a RST_STREAM
2006 * frame. An endpoint MAY choose to limit the period
2007 * over which it ignores frames and treat frames that
2008 * arrive after this time as being in error.
2009 */
2010 if (!(h2s->flags & H2_SF_RST_SENT)) {
2011 /* RFC7540#5.1:closed: any frame other than
2012 * PRIO/WU/RST in this state MUST be treated as
2013 * a connection error
2014 */
2015 if (h2c->dft != H2_FT_RST_STREAM &&
2016 h2c->dft != H2_FT_PRIORITY &&
2017 h2c->dft != H2_FT_WINDOW_UPDATE) {
2018 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2019 goto strm_err;
2020 }
2021 }
2022 }
2023
Willy Tarreauc0da1962017-10-30 18:38:00 +01002024#if 0
2025 // problem below: it is not possible to completely ignore such
2026 // streams as we need to maintain the compression state as well
2027 // and for this we need to completely process these frames (eg:
2028 // HEADERS frames) as well as counting DATA frames to emit
2029 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2030 // This is a typical case of layer violation where the
2031 // transported contents are critical to the connection's
2032 // validity and must be ignored at the same time :-(
2033
2034 /* graceful shutdown, ignore streams whose ID is higher than
2035 * the one advertised in GOAWAY. RFC7540#6.8.
2036 */
2037 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002038 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2039 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002040 h2c->dfl -= ret;
2041 ret = h2c->dfl == 0;
2042 goto strm_err;
2043 }
2044#endif
2045
Willy Tarreau7e98c052017-10-10 15:56:59 +02002046 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002047 case H2_FT_SETTINGS:
2048 if (h2c->st0 == H2_CS_FRAME_P)
2049 ret = h2c_handle_settings(h2c);
2050
2051 if (h2c->st0 == H2_CS_FRAME_A)
2052 ret = h2c_ack_settings(h2c);
2053 break;
2054
Willy Tarreaucf68c782017-10-10 17:11:41 +02002055 case H2_FT_PING:
2056 if (h2c->st0 == H2_CS_FRAME_P)
2057 ret = h2c_handle_ping(h2c);
2058
2059 if (h2c->st0 == H2_CS_FRAME_A)
2060 ret = h2c_ack_ping(h2c);
2061 break;
2062
Willy Tarreau26f95952017-07-27 17:18:30 +02002063 case H2_FT_WINDOW_UPDATE:
2064 if (h2c->st0 == H2_CS_FRAME_P)
2065 ret = h2c_handle_window_update(h2c, h2s);
2066 break;
2067
Willy Tarreau61290ec2017-10-17 08:19:21 +02002068 case H2_FT_CONTINUATION:
2069 /* we currently don't support CONTINUATION frames since
2070 * we have nowhere to store the partial HEADERS frame.
2071 * Let's abort the stream on an INTERNAL_ERROR here.
2072 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002073 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02002074 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002075 h2c->st0 = H2_CS_FRAME_E;
2076 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02002077 break;
2078
Willy Tarreau13278b42017-10-13 19:23:14 +02002079 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002080 if (h2c->st0 == H2_CS_FRAME_P) {
2081 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
2082 if (tmp_h2s) {
2083 h2s = tmp_h2s;
2084 ret = 1;
2085 }
2086 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002087 break;
2088
Willy Tarreau454f9052017-10-26 19:40:35 +02002089 case H2_FT_DATA:
2090 if (h2c->st0 == H2_CS_FRAME_P)
2091 ret = h2c_frt_handle_data(h2c, h2s);
2092
2093 if (h2c->st0 == H2_CS_FRAME_A)
2094 ret = h2c_send_strm_wu(h2c);
2095 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002096
Willy Tarreau92153fc2017-12-03 19:46:19 +01002097 case H2_FT_PRIORITY:
2098 if (h2c->st0 == H2_CS_FRAME_P)
2099 ret = h2c_handle_priority(h2c);
2100 break;
2101
Willy Tarreaucd234e92017-08-18 10:59:39 +02002102 case H2_FT_RST_STREAM:
2103 if (h2c->st0 == H2_CS_FRAME_P)
2104 ret = h2c_handle_rst_stream(h2c, h2s);
2105 break;
2106
Willy Tarreaue96b0922017-10-30 00:28:29 +01002107 case H2_FT_GOAWAY:
2108 if (h2c->st0 == H2_CS_FRAME_P)
2109 ret = h2c_handle_goaway(h2c);
2110 break;
2111
Willy Tarreau1c661982017-10-30 13:52:01 +01002112 case H2_FT_PUSH_PROMISE:
2113 /* not permitted here, RFC7540#5.1 */
2114 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002115 if (!h2c->nb_streams) {
2116 /* only log if no other stream can report the error */
2117 sess_log(h2c->conn->owner);
2118 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002119 break;
2120
2121 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002122 default:
2123 /* drop frames that we ignore. They may be larger than
2124 * the buffer so we drain all of their contents until
2125 * we reach the end.
2126 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002127 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2128 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002129 h2c->dfl -= ret;
2130 ret = h2c->dfl == 0;
2131 }
2132
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002133 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002134 /* We may have to send an RST if not done yet */
2135 if (h2s->st == H2_SS_ERROR)
2136 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002137
Willy Tarreaua20a5192017-12-27 11:02:06 +01002138 if (h2c->st0 == H2_CS_FRAME_E)
2139 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002140
Willy Tarreau7e98c052017-10-10 15:56:59 +02002141 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002142 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002143 break;
2144
2145 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002146 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002147 h2c->st0 = H2_CS_FRAME_H;
2148 }
2149 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002150
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002151 if (h2c->rcvd_c > 0 &&
2152 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2153 h2c_send_conn_wu(h2c);
2154
Willy Tarreau52eed752017-09-22 15:05:09 +02002155 fail:
2156 /* we can go here on missing data, blocked response or error */
Olivier Houchard638b7992018-08-16 15:41:52 +02002157 if (h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002158 /* we may have to signal the upper layers */
2159 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002160 if (h2s->recv_wait) {
2161 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
2162 tasklet_wakeup(h2s->recv_wait->task);
2163 h2s->recv_wait = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002164 }
2165 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002166
2167 if (h2_recv_allowed(h2c))
2168 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreaubc933932017-10-09 16:21:43 +02002169}
2170
2171/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2172 * the end.
2173 */
2174static int h2_process_mux(struct h2c *h2c)
2175{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002176 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002177
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002178 /* start by sending possibly pending window updates */
2179 if (h2c->rcvd_c > 0 &&
2180 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2181 h2c_send_conn_wu(h2c) < 0)
2182 goto fail;
2183
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002184 /* First we always process the flow control list because the streams
2185 * waiting there were already elected for immediate emission but were
2186 * blocked just on this.
2187 */
2188
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002189 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002190 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2191 h2c->st0 >= H2_CS_ERROR)
2192 break;
2193
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002194 h2s->flags &= ~H2_SF_BLK_ANY;
2195 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002196 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002197 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002198 LIST_DEL(&h2s->list);
2199 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002200 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002201 }
2202
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002203 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002204 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2205 break;
2206
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002207 h2s->flags &= ~H2_SF_BLK_ANY;
2208 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002209 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002210 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002211 LIST_DEL(&h2s->list);
2212 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002213 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002214 }
2215
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002216 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002217 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002218 if (h2c->st0 == H2_CS_ERROR) {
2219 if (h2c->max_id >= 0) {
2220 h2c_send_goaway_error(h2c, NULL);
2221 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2222 return 0;
2223 }
2224
2225 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2226 }
2227 return 1;
2228 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002229 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002230}
2231
Willy Tarreau62f52692017-10-08 23:01:42 +02002232
Willy Tarreau479998a2018-11-18 06:30:59 +01002233/* Attempt to read data, and subscribe if none available.
2234 * The function returns 1 if data has been received, otherwise zero.
2235 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002236static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002237{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002238 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002239 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002240 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002241 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002242
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002243 if (h2c->wait_event.wait_reason & SUB_CAN_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002244 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002245
Willy Tarreau315d8072017-12-10 22:17:57 +01002246 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002247 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002248
Willy Tarreau44e973f2018-03-01 17:49:30 +01002249 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002250 if (!buf) {
2251 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002252 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002253 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002254
Olivier Houchard7505f942018-08-21 18:10:44 +02002255 do {
2256 max = buf->size - b_data(buf);
2257 if (max)
2258 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2259 else
2260 ret = 0;
2261 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002262
Olivier Houchard53216e72018-10-10 15:46:36 +02002263 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002264 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002265
Olivier Houcharda1411e62018-08-17 18:42:48 +02002266 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002267 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002268 return conn_xprt_read0_pending(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002269 }
2270
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002271 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002272 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002273 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002274}
2275
Willy Tarreau479998a2018-11-18 06:30:59 +01002276/* Try to send data if possible.
2277 * The function returns 1 if data have been sent, otherwise zero.
2278 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002279static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002280{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002281 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002282 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002283 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002284
2285 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002286 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002287
Olivier Houchard7505f942018-08-21 18:10:44 +02002288
Willy Tarreaua2af5122017-10-09 11:56:46 +02002289 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2290 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002291 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002292 }
2293
Willy Tarreaubc933932017-10-09 16:21:43 +02002294 /* This loop is quite simple : it tries to fill as much as it can from
2295 * pending streams into the existing buffer until it's reportedly full
2296 * or the end of send requests is reached. Then it tries to send this
2297 * buffer's contents out, marks it not full if at least one byte could
2298 * be sent, and tries again.
2299 *
2300 * The snd_buf() function normally takes a "flags" argument which may
2301 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2302 * data immediately comes and CO_SFL_STREAMER to indicate that the
2303 * connection is streaming lots of data (used to increase TLS record
2304 * size at the expense of latency). The former can be sent any time
2305 * there's a buffer full flag, as it indicates at least one stream
2306 * attempted to send and failed so there are pending data. An
2307 * alternative would be to set it as long as there's an active stream
2308 * but that would be problematic for ACKs until we have an absolute
2309 * guarantee that all waiters have at least one byte to send. The
2310 * latter should possibly not be set for now.
2311 */
2312
2313 done = 0;
2314 while (!done) {
2315 unsigned int flags = 0;
2316
2317 /* fill as much as we can into the current buffer */
2318 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2319 done = h2_process_mux(h2c);
2320
2321 if (conn->flags & CO_FL_ERROR)
2322 break;
2323
2324 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2325 flags |= CO_SFL_MSG_MORE;
2326
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002327 if (b_data(&h2c->mbuf)) {
2328 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002329 if (!ret)
2330 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002331 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002332 b_del(&h2c->mbuf, ret);
2333 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002334 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002335
2336 /* wrote at least one byte, the buffer is not full anymore */
2337 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2338 }
2339
Willy Tarreaua2af5122017-10-09 11:56:46 +02002340 if (conn->flags & CO_FL_SOCK_WR_SH) {
2341 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002342 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002343 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002344 /* We're not full anymore, so we can wake any task that are waiting
2345 * for us.
2346 */
2347 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002348 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002349 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2350 struct h2s *, list);
2351 LIST_DEL(&h2s->list);
2352 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002353 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002354 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002355 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002356 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002357 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002358 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002359 /* We're done, no more to send */
2360 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002361 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002362schedule:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002363 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
2364 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002365 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002366}
2367
2368static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2369{
2370 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002371 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002372
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002373 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002374 ret = h2_send(h2c);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002375 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002376 ret |= h2_recv(h2c);
2377 if (ret)
2378 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002379 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002380}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002381
Willy Tarreau62f52692017-10-08 23:01:42 +02002382/* callback called on any event by the connection handler.
2383 * It applies changes and returns zero, or < 0 if it wants immediate
2384 * destruction of the connection (which normally doesn not happen in h2).
2385 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002386static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002387{
Olivier Houchard7505f942018-08-21 18:10:44 +02002388 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002389
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002390 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002391 h2_process_demux(h2c);
2392
2393 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002394 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002395
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002396 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002397 h2c->flags &= ~H2_CF_DEM_DFULL;
2398 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002399 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002400
Willy Tarreau0b37d652018-10-03 10:33:02 +02002401 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002402 /* frontend is stopping, reload likely in progress, let's try
2403 * to announce a graceful shutdown if not yet done. We don't
2404 * care if it fails, it will be tried again later.
2405 */
2406 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2407 if (h2c->last_sid < 0)
2408 h2c->last_sid = (1U << 31) - 1;
2409 h2c_send_goaway_error(h2c, NULL);
2410 }
2411 }
2412
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002413 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002414 * If we received early data, and the handshake is done, wake
2415 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002416 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002417 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2418 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2419 struct eb32_node *node;
2420 struct h2s *h2s;
2421
2422 h2c->flags |= H2_CF_WAIT_FOR_HS;
2423 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2424
2425 while (node) {
2426 h2s = container_of(node, struct h2s, by_id);
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002427 if ((h2s->cs->flags & CS_FL_WAIT_FOR_HS) &&
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002428 h2s->recv_wait) {
2429 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002430 sw->wait_reason &= ~SUB_CAN_RECV;
2431 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002432 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002433 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002434 node = eb32_next(node);
2435 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002436 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002437
Willy Tarreau26bd7612017-10-09 16:47:04 +02002438 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002439 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2440 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2441 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002442 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002443
2444 if (eb_is_empty(&h2c->streams_by_id)) {
2445 /* no more stream, kill the connection now */
2446 h2_release(conn);
2447 return -1;
2448 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002449 }
2450
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002451 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002452 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002453
Olivier Houchard53216e72018-10-10 15:46:36 +02002454 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2455 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2456 (h2c->st0 != H2_CS_ERROR &&
2457 !b_data(&h2c->mbuf) &&
2458 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2459 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002460 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002461
Willy Tarreau3f133572017-10-31 19:21:06 +01002462 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002463 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002464 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002465 task_queue(h2c->task);
2466 }
2467 else
2468 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002469 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002470
Olivier Houchard7505f942018-08-21 18:10:44 +02002471 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002472 return 0;
2473}
2474
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002475static int h2_wake(struct connection *conn)
2476{
2477 struct h2c *h2c = conn->mux_ctx;
2478
2479 return (h2_process(h2c));
2480}
2481
Willy Tarreauea392822017-10-31 10:02:25 +01002482/* Connection timeout management. The principle is that if there's no receipt
2483 * nor sending for a certain amount of time, the connection is closed. If the
2484 * MUX buffer still has lying data or is not allocatable, the connection is
2485 * immediately killed. If it's allocatable and empty, we attempt to send a
2486 * GOAWAY frame.
2487 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002488static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002489{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002490 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002491 int expired = tick_is_expired(t->expire, now_ms);
2492
Willy Tarreau0975f112018-03-29 15:22:59 +02002493 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002494 return t;
2495
Willy Tarreau0975f112018-03-29 15:22:59 +02002496 task_delete(t);
2497 task_free(t);
2498
2499 if (!h2c) {
2500 /* resources were already deleted */
2501 return NULL;
2502 }
2503
2504 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002505 h2c_error(h2c, H2_ERR_NO_ERROR);
2506 h2_wake_some_streams(h2c, 0, 0);
2507
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002508 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002509 /* don't even try to send a GOAWAY, the buffer is stuck */
2510 h2c->flags |= H2_CF_GOAWAY_FAILED;
2511 }
2512
2513 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002514 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002515 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2516 h2c->flags |= H2_CF_GOAWAY_FAILED;
2517
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002518 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2519 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002520 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002521 b_del(&h2c->mbuf, ret);
2522 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002523 }
2524 }
Willy Tarreauea392822017-10-31 10:02:25 +01002525
Willy Tarreau0975f112018-03-29 15:22:59 +02002526 /* either we can release everything now or it will be done later once
2527 * the last stream closes.
2528 */
2529 if (eb_is_empty(&h2c->streams_by_id))
2530 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002531
Willy Tarreauea392822017-10-31 10:02:25 +01002532 return NULL;
2533}
2534
2535
Willy Tarreau62f52692017-10-08 23:01:42 +02002536/*******************************************/
2537/* functions below are used by the streams */
2538/*******************************************/
2539
2540/*
2541 * Attach a new stream to a connection
2542 * (Used for outgoing connections)
2543 */
2544static struct conn_stream *h2_attach(struct connection *conn)
2545{
2546 return NULL;
2547}
2548
Willy Tarreaufafd3982018-11-18 21:29:20 +01002549/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2550 * We have to scan because we may have some orphan streams. It might be
2551 * beneficial to scan backwards from the end to reduce the likeliness to find
2552 * orphans.
2553 */
2554static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2555{
2556 struct h2c *h2c = conn->mux_ctx;
2557 struct h2s *h2s;
2558 struct eb32_node *node;
2559
2560 node = eb32_first(&h2c->streams_by_id);
2561 while (node) {
2562 h2s = container_of(node, struct h2s, by_id);
2563 if (h2s->cs)
2564 return h2s->cs;
2565 node = eb32_next(node);
2566 }
2567 return NULL;
2568}
2569
Willy Tarreau62f52692017-10-08 23:01:42 +02002570/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002571 * Destroy the mux and the associated connection, if it is no longer used
2572 */
2573static void h2_destroy(struct connection *conn)
2574{
2575 struct h2c *h2c = conn->mux_ctx;
2576
2577 if (eb_is_empty(&h2c->streams_by_id))
2578 h2_release(h2c->conn);
2579}
2580
2581/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002582 * Detach the stream from the connection and possibly release the connection.
2583 */
2584static void h2_detach(struct conn_stream *cs)
2585{
Willy Tarreau60935142017-10-16 18:11:19 +02002586 struct h2s *h2s = cs->ctx;
2587 struct h2c *h2c;
2588
2589 cs->ctx = NULL;
2590 if (!h2s)
2591 return;
2592
2593 h2c = h2s->h2c;
2594 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002595 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002596 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2597 !h2_has_too_many_cs(h2c)) {
2598 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard53216e72018-10-10 15:46:36 +02002599 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002600 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreauf2101912018-07-19 10:11:38 +02002601 }
Willy Tarreau60935142017-10-16 18:11:19 +02002602
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002603 /* this stream may be blocked waiting for some data to leave (possibly
2604 * an ES or RST frame), so orphan it in this case.
2605 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002606 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002607 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002608 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002609 return;
2610
Willy Tarreau45f752e2017-10-30 15:44:59 +01002611 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2612 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2613 /* unblock the connection if it was blocked on this
2614 * stream.
2615 */
2616 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2617 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002618 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002619 }
2620
Willy Tarreau71049cc2018-03-28 13:56:39 +02002621 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002622
Willy Tarreaue323f342018-03-28 13:51:45 +02002623 /* We don't want to close right now unless we're removing the
2624 * last stream, and either the connection is in error, or it
2625 * reached the ID already specified in a GOAWAY frame received
2626 * or sent (as seen by last_sid >= 0).
2627 */
2628 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2629 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002630 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard52b94662018-10-21 03:01:20 +02002631 (h2c->flags & (H2_CF_GOAWAY_FAILED | H2_CF_GOAWAY_SENT)) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002632 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002633 (conn_xprt_read0_pending(h2c->conn) ||
2634 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2635 /* no more stream will come, kill it now */
2636 h2_release(h2c->conn);
2637 }
2638 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002639 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002640 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2641 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002642 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002643 else
2644 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002645 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002646}
2647
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002648static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002649{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002650 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002651 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002652
Willy Tarreau721c9742017-11-07 11:05:42 +01002653 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002654 return;
2655
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002656 /* if no outgoing data was seen on this stream, it means it was
2657 * closed with a "tcp-request content" rule that is normally
2658 * used to kill the connection ASAP (eg: limit abuse). In this
2659 * case we send a goaway to close the connection.
2660 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002661 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002662 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002663 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002664
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002665 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2666 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002667 h2c_send_goaway_error(h2c, h2s) <= 0)
2668 return;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002669
Willy Tarreau00dd0782018-03-01 16:31:34 +01002670 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002671
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002672 return;
2673add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002674 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002675 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002676 if (h2s->flags & H2_SF_BLK_MFCTL) {
2677 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2678 h2s->send_wait = sw;
2679 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2680 h2s->send_wait = sw;
2681 LIST_ADDQ(&h2c->send_list, &h2s->list);
2682 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002683 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002684 /* Let the handler know we want shutr */
2685 sw->handle = (void *)((long)sw->handle | 1);
2686
Willy Tarreau62f52692017-10-08 23:01:42 +02002687}
2688
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002689static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002690{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002691 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002692 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002693
Willy Tarreau721c9742017-11-07 11:05:42 +01002694 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002695 return;
2696
Willy Tarreau67434202017-11-06 20:20:51 +01002697 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002698 /* we can cleanly close using an empty data frame only after headers */
2699
2700 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2701 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002702 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002703
2704 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002705 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002706 else
2707 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002708 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002709 /* if no outgoing data was seen on this stream, it means it was
2710 * closed with a "tcp-request content" rule that is normally
2711 * used to kill the connection ASAP (eg: limit abuse). In this
2712 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002713 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002714 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002715 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002716 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002717
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002718 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2719 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002720 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002721 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002722
Willy Tarreau00dd0782018-03-01 16:31:34 +01002723 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002724 }
2725
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002726
2727 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002728 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002729 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002730 if (h2s->flags & H2_SF_BLK_MFCTL) {
2731 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2732 h2s->send_wait = sw;
2733 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2734 h2s->send_wait = sw;
2735 LIST_ADDQ(&h2c->send_list, &h2s->list);
2736 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002737 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002738 /* let the handler know we want to shutw */
2739 sw->handle = (void *)((long)(sw->handle) | 2);
2740
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002741}
2742
2743static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
2744{
2745 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002746 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002747
2748 if (reason & 1)
2749 h2_do_shutr(h2s);
2750 if (reason & 2)
2751 h2_do_shutw(h2s);
2752
2753 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02002754}
2755
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002756static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2757{
2758 struct h2s *h2s = cs->ctx;
2759
2760 if (!mode)
2761 return;
2762
2763 h2_do_shutr(h2s);
2764}
2765
2766static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2767{
2768 struct h2s *h2s = cs->ctx;
2769
2770 h2_do_shutw(h2s);
2771}
2772
Willy Tarreau13278b42017-10-13 19:23:14 +02002773/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2774 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2775 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002776 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002777 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002778static int h2_frt_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002779{
2780 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002781 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02002782 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002783 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02002784 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002785 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01002786 struct buffer *csbuf;
Willy Tarreau13278b42017-10-13 19:23:14 +02002787 int flen = h2c->dfl;
2788 int outlen = 0;
2789 int wrap;
2790 int try;
2791
2792 if (!h2c->dfl) {
2793 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002794 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002795 return 0;
2796 }
2797
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002798 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01002799 return 0; // incomplete input frame
2800
Willy Tarreau13278b42017-10-13 19:23:14 +02002801 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002802 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02002803 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002804 copy = alloc_trash_chunk();
2805 if (!copy) {
2806 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2807 goto fail;
2808 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002809 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
2810 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
2811 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02002812 }
2813
2814 /* The padlen is the first byte before data, and the padding appears
2815 * after data. padlen+data+padding are included in flen.
2816 */
2817 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002818 h2c->dpl = *hdrs;
2819 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002820 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2821 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002822 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002823 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002824 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002825 hdrs += 1; // skip Pad Length
2826 }
2827
2828 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2829 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002830 if (read_n32(hdrs) == h2s->id) {
2831 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2832 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002833 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002834 }
2835
Willy Tarreau13278b42017-10-13 19:23:14 +02002836 hdrs += 5; // stream dep = 4, weight = 1
2837 flen -= 5;
2838 }
2839
2840 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2841 * don't support this for now and can't even decompress so we have to
2842 * break the connection.
2843 */
2844 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2845 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002846 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002847 }
2848
Olivier Houchard638b7992018-08-16 15:41:52 +02002849 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01002850 if (!csbuf) {
2851 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002852 goto fail;
2853 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002854
Willy Tarreau937f7602018-02-26 15:22:17 +01002855 /* we can't retry a failed decompression operation so we must be very
2856 * careful not to take any risks. In practice the output buffer is
2857 * always empty except maybe for trailers, in which case we simply have
2858 * to wait for the upper layer to finish consuming what is available.
2859 */
2860 if (b_data(csbuf))
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002861 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002862
Willy Tarreau937f7602018-02-26 15:22:17 +01002863 csbuf->head = 0;
2864 try = b_size(csbuf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002865
2866 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2867 sizeof(list)/sizeof(list[0]), tmp);
2868 if (outlen < 0) {
2869 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2870 goto fail;
2871 }
2872
2873 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002874 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreau937f7602018-02-26 15:22:17 +01002875 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002876
2877 if (outlen < 0) {
2878 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2879 goto fail;
2880 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002881
Willy Tarreau174b06a2018-04-25 18:13:58 +02002882 if (msgf & H2_MSGF_BODY) {
2883 /* a payload is present */
2884 if (msgf & H2_MSGF_BODY_CL)
2885 h2s->flags |= H2_SF_DATA_CLEN;
2886 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
2887 h2s->flags |= H2_SF_DATA_CHNK;
2888 }
2889
Willy Tarreau13278b42017-10-13 19:23:14 +02002890 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002891 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02002892 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01002893 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02002894
Willy Tarreau39d68502018-03-02 12:26:37 +01002895 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002896 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002897 h2s->cs->flags |= CS_FL_REOS;
2898 }
Willy Tarreau937f7602018-02-26 15:22:17 +01002899
Willy Tarreau68dd9852017-07-03 14:44:26 +02002900 leave:
2901 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002902 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002903 fail:
2904 outlen = 0;
2905 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002906}
2907
Willy Tarreau454f9052017-10-26 19:40:35 +02002908/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2909 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2910 * in use, a new chunk is emitted for each frame. This is supposed to fit
2911 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2912 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2913 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2914 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002915 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2916 * checked to know if some data remain pending (an empty DATA frame can return
2917 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2918 * connection errors in h2c->errcode. The caller must already have checked the
2919 * frame header and ensured that the frame was complete or the buffer full. It
2920 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002921 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002922static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02002923{
2924 struct h2c *h2c = h2s->h2c;
2925 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002926 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002927 unsigned int chklen = 0;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002928 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02002929
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002930 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002931
2932 /* The padlen is the first byte before data, and the padding appears
2933 * after data. padlen+data+padding are included in flen.
2934 */
Willy Tarreau79127812017-12-03 21:06:59 +01002935 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002936 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002937 return 0;
2938
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002939 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002940 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002941 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2942 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002943 return 0;
2944 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002945
2946 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002947 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002948 h2c->dfl--;
2949 h2c->rcvd_c++; h2c->rcvd_s++;
2950 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002951 }
2952
Olivier Houchard638b7992018-08-16 15:41:52 +02002953 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01002954 if (!csbuf) {
2955 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002956 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002957 }
2958
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002959 flen = h2c->dfl - h2c->dpl;
2960 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002961 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002962
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002963 if (flen > b_data(&h2c->dbuf)) {
2964 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002965 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01002966 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002967 }
2968
2969 if (unlikely(b_space_wraps(csbuf))) {
2970 /* it doesn't fit and the buffer is fragmented,
2971 * so let's defragment it and try again.
2972 */
2973 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02002974 }
2975
Willy Tarreaueba10f22018-04-25 20:44:22 +02002976 /* chunked-encoding requires more room */
2977 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01002978 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02002979 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
2980 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2981 (chklen < 1048576) ? 4 : 8;
2982 chklen += 4; // CRLF, CRLF
2983 }
2984
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002985 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002986 if (flen + chklen > b_room(csbuf)) {
2987 if (chklen >= b_room(csbuf)) {
2988 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002989 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002990 }
2991 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002992 }
2993
2994 if (h2s->flags & H2_SF_DATA_CHNK) {
2995 /* emit the chunk size */
2996 unsigned int chksz = flen;
2997 char str[10];
2998 char *beg;
2999
3000 beg = str + sizeof(str);
3001 *--beg = '\n';
3002 *--beg = '\r';
3003 do {
3004 *--beg = hextab[chksz & 0xF];
3005 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003006 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003007 }
3008
Willy Tarreau454f9052017-10-26 19:40:35 +02003009 /* Block1 is the length of the first block before the buffer wraps,
3010 * block2 is the optional second block to reach the end of the frame.
3011 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003012 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003013 if (block1 > flen)
3014 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003015 block2 = flen - block1;
3016
3017 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003018 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003019
3020 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003021 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003022
Willy Tarreaueba10f22018-04-25 20:44:22 +02003023 if (h2s->flags & H2_SF_DATA_CHNK) {
3024 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003025 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003026 }
3027
Willy Tarreau454f9052017-10-26 19:40:35 +02003028 /* now mark the input data as consumed (will be deleted from the buffer
3029 * by the caller when seeing FRAME_A after sending the window update).
3030 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003031 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003032 h2c->dfl -= flen;
3033 h2c->rcvd_c += flen;
3034 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3035
3036 if (h2c->dfl > h2c->dpl) {
3037 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003038 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003039 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003040 }
3041
Willy Tarreau4a28da12018-01-04 14:41:00 +01003042 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003043 /* here we're done with the frame, all the payload (except padding) was
3044 * transferred.
3045 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003046
3047 if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) {
3048 /* emit the trailing 0 CRLF CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003049 if (b_room(csbuf) < 5) {
3050 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003051 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003052 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003053 chklen += 5;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003054 b_putblk(csbuf, "0\r\n\r\n", 5);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003055 }
3056
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003057 h2c->rcvd_c += h2c->dpl;
3058 h2c->rcvd_s += h2c->dpl;
3059 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003060 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3061
Willy Tarreau39d68502018-03-02 12:26:37 +01003062 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003063 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003064 h2s->cs->flags |= CS_FL_REOS;
3065 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003066
Willy Tarreau454b57b2018-02-26 15:50:05 +01003067 return flen + chklen;
3068 fail:
3069 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003070}
3071
Willy Tarreau5dd17352018-06-14 13:33:30 +02003072/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3073 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3074 * number of bytes sent. The caller must check the stream's status to detect
3075 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003076 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003077static 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 +02003078{
3079 struct http_hdr list[MAX_HTTP_HDR];
3080 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003081 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003082 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003083 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003084 int es_now = 0;
3085 int ret = 0;
3086 int hdr;
3087
3088 if (h2c_mux_busy(h2c, h2s)) {
3089 h2s->flags |= H2_SF_BLK_MBUSY;
3090 return 0;
3091 }
3092
Willy Tarreau44e973f2018-03-01 17:49:30 +01003093 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003094 h2c->flags |= H2_CF_MUX_MALLOC;
3095 h2s->flags |= H2_SF_BLK_MROOM;
3096 return 0;
3097 }
3098
3099 /* First, try to parse the H1 response and index it into <list>.
3100 * NOTE! Since it comes from haproxy, we *know* that a response header
3101 * block does not wrap and we can safely read it this way without
3102 * having to realign the buffer.
3103 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003104 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003105 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003106 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003107 /* incomplete or invalid response, this is abnormal coming from
3108 * haproxy and may only result in a bad errorfile or bad Lua code
3109 * so that won't be fixed, raise an error now.
3110 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003111 * FIXME: we should instead add the ability to only return a
3112 * 502 bad gateway. But in theory this is not supposed to
3113 * happen.
3114 */
3115 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3116 ret = 0;
3117 goto end;
3118 }
3119
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003120 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003121
3122 /* certain statuses have no body or an empty one, regardless of
3123 * what the headers say.
3124 */
3125 if (sl.st.status >= 100 && sl.st.status < 200) {
3126 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3127 h1m->curr_len = h1m->body_len = 0;
3128 }
3129 else if (sl.st.status == 204 || sl.st.status == 304) {
3130 /* no contents, claim c-len is present and set to zero */
3131 h1m->flags &= ~H1_MF_CHNK;
3132 h1m->flags |= H1_MF_CLEN;
3133 h1m->curr_len = h1m->body_len = 0;
3134 }
3135
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003136 chunk_reset(&outbuf);
3137
3138 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003139 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003140 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003141 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003142
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003143 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003144 break;
3145 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003146 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003147 }
3148
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003149 if (outbuf.size < 9)
3150 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003151
3152 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003153 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3154 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3155 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003156
3157 /* encode status, which necessarily is the first one */
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003158 if (outbuf.data < outbuf.size && h2s->status == 200)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003159 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003160 else if (outbuf.data < outbuf.size && h2s->status == 304)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003161 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003162 else if (unlikely(list[0].v.len != 3)) {
3163 /* this is an unparsable response */
3164 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3165 ret = 0;
3166 goto end;
3167 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003168 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003169 /* basic encoding of the status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003170 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3171 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3172 outbuf.area[outbuf.data++] = list[0].v.ptr[0];
3173 outbuf.area[outbuf.data++] = list[0].v.ptr[1];
3174 outbuf.area[outbuf.data++] = list[0].v.ptr[2];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003175 }
3176 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003177 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003178 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003179 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003180 }
3181
3182 /* encode all headers, stop at empty name */
3183 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003184 /* these ones do not exist in H2 and must be dropped. */
3185 if (isteq(list[hdr].n, ist("connection")) ||
3186 isteq(list[hdr].n, ist("proxy-connection")) ||
3187 isteq(list[hdr].n, ist("keep-alive")) ||
3188 isteq(list[hdr].n, ist("upgrade")) ||
3189 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003190 continue;
3191
3192 if (isteq(list[hdr].n, ist("")))
3193 break; // end
3194
3195 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3196 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003197 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003198 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003199 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003200 }
3201 }
3202
3203 /* we may need to add END_STREAM */
3204 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3205 es_now = 1;
3206
3207 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003208 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003209
3210 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003211 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003212
3213 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003214 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003215
3216 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003217 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003218 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003219
3220 /* for now we don't implemented CONTINUATION, so we wait for a
3221 * body or directly end in TRL2.
3222 */
3223 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003224 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003225 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003226
Willy Tarreau801250e2018-09-11 11:45:04 +02003227 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003228 h2s->flags |= H2_SF_ES_SENT;
3229 if (h2s->st == H2_SS_OPEN)
3230 h2s->st = H2_SS_HLOC;
3231 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003232 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003233 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003234 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003235 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003236 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003237 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003238 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003239 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003240 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003241
3242 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003243
3244 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003245 //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 +02003246 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003247 full:
3248 h1m_init_res(h1m);
3249 h1m->err_pos = -1; // don't care about errors on the response path
3250 h2c->flags |= H2_CF_MUX_MFULL;
3251 h2s->flags |= H2_SF_BLK_MROOM;
3252 ret = 0;
3253 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003254}
3255
Willy Tarreau5dd17352018-06-14 13:33:30 +02003256/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3257 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3258 * the number of bytes sent. The caller must check the stream's status to
3259 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003260 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003261static 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 +02003262{
3263 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003264 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003265 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003266 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003267 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003268 int es_now = 0;
3269 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003270 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003271 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003272
3273 if (h2c_mux_busy(h2c, h2s)) {
3274 h2s->flags |= H2_SF_BLK_MBUSY;
3275 goto end;
3276 }
3277
Willy Tarreau44e973f2018-03-01 17:49:30 +01003278 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003279 h2c->flags |= H2_CF_MUX_MALLOC;
3280 h2s->flags |= H2_SF_BLK_MROOM;
3281 goto end;
3282 }
3283
3284 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003285 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003286 goto end;
3287
3288 chunk_reset(&outbuf);
3289
3290 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003291 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003292 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003293 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003294
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003295 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003296 break;
3297 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003298 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003299 }
3300
3301 if (outbuf.size < 9) {
3302 h2c->flags |= H2_CF_MUX_MFULL;
3303 h2s->flags |= H2_SF_BLK_MROOM;
3304 goto end;
3305 }
3306
3307 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003308 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3309 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3310 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003311
3312 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3313 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003314 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003315 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003316 break;
3317 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003318 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003319 if ((long long)size > h1m->curr_len)
3320 size = h1m->curr_len;
3321 break;
3322 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003323 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003324 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003325 if (!ret)
3326 goto end;
3327
3328 if (ret < 0) {
3329 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003330 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003331 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3332 goto end;
3333 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003334 max -= ret;
3335 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003336 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003337 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003338 }
3339
Willy Tarreau801250e2018-09-11 11:45:04 +02003340 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003341 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003342 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003343 if (!ret)
3344 goto end;
3345
3346 if (ret < 0) {
3347 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003348 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003349 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3350 goto end;
3351 }
3352
3353 size = chunk;
3354 h1m->curr_len = chunk;
3355 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003356 max -= ret;
3357 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003358 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003359 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003360 if (!size)
3361 goto send_empty;
3362 }
3363
3364 /* in MSG_DATA state, continue below */
3365 size = h1m->curr_len;
3366 break;
3367 }
3368
3369 /* we have in <size> the exact number of bytes we need to copy from
3370 * the H1 buffer. We need to check this against the connection's and
3371 * the stream's send windows, and to ensure that this fits in the max
3372 * frame size and in the buffer's available space minus 9 bytes (for
3373 * the frame header). The connection's flow control is applied last so
3374 * that we can use a separate list of streams which are immediately
3375 * unblocked on window opening. Note: we don't implement padding.
3376 */
3377
Willy Tarreau5dd17352018-06-14 13:33:30 +02003378 if (size > max)
3379 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003380
3381 if (size > h2s->mws)
3382 size = h2s->mws;
3383
3384 if (size <= 0) {
3385 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02003386 if (h2s->send_wait) {
3387 LIST_DEL(&h2s->list);
3388 LIST_INIT(&h2s->list);
3389 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003390 goto end;
3391 }
3392
3393 if (h2c->mfs && size > h2c->mfs)
3394 size = h2c->mfs;
3395
3396 if (size + 9 > outbuf.size) {
3397 /* we have an opportunity for enlarging the too small
3398 * available space, let's try.
3399 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003400 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003401 goto realign_again;
3402 size = outbuf.size - 9;
3403 }
3404
3405 if (size <= 0) {
3406 h2c->flags |= H2_CF_MUX_MFULL;
3407 h2s->flags |= H2_SF_BLK_MROOM;
3408 goto end;
3409 }
3410
3411 if (size > h2c->mws)
3412 size = h2c->mws;
3413
3414 if (size <= 0) {
3415 h2s->flags |= H2_SF_BLK_MFCTL;
3416 goto end;
3417 }
3418
3419 /* copy whatever we can */
3420 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003421 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003422 if (ret == 1)
3423 len2 = 0;
3424
3425 if (!ret || len1 + len2 < size) {
3426 /* FIXME: must normally never happen */
3427 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3428 goto end;
3429 }
3430
3431 /* limit len1/len2 to size */
3432 if (len1 + len2 > size) {
3433 int sub = len1 + len2 - size;
3434
3435 if (len2 > sub)
3436 len2 -= sub;
3437 else {
3438 sub -= len2;
3439 len2 = 0;
3440 len1 -= sub;
3441 }
3442 }
3443
3444 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003445 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003446 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003447 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003448
3449 send_empty:
3450 /* we may need to add END_STREAM */
3451 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3452 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003453 *
3454 * FIXME: what we do here is not correct because we send end_stream
3455 * before knowing if we'll have to send a HEADERS frame for the
3456 * trailers. More importantly we're not consuming the trailing CRLF
3457 * after the end of trailers, so it will be left to the caller to
3458 * eat it. The right way to do it would be to measure trailers here
3459 * and to send ES only if there are no trailers.
3460 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003461 */
3462 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02003463 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003464 es_now = 1;
3465
3466 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003467 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003468
3469 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003470 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003471
3472 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003473 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003474
3475 /* consume incoming H1 response */
3476 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003477 max -= size;
3478 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003479 total += size;
3480 h1m->curr_len -= size;
3481 h2s->mws -= size;
3482 h2c->mws -= size;
3483
3484 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02003485 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003486 goto new_frame;
3487 }
3488 }
3489
3490 if (es_now) {
3491 if (h2s->st == H2_SS_OPEN)
3492 h2s->st = H2_SS_HLOC;
3493 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003494 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003495
Willy Tarreau35a62702018-02-27 15:37:25 +01003496 if (!(h1m->flags & H1_MF_CHNK)) {
3497 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003498 total += max;
3499 ofs += max;
3500 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003501
Willy Tarreau801250e2018-09-11 11:45:04 +02003502 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003503 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003504
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003505 h2s->flags |= H2_SF_ES_SENT;
3506 }
3507
3508 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003509 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 +02003510 return total;
3511}
3512
Olivier Houchard6ff20392018-07-17 18:46:31 +02003513/* Called from the upper layer, to subscribe to events, such as being able to send */
3514static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
3515{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003516 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003517 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003518 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003519
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003520 if (event_type & SUB_CAN_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003521 sw = param;
3522 if (!(sw->wait_reason & SUB_CAN_RECV)) {
3523 sw->wait_reason |= SUB_CAN_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003524 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003525 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003526 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003527 event_type &= ~SUB_CAN_RECV;
3528 }
3529 if (event_type & SUB_CAN_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02003530 sw = param;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003531 if (!(sw->wait_reason & SUB_CAN_SEND)) {
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003532 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003533 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003534 h2s->send_wait = sw;
3535 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
3536 if (h2s->flags & H2_SF_BLK_MFCTL)
3537 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3538 else
3539 LIST_ADDQ(&h2c->send_list, &h2s->list);
3540 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003541 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003542 event_type &= ~SUB_CAN_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003543 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003544 if (event_type != 0)
3545 return -1;
3546 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003547
3548
3549}
3550
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003551static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
3552{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003553 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003554 struct h2s *h2s = cs->ctx;
3555
3556 if (event_type & SUB_CAN_RECV) {
3557 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003558 if (h2s->recv_wait == sw) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003559 sw->wait_reason &= ~SUB_CAN_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003560 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003561 }
3562 }
3563 if (event_type & SUB_CAN_SEND) {
3564 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003565 if (h2s->send_wait == sw) {
3566 LIST_DEL(&h2s->list);
3567 LIST_INIT(&h2s->list);
3568 sw->wait_reason &= ~SUB_CAN_SEND;
3569 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003570 }
3571 }
Olivier Houchardd846c262018-10-19 17:24:29 +02003572 if (event_type & SUB_CALL_UNSUBSCRIBE) {
3573 sw = param;
3574 if (h2s->send_wait == sw) {
3575 sw->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
3576 h2s->send_wait = NULL;
3577 }
3578 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003579 return 0;
3580}
3581
3582
Olivier Houchard511efea2018-08-16 15:30:32 +02003583/* Called from the upper layer, to receive data */
3584static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3585{
Olivier Houchard638b7992018-08-16 15:41:52 +02003586 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01003587 struct h2c *h2c = h2s->h2c;
Olivier Houchard511efea2018-08-16 15:30:32 +02003588 size_t ret = 0;
3589
3590 /* transfer possibly pending data to the upper layer */
Olivier Houchard638b7992018-08-16 15:41:52 +02003591 ret = b_xfer(buf, &h2s->rxbuf, count);
Olivier Houchard511efea2018-08-16 15:30:32 +02003592
Olivier Houchard638b7992018-08-16 15:41:52 +02003593 if (b_data(&h2s->rxbuf))
Olivier Houchard511efea2018-08-16 15:30:32 +02003594 cs->flags |= CS_FL_RCV_MORE;
3595 else {
3596 cs->flags &= ~CS_FL_RCV_MORE;
3597 if (cs->flags & CS_FL_REOS)
3598 cs->flags |= CS_FL_EOS;
Olivier Houchard638b7992018-08-16 15:41:52 +02003599 if (b_size(&h2s->rxbuf)) {
3600 b_free(&h2s->rxbuf);
3601 offer_buffers(NULL, tasks_run_queue);
3602 }
Olivier Houchard511efea2018-08-16 15:30:32 +02003603 }
3604
Willy Tarreau082f5592018-11-25 08:03:32 +01003605 if (ret && h2c->dsi == h2s->id) {
3606 /* demux is blocking on this stream's buffer */
3607 h2c->flags &= ~H2_CF_DEM_SFULL;
3608 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV)) {
3609 if (h2_recv_allowed(h2c))
3610 tasklet_wakeup(h2c->wait_event.task);
3611 }
3612 }
3613
Olivier Houchard511efea2018-08-16 15:30:32 +02003614 return ret;
3615}
3616
Olivier Houchardd846c262018-10-19 17:24:29 +02003617static void h2_stop_senders(struct h2c *h2c)
3618{
3619 struct h2s *h2s, *h2s_back;
3620
3621 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
3622 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
3623 if (h2c->msi == h2s_id(h2s))
3624 continue;
3625 LIST_DEL(&h2s->list);
3626 LIST_INIT(&h2s->list);
3627 task_remove_from_task_list((struct task *)h2s->send_wait->task);
3628 h2s->send_wait->wait_reason |= SUB_CAN_SEND;
3629 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
3630 LIST_ADD(&h2c->send_list, &h2s->list);
3631 }
3632}
3633
Willy Tarreau62f52692017-10-08 23:01:42 +02003634/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003635static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02003636{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003637 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003638 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003639 size_t ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003640
Olivier Houchardd846c262018-10-19 17:24:29 +02003641 if (h2s->send_wait) {
3642 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
3643 h2s->send_wait = NULL;
3644 LIST_DEL(&h2s->list);
3645 LIST_INIT(&h2s->list);
3646 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02003647 if (h2s->h2c->st0 < H2_CS_FRAME_H)
3648 return 0;
3649
Willy Tarreau0bad0432018-06-14 16:54:01 +02003650 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01003651 h2s->flags |= H2_SF_OUTGOING_DATA;
3652
Willy Tarreaua40704a2018-09-11 13:52:04 +02003653 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02003654 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003655 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003656 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02003657 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003658 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003659 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02003660 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003661 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003662 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003663
Willy Tarreau5dd17352018-06-14 13:33:30 +02003664 if (unlikely((int)ret <= 0)) {
3665 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003666 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3667 break;
3668 }
Willy Tarreau35a62702018-02-27 15:37:25 +01003669 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02003670 total += count;
3671 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003672 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003673 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003674 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003675 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003676 cs->flags |= CS_FL_ERROR;
3677 break;
3678 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02003679
3680 total += ret;
3681 count -= ret;
3682
3683 if (h2s->st >= H2_SS_ERROR)
3684 break;
3685
3686 if (h2s->flags & H2_SF_BLK_ANY)
3687 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003688 }
3689
Willy Tarreau00610962018-07-19 10:58:28 +02003690 if (h2s->st >= H2_SS_ERROR) {
3691 /* trim any possibly pending data after we close (extra CR-LF,
3692 * unprocessed trailers, abnormal extra data, ...)
3693 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003694 total += count;
3695 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02003696 }
3697
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003698 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003699 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003700 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003701 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003702 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003703 }
3704
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003705 b_del(buf, total);
Olivier Houchardd846c262018-10-19 17:24:29 +02003706
3707 /* The mux is full, cancel the pending tasks */
3708 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
3709 (h2s->flags & H2_SF_BLK_MBUSY))
3710 h2_stop_senders(h2s->h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +02003711 if (total > 0) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003712 if (!(h2s->h2c->wait_event.wait_reason & SUB_CAN_SEND))
3713 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02003714
Olivier Houchard7505f942018-08-21 18:10:44 +02003715 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003716 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003717}
3718
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003719/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02003720static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003721{
3722 struct h2c *h2c = conn->mux_ctx;
3723 struct h2s *h2s;
3724 struct eb32_node *node;
3725 int fctl_cnt = 0;
3726 int send_cnt = 0;
3727 int tree_cnt = 0;
3728 int orph_cnt = 0;
3729
3730 if (!h2c)
3731 return;
3732
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003733 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003734 fctl_cnt++;
3735
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003736 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003737 send_cnt++;
3738
3739 node = eb32_first(&h2c->streams_by_id);
3740 while (node) {
3741 h2s = container_of(node, struct h2s, by_id);
3742 tree_cnt++;
3743 if (!h2s->cs)
3744 orph_cnt++;
3745 node = eb32_next(node);
3746 }
3747
Willy Tarreau616ac812018-07-24 14:12:42 +02003748 chunk_appendf(msg, " st0=%d err=%d maxid=%d lastid=%d flg=0x%08x nbst=%u nbcs=%u"
3749 " fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
3750 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
3751 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
3752 (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
3753 (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003754}
Willy Tarreau62f52692017-10-08 23:01:42 +02003755
3756/*******************************************************/
3757/* functions below are dedicated to the config parsers */
3758/*******************************************************/
3759
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003760/* config parser for global "tune.h2.header-table-size" */
3761static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3762 struct proxy *defpx, const char *file, int line,
3763 char **err)
3764{
3765 if (too_many_args(1, args, err, NULL))
3766 return -1;
3767
3768 h2_settings_header_table_size = atoi(args[1]);
3769 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3770 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3771 return -1;
3772 }
3773 return 0;
3774}
Willy Tarreau62f52692017-10-08 23:01:42 +02003775
Willy Tarreaue6baec02017-07-27 11:45:11 +02003776/* config parser for global "tune.h2.initial-window-size" */
3777static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3778 struct proxy *defpx, const char *file, int line,
3779 char **err)
3780{
3781 if (too_many_args(1, args, err, NULL))
3782 return -1;
3783
3784 h2_settings_initial_window_size = atoi(args[1]);
3785 if (h2_settings_initial_window_size < 0) {
3786 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3787 return -1;
3788 }
3789 return 0;
3790}
3791
Willy Tarreau5242ef82017-07-27 11:47:28 +02003792/* config parser for global "tune.h2.max-concurrent-streams" */
3793static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3794 struct proxy *defpx, const char *file, int line,
3795 char **err)
3796{
3797 if (too_many_args(1, args, err, NULL))
3798 return -1;
3799
3800 h2_settings_max_concurrent_streams = atoi(args[1]);
3801 if (h2_settings_max_concurrent_streams < 0) {
3802 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3803 return -1;
3804 }
3805 return 0;
3806}
3807
Willy Tarreau62f52692017-10-08 23:01:42 +02003808
3809/****************************************/
3810/* MUX initialization and instanciation */
3811/***************************************/
3812
3813/* The mux operations */
3814const struct mux_ops h2_ops = {
3815 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003816 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02003817 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02003818 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02003819 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003820 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02003821 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01003822 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02003823 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01003824 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01003825 .avail_streams = h2_avail_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02003826 .shutr = h2_shutr,
3827 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003828 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003829 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003830 .name = "H2",
3831};
3832
Christopher Faulet32f61c02018-04-10 14:33:41 +02003833/* PROTO selection : this mux registers PROTO token "h2" */
3834static struct mux_proto_list mux_proto_h2 =
3835 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02003836
Willy Tarreau0108d902018-11-25 19:14:37 +01003837INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
3838
Willy Tarreau62f52692017-10-08 23:01:42 +02003839/* config keyword parsers */
3840static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003841 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003842 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003843 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003844 { 0, NULL, NULL }
3845}};
3846
Willy Tarreau0108d902018-11-25 19:14:37 +01003847INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);