blob: 616038118eef8dc3ae0b5f1308413589ee5dd06f [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 Houchard7505f942018-08-21 18:10:44 +0200287 if (h2_recv(h2c))
288 h2_process(h2c);
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200289 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200290 return 1;
291 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200292
Willy Tarreau44e973f2018-03-01 17:49:30 +0100293 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
294 h2c->flags &= ~H2_CF_MUX_MALLOC;
295 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
296 conn_xprt_want_send(h2c->conn);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200297
298 if (h2c->flags & H2_CF_DEM_MROOM) {
299 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200300 if (h2_recv_allowed(h2c)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200301 conn_xprt_want_recv(h2c->conn);
Olivier Houchard7505f942018-08-21 18:10:44 +0200302 if (h2_recv(h2c))
303 h2_process(h2c);
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200304 }
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200305 }
Willy Tarreau14398122017-09-22 14:26:04 +0200306 return 1;
307 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100308
309 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
310 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200311 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100312 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200313 if (h2_recv_allowed(h2c)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100314 conn_xprt_want_recv(h2c->conn);
Olivier Houchard7505f942018-08-21 18:10:44 +0200315 if (h2_recv(h2c))
316 h2_process(h2c);
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200317 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100318 return 1;
319 }
320
Willy Tarreau14398122017-09-22 14:26:04 +0200321 return 0;
322}
323
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200324static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200325{
326 struct buffer *buf = NULL;
327
Willy Tarreau44e973f2018-03-01 17:49:30 +0100328 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
329 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
330 h2c->buf_wait.target = h2c;
331 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100332 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100333 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100334 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200335 __conn_xprt_stop_recv(h2c->conn);
336 }
337 return buf;
338}
339
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200340static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200341{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200342 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100343 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200344 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200345 }
346}
347
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200348
Willy Tarreau62f52692017-10-08 23:01:42 +0200349/*****************************************************************/
350/* functions below are dedicated to the mux setup and management */
351/*****************************************************************/
352
Willy Tarreau32218eb2017-09-22 08:07:25 +0200353/* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */
354static int h2c_frt_init(struct connection *conn)
355{
356 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100357 struct task *t = NULL;
358 struct session *sess = conn->owner;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200359
Willy Tarreaubafbe012017-11-24 17:34:44 +0100360 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200361 if (!h2c)
362 goto fail;
363
Willy Tarreau3f133572017-10-31 19:21:06 +0100364
Willy Tarreau599391a2017-11-24 10:16:00 +0100365 h2c->shut_timeout = h2c->timeout = sess->fe->timeout.client;
366 if (tick_isset(sess->fe->timeout.clientfin))
367 h2c->shut_timeout = sess->fe->timeout.clientfin;
368
Willy Tarreau33400292017-11-05 11:23:40 +0100369 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100370 if (tick_isset(h2c->timeout)) {
371 t = task_new(tid_bit);
372 if (!t)
373 goto fail;
374
375 h2c->task = t;
376 t->process = h2_timeout_task;
377 t->context = h2c;
378 t->expire = tick_add(now_ms, h2c->timeout);
379 }
Willy Tarreauea392822017-10-31 10:02:25 +0100380
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200381 h2c->wait_list.task = tasklet_new();
382 if (!h2c->wait_list.task)
383 goto fail;
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200384 h2c->wait_list.task->process = h2_io_cb;
385 h2c->wait_list.task->context = h2c;
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +0200386 h2c->wait_list.wait_reason = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200387
Willy Tarreau32218eb2017-09-22 08:07:25 +0200388 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
389 if (!h2c->ddht)
390 goto fail;
391
392 /* Initialise the context. */
393 h2c->st0 = H2_CS_PREFACE;
394 h2c->conn = conn;
395 h2c->max_id = -1;
396 h2c->errcode = H2_ERR_NO_ERROR;
397 h2c->flags = H2_CF_NONE;
398 h2c->rcvd_c = 0;
399 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100400 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200401 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200402
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200403 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200404 h2c->dsi = -1;
405 h2c->msi = -1;
406 h2c->last_sid = -1;
407
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200408 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200409 h2c->miw = 65535; /* mux initial window size */
410 h2c->mws = 65535; /* mux window size */
411 h2c->mfs = 16384; /* initial max frame size */
412 h2c->streams_by_id = EB_ROOT_UNIQUE;
413 LIST_INIT(&h2c->send_list);
414 LIST_INIT(&h2c->fctl_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100415 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200416 conn->mux_ctx = h2c;
417
Willy Tarreau3f133572017-10-31 19:21:06 +0100418 if (t)
419 task_queue(t);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200420 conn_xprt_want_recv(conn);
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200421 LIST_INIT(&h2c->wait_list.list);
Willy Tarreauea392822017-10-31 10:02:25 +0100422
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200423 /* Try to read, if nothing is available yet we'll just subscribe */
Olivier Houchard7505f942018-08-21 18:10:44 +0200424 if (h2_recv(h2c))
425 h2_process(h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200426 return 0;
427 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100428 if (t)
429 task_free(t);
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200430 if (h2c->wait_list.task)
431 tasklet_free(h2c->wait_list.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100432 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200433 return -1;
434}
435
Willy Tarreau62f52692017-10-08 23:01:42 +0200436/* Initialize the mux once it's attached. For outgoing connections, the context
437 * is already initialized before installing the mux, so we detect incoming
438 * connections from the fact that the context is still NULL. Returns < 0 on
439 * error.
440 */
Willy Tarreau175a2bb2018-09-12 12:02:05 +0200441static int h2_init(struct connection *conn, struct proxy *prx)
Willy Tarreau62f52692017-10-08 23:01:42 +0200442{
443 if (conn->mux_ctx) {
444 /* we don't support outgoing connections for now */
445 return -1;
446 }
447
Willy Tarreau32218eb2017-09-22 08:07:25 +0200448 return h2c_frt_init(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200449}
450
Willy Tarreau2373acc2017-10-12 17:35:14 +0200451/* returns the stream associated with id <id> or NULL if not found */
452static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
453{
454 struct eb32_node *node;
455
Willy Tarreau2a856182017-05-16 15:20:39 +0200456 if (id > h2c->max_id)
457 return (struct h2s *)h2_idle_stream;
458
Willy Tarreau2373acc2017-10-12 17:35:14 +0200459 node = eb32_lookup(&h2c->streams_by_id, id);
460 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200461 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200462
463 return container_of(node, struct h2s, by_id);
464}
465
Willy Tarreau62f52692017-10-08 23:01:42 +0200466/* release function for a connection. This one should be called to free all
467 * resources allocated to the mux.
468 */
469static void h2_release(struct connection *conn)
470{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200471 struct h2c *h2c = conn->mux_ctx;
472
473 LIST_DEL(&conn->list);
474
475 if (h2c) {
476 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200477
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100478 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100479 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100480 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200481
Willy Tarreau44e973f2018-03-01 17:49:30 +0100482 h2_release_buf(h2c, &h2c->dbuf);
483 h2_release_buf(h2c, &h2c->mbuf);
484
Willy Tarreauea392822017-10-31 10:02:25 +0100485 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200486 h2c->task->context = NULL;
487 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100488 h2c->task = NULL;
489 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200490 if (h2c->wait_list.task)
491 tasklet_free(h2c->wait_list.task);
Olivier Houchardc2aa7112018-09-11 18:27:21 +0200492 LIST_DEL(&h2c->wait_list.list);
493 LIST_INIT(&h2c->wait_list.list);
Olivier Houchard251f6a22018-09-12 17:52:46 +0200494 while (!LIST_ISEMPTY(&h2c->send_list)) {
495 struct wait_list *sw = LIST_ELEM(h2c->send_list.n,
496 struct wait_list *, list);
497 LIST_DEL(&sw->list);
498 LIST_INIT(&sw->list);
499 }
500 while (!LIST_ISEMPTY(&h2c->fctl_list)) {
501 struct wait_list *sw = LIST_ELEM(h2c->fctl_list.n,
502 struct wait_list *, list);
503 LIST_DEL(&sw->list);
504 LIST_INIT(&sw->list);
505 }
506
Willy Tarreauea392822017-10-31 10:02:25 +0100507
Willy Tarreaubafbe012017-11-24 17:34:44 +0100508 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200509 }
510
511 conn->mux = NULL;
512 conn->mux_ctx = NULL;
513
514 conn_stop_tracking(conn);
515 conn_full_close(conn);
516 if (conn->destroy_cb)
517 conn->destroy_cb(conn);
518 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200519}
520
521
Willy Tarreau71681172017-10-23 14:39:06 +0200522/******************************************************/
523/* functions below are for the H2 protocol processing */
524/******************************************************/
525
526/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100527static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200528{
529 return h2s ? h2s->id : 0;
530}
531
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200532/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100533static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200534{
535 if (h2c->msi < 0)
536 return 0;
537
538 if (h2c->msi == h2s_id(h2s))
539 return 0;
540
541 return 1;
542}
543
Willy Tarreau741d6df2017-10-17 08:00:59 +0200544/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100545static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200546{
547 h2c->errcode = err;
548 h2c->st0 = H2_CS_ERROR;
549}
550
Willy Tarreau2e43f082017-10-17 08:03:59 +0200551/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100552static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200553{
554 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) {
555 h2s->errcode = err;
556 h2s->st = H2_SS_ERROR;
557 if (h2s->cs)
558 h2s->cs->flags |= CS_FL_ERROR;
559 }
560}
561
Willy Tarreaue4820742017-07-27 13:37:23 +0200562/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100563static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200564{
565 uint8_t *out = frame;
566
567 *out = len >> 16;
568 write_n16(out + 1, len);
569}
570
Willy Tarreau54c15062017-10-10 17:10:03 +0200571/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
572 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
573 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200574 * available in the buffer's input prior to calling this function. The buffer
575 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200576 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100577static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200578 const struct buffer *b, int o)
579{
Willy Tarreau591d4452018-06-15 17:21:00 +0200580 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 +0200581}
582
Willy Tarreau1f094672017-11-20 21:27:45 +0100583static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200584{
Willy Tarreau591d4452018-06-15 17:21:00 +0200585 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200586}
587
Willy Tarreau1f094672017-11-20 21:27:45 +0100588static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200589{
Willy Tarreau591d4452018-06-15 17:21:00 +0200590 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200591}
592
Willy Tarreau1f094672017-11-20 21:27:45 +0100593static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200594{
Willy Tarreau591d4452018-06-15 17:21:00 +0200595 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200596}
597
598
Willy Tarreau715d5312017-07-11 15:20:24 +0200599/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
600 * is not obvious. It turns out that H2 headers are neither aligned nor do they
601 * use regular sizes. And to add to the trouble, the buffer may wrap so each
602 * byte read must be checked. The header is formed like this :
603 *
604 * b0 b1 b2 b3 b4 b5..b8
605 * +----------+---------+--------+----+----+----------------------+
606 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
607 * +----------+---------+--------+----+----+----------------------+
608 *
609 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
610 * we get the sid properly aligned and ordered, and 16 bits of len properly
611 * ordered as well. The type and flags can be extracted using bit shifts from
612 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200613 * Returns zero if some bytes are missing, otherwise non-zero on success. The
614 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200615 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100616static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200617{
618 uint64_t w;
619
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200620 if (b_data(b) < 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200621 return 0;
622
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200623 w = h2_get_n64(b, 1);
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200624 h->len = *(uint8_t*)b_head(b) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200625 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
626 h->ff = w >> 32;
627 h->ft = w >> 40;
628 h->len += w >> 48;
629 return 1;
630}
631
632/* skip the next 9 bytes corresponding to the frame header possibly parsed by
633 * h2_peek_frame_hdr() above.
634 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100635static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200636{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200637 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200638}
639
640/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100641static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200642{
643 int ret;
644
645 ret = h2_peek_frame_hdr(b, h);
646 if (ret > 0)
647 h2_skip_frame_hdr(b);
648 return ret;
649}
650
Willy Tarreau00dd0782018-03-01 16:31:34 +0100651/* marks stream <h2s> as CLOSED and decrement the number of active streams for
652 * its connection if the stream was not yet closed. Please use this exclusively
653 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100654 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100655static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100656{
657 if (h2s->st != H2_SS_CLOSED)
658 h2s->h2c->nb_streams--;
659 h2s->st = H2_SS_CLOSED;
660}
661
Willy Tarreau71049cc2018-03-28 13:56:39 +0200662/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
663static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100664{
665 h2s_close(h2s);
666 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200667 if (b_size(&h2s->rxbuf)) {
668 b_free(&h2s->rxbuf);
669 offer_buffers(NULL, tasks_run_queue);
670 }
Olivier Houchardc2aa7112018-09-11 18:27:21 +0200671 LIST_DEL(&h2s->wait_list.list);
672 LIST_INIT(&h2s->wait_list.list);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200673 tasklet_free(h2s->wait_list.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100674 pool_free(pool_head_h2s, h2s);
675}
676
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200677/* creates a new stream <id> on the h2c connection and returns it, or NULL in
678 * case of memory allocation error.
679 */
680static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
681{
Willy Tarreau590a0512018-09-05 11:56:48 +0200682 struct session *sess = h2c->conn->owner;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200683 struct conn_stream *cs;
684 struct h2s *h2s;
685
Willy Tarreaubafbe012017-11-24 17:34:44 +0100686 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200687 if (!h2s)
688 goto out;
689
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200690 h2s->wait_list.task = tasklet_new();
691 if (!h2s->wait_list.task) {
692 pool_free(pool_head_h2s, h2s);
693 goto out;
694 }
695 LIST_INIT(&h2s->wait_list.list);
696 h2s->recv_wait_list = NULL;
697 h2s->wait_list.task->process = h2_deferred_shut;
698 h2s->wait_list.task->context = h2s;
699 h2s->wait_list.handle = NULL;
700 h2s->wait_list.wait_reason = 0;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200701 h2s->h2c = h2c;
702 h2s->mws = h2c->miw;
703 h2s->flags = H2_SF_NONE;
704 h2s->errcode = H2_ERR_NO_ERROR;
705 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200706 h2s->status = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200707 h2s->rxbuf = BUF_NULL;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200708 h1m_init_res(&h2s->h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +0200709 h2s->h1m.err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +0200710 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200711 h2s->by_id.key = h2s->id = id;
712 h2c->max_id = id;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200713
714 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100715 h2c->nb_streams++;
716 if (h2c->nb_streams > h2_settings_max_concurrent_streams)
717 goto out_close;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200718
719 cs = cs_new(h2c->conn);
720 if (!cs)
721 goto out_close;
722
723 h2s->cs = cs;
724 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200725 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200726
727 if (stream_create_from_cs(cs) < 0)
728 goto out_free_cs;
729
Willy Tarreau590a0512018-09-05 11:56:48 +0200730 /* We want the accept date presented to the next stream to be the one
731 * we have now, the handshake time to be null (since the next stream
732 * is not delayed by a handshake), and the idle time to count since
733 * right now.
734 */
735 sess->accept_date = date;
736 sess->tv_accept = now;
737 sess->t_handshake = 0;
738
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200739 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200740 if (h2_has_too_many_cs(h2c))
741 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200742 return h2s;
743
744 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200745 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200746 cs_free(cs);
747 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200748 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200749 h2s = NULL;
Willy Tarreau22de8d32018-09-05 19:55:58 +0200750 sess_log(sess);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200751 out:
752 return h2s;
753}
754
Willy Tarreaube5b7152017-09-25 16:25:39 +0200755/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
756 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
757 * the various settings codes.
758 */
759static int h2c_snd_settings(struct h2c *h2c)
760{
761 struct buffer *res;
762 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200763 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200764 int ret;
765
766 if (h2c_mux_busy(h2c, NULL)) {
767 h2c->flags |= H2_CF_DEM_MBUSY;
768 return 0;
769 }
770
Willy Tarreau44e973f2018-03-01 17:49:30 +0100771 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200772 if (!res) {
773 h2c->flags |= H2_CF_MUX_MALLOC;
774 h2c->flags |= H2_CF_DEM_MROOM;
775 return 0;
776 }
777
778 chunk_init(&buf, buf_data, sizeof(buf_data));
779 chunk_memcpy(&buf,
780 "\x00\x00\x00" /* length : 0 for now */
781 "\x04\x00" /* type : 4 (settings), flags : 0 */
782 "\x00\x00\x00\x00", /* stream ID : 0 */
783 9);
784
785 if (h2_settings_header_table_size != 4096) {
786 char str[6] = "\x00\x01"; /* header_table_size */
787
788 write_n32(str + 2, h2_settings_header_table_size);
789 chunk_memcat(&buf, str, 6);
790 }
791
792 if (h2_settings_initial_window_size != 65535) {
793 char str[6] = "\x00\x04"; /* initial_window_size */
794
795 write_n32(str + 2, h2_settings_initial_window_size);
796 chunk_memcat(&buf, str, 6);
797 }
798
799 if (h2_settings_max_concurrent_streams != 0) {
800 char str[6] = "\x00\x03"; /* max_concurrent_streams */
801
802 /* Note: 0 means "unlimited" for haproxy's config but not for
803 * the protocol, so never send this value!
804 */
805 write_n32(str + 2, h2_settings_max_concurrent_streams);
806 chunk_memcat(&buf, str, 6);
807 }
808
809 if (global.tune.bufsize != 16384) {
810 char str[6] = "\x00\x05"; /* max_frame_size */
811
812 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
813 * match bufsize - rewrite size, but at the moment it seems
814 * that clients don't take care of it.
815 */
816 write_n32(str + 2, global.tune.bufsize);
817 chunk_memcat(&buf, str, 6);
818 }
819
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200820 h2_set_frame_size(buf.area, buf.data - 9);
821 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200822 if (unlikely(ret <= 0)) {
823 if (!ret) {
824 h2c->flags |= H2_CF_MUX_MFULL;
825 h2c->flags |= H2_CF_DEM_MROOM;
826 return 0;
827 }
828 else {
829 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
830 return 0;
831 }
832 }
833 return ret;
834}
835
Willy Tarreau52eed752017-09-22 15:05:09 +0200836/* Try to receive a connection preface, then upon success try to send our
837 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
838 * missing data. It may return an error in h2c.
839 */
840static int h2c_frt_recv_preface(struct h2c *h2c)
841{
842 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200843 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200844
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200845 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200846
847 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +0200848 if (ret1 < 0)
849 sess_log(h2c->conn->owner);
850
Willy Tarreau52eed752017-09-22 15:05:09 +0200851 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
852 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
853 return 0;
854 }
855
Willy Tarreaube5b7152017-09-25 16:25:39 +0200856 ret2 = h2c_snd_settings(h2c);
857 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200858 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200859
Willy Tarreaube5b7152017-09-25 16:25:39 +0200860 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200861}
862
Willy Tarreau081d4722017-05-16 21:51:05 +0200863/* try to send a GOAWAY frame on the connection to report an error or a graceful
864 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
865 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
866 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
867 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
868 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
869 * on unrecoverable failure. It will not attempt to send one again in this last
870 * case so that it is safe to use h2c_error() to report such errors.
871 */
872static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
873{
874 struct buffer *res;
875 char str[17];
876 int ret;
877
878 if (h2c->flags & H2_CF_GOAWAY_FAILED)
879 return 1; // claim that it worked
880
881 if (h2c_mux_busy(h2c, h2s)) {
882 if (h2s)
883 h2s->flags |= H2_SF_BLK_MBUSY;
884 else
885 h2c->flags |= H2_CF_DEM_MBUSY;
886 return 0;
887 }
888
Willy Tarreau44e973f2018-03-01 17:49:30 +0100889 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +0200890 if (!res) {
891 h2c->flags |= H2_CF_MUX_MALLOC;
892 if (h2s)
893 h2s->flags |= H2_SF_BLK_MROOM;
894 else
895 h2c->flags |= H2_CF_DEM_MROOM;
896 return 0;
897 }
898
899 /* len: 8, type: 7, flags: none, sid: 0 */
900 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
901
902 if (h2c->last_sid < 0)
903 h2c->last_sid = h2c->max_id;
904
905 write_n32(str + 9, h2c->last_sid);
906 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200907 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +0200908 if (unlikely(ret <= 0)) {
909 if (!ret) {
910 h2c->flags |= H2_CF_MUX_MFULL;
911 if (h2s)
912 h2s->flags |= H2_SF_BLK_MROOM;
913 else
914 h2c->flags |= H2_CF_DEM_MROOM;
915 return 0;
916 }
917 else {
918 /* we cannot report this error using GOAWAY, so we mark
919 * it and claim a success.
920 */
921 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
922 h2c->flags |= H2_CF_GOAWAY_FAILED;
923 return 1;
924 }
925 }
926 h2c->flags |= H2_CF_GOAWAY_SENT;
927 return ret;
928}
929
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100930/* Try to send an RST_STREAM frame on the connection for the indicated stream
931 * during mux operations. This stream must be valid and cannot be closed
932 * already. h2s->id will be used for the stream ID and h2s->errcode will be
933 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
934 * not yet.
935 *
936 * Returns > 0 on success or zero if nothing was done. In case of lack of room
937 * to write the message, it subscribes the stream to future notifications.
938 */
939static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
940{
941 struct buffer *res;
942 char str[13];
943 int ret;
944
945 if (!h2s || h2s->st == H2_SS_CLOSED)
946 return 1;
947
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100948 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
949 * RST_STREAM in response to a RST_STREAM frame.
950 */
951 if (h2c->dft == H2_FT_RST_STREAM) {
952 ret = 1;
953 goto ignore;
954 }
955
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100956 if (h2c_mux_busy(h2c, h2s)) {
957 h2s->flags |= H2_SF_BLK_MBUSY;
958 return 0;
959 }
960
Willy Tarreau44e973f2018-03-01 17:49:30 +0100961 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100962 if (!res) {
963 h2c->flags |= H2_CF_MUX_MALLOC;
964 h2s->flags |= H2_SF_BLK_MROOM;
965 return 0;
966 }
967
968 /* len: 4, type: 3, flags: none */
969 memcpy(str, "\x00\x00\x04\x03\x00", 5);
970 write_n32(str + 5, h2s->id);
971 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200972 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100973
974 if (unlikely(ret <= 0)) {
975 if (!ret) {
976 h2c->flags |= H2_CF_MUX_MFULL;
977 h2s->flags |= H2_SF_BLK_MROOM;
978 return 0;
979 }
980 else {
981 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
982 return 0;
983 }
984 }
985
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100986 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100987 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100988 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100989 return ret;
990}
991
992/* Try to send an RST_STREAM frame on the connection for the stream being
993 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
994 * error code unless the stream's state already is IDLE or CLOSED in which
995 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
996 * it was not yet.
997 *
998 * Returns > 0 on success or zero if nothing was done. In case of lack of room
999 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +02001000 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001001 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001002 */
1003static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1004{
1005 struct buffer *res;
1006 char str[13];
1007 int ret;
1008
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001009 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1010 * RST_STREAM in response to a RST_STREAM frame.
1011 */
1012 if (h2c->dft == H2_FT_RST_STREAM) {
1013 ret = 1;
1014 goto ignore;
1015 }
1016
Willy Tarreau27a84c92017-10-17 08:10:17 +02001017 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001018 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001019 return 0;
1020 }
1021
Willy Tarreau44e973f2018-03-01 17:49:30 +01001022 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001023 if (!res) {
1024 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001025 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001026 return 0;
1027 }
1028
1029 /* len: 4, type: 3, flags: none */
1030 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001031
Willy Tarreau27a84c92017-10-17 08:10:17 +02001032 write_n32(str + 5, h2c->dsi);
Willy Tarreau721c9742017-11-07 11:05:42 +01001033 write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ?
Willy Tarreau27a84c92017-10-17 08:10:17 +02001034 h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001035 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001036
Willy Tarreau27a84c92017-10-17 08:10:17 +02001037 if (unlikely(ret <= 0)) {
1038 if (!ret) {
1039 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001040 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001041 return 0;
1042 }
1043 else {
1044 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1045 return 0;
1046 }
1047 }
1048
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001049 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001050 if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001051 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001052 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001053 }
1054
Willy Tarreau27a84c92017-10-17 08:10:17 +02001055 return ret;
1056}
1057
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001058/* try to send an empty DATA frame with the ES flag set to notify about the
1059 * end of stream and match a shutdown(write). If an ES was already sent as
1060 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1061 * on success or zero if nothing was done. In case of lack of room to write the
1062 * message, it subscribes the requesting stream to future notifications.
1063 */
1064static int h2_send_empty_data_es(struct h2s *h2s)
1065{
1066 struct h2c *h2c = h2s->h2c;
1067 struct buffer *res;
1068 char str[9];
1069 int ret;
1070
Willy Tarreau721c9742017-11-07 11:05:42 +01001071 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001072 return 1;
1073
1074 if (h2c_mux_busy(h2c, h2s)) {
1075 h2s->flags |= H2_SF_BLK_MBUSY;
1076 return 0;
1077 }
1078
Willy Tarreau44e973f2018-03-01 17:49:30 +01001079 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001080 if (!res) {
1081 h2c->flags |= H2_CF_MUX_MALLOC;
1082 h2s->flags |= H2_SF_BLK_MROOM;
1083 return 0;
1084 }
1085
1086 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1087 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1088 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001089 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001090 if (likely(ret > 0)) {
1091 h2s->flags |= H2_SF_ES_SENT;
1092 }
1093 else if (!ret) {
1094 h2c->flags |= H2_CF_MUX_MFULL;
1095 h2s->flags |= H2_SF_BLK_MROOM;
1096 return 0;
1097 }
1098 else {
1099 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1100 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001101 }
1102 return ret;
1103}
1104
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001105/* wake the streams attached to the connection, whose id is greater than <last>,
1106 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001107 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1108 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001109 */
1110static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1111{
1112 struct eb32_node *node;
1113 struct h2s *h2s;
1114
1115 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1116 flags |= CS_FL_ERROR;
1117
1118 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001119 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001120
1121 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1122 while (node) {
1123 h2s = container_of(node, struct h2s, by_id);
1124 if (h2s->id <= last)
1125 break;
1126 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001127
1128 if (!h2s->cs) {
1129 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001130 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001131 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001132 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001133
1134 h2s->cs->flags |= flags;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001135 if (h2s->recv_wait_list) {
1136 struct wait_list *sw = h2s->recv_wait_list;
1137 sw->wait_reason &= ~SUB_CAN_RECV;
1138 tasklet_wakeup(sw->task);
1139 h2s->recv_wait_list = NULL;
1140 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001141
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001142 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1143 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001144 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001145 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001146 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001147 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001148 }
1149}
1150
Willy Tarreau3421aba2017-07-27 15:41:03 +02001151/* Increase all streams' outgoing window size by the difference passed in
1152 * argument. This is needed upon receipt of the settings frame if the initial
1153 * window size is different. The difference may be negative and the resulting
1154 * window size as well, for the time it takes to receive some window updates.
1155 */
1156static void h2c_update_all_ws(struct h2c *h2c, int diff)
1157{
1158 struct h2s *h2s;
1159 struct eb32_node *node;
1160
1161 if (!diff)
1162 return;
1163
1164 node = eb32_first(&h2c->streams_by_id);
1165 while (node) {
1166 h2s = container_of(node, struct h2s, by_id);
1167 h2s->mws += diff;
1168 node = eb32_next(node);
1169 }
1170}
1171
1172/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1173 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1174 * return an error in h2c. Described in RFC7540#6.5.
1175 */
1176static int h2c_handle_settings(struct h2c *h2c)
1177{
1178 unsigned int offset;
1179 int error;
1180
1181 if (h2c->dff & H2_F_SETTINGS_ACK) {
1182 if (h2c->dfl) {
1183 error = H2_ERR_FRAME_SIZE_ERROR;
1184 goto fail;
1185 }
1186 return 1;
1187 }
1188
1189 if (h2c->dsi != 0) {
1190 error = H2_ERR_PROTOCOL_ERROR;
1191 goto fail;
1192 }
1193
1194 if (h2c->dfl % 6) {
1195 error = H2_ERR_FRAME_SIZE_ERROR;
1196 goto fail;
1197 }
1198
1199 /* that's the limit we can process */
1200 if (h2c->dfl > global.tune.bufsize) {
1201 error = H2_ERR_FRAME_SIZE_ERROR;
1202 goto fail;
1203 }
1204
1205 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001206 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001207 return 0;
1208
1209 /* parse the frame */
1210 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001211 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1212 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001213
1214 switch (type) {
1215 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1216 /* we need to update all existing streams with the
1217 * difference from the previous iws.
1218 */
1219 if (arg < 0) { // RFC7540#6.5.2
1220 error = H2_ERR_FLOW_CONTROL_ERROR;
1221 goto fail;
1222 }
1223 h2c_update_all_ws(h2c, arg - h2c->miw);
1224 h2c->miw = arg;
1225 break;
1226 case H2_SETTINGS_MAX_FRAME_SIZE:
1227 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1228 error = H2_ERR_PROTOCOL_ERROR;
1229 goto fail;
1230 }
1231 h2c->mfs = arg;
1232 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001233 case H2_SETTINGS_ENABLE_PUSH:
1234 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1235 error = H2_ERR_PROTOCOL_ERROR;
1236 goto fail;
1237 }
1238 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001239 }
1240 }
1241
1242 /* need to ACK this frame now */
1243 h2c->st0 = H2_CS_FRAME_A;
1244 return 1;
1245 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001246 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001247 h2c_error(h2c, error);
1248 return 0;
1249}
1250
1251/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1252 * success or one of the h2_status values.
1253 */
1254static int h2c_ack_settings(struct h2c *h2c)
1255{
1256 struct buffer *res;
1257 char str[9];
1258 int ret = -1;
1259
1260 if (h2c_mux_busy(h2c, NULL)) {
1261 h2c->flags |= H2_CF_DEM_MBUSY;
1262 return 0;
1263 }
1264
Willy Tarreau44e973f2018-03-01 17:49:30 +01001265 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001266 if (!res) {
1267 h2c->flags |= H2_CF_MUX_MALLOC;
1268 h2c->flags |= H2_CF_DEM_MROOM;
1269 return 0;
1270 }
1271
1272 memcpy(str,
1273 "\x00\x00\x00" /* length : 0 (no data) */
1274 "\x04" "\x01" /* type : 4, flags : ACK */
1275 "\x00\x00\x00\x00" /* stream ID */, 9);
1276
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001277 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001278 if (unlikely(ret <= 0)) {
1279 if (!ret) {
1280 h2c->flags |= H2_CF_MUX_MFULL;
1281 h2c->flags |= H2_CF_DEM_MROOM;
1282 return 0;
1283 }
1284 else {
1285 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1286 return 0;
1287 }
1288 }
1289 return ret;
1290}
1291
Willy Tarreaucf68c782017-10-10 17:11:41 +02001292/* processes a PING frame and schedules an ACK if needed. The caller must pass
1293 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1294 * missing data. It may return an error in h2c.
1295 */
1296static int h2c_handle_ping(struct h2c *h2c)
1297{
1298 /* frame length must be exactly 8 */
1299 if (h2c->dfl != 8) {
1300 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1301 return 0;
1302 }
1303
1304 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001305 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001306 h2c->st0 = H2_CS_FRAME_A;
1307 return 1;
1308}
1309
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001310/* Try to send a window update for stream id <sid> and value <increment>.
1311 * Returns > 0 on success or zero on missing room or failure. It may return an
1312 * error in h2c.
1313 */
1314static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1315{
1316 struct buffer *res;
1317 char str[13];
1318 int ret = -1;
1319
1320 if (h2c_mux_busy(h2c, NULL)) {
1321 h2c->flags |= H2_CF_DEM_MBUSY;
1322 return 0;
1323 }
1324
Willy Tarreau44e973f2018-03-01 17:49:30 +01001325 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001326 if (!res) {
1327 h2c->flags |= H2_CF_MUX_MALLOC;
1328 h2c->flags |= H2_CF_DEM_MROOM;
1329 return 0;
1330 }
1331
1332 /* length: 4, type: 8, flags: none */
1333 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1334 write_n32(str + 5, sid);
1335 write_n32(str + 9, increment);
1336
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001337 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001338
1339 if (unlikely(ret <= 0)) {
1340 if (!ret) {
1341 h2c->flags |= H2_CF_MUX_MFULL;
1342 h2c->flags |= H2_CF_DEM_MROOM;
1343 return 0;
1344 }
1345 else {
1346 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1347 return 0;
1348 }
1349 }
1350 return ret;
1351}
1352
1353/* try to send pending window update for the connection. It's safe to call it
1354 * with no pending updates. Returns > 0 on success or zero on missing room or
1355 * failure. It may return an error in h2c.
1356 */
1357static int h2c_send_conn_wu(struct h2c *h2c)
1358{
1359 int ret = 1;
1360
1361 if (h2c->rcvd_c <= 0)
1362 return 1;
1363
1364 /* send WU for the connection */
1365 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1366 if (ret > 0)
1367 h2c->rcvd_c = 0;
1368
1369 return ret;
1370}
1371
1372/* try to send pending window update for the current dmux stream. It's safe to
1373 * call it with no pending updates. Returns > 0 on success or zero on missing
1374 * room or failure. It may return an error in h2c.
1375 */
1376static int h2c_send_strm_wu(struct h2c *h2c)
1377{
1378 int ret = 1;
1379
1380 if (h2c->rcvd_s <= 0)
1381 return 1;
1382
1383 /* send WU for the stream */
1384 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1385 if (ret > 0)
1386 h2c->rcvd_s = 0;
1387
1388 return ret;
1389}
1390
Willy Tarreaucf68c782017-10-10 17:11:41 +02001391/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1392 * success, 0 on missing data or one of the h2_status values.
1393 */
1394static int h2c_ack_ping(struct h2c *h2c)
1395{
1396 struct buffer *res;
1397 char str[17];
1398 int ret = -1;
1399
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001400 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001401 return 0;
1402
1403 if (h2c_mux_busy(h2c, NULL)) {
1404 h2c->flags |= H2_CF_DEM_MBUSY;
1405 return 0;
1406 }
1407
Willy Tarreau44e973f2018-03-01 17:49:30 +01001408 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001409 if (!res) {
1410 h2c->flags |= H2_CF_MUX_MALLOC;
1411 h2c->flags |= H2_CF_DEM_MROOM;
1412 return 0;
1413 }
1414
1415 memcpy(str,
1416 "\x00\x00\x08" /* length : 8 (same payload) */
1417 "\x06" "\x01" /* type : 6, flags : ACK */
1418 "\x00\x00\x00\x00" /* stream ID */, 9);
1419
1420 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001421 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001422
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001423 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001424 if (unlikely(ret <= 0)) {
1425 if (!ret) {
1426 h2c->flags |= H2_CF_MUX_MFULL;
1427 h2c->flags |= H2_CF_DEM_MROOM;
1428 return 0;
1429 }
1430 else {
1431 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1432 return 0;
1433 }
1434 }
1435 return ret;
1436}
1437
Willy Tarreau26f95952017-07-27 17:18:30 +02001438/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1439 * Returns > 0 on success or zero on missing data. It may return an error in
1440 * h2c or h2s. Described in RFC7540#6.9.
1441 */
1442static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1443{
1444 int32_t inc;
1445 int error;
1446
1447 if (h2c->dfl != 4) {
1448 error = H2_ERR_FRAME_SIZE_ERROR;
1449 goto conn_err;
1450 }
1451
1452 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001453 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001454 return 0;
1455
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001456 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001457
1458 if (h2c->dsi != 0) {
1459 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001460
1461 /* it's not an error to receive WU on a closed stream */
1462 if (h2s->st == H2_SS_CLOSED)
1463 return 1;
1464
1465 if (!inc) {
1466 error = H2_ERR_PROTOCOL_ERROR;
1467 goto strm_err;
1468 }
1469
1470 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1471 error = H2_ERR_FLOW_CONTROL_ERROR;
1472 goto strm_err;
1473 }
1474
1475 h2s->mws += inc;
1476 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1477 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02001478 /* The task will be waken up later */
Willy Tarreau26f95952017-07-27 17:18:30 +02001479 }
1480 }
1481 else {
1482 /* connection window update */
1483 if (!inc) {
1484 error = H2_ERR_PROTOCOL_ERROR;
1485 goto conn_err;
1486 }
1487
1488 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1489 error = H2_ERR_FLOW_CONTROL_ERROR;
1490 goto conn_err;
1491 }
1492
1493 h2c->mws += inc;
1494 }
1495
1496 return 1;
1497
1498 conn_err:
1499 h2c_error(h2c, error);
1500 return 0;
1501
1502 strm_err:
1503 if (h2s) {
1504 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001505 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001506 }
1507 else
1508 h2c_error(h2c, error);
1509 return 0;
1510}
1511
Willy Tarreaue96b0922017-10-30 00:28:29 +01001512/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1513 * the last ID. Returns > 0 on success or zero on missing data. It may return
1514 * an error in h2c. Described in RFC7540#6.8.
1515 */
1516static int h2c_handle_goaway(struct h2c *h2c)
1517{
1518 int error;
1519 int last;
1520
1521 if (h2c->dsi != 0) {
1522 error = H2_ERR_PROTOCOL_ERROR;
1523 goto conn_err;
1524 }
1525
1526 if (h2c->dfl < 8) {
1527 error = H2_ERR_FRAME_SIZE_ERROR;
1528 goto conn_err;
1529 }
1530
1531 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001532 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001533 return 0;
1534
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001535 last = h2_get_n32(&h2c->dbuf, 0);
1536 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001537 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001538 if (h2c->last_sid < 0)
1539 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001540 return 1;
1541
1542 conn_err:
1543 h2c_error(h2c, error);
1544 return 0;
1545}
1546
Willy Tarreau92153fc2017-12-03 19:46:19 +01001547/* processes a PRIORITY frame, and either skips it or rejects if it is
1548 * invalid. Returns > 0 on success or zero on missing data. It may return
1549 * an error in h2c. Described in RFC7540#6.3.
1550 */
1551static int h2c_handle_priority(struct h2c *h2c)
1552{
1553 int error;
1554
1555 if (h2c->dsi == 0) {
1556 error = H2_ERR_PROTOCOL_ERROR;
1557 goto conn_err;
1558 }
1559
1560 if (h2c->dfl != 5) {
1561 error = H2_ERR_FRAME_SIZE_ERROR;
1562 goto conn_err;
1563 }
1564
1565 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001566 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001567 return 0;
1568
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001569 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001570 /* 7540#5.3 : can't depend on itself */
1571 error = H2_ERR_PROTOCOL_ERROR;
1572 goto conn_err;
1573 }
1574 return 1;
1575
1576 conn_err:
1577 h2c_error(h2c, error);
1578 return 0;
1579}
1580
Willy Tarreaucd234e92017-08-18 10:59:39 +02001581/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1582 * Returns > 0 on success or zero on missing data. It may return an error in
1583 * h2c. Described in RFC7540#6.4.
1584 */
1585static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1586{
1587 int error;
1588
1589 if (h2c->dsi == 0) {
1590 error = H2_ERR_PROTOCOL_ERROR;
1591 goto conn_err;
1592 }
1593
Willy Tarreaucd234e92017-08-18 10:59:39 +02001594 if (h2c->dfl != 4) {
1595 error = H2_ERR_FRAME_SIZE_ERROR;
1596 goto conn_err;
1597 }
1598
1599 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001600 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001601 return 0;
1602
1603 /* late RST, already handled */
1604 if (h2s->st == H2_SS_CLOSED)
1605 return 1;
1606
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001607 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001608 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001609
1610 if (h2s->cs) {
Willy Tarreau2c096c32018-09-12 09:45:54 +02001611 h2s->cs->flags |= CS_FL_REOS | CS_FL_ERROR;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001612 if (h2s->recv_wait_list) {
1613 struct wait_list *sw = h2s->recv_wait_list;
1614
1615 sw->wait_reason &= ~SUB_CAN_RECV;
1616 tasklet_wakeup(sw->task);
1617 h2s->recv_wait_list = NULL;
1618 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02001619 }
1620
1621 h2s->flags |= H2_SF_RST_RCVD;
1622 return 1;
1623
1624 conn_err:
1625 h2c_error(h2c, error);
1626 return 0;
1627}
1628
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001629/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1630 * It may return an error in h2c or h2s. The caller must consider that the
1631 * return value is the new h2s in case one was allocated (most common case).
1632 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001633 * errors here are reported as connection errors since it's impossible to
1634 * recover from such errors after the compression context has been altered.
1635 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001636static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001637{
1638 int error;
1639
1640 if (!h2c->dfl) {
1641 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001642 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001643 goto strm_err;
1644 }
1645
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001646 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001647 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001648
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001649 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001650 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001651
Willy Tarreauf2101912018-07-19 10:11:38 +02001652 if (h2c->flags & H2_CF_DEM_TOOMANY)
1653 return 0; // too many cs still present
1654
Willy Tarreau13278b42017-10-13 19:23:14 +02001655 /* now either the frame is complete or the buffer is complete */
1656 if (h2s->st != H2_SS_IDLE) {
1657 /* FIXME: stream already exists, this is only allowed for
1658 * trailers (not supported for now).
1659 */
1660 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001661 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001662 goto conn_err;
1663 }
1664 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1665 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1666 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001667 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001668 goto conn_err;
1669 }
1670
Willy Tarreau22de8d32018-09-05 19:55:58 +02001671 /* Note: we don't emit any other logs below because ff we return
1672 * positively from h2c_stream_new(), the stream will report the error,
1673 * and if we return in error, h2c_stream_new() will emit the error.
1674 */
Willy Tarreau13278b42017-10-13 19:23:14 +02001675 h2s = h2c_stream_new(h2c, h2c->dsi);
1676 if (!h2s) {
1677 error = H2_ERR_INTERNAL_ERROR;
1678 goto conn_err;
1679 }
1680
1681 h2s->st = H2_SS_OPEN;
1682 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1683 h2s->st = H2_SS_HREM;
1684 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001685 /* note: cs cannot be null for now (just created above) */
1686 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001687 }
1688
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001689 if (!h2_frt_decode_headers(h2s))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001690 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001691
Willy Tarreau8f650c32017-11-21 19:36:21 +01001692 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001693 return NULL;
Willy Tarreau8f650c32017-11-21 19:36:21 +01001694
Willy Tarreau721c9742017-11-07 11:05:42 +01001695 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001696 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001697 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001698 }
1699 else {
1700 /* update the max stream ID if the request is being processed */
1701 if (h2s->id > h2c->max_id)
1702 h2c->max_id = h2s->id;
1703 }
1704
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001705 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001706
1707 conn_err:
1708 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001709 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001710
1711 strm_err:
1712 if (h2s) {
1713 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001714 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001715 }
1716 else
1717 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001718 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001719}
1720
Willy Tarreau454f9052017-10-26 19:40:35 +02001721/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1722 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1723 */
1724static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1725{
1726 int error;
1727
1728 /* note that empty DATA frames are perfectly valid and sometimes used
1729 * to signal an end of stream (with the ES flag).
1730 */
1731
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001732 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001733 return 0; // empty buffer
1734
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001735 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001736 return 0; // incomplete frame
1737
1738 /* now either the frame is complete or the buffer is complete */
1739
1740 if (!h2c->dsi) {
1741 /* RFC7540#6.1 */
1742 error = H2_ERR_PROTOCOL_ERROR;
1743 goto conn_err;
1744 }
1745
1746 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1747 /* RFC7540#6.1 */
1748 error = H2_ERR_STREAM_CLOSED;
1749 goto strm_err;
1750 }
1751
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001752 if (!h2_frt_transfer_data(h2s))
1753 return 0;
1754
Willy Tarreau454f9052017-10-26 19:40:35 +02001755 /* call the upper layers to process the frame, then let the upper layer
1756 * notify the stream about any change.
1757 */
1758 if (!h2s->cs) {
1759 error = H2_ERR_STREAM_CLOSED;
1760 goto strm_err;
1761 }
1762
Willy Tarreau8f650c32017-11-21 19:36:21 +01001763 if (h2c->st0 >= H2_CS_ERROR)
1764 return 0;
1765
Willy Tarreau721c9742017-11-07 11:05:42 +01001766 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001767 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001768 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001769 }
1770
1771 /* check for completion : the callee will change this to FRAME_A or
1772 * FRAME_H once done.
1773 */
1774 if (h2c->st0 == H2_CS_FRAME_P)
1775 return 0;
1776
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001777
1778 /* last frame */
1779 if (h2c->dff & H2_F_DATA_END_STREAM) {
1780 h2s->st = H2_SS_HREM;
1781 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001782 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001783 }
1784
Willy Tarreau454f9052017-10-26 19:40:35 +02001785 return 1;
1786
1787 conn_err:
1788 h2c_error(h2c, error);
1789 return 0;
1790
1791 strm_err:
1792 if (h2s) {
1793 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001794 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001795 }
1796 else
1797 h2c_error(h2c, error);
1798 return 0;
1799}
1800
Willy Tarreaubc933932017-10-09 16:21:43 +02001801/* process Rx frames to be demultiplexed */
1802static void h2_process_demux(struct h2c *h2c)
1803{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001804 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001805
Willy Tarreau081d4722017-05-16 21:51:05 +02001806 if (h2c->st0 >= H2_CS_ERROR)
1807 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001808
1809 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1810 if (h2c->st0 == H2_CS_PREFACE) {
1811 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1812 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001813 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02001814 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001815 sess_log(h2c->conn->owner);
1816 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001817 goto fail;
1818 }
1819
1820 h2c->max_id = 0;
1821 h2c->st0 = H2_CS_SETTINGS1;
1822 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001823
1824 if (h2c->st0 == H2_CS_SETTINGS1) {
1825 struct h2_fh hdr;
1826
1827 /* ensure that what is pending is a valid SETTINGS frame
1828 * without an ACK.
1829 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001830 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001831 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001832 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001833 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001834 sess_log(h2c->conn->owner);
1835 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001836 goto fail;
1837 }
1838
1839 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1840 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1841 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1842 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001843 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001844 goto fail;
1845 }
1846
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001847 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001848 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1849 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1850 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001851 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001852 goto fail;
1853 }
1854
1855 /* that's OK, switch to FRAME_P to process it */
1856 h2c->dfl = hdr.len;
1857 h2c->dsi = hdr.sid;
1858 h2c->dft = hdr.ft;
1859 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001860 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001861 h2c->st0 = H2_CS_FRAME_P;
1862 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001863 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001864
1865 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001866 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001867 int ret = 0;
1868
1869 if (h2c->st0 >= H2_CS_ERROR)
1870 break;
1871
1872 if (h2c->st0 == H2_CS_FRAME_H) {
1873 struct h2_fh hdr;
1874
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001875 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02001876 break;
1877
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001878 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001879 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1880 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001881 if (!h2c->nb_streams) {
1882 /* only log if no other stream can report the error */
1883 sess_log(h2c->conn->owner);
1884 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001885 break;
1886 }
1887
1888 h2c->dfl = hdr.len;
1889 h2c->dsi = hdr.sid;
1890 h2c->dft = hdr.ft;
1891 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001892 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001893 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001894 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001895 }
1896
1897 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001898 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
1899
Olivier Houchard638b7992018-08-16 15:41:52 +02001900 if (tmp_h2s != h2s && h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001901 /* we may have to signal the upper layers */
1902 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001903 if (h2s->recv_wait_list) {
1904 h2s->recv_wait_list->wait_reason &= ~SUB_CAN_RECV;
1905 tasklet_wakeup(h2s->recv_wait_list->task);
1906 h2s->recv_wait_list = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001907 }
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001908 if (h2c->st0 >= H2_CS_ERROR)
1909 goto strm_err;
1910
1911 if (h2s->st >= H2_SS_ERROR) {
1912 /* stream error : send RST_STREAM */
1913 h2c->st0 = H2_CS_FRAME_E;
1914 }
1915 }
1916 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001917
Willy Tarreaud7901432017-12-29 11:34:40 +01001918 if (h2c->st0 == H2_CS_FRAME_E)
1919 goto strm_err;
1920
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001921 if (h2s->st == H2_SS_IDLE &&
1922 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1923 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1924 * this state MUST be treated as a connection error
1925 */
1926 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1927 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001928 if (!h2c->nb_streams) {
1929 /* only log if no other stream can report the error */
1930 sess_log(h2c->conn->owner);
1931 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001932 break;
1933 }
1934
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001935 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1936 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1937 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1938 * this state MUST be treated as a stream error
1939 */
1940 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1941 goto strm_err;
1942 }
1943
Willy Tarreauab837502017-12-27 15:07:30 +01001944 /* Below the management of frames received in closed state is a
1945 * bit hackish because the spec makes strong differences between
1946 * streams closed by receiving RST, sending RST, and seeing ES
1947 * in both directions. In addition to this, the creation of a
1948 * new stream reusing the identifier of a closed one will be
1949 * detected here. Given that we cannot keep track of all closed
1950 * streams forever, we consider that unknown closed streams were
1951 * closed on RST received, which allows us to respond with an
1952 * RST without breaking the connection (eg: to abort a transfer).
1953 * Some frames have to be silently ignored as well.
1954 */
1955 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1956 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1957 /* #5.1.1: The identifier of a newly
1958 * established stream MUST be numerically
1959 * greater than all streams that the initiating
1960 * endpoint has opened or reserved. This
1961 * governs streams that are opened using a
1962 * HEADERS frame and streams that are reserved
1963 * using PUSH_PROMISE. An endpoint that
1964 * receives an unexpected stream identifier
1965 * MUST respond with a connection error.
1966 */
1967 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1968 goto strm_err;
1969 }
1970
1971 if (h2s->flags & H2_SF_RST_RCVD) {
1972 /* RFC7540#5.1:closed: an endpoint that
1973 * receives any frame other than PRIORITY after
1974 * receiving a RST_STREAM MUST treat that as a
1975 * stream error of type STREAM_CLOSED.
1976 *
1977 * Note that old streams fall into this category
1978 * and will lead to an RST being sent.
1979 */
1980 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1981 h2c->st0 = H2_CS_FRAME_E;
1982 goto strm_err;
1983 }
1984
1985 /* RFC7540#5.1:closed: if this state is reached as a
1986 * result of sending a RST_STREAM frame, the peer that
1987 * receives the RST_STREAM might have already sent
1988 * frames on the stream that cannot be withdrawn. An
1989 * endpoint MUST ignore frames that it receives on
1990 * closed streams after it has sent a RST_STREAM
1991 * frame. An endpoint MAY choose to limit the period
1992 * over which it ignores frames and treat frames that
1993 * arrive after this time as being in error.
1994 */
1995 if (!(h2s->flags & H2_SF_RST_SENT)) {
1996 /* RFC7540#5.1:closed: any frame other than
1997 * PRIO/WU/RST in this state MUST be treated as
1998 * a connection error
1999 */
2000 if (h2c->dft != H2_FT_RST_STREAM &&
2001 h2c->dft != H2_FT_PRIORITY &&
2002 h2c->dft != H2_FT_WINDOW_UPDATE) {
2003 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2004 goto strm_err;
2005 }
2006 }
2007 }
2008
Willy Tarreauc0da1962017-10-30 18:38:00 +01002009#if 0
2010 // problem below: it is not possible to completely ignore such
2011 // streams as we need to maintain the compression state as well
2012 // and for this we need to completely process these frames (eg:
2013 // HEADERS frames) as well as counting DATA frames to emit
2014 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2015 // This is a typical case of layer violation where the
2016 // transported contents are critical to the connection's
2017 // validity and must be ignored at the same time :-(
2018
2019 /* graceful shutdown, ignore streams whose ID is higher than
2020 * the one advertised in GOAWAY. RFC7540#6.8.
2021 */
2022 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002023 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2024 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002025 h2c->dfl -= ret;
2026 ret = h2c->dfl == 0;
2027 goto strm_err;
2028 }
2029#endif
2030
Willy Tarreau7e98c052017-10-10 15:56:59 +02002031 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002032 case H2_FT_SETTINGS:
2033 if (h2c->st0 == H2_CS_FRAME_P)
2034 ret = h2c_handle_settings(h2c);
2035
2036 if (h2c->st0 == H2_CS_FRAME_A)
2037 ret = h2c_ack_settings(h2c);
2038 break;
2039
Willy Tarreaucf68c782017-10-10 17:11:41 +02002040 case H2_FT_PING:
2041 if (h2c->st0 == H2_CS_FRAME_P)
2042 ret = h2c_handle_ping(h2c);
2043
2044 if (h2c->st0 == H2_CS_FRAME_A)
2045 ret = h2c_ack_ping(h2c);
2046 break;
2047
Willy Tarreau26f95952017-07-27 17:18:30 +02002048 case H2_FT_WINDOW_UPDATE:
2049 if (h2c->st0 == H2_CS_FRAME_P)
2050 ret = h2c_handle_window_update(h2c, h2s);
2051 break;
2052
Willy Tarreau61290ec2017-10-17 08:19:21 +02002053 case H2_FT_CONTINUATION:
2054 /* we currently don't support CONTINUATION frames since
2055 * we have nowhere to store the partial HEADERS frame.
2056 * Let's abort the stream on an INTERNAL_ERROR here.
2057 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002058 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02002059 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002060 h2c->st0 = H2_CS_FRAME_E;
2061 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02002062 break;
2063
Willy Tarreau13278b42017-10-13 19:23:14 +02002064 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002065 if (h2c->st0 == H2_CS_FRAME_P) {
2066 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
2067 if (tmp_h2s) {
2068 h2s = tmp_h2s;
2069 ret = 1;
2070 }
2071 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002072 break;
2073
Willy Tarreau454f9052017-10-26 19:40:35 +02002074 case H2_FT_DATA:
2075 if (h2c->st0 == H2_CS_FRAME_P)
2076 ret = h2c_frt_handle_data(h2c, h2s);
2077
2078 if (h2c->st0 == H2_CS_FRAME_A)
2079 ret = h2c_send_strm_wu(h2c);
2080 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002081
Willy Tarreau92153fc2017-12-03 19:46:19 +01002082 case H2_FT_PRIORITY:
2083 if (h2c->st0 == H2_CS_FRAME_P)
2084 ret = h2c_handle_priority(h2c);
2085 break;
2086
Willy Tarreaucd234e92017-08-18 10:59:39 +02002087 case H2_FT_RST_STREAM:
2088 if (h2c->st0 == H2_CS_FRAME_P)
2089 ret = h2c_handle_rst_stream(h2c, h2s);
2090 break;
2091
Willy Tarreaue96b0922017-10-30 00:28:29 +01002092 case H2_FT_GOAWAY:
2093 if (h2c->st0 == H2_CS_FRAME_P)
2094 ret = h2c_handle_goaway(h2c);
2095 break;
2096
Willy Tarreau1c661982017-10-30 13:52:01 +01002097 case H2_FT_PUSH_PROMISE:
2098 /* not permitted here, RFC7540#5.1 */
2099 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002100 if (!h2c->nb_streams) {
2101 /* only log if no other stream can report the error */
2102 sess_log(h2c->conn->owner);
2103 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002104 break;
2105
2106 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002107 default:
2108 /* drop frames that we ignore. They may be larger than
2109 * the buffer so we drain all of their contents until
2110 * we reach the end.
2111 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002112 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2113 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002114 h2c->dfl -= ret;
2115 ret = h2c->dfl == 0;
2116 }
2117
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002118 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002119 /* We may have to send an RST if not done yet */
2120 if (h2s->st == H2_SS_ERROR)
2121 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002122
Willy Tarreaua20a5192017-12-27 11:02:06 +01002123 if (h2c->st0 == H2_CS_FRAME_E)
2124 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002125
Willy Tarreau7e98c052017-10-10 15:56:59 +02002126 /* error or missing data condition met above ? */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002127 if (ret <= 0) {
2128 h2s = NULL;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002129 break;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002130 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002131
2132 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002133 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002134 h2c->st0 = H2_CS_FRAME_H;
2135 }
2136 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002137
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002138 if (h2c->rcvd_c > 0 &&
2139 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2140 h2c_send_conn_wu(h2c);
2141
Willy Tarreau52eed752017-09-22 15:05:09 +02002142 fail:
2143 /* we can go here on missing data, blocked response or error */
Olivier Houchard638b7992018-08-16 15:41:52 +02002144 if (h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002145 /* we may have to signal the upper layers */
2146 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002147 if (h2s->recv_wait_list) {
2148 h2s->recv_wait_list->wait_reason &= ~SUB_CAN_RECV;
2149 tasklet_wakeup(h2s->recv_wait_list->task);
2150 h2s->recv_wait_list = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002151 }
2152 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002153 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02002154}
2155
2156/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2157 * the end.
2158 */
2159static int h2_process_mux(struct h2c *h2c)
2160{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002161 struct h2s *h2s;
2162 struct wait_list *sw, *sw_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002163
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002164 /* start by sending possibly pending window updates */
2165 if (h2c->rcvd_c > 0 &&
2166 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2167 h2c_send_conn_wu(h2c) < 0)
2168 goto fail;
2169
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002170 /* First we always process the flow control list because the streams
2171 * waiting there were already elected for immediate emission but were
2172 * blocked just on this.
2173 */
2174
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002175 list_for_each_entry_safe(sw, sw_back, &h2c->fctl_list, list) {
2176 h2s = sw->handle;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002177 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2178 h2c->st0 >= H2_CS_ERROR)
2179 break;
2180
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002181 /* If the tasklet was added to finish shutr/shutw, just wake the task */
2182 if ((long)(h2s) & 3) {
2183 sw->wait_reason &= ~SUB_CAN_SEND;
2184 LIST_DEL(&sw->list);
2185 LIST_INIT(&sw->list);
2186 tasklet_wakeup(sw->task);
2187 } else if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
2188 h2s->flags &= ~H2_SF_BLK_ANY;
2189 LIST_DEL(&sw->list);
2190 LIST_INIT(&sw->list);
2191 sw->wait_reason &= ~SUB_CAN_SEND;
2192 tasklet_wakeup(sw->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002193 }
2194 }
2195
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002196 list_for_each_entry_safe(sw, sw_back, &h2c->send_list, list) {
2197 h2s = sw->handle;
2198
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002199 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2200 break;
2201
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002202 /* If the tasklet was added to finish shutr/shutw, just wake the task */
2203 if ((long)(h2s) & 3) {
2204 sw->wait_reason &= ~SUB_CAN_SEND;
2205 LIST_DEL(&sw->list);
2206 LIST_INIT(&sw->list);
2207 tasklet_wakeup(sw->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002208 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002209 else if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
2210 h2s->flags &= ~H2_SF_BLK_ANY;
2211
2212 LIST_DEL(&sw->list);
2213 LIST_INIT(&sw->list);
2214 sw->wait_reason &= ~SUB_CAN_SEND;
2215 tasklet_wakeup(sw->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002216 }
2217 }
2218
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002219 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002220 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002221 if (h2c->st0 == H2_CS_ERROR) {
2222 if (h2c->max_id >= 0) {
2223 h2c_send_goaway_error(h2c, NULL);
2224 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2225 return 0;
2226 }
2227
2228 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2229 }
2230 return 1;
2231 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002232 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002233}
2234
Willy Tarreau62f52692017-10-08 23:01:42 +02002235
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002236/* Attempt to read data, and subscribe if none available */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002237static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002238{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002239 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002240 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002241 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002242 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002243
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002244 if (h2c->wait_list.wait_reason & SUB_CAN_RECV)
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002245 return 0;
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002246
Willy Tarreau315d8072017-12-10 22:17:57 +01002247 if (!h2_recv_allowed(h2c))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002248 return 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002249
Willy Tarreau44e973f2018-03-01 17:49:30 +01002250 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002251 if (!buf) {
2252 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002253 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002254 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002255
Olivier Houchard7505f942018-08-21 18:10:44 +02002256 do {
2257 max = buf->size - b_data(buf);
2258 if (max)
2259 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2260 else
2261 ret = 0;
2262 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002263
Olivier Houchard7505f942018-08-21 18:10:44 +02002264 if (h2_recv_allowed(h2c)) {
2265 conn_xprt_want_recv(conn);
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002266 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h2c->wait_list);
Olivier Houchard7505f942018-08-21 18:10:44 +02002267 }
Olivier Houcharda1411e62018-08-17 18:42:48 +02002268 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002269 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002270 return 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002271 }
2272
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002273 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002274 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002275 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002276}
2277
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002278/* Try to send data if possible */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002279static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002280{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002281 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002282 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002283 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002284
2285 if (conn->flags & CO_FL_ERROR)
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002286 return 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002287
Olivier Houchard7505f942018-08-21 18:10:44 +02002288
Willy Tarreaua2af5122017-10-09 11:56:46 +02002289 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2290 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002291 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002292 }
2293
Willy Tarreaubc933932017-10-09 16:21:43 +02002294 /* This loop is quite simple : it tries to fill as much as it can from
2295 * pending streams into the existing buffer until it's reportedly full
2296 * or the end of send requests is reached. Then it tries to send this
2297 * buffer's contents out, marks it not full if at least one byte could
2298 * be sent, and tries again.
2299 *
2300 * The snd_buf() function normally takes a "flags" argument which may
2301 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2302 * data immediately comes and CO_SFL_STREAMER to indicate that the
2303 * connection is streaming lots of data (used to increase TLS record
2304 * size at the expense of latency). The former can be sent any time
2305 * there's a buffer full flag, as it indicates at least one stream
2306 * attempted to send and failed so there are pending data. An
2307 * alternative would be to set it as long as there's an active stream
2308 * but that would be problematic for ACKs until we have an absolute
2309 * guarantee that all waiters have at least one byte to send. The
2310 * latter should possibly not be set for now.
2311 */
2312
2313 done = 0;
2314 while (!done) {
2315 unsigned int flags = 0;
2316
2317 /* fill as much as we can into the current buffer */
2318 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2319 done = h2_process_mux(h2c);
2320
2321 if (conn->flags & CO_FL_ERROR)
2322 break;
2323
2324 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2325 flags |= CO_SFL_MSG_MORE;
2326
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002327 if (b_data(&h2c->mbuf)) {
2328 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002329 if (!ret)
2330 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002331 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002332 b_del(&h2c->mbuf, ret);
2333 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002334 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002335
2336 /* wrote at least one byte, the buffer is not full anymore */
2337 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2338 }
2339
Willy Tarreaua2af5122017-10-09 11:56:46 +02002340 if (conn->flags & CO_FL_SOCK_WR_SH) {
2341 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002342 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002343 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002344 /* We're not full anymore, so we can wake any task that are waiting
2345 * for us.
2346 */
2347 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002348 while (!LIST_ISEMPTY(&h2c->send_list)) {
2349 struct wait_list *sw = LIST_ELEM(h2c->send_list.n,
Olivier Houchard6ff20392018-07-17 18:46:31 +02002350 struct wait_list *, list);
2351 LIST_DEL(&sw->list);
2352 LIST_INIT(&sw->list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002353 sw->wait_reason &= ~SUB_CAN_SEND;
2354 tasklet_wakeup(sw->task);
2355 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002356 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002357 /* We're done, no more to send */
2358 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002359 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002360schedule:
2361 if (LIST_ISEMPTY(&h2c->wait_list.list))
2362 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_list);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002363 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002364}
2365
2366static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2367{
2368 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002369 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002370
2371 if (!(h2c->wait_list.wait_reason & SUB_CAN_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002372 ret = h2_send(h2c);
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002373 if (!(h2c->wait_list.wait_reason & SUB_CAN_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002374 ret |= h2_recv(h2c);
2375 if (ret)
2376 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002377 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002378}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002379
Willy Tarreau62f52692017-10-08 23:01:42 +02002380/* callback called on any event by the connection handler.
2381 * It applies changes and returns zero, or < 0 if it wants immediate
2382 * destruction of the connection (which normally doesn not happen in h2).
2383 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002384static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002385{
Olivier Houchard7505f942018-08-21 18:10:44 +02002386 struct connection *conn = h2c->conn;
Willy Tarreau8ec14062017-12-30 18:08:13 +01002387 struct session *sess = conn->owner;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002388
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002389 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002390 h2_process_demux(h2c);
2391
2392 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002393 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002394
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002395 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002396 h2c->flags &= ~H2_CF_DEM_DFULL;
2397 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002398 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002399
Willy Tarreau8ec14062017-12-30 18:08:13 +01002400 if (sess && unlikely(sess->fe->state == PR_STSTOPPED)) {
2401 /* frontend is stopping, reload likely in progress, let's try
2402 * to announce a graceful shutdown if not yet done. We don't
2403 * care if it fails, it will be tried again later.
2404 */
2405 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2406 if (h2c->last_sid < 0)
2407 h2c->last_sid = (1U << 31) - 1;
2408 h2c_send_goaway_error(h2c, NULL);
2409 }
2410 }
2411
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002412 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002413 * If we received early data, and the handshake is done, wake
2414 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002415 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002416 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2417 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2418 struct eb32_node *node;
2419 struct h2s *h2s;
2420
2421 h2c->flags |= H2_CF_WAIT_FOR_HS;
2422 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2423
2424 while (node) {
2425 h2s = container_of(node, struct h2s, by_id);
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002426 if ((h2s->cs->flags & CS_FL_WAIT_FOR_HS) &&
2427 h2s->recv_wait_list) {
2428 struct wait_list *sw = h2s->recv_wait_list;
2429 sw->wait_reason &= ~SUB_CAN_RECV;
2430 tasklet_wakeup(sw->task);
2431 h2s->recv_wait_list = NULL;
2432 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002433 node = eb32_next(node);
2434 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002435 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002436
Willy Tarreau26bd7612017-10-09 16:47:04 +02002437 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002438 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2439 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2440 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002441 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002442
2443 if (eb_is_empty(&h2c->streams_by_id)) {
2444 /* no more stream, kill the connection now */
2445 h2_release(conn);
2446 return -1;
2447 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002448 }
2449
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002450 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002451 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002452
2453 /* stop being notified of incoming data if we can't process them */
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002454 if (!h2_recv_allowed(h2c))
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002455 __conn_xprt_stop_recv(conn);
Olivier Houchard7505f942018-08-21 18:10:44 +02002456 else
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002457 __conn_xprt_want_recv(conn);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002458
2459 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002460 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002461 h2c->st0 != H2_CS_ERROR2 && !(h2c->flags & H2_CF_GOAWAY_FAILED) &&
Willy Tarreau51606832017-10-17 15:30:07 +02002462 (h2c->st0 == H2_CS_ERROR ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002463 b_data(&h2c->mbuf) ||
Willy Tarreau51606832017-10-17 15:30:07 +02002464 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2465 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002466 __conn_xprt_want_send(conn);
2467 }
2468 else {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002469 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002470 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002471 }
2472
Willy Tarreau3f133572017-10-31 19:21:06 +01002473 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002474 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002475 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002476 task_queue(h2c->task);
2477 }
2478 else
2479 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002480 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002481
Olivier Houchard7505f942018-08-21 18:10:44 +02002482 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002483 return 0;
2484}
2485
Willy Tarreauea392822017-10-31 10:02:25 +01002486/* Connection timeout management. The principle is that if there's no receipt
2487 * nor sending for a certain amount of time, the connection is closed. If the
2488 * MUX buffer still has lying data or is not allocatable, the connection is
2489 * immediately killed. If it's allocatable and empty, we attempt to send a
2490 * GOAWAY frame.
2491 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002492static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002493{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002494 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002495 int expired = tick_is_expired(t->expire, now_ms);
2496
Willy Tarreau0975f112018-03-29 15:22:59 +02002497 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002498 return t;
2499
Willy Tarreau0975f112018-03-29 15:22:59 +02002500 task_delete(t);
2501 task_free(t);
2502
2503 if (!h2c) {
2504 /* resources were already deleted */
2505 return NULL;
2506 }
2507
2508 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002509 h2c_error(h2c, H2_ERR_NO_ERROR);
2510 h2_wake_some_streams(h2c, 0, 0);
2511
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002512 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002513 /* don't even try to send a GOAWAY, the buffer is stuck */
2514 h2c->flags |= H2_CF_GOAWAY_FAILED;
2515 }
2516
2517 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002518 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002519 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2520 h2c->flags |= H2_CF_GOAWAY_FAILED;
2521
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002522 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2523 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002524 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002525 b_del(&h2c->mbuf, ret);
2526 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002527 }
2528 }
Willy Tarreauea392822017-10-31 10:02:25 +01002529
Willy Tarreau0975f112018-03-29 15:22:59 +02002530 /* either we can release everything now or it will be done later once
2531 * the last stream closes.
2532 */
2533 if (eb_is_empty(&h2c->streams_by_id))
2534 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002535
Willy Tarreauea392822017-10-31 10:02:25 +01002536 return NULL;
2537}
2538
2539
Willy Tarreau62f52692017-10-08 23:01:42 +02002540/*******************************************/
2541/* functions below are used by the streams */
2542/*******************************************/
2543
2544/*
2545 * Attach a new stream to a connection
2546 * (Used for outgoing connections)
2547 */
2548static struct conn_stream *h2_attach(struct connection *conn)
2549{
2550 return NULL;
2551}
2552
2553/* callback used to update the mux's polling flags after changing a cs' status.
2554 * The caller (cs_update_mux_polling) will take care of propagating any changes
2555 * to the transport layer.
2556 */
2557static void h2_update_poll(struct conn_stream *cs)
2558{
Willy Tarreau1d393222017-10-17 10:26:19 +02002559 struct h2s *h2s = cs->ctx;
2560
2561 if (!h2s)
2562 return;
2563
Willy Tarreaud7739c82017-10-30 15:38:23 +01002564 /* we may unblock a blocked read */
2565
Willy Tarreau315d8072017-12-10 22:17:57 +01002566 if (cs->flags & CS_FL_DATA_RD_ENA) {
2567 /* the stream indicates it's willing to read */
Willy Tarreaud7739c82017-10-30 15:38:23 +01002568 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreaud13bf272017-12-14 10:34:52 +01002569 if (h2s->h2c->dsi == h2s->id) {
Willy Tarreau315d8072017-12-10 22:17:57 +01002570 conn_xprt_want_recv(cs->conn);
Olivier Houchard7505f942018-08-21 18:10:44 +02002571 if (h2_recv(h2s->h2c))
2572 h2_process(h2s->h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002573 conn_xprt_want_send(cs->conn);
2574 }
Willy Tarreaud7739c82017-10-30 15:38:23 +01002575 }
2576
Willy Tarreau1d393222017-10-17 10:26:19 +02002577 /* Note: the stream and stream-int code doesn't allow us to perform a
2578 * synchronous send() here unfortunately, because this code is called
2579 * as si_update() from the process_stream() context. This means that
2580 * we have to queue the current cs and defer its processing after the
2581 * connection's cs list is processed anyway.
2582 */
2583
2584 if (cs->flags & CS_FL_DATA_WR_ENA) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002585 if (!b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2586 conn_xprt_want_send(cs->conn);
2587 tasklet_wakeup(h2s->h2c->wait_list.task);
Willy Tarreau1d393222017-10-17 10:26:19 +02002588 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002589 /* We don't support unsubscribing from here, it shouldn't be a problem */
Willy Tarreau1d393222017-10-17 10:26:19 +02002590
2591 /* this can happen from within si_chk_snd() */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002592 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreau1d393222017-10-17 10:26:19 +02002593 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002594}
2595
2596/*
2597 * Detach the stream from the connection and possibly release the connection.
2598 */
2599static void h2_detach(struct conn_stream *cs)
2600{
Willy Tarreau60935142017-10-16 18:11:19 +02002601 struct h2s *h2s = cs->ctx;
2602 struct h2c *h2c;
2603
2604 cs->ctx = NULL;
2605 if (!h2s)
2606 return;
2607
2608 h2c = h2s->h2c;
Olivier Houchard70d0d182018-09-12 17:56:08 +02002609 /* If the stream we're detaching waited for more data, unsubscribe it now */
2610 if (h2s->recv_wait_list && !((long)h2s->recv_wait_list->handle & 3))
2611 h2s->recv_wait_list = NULL;
Willy Tarreau60935142017-10-16 18:11:19 +02002612 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002613 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002614 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2615 !h2_has_too_many_cs(h2c)) {
2616 h2c->flags &= ~H2_CF_DEM_TOOMANY;
2617 if (h2_recv_allowed(h2c)) {
2618 __conn_xprt_want_recv(h2c->conn);
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002619 h2_recv(h2c);
Willy Tarreauf2101912018-07-19 10:11:38 +02002620 conn_xprt_want_send(h2c->conn);
2621 }
2622 }
Willy Tarreau60935142017-10-16 18:11:19 +02002623
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002624 /* this stream may be blocked waiting for some data to leave (possibly
2625 * an ES or RST frame), so orphan it in this case.
2626 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002627 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002628 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002629 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002630 return;
2631
Willy Tarreau45f752e2017-10-30 15:44:59 +01002632 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2633 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2634 /* unblock the connection if it was blocked on this
2635 * stream.
2636 */
2637 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2638 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2639 conn_xprt_want_recv(cs->conn);
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002640 h2_recv(h2c);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002641 conn_xprt_want_send(cs->conn);
2642 }
2643
Willy Tarreau71049cc2018-03-28 13:56:39 +02002644 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002645
Willy Tarreaue323f342018-03-28 13:51:45 +02002646 /* We don't want to close right now unless we're removing the
2647 * last stream, and either the connection is in error, or it
2648 * reached the ID already specified in a GOAWAY frame received
2649 * or sent (as seen by last_sid >= 0).
2650 */
2651 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2652 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002653 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Willy Tarreaue323f342018-03-28 13:51:45 +02002654 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002655 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002656 (conn_xprt_read0_pending(h2c->conn) ||
2657 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2658 /* no more stream will come, kill it now */
2659 h2_release(h2c->conn);
2660 }
2661 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002662 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002663 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2664 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002665 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002666 else
2667 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002668 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002669}
2670
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002671static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002672{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002673 struct h2c *h2c = h2s->h2c;
2674 struct wait_list *sw = &h2s->wait_list;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002675
Willy Tarreau721c9742017-11-07 11:05:42 +01002676 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002677 return;
2678
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002679 /* if no outgoing data was seen on this stream, it means it was
2680 * closed with a "tcp-request content" rule that is normally
2681 * used to kill the connection ASAP (eg: limit abuse). In this
2682 * case we send a goaway to close the connection.
2683 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002684 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002685 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002686 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002687
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002688 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2689 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002690 h2c_send_goaway_error(h2c, h2s) <= 0)
2691 return;
2692 if (b_data(&h2c->mbuf) && !(h2c->conn->flags & CO_FL_XPRT_WR_ENA))
2693 conn_xprt_want_send(h2c->conn);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002694
Willy Tarreau00dd0782018-03-01 16:31:34 +01002695 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002696
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002697 return;
2698add_to_list:
2699 if (LIST_ISEMPTY(&sw->list)) {
2700 sw->wait_reason |= SUB_CAN_SEND;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002701 if (h2s->flags & H2_SF_BLK_MFCTL)
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002702 LIST_ADDQ(&h2c->fctl_list, &sw->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002703 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002704 LIST_ADDQ(&h2c->send_list, &sw->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002705 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002706 /* Let the handler know we want shutr */
2707 sw->handle = (void *)((long)sw->handle | 1);
2708
Willy Tarreau62f52692017-10-08 23:01:42 +02002709}
2710
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002711static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002712{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002713 struct h2c *h2c = h2s->h2c;
2714 struct wait_list *sw = &h2s->wait_list;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002715
Willy Tarreau721c9742017-11-07 11:05:42 +01002716 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002717 return;
2718
Willy Tarreau67434202017-11-06 20:20:51 +01002719 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002720 /* we can cleanly close using an empty data frame only after headers */
2721
2722 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2723 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002724 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002725
2726 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002727 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002728 else
2729 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002730 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002731 /* if no outgoing data was seen on this stream, it means it was
2732 * closed with a "tcp-request content" rule that is normally
2733 * used to kill the connection ASAP (eg: limit abuse). In this
2734 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002735 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002736 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002737 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002738 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002739
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002740 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2741 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002742 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002743 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002744
Willy Tarreau00dd0782018-03-01 16:31:34 +01002745 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002746 }
2747
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002748 if (b_data(&h2s->h2c->mbuf) && !(h2c->conn->flags & CO_FL_XPRT_WR_ENA))
2749 conn_xprt_want_send(h2c->conn);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002750
2751 add_to_list:
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002752 sw = &h2s->wait_list;
2753
2754 if (LIST_ISEMPTY(&sw->list)) {
2755 sw->wait_reason |= SUB_CAN_SEND;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002756 if (h2s->flags & H2_SF_BLK_MFCTL)
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002757 LIST_ADDQ(&h2s->h2c->fctl_list, &sw->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002758 else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM))
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002759 LIST_ADDQ(&h2s->h2c->send_list, &sw->list);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002760 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002761 /* let the handler know we want to shutr */
2762 sw->handle = (void *)((long)(sw->handle) | 2);
2763}
2764
2765static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
2766{
2767 struct h2s *h2s = ctx;
2768 long reason = (long)h2s->wait_list.handle;
2769
2770 if (reason & 1)
2771 h2_do_shutr(h2s);
2772 if (reason & 2)
2773 h2_do_shutw(h2s);
2774
2775 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02002776}
2777
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002778static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2779{
2780 struct h2s *h2s = cs->ctx;
2781
2782 if (!mode)
2783 return;
2784
2785 h2_do_shutr(h2s);
2786}
2787
2788static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2789{
2790 struct h2s *h2s = cs->ctx;
2791
2792 h2_do_shutw(h2s);
2793}
2794
Willy Tarreau13278b42017-10-13 19:23:14 +02002795/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2796 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2797 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002798 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002799 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002800static int h2_frt_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002801{
2802 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002803 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02002804 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002805 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02002806 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002807 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01002808 struct buffer *csbuf;
Willy Tarreau13278b42017-10-13 19:23:14 +02002809 int flen = h2c->dfl;
2810 int outlen = 0;
2811 int wrap;
2812 int try;
2813
2814 if (!h2c->dfl) {
2815 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002816 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002817 return 0;
2818 }
2819
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002820 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01002821 return 0; // incomplete input frame
2822
Willy Tarreau13278b42017-10-13 19:23:14 +02002823 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002824 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02002825 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002826 copy = alloc_trash_chunk();
2827 if (!copy) {
2828 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2829 goto fail;
2830 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002831 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
2832 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
2833 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02002834 }
2835
2836 /* The padlen is the first byte before data, and the padding appears
2837 * after data. padlen+data+padding are included in flen.
2838 */
2839 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002840 h2c->dpl = *hdrs;
2841 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002842 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2843 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002844 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002845 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002846 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002847 hdrs += 1; // skip Pad Length
2848 }
2849
2850 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2851 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002852 if (read_n32(hdrs) == h2s->id) {
2853 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2854 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002855 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002856 }
2857
Willy Tarreau13278b42017-10-13 19:23:14 +02002858 hdrs += 5; // stream dep = 4, weight = 1
2859 flen -= 5;
2860 }
2861
2862 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2863 * don't support this for now and can't even decompress so we have to
2864 * break the connection.
2865 */
2866 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2867 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002868 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002869 }
2870
Olivier Houchard638b7992018-08-16 15:41:52 +02002871 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01002872 if (!csbuf) {
2873 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002874 goto fail;
2875 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002876
Willy Tarreau937f7602018-02-26 15:22:17 +01002877 /* we can't retry a failed decompression operation so we must be very
2878 * careful not to take any risks. In practice the output buffer is
2879 * always empty except maybe for trailers, in which case we simply have
2880 * to wait for the upper layer to finish consuming what is available.
2881 */
2882 if (b_data(csbuf))
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002883 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002884
Willy Tarreau937f7602018-02-26 15:22:17 +01002885 csbuf->head = 0;
2886 try = b_size(csbuf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002887
2888 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2889 sizeof(list)/sizeof(list[0]), tmp);
2890 if (outlen < 0) {
2891 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2892 goto fail;
2893 }
2894
2895 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002896 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreau937f7602018-02-26 15:22:17 +01002897 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002898
2899 if (outlen < 0) {
2900 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2901 goto fail;
2902 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002903
Willy Tarreau174b06a2018-04-25 18:13:58 +02002904 if (msgf & H2_MSGF_BODY) {
2905 /* a payload is present */
2906 if (msgf & H2_MSGF_BODY_CL)
2907 h2s->flags |= H2_SF_DATA_CLEN;
2908 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
2909 h2s->flags |= H2_SF_DATA_CHNK;
2910 }
2911
Willy Tarreau13278b42017-10-13 19:23:14 +02002912 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002913 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02002914 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01002915 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02002916
Willy Tarreau39d68502018-03-02 12:26:37 +01002917 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002918 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002919 h2s->cs->flags |= CS_FL_REOS;
2920 }
Willy Tarreau937f7602018-02-26 15:22:17 +01002921
Willy Tarreau68dd9852017-07-03 14:44:26 +02002922 leave:
2923 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002924 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002925 fail:
2926 outlen = 0;
2927 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002928}
2929
Willy Tarreau454f9052017-10-26 19:40:35 +02002930/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2931 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2932 * in use, a new chunk is emitted for each frame. This is supposed to fit
2933 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2934 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2935 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2936 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002937 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2938 * checked to know if some data remain pending (an empty DATA frame can return
2939 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2940 * connection errors in h2c->errcode. The caller must already have checked the
2941 * frame header and ensured that the frame was complete or the buffer full. It
2942 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002943 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002944static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02002945{
2946 struct h2c *h2c = h2s->h2c;
2947 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002948 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002949 unsigned int chklen = 0;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002950 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02002951
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002952 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002953
2954 /* The padlen is the first byte before data, and the padding appears
2955 * after data. padlen+data+padding are included in flen.
2956 */
Willy Tarreau79127812017-12-03 21:06:59 +01002957 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002958 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002959 return 0;
2960
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002961 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002962 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002963 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2964 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002965 return 0;
2966 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002967
2968 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002969 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002970 h2c->dfl--;
2971 h2c->rcvd_c++; h2c->rcvd_s++;
2972 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002973 }
2974
Olivier Houchard638b7992018-08-16 15:41:52 +02002975 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01002976 if (!csbuf) {
2977 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002978 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002979 }
2980
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002981 flen = h2c->dfl - h2c->dpl;
2982 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002983 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002984
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002985 if (flen > b_data(&h2c->dbuf)) {
2986 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002987 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01002988 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002989 }
2990
2991 if (unlikely(b_space_wraps(csbuf))) {
2992 /* it doesn't fit and the buffer is fragmented,
2993 * so let's defragment it and try again.
2994 */
2995 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02002996 }
2997
Willy Tarreaueba10f22018-04-25 20:44:22 +02002998 /* chunked-encoding requires more room */
2999 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003000 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003001 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3002 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3003 (chklen < 1048576) ? 4 : 8;
3004 chklen += 4; // CRLF, CRLF
3005 }
3006
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003007 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003008 if (flen + chklen > b_room(csbuf)) {
3009 if (chklen >= b_room(csbuf)) {
3010 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003011 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003012 }
3013 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003014 }
3015
3016 if (h2s->flags & H2_SF_DATA_CHNK) {
3017 /* emit the chunk size */
3018 unsigned int chksz = flen;
3019 char str[10];
3020 char *beg;
3021
3022 beg = str + sizeof(str);
3023 *--beg = '\n';
3024 *--beg = '\r';
3025 do {
3026 *--beg = hextab[chksz & 0xF];
3027 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003028 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003029 }
3030
Willy Tarreau454f9052017-10-26 19:40:35 +02003031 /* Block1 is the length of the first block before the buffer wraps,
3032 * block2 is the optional second block to reach the end of the frame.
3033 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003034 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003035 if (block1 > flen)
3036 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003037 block2 = flen - block1;
3038
3039 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003040 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003041
3042 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003043 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003044
Willy Tarreaueba10f22018-04-25 20:44:22 +02003045 if (h2s->flags & H2_SF_DATA_CHNK) {
3046 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003047 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003048 }
3049
Willy Tarreau454f9052017-10-26 19:40:35 +02003050 /* now mark the input data as consumed (will be deleted from the buffer
3051 * by the caller when seeing FRAME_A after sending the window update).
3052 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003053 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003054 h2c->dfl -= flen;
3055 h2c->rcvd_c += flen;
3056 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3057
3058 if (h2c->dfl > h2c->dpl) {
3059 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003060 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003061 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003062 }
3063
Willy Tarreau4a28da12018-01-04 14:41:00 +01003064 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003065 /* here we're done with the frame, all the payload (except padding) was
3066 * transferred.
3067 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003068
3069 if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) {
3070 /* emit the trailing 0 CRLF CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003071 if (b_room(csbuf) < 5) {
3072 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003073 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003074 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003075 chklen += 5;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003076 b_putblk(csbuf, "0\r\n\r\n", 5);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003077 }
3078
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003079 h2c->rcvd_c += h2c->dpl;
3080 h2c->rcvd_s += h2c->dpl;
3081 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003082 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3083
Willy Tarreau39d68502018-03-02 12:26:37 +01003084 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003085 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003086 h2s->cs->flags |= CS_FL_REOS;
3087 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003088
Willy Tarreau454b57b2018-02-26 15:50:05 +01003089 return flen + chklen;
3090 fail:
3091 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003092}
3093
Willy Tarreau5dd17352018-06-14 13:33:30 +02003094/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3095 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3096 * number of bytes sent. The caller must check the stream's status to detect
3097 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003098 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003099static 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 +02003100{
3101 struct http_hdr list[MAX_HTTP_HDR];
3102 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003103 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003104 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003105 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003106 int es_now = 0;
3107 int ret = 0;
3108 int hdr;
3109
3110 if (h2c_mux_busy(h2c, h2s)) {
3111 h2s->flags |= H2_SF_BLK_MBUSY;
3112 return 0;
3113 }
3114
Willy Tarreau44e973f2018-03-01 17:49:30 +01003115 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003116 h2c->flags |= H2_CF_MUX_MALLOC;
3117 h2s->flags |= H2_SF_BLK_MROOM;
3118 return 0;
3119 }
3120
3121 /* First, try to parse the H1 response and index it into <list>.
3122 * NOTE! Since it comes from haproxy, we *know* that a response header
3123 * block does not wrap and we can safely read it this way without
3124 * having to realign the buffer.
3125 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003126 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003127 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003128 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003129 /* incomplete or invalid response, this is abnormal coming from
3130 * haproxy and may only result in a bad errorfile or bad Lua code
3131 * so that won't be fixed, raise an error now.
3132 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003133 * FIXME: we should instead add the ability to only return a
3134 * 502 bad gateway. But in theory this is not supposed to
3135 * happen.
3136 */
3137 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3138 ret = 0;
3139 goto end;
3140 }
3141
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003142 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003143
3144 /* certain statuses have no body or an empty one, regardless of
3145 * what the headers say.
3146 */
3147 if (sl.st.status >= 100 && sl.st.status < 200) {
3148 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3149 h1m->curr_len = h1m->body_len = 0;
3150 }
3151 else if (sl.st.status == 204 || sl.st.status == 304) {
3152 /* no contents, claim c-len is present and set to zero */
3153 h1m->flags &= ~H1_MF_CHNK;
3154 h1m->flags |= H1_MF_CLEN;
3155 h1m->curr_len = h1m->body_len = 0;
3156 }
3157
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003158 chunk_reset(&outbuf);
3159
3160 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003161 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003162 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003163 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003164
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003165 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003166 break;
3167 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003168 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003169 }
3170
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003171 if (outbuf.size < 9)
3172 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003173
3174 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003175 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3176 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3177 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003178
3179 /* encode status, which necessarily is the first one */
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003180 if (outbuf.data < outbuf.size && h2s->status == 200)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003181 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003182 else if (outbuf.data < outbuf.size && h2s->status == 304)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003183 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003184 else if (unlikely(list[0].v.len != 3)) {
3185 /* this is an unparsable response */
3186 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3187 ret = 0;
3188 goto end;
3189 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003190 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003191 /* basic encoding of the status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003192 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3193 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3194 outbuf.area[outbuf.data++] = list[0].v.ptr[0];
3195 outbuf.area[outbuf.data++] = list[0].v.ptr[1];
3196 outbuf.area[outbuf.data++] = list[0].v.ptr[2];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003197 }
3198 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003199 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003200 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003201 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003202 }
3203
3204 /* encode all headers, stop at empty name */
3205 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003206 /* these ones do not exist in H2 and must be dropped. */
3207 if (isteq(list[hdr].n, ist("connection")) ||
3208 isteq(list[hdr].n, ist("proxy-connection")) ||
3209 isteq(list[hdr].n, ist("keep-alive")) ||
3210 isteq(list[hdr].n, ist("upgrade")) ||
3211 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003212 continue;
3213
3214 if (isteq(list[hdr].n, ist("")))
3215 break; // end
3216
3217 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3218 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003219 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003220 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003221 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003222 }
3223 }
3224
3225 /* we may need to add END_STREAM */
3226 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3227 es_now = 1;
3228
3229 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003230 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003231
3232 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003233 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003234
3235 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003236 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003237
3238 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003239 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003240 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003241
3242 /* for now we don't implemented CONTINUATION, so we wait for a
3243 * body or directly end in TRL2.
3244 */
3245 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003246 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003247 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003248
Willy Tarreau801250e2018-09-11 11:45:04 +02003249 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003250 h2s->flags |= H2_SF_ES_SENT;
3251 if (h2s->st == H2_SS_OPEN)
3252 h2s->st = H2_SS_HLOC;
3253 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003254 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003255 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003256 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003257 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003258 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003259 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003260 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003261 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003262 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003263
3264 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003265
3266 end:
3267 //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));
3268 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003269 full:
3270 h1m_init_res(h1m);
3271 h1m->err_pos = -1; // don't care about errors on the response path
3272 h2c->flags |= H2_CF_MUX_MFULL;
3273 h2s->flags |= H2_SF_BLK_MROOM;
3274 ret = 0;
3275 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003276}
3277
Willy Tarreau5dd17352018-06-14 13:33:30 +02003278/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3279 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3280 * the number of bytes sent. The caller must check the stream's status to
3281 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003282 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003283static 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 +02003284{
3285 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003286 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003287 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003288 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003289 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003290 int es_now = 0;
3291 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003292 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003293 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003294
3295 if (h2c_mux_busy(h2c, h2s)) {
3296 h2s->flags |= H2_SF_BLK_MBUSY;
3297 goto end;
3298 }
3299
Willy Tarreau44e973f2018-03-01 17:49:30 +01003300 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003301 h2c->flags |= H2_CF_MUX_MALLOC;
3302 h2s->flags |= H2_SF_BLK_MROOM;
3303 goto end;
3304 }
3305
3306 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003307 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003308 goto end;
3309
3310 chunk_reset(&outbuf);
3311
3312 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003313 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003314 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003315 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003316
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003317 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003318 break;
3319 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003320 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003321 }
3322
3323 if (outbuf.size < 9) {
3324 h2c->flags |= H2_CF_MUX_MFULL;
3325 h2s->flags |= H2_SF_BLK_MROOM;
3326 goto end;
3327 }
3328
3329 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003330 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3331 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3332 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003333
3334 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3335 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003336 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003337 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003338 break;
3339 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003340 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003341 if ((long long)size > h1m->curr_len)
3342 size = h1m->curr_len;
3343 break;
3344 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003345 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003346 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003347 if (!ret)
3348 goto end;
3349
3350 if (ret < 0) {
3351 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003352 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003353 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3354 goto end;
3355 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003356 max -= ret;
3357 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003358 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003359 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003360 }
3361
Willy Tarreau801250e2018-09-11 11:45:04 +02003362 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003363 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003364 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003365 if (!ret)
3366 goto end;
3367
3368 if (ret < 0) {
3369 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003370 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003371 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3372 goto end;
3373 }
3374
3375 size = chunk;
3376 h1m->curr_len = chunk;
3377 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003378 max -= ret;
3379 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003380 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003381 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003382 if (!size)
3383 goto send_empty;
3384 }
3385
3386 /* in MSG_DATA state, continue below */
3387 size = h1m->curr_len;
3388 break;
3389 }
3390
3391 /* we have in <size> the exact number of bytes we need to copy from
3392 * the H1 buffer. We need to check this against the connection's and
3393 * the stream's send windows, and to ensure that this fits in the max
3394 * frame size and in the buffer's available space minus 9 bytes (for
3395 * the frame header). The connection's flow control is applied last so
3396 * that we can use a separate list of streams which are immediately
3397 * unblocked on window opening. Note: we don't implement padding.
3398 */
3399
Willy Tarreau5dd17352018-06-14 13:33:30 +02003400 if (size > max)
3401 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003402
3403 if (size > h2s->mws)
3404 size = h2s->mws;
3405
3406 if (size <= 0) {
3407 h2s->flags |= H2_SF_BLK_SFCTL;
3408 goto end;
3409 }
3410
3411 if (h2c->mfs && size > h2c->mfs)
3412 size = h2c->mfs;
3413
3414 if (size + 9 > outbuf.size) {
3415 /* we have an opportunity for enlarging the too small
3416 * available space, let's try.
3417 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003418 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003419 goto realign_again;
3420 size = outbuf.size - 9;
3421 }
3422
3423 if (size <= 0) {
3424 h2c->flags |= H2_CF_MUX_MFULL;
3425 h2s->flags |= H2_SF_BLK_MROOM;
3426 goto end;
3427 }
3428
3429 if (size > h2c->mws)
3430 size = h2c->mws;
3431
3432 if (size <= 0) {
3433 h2s->flags |= H2_SF_BLK_MFCTL;
3434 goto end;
3435 }
3436
3437 /* copy whatever we can */
3438 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003439 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003440 if (ret == 1)
3441 len2 = 0;
3442
3443 if (!ret || len1 + len2 < size) {
3444 /* FIXME: must normally never happen */
3445 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3446 goto end;
3447 }
3448
3449 /* limit len1/len2 to size */
3450 if (len1 + len2 > size) {
3451 int sub = len1 + len2 - size;
3452
3453 if (len2 > sub)
3454 len2 -= sub;
3455 else {
3456 sub -= len2;
3457 len2 = 0;
3458 len1 -= sub;
3459 }
3460 }
3461
3462 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003463 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003464 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003465 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003466
3467 send_empty:
3468 /* we may need to add END_STREAM */
3469 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3470 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003471 *
3472 * FIXME: what we do here is not correct because we send end_stream
3473 * before knowing if we'll have to send a HEADERS frame for the
3474 * trailers. More importantly we're not consuming the trailing CRLF
3475 * after the end of trailers, so it will be left to the caller to
3476 * eat it. The right way to do it would be to measure trailers here
3477 * and to send ES only if there are no trailers.
3478 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003479 */
3480 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02003481 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003482 es_now = 1;
3483
3484 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003485 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003486
3487 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003488 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003489
3490 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003491 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003492
3493 /* consume incoming H1 response */
3494 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003495 max -= size;
3496 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003497 total += size;
3498 h1m->curr_len -= size;
3499 h2s->mws -= size;
3500 h2c->mws -= size;
3501
3502 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02003503 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003504 goto new_frame;
3505 }
3506 }
3507
3508 if (es_now) {
3509 if (h2s->st == H2_SS_OPEN)
3510 h2s->st = H2_SS_HLOC;
3511 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003512 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003513
Willy Tarreau35a62702018-02-27 15:37:25 +01003514 if (!(h1m->flags & H1_MF_CHNK)) {
3515 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003516 total += max;
3517 ofs += max;
3518 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003519
Willy Tarreau801250e2018-09-11 11:45:04 +02003520 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003521 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003522
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003523 h2s->flags |= H2_SF_ES_SENT;
3524 }
3525
3526 end:
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02003527 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 +02003528 return total;
3529}
3530
Olivier Houchard6ff20392018-07-17 18:46:31 +02003531/* Called from the upper layer, to subscribe to events, such as being able to send */
3532static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
3533{
3534 struct wait_list *sw;
3535 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003536 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003537
3538 switch (event_type) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003539 case SUB_CAN_RECV:
3540 sw = param;
3541 if (!(sw->wait_reason & SUB_CAN_RECV)) {
3542 sw->wait_reason |= SUB_CAN_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003543 sw->handle = h2s;
Olivier Houchardaf4021e2018-08-09 13:06:55 +02003544 h2s->recv_wait_list = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003545 }
3546 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003547 case SUB_CAN_SEND:
3548 sw = param;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003549 if (!(sw->wait_reason & SUB_CAN_SEND)) {
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003550 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003551 sw->handle = h2s;
3552 if (h2s->flags & H2_SF_BLK_MFCTL)
3553 LIST_ADDQ(&h2c->fctl_list, &sw->list);
3554 else
3555 LIST_ADDQ(&h2c->send_list, &sw->list);
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003556 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02003557 return 0;
3558 default:
3559 break;
3560 }
3561 return -1;
3562
3563
3564}
3565
Olivier Houchard511efea2018-08-16 15:30:32 +02003566/* Called from the upper layer, to receive data */
3567static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3568{
Olivier Houchard638b7992018-08-16 15:41:52 +02003569 struct h2s *h2s = cs->ctx;
Olivier Houchard511efea2018-08-16 15:30:32 +02003570 size_t ret = 0;
3571
3572 /* transfer possibly pending data to the upper layer */
Olivier Houchard638b7992018-08-16 15:41:52 +02003573 ret = b_xfer(buf, &h2s->rxbuf, count);
Olivier Houchard511efea2018-08-16 15:30:32 +02003574
Olivier Houchard638b7992018-08-16 15:41:52 +02003575 if (b_data(&h2s->rxbuf))
Olivier Houchard511efea2018-08-16 15:30:32 +02003576 cs->flags |= CS_FL_RCV_MORE;
3577 else {
3578 cs->flags &= ~CS_FL_RCV_MORE;
3579 if (cs->flags & CS_FL_REOS)
3580 cs->flags |= CS_FL_EOS;
Olivier Houchard638b7992018-08-16 15:41:52 +02003581 if (b_size(&h2s->rxbuf)) {
3582 b_free(&h2s->rxbuf);
3583 offer_buffers(NULL, tasks_run_queue);
3584 }
Olivier Houchard511efea2018-08-16 15:30:32 +02003585 }
3586
3587 return ret;
3588}
3589
Willy Tarreau62f52692017-10-08 23:01:42 +02003590/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003591static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02003592{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003593 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003594 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003595 size_t ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003596
Willy Tarreau0bad0432018-06-14 16:54:01 +02003597 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01003598 h2s->flags |= H2_SF_OUTGOING_DATA;
3599
Willy Tarreaua40704a2018-09-11 13:52:04 +02003600 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02003601 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003602 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003603 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02003604 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003605 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003606 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02003607 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003608 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003609 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003610
Willy Tarreau5dd17352018-06-14 13:33:30 +02003611 if (unlikely((int)ret <= 0)) {
3612 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003613 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3614 break;
3615 }
Willy Tarreau35a62702018-02-27 15:37:25 +01003616 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02003617 total += count;
3618 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003619 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003620 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003621 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003622 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003623 cs->flags |= CS_FL_ERROR;
3624 break;
3625 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02003626
3627 total += ret;
3628 count -= ret;
3629
3630 if (h2s->st >= H2_SS_ERROR)
3631 break;
3632
3633 if (h2s->flags & H2_SF_BLK_ANY)
3634 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003635 }
3636
Willy Tarreau00610962018-07-19 10:58:28 +02003637 if (h2s->st >= H2_SS_ERROR) {
3638 /* trim any possibly pending data after we close (extra CR-LF,
3639 * unprocessed trailers, abnormal extra data, ...)
3640 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003641 total += count;
3642 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02003643 }
3644
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003645 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003646 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003647 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003648 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003649 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003650 }
3651
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003652 b_del(buf, total);
Olivier Houchard7505f942018-08-21 18:10:44 +02003653 if (total > 0) {
Olivier Houchardfab7c7e2018-08-21 16:36:10 +02003654 conn_xprt_want_send(h2s->h2c->conn);
Olivier Houchard7505f942018-08-21 18:10:44 +02003655 if (!(h2s->h2c->wait_list.wait_reason & SUB_CAN_SEND))
3656 tasklet_wakeup(h2s->h2c->wait_list.task);
3657 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003658 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003659}
3660
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003661/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02003662static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003663{
3664 struct h2c *h2c = conn->mux_ctx;
3665 struct h2s *h2s;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003666 struct wait_list *sw;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003667 struct eb32_node *node;
3668 int fctl_cnt = 0;
3669 int send_cnt = 0;
3670 int tree_cnt = 0;
3671 int orph_cnt = 0;
3672
3673 if (!h2c)
3674 return;
3675
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003676 list_for_each_entry(sw, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003677 fctl_cnt++;
3678
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003679 list_for_each_entry(sw, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003680 send_cnt++;
3681
3682 node = eb32_first(&h2c->streams_by_id);
3683 while (node) {
3684 h2s = container_of(node, struct h2s, by_id);
3685 tree_cnt++;
3686 if (!h2s->cs)
3687 orph_cnt++;
3688 node = eb32_next(node);
3689 }
3690
Willy Tarreau616ac812018-07-24 14:12:42 +02003691 chunk_appendf(msg, " st0=%d err=%d maxid=%d lastid=%d flg=0x%08x nbst=%u nbcs=%u"
3692 " fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
3693 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
3694 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
3695 (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
3696 (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003697}
Willy Tarreau62f52692017-10-08 23:01:42 +02003698
3699/*******************************************************/
3700/* functions below are dedicated to the config parsers */
3701/*******************************************************/
3702
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003703/* config parser for global "tune.h2.header-table-size" */
3704static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
3705 struct proxy *defpx, const char *file, int line,
3706 char **err)
3707{
3708 if (too_many_args(1, args, err, NULL))
3709 return -1;
3710
3711 h2_settings_header_table_size = atoi(args[1]);
3712 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3713 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3714 return -1;
3715 }
3716 return 0;
3717}
Willy Tarreau62f52692017-10-08 23:01:42 +02003718
Willy Tarreaue6baec02017-07-27 11:45:11 +02003719/* config parser for global "tune.h2.initial-window-size" */
3720static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
3721 struct proxy *defpx, const char *file, int line,
3722 char **err)
3723{
3724 if (too_many_args(1, args, err, NULL))
3725 return -1;
3726
3727 h2_settings_initial_window_size = atoi(args[1]);
3728 if (h2_settings_initial_window_size < 0) {
3729 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3730 return -1;
3731 }
3732 return 0;
3733}
3734
Willy Tarreau5242ef82017-07-27 11:47:28 +02003735/* config parser for global "tune.h2.max-concurrent-streams" */
3736static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3737 struct proxy *defpx, const char *file, int line,
3738 char **err)
3739{
3740 if (too_many_args(1, args, err, NULL))
3741 return -1;
3742
3743 h2_settings_max_concurrent_streams = atoi(args[1]);
3744 if (h2_settings_max_concurrent_streams < 0) {
3745 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3746 return -1;
3747 }
3748 return 0;
3749}
3750
Willy Tarreau62f52692017-10-08 23:01:42 +02003751
3752/****************************************/
3753/* MUX initialization and instanciation */
3754/***************************************/
3755
3756/* The mux operations */
3757const struct mux_ops h2_ops = {
3758 .init = h2_init,
Willy Tarreau62f52692017-10-08 23:01:42 +02003759 .update_poll = h2_update_poll,
Willy Tarreau62f52692017-10-08 23:01:42 +02003760 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02003761 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02003762 .subscribe = h2_subscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02003763 .attach = h2_attach,
3764 .detach = h2_detach,
3765 .shutr = h2_shutr,
3766 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003767 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003768 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003769 .name = "H2",
3770};
3771
Christopher Faulet32f61c02018-04-10 14:33:41 +02003772/* PROTO selection : this mux registers PROTO token "h2" */
3773static struct mux_proto_list mux_proto_h2 =
3774 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02003775
3776/* config keyword parsers */
3777static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003778 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003779 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003780 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003781 { 0, NULL, NULL }
3782}};
3783
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003784static void __h2_deinit(void)
3785{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003786 pool_destroy(pool_head_h2s);
3787 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003788}
3789
Willy Tarreau62f52692017-10-08 23:01:42 +02003790__attribute__((constructor))
3791static void __h2_init(void)
3792{
Christopher Faulet32f61c02018-04-10 14:33:41 +02003793 register_mux_proto(&mux_proto_h2);
Willy Tarreau62f52692017-10-08 23:01:42 +02003794 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003795 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003796 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3797 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003798}