blob: 3509db5a6eaa0568ee9ae8a5c855aeeec4ff8fae [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
799 h2s->cs = cs;
800 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200801 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200802
803 if (stream_create_from_cs(cs) < 0)
804 goto out_free_cs;
805
Willy Tarreau590a0512018-09-05 11:56:48 +0200806 /* We want the accept date presented to the next stream to be the one
807 * we have now, the handshake time to be null (since the next stream
808 * is not delayed by a handshake), and the idle time to count since
809 * right now.
810 */
811 sess->accept_date = date;
812 sess->tv_accept = now;
813 sess->t_handshake = 0;
814
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200815 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200816 if (h2_has_too_many_cs(h2c))
817 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200818 return h2s;
819
820 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200821 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200822 cs_free(cs);
823 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200824 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200825 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200826 sess_log(sess);
827 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200828}
829
Willy Tarreau751f2d02018-10-05 09:35:00 +0200830/* allocates a new stream associated to conn_stream <cs> on the h2c connection
831 * and returns it, or NULL in case of memory allocation error or if the highest
832 * possible stream ID was reached.
833 */
Olivier Houchardf502aca2018-12-14 19:42:40 +0100834static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +0200835{
836 struct h2s *h2s = NULL;
837
838 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
839 goto out;
840
841 /* Defer choosing the ID until we send the first message to create the stream */
842 h2s = h2s_new(h2c, 0);
843 if (!h2s)
844 goto out;
845
846 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100847 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200848 cs->ctx = h2s;
849 h2c->nb_cs++;
850
Willy Tarreau751f2d02018-10-05 09:35:00 +0200851 out:
852 return h2s;
853}
854
Willy Tarreaube5b7152017-09-25 16:25:39 +0200855/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
856 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
857 * the various settings codes.
858 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200859static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +0200860{
861 struct buffer *res;
862 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200863 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200864 int ret;
865
866 if (h2c_mux_busy(h2c, NULL)) {
867 h2c->flags |= H2_CF_DEM_MBUSY;
868 return 0;
869 }
870
Willy Tarreau44e973f2018-03-01 17:49:30 +0100871 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200872 if (!res) {
873 h2c->flags |= H2_CF_MUX_MALLOC;
874 h2c->flags |= H2_CF_DEM_MROOM;
875 return 0;
876 }
877
878 chunk_init(&buf, buf_data, sizeof(buf_data));
879 chunk_memcpy(&buf,
880 "\x00\x00\x00" /* length : 0 for now */
881 "\x04\x00" /* type : 4 (settings), flags : 0 */
882 "\x00\x00\x00\x00", /* stream ID : 0 */
883 9);
884
885 if (h2_settings_header_table_size != 4096) {
886 char str[6] = "\x00\x01"; /* header_table_size */
887
888 write_n32(str + 2, h2_settings_header_table_size);
889 chunk_memcat(&buf, str, 6);
890 }
891
892 if (h2_settings_initial_window_size != 65535) {
893 char str[6] = "\x00\x04"; /* initial_window_size */
894
895 write_n32(str + 2, h2_settings_initial_window_size);
896 chunk_memcat(&buf, str, 6);
897 }
898
899 if (h2_settings_max_concurrent_streams != 0) {
900 char str[6] = "\x00\x03"; /* max_concurrent_streams */
901
902 /* Note: 0 means "unlimited" for haproxy's config but not for
903 * the protocol, so never send this value!
904 */
905 write_n32(str + 2, h2_settings_max_concurrent_streams);
906 chunk_memcat(&buf, str, 6);
907 }
908
909 if (global.tune.bufsize != 16384) {
910 char str[6] = "\x00\x05"; /* max_frame_size */
911
912 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
913 * match bufsize - rewrite size, but at the moment it seems
914 * that clients don't take care of it.
915 */
916 write_n32(str + 2, global.tune.bufsize);
917 chunk_memcat(&buf, str, 6);
918 }
919
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200920 h2_set_frame_size(buf.area, buf.data - 9);
921 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200922 if (unlikely(ret <= 0)) {
923 if (!ret) {
924 h2c->flags |= H2_CF_MUX_MFULL;
925 h2c->flags |= H2_CF_DEM_MROOM;
926 return 0;
927 }
928 else {
929 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
930 return 0;
931 }
932 }
933 return ret;
934}
935
Willy Tarreau52eed752017-09-22 15:05:09 +0200936/* Try to receive a connection preface, then upon success try to send our
937 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
938 * missing data. It may return an error in h2c.
939 */
940static int h2c_frt_recv_preface(struct h2c *h2c)
941{
942 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200943 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200944
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200945 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200946
947 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +0200948 if (ret1 < 0)
949 sess_log(h2c->conn->owner);
950
Willy Tarreau52eed752017-09-22 15:05:09 +0200951 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
952 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
953 return 0;
954 }
955
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200956 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200957 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200958 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200959
Willy Tarreaube5b7152017-09-25 16:25:39 +0200960 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200961}
962
Willy Tarreau01b44822018-10-03 14:26:37 +0200963/* Try to send a connection preface, then upon success try to send our
964 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
965 * missing data. It may return an error in h2c.
966 */
967static int h2c_bck_send_preface(struct h2c *h2c)
968{
969 struct buffer *res;
970
971 if (h2c_mux_busy(h2c, NULL)) {
972 h2c->flags |= H2_CF_DEM_MBUSY;
973 return 0;
974 }
975
976 res = h2_get_buf(h2c, &h2c->mbuf);
977 if (!res) {
978 h2c->flags |= H2_CF_MUX_MALLOC;
979 h2c->flags |= H2_CF_DEM_MROOM;
980 return 0;
981 }
982
983 if (!b_data(res)) {
984 /* preface not yet sent */
985 b_istput(res, ist(H2_CONN_PREFACE));
986 }
987
988 return h2c_send_settings(h2c);
989}
990
Willy Tarreau081d4722017-05-16 21:51:05 +0200991/* try to send a GOAWAY frame on the connection to report an error or a graceful
992 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
993 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
994 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
995 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
996 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
997 * on unrecoverable failure. It will not attempt to send one again in this last
998 * case so that it is safe to use h2c_error() to report such errors.
999 */
1000static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1001{
1002 struct buffer *res;
1003 char str[17];
1004 int ret;
1005
1006 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1007 return 1; // claim that it worked
1008
1009 if (h2c_mux_busy(h2c, h2s)) {
1010 if (h2s)
1011 h2s->flags |= H2_SF_BLK_MBUSY;
1012 else
1013 h2c->flags |= H2_CF_DEM_MBUSY;
1014 return 0;
1015 }
1016
Willy Tarreau44e973f2018-03-01 17:49:30 +01001017 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001018 if (!res) {
1019 h2c->flags |= H2_CF_MUX_MALLOC;
1020 if (h2s)
1021 h2s->flags |= H2_SF_BLK_MROOM;
1022 else
1023 h2c->flags |= H2_CF_DEM_MROOM;
1024 return 0;
1025 }
1026
1027 /* len: 8, type: 7, flags: none, sid: 0 */
1028 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1029
1030 if (h2c->last_sid < 0)
1031 h2c->last_sid = h2c->max_id;
1032
1033 write_n32(str + 9, h2c->last_sid);
1034 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001035 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001036 if (unlikely(ret <= 0)) {
1037 if (!ret) {
1038 h2c->flags |= H2_CF_MUX_MFULL;
1039 if (h2s)
1040 h2s->flags |= H2_SF_BLK_MROOM;
1041 else
1042 h2c->flags |= H2_CF_DEM_MROOM;
1043 return 0;
1044 }
1045 else {
1046 /* we cannot report this error using GOAWAY, so we mark
1047 * it and claim a success.
1048 */
1049 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1050 h2c->flags |= H2_CF_GOAWAY_FAILED;
1051 return 1;
1052 }
1053 }
1054 h2c->flags |= H2_CF_GOAWAY_SENT;
1055 return ret;
1056}
1057
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001058/* Try to send an RST_STREAM frame on the connection for the indicated stream
1059 * during mux operations. This stream must be valid and cannot be closed
1060 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1061 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1062 * not yet.
1063 *
1064 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1065 * to write the message, it subscribes the stream to future notifications.
1066 */
1067static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1068{
1069 struct buffer *res;
1070 char str[13];
1071 int ret;
1072
1073 if (!h2s || h2s->st == H2_SS_CLOSED)
1074 return 1;
1075
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001076 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1077 * RST_STREAM in response to a RST_STREAM frame.
1078 */
1079 if (h2c->dft == H2_FT_RST_STREAM) {
1080 ret = 1;
1081 goto ignore;
1082 }
1083
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001084 if (h2c_mux_busy(h2c, h2s)) {
1085 h2s->flags |= H2_SF_BLK_MBUSY;
1086 return 0;
1087 }
1088
Willy Tarreau44e973f2018-03-01 17:49:30 +01001089 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001090 if (!res) {
1091 h2c->flags |= H2_CF_MUX_MALLOC;
1092 h2s->flags |= H2_SF_BLK_MROOM;
1093 return 0;
1094 }
1095
1096 /* len: 4, type: 3, flags: none */
1097 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1098 write_n32(str + 5, h2s->id);
1099 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001100 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001101
1102 if (unlikely(ret <= 0)) {
1103 if (!ret) {
1104 h2c->flags |= H2_CF_MUX_MFULL;
1105 h2s->flags |= H2_SF_BLK_MROOM;
1106 return 0;
1107 }
1108 else {
1109 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1110 return 0;
1111 }
1112 }
1113
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001114 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001115 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001116 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001117 return ret;
1118}
1119
1120/* Try to send an RST_STREAM frame on the connection for the stream being
1121 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
1122 * error code unless the stream's state already is IDLE or CLOSED in which
1123 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
1124 * it was not yet.
1125 *
1126 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1127 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001128 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001129 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001130 */
1131static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1132{
1133 struct buffer *res;
1134 char str[13];
1135 int ret;
1136
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001137 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1138 * RST_STREAM in response to a RST_STREAM frame.
1139 */
1140 if (h2c->dft == H2_FT_RST_STREAM) {
1141 ret = 1;
1142 goto ignore;
1143 }
1144
Willy Tarreau27a84c92017-10-17 08:10:17 +02001145 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001146 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001147 return 0;
1148 }
1149
Willy Tarreau44e973f2018-03-01 17:49:30 +01001150 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001151 if (!res) {
1152 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001153 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001154 return 0;
1155 }
1156
1157 /* len: 4, type: 3, flags: none */
1158 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001159
Willy Tarreau27a84c92017-10-17 08:10:17 +02001160 write_n32(str + 5, h2c->dsi);
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001161 write_n32(str + 9, h2s->id ? h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001162 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001163
Willy Tarreau27a84c92017-10-17 08:10:17 +02001164 if (unlikely(ret <= 0)) {
1165 if (!ret) {
1166 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001167 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001168 return 0;
1169 }
1170 else {
1171 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1172 return 0;
1173 }
1174 }
1175
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001176 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001177 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001178 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001179 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001180 }
1181
Willy Tarreau27a84c92017-10-17 08:10:17 +02001182 return ret;
1183}
1184
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001185/* try to send an empty DATA frame with the ES flag set to notify about the
1186 * end of stream and match a shutdown(write). If an ES was already sent as
1187 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1188 * on success or zero if nothing was done. In case of lack of room to write the
1189 * message, it subscribes the requesting stream to future notifications.
1190 */
1191static int h2_send_empty_data_es(struct h2s *h2s)
1192{
1193 struct h2c *h2c = h2s->h2c;
1194 struct buffer *res;
1195 char str[9];
1196 int ret;
1197
Willy Tarreau721c9742017-11-07 11:05:42 +01001198 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001199 return 1;
1200
1201 if (h2c_mux_busy(h2c, h2s)) {
1202 h2s->flags |= H2_SF_BLK_MBUSY;
1203 return 0;
1204 }
1205
Willy Tarreau44e973f2018-03-01 17:49:30 +01001206 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001207 if (!res) {
1208 h2c->flags |= H2_CF_MUX_MALLOC;
1209 h2s->flags |= H2_SF_BLK_MROOM;
1210 return 0;
1211 }
1212
1213 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1214 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1215 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001216 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001217 if (likely(ret > 0)) {
1218 h2s->flags |= H2_SF_ES_SENT;
1219 }
1220 else if (!ret) {
1221 h2c->flags |= H2_CF_MUX_MFULL;
1222 h2s->flags |= H2_SF_BLK_MROOM;
1223 return 0;
1224 }
1225 else {
1226 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1227 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001228 }
1229 return ret;
1230}
1231
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001232/* wake the streams attached to the connection, whose id is greater than <last>,
1233 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001234 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1235 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001236 */
1237static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1238{
1239 struct eb32_node *node;
1240 struct h2s *h2s;
1241
1242 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1243 flags |= CS_FL_ERROR;
1244
1245 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001246 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001247
1248 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1249 while (node) {
1250 h2s = container_of(node, struct h2s, by_id);
1251 if (h2s->id <= last)
1252 break;
1253 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001254
1255 if (!h2s->cs) {
1256 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001257 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001258 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001259 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001260
1261 h2s->cs->flags |= flags;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001262 if (h2s->recv_wait) {
1263 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001264 sw->wait_reason &= ~SUB_CAN_RECV;
1265 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001266 h2s->recv_wait = NULL;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02001267 } else if (h2s->cs->data_cb->wake != NULL)
1268 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001269
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001270 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1271 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001272 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001273 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001274 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001275 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001276 }
1277}
1278
Willy Tarreau3421aba2017-07-27 15:41:03 +02001279/* Increase all streams' outgoing window size by the difference passed in
1280 * argument. This is needed upon receipt of the settings frame if the initial
1281 * window size is different. The difference may be negative and the resulting
1282 * window size as well, for the time it takes to receive some window updates.
1283 */
1284static void h2c_update_all_ws(struct h2c *h2c, int diff)
1285{
1286 struct h2s *h2s;
1287 struct eb32_node *node;
1288
1289 if (!diff)
1290 return;
1291
1292 node = eb32_first(&h2c->streams_by_id);
1293 while (node) {
1294 h2s = container_of(node, struct h2s, by_id);
1295 h2s->mws += diff;
1296 node = eb32_next(node);
1297 }
1298}
1299
1300/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1301 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1302 * return an error in h2c. Described in RFC7540#6.5.
1303 */
1304static int h2c_handle_settings(struct h2c *h2c)
1305{
1306 unsigned int offset;
1307 int error;
1308
1309 if (h2c->dff & H2_F_SETTINGS_ACK) {
1310 if (h2c->dfl) {
1311 error = H2_ERR_FRAME_SIZE_ERROR;
1312 goto fail;
1313 }
1314 return 1;
1315 }
1316
1317 if (h2c->dsi != 0) {
1318 error = H2_ERR_PROTOCOL_ERROR;
1319 goto fail;
1320 }
1321
1322 if (h2c->dfl % 6) {
1323 error = H2_ERR_FRAME_SIZE_ERROR;
1324 goto fail;
1325 }
1326
1327 /* that's the limit we can process */
1328 if (h2c->dfl > global.tune.bufsize) {
1329 error = H2_ERR_FRAME_SIZE_ERROR;
1330 goto fail;
1331 }
1332
1333 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001334 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001335 return 0;
1336
1337 /* parse the frame */
1338 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001339 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1340 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001341
1342 switch (type) {
1343 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1344 /* we need to update all existing streams with the
1345 * difference from the previous iws.
1346 */
1347 if (arg < 0) { // RFC7540#6.5.2
1348 error = H2_ERR_FLOW_CONTROL_ERROR;
1349 goto fail;
1350 }
1351 h2c_update_all_ws(h2c, arg - h2c->miw);
1352 h2c->miw = arg;
1353 break;
1354 case H2_SETTINGS_MAX_FRAME_SIZE:
1355 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1356 error = H2_ERR_PROTOCOL_ERROR;
1357 goto fail;
1358 }
1359 h2c->mfs = arg;
1360 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001361 case H2_SETTINGS_ENABLE_PUSH:
1362 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1363 error = H2_ERR_PROTOCOL_ERROR;
1364 goto fail;
1365 }
1366 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001367 }
1368 }
1369
1370 /* need to ACK this frame now */
1371 h2c->st0 = H2_CS_FRAME_A;
1372 return 1;
1373 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001374 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001375 h2c_error(h2c, error);
1376 return 0;
1377}
1378
1379/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1380 * success or one of the h2_status values.
1381 */
1382static int h2c_ack_settings(struct h2c *h2c)
1383{
1384 struct buffer *res;
1385 char str[9];
1386 int ret = -1;
1387
1388 if (h2c_mux_busy(h2c, NULL)) {
1389 h2c->flags |= H2_CF_DEM_MBUSY;
1390 return 0;
1391 }
1392
Willy Tarreau44e973f2018-03-01 17:49:30 +01001393 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001394 if (!res) {
1395 h2c->flags |= H2_CF_MUX_MALLOC;
1396 h2c->flags |= H2_CF_DEM_MROOM;
1397 return 0;
1398 }
1399
1400 memcpy(str,
1401 "\x00\x00\x00" /* length : 0 (no data) */
1402 "\x04" "\x01" /* type : 4, flags : ACK */
1403 "\x00\x00\x00\x00" /* stream ID */, 9);
1404
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001405 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001406 if (unlikely(ret <= 0)) {
1407 if (!ret) {
1408 h2c->flags |= H2_CF_MUX_MFULL;
1409 h2c->flags |= H2_CF_DEM_MROOM;
1410 return 0;
1411 }
1412 else {
1413 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1414 return 0;
1415 }
1416 }
1417 return ret;
1418}
1419
Willy Tarreaucf68c782017-10-10 17:11:41 +02001420/* processes a PING frame and schedules an ACK if needed. The caller must pass
1421 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1422 * missing data. It may return an error in h2c.
1423 */
1424static int h2c_handle_ping(struct h2c *h2c)
1425{
1426 /* frame length must be exactly 8 */
1427 if (h2c->dfl != 8) {
1428 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1429 return 0;
1430 }
1431
1432 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001433 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001434 h2c->st0 = H2_CS_FRAME_A;
1435 return 1;
1436}
1437
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001438/* Try to send a window update for stream id <sid> and value <increment>.
1439 * Returns > 0 on success or zero on missing room or failure. It may return an
1440 * error in h2c.
1441 */
1442static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1443{
1444 struct buffer *res;
1445 char str[13];
1446 int ret = -1;
1447
1448 if (h2c_mux_busy(h2c, NULL)) {
1449 h2c->flags |= H2_CF_DEM_MBUSY;
1450 return 0;
1451 }
1452
Willy Tarreau44e973f2018-03-01 17:49:30 +01001453 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001454 if (!res) {
1455 h2c->flags |= H2_CF_MUX_MALLOC;
1456 h2c->flags |= H2_CF_DEM_MROOM;
1457 return 0;
1458 }
1459
1460 /* length: 4, type: 8, flags: none */
1461 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1462 write_n32(str + 5, sid);
1463 write_n32(str + 9, increment);
1464
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001465 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001466
1467 if (unlikely(ret <= 0)) {
1468 if (!ret) {
1469 h2c->flags |= H2_CF_MUX_MFULL;
1470 h2c->flags |= H2_CF_DEM_MROOM;
1471 return 0;
1472 }
1473 else {
1474 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1475 return 0;
1476 }
1477 }
1478 return ret;
1479}
1480
1481/* try to send pending window update for the connection. It's safe to call it
1482 * with no pending updates. Returns > 0 on success or zero on missing room or
1483 * failure. It may return an error in h2c.
1484 */
1485static int h2c_send_conn_wu(struct h2c *h2c)
1486{
1487 int ret = 1;
1488
1489 if (h2c->rcvd_c <= 0)
1490 return 1;
1491
1492 /* send WU for the connection */
1493 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1494 if (ret > 0)
1495 h2c->rcvd_c = 0;
1496
1497 return ret;
1498}
1499
1500/* try to send pending window update for the current dmux stream. It's safe to
1501 * call it with no pending updates. Returns > 0 on success or zero on missing
1502 * room or failure. It may return an error in h2c.
1503 */
1504static int h2c_send_strm_wu(struct h2c *h2c)
1505{
1506 int ret = 1;
1507
1508 if (h2c->rcvd_s <= 0)
1509 return 1;
1510
1511 /* send WU for the stream */
1512 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1513 if (ret > 0)
1514 h2c->rcvd_s = 0;
1515
1516 return ret;
1517}
1518
Willy Tarreaucf68c782017-10-10 17:11:41 +02001519/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1520 * success, 0 on missing data or one of the h2_status values.
1521 */
1522static int h2c_ack_ping(struct h2c *h2c)
1523{
1524 struct buffer *res;
1525 char str[17];
1526 int ret = -1;
1527
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001528 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001529 return 0;
1530
1531 if (h2c_mux_busy(h2c, NULL)) {
1532 h2c->flags |= H2_CF_DEM_MBUSY;
1533 return 0;
1534 }
1535
Willy Tarreau44e973f2018-03-01 17:49:30 +01001536 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001537 if (!res) {
1538 h2c->flags |= H2_CF_MUX_MALLOC;
1539 h2c->flags |= H2_CF_DEM_MROOM;
1540 return 0;
1541 }
1542
1543 memcpy(str,
1544 "\x00\x00\x08" /* length : 8 (same payload) */
1545 "\x06" "\x01" /* type : 6, flags : ACK */
1546 "\x00\x00\x00\x00" /* stream ID */, 9);
1547
1548 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001549 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001550
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001551 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001552 if (unlikely(ret <= 0)) {
1553 if (!ret) {
1554 h2c->flags |= H2_CF_MUX_MFULL;
1555 h2c->flags |= H2_CF_DEM_MROOM;
1556 return 0;
1557 }
1558 else {
1559 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1560 return 0;
1561 }
1562 }
1563 return ret;
1564}
1565
Willy Tarreau26f95952017-07-27 17:18:30 +02001566/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1567 * Returns > 0 on success or zero on missing data. It may return an error in
1568 * h2c or h2s. Described in RFC7540#6.9.
1569 */
1570static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1571{
1572 int32_t inc;
1573 int error;
1574
1575 if (h2c->dfl != 4) {
1576 error = H2_ERR_FRAME_SIZE_ERROR;
1577 goto conn_err;
1578 }
1579
1580 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001581 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001582 return 0;
1583
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001584 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001585
1586 if (h2c->dsi != 0) {
1587 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001588
1589 /* it's not an error to receive WU on a closed stream */
1590 if (h2s->st == H2_SS_CLOSED)
1591 return 1;
1592
1593 if (!inc) {
1594 error = H2_ERR_PROTOCOL_ERROR;
1595 goto strm_err;
1596 }
1597
1598 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1599 error = H2_ERR_FLOW_CONTROL_ERROR;
1600 goto strm_err;
1601 }
1602
1603 h2s->mws += inc;
1604 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1605 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001606 if (h2s->send_wait)
1607 LIST_ADDQ(&h2c->send_list, &h2s->list);
1608
Willy Tarreau26f95952017-07-27 17:18:30 +02001609 }
1610 }
1611 else {
1612 /* connection window update */
1613 if (!inc) {
1614 error = H2_ERR_PROTOCOL_ERROR;
1615 goto conn_err;
1616 }
1617
1618 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1619 error = H2_ERR_FLOW_CONTROL_ERROR;
1620 goto conn_err;
1621 }
1622
1623 h2c->mws += inc;
1624 }
1625
1626 return 1;
1627
1628 conn_err:
1629 h2c_error(h2c, error);
1630 return 0;
1631
1632 strm_err:
1633 if (h2s) {
1634 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001635 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001636 }
1637 else
1638 h2c_error(h2c, error);
1639 return 0;
1640}
1641
Willy Tarreaue96b0922017-10-30 00:28:29 +01001642/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1643 * the last ID. Returns > 0 on success or zero on missing data. It may return
1644 * an error in h2c. Described in RFC7540#6.8.
1645 */
1646static int h2c_handle_goaway(struct h2c *h2c)
1647{
1648 int error;
1649 int last;
1650
1651 if (h2c->dsi != 0) {
1652 error = H2_ERR_PROTOCOL_ERROR;
1653 goto conn_err;
1654 }
1655
1656 if (h2c->dfl < 8) {
1657 error = H2_ERR_FRAME_SIZE_ERROR;
1658 goto conn_err;
1659 }
1660
1661 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001662 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001663 return 0;
1664
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001665 last = h2_get_n32(&h2c->dbuf, 0);
1666 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001667 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001668 if (h2c->last_sid < 0)
1669 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001670 return 1;
1671
1672 conn_err:
1673 h2c_error(h2c, error);
1674 return 0;
1675}
1676
Willy Tarreau92153fc2017-12-03 19:46:19 +01001677/* processes a PRIORITY frame, and either skips it or rejects if it is
1678 * invalid. Returns > 0 on success or zero on missing data. It may return
1679 * an error in h2c. Described in RFC7540#6.3.
1680 */
1681static int h2c_handle_priority(struct h2c *h2c)
1682{
1683 int error;
1684
1685 if (h2c->dsi == 0) {
1686 error = H2_ERR_PROTOCOL_ERROR;
1687 goto conn_err;
1688 }
1689
1690 if (h2c->dfl != 5) {
1691 error = H2_ERR_FRAME_SIZE_ERROR;
1692 goto conn_err;
1693 }
1694
1695 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001696 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001697 return 0;
1698
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001699 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001700 /* 7540#5.3 : can't depend on itself */
1701 error = H2_ERR_PROTOCOL_ERROR;
1702 goto conn_err;
1703 }
1704 return 1;
1705
1706 conn_err:
1707 h2c_error(h2c, error);
1708 return 0;
1709}
1710
Willy Tarreaucd234e92017-08-18 10:59:39 +02001711/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1712 * Returns > 0 on success or zero on missing data. It may return an error in
1713 * h2c. Described in RFC7540#6.4.
1714 */
1715static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1716{
1717 int error;
1718
1719 if (h2c->dsi == 0) {
1720 error = H2_ERR_PROTOCOL_ERROR;
1721 goto conn_err;
1722 }
1723
Willy Tarreaucd234e92017-08-18 10:59:39 +02001724 if (h2c->dfl != 4) {
1725 error = H2_ERR_FRAME_SIZE_ERROR;
1726 goto conn_err;
1727 }
1728
1729 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001730 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001731 return 0;
1732
1733 /* late RST, already handled */
1734 if (h2s->st == H2_SS_CLOSED)
1735 return 1;
1736
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001737 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001738 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001739
1740 if (h2s->cs) {
Willy Tarreau2c096c32018-09-12 09:45:54 +02001741 h2s->cs->flags |= CS_FL_REOS | CS_FL_ERROR;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001742 if (h2s->recv_wait) {
1743 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001744
1745 sw->wait_reason &= ~SUB_CAN_RECV;
1746 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001747 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001748 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02001749 }
1750
1751 h2s->flags |= H2_SF_RST_RCVD;
1752 return 1;
1753
1754 conn_err:
1755 h2c_error(h2c, error);
1756 return 0;
1757}
1758
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001759/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1760 * It may return an error in h2c or h2s. The caller must consider that the
1761 * return value is the new h2s in case one was allocated (most common case).
1762 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001763 * errors here are reported as connection errors since it's impossible to
1764 * recover from such errors after the compression context has been altered.
1765 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001766static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001767{
1768 int error;
1769
1770 if (!h2c->dfl) {
1771 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001772 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001773 goto strm_err;
1774 }
1775
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001776 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001777 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001778
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001779 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001780 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001781
Willy Tarreauf2101912018-07-19 10:11:38 +02001782 if (h2c->flags & H2_CF_DEM_TOOMANY)
1783 return 0; // too many cs still present
1784
Willy Tarreau13278b42017-10-13 19:23:14 +02001785 /* now either the frame is complete or the buffer is complete */
1786 if (h2s->st != H2_SS_IDLE) {
1787 /* FIXME: stream already exists, this is only allowed for
1788 * trailers (not supported for now).
1789 */
1790 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001791 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001792 goto conn_err;
1793 }
1794 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1795 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1796 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001797 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001798 goto conn_err;
1799 }
1800
Willy Tarreau22de8d32018-09-05 19:55:58 +02001801 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001802 * positively from h2c_frt_stream_new(), the stream will report the error,
1803 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001804 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001805 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001806 if (!h2s) {
1807 error = H2_ERR_INTERNAL_ERROR;
1808 goto conn_err;
1809 }
1810
1811 h2s->st = H2_SS_OPEN;
1812 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1813 h2s->st = H2_SS_HREM;
1814 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001815 /* note: cs cannot be null for now (just created above) */
1816 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001817 }
1818
Willy Tarreauc3e18f32018-10-08 14:51:56 +02001819 if (!h2s_decode_headers(h2s))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001820 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001821
Willy Tarreau8f650c32017-11-21 19:36:21 +01001822 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001823 return NULL;
Willy Tarreau8f650c32017-11-21 19:36:21 +01001824
Willy Tarreau721c9742017-11-07 11:05:42 +01001825 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001826 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001827 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001828 }
1829 else {
1830 /* update the max stream ID if the request is being processed */
1831 if (h2s->id > h2c->max_id)
1832 h2c->max_id = h2s->id;
1833 }
1834
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001835 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001836
1837 conn_err:
1838 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001839 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001840
1841 strm_err:
1842 if (h2s) {
1843 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001844 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001845 }
1846 else
1847 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001848 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001849}
1850
Willy Tarreauc12f38f2018-10-08 14:53:27 +02001851/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1852 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
1853 * errors here are reported as connection errors since it's impossible to
1854 * recover from such errors after the compression context has been altered.
1855 */
1856static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
1857{
1858 int error;
1859
1860 if (!h2c->dfl) {
1861 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
1862 sess_log(h2c->conn->owner);
1863 goto strm_err;
1864 }
1865
1866 if (!b_size(&h2c->dbuf))
1867 return NULL; // empty buffer
1868
1869 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
1870 return NULL; // incomplete frame
1871
1872 if (h2c->flags & H2_CF_DEM_TOOMANY)
1873 return 0; // too many cs still present
1874
1875 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1876 h2s->flags |= H2_SF_ES_RCVD;
1877 h2s->cs->flags |= CS_FL_REOS;
1878 }
1879
1880 if (!h2s_decode_headers(h2s))
1881 return NULL;
1882
1883 if (h2c->st0 >= H2_CS_ERROR)
1884 return NULL;
1885
1886 if (h2s->st >= H2_SS_ERROR) {
1887 /* stream error : send RST_STREAM */
1888 h2c->st0 = H2_CS_FRAME_E;
1889 }
1890
1891 if (h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1892 h2s->st = H2_SS_ERROR;
1893 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
1894 h2s->st = H2_SS_HREM;
1895 else if (h2s->cs->flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
1896 h2s_close(h2s);
1897
1898 return h2s;
1899
1900 conn_err:
1901 h2c_error(h2c, error);
1902 return NULL;
1903
1904 strm_err:
1905 if (h2s) {
1906 h2s_error(h2s, error);
1907 h2c->st0 = H2_CS_FRAME_E;
1908 }
1909 else
1910 h2c_error(h2c, error);
1911 return NULL;
1912}
1913
Willy Tarreau454f9052017-10-26 19:40:35 +02001914/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1915 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1916 */
1917static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1918{
1919 int error;
1920
1921 /* note that empty DATA frames are perfectly valid and sometimes used
1922 * to signal an end of stream (with the ES flag).
1923 */
1924
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001925 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001926 return 0; // empty buffer
1927
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001928 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001929 return 0; // incomplete frame
1930
1931 /* now either the frame is complete or the buffer is complete */
1932
1933 if (!h2c->dsi) {
1934 /* RFC7540#6.1 */
1935 error = H2_ERR_PROTOCOL_ERROR;
1936 goto conn_err;
1937 }
1938
1939 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1940 /* RFC7540#6.1 */
1941 error = H2_ERR_STREAM_CLOSED;
1942 goto strm_err;
1943 }
1944
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001945 if (!h2_frt_transfer_data(h2s))
1946 return 0;
1947
Willy Tarreau454f9052017-10-26 19:40:35 +02001948 /* call the upper layers to process the frame, then let the upper layer
1949 * notify the stream about any change.
1950 */
1951 if (!h2s->cs) {
1952 error = H2_ERR_STREAM_CLOSED;
1953 goto strm_err;
1954 }
1955
Willy Tarreau8f650c32017-11-21 19:36:21 +01001956 if (h2c->st0 >= H2_CS_ERROR)
1957 return 0;
1958
Willy Tarreau721c9742017-11-07 11:05:42 +01001959 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001960 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001961 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001962 }
1963
1964 /* check for completion : the callee will change this to FRAME_A or
1965 * FRAME_H once done.
1966 */
1967 if (h2c->st0 == H2_CS_FRAME_P)
1968 return 0;
1969
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001970
1971 /* last frame */
1972 if (h2c->dff & H2_F_DATA_END_STREAM) {
1973 h2s->st = H2_SS_HREM;
1974 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001975 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001976 }
1977
Willy Tarreau454f9052017-10-26 19:40:35 +02001978 return 1;
1979
1980 conn_err:
1981 h2c_error(h2c, error);
1982 return 0;
1983
1984 strm_err:
1985 if (h2s) {
1986 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001987 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001988 }
1989 else
1990 h2c_error(h2c, error);
1991 return 0;
1992}
1993
Willy Tarreaubc933932017-10-09 16:21:43 +02001994/* process Rx frames to be demultiplexed */
1995static void h2_process_demux(struct h2c *h2c)
1996{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001997 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001998
Willy Tarreau081d4722017-05-16 21:51:05 +02001999 if (h2c->st0 >= H2_CS_ERROR)
2000 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002001
2002 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2003 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002004 if (h2c->flags & H2_CF_IS_BACK)
2005 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002006 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2007 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002008 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002009 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002010 sess_log(h2c->conn->owner);
2011 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002012 goto fail;
2013 }
2014
2015 h2c->max_id = 0;
2016 h2c->st0 = H2_CS_SETTINGS1;
2017 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002018
2019 if (h2c->st0 == H2_CS_SETTINGS1) {
2020 struct h2_fh hdr;
2021
2022 /* ensure that what is pending is a valid SETTINGS frame
2023 * without an ACK.
2024 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002025 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002026 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002027 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002028 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002029 sess_log(h2c->conn->owner);
2030 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002031 goto fail;
2032 }
2033
2034 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2035 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2036 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2037 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002038 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002039 goto fail;
2040 }
2041
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002042 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002043 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2044 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2045 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002046 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002047 goto fail;
2048 }
2049
2050 /* that's OK, switch to FRAME_P to process it */
2051 h2c->dfl = hdr.len;
2052 h2c->dsi = hdr.sid;
2053 h2c->dft = hdr.ft;
2054 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002055 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002056 h2c->st0 = H2_CS_FRAME_P;
2057 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002058 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002059
2060 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002061 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002062 int ret = 0;
2063
2064 if (h2c->st0 >= H2_CS_ERROR)
2065 break;
2066
2067 if (h2c->st0 == H2_CS_FRAME_H) {
2068 struct h2_fh hdr;
2069
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002070 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002071 break;
2072
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002073 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002074 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2075 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002076 if (!h2c->nb_streams) {
2077 /* only log if no other stream can report the error */
2078 sess_log(h2c->conn->owner);
2079 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002080 break;
2081 }
2082
2083 h2c->dfl = hdr.len;
2084 h2c->dsi = hdr.sid;
2085 h2c->dft = hdr.ft;
2086 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002087 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002088 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002089 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002090 }
2091
2092 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002093 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2094
Olivier Houchard638b7992018-08-16 15:41:52 +02002095 if (tmp_h2s != h2s && h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002096 /* we may have to signal the upper layers */
2097 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002098 if (h2s->recv_wait) {
2099 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
2100 tasklet_wakeup(h2s->recv_wait->task);
2101 h2s->recv_wait = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002102 }
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002103 }
2104 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002105
Willy Tarreaud7901432017-12-29 11:34:40 +01002106 if (h2c->st0 == H2_CS_FRAME_E)
2107 goto strm_err;
2108
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002109 if (h2s->st == H2_SS_IDLE &&
2110 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2111 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2112 * this state MUST be treated as a connection error
2113 */
2114 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2115 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002116 if (!h2c->nb_streams) {
2117 /* only log if no other stream can report the error */
2118 sess_log(h2c->conn->owner);
2119 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002120 break;
2121 }
2122
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002123 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2124 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2125 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
2126 * this state MUST be treated as a stream error
2127 */
2128 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2129 goto strm_err;
2130 }
2131
Willy Tarreauab837502017-12-27 15:07:30 +01002132 /* Below the management of frames received in closed state is a
2133 * bit hackish because the spec makes strong differences between
2134 * streams closed by receiving RST, sending RST, and seeing ES
2135 * in both directions. In addition to this, the creation of a
2136 * new stream reusing the identifier of a closed one will be
2137 * detected here. Given that we cannot keep track of all closed
2138 * streams forever, we consider that unknown closed streams were
2139 * closed on RST received, which allows us to respond with an
2140 * RST without breaking the connection (eg: to abort a transfer).
2141 * Some frames have to be silently ignored as well.
2142 */
2143 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
2144 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
2145 /* #5.1.1: The identifier of a newly
2146 * established stream MUST be numerically
2147 * greater than all streams that the initiating
2148 * endpoint has opened or reserved. This
2149 * governs streams that are opened using a
2150 * HEADERS frame and streams that are reserved
2151 * using PUSH_PROMISE. An endpoint that
2152 * receives an unexpected stream identifier
2153 * MUST respond with a connection error.
2154 */
2155 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2156 goto strm_err;
2157 }
2158
2159 if (h2s->flags & H2_SF_RST_RCVD) {
2160 /* RFC7540#5.1:closed: an endpoint that
2161 * receives any frame other than PRIORITY after
2162 * receiving a RST_STREAM MUST treat that as a
2163 * stream error of type STREAM_CLOSED.
2164 *
2165 * Note that old streams fall into this category
2166 * and will lead to an RST being sent.
2167 */
2168 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2169 h2c->st0 = H2_CS_FRAME_E;
2170 goto strm_err;
2171 }
2172
2173 /* RFC7540#5.1:closed: if this state is reached as a
2174 * result of sending a RST_STREAM frame, the peer that
2175 * receives the RST_STREAM might have already sent
2176 * frames on the stream that cannot be withdrawn. An
2177 * endpoint MUST ignore frames that it receives on
2178 * closed streams after it has sent a RST_STREAM
2179 * frame. An endpoint MAY choose to limit the period
2180 * over which it ignores frames and treat frames that
2181 * arrive after this time as being in error.
2182 */
2183 if (!(h2s->flags & H2_SF_RST_SENT)) {
2184 /* RFC7540#5.1:closed: any frame other than
2185 * PRIO/WU/RST in this state MUST be treated as
2186 * a connection error
2187 */
2188 if (h2c->dft != H2_FT_RST_STREAM &&
2189 h2c->dft != H2_FT_PRIORITY &&
2190 h2c->dft != H2_FT_WINDOW_UPDATE) {
2191 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2192 goto strm_err;
2193 }
2194 }
2195 }
2196
Willy Tarreauc0da1962017-10-30 18:38:00 +01002197#if 0
2198 // problem below: it is not possible to completely ignore such
2199 // streams as we need to maintain the compression state as well
2200 // and for this we need to completely process these frames (eg:
2201 // HEADERS frames) as well as counting DATA frames to emit
2202 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2203 // This is a typical case of layer violation where the
2204 // transported contents are critical to the connection's
2205 // validity and must be ignored at the same time :-(
2206
2207 /* graceful shutdown, ignore streams whose ID is higher than
2208 * the one advertised in GOAWAY. RFC7540#6.8.
2209 */
2210 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002211 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2212 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002213 h2c->dfl -= ret;
2214 ret = h2c->dfl == 0;
2215 goto strm_err;
2216 }
2217#endif
2218
Willy Tarreau7e98c052017-10-10 15:56:59 +02002219 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002220 case H2_FT_SETTINGS:
2221 if (h2c->st0 == H2_CS_FRAME_P)
2222 ret = h2c_handle_settings(h2c);
2223
2224 if (h2c->st0 == H2_CS_FRAME_A)
2225 ret = h2c_ack_settings(h2c);
2226 break;
2227
Willy Tarreaucf68c782017-10-10 17:11:41 +02002228 case H2_FT_PING:
2229 if (h2c->st0 == H2_CS_FRAME_P)
2230 ret = h2c_handle_ping(h2c);
2231
2232 if (h2c->st0 == H2_CS_FRAME_A)
2233 ret = h2c_ack_ping(h2c);
2234 break;
2235
Willy Tarreau26f95952017-07-27 17:18:30 +02002236 case H2_FT_WINDOW_UPDATE:
2237 if (h2c->st0 == H2_CS_FRAME_P)
2238 ret = h2c_handle_window_update(h2c, h2s);
2239 break;
2240
Willy Tarreau61290ec2017-10-17 08:19:21 +02002241 case H2_FT_CONTINUATION:
2242 /* we currently don't support CONTINUATION frames since
2243 * we have nowhere to store the partial HEADERS frame.
2244 * Let's abort the stream on an INTERNAL_ERROR here.
2245 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002246 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02002247 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002248 h2c->st0 = H2_CS_FRAME_E;
2249 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02002250 break;
2251
Willy Tarreau13278b42017-10-13 19:23:14 +02002252 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002253 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002254 if (h2c->flags & H2_CF_IS_BACK)
2255 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2256 else
2257 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002258 if (tmp_h2s) {
2259 h2s = tmp_h2s;
2260 ret = 1;
2261 }
2262 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002263 break;
2264
Willy Tarreau454f9052017-10-26 19:40:35 +02002265 case H2_FT_DATA:
2266 if (h2c->st0 == H2_CS_FRAME_P)
2267 ret = h2c_frt_handle_data(h2c, h2s);
2268
2269 if (h2c->st0 == H2_CS_FRAME_A)
2270 ret = h2c_send_strm_wu(h2c);
2271 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002272
Willy Tarreau92153fc2017-12-03 19:46:19 +01002273 case H2_FT_PRIORITY:
2274 if (h2c->st0 == H2_CS_FRAME_P)
2275 ret = h2c_handle_priority(h2c);
2276 break;
2277
Willy Tarreaucd234e92017-08-18 10:59:39 +02002278 case H2_FT_RST_STREAM:
2279 if (h2c->st0 == H2_CS_FRAME_P)
2280 ret = h2c_handle_rst_stream(h2c, h2s);
2281 break;
2282
Willy Tarreaue96b0922017-10-30 00:28:29 +01002283 case H2_FT_GOAWAY:
2284 if (h2c->st0 == H2_CS_FRAME_P)
2285 ret = h2c_handle_goaway(h2c);
2286 break;
2287
Willy Tarreau1c661982017-10-30 13:52:01 +01002288 case H2_FT_PUSH_PROMISE:
2289 /* not permitted here, RFC7540#5.1 */
2290 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002291 if (!h2c->nb_streams) {
2292 /* only log if no other stream can report the error */
2293 sess_log(h2c->conn->owner);
2294 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002295 break;
2296
2297 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002298 default:
2299 /* drop frames that we ignore. They may be larger than
2300 * the buffer so we drain all of their contents until
2301 * we reach the end.
2302 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002303 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2304 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002305 h2c->dfl -= ret;
2306 ret = h2c->dfl == 0;
2307 }
2308
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002309 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002310 /* We may have to send an RST if not done yet */
2311 if (h2s->st == H2_SS_ERROR)
2312 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002313
Willy Tarreaua20a5192017-12-27 11:02:06 +01002314 if (h2c->st0 == H2_CS_FRAME_E)
2315 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002316
Willy Tarreau7e98c052017-10-10 15:56:59 +02002317 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002318 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002319 break;
2320
2321 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002322 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002323 h2c->st0 = H2_CS_FRAME_H;
2324 }
2325 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002326
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002327 if (h2c->rcvd_c > 0 &&
2328 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2329 h2c_send_conn_wu(h2c);
2330
Willy Tarreau52eed752017-09-22 15:05:09 +02002331 fail:
2332 /* we can go here on missing data, blocked response or error */
Olivier Houchard638b7992018-08-16 15:41:52 +02002333 if (h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002334 /* we may have to signal the upper layers */
2335 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002336 if (h2s->recv_wait) {
2337 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
2338 tasklet_wakeup(h2s->recv_wait->task);
2339 h2s->recv_wait = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002340 }
2341 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002342
2343 if (h2_recv_allowed(h2c))
2344 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreaubc933932017-10-09 16:21:43 +02002345}
2346
2347/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2348 * the end.
2349 */
2350static int h2_process_mux(struct h2c *h2c)
2351{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002352 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002353
Willy Tarreau01b44822018-10-03 14:26:37 +02002354 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2355 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2356 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2357 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2358 if (h2c->st0 == H2_CS_ERROR) {
2359 h2c->st0 = H2_CS_ERROR2;
2360 sess_log(h2c->conn->owner);
2361 }
2362 goto fail;
2363 }
2364 h2c->st0 = H2_CS_SETTINGS1;
2365 }
2366 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002367 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002368 return 1;
2369 }
2370
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002371 /* start by sending possibly pending window updates */
2372 if (h2c->rcvd_c > 0 &&
2373 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2374 h2c_send_conn_wu(h2c) < 0)
2375 goto fail;
2376
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002377 /* First we always process the flow control list because the streams
2378 * waiting there were already elected for immediate emission but were
2379 * blocked just on this.
2380 */
2381
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002382 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002383 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2384 h2c->st0 >= H2_CS_ERROR)
2385 break;
2386
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002387 h2s->flags &= ~H2_SF_BLK_ANY;
2388 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002389 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002390 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002391 LIST_DEL(&h2s->list);
2392 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002393 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002394 }
2395
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002396 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002397 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2398 break;
2399
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002400 h2s->flags &= ~H2_SF_BLK_ANY;
2401 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002402 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002403 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002404 LIST_DEL(&h2s->list);
2405 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002406 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002407 }
2408
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002409 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002410 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002411 if (h2c->st0 == H2_CS_ERROR) {
2412 if (h2c->max_id >= 0) {
2413 h2c_send_goaway_error(h2c, NULL);
2414 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2415 return 0;
2416 }
2417
2418 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2419 }
2420 return 1;
2421 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002422 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002423}
2424
Willy Tarreau62f52692017-10-08 23:01:42 +02002425
Willy Tarreau479998a2018-11-18 06:30:59 +01002426/* Attempt to read data, and subscribe if none available.
2427 * The function returns 1 if data has been received, otherwise zero.
2428 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002429static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002430{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002431 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002432 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002433 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002434 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002435
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002436 if (h2c->wait_event.wait_reason & SUB_CAN_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002437 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002438
Willy Tarreau315d8072017-12-10 22:17:57 +01002439 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002440 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002441
Willy Tarreau44e973f2018-03-01 17:49:30 +01002442 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002443 if (!buf) {
2444 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002445 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002446 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002447
Olivier Houchard7505f942018-08-21 18:10:44 +02002448 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002449 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002450 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2451 /* HTX in use : try to pre-align the buffer like the
2452 * rxbufs will be to optimize memory copies. We'll make
2453 * sure that the frame header lands at the end of the
2454 * HTX block to alias it upon recv. We cannot use the
2455 * head because rcv_buf() will realign the buffer if
2456 * it's empty. Thus we cheat and pretend we already
2457 * have a few bytes there.
2458 */
2459 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002460 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002461 }
2462 else
2463 max = b_room(buf);
2464
Olivier Houchard7505f942018-08-21 18:10:44 +02002465 if (max)
2466 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2467 else
2468 ret = 0;
2469 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002470
Olivier Houchard53216e72018-10-10 15:46:36 +02002471 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002472 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002473
Olivier Houcharda1411e62018-08-17 18:42:48 +02002474 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002475 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002476 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002477 }
2478
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002479 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002480 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002481 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002482}
2483
Willy Tarreau479998a2018-11-18 06:30:59 +01002484/* Try to send data if possible.
2485 * The function returns 1 if data have been sent, otherwise zero.
2486 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002487static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002488{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002489 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002490 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002491 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002492
2493 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002494 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002495
Olivier Houchard7505f942018-08-21 18:10:44 +02002496
Willy Tarreaua2af5122017-10-09 11:56:46 +02002497 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2498 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002499 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002500 }
2501
Willy Tarreaubc933932017-10-09 16:21:43 +02002502 /* This loop is quite simple : it tries to fill as much as it can from
2503 * pending streams into the existing buffer until it's reportedly full
2504 * or the end of send requests is reached. Then it tries to send this
2505 * buffer's contents out, marks it not full if at least one byte could
2506 * be sent, and tries again.
2507 *
2508 * The snd_buf() function normally takes a "flags" argument which may
2509 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2510 * data immediately comes and CO_SFL_STREAMER to indicate that the
2511 * connection is streaming lots of data (used to increase TLS record
2512 * size at the expense of latency). The former can be sent any time
2513 * there's a buffer full flag, as it indicates at least one stream
2514 * attempted to send and failed so there are pending data. An
2515 * alternative would be to set it as long as there's an active stream
2516 * but that would be problematic for ACKs until we have an absolute
2517 * guarantee that all waiters have at least one byte to send. The
2518 * latter should possibly not be set for now.
2519 */
2520
2521 done = 0;
2522 while (!done) {
2523 unsigned int flags = 0;
2524
2525 /* fill as much as we can into the current buffer */
2526 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2527 done = h2_process_mux(h2c);
2528
2529 if (conn->flags & CO_FL_ERROR)
2530 break;
2531
2532 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2533 flags |= CO_SFL_MSG_MORE;
2534
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002535 if (b_data(&h2c->mbuf)) {
2536 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002537 if (!ret)
2538 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002539 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002540 b_del(&h2c->mbuf, ret);
2541 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002542 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002543
2544 /* wrote at least one byte, the buffer is not full anymore */
2545 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2546 }
2547
Willy Tarreaua2af5122017-10-09 11:56:46 +02002548 if (conn->flags & CO_FL_SOCK_WR_SH) {
2549 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002550 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002551 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002552 /* We're not full anymore, so we can wake any task that are waiting
2553 * for us.
2554 */
2555 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002556 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002557 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2558 struct h2s *, list);
2559 LIST_DEL(&h2s->list);
2560 LIST_INIT(&h2s->list);
Olivier Houchardd846c262018-10-19 17:24:29 +02002561 LIST_ADDQ(&h2c->sending_list, &h2s->list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002562 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02002563 h2s->send_wait->wait_reason |= SUB_CALL_UNSUBSCRIBE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002564 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002565 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002566 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002567 /* We're done, no more to send */
2568 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002569 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002570schedule:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002571 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
2572 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002573 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002574}
2575
2576static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2577{
2578 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002579 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002580
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002581 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002582 ret = h2_send(h2c);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002583 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002584 ret |= h2_recv(h2c);
2585 if (ret)
2586 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002587 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002588}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002589
Willy Tarreau62f52692017-10-08 23:01:42 +02002590/* callback called on any event by the connection handler.
2591 * It applies changes and returns zero, or < 0 if it wants immediate
2592 * destruction of the connection (which normally doesn not happen in h2).
2593 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002594static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002595{
Olivier Houchard7505f942018-08-21 18:10:44 +02002596 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002597
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002598 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002599 h2_process_demux(h2c);
2600
2601 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002602 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002603
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002604 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002605 h2c->flags &= ~H2_CF_DEM_DFULL;
2606 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002607 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002608
Willy Tarreau0b37d652018-10-03 10:33:02 +02002609 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002610 /* frontend is stopping, reload likely in progress, let's try
2611 * to announce a graceful shutdown if not yet done. We don't
2612 * care if it fails, it will be tried again later.
2613 */
2614 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2615 if (h2c->last_sid < 0)
2616 h2c->last_sid = (1U << 31) - 1;
2617 h2c_send_goaway_error(h2c, NULL);
2618 }
2619 }
2620
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002621 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002622 * If we received early data, and the handshake is done, wake
2623 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002624 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002625 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2626 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2627 struct eb32_node *node;
2628 struct h2s *h2s;
2629
2630 h2c->flags |= H2_CF_WAIT_FOR_HS;
2631 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2632
2633 while (node) {
2634 h2s = container_of(node, struct h2s, by_id);
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002635 if ((h2s->cs->flags & CS_FL_WAIT_FOR_HS) &&
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002636 h2s->recv_wait) {
2637 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002638 sw->wait_reason &= ~SUB_CAN_RECV;
2639 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002640 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002641 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002642 node = eb32_next(node);
2643 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002644 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002645
Willy Tarreau26bd7612017-10-09 16:47:04 +02002646 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002647 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2648 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2649 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002650 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002651
2652 if (eb_is_empty(&h2c->streams_by_id)) {
2653 /* no more stream, kill the connection now */
2654 h2_release(conn);
2655 return -1;
2656 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002657 }
2658
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002659 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002660 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002661
Olivier Houchard53216e72018-10-10 15:46:36 +02002662 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2663 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2664 (h2c->st0 != H2_CS_ERROR &&
2665 !b_data(&h2c->mbuf) &&
2666 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2667 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002668 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002669
Willy Tarreau3f133572017-10-31 19:21:06 +01002670 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002671 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002672 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002673 task_queue(h2c->task);
2674 }
2675 else
2676 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002677 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002678
Olivier Houchard7505f942018-08-21 18:10:44 +02002679 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002680 return 0;
2681}
2682
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002683static int h2_wake(struct connection *conn)
2684{
2685 struct h2c *h2c = conn->mux_ctx;
2686
2687 return (h2_process(h2c));
2688}
2689
Willy Tarreauea392822017-10-31 10:02:25 +01002690/* Connection timeout management. The principle is that if there's no receipt
2691 * nor sending for a certain amount of time, the connection is closed. If the
2692 * MUX buffer still has lying data or is not allocatable, the connection is
2693 * immediately killed. If it's allocatable and empty, we attempt to send a
2694 * GOAWAY frame.
2695 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002696static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002697{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002698 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002699 int expired = tick_is_expired(t->expire, now_ms);
2700
Willy Tarreau0975f112018-03-29 15:22:59 +02002701 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002702 return t;
2703
Willy Tarreau0975f112018-03-29 15:22:59 +02002704 task_delete(t);
2705 task_free(t);
2706
2707 if (!h2c) {
2708 /* resources were already deleted */
2709 return NULL;
2710 }
2711
2712 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002713 h2c_error(h2c, H2_ERR_NO_ERROR);
2714 h2_wake_some_streams(h2c, 0, 0);
2715
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002716 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002717 /* don't even try to send a GOAWAY, the buffer is stuck */
2718 h2c->flags |= H2_CF_GOAWAY_FAILED;
2719 }
2720
2721 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002722 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002723 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2724 h2c->flags |= H2_CF_GOAWAY_FAILED;
2725
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002726 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2727 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002728 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002729 b_del(&h2c->mbuf, ret);
2730 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002731 }
2732 }
Willy Tarreauea392822017-10-31 10:02:25 +01002733
Willy Tarreau0975f112018-03-29 15:22:59 +02002734 /* either we can release everything now or it will be done later once
2735 * the last stream closes.
2736 */
2737 if (eb_is_empty(&h2c->streams_by_id))
2738 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002739
Willy Tarreauea392822017-10-31 10:02:25 +01002740 return NULL;
2741}
2742
2743
Willy Tarreau62f52692017-10-08 23:01:42 +02002744/*******************************************/
2745/* functions below are used by the streams */
2746/*******************************************/
2747
2748/*
2749 * Attach a new stream to a connection
2750 * (Used for outgoing connections)
2751 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002752static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002753{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002754 struct conn_stream *cs;
2755 struct h2s *h2s;
2756 struct h2c *h2c = conn->mux_ctx;
2757
2758 cs = cs_new(conn);
2759 if (!cs)
2760 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002761 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002762 if (!h2s) {
2763 cs_free(cs);
2764 return NULL;
2765 }
2766 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02002767}
2768
Willy Tarreaufafd3982018-11-18 21:29:20 +01002769/* Retrieves the first valid conn_stream from this connection, or returns NULL.
2770 * We have to scan because we may have some orphan streams. It might be
2771 * beneficial to scan backwards from the end to reduce the likeliness to find
2772 * orphans.
2773 */
2774static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
2775{
2776 struct h2c *h2c = conn->mux_ctx;
2777 struct h2s *h2s;
2778 struct eb32_node *node;
2779
2780 node = eb32_first(&h2c->streams_by_id);
2781 while (node) {
2782 h2s = container_of(node, struct h2s, by_id);
2783 if (h2s->cs)
2784 return h2s->cs;
2785 node = eb32_next(node);
2786 }
2787 return NULL;
2788}
2789
Willy Tarreau62f52692017-10-08 23:01:42 +02002790/*
Olivier Houchard060ed432018-11-06 16:32:42 +01002791 * Destroy the mux and the associated connection, if it is no longer used
2792 */
2793static void h2_destroy(struct connection *conn)
2794{
2795 struct h2c *h2c = conn->mux_ctx;
2796
2797 if (eb_is_empty(&h2c->streams_by_id))
2798 h2_release(h2c->conn);
2799}
2800
2801/*
Willy Tarreau62f52692017-10-08 23:01:42 +02002802 * Detach the stream from the connection and possibly release the connection.
2803 */
2804static void h2_detach(struct conn_stream *cs)
2805{
Willy Tarreau60935142017-10-16 18:11:19 +02002806 struct h2s *h2s = cs->ctx;
2807 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002808 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002809
2810 cs->ctx = NULL;
2811 if (!h2s)
2812 return;
2813
Olivier Houchardf502aca2018-12-14 19:42:40 +01002814 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02002815 h2c = h2s->h2c;
2816 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002817 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002818 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2819 !h2_has_too_many_cs(h2c)) {
2820 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard53216e72018-10-10 15:46:36 +02002821 if (h2_recv_allowed(h2c))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002822 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreauf2101912018-07-19 10:11:38 +02002823 }
Willy Tarreau60935142017-10-16 18:11:19 +02002824
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002825 /* this stream may be blocked waiting for some data to leave (possibly
2826 * an ES or RST frame), so orphan it in this case.
2827 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002828 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002829 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002830 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002831 return;
2832
Willy Tarreau45f752e2017-10-30 15:44:59 +01002833 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2834 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2835 /* unblock the connection if it was blocked on this
2836 * stream.
2837 */
2838 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2839 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002840 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002841 }
2842
Willy Tarreau71049cc2018-03-28 13:56:39 +02002843 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002844
Olivier Houchard8a786902018-12-15 16:05:40 +01002845 if (h2c->flags & H2_CF_IS_BACK &&
2846 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01002847 if (!(h2c->conn->flags &
2848 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
2849 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01002850 h2c->conn->owner = sess;
2851 session_add_conn(sess, h2c->conn, h2c->conn->target);
Olivier Houchard8a786902018-12-15 16:05:40 +01002852 }
2853 /* Never ever allow to reuse a connection from a non-reuse backend */
2854 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
2855 h2c->conn->flags |= CO_FL_PRIVATE;
2856 if (LIST_ISEMPTY(&h2c->conn->list)) {
2857 struct server *srv = objt_server(h2c->conn->target);
2858
2859 if (srv) {
2860 if (h2c->conn->flags & CO_FL_PRIVATE)
2861 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
2862 else
2863 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
2864 }
2865
2866 }
2867 }
2868 }
2869
Willy Tarreaue323f342018-03-28 13:51:45 +02002870 /* We don't want to close right now unless we're removing the
2871 * last stream, and either the connection is in error, or it
2872 * reached the ID already specified in a GOAWAY frame received
2873 * or sent (as seen by last_sid >= 0).
2874 */
2875 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2876 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002877 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Olivier Houchard52b94662018-10-21 03:01:20 +02002878 (h2c->flags & (H2_CF_GOAWAY_FAILED | H2_CF_GOAWAY_SENT)) ||
Olivier Houchard93c88522018-11-30 15:39:16 +01002879 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002880 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002881 (conn_xprt_read0_pending(h2c->conn) ||
2882 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2883 /* no more stream will come, kill it now */
2884 h2_release(h2c->conn);
2885 }
2886 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002887 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002888 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2889 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002890 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002891 else
2892 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002893 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002894}
2895
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002896static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002897{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002898 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002899 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002900
Willy Tarreau721c9742017-11-07 11:05:42 +01002901 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002902 return;
2903
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002904 /* if no outgoing data was seen on this stream, it means it was
2905 * closed with a "tcp-request content" rule that is normally
2906 * used to kill the connection ASAP (eg: limit abuse). In this
2907 * case we send a goaway to close the connection.
2908 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002909 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002910 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002911 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002912
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002913 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2914 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002915 h2c_send_goaway_error(h2c, h2s) <= 0)
2916 return;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002917
Olivier Houchard435ce2d2018-12-03 18:43:16 +01002918 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
2919 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01002920 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002921
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002922 return;
2923add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002924 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002925 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002926 if (h2s->flags & H2_SF_BLK_MFCTL) {
2927 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2928 h2s->send_wait = sw;
2929 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2930 h2s->send_wait = sw;
2931 LIST_ADDQ(&h2c->send_list, &h2s->list);
2932 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002933 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002934 /* Let the handler know we want shutr */
2935 sw->handle = (void *)((long)sw->handle | 1);
2936
Willy Tarreau62f52692017-10-08 23:01:42 +02002937}
2938
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002939static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002940{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002941 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002942 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002943
Willy Tarreau721c9742017-11-07 11:05:42 +01002944 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002945 return;
2946
Willy Tarreau67434202017-11-06 20:20:51 +01002947 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002948 /* we can cleanly close using an empty data frame only after headers */
2949
2950 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2951 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002952 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002953
2954 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002955 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002956 else
2957 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002958 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002959 /* if no outgoing data was seen on this stream, it means it was
2960 * closed with a "tcp-request content" rule that is normally
2961 * used to kill the connection ASAP (eg: limit abuse). In this
2962 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002963 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002964 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002965 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002966 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002967
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002968 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2969 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002970 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002971 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002972
Willy Tarreau00dd0782018-03-01 16:31:34 +01002973 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002974 }
2975
Olivier Houchard435ce2d2018-12-03 18:43:16 +01002976 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
2977 tasklet_wakeup(h2c->wait_event.task);
2978 return;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002979
2980 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002981 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002982 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002983 if (h2s->flags & H2_SF_BLK_MFCTL) {
2984 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2985 h2s->send_wait = sw;
2986 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2987 h2s->send_wait = sw;
2988 LIST_ADDQ(&h2c->send_list, &h2s->list);
2989 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002990 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002991 /* let the handler know we want to shutw */
2992 sw->handle = (void *)((long)(sw->handle) | 2);
2993
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002994}
2995
2996static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
2997{
2998 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002999 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003000
Olivier Houchard2c68a462018-12-15 22:42:20 +01003001 if (h2s->send_wait) {
3002 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
3003 h2s->send_wait = NULL;
3004 LIST_DEL(&h2s->list);
3005 LIST_INIT(&h2s->list);
3006 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003007 if (reason & 2)
3008 h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003009 if (reason & 1)
3010 h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003011
Olivier Houchard2c68a462018-12-15 22:42:20 +01003012 if (h2s->st == H2_SS_CLOSED &&
3013 !((h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL))))
3014 h2s_destroy(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003015 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003016}
3017
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003018static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3019{
3020 struct h2s *h2s = cs->ctx;
3021
3022 if (!mode)
3023 return;
3024
3025 h2_do_shutr(h2s);
3026}
3027
3028static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3029{
3030 struct h2s *h2s = cs->ctx;
3031
3032 h2_do_shutw(h2s);
3033}
3034
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003035/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003036 * HTX request or response depending on the connection's side. Returns the
3037 * number of bytes emitted if > 0, or 0 if it couldn't proceed. Stream errors
3038 * are reported in h2s->errcode and connection errors in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02003039 */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003040static int h2s_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02003041{
3042 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003043 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003044 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003045 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003046 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003047 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01003048 struct buffer *csbuf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003049 struct htx *htx = NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02003050 int flen = h2c->dfl;
3051 int outlen = 0;
3052 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003053 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003054
3055 if (!h2c->dfl) {
3056 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01003057 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02003058 return 0;
3059 }
3060
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003061 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01003062 return 0; // incomplete input frame
3063
Willy Tarreau13278b42017-10-13 19:23:14 +02003064 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003065 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003066 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003067 copy = alloc_trash_chunk();
3068 if (!copy) {
3069 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3070 goto fail;
3071 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003072 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3073 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3074 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003075 }
3076
3077 /* The padlen is the first byte before data, and the padding appears
3078 * after data. padlen+data+padding are included in flen.
3079 */
3080 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003081 h2c->dpl = *hdrs;
3082 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02003083 /* RFC7540#6.2 : pad length = length of frame payload or greater */
3084 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003085 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02003086 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003087 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02003088 hdrs += 1; // skip Pad Length
3089 }
3090
3091 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3092 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003093 if (read_n32(hdrs) == h2s->id) {
3094 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3095 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003096 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003097 }
3098
Willy Tarreau13278b42017-10-13 19:23:14 +02003099 hdrs += 5; // stream dep = 4, weight = 1
3100 flen -= 5;
3101 }
3102
3103 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
3104 * don't support this for now and can't even decompress so we have to
3105 * break the connection.
3106 */
3107 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
3108 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003109 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02003110 }
3111
Olivier Houchard638b7992018-08-16 15:41:52 +02003112 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01003113 if (!csbuf) {
3114 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003115 goto fail;
3116 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003117
Willy Tarreau937f7602018-02-26 15:22:17 +01003118 /* we can't retry a failed decompression operation so we must be very
3119 * careful not to take any risks. In practice the output buffer is
3120 * always empty except maybe for trailers, in which case we simply have
3121 * to wait for the upper layer to finish consuming what is available.
3122 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003123
3124 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
3125 htx = htx_from_buf(&h2s->rxbuf);
3126 if (!htx_is_empty(htx))
3127 goto fail;
3128 } else {
3129 if (b_data(csbuf))
3130 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003131
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003132 csbuf->head = 0;
3133 try = b_size(csbuf);
3134 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003135
3136 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3137 sizeof(list)/sizeof(list[0]), tmp);
3138 if (outlen < 0) {
3139 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3140 goto fail;
3141 }
3142
3143 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02003144 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003145
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003146 if (htx) {
3147 /* HTX mode */
3148 if (h2c->flags & H2_CF_IS_BACK)
3149 outlen = h2_make_htx_response(list, htx, &msgf);
3150 else
3151 outlen = h2_make_htx_request(list, htx, &msgf);
3152 } else {
3153 /* HTTP/1 mode */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003154 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003155 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003156
3157 if (outlen < 0) {
3158 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3159 goto fail;
3160 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003161
Willy Tarreau174b06a2018-04-25 18:13:58 +02003162 if (msgf & H2_MSGF_BODY) {
3163 /* a payload is present */
3164 if (msgf & H2_MSGF_BODY_CL)
3165 h2s->flags |= H2_SF_DATA_CLEN;
Olivier Houchard50d660c2018-12-08 00:18:31 +01003166 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau174b06a2018-04-25 18:13:58 +02003167 h2s->flags |= H2_SF_DATA_CHNK;
3168 }
3169
Willy Tarreau13278b42017-10-13 19:23:14 +02003170 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003171 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02003172 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01003173 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02003174
Willy Tarreau39d68502018-03-02 12:26:37 +01003175 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02003176 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003177 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003178 if (htx)
3179 htx_add_endof(htx, HTX_BLK_EOM);
Willy Tarreau39d68502018-03-02 12:26:37 +01003180 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003181
Willy Tarreau68dd9852017-07-03 14:44:26 +02003182 leave:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003183 if (htx)
3184 htx_to_buf(htx, &h2s->rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003185 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02003186 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003187 fail:
3188 outlen = 0;
3189 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02003190}
3191
Willy Tarreau454f9052017-10-26 19:40:35 +02003192/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3193 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3194 * in use, a new chunk is emitted for each frame. This is supposed to fit
3195 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3196 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3197 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003198 * parser state is automatically updated. Returns > 0 if it could completely
3199 * send the current frame, 0 if it couldn't complete, in which case
3200 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3201 * DATA frame can return 0 as a valid result). Stream errors are reported in
3202 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3203 * have checked the frame header and ensured that the frame was complete or the
3204 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003205 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003206static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003207{
3208 struct h2c *h2c = h2s->h2c;
3209 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003210 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003211 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003212 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003213 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003214
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003215 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003216
3217 /* The padlen is the first byte before data, and the padding appears
3218 * after data. padlen+data+padding are included in flen.
3219 */
Willy Tarreau79127812017-12-03 21:06:59 +01003220 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003221 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003222 return 0;
3223
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003224 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01003225 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003226 /* RFC7540#6.1 : pad length = length of frame payload or greater */
3227 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02003228 return 0;
3229 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003230
3231 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003232 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003233 h2c->dfl--;
3234 h2c->rcvd_c++; h2c->rcvd_s++;
3235 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02003236 }
3237
Olivier Houchard638b7992018-08-16 15:41:52 +02003238 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003239 if (!csbuf) {
3240 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003241 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003242 }
3243
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003244try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003245 flen = h2c->dfl - h2c->dpl;
3246 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003247 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003248
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003249 if (flen > b_data(&h2c->dbuf)) {
3250 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003251 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003252 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003253 }
3254
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003255 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
3256 htx = htx_from_buf(csbuf);
3257 block1 = htx_free_data_space(htx);
3258 if (!block1) {
3259 h2c->flags |= H2_CF_DEM_SFULL;
3260 goto fail;
3261 }
3262 if (flen > block1)
3263 flen = block1;
3264
3265 /* here, flen is the max we can copy into the output buffer */
3266 block1 = b_contig_data(&h2c->dbuf, 0);
3267 if (flen > block1)
3268 flen = block1;
3269
3270 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3271 h2c->flags |= H2_CF_DEM_SFULL;
3272 goto fail;
3273 }
3274
3275 b_del(&h2c->dbuf, flen);
3276 h2c->dfl -= flen;
3277 h2c->rcvd_c += flen;
3278 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3279 goto try_again;
3280 }
3281 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003282 /* it doesn't fit and the buffer is fragmented,
3283 * so let's defragment it and try again.
3284 */
3285 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003286 }
3287
Willy Tarreaueba10f22018-04-25 20:44:22 +02003288 /* chunked-encoding requires more room */
3289 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003290 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003291 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3292 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3293 (chklen < 1048576) ? 4 : 8;
3294 chklen += 4; // CRLF, CRLF
3295 }
3296
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003297 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003298 if (flen + chklen > b_room(csbuf)) {
3299 if (chklen >= b_room(csbuf)) {
3300 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003301 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003302 }
3303 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003304 }
3305
3306 if (h2s->flags & H2_SF_DATA_CHNK) {
3307 /* emit the chunk size */
3308 unsigned int chksz = flen;
3309 char str[10];
3310 char *beg;
3311
3312 beg = str + sizeof(str);
3313 *--beg = '\n';
3314 *--beg = '\r';
3315 do {
3316 *--beg = hextab[chksz & 0xF];
3317 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003318 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003319 }
3320
Willy Tarreau454f9052017-10-26 19:40:35 +02003321 /* Block1 is the length of the first block before the buffer wraps,
3322 * block2 is the optional second block to reach the end of the frame.
3323 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003324 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003325 if (block1 > flen)
3326 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003327 block2 = flen - block1;
3328
3329 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003330 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003331
3332 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003333 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003334
Willy Tarreaueba10f22018-04-25 20:44:22 +02003335 if (h2s->flags & H2_SF_DATA_CHNK) {
3336 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003337 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003338 }
3339
Willy Tarreau454f9052017-10-26 19:40:35 +02003340 /* now mark the input data as consumed (will be deleted from the buffer
3341 * by the caller when seeing FRAME_A after sending the window update).
3342 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003343 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003344 h2c->dfl -= flen;
3345 h2c->rcvd_c += flen;
3346 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3347
3348 if (h2c->dfl > h2c->dpl) {
3349 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003350 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003351 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003352 }
3353
Willy Tarreau4a28da12018-01-04 14:41:00 +01003354 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003355 /* here we're done with the frame, all the payload (except padding) was
3356 * transferred.
3357 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003358
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003359 if (h2c->dff & H2_F_DATA_END_STREAM) {
3360 if (htx) {
3361 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3362 h2c->flags |= H2_CF_DEM_SFULL;
3363 goto fail;
3364 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003365 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003366 else if (h2s->flags & H2_SF_DATA_CHNK) {
3367 /* emit the trailing 0 CRLF CRLF */
3368 if (b_room(csbuf) < 5) {
3369 h2c->flags |= H2_CF_DEM_SFULL;
3370 goto fail;
3371 }
3372 chklen += 5;
3373 b_putblk(csbuf, "0\r\n\r\n", 5);
3374 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003375 }
3376
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003377 h2c->rcvd_c += h2c->dpl;
3378 h2c->rcvd_s += h2c->dpl;
3379 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003380 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3381
Willy Tarreau39d68502018-03-02 12:26:37 +01003382 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003383 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003384 h2s->cs->flags |= CS_FL_REOS;
3385 }
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003386 if (htx)
3387 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003388 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003389 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003390 if (htx)
3391 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003392 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003393}
3394
Willy Tarreau5dd17352018-06-14 13:33:30 +02003395/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3396 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3397 * number of bytes sent. The caller must check the stream's status to detect
3398 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003399 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003400static 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 +02003401{
3402 struct http_hdr list[MAX_HTTP_HDR];
3403 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003404 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003405 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003406 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003407 int es_now = 0;
3408 int ret = 0;
3409 int hdr;
3410
3411 if (h2c_mux_busy(h2c, h2s)) {
3412 h2s->flags |= H2_SF_BLK_MBUSY;
3413 return 0;
3414 }
3415
Willy Tarreau44e973f2018-03-01 17:49:30 +01003416 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003417 h2c->flags |= H2_CF_MUX_MALLOC;
3418 h2s->flags |= H2_SF_BLK_MROOM;
3419 return 0;
3420 }
3421
3422 /* First, try to parse the H1 response and index it into <list>.
3423 * NOTE! Since it comes from haproxy, we *know* that a response header
3424 * block does not wrap and we can safely read it this way without
3425 * having to realign the buffer.
3426 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003427 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003428 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003429 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003430 /* incomplete or invalid response, this is abnormal coming from
3431 * haproxy and may only result in a bad errorfile or bad Lua code
3432 * so that won't be fixed, raise an error now.
3433 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003434 * FIXME: we should instead add the ability to only return a
3435 * 502 bad gateway. But in theory this is not supposed to
3436 * happen.
3437 */
3438 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3439 ret = 0;
3440 goto end;
3441 }
3442
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003443 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003444
3445 /* certain statuses have no body or an empty one, regardless of
3446 * what the headers say.
3447 */
3448 if (sl.st.status >= 100 && sl.st.status < 200) {
3449 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3450 h1m->curr_len = h1m->body_len = 0;
3451 }
3452 else if (sl.st.status == 204 || sl.st.status == 304) {
3453 /* no contents, claim c-len is present and set to zero */
3454 h1m->flags &= ~H1_MF_CHNK;
3455 h1m->flags |= H1_MF_CLEN;
3456 h1m->curr_len = h1m->body_len = 0;
3457 }
3458
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003459 chunk_reset(&outbuf);
3460
3461 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003462 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003463 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003464 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003465
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003466 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003467 break;
3468 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003469 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003470 }
3471
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003472 if (outbuf.size < 9)
3473 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003474
3475 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003476 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3477 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3478 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003479
3480 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003481 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003482 /* this is an unparsable response */
3483 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3484 ret = 0;
3485 goto end;
3486 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003487
3488 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003489 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003490 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003491 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003492 }
3493
3494 /* encode all headers, stop at empty name */
3495 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003496 /* these ones do not exist in H2 and must be dropped. */
3497 if (isteq(list[hdr].n, ist("connection")) ||
3498 isteq(list[hdr].n, ist("proxy-connection")) ||
3499 isteq(list[hdr].n, ist("keep-alive")) ||
3500 isteq(list[hdr].n, ist("upgrade")) ||
3501 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003502 continue;
3503
3504 if (isteq(list[hdr].n, ist("")))
3505 break; // end
3506
3507 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3508 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003509 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003510 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003511 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003512 }
3513 }
3514
3515 /* we may need to add END_STREAM */
3516 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3517 es_now = 1;
3518
3519 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003520 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003521
3522 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003523 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003524
3525 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003526 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003527
3528 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003529 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003530 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003531
3532 /* for now we don't implemented CONTINUATION, so we wait for a
3533 * body or directly end in TRL2.
3534 */
3535 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003536 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003537 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003538
Willy Tarreau801250e2018-09-11 11:45:04 +02003539 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003540 h2s->flags |= H2_SF_ES_SENT;
3541 if (h2s->st == H2_SS_OPEN)
3542 h2s->st = H2_SS_HLOC;
3543 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003544 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003545 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003546 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003547 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003548 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003549 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003550 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003551 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003552 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003553
3554 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003555
3556 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003557 //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 +02003558 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003559 full:
3560 h1m_init_res(h1m);
3561 h1m->err_pos = -1; // don't care about errors on the response path
3562 h2c->flags |= H2_CF_MUX_MFULL;
3563 h2s->flags |= H2_SF_BLK_MROOM;
3564 ret = 0;
3565 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003566}
3567
Willy Tarreau5dd17352018-06-14 13:33:30 +02003568/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3569 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3570 * the number of bytes sent. The caller must check the stream's status to
3571 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003572 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003573static 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 +02003574{
3575 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003576 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003577 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003578 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003579 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003580 int es_now = 0;
3581 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003582 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003583 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003584
3585 if (h2c_mux_busy(h2c, h2s)) {
3586 h2s->flags |= H2_SF_BLK_MBUSY;
3587 goto end;
3588 }
3589
Willy Tarreau44e973f2018-03-01 17:49:30 +01003590 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003591 h2c->flags |= H2_CF_MUX_MALLOC;
3592 h2s->flags |= H2_SF_BLK_MROOM;
3593 goto end;
3594 }
3595
3596 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003597 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003598 goto end;
3599
3600 chunk_reset(&outbuf);
3601
3602 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003603 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003604 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003605 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003606
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003607 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003608 break;
3609 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01003610 /* If there are pending data in the output buffer, and we have
3611 * less than 1/4 of the mbuf's size and everything fits, we'll
3612 * still perform a copy anyway. Otherwise we'll pretend the mbuf
3613 * is full and wait, to save some slow realign calls.
3614 */
3615 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
3616 h2c->flags |= H2_CF_MUX_MFULL;
3617 h2s->flags |= H2_SF_BLK_MROOM;
3618 goto end;
3619 }
3620
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003621 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003622 }
3623
3624 if (outbuf.size < 9) {
3625 h2c->flags |= H2_CF_MUX_MFULL;
3626 h2s->flags |= H2_SF_BLK_MROOM;
3627 goto end;
3628 }
3629
3630 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003631 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3632 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3633 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003634
3635 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3636 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003637 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003638 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003639 break;
3640 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003641 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003642 if ((long long)size > h1m->curr_len)
3643 size = h1m->curr_len;
3644 break;
3645 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003646 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003647 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003648 if (!ret)
3649 goto end;
3650
3651 if (ret < 0) {
3652 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003653 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003654 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3655 goto end;
3656 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003657 max -= ret;
3658 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003659 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003660 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003661 }
3662
Willy Tarreau801250e2018-09-11 11:45:04 +02003663 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003664 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003665 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003666 if (!ret)
3667 goto end;
3668
3669 if (ret < 0) {
3670 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003671 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003672 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3673 goto end;
3674 }
3675
3676 size = chunk;
3677 h1m->curr_len = chunk;
3678 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003679 max -= ret;
3680 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003681 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003682 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003683 if (!size)
3684 goto send_empty;
3685 }
3686
3687 /* in MSG_DATA state, continue below */
3688 size = h1m->curr_len;
3689 break;
3690 }
3691
3692 /* we have in <size> the exact number of bytes we need to copy from
3693 * the H1 buffer. We need to check this against the connection's and
3694 * the stream's send windows, and to ensure that this fits in the max
3695 * frame size and in the buffer's available space minus 9 bytes (for
3696 * the frame header). The connection's flow control is applied last so
3697 * that we can use a separate list of streams which are immediately
3698 * unblocked on window opening. Note: we don't implement padding.
3699 */
3700
Willy Tarreau5dd17352018-06-14 13:33:30 +02003701 if (size > max)
3702 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003703
3704 if (size > h2s->mws)
3705 size = h2s->mws;
3706
3707 if (size <= 0) {
3708 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02003709 if (h2s->send_wait) {
3710 LIST_DEL(&h2s->list);
3711 LIST_INIT(&h2s->list);
3712 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003713 goto end;
3714 }
3715
3716 if (h2c->mfs && size > h2c->mfs)
3717 size = h2c->mfs;
3718
3719 if (size + 9 > outbuf.size) {
3720 /* we have an opportunity for enlarging the too small
3721 * available space, let's try.
3722 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003723 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003724 goto realign_again;
3725 size = outbuf.size - 9;
3726 }
3727
3728 if (size <= 0) {
3729 h2c->flags |= H2_CF_MUX_MFULL;
3730 h2s->flags |= H2_SF_BLK_MROOM;
3731 goto end;
3732 }
3733
3734 if (size > h2c->mws)
3735 size = h2c->mws;
3736
3737 if (size <= 0) {
3738 h2s->flags |= H2_SF_BLK_MFCTL;
3739 goto end;
3740 }
3741
3742 /* copy whatever we can */
3743 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003744 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003745 if (ret == 1)
3746 len2 = 0;
3747
3748 if (!ret || len1 + len2 < size) {
3749 /* FIXME: must normally never happen */
3750 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3751 goto end;
3752 }
3753
3754 /* limit len1/len2 to size */
3755 if (len1 + len2 > size) {
3756 int sub = len1 + len2 - size;
3757
3758 if (len2 > sub)
3759 len2 -= sub;
3760 else {
3761 sub -= len2;
3762 len2 = 0;
3763 len1 -= sub;
3764 }
3765 }
3766
3767 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003768 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003769 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003770 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003771
3772 send_empty:
3773 /* we may need to add END_STREAM */
3774 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3775 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003776 *
3777 * FIXME: what we do here is not correct because we send end_stream
3778 * before knowing if we'll have to send a HEADERS frame for the
3779 * trailers. More importantly we're not consuming the trailing CRLF
3780 * after the end of trailers, so it will be left to the caller to
3781 * eat it. The right way to do it would be to measure trailers here
3782 * and to send ES only if there are no trailers.
3783 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003784 */
3785 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02003786 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003787 es_now = 1;
3788
3789 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003790 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003791
3792 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003793 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003794
3795 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003796 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003797
3798 /* consume incoming H1 response */
3799 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003800 max -= size;
3801 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003802 total += size;
3803 h1m->curr_len -= size;
3804 h2s->mws -= size;
3805 h2c->mws -= size;
3806
3807 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02003808 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003809 goto new_frame;
3810 }
3811 }
3812
3813 if (es_now) {
3814 if (h2s->st == H2_SS_OPEN)
3815 h2s->st = H2_SS_HLOC;
3816 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003817 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003818
Willy Tarreau35a62702018-02-27 15:37:25 +01003819 if (!(h1m->flags & H1_MF_CHNK)) {
3820 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003821 total += max;
3822 ofs += max;
3823 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003824
Willy Tarreau801250e2018-09-11 11:45:04 +02003825 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003826 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003827
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003828 h2s->flags |= H2_SF_ES_SENT;
3829 }
3830
3831 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003832 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 +02003833 return total;
3834}
3835
Willy Tarreau115e83b2018-12-01 19:17:53 +01003836/* Try to send a HEADERS frame matching HTX response present in HTX message
3837 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
3838 * must check the stream's status to detect any error which might have happened
3839 * subsequently to a successful send. The htx blocks are automatically removed
3840 * from the message. The htx message is assumed to be valid since produced from
3841 * the internal code, hence it contains a start line, an optional series of
3842 * header blocks and an end of header, otherwise an invalid frame could be
3843 * emitted and the resulting htx message could be left in an inconsistent state.
3844 */
3845static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
3846{
3847 struct http_hdr list[MAX_HTTP_HDR];
3848 struct h2c *h2c = h2s->h2c;
3849 struct htx_blk *blk;
3850 struct htx_blk *blk_end;
3851 struct buffer outbuf;
3852 struct htx_sl *sl;
3853 enum htx_blk_type type;
3854 int es_now = 0;
3855 int ret = 0;
3856 int hdr;
3857 int idx;
3858
3859 if (h2c_mux_busy(h2c, h2s)) {
3860 h2s->flags |= H2_SF_BLK_MBUSY;
3861 return 0;
3862 }
3863
3864 if (!h2_get_buf(h2c, &h2c->mbuf)) {
3865 h2c->flags |= H2_CF_MUX_MALLOC;
3866 h2s->flags |= H2_SF_BLK_MROOM;
3867 return 0;
3868 }
3869
3870 /* determine the first block which must not be deleted, blk_end may
3871 * be NULL if all blocks have to be deleted.
3872 */
3873 idx = htx_get_head(htx);
3874 blk_end = NULL;
3875 while (idx != -1) {
3876 type = htx_get_blk_type(htx_get_blk(htx, idx));
3877 idx = htx_get_next(htx, idx);
3878 if (type == HTX_BLK_EOH) {
3879 if (idx != -1)
3880 blk_end = htx_get_blk(htx, idx);
3881 break;
3882 }
3883 }
3884
3885 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01003886 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01003887 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003888 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01003889 if (h2s->status < 100 || h2s->status > 999)
3890 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01003891
3892 /* and the rest of the headers, that we dump starting at header 0 */
3893 hdr = 0;
3894
Willy Tarreau8e162ee2018-12-06 14:07:27 +01003895 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01003896 while ((idx = htx_get_next(htx, idx)) != -1) {
3897 blk = htx_get_blk(htx, idx);
3898 type = htx_get_blk_type(blk);
3899
3900 if (type == HTX_BLK_UNUSED)
3901 continue;
3902
3903 if (type != HTX_BLK_HDR)
3904 break;
3905
3906 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
3907 goto fail;
3908
3909 list[hdr].n = htx_get_blk_name(htx, blk);
3910 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01003911 hdr++;
3912 }
3913
3914 /* marker for end of headers */
3915 list[hdr].n = ist("");
3916
3917 if (h2s->status == 204 || h2s->status == 304) {
3918 /* no contents, claim c-len is present and set to zero */
3919 es_now = 1;
3920 }
3921
3922 chunk_reset(&outbuf);
3923
3924 while (1) {
3925 outbuf.area = b_tail(&h2c->mbuf);
3926 outbuf.size = b_contig_space(&h2c->mbuf);
3927 outbuf.data = 0;
3928
3929 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
3930 break;
3931 realign_again:
3932 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
3933 }
3934
3935 if (outbuf.size < 9)
3936 goto full;
3937
3938 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
3939 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3940 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3941 outbuf.data = 9;
3942
3943 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003944 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01003945 if (b_space_wraps(&h2c->mbuf))
3946 goto realign_again;
3947 goto full;
3948 }
3949
3950 /* encode all headers, stop at empty name */
3951 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
3952 /* these ones do not exist in H2 and must be dropped. */
3953 if (isteq(list[hdr].n, ist("connection")) ||
3954 isteq(list[hdr].n, ist("proxy-connection")) ||
3955 isteq(list[hdr].n, ist("keep-alive")) ||
3956 isteq(list[hdr].n, ist("upgrade")) ||
3957 isteq(list[hdr].n, ist("transfer-encoding")))
3958 continue;
3959
3960 if (isteq(list[hdr].n, ist("")))
3961 break; // end
3962
3963 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3964 /* output full */
3965 if (b_space_wraps(&h2c->mbuf))
3966 goto realign_again;
3967 goto full;
3968 }
3969 }
3970
3971 /* we may need to add END_STREAM.
3972 * FIXME: we should also set it when we know for sure that the
3973 * content-length is zero as well as on 204/304
3974 */
3975 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
3976 es_now = 1;
3977
3978 if (h2s->cs->flags & CS_FL_SHW)
3979 es_now = 1;
3980
3981 /* update the frame's size */
3982 h2_set_frame_size(outbuf.area, outbuf.data - 9);
3983
3984 if (es_now)
3985 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
3986
3987 /* commit the H2 response */
3988 b_add(&h2c->mbuf, outbuf.data);
3989 h2s->flags |= H2_SF_HEADERS_SENT;
3990
3991 /* for now we don't implemented CONTINUATION, so we wait for a
3992 * body or directly end in TRL2.
3993 */
3994 if (es_now) {
3995 h2s->flags |= H2_SF_ES_SENT;
3996 if (h2s->st == H2_SS_OPEN)
3997 h2s->st = H2_SS_HLOC;
3998 else
3999 h2s_close(h2s);
4000 }
4001
4002 /* OK we could properly deliver the response */
4003
4004 /* remove all header blocks including the EOH and compute the
4005 * corresponding size.
4006 *
4007 * FIXME: We should remove everything when es_now is set.
4008 */
4009 ret = 0;
4010 idx = htx_get_head(htx);
4011 blk = htx_get_blk(htx, idx);
4012 while (blk != blk_end) {
4013 ret += htx_get_blksz(blk);
4014 blk = htx_remove_blk(htx, blk);
4015 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004016
4017 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4018 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004019 end:
4020 return ret;
4021 full:
4022 h2c->flags |= H2_CF_MUX_MFULL;
4023 h2s->flags |= H2_SF_BLK_MROOM;
4024 ret = 0;
4025 goto end;
4026 fail:
4027 /* unparsable HTX messages, too large ones to be produced in the local
4028 * list etc go here (unrecoverable errors).
4029 */
4030 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4031 ret = 0;
4032 goto end;
4033}
4034
Willy Tarreau80739692018-10-05 11:35:57 +02004035/* Try to send a HEADERS frame matching HTX request present in HTX message
4036 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4037 * must check the stream's status to detect any error which might have happened
4038 * subsequently to a successful send. The htx blocks are automatically removed
4039 * from the message. The htx message is assumed to be valid since produced from
4040 * the internal code, hence it contains a start line, an optional series of
4041 * header blocks and an end of header, otherwise an invalid frame could be
4042 * emitted and the resulting htx message could be left in an inconsistent state.
4043 */
4044static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4045{
4046 struct http_hdr list[MAX_HTTP_HDR];
4047 struct h2c *h2c = h2s->h2c;
4048 struct htx_blk *blk;
4049 struct htx_blk *blk_end;
4050 struct buffer outbuf;
4051 struct htx_sl *sl;
4052 struct ist meth, path;
4053 enum htx_blk_type type;
4054 int es_now = 0;
4055 int ret = 0;
4056 int hdr;
4057 int idx;
4058
4059 if (h2c_mux_busy(h2c, h2s)) {
4060 h2s->flags |= H2_SF_BLK_MBUSY;
4061 return 0;
4062 }
4063
4064 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4065 h2c->flags |= H2_CF_MUX_MALLOC;
4066 h2s->flags |= H2_SF_BLK_MROOM;
4067 return 0;
4068 }
4069
4070 /* determine the first block which must not be deleted, blk_end may
4071 * be NULL if all blocks have to be deleted.
4072 */
4073 idx = htx_get_head(htx);
4074 blk_end = NULL;
4075 while (idx != -1) {
4076 type = htx_get_blk_type(htx_get_blk(htx, idx));
4077 idx = htx_get_next(htx, idx);
4078 if (type == HTX_BLK_EOH) {
4079 if (idx != -1)
4080 blk_end = htx_get_blk(htx, idx);
4081 break;
4082 }
4083 }
4084
4085 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004086 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004087 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004088 meth = htx_sl_req_meth(sl);
4089 path = htx_sl_req_uri(sl);
4090
4091 /* and the rest of the headers, that we dump starting at header 0 */
4092 hdr = 0;
4093
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004094 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004095 while ((idx = htx_get_next(htx, idx)) != -1) {
4096 blk = htx_get_blk(htx, idx);
4097 type = htx_get_blk_type(blk);
4098
4099 if (type == HTX_BLK_UNUSED)
4100 continue;
4101
4102 if (type != HTX_BLK_HDR)
4103 break;
4104
4105 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4106 goto fail;
4107
4108 list[hdr].n = htx_get_blk_name(htx, blk);
4109 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004110 hdr++;
4111 }
4112
4113 /* marker for end of headers */
4114 list[hdr].n = ist("");
4115
4116 chunk_reset(&outbuf);
4117
4118 while (1) {
4119 outbuf.area = b_tail(&h2c->mbuf);
4120 outbuf.size = b_contig_space(&h2c->mbuf);
4121 outbuf.data = 0;
4122
4123 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4124 break;
4125 realign_again:
4126 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4127 }
4128
4129 if (outbuf.size < 9)
4130 goto full;
4131
4132 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4133 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4134 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4135 outbuf.data = 9;
4136
4137 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004138 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004139 if (b_space_wraps(&h2c->mbuf))
4140 goto realign_again;
4141 goto full;
4142 }
4143
4144 /* encode the scheme which is always "https" (or 0x86 for "http") */
Willy Tarreau7561bcb2018-12-10 19:17:06 +01004145 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4146 /* output full */
4147 if (b_space_wraps(&h2c->mbuf))
4148 goto realign_again;
4149 goto full;
4150 }
Willy Tarreau80739692018-10-05 11:35:57 +02004151
4152 /* encode the path, which necessarily is the second one */
Willy Tarreau90799812018-12-10 19:28:38 +01004153 if (!hpack_encode_path(&outbuf, path)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004154 /* output full */
4155 if (b_space_wraps(&h2c->mbuf))
4156 goto realign_again;
4157 goto full;
4158 }
4159
4160 /* encode all headers, stop at empty name */
4161 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4162 /* these ones do not exist in H2 and must be dropped. */
4163 if (isteq(list[hdr].n, ist("connection")) ||
4164 isteq(list[hdr].n, ist("proxy-connection")) ||
4165 isteq(list[hdr].n, ist("keep-alive")) ||
4166 isteq(list[hdr].n, ist("upgrade")) ||
4167 isteq(list[hdr].n, ist("transfer-encoding")))
4168 continue;
4169
4170 if (isteq(list[hdr].n, ist("")))
4171 break; // end
4172
4173 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4174 /* output full */
4175 if (b_space_wraps(&h2c->mbuf))
4176 goto realign_again;
4177 goto full;
4178 }
4179 }
4180
4181 /* we may need to add END_STREAM if we have no body :
4182 * - request already closed, or :
4183 * - no transfer-encoding, and :
4184 * - no content-length or content-length:0
4185 * Fixme: this doesn't take into account CONNECT requests.
4186 */
4187 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4188 es_now = 1;
4189
4190 if (sl->flags & HTX_SL_F_BODYLESS)
4191 es_now = 1;
4192
4193 if (h2s->cs->flags & CS_FL_SHW)
4194 es_now = 1;
4195
4196 /* update the frame's size */
4197 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4198
4199 if (es_now)
4200 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4201
4202 /* commit the H2 response */
4203 b_add(&h2c->mbuf, outbuf.data);
4204 h2s->flags |= H2_SF_HEADERS_SENT;
4205 h2s->st = H2_SS_OPEN;
4206
4207 /* for now we don't implemented CONTINUATION, so we wait for a
4208 * body or directly end in TRL2.
4209 */
4210 if (es_now) {
4211 // trim any possibly pending data (eg: inconsistent content-length)
4212 h2s->flags |= H2_SF_ES_SENT;
4213 h2s->st = H2_SS_HLOC;
4214 }
4215
4216 /* remove all header blocks including the EOH and compute the
4217 * corresponding size.
4218 *
4219 * FIXME: We should remove everything when es_now is set.
4220 */
4221 ret = 0;
4222 idx = htx_get_head(htx);
4223 blk = htx_get_blk(htx, idx);
4224 while (blk != blk_end) {
4225 ret += htx_get_blksz(blk);
4226 blk = htx_remove_blk(htx, blk);
4227 }
4228
4229 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4230 htx_remove_blk(htx, blk_end);
4231
4232 end:
4233 return ret;
4234 full:
4235 h2c->flags |= H2_CF_MUX_MFULL;
4236 h2s->flags |= H2_SF_BLK_MROOM;
4237 ret = 0;
4238 goto end;
4239 fail:
4240 /* unparsable HTX messages, too large ones to be produced in the local
4241 * list etc go here (unrecoverable errors).
4242 */
4243 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4244 ret = 0;
4245 goto end;
4246}
4247
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004248/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004249 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4250 * caller must check the stream's status to detect any error which might have
4251 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004252 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4253 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004254static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004255{
4256 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004257 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004258 struct buffer outbuf;
4259 size_t total = 0;
4260 int es_now = 0;
4261 int bsize; /* htx block size */
4262 int fsize; /* h2 frame size */
4263 struct htx_blk *blk;
4264 enum htx_blk_type type;
4265 int idx;
4266
4267 if (h2c_mux_busy(h2c, h2s)) {
4268 h2s->flags |= H2_SF_BLK_MBUSY;
4269 goto end;
4270 }
4271
4272 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4273 h2c->flags |= H2_CF_MUX_MALLOC;
4274 h2s->flags |= H2_SF_BLK_MROOM;
4275 goto end;
4276 }
4277
Willy Tarreau98de12a2018-12-12 07:03:00 +01004278 htx = htx_from_buf(buf);
4279
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004280 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4281 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4282 * the caller to handle.
4283 */
4284
4285 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004286 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004287 goto end;
4288
4289 idx = htx_get_head(htx);
4290 blk = htx_get_blk(htx, idx);
4291 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4292 bsize = htx_get_blksz(blk);
4293 fsize = bsize;
4294
4295 if (type == HTX_BLK_EOD) {
4296 /* if we have an EOD, we're dealing with chunked data. We may
4297 * have a set of trailers after us that the caller will want to
4298 * deal with. Let's simply remove the EOD and return.
4299 */
4300 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004301 total++; // EOD counts as one byte
4302 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004303 goto end;
4304 }
4305
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004306 if (type != HTX_BLK_DATA && type != HTX_BLK_EOM)
4307 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004308
4309 /* Perform some optimizations to reduce the number of buffer copies.
4310 * First, if the mux's buffer is empty and the htx area contains
4311 * exactly one data block of the same size as the requested count, and
4312 * this count fits within the frame size, the stream's window size, and
4313 * the connection's window size, then it's possible to simply swap the
4314 * caller's buffer with the mux's output buffer and adjust offsets and
4315 * length to match the entire DATA HTX block in the middle. In this
4316 * case we perform a true zero-copy operation from end-to-end. This is
4317 * the situation that happens all the time with large files. Second, if
4318 * this is not possible, but the mux's output buffer is empty, we still
4319 * have an opportunity to avoid the copy to the intermediary buffer, by
4320 * making the intermediary buffer's area point to the output buffer's
4321 * area. In this case we want to skip the HTX header to make sure that
4322 * copies remain aligned and that this operation remains possible all
4323 * the time. This goes for headers, data blocks and any data extracted
4324 * from the HTX blocks.
4325 */
4326 if (unlikely(fsize == count &&
4327 htx->used == 1 && type == HTX_BLK_DATA &&
4328 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4329 void *old_area = h2c->mbuf.area;
4330
4331 if (b_data(&h2c->mbuf)) {
4332 /* too bad there are data left there. If we have less
4333 * than 1/4 of the mbuf's size and everything fits,
4334 * we'll perform a copy anyway. Otherwise we'll pretend
4335 * the mbuf is full and wait.
4336 */
4337 if (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_room(&h2c->mbuf))
4338 goto copy;
4339 h2c->flags |= H2_CF_MUX_MFULL;
4340 h2s->flags |= H2_SF_BLK_MROOM;
4341 goto end;
4342 }
4343
4344 /* map an H2 frame to the HTX block so that we can put the
4345 * frame header there.
4346 */
4347 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004348 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004349 h2c->mbuf.data = fsize + 9;
4350 outbuf.area = b_head(&h2c->mbuf);
4351
4352 /* prepend an H2 DATA frame header just before the DATA block */
4353 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4354 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4355 h2_set_frame_size(outbuf.area, fsize);
4356
4357 /* update windows */
4358 h2s->mws -= fsize;
4359 h2c->mws -= fsize;
4360
4361 /* and exchange with our old area */
4362 buf->area = old_area;
4363 buf->data = buf->head = 0;
4364 total += fsize;
4365 goto end;
4366 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004367
Willy Tarreau98de12a2018-12-12 07:03:00 +01004368 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004369 /* for DATA and EOM we'll have to emit a frame, even if empty */
4370
4371 while (1) {
4372 outbuf.area = b_tail(&h2c->mbuf);
4373 outbuf.size = b_contig_space(&h2c->mbuf);
4374 outbuf.data = 0;
4375
4376 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4377 break;
4378 realign_again:
4379 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4380 }
4381
4382 if (outbuf.size < 9) {
4383 h2c->flags |= H2_CF_MUX_MFULL;
4384 h2s->flags |= H2_SF_BLK_MROOM;
4385 goto end;
4386 }
4387
4388 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4389 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4390 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4391 outbuf.data = 9;
4392
4393 /* we have in <fsize> the exact number of bytes we need to copy from
4394 * the HTX buffer. We need to check this against the connection's and
4395 * the stream's send windows, and to ensure that this fits in the max
4396 * frame size and in the buffer's available space minus 9 bytes (for
4397 * the frame header). The connection's flow control is applied last so
4398 * that we can use a separate list of streams which are immediately
4399 * unblocked on window opening. Note: we don't implement padding.
4400 */
4401
4402 /* EOM is presented with bsize==1 but would lead to the emission of an
4403 * empty frame, thus we force it to zero here.
4404 */
4405 if (type == HTX_BLK_EOM)
4406 bsize = fsize = 0;
4407
4408 if (!fsize)
4409 goto send_empty;
4410
4411 if (h2s->mws <= 0) {
4412 h2s->flags |= H2_SF_BLK_SFCTL;
4413 if (h2s->send_wait) {
4414 LIST_DEL(&h2s->list);
4415 LIST_INIT(&h2s->list);
4416 }
4417 goto end;
4418 }
4419
Willy Tarreauee573762018-12-04 15:25:57 +01004420 if (fsize > count)
4421 fsize = count;
4422
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004423 if (fsize > h2s->mws)
4424 fsize = h2s->mws; // >0
4425
4426 if (h2c->mfs && fsize > h2c->mfs)
4427 fsize = h2c->mfs; // >0
4428
4429 if (fsize + 9 > outbuf.size) {
4430 /* we have an opportunity for enlarging the too small
4431 * available space, let's try.
4432 * FIXME: is this really interesting to do? Maybe we'll
4433 * spend lots of time realigning instead of using two
4434 * frames.
4435 */
4436 if (b_space_wraps(&h2c->mbuf))
4437 goto realign_again;
4438 fsize = outbuf.size - 9;
4439
4440 if (fsize <= 0) {
4441 /* no need to send an empty frame here */
4442 h2c->flags |= H2_CF_MUX_MFULL;
4443 h2s->flags |= H2_SF_BLK_MROOM;
4444 goto end;
4445 }
4446 }
4447
4448 if (h2c->mws <= 0) {
4449 h2s->flags |= H2_SF_BLK_MFCTL;
4450 goto end;
4451 }
4452
4453 if (fsize > h2c->mws)
4454 fsize = h2c->mws;
4455
4456 /* now let's copy this this into the output buffer */
4457 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004458 h2s->mws -= fsize;
4459 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004460 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004461
4462 send_empty:
4463 /* update the frame's size */
4464 h2_set_frame_size(outbuf.area, fsize);
4465
4466 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4467 * meeting EOM. We should optimize this later.
4468 */
4469 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004470 total++; // EOM counts as one byte
4471 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004472 es_now = 1;
4473 }
4474
4475 if (es_now)
4476 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4477
4478 /* commit the H2 response */
4479 b_add(&h2c->mbuf, fsize + 9);
4480
4481 /* consume incoming HTX block, including EOM */
4482 total += fsize;
4483 if (fsize == bsize) {
4484 htx_remove_blk(htx, blk);
4485 if (fsize)
4486 goto new_frame;
4487 } else {
4488 /* we've truncated this block */
4489 htx_cut_data_blk(htx, blk, fsize);
4490 }
4491
4492 if (es_now) {
4493 if (h2s->st == H2_SS_OPEN)
4494 h2s->st = H2_SS_HLOC;
4495 else
4496 h2s_close(h2s);
4497
4498 h2s->flags |= H2_SF_ES_SENT;
4499 }
4500
4501 end:
4502 return total;
4503}
4504
Olivier Houchard6ff20392018-07-17 18:46:31 +02004505/* Called from the upper layer, to subscribe to events, such as being able to send */
4506static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
4507{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004508 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004509 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004510 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004511
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004512 if (event_type & SUB_CAN_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004513 sw = param;
4514 if (!(sw->wait_reason & SUB_CAN_RECV)) {
4515 sw->wait_reason |= SUB_CAN_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004516 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004517 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004518 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004519 event_type &= ~SUB_CAN_RECV;
4520 }
4521 if (event_type & SUB_CAN_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02004522 sw = param;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02004523 if (!(sw->wait_reason & SUB_CAN_SEND)) {
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004524 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02004525 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004526 h2s->send_wait = sw;
4527 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
4528 if (h2s->flags & H2_SF_BLK_MFCTL)
4529 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
4530 else
4531 LIST_ADDQ(&h2c->send_list, &h2s->list);
4532 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02004533 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004534 event_type &= ~SUB_CAN_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004535 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004536 if (event_type != 0)
4537 return -1;
4538 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02004539
4540
4541}
4542
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004543static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
4544{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004545 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004546 struct h2s *h2s = cs->ctx;
4547
4548 if (event_type & SUB_CAN_RECV) {
4549 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004550 if (h2s->recv_wait == sw) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004551 sw->wait_reason &= ~SUB_CAN_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004552 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004553 }
4554 }
4555 if (event_type & SUB_CAN_SEND) {
4556 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004557 if (h2s->send_wait == sw) {
4558 LIST_DEL(&h2s->list);
4559 LIST_INIT(&h2s->list);
4560 sw->wait_reason &= ~SUB_CAN_SEND;
4561 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004562 }
4563 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004564 if (event_type & SUB_CALL_UNSUBSCRIBE) {
4565 sw = param;
4566 if (h2s->send_wait == sw) {
4567 sw->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
4568 h2s->send_wait = NULL;
4569 }
4570 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004571 return 0;
4572}
4573
4574
Olivier Houchard511efea2018-08-16 15:30:32 +02004575/* Called from the upper layer, to receive data */
4576static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
4577{
Olivier Houchard638b7992018-08-16 15:41:52 +02004578 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01004579 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01004580 struct htx *h2s_htx = NULL;
4581 struct htx *buf_htx = NULL;
4582 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02004583 size_t ret = 0;
4584
4585 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01004586 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
4587 /* in HTX mode we ignore the count argument */
4588 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01004589 if (htx_is_empty(h2s_htx)) {
4590 if (cs->flags & CS_FL_REOS)
4591 cs->flags |= CS_FL_EOS;
Willy Tarreau86724e22018-12-01 23:19:43 +01004592 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01004593 }
Willy Tarreau86724e22018-12-01 23:19:43 +01004594
4595 buf_htx = htx_from_buf(buf);
4596 count = htx_free_space(buf_htx);
4597
Willy Tarreau0c22fa72018-12-04 15:21:35 +01004598 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau86724e22018-12-01 23:19:43 +01004599
4600 buf_htx->extra = h2s_htx->extra;
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004601 htx_to_buf(buf_htx, buf);
4602 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01004603 ret = htx_ret.ret;
4604 }
4605 else {
4606 ret = b_xfer(buf, &h2s->rxbuf, count);
4607 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004608
Olivier Houchard638b7992018-08-16 15:41:52 +02004609 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01004610 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02004611 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01004612 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02004613 if (cs->flags & CS_FL_REOS)
4614 cs->flags |= CS_FL_EOS;
Olivier Houchard638b7992018-08-16 15:41:52 +02004615 if (b_size(&h2s->rxbuf)) {
4616 b_free(&h2s->rxbuf);
4617 offer_buffers(NULL, tasks_run_queue);
4618 }
Olivier Houchard511efea2018-08-16 15:30:32 +02004619 }
4620
Willy Tarreau082f5592018-11-25 08:03:32 +01004621 if (ret && h2c->dsi == h2s->id) {
4622 /* demux is blocking on this stream's buffer */
4623 h2c->flags &= ~H2_CF_DEM_SFULL;
4624 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV)) {
4625 if (h2_recv_allowed(h2c))
4626 tasklet_wakeup(h2c->wait_event.task);
4627 }
4628 }
Willy Tarreau86724e22018-12-01 23:19:43 +01004629end:
Olivier Houchard511efea2018-08-16 15:30:32 +02004630 return ret;
4631}
4632
Olivier Houchardd846c262018-10-19 17:24:29 +02004633static void h2_stop_senders(struct h2c *h2c)
4634{
4635 struct h2s *h2s, *h2s_back;
4636
4637 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, list) {
4638 /* Don't unschedule the stream if the mux is just busy waiting for more data fro mthat stream */
4639 if (h2c->msi == h2s_id(h2s))
4640 continue;
4641 LIST_DEL(&h2s->list);
4642 LIST_INIT(&h2s->list);
4643 task_remove_from_task_list((struct task *)h2s->send_wait->task);
4644 h2s->send_wait->wait_reason |= SUB_CAN_SEND;
4645 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
4646 LIST_ADD(&h2c->send_list, &h2s->list);
4647 }
4648}
4649
Willy Tarreau62f52692017-10-08 23:01:42 +02004650/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02004651static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02004652{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004653 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01004654 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004655 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004656 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004657 struct htx *htx;
4658 struct htx_blk *blk;
4659 enum htx_blk_type btype;
4660 uint32_t bsize;
4661 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004662
Olivier Houchardd846c262018-10-19 17:24:29 +02004663 if (h2s->send_wait) {
4664 h2s->send_wait->wait_reason &= ~SUB_CALL_UNSUBSCRIBE;
4665 h2s->send_wait = NULL;
4666 LIST_DEL(&h2s->list);
4667 LIST_INIT(&h2s->list);
4668 }
Willy Tarreau6bf641a2018-10-08 09:43:03 +02004669 if (h2s->h2c->st0 < H2_CS_FRAME_H)
4670 return 0;
4671
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004672 /* htx will be enough to decide if we're using HTX or legacy */
4673 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
4674
Willy Tarreau0bad0432018-06-14 16:54:01 +02004675 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01004676 h2s->flags |= H2_SF_OUTGOING_DATA;
4677
Willy Tarreau751f2d02018-10-05 09:35:00 +02004678 if (h2s->id == 0) {
4679 int32_t id = h2c_get_next_sid(h2s->h2c);
4680
4681 if (id < 0) {
4682 cs->ctx = NULL;
4683 cs->flags |= CS_FL_ERROR;
4684 h2s_destroy(h2s);
4685 return 0;
4686 }
4687
4688 eb32_delete(&h2s->by_id);
4689 h2s->by_id.key = h2s->id = id;
4690 h2s->h2c->max_id = id;
4691 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
4692 }
4693
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004694 if (htx) {
Willy Tarreauc14999b2018-12-06 14:09:09 +01004695 while (h2s->st < H2_SS_ERROR && !(h2s->flags & H2_SF_BLK_ANY) &&
4696 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004697 idx = htx_get_head(htx);
4698 blk = htx_get_blk(htx, idx);
4699 btype = htx_get_blk_type(blk);
4700 bsize = htx_get_blksz(blk);
4701
4702 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02004703 case HTX_BLK_REQ_SL:
4704 /* start-line before headers */
4705 ret = h2s_htx_bck_make_req_headers(h2s, htx);
4706 if (ret > 0) {
4707 total += ret;
4708 count -= ret;
4709 if (ret < bsize)
4710 goto done;
4711 }
4712 break;
4713
Willy Tarreau115e83b2018-12-01 19:17:53 +01004714 case HTX_BLK_RES_SL:
4715 /* start-line before headers */
4716 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
4717 if (ret > 0) {
4718 total += ret;
4719 count -= ret;
4720 if (ret < bsize)
4721 goto done;
4722 }
4723 break;
4724
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004725 case HTX_BLK_DATA:
4726 case HTX_BLK_EOD:
4727 case HTX_BLK_EOM:
4728 /* all these cause the emission of a DATA frame (possibly empty) */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004729 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004730 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01004731 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004732 total += ret;
4733 count -= ret;
4734 if (ret < bsize)
4735 goto done;
4736 }
4737 break;
4738
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004739 default:
4740 htx_remove_blk(htx, blk);
4741 total += bsize;
4742 count -= bsize;
4743 break;
4744 }
4745 }
4746 goto done;
4747 }
4748
4749 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02004750 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02004751 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02004752 if (h2s->h2c->flags & H2_CF_IS_BACK)
4753 ret = -1;
4754 else
4755 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004756 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004757 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02004758 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004759 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02004760 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004761 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004762 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004763
Willy Tarreau5dd17352018-06-14 13:33:30 +02004764 if (unlikely((int)ret <= 0)) {
4765 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004766 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4767 break;
4768 }
Willy Tarreau35a62702018-02-27 15:37:25 +01004769 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02004770 total += count;
4771 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004772 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004773 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004774 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004775 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004776 cs->flags |= CS_FL_ERROR;
4777 break;
4778 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02004779
4780 total += ret;
4781 count -= ret;
4782
4783 if (h2s->st >= H2_SS_ERROR)
4784 break;
4785
4786 if (h2s->flags & H2_SF_BLK_ANY)
4787 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004788 }
4789
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004790 done:
Willy Tarreau00610962018-07-19 10:58:28 +02004791 if (h2s->st >= H2_SS_ERROR) {
4792 /* trim any possibly pending data after we close (extra CR-LF,
4793 * unprocessed trailers, abnormal extra data, ...)
4794 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02004795 total += count;
4796 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02004797 }
4798
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004799 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01004800 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004801 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01004802 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01004803 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01004804 }
4805
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004806 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004807 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004808 } else {
4809 b_del(buf, total);
4810 }
Olivier Houchardd846c262018-10-19 17:24:29 +02004811
4812 /* The mux is full, cancel the pending tasks */
4813 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
4814 (h2s->flags & H2_SF_BLK_MBUSY))
4815 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01004816
Olivier Houchard8122a8d2018-12-03 19:13:29 +01004817 /* If we're running HTX, and we read the whole buffer, then pretend
4818 * we read exactly what the caller specified, as with HTX the caller
4819 * will always give the buffer size, instead of the amount of data
4820 * available.
4821 */
4822 if (htx && !b_data(buf))
4823 total = orig_count;
4824
Olivier Houchard7505f942018-08-21 18:10:44 +02004825 if (total > 0) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004826 if (!(h2s->h2c->wait_event.wait_reason & SUB_CAN_SEND))
4827 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02004828
Olivier Houchard7505f942018-08-21 18:10:44 +02004829 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004830 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02004831}
4832
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004833/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02004834static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004835{
4836 struct h2c *h2c = conn->mux_ctx;
4837 struct h2s *h2s;
4838 struct eb32_node *node;
4839 int fctl_cnt = 0;
4840 int send_cnt = 0;
4841 int tree_cnt = 0;
4842 int orph_cnt = 0;
4843
4844 if (!h2c)
4845 return;
4846
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004847 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004848 fctl_cnt++;
4849
Olivier Houchardfa8aa862018-10-10 18:25:41 +02004850 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004851 send_cnt++;
4852
4853 node = eb32_first(&h2c->streams_by_id);
4854 while (node) {
4855 h2s = container_of(node, struct h2s, by_id);
4856 tree_cnt++;
4857 if (!h2s->cs)
4858 orph_cnt++;
4859 node = eb32_next(node);
4860 }
4861
Willy Tarreau616ac812018-07-24 14:12:42 +02004862 chunk_appendf(msg, " st0=%d err=%d maxid=%d lastid=%d flg=0x%08x nbst=%u nbcs=%u"
4863 " fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
4864 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
4865 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
4866 (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
4867 (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004868}
Willy Tarreau62f52692017-10-08 23:01:42 +02004869
4870/*******************************************************/
4871/* functions below are dedicated to the config parsers */
4872/*******************************************************/
4873
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02004874/* config parser for global "tune.h2.header-table-size" */
4875static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
4876 struct proxy *defpx, const char *file, int line,
4877 char **err)
4878{
4879 if (too_many_args(1, args, err, NULL))
4880 return -1;
4881
4882 h2_settings_header_table_size = atoi(args[1]);
4883 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
4884 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
4885 return -1;
4886 }
4887 return 0;
4888}
Willy Tarreau62f52692017-10-08 23:01:42 +02004889
Willy Tarreaue6baec02017-07-27 11:45:11 +02004890/* config parser for global "tune.h2.initial-window-size" */
4891static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
4892 struct proxy *defpx, const char *file, int line,
4893 char **err)
4894{
4895 if (too_many_args(1, args, err, NULL))
4896 return -1;
4897
4898 h2_settings_initial_window_size = atoi(args[1]);
4899 if (h2_settings_initial_window_size < 0) {
4900 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
4901 return -1;
4902 }
4903 return 0;
4904}
4905
Willy Tarreau5242ef82017-07-27 11:47:28 +02004906/* config parser for global "tune.h2.max-concurrent-streams" */
4907static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
4908 struct proxy *defpx, const char *file, int line,
4909 char **err)
4910{
4911 if (too_many_args(1, args, err, NULL))
4912 return -1;
4913
4914 h2_settings_max_concurrent_streams = atoi(args[1]);
4915 if (h2_settings_max_concurrent_streams < 0) {
4916 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
4917 return -1;
4918 }
4919 return 0;
4920}
4921
Willy Tarreau62f52692017-10-08 23:01:42 +02004922
4923/****************************************/
4924/* MUX initialization and instanciation */
4925/***************************************/
4926
4927/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01004928static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02004929 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02004930 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02004931 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02004932 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02004933 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02004934 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02004935 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01004936 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02004937 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01004938 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01004939 .avail_streams = h2_avail_streams,
Olivier Houchard8defe4b2018-12-02 01:31:17 +01004940 .max_streams = h2_max_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02004941 .shutr = h2_shutr,
4942 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02004943 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01004944 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02004945 .name = "H2",
4946};
4947
Christopher Faulet32f61c02018-04-10 14:33:41 +02004948/* PROTO selection : this mux registers PROTO token "h2" */
4949static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02004950 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02004951
Willy Tarreau0108d902018-11-25 19:14:37 +01004952INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
4953
Willy Tarreauf8957272018-10-03 10:25:20 +02004954static struct mux_proto_list mux_proto_h2_htx =
4955 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_ops };
4956
4957INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
4958
Willy Tarreau62f52692017-10-08 23:01:42 +02004959/* config keyword parsers */
4960static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02004961 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02004962 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02004963 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02004964 { 0, NULL, NULL }
4965}};
4966
Willy Tarreau0108d902018-11-25 19:14:37 +01004967INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);