blob: a19a319885123ae7d9545c4153268e7be865dce6 [file] [log] [blame]
Willy Tarreau62f52692017-10-08 23:01:42 +02001/*
2 * HTTP/2 mux-demux for connections
3 *
4 * Copyright 2017 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <common/cfgparse.h>
14#include <common/config.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020015#include <common/h2.h>
Willy Tarreau13278b42017-10-13 19:23:14 +020016#include <common/hpack-dec.h>
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020017#include <common/hpack-enc.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020018#include <common/hpack-tbl.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010019#include <common/initcall.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020020#include <common/net_helper.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020021#include <proto/connection.h>
Willy Tarreau3ccf4b22017-10-13 19:07:26 +020022#include <proto/h1.h>
Willy Tarreaubcd3bb32018-12-01 18:59:00 +010023#include <proto/http_htx.h>
24#include <proto/htx.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020025#include <proto/stream.h>
Willy Tarreauea392822017-10-31 10:02:25 +010026#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020027#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020028
29
Willy Tarreau2a856182017-05-16 15:20:39 +020030/* dummy streams returned for idle and closed states */
31static const struct h2s *h2_closed_stream;
32static const struct h2s *h2_idle_stream;
33
Willy Tarreau5ab6b572017-09-22 08:05:00 +020034/* Connection flags (32 bit), in h2c->flags */
35#define H2_CF_NONE 0x00000000
36
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020037/* Flags indicating why writing to the mux is blocked. */
38#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
39#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
40#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
41
Willy Tarreau315d8072017-12-10 22:17:57 +010042/* Flags indicating why writing to the demux is blocked.
43 * The first two ones directly affect the ability for the mux to receive data
44 * from the connection. The other ones affect the mux's ability to demux
45 * received data.
46 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020047#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
48#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010049
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020050#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
51#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
52#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
53#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020054#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
55#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020056
Willy Tarreau081d4722017-05-16 21:51:05 +020057/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020058#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
59#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
60#define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake
Willy Tarreaub3fb56d2018-10-03 13:56:38 +020061#define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection
Willy Tarreau081d4722017-05-16 21:51:05 +020062
Willy Tarreau5ab6b572017-09-22 08:05:00 +020063/* H2 connection state, in h2c->st0 */
64enum h2_cs {
65 H2_CS_PREFACE, // init done, waiting for connection preface
66 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
67 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
68 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010069 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
70 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020071 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
72 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
73 H2_CS_ENTRIES // must be last
74} __attribute__((packed));
75
76/* H2 connection descriptor */
77struct h2c {
78 struct connection *conn;
79
80 enum h2_cs st0; /* mux state */
81 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
82
83 /* 16 bit hole here */
84 uint32_t flags; /* connection flags: H2_CF_* */
85 int32_t max_id; /* highest ID known on this connection, <0 before preface */
86 uint32_t rcvd_c; /* newly received data to ACK for the connection */
87 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
88
89 /* states for the demux direction */
90 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020091 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020092
93 int32_t dsi; /* demux stream ID (<0 = idle) */
94 int32_t dfl; /* demux frame length (if dsi >= 0) */
95 int8_t dft; /* demux frame type (if dsi >= 0) */
96 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +010097 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
98 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020099 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
100
101 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200102 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200103 int32_t msi; /* mux stream ID (<0 = idle) */
104 int32_t mfl; /* mux frame length (if dsi >= 0) */
105 int8_t mft; /* mux frame type (if dsi >= 0) */
106 int8_t mff; /* mux frame flags (if dsi >= 0) */
107 /* 16 bit hole here */
108 int32_t miw; /* mux initial window size for all new streams */
109 int32_t mws; /* mux window size. Can be negative. */
110 int32_t mfs; /* mux's max frame size */
111
Willy Tarreauea392822017-10-31 10:02:25 +0100112 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100113 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100114 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200115 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200116 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100117 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200118 struct eb_root streams_by_id; /* all active streams by their ID */
119 struct list send_list; /* list of blocked streams requesting to send */
120 struct list fctl_list; /* list of streams blocked by connection's fctl */
Olivier Houchardd846c262018-10-19 17:24:29 +0200121 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100122 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200123 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200124};
125
Willy Tarreau18312642017-10-11 07:57:07 +0200126/* H2 stream state, in h2s->st */
127enum h2_ss {
128 H2_SS_IDLE = 0, // idle
129 H2_SS_RLOC, // reserved(local)
130 H2_SS_RREM, // reserved(remote)
131 H2_SS_OPEN, // open
132 H2_SS_HREM, // half-closed(remote)
133 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200134 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200135 H2_SS_CLOSED, // closed
136 H2_SS_ENTRIES // must be last
137} __attribute__((packed));
138
139/* HTTP/2 stream flags (32 bit), in h2s->flags */
140#define H2_SF_NONE 0x00000000
141#define H2_SF_ES_RCVD 0x00000001
142#define H2_SF_ES_SENT 0x00000002
143
144#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
145#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
146
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200147/* stream flags indicating the reason the stream is blocked */
148#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
149#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
150#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
151#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
152#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
153
Willy Tarreau454f9052017-10-26 19:40:35 +0200154/* stream flags indicating how data is supposed to be sent */
155#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
156#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
157
158/* step we're currently in when sending chunks. This is needed because we may
159 * have to transfer chunks as large as a full buffer so there's no room left
160 * for size nor crlf around.
161 */
162#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
163#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
164#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
165
166#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
167
Willy Tarreau67434202017-11-06 20:20:51 +0100168#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100169#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100170
Willy Tarreau18312642017-10-11 07:57:07 +0200171/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
172 * it is being processed in the internal HTTP representation (H1 for now).
173 */
174struct h2s {
175 struct conn_stream *cs;
176 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200177 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200178 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200179 int32_t id; /* stream ID */
180 uint32_t flags; /* H2_SF_* */
181 int mws; /* mux window size for this stream */
182 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
183 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200184 uint16_t status; /* HTTP response status */
Olivier Houchard638b7992018-08-16 15:41:52 +0200185 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200186 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
187 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
188 struct wait_event *send_wait; /* The streeam is waiting for flow control */
189 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Willy Tarreau18312642017-10-11 07:57:07 +0200190};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200191
Willy Tarreauc6405142017-09-21 20:23:50 +0200192/* descriptor for an h2 frame header */
193struct h2_fh {
194 uint32_t len; /* length, host order, 24 bits */
195 uint32_t sid; /* stream id, host order, 31 bits */
196 uint8_t ft; /* frame type */
197 uint8_t ff; /* frame flags */
198};
199
Willy Tarreau8ceae722018-11-26 11:58:30 +0100200/* the h2c connection pool */
201DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
202
203/* the h2s stream pool */
204DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
205
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200206/* a few settings from the global section */
207static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200208static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200209static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200210
Willy Tarreau2a856182017-05-16 15:20:39 +0200211/* a dmumy closed stream */
212static const struct h2s *h2_closed_stream = &(const struct h2s){
213 .cs = NULL,
214 .h2c = NULL,
215 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100216 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100217 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200218 .id = 0,
219};
220
221/* and a dummy idle stream for use with any unannounced stream */
222static const struct h2s *h2_idle_stream = &(const struct h2s){
223 .cs = NULL,
224 .h2c = NULL,
225 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100226 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200227 .id = 0,
228};
229
Olivier Houchard9f6af332018-05-25 14:04:04 +0200230static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200231static int h2_send(struct h2c *h2c);
232static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200233static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200234static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100235static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100236static int h2_frt_decode_headers(struct h2s *h2s);
237static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200238static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200239
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200240/*****************************************************/
241/* functions below are for dynamic buffer management */
242/*****************************************************/
243
Willy Tarreau315d8072017-12-10 22:17:57 +0100244/* indicates whether or not the we may call the h2_recv() function to attempt
245 * to receive data into the buffer and/or demux pending data. The condition is
246 * a bit complex due to some API limits for now. The rules are the following :
247 * - if an error or a shutdown was detected on the connection and the buffer
248 * is empty, we must not attempt to receive
249 * - if the demux buf failed to be allocated, we must not try to receive and
250 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100251 * - if no flag indicates a blocking condition, we may attempt to receive,
252 * regardless of whether the demux buffer is full or not, so that only
253 * de demux part decides whether or not to block. This is needed because
254 * the connection API indeed prevents us from re-enabling receipt that is
255 * already enabled in a polled state, so we must always immediately stop
256 * as soon as the demux can't proceed so as never to hit an end of read
257 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100258 * - otherwise must may not attempt
259 */
260static inline int h2_recv_allowed(const struct h2c *h2c)
261{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200262 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100263 (h2c->st0 >= H2_CS_ERROR ||
264 h2c->conn->flags & CO_FL_ERROR ||
265 conn_xprt_read0_pending(h2c->conn)))
266 return 0;
267
268 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100269 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100270 return 1;
271
272 return 0;
273}
274
Willy Tarreauf2101912018-07-19 10:11:38 +0200275/* returns true if the connection has too many conn_streams attached */
276static inline int h2_has_too_many_cs(const struct h2c *h2c)
277{
278 return h2c->nb_cs >= h2_settings_max_concurrent_streams;
279}
280
Willy Tarreau44e973f2018-03-01 17:49:30 +0100281/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
282 * flags are used to figure what buffer was requested. It returns 1 if the
283 * allocation succeeds, in which case the connection is woken up, or 0 if it's
284 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200285 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100286static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200287{
288 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100289 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200290
Willy Tarreau44e973f2018-03-01 17:49:30 +0100291 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200292 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard53216e72018-10-10 15:46:36 +0200293 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200294 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200295 return 1;
296 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200297
Willy Tarreau44e973f2018-03-01 17:49:30 +0100298 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
299 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200300
301 if (h2c->flags & H2_CF_DEM_MROOM) {
302 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard53216e72018-10-10 15:46:36 +0200303 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200304 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200305 }
Willy Tarreau14398122017-09-22 14:26:04 +0200306 return 1;
307 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100308
309 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
310 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200311 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100312 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard53216e72018-10-10 15:46:36 +0200313 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200314 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau0b559072018-02-26 15:22:17 +0100315 return 1;
316 }
317
Willy Tarreau14398122017-09-22 14:26:04 +0200318 return 0;
319}
320
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200321static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200322{
323 struct buffer *buf = NULL;
324
Willy Tarreau44e973f2018-03-01 17:49:30 +0100325 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
326 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
327 h2c->buf_wait.target = h2c;
328 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100329 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100330 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100331 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200332 __conn_xprt_stop_recv(h2c->conn);
333 }
334 return buf;
335}
336
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200337static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200338{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200339 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100340 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200341 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200342 }
343}
344
Olivier Houchardd540b362018-11-05 18:37:53 +0100345static int h2_avail_streams(struct connection *conn)
346{
347 struct h2c *h2c = conn->mux_ctx;
348
349 return (h2_settings_max_concurrent_streams - h2c->nb_streams);
350}
351
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200352
Willy Tarreau62f52692017-10-08 23:01:42 +0200353/*****************************************************************/
354/* functions below are dedicated to the mux setup and management */
355/*****************************************************************/
356
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200357/* Initialize the mux once it's attached. For outgoing connections, the context
358 * is already initialized before installing the mux, so we detect incoming
359 * connections from the fact that the context is still NULL. Returns < 0 on
360 * error.
361 */
362static int h2_init(struct connection *conn, struct proxy *prx)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200363{
364 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100365 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200366
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200367 /* we don't support outgoing connections for now */
368 if (conn->mux_ctx)
369 goto fail_no_h2c;
370
Willy Tarreaubafbe012017-11-24 17:34:44 +0100371 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200372 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200373 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200374
Willy Tarreau0b37d652018-10-03 10:33:02 +0200375 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
376 if (tick_isset(prx->timeout.clientfin))
377 h2c->shut_timeout = prx->timeout.clientfin;
Willy Tarreau3f133572017-10-31 19:21:06 +0100378
Willy Tarreau0b37d652018-10-03 10:33:02 +0200379 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100380 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100381 if (tick_isset(h2c->timeout)) {
382 t = task_new(tid_bit);
383 if (!t)
384 goto fail;
385
386 h2c->task = t;
387 t->process = h2_timeout_task;
388 t->context = h2c;
389 t->expire = tick_add(now_ms, h2c->timeout);
390 }
Willy Tarreauea392822017-10-31 10:02:25 +0100391
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200392 h2c->wait_event.task = tasklet_new();
393 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200394 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200395 h2c->wait_event.task->process = h2_io_cb;
396 h2c->wait_event.task->context = h2c;
397 h2c->wait_event.wait_reason = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200398
Willy Tarreau32218eb2017-09-22 08:07:25 +0200399 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
400 if (!h2c->ddht)
401 goto fail;
402
403 /* Initialise the context. */
404 h2c->st0 = H2_CS_PREFACE;
405 h2c->conn = conn;
406 h2c->max_id = -1;
407 h2c->errcode = H2_ERR_NO_ERROR;
408 h2c->flags = H2_CF_NONE;
409 h2c->rcvd_c = 0;
410 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100411 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200412 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200413
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200414 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200415 h2c->dsi = -1;
416 h2c->msi = -1;
417 h2c->last_sid = -1;
418
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200419 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200420 h2c->miw = 65535; /* mux initial window size */
421 h2c->mws = 65535; /* mux window size */
422 h2c->mfs = 16384; /* initial max frame size */
423 h2c->streams_by_id = EB_ROOT_UNIQUE;
424 LIST_INIT(&h2c->send_list);
425 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200426 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100427 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200428 conn->mux_ctx = h2c;
429
Willy Tarreau3f133572017-10-31 19:21:06 +0100430 if (t)
431 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100432
Willy Tarreau0f383582018-10-03 14:22:21 +0200433 /* prepare to read something */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200434 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200435 return 0;
mildiscd2d7de2018-10-02 16:44:18 +0200436 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100437 if (t)
438 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200439 if (h2c->wait_event.task)
440 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100441 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200442 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200443 return -1;
444}
445
Willy Tarreau2373acc2017-10-12 17:35:14 +0200446/* returns the stream associated with id <id> or NULL if not found */
447static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
448{
449 struct eb32_node *node;
450
Willy Tarreau2a856182017-05-16 15:20:39 +0200451 if (id > h2c->max_id)
452 return (struct h2s *)h2_idle_stream;
453
Willy Tarreau2373acc2017-10-12 17:35:14 +0200454 node = eb32_lookup(&h2c->streams_by_id, id);
455 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200456 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200457
458 return container_of(node, struct h2s, by_id);
459}
460
Willy Tarreau62f52692017-10-08 23:01:42 +0200461/* release function for a connection. This one should be called to free all
462 * resources allocated to the mux.
463 */
464static void h2_release(struct connection *conn)
465{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200466 struct h2c *h2c = conn->mux_ctx;
467
468 LIST_DEL(&conn->list);
469
470 if (h2c) {
471 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200472
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100473 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100474 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100475 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200476
Willy Tarreau44e973f2018-03-01 17:49:30 +0100477 h2_release_buf(h2c, &h2c->dbuf);
478 h2_release_buf(h2c, &h2c->mbuf);
479
Willy Tarreauea392822017-10-31 10:02:25 +0100480 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200481 h2c->task->context = NULL;
482 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100483 h2c->task = NULL;
484 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200485 if (h2c->wait_event.task)
486 tasklet_free(h2c->wait_event.task);
487 if (h2c->wait_event.wait_reason != 0)
488 conn->xprt->unsubscribe(conn, h2c->wait_event.wait_reason,
489 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100490
Willy Tarreaubafbe012017-11-24 17:34:44 +0100491 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200492 }
493
494 conn->mux = NULL;
495 conn->mux_ctx = NULL;
496
497 conn_stop_tracking(conn);
498 conn_full_close(conn);
499 if (conn->destroy_cb)
500 conn->destroy_cb(conn);
501 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200502}
503
504
Willy Tarreau71681172017-10-23 14:39:06 +0200505/******************************************************/
506/* functions below are for the H2 protocol processing */
507/******************************************************/
508
509/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100510static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200511{
512 return h2s ? h2s->id : 0;
513}
514
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200515/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100516static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200517{
518 if (h2c->msi < 0)
519 return 0;
520
521 if (h2c->msi == h2s_id(h2s))
522 return 0;
523
524 return 1;
525}
526
Willy Tarreau741d6df2017-10-17 08:00:59 +0200527/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100528static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200529{
530 h2c->errcode = err;
531 h2c->st0 = H2_CS_ERROR;
532}
533
Willy Tarreau2e43f082017-10-17 08:03:59 +0200534/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100535static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200536{
Willy Tarreauab0e1da2018-10-05 10:16:37 +0200537 if (h2s->id && h2s->st < H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200538 h2s->errcode = err;
539 h2s->st = H2_SS_ERROR;
540 if (h2s->cs)
541 h2s->cs->flags |= CS_FL_ERROR;
542 }
543}
544
Willy Tarreaue4820742017-07-27 13:37:23 +0200545/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100546static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200547{
548 uint8_t *out = frame;
549
550 *out = len >> 16;
551 write_n16(out + 1, len);
552}
553
Willy Tarreau54c15062017-10-10 17:10:03 +0200554/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
555 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
556 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200557 * available in the buffer's input prior to calling this function. The buffer
558 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200559 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100560static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200561 const struct buffer *b, int o)
562{
Willy Tarreau591d4452018-06-15 17:21:00 +0200563 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 +0200564}
565
Willy Tarreau1f094672017-11-20 21:27:45 +0100566static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200567{
Willy Tarreau591d4452018-06-15 17:21:00 +0200568 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200569}
570
Willy Tarreau1f094672017-11-20 21:27:45 +0100571static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200572{
Willy Tarreau591d4452018-06-15 17:21:00 +0200573 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200574}
575
Willy Tarreau1f094672017-11-20 21:27:45 +0100576static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200577{
Willy Tarreau591d4452018-06-15 17:21:00 +0200578 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200579}
580
581
Willy Tarreau715d5312017-07-11 15:20:24 +0200582/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
583 * is not obvious. It turns out that H2 headers are neither aligned nor do they
584 * use regular sizes. And to add to the trouble, the buffer may wrap so each
585 * byte read must be checked. The header is formed like this :
586 *
587 * b0 b1 b2 b3 b4 b5..b8
588 * +----------+---------+--------+----+----+----------------------+
589 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
590 * +----------+---------+--------+----+----+----------------------+
591 *
592 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
593 * we get the sid properly aligned and ordered, and 16 bits of len properly
594 * ordered as well. The type and flags can be extracted using bit shifts from
595 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200596 * Returns zero if some bytes are missing, otherwise non-zero on success. The
597 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200598 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100599static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200600{
601 uint64_t w;
602
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200603 if (b_data(b) < 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200604 return 0;
605
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200606 w = h2_get_n64(b, 1);
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200607 h->len = *(uint8_t*)b_head(b) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200608 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
609 h->ff = w >> 32;
610 h->ft = w >> 40;
611 h->len += w >> 48;
612 return 1;
613}
614
615/* skip the next 9 bytes corresponding to the frame header possibly parsed by
616 * h2_peek_frame_hdr() above.
617 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100618static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200619{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200620 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200621}
622
623/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100624static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200625{
626 int ret;
627
628 ret = h2_peek_frame_hdr(b, h);
629 if (ret > 0)
630 h2_skip_frame_hdr(b);
631 return ret;
632}
633
Willy Tarreau00dd0782018-03-01 16:31:34 +0100634/* marks stream <h2s> as CLOSED and decrement the number of active streams for
635 * its connection if the stream was not yet closed. Please use this exclusively
636 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100637 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100638static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100639{
640 if (h2s->st != H2_SS_CLOSED)
641 h2s->h2c->nb_streams--;
642 h2s->st = H2_SS_CLOSED;
643}
644
Willy Tarreau71049cc2018-03-28 13:56:39 +0200645/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
646static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100647{
648 h2s_close(h2s);
649 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200650 if (b_size(&h2s->rxbuf)) {
651 b_free(&h2s->rxbuf);
652 offer_buffers(NULL, tasks_run_queue);
653 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200654 if (h2s->send_wait != NULL)
655 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
656 if (h2s->recv_wait != NULL)
657 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
658 /* There's no need to explicitely call unsubscribe here, the only
659 * reference left would be in the h2c send_list/fctl_list, and if
660 * we're in it, we're getting out anyway
661 */
662 LIST_DEL(&h2s->list);
663 LIST_INIT(&h2s->list);
664 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100665 pool_free(pool_head_h2s, h2s);
666}
667
Willy Tarreaua8e49542018-10-03 18:53:55 +0200668/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
669 * stream tree. In case of error, nothing is added and NULL is returned. The
670 * causes of errors can be any failed memory allocation. The caller is
671 * responsible for checking if the connection may support an extra stream
672 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200673 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200674static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200675{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200676 struct h2s *h2s;
677
Willy Tarreaubafbe012017-11-24 17:34:44 +0100678 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200679 if (!h2s)
680 goto out;
681
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200682 h2s->wait_event.task = tasklet_new();
683 if (!h2s->wait_event.task) {
684 pool_free(pool_head_h2s, h2s);
685 goto out;
686 }
687 h2s->send_wait = NULL;
688 h2s->recv_wait = NULL;
689 h2s->wait_event.task->process = h2_deferred_shut;
690 h2s->wait_event.task->context = h2s;
691 h2s->wait_event.handle = NULL;
692 h2s->wait_event.wait_reason = 0;
693 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200694 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200695 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200696 h2s->mws = h2c->miw;
697 h2s->flags = H2_SF_NONE;
698 h2s->errcode = H2_ERR_NO_ERROR;
699 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200700 h2s->status = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200701 h2s->rxbuf = BUF_NULL;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200702 h1m_init_res(&h2s->h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +0200703 h2s->h1m.err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +0200704 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200705 h2s->by_id.key = h2s->id = id;
706 h2c->max_id = id;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200707
708 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100709 h2c->nb_streams++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200710
711 return h2s;
712
713 out_free_h2s:
714 pool_free(pool_head_h2s, h2s);
715 out:
716 return NULL;
717}
718
719/* creates a new stream <id> on the h2c connection and returns it, or NULL in
720 * case of memory allocation error.
721 */
722static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
723{
724 struct session *sess = h2c->conn->owner;
725 struct conn_stream *cs;
726 struct h2s *h2s;
727
728 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
729 goto out;
730
731 h2s = h2s_new(h2c, id);
732 if (!h2s)
733 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200734
735 cs = cs_new(h2c->conn);
736 if (!cs)
737 goto out_close;
738
739 h2s->cs = cs;
740 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200741 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200742
743 if (stream_create_from_cs(cs) < 0)
744 goto out_free_cs;
745
Willy Tarreau590a0512018-09-05 11:56:48 +0200746 /* We want the accept date presented to the next stream to be the one
747 * we have now, the handshake time to be null (since the next stream
748 * is not delayed by a handshake), and the idle time to count since
749 * right now.
750 */
751 sess->accept_date = date;
752 sess->tv_accept = now;
753 sess->t_handshake = 0;
754
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200755 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200756 if (h2_has_too_many_cs(h2c))
757 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200758 return h2s;
759
760 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200761 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200762 cs_free(cs);
763 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200764 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200765 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200766 sess_log(sess);
767 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200768}
769
Willy Tarreaube5b7152017-09-25 16:25:39 +0200770/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
771 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
772 * the various settings codes.
773 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200774static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +0200775{
776 struct buffer *res;
777 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200778 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200779 int ret;
780
781 if (h2c_mux_busy(h2c, NULL)) {
782 h2c->flags |= H2_CF_DEM_MBUSY;
783 return 0;
784 }
785
Willy Tarreau44e973f2018-03-01 17:49:30 +0100786 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200787 if (!res) {
788 h2c->flags |= H2_CF_MUX_MALLOC;
789 h2c->flags |= H2_CF_DEM_MROOM;
790 return 0;
791 }
792
793 chunk_init(&buf, buf_data, sizeof(buf_data));
794 chunk_memcpy(&buf,
795 "\x00\x00\x00" /* length : 0 for now */
796 "\x04\x00" /* type : 4 (settings), flags : 0 */
797 "\x00\x00\x00\x00", /* stream ID : 0 */
798 9);
799
800 if (h2_settings_header_table_size != 4096) {
801 char str[6] = "\x00\x01"; /* header_table_size */
802
803 write_n32(str + 2, h2_settings_header_table_size);
804 chunk_memcat(&buf, str, 6);
805 }
806
807 if (h2_settings_initial_window_size != 65535) {
808 char str[6] = "\x00\x04"; /* initial_window_size */
809
810 write_n32(str + 2, h2_settings_initial_window_size);
811 chunk_memcat(&buf, str, 6);
812 }
813
814 if (h2_settings_max_concurrent_streams != 0) {
815 char str[6] = "\x00\x03"; /* max_concurrent_streams */
816
817 /* Note: 0 means "unlimited" for haproxy's config but not for
818 * the protocol, so never send this value!
819 */
820 write_n32(str + 2, h2_settings_max_concurrent_streams);
821 chunk_memcat(&buf, str, 6);
822 }
823
824 if (global.tune.bufsize != 16384) {
825 char str[6] = "\x00\x05"; /* max_frame_size */
826
827 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
828 * match bufsize - rewrite size, but at the moment it seems
829 * that clients don't take care of it.
830 */
831 write_n32(str + 2, global.tune.bufsize);
832 chunk_memcat(&buf, str, 6);
833 }
834
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200835 h2_set_frame_size(buf.area, buf.data - 9);
836 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200837 if (unlikely(ret <= 0)) {
838 if (!ret) {
839 h2c->flags |= H2_CF_MUX_MFULL;
840 h2c->flags |= H2_CF_DEM_MROOM;
841 return 0;
842 }
843 else {
844 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
845 return 0;
846 }
847 }
848 return ret;
849}
850
Willy Tarreau52eed752017-09-22 15:05:09 +0200851/* Try to receive a connection preface, then upon success try to send our
852 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
853 * missing data. It may return an error in h2c.
854 */
855static int h2c_frt_recv_preface(struct h2c *h2c)
856{
857 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200858 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200859
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200860 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200861
862 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +0200863 if (ret1 < 0)
864 sess_log(h2c->conn->owner);
865
Willy Tarreau52eed752017-09-22 15:05:09 +0200866 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
867 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
868 return 0;
869 }
870
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200871 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200872 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200873 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200874
Willy Tarreaube5b7152017-09-25 16:25:39 +0200875 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200876}
877
Willy Tarreau081d4722017-05-16 21:51:05 +0200878/* try to send a GOAWAY frame on the connection to report an error or a graceful
879 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
880 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
881 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
882 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
883 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
884 * on unrecoverable failure. It will not attempt to send one again in this last
885 * case so that it is safe to use h2c_error() to report such errors.
886 */
887static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
888{
889 struct buffer *res;
890 char str[17];
891 int ret;
892
893 if (h2c->flags & H2_CF_GOAWAY_FAILED)
894 return 1; // claim that it worked
895
896 if (h2c_mux_busy(h2c, h2s)) {
897 if (h2s)
898 h2s->flags |= H2_SF_BLK_MBUSY;
899 else
900 h2c->flags |= H2_CF_DEM_MBUSY;
901 return 0;
902 }
903
Willy Tarreau44e973f2018-03-01 17:49:30 +0100904 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +0200905 if (!res) {
906 h2c->flags |= H2_CF_MUX_MALLOC;
907 if (h2s)
908 h2s->flags |= H2_SF_BLK_MROOM;
909 else
910 h2c->flags |= H2_CF_DEM_MROOM;
911 return 0;
912 }
913
914 /* len: 8, type: 7, flags: none, sid: 0 */
915 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
916
917 if (h2c->last_sid < 0)
918 h2c->last_sid = h2c->max_id;
919
920 write_n32(str + 9, h2c->last_sid);
921 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200922 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +0200923 if (unlikely(ret <= 0)) {
924 if (!ret) {
925 h2c->flags |= H2_CF_MUX_MFULL;
926 if (h2s)
927 h2s->flags |= H2_SF_BLK_MROOM;
928 else
929 h2c->flags |= H2_CF_DEM_MROOM;
930 return 0;
931 }
932 else {
933 /* we cannot report this error using GOAWAY, so we mark
934 * it and claim a success.
935 */
936 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
937 h2c->flags |= H2_CF_GOAWAY_FAILED;
938 return 1;
939 }
940 }
941 h2c->flags |= H2_CF_GOAWAY_SENT;
942 return ret;
943}
944
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100945/* Try to send an RST_STREAM frame on the connection for the indicated stream
946 * during mux operations. This stream must be valid and cannot be closed
947 * already. h2s->id will be used for the stream ID and h2s->errcode will be
948 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
949 * not yet.
950 *
951 * Returns > 0 on success or zero if nothing was done. In case of lack of room
952 * to write the message, it subscribes the stream to future notifications.
953 */
954static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
955{
956 struct buffer *res;
957 char str[13];
958 int ret;
959
960 if (!h2s || h2s->st == H2_SS_CLOSED)
961 return 1;
962
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100963 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
964 * RST_STREAM in response to a RST_STREAM frame.
965 */
966 if (h2c->dft == H2_FT_RST_STREAM) {
967 ret = 1;
968 goto ignore;
969 }
970
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100971 if (h2c_mux_busy(h2c, h2s)) {
972 h2s->flags |= H2_SF_BLK_MBUSY;
973 return 0;
974 }
975
Willy Tarreau44e973f2018-03-01 17:49:30 +0100976 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100977 if (!res) {
978 h2c->flags |= H2_CF_MUX_MALLOC;
979 h2s->flags |= H2_SF_BLK_MROOM;
980 return 0;
981 }
982
983 /* len: 4, type: 3, flags: none */
984 memcpy(str, "\x00\x00\x04\x03\x00", 5);
985 write_n32(str + 5, h2s->id);
986 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200987 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100988
989 if (unlikely(ret <= 0)) {
990 if (!ret) {
991 h2c->flags |= H2_CF_MUX_MFULL;
992 h2s->flags |= H2_SF_BLK_MROOM;
993 return 0;
994 }
995 else {
996 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
997 return 0;
998 }
999 }
1000
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001001 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001002 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001003 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001004 return ret;
1005}
1006
1007/* Try to send an RST_STREAM frame on the connection for the stream being
1008 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
1009 * error code unless the stream's state already is IDLE or CLOSED in which
1010 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
1011 * it was not yet.
1012 *
1013 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1014 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +02001015 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001016 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001017 */
1018static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1019{
1020 struct buffer *res;
1021 char str[13];
1022 int ret;
1023
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001024 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1025 * RST_STREAM in response to a RST_STREAM frame.
1026 */
1027 if (h2c->dft == H2_FT_RST_STREAM) {
1028 ret = 1;
1029 goto ignore;
1030 }
1031
Willy Tarreau27a84c92017-10-17 08:10:17 +02001032 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001033 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001034 return 0;
1035 }
1036
Willy Tarreau44e973f2018-03-01 17:49:30 +01001037 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001038 if (!res) {
1039 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001040 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001041 return 0;
1042 }
1043
1044 /* len: 4, type: 3, flags: none */
1045 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001046
Willy Tarreau27a84c92017-10-17 08:10:17 +02001047 write_n32(str + 5, h2c->dsi);
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001048 write_n32(str + 9, h2s->id ? h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001049 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001050
Willy Tarreau27a84c92017-10-17 08:10:17 +02001051 if (unlikely(ret <= 0)) {
1052 if (!ret) {
1053 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001054 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001055 return 0;
1056 }
1057 else {
1058 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1059 return 0;
1060 }
1061 }
1062
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001063 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001064 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001065 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001066 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001067 }
1068
Willy Tarreau27a84c92017-10-17 08:10:17 +02001069 return ret;
1070}
1071
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001072/* try to send an empty DATA frame with the ES flag set to notify about the
1073 * end of stream and match a shutdown(write). If an ES was already sent as
1074 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1075 * on success or zero if nothing was done. In case of lack of room to write the
1076 * message, it subscribes the requesting stream to future notifications.
1077 */
1078static int h2_send_empty_data_es(struct h2s *h2s)
1079{
1080 struct h2c *h2c = h2s->h2c;
1081 struct buffer *res;
1082 char str[9];
1083 int ret;
1084
Willy Tarreau721c9742017-11-07 11:05:42 +01001085 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001086 return 1;
1087
1088 if (h2c_mux_busy(h2c, h2s)) {
1089 h2s->flags |= H2_SF_BLK_MBUSY;
1090 return 0;
1091 }
1092
Willy Tarreau44e973f2018-03-01 17:49:30 +01001093 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001094 if (!res) {
1095 h2c->flags |= H2_CF_MUX_MALLOC;
1096 h2s->flags |= H2_SF_BLK_MROOM;
1097 return 0;
1098 }
1099
1100 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1101 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1102 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001103 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001104 if (likely(ret > 0)) {
1105 h2s->flags |= H2_SF_ES_SENT;
1106 }
1107 else if (!ret) {
1108 h2c->flags |= H2_CF_MUX_MFULL;
1109 h2s->flags |= H2_SF_BLK_MROOM;
1110 return 0;
1111 }
1112 else {
1113 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1114 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001115 }
1116 return ret;
1117}
1118
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001119/* wake the streams attached to the connection, whose id is greater than <last>,
1120 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001121 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1122 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001123 */
1124static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1125{
1126 struct eb32_node *node;
1127 struct h2s *h2s;
1128
1129 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1130 flags |= CS_FL_ERROR;
1131
1132 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001133 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001134
1135 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1136 while (node) {
1137 h2s = container_of(node, struct h2s, by_id);
1138 if (h2s->id <= last)
1139 break;
1140 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001141
1142 if (!h2s->cs) {
1143 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001144 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001145 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001146 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001147
1148 h2s->cs->flags |= flags;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001149 if (h2s->recv_wait) {
1150 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001151 sw->wait_reason &= ~SUB_CAN_RECV;
1152 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001153 h2s->recv_wait = NULL;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02001154 } else if (h2s->cs->data_cb->wake != NULL)
1155 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001156
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001157 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1158 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001159 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001160 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001161 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001162 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001163 }
1164}
1165
Willy Tarreau3421aba2017-07-27 15:41:03 +02001166/* Increase all streams' outgoing window size by the difference passed in
1167 * argument. This is needed upon receipt of the settings frame if the initial
1168 * window size is different. The difference may be negative and the resulting
1169 * window size as well, for the time it takes to receive some window updates.
1170 */
1171static void h2c_update_all_ws(struct h2c *h2c, int diff)
1172{
1173 struct h2s *h2s;
1174 struct eb32_node *node;
1175
1176 if (!diff)
1177 return;
1178
1179 node = eb32_first(&h2c->streams_by_id);
1180 while (node) {
1181 h2s = container_of(node, struct h2s, by_id);
1182 h2s->mws += diff;
1183 node = eb32_next(node);
1184 }
1185}
1186
1187/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1188 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1189 * return an error in h2c. Described in RFC7540#6.5.
1190 */
1191static int h2c_handle_settings(struct h2c *h2c)
1192{
1193 unsigned int offset;
1194 int error;
1195
1196 if (h2c->dff & H2_F_SETTINGS_ACK) {
1197 if (h2c->dfl) {
1198 error = H2_ERR_FRAME_SIZE_ERROR;
1199 goto fail;
1200 }
1201 return 1;
1202 }
1203
1204 if (h2c->dsi != 0) {
1205 error = H2_ERR_PROTOCOL_ERROR;
1206 goto fail;
1207 }
1208
1209 if (h2c->dfl % 6) {
1210 error = H2_ERR_FRAME_SIZE_ERROR;
1211 goto fail;
1212 }
1213
1214 /* that's the limit we can process */
1215 if (h2c->dfl > global.tune.bufsize) {
1216 error = H2_ERR_FRAME_SIZE_ERROR;
1217 goto fail;
1218 }
1219
1220 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001221 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001222 return 0;
1223
1224 /* parse the frame */
1225 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001226 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1227 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001228
1229 switch (type) {
1230 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1231 /* we need to update all existing streams with the
1232 * difference from the previous iws.
1233 */
1234 if (arg < 0) { // RFC7540#6.5.2
1235 error = H2_ERR_FLOW_CONTROL_ERROR;
1236 goto fail;
1237 }
1238 h2c_update_all_ws(h2c, arg - h2c->miw);
1239 h2c->miw = arg;
1240 break;
1241 case H2_SETTINGS_MAX_FRAME_SIZE:
1242 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1243 error = H2_ERR_PROTOCOL_ERROR;
1244 goto fail;
1245 }
1246 h2c->mfs = arg;
1247 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001248 case H2_SETTINGS_ENABLE_PUSH:
1249 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1250 error = H2_ERR_PROTOCOL_ERROR;
1251 goto fail;
1252 }
1253 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001254 }
1255 }
1256
1257 /* need to ACK this frame now */
1258 h2c->st0 = H2_CS_FRAME_A;
1259 return 1;
1260 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001261 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001262 h2c_error(h2c, error);
1263 return 0;
1264}
1265
1266/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1267 * success or one of the h2_status values.
1268 */
1269static int h2c_ack_settings(struct h2c *h2c)
1270{
1271 struct buffer *res;
1272 char str[9];
1273 int ret = -1;
1274
1275 if (h2c_mux_busy(h2c, NULL)) {
1276 h2c->flags |= H2_CF_DEM_MBUSY;
1277 return 0;
1278 }
1279
Willy Tarreau44e973f2018-03-01 17:49:30 +01001280 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001281 if (!res) {
1282 h2c->flags |= H2_CF_MUX_MALLOC;
1283 h2c->flags |= H2_CF_DEM_MROOM;
1284 return 0;
1285 }
1286
1287 memcpy(str,
1288 "\x00\x00\x00" /* length : 0 (no data) */
1289 "\x04" "\x01" /* type : 4, flags : ACK */
1290 "\x00\x00\x00\x00" /* stream ID */, 9);
1291
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001292 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001293 if (unlikely(ret <= 0)) {
1294 if (!ret) {
1295 h2c->flags |= H2_CF_MUX_MFULL;
1296 h2c->flags |= H2_CF_DEM_MROOM;
1297 return 0;
1298 }
1299 else {
1300 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1301 return 0;
1302 }
1303 }
1304 return ret;
1305}
1306
Willy Tarreaucf68c782017-10-10 17:11:41 +02001307/* processes a PING frame and schedules an ACK if needed. The caller must pass
1308 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1309 * missing data. It may return an error in h2c.
1310 */
1311static int h2c_handle_ping(struct h2c *h2c)
1312{
1313 /* frame length must be exactly 8 */
1314 if (h2c->dfl != 8) {
1315 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1316 return 0;
1317 }
1318
1319 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001320 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001321 h2c->st0 = H2_CS_FRAME_A;
1322 return 1;
1323}
1324
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001325/* Try to send a window update for stream id <sid> and value <increment>.
1326 * Returns > 0 on success or zero on missing room or failure. It may return an
1327 * error in h2c.
1328 */
1329static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1330{
1331 struct buffer *res;
1332 char str[13];
1333 int ret = -1;
1334
1335 if (h2c_mux_busy(h2c, NULL)) {
1336 h2c->flags |= H2_CF_DEM_MBUSY;
1337 return 0;
1338 }
1339
Willy Tarreau44e973f2018-03-01 17:49:30 +01001340 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001341 if (!res) {
1342 h2c->flags |= H2_CF_MUX_MALLOC;
1343 h2c->flags |= H2_CF_DEM_MROOM;
1344 return 0;
1345 }
1346
1347 /* length: 4, type: 8, flags: none */
1348 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1349 write_n32(str + 5, sid);
1350 write_n32(str + 9, increment);
1351
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001352 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001353
1354 if (unlikely(ret <= 0)) {
1355 if (!ret) {
1356 h2c->flags |= H2_CF_MUX_MFULL;
1357 h2c->flags |= H2_CF_DEM_MROOM;
1358 return 0;
1359 }
1360 else {
1361 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1362 return 0;
1363 }
1364 }
1365 return ret;
1366}
1367
1368/* try to send pending window update for the connection. It's safe to call it
1369 * with no pending updates. Returns > 0 on success or zero on missing room or
1370 * failure. It may return an error in h2c.
1371 */
1372static int h2c_send_conn_wu(struct h2c *h2c)
1373{
1374 int ret = 1;
1375
1376 if (h2c->rcvd_c <= 0)
1377 return 1;
1378
1379 /* send WU for the connection */
1380 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1381 if (ret > 0)
1382 h2c->rcvd_c = 0;
1383
1384 return ret;
1385}
1386
1387/* try to send pending window update for the current dmux stream. It's safe to
1388 * call it with no pending updates. Returns > 0 on success or zero on missing
1389 * room or failure. It may return an error in h2c.
1390 */
1391static int h2c_send_strm_wu(struct h2c *h2c)
1392{
1393 int ret = 1;
1394
1395 if (h2c->rcvd_s <= 0)
1396 return 1;
1397
1398 /* send WU for the stream */
1399 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1400 if (ret > 0)
1401 h2c->rcvd_s = 0;
1402
1403 return ret;
1404}
1405
Willy Tarreaucf68c782017-10-10 17:11:41 +02001406/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1407 * success, 0 on missing data or one of the h2_status values.
1408 */
1409static int h2c_ack_ping(struct h2c *h2c)
1410{
1411 struct buffer *res;
1412 char str[17];
1413 int ret = -1;
1414
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001415 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001416 return 0;
1417
1418 if (h2c_mux_busy(h2c, NULL)) {
1419 h2c->flags |= H2_CF_DEM_MBUSY;
1420 return 0;
1421 }
1422
Willy Tarreau44e973f2018-03-01 17:49:30 +01001423 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001424 if (!res) {
1425 h2c->flags |= H2_CF_MUX_MALLOC;
1426 h2c->flags |= H2_CF_DEM_MROOM;
1427 return 0;
1428 }
1429
1430 memcpy(str,
1431 "\x00\x00\x08" /* length : 8 (same payload) */
1432 "\x06" "\x01" /* type : 6, flags : ACK */
1433 "\x00\x00\x00\x00" /* stream ID */, 9);
1434
1435 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001436 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001437
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001438 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001439 if (unlikely(ret <= 0)) {
1440 if (!ret) {
1441 h2c->flags |= H2_CF_MUX_MFULL;
1442 h2c->flags |= H2_CF_DEM_MROOM;
1443 return 0;
1444 }
1445 else {
1446 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1447 return 0;
1448 }
1449 }
1450 return ret;
1451}
1452
Willy Tarreau26f95952017-07-27 17:18:30 +02001453/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1454 * Returns > 0 on success or zero on missing data. It may return an error in
1455 * h2c or h2s. Described in RFC7540#6.9.
1456 */
1457static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1458{
1459 int32_t inc;
1460 int error;
1461
1462 if (h2c->dfl != 4) {
1463 error = H2_ERR_FRAME_SIZE_ERROR;
1464 goto conn_err;
1465 }
1466
1467 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001468 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001469 return 0;
1470
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001471 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001472
1473 if (h2c->dsi != 0) {
1474 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001475
1476 /* it's not an error to receive WU on a closed stream */
1477 if (h2s->st == H2_SS_CLOSED)
1478 return 1;
1479
1480 if (!inc) {
1481 error = H2_ERR_PROTOCOL_ERROR;
1482 goto strm_err;
1483 }
1484
1485 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1486 error = H2_ERR_FLOW_CONTROL_ERROR;
1487 goto strm_err;
1488 }
1489
1490 h2s->mws += inc;
1491 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1492 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001493 if (h2s->send_wait)
1494 LIST_ADDQ(&h2c->send_list, &h2s->list);
1495
Willy Tarreau26f95952017-07-27 17:18:30 +02001496 }
1497 }
1498 else {
1499 /* connection window update */
1500 if (!inc) {
1501 error = H2_ERR_PROTOCOL_ERROR;
1502 goto conn_err;
1503 }
1504
1505 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1506 error = H2_ERR_FLOW_CONTROL_ERROR;
1507 goto conn_err;
1508 }
1509
1510 h2c->mws += inc;
1511 }
1512
1513 return 1;
1514
1515 conn_err:
1516 h2c_error(h2c, error);
1517 return 0;
1518
1519 strm_err:
1520 if (h2s) {
1521 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001522 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001523 }
1524 else
1525 h2c_error(h2c, error);
1526 return 0;
1527}
1528
Willy Tarreaue96b0922017-10-30 00:28:29 +01001529/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1530 * the last ID. Returns > 0 on success or zero on missing data. It may return
1531 * an error in h2c. Described in RFC7540#6.8.
1532 */
1533static int h2c_handle_goaway(struct h2c *h2c)
1534{
1535 int error;
1536 int last;
1537
1538 if (h2c->dsi != 0) {
1539 error = H2_ERR_PROTOCOL_ERROR;
1540 goto conn_err;
1541 }
1542
1543 if (h2c->dfl < 8) {
1544 error = H2_ERR_FRAME_SIZE_ERROR;
1545 goto conn_err;
1546 }
1547
1548 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001549 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001550 return 0;
1551
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001552 last = h2_get_n32(&h2c->dbuf, 0);
1553 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001554 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001555 if (h2c->last_sid < 0)
1556 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001557 return 1;
1558
1559 conn_err:
1560 h2c_error(h2c, error);
1561 return 0;
1562}
1563
Willy Tarreau92153fc2017-12-03 19:46:19 +01001564/* processes a PRIORITY frame, and either skips it or rejects if it is
1565 * invalid. Returns > 0 on success or zero on missing data. It may return
1566 * an error in h2c. Described in RFC7540#6.3.
1567 */
1568static int h2c_handle_priority(struct h2c *h2c)
1569{
1570 int error;
1571
1572 if (h2c->dsi == 0) {
1573 error = H2_ERR_PROTOCOL_ERROR;
1574 goto conn_err;
1575 }
1576
1577 if (h2c->dfl != 5) {
1578 error = H2_ERR_FRAME_SIZE_ERROR;
1579 goto conn_err;
1580 }
1581
1582 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001583 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001584 return 0;
1585
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001586 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001587 /* 7540#5.3 : can't depend on itself */
1588 error = H2_ERR_PROTOCOL_ERROR;
1589 goto conn_err;
1590 }
1591 return 1;
1592
1593 conn_err:
1594 h2c_error(h2c, error);
1595 return 0;
1596}
1597
Willy Tarreaucd234e92017-08-18 10:59:39 +02001598/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1599 * Returns > 0 on success or zero on missing data. It may return an error in
1600 * h2c. Described in RFC7540#6.4.
1601 */
1602static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1603{
1604 int error;
1605
1606 if (h2c->dsi == 0) {
1607 error = H2_ERR_PROTOCOL_ERROR;
1608 goto conn_err;
1609 }
1610
Willy Tarreaucd234e92017-08-18 10:59:39 +02001611 if (h2c->dfl != 4) {
1612 error = H2_ERR_FRAME_SIZE_ERROR;
1613 goto conn_err;
1614 }
1615
1616 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001617 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001618 return 0;
1619
1620 /* late RST, already handled */
1621 if (h2s->st == H2_SS_CLOSED)
1622 return 1;
1623
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001624 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001625 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001626
1627 if (h2s->cs) {
Willy Tarreau2c096c32018-09-12 09:45:54 +02001628 h2s->cs->flags |= CS_FL_REOS | CS_FL_ERROR;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001629 if (h2s->recv_wait) {
1630 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001631
1632 sw->wait_reason &= ~SUB_CAN_RECV;
1633 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001634 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001635 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02001636 }
1637
1638 h2s->flags |= H2_SF_RST_RCVD;
1639 return 1;
1640
1641 conn_err:
1642 h2c_error(h2c, error);
1643 return 0;
1644}
1645
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001646/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1647 * It may return an error in h2c or h2s. The caller must consider that the
1648 * return value is the new h2s in case one was allocated (most common case).
1649 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001650 * errors here are reported as connection errors since it's impossible to
1651 * recover from such errors after the compression context has been altered.
1652 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001653static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001654{
1655 int error;
1656
1657 if (!h2c->dfl) {
1658 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001659 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001660 goto strm_err;
1661 }
1662
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001663 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001664 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001665
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001666 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001667 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001668
Willy Tarreauf2101912018-07-19 10:11:38 +02001669 if (h2c->flags & H2_CF_DEM_TOOMANY)
1670 return 0; // too many cs still present
1671
Willy Tarreau13278b42017-10-13 19:23:14 +02001672 /* now either the frame is complete or the buffer is complete */
1673 if (h2s->st != H2_SS_IDLE) {
1674 /* FIXME: stream already exists, this is only allowed for
1675 * trailers (not supported for now).
1676 */
1677 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001678 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001679 goto conn_err;
1680 }
1681 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1682 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1683 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001684 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001685 goto conn_err;
1686 }
1687
Willy Tarreau22de8d32018-09-05 19:55:58 +02001688 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001689 * positively from h2c_frt_stream_new(), the stream will report the error,
1690 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001691 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001692 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001693 if (!h2s) {
1694 error = H2_ERR_INTERNAL_ERROR;
1695 goto conn_err;
1696 }
1697
1698 h2s->st = H2_SS_OPEN;
1699 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1700 h2s->st = H2_SS_HREM;
1701 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001702 /* note: cs cannot be null for now (just created above) */
1703 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001704 }
1705
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001706 if (!h2_frt_decode_headers(h2s))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001707 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001708
Willy Tarreau8f650c32017-11-21 19:36:21 +01001709 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001710 return NULL;
Willy Tarreau8f650c32017-11-21 19:36:21 +01001711
Willy Tarreau721c9742017-11-07 11:05:42 +01001712 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001713 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001714 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001715 }
1716 else {
1717 /* update the max stream ID if the request is being processed */
1718 if (h2s->id > h2c->max_id)
1719 h2c->max_id = h2s->id;
1720 }
1721
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001722 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001723
1724 conn_err:
1725 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001726 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001727
1728 strm_err:
1729 if (h2s) {
1730 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001731 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001732 }
1733 else
1734 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001735 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001736}
1737
Willy Tarreau454f9052017-10-26 19:40:35 +02001738/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1739 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1740 */
1741static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1742{
1743 int error;
1744
1745 /* note that empty DATA frames are perfectly valid and sometimes used
1746 * to signal an end of stream (with the ES flag).
1747 */
1748
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001749 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001750 return 0; // empty buffer
1751
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001752 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001753 return 0; // incomplete frame
1754
1755 /* now either the frame is complete or the buffer is complete */
1756
1757 if (!h2c->dsi) {
1758 /* RFC7540#6.1 */
1759 error = H2_ERR_PROTOCOL_ERROR;
1760 goto conn_err;
1761 }
1762
1763 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1764 /* RFC7540#6.1 */
1765 error = H2_ERR_STREAM_CLOSED;
1766 goto strm_err;
1767 }
1768
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001769 if (!h2_frt_transfer_data(h2s))
1770 return 0;
1771
Willy Tarreau454f9052017-10-26 19:40:35 +02001772 /* call the upper layers to process the frame, then let the upper layer
1773 * notify the stream about any change.
1774 */
1775 if (!h2s->cs) {
1776 error = H2_ERR_STREAM_CLOSED;
1777 goto strm_err;
1778 }
1779
Willy Tarreau8f650c32017-11-21 19:36:21 +01001780 if (h2c->st0 >= H2_CS_ERROR)
1781 return 0;
1782
Willy Tarreau721c9742017-11-07 11:05:42 +01001783 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001784 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001785 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001786 }
1787
1788 /* check for completion : the callee will change this to FRAME_A or
1789 * FRAME_H once done.
1790 */
1791 if (h2c->st0 == H2_CS_FRAME_P)
1792 return 0;
1793
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001794
1795 /* last frame */
1796 if (h2c->dff & H2_F_DATA_END_STREAM) {
1797 h2s->st = H2_SS_HREM;
1798 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001799 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001800 }
1801
Willy Tarreau454f9052017-10-26 19:40:35 +02001802 return 1;
1803
1804 conn_err:
1805 h2c_error(h2c, error);
1806 return 0;
1807
1808 strm_err:
1809 if (h2s) {
1810 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001811 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001812 }
1813 else
1814 h2c_error(h2c, error);
1815 return 0;
1816}
1817
Willy Tarreaubc933932017-10-09 16:21:43 +02001818/* process Rx frames to be demultiplexed */
1819static void h2_process_demux(struct h2c *h2c)
1820{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001821 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001822
Willy Tarreau081d4722017-05-16 21:51:05 +02001823 if (h2c->st0 >= H2_CS_ERROR)
1824 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001825
1826 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1827 if (h2c->st0 == H2_CS_PREFACE) {
1828 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1829 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001830 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02001831 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001832 sess_log(h2c->conn->owner);
1833 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001834 goto fail;
1835 }
1836
1837 h2c->max_id = 0;
1838 h2c->st0 = H2_CS_SETTINGS1;
1839 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001840
1841 if (h2c->st0 == H2_CS_SETTINGS1) {
1842 struct h2_fh hdr;
1843
1844 /* ensure that what is pending is a valid SETTINGS frame
1845 * without an ACK.
1846 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001847 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001848 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001849 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001850 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001851 sess_log(h2c->conn->owner);
1852 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001853 goto fail;
1854 }
1855
1856 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1857 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1858 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1859 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001860 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001861 goto fail;
1862 }
1863
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001864 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001865 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1866 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1867 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001868 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001869 goto fail;
1870 }
1871
1872 /* that's OK, switch to FRAME_P to process it */
1873 h2c->dfl = hdr.len;
1874 h2c->dsi = hdr.sid;
1875 h2c->dft = hdr.ft;
1876 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001877 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001878 h2c->st0 = H2_CS_FRAME_P;
1879 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001880 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001881
1882 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001883 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001884 int ret = 0;
1885
1886 if (h2c->st0 >= H2_CS_ERROR)
1887 break;
1888
1889 if (h2c->st0 == H2_CS_FRAME_H) {
1890 struct h2_fh hdr;
1891
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001892 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02001893 break;
1894
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001895 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001896 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1897 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001898 if (!h2c->nb_streams) {
1899 /* only log if no other stream can report the error */
1900 sess_log(h2c->conn->owner);
1901 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001902 break;
1903 }
1904
1905 h2c->dfl = hdr.len;
1906 h2c->dsi = hdr.sid;
1907 h2c->dft = hdr.ft;
1908 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001909 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001910 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001911 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001912 }
1913
1914 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001915 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
1916
Olivier Houchard638b7992018-08-16 15:41:52 +02001917 if (tmp_h2s != h2s && h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001918 /* we may have to signal the upper layers */
1919 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001920 if (h2s->recv_wait) {
1921 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
1922 tasklet_wakeup(h2s->recv_wait->task);
1923 h2s->recv_wait = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001924 }
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001925 }
1926 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001927
Willy Tarreaud7901432017-12-29 11:34:40 +01001928 if (h2c->st0 == H2_CS_FRAME_E)
1929 goto strm_err;
1930
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001931 if (h2s->st == H2_SS_IDLE &&
1932 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1933 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1934 * this state MUST be treated as a connection error
1935 */
1936 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1937 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001938 if (!h2c->nb_streams) {
1939 /* only log if no other stream can report the error */
1940 sess_log(h2c->conn->owner);
1941 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001942 break;
1943 }
1944
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001945 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1946 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1947 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1948 * this state MUST be treated as a stream error
1949 */
1950 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1951 goto strm_err;
1952 }
1953
Willy Tarreauab837502017-12-27 15:07:30 +01001954 /* Below the management of frames received in closed state is a
1955 * bit hackish because the spec makes strong differences between
1956 * streams closed by receiving RST, sending RST, and seeing ES
1957 * in both directions. In addition to this, the creation of a
1958 * new stream reusing the identifier of a closed one will be
1959 * detected here. Given that we cannot keep track of all closed
1960 * streams forever, we consider that unknown closed streams were
1961 * closed on RST received, which allows us to respond with an
1962 * RST without breaking the connection (eg: to abort a transfer).
1963 * Some frames have to be silently ignored as well.
1964 */
1965 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1966 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1967 /* #5.1.1: The identifier of a newly
1968 * established stream MUST be numerically
1969 * greater than all streams that the initiating
1970 * endpoint has opened or reserved. This
1971 * governs streams that are opened using a
1972 * HEADERS frame and streams that are reserved
1973 * using PUSH_PROMISE. An endpoint that
1974 * receives an unexpected stream identifier
1975 * MUST respond with a connection error.
1976 */
1977 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1978 goto strm_err;
1979 }
1980
1981 if (h2s->flags & H2_SF_RST_RCVD) {
1982 /* RFC7540#5.1:closed: an endpoint that
1983 * receives any frame other than PRIORITY after
1984 * receiving a RST_STREAM MUST treat that as a
1985 * stream error of type STREAM_CLOSED.
1986 *
1987 * Note that old streams fall into this category
1988 * and will lead to an RST being sent.
1989 */
1990 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1991 h2c->st0 = H2_CS_FRAME_E;
1992 goto strm_err;
1993 }
1994
1995 /* RFC7540#5.1:closed: if this state is reached as a
1996 * result of sending a RST_STREAM frame, the peer that
1997 * receives the RST_STREAM might have already sent
1998 * frames on the stream that cannot be withdrawn. An
1999 * endpoint MUST ignore frames that it receives on
2000 * closed streams after it has sent a RST_STREAM
2001 * frame. An endpoint MAY choose to limit the period
2002 * over which it ignores frames and treat frames that
2003 * arrive after this time as being in error.
2004 */
2005 if (!(h2s->flags & H2_SF_RST_SENT)) {
2006 /* RFC7540#5.1:closed: any frame other than
2007 * PRIO/WU/RST in this state MUST be treated as
2008 * a connection error
2009 */
2010 if (h2c->dft != H2_FT_RST_STREAM &&
2011 h2c->dft != H2_FT_PRIORITY &&
2012 h2c->dft != H2_FT_WINDOW_UPDATE) {
2013 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2014 goto strm_err;
2015 }
2016 }
2017 }
2018
Willy Tarreauc0da1962017-10-30 18:38:00 +01002019#if 0
2020 // problem below: it is not possible to completely ignore such
2021 // streams as we need to maintain the compression state as well
2022 // and for this we need to completely process these frames (eg:
2023 // HEADERS frames) as well as counting DATA frames to emit
2024 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2025 // This is a typical case of layer violation where the
2026 // transported contents are critical to the connection's
2027 // validity and must be ignored at the same time :-(
2028
2029 /* graceful shutdown, ignore streams whose ID is higher than
2030 * the one advertised in GOAWAY. RFC7540#6.8.
2031 */
2032 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002033 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2034 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002035 h2c->dfl -= ret;
2036 ret = h2c->dfl == 0;
2037 goto strm_err;
2038 }
2039#endif
2040
Willy Tarreau7e98c052017-10-10 15:56:59 +02002041 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002042 case H2_FT_SETTINGS:
2043 if (h2c->st0 == H2_CS_FRAME_P)
2044 ret = h2c_handle_settings(h2c);
2045
2046 if (h2c->st0 == H2_CS_FRAME_A)
2047 ret = h2c_ack_settings(h2c);
2048 break;
2049
Willy Tarreaucf68c782017-10-10 17:11:41 +02002050 case H2_FT_PING:
2051 if (h2c->st0 == H2_CS_FRAME_P)
2052 ret = h2c_handle_ping(h2c);
2053
2054 if (h2c->st0 == H2_CS_FRAME_A)
2055 ret = h2c_ack_ping(h2c);
2056 break;
2057
Willy Tarreau26f95952017-07-27 17:18:30 +02002058 case H2_FT_WINDOW_UPDATE:
2059 if (h2c->st0 == H2_CS_FRAME_P)
2060 ret = h2c_handle_window_update(h2c, h2s);
2061 break;
2062
Willy Tarreau61290ec2017-10-17 08:19:21 +02002063 case H2_FT_CONTINUATION:
2064 /* we currently don't support CONTINUATION frames since
2065 * we have nowhere to store the partial HEADERS frame.
2066 * Let's abort the stream on an INTERNAL_ERROR here.
2067 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002068 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02002069 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002070 h2c->st0 = H2_CS_FRAME_E;
2071 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02002072 break;
2073
Willy Tarreau13278b42017-10-13 19:23:14 +02002074 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002075 if (h2c->st0 == H2_CS_FRAME_P) {
2076 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
2077 if (tmp_h2s) {
2078 h2s = tmp_h2s;
2079 ret = 1;
2080 }
2081 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002082 break;
2083
Willy Tarreau454f9052017-10-26 19:40:35 +02002084 case H2_FT_DATA:
2085 if (h2c->st0 == H2_CS_FRAME_P)
2086 ret = h2c_frt_handle_data(h2c, h2s);
2087
2088 if (h2c->st0 == H2_CS_FRAME_A)
2089 ret = h2c_send_strm_wu(h2c);
2090 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002091
Willy Tarreau92153fc2017-12-03 19:46:19 +01002092 case H2_FT_PRIORITY:
2093 if (h2c->st0 == H2_CS_FRAME_P)
2094 ret = h2c_handle_priority(h2c);
2095 break;
2096
Willy Tarreaucd234e92017-08-18 10:59:39 +02002097 case H2_FT_RST_STREAM:
2098 if (h2c->st0 == H2_CS_FRAME_P)
2099 ret = h2c_handle_rst_stream(h2c, h2s);
2100 break;
2101
Willy Tarreaue96b0922017-10-30 00:28:29 +01002102 case H2_FT_GOAWAY:
2103 if (h2c->st0 == H2_CS_FRAME_P)
2104 ret = h2c_handle_goaway(h2c);
2105 break;
2106
Willy Tarreau1c661982017-10-30 13:52:01 +01002107 case H2_FT_PUSH_PROMISE:
2108 /* not permitted here, RFC7540#5.1 */
2109 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002110 if (!h2c->nb_streams) {
2111 /* only log if no other stream can report the error */
2112 sess_log(h2c->conn->owner);
2113 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002114 break;
2115
2116 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002117 default:
2118 /* drop frames that we ignore. They may be larger than
2119 * the buffer so we drain all of their contents until
2120 * we reach the end.
2121 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002122 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2123 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002124 h2c->dfl -= ret;
2125 ret = h2c->dfl == 0;
2126 }
2127
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002128 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002129 /* We may have to send an RST if not done yet */
2130 if (h2s->st == H2_SS_ERROR)
2131 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002132
Willy Tarreaua20a5192017-12-27 11:02:06 +01002133 if (h2c->st0 == H2_CS_FRAME_E)
2134 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002135
Willy Tarreau7e98c052017-10-10 15:56:59 +02002136 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002137 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002138 break;
2139
2140 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002141 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002142 h2c->st0 = H2_CS_FRAME_H;
2143 }
2144 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002145
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002146 if (h2c->rcvd_c > 0 &&
2147 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2148 h2c_send_conn_wu(h2c);
2149
Willy Tarreau52eed752017-09-22 15:05:09 +02002150 fail:
2151 /* we can go here on missing data, blocked response or error */
Olivier Houchard638b7992018-08-16 15:41:52 +02002152 if (h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002153 /* we may have to signal the upper layers */
2154 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002155 if (h2s->recv_wait) {
2156 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
2157 tasklet_wakeup(h2s->recv_wait->task);
2158 h2s->recv_wait = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002159 }
2160 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002161
2162 if (h2_recv_allowed(h2c))
2163 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreaubc933932017-10-09 16:21:43 +02002164}
2165
2166/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2167 * the end.
2168 */
2169static int h2_process_mux(struct h2c *h2c)
2170{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002171 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002172
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002173 /* start by sending possibly pending window updates */
2174 if (h2c->rcvd_c > 0 &&
2175 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2176 h2c_send_conn_wu(h2c) < 0)
2177 goto fail;
2178
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002179 /* First we always process the flow control list because the streams
2180 * waiting there were already elected for immediate emission but were
2181 * blocked just on this.
2182 */
2183
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002184 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002185 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2186 h2c->st0 >= H2_CS_ERROR)
2187 break;
2188
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002189 h2s->flags &= ~H2_SF_BLK_ANY;
2190 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002191 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002192 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002193 LIST_DEL(&h2s->list);
2194 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002195 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002196 }
2197
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002198 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002199 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2200 break;
2201
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002202 h2s->flags &= ~H2_SF_BLK_ANY;
2203 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002204 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002205 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002206 LIST_DEL(&h2s->list);
2207 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002208 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002209 }
2210
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002211 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002212 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002213 if (h2c->st0 == H2_CS_ERROR) {
2214 if (h2c->max_id >= 0) {
2215 h2c_send_goaway_error(h2c, NULL);
2216 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2217 return 0;
2218 }
2219
2220 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2221 }
2222 return 1;
2223 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002224 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002225}
2226
Willy Tarreau62f52692017-10-08 23:01:42 +02002227
Willy Tarreau479998a2018-11-18 06:30:59 +01002228/* Attempt to read data, and subscribe if none available.
2229 * The function returns 1 if data has been received, otherwise zero.
2230 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002231static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002232{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002233 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002234 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002235 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002236 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002237
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002238 if (h2c->wait_event.wait_reason & SUB_CAN_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002239 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002240
Willy Tarreau315d8072017-12-10 22:17:57 +01002241 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002242 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002243
Willy Tarreau44e973f2018-03-01 17:49:30 +01002244 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002245 if (!buf) {
2246 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002247 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002248 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002249
Olivier Houchard7505f942018-08-21 18:10:44 +02002250 do {
2251 max = buf->size - b_data(buf);
2252 if (max)
2253 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2254 else
2255 ret = 0;
2256 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002257
Olivier Houchard53216e72018-10-10 15:46:36 +02002258 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002259 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002260
Olivier Houcharda1411e62018-08-17 18:42:48 +02002261 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002262 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002263 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002264 }
2265
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002266 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002267 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002268 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002269}
2270
Willy Tarreau479998a2018-11-18 06:30:59 +01002271/* Try to send data if possible.
2272 * The function returns 1 if data have been sent, otherwise zero.
2273 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002274static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002275{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002276 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002277 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002278 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002279
2280 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002281 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002282
Olivier Houchard7505f942018-08-21 18:10:44 +02002283
Willy Tarreaua2af5122017-10-09 11:56:46 +02002284 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2285 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002286 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002287 }
2288
Willy Tarreaubc933932017-10-09 16:21:43 +02002289 /* This loop is quite simple : it tries to fill as much as it can from
2290 * pending streams into the existing buffer until it's reportedly full
2291 * or the end of send requests is reached. Then it tries to send this
2292 * buffer's contents out, marks it not full if at least one byte could
2293 * be sent, and tries again.
2294 *
2295 * The snd_buf() function normally takes a "flags" argument which may
2296 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2297 * data immediately comes and CO_SFL_STREAMER to indicate that the
2298 * connection is streaming lots of data (used to increase TLS record
2299 * size at the expense of latency). The former can be sent any time
2300 * there's a buffer full flag, as it indicates at least one stream
2301 * attempted to send and failed so there are pending data. An
2302 * alternative would be to set it as long as there's an active stream
2303 * but that would be problematic for ACKs until we have an absolute
2304 * guarantee that all waiters have at least one byte to send. The
2305 * latter should possibly not be set for now.
2306 */
2307
2308 done = 0;
2309 while (!done) {
2310 unsigned int flags = 0;
2311
2312 /* fill as much as we can into the current buffer */
2313 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2314 done = h2_process_mux(h2c);
2315
2316 if (conn->flags & CO_FL_ERROR)
2317 break;
2318
2319 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2320 flags |= CO_SFL_MSG_MORE;
2321
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002322 if (b_data(&h2c->mbuf)) {
2323 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002324 if (!ret)
2325 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002326 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002327 b_del(&h2c->mbuf, ret);
2328 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002329 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002330
2331 /* wrote at least one byte, the buffer is not full anymore */
2332 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2333 }
2334
Willy Tarreaua2af5122017-10-09 11:56:46 +02002335 if (conn->flags & CO_FL_SOCK_WR_SH) {
2336 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002337 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002338 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002339 /* We're not full anymore, so we can wake any task that are waiting
2340 * for us.
2341 */
2342 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002343 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002344 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2345 struct h2s *, list);
2346 LIST_DEL(&h2s->list);
2347 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002348 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002349 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002350 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002351 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002352 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002353 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002354 /* We're done, no more to send */
2355 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002356 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002357schedule:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002358 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
2359 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002360 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002361}
2362
2363static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2364{
2365 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002366 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002367
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002368 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002369 ret = h2_send(h2c);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002370 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002371 ret |= h2_recv(h2c);
2372 if (ret)
2373 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002374 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002375}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002376
Willy Tarreau62f52692017-10-08 23:01:42 +02002377/* callback called on any event by the connection handler.
2378 * It applies changes and returns zero, or < 0 if it wants immediate
2379 * destruction of the connection (which normally doesn not happen in h2).
2380 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002381static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002382{
Olivier Houchard7505f942018-08-21 18:10:44 +02002383 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002384
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002385 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002386 h2_process_demux(h2c);
2387
2388 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002389 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002390
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002391 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002392 h2c->flags &= ~H2_CF_DEM_DFULL;
2393 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002394 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002395
Willy Tarreau0b37d652018-10-03 10:33:02 +02002396 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002397 /* frontend is stopping, reload likely in progress, let's try
2398 * to announce a graceful shutdown if not yet done. We don't
2399 * care if it fails, it will be tried again later.
2400 */
2401 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2402 if (h2c->last_sid < 0)
2403 h2c->last_sid = (1U << 31) - 1;
2404 h2c_send_goaway_error(h2c, NULL);
2405 }
2406 }
2407
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002408 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002409 * If we received early data, and the handshake is done, wake
2410 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002411 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002412 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2413 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2414 struct eb32_node *node;
2415 struct h2s *h2s;
2416
2417 h2c->flags |= H2_CF_WAIT_FOR_HS;
2418 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2419
2420 while (node) {
2421 h2s = container_of(node, struct h2s, by_id);
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002422 if ((h2s->cs->flags & CS_FL_WAIT_FOR_HS) &&
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002423 h2s->recv_wait) {
2424 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002425 sw->wait_reason &= ~SUB_CAN_RECV;
2426 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002427 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002428 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002429 node = eb32_next(node);
2430 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002431 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002432
Willy Tarreau26bd7612017-10-09 16:47:04 +02002433 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002434 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2435 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2436 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002437 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002438
2439 if (eb_is_empty(&h2c->streams_by_id)) {
2440 /* no more stream, kill the connection now */
2441 h2_release(conn);
2442 return -1;
2443 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002444 }
2445
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002446 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002447 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002448
Olivier Houchard53216e72018-10-10 15:46:36 +02002449 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2450 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2451 (h2c->st0 != H2_CS_ERROR &&
2452 !b_data(&h2c->mbuf) &&
2453 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2454 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002455 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002456
Willy Tarreau3f133572017-10-31 19:21:06 +01002457 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002458 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002459 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002460 task_queue(h2c->task);
2461 }
2462 else
2463 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002464 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002465
Olivier Houchard7505f942018-08-21 18:10:44 +02002466 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002467 return 0;
2468}
2469
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002470static int h2_wake(struct connection *conn)
2471{
2472 struct h2c *h2c = conn->mux_ctx;
2473
2474 return (h2_process(h2c));
2475}
2476
Willy Tarreauea392822017-10-31 10:02:25 +01002477/* Connection timeout management. The principle is that if there's no receipt
2478 * nor sending for a certain amount of time, the connection is closed. If the
2479 * MUX buffer still has lying data or is not allocatable, the connection is
2480 * immediately killed. If it's allocatable and empty, we attempt to send a
2481 * GOAWAY frame.
2482 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002483static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002484{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002485 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002486 int expired = tick_is_expired(t->expire, now_ms);
2487
Willy Tarreau0975f112018-03-29 15:22:59 +02002488 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002489 return t;
2490
Willy Tarreau0975f112018-03-29 15:22:59 +02002491 task_delete(t);
2492 task_free(t);
2493
2494 if (!h2c) {
2495 /* resources were already deleted */
2496 return NULL;
2497 }
2498
2499 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002500 h2c_error(h2c, H2_ERR_NO_ERROR);
2501 h2_wake_some_streams(h2c, 0, 0);
2502
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002503 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002504 /* don't even try to send a GOAWAY, the buffer is stuck */
2505 h2c->flags |= H2_CF_GOAWAY_FAILED;
2506 }
2507
2508 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002509 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002510 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2511 h2c->flags |= H2_CF_GOAWAY_FAILED;
2512
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002513 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2514 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002515 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002516 b_del(&h2c->mbuf, ret);
2517 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002518 }
2519 }
Willy Tarreauea392822017-10-31 10:02:25 +01002520
Willy Tarreau0975f112018-03-29 15:22:59 +02002521 /* either we can release everything now or it will be done later once
2522 * the last stream closes.
2523 */
2524 if (eb_is_empty(&h2c->streams_by_id))
2525 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002526
Willy Tarreauea392822017-10-31 10:02:25 +01002527 return NULL;
2528}
2529
2530
Willy Tarreau62f52692017-10-08 23:01:42 +02002531/*******************************************/
2532/* functions below are used by the streams */
2533/*******************************************/
2534
2535/*
2536 * Attach a new stream to a connection
2537 * (Used for outgoing connections)
2538 */
2539static struct conn_stream *h2_attach(struct connection *conn)
2540{
2541 return NULL;
2542}
2543
Willy Tarreaufafd3982018-11-18 21:29:20 +01002544/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2545 * We have to scan because we may have some orphan streams. It might be
2546 * beneficial to scan backwards from the end to reduce the likeliness to find
2547 * orphans.
2548 */
2549static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2550{
2551 struct h2c *h2c = conn->mux_ctx;
2552 struct h2s *h2s;
2553 struct eb32_node *node;
2554
2555 node = eb32_first(&h2c->streams_by_id);
2556 while (node) {
2557 h2s = container_of(node, struct h2s, by_id);
2558 if (h2s->cs)
2559 return h2s->cs;
2560 node = eb32_next(node);
2561 }
2562 return NULL;
2563}
2564
Willy Tarreau62f52692017-10-08 23:01:42 +02002565/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002566 * Destroy the mux and the associated connection, if it is no longer used
2567 */
2568static void h2_destroy(struct connection *conn)
2569{
2570 struct h2c *h2c = conn->mux_ctx;
2571
2572 if (eb_is_empty(&h2c->streams_by_id))
2573 h2_release(h2c->conn);
2574}
2575
2576/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002577 * Detach the stream from the connection and possibly release the connection.
2578 */
2579static void h2_detach(struct conn_stream *cs)
2580{
Willy Tarreau60935142017-10-16 18:11:19 +02002581 struct h2s *h2s = cs->ctx;
2582 struct h2c *h2c;
2583
2584 cs->ctx = NULL;
2585 if (!h2s)
2586 return;
2587
2588 h2c = h2s->h2c;
2589 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002590 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002591 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2592 !h2_has_too_many_cs(h2c)) {
2593 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard53216e72018-10-10 15:46:36 +02002594 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002595 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreauf2101912018-07-19 10:11:38 +02002596 }
Willy Tarreau60935142017-10-16 18:11:19 +02002597
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002598 /* this stream may be blocked waiting for some data to leave (possibly
2599 * an ES or RST frame), so orphan it in this case.
2600 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002601 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002602 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002603 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002604 return;
2605
Willy Tarreau45f752e2017-10-30 15:44:59 +01002606 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2607 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2608 /* unblock the connection if it was blocked on this
2609 * stream.
2610 */
2611 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2612 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002613 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002614 }
2615
Willy Tarreau71049cc2018-03-28 13:56:39 +02002616 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002617
Willy Tarreaue323f342018-03-28 13:51:45 +02002618 /* We don't want to close right now unless we're removing the
2619 * last stream, and either the connection is in error, or it
2620 * reached the ID already specified in a GOAWAY frame received
2621 * or sent (as seen by last_sid >= 0).
2622 */
2623 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2624 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002625 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard52b94662018-10-21 03:01:20 +02002626 (h2c->flags & (H2_CF_GOAWAY_FAILED | H2_CF_GOAWAY_SENT)) ||
Olivier Houchard93c88522018-11-30 15:39:16 +01002627 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002628 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002629 (conn_xprt_read0_pending(h2c->conn) ||
2630 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2631 /* no more stream will come, kill it now */
2632 h2_release(h2c->conn);
2633 }
2634 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002635 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002636 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2637 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002638 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002639 else
2640 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002641 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002642}
2643
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002644static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002645{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002646 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002647 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002648
Willy Tarreau721c9742017-11-07 11:05:42 +01002649 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002650 return;
2651
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002652 /* if no outgoing data was seen on this stream, it means it was
2653 * closed with a "tcp-request content" rule that is normally
2654 * used to kill the connection ASAP (eg: limit abuse). In this
2655 * case we send a goaway to close the connection.
2656 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002657 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002658 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002659 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002660
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002661 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2662 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002663 h2c_send_goaway_error(h2c, h2s) <= 0)
2664 return;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002665
Willy Tarreau00dd0782018-03-01 16:31:34 +01002666 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002667
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002668 return;
2669add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002670 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002671 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002672 if (h2s->flags & H2_SF_BLK_MFCTL) {
2673 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2674 h2s->send_wait = sw;
2675 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2676 h2s->send_wait = sw;
2677 LIST_ADDQ(&h2c->send_list, &h2s->list);
2678 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002679 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002680 /* Let the handler know we want shutr */
2681 sw->handle = (void *)((long)sw->handle | 1);
2682
Willy Tarreau62f52692017-10-08 23:01:42 +02002683}
2684
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002685static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002686{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002687 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002688 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002689
Willy Tarreau721c9742017-11-07 11:05:42 +01002690 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002691 return;
2692
Willy Tarreau67434202017-11-06 20:20:51 +01002693 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002694 /* we can cleanly close using an empty data frame only after headers */
2695
2696 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2697 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002698 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002699
2700 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002701 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002702 else
2703 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002704 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002705 /* if no outgoing data was seen on this stream, it means it was
2706 * closed with a "tcp-request content" rule that is normally
2707 * used to kill the connection ASAP (eg: limit abuse). In this
2708 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002709 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002710 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002711 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002712 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002713
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002714 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2715 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002716 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002717 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002718
Willy Tarreau00dd0782018-03-01 16:31:34 +01002719 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002720 }
2721
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002722
2723 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002724 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002725 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002726 if (h2s->flags & H2_SF_BLK_MFCTL) {
2727 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2728 h2s->send_wait = sw;
2729 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2730 h2s->send_wait = sw;
2731 LIST_ADDQ(&h2c->send_list, &h2s->list);
2732 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002733 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002734 /* let the handler know we want to shutw */
2735 sw->handle = (void *)((long)(sw->handle) | 2);
2736
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002737}
2738
2739static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
2740{
2741 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002742 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002743
2744 if (reason & 1)
2745 h2_do_shutr(h2s);
2746 if (reason & 2)
2747 h2_do_shutw(h2s);
2748
2749 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02002750}
2751
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002752static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2753{
2754 struct h2s *h2s = cs->ctx;
2755
2756 if (!mode)
2757 return;
2758
2759 h2_do_shutr(h2s);
2760}
2761
2762static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2763{
2764 struct h2s *h2s = cs->ctx;
2765
2766 h2_do_shutw(h2s);
2767}
2768
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01002769/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
2770 * HTX request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2771 * proceed. Stream errors are reported in h2s->errcode and connection errors in
2772 * h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002773 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002774static int h2_frt_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002775{
2776 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002777 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02002778 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002779 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02002780 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002781 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01002782 struct buffer *csbuf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01002783 struct htx *htx = NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02002784 int flen = h2c->dfl;
2785 int outlen = 0;
2786 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01002787 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02002788
2789 if (!h2c->dfl) {
2790 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002791 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002792 return 0;
2793 }
2794
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002795 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01002796 return 0; // incomplete input frame
2797
Willy Tarreau13278b42017-10-13 19:23:14 +02002798 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002799 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02002800 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002801 copy = alloc_trash_chunk();
2802 if (!copy) {
2803 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2804 goto fail;
2805 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002806 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
2807 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
2808 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02002809 }
2810
2811 /* The padlen is the first byte before data, and the padding appears
2812 * after data. padlen+data+padding are included in flen.
2813 */
2814 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002815 h2c->dpl = *hdrs;
2816 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002817 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2818 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002819 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002820 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002821 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002822 hdrs += 1; // skip Pad Length
2823 }
2824
2825 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2826 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002827 if (read_n32(hdrs) == h2s->id) {
2828 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2829 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002830 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002831 }
2832
Willy Tarreau13278b42017-10-13 19:23:14 +02002833 hdrs += 5; // stream dep = 4, weight = 1
2834 flen -= 5;
2835 }
2836
2837 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2838 * don't support this for now and can't even decompress so we have to
2839 * break the connection.
2840 */
2841 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2842 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002843 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002844 }
2845
Olivier Houchard638b7992018-08-16 15:41:52 +02002846 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01002847 if (!csbuf) {
2848 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002849 goto fail;
2850 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002851
Willy Tarreau937f7602018-02-26 15:22:17 +01002852 /* we can't retry a failed decompression operation so we must be very
2853 * careful not to take any risks. In practice the output buffer is
2854 * always empty except maybe for trailers, in which case we simply have
2855 * to wait for the upper layer to finish consuming what is available.
2856 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01002857
2858 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
2859 htx = htx_from_buf(&h2s->rxbuf);
2860 if (!htx_is_empty(htx))
2861 goto fail;
2862 } else {
2863 if (b_data(csbuf))
2864 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002865
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01002866 csbuf->head = 0;
2867 try = b_size(csbuf);
2868 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002869
2870 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2871 sizeof(list)/sizeof(list[0]), tmp);
2872 if (outlen < 0) {
2873 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2874 goto fail;
2875 }
2876
2877 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002878 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01002879
2880 if (htx)
2881 outlen = h2_make_htx_request(list, htx, &msgf);
2882 else
2883 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002884
2885 if (outlen < 0) {
2886 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2887 goto fail;
2888 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002889
Willy Tarreau174b06a2018-04-25 18:13:58 +02002890 if (msgf & H2_MSGF_BODY) {
2891 /* a payload is present */
2892 if (msgf & H2_MSGF_BODY_CL)
2893 h2s->flags |= H2_SF_DATA_CLEN;
2894 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
2895 h2s->flags |= H2_SF_DATA_CHNK;
2896 }
2897
Willy Tarreau13278b42017-10-13 19:23:14 +02002898 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002899 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02002900 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01002901 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02002902
Willy Tarreau39d68502018-03-02 12:26:37 +01002903 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002904 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002905 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01002906 if (htx)
2907 htx_add_endof(htx, HTX_BLK_EOM);
Willy Tarreau39d68502018-03-02 12:26:37 +01002908 }
Willy Tarreau937f7602018-02-26 15:22:17 +01002909
Willy Tarreau68dd9852017-07-03 14:44:26 +02002910 leave:
2911 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002912 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002913 fail:
2914 outlen = 0;
2915 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002916}
2917
Willy Tarreau454f9052017-10-26 19:40:35 +02002918/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2919 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2920 * in use, a new chunk is emitted for each frame. This is supposed to fit
2921 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2922 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2923 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2924 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002925 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2926 * checked to know if some data remain pending (an empty DATA frame can return
2927 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2928 * connection errors in h2c->errcode. The caller must already have checked the
2929 * frame header and ensured that the frame was complete or the buffer full. It
2930 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002931 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002932static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02002933{
2934 struct h2c *h2c = h2s->h2c;
2935 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002936 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002937 unsigned int chklen = 0;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002938 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02002939
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002940 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002941
2942 /* The padlen is the first byte before data, and the padding appears
2943 * after data. padlen+data+padding are included in flen.
2944 */
Willy Tarreau79127812017-12-03 21:06:59 +01002945 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002946 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002947 return 0;
2948
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002949 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002950 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002951 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2952 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002953 return 0;
2954 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002955
2956 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002957 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002958 h2c->dfl--;
2959 h2c->rcvd_c++; h2c->rcvd_s++;
2960 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002961 }
2962
Olivier Houchard638b7992018-08-16 15:41:52 +02002963 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01002964 if (!csbuf) {
2965 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002966 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002967 }
2968
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002969 flen = h2c->dfl - h2c->dpl;
2970 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002971 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002972
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002973 if (flen > b_data(&h2c->dbuf)) {
2974 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002975 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01002976 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002977 }
2978
2979 if (unlikely(b_space_wraps(csbuf))) {
2980 /* it doesn't fit and the buffer is fragmented,
2981 * so let's defragment it and try again.
2982 */
2983 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02002984 }
2985
Willy Tarreaueba10f22018-04-25 20:44:22 +02002986 /* chunked-encoding requires more room */
2987 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01002988 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02002989 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
2990 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2991 (chklen < 1048576) ? 4 : 8;
2992 chklen += 4; // CRLF, CRLF
2993 }
2994
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002995 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002996 if (flen + chklen > b_room(csbuf)) {
2997 if (chklen >= b_room(csbuf)) {
2998 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002999 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003000 }
3001 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003002 }
3003
3004 if (h2s->flags & H2_SF_DATA_CHNK) {
3005 /* emit the chunk size */
3006 unsigned int chksz = flen;
3007 char str[10];
3008 char *beg;
3009
3010 beg = str + sizeof(str);
3011 *--beg = '\n';
3012 *--beg = '\r';
3013 do {
3014 *--beg = hextab[chksz & 0xF];
3015 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003016 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003017 }
3018
Willy Tarreau454f9052017-10-26 19:40:35 +02003019 /* Block1 is the length of the first block before the buffer wraps,
3020 * block2 is the optional second block to reach the end of the frame.
3021 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003022 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003023 if (block1 > flen)
3024 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003025 block2 = flen - block1;
3026
3027 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003028 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003029
3030 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003031 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003032
Willy Tarreaueba10f22018-04-25 20:44:22 +02003033 if (h2s->flags & H2_SF_DATA_CHNK) {
3034 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003035 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003036 }
3037
Willy Tarreau454f9052017-10-26 19:40:35 +02003038 /* now mark the input data as consumed (will be deleted from the buffer
3039 * by the caller when seeing FRAME_A after sending the window update).
3040 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003041 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003042 h2c->dfl -= flen;
3043 h2c->rcvd_c += flen;
3044 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3045
3046 if (h2c->dfl > h2c->dpl) {
3047 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003048 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003049 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003050 }
3051
Willy Tarreau4a28da12018-01-04 14:41:00 +01003052 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003053 /* here we're done with the frame, all the payload (except padding) was
3054 * transferred.
3055 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003056
3057 if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) {
3058 /* emit the trailing 0 CRLF CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003059 if (b_room(csbuf) < 5) {
3060 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003061 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003062 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003063 chklen += 5;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003064 b_putblk(csbuf, "0\r\n\r\n", 5);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003065 }
3066
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003067 h2c->rcvd_c += h2c->dpl;
3068 h2c->rcvd_s += h2c->dpl;
3069 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003070 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3071
Willy Tarreau39d68502018-03-02 12:26:37 +01003072 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003073 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003074 h2s->cs->flags |= CS_FL_REOS;
3075 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003076
Willy Tarreau454b57b2018-02-26 15:50:05 +01003077 return flen + chklen;
3078 fail:
3079 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003080}
3081
Willy Tarreau5dd17352018-06-14 13:33:30 +02003082/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3083 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3084 * number of bytes sent. The caller must check the stream's status to detect
3085 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003086 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003087static 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 +02003088{
3089 struct http_hdr list[MAX_HTTP_HDR];
3090 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003091 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003092 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003093 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003094 int es_now = 0;
3095 int ret = 0;
3096 int hdr;
3097
3098 if (h2c_mux_busy(h2c, h2s)) {
3099 h2s->flags |= H2_SF_BLK_MBUSY;
3100 return 0;
3101 }
3102
Willy Tarreau44e973f2018-03-01 17:49:30 +01003103 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003104 h2c->flags |= H2_CF_MUX_MALLOC;
3105 h2s->flags |= H2_SF_BLK_MROOM;
3106 return 0;
3107 }
3108
3109 /* First, try to parse the H1 response and index it into <list>.
3110 * NOTE! Since it comes from haproxy, we *know* that a response header
3111 * block does not wrap and we can safely read it this way without
3112 * having to realign the buffer.
3113 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003114 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003115 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003116 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003117 /* incomplete or invalid response, this is abnormal coming from
3118 * haproxy and may only result in a bad errorfile or bad Lua code
3119 * so that won't be fixed, raise an error now.
3120 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003121 * FIXME: we should instead add the ability to only return a
3122 * 502 bad gateway. But in theory this is not supposed to
3123 * happen.
3124 */
3125 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3126 ret = 0;
3127 goto end;
3128 }
3129
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003130 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003131
3132 /* certain statuses have no body or an empty one, regardless of
3133 * what the headers say.
3134 */
3135 if (sl.st.status >= 100 && sl.st.status < 200) {
3136 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3137 h1m->curr_len = h1m->body_len = 0;
3138 }
3139 else if (sl.st.status == 204 || sl.st.status == 304) {
3140 /* no contents, claim c-len is present and set to zero */
3141 h1m->flags &= ~H1_MF_CHNK;
3142 h1m->flags |= H1_MF_CLEN;
3143 h1m->curr_len = h1m->body_len = 0;
3144 }
3145
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003146 chunk_reset(&outbuf);
3147
3148 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003149 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003150 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003151 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003152
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003153 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003154 break;
3155 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003156 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003157 }
3158
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003159 if (outbuf.size < 9)
3160 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003161
3162 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003163 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3164 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3165 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003166
3167 /* encode status, which necessarily is the first one */
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003168 if (outbuf.data < outbuf.size && h2s->status == 200)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003169 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003170 else if (outbuf.data < outbuf.size && h2s->status == 304)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003171 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003172 else if (unlikely(list[0].v.len != 3)) {
3173 /* this is an unparsable response */
3174 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3175 ret = 0;
3176 goto end;
3177 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003178 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003179 /* basic encoding of the status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003180 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3181 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3182 outbuf.area[outbuf.data++] = list[0].v.ptr[0];
3183 outbuf.area[outbuf.data++] = list[0].v.ptr[1];
3184 outbuf.area[outbuf.data++] = list[0].v.ptr[2];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003185 }
3186 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003187 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003188 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003189 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003190 }
3191
3192 /* encode all headers, stop at empty name */
3193 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003194 /* these ones do not exist in H2 and must be dropped. */
3195 if (isteq(list[hdr].n, ist("connection")) ||
3196 isteq(list[hdr].n, ist("proxy-connection")) ||
3197 isteq(list[hdr].n, ist("keep-alive")) ||
3198 isteq(list[hdr].n, ist("upgrade")) ||
3199 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003200 continue;
3201
3202 if (isteq(list[hdr].n, ist("")))
3203 break; // end
3204
3205 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3206 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003207 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003208 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003209 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003210 }
3211 }
3212
3213 /* we may need to add END_STREAM */
3214 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3215 es_now = 1;
3216
3217 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003218 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003219
3220 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003221 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003222
3223 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003224 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003225
3226 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003227 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003228 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003229
3230 /* for now we don't implemented CONTINUATION, so we wait for a
3231 * body or directly end in TRL2.
3232 */
3233 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003234 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003235 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003236
Willy Tarreau801250e2018-09-11 11:45:04 +02003237 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003238 h2s->flags |= H2_SF_ES_SENT;
3239 if (h2s->st == H2_SS_OPEN)
3240 h2s->st = H2_SS_HLOC;
3241 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003242 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003243 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003244 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003245 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003246 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003247 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003248 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003249 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003250 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003251
3252 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003253
3254 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003255 //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 +02003256 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003257 full:
3258 h1m_init_res(h1m);
3259 h1m->err_pos = -1; // don't care about errors on the response path
3260 h2c->flags |= H2_CF_MUX_MFULL;
3261 h2s->flags |= H2_SF_BLK_MROOM;
3262 ret = 0;
3263 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003264}
3265
Willy Tarreau5dd17352018-06-14 13:33:30 +02003266/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3267 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3268 * the number of bytes sent. The caller must check the stream's status to
3269 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003270 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003271static 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 +02003272{
3273 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003274 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003275 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003276 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003277 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003278 int es_now = 0;
3279 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003280 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003281 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003282
3283 if (h2c_mux_busy(h2c, h2s)) {
3284 h2s->flags |= H2_SF_BLK_MBUSY;
3285 goto end;
3286 }
3287
Willy Tarreau44e973f2018-03-01 17:49:30 +01003288 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003289 h2c->flags |= H2_CF_MUX_MALLOC;
3290 h2s->flags |= H2_SF_BLK_MROOM;
3291 goto end;
3292 }
3293
3294 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003295 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003296 goto end;
3297
3298 chunk_reset(&outbuf);
3299
3300 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003301 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003302 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003303 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003304
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003305 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003306 break;
3307 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003308 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003309 }
3310
3311 if (outbuf.size < 9) {
3312 h2c->flags |= H2_CF_MUX_MFULL;
3313 h2s->flags |= H2_SF_BLK_MROOM;
3314 goto end;
3315 }
3316
3317 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003318 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3319 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3320 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003321
3322 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3323 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003324 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003325 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003326 break;
3327 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003328 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003329 if ((long long)size > h1m->curr_len)
3330 size = h1m->curr_len;
3331 break;
3332 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003333 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003334 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003335 if (!ret)
3336 goto end;
3337
3338 if (ret < 0) {
3339 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003340 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003341 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3342 goto end;
3343 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003344 max -= ret;
3345 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003346 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003347 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003348 }
3349
Willy Tarreau801250e2018-09-11 11:45:04 +02003350 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003351 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003352 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003353 if (!ret)
3354 goto end;
3355
3356 if (ret < 0) {
3357 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003358 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003359 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3360 goto end;
3361 }
3362
3363 size = chunk;
3364 h1m->curr_len = chunk;
3365 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003366 max -= ret;
3367 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003368 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003369 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003370 if (!size)
3371 goto send_empty;
3372 }
3373
3374 /* in MSG_DATA state, continue below */
3375 size = h1m->curr_len;
3376 break;
3377 }
3378
3379 /* we have in <size> the exact number of bytes we need to copy from
3380 * the H1 buffer. We need to check this against the connection's and
3381 * the stream's send windows, and to ensure that this fits in the max
3382 * frame size and in the buffer's available space minus 9 bytes (for
3383 * the frame header). The connection's flow control is applied last so
3384 * that we can use a separate list of streams which are immediately
3385 * unblocked on window opening. Note: we don't implement padding.
3386 */
3387
Willy Tarreau5dd17352018-06-14 13:33:30 +02003388 if (size > max)
3389 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003390
3391 if (size > h2s->mws)
3392 size = h2s->mws;
3393
3394 if (size <= 0) {
3395 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02003396 if (h2s->send_wait) {
3397 LIST_DEL(&h2s->list);
3398 LIST_INIT(&h2s->list);
3399 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003400 goto end;
3401 }
3402
3403 if (h2c->mfs && size > h2c->mfs)
3404 size = h2c->mfs;
3405
3406 if (size + 9 > outbuf.size) {
3407 /* we have an opportunity for enlarging the too small
3408 * available space, let's try.
3409 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003410 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003411 goto realign_again;
3412 size = outbuf.size - 9;
3413 }
3414
3415 if (size <= 0) {
3416 h2c->flags |= H2_CF_MUX_MFULL;
3417 h2s->flags |= H2_SF_BLK_MROOM;
3418 goto end;
3419 }
3420
3421 if (size > h2c->mws)
3422 size = h2c->mws;
3423
3424 if (size <= 0) {
3425 h2s->flags |= H2_SF_BLK_MFCTL;
3426 goto end;
3427 }
3428
3429 /* copy whatever we can */
3430 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003431 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003432 if (ret == 1)
3433 len2 = 0;
3434
3435 if (!ret || len1 + len2 < size) {
3436 /* FIXME: must normally never happen */
3437 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3438 goto end;
3439 }
3440
3441 /* limit len1/len2 to size */
3442 if (len1 + len2 > size) {
3443 int sub = len1 + len2 - size;
3444
3445 if (len2 > sub)
3446 len2 -= sub;
3447 else {
3448 sub -= len2;
3449 len2 = 0;
3450 len1 -= sub;
3451 }
3452 }
3453
3454 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003455 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003456 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003457 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003458
3459 send_empty:
3460 /* we may need to add END_STREAM */
3461 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3462 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003463 *
3464 * FIXME: what we do here is not correct because we send end_stream
3465 * before knowing if we'll have to send a HEADERS frame for the
3466 * trailers. More importantly we're not consuming the trailing CRLF
3467 * after the end of trailers, so it will be left to the caller to
3468 * eat it. The right way to do it would be to measure trailers here
3469 * and to send ES only if there are no trailers.
3470 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003471 */
3472 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02003473 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003474 es_now = 1;
3475
3476 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003477 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003478
3479 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003480 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003481
3482 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003483 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003484
3485 /* consume incoming H1 response */
3486 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003487 max -= size;
3488 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003489 total += size;
3490 h1m->curr_len -= size;
3491 h2s->mws -= size;
3492 h2c->mws -= size;
3493
3494 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02003495 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003496 goto new_frame;
3497 }
3498 }
3499
3500 if (es_now) {
3501 if (h2s->st == H2_SS_OPEN)
3502 h2s->st = H2_SS_HLOC;
3503 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003504 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003505
Willy Tarreau35a62702018-02-27 15:37:25 +01003506 if (!(h1m->flags & H1_MF_CHNK)) {
3507 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003508 total += max;
3509 ofs += max;
3510 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003511
Willy Tarreau801250e2018-09-11 11:45:04 +02003512 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003513 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003514
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003515 h2s->flags |= H2_SF_ES_SENT;
3516 }
3517
3518 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003519 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 +02003520 return total;
3521}
3522
Olivier Houchard6ff20392018-07-17 18:46:31 +02003523/* Called from the upper layer, to subscribe to events, such as being able to send */
3524static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
3525{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003526 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003527 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003528 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003529
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003530 if (event_type & SUB_CAN_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003531 sw = param;
3532 if (!(sw->wait_reason & SUB_CAN_RECV)) {
3533 sw->wait_reason |= SUB_CAN_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003534 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003535 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003536 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003537 event_type &= ~SUB_CAN_RECV;
3538 }
3539 if (event_type & SUB_CAN_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02003540 sw = param;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003541 if (!(sw->wait_reason & SUB_CAN_SEND)) {
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003542 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003543 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003544 h2s->send_wait = sw;
3545 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
3546 if (h2s->flags & H2_SF_BLK_MFCTL)
3547 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3548 else
3549 LIST_ADDQ(&h2c->send_list, &h2s->list);
3550 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003551 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003552 event_type &= ~SUB_CAN_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003553 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003554 if (event_type != 0)
3555 return -1;
3556 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003557
3558
3559}
3560
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003561static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
3562{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003563 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003564 struct h2s *h2s = cs->ctx;
3565
3566 if (event_type & SUB_CAN_RECV) {
3567 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003568 if (h2s->recv_wait == sw) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003569 sw->wait_reason &= ~SUB_CAN_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003570 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003571 }
3572 }
3573 if (event_type & SUB_CAN_SEND) {
3574 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003575 if (h2s->send_wait == sw) {
3576 LIST_DEL(&h2s->list);
3577 LIST_INIT(&h2s->list);
3578 sw->wait_reason &= ~SUB_CAN_SEND;
3579 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003580 }
3581 }
Olivier Houchardd846c262018-10-19 17:24:29 +02003582 if (event_type & SUB_CALL_UNSUBSCRIBE) {
3583 sw = param;
3584 if (h2s->send_wait == sw) {
3585 sw->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
3586 h2s->send_wait = NULL;
3587 }
3588 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003589 return 0;
3590}
3591
3592
Olivier Houchard511efea2018-08-16 15:30:32 +02003593/* Called from the upper layer, to receive data */
3594static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3595{
Olivier Houchard638b7992018-08-16 15:41:52 +02003596 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01003597 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01003598 struct htx *h2s_htx = NULL;
3599 struct htx *buf_htx = NULL;
3600 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02003601 size_t ret = 0;
3602
3603 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01003604 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
3605 /* in HTX mode we ignore the count argument */
3606 h2s_htx = htx_from_buf(&h2s->rxbuf);
3607 if (htx_is_empty(h2s_htx))
3608 goto end;
3609
3610 buf_htx = htx_from_buf(buf);
3611 count = htx_free_space(buf_htx);
3612
3613 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, (h2s_htx->sl_off != -1) ? HTX_BLK_EOH : HTX_BLK_EOM);
3614
3615 buf_htx->extra = h2s_htx->extra;
3616 if (htx_is_not_empty(buf_htx))
3617 b_set_data(buf, b_size(buf));
3618 ret = htx_ret.ret;
3619 }
3620 else {
3621 ret = b_xfer(buf, &h2s->rxbuf, count);
3622 }
Olivier Houchard511efea2018-08-16 15:30:32 +02003623
Olivier Houchard638b7992018-08-16 15:41:52 +02003624 if (b_data(&h2s->rxbuf))
Olivier Houchard511efea2018-08-16 15:30:32 +02003625 cs->flags |= CS_FL_RCV_MORE;
3626 else {
3627 cs->flags &= ~CS_FL_RCV_MORE;
3628 if (cs->flags & CS_FL_REOS)
3629 cs->flags |= CS_FL_EOS;
Olivier Houchard638b7992018-08-16 15:41:52 +02003630 if (b_size(&h2s->rxbuf)) {
3631 b_free(&h2s->rxbuf);
3632 offer_buffers(NULL, tasks_run_queue);
3633 }
Olivier Houchard511efea2018-08-16 15:30:32 +02003634 }
3635
Willy Tarreau082f5592018-11-25 08:03:32 +01003636 if (ret && h2c->dsi == h2s->id) {
3637 /* demux is blocking on this stream's buffer */
3638 h2c->flags &= ~H2_CF_DEM_SFULL;
3639 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV)) {
3640 if (h2_recv_allowed(h2c))
3641 tasklet_wakeup(h2c->wait_event.task);
3642 }
3643 }
Willy Tarreau86724e22018-12-01 23:19:43 +01003644end:
Olivier Houchard511efea2018-08-16 15:30:32 +02003645 return ret;
3646}
3647
Olivier Houchardd846c262018-10-19 17:24:29 +02003648static void h2_stop_senders(struct h2c *h2c)
3649{
3650 struct h2s *h2s, *h2s_back;
3651
3652 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
3653 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
3654 if (h2c->msi == h2s_id(h2s))
3655 continue;
3656 LIST_DEL(&h2s->list);
3657 LIST_INIT(&h2s->list);
3658 task_remove_from_task_list((struct task *)h2s->send_wait->task);
3659 h2s->send_wait->wait_reason |= SUB_CAN_SEND;
3660 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
3661 LIST_ADD(&h2c->send_list, &h2s->list);
3662 }
3663}
3664
Willy Tarreau62f52692017-10-08 23:01:42 +02003665/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003666static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02003667{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003668 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003669 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003670 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01003671 struct htx *htx;
3672 struct htx_blk *blk;
3673 enum htx_blk_type btype;
3674 uint32_t bsize;
3675 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003676
Olivier Houchardd846c262018-10-19 17:24:29 +02003677 if (h2s->send_wait) {
3678 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
3679 h2s->send_wait = NULL;
3680 LIST_DEL(&h2s->list);
3681 LIST_INIT(&h2s->list);
3682 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02003683 if (h2s->h2c->st0 < H2_CS_FRAME_H)
3684 return 0;
3685
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01003686 /* htx will be enough to decide if we're using HTX or legacy */
3687 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
3688
Willy Tarreau0bad0432018-06-14 16:54:01 +02003689 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01003690 h2s->flags |= H2_SF_OUTGOING_DATA;
3691
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01003692 if (htx) {
3693 while (count && !htx_is_empty(htx)) {
3694 idx = htx_get_head(htx);
3695 blk = htx_get_blk(htx, idx);
3696 btype = htx_get_blk_type(blk);
3697 bsize = htx_get_blksz(blk);
3698
3699 switch (btype) {
3700 default:
3701 htx_remove_blk(htx, blk);
3702 total += bsize;
3703 count -= bsize;
3704 break;
3705 }
3706 }
3707 goto done;
3708 }
3709
3710 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02003711 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02003712 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003713 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003714 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02003715 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003716 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003717 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02003718 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003719 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003720 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003721
Willy Tarreau5dd17352018-06-14 13:33:30 +02003722 if (unlikely((int)ret <= 0)) {
3723 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003724 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3725 break;
3726 }
Willy Tarreau35a62702018-02-27 15:37:25 +01003727 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02003728 total += count;
3729 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003730 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003731 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003732 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003733 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003734 cs->flags |= CS_FL_ERROR;
3735 break;
3736 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02003737
3738 total += ret;
3739 count -= ret;
3740
3741 if (h2s->st >= H2_SS_ERROR)
3742 break;
3743
3744 if (h2s->flags & H2_SF_BLK_ANY)
3745 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003746 }
3747
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01003748 done:
Willy Tarreau00610962018-07-19 10:58:28 +02003749 if (h2s->st >= H2_SS_ERROR) {
3750 /* trim any possibly pending data after we close (extra CR-LF,
3751 * unprocessed trailers, abnormal extra data, ...)
3752 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003753 total += count;
3754 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02003755 }
3756
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003757 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003758 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003759 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003760 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003761 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003762 }
3763
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01003764 if (htx) {
3765 if (htx_is_empty(htx)) {
3766 htx_reset(htx);
3767 b_set_data(buf, 0);
3768 }
3769 } else {
3770 b_del(buf, total);
3771 }
Olivier Houchardd846c262018-10-19 17:24:29 +02003772
3773 /* The mux is full, cancel the pending tasks */
3774 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
3775 (h2s->flags & H2_SF_BLK_MBUSY))
3776 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01003777
Olivier Houchard7505f942018-08-21 18:10:44 +02003778 if (total > 0) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003779 if (!(h2s->h2c->wait_event.wait_reason & SUB_CAN_SEND))
3780 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02003781
Olivier Houchard7505f942018-08-21 18:10:44 +02003782 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003783 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003784}
3785
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003786/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02003787static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003788{
3789 struct h2c *h2c = conn->mux_ctx;
3790 struct h2s *h2s;
3791 struct eb32_node *node;
3792 int fctl_cnt = 0;
3793 int send_cnt = 0;
3794 int tree_cnt = 0;
3795 int orph_cnt = 0;
3796
3797 if (!h2c)
3798 return;
3799
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003800 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003801 fctl_cnt++;
3802
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003803 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003804 send_cnt++;
3805
3806 node = eb32_first(&h2c->streams_by_id);
3807 while (node) {
3808 h2s = container_of(node, struct h2s, by_id);
3809 tree_cnt++;
3810 if (!h2s->cs)
3811 orph_cnt++;
3812 node = eb32_next(node);
3813 }
3814
Willy Tarreau616ac812018-07-24 14:12:42 +02003815 chunk_appendf(msg, " st0=%d err=%d maxid=%d lastid=%d flg=0x%08x nbst=%u nbcs=%u"
3816 " fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
3817 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
3818 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
3819 (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
3820 (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003821}
Willy Tarreau62f52692017-10-08 23:01:42 +02003822
3823/*******************************************************/
3824/* functions below are dedicated to the config parsers */
3825/*******************************************************/
3826
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003827/* config parser for global "tune.h2.header-table-size" */
3828static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3829 struct proxy *defpx, const char *file, int line,
3830 char **err)
3831{
3832 if (too_many_args(1, args, err, NULL))
3833 return -1;
3834
3835 h2_settings_header_table_size = atoi(args[1]);
3836 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3837 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3838 return -1;
3839 }
3840 return 0;
3841}
Willy Tarreau62f52692017-10-08 23:01:42 +02003842
Willy Tarreaue6baec02017-07-27 11:45:11 +02003843/* config parser for global "tune.h2.initial-window-size" */
3844static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3845 struct proxy *defpx, const char *file, int line,
3846 char **err)
3847{
3848 if (too_many_args(1, args, err, NULL))
3849 return -1;
3850
3851 h2_settings_initial_window_size = atoi(args[1]);
3852 if (h2_settings_initial_window_size < 0) {
3853 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3854 return -1;
3855 }
3856 return 0;
3857}
3858
Willy Tarreau5242ef82017-07-27 11:47:28 +02003859/* config parser for global "tune.h2.max-concurrent-streams" */
3860static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3861 struct proxy *defpx, const char *file, int line,
3862 char **err)
3863{
3864 if (too_many_args(1, args, err, NULL))
3865 return -1;
3866
3867 h2_settings_max_concurrent_streams = atoi(args[1]);
3868 if (h2_settings_max_concurrent_streams < 0) {
3869 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3870 return -1;
3871 }
3872 return 0;
3873}
3874
Willy Tarreau62f52692017-10-08 23:01:42 +02003875
3876/****************************************/
3877/* MUX initialization and instanciation */
3878/***************************************/
3879
3880/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01003881static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02003882 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003883 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02003884 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02003885 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02003886 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003887 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02003888 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01003889 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02003890 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01003891 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01003892 .avail_streams = h2_avail_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02003893 .shutr = h2_shutr,
3894 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003895 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003896 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003897 .name = "H2",
3898};
3899
Christopher Faulet32f61c02018-04-10 14:33:41 +02003900/* PROTO selection : this mux registers PROTO token "h2" */
3901static struct mux_proto_list mux_proto_h2 =
Willy Tarreau5ae96002018-11-30 16:49:47 +01003902 { .token = IST("h2"), .mode = PROTO_MODE_HTTP | PROTO_MODE_HTX, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02003903
Willy Tarreau0108d902018-11-25 19:14:37 +01003904INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
3905
Willy Tarreau62f52692017-10-08 23:01:42 +02003906/* config keyword parsers */
3907static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003908 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003909 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003910 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003911 { 0, NULL, NULL }
3912}};
3913
Willy Tarreau0108d902018-11-25 19:14:37 +01003914INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);