blob: bfcfc267f4ecef1fa3acfd3aab881c93fbe29821 [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 Tarreauc3e18f32018-10-08 14:51:56 +0200236static int h2s_decode_headers(struct h2s *h2s);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100237static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200238static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Willy Tarreau751f2d02018-10-05 09:35:00 +0200239static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200240
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200241/*****************************************************/
242/* functions below are for dynamic buffer management */
243/*****************************************************/
244
Willy Tarreau315d8072017-12-10 22:17:57 +0100245/* indicates whether or not the we may call the h2_recv() function to attempt
246 * to receive data into the buffer and/or demux pending data. The condition is
247 * a bit complex due to some API limits for now. The rules are the following :
248 * - if an error or a shutdown was detected on the connection and the buffer
249 * is empty, we must not attempt to receive
250 * - if the demux buf failed to be allocated, we must not try to receive and
251 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100252 * - if no flag indicates a blocking condition, we may attempt to receive,
253 * regardless of whether the demux buffer is full or not, so that only
254 * de demux part decides whether or not to block. This is needed because
255 * the connection API indeed prevents us from re-enabling receipt that is
256 * already enabled in a polled state, so we must always immediately stop
257 * as soon as the demux can't proceed so as never to hit an end of read
258 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100259 * - otherwise must may not attempt
260 */
261static inline int h2_recv_allowed(const struct h2c *h2c)
262{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200263 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100264 (h2c->st0 >= H2_CS_ERROR ||
265 h2c->conn->flags & CO_FL_ERROR ||
266 conn_xprt_read0_pending(h2c->conn)))
267 return 0;
268
269 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100270 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100271 return 1;
272
273 return 0;
274}
275
Willy Tarreauf2101912018-07-19 10:11:38 +0200276/* returns true if the connection has too many conn_streams attached */
277static inline int h2_has_too_many_cs(const struct h2c *h2c)
278{
279 return h2c->nb_cs >= h2_settings_max_concurrent_streams;
280}
281
Willy Tarreau44e973f2018-03-01 17:49:30 +0100282/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
283 * flags are used to figure what buffer was requested. It returns 1 if the
284 * allocation succeeds, in which case the connection is woken up, or 0 if it's
285 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200286 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100287static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200288{
289 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100290 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200291
Willy Tarreau44e973f2018-03-01 17:49:30 +0100292 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200293 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard53216e72018-10-10 15:46:36 +0200294 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200295 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200296 return 1;
297 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200298
Willy Tarreau44e973f2018-03-01 17:49:30 +0100299 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
300 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200301
302 if (h2c->flags & H2_CF_DEM_MROOM) {
303 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard53216e72018-10-10 15:46:36 +0200304 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200305 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200306 }
Willy Tarreau14398122017-09-22 14:26:04 +0200307 return 1;
308 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100309
310 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
311 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200312 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100313 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard53216e72018-10-10 15:46:36 +0200314 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200315 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau0b559072018-02-26 15:22:17 +0100316 return 1;
317 }
318
Willy Tarreau14398122017-09-22 14:26:04 +0200319 return 0;
320}
321
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200322static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200323{
324 struct buffer *buf = NULL;
325
Willy Tarreau44e973f2018-03-01 17:49:30 +0100326 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
327 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
328 h2c->buf_wait.target = h2c;
329 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100330 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100331 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100332 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200333 __conn_xprt_stop_recv(h2c->conn);
334 }
335 return buf;
336}
337
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200338static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200339{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200340 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100341 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200342 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200343 }
344}
345
Olivier Houchardd540b362018-11-05 18:37:53 +0100346static int h2_avail_streams(struct connection *conn)
347{
348 struct h2c *h2c = conn->mux_ctx;
349
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100350 /* XXX Should use the negociated max concurrent stream nb instead of the conf value */
Olivier Houchardd540b362018-11-05 18:37:53 +0100351 return (h2_settings_max_concurrent_streams - h2c->nb_streams);
352}
353
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100354static int h2_max_streams(struct connection *conn)
355{
356 /* XXX Should use the negociated max concurrent stream nb instead of the conf value */
357 return h2_settings_max_concurrent_streams;
358}
359
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200360
Willy Tarreau62f52692017-10-08 23:01:42 +0200361/*****************************************************************/
362/* functions below are dedicated to the mux setup and management */
363/*****************************************************************/
364
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200365/* Initialize the mux once it's attached. For outgoing connections, the context
366 * is already initialized before installing the mux, so we detect incoming
367 * connections from the fact that the context is still NULL. Returns < 0 on
368 * error.
369 */
370static int h2_init(struct connection *conn, struct proxy *prx)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200371{
372 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100373 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200374
Willy Tarreaubafbe012017-11-24 17:34:44 +0100375 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200376 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200377 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200378
Willy Tarreau01b44822018-10-03 14:26:37 +0200379 if (conn->mux_ctx) {
380 h2c->flags = H2_CF_IS_BACK;
381 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
382 if (tick_isset(prx->timeout.serverfin))
383 h2c->shut_timeout = prx->timeout.serverfin;
384 } else {
385 h2c->flags = H2_CF_NONE;
386 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
387 if (tick_isset(prx->timeout.clientfin))
388 h2c->shut_timeout = prx->timeout.clientfin;
389 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100390
Willy Tarreau0b37d652018-10-03 10:33:02 +0200391 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100392 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100393 if (tick_isset(h2c->timeout)) {
394 t = task_new(tid_bit);
395 if (!t)
396 goto fail;
397
398 h2c->task = t;
399 t->process = h2_timeout_task;
400 t->context = h2c;
401 t->expire = tick_add(now_ms, h2c->timeout);
402 }
Willy Tarreauea392822017-10-31 10:02:25 +0100403
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200404 h2c->wait_event.task = tasklet_new();
405 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200406 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200407 h2c->wait_event.task->process = h2_io_cb;
408 h2c->wait_event.task->context = h2c;
409 h2c->wait_event.wait_reason = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200410
Willy Tarreau32218eb2017-09-22 08:07:25 +0200411 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
412 if (!h2c->ddht)
413 goto fail;
414
415 /* Initialise the context. */
416 h2c->st0 = H2_CS_PREFACE;
417 h2c->conn = conn;
418 h2c->max_id = -1;
419 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200420 h2c->rcvd_c = 0;
421 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100422 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200423 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200424
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200425 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200426 h2c->dsi = -1;
427 h2c->msi = -1;
428 h2c->last_sid = -1;
429
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200430 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200431 h2c->miw = 65535; /* mux initial window size */
432 h2c->mws = 65535; /* mux window size */
433 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200434 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200435 LIST_INIT(&h2c->send_list);
436 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200437 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100438 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200439
Willy Tarreau3f133572017-10-31 19:21:06 +0100440 if (t)
441 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100442
Willy Tarreau01b44822018-10-03 14:26:37 +0200443 if (h2c->flags & H2_CF_IS_BACK) {
444 /* FIXME: this is temporary, for outgoing connections we need
445 * to immediately allocate a stream until the code is modified
446 * so that the caller calls ->attach(). For now the outgoing cs
447 * is stored as conn->mux_ctx by the caller.
448 */
449 struct h2s *h2s;
450
451 h2s = h2c_bck_stream_new(h2c, conn->mux_ctx);
452 if (!h2s)
453 goto fail_stream;
454 }
455
456 conn->mux_ctx = h2c;
457
Willy Tarreau0f383582018-10-03 14:22:21 +0200458 /* prepare to read something */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200459 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200460 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200461 fail_stream:
462 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200463 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100464 if (t)
465 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200466 if (h2c->wait_event.task)
467 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100468 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200469 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200470 return -1;
471}
472
Willy Tarreau751f2d02018-10-05 09:35:00 +0200473/* returns the next allocatable outgoing stream ID for the H2 connection, or
474 * -1 if no more is allocatable.
475 */
476static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
477{
478 int32_t id = (h2c->max_id + 1) | 1;
479 if (id & 0x80000000U)
480 id = -1;
481 return id;
482}
483
Willy Tarreau2373acc2017-10-12 17:35:14 +0200484/* returns the stream associated with id <id> or NULL if not found */
485static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
486{
487 struct eb32_node *node;
488
Willy Tarreau751f2d02018-10-05 09:35:00 +0200489 if (id == 0)
490 return (struct h2s *)h2_closed_stream;
491
Willy Tarreau2a856182017-05-16 15:20:39 +0200492 if (id > h2c->max_id)
493 return (struct h2s *)h2_idle_stream;
494
Willy Tarreau2373acc2017-10-12 17:35:14 +0200495 node = eb32_lookup(&h2c->streams_by_id, id);
496 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200497 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200498
499 return container_of(node, struct h2s, by_id);
500}
501
Willy Tarreau62f52692017-10-08 23:01:42 +0200502/* release function for a connection. This one should be called to free all
503 * resources allocated to the mux.
504 */
505static void h2_release(struct connection *conn)
506{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200507 struct h2c *h2c = conn->mux_ctx;
508
509 LIST_DEL(&conn->list);
510
511 if (h2c) {
512 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200513
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100514 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100515 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100516 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200517
Willy Tarreau44e973f2018-03-01 17:49:30 +0100518 h2_release_buf(h2c, &h2c->dbuf);
519 h2_release_buf(h2c, &h2c->mbuf);
520
Willy Tarreauea392822017-10-31 10:02:25 +0100521 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200522 h2c->task->context = NULL;
523 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100524 h2c->task = NULL;
525 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200526 if (h2c->wait_event.task)
527 tasklet_free(h2c->wait_event.task);
528 if (h2c->wait_event.wait_reason != 0)
529 conn->xprt->unsubscribe(conn, h2c->wait_event.wait_reason,
530 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100531
Willy Tarreaubafbe012017-11-24 17:34:44 +0100532 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200533 }
534
535 conn->mux = NULL;
536 conn->mux_ctx = NULL;
537
538 conn_stop_tracking(conn);
539 conn_full_close(conn);
540 if (conn->destroy_cb)
541 conn->destroy_cb(conn);
542 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200543}
544
545
Willy Tarreau71681172017-10-23 14:39:06 +0200546/******************************************************/
547/* functions below are for the H2 protocol processing */
548/******************************************************/
549
550/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100551static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200552{
553 return h2s ? h2s->id : 0;
554}
555
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200556/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100557static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200558{
559 if (h2c->msi < 0)
560 return 0;
561
562 if (h2c->msi == h2s_id(h2s))
563 return 0;
564
565 return 1;
566}
567
Willy Tarreau741d6df2017-10-17 08:00:59 +0200568/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100569static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200570{
571 h2c->errcode = err;
572 h2c->st0 = H2_CS_ERROR;
573}
574
Willy Tarreau2e43f082017-10-17 08:03:59 +0200575/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100576static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200577{
Willy Tarreauab0e1da2018-10-05 10:16:37 +0200578 if (h2s->id && h2s->st < H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200579 h2s->errcode = err;
580 h2s->st = H2_SS_ERROR;
581 if (h2s->cs)
582 h2s->cs->flags |= CS_FL_ERROR;
583 }
584}
585
Willy Tarreaue4820742017-07-27 13:37:23 +0200586/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100587static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200588{
589 uint8_t *out = frame;
590
591 *out = len >> 16;
592 write_n16(out + 1, len);
593}
594
Willy Tarreau54c15062017-10-10 17:10:03 +0200595/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
596 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
597 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200598 * available in the buffer's input prior to calling this function. The buffer
599 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200600 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100601static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200602 const struct buffer *b, int o)
603{
Willy Tarreau591d4452018-06-15 17:21:00 +0200604 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 +0200605}
606
Willy Tarreau1f094672017-11-20 21:27:45 +0100607static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200608{
Willy Tarreau591d4452018-06-15 17:21:00 +0200609 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200610}
611
Willy Tarreau1f094672017-11-20 21:27:45 +0100612static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200613{
Willy Tarreau591d4452018-06-15 17:21:00 +0200614 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200615}
616
Willy Tarreau1f094672017-11-20 21:27:45 +0100617static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200618{
Willy Tarreau591d4452018-06-15 17:21:00 +0200619 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200620}
621
622
Willy Tarreau715d5312017-07-11 15:20:24 +0200623/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
624 * is not obvious. It turns out that H2 headers are neither aligned nor do they
625 * use regular sizes. And to add to the trouble, the buffer may wrap so each
626 * byte read must be checked. The header is formed like this :
627 *
628 * b0 b1 b2 b3 b4 b5..b8
629 * +----------+---------+--------+----+----+----------------------+
630 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
631 * +----------+---------+--------+----+----+----------------------+
632 *
633 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
634 * we get the sid properly aligned and ordered, and 16 bits of len properly
635 * ordered as well. The type and flags can be extracted using bit shifts from
636 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200637 * Returns zero if some bytes are missing, otherwise non-zero on success. The
638 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200639 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100640static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200641{
642 uint64_t w;
643
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200644 if (b_data(b) < 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200645 return 0;
646
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200647 w = h2_get_n64(b, 1);
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200648 h->len = *(uint8_t*)b_head(b) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200649 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
650 h->ff = w >> 32;
651 h->ft = w >> 40;
652 h->len += w >> 48;
653 return 1;
654}
655
656/* skip the next 9 bytes corresponding to the frame header possibly parsed by
657 * h2_peek_frame_hdr() above.
658 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100659static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200660{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200661 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200662}
663
664/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100665static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200666{
667 int ret;
668
669 ret = h2_peek_frame_hdr(b, h);
670 if (ret > 0)
671 h2_skip_frame_hdr(b);
672 return ret;
673}
674
Willy Tarreau00dd0782018-03-01 16:31:34 +0100675/* marks stream <h2s> as CLOSED and decrement the number of active streams for
676 * its connection if the stream was not yet closed. Please use this exclusively
677 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100678 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100679static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100680{
681 if (h2s->st != H2_SS_CLOSED)
682 h2s->h2c->nb_streams--;
683 h2s->st = H2_SS_CLOSED;
684}
685
Willy Tarreau71049cc2018-03-28 13:56:39 +0200686/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
687static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100688{
689 h2s_close(h2s);
690 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200691 if (b_size(&h2s->rxbuf)) {
692 b_free(&h2s->rxbuf);
693 offer_buffers(NULL, tasks_run_queue);
694 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200695 if (h2s->send_wait != NULL)
696 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
697 if (h2s->recv_wait != NULL)
698 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800699 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200700 * reference left would be in the h2c send_list/fctl_list, and if
701 * we're in it, we're getting out anyway
702 */
703 LIST_DEL(&h2s->list);
704 LIST_INIT(&h2s->list);
705 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100706 pool_free(pool_head_h2s, h2s);
707}
708
Willy Tarreaua8e49542018-10-03 18:53:55 +0200709/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
710 * stream tree. In case of error, nothing is added and NULL is returned. The
711 * causes of errors can be any failed memory allocation. The caller is
712 * responsible for checking if the connection may support an extra stream
713 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200714 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200715static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200716{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200717 struct h2s *h2s;
718
Willy Tarreaubafbe012017-11-24 17:34:44 +0100719 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200720 if (!h2s)
721 goto out;
722
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200723 h2s->wait_event.task = tasklet_new();
724 if (!h2s->wait_event.task) {
725 pool_free(pool_head_h2s, h2s);
726 goto out;
727 }
728 h2s->send_wait = NULL;
729 h2s->recv_wait = NULL;
730 h2s->wait_event.task->process = h2_deferred_shut;
731 h2s->wait_event.task->context = h2s;
732 h2s->wait_event.handle = NULL;
733 h2s->wait_event.wait_reason = 0;
734 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200735 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200736 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200737 h2s->mws = h2c->miw;
738 h2s->flags = H2_SF_NONE;
739 h2s->errcode = H2_ERR_NO_ERROR;
740 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200741 h2s->status = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200742 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200743
744 if (h2c->flags & H2_CF_IS_BACK) {
745 h1m_init_req(&h2s->h1m);
746 h2s->h1m.err_pos = -1; // don't care about errors on the request path
747 h2s->h1m.flags |= H1_MF_TOLOWER;
748 } else {
749 h1m_init_res(&h2s->h1m);
750 h2s->h1m.err_pos = -1; // don't care about errors on the response path
751 h2s->h1m.flags |= H1_MF_TOLOWER;
752 }
753
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200754 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200755 if (id > 0)
756 h2c->max_id = id;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200757
758 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100759 h2c->nb_streams++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200760
761 return h2s;
762
763 out_free_h2s:
764 pool_free(pool_head_h2s, h2s);
765 out:
766 return NULL;
767}
768
769/* creates a new stream <id> on the h2c connection and returns it, or NULL in
770 * case of memory allocation error.
771 */
772static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
773{
774 struct session *sess = h2c->conn->owner;
775 struct conn_stream *cs;
776 struct h2s *h2s;
777
778 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
779 goto out;
780
781 h2s = h2s_new(h2c, id);
782 if (!h2s)
783 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200784
785 cs = cs_new(h2c->conn);
786 if (!cs)
787 goto out_close;
788
789 h2s->cs = cs;
790 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200791 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200792
793 if (stream_create_from_cs(cs) < 0)
794 goto out_free_cs;
795
Willy Tarreau590a0512018-09-05 11:56:48 +0200796 /* We want the accept date presented to the next stream to be the one
797 * we have now, the handshake time to be null (since the next stream
798 * is not delayed by a handshake), and the idle time to count since
799 * right now.
800 */
801 sess->accept_date = date;
802 sess->tv_accept = now;
803 sess->t_handshake = 0;
804
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200805 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200806 if (h2_has_too_many_cs(h2c))
807 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200808 return h2s;
809
810 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200811 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200812 cs_free(cs);
813 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200814 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200815 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200816 sess_log(sess);
817 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200818}
819
Willy Tarreau751f2d02018-10-05 09:35:00 +0200820/* allocates a new stream associated to conn_stream <cs> on the h2c connection
821 * and returns it, or NULL in case of memory allocation error or if the highest
822 * possible stream ID was reached.
823 */
824static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs)
825{
826 struct h2s *h2s = NULL;
827
828 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
829 goto out;
830
831 /* Defer choosing the ID until we send the first message to create the stream */
832 h2s = h2s_new(h2c, 0);
833 if (!h2s)
834 goto out;
835
836 h2s->cs = cs;
837 cs->ctx = h2s;
838 h2c->nb_cs++;
839
Willy Tarreau751f2d02018-10-05 09:35:00 +0200840 out:
841 return h2s;
842}
843
Willy Tarreaube5b7152017-09-25 16:25:39 +0200844/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
845 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
846 * the various settings codes.
847 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200848static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +0200849{
850 struct buffer *res;
851 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200852 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200853 int ret;
854
855 if (h2c_mux_busy(h2c, NULL)) {
856 h2c->flags |= H2_CF_DEM_MBUSY;
857 return 0;
858 }
859
Willy Tarreau44e973f2018-03-01 17:49:30 +0100860 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200861 if (!res) {
862 h2c->flags |= H2_CF_MUX_MALLOC;
863 h2c->flags |= H2_CF_DEM_MROOM;
864 return 0;
865 }
866
867 chunk_init(&buf, buf_data, sizeof(buf_data));
868 chunk_memcpy(&buf,
869 "\x00\x00\x00" /* length : 0 for now */
870 "\x04\x00" /* type : 4 (settings), flags : 0 */
871 "\x00\x00\x00\x00", /* stream ID : 0 */
872 9);
873
874 if (h2_settings_header_table_size != 4096) {
875 char str[6] = "\x00\x01"; /* header_table_size */
876
877 write_n32(str + 2, h2_settings_header_table_size);
878 chunk_memcat(&buf, str, 6);
879 }
880
881 if (h2_settings_initial_window_size != 65535) {
882 char str[6] = "\x00\x04"; /* initial_window_size */
883
884 write_n32(str + 2, h2_settings_initial_window_size);
885 chunk_memcat(&buf, str, 6);
886 }
887
888 if (h2_settings_max_concurrent_streams != 0) {
889 char str[6] = "\x00\x03"; /* max_concurrent_streams */
890
891 /* Note: 0 means "unlimited" for haproxy's config but not for
892 * the protocol, so never send this value!
893 */
894 write_n32(str + 2, h2_settings_max_concurrent_streams);
895 chunk_memcat(&buf, str, 6);
896 }
897
898 if (global.tune.bufsize != 16384) {
899 char str[6] = "\x00\x05"; /* max_frame_size */
900
901 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
902 * match bufsize - rewrite size, but at the moment it seems
903 * that clients don't take care of it.
904 */
905 write_n32(str + 2, global.tune.bufsize);
906 chunk_memcat(&buf, str, 6);
907 }
908
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200909 h2_set_frame_size(buf.area, buf.data - 9);
910 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200911 if (unlikely(ret <= 0)) {
912 if (!ret) {
913 h2c->flags |= H2_CF_MUX_MFULL;
914 h2c->flags |= H2_CF_DEM_MROOM;
915 return 0;
916 }
917 else {
918 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
919 return 0;
920 }
921 }
922 return ret;
923}
924
Willy Tarreau52eed752017-09-22 15:05:09 +0200925/* Try to receive a connection preface, then upon success try to send our
926 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
927 * missing data. It may return an error in h2c.
928 */
929static int h2c_frt_recv_preface(struct h2c *h2c)
930{
931 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200932 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200933
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200934 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200935
936 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +0200937 if (ret1 < 0)
938 sess_log(h2c->conn->owner);
939
Willy Tarreau52eed752017-09-22 15:05:09 +0200940 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
941 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
942 return 0;
943 }
944
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200945 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200946 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200947 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200948
Willy Tarreaube5b7152017-09-25 16:25:39 +0200949 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200950}
951
Willy Tarreau01b44822018-10-03 14:26:37 +0200952/* Try to send a connection preface, then upon success try to send our
953 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
954 * missing data. It may return an error in h2c.
955 */
956static int h2c_bck_send_preface(struct h2c *h2c)
957{
958 struct buffer *res;
959
960 if (h2c_mux_busy(h2c, NULL)) {
961 h2c->flags |= H2_CF_DEM_MBUSY;
962 return 0;
963 }
964
965 res = h2_get_buf(h2c, &h2c->mbuf);
966 if (!res) {
967 h2c->flags |= H2_CF_MUX_MALLOC;
968 h2c->flags |= H2_CF_DEM_MROOM;
969 return 0;
970 }
971
972 if (!b_data(res)) {
973 /* preface not yet sent */
974 b_istput(res, ist(H2_CONN_PREFACE));
975 }
976
977 return h2c_send_settings(h2c);
978}
979
Willy Tarreau081d4722017-05-16 21:51:05 +0200980/* try to send a GOAWAY frame on the connection to report an error or a graceful
981 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
982 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
983 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
984 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
985 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
986 * on unrecoverable failure. It will not attempt to send one again in this last
987 * case so that it is safe to use h2c_error() to report such errors.
988 */
989static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
990{
991 struct buffer *res;
992 char str[17];
993 int ret;
994
995 if (h2c->flags & H2_CF_GOAWAY_FAILED)
996 return 1; // claim that it worked
997
998 if (h2c_mux_busy(h2c, h2s)) {
999 if (h2s)
1000 h2s->flags |= H2_SF_BLK_MBUSY;
1001 else
1002 h2c->flags |= H2_CF_DEM_MBUSY;
1003 return 0;
1004 }
1005
Willy Tarreau44e973f2018-03-01 17:49:30 +01001006 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001007 if (!res) {
1008 h2c->flags |= H2_CF_MUX_MALLOC;
1009 if (h2s)
1010 h2s->flags |= H2_SF_BLK_MROOM;
1011 else
1012 h2c->flags |= H2_CF_DEM_MROOM;
1013 return 0;
1014 }
1015
1016 /* len: 8, type: 7, flags: none, sid: 0 */
1017 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1018
1019 if (h2c->last_sid < 0)
1020 h2c->last_sid = h2c->max_id;
1021
1022 write_n32(str + 9, h2c->last_sid);
1023 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001024 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001025 if (unlikely(ret <= 0)) {
1026 if (!ret) {
1027 h2c->flags |= H2_CF_MUX_MFULL;
1028 if (h2s)
1029 h2s->flags |= H2_SF_BLK_MROOM;
1030 else
1031 h2c->flags |= H2_CF_DEM_MROOM;
1032 return 0;
1033 }
1034 else {
1035 /* we cannot report this error using GOAWAY, so we mark
1036 * it and claim a success.
1037 */
1038 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1039 h2c->flags |= H2_CF_GOAWAY_FAILED;
1040 return 1;
1041 }
1042 }
1043 h2c->flags |= H2_CF_GOAWAY_SENT;
1044 return ret;
1045}
1046
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001047/* Try to send an RST_STREAM frame on the connection for the indicated stream
1048 * during mux operations. This stream must be valid and cannot be closed
1049 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1050 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1051 * not yet.
1052 *
1053 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1054 * to write the message, it subscribes the stream to future notifications.
1055 */
1056static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1057{
1058 struct buffer *res;
1059 char str[13];
1060 int ret;
1061
1062 if (!h2s || h2s->st == H2_SS_CLOSED)
1063 return 1;
1064
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001065 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1066 * RST_STREAM in response to a RST_STREAM frame.
1067 */
1068 if (h2c->dft == H2_FT_RST_STREAM) {
1069 ret = 1;
1070 goto ignore;
1071 }
1072
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001073 if (h2c_mux_busy(h2c, h2s)) {
1074 h2s->flags |= H2_SF_BLK_MBUSY;
1075 return 0;
1076 }
1077
Willy Tarreau44e973f2018-03-01 17:49:30 +01001078 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001079 if (!res) {
1080 h2c->flags |= H2_CF_MUX_MALLOC;
1081 h2s->flags |= H2_SF_BLK_MROOM;
1082 return 0;
1083 }
1084
1085 /* len: 4, type: 3, flags: none */
1086 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1087 write_n32(str + 5, h2s->id);
1088 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001089 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001090
1091 if (unlikely(ret <= 0)) {
1092 if (!ret) {
1093 h2c->flags |= H2_CF_MUX_MFULL;
1094 h2s->flags |= H2_SF_BLK_MROOM;
1095 return 0;
1096 }
1097 else {
1098 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1099 return 0;
1100 }
1101 }
1102
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001103 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001104 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001105 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001106 return ret;
1107}
1108
1109/* Try to send an RST_STREAM frame on the connection for the stream being
1110 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
1111 * error code unless the stream's state already is IDLE or CLOSED in which
1112 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
1113 * it was not yet.
1114 *
1115 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1116 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001117 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001118 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001119 */
1120static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1121{
1122 struct buffer *res;
1123 char str[13];
1124 int ret;
1125
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001126 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1127 * RST_STREAM in response to a RST_STREAM frame.
1128 */
1129 if (h2c->dft == H2_FT_RST_STREAM) {
1130 ret = 1;
1131 goto ignore;
1132 }
1133
Willy Tarreau27a84c92017-10-17 08:10:17 +02001134 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001135 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001136 return 0;
1137 }
1138
Willy Tarreau44e973f2018-03-01 17:49:30 +01001139 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001140 if (!res) {
1141 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001142 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001143 return 0;
1144 }
1145
1146 /* len: 4, type: 3, flags: none */
1147 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001148
Willy Tarreau27a84c92017-10-17 08:10:17 +02001149 write_n32(str + 5, h2c->dsi);
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001150 write_n32(str + 9, h2s->id ? h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001151 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001152
Willy Tarreau27a84c92017-10-17 08:10:17 +02001153 if (unlikely(ret <= 0)) {
1154 if (!ret) {
1155 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001156 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001157 return 0;
1158 }
1159 else {
1160 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1161 return 0;
1162 }
1163 }
1164
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001165 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001166 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001167 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001168 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001169 }
1170
Willy Tarreau27a84c92017-10-17 08:10:17 +02001171 return ret;
1172}
1173
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001174/* try to send an empty DATA frame with the ES flag set to notify about the
1175 * end of stream and match a shutdown(write). If an ES was already sent as
1176 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1177 * on success or zero if nothing was done. In case of lack of room to write the
1178 * message, it subscribes the requesting stream to future notifications.
1179 */
1180static int h2_send_empty_data_es(struct h2s *h2s)
1181{
1182 struct h2c *h2c = h2s->h2c;
1183 struct buffer *res;
1184 char str[9];
1185 int ret;
1186
Willy Tarreau721c9742017-11-07 11:05:42 +01001187 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001188 return 1;
1189
1190 if (h2c_mux_busy(h2c, h2s)) {
1191 h2s->flags |= H2_SF_BLK_MBUSY;
1192 return 0;
1193 }
1194
Willy Tarreau44e973f2018-03-01 17:49:30 +01001195 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001196 if (!res) {
1197 h2c->flags |= H2_CF_MUX_MALLOC;
1198 h2s->flags |= H2_SF_BLK_MROOM;
1199 return 0;
1200 }
1201
1202 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1203 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1204 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001205 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001206 if (likely(ret > 0)) {
1207 h2s->flags |= H2_SF_ES_SENT;
1208 }
1209 else if (!ret) {
1210 h2c->flags |= H2_CF_MUX_MFULL;
1211 h2s->flags |= H2_SF_BLK_MROOM;
1212 return 0;
1213 }
1214 else {
1215 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1216 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001217 }
1218 return ret;
1219}
1220
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001221/* wake the streams attached to the connection, whose id is greater than <last>,
1222 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001223 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1224 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001225 */
1226static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1227{
1228 struct eb32_node *node;
1229 struct h2s *h2s;
1230
1231 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1232 flags |= CS_FL_ERROR;
1233
1234 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001235 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001236
1237 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1238 while (node) {
1239 h2s = container_of(node, struct h2s, by_id);
1240 if (h2s->id <= last)
1241 break;
1242 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001243
1244 if (!h2s->cs) {
1245 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001246 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001247 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001248 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001249
1250 h2s->cs->flags |= flags;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001251 if (h2s->recv_wait) {
1252 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001253 sw->wait_reason &= ~SUB_CAN_RECV;
1254 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001255 h2s->recv_wait = NULL;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02001256 } else if (h2s->cs->data_cb->wake != NULL)
1257 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001258
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001259 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1260 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001261 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001262 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001263 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001264 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001265 }
1266}
1267
Willy Tarreau3421aba2017-07-27 15:41:03 +02001268/* Increase all streams' outgoing window size by the difference passed in
1269 * argument. This is needed upon receipt of the settings frame if the initial
1270 * window size is different. The difference may be negative and the resulting
1271 * window size as well, for the time it takes to receive some window updates.
1272 */
1273static void h2c_update_all_ws(struct h2c *h2c, int diff)
1274{
1275 struct h2s *h2s;
1276 struct eb32_node *node;
1277
1278 if (!diff)
1279 return;
1280
1281 node = eb32_first(&h2c->streams_by_id);
1282 while (node) {
1283 h2s = container_of(node, struct h2s, by_id);
1284 h2s->mws += diff;
1285 node = eb32_next(node);
1286 }
1287}
1288
1289/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1290 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1291 * return an error in h2c. Described in RFC7540#6.5.
1292 */
1293static int h2c_handle_settings(struct h2c *h2c)
1294{
1295 unsigned int offset;
1296 int error;
1297
1298 if (h2c->dff & H2_F_SETTINGS_ACK) {
1299 if (h2c->dfl) {
1300 error = H2_ERR_FRAME_SIZE_ERROR;
1301 goto fail;
1302 }
1303 return 1;
1304 }
1305
1306 if (h2c->dsi != 0) {
1307 error = H2_ERR_PROTOCOL_ERROR;
1308 goto fail;
1309 }
1310
1311 if (h2c->dfl % 6) {
1312 error = H2_ERR_FRAME_SIZE_ERROR;
1313 goto fail;
1314 }
1315
1316 /* that's the limit we can process */
1317 if (h2c->dfl > global.tune.bufsize) {
1318 error = H2_ERR_FRAME_SIZE_ERROR;
1319 goto fail;
1320 }
1321
1322 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001323 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001324 return 0;
1325
1326 /* parse the frame */
1327 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001328 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1329 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001330
1331 switch (type) {
1332 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1333 /* we need to update all existing streams with the
1334 * difference from the previous iws.
1335 */
1336 if (arg < 0) { // RFC7540#6.5.2
1337 error = H2_ERR_FLOW_CONTROL_ERROR;
1338 goto fail;
1339 }
1340 h2c_update_all_ws(h2c, arg - h2c->miw);
1341 h2c->miw = arg;
1342 break;
1343 case H2_SETTINGS_MAX_FRAME_SIZE:
1344 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1345 error = H2_ERR_PROTOCOL_ERROR;
1346 goto fail;
1347 }
1348 h2c->mfs = arg;
1349 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001350 case H2_SETTINGS_ENABLE_PUSH:
1351 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1352 error = H2_ERR_PROTOCOL_ERROR;
1353 goto fail;
1354 }
1355 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001356 }
1357 }
1358
1359 /* need to ACK this frame now */
1360 h2c->st0 = H2_CS_FRAME_A;
1361 return 1;
1362 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001363 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001364 h2c_error(h2c, error);
1365 return 0;
1366}
1367
1368/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1369 * success or one of the h2_status values.
1370 */
1371static int h2c_ack_settings(struct h2c *h2c)
1372{
1373 struct buffer *res;
1374 char str[9];
1375 int ret = -1;
1376
1377 if (h2c_mux_busy(h2c, NULL)) {
1378 h2c->flags |= H2_CF_DEM_MBUSY;
1379 return 0;
1380 }
1381
Willy Tarreau44e973f2018-03-01 17:49:30 +01001382 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001383 if (!res) {
1384 h2c->flags |= H2_CF_MUX_MALLOC;
1385 h2c->flags |= H2_CF_DEM_MROOM;
1386 return 0;
1387 }
1388
1389 memcpy(str,
1390 "\x00\x00\x00" /* length : 0 (no data) */
1391 "\x04" "\x01" /* type : 4, flags : ACK */
1392 "\x00\x00\x00\x00" /* stream ID */, 9);
1393
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001394 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001395 if (unlikely(ret <= 0)) {
1396 if (!ret) {
1397 h2c->flags |= H2_CF_MUX_MFULL;
1398 h2c->flags |= H2_CF_DEM_MROOM;
1399 return 0;
1400 }
1401 else {
1402 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1403 return 0;
1404 }
1405 }
1406 return ret;
1407}
1408
Willy Tarreaucf68c782017-10-10 17:11:41 +02001409/* processes a PING frame and schedules an ACK if needed. The caller must pass
1410 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1411 * missing data. It may return an error in h2c.
1412 */
1413static int h2c_handle_ping(struct h2c *h2c)
1414{
1415 /* frame length must be exactly 8 */
1416 if (h2c->dfl != 8) {
1417 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1418 return 0;
1419 }
1420
1421 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001422 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001423 h2c->st0 = H2_CS_FRAME_A;
1424 return 1;
1425}
1426
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001427/* Try to send a window update for stream id <sid> and value <increment>.
1428 * Returns > 0 on success or zero on missing room or failure. It may return an
1429 * error in h2c.
1430 */
1431static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1432{
1433 struct buffer *res;
1434 char str[13];
1435 int ret = -1;
1436
1437 if (h2c_mux_busy(h2c, NULL)) {
1438 h2c->flags |= H2_CF_DEM_MBUSY;
1439 return 0;
1440 }
1441
Willy Tarreau44e973f2018-03-01 17:49:30 +01001442 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001443 if (!res) {
1444 h2c->flags |= H2_CF_MUX_MALLOC;
1445 h2c->flags |= H2_CF_DEM_MROOM;
1446 return 0;
1447 }
1448
1449 /* length: 4, type: 8, flags: none */
1450 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1451 write_n32(str + 5, sid);
1452 write_n32(str + 9, increment);
1453
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001454 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001455
1456 if (unlikely(ret <= 0)) {
1457 if (!ret) {
1458 h2c->flags |= H2_CF_MUX_MFULL;
1459 h2c->flags |= H2_CF_DEM_MROOM;
1460 return 0;
1461 }
1462 else {
1463 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1464 return 0;
1465 }
1466 }
1467 return ret;
1468}
1469
1470/* try to send pending window update for the connection. It's safe to call it
1471 * with no pending updates. Returns > 0 on success or zero on missing room or
1472 * failure. It may return an error in h2c.
1473 */
1474static int h2c_send_conn_wu(struct h2c *h2c)
1475{
1476 int ret = 1;
1477
1478 if (h2c->rcvd_c <= 0)
1479 return 1;
1480
1481 /* send WU for the connection */
1482 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1483 if (ret > 0)
1484 h2c->rcvd_c = 0;
1485
1486 return ret;
1487}
1488
1489/* try to send pending window update for the current dmux stream. It's safe to
1490 * call it with no pending updates. Returns > 0 on success or zero on missing
1491 * room or failure. It may return an error in h2c.
1492 */
1493static int h2c_send_strm_wu(struct h2c *h2c)
1494{
1495 int ret = 1;
1496
1497 if (h2c->rcvd_s <= 0)
1498 return 1;
1499
1500 /* send WU for the stream */
1501 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1502 if (ret > 0)
1503 h2c->rcvd_s = 0;
1504
1505 return ret;
1506}
1507
Willy Tarreaucf68c782017-10-10 17:11:41 +02001508/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1509 * success, 0 on missing data or one of the h2_status values.
1510 */
1511static int h2c_ack_ping(struct h2c *h2c)
1512{
1513 struct buffer *res;
1514 char str[17];
1515 int ret = -1;
1516
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001517 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001518 return 0;
1519
1520 if (h2c_mux_busy(h2c, NULL)) {
1521 h2c->flags |= H2_CF_DEM_MBUSY;
1522 return 0;
1523 }
1524
Willy Tarreau44e973f2018-03-01 17:49:30 +01001525 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001526 if (!res) {
1527 h2c->flags |= H2_CF_MUX_MALLOC;
1528 h2c->flags |= H2_CF_DEM_MROOM;
1529 return 0;
1530 }
1531
1532 memcpy(str,
1533 "\x00\x00\x08" /* length : 8 (same payload) */
1534 "\x06" "\x01" /* type : 6, flags : ACK */
1535 "\x00\x00\x00\x00" /* stream ID */, 9);
1536
1537 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001538 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001539
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001540 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001541 if (unlikely(ret <= 0)) {
1542 if (!ret) {
1543 h2c->flags |= H2_CF_MUX_MFULL;
1544 h2c->flags |= H2_CF_DEM_MROOM;
1545 return 0;
1546 }
1547 else {
1548 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1549 return 0;
1550 }
1551 }
1552 return ret;
1553}
1554
Willy Tarreau26f95952017-07-27 17:18:30 +02001555/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1556 * Returns > 0 on success or zero on missing data. It may return an error in
1557 * h2c or h2s. Described in RFC7540#6.9.
1558 */
1559static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1560{
1561 int32_t inc;
1562 int error;
1563
1564 if (h2c->dfl != 4) {
1565 error = H2_ERR_FRAME_SIZE_ERROR;
1566 goto conn_err;
1567 }
1568
1569 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001570 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001571 return 0;
1572
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001573 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001574
1575 if (h2c->dsi != 0) {
1576 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001577
1578 /* it's not an error to receive WU on a closed stream */
1579 if (h2s->st == H2_SS_CLOSED)
1580 return 1;
1581
1582 if (!inc) {
1583 error = H2_ERR_PROTOCOL_ERROR;
1584 goto strm_err;
1585 }
1586
1587 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1588 error = H2_ERR_FLOW_CONTROL_ERROR;
1589 goto strm_err;
1590 }
1591
1592 h2s->mws += inc;
1593 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1594 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001595 if (h2s->send_wait)
1596 LIST_ADDQ(&h2c->send_list, &h2s->list);
1597
Willy Tarreau26f95952017-07-27 17:18:30 +02001598 }
1599 }
1600 else {
1601 /* connection window update */
1602 if (!inc) {
1603 error = H2_ERR_PROTOCOL_ERROR;
1604 goto conn_err;
1605 }
1606
1607 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1608 error = H2_ERR_FLOW_CONTROL_ERROR;
1609 goto conn_err;
1610 }
1611
1612 h2c->mws += inc;
1613 }
1614
1615 return 1;
1616
1617 conn_err:
1618 h2c_error(h2c, error);
1619 return 0;
1620
1621 strm_err:
1622 if (h2s) {
1623 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001624 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001625 }
1626 else
1627 h2c_error(h2c, error);
1628 return 0;
1629}
1630
Willy Tarreaue96b0922017-10-30 00:28:29 +01001631/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1632 * the last ID. Returns > 0 on success or zero on missing data. It may return
1633 * an error in h2c. Described in RFC7540#6.8.
1634 */
1635static int h2c_handle_goaway(struct h2c *h2c)
1636{
1637 int error;
1638 int last;
1639
1640 if (h2c->dsi != 0) {
1641 error = H2_ERR_PROTOCOL_ERROR;
1642 goto conn_err;
1643 }
1644
1645 if (h2c->dfl < 8) {
1646 error = H2_ERR_FRAME_SIZE_ERROR;
1647 goto conn_err;
1648 }
1649
1650 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001651 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001652 return 0;
1653
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001654 last = h2_get_n32(&h2c->dbuf, 0);
1655 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001656 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001657 if (h2c->last_sid < 0)
1658 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001659 return 1;
1660
1661 conn_err:
1662 h2c_error(h2c, error);
1663 return 0;
1664}
1665
Willy Tarreau92153fc2017-12-03 19:46:19 +01001666/* processes a PRIORITY frame, and either skips it or rejects if it is
1667 * invalid. Returns > 0 on success or zero on missing data. It may return
1668 * an error in h2c. Described in RFC7540#6.3.
1669 */
1670static int h2c_handle_priority(struct h2c *h2c)
1671{
1672 int error;
1673
1674 if (h2c->dsi == 0) {
1675 error = H2_ERR_PROTOCOL_ERROR;
1676 goto conn_err;
1677 }
1678
1679 if (h2c->dfl != 5) {
1680 error = H2_ERR_FRAME_SIZE_ERROR;
1681 goto conn_err;
1682 }
1683
1684 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001685 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001686 return 0;
1687
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001688 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001689 /* 7540#5.3 : can't depend on itself */
1690 error = H2_ERR_PROTOCOL_ERROR;
1691 goto conn_err;
1692 }
1693 return 1;
1694
1695 conn_err:
1696 h2c_error(h2c, error);
1697 return 0;
1698}
1699
Willy Tarreaucd234e92017-08-18 10:59:39 +02001700/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1701 * Returns > 0 on success or zero on missing data. It may return an error in
1702 * h2c. Described in RFC7540#6.4.
1703 */
1704static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1705{
1706 int error;
1707
1708 if (h2c->dsi == 0) {
1709 error = H2_ERR_PROTOCOL_ERROR;
1710 goto conn_err;
1711 }
1712
Willy Tarreaucd234e92017-08-18 10:59:39 +02001713 if (h2c->dfl != 4) {
1714 error = H2_ERR_FRAME_SIZE_ERROR;
1715 goto conn_err;
1716 }
1717
1718 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001719 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001720 return 0;
1721
1722 /* late RST, already handled */
1723 if (h2s->st == H2_SS_CLOSED)
1724 return 1;
1725
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001726 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001727 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001728
1729 if (h2s->cs) {
Willy Tarreau2c096c32018-09-12 09:45:54 +02001730 h2s->cs->flags |= CS_FL_REOS | CS_FL_ERROR;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001731 if (h2s->recv_wait) {
1732 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001733
1734 sw->wait_reason &= ~SUB_CAN_RECV;
1735 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001736 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001737 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02001738 }
1739
1740 h2s->flags |= H2_SF_RST_RCVD;
1741 return 1;
1742
1743 conn_err:
1744 h2c_error(h2c, error);
1745 return 0;
1746}
1747
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001748/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1749 * It may return an error in h2c or h2s. The caller must consider that the
1750 * return value is the new h2s in case one was allocated (most common case).
1751 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001752 * errors here are reported as connection errors since it's impossible to
1753 * recover from such errors after the compression context has been altered.
1754 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001755static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001756{
1757 int error;
1758
1759 if (!h2c->dfl) {
1760 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001761 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001762 goto strm_err;
1763 }
1764
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001765 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001766 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001767
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001768 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001769 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001770
Willy Tarreauf2101912018-07-19 10:11:38 +02001771 if (h2c->flags & H2_CF_DEM_TOOMANY)
1772 return 0; // too many cs still present
1773
Willy Tarreau13278b42017-10-13 19:23:14 +02001774 /* now either the frame is complete or the buffer is complete */
1775 if (h2s->st != H2_SS_IDLE) {
1776 /* FIXME: stream already exists, this is only allowed for
1777 * trailers (not supported for now).
1778 */
1779 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001780 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001781 goto conn_err;
1782 }
1783 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1784 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1785 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001786 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001787 goto conn_err;
1788 }
1789
Willy Tarreau22de8d32018-09-05 19:55:58 +02001790 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001791 * positively from h2c_frt_stream_new(), the stream will report the error,
1792 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001793 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001794 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001795 if (!h2s) {
1796 error = H2_ERR_INTERNAL_ERROR;
1797 goto conn_err;
1798 }
1799
1800 h2s->st = H2_SS_OPEN;
1801 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1802 h2s->st = H2_SS_HREM;
1803 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001804 /* note: cs cannot be null for now (just created above) */
1805 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001806 }
1807
Willy Tarreauc3e18f32018-10-08 14:51:56 +02001808 if (!h2s_decode_headers(h2s))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001809 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001810
Willy Tarreau8f650c32017-11-21 19:36:21 +01001811 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001812 return NULL;
Willy Tarreau8f650c32017-11-21 19:36:21 +01001813
Willy Tarreau721c9742017-11-07 11:05:42 +01001814 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001815 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001816 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001817 }
1818 else {
1819 /* update the max stream ID if the request is being processed */
1820 if (h2s->id > h2c->max_id)
1821 h2c->max_id = h2s->id;
1822 }
1823
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001824 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001825
1826 conn_err:
1827 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001828 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001829
1830 strm_err:
1831 if (h2s) {
1832 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001833 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001834 }
1835 else
1836 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001837 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001838}
1839
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001840/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1841 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1842 * errors here are reported as connection errors since it's impossible to
1843 * recover from such errors after the compression context has been altered.
1844 */
1845static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
1846{
1847 int error;
1848
1849 if (!h2c->dfl) {
1850 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1851 sess_log(h2c->conn->owner);
1852 goto strm_err;
1853 }
1854
1855 if (!b_size(&h2c->dbuf))
1856 return NULL; // empty buffer
1857
1858 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
1859 return NULL; // incomplete frame
1860
1861 if (h2c->flags & H2_CF_DEM_TOOMANY)
1862 return 0; // too many cs still present
1863
1864 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1865 h2s->flags |= H2_SF_ES_RCVD;
1866 h2s->cs->flags |= CS_FL_REOS;
1867 }
1868
1869 if (!h2s_decode_headers(h2s))
1870 return NULL;
1871
1872 if (h2c->st0 >= H2_CS_ERROR)
1873 return NULL;
1874
1875 if (h2s->st >= H2_SS_ERROR) {
1876 /* stream error : send RST_STREAM */
1877 h2c->st0 = H2_CS_FRAME_E;
1878 }
1879
1880 if (h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1881 h2s->st = H2_SS_ERROR;
1882 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
1883 h2s->st = H2_SS_HREM;
1884 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
1885 h2s_close(h2s);
1886
1887 return h2s;
1888
1889 conn_err:
1890 h2c_error(h2c, error);
1891 return NULL;
1892
1893 strm_err:
1894 if (h2s) {
1895 h2s_error(h2s, error);
1896 h2c->st0 = H2_CS_FRAME_E;
1897 }
1898 else
1899 h2c_error(h2c, error);
1900 return NULL;
1901}
1902
Willy Tarreau454f9052017-10-26 19:40:35 +02001903/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1904 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1905 */
1906static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1907{
1908 int error;
1909
1910 /* note that empty DATA frames are perfectly valid and sometimes used
1911 * to signal an end of stream (with the ES flag).
1912 */
1913
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001914 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001915 return 0; // empty buffer
1916
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001917 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001918 return 0; // incomplete frame
1919
1920 /* now either the frame is complete or the buffer is complete */
1921
1922 if (!h2c->dsi) {
1923 /* RFC7540#6.1 */
1924 error = H2_ERR_PROTOCOL_ERROR;
1925 goto conn_err;
1926 }
1927
1928 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1929 /* RFC7540#6.1 */
1930 error = H2_ERR_STREAM_CLOSED;
1931 goto strm_err;
1932 }
1933
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001934 if (!h2_frt_transfer_data(h2s))
1935 return 0;
1936
Willy Tarreau454f9052017-10-26 19:40:35 +02001937 /* call the upper layers to process the frame, then let the upper layer
1938 * notify the stream about any change.
1939 */
1940 if (!h2s->cs) {
1941 error = H2_ERR_STREAM_CLOSED;
1942 goto strm_err;
1943 }
1944
Willy Tarreau8f650c32017-11-21 19:36:21 +01001945 if (h2c->st0 >= H2_CS_ERROR)
1946 return 0;
1947
Willy Tarreau721c9742017-11-07 11:05:42 +01001948 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001949 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001950 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001951 }
1952
1953 /* check for completion : the callee will change this to FRAME_A or
1954 * FRAME_H once done.
1955 */
1956 if (h2c->st0 == H2_CS_FRAME_P)
1957 return 0;
1958
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001959
1960 /* last frame */
1961 if (h2c->dff & H2_F_DATA_END_STREAM) {
1962 h2s->st = H2_SS_HREM;
1963 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001964 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001965 }
1966
Willy Tarreau454f9052017-10-26 19:40:35 +02001967 return 1;
1968
1969 conn_err:
1970 h2c_error(h2c, error);
1971 return 0;
1972
1973 strm_err:
1974 if (h2s) {
1975 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001976 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001977 }
1978 else
1979 h2c_error(h2c, error);
1980 return 0;
1981}
1982
Willy Tarreaubc933932017-10-09 16:21:43 +02001983/* process Rx frames to be demultiplexed */
1984static void h2_process_demux(struct h2c *h2c)
1985{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001986 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001987
Willy Tarreau081d4722017-05-16 21:51:05 +02001988 if (h2c->st0 >= H2_CS_ERROR)
1989 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001990
1991 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1992 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02001993 if (h2c->flags & H2_CF_IS_BACK)
1994 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001995 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1996 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001997 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02001998 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001999 sess_log(h2c->conn->owner);
2000 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002001 goto fail;
2002 }
2003
2004 h2c->max_id = 0;
2005 h2c->st0 = H2_CS_SETTINGS1;
2006 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002007
2008 if (h2c->st0 == H2_CS_SETTINGS1) {
2009 struct h2_fh hdr;
2010
2011 /* ensure that what is pending is a valid SETTINGS frame
2012 * without an ACK.
2013 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002014 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002015 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002016 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002017 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002018 sess_log(h2c->conn->owner);
2019 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002020 goto fail;
2021 }
2022
2023 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2024 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2025 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2026 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002027 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002028 goto fail;
2029 }
2030
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002031 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002032 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2033 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2034 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002035 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002036 goto fail;
2037 }
2038
2039 /* that's OK, switch to FRAME_P to process it */
2040 h2c->dfl = hdr.len;
2041 h2c->dsi = hdr.sid;
2042 h2c->dft = hdr.ft;
2043 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002044 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002045 h2c->st0 = H2_CS_FRAME_P;
2046 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002047 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002048
2049 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002050 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002051 int ret = 0;
2052
2053 if (h2c->st0 >= H2_CS_ERROR)
2054 break;
2055
2056 if (h2c->st0 == H2_CS_FRAME_H) {
2057 struct h2_fh hdr;
2058
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002059 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002060 break;
2061
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002062 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002063 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2064 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002065 if (!h2c->nb_streams) {
2066 /* only log if no other stream can report the error */
2067 sess_log(h2c->conn->owner);
2068 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002069 break;
2070 }
2071
2072 h2c->dfl = hdr.len;
2073 h2c->dsi = hdr.sid;
2074 h2c->dft = hdr.ft;
2075 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002076 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002077 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002078 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002079 }
2080
2081 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002082 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2083
Olivier Houchard638b7992018-08-16 15:41:52 +02002084 if (tmp_h2s != h2s && h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002085 /* we may have to signal the upper layers */
2086 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002087 if (h2s->recv_wait) {
2088 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
2089 tasklet_wakeup(h2s->recv_wait->task);
2090 h2s->recv_wait = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002091 }
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002092 }
2093 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002094
Willy Tarreaud7901432017-12-29 11:34:40 +01002095 if (h2c->st0 == H2_CS_FRAME_E)
2096 goto strm_err;
2097
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002098 if (h2s->st == H2_SS_IDLE &&
2099 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2100 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2101 * this state MUST be treated as a connection error
2102 */
2103 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2104 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002105 if (!h2c->nb_streams) {
2106 /* only log if no other stream can report the error */
2107 sess_log(h2c->conn->owner);
2108 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002109 break;
2110 }
2111
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002112 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2113 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2114 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
2115 * this state MUST be treated as a stream error
2116 */
2117 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2118 goto strm_err;
2119 }
2120
Willy Tarreauab837502017-12-27 15:07:30 +01002121 /* Below the management of frames received in closed state is a
2122 * bit hackish because the spec makes strong differences between
2123 * streams closed by receiving RST, sending RST, and seeing ES
2124 * in both directions. In addition to this, the creation of a
2125 * new stream reusing the identifier of a closed one will be
2126 * detected here. Given that we cannot keep track of all closed
2127 * streams forever, we consider that unknown closed streams were
2128 * closed on RST received, which allows us to respond with an
2129 * RST without breaking the connection (eg: to abort a transfer).
2130 * Some frames have to be silently ignored as well.
2131 */
2132 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
2133 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
2134 /* #5.1.1: The identifier of a newly
2135 * established stream MUST be numerically
2136 * greater than all streams that the initiating
2137 * endpoint has opened or reserved. This
2138 * governs streams that are opened using a
2139 * HEADERS frame and streams that are reserved
2140 * using PUSH_PROMISE. An endpoint that
2141 * receives an unexpected stream identifier
2142 * MUST respond with a connection error.
2143 */
2144 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2145 goto strm_err;
2146 }
2147
2148 if (h2s->flags & H2_SF_RST_RCVD) {
2149 /* RFC7540#5.1:closed: an endpoint that
2150 * receives any frame other than PRIORITY after
2151 * receiving a RST_STREAM MUST treat that as a
2152 * stream error of type STREAM_CLOSED.
2153 *
2154 * Note that old streams fall into this category
2155 * and will lead to an RST being sent.
2156 */
2157 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2158 h2c->st0 = H2_CS_FRAME_E;
2159 goto strm_err;
2160 }
2161
2162 /* RFC7540#5.1:closed: if this state is reached as a
2163 * result of sending a RST_STREAM frame, the peer that
2164 * receives the RST_STREAM might have already sent
2165 * frames on the stream that cannot be withdrawn. An
2166 * endpoint MUST ignore frames that it receives on
2167 * closed streams after it has sent a RST_STREAM
2168 * frame. An endpoint MAY choose to limit the period
2169 * over which it ignores frames and treat frames that
2170 * arrive after this time as being in error.
2171 */
2172 if (!(h2s->flags & H2_SF_RST_SENT)) {
2173 /* RFC7540#5.1:closed: any frame other than
2174 * PRIO/WU/RST in this state MUST be treated as
2175 * a connection error
2176 */
2177 if (h2c->dft != H2_FT_RST_STREAM &&
2178 h2c->dft != H2_FT_PRIORITY &&
2179 h2c->dft != H2_FT_WINDOW_UPDATE) {
2180 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2181 goto strm_err;
2182 }
2183 }
2184 }
2185
Willy Tarreauc0da1962017-10-30 18:38:00 +01002186#if 0
2187 // problem below: it is not possible to completely ignore such
2188 // streams as we need to maintain the compression state as well
2189 // and for this we need to completely process these frames (eg:
2190 // HEADERS frames) as well as counting DATA frames to emit
2191 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2192 // This is a typical case of layer violation where the
2193 // transported contents are critical to the connection's
2194 // validity and must be ignored at the same time :-(
2195
2196 /* graceful shutdown, ignore streams whose ID is higher than
2197 * the one advertised in GOAWAY. RFC7540#6.8.
2198 */
2199 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002200 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2201 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002202 h2c->dfl -= ret;
2203 ret = h2c->dfl == 0;
2204 goto strm_err;
2205 }
2206#endif
2207
Willy Tarreau7e98c052017-10-10 15:56:59 +02002208 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002209 case H2_FT_SETTINGS:
2210 if (h2c->st0 == H2_CS_FRAME_P)
2211 ret = h2c_handle_settings(h2c);
2212
2213 if (h2c->st0 == H2_CS_FRAME_A)
2214 ret = h2c_ack_settings(h2c);
2215 break;
2216
Willy Tarreaucf68c782017-10-10 17:11:41 +02002217 case H2_FT_PING:
2218 if (h2c->st0 == H2_CS_FRAME_P)
2219 ret = h2c_handle_ping(h2c);
2220
2221 if (h2c->st0 == H2_CS_FRAME_A)
2222 ret = h2c_ack_ping(h2c);
2223 break;
2224
Willy Tarreau26f95952017-07-27 17:18:30 +02002225 case H2_FT_WINDOW_UPDATE:
2226 if (h2c->st0 == H2_CS_FRAME_P)
2227 ret = h2c_handle_window_update(h2c, h2s);
2228 break;
2229
Willy Tarreau61290ec2017-10-17 08:19:21 +02002230 case H2_FT_CONTINUATION:
2231 /* we currently don't support CONTINUATION frames since
2232 * we have nowhere to store the partial HEADERS frame.
2233 * Let's abort the stream on an INTERNAL_ERROR here.
2234 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002235 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02002236 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002237 h2c->st0 = H2_CS_FRAME_E;
2238 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02002239 break;
2240
Willy Tarreau13278b42017-10-13 19:23:14 +02002241 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002242 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002243 if (h2c->flags & H2_CF_IS_BACK)
2244 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2245 else
2246 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002247 if (tmp_h2s) {
2248 h2s = tmp_h2s;
2249 ret = 1;
2250 }
2251 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002252 break;
2253
Willy Tarreau454f9052017-10-26 19:40:35 +02002254 case H2_FT_DATA:
2255 if (h2c->st0 == H2_CS_FRAME_P)
2256 ret = h2c_frt_handle_data(h2c, h2s);
2257
2258 if (h2c->st0 == H2_CS_FRAME_A)
2259 ret = h2c_send_strm_wu(h2c);
2260 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002261
Willy Tarreau92153fc2017-12-03 19:46:19 +01002262 case H2_FT_PRIORITY:
2263 if (h2c->st0 == H2_CS_FRAME_P)
2264 ret = h2c_handle_priority(h2c);
2265 break;
2266
Willy Tarreaucd234e92017-08-18 10:59:39 +02002267 case H2_FT_RST_STREAM:
2268 if (h2c->st0 == H2_CS_FRAME_P)
2269 ret = h2c_handle_rst_stream(h2c, h2s);
2270 break;
2271
Willy Tarreaue96b0922017-10-30 00:28:29 +01002272 case H2_FT_GOAWAY:
2273 if (h2c->st0 == H2_CS_FRAME_P)
2274 ret = h2c_handle_goaway(h2c);
2275 break;
2276
Willy Tarreau1c661982017-10-30 13:52:01 +01002277 case H2_FT_PUSH_PROMISE:
2278 /* not permitted here, RFC7540#5.1 */
2279 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002280 if (!h2c->nb_streams) {
2281 /* only log if no other stream can report the error */
2282 sess_log(h2c->conn->owner);
2283 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002284 break;
2285
2286 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002287 default:
2288 /* drop frames that we ignore. They may be larger than
2289 * the buffer so we drain all of their contents until
2290 * we reach the end.
2291 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002292 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2293 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002294 h2c->dfl -= ret;
2295 ret = h2c->dfl == 0;
2296 }
2297
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002298 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002299 /* We may have to send an RST if not done yet */
2300 if (h2s->st == H2_SS_ERROR)
2301 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002302
Willy Tarreaua20a5192017-12-27 11:02:06 +01002303 if (h2c->st0 == H2_CS_FRAME_E)
2304 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002305
Willy Tarreau7e98c052017-10-10 15:56:59 +02002306 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002307 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002308 break;
2309
2310 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002311 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002312 h2c->st0 = H2_CS_FRAME_H;
2313 }
2314 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002315
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002316 if (h2c->rcvd_c > 0 &&
2317 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2318 h2c_send_conn_wu(h2c);
2319
Willy Tarreau52eed752017-09-22 15:05:09 +02002320 fail:
2321 /* we can go here on missing data, blocked response or error */
Olivier Houchard638b7992018-08-16 15:41:52 +02002322 if (h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002323 /* we may have to signal the upper layers */
2324 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002325 if (h2s->recv_wait) {
2326 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
2327 tasklet_wakeup(h2s->recv_wait->task);
2328 h2s->recv_wait = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002329 }
2330 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002331
2332 if (h2_recv_allowed(h2c))
2333 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreaubc933932017-10-09 16:21:43 +02002334}
2335
2336/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2337 * the end.
2338 */
2339static int h2_process_mux(struct h2c *h2c)
2340{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002341 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002342
Willy Tarreau01b44822018-10-03 14:26:37 +02002343 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2344 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2345 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2346 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2347 if (h2c->st0 == H2_CS_ERROR) {
2348 h2c->st0 = H2_CS_ERROR2;
2349 sess_log(h2c->conn->owner);
2350 }
2351 goto fail;
2352 }
2353 h2c->st0 = H2_CS_SETTINGS1;
2354 }
2355 /* need to wait for the other side */
2356 if (h2c->st0 == H2_CS_SETTINGS1)
2357 return 1;
2358 }
2359
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002360 /* start by sending possibly pending window updates */
2361 if (h2c->rcvd_c > 0 &&
2362 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2363 h2c_send_conn_wu(h2c) < 0)
2364 goto fail;
2365
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002366 /* First we always process the flow control list because the streams
2367 * waiting there were already elected for immediate emission but were
2368 * blocked just on this.
2369 */
2370
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002371 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002372 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2373 h2c->st0 >= H2_CS_ERROR)
2374 break;
2375
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002376 h2s->flags &= ~H2_SF_BLK_ANY;
2377 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002378 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002379 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002380 LIST_DEL(&h2s->list);
2381 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002382 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002383 }
2384
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002385 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002386 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2387 break;
2388
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002389 h2s->flags &= ~H2_SF_BLK_ANY;
2390 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002391 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002392 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002393 LIST_DEL(&h2s->list);
2394 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002395 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002396 }
2397
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002398 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002399 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002400 if (h2c->st0 == H2_CS_ERROR) {
2401 if (h2c->max_id >= 0) {
2402 h2c_send_goaway_error(h2c, NULL);
2403 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2404 return 0;
2405 }
2406
2407 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2408 }
2409 return 1;
2410 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002411 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002412}
2413
Willy Tarreau62f52692017-10-08 23:01:42 +02002414
Willy Tarreau479998a2018-11-18 06:30:59 +01002415/* Attempt to read data, and subscribe if none available.
2416 * The function returns 1 if data has been received, otherwise zero.
2417 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002418static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002419{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002420 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002421 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002422 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002423 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002424
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002425 if (h2c->wait_event.wait_reason & SUB_CAN_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002426 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002427
Willy Tarreau315d8072017-12-10 22:17:57 +01002428 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002429 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002430
Willy Tarreau44e973f2018-03-01 17:49:30 +01002431 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002432 if (!buf) {
2433 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002434 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002435 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002436
Olivier Houchard7505f942018-08-21 18:10:44 +02002437 do {
2438 max = buf->size - b_data(buf);
2439 if (max)
2440 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2441 else
2442 ret = 0;
2443 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002444
Olivier Houchard53216e72018-10-10 15:46:36 +02002445 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002446 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002447
Olivier Houcharda1411e62018-08-17 18:42:48 +02002448 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002449 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002450 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002451 }
2452
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002453 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002454 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002455 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002456}
2457
Willy Tarreau479998a2018-11-18 06:30:59 +01002458/* Try to send data if possible.
2459 * The function returns 1 if data have been sent, otherwise zero.
2460 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002461static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002462{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002463 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002464 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002465 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002466
2467 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002468 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002469
Olivier Houchard7505f942018-08-21 18:10:44 +02002470
Willy Tarreaua2af5122017-10-09 11:56:46 +02002471 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2472 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002473 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002474 }
2475
Willy Tarreaubc933932017-10-09 16:21:43 +02002476 /* This loop is quite simple : it tries to fill as much as it can from
2477 * pending streams into the existing buffer until it's reportedly full
2478 * or the end of send requests is reached. Then it tries to send this
2479 * buffer's contents out, marks it not full if at least one byte could
2480 * be sent, and tries again.
2481 *
2482 * The snd_buf() function normally takes a "flags" argument which may
2483 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2484 * data immediately comes and CO_SFL_STREAMER to indicate that the
2485 * connection is streaming lots of data (used to increase TLS record
2486 * size at the expense of latency). The former can be sent any time
2487 * there's a buffer full flag, as it indicates at least one stream
2488 * attempted to send and failed so there are pending data. An
2489 * alternative would be to set it as long as there's an active stream
2490 * but that would be problematic for ACKs until we have an absolute
2491 * guarantee that all waiters have at least one byte to send. The
2492 * latter should possibly not be set for now.
2493 */
2494
2495 done = 0;
2496 while (!done) {
2497 unsigned int flags = 0;
2498
2499 /* fill as much as we can into the current buffer */
2500 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2501 done = h2_process_mux(h2c);
2502
2503 if (conn->flags & CO_FL_ERROR)
2504 break;
2505
2506 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2507 flags |= CO_SFL_MSG_MORE;
2508
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002509 if (b_data(&h2c->mbuf)) {
2510 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002511 if (!ret)
2512 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002513 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002514 b_del(&h2c->mbuf, ret);
2515 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002516 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002517
2518 /* wrote at least one byte, the buffer is not full anymore */
2519 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2520 }
2521
Willy Tarreaua2af5122017-10-09 11:56:46 +02002522 if (conn->flags & CO_FL_SOCK_WR_SH) {
2523 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002524 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002525 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002526 /* We're not full anymore, so we can wake any task that are waiting
2527 * for us.
2528 */
2529 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002530 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002531 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2532 struct h2s *, list);
2533 LIST_DEL(&h2s->list);
2534 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002535 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002536 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002537 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002538 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002539 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002540 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002541 /* We're done, no more to send */
2542 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002543 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002544schedule:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002545 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
2546 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002547 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002548}
2549
2550static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2551{
2552 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002553 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002554
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002555 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002556 ret = h2_send(h2c);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002557 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002558 ret |= h2_recv(h2c);
2559 if (ret)
2560 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002561 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002562}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002563
Willy Tarreau62f52692017-10-08 23:01:42 +02002564/* callback called on any event by the connection handler.
2565 * It applies changes and returns zero, or < 0 if it wants immediate
2566 * destruction of the connection (which normally doesn not happen in h2).
2567 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002568static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002569{
Olivier Houchard7505f942018-08-21 18:10:44 +02002570 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002571
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002572 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002573 h2_process_demux(h2c);
2574
2575 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002576 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002577
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002578 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002579 h2c->flags &= ~H2_CF_DEM_DFULL;
2580 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002581 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002582
Willy Tarreau0b37d652018-10-03 10:33:02 +02002583 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002584 /* frontend is stopping, reload likely in progress, let's try
2585 * to announce a graceful shutdown if not yet done. We don't
2586 * care if it fails, it will be tried again later.
2587 */
2588 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2589 if (h2c->last_sid < 0)
2590 h2c->last_sid = (1U << 31) - 1;
2591 h2c_send_goaway_error(h2c, NULL);
2592 }
2593 }
2594
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002595 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002596 * If we received early data, and the handshake is done, wake
2597 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002598 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002599 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2600 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2601 struct eb32_node *node;
2602 struct h2s *h2s;
2603
2604 h2c->flags |= H2_CF_WAIT_FOR_HS;
2605 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2606
2607 while (node) {
2608 h2s = container_of(node, struct h2s, by_id);
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002609 if ((h2s->cs->flags & CS_FL_WAIT_FOR_HS) &&
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002610 h2s->recv_wait) {
2611 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002612 sw->wait_reason &= ~SUB_CAN_RECV;
2613 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002614 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002615 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002616 node = eb32_next(node);
2617 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002618 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002619
Willy Tarreau26bd7612017-10-09 16:47:04 +02002620 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002621 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2622 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2623 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002624 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002625
2626 if (eb_is_empty(&h2c->streams_by_id)) {
2627 /* no more stream, kill the connection now */
2628 h2_release(conn);
2629 return -1;
2630 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002631 }
2632
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002633 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002634 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002635
Olivier Houchard53216e72018-10-10 15:46:36 +02002636 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2637 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2638 (h2c->st0 != H2_CS_ERROR &&
2639 !b_data(&h2c->mbuf) &&
2640 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2641 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002642 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002643
Willy Tarreau3f133572017-10-31 19:21:06 +01002644 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002645 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002646 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002647 task_queue(h2c->task);
2648 }
2649 else
2650 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002651 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002652
Olivier Houchard7505f942018-08-21 18:10:44 +02002653 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002654 return 0;
2655}
2656
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002657static int h2_wake(struct connection *conn)
2658{
2659 struct h2c *h2c = conn->mux_ctx;
2660
2661 return (h2_process(h2c));
2662}
2663
Willy Tarreauea392822017-10-31 10:02:25 +01002664/* Connection timeout management. The principle is that if there's no receipt
2665 * nor sending for a certain amount of time, the connection is closed. If the
2666 * MUX buffer still has lying data or is not allocatable, the connection is
2667 * immediately killed. If it's allocatable and empty, we attempt to send a
2668 * GOAWAY frame.
2669 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002670static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002671{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002672 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002673 int expired = tick_is_expired(t->expire, now_ms);
2674
Willy Tarreau0975f112018-03-29 15:22:59 +02002675 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002676 return t;
2677
Willy Tarreau0975f112018-03-29 15:22:59 +02002678 task_delete(t);
2679 task_free(t);
2680
2681 if (!h2c) {
2682 /* resources were already deleted */
2683 return NULL;
2684 }
2685
2686 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002687 h2c_error(h2c, H2_ERR_NO_ERROR);
2688 h2_wake_some_streams(h2c, 0, 0);
2689
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002690 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002691 /* don't even try to send a GOAWAY, the buffer is stuck */
2692 h2c->flags |= H2_CF_GOAWAY_FAILED;
2693 }
2694
2695 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002696 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002697 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2698 h2c->flags |= H2_CF_GOAWAY_FAILED;
2699
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002700 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2701 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002702 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002703 b_del(&h2c->mbuf, ret);
2704 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002705 }
2706 }
Willy Tarreauea392822017-10-31 10:02:25 +01002707
Willy Tarreau0975f112018-03-29 15:22:59 +02002708 /* either we can release everything now or it will be done later once
2709 * the last stream closes.
2710 */
2711 if (eb_is_empty(&h2c->streams_by_id))
2712 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002713
Willy Tarreauea392822017-10-31 10:02:25 +01002714 return NULL;
2715}
2716
2717
Willy Tarreau62f52692017-10-08 23:01:42 +02002718/*******************************************/
2719/* functions below are used by the streams */
2720/*******************************************/
2721
2722/*
2723 * Attach a new stream to a connection
2724 * (Used for outgoing connections)
2725 */
2726static struct conn_stream *h2_attach(struct connection *conn)
2727{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002728 struct conn_stream *cs;
2729 struct h2s *h2s;
2730 struct h2c *h2c = conn->mux_ctx;
2731
2732 cs = cs_new(conn);
2733 if (!cs)
2734 return NULL;
2735 h2s = h2c_bck_stream_new(h2c, cs);
2736 if (!h2s) {
2737 cs_free(cs);
2738 return NULL;
2739 }
2740 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002741}
2742
Willy Tarreaufafd3982018-11-18 21:29:20 +01002743/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2744 * We have to scan because we may have some orphan streams. It might be
2745 * beneficial to scan backwards from the end to reduce the likeliness to find
2746 * orphans.
2747 */
2748static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2749{
2750 struct h2c *h2c = conn->mux_ctx;
2751 struct h2s *h2s;
2752 struct eb32_node *node;
2753
2754 node = eb32_first(&h2c->streams_by_id);
2755 while (node) {
2756 h2s = container_of(node, struct h2s, by_id);
2757 if (h2s->cs)
2758 return h2s->cs;
2759 node = eb32_next(node);
2760 }
2761 return NULL;
2762}
2763
Willy Tarreau62f52692017-10-08 23:01:42 +02002764/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002765 * Destroy the mux and the associated connection, if it is no longer used
2766 */
2767static void h2_destroy(struct connection *conn)
2768{
2769 struct h2c *h2c = conn->mux_ctx;
2770
2771 if (eb_is_empty(&h2c->streams_by_id))
2772 h2_release(h2c->conn);
2773}
2774
2775/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002776 * Detach the stream from the connection and possibly release the connection.
2777 */
2778static void h2_detach(struct conn_stream *cs)
2779{
Willy Tarreau60935142017-10-16 18:11:19 +02002780 struct h2s *h2s = cs->ctx;
2781 struct h2c *h2c;
2782
2783 cs->ctx = NULL;
2784 if (!h2s)
2785 return;
2786
2787 h2c = h2s->h2c;
2788 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002789 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002790 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2791 !h2_has_too_many_cs(h2c)) {
2792 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard53216e72018-10-10 15:46:36 +02002793 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002794 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreauf2101912018-07-19 10:11:38 +02002795 }
Willy Tarreau60935142017-10-16 18:11:19 +02002796
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002797 /* this stream may be blocked waiting for some data to leave (possibly
2798 * an ES or RST frame), so orphan it in this case.
2799 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002800 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002801 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002802 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002803 return;
2804
Willy Tarreau45f752e2017-10-30 15:44:59 +01002805 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2806 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2807 /* unblock the connection if it was blocked on this
2808 * stream.
2809 */
2810 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2811 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002812 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002813 }
2814
Willy Tarreau71049cc2018-03-28 13:56:39 +02002815 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002816
Willy Tarreaue323f342018-03-28 13:51:45 +02002817 /* We don't want to close right now unless we're removing the
2818 * last stream, and either the connection is in error, or it
2819 * reached the ID already specified in a GOAWAY frame received
2820 * or sent (as seen by last_sid >= 0).
2821 */
2822 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2823 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002824 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard52b94662018-10-21 03:01:20 +02002825 (h2c->flags & (H2_CF_GOAWAY_FAILED | H2_CF_GOAWAY_SENT)) ||
Olivier Houchard93c88522018-11-30 15:39:16 +01002826 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002827 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002828 (conn_xprt_read0_pending(h2c->conn) ||
2829 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2830 /* no more stream will come, kill it now */
2831 h2_release(h2c->conn);
2832 }
2833 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002834 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002835 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2836 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002837 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002838 else
2839 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002840 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002841}
2842
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002843static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002844{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002845 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002846 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002847
Willy Tarreau721c9742017-11-07 11:05:42 +01002848 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002849 return;
2850
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002851 /* if no outgoing data was seen on this stream, it means it was
2852 * closed with a "tcp-request content" rule that is normally
2853 * used to kill the connection ASAP (eg: limit abuse). In this
2854 * case we send a goaway to close the connection.
2855 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002856 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002857 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002858 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002859
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002860 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2861 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002862 h2c_send_goaway_error(h2c, h2s) <= 0)
2863 return;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002864
Olivier Houchard435ce2d2018-12-03 18:43:16 +01002865 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
2866 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01002867 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002868
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002869 return;
2870add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002871 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002872 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002873 if (h2s->flags & H2_SF_BLK_MFCTL) {
2874 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2875 h2s->send_wait = sw;
2876 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2877 h2s->send_wait = sw;
2878 LIST_ADDQ(&h2c->send_list, &h2s->list);
2879 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002880 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002881 /* Let the handler know we want shutr */
2882 sw->handle = (void *)((long)sw->handle | 1);
2883
Willy Tarreau62f52692017-10-08 23:01:42 +02002884}
2885
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002886static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002887{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002888 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002889 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002890
Willy Tarreau721c9742017-11-07 11:05:42 +01002891 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002892 return;
2893
Willy Tarreau67434202017-11-06 20:20:51 +01002894 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002895 /* we can cleanly close using an empty data frame only after headers */
2896
2897 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2898 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002899 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002900
2901 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002902 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002903 else
2904 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002905 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002906 /* if no outgoing data was seen on this stream, it means it was
2907 * closed with a "tcp-request content" rule that is normally
2908 * used to kill the connection ASAP (eg: limit abuse). In this
2909 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002910 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002911 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002912 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002913 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002914
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002915 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2916 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002917 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002918 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002919
Willy Tarreau00dd0782018-03-01 16:31:34 +01002920 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002921 }
2922
Olivier Houchard435ce2d2018-12-03 18:43:16 +01002923 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
2924 tasklet_wakeup(h2c->wait_event.task);
2925 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002926
2927 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002928 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002929 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002930 if (h2s->flags & H2_SF_BLK_MFCTL) {
2931 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2932 h2s->send_wait = sw;
2933 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2934 h2s->send_wait = sw;
2935 LIST_ADDQ(&h2c->send_list, &h2s->list);
2936 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002937 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002938 /* let the handler know we want to shutw */
2939 sw->handle = (void *)((long)(sw->handle) | 2);
2940
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002941}
2942
2943static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
2944{
2945 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002946 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002947
2948 if (reason & 1)
2949 h2_do_shutr(h2s);
2950 if (reason & 2)
2951 h2_do_shutw(h2s);
2952
2953 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02002954}
2955
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002956static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2957{
2958 struct h2s *h2s = cs->ctx;
2959
2960 if (!mode)
2961 return;
2962
2963 h2_do_shutr(h2s);
2964}
2965
2966static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2967{
2968 struct h2s *h2s = cs->ctx;
2969
2970 h2_do_shutw(h2s);
2971}
2972
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01002973/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreauc3e18f32018-10-08 14:51:56 +02002974 * HTX request or response depending on the connection's side. Returns the
2975 * number of bytes emitted if > 0, or 0 if it couldn't proceed. Stream errors
2976 * are reported in h2s->errcode and connection errors in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002977 */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02002978static int h2s_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002979{
2980 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002981 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02002982 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002983 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02002984 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002985 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01002986 struct buffer *csbuf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01002987 struct htx *htx = NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02002988 int flen = h2c->dfl;
2989 int outlen = 0;
2990 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01002991 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02002992
2993 if (!h2c->dfl) {
2994 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002995 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002996 return 0;
2997 }
2998
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002999 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01003000 return 0; // incomplete input frame
3001
Willy Tarreau13278b42017-10-13 19:23:14 +02003002 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003003 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003004 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003005 copy = alloc_trash_chunk();
3006 if (!copy) {
3007 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3008 goto fail;
3009 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003010 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3011 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3012 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003013 }
3014
3015 /* The padlen is the first byte before data, and the padding appears
3016 * after data. padlen+data+padding are included in flen.
3017 */
3018 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003019 h2c->dpl = *hdrs;
3020 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02003021 /* RFC7540#6.2 : pad length = length of frame payload or greater */
3022 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003023 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02003024 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003025 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02003026 hdrs += 1; // skip Pad Length
3027 }
3028
3029 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3030 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003031 if (read_n32(hdrs) == h2s->id) {
3032 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3033 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003034 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003035 }
3036
Willy Tarreau13278b42017-10-13 19:23:14 +02003037 hdrs += 5; // stream dep = 4, weight = 1
3038 flen -= 5;
3039 }
3040
3041 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
3042 * don't support this for now and can't even decompress so we have to
3043 * break the connection.
3044 */
3045 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
3046 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003047 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02003048 }
3049
Olivier Houchard638b7992018-08-16 15:41:52 +02003050 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01003051 if (!csbuf) {
3052 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003053 goto fail;
3054 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003055
Willy Tarreau937f7602018-02-26 15:22:17 +01003056 /* we can't retry a failed decompression operation so we must be very
3057 * careful not to take any risks. In practice the output buffer is
3058 * always empty except maybe for trailers, in which case we simply have
3059 * to wait for the upper layer to finish consuming what is available.
3060 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003061
3062 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
3063 htx = htx_from_buf(&h2s->rxbuf);
3064 if (!htx_is_empty(htx))
3065 goto fail;
3066 } else {
3067 if (b_data(csbuf))
3068 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003069
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003070 csbuf->head = 0;
3071 try = b_size(csbuf);
3072 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003073
3074 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3075 sizeof(list)/sizeof(list[0]), tmp);
3076 if (outlen < 0) {
3077 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3078 goto fail;
3079 }
3080
3081 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02003082 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003083
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003084 if (htx) {
3085 /* HTX mode */
3086 if (h2c->flags & H2_CF_IS_BACK)
3087 outlen = h2_make_htx_response(list, htx, &msgf);
3088 else
3089 outlen = h2_make_htx_request(list, htx, &msgf);
3090 } else {
3091 /* HTTP/1 mode */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003092 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003093 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003094
3095 if (outlen < 0) {
3096 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3097 goto fail;
3098 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003099
Willy Tarreau174b06a2018-04-25 18:13:58 +02003100 if (msgf & H2_MSGF_BODY) {
3101 /* a payload is present */
3102 if (msgf & H2_MSGF_BODY_CL)
3103 h2s->flags |= H2_SF_DATA_CLEN;
3104 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
3105 h2s->flags |= H2_SF_DATA_CHNK;
3106 }
3107
Willy Tarreau13278b42017-10-13 19:23:14 +02003108 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003109 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02003110 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01003111 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02003112
Willy Tarreau39d68502018-03-02 12:26:37 +01003113 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02003114 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003115 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003116 if (htx)
3117 htx_add_endof(htx, HTX_BLK_EOM);
Willy Tarreau39d68502018-03-02 12:26:37 +01003118 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003119
Willy Tarreau68dd9852017-07-03 14:44:26 +02003120 leave:
3121 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02003122 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003123 fail:
3124 outlen = 0;
3125 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02003126}
3127
Willy Tarreau454f9052017-10-26 19:40:35 +02003128/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3129 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3130 * in use, a new chunk is emitted for each frame. This is supposed to fit
3131 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3132 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3133 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003134 * parser state is automatically updated. Returns > 0 if it could completely
3135 * send the current frame, 0 if it couldn't complete, in which case
3136 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3137 * DATA frame can return 0 as a valid result). Stream errors are reported in
3138 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3139 * have checked the frame header and ensured that the frame was complete or the
3140 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003141 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003142static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003143{
3144 struct h2c *h2c = h2s->h2c;
3145 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003146 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003147 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003148 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003149 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003150
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003151 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003152
3153 /* The padlen is the first byte before data, and the padding appears
3154 * after data. padlen+data+padding are included in flen.
3155 */
Willy Tarreau79127812017-12-03 21:06:59 +01003156 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003157 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003158 return 0;
3159
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003160 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003161 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003162 /* RFC7540#6.1 : pad length = length of frame payload or greater */
3163 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02003164 return 0;
3165 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003166
3167 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003168 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003169 h2c->dfl--;
3170 h2c->rcvd_c++; h2c->rcvd_s++;
3171 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02003172 }
3173
Olivier Houchard638b7992018-08-16 15:41:52 +02003174 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003175 if (!csbuf) {
3176 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003177 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003178 }
3179
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003180try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003181 flen = h2c->dfl - h2c->dpl;
3182 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003183 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003184
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003185 if (flen > b_data(&h2c->dbuf)) {
3186 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003187 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003188 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003189 }
3190
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003191 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
3192 htx = htx_from_buf(csbuf);
3193 block1 = htx_free_data_space(htx);
3194 if (!block1) {
3195 h2c->flags |= H2_CF_DEM_SFULL;
3196 goto fail;
3197 }
3198 if (flen > block1)
3199 flen = block1;
3200
3201 /* here, flen is the max we can copy into the output buffer */
3202 block1 = b_contig_data(&h2c->dbuf, 0);
3203 if (flen > block1)
3204 flen = block1;
3205
3206 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3207 h2c->flags |= H2_CF_DEM_SFULL;
3208 goto fail;
3209 }
3210
3211 b_del(&h2c->dbuf, flen);
3212 h2c->dfl -= flen;
3213 h2c->rcvd_c += flen;
3214 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3215 goto try_again;
3216 }
3217 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003218 /* it doesn't fit and the buffer is fragmented,
3219 * so let's defragment it and try again.
3220 */
3221 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003222 }
3223
Willy Tarreaueba10f22018-04-25 20:44:22 +02003224 /* chunked-encoding requires more room */
3225 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003226 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003227 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3228 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3229 (chklen < 1048576) ? 4 : 8;
3230 chklen += 4; // CRLF, CRLF
3231 }
3232
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003233 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003234 if (flen + chklen > b_room(csbuf)) {
3235 if (chklen >= b_room(csbuf)) {
3236 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003237 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003238 }
3239 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003240 }
3241
3242 if (h2s->flags & H2_SF_DATA_CHNK) {
3243 /* emit the chunk size */
3244 unsigned int chksz = flen;
3245 char str[10];
3246 char *beg;
3247
3248 beg = str + sizeof(str);
3249 *--beg = '\n';
3250 *--beg = '\r';
3251 do {
3252 *--beg = hextab[chksz & 0xF];
3253 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003254 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003255 }
3256
Willy Tarreau454f9052017-10-26 19:40:35 +02003257 /* Block1 is the length of the first block before the buffer wraps,
3258 * block2 is the optional second block to reach the end of the frame.
3259 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003260 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003261 if (block1 > flen)
3262 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003263 block2 = flen - block1;
3264
3265 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003266 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003267
3268 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003269 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003270
Willy Tarreaueba10f22018-04-25 20:44:22 +02003271 if (h2s->flags & H2_SF_DATA_CHNK) {
3272 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003273 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003274 }
3275
Willy Tarreau454f9052017-10-26 19:40:35 +02003276 /* now mark the input data as consumed (will be deleted from the buffer
3277 * by the caller when seeing FRAME_A after sending the window update).
3278 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003279 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003280 h2c->dfl -= flen;
3281 h2c->rcvd_c += flen;
3282 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3283
3284 if (h2c->dfl > h2c->dpl) {
3285 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003286 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003287 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003288 }
3289
Willy Tarreau4a28da12018-01-04 14:41:00 +01003290 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003291 /* here we're done with the frame, all the payload (except padding) was
3292 * transferred.
3293 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003294
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003295 if (h2c->dff & H2_F_DATA_END_STREAM) {
3296 if (htx) {
3297 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3298 h2c->flags |= H2_CF_DEM_SFULL;
3299 goto fail;
3300 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003301 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003302 else if (h2s->flags & H2_SF_DATA_CHNK) {
3303 /* emit the trailing 0 CRLF CRLF */
3304 if (b_room(csbuf) < 5) {
3305 h2c->flags |= H2_CF_DEM_SFULL;
3306 goto fail;
3307 }
3308 chklen += 5;
3309 b_putblk(csbuf, "0\r\n\r\n", 5);
3310 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003311 }
3312
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003313 h2c->rcvd_c += h2c->dpl;
3314 h2c->rcvd_s += h2c->dpl;
3315 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003316 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3317
Willy Tarreau39d68502018-03-02 12:26:37 +01003318 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003319 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003320 h2s->cs->flags |= CS_FL_REOS;
3321 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003322
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003323 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003324 fail:
3325 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003326}
3327
Willy Tarreau5dd17352018-06-14 13:33:30 +02003328/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3329 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3330 * number of bytes sent. The caller must check the stream's status to detect
3331 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003332 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003333static 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 +02003334{
3335 struct http_hdr list[MAX_HTTP_HDR];
3336 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003337 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003338 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003339 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003340 int es_now = 0;
3341 int ret = 0;
3342 int hdr;
3343
3344 if (h2c_mux_busy(h2c, h2s)) {
3345 h2s->flags |= H2_SF_BLK_MBUSY;
3346 return 0;
3347 }
3348
Willy Tarreau44e973f2018-03-01 17:49:30 +01003349 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003350 h2c->flags |= H2_CF_MUX_MALLOC;
3351 h2s->flags |= H2_SF_BLK_MROOM;
3352 return 0;
3353 }
3354
3355 /* First, try to parse the H1 response and index it into <list>.
3356 * NOTE! Since it comes from haproxy, we *know* that a response header
3357 * block does not wrap and we can safely read it this way without
3358 * having to realign the buffer.
3359 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003360 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003361 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003362 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003363 /* incomplete or invalid response, this is abnormal coming from
3364 * haproxy and may only result in a bad errorfile or bad Lua code
3365 * so that won't be fixed, raise an error now.
3366 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003367 * FIXME: we should instead add the ability to only return a
3368 * 502 bad gateway. But in theory this is not supposed to
3369 * happen.
3370 */
3371 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3372 ret = 0;
3373 goto end;
3374 }
3375
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003376 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003377
3378 /* certain statuses have no body or an empty one, regardless of
3379 * what the headers say.
3380 */
3381 if (sl.st.status >= 100 && sl.st.status < 200) {
3382 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3383 h1m->curr_len = h1m->body_len = 0;
3384 }
3385 else if (sl.st.status == 204 || sl.st.status == 304) {
3386 /* no contents, claim c-len is present and set to zero */
3387 h1m->flags &= ~H1_MF_CHNK;
3388 h1m->flags |= H1_MF_CLEN;
3389 h1m->curr_len = h1m->body_len = 0;
3390 }
3391
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003392 chunk_reset(&outbuf);
3393
3394 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003395 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003396 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003397 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003398
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003399 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003400 break;
3401 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003402 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003403 }
3404
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003405 if (outbuf.size < 9)
3406 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003407
3408 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003409 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3410 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3411 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003412
3413 /* encode status, which necessarily is the first one */
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003414 if (outbuf.data < outbuf.size && h2s->status == 200)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003415 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003416 else if (outbuf.data < outbuf.size && h2s->status == 304)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003417 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003418 else if (unlikely(list[0].v.len != 3)) {
3419 /* this is an unparsable response */
3420 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3421 ret = 0;
3422 goto end;
3423 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003424 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003425 /* basic encoding of the status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003426 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3427 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3428 outbuf.area[outbuf.data++] = list[0].v.ptr[0];
3429 outbuf.area[outbuf.data++] = list[0].v.ptr[1];
3430 outbuf.area[outbuf.data++] = list[0].v.ptr[2];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003431 }
3432 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003433 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003434 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003435 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003436 }
3437
3438 /* encode all headers, stop at empty name */
3439 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003440 /* these ones do not exist in H2 and must be dropped. */
3441 if (isteq(list[hdr].n, ist("connection")) ||
3442 isteq(list[hdr].n, ist("proxy-connection")) ||
3443 isteq(list[hdr].n, ist("keep-alive")) ||
3444 isteq(list[hdr].n, ist("upgrade")) ||
3445 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003446 continue;
3447
3448 if (isteq(list[hdr].n, ist("")))
3449 break; // end
3450
3451 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3452 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003453 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003454 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003455 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003456 }
3457 }
3458
3459 /* we may need to add END_STREAM */
3460 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3461 es_now = 1;
3462
3463 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003464 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003465
3466 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003467 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003468
3469 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003470 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003471
3472 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003473 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003474 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003475
3476 /* for now we don't implemented CONTINUATION, so we wait for a
3477 * body or directly end in TRL2.
3478 */
3479 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003480 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003481 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003482
Willy Tarreau801250e2018-09-11 11:45:04 +02003483 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003484 h2s->flags |= H2_SF_ES_SENT;
3485 if (h2s->st == H2_SS_OPEN)
3486 h2s->st = H2_SS_HLOC;
3487 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003488 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003489 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003490 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003491 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003492 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003493 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003494 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003495 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003496 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003497
3498 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003499
3500 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003501 //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 +02003502 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003503 full:
3504 h1m_init_res(h1m);
3505 h1m->err_pos = -1; // don't care about errors on the response path
3506 h2c->flags |= H2_CF_MUX_MFULL;
3507 h2s->flags |= H2_SF_BLK_MROOM;
3508 ret = 0;
3509 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003510}
3511
Willy Tarreau5dd17352018-06-14 13:33:30 +02003512/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3513 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3514 * the number of bytes sent. The caller must check the stream's status to
3515 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003516 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003517static 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 +02003518{
3519 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003520 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003521 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003522 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003523 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003524 int es_now = 0;
3525 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003526 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003527 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003528
3529 if (h2c_mux_busy(h2c, h2s)) {
3530 h2s->flags |= H2_SF_BLK_MBUSY;
3531 goto end;
3532 }
3533
Willy Tarreau44e973f2018-03-01 17:49:30 +01003534 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003535 h2c->flags |= H2_CF_MUX_MALLOC;
3536 h2s->flags |= H2_SF_BLK_MROOM;
3537 goto end;
3538 }
3539
3540 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003541 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003542 goto end;
3543
3544 chunk_reset(&outbuf);
3545
3546 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003547 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003548 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003549 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003550
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003551 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003552 break;
3553 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003554 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003555 }
3556
3557 if (outbuf.size < 9) {
3558 h2c->flags |= H2_CF_MUX_MFULL;
3559 h2s->flags |= H2_SF_BLK_MROOM;
3560 goto end;
3561 }
3562
3563 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003564 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3565 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3566 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003567
3568 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3569 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003570 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003571 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003572 break;
3573 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003574 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003575 if ((long long)size > h1m->curr_len)
3576 size = h1m->curr_len;
3577 break;
3578 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003579 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003580 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003581 if (!ret)
3582 goto end;
3583
3584 if (ret < 0) {
3585 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003586 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003587 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3588 goto end;
3589 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003590 max -= ret;
3591 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003592 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003593 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003594 }
3595
Willy Tarreau801250e2018-09-11 11:45:04 +02003596 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003597 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003598 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003599 if (!ret)
3600 goto end;
3601
3602 if (ret < 0) {
3603 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003604 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003605 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3606 goto end;
3607 }
3608
3609 size = chunk;
3610 h1m->curr_len = chunk;
3611 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003612 max -= ret;
3613 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003614 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003615 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003616 if (!size)
3617 goto send_empty;
3618 }
3619
3620 /* in MSG_DATA state, continue below */
3621 size = h1m->curr_len;
3622 break;
3623 }
3624
3625 /* we have in <size> the exact number of bytes we need to copy from
3626 * the H1 buffer. We need to check this against the connection's and
3627 * the stream's send windows, and to ensure that this fits in the max
3628 * frame size and in the buffer's available space minus 9 bytes (for
3629 * the frame header). The connection's flow control is applied last so
3630 * that we can use a separate list of streams which are immediately
3631 * unblocked on window opening. Note: we don't implement padding.
3632 */
3633
Willy Tarreau5dd17352018-06-14 13:33:30 +02003634 if (size > max)
3635 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003636
3637 if (size > h2s->mws)
3638 size = h2s->mws;
3639
3640 if (size <= 0) {
3641 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02003642 if (h2s->send_wait) {
3643 LIST_DEL(&h2s->list);
3644 LIST_INIT(&h2s->list);
3645 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003646 goto end;
3647 }
3648
3649 if (h2c->mfs && size > h2c->mfs)
3650 size = h2c->mfs;
3651
3652 if (size + 9 > outbuf.size) {
3653 /* we have an opportunity for enlarging the too small
3654 * available space, let's try.
3655 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003656 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003657 goto realign_again;
3658 size = outbuf.size - 9;
3659 }
3660
3661 if (size <= 0) {
3662 h2c->flags |= H2_CF_MUX_MFULL;
3663 h2s->flags |= H2_SF_BLK_MROOM;
3664 goto end;
3665 }
3666
3667 if (size > h2c->mws)
3668 size = h2c->mws;
3669
3670 if (size <= 0) {
3671 h2s->flags |= H2_SF_BLK_MFCTL;
3672 goto end;
3673 }
3674
3675 /* copy whatever we can */
3676 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003677 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003678 if (ret == 1)
3679 len2 = 0;
3680
3681 if (!ret || len1 + len2 < size) {
3682 /* FIXME: must normally never happen */
3683 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3684 goto end;
3685 }
3686
3687 /* limit len1/len2 to size */
3688 if (len1 + len2 > size) {
3689 int sub = len1 + len2 - size;
3690
3691 if (len2 > sub)
3692 len2 -= sub;
3693 else {
3694 sub -= len2;
3695 len2 = 0;
3696 len1 -= sub;
3697 }
3698 }
3699
3700 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003701 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003702 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003703 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003704
3705 send_empty:
3706 /* we may need to add END_STREAM */
3707 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3708 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003709 *
3710 * FIXME: what we do here is not correct because we send end_stream
3711 * before knowing if we'll have to send a HEADERS frame for the
3712 * trailers. More importantly we're not consuming the trailing CRLF
3713 * after the end of trailers, so it will be left to the caller to
3714 * eat it. The right way to do it would be to measure trailers here
3715 * and to send ES only if there are no trailers.
3716 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003717 */
3718 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02003719 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003720 es_now = 1;
3721
3722 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003723 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003724
3725 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003726 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003727
3728 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003729 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003730
3731 /* consume incoming H1 response */
3732 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003733 max -= size;
3734 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003735 total += size;
3736 h1m->curr_len -= size;
3737 h2s->mws -= size;
3738 h2c->mws -= size;
3739
3740 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02003741 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003742 goto new_frame;
3743 }
3744 }
3745
3746 if (es_now) {
3747 if (h2s->st == H2_SS_OPEN)
3748 h2s->st = H2_SS_HLOC;
3749 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003750 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003751
Willy Tarreau35a62702018-02-27 15:37:25 +01003752 if (!(h1m->flags & H1_MF_CHNK)) {
3753 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003754 total += max;
3755 ofs += max;
3756 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003757
Willy Tarreau801250e2018-09-11 11:45:04 +02003758 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003759 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003760
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003761 h2s->flags |= H2_SF_ES_SENT;
3762 }
3763
3764 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003765 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 +02003766 return total;
3767}
3768
Willy Tarreau115e83b2018-12-01 19:17:53 +01003769/* Try to send a HEADERS frame matching HTX response present in HTX message
3770 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
3771 * must check the stream's status to detect any error which might have happened
3772 * subsequently to a successful send. The htx blocks are automatically removed
3773 * from the message. The htx message is assumed to be valid since produced from
3774 * the internal code, hence it contains a start line, an optional series of
3775 * header blocks and an end of header, otherwise an invalid frame could be
3776 * emitted and the resulting htx message could be left in an inconsistent state.
3777 */
3778static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
3779{
3780 struct http_hdr list[MAX_HTTP_HDR];
3781 struct h2c *h2c = h2s->h2c;
3782 struct htx_blk *blk;
3783 struct htx_blk *blk_end;
3784 struct buffer outbuf;
3785 struct htx_sl *sl;
3786 enum htx_blk_type type;
3787 int es_now = 0;
3788 int ret = 0;
3789 int hdr;
3790 int idx;
3791
3792 if (h2c_mux_busy(h2c, h2s)) {
3793 h2s->flags |= H2_SF_BLK_MBUSY;
3794 return 0;
3795 }
3796
3797 if (!h2_get_buf(h2c, &h2c->mbuf)) {
3798 h2c->flags |= H2_CF_MUX_MALLOC;
3799 h2s->flags |= H2_SF_BLK_MROOM;
3800 return 0;
3801 }
3802
3803 /* determine the first block which must not be deleted, blk_end may
3804 * be NULL if all blocks have to be deleted.
3805 */
3806 idx = htx_get_head(htx);
3807 blk_end = NULL;
3808 while (idx != -1) {
3809 type = htx_get_blk_type(htx_get_blk(htx, idx));
3810 idx = htx_get_next(htx, idx);
3811 if (type == HTX_BLK_EOH) {
3812 if (idx != -1)
3813 blk_end = htx_get_blk(htx, idx);
3814 break;
3815 }
3816 }
3817
3818 /* get the start line, we do have one */
3819 blk = htx_get_blk(htx, htx->sl_off);
3820 sl = htx_get_blk_ptr(htx, blk);
3821 h2s->status = sl->info.res.status;
3822
3823 /* and the rest of the headers, that we dump starting at header 0 */
3824 hdr = 0;
3825
Willy Tarreaufab9bb02018-12-02 12:11:16 +01003826 idx = htx->sl_off;
Willy Tarreau115e83b2018-12-01 19:17:53 +01003827 while ((idx = htx_get_next(htx, idx)) != -1) {
3828 blk = htx_get_blk(htx, idx);
3829 type = htx_get_blk_type(blk);
3830
3831 if (type == HTX_BLK_UNUSED)
3832 continue;
3833
3834 if (type != HTX_BLK_HDR)
3835 break;
3836
3837 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
3838 goto fail;
3839
3840 list[hdr].n = htx_get_blk_name(htx, blk);
3841 list[hdr].v = htx_get_blk_value(htx, blk);
3842
3843#if 1
3844 {
3845 /* FIXME: header names MUST be lower case in H2. For now it's
3846 * not granted by HTX so let's force them now.
3847 */
3848 char *p;
3849 for (p = list[hdr].n.ptr; p != list[hdr].n.ptr + list[hdr].n.len; p++)
3850 if (unlikely(isupper(*p)))
3851 *p = tolower(*p);
3852 }
3853#endif
3854 hdr++;
3855 }
3856
3857 /* marker for end of headers */
3858 list[hdr].n = ist("");
3859
3860 if (h2s->status == 204 || h2s->status == 304) {
3861 /* no contents, claim c-len is present and set to zero */
3862 es_now = 1;
3863 }
3864
3865 chunk_reset(&outbuf);
3866
3867 while (1) {
3868 outbuf.area = b_tail(&h2c->mbuf);
3869 outbuf.size = b_contig_space(&h2c->mbuf);
3870 outbuf.data = 0;
3871
3872 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
3873 break;
3874 realign_again:
3875 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
3876 }
3877
3878 if (outbuf.size < 9)
3879 goto full;
3880
3881 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
3882 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3883 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3884 outbuf.data = 9;
3885
3886 /* encode status, which necessarily is the first one */
3887 if (outbuf.data < outbuf.size && h2s->status == 200)
3888 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
3889 else if (outbuf.data < outbuf.size && h2s->status == 304)
3890 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
3891 else if (unlikely(h2s->status < 100 || h2s->status > 999)) {
3892 /* this is an unparsable response */
3893 goto fail;
3894 }
3895 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
3896 /* basic encoding of the status code */
3897 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3898 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3899 outbuf.area[outbuf.data++] = '0' + h2s->status / 100;
3900 outbuf.area[outbuf.data++] = '0' + h2s->status / 10 % 10;
3901 outbuf.area[outbuf.data++] = '0' + h2s->status % 10;
3902 }
3903 else {
3904 if (b_space_wraps(&h2c->mbuf))
3905 goto realign_again;
3906 goto full;
3907 }
3908
3909 /* encode all headers, stop at empty name */
3910 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
3911 /* these ones do not exist in H2 and must be dropped. */
3912 if (isteq(list[hdr].n, ist("connection")) ||
3913 isteq(list[hdr].n, ist("proxy-connection")) ||
3914 isteq(list[hdr].n, ist("keep-alive")) ||
3915 isteq(list[hdr].n, ist("upgrade")) ||
3916 isteq(list[hdr].n, ist("transfer-encoding")))
3917 continue;
3918
3919 if (isteq(list[hdr].n, ist("")))
3920 break; // end
3921
3922 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3923 /* output full */
3924 if (b_space_wraps(&h2c->mbuf))
3925 goto realign_again;
3926 goto full;
3927 }
3928 }
3929
3930 /* we may need to add END_STREAM.
3931 * FIXME: we should also set it when we know for sure that the
3932 * content-length is zero as well as on 204/304
3933 */
3934 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
3935 es_now = 1;
3936
3937 if (h2s->cs->flags & CS_FL_SHW)
3938 es_now = 1;
3939
3940 /* update the frame's size */
3941 h2_set_frame_size(outbuf.area, outbuf.data - 9);
3942
3943 if (es_now)
3944 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
3945
3946 /* commit the H2 response */
3947 b_add(&h2c->mbuf, outbuf.data);
3948 h2s->flags |= H2_SF_HEADERS_SENT;
3949
3950 /* for now we don't implemented CONTINUATION, so we wait for a
3951 * body or directly end in TRL2.
3952 */
3953 if (es_now) {
3954 h2s->flags |= H2_SF_ES_SENT;
3955 if (h2s->st == H2_SS_OPEN)
3956 h2s->st = H2_SS_HLOC;
3957 else
3958 h2s_close(h2s);
3959 }
3960
3961 /* OK we could properly deliver the response */
3962
3963 /* remove all header blocks including the EOH and compute the
3964 * corresponding size.
3965 *
3966 * FIXME: We should remove everything when es_now is set.
3967 */
3968 ret = 0;
3969 idx = htx_get_head(htx);
3970 blk = htx_get_blk(htx, idx);
3971 while (blk != blk_end) {
3972 ret += htx_get_blksz(blk);
3973 blk = htx_remove_blk(htx, blk);
3974 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01003975
3976 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
3977 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003978 end:
3979 return ret;
3980 full:
3981 h2c->flags |= H2_CF_MUX_MFULL;
3982 h2s->flags |= H2_SF_BLK_MROOM;
3983 ret = 0;
3984 goto end;
3985 fail:
3986 /* unparsable HTX messages, too large ones to be produced in the local
3987 * list etc go here (unrecoverable errors).
3988 */
3989 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3990 ret = 0;
3991 goto end;
3992}
3993
Willy Tarreau80739692018-10-05 11:35:57 +02003994/* Try to send a HEADERS frame matching HTX request present in HTX message
3995 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
3996 * must check the stream's status to detect any error which might have happened
3997 * subsequently to a successful send. The htx blocks are automatically removed
3998 * from the message. The htx message is assumed to be valid since produced from
3999 * the internal code, hence it contains a start line, an optional series of
4000 * header blocks and an end of header, otherwise an invalid frame could be
4001 * emitted and the resulting htx message could be left in an inconsistent state.
4002 */
4003static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4004{
4005 struct http_hdr list[MAX_HTTP_HDR];
4006 struct h2c *h2c = h2s->h2c;
4007 struct htx_blk *blk;
4008 struct htx_blk *blk_end;
4009 struct buffer outbuf;
4010 struct htx_sl *sl;
4011 struct ist meth, path;
4012 enum htx_blk_type type;
4013 int es_now = 0;
4014 int ret = 0;
4015 int hdr;
4016 int idx;
4017
4018 if (h2c_mux_busy(h2c, h2s)) {
4019 h2s->flags |= H2_SF_BLK_MBUSY;
4020 return 0;
4021 }
4022
4023 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4024 h2c->flags |= H2_CF_MUX_MALLOC;
4025 h2s->flags |= H2_SF_BLK_MROOM;
4026 return 0;
4027 }
4028
4029 /* determine the first block which must not be deleted, blk_end may
4030 * be NULL if all blocks have to be deleted.
4031 */
4032 idx = htx_get_head(htx);
4033 blk_end = NULL;
4034 while (idx != -1) {
4035 type = htx_get_blk_type(htx_get_blk(htx, idx));
4036 idx = htx_get_next(htx, idx);
4037 if (type == HTX_BLK_EOH) {
4038 if (idx != -1)
4039 blk_end = htx_get_blk(htx, idx);
4040 break;
4041 }
4042 }
4043
4044 /* get the start line, we do have one */
4045 blk = htx_get_blk(htx, htx->sl_off);
4046 sl = htx_get_blk_ptr(htx, blk);
4047 meth = htx_sl_req_meth(sl);
4048 path = htx_sl_req_uri(sl);
4049
4050 /* and the rest of the headers, that we dump starting at header 0 */
4051 hdr = 0;
4052
4053 idx = htx->sl_off;
4054 while ((idx = htx_get_next(htx, idx)) != -1) {
4055 blk = htx_get_blk(htx, idx);
4056 type = htx_get_blk_type(blk);
4057
4058 if (type == HTX_BLK_UNUSED)
4059 continue;
4060
4061 if (type != HTX_BLK_HDR)
4062 break;
4063
4064 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4065 goto fail;
4066
4067 list[hdr].n = htx_get_blk_name(htx, blk);
4068 list[hdr].v = htx_get_blk_value(htx, blk);
4069
4070#if 1
4071 {
4072 /* FIXME: header names MUST be lower case in H2. For now it's
4073 * not granted by HTX so let's force them now.
4074 */
4075 char *p;
4076 for (p = list[hdr].n.ptr; p != list[hdr].n.ptr + list[hdr].n.len; p++)
4077 if (unlikely(isupper(*p)))
4078 *p = tolower(*p);
4079 }
4080#endif
4081 hdr++;
4082 }
4083
4084 /* marker for end of headers */
4085 list[hdr].n = ist("");
4086
4087 chunk_reset(&outbuf);
4088
4089 while (1) {
4090 outbuf.area = b_tail(&h2c->mbuf);
4091 outbuf.size = b_contig_space(&h2c->mbuf);
4092 outbuf.data = 0;
4093
4094 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4095 break;
4096 realign_again:
4097 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4098 }
4099
4100 if (outbuf.size < 9)
4101 goto full;
4102
4103 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4104 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4105 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4106 outbuf.data = 9;
4107
4108 /* encode the method, which necessarily is the first one */
4109 if (outbuf.data < outbuf.size && sl->info.req.meth == HTTP_METH_GET)
4110 outbuf.area[outbuf.data++] = 0x82; // indexed field : idx[02]=(":method", "GET")
4111 else if (outbuf.data < outbuf.size && sl->info.req.meth == HTTP_METH_POST)
4112 outbuf.area[outbuf.data++] = 0x83; // indexed field : idx[03]=(":method", "POST")
4113 else if (unlikely(outbuf.data + 2 + meth.len <= outbuf.size)) {
4114 /* basic encoding of the method code */
4115 outbuf.area[outbuf.data++] = 0x42; // indexed name -- name=":method" (idx 2)
4116 outbuf.area[outbuf.data++] = meth.len; // method length
4117 memcpy(&outbuf.area[outbuf.data], meth.ptr, meth.len);
4118 }
4119 else {
4120 if (b_space_wraps(&h2c->mbuf))
4121 goto realign_again;
4122 goto full;
4123 }
4124
4125 /* encode the scheme which is always "https" (or 0x86 for "http") */
4126 if (outbuf.data < outbuf.size)
4127 outbuf.area[outbuf.data++] = 0x87; // indexed field : idx[02]=(":scheme", "https")
4128
4129 /* encode the path, which necessarily is the second one */
4130 if (outbuf.data < outbuf.size && isteq(path, ist("/"))) {
4131 outbuf.area[outbuf.data++] = 0x84; // indexed field : idx[04]=(":path", "/")
4132 }
4133 else if (outbuf.data < outbuf.size && isteq(path, ist("/index.html"))) {
4134 outbuf.area[outbuf.data++] = 0x85; // indexed field : idx[04]=(":path", "/index.html")
4135 }
4136 else if (!hpack_encode_header(&outbuf, ist(":path"), path)) {
4137 /* output full */
4138 if (b_space_wraps(&h2c->mbuf))
4139 goto realign_again;
4140 goto full;
4141 }
4142
4143 /* encode all headers, stop at empty name */
4144 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4145 /* these ones do not exist in H2 and must be dropped. */
4146 if (isteq(list[hdr].n, ist("connection")) ||
4147 isteq(list[hdr].n, ist("proxy-connection")) ||
4148 isteq(list[hdr].n, ist("keep-alive")) ||
4149 isteq(list[hdr].n, ist("upgrade")) ||
4150 isteq(list[hdr].n, ist("transfer-encoding")))
4151 continue;
4152
4153 if (isteq(list[hdr].n, ist("")))
4154 break; // end
4155
4156 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4157 /* output full */
4158 if (b_space_wraps(&h2c->mbuf))
4159 goto realign_again;
4160 goto full;
4161 }
4162 }
4163
4164 /* we may need to add END_STREAM if we have no body :
4165 * - request already closed, or :
4166 * - no transfer-encoding, and :
4167 * - no content-length or content-length:0
4168 * Fixme: this doesn't take into account CONNECT requests.
4169 */
4170 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4171 es_now = 1;
4172
4173 if (sl->flags & HTX_SL_F_BODYLESS)
4174 es_now = 1;
4175
4176 if (h2s->cs->flags & CS_FL_SHW)
4177 es_now = 1;
4178
4179 /* update the frame's size */
4180 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4181
4182 if (es_now)
4183 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4184
4185 /* commit the H2 response */
4186 b_add(&h2c->mbuf, outbuf.data);
4187 h2s->flags |= H2_SF_HEADERS_SENT;
4188 h2s->st = H2_SS_OPEN;
4189
4190 /* for now we don't implemented CONTINUATION, so we wait for a
4191 * body or directly end in TRL2.
4192 */
4193 if (es_now) {
4194 // trim any possibly pending data (eg: inconsistent content-length)
4195 h2s->flags |= H2_SF_ES_SENT;
4196 h2s->st = H2_SS_HLOC;
4197 }
4198
4199 /* remove all header blocks including the EOH and compute the
4200 * corresponding size.
4201 *
4202 * FIXME: We should remove everything when es_now is set.
4203 */
4204 ret = 0;
4205 idx = htx_get_head(htx);
4206 blk = htx_get_blk(htx, idx);
4207 while (blk != blk_end) {
4208 ret += htx_get_blksz(blk);
4209 blk = htx_remove_blk(htx, blk);
4210 }
4211
4212 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4213 htx_remove_blk(htx, blk_end);
4214
4215 end:
4216 return ret;
4217 full:
4218 h2c->flags |= H2_CF_MUX_MFULL;
4219 h2s->flags |= H2_SF_BLK_MROOM;
4220 ret = 0;
4221 goto end;
4222 fail:
4223 /* unparsable HTX messages, too large ones to be produced in the local
4224 * list etc go here (unrecoverable errors).
4225 */
4226 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4227 ret = 0;
4228 goto end;
4229}
4230
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004231/* Try to send a DATA frame matching HTTP response present in HTX structure
4232 * <htx>, for stream <h2s>. Returns the number of bytes sent. The caller must
4233 * check the stream's status to detect any error which might have happened
4234 * subsequently to a successful send. Returns the number of data bytes
4235 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4236 */
4237static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct htx *htx)
4238{
4239 struct h2c *h2c = h2s->h2c;
4240 struct buffer outbuf;
4241 size_t total = 0;
4242 int es_now = 0;
4243 int bsize; /* htx block size */
4244 int fsize; /* h2 frame size */
4245 struct htx_blk *blk;
4246 enum htx_blk_type type;
4247 int idx;
4248
4249 if (h2c_mux_busy(h2c, h2s)) {
4250 h2s->flags |= H2_SF_BLK_MBUSY;
4251 goto end;
4252 }
4253
4254 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4255 h2c->flags |= H2_CF_MUX_MALLOC;
4256 h2s->flags |= H2_SF_BLK_MROOM;
4257 goto end;
4258 }
4259
4260 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4261 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4262 * the caller to handle.
4263 */
4264
4265 new_frame:
4266 if (htx_is_empty(htx))
4267 goto end;
4268
4269 idx = htx_get_head(htx);
4270 blk = htx_get_blk(htx, idx);
4271 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4272 bsize = htx_get_blksz(blk);
4273 fsize = bsize;
4274
4275 if (type == HTX_BLK_EOD) {
4276 /* if we have an EOD, we're dealing with chunked data. We may
4277 * have a set of trailers after us that the caller will want to
4278 * deal with. Let's simply remove the EOD and return.
4279 */
4280 htx_remove_blk(htx, blk);
4281 // FIXME, it seems we must not return it in the total bytes count?
4282 //total++; // EOD counts as one byte
4283 goto end;
4284 }
4285
4286 /* for DATA and EOM we'll have to emit a frame, even if empty */
4287
4288 while (1) {
4289 outbuf.area = b_tail(&h2c->mbuf);
4290 outbuf.size = b_contig_space(&h2c->mbuf);
4291 outbuf.data = 0;
4292
4293 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4294 break;
4295 realign_again:
4296 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4297 }
4298
4299 if (outbuf.size < 9) {
4300 h2c->flags |= H2_CF_MUX_MFULL;
4301 h2s->flags |= H2_SF_BLK_MROOM;
4302 goto end;
4303 }
4304
4305 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4306 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4307 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4308 outbuf.data = 9;
4309
4310 /* we have in <fsize> the exact number of bytes we need to copy from
4311 * the HTX buffer. We need to check this against the connection's and
4312 * the stream's send windows, and to ensure that this fits in the max
4313 * frame size and in the buffer's available space minus 9 bytes (for
4314 * the frame header). The connection's flow control is applied last so
4315 * that we can use a separate list of streams which are immediately
4316 * unblocked on window opening. Note: we don't implement padding.
4317 */
4318
4319 /* EOM is presented with bsize==1 but would lead to the emission of an
4320 * empty frame, thus we force it to zero here.
4321 */
4322 if (type == HTX_BLK_EOM)
4323 bsize = fsize = 0;
4324
4325 if (!fsize)
4326 goto send_empty;
4327
4328 if (h2s->mws <= 0) {
4329 h2s->flags |= H2_SF_BLK_SFCTL;
4330 if (h2s->send_wait) {
4331 LIST_DEL(&h2s->list);
4332 LIST_INIT(&h2s->list);
4333 }
4334 goto end;
4335 }
4336
4337 if (fsize > h2s->mws)
4338 fsize = h2s->mws; // >0
4339
4340 if (h2c->mfs && fsize > h2c->mfs)
4341 fsize = h2c->mfs; // >0
4342
4343 if (fsize + 9 > outbuf.size) {
4344 /* we have an opportunity for enlarging the too small
4345 * available space, let's try.
4346 * FIXME: is this really interesting to do? Maybe we'll
4347 * spend lots of time realigning instead of using two
4348 * frames.
4349 */
4350 if (b_space_wraps(&h2c->mbuf))
4351 goto realign_again;
4352 fsize = outbuf.size - 9;
4353
4354 if (fsize <= 0) {
4355 /* no need to send an empty frame here */
4356 h2c->flags |= H2_CF_MUX_MFULL;
4357 h2s->flags |= H2_SF_BLK_MROOM;
4358 goto end;
4359 }
4360 }
4361
4362 if (h2c->mws <= 0) {
4363 h2s->flags |= H2_SF_BLK_MFCTL;
4364 goto end;
4365 }
4366
4367 if (fsize > h2c->mws)
4368 fsize = h2c->mws;
4369
4370 /* now let's copy this this into the output buffer */
4371 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004372 h2s->mws -= fsize;
4373 h2c->mws -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004374
4375 send_empty:
4376 /* update the frame's size */
4377 h2_set_frame_size(outbuf.area, fsize);
4378
4379 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4380 * meeting EOM. We should optimize this later.
4381 */
4382 if (type == HTX_BLK_EOM) {
4383 // FIXME, it seems we must not return it in the total bytes count?
4384 // total++; // EOM counts as one byte
4385 es_now = 1;
4386 }
4387
4388 if (es_now)
4389 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4390
4391 /* commit the H2 response */
4392 b_add(&h2c->mbuf, fsize + 9);
4393
4394 /* consume incoming HTX block, including EOM */
4395 total += fsize;
4396 if (fsize == bsize) {
4397 htx_remove_blk(htx, blk);
4398 if (fsize)
4399 goto new_frame;
4400 } else {
4401 /* we've truncated this block */
4402 htx_cut_data_blk(htx, blk, fsize);
4403 }
4404
4405 if (es_now) {
4406 if (h2s->st == H2_SS_OPEN)
4407 h2s->st = H2_SS_HLOC;
4408 else
4409 h2s_close(h2s);
4410
4411 h2s->flags |= H2_SF_ES_SENT;
4412 }
4413
4414 end:
4415 return total;
4416}
4417
Olivier Houchard6ff20392018-07-17 18:46:31 +02004418/* Called from the upper layer, to subscribe to events, such as being able to send */
4419static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
4420{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004421 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004422 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004423 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004424
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004425 if (event_type & SUB_CAN_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004426 sw = param;
4427 if (!(sw->wait_reason & SUB_CAN_RECV)) {
4428 sw->wait_reason |= SUB_CAN_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004429 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004430 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004431 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004432 event_type &= ~SUB_CAN_RECV;
4433 }
4434 if (event_type & SUB_CAN_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02004435 sw = param;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004436 if (!(sw->wait_reason & SUB_CAN_SEND)) {
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004437 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004438 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004439 h2s->send_wait = sw;
4440 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
4441 if (h2s->flags & H2_SF_BLK_MFCTL)
4442 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
4443 else
4444 LIST_ADDQ(&h2c->send_list, &h2s->list);
4445 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004446 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004447 event_type &= ~SUB_CAN_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004448 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004449 if (event_type != 0)
4450 return -1;
4451 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004452
4453
4454}
4455
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004456static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
4457{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004458 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004459 struct h2s *h2s = cs->ctx;
4460
4461 if (event_type & SUB_CAN_RECV) {
4462 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004463 if (h2s->recv_wait == sw) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004464 sw->wait_reason &= ~SUB_CAN_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004465 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004466 }
4467 }
4468 if (event_type & SUB_CAN_SEND) {
4469 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004470 if (h2s->send_wait == sw) {
4471 LIST_DEL(&h2s->list);
4472 LIST_INIT(&h2s->list);
4473 sw->wait_reason &= ~SUB_CAN_SEND;
4474 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004475 }
4476 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004477 if (event_type & SUB_CALL_UNSUBSCRIBE) {
4478 sw = param;
4479 if (h2s->send_wait == sw) {
4480 sw->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
4481 h2s->send_wait = NULL;
4482 }
4483 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004484 return 0;
4485}
4486
4487
Olivier Houchard511efea2018-08-16 15:30:32 +02004488/* Called from the upper layer, to receive data */
4489static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
4490{
Olivier Houchard638b7992018-08-16 15:41:52 +02004491 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01004492 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01004493 struct htx *h2s_htx = NULL;
4494 struct htx *buf_htx = NULL;
4495 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02004496 size_t ret = 0;
4497
4498 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01004499 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
4500 /* in HTX mode we ignore the count argument */
4501 h2s_htx = htx_from_buf(&h2s->rxbuf);
4502 if (htx_is_empty(h2s_htx))
4503 goto end;
4504
4505 buf_htx = htx_from_buf(buf);
4506 count = htx_free_space(buf_htx);
4507
Willy Tarreau0c22fa72018-12-04 15:21:35 +01004508 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau86724e22018-12-01 23:19:43 +01004509
4510 buf_htx->extra = h2s_htx->extra;
4511 if (htx_is_not_empty(buf_htx))
4512 b_set_data(buf, b_size(buf));
4513 ret = htx_ret.ret;
4514 }
4515 else {
4516 ret = b_xfer(buf, &h2s->rxbuf, count);
4517 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004518
Olivier Houchard638b7992018-08-16 15:41:52 +02004519 if (b_data(&h2s->rxbuf))
Olivier Houchard511efea2018-08-16 15:30:32 +02004520 cs->flags |= CS_FL_RCV_MORE;
4521 else {
4522 cs->flags &= ~CS_FL_RCV_MORE;
4523 if (cs->flags & CS_FL_REOS)
4524 cs->flags |= CS_FL_EOS;
Olivier Houchard638b7992018-08-16 15:41:52 +02004525 if (b_size(&h2s->rxbuf)) {
4526 b_free(&h2s->rxbuf);
4527 offer_buffers(NULL, tasks_run_queue);
4528 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004529 }
4530
Willy Tarreau082f5592018-11-25 08:03:32 +01004531 if (ret && h2c->dsi == h2s->id) {
4532 /* demux is blocking on this stream's buffer */
4533 h2c->flags &= ~H2_CF_DEM_SFULL;
4534 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV)) {
4535 if (h2_recv_allowed(h2c))
4536 tasklet_wakeup(h2c->wait_event.task);
4537 }
4538 }
Willy Tarreau86724e22018-12-01 23:19:43 +01004539end:
Olivier Houchard511efea2018-08-16 15:30:32 +02004540 return ret;
4541}
4542
Olivier Houchardd846c262018-10-19 17:24:29 +02004543static void h2_stop_senders(struct h2c *h2c)
4544{
4545 struct h2s *h2s, *h2s_back;
4546
4547 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
4548 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
4549 if (h2c->msi == h2s_id(h2s))
4550 continue;
4551 LIST_DEL(&h2s->list);
4552 LIST_INIT(&h2s->list);
4553 task_remove_from_task_list((struct task *)h2s->send_wait->task);
4554 h2s->send_wait->wait_reason |= SUB_CAN_SEND;
4555 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
4556 LIST_ADD(&h2c->send_list, &h2s->list);
4557 }
4558}
4559
Willy Tarreau62f52692017-10-08 23:01:42 +02004560/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02004561static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02004562{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004563 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01004564 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004565 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004566 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004567 struct htx *htx;
4568 struct htx_blk *blk;
4569 enum htx_blk_type btype;
4570 uint32_t bsize;
4571 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004572
Olivier Houchardd846c262018-10-19 17:24:29 +02004573 if (h2s->send_wait) {
4574 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
4575 h2s->send_wait = NULL;
4576 LIST_DEL(&h2s->list);
4577 LIST_INIT(&h2s->list);
4578 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02004579 if (h2s->h2c->st0 < H2_CS_FRAME_H)
4580 return 0;
4581
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004582 /* htx will be enough to decide if we're using HTX or legacy */
4583 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
4584
Willy Tarreau0bad0432018-06-14 16:54:01 +02004585 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01004586 h2s->flags |= H2_SF_OUTGOING_DATA;
4587
Willy Tarreau751f2d02018-10-05 09:35:00 +02004588 if (h2s->id == 0) {
4589 int32_t id = h2c_get_next_sid(h2s->h2c);
4590
4591 if (id < 0) {
4592 cs->ctx = NULL;
4593 cs->flags |= CS_FL_ERROR;
4594 h2s_destroy(h2s);
4595 return 0;
4596 }
4597
4598 eb32_delete(&h2s->by_id);
4599 h2s->by_id.key = h2s->id = id;
4600 h2s->h2c->max_id = id;
4601 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
4602 }
4603
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004604 if (htx) {
Willy Tarreaub08d91f2018-12-04 15:23:57 +01004605 while (!(h2s->flags & H2_SF_BLK_ANY) && count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004606 idx = htx_get_head(htx);
4607 blk = htx_get_blk(htx, idx);
4608 btype = htx_get_blk_type(blk);
4609 bsize = htx_get_blksz(blk);
4610
4611 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02004612 case HTX_BLK_REQ_SL:
4613 /* start-line before headers */
4614 ret = h2s_htx_bck_make_req_headers(h2s, htx);
4615 if (ret > 0) {
4616 total += ret;
4617 count -= ret;
4618 if (ret < bsize)
4619 goto done;
4620 }
4621 break;
4622
Willy Tarreau115e83b2018-12-01 19:17:53 +01004623 case HTX_BLK_RES_SL:
4624 /* start-line before headers */
4625 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
4626 if (ret > 0) {
4627 total += ret;
4628 count -= ret;
4629 if (ret < bsize)
4630 goto done;
4631 }
4632 break;
4633
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004634 case HTX_BLK_DATA:
4635 case HTX_BLK_EOD:
4636 case HTX_BLK_EOM:
4637 /* all these cause the emission of a DATA frame (possibly empty) */
4638 ret = h2s_htx_frt_make_resp_data(h2s, htx);
4639 if (ret > 0) {
4640 total += ret;
4641 count -= ret;
4642 if (ret < bsize)
4643 goto done;
4644 }
4645 break;
4646
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004647 default:
4648 htx_remove_blk(htx, blk);
4649 total += bsize;
4650 count -= bsize;
4651 break;
4652 }
4653 }
4654 goto done;
4655 }
4656
4657 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02004658 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02004659 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02004660 if (h2s->h2c->flags & H2_CF_IS_BACK)
4661 ret = -1;
4662 else
4663 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004664 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004665 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02004666 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004667 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004668 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004669 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004670 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004671
Willy Tarreau5dd17352018-06-14 13:33:30 +02004672 if (unlikely((int)ret <= 0)) {
4673 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004674 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4675 break;
4676 }
Willy Tarreau35a62702018-02-27 15:37:25 +01004677 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02004678 total += count;
4679 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004680 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004681 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004682 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004683 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004684 cs->flags |= CS_FL_ERROR;
4685 break;
4686 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02004687
4688 total += ret;
4689 count -= ret;
4690
4691 if (h2s->st >= H2_SS_ERROR)
4692 break;
4693
4694 if (h2s->flags & H2_SF_BLK_ANY)
4695 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004696 }
4697
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004698 done:
Willy Tarreau00610962018-07-19 10:58:28 +02004699 if (h2s->st >= H2_SS_ERROR) {
4700 /* trim any possibly pending data after we close (extra CR-LF,
4701 * unprocessed trailers, abnormal extra data, ...)
4702 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004703 total += count;
4704 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02004705 }
4706
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004707 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01004708 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004709 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01004710 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01004711 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004712 }
4713
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004714 if (htx) {
4715 if (htx_is_empty(htx)) {
4716 htx_reset(htx);
4717 b_set_data(buf, 0);
4718 }
4719 } else {
4720 b_del(buf, total);
4721 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004722
4723 /* The mux is full, cancel the pending tasks */
4724 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
4725 (h2s->flags & H2_SF_BLK_MBUSY))
4726 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004727
Olivier Houchard8122a8d2018-12-03 19:13:29 +01004728 /* If we're running HTX, and we read the whole buffer, then pretend
4729 * we read exactly what the caller specified, as with HTX the caller
4730 * will always give the buffer size, instead of the amount of data
4731 * available.
4732 */
4733 if (htx && !b_data(buf))
4734 total = orig_count;
4735
Olivier Houchard7505f942018-08-21 18:10:44 +02004736 if (total > 0) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004737 if (!(h2s->h2c->wait_event.wait_reason & SUB_CAN_SEND))
4738 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02004739
Olivier Houchard7505f942018-08-21 18:10:44 +02004740 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004741 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02004742}
4743
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004744/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02004745static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004746{
4747 struct h2c *h2c = conn->mux_ctx;
4748 struct h2s *h2s;
4749 struct eb32_node *node;
4750 int fctl_cnt = 0;
4751 int send_cnt = 0;
4752 int tree_cnt = 0;
4753 int orph_cnt = 0;
4754
4755 if (!h2c)
4756 return;
4757
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004758 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004759 fctl_cnt++;
4760
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004761 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004762 send_cnt++;
4763
4764 node = eb32_first(&h2c->streams_by_id);
4765 while (node) {
4766 h2s = container_of(node, struct h2s, by_id);
4767 tree_cnt++;
4768 if (!h2s->cs)
4769 orph_cnt++;
4770 node = eb32_next(node);
4771 }
4772
Willy Tarreau616ac812018-07-24 14:12:42 +02004773 chunk_appendf(msg, " st0=%d err=%d maxid=%d lastid=%d flg=0x%08x nbst=%u nbcs=%u"
4774 " fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
4775 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
4776 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
4777 (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
4778 (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004779}
Willy Tarreau62f52692017-10-08 23:01:42 +02004780
4781/*******************************************************/
4782/* functions below are dedicated to the config parsers */
4783/*******************************************************/
4784
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02004785/* config parser for global "tune.h2.header-table-size" */
4786static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
4787 struct proxy *defpx, const char *file, int line,
4788 char **err)
4789{
4790 if (too_many_args(1, args, err, NULL))
4791 return -1;
4792
4793 h2_settings_header_table_size = atoi(args[1]);
4794 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
4795 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
4796 return -1;
4797 }
4798 return 0;
4799}
Willy Tarreau62f52692017-10-08 23:01:42 +02004800
Willy Tarreaue6baec02017-07-27 11:45:11 +02004801/* config parser for global "tune.h2.initial-window-size" */
4802static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
4803 struct proxy *defpx, const char *file, int line,
4804 char **err)
4805{
4806 if (too_many_args(1, args, err, NULL))
4807 return -1;
4808
4809 h2_settings_initial_window_size = atoi(args[1]);
4810 if (h2_settings_initial_window_size < 0) {
4811 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
4812 return -1;
4813 }
4814 return 0;
4815}
4816
Willy Tarreau5242ef82017-07-27 11:47:28 +02004817/* config parser for global "tune.h2.max-concurrent-streams" */
4818static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
4819 struct proxy *defpx, const char *file, int line,
4820 char **err)
4821{
4822 if (too_many_args(1, args, err, NULL))
4823 return -1;
4824
4825 h2_settings_max_concurrent_streams = atoi(args[1]);
4826 if (h2_settings_max_concurrent_streams < 0) {
4827 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
4828 return -1;
4829 }
4830 return 0;
4831}
4832
Willy Tarreau62f52692017-10-08 23:01:42 +02004833
4834/****************************************/
4835/* MUX initialization and instanciation */
4836/***************************************/
4837
4838/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01004839static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02004840 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02004841 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02004842 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02004843 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02004844 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004845 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02004846 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01004847 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02004848 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01004849 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01004850 .avail_streams = h2_avail_streams,
Olivier Houchard8defe4b2018-12-02 01:31:17 +01004851 .max_streams = h2_max_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02004852 .shutr = h2_shutr,
4853 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004854 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01004855 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02004856 .name = "H2",
4857};
4858
Christopher Faulet32f61c02018-04-10 14:33:41 +02004859/* PROTO selection : this mux registers PROTO token "h2" */
4860static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02004861 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02004862
Willy Tarreau0108d902018-11-25 19:14:37 +01004863INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
4864
Willy Tarreauf8957272018-10-03 10:25:20 +02004865static struct mux_proto_list mux_proto_h2_htx =
4866 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
4867
4868INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
4869
Willy Tarreau62f52692017-10-08 23:01:42 +02004870/* config keyword parsers */
4871static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02004872 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02004873 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02004874 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02004875 { 0, NULL, NULL }
4876}};
4877
Willy Tarreau0108d902018-11-25 19:14:37 +01004878INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);