blob: 58a052174295285ae72788e0b660c1c34b6d8a49 [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 Tarreauafba57a2018-12-11 13:44:24 +010015#include <common/h1.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020016#include <common/h2.h>
Willy Tarreau13278b42017-10-13 19:23:14 +020017#include <common/hpack-dec.h>
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020018#include <common/hpack-enc.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020019#include <common/hpack-tbl.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010020#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010021#include <common/initcall.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020022#include <common/net_helper.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020023#include <proto/connection.h>
Willy Tarreaubcd3bb32018-12-01 18:59:00 +010024#include <proto/http_htx.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010025#include <proto/session.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020026#include <proto/stream.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010027#include <proto/stream_interface.h>
Willy Tarreauea392822017-10-31 10:02:25 +010028#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020029#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020030
31
Willy Tarreau2a856182017-05-16 15:20:39 +020032/* dummy streams returned for idle and closed states */
33static const struct h2s *h2_closed_stream;
34static const struct h2s *h2_idle_stream;
35
Willy Tarreau5ab6b572017-09-22 08:05:00 +020036/* Connection flags (32 bit), in h2c->flags */
37#define H2_CF_NONE 0x00000000
38
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020039/* Flags indicating why writing to the mux is blocked. */
40#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
41#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
42#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
43
Willy Tarreau315d8072017-12-10 22:17:57 +010044/* Flags indicating why writing to the demux is blocked.
45 * The first two ones directly affect the ability for the mux to receive data
46 * from the connection. The other ones affect the mux's ability to demux
47 * received data.
48 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020049#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
50#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010051
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020052#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
53#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
54#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
55#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020056#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
57#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020058
Willy Tarreau081d4722017-05-16 21:51:05 +020059/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020060#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
61#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
62#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 +020063#define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection
Willy Tarreau081d4722017-05-16 21:51:05 +020064
Willy Tarreau5ab6b572017-09-22 08:05:00 +020065/* H2 connection state, in h2c->st0 */
66enum h2_cs {
67 H2_CS_PREFACE, // init done, waiting for connection preface
68 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
69 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
70 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010071 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
72 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020073 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
74 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
75 H2_CS_ENTRIES // must be last
76} __attribute__((packed));
77
78/* H2 connection descriptor */
79struct h2c {
80 struct connection *conn;
81
82 enum h2_cs st0; /* mux state */
83 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
84
85 /* 16 bit hole here */
86 uint32_t flags; /* connection flags: H2_CF_* */
87 int32_t max_id; /* highest ID known on this connection, <0 before preface */
88 uint32_t rcvd_c; /* newly received data to ACK for the connection */
89 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
90
91 /* states for the demux direction */
92 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020093 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020094
95 int32_t dsi; /* demux stream ID (<0 = idle) */
96 int32_t dfl; /* demux frame length (if dsi >= 0) */
97 int8_t dft; /* demux frame type (if dsi >= 0) */
98 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +010099 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
100 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200101 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
102
103 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200104 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200105 int32_t msi; /* mux stream ID (<0 = idle) */
106 int32_t mfl; /* mux frame length (if dsi >= 0) */
107 int8_t mft; /* mux frame type (if dsi >= 0) */
108 int8_t mff; /* mux frame flags (if dsi >= 0) */
109 /* 16 bit hole here */
110 int32_t miw; /* mux initial window size for all new streams */
111 int32_t mws; /* mux window size. Can be negative. */
112 int32_t mfs; /* mux's max frame size */
113
Willy Tarreauea392822017-10-31 10:02:25 +0100114 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100115 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100116 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200117 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200118 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100119 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200120 struct eb_root streams_by_id; /* all active streams by their ID */
121 struct list send_list; /* list of blocked streams requesting to send */
122 struct list fctl_list; /* list of streams blocked by connection's fctl */
Olivier Houchardd846c262018-10-19 17:24:29 +0200123 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100124 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200125 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200126};
127
Willy Tarreau18312642017-10-11 07:57:07 +0200128/* H2 stream state, in h2s->st */
129enum h2_ss {
130 H2_SS_IDLE = 0, // idle
131 H2_SS_RLOC, // reserved(local)
132 H2_SS_RREM, // reserved(remote)
133 H2_SS_OPEN, // open
134 H2_SS_HREM, // half-closed(remote)
135 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200136 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200137 H2_SS_CLOSED, // closed
138 H2_SS_ENTRIES // must be last
139} __attribute__((packed));
140
141/* HTTP/2 stream flags (32 bit), in h2s->flags */
142#define H2_SF_NONE 0x00000000
143#define H2_SF_ES_RCVD 0x00000001
144#define H2_SF_ES_SENT 0x00000002
145
146#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
147#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
148
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200149/* stream flags indicating the reason the stream is blocked */
150#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
151#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
152#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
153#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
154#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
155
Willy Tarreau454f9052017-10-26 19:40:35 +0200156/* stream flags indicating how data is supposed to be sent */
157#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
158#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
159
160/* step we're currently in when sending chunks. This is needed because we may
161 * have to transfer chunks as large as a full buffer so there's no room left
162 * for size nor crlf around.
163 */
164#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
165#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
166#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
167
168#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
169
Willy Tarreau67434202017-11-06 20:20:51 +0100170#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100171#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100172
Willy Tarreau18312642017-10-11 07:57:07 +0200173/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
174 * it is being processed in the internal HTTP representation (H1 for now).
175 */
176struct h2s {
177 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100178 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200179 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200180 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200181 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200182 int32_t id; /* stream ID */
183 uint32_t flags; /* H2_SF_* */
184 int mws; /* mux window size for this stream */
185 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
186 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200187 uint16_t status; /* HTTP response status */
Olivier Houchard638b7992018-08-16 15:41:52 +0200188 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200189 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
190 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
191 struct wait_event *send_wait; /* The streeam is waiting for flow control */
192 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Willy Tarreau18312642017-10-11 07:57:07 +0200193};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200194
Willy Tarreauc6405142017-09-21 20:23:50 +0200195/* descriptor for an h2 frame header */
196struct h2_fh {
197 uint32_t len; /* length, host order, 24 bits */
198 uint32_t sid; /* stream id, host order, 31 bits */
199 uint8_t ft; /* frame type */
200 uint8_t ff; /* frame flags */
201};
202
Willy Tarreau8ceae722018-11-26 11:58:30 +0100203/* the h2c connection pool */
204DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
205
206/* the h2s stream pool */
207DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
208
Willy Tarreaudc572362018-12-12 08:08:05 +0100209/* The default connection window size is 65535, it may only be enlarged using
210 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
211 * we'll pretend we already received the difference between the two to send
212 * an equivalent window update to enlarge it to 2G-1.
213 */
214#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
215
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200216/* a few settings from the global section */
217static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200218static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200219static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200220
Willy Tarreau2a856182017-05-16 15:20:39 +0200221/* a dmumy closed stream */
222static const struct h2s *h2_closed_stream = &(const struct h2s){
223 .cs = NULL,
224 .h2c = NULL,
225 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100226 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100227 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200228 .id = 0,
229};
230
231/* and a dummy idle stream for use with any unannounced stream */
232static const struct h2s *h2_idle_stream = &(const struct h2s){
233 .cs = NULL,
234 .h2c = NULL,
235 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100236 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200237 .id = 0,
238};
239
Olivier Houchard9f6af332018-05-25 14:04:04 +0200240static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200241static int h2_send(struct h2c *h2c);
242static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200243static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200244static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100245static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreauc3e18f32018-10-08 14:51:56 +0200246static int h2s_decode_headers(struct h2s *h2s);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100247static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200248static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100249static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200250
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200251/*****************************************************/
252/* functions below are for dynamic buffer management */
253/*****************************************************/
254
Willy Tarreau315d8072017-12-10 22:17:57 +0100255/* indicates whether or not the we may call the h2_recv() function to attempt
256 * to receive data into the buffer and/or demux pending data. The condition is
257 * a bit complex due to some API limits for now. The rules are the following :
258 * - if an error or a shutdown was detected on the connection and the buffer
259 * is empty, we must not attempt to receive
260 * - if the demux buf failed to be allocated, we must not try to receive and
261 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100262 * - if no flag indicates a blocking condition, we may attempt to receive,
263 * regardless of whether the demux buffer is full or not, so that only
264 * de demux part decides whether or not to block. This is needed because
265 * the connection API indeed prevents us from re-enabling receipt that is
266 * already enabled in a polled state, so we must always immediately stop
267 * as soon as the demux can't proceed so as never to hit an end of read
268 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100269 * - otherwise must may not attempt
270 */
271static inline int h2_recv_allowed(const struct h2c *h2c)
272{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200273 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100274 (h2c->st0 >= H2_CS_ERROR ||
275 h2c->conn->flags & CO_FL_ERROR ||
276 conn_xprt_read0_pending(h2c->conn)))
277 return 0;
278
279 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100280 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100281 return 1;
282
283 return 0;
284}
285
Willy Tarreauf2101912018-07-19 10:11:38 +0200286/* returns true if the connection has too many conn_streams attached */
287static inline int h2_has_too_many_cs(const struct h2c *h2c)
288{
289 return h2c->nb_cs >= h2_settings_max_concurrent_streams;
290}
291
Willy Tarreau44e973f2018-03-01 17:49:30 +0100292/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
293 * flags are used to figure what buffer was requested. It returns 1 if the
294 * allocation succeeds, in which case the connection is woken up, or 0 if it's
295 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200296 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100297static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200298{
299 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100300 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200301
Willy Tarreau44e973f2018-03-01 17:49:30 +0100302 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200303 h2c->flags &= ~H2_CF_DEM_DALLOC;
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 Tarreau35dbd5d2017-09-22 09:13:49 +0200306 return 1;
307 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200308
Willy Tarreau44e973f2018-03-01 17:49:30 +0100309 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
310 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200311
312 if (h2c->flags & H2_CF_DEM_MROOM) {
313 h2c->flags &= ~H2_CF_DEM_MROOM;
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 Tarreau1b62c5c2017-09-25 11:55:01 +0200316 }
Willy Tarreau14398122017-09-22 14:26:04 +0200317 return 1;
318 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100319
320 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
321 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200322 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100323 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard53216e72018-10-10 15:46:36 +0200324 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200325 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau0b559072018-02-26 15:22:17 +0100326 return 1;
327 }
328
Willy Tarreau14398122017-09-22 14:26:04 +0200329 return 0;
330}
331
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200332static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200333{
334 struct buffer *buf = NULL;
335
Willy Tarreau44e973f2018-03-01 17:49:30 +0100336 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
337 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
338 h2c->buf_wait.target = h2c;
339 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100340 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100341 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100342 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200343 __conn_xprt_stop_recv(h2c->conn);
344 }
345 return buf;
346}
347
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200348static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200349{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200350 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100351 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200352 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200353 }
354}
355
Olivier Houchardd540b362018-11-05 18:37:53 +0100356static int h2_avail_streams(struct connection *conn)
357{
358 struct h2c *h2c = conn->mux_ctx;
359
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100360 /* XXX Should use the negociated max concurrent stream nb instead of the conf value */
Olivier Houchardd540b362018-11-05 18:37:53 +0100361 return (h2_settings_max_concurrent_streams - h2c->nb_streams);
362}
363
Olivier Houchard8defe4b2018-12-02 01:31:17 +0100364static int h2_max_streams(struct connection *conn)
365{
366 /* XXX Should use the negociated max concurrent stream nb instead of the conf value */
367 return h2_settings_max_concurrent_streams;
368}
369
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200370
Willy Tarreau62f52692017-10-08 23:01:42 +0200371/*****************************************************************/
372/* functions below are dedicated to the mux setup and management */
373/*****************************************************************/
374
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200375/* Initialize the mux once it's attached. For outgoing connections, the context
376 * is already initialized before installing the mux, so we detect incoming
377 * connections from the fact that the context is still NULL. Returns < 0 on
378 * error.
379 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100380static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200381{
382 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100383 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200384
Willy Tarreaubafbe012017-11-24 17:34:44 +0100385 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200386 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200387 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200388
Willy Tarreau01b44822018-10-03 14:26:37 +0200389 if (conn->mux_ctx) {
390 h2c->flags = H2_CF_IS_BACK;
391 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
392 if (tick_isset(prx->timeout.serverfin))
393 h2c->shut_timeout = prx->timeout.serverfin;
394 } else {
395 h2c->flags = H2_CF_NONE;
396 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
397 if (tick_isset(prx->timeout.clientfin))
398 h2c->shut_timeout = prx->timeout.clientfin;
399 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100400
Willy Tarreau0b37d652018-10-03 10:33:02 +0200401 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100402 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100403 if (tick_isset(h2c->timeout)) {
404 t = task_new(tid_bit);
405 if (!t)
406 goto fail;
407
408 h2c->task = t;
409 t->process = h2_timeout_task;
410 t->context = h2c;
411 t->expire = tick_add(now_ms, h2c->timeout);
412 }
Willy Tarreauea392822017-10-31 10:02:25 +0100413
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200414 h2c->wait_event.task = tasklet_new();
415 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200416 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200417 h2c->wait_event.task->process = h2_io_cb;
418 h2c->wait_event.task->context = h2c;
419 h2c->wait_event.wait_reason = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200420
Willy Tarreau32218eb2017-09-22 08:07:25 +0200421 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
422 if (!h2c->ddht)
423 goto fail;
424
425 /* Initialise the context. */
426 h2c->st0 = H2_CS_PREFACE;
427 h2c->conn = conn;
428 h2c->max_id = -1;
429 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreaudc572362018-12-12 08:08:05 +0100430 h2c->rcvd_c = H2_INITIAL_WINDOW_INCREMENT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200431 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100432 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200433 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200434
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200435 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200436 h2c->dsi = -1;
437 h2c->msi = -1;
438 h2c->last_sid = -1;
439
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200440 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200441 h2c->miw = 65535; /* mux initial window size */
442 h2c->mws = 65535; /* mux window size */
443 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200444 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200445 LIST_INIT(&h2c->send_list);
446 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200447 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100448 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200449
Willy Tarreau3f133572017-10-31 19:21:06 +0100450 if (t)
451 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100452
Willy Tarreau01b44822018-10-03 14:26:37 +0200453 if (h2c->flags & H2_CF_IS_BACK) {
454 /* FIXME: this is temporary, for outgoing connections we need
455 * to immediately allocate a stream until the code is modified
456 * so that the caller calls ->attach(). For now the outgoing cs
457 * is stored as conn->mux_ctx by the caller.
458 */
459 struct h2s *h2s;
460
Olivier Houchardf502aca2018-12-14 19:42:40 +0100461 h2s = h2c_bck_stream_new(h2c, conn->mux_ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200462 if (!h2s)
463 goto fail_stream;
464 }
465
466 conn->mux_ctx = h2c;
467
Willy Tarreau0f383582018-10-03 14:22:21 +0200468 /* prepare to read something */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200469 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200470 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200471 fail_stream:
472 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200473 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100474 if (t)
475 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200476 if (h2c->wait_event.task)
477 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100478 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200479 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200480 return -1;
481}
482
Willy Tarreau751f2d02018-10-05 09:35:00 +0200483/* returns the next allocatable outgoing stream ID for the H2 connection, or
484 * -1 if no more is allocatable.
485 */
486static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
487{
488 int32_t id = (h2c->max_id + 1) | 1;
489 if (id & 0x80000000U)
490 id = -1;
491 return id;
492}
493
Willy Tarreau2373acc2017-10-12 17:35:14 +0200494/* returns the stream associated with id <id> or NULL if not found */
495static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
496{
497 struct eb32_node *node;
498
Willy Tarreau751f2d02018-10-05 09:35:00 +0200499 if (id == 0)
500 return (struct h2s *)h2_closed_stream;
501
Willy Tarreau2a856182017-05-16 15:20:39 +0200502 if (id > h2c->max_id)
503 return (struct h2s *)h2_idle_stream;
504
Willy Tarreau2373acc2017-10-12 17:35:14 +0200505 node = eb32_lookup(&h2c->streams_by_id, id);
506 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200507 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200508
509 return container_of(node, struct h2s, by_id);
510}
511
Willy Tarreau62f52692017-10-08 23:01:42 +0200512/* release function for a connection. This one should be called to free all
513 * resources allocated to the mux.
514 */
515static void h2_release(struct connection *conn)
516{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200517 struct h2c *h2c = conn->mux_ctx;
518
519 LIST_DEL(&conn->list);
520
521 if (h2c) {
522 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200523
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100524 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100525 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100526 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200527
Willy Tarreau44e973f2018-03-01 17:49:30 +0100528 h2_release_buf(h2c, &h2c->dbuf);
529 h2_release_buf(h2c, &h2c->mbuf);
530
Willy Tarreauea392822017-10-31 10:02:25 +0100531 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200532 h2c->task->context = NULL;
533 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100534 h2c->task = NULL;
535 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200536 if (h2c->wait_event.task)
537 tasklet_free(h2c->wait_event.task);
538 if (h2c->wait_event.wait_reason != 0)
539 conn->xprt->unsubscribe(conn, h2c->wait_event.wait_reason,
540 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100541
Willy Tarreaubafbe012017-11-24 17:34:44 +0100542 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200543 }
544
545 conn->mux = NULL;
546 conn->mux_ctx = NULL;
547
548 conn_stop_tracking(conn);
549 conn_full_close(conn);
550 if (conn->destroy_cb)
551 conn->destroy_cb(conn);
552 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200553}
554
555
Willy Tarreau71681172017-10-23 14:39:06 +0200556/******************************************************/
557/* functions below are for the H2 protocol processing */
558/******************************************************/
559
560/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100561static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200562{
563 return h2s ? h2s->id : 0;
564}
565
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200566/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100567static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200568{
569 if (h2c->msi < 0)
570 return 0;
571
572 if (h2c->msi == h2s_id(h2s))
573 return 0;
574
575 return 1;
576}
577
Willy Tarreau741d6df2017-10-17 08:00:59 +0200578/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100579static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200580{
581 h2c->errcode = err;
582 h2c->st0 = H2_CS_ERROR;
583}
584
Willy Tarreau2e43f082017-10-17 08:03:59 +0200585/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100586static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200587{
Willy Tarreauab0e1da2018-10-05 10:16:37 +0200588 if (h2s->id && h2s->st < H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200589 h2s->errcode = err;
590 h2s->st = H2_SS_ERROR;
591 if (h2s->cs)
592 h2s->cs->flags |= CS_FL_ERROR;
593 }
594}
595
Willy Tarreaue4820742017-07-27 13:37:23 +0200596/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100597static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200598{
599 uint8_t *out = frame;
600
601 *out = len >> 16;
602 write_n16(out + 1, len);
603}
604
Willy Tarreau54c15062017-10-10 17:10:03 +0200605/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
606 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
607 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200608 * available in the buffer's input prior to calling this function. The buffer
609 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200610 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100611static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200612 const struct buffer *b, int o)
613{
Willy Tarreau591d4452018-06-15 17:21:00 +0200614 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 +0200615}
616
Willy Tarreau1f094672017-11-20 21:27:45 +0100617static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200618{
Willy Tarreau591d4452018-06-15 17:21:00 +0200619 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200620}
621
Willy Tarreau1f094672017-11-20 21:27:45 +0100622static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200623{
Willy Tarreau591d4452018-06-15 17:21:00 +0200624 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200625}
626
Willy Tarreau1f094672017-11-20 21:27:45 +0100627static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200628{
Willy Tarreau591d4452018-06-15 17:21:00 +0200629 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200630}
631
632
Willy Tarreau715d5312017-07-11 15:20:24 +0200633/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
634 * is not obvious. It turns out that H2 headers are neither aligned nor do they
635 * use regular sizes. And to add to the trouble, the buffer may wrap so each
636 * byte read must be checked. The header is formed like this :
637 *
638 * b0 b1 b2 b3 b4 b5..b8
639 * +----------+---------+--------+----+----+----------------------+
640 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
641 * +----------+---------+--------+----+----+----------------------+
642 *
643 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
644 * we get the sid properly aligned and ordered, and 16 bits of len properly
645 * ordered as well. The type and flags can be extracted using bit shifts from
646 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200647 * Returns zero if some bytes are missing, otherwise non-zero on success. The
648 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200649 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100650static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200651{
652 uint64_t w;
653
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200654 if (b_data(b) < 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200655 return 0;
656
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200657 w = h2_get_n64(b, 1);
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200658 h->len = *(uint8_t*)b_head(b) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200659 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
660 h->ff = w >> 32;
661 h->ft = w >> 40;
662 h->len += w >> 48;
663 return 1;
664}
665
666/* skip the next 9 bytes corresponding to the frame header possibly parsed by
667 * h2_peek_frame_hdr() above.
668 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100669static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200670{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200671 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200672}
673
674/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100675static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200676{
677 int ret;
678
679 ret = h2_peek_frame_hdr(b, h);
680 if (ret > 0)
681 h2_skip_frame_hdr(b);
682 return ret;
683}
684
Willy Tarreau00dd0782018-03-01 16:31:34 +0100685/* marks stream <h2s> as CLOSED and decrement the number of active streams for
686 * its connection if the stream was not yet closed. Please use this exclusively
687 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100688 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100689static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100690{
691 if (h2s->st != H2_SS_CLOSED)
692 h2s->h2c->nb_streams--;
693 h2s->st = H2_SS_CLOSED;
694}
695
Willy Tarreau71049cc2018-03-28 13:56:39 +0200696/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
697static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100698{
699 h2s_close(h2s);
700 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200701 if (b_size(&h2s->rxbuf)) {
702 b_free(&h2s->rxbuf);
703 offer_buffers(NULL, tasks_run_queue);
704 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200705 if (h2s->send_wait != NULL)
706 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
707 if (h2s->recv_wait != NULL)
708 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800709 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200710 * reference left would be in the h2c send_list/fctl_list, and if
711 * we're in it, we're getting out anyway
712 */
713 LIST_DEL(&h2s->list);
714 LIST_INIT(&h2s->list);
715 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100716 pool_free(pool_head_h2s, h2s);
717}
718
Willy Tarreaua8e49542018-10-03 18:53:55 +0200719/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
720 * stream tree. In case of error, nothing is added and NULL is returned. The
721 * causes of errors can be any failed memory allocation. The caller is
722 * responsible for checking if the connection may support an extra stream
723 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200724 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200725static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200726{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200727 struct h2s *h2s;
728
Willy Tarreaubafbe012017-11-24 17:34:44 +0100729 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200730 if (!h2s)
731 goto out;
732
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200733 h2s->wait_event.task = tasklet_new();
734 if (!h2s->wait_event.task) {
735 pool_free(pool_head_h2s, h2s);
736 goto out;
737 }
738 h2s->send_wait = NULL;
739 h2s->recv_wait = NULL;
740 h2s->wait_event.task->process = h2_deferred_shut;
741 h2s->wait_event.task->context = h2s;
742 h2s->wait_event.handle = NULL;
743 h2s->wait_event.wait_reason = 0;
744 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200745 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200746 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200747 h2s->mws = h2c->miw;
748 h2s->flags = H2_SF_NONE;
749 h2s->errcode = H2_ERR_NO_ERROR;
750 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200751 h2s->status = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200752 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200753
754 if (h2c->flags & H2_CF_IS_BACK) {
755 h1m_init_req(&h2s->h1m);
756 h2s->h1m.err_pos = -1; // don't care about errors on the request path
757 h2s->h1m.flags |= H1_MF_TOLOWER;
758 } else {
759 h1m_init_res(&h2s->h1m);
760 h2s->h1m.err_pos = -1; // don't care about errors on the response path
761 h2s->h1m.flags |= H1_MF_TOLOWER;
762 }
763
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200764 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200765 if (id > 0)
766 h2c->max_id = id;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200767
768 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100769 h2c->nb_streams++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200770
771 return h2s;
772
773 out_free_h2s:
774 pool_free(pool_head_h2s, h2s);
775 out:
776 return NULL;
777}
778
779/* creates a new stream <id> on the h2c connection and returns it, or NULL in
780 * case of memory allocation error.
781 */
782static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
783{
784 struct session *sess = h2c->conn->owner;
785 struct conn_stream *cs;
786 struct h2s *h2s;
787
788 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
789 goto out;
790
791 h2s = h2s_new(h2c, id);
792 if (!h2s)
793 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200794
795 cs = cs_new(h2c->conn);
796 if (!cs)
797 goto out_close;
798
Olivier Houchard746fb772018-12-15 19:42:00 +0100799 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200800 h2s->cs = cs;
801 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200802 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200803
804 if (stream_create_from_cs(cs) < 0)
805 goto out_free_cs;
806
Willy Tarreau590a0512018-09-05 11:56:48 +0200807 /* We want the accept date presented to the next stream to be the one
808 * we have now, the handshake time to be null (since the next stream
809 * is not delayed by a handshake), and the idle time to count since
810 * right now.
811 */
812 sess->accept_date = date;
813 sess->tv_accept = now;
814 sess->t_handshake = 0;
815
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200816 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200817 if (h2_has_too_many_cs(h2c))
818 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200819 return h2s;
820
821 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200822 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200823 cs_free(cs);
824 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200825 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200826 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200827 sess_log(sess);
828 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200829}
830
Willy Tarreau751f2d02018-10-05 09:35:00 +0200831/* allocates a new stream associated to conn_stream <cs> on the h2c connection
832 * and returns it, or NULL in case of memory allocation error or if the highest
833 * possible stream ID was reached.
834 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100835static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +0200836{
837 struct h2s *h2s = NULL;
838
839 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
840 goto out;
841
842 /* Defer choosing the ID until we send the first message to create the stream */
843 h2s = h2s_new(h2c, 0);
844 if (!h2s)
845 goto out;
846
847 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100848 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200849 cs->ctx = h2s;
850 h2c->nb_cs++;
851
Willy Tarreau751f2d02018-10-05 09:35:00 +0200852 out:
853 return h2s;
854}
855
Willy Tarreaube5b7152017-09-25 16:25:39 +0200856/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
857 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
858 * the various settings codes.
859 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200860static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +0200861{
862 struct buffer *res;
863 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200864 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200865 int ret;
866
867 if (h2c_mux_busy(h2c, NULL)) {
868 h2c->flags |= H2_CF_DEM_MBUSY;
869 return 0;
870 }
871
Willy Tarreau44e973f2018-03-01 17:49:30 +0100872 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200873 if (!res) {
874 h2c->flags |= H2_CF_MUX_MALLOC;
875 h2c->flags |= H2_CF_DEM_MROOM;
876 return 0;
877 }
878
879 chunk_init(&buf, buf_data, sizeof(buf_data));
880 chunk_memcpy(&buf,
881 "\x00\x00\x00" /* length : 0 for now */
882 "\x04\x00" /* type : 4 (settings), flags : 0 */
883 "\x00\x00\x00\x00", /* stream ID : 0 */
884 9);
885
886 if (h2_settings_header_table_size != 4096) {
887 char str[6] = "\x00\x01"; /* header_table_size */
888
889 write_n32(str + 2, h2_settings_header_table_size);
890 chunk_memcat(&buf, str, 6);
891 }
892
893 if (h2_settings_initial_window_size != 65535) {
894 char str[6] = "\x00\x04"; /* initial_window_size */
895
896 write_n32(str + 2, h2_settings_initial_window_size);
897 chunk_memcat(&buf, str, 6);
898 }
899
900 if (h2_settings_max_concurrent_streams != 0) {
901 char str[6] = "\x00\x03"; /* max_concurrent_streams */
902
903 /* Note: 0 means "unlimited" for haproxy's config but not for
904 * the protocol, so never send this value!
905 */
906 write_n32(str + 2, h2_settings_max_concurrent_streams);
907 chunk_memcat(&buf, str, 6);
908 }
909
910 if (global.tune.bufsize != 16384) {
911 char str[6] = "\x00\x05"; /* max_frame_size */
912
913 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
914 * match bufsize - rewrite size, but at the moment it seems
915 * that clients don't take care of it.
916 */
917 write_n32(str + 2, global.tune.bufsize);
918 chunk_memcat(&buf, str, 6);
919 }
920
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200921 h2_set_frame_size(buf.area, buf.data - 9);
922 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200923 if (unlikely(ret <= 0)) {
924 if (!ret) {
925 h2c->flags |= H2_CF_MUX_MFULL;
926 h2c->flags |= H2_CF_DEM_MROOM;
927 return 0;
928 }
929 else {
930 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
931 return 0;
932 }
933 }
934 return ret;
935}
936
Willy Tarreau52eed752017-09-22 15:05:09 +0200937/* Try to receive a connection preface, then upon success try to send our
938 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
939 * missing data. It may return an error in h2c.
940 */
941static int h2c_frt_recv_preface(struct h2c *h2c)
942{
943 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200944 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200945
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200946 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200947
948 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +0200949 if (ret1 < 0)
950 sess_log(h2c->conn->owner);
951
Willy Tarreau52eed752017-09-22 15:05:09 +0200952 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
953 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
954 return 0;
955 }
956
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200957 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200958 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200959 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200960
Willy Tarreaube5b7152017-09-25 16:25:39 +0200961 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200962}
963
Willy Tarreau01b44822018-10-03 14:26:37 +0200964/* Try to send a connection preface, then upon success try to send our
965 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
966 * missing data. It may return an error in h2c.
967 */
968static int h2c_bck_send_preface(struct h2c *h2c)
969{
970 struct buffer *res;
971
972 if (h2c_mux_busy(h2c, NULL)) {
973 h2c->flags |= H2_CF_DEM_MBUSY;
974 return 0;
975 }
976
977 res = h2_get_buf(h2c, &h2c->mbuf);
978 if (!res) {
979 h2c->flags |= H2_CF_MUX_MALLOC;
980 h2c->flags |= H2_CF_DEM_MROOM;
981 return 0;
982 }
983
984 if (!b_data(res)) {
985 /* preface not yet sent */
986 b_istput(res, ist(H2_CONN_PREFACE));
987 }
988
989 return h2c_send_settings(h2c);
990}
991
Willy Tarreau081d4722017-05-16 21:51:05 +0200992/* try to send a GOAWAY frame on the connection to report an error or a graceful
993 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
994 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
995 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
996 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
997 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
998 * on unrecoverable failure. It will not attempt to send one again in this last
999 * case so that it is safe to use h2c_error() to report such errors.
1000 */
1001static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1002{
1003 struct buffer *res;
1004 char str[17];
1005 int ret;
1006
1007 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1008 return 1; // claim that it worked
1009
1010 if (h2c_mux_busy(h2c, h2s)) {
1011 if (h2s)
1012 h2s->flags |= H2_SF_BLK_MBUSY;
1013 else
1014 h2c->flags |= H2_CF_DEM_MBUSY;
1015 return 0;
1016 }
1017
Willy Tarreau44e973f2018-03-01 17:49:30 +01001018 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001019 if (!res) {
1020 h2c->flags |= H2_CF_MUX_MALLOC;
1021 if (h2s)
1022 h2s->flags |= H2_SF_BLK_MROOM;
1023 else
1024 h2c->flags |= H2_CF_DEM_MROOM;
1025 return 0;
1026 }
1027
1028 /* len: 8, type: 7, flags: none, sid: 0 */
1029 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1030
1031 if (h2c->last_sid < 0)
1032 h2c->last_sid = h2c->max_id;
1033
1034 write_n32(str + 9, h2c->last_sid);
1035 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001036 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001037 if (unlikely(ret <= 0)) {
1038 if (!ret) {
1039 h2c->flags |= H2_CF_MUX_MFULL;
1040 if (h2s)
1041 h2s->flags |= H2_SF_BLK_MROOM;
1042 else
1043 h2c->flags |= H2_CF_DEM_MROOM;
1044 return 0;
1045 }
1046 else {
1047 /* we cannot report this error using GOAWAY, so we mark
1048 * it and claim a success.
1049 */
1050 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1051 h2c->flags |= H2_CF_GOAWAY_FAILED;
1052 return 1;
1053 }
1054 }
1055 h2c->flags |= H2_CF_GOAWAY_SENT;
1056 return ret;
1057}
1058
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001059/* Try to send an RST_STREAM frame on the connection for the indicated stream
1060 * during mux operations. This stream must be valid and cannot be closed
1061 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1062 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1063 * not yet.
1064 *
1065 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1066 * to write the message, it subscribes the stream to future notifications.
1067 */
1068static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1069{
1070 struct buffer *res;
1071 char str[13];
1072 int ret;
1073
1074 if (!h2s || h2s->st == H2_SS_CLOSED)
1075 return 1;
1076
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001077 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1078 * RST_STREAM in response to a RST_STREAM frame.
1079 */
1080 if (h2c->dft == H2_FT_RST_STREAM) {
1081 ret = 1;
1082 goto ignore;
1083 }
1084
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001085 if (h2c_mux_busy(h2c, h2s)) {
1086 h2s->flags |= H2_SF_BLK_MBUSY;
1087 return 0;
1088 }
1089
Willy Tarreau44e973f2018-03-01 17:49:30 +01001090 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001091 if (!res) {
1092 h2c->flags |= H2_CF_MUX_MALLOC;
1093 h2s->flags |= H2_SF_BLK_MROOM;
1094 return 0;
1095 }
1096
1097 /* len: 4, type: 3, flags: none */
1098 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1099 write_n32(str + 5, h2s->id);
1100 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001101 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001102
1103 if (unlikely(ret <= 0)) {
1104 if (!ret) {
1105 h2c->flags |= H2_CF_MUX_MFULL;
1106 h2s->flags |= H2_SF_BLK_MROOM;
1107 return 0;
1108 }
1109 else {
1110 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1111 return 0;
1112 }
1113 }
1114
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001115 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001116 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001117 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001118 return ret;
1119}
1120
1121/* Try to send an RST_STREAM frame on the connection for the stream being
1122 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
1123 * error code unless the stream's state already is IDLE or CLOSED in which
1124 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
1125 * it was not yet.
1126 *
1127 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1128 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001129 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001130 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001131 */
1132static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1133{
1134 struct buffer *res;
1135 char str[13];
1136 int ret;
1137
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001138 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1139 * RST_STREAM in response to a RST_STREAM frame.
1140 */
1141 if (h2c->dft == H2_FT_RST_STREAM) {
1142 ret = 1;
1143 goto ignore;
1144 }
1145
Willy Tarreau27a84c92017-10-17 08:10:17 +02001146 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001147 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001148 return 0;
1149 }
1150
Willy Tarreau44e973f2018-03-01 17:49:30 +01001151 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001152 if (!res) {
1153 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001154 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001155 return 0;
1156 }
1157
1158 /* len: 4, type: 3, flags: none */
1159 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001160
Willy Tarreau27a84c92017-10-17 08:10:17 +02001161 write_n32(str + 5, h2c->dsi);
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001162 write_n32(str + 9, h2s->id ? h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001163 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001164
Willy Tarreau27a84c92017-10-17 08:10:17 +02001165 if (unlikely(ret <= 0)) {
1166 if (!ret) {
1167 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001168 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001169 return 0;
1170 }
1171 else {
1172 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1173 return 0;
1174 }
1175 }
1176
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001177 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001178 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001179 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001180 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001181 }
1182
Willy Tarreau27a84c92017-10-17 08:10:17 +02001183 return ret;
1184}
1185
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001186/* try to send an empty DATA frame with the ES flag set to notify about the
1187 * end of stream and match a shutdown(write). If an ES was already sent as
1188 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1189 * on success or zero if nothing was done. In case of lack of room to write the
1190 * message, it subscribes the requesting stream to future notifications.
1191 */
1192static int h2_send_empty_data_es(struct h2s *h2s)
1193{
1194 struct h2c *h2c = h2s->h2c;
1195 struct buffer *res;
1196 char str[9];
1197 int ret;
1198
Willy Tarreau721c9742017-11-07 11:05:42 +01001199 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001200 return 1;
1201
1202 if (h2c_mux_busy(h2c, h2s)) {
1203 h2s->flags |= H2_SF_BLK_MBUSY;
1204 return 0;
1205 }
1206
Willy Tarreau44e973f2018-03-01 17:49:30 +01001207 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001208 if (!res) {
1209 h2c->flags |= H2_CF_MUX_MALLOC;
1210 h2s->flags |= H2_SF_BLK_MROOM;
1211 return 0;
1212 }
1213
1214 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1215 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1216 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001217 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001218 if (likely(ret > 0)) {
1219 h2s->flags |= H2_SF_ES_SENT;
1220 }
1221 else if (!ret) {
1222 h2c->flags |= H2_CF_MUX_MFULL;
1223 h2s->flags |= H2_SF_BLK_MROOM;
1224 return 0;
1225 }
1226 else {
1227 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1228 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001229 }
1230 return ret;
1231}
1232
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001233/* wake the streams attached to the connection, whose id is greater than <last>,
1234 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001235 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1236 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001237 */
1238static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1239{
1240 struct eb32_node *node;
1241 struct h2s *h2s;
1242
1243 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1244 flags |= CS_FL_ERROR;
1245
1246 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001247 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001248
1249 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1250 while (node) {
1251 h2s = container_of(node, struct h2s, by_id);
1252 if (h2s->id <= last)
1253 break;
1254 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001255
1256 if (!h2s->cs) {
1257 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001258 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001259 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001260 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001261
1262 h2s->cs->flags |= flags;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001263 if (h2s->recv_wait) {
1264 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001265 sw->wait_reason &= ~SUB_CAN_RECV;
1266 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001267 h2s->recv_wait = NULL;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02001268 } else if (h2s->cs->data_cb->wake != NULL)
1269 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001270
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001271 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1272 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001273 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001274 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001275 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001276 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001277 }
1278}
1279
Willy Tarreau3421aba2017-07-27 15:41:03 +02001280/* Increase all streams' outgoing window size by the difference passed in
1281 * argument. This is needed upon receipt of the settings frame if the initial
1282 * window size is different. The difference may be negative and the resulting
1283 * window size as well, for the time it takes to receive some window updates.
1284 */
1285static void h2c_update_all_ws(struct h2c *h2c, int diff)
1286{
1287 struct h2s *h2s;
1288 struct eb32_node *node;
1289
1290 if (!diff)
1291 return;
1292
1293 node = eb32_first(&h2c->streams_by_id);
1294 while (node) {
1295 h2s = container_of(node, struct h2s, by_id);
1296 h2s->mws += diff;
1297 node = eb32_next(node);
1298 }
1299}
1300
1301/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1302 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1303 * return an error in h2c. Described in RFC7540#6.5.
1304 */
1305static int h2c_handle_settings(struct h2c *h2c)
1306{
1307 unsigned int offset;
1308 int error;
1309
1310 if (h2c->dff & H2_F_SETTINGS_ACK) {
1311 if (h2c->dfl) {
1312 error = H2_ERR_FRAME_SIZE_ERROR;
1313 goto fail;
1314 }
1315 return 1;
1316 }
1317
1318 if (h2c->dsi != 0) {
1319 error = H2_ERR_PROTOCOL_ERROR;
1320 goto fail;
1321 }
1322
1323 if (h2c->dfl % 6) {
1324 error = H2_ERR_FRAME_SIZE_ERROR;
1325 goto fail;
1326 }
1327
1328 /* that's the limit we can process */
1329 if (h2c->dfl > global.tune.bufsize) {
1330 error = H2_ERR_FRAME_SIZE_ERROR;
1331 goto fail;
1332 }
1333
1334 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001335 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001336 return 0;
1337
1338 /* parse the frame */
1339 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001340 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1341 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001342
1343 switch (type) {
1344 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1345 /* we need to update all existing streams with the
1346 * difference from the previous iws.
1347 */
1348 if (arg < 0) { // RFC7540#6.5.2
1349 error = H2_ERR_FLOW_CONTROL_ERROR;
1350 goto fail;
1351 }
1352 h2c_update_all_ws(h2c, arg - h2c->miw);
1353 h2c->miw = arg;
1354 break;
1355 case H2_SETTINGS_MAX_FRAME_SIZE:
1356 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1357 error = H2_ERR_PROTOCOL_ERROR;
1358 goto fail;
1359 }
1360 h2c->mfs = arg;
1361 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001362 case H2_SETTINGS_ENABLE_PUSH:
1363 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1364 error = H2_ERR_PROTOCOL_ERROR;
1365 goto fail;
1366 }
1367 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001368 }
1369 }
1370
1371 /* need to ACK this frame now */
1372 h2c->st0 = H2_CS_FRAME_A;
1373 return 1;
1374 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001375 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001376 h2c_error(h2c, error);
1377 return 0;
1378}
1379
1380/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1381 * success or one of the h2_status values.
1382 */
1383static int h2c_ack_settings(struct h2c *h2c)
1384{
1385 struct buffer *res;
1386 char str[9];
1387 int ret = -1;
1388
1389 if (h2c_mux_busy(h2c, NULL)) {
1390 h2c->flags |= H2_CF_DEM_MBUSY;
1391 return 0;
1392 }
1393
Willy Tarreau44e973f2018-03-01 17:49:30 +01001394 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001395 if (!res) {
1396 h2c->flags |= H2_CF_MUX_MALLOC;
1397 h2c->flags |= H2_CF_DEM_MROOM;
1398 return 0;
1399 }
1400
1401 memcpy(str,
1402 "\x00\x00\x00" /* length : 0 (no data) */
1403 "\x04" "\x01" /* type : 4, flags : ACK */
1404 "\x00\x00\x00\x00" /* stream ID */, 9);
1405
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001406 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001407 if (unlikely(ret <= 0)) {
1408 if (!ret) {
1409 h2c->flags |= H2_CF_MUX_MFULL;
1410 h2c->flags |= H2_CF_DEM_MROOM;
1411 return 0;
1412 }
1413 else {
1414 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1415 return 0;
1416 }
1417 }
1418 return ret;
1419}
1420
Willy Tarreaucf68c782017-10-10 17:11:41 +02001421/* processes a PING frame and schedules an ACK if needed. The caller must pass
1422 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1423 * missing data. It may return an error in h2c.
1424 */
1425static int h2c_handle_ping(struct h2c *h2c)
1426{
1427 /* frame length must be exactly 8 */
1428 if (h2c->dfl != 8) {
1429 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1430 return 0;
1431 }
1432
1433 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001434 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001435 h2c->st0 = H2_CS_FRAME_A;
1436 return 1;
1437}
1438
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001439/* Try to send a window update for stream id <sid> and value <increment>.
1440 * Returns > 0 on success or zero on missing room or failure. It may return an
1441 * error in h2c.
1442 */
1443static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1444{
1445 struct buffer *res;
1446 char str[13];
1447 int ret = -1;
1448
1449 if (h2c_mux_busy(h2c, NULL)) {
1450 h2c->flags |= H2_CF_DEM_MBUSY;
1451 return 0;
1452 }
1453
Willy Tarreau44e973f2018-03-01 17:49:30 +01001454 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001455 if (!res) {
1456 h2c->flags |= H2_CF_MUX_MALLOC;
1457 h2c->flags |= H2_CF_DEM_MROOM;
1458 return 0;
1459 }
1460
1461 /* length: 4, type: 8, flags: none */
1462 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1463 write_n32(str + 5, sid);
1464 write_n32(str + 9, increment);
1465
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001466 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001467
1468 if (unlikely(ret <= 0)) {
1469 if (!ret) {
1470 h2c->flags |= H2_CF_MUX_MFULL;
1471 h2c->flags |= H2_CF_DEM_MROOM;
1472 return 0;
1473 }
1474 else {
1475 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1476 return 0;
1477 }
1478 }
1479 return ret;
1480}
1481
1482/* try to send pending window update for the connection. It's safe to call it
1483 * with no pending updates. Returns > 0 on success or zero on missing room or
1484 * failure. It may return an error in h2c.
1485 */
1486static int h2c_send_conn_wu(struct h2c *h2c)
1487{
1488 int ret = 1;
1489
1490 if (h2c->rcvd_c <= 0)
1491 return 1;
1492
1493 /* send WU for the connection */
1494 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1495 if (ret > 0)
1496 h2c->rcvd_c = 0;
1497
1498 return ret;
1499}
1500
1501/* try to send pending window update for the current dmux stream. It's safe to
1502 * call it with no pending updates. Returns > 0 on success or zero on missing
1503 * room or failure. It may return an error in h2c.
1504 */
1505static int h2c_send_strm_wu(struct h2c *h2c)
1506{
1507 int ret = 1;
1508
1509 if (h2c->rcvd_s <= 0)
1510 return 1;
1511
1512 /* send WU for the stream */
1513 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1514 if (ret > 0)
1515 h2c->rcvd_s = 0;
1516
1517 return ret;
1518}
1519
Willy Tarreaucf68c782017-10-10 17:11:41 +02001520/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1521 * success, 0 on missing data or one of the h2_status values.
1522 */
1523static int h2c_ack_ping(struct h2c *h2c)
1524{
1525 struct buffer *res;
1526 char str[17];
1527 int ret = -1;
1528
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001529 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001530 return 0;
1531
1532 if (h2c_mux_busy(h2c, NULL)) {
1533 h2c->flags |= H2_CF_DEM_MBUSY;
1534 return 0;
1535 }
1536
Willy Tarreau44e973f2018-03-01 17:49:30 +01001537 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001538 if (!res) {
1539 h2c->flags |= H2_CF_MUX_MALLOC;
1540 h2c->flags |= H2_CF_DEM_MROOM;
1541 return 0;
1542 }
1543
1544 memcpy(str,
1545 "\x00\x00\x08" /* length : 8 (same payload) */
1546 "\x06" "\x01" /* type : 6, flags : ACK */
1547 "\x00\x00\x00\x00" /* stream ID */, 9);
1548
1549 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001550 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001551
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001552 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001553 if (unlikely(ret <= 0)) {
1554 if (!ret) {
1555 h2c->flags |= H2_CF_MUX_MFULL;
1556 h2c->flags |= H2_CF_DEM_MROOM;
1557 return 0;
1558 }
1559 else {
1560 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1561 return 0;
1562 }
1563 }
1564 return ret;
1565}
1566
Willy Tarreau26f95952017-07-27 17:18:30 +02001567/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1568 * Returns > 0 on success or zero on missing data. It may return an error in
1569 * h2c or h2s. Described in RFC7540#6.9.
1570 */
1571static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1572{
1573 int32_t inc;
1574 int error;
1575
1576 if (h2c->dfl != 4) {
1577 error = H2_ERR_FRAME_SIZE_ERROR;
1578 goto conn_err;
1579 }
1580
1581 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001582 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001583 return 0;
1584
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001585 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001586
1587 if (h2c->dsi != 0) {
1588 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001589
1590 /* it's not an error to receive WU on a closed stream */
1591 if (h2s->st == H2_SS_CLOSED)
1592 return 1;
1593
1594 if (!inc) {
1595 error = H2_ERR_PROTOCOL_ERROR;
1596 goto strm_err;
1597 }
1598
1599 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1600 error = H2_ERR_FLOW_CONTROL_ERROR;
1601 goto strm_err;
1602 }
1603
1604 h2s->mws += inc;
1605 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1606 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001607 if (h2s->send_wait)
1608 LIST_ADDQ(&h2c->send_list, &h2s->list);
1609
Willy Tarreau26f95952017-07-27 17:18:30 +02001610 }
1611 }
1612 else {
1613 /* connection window update */
1614 if (!inc) {
1615 error = H2_ERR_PROTOCOL_ERROR;
1616 goto conn_err;
1617 }
1618
1619 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1620 error = H2_ERR_FLOW_CONTROL_ERROR;
1621 goto conn_err;
1622 }
1623
1624 h2c->mws += inc;
1625 }
1626
1627 return 1;
1628
1629 conn_err:
1630 h2c_error(h2c, error);
1631 return 0;
1632
1633 strm_err:
1634 if (h2s) {
1635 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001636 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001637 }
1638 else
1639 h2c_error(h2c, error);
1640 return 0;
1641}
1642
Willy Tarreaue96b0922017-10-30 00:28:29 +01001643/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1644 * the last ID. Returns > 0 on success or zero on missing data. It may return
1645 * an error in h2c. Described in RFC7540#6.8.
1646 */
1647static int h2c_handle_goaway(struct h2c *h2c)
1648{
1649 int error;
1650 int last;
1651
1652 if (h2c->dsi != 0) {
1653 error = H2_ERR_PROTOCOL_ERROR;
1654 goto conn_err;
1655 }
1656
1657 if (h2c->dfl < 8) {
1658 error = H2_ERR_FRAME_SIZE_ERROR;
1659 goto conn_err;
1660 }
1661
1662 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001663 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001664 return 0;
1665
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001666 last = h2_get_n32(&h2c->dbuf, 0);
1667 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001668 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001669 if (h2c->last_sid < 0)
1670 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001671 return 1;
1672
1673 conn_err:
1674 h2c_error(h2c, error);
1675 return 0;
1676}
1677
Willy Tarreau92153fc2017-12-03 19:46:19 +01001678/* processes a PRIORITY frame, and either skips it or rejects if it is
1679 * invalid. Returns > 0 on success or zero on missing data. It may return
1680 * an error in h2c. Described in RFC7540#6.3.
1681 */
1682static int h2c_handle_priority(struct h2c *h2c)
1683{
1684 int error;
1685
1686 if (h2c->dsi == 0) {
1687 error = H2_ERR_PROTOCOL_ERROR;
1688 goto conn_err;
1689 }
1690
1691 if (h2c->dfl != 5) {
1692 error = H2_ERR_FRAME_SIZE_ERROR;
1693 goto conn_err;
1694 }
1695
1696 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001697 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001698 return 0;
1699
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001700 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001701 /* 7540#5.3 : can't depend on itself */
1702 error = H2_ERR_PROTOCOL_ERROR;
1703 goto conn_err;
1704 }
1705 return 1;
1706
1707 conn_err:
1708 h2c_error(h2c, error);
1709 return 0;
1710}
1711
Willy Tarreaucd234e92017-08-18 10:59:39 +02001712/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1713 * Returns > 0 on success or zero on missing data. It may return an error in
1714 * h2c. Described in RFC7540#6.4.
1715 */
1716static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1717{
1718 int error;
1719
1720 if (h2c->dsi == 0) {
1721 error = H2_ERR_PROTOCOL_ERROR;
1722 goto conn_err;
1723 }
1724
Willy Tarreaucd234e92017-08-18 10:59:39 +02001725 if (h2c->dfl != 4) {
1726 error = H2_ERR_FRAME_SIZE_ERROR;
1727 goto conn_err;
1728 }
1729
1730 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001731 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001732 return 0;
1733
1734 /* late RST, already handled */
1735 if (h2s->st == H2_SS_CLOSED)
1736 return 1;
1737
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001738 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001739 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001740
1741 if (h2s->cs) {
Olivier Houchard71748cb2018-12-17 14:16:46 +01001742 h2s->cs->flags |= CS_FL_REOS | CS_FL_ERR_PENDING;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001743 if (h2s->recv_wait) {
1744 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001745
1746 sw->wait_reason &= ~SUB_CAN_RECV;
1747 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001748 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001749 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02001750 }
1751
1752 h2s->flags |= H2_SF_RST_RCVD;
1753 return 1;
1754
1755 conn_err:
1756 h2c_error(h2c, error);
1757 return 0;
1758}
1759
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001760/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1761 * It may return an error in h2c or h2s. The caller must consider that the
1762 * return value is the new h2s in case one was allocated (most common case).
1763 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001764 * errors here are reported as connection errors since it's impossible to
1765 * recover from such errors after the compression context has been altered.
1766 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001767static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001768{
1769 int error;
1770
1771 if (!h2c->dfl) {
1772 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001773 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001774 goto strm_err;
1775 }
1776
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001777 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001778 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001779
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001780 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001781 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001782
Willy Tarreauf2101912018-07-19 10:11:38 +02001783 if (h2c->flags & H2_CF_DEM_TOOMANY)
1784 return 0; // too many cs still present
1785
Willy Tarreau13278b42017-10-13 19:23:14 +02001786 /* now either the frame is complete or the buffer is complete */
1787 if (h2s->st != H2_SS_IDLE) {
1788 /* FIXME: stream already exists, this is only allowed for
1789 * trailers (not supported for now).
1790 */
1791 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001792 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001793 goto conn_err;
1794 }
1795 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1796 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1797 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001798 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001799 goto conn_err;
1800 }
1801
Willy Tarreau22de8d32018-09-05 19:55:58 +02001802 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001803 * positively from h2c_frt_stream_new(), the stream will report the error,
1804 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001805 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001806 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001807 if (!h2s) {
1808 error = H2_ERR_INTERNAL_ERROR;
1809 goto conn_err;
1810 }
1811
1812 h2s->st = H2_SS_OPEN;
1813 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1814 h2s->st = H2_SS_HREM;
1815 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001816 /* note: cs cannot be null for now (just created above) */
1817 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001818 }
1819
Willy Tarreauc3e18f32018-10-08 14:51:56 +02001820 if (!h2s_decode_headers(h2s))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001821 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001822
Willy Tarreau8f650c32017-11-21 19:36:21 +01001823 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001824 return NULL;
Willy Tarreau8f650c32017-11-21 19:36:21 +01001825
Willy Tarreau721c9742017-11-07 11:05:42 +01001826 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001827 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001828 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001829 }
1830 else {
1831 /* update the max stream ID if the request is being processed */
1832 if (h2s->id > h2c->max_id)
1833 h2c->max_id = h2s->id;
1834 }
1835
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001836 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001837
1838 conn_err:
1839 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001840 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001841
1842 strm_err:
1843 if (h2s) {
1844 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001845 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001846 }
1847 else
1848 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001849 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001850}
1851
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001852/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1853 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1854 * errors here are reported as connection errors since it's impossible to
1855 * recover from such errors after the compression context has been altered.
1856 */
1857static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
1858{
1859 int error;
1860
1861 if (!h2c->dfl) {
1862 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1863 sess_log(h2c->conn->owner);
1864 goto strm_err;
1865 }
1866
1867 if (!b_size(&h2c->dbuf))
1868 return NULL; // empty buffer
1869
1870 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
1871 return NULL; // incomplete frame
1872
1873 if (h2c->flags & H2_CF_DEM_TOOMANY)
1874 return 0; // too many cs still present
1875
1876 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1877 h2s->flags |= H2_SF_ES_RCVD;
1878 h2s->cs->flags |= CS_FL_REOS;
1879 }
1880
1881 if (!h2s_decode_headers(h2s))
1882 return NULL;
1883
1884 if (h2c->st0 >= H2_CS_ERROR)
1885 return NULL;
1886
1887 if (h2s->st >= H2_SS_ERROR) {
1888 /* stream error : send RST_STREAM */
1889 h2c->st0 = H2_CS_FRAME_E;
1890 }
1891
1892 if (h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1893 h2s->st = H2_SS_ERROR;
1894 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
1895 h2s->st = H2_SS_HREM;
1896 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
1897 h2s_close(h2s);
1898
1899 return h2s;
1900
1901 conn_err:
1902 h2c_error(h2c, error);
1903 return NULL;
1904
1905 strm_err:
1906 if (h2s) {
1907 h2s_error(h2s, error);
1908 h2c->st0 = H2_CS_FRAME_E;
1909 }
1910 else
1911 h2c_error(h2c, error);
1912 return NULL;
1913}
1914
Willy Tarreau454f9052017-10-26 19:40:35 +02001915/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1916 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1917 */
1918static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1919{
1920 int error;
1921
1922 /* note that empty DATA frames are perfectly valid and sometimes used
1923 * to signal an end of stream (with the ES flag).
1924 */
1925
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001926 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001927 return 0; // empty buffer
1928
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001929 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001930 return 0; // incomplete frame
1931
1932 /* now either the frame is complete or the buffer is complete */
1933
1934 if (!h2c->dsi) {
1935 /* RFC7540#6.1 */
1936 error = H2_ERR_PROTOCOL_ERROR;
1937 goto conn_err;
1938 }
1939
1940 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1941 /* RFC7540#6.1 */
1942 error = H2_ERR_STREAM_CLOSED;
1943 goto strm_err;
1944 }
1945
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001946 if (!h2_frt_transfer_data(h2s))
1947 return 0;
1948
Willy Tarreau454f9052017-10-26 19:40:35 +02001949 /* call the upper layers to process the frame, then let the upper layer
1950 * notify the stream about any change.
1951 */
1952 if (!h2s->cs) {
1953 error = H2_ERR_STREAM_CLOSED;
1954 goto strm_err;
1955 }
1956
Willy Tarreau8f650c32017-11-21 19:36:21 +01001957 if (h2c->st0 >= H2_CS_ERROR)
1958 return 0;
1959
Willy Tarreau721c9742017-11-07 11:05:42 +01001960 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001961 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001962 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001963 }
1964
1965 /* check for completion : the callee will change this to FRAME_A or
1966 * FRAME_H once done.
1967 */
1968 if (h2c->st0 == H2_CS_FRAME_P)
1969 return 0;
1970
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001971
1972 /* last frame */
1973 if (h2c->dff & H2_F_DATA_END_STREAM) {
1974 h2s->st = H2_SS_HREM;
1975 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001976 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001977 }
1978
Willy Tarreau454f9052017-10-26 19:40:35 +02001979 return 1;
1980
1981 conn_err:
1982 h2c_error(h2c, error);
1983 return 0;
1984
1985 strm_err:
1986 if (h2s) {
1987 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001988 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001989 }
1990 else
1991 h2c_error(h2c, error);
1992 return 0;
1993}
1994
Willy Tarreaubc933932017-10-09 16:21:43 +02001995/* process Rx frames to be demultiplexed */
1996static void h2_process_demux(struct h2c *h2c)
1997{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001998 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001999
Willy Tarreau081d4722017-05-16 21:51:05 +02002000 if (h2c->st0 >= H2_CS_ERROR)
2001 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002002
2003 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2004 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002005 if (h2c->flags & H2_CF_IS_BACK)
2006 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002007 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2008 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002009 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002010 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002011 sess_log(h2c->conn->owner);
2012 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002013 goto fail;
2014 }
2015
2016 h2c->max_id = 0;
2017 h2c->st0 = H2_CS_SETTINGS1;
2018 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002019
2020 if (h2c->st0 == H2_CS_SETTINGS1) {
2021 struct h2_fh hdr;
2022
2023 /* ensure that what is pending is a valid SETTINGS frame
2024 * without an ACK.
2025 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002026 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002027 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002028 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002029 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002030 sess_log(h2c->conn->owner);
2031 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002032 goto fail;
2033 }
2034
2035 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2036 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2037 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2038 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002039 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002040 goto fail;
2041 }
2042
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002043 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002044 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2045 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2046 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002047 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002048 goto fail;
2049 }
2050
2051 /* that's OK, switch to FRAME_P to process it */
2052 h2c->dfl = hdr.len;
2053 h2c->dsi = hdr.sid;
2054 h2c->dft = hdr.ft;
2055 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002056 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002057 h2c->st0 = H2_CS_FRAME_P;
2058 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002059 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002060
2061 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002062 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002063 int ret = 0;
2064
2065 if (h2c->st0 >= H2_CS_ERROR)
2066 break;
2067
2068 if (h2c->st0 == H2_CS_FRAME_H) {
2069 struct h2_fh hdr;
2070
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002071 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002072 break;
2073
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002074 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002075 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2076 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002077 if (!h2c->nb_streams) {
2078 /* only log if no other stream can report the error */
2079 sess_log(h2c->conn->owner);
2080 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002081 break;
2082 }
2083
2084 h2c->dfl = hdr.len;
2085 h2c->dsi = hdr.sid;
2086 h2c->dft = hdr.ft;
2087 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002088 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002089 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002090 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002091 }
2092
2093 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002094 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2095
Olivier Houchard638b7992018-08-16 15:41:52 +02002096 if (tmp_h2s != h2s && h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002097 /* we may have to signal the upper layers */
2098 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002099 if (h2s->recv_wait) {
2100 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
2101 tasklet_wakeup(h2s->recv_wait->task);
2102 h2s->recv_wait = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002103 }
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002104 }
2105 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002106
Willy Tarreaud7901432017-12-29 11:34:40 +01002107 if (h2c->st0 == H2_CS_FRAME_E)
2108 goto strm_err;
2109
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002110 if (h2s->st == H2_SS_IDLE &&
2111 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2112 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2113 * this state MUST be treated as a connection error
2114 */
2115 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2116 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002117 if (!h2c->nb_streams) {
2118 /* only log if no other stream can report the error */
2119 sess_log(h2c->conn->owner);
2120 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002121 break;
2122 }
2123
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002124 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2125 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2126 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
2127 * this state MUST be treated as a stream error
2128 */
2129 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2130 goto strm_err;
2131 }
2132
Willy Tarreauab837502017-12-27 15:07:30 +01002133 /* Below the management of frames received in closed state is a
2134 * bit hackish because the spec makes strong differences between
2135 * streams closed by receiving RST, sending RST, and seeing ES
2136 * in both directions. In addition to this, the creation of a
2137 * new stream reusing the identifier of a closed one will be
2138 * detected here. Given that we cannot keep track of all closed
2139 * streams forever, we consider that unknown closed streams were
2140 * closed on RST received, which allows us to respond with an
2141 * RST without breaking the connection (eg: to abort a transfer).
2142 * Some frames have to be silently ignored as well.
2143 */
2144 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
2145 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
2146 /* #5.1.1: The identifier of a newly
2147 * established stream MUST be numerically
2148 * greater than all streams that the initiating
2149 * endpoint has opened or reserved. This
2150 * governs streams that are opened using a
2151 * HEADERS frame and streams that are reserved
2152 * using PUSH_PROMISE. An endpoint that
2153 * receives an unexpected stream identifier
2154 * MUST respond with a connection error.
2155 */
2156 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2157 goto strm_err;
2158 }
2159
2160 if (h2s->flags & H2_SF_RST_RCVD) {
2161 /* RFC7540#5.1:closed: an endpoint that
2162 * receives any frame other than PRIORITY after
2163 * receiving a RST_STREAM MUST treat that as a
2164 * stream error of type STREAM_CLOSED.
2165 *
2166 * Note that old streams fall into this category
2167 * and will lead to an RST being sent.
2168 */
2169 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2170 h2c->st0 = H2_CS_FRAME_E;
2171 goto strm_err;
2172 }
2173
2174 /* RFC7540#5.1:closed: if this state is reached as a
2175 * result of sending a RST_STREAM frame, the peer that
2176 * receives the RST_STREAM might have already sent
2177 * frames on the stream that cannot be withdrawn. An
2178 * endpoint MUST ignore frames that it receives on
2179 * closed streams after it has sent a RST_STREAM
2180 * frame. An endpoint MAY choose to limit the period
2181 * over which it ignores frames and treat frames that
2182 * arrive after this time as being in error.
2183 */
2184 if (!(h2s->flags & H2_SF_RST_SENT)) {
2185 /* RFC7540#5.1:closed: any frame other than
2186 * PRIO/WU/RST in this state MUST be treated as
2187 * a connection error
2188 */
2189 if (h2c->dft != H2_FT_RST_STREAM &&
2190 h2c->dft != H2_FT_PRIORITY &&
2191 h2c->dft != H2_FT_WINDOW_UPDATE) {
2192 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2193 goto strm_err;
2194 }
2195 }
2196 }
2197
Willy Tarreauc0da1962017-10-30 18:38:00 +01002198#if 0
2199 // problem below: it is not possible to completely ignore such
2200 // streams as we need to maintain the compression state as well
2201 // and for this we need to completely process these frames (eg:
2202 // HEADERS frames) as well as counting DATA frames to emit
2203 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2204 // This is a typical case of layer violation where the
2205 // transported contents are critical to the connection's
2206 // validity and must be ignored at the same time :-(
2207
2208 /* graceful shutdown, ignore streams whose ID is higher than
2209 * the one advertised in GOAWAY. RFC7540#6.8.
2210 */
2211 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002212 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2213 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002214 h2c->dfl -= ret;
2215 ret = h2c->dfl == 0;
2216 goto strm_err;
2217 }
2218#endif
2219
Willy Tarreau7e98c052017-10-10 15:56:59 +02002220 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002221 case H2_FT_SETTINGS:
2222 if (h2c->st0 == H2_CS_FRAME_P)
2223 ret = h2c_handle_settings(h2c);
2224
2225 if (h2c->st0 == H2_CS_FRAME_A)
2226 ret = h2c_ack_settings(h2c);
2227 break;
2228
Willy Tarreaucf68c782017-10-10 17:11:41 +02002229 case H2_FT_PING:
2230 if (h2c->st0 == H2_CS_FRAME_P)
2231 ret = h2c_handle_ping(h2c);
2232
2233 if (h2c->st0 == H2_CS_FRAME_A)
2234 ret = h2c_ack_ping(h2c);
2235 break;
2236
Willy Tarreau26f95952017-07-27 17:18:30 +02002237 case H2_FT_WINDOW_UPDATE:
2238 if (h2c->st0 == H2_CS_FRAME_P)
2239 ret = h2c_handle_window_update(h2c, h2s);
2240 break;
2241
Willy Tarreau61290ec2017-10-17 08:19:21 +02002242 case H2_FT_CONTINUATION:
2243 /* we currently don't support CONTINUATION frames since
2244 * we have nowhere to store the partial HEADERS frame.
2245 * Let's abort the stream on an INTERNAL_ERROR here.
2246 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002247 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02002248 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002249 h2c->st0 = H2_CS_FRAME_E;
2250 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02002251 break;
2252
Willy Tarreau13278b42017-10-13 19:23:14 +02002253 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002254 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002255 if (h2c->flags & H2_CF_IS_BACK)
2256 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2257 else
2258 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002259 if (tmp_h2s) {
2260 h2s = tmp_h2s;
2261 ret = 1;
2262 }
2263 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002264 break;
2265
Willy Tarreau454f9052017-10-26 19:40:35 +02002266 case H2_FT_DATA:
2267 if (h2c->st0 == H2_CS_FRAME_P)
2268 ret = h2c_frt_handle_data(h2c, h2s);
2269
2270 if (h2c->st0 == H2_CS_FRAME_A)
2271 ret = h2c_send_strm_wu(h2c);
2272 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002273
Willy Tarreau92153fc2017-12-03 19:46:19 +01002274 case H2_FT_PRIORITY:
2275 if (h2c->st0 == H2_CS_FRAME_P)
2276 ret = h2c_handle_priority(h2c);
2277 break;
2278
Willy Tarreaucd234e92017-08-18 10:59:39 +02002279 case H2_FT_RST_STREAM:
2280 if (h2c->st0 == H2_CS_FRAME_P)
2281 ret = h2c_handle_rst_stream(h2c, h2s);
2282 break;
2283
Willy Tarreaue96b0922017-10-30 00:28:29 +01002284 case H2_FT_GOAWAY:
2285 if (h2c->st0 == H2_CS_FRAME_P)
2286 ret = h2c_handle_goaway(h2c);
2287 break;
2288
Willy Tarreau1c661982017-10-30 13:52:01 +01002289 case H2_FT_PUSH_PROMISE:
2290 /* not permitted here, RFC7540#5.1 */
2291 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002292 if (!h2c->nb_streams) {
2293 /* only log if no other stream can report the error */
2294 sess_log(h2c->conn->owner);
2295 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002296 break;
2297
2298 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002299 default:
2300 /* drop frames that we ignore. They may be larger than
2301 * the buffer so we drain all of their contents until
2302 * we reach the end.
2303 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002304 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2305 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002306 h2c->dfl -= ret;
2307 ret = h2c->dfl == 0;
2308 }
2309
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002310 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002311 /* We may have to send an RST if not done yet */
2312 if (h2s->st == H2_SS_ERROR)
2313 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002314
Willy Tarreaua20a5192017-12-27 11:02:06 +01002315 if (h2c->st0 == H2_CS_FRAME_E)
2316 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002317
Willy Tarreau7e98c052017-10-10 15:56:59 +02002318 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002319 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002320 break;
2321
2322 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002323 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002324 h2c->st0 = H2_CS_FRAME_H;
2325 }
2326 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002327
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002328 if (h2c->rcvd_c > 0 &&
2329 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2330 h2c_send_conn_wu(h2c);
2331
Willy Tarreau52eed752017-09-22 15:05:09 +02002332 fail:
2333 /* we can go here on missing data, blocked response or error */
Olivier Houchard638b7992018-08-16 15:41:52 +02002334 if (h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002335 /* we may have to signal the upper layers */
2336 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002337 if (h2s->recv_wait) {
2338 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
2339 tasklet_wakeup(h2s->recv_wait->task);
2340 h2s->recv_wait = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002341 }
2342 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002343
2344 if (h2_recv_allowed(h2c))
2345 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreaubc933932017-10-09 16:21:43 +02002346}
2347
2348/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2349 * the end.
2350 */
2351static int h2_process_mux(struct h2c *h2c)
2352{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002353 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002354
Willy Tarreau01b44822018-10-03 14:26:37 +02002355 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2356 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2357 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2358 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2359 if (h2c->st0 == H2_CS_ERROR) {
2360 h2c->st0 = H2_CS_ERROR2;
2361 sess_log(h2c->conn->owner);
2362 }
2363 goto fail;
2364 }
2365 h2c->st0 = H2_CS_SETTINGS1;
2366 }
2367 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002368 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002369 return 1;
2370 }
2371
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002372 /* start by sending possibly pending window updates */
2373 if (h2c->rcvd_c > 0 &&
2374 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2375 h2c_send_conn_wu(h2c) < 0)
2376 goto fail;
2377
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002378 /* First we always process the flow control list because the streams
2379 * waiting there were already elected for immediate emission but were
2380 * blocked just on this.
2381 */
2382
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002383 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002384 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2385 h2c->st0 >= H2_CS_ERROR)
2386 break;
2387
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002388 h2s->flags &= ~H2_SF_BLK_ANY;
2389 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002390 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002391 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002392 LIST_DEL(&h2s->list);
2393 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002394 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002395 }
2396
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002397 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002398 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2399 break;
2400
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002401 h2s->flags &= ~H2_SF_BLK_ANY;
2402 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002403 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002404 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002405 LIST_DEL(&h2s->list);
2406 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002407 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002408 }
2409
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002410 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002411 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002412 if (h2c->st0 == H2_CS_ERROR) {
2413 if (h2c->max_id >= 0) {
2414 h2c_send_goaway_error(h2c, NULL);
2415 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2416 return 0;
2417 }
2418
2419 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2420 }
2421 return 1;
2422 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002423 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002424}
2425
Willy Tarreau62f52692017-10-08 23:01:42 +02002426
Willy Tarreau479998a2018-11-18 06:30:59 +01002427/* Attempt to read data, and subscribe if none available.
2428 * The function returns 1 if data has been received, otherwise zero.
2429 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002430static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002431{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002432 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002433 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002434 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002435 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002436
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002437 if (h2c->wait_event.wait_reason & SUB_CAN_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002438 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002439
Willy Tarreau315d8072017-12-10 22:17:57 +01002440 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002441 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002442
Willy Tarreau44e973f2018-03-01 17:49:30 +01002443 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002444 if (!buf) {
2445 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002446 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002447 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002448
Olivier Houchard7505f942018-08-21 18:10:44 +02002449 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002450 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002451 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2452 /* HTX in use : try to pre-align the buffer like the
2453 * rxbufs will be to optimize memory copies. We'll make
2454 * sure that the frame header lands at the end of the
2455 * HTX block to alias it upon recv. We cannot use the
2456 * head because rcv_buf() will realign the buffer if
2457 * it's empty. Thus we cheat and pretend we already
2458 * have a few bytes there.
2459 */
2460 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002461 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002462 }
2463 else
2464 max = b_room(buf);
2465
Olivier Houchard7505f942018-08-21 18:10:44 +02002466 if (max)
2467 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2468 else
2469 ret = 0;
2470 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002471
Olivier Houchard53216e72018-10-10 15:46:36 +02002472 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002473 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002474
Olivier Houcharda1411e62018-08-17 18:42:48 +02002475 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002476 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002477 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002478 }
2479
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002480 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002481 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002482 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002483}
2484
Willy Tarreau479998a2018-11-18 06:30:59 +01002485/* Try to send data if possible.
2486 * The function returns 1 if data have been sent, otherwise zero.
2487 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002488static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002489{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002490 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002491 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002492 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002493
2494 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002495 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002496
Olivier Houchard7505f942018-08-21 18:10:44 +02002497
Willy Tarreaua2af5122017-10-09 11:56:46 +02002498 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2499 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002500 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002501 }
2502
Willy Tarreaubc933932017-10-09 16:21:43 +02002503 /* This loop is quite simple : it tries to fill as much as it can from
2504 * pending streams into the existing buffer until it's reportedly full
2505 * or the end of send requests is reached. Then it tries to send this
2506 * buffer's contents out, marks it not full if at least one byte could
2507 * be sent, and tries again.
2508 *
2509 * The snd_buf() function normally takes a "flags" argument which may
2510 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2511 * data immediately comes and CO_SFL_STREAMER to indicate that the
2512 * connection is streaming lots of data (used to increase TLS record
2513 * size at the expense of latency). The former can be sent any time
2514 * there's a buffer full flag, as it indicates at least one stream
2515 * attempted to send and failed so there are pending data. An
2516 * alternative would be to set it as long as there's an active stream
2517 * but that would be problematic for ACKs until we have an absolute
2518 * guarantee that all waiters have at least one byte to send. The
2519 * latter should possibly not be set for now.
2520 */
2521
2522 done = 0;
2523 while (!done) {
2524 unsigned int flags = 0;
2525
2526 /* fill as much as we can into the current buffer */
2527 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2528 done = h2_process_mux(h2c);
2529
2530 if (conn->flags & CO_FL_ERROR)
2531 break;
2532
2533 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2534 flags |= CO_SFL_MSG_MORE;
2535
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002536 if (b_data(&h2c->mbuf)) {
2537 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002538 if (!ret)
2539 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002540 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002541 b_del(&h2c->mbuf, ret);
2542 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002543 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002544
2545 /* wrote at least one byte, the buffer is not full anymore */
2546 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2547 }
2548
Willy Tarreaua2af5122017-10-09 11:56:46 +02002549 if (conn->flags & CO_FL_SOCK_WR_SH) {
2550 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002551 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002552 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002553 /* We're not full anymore, so we can wake any task that are waiting
2554 * for us.
2555 */
2556 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002557 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002558 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2559 struct h2s *, list);
2560 LIST_DEL(&h2s->list);
2561 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002562 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002563 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002564 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002565 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002566 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002567 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002568 /* We're done, no more to send */
2569 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002570 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002571schedule:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002572 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
2573 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002574 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002575}
2576
2577static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2578{
2579 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002580 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002581
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002582 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002583 ret = h2_send(h2c);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002584 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002585 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002586 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002587 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002588 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002589}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002590
Willy Tarreau62f52692017-10-08 23:01:42 +02002591/* callback called on any event by the connection handler.
2592 * It applies changes and returns zero, or < 0 if it wants immediate
2593 * destruction of the connection (which normally doesn not happen in h2).
2594 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002595static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002596{
Olivier Houchard7505f942018-08-21 18:10:44 +02002597 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002598
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002599 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002600 h2_process_demux(h2c);
2601
2602 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002603 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002604
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002605 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002606 h2c->flags &= ~H2_CF_DEM_DFULL;
2607 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002608 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002609
Willy Tarreau0b37d652018-10-03 10:33:02 +02002610 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002611 /* frontend is stopping, reload likely in progress, let's try
2612 * to announce a graceful shutdown if not yet done. We don't
2613 * care if it fails, it will be tried again later.
2614 */
2615 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2616 if (h2c->last_sid < 0)
2617 h2c->last_sid = (1U << 31) - 1;
2618 h2c_send_goaway_error(h2c, NULL);
2619 }
2620 }
2621
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002622 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002623 * If we received early data, and the handshake is done, wake
2624 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002625 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002626 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2627 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2628 struct eb32_node *node;
2629 struct h2s *h2s;
2630
2631 h2c->flags |= H2_CF_WAIT_FOR_HS;
2632 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2633
2634 while (node) {
2635 h2s = container_of(node, struct h2s, by_id);
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002636 if ((h2s->cs->flags & CS_FL_WAIT_FOR_HS) &&
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002637 h2s->recv_wait) {
2638 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002639 sw->wait_reason &= ~SUB_CAN_RECV;
2640 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002641 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002642 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002643 node = eb32_next(node);
2644 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002645 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002646
Willy Tarreau26bd7612017-10-09 16:47:04 +02002647 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002648 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2649 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2650 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002651 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002652
2653 if (eb_is_empty(&h2c->streams_by_id)) {
2654 /* no more stream, kill the connection now */
2655 h2_release(conn);
2656 return -1;
2657 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002658 }
2659
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002660 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002661 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002662
Olivier Houchard53216e72018-10-10 15:46:36 +02002663 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2664 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2665 (h2c->st0 != H2_CS_ERROR &&
2666 !b_data(&h2c->mbuf) &&
2667 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2668 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002669 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002670
Willy Tarreau3f133572017-10-31 19:21:06 +01002671 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002672 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002673 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002674 task_queue(h2c->task);
2675 }
2676 else
2677 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002678 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002679
Olivier Houchard7505f942018-08-21 18:10:44 +02002680 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002681 return 0;
2682}
2683
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002684static int h2_wake(struct connection *conn)
2685{
2686 struct h2c *h2c = conn->mux_ctx;
2687
2688 return (h2_process(h2c));
2689}
2690
Willy Tarreauea392822017-10-31 10:02:25 +01002691/* Connection timeout management. The principle is that if there's no receipt
2692 * nor sending for a certain amount of time, the connection is closed. If the
2693 * MUX buffer still has lying data or is not allocatable, the connection is
2694 * immediately killed. If it's allocatable and empty, we attempt to send a
2695 * GOAWAY frame.
2696 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002697static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002698{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002699 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002700 int expired = tick_is_expired(t->expire, now_ms);
2701
Willy Tarreau0975f112018-03-29 15:22:59 +02002702 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002703 return t;
2704
Willy Tarreau0975f112018-03-29 15:22:59 +02002705 task_delete(t);
2706 task_free(t);
2707
2708 if (!h2c) {
2709 /* resources were already deleted */
2710 return NULL;
2711 }
2712
2713 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002714 h2c_error(h2c, H2_ERR_NO_ERROR);
2715 h2_wake_some_streams(h2c, 0, 0);
2716
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002717 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002718 /* don't even try to send a GOAWAY, the buffer is stuck */
2719 h2c->flags |= H2_CF_GOAWAY_FAILED;
2720 }
2721
2722 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002723 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002724 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2725 h2c->flags |= H2_CF_GOAWAY_FAILED;
2726
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002727 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2728 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002729 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002730 b_del(&h2c->mbuf, ret);
2731 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002732 }
2733 }
Willy Tarreauea392822017-10-31 10:02:25 +01002734
Willy Tarreau0975f112018-03-29 15:22:59 +02002735 /* either we can release everything now or it will be done later once
2736 * the last stream closes.
2737 */
2738 if (eb_is_empty(&h2c->streams_by_id))
2739 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002740
Willy Tarreauea392822017-10-31 10:02:25 +01002741 return NULL;
2742}
2743
2744
Willy Tarreau62f52692017-10-08 23:01:42 +02002745/*******************************************/
2746/* functions below are used by the streams */
2747/*******************************************/
2748
2749/*
2750 * Attach a new stream to a connection
2751 * (Used for outgoing connections)
2752 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002753static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002754{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002755 struct conn_stream *cs;
2756 struct h2s *h2s;
2757 struct h2c *h2c = conn->mux_ctx;
2758
2759 cs = cs_new(conn);
2760 if (!cs)
2761 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002762 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002763 if (!h2s) {
2764 cs_free(cs);
2765 return NULL;
2766 }
2767 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002768}
2769
Willy Tarreaufafd3982018-11-18 21:29:20 +01002770/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2771 * We have to scan because we may have some orphan streams. It might be
2772 * beneficial to scan backwards from the end to reduce the likeliness to find
2773 * orphans.
2774 */
2775static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2776{
2777 struct h2c *h2c = conn->mux_ctx;
2778 struct h2s *h2s;
2779 struct eb32_node *node;
2780
2781 node = eb32_first(&h2c->streams_by_id);
2782 while (node) {
2783 h2s = container_of(node, struct h2s, by_id);
2784 if (h2s->cs)
2785 return h2s->cs;
2786 node = eb32_next(node);
2787 }
2788 return NULL;
2789}
2790
Willy Tarreau62f52692017-10-08 23:01:42 +02002791/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002792 * Destroy the mux and the associated connection, if it is no longer used
2793 */
2794static void h2_destroy(struct connection *conn)
2795{
2796 struct h2c *h2c = conn->mux_ctx;
2797
2798 if (eb_is_empty(&h2c->streams_by_id))
2799 h2_release(h2c->conn);
2800}
2801
2802/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002803 * Detach the stream from the connection and possibly release the connection.
2804 */
2805static void h2_detach(struct conn_stream *cs)
2806{
Willy Tarreau60935142017-10-16 18:11:19 +02002807 struct h2s *h2s = cs->ctx;
2808 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002809 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002810
2811 cs->ctx = NULL;
2812 if (!h2s)
2813 return;
2814
Olivier Houchardf502aca2018-12-14 19:42:40 +01002815 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002816 h2c = h2s->h2c;
2817 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002818 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002819 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2820 !h2_has_too_many_cs(h2c)) {
2821 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard53216e72018-10-10 15:46:36 +02002822 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002823 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreauf2101912018-07-19 10:11:38 +02002824 }
Willy Tarreau60935142017-10-16 18:11:19 +02002825
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002826 /* this stream may be blocked waiting for some data to leave (possibly
2827 * an ES or RST frame), so orphan it in this case.
2828 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002829 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002830 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002831 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002832 return;
2833
Willy Tarreau45f752e2017-10-30 15:44:59 +01002834 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2835 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2836 /* unblock the connection if it was blocked on this
2837 * stream.
2838 */
2839 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2840 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002841 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002842 }
2843
Willy Tarreau71049cc2018-03-28 13:56:39 +02002844 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002845
Olivier Houchard8a786902018-12-15 16:05:40 +01002846 if (h2c->flags & H2_CF_IS_BACK &&
2847 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01002848 if (!(h2c->conn->flags &
2849 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
2850 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002851 h2c->conn->owner = sess;
2852 session_add_conn(sess, h2c->conn, h2c->conn->target);
Olivier Houchard8a786902018-12-15 16:05:40 +01002853 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01002854 if (eb_is_empty(&h2c->streams_by_id)) {
2855 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
2856 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
2857 return;
2858 }
Olivier Houchard8a786902018-12-15 16:05:40 +01002859 /* Never ever allow to reuse a connection from a non-reuse backend */
2860 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
2861 h2c->conn->flags |= CO_FL_PRIVATE;
2862 if (LIST_ISEMPTY(&h2c->conn->list)) {
2863 struct server *srv = objt_server(h2c->conn->target);
2864
2865 if (srv) {
2866 if (h2c->conn->flags & CO_FL_PRIVATE)
2867 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
2868 else
2869 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
2870 }
2871
2872 }
2873 }
2874 }
2875
Willy Tarreaue323f342018-03-28 13:51:45 +02002876 /* We don't want to close right now unless we're removing the
2877 * last stream, and either the connection is in error, or it
2878 * reached the ID already specified in a GOAWAY frame received
2879 * or sent (as seen by last_sid >= 0).
2880 */
2881 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2882 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002883 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard52b94662018-10-21 03:01:20 +02002884 (h2c->flags & (H2_CF_GOAWAY_FAILED | H2_CF_GOAWAY_SENT)) ||
Olivier Houchard93c88522018-11-30 15:39:16 +01002885 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002886 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002887 (conn_xprt_read0_pending(h2c->conn) ||
2888 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2889 /* no more stream will come, kill it now */
2890 h2_release(h2c->conn);
2891 }
2892 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002893 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002894 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2895 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002896 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002897 else
2898 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002899 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002900}
2901
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002902static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002903{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002904 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002905 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002906
Willy Tarreau721c9742017-11-07 11:05:42 +01002907 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002908 return;
2909
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002910 /* if no outgoing data was seen on this stream, it means it was
2911 * closed with a "tcp-request content" rule that is normally
2912 * used to kill the connection ASAP (eg: limit abuse). In this
2913 * case we send a goaway to close the connection.
2914 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002915 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002916 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002917 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002918
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002919 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2920 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002921 h2c_send_goaway_error(h2c, h2s) <= 0)
2922 return;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002923
Olivier Houchard435ce2d2018-12-03 18:43:16 +01002924 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
2925 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01002926 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002927
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002928 return;
2929add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002930 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002931 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002932 if (h2s->flags & H2_SF_BLK_MFCTL) {
2933 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2934 h2s->send_wait = sw;
2935 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2936 h2s->send_wait = sw;
2937 LIST_ADDQ(&h2c->send_list, &h2s->list);
2938 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002939 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002940 /* Let the handler know we want shutr */
2941 sw->handle = (void *)((long)sw->handle | 1);
2942
Willy Tarreau62f52692017-10-08 23:01:42 +02002943}
2944
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002945static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002946{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002947 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002948 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002949
Willy Tarreau721c9742017-11-07 11:05:42 +01002950 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002951 return;
2952
Willy Tarreau67434202017-11-06 20:20:51 +01002953 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002954 /* we can cleanly close using an empty data frame only after headers */
2955
2956 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2957 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002958 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002959
2960 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002961 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002962 else
2963 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002964 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002965 /* if no outgoing data was seen on this stream, it means it was
2966 * closed with a "tcp-request content" rule that is normally
2967 * used to kill the connection ASAP (eg: limit abuse). In this
2968 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002969 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002970 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002971 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002972 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002973
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002974 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2975 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002976 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002977 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002978
Willy Tarreau00dd0782018-03-01 16:31:34 +01002979 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002980 }
2981
Olivier Houchard435ce2d2018-12-03 18:43:16 +01002982 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
2983 tasklet_wakeup(h2c->wait_event.task);
2984 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002985
2986 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002987 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002988 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002989 if (h2s->flags & H2_SF_BLK_MFCTL) {
2990 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2991 h2s->send_wait = sw;
2992 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2993 h2s->send_wait = sw;
2994 LIST_ADDQ(&h2c->send_list, &h2s->list);
2995 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002996 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002997 /* let the handler know we want to shutw */
2998 sw->handle = (void *)((long)(sw->handle) | 2);
2999
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003000}
3001
3002static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3003{
3004 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003005 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003006
Olivier Houchard2c68a462018-12-15 22:42:20 +01003007 if (h2s->send_wait) {
3008 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
3009 h2s->send_wait = NULL;
3010 LIST_DEL(&h2s->list);
3011 LIST_INIT(&h2s->list);
3012 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003013 if (reason & 2)
3014 h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003015 if (reason & 1)
3016 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003017
Olivier Houchard2c68a462018-12-15 22:42:20 +01003018 if (h2s->st == H2_SS_CLOSED &&
Olivier Houchardffda58b2018-12-16 01:29:11 +01003019 !((h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))) && !h2s->cs)
Olivier Houchard2c68a462018-12-15 22:42:20 +01003020 h2s_destroy(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003021 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003022}
3023
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003024static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3025{
3026 struct h2s *h2s = cs->ctx;
3027
3028 if (!mode)
3029 return;
3030
3031 h2_do_shutr(h2s);
3032}
3033
3034static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3035{
3036 struct h2s *h2s = cs->ctx;
3037
3038 h2_do_shutw(h2s);
3039}
3040
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003041/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003042 * HTX request or response depending on the connection's side. Returns the
3043 * number of bytes emitted if > 0, or 0 if it couldn't proceed. Stream errors
3044 * are reported in h2s->errcode and connection errors in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02003045 */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003046static int h2s_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02003047{
3048 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003049 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003050 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003051 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003052 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003053 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01003054 struct buffer *csbuf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003055 struct htx *htx = NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02003056 int flen = h2c->dfl;
3057 int outlen = 0;
3058 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003059 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003060
3061 if (!h2c->dfl) {
3062 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01003063 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02003064 return 0;
3065 }
3066
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003067 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01003068 return 0; // incomplete input frame
3069
Willy Tarreau13278b42017-10-13 19:23:14 +02003070 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003071 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003072 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003073 copy = alloc_trash_chunk();
3074 if (!copy) {
3075 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3076 goto fail;
3077 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003078 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3079 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3080 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003081 }
3082
3083 /* The padlen is the first byte before data, and the padding appears
3084 * after data. padlen+data+padding are included in flen.
3085 */
3086 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003087 h2c->dpl = *hdrs;
3088 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02003089 /* RFC7540#6.2 : pad length = length of frame payload or greater */
3090 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003091 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02003092 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003093 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02003094 hdrs += 1; // skip Pad Length
3095 }
3096
3097 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3098 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003099 if (read_n32(hdrs) == h2s->id) {
3100 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3101 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003102 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003103 }
3104
Willy Tarreau13278b42017-10-13 19:23:14 +02003105 hdrs += 5; // stream dep = 4, weight = 1
3106 flen -= 5;
3107 }
3108
3109 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
3110 * don't support this for now and can't even decompress so we have to
3111 * break the connection.
3112 */
3113 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
3114 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003115 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02003116 }
3117
Olivier Houchard638b7992018-08-16 15:41:52 +02003118 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01003119 if (!csbuf) {
3120 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003121 goto fail;
3122 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003123
Willy Tarreau937f7602018-02-26 15:22:17 +01003124 /* we can't retry a failed decompression operation so we must be very
3125 * careful not to take any risks. In practice the output buffer is
3126 * always empty except maybe for trailers, in which case we simply have
3127 * to wait for the upper layer to finish consuming what is available.
3128 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003129
3130 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
3131 htx = htx_from_buf(&h2s->rxbuf);
3132 if (!htx_is_empty(htx))
3133 goto fail;
3134 } else {
3135 if (b_data(csbuf))
3136 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003137
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003138 csbuf->head = 0;
3139 try = b_size(csbuf);
3140 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003141
3142 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3143 sizeof(list)/sizeof(list[0]), tmp);
3144 if (outlen < 0) {
3145 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3146 goto fail;
3147 }
3148
3149 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02003150 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003151
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003152 if (htx) {
3153 /* HTX mode */
3154 if (h2c->flags & H2_CF_IS_BACK)
3155 outlen = h2_make_htx_response(list, htx, &msgf);
3156 else
3157 outlen = h2_make_htx_request(list, htx, &msgf);
3158 } else {
3159 /* HTTP/1 mode */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003160 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003161 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003162
3163 if (outlen < 0) {
3164 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3165 goto fail;
3166 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003167
Willy Tarreau174b06a2018-04-25 18:13:58 +02003168 if (msgf & H2_MSGF_BODY) {
3169 /* a payload is present */
3170 if (msgf & H2_MSGF_BODY_CL)
3171 h2s->flags |= H2_SF_DATA_CLEN;
Olivier Houchard50d660c2018-12-08 00:18:31 +01003172 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau174b06a2018-04-25 18:13:58 +02003173 h2s->flags |= H2_SF_DATA_CHNK;
3174 }
3175
Willy Tarreau13278b42017-10-13 19:23:14 +02003176 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003177 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02003178 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01003179 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02003180
Willy Tarreau39d68502018-03-02 12:26:37 +01003181 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02003182 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003183 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003184 if (htx)
3185 htx_add_endof(htx, HTX_BLK_EOM);
Willy Tarreau39d68502018-03-02 12:26:37 +01003186 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003187
Willy Tarreau68dd9852017-07-03 14:44:26 +02003188 leave:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003189 if (htx)
3190 htx_to_buf(htx, &h2s->rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003191 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02003192 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003193 fail:
3194 outlen = 0;
3195 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02003196}
3197
Willy Tarreau454f9052017-10-26 19:40:35 +02003198/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3199 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3200 * in use, a new chunk is emitted for each frame. This is supposed to fit
3201 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3202 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3203 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003204 * parser state is automatically updated. Returns > 0 if it could completely
3205 * send the current frame, 0 if it couldn't complete, in which case
3206 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3207 * DATA frame can return 0 as a valid result). Stream errors are reported in
3208 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3209 * have checked the frame header and ensured that the frame was complete or the
3210 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003211 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003212static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003213{
3214 struct h2c *h2c = h2s->h2c;
3215 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003216 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003217 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003218 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003219 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003220
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003221 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003222
3223 /* The padlen is the first byte before data, and the padding appears
3224 * after data. padlen+data+padding are included in flen.
3225 */
Willy Tarreau79127812017-12-03 21:06:59 +01003226 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003227 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003228 return 0;
3229
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003230 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003231 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003232 /* RFC7540#6.1 : pad length = length of frame payload or greater */
3233 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02003234 return 0;
3235 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003236
3237 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003238 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003239 h2c->dfl--;
3240 h2c->rcvd_c++; h2c->rcvd_s++;
3241 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02003242 }
3243
Olivier Houchard638b7992018-08-16 15:41:52 +02003244 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003245 if (!csbuf) {
3246 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003247 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003248 }
3249
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003250try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003251 flen = h2c->dfl - h2c->dpl;
3252 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003253 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003254
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003255 if (flen > b_data(&h2c->dbuf)) {
3256 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003257 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003258 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003259 }
3260
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003261 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
3262 htx = htx_from_buf(csbuf);
3263 block1 = htx_free_data_space(htx);
3264 if (!block1) {
3265 h2c->flags |= H2_CF_DEM_SFULL;
3266 goto fail;
3267 }
3268 if (flen > block1)
3269 flen = block1;
3270
3271 /* here, flen is the max we can copy into the output buffer */
3272 block1 = b_contig_data(&h2c->dbuf, 0);
3273 if (flen > block1)
3274 flen = block1;
3275
3276 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3277 h2c->flags |= H2_CF_DEM_SFULL;
3278 goto fail;
3279 }
3280
3281 b_del(&h2c->dbuf, flen);
3282 h2c->dfl -= flen;
3283 h2c->rcvd_c += flen;
3284 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3285 goto try_again;
3286 }
3287 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003288 /* it doesn't fit and the buffer is fragmented,
3289 * so let's defragment it and try again.
3290 */
3291 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003292 }
3293
Willy Tarreaueba10f22018-04-25 20:44:22 +02003294 /* chunked-encoding requires more room */
3295 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003296 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003297 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3298 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3299 (chklen < 1048576) ? 4 : 8;
3300 chklen += 4; // CRLF, CRLF
3301 }
3302
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003303 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003304 if (flen + chklen > b_room(csbuf)) {
3305 if (chklen >= b_room(csbuf)) {
3306 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003307 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003308 }
3309 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003310 }
3311
3312 if (h2s->flags & H2_SF_DATA_CHNK) {
3313 /* emit the chunk size */
3314 unsigned int chksz = flen;
3315 char str[10];
3316 char *beg;
3317
3318 beg = str + sizeof(str);
3319 *--beg = '\n';
3320 *--beg = '\r';
3321 do {
3322 *--beg = hextab[chksz & 0xF];
3323 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003324 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003325 }
3326
Willy Tarreau454f9052017-10-26 19:40:35 +02003327 /* Block1 is the length of the first block before the buffer wraps,
3328 * block2 is the optional second block to reach the end of the frame.
3329 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003330 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003331 if (block1 > flen)
3332 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003333 block2 = flen - block1;
3334
3335 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003336 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003337
3338 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003339 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003340
Willy Tarreaueba10f22018-04-25 20:44:22 +02003341 if (h2s->flags & H2_SF_DATA_CHNK) {
3342 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003343 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003344 }
3345
Willy Tarreau454f9052017-10-26 19:40:35 +02003346 /* now mark the input data as consumed (will be deleted from the buffer
3347 * by the caller when seeing FRAME_A after sending the window update).
3348 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003349 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003350 h2c->dfl -= flen;
3351 h2c->rcvd_c += flen;
3352 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3353
3354 if (h2c->dfl > h2c->dpl) {
3355 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003356 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003357 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003358 }
3359
Willy Tarreau4a28da12018-01-04 14:41:00 +01003360 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003361 /* here we're done with the frame, all the payload (except padding) was
3362 * transferred.
3363 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003364
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003365 if (h2c->dff & H2_F_DATA_END_STREAM) {
3366 if (htx) {
3367 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3368 h2c->flags |= H2_CF_DEM_SFULL;
3369 goto fail;
3370 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003371 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003372 else if (h2s->flags & H2_SF_DATA_CHNK) {
3373 /* emit the trailing 0 CRLF CRLF */
3374 if (b_room(csbuf) < 5) {
3375 h2c->flags |= H2_CF_DEM_SFULL;
3376 goto fail;
3377 }
3378 chklen += 5;
3379 b_putblk(csbuf, "0\r\n\r\n", 5);
3380 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003381 }
3382
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003383 h2c->rcvd_c += h2c->dpl;
3384 h2c->rcvd_s += h2c->dpl;
3385 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003386 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3387
Willy Tarreau39d68502018-03-02 12:26:37 +01003388 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003389 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003390 h2s->cs->flags |= CS_FL_REOS;
3391 }
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003392 if (htx)
3393 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003394 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003395 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003396 if (htx)
3397 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003398 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003399}
3400
Willy Tarreau5dd17352018-06-14 13:33:30 +02003401/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3402 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3403 * number of bytes sent. The caller must check the stream's status to detect
3404 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003405 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003406static 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 +02003407{
3408 struct http_hdr list[MAX_HTTP_HDR];
3409 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003410 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003411 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003412 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003413 int es_now = 0;
3414 int ret = 0;
3415 int hdr;
3416
3417 if (h2c_mux_busy(h2c, h2s)) {
3418 h2s->flags |= H2_SF_BLK_MBUSY;
3419 return 0;
3420 }
3421
Willy Tarreau44e973f2018-03-01 17:49:30 +01003422 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003423 h2c->flags |= H2_CF_MUX_MALLOC;
3424 h2s->flags |= H2_SF_BLK_MROOM;
3425 return 0;
3426 }
3427
3428 /* First, try to parse the H1 response and index it into <list>.
3429 * NOTE! Since it comes from haproxy, we *know* that a response header
3430 * block does not wrap and we can safely read it this way without
3431 * having to realign the buffer.
3432 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003433 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003434 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003435 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003436 /* incomplete or invalid response, this is abnormal coming from
3437 * haproxy and may only result in a bad errorfile or bad Lua code
3438 * so that won't be fixed, raise an error now.
3439 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003440 * FIXME: we should instead add the ability to only return a
3441 * 502 bad gateway. But in theory this is not supposed to
3442 * happen.
3443 */
3444 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3445 ret = 0;
3446 goto end;
3447 }
3448
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003449 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003450
3451 /* certain statuses have no body or an empty one, regardless of
3452 * what the headers say.
3453 */
3454 if (sl.st.status >= 100 && sl.st.status < 200) {
3455 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3456 h1m->curr_len = h1m->body_len = 0;
3457 }
3458 else if (sl.st.status == 204 || sl.st.status == 304) {
3459 /* no contents, claim c-len is present and set to zero */
3460 h1m->flags &= ~H1_MF_CHNK;
3461 h1m->flags |= H1_MF_CLEN;
3462 h1m->curr_len = h1m->body_len = 0;
3463 }
3464
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003465 chunk_reset(&outbuf);
3466
3467 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003468 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003469 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003470 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003471
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003472 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003473 break;
3474 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003475 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003476 }
3477
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003478 if (outbuf.size < 9)
3479 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003480
3481 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003482 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3483 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3484 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003485
3486 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003487 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003488 /* this is an unparsable response */
3489 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3490 ret = 0;
3491 goto end;
3492 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003493
3494 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003495 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003496 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003497 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003498 }
3499
3500 /* encode all headers, stop at empty name */
3501 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003502 /* these ones do not exist in H2 and must be dropped. */
3503 if (isteq(list[hdr].n, ist("connection")) ||
3504 isteq(list[hdr].n, ist("proxy-connection")) ||
3505 isteq(list[hdr].n, ist("keep-alive")) ||
3506 isteq(list[hdr].n, ist("upgrade")) ||
3507 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003508 continue;
3509
3510 if (isteq(list[hdr].n, ist("")))
3511 break; // end
3512
3513 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3514 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003515 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003516 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003517 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003518 }
3519 }
3520
3521 /* we may need to add END_STREAM */
3522 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3523 es_now = 1;
3524
3525 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003526 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003527
3528 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003529 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003530
3531 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003532 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003533
3534 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003535 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003536 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003537
3538 /* for now we don't implemented CONTINUATION, so we wait for a
3539 * body or directly end in TRL2.
3540 */
3541 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003542 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003543 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003544
Willy Tarreau801250e2018-09-11 11:45:04 +02003545 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003546 h2s->flags |= H2_SF_ES_SENT;
3547 if (h2s->st == H2_SS_OPEN)
3548 h2s->st = H2_SS_HLOC;
3549 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003550 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003551 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003552 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003553 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003554 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003555 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003556 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003557 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003558 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003559
3560 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003561
3562 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003563 //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 +02003564 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003565 full:
3566 h1m_init_res(h1m);
3567 h1m->err_pos = -1; // don't care about errors on the response path
3568 h2c->flags |= H2_CF_MUX_MFULL;
3569 h2s->flags |= H2_SF_BLK_MROOM;
3570 ret = 0;
3571 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003572}
3573
Willy Tarreau5dd17352018-06-14 13:33:30 +02003574/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3575 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3576 * the number of bytes sent. The caller must check the stream's status to
3577 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003578 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003579static 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 +02003580{
3581 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003582 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003583 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003584 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003585 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003586 int es_now = 0;
3587 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003588 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003589 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003590
3591 if (h2c_mux_busy(h2c, h2s)) {
3592 h2s->flags |= H2_SF_BLK_MBUSY;
3593 goto end;
3594 }
3595
Willy Tarreau44e973f2018-03-01 17:49:30 +01003596 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003597 h2c->flags |= H2_CF_MUX_MALLOC;
3598 h2s->flags |= H2_SF_BLK_MROOM;
3599 goto end;
3600 }
3601
3602 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003603 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003604 goto end;
3605
3606 chunk_reset(&outbuf);
3607
3608 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003609 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003610 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003611 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003612
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003613 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003614 break;
3615 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01003616 /* If there are pending data in the output buffer, and we have
3617 * less than 1/4 of the mbuf's size and everything fits, we'll
3618 * still perform a copy anyway. Otherwise we'll pretend the mbuf
3619 * is full and wait, to save some slow realign calls.
3620 */
3621 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
3622 h2c->flags |= H2_CF_MUX_MFULL;
3623 h2s->flags |= H2_SF_BLK_MROOM;
3624 goto end;
3625 }
3626
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003627 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003628 }
3629
3630 if (outbuf.size < 9) {
3631 h2c->flags |= H2_CF_MUX_MFULL;
3632 h2s->flags |= H2_SF_BLK_MROOM;
3633 goto end;
3634 }
3635
3636 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003637 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3638 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3639 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003640
3641 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3642 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003643 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003644 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003645 break;
3646 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003647 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003648 if ((long long)size > h1m->curr_len)
3649 size = h1m->curr_len;
3650 break;
3651 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003652 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003653 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003654 if (!ret)
3655 goto end;
3656
3657 if (ret < 0) {
3658 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003659 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003660 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3661 goto end;
3662 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003663 max -= ret;
3664 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003665 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003666 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003667 }
3668
Willy Tarreau801250e2018-09-11 11:45:04 +02003669 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003670 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003671 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003672 if (!ret)
3673 goto end;
3674
3675 if (ret < 0) {
3676 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003677 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003678 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3679 goto end;
3680 }
3681
3682 size = chunk;
3683 h1m->curr_len = chunk;
3684 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003685 max -= ret;
3686 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003687 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003688 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003689 if (!size)
3690 goto send_empty;
3691 }
3692
3693 /* in MSG_DATA state, continue below */
3694 size = h1m->curr_len;
3695 break;
3696 }
3697
3698 /* we have in <size> the exact number of bytes we need to copy from
3699 * the H1 buffer. We need to check this against the connection's and
3700 * the stream's send windows, and to ensure that this fits in the max
3701 * frame size and in the buffer's available space minus 9 bytes (for
3702 * the frame header). The connection's flow control is applied last so
3703 * that we can use a separate list of streams which are immediately
3704 * unblocked on window opening. Note: we don't implement padding.
3705 */
3706
Willy Tarreau5dd17352018-06-14 13:33:30 +02003707 if (size > max)
3708 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003709
3710 if (size > h2s->mws)
3711 size = h2s->mws;
3712
3713 if (size <= 0) {
3714 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02003715 if (h2s->send_wait) {
3716 LIST_DEL(&h2s->list);
3717 LIST_INIT(&h2s->list);
3718 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003719 goto end;
3720 }
3721
3722 if (h2c->mfs && size > h2c->mfs)
3723 size = h2c->mfs;
3724
3725 if (size + 9 > outbuf.size) {
3726 /* we have an opportunity for enlarging the too small
3727 * available space, let's try.
3728 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003729 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003730 goto realign_again;
3731 size = outbuf.size - 9;
3732 }
3733
3734 if (size <= 0) {
3735 h2c->flags |= H2_CF_MUX_MFULL;
3736 h2s->flags |= H2_SF_BLK_MROOM;
3737 goto end;
3738 }
3739
3740 if (size > h2c->mws)
3741 size = h2c->mws;
3742
3743 if (size <= 0) {
3744 h2s->flags |= H2_SF_BLK_MFCTL;
3745 goto end;
3746 }
3747
3748 /* copy whatever we can */
3749 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003750 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003751 if (ret == 1)
3752 len2 = 0;
3753
3754 if (!ret || len1 + len2 < size) {
3755 /* FIXME: must normally never happen */
3756 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3757 goto end;
3758 }
3759
3760 /* limit len1/len2 to size */
3761 if (len1 + len2 > size) {
3762 int sub = len1 + len2 - size;
3763
3764 if (len2 > sub)
3765 len2 -= sub;
3766 else {
3767 sub -= len2;
3768 len2 = 0;
3769 len1 -= sub;
3770 }
3771 }
3772
3773 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003774 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003775 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003776 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003777
3778 send_empty:
3779 /* we may need to add END_STREAM */
3780 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3781 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003782 *
3783 * FIXME: what we do here is not correct because we send end_stream
3784 * before knowing if we'll have to send a HEADERS frame for the
3785 * trailers. More importantly we're not consuming the trailing CRLF
3786 * after the end of trailers, so it will be left to the caller to
3787 * eat it. The right way to do it would be to measure trailers here
3788 * and to send ES only if there are no trailers.
3789 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003790 */
3791 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02003792 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003793 es_now = 1;
3794
3795 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003796 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003797
3798 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003799 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003800
3801 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003802 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003803
3804 /* consume incoming H1 response */
3805 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003806 max -= size;
3807 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003808 total += size;
3809 h1m->curr_len -= size;
3810 h2s->mws -= size;
3811 h2c->mws -= size;
3812
3813 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02003814 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003815 goto new_frame;
3816 }
3817 }
3818
3819 if (es_now) {
3820 if (h2s->st == H2_SS_OPEN)
3821 h2s->st = H2_SS_HLOC;
3822 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003823 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003824
Willy Tarreau35a62702018-02-27 15:37:25 +01003825 if (!(h1m->flags & H1_MF_CHNK)) {
3826 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003827 total += max;
3828 ofs += max;
3829 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003830
Willy Tarreau801250e2018-09-11 11:45:04 +02003831 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003832 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003833
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003834 h2s->flags |= H2_SF_ES_SENT;
3835 }
3836
3837 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003838 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 +02003839 return total;
3840}
3841
Willy Tarreau115e83b2018-12-01 19:17:53 +01003842/* Try to send a HEADERS frame matching HTX response present in HTX message
3843 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
3844 * must check the stream's status to detect any error which might have happened
3845 * subsequently to a successful send. The htx blocks are automatically removed
3846 * from the message. The htx message is assumed to be valid since produced from
3847 * the internal code, hence it contains a start line, an optional series of
3848 * header blocks and an end of header, otherwise an invalid frame could be
3849 * emitted and the resulting htx message could be left in an inconsistent state.
3850 */
3851static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
3852{
3853 struct http_hdr list[MAX_HTTP_HDR];
3854 struct h2c *h2c = h2s->h2c;
3855 struct htx_blk *blk;
3856 struct htx_blk *blk_end;
3857 struct buffer outbuf;
3858 struct htx_sl *sl;
3859 enum htx_blk_type type;
3860 int es_now = 0;
3861 int ret = 0;
3862 int hdr;
3863 int idx;
3864
3865 if (h2c_mux_busy(h2c, h2s)) {
3866 h2s->flags |= H2_SF_BLK_MBUSY;
3867 return 0;
3868 }
3869
3870 if (!h2_get_buf(h2c, &h2c->mbuf)) {
3871 h2c->flags |= H2_CF_MUX_MALLOC;
3872 h2s->flags |= H2_SF_BLK_MROOM;
3873 return 0;
3874 }
3875
3876 /* determine the first block which must not be deleted, blk_end may
3877 * be NULL if all blocks have to be deleted.
3878 */
3879 idx = htx_get_head(htx);
3880 blk_end = NULL;
3881 while (idx != -1) {
3882 type = htx_get_blk_type(htx_get_blk(htx, idx));
3883 idx = htx_get_next(htx, idx);
3884 if (type == HTX_BLK_EOH) {
3885 if (idx != -1)
3886 blk_end = htx_get_blk(htx, idx);
3887 break;
3888 }
3889 }
3890
3891 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01003892 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01003893 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003894 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01003895 if (h2s->status < 100 || h2s->status > 999)
3896 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01003897
3898 /* and the rest of the headers, that we dump starting at header 0 */
3899 hdr = 0;
3900
Willy Tarreau8e162ee2018-12-06 14:07:27 +01003901 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01003902 while ((idx = htx_get_next(htx, idx)) != -1) {
3903 blk = htx_get_blk(htx, idx);
3904 type = htx_get_blk_type(blk);
3905
3906 if (type == HTX_BLK_UNUSED)
3907 continue;
3908
3909 if (type != HTX_BLK_HDR)
3910 break;
3911
3912 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
3913 goto fail;
3914
3915 list[hdr].n = htx_get_blk_name(htx, blk);
3916 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003917 hdr++;
3918 }
3919
3920 /* marker for end of headers */
3921 list[hdr].n = ist("");
3922
3923 if (h2s->status == 204 || h2s->status == 304) {
3924 /* no contents, claim c-len is present and set to zero */
3925 es_now = 1;
3926 }
3927
3928 chunk_reset(&outbuf);
3929
3930 while (1) {
3931 outbuf.area = b_tail(&h2c->mbuf);
3932 outbuf.size = b_contig_space(&h2c->mbuf);
3933 outbuf.data = 0;
3934
3935 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
3936 break;
3937 realign_again:
3938 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
3939 }
3940
3941 if (outbuf.size < 9)
3942 goto full;
3943
3944 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
3945 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3946 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3947 outbuf.data = 9;
3948
3949 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003950 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01003951 if (b_space_wraps(&h2c->mbuf))
3952 goto realign_again;
3953 goto full;
3954 }
3955
3956 /* encode all headers, stop at empty name */
3957 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
3958 /* these ones do not exist in H2 and must be dropped. */
3959 if (isteq(list[hdr].n, ist("connection")) ||
3960 isteq(list[hdr].n, ist("proxy-connection")) ||
3961 isteq(list[hdr].n, ist("keep-alive")) ||
3962 isteq(list[hdr].n, ist("upgrade")) ||
3963 isteq(list[hdr].n, ist("transfer-encoding")))
3964 continue;
3965
3966 if (isteq(list[hdr].n, ist("")))
3967 break; // end
3968
3969 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3970 /* output full */
3971 if (b_space_wraps(&h2c->mbuf))
3972 goto realign_again;
3973 goto full;
3974 }
3975 }
3976
3977 /* we may need to add END_STREAM.
3978 * FIXME: we should also set it when we know for sure that the
3979 * content-length is zero as well as on 204/304
3980 */
3981 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
3982 es_now = 1;
3983
3984 if (h2s->cs->flags & CS_FL_SHW)
3985 es_now = 1;
3986
3987 /* update the frame's size */
3988 h2_set_frame_size(outbuf.area, outbuf.data - 9);
3989
3990 if (es_now)
3991 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
3992
3993 /* commit the H2 response */
3994 b_add(&h2c->mbuf, outbuf.data);
3995 h2s->flags |= H2_SF_HEADERS_SENT;
3996
3997 /* for now we don't implemented CONTINUATION, so we wait for a
3998 * body or directly end in TRL2.
3999 */
4000 if (es_now) {
4001 h2s->flags |= H2_SF_ES_SENT;
4002 if (h2s->st == H2_SS_OPEN)
4003 h2s->st = H2_SS_HLOC;
4004 else
4005 h2s_close(h2s);
4006 }
4007
4008 /* OK we could properly deliver the response */
4009
4010 /* remove all header blocks including the EOH and compute the
4011 * corresponding size.
4012 *
4013 * FIXME: We should remove everything when es_now is set.
4014 */
4015 ret = 0;
4016 idx = htx_get_head(htx);
4017 blk = htx_get_blk(htx, idx);
4018 while (blk != blk_end) {
4019 ret += htx_get_blksz(blk);
4020 blk = htx_remove_blk(htx, blk);
4021 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004022
4023 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4024 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004025 end:
4026 return ret;
4027 full:
4028 h2c->flags |= H2_CF_MUX_MFULL;
4029 h2s->flags |= H2_SF_BLK_MROOM;
4030 ret = 0;
4031 goto end;
4032 fail:
4033 /* unparsable HTX messages, too large ones to be produced in the local
4034 * list etc go here (unrecoverable errors).
4035 */
4036 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4037 ret = 0;
4038 goto end;
4039}
4040
Willy Tarreau80739692018-10-05 11:35:57 +02004041/* Try to send a HEADERS frame matching HTX request present in HTX message
4042 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4043 * must check the stream's status to detect any error which might have happened
4044 * subsequently to a successful send. The htx blocks are automatically removed
4045 * from the message. The htx message is assumed to be valid since produced from
4046 * the internal code, hence it contains a start line, an optional series of
4047 * header blocks and an end of header, otherwise an invalid frame could be
4048 * emitted and the resulting htx message could be left in an inconsistent state.
4049 */
4050static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4051{
4052 struct http_hdr list[MAX_HTTP_HDR];
4053 struct h2c *h2c = h2s->h2c;
4054 struct htx_blk *blk;
4055 struct htx_blk *blk_end;
4056 struct buffer outbuf;
4057 struct htx_sl *sl;
4058 struct ist meth, path;
4059 enum htx_blk_type type;
4060 int es_now = 0;
4061 int ret = 0;
4062 int hdr;
4063 int idx;
4064
4065 if (h2c_mux_busy(h2c, h2s)) {
4066 h2s->flags |= H2_SF_BLK_MBUSY;
4067 return 0;
4068 }
4069
4070 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4071 h2c->flags |= H2_CF_MUX_MALLOC;
4072 h2s->flags |= H2_SF_BLK_MROOM;
4073 return 0;
4074 }
4075
4076 /* determine the first block which must not be deleted, blk_end may
4077 * be NULL if all blocks have to be deleted.
4078 */
4079 idx = htx_get_head(htx);
4080 blk_end = NULL;
4081 while (idx != -1) {
4082 type = htx_get_blk_type(htx_get_blk(htx, idx));
4083 idx = htx_get_next(htx, idx);
4084 if (type == HTX_BLK_EOH) {
4085 if (idx != -1)
4086 blk_end = htx_get_blk(htx, idx);
4087 break;
4088 }
4089 }
4090
4091 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004092 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004093 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004094 meth = htx_sl_req_meth(sl);
4095 path = htx_sl_req_uri(sl);
4096
4097 /* and the rest of the headers, that we dump starting at header 0 */
4098 hdr = 0;
4099
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004100 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004101 while ((idx = htx_get_next(htx, idx)) != -1) {
4102 blk = htx_get_blk(htx, idx);
4103 type = htx_get_blk_type(blk);
4104
4105 if (type == HTX_BLK_UNUSED)
4106 continue;
4107
4108 if (type != HTX_BLK_HDR)
4109 break;
4110
4111 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4112 goto fail;
4113
4114 list[hdr].n = htx_get_blk_name(htx, blk);
4115 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004116 hdr++;
4117 }
4118
4119 /* marker for end of headers */
4120 list[hdr].n = ist("");
4121
4122 chunk_reset(&outbuf);
4123
4124 while (1) {
4125 outbuf.area = b_tail(&h2c->mbuf);
4126 outbuf.size = b_contig_space(&h2c->mbuf);
4127 outbuf.data = 0;
4128
4129 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4130 break;
4131 realign_again:
4132 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4133 }
4134
4135 if (outbuf.size < 9)
4136 goto full;
4137
4138 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4139 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4140 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4141 outbuf.data = 9;
4142
4143 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004144 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004145 if (b_space_wraps(&h2c->mbuf))
4146 goto realign_again;
4147 goto full;
4148 }
4149
4150 /* encode the scheme which is always "https" (or 0x86 for "http") */
Willy Tarreau7561bcb2018-12-10 19:17:06 +01004151 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4152 /* output full */
4153 if (b_space_wraps(&h2c->mbuf))
4154 goto realign_again;
4155 goto full;
4156 }
Willy Tarreau80739692018-10-05 11:35:57 +02004157
4158 /* encode the path, which necessarily is the second one */
Willy Tarreau90799812018-12-10 19:28:38 +01004159 if (!hpack_encode_path(&outbuf, path)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004160 /* output full */
4161 if (b_space_wraps(&h2c->mbuf))
4162 goto realign_again;
4163 goto full;
4164 }
4165
4166 /* encode all headers, stop at empty name */
4167 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4168 /* these ones do not exist in H2 and must be dropped. */
4169 if (isteq(list[hdr].n, ist("connection")) ||
4170 isteq(list[hdr].n, ist("proxy-connection")) ||
4171 isteq(list[hdr].n, ist("keep-alive")) ||
4172 isteq(list[hdr].n, ist("upgrade")) ||
4173 isteq(list[hdr].n, ist("transfer-encoding")))
4174 continue;
4175
4176 if (isteq(list[hdr].n, ist("")))
4177 break; // end
4178
4179 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4180 /* output full */
4181 if (b_space_wraps(&h2c->mbuf))
4182 goto realign_again;
4183 goto full;
4184 }
4185 }
4186
4187 /* we may need to add END_STREAM if we have no body :
4188 * - request already closed, or :
4189 * - no transfer-encoding, and :
4190 * - no content-length or content-length:0
4191 * Fixme: this doesn't take into account CONNECT requests.
4192 */
4193 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4194 es_now = 1;
4195
4196 if (sl->flags & HTX_SL_F_BODYLESS)
4197 es_now = 1;
4198
4199 if (h2s->cs->flags & CS_FL_SHW)
4200 es_now = 1;
4201
4202 /* update the frame's size */
4203 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4204
4205 if (es_now)
4206 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4207
4208 /* commit the H2 response */
4209 b_add(&h2c->mbuf, outbuf.data);
4210 h2s->flags |= H2_SF_HEADERS_SENT;
4211 h2s->st = H2_SS_OPEN;
4212
4213 /* for now we don't implemented CONTINUATION, so we wait for a
4214 * body or directly end in TRL2.
4215 */
4216 if (es_now) {
4217 // trim any possibly pending data (eg: inconsistent content-length)
4218 h2s->flags |= H2_SF_ES_SENT;
4219 h2s->st = H2_SS_HLOC;
4220 }
4221
4222 /* remove all header blocks including the EOH and compute the
4223 * corresponding size.
4224 *
4225 * FIXME: We should remove everything when es_now is set.
4226 */
4227 ret = 0;
4228 idx = htx_get_head(htx);
4229 blk = htx_get_blk(htx, idx);
4230 while (blk != blk_end) {
4231 ret += htx_get_blksz(blk);
4232 blk = htx_remove_blk(htx, blk);
4233 }
4234
4235 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4236 htx_remove_blk(htx, blk_end);
4237
4238 end:
4239 return ret;
4240 full:
4241 h2c->flags |= H2_CF_MUX_MFULL;
4242 h2s->flags |= H2_SF_BLK_MROOM;
4243 ret = 0;
4244 goto end;
4245 fail:
4246 /* unparsable HTX messages, too large ones to be produced in the local
4247 * list etc go here (unrecoverable errors).
4248 */
4249 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4250 ret = 0;
4251 goto end;
4252}
4253
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004254/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004255 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4256 * caller must check the stream's status to detect any error which might have
4257 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004258 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4259 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004260static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004261{
4262 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004263 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004264 struct buffer outbuf;
4265 size_t total = 0;
4266 int es_now = 0;
4267 int bsize; /* htx block size */
4268 int fsize; /* h2 frame size */
4269 struct htx_blk *blk;
4270 enum htx_blk_type type;
4271 int idx;
4272
4273 if (h2c_mux_busy(h2c, h2s)) {
4274 h2s->flags |= H2_SF_BLK_MBUSY;
4275 goto end;
4276 }
4277
4278 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4279 h2c->flags |= H2_CF_MUX_MALLOC;
4280 h2s->flags |= H2_SF_BLK_MROOM;
4281 goto end;
4282 }
4283
Willy Tarreau98de12a2018-12-12 07:03:00 +01004284 htx = htx_from_buf(buf);
4285
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004286 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4287 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4288 * the caller to handle.
4289 */
4290
4291 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004292 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004293 goto end;
4294
4295 idx = htx_get_head(htx);
4296 blk = htx_get_blk(htx, idx);
4297 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4298 bsize = htx_get_blksz(blk);
4299 fsize = bsize;
4300
4301 if (type == HTX_BLK_EOD) {
4302 /* if we have an EOD, we're dealing with chunked data. We may
4303 * have a set of trailers after us that the caller will want to
4304 * deal with. Let's simply remove the EOD and return.
4305 */
4306 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004307 total++; // EOD counts as one byte
4308 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004309 goto end;
4310 }
4311
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004312 if (type != HTX_BLK_DATA && type != HTX_BLK_EOM)
4313 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004314
4315 /* Perform some optimizations to reduce the number of buffer copies.
4316 * First, if the mux's buffer is empty and the htx area contains
4317 * exactly one data block of the same size as the requested count, and
4318 * this count fits within the frame size, the stream's window size, and
4319 * the connection's window size, then it's possible to simply swap the
4320 * caller's buffer with the mux's output buffer and adjust offsets and
4321 * length to match the entire DATA HTX block in the middle. In this
4322 * case we perform a true zero-copy operation from end-to-end. This is
4323 * the situation that happens all the time with large files. Second, if
4324 * this is not possible, but the mux's output buffer is empty, we still
4325 * have an opportunity to avoid the copy to the intermediary buffer, by
4326 * making the intermediary buffer's area point to the output buffer's
4327 * area. In this case we want to skip the HTX header to make sure that
4328 * copies remain aligned and that this operation remains possible all
4329 * the time. This goes for headers, data blocks and any data extracted
4330 * from the HTX blocks.
4331 */
4332 if (unlikely(fsize == count &&
4333 htx->used == 1 && type == HTX_BLK_DATA &&
4334 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4335 void *old_area = h2c->mbuf.area;
4336
4337 if (b_data(&h2c->mbuf)) {
4338 /* too bad there are data left there. If we have less
4339 * than 1/4 of the mbuf's size and everything fits,
4340 * we'll perform a copy anyway. Otherwise we'll pretend
4341 * the mbuf is full and wait.
4342 */
4343 if (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_room(&h2c->mbuf))
4344 goto copy;
4345 h2c->flags |= H2_CF_MUX_MFULL;
4346 h2s->flags |= H2_SF_BLK_MROOM;
4347 goto end;
4348 }
4349
4350 /* map an H2 frame to the HTX block so that we can put the
4351 * frame header there.
4352 */
4353 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004354 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004355 h2c->mbuf.data = fsize + 9;
4356 outbuf.area = b_head(&h2c->mbuf);
4357
4358 /* prepend an H2 DATA frame header just before the DATA block */
4359 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4360 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4361 h2_set_frame_size(outbuf.area, fsize);
4362
4363 /* update windows */
4364 h2s->mws -= fsize;
4365 h2c->mws -= fsize;
4366
4367 /* and exchange with our old area */
4368 buf->area = old_area;
4369 buf->data = buf->head = 0;
4370 total += fsize;
4371 goto end;
4372 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004373
Willy Tarreau98de12a2018-12-12 07:03:00 +01004374 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004375 /* for DATA and EOM we'll have to emit a frame, even if empty */
4376
4377 while (1) {
4378 outbuf.area = b_tail(&h2c->mbuf);
4379 outbuf.size = b_contig_space(&h2c->mbuf);
4380 outbuf.data = 0;
4381
4382 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4383 break;
4384 realign_again:
4385 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4386 }
4387
4388 if (outbuf.size < 9) {
4389 h2c->flags |= H2_CF_MUX_MFULL;
4390 h2s->flags |= H2_SF_BLK_MROOM;
4391 goto end;
4392 }
4393
4394 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4395 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4396 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4397 outbuf.data = 9;
4398
4399 /* we have in <fsize> the exact number of bytes we need to copy from
4400 * the HTX buffer. We need to check this against the connection's and
4401 * the stream's send windows, and to ensure that this fits in the max
4402 * frame size and in the buffer's available space minus 9 bytes (for
4403 * the frame header). The connection's flow control is applied last so
4404 * that we can use a separate list of streams which are immediately
4405 * unblocked on window opening. Note: we don't implement padding.
4406 */
4407
4408 /* EOM is presented with bsize==1 but would lead to the emission of an
4409 * empty frame, thus we force it to zero here.
4410 */
4411 if (type == HTX_BLK_EOM)
4412 bsize = fsize = 0;
4413
4414 if (!fsize)
4415 goto send_empty;
4416
4417 if (h2s->mws <= 0) {
4418 h2s->flags |= H2_SF_BLK_SFCTL;
4419 if (h2s->send_wait) {
4420 LIST_DEL(&h2s->list);
4421 LIST_INIT(&h2s->list);
4422 }
4423 goto end;
4424 }
4425
Willy Tarreauee573762018-12-04 15:25:57 +01004426 if (fsize > count)
4427 fsize = count;
4428
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004429 if (fsize > h2s->mws)
4430 fsize = h2s->mws; // >0
4431
4432 if (h2c->mfs && fsize > h2c->mfs)
4433 fsize = h2c->mfs; // >0
4434
4435 if (fsize + 9 > outbuf.size) {
4436 /* we have an opportunity for enlarging the too small
4437 * available space, let's try.
4438 * FIXME: is this really interesting to do? Maybe we'll
4439 * spend lots of time realigning instead of using two
4440 * frames.
4441 */
4442 if (b_space_wraps(&h2c->mbuf))
4443 goto realign_again;
4444 fsize = outbuf.size - 9;
4445
4446 if (fsize <= 0) {
4447 /* no need to send an empty frame here */
4448 h2c->flags |= H2_CF_MUX_MFULL;
4449 h2s->flags |= H2_SF_BLK_MROOM;
4450 goto end;
4451 }
4452 }
4453
4454 if (h2c->mws <= 0) {
4455 h2s->flags |= H2_SF_BLK_MFCTL;
4456 goto end;
4457 }
4458
4459 if (fsize > h2c->mws)
4460 fsize = h2c->mws;
4461
4462 /* now let's copy this this into the output buffer */
4463 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004464 h2s->mws -= fsize;
4465 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004466 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004467
4468 send_empty:
4469 /* update the frame's size */
4470 h2_set_frame_size(outbuf.area, fsize);
4471
4472 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4473 * meeting EOM. We should optimize this later.
4474 */
4475 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004476 total++; // EOM counts as one byte
4477 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004478 es_now = 1;
4479 }
4480
4481 if (es_now)
4482 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4483
4484 /* commit the H2 response */
4485 b_add(&h2c->mbuf, fsize + 9);
4486
4487 /* consume incoming HTX block, including EOM */
4488 total += fsize;
4489 if (fsize == bsize) {
4490 htx_remove_blk(htx, blk);
4491 if (fsize)
4492 goto new_frame;
4493 } else {
4494 /* we've truncated this block */
4495 htx_cut_data_blk(htx, blk, fsize);
4496 }
4497
4498 if (es_now) {
4499 if (h2s->st == H2_SS_OPEN)
4500 h2s->st = H2_SS_HLOC;
4501 else
4502 h2s_close(h2s);
4503
4504 h2s->flags |= H2_SF_ES_SENT;
4505 }
4506
4507 end:
4508 return total;
4509}
4510
Olivier Houchard6ff20392018-07-17 18:46:31 +02004511/* Called from the upper layer, to subscribe to events, such as being able to send */
4512static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
4513{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004514 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004515 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004516 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004517
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004518 if (event_type & SUB_CAN_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004519 sw = param;
4520 if (!(sw->wait_reason & SUB_CAN_RECV)) {
4521 sw->wait_reason |= SUB_CAN_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004522 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004523 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004524 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004525 event_type &= ~SUB_CAN_RECV;
4526 }
4527 if (event_type & SUB_CAN_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02004528 sw = param;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004529 if (!(sw->wait_reason & SUB_CAN_SEND)) {
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004530 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004531 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004532 h2s->send_wait = sw;
4533 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
4534 if (h2s->flags & H2_SF_BLK_MFCTL)
4535 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
4536 else
4537 LIST_ADDQ(&h2c->send_list, &h2s->list);
4538 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004539 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004540 event_type &= ~SUB_CAN_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004541 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004542 if (event_type != 0)
4543 return -1;
4544 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004545
4546
4547}
4548
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004549static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
4550{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004551 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004552 struct h2s *h2s = cs->ctx;
4553
4554 if (event_type & SUB_CAN_RECV) {
4555 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004556 if (h2s->recv_wait == sw) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004557 sw->wait_reason &= ~SUB_CAN_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004558 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004559 }
4560 }
4561 if (event_type & SUB_CAN_SEND) {
4562 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004563 if (h2s->send_wait == sw) {
4564 LIST_DEL(&h2s->list);
4565 LIST_INIT(&h2s->list);
4566 sw->wait_reason &= ~SUB_CAN_SEND;
4567 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004568 }
4569 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004570 if (event_type & SUB_CALL_UNSUBSCRIBE) {
4571 sw = param;
4572 if (h2s->send_wait == sw) {
4573 sw->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
4574 h2s->send_wait = NULL;
4575 }
4576 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004577 return 0;
4578}
4579
4580
Olivier Houchard511efea2018-08-16 15:30:32 +02004581/* Called from the upper layer, to receive data */
4582static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
4583{
Olivier Houchard638b7992018-08-16 15:41:52 +02004584 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01004585 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01004586 struct htx *h2s_htx = NULL;
4587 struct htx *buf_htx = NULL;
4588 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02004589 size_t ret = 0;
4590
4591 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01004592 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
4593 /* in HTX mode we ignore the count argument */
4594 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01004595 if (htx_is_empty(h2s_htx)) {
4596 if (cs->flags & CS_FL_REOS)
4597 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01004598 if (cs->flags & CS_FL_ERR_PENDING)
4599 cs->flags |= CS_FL_ERROR;
Willy Tarreau86724e22018-12-01 23:19:43 +01004600 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01004601 }
Willy Tarreau86724e22018-12-01 23:19:43 +01004602
4603 buf_htx = htx_from_buf(buf);
4604 count = htx_free_space(buf_htx);
4605
Willy Tarreau0c22fa72018-12-04 15:21:35 +01004606 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau86724e22018-12-01 23:19:43 +01004607
4608 buf_htx->extra = h2s_htx->extra;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004609 htx_to_buf(buf_htx, buf);
4610 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01004611 ret = htx_ret.ret;
4612 }
4613 else {
4614 ret = b_xfer(buf, &h2s->rxbuf, count);
4615 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004616
Olivier Houchard638b7992018-08-16 15:41:52 +02004617 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01004618 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02004619 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01004620 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02004621 if (cs->flags & CS_FL_REOS)
4622 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01004623 if (cs->flags & CS_FL_ERR_PENDING)
4624 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02004625 if (b_size(&h2s->rxbuf)) {
4626 b_free(&h2s->rxbuf);
4627 offer_buffers(NULL, tasks_run_queue);
4628 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004629 }
4630
Willy Tarreau082f5592018-11-25 08:03:32 +01004631 if (ret && h2c->dsi == h2s->id) {
4632 /* demux is blocking on this stream's buffer */
4633 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreauc5b10042018-12-18 10:27:18 +01004634 if (b_data(&h2c->dbuf) || !(h2c->wait_event.wait_reason & SUB_CAN_RECV)) {
Willy Tarreau082f5592018-11-25 08:03:32 +01004635 if (h2_recv_allowed(h2c))
4636 tasklet_wakeup(h2c->wait_event.task);
4637 }
4638 }
Willy Tarreau86724e22018-12-01 23:19:43 +01004639end:
Olivier Houchard511efea2018-08-16 15:30:32 +02004640 return ret;
4641}
4642
Olivier Houchardd846c262018-10-19 17:24:29 +02004643static void h2_stop_senders(struct h2c *h2c)
4644{
4645 struct h2s *h2s, *h2s_back;
4646
4647 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
4648 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
4649 if (h2c->msi == h2s_id(h2s))
4650 continue;
4651 LIST_DEL(&h2s->list);
4652 LIST_INIT(&h2s->list);
4653 task_remove_from_task_list((struct task *)h2s->send_wait->task);
4654 h2s->send_wait->wait_reason |= SUB_CAN_SEND;
4655 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
4656 LIST_ADD(&h2c->send_list, &h2s->list);
4657 }
4658}
4659
Willy Tarreau62f52692017-10-08 23:01:42 +02004660/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02004661static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02004662{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004663 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01004664 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004665 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004666 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004667 struct htx *htx;
4668 struct htx_blk *blk;
4669 enum htx_blk_type btype;
4670 uint32_t bsize;
4671 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004672
Olivier Houchardd846c262018-10-19 17:24:29 +02004673 if (h2s->send_wait) {
4674 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
4675 h2s->send_wait = NULL;
4676 LIST_DEL(&h2s->list);
4677 LIST_INIT(&h2s->list);
4678 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02004679 if (h2s->h2c->st0 < H2_CS_FRAME_H)
4680 return 0;
4681
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004682 /* htx will be enough to decide if we're using HTX or legacy */
4683 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
4684
Willy Tarreau0bad0432018-06-14 16:54:01 +02004685 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01004686 h2s->flags |= H2_SF_OUTGOING_DATA;
4687
Willy Tarreau751f2d02018-10-05 09:35:00 +02004688 if (h2s->id == 0) {
4689 int32_t id = h2c_get_next_sid(h2s->h2c);
4690
4691 if (id < 0) {
4692 cs->ctx = NULL;
4693 cs->flags |= CS_FL_ERROR;
4694 h2s_destroy(h2s);
4695 return 0;
4696 }
4697
4698 eb32_delete(&h2s->by_id);
4699 h2s->by_id.key = h2s->id = id;
4700 h2s->h2c->max_id = id;
4701 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
4702 }
4703
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004704 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01004705 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
4706 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004707 idx = htx_get_head(htx);
4708 blk = htx_get_blk(htx, idx);
4709 btype = htx_get_blk_type(blk);
4710 bsize = htx_get_blksz(blk);
4711
4712 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02004713 case HTX_BLK_REQ_SL:
4714 /* start-line before headers */
4715 ret = h2s_htx_bck_make_req_headers(h2s, htx);
4716 if (ret > 0) {
4717 total += ret;
4718 count -= ret;
4719 if (ret < bsize)
4720 goto done;
4721 }
4722 break;
4723
Willy Tarreau115e83b2018-12-01 19:17:53 +01004724 case HTX_BLK_RES_SL:
4725 /* start-line before headers */
4726 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
4727 if (ret > 0) {
4728 total += ret;
4729 count -= ret;
4730 if (ret < bsize)
4731 goto done;
4732 }
4733 break;
4734
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004735 case HTX_BLK_DATA:
4736 case HTX_BLK_EOD:
4737 case HTX_BLK_EOM:
4738 /* all these cause the emission of a DATA frame (possibly empty) */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004739 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004740 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01004741 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004742 total += ret;
4743 count -= ret;
4744 if (ret < bsize)
4745 goto done;
4746 }
4747 break;
4748
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004749 default:
4750 htx_remove_blk(htx, blk);
4751 total += bsize;
4752 count -= bsize;
4753 break;
4754 }
4755 }
4756 goto done;
4757 }
4758
4759 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02004760 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02004761 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02004762 if (h2s->h2c->flags & H2_CF_IS_BACK)
4763 ret = -1;
4764 else
4765 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004766 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004767 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02004768 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004769 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004770 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004771 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004772 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004773
Willy Tarreau5dd17352018-06-14 13:33:30 +02004774 if (unlikely((int)ret <= 0)) {
4775 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004776 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4777 break;
4778 }
Willy Tarreau35a62702018-02-27 15:37:25 +01004779 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02004780 total += count;
4781 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004782 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004783 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004784 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004785 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004786 cs->flags |= CS_FL_ERROR;
4787 break;
4788 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02004789
4790 total += ret;
4791 count -= ret;
4792
4793 if (h2s->st >= H2_SS_ERROR)
4794 break;
4795
4796 if (h2s->flags & H2_SF_BLK_ANY)
4797 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004798 }
4799
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004800 done:
Willy Tarreau00610962018-07-19 10:58:28 +02004801 if (h2s->st >= H2_SS_ERROR) {
4802 /* trim any possibly pending data after we close (extra CR-LF,
4803 * unprocessed trailers, abnormal extra data, ...)
4804 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004805 total += count;
4806 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02004807 }
4808
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004809 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01004810 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004811 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01004812 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01004813 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004814 }
4815
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004816 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004817 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004818 } else {
4819 b_del(buf, total);
4820 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004821
4822 /* The mux is full, cancel the pending tasks */
4823 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
4824 (h2s->flags & H2_SF_BLK_MBUSY))
4825 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004826
Olivier Houchard8122a8d2018-12-03 19:13:29 +01004827 /* If we're running HTX, and we read the whole buffer, then pretend
4828 * we read exactly what the caller specified, as with HTX the caller
4829 * will always give the buffer size, instead of the amount of data
4830 * available.
4831 */
4832 if (htx && !b_data(buf))
4833 total = orig_count;
4834
Olivier Houchard7505f942018-08-21 18:10:44 +02004835 if (total > 0) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004836 if (!(h2s->h2c->wait_event.wait_reason & SUB_CAN_SEND))
4837 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02004838
Olivier Houchard7505f942018-08-21 18:10:44 +02004839 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004840 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02004841}
4842
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004843/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02004844static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004845{
4846 struct h2c *h2c = conn->mux_ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01004847 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004848 struct eb32_node *node;
4849 int fctl_cnt = 0;
4850 int send_cnt = 0;
4851 int tree_cnt = 0;
4852 int orph_cnt = 0;
4853
4854 if (!h2c)
4855 return;
4856
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004857 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004858 fctl_cnt++;
4859
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004860 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004861 send_cnt++;
4862
Willy Tarreau3af37712018-12-18 14:34:41 +01004863 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004864 node = eb32_first(&h2c->streams_by_id);
4865 while (node) {
4866 h2s = container_of(node, struct h2s, by_id);
4867 tree_cnt++;
4868 if (!h2s->cs)
4869 orph_cnt++;
4870 node = eb32_next(node);
4871 }
4872
Willy Tarreau987c0632018-12-18 10:32:05 +01004873 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
4874 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
4875 " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d .mbuf=%u@%p+%u/%u",
Willy Tarreau616ac812018-07-24 14:12:42 +02004876 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
4877 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau987c0632018-12-18 10:32:05 +01004878 h2c->wait_event.wait_reason, h2c->dsi,
4879 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
4880 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
4881 h2c->msi,
4882 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
4883 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
4884
4885 if (h2s) {
4886 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
4887 h2s, h2s->id, h2s->flags,
4888 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
4889 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
4890 h2s->cs);
4891 if (h2s->cs)
4892 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
4893 h2s->cs->flags, h2s->cs->data);
4894 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004895}
Willy Tarreau62f52692017-10-08 23:01:42 +02004896
4897/*******************************************************/
4898/* functions below are dedicated to the config parsers */
4899/*******************************************************/
4900
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02004901/* config parser for global "tune.h2.header-table-size" */
4902static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
4903 struct proxy *defpx, const char *file, int line,
4904 char **err)
4905{
4906 if (too_many_args(1, args, err, NULL))
4907 return -1;
4908
4909 h2_settings_header_table_size = atoi(args[1]);
4910 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
4911 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
4912 return -1;
4913 }
4914 return 0;
4915}
Willy Tarreau62f52692017-10-08 23:01:42 +02004916
Willy Tarreaue6baec02017-07-27 11:45:11 +02004917/* config parser for global "tune.h2.initial-window-size" */
4918static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
4919 struct proxy *defpx, const char *file, int line,
4920 char **err)
4921{
4922 if (too_many_args(1, args, err, NULL))
4923 return -1;
4924
4925 h2_settings_initial_window_size = atoi(args[1]);
4926 if (h2_settings_initial_window_size < 0) {
4927 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
4928 return -1;
4929 }
4930 return 0;
4931}
4932
Willy Tarreau5242ef82017-07-27 11:47:28 +02004933/* config parser for global "tune.h2.max-concurrent-streams" */
4934static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
4935 struct proxy *defpx, const char *file, int line,
4936 char **err)
4937{
4938 if (too_many_args(1, args, err, NULL))
4939 return -1;
4940
4941 h2_settings_max_concurrent_streams = atoi(args[1]);
4942 if (h2_settings_max_concurrent_streams < 0) {
4943 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
4944 return -1;
4945 }
4946 return 0;
4947}
4948
Willy Tarreau62f52692017-10-08 23:01:42 +02004949
4950/****************************************/
4951/* MUX initialization and instanciation */
4952/***************************************/
4953
4954/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01004955static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02004956 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02004957 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02004958 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02004959 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02004960 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004961 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02004962 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01004963 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02004964 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01004965 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01004966 .avail_streams = h2_avail_streams,
Olivier Houchard8defe4b2018-12-02 01:31:17 +01004967 .max_streams = h2_max_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02004968 .shutr = h2_shutr,
4969 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004970 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01004971 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02004972 .name = "H2",
4973};
4974
Christopher Faulet32f61c02018-04-10 14:33:41 +02004975/* PROTO selection : this mux registers PROTO token "h2" */
4976static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02004977 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02004978
Willy Tarreau0108d902018-11-25 19:14:37 +01004979INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
4980
Willy Tarreauf8957272018-10-03 10:25:20 +02004981static struct mux_proto_list mux_proto_h2_htx =
4982 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
4983
4984INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
4985
Willy Tarreau62f52692017-10-08 23:01:42 +02004986/* config keyword parsers */
4987static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02004988 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02004989 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02004990 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02004991 { 0, NULL, NULL }
4992}};
4993
Willy Tarreau0108d902018-11-25 19:14:37 +01004994INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);