blob: 62eb909b391d81aa1800b8b525548151bc233a9d [file] [log] [blame]
Willy Tarreau62f52692017-10-08 23:01:42 +02001/*
2 * HTTP/2 mux-demux for connections
3 *
4 * Copyright 2017 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <common/cfgparse.h>
14#include <common/config.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020015#include <common/h2.h>
Willy Tarreau13278b42017-10-13 19:23:14 +020016#include <common/hpack-dec.h>
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020017#include <common/hpack-enc.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020018#include <common/hpack-tbl.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020019#include <common/net_helper.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020020#include <proto/connection.h>
Willy Tarreau3ccf4b22017-10-13 19:07:26 +020021#include <proto/h1.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020022#include <proto/stream.h>
Willy Tarreauea392822017-10-31 10:02:25 +010023#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020024#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020025
26
Willy Tarreau2a856182017-05-16 15:20:39 +020027/* dummy streams returned for idle and closed states */
28static const struct h2s *h2_closed_stream;
29static const struct h2s *h2_idle_stream;
30
Willy Tarreau5ab6b572017-09-22 08:05:00 +020031/* the h2c connection pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +010032static struct pool_head *pool_head_h2c;
Willy Tarreau18312642017-10-11 07:57:07 +020033/* the h2s stream pool */
Willy Tarreaubafbe012017-11-24 17:34:44 +010034static struct pool_head *pool_head_h2s;
Willy Tarreau5ab6b572017-09-22 08:05:00 +020035
36/* 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 Tarreau081d4722017-05-16 21:51:05 +020063
64
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 Tarreauea392822017-10-31 10:02:25 +0100118 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200119 struct eb_root streams_by_id; /* all active streams by their ID */
120 struct list send_list; /* list of blocked streams requesting to send */
121 struct list fctl_list; /* list of streams blocked by connection's fctl */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100122 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200123 struct wait_list wait_list; /* We're in a wait list, to send */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200124};
125
Willy Tarreau18312642017-10-11 07:57:07 +0200126/* H2 stream state, in h2s->st */
127enum h2_ss {
128 H2_SS_IDLE = 0, // idle
129 H2_SS_RLOC, // reserved(local)
130 H2_SS_RREM, // reserved(remote)
131 H2_SS_OPEN, // open
132 H2_SS_HREM, // half-closed(remote)
133 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200134 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200135 H2_SS_CLOSED, // closed
136 H2_SS_ENTRIES // must be last
137} __attribute__((packed));
138
139/* HTTP/2 stream flags (32 bit), in h2s->flags */
140#define H2_SF_NONE 0x00000000
141#define H2_SF_ES_RCVD 0x00000001
142#define H2_SF_ES_SENT 0x00000002
143
144#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
145#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
146
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200147/* stream flags indicating the reason the stream is blocked */
148#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
149#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
150#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
151#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
152#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
153
Willy Tarreau454f9052017-10-26 19:40:35 +0200154/* stream flags indicating how data is supposed to be sent */
155#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
156#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
157
158/* step we're currently in when sending chunks. This is needed because we may
159 * have to transfer chunks as large as a full buffer so there's no room left
160 * for size nor crlf around.
161 */
162#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
163#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
164#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
165
166#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
167
Willy Tarreau67434202017-11-06 20:20:51 +0100168#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100169#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100170
Willy Tarreau18312642017-10-11 07:57:07 +0200171/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
172 * it is being processed in the internal HTTP representation (H1 for now).
173 */
174struct h2s {
175 struct conn_stream *cs;
176 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200177 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200178 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200179 int32_t id; /* stream ID */
180 uint32_t flags; /* H2_SF_* */
181 int mws; /* mux window size for this stream */
182 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
183 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200184 uint16_t status; /* HTTP response status */
Olivier Houchard638b7992018-08-16 15:41:52 +0200185 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200186 struct wait_list wait_list; /* Wait list, when we're attempting to send a RST but we can't send */
187 struct wait_list *recv_wait_list; /* Address of the wait_list the conn_stream associated is waiting on */
Willy Tarreau18312642017-10-11 07:57:07 +0200188};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200189
Willy Tarreauc6405142017-09-21 20:23:50 +0200190/* descriptor for an h2 frame header */
191struct h2_fh {
192 uint32_t len; /* length, host order, 24 bits */
193 uint32_t sid; /* stream id, host order, 31 bits */
194 uint8_t ft; /* frame type */
195 uint8_t ff; /* frame flags */
196};
197
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200198/* a few settings from the global section */
199static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200200static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200201static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200202
Willy Tarreau2a856182017-05-16 15:20:39 +0200203/* a dmumy closed stream */
204static const struct h2s *h2_closed_stream = &(const struct h2s){
205 .cs = NULL,
206 .h2c = NULL,
207 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100208 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100209 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200210 .id = 0,
211};
212
213/* and a dummy idle stream for use with any unannounced stream */
214static const struct h2s *h2_idle_stream = &(const struct h2s){
215 .cs = NULL,
216 .h2c = NULL,
217 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100218 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200219 .id = 0,
220};
221
Olivier Houchard9f6af332018-05-25 14:04:04 +0200222static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200223static int h2_send(struct h2c *h2c);
224static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200225static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200226static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100227static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100228static int h2_frt_decode_headers(struct h2s *h2s);
229static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200230static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200231
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200232/*****************************************************/
233/* functions below are for dynamic buffer management */
234/*****************************************************/
235
Willy Tarreau315d8072017-12-10 22:17:57 +0100236/* indicates whether or not the we may call the h2_recv() function to attempt
237 * to receive data into the buffer and/or demux pending data. The condition is
238 * a bit complex due to some API limits for now. The rules are the following :
239 * - if an error or a shutdown was detected on the connection and the buffer
240 * is empty, we must not attempt to receive
241 * - if the demux buf failed to be allocated, we must not try to receive and
242 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100243 * - if no flag indicates a blocking condition, we may attempt to receive,
244 * regardless of whether the demux buffer is full or not, so that only
245 * de demux part decides whether or not to block. This is needed because
246 * the connection API indeed prevents us from re-enabling receipt that is
247 * already enabled in a polled state, so we must always immediately stop
248 * as soon as the demux can't proceed so as never to hit an end of read
249 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100250 * - otherwise must may not attempt
251 */
252static inline int h2_recv_allowed(const struct h2c *h2c)
253{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200254 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100255 (h2c->st0 >= H2_CS_ERROR ||
256 h2c->conn->flags & CO_FL_ERROR ||
257 conn_xprt_read0_pending(h2c->conn)))
258 return 0;
259
260 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100261 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100262 return 1;
263
264 return 0;
265}
266
Willy Tarreauf2101912018-07-19 10:11:38 +0200267/* returns true if the connection has too many conn_streams attached */
268static inline int h2_has_too_many_cs(const struct h2c *h2c)
269{
270 return h2c->nb_cs >= h2_settings_max_concurrent_streams;
271}
272
Willy Tarreau44e973f2018-03-01 17:49:30 +0100273/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
274 * flags are used to figure what buffer was requested. It returns 1 if the
275 * allocation succeeds, in which case the connection is woken up, or 0 if it's
276 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200277 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100278static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200279{
280 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100281 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200282
Willy Tarreau44e973f2018-03-01 17:49:30 +0100283 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200284 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200285 if (h2_recv_allowed(h2c)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200286 conn_xprt_want_recv(h2c->conn);
Olivier Houchard61d322f2018-09-24 18:02:03 +0200287 tasklet_wakeup(h2c->wait_list.task);
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200288 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200289 return 1;
290 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200291
Willy Tarreau44e973f2018-03-01 17:49:30 +0100292 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
293 h2c->flags &= ~H2_CF_MUX_MALLOC;
294 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
295 conn_xprt_want_send(h2c->conn);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200296
297 if (h2c->flags & H2_CF_DEM_MROOM) {
298 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200299 if (h2_recv_allowed(h2c)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200300 conn_xprt_want_recv(h2c->conn);
Olivier Houchard61d322f2018-09-24 18:02:03 +0200301 tasklet_wakeup(h2c->wait_list.task);
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200302 }
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200303 }
Willy Tarreau14398122017-09-22 14:26:04 +0200304 return 1;
305 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100306
307 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
308 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200309 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100310 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200311 if (h2_recv_allowed(h2c)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100312 conn_xprt_want_recv(h2c->conn);
Olivier Houchard61d322f2018-09-24 18:02:03 +0200313 tasklet_wakeup(h2c->wait_list.task);
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200314 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100315 return 1;
316 }
317
Willy Tarreau14398122017-09-22 14:26:04 +0200318 return 0;
319}
320
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200321static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200322{
323 struct buffer *buf = NULL;
324
Willy Tarreau44e973f2018-03-01 17:49:30 +0100325 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
326 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
327 h2c->buf_wait.target = h2c;
328 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100329 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100330 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100331 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200332 __conn_xprt_stop_recv(h2c->conn);
333 }
334 return buf;
335}
336
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200337static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200338{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200339 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100340 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200341 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200342 }
343}
344
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200345
Willy Tarreau62f52692017-10-08 23:01:42 +0200346/*****************************************************************/
347/* functions below are dedicated to the mux setup and management */
348/*****************************************************************/
349
Willy Tarreau32218eb2017-09-22 08:07:25 +0200350/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
351static int h2c_frt_init(struct connection *conn)
352{
353 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100354 struct task *t = NULL;
355 struct session *sess = conn->owner;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200356
Willy Tarreaubafbe012017-11-24 17:34:44 +0100357 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200358 if (!h2c)
359 goto fail;
360
Willy Tarreau3f133572017-10-31 19:21:06 +0100361
Willy Tarreau599391a2017-11-24 10:16:00 +0100362 h2c->shut_timeout = h2c->timeout = sess->fe->timeout.client;
363 if (tick_isset(sess->fe->timeout.clientfin))
364 h2c->shut_timeout = sess->fe->timeout.clientfin;
365
Willy Tarreau33400292017-11-05 11:23:40 +0100366 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100367 if (tick_isset(h2c->timeout)) {
368 t = task_new(tid_bit);
369 if (!t)
370 goto fail;
371
372 h2c->task = t;
373 t->process = h2_timeout_task;
374 t->context = h2c;
375 t->expire = tick_add(now_ms, h2c->timeout);
376 }
Willy Tarreauea392822017-10-31 10:02:25 +0100377
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200378 h2c->wait_list.task = tasklet_new();
379 if (!h2c->wait_list.task)
380 goto fail;
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200381 h2c->wait_list.task->process = h2_io_cb;
382 h2c->wait_list.task->context = h2c;
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +0200383 h2c->wait_list.wait_reason = 0;
Willy Tarreau0f383582018-10-03 14:22:21 +0200384 LIST_INIT(&h2c->wait_list.list);
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200385
Willy Tarreau32218eb2017-09-22 08:07:25 +0200386 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
387 if (!h2c->ddht)
388 goto fail;
389
390 /* Initialise the context. */
391 h2c->st0 = H2_CS_PREFACE;
392 h2c->conn = conn;
393 h2c->max_id = -1;
394 h2c->errcode = H2_ERR_NO_ERROR;
395 h2c->flags = H2_CF_NONE;
396 h2c->rcvd_c = 0;
397 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100398 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200399 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200400
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200401 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200402 h2c->dsi = -1;
403 h2c->msi = -1;
404 h2c->last_sid = -1;
405
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200406 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200407 h2c->miw = 65535; /* mux initial window size */
408 h2c->mws = 65535; /* mux window size */
409 h2c->mfs = 16384; /* initial max frame size */
410 h2c->streams_by_id = EB_ROOT_UNIQUE;
411 LIST_INIT(&h2c->send_list);
412 LIST_INIT(&h2c->fctl_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100413 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200414 conn->mux_ctx = h2c;
415
Willy Tarreau3f133572017-10-31 19:21:06 +0100416 if (t)
417 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100418
Willy Tarreau0f383582018-10-03 14:22:21 +0200419 /* prepare to read something */
420 conn_xprt_want_recv(conn);
421 tasklet_wakeup(h2c->wait_list.task);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200422 return 0;
423 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100424 if (t)
425 task_free(t);
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200426 if (h2c->wait_list.task)
427 tasklet_free(h2c->wait_list.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100428 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200429 return -1;
430}
431
Willy Tarreau62f52692017-10-08 23:01:42 +0200432/* Initialize the mux once it's attached. For outgoing connections, the context
433 * is already initialized before installing the mux, so we detect incoming
434 * connections from the fact that the context is still NULL. Returns < 0 on
435 * error.
436 */
Willy Tarreau175a2bb2018-09-12 12:02:05 +0200437static int h2_init(struct connection *conn, struct proxy *prx)
Willy Tarreau62f52692017-10-08 23:01:42 +0200438{
439 if (conn->mux_ctx) {
440 /* we don't support outgoing connections for now */
441 return -1;
442 }
443
Willy Tarreau32218eb2017-09-22 08:07:25 +0200444 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200445}
446
Willy Tarreau2373acc2017-10-12 17:35:14 +0200447/* returns the stream associated with id <id> or NULL if not found */
448static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
449{
450 struct eb32_node *node;
451
Willy Tarreau2a856182017-05-16 15:20:39 +0200452 if (id > h2c->max_id)
453 return (struct h2s *)h2_idle_stream;
454
Willy Tarreau2373acc2017-10-12 17:35:14 +0200455 node = eb32_lookup(&h2c->streams_by_id, id);
456 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200457 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200458
459 return container_of(node, struct h2s, by_id);
460}
461
Willy Tarreau62f52692017-10-08 23:01:42 +0200462/* release function for a connection. This one should be called to free all
463 * resources allocated to the mux.
464 */
465static void h2_release(struct connection *conn)
466{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200467 struct h2c *h2c = conn->mux_ctx;
468
469 LIST_DEL(&conn->list);
470
471 if (h2c) {
472 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200473
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100474 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100475 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100476 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200477
Willy Tarreau44e973f2018-03-01 17:49:30 +0100478 h2_release_buf(h2c, &h2c->dbuf);
479 h2_release_buf(h2c, &h2c->mbuf);
480
Willy Tarreauea392822017-10-31 10:02:25 +0100481 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200482 h2c->task->context = NULL;
483 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100484 h2c->task = NULL;
485 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200486 if (h2c->wait_list.task)
487 tasklet_free(h2c->wait_list.task);
Olivier Houchardc2aa7112018-09-11 18:27:21 +0200488 LIST_DEL(&h2c->wait_list.list);
489 LIST_INIT(&h2c->wait_list.list);
Olivier Houchard251f6a22018-09-12 17:52:46 +0200490 while (!LIST_ISEMPTY(&h2c->send_list)) {
491 struct wait_list *sw = LIST_ELEM(h2c->send_list.n,
492 struct wait_list *, list);
493 LIST_DEL(&sw->list);
494 LIST_INIT(&sw->list);
495 }
496 while (!LIST_ISEMPTY(&h2c->fctl_list)) {
497 struct wait_list *sw = LIST_ELEM(h2c->fctl_list.n,
498 struct wait_list *, list);
499 LIST_DEL(&sw->list);
500 LIST_INIT(&sw->list);
501 }
502
Willy Tarreauea392822017-10-31 10:02:25 +0100503
Willy Tarreaubafbe012017-11-24 17:34:44 +0100504 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200505 }
506
507 conn->mux = NULL;
508 conn->mux_ctx = NULL;
509
510 conn_stop_tracking(conn);
511 conn_full_close(conn);
512 if (conn->destroy_cb)
513 conn->destroy_cb(conn);
514 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200515}
516
517
Willy Tarreau71681172017-10-23 14:39:06 +0200518/******************************************************/
519/* functions below are for the H2 protocol processing */
520/******************************************************/
521
522/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100523static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200524{
525 return h2s ? h2s->id : 0;
526}
527
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200528/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100529static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200530{
531 if (h2c->msi < 0)
532 return 0;
533
534 if (h2c->msi == h2s_id(h2s))
535 return 0;
536
537 return 1;
538}
539
Willy Tarreau741d6df2017-10-17 08:00:59 +0200540/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100541static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200542{
543 h2c->errcode = err;
544 h2c->st0 = H2_CS_ERROR;
545}
546
Willy Tarreau2e43f082017-10-17 08:03:59 +0200547/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100548static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200549{
550 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
551 h2s->errcode = err;
552 h2s->st = H2_SS_ERROR;
553 if (h2s->cs)
554 h2s->cs->flags |= CS_FL_ERROR;
555 }
556}
557
Willy Tarreaue4820742017-07-27 13:37:23 +0200558/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100559static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200560{
561 uint8_t *out = frame;
562
563 *out = len >> 16;
564 write_n16(out + 1, len);
565}
566
Willy Tarreau54c15062017-10-10 17:10:03 +0200567/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
568 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
569 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200570 * available in the buffer's input prior to calling this function. The buffer
571 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200572 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100573static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200574 const struct buffer *b, int o)
575{
Willy Tarreau591d4452018-06-15 17:21:00 +0200576 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 +0200577}
578
Willy Tarreau1f094672017-11-20 21:27:45 +0100579static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200580{
Willy Tarreau591d4452018-06-15 17:21:00 +0200581 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200582}
583
Willy Tarreau1f094672017-11-20 21:27:45 +0100584static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200585{
Willy Tarreau591d4452018-06-15 17:21:00 +0200586 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200587}
588
Willy Tarreau1f094672017-11-20 21:27:45 +0100589static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200590{
Willy Tarreau591d4452018-06-15 17:21:00 +0200591 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200592}
593
594
Willy Tarreau715d5312017-07-11 15:20:24 +0200595/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
596 * is not obvious. It turns out that H2 headers are neither aligned nor do they
597 * use regular sizes. And to add to the trouble, the buffer may wrap so each
598 * byte read must be checked. The header is formed like this :
599 *
600 * b0 b1 b2 b3 b4 b5..b8
601 * +----------+---------+--------+----+----+----------------------+
602 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
603 * +----------+---------+--------+----+----+----------------------+
604 *
605 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
606 * we get the sid properly aligned and ordered, and 16 bits of len properly
607 * ordered as well. The type and flags can be extracted using bit shifts from
608 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200609 * Returns zero if some bytes are missing, otherwise non-zero on success. The
610 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200611 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100612static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200613{
614 uint64_t w;
615
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200616 if (b_data(b) < 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200617 return 0;
618
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200619 w = h2_get_n64(b, 1);
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200620 h->len = *(uint8_t*)b_head(b) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200621 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
622 h->ff = w >> 32;
623 h->ft = w >> 40;
624 h->len += w >> 48;
625 return 1;
626}
627
628/* skip the next 9 bytes corresponding to the frame header possibly parsed by
629 * h2_peek_frame_hdr() above.
630 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100631static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200632{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200633 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200634}
635
636/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100637static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200638{
639 int ret;
640
641 ret = h2_peek_frame_hdr(b, h);
642 if (ret > 0)
643 h2_skip_frame_hdr(b);
644 return ret;
645}
646
Willy Tarreau00dd0782018-03-01 16:31:34 +0100647/* marks stream <h2s> as CLOSED and decrement the number of active streams for
648 * its connection if the stream was not yet closed. Please use this exclusively
649 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100650 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100651static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100652{
653 if (h2s->st != H2_SS_CLOSED)
654 h2s->h2c->nb_streams--;
655 h2s->st = H2_SS_CLOSED;
656}
657
Willy Tarreau71049cc2018-03-28 13:56:39 +0200658/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
659static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100660{
661 h2s_close(h2s);
662 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200663 if (b_size(&h2s->rxbuf)) {
664 b_free(&h2s->rxbuf);
665 offer_buffers(NULL, tasks_run_queue);
666 }
Olivier Houchardc2aa7112018-09-11 18:27:21 +0200667 LIST_DEL(&h2s->wait_list.list);
668 LIST_INIT(&h2s->wait_list.list);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200669 tasklet_free(h2s->wait_list.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100670 pool_free(pool_head_h2s, h2s);
671}
672
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200673/* creates a new stream <id> on the h2c connection and returns it, or NULL in
674 * case of memory allocation error.
675 */
676static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
677{
Willy Tarreau590a0512018-09-05 11:56:48 +0200678 struct session *sess = h2c->conn->owner;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200679 struct conn_stream *cs;
680 struct h2s *h2s;
681
Willy Tarreaubafbe012017-11-24 17:34:44 +0100682 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200683 if (!h2s)
684 goto out;
685
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200686 h2s->wait_list.task = tasklet_new();
687 if (!h2s->wait_list.task) {
688 pool_free(pool_head_h2s, h2s);
689 goto out;
690 }
691 LIST_INIT(&h2s->wait_list.list);
692 h2s->recv_wait_list = NULL;
693 h2s->wait_list.task->process = h2_deferred_shut;
694 h2s->wait_list.task->context = h2s;
695 h2s->wait_list.handle = NULL;
696 h2s->wait_list.wait_reason = 0;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200697 h2s->h2c = h2c;
698 h2s->mws = h2c->miw;
699 h2s->flags = H2_SF_NONE;
700 h2s->errcode = H2_ERR_NO_ERROR;
701 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200702 h2s->status = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200703 h2s->rxbuf = BUF_NULL;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200704 h1m_init_res(&h2s->h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +0200705 h2s->h1m.err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +0200706 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200707 h2s->by_id.key = h2s->id = id;
708 h2c->max_id = id;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200709
710 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100711 h2c->nb_streams++;
712 if (h2c->nb_streams > h2_settings_max_concurrent_streams)
713 goto out_close;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200714
715 cs = cs_new(h2c->conn);
716 if (!cs)
717 goto out_close;
718
719 h2s->cs = cs;
720 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200721 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200722
723 if (stream_create_from_cs(cs) < 0)
724 goto out_free_cs;
725
Willy Tarreau590a0512018-09-05 11:56:48 +0200726 /* We want the accept date presented to the next stream to be the one
727 * we have now, the handshake time to be null (since the next stream
728 * is not delayed by a handshake), and the idle time to count since
729 * right now.
730 */
731 sess->accept_date = date;
732 sess->tv_accept = now;
733 sess->t_handshake = 0;
734
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200735 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200736 if (h2_has_too_many_cs(h2c))
737 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200738 return h2s;
739
740 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200741 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200742 cs_free(cs);
743 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200744 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200745 h2s = NULL;
Willy Tarreau22de8d32018-09-05 19:55:58 +0200746 sess_log(sess);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200747 out:
748 return h2s;
749}
750
Willy Tarreaube5b7152017-09-25 16:25:39 +0200751/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
752 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
753 * the various settings codes.
754 */
755static int h2c_snd_settings(struct h2c *h2c)
756{
757 struct buffer *res;
758 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200759 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200760 int ret;
761
762 if (h2c_mux_busy(h2c, NULL)) {
763 h2c->flags |= H2_CF_DEM_MBUSY;
764 return 0;
765 }
766
Willy Tarreau44e973f2018-03-01 17:49:30 +0100767 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200768 if (!res) {
769 h2c->flags |= H2_CF_MUX_MALLOC;
770 h2c->flags |= H2_CF_DEM_MROOM;
771 return 0;
772 }
773
774 chunk_init(&buf, buf_data, sizeof(buf_data));
775 chunk_memcpy(&buf,
776 "\x00\x00\x00" /* length : 0 for now */
777 "\x04\x00" /* type : 4 (settings), flags : 0 */
778 "\x00\x00\x00\x00", /* stream ID : 0 */
779 9);
780
781 if (h2_settings_header_table_size != 4096) {
782 char str[6] = "\x00\x01"; /* header_table_size */
783
784 write_n32(str + 2, h2_settings_header_table_size);
785 chunk_memcat(&buf, str, 6);
786 }
787
788 if (h2_settings_initial_window_size != 65535) {
789 char str[6] = "\x00\x04"; /* initial_window_size */
790
791 write_n32(str + 2, h2_settings_initial_window_size);
792 chunk_memcat(&buf, str, 6);
793 }
794
795 if (h2_settings_max_concurrent_streams != 0) {
796 char str[6] = "\x00\x03"; /* max_concurrent_streams */
797
798 /* Note: 0 means "unlimited" for haproxy's config but not for
799 * the protocol, so never send this value!
800 */
801 write_n32(str + 2, h2_settings_max_concurrent_streams);
802 chunk_memcat(&buf, str, 6);
803 }
804
805 if (global.tune.bufsize != 16384) {
806 char str[6] = "\x00\x05"; /* max_frame_size */
807
808 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
809 * match bufsize - rewrite size, but at the moment it seems
810 * that clients don't take care of it.
811 */
812 write_n32(str + 2, global.tune.bufsize);
813 chunk_memcat(&buf, str, 6);
814 }
815
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200816 h2_set_frame_size(buf.area, buf.data - 9);
817 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200818 if (unlikely(ret <= 0)) {
819 if (!ret) {
820 h2c->flags |= H2_CF_MUX_MFULL;
821 h2c->flags |= H2_CF_DEM_MROOM;
822 return 0;
823 }
824 else {
825 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
826 return 0;
827 }
828 }
829 return ret;
830}
831
Willy Tarreau52eed752017-09-22 15:05:09 +0200832/* Try to receive a connection preface, then upon success try to send our
833 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
834 * missing data. It may return an error in h2c.
835 */
836static int h2c_frt_recv_preface(struct h2c *h2c)
837{
838 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200839 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200840
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200841 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200842
843 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +0200844 if (ret1 < 0)
845 sess_log(h2c->conn->owner);
846
Willy Tarreau52eed752017-09-22 15:05:09 +0200847 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
848 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
849 return 0;
850 }
851
Willy Tarreaube5b7152017-09-25 16:25:39 +0200852 ret2 = h2c_snd_settings(h2c);
853 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200854 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200855
Willy Tarreaube5b7152017-09-25 16:25:39 +0200856 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200857}
858
Willy Tarreau081d4722017-05-16 21:51:05 +0200859/* try to send a GOAWAY frame on the connection to report an error or a graceful
860 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
861 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
862 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
863 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
864 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
865 * on unrecoverable failure. It will not attempt to send one again in this last
866 * case so that it is safe to use h2c_error() to report such errors.
867 */
868static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
869{
870 struct buffer *res;
871 char str[17];
872 int ret;
873
874 if (h2c->flags & H2_CF_GOAWAY_FAILED)
875 return 1; // claim that it worked
876
877 if (h2c_mux_busy(h2c, h2s)) {
878 if (h2s)
879 h2s->flags |= H2_SF_BLK_MBUSY;
880 else
881 h2c->flags |= H2_CF_DEM_MBUSY;
882 return 0;
883 }
884
Willy Tarreau44e973f2018-03-01 17:49:30 +0100885 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +0200886 if (!res) {
887 h2c->flags |= H2_CF_MUX_MALLOC;
888 if (h2s)
889 h2s->flags |= H2_SF_BLK_MROOM;
890 else
891 h2c->flags |= H2_CF_DEM_MROOM;
892 return 0;
893 }
894
895 /* len: 8, type: 7, flags: none, sid: 0 */
896 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
897
898 if (h2c->last_sid < 0)
899 h2c->last_sid = h2c->max_id;
900
901 write_n32(str + 9, h2c->last_sid);
902 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200903 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +0200904 if (unlikely(ret <= 0)) {
905 if (!ret) {
906 h2c->flags |= H2_CF_MUX_MFULL;
907 if (h2s)
908 h2s->flags |= H2_SF_BLK_MROOM;
909 else
910 h2c->flags |= H2_CF_DEM_MROOM;
911 return 0;
912 }
913 else {
914 /* we cannot report this error using GOAWAY, so we mark
915 * it and claim a success.
916 */
917 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
918 h2c->flags |= H2_CF_GOAWAY_FAILED;
919 return 1;
920 }
921 }
922 h2c->flags |= H2_CF_GOAWAY_SENT;
923 return ret;
924}
925
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100926/* Try to send an RST_STREAM frame on the connection for the indicated stream
927 * during mux operations. This stream must be valid and cannot be closed
928 * already. h2s->id will be used for the stream ID and h2s->errcode will be
929 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
930 * not yet.
931 *
932 * Returns > 0 on success or zero if nothing was done. In case of lack of room
933 * to write the message, it subscribes the stream to future notifications.
934 */
935static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
936{
937 struct buffer *res;
938 char str[13];
939 int ret;
940
941 if (!h2s || h2s->st == H2_SS_CLOSED)
942 return 1;
943
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100944 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
945 * RST_STREAM in response to a RST_STREAM frame.
946 */
947 if (h2c->dft == H2_FT_RST_STREAM) {
948 ret = 1;
949 goto ignore;
950 }
951
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100952 if (h2c_mux_busy(h2c, h2s)) {
953 h2s->flags |= H2_SF_BLK_MBUSY;
954 return 0;
955 }
956
Willy Tarreau44e973f2018-03-01 17:49:30 +0100957 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100958 if (!res) {
959 h2c->flags |= H2_CF_MUX_MALLOC;
960 h2s->flags |= H2_SF_BLK_MROOM;
961 return 0;
962 }
963
964 /* len: 4, type: 3, flags: none */
965 memcpy(str, "\x00\x00\x04\x03\x00", 5);
966 write_n32(str + 5, h2s->id);
967 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200968 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100969
970 if (unlikely(ret <= 0)) {
971 if (!ret) {
972 h2c->flags |= H2_CF_MUX_MFULL;
973 h2s->flags |= H2_SF_BLK_MROOM;
974 return 0;
975 }
976 else {
977 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
978 return 0;
979 }
980 }
981
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100982 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100983 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100984 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100985 return ret;
986}
987
988/* Try to send an RST_STREAM frame on the connection for the stream being
989 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
990 * error code unless the stream's state already is IDLE or CLOSED in which
991 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
992 * it was not yet.
993 *
994 * Returns > 0 on success or zero if nothing was done. In case of lack of room
995 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200996 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100997 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200998 */
999static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1000{
1001 struct buffer *res;
1002 char str[13];
1003 int ret;
1004
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001005 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1006 * RST_STREAM in response to a RST_STREAM frame.
1007 */
1008 if (h2c->dft == H2_FT_RST_STREAM) {
1009 ret = 1;
1010 goto ignore;
1011 }
1012
Willy Tarreau27a84c92017-10-17 08:10:17 +02001013 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001014 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001015 return 0;
1016 }
1017
Willy Tarreau44e973f2018-03-01 17:49:30 +01001018 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001019 if (!res) {
1020 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001021 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001022 return 0;
1023 }
1024
1025 /* len: 4, type: 3, flags: none */
1026 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001027
Willy Tarreau27a84c92017-10-17 08:10:17 +02001028 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +01001029 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +02001030 h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001031 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001032
Willy Tarreau27a84c92017-10-17 08:10:17 +02001033 if (unlikely(ret <= 0)) {
1034 if (!ret) {
1035 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001036 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001037 return 0;
1038 }
1039 else {
1040 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1041 return 0;
1042 }
1043 }
1044
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001045 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001046 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001047 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001048 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001049 }
1050
Willy Tarreau27a84c92017-10-17 08:10:17 +02001051 return ret;
1052}
1053
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001054/* try to send an empty DATA frame with the ES flag set to notify about the
1055 * end of stream and match a shutdown(write). If an ES was already sent as
1056 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1057 * on success or zero if nothing was done. In case of lack of room to write the
1058 * message, it subscribes the requesting stream to future notifications.
1059 */
1060static int h2_send_empty_data_es(struct h2s *h2s)
1061{
1062 struct h2c *h2c = h2s->h2c;
1063 struct buffer *res;
1064 char str[9];
1065 int ret;
1066
Willy Tarreau721c9742017-11-07 11:05:42 +01001067 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001068 return 1;
1069
1070 if (h2c_mux_busy(h2c, h2s)) {
1071 h2s->flags |= H2_SF_BLK_MBUSY;
1072 return 0;
1073 }
1074
Willy Tarreau44e973f2018-03-01 17:49:30 +01001075 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001076 if (!res) {
1077 h2c->flags |= H2_CF_MUX_MALLOC;
1078 h2s->flags |= H2_SF_BLK_MROOM;
1079 return 0;
1080 }
1081
1082 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1083 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1084 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001085 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001086 if (likely(ret > 0)) {
1087 h2s->flags |= H2_SF_ES_SENT;
1088 }
1089 else if (!ret) {
1090 h2c->flags |= H2_CF_MUX_MFULL;
1091 h2s->flags |= H2_SF_BLK_MROOM;
1092 return 0;
1093 }
1094 else {
1095 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1096 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001097 }
1098 return ret;
1099}
1100
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001101/* wake the streams attached to the connection, whose id is greater than <last>,
1102 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001103 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1104 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001105 */
1106static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1107{
1108 struct eb32_node *node;
1109 struct h2s *h2s;
1110
1111 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1112 flags |= CS_FL_ERROR;
1113
1114 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001115 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001116
1117 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1118 while (node) {
1119 h2s = container_of(node, struct h2s, by_id);
1120 if (h2s->id <= last)
1121 break;
1122 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001123
1124 if (!h2s->cs) {
1125 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001126 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001127 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001128 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001129
1130 h2s->cs->flags |= flags;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001131 if (h2s->recv_wait_list) {
1132 struct wait_list *sw = h2s->recv_wait_list;
1133 sw->wait_reason &= ~SUB_CAN_RECV;
1134 tasklet_wakeup(sw->task);
1135 h2s->recv_wait_list = NULL;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02001136 } else if (h2s->cs->data_cb->wake != NULL)
1137 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001138
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001139 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1140 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001141 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001142 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001143 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001144 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001145 }
1146}
1147
Willy Tarreau3421aba2017-07-27 15:41:03 +02001148/* Increase all streams' outgoing window size by the difference passed in
1149 * argument. This is needed upon receipt of the settings frame if the initial
1150 * window size is different. The difference may be negative and the resulting
1151 * window size as well, for the time it takes to receive some window updates.
1152 */
1153static void h2c_update_all_ws(struct h2c *h2c, int diff)
1154{
1155 struct h2s *h2s;
1156 struct eb32_node *node;
1157
1158 if (!diff)
1159 return;
1160
1161 node = eb32_first(&h2c->streams_by_id);
1162 while (node) {
1163 h2s = container_of(node, struct h2s, by_id);
1164 h2s->mws += diff;
1165 node = eb32_next(node);
1166 }
1167}
1168
1169/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1170 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1171 * return an error in h2c. Described in RFC7540#6.5.
1172 */
1173static int h2c_handle_settings(struct h2c *h2c)
1174{
1175 unsigned int offset;
1176 int error;
1177
1178 if (h2c->dff & H2_F_SETTINGS_ACK) {
1179 if (h2c->dfl) {
1180 error = H2_ERR_FRAME_SIZE_ERROR;
1181 goto fail;
1182 }
1183 return 1;
1184 }
1185
1186 if (h2c->dsi != 0) {
1187 error = H2_ERR_PROTOCOL_ERROR;
1188 goto fail;
1189 }
1190
1191 if (h2c->dfl % 6) {
1192 error = H2_ERR_FRAME_SIZE_ERROR;
1193 goto fail;
1194 }
1195
1196 /* that's the limit we can process */
1197 if (h2c->dfl > global.tune.bufsize) {
1198 error = H2_ERR_FRAME_SIZE_ERROR;
1199 goto fail;
1200 }
1201
1202 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001203 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001204 return 0;
1205
1206 /* parse the frame */
1207 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001208 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1209 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001210
1211 switch (type) {
1212 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1213 /* we need to update all existing streams with the
1214 * difference from the previous iws.
1215 */
1216 if (arg < 0) { // RFC7540#6.5.2
1217 error = H2_ERR_FLOW_CONTROL_ERROR;
1218 goto fail;
1219 }
1220 h2c_update_all_ws(h2c, arg - h2c->miw);
1221 h2c->miw = arg;
1222 break;
1223 case H2_SETTINGS_MAX_FRAME_SIZE:
1224 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1225 error = H2_ERR_PROTOCOL_ERROR;
1226 goto fail;
1227 }
1228 h2c->mfs = arg;
1229 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001230 case H2_SETTINGS_ENABLE_PUSH:
1231 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1232 error = H2_ERR_PROTOCOL_ERROR;
1233 goto fail;
1234 }
1235 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001236 }
1237 }
1238
1239 /* need to ACK this frame now */
1240 h2c->st0 = H2_CS_FRAME_A;
1241 return 1;
1242 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001243 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001244 h2c_error(h2c, error);
1245 return 0;
1246}
1247
1248/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1249 * success or one of the h2_status values.
1250 */
1251static int h2c_ack_settings(struct h2c *h2c)
1252{
1253 struct buffer *res;
1254 char str[9];
1255 int ret = -1;
1256
1257 if (h2c_mux_busy(h2c, NULL)) {
1258 h2c->flags |= H2_CF_DEM_MBUSY;
1259 return 0;
1260 }
1261
Willy Tarreau44e973f2018-03-01 17:49:30 +01001262 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001263 if (!res) {
1264 h2c->flags |= H2_CF_MUX_MALLOC;
1265 h2c->flags |= H2_CF_DEM_MROOM;
1266 return 0;
1267 }
1268
1269 memcpy(str,
1270 "\x00\x00\x00" /* length : 0 (no data) */
1271 "\x04" "\x01" /* type : 4, flags : ACK */
1272 "\x00\x00\x00\x00" /* stream ID */, 9);
1273
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001274 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001275 if (unlikely(ret <= 0)) {
1276 if (!ret) {
1277 h2c->flags |= H2_CF_MUX_MFULL;
1278 h2c->flags |= H2_CF_DEM_MROOM;
1279 return 0;
1280 }
1281 else {
1282 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1283 return 0;
1284 }
1285 }
1286 return ret;
1287}
1288
Willy Tarreaucf68c782017-10-10 17:11:41 +02001289/* processes a PING frame and schedules an ACK if needed. The caller must pass
1290 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1291 * missing data. It may return an error in h2c.
1292 */
1293static int h2c_handle_ping(struct h2c *h2c)
1294{
1295 /* frame length must be exactly 8 */
1296 if (h2c->dfl != 8) {
1297 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1298 return 0;
1299 }
1300
1301 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001302 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001303 h2c->st0 = H2_CS_FRAME_A;
1304 return 1;
1305}
1306
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001307/* Try to send a window update for stream id <sid> and value <increment>.
1308 * Returns > 0 on success or zero on missing room or failure. It may return an
1309 * error in h2c.
1310 */
1311static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1312{
1313 struct buffer *res;
1314 char str[13];
1315 int ret = -1;
1316
1317 if (h2c_mux_busy(h2c, NULL)) {
1318 h2c->flags |= H2_CF_DEM_MBUSY;
1319 return 0;
1320 }
1321
Willy Tarreau44e973f2018-03-01 17:49:30 +01001322 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001323 if (!res) {
1324 h2c->flags |= H2_CF_MUX_MALLOC;
1325 h2c->flags |= H2_CF_DEM_MROOM;
1326 return 0;
1327 }
1328
1329 /* length: 4, type: 8, flags: none */
1330 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1331 write_n32(str + 5, sid);
1332 write_n32(str + 9, increment);
1333
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001334 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001335
1336 if (unlikely(ret <= 0)) {
1337 if (!ret) {
1338 h2c->flags |= H2_CF_MUX_MFULL;
1339 h2c->flags |= H2_CF_DEM_MROOM;
1340 return 0;
1341 }
1342 else {
1343 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1344 return 0;
1345 }
1346 }
1347 return ret;
1348}
1349
1350/* try to send pending window update for the connection. It's safe to call it
1351 * with no pending updates. Returns > 0 on success or zero on missing room or
1352 * failure. It may return an error in h2c.
1353 */
1354static int h2c_send_conn_wu(struct h2c *h2c)
1355{
1356 int ret = 1;
1357
1358 if (h2c->rcvd_c <= 0)
1359 return 1;
1360
1361 /* send WU for the connection */
1362 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1363 if (ret > 0)
1364 h2c->rcvd_c = 0;
1365
1366 return ret;
1367}
1368
1369/* try to send pending window update for the current dmux stream. It's safe to
1370 * call it with no pending updates. Returns > 0 on success or zero on missing
1371 * room or failure. It may return an error in h2c.
1372 */
1373static int h2c_send_strm_wu(struct h2c *h2c)
1374{
1375 int ret = 1;
1376
1377 if (h2c->rcvd_s <= 0)
1378 return 1;
1379
1380 /* send WU for the stream */
1381 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1382 if (ret > 0)
1383 h2c->rcvd_s = 0;
1384
1385 return ret;
1386}
1387
Willy Tarreaucf68c782017-10-10 17:11:41 +02001388/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1389 * success, 0 on missing data or one of the h2_status values.
1390 */
1391static int h2c_ack_ping(struct h2c *h2c)
1392{
1393 struct buffer *res;
1394 char str[17];
1395 int ret = -1;
1396
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001397 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001398 return 0;
1399
1400 if (h2c_mux_busy(h2c, NULL)) {
1401 h2c->flags |= H2_CF_DEM_MBUSY;
1402 return 0;
1403 }
1404
Willy Tarreau44e973f2018-03-01 17:49:30 +01001405 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001406 if (!res) {
1407 h2c->flags |= H2_CF_MUX_MALLOC;
1408 h2c->flags |= H2_CF_DEM_MROOM;
1409 return 0;
1410 }
1411
1412 memcpy(str,
1413 "\x00\x00\x08" /* length : 8 (same payload) */
1414 "\x06" "\x01" /* type : 6, flags : ACK */
1415 "\x00\x00\x00\x00" /* stream ID */, 9);
1416
1417 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001418 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001419
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001420 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001421 if (unlikely(ret <= 0)) {
1422 if (!ret) {
1423 h2c->flags |= H2_CF_MUX_MFULL;
1424 h2c->flags |= H2_CF_DEM_MROOM;
1425 return 0;
1426 }
1427 else {
1428 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1429 return 0;
1430 }
1431 }
1432 return ret;
1433}
1434
Willy Tarreau26f95952017-07-27 17:18:30 +02001435/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1436 * Returns > 0 on success or zero on missing data. It may return an error in
1437 * h2c or h2s. Described in RFC7540#6.9.
1438 */
1439static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1440{
1441 int32_t inc;
1442 int error;
1443
1444 if (h2c->dfl != 4) {
1445 error = H2_ERR_FRAME_SIZE_ERROR;
1446 goto conn_err;
1447 }
1448
1449 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001450 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001451 return 0;
1452
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001453 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001454
1455 if (h2c->dsi != 0) {
1456 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001457
1458 /* it's not an error to receive WU on a closed stream */
1459 if (h2s->st == H2_SS_CLOSED)
1460 return 1;
1461
1462 if (!inc) {
1463 error = H2_ERR_PROTOCOL_ERROR;
1464 goto strm_err;
1465 }
1466
1467 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1468 error = H2_ERR_FLOW_CONTROL_ERROR;
1469 goto strm_err;
1470 }
1471
1472 h2s->mws += inc;
1473 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1474 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02001475 /* The task will be waken up later */
Willy Tarreau26f95952017-07-27 17:18:30 +02001476 }
1477 }
1478 else {
1479 /* connection window update */
1480 if (!inc) {
1481 error = H2_ERR_PROTOCOL_ERROR;
1482 goto conn_err;
1483 }
1484
1485 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1486 error = H2_ERR_FLOW_CONTROL_ERROR;
1487 goto conn_err;
1488 }
1489
1490 h2c->mws += inc;
1491 }
1492
1493 return 1;
1494
1495 conn_err:
1496 h2c_error(h2c, error);
1497 return 0;
1498
1499 strm_err:
1500 if (h2s) {
1501 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001502 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001503 }
1504 else
1505 h2c_error(h2c, error);
1506 return 0;
1507}
1508
Willy Tarreaue96b0922017-10-30 00:28:29 +01001509/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1510 * the last ID. Returns > 0 on success or zero on missing data. It may return
1511 * an error in h2c. Described in RFC7540#6.8.
1512 */
1513static int h2c_handle_goaway(struct h2c *h2c)
1514{
1515 int error;
1516 int last;
1517
1518 if (h2c->dsi != 0) {
1519 error = H2_ERR_PROTOCOL_ERROR;
1520 goto conn_err;
1521 }
1522
1523 if (h2c->dfl < 8) {
1524 error = H2_ERR_FRAME_SIZE_ERROR;
1525 goto conn_err;
1526 }
1527
1528 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001529 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001530 return 0;
1531
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001532 last = h2_get_n32(&h2c->dbuf, 0);
1533 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001534 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001535 if (h2c->last_sid < 0)
1536 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001537 return 1;
1538
1539 conn_err:
1540 h2c_error(h2c, error);
1541 return 0;
1542}
1543
Willy Tarreau92153fc2017-12-03 19:46:19 +01001544/* processes a PRIORITY frame, and either skips it or rejects if it is
1545 * invalid. Returns > 0 on success or zero on missing data. It may return
1546 * an error in h2c. Described in RFC7540#6.3.
1547 */
1548static int h2c_handle_priority(struct h2c *h2c)
1549{
1550 int error;
1551
1552 if (h2c->dsi == 0) {
1553 error = H2_ERR_PROTOCOL_ERROR;
1554 goto conn_err;
1555 }
1556
1557 if (h2c->dfl != 5) {
1558 error = H2_ERR_FRAME_SIZE_ERROR;
1559 goto conn_err;
1560 }
1561
1562 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001563 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001564 return 0;
1565
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001566 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001567 /* 7540#5.3 : can't depend on itself */
1568 error = H2_ERR_PROTOCOL_ERROR;
1569 goto conn_err;
1570 }
1571 return 1;
1572
1573 conn_err:
1574 h2c_error(h2c, error);
1575 return 0;
1576}
1577
Willy Tarreaucd234e92017-08-18 10:59:39 +02001578/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1579 * Returns > 0 on success or zero on missing data. It may return an error in
1580 * h2c. Described in RFC7540#6.4.
1581 */
1582static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1583{
1584 int error;
1585
1586 if (h2c->dsi == 0) {
1587 error = H2_ERR_PROTOCOL_ERROR;
1588 goto conn_err;
1589 }
1590
Willy Tarreaucd234e92017-08-18 10:59:39 +02001591 if (h2c->dfl != 4) {
1592 error = H2_ERR_FRAME_SIZE_ERROR;
1593 goto conn_err;
1594 }
1595
1596 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001597 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001598 return 0;
1599
1600 /* late RST, already handled */
1601 if (h2s->st == H2_SS_CLOSED)
1602 return 1;
1603
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001604 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001605 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001606
1607 if (h2s->cs) {
Willy Tarreau2c096c32018-09-12 09:45:54 +02001608 h2s->cs->flags |= CS_FL_REOS | CS_FL_ERROR;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001609 if (h2s->recv_wait_list) {
1610 struct wait_list *sw = h2s->recv_wait_list;
1611
1612 sw->wait_reason &= ~SUB_CAN_RECV;
1613 tasklet_wakeup(sw->task);
1614 h2s->recv_wait_list = NULL;
1615 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02001616 }
1617
1618 h2s->flags |= H2_SF_RST_RCVD;
1619 return 1;
1620
1621 conn_err:
1622 h2c_error(h2c, error);
1623 return 0;
1624}
1625
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001626/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1627 * It may return an error in h2c or h2s. The caller must consider that the
1628 * return value is the new h2s in case one was allocated (most common case).
1629 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001630 * errors here are reported as connection errors since it's impossible to
1631 * recover from such errors after the compression context has been altered.
1632 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001633static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001634{
1635 int error;
1636
1637 if (!h2c->dfl) {
1638 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001639 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001640 goto strm_err;
1641 }
1642
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001643 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001644 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001645
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001646 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001647 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001648
Willy Tarreauf2101912018-07-19 10:11:38 +02001649 if (h2c->flags & H2_CF_DEM_TOOMANY)
1650 return 0; // too many cs still present
1651
Willy Tarreau13278b42017-10-13 19:23:14 +02001652 /* now either the frame is complete or the buffer is complete */
1653 if (h2s->st != H2_SS_IDLE) {
1654 /* FIXME: stream already exists, this is only allowed for
1655 * trailers (not supported for now).
1656 */
1657 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001658 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001659 goto conn_err;
1660 }
1661 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1662 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1663 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001664 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001665 goto conn_err;
1666 }
1667
Willy Tarreau22de8d32018-09-05 19:55:58 +02001668 /* Note: we don't emit any other logs below because ff we return
1669 * positively from h2c_stream_new(), the stream will report the error,
1670 * and if we return in error, h2c_stream_new() will emit the error.
1671 */
Willy Tarreau13278b42017-10-13 19:23:14 +02001672 h2s = h2c_stream_new(h2c, h2c->dsi);
1673 if (!h2s) {
1674 error = H2_ERR_INTERNAL_ERROR;
1675 goto conn_err;
1676 }
1677
1678 h2s->st = H2_SS_OPEN;
1679 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1680 h2s->st = H2_SS_HREM;
1681 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001682 /* note: cs cannot be null for now (just created above) */
1683 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001684 }
1685
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001686 if (!h2_frt_decode_headers(h2s))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001687 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001688
Willy Tarreau8f650c32017-11-21 19:36:21 +01001689 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001690 return NULL;
Willy Tarreau8f650c32017-11-21 19:36:21 +01001691
Willy Tarreau721c9742017-11-07 11:05:42 +01001692 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001693 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001694 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001695 }
1696 else {
1697 /* update the max stream ID if the request is being processed */
1698 if (h2s->id > h2c->max_id)
1699 h2c->max_id = h2s->id;
1700 }
1701
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001702 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001703
1704 conn_err:
1705 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001706 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001707
1708 strm_err:
1709 if (h2s) {
1710 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001711 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001712 }
1713 else
1714 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001715 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001716}
1717
Willy Tarreau454f9052017-10-26 19:40:35 +02001718/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1719 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1720 */
1721static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1722{
1723 int error;
1724
1725 /* note that empty DATA frames are perfectly valid and sometimes used
1726 * to signal an end of stream (with the ES flag).
1727 */
1728
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001729 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001730 return 0; // empty buffer
1731
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001732 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001733 return 0; // incomplete frame
1734
1735 /* now either the frame is complete or the buffer is complete */
1736
1737 if (!h2c->dsi) {
1738 /* RFC7540#6.1 */
1739 error = H2_ERR_PROTOCOL_ERROR;
1740 goto conn_err;
1741 }
1742
1743 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1744 /* RFC7540#6.1 */
1745 error = H2_ERR_STREAM_CLOSED;
1746 goto strm_err;
1747 }
1748
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001749 if (!h2_frt_transfer_data(h2s))
1750 return 0;
1751
Willy Tarreau454f9052017-10-26 19:40:35 +02001752 /* call the upper layers to process the frame, then let the upper layer
1753 * notify the stream about any change.
1754 */
1755 if (!h2s->cs) {
1756 error = H2_ERR_STREAM_CLOSED;
1757 goto strm_err;
1758 }
1759
Willy Tarreau8f650c32017-11-21 19:36:21 +01001760 if (h2c->st0 >= H2_CS_ERROR)
1761 return 0;
1762
Willy Tarreau721c9742017-11-07 11:05:42 +01001763 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001764 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001765 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001766 }
1767
1768 /* check for completion : the callee will change this to FRAME_A or
1769 * FRAME_H once done.
1770 */
1771 if (h2c->st0 == H2_CS_FRAME_P)
1772 return 0;
1773
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001774
1775 /* last frame */
1776 if (h2c->dff & H2_F_DATA_END_STREAM) {
1777 h2s->st = H2_SS_HREM;
1778 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001779 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001780 }
1781
Willy Tarreau454f9052017-10-26 19:40:35 +02001782 return 1;
1783
1784 conn_err:
1785 h2c_error(h2c, error);
1786 return 0;
1787
1788 strm_err:
1789 if (h2s) {
1790 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001791 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001792 }
1793 else
1794 h2c_error(h2c, error);
1795 return 0;
1796}
1797
Willy Tarreaubc933932017-10-09 16:21:43 +02001798/* process Rx frames to be demultiplexed */
1799static void h2_process_demux(struct h2c *h2c)
1800{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001801 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001802
Willy Tarreau081d4722017-05-16 21:51:05 +02001803 if (h2c->st0 >= H2_CS_ERROR)
1804 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001805
1806 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1807 if (h2c->st0 == H2_CS_PREFACE) {
1808 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1809 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001810 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02001811 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001812 sess_log(h2c->conn->owner);
1813 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001814 goto fail;
1815 }
1816
1817 h2c->max_id = 0;
1818 h2c->st0 = H2_CS_SETTINGS1;
1819 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001820
1821 if (h2c->st0 == H2_CS_SETTINGS1) {
1822 struct h2_fh hdr;
1823
1824 /* ensure that what is pending is a valid SETTINGS frame
1825 * without an ACK.
1826 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001827 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001828 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001829 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001830 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001831 sess_log(h2c->conn->owner);
1832 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001833 goto fail;
1834 }
1835
1836 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1837 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1838 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1839 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001840 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001841 goto fail;
1842 }
1843
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001844 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001845 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1846 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1847 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001848 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001849 goto fail;
1850 }
1851
1852 /* that's OK, switch to FRAME_P to process it */
1853 h2c->dfl = hdr.len;
1854 h2c->dsi = hdr.sid;
1855 h2c->dft = hdr.ft;
1856 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001857 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001858 h2c->st0 = H2_CS_FRAME_P;
1859 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001860 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001861
1862 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001863 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001864 int ret = 0;
1865
1866 if (h2c->st0 >= H2_CS_ERROR)
1867 break;
1868
1869 if (h2c->st0 == H2_CS_FRAME_H) {
1870 struct h2_fh hdr;
1871
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001872 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02001873 break;
1874
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001875 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001876 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1877 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001878 if (!h2c->nb_streams) {
1879 /* only log if no other stream can report the error */
1880 sess_log(h2c->conn->owner);
1881 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001882 break;
1883 }
1884
1885 h2c->dfl = hdr.len;
1886 h2c->dsi = hdr.sid;
1887 h2c->dft = hdr.ft;
1888 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001889 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001890 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001891 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001892 }
1893
1894 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001895 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
1896
Olivier Houchard638b7992018-08-16 15:41:52 +02001897 if (tmp_h2s != h2s && h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001898 /* we may have to signal the upper layers */
1899 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001900 if (h2s->recv_wait_list) {
1901 h2s->recv_wait_list->wait_reason &= ~SUB_CAN_RECV;
1902 tasklet_wakeup(h2s->recv_wait_list->task);
1903 h2s->recv_wait_list = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001904 }
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001905 if (h2c->st0 >= H2_CS_ERROR)
1906 goto strm_err;
1907
1908 if (h2s->st >= H2_SS_ERROR) {
1909 /* stream error : send RST_STREAM */
1910 h2c->st0 = H2_CS_FRAME_E;
1911 }
1912 }
1913 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001914
Willy Tarreaud7901432017-12-29 11:34:40 +01001915 if (h2c->st0 == H2_CS_FRAME_E)
1916 goto strm_err;
1917
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001918 if (h2s->st == H2_SS_IDLE &&
1919 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1920 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1921 * this state MUST be treated as a connection error
1922 */
1923 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1924 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001925 if (!h2c->nb_streams) {
1926 /* only log if no other stream can report the error */
1927 sess_log(h2c->conn->owner);
1928 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001929 break;
1930 }
1931
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001932 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1933 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1934 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1935 * this state MUST be treated as a stream error
1936 */
1937 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1938 goto strm_err;
1939 }
1940
Willy Tarreauab837502017-12-27 15:07:30 +01001941 /* Below the management of frames received in closed state is a
1942 * bit hackish because the spec makes strong differences between
1943 * streams closed by receiving RST, sending RST, and seeing ES
1944 * in both directions. In addition to this, the creation of a
1945 * new stream reusing the identifier of a closed one will be
1946 * detected here. Given that we cannot keep track of all closed
1947 * streams forever, we consider that unknown closed streams were
1948 * closed on RST received, which allows us to respond with an
1949 * RST without breaking the connection (eg: to abort a transfer).
1950 * Some frames have to be silently ignored as well.
1951 */
1952 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1953 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1954 /* #5.1.1: The identifier of a newly
1955 * established stream MUST be numerically
1956 * greater than all streams that the initiating
1957 * endpoint has opened or reserved. This
1958 * governs streams that are opened using a
1959 * HEADERS frame and streams that are reserved
1960 * using PUSH_PROMISE. An endpoint that
1961 * receives an unexpected stream identifier
1962 * MUST respond with a connection error.
1963 */
1964 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1965 goto strm_err;
1966 }
1967
1968 if (h2s->flags & H2_SF_RST_RCVD) {
1969 /* RFC7540#5.1:closed: an endpoint that
1970 * receives any frame other than PRIORITY after
1971 * receiving a RST_STREAM MUST treat that as a
1972 * stream error of type STREAM_CLOSED.
1973 *
1974 * Note that old streams fall into this category
1975 * and will lead to an RST being sent.
1976 */
1977 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1978 h2c->st0 = H2_CS_FRAME_E;
1979 goto strm_err;
1980 }
1981
1982 /* RFC7540#5.1:closed: if this state is reached as a
1983 * result of sending a RST_STREAM frame, the peer that
1984 * receives the RST_STREAM might have already sent
1985 * frames on the stream that cannot be withdrawn. An
1986 * endpoint MUST ignore frames that it receives on
1987 * closed streams after it has sent a RST_STREAM
1988 * frame. An endpoint MAY choose to limit the period
1989 * over which it ignores frames and treat frames that
1990 * arrive after this time as being in error.
1991 */
1992 if (!(h2s->flags & H2_SF_RST_SENT)) {
1993 /* RFC7540#5.1:closed: any frame other than
1994 * PRIO/WU/RST in this state MUST be treated as
1995 * a connection error
1996 */
1997 if (h2c->dft != H2_FT_RST_STREAM &&
1998 h2c->dft != H2_FT_PRIORITY &&
1999 h2c->dft != H2_FT_WINDOW_UPDATE) {
2000 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2001 goto strm_err;
2002 }
2003 }
2004 }
2005
Willy Tarreauc0da1962017-10-30 18:38:00 +01002006#if 0
2007 // problem below: it is not possible to completely ignore such
2008 // streams as we need to maintain the compression state as well
2009 // and for this we need to completely process these frames (eg:
2010 // HEADERS frames) as well as counting DATA frames to emit
2011 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2012 // This is a typical case of layer violation where the
2013 // transported contents are critical to the connection's
2014 // validity and must be ignored at the same time :-(
2015
2016 /* graceful shutdown, ignore streams whose ID is higher than
2017 * the one advertised in GOAWAY. RFC7540#6.8.
2018 */
2019 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002020 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2021 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002022 h2c->dfl -= ret;
2023 ret = h2c->dfl == 0;
2024 goto strm_err;
2025 }
2026#endif
2027
Willy Tarreau7e98c052017-10-10 15:56:59 +02002028 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002029 case H2_FT_SETTINGS:
2030 if (h2c->st0 == H2_CS_FRAME_P)
2031 ret = h2c_handle_settings(h2c);
2032
2033 if (h2c->st0 == H2_CS_FRAME_A)
2034 ret = h2c_ack_settings(h2c);
2035 break;
2036
Willy Tarreaucf68c782017-10-10 17:11:41 +02002037 case H2_FT_PING:
2038 if (h2c->st0 == H2_CS_FRAME_P)
2039 ret = h2c_handle_ping(h2c);
2040
2041 if (h2c->st0 == H2_CS_FRAME_A)
2042 ret = h2c_ack_ping(h2c);
2043 break;
2044
Willy Tarreau26f95952017-07-27 17:18:30 +02002045 case H2_FT_WINDOW_UPDATE:
2046 if (h2c->st0 == H2_CS_FRAME_P)
2047 ret = h2c_handle_window_update(h2c, h2s);
2048 break;
2049
Willy Tarreau61290ec2017-10-17 08:19:21 +02002050 case H2_FT_CONTINUATION:
2051 /* we currently don't support CONTINUATION frames since
2052 * we have nowhere to store the partial HEADERS frame.
2053 * Let's abort the stream on an INTERNAL_ERROR here.
2054 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002055 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02002056 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002057 h2c->st0 = H2_CS_FRAME_E;
2058 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02002059 break;
2060
Willy Tarreau13278b42017-10-13 19:23:14 +02002061 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002062 if (h2c->st0 == H2_CS_FRAME_P) {
2063 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
2064 if (tmp_h2s) {
2065 h2s = tmp_h2s;
2066 ret = 1;
2067 }
2068 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002069 break;
2070
Willy Tarreau454f9052017-10-26 19:40:35 +02002071 case H2_FT_DATA:
2072 if (h2c->st0 == H2_CS_FRAME_P)
2073 ret = h2c_frt_handle_data(h2c, h2s);
2074
2075 if (h2c->st0 == H2_CS_FRAME_A)
2076 ret = h2c_send_strm_wu(h2c);
2077 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002078
Willy Tarreau92153fc2017-12-03 19:46:19 +01002079 case H2_FT_PRIORITY:
2080 if (h2c->st0 == H2_CS_FRAME_P)
2081 ret = h2c_handle_priority(h2c);
2082 break;
2083
Willy Tarreaucd234e92017-08-18 10:59:39 +02002084 case H2_FT_RST_STREAM:
2085 if (h2c->st0 == H2_CS_FRAME_P)
2086 ret = h2c_handle_rst_stream(h2c, h2s);
2087 break;
2088
Willy Tarreaue96b0922017-10-30 00:28:29 +01002089 case H2_FT_GOAWAY:
2090 if (h2c->st0 == H2_CS_FRAME_P)
2091 ret = h2c_handle_goaway(h2c);
2092 break;
2093
Willy Tarreau1c661982017-10-30 13:52:01 +01002094 case H2_FT_PUSH_PROMISE:
2095 /* not permitted here, RFC7540#5.1 */
2096 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002097 if (!h2c->nb_streams) {
2098 /* only log if no other stream can report the error */
2099 sess_log(h2c->conn->owner);
2100 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002101 break;
2102
2103 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002104 default:
2105 /* drop frames that we ignore. They may be larger than
2106 * the buffer so we drain all of their contents until
2107 * we reach the end.
2108 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002109 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2110 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002111 h2c->dfl -= ret;
2112 ret = h2c->dfl == 0;
2113 }
2114
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002115 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002116 /* We may have to send an RST if not done yet */
2117 if (h2s->st == H2_SS_ERROR)
2118 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002119
Willy Tarreaua20a5192017-12-27 11:02:06 +01002120 if (h2c->st0 == H2_CS_FRAME_E)
2121 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002122
Willy Tarreau7e98c052017-10-10 15:56:59 +02002123 /* error or missing data condition met above ? */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002124 if (ret <= 0) {
2125 h2s = NULL;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002126 break;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002127 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002128
2129 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002130 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002131 h2c->st0 = H2_CS_FRAME_H;
2132 }
2133 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002134
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002135 if (h2c->rcvd_c > 0 &&
2136 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2137 h2c_send_conn_wu(h2c);
2138
Willy Tarreau52eed752017-09-22 15:05:09 +02002139 fail:
2140 /* we can go here on missing data, blocked response or error */
Olivier Houchard638b7992018-08-16 15:41:52 +02002141 if (h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002142 /* we may have to signal the upper layers */
2143 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002144 if (h2s->recv_wait_list) {
2145 h2s->recv_wait_list->wait_reason &= ~SUB_CAN_RECV;
2146 tasklet_wakeup(h2s->recv_wait_list->task);
2147 h2s->recv_wait_list = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002148 }
2149 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002150 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02002151}
2152
2153/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2154 * the end.
2155 */
2156static int h2_process_mux(struct h2c *h2c)
2157{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002158 struct h2s *h2s;
2159 struct wait_list *sw, *sw_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002160
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002161 /* start by sending possibly pending window updates */
2162 if (h2c->rcvd_c > 0 &&
2163 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2164 h2c_send_conn_wu(h2c) < 0)
2165 goto fail;
2166
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002167 /* First we always process the flow control list because the streams
2168 * waiting there were already elected for immediate emission but were
2169 * blocked just on this.
2170 */
2171
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002172 list_for_each_entry_safe(sw, sw_back, &h2c->fctl_list, list) {
2173 h2s = sw->handle;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002174 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2175 h2c->st0 >= H2_CS_ERROR)
2176 break;
2177
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002178 /* If the tasklet was added to finish shutr/shutw, just wake the task */
2179 if ((long)(h2s) & 3) {
2180 sw->wait_reason &= ~SUB_CAN_SEND;
2181 LIST_DEL(&sw->list);
2182 LIST_INIT(&sw->list);
2183 tasklet_wakeup(sw->task);
2184 } else if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
2185 h2s->flags &= ~H2_SF_BLK_ANY;
2186 LIST_DEL(&sw->list);
2187 LIST_INIT(&sw->list);
2188 sw->wait_reason &= ~SUB_CAN_SEND;
2189 tasklet_wakeup(sw->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002190 }
2191 }
2192
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002193 list_for_each_entry_safe(sw, sw_back, &h2c->send_list, list) {
2194 h2s = sw->handle;
2195
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002196 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2197 break;
2198
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002199 /* If the tasklet was added to finish shutr/shutw, just wake the task */
2200 if ((long)(h2s) & 3) {
2201 sw->wait_reason &= ~SUB_CAN_SEND;
2202 LIST_DEL(&sw->list);
2203 LIST_INIT(&sw->list);
2204 tasklet_wakeup(sw->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002205 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002206 else if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
2207 h2s->flags &= ~H2_SF_BLK_ANY;
2208
2209 LIST_DEL(&sw->list);
2210 LIST_INIT(&sw->list);
2211 sw->wait_reason &= ~SUB_CAN_SEND;
2212 tasklet_wakeup(sw->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002213 }
2214 }
2215
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002216 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002217 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002218 if (h2c->st0 == H2_CS_ERROR) {
2219 if (h2c->max_id >= 0) {
2220 h2c_send_goaway_error(h2c, NULL);
2221 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2222 return 0;
2223 }
2224
2225 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2226 }
2227 return 1;
2228 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002229 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002230}
2231
Willy Tarreau62f52692017-10-08 23:01:42 +02002232
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002233/* Attempt to read data, and subscribe if none available */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002234static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002235{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002236 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002237 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002238 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002239 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002240
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002241 if (h2c->wait_list.wait_reason & SUB_CAN_RECV)
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002242 return 0;
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002243
Willy Tarreau315d8072017-12-10 22:17:57 +01002244 if (!h2_recv_allowed(h2c))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002245 return 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002246
Willy Tarreau44e973f2018-03-01 17:49:30 +01002247 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002248 if (!buf) {
2249 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002250 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002251 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002252
Olivier Houchard7505f942018-08-21 18:10:44 +02002253 do {
2254 max = buf->size - b_data(buf);
2255 if (max)
2256 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2257 else
2258 ret = 0;
2259 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002260
Olivier Houchard7505f942018-08-21 18:10:44 +02002261 if (h2_recv_allowed(h2c)) {
2262 conn_xprt_want_recv(conn);
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002263 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h2c->wait_list);
Olivier Houchard7505f942018-08-21 18:10:44 +02002264 }
Olivier Houcharda1411e62018-08-17 18:42:48 +02002265 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002266 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002267 return 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002268 }
2269
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002270 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002271 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002272 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002273}
2274
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002275/* Try to send data if possible */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002276static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002277{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002278 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002279 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002280 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002281
2282 if (conn->flags & CO_FL_ERROR)
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002283 return 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002284
Olivier Houchard7505f942018-08-21 18:10:44 +02002285
Willy Tarreaua2af5122017-10-09 11:56:46 +02002286 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2287 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002288 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002289 }
2290
Willy Tarreaubc933932017-10-09 16:21:43 +02002291 /* This loop is quite simple : it tries to fill as much as it can from
2292 * pending streams into the existing buffer until it's reportedly full
2293 * or the end of send requests is reached. Then it tries to send this
2294 * buffer's contents out, marks it not full if at least one byte could
2295 * be sent, and tries again.
2296 *
2297 * The snd_buf() function normally takes a "flags" argument which may
2298 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2299 * data immediately comes and CO_SFL_STREAMER to indicate that the
2300 * connection is streaming lots of data (used to increase TLS record
2301 * size at the expense of latency). The former can be sent any time
2302 * there's a buffer full flag, as it indicates at least one stream
2303 * attempted to send and failed so there are pending data. An
2304 * alternative would be to set it as long as there's an active stream
2305 * but that would be problematic for ACKs until we have an absolute
2306 * guarantee that all waiters have at least one byte to send. The
2307 * latter should possibly not be set for now.
2308 */
2309
2310 done = 0;
2311 while (!done) {
2312 unsigned int flags = 0;
2313
2314 /* fill as much as we can into the current buffer */
2315 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2316 done = h2_process_mux(h2c);
2317
2318 if (conn->flags & CO_FL_ERROR)
2319 break;
2320
2321 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2322 flags |= CO_SFL_MSG_MORE;
2323
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002324 if (b_data(&h2c->mbuf)) {
2325 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002326 if (!ret)
2327 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002328 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002329 b_del(&h2c->mbuf, ret);
2330 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002331 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002332
2333 /* wrote at least one byte, the buffer is not full anymore */
2334 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2335 }
2336
Willy Tarreaua2af5122017-10-09 11:56:46 +02002337 if (conn->flags & CO_FL_SOCK_WR_SH) {
2338 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002339 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002340 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002341 /* We're not full anymore, so we can wake any task that are waiting
2342 * for us.
2343 */
2344 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002345 while (!LIST_ISEMPTY(&h2c->send_list)) {
2346 struct wait_list *sw = LIST_ELEM(h2c->send_list.n,
Olivier Houchard6ff20392018-07-17 18:46:31 +02002347 struct wait_list *, list);
2348 LIST_DEL(&sw->list);
2349 LIST_INIT(&sw->list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002350 sw->wait_reason &= ~SUB_CAN_SEND;
2351 tasklet_wakeup(sw->task);
2352 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002353 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002354 /* We're done, no more to send */
2355 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002356 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002357schedule:
2358 if (LIST_ISEMPTY(&h2c->wait_list.list))
2359 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_list);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002360 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002361}
2362
2363static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2364{
2365 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002366 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002367
2368 if (!(h2c->wait_list.wait_reason & SUB_CAN_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002369 ret = h2_send(h2c);
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002370 if (!(h2c->wait_list.wait_reason & SUB_CAN_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002371 ret |= h2_recv(h2c);
2372 if (ret)
2373 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002374 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002375}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002376
Willy Tarreau62f52692017-10-08 23:01:42 +02002377/* callback called on any event by the connection handler.
2378 * It applies changes and returns zero, or < 0 if it wants immediate
2379 * destruction of the connection (which normally doesn not happen in h2).
2380 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002381static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002382{
Olivier Houchard7505f942018-08-21 18:10:44 +02002383 struct connection *conn = h2c->conn;
Willy Tarreau8ec14062017-12-30 18:08:13 +01002384 struct session *sess = conn->owner;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002385
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002386 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002387 h2_process_demux(h2c);
2388
2389 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002390 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002391
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002392 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002393 h2c->flags &= ~H2_CF_DEM_DFULL;
2394 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002395 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002396
Willy Tarreau8ec14062017-12-30 18:08:13 +01002397 if (sess && unlikely(sess->fe->state == PR_STSTOPPED)) {
2398 /* frontend is stopping, reload likely in progress, let's try
2399 * to announce a graceful shutdown if not yet done. We don't
2400 * care if it fails, it will be tried again later.
2401 */
2402 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2403 if (h2c->last_sid < 0)
2404 h2c->last_sid = (1U << 31) - 1;
2405 h2c_send_goaway_error(h2c, NULL);
2406 }
2407 }
2408
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002409 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002410 * If we received early data, and the handshake is done, wake
2411 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002412 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002413 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2414 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2415 struct eb32_node *node;
2416 struct h2s *h2s;
2417
2418 h2c->flags |= H2_CF_WAIT_FOR_HS;
2419 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2420
2421 while (node) {
2422 h2s = container_of(node, struct h2s, by_id);
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002423 if ((h2s->cs->flags & CS_FL_WAIT_FOR_HS) &&
2424 h2s->recv_wait_list) {
2425 struct wait_list *sw = h2s->recv_wait_list;
2426 sw->wait_reason &= ~SUB_CAN_RECV;
2427 tasklet_wakeup(sw->task);
2428 h2s->recv_wait_list = NULL;
2429 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002430 node = eb32_next(node);
2431 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002432 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002433
Willy Tarreau26bd7612017-10-09 16:47:04 +02002434 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002435 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2436 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2437 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002438 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002439
2440 if (eb_is_empty(&h2c->streams_by_id)) {
2441 /* no more stream, kill the connection now */
2442 h2_release(conn);
2443 return -1;
2444 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002445 }
2446
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002447 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002448 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002449
2450 /* stop being notified of incoming data if we can't process them */
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002451 if (!h2_recv_allowed(h2c))
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002452 __conn_xprt_stop_recv(conn);
Olivier Houchard7505f942018-08-21 18:10:44 +02002453 else
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002454 __conn_xprt_want_recv(conn);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002455
2456 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002457 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002458 h2c->st0 != H2_CS_ERROR2 && !(h2c->flags & H2_CF_GOAWAY_FAILED) &&
Willy Tarreau51606832017-10-17 15:30:07 +02002459 (h2c->st0 == H2_CS_ERROR ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002460 b_data(&h2c->mbuf) ||
Willy Tarreau51606832017-10-17 15:30:07 +02002461 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2462 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002463 __conn_xprt_want_send(conn);
2464 }
2465 else {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002466 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002467 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002468 }
2469
Willy Tarreau3f133572017-10-31 19:21:06 +01002470 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002471 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002472 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002473 task_queue(h2c->task);
2474 }
2475 else
2476 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002477 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002478
Olivier Houchard7505f942018-08-21 18:10:44 +02002479 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002480 return 0;
2481}
2482
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002483static int h2_wake(struct connection *conn)
2484{
2485 struct h2c *h2c = conn->mux_ctx;
2486
2487 return (h2_process(h2c));
2488}
2489
2490
Willy Tarreauea392822017-10-31 10:02:25 +01002491/* Connection timeout management. The principle is that if there's no receipt
2492 * nor sending for a certain amount of time, the connection is closed. If the
2493 * MUX buffer still has lying data or is not allocatable, the connection is
2494 * immediately killed. If it's allocatable and empty, we attempt to send a
2495 * GOAWAY frame.
2496 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002497static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002498{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002499 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002500 int expired = tick_is_expired(t->expire, now_ms);
2501
Willy Tarreau0975f112018-03-29 15:22:59 +02002502 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002503 return t;
2504
Willy Tarreau0975f112018-03-29 15:22:59 +02002505 task_delete(t);
2506 task_free(t);
2507
2508 if (!h2c) {
2509 /* resources were already deleted */
2510 return NULL;
2511 }
2512
2513 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002514 h2c_error(h2c, H2_ERR_NO_ERROR);
2515 h2_wake_some_streams(h2c, 0, 0);
2516
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002517 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002518 /* don't even try to send a GOAWAY, the buffer is stuck */
2519 h2c->flags |= H2_CF_GOAWAY_FAILED;
2520 }
2521
2522 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002523 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002524 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2525 h2c->flags |= H2_CF_GOAWAY_FAILED;
2526
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002527 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2528 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002529 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002530 b_del(&h2c->mbuf, ret);
2531 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002532 }
2533 }
Willy Tarreauea392822017-10-31 10:02:25 +01002534
Willy Tarreau0975f112018-03-29 15:22:59 +02002535 /* either we can release everything now or it will be done later once
2536 * the last stream closes.
2537 */
2538 if (eb_is_empty(&h2c->streams_by_id))
2539 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002540
Willy Tarreauea392822017-10-31 10:02:25 +01002541 return NULL;
2542}
2543
2544
Willy Tarreau62f52692017-10-08 23:01:42 +02002545/*******************************************/
2546/* functions below are used by the streams */
2547/*******************************************/
2548
2549/*
2550 * Attach a new stream to a connection
2551 * (Used for outgoing connections)
2552 */
2553static struct conn_stream *h2_attach(struct connection *conn)
2554{
2555 return NULL;
2556}
2557
2558/* callback used to update the mux's polling flags after changing a cs' status.
2559 * The caller (cs_update_mux_polling) will take care of propagating any changes
2560 * to the transport layer.
2561 */
2562static void h2_update_poll(struct conn_stream *cs)
2563{
Willy Tarreau1d393222017-10-17 10:26:19 +02002564 struct h2s *h2s = cs->ctx;
2565
2566 if (!h2s)
2567 return;
2568
Willy Tarreaud7739c82017-10-30 15:38:23 +01002569 /* we may unblock a blocked read */
2570
Willy Tarreau315d8072017-12-10 22:17:57 +01002571 if (cs->flags & CS_FL_DATA_RD_ENA) {
2572 /* the stream indicates it's willing to read */
Willy Tarreaud7739c82017-10-30 15:38:23 +01002573 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreaud13bf272017-12-14 10:34:52 +01002574 if (h2s->h2c->dsi == h2s->id) {
Willy Tarreau315d8072017-12-10 22:17:57 +01002575 conn_xprt_want_recv(cs->conn);
Olivier Houchard61d322f2018-09-24 18:02:03 +02002576 tasklet_wakeup(h2s->h2c->wait_list.task);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002577 conn_xprt_want_send(cs->conn);
2578 }
Willy Tarreaud7739c82017-10-30 15:38:23 +01002579 }
2580
Willy Tarreau1d393222017-10-17 10:26:19 +02002581 /* Note: the stream and stream-int code doesn't allow us to perform a
2582 * synchronous send() here unfortunately, because this code is called
2583 * as si_update() from the process_stream() context. This means that
2584 * we have to queue the current cs and defer its processing after the
2585 * connection's cs list is processed anyway.
2586 */
2587
2588 if (cs->flags & CS_FL_DATA_WR_ENA) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002589 if (!b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2590 conn_xprt_want_send(cs->conn);
2591 tasklet_wakeup(h2s->h2c->wait_list.task);
Willy Tarreau1d393222017-10-17 10:26:19 +02002592 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002593 /* We don't support unsubscribing from here, it shouldn't be a problem */
Willy Tarreau1d393222017-10-17 10:26:19 +02002594
2595 /* this can happen from within si_chk_snd() */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002596 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreau1d393222017-10-17 10:26:19 +02002597 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002598}
2599
2600/*
2601 * Detach the stream from the connection and possibly release the connection.
2602 */
2603static void h2_detach(struct conn_stream *cs)
2604{
Willy Tarreau60935142017-10-16 18:11:19 +02002605 struct h2s *h2s = cs->ctx;
2606 struct h2c *h2c;
2607
2608 cs->ctx = NULL;
2609 if (!h2s)
2610 return;
2611
2612 h2c = h2s->h2c;
Olivier Houchard70d0d182018-09-12 17:56:08 +02002613 /* If the stream we're detaching waited for more data, unsubscribe it now */
2614 if (h2s->recv_wait_list && !((long)h2s->recv_wait_list->handle & 3))
2615 h2s->recv_wait_list = NULL;
Willy Tarreau60935142017-10-16 18:11:19 +02002616 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002617 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002618 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2619 !h2_has_too_many_cs(h2c)) {
2620 h2c->flags &= ~H2_CF_DEM_TOOMANY;
2621 if (h2_recv_allowed(h2c)) {
2622 __conn_xprt_want_recv(h2c->conn);
Olivier Houchard61d322f2018-09-24 18:02:03 +02002623 tasklet_wakeup(h2c->wait_list.task);
Willy Tarreauf2101912018-07-19 10:11:38 +02002624 conn_xprt_want_send(h2c->conn);
2625 }
2626 }
Willy Tarreau60935142017-10-16 18:11:19 +02002627
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002628 /* this stream may be blocked waiting for some data to leave (possibly
2629 * an ES or RST frame), so orphan it in this case.
2630 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002631 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002632 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002633 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002634 return;
2635
Willy Tarreau45f752e2017-10-30 15:44:59 +01002636 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2637 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2638 /* unblock the connection if it was blocked on this
2639 * stream.
2640 */
2641 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2642 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2643 conn_xprt_want_recv(cs->conn);
Olivier Houchard61d322f2018-09-24 18:02:03 +02002644 tasklet_wakeup(h2c->wait_list.task);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002645 conn_xprt_want_send(cs->conn);
2646 }
2647
Willy Tarreau71049cc2018-03-28 13:56:39 +02002648 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002649
Willy Tarreaue323f342018-03-28 13:51:45 +02002650 /* We don't want to close right now unless we're removing the
2651 * last stream, and either the connection is in error, or it
2652 * reached the ID already specified in a GOAWAY frame received
2653 * or sent (as seen by last_sid >= 0).
2654 */
2655 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2656 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002657 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Willy Tarreaue323f342018-03-28 13:51:45 +02002658 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002659 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002660 (conn_xprt_read0_pending(h2c->conn) ||
2661 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2662 /* no more stream will come, kill it now */
2663 h2_release(h2c->conn);
2664 }
2665 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002666 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002667 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2668 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002669 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002670 else
2671 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002672 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002673}
2674
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002675static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002676{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002677 struct h2c *h2c = h2s->h2c;
2678 struct wait_list *sw = &h2s->wait_list;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002679
Willy Tarreau721c9742017-11-07 11:05:42 +01002680 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002681 return;
2682
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002683 /* if no outgoing data was seen on this stream, it means it was
2684 * closed with a "tcp-request content" rule that is normally
2685 * used to kill the connection ASAP (eg: limit abuse). In this
2686 * case we send a goaway to close the connection.
2687 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002688 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002689 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002690 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002691
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002692 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2693 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002694 h2c_send_goaway_error(h2c, h2s) <= 0)
2695 return;
2696 if (b_data(&h2c->mbuf) && !(h2c->conn->flags & CO_FL_XPRT_WR_ENA))
2697 conn_xprt_want_send(h2c->conn);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002698
Willy Tarreau00dd0782018-03-01 16:31:34 +01002699 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002700
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002701 return;
2702add_to_list:
2703 if (LIST_ISEMPTY(&sw->list)) {
2704 sw->wait_reason |= SUB_CAN_SEND;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002705 if (h2s->flags & H2_SF_BLK_MFCTL)
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002706 LIST_ADDQ(&h2c->fctl_list, &sw->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002707 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002708 LIST_ADDQ(&h2c->send_list, &sw->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002709 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002710 /* Let the handler know we want shutr */
2711 sw->handle = (void *)((long)sw->handle | 1);
2712
Willy Tarreau62f52692017-10-08 23:01:42 +02002713}
2714
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002715static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002716{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002717 struct h2c *h2c = h2s->h2c;
2718 struct wait_list *sw = &h2s->wait_list;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002719
Willy Tarreau721c9742017-11-07 11:05:42 +01002720 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002721 return;
2722
Willy Tarreau67434202017-11-06 20:20:51 +01002723 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002724 /* we can cleanly close using an empty data frame only after headers */
2725
2726 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2727 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002728 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002729
2730 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002731 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002732 else
2733 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002734 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002735 /* if no outgoing data was seen on this stream, it means it was
2736 * closed with a "tcp-request content" rule that is normally
2737 * used to kill the connection ASAP (eg: limit abuse). In this
2738 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002739 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002740 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002741 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002742 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002743
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002744 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2745 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002746 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002747 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002748
Willy Tarreau00dd0782018-03-01 16:31:34 +01002749 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002750 }
2751
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002752 if (b_data(&h2s->h2c->mbuf) && !(h2c->conn->flags & CO_FL_XPRT_WR_ENA))
2753 conn_xprt_want_send(h2c->conn);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002754
2755 add_to_list:
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002756 sw = &h2s->wait_list;
2757
2758 if (LIST_ISEMPTY(&sw->list)) {
2759 sw->wait_reason |= SUB_CAN_SEND;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002760 if (h2s->flags & H2_SF_BLK_MFCTL)
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002761 LIST_ADDQ(&h2s->h2c->fctl_list, &sw->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002762 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002763 LIST_ADDQ(&h2s->h2c->send_list, &sw->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002764 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002765 /* let the handler know we want to shutr */
2766 sw->handle = (void *)((long)(sw->handle) | 2);
2767}
2768
2769static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
2770{
2771 struct h2s *h2s = ctx;
2772 long reason = (long)h2s->wait_list.handle;
2773
2774 if (reason & 1)
2775 h2_do_shutr(h2s);
2776 if (reason & 2)
2777 h2_do_shutw(h2s);
2778
2779 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02002780}
2781
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002782static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2783{
2784 struct h2s *h2s = cs->ctx;
2785
2786 if (!mode)
2787 return;
2788
2789 h2_do_shutr(h2s);
2790}
2791
2792static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2793{
2794 struct h2s *h2s = cs->ctx;
2795
2796 h2_do_shutw(h2s);
2797}
2798
Willy Tarreau13278b42017-10-13 19:23:14 +02002799/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2800 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2801 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002802 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002803 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002804static int h2_frt_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002805{
2806 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002807 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02002808 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002809 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02002810 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002811 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01002812 struct buffer *csbuf;
Willy Tarreau13278b42017-10-13 19:23:14 +02002813 int flen = h2c->dfl;
2814 int outlen = 0;
2815 int wrap;
2816 int try;
2817
2818 if (!h2c->dfl) {
2819 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002820 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002821 return 0;
2822 }
2823
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002824 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01002825 return 0; // incomplete input frame
2826
Willy Tarreau13278b42017-10-13 19:23:14 +02002827 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002828 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02002829 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002830 copy = alloc_trash_chunk();
2831 if (!copy) {
2832 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2833 goto fail;
2834 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002835 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
2836 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
2837 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02002838 }
2839
2840 /* The padlen is the first byte before data, and the padding appears
2841 * after data. padlen+data+padding are included in flen.
2842 */
2843 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002844 h2c->dpl = *hdrs;
2845 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002846 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2847 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002848 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002849 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002850 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002851 hdrs += 1; // skip Pad Length
2852 }
2853
2854 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2855 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002856 if (read_n32(hdrs) == h2s->id) {
2857 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2858 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002859 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002860 }
2861
Willy Tarreau13278b42017-10-13 19:23:14 +02002862 hdrs += 5; // stream dep = 4, weight = 1
2863 flen -= 5;
2864 }
2865
2866 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2867 * don't support this for now and can't even decompress so we have to
2868 * break the connection.
2869 */
2870 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2871 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002872 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002873 }
2874
Olivier Houchard638b7992018-08-16 15:41:52 +02002875 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01002876 if (!csbuf) {
2877 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002878 goto fail;
2879 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002880
Willy Tarreau937f7602018-02-26 15:22:17 +01002881 /* we can't retry a failed decompression operation so we must be very
2882 * careful not to take any risks. In practice the output buffer is
2883 * always empty except maybe for trailers, in which case we simply have
2884 * to wait for the upper layer to finish consuming what is available.
2885 */
2886 if (b_data(csbuf))
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002887 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002888
Willy Tarreau937f7602018-02-26 15:22:17 +01002889 csbuf->head = 0;
2890 try = b_size(csbuf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002891
2892 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2893 sizeof(list)/sizeof(list[0]), tmp);
2894 if (outlen < 0) {
2895 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2896 goto fail;
2897 }
2898
2899 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002900 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreau937f7602018-02-26 15:22:17 +01002901 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002902
2903 if (outlen < 0) {
2904 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2905 goto fail;
2906 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002907
Willy Tarreau174b06a2018-04-25 18:13:58 +02002908 if (msgf & H2_MSGF_BODY) {
2909 /* a payload is present */
2910 if (msgf & H2_MSGF_BODY_CL)
2911 h2s->flags |= H2_SF_DATA_CLEN;
2912 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
2913 h2s->flags |= H2_SF_DATA_CHNK;
2914 }
2915
Willy Tarreau13278b42017-10-13 19:23:14 +02002916 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002917 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02002918 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01002919 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02002920
Willy Tarreau39d68502018-03-02 12:26:37 +01002921 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002922 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002923 h2s->cs->flags |= CS_FL_REOS;
2924 }
Willy Tarreau937f7602018-02-26 15:22:17 +01002925
Willy Tarreau68dd9852017-07-03 14:44:26 +02002926 leave:
2927 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002928 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002929 fail:
2930 outlen = 0;
2931 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002932}
2933
Willy Tarreau454f9052017-10-26 19:40:35 +02002934/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2935 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2936 * in use, a new chunk is emitted for each frame. This is supposed to fit
2937 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2938 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2939 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2940 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002941 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2942 * checked to know if some data remain pending (an empty DATA frame can return
2943 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2944 * connection errors in h2c->errcode. The caller must already have checked the
2945 * frame header and ensured that the frame was complete or the buffer full. It
2946 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002947 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002948static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02002949{
2950 struct h2c *h2c = h2s->h2c;
2951 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002952 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002953 unsigned int chklen = 0;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002954 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02002955
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002956 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002957
2958 /* The padlen is the first byte before data, and the padding appears
2959 * after data. padlen+data+padding are included in flen.
2960 */
Willy Tarreau79127812017-12-03 21:06:59 +01002961 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002962 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002963 return 0;
2964
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002965 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002966 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002967 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2968 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002969 return 0;
2970 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002971
2972 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002973 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002974 h2c->dfl--;
2975 h2c->rcvd_c++; h2c->rcvd_s++;
2976 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002977 }
2978
Olivier Houchard638b7992018-08-16 15:41:52 +02002979 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01002980 if (!csbuf) {
2981 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002982 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002983 }
2984
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002985 flen = h2c->dfl - h2c->dpl;
2986 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002987 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002988
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002989 if (flen > b_data(&h2c->dbuf)) {
2990 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002991 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01002992 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002993 }
2994
2995 if (unlikely(b_space_wraps(csbuf))) {
2996 /* it doesn't fit and the buffer is fragmented,
2997 * so let's defragment it and try again.
2998 */
2999 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003000 }
3001
Willy Tarreaueba10f22018-04-25 20:44:22 +02003002 /* chunked-encoding requires more room */
3003 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003004 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003005 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3006 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3007 (chklen < 1048576) ? 4 : 8;
3008 chklen += 4; // CRLF, CRLF
3009 }
3010
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003011 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003012 if (flen + chklen > b_room(csbuf)) {
3013 if (chklen >= b_room(csbuf)) {
3014 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003015 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003016 }
3017 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003018 }
3019
3020 if (h2s->flags & H2_SF_DATA_CHNK) {
3021 /* emit the chunk size */
3022 unsigned int chksz = flen;
3023 char str[10];
3024 char *beg;
3025
3026 beg = str + sizeof(str);
3027 *--beg = '\n';
3028 *--beg = '\r';
3029 do {
3030 *--beg = hextab[chksz & 0xF];
3031 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003032 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003033 }
3034
Willy Tarreau454f9052017-10-26 19:40:35 +02003035 /* Block1 is the length of the first block before the buffer wraps,
3036 * block2 is the optional second block to reach the end of the frame.
3037 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003038 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003039 if (block1 > flen)
3040 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003041 block2 = flen - block1;
3042
3043 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003044 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003045
3046 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003047 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003048
Willy Tarreaueba10f22018-04-25 20:44:22 +02003049 if (h2s->flags & H2_SF_DATA_CHNK) {
3050 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003051 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003052 }
3053
Willy Tarreau454f9052017-10-26 19:40:35 +02003054 /* now mark the input data as consumed (will be deleted from the buffer
3055 * by the caller when seeing FRAME_A after sending the window update).
3056 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003057 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003058 h2c->dfl -= flen;
3059 h2c->rcvd_c += flen;
3060 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3061
3062 if (h2c->dfl > h2c->dpl) {
3063 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003064 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003065 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003066 }
3067
Willy Tarreau4a28da12018-01-04 14:41:00 +01003068 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003069 /* here we're done with the frame, all the payload (except padding) was
3070 * transferred.
3071 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003072
3073 if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) {
3074 /* emit the trailing 0 CRLF CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003075 if (b_room(csbuf) < 5) {
3076 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003077 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003078 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003079 chklen += 5;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003080 b_putblk(csbuf, "0\r\n\r\n", 5);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003081 }
3082
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003083 h2c->rcvd_c += h2c->dpl;
3084 h2c->rcvd_s += h2c->dpl;
3085 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003086 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3087
Willy Tarreau39d68502018-03-02 12:26:37 +01003088 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003089 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003090 h2s->cs->flags |= CS_FL_REOS;
3091 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003092
Willy Tarreau454b57b2018-02-26 15:50:05 +01003093 return flen + chklen;
3094 fail:
3095 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003096}
3097
Willy Tarreau5dd17352018-06-14 13:33:30 +02003098/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3099 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3100 * number of bytes sent. The caller must check the stream's status to detect
3101 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003102 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003103static 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 +02003104{
3105 struct http_hdr list[MAX_HTTP_HDR];
3106 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003107 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003108 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003109 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003110 int es_now = 0;
3111 int ret = 0;
3112 int hdr;
3113
3114 if (h2c_mux_busy(h2c, h2s)) {
3115 h2s->flags |= H2_SF_BLK_MBUSY;
3116 return 0;
3117 }
3118
Willy Tarreau44e973f2018-03-01 17:49:30 +01003119 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003120 h2c->flags |= H2_CF_MUX_MALLOC;
3121 h2s->flags |= H2_SF_BLK_MROOM;
3122 return 0;
3123 }
3124
3125 /* First, try to parse the H1 response and index it into <list>.
3126 * NOTE! Since it comes from haproxy, we *know* that a response header
3127 * block does not wrap and we can safely read it this way without
3128 * having to realign the buffer.
3129 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003130 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003131 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003132 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003133 /* incomplete or invalid response, this is abnormal coming from
3134 * haproxy and may only result in a bad errorfile or bad Lua code
3135 * so that won't be fixed, raise an error now.
3136 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003137 * FIXME: we should instead add the ability to only return a
3138 * 502 bad gateway. But in theory this is not supposed to
3139 * happen.
3140 */
3141 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3142 ret = 0;
3143 goto end;
3144 }
3145
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003146 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003147
3148 /* certain statuses have no body or an empty one, regardless of
3149 * what the headers say.
3150 */
3151 if (sl.st.status >= 100 && sl.st.status < 200) {
3152 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3153 h1m->curr_len = h1m->body_len = 0;
3154 }
3155 else if (sl.st.status == 204 || sl.st.status == 304) {
3156 /* no contents, claim c-len is present and set to zero */
3157 h1m->flags &= ~H1_MF_CHNK;
3158 h1m->flags |= H1_MF_CLEN;
3159 h1m->curr_len = h1m->body_len = 0;
3160 }
3161
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003162 chunk_reset(&outbuf);
3163
3164 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003165 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003166 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003167 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003168
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003169 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003170 break;
3171 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003172 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003173 }
3174
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003175 if (outbuf.size < 9)
3176 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003177
3178 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003179 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3180 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3181 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003182
3183 /* encode status, which necessarily is the first one */
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003184 if (outbuf.data < outbuf.size && h2s->status == 200)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003185 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003186 else if (outbuf.data < outbuf.size && h2s->status == 304)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003187 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003188 else if (unlikely(list[0].v.len != 3)) {
3189 /* this is an unparsable response */
3190 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3191 ret = 0;
3192 goto end;
3193 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003194 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003195 /* basic encoding of the status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003196 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3197 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3198 outbuf.area[outbuf.data++] = list[0].v.ptr[0];
3199 outbuf.area[outbuf.data++] = list[0].v.ptr[1];
3200 outbuf.area[outbuf.data++] = list[0].v.ptr[2];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003201 }
3202 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003203 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003204 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003205 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003206 }
3207
3208 /* encode all headers, stop at empty name */
3209 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003210 /* these ones do not exist in H2 and must be dropped. */
3211 if (isteq(list[hdr].n, ist("connection")) ||
3212 isteq(list[hdr].n, ist("proxy-connection")) ||
3213 isteq(list[hdr].n, ist("keep-alive")) ||
3214 isteq(list[hdr].n, ist("upgrade")) ||
3215 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003216 continue;
3217
3218 if (isteq(list[hdr].n, ist("")))
3219 break; // end
3220
3221 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3222 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003223 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003224 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003225 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003226 }
3227 }
3228
3229 /* we may need to add END_STREAM */
3230 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3231 es_now = 1;
3232
3233 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003234 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003235
3236 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003237 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003238
3239 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003240 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003241
3242 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003243 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003244 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003245
3246 /* for now we don't implemented CONTINUATION, so we wait for a
3247 * body or directly end in TRL2.
3248 */
3249 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003250 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003251 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003252
Willy Tarreau801250e2018-09-11 11:45:04 +02003253 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003254 h2s->flags |= H2_SF_ES_SENT;
3255 if (h2s->st == H2_SS_OPEN)
3256 h2s->st = H2_SS_HLOC;
3257 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003258 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003259 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003260 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003261 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003262 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003263 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003264 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003265 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003266 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003267
3268 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003269
3270 end:
3271 //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, h1_msg_state_str(h1m->err_state));
3272 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003273 full:
3274 h1m_init_res(h1m);
3275 h1m->err_pos = -1; // don't care about errors on the response path
3276 h2c->flags |= H2_CF_MUX_MFULL;
3277 h2s->flags |= H2_SF_BLK_MROOM;
3278 ret = 0;
3279 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003280}
3281
Willy Tarreau5dd17352018-06-14 13:33:30 +02003282/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3283 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3284 * the number of bytes sent. The caller must check the stream's status to
3285 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003286 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003287static 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 +02003288{
3289 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003290 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003291 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003292 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003293 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003294 int es_now = 0;
3295 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003296 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003297 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003298
3299 if (h2c_mux_busy(h2c, h2s)) {
3300 h2s->flags |= H2_SF_BLK_MBUSY;
3301 goto end;
3302 }
3303
Willy Tarreau44e973f2018-03-01 17:49:30 +01003304 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003305 h2c->flags |= H2_CF_MUX_MALLOC;
3306 h2s->flags |= H2_SF_BLK_MROOM;
3307 goto end;
3308 }
3309
3310 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003311 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003312 goto end;
3313
3314 chunk_reset(&outbuf);
3315
3316 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003317 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003318 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003319 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003320
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003321 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003322 break;
3323 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003324 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003325 }
3326
3327 if (outbuf.size < 9) {
3328 h2c->flags |= H2_CF_MUX_MFULL;
3329 h2s->flags |= H2_SF_BLK_MROOM;
3330 goto end;
3331 }
3332
3333 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003334 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3335 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3336 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003337
3338 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3339 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003340 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003341 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003342 break;
3343 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003344 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003345 if ((long long)size > h1m->curr_len)
3346 size = h1m->curr_len;
3347 break;
3348 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003349 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003350 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003351 if (!ret)
3352 goto end;
3353
3354 if (ret < 0) {
3355 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003356 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003357 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3358 goto end;
3359 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003360 max -= ret;
3361 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003362 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003363 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003364 }
3365
Willy Tarreau801250e2018-09-11 11:45:04 +02003366 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003367 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003368 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003369 if (!ret)
3370 goto end;
3371
3372 if (ret < 0) {
3373 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003374 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003375 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3376 goto end;
3377 }
3378
3379 size = chunk;
3380 h1m->curr_len = chunk;
3381 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003382 max -= ret;
3383 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003384 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003385 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003386 if (!size)
3387 goto send_empty;
3388 }
3389
3390 /* in MSG_DATA state, continue below */
3391 size = h1m->curr_len;
3392 break;
3393 }
3394
3395 /* we have in <size> the exact number of bytes we need to copy from
3396 * the H1 buffer. We need to check this against the connection's and
3397 * the stream's send windows, and to ensure that this fits in the max
3398 * frame size and in the buffer's available space minus 9 bytes (for
3399 * the frame header). The connection's flow control is applied last so
3400 * that we can use a separate list of streams which are immediately
3401 * unblocked on window opening. Note: we don't implement padding.
3402 */
3403
Willy Tarreau5dd17352018-06-14 13:33:30 +02003404 if (size > max)
3405 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003406
3407 if (size > h2s->mws)
3408 size = h2s->mws;
3409
3410 if (size <= 0) {
3411 h2s->flags |= H2_SF_BLK_SFCTL;
3412 goto end;
3413 }
3414
3415 if (h2c->mfs && size > h2c->mfs)
3416 size = h2c->mfs;
3417
3418 if (size + 9 > outbuf.size) {
3419 /* we have an opportunity for enlarging the too small
3420 * available space, let's try.
3421 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003422 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003423 goto realign_again;
3424 size = outbuf.size - 9;
3425 }
3426
3427 if (size <= 0) {
3428 h2c->flags |= H2_CF_MUX_MFULL;
3429 h2s->flags |= H2_SF_BLK_MROOM;
3430 goto end;
3431 }
3432
3433 if (size > h2c->mws)
3434 size = h2c->mws;
3435
3436 if (size <= 0) {
3437 h2s->flags |= H2_SF_BLK_MFCTL;
3438 goto end;
3439 }
3440
3441 /* copy whatever we can */
3442 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003443 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003444 if (ret == 1)
3445 len2 = 0;
3446
3447 if (!ret || len1 + len2 < size) {
3448 /* FIXME: must normally never happen */
3449 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3450 goto end;
3451 }
3452
3453 /* limit len1/len2 to size */
3454 if (len1 + len2 > size) {
3455 int sub = len1 + len2 - size;
3456
3457 if (len2 > sub)
3458 len2 -= sub;
3459 else {
3460 sub -= len2;
3461 len2 = 0;
3462 len1 -= sub;
3463 }
3464 }
3465
3466 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003467 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003468 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003469 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003470
3471 send_empty:
3472 /* we may need to add END_STREAM */
3473 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3474 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003475 *
3476 * FIXME: what we do here is not correct because we send end_stream
3477 * before knowing if we'll have to send a HEADERS frame for the
3478 * trailers. More importantly we're not consuming the trailing CRLF
3479 * after the end of trailers, so it will be left to the caller to
3480 * eat it. The right way to do it would be to measure trailers here
3481 * and to send ES only if there are no trailers.
3482 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003483 */
3484 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02003485 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003486 es_now = 1;
3487
3488 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003489 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003490
3491 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003492 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003493
3494 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003495 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003496
3497 /* consume incoming H1 response */
3498 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003499 max -= size;
3500 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003501 total += size;
3502 h1m->curr_len -= size;
3503 h2s->mws -= size;
3504 h2c->mws -= size;
3505
3506 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02003507 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003508 goto new_frame;
3509 }
3510 }
3511
3512 if (es_now) {
3513 if (h2s->st == H2_SS_OPEN)
3514 h2s->st = H2_SS_HLOC;
3515 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003516 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003517
Willy Tarreau35a62702018-02-27 15:37:25 +01003518 if (!(h1m->flags & H1_MF_CHNK)) {
3519 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003520 total += max;
3521 ofs += max;
3522 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003523
Willy Tarreau801250e2018-09-11 11:45:04 +02003524 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003525 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003526
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003527 h2s->flags |= H2_SF_ES_SENT;
3528 }
3529
3530 end:
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02003531 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, h1_msg_state_str(h1m->state), h1m->err_pos, h1_msg_state_str(h1m->err_state), h2c->mws, h2s->mws, (unsigned int)b_data(buf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003532 return total;
3533}
3534
Olivier Houchard6ff20392018-07-17 18:46:31 +02003535/* Called from the upper layer, to subscribe to events, such as being able to send */
3536static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
3537{
3538 struct wait_list *sw;
3539 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003540 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003541
3542 switch (event_type) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003543 case SUB_CAN_RECV:
3544 sw = param;
3545 if (!(sw->wait_reason & SUB_CAN_RECV)) {
3546 sw->wait_reason |= SUB_CAN_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003547 sw->handle = h2s;
Olivier Houchardaf4021e2018-08-09 13:06:55 +02003548 h2s->recv_wait_list = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003549 }
3550 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003551 case SUB_CAN_SEND:
3552 sw = param;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003553 if (!(sw->wait_reason & SUB_CAN_SEND)) {
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003554 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003555 sw->handle = h2s;
3556 if (h2s->flags & H2_SF_BLK_MFCTL)
3557 LIST_ADDQ(&h2c->fctl_list, &sw->list);
3558 else
3559 LIST_ADDQ(&h2c->send_list, &sw->list);
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003560 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02003561 return 0;
3562 default:
3563 break;
3564 }
3565 return -1;
3566
3567
3568}
3569
Olivier Houchard511efea2018-08-16 15:30:32 +02003570/* Called from the upper layer, to receive data */
3571static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3572{
Olivier Houchard638b7992018-08-16 15:41:52 +02003573 struct h2s *h2s = cs->ctx;
Olivier Houchard511efea2018-08-16 15:30:32 +02003574 size_t ret = 0;
3575
3576 /* transfer possibly pending data to the upper layer */
Olivier Houchard638b7992018-08-16 15:41:52 +02003577 ret = b_xfer(buf, &h2s->rxbuf, count);
Olivier Houchard511efea2018-08-16 15:30:32 +02003578
Olivier Houchard638b7992018-08-16 15:41:52 +02003579 if (b_data(&h2s->rxbuf))
Olivier Houchard511efea2018-08-16 15:30:32 +02003580 cs->flags |= CS_FL_RCV_MORE;
3581 else {
3582 cs->flags &= ~CS_FL_RCV_MORE;
3583 if (cs->flags & CS_FL_REOS)
3584 cs->flags |= CS_FL_EOS;
Olivier Houchard638b7992018-08-16 15:41:52 +02003585 if (b_size(&h2s->rxbuf)) {
3586 b_free(&h2s->rxbuf);
3587 offer_buffers(NULL, tasks_run_queue);
3588 }
Olivier Houchard511efea2018-08-16 15:30:32 +02003589 }
3590
3591 return ret;
3592}
3593
Willy Tarreau62f52692017-10-08 23:01:42 +02003594/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003595static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02003596{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003597 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003598 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003599 size_t ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003600
Willy Tarreau0bad0432018-06-14 16:54:01 +02003601 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01003602 h2s->flags |= H2_SF_OUTGOING_DATA;
3603
Willy Tarreaua40704a2018-09-11 13:52:04 +02003604 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02003605 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003606 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003607 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02003608 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003609 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003610 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02003611 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003612 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003613 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003614
Willy Tarreau5dd17352018-06-14 13:33:30 +02003615 if (unlikely((int)ret <= 0)) {
3616 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003617 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3618 break;
3619 }
Willy Tarreau35a62702018-02-27 15:37:25 +01003620 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02003621 total += count;
3622 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003623 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003624 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003625 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003626 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003627 cs->flags |= CS_FL_ERROR;
3628 break;
3629 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02003630
3631 total += ret;
3632 count -= ret;
3633
3634 if (h2s->st >= H2_SS_ERROR)
3635 break;
3636
3637 if (h2s->flags & H2_SF_BLK_ANY)
3638 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003639 }
3640
Willy Tarreau00610962018-07-19 10:58:28 +02003641 if (h2s->st >= H2_SS_ERROR) {
3642 /* trim any possibly pending data after we close (extra CR-LF,
3643 * unprocessed trailers, abnormal extra data, ...)
3644 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003645 total += count;
3646 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02003647 }
3648
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003649 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003650 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003651 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003652 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003653 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003654 }
3655
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003656 b_del(buf, total);
Olivier Houchard7505f942018-08-21 18:10:44 +02003657 if (total > 0) {
Olivier Houchardfab7c7e2018-08-21 16:36:10 +02003658 conn_xprt_want_send(h2s->h2c->conn);
Olivier Houchard7505f942018-08-21 18:10:44 +02003659 if (!(h2s->h2c->wait_list.wait_reason & SUB_CAN_SEND))
3660 tasklet_wakeup(h2s->h2c->wait_list.task);
3661 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003662 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003663}
3664
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003665/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02003666static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003667{
3668 struct h2c *h2c = conn->mux_ctx;
3669 struct h2s *h2s;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003670 struct wait_list *sw;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003671 struct eb32_node *node;
3672 int fctl_cnt = 0;
3673 int send_cnt = 0;
3674 int tree_cnt = 0;
3675 int orph_cnt = 0;
3676
3677 if (!h2c)
3678 return;
3679
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003680 list_for_each_entry(sw, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003681 fctl_cnt++;
3682
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003683 list_for_each_entry(sw, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003684 send_cnt++;
3685
3686 node = eb32_first(&h2c->streams_by_id);
3687 while (node) {
3688 h2s = container_of(node, struct h2s, by_id);
3689 tree_cnt++;
3690 if (!h2s->cs)
3691 orph_cnt++;
3692 node = eb32_next(node);
3693 }
3694
Willy Tarreau616ac812018-07-24 14:12:42 +02003695 chunk_appendf(msg, " st0=%d err=%d maxid=%d lastid=%d flg=0x%08x nbst=%u nbcs=%u"
3696 " fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
3697 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
3698 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
3699 (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
3700 (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003701}
Willy Tarreau62f52692017-10-08 23:01:42 +02003702
3703/*******************************************************/
3704/* functions below are dedicated to the config parsers */
3705/*******************************************************/
3706
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003707/* config parser for global "tune.h2.header-table-size" */
3708static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3709 struct proxy *defpx, const char *file, int line,
3710 char **err)
3711{
3712 if (too_many_args(1, args, err, NULL))
3713 return -1;
3714
3715 h2_settings_header_table_size = atoi(args[1]);
3716 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3717 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3718 return -1;
3719 }
3720 return 0;
3721}
Willy Tarreau62f52692017-10-08 23:01:42 +02003722
Willy Tarreaue6baec02017-07-27 11:45:11 +02003723/* config parser for global "tune.h2.initial-window-size" */
3724static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3725 struct proxy *defpx, const char *file, int line,
3726 char **err)
3727{
3728 if (too_many_args(1, args, err, NULL))
3729 return -1;
3730
3731 h2_settings_initial_window_size = atoi(args[1]);
3732 if (h2_settings_initial_window_size < 0) {
3733 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3734 return -1;
3735 }
3736 return 0;
3737}
3738
Willy Tarreau5242ef82017-07-27 11:47:28 +02003739/* config parser for global "tune.h2.max-concurrent-streams" */
3740static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3741 struct proxy *defpx, const char *file, int line,
3742 char **err)
3743{
3744 if (too_many_args(1, args, err, NULL))
3745 return -1;
3746
3747 h2_settings_max_concurrent_streams = atoi(args[1]);
3748 if (h2_settings_max_concurrent_streams < 0) {
3749 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3750 return -1;
3751 }
3752 return 0;
3753}
3754
Willy Tarreau62f52692017-10-08 23:01:42 +02003755
3756/****************************************/
3757/* MUX initialization and instanciation */
3758/***************************************/
3759
3760/* The mux operations */
3761const struct mux_ops h2_ops = {
3762 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003763 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02003764 .update_poll = h2_update_poll,
Willy Tarreau62f52692017-10-08 23:01:42 +02003765 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02003766 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02003767 .subscribe = h2_subscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02003768 .attach = h2_attach,
3769 .detach = h2_detach,
3770 .shutr = h2_shutr,
3771 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003772 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003773 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003774 .name = "H2",
3775};
3776
Christopher Faulet32f61c02018-04-10 14:33:41 +02003777/* PROTO selection : this mux registers PROTO token "h2" */
3778static struct mux_proto_list mux_proto_h2 =
3779 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02003780
3781/* config keyword parsers */
3782static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003783 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003784 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003785 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003786 { 0, NULL, NULL }
3787}};
3788
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003789static void __h2_deinit(void)
3790{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003791 pool_destroy(pool_head_h2s);
3792 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003793}
3794
Willy Tarreau62f52692017-10-08 23:01:42 +02003795__attribute__((constructor))
3796static void __h2_init(void)
3797{
Christopher Faulet32f61c02018-04-10 14:33:41 +02003798 register_mux_proto(&mux_proto_h2);
Willy Tarreau62f52692017-10-08 23:01:42 +02003799 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003800 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003801 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3802 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003803}