blob: 2c8d1cc96597608a95379a1ecaf85a93db5aa88a [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:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003121 if (htx)
3122 htx_to_buf(htx, &h2s->rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003123 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02003124 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003125 fail:
3126 outlen = 0;
3127 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02003128}
3129
Willy Tarreau454f9052017-10-26 19:40:35 +02003130/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3131 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3132 * in use, a new chunk is emitted for each frame. This is supposed to fit
3133 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3134 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3135 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003136 * parser state is automatically updated. Returns > 0 if it could completely
3137 * send the current frame, 0 if it couldn't complete, in which case
3138 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3139 * DATA frame can return 0 as a valid result). Stream errors are reported in
3140 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3141 * have checked the frame header and ensured that the frame was complete or the
3142 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003143 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003144static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003145{
3146 struct h2c *h2c = h2s->h2c;
3147 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003148 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003149 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003150 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003151 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003152
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003153 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003154
3155 /* The padlen is the first byte before data, and the padding appears
3156 * after data. padlen+data+padding are included in flen.
3157 */
Willy Tarreau79127812017-12-03 21:06:59 +01003158 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003159 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003160 return 0;
3161
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003162 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003163 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003164 /* RFC7540#6.1 : pad length = length of frame payload or greater */
3165 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02003166 return 0;
3167 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003168
3169 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003170 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003171 h2c->dfl--;
3172 h2c->rcvd_c++; h2c->rcvd_s++;
3173 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02003174 }
3175
Olivier Houchard638b7992018-08-16 15:41:52 +02003176 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003177 if (!csbuf) {
3178 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003179 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003180 }
3181
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003182try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003183 flen = h2c->dfl - h2c->dpl;
3184 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003185 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003186
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003187 if (flen > b_data(&h2c->dbuf)) {
3188 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003189 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003190 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003191 }
3192
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003193 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
3194 htx = htx_from_buf(csbuf);
3195 block1 = htx_free_data_space(htx);
3196 if (!block1) {
3197 h2c->flags |= H2_CF_DEM_SFULL;
3198 goto fail;
3199 }
3200 if (flen > block1)
3201 flen = block1;
3202
3203 /* here, flen is the max we can copy into the output buffer */
3204 block1 = b_contig_data(&h2c->dbuf, 0);
3205 if (flen > block1)
3206 flen = block1;
3207
3208 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3209 h2c->flags |= H2_CF_DEM_SFULL;
3210 goto fail;
3211 }
3212
3213 b_del(&h2c->dbuf, flen);
3214 h2c->dfl -= flen;
3215 h2c->rcvd_c += flen;
3216 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3217 goto try_again;
3218 }
3219 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003220 /* it doesn't fit and the buffer is fragmented,
3221 * so let's defragment it and try again.
3222 */
3223 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003224 }
3225
Willy Tarreaueba10f22018-04-25 20:44:22 +02003226 /* chunked-encoding requires more room */
3227 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003228 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003229 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3230 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3231 (chklen < 1048576) ? 4 : 8;
3232 chklen += 4; // CRLF, CRLF
3233 }
3234
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003235 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003236 if (flen + chklen > b_room(csbuf)) {
3237 if (chklen >= b_room(csbuf)) {
3238 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003239 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003240 }
3241 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003242 }
3243
3244 if (h2s->flags & H2_SF_DATA_CHNK) {
3245 /* emit the chunk size */
3246 unsigned int chksz = flen;
3247 char str[10];
3248 char *beg;
3249
3250 beg = str + sizeof(str);
3251 *--beg = '\n';
3252 *--beg = '\r';
3253 do {
3254 *--beg = hextab[chksz & 0xF];
3255 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003256 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003257 }
3258
Willy Tarreau454f9052017-10-26 19:40:35 +02003259 /* Block1 is the length of the first block before the buffer wraps,
3260 * block2 is the optional second block to reach the end of the frame.
3261 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003262 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003263 if (block1 > flen)
3264 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003265 block2 = flen - block1;
3266
3267 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003268 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003269
3270 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003271 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003272
Willy Tarreaueba10f22018-04-25 20:44:22 +02003273 if (h2s->flags & H2_SF_DATA_CHNK) {
3274 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003275 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003276 }
3277
Willy Tarreau454f9052017-10-26 19:40:35 +02003278 /* now mark the input data as consumed (will be deleted from the buffer
3279 * by the caller when seeing FRAME_A after sending the window update).
3280 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003281 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003282 h2c->dfl -= flen;
3283 h2c->rcvd_c += flen;
3284 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3285
3286 if (h2c->dfl > h2c->dpl) {
3287 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003288 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003289 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003290 }
3291
Willy Tarreau4a28da12018-01-04 14:41:00 +01003292 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003293 /* here we're done with the frame, all the payload (except padding) was
3294 * transferred.
3295 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003296
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003297 if (h2c->dff & H2_F_DATA_END_STREAM) {
3298 if (htx) {
3299 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3300 h2c->flags |= H2_CF_DEM_SFULL;
3301 goto fail;
3302 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003303 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003304 else if (h2s->flags & H2_SF_DATA_CHNK) {
3305 /* emit the trailing 0 CRLF CRLF */
3306 if (b_room(csbuf) < 5) {
3307 h2c->flags |= H2_CF_DEM_SFULL;
3308 goto fail;
3309 }
3310 chklen += 5;
3311 b_putblk(csbuf, "0\r\n\r\n", 5);
3312 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003313 }
3314
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003315 h2c->rcvd_c += h2c->dpl;
3316 h2c->rcvd_s += h2c->dpl;
3317 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003318 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3319
Willy Tarreau39d68502018-03-02 12:26:37 +01003320 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003321 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003322 h2s->cs->flags |= CS_FL_REOS;
3323 }
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003324 if (htx)
3325 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003326 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003327 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003328 if (htx)
3329 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003330 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003331}
3332
Willy Tarreau5dd17352018-06-14 13:33:30 +02003333/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3334 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3335 * number of bytes sent. The caller must check the stream's status to detect
3336 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003337 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003338static 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 +02003339{
3340 struct http_hdr list[MAX_HTTP_HDR];
3341 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003342 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003343 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003344 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003345 int es_now = 0;
3346 int ret = 0;
3347 int hdr;
3348
3349 if (h2c_mux_busy(h2c, h2s)) {
3350 h2s->flags |= H2_SF_BLK_MBUSY;
3351 return 0;
3352 }
3353
Willy Tarreau44e973f2018-03-01 17:49:30 +01003354 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003355 h2c->flags |= H2_CF_MUX_MALLOC;
3356 h2s->flags |= H2_SF_BLK_MROOM;
3357 return 0;
3358 }
3359
3360 /* First, try to parse the H1 response and index it into <list>.
3361 * NOTE! Since it comes from haproxy, we *know* that a response header
3362 * block does not wrap and we can safely read it this way without
3363 * having to realign the buffer.
3364 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003365 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003366 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003367 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003368 /* incomplete or invalid response, this is abnormal coming from
3369 * haproxy and may only result in a bad errorfile or bad Lua code
3370 * so that won't be fixed, raise an error now.
3371 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003372 * FIXME: we should instead add the ability to only return a
3373 * 502 bad gateway. But in theory this is not supposed to
3374 * happen.
3375 */
3376 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3377 ret = 0;
3378 goto end;
3379 }
3380
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003381 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003382
3383 /* certain statuses have no body or an empty one, regardless of
3384 * what the headers say.
3385 */
3386 if (sl.st.status >= 100 && sl.st.status < 200) {
3387 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3388 h1m->curr_len = h1m->body_len = 0;
3389 }
3390 else if (sl.st.status == 204 || sl.st.status == 304) {
3391 /* no contents, claim c-len is present and set to zero */
3392 h1m->flags &= ~H1_MF_CHNK;
3393 h1m->flags |= H1_MF_CLEN;
3394 h1m->curr_len = h1m->body_len = 0;
3395 }
3396
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003397 chunk_reset(&outbuf);
3398
3399 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003400 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003401 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003402 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003403
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003404 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003405 break;
3406 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003407 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003408 }
3409
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003410 if (outbuf.size < 9)
3411 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003412
3413 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003414 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3415 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3416 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003417
3418 /* encode status, which necessarily is the first one */
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003419 if (outbuf.data < outbuf.size && h2s->status == 200)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003420 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003421 else if (outbuf.data < outbuf.size && h2s->status == 304)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003422 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003423 else if (unlikely(list[0].v.len != 3)) {
3424 /* this is an unparsable response */
3425 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3426 ret = 0;
3427 goto end;
3428 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003429 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003430 /* basic encoding of the status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003431 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3432 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3433 outbuf.area[outbuf.data++] = list[0].v.ptr[0];
3434 outbuf.area[outbuf.data++] = list[0].v.ptr[1];
3435 outbuf.area[outbuf.data++] = list[0].v.ptr[2];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003436 }
3437 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003438 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003439 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003440 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003441 }
3442
3443 /* encode all headers, stop at empty name */
3444 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003445 /* these ones do not exist in H2 and must be dropped. */
3446 if (isteq(list[hdr].n, ist("connection")) ||
3447 isteq(list[hdr].n, ist("proxy-connection")) ||
3448 isteq(list[hdr].n, ist("keep-alive")) ||
3449 isteq(list[hdr].n, ist("upgrade")) ||
3450 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003451 continue;
3452
3453 if (isteq(list[hdr].n, ist("")))
3454 break; // end
3455
3456 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3457 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003458 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003459 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003460 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003461 }
3462 }
3463
3464 /* we may need to add END_STREAM */
3465 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3466 es_now = 1;
3467
3468 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003469 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003470
3471 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003472 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003473
3474 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003475 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003476
3477 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003478 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003479 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003480
3481 /* for now we don't implemented CONTINUATION, so we wait for a
3482 * body or directly end in TRL2.
3483 */
3484 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003485 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003486 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003487
Willy Tarreau801250e2018-09-11 11:45:04 +02003488 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003489 h2s->flags |= H2_SF_ES_SENT;
3490 if (h2s->st == H2_SS_OPEN)
3491 h2s->st = H2_SS_HLOC;
3492 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003493 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003494 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003495 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003496 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003497 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003498 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003499 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003500 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003501 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003502
3503 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003504
3505 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003506 //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 +02003507 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003508 full:
3509 h1m_init_res(h1m);
3510 h1m->err_pos = -1; // don't care about errors on the response path
3511 h2c->flags |= H2_CF_MUX_MFULL;
3512 h2s->flags |= H2_SF_BLK_MROOM;
3513 ret = 0;
3514 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003515}
3516
Willy Tarreau5dd17352018-06-14 13:33:30 +02003517/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3518 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3519 * the number of bytes sent. The caller must check the stream's status to
3520 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003521 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003522static 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 +02003523{
3524 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003525 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003526 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003527 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003528 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003529 int es_now = 0;
3530 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003531 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003532 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003533
3534 if (h2c_mux_busy(h2c, h2s)) {
3535 h2s->flags |= H2_SF_BLK_MBUSY;
3536 goto end;
3537 }
3538
Willy Tarreau44e973f2018-03-01 17:49:30 +01003539 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003540 h2c->flags |= H2_CF_MUX_MALLOC;
3541 h2s->flags |= H2_SF_BLK_MROOM;
3542 goto end;
3543 }
3544
3545 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003546 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003547 goto end;
3548
3549 chunk_reset(&outbuf);
3550
3551 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003552 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003553 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003554 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003555
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003556 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003557 break;
3558 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003559 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003560 }
3561
3562 if (outbuf.size < 9) {
3563 h2c->flags |= H2_CF_MUX_MFULL;
3564 h2s->flags |= H2_SF_BLK_MROOM;
3565 goto end;
3566 }
3567
3568 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003569 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3570 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3571 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003572
3573 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3574 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003575 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003576 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003577 break;
3578 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003579 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003580 if ((long long)size > h1m->curr_len)
3581 size = h1m->curr_len;
3582 break;
3583 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003584 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003585 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003586 if (!ret)
3587 goto end;
3588
3589 if (ret < 0) {
3590 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003591 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003592 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3593 goto end;
3594 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003595 max -= ret;
3596 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003597 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003598 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003599 }
3600
Willy Tarreau801250e2018-09-11 11:45:04 +02003601 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003602 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003603 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003604 if (!ret)
3605 goto end;
3606
3607 if (ret < 0) {
3608 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003609 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003610 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3611 goto end;
3612 }
3613
3614 size = chunk;
3615 h1m->curr_len = chunk;
3616 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003617 max -= ret;
3618 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003619 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003620 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003621 if (!size)
3622 goto send_empty;
3623 }
3624
3625 /* in MSG_DATA state, continue below */
3626 size = h1m->curr_len;
3627 break;
3628 }
3629
3630 /* we have in <size> the exact number of bytes we need to copy from
3631 * the H1 buffer. We need to check this against the connection's and
3632 * the stream's send windows, and to ensure that this fits in the max
3633 * frame size and in the buffer's available space minus 9 bytes (for
3634 * the frame header). The connection's flow control is applied last so
3635 * that we can use a separate list of streams which are immediately
3636 * unblocked on window opening. Note: we don't implement padding.
3637 */
3638
Willy Tarreau5dd17352018-06-14 13:33:30 +02003639 if (size > max)
3640 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003641
3642 if (size > h2s->mws)
3643 size = h2s->mws;
3644
3645 if (size <= 0) {
3646 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02003647 if (h2s->send_wait) {
3648 LIST_DEL(&h2s->list);
3649 LIST_INIT(&h2s->list);
3650 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003651 goto end;
3652 }
3653
3654 if (h2c->mfs && size > h2c->mfs)
3655 size = h2c->mfs;
3656
3657 if (size + 9 > outbuf.size) {
3658 /* we have an opportunity for enlarging the too small
3659 * available space, let's try.
3660 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003661 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003662 goto realign_again;
3663 size = outbuf.size - 9;
3664 }
3665
3666 if (size <= 0) {
3667 h2c->flags |= H2_CF_MUX_MFULL;
3668 h2s->flags |= H2_SF_BLK_MROOM;
3669 goto end;
3670 }
3671
3672 if (size > h2c->mws)
3673 size = h2c->mws;
3674
3675 if (size <= 0) {
3676 h2s->flags |= H2_SF_BLK_MFCTL;
3677 goto end;
3678 }
3679
3680 /* copy whatever we can */
3681 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003682 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003683 if (ret == 1)
3684 len2 = 0;
3685
3686 if (!ret || len1 + len2 < size) {
3687 /* FIXME: must normally never happen */
3688 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3689 goto end;
3690 }
3691
3692 /* limit len1/len2 to size */
3693 if (len1 + len2 > size) {
3694 int sub = len1 + len2 - size;
3695
3696 if (len2 > sub)
3697 len2 -= sub;
3698 else {
3699 sub -= len2;
3700 len2 = 0;
3701 len1 -= sub;
3702 }
3703 }
3704
3705 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003706 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003707 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003708 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003709
3710 send_empty:
3711 /* we may need to add END_STREAM */
3712 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3713 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003714 *
3715 * FIXME: what we do here is not correct because we send end_stream
3716 * before knowing if we'll have to send a HEADERS frame for the
3717 * trailers. More importantly we're not consuming the trailing CRLF
3718 * after the end of trailers, so it will be left to the caller to
3719 * eat it. The right way to do it would be to measure trailers here
3720 * and to send ES only if there are no trailers.
3721 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003722 */
3723 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02003724 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003725 es_now = 1;
3726
3727 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003728 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003729
3730 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003731 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003732
3733 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003734 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003735
3736 /* consume incoming H1 response */
3737 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003738 max -= size;
3739 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003740 total += size;
3741 h1m->curr_len -= size;
3742 h2s->mws -= size;
3743 h2c->mws -= size;
3744
3745 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02003746 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003747 goto new_frame;
3748 }
3749 }
3750
3751 if (es_now) {
3752 if (h2s->st == H2_SS_OPEN)
3753 h2s->st = H2_SS_HLOC;
3754 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003755 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003756
Willy Tarreau35a62702018-02-27 15:37:25 +01003757 if (!(h1m->flags & H1_MF_CHNK)) {
3758 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003759 total += max;
3760 ofs += max;
3761 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003762
Willy Tarreau801250e2018-09-11 11:45:04 +02003763 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003764 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003765
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003766 h2s->flags |= H2_SF_ES_SENT;
3767 }
3768
3769 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003770 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 +02003771 return total;
3772}
3773
Willy Tarreau115e83b2018-12-01 19:17:53 +01003774/* Try to send a HEADERS frame matching HTX response present in HTX message
3775 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
3776 * must check the stream's status to detect any error which might have happened
3777 * subsequently to a successful send. The htx blocks are automatically removed
3778 * from the message. The htx message is assumed to be valid since produced from
3779 * the internal code, hence it contains a start line, an optional series of
3780 * header blocks and an end of header, otherwise an invalid frame could be
3781 * emitted and the resulting htx message could be left in an inconsistent state.
3782 */
3783static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
3784{
3785 struct http_hdr list[MAX_HTTP_HDR];
3786 struct h2c *h2c = h2s->h2c;
3787 struct htx_blk *blk;
3788 struct htx_blk *blk_end;
3789 struct buffer outbuf;
3790 struct htx_sl *sl;
3791 enum htx_blk_type type;
3792 int es_now = 0;
3793 int ret = 0;
3794 int hdr;
3795 int idx;
3796
3797 if (h2c_mux_busy(h2c, h2s)) {
3798 h2s->flags |= H2_SF_BLK_MBUSY;
3799 return 0;
3800 }
3801
3802 if (!h2_get_buf(h2c, &h2c->mbuf)) {
3803 h2c->flags |= H2_CF_MUX_MALLOC;
3804 h2s->flags |= H2_SF_BLK_MROOM;
3805 return 0;
3806 }
3807
3808 /* determine the first block which must not be deleted, blk_end may
3809 * be NULL if all blocks have to be deleted.
3810 */
3811 idx = htx_get_head(htx);
3812 blk_end = NULL;
3813 while (idx != -1) {
3814 type = htx_get_blk_type(htx_get_blk(htx, idx));
3815 idx = htx_get_next(htx, idx);
3816 if (type == HTX_BLK_EOH) {
3817 if (idx != -1)
3818 blk_end = htx_get_blk(htx, idx);
3819 break;
3820 }
3821 }
3822
3823 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01003824 sl = htx_get_stline(htx);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003825 h2s->status = sl->info.res.status;
3826
3827 /* and the rest of the headers, that we dump starting at header 0 */
3828 hdr = 0;
3829
Willy Tarreau8e162ee2018-12-06 14:07:27 +01003830 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01003831 while ((idx = htx_get_next(htx, idx)) != -1) {
3832 blk = htx_get_blk(htx, idx);
3833 type = htx_get_blk_type(blk);
3834
3835 if (type == HTX_BLK_UNUSED)
3836 continue;
3837
3838 if (type != HTX_BLK_HDR)
3839 break;
3840
3841 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
3842 goto fail;
3843
3844 list[hdr].n = htx_get_blk_name(htx, blk);
3845 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003846 hdr++;
3847 }
3848
3849 /* marker for end of headers */
3850 list[hdr].n = ist("");
3851
3852 if (h2s->status == 204 || h2s->status == 304) {
3853 /* no contents, claim c-len is present and set to zero */
3854 es_now = 1;
3855 }
3856
3857 chunk_reset(&outbuf);
3858
3859 while (1) {
3860 outbuf.area = b_tail(&h2c->mbuf);
3861 outbuf.size = b_contig_space(&h2c->mbuf);
3862 outbuf.data = 0;
3863
3864 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
3865 break;
3866 realign_again:
3867 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
3868 }
3869
3870 if (outbuf.size < 9)
3871 goto full;
3872
3873 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
3874 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3875 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3876 outbuf.data = 9;
3877
3878 /* encode status, which necessarily is the first one */
3879 if (outbuf.data < outbuf.size && h2s->status == 200)
3880 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
3881 else if (outbuf.data < outbuf.size && h2s->status == 304)
3882 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
3883 else if (unlikely(h2s->status < 100 || h2s->status > 999)) {
3884 /* this is an unparsable response */
3885 goto fail;
3886 }
3887 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
3888 /* basic encoding of the status code */
3889 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3890 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3891 outbuf.area[outbuf.data++] = '0' + h2s->status / 100;
3892 outbuf.area[outbuf.data++] = '0' + h2s->status / 10 % 10;
3893 outbuf.area[outbuf.data++] = '0' + h2s->status % 10;
3894 }
3895 else {
3896 if (b_space_wraps(&h2c->mbuf))
3897 goto realign_again;
3898 goto full;
3899 }
3900
3901 /* encode all headers, stop at empty name */
3902 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
3903 /* these ones do not exist in H2 and must be dropped. */
3904 if (isteq(list[hdr].n, ist("connection")) ||
3905 isteq(list[hdr].n, ist("proxy-connection")) ||
3906 isteq(list[hdr].n, ist("keep-alive")) ||
3907 isteq(list[hdr].n, ist("upgrade")) ||
3908 isteq(list[hdr].n, ist("transfer-encoding")))
3909 continue;
3910
3911 if (isteq(list[hdr].n, ist("")))
3912 break; // end
3913
3914 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3915 /* output full */
3916 if (b_space_wraps(&h2c->mbuf))
3917 goto realign_again;
3918 goto full;
3919 }
3920 }
3921
3922 /* we may need to add END_STREAM.
3923 * FIXME: we should also set it when we know for sure that the
3924 * content-length is zero as well as on 204/304
3925 */
3926 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
3927 es_now = 1;
3928
3929 if (h2s->cs->flags & CS_FL_SHW)
3930 es_now = 1;
3931
3932 /* update the frame's size */
3933 h2_set_frame_size(outbuf.area, outbuf.data - 9);
3934
3935 if (es_now)
3936 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
3937
3938 /* commit the H2 response */
3939 b_add(&h2c->mbuf, outbuf.data);
3940 h2s->flags |= H2_SF_HEADERS_SENT;
3941
3942 /* for now we don't implemented CONTINUATION, so we wait for a
3943 * body or directly end in TRL2.
3944 */
3945 if (es_now) {
3946 h2s->flags |= H2_SF_ES_SENT;
3947 if (h2s->st == H2_SS_OPEN)
3948 h2s->st = H2_SS_HLOC;
3949 else
3950 h2s_close(h2s);
3951 }
3952
3953 /* OK we could properly deliver the response */
3954
3955 /* remove all header blocks including the EOH and compute the
3956 * corresponding size.
3957 *
3958 * FIXME: We should remove everything when es_now is set.
3959 */
3960 ret = 0;
3961 idx = htx_get_head(htx);
3962 blk = htx_get_blk(htx, idx);
3963 while (blk != blk_end) {
3964 ret += htx_get_blksz(blk);
3965 blk = htx_remove_blk(htx, blk);
3966 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01003967
3968 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
3969 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003970 end:
3971 return ret;
3972 full:
3973 h2c->flags |= H2_CF_MUX_MFULL;
3974 h2s->flags |= H2_SF_BLK_MROOM;
3975 ret = 0;
3976 goto end;
3977 fail:
3978 /* unparsable HTX messages, too large ones to be produced in the local
3979 * list etc go here (unrecoverable errors).
3980 */
3981 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3982 ret = 0;
3983 goto end;
3984}
3985
Willy Tarreau80739692018-10-05 11:35:57 +02003986/* Try to send a HEADERS frame matching HTX request present in HTX message
3987 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
3988 * must check the stream's status to detect any error which might have happened
3989 * subsequently to a successful send. The htx blocks are automatically removed
3990 * from the message. The htx message is assumed to be valid since produced from
3991 * the internal code, hence it contains a start line, an optional series of
3992 * header blocks and an end of header, otherwise an invalid frame could be
3993 * emitted and the resulting htx message could be left in an inconsistent state.
3994 */
3995static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
3996{
3997 struct http_hdr list[MAX_HTTP_HDR];
3998 struct h2c *h2c = h2s->h2c;
3999 struct htx_blk *blk;
4000 struct htx_blk *blk_end;
4001 struct buffer outbuf;
4002 struct htx_sl *sl;
4003 struct ist meth, path;
4004 enum htx_blk_type type;
4005 int es_now = 0;
4006 int ret = 0;
4007 int hdr;
4008 int idx;
4009
4010 if (h2c_mux_busy(h2c, h2s)) {
4011 h2s->flags |= H2_SF_BLK_MBUSY;
4012 return 0;
4013 }
4014
4015 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4016 h2c->flags |= H2_CF_MUX_MALLOC;
4017 h2s->flags |= H2_SF_BLK_MROOM;
4018 return 0;
4019 }
4020
4021 /* determine the first block which must not be deleted, blk_end may
4022 * be NULL if all blocks have to be deleted.
4023 */
4024 idx = htx_get_head(htx);
4025 blk_end = NULL;
4026 while (idx != -1) {
4027 type = htx_get_blk_type(htx_get_blk(htx, idx));
4028 idx = htx_get_next(htx, idx);
4029 if (type == HTX_BLK_EOH) {
4030 if (idx != -1)
4031 blk_end = htx_get_blk(htx, idx);
4032 break;
4033 }
4034 }
4035
4036 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004037 sl = htx_get_stline(htx);
Willy Tarreau80739692018-10-05 11:35:57 +02004038 meth = htx_sl_req_meth(sl);
4039 path = htx_sl_req_uri(sl);
4040
4041 /* and the rest of the headers, that we dump starting at header 0 */
4042 hdr = 0;
4043
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004044 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004045 while ((idx = htx_get_next(htx, idx)) != -1) {
4046 blk = htx_get_blk(htx, idx);
4047 type = htx_get_blk_type(blk);
4048
4049 if (type == HTX_BLK_UNUSED)
4050 continue;
4051
4052 if (type != HTX_BLK_HDR)
4053 break;
4054
4055 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4056 goto fail;
4057
4058 list[hdr].n = htx_get_blk_name(htx, blk);
4059 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004060 hdr++;
4061 }
4062
4063 /* marker for end of headers */
4064 list[hdr].n = ist("");
4065
4066 chunk_reset(&outbuf);
4067
4068 while (1) {
4069 outbuf.area = b_tail(&h2c->mbuf);
4070 outbuf.size = b_contig_space(&h2c->mbuf);
4071 outbuf.data = 0;
4072
4073 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4074 break;
4075 realign_again:
4076 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4077 }
4078
4079 if (outbuf.size < 9)
4080 goto full;
4081
4082 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4083 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4084 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4085 outbuf.data = 9;
4086
4087 /* encode the method, which necessarily is the first one */
4088 if (outbuf.data < outbuf.size && sl->info.req.meth == HTTP_METH_GET)
4089 outbuf.area[outbuf.data++] = 0x82; // indexed field : idx[02]=(":method", "GET")
4090 else if (outbuf.data < outbuf.size && sl->info.req.meth == HTTP_METH_POST)
4091 outbuf.area[outbuf.data++] = 0x83; // indexed field : idx[03]=(":method", "POST")
4092 else if (unlikely(outbuf.data + 2 + meth.len <= outbuf.size)) {
4093 /* basic encoding of the method code */
4094 outbuf.area[outbuf.data++] = 0x42; // indexed name -- name=":method" (idx 2)
4095 outbuf.area[outbuf.data++] = meth.len; // method length
4096 memcpy(&outbuf.area[outbuf.data], meth.ptr, meth.len);
4097 }
4098 else {
4099 if (b_space_wraps(&h2c->mbuf))
4100 goto realign_again;
4101 goto full;
4102 }
4103
4104 /* encode the scheme which is always "https" (or 0x86 for "http") */
4105 if (outbuf.data < outbuf.size)
4106 outbuf.area[outbuf.data++] = 0x87; // indexed field : idx[02]=(":scheme", "https")
4107
4108 /* encode the path, which necessarily is the second one */
4109 if (outbuf.data < outbuf.size && isteq(path, ist("/"))) {
4110 outbuf.area[outbuf.data++] = 0x84; // indexed field : idx[04]=(":path", "/")
4111 }
4112 else if (outbuf.data < outbuf.size && isteq(path, ist("/index.html"))) {
4113 outbuf.area[outbuf.data++] = 0x85; // indexed field : idx[04]=(":path", "/index.html")
4114 }
4115 else if (!hpack_encode_header(&outbuf, ist(":path"), path)) {
4116 /* output full */
4117 if (b_space_wraps(&h2c->mbuf))
4118 goto realign_again;
4119 goto full;
4120 }
4121
4122 /* encode all headers, stop at empty name */
4123 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4124 /* these ones do not exist in H2 and must be dropped. */
4125 if (isteq(list[hdr].n, ist("connection")) ||
4126 isteq(list[hdr].n, ist("proxy-connection")) ||
4127 isteq(list[hdr].n, ist("keep-alive")) ||
4128 isteq(list[hdr].n, ist("upgrade")) ||
4129 isteq(list[hdr].n, ist("transfer-encoding")))
4130 continue;
4131
4132 if (isteq(list[hdr].n, ist("")))
4133 break; // end
4134
4135 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4136 /* output full */
4137 if (b_space_wraps(&h2c->mbuf))
4138 goto realign_again;
4139 goto full;
4140 }
4141 }
4142
4143 /* we may need to add END_STREAM if we have no body :
4144 * - request already closed, or :
4145 * - no transfer-encoding, and :
4146 * - no content-length or content-length:0
4147 * Fixme: this doesn't take into account CONNECT requests.
4148 */
4149 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4150 es_now = 1;
4151
4152 if (sl->flags & HTX_SL_F_BODYLESS)
4153 es_now = 1;
4154
4155 if (h2s->cs->flags & CS_FL_SHW)
4156 es_now = 1;
4157
4158 /* update the frame's size */
4159 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4160
4161 if (es_now)
4162 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4163
4164 /* commit the H2 response */
4165 b_add(&h2c->mbuf, outbuf.data);
4166 h2s->flags |= H2_SF_HEADERS_SENT;
4167 h2s->st = H2_SS_OPEN;
4168
4169 /* for now we don't implemented CONTINUATION, so we wait for a
4170 * body or directly end in TRL2.
4171 */
4172 if (es_now) {
4173 // trim any possibly pending data (eg: inconsistent content-length)
4174 h2s->flags |= H2_SF_ES_SENT;
4175 h2s->st = H2_SS_HLOC;
4176 }
4177
4178 /* remove all header blocks including the EOH and compute the
4179 * corresponding size.
4180 *
4181 * FIXME: We should remove everything when es_now is set.
4182 */
4183 ret = 0;
4184 idx = htx_get_head(htx);
4185 blk = htx_get_blk(htx, idx);
4186 while (blk != blk_end) {
4187 ret += htx_get_blksz(blk);
4188 blk = htx_remove_blk(htx, blk);
4189 }
4190
4191 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4192 htx_remove_blk(htx, blk_end);
4193
4194 end:
4195 return ret;
4196 full:
4197 h2c->flags |= H2_CF_MUX_MFULL;
4198 h2s->flags |= H2_SF_BLK_MROOM;
4199 ret = 0;
4200 goto end;
4201 fail:
4202 /* unparsable HTX messages, too large ones to be produced in the local
4203 * list etc go here (unrecoverable errors).
4204 */
4205 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4206 ret = 0;
4207 goto end;
4208}
4209
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004210/* Try to send a DATA frame matching HTTP response present in HTX structure
4211 * <htx>, for stream <h2s>. Returns the number of bytes sent. The caller must
4212 * check the stream's status to detect any error which might have happened
4213 * subsequently to a successful send. Returns the number of data bytes
4214 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4215 */
Willy Tarreauee573762018-12-04 15:25:57 +01004216static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct htx *htx, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004217{
4218 struct h2c *h2c = h2s->h2c;
4219 struct buffer outbuf;
4220 size_t total = 0;
4221 int es_now = 0;
4222 int bsize; /* htx block size */
4223 int fsize; /* h2 frame size */
4224 struct htx_blk *blk;
4225 enum htx_blk_type type;
4226 int idx;
4227
4228 if (h2c_mux_busy(h2c, h2s)) {
4229 h2s->flags |= H2_SF_BLK_MBUSY;
4230 goto end;
4231 }
4232
4233 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4234 h2c->flags |= H2_CF_MUX_MALLOC;
4235 h2s->flags |= H2_SF_BLK_MROOM;
4236 goto end;
4237 }
4238
4239 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4240 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4241 * the caller to handle.
4242 */
4243
4244 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004245 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004246 goto end;
4247
4248 idx = htx_get_head(htx);
4249 blk = htx_get_blk(htx, idx);
4250 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4251 bsize = htx_get_blksz(blk);
4252 fsize = bsize;
4253
4254 if (type == HTX_BLK_EOD) {
4255 /* if we have an EOD, we're dealing with chunked data. We may
4256 * have a set of trailers after us that the caller will want to
4257 * deal with. Let's simply remove the EOD and return.
4258 */
4259 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004260 total++; // EOD counts as one byte
4261 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004262 goto end;
4263 }
4264
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004265 if (type != HTX_BLK_DATA && type != HTX_BLK_EOM)
4266 goto end;
4267
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004268 /* for DATA and EOM we'll have to emit a frame, even if empty */
4269
4270 while (1) {
4271 outbuf.area = b_tail(&h2c->mbuf);
4272 outbuf.size = b_contig_space(&h2c->mbuf);
4273 outbuf.data = 0;
4274
4275 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4276 break;
4277 realign_again:
4278 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4279 }
4280
4281 if (outbuf.size < 9) {
4282 h2c->flags |= H2_CF_MUX_MFULL;
4283 h2s->flags |= H2_SF_BLK_MROOM;
4284 goto end;
4285 }
4286
4287 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4288 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4289 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4290 outbuf.data = 9;
4291
4292 /* we have in <fsize> the exact number of bytes we need to copy from
4293 * the HTX buffer. We need to check this against the connection's and
4294 * the stream's send windows, and to ensure that this fits in the max
4295 * frame size and in the buffer's available space minus 9 bytes (for
4296 * the frame header). The connection's flow control is applied last so
4297 * that we can use a separate list of streams which are immediately
4298 * unblocked on window opening. Note: we don't implement padding.
4299 */
4300
4301 /* EOM is presented with bsize==1 but would lead to the emission of an
4302 * empty frame, thus we force it to zero here.
4303 */
4304 if (type == HTX_BLK_EOM)
4305 bsize = fsize = 0;
4306
4307 if (!fsize)
4308 goto send_empty;
4309
4310 if (h2s->mws <= 0) {
4311 h2s->flags |= H2_SF_BLK_SFCTL;
4312 if (h2s->send_wait) {
4313 LIST_DEL(&h2s->list);
4314 LIST_INIT(&h2s->list);
4315 }
4316 goto end;
4317 }
4318
Willy Tarreauee573762018-12-04 15:25:57 +01004319 if (fsize > count)
4320 fsize = count;
4321
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004322 if (fsize > h2s->mws)
4323 fsize = h2s->mws; // >0
4324
4325 if (h2c->mfs && fsize > h2c->mfs)
4326 fsize = h2c->mfs; // >0
4327
4328 if (fsize + 9 > outbuf.size) {
4329 /* we have an opportunity for enlarging the too small
4330 * available space, let's try.
4331 * FIXME: is this really interesting to do? Maybe we'll
4332 * spend lots of time realigning instead of using two
4333 * frames.
4334 */
4335 if (b_space_wraps(&h2c->mbuf))
4336 goto realign_again;
4337 fsize = outbuf.size - 9;
4338
4339 if (fsize <= 0) {
4340 /* no need to send an empty frame here */
4341 h2c->flags |= H2_CF_MUX_MFULL;
4342 h2s->flags |= H2_SF_BLK_MROOM;
4343 goto end;
4344 }
4345 }
4346
4347 if (h2c->mws <= 0) {
4348 h2s->flags |= H2_SF_BLK_MFCTL;
4349 goto end;
4350 }
4351
4352 if (fsize > h2c->mws)
4353 fsize = h2c->mws;
4354
4355 /* now let's copy this this into the output buffer */
4356 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004357 h2s->mws -= fsize;
4358 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004359 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004360
4361 send_empty:
4362 /* update the frame's size */
4363 h2_set_frame_size(outbuf.area, fsize);
4364
4365 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4366 * meeting EOM. We should optimize this later.
4367 */
4368 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004369 total++; // EOM counts as one byte
4370 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004371 es_now = 1;
4372 }
4373
4374 if (es_now)
4375 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4376
4377 /* commit the H2 response */
4378 b_add(&h2c->mbuf, fsize + 9);
4379
4380 /* consume incoming HTX block, including EOM */
4381 total += fsize;
4382 if (fsize == bsize) {
4383 htx_remove_blk(htx, blk);
4384 if (fsize)
4385 goto new_frame;
4386 } else {
4387 /* we've truncated this block */
4388 htx_cut_data_blk(htx, blk, fsize);
4389 }
4390
4391 if (es_now) {
4392 if (h2s->st == H2_SS_OPEN)
4393 h2s->st = H2_SS_HLOC;
4394 else
4395 h2s_close(h2s);
4396
4397 h2s->flags |= H2_SF_ES_SENT;
4398 }
4399
4400 end:
4401 return total;
4402}
4403
Olivier Houchard6ff20392018-07-17 18:46:31 +02004404/* Called from the upper layer, to subscribe to events, such as being able to send */
4405static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
4406{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004407 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004408 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004409 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004410
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004411 if (event_type & SUB_CAN_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004412 sw = param;
4413 if (!(sw->wait_reason & SUB_CAN_RECV)) {
4414 sw->wait_reason |= SUB_CAN_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004415 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004416 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004417 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004418 event_type &= ~SUB_CAN_RECV;
4419 }
4420 if (event_type & SUB_CAN_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02004421 sw = param;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004422 if (!(sw->wait_reason & SUB_CAN_SEND)) {
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004423 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004424 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004425 h2s->send_wait = sw;
4426 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
4427 if (h2s->flags & H2_SF_BLK_MFCTL)
4428 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
4429 else
4430 LIST_ADDQ(&h2c->send_list, &h2s->list);
4431 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004432 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004433 event_type &= ~SUB_CAN_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004434 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004435 if (event_type != 0)
4436 return -1;
4437 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004438
4439
4440}
4441
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004442static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
4443{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004444 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004445 struct h2s *h2s = cs->ctx;
4446
4447 if (event_type & SUB_CAN_RECV) {
4448 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004449 if (h2s->recv_wait == sw) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004450 sw->wait_reason &= ~SUB_CAN_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004451 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004452 }
4453 }
4454 if (event_type & SUB_CAN_SEND) {
4455 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004456 if (h2s->send_wait == sw) {
4457 LIST_DEL(&h2s->list);
4458 LIST_INIT(&h2s->list);
4459 sw->wait_reason &= ~SUB_CAN_SEND;
4460 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004461 }
4462 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004463 if (event_type & SUB_CALL_UNSUBSCRIBE) {
4464 sw = param;
4465 if (h2s->send_wait == sw) {
4466 sw->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
4467 h2s->send_wait = NULL;
4468 }
4469 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004470 return 0;
4471}
4472
4473
Olivier Houchard511efea2018-08-16 15:30:32 +02004474/* Called from the upper layer, to receive data */
4475static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
4476{
Olivier Houchard638b7992018-08-16 15:41:52 +02004477 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01004478 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01004479 struct htx *h2s_htx = NULL;
4480 struct htx *buf_htx = NULL;
4481 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02004482 size_t ret = 0;
4483
4484 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01004485 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
4486 /* in HTX mode we ignore the count argument */
4487 h2s_htx = htx_from_buf(&h2s->rxbuf);
4488 if (htx_is_empty(h2s_htx))
4489 goto end;
4490
4491 buf_htx = htx_from_buf(buf);
4492 count = htx_free_space(buf_htx);
4493
Willy Tarreau0c22fa72018-12-04 15:21:35 +01004494 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau86724e22018-12-01 23:19:43 +01004495
4496 buf_htx->extra = h2s_htx->extra;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004497 htx_to_buf(buf_htx, buf);
4498 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01004499 ret = htx_ret.ret;
4500 }
4501 else {
4502 ret = b_xfer(buf, &h2s->rxbuf, count);
4503 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004504
Olivier Houchard638b7992018-08-16 15:41:52 +02004505 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01004506 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02004507 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01004508 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02004509 if (cs->flags & CS_FL_REOS)
4510 cs->flags |= CS_FL_EOS;
Olivier Houchard638b7992018-08-16 15:41:52 +02004511 if (b_size(&h2s->rxbuf)) {
4512 b_free(&h2s->rxbuf);
4513 offer_buffers(NULL, tasks_run_queue);
4514 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004515 }
4516
Willy Tarreau082f5592018-11-25 08:03:32 +01004517 if (ret && h2c->dsi == h2s->id) {
4518 /* demux is blocking on this stream's buffer */
4519 h2c->flags &= ~H2_CF_DEM_SFULL;
4520 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV)) {
4521 if (h2_recv_allowed(h2c))
4522 tasklet_wakeup(h2c->wait_event.task);
4523 }
4524 }
Willy Tarreau86724e22018-12-01 23:19:43 +01004525end:
Olivier Houchard511efea2018-08-16 15:30:32 +02004526 return ret;
4527}
4528
Olivier Houchardd846c262018-10-19 17:24:29 +02004529static void h2_stop_senders(struct h2c *h2c)
4530{
4531 struct h2s *h2s, *h2s_back;
4532
4533 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
4534 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
4535 if (h2c->msi == h2s_id(h2s))
4536 continue;
4537 LIST_DEL(&h2s->list);
4538 LIST_INIT(&h2s->list);
4539 task_remove_from_task_list((struct task *)h2s->send_wait->task);
4540 h2s->send_wait->wait_reason |= SUB_CAN_SEND;
4541 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
4542 LIST_ADD(&h2c->send_list, &h2s->list);
4543 }
4544}
4545
Willy Tarreau62f52692017-10-08 23:01:42 +02004546/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02004547static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02004548{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004549 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01004550 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004551 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004552 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004553 struct htx *htx;
4554 struct htx_blk *blk;
4555 enum htx_blk_type btype;
4556 uint32_t bsize;
4557 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004558
Olivier Houchardd846c262018-10-19 17:24:29 +02004559 if (h2s->send_wait) {
4560 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
4561 h2s->send_wait = NULL;
4562 LIST_DEL(&h2s->list);
4563 LIST_INIT(&h2s->list);
4564 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02004565 if (h2s->h2c->st0 < H2_CS_FRAME_H)
4566 return 0;
4567
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004568 /* htx will be enough to decide if we're using HTX or legacy */
4569 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
4570
Willy Tarreau0bad0432018-06-14 16:54:01 +02004571 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01004572 h2s->flags |= H2_SF_OUTGOING_DATA;
4573
Willy Tarreau751f2d02018-10-05 09:35:00 +02004574 if (h2s->id == 0) {
4575 int32_t id = h2c_get_next_sid(h2s->h2c);
4576
4577 if (id < 0) {
4578 cs->ctx = NULL;
4579 cs->flags |= CS_FL_ERROR;
4580 h2s_destroy(h2s);
4581 return 0;
4582 }
4583
4584 eb32_delete(&h2s->by_id);
4585 h2s->by_id.key = h2s->id = id;
4586 h2s->h2c->max_id = id;
4587 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
4588 }
4589
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004590 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01004591 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
4592 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004593 idx = htx_get_head(htx);
4594 blk = htx_get_blk(htx, idx);
4595 btype = htx_get_blk_type(blk);
4596 bsize = htx_get_blksz(blk);
4597
4598 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02004599 case HTX_BLK_REQ_SL:
4600 /* start-line before headers */
4601 ret = h2s_htx_bck_make_req_headers(h2s, htx);
4602 if (ret > 0) {
4603 total += ret;
4604 count -= ret;
4605 if (ret < bsize)
4606 goto done;
4607 }
4608 break;
4609
Willy Tarreau115e83b2018-12-01 19:17:53 +01004610 case HTX_BLK_RES_SL:
4611 /* start-line before headers */
4612 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
4613 if (ret > 0) {
4614 total += ret;
4615 count -= ret;
4616 if (ret < bsize)
4617 goto done;
4618 }
4619 break;
4620
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004621 case HTX_BLK_DATA:
4622 case HTX_BLK_EOD:
4623 case HTX_BLK_EOM:
4624 /* all these cause the emission of a DATA frame (possibly empty) */
Willy Tarreauee573762018-12-04 15:25:57 +01004625 ret = h2s_htx_frt_make_resp_data(h2s, htx, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004626 if (ret > 0) {
4627 total += ret;
4628 count -= ret;
4629 if (ret < bsize)
4630 goto done;
4631 }
4632 break;
4633
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004634 default:
4635 htx_remove_blk(htx, blk);
4636 total += bsize;
4637 count -= bsize;
4638 break;
4639 }
4640 }
4641 goto done;
4642 }
4643
4644 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02004645 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02004646 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02004647 if (h2s->h2c->flags & H2_CF_IS_BACK)
4648 ret = -1;
4649 else
4650 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004651 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004652 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02004653 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004654 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004655 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004656 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004657 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004658
Willy Tarreau5dd17352018-06-14 13:33:30 +02004659 if (unlikely((int)ret <= 0)) {
4660 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004661 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4662 break;
4663 }
Willy Tarreau35a62702018-02-27 15:37:25 +01004664 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02004665 total += count;
4666 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004667 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004668 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004669 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004670 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004671 cs->flags |= CS_FL_ERROR;
4672 break;
4673 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02004674
4675 total += ret;
4676 count -= ret;
4677
4678 if (h2s->st >= H2_SS_ERROR)
4679 break;
4680
4681 if (h2s->flags & H2_SF_BLK_ANY)
4682 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004683 }
4684
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004685 done:
Willy Tarreau00610962018-07-19 10:58:28 +02004686 if (h2s->st >= H2_SS_ERROR) {
4687 /* trim any possibly pending data after we close (extra CR-LF,
4688 * unprocessed trailers, abnormal extra data, ...)
4689 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004690 total += count;
4691 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02004692 }
4693
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004694 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01004695 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004696 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01004697 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01004698 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004699 }
4700
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004701 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004702 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004703 } else {
4704 b_del(buf, total);
4705 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004706
4707 /* The mux is full, cancel the pending tasks */
4708 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
4709 (h2s->flags & H2_SF_BLK_MBUSY))
4710 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004711
Olivier Houchard8122a8d2018-12-03 19:13:29 +01004712 /* If we're running HTX, and we read the whole buffer, then pretend
4713 * we read exactly what the caller specified, as with HTX the caller
4714 * will always give the buffer size, instead of the amount of data
4715 * available.
4716 */
4717 if (htx && !b_data(buf))
4718 total = orig_count;
4719
Olivier Houchard7505f942018-08-21 18:10:44 +02004720 if (total > 0) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004721 if (!(h2s->h2c->wait_event.wait_reason & SUB_CAN_SEND))
4722 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02004723
Olivier Houchard7505f942018-08-21 18:10:44 +02004724 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004725 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02004726}
4727
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004728/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02004729static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004730{
4731 struct h2c *h2c = conn->mux_ctx;
4732 struct h2s *h2s;
4733 struct eb32_node *node;
4734 int fctl_cnt = 0;
4735 int send_cnt = 0;
4736 int tree_cnt = 0;
4737 int orph_cnt = 0;
4738
4739 if (!h2c)
4740 return;
4741
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004742 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004743 fctl_cnt++;
4744
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004745 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004746 send_cnt++;
4747
4748 node = eb32_first(&h2c->streams_by_id);
4749 while (node) {
4750 h2s = container_of(node, struct h2s, by_id);
4751 tree_cnt++;
4752 if (!h2s->cs)
4753 orph_cnt++;
4754 node = eb32_next(node);
4755 }
4756
Willy Tarreau616ac812018-07-24 14:12:42 +02004757 chunk_appendf(msg, " st0=%d err=%d maxid=%d lastid=%d flg=0x%08x nbst=%u nbcs=%u"
4758 " fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
4759 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
4760 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
4761 (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
4762 (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004763}
Willy Tarreau62f52692017-10-08 23:01:42 +02004764
4765/*******************************************************/
4766/* functions below are dedicated to the config parsers */
4767/*******************************************************/
4768
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02004769/* config parser for global "tune.h2.header-table-size" */
4770static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
4771 struct proxy *defpx, const char *file, int line,
4772 char **err)
4773{
4774 if (too_many_args(1, args, err, NULL))
4775 return -1;
4776
4777 h2_settings_header_table_size = atoi(args[1]);
4778 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
4779 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
4780 return -1;
4781 }
4782 return 0;
4783}
Willy Tarreau62f52692017-10-08 23:01:42 +02004784
Willy Tarreaue6baec02017-07-27 11:45:11 +02004785/* config parser for global "tune.h2.initial-window-size" */
4786static int h2_parse_initial_window_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_initial_window_size = atoi(args[1]);
4794 if (h2_settings_initial_window_size < 0) {
4795 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
4796 return -1;
4797 }
4798 return 0;
4799}
4800
Willy Tarreau5242ef82017-07-27 11:47:28 +02004801/* config parser for global "tune.h2.max-concurrent-streams" */
4802static int h2_parse_max_concurrent_streams(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_max_concurrent_streams = atoi(args[1]);
4810 if (h2_settings_max_concurrent_streams < 0) {
4811 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
4812 return -1;
4813 }
4814 return 0;
4815}
4816
Willy Tarreau62f52692017-10-08 23:01:42 +02004817
4818/****************************************/
4819/* MUX initialization and instanciation */
4820/***************************************/
4821
4822/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01004823static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02004824 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02004825 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02004826 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02004827 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02004828 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004829 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02004830 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01004831 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02004832 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01004833 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01004834 .avail_streams = h2_avail_streams,
Olivier Houchard8defe4b2018-12-02 01:31:17 +01004835 .max_streams = h2_max_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02004836 .shutr = h2_shutr,
4837 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004838 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01004839 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02004840 .name = "H2",
4841};
4842
Christopher Faulet32f61c02018-04-10 14:33:41 +02004843/* PROTO selection : this mux registers PROTO token "h2" */
4844static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02004845 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02004846
Willy Tarreau0108d902018-11-25 19:14:37 +01004847INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
4848
Willy Tarreauf8957272018-10-03 10:25:20 +02004849static struct mux_proto_list mux_proto_h2_htx =
4850 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
4851
4852INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
4853
Willy Tarreau62f52692017-10-08 23:01:42 +02004854/* config keyword parsers */
4855static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02004856 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02004857 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02004858 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02004859 { 0, NULL, NULL }
4860}};
4861
Willy Tarreau0108d902018-11-25 19:14:37 +01004862INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);