blob: fc34f7cc548df8bb18d4de393e724df82138d8df [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 Houchardfa8aa862018-10-10 18:25:41 +0200123 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
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 Houchardfa8aa862018-10-10 18:25:41 +0200186 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
187 struct wait_event *recv_wait; /* Address of the wait_event the conn_stream associated is waiting on */
188 struct wait_event *send_wait; /* The streeam is waiting for flow control */
189 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Willy Tarreau18312642017-10-11 07:57:07 +0200190};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200191
Willy Tarreauc6405142017-09-21 20:23:50 +0200192/* descriptor for an h2 frame header */
193struct h2_fh {
194 uint32_t len; /* length, host order, 24 bits */
195 uint32_t sid; /* stream id, host order, 31 bits */
196 uint8_t ft; /* frame type */
197 uint8_t ff; /* frame flags */
198};
199
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200200/* a few settings from the global section */
201static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200202static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5242ef82017-07-27 11:47:28 +0200203static int h2_settings_max_concurrent_streams = 100;
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200204
Willy Tarreau2a856182017-05-16 15:20:39 +0200205/* a dmumy closed stream */
206static const struct h2s *h2_closed_stream = &(const struct h2s){
207 .cs = NULL,
208 .h2c = NULL,
209 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100210 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100211 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200212 .id = 0,
213};
214
215/* and a dummy idle stream for use with any unannounced stream */
216static const struct h2s *h2_idle_stream = &(const struct h2s){
217 .cs = NULL,
218 .h2c = NULL,
219 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100220 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200221 .id = 0,
222};
223
Olivier Houchard9f6af332018-05-25 14:04:04 +0200224static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200225static int h2_send(struct h2c *h2c);
226static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200227static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200228static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100229static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100230static int h2_frt_decode_headers(struct h2s *h2s);
231static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200232static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200233
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200234/*****************************************************/
235/* functions below are for dynamic buffer management */
236/*****************************************************/
237
Willy Tarreau315d8072017-12-10 22:17:57 +0100238/* indicates whether or not the we may call the h2_recv() function to attempt
239 * to receive data into the buffer and/or demux pending data. The condition is
240 * a bit complex due to some API limits for now. The rules are the following :
241 * - if an error or a shutdown was detected on the connection and the buffer
242 * is empty, we must not attempt to receive
243 * - if the demux buf failed to be allocated, we must not try to receive and
244 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100245 * - if no flag indicates a blocking condition, we may attempt to receive,
246 * regardless of whether the demux buffer is full or not, so that only
247 * de demux part decides whether or not to block. This is needed because
248 * the connection API indeed prevents us from re-enabling receipt that is
249 * already enabled in a polled state, so we must always immediately stop
250 * as soon as the demux can't proceed so as never to hit an end of read
251 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100252 * - otherwise must may not attempt
253 */
254static inline int h2_recv_allowed(const struct h2c *h2c)
255{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200256 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100257 (h2c->st0 >= H2_CS_ERROR ||
258 h2c->conn->flags & CO_FL_ERROR ||
259 conn_xprt_read0_pending(h2c->conn)))
260 return 0;
261
262 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100263 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100264 return 1;
265
266 return 0;
267}
268
Willy Tarreauf2101912018-07-19 10:11:38 +0200269/* returns true if the connection has too many conn_streams attached */
270static inline int h2_has_too_many_cs(const struct h2c *h2c)
271{
272 return h2c->nb_cs >= h2_settings_max_concurrent_streams;
273}
274
Willy Tarreau44e973f2018-03-01 17:49:30 +0100275/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
276 * flags are used to figure what buffer was requested. It returns 1 if the
277 * allocation succeeds, in which case the connection is woken up, or 0 if it's
278 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200279 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100280static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200281{
282 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100283 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200284
Willy Tarreau44e973f2018-03-01 17:49:30 +0100285 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200286 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200287 if (h2_recv_allowed(h2c)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200288 conn_xprt_want_recv(h2c->conn);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200289 tasklet_wakeup(h2c->wait_event.task);
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200290 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200291 return 1;
292 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200293
Willy Tarreau44e973f2018-03-01 17:49:30 +0100294 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
295 h2c->flags &= ~H2_CF_MUX_MALLOC;
296 if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY))
297 conn_xprt_want_send(h2c->conn);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200298
299 if (h2c->flags & H2_CF_DEM_MROOM) {
300 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200301 if (h2_recv_allowed(h2c)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200302 conn_xprt_want_recv(h2c->conn);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200303 tasklet_wakeup(h2c->wait_event.task);
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 Houchardfa8aa862018-10-10 18:25:41 +0200315 tasklet_wakeup(h2c->wait_event.task);
Olivier Houchardaf4021e2018-08-09 13:06:55 +0200316 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100317 return 1;
318 }
319
Willy Tarreau14398122017-09-22 14:26:04 +0200320 return 0;
321}
322
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200323static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200324{
325 struct buffer *buf = NULL;
326
Willy Tarreau44e973f2018-03-01 17:49:30 +0100327 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
328 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
329 h2c->buf_wait.target = h2c;
330 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100331 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100332 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100333 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200334 __conn_xprt_stop_recv(h2c->conn);
335 }
336 return buf;
337}
338
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200339static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200340{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200341 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100342 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200343 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200344 }
345}
346
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200347
Willy Tarreau62f52692017-10-08 23:01:42 +0200348/*****************************************************************/
349/* functions below are dedicated to the mux setup and management */
350/*****************************************************************/
351
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200352/* Initialize the mux once it's attached. For outgoing connections, the context
353 * is already initialized before installing the mux, so we detect incoming
354 * connections from the fact that the context is still NULL. Returns < 0 on
355 * error.
356 */
357static int h2_init(struct connection *conn, struct proxy *prx)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200358{
359 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100360 struct task *t = NULL;
361 struct session *sess = conn->owner;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200362
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200363 /* we don't support outgoing connections for now */
364 if (conn->mux_ctx)
365 goto fail_no_h2c;
366
Willy Tarreaubafbe012017-11-24 17:34:44 +0100367 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200368 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200369 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200370
Willy Tarreau3f133572017-10-31 19:21:06 +0100371
Willy Tarreau599391a2017-11-24 10:16:00 +0100372 h2c->shut_timeout = h2c->timeout = sess->fe->timeout.client;
373 if (tick_isset(sess->fe->timeout.clientfin))
374 h2c->shut_timeout = sess->fe->timeout.clientfin;
375
Willy Tarreau33400292017-11-05 11:23:40 +0100376 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100377 if (tick_isset(h2c->timeout)) {
378 t = task_new(tid_bit);
379 if (!t)
380 goto fail;
381
382 h2c->task = t;
383 t->process = h2_timeout_task;
384 t->context = h2c;
385 t->expire = tick_add(now_ms, h2c->timeout);
386 }
Willy Tarreauea392822017-10-31 10:02:25 +0100387
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200388 h2c->wait_event.task = tasklet_new();
389 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200390 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200391 h2c->wait_event.task->process = h2_io_cb;
392 h2c->wait_event.task->context = h2c;
393 h2c->wait_event.wait_reason = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200394
Willy Tarreau32218eb2017-09-22 08:07:25 +0200395 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
396 if (!h2c->ddht)
397 goto fail;
398
399 /* Initialise the context. */
400 h2c->st0 = H2_CS_PREFACE;
401 h2c->conn = conn;
402 h2c->max_id = -1;
403 h2c->errcode = H2_ERR_NO_ERROR;
404 h2c->flags = H2_CF_NONE;
405 h2c->rcvd_c = 0;
406 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100407 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200408 h2c->nb_cs = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200409
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200410 h2c->dbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200411 h2c->dsi = -1;
412 h2c->msi = -1;
413 h2c->last_sid = -1;
414
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200415 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200416 h2c->miw = 65535; /* mux initial window size */
417 h2c->mws = 65535; /* mux window size */
418 h2c->mfs = 16384; /* initial max frame size */
419 h2c->streams_by_id = EB_ROOT_UNIQUE;
420 LIST_INIT(&h2c->send_list);
421 LIST_INIT(&h2c->fctl_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100422 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200423 conn->mux_ctx = h2c;
424
Willy Tarreau3f133572017-10-31 19:21:06 +0100425 if (t)
426 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100427
Willy Tarreau0f383582018-10-03 14:22:21 +0200428 /* prepare to read something */
429 conn_xprt_want_recv(conn);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200430 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200431 return 0;
mildiscd2d7de2018-10-02 16:44:18 +0200432 fail:
Willy Tarreauea392822017-10-31 10:02:25 +0100433 if (t)
434 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200435 if (h2c->wait_event.task)
436 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100437 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200438 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200439 return -1;
440}
441
Willy Tarreau2373acc2017-10-12 17:35:14 +0200442/* returns the stream associated with id <id> or NULL if not found */
443static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
444{
445 struct eb32_node *node;
446
Willy Tarreau2a856182017-05-16 15:20:39 +0200447 if (id > h2c->max_id)
448 return (struct h2s *)h2_idle_stream;
449
Willy Tarreau2373acc2017-10-12 17:35:14 +0200450 node = eb32_lookup(&h2c->streams_by_id, id);
451 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200452 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200453
454 return container_of(node, struct h2s, by_id);
455}
456
Willy Tarreau62f52692017-10-08 23:01:42 +0200457/* release function for a connection. This one should be called to free all
458 * resources allocated to the mux.
459 */
460static void h2_release(struct connection *conn)
461{
Willy Tarreau32218eb2017-09-22 08:07:25 +0200462 struct h2c *h2c = conn->mux_ctx;
463
464 LIST_DEL(&conn->list);
465
466 if (h2c) {
467 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200468
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100469 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100470 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100471 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200472
Willy Tarreau44e973f2018-03-01 17:49:30 +0100473 h2_release_buf(h2c, &h2c->dbuf);
474 h2_release_buf(h2c, &h2c->mbuf);
475
Willy Tarreauea392822017-10-31 10:02:25 +0100476 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200477 h2c->task->context = NULL;
478 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100479 h2c->task = NULL;
480 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200481 if (h2c->wait_event.task)
482 tasklet_free(h2c->wait_event.task);
483 if (h2c->wait_event.wait_reason != 0)
484 conn->xprt->unsubscribe(conn, h2c->wait_event.wait_reason,
485 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100486
Willy Tarreaubafbe012017-11-24 17:34:44 +0100487 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200488 }
489
490 conn->mux = NULL;
491 conn->mux_ctx = NULL;
492
493 conn_stop_tracking(conn);
494 conn_full_close(conn);
495 if (conn->destroy_cb)
496 conn->destroy_cb(conn);
497 conn_free(conn);
Willy Tarreau62f52692017-10-08 23:01:42 +0200498}
499
500
Willy Tarreau71681172017-10-23 14:39:06 +0200501/******************************************************/
502/* functions below are for the H2 protocol processing */
503/******************************************************/
504
505/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100506static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200507{
508 return h2s ? h2s->id : 0;
509}
510
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200511/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100512static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200513{
514 if (h2c->msi < 0)
515 return 0;
516
517 if (h2c->msi == h2s_id(h2s))
518 return 0;
519
520 return 1;
521}
522
Willy Tarreau741d6df2017-10-17 08:00:59 +0200523/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100524static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200525{
526 h2c->errcode = err;
527 h2c->st0 = H2_CS_ERROR;
528}
529
Willy Tarreau2e43f082017-10-17 08:03:59 +0200530/* marks an error on the stream */
Willy Tarreau1f094672017-11-20 21:27:45 +0100531static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200532{
Willy Tarreauab0e1da2018-10-05 10:16:37 +0200533 if (h2s->id && h2s->st < H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200534 h2s->errcode = err;
535 h2s->st = H2_SS_ERROR;
536 if (h2s->cs)
537 h2s->cs->flags |= CS_FL_ERROR;
538 }
539}
540
Willy Tarreaue4820742017-07-27 13:37:23 +0200541/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100542static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200543{
544 uint8_t *out = frame;
545
546 *out = len >> 16;
547 write_n16(out + 1, len);
548}
549
Willy Tarreau54c15062017-10-10 17:10:03 +0200550/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
551 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
552 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200553 * available in the buffer's input prior to calling this function. The buffer
554 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200555 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100556static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200557 const struct buffer *b, int o)
558{
Willy Tarreau591d4452018-06-15 17:21:00 +0200559 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 +0200560}
561
Willy Tarreau1f094672017-11-20 21:27:45 +0100562static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200563{
Willy Tarreau591d4452018-06-15 17:21:00 +0200564 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200565}
566
Willy Tarreau1f094672017-11-20 21:27:45 +0100567static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200568{
Willy Tarreau591d4452018-06-15 17:21:00 +0200569 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200570}
571
Willy Tarreau1f094672017-11-20 21:27:45 +0100572static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200573{
Willy Tarreau591d4452018-06-15 17:21:00 +0200574 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200575}
576
577
Willy Tarreau715d5312017-07-11 15:20:24 +0200578/* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm
579 * is not obvious. It turns out that H2 headers are neither aligned nor do they
580 * use regular sizes. And to add to the trouble, the buffer may wrap so each
581 * byte read must be checked. The header is formed like this :
582 *
583 * b0 b1 b2 b3 b4 b5..b8
584 * +----------+---------+--------+----+----+----------------------+
585 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
586 * +----------+---------+--------+----+----+----------------------+
587 *
588 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
589 * we get the sid properly aligned and ordered, and 16 bits of len properly
590 * ordered as well. The type and flags can be extracted using bit shifts from
591 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200592 * Returns zero if some bytes are missing, otherwise non-zero on success. The
593 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200594 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100595static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200596{
597 uint64_t w;
598
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200599 if (b_data(b) < 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200600 return 0;
601
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200602 w = h2_get_n64(b, 1);
Willy Tarreaub7b5fe12018-06-18 13:33:09 +0200603 h->len = *(uint8_t*)b_head(b) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200604 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
605 h->ff = w >> 32;
606 h->ft = w >> 40;
607 h->len += w >> 48;
608 return 1;
609}
610
611/* skip the next 9 bytes corresponding to the frame header possibly parsed by
612 * h2_peek_frame_hdr() above.
613 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100614static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200615{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200616 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200617}
618
619/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100620static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200621{
622 int ret;
623
624 ret = h2_peek_frame_hdr(b, h);
625 if (ret > 0)
626 h2_skip_frame_hdr(b);
627 return ret;
628}
629
Willy Tarreau00dd0782018-03-01 16:31:34 +0100630/* marks stream <h2s> as CLOSED and decrement the number of active streams for
631 * its connection if the stream was not yet closed. Please use this exclusively
632 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100633 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100634static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100635{
636 if (h2s->st != H2_SS_CLOSED)
637 h2s->h2c->nb_streams--;
638 h2s->st = H2_SS_CLOSED;
639}
640
Willy Tarreau71049cc2018-03-28 13:56:39 +0200641/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
642static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100643{
644 h2s_close(h2s);
645 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200646 if (b_size(&h2s->rxbuf)) {
647 b_free(&h2s->rxbuf);
648 offer_buffers(NULL, tasks_run_queue);
649 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200650 if (h2s->send_wait != NULL)
651 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
652 if (h2s->recv_wait != NULL)
653 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
654 /* There's no need to explicitely call unsubscribe here, the only
655 * reference left would be in the h2c send_list/fctl_list, and if
656 * we're in it, we're getting out anyway
657 */
658 LIST_DEL(&h2s->list);
659 LIST_INIT(&h2s->list);
660 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100661 pool_free(pool_head_h2s, h2s);
662}
663
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200664/* creates a new stream <id> on the h2c connection and returns it, or NULL in
665 * case of memory allocation error.
666 */
667static struct h2s *h2c_stream_new(struct h2c *h2c, int id)
668{
Willy Tarreau590a0512018-09-05 11:56:48 +0200669 struct session *sess = h2c->conn->owner;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200670 struct conn_stream *cs;
671 struct h2s *h2s;
672
Willy Tarreaubafbe012017-11-24 17:34:44 +0100673 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200674 if (!h2s)
675 goto out;
676
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200677 h2s->wait_event.task = tasklet_new();
678 if (!h2s->wait_event.task) {
679 pool_free(pool_head_h2s, h2s);
680 goto out;
681 }
682 h2s->send_wait = NULL;
683 h2s->recv_wait = NULL;
684 h2s->wait_event.task->process = h2_deferred_shut;
685 h2s->wait_event.task->context = h2s;
686 h2s->wait_event.handle = NULL;
687 h2s->wait_event.wait_reason = 0;
688 LIST_INIT(&h2s->list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200689 h2s->h2c = h2c;
690 h2s->mws = h2c->miw;
691 h2s->flags = H2_SF_NONE;
692 h2s->errcode = H2_ERR_NO_ERROR;
693 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200694 h2s->status = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200695 h2s->rxbuf = BUF_NULL;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200696 h1m_init_res(&h2s->h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +0200697 h2s->h1m.err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +0200698 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200699 h2s->by_id.key = h2s->id = id;
700 h2c->max_id = id;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200701
702 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100703 h2c->nb_streams++;
704 if (h2c->nb_streams > h2_settings_max_concurrent_streams)
705 goto out_close;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200706
707 cs = cs_new(h2c->conn);
708 if (!cs)
709 goto out_close;
710
711 h2s->cs = cs;
712 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200713 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200714
715 if (stream_create_from_cs(cs) < 0)
716 goto out_free_cs;
717
Willy Tarreau590a0512018-09-05 11:56:48 +0200718 /* We want the accept date presented to the next stream to be the one
719 * we have now, the handshake time to be null (since the next stream
720 * is not delayed by a handshake), and the idle time to count since
721 * right now.
722 */
723 sess->accept_date = date;
724 sess->tv_accept = now;
725 sess->t_handshake = 0;
726
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200727 /* OK done, the stream lives its own life now */
Willy Tarreauf2101912018-07-19 10:11:38 +0200728 if (h2_has_too_many_cs(h2c))
729 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200730 return h2s;
731
732 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200733 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200734 cs_free(cs);
735 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200736 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200737 h2s = NULL;
Willy Tarreau45efc072018-10-03 18:27:52 +0200738 out_free_h2s:
739 pool_free(pool_head_h2s, h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200740 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200741 sess_log(sess);
742 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200743}
744
Willy Tarreaube5b7152017-09-25 16:25:39 +0200745/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
746 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
747 * the various settings codes.
748 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200749static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +0200750{
751 struct buffer *res;
752 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +0200753 struct buffer buf;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200754 int ret;
755
756 if (h2c_mux_busy(h2c, NULL)) {
757 h2c->flags |= H2_CF_DEM_MBUSY;
758 return 0;
759 }
760
Willy Tarreau44e973f2018-03-01 17:49:30 +0100761 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200762 if (!res) {
763 h2c->flags |= H2_CF_MUX_MALLOC;
764 h2c->flags |= H2_CF_DEM_MROOM;
765 return 0;
766 }
767
768 chunk_init(&buf, buf_data, sizeof(buf_data));
769 chunk_memcpy(&buf,
770 "\x00\x00\x00" /* length : 0 for now */
771 "\x04\x00" /* type : 4 (settings), flags : 0 */
772 "\x00\x00\x00\x00", /* stream ID : 0 */
773 9);
774
775 if (h2_settings_header_table_size != 4096) {
776 char str[6] = "\x00\x01"; /* header_table_size */
777
778 write_n32(str + 2, h2_settings_header_table_size);
779 chunk_memcat(&buf, str, 6);
780 }
781
782 if (h2_settings_initial_window_size != 65535) {
783 char str[6] = "\x00\x04"; /* initial_window_size */
784
785 write_n32(str + 2, h2_settings_initial_window_size);
786 chunk_memcat(&buf, str, 6);
787 }
788
789 if (h2_settings_max_concurrent_streams != 0) {
790 char str[6] = "\x00\x03"; /* max_concurrent_streams */
791
792 /* Note: 0 means "unlimited" for haproxy's config but not for
793 * the protocol, so never send this value!
794 */
795 write_n32(str + 2, h2_settings_max_concurrent_streams);
796 chunk_memcat(&buf, str, 6);
797 }
798
799 if (global.tune.bufsize != 16384) {
800 char str[6] = "\x00\x05"; /* max_frame_size */
801
802 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
803 * match bufsize - rewrite size, but at the moment it seems
804 * that clients don't take care of it.
805 */
806 write_n32(str + 2, global.tune.bufsize);
807 chunk_memcat(&buf, str, 6);
808 }
809
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200810 h2_set_frame_size(buf.area, buf.data - 9);
811 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +0200812 if (unlikely(ret <= 0)) {
813 if (!ret) {
814 h2c->flags |= H2_CF_MUX_MFULL;
815 h2c->flags |= H2_CF_DEM_MROOM;
816 return 0;
817 }
818 else {
819 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
820 return 0;
821 }
822 }
823 return ret;
824}
825
Willy Tarreau52eed752017-09-22 15:05:09 +0200826/* Try to receive a connection preface, then upon success try to send our
827 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
828 * missing data. It may return an error in h2c.
829 */
830static int h2c_frt_recv_preface(struct h2c *h2c)
831{
832 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +0200833 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200834
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200835 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +0200836
837 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +0200838 if (ret1 < 0)
839 sess_log(h2c->conn->owner);
840
Willy Tarreau52eed752017-09-22 15:05:09 +0200841 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
842 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
843 return 0;
844 }
845
Willy Tarreau7f0cc492018-10-08 07:13:08 +0200846 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +0200847 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200848 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +0200849
Willy Tarreaube5b7152017-09-25 16:25:39 +0200850 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +0200851}
852
Willy Tarreau081d4722017-05-16 21:51:05 +0200853/* try to send a GOAWAY frame on the connection to report an error or a graceful
854 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
855 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
856 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
857 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
858 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
859 * on unrecoverable failure. It will not attempt to send one again in this last
860 * case so that it is safe to use h2c_error() to report such errors.
861 */
862static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
863{
864 struct buffer *res;
865 char str[17];
866 int ret;
867
868 if (h2c->flags & H2_CF_GOAWAY_FAILED)
869 return 1; // claim that it worked
870
871 if (h2c_mux_busy(h2c, h2s)) {
872 if (h2s)
873 h2s->flags |= H2_SF_BLK_MBUSY;
874 else
875 h2c->flags |= H2_CF_DEM_MBUSY;
876 return 0;
877 }
878
Willy Tarreau44e973f2018-03-01 17:49:30 +0100879 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +0200880 if (!res) {
881 h2c->flags |= H2_CF_MUX_MALLOC;
882 if (h2s)
883 h2s->flags |= H2_SF_BLK_MROOM;
884 else
885 h2c->flags |= H2_CF_DEM_MROOM;
886 return 0;
887 }
888
889 /* len: 8, type: 7, flags: none, sid: 0 */
890 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
891
892 if (h2c->last_sid < 0)
893 h2c->last_sid = h2c->max_id;
894
895 write_n32(str + 9, h2c->last_sid);
896 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200897 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +0200898 if (unlikely(ret <= 0)) {
899 if (!ret) {
900 h2c->flags |= H2_CF_MUX_MFULL;
901 if (h2s)
902 h2s->flags |= H2_SF_BLK_MROOM;
903 else
904 h2c->flags |= H2_CF_DEM_MROOM;
905 return 0;
906 }
907 else {
908 /* we cannot report this error using GOAWAY, so we mark
909 * it and claim a success.
910 */
911 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
912 h2c->flags |= H2_CF_GOAWAY_FAILED;
913 return 1;
914 }
915 }
916 h2c->flags |= H2_CF_GOAWAY_SENT;
917 return ret;
918}
919
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100920/* Try to send an RST_STREAM frame on the connection for the indicated stream
921 * during mux operations. This stream must be valid and cannot be closed
922 * already. h2s->id will be used for the stream ID and h2s->errcode will be
923 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
924 * not yet.
925 *
926 * Returns > 0 on success or zero if nothing was done. In case of lack of room
927 * to write the message, it subscribes the stream to future notifications.
928 */
929static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
930{
931 struct buffer *res;
932 char str[13];
933 int ret;
934
935 if (!h2s || h2s->st == H2_SS_CLOSED)
936 return 1;
937
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100938 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
939 * RST_STREAM in response to a RST_STREAM frame.
940 */
941 if (h2c->dft == H2_FT_RST_STREAM) {
942 ret = 1;
943 goto ignore;
944 }
945
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100946 if (h2c_mux_busy(h2c, h2s)) {
947 h2s->flags |= H2_SF_BLK_MBUSY;
948 return 0;
949 }
950
Willy Tarreau44e973f2018-03-01 17:49:30 +0100951 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100952 if (!res) {
953 h2c->flags |= H2_CF_MUX_MALLOC;
954 h2s->flags |= H2_SF_BLK_MROOM;
955 return 0;
956 }
957
958 /* len: 4, type: 3, flags: none */
959 memcpy(str, "\x00\x00\x04\x03\x00", 5);
960 write_n32(str + 5, h2s->id);
961 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +0200962 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100963
964 if (unlikely(ret <= 0)) {
965 if (!ret) {
966 h2c->flags |= H2_CF_MUX_MFULL;
967 h2s->flags |= H2_SF_BLK_MROOM;
968 return 0;
969 }
970 else {
971 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
972 return 0;
973 }
974 }
975
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100976 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100977 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +0100978 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100979 return ret;
980}
981
982/* Try to send an RST_STREAM frame on the connection for the stream being
983 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
984 * error code unless the stream's state already is IDLE or CLOSED in which
985 * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if
986 * it was not yet.
987 *
988 * Returns > 0 on success or zero if nothing was done. In case of lack of room
989 * to write the message, it blocks the demuxer and subscribes it to future
Willy Tarreau27a84c92017-10-17 08:10:17 +0200990 * notifications. It's worth mentionning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100991 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +0200992 */
993static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
994{
995 struct buffer *res;
996 char str[13];
997 int ret;
998
Willy Tarreau8adae7c2018-03-22 17:37:05 +0100999 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1000 * RST_STREAM in response to a RST_STREAM frame.
1001 */
1002 if (h2c->dft == H2_FT_RST_STREAM) {
1003 ret = 1;
1004 goto ignore;
1005 }
1006
Willy Tarreau27a84c92017-10-17 08:10:17 +02001007 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001008 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001009 return 0;
1010 }
1011
Willy Tarreau44e973f2018-03-01 17:49:30 +01001012 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001013 if (!res) {
1014 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001015 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001016 return 0;
1017 }
1018
1019 /* len: 4, type: 3, flags: none */
1020 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001021
Willy Tarreau27a84c92017-10-17 08:10:17 +02001022 write_n32(str + 5, h2c->dsi);
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001023 write_n32(str + 9, h2s->id ? h2s->errcode : H2_ERR_STREAM_CLOSED);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001024 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001025
Willy Tarreau27a84c92017-10-17 08:10:17 +02001026 if (unlikely(ret <= 0)) {
1027 if (!ret) {
1028 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001029 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001030 return 0;
1031 }
1032 else {
1033 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1034 return 0;
1035 }
1036 }
1037
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001038 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001039 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001040 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001041 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001042 }
1043
Willy Tarreau27a84c92017-10-17 08:10:17 +02001044 return ret;
1045}
1046
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001047/* try to send an empty DATA frame with the ES flag set to notify about the
1048 * end of stream and match a shutdown(write). If an ES was already sent as
1049 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1050 * on success or zero if nothing was done. In case of lack of room to write the
1051 * message, it subscribes the requesting stream to future notifications.
1052 */
1053static int h2_send_empty_data_es(struct h2s *h2s)
1054{
1055 struct h2c *h2c = h2s->h2c;
1056 struct buffer *res;
1057 char str[9];
1058 int ret;
1059
Willy Tarreau721c9742017-11-07 11:05:42 +01001060 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001061 return 1;
1062
1063 if (h2c_mux_busy(h2c, h2s)) {
1064 h2s->flags |= H2_SF_BLK_MBUSY;
1065 return 0;
1066 }
1067
Willy Tarreau44e973f2018-03-01 17:49:30 +01001068 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001069 if (!res) {
1070 h2c->flags |= H2_CF_MUX_MALLOC;
1071 h2s->flags |= H2_SF_BLK_MROOM;
1072 return 0;
1073 }
1074
1075 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1076 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1077 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001078 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001079 if (likely(ret > 0)) {
1080 h2s->flags |= H2_SF_ES_SENT;
1081 }
1082 else if (!ret) {
1083 h2c->flags |= H2_CF_MUX_MFULL;
1084 h2s->flags |= H2_SF_BLK_MROOM;
1085 return 0;
1086 }
1087 else {
1088 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1089 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001090 }
1091 return ret;
1092}
1093
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001094/* wake the streams attached to the connection, whose id is greater than <last>,
1095 * and assign their conn_stream the CS_FL_* flags <flags> in addition to
Willy Tarreau2c096c32018-09-12 09:45:54 +02001096 * CS_FL_ERROR in case of error and CS_FL_REOS in case of closed connection.
1097 * The stream's state is automatically updated accordingly.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001098 */
1099static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1100{
1101 struct eb32_node *node;
1102 struct h2s *h2s;
1103
1104 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
1105 flags |= CS_FL_ERROR;
1106
1107 if (conn_xprt_read0_pending(h2c->conn))
Willy Tarreau2c096c32018-09-12 09:45:54 +02001108 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001109
1110 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1111 while (node) {
1112 h2s = container_of(node, struct h2s, by_id);
1113 if (h2s->id <= last)
1114 break;
1115 node = eb32_next(node);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001116
1117 if (!h2s->cs) {
1118 /* this stream was already orphaned */
Willy Tarreau71049cc2018-03-28 13:56:39 +02001119 h2s_destroy(h2s);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001120 continue;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001121 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001122
1123 h2s->cs->flags |= flags;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001124 if (h2s->recv_wait) {
1125 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001126 sw->wait_reason &= ~SUB_CAN_RECV;
1127 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001128 h2s->recv_wait = NULL;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02001129 } else if (h2s->cs->data_cb->wake != NULL)
1130 h2s->cs->data_cb->wake(h2s->cs);
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001131
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001132 if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
1133 h2s->st = H2_SS_ERROR;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001134 else if (flags & CS_FL_REOS && h2s->st == H2_SS_OPEN)
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001135 h2s->st = H2_SS_HREM;
Willy Tarreau2c096c32018-09-12 09:45:54 +02001136 else if (flags & CS_FL_REOS && h2s->st == H2_SS_HLOC)
Willy Tarreau00dd0782018-03-01 16:31:34 +01001137 h2s_close(h2s);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001138 }
1139}
1140
Willy Tarreau3421aba2017-07-27 15:41:03 +02001141/* Increase all streams' outgoing window size by the difference passed in
1142 * argument. This is needed upon receipt of the settings frame if the initial
1143 * window size is different. The difference may be negative and the resulting
1144 * window size as well, for the time it takes to receive some window updates.
1145 */
1146static void h2c_update_all_ws(struct h2c *h2c, int diff)
1147{
1148 struct h2s *h2s;
1149 struct eb32_node *node;
1150
1151 if (!diff)
1152 return;
1153
1154 node = eb32_first(&h2c->streams_by_id);
1155 while (node) {
1156 h2s = container_of(node, struct h2s, by_id);
1157 h2s->mws += diff;
1158 node = eb32_next(node);
1159 }
1160}
1161
1162/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1163 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
1164 * return an error in h2c. Described in RFC7540#6.5.
1165 */
1166static int h2c_handle_settings(struct h2c *h2c)
1167{
1168 unsigned int offset;
1169 int error;
1170
1171 if (h2c->dff & H2_F_SETTINGS_ACK) {
1172 if (h2c->dfl) {
1173 error = H2_ERR_FRAME_SIZE_ERROR;
1174 goto fail;
1175 }
1176 return 1;
1177 }
1178
1179 if (h2c->dsi != 0) {
1180 error = H2_ERR_PROTOCOL_ERROR;
1181 goto fail;
1182 }
1183
1184 if (h2c->dfl % 6) {
1185 error = H2_ERR_FRAME_SIZE_ERROR;
1186 goto fail;
1187 }
1188
1189 /* that's the limit we can process */
1190 if (h2c->dfl > global.tune.bufsize) {
1191 error = H2_ERR_FRAME_SIZE_ERROR;
1192 goto fail;
1193 }
1194
1195 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001196 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001197 return 0;
1198
1199 /* parse the frame */
1200 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001201 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1202 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001203
1204 switch (type) {
1205 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1206 /* we need to update all existing streams with the
1207 * difference from the previous iws.
1208 */
1209 if (arg < 0) { // RFC7540#6.5.2
1210 error = H2_ERR_FLOW_CONTROL_ERROR;
1211 goto fail;
1212 }
1213 h2c_update_all_ws(h2c, arg - h2c->miw);
1214 h2c->miw = arg;
1215 break;
1216 case H2_SETTINGS_MAX_FRAME_SIZE:
1217 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1218 error = H2_ERR_PROTOCOL_ERROR;
1219 goto fail;
1220 }
1221 h2c->mfs = arg;
1222 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001223 case H2_SETTINGS_ENABLE_PUSH:
1224 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1225 error = H2_ERR_PROTOCOL_ERROR;
1226 goto fail;
1227 }
1228 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001229 }
1230 }
1231
1232 /* need to ACK this frame now */
1233 h2c->st0 = H2_CS_FRAME_A;
1234 return 1;
1235 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001236 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001237 h2c_error(h2c, error);
1238 return 0;
1239}
1240
1241/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1242 * success or one of the h2_status values.
1243 */
1244static int h2c_ack_settings(struct h2c *h2c)
1245{
1246 struct buffer *res;
1247 char str[9];
1248 int ret = -1;
1249
1250 if (h2c_mux_busy(h2c, NULL)) {
1251 h2c->flags |= H2_CF_DEM_MBUSY;
1252 return 0;
1253 }
1254
Willy Tarreau44e973f2018-03-01 17:49:30 +01001255 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001256 if (!res) {
1257 h2c->flags |= H2_CF_MUX_MALLOC;
1258 h2c->flags |= H2_CF_DEM_MROOM;
1259 return 0;
1260 }
1261
1262 memcpy(str,
1263 "\x00\x00\x00" /* length : 0 (no data) */
1264 "\x04" "\x01" /* type : 4, flags : ACK */
1265 "\x00\x00\x00\x00" /* stream ID */, 9);
1266
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001267 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001268 if (unlikely(ret <= 0)) {
1269 if (!ret) {
1270 h2c->flags |= H2_CF_MUX_MFULL;
1271 h2c->flags |= H2_CF_DEM_MROOM;
1272 return 0;
1273 }
1274 else {
1275 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1276 return 0;
1277 }
1278 }
1279 return ret;
1280}
1281
Willy Tarreaucf68c782017-10-10 17:11:41 +02001282/* processes a PING frame and schedules an ACK if needed. The caller must pass
1283 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
1284 * missing data. It may return an error in h2c.
1285 */
1286static int h2c_handle_ping(struct h2c *h2c)
1287{
1288 /* frame length must be exactly 8 */
1289 if (h2c->dfl != 8) {
1290 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1291 return 0;
1292 }
1293
1294 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001295 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001296 h2c->st0 = H2_CS_FRAME_A;
1297 return 1;
1298}
1299
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001300/* Try to send a window update for stream id <sid> and value <increment>.
1301 * Returns > 0 on success or zero on missing room or failure. It may return an
1302 * error in h2c.
1303 */
1304static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1305{
1306 struct buffer *res;
1307 char str[13];
1308 int ret = -1;
1309
1310 if (h2c_mux_busy(h2c, NULL)) {
1311 h2c->flags |= H2_CF_DEM_MBUSY;
1312 return 0;
1313 }
1314
Willy Tarreau44e973f2018-03-01 17:49:30 +01001315 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001316 if (!res) {
1317 h2c->flags |= H2_CF_MUX_MALLOC;
1318 h2c->flags |= H2_CF_DEM_MROOM;
1319 return 0;
1320 }
1321
1322 /* length: 4, type: 8, flags: none */
1323 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1324 write_n32(str + 5, sid);
1325 write_n32(str + 9, increment);
1326
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001327 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001328
1329 if (unlikely(ret <= 0)) {
1330 if (!ret) {
1331 h2c->flags |= H2_CF_MUX_MFULL;
1332 h2c->flags |= H2_CF_DEM_MROOM;
1333 return 0;
1334 }
1335 else {
1336 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1337 return 0;
1338 }
1339 }
1340 return ret;
1341}
1342
1343/* try to send pending window update for the connection. It's safe to call it
1344 * with no pending updates. Returns > 0 on success or zero on missing room or
1345 * failure. It may return an error in h2c.
1346 */
1347static int h2c_send_conn_wu(struct h2c *h2c)
1348{
1349 int ret = 1;
1350
1351 if (h2c->rcvd_c <= 0)
1352 return 1;
1353
1354 /* send WU for the connection */
1355 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1356 if (ret > 0)
1357 h2c->rcvd_c = 0;
1358
1359 return ret;
1360}
1361
1362/* try to send pending window update for the current dmux stream. It's safe to
1363 * call it with no pending updates. Returns > 0 on success or zero on missing
1364 * room or failure. It may return an error in h2c.
1365 */
1366static int h2c_send_strm_wu(struct h2c *h2c)
1367{
1368 int ret = 1;
1369
1370 if (h2c->rcvd_s <= 0)
1371 return 1;
1372
1373 /* send WU for the stream */
1374 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1375 if (ret > 0)
1376 h2c->rcvd_s = 0;
1377
1378 return ret;
1379}
1380
Willy Tarreaucf68c782017-10-10 17:11:41 +02001381/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1382 * success, 0 on missing data or one of the h2_status values.
1383 */
1384static int h2c_ack_ping(struct h2c *h2c)
1385{
1386 struct buffer *res;
1387 char str[17];
1388 int ret = -1;
1389
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001390 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001391 return 0;
1392
1393 if (h2c_mux_busy(h2c, NULL)) {
1394 h2c->flags |= H2_CF_DEM_MBUSY;
1395 return 0;
1396 }
1397
Willy Tarreau44e973f2018-03-01 17:49:30 +01001398 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001399 if (!res) {
1400 h2c->flags |= H2_CF_MUX_MALLOC;
1401 h2c->flags |= H2_CF_DEM_MROOM;
1402 return 0;
1403 }
1404
1405 memcpy(str,
1406 "\x00\x00\x08" /* length : 8 (same payload) */
1407 "\x06" "\x01" /* type : 6, flags : ACK */
1408 "\x00\x00\x00\x00" /* stream ID */, 9);
1409
1410 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001411 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001412
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001413 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001414 if (unlikely(ret <= 0)) {
1415 if (!ret) {
1416 h2c->flags |= H2_CF_MUX_MFULL;
1417 h2c->flags |= H2_CF_DEM_MROOM;
1418 return 0;
1419 }
1420 else {
1421 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1422 return 0;
1423 }
1424 }
1425 return ret;
1426}
1427
Willy Tarreau26f95952017-07-27 17:18:30 +02001428/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1429 * Returns > 0 on success or zero on missing data. It may return an error in
1430 * h2c or h2s. Described in RFC7540#6.9.
1431 */
1432static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1433{
1434 int32_t inc;
1435 int error;
1436
1437 if (h2c->dfl != 4) {
1438 error = H2_ERR_FRAME_SIZE_ERROR;
1439 goto conn_err;
1440 }
1441
1442 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001443 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001444 return 0;
1445
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001446 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001447
1448 if (h2c->dsi != 0) {
1449 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001450
1451 /* it's not an error to receive WU on a closed stream */
1452 if (h2s->st == H2_SS_CLOSED)
1453 return 1;
1454
1455 if (!inc) {
1456 error = H2_ERR_PROTOCOL_ERROR;
1457 goto strm_err;
1458 }
1459
1460 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1461 error = H2_ERR_FLOW_CONTROL_ERROR;
1462 goto strm_err;
1463 }
1464
1465 h2s->mws += inc;
1466 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1467 h2s->flags &= ~H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02001468 if (h2s->send_wait)
1469 LIST_ADDQ(&h2c->send_list, &h2s->list);
1470
Willy Tarreau26f95952017-07-27 17:18:30 +02001471 }
1472 }
1473 else {
1474 /* connection window update */
1475 if (!inc) {
1476 error = H2_ERR_PROTOCOL_ERROR;
1477 goto conn_err;
1478 }
1479
1480 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1481 error = H2_ERR_FLOW_CONTROL_ERROR;
1482 goto conn_err;
1483 }
1484
1485 h2c->mws += inc;
1486 }
1487
1488 return 1;
1489
1490 conn_err:
1491 h2c_error(h2c, error);
1492 return 0;
1493
1494 strm_err:
1495 if (h2s) {
1496 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001497 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001498 }
1499 else
1500 h2c_error(h2c, error);
1501 return 0;
1502}
1503
Willy Tarreaue96b0922017-10-30 00:28:29 +01001504/* processes a GOAWAY frame, and signals all streams whose ID is greater than
1505 * the last ID. Returns > 0 on success or zero on missing data. It may return
1506 * an error in h2c. Described in RFC7540#6.8.
1507 */
1508static int h2c_handle_goaway(struct h2c *h2c)
1509{
1510 int error;
1511 int last;
1512
1513 if (h2c->dsi != 0) {
1514 error = H2_ERR_PROTOCOL_ERROR;
1515 goto conn_err;
1516 }
1517
1518 if (h2c->dfl < 8) {
1519 error = H2_ERR_FRAME_SIZE_ERROR;
1520 goto conn_err;
1521 }
1522
1523 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001524 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001525 return 0;
1526
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001527 last = h2_get_n32(&h2c->dbuf, 0);
1528 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Willy Tarreaue96b0922017-10-30 00:28:29 +01001529 h2_wake_some_streams(h2c, last, CS_FL_ERROR);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001530 if (h2c->last_sid < 0)
1531 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001532 return 1;
1533
1534 conn_err:
1535 h2c_error(h2c, error);
1536 return 0;
1537}
1538
Willy Tarreau92153fc2017-12-03 19:46:19 +01001539/* processes a PRIORITY frame, and either skips it or rejects if it is
1540 * invalid. Returns > 0 on success or zero on missing data. It may return
1541 * an error in h2c. Described in RFC7540#6.3.
1542 */
1543static int h2c_handle_priority(struct h2c *h2c)
1544{
1545 int error;
1546
1547 if (h2c->dsi == 0) {
1548 error = H2_ERR_PROTOCOL_ERROR;
1549 goto conn_err;
1550 }
1551
1552 if (h2c->dfl != 5) {
1553 error = H2_ERR_FRAME_SIZE_ERROR;
1554 goto conn_err;
1555 }
1556
1557 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001558 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001559 return 0;
1560
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001561 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001562 /* 7540#5.3 : can't depend on itself */
1563 error = H2_ERR_PROTOCOL_ERROR;
1564 goto conn_err;
1565 }
1566 return 1;
1567
1568 conn_err:
1569 h2c_error(h2c, error);
1570 return 0;
1571}
1572
Willy Tarreaucd234e92017-08-18 10:59:39 +02001573/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
1574 * Returns > 0 on success or zero on missing data. It may return an error in
1575 * h2c. Described in RFC7540#6.4.
1576 */
1577static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1578{
1579 int error;
1580
1581 if (h2c->dsi == 0) {
1582 error = H2_ERR_PROTOCOL_ERROR;
1583 goto conn_err;
1584 }
1585
Willy Tarreaucd234e92017-08-18 10:59:39 +02001586 if (h2c->dfl != 4) {
1587 error = H2_ERR_FRAME_SIZE_ERROR;
1588 goto conn_err;
1589 }
1590
1591 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001592 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001593 return 0;
1594
1595 /* late RST, already handled */
1596 if (h2s->st == H2_SS_CLOSED)
1597 return 1;
1598
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001599 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001600 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001601
1602 if (h2s->cs) {
Willy Tarreau2c096c32018-09-12 09:45:54 +02001603 h2s->cs->flags |= CS_FL_REOS | CS_FL_ERROR;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001604 if (h2s->recv_wait) {
1605 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001606
1607 sw->wait_reason &= ~SUB_CAN_RECV;
1608 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001609 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001610 }
Willy Tarreaucd234e92017-08-18 10:59:39 +02001611 }
1612
1613 h2s->flags |= H2_SF_RST_RCVD;
1614 return 1;
1615
1616 conn_err:
1617 h2c_error(h2c, error);
1618 return 0;
1619}
1620
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001621/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1622 * It may return an error in h2c or h2s. The caller must consider that the
1623 * return value is the new h2s in case one was allocated (most common case).
1624 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001625 * errors here are reported as connection errors since it's impossible to
1626 * recover from such errors after the compression context has been altered.
1627 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001628static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001629{
1630 int error;
1631
1632 if (!h2c->dfl) {
1633 error = H2_ERR_PROTOCOL_ERROR; // empty headers frame!
Willy Tarreau22de8d32018-09-05 19:55:58 +02001634 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001635 goto strm_err;
1636 }
1637
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001638 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001639 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001640
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001641 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001642 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001643
Willy Tarreauf2101912018-07-19 10:11:38 +02001644 if (h2c->flags & H2_CF_DEM_TOOMANY)
1645 return 0; // too many cs still present
1646
Willy Tarreau13278b42017-10-13 19:23:14 +02001647 /* now either the frame is complete or the buffer is complete */
1648 if (h2s->st != H2_SS_IDLE) {
1649 /* FIXME: stream already exists, this is only allowed for
1650 * trailers (not supported for now).
1651 */
1652 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001653 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001654 goto conn_err;
1655 }
1656 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1657 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1658 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001659 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001660 goto conn_err;
1661 }
1662
Willy Tarreau22de8d32018-09-05 19:55:58 +02001663 /* Note: we don't emit any other logs below because ff we return
1664 * positively from h2c_stream_new(), the stream will report the error,
1665 * and if we return in error, h2c_stream_new() will emit the error.
1666 */
Willy Tarreau13278b42017-10-13 19:23:14 +02001667 h2s = h2c_stream_new(h2c, h2c->dsi);
1668 if (!h2s) {
1669 error = H2_ERR_INTERNAL_ERROR;
1670 goto conn_err;
1671 }
1672
1673 h2s->st = H2_SS_OPEN;
1674 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
1675 h2s->st = H2_SS_HREM;
1676 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001677 /* note: cs cannot be null for now (just created above) */
1678 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreau13278b42017-10-13 19:23:14 +02001679 }
1680
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001681 if (!h2_frt_decode_headers(h2s))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001682 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001683
Willy Tarreau8f650c32017-11-21 19:36:21 +01001684 if (h2c->st0 >= H2_CS_ERROR)
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001685 return NULL;
Willy Tarreau8f650c32017-11-21 19:36:21 +01001686
Willy Tarreau721c9742017-11-07 11:05:42 +01001687 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau13278b42017-10-13 19:23:14 +02001688 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001689 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001690 }
1691 else {
1692 /* update the max stream ID if the request is being processed */
1693 if (h2s->id > h2c->max_id)
1694 h2c->max_id = h2s->id;
1695 }
1696
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001697 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02001698
1699 conn_err:
1700 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001701 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001702
1703 strm_err:
1704 if (h2s) {
1705 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001706 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02001707 }
1708 else
1709 h2c_error(h2c, error);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001710 return NULL;
Willy Tarreau13278b42017-10-13 19:23:14 +02001711}
1712
Willy Tarreau454f9052017-10-26 19:40:35 +02001713/* processes a DATA frame. Returns > 0 on success or zero on missing data.
1714 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
1715 */
1716static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
1717{
1718 int error;
1719
1720 /* note that empty DATA frames are perfectly valid and sometimes used
1721 * to signal an end of stream (with the ES flag).
1722 */
1723
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001724 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02001725 return 0; // empty buffer
1726
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001727 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02001728 return 0; // incomplete frame
1729
1730 /* now either the frame is complete or the buffer is complete */
1731
1732 if (!h2c->dsi) {
1733 /* RFC7540#6.1 */
1734 error = H2_ERR_PROTOCOL_ERROR;
1735 goto conn_err;
1736 }
1737
1738 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
1739 /* RFC7540#6.1 */
1740 error = H2_ERR_STREAM_CLOSED;
1741 goto strm_err;
1742 }
1743
Willy Tarreaua56a6de2018-02-26 15:59:07 +01001744 if (!h2_frt_transfer_data(h2s))
1745 return 0;
1746
Willy Tarreau454f9052017-10-26 19:40:35 +02001747 /* call the upper layers to process the frame, then let the upper layer
1748 * notify the stream about any change.
1749 */
1750 if (!h2s->cs) {
1751 error = H2_ERR_STREAM_CLOSED;
1752 goto strm_err;
1753 }
1754
Willy Tarreau8f650c32017-11-21 19:36:21 +01001755 if (h2c->st0 >= H2_CS_ERROR)
1756 return 0;
1757
Willy Tarreau721c9742017-11-07 11:05:42 +01001758 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02001759 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01001760 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001761 }
1762
1763 /* check for completion : the callee will change this to FRAME_A or
1764 * FRAME_H once done.
1765 */
1766 if (h2c->st0 == H2_CS_FRAME_P)
1767 return 0;
1768
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001769
1770 /* last frame */
1771 if (h2c->dff & H2_F_DATA_END_STREAM) {
1772 h2s->st = H2_SS_HREM;
1773 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01001774 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreauc4134ba2017-12-11 18:45:08 +01001775 }
1776
Willy Tarreau454f9052017-10-26 19:40:35 +02001777 return 1;
1778
1779 conn_err:
1780 h2c_error(h2c, error);
1781 return 0;
1782
1783 strm_err:
1784 if (h2s) {
1785 h2s_error(h2s, error);
Willy Tarreaua20a5192017-12-27 11:02:06 +01001786 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02001787 }
1788 else
1789 h2c_error(h2c, error);
1790 return 0;
1791}
1792
Willy Tarreaubc933932017-10-09 16:21:43 +02001793/* process Rx frames to be demultiplexed */
1794static void h2_process_demux(struct h2c *h2c)
1795{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001796 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02001797
Willy Tarreau081d4722017-05-16 21:51:05 +02001798 if (h2c->st0 >= H2_CS_ERROR)
1799 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02001800
1801 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
1802 if (h2c->st0 == H2_CS_PREFACE) {
1803 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
1804 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001805 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02001806 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001807 sess_log(h2c->conn->owner);
1808 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001809 goto fail;
1810 }
1811
1812 h2c->max_id = 0;
1813 h2c->st0 = H2_CS_SETTINGS1;
1814 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001815
1816 if (h2c->st0 == H2_CS_SETTINGS1) {
1817 struct h2_fh hdr;
1818
1819 /* ensure that what is pending is a valid SETTINGS frame
1820 * without an ACK.
1821 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001822 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001823 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02001824 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001825 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001826 sess_log(h2c->conn->owner);
1827 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001828 goto fail;
1829 }
1830
1831 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
1832 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1833 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1834 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001835 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001836 goto fail;
1837 }
1838
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001839 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001840 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
1841 h2c_error(h2c, H2_ERR_FRAME_SIZE_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
1847 /* that's OK, switch to FRAME_P to process it */
1848 h2c->dfl = hdr.len;
1849 h2c->dsi = hdr.sid;
1850 h2c->dft = hdr.ft;
1851 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001852 h2c->dpl = 0;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02001853 h2c->st0 = H2_CS_FRAME_P;
1854 }
Willy Tarreau52eed752017-09-22 15:05:09 +02001855 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001856
1857 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001858 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001859 int ret = 0;
1860
1861 if (h2c->st0 >= H2_CS_ERROR)
1862 break;
1863
1864 if (h2c->st0 == H2_CS_FRAME_H) {
1865 struct h2_fh hdr;
1866
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001867 if (!h2_peek_frame_hdr(&h2c->dbuf, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02001868 break;
1869
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02001870 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02001871 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
1872 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001873 if (!h2c->nb_streams) {
1874 /* only log if no other stream can report the error */
1875 sess_log(h2c->conn->owner);
1876 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02001877 break;
1878 }
1879
1880 h2c->dfl = hdr.len;
1881 h2c->dsi = hdr.sid;
1882 h2c->dft = hdr.ft;
1883 h2c->dff = hdr.ff;
Willy Tarreau05e5daf2017-12-11 15:17:36 +01001884 h2c->dpl = 0;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001885 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001886 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau7e98c052017-10-10 15:56:59 +02001887 }
1888
1889 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001890 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
1891
Olivier Houchard638b7992018-08-16 15:41:52 +02001892 if (tmp_h2s != h2s && h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001893 /* we may have to signal the upper layers */
1894 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02001895 if (h2s->recv_wait) {
1896 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
1897 tasklet_wakeup(h2s->recv_wait->task);
1898 h2s->recv_wait = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001899 }
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001900 if (h2c->st0 >= H2_CS_ERROR)
1901 goto strm_err;
1902
1903 if (h2s->st >= H2_SS_ERROR) {
1904 /* stream error : send RST_STREAM */
1905 h2c->st0 = H2_CS_FRAME_E;
1906 }
1907 }
1908 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02001909
Willy Tarreaud7901432017-12-29 11:34:40 +01001910 if (h2c->st0 == H2_CS_FRAME_E)
1911 goto strm_err;
1912
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001913 if (h2s->st == H2_SS_IDLE &&
1914 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
1915 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
1916 * this state MUST be treated as a connection error
1917 */
1918 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1919 h2c->st0 = H2_CS_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001920 if (!h2c->nb_streams) {
1921 /* only log if no other stream can report the error */
1922 sess_log(h2c->conn->owner);
1923 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01001924 break;
1925 }
1926
Willy Tarreauf182a9a2017-10-30 12:03:50 +01001927 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
1928 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
1929 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
1930 * this state MUST be treated as a stream error
1931 */
1932 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1933 goto strm_err;
1934 }
1935
Willy Tarreauab837502017-12-27 15:07:30 +01001936 /* Below the management of frames received in closed state is a
1937 * bit hackish because the spec makes strong differences between
1938 * streams closed by receiving RST, sending RST, and seeing ES
1939 * in both directions. In addition to this, the creation of a
1940 * new stream reusing the identifier of a closed one will be
1941 * detected here. Given that we cannot keep track of all closed
1942 * streams forever, we consider that unknown closed streams were
1943 * closed on RST received, which allows us to respond with an
1944 * RST without breaking the connection (eg: to abort a transfer).
1945 * Some frames have to be silently ignored as well.
1946 */
1947 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
1948 if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) {
1949 /* #5.1.1: The identifier of a newly
1950 * established stream MUST be numerically
1951 * greater than all streams that the initiating
1952 * endpoint has opened or reserved. This
1953 * governs streams that are opened using a
1954 * HEADERS frame and streams that are reserved
1955 * using PUSH_PROMISE. An endpoint that
1956 * receives an unexpected stream identifier
1957 * MUST respond with a connection error.
1958 */
1959 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1960 goto strm_err;
1961 }
1962
1963 if (h2s->flags & H2_SF_RST_RCVD) {
1964 /* RFC7540#5.1:closed: an endpoint that
1965 * receives any frame other than PRIORITY after
1966 * receiving a RST_STREAM MUST treat that as a
1967 * stream error of type STREAM_CLOSED.
1968 *
1969 * Note that old streams fall into this category
1970 * and will lead to an RST being sent.
1971 */
1972 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
1973 h2c->st0 = H2_CS_FRAME_E;
1974 goto strm_err;
1975 }
1976
1977 /* RFC7540#5.1:closed: if this state is reached as a
1978 * result of sending a RST_STREAM frame, the peer that
1979 * receives the RST_STREAM might have already sent
1980 * frames on the stream that cannot be withdrawn. An
1981 * endpoint MUST ignore frames that it receives on
1982 * closed streams after it has sent a RST_STREAM
1983 * frame. An endpoint MAY choose to limit the period
1984 * over which it ignores frames and treat frames that
1985 * arrive after this time as being in error.
1986 */
1987 if (!(h2s->flags & H2_SF_RST_SENT)) {
1988 /* RFC7540#5.1:closed: any frame other than
1989 * PRIO/WU/RST in this state MUST be treated as
1990 * a connection error
1991 */
1992 if (h2c->dft != H2_FT_RST_STREAM &&
1993 h2c->dft != H2_FT_PRIORITY &&
1994 h2c->dft != H2_FT_WINDOW_UPDATE) {
1995 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
1996 goto strm_err;
1997 }
1998 }
1999 }
2000
Willy Tarreauc0da1962017-10-30 18:38:00 +01002001#if 0
2002 // problem below: it is not possible to completely ignore such
2003 // streams as we need to maintain the compression state as well
2004 // and for this we need to completely process these frames (eg:
2005 // HEADERS frames) as well as counting DATA frames to emit
2006 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2007 // This is a typical case of layer violation where the
2008 // transported contents are critical to the connection's
2009 // validity and must be ignored at the same time :-(
2010
2011 /* graceful shutdown, ignore streams whose ID is higher than
2012 * the one advertised in GOAWAY. RFC7540#6.8.
2013 */
2014 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002015 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2016 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002017 h2c->dfl -= ret;
2018 ret = h2c->dfl == 0;
2019 goto strm_err;
2020 }
2021#endif
2022
Willy Tarreau7e98c052017-10-10 15:56:59 +02002023 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002024 case H2_FT_SETTINGS:
2025 if (h2c->st0 == H2_CS_FRAME_P)
2026 ret = h2c_handle_settings(h2c);
2027
2028 if (h2c->st0 == H2_CS_FRAME_A)
2029 ret = h2c_ack_settings(h2c);
2030 break;
2031
Willy Tarreaucf68c782017-10-10 17:11:41 +02002032 case H2_FT_PING:
2033 if (h2c->st0 == H2_CS_FRAME_P)
2034 ret = h2c_handle_ping(h2c);
2035
2036 if (h2c->st0 == H2_CS_FRAME_A)
2037 ret = h2c_ack_ping(h2c);
2038 break;
2039
Willy Tarreau26f95952017-07-27 17:18:30 +02002040 case H2_FT_WINDOW_UPDATE:
2041 if (h2c->st0 == H2_CS_FRAME_P)
2042 ret = h2c_handle_window_update(h2c, h2s);
2043 break;
2044
Willy Tarreau61290ec2017-10-17 08:19:21 +02002045 case H2_FT_CONTINUATION:
2046 /* we currently don't support CONTINUATION frames since
2047 * we have nowhere to store the partial HEADERS frame.
2048 * Let's abort the stream on an INTERNAL_ERROR here.
2049 */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002050 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreau61290ec2017-10-17 08:19:21 +02002051 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
Willy Tarreaua20a5192017-12-27 11:02:06 +01002052 h2c->st0 = H2_CS_FRAME_E;
2053 }
Willy Tarreau61290ec2017-10-17 08:19:21 +02002054 break;
2055
Willy Tarreau13278b42017-10-13 19:23:14 +02002056 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002057 if (h2c->st0 == H2_CS_FRAME_P) {
2058 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
2059 if (tmp_h2s) {
2060 h2s = tmp_h2s;
2061 ret = 1;
2062 }
2063 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002064 break;
2065
Willy Tarreau454f9052017-10-26 19:40:35 +02002066 case H2_FT_DATA:
2067 if (h2c->st0 == H2_CS_FRAME_P)
2068 ret = h2c_frt_handle_data(h2c, h2s);
2069
2070 if (h2c->st0 == H2_CS_FRAME_A)
2071 ret = h2c_send_strm_wu(h2c);
2072 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002073
Willy Tarreau92153fc2017-12-03 19:46:19 +01002074 case H2_FT_PRIORITY:
2075 if (h2c->st0 == H2_CS_FRAME_P)
2076 ret = h2c_handle_priority(h2c);
2077 break;
2078
Willy Tarreaucd234e92017-08-18 10:59:39 +02002079 case H2_FT_RST_STREAM:
2080 if (h2c->st0 == H2_CS_FRAME_P)
2081 ret = h2c_handle_rst_stream(h2c, h2s);
2082 break;
2083
Willy Tarreaue96b0922017-10-30 00:28:29 +01002084 case H2_FT_GOAWAY:
2085 if (h2c->st0 == H2_CS_FRAME_P)
2086 ret = h2c_handle_goaway(h2c);
2087 break;
2088
Willy Tarreau1c661982017-10-30 13:52:01 +01002089 case H2_FT_PUSH_PROMISE:
2090 /* not permitted here, RFC7540#5.1 */
2091 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002092 if (!h2c->nb_streams) {
2093 /* only log if no other stream can report the error */
2094 sess_log(h2c->conn->owner);
2095 }
Willy Tarreau1c661982017-10-30 13:52:01 +01002096 break;
2097
2098 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002099 default:
2100 /* drop frames that we ignore. They may be larger than
2101 * the buffer so we drain all of their contents until
2102 * we reach the end.
2103 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002104 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2105 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002106 h2c->dfl -= ret;
2107 ret = h2c->dfl == 0;
2108 }
2109
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002110 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002111 /* We may have to send an RST if not done yet */
2112 if (h2s->st == H2_SS_ERROR)
2113 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002114
Willy Tarreaua20a5192017-12-27 11:02:06 +01002115 if (h2c->st0 == H2_CS_FRAME_E)
2116 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002117
Willy Tarreau7e98c052017-10-10 15:56:59 +02002118 /* error or missing data condition met above ? */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002119 if (ret <= 0) {
2120 h2s = NULL;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002121 break;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002122 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002123
2124 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002125 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002126 h2c->st0 = H2_CS_FRAME_H;
2127 }
2128 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002129
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002130 if (h2c->rcvd_c > 0 &&
2131 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2132 h2c_send_conn_wu(h2c);
2133
Willy Tarreau52eed752017-09-22 15:05:09 +02002134 fail:
2135 /* we can go here on missing data, blocked response or error */
Olivier Houchard638b7992018-08-16 15:41:52 +02002136 if (h2s && h2s->cs && b_data(&h2s->rxbuf)) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002137 /* we may have to signal the upper layers */
2138 h2s->cs->flags |= CS_FL_RCV_MORE;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002139 if (h2s->recv_wait) {
2140 h2s->recv_wait->wait_reason &= ~SUB_CAN_RECV;
2141 tasklet_wakeup(h2s->recv_wait->task);
2142 h2s->recv_wait = NULL;
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002143 }
2144 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002145 return;
Willy Tarreaubc933932017-10-09 16:21:43 +02002146}
2147
2148/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2149 * the end.
2150 */
2151static int h2_process_mux(struct h2c *h2c)
2152{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002153 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002154
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002155 /* start by sending possibly pending window updates */
2156 if (h2c->rcvd_c > 0 &&
2157 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2158 h2c_send_conn_wu(h2c) < 0)
2159 goto fail;
2160
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002161 /* First we always process the flow control list because the streams
2162 * waiting there were already elected for immediate emission but were
2163 * blocked just on this.
2164 */
2165
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002166 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002167 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2168 h2c->st0 >= H2_CS_ERROR)
2169 break;
2170
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002171 h2s->flags &= ~H2_SF_BLK_ANY;
2172 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
2173 tasklet_wakeup(h2s->send_wait->task);
2174 h2s->send_wait = NULL;
2175 LIST_DEL(&h2s->list);
2176 LIST_INIT(&h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002177 }
2178
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002179 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002180 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2181 break;
2182
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002183 h2s->flags &= ~H2_SF_BLK_ANY;
2184 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
2185 tasklet_wakeup(h2s->send_wait->task);
2186 h2s->send_wait = NULL;
2187 LIST_DEL(&h2s->list);
2188 LIST_INIT(&h2s->list);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002189 }
2190
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002191 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002192 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002193 if (h2c->st0 == H2_CS_ERROR) {
2194 if (h2c->max_id >= 0) {
2195 h2c_send_goaway_error(h2c, NULL);
2196 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2197 return 0;
2198 }
2199
2200 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2201 }
2202 return 1;
2203 }
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002204 return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list);
Willy Tarreaubc933932017-10-09 16:21:43 +02002205}
2206
Willy Tarreau62f52692017-10-08 23:01:42 +02002207
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002208/* Attempt to read data, and subscribe if none available */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002209static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002210{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002211 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002212 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002213 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002214 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002215
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002216 if (h2c->wait_event.wait_reason & SUB_CAN_RECV)
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002217 return 0;
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002218
Willy Tarreau315d8072017-12-10 22:17:57 +01002219 if (!h2_recv_allowed(h2c))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002220 return 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002221
Willy Tarreau44e973f2018-03-01 17:49:30 +01002222 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002223 if (!buf) {
2224 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002225 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002226 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002227
Olivier Houchard7505f942018-08-21 18:10:44 +02002228 do {
2229 max = buf->size - b_data(buf);
2230 if (max)
2231 ret = conn->xprt->rcv_buf(conn, buf, max, 0);
2232 else
2233 ret = 0;
2234 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002235
Olivier Houchard7505f942018-08-21 18:10:44 +02002236 if (h2_recv_allowed(h2c)) {
2237 conn_xprt_want_recv(conn);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002238 conn->xprt->subscribe(conn, SUB_CAN_RECV, &h2c->wait_event);
Olivier Houchard7505f942018-08-21 18:10:44 +02002239 }
Olivier Houcharda1411e62018-08-17 18:42:48 +02002240 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002241 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002242 return 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002243 }
2244
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002245 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002246 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002247 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002248}
2249
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002250/* Try to send data if possible */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002251static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002252{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002253 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002254 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002255 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002256
2257 if (conn->flags & CO_FL_ERROR)
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002258 return 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002259
Olivier Houchard7505f942018-08-21 18:10:44 +02002260
Willy Tarreaua2af5122017-10-09 11:56:46 +02002261 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2262 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002263 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002264 }
2265
Willy Tarreaubc933932017-10-09 16:21:43 +02002266 /* This loop is quite simple : it tries to fill as much as it can from
2267 * pending streams into the existing buffer until it's reportedly full
2268 * or the end of send requests is reached. Then it tries to send this
2269 * buffer's contents out, marks it not full if at least one byte could
2270 * be sent, and tries again.
2271 *
2272 * The snd_buf() function normally takes a "flags" argument which may
2273 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2274 * data immediately comes and CO_SFL_STREAMER to indicate that the
2275 * connection is streaming lots of data (used to increase TLS record
2276 * size at the expense of latency). The former can be sent any time
2277 * there's a buffer full flag, as it indicates at least one stream
2278 * attempted to send and failed so there are pending data. An
2279 * alternative would be to set it as long as there's an active stream
2280 * but that would be problematic for ACKs until we have an absolute
2281 * guarantee that all waiters have at least one byte to send. The
2282 * latter should possibly not be set for now.
2283 */
2284
2285 done = 0;
2286 while (!done) {
2287 unsigned int flags = 0;
2288
2289 /* fill as much as we can into the current buffer */
2290 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2291 done = h2_process_mux(h2c);
2292
2293 if (conn->flags & CO_FL_ERROR)
2294 break;
2295
2296 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2297 flags |= CO_SFL_MSG_MORE;
2298
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002299 if (b_data(&h2c->mbuf)) {
2300 int ret = conn->xprt->snd_buf(conn, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002301 if (!ret)
2302 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002303 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002304 b_del(&h2c->mbuf, ret);
2305 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002306 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002307
2308 /* wrote at least one byte, the buffer is not full anymore */
2309 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2310 }
2311
Willy Tarreaua2af5122017-10-09 11:56:46 +02002312 if (conn->flags & CO_FL_SOCK_WR_SH) {
2313 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002314 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002315 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002316 /* We're not full anymore, so we can wake any task that are waiting
2317 * for us.
2318 */
2319 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002320 while (!LIST_ISEMPTY(&h2c->send_list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002321 struct h2s *h2s = LIST_ELEM(h2c->send_list.n,
2322 struct h2s *, list);
2323 LIST_DEL(&h2s->list);
2324 LIST_INIT(&h2s->list);
2325 h2s->send_wait->wait_reason &= ~SUB_CAN_SEND;
2326 tasklet_wakeup(h2s->send_wait->task);
2327 h2s->send_wait = NULL;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002328 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002329 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002330 /* We're done, no more to send */
2331 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002332 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002333schedule:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002334 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
2335 conn->xprt->subscribe(conn, SUB_CAN_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002336 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002337}
2338
2339static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2340{
2341 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002342 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002343
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002344 if (!(h2c->wait_event.wait_reason & SUB_CAN_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002345 ret = h2_send(h2c);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002346 if (!(h2c->wait_event.wait_reason & SUB_CAN_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002347 ret |= h2_recv(h2c);
2348 if (ret)
2349 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002350 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002351}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002352
Willy Tarreau62f52692017-10-08 23:01:42 +02002353/* callback called on any event by the connection handler.
2354 * It applies changes and returns zero, or < 0 if it wants immediate
2355 * destruction of the connection (which normally doesn not happen in h2).
2356 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002357static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002358{
Olivier Houchard7505f942018-08-21 18:10:44 +02002359 struct connection *conn = h2c->conn;
Willy Tarreau8ec14062017-12-30 18:08:13 +01002360 struct session *sess = conn->owner;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002361
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002362 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002363 h2_process_demux(h2c);
2364
2365 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002366 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002367
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002368 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002369 h2c->flags &= ~H2_CF_DEM_DFULL;
2370 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002371 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002372
Willy Tarreau8ec14062017-12-30 18:08:13 +01002373 if (sess && unlikely(sess->fe->state == PR_STSTOPPED)) {
2374 /* frontend is stopping, reload likely in progress, let's try
2375 * to announce a graceful shutdown if not yet done. We don't
2376 * care if it fails, it will be tried again later.
2377 */
2378 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2379 if (h2c->last_sid < 0)
2380 h2c->last_sid = (1U << 31) - 1;
2381 h2c_send_goaway_error(h2c, NULL);
2382 }
2383 }
2384
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002385 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002386 * If we received early data, and the handshake is done, wake
2387 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002388 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002389 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2390 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2391 struct eb32_node *node;
2392 struct h2s *h2s;
2393
2394 h2c->flags |= H2_CF_WAIT_FOR_HS;
2395 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2396
2397 while (node) {
2398 h2s = container_of(node, struct h2s, by_id);
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002399 if ((h2s->cs->flags & CS_FL_WAIT_FOR_HS) &&
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002400 h2s->recv_wait) {
2401 struct wait_event *sw = h2s->recv_wait;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002402 sw->wait_reason &= ~SUB_CAN_RECV;
2403 tasklet_wakeup(sw->task);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002404 h2s->recv_wait = NULL;
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002405 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002406 node = eb32_next(node);
2407 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002408 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002409
Willy Tarreau26bd7612017-10-09 16:47:04 +02002410 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002411 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2412 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2413 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002414 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002415
2416 if (eb_is_empty(&h2c->streams_by_id)) {
2417 /* no more stream, kill the connection now */
2418 h2_release(conn);
2419 return -1;
2420 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002421 }
2422
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002423 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002424 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002425
2426 /* stop being notified of incoming data if we can't process them */
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002427 if (!h2_recv_allowed(h2c))
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002428 __conn_xprt_stop_recv(conn);
Olivier Houchard7505f942018-08-21 18:10:44 +02002429 else
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002430 __conn_xprt_want_recv(conn);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002431
2432 /* adjust output polling */
Willy Tarreau51606832017-10-17 15:30:07 +02002433 if (!(conn->flags & CO_FL_SOCK_WR_SH) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002434 h2c->st0 != H2_CS_ERROR2 && !(h2c->flags & H2_CF_GOAWAY_FAILED) &&
Willy Tarreau51606832017-10-17 15:30:07 +02002435 (h2c->st0 == H2_CS_ERROR ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002436 b_data(&h2c->mbuf) ||
Willy Tarreau51606832017-10-17 15:30:07 +02002437 (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) ||
2438 (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) {
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002439 __conn_xprt_want_send(conn);
2440 }
2441 else {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002442 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002443 __conn_xprt_stop_send(conn);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002444 }
2445
Willy Tarreau3f133572017-10-31 19:21:06 +01002446 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002447 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002448 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002449 task_queue(h2c->task);
2450 }
2451 else
2452 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002453 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002454
Olivier Houchard7505f942018-08-21 18:10:44 +02002455 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002456 return 0;
2457}
2458
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002459static int h2_wake(struct connection *conn)
2460{
2461 struct h2c *h2c = conn->mux_ctx;
2462
2463 return (h2_process(h2c));
2464}
2465
2466
Willy Tarreauea392822017-10-31 10:02:25 +01002467/* Connection timeout management. The principle is that if there's no receipt
2468 * nor sending for a certain amount of time, the connection is closed. If the
2469 * MUX buffer still has lying data or is not allocatable, the connection is
2470 * immediately killed. If it's allocatable and empty, we attempt to send a
2471 * GOAWAY frame.
2472 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002473static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002474{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002475 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002476 int expired = tick_is_expired(t->expire, now_ms);
2477
Willy Tarreau0975f112018-03-29 15:22:59 +02002478 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002479 return t;
2480
Willy Tarreau0975f112018-03-29 15:22:59 +02002481 task_delete(t);
2482 task_free(t);
2483
2484 if (!h2c) {
2485 /* resources were already deleted */
2486 return NULL;
2487 }
2488
2489 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002490 h2c_error(h2c, H2_ERR_NO_ERROR);
2491 h2_wake_some_streams(h2c, 0, 0);
2492
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002493 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002494 /* don't even try to send a GOAWAY, the buffer is stuck */
2495 h2c->flags |= H2_CF_GOAWAY_FAILED;
2496 }
2497
2498 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002499 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002500 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2501 h2c->flags |= H2_CF_GOAWAY_FAILED;
2502
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002503 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
2504 int ret = h2c->conn->xprt->snd_buf(h2c->conn, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002505 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002506 b_del(&h2c->mbuf, ret);
2507 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002508 }
2509 }
Willy Tarreauea392822017-10-31 10:02:25 +01002510
Willy Tarreau0975f112018-03-29 15:22:59 +02002511 /* either we can release everything now or it will be done later once
2512 * the last stream closes.
2513 */
2514 if (eb_is_empty(&h2c->streams_by_id))
2515 h2_release(h2c->conn);
Willy Tarreauea392822017-10-31 10:02:25 +01002516
Willy Tarreauea392822017-10-31 10:02:25 +01002517 return NULL;
2518}
2519
2520
Willy Tarreau62f52692017-10-08 23:01:42 +02002521/*******************************************/
2522/* functions below are used by the streams */
2523/*******************************************/
2524
2525/*
2526 * Attach a new stream to a connection
2527 * (Used for outgoing connections)
2528 */
2529static struct conn_stream *h2_attach(struct connection *conn)
2530{
2531 return NULL;
2532}
2533
2534/* callback used to update the mux's polling flags after changing a cs' status.
2535 * The caller (cs_update_mux_polling) will take care of propagating any changes
2536 * to the transport layer.
2537 */
2538static void h2_update_poll(struct conn_stream *cs)
2539{
Willy Tarreau1d393222017-10-17 10:26:19 +02002540 struct h2s *h2s = cs->ctx;
2541
2542 if (!h2s)
2543 return;
2544
Willy Tarreaud7739c82017-10-30 15:38:23 +01002545 /* we may unblock a blocked read */
2546
Willy Tarreau315d8072017-12-10 22:17:57 +01002547 if (cs->flags & CS_FL_DATA_RD_ENA) {
2548 /* the stream indicates it's willing to read */
Willy Tarreaud7739c82017-10-30 15:38:23 +01002549 h2s->h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreaud13bf272017-12-14 10:34:52 +01002550 if (h2s->h2c->dsi == h2s->id) {
Willy Tarreau315d8072017-12-10 22:17:57 +01002551 conn_xprt_want_recv(cs->conn);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002552 tasklet_wakeup(h2s->h2c->wait_event.task);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002553 conn_xprt_want_send(cs->conn);
2554 }
Willy Tarreaud7739c82017-10-30 15:38:23 +01002555 }
2556
Willy Tarreau1d393222017-10-17 10:26:19 +02002557 /* Note: the stream and stream-int code doesn't allow us to perform a
2558 * synchronous send() here unfortunately, because this code is called
2559 * as si_update() from the process_stream() context. This means that
2560 * we have to queue the current cs and defer its processing after the
2561 * connection's cs list is processed anyway.
2562 */
2563
2564 if (cs->flags & CS_FL_DATA_WR_ENA) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002565 if (!b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_SOCK_WR_SH))
2566 conn_xprt_want_send(cs->conn);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002567 tasklet_wakeup(h2s->h2c->wait_event.task);
Willy Tarreau1d393222017-10-17 10:26:19 +02002568 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002569 /* We don't support unsubscribing from here, it shouldn't be a problem */
Willy Tarreau1d393222017-10-17 10:26:19 +02002570
2571 /* this can happen from within si_chk_snd() */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002572 if (b_data(&h2s->h2c->mbuf) && !(cs->conn->flags & CO_FL_XPRT_WR_ENA))
Willy Tarreau1d393222017-10-17 10:26:19 +02002573 conn_xprt_want_send(cs->conn);
Willy Tarreau62f52692017-10-08 23:01:42 +02002574}
2575
2576/*
2577 * Detach the stream from the connection and possibly release the connection.
2578 */
2579static void h2_detach(struct conn_stream *cs)
2580{
Willy Tarreau60935142017-10-16 18:11:19 +02002581 struct h2s *h2s = cs->ctx;
2582 struct h2c *h2c;
2583
2584 cs->ctx = NULL;
2585 if (!h2s)
2586 return;
2587
2588 h2c = h2s->h2c;
2589 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02002590 h2c->nb_cs--;
Willy Tarreauf2101912018-07-19 10:11:38 +02002591 if (h2c->flags & H2_CF_DEM_TOOMANY &&
2592 !h2_has_too_many_cs(h2c)) {
2593 h2c->flags &= ~H2_CF_DEM_TOOMANY;
2594 if (h2_recv_allowed(h2c)) {
2595 __conn_xprt_want_recv(h2c->conn);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002596 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreauf2101912018-07-19 10:11:38 +02002597 conn_xprt_want_send(h2c->conn);
2598 }
2599 }
Willy Tarreau60935142017-10-16 18:11:19 +02002600
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002601 /* this stream may be blocked waiting for some data to leave (possibly
2602 * an ES or RST frame), so orphan it in this case.
2603 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002604 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02002605 (h2c->st0 < H2_CS_ERROR) &&
Willy Tarreau3041fcc2018-03-29 15:41:32 +02002606 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01002607 return;
2608
Willy Tarreau45f752e2017-10-30 15:44:59 +01002609 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
2610 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
2611 /* unblock the connection if it was blocked on this
2612 * stream.
2613 */
2614 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
2615 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
2616 conn_xprt_want_recv(cs->conn);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002617 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau45f752e2017-10-30 15:44:59 +01002618 conn_xprt_want_send(cs->conn);
2619 }
2620
Willy Tarreau71049cc2018-03-28 13:56:39 +02002621 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02002622
Willy Tarreaue323f342018-03-28 13:51:45 +02002623 /* We don't want to close right now unless we're removing the
2624 * last stream, and either the connection is in error, or it
2625 * reached the ID already specified in a GOAWAY frame received
2626 * or sent (as seen by last_sid >= 0).
2627 */
2628 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
2629 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
Willy Tarreau42d55b92018-06-13 14:24:56 +02002630 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
Willy Tarreaue323f342018-03-28 13:51:45 +02002631 (h2c->flags & H2_CF_GOAWAY_FAILED) ||
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002632 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
Willy Tarreaue323f342018-03-28 13:51:45 +02002633 (conn_xprt_read0_pending(h2c->conn) ||
2634 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) {
2635 /* no more stream will come, kill it now */
2636 h2_release(h2c->conn);
2637 }
2638 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002639 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02002640 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
2641 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01002642 }
Willy Tarreaue323f342018-03-28 13:51:45 +02002643 else
2644 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02002645 }
Willy Tarreau62f52692017-10-08 23:01:42 +02002646}
2647
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002648static void h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002649{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002650 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002651 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002652
Willy Tarreau721c9742017-11-07 11:05:42 +01002653 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002654 return;
2655
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002656 /* if no outgoing data was seen on this stream, it means it was
2657 * closed with a "tcp-request content" rule that is normally
2658 * used to kill the connection ASAP (eg: limit abuse). In this
2659 * case we send a goaway to close the connection.
2660 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002661 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002662 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002663 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002664
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002665 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2666 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002667 h2c_send_goaway_error(h2c, h2s) <= 0)
2668 return;
2669 if (b_data(&h2c->mbuf) && !(h2c->conn->flags & CO_FL_XPRT_WR_ENA))
2670 conn_xprt_want_send(h2c->conn);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002671
Willy Tarreau00dd0782018-03-01 16:31:34 +01002672 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002673
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002674 return;
2675add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002676 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002677 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002678 if (h2s->flags & H2_SF_BLK_MFCTL) {
2679 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2680 h2s->send_wait = sw;
2681 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2682 h2s->send_wait = sw;
2683 LIST_ADDQ(&h2c->send_list, &h2s->list);
2684 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002685 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002686 /* Let the handler know we want shutr */
2687 sw->handle = (void *)((long)sw->handle | 1);
2688
Willy Tarreau62f52692017-10-08 23:01:42 +02002689}
2690
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002691static void h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02002692{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002693 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002694 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002695
Willy Tarreau721c9742017-11-07 11:05:42 +01002696 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002697 return;
2698
Willy Tarreau67434202017-11-06 20:20:51 +01002699 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01002700 /* we can cleanly close using an empty data frame only after headers */
2701
2702 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
2703 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002704 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01002705
2706 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01002707 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01002708 else
2709 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002710 } else {
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002711 /* if no outgoing data was seen on this stream, it means it was
2712 * closed with a "tcp-request content" rule that is normally
2713 * used to kill the connection ASAP (eg: limit abuse). In this
2714 * case we send a goaway to close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01002715 */
Willy Tarreau90c32322017-11-24 08:00:30 +01002716 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002717 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002718 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01002719
Willy Tarreau926fa4c2017-11-07 14:42:12 +01002720 if (!(h2s->flags & H2_SF_OUTGOING_DATA) &&
2721 !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002722 h2c_send_goaway_error(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002723 goto add_to_list;
Willy Tarreaua1349f02017-10-31 07:41:55 +01002724
Willy Tarreau00dd0782018-03-01 16:31:34 +01002725 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01002726 }
2727
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002728 if (b_data(&h2s->h2c->mbuf) && !(h2c->conn->flags & CO_FL_XPRT_WR_ENA))
2729 conn_xprt_want_send(h2c->conn);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002730
2731 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002732 if (LIST_ISEMPTY(&h2s->list)) {
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002733 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002734 if (h2s->flags & H2_SF_BLK_MFCTL) {
2735 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
2736 h2s->send_wait = sw;
2737 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
2738 h2s->send_wait = sw;
2739 LIST_ADDQ(&h2c->send_list, &h2s->list);
2740 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02002741 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002742 /* let the handler know we want to shutw */
2743 sw->handle = (void *)((long)(sw->handle) | 2);
2744
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002745}
2746
2747static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
2748{
2749 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002750 long reason = (long)h2s->wait_event.handle;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002751
2752 if (reason & 1)
2753 h2_do_shutr(h2s);
2754 if (reason & 2)
2755 h2_do_shutw(h2s);
2756
2757 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02002758}
2759
Olivier Houchard8ae735d2018-09-11 18:24:28 +02002760static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
2761{
2762 struct h2s *h2s = cs->ctx;
2763
2764 if (!mode)
2765 return;
2766
2767 h2_do_shutr(h2s);
2768}
2769
2770static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
2771{
2772 struct h2s *h2s = cs->ctx;
2773
2774 h2_do_shutw(h2s);
2775}
2776
Willy Tarreau13278b42017-10-13 19:23:14 +02002777/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1
2778 * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't
2779 * proceed. Stream errors are reported in h2s->errcode and connection errors
Willy Tarreau68472622017-12-11 18:36:37 +01002780 * in h2c->errcode.
Willy Tarreau13278b42017-10-13 19:23:14 +02002781 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002782static int h2_frt_decode_headers(struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02002783{
2784 struct h2c *h2c = h2s->h2c;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002785 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02002786 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002787 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02002788 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02002789 unsigned int msgf;
Willy Tarreau937f7602018-02-26 15:22:17 +01002790 struct buffer *csbuf;
Willy Tarreau13278b42017-10-13 19:23:14 +02002791 int flen = h2c->dfl;
2792 int outlen = 0;
2793 int wrap;
2794 int try;
2795
2796 if (!h2c->dfl) {
2797 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame!
Willy Tarreaua20a5192017-12-27 11:02:06 +01002798 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau13278b42017-10-13 19:23:14 +02002799 return 0;
2800 }
2801
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002802 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau68472622017-12-11 18:36:37 +01002803 return 0; // incomplete input frame
2804
Willy Tarreau13278b42017-10-13 19:23:14 +02002805 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002806 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02002807 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02002808 copy = alloc_trash_chunk();
2809 if (!copy) {
2810 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
2811 goto fail;
2812 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002813 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
2814 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
2815 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02002816 }
2817
2818 /* The padlen is the first byte before data, and the padding appears
2819 * after data. padlen+data+padding are included in flen.
2820 */
2821 if (h2c->dff & H2_F_HEADERS_PADDED) {
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002822 h2c->dpl = *hdrs;
2823 if (h2c->dpl >= flen) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002824 /* RFC7540#6.2 : pad length = length of frame payload or greater */
2825 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002826 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002827 }
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002828 flen -= h2c->dpl + 1;
Willy Tarreau13278b42017-10-13 19:23:14 +02002829 hdrs += 1; // skip Pad Length
2830 }
2831
2832 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
2833 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002834 if (read_n32(hdrs) == h2s->id) {
2835 /* RFC7540#5.3.1 : stream dep may not depend on itself */
2836 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02002837 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01002838 }
2839
Willy Tarreau13278b42017-10-13 19:23:14 +02002840 hdrs += 5; // stream dep = 4, weight = 1
2841 flen -= 5;
2842 }
2843
2844 /* FIXME: lack of END_HEADERS means there's a continuation frame, we
2845 * don't support this for now and can't even decompress so we have to
2846 * break the connection.
2847 */
2848 if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) {
2849 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau68dd9852017-07-03 14:44:26 +02002850 goto fail;
Willy Tarreau13278b42017-10-13 19:23:14 +02002851 }
2852
Olivier Houchard638b7992018-08-16 15:41:52 +02002853 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreau937f7602018-02-26 15:22:17 +01002854 if (!csbuf) {
2855 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002856 goto fail;
2857 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002858
Willy Tarreau937f7602018-02-26 15:22:17 +01002859 /* we can't retry a failed decompression operation so we must be very
2860 * careful not to take any risks. In practice the output buffer is
2861 * always empty except maybe for trailers, in which case we simply have
2862 * to wait for the upper layer to finish consuming what is available.
2863 */
2864 if (b_data(csbuf))
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002865 goto fail;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002866
Willy Tarreau937f7602018-02-26 15:22:17 +01002867 csbuf->head = 0;
2868 try = b_size(csbuf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002869
2870 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
2871 sizeof(list)/sizeof(list[0]), tmp);
2872 if (outlen < 0) {
2873 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2874 goto fail;
2875 }
2876
2877 /* OK now we have our header list in <list> */
Willy Tarreau174b06a2018-04-25 18:13:58 +02002878 msgf = (h2c->dff & H2_F_DATA_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreau937f7602018-02-26 15:22:17 +01002879 outlen = h2_make_h1_request(list, b_tail(csbuf), try, &msgf);
Willy Tarreau59a10fb2017-11-21 20:03:02 +01002880
2881 if (outlen < 0) {
2882 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
2883 goto fail;
2884 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002885
Willy Tarreau174b06a2018-04-25 18:13:58 +02002886 if (msgf & H2_MSGF_BODY) {
2887 /* a payload is present */
2888 if (msgf & H2_MSGF_BODY_CL)
2889 h2s->flags |= H2_SF_DATA_CLEN;
2890 else if (!(msgf & H2_MSGF_BODY_TUNNEL))
2891 h2s->flags |= H2_SF_DATA_CHNK;
2892 }
2893
Willy Tarreau13278b42017-10-13 19:23:14 +02002894 /* now consume the input data */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002895 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau13278b42017-10-13 19:23:14 +02002896 h2c->st0 = H2_CS_FRAME_H;
Willy Tarreau937f7602018-02-26 15:22:17 +01002897 b_add(csbuf, outlen);
Willy Tarreau13278b42017-10-13 19:23:14 +02002898
Willy Tarreau39d68502018-03-02 12:26:37 +01002899 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
Willy Tarreau13278b42017-10-13 19:23:14 +02002900 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01002901 h2s->cs->flags |= CS_FL_REOS;
2902 }
Willy Tarreau937f7602018-02-26 15:22:17 +01002903
Willy Tarreau68dd9852017-07-03 14:44:26 +02002904 leave:
2905 free_trash_chunk(copy);
Willy Tarreau13278b42017-10-13 19:23:14 +02002906 return outlen;
Willy Tarreau68dd9852017-07-03 14:44:26 +02002907 fail:
2908 outlen = 0;
2909 goto leave;
Willy Tarreau13278b42017-10-13 19:23:14 +02002910}
2911
Willy Tarreau454f9052017-10-26 19:40:35 +02002912/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
2913 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
2914 * in use, a new chunk is emitted for each frame. This is supposed to fit
2915 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
2916 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
2917 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
2918 * parser state is automatically updated. Returns the number of bytes emitted
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002919 * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be
2920 * checked to know if some data remain pending (an empty DATA frame can return
2921 * 0 as a valid result). Stream errors are reported in h2s->errcode and
2922 * connection errors in h2c->errcode. The caller must already have checked the
2923 * frame header and ensured that the frame was complete or the buffer full. It
2924 * changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02002925 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01002926static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02002927{
2928 struct h2c *h2c = h2s->h2c;
2929 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002930 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002931 unsigned int chklen = 0;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002932 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02002933
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002934 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02002935
2936 /* The padlen is the first byte before data, and the padding appears
2937 * after data. padlen+data+padding are included in flen.
2938 */
Willy Tarreau79127812017-12-03 21:06:59 +01002939 if (h2c->dff & H2_F_DATA_PADDED) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002940 if (b_data(&h2c->dbuf) < 1)
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002941 return 0;
2942
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002943 h2c->dpl = *(uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau05e5daf2017-12-11 15:17:36 +01002944 if (h2c->dpl >= h2c->dfl) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002945 /* RFC7540#6.1 : pad length = length of frame payload or greater */
2946 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau454f9052017-10-26 19:40:35 +02002947 return 0;
2948 }
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002949
2950 /* skip the padlen byte */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002951 b_del(&h2c->dbuf, 1);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002952 h2c->dfl--;
2953 h2c->rcvd_c++; h2c->rcvd_s++;
2954 h2c->dff &= ~H2_F_DATA_PADDED;
Willy Tarreau454f9052017-10-26 19:40:35 +02002955 }
2956
Olivier Houchard638b7992018-08-16 15:41:52 +02002957 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01002958 if (!csbuf) {
2959 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002960 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002961 }
2962
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002963 flen = h2c->dfl - h2c->dpl;
2964 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01002965 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002966
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002967 if (flen > b_data(&h2c->dbuf)) {
2968 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002969 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01002970 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002971 }
2972
2973 if (unlikely(b_space_wraps(csbuf))) {
2974 /* it doesn't fit and the buffer is fragmented,
2975 * so let's defragment it and try again.
2976 */
2977 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02002978 }
2979
Willy Tarreaueba10f22018-04-25 20:44:22 +02002980 /* chunked-encoding requires more room */
2981 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01002982 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02002983 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
2984 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
2985 (chklen < 1048576) ? 4 : 8;
2986 chklen += 4; // CRLF, CRLF
2987 }
2988
Willy Tarreau8fc016d2017-12-11 18:27:15 +01002989 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01002990 if (flen + chklen > b_room(csbuf)) {
2991 if (chklen >= b_room(csbuf)) {
2992 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01002993 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01002994 }
2995 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02002996 }
2997
2998 if (h2s->flags & H2_SF_DATA_CHNK) {
2999 /* emit the chunk size */
3000 unsigned int chksz = flen;
3001 char str[10];
3002 char *beg;
3003
3004 beg = str + sizeof(str);
3005 *--beg = '\n';
3006 *--beg = '\r';
3007 do {
3008 *--beg = hextab[chksz & 0xF];
3009 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003010 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003011 }
3012
Willy Tarreau454f9052017-10-26 19:40:35 +02003013 /* Block1 is the length of the first block before the buffer wraps,
3014 * block2 is the optional second block to reach the end of the frame.
3015 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003016 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003017 if (block1 > flen)
3018 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003019 block2 = flen - block1;
3020
3021 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003022 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003023
3024 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003025 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003026
Willy Tarreaueba10f22018-04-25 20:44:22 +02003027 if (h2s->flags & H2_SF_DATA_CHNK) {
3028 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003029 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003030 }
3031
Willy Tarreau454f9052017-10-26 19:40:35 +02003032 /* now mark the input data as consumed (will be deleted from the buffer
3033 * by the caller when seeing FRAME_A after sending the window update).
3034 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003035 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003036 h2c->dfl -= flen;
3037 h2c->rcvd_c += flen;
3038 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3039
3040 if (h2c->dfl > h2c->dpl) {
3041 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003042 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003043 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003044 }
3045
Willy Tarreau4a28da12018-01-04 14:41:00 +01003046 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003047 /* here we're done with the frame, all the payload (except padding) was
3048 * transferred.
3049 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003050
3051 if (h2c->dff & H2_F_DATA_END_STREAM && h2s->flags & H2_SF_DATA_CHNK) {
3052 /* emit the trailing 0 CRLF CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003053 if (b_room(csbuf) < 5) {
3054 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003055 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003056 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003057 chklen += 5;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003058 b_putblk(csbuf, "0\r\n\r\n", 5);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003059 }
3060
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003061 h2c->rcvd_c += h2c->dpl;
3062 h2c->rcvd_s += h2c->dpl;
3063 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003064 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
3065
Willy Tarreau39d68502018-03-02 12:26:37 +01003066 if (h2c->dff & H2_F_DATA_END_STREAM) {
Willy Tarreau454f9052017-10-26 19:40:35 +02003067 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau39d68502018-03-02 12:26:37 +01003068 h2s->cs->flags |= CS_FL_REOS;
3069 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003070
Willy Tarreau454b57b2018-02-26 15:50:05 +01003071 return flen + chklen;
3072 fail:
3073 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003074}
3075
Willy Tarreau5dd17352018-06-14 13:33:30 +02003076/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3077 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3078 * number of bytes sent. The caller must check the stream's status to detect
3079 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003080 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003081static 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 +02003082{
3083 struct http_hdr list[MAX_HTTP_HDR];
3084 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003085 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003086 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003087 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003088 int es_now = 0;
3089 int ret = 0;
3090 int hdr;
3091
3092 if (h2c_mux_busy(h2c, h2s)) {
3093 h2s->flags |= H2_SF_BLK_MBUSY;
3094 return 0;
3095 }
3096
Willy Tarreau44e973f2018-03-01 17:49:30 +01003097 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003098 h2c->flags |= H2_CF_MUX_MALLOC;
3099 h2s->flags |= H2_SF_BLK_MROOM;
3100 return 0;
3101 }
3102
3103 /* First, try to parse the H1 response and index it into <list>.
3104 * NOTE! Since it comes from haproxy, we *know* that a response header
3105 * block does not wrap and we can safely read it this way without
3106 * having to realign the buffer.
3107 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003108 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003109 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003110 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003111 /* incomplete or invalid response, this is abnormal coming from
3112 * haproxy and may only result in a bad errorfile or bad Lua code
3113 * so that won't be fixed, raise an error now.
3114 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003115 * FIXME: we should instead add the ability to only return a
3116 * 502 bad gateway. But in theory this is not supposed to
3117 * happen.
3118 */
3119 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3120 ret = 0;
3121 goto end;
3122 }
3123
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003124 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003125
3126 /* certain statuses have no body or an empty one, regardless of
3127 * what the headers say.
3128 */
3129 if (sl.st.status >= 100 && sl.st.status < 200) {
3130 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3131 h1m->curr_len = h1m->body_len = 0;
3132 }
3133 else if (sl.st.status == 204 || sl.st.status == 304) {
3134 /* no contents, claim c-len is present and set to zero */
3135 h1m->flags &= ~H1_MF_CHNK;
3136 h1m->flags |= H1_MF_CLEN;
3137 h1m->curr_len = h1m->body_len = 0;
3138 }
3139
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003140 chunk_reset(&outbuf);
3141
3142 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003143 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003144 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003145 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003146
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003147 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003148 break;
3149 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003150 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003151 }
3152
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003153 if (outbuf.size < 9)
3154 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003155
3156 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003157 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3158 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3159 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003160
3161 /* encode status, which necessarily is the first one */
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003162 if (outbuf.data < outbuf.size && h2s->status == 200)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003163 outbuf.area[outbuf.data++] = 0x88; // indexed field : idx[08]=(":status", "200")
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003164 else if (outbuf.data < outbuf.size && h2s->status == 304)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003165 outbuf.area[outbuf.data++] = 0x8b; // indexed field : idx[11]=(":status", "304")
Willy Tarreaua87f2022017-11-09 11:23:00 +01003166 else if (unlikely(list[0].v.len != 3)) {
3167 /* this is an unparsable response */
3168 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3169 ret = 0;
3170 goto end;
3171 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003172 else if (unlikely(outbuf.data + 2 + 3 <= outbuf.size)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003173 /* basic encoding of the status code */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003174 outbuf.area[outbuf.data++] = 0x48; // indexed name -- name=":status" (idx 8)
3175 outbuf.area[outbuf.data++] = 0x03; // 3 bytes status
3176 outbuf.area[outbuf.data++] = list[0].v.ptr[0];
3177 outbuf.area[outbuf.data++] = list[0].v.ptr[1];
3178 outbuf.area[outbuf.data++] = list[0].v.ptr[2];
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003179 }
3180 else {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003181 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003182 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003183 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003184 }
3185
3186 /* encode all headers, stop at empty name */
3187 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003188 /* these ones do not exist in H2 and must be dropped. */
3189 if (isteq(list[hdr].n, ist("connection")) ||
3190 isteq(list[hdr].n, ist("proxy-connection")) ||
3191 isteq(list[hdr].n, ist("keep-alive")) ||
3192 isteq(list[hdr].n, ist("upgrade")) ||
3193 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003194 continue;
3195
3196 if (isteq(list[hdr].n, ist("")))
3197 break; // end
3198
3199 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3200 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003201 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003202 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003203 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003204 }
3205 }
3206
3207 /* we may need to add END_STREAM */
3208 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW)
3209 es_now = 1;
3210
3211 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003212 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003213
3214 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003215 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003216
3217 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003218 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003219
3220 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003221 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003222 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003223
3224 /* for now we don't implemented CONTINUATION, so we wait for a
3225 * body or directly end in TRL2.
3226 */
3227 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003228 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003229 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003230
Willy Tarreau801250e2018-09-11 11:45:04 +02003231 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003232 h2s->flags |= H2_SF_ES_SENT;
3233 if (h2s->st == H2_SS_OPEN)
3234 h2s->st = H2_SS_HLOC;
3235 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003236 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003237 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003238 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003239 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02003240 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02003241 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02003242 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01003243 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01003244 }
Willy Tarreau001823c2018-09-12 17:25:32 +02003245
3246 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003247
3248 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003249 //fprintf(stderr, "[%d] sent simple H2 response (sid=%d) = %d bytes (%d in, ep=%u, es=%s)\n", h2c->st0, h2s->id, outbuf.len, ret, h1m->err_pos, h1m_state_str(h1m->err_state));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003250 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003251 full:
3252 h1m_init_res(h1m);
3253 h1m->err_pos = -1; // don't care about errors on the response path
3254 h2c->flags |= H2_CF_MUX_MFULL;
3255 h2s->flags |= H2_SF_BLK_MROOM;
3256 ret = 0;
3257 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003258}
3259
Willy Tarreau5dd17352018-06-14 13:33:30 +02003260/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
3261 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
3262 * the number of bytes sent. The caller must check the stream's status to
3263 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003264 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003265static 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 +02003266{
3267 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003268 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003269 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003270 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003271 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003272 int es_now = 0;
3273 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02003274 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003275 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003276
3277 if (h2c_mux_busy(h2c, h2s)) {
3278 h2s->flags |= H2_SF_BLK_MBUSY;
3279 goto end;
3280 }
3281
Willy Tarreau44e973f2018-03-01 17:49:30 +01003282 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003283 h2c->flags |= H2_CF_MUX_MALLOC;
3284 h2s->flags |= H2_SF_BLK_MROOM;
3285 goto end;
3286 }
3287
3288 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02003289 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003290 goto end;
3291
3292 chunk_reset(&outbuf);
3293
3294 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003295 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003296 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003297 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003298
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003299 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003300 break;
3301 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003302 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003303 }
3304
3305 if (outbuf.size < 9) {
3306 h2c->flags |= H2_CF_MUX_MFULL;
3307 h2s->flags |= H2_SF_BLK_MROOM;
3308 goto end;
3309 }
3310
3311 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003312 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
3313 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3314 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003315
3316 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
3317 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003318 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01003319 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003320 break;
3321 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003322 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003323 if ((long long)size > h1m->curr_len)
3324 size = h1m->curr_len;
3325 break;
3326 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02003327 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02003328 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003329 if (!ret)
3330 goto end;
3331
3332 if (ret < 0) {
3333 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02003334 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003335 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3336 goto end;
3337 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02003338 max -= ret;
3339 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003340 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003341 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003342 }
3343
Willy Tarreau801250e2018-09-11 11:45:04 +02003344 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003345 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02003346 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
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 }
3356
3357 size = chunk;
3358 h1m->curr_len = chunk;
3359 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003360 max -= ret;
3361 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003362 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02003363 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003364 if (!size)
3365 goto send_empty;
3366 }
3367
3368 /* in MSG_DATA state, continue below */
3369 size = h1m->curr_len;
3370 break;
3371 }
3372
3373 /* we have in <size> the exact number of bytes we need to copy from
3374 * the H1 buffer. We need to check this against the connection's and
3375 * the stream's send windows, and to ensure that this fits in the max
3376 * frame size and in the buffer's available space minus 9 bytes (for
3377 * the frame header). The connection's flow control is applied last so
3378 * that we can use a separate list of streams which are immediately
3379 * unblocked on window opening. Note: we don't implement padding.
3380 */
3381
Willy Tarreau5dd17352018-06-14 13:33:30 +02003382 if (size > max)
3383 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003384
3385 if (size > h2s->mws)
3386 size = h2s->mws;
3387
3388 if (size <= 0) {
3389 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houcharddddfe312018-10-10 18:51:00 +02003390 if (h2s->send_wait) {
3391 LIST_DEL(&h2s->list);
3392 LIST_INIT(&h2s->list);
3393 }
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003394 goto end;
3395 }
3396
3397 if (h2c->mfs && size > h2c->mfs)
3398 size = h2c->mfs;
3399
3400 if (size + 9 > outbuf.size) {
3401 /* we have an opportunity for enlarging the too small
3402 * available space, let's try.
3403 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003404 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003405 goto realign_again;
3406 size = outbuf.size - 9;
3407 }
3408
3409 if (size <= 0) {
3410 h2c->flags |= H2_CF_MUX_MFULL;
3411 h2s->flags |= H2_SF_BLK_MROOM;
3412 goto end;
3413 }
3414
3415 if (size > h2c->mws)
3416 size = h2c->mws;
3417
3418 if (size <= 0) {
3419 h2s->flags |= H2_SF_BLK_MFCTL;
3420 goto end;
3421 }
3422
3423 /* copy whatever we can */
3424 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02003425 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003426 if (ret == 1)
3427 len2 = 0;
3428
3429 if (!ret || len1 + len2 < size) {
3430 /* FIXME: must normally never happen */
3431 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3432 goto end;
3433 }
3434
3435 /* limit len1/len2 to size */
3436 if (len1 + len2 > size) {
3437 int sub = len1 + len2 - size;
3438
3439 if (len2 > sub)
3440 len2 -= sub;
3441 else {
3442 sub -= len2;
3443 len2 = 0;
3444 len1 -= sub;
3445 }
3446 }
3447
3448 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003449 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003450 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003451 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003452
3453 send_empty:
3454 /* we may need to add END_STREAM */
3455 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
3456 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02003457 *
3458 * FIXME: what we do here is not correct because we send end_stream
3459 * before knowing if we'll have to send a HEADERS frame for the
3460 * trailers. More importantly we're not consuming the trailing CRLF
3461 * after the end of trailers, so it will be left to the caller to
3462 * eat it. The right way to do it would be to measure trailers here
3463 * and to send ES only if there are no trailers.
3464 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003465 */
3466 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02003467 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003468 es_now = 1;
3469
3470 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003471 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003472
3473 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003474 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003475
3476 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003477 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003478
3479 /* consume incoming H1 response */
3480 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02003481 max -= size;
3482 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003483 total += size;
3484 h1m->curr_len -= size;
3485 h2s->mws -= size;
3486 h2c->mws -= size;
3487
3488 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02003489 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003490 goto new_frame;
3491 }
3492 }
3493
3494 if (es_now) {
3495 if (h2s->st == H2_SS_OPEN)
3496 h2s->st = H2_SS_HLOC;
3497 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003498 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003499
Willy Tarreau35a62702018-02-27 15:37:25 +01003500 if (!(h1m->flags & H1_MF_CHNK)) {
3501 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003502 total += max;
3503 ofs += max;
3504 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01003505
Willy Tarreau801250e2018-09-11 11:45:04 +02003506 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01003507 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003508
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003509 h2s->flags |= H2_SF_ES_SENT;
3510 }
3511
3512 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02003513 trace("[%d] sent simple H2 DATA response (sid=%d) = %d bytes out (%u in, st=%s, ep=%u, es=%s, h2cws=%d h2sws=%d) data=%u", h2c->st0, h2s->id, size+9, (unsigned int)total, h1m_state_str(h1m->state), h1m->err_pos, h1m_state_str(h1m->err_state), h2c->mws, h2s->mws, (unsigned int)b_data(buf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003514 return total;
3515}
3516
Olivier Houchard6ff20392018-07-17 18:46:31 +02003517/* Called from the upper layer, to subscribe to events, such as being able to send */
3518static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
3519{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003520 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003521 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003522 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003523
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003524 if (event_type & SUB_CAN_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003525 sw = param;
3526 if (!(sw->wait_reason & SUB_CAN_RECV)) {
3527 sw->wait_reason |= SUB_CAN_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003528 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003529 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003530 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003531 event_type &= ~SUB_CAN_RECV;
3532 }
3533 if (event_type & SUB_CAN_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02003534 sw = param;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02003535 if (!(sw->wait_reason & SUB_CAN_SEND)) {
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003536 sw->wait_reason |= SUB_CAN_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003537 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003538 h2s->send_wait = sw;
3539 if (!(h2s->flags & H2_SF_BLK_SFCTL)) {
3540 if (h2s->flags & H2_SF_BLK_MFCTL)
3541 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3542 else
3543 LIST_ADDQ(&h2c->send_list, &h2s->list);
3544 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02003545 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003546 event_type &= ~SUB_CAN_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003547 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003548 if (event_type != 0)
3549 return -1;
3550 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02003551
3552
3553}
3554
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003555static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
3556{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003557 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003558 struct h2s *h2s = cs->ctx;
3559
3560 if (event_type & SUB_CAN_RECV) {
3561 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003562 if (h2s->recv_wait == sw) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003563 sw->wait_reason &= ~SUB_CAN_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003564 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003565 }
3566 }
3567 if (event_type & SUB_CAN_SEND) {
3568 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003569 if (h2s->send_wait == sw) {
3570 LIST_DEL(&h2s->list);
3571 LIST_INIT(&h2s->list);
3572 sw->wait_reason &= ~SUB_CAN_SEND;
3573 h2s->send_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003574 }
3575 }
3576 return 0;
3577}
3578
3579
Olivier Houchard511efea2018-08-16 15:30:32 +02003580/* Called from the upper layer, to receive data */
3581static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
3582{
Olivier Houchard638b7992018-08-16 15:41:52 +02003583 struct h2s *h2s = cs->ctx;
Olivier Houchard511efea2018-08-16 15:30:32 +02003584 size_t ret = 0;
3585
3586 /* transfer possibly pending data to the upper layer */
Olivier Houchard638b7992018-08-16 15:41:52 +02003587 ret = b_xfer(buf, &h2s->rxbuf, count);
Olivier Houchard511efea2018-08-16 15:30:32 +02003588
Olivier Houchard638b7992018-08-16 15:41:52 +02003589 if (b_data(&h2s->rxbuf))
Olivier Houchard511efea2018-08-16 15:30:32 +02003590 cs->flags |= CS_FL_RCV_MORE;
3591 else {
3592 cs->flags &= ~CS_FL_RCV_MORE;
3593 if (cs->flags & CS_FL_REOS)
3594 cs->flags |= CS_FL_EOS;
Olivier Houchard638b7992018-08-16 15:41:52 +02003595 if (b_size(&h2s->rxbuf)) {
3596 b_free(&h2s->rxbuf);
3597 offer_buffers(NULL, tasks_run_queue);
3598 }
Olivier Houchard511efea2018-08-16 15:30:32 +02003599 }
3600
3601 return ret;
3602}
3603
Willy Tarreau62f52692017-10-08 23:01:42 +02003604/* Called from the upper layer, to send data */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003605static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02003606{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003607 struct h2s *h2s = cs->ctx;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02003608 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02003609 size_t ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003610
Willy Tarreau6bf641a2018-10-08 09:43:03 +02003611 if (h2s->h2c->st0 < H2_CS_FRAME_H)
3612 return 0;
3613
Willy Tarreau0bad0432018-06-14 16:54:01 +02003614 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01003615 h2s->flags |= H2_SF_OUTGOING_DATA;
3616
Willy Tarreaua40704a2018-09-11 13:52:04 +02003617 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02003618 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003619 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003620 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02003621 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02003622 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003623 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02003624 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003625 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003626 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003627
Willy Tarreau5dd17352018-06-14 13:33:30 +02003628 if (unlikely((int)ret <= 0)) {
3629 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003630 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3631 break;
3632 }
Willy Tarreau35a62702018-02-27 15:37:25 +01003633 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02003634 total += count;
3635 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003636 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01003637 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02003638 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003639 else {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003640 cs->flags |= CS_FL_ERROR;
3641 break;
3642 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02003643
3644 total += ret;
3645 count -= ret;
3646
3647 if (h2s->st >= H2_SS_ERROR)
3648 break;
3649
3650 if (h2s->flags & H2_SF_BLK_ANY)
3651 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003652 }
3653
Willy Tarreau00610962018-07-19 10:58:28 +02003654 if (h2s->st >= H2_SS_ERROR) {
3655 /* trim any possibly pending data after we close (extra CR-LF,
3656 * unprocessed trailers, abnormal extra data, ...)
3657 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02003658 total += count;
3659 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02003660 }
3661
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003662 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01003663 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003664 cs->flags |= CS_FL_ERROR;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01003665 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003666 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01003667 }
3668
Christopher Fauletd44a9b32018-07-27 11:59:41 +02003669 b_del(buf, total);
Olivier Houchard7505f942018-08-21 18:10:44 +02003670 if (total > 0) {
Olivier Houchardfab7c7e2018-08-21 16:36:10 +02003671 conn_xprt_want_send(h2s->h2c->conn);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003672 if (!(h2s->h2c->wait_event.wait_reason & SUB_CAN_SEND))
3673 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchard7505f942018-08-21 18:10:44 +02003674 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003675 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02003676}
3677
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003678/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02003679static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003680{
3681 struct h2c *h2c = conn->mux_ctx;
3682 struct h2s *h2s;
3683 struct eb32_node *node;
3684 int fctl_cnt = 0;
3685 int send_cnt = 0;
3686 int tree_cnt = 0;
3687 int orph_cnt = 0;
3688
3689 if (!h2c)
3690 return;
3691
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003692 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003693 fctl_cnt++;
3694
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003695 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003696 send_cnt++;
3697
3698 node = eb32_first(&h2c->streams_by_id);
3699 while (node) {
3700 h2s = container_of(node, struct h2s, by_id);
3701 tree_cnt++;
3702 if (!h2s->cs)
3703 orph_cnt++;
3704 node = eb32_next(node);
3705 }
3706
Willy Tarreau616ac812018-07-24 14:12:42 +02003707 chunk_appendf(msg, " st0=%d err=%d maxid=%d lastid=%d flg=0x%08x nbst=%u nbcs=%u"
3708 " fctl_cnt=%d send_cnt=%d tree_cnt=%d orph_cnt=%d dbuf=%u/%u mbuf=%u/%u",
3709 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
3710 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
3711 (unsigned int)b_data(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
3712 (unsigned int)b_data(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003713}
Willy Tarreau62f52692017-10-08 23:01:42 +02003714
3715/*******************************************************/
3716/* functions below are dedicated to the config parsers */
3717/*******************************************************/
3718
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003719/* config parser for global "tune.h2.header-table-size" */
3720static int h2_parse_header_table_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_header_table_size = atoi(args[1]);
3728 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
3729 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
3730 return -1;
3731 }
3732 return 0;
3733}
Willy Tarreau62f52692017-10-08 23:01:42 +02003734
Willy Tarreaue6baec02017-07-27 11:45:11 +02003735/* config parser for global "tune.h2.initial-window-size" */
3736static int h2_parse_initial_window_size(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_initial_window_size = atoi(args[1]);
3744 if (h2_settings_initial_window_size < 0) {
3745 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3746 return -1;
3747 }
3748 return 0;
3749}
3750
Willy Tarreau5242ef82017-07-27 11:47:28 +02003751/* config parser for global "tune.h2.max-concurrent-streams" */
3752static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
3753 struct proxy *defpx, const char *file, int line,
3754 char **err)
3755{
3756 if (too_many_args(1, args, err, NULL))
3757 return -1;
3758
3759 h2_settings_max_concurrent_streams = atoi(args[1]);
3760 if (h2_settings_max_concurrent_streams < 0) {
3761 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
3762 return -1;
3763 }
3764 return 0;
3765}
3766
Willy Tarreau62f52692017-10-08 23:01:42 +02003767
3768/****************************************/
3769/* MUX initialization and instanciation */
3770/***************************************/
3771
3772/* The mux operations */
3773const struct mux_ops h2_ops = {
3774 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02003775 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02003776 .update_poll = h2_update_poll,
Willy Tarreau62f52692017-10-08 23:01:42 +02003777 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02003778 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02003779 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02003780 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02003781 .attach = h2_attach,
3782 .detach = h2_detach,
3783 .shutr = h2_shutr,
3784 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02003785 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01003786 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02003787 .name = "H2",
3788};
3789
Christopher Faulet32f61c02018-04-10 14:33:41 +02003790/* PROTO selection : this mux registers PROTO token "h2" */
3791static struct mux_proto_list mux_proto_h2 =
3792 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02003793
3794/* config keyword parsers */
3795static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02003796 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02003797 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02003798 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreau62f52692017-10-08 23:01:42 +02003799 { 0, NULL, NULL }
3800}};
3801
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003802static void __h2_deinit(void)
3803{
Willy Tarreaubafbe012017-11-24 17:34:44 +01003804 pool_destroy(pool_head_h2s);
3805 pool_destroy(pool_head_h2c);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003806}
3807
Willy Tarreau62f52692017-10-08 23:01:42 +02003808__attribute__((constructor))
3809static void __h2_init(void)
3810{
Christopher Faulet32f61c02018-04-10 14:33:41 +02003811 register_mux_proto(&mux_proto_h2);
Willy Tarreau62f52692017-10-08 23:01:42 +02003812 cfg_register_keywords(&cfg_kws);
Willy Tarreau5ab6b572017-09-22 08:05:00 +02003813 hap_register_post_deinit(__h2_deinit);
Willy Tarreaubafbe012017-11-24 17:34:44 +01003814 pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED);
3815 pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED);
Willy Tarreau62f52692017-10-08 23:01:42 +02003816}