blob: b02f4c3136fd7bebf7fe1f05a21c512892c23ca8 [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;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200384
Willy Tarreau32218eb2017-09-22 08:07:25 +0200385 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
386 if (!h2c->ddht)
387 goto fail;
388
389 /* Initialise the context. */
390 h2c->st0 = H2_CS_PREFACE;
391 h2c->conn = conn;
392 h2c->max_id = -1;
393 h2c->errcode = H2_ERR_NO_ERROR;
394 h2c->flags = H2_CF_NONE;
395 h2c->rcvd_c = 0;
396 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100397 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200398 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200399
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200400 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200401 h2c->dsi = -1;
402 h2c->msi = -1;
403 h2c->last_sid = -1;
404
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200405 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200406 h2c->miw = 65535; /* mux initial window size */
407 h2c->mws = 65535; /* mux window size */
408 h2c->mfs = 16384; /* initial max frame size */
409 h2c->streams_by_id = EB_ROOT_UNIQUE;
410 LIST_INIT(&h2c->send_list);
411 LIST_INIT(&h2c->fctl_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100412 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200413 conn->mux_ctx = h2c;
414
Willy Tarreau3f133572017-10-31 19:21:06 +0100415 if (t)
416 task_queue(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200417 conn_xprt_want_recv(conn);
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200418 LIST_INIT(&h2c->wait_list.list);
Willy Tarreauea392822017-10-31 10:02:25 +0100419
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200420 /* Try to read, if nothing is available yet we'll just subscribe */
Olivier Houchard7505f942018-08-21 18:10:44 +0200421 if (h2_recv(h2c))
422 h2_process(h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200423 return 0;
424 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100425 if (t)
426 task_free(t);
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200427 if (h2c->wait_list.task)
428 tasklet_free(h2c->wait_list.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100429 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200430 return -1;
431}
432
Willy Tarreau62f52692017-10-08 23:01:42 +0200433/* Initialize the mux once it's attached. For outgoing connections, the context
434 * is already initialized before installing the mux, so we detect incoming
435 * connections from the fact that the context is still NULL. Returns < 0 on
436 * error.
437 */
Willy Tarreau175a2bb2018-09-12 12:02:05 +0200438static int h2_init(struct connection *conn, struct proxy *prx)
Willy Tarreau62f52692017-10-08 23:01:42 +0200439{
440 if (conn->mux_ctx) {
441 /* we don't support outgoing connections for now */
442 return -1;
443 }
444
Willy Tarreau32218eb2017-09-22 08:07:25 +0200445 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200446}
447
Willy Tarreau2373acc2017-10-12 17:35:14 +0200448/* returns the stream associated with id <id> or NULL if not found */
449static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
450{
451 struct eb32_node *node;
452
Willy Tarreau2a856182017-05-16 15:20:39 +0200453 if (id > h2c->max_id)
454 return (struct h2s *)h2_idle_stream;
455
Willy Tarreau2373acc2017-10-12 17:35:14 +0200456 node = eb32_lookup(&h2c->streams_by_id, id);
457 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200458 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200459
460 return container_of(node, struct h2s, by_id);
461}
462
Willy Tarreau62f52692017-10-08 23:01:42 +0200463/* release function for a connection. This one should be called to free all
464 * resources allocated to the mux.
465 */
466static void h2_release(struct connection *conn)
467{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200468 struct h2c *h2c = conn->mux_ctx;
469
470 LIST_DEL(&conn->list);
471
472 if (h2c) {
473 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200474
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100475 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100476 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100477 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200478
Willy Tarreau44e973f2018-03-01 17:49:30 +0100479 h2_release_buf(h2c, &h2c->dbuf);
480 h2_release_buf(h2c, &h2c->mbuf);
481
Willy Tarreauea392822017-10-31 10:02:25 +0100482 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200483 h2c->task->context = NULL;
484 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100485 h2c->task = NULL;
486 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200487 if (h2c->wait_list.task)
488 tasklet_free(h2c->wait_list.task);
Olivier Houchardc2aa7112018-09-11 18:27:21 +0200489 LIST_DEL(&h2c->wait_list.list);
490 LIST_INIT(&h2c->wait_list.list);
Olivier Houchard251f6a22018-09-12 17:52:46 +0200491 while (!LIST_ISEMPTY(&h2c->send_list)) {
492 struct wait_list *sw = LIST_ELEM(h2c->send_list.n,
493 struct wait_list *, list);
494 LIST_DEL(&sw->list);
495 LIST_INIT(&sw->list);
496 }
497 while (!LIST_ISEMPTY(&h2c->fctl_list)) {
498 struct wait_list *sw = LIST_ELEM(h2c->fctl_list.n,
499 struct wait_list *, list);
500 LIST_DEL(&sw->list);
501 LIST_INIT(&sw->list);
502 }
503
Willy Tarreauea392822017-10-31 10:02:25 +0100504
Willy Tarreaubafbe012017-11-24 17:34:44 +0100505 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200506 }
507
508 conn->mux = NULL;
509 conn->mux_ctx = NULL;
510
511 conn_stop_tracking(conn);
512 conn_full_close(conn);
513 if (conn->destroy_cb)
514 conn->destroy_cb(conn);
515 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200516}
517
518
Willy Tarreau71681172017-10-23 14:39:06 +0200519/******************************************************/
520/* functions below are for the H2 protocol processing */
521/******************************************************/
522
523/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100524static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200525{
526 return h2s ? h2s->id : 0;
527}
528
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200529/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100530static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200531{
532 if (h2c->msi < 0)
533 return 0;
534
535 if (h2c->msi == h2s_id(h2s))
536 return 0;
537
538 return 1;
539}
540
Willy Tarreau741d6df2017-10-17 08:00:59 +0200541/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100542static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200543{
544 h2c->errcode = err;
545 h2c->st0 = H2_CS_ERROR;
546}
547
Willy Tarreau2e43f082017-10-17 08:03:59 +0200548/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100549static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200550{
551 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
552 h2s->errcode = err;
553 h2s->st = H2_SS_ERROR;
554 if (h2s->cs)
555 h2s->cs->flags |= CS_FL_ERROR;
556 }
557}
558
Willy Tarreaue4820742017-07-27 13:37:23 +0200559/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100560static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200561{
562 uint8_t *out = frame;
563
564 *out = len >> 16;
565 write_n16(out + 1, len);
566}
567
Willy Tarreau54c15062017-10-10 17:10:03 +0200568/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
569 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
570 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200571 * available in the buffer's input prior to calling this function. The buffer
572 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200573 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100574static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200575 const struct buffer *b, int o)
576{
Willy Tarreau591d4452018-06-15 17:21:00 +0200577 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 +0200578}
579
Willy Tarreau1f094672017-11-20 21:27:45 +0100580static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200581{
Willy Tarreau591d4452018-06-15 17:21:00 +0200582 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200583}
584
Willy Tarreau1f094672017-11-20 21:27:45 +0100585static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200586{
Willy Tarreau591d4452018-06-15 17:21:00 +0200587 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200588}
589
Willy Tarreau1f094672017-11-20 21:27:45 +0100590static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200591{
Willy Tarreau591d4452018-06-15 17:21:00 +0200592 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200593}
594
595
Willy Tarreau715d5312017-07-11 15:20:24 +0200596/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
597 * is not obvious. It turns out that H2 headers are neither aligned nor do they
598 * use regular sizes. And to add to the trouble, the buffer may wrap so each
599 * byte read must be checked. The header is formed like this :
600 *
601 * b0 b1 b2 b3 b4 b5..b8
602 * +----------+---------+--------+----+----+----------------------+
603 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
604 * +----------+---------+--------+----+----+----------------------+
605 *
606 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
607 * we get the sid properly aligned and ordered, and 16 bits of len properly
608 * ordered as well. The type and flags can be extracted using bit shifts from
609 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200610 * Returns zero if some bytes are missing, otherwise non-zero on success. The
611 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200612 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100613static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200614{
615 uint64_t w;
616
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200617 if (b_data(b) < 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200618 return 0;
619
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200620 w = h2_get_n64(b, 1);
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200621 h->len = *(uint8_t*)b_head(b) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200622 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
623 h->ff = w >> 32;
624 h->ft = w >> 40;
625 h->len += w >> 48;
626 return 1;
627}
628
629/* skip the next 9 bytes corresponding to the frame header possibly parsed by
630 * h2_peek_frame_hdr() above.
631 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100632static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200633{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200634 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200635}
636
637/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100638static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200639{
640 int ret;
641
642 ret = h2_peek_frame_hdr(b, h);
643 if (ret > 0)
644 h2_skip_frame_hdr(b);
645 return ret;
646}
647
Willy Tarreau00dd0782018-03-01 16:31:34 +0100648/* marks stream <h2s> as CLOSED and decrement the number of active streams for
649 * its connection if the stream was not yet closed. Please use this exclusively
650 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100651 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100652static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100653{
654 if (h2s->st != H2_SS_CLOSED)
655 h2s->h2c->nb_streams--;
656 h2s->st = H2_SS_CLOSED;
657}
658
Willy Tarreau71049cc2018-03-28 13:56:39 +0200659/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
660static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100661{
662 h2s_close(h2s);
663 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200664 if (b_size(&h2s->rxbuf)) {
665 b_free(&h2s->rxbuf);
666 offer_buffers(NULL, tasks_run_queue);
667 }
Olivier Houchardc2aa7112018-09-11 18:27:21 +0200668 LIST_DEL(&h2s->wait_list.list);
669 LIST_INIT(&h2s->wait_list.list);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200670 tasklet_free(h2s->wait_list.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100671 pool_free(pool_head_h2s, h2s);
672}
673
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200674/* creates a new stream <id> on the h2c connection and returns it, or NULL in
675 * case of memory allocation error.
676 */
677static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
678{
Willy Tarreau590a0512018-09-05 11:56:48 +0200679 struct session *sess = h2c->conn->owner;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200680 struct conn_stream *cs;
681 struct h2s *h2s;
682
Willy Tarreaubafbe012017-11-24 17:34:44 +0100683 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200684 if (!h2s)
685 goto out;
686
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200687 h2s->wait_list.task = tasklet_new();
688 if (!h2s->wait_list.task) {
689 pool_free(pool_head_h2s, h2s);
690 goto out;
691 }
692 LIST_INIT(&h2s->wait_list.list);
693 h2s->recv_wait_list = NULL;
694 h2s->wait_list.task->process = h2_deferred_shut;
695 h2s->wait_list.task->context = h2s;
696 h2s->wait_list.handle = NULL;
697 h2s->wait_list.wait_reason = 0;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200698 h2s->h2c = h2c;
699 h2s->mws = h2c->miw;
700 h2s->flags = H2_SF_NONE;
701 h2s->errcode = H2_ERR_NO_ERROR;
702 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200703 h2s->status = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200704 h2s->rxbuf = BUF_NULL;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200705 h1m_init_res(&h2s->h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +0200706 h2s->h1m.err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +0200707 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200708 h2s->by_id.key = h2s->id = id;
709 h2c->max_id = id;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200710
711 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100712 h2c->nb_streams++;
713 if (h2c->nb_streams > h2_settings_max_concurrent_streams)
714 goto out_close;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200715
716 cs = cs_new(h2c->conn);
717 if (!cs)
718 goto out_close;
719
720 h2s->cs = cs;
721 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200722 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200723
724 if (stream_create_from_cs(cs) < 0)
725 goto out_free_cs;
726
Willy Tarreau590a0512018-09-05 11:56:48 +0200727 /* We want the accept date presented to the next stream to be the one
728 * we have now, the handshake time to be null (since the next stream
729 * is not delayed by a handshake), and the idle time to count since
730 * right now.
731 */
732 sess->accept_date = date;
733 sess->tv_accept = now;
734 sess->t_handshake = 0;
735
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200736 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200737 if (h2_has_too_many_cs(h2c))
738 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200739 return h2s;
740
741 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200742 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200743 cs_free(cs);
744 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200745 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200746 h2s = NULL;
Willy Tarreau22de8d32018-09-05 19:55:58 +0200747 sess_log(sess);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200748 out:
749 return h2s;
750}
751
Willy Tarreaube5b7152017-09-25 16:25:39 +0200752/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
753 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
754 * the various settings codes.
755 */
756static int h2c_snd_settings(struct h2c *h2c)
757{
758 struct buffer *res;
759 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200760 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200761 int ret;
762
763 if (h2c_mux_busy(h2c, NULL)) {
764 h2c->flags |= H2_CF_DEM_MBUSY;
765 return 0;
766 }
767
Willy Tarreau44e973f2018-03-01 17:49:30 +0100768 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200769 if (!res) {
770 h2c->flags |= H2_CF_MUX_MALLOC;
771 h2c->flags |= H2_CF_DEM_MROOM;
772 return 0;
773 }
774
775 chunk_init(&buf, buf_data, sizeof(buf_data));
776 chunk_memcpy(&buf,
777 "\x00\x00\x00" /* length : 0 for now */
778 "\x04\x00" /* type : 4 (settings), flags : 0 */
779 "\x00\x00\x00\x00", /* stream ID : 0 */
780 9);
781
782 if (h2_settings_header_table_size != 4096) {
783 char str[6] = "\x00\x01"; /* header_table_size */
784
785 write_n32(str + 2, h2_settings_header_table_size);
786 chunk_memcat(&buf, str, 6);
787 }
788
789 if (h2_settings_initial_window_size != 65535) {
790 char str[6] = "\x00\x04"; /* initial_window_size */
791
792 write_n32(str + 2, h2_settings_initial_window_size);
793 chunk_memcat(&buf, str, 6);
794 }
795
796 if (h2_settings_max_concurrent_streams != 0) {
797 char str[6] = "\x00\x03"; /* max_concurrent_streams */
798
799 /* Note: 0 means "unlimited" for haproxy's config but not for
800 * the protocol, so never send this value!
801 */
802 write_n32(str + 2, h2_settings_max_concurrent_streams);
803 chunk_memcat(&buf, str, 6);
804 }
805
806 if (global.tune.bufsize != 16384) {
807 char str[6] = "\x00\x05"; /* max_frame_size */
808
809 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
810 * match bufsize - rewrite size, but at the moment it seems
811 * that clients don't take care of it.
812 */
813 write_n32(str + 2, global.tune.bufsize);
814 chunk_memcat(&buf, str, 6);
815 }
816
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200817 h2_set_frame_size(buf.area, buf.data - 9);
818 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200819 if (unlikely(ret <= 0)) {
820 if (!ret) {
821 h2c->flags |= H2_CF_MUX_MFULL;
822 h2c->flags |= H2_CF_DEM_MROOM;
823 return 0;
824 }
825 else {
826 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
827 return 0;
828 }
829 }
830 return ret;
831}
832
Willy Tarreau52eed752017-09-22 15:05:09 +0200833/* Try to receive a connection preface, then upon success try to send our
834 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
835 * missing data. It may return an error in h2c.
836 */
837static int h2c_frt_recv_preface(struct h2c *h2c)
838{
839 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200840 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200841
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200842 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200843
844 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +0200845 if (ret1 < 0)
846 sess_log(h2c->conn->owner);
847
Willy Tarreau52eed752017-09-22 15:05:09 +0200848 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
849 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
850 return 0;
851 }
852
Willy Tarreaube5b7152017-09-25 16:25:39 +0200853 ret2 = h2c_snd_settings(h2c);
854 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200855 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200856
Willy Tarreaube5b7152017-09-25 16:25:39 +0200857 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200858}
859
Willy Tarreau081d4722017-05-16 21:51:05 +0200860/* try to send a GOAWAY frame on the connection to report an error or a graceful
861 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
862 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
863 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
864 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
865 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
866 * on unrecoverable failure. It will not attempt to send one again in this last
867 * case so that it is safe to use h2c_error() to report such errors.
868 */
869static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
870{
871 struct buffer *res;
872 char str[17];
873 int ret;
874
875 if (h2c->flags & H2_CF_GOAWAY_FAILED)
876 return 1; // claim that it worked
877
878 if (h2c_mux_busy(h2c, h2s)) {
879 if (h2s)
880 h2s->flags |= H2_SF_BLK_MBUSY;
881 else
882 h2c->flags |= H2_CF_DEM_MBUSY;
883 return 0;
884 }
885
Willy Tarreau44e973f2018-03-01 17:49:30 +0100886 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +0200887 if (!res) {
888 h2c->flags |= H2_CF_MUX_MALLOC;
889 if (h2s)
890 h2s->flags |= H2_SF_BLK_MROOM;
891 else
892 h2c->flags |= H2_CF_DEM_MROOM;
893 return 0;
894 }
895
896 /* len: 8, type: 7, flags: none, sid: 0 */
897 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
898
899 if (h2c->last_sid < 0)
900 h2c->last_sid = h2c->max_id;
901
902 write_n32(str + 9, h2c->last_sid);
903 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200904 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +0200905 if (unlikely(ret <= 0)) {
906 if (!ret) {
907 h2c->flags |= H2_CF_MUX_MFULL;
908 if (h2s)
909 h2s->flags |= H2_SF_BLK_MROOM;
910 else
911 h2c->flags |= H2_CF_DEM_MROOM;
912 return 0;
913 }
914 else {
915 /* we cannot report this error using GOAWAY, so we mark
916 * it and claim a success.
917 */
918 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
919 h2c->flags |= H2_CF_GOAWAY_FAILED;
920 return 1;
921 }
922 }
923 h2c->flags |= H2_CF_GOAWAY_SENT;
924 return ret;
925}
926
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100927/* Try to send an RST_STREAM frame on the connection for the indicated stream
928 * during mux operations. This stream must be valid and cannot be closed
929 * already. h2s->id will be used for the stream ID and h2s->errcode will be
930 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
931 * not yet.
932 *
933 * Returns > 0 on success or zero if nothing was done. In case of lack of room
934 * to write the message, it subscribes the stream to future notifications.
935 */
936static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
937{
938 struct buffer *res;
939 char str[13];
940 int ret;
941
942 if (!h2s || h2s->st == H2_SS_CLOSED)
943 return 1;
944
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100945 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
946 * RST_STREAM in response to a RST_STREAM frame.
947 */
948 if (h2c->dft == H2_FT_RST_STREAM) {
949 ret = 1;
950 goto ignore;
951 }
952
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100953 if (h2c_mux_busy(h2c, h2s)) {
954 h2s->flags |= H2_SF_BLK_MBUSY;
955 return 0;
956 }
957
Willy Tarreau44e973f2018-03-01 17:49:30 +0100958 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100959 if (!res) {
960 h2c->flags |= H2_CF_MUX_MALLOC;
961 h2s->flags |= H2_SF_BLK_MROOM;
962 return 0;
963 }
964
965 /* len: 4, type: 3, flags: none */
966 memcpy(str, "\x00\x00\x04\x03\x00", 5);
967 write_n32(str + 5, h2s->id);
968 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200969 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100970
971 if (unlikely(ret <= 0)) {
972 if (!ret) {
973 h2c->flags |= H2_CF_MUX_MFULL;
974 h2s->flags |= H2_SF_BLK_MROOM;
975 return 0;
976 }
977 else {
978 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
979 return 0;
980 }
981 }
982
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100983 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100984 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100985 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100986 return ret;
987}
988
989/* Try to send an RST_STREAM frame on the connection for the stream being
990 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
991 * error code unless the stream's state already is IDLE or CLOSED in which
992 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
993 * it was not yet.
994 *
995 * Returns > 0 on success or zero if nothing was done. In case of lack of room
996 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200997 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100998 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200999 */
1000static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1001{
1002 struct buffer *res;
1003 char str[13];
1004 int ret;
1005
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001006 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1007 * RST_STREAM in response to a RST_STREAM frame.
1008 */
1009 if (h2c->dft == H2_FT_RST_STREAM) {
1010 ret = 1;
1011 goto ignore;
1012 }
1013
Willy Tarreau27a84c92017-10-17 08:10:17 +02001014 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001015 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001016 return 0;
1017 }
1018
Willy Tarreau44e973f2018-03-01 17:49:30 +01001019 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001020 if (!res) {
1021 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001022 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001023 return 0;
1024 }
1025
1026 /* len: 4, type: 3, flags: none */
1027 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001028
Willy Tarreau27a84c92017-10-17 08:10:17 +02001029 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +01001030 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +02001031 h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001032 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001033
Willy Tarreau27a84c92017-10-17 08:10:17 +02001034 if (unlikely(ret <= 0)) {
1035 if (!ret) {
1036 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001037 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001038 return 0;
1039 }
1040 else {
1041 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1042 return 0;
1043 }
1044 }
1045
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001046 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001047 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001048 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001049 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001050 }
1051
Willy Tarreau27a84c92017-10-17 08:10:17 +02001052 return ret;
1053}
1054
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001055/* try to send an empty DATA frame with the ES flag set to notify about the
1056 * end of stream and match a shutdown(write). If an ES was already sent as
1057 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1058 * on success or zero if nothing was done. In case of lack of room to write the
1059 * message, it subscribes the requesting stream to future notifications.
1060 */
1061static int h2_send_empty_data_es(struct h2s *h2s)
1062{
1063 struct h2c *h2c = h2s->h2c;
1064 struct buffer *res;
1065 char str[9];
1066 int ret;
1067
Willy Tarreau721c9742017-11-07 11:05:42 +01001068 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001069 return 1;
1070
1071 if (h2c_mux_busy(h2c, h2s)) {
1072 h2s->flags |= H2_SF_BLK_MBUSY;
1073 return 0;
1074 }
1075
Willy Tarreau44e973f2018-03-01 17:49:30 +01001076 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001077 if (!res) {
1078 h2c->flags |= H2_CF_MUX_MALLOC;
1079 h2s->flags |= H2_SF_BLK_MROOM;
1080 return 0;
1081 }
1082
1083 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1084 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1085 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001086 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001087 if (likely(ret > 0)) {
1088 h2s->flags |= H2_SF_ES_SENT;
1089 }
1090 else if (!ret) {
1091 h2c->flags |= H2_CF_MUX_MFULL;
1092 h2s->flags |= H2_SF_BLK_MROOM;
1093 return 0;
1094 }
1095 else {
1096 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1097 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001098 }
1099 return ret;
1100}
1101
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001102/* wake the streams attached to the connection, whose id is greater than <last>,
1103 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001104 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1105 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001106 */
1107static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1108{
1109 struct eb32_node *node;
1110 struct h2s *h2s;
1111
1112 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1113 flags |= CS_FL_ERROR;
1114
1115 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001116 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001117
1118 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1119 while (node) {
1120 h2s = container_of(node, struct h2s, by_id);
1121 if (h2s->id <= last)
1122 break;
1123 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001124
1125 if (!h2s->cs) {
1126 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001127 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001128 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001129 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001130
1131 h2s->cs->flags |= flags;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001132 if (h2s->recv_wait_list) {
1133 struct wait_list *sw = h2s->recv_wait_list;
1134 sw->wait_reason &= ~SUB_CAN_RECV;
1135 tasklet_wakeup(sw->task);
1136 h2s->recv_wait_list = NULL;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02001137 } else if (h2s->cs->data_cb->wake != NULL)
1138 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001139
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001140 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1141 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001142 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001143 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001144 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001145 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001146 }
1147}
1148
Willy Tarreau3421aba2017-07-27 15:41:03 +02001149/* Increase all streams' outgoing window size by the difference passed in
1150 * argument. This is needed upon receipt of the settings frame if the initial
1151 * window size is different. The difference may be negative and the resulting
1152 * window size as well, for the time it takes to receive some window updates.
1153 */
1154static void h2c_update_all_ws(struct h2c *h2c, int diff)
1155{
1156 struct h2s *h2s;
1157 struct eb32_node *node;
1158
1159 if (!diff)
1160 return;
1161
1162 node = eb32_first(&h2c->streams_by_id);
1163 while (node) {
1164 h2s = container_of(node, struct h2s, by_id);
1165 h2s->mws += diff;
1166 node = eb32_next(node);
1167 }
1168}
1169
1170/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1171 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1172 * return an error in h2c. Described in RFC7540#6.5.
1173 */
1174static int h2c_handle_settings(struct h2c *h2c)
1175{
1176 unsigned int offset;
1177 int error;
1178
1179 if (h2c->dff & H2_F_SETTINGS_ACK) {
1180 if (h2c->dfl) {
1181 error = H2_ERR_FRAME_SIZE_ERROR;
1182 goto fail;
1183 }
1184 return 1;
1185 }
1186
1187 if (h2c->dsi != 0) {
1188 error = H2_ERR_PROTOCOL_ERROR;
1189 goto fail;
1190 }
1191
1192 if (h2c->dfl % 6) {
1193 error = H2_ERR_FRAME_SIZE_ERROR;
1194 goto fail;
1195 }
1196
1197 /* that's the limit we can process */
1198 if (h2c->dfl > global.tune.bufsize) {
1199 error = H2_ERR_FRAME_SIZE_ERROR;
1200 goto fail;
1201 }
1202
1203 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001204 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001205 return 0;
1206
1207 /* parse the frame */
1208 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001209 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1210 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001211
1212 switch (type) {
1213 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1214 /* we need to update all existing streams with the
1215 * difference from the previous iws.
1216 */
1217 if (arg < 0) { // RFC7540#6.5.2
1218 error = H2_ERR_FLOW_CONTROL_ERROR;
1219 goto fail;
1220 }
1221 h2c_update_all_ws(h2c, arg - h2c->miw);
1222 h2c->miw = arg;
1223 break;
1224 case H2_SETTINGS_MAX_FRAME_SIZE:
1225 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1226 error = H2_ERR_PROTOCOL_ERROR;
1227 goto fail;
1228 }
1229 h2c->mfs = arg;
1230 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001231 case H2_SETTINGS_ENABLE_PUSH:
1232 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1233 error = H2_ERR_PROTOCOL_ERROR;
1234 goto fail;
1235 }
1236 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001237 }
1238 }
1239
1240 /* need to ACK this frame now */
1241 h2c->st0 = H2_CS_FRAME_A;
1242 return 1;
1243 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001244 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001245 h2c_error(h2c, error);
1246 return 0;
1247}
1248
1249/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1250 * success or one of the h2_status values.
1251 */
1252static int h2c_ack_settings(struct h2c *h2c)
1253{
1254 struct buffer *res;
1255 char str[9];
1256 int ret = -1;
1257
1258 if (h2c_mux_busy(h2c, NULL)) {
1259 h2c->flags |= H2_CF_DEM_MBUSY;
1260 return 0;
1261 }
1262
Willy Tarreau44e973f2018-03-01 17:49:30 +01001263 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001264 if (!res) {
1265 h2c->flags |= H2_CF_MUX_MALLOC;
1266 h2c->flags |= H2_CF_DEM_MROOM;
1267 return 0;
1268 }
1269
1270 memcpy(str,
1271 "\x00\x00\x00" /* length : 0 (no data) */
1272 "\x04" "\x01" /* type : 4, flags : ACK */
1273 "\x00\x00\x00\x00" /* stream ID */, 9);
1274
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001275 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001276 if (unlikely(ret <= 0)) {
1277 if (!ret) {
1278 h2c->flags |= H2_CF_MUX_MFULL;
1279 h2c->flags |= H2_CF_DEM_MROOM;
1280 return 0;
1281 }
1282 else {
1283 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1284 return 0;
1285 }
1286 }
1287 return ret;
1288}
1289
Willy Tarreaucf68c782017-10-10 17:11:41 +02001290/* processes a PING frame and schedules an ACK if needed. The caller must pass
1291 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1292 * missing data. It may return an error in h2c.
1293 */
1294static int h2c_handle_ping(struct h2c *h2c)
1295{
1296 /* frame length must be exactly 8 */
1297 if (h2c->dfl != 8) {
1298 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1299 return 0;
1300 }
1301
1302 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001303 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001304 h2c->st0 = H2_CS_FRAME_A;
1305 return 1;
1306}
1307
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001308/* Try to send a window update for stream id <sid> and value <increment>.
1309 * Returns > 0 on success or zero on missing room or failure. It may return an
1310 * error in h2c.
1311 */
1312static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1313{
1314 struct buffer *res;
1315 char str[13];
1316 int ret = -1;
1317
1318 if (h2c_mux_busy(h2c, NULL)) {
1319 h2c->flags |= H2_CF_DEM_MBUSY;
1320 return 0;
1321 }
1322
Willy Tarreau44e973f2018-03-01 17:49:30 +01001323 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001324 if (!res) {
1325 h2c->flags |= H2_CF_MUX_MALLOC;
1326 h2c->flags |= H2_CF_DEM_MROOM;
1327 return 0;
1328 }
1329
1330 /* length: 4, type: 8, flags: none */
1331 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1332 write_n32(str + 5, sid);
1333 write_n32(str + 9, increment);
1334
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001335 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001336
1337 if (unlikely(ret <= 0)) {
1338 if (!ret) {
1339 h2c->flags |= H2_CF_MUX_MFULL;
1340 h2c->flags |= H2_CF_DEM_MROOM;
1341 return 0;
1342 }
1343 else {
1344 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1345 return 0;
1346 }
1347 }
1348 return ret;
1349}
1350
1351/* try to send pending window update for the connection. It's safe to call it
1352 * with no pending updates. Returns > 0 on success or zero on missing room or
1353 * failure. It may return an error in h2c.
1354 */
1355static int h2c_send_conn_wu(struct h2c *h2c)
1356{
1357 int ret = 1;
1358
1359 if (h2c->rcvd_c <= 0)
1360 return 1;
1361
1362 /* send WU for the connection */
1363 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1364 if (ret > 0)
1365 h2c->rcvd_c = 0;
1366
1367 return ret;
1368}
1369
1370/* try to send pending window update for the current dmux stream. It's safe to
1371 * call it with no pending updates. Returns > 0 on success or zero on missing
1372 * room or failure. It may return an error in h2c.
1373 */
1374static int h2c_send_strm_wu(struct h2c *h2c)
1375{
1376 int ret = 1;
1377
1378 if (h2c->rcvd_s <= 0)
1379 return 1;
1380
1381 /* send WU for the stream */
1382 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1383 if (ret > 0)
1384 h2c->rcvd_s = 0;
1385
1386 return ret;
1387}
1388
Willy Tarreaucf68c782017-10-10 17:11:41 +02001389/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1390 * success, 0 on missing data or one of the h2_status values.
1391 */
1392static int h2c_ack_ping(struct h2c *h2c)
1393{
1394 struct buffer *res;
1395 char str[17];
1396 int ret = -1;
1397
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001398 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001399 return 0;
1400
1401 if (h2c_mux_busy(h2c, NULL)) {
1402 h2c->flags |= H2_CF_DEM_MBUSY;
1403 return 0;
1404 }
1405
Willy Tarreau44e973f2018-03-01 17:49:30 +01001406 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001407 if (!res) {
1408 h2c->flags |= H2_CF_MUX_MALLOC;
1409 h2c->flags |= H2_CF_DEM_MROOM;
1410 return 0;
1411 }
1412
1413 memcpy(str,
1414 "\x00\x00\x08" /* length : 8 (same payload) */
1415 "\x06" "\x01" /* type : 6, flags : ACK */
1416 "\x00\x00\x00\x00" /* stream ID */, 9);
1417
1418 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001419 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001420
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001421 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001422 if (unlikely(ret <= 0)) {
1423 if (!ret) {
1424 h2c->flags |= H2_CF_MUX_MFULL;
1425 h2c->flags |= H2_CF_DEM_MROOM;
1426 return 0;
1427 }
1428 else {
1429 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1430 return 0;
1431 }
1432 }
1433 return ret;
1434}
1435
Willy Tarreau26f95952017-07-27 17:18:30 +02001436/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1437 * Returns > 0 on success or zero on missing data. It may return an error in
1438 * h2c or h2s. Described in RFC7540#6.9.
1439 */
1440static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1441{
1442 int32_t inc;
1443 int error;
1444
1445 if (h2c->dfl != 4) {
1446 error = H2_ERR_FRAME_SIZE_ERROR;
1447 goto conn_err;
1448 }
1449
1450 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001451 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001452 return 0;
1453
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001454 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001455
1456 if (h2c->dsi != 0) {
1457 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001458
1459 /* it's not an error to receive WU on a closed stream */
1460 if (h2s->st == H2_SS_CLOSED)
1461 return 1;
1462
1463 if (!inc) {
1464 error = H2_ERR_PROTOCOL_ERROR;
1465 goto strm_err;
1466 }
1467
1468 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1469 error = H2_ERR_FLOW_CONTROL_ERROR;
1470 goto strm_err;
1471 }
1472
1473 h2s->mws += inc;
1474 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1475 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02001476 /* The task will be waken up later */
Willy Tarreau26f95952017-07-27 17:18:30 +02001477 }
1478 }
1479 else {
1480 /* connection window update */
1481 if (!inc) {
1482 error = H2_ERR_PROTOCOL_ERROR;
1483 goto conn_err;
1484 }
1485
1486 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1487 error = H2_ERR_FLOW_CONTROL_ERROR;
1488 goto conn_err;
1489 }
1490
1491 h2c->mws += inc;
1492 }
1493
1494 return 1;
1495
1496 conn_err:
1497 h2c_error(h2c, error);
1498 return 0;
1499
1500 strm_err:
1501 if (h2s) {
1502 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001503 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001504 }
1505 else
1506 h2c_error(h2c, error);
1507 return 0;
1508}
1509
Willy Tarreaue96b0922017-10-30 00:28:29 +01001510/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1511 * the last ID. Returns > 0 on success or zero on missing data. It may return
1512 * an error in h2c. Described in RFC7540#6.8.
1513 */
1514static int h2c_handle_goaway(struct h2c *h2c)
1515{
1516 int error;
1517 int last;
1518
1519 if (h2c->dsi != 0) {
1520 error = H2_ERR_PROTOCOL_ERROR;
1521 goto conn_err;
1522 }
1523
1524 if (h2c->dfl < 8) {
1525 error = H2_ERR_FRAME_SIZE_ERROR;
1526 goto conn_err;
1527 }
1528
1529 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001530 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001531 return 0;
1532
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001533 last = h2_get_n32(&h2c->dbuf, 0);
1534 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001535 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001536 if (h2c->last_sid < 0)
1537 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001538 return 1;
1539
1540 conn_err:
1541 h2c_error(h2c, error);
1542 return 0;
1543}
1544
Willy Tarreau92153fc2017-12-03 19:46:19 +01001545/* processes a PRIORITY frame, and either skips it or rejects if it is
1546 * invalid. Returns > 0 on success or zero on missing data. It may return
1547 * an error in h2c. Described in RFC7540#6.3.
1548 */
1549static int h2c_handle_priority(struct h2c *h2c)
1550{
1551 int error;
1552
1553 if (h2c->dsi == 0) {
1554 error = H2_ERR_PROTOCOL_ERROR;
1555 goto conn_err;
1556 }
1557
1558 if (h2c->dfl != 5) {
1559 error = H2_ERR_FRAME_SIZE_ERROR;
1560 goto conn_err;
1561 }
1562
1563 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001564 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001565 return 0;
1566
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001567 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001568 /* 7540#5.3 : can't depend on itself */
1569 error = H2_ERR_PROTOCOL_ERROR;
1570 goto conn_err;
1571 }
1572 return 1;
1573
1574 conn_err:
1575 h2c_error(h2c, error);
1576 return 0;
1577}
1578
Willy Tarreaucd234e92017-08-18 10:59:39 +02001579/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1580 * Returns > 0 on success or zero on missing data. It may return an error in
1581 * h2c. Described in RFC7540#6.4.
1582 */
1583static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1584{
1585 int error;
1586
1587 if (h2c->dsi == 0) {
1588 error = H2_ERR_PROTOCOL_ERROR;
1589 goto conn_err;
1590 }
1591
Willy Tarreaucd234e92017-08-18 10:59:39 +02001592 if (h2c->dfl != 4) {
1593 error = H2_ERR_FRAME_SIZE_ERROR;
1594 goto conn_err;
1595 }
1596
1597 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001598 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001599 return 0;
1600
1601 /* late RST, already handled */
1602 if (h2s->st == H2_SS_CLOSED)
1603 return 1;
1604
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001605 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001606 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001607
1608 if (h2s->cs) {
Willy Tarreau2c096c32018-09-12 09:45:54 +02001609 h2s->cs->flags |= CS_FL_REOS | CS_FL_ERROR;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001610 if (h2s->recv_wait_list) {
1611 struct wait_list *sw = h2s->recv_wait_list;
1612
1613 sw->wait_reason &= ~SUB_CAN_RECV;
1614 tasklet_wakeup(sw->task);
1615 h2s->recv_wait_list = NULL;
1616 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02001617 }
1618
1619 h2s->flags |= H2_SF_RST_RCVD;
1620 return 1;
1621
1622 conn_err:
1623 h2c_error(h2c, error);
1624 return 0;
1625}
1626
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001627/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1628 * It may return an error in h2c or h2s. The caller must consider that the
1629 * return value is the new h2s in case one was allocated (most common case).
1630 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001631 * errors here are reported as connection errors since it's impossible to
1632 * recover from such errors after the compression context has been altered.
1633 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001634static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001635{
1636 int error;
1637
1638 if (!h2c->dfl) {
1639 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001640 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001641 goto strm_err;
1642 }
1643
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001644 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001645 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001646
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001647 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001648 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001649
Willy Tarreauf2101912018-07-19 10:11:38 +02001650 if (h2c->flags & H2_CF_DEM_TOOMANY)
1651 return 0; // too many cs still present
1652
Willy Tarreau13278b42017-10-13 19:23:14 +02001653 /* now either the frame is complete or the buffer is complete */
1654 if (h2s->st != H2_SS_IDLE) {
1655 /* FIXME: stream already exists, this is only allowed for
1656 * trailers (not supported for now).
1657 */
1658 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001659 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001660 goto conn_err;
1661 }
1662 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1663 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1664 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001665 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001666 goto conn_err;
1667 }
1668
Willy Tarreau22de8d32018-09-05 19:55:58 +02001669 /* Note: we don't emit any other logs below because ff we return
1670 * positively from h2c_stream_new(), the stream will report the error,
1671 * and if we return in error, h2c_stream_new() will emit the error.
1672 */
Willy Tarreau13278b42017-10-13 19:23:14 +02001673 h2s = h2c_stream_new(h2c, h2c->dsi);
1674 if (!h2s) {
1675 error = H2_ERR_INTERNAL_ERROR;
1676 goto conn_err;
1677 }
1678
1679 h2s->st = H2_SS_OPEN;
1680 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1681 h2s->st = H2_SS_HREM;
1682 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001683 /* note: cs cannot be null for now (just created above) */
1684 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001685 }
1686
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001687 if (!h2_frt_decode_headers(h2s))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001688 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001689
Willy Tarreau8f650c32017-11-21 19:36:21 +01001690 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001691 return NULL;
Willy Tarreau8f650c32017-11-21 19:36:21 +01001692
Willy Tarreau721c9742017-11-07 11:05:42 +01001693 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001694 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001695 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001696 }
1697 else {
1698 /* update the max stream ID if the request is being processed */
1699 if (h2s->id > h2c->max_id)
1700 h2c->max_id = h2s->id;
1701 }
1702
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001703 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001704
1705 conn_err:
1706 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001707 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001708
1709 strm_err:
1710 if (h2s) {
1711 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001712 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001713 }
1714 else
1715 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001716 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001717}
1718
Willy Tarreau454f9052017-10-26 19:40:35 +02001719/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1720 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1721 */
1722static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1723{
1724 int error;
1725
1726 /* note that empty DATA frames are perfectly valid and sometimes used
1727 * to signal an end of stream (with the ES flag).
1728 */
1729
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001730 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001731 return 0; // empty buffer
1732
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001733 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001734 return 0; // incomplete frame
1735
1736 /* now either the frame is complete or the buffer is complete */
1737
1738 if (!h2c->dsi) {
1739 /* RFC7540#6.1 */
1740 error = H2_ERR_PROTOCOL_ERROR;
1741 goto conn_err;
1742 }
1743
1744 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1745 /* RFC7540#6.1 */
1746 error = H2_ERR_STREAM_CLOSED;
1747 goto strm_err;
1748 }
1749
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001750 if (!h2_frt_transfer_data(h2s))
1751 return 0;
1752
Willy Tarreau454f9052017-10-26 19:40:35 +02001753 /* call the upper layers to process the frame, then let the upper layer
1754 * notify the stream about any change.
1755 */
1756 if (!h2s->cs) {
1757 error = H2_ERR_STREAM_CLOSED;
1758 goto strm_err;
1759 }
1760
Willy Tarreau8f650c32017-11-21 19:36:21 +01001761 if (h2c->st0 >= H2_CS_ERROR)
1762 return 0;
1763
Willy Tarreau721c9742017-11-07 11:05:42 +01001764 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001765 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001766 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001767 }
1768
1769 /* check for completion : the callee will change this to FRAME_A or
1770 * FRAME_H once done.
1771 */
1772 if (h2c->st0 == H2_CS_FRAME_P)
1773 return 0;
1774
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001775
1776 /* last frame */
1777 if (h2c->dff & H2_F_DATA_END_STREAM) {
1778 h2s->st = H2_SS_HREM;
1779 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001780 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001781 }
1782
Willy Tarreau454f9052017-10-26 19:40:35 +02001783 return 1;
1784
1785 conn_err:
1786 h2c_error(h2c, error);
1787 return 0;
1788
1789 strm_err:
1790 if (h2s) {
1791 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001792 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001793 }
1794 else
1795 h2c_error(h2c, error);
1796 return 0;
1797}
1798
Willy Tarreaubc933932017-10-09 16:21:43 +02001799/* process Rx frames to be demultiplexed */
1800static void h2_process_demux(struct h2c *h2c)
1801{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001802 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001803
Willy Tarreau081d4722017-05-16 21:51:05 +02001804 if (h2c->st0 >= H2_CS_ERROR)
1805 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001806
1807 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1808 if (h2c->st0 == H2_CS_PREFACE) {
1809 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1810 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001811 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02001812 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001813 sess_log(h2c->conn->owner);
1814 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001815 goto fail;
1816 }
1817
1818 h2c->max_id = 0;
1819 h2c->st0 = H2_CS_SETTINGS1;
1820 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001821
1822 if (h2c->st0 == H2_CS_SETTINGS1) {
1823 struct h2_fh hdr;
1824
1825 /* ensure that what is pending is a valid SETTINGS frame
1826 * without an ACK.
1827 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001828 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001829 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001830 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001831 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001832 sess_log(h2c->conn->owner);
1833 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001834 goto fail;
1835 }
1836
1837 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1838 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1839 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1840 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001841 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001842 goto fail;
1843 }
1844
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001845 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001846 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1847 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1848 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001849 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001850 goto fail;
1851 }
1852
1853 /* that's OK, switch to FRAME_P to process it */
1854 h2c->dfl = hdr.len;
1855 h2c->dsi = hdr.sid;
1856 h2c->dft = hdr.ft;
1857 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001858 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001859 h2c->st0 = H2_CS_FRAME_P;
1860 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001861 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001862
1863 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001864 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001865 int ret = 0;
1866
1867 if (h2c->st0 >= H2_CS_ERROR)
1868 break;
1869
1870 if (h2c->st0 == H2_CS_FRAME_H) {
1871 struct h2_fh hdr;
1872
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001873 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02001874 break;
1875
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001876 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001877 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1878 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001879 if (!h2c->nb_streams) {
1880 /* only log if no other stream can report the error */
1881 sess_log(h2c->conn->owner);
1882 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001883 break;
1884 }
1885
1886 h2c->dfl = hdr.len;
1887 h2c->dsi = hdr.sid;
1888 h2c->dft = hdr.ft;
1889 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001890 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001891 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001892 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001893 }
1894
1895 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001896 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
1897
Olivier Houchard638b7992018-08-16 15:41:52 +02001898 if (tmp_h2s != h2s && h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001899 /* we may have to signal the upper layers */
1900 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001901 if (h2s->recv_wait_list) {
1902 h2s->recv_wait_list->wait_reason &= ~SUB_CAN_RECV;
1903 tasklet_wakeup(h2s->recv_wait_list->task);
1904 h2s->recv_wait_list = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001905 }
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001906 if (h2c->st0 >= H2_CS_ERROR)
1907 goto strm_err;
1908
1909 if (h2s->st >= H2_SS_ERROR) {
1910 /* stream error : send RST_STREAM */
1911 h2c->st0 = H2_CS_FRAME_E;
1912 }
1913 }
1914 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001915
Willy Tarreaud7901432017-12-29 11:34:40 +01001916 if (h2c->st0 == H2_CS_FRAME_E)
1917 goto strm_err;
1918
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001919 if (h2s->st == H2_SS_IDLE &&
1920 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1921 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1922 * this state MUST be treated as a connection error
1923 */
1924 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1925 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001926 if (!h2c->nb_streams) {
1927 /* only log if no other stream can report the error */
1928 sess_log(h2c->conn->owner);
1929 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001930 break;
1931 }
1932
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001933 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1934 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1935 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1936 * this state MUST be treated as a stream error
1937 */
1938 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1939 goto strm_err;
1940 }
1941
Willy Tarreauab837502017-12-27 15:07:30 +01001942 /* Below the management of frames received in closed state is a
1943 * bit hackish because the spec makes strong differences between
1944 * streams closed by receiving RST, sending RST, and seeing ES
1945 * in both directions. In addition to this, the creation of a
1946 * new stream reusing the identifier of a closed one will be
1947 * detected here. Given that we cannot keep track of all closed
1948 * streams forever, we consider that unknown closed streams were
1949 * closed on RST received, which allows us to respond with an
1950 * RST without breaking the connection (eg: to abort a transfer).
1951 * Some frames have to be silently ignored as well.
1952 */
1953 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1954 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1955 /* #5.1.1: The identifier of a newly
1956 * established stream MUST be numerically
1957 * greater than all streams that the initiating
1958 * endpoint has opened or reserved. This
1959 * governs streams that are opened using a
1960 * HEADERS frame and streams that are reserved
1961 * using PUSH_PROMISE. An endpoint that
1962 * receives an unexpected stream identifier
1963 * MUST respond with a connection error.
1964 */
1965 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1966 goto strm_err;
1967 }
1968
1969 if (h2s->flags & H2_SF_RST_RCVD) {
1970 /* RFC7540#5.1:closed: an endpoint that
1971 * receives any frame other than PRIORITY after
1972 * receiving a RST_STREAM MUST treat that as a
1973 * stream error of type STREAM_CLOSED.
1974 *
1975 * Note that old streams fall into this category
1976 * and will lead to an RST being sent.
1977 */
1978 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1979 h2c->st0 = H2_CS_FRAME_E;
1980 goto strm_err;
1981 }
1982
1983 /* RFC7540#5.1:closed: if this state is reached as a
1984 * result of sending a RST_STREAM frame, the peer that
1985 * receives the RST_STREAM might have already sent
1986 * frames on the stream that cannot be withdrawn. An
1987 * endpoint MUST ignore frames that it receives on
1988 * closed streams after it has sent a RST_STREAM
1989 * frame. An endpoint MAY choose to limit the period
1990 * over which it ignores frames and treat frames that
1991 * arrive after this time as being in error.
1992 */
1993 if (!(h2s->flags & H2_SF_RST_SENT)) {
1994 /* RFC7540#5.1:closed: any frame other than
1995 * PRIO/WU/RST in this state MUST be treated as
1996 * a connection error
1997 */
1998 if (h2c->dft != H2_FT_RST_STREAM &&
1999 h2c->dft != H2_FT_PRIORITY &&
2000 h2c->dft != H2_FT_WINDOW_UPDATE) {
2001 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2002 goto strm_err;
2003 }
2004 }
2005 }
2006
Willy Tarreauc0da1962017-10-30 18:38:00 +01002007#if 0
2008 // problem below: it is not possible to completely ignore such
2009 // streams as we need to maintain the compression state as well
2010 // and for this we need to completely process these frames (eg:
2011 // HEADERS frames) as well as counting DATA frames to emit
2012 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2013 // This is a typical case of layer violation where the
2014 // transported contents are critical to the connection's
2015 // validity and must be ignored at the same time :-(
2016
2017 /* graceful shutdown, ignore streams whose ID is higher than
2018 * the one advertised in GOAWAY. RFC7540#6.8.
2019 */
2020 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002021 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2022 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002023 h2c->dfl -= ret;
2024 ret = h2c->dfl == 0;
2025 goto strm_err;
2026 }
2027#endif
2028
Willy Tarreau7e98c052017-10-10 15:56:59 +02002029 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002030 case H2_FT_SETTINGS:
2031 if (h2c->st0 == H2_CS_FRAME_P)
2032 ret = h2c_handle_settings(h2c);
2033
2034 if (h2c->st0 == H2_CS_FRAME_A)
2035 ret = h2c_ack_settings(h2c);
2036 break;
2037
Willy Tarreaucf68c782017-10-10 17:11:41 +02002038 case H2_FT_PING:
2039 if (h2c->st0 == H2_CS_FRAME_P)
2040 ret = h2c_handle_ping(h2c);
2041
2042 if (h2c->st0 == H2_CS_FRAME_A)
2043 ret = h2c_ack_ping(h2c);
2044 break;
2045
Willy Tarreau26f95952017-07-27 17:18:30 +02002046 case H2_FT_WINDOW_UPDATE:
2047 if (h2c->st0 == H2_CS_FRAME_P)
2048 ret = h2c_handle_window_update(h2c, h2s);
2049 break;
2050
Willy Tarreau61290ec2017-10-17 08:19:21 +02002051 case H2_FT_CONTINUATION:
2052 /* we currently don't support CONTINUATION frames since
2053 * we have nowhere to store the partial HEADERS frame.
2054 * Let's abort the stream on an INTERNAL_ERROR here.
2055 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002056 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02002057 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002058 h2c->st0 = H2_CS_FRAME_E;
2059 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02002060 break;
2061
Willy Tarreau13278b42017-10-13 19:23:14 +02002062 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002063 if (h2c->st0 == H2_CS_FRAME_P) {
2064 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
2065 if (tmp_h2s) {
2066 h2s = tmp_h2s;
2067 ret = 1;
2068 }
2069 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002070 break;
2071
Willy Tarreau454f9052017-10-26 19:40:35 +02002072 case H2_FT_DATA:
2073 if (h2c->st0 == H2_CS_FRAME_P)
2074 ret = h2c_frt_handle_data(h2c, h2s);
2075
2076 if (h2c->st0 == H2_CS_FRAME_A)
2077 ret = h2c_send_strm_wu(h2c);
2078 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002079
Willy Tarreau92153fc2017-12-03 19:46:19 +01002080 case H2_FT_PRIORITY:
2081 if (h2c->st0 == H2_CS_FRAME_P)
2082 ret = h2c_handle_priority(h2c);
2083 break;
2084
Willy Tarreaucd234e92017-08-18 10:59:39 +02002085 case H2_FT_RST_STREAM:
2086 if (h2c->st0 == H2_CS_FRAME_P)
2087 ret = h2c_handle_rst_stream(h2c, h2s);
2088 break;
2089
Willy Tarreaue96b0922017-10-30 00:28:29 +01002090 case H2_FT_GOAWAY:
2091 if (h2c->st0 == H2_CS_FRAME_P)
2092 ret = h2c_handle_goaway(h2c);
2093 break;
2094
Willy Tarreau1c661982017-10-30 13:52:01 +01002095 case H2_FT_PUSH_PROMISE:
2096 /* not permitted here, RFC7540#5.1 */
2097 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002098 if (!h2c->nb_streams) {
2099 /* only log if no other stream can report the error */
2100 sess_log(h2c->conn->owner);
2101 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002102 break;
2103
2104 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002105 default:
2106 /* drop frames that we ignore. They may be larger than
2107 * the buffer so we drain all of their contents until
2108 * we reach the end.
2109 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002110 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2111 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002112 h2c->dfl -= ret;
2113 ret = h2c->dfl == 0;
2114 }
2115
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002116 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002117 /* We may have to send an RST if not done yet */
2118 if (h2s->st == H2_SS_ERROR)
2119 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002120
Willy Tarreaua20a5192017-12-27 11:02:06 +01002121 if (h2c->st0 == H2_CS_FRAME_E)
2122 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002123
Willy Tarreau7e98c052017-10-10 15:56:59 +02002124 /* error or missing data condition met above ? */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002125 if (ret <= 0) {
2126 h2s = NULL;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002127 break;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002128 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002129
2130 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002131 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002132 h2c->st0 = H2_CS_FRAME_H;
2133 }
2134 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002135
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002136 if (h2c->rcvd_c > 0 &&
2137 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2138 h2c_send_conn_wu(h2c);
2139
Willy Tarreau52eed752017-09-22 15:05:09 +02002140 fail:
2141 /* we can go here on missing data, blocked response or error */
Olivier Houchard638b7992018-08-16 15:41:52 +02002142 if (h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002143 /* we may have to signal the upper layers */
2144 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002145 if (h2s->recv_wait_list) {
2146 h2s->recv_wait_list->wait_reason &= ~SUB_CAN_RECV;
2147 tasklet_wakeup(h2s->recv_wait_list->task);
2148 h2s->recv_wait_list = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002149 }
2150 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002151 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02002152}
2153
2154/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2155 * the end.
2156 */
2157static int h2_process_mux(struct h2c *h2c)
2158{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002159 struct h2s *h2s;
2160 struct wait_list *sw, *sw_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002161
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002162 /* start by sending possibly pending window updates */
2163 if (h2c->rcvd_c > 0 &&
2164 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2165 h2c_send_conn_wu(h2c) < 0)
2166 goto fail;
2167
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002168 /* First we always process the flow control list because the streams
2169 * waiting there were already elected for immediate emission but were
2170 * blocked just on this.
2171 */
2172
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002173 list_for_each_entry_safe(sw, sw_back, &h2c->fctl_list, list) {
2174 h2s = sw->handle;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002175 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2176 h2c->st0 >= H2_CS_ERROR)
2177 break;
2178
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002179 /* If the tasklet was added to finish shutr/shutw, just wake the task */
2180 if ((long)(h2s) & 3) {
2181 sw->wait_reason &= ~SUB_CAN_SEND;
2182 LIST_DEL(&sw->list);
2183 LIST_INIT(&sw->list);
2184 tasklet_wakeup(sw->task);
2185 } else if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
2186 h2s->flags &= ~H2_SF_BLK_ANY;
2187 LIST_DEL(&sw->list);
2188 LIST_INIT(&sw->list);
2189 sw->wait_reason &= ~SUB_CAN_SEND;
2190 tasklet_wakeup(sw->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002191 }
2192 }
2193
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002194 list_for_each_entry_safe(sw, sw_back, &h2c->send_list, list) {
2195 h2s = sw->handle;
2196
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002197 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2198 break;
2199
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002200 /* If the tasklet was added to finish shutr/shutw, just wake the task */
2201 if ((long)(h2s) & 3) {
2202 sw->wait_reason &= ~SUB_CAN_SEND;
2203 LIST_DEL(&sw->list);
2204 LIST_INIT(&sw->list);
2205 tasklet_wakeup(sw->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002206 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002207 else if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
2208 h2s->flags &= ~H2_SF_BLK_ANY;
2209
2210 LIST_DEL(&sw->list);
2211 LIST_INIT(&sw->list);
2212 sw->wait_reason &= ~SUB_CAN_SEND;
2213 tasklet_wakeup(sw->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002214 }
2215 }
2216
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002217 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002218 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002219 if (h2c->st0 == H2_CS_ERROR) {
2220 if (h2c->max_id >= 0) {
2221 h2c_send_goaway_error(h2c, NULL);
2222 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2223 return 0;
2224 }
2225
2226 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2227 }
2228 return 1;
2229 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002230 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002231}
2232
Willy Tarreau62f52692017-10-08 23:01:42 +02002233
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002234/* Attempt to read data, and subscribe if none available */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002235static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002236{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002237 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002238 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002239 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002240 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002241
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002242 if (h2c->wait_list.wait_reason & SUB_CAN_RECV)
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002243 return 0;
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002244
Willy Tarreau315d8072017-12-10 22:17:57 +01002245 if (!h2_recv_allowed(h2c))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002246 return 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002247
Willy Tarreau44e973f2018-03-01 17:49:30 +01002248 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002249 if (!buf) {
2250 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002251 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002252 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002253
Olivier Houchard7505f942018-08-21 18:10:44 +02002254 do {
2255 max = buf->size - b_data(buf);
2256 if (max)
2257 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2258 else
2259 ret = 0;
2260 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002261
Olivier Houchard7505f942018-08-21 18:10:44 +02002262 if (h2_recv_allowed(h2c)) {
2263 conn_xprt_want_recv(conn);
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002264 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h2c->wait_list);
Olivier Houchard7505f942018-08-21 18:10:44 +02002265 }
Olivier Houcharda1411e62018-08-17 18:42:48 +02002266 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002267 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002268 return 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002269 }
2270
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002271 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002272 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002273 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002274}
2275
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002276/* Try to send data if possible */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002277static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002278{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002279 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002280 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002281 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002282
2283 if (conn->flags & CO_FL_ERROR)
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002284 return 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002285
Olivier Houchard7505f942018-08-21 18:10:44 +02002286
Willy Tarreaua2af5122017-10-09 11:56:46 +02002287 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2288 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002289 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002290 }
2291
Willy Tarreaubc933932017-10-09 16:21:43 +02002292 /* This loop is quite simple : it tries to fill as much as it can from
2293 * pending streams into the existing buffer until it's reportedly full
2294 * or the end of send requests is reached. Then it tries to send this
2295 * buffer's contents out, marks it not full if at least one byte could
2296 * be sent, and tries again.
2297 *
2298 * The snd_buf() function normally takes a "flags" argument which may
2299 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2300 * data immediately comes and CO_SFL_STREAMER to indicate that the
2301 * connection is streaming lots of data (used to increase TLS record
2302 * size at the expense of latency). The former can be sent any time
2303 * there's a buffer full flag, as it indicates at least one stream
2304 * attempted to send and failed so there are pending data. An
2305 * alternative would be to set it as long as there's an active stream
2306 * but that would be problematic for ACKs until we have an absolute
2307 * guarantee that all waiters have at least one byte to send. The
2308 * latter should possibly not be set for now.
2309 */
2310
2311 done = 0;
2312 while (!done) {
2313 unsigned int flags = 0;
2314
2315 /* fill as much as we can into the current buffer */
2316 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2317 done = h2_process_mux(h2c);
2318
2319 if (conn->flags & CO_FL_ERROR)
2320 break;
2321
2322 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2323 flags |= CO_SFL_MSG_MORE;
2324
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002325 if (b_data(&h2c->mbuf)) {
2326 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002327 if (!ret)
2328 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002329 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002330 b_del(&h2c->mbuf, ret);
2331 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002332 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002333
2334 /* wrote at least one byte, the buffer is not full anymore */
2335 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2336 }
2337
Willy Tarreaua2af5122017-10-09 11:56:46 +02002338 if (conn->flags & CO_FL_SOCK_WR_SH) {
2339 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002340 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002341 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002342 /* We're not full anymore, so we can wake any task that are waiting
2343 * for us.
2344 */
2345 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002346 while (!LIST_ISEMPTY(&h2c->send_list)) {
2347 struct wait_list *sw = LIST_ELEM(h2c->send_list.n,
Olivier Houchard6ff20392018-07-17 18:46:31 +02002348 struct wait_list *, list);
2349 LIST_DEL(&sw->list);
2350 LIST_INIT(&sw->list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002351 sw->wait_reason &= ~SUB_CAN_SEND;
2352 tasklet_wakeup(sw->task);
2353 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002354 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002355 /* We're done, no more to send */
2356 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002357 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002358schedule:
2359 if (LIST_ISEMPTY(&h2c->wait_list.list))
2360 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_list);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002361 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002362}
2363
2364static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2365{
2366 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002367 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002368
2369 if (!(h2c->wait_list.wait_reason & SUB_CAN_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002370 ret = h2_send(h2c);
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002371 if (!(h2c->wait_list.wait_reason & SUB_CAN_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002372 ret |= h2_recv(h2c);
2373 if (ret)
2374 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002375 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002376}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002377
Willy Tarreau62f52692017-10-08 23:01:42 +02002378/* callback called on any event by the connection handler.
2379 * It applies changes and returns zero, or < 0 if it wants immediate
2380 * destruction of the connection (which normally doesn not happen in h2).
2381 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002382static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002383{
Olivier Houchard7505f942018-08-21 18:10:44 +02002384 struct connection *conn = h2c->conn;
Willy Tarreau8ec14062017-12-30 18:08:13 +01002385 struct session *sess = conn->owner;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002386
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002387 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002388 h2_process_demux(h2c);
2389
2390 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002391 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002392
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002393 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002394 h2c->flags &= ~H2_CF_DEM_DFULL;
2395 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002396 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002397
Willy Tarreau8ec14062017-12-30 18:08:13 +01002398 if (sess && unlikely(sess->fe->state == PR_STSTOPPED)) {
2399 /* frontend is stopping, reload likely in progress, let's try
2400 * to announce a graceful shutdown if not yet done. We don't
2401 * care if it fails, it will be tried again later.
2402 */
2403 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2404 if (h2c->last_sid < 0)
2405 h2c->last_sid = (1U << 31) - 1;
2406 h2c_send_goaway_error(h2c, NULL);
2407 }
2408 }
2409
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002410 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002411 * If we received early data, and the handshake is done, wake
2412 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002413 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002414 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2415 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2416 struct eb32_node *node;
2417 struct h2s *h2s;
2418
2419 h2c->flags |= H2_CF_WAIT_FOR_HS;
2420 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2421
2422 while (node) {
2423 h2s = container_of(node, struct h2s, by_id);
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002424 if ((h2s->cs->flags & CS_FL_WAIT_FOR_HS) &&
2425 h2s->recv_wait_list) {
2426 struct wait_list *sw = h2s->recv_wait_list;
2427 sw->wait_reason &= ~SUB_CAN_RECV;
2428 tasklet_wakeup(sw->task);
2429 h2s->recv_wait_list = NULL;
2430 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002431 node = eb32_next(node);
2432 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002433 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002434
Willy Tarreau26bd7612017-10-09 16:47:04 +02002435 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002436 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2437 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2438 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002439 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002440
2441 if (eb_is_empty(&h2c->streams_by_id)) {
2442 /* no more stream, kill the connection now */
2443 h2_release(conn);
2444 return -1;
2445 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002446 }
2447
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002448 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002449 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002450
2451 /* stop being notified of incoming data if we can't process them */
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002452 if (!h2_recv_allowed(h2c))
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002453 __conn_xprt_stop_recv(conn);
Olivier Houchard7505f942018-08-21 18:10:44 +02002454 else
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002455 __conn_xprt_want_recv(conn);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002456
2457 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002458 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002459 h2c->st0 != H2_CS_ERROR2 && !(h2c->flags & H2_CF_GOAWAY_FAILED) &&
Willy Tarreau51606832017-10-17 15:30:07 +02002460 (h2c->st0 == H2_CS_ERROR ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002461 b_data(&h2c->mbuf) ||
Willy Tarreau51606832017-10-17 15:30:07 +02002462 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2463 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002464 __conn_xprt_want_send(conn);
2465 }
2466 else {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002467 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002468 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002469 }
2470
Willy Tarreau3f133572017-10-31 19:21:06 +01002471 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002472 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002473 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002474 task_queue(h2c->task);
2475 }
2476 else
2477 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002478 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002479
Olivier Houchard7505f942018-08-21 18:10:44 +02002480 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002481 return 0;
2482}
2483
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002484static int h2_wake(struct connection *conn)
2485{
2486 struct h2c *h2c = conn->mux_ctx;
2487
2488 return (h2_process(h2c));
2489}
2490
2491
Willy Tarreauea392822017-10-31 10:02:25 +01002492/* Connection timeout management. The principle is that if there's no receipt
2493 * nor sending for a certain amount of time, the connection is closed. If the
2494 * MUX buffer still has lying data or is not allocatable, the connection is
2495 * immediately killed. If it's allocatable and empty, we attempt to send a
2496 * GOAWAY frame.
2497 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002498static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002499{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002500 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002501 int expired = tick_is_expired(t->expire, now_ms);
2502
Willy Tarreau0975f112018-03-29 15:22:59 +02002503 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002504 return t;
2505
Willy Tarreau0975f112018-03-29 15:22:59 +02002506 task_delete(t);
2507 task_free(t);
2508
2509 if (!h2c) {
2510 /* resources were already deleted */
2511 return NULL;
2512 }
2513
2514 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002515 h2c_error(h2c, H2_ERR_NO_ERROR);
2516 h2_wake_some_streams(h2c, 0, 0);
2517
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002518 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002519 /* don't even try to send a GOAWAY, the buffer is stuck */
2520 h2c->flags |= H2_CF_GOAWAY_FAILED;
2521 }
2522
2523 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002524 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002525 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2526 h2c->flags |= H2_CF_GOAWAY_FAILED;
2527
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002528 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2529 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002530 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002531 b_del(&h2c->mbuf, ret);
2532 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002533 }
2534 }
Willy Tarreauea392822017-10-31 10:02:25 +01002535
Willy Tarreau0975f112018-03-29 15:22:59 +02002536 /* either we can release everything now or it will be done later once
2537 * the last stream closes.
2538 */
2539 if (eb_is_empty(&h2c->streams_by_id))
2540 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002541
Willy Tarreauea392822017-10-31 10:02:25 +01002542 return NULL;
2543}
2544
2545
Willy Tarreau62f52692017-10-08 23:01:42 +02002546/*******************************************/
2547/* functions below are used by the streams */
2548/*******************************************/
2549
2550/*
2551 * Attach a new stream to a connection
2552 * (Used for outgoing connections)
2553 */
2554static struct conn_stream *h2_attach(struct connection *conn)
2555{
2556 return NULL;
2557}
2558
2559/* callback used to update the mux's polling flags after changing a cs' status.
2560 * The caller (cs_update_mux_polling) will take care of propagating any changes
2561 * to the transport layer.
2562 */
2563static void h2_update_poll(struct conn_stream *cs)
2564{
Willy Tarreau1d393222017-10-17 10:26:19 +02002565 struct h2s *h2s = cs->ctx;
2566
2567 if (!h2s)
2568 return;
2569
Willy Tarreaud7739c82017-10-30 15:38:23 +01002570 /* we may unblock a blocked read */
2571
Willy Tarreau315d8072017-12-10 22:17:57 +01002572 if (cs->flags & CS_FL_DATA_RD_ENA) {
2573 /* the stream indicates it's willing to read */
Willy Tarreaud7739c82017-10-30 15:38:23 +01002574 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreaud13bf272017-12-14 10:34:52 +01002575 if (h2s->h2c->dsi == h2s->id) {
Willy Tarreau315d8072017-12-10 22:17:57 +01002576 conn_xprt_want_recv(cs->conn);
Olivier Houchard61d322f2018-09-24 18:02:03 +02002577 tasklet_wakeup(h2s->h2c->wait_list.task);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002578 conn_xprt_want_send(cs->conn);
2579 }
Willy Tarreaud7739c82017-10-30 15:38:23 +01002580 }
2581
Willy Tarreau1d393222017-10-17 10:26:19 +02002582 /* Note: the stream and stream-int code doesn't allow us to perform a
2583 * synchronous send() here unfortunately, because this code is called
2584 * as si_update() from the process_stream() context. This means that
2585 * we have to queue the current cs and defer its processing after the
2586 * connection's cs list is processed anyway.
2587 */
2588
2589 if (cs->flags & CS_FL_DATA_WR_ENA) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002590 if (!b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2591 conn_xprt_want_send(cs->conn);
2592 tasklet_wakeup(h2s->h2c->wait_list.task);
Willy Tarreau1d393222017-10-17 10:26:19 +02002593 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002594 /* We don't support unsubscribing from here, it shouldn't be a problem */
Willy Tarreau1d393222017-10-17 10:26:19 +02002595
2596 /* this can happen from within si_chk_snd() */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002597 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreau1d393222017-10-17 10:26:19 +02002598 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002599}
2600
2601/*
2602 * Detach the stream from the connection and possibly release the connection.
2603 */
2604static void h2_detach(struct conn_stream *cs)
2605{
Willy Tarreau60935142017-10-16 18:11:19 +02002606 struct h2s *h2s = cs->ctx;
2607 struct h2c *h2c;
2608
2609 cs->ctx = NULL;
2610 if (!h2s)
2611 return;
2612
2613 h2c = h2s->h2c;
Olivier Houchard70d0d182018-09-12 17:56:08 +02002614 /* If the stream we're detaching waited for more data, unsubscribe it now */
2615 if (h2s->recv_wait_list && !((long)h2s->recv_wait_list->handle & 3))
2616 h2s->recv_wait_list = NULL;
Willy Tarreau60935142017-10-16 18:11:19 +02002617 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002618 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002619 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2620 !h2_has_too_many_cs(h2c)) {
2621 h2c->flags &= ~H2_CF_DEM_TOOMANY;
2622 if (h2_recv_allowed(h2c)) {
2623 __conn_xprt_want_recv(h2c->conn);
Olivier Houchard61d322f2018-09-24 18:02:03 +02002624 tasklet_wakeup(h2c->wait_list.task);
Willy Tarreauf2101912018-07-19 10:11:38 +02002625 conn_xprt_want_send(h2c->conn);
2626 }
2627 }
Willy Tarreau60935142017-10-16 18:11:19 +02002628
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002629 /* this stream may be blocked waiting for some data to leave (possibly
2630 * an ES or RST frame), so orphan it in this case.
2631 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002632 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002633 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002634 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002635 return;
2636
Willy Tarreau45f752e2017-10-30 15:44:59 +01002637 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2638 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2639 /* unblock the connection if it was blocked on this
2640 * stream.
2641 */
2642 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2643 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2644 conn_xprt_want_recv(cs->conn);
Olivier Houchard61d322f2018-09-24 18:02:03 +02002645 tasklet_wakeup(h2c->wait_list.task);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002646 conn_xprt_want_send(cs->conn);
2647 }
2648
Willy Tarreau71049cc2018-03-28 13:56:39 +02002649 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002650
Willy Tarreaue323f342018-03-28 13:51:45 +02002651 /* We don't want to close right now unless we're removing the
2652 * last stream, and either the connection is in error, or it
2653 * reached the ID already specified in a GOAWAY frame received
2654 * or sent (as seen by last_sid >= 0).
2655 */
2656 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2657 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002658 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Willy Tarreaue323f342018-03-28 13:51:45 +02002659 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002660 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002661 (conn_xprt_read0_pending(h2c->conn) ||
2662 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2663 /* no more stream will come, kill it now */
2664 h2_release(h2c->conn);
2665 }
2666 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002667 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002668 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2669 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002670 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002671 else
2672 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002673 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002674}
2675
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002676static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002677{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002678 struct h2c *h2c = h2s->h2c;
2679 struct wait_list *sw = &h2s->wait_list;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002680
Willy Tarreau721c9742017-11-07 11:05:42 +01002681 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002682 return;
2683
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002684 /* if no outgoing data was seen on this stream, it means it was
2685 * closed with a "tcp-request content" rule that is normally
2686 * used to kill the connection ASAP (eg: limit abuse). In this
2687 * case we send a goaway to close the connection.
2688 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002689 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002690 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002691 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002692
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002693 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2694 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002695 h2c_send_goaway_error(h2c, h2s) <= 0)
2696 return;
2697 if (b_data(&h2c->mbuf) && !(h2c->conn->flags & CO_FL_XPRT_WR_ENA))
2698 conn_xprt_want_send(h2c->conn);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002699
Willy Tarreau00dd0782018-03-01 16:31:34 +01002700 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002701
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002702 return;
2703add_to_list:
2704 if (LIST_ISEMPTY(&sw->list)) {
2705 sw->wait_reason |= SUB_CAN_SEND;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002706 if (h2s->flags & H2_SF_BLK_MFCTL)
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002707 LIST_ADDQ(&h2c->fctl_list, &sw->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002708 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002709 LIST_ADDQ(&h2c->send_list, &sw->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002710 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002711 /* Let the handler know we want shutr */
2712 sw->handle = (void *)((long)sw->handle | 1);
2713
Willy Tarreau62f52692017-10-08 23:01:42 +02002714}
2715
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002716static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002717{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002718 struct h2c *h2c = h2s->h2c;
2719 struct wait_list *sw = &h2s->wait_list;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002720
Willy Tarreau721c9742017-11-07 11:05:42 +01002721 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002722 return;
2723
Willy Tarreau67434202017-11-06 20:20:51 +01002724 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002725 /* we can cleanly close using an empty data frame only after headers */
2726
2727 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2728 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002729 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002730
2731 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002732 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002733 else
2734 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002735 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002736 /* if no outgoing data was seen on this stream, it means it was
2737 * closed with a "tcp-request content" rule that is normally
2738 * used to kill the connection ASAP (eg: limit abuse). In this
2739 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002740 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002741 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002742 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002743 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002744
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002745 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2746 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002747 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002748 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002749
Willy Tarreau00dd0782018-03-01 16:31:34 +01002750 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002751 }
2752
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002753 if (b_data(&h2s->h2c->mbuf) && !(h2c->conn->flags & CO_FL_XPRT_WR_ENA))
2754 conn_xprt_want_send(h2c->conn);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002755
2756 add_to_list:
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002757 sw = &h2s->wait_list;
2758
2759 if (LIST_ISEMPTY(&sw->list)) {
2760 sw->wait_reason |= SUB_CAN_SEND;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002761 if (h2s->flags & H2_SF_BLK_MFCTL)
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002762 LIST_ADDQ(&h2s->h2c->fctl_list, &sw->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002763 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002764 LIST_ADDQ(&h2s->h2c->send_list, &sw->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002765 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002766 /* let the handler know we want to shutr */
2767 sw->handle = (void *)((long)(sw->handle) | 2);
2768}
2769
2770static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
2771{
2772 struct h2s *h2s = ctx;
2773 long reason = (long)h2s->wait_list.handle;
2774
2775 if (reason & 1)
2776 h2_do_shutr(h2s);
2777 if (reason & 2)
2778 h2_do_shutw(h2s);
2779
2780 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02002781}
2782
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002783static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2784{
2785 struct h2s *h2s = cs->ctx;
2786
2787 if (!mode)
2788 return;
2789
2790 h2_do_shutr(h2s);
2791}
2792
2793static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2794{
2795 struct h2s *h2s = cs->ctx;
2796
2797 h2_do_shutw(h2s);
2798}
2799
Willy Tarreau13278b42017-10-13 19:23:14 +02002800/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2801 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2802 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002803 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002804 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002805static int h2_frt_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002806{
2807 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002808 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02002809 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002810 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02002811 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002812 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01002813 struct buffer *csbuf;
Willy Tarreau13278b42017-10-13 19:23:14 +02002814 int flen = h2c->dfl;
2815 int outlen = 0;
2816 int wrap;
2817 int try;
2818
2819 if (!h2c->dfl) {
2820 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002821 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002822 return 0;
2823 }
2824
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002825 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01002826 return 0; // incomplete input frame
2827
Willy Tarreau13278b42017-10-13 19:23:14 +02002828 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002829 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02002830 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002831 copy = alloc_trash_chunk();
2832 if (!copy) {
2833 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2834 goto fail;
2835 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002836 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
2837 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
2838 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02002839 }
2840
2841 /* The padlen is the first byte before data, and the padding appears
2842 * after data. padlen+data+padding are included in flen.
2843 */
2844 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002845 h2c->dpl = *hdrs;
2846 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002847 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2848 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002849 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002850 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002851 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002852 hdrs += 1; // skip Pad Length
2853 }
2854
2855 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2856 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002857 if (read_n32(hdrs) == h2s->id) {
2858 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2859 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002860 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002861 }
2862
Willy Tarreau13278b42017-10-13 19:23:14 +02002863 hdrs += 5; // stream dep = 4, weight = 1
2864 flen -= 5;
2865 }
2866
2867 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2868 * don't support this for now and can't even decompress so we have to
2869 * break the connection.
2870 */
2871 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2872 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002873 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002874 }
2875
Olivier Houchard638b7992018-08-16 15:41:52 +02002876 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01002877 if (!csbuf) {
2878 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002879 goto fail;
2880 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002881
Willy Tarreau937f7602018-02-26 15:22:17 +01002882 /* we can't retry a failed decompression operation so we must be very
2883 * careful not to take any risks. In practice the output buffer is
2884 * always empty except maybe for trailers, in which case we simply have
2885 * to wait for the upper layer to finish consuming what is available.
2886 */
2887 if (b_data(csbuf))
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002888 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002889
Willy Tarreau937f7602018-02-26 15:22:17 +01002890 csbuf->head = 0;
2891 try = b_size(csbuf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002892
2893 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2894 sizeof(list)/sizeof(list[0]), tmp);
2895 if (outlen < 0) {
2896 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2897 goto fail;
2898 }
2899
2900 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002901 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreau937f7602018-02-26 15:22:17 +01002902 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002903
2904 if (outlen < 0) {
2905 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2906 goto fail;
2907 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002908
Willy Tarreau174b06a2018-04-25 18:13:58 +02002909 if (msgf & H2_MSGF_BODY) {
2910 /* a payload is present */
2911 if (msgf & H2_MSGF_BODY_CL)
2912 h2s->flags |= H2_SF_DATA_CLEN;
2913 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
2914 h2s->flags |= H2_SF_DATA_CHNK;
2915 }
2916
Willy Tarreau13278b42017-10-13 19:23:14 +02002917 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002918 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02002919 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01002920 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02002921
Willy Tarreau39d68502018-03-02 12:26:37 +01002922 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002923 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002924 h2s->cs->flags |= CS_FL_REOS;
2925 }
Willy Tarreau937f7602018-02-26 15:22:17 +01002926
Willy Tarreau68dd9852017-07-03 14:44:26 +02002927 leave:
2928 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002929 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002930 fail:
2931 outlen = 0;
2932 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002933}
2934
Willy Tarreau454f9052017-10-26 19:40:35 +02002935/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2936 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2937 * in use, a new chunk is emitted for each frame. This is supposed to fit
2938 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2939 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2940 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2941 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002942 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2943 * checked to know if some data remain pending (an empty DATA frame can return
2944 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2945 * connection errors in h2c->errcode. The caller must already have checked the
2946 * frame header and ensured that the frame was complete or the buffer full. It
2947 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002948 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002949static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02002950{
2951 struct h2c *h2c = h2s->h2c;
2952 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002953 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002954 unsigned int chklen = 0;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002955 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02002956
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002957 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002958
2959 /* The padlen is the first byte before data, and the padding appears
2960 * after data. padlen+data+padding are included in flen.
2961 */
Willy Tarreau79127812017-12-03 21:06:59 +01002962 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002963 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002964 return 0;
2965
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002966 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002967 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002968 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2969 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002970 return 0;
2971 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002972
2973 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002974 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002975 h2c->dfl--;
2976 h2c->rcvd_c++; h2c->rcvd_s++;
2977 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002978 }
2979
Olivier Houchard638b7992018-08-16 15:41:52 +02002980 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01002981 if (!csbuf) {
2982 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002983 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002984 }
2985
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002986 flen = h2c->dfl - h2c->dpl;
2987 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002988 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002989
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002990 if (flen > b_data(&h2c->dbuf)) {
2991 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002992 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01002993 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002994 }
2995
2996 if (unlikely(b_space_wraps(csbuf))) {
2997 /* it doesn't fit and the buffer is fragmented,
2998 * so let's defragment it and try again.
2999 */
3000 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003001 }
3002
Willy Tarreaueba10f22018-04-25 20:44:22 +02003003 /* chunked-encoding requires more room */
3004 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003005 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003006 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3007 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3008 (chklen < 1048576) ? 4 : 8;
3009 chklen += 4; // CRLF, CRLF
3010 }
3011
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003012 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003013 if (flen + chklen > b_room(csbuf)) {
3014 if (chklen >= b_room(csbuf)) {
3015 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003016 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003017 }
3018 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003019 }
3020
3021 if (h2s->flags & H2_SF_DATA_CHNK) {
3022 /* emit the chunk size */
3023 unsigned int chksz = flen;
3024 char str[10];
3025 char *beg;
3026
3027 beg = str + sizeof(str);
3028 *--beg = '\n';
3029 *--beg = '\r';
3030 do {
3031 *--beg = hextab[chksz & 0xF];
3032 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003033 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003034 }
3035
Willy Tarreau454f9052017-10-26 19:40:35 +02003036 /* Block1 is the length of the first block before the buffer wraps,
3037 * block2 is the optional second block to reach the end of the frame.
3038 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003039 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003040 if (block1 > flen)
3041 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003042 block2 = flen - block1;
3043
3044 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003045 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003046
3047 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003048 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003049
Willy Tarreaueba10f22018-04-25 20:44:22 +02003050 if (h2s->flags & H2_SF_DATA_CHNK) {
3051 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003052 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003053 }
3054
Willy Tarreau454f9052017-10-26 19:40:35 +02003055 /* now mark the input data as consumed (will be deleted from the buffer
3056 * by the caller when seeing FRAME_A after sending the window update).
3057 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003058 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003059 h2c->dfl -= flen;
3060 h2c->rcvd_c += flen;
3061 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3062
3063 if (h2c->dfl > h2c->dpl) {
3064 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003065 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003066 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003067 }
3068
Willy Tarreau4a28da12018-01-04 14:41:00 +01003069 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003070 /* here we're done with the frame, all the payload (except padding) was
3071 * transferred.
3072 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003073
3074 if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) {
3075 /* emit the trailing 0 CRLF CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003076 if (b_room(csbuf) < 5) {
3077 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003078 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003079 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003080 chklen += 5;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003081 b_putblk(csbuf, "0\r\n\r\n", 5);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003082 }
3083
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003084 h2c->rcvd_c += h2c->dpl;
3085 h2c->rcvd_s += h2c->dpl;
3086 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003087 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3088
Willy Tarreau39d68502018-03-02 12:26:37 +01003089 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003090 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003091 h2s->cs->flags |= CS_FL_REOS;
3092 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003093
Willy Tarreau454b57b2018-02-26 15:50:05 +01003094 return flen + chklen;
3095 fail:
3096 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003097}
3098
Willy Tarreau5dd17352018-06-14 13:33:30 +02003099/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3100 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3101 * number of bytes sent. The caller must check the stream's status to detect
3102 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003103 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003104static 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 +02003105{
3106 struct http_hdr list[MAX_HTTP_HDR];
3107 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003108 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003109 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003110 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003111 int es_now = 0;
3112 int ret = 0;
3113 int hdr;
3114
3115 if (h2c_mux_busy(h2c, h2s)) {
3116 h2s->flags |= H2_SF_BLK_MBUSY;
3117 return 0;
3118 }
3119
Willy Tarreau44e973f2018-03-01 17:49:30 +01003120 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003121 h2c->flags |= H2_CF_MUX_MALLOC;
3122 h2s->flags |= H2_SF_BLK_MROOM;
3123 return 0;
3124 }
3125
3126 /* First, try to parse the H1 response and index it into <list>.
3127 * NOTE! Since it comes from haproxy, we *know* that a response header
3128 * block does not wrap and we can safely read it this way without
3129 * having to realign the buffer.
3130 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003131 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003132 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003133 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003134 /* incomplete or invalid response, this is abnormal coming from
3135 * haproxy and may only result in a bad errorfile or bad Lua code
3136 * so that won't be fixed, raise an error now.
3137 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003138 * FIXME: we should instead add the ability to only return a
3139 * 502 bad gateway. But in theory this is not supposed to
3140 * happen.
3141 */
3142 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3143 ret = 0;
3144 goto end;
3145 }
3146
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003147 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003148
3149 /* certain statuses have no body or an empty one, regardless of
3150 * what the headers say.
3151 */
3152 if (sl.st.status >= 100 && sl.st.status < 200) {
3153 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3154 h1m->curr_len = h1m->body_len = 0;
3155 }
3156 else if (sl.st.status == 204 || sl.st.status == 304) {
3157 /* no contents, claim c-len is present and set to zero */
3158 h1m->flags &= ~H1_MF_CHNK;
3159 h1m->flags |= H1_MF_CLEN;
3160 h1m->curr_len = h1m->body_len = 0;
3161 }
3162
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003163 chunk_reset(&outbuf);
3164
3165 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003166 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003167 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003168 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003169
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003170 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003171 break;
3172 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003173 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003174 }
3175
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003176 if (outbuf.size < 9)
3177 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003178
3179 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003180 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3181 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3182 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003183
3184 /* encode status, which necessarily is the first one */
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003185 if (outbuf.data < outbuf.size && h2s->status == 200)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003186 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003187 else if (outbuf.data < outbuf.size && h2s->status == 304)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003188 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003189 else if (unlikely(list[0].v.len != 3)) {
3190 /* this is an unparsable response */
3191 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3192 ret = 0;
3193 goto end;
3194 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003195 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003196 /* basic encoding of the status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003197 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3198 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3199 outbuf.area[outbuf.data++] = list[0].v.ptr[0];
3200 outbuf.area[outbuf.data++] = list[0].v.ptr[1];
3201 outbuf.area[outbuf.data++] = list[0].v.ptr[2];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003202 }
3203 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003204 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003205 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003206 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003207 }
3208
3209 /* encode all headers, stop at empty name */
3210 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003211 /* these ones do not exist in H2 and must be dropped. */
3212 if (isteq(list[hdr].n, ist("connection")) ||
3213 isteq(list[hdr].n, ist("proxy-connection")) ||
3214 isteq(list[hdr].n, ist("keep-alive")) ||
3215 isteq(list[hdr].n, ist("upgrade")) ||
3216 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003217 continue;
3218
3219 if (isteq(list[hdr].n, ist("")))
3220 break; // end
3221
3222 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3223 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003224 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003225 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003226 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003227 }
3228 }
3229
3230 /* we may need to add END_STREAM */
3231 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3232 es_now = 1;
3233
3234 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003235 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003236
3237 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003238 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003239
3240 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003241 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003242
3243 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003244 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003245 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003246
3247 /* for now we don't implemented CONTINUATION, so we wait for a
3248 * body or directly end in TRL2.
3249 */
3250 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003251 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003252 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003253
Willy Tarreau801250e2018-09-11 11:45:04 +02003254 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003255 h2s->flags |= H2_SF_ES_SENT;
3256 if (h2s->st == H2_SS_OPEN)
3257 h2s->st = H2_SS_HLOC;
3258 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003259 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003260 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003261 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003262 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003263 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003264 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003265 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003266 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003267 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003268
3269 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003270
3271 end:
3272 //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));
3273 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003274 full:
3275 h1m_init_res(h1m);
3276 h1m->err_pos = -1; // don't care about errors on the response path
3277 h2c->flags |= H2_CF_MUX_MFULL;
3278 h2s->flags |= H2_SF_BLK_MROOM;
3279 ret = 0;
3280 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003281}
3282
Willy Tarreau5dd17352018-06-14 13:33:30 +02003283/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3284 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3285 * the number of bytes sent. The caller must check the stream's status to
3286 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003287 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003288static 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 +02003289{
3290 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003291 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003292 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003293 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003294 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003295 int es_now = 0;
3296 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003297 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003298 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003299
3300 if (h2c_mux_busy(h2c, h2s)) {
3301 h2s->flags |= H2_SF_BLK_MBUSY;
3302 goto end;
3303 }
3304
Willy Tarreau44e973f2018-03-01 17:49:30 +01003305 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003306 h2c->flags |= H2_CF_MUX_MALLOC;
3307 h2s->flags |= H2_SF_BLK_MROOM;
3308 goto end;
3309 }
3310
3311 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003312 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003313 goto end;
3314
3315 chunk_reset(&outbuf);
3316
3317 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003318 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003319 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003320 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003321
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003322 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003323 break;
3324 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003325 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003326 }
3327
3328 if (outbuf.size < 9) {
3329 h2c->flags |= H2_CF_MUX_MFULL;
3330 h2s->flags |= H2_SF_BLK_MROOM;
3331 goto end;
3332 }
3333
3334 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003335 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3336 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3337 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003338
3339 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3340 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003341 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003342 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003343 break;
3344 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003345 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003346 if ((long long)size > h1m->curr_len)
3347 size = h1m->curr_len;
3348 break;
3349 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003350 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003351 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003352 if (!ret)
3353 goto end;
3354
3355 if (ret < 0) {
3356 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003357 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003358 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3359 goto end;
3360 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003361 max -= ret;
3362 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003363 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003364 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003365 }
3366
Willy Tarreau801250e2018-09-11 11:45:04 +02003367 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003368 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003369 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003370 if (!ret)
3371 goto end;
3372
3373 if (ret < 0) {
3374 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003375 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003376 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3377 goto end;
3378 }
3379
3380 size = chunk;
3381 h1m->curr_len = chunk;
3382 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003383 max -= ret;
3384 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003385 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003386 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003387 if (!size)
3388 goto send_empty;
3389 }
3390
3391 /* in MSG_DATA state, continue below */
3392 size = h1m->curr_len;
3393 break;
3394 }
3395
3396 /* we have in <size> the exact number of bytes we need to copy from
3397 * the H1 buffer. We need to check this against the connection's and
3398 * the stream's send windows, and to ensure that this fits in the max
3399 * frame size and in the buffer's available space minus 9 bytes (for
3400 * the frame header). The connection's flow control is applied last so
3401 * that we can use a separate list of streams which are immediately
3402 * unblocked on window opening. Note: we don't implement padding.
3403 */
3404
Willy Tarreau5dd17352018-06-14 13:33:30 +02003405 if (size > max)
3406 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003407
3408 if (size > h2s->mws)
3409 size = h2s->mws;
3410
3411 if (size <= 0) {
3412 h2s->flags |= H2_SF_BLK_SFCTL;
3413 goto end;
3414 }
3415
3416 if (h2c->mfs && size > h2c->mfs)
3417 size = h2c->mfs;
3418
3419 if (size + 9 > outbuf.size) {
3420 /* we have an opportunity for enlarging the too small
3421 * available space, let's try.
3422 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003423 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003424 goto realign_again;
3425 size = outbuf.size - 9;
3426 }
3427
3428 if (size <= 0) {
3429 h2c->flags |= H2_CF_MUX_MFULL;
3430 h2s->flags |= H2_SF_BLK_MROOM;
3431 goto end;
3432 }
3433
3434 if (size > h2c->mws)
3435 size = h2c->mws;
3436
3437 if (size <= 0) {
3438 h2s->flags |= H2_SF_BLK_MFCTL;
3439 goto end;
3440 }
3441
3442 /* copy whatever we can */
3443 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003444 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003445 if (ret == 1)
3446 len2 = 0;
3447
3448 if (!ret || len1 + len2 < size) {
3449 /* FIXME: must normally never happen */
3450 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3451 goto end;
3452 }
3453
3454 /* limit len1/len2 to size */
3455 if (len1 + len2 > size) {
3456 int sub = len1 + len2 - size;
3457
3458 if (len2 > sub)
3459 len2 -= sub;
3460 else {
3461 sub -= len2;
3462 len2 = 0;
3463 len1 -= sub;
3464 }
3465 }
3466
3467 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003468 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003469 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003470 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003471
3472 send_empty:
3473 /* we may need to add END_STREAM */
3474 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3475 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003476 *
3477 * FIXME: what we do here is not correct because we send end_stream
3478 * before knowing if we'll have to send a HEADERS frame for the
3479 * trailers. More importantly we're not consuming the trailing CRLF
3480 * after the end of trailers, so it will be left to the caller to
3481 * eat it. The right way to do it would be to measure trailers here
3482 * and to send ES only if there are no trailers.
3483 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003484 */
3485 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02003486 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003487 es_now = 1;
3488
3489 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003490 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003491
3492 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003493 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003494
3495 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003496 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003497
3498 /* consume incoming H1 response */
3499 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003500 max -= size;
3501 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003502 total += size;
3503 h1m->curr_len -= size;
3504 h2s->mws -= size;
3505 h2c->mws -= size;
3506
3507 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02003508 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003509 goto new_frame;
3510 }
3511 }
3512
3513 if (es_now) {
3514 if (h2s->st == H2_SS_OPEN)
3515 h2s->st = H2_SS_HLOC;
3516 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003517 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003518
Willy Tarreau35a62702018-02-27 15:37:25 +01003519 if (!(h1m->flags & H1_MF_CHNK)) {
3520 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003521 total += max;
3522 ofs += max;
3523 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003524
Willy Tarreau801250e2018-09-11 11:45:04 +02003525 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003526 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003527
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003528 h2s->flags |= H2_SF_ES_SENT;
3529 }
3530
3531 end:
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02003532 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 +02003533 return total;
3534}
3535
Olivier Houchard6ff20392018-07-17 18:46:31 +02003536/* Called from the upper layer, to subscribe to events, such as being able to send */
3537static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
3538{
3539 struct wait_list *sw;
3540 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003541 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003542
3543 switch (event_type) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003544 case SUB_CAN_RECV:
3545 sw = param;
3546 if (!(sw->wait_reason & SUB_CAN_RECV)) {
3547 sw->wait_reason |= SUB_CAN_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003548 sw->handle = h2s;
Olivier Houchardaf4021e2018-08-09 13:06:55 +02003549 h2s->recv_wait_list = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003550 }
3551 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003552 case SUB_CAN_SEND:
3553 sw = param;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003554 if (!(sw->wait_reason & SUB_CAN_SEND)) {
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003555 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003556 sw->handle = h2s;
3557 if (h2s->flags & H2_SF_BLK_MFCTL)
3558 LIST_ADDQ(&h2c->fctl_list, &sw->list);
3559 else
3560 LIST_ADDQ(&h2c->send_list, &sw->list);
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003561 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02003562 return 0;
3563 default:
3564 break;
3565 }
3566 return -1;
3567
3568
3569}
3570
Olivier Houchard511efea2018-08-16 15:30:32 +02003571/* Called from the upper layer, to receive data */
3572static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3573{
Olivier Houchard638b7992018-08-16 15:41:52 +02003574 struct h2s *h2s = cs->ctx;
Olivier Houchard511efea2018-08-16 15:30:32 +02003575 size_t ret = 0;
3576
3577 /* transfer possibly pending data to the upper layer */
Olivier Houchard638b7992018-08-16 15:41:52 +02003578 ret = b_xfer(buf, &h2s->rxbuf, count);
Olivier Houchard511efea2018-08-16 15:30:32 +02003579
Olivier Houchard638b7992018-08-16 15:41:52 +02003580 if (b_data(&h2s->rxbuf))
Olivier Houchard511efea2018-08-16 15:30:32 +02003581 cs->flags |= CS_FL_RCV_MORE;
3582 else {
3583 cs->flags &= ~CS_FL_RCV_MORE;
3584 if (cs->flags & CS_FL_REOS)
3585 cs->flags |= CS_FL_EOS;
Olivier Houchard638b7992018-08-16 15:41:52 +02003586 if (b_size(&h2s->rxbuf)) {
3587 b_free(&h2s->rxbuf);
3588 offer_buffers(NULL, tasks_run_queue);
3589 }
Olivier Houchard511efea2018-08-16 15:30:32 +02003590 }
3591
3592 return ret;
3593}
3594
Willy Tarreau62f52692017-10-08 23:01:42 +02003595/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003596static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02003597{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003598 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003599 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003600 size_t ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003601
Willy Tarreau0bad0432018-06-14 16:54:01 +02003602 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01003603 h2s->flags |= H2_SF_OUTGOING_DATA;
3604
Willy Tarreaua40704a2018-09-11 13:52:04 +02003605 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02003606 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003607 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003608 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02003609 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003610 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003611 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02003612 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003613 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003614 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003615
Willy Tarreau5dd17352018-06-14 13:33:30 +02003616 if (unlikely((int)ret <= 0)) {
3617 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003618 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3619 break;
3620 }
Willy Tarreau35a62702018-02-27 15:37:25 +01003621 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02003622 total += count;
3623 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003624 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003625 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003626 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003627 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003628 cs->flags |= CS_FL_ERROR;
3629 break;
3630 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02003631
3632 total += ret;
3633 count -= ret;
3634
3635 if (h2s->st >= H2_SS_ERROR)
3636 break;
3637
3638 if (h2s->flags & H2_SF_BLK_ANY)
3639 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003640 }
3641
Willy Tarreau00610962018-07-19 10:58:28 +02003642 if (h2s->st >= H2_SS_ERROR) {
3643 /* trim any possibly pending data after we close (extra CR-LF,
3644 * unprocessed trailers, abnormal extra data, ...)
3645 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003646 total += count;
3647 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02003648 }
3649
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003650 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003651 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003652 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003653 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003654 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003655 }
3656
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003657 b_del(buf, total);
Olivier Houchard7505f942018-08-21 18:10:44 +02003658 if (total > 0) {
Olivier Houchardfab7c7e2018-08-21 16:36:10 +02003659 conn_xprt_want_send(h2s->h2c->conn);
Olivier Houchard7505f942018-08-21 18:10:44 +02003660 if (!(h2s->h2c->wait_list.wait_reason & SUB_CAN_SEND))
3661 tasklet_wakeup(h2s->h2c->wait_list.task);
3662 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003663 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003664}
3665
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003666/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02003667static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003668{
3669 struct h2c *h2c = conn->mux_ctx;
3670 struct h2s *h2s;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003671 struct wait_list *sw;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003672 struct eb32_node *node;
3673 int fctl_cnt = 0;
3674 int send_cnt = 0;
3675 int tree_cnt = 0;
3676 int orph_cnt = 0;
3677
3678 if (!h2c)
3679 return;
3680
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003681 list_for_each_entry(sw, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003682 fctl_cnt++;
3683
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003684 list_for_each_entry(sw, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003685 send_cnt++;
3686
3687 node = eb32_first(&h2c->streams_by_id);
3688 while (node) {
3689 h2s = container_of(node, struct h2s, by_id);
3690 tree_cnt++;
3691 if (!h2s->cs)
3692 orph_cnt++;
3693 node = eb32_next(node);
3694 }
3695
Willy Tarreau616ac812018-07-24 14:12:42 +02003696 chunk_appendf(msg, " st0=%d err=%d maxid=%d lastid=%d flg=0x%08x nbst=%u nbcs=%u"
3697 " fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
3698 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
3699 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
3700 (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
3701 (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003702}
Willy Tarreau62f52692017-10-08 23:01:42 +02003703
3704/*******************************************************/
3705/* functions below are dedicated to the config parsers */
3706/*******************************************************/
3707
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003708/* config parser for global "tune.h2.header-table-size" */
3709static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3710 struct proxy *defpx, const char *file, int line,
3711 char **err)
3712{
3713 if (too_many_args(1, args, err, NULL))
3714 return -1;
3715
3716 h2_settings_header_table_size = atoi(args[1]);
3717 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3718 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3719 return -1;
3720 }
3721 return 0;
3722}
Willy Tarreau62f52692017-10-08 23:01:42 +02003723
Willy Tarreaue6baec02017-07-27 11:45:11 +02003724/* config parser for global "tune.h2.initial-window-size" */
3725static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3726 struct proxy *defpx, const char *file, int line,
3727 char **err)
3728{
3729 if (too_many_args(1, args, err, NULL))
3730 return -1;
3731
3732 h2_settings_initial_window_size = atoi(args[1]);
3733 if (h2_settings_initial_window_size < 0) {
3734 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3735 return -1;
3736 }
3737 return 0;
3738}
3739
Willy Tarreau5242ef82017-07-27 11:47:28 +02003740/* config parser for global "tune.h2.max-concurrent-streams" */
3741static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3742 struct proxy *defpx, const char *file, int line,
3743 char **err)
3744{
3745 if (too_many_args(1, args, err, NULL))
3746 return -1;
3747
3748 h2_settings_max_concurrent_streams = atoi(args[1]);
3749 if (h2_settings_max_concurrent_streams < 0) {
3750 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3751 return -1;
3752 }
3753 return 0;
3754}
3755
Willy Tarreau62f52692017-10-08 23:01:42 +02003756
3757/****************************************/
3758/* MUX initialization and instanciation */
3759/***************************************/
3760
3761/* The mux operations */
3762const struct mux_ops h2_ops = {
3763 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003764 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02003765 .update_poll = h2_update_poll,
Willy Tarreau62f52692017-10-08 23:01:42 +02003766 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02003767 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02003768 .subscribe = h2_subscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02003769 .attach = h2_attach,
3770 .detach = h2_detach,
3771 .shutr = h2_shutr,
3772 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003773 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003774 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003775 .name = "H2",
3776};
3777
Christopher Faulet32f61c02018-04-10 14:33:41 +02003778/* PROTO selection : this mux registers PROTO token "h2" */
3779static struct mux_proto_list mux_proto_h2 =
3780 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02003781
3782/* config keyword parsers */
3783static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003784 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003785 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003786 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003787 { 0, NULL, NULL }
3788}};
3789
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003790static void __h2_deinit(void)
3791{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003792 pool_destroy(pool_head_h2s);
3793 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003794}
3795
Willy Tarreau62f52692017-10-08 23:01:42 +02003796__attribute__((constructor))
3797static void __h2_init(void)
3798{
Christopher Faulet32f61c02018-04-10 14:33:41 +02003799 register_mux_proto(&mux_proto_h2);
Willy Tarreau62f52692017-10-08 23:01:42 +02003800 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003801 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003802 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3803 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003804}