blob: 608b7389353f4d6bc8c507e7ff26ed31a307a8d3 [file] [log] [blame]
Willy Tarreau62f52692017-10-08 23:01:42 +02001/*
2 * HTTP/2 mux-demux for connections
3 *
4 * Copyright 2017 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <common/cfgparse.h>
14#include <common/config.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010015#include <common/h1.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020016#include <common/h2.h>
Willy Tarreau13278b42017-10-13 19:23:14 +020017#include <common/hpack-dec.h>
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +020018#include <common/hpack-enc.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020019#include <common/hpack-tbl.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010020#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010021#include <common/initcall.h>
Willy Tarreaue4820742017-07-27 13:37:23 +020022#include <common/net_helper.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020023#include <proto/connection.h>
Willy Tarreaubcd3bb32018-12-01 18:59:00 +010024#include <proto/http_htx.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010025#include <proto/session.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020026#include <proto/stream.h>
Olivier Houchard44d59142018-12-13 18:46:22 +010027#include <proto/stream_interface.h>
Willy Tarreauea392822017-10-31 10:02:25 +010028#include <types/session.h>
Willy Tarreau5ab6b572017-09-22 08:05:00 +020029#include <eb32tree.h>
Willy Tarreau62f52692017-10-08 23:01:42 +020030
31
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010032/* dummy streams returned for closed, error, refused, idle and states */
Willy Tarreau2a856182017-05-16 15:20:39 +020033static const struct h2s *h2_closed_stream;
Willy Tarreauecb9dcd2019-01-03 12:00:17 +010034static const struct h2s *h2_error_stream;
Willy Tarreau8d0d58b2018-12-23 18:29:12 +010035static const struct h2s *h2_refused_stream;
Willy Tarreau2a856182017-05-16 15:20:39 +020036static const struct h2s *h2_idle_stream;
37
Willy Tarreau5ab6b572017-09-22 08:05:00 +020038/* Connection flags (32 bit), in h2c->flags */
39#define H2_CF_NONE 0x00000000
40
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020041/* Flags indicating why writing to the mux is blocked. */
42#define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer
43#define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full
44#define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above
45
Willy Tarreau315d8072017-12-10 22:17:57 +010046/* Flags indicating why writing to the demux is blocked.
47 * The first two ones directly affect the ability for the mux to receive data
48 * from the connection. The other ones affect the mux's ability to demux
49 * received data.
50 */
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020051#define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer
52#define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full
Willy Tarreau315d8072017-12-10 22:17:57 +010053
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020054#define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy
55#define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer
56#define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer
57#define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full
Willy Tarreauf2101912018-07-19 10:11:38 +020058#define H2_CF_DEM_TOOMANY 0x00000100 // demux blocked waiting for some conn_streams to leave
59#define H2_CF_DEM_BLOCK_ANY 0x000001F0 // aggregate of the demux flags above except DALLOC/DFULL
Willy Tarreau2e5b60e2017-09-25 11:49:03 +020060
Willy Tarreau081d4722017-05-16 21:51:05 +020061/* other flags */
Willy Tarreauf2101912018-07-19 10:11:38 +020062#define H2_CF_GOAWAY_SENT 0x00001000 // a GOAWAY frame was successfully sent
63#define H2_CF_GOAWAY_FAILED 0x00002000 // a GOAWAY frame failed to be sent
64#define H2_CF_WAIT_FOR_HS 0x00004000 // We did check that at least a stream was waiting for handshake
Willy Tarreaub3fb56d2018-10-03 13:56:38 +020065#define H2_CF_IS_BACK 0x00008000 // this is an outgoing connection
Willy Tarreau97aaa672018-12-23 09:49:04 +010066#define H2_CF_WINDOW_OPENED 0x00010000 // demux increased window already advertised
Willy Tarreau081d4722017-05-16 21:51:05 +020067
Willy Tarreau5ab6b572017-09-22 08:05:00 +020068/* H2 connection state, in h2c->st0 */
69enum h2_cs {
70 H2_CS_PREFACE, // init done, waiting for connection preface
71 H2_CS_SETTINGS1, // preface OK, waiting for first settings frame
72 H2_CS_FRAME_H, // first settings frame ok, waiting for frame header
73 H2_CS_FRAME_P, // frame header OK, waiting for frame payload
Willy Tarreaua20a5192017-12-27 11:02:06 +010074 H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame
75 H2_CS_FRAME_E, // frame payload OK, trying to send RST frame
Willy Tarreau5ab6b572017-09-22 08:05:00 +020076 H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP
77 H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP
78 H2_CS_ENTRIES // must be last
79} __attribute__((packed));
80
81/* H2 connection descriptor */
82struct h2c {
83 struct connection *conn;
84
85 enum h2_cs st0; /* mux state */
86 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
87
88 /* 16 bit hole here */
89 uint32_t flags; /* connection flags: H2_CF_* */
Willy Tarreau2e2083a2019-01-31 10:34:07 +010090 uint32_t streams_limit; /* maximum number of concurrent streams the peer supports */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020091 int32_t max_id; /* highest ID known on this connection, <0 before preface */
92 uint32_t rcvd_c; /* newly received data to ACK for the connection */
93 uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */
94
95 /* states for the demux direction */
96 struct hpack_dht *ddht; /* demux dynamic header table */
Willy Tarreauc9fa0482018-07-10 17:43:27 +020097 struct buffer dbuf; /* demux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +020098
99 int32_t dsi; /* demux stream ID (<0 = idle) */
100 int32_t dfl; /* demux frame length (if dsi >= 0) */
101 int8_t dft; /* demux frame type (if dsi >= 0) */
102 int8_t dff; /* demux frame flags (if dsi >= 0) */
Willy Tarreau05e5daf2017-12-11 15:17:36 +0100103 uint8_t dpl; /* demux pad length (part of dfl), init to 0 */
104 /* 8 bit hole here */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200105 int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */
106
107 /* states for the mux direction */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200108 struct buffer mbuf; /* mux buffer */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200109 int32_t msi; /* mux stream ID (<0 = idle) */
110 int32_t mfl; /* mux frame length (if dsi >= 0) */
111 int8_t mft; /* mux frame type (if dsi >= 0) */
112 int8_t mff; /* mux frame flags (if dsi >= 0) */
113 /* 16 bit hole here */
114 int32_t miw; /* mux initial window size for all new streams */
115 int32_t mws; /* mux window size. Can be negative. */
116 int32_t mfs; /* mux's max frame size */
117
Willy Tarreauea392822017-10-31 10:02:25 +0100118 int timeout; /* idle timeout duration in ticks */
Willy Tarreau599391a2017-11-24 10:16:00 +0100119 int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */
Willy Tarreau49745612017-12-03 18:56:02 +0100120 unsigned int nb_streams; /* number of streams in the tree */
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200121 unsigned int nb_cs; /* number of attached conn_streams */
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100122 unsigned int nb_reserved; /* number of reserved streams */
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100123 unsigned int stream_cnt; /* total number of streams seen */
Willy Tarreau0b37d652018-10-03 10:33:02 +0200124 struct proxy *proxy; /* the proxy this connection was created for */
Willy Tarreauea392822017-10-31 10:02:25 +0100125 struct task *task; /* timeout management task */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200126 struct eb_root streams_by_id; /* all active streams by their ID */
127 struct list send_list; /* list of blocked streams requesting to send */
128 struct list fctl_list; /* list of streams blocked by connection's fctl */
Olivier Houchardd846c262018-10-19 17:24:29 +0200129 struct list sending_list; /* list of h2s scheduled to send data */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100130 struct buffer_wait buf_wait; /* wait list for buffer allocations */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200131 struct wait_event wait_event; /* To be used if we're waiting for I/Os */
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200132};
133
Willy Tarreau18312642017-10-11 07:57:07 +0200134/* H2 stream state, in h2s->st */
135enum h2_ss {
136 H2_SS_IDLE = 0, // idle
137 H2_SS_RLOC, // reserved(local)
138 H2_SS_RREM, // reserved(remote)
139 H2_SS_OPEN, // open
140 H2_SS_HREM, // half-closed(remote)
141 H2_SS_HLOC, // half-closed(local)
Willy Tarreau96060ba2017-10-16 18:34:34 +0200142 H2_SS_ERROR, // an error needs to be sent using RST_STREAM
Willy Tarreau18312642017-10-11 07:57:07 +0200143 H2_SS_CLOSED, // closed
144 H2_SS_ENTRIES // must be last
145} __attribute__((packed));
146
147/* HTTP/2 stream flags (32 bit), in h2s->flags */
148#define H2_SF_NONE 0x00000000
149#define H2_SF_ES_RCVD 0x00000001
150#define H2_SF_ES_SENT 0x00000002
151
152#define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM
153#define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM
154
Willy Tarreau2e5b60e2017-09-25 11:49:03 +0200155/* stream flags indicating the reason the stream is blocked */
156#define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient)
157#define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux
158#define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl
159#define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl
160#define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above
161
Willy Tarreau454f9052017-10-26 19:40:35 +0200162/* stream flags indicating how data is supposed to be sent */
163#define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length
164#define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding
165
166/* step we're currently in when sending chunks. This is needed because we may
167 * have to transfer chunks as large as a full buffer so there's no room left
168 * for size nor crlf around.
169 */
170#define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size
171#define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data
172#define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data
173
174#define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size
175
Willy Tarreau67434202017-11-06 20:20:51 +0100176#define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream
Willy Tarreauc4312d32017-11-07 12:01:53 +0100177#define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data
Willy Tarreau67434202017-11-06 20:20:51 +0100178
Willy Tarreau6cc85a52019-01-02 15:49:20 +0100179#define H2_SF_HEADERS_RCVD 0x00004000 // a HEADERS frame was received for this stream
180
Willy Tarreau18312642017-10-11 07:57:07 +0200181/* H2 stream descriptor, describing the stream as it appears in the H2C, and as
182 * it is being processed in the internal HTTP representation (H1 for now).
183 */
184struct h2s {
185 struct conn_stream *cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +0100186 struct session *sess;
Willy Tarreau18312642017-10-11 07:57:07 +0200187 struct h2c *h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +0200188 struct h1m h1m; /* request or response parser state for H1 */
Willy Tarreau18312642017-10-11 07:57:07 +0200189 struct eb32_node by_id; /* place in h2c's streams_by_id */
Willy Tarreau18312642017-10-11 07:57:07 +0200190 int32_t id; /* stream ID */
191 uint32_t flags; /* H2_SF_* */
192 int mws; /* mux window size for this stream */
193 enum h2_err errcode; /* H2 err code (H2_ERR_*) */
194 enum h2_ss st;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200195 uint16_t status; /* HTTP response status */
Willy Tarreau1915ca22019-01-24 11:49:37 +0100196 unsigned long long body_len; /* remaining body length according to content-length if H2_SF_DATA_CLEN */
Olivier Houchard638b7992018-08-16 15:41:52 +0200197 struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200198 struct wait_event wait_event; /* Wait list, when we're attempting to send a RST but we can't send */
Willy Tarreau749f5ca2019-03-21 19:19:36 +0100199 struct wait_event *recv_wait; /* recv wait_event the conn_stream associated is waiting on (via h2_subscribe) */
200 struct wait_event *send_wait; /* send wait_event the conn_stream associated is waiting on (via h2_subscribe) */
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200201 struct list list; /* To be used when adding in h2c->send_list or h2c->fctl_lsit */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100202 struct list sending_list; /* To be used when adding in h2c->sending_list */
Willy Tarreau18312642017-10-11 07:57:07 +0200203};
Willy Tarreau5ab6b572017-09-22 08:05:00 +0200204
Willy Tarreauc6405142017-09-21 20:23:50 +0200205/* descriptor for an h2 frame header */
206struct h2_fh {
207 uint32_t len; /* length, host order, 24 bits */
208 uint32_t sid; /* stream id, host order, 31 bits */
209 uint8_t ft; /* frame type */
210 uint8_t ff; /* frame flags */
211};
212
Willy Tarreau8ceae722018-11-26 11:58:30 +0100213/* the h2c connection pool */
214DECLARE_STATIC_POOL(pool_head_h2c, "h2c", sizeof(struct h2c));
215
216/* the h2s stream pool */
217DECLARE_STATIC_POOL(pool_head_h2s, "h2s", sizeof(struct h2s));
218
Willy Tarreaudc572362018-12-12 08:08:05 +0100219/* The default connection window size is 65535, it may only be enlarged using
220 * a WINDOW_UPDATE message. Since the window must never be larger than 2G-1,
221 * we'll pretend we already received the difference between the two to send
222 * an equivalent window update to enlarge it to 2G-1.
223 */
224#define H2_INITIAL_WINDOW_INCREMENT ((1U<<31)-1 - 65535)
225
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200226/* a few settings from the global section */
227static int h2_settings_header_table_size = 4096; /* initial value */
Willy Tarreaue6baec02017-07-27 11:45:11 +0200228static int h2_settings_initial_window_size = 65535; /* initial value */
Willy Tarreau5a490b62019-01-31 10:39:51 +0100229static unsigned int h2_settings_max_concurrent_streams = 100;
Willy Tarreaua24b35c2019-02-21 13:24:36 +0100230static int h2_settings_max_frame_size = 0; /* unset */
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200231
Willy Tarreau2a856182017-05-16 15:20:39 +0200232/* a dmumy closed stream */
233static const struct h2s *h2_closed_stream = &(const struct h2s){
234 .cs = NULL,
235 .h2c = NULL,
236 .st = H2_SS_CLOSED,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100237 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreauab837502017-12-27 15:07:30 +0100238 .flags = H2_SF_RST_RCVD,
Willy Tarreau2a856182017-05-16 15:20:39 +0200239 .id = 0,
240};
241
Willy Tarreauecb9dcd2019-01-03 12:00:17 +0100242/* a dmumy closed stream returning a PROTOCOL_ERROR error */
243static const struct h2s *h2_error_stream = &(const struct h2s){
244 .cs = NULL,
245 .h2c = NULL,
246 .st = H2_SS_CLOSED,
247 .errcode = H2_ERR_PROTOCOL_ERROR,
248 .flags = 0,
249 .id = 0,
250};
251
Willy Tarreau8d0d58b2018-12-23 18:29:12 +0100252/* a dmumy closed stream returning a REFUSED_STREAM error */
253static const struct h2s *h2_refused_stream = &(const struct h2s){
254 .cs = NULL,
255 .h2c = NULL,
256 .st = H2_SS_CLOSED,
257 .errcode = H2_ERR_REFUSED_STREAM,
258 .flags = 0,
259 .id = 0,
260};
261
Willy Tarreau2a856182017-05-16 15:20:39 +0200262/* and a dummy idle stream for use with any unannounced stream */
263static const struct h2s *h2_idle_stream = &(const struct h2s){
264 .cs = NULL,
265 .h2c = NULL,
266 .st = H2_SS_IDLE,
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +0100267 .errcode = H2_ERR_STREAM_CLOSED,
Willy Tarreau2a856182017-05-16 15:20:39 +0200268 .id = 0,
269};
270
Olivier Houchard9f6af332018-05-25 14:04:04 +0200271static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +0200272static int h2_send(struct h2c *h2c);
273static int h2_recv(struct h2c *h2c);
Olivier Houchard7505f942018-08-21 18:10:44 +0200274static int h2_process(struct h2c *h2c);
Olivier Houchard29fb89d2018-08-02 18:56:36 +0200275static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short state);
Willy Tarreau0b559072018-02-26 15:22:17 +0100276static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id);
Willy Tarreau4790f7c2019-01-24 11:33:02 +0100277static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags, unsigned long long *body_len);
Willy Tarreaua56a6de2018-02-26 15:59:07 +0100278static int h2_frt_transfer_data(struct h2s *h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +0200279static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state);
Olivier Houchardf502aca2018-12-14 19:42:40 +0100280static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess);
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100281static void h2s_alert(struct h2s *h2s);
Willy Tarreaufe20e5b2017-07-27 11:42:14 +0200282
Olivier Houchard7a977432019-03-21 15:47:13 +0100283static __inline int
284h2c_is_dead(struct h2c *h2c)
285{
286 if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */
287 ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */
288 (h2c->st0 >= H2_CS_ERROR && !h2c->task) || /* a timeout stroke earlier */
289 (!(h2c->conn->owner)) || /* Nobody's left to take care of the connection, drop it now */
290 (!b_data(&h2c->mbuf) && /* mux buffer empty, also process clean events below */
291 (conn_xprt_read0_pending(h2c->conn) ||
292 (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid)))))
293 return 1;
294
295 return 0;
296
297}
298
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200299/*****************************************************/
300/* functions below are for dynamic buffer management */
301/*****************************************************/
302
Willy Tarreau315d8072017-12-10 22:17:57 +0100303/* indicates whether or not the we may call the h2_recv() function to attempt
304 * to receive data into the buffer and/or demux pending data. The condition is
305 * a bit complex due to some API limits for now. The rules are the following :
306 * - if an error or a shutdown was detected on the connection and the buffer
307 * is empty, we must not attempt to receive
308 * - if the demux buf failed to be allocated, we must not try to receive and
309 * we know there is nothing pending
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100310 * - if no flag indicates a blocking condition, we may attempt to receive,
311 * regardless of whether the demux buffer is full or not, so that only
312 * de demux part decides whether or not to block. This is needed because
313 * the connection API indeed prevents us from re-enabling receipt that is
314 * already enabled in a polled state, so we must always immediately stop
315 * as soon as the demux can't proceed so as never to hit an end of read
316 * with data pending in the buffers.
Willy Tarreau315d8072017-12-10 22:17:57 +0100317 * - otherwise must may not attempt
318 */
319static inline int h2_recv_allowed(const struct h2c *h2c)
320{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200321 if (b_data(&h2c->dbuf) == 0 &&
Willy Tarreau315d8072017-12-10 22:17:57 +0100322 (h2c->st0 >= H2_CS_ERROR ||
323 h2c->conn->flags & CO_FL_ERROR ||
324 conn_xprt_read0_pending(h2c->conn)))
325 return 0;
326
327 if (!(h2c->flags & H2_CF_DEM_DALLOC) &&
Willy Tarreau6042aeb2017-12-12 11:01:44 +0100328 !(h2c->flags & H2_CF_DEM_BLOCK_ANY))
Willy Tarreau315d8072017-12-10 22:17:57 +0100329 return 1;
330
331 return 0;
332}
333
Willy Tarreau47b515a2018-12-21 16:09:41 +0100334/* restarts reading on the connection if it was not enabled */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200335static inline void h2c_restart_reading(const struct h2c *h2c, int consider_buffer)
Willy Tarreau47b515a2018-12-21 16:09:41 +0100336{
337 if (!h2_recv_allowed(h2c))
338 return;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200339 if ((!consider_buffer || !b_data(&h2c->dbuf))
340 && (h2c->wait_event.events & SUB_RETRY_RECV))
Willy Tarreau47b515a2018-12-21 16:09:41 +0100341 return;
342 tasklet_wakeup(h2c->wait_event.task);
343}
344
345
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100346/* returns true if the front connection has too many conn_streams attached */
347static inline int h2_frt_has_too_many_cs(const struct h2c *h2c)
Willy Tarreauf2101912018-07-19 10:11:38 +0200348{
Willy Tarreaua8754662018-12-23 20:43:58 +0100349 return h2c->nb_cs > h2_settings_max_concurrent_streams;
Willy Tarreauf2101912018-07-19 10:11:38 +0200350}
351
Willy Tarreau44e973f2018-03-01 17:49:30 +0100352/* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c
353 * flags are used to figure what buffer was requested. It returns 1 if the
354 * allocation succeeds, in which case the connection is woken up, or 0 if it's
355 * impossible to wake up and we prefer to be woken up later.
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200356 */
Willy Tarreau44e973f2018-03-01 17:49:30 +0100357static int h2_buf_available(void *target)
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200358{
359 struct h2c *h2c = target;
Willy Tarreau0b559072018-02-26 15:22:17 +0100360 struct h2s *h2s;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200361
Willy Tarreau44e973f2018-03-01 17:49:30 +0100362 if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) {
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200363 h2c->flags &= ~H2_CF_DEM_DALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200364 h2c_restart_reading(h2c, 1);
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200365 return 1;
366 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200367
Willy Tarreau44e973f2018-03-01 17:49:30 +0100368 if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) {
369 h2c->flags &= ~H2_CF_MUX_MALLOC;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200370
371 if (h2c->flags & H2_CF_DEM_MROOM) {
372 h2c->flags &= ~H2_CF_DEM_MROOM;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200373 h2c_restart_reading(h2c, 1);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +0200374 }
Willy Tarreau14398122017-09-22 14:26:04 +0200375 return 1;
376 }
Willy Tarreau0b559072018-02-26 15:22:17 +0100377
378 if ((h2c->flags & H2_CF_DEM_SALLOC) &&
379 (h2s = h2c_st_by_id(h2c, h2c->dsi)) && h2s->cs &&
Olivier Houchard638b7992018-08-16 15:41:52 +0200380 b_alloc_margin(&h2s->rxbuf, 0)) {
Willy Tarreau0b559072018-02-26 15:22:17 +0100381 h2c->flags &= ~H2_CF_DEM_SALLOC;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200382 h2c_restart_reading(h2c, 1);
Willy Tarreau0b559072018-02-26 15:22:17 +0100383 return 1;
384 }
385
Willy Tarreau14398122017-09-22 14:26:04 +0200386 return 0;
387}
388
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200389static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200390{
391 struct buffer *buf = NULL;
392
Willy Tarreau44e973f2018-03-01 17:49:30 +0100393 if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) &&
394 unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) {
395 h2c->buf_wait.target = h2c;
396 h2c->buf_wait.wakeup_cb = h2_buf_available;
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100397 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100398 LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100399 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200400 __conn_xprt_stop_recv(h2c->conn);
401 }
402 return buf;
403}
404
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200405static inline void h2_release_buf(struct h2c *h2c, struct buffer *bptr)
Willy Tarreau14398122017-09-22 14:26:04 +0200406{
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200407 if (bptr->size) {
Willy Tarreau44e973f2018-03-01 17:49:30 +0100408 b_free(bptr);
Olivier Houchard673867c2018-05-25 16:58:52 +0200409 offer_buffers(h2c->buf_wait.target, tasks_run_queue);
Willy Tarreau14398122017-09-22 14:26:04 +0200410 }
411}
412
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100413/* returns the number of allocatable outgoing streams for the connection taking
414 * the last_sid and the reserved ones into account.
415 */
416static inline int h2_streams_left(const struct h2c *h2c)
417{
418 int ret;
419
420 /* consider the number of outgoing streams we're allowed to create before
421 * reaching the last GOAWAY frame seen. max_id is the last assigned id,
422 * nb_reserved is the number of streams which don't yet have an ID.
423 */
424 ret = (h2c->last_sid >= 0) ? h2c->last_sid : 0x7FFFFFFF;
425 ret = (unsigned int)(ret - h2c->max_id) / 2 - h2c->nb_reserved - 1;
426 if (ret < 0)
427 ret = 0;
428 return ret;
429}
430
Willy Tarreau00f18a32019-01-26 12:19:01 +0100431/* returns the number of streams in use on a connection to figure if it's
432 * idle or not. We check nb_cs and not nb_streams as the caller will want
433 * to know if it was the last one after a detach().
434 */
435static int h2_used_streams(struct connection *conn)
436{
437 struct h2c *h2c = conn->ctx;
438
439 return h2c->nb_cs;
440}
441
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100442/* returns the number of concurrent streams available on the connection */
Olivier Houchardd540b362018-11-05 18:37:53 +0100443static int h2_avail_streams(struct connection *conn)
444{
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100445 struct server *srv = objt_server(conn->target);
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100446 struct h2c *h2c = conn->ctx;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100447 int ret1, ret2;
Olivier Houchardd540b362018-11-05 18:37:53 +0100448
Willy Tarreau6afec462019-01-28 06:40:19 +0100449 /* RFC7540#6.8: Receivers of a GOAWAY frame MUST NOT open additional
450 * streams on the connection.
451 */
452 if (h2c->last_sid >= 0)
453 return 0;
454
Willy Tarreau86949782019-01-31 10:42:05 +0100455 /* note: may be negative if a SETTINGS frame changes the limit */
456 ret1 = h2c->streams_limit - h2c->nb_streams;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100457
458 /* we must also consider the limit imposed by stream IDs */
459 ret2 = h2_streams_left(h2c);
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100460 ret1 = MIN(ret1, ret2);
Willy Tarreau86949782019-01-31 10:42:05 +0100461 if (ret1 > 0 && srv && srv->max_reuse >= 0) {
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100462 ret2 = h2c->stream_cnt <= srv->max_reuse ? srv->max_reuse - h2c->stream_cnt + 1: 0;
463 ret1 = MIN(ret1, ret2);
464 }
465 return ret1;
Olivier Houchardd540b362018-11-05 18:37:53 +0100466}
467
Willy Tarreau35dbd5d2017-09-22 09:13:49 +0200468
Willy Tarreau62f52692017-10-08 23:01:42 +0200469/*****************************************************************/
470/* functions below are dedicated to the mux setup and management */
471/*****************************************************************/
472
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200473/* Initialize the mux once it's attached. For outgoing connections, the context
474 * is already initialized before installing the mux, so we detect incoming
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200475 * connections from the fact that the context is still NULL (even during mux
476 * upgrades). <input> is always used as Input buffer and may contain data. It is
477 * the caller responsibility to not reuse it anymore. Returns < 0 on error.
Willy Tarreau7dc24e42018-10-03 13:52:41 +0200478 */
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200479static int h2_init(struct connection *conn, struct proxy *prx, struct session *sess,
480 struct buffer *input)
Willy Tarreau32218eb2017-09-22 08:07:25 +0200481{
482 struct h2c *h2c;
Willy Tarreauea392822017-10-31 10:02:25 +0100483 struct task *t = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200484
Willy Tarreaubafbe012017-11-24 17:34:44 +0100485 h2c = pool_alloc(pool_head_h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200486 if (!h2c)
mildiscd2d7de2018-10-02 16:44:18 +0200487 goto fail_no_h2c;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200488
Christopher Faulete9b70722019-04-08 10:46:02 +0200489 if (conn_is_back(conn)) {
Willy Tarreau01b44822018-10-03 14:26:37 +0200490 h2c->flags = H2_CF_IS_BACK;
491 h2c->shut_timeout = h2c->timeout = prx->timeout.server;
492 if (tick_isset(prx->timeout.serverfin))
493 h2c->shut_timeout = prx->timeout.serverfin;
494 } else {
495 h2c->flags = H2_CF_NONE;
496 h2c->shut_timeout = h2c->timeout = prx->timeout.client;
497 if (tick_isset(prx->timeout.clientfin))
498 h2c->shut_timeout = prx->timeout.clientfin;
499 }
Willy Tarreau3f133572017-10-31 19:21:06 +0100500
Willy Tarreau0b37d652018-10-03 10:33:02 +0200501 h2c->proxy = prx;
Willy Tarreau33400292017-11-05 11:23:40 +0100502 h2c->task = NULL;
Willy Tarreau3f133572017-10-31 19:21:06 +0100503 if (tick_isset(h2c->timeout)) {
504 t = task_new(tid_bit);
505 if (!t)
506 goto fail;
507
508 h2c->task = t;
509 t->process = h2_timeout_task;
510 t->context = h2c;
511 t->expire = tick_add(now_ms, h2c->timeout);
512 }
Willy Tarreauea392822017-10-31 10:02:25 +0100513
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200514 h2c->wait_event.task = tasklet_new();
515 if (!h2c->wait_event.task)
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200516 goto fail;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200517 h2c->wait_event.task->process = h2_io_cb;
518 h2c->wait_event.task->context = h2c;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100519 h2c->wait_event.events = 0;
Olivier Houchard910b2bc2018-07-17 18:49:38 +0200520
Willy Tarreau32218eb2017-09-22 08:07:25 +0200521 h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size);
522 if (!h2c->ddht)
523 goto fail;
524
525 /* Initialise the context. */
526 h2c->st0 = H2_CS_PREFACE;
527 h2c->conn = conn;
Willy Tarreau2e2083a2019-01-31 10:34:07 +0100528 h2c->streams_limit = h2_settings_max_concurrent_streams;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200529 h2c->max_id = -1;
530 h2c->errcode = H2_ERR_NO_ERROR;
Willy Tarreau97aaa672018-12-23 09:49:04 +0100531 h2c->rcvd_c = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200532 h2c->rcvd_s = 0;
Willy Tarreau49745612017-12-03 18:56:02 +0100533 h2c->nb_streams = 0;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200534 h2c->nb_cs = 0;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100535 h2c->nb_reserved = 0;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100536 h2c->stream_cnt = 0;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200537
Christopher Faulet51f73eb2019-04-08 11:22:47 +0200538 h2c->dbuf = *input;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200539 h2c->dsi = -1;
540 h2c->msi = -1;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100541
Willy Tarreau32218eb2017-09-22 08:07:25 +0200542 h2c->last_sid = -1;
543
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200544 h2c->mbuf = BUF_NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200545 h2c->miw = 65535; /* mux initial window size */
546 h2c->mws = 65535; /* mux window size */
547 h2c->mfs = 16384; /* initial max frame size */
Willy Tarreau751f2d02018-10-05 09:35:00 +0200548 h2c->streams_by_id = EB_ROOT;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200549 LIST_INIT(&h2c->send_list);
550 LIST_INIT(&h2c->fctl_list);
Olivier Houchardd846c262018-10-19 17:24:29 +0200551 LIST_INIT(&h2c->sending_list);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100552 LIST_INIT(&h2c->buf_wait.list);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200553
Willy Tarreau3f133572017-10-31 19:21:06 +0100554 if (t)
555 task_queue(t);
Willy Tarreauea392822017-10-31 10:02:25 +0100556
Willy Tarreau01b44822018-10-03 14:26:37 +0200557 if (h2c->flags & H2_CF_IS_BACK) {
558 /* FIXME: this is temporary, for outgoing connections we need
559 * to immediately allocate a stream until the code is modified
560 * so that the caller calls ->attach(). For now the outgoing cs
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100561 * is stored as conn->ctx by the caller.
Willy Tarreau01b44822018-10-03 14:26:37 +0200562 */
563 struct h2s *h2s;
564
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100565 h2s = h2c_bck_stream_new(h2c, conn->ctx, sess);
Willy Tarreau01b44822018-10-03 14:26:37 +0200566 if (!h2s)
567 goto fail_stream;
568 }
569
Willy Tarreau3d2ee552018-12-19 14:12:10 +0100570 conn->ctx = h2c;
Willy Tarreau01b44822018-10-03 14:26:37 +0200571
Willy Tarreau0f383582018-10-03 14:22:21 +0200572 /* prepare to read something */
Olivier Houchard3ca18bf2019-04-05 15:34:34 +0200573 h2c_restart_reading(h2c, 1);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200574 return 0;
Willy Tarreau01b44822018-10-03 14:26:37 +0200575 fail_stream:
576 hpack_dht_free(h2c->ddht);
mildiscd2d7de2018-10-02 16:44:18 +0200577 fail:
Willy Tarreauf6562792019-05-07 19:05:35 +0200578 task_destroy(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200579 if (h2c->wait_event.task)
580 tasklet_free(h2c->wait_event.task);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100581 pool_free(pool_head_h2c, h2c);
mildiscd2d7de2018-10-02 16:44:18 +0200582 fail_no_h2c:
Willy Tarreau32218eb2017-09-22 08:07:25 +0200583 return -1;
584}
585
Willy Tarreau751f2d02018-10-05 09:35:00 +0200586/* returns the next allocatable outgoing stream ID for the H2 connection, or
587 * -1 if no more is allocatable.
588 */
589static inline int32_t h2c_get_next_sid(const struct h2c *h2c)
590{
591 int32_t id = (h2c->max_id + 1) | 1;
Willy Tarreaua80dca82019-01-24 17:08:28 +0100592
593 if ((id & 0x80000000U) || (h2c->last_sid >= 0 && id > h2c->last_sid))
Willy Tarreau751f2d02018-10-05 09:35:00 +0200594 id = -1;
595 return id;
596}
597
Willy Tarreau2373acc2017-10-12 17:35:14 +0200598/* returns the stream associated with id <id> or NULL if not found */
599static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id)
600{
601 struct eb32_node *node;
602
Willy Tarreau751f2d02018-10-05 09:35:00 +0200603 if (id == 0)
604 return (struct h2s *)h2_closed_stream;
605
Willy Tarreau2a856182017-05-16 15:20:39 +0200606 if (id > h2c->max_id)
607 return (struct h2s *)h2_idle_stream;
608
Willy Tarreau2373acc2017-10-12 17:35:14 +0200609 node = eb32_lookup(&h2c->streams_by_id, id);
610 if (!node)
Willy Tarreau2a856182017-05-16 15:20:39 +0200611 return (struct h2s *)h2_closed_stream;
Willy Tarreau2373acc2017-10-12 17:35:14 +0200612
613 return container_of(node, struct h2s, by_id);
614}
615
Christopher Faulet73c12072019-04-08 11:23:22 +0200616/* release function. This one should be called to free all resources allocated
617 * to the mux.
Willy Tarreau62f52692017-10-08 23:01:42 +0200618 */
Christopher Faulet73c12072019-04-08 11:23:22 +0200619static void h2_release(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +0200620{
Christopher Faulet61840e72019-04-15 09:33:32 +0200621 struct connection *conn = NULL;;
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200622
Willy Tarreau32218eb2017-09-22 08:07:25 +0200623 if (h2c) {
Christopher Faulet61840e72019-04-15 09:33:32 +0200624 /* The connection must be aattached to this mux to be released */
625 if (h2c->conn && h2c->conn->ctx == h2c)
626 conn = h2c->conn;
627
Willy Tarreau32218eb2017-09-22 08:07:25 +0200628 hpack_dht_free(h2c->ddht);
Willy Tarreau14398122017-09-22 14:26:04 +0200629
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100630 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau44e973f2018-03-01 17:49:30 +0100631 LIST_DEL(&h2c->buf_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100632 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau14398122017-09-22 14:26:04 +0200633
Willy Tarreau44e973f2018-03-01 17:49:30 +0100634 h2_release_buf(h2c, &h2c->dbuf);
635 h2_release_buf(h2c, &h2c->mbuf);
636
Willy Tarreauea392822017-10-31 10:02:25 +0100637 if (h2c->task) {
Willy Tarreau0975f112018-03-29 15:22:59 +0200638 h2c->task->context = NULL;
639 task_wakeup(h2c->task, TASK_WOKEN_OTHER);
Willy Tarreauea392822017-10-31 10:02:25 +0100640 h2c->task = NULL;
641 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200642 if (h2c->wait_event.task)
643 tasklet_free(h2c->wait_event.task);
Olivier Houchard0e079372019-04-15 17:51:16 +0200644 if (h2c->wait_event.events != 0)
Olivier Houcharde179d0e2019-03-21 18:27:17 +0100645 conn->xprt->unsubscribe(conn, conn->xprt_ctx, h2c->wait_event.events,
Olivier Houchard0e079372019-04-15 17:51:16 +0200646 &h2c->wait_event);
Willy Tarreauea392822017-10-31 10:02:25 +0100647
Willy Tarreaubafbe012017-11-24 17:34:44 +0100648 pool_free(pool_head_h2c, h2c);
Willy Tarreau32218eb2017-09-22 08:07:25 +0200649 }
650
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200651 if (conn) {
652 conn->mux = NULL;
653 conn->ctx = NULL;
Willy Tarreau32218eb2017-09-22 08:07:25 +0200654
Christopher Faulet39a96ee2019-04-08 10:52:21 +0200655 conn_stop_tracking(conn);
656 conn_full_close(conn);
657 if (conn->destroy_cb)
658 conn->destroy_cb(conn);
659 conn_free(conn);
660 }
Willy Tarreau62f52692017-10-08 23:01:42 +0200661}
662
663
Willy Tarreau71681172017-10-23 14:39:06 +0200664/******************************************************/
665/* functions below are for the H2 protocol processing */
666/******************************************************/
667
668/* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */
Willy Tarreau1f094672017-11-20 21:27:45 +0100669static inline __maybe_unused int h2s_id(const struct h2s *h2s)
Willy Tarreau71681172017-10-23 14:39:06 +0200670{
671 return h2s ? h2s->id : 0;
672}
673
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200674/* returns true of the mux is currently busy as seen from stream <h2s> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100675static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s)
Willy Tarreau5b5e6872017-09-25 16:17:25 +0200676{
677 if (h2c->msi < 0)
678 return 0;
679
680 if (h2c->msi == h2s_id(h2s))
681 return 0;
682
683 return 1;
684}
685
Willy Tarreau741d6df2017-10-17 08:00:59 +0200686/* marks an error on the connection */
Willy Tarreau1f094672017-11-20 21:27:45 +0100687static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err)
Willy Tarreau741d6df2017-10-17 08:00:59 +0200688{
689 h2c->errcode = err;
690 h2c->st0 = H2_CS_ERROR;
691}
692
Willy Tarreau175cebb2019-01-24 10:02:24 +0100693/* marks an error on the stream. It may also update an already closed stream
694 * (e.g. to report an error after an RST was received).
695 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100696static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err)
Willy Tarreau2e43f082017-10-17 08:03:59 +0200697{
Willy Tarreau175cebb2019-01-24 10:02:24 +0100698 if (h2s->id && h2s->st != H2_SS_ERROR) {
Willy Tarreau2e43f082017-10-17 08:03:59 +0200699 h2s->errcode = err;
Willy Tarreau175cebb2019-01-24 10:02:24 +0100700 if (h2s->st < H2_SS_ERROR)
701 h2s->st = H2_SS_ERROR;
Willy Tarreauec988c72018-12-19 18:00:29 +0100702 if (h2s->cs)
703 cs_set_error(h2s->cs);
Willy Tarreau2e43f082017-10-17 08:03:59 +0200704 }
705}
706
Willy Tarreau7e094452018-12-19 18:08:52 +0100707/* attempt to notify the data layer of recv availability */
708static void __maybe_unused h2s_notify_recv(struct h2s *h2s)
709{
710 struct wait_event *sw;
711
712 if (h2s->recv_wait) {
713 sw = h2s->recv_wait;
714 sw->events &= ~SUB_RETRY_RECV;
715 tasklet_wakeup(sw->task);
716 h2s->recv_wait = NULL;
717 }
718}
719
720/* attempt to notify the data layer of send availability */
721static void __maybe_unused h2s_notify_send(struct h2s *h2s)
722{
723 struct wait_event *sw;
724
Olivier Houchard998410a2019-04-15 19:23:37 +0200725 if (h2s->send_wait && LIST_ISEMPTY(&h2s->sending_list)) {
Willy Tarreau7e094452018-12-19 18:08:52 +0100726 sw = h2s->send_wait;
727 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchardfd1e96d2019-03-25 14:04:25 +0100728 LIST_ADDQ(&h2s->h2c->sending_list, &h2s->sending_list);
Willy Tarreau7e094452018-12-19 18:08:52 +0100729 tasklet_wakeup(sw->task);
Willy Tarreau7e094452018-12-19 18:08:52 +0100730 }
731}
732
Willy Tarreau8b2757c2018-12-19 17:36:48 +0100733/* alerts the data layer, trying to wake it up by all means, following
734 * this sequence :
735 * - if the h2s' data layer is subscribed to recv, then it's woken up for recv
736 * - if its subscribed to send, then it's woken up for send
737 * - if it was subscribed to neither, its ->wake() callback is called
738 * It is safe to call this function with a closed stream which doesn't have a
739 * conn_stream anymore.
740 */
741static void __maybe_unused h2s_alert(struct h2s *h2s)
742{
743 if (h2s->recv_wait || h2s->send_wait) {
744 h2s_notify_recv(h2s);
745 h2s_notify_send(h2s);
746 }
747 else if (h2s->cs && h2s->cs->data_cb->wake != NULL)
748 h2s->cs->data_cb->wake(h2s->cs);
749}
750
Willy Tarreaue4820742017-07-27 13:37:23 +0200751/* writes the 24-bit frame size <len> at address <frame> */
Willy Tarreau1f094672017-11-20 21:27:45 +0100752static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len)
Willy Tarreaue4820742017-07-27 13:37:23 +0200753{
754 uint8_t *out = frame;
755
756 *out = len >> 16;
757 write_n16(out + 1, len);
758}
759
Willy Tarreau54c15062017-10-10 17:10:03 +0200760/* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the
761 * current pointer, dealing with wrapping, and stores the result in <dst>. It's
762 * the caller's responsibility to verify that there are at least <bytes> bytes
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200763 * available in the buffer's input prior to calling this function. The buffer
764 * is assumed not to hold any output data.
Willy Tarreau54c15062017-10-10 17:10:03 +0200765 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100766static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes,
Willy Tarreau54c15062017-10-10 17:10:03 +0200767 const struct buffer *b, int o)
768{
Willy Tarreau591d4452018-06-15 17:21:00 +0200769 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 +0200770}
771
Willy Tarreau1f094672017-11-20 21:27:45 +0100772static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200773{
Willy Tarreau591d4452018-06-15 17:21:00 +0200774 return readv_n16(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200775}
776
Willy Tarreau1f094672017-11-20 21:27:45 +0100777static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200778{
Willy Tarreau591d4452018-06-15 17:21:00 +0200779 return readv_n32(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200780}
781
Willy Tarreau1f094672017-11-20 21:27:45 +0100782static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o)
Willy Tarreau54c15062017-10-10 17:10:03 +0200783{
Willy Tarreau591d4452018-06-15 17:21:00 +0200784 return readv_n64(b_peek(b, o), b_wrap(b) - b_peek(b, o), b_orig(b));
Willy Tarreau54c15062017-10-10 17:10:03 +0200785}
786
787
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100788/* Peeks an H2 frame header from offset <o> of buffer <b> into descriptor <h>.
789 * The algorithm is not obvious. It turns out that H2 headers are neither
790 * aligned nor do they use regular sizes. And to add to the trouble, the buffer
791 * may wrap so each byte read must be checked. The header is formed like this :
Willy Tarreau715d5312017-07-11 15:20:24 +0200792 *
793 * b0 b1 b2 b3 b4 b5..b8
794 * +----------+---------+--------+----+----+----------------------+
795 * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)|
796 * +----------+---------+--------+----+----+----------------------+
797 *
798 * Here we read a big-endian 64 bit word from h[1]. This way in a single read
799 * we get the sid properly aligned and ordered, and 16 bits of len properly
800 * ordered as well. The type and flags can be extracted using bit shifts from
801 * the word, and only one extra read is needed to fetch len[16:23].
Willy Tarreau9c7f2d12018-06-15 11:51:32 +0200802 * Returns zero if some bytes are missing, otherwise non-zero on success. The
803 * buffer is assumed not to contain any output data.
Willy Tarreau715d5312017-07-11 15:20:24 +0200804 */
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100805static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, int o, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200806{
807 uint64_t w;
808
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100809 if (b_data(b) < o + 9)
Willy Tarreau715d5312017-07-11 15:20:24 +0200810 return 0;
811
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100812 w = h2_get_n64(b, o + 1);
813 h->len = *(uint8_t*)b_peek(b, o) << 16;
Willy Tarreau715d5312017-07-11 15:20:24 +0200814 h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */
815 h->ff = w >> 32;
816 h->ft = w >> 40;
817 h->len += w >> 48;
818 return 1;
819}
820
821/* skip the next 9 bytes corresponding to the frame header possibly parsed by
822 * h2_peek_frame_hdr() above.
823 */
Willy Tarreau1f094672017-11-20 21:27:45 +0100824static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b)
Willy Tarreau715d5312017-07-11 15:20:24 +0200825{
Willy Tarreaue5f12ce2018-06-15 10:28:05 +0200826 b_del(b, 9);
Willy Tarreau715d5312017-07-11 15:20:24 +0200827}
828
829/* same as above, automatically advances the buffer on success */
Willy Tarreau1f094672017-11-20 21:27:45 +0100830static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h)
Willy Tarreau715d5312017-07-11 15:20:24 +0200831{
832 int ret;
833
Willy Tarreaua4428bd2018-12-22 18:11:41 +0100834 ret = h2_peek_frame_hdr(b, 0, h);
Willy Tarreau715d5312017-07-11 15:20:24 +0200835 if (ret > 0)
836 h2_skip_frame_hdr(b);
837 return ret;
838}
839
Willy Tarreau00dd0782018-03-01 16:31:34 +0100840/* marks stream <h2s> as CLOSED and decrement the number of active streams for
841 * its connection if the stream was not yet closed. Please use this exclusively
842 * before closing a stream to ensure stream count is well maintained.
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100843 */
Willy Tarreau00dd0782018-03-01 16:31:34 +0100844static inline void h2s_close(struct h2s *h2s)
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100845{
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100846 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100847 h2s->h2c->nb_streams--;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100848 if (!h2s->id)
849 h2s->h2c->nb_reserved--;
Willy Tarreaua27db382019-03-25 18:13:16 +0100850 if (h2s->cs) {
Christopher Faulet63768a62019-03-22 14:05:52 +0100851 h2s->cs->flags |= CS_FL_REOS;
Willy Tarreaua27db382019-03-25 18:13:16 +0100852 if (!(h2s->cs->flags & CS_FL_EOS) && !b_data(&h2s->rxbuf))
853 h2s_notify_recv(h2s);
854 }
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100855 }
Willy Tarreau91bfdd72017-12-14 12:00:14 +0100856 h2s->st = H2_SS_CLOSED;
857}
858
Willy Tarreau71049cc2018-03-28 13:56:39 +0200859/* detaches an H2 stream from its H2C and releases it to the H2S pool. */
860static void h2s_destroy(struct h2s *h2s)
Willy Tarreau0a10de62018-03-01 16:27:53 +0100861{
862 h2s_close(h2s);
863 eb32_delete(&h2s->by_id);
Olivier Houchard638b7992018-08-16 15:41:52 +0200864 if (b_size(&h2s->rxbuf)) {
865 b_free(&h2s->rxbuf);
866 offer_buffers(NULL, tasks_run_queue);
867 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200868 if (h2s->send_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100869 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200870 if (h2s->recv_wait != NULL)
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100871 h2s->recv_wait->events &= ~SUB_RETRY_RECV;
Joseph Herlantd77575d2018-11-25 10:54:45 -0800872 /* There's no need to explicitly call unsubscribe here, the only
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200873 * reference left would be in the h2c send_list/fctl_list, and if
874 * we're in it, we're getting out anyway
875 */
Olivier Houchardd360ac62019-03-22 17:37:16 +0100876 LIST_DEL_INIT(&h2s->list);
Olivier Houchard998410a2019-04-15 19:23:37 +0200877 if (!LIST_ISEMPTY(&h2s->sending_list)) {
878 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
879 LIST_DEL_INIT(&h2s->sending_list);
880 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200881 tasklet_free(h2s->wait_event.task);
Willy Tarreau0a10de62018-03-01 16:27:53 +0100882 pool_free(pool_head_h2s, h2s);
883}
884
Willy Tarreaua8e49542018-10-03 18:53:55 +0200885/* allocates a new stream <id> for connection <h2c> and adds it into h2c's
886 * stream tree. In case of error, nothing is added and NULL is returned. The
887 * causes of errors can be any failed memory allocation. The caller is
888 * responsible for checking if the connection may support an extra stream
889 * prior to calling this function.
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200890 */
Willy Tarreaua8e49542018-10-03 18:53:55 +0200891static struct h2s *h2s_new(struct h2c *h2c, int id)
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200892{
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200893 struct h2s *h2s;
894
Willy Tarreaubafbe012017-11-24 17:34:44 +0100895 h2s = pool_alloc(pool_head_h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200896 if (!h2s)
897 goto out;
898
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200899 h2s->wait_event.task = tasklet_new();
900 if (!h2s->wait_event.task) {
901 pool_free(pool_head_h2s, h2s);
902 goto out;
903 }
904 h2s->send_wait = NULL;
905 h2s->recv_wait = NULL;
906 h2s->wait_event.task->process = h2_deferred_shut;
907 h2s->wait_event.task->context = h2s;
908 h2s->wait_event.handle = NULL;
Willy Tarreau4f6516d2018-12-19 13:59:17 +0100909 h2s->wait_event.events = 0;
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200910 LIST_INIT(&h2s->list);
Olivier Houchardd360ac62019-03-22 17:37:16 +0100911 LIST_INIT(&h2s->sending_list);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200912 h2s->h2c = h2c;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200913 h2s->cs = NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200914 h2s->mws = h2c->miw;
915 h2s->flags = H2_SF_NONE;
916 h2s->errcode = H2_ERR_NO_ERROR;
917 h2s->st = H2_SS_IDLE;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +0200918 h2s->status = 0;
Willy Tarreau1915ca22019-01-24 11:49:37 +0100919 h2s->body_len = 0;
Olivier Houchard638b7992018-08-16 15:41:52 +0200920 h2s->rxbuf = BUF_NULL;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200921
922 if (h2c->flags & H2_CF_IS_BACK) {
923 h1m_init_req(&h2s->h1m);
924 h2s->h1m.err_pos = -1; // don't care about errors on the request path
925 h2s->h1m.flags |= H1_MF_TOLOWER;
926 } else {
927 h1m_init_res(&h2s->h1m);
928 h2s->h1m.err_pos = -1; // don't care about errors on the response path
929 h2s->h1m.flags |= H1_MF_TOLOWER;
930 }
931
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200932 h2s->by_id.key = h2s->id = id;
Willy Tarreau751f2d02018-10-05 09:35:00 +0200933 if (id > 0)
934 h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +0100935 else
936 h2c->nb_reserved++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200937
938 eb32_insert(&h2c->streams_by_id, &h2s->by_id);
Willy Tarreau49745612017-12-03 18:56:02 +0100939 h2c->nb_streams++;
Willy Tarreaue9634bd2019-01-23 10:25:10 +0100940 h2c->stream_cnt++;
Willy Tarreaua8e49542018-10-03 18:53:55 +0200941
942 return h2s;
943
944 out_free_h2s:
945 pool_free(pool_head_h2s, h2s);
946 out:
947 return NULL;
948}
949
950/* creates a new stream <id> on the h2c connection and returns it, or NULL in
951 * case of memory allocation error.
952 */
953static struct h2s *h2c_frt_stream_new(struct h2c *h2c, int id)
954{
955 struct session *sess = h2c->conn->owner;
956 struct conn_stream *cs;
957 struct h2s *h2s;
958
959 if (h2c->nb_streams >= h2_settings_max_concurrent_streams)
960 goto out;
961
962 h2s = h2s_new(h2c, id);
963 if (!h2s)
964 goto out;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200965
966 cs = cs_new(h2c->conn);
967 if (!cs)
968 goto out_close;
969
Olivier Houchard746fb772018-12-15 19:42:00 +0100970 cs->flags |= CS_FL_NOT_FIRST;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200971 h2s->cs = cs;
972 cs->ctx = h2s;
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200973 h2c->nb_cs++;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200974
975 if (stream_create_from_cs(cs) < 0)
976 goto out_free_cs;
977
Willy Tarreau590a0512018-09-05 11:56:48 +0200978 /* We want the accept date presented to the next stream to be the one
979 * we have now, the handshake time to be null (since the next stream
980 * is not delayed by a handshake), and the idle time to count since
981 * right now.
982 */
983 sess->accept_date = date;
984 sess->tv_accept = now;
985 sess->t_handshake = 0;
986
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200987 /* OK done, the stream lives its own life now */
Willy Tarreaufa1d3572019-01-31 10:31:51 +0100988 if (h2_frt_has_too_many_cs(h2c))
Willy Tarreauf2101912018-07-19 10:11:38 +0200989 h2c->flags |= H2_CF_DEM_TOOMANY;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200990 return h2s;
991
992 out_free_cs:
Willy Tarreau7ac60e82018-07-19 09:04:05 +0200993 h2c->nb_cs--;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200994 cs_free(cs);
995 out_close:
Willy Tarreau71049cc2018-03-28 13:56:39 +0200996 h2s_destroy(h2s);
Willy Tarreau3ccf4b22017-10-13 19:07:26 +0200997 out:
Willy Tarreau45efc072018-10-03 18:27:52 +0200998 sess_log(sess);
999 return NULL;
Willy Tarreau3ccf4b22017-10-13 19:07:26 +02001000}
1001
Willy Tarreau751f2d02018-10-05 09:35:00 +02001002/* allocates a new stream associated to conn_stream <cs> on the h2c connection
1003 * and returns it, or NULL in case of memory allocation error or if the highest
1004 * possible stream ID was reached.
1005 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01001006static struct h2s *h2c_bck_stream_new(struct h2c *h2c, struct conn_stream *cs, struct session *sess)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001007{
1008 struct h2s *h2s = NULL;
1009
Willy Tarreau86949782019-01-31 10:42:05 +01001010 if (h2c->nb_streams >= h2c->streams_limit)
Willy Tarreau751f2d02018-10-05 09:35:00 +02001011 goto out;
1012
Willy Tarreaua80dca82019-01-24 17:08:28 +01001013 if (h2_streams_left(h2c) < 1)
1014 goto out;
1015
Willy Tarreau751f2d02018-10-05 09:35:00 +02001016 /* Defer choosing the ID until we send the first message to create the stream */
1017 h2s = h2s_new(h2c, 0);
1018 if (!h2s)
1019 goto out;
1020
1021 h2s->cs = cs;
Olivier Houchardf502aca2018-12-14 19:42:40 +01001022 h2s->sess = sess;
Willy Tarreau751f2d02018-10-05 09:35:00 +02001023 cs->ctx = h2s;
1024 h2c->nb_cs++;
1025
Willy Tarreau751f2d02018-10-05 09:35:00 +02001026 out:
1027 return h2s;
1028}
1029
Willy Tarreaube5b7152017-09-25 16:25:39 +02001030/* try to send a settings frame on the connection. Returns > 0 on success, 0 if
1031 * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for
1032 * the various settings codes.
1033 */
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001034static int h2c_send_settings(struct h2c *h2c)
Willy Tarreaube5b7152017-09-25 16:25:39 +02001035{
1036 struct buffer *res;
1037 char buf_data[100]; // enough for 15 settings
Willy Tarreau83061a82018-07-13 11:56:34 +02001038 struct buffer buf;
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001039 int mfs;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001040 int ret;
1041
1042 if (h2c_mux_busy(h2c, NULL)) {
1043 h2c->flags |= H2_CF_DEM_MBUSY;
1044 return 0;
1045 }
1046
Willy Tarreau44e973f2018-03-01 17:49:30 +01001047 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001048 if (!res) {
1049 h2c->flags |= H2_CF_MUX_MALLOC;
1050 h2c->flags |= H2_CF_DEM_MROOM;
1051 return 0;
1052 }
1053
1054 chunk_init(&buf, buf_data, sizeof(buf_data));
1055 chunk_memcpy(&buf,
1056 "\x00\x00\x00" /* length : 0 for now */
1057 "\x04\x00" /* type : 4 (settings), flags : 0 */
1058 "\x00\x00\x00\x00", /* stream ID : 0 */
1059 9);
1060
Willy Tarreau0bbad6b2019-02-26 16:01:52 +01001061 if (h2c->flags & H2_CF_IS_BACK) {
1062 /* send settings_enable_push=0 */
1063 chunk_memcat(&buf, "\x00\x02\x00\x00\x00\x00", 6);
1064 }
1065
Willy Tarreaube5b7152017-09-25 16:25:39 +02001066 if (h2_settings_header_table_size != 4096) {
1067 char str[6] = "\x00\x01"; /* header_table_size */
1068
1069 write_n32(str + 2, h2_settings_header_table_size);
1070 chunk_memcat(&buf, str, 6);
1071 }
1072
1073 if (h2_settings_initial_window_size != 65535) {
1074 char str[6] = "\x00\x04"; /* initial_window_size */
1075
1076 write_n32(str + 2, h2_settings_initial_window_size);
1077 chunk_memcat(&buf, str, 6);
1078 }
1079
1080 if (h2_settings_max_concurrent_streams != 0) {
1081 char str[6] = "\x00\x03"; /* max_concurrent_streams */
1082
1083 /* Note: 0 means "unlimited" for haproxy's config but not for
1084 * the protocol, so never send this value!
1085 */
1086 write_n32(str + 2, h2_settings_max_concurrent_streams);
1087 chunk_memcat(&buf, str, 6);
1088 }
1089
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001090 mfs = h2_settings_max_frame_size;
1091 if (mfs > global.tune.bufsize)
1092 mfs = global.tune.bufsize;
1093
1094 if (!mfs)
1095 mfs = global.tune.bufsize;
1096
1097 if (mfs != 16384) {
Willy Tarreaube5b7152017-09-25 16:25:39 +02001098 char str[6] = "\x00\x05"; /* max_frame_size */
1099
1100 /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to
1101 * match bufsize - rewrite size, but at the moment it seems
1102 * that clients don't take care of it.
1103 */
Willy Tarreaua24b35c2019-02-21 13:24:36 +01001104 write_n32(str + 2, mfs);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001105 chunk_memcat(&buf, str, 6);
1106 }
1107
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001108 h2_set_frame_size(buf.area, buf.data - 9);
1109 ret = b_istput(res, ist2(buf.area, buf.data));
Willy Tarreaube5b7152017-09-25 16:25:39 +02001110 if (unlikely(ret <= 0)) {
1111 if (!ret) {
1112 h2c->flags |= H2_CF_MUX_MFULL;
1113 h2c->flags |= H2_CF_DEM_MROOM;
1114 return 0;
1115 }
1116 else {
1117 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1118 return 0;
1119 }
1120 }
1121 return ret;
1122}
1123
Willy Tarreau52eed752017-09-22 15:05:09 +02001124/* Try to receive a connection preface, then upon success try to send our
1125 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1126 * missing data. It may return an error in h2c.
1127 */
1128static int h2c_frt_recv_preface(struct h2c *h2c)
1129{
1130 int ret1;
Willy Tarreaube5b7152017-09-25 16:25:39 +02001131 int ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001132
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001133 ret1 = b_isteq(&h2c->dbuf, 0, b_data(&h2c->dbuf), ist(H2_CONN_PREFACE));
Willy Tarreau52eed752017-09-22 15:05:09 +02001134
1135 if (unlikely(ret1 <= 0)) {
Willy Tarreau22de8d32018-09-05 19:55:58 +02001136 if (ret1 < 0)
1137 sess_log(h2c->conn->owner);
1138
Willy Tarreau52eed752017-09-22 15:05:09 +02001139 if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn))
1140 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1141 return 0;
1142 }
1143
Willy Tarreau7f0cc492018-10-08 07:13:08 +02001144 ret2 = h2c_send_settings(h2c);
Willy Tarreaube5b7152017-09-25 16:25:39 +02001145 if (ret2 > 0)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001146 b_del(&h2c->dbuf, ret1);
Willy Tarreau52eed752017-09-22 15:05:09 +02001147
Willy Tarreaube5b7152017-09-25 16:25:39 +02001148 return ret2;
Willy Tarreau52eed752017-09-22 15:05:09 +02001149}
1150
Willy Tarreau01b44822018-10-03 14:26:37 +02001151/* Try to send a connection preface, then upon success try to send our
1152 * preface which is a SETTINGS frame. Returns > 0 on success or zero on
1153 * missing data. It may return an error in h2c.
1154 */
1155static int h2c_bck_send_preface(struct h2c *h2c)
1156{
1157 struct buffer *res;
1158
1159 if (h2c_mux_busy(h2c, NULL)) {
1160 h2c->flags |= H2_CF_DEM_MBUSY;
1161 return 0;
1162 }
1163
1164 res = h2_get_buf(h2c, &h2c->mbuf);
1165 if (!res) {
1166 h2c->flags |= H2_CF_MUX_MALLOC;
1167 h2c->flags |= H2_CF_DEM_MROOM;
1168 return 0;
1169 }
1170
1171 if (!b_data(res)) {
1172 /* preface not yet sent */
1173 b_istput(res, ist(H2_CONN_PREFACE));
1174 }
1175
1176 return h2c_send_settings(h2c);
1177}
1178
Willy Tarreau081d4722017-05-16 21:51:05 +02001179/* try to send a GOAWAY frame on the connection to report an error or a graceful
1180 * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero
1181 * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it
1182 * from h2c->max_id if it's not set yet (<0). In case of lack of room to write
1183 * the message, it subscribes the requester (either <h2s> or <h2c>) to future
1184 * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED
1185 * on unrecoverable failure. It will not attempt to send one again in this last
1186 * case so that it is safe to use h2c_error() to report such errors.
1187 */
1188static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s)
1189{
1190 struct buffer *res;
1191 char str[17];
1192 int ret;
1193
1194 if (h2c->flags & H2_CF_GOAWAY_FAILED)
1195 return 1; // claim that it worked
1196
1197 if (h2c_mux_busy(h2c, h2s)) {
1198 if (h2s)
1199 h2s->flags |= H2_SF_BLK_MBUSY;
1200 else
1201 h2c->flags |= H2_CF_DEM_MBUSY;
1202 return 0;
1203 }
1204
Willy Tarreau44e973f2018-03-01 17:49:30 +01001205 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau081d4722017-05-16 21:51:05 +02001206 if (!res) {
1207 h2c->flags |= H2_CF_MUX_MALLOC;
1208 if (h2s)
1209 h2s->flags |= H2_SF_BLK_MROOM;
1210 else
1211 h2c->flags |= H2_CF_DEM_MROOM;
1212 return 0;
1213 }
1214
1215 /* len: 8, type: 7, flags: none, sid: 0 */
1216 memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9);
1217
1218 if (h2c->last_sid < 0)
1219 h2c->last_sid = h2c->max_id;
1220
1221 write_n32(str + 9, h2c->last_sid);
1222 write_n32(str + 13, h2c->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001223 ret = b_istput(res, ist2(str, 17));
Willy Tarreau081d4722017-05-16 21:51:05 +02001224 if (unlikely(ret <= 0)) {
1225 if (!ret) {
1226 h2c->flags |= H2_CF_MUX_MFULL;
1227 if (h2s)
1228 h2s->flags |= H2_SF_BLK_MROOM;
1229 else
1230 h2c->flags |= H2_CF_DEM_MROOM;
1231 return 0;
1232 }
1233 else {
1234 /* we cannot report this error using GOAWAY, so we mark
1235 * it and claim a success.
1236 */
1237 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1238 h2c->flags |= H2_CF_GOAWAY_FAILED;
1239 return 1;
1240 }
1241 }
1242 h2c->flags |= H2_CF_GOAWAY_SENT;
1243 return ret;
1244}
1245
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001246/* Try to send an RST_STREAM frame on the connection for the indicated stream
1247 * during mux operations. This stream must be valid and cannot be closed
1248 * already. h2s->id will be used for the stream ID and h2s->errcode will be
1249 * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was
1250 * not yet.
1251 *
1252 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1253 * to write the message, it subscribes the stream to future notifications.
1254 */
1255static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1256{
1257 struct buffer *res;
1258 char str[13];
1259 int ret;
1260
1261 if (!h2s || h2s->st == H2_SS_CLOSED)
1262 return 1;
1263
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001264 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1265 * RST_STREAM in response to a RST_STREAM frame.
1266 */
1267 if (h2c->dft == H2_FT_RST_STREAM) {
1268 ret = 1;
1269 goto ignore;
1270 }
1271
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001272 if (h2c_mux_busy(h2c, h2s)) {
1273 h2s->flags |= H2_SF_BLK_MBUSY;
1274 return 0;
1275 }
1276
Willy Tarreau44e973f2018-03-01 17:49:30 +01001277 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001278 if (!res) {
1279 h2c->flags |= H2_CF_MUX_MALLOC;
1280 h2s->flags |= H2_SF_BLK_MROOM;
1281 return 0;
1282 }
1283
1284 /* len: 4, type: 3, flags: none */
1285 memcpy(str, "\x00\x00\x04\x03\x00", 5);
1286 write_n32(str + 5, h2s->id);
1287 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001288 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001289
1290 if (unlikely(ret <= 0)) {
1291 if (!ret) {
1292 h2c->flags |= H2_CF_MUX_MFULL;
1293 h2s->flags |= H2_SF_BLK_MROOM;
1294 return 0;
1295 }
1296 else {
1297 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1298 return 0;
1299 }
1300 }
1301
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001302 ignore:
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001303 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001304 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001305 return ret;
1306}
1307
1308/* Try to send an RST_STREAM frame on the connection for the stream being
1309 * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001310 * error code, even if the stream is one of the dummy ones, and will update
1311 * h2s->st to H2_SS_CLOSED if it was not yet.
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001312 *
1313 * Returns > 0 on success or zero if nothing was done. In case of lack of room
1314 * to write the message, it blocks the demuxer and subscribes it to future
Joseph Herlantd77575d2018-11-25 10:54:45 -08001315 * notifications. It's worth mentioning that an RST may even be sent for a
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001316 * closed stream.
Willy Tarreau27a84c92017-10-17 08:10:17 +02001317 */
1318static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s)
1319{
1320 struct buffer *res;
1321 char str[13];
1322 int ret;
1323
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001324 /* RFC7540#5.4.2: To avoid looping, an endpoint MUST NOT send a
1325 * RST_STREAM in response to a RST_STREAM frame.
1326 */
1327 if (h2c->dft == H2_FT_RST_STREAM) {
1328 ret = 1;
1329 goto ignore;
1330 }
1331
Willy Tarreau27a84c92017-10-17 08:10:17 +02001332 if (h2c_mux_busy(h2c, h2s)) {
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001333 h2c->flags |= H2_CF_DEM_MBUSY;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001334 return 0;
1335 }
1336
Willy Tarreau44e973f2018-03-01 17:49:30 +01001337 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau27a84c92017-10-17 08:10:17 +02001338 if (!res) {
1339 h2c->flags |= H2_CF_MUX_MALLOC;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001340 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001341 return 0;
1342 }
1343
1344 /* len: 4, type: 3, flags: none */
1345 memcpy(str, "\x00\x00\x04\x03\x00", 5);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001346
Willy Tarreau27a84c92017-10-17 08:10:17 +02001347 write_n32(str + 5, h2c->dsi);
Willy Tarreaue6888ff2018-12-23 18:26:26 +01001348 write_n32(str + 9, h2s->errcode);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001349 ret = b_istput(res, ist2(str, 13));
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001350
Willy Tarreau27a84c92017-10-17 08:10:17 +02001351 if (unlikely(ret <= 0)) {
1352 if (!ret) {
1353 h2c->flags |= H2_CF_MUX_MFULL;
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001354 h2c->flags |= H2_CF_DEM_MROOM;
Willy Tarreau27a84c92017-10-17 08:10:17 +02001355 return 0;
1356 }
1357 else {
1358 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1359 return 0;
1360 }
1361 }
1362
Willy Tarreau8adae7c2018-03-22 17:37:05 +01001363 ignore:
Willy Tarreauab0e1da2018-10-05 10:16:37 +02001364 if (h2s->id) {
Willy Tarreau27a84c92017-10-17 08:10:17 +02001365 h2s->flags |= H2_SF_RST_SENT;
Willy Tarreau00dd0782018-03-01 16:31:34 +01001366 h2s_close(h2s);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01001367 }
1368
Willy Tarreau27a84c92017-10-17 08:10:17 +02001369 return ret;
1370}
1371
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001372/* try to send an empty DATA frame with the ES flag set to notify about the
1373 * end of stream and match a shutdown(write). If an ES was already sent as
1374 * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0
1375 * on success or zero if nothing was done. In case of lack of room to write the
1376 * message, it subscribes the requesting stream to future notifications.
1377 */
1378static int h2_send_empty_data_es(struct h2s *h2s)
1379{
1380 struct h2c *h2c = h2s->h2c;
1381 struct buffer *res;
1382 char str[9];
1383 int ret;
1384
Willy Tarreau721c9742017-11-07 11:05:42 +01001385 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001386 return 1;
1387
1388 if (h2c_mux_busy(h2c, h2s)) {
1389 h2s->flags |= H2_SF_BLK_MBUSY;
1390 return 0;
1391 }
1392
Willy Tarreau44e973f2018-03-01 17:49:30 +01001393 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001394 if (!res) {
1395 h2c->flags |= H2_CF_MUX_MALLOC;
1396 h2s->flags |= H2_SF_BLK_MROOM;
1397 return 0;
1398 }
1399
1400 /* len: 0x000000, type: 0(DATA), flags: ES=1 */
1401 memcpy(str, "\x00\x00\x00\x00\x01", 5);
1402 write_n32(str + 5, h2s->id);
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001403 ret = b_istput(res, ist2(str, 9));
Willy Tarreau6d8b6822017-11-07 14:39:09 +01001404 if (likely(ret > 0)) {
1405 h2s->flags |= H2_SF_ES_SENT;
1406 }
1407 else if (!ret) {
1408 h2c->flags |= H2_CF_MUX_MFULL;
1409 h2s->flags |= H2_SF_BLK_MROOM;
1410 return 0;
1411 }
1412 else {
1413 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1414 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01001415 }
1416 return ret;
1417}
1418
Christopher Fauletf02ca002019-03-07 16:21:34 +01001419/* wake a specific stream and assign its conn_stream the CS_FL_* flags <flags>
1420 * in addition to CS_FL_ERROR in case of error and CS_FL_REOS in case of close
1421 * connection. The stream's state is automatically updated accordingly. If the
1422 * stream is orphaned, it is destroyed.
1423 */
1424static void h2s_wake_one_stream(struct h2s *h2s, uint32_t flags)
1425{
1426 if (!h2s->cs) {
1427 /* this stream was already orphaned */
1428 h2s_destroy(h2s);
1429 return;
1430 }
1431
1432 h2s->cs->flags |= flags;
1433 if ((flags & CS_FL_ERR_PENDING) && (h2s->cs->flags & CS_FL_EOS))
1434 h2s->cs->flags |= CS_FL_ERROR;
1435
1436 h2s_alert(h2s);
1437
1438 if (flags & CS_FL_ERR_PENDING && h2s->st < H2_SS_ERROR)
1439 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01001440 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_OPEN)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001441 h2s->st = H2_SS_HREM;
Christopher Faulet63768a62019-03-22 14:05:52 +01001442 else if ((flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC)
Christopher Fauletf02ca002019-03-07 16:21:34 +01001443 h2s_close(h2s);
1444}
1445
1446/* wake the streams attached to the connection, whose id is greater than <last>
1447 * or unassigned.
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001448 */
1449static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags)
1450{
1451 struct eb32_node *node;
1452 struct h2s *h2s;
1453
1454 if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR)
Willy Tarreaua8519352018-12-18 16:44:28 +01001455 flags |= CS_FL_ERR_PENDING;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001456
1457 if (conn_xprt_read0_pending(h2c->conn))
Christopher Faulet87a8f352019-03-22 14:51:36 +01001458 flags |= CS_FL_REOS;
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001459
Christopher Fauletf02ca002019-03-07 16:21:34 +01001460 /* Wake all streams with ID > last */
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001461 node = eb32_lookup_ge(&h2c->streams_by_id, last + 1);
1462 while (node) {
1463 h2s = container_of(node, struct h2s, by_id);
1464 if (h2s->id <= last)
1465 break;
1466 node = eb32_next(node);
Christopher Fauletf02ca002019-03-07 16:21:34 +01001467 h2s_wake_one_stream(h2s, flags);
1468 }
Willy Tarreau22cf59b2017-11-10 11:42:33 +01001469
Christopher Fauletf02ca002019-03-07 16:21:34 +01001470 /* Wake all streams with unassigned ID (ID == 0) */
1471 node = eb32_lookup(&h2c->streams_by_id, 0);
1472 while (node) {
1473 h2s = container_of(node, struct h2s, by_id);
1474 if (h2s->id > 0)
1475 break;
1476 node = eb32_next(node);
1477 h2s_wake_one_stream(h2s, flags);
Willy Tarreau23b92aa2017-10-30 00:26:54 +01001478 }
1479}
1480
Willy Tarreau3421aba2017-07-27 15:41:03 +02001481/* Increase all streams' outgoing window size by the difference passed in
1482 * argument. This is needed upon receipt of the settings frame if the initial
1483 * window size is different. The difference may be negative and the resulting
1484 * window size as well, for the time it takes to receive some window updates.
1485 */
1486static void h2c_update_all_ws(struct h2c *h2c, int diff)
1487{
1488 struct h2s *h2s;
1489 struct eb32_node *node;
1490
1491 if (!diff)
1492 return;
1493
1494 node = eb32_first(&h2c->streams_by_id);
1495 while (node) {
1496 h2s = container_of(node, struct h2s, by_id);
1497 h2s->mws += diff;
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001498
1499 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1500 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreau40873462019-05-13 08:05:28 +02001501 if (h2s->send_wait && LIST_ISEMPTY(&h2s->list))
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001502 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreaub1c9edc2019-01-30 16:11:20 +01001503 }
1504
Willy Tarreau3421aba2017-07-27 15:41:03 +02001505 node = eb32_next(node);
1506 }
1507}
1508
1509/* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and
1510 * ACKs it if needed. Returns > 0 on success or zero on missing data. It may
Willy Tarreaub860c732019-01-30 15:39:55 +01001511 * return an error in h2c. The caller must have already verified frame length
1512 * and stream ID validity. Described in RFC7540#6.5.
Willy Tarreau3421aba2017-07-27 15:41:03 +02001513 */
1514static int h2c_handle_settings(struct h2c *h2c)
1515{
1516 unsigned int offset;
1517 int error;
1518
1519 if (h2c->dff & H2_F_SETTINGS_ACK) {
1520 if (h2c->dfl) {
1521 error = H2_ERR_FRAME_SIZE_ERROR;
1522 goto fail;
1523 }
1524 return 1;
1525 }
1526
Willy Tarreau3421aba2017-07-27 15:41:03 +02001527 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001528 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau3421aba2017-07-27 15:41:03 +02001529 return 0;
1530
1531 /* parse the frame */
1532 for (offset = 0; offset < h2c->dfl; offset += 6) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001533 uint16_t type = h2_get_n16(&h2c->dbuf, offset);
1534 int32_t arg = h2_get_n32(&h2c->dbuf, offset + 2);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001535
1536 switch (type) {
1537 case H2_SETTINGS_INITIAL_WINDOW_SIZE:
1538 /* we need to update all existing streams with the
1539 * difference from the previous iws.
1540 */
1541 if (arg < 0) { // RFC7540#6.5.2
1542 error = H2_ERR_FLOW_CONTROL_ERROR;
1543 goto fail;
1544 }
1545 h2c_update_all_ws(h2c, arg - h2c->miw);
1546 h2c->miw = arg;
1547 break;
1548 case H2_SETTINGS_MAX_FRAME_SIZE:
1549 if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2
1550 error = H2_ERR_PROTOCOL_ERROR;
1551 goto fail;
1552 }
1553 h2c->mfs = arg;
1554 break;
Willy Tarreau1b38b462017-12-03 19:02:28 +01001555 case H2_SETTINGS_ENABLE_PUSH:
1556 if (arg < 0 || arg > 1) { // RFC7540#6.5.2
1557 error = H2_ERR_PROTOCOL_ERROR;
1558 goto fail;
1559 }
1560 break;
Willy Tarreau2e2083a2019-01-31 10:34:07 +01001561 case H2_SETTINGS_MAX_CONCURRENT_STREAMS:
1562 if (h2c->flags & H2_CF_IS_BACK) {
1563 /* the limit is only for the backend; for the frontend it is our limit */
1564 if ((unsigned int)arg > h2_settings_max_concurrent_streams)
1565 arg = h2_settings_max_concurrent_streams;
1566 h2c->streams_limit = arg;
1567 }
1568 break;
Willy Tarreau3421aba2017-07-27 15:41:03 +02001569 }
1570 }
1571
1572 /* need to ACK this frame now */
1573 h2c->st0 = H2_CS_FRAME_A;
1574 return 1;
1575 fail:
Willy Tarreau22de8d32018-09-05 19:55:58 +02001576 sess_log(h2c->conn->owner);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001577 h2c_error(h2c, error);
1578 return 0;
1579}
1580
1581/* try to send an ACK for a settings frame on the connection. Returns > 0 on
1582 * success or one of the h2_status values.
1583 */
1584static int h2c_ack_settings(struct h2c *h2c)
1585{
1586 struct buffer *res;
1587 char str[9];
1588 int ret = -1;
1589
1590 if (h2c_mux_busy(h2c, NULL)) {
1591 h2c->flags |= H2_CF_DEM_MBUSY;
1592 return 0;
1593 }
1594
Willy Tarreau44e973f2018-03-01 17:49:30 +01001595 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreau3421aba2017-07-27 15:41:03 +02001596 if (!res) {
1597 h2c->flags |= H2_CF_MUX_MALLOC;
1598 h2c->flags |= H2_CF_DEM_MROOM;
1599 return 0;
1600 }
1601
1602 memcpy(str,
1603 "\x00\x00\x00" /* length : 0 (no data) */
1604 "\x04" "\x01" /* type : 4, flags : ACK */
1605 "\x00\x00\x00\x00" /* stream ID */, 9);
1606
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001607 ret = b_istput(res, ist2(str, 9));
Willy Tarreau3421aba2017-07-27 15:41:03 +02001608 if (unlikely(ret <= 0)) {
1609 if (!ret) {
1610 h2c->flags |= H2_CF_MUX_MFULL;
1611 h2c->flags |= H2_CF_DEM_MROOM;
1612 return 0;
1613 }
1614 else {
1615 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1616 return 0;
1617 }
1618 }
1619 return ret;
1620}
1621
Willy Tarreaucf68c782017-10-10 17:11:41 +02001622/* processes a PING frame and schedules an ACK if needed. The caller must pass
1623 * the pointer to the payload in <payload>. Returns > 0 on success or zero on
Willy Tarreaub860c732019-01-30 15:39:55 +01001624 * missing data. The caller must have already verified frame length
1625 * and stream ID validity.
Willy Tarreaucf68c782017-10-10 17:11:41 +02001626 */
1627static int h2c_handle_ping(struct h2c *h2c)
1628{
Willy Tarreaucf68c782017-10-10 17:11:41 +02001629 /* schedule a response */
Willy Tarreau68ed6412017-12-03 18:15:56 +01001630 if (!(h2c->dff & H2_F_PING_ACK))
Willy Tarreaucf68c782017-10-10 17:11:41 +02001631 h2c->st0 = H2_CS_FRAME_A;
1632 return 1;
1633}
1634
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001635/* Try to send a window update for stream id <sid> and value <increment>.
1636 * Returns > 0 on success or zero on missing room or failure. It may return an
1637 * error in h2c.
1638 */
1639static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment)
1640{
1641 struct buffer *res;
1642 char str[13];
1643 int ret = -1;
1644
1645 if (h2c_mux_busy(h2c, NULL)) {
1646 h2c->flags |= H2_CF_DEM_MBUSY;
1647 return 0;
1648 }
1649
Willy Tarreau44e973f2018-03-01 17:49:30 +01001650 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001651 if (!res) {
1652 h2c->flags |= H2_CF_MUX_MALLOC;
1653 h2c->flags |= H2_CF_DEM_MROOM;
1654 return 0;
1655 }
1656
1657 /* length: 4, type: 8, flags: none */
1658 memcpy(str, "\x00\x00\x04\x08\x00", 5);
1659 write_n32(str + 5, sid);
1660 write_n32(str + 9, increment);
1661
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001662 ret = b_istput(res, ist2(str, 13));
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001663
1664 if (unlikely(ret <= 0)) {
1665 if (!ret) {
1666 h2c->flags |= H2_CF_MUX_MFULL;
1667 h2c->flags |= H2_CF_DEM_MROOM;
1668 return 0;
1669 }
1670 else {
1671 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1672 return 0;
1673 }
1674 }
1675 return ret;
1676}
1677
1678/* try to send pending window update for the connection. It's safe to call it
1679 * with no pending updates. Returns > 0 on success or zero on missing room or
1680 * failure. It may return an error in h2c.
1681 */
1682static int h2c_send_conn_wu(struct h2c *h2c)
1683{
1684 int ret = 1;
1685
1686 if (h2c->rcvd_c <= 0)
1687 return 1;
1688
Willy Tarreau97aaa672018-12-23 09:49:04 +01001689 if (!(h2c->flags & H2_CF_WINDOW_OPENED)) {
1690 /* increase the advertised connection window to 2G on
1691 * first update.
1692 */
1693 h2c->flags |= H2_CF_WINDOW_OPENED;
1694 h2c->rcvd_c += H2_INITIAL_WINDOW_INCREMENT;
1695 }
1696
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02001697 /* send WU for the connection */
1698 ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c);
1699 if (ret > 0)
1700 h2c->rcvd_c = 0;
1701
1702 return ret;
1703}
1704
1705/* try to send pending window update for the current dmux stream. It's safe to
1706 * call it with no pending updates. Returns > 0 on success or zero on missing
1707 * room or failure. It may return an error in h2c.
1708 */
1709static int h2c_send_strm_wu(struct h2c *h2c)
1710{
1711 int ret = 1;
1712
1713 if (h2c->rcvd_s <= 0)
1714 return 1;
1715
1716 /* send WU for the stream */
1717 ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s);
1718 if (ret > 0)
1719 h2c->rcvd_s = 0;
1720
1721 return ret;
1722}
1723
Willy Tarreaucf68c782017-10-10 17:11:41 +02001724/* try to send an ACK for a ping frame on the connection. Returns > 0 on
1725 * success, 0 on missing data or one of the h2_status values.
1726 */
1727static int h2c_ack_ping(struct h2c *h2c)
1728{
1729 struct buffer *res;
1730 char str[17];
1731 int ret = -1;
1732
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001733 if (b_data(&h2c->dbuf) < 8)
Willy Tarreaucf68c782017-10-10 17:11:41 +02001734 return 0;
1735
1736 if (h2c_mux_busy(h2c, NULL)) {
1737 h2c->flags |= H2_CF_DEM_MBUSY;
1738 return 0;
1739 }
1740
Willy Tarreau44e973f2018-03-01 17:49:30 +01001741 res = h2_get_buf(h2c, &h2c->mbuf);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001742 if (!res) {
1743 h2c->flags |= H2_CF_MUX_MALLOC;
1744 h2c->flags |= H2_CF_DEM_MROOM;
1745 return 0;
1746 }
1747
1748 memcpy(str,
1749 "\x00\x00\x08" /* length : 8 (same payload) */
1750 "\x06" "\x01" /* type : 6, flags : ACK */
1751 "\x00\x00\x00\x00" /* stream ID */, 9);
1752
1753 /* copy the original payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001754 h2_get_buf_bytes(str + 9, 8, &h2c->dbuf, 0);
Willy Tarreaucf68c782017-10-10 17:11:41 +02001755
Willy Tarreauea1b06d2018-07-12 09:02:47 +02001756 ret = b_istput(res, ist2(str, 17));
Willy Tarreaucf68c782017-10-10 17:11:41 +02001757 if (unlikely(ret <= 0)) {
1758 if (!ret) {
1759 h2c->flags |= H2_CF_MUX_MFULL;
1760 h2c->flags |= H2_CF_DEM_MROOM;
1761 return 0;
1762 }
1763 else {
1764 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
1765 return 0;
1766 }
1767 }
1768 return ret;
1769}
1770
Willy Tarreau26f95952017-07-27 17:18:30 +02001771/* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes.
1772 * Returns > 0 on success or zero on missing data. It may return an error in
Willy Tarreaub860c732019-01-30 15:39:55 +01001773 * h2c or h2s. The caller must have already verified frame length and stream ID
1774 * validity. Described in RFC7540#6.9.
Willy Tarreau26f95952017-07-27 17:18:30 +02001775 */
1776static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s)
1777{
1778 int32_t inc;
1779 int error;
1780
Willy Tarreau26f95952017-07-27 17:18:30 +02001781 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001782 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau26f95952017-07-27 17:18:30 +02001783 return 0;
1784
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001785 inc = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau26f95952017-07-27 17:18:30 +02001786
1787 if (h2c->dsi != 0) {
1788 /* stream window update */
Willy Tarreau26f95952017-07-27 17:18:30 +02001789
1790 /* it's not an error to receive WU on a closed stream */
1791 if (h2s->st == H2_SS_CLOSED)
1792 return 1;
1793
1794 if (!inc) {
1795 error = H2_ERR_PROTOCOL_ERROR;
1796 goto strm_err;
1797 }
1798
1799 if (h2s->mws >= 0 && h2s->mws + inc < 0) {
1800 error = H2_ERR_FLOW_CONTROL_ERROR;
1801 goto strm_err;
1802 }
1803
1804 h2s->mws += inc;
1805 if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) {
1806 h2s->flags &= ~H2_SF_BLK_SFCTL;
Willy Tarreau40873462019-05-13 08:05:28 +02001807 if (h2s->send_wait && LIST_ISEMPTY(&h2s->list))
Olivier Houcharddddfe312018-10-10 18:51:00 +02001808 LIST_ADDQ(&h2c->send_list, &h2s->list);
Willy Tarreau26f95952017-07-27 17:18:30 +02001809 }
1810 }
1811 else {
1812 /* connection window update */
1813 if (!inc) {
1814 error = H2_ERR_PROTOCOL_ERROR;
1815 goto conn_err;
1816 }
1817
1818 if (h2c->mws >= 0 && h2c->mws + inc < 0) {
1819 error = H2_ERR_FLOW_CONTROL_ERROR;
1820 goto conn_err;
1821 }
1822
1823 h2c->mws += inc;
1824 }
1825
1826 return 1;
1827
1828 conn_err:
1829 h2c_error(h2c, error);
1830 return 0;
1831
1832 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01001833 h2s_error(h2s, error);
1834 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau26f95952017-07-27 17:18:30 +02001835 return 0;
1836}
1837
Willy Tarreaue96b0922017-10-30 00:28:29 +01001838/* processes a GOAWAY frame, and signals all streams whose ID is greater than
Willy Tarreaub860c732019-01-30 15:39:55 +01001839 * the last ID. Returns > 0 on success or zero on missing data. The caller must
1840 * have already verified frame length and stream ID validity. Described in
1841 * RFC7540#6.8.
Willy Tarreaue96b0922017-10-30 00:28:29 +01001842 */
1843static int h2c_handle_goaway(struct h2c *h2c)
1844{
Willy Tarreaue96b0922017-10-30 00:28:29 +01001845 int last;
1846
Willy Tarreaue96b0922017-10-30 00:28:29 +01001847 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001848 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaue96b0922017-10-30 00:28:29 +01001849 return 0;
1850
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001851 last = h2_get_n32(&h2c->dbuf, 0);
1852 h2c->errcode = h2_get_n32(&h2c->dbuf, 4);
Olivier Houchard91177802018-12-19 14:49:39 +01001853 h2_wake_some_streams(h2c, last, CS_FL_ERR_PENDING);
Willy Tarreau11cc2d62017-12-03 10:27:47 +01001854 if (h2c->last_sid < 0)
1855 h2c->last_sid = last;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001856 return 1;
Willy Tarreaue96b0922017-10-30 00:28:29 +01001857}
1858
Willy Tarreau92153fc2017-12-03 19:46:19 +01001859/* processes a PRIORITY frame, and either skips it or rejects if it is
Willy Tarreaub860c732019-01-30 15:39:55 +01001860 * invalid. Returns > 0 on success or zero on missing data. It may return an
1861 * error in h2c. The caller must have already verified frame length and stream
1862 * ID validity. Described in RFC7540#6.3.
Willy Tarreau92153fc2017-12-03 19:46:19 +01001863 */
1864static int h2c_handle_priority(struct h2c *h2c)
1865{
Willy Tarreau92153fc2017-12-03 19:46:19 +01001866 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001867 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreau92153fc2017-12-03 19:46:19 +01001868 return 0;
1869
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001870 if (h2_get_n32(&h2c->dbuf, 0) == h2c->dsi) {
Willy Tarreau92153fc2017-12-03 19:46:19 +01001871 /* 7540#5.3 : can't depend on itself */
Willy Tarreaub860c732019-01-30 15:39:55 +01001872 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
1873 return 0;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001874 }
1875 return 1;
Willy Tarreau92153fc2017-12-03 19:46:19 +01001876}
1877
Willy Tarreaucd234e92017-08-18 10:59:39 +02001878/* processes an RST_STREAM frame, and sets the 32-bit error code on the stream.
Willy Tarreaub860c732019-01-30 15:39:55 +01001879 * Returns > 0 on success or zero on missing data. The caller must have already
1880 * verified frame length and stream ID validity. Described in RFC7540#6.4.
Willy Tarreaucd234e92017-08-18 10:59:39 +02001881 */
1882static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s)
1883{
Willy Tarreaucd234e92017-08-18 10:59:39 +02001884 /* process full frame only */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001885 if (b_data(&h2c->dbuf) < h2c->dfl)
Willy Tarreaucd234e92017-08-18 10:59:39 +02001886 return 0;
1887
1888 /* late RST, already handled */
1889 if (h2s->st == H2_SS_CLOSED)
1890 return 1;
1891
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001892 h2s->errcode = h2_get_n32(&h2c->dbuf, 0);
Willy Tarreau00dd0782018-03-01 16:31:34 +01001893 h2s_close(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001894
1895 if (h2s->cs) {
Willy Tarreauec988c72018-12-19 18:00:29 +01001896 cs_set_error(h2s->cs);
Willy Tarreauf830f012018-12-19 17:44:55 +01001897 h2s_alert(h2s);
Willy Tarreaucd234e92017-08-18 10:59:39 +02001898 }
1899
1900 h2s->flags |= H2_SF_RST_RCVD;
1901 return 1;
Willy Tarreaucd234e92017-08-18 10:59:39 +02001902}
1903
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001904/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
1905 * It may return an error in h2c or h2s. The caller must consider that the
1906 * return value is the new h2s in case one was allocated (most common case).
1907 * Described in RFC7540#6.2. Most of the
Willy Tarreau13278b42017-10-13 19:23:14 +02001908 * errors here are reported as connection errors since it's impossible to
1909 * recover from such errors after the compression context has been altered.
1910 */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001911static struct h2s *h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s)
Willy Tarreau13278b42017-10-13 19:23:14 +02001912{
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001913 struct buffer rxbuf = BUF_NULL;
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001914 unsigned long long body_len = 0;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001915 uint32_t flags = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02001916 int error;
1917
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001918 if (!b_size(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001919 return NULL; // empty buffer
Willy Tarreau13278b42017-10-13 19:23:14 +02001920
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001921 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau2a761dc2018-02-26 18:50:57 +01001922 return NULL; // incomplete frame
Willy Tarreau13278b42017-10-13 19:23:14 +02001923
1924 /* now either the frame is complete or the buffer is complete */
1925 if (h2s->st != H2_SS_IDLE) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01001926 /* The stream exists/existed, this must be a trailers frame */
1927 if (h2s->st != H2_SS_CLOSED) {
Willy Tarreauaab1a602019-05-06 11:12:18 +02001928 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &body_len);
1929 /* unrecoverable error ? */
1930 if (h2c->st0 >= H2_CS_ERROR)
1931 goto out;
1932
1933 if (error == 0)
1934 goto out; // missing data
1935
1936 if (error < 0) {
1937 /* Failed to decode this frame (e.g. too large request)
1938 * but the HPACK decompressor is still synchronized.
1939 */
1940 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
1941 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau88d138e2019-01-02 19:38:14 +01001942 goto out;
Willy Tarreauaab1a602019-05-06 11:12:18 +02001943 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01001944 goto done;
1945 }
Willy Tarreau1f035502019-01-30 11:44:07 +01001946 /* the connection was already killed by an RST, let's consume
1947 * the data and send another RST.
1948 */
1949 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
1950 h2s = (struct h2s*)h2_error_stream;
1951 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001952 }
1953 else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) {
1954 /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */
1955 error = H2_ERR_PROTOCOL_ERROR;
Willy Tarreau22de8d32018-09-05 19:55:58 +02001956 sess_log(h2c->conn->owner);
Willy Tarreau13278b42017-10-13 19:23:14 +02001957 goto conn_err;
1958 }
Willy Tarreau415b1ee2019-01-02 13:59:43 +01001959 else if (h2c->flags & H2_CF_DEM_TOOMANY)
1960 goto out; // IDLE but too many cs still present
Willy Tarreau13278b42017-10-13 19:23:14 +02001961
Willy Tarreau4790f7c2019-01-24 11:33:02 +01001962 error = h2c_decode_headers(h2c, &rxbuf, &flags, &body_len);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001963
Willy Tarreau25919232019-01-03 14:48:18 +01001964 /* unrecoverable error ? */
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001965 if (h2c->st0 >= H2_CS_ERROR)
1966 goto out;
1967
Willy Tarreau25919232019-01-03 14:48:18 +01001968 if (error <= 0) {
1969 if (error == 0)
1970 goto out; // missing data
1971
1972 /* Failed to decode this stream (e.g. too large request)
1973 * but the HPACK decompressor is still synchronized.
1974 */
1975 h2s = (struct h2s*)h2_error_stream;
1976 goto send_rst;
1977 }
1978
Willy Tarreau22de8d32018-09-05 19:55:58 +02001979 /* Note: we don't emit any other logs below because ff we return
Willy Tarreaua8e49542018-10-03 18:53:55 +02001980 * positively from h2c_frt_stream_new(), the stream will report the error,
1981 * and if we return in error, h2c_frt_stream_new() will emit the error.
Willy Tarreau22de8d32018-09-05 19:55:58 +02001982 */
Willy Tarreaua8e49542018-10-03 18:53:55 +02001983 h2s = h2c_frt_stream_new(h2c, h2c->dsi);
Willy Tarreau13278b42017-10-13 19:23:14 +02001984 if (!h2s) {
Willy Tarreau96a10c22018-12-23 18:30:44 +01001985 h2s = (struct h2s*)h2_refused_stream;
1986 goto send_rst;
Willy Tarreau13278b42017-10-13 19:23:14 +02001987 }
1988
1989 h2s->st = H2_SS_OPEN;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001990 h2s->rxbuf = rxbuf;
1991 h2s->flags |= flags;
Willy Tarreau1915ca22019-01-24 11:49:37 +01001992 h2s->body_len = body_len;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001993
Willy Tarreau88d138e2019-01-02 19:38:14 +01001994 done:
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001995 if (h2c->dff & H2_F_HEADERS_END_STREAM)
Willy Tarreau13278b42017-10-13 19:23:14 +02001996 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01001997
1998 if (h2s->flags & H2_SF_ES_RCVD) {
Christopher Faulet63768a62019-03-22 14:05:52 +01001999 if (h2s->cs)
2000 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002001 if (h2s->st == H2_SS_OPEN)
2002 h2s->st = H2_SS_HREM;
2003 else
2004 h2s_close(h2s);
Willy Tarreau13278b42017-10-13 19:23:14 +02002005 }
2006
Willy Tarreau3a429f02019-01-03 11:41:50 +01002007 /* update the max stream ID if the request is being processed */
2008 if (h2s->id > h2c->max_id)
2009 h2c->max_id = h2s->id;
Willy Tarreau13278b42017-10-13 19:23:14 +02002010
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002011 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002012
2013 conn_err:
2014 h2c_error(h2c, error);
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002015 goto out;
Willy Tarreau13278b42017-10-13 19:23:14 +02002016
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01002017 out:
2018 h2_release_buf(h2c, &rxbuf);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002019 return NULL;
Willy Tarreau96a10c22018-12-23 18:30:44 +01002020
2021 send_rst:
2022 /* make the demux send an RST for the current stream. We may only
2023 * do this if we're certain that the HEADERS frame was properly
2024 * decompressed so that the HPACK decoder is still kept up to date.
2025 */
2026 h2_release_buf(h2c, &rxbuf);
2027 h2c->st0 = H2_CS_FRAME_E;
2028 return h2s;
Willy Tarreau13278b42017-10-13 19:23:14 +02002029}
2030
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002031/* processes a HEADERS frame. Returns h2s on success or NULL on missing data.
2032 * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the
2033 * errors here are reported as connection errors since it's impossible to
2034 * recover from such errors after the compression context has been altered.
2035 */
2036static struct h2s *h2c_bck_handle_headers(struct h2c *h2c, struct h2s *h2s)
2037{
2038 int error;
2039
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002040 if (!b_size(&h2c->dbuf))
2041 return NULL; // empty buffer
2042
2043 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
2044 return NULL; // incomplete frame
2045
Willy Tarreau1915ca22019-01-24 11:49:37 +01002046 error = h2c_decode_headers(h2c, &h2s->rxbuf, &h2s->flags, &h2s->body_len);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002047
Willy Tarreau25919232019-01-03 14:48:18 +01002048 /* unrecoverable error ? */
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002049 if (h2c->st0 >= H2_CS_ERROR)
2050 return NULL;
2051
Willy Tarreau08bb1d62019-01-30 16:55:48 +01002052 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2053 /* RFC7540#5.1 */
2054 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2055 h2c->st0 = H2_CS_FRAME_E;
2056 return NULL;
2057 }
2058
Willy Tarreau25919232019-01-03 14:48:18 +01002059 if (error <= 0) {
2060 if (error == 0)
2061 return NULL; // missing data
2062
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002063 /* stream error : send RST_STREAM */
Willy Tarreau25919232019-01-03 14:48:18 +01002064 h2s_error(h2s, H2_ERR_PROTOCOL_ERROR);
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002065 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau25919232019-01-03 14:48:18 +01002066 return NULL;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002067 }
2068
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002069 if (h2c->dff & H2_F_HEADERS_END_STREAM) {
2070 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau927b88b2019-03-04 08:03:25 +01002071 if (h2s->cs)
Christopher Faulet63768a62019-03-22 14:05:52 +01002072 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreau45ffc0c2019-01-03 09:32:20 +01002073 }
2074
Willy Tarreau927b88b2019-03-04 08:03:25 +01002075 if (h2s->cs && h2s->cs->flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002076 h2s->st = H2_SS_ERROR;
Christopher Faulet63768a62019-03-22 14:05:52 +01002077 else if (h2s->cs && (h2s->cs->flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_OPEN)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002078 h2s->st = H2_SS_HREM;
Willy Tarreau201fe402019-05-07 18:10:10 +02002079 else if ((!h2s->cs || h2s->cs->flags & (CS_FL_EOI|CS_FL_REOS)) && h2s->st == H2_SS_HLOC)
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002080 h2s_close(h2s);
2081
2082 return h2s;
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002083}
2084
Willy Tarreau454f9052017-10-26 19:40:35 +02002085/* processes a DATA frame. Returns > 0 on success or zero on missing data.
2086 * It may return an error in h2c or h2s. Described in RFC7540#6.1.
2087 */
2088static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s)
2089{
2090 int error;
2091
2092 /* note that empty DATA frames are perfectly valid and sometimes used
2093 * to signal an end of stream (with the ES flag).
2094 */
2095
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002096 if (!b_size(&h2c->dbuf) && h2c->dfl)
Willy Tarreau454f9052017-10-26 19:40:35 +02002097 return 0; // empty buffer
2098
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002099 if (b_data(&h2c->dbuf) < h2c->dfl && !b_full(&h2c->dbuf))
Willy Tarreau454f9052017-10-26 19:40:35 +02002100 return 0; // incomplete frame
2101
2102 /* now either the frame is complete or the buffer is complete */
2103
Willy Tarreau454f9052017-10-26 19:40:35 +02002104 if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) {
2105 /* RFC7540#6.1 */
2106 error = H2_ERR_STREAM_CLOSED;
2107 goto strm_err;
2108 }
2109
Willy Tarreau1915ca22019-01-24 11:49:37 +01002110 if ((h2s->flags & H2_SF_DATA_CLEN) && h2c->dfl > h2s->body_len) {
2111 /* RFC7540#8.1.2 */
2112 error = H2_ERR_PROTOCOL_ERROR;
2113 goto strm_err;
2114 }
2115
Willy Tarreaua56a6de2018-02-26 15:59:07 +01002116 if (!h2_frt_transfer_data(h2s))
2117 return 0;
2118
Willy Tarreau454f9052017-10-26 19:40:35 +02002119 /* call the upper layers to process the frame, then let the upper layer
2120 * notify the stream about any change.
2121 */
2122 if (!h2s->cs) {
2123 error = H2_ERR_STREAM_CLOSED;
2124 goto strm_err;
2125 }
2126
Willy Tarreau8f650c32017-11-21 19:36:21 +01002127 if (h2c->st0 >= H2_CS_ERROR)
2128 return 0;
2129
Willy Tarreau721c9742017-11-07 11:05:42 +01002130 if (h2s->st >= H2_SS_ERROR) {
Willy Tarreau454f9052017-10-26 19:40:35 +02002131 /* stream error : send RST_STREAM */
Willy Tarreaua20a5192017-12-27 11:02:06 +01002132 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002133 }
2134
2135 /* check for completion : the callee will change this to FRAME_A or
2136 * FRAME_H once done.
2137 */
2138 if (h2c->st0 == H2_CS_FRAME_P)
2139 return 0;
2140
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002141 /* last frame */
2142 if (h2c->dff & H2_F_DATA_END_STREAM) {
Christopher Faulet63768a62019-03-22 14:05:52 +01002143 if (h2s->cs)
2144 h2s->cs->flags |= CS_FL_EOI;
Willy Tarreaufc10f592019-01-30 19:28:32 +01002145 if (h2s->st == H2_SS_OPEN)
2146 h2s->st = H2_SS_HREM;
2147 else
2148 h2s_close(h2s);
2149
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002150 h2s->flags |= H2_SF_ES_RCVD;
Willy Tarreau1915ca22019-01-24 11:49:37 +01002151
2152 if (h2s->flags & H2_SF_DATA_CLEN && h2s->body_len) {
2153 /* RFC7540#8.1.2 */
2154 error = H2_ERR_PROTOCOL_ERROR;
2155 goto strm_err;
2156 }
Willy Tarreauc4134ba2017-12-11 18:45:08 +01002157 }
2158
Willy Tarreau454f9052017-10-26 19:40:35 +02002159 return 1;
2160
Willy Tarreau454f9052017-10-26 19:40:35 +02002161 strm_err:
Willy Tarreau6432dc82019-01-30 15:42:44 +01002162 h2s_error(h2s, error);
2163 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau454f9052017-10-26 19:40:35 +02002164 return 0;
2165}
2166
Willy Tarreaubc933932017-10-09 16:21:43 +02002167/* process Rx frames to be demultiplexed */
2168static void h2_process_demux(struct h2c *h2c)
2169{
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002170 struct h2s *h2s = NULL, *tmp_h2s;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002171 struct h2_fh hdr;
2172 unsigned int padlen = 0;
Willy Tarreauf3ee0692017-10-17 08:18:25 +02002173
Willy Tarreau081d4722017-05-16 21:51:05 +02002174 if (h2c->st0 >= H2_CS_ERROR)
2175 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002176
2177 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2178 if (h2c->st0 == H2_CS_PREFACE) {
Willy Tarreau01b44822018-10-03 14:26:37 +02002179 if (h2c->flags & H2_CF_IS_BACK)
2180 return;
Willy Tarreau52eed752017-09-22 15:05:09 +02002181 if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) {
2182 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002183 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau52eed752017-09-22 15:05:09 +02002184 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002185 sess_log(h2c->conn->owner);
2186 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002187 goto fail;
2188 }
2189
2190 h2c->max_id = 0;
2191 h2c->st0 = H2_CS_SETTINGS1;
2192 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002193
2194 if (h2c->st0 == H2_CS_SETTINGS1) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002195 /* ensure that what is pending is a valid SETTINGS frame
2196 * without an ACK.
2197 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002198 if (!h2_get_frame_hdr(&h2c->dbuf, &hdr)) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002199 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
Willy Tarreau22de8d32018-09-05 19:55:58 +02002200 if (h2c->st0 == H2_CS_ERROR) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002201 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002202 sess_log(h2c->conn->owner);
2203 }
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002204 goto fail;
2205 }
2206
2207 if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) {
2208 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2209 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2210 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002211 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002212 goto fail;
2213 }
2214
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002215 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002216 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2217 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2218 h2c->st0 = H2_CS_ERROR2;
Willy Tarreau22de8d32018-09-05 19:55:58 +02002219 sess_log(h2c->conn->owner);
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002220 goto fail;
2221 }
2222
Willy Tarreau3bf69182018-12-21 15:34:50 +01002223 /* that's OK, switch to FRAME_P to process it. This is
2224 * a SETTINGS frame whose header has already been
2225 * deleted above.
2226 */
Willy Tarreau54f46e52019-01-30 15:11:03 +01002227 padlen = 0;
2228 goto new_frame;
Willy Tarreau4c3690b2017-10-10 15:16:55 +02002229 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002230 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002231
2232 /* process as many incoming frames as possible below */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002233 while (b_data(&h2c->dbuf)) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002234 int ret = 0;
2235
2236 if (h2c->st0 >= H2_CS_ERROR)
2237 break;
2238
2239 if (h2c->st0 == H2_CS_FRAME_H) {
Willy Tarreaua4428bd2018-12-22 18:11:41 +01002240 if (!h2_peek_frame_hdr(&h2c->dbuf, 0, &hdr))
Willy Tarreau7e98c052017-10-10 15:56:59 +02002241 break;
2242
Willy Tarreau3f0e1ec2018-04-17 10:28:27 +02002243 if ((int)hdr.len < 0 || (int)hdr.len > global.tune.bufsize) {
Willy Tarreau7e98c052017-10-10 15:56:59 +02002244 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002245 if (!h2c->nb_streams) {
2246 /* only log if no other stream can report the error */
2247 sess_log(h2c->conn->owner);
2248 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002249 break;
2250 }
2251
Willy Tarreau3bf69182018-12-21 15:34:50 +01002252 if (h2_ft_bit(hdr.ft) & H2_FT_PADDED_MASK && hdr.ff & H2_F_PADDED) {
2253 /* If the frame is padded (HEADERS, PUSH_PROMISE or DATA),
2254 * we read the pad length and drop it from the remaining
2255 * payload (one byte + the 9 remaining ones = 10 total
2256 * removed), so we have a frame payload starting after the
2257 * pad len. Flow controlled frames (DATA) also count the
2258 * padlen in the flow control, so it must be adjusted.
2259 */
2260 if (hdr.len < 1) {
2261 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
2262 sess_log(h2c->conn->owner);
2263 goto fail;
2264 }
2265 hdr.len--;
2266
2267 if (b_data(&h2c->dbuf) < 10)
2268 break; // missing padlen
2269
2270 padlen = *(uint8_t *)b_peek(&h2c->dbuf, 9);
2271
2272 if (padlen > hdr.len) {
2273 /* RFC7540#6.1 : pad length = length of
2274 * frame payload or greater => error.
2275 */
2276 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2277 sess_log(h2c->conn->owner);
2278 goto fail;
2279 }
2280
2281 if (h2_ft_bit(hdr.ft) & H2_FT_FC_MASK) {
2282 h2c->rcvd_c++;
2283 h2c->rcvd_s++;
2284 }
2285 b_del(&h2c->dbuf, 1);
2286 }
2287 h2_skip_frame_hdr(&h2c->dbuf);
Willy Tarreau54f46e52019-01-30 15:11:03 +01002288
2289 new_frame:
Willy Tarreau7e98c052017-10-10 15:56:59 +02002290 h2c->dfl = hdr.len;
2291 h2c->dsi = hdr.sid;
2292 h2c->dft = hdr.ft;
2293 h2c->dff = hdr.ff;
Willy Tarreau3bf69182018-12-21 15:34:50 +01002294 h2c->dpl = padlen;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002295 h2c->st0 = H2_CS_FRAME_P;
Willy Tarreau54f46e52019-01-30 15:11:03 +01002296
2297 /* check for minimum basic frame format validity */
2298 ret = h2_frame_check(h2c->dft, 1, h2c->dsi, h2c->dfl, global.tune.bufsize);
2299 if (ret != H2_ERR_NO_ERROR) {
2300 h2c_error(h2c, ret);
2301 sess_log(h2c->conn->owner);
2302 goto fail;
2303 }
Willy Tarreau7e98c052017-10-10 15:56:59 +02002304 }
2305
2306 /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002307 tmp_h2s = h2c_st_by_id(h2c, h2c->dsi);
2308
Willy Tarreau567beb82018-12-18 16:52:44 +01002309 if (tmp_h2s != h2s && h2s && h2s->cs &&
2310 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002311 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS|CS_FL_EOI)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002312 /* we may have to signal the upper layers */
2313 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002314 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002315 }
2316 h2s = tmp_h2s;
Willy Tarreau7e98c052017-10-10 15:56:59 +02002317
Willy Tarreaud7901432017-12-29 11:34:40 +01002318 if (h2c->st0 == H2_CS_FRAME_E)
2319 goto strm_err;
2320
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002321 if (h2s->st == H2_SS_IDLE &&
2322 h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) {
2323 /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in
2324 * this state MUST be treated as a connection error
2325 */
2326 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreau22de8d32018-09-05 19:55:58 +02002327 if (!h2c->nb_streams) {
2328 /* only log if no other stream can report the error */
2329 sess_log(h2c->conn->owner);
2330 }
Willy Tarreauf65b80d2017-10-30 11:46:49 +01002331 break;
2332 }
2333
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002334 if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE &&
2335 h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) {
2336 /* RFC7540#5.1: any frame other than WU/PRIO/RST in
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002337 * this state MUST be treated as a stream error.
2338 * 6.2, 6.6 and 6.10 further mandate that HEADERS/
2339 * PUSH_PROMISE/CONTINUATION cause connection errors.
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002340 */
Willy Tarreau5b4eae32019-01-24 09:43:32 +01002341 if (h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK)
2342 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2343 else
2344 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002345 goto strm_err;
2346 }
2347
Willy Tarreauab837502017-12-27 15:07:30 +01002348 /* Below the management of frames received in closed state is a
2349 * bit hackish because the spec makes strong differences between
2350 * streams closed by receiving RST, sending RST, and seeing ES
2351 * in both directions. In addition to this, the creation of a
2352 * new stream reusing the identifier of a closed one will be
2353 * detected here. Given that we cannot keep track of all closed
2354 * streams forever, we consider that unknown closed streams were
2355 * closed on RST received, which allows us to respond with an
2356 * RST without breaking the connection (eg: to abort a transfer).
2357 * Some frames have to be silently ignored as well.
2358 */
2359 if (h2s->st == H2_SS_CLOSED && h2c->dsi) {
Willy Tarreau3ad5d312019-01-29 18:33:26 +01002360 if (!(h2c->flags & H2_CF_IS_BACK) && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002361 /* #5.1.1: The identifier of a newly
2362 * established stream MUST be numerically
2363 * greater than all streams that the initiating
2364 * endpoint has opened or reserved. This
2365 * governs streams that are opened using a
2366 * HEADERS frame and streams that are reserved
2367 * using PUSH_PROMISE. An endpoint that
2368 * receives an unexpected stream identifier
2369 * MUST respond with a connection error.
2370 */
2371 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2372 goto strm_err;
2373 }
2374
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002375 if (h2s->flags & H2_SF_RST_RCVD && h2_ft_bit(h2c->dft) & H2_FT_HDR_MASK) {
Willy Tarreauab837502017-12-27 15:07:30 +01002376 /* RFC7540#5.1:closed: an endpoint that
2377 * receives any frame other than PRIORITY after
2378 * receiving a RST_STREAM MUST treat that as a
2379 * stream error of type STREAM_CLOSED.
2380 *
2381 * Note that old streams fall into this category
2382 * and will lead to an RST being sent.
Willy Tarreau8d9ac3e2019-01-30 16:58:30 +01002383 *
2384 * However, we cannot generalize this to all frame types. Those
2385 * carrying compression state must still be processed before
2386 * being dropped or we'll desynchronize the decoder. This can
2387 * happen with request trailers received after sending an
2388 * RST_STREAM, or with header/trailers responses received after
2389 * sending RST_STREAM (aborted stream).
Willy Tarreauab837502017-12-27 15:07:30 +01002390 */
2391 h2s_error(h2s, H2_ERR_STREAM_CLOSED);
2392 h2c->st0 = H2_CS_FRAME_E;
2393 goto strm_err;
2394 }
2395
2396 /* RFC7540#5.1:closed: if this state is reached as a
2397 * result of sending a RST_STREAM frame, the peer that
2398 * receives the RST_STREAM might have already sent
2399 * frames on the stream that cannot be withdrawn. An
2400 * endpoint MUST ignore frames that it receives on
2401 * closed streams after it has sent a RST_STREAM
2402 * frame. An endpoint MAY choose to limit the period
2403 * over which it ignores frames and treat frames that
2404 * arrive after this time as being in error.
2405 */
Willy Tarreau24ff1f82019-01-30 19:20:09 +01002406 if (h2s->id && !(h2s->flags & H2_SF_RST_SENT)) {
Willy Tarreauab837502017-12-27 15:07:30 +01002407 /* RFC7540#5.1:closed: any frame other than
2408 * PRIO/WU/RST in this state MUST be treated as
2409 * a connection error
2410 */
2411 if (h2c->dft != H2_FT_RST_STREAM &&
2412 h2c->dft != H2_FT_PRIORITY &&
2413 h2c->dft != H2_FT_WINDOW_UPDATE) {
2414 h2c_error(h2c, H2_ERR_STREAM_CLOSED);
2415 goto strm_err;
2416 }
2417 }
2418 }
2419
Willy Tarreauc0da1962017-10-30 18:38:00 +01002420#if 0
2421 // problem below: it is not possible to completely ignore such
2422 // streams as we need to maintain the compression state as well
2423 // and for this we need to completely process these frames (eg:
2424 // HEADERS frames) as well as counting DATA frames to emit
2425 // proper WINDOW UPDATES and ensure the connection doesn't stall.
2426 // This is a typical case of layer violation where the
2427 // transported contents are critical to the connection's
2428 // validity and must be ignored at the same time :-(
2429
2430 /* graceful shutdown, ignore streams whose ID is higher than
2431 * the one advertised in GOAWAY. RFC7540#6.8.
2432 */
2433 if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002434 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2435 b_del(&h2c->dbuf, ret);
Willy Tarreauc0da1962017-10-30 18:38:00 +01002436 h2c->dfl -= ret;
2437 ret = h2c->dfl == 0;
2438 goto strm_err;
2439 }
2440#endif
2441
Willy Tarreau7e98c052017-10-10 15:56:59 +02002442 switch (h2c->dft) {
Willy Tarreau3421aba2017-07-27 15:41:03 +02002443 case H2_FT_SETTINGS:
2444 if (h2c->st0 == H2_CS_FRAME_P)
2445 ret = h2c_handle_settings(h2c);
2446
2447 if (h2c->st0 == H2_CS_FRAME_A)
2448 ret = h2c_ack_settings(h2c);
2449 break;
2450
Willy Tarreaucf68c782017-10-10 17:11:41 +02002451 case H2_FT_PING:
2452 if (h2c->st0 == H2_CS_FRAME_P)
2453 ret = h2c_handle_ping(h2c);
2454
2455 if (h2c->st0 == H2_CS_FRAME_A)
2456 ret = h2c_ack_ping(h2c);
2457 break;
2458
Willy Tarreau26f95952017-07-27 17:18:30 +02002459 case H2_FT_WINDOW_UPDATE:
2460 if (h2c->st0 == H2_CS_FRAME_P)
2461 ret = h2c_handle_window_update(h2c, h2s);
2462 break;
2463
Willy Tarreau61290ec2017-10-17 08:19:21 +02002464 case H2_FT_CONTINUATION:
Willy Tarreauea18f862018-12-22 20:19:26 +01002465 /* RFC7540#6.10: CONTINUATION may only be preceeded by
2466 * a HEADERS/PUSH_PROMISE/CONTINUATION frame. These
2467 * frames' parsers consume all following CONTINUATION
2468 * frames so this one is out of sequence.
Willy Tarreau61290ec2017-10-17 08:19:21 +02002469 */
Willy Tarreauea18f862018-12-22 20:19:26 +01002470 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
2471 sess_log(h2c->conn->owner);
2472 goto fail;
Willy Tarreau61290ec2017-10-17 08:19:21 +02002473
Willy Tarreau13278b42017-10-13 19:23:14 +02002474 case H2_FT_HEADERS:
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002475 if (h2c->st0 == H2_CS_FRAME_P) {
Willy Tarreauc12f38f2018-10-08 14:53:27 +02002476 if (h2c->flags & H2_CF_IS_BACK)
2477 tmp_h2s = h2c_bck_handle_headers(h2c, h2s);
2478 else
2479 tmp_h2s = h2c_frt_handle_headers(h2c, h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002480 if (tmp_h2s) {
2481 h2s = tmp_h2s;
2482 ret = 1;
2483 }
2484 }
Willy Tarreau13278b42017-10-13 19:23:14 +02002485 break;
2486
Willy Tarreau454f9052017-10-26 19:40:35 +02002487 case H2_FT_DATA:
2488 if (h2c->st0 == H2_CS_FRAME_P)
2489 ret = h2c_frt_handle_data(h2c, h2s);
2490
2491 if (h2c->st0 == H2_CS_FRAME_A)
2492 ret = h2c_send_strm_wu(h2c);
2493 break;
Willy Tarreaucd234e92017-08-18 10:59:39 +02002494
Willy Tarreau92153fc2017-12-03 19:46:19 +01002495 case H2_FT_PRIORITY:
2496 if (h2c->st0 == H2_CS_FRAME_P)
2497 ret = h2c_handle_priority(h2c);
2498 break;
2499
Willy Tarreaucd234e92017-08-18 10:59:39 +02002500 case H2_FT_RST_STREAM:
2501 if (h2c->st0 == H2_CS_FRAME_P)
2502 ret = h2c_handle_rst_stream(h2c, h2s);
2503 break;
2504
Willy Tarreaue96b0922017-10-30 00:28:29 +01002505 case H2_FT_GOAWAY:
2506 if (h2c->st0 == H2_CS_FRAME_P)
2507 ret = h2c_handle_goaway(h2c);
2508 break;
2509
Willy Tarreau1c661982017-10-30 13:52:01 +01002510 /* implement all extra frame types here */
Willy Tarreau7e98c052017-10-10 15:56:59 +02002511 default:
2512 /* drop frames that we ignore. They may be larger than
2513 * the buffer so we drain all of their contents until
2514 * we reach the end.
2515 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002516 ret = MIN(b_data(&h2c->dbuf), h2c->dfl);
2517 b_del(&h2c->dbuf, ret);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002518 h2c->dfl -= ret;
2519 ret = h2c->dfl == 0;
2520 }
2521
Willy Tarreauf182a9a2017-10-30 12:03:50 +01002522 strm_err:
Willy Tarreaua20a5192017-12-27 11:02:06 +01002523 /* We may have to send an RST if not done yet */
2524 if (h2s->st == H2_SS_ERROR)
2525 h2c->st0 = H2_CS_FRAME_E;
Willy Tarreau27a84c92017-10-17 08:10:17 +02002526
Willy Tarreaua20a5192017-12-27 11:02:06 +01002527 if (h2c->st0 == H2_CS_FRAME_E)
2528 ret = h2c_send_rst_stream(h2c, h2s);
Willy Tarreau27a84c92017-10-17 08:10:17 +02002529
Willy Tarreau7e98c052017-10-10 15:56:59 +02002530 /* error or missing data condition met above ? */
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002531 if (ret <= 0)
Willy Tarreau7e98c052017-10-10 15:56:59 +02002532 break;
2533
2534 if (h2c->st0 != H2_CS_FRAME_H) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002535 b_del(&h2c->dbuf, h2c->dfl);
Willy Tarreau7e98c052017-10-10 15:56:59 +02002536 h2c->st0 = H2_CS_FRAME_H;
2537 }
2538 }
Willy Tarreau52eed752017-09-22 15:05:09 +02002539
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002540 if (h2c->rcvd_c > 0 &&
2541 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)))
2542 h2c_send_conn_wu(h2c);
2543
Willy Tarreau52eed752017-09-22 15:05:09 +02002544 fail:
2545 /* we can go here on missing data, blocked response or error */
Willy Tarreau567beb82018-12-18 16:52:44 +01002546 if (h2s && h2s->cs &&
2547 (b_data(&h2s->rxbuf) ||
Christopher Faulet63768a62019-03-22 14:05:52 +01002548 (h2s->cs->flags & (CS_FL_ERROR|CS_FL_ERR_PENDING|CS_FL_EOS|CS_FL_REOS|CS_FL_EOI)))) {
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002549 /* we may have to signal the upper layers */
2550 h2s->cs->flags |= CS_FL_RCV_MORE;
Willy Tarreau7e094452018-12-19 18:08:52 +01002551 h2s_notify_recv(h2s);
Willy Tarreau2a761dc2018-02-26 18:50:57 +01002552 }
Willy Tarreau1ed87b72018-11-25 08:45:16 +01002553
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02002554 h2c_restart_reading(h2c, 0);
Willy Tarreaubc933932017-10-09 16:21:43 +02002555}
2556
2557/* process Tx frames from streams to be multiplexed. Returns > 0 if it reached
2558 * the end.
2559 */
2560static int h2_process_mux(struct h2c *h2c)
2561{
Olivier Houchard998410a2019-04-15 19:23:37 +02002562 struct h2s *h2s, *h2s_back;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002563
Willy Tarreau01b44822018-10-03 14:26:37 +02002564 if (unlikely(h2c->st0 < H2_CS_FRAME_H)) {
2565 if (unlikely(h2c->st0 == H2_CS_PREFACE && (h2c->flags & H2_CF_IS_BACK))) {
2566 if (unlikely(h2c_bck_send_preface(h2c) <= 0)) {
2567 /* RFC7540#3.5: a GOAWAY frame MAY be omitted */
2568 if (h2c->st0 == H2_CS_ERROR) {
2569 h2c->st0 = H2_CS_ERROR2;
2570 sess_log(h2c->conn->owner);
2571 }
2572 goto fail;
2573 }
2574 h2c->st0 = H2_CS_SETTINGS1;
2575 }
2576 /* need to wait for the other side */
Willy Tarreau75a930a2018-12-12 08:03:58 +01002577 if (h2c->st0 < H2_CS_FRAME_H)
Willy Tarreau01b44822018-10-03 14:26:37 +02002578 return 1;
2579 }
2580
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002581 /* start by sending possibly pending window updates */
2582 if (h2c->rcvd_c > 0 &&
2583 !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) &&
2584 h2c_send_conn_wu(h2c) < 0)
2585 goto fail;
2586
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002587 /* First we always process the flow control list because the streams
2588 * waiting there were already elected for immediate emission but were
2589 * blocked just on this.
2590 */
2591
Olivier Houchard998410a2019-04-15 19:23:37 +02002592 list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002593 if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY ||
2594 h2c->st0 >= H2_CS_ERROR)
2595 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002596
2597 if (!LIST_ISEMPTY(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002598 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002599
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002600 h2s->flags &= ~H2_SF_BLK_ANY;
Olivier Houchard998410a2019-04-15 19:23:37 +02002601 /* For some reason, the upper layer failed to subsribe again,
2602 * so remove it from the send_list
2603 */
2604 if (!h2s->send_wait) {
2605 LIST_DEL_INIT(&h2s->list);
2606 continue;
2607 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002608 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002609 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002610 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002611 }
2612
Olivier Houchard998410a2019-04-15 19:23:37 +02002613 list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) {
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002614 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2615 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002616
2617 if (!LIST_ISEMPTY(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002618 continue;
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002619
Olivier Houchard998410a2019-04-15 19:23:37 +02002620 /* For some reason, the upper layer failed to subsribe again,
2621 * so remove it from the send_list
2622 */
2623 if (!h2s->send_wait) {
2624 LIST_DEL_INIT(&h2s->list);
2625 continue;
2626 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002627 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002628 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardd360ac62019-03-22 17:37:16 +01002629 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002630 tasklet_wakeup(h2s->send_wait->task);
Willy Tarreaubacdf5a2017-10-17 10:57:04 +02002631 }
2632
Willy Tarreaucc0b8c32017-10-26 16:55:59 +02002633 fail:
Willy Tarreau3eabe9b2017-11-07 11:03:01 +01002634 if (unlikely(h2c->st0 >= H2_CS_ERROR)) {
Willy Tarreau081d4722017-05-16 21:51:05 +02002635 if (h2c->st0 == H2_CS_ERROR) {
2636 if (h2c->max_id >= 0) {
2637 h2c_send_goaway_error(h2c, NULL);
2638 if (h2c->flags & H2_CF_MUX_BLOCK_ANY)
2639 return 0;
2640 }
2641
2642 h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) !
2643 }
2644 return 1;
2645 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002646 return (1);
Willy Tarreaubc933932017-10-09 16:21:43 +02002647}
2648
Willy Tarreau62f52692017-10-08 23:01:42 +02002649
Willy Tarreau479998a2018-11-18 06:30:59 +01002650/* Attempt to read data, and subscribe if none available.
2651 * The function returns 1 if data has been received, otherwise zero.
2652 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002653static int h2_recv(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002654{
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002655 struct connection *conn = h2c->conn;
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002656 struct buffer *buf;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002657 int max;
Olivier Houchard7505f942018-08-21 18:10:44 +02002658 size_t ret;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002659
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002660 if (h2c->wait_event.events & SUB_RETRY_RECV)
Olivier Houchard81a15af2018-10-19 17:26:49 +02002661 return (b_data(&h2c->dbuf));
Olivier Houchardaf4021e2018-08-09 13:06:55 +02002662
Willy Tarreau315d8072017-12-10 22:17:57 +01002663 if (!h2_recv_allowed(h2c))
Olivier Houchard81a15af2018-10-19 17:26:49 +02002664 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002665
Willy Tarreau44e973f2018-03-01 17:49:30 +01002666 buf = h2_get_buf(h2c, &h2c->dbuf);
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002667 if (!buf) {
2668 h2c->flags |= H2_CF_DEM_DALLOC;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002669 return 0;
Willy Tarreau1b62c5c2017-09-25 11:55:01 +02002670 }
Willy Tarreau35dbd5d2017-09-22 09:13:49 +02002671
Olivier Houchard7505f942018-08-21 18:10:44 +02002672 do {
Willy Tarreaue0f24ee2018-12-14 10:51:23 +01002673 b_realign_if_empty(buf);
Willy Tarreau2a59e872018-12-12 08:23:47 +01002674 if (!b_data(buf) && (h2c->proxy->options2 & PR_O2_USE_HTX)) {
2675 /* HTX in use : try to pre-align the buffer like the
2676 * rxbufs will be to optimize memory copies. We'll make
2677 * sure that the frame header lands at the end of the
2678 * HTX block to alias it upon recv. We cannot use the
2679 * head because rcv_buf() will realign the buffer if
2680 * it's empty. Thus we cheat and pretend we already
2681 * have a few bytes there.
2682 */
2683 max = buf_room_for_htx_data(buf) + 9;
Willy Tarreauc0960d12018-12-14 10:59:15 +01002684 buf->head = sizeof(struct htx) - 9;
Willy Tarreau2a59e872018-12-12 08:23:47 +01002685 }
2686 else
2687 max = b_room(buf);
2688
Olivier Houchard7505f942018-08-21 18:10:44 +02002689 if (max)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002690 ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, buf, max, 0);
Olivier Houchard7505f942018-08-21 18:10:44 +02002691 else
2692 ret = 0;
2693 } while (ret > 0);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002694
Olivier Houchard53216e72018-10-10 15:46:36 +02002695 if (h2_recv_allowed(h2c) && (b_data(buf) < buf->size))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002696 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h2c->wait_event);
Olivier Houchard81a15af2018-10-19 17:26:49 +02002697
Olivier Houcharda1411e62018-08-17 18:42:48 +02002698 if (!b_data(buf)) {
Willy Tarreau44e973f2018-03-01 17:49:30 +01002699 h2_release_buf(h2c, &h2c->dbuf);
Olivier Houchard46677732018-11-29 17:06:17 +01002700 return (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn));
Willy Tarreaua2af5122017-10-09 11:56:46 +02002701 }
2702
Willy Tarreaub7b5fe12018-06-18 13:33:09 +02002703 if (b_data(buf) == buf->size)
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002704 h2c->flags |= H2_CF_DEM_DFULL;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002705 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02002706}
2707
Willy Tarreau479998a2018-11-18 06:30:59 +01002708/* Try to send data if possible.
2709 * The function returns 1 if data have been sent, otherwise zero.
2710 */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002711static int h2_send(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002712{
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002713 struct connection *conn = h2c->conn;
Willy Tarreaubc933932017-10-09 16:21:43 +02002714 int done;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002715 int sent = 0;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002716
2717 if (conn->flags & CO_FL_ERROR)
Olivier Houchard7c6f8b12018-11-13 16:48:36 +01002718 return 1;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002719
Olivier Houchard7505f942018-08-21 18:10:44 +02002720
Willy Tarreaua2af5122017-10-09 11:56:46 +02002721 if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) {
2722 /* a handshake was requested */
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002723 goto schedule;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002724 }
2725
Willy Tarreaubc933932017-10-09 16:21:43 +02002726 /* This loop is quite simple : it tries to fill as much as it can from
2727 * pending streams into the existing buffer until it's reportedly full
2728 * or the end of send requests is reached. Then it tries to send this
2729 * buffer's contents out, marks it not full if at least one byte could
2730 * be sent, and tries again.
2731 *
2732 * The snd_buf() function normally takes a "flags" argument which may
2733 * be made of a combination of CO_SFL_MSG_MORE to indicate that more
2734 * data immediately comes and CO_SFL_STREAMER to indicate that the
2735 * connection is streaming lots of data (used to increase TLS record
2736 * size at the expense of latency). The former can be sent any time
2737 * there's a buffer full flag, as it indicates at least one stream
2738 * attempted to send and failed so there are pending data. An
2739 * alternative would be to set it as long as there's an active stream
2740 * but that would be problematic for ACKs until we have an absolute
2741 * guarantee that all waiters have at least one byte to send. The
2742 * latter should possibly not be set for now.
2743 */
2744
2745 done = 0;
2746 while (!done) {
2747 unsigned int flags = 0;
2748
2749 /* fill as much as we can into the current buffer */
2750 while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done)
2751 done = h2_process_mux(h2c);
2752
Olivier Houchard2b094432019-01-29 18:28:36 +01002753 if (h2c->flags & H2_CF_MUX_MALLOC)
2754 break;
2755
Willy Tarreaubc933932017-10-09 16:21:43 +02002756 if (conn->flags & CO_FL_ERROR)
2757 break;
2758
2759 if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))
2760 flags |= CO_SFL_MSG_MORE;
2761
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002762 if (b_data(&h2c->mbuf)) {
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002763 int ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h2c->mbuf, b_data(&h2c->mbuf), flags);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002764 if (!ret)
2765 break;
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002766 sent = 1;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002767 b_del(&h2c->mbuf, ret);
2768 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002769 }
Willy Tarreaubc933932017-10-09 16:21:43 +02002770
2771 /* wrote at least one byte, the buffer is not full anymore */
2772 h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM);
2773 }
2774
Willy Tarreaua2af5122017-10-09 11:56:46 +02002775 if (conn->flags & CO_FL_SOCK_WR_SH) {
2776 /* output closed, nothing to send, clear the buffer to release it */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002777 b_reset(&h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002778 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002779 /* We're not full anymore, so we can wake any task that are waiting
2780 * for us.
2781 */
2782 if (!(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MROOM))) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01002783 struct h2s *h2s;
2784
2785 list_for_each_entry(h2s, &h2c->send_list, list) {
2786 if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY)
2787 break;
Olivier Houchard998410a2019-04-15 19:23:37 +02002788
2789 if (!LIST_ISEMPTY(&h2s->sending_list))
Olivier Houchardd360ac62019-03-22 17:37:16 +01002790 continue;
2791
Olivier Houchard998410a2019-04-15 19:23:37 +02002792 /* For some reason, the upper layer failed to subsribe again,
2793 * so remove it from the send_list
2794 */
2795 if (!h2s->send_wait) {
2796 LIST_DEL_INIT(&h2s->list);
2797 continue;
2798 }
Olivier Houchardd360ac62019-03-22 17:37:16 +01002799 h2s->flags &= ~H2_SF_BLK_ANY;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002800 h2s->send_wait->events &= ~SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02002801 tasklet_wakeup(h2s->send_wait->task);
Olivier Houchardd360ac62019-03-22 17:37:16 +01002802 LIST_ADDQ(&h2c->sending_list, &h2s->sending_list);
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02002803 }
Olivier Houchard6ff20392018-07-17 18:46:31 +02002804 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002805 /* We're done, no more to send */
2806 if (!b_data(&h2c->mbuf))
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002807 return sent;
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002808schedule:
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002809 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002810 conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h2c->wait_event);
Olivier Houchardd4dd22d2018-08-17 18:39:46 +02002811 return sent;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002812}
2813
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002814/* this is the tasklet referenced in h2c->wait_event.task */
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002815static struct task *h2_io_cb(struct task *t, void *ctx, unsigned short status)
2816{
2817 struct h2c *h2c = ctx;
Olivier Houchard7505f942018-08-21 18:10:44 +02002818 int ret = 0;
Olivier Houchard29fb89d2018-08-02 18:56:36 +02002819
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002820 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard7505f942018-08-21 18:10:44 +02002821 ret = h2_send(h2c);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01002822 if (!(h2c->wait_event.events & SUB_RETRY_RECV))
Olivier Houchard7505f942018-08-21 18:10:44 +02002823 ret |= h2_recv(h2c);
Willy Tarreaucef5c8e2018-12-18 10:29:54 +01002824 if (ret || b_data(&h2c->dbuf))
Olivier Houchard7505f942018-08-21 18:10:44 +02002825 h2_process(h2c);
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002826 return NULL;
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002827}
Willy Tarreaua2af5122017-10-09 11:56:46 +02002828
Willy Tarreau62f52692017-10-08 23:01:42 +02002829/* callback called on any event by the connection handler.
2830 * It applies changes and returns zero, or < 0 if it wants immediate
2831 * destruction of the connection (which normally doesn not happen in h2).
2832 */
Olivier Houchard7505f942018-08-21 18:10:44 +02002833static int h2_process(struct h2c *h2c)
Willy Tarreau62f52692017-10-08 23:01:42 +02002834{
Olivier Houchard7505f942018-08-21 18:10:44 +02002835 struct connection *conn = h2c->conn;
Willy Tarreaua2af5122017-10-09 11:56:46 +02002836
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002837 if (b_data(&h2c->dbuf) && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) {
Willy Tarreaud13bf272017-12-14 10:34:52 +01002838 h2_process_demux(h2c);
2839
2840 if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR)
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002841 b_reset(&h2c->dbuf);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002842
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002843 if (!b_full(&h2c->dbuf))
Willy Tarreaud13bf272017-12-14 10:34:52 +01002844 h2c->flags &= ~H2_CF_DEM_DFULL;
2845 }
Olivier Houchard7505f942018-08-21 18:10:44 +02002846 h2_send(h2c);
Willy Tarreaud13bf272017-12-14 10:34:52 +01002847
Willy Tarreau0b37d652018-10-03 10:33:02 +02002848 if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
Willy Tarreau8ec14062017-12-30 18:08:13 +01002849 /* frontend is stopping, reload likely in progress, let's try
2850 * to announce a graceful shutdown if not yet done. We don't
2851 * care if it fails, it will be tried again later.
2852 */
2853 if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
2854 if (h2c->last_sid < 0)
2855 h2c->last_sid = (1U << 31) - 1;
2856 h2c_send_goaway_error(h2c, NULL);
2857 }
2858 }
2859
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002860 /*
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002861 * If we received early data, and the handshake is done, wake
2862 * any stream that was waiting for it.
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002863 */
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002864 if (!(h2c->flags & H2_CF_WAIT_FOR_HS) &&
2865 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) {
2866 struct eb32_node *node;
2867 struct h2s *h2s;
2868
2869 h2c->flags |= H2_CF_WAIT_FOR_HS;
2870 node = eb32_lookup_ge(&h2c->streams_by_id, 1);
2871
2872 while (node) {
2873 h2s = container_of(node, struct h2s, by_id);
Willy Tarreaufde287c2018-12-19 18:33:16 +01002874 if (h2s->cs && h2s->cs->flags & CS_FL_WAIT_FOR_HS)
Willy Tarreau7e094452018-12-19 18:08:52 +01002875 h2s_notify_recv(h2s);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002876 node = eb32_next(node);
2877 }
Olivier Houchard7fc96d52017-11-23 18:25:47 +01002878 }
Olivier Houchard6fa63d92017-11-27 18:41:32 +01002879
Willy Tarreau26bd7612017-10-09 16:47:04 +02002880 if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) ||
Willy Tarreau29a98242017-10-31 06:59:15 +01002881 h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED ||
2882 (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 &&
2883 h2c->max_id >= h2c->last_sid)) {
Willy Tarreau23b92aa2017-10-30 00:26:54 +01002884 h2_wake_some_streams(h2c, 0, 0);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002885
2886 if (eb_is_empty(&h2c->streams_by_id)) {
2887 /* no more stream, kill the connection now */
Christopher Faulet73c12072019-04-08 11:23:22 +02002888 h2_release(h2c);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002889 return -1;
2890 }
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002891 }
2892
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002893 if (!b_data(&h2c->dbuf))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002894 h2_release_buf(h2c, &h2c->dbuf);
Willy Tarreaufbe3b4f2017-10-09 15:14:19 +02002895
Olivier Houchard53216e72018-10-10 15:46:36 +02002896 if ((conn->flags & CO_FL_SOCK_WR_SH) ||
2897 h2c->st0 == H2_CS_ERROR2 || (h2c->flags & H2_CF_GOAWAY_FAILED) ||
2898 (h2c->st0 != H2_CS_ERROR &&
2899 !b_data(&h2c->mbuf) &&
2900 (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) &&
2901 ((h2c->flags & H2_CF_MUX_BLOCK_ANY) || LIST_ISEMPTY(&h2c->send_list))))
Willy Tarreau44e973f2018-03-01 17:49:30 +01002902 h2_release_buf(h2c, &h2c->mbuf);
Willy Tarreaua2af5122017-10-09 11:56:46 +02002903
Willy Tarreau3f133572017-10-31 19:21:06 +01002904 if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002905 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreau599391a2017-11-24 10:16:00 +01002906 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
Willy Tarreau3f133572017-10-31 19:21:06 +01002907 task_queue(h2c->task);
2908 }
2909 else
2910 h2c->task->expire = TICK_ETERNITY;
Willy Tarreauea392822017-10-31 10:02:25 +01002911 }
Olivier Houchard910b2bc2018-07-17 18:49:38 +02002912
Olivier Houchard7505f942018-08-21 18:10:44 +02002913 h2_send(h2c);
Willy Tarreau62f52692017-10-08 23:01:42 +02002914 return 0;
2915}
2916
Willy Tarreau749f5ca2019-03-21 19:19:36 +01002917/* wake-up function called by the connection layer (mux_ops.wake) */
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002918static int h2_wake(struct connection *conn)
2919{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002920 struct h2c *h2c = conn->ctx;
Olivier Houchard21df6cc2018-09-14 23:21:44 +02002921
2922 return (h2_process(h2c));
2923}
2924
Willy Tarreauea392822017-10-31 10:02:25 +01002925/* Connection timeout management. The principle is that if there's no receipt
2926 * nor sending for a certain amount of time, the connection is closed. If the
2927 * MUX buffer still has lying data or is not allocatable, the connection is
2928 * immediately killed. If it's allocatable and empty, we attempt to send a
2929 * GOAWAY frame.
2930 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02002931static struct task *h2_timeout_task(struct task *t, void *context, unsigned short state)
Willy Tarreauea392822017-10-31 10:02:25 +01002932{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002933 struct h2c *h2c = context;
Willy Tarreauea392822017-10-31 10:02:25 +01002934 int expired = tick_is_expired(t->expire, now_ms);
2935
Willy Tarreau0975f112018-03-29 15:22:59 +02002936 if (!expired && h2c)
Willy Tarreauea392822017-10-31 10:02:25 +01002937 return t;
2938
Olivier Houchard3f795f72019-04-17 22:51:06 +02002939 task_destroy(t);
Willy Tarreau0975f112018-03-29 15:22:59 +02002940
2941 if (!h2c) {
2942 /* resources were already deleted */
2943 return NULL;
2944 }
2945
2946 h2c->task = NULL;
Willy Tarreauea392822017-10-31 10:02:25 +01002947 h2c_error(h2c, H2_ERR_NO_ERROR);
2948 h2_wake_some_streams(h2c, 0, 0);
2949
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002950 if (b_data(&h2c->mbuf)) {
Willy Tarreauea392822017-10-31 10:02:25 +01002951 /* don't even try to send a GOAWAY, the buffer is stuck */
2952 h2c->flags |= H2_CF_GOAWAY_FAILED;
2953 }
2954
2955 /* try to send but no need to insist */
Willy Tarreau599391a2017-11-24 10:16:00 +01002956 h2c->last_sid = h2c->max_id;
Willy Tarreauea392822017-10-31 10:02:25 +01002957 if (h2c_send_goaway_error(h2c, NULL) <= 0)
2958 h2c->flags |= H2_CF_GOAWAY_FAILED;
2959
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002960 if (b_data(&h2c->mbuf) && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) {
Olivier Houcharde179d0e2019-03-21 18:27:17 +01002961 int ret = h2c->conn->xprt->snd_buf(h2c->conn, h2c->conn->xprt_ctx, &h2c->mbuf, b_data(&h2c->mbuf), 0);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002962 if (ret > 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002963 b_del(&h2c->mbuf, ret);
2964 b_realign_if_empty(&h2c->mbuf);
Willy Tarreau787db9a2018-06-14 18:31:46 +02002965 }
2966 }
Willy Tarreauea392822017-10-31 10:02:25 +01002967
Willy Tarreau0975f112018-03-29 15:22:59 +02002968 /* either we can release everything now or it will be done later once
2969 * the last stream closes.
2970 */
2971 if (eb_is_empty(&h2c->streams_by_id))
Christopher Faulet73c12072019-04-08 11:23:22 +02002972 h2_release(h2c);
Willy Tarreauea392822017-10-31 10:02:25 +01002973
Willy Tarreauea392822017-10-31 10:02:25 +01002974 return NULL;
2975}
2976
2977
Willy Tarreau62f52692017-10-08 23:01:42 +02002978/*******************************************/
2979/* functions below are used by the streams */
2980/*******************************************/
2981
2982/*
2983 * Attach a new stream to a connection
2984 * (Used for outgoing connections)
2985 */
Olivier Houchardf502aca2018-12-14 19:42:40 +01002986static struct conn_stream *h2_attach(struct connection *conn, struct session *sess)
Willy Tarreau62f52692017-10-08 23:01:42 +02002987{
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002988 struct conn_stream *cs;
2989 struct h2s *h2s;
Willy Tarreau3d2ee552018-12-19 14:12:10 +01002990 struct h2c *h2c = conn->ctx;
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002991
2992 cs = cs_new(conn);
2993 if (!cs)
2994 return NULL;
Olivier Houchardf502aca2018-12-14 19:42:40 +01002995 h2s = h2c_bck_stream_new(h2c, cs, sess);
Olivier Houchard7a57e8a2018-11-27 17:36:33 +01002996 if (!h2s) {
2997 cs_free(cs);
2998 return NULL;
2999 }
3000 return cs;
Willy Tarreau62f52692017-10-08 23:01:42 +02003001}
3002
Willy Tarreaufafd3982018-11-18 21:29:20 +01003003/* Retrieves the first valid conn_stream from this connection, or returns NULL.
3004 * We have to scan because we may have some orphan streams. It might be
3005 * beneficial to scan backwards from the end to reduce the likeliness to find
3006 * orphans.
3007 */
3008static const struct conn_stream *h2_get_first_cs(const struct connection *conn)
3009{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01003010 struct h2c *h2c = conn->ctx;
Willy Tarreaufafd3982018-11-18 21:29:20 +01003011 struct h2s *h2s;
3012 struct eb32_node *node;
3013
3014 node = eb32_first(&h2c->streams_by_id);
3015 while (node) {
3016 h2s = container_of(node, struct h2s, by_id);
3017 if (h2s->cs)
3018 return h2s->cs;
3019 node = eb32_next(node);
3020 }
3021 return NULL;
3022}
3023
Willy Tarreau62f52692017-10-08 23:01:42 +02003024/*
Olivier Houchard060ed432018-11-06 16:32:42 +01003025 * Destroy the mux and the associated connection, if it is no longer used
3026 */
Christopher Faulet73c12072019-04-08 11:23:22 +02003027static void h2_destroy(void *ctx)
Olivier Houchard060ed432018-11-06 16:32:42 +01003028{
Christopher Faulet73c12072019-04-08 11:23:22 +02003029 struct h2c *h2c = ctx;
Olivier Houchard060ed432018-11-06 16:32:42 +01003030
Christopher Faulet39a96ee2019-04-08 10:52:21 +02003031 if (eb_is_empty(&h2c->streams_by_id) || !h2c->conn || h2c->conn->ctx != h2c)
Christopher Faulet73c12072019-04-08 11:23:22 +02003032 h2_release(h2c);
Olivier Houchard060ed432018-11-06 16:32:42 +01003033}
3034
3035/*
Willy Tarreau62f52692017-10-08 23:01:42 +02003036 * Detach the stream from the connection and possibly release the connection.
3037 */
3038static void h2_detach(struct conn_stream *cs)
3039{
Willy Tarreau60935142017-10-16 18:11:19 +02003040 struct h2s *h2s = cs->ctx;
3041 struct h2c *h2c;
Olivier Houchardf502aca2018-12-14 19:42:40 +01003042 struct session *sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003043
3044 cs->ctx = NULL;
3045 if (!h2s)
3046 return;
3047
Olivier Houchard998410a2019-04-15 19:23:37 +02003048 /* The stream is about to die, so no need to attempt to run its task */
3049 if (!LIST_ISEMPTY(&h2s->sending_list) &&
3050 h2s->send_wait != &h2s->wait_event) {
3051 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
3052 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd9986ed2019-05-09 13:24:08 +02003053 /*
3054 * At this point, the stream_interface is supposed to have called
3055 * h2_unsubscribe(), so the only way there's still a
3056 * subscription that came from the stream_interface (as we
3057 * can subscribe ourself, in h2_do_shutw() and h2_do_shutr(),
3058 * without the stream_interface involved) is that we subscribed
3059 * for sending, we woke the tasklet up and removed the
3060 * SUB_RETRY_SEND flag, so the stream_interface would not
3061 * know it has to unsubscribe for send, but the tasklet hasn't
3062 * run yet. Make sure to handle that by explicitely setting
3063 * send_wait to NULL, as nothing else will do it for us.
3064 */
3065 h2s->send_wait = NULL;
Olivier Houchard998410a2019-04-15 19:23:37 +02003066 }
3067
Olivier Houchardf502aca2018-12-14 19:42:40 +01003068 sess = h2s->sess;
Willy Tarreau60935142017-10-16 18:11:19 +02003069 h2c = h2s->h2c;
3070 h2s->cs = NULL;
Willy Tarreau7ac60e82018-07-19 09:04:05 +02003071 h2c->nb_cs--;
Willy Tarreaufa1d3572019-01-31 10:31:51 +01003072 if ((h2c->flags & (H2_CF_IS_BACK|H2_CF_DEM_TOOMANY)) == H2_CF_DEM_TOOMANY &&
3073 !h2_frt_has_too_many_cs(h2c)) {
3074 /* frontend connection was blocking new streams creation */
Willy Tarreauf2101912018-07-19 10:11:38 +02003075 h2c->flags &= ~H2_CF_DEM_TOOMANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003076 h2c_restart_reading(h2c, 1);
Willy Tarreauf2101912018-07-19 10:11:38 +02003077 }
Willy Tarreau60935142017-10-16 18:11:19 +02003078
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003079 /* this stream may be blocked waiting for some data to leave (possibly
3080 * an ES or RST frame), so orphan it in this case.
3081 */
Willy Tarreau3041fcc2018-03-29 15:41:32 +02003082 if (!(cs->conn->flags & CO_FL_ERROR) &&
Willy Tarreaua2b51812018-07-27 09:55:14 +02003083 (h2c->st0 < H2_CS_ERROR) &&
Olivier Houchard16ff2612019-03-21 15:48:46 +01003084 (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)) && (h2s->send_wait || h2s->recv_wait))
Willy Tarreau22cf59b2017-11-10 11:42:33 +01003085 return;
3086
Willy Tarreau45f752e2017-10-30 15:44:59 +01003087 if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) ||
3088 (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) {
3089 /* unblock the connection if it was blocked on this
3090 * stream.
3091 */
3092 h2c->flags &= ~H2_CF_DEM_BLOCK_ANY;
3093 h2c->flags &= ~H2_CF_MUX_BLOCK_ANY;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02003094 h2c_restart_reading(h2c, 1);
Willy Tarreau45f752e2017-10-30 15:44:59 +01003095 }
3096
Willy Tarreau71049cc2018-03-28 13:56:39 +02003097 h2s_destroy(h2s);
Willy Tarreau60935142017-10-16 18:11:19 +02003098
Olivier Houchard8a786902018-12-15 16:05:40 +01003099 if (h2c->flags & H2_CF_IS_BACK &&
3100 (h2c->proxy->options2 & PR_O2_USE_HTX)) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003101 if (!(h2c->conn->flags &
3102 (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))) {
3103 if (!h2c->conn->owner) {
Olivier Houchardf502aca2018-12-14 19:42:40 +01003104 h2c->conn->owner = sess;
Olivier Houchard351411f2018-12-27 17:20:54 +01003105 if (!session_add_conn(sess, h2c->conn, h2c->conn->target)) {
3106 h2c->conn->owner = NULL;
3107 if (eb_is_empty(&h2c->streams_by_id)) {
3108 if (!srv_add_to_idle_list(objt_server(h2c->conn->target), h2c->conn))
3109 /* The server doesn't want it, let's kill the connection right away */
3110 h2c->conn->mux->destroy(h2c->conn);
3111 return;
3112 }
3113 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003114 }
Olivier Houcharda4d4fdf2018-12-14 19:27:06 +01003115 if (eb_is_empty(&h2c->streams_by_id)) {
3116 if (session_check_idle_conn(h2c->conn->owner, h2c->conn) != 0)
3117 /* At this point either the connection is destroyed, or it's been added to the server idle list, just stop */
3118 return;
3119 }
Olivier Houchard8a786902018-12-15 16:05:40 +01003120 /* Never ever allow to reuse a connection from a non-reuse backend */
3121 if ((h2c->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_NEVR)
3122 h2c->conn->flags |= CO_FL_PRIVATE;
Willy Tarreau86949782019-01-31 10:42:05 +01003123 if (LIST_ISEMPTY(&h2c->conn->list) && h2c->nb_streams < h2c->streams_limit) {
Olivier Houchard8a786902018-12-15 16:05:40 +01003124 struct server *srv = objt_server(h2c->conn->target);
3125
3126 if (srv) {
3127 if (h2c->conn->flags & CO_FL_PRIVATE)
3128 LIST_ADD(&srv->priv_conns[tid], &h2c->conn->list);
3129 else
3130 LIST_ADD(&srv->idle_conns[tid], &h2c->conn->list);
3131 }
3132
3133 }
3134 }
3135 }
3136
Willy Tarreaue323f342018-03-28 13:51:45 +02003137 /* We don't want to close right now unless we're removing the
3138 * last stream, and either the connection is in error, or it
3139 * reached the ID already specified in a GOAWAY frame received
3140 * or sent (as seen by last_sid >= 0).
3141 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003142 if (h2c_is_dead(h2c)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003143 /* no more stream will come, kill it now */
Christopher Faulet73c12072019-04-08 11:23:22 +02003144 h2_release(h2c);
Willy Tarreaue323f342018-03-28 13:51:45 +02003145 }
3146 else if (h2c->task) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003147 if (eb_is_empty(&h2c->streams_by_id) || b_data(&h2c->mbuf)) {
Willy Tarreaue323f342018-03-28 13:51:45 +02003148 h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout);
3149 task_queue(h2c->task);
Willy Tarreaue6ae77f2017-11-07 11:59:51 +01003150 }
Willy Tarreaue323f342018-03-28 13:51:45 +02003151 else
3152 h2c->task->expire = TICK_ETERNITY;
Willy Tarreau60935142017-10-16 18:11:19 +02003153 }
Willy Tarreau62f52692017-10-08 23:01:42 +02003154}
3155
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003156/* Performs a synchronous or asynchronous shutr().
3157 * FIXME: guess what the return code tries to indicate!
3158 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003159static int h2_do_shutr(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003160{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003161 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003162 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003163
Willy Tarreau721c9742017-11-07 11:05:42 +01003164 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Olivier Houchard7a977432019-03-21 15:47:13 +01003165 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003166
Willy Tarreau18059042019-01-31 19:12:48 +01003167 /* a connstream may require us to immediately kill the whole connection
3168 * for example because of a "tcp-request content reject" rule that is
3169 * normally used to limit abuse. In this case we schedule a goaway to
3170 * close the connection.
Willy Tarreau926fa4c2017-11-07 14:42:12 +01003171 */
Willy Tarreau18059042019-01-31 19:12:48 +01003172 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3173 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3174 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3175 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3176 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003177 else if (!(h2s->flags & H2_SF_HEADERS_SENT)) {
3178 /* Nothing was never sent for this stream, so reset with
3179 * REFUSED_STREAM error to let the client retry the
3180 * request.
3181 */
3182 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3183 }
Willy Tarreau18059042019-01-31 19:12:48 +01003184
Willy Tarreau90c32322017-11-24 08:00:30 +01003185 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003186 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003187 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003188
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003189 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003190 tasklet_wakeup(h2c->wait_event.task);
Willy Tarreau00dd0782018-03-01 16:31:34 +01003191 h2s_close(h2s);
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003192
Olivier Houchard7a977432019-03-21 15:47:13 +01003193 return 0;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003194add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003195 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003196 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003197 if (h2s->flags & H2_SF_BLK_MFCTL) {
3198 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3199 h2s->send_wait = sw;
3200 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3201 h2s->send_wait = sw;
3202 LIST_ADDQ(&h2c->send_list, &h2s->list);
3203 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003204 }
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003205 /* Let the handler know we want shutr */
3206 sw->handle = (void *)((long)sw->handle | 1);
Olivier Houchard7a977432019-03-21 15:47:13 +01003207 return 1;
Willy Tarreau62f52692017-10-08 23:01:42 +02003208}
3209
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003210/* Performs a synchronous or asynchronous shutw().
3211 * FIXME: guess what the return code tries to indicate!
3212 */
Olivier Houchard7a977432019-03-21 15:47:13 +01003213static int h2_do_shutw(struct h2s *h2s)
Willy Tarreau62f52692017-10-08 23:01:42 +02003214{
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003215 struct h2c *h2c = h2s->h2c;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003216 struct wait_event *sw = &h2s->wait_event;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003217
Willy Tarreau721c9742017-11-07 11:05:42 +01003218 if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED)
Olivier Houchard7a977432019-03-21 15:47:13 +01003219 return 0;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003220
Willy Tarreau67434202017-11-06 20:20:51 +01003221 if (h2s->flags & H2_SF_HEADERS_SENT) {
Willy Tarreau58e32082017-11-07 14:41:09 +01003222 /* we can cleanly close using an empty data frame only after headers */
3223
3224 if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) &&
3225 h2_send_empty_data_es(h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003226 goto add_to_list;
Willy Tarreau58e32082017-11-07 14:41:09 +01003227
3228 if (h2s->st == H2_SS_HREM)
Willy Tarreau00dd0782018-03-01 16:31:34 +01003229 h2s_close(h2s);
Willy Tarreau58e32082017-11-07 14:41:09 +01003230 else
3231 h2s->st = H2_SS_HLOC;
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003232 } else {
Willy Tarreau18059042019-01-31 19:12:48 +01003233 /* a connstream may require us to immediately kill the whole connection
3234 * for example because of a "tcp-request content reject" rule that is
3235 * normally used to limit abuse. In this case we schedule a goaway to
3236 * close the connection.
Willy Tarreaua1349f02017-10-31 07:41:55 +01003237 */
Willy Tarreau18059042019-01-31 19:12:48 +01003238 if ((h2s->cs && h2s->cs->flags & CS_FL_KILL_CONN) &&
3239 !(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) {
3240 h2c_error(h2c, H2_ERR_ENHANCE_YOUR_CALM);
3241 h2s_error(h2s, H2_ERR_ENHANCE_YOUR_CALM);
3242 }
Christopher Faulet35757d32019-03-07 15:51:33 +01003243 else {
3244 /* Nothing was never sent for this stream, so reset with
3245 * REFUSED_STREAM error to let the client retry the
3246 * request.
3247 */
3248 h2s_error(h2s, H2_ERR_REFUSED_STREAM);
3249 }
Willy Tarreau18059042019-01-31 19:12:48 +01003250
Willy Tarreau90c32322017-11-24 08:00:30 +01003251 if (!(h2s->flags & H2_SF_RST_SENT) &&
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003252 h2s_send_rst_stream(h2c, h2s) <= 0)
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003253 goto add_to_list;
Willy Tarreau90c32322017-11-24 08:00:30 +01003254
Willy Tarreau00dd0782018-03-01 16:31:34 +01003255 h2s_close(h2s);
Willy Tarreauc7576ea2017-10-29 22:00:09 +01003256 }
3257
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003258 if (!(h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchard435ce2d2018-12-03 18:43:16 +01003259 tasklet_wakeup(h2c->wait_event.task);
Olivier Houchard7a977432019-03-21 15:47:13 +01003260 return 0;
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003261
3262 add_to_list:
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003263 if (LIST_ISEMPTY(&h2s->list)) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01003264 sw->events |= SUB_RETRY_SEND;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003265 if (h2s->flags & H2_SF_BLK_MFCTL) {
3266 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
3267 h2s->send_wait = sw;
3268 } else if (h2s->flags & (H2_SF_BLK_MBUSY|H2_SF_BLK_MROOM)) {
3269 h2s->send_wait = sw;
3270 LIST_ADDQ(&h2c->send_list, &h2s->list);
3271 }
Willy Tarreaub2e290a2018-03-30 17:35:38 +02003272 }
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003273 /* let the handler know we want to shutw */
3274 sw->handle = (void *)((long)(sw->handle) | 2);
Olivier Houchard7a977432019-03-21 15:47:13 +01003275 return 1;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003276}
3277
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003278/* This is the tasklet referenced in h2s->wait_event.task, it is used for
3279 * deferred shutdowns when the h2_detach() was done but the mux buffer was full
3280 * and prevented the last frame from being emitted.
3281 */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003282static struct task *h2_deferred_shut(struct task *t, void *ctx, unsigned short state)
3283{
3284 struct h2s *h2s = ctx;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02003285 long reason = (long)h2s->wait_event.handle;
Olivier Houchard7a977432019-03-21 15:47:13 +01003286 int ret = 0;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003287
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003288 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003289 if (reason & 2)
Olivier Houchard7a977432019-03-21 15:47:13 +01003290 ret |= h2_do_shutw(h2s);
Olivier Houchard2c68a462018-12-15 22:42:20 +01003291 if (reason & 1)
Olivier Houchard7a977432019-03-21 15:47:13 +01003292 ret |= h2_do_shutr(h2s);
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003293
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003294 if (!ret) {
3295 /* We're done trying to send, remove ourself from the send_list */
Olivier Houchardafc7cb82019-03-25 14:08:01 +01003296 LIST_DEL_INIT(&h2s->list);
3297 }
Olivier Houchard7a977432019-03-21 15:47:13 +01003298 /* We're no longer trying to send anything, let's destroy the h2s */
Olivier Houchard01d4cb52019-03-25 13:25:02 +01003299 if (!ret && (h2s->cs == NULL)) {
Olivier Houchard7a977432019-03-21 15:47:13 +01003300 struct h2c *h2c = h2s->h2c;
Olivier Houchard2c68a462018-12-15 22:42:20 +01003301 h2s_destroy(h2s);
Olivier Houchard7a977432019-03-21 15:47:13 +01003302
3303 if (h2c_is_dead(h2c))
Christopher Faulet73c12072019-04-08 11:23:22 +02003304 h2_release(h2c);
Olivier Houchard7a977432019-03-21 15:47:13 +01003305 }
3306
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003307 return NULL;
Willy Tarreau62f52692017-10-08 23:01:42 +02003308}
3309
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003310/* shutr() called by the conn_stream (mux_ops.shutr) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003311static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode)
3312{
3313 struct h2s *h2s = cs->ctx;
3314
3315 if (!mode)
3316 return;
3317
3318 h2_do_shutr(h2s);
3319}
3320
Willy Tarreau749f5ca2019-03-21 19:19:36 +01003321/* shutw() called by the conn_stream (mux_ops.shutw) */
Olivier Houchard8ae735d2018-09-11 18:24:28 +02003322static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode)
3323{
3324 struct h2s *h2s = cs->ctx;
3325
3326 h2_do_shutw(h2s);
3327}
3328
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003329/* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 or
Willy Tarreau86277d42019-01-02 15:36:11 +01003330 * HTX request or response depending on the connection's side. Returns a
3331 * positive value on success, a negative value on failure, or 0 if it couldn't
3332 * proceed. May report connection errors in h2c->errcode if the frame is
3333 * non-decodable and the connection unrecoverable. In absence of connection
3334 * error when a failure is reported, the caller must assume a stream error.
Willy Tarreauea18f862018-12-22 20:19:26 +01003335 *
3336 * The function may fold CONTINUATION frames into the initial HEADERS frame
3337 * by removing padding and next frame header, then moving the CONTINUATION
3338 * frame's payload and adjusting h2c->dfl to match the new aggregated frame,
3339 * leaving a hole between the main frame and the beginning of the next one.
3340 * The possibly remaining incomplete or next frame at the end may be moved
3341 * if the aggregated frame is not deleted, in order to fill the hole. Wrapped
3342 * HEADERS frames are unwrapped into a temporary buffer before decoding.
3343 *
3344 * A buffer at the beginning of processing may look like this :
3345 *
3346 * ,---.---------.-----.--------------.--------------.------.---.
3347 * |///| HEADERS | PAD | CONTINUATION | CONTINUATION | DATA |///|
3348 * `---^---------^-----^--------------^--------------^------^---'
3349 * | | <-----> | |
3350 * area | dpl | wrap
3351 * |<--------------> |
3352 * | dfl |
3353 * |<-------------------------------------------------->|
3354 * head data
3355 *
3356 * Padding is automatically overwritten when folding, participating to the
3357 * hole size after dfl :
3358 *
3359 * ,---.------------------------.-----.--------------.------.---.
3360 * |///| HEADERS : CONTINUATION |/////| CONTINUATION | DATA |///|
3361 * `---^------------------------^-----^--------------^------^---'
3362 * | | <-----> | |
3363 * area | hole | wrap
3364 * |<-----------------------> |
3365 * | dfl |
3366 * |<-------------------------------------------------->|
3367 * head data
3368 *
3369 * Please note that the HEADERS frame is always deprived from its PADLEN byte
3370 * however it may start with the 5 stream-dep+weight bytes in case of PRIORITY
3371 * bit.
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003372 *
3373 * The <flags> field must point to either the stream's flags or to a copy of it
3374 * so that the function can update the following flags :
3375 * - H2_SF_DATA_CLEN when content-length is seen
3376 * - H2_SF_DATA_CHNK when chunking should be used for the H1 conversion
3377 * - H2_SF_HEADERS_RCVD once the frame is successfully decoded
Willy Tarreau88d138e2019-01-02 19:38:14 +01003378 *
3379 * The H2_SF_HEADERS_RCVD flag is also looked at in the <flags> field prior to
3380 * decoding, in order to detect if we're dealing with a headers or a trailers
3381 * block (the trailers block appears after H2_SF_HEADERS_RCVD was seen).
Willy Tarreau13278b42017-10-13 19:23:14 +02003382 */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003383static int h2c_decode_headers(struct h2c *h2c, struct buffer *rxbuf, uint32_t *flags, unsigned long long *body_len)
Willy Tarreau13278b42017-10-13 19:23:14 +02003384{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003385 const uint8_t *hdrs = (uint8_t *)b_head(&h2c->dbuf);
Willy Tarreau83061a82018-07-13 11:56:34 +02003386 struct buffer *tmp = get_trash_chunk();
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003387 struct http_hdr list[MAX_HTTP_HDR * 2];
Willy Tarreau83061a82018-07-13 11:56:34 +02003388 struct buffer *copy = NULL;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003389 unsigned int msgf;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003390 struct htx *htx = NULL;
Willy Tarreauea18f862018-12-22 20:19:26 +01003391 int flen; // header frame len
3392 int hole = 0;
Willy Tarreau86277d42019-01-02 15:36:11 +01003393 int ret = 0;
3394 int outlen;
Willy Tarreau13278b42017-10-13 19:23:14 +02003395 int wrap;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003396 int try = 0;
Willy Tarreau13278b42017-10-13 19:23:14 +02003397
Willy Tarreauea18f862018-12-22 20:19:26 +01003398next_frame:
3399 if (b_data(&h2c->dbuf) - hole < h2c->dfl)
3400 goto leave; // incomplete input frame
3401
3402 /* No END_HEADERS means there's one or more CONTINUATION frames. In
3403 * this case, we'll try to paste it immediately after the initial
3404 * HEADERS frame payload and kill any possible padding. The initial
3405 * frame's length will be increased to represent the concatenation
3406 * of the two frames. The next frame is read from position <tlen>
3407 * and written at position <flen> (minus padding if some is present).
3408 */
3409 if (unlikely(!(h2c->dff & H2_F_HEADERS_END_HEADERS))) {
3410 struct h2_fh hdr;
3411 int clen; // CONTINUATION frame's payload length
3412
3413 if (!h2_peek_frame_hdr(&h2c->dbuf, h2c->dfl + hole, &hdr)) {
3414 /* no more data, the buffer may be full, either due to
3415 * too large a frame or because of too large a hole that
3416 * we're going to compact at the end.
3417 */
3418 goto leave;
3419 }
3420
3421 if (hdr.ft != H2_FT_CONTINUATION) {
3422 /* RFC7540#6.10: frame of unexpected type */
3423 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3424 goto fail;
3425 }
3426
3427 if (hdr.sid != h2c->dsi) {
3428 /* RFC7540#6.10: frame of different stream */
3429 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3430 goto fail;
3431 }
3432
3433 if ((unsigned)hdr.len > (unsigned)global.tune.bufsize) {
3434 /* RFC7540#4.2: invalid frame length */
3435 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3436 goto fail;
3437 }
3438
3439 /* detect when we must stop aggragating frames */
3440 h2c->dff |= hdr.ff & H2_F_HEADERS_END_HEADERS;
3441
3442 /* Take as much as we can of the CONTINUATION frame's payload */
3443 clen = b_data(&h2c->dbuf) - (h2c->dfl + hole + 9);
3444 if (clen > hdr.len)
3445 clen = hdr.len;
3446
3447 /* Move the frame's payload over the padding, hole and frame
3448 * header. At least one of hole or dpl is null (see diagrams
3449 * above). The hole moves after the new aggragated frame.
3450 */
3451 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole + 9), clen, -(h2c->dpl + hole + 9));
3452 h2c->dfl += clen - h2c->dpl;
3453 hole += h2c->dpl + 9;
3454 h2c->dpl = 0;
3455 goto next_frame;
3456 }
3457
3458 flen = h2c->dfl - h2c->dpl;
Willy Tarreau68472622017-12-11 18:36:37 +01003459
Willy Tarreau13278b42017-10-13 19:23:14 +02003460 /* if the input buffer wraps, take a temporary copy of it (rare) */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003461 wrap = b_wrap(&h2c->dbuf) - b_head(&h2c->dbuf);
Willy Tarreau13278b42017-10-13 19:23:14 +02003462 if (wrap < h2c->dfl) {
Willy Tarreau68dd9852017-07-03 14:44:26 +02003463 copy = alloc_trash_chunk();
3464 if (!copy) {
3465 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
3466 goto fail;
3467 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003468 memcpy(copy->area, b_head(&h2c->dbuf), wrap);
3469 memcpy(copy->area + wrap, b_orig(&h2c->dbuf), h2c->dfl - wrap);
3470 hdrs = (uint8_t *) copy->area;
Willy Tarreau13278b42017-10-13 19:23:14 +02003471 }
3472
Willy Tarreau13278b42017-10-13 19:23:14 +02003473 /* Skip StreamDep and weight for now (we don't support PRIORITY) */
3474 if (h2c->dff & H2_F_HEADERS_PRIORITY) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003475 if (read_n32(hdrs) == h2c->dsi) {
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003476 /* RFC7540#5.3.1 : stream dep may not depend on itself */
3477 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
Willy Tarreaua0d11b62018-09-05 18:30:05 +02003478 goto fail;
Willy Tarreau18b86cd2017-12-03 19:24:50 +01003479 }
3480
Willy Tarreaua01f45e2018-12-31 07:41:24 +01003481 if (flen < 5) {
3482 h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR);
3483 goto fail;
3484 }
3485
Willy Tarreau13278b42017-10-13 19:23:14 +02003486 hdrs += 5; // stream dep = 4, weight = 1
3487 flen -= 5;
3488 }
3489
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003490 if (!h2_get_buf(h2c, rxbuf)) {
Willy Tarreau937f7602018-02-26 15:22:17 +01003491 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau86277d42019-01-02 15:36:11 +01003492 goto leave;
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003493 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003494
Willy Tarreau937f7602018-02-26 15:22:17 +01003495 /* we can't retry a failed decompression operation so we must be very
3496 * careful not to take any risks. In practice the output buffer is
3497 * always empty except maybe for trailers, in which case we simply have
3498 * to wait for the upper layer to finish consuming what is available.
3499 */
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003500
3501 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003502 htx = htx_from_buf(rxbuf);
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003503 if (!htx_is_empty(htx)) {
3504 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003505 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003506 }
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003507 } else {
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003508 if (b_data(rxbuf)) {
3509 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau86277d42019-01-02 15:36:11 +01003510 goto leave;
Willy Tarreau8dbb1702019-01-03 08:52:09 +01003511 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003512
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003513 rxbuf->head = 0;
3514 try = b_size(rxbuf);
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003515 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003516
Willy Tarreau25919232019-01-03 14:48:18 +01003517 /* past this point we cannot roll back in case of error */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003518 outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list,
3519 sizeof(list)/sizeof(list[0]), tmp);
3520 if (outlen < 0) {
3521 h2c_error(h2c, H2_ERR_COMPRESSION_ERROR);
3522 goto fail;
3523 }
3524
Willy Tarreau25919232019-01-03 14:48:18 +01003525 /* The PACK decompressor was updated, let's update the input buffer and
3526 * the parser's state to commit these changes and allow us to later
3527 * fail solely on the stream if needed.
3528 */
3529 b_del(&h2c->dbuf, h2c->dfl + hole);
3530 h2c->dfl = hole = 0;
3531 h2c->st0 = H2_CS_FRAME_H;
3532
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003533 /* OK now we have our header list in <list> */
Willy Tarreau880f5802019-01-03 08:10:14 +01003534 msgf = (h2c->dff & H2_F_HEADERS_END_STREAM) ? 0 : H2_MSGF_BODY;
Willy Tarreaubd4a6b62018-11-27 09:29:36 +01003535
Willy Tarreau88d138e2019-01-02 19:38:14 +01003536 if (*flags & H2_SF_HEADERS_RCVD)
3537 goto trailers;
3538
3539 /* This is the first HEADERS frame so it's a headers block */
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003540 if (htx) {
3541 /* HTX mode */
3542 if (h2c->flags & H2_CF_IS_BACK)
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003543 outlen = h2_make_htx_response(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003544 else
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003545 outlen = h2_make_htx_request(list, htx, &msgf, body_len);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003546 } else {
3547 /* HTTP/1 mode */
Willy Tarreau4790f7c2019-01-24 11:33:02 +01003548 outlen = h2_make_h1_request(list, b_tail(rxbuf), try, &msgf, body_len);
Willy Tarreau83195932019-01-03 10:26:23 +01003549 if (outlen > 0)
3550 b_add(rxbuf, outlen);
Willy Tarreauc3e18f32018-10-08 14:51:56 +02003551 }
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003552
3553 if (outlen < 0) {
Willy Tarreau25919232019-01-03 14:48:18 +01003554 /* too large headers? this is a stream error only */
Willy Tarreau59a10fb2017-11-21 20:03:02 +01003555 goto fail;
3556 }
Willy Tarreau13278b42017-10-13 19:23:14 +02003557
Willy Tarreau174b06a2018-04-25 18:13:58 +02003558 if (msgf & H2_MSGF_BODY) {
3559 /* a payload is present */
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003560 if (msgf & H2_MSGF_BODY_CL) {
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003561 *flags |= H2_SF_DATA_CLEN;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003562 if (htx)
3563 htx->extra = *body_len;
3564 }
Olivier Houchard50d660c2018-12-08 00:18:31 +01003565 else if (!(msgf & H2_MSGF_BODY_TUNNEL) && !htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003566 *flags |= H2_SF_DATA_CHNK;
Willy Tarreau174b06a2018-04-25 18:13:58 +02003567 }
3568
Willy Tarreau88d138e2019-01-02 19:38:14 +01003569 done:
Christopher Faulet0b465482019-02-19 15:14:23 +01003570 /* indicate that a HEADERS frame was received for this stream, except
3571 * for 1xx responses. For 1xx responses, another HEADERS frame is
3572 * expected.
3573 */
3574 if (!(msgf & H2_MSGF_RSP_1XX))
3575 *flags |= H2_SF_HEADERS_RCVD;
Willy Tarreau6cc85a52019-01-02 15:49:20 +01003576
Christopher Faulet0b465482019-02-19 15:14:23 +01003577 if ((h2c->dff & H2_F_HEADERS_END_STREAM) || (msgf & H2_MSGF_RSP_1XX)) {
Willy Tarreau88d138e2019-01-02 19:38:14 +01003578 /* Mark the end of message, either using EOM in HTX or with the
3579 * trailing CRLF after the end of trailers. Note that DATA_CHNK
Willy Tarreau2135f912019-05-07 11:17:32 +02003580 * is not set during headers with END_STREAM. For HTX trailers,
3581 * we must not leave an HTX trailers block not followed by an
3582 * EOM block, the two must be atomic. Thus if we fail to emit
3583 * the EOM block we must remove the TLR block we've just added.
Willy Tarreau88d138e2019-01-02 19:38:14 +01003584 */
3585 if (htx) {
Willy Tarreau2135f912019-05-07 11:17:32 +02003586 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3587 struct htx_blk *tail = htx_get_tail_blk(htx);
3588
3589 if (tail && htx_get_blk_type(tail) == HTX_BLK_TLR)
3590 htx_remove_blk(htx, tail);
Willy Tarreau88d138e2019-01-02 19:38:14 +01003591 goto fail;
Willy Tarreau2135f912019-05-07 11:17:32 +02003592 }
Willy Tarreau88d138e2019-01-02 19:38:14 +01003593 }
3594 else if (*flags & H2_SF_DATA_CHNK) {
3595 if (!b_putblk(rxbuf, "\r\n", 2))
3596 goto fail;
3597 }
3598 }
Willy Tarreau937f7602018-02-26 15:22:17 +01003599
Willy Tarreau86277d42019-01-02 15:36:11 +01003600 /* success */
3601 ret = 1;
3602
Willy Tarreau68dd9852017-07-03 14:44:26 +02003603 leave:
Willy Tarreau86277d42019-01-02 15:36:11 +01003604 /* If there is a hole left and it's not at the end, we are forced to
Willy Tarreauea18f862018-12-22 20:19:26 +01003605 * move the remaining data over it.
3606 */
3607 if (hole) {
3608 if (b_data(&h2c->dbuf) > h2c->dfl + hole)
3609 b_move(&h2c->dbuf, b_peek_ofs(&h2c->dbuf, h2c->dfl + hole),
3610 b_data(&h2c->dbuf) - (h2c->dfl + hole), -hole);
3611 b_sub(&h2c->dbuf, hole);
3612 }
3613
Willy Tarreau97215ca2019-04-29 10:20:21 +02003614 if (b_full(&h2c->dbuf) && h2c->dfl >= b_data(&h2c->dbuf)) {
Willy Tarreauea18f862018-12-22 20:19:26 +01003615 /* too large frames */
3616 h2c_error(h2c, H2_ERR_INTERNAL_ERROR);
Willy Tarreau86277d42019-01-02 15:36:11 +01003617 ret = -1;
Willy Tarreauea18f862018-12-22 20:19:26 +01003618 }
3619
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003620 if (htx)
Willy Tarreau5c8cafa2018-12-23 11:30:42 +01003621 htx_to_buf(htx, rxbuf);
Willy Tarreau68dd9852017-07-03 14:44:26 +02003622 free_trash_chunk(copy);
Willy Tarreau86277d42019-01-02 15:36:11 +01003623 return ret;
3624
Willy Tarreau68dd9852017-07-03 14:44:26 +02003625 fail:
Willy Tarreau86277d42019-01-02 15:36:11 +01003626 ret = -1;
Willy Tarreau68dd9852017-07-03 14:44:26 +02003627 goto leave;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003628
3629 trailers:
3630 /* This is the last HEADERS frame hence a trailer */
3631
3632 if (!(h2c->dff & H2_F_HEADERS_END_STREAM)) {
3633 /* It's a trailer but it's missing ES flag */
3634 h2c_error(h2c, H2_ERR_PROTOCOL_ERROR);
3635 goto fail;
3636 }
3637
3638 /* Trailers terminate a DATA sequence. In HTX we have to emit an EOD
3639 * block, and when using chunks we must send the 0 CRLF marker. For
3640 * other modes, the trailers are silently dropped.
3641 */
3642 if (htx) {
3643 if (!htx_add_endof(htx, HTX_BLK_EOD))
3644 goto fail;
Willy Tarreau5255f282019-01-03 18:41:05 +01003645 if (h2_make_htx_trailers(list, htx) <= 0)
3646 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003647 }
3648 else if (*flags & H2_SF_DATA_CHNK) {
3649 /* Legacy mode with chunked encoding : we must finalize the
3650 * data block message emit the trailing CRLF */
3651 if (!b_putblk(rxbuf, "0\r\n", 3))
3652 goto fail;
Willy Tarreaue2b05cc2019-01-03 16:18:34 +01003653
3654 outlen = h2_make_h1_trailers(list, b_tail(rxbuf), try);
3655 if (outlen > 0)
3656 b_add(rxbuf, outlen);
3657 else
3658 goto fail;
Willy Tarreau88d138e2019-01-02 19:38:14 +01003659 }
3660
3661 goto done;
Willy Tarreau13278b42017-10-13 19:23:14 +02003662}
3663
Willy Tarreau454f9052017-10-26 19:40:35 +02003664/* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length
3665 * or a tunnel is used, the contents are copied as-is. When chunked encoding is
3666 * in use, a new chunk is emitted for each frame. This is supposed to fit
3667 * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the
3668 * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest
3669 * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003670 * parser state is automatically updated. Returns > 0 if it could completely
3671 * send the current frame, 0 if it couldn't complete, in which case
3672 * CS_FL_RCV_MORE must be checked to know if some data remain pending (an empty
3673 * DATA frame can return 0 as a valid result). Stream errors are reported in
3674 * h2s->errcode and connection errors in h2c->errcode. The caller must already
3675 * have checked the frame header and ensured that the frame was complete or the
3676 * buffer full. It changes the frame state to FRAME_A once done.
Willy Tarreau454f9052017-10-26 19:40:35 +02003677 */
Willy Tarreau454b57b2018-02-26 15:50:05 +01003678static int h2_frt_transfer_data(struct h2s *h2s)
Willy Tarreau454f9052017-10-26 19:40:35 +02003679{
3680 struct h2c *h2c = h2s->h2c;
3681 int block1, block2;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003682 unsigned int flen = 0;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003683 unsigned int chklen = 0;
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003684 struct htx *htx = NULL;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003685 struct buffer *csbuf;
Willy Tarreau454f9052017-10-26 19:40:35 +02003686
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003687 h2c->flags &= ~H2_CF_DEM_SFULL;
Willy Tarreau454f9052017-10-26 19:40:35 +02003688
Olivier Houchard638b7992018-08-16 15:41:52 +02003689 csbuf = h2_get_buf(h2c, &h2s->rxbuf);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003690 if (!csbuf) {
3691 h2c->flags |= H2_CF_DEM_SALLOC;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003692 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003693 }
3694
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003695try_again:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003696 flen = h2c->dfl - h2c->dpl;
Olivier Houchard2f308832018-12-19 15:53:53 +01003697 if (h2c->proxy->options2 & PR_O2_USE_HTX)
3698 htx = htx_from_buf(csbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003699 if (!flen)
Willy Tarreau4a28da12018-01-04 14:41:00 +01003700 goto end_transfer;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003701
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003702 if (flen > b_data(&h2c->dbuf)) {
3703 flen = b_data(&h2c->dbuf);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003704 if (!flen)
Willy Tarreau454b57b2018-02-26 15:50:05 +01003705 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003706 }
3707
Willy Tarreaua9b77962019-01-31 07:23:00 +01003708 if (htx) {
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003709 block1 = htx_free_data_space(htx);
3710 if (!block1) {
3711 h2c->flags |= H2_CF_DEM_SFULL;
3712 goto fail;
3713 }
3714 if (flen > block1)
3715 flen = block1;
3716
3717 /* here, flen is the max we can copy into the output buffer */
3718 block1 = b_contig_data(&h2c->dbuf, 0);
3719 if (flen > block1)
3720 flen = block1;
3721
3722 if (!htx_add_data(htx, ist2(b_head(&h2c->dbuf), flen))) {
3723 h2c->flags |= H2_CF_DEM_SFULL;
3724 goto fail;
3725 }
3726
3727 b_del(&h2c->dbuf, flen);
3728 h2c->dfl -= flen;
3729 h2c->rcvd_c += flen;
3730 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
Willy Tarreau1915ca22019-01-24 11:49:37 +01003731
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003732 if (h2s->flags & H2_SF_DATA_CLEN) {
Willy Tarreau1915ca22019-01-24 11:49:37 +01003733 h2s->body_len -= flen;
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01003734 htx->extra = h2s->body_len;
3735 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003736 goto try_again;
3737 }
3738 else if (unlikely(b_space_wraps(csbuf))) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003739 /* it doesn't fit and the buffer is fragmented,
3740 * so let's defragment it and try again.
3741 */
3742 b_slow_realign(csbuf, trash.area, 0);
Willy Tarreau454f9052017-10-26 19:40:35 +02003743 }
3744
Willy Tarreaueba10f22018-04-25 20:44:22 +02003745 /* chunked-encoding requires more room */
3746 if (h2s->flags & H2_SF_DATA_CHNK) {
Willy Tarreaud755ea62018-02-26 15:44:54 +01003747 chklen = MIN(flen, b_room(csbuf));
Willy Tarreaueba10f22018-04-25 20:44:22 +02003748 chklen = (chklen < 16) ? 1 : (chklen < 256) ? 2 :
3749 (chklen < 4096) ? 3 : (chklen < 65536) ? 4 :
3750 (chklen < 1048576) ? 4 : 8;
3751 chklen += 4; // CRLF, CRLF
3752 }
3753
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003754 /* does it fit in output buffer or should we wait ? */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003755 if (flen + chklen > b_room(csbuf)) {
3756 if (chklen >= b_room(csbuf)) {
3757 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003758 goto fail;
Willy Tarreaud755ea62018-02-26 15:44:54 +01003759 }
3760 flen = b_room(csbuf) - chklen;
Willy Tarreaueba10f22018-04-25 20:44:22 +02003761 }
3762
3763 if (h2s->flags & H2_SF_DATA_CHNK) {
3764 /* emit the chunk size */
3765 unsigned int chksz = flen;
3766 char str[10];
3767 char *beg;
3768
3769 beg = str + sizeof(str);
3770 *--beg = '\n';
3771 *--beg = '\r';
3772 do {
3773 *--beg = hextab[chksz & 0xF];
3774 } while (chksz >>= 4);
Willy Tarreaud755ea62018-02-26 15:44:54 +01003775 b_putblk(csbuf, beg, str + sizeof(str) - beg);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003776 }
3777
Willy Tarreau454f9052017-10-26 19:40:35 +02003778 /* Block1 is the length of the first block before the buffer wraps,
3779 * block2 is the optional second block to reach the end of the frame.
3780 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003781 block1 = b_contig_data(&h2c->dbuf, 0);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003782 if (block1 > flen)
3783 block1 = flen;
Willy Tarreau454f9052017-10-26 19:40:35 +02003784 block2 = flen - block1;
3785
3786 if (block1)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003787 b_putblk(csbuf, b_head(&h2c->dbuf), block1);
Willy Tarreau454f9052017-10-26 19:40:35 +02003788
3789 if (block2)
Willy Tarreaud755ea62018-02-26 15:44:54 +01003790 b_putblk(csbuf, b_peek(&h2c->dbuf, block1), block2);
Willy Tarreau454f9052017-10-26 19:40:35 +02003791
Willy Tarreaueba10f22018-04-25 20:44:22 +02003792 if (h2s->flags & H2_SF_DATA_CHNK) {
3793 /* emit the CRLF */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003794 b_putblk(csbuf, "\r\n", 2);
Willy Tarreaueba10f22018-04-25 20:44:22 +02003795 }
3796
Willy Tarreau454f9052017-10-26 19:40:35 +02003797 /* now mark the input data as consumed (will be deleted from the buffer
3798 * by the caller when seeing FRAME_A after sending the window update).
3799 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003800 b_del(&h2c->dbuf, flen);
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003801 h2c->dfl -= flen;
3802 h2c->rcvd_c += flen;
3803 h2c->rcvd_s += flen; // warning, this can also affect the closed streams!
3804
Willy Tarreau1915ca22019-01-24 11:49:37 +01003805 if (h2s->flags & H2_SF_DATA_CLEN)
3806 h2s->body_len -= flen;
3807
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003808 if (h2c->dfl > h2c->dpl) {
3809 /* more data available, transfer stalled on stream full */
Willy Tarreaud755ea62018-02-26 15:44:54 +01003810 h2c->flags |= H2_CF_DEM_SFULL;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003811 goto fail;
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003812 }
3813
Willy Tarreau4a28da12018-01-04 14:41:00 +01003814 end_transfer:
Willy Tarreau8fc016d2017-12-11 18:27:15 +01003815 /* here we're done with the frame, all the payload (except padding) was
3816 * transferred.
3817 */
Willy Tarreaueba10f22018-04-25 20:44:22 +02003818
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003819 if (h2c->dff & H2_F_DATA_END_STREAM) {
3820 if (htx) {
3821 if (!htx_add_endof(htx, HTX_BLK_EOM)) {
3822 h2c->flags |= H2_CF_DEM_SFULL;
3823 goto fail;
3824 }
Willy Tarreaud755ea62018-02-26 15:44:54 +01003825 }
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003826 else if (h2s->flags & H2_SF_DATA_CHNK) {
3827 /* emit the trailing 0 CRLF CRLF */
3828 if (b_room(csbuf) < 5) {
3829 h2c->flags |= H2_CF_DEM_SFULL;
3830 goto fail;
3831 }
3832 chklen += 5;
3833 b_putblk(csbuf, "0\r\n\r\n", 5);
3834 }
Willy Tarreaueba10f22018-04-25 20:44:22 +02003835 }
3836
Willy Tarreaud1023bb2018-03-22 16:53:12 +01003837 h2c->rcvd_c += h2c->dpl;
3838 h2c->rcvd_s += h2c->dpl;
3839 h2c->dpl = 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003840 h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003841 if (htx)
3842 htx_to_buf(htx, csbuf);
Willy Tarreau61ea7dc2018-12-01 23:23:04 +01003843 return 1;
Willy Tarreau454b57b2018-02-26 15:50:05 +01003844 fail:
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003845 if (htx)
3846 htx_to_buf(htx, csbuf);
Willy Tarreau454b57b2018-02-26 15:50:05 +01003847 return 0;
Willy Tarreau454f9052017-10-26 19:40:35 +02003848}
3849
Willy Tarreau5dd17352018-06-14 13:33:30 +02003850/* Try to send a HEADERS frame matching HTTP/1 response present at offset <ofs>
3851 * and for <max> bytes in buffer <buf> for the H2 stream <h2s>. Returns the
3852 * number of bytes sent. The caller must check the stream's status to detect
3853 * any error which might have happened subsequently to a successful send.
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003854 */
Willy Tarreau206ba832018-06-14 15:27:31 +02003855static 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 +02003856{
3857 struct http_hdr list[MAX_HTTP_HDR];
3858 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02003859 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02003860 struct buffer outbuf;
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003861 union h1_sl sl;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003862 int es_now = 0;
3863 int ret = 0;
3864 int hdr;
3865
3866 if (h2c_mux_busy(h2c, h2s)) {
3867 h2s->flags |= H2_SF_BLK_MBUSY;
3868 return 0;
3869 }
3870
Willy Tarreau44e973f2018-03-01 17:49:30 +01003871 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003872 h2c->flags |= H2_CF_MUX_MALLOC;
3873 h2s->flags |= H2_SF_BLK_MROOM;
3874 return 0;
3875 }
3876
3877 /* First, try to parse the H1 response and index it into <list>.
3878 * NOTE! Since it comes from haproxy, we *know* that a response header
3879 * block does not wrap and we can safely read it this way without
3880 * having to realign the buffer.
3881 */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003882 ret = h1_headers_to_hdr_list(b_peek(buf, ofs), b_peek(buf, ofs) + max,
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003883 list, sizeof(list)/sizeof(list[0]), h1m, &sl);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003884 if (ret <= 0) {
Willy Tarreauf13ef962017-11-02 15:14:19 +01003885 /* incomplete or invalid response, this is abnormal coming from
3886 * haproxy and may only result in a bad errorfile or bad Lua code
3887 * so that won't be fixed, raise an error now.
3888 *
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003889 * FIXME: we should instead add the ability to only return a
3890 * 502 bad gateway. But in theory this is not supposed to
3891 * happen.
3892 */
3893 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3894 ret = 0;
3895 goto end;
3896 }
3897
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003898 h2s->status = sl.st.status;
Willy Tarreaudb72da02018-09-13 11:52:20 +02003899
3900 /* certain statuses have no body or an empty one, regardless of
3901 * what the headers say.
3902 */
3903 if (sl.st.status >= 100 && sl.st.status < 200) {
3904 h1m->flags &= ~(H1_MF_CLEN | H1_MF_CHNK);
3905 h1m->curr_len = h1m->body_len = 0;
3906 }
3907 else if (sl.st.status == 204 || sl.st.status == 304) {
3908 /* no contents, claim c-len is present and set to zero */
3909 h1m->flags &= ~H1_MF_CHNK;
3910 h1m->flags |= H1_MF_CLEN;
3911 h1m->curr_len = h1m->body_len = 0;
3912 }
3913
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003914 chunk_reset(&outbuf);
3915
3916 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003917 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003918 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003919 outbuf.data = 0;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003920
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003921 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003922 break;
3923 realign_again:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003924 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003925 }
3926
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003927 if (outbuf.size < 9)
3928 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003929
3930 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003931 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
3932 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
3933 outbuf.data = 9;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003934
3935 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01003936 if (unlikely(list[0].v.len != 3)) {
Willy Tarreaua87f2022017-11-09 11:23:00 +01003937 /* this is an unparsable response */
3938 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
3939 ret = 0;
3940 goto end;
3941 }
Willy Tarreauaafdf582018-12-10 18:06:40 +01003942
3943 if (!hpack_encode_str_status(&outbuf, h2s->status, list[0].v)) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003944 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003945 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003946 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003947 }
3948
3949 /* encode all headers, stop at empty name */
3950 for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
Willy Tarreaua76e4c22017-11-24 08:17:28 +01003951 /* these ones do not exist in H2 and must be dropped. */
3952 if (isteq(list[hdr].n, ist("connection")) ||
3953 isteq(list[hdr].n, ist("proxy-connection")) ||
3954 isteq(list[hdr].n, ist("keep-alive")) ||
3955 isteq(list[hdr].n, ist("upgrade")) ||
3956 isteq(list[hdr].n, ist("transfer-encoding")))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003957 continue;
3958
3959 if (isteq(list[hdr].n, ist("")))
3960 break; // end
3961
3962 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
3963 /* output full */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003964 if (b_space_wraps(&h2c->mbuf))
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003965 goto realign_again;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02003966 goto full;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003967 }
3968 }
3969
3970 /* we may need to add END_STREAM */
Willy Tarreau927b88b2019-03-04 08:03:25 +01003971 if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || !h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003972 es_now = 1;
3973
3974 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003975 h2_set_frame_size(outbuf.area, outbuf.data - 9);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003976
3977 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003978 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003979
3980 /* consume incoming H1 response */
Willy Tarreau5dd17352018-06-14 13:33:30 +02003981 max -= ret;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003982
3983 /* commit the H2 response */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003984 b_add(&h2c->mbuf, outbuf.data);
Willy Tarreau67434202017-11-06 20:20:51 +01003985 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003986
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003987 if (es_now) {
Willy Tarreau35a62702018-02-27 15:37:25 +01003988 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02003989 ret += max;
Willy Tarreau35a62702018-02-27 15:37:25 +01003990
Willy Tarreau801250e2018-09-11 11:45:04 +02003991 h1m->state = H1_MSG_DONE;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003992 h2s->flags |= H2_SF_ES_SENT;
3993 if (h2s->st == H2_SS_OPEN)
3994 h2s->st = H2_SS_HLOC;
3995 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01003996 h2s_close(h2s);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02003997 }
Willy Tarreau9c5e22e2018-09-11 19:22:14 +02003998 else if (h2s->status >= 100 && h2s->status < 200) {
Willy Tarreau87285592017-11-29 15:41:32 +01003999 /* we'll let the caller check if it has more headers to send */
Willy Tarreau7f437ff2018-09-11 13:51:19 +02004000 h1m_init_res(h1m);
Willy Tarreau9b8cd1f2018-09-12 09:24:38 +02004001 h1m->err_pos = -1; // don't care about errors on the response path
Willy Tarreaueb528db2018-09-12 09:54:00 +02004002 h2s->h1m.flags |= H1_MF_TOLOWER;
Willy Tarreau87285592017-11-29 15:41:32 +01004003 goto end;
Willy Tarreauc199faf2017-10-31 08:35:27 +01004004 }
Willy Tarreau001823c2018-09-12 17:25:32 +02004005
4006 /* now the h1m state is either H1_MSG_CHUNK_SIZE or H1_MSG_DATA */
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004007
4008 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004009 //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 +02004010 return ret;
Willy Tarreaub5b7d4a2018-09-12 18:51:18 +02004011 full:
4012 h1m_init_res(h1m);
4013 h1m->err_pos = -1; // don't care about errors on the response path
4014 h2c->flags |= H2_CF_MUX_MFULL;
4015 h2s->flags |= H2_SF_BLK_MROOM;
4016 ret = 0;
4017 goto end;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02004018}
4019
Willy Tarreau5dd17352018-06-14 13:33:30 +02004020/* Try to send a DATA frame matching HTTP/1 response present at offset <ofs>
4021 * for up to <max> bytes in response buffer <buf>, for stream <h2s>. Returns
4022 * the number of bytes sent. The caller must check the stream's status to
4023 * detect any error which might have happened subsequently to a successful send.
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004024 */
Willy Tarreau206ba832018-06-14 15:27:31 +02004025static 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 +02004026{
4027 struct h2c *h2c = h2s->h2c;
Willy Tarreaua40704a2018-09-11 13:52:04 +02004028 struct h1m *h1m = &h2s->h1m;
Willy Tarreau83061a82018-07-13 11:56:34 +02004029 struct buffer outbuf;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004030 int ret = 0;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02004031 size_t total = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004032 int es_now = 0;
4033 int size = 0;
Willy Tarreau206ba832018-06-14 15:27:31 +02004034 const char *blk1, *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02004035 size_t len1, len2;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004036
4037 if (h2c_mux_busy(h2c, h2s)) {
4038 h2s->flags |= H2_SF_BLK_MBUSY;
4039 goto end;
4040 }
4041
Willy Tarreau44e973f2018-03-01 17:49:30 +01004042 if (!h2_get_buf(h2c, &h2c->mbuf)) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004043 h2c->flags |= H2_CF_MUX_MALLOC;
4044 h2s->flags |= H2_SF_BLK_MROOM;
4045 goto end;
4046 }
4047
4048 new_frame:
Willy Tarreau5dd17352018-06-14 13:33:30 +02004049 if (!max)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004050 goto end;
4051
4052 chunk_reset(&outbuf);
4053
4054 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004055 outbuf.area = b_tail(&h2c->mbuf);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004056 outbuf.size = b_contig_space(&h2c->mbuf);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004057 outbuf.data = 0;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004058
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004059 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004060 break;
4061 realign_again:
Willy Tarreau06ae84a2018-12-12 09:17:21 +01004062 /* If there are pending data in the output buffer, and we have
4063 * less than 1/4 of the mbuf's size and everything fits, we'll
4064 * still perform a copy anyway. Otherwise we'll pretend the mbuf
4065 * is full and wait, to save some slow realign calls.
4066 */
4067 if ((max + 9 > b_room(&h2c->mbuf) || max >= b_size(&h2c->mbuf) / 4)) {
4068 h2c->flags |= H2_CF_MUX_MFULL;
4069 h2s->flags |= H2_SF_BLK_MROOM;
4070 goto end;
4071 }
4072
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004073 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004074 }
4075
4076 if (outbuf.size < 9) {
4077 h2c->flags |= H2_CF_MUX_MFULL;
4078 h2s->flags |= H2_SF_BLK_MROOM;
4079 goto end;
4080 }
4081
4082 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004083 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4084 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4085 outbuf.data = 9;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004086
4087 switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) {
4088 case 0: /* no content length, read till SHUTW */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004089 size = max;
Willy Tarreau13e4e942017-12-14 10:55:21 +01004090 h1m->curr_len = size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004091 break;
4092 case H1_MF_CLEN: /* content-length: read only h2m->body_len */
Willy Tarreau5dd17352018-06-14 13:33:30 +02004093 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004094 if ((long long)size > h1m->curr_len)
4095 size = h1m->curr_len;
4096 break;
4097 default: /* te:chunked : parse chunks */
Willy Tarreau801250e2018-09-11 11:45:04 +02004098 if (h1m->state == H1_MSG_CHUNK_CRLF) {
Willy Tarreauc0973c62018-06-14 15:53:21 +02004099 ret = h1_skip_chunk_crlf(buf, ofs, ofs + max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004100 if (!ret)
4101 goto end;
4102
4103 if (ret < 0) {
4104 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004105 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004106 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4107 goto end;
4108 }
Willy Tarreau5dd17352018-06-14 13:33:30 +02004109 max -= ret;
4110 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004111 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004112 h1m->state = H1_MSG_CHUNK_SIZE;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004113 }
4114
Willy Tarreau801250e2018-09-11 11:45:04 +02004115 if (h1m->state == H1_MSG_CHUNK_SIZE) {
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004116 unsigned int chunk;
Willy Tarreau84d6b7a2018-06-14 15:59:05 +02004117 ret = h1_parse_chunk_size(buf, ofs, ofs + max, &chunk);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004118 if (!ret)
4119 goto end;
4120
4121 if (ret < 0) {
4122 /* FIXME: bad contents. how to proceed here when we're in H2 ? */
Willy Tarreau25173a72018-09-12 09:05:16 +02004123 h1m->err_pos = ofs + max + ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004124 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4125 goto end;
4126 }
4127
4128 size = chunk;
4129 h1m->curr_len = chunk;
4130 h1m->body_len += chunk;
Willy Tarreau5dd17352018-06-14 13:33:30 +02004131 max -= ret;
4132 ofs += ret;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004133 total += ret;
Willy Tarreau801250e2018-09-11 11:45:04 +02004134 h1m->state = size ? H1_MSG_DATA : H1_MSG_TRAILERS;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004135 if (!size)
4136 goto send_empty;
4137 }
4138
4139 /* in MSG_DATA state, continue below */
4140 size = h1m->curr_len;
4141 break;
4142 }
4143
4144 /* we have in <size> the exact number of bytes we need to copy from
4145 * the H1 buffer. We need to check this against the connection's and
4146 * the stream's send windows, and to ensure that this fits in the max
4147 * frame size and in the buffer's available space minus 9 bytes (for
4148 * the frame header). The connection's flow control is applied last so
4149 * that we can use a separate list of streams which are immediately
4150 * unblocked on window opening. Note: we don't implement padding.
4151 */
4152
Willy Tarreau5dd17352018-06-14 13:33:30 +02004153 if (size > max)
4154 size = max;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004155
4156 if (size > h2s->mws)
4157 size = h2s->mws;
4158
4159 if (size <= 0) {
4160 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houchardbfe2a832019-05-10 14:02:21 +02004161 if (!LIST_ISEMPTY(&h2s->list))
4162 LIST_DEL_INIT(&h2s->list);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004163 goto end;
4164 }
4165
4166 if (h2c->mfs && size > h2c->mfs)
4167 size = h2c->mfs;
4168
4169 if (size + 9 > outbuf.size) {
4170 /* we have an opportunity for enlarging the too small
4171 * available space, let's try.
4172 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004173 if (b_space_wraps(&h2c->mbuf))
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004174 goto realign_again;
4175 size = outbuf.size - 9;
4176 }
4177
4178 if (size <= 0) {
4179 h2c->flags |= H2_CF_MUX_MFULL;
4180 h2s->flags |= H2_SF_BLK_MROOM;
4181 goto end;
4182 }
4183
4184 if (size > h2c->mws)
4185 size = h2c->mws;
4186
4187 if (size <= 0) {
4188 h2s->flags |= H2_SF_BLK_MFCTL;
4189 goto end;
4190 }
4191
4192 /* copy whatever we can */
4193 blk1 = blk2 = NULL; // silence a maybe-uninitialized warning
Willy Tarreau5dd17352018-06-14 13:33:30 +02004194 ret = b_getblk_nc(buf, &blk1, &len1, &blk2, &len2, ofs, max);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004195 if (ret == 1)
4196 len2 = 0;
4197
4198 if (!ret || len1 + len2 < size) {
4199 /* FIXME: must normally never happen */
4200 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4201 goto end;
4202 }
4203
4204 /* limit len1/len2 to size */
4205 if (len1 + len2 > size) {
4206 int sub = len1 + len2 - size;
4207
4208 if (len2 > sub)
4209 len2 -= sub;
4210 else {
4211 sub -= len2;
4212 len2 = 0;
4213 len1 -= sub;
4214 }
4215 }
4216
4217 /* now let's copy this this into the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004218 memcpy(outbuf.area + 9, blk1, len1);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004219 if (len2)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004220 memcpy(outbuf.area + 9 + len1, blk2, len2);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004221
4222 send_empty:
4223 /* we may need to add END_STREAM */
4224 /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we
4225 * could rely on the MSG_MORE flag as a hint for this ?
Willy Tarreau00610962018-07-19 10:58:28 +02004226 *
4227 * FIXME: what we do here is not correct because we send end_stream
4228 * before knowing if we'll have to send a HEADERS frame for the
4229 * trailers. More importantly we're not consuming the trailing CRLF
4230 * after the end of trailers, so it will be left to the caller to
4231 * eat it. The right way to do it would be to measure trailers here
4232 * and to send ES only if there are no trailers.
4233 *
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004234 */
4235 if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) ||
Willy Tarreau801250e2018-09-11 11:45:04 +02004236 !h1m->curr_len || h1m->state >= H1_MSG_DONE)
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004237 es_now = 1;
4238
4239 /* update the frame's size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004240 h2_set_frame_size(outbuf.area, size);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004241
4242 if (es_now)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004243 outbuf.area[4] |= H2_F_DATA_END_STREAM;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004244
4245 /* commit the H2 response */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02004246 b_add(&h2c->mbuf, size + 9);
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004247
4248 /* consume incoming H1 response */
4249 if (size > 0) {
Willy Tarreau5dd17352018-06-14 13:33:30 +02004250 max -= size;
4251 ofs += size;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004252 total += size;
4253 h1m->curr_len -= size;
4254 h2s->mws -= size;
4255 h2c->mws -= size;
4256
4257 if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) {
Willy Tarreau801250e2018-09-11 11:45:04 +02004258 h1m->state = H1_MSG_CHUNK_CRLF;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004259 goto new_frame;
4260 }
4261 }
4262
4263 if (es_now) {
4264 if (h2s->st == H2_SS_OPEN)
4265 h2s->st = H2_SS_HLOC;
4266 else
Willy Tarreau00dd0782018-03-01 16:31:34 +01004267 h2s_close(h2s);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004268
Willy Tarreau35a62702018-02-27 15:37:25 +01004269 if (!(h1m->flags & H1_MF_CHNK)) {
4270 // trim any possibly pending data (eg: inconsistent content-length)
Willy Tarreau5dd17352018-06-14 13:33:30 +02004271 total += max;
4272 ofs += max;
4273 max = 0;
Willy Tarreau35a62702018-02-27 15:37:25 +01004274
Willy Tarreau801250e2018-09-11 11:45:04 +02004275 h1m->state = H1_MSG_DONE;
Willy Tarreau35a62702018-02-27 15:37:25 +01004276 }
Willy Tarreau9d89ac82017-10-31 17:15:59 +01004277
Willy Tarreauc652dbd2017-10-19 11:16:37 +02004278 h2s->flags |= H2_SF_ES_SENT;
4279 }
4280
4281 end:
Dirkjan Bussinkc26c72d2018-09-14 14:30:25 +02004282 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 +02004283 return total;
4284}
4285
Willy Tarreau115e83b2018-12-01 19:17:53 +01004286/* Try to send a HEADERS frame matching HTX response present in HTX message
4287 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4288 * must check the stream's status to detect any error which might have happened
4289 * subsequently to a successful send. The htx blocks are automatically removed
4290 * from the message. The htx message is assumed to be valid since produced from
4291 * the internal code, hence it contains a start line, an optional series of
4292 * header blocks and an end of header, otherwise an invalid frame could be
4293 * emitted and the resulting htx message could be left in an inconsistent state.
4294 */
4295static size_t h2s_htx_frt_make_resp_headers(struct h2s *h2s, struct htx *htx)
4296{
4297 struct http_hdr list[MAX_HTTP_HDR];
4298 struct h2c *h2c = h2s->h2c;
4299 struct htx_blk *blk;
4300 struct htx_blk *blk_end;
4301 struct buffer outbuf;
4302 struct htx_sl *sl;
4303 enum htx_blk_type type;
4304 int es_now = 0;
4305 int ret = 0;
4306 int hdr;
4307 int idx;
4308
4309 if (h2c_mux_busy(h2c, h2s)) {
4310 h2s->flags |= H2_SF_BLK_MBUSY;
4311 return 0;
4312 }
4313
4314 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4315 h2c->flags |= H2_CF_MUX_MALLOC;
4316 h2s->flags |= H2_SF_BLK_MROOM;
4317 return 0;
4318 }
4319
4320 /* determine the first block which must not be deleted, blk_end may
4321 * be NULL if all blocks have to be deleted.
4322 */
4323 idx = htx_get_head(htx);
4324 blk_end = NULL;
4325 while (idx != -1) {
4326 type = htx_get_blk_type(htx_get_blk(htx, idx));
4327 idx = htx_get_next(htx, idx);
4328 if (type == HTX_BLK_EOH) {
4329 if (idx != -1)
4330 blk_end = htx_get_blk(htx, idx);
4331 break;
4332 }
4333 }
4334
4335 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004336 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004337 ALREADY_CHECKED(sl);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004338 h2s->status = sl->info.res.status;
Willy Tarreauaafdf582018-12-10 18:06:40 +01004339 if (h2s->status < 100 || h2s->status > 999)
4340 goto fail;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004341
4342 /* and the rest of the headers, that we dump starting at header 0 */
4343 hdr = 0;
4344
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004345 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau115e83b2018-12-01 19:17:53 +01004346 while ((idx = htx_get_next(htx, idx)) != -1) {
4347 blk = htx_get_blk(htx, idx);
4348 type = htx_get_blk_type(blk);
4349
4350 if (type == HTX_BLK_UNUSED)
4351 continue;
4352
4353 if (type != HTX_BLK_HDR)
4354 break;
4355
4356 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4357 goto fail;
4358
4359 list[hdr].n = htx_get_blk_name(htx, blk);
4360 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004361 hdr++;
4362 }
4363
4364 /* marker for end of headers */
4365 list[hdr].n = ist("");
4366
4367 if (h2s->status == 204 || h2s->status == 304) {
4368 /* no contents, claim c-len is present and set to zero */
4369 es_now = 1;
4370 }
4371
4372 chunk_reset(&outbuf);
4373
4374 while (1) {
4375 outbuf.area = b_tail(&h2c->mbuf);
4376 outbuf.size = b_contig_space(&h2c->mbuf);
4377 outbuf.data = 0;
4378
4379 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4380 break;
4381 realign_again:
4382 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4383 }
4384
4385 if (outbuf.size < 9)
4386 goto full;
4387
4388 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4389 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4390 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4391 outbuf.data = 9;
4392
4393 /* encode status, which necessarily is the first one */
Willy Tarreauaafdf582018-12-10 18:06:40 +01004394 if (!hpack_encode_int_status(&outbuf, h2s->status)) {
Willy Tarreau115e83b2018-12-01 19:17:53 +01004395 if (b_space_wraps(&h2c->mbuf))
4396 goto realign_again;
4397 goto full;
4398 }
4399
4400 /* encode all headers, stop at empty name */
4401 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4402 /* these ones do not exist in H2 and must be dropped. */
4403 if (isteq(list[hdr].n, ist("connection")) ||
4404 isteq(list[hdr].n, ist("proxy-connection")) ||
4405 isteq(list[hdr].n, ist("keep-alive")) ||
4406 isteq(list[hdr].n, ist("upgrade")) ||
4407 isteq(list[hdr].n, ist("transfer-encoding")))
4408 continue;
4409
4410 if (isteq(list[hdr].n, ist("")))
4411 break; // end
4412
4413 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4414 /* output full */
4415 if (b_space_wraps(&h2c->mbuf))
4416 goto realign_again;
4417 goto full;
4418 }
4419 }
4420
Christopher Faulet0b465482019-02-19 15:14:23 +01004421 /* we may need to add END_STREAM except for 1xx responses.
Willy Tarreau115e83b2018-12-01 19:17:53 +01004422 * FIXME: we should also set it when we know for sure that the
4423 * content-length is zero as well as on 204/304
4424 */
Christopher Faulet0b465482019-02-19 15:14:23 +01004425 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM &&
4426 (h2s->status >= 200 || h2s->status == 101))
Willy Tarreau115e83b2018-12-01 19:17:53 +01004427 es_now = 1;
4428
Willy Tarreau927b88b2019-03-04 08:03:25 +01004429 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau115e83b2018-12-01 19:17:53 +01004430 es_now = 1;
4431
4432 /* update the frame's size */
4433 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4434
4435 if (es_now)
4436 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4437
4438 /* commit the H2 response */
4439 b_add(&h2c->mbuf, outbuf.data);
Christopher Faulet0b465482019-02-19 15:14:23 +01004440
4441 /* indicates the HEADERS frame was sent, except for 1xx responses. For
4442 * 1xx responses, another HEADERS frame is expected.
4443 */
4444 if (h2s->status >= 200 || h2s->status == 101)
4445 h2s->flags |= H2_SF_HEADERS_SENT;
Willy Tarreau115e83b2018-12-01 19:17:53 +01004446
Willy Tarreau115e83b2018-12-01 19:17:53 +01004447 if (es_now) {
4448 h2s->flags |= H2_SF_ES_SENT;
4449 if (h2s->st == H2_SS_OPEN)
4450 h2s->st = H2_SS_HLOC;
4451 else
4452 h2s_close(h2s);
4453 }
4454
4455 /* OK we could properly deliver the response */
4456
4457 /* remove all header blocks including the EOH and compute the
4458 * corresponding size.
4459 *
4460 * FIXME: We should remove everything when es_now is set.
4461 */
4462 ret = 0;
4463 idx = htx_get_head(htx);
4464 blk = htx_get_blk(htx, idx);
4465 while (blk != blk_end) {
4466 ret += htx_get_blksz(blk);
4467 blk = htx_remove_blk(htx, blk);
4468 }
Willy Tarreauc5753ae2018-12-02 12:28:01 +01004469
4470 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4471 htx_remove_blk(htx, blk_end);
Willy Tarreau115e83b2018-12-01 19:17:53 +01004472 end:
4473 return ret;
4474 full:
4475 h2c->flags |= H2_CF_MUX_MFULL;
4476 h2s->flags |= H2_SF_BLK_MROOM;
4477 ret = 0;
4478 goto end;
4479 fail:
4480 /* unparsable HTX messages, too large ones to be produced in the local
4481 * list etc go here (unrecoverable errors).
4482 */
4483 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4484 ret = 0;
4485 goto end;
4486}
4487
Willy Tarreau80739692018-10-05 11:35:57 +02004488/* Try to send a HEADERS frame matching HTX request present in HTX message
4489 * <htx> for the H2 stream <h2s>. Returns the number of bytes sent. The caller
4490 * must check the stream's status to detect any error which might have happened
4491 * subsequently to a successful send. The htx blocks are automatically removed
4492 * from the message. The htx message is assumed to be valid since produced from
4493 * the internal code, hence it contains a start line, an optional series of
4494 * header blocks and an end of header, otherwise an invalid frame could be
4495 * emitted and the resulting htx message could be left in an inconsistent state.
4496 */
4497static size_t h2s_htx_bck_make_req_headers(struct h2s *h2s, struct htx *htx)
4498{
4499 struct http_hdr list[MAX_HTTP_HDR];
4500 struct h2c *h2c = h2s->h2c;
4501 struct htx_blk *blk;
4502 struct htx_blk *blk_end;
4503 struct buffer outbuf;
4504 struct htx_sl *sl;
Willy Tarreau053c1572019-02-01 16:13:59 +01004505 struct ist meth, path, auth;
Willy Tarreau80739692018-10-05 11:35:57 +02004506 enum htx_blk_type type;
4507 int es_now = 0;
4508 int ret = 0;
4509 int hdr;
4510 int idx;
4511
4512 if (h2c_mux_busy(h2c, h2s)) {
4513 h2s->flags |= H2_SF_BLK_MBUSY;
4514 return 0;
4515 }
4516
4517 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4518 h2c->flags |= H2_CF_MUX_MALLOC;
4519 h2s->flags |= H2_SF_BLK_MROOM;
4520 return 0;
4521 }
4522
4523 /* determine the first block which must not be deleted, blk_end may
4524 * be NULL if all blocks have to be deleted.
4525 */
4526 idx = htx_get_head(htx);
4527 blk_end = NULL;
4528 while (idx != -1) {
4529 type = htx_get_blk_type(htx_get_blk(htx, idx));
4530 idx = htx_get_next(htx, idx);
4531 if (type == HTX_BLK_EOH) {
4532 if (idx != -1)
4533 blk_end = htx_get_blk(htx, idx);
4534 break;
4535 }
4536 }
4537
4538 /* get the start line, we do have one */
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004539 sl = htx_get_stline(htx);
Willy Tarreaue2778a42018-12-08 15:30:46 +01004540 ALREADY_CHECKED(sl);
Willy Tarreau80739692018-10-05 11:35:57 +02004541 meth = htx_sl_req_meth(sl);
4542 path = htx_sl_req_uri(sl);
4543
4544 /* and the rest of the headers, that we dump starting at header 0 */
4545 hdr = 0;
4546
Willy Tarreau8e162ee2018-12-06 14:07:27 +01004547 idx = htx_get_head(htx); // returns the SL that we skip
Willy Tarreau80739692018-10-05 11:35:57 +02004548 while ((idx = htx_get_next(htx, idx)) != -1) {
4549 blk = htx_get_blk(htx, idx);
4550 type = htx_get_blk_type(blk);
4551
4552 if (type == HTX_BLK_UNUSED)
4553 continue;
4554
4555 if (type != HTX_BLK_HDR)
4556 break;
4557
4558 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
4559 goto fail;
4560
4561 list[hdr].n = htx_get_blk_name(htx, blk);
4562 list[hdr].v = htx_get_blk_value(htx, blk);
Willy Tarreau80739692018-10-05 11:35:57 +02004563 hdr++;
4564 }
4565
4566 /* marker for end of headers */
4567 list[hdr].n = ist("");
4568
4569 chunk_reset(&outbuf);
4570
4571 while (1) {
4572 outbuf.area = b_tail(&h2c->mbuf);
4573 outbuf.size = b_contig_space(&h2c->mbuf);
4574 outbuf.data = 0;
4575
4576 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4577 break;
4578 realign_again:
4579 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4580 }
4581
4582 if (outbuf.size < 9)
4583 goto full;
4584
4585 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */
4586 memcpy(outbuf.area, "\x00\x00\x00\x01\x04", 5);
4587 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4588 outbuf.data = 9;
4589
4590 /* encode the method, which necessarily is the first one */
Willy Tarreaubdabc3a2018-12-10 18:25:11 +01004591 if (!hpack_encode_method(&outbuf, sl->info.req.meth, meth)) {
Willy Tarreau80739692018-10-05 11:35:57 +02004592 if (b_space_wraps(&h2c->mbuf))
4593 goto realign_again;
4594 goto full;
4595 }
4596
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004597 /* RFC7540 #8.3: the CONNECT method must have :
4598 * - :authority set to the URI part (host:port)
4599 * - :method set to CONNECT
4600 * - :scheme and :path omitted
4601 */
4602 if (sl->info.req.meth != HTTP_METH_CONNECT) {
4603 /* encode the scheme which is always "https" (or 0x86 for "http") */
4604 if (!hpack_encode_scheme(&outbuf, ist("https"))) {
4605 /* output full */
4606 if (b_space_wraps(&h2c->mbuf))
4607 goto realign_again;
4608 goto full;
4609 }
Willy Tarreau80739692018-10-05 11:35:57 +02004610
Willy Tarreau5be92ff2019-02-01 15:51:59 +01004611 /* encode the path, which necessarily is the second one */
4612 if (!hpack_encode_path(&outbuf, path)) {
4613 /* output full */
4614 if (b_space_wraps(&h2c->mbuf))
4615 goto realign_again;
4616 goto full;
4617 }
Willy Tarreau053c1572019-02-01 16:13:59 +01004618
4619 /* look for the Host header and place it in :authority */
4620 auth = ist2(NULL, 0);
4621 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4622 if (isteq(list[hdr].n, ist("")))
4623 break; // end
4624
4625 if (isteq(list[hdr].n, ist("host"))) {
4626 auth = list[hdr].v;
4627 break;
4628 }
4629 }
4630 }
4631 else {
4632 /* for CONNECT, :authority is taken from the path */
4633 auth = path;
4634 }
4635
4636 if (auth.ptr && !hpack_encode_header(&outbuf, ist(":authority"), auth)) {
4637 /* output full */
4638 if (b_space_wraps(&h2c->mbuf))
4639 goto realign_again;
4640 goto full;
Willy Tarreau80739692018-10-05 11:35:57 +02004641 }
4642
4643 /* encode all headers, stop at empty name */
4644 for (hdr = 0; hdr < sizeof(list)/sizeof(list[0]); hdr++) {
4645 /* these ones do not exist in H2 and must be dropped. */
4646 if (isteq(list[hdr].n, ist("connection")) ||
Willy Tarreau053c1572019-02-01 16:13:59 +01004647 isteq(list[hdr].n, ist("host")) ||
Willy Tarreau80739692018-10-05 11:35:57 +02004648 isteq(list[hdr].n, ist("proxy-connection")) ||
4649 isteq(list[hdr].n, ist("keep-alive")) ||
4650 isteq(list[hdr].n, ist("upgrade")) ||
4651 isteq(list[hdr].n, ist("transfer-encoding")))
4652 continue;
4653
4654 if (isteq(list[hdr].n, ist("")))
4655 break; // end
4656
4657 if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) {
4658 /* output full */
4659 if (b_space_wraps(&h2c->mbuf))
4660 goto realign_again;
4661 goto full;
4662 }
4663 }
4664
4665 /* we may need to add END_STREAM if we have no body :
4666 * - request already closed, or :
4667 * - no transfer-encoding, and :
4668 * - no content-length or content-length:0
4669 * Fixme: this doesn't take into account CONNECT requests.
4670 */
4671 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4672 es_now = 1;
4673
4674 if (sl->flags & HTX_SL_F_BODYLESS)
4675 es_now = 1;
4676
Willy Tarreau927b88b2019-03-04 08:03:25 +01004677 if (!h2s->cs || h2s->cs->flags & CS_FL_SHW)
Willy Tarreau80739692018-10-05 11:35:57 +02004678 es_now = 1;
4679
4680 /* update the frame's size */
4681 h2_set_frame_size(outbuf.area, outbuf.data - 9);
4682
4683 if (es_now)
4684 outbuf.area[4] |= H2_F_HEADERS_END_STREAM;
4685
4686 /* commit the H2 response */
4687 b_add(&h2c->mbuf, outbuf.data);
4688 h2s->flags |= H2_SF_HEADERS_SENT;
4689 h2s->st = H2_SS_OPEN;
4690
Willy Tarreau80739692018-10-05 11:35:57 +02004691 if (es_now) {
4692 // trim any possibly pending data (eg: inconsistent content-length)
4693 h2s->flags |= H2_SF_ES_SENT;
4694 h2s->st = H2_SS_HLOC;
4695 }
4696
4697 /* remove all header blocks including the EOH and compute the
4698 * corresponding size.
4699 *
4700 * FIXME: We should remove everything when es_now is set.
4701 */
4702 ret = 0;
4703 idx = htx_get_head(htx);
4704 blk = htx_get_blk(htx, idx);
4705 while (blk != blk_end) {
4706 ret += htx_get_blksz(blk);
4707 blk = htx_remove_blk(htx, blk);
4708 }
4709
4710 if (blk_end && htx_get_blk_type(blk_end) == HTX_BLK_EOM)
4711 htx_remove_blk(htx, blk_end);
4712
4713 end:
4714 return ret;
4715 full:
4716 h2c->flags |= H2_CF_MUX_MFULL;
4717 h2s->flags |= H2_SF_BLK_MROOM;
4718 ret = 0;
4719 goto end;
4720 fail:
4721 /* unparsable HTX messages, too large ones to be produced in the local
4722 * list etc go here (unrecoverable errors).
4723 */
4724 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
4725 ret = 0;
4726 goto end;
4727}
4728
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004729/* Try to send a DATA frame matching HTTP response present in HTX structure
Willy Tarreau98de12a2018-12-12 07:03:00 +01004730 * present in <buf>, for stream <h2s>. Returns the number of bytes sent. The
4731 * caller must check the stream's status to detect any error which might have
4732 * happened subsequently to a successful send. Returns the number of data bytes
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004733 * consumed, or zero if nothing done. Note that EOD/EOM count for 1 byte.
4734 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01004735static size_t h2s_htx_frt_make_resp_data(struct h2s *h2s, struct buffer *buf, size_t count)
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004736{
4737 struct h2c *h2c = h2s->h2c;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004738 struct htx *htx;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004739 struct buffer outbuf;
4740 size_t total = 0;
4741 int es_now = 0;
4742 int bsize; /* htx block size */
4743 int fsize; /* h2 frame size */
4744 struct htx_blk *blk;
4745 enum htx_blk_type type;
4746 int idx;
4747
4748 if (h2c_mux_busy(h2c, h2s)) {
4749 h2s->flags |= H2_SF_BLK_MBUSY;
4750 goto end;
4751 }
4752
4753 if (!h2_get_buf(h2c, &h2c->mbuf)) {
4754 h2c->flags |= H2_CF_MUX_MALLOC;
4755 h2s->flags |= H2_SF_BLK_MROOM;
4756 goto end;
4757 }
4758
Willy Tarreau98de12a2018-12-12 07:03:00 +01004759 htx = htx_from_buf(buf);
4760
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004761 /* We only come here with HTX_BLK_DATA or HTX_BLK_EOD blocks. However,
4762 * while looping, we can meet an HTX_BLK_EOM block that we'll leave to
4763 * the caller to handle.
4764 */
4765
4766 new_frame:
Willy Tarreauee573762018-12-04 15:25:57 +01004767 if (!count || htx_is_empty(htx))
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004768 goto end;
4769
4770 idx = htx_get_head(htx);
4771 blk = htx_get_blk(htx, idx);
4772 type = htx_get_blk_type(blk); // DATA or EOD or EOM
4773 bsize = htx_get_blksz(blk);
4774 fsize = bsize;
4775
4776 if (type == HTX_BLK_EOD) {
4777 /* if we have an EOD, we're dealing with chunked data. We may
4778 * have a set of trailers after us that the caller will want to
4779 * deal with. Let's simply remove the EOD and return.
4780 */
4781 htx_remove_blk(htx, blk);
Willy Tarreauee573762018-12-04 15:25:57 +01004782 total++; // EOD counts as one byte
4783 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004784 goto end;
4785 }
Willy Tarreau7eeb10a2019-01-04 09:28:17 +01004786 else if (type == HTX_BLK_EOM) {
4787 if (h2s->flags & H2_SF_ES_SENT) {
4788 /* ES already sent */
4789 htx_remove_blk(htx, blk);
4790 total++; // EOM counts as one byte
4791 count--;
4792 goto end;
4793 }
4794 }
4795 else if (type != HTX_BLK_DATA)
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004796 goto end;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004797
4798 /* Perform some optimizations to reduce the number of buffer copies.
4799 * First, if the mux's buffer is empty and the htx area contains
4800 * exactly one data block of the same size as the requested count, and
4801 * this count fits within the frame size, the stream's window size, and
4802 * the connection's window size, then it's possible to simply swap the
4803 * caller's buffer with the mux's output buffer and adjust offsets and
4804 * length to match the entire DATA HTX block in the middle. In this
4805 * case we perform a true zero-copy operation from end-to-end. This is
4806 * the situation that happens all the time with large files. Second, if
4807 * this is not possible, but the mux's output buffer is empty, we still
4808 * have an opportunity to avoid the copy to the intermediary buffer, by
4809 * making the intermediary buffer's area point to the output buffer's
4810 * area. In this case we want to skip the HTX header to make sure that
4811 * copies remain aligned and that this operation remains possible all
4812 * the time. This goes for headers, data blocks and any data extracted
4813 * from the HTX blocks.
4814 */
4815 if (unlikely(fsize == count &&
4816 htx->used == 1 && type == HTX_BLK_DATA &&
4817 fsize <= h2s->mws && fsize <= h2c->mws && fsize <= h2c->mfs)) {
4818 void *old_area = h2c->mbuf.area;
4819
4820 if (b_data(&h2c->mbuf)) {
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004821 /* Too bad there are data left there. We're willing to memcpy/memmove
4822 * up to 1/4 of the buffer, which means that it's OK to copy a large
4823 * frame into a buffer containing few data if it needs to be realigned,
4824 * and that it's also OK to copy few data without realigning. Otherwise
4825 * we'll pretend the mbuf is full and wait for it to become empty.
Willy Tarreau98de12a2018-12-12 07:03:00 +01004826 */
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004827 if (fsize + 9 <= b_room(&h2c->mbuf) &&
4828 (b_data(&h2c->mbuf) <= b_size(&h2c->mbuf) / 4 ||
4829 (fsize <= b_size(&h2c->mbuf) / 4 && fsize + 9 <= b_contig_space(&h2c->mbuf))))
Willy Tarreau98de12a2018-12-12 07:03:00 +01004830 goto copy;
Willy Tarreau8ab128c2019-03-21 17:47:28 +01004831
Willy Tarreau98de12a2018-12-12 07:03:00 +01004832 h2c->flags |= H2_CF_MUX_MFULL;
4833 h2s->flags |= H2_SF_BLK_MROOM;
4834 goto end;
4835 }
4836
4837 /* map an H2 frame to the HTX block so that we can put the
4838 * frame header there.
4839 */
4840 h2c->mbuf.area = buf->area;
Olivier Houchard84cca662018-12-14 16:28:08 +01004841 h2c->mbuf.head = sizeof(struct htx) + blk->addr - 9;
Willy Tarreau98de12a2018-12-12 07:03:00 +01004842 h2c->mbuf.data = fsize + 9;
4843 outbuf.area = b_head(&h2c->mbuf);
4844
4845 /* prepend an H2 DATA frame header just before the DATA block */
4846 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4847 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4848 h2_set_frame_size(outbuf.area, fsize);
4849
4850 /* update windows */
4851 h2s->mws -= fsize;
4852 h2c->mws -= fsize;
4853
4854 /* and exchange with our old area */
4855 buf->area = old_area;
4856 buf->data = buf->head = 0;
4857 total += fsize;
4858 goto end;
4859 }
Willy Tarreau2fb1d4c2018-12-04 15:28:03 +01004860
Willy Tarreau98de12a2018-12-12 07:03:00 +01004861 copy:
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004862 /* for DATA and EOM we'll have to emit a frame, even if empty */
4863
4864 while (1) {
4865 outbuf.area = b_tail(&h2c->mbuf);
4866 outbuf.size = b_contig_space(&h2c->mbuf);
4867 outbuf.data = 0;
4868
4869 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
4870 break;
4871 realign_again:
4872 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
4873 }
4874
4875 if (outbuf.size < 9) {
4876 h2c->flags |= H2_CF_MUX_MFULL;
4877 h2s->flags |= H2_SF_BLK_MROOM;
4878 goto end;
4879 }
4880
4881 /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */
4882 memcpy(outbuf.area, "\x00\x00\x00\x00\x00", 5);
4883 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
4884 outbuf.data = 9;
4885
4886 /* we have in <fsize> the exact number of bytes we need to copy from
4887 * the HTX buffer. We need to check this against the connection's and
4888 * the stream's send windows, and to ensure that this fits in the max
4889 * frame size and in the buffer's available space minus 9 bytes (for
4890 * the frame header). The connection's flow control is applied last so
4891 * that we can use a separate list of streams which are immediately
4892 * unblocked on window opening. Note: we don't implement padding.
4893 */
4894
4895 /* EOM is presented with bsize==1 but would lead to the emission of an
4896 * empty frame, thus we force it to zero here.
4897 */
4898 if (type == HTX_BLK_EOM)
4899 bsize = fsize = 0;
4900
4901 if (!fsize)
4902 goto send_empty;
4903
4904 if (h2s->mws <= 0) {
4905 h2s->flags |= H2_SF_BLK_SFCTL;
Olivier Houchardbfe2a832019-05-10 14:02:21 +02004906 if (!LIST_ISEMPTY(&h2s->list))
4907 LIST_DEL_INIT(&h2s->list);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004908 goto end;
4909 }
4910
Willy Tarreauee573762018-12-04 15:25:57 +01004911 if (fsize > count)
4912 fsize = count;
4913
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004914 if (fsize > h2s->mws)
4915 fsize = h2s->mws; // >0
4916
4917 if (h2c->mfs && fsize > h2c->mfs)
4918 fsize = h2c->mfs; // >0
4919
4920 if (fsize + 9 > outbuf.size) {
4921 /* we have an opportunity for enlarging the too small
4922 * available space, let's try.
4923 * FIXME: is this really interesting to do? Maybe we'll
4924 * spend lots of time realigning instead of using two
4925 * frames.
4926 */
4927 if (b_space_wraps(&h2c->mbuf))
4928 goto realign_again;
4929 fsize = outbuf.size - 9;
4930
4931 if (fsize <= 0) {
4932 /* no need to send an empty frame here */
4933 h2c->flags |= H2_CF_MUX_MFULL;
4934 h2s->flags |= H2_SF_BLK_MROOM;
4935 goto end;
4936 }
4937 }
4938
4939 if (h2c->mws <= 0) {
4940 h2s->flags |= H2_SF_BLK_MFCTL;
4941 goto end;
4942 }
4943
4944 if (fsize > h2c->mws)
4945 fsize = h2c->mws;
4946
4947 /* now let's copy this this into the output buffer */
4948 memcpy(outbuf.area + 9, htx_get_blk_ptr(htx, blk), fsize);
Willy Tarreau0f799ca2018-12-04 15:20:11 +01004949 h2s->mws -= fsize;
4950 h2c->mws -= fsize;
Willy Tarreauee573762018-12-04 15:25:57 +01004951 count -= fsize;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004952
4953 send_empty:
4954 /* update the frame's size */
4955 h2_set_frame_size(outbuf.area, fsize);
4956
4957 /* FIXME: for now we only set the ES flag on empty DATA frames, once
4958 * meeting EOM. We should optimize this later.
4959 */
4960 if (type == HTX_BLK_EOM) {
Willy Tarreauee573762018-12-04 15:25:57 +01004961 total++; // EOM counts as one byte
4962 count--;
Willy Tarreau0c535fd2018-12-01 19:25:56 +01004963 es_now = 1;
4964 }
4965
4966 if (es_now)
4967 outbuf.area[4] |= H2_F_DATA_END_STREAM;
4968
4969 /* commit the H2 response */
4970 b_add(&h2c->mbuf, fsize + 9);
4971
4972 /* consume incoming HTX block, including EOM */
4973 total += fsize;
4974 if (fsize == bsize) {
4975 htx_remove_blk(htx, blk);
4976 if (fsize)
4977 goto new_frame;
4978 } else {
4979 /* we've truncated this block */
4980 htx_cut_data_blk(htx, blk, fsize);
4981 }
4982
4983 if (es_now) {
4984 if (h2s->st == H2_SS_OPEN)
4985 h2s->st = H2_SS_HLOC;
4986 else
4987 h2s_close(h2s);
4988
4989 h2s->flags |= H2_SF_ES_SENT;
4990 }
4991
4992 end:
4993 return total;
4994}
4995
Willy Tarreau1bb812f2019-01-04 10:56:26 +01004996/* Try to send a HEADERS frame matching HTX_BLK_TLR series of blocks present in
4997 * HTX message <htx> for the H2 stream <h2s>. Returns the number of bytes
4998 * processed. The caller must check the stream's status to detect any error
4999 * which might have happened subsequently to a successful send. The htx blocks
5000 * are automatically removed from the message. The htx message is assumed to be
5001 * valid since produced from the internal code. Processing stops when meeting
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005002 * the EOM, which is *not* removed. All trailers are processed at once and sent
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005003 * as a single frame. The ES flag is always set.
5004 */
5005static size_t h2s_htx_make_trailers(struct h2s *h2s, struct htx *htx)
5006{
5007 struct http_hdr list[MAX_HTTP_HDR];
5008 struct h2c *h2c = h2s->h2c;
5009 struct htx_blk *blk;
5010 struct htx_blk *blk_end;
5011 struct buffer outbuf;
5012 struct h1m h1m;
5013 enum htx_blk_type type;
5014 uint32_t size;
5015 int ret = 0;
5016 int hdr;
5017 int idx;
5018 void *start;
5019
5020 if (h2c_mux_busy(h2c, h2s)) {
5021 h2s->flags |= H2_SF_BLK_MBUSY;
5022 goto end;
5023 }
5024
5025 if (!h2_get_buf(h2c, &h2c->mbuf)) {
5026 h2c->flags |= H2_CF_MUX_MALLOC;
5027 h2s->flags |= H2_SF_BLK_MROOM;
5028 goto end;
5029 }
5030
5031 /* The principle is that we parse each and every trailers block using
5032 * the H1 headers parser, and append it to the list. We don't proceed
5033 * until EOM is met. blk_end will point to the EOM block.
5034 */
5035 hdr = 0;
5036 memset(list, 0, sizeof(list));
5037 blk_end = NULL;
5038
5039 for (idx = htx_get_head(htx); idx != -1; idx = htx_get_next(htx, idx)) {
5040 blk = htx_get_blk(htx, idx);
5041 type = htx_get_blk_type(blk);
5042
5043 if (type == HTX_BLK_UNUSED)
5044 continue;
5045
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005046 if (type != HTX_BLK_TLR)
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005047 break;
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005048
5049 if (unlikely(hdr >= sizeof(list)/sizeof(list[0]) - 1))
5050 goto fail;
5051
5052 size = htx_get_blksz(blk);
5053 start = htx_get_blk_ptr(htx, blk);
5054
5055 h1m.flags = H1_MF_HDRS_ONLY | H1_MF_TOLOWER;
5056 h1m.err_pos = 0;
5057 ret = h1_headers_to_hdr_list(start, start + size,
5058 list + hdr, sizeof(list)/sizeof(list[0]) - hdr,
5059 &h1m, NULL);
5060 if (ret < 0)
5061 goto fail;
5062
5063 /* ret == 0 if an incomplete trailers block was found (missing
5064 * empty line), or > 0 if it was found. We have to continue on
5065 * incomplete messages because the trailers block might be
5066 * incomplete.
5067 */
5068
5069 /* search the new end */
5070 while (hdr <= sizeof(list)/sizeof(list[0])) {
5071 if (!list[hdr].n.len)
5072 break;
5073 hdr++;
5074 }
5075 }
5076
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005077 if (list[hdr].n.len != 0)
5078 goto fail; // empty trailer not found: internal error
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005079
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005080 chunk_reset(&outbuf);
5081
5082 while (1) {
5083 outbuf.area = b_tail(&h2c->mbuf);
5084 outbuf.size = b_contig_space(&h2c->mbuf);
5085 outbuf.data = 0;
5086
5087 if (outbuf.size >= 9 || !b_space_wraps(&h2c->mbuf))
5088 break;
5089 realign_again:
5090 b_slow_realign(&h2c->mbuf, trash.area, b_data(&h2c->mbuf));
5091 }
5092
5093 if (outbuf.size < 9)
5094 goto full;
5095
5096 /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4,ES=1 */
5097 memcpy(outbuf.area, "\x00\x00\x00\x01\x05", 5);
5098 write_n32(outbuf.area + 5, h2s->id); // 4 bytes
5099 outbuf.data = 9;
5100
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005101 /* encode all headers */
5102 for (idx = 0; idx < hdr; idx++) {
5103 /* these ones do not exist in H2 or must not appear in
5104 * trailers and must be dropped.
5105 */
5106 if (isteq(list[idx].n, ist("host")) ||
5107 isteq(list[idx].n, ist("content-length")) ||
5108 isteq(list[idx].n, ist("connection")) ||
5109 isteq(list[idx].n, ist("proxy-connection")) ||
5110 isteq(list[idx].n, ist("keep-alive")) ||
5111 isteq(list[idx].n, ist("upgrade")) ||
5112 isteq(list[idx].n, ist("te")) ||
5113 isteq(list[idx].n, ist("transfer-encoding")))
5114 continue;
5115
5116 if (!hpack_encode_header(&outbuf, list[idx].n, list[idx].v)) {
5117 /* output full */
5118 if (b_space_wraps(&h2c->mbuf))
5119 goto realign_again;
5120 goto full;
5121 }
5122 }
5123
Willy Tarreau5121e5d2019-05-06 15:13:41 +02005124 if (outbuf.data == 9) {
5125 /* here we have a problem, we have nothing to emit (either we
5126 * received an empty trailers block followed or we removed its
5127 * contents above). Because of this we can't send a HEADERS
5128 * frame, so we have to cheat and instead send an empty DATA
5129 * frame conveying the ES flag.
Willy Tarreau67b8cae2019-02-21 18:16:35 +01005130 */
5131 outbuf.area[3] = H2_FT_DATA;
5132 outbuf.area[4] = H2_F_DATA_END_STREAM;
5133 }
5134
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005135 /* update the frame's size */
5136 h2_set_frame_size(outbuf.area, outbuf.data - 9);
5137
5138 /* commit the H2 response */
5139 b_add(&h2c->mbuf, outbuf.data);
5140 h2s->flags |= H2_SF_ES_SENT;
5141
5142 if (h2s->st == H2_SS_OPEN)
5143 h2s->st = H2_SS_HLOC;
5144 else
5145 h2s_close(h2s);
5146
5147 /* OK we could properly deliver the response */
5148 done:
Willy Tarreaufb07b3f2019-05-06 11:23:29 +02005149 /* remove all header blocks till the end and compute the corresponding size. */
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005150 ret = 0;
5151 idx = htx_get_head(htx);
5152 blk = htx_get_blk(htx, idx);
5153 while (blk != blk_end) {
5154 ret += htx_get_blksz(blk);
5155 blk = htx_remove_blk(htx, blk);
5156 }
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005157 end:
5158 return ret;
5159 full:
5160 h2c->flags |= H2_CF_MUX_MFULL;
5161 h2s->flags |= H2_SF_BLK_MROOM;
5162 ret = 0;
5163 goto end;
5164 fail:
5165 /* unparsable HTX messages, too large ones to be produced in the local
5166 * list etc go here (unrecoverable errors).
5167 */
5168 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5169 ret = 0;
5170 goto end;
5171}
5172
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005173/* Called from the upper layer, to subscribe to events, such as being able to send.
5174 * The <param> argument here is supposed to be a pointer to a wait_event struct
5175 * which will be passed to h2s->recv_wait or h2s->send_wait depending on the
5176 * event_type. The event_type must only be a combination of SUB_RETRY_RECV and
5177 * SUB_RETRY_SEND, other values will lead to -1 being returned. It always
5178 * returns 0 except for the error above.
5179 */
Olivier Houchard6ff20392018-07-17 18:46:31 +02005180static int h2_subscribe(struct conn_stream *cs, int event_type, void *param)
5181{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005182 struct wait_event *sw;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005183 struct h2s *h2s = cs->ctx;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005184 struct h2c *h2c = h2s->h2c;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005185
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005186 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005187 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005188 if (!(sw->events & SUB_RETRY_RECV)) {
5189 sw->events |= SUB_RETRY_RECV;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005190 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005191 h2s->recv_wait = sw;
Olivier Houchard4cf7fb12018-08-02 19:23:05 +02005192 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005193 event_type &= ~SUB_RETRY_RECV;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005194 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005195 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard6ff20392018-07-17 18:46:31 +02005196 sw = param;
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005197 if (!(sw->events & SUB_RETRY_SEND)) {
5198 sw->events |= SUB_RETRY_SEND;
Olivier Houchard8ae735d2018-09-11 18:24:28 +02005199 sw->handle = h2s;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005200 h2s->send_wait = sw;
Olivier Houchard9a0f5592019-04-15 19:22:24 +02005201 if (!(h2s->flags & H2_SF_BLK_SFCTL) &&
5202 LIST_ISEMPTY(&h2s->list)) {
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005203 if (h2s->flags & H2_SF_BLK_MFCTL)
5204 LIST_ADDQ(&h2c->fctl_list, &h2s->list);
5205 else
5206 LIST_ADDQ(&h2c->send_list, &h2s->list);
5207 }
Olivier Houcharde1c6dbc2018-08-01 17:06:43 +02005208 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005209 event_type &= ~SUB_RETRY_SEND;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005210 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005211 if (event_type != 0)
5212 return -1;
5213 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +02005214}
5215
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005216/* Called from the upper layer, to unsubscribe some events (undo h2_subscribe).
5217 * The <param> argument here is supposed to be a pointer to the same wait_event
5218 * struct that was passed to h2_subscribe() otherwise nothing will be changed.
5219 * It always returns zero.
5220 */
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005221static int h2_unsubscribe(struct conn_stream *cs, int event_type, void *param)
5222{
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005223 struct wait_event *sw;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005224 struct h2s *h2s = cs->ctx;
5225
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005226 if (event_type & SUB_RETRY_RECV) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005227 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005228 if (h2s->recv_wait == sw) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005229 sw->events &= ~SUB_RETRY_RECV;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005230 h2s->recv_wait = NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005231 }
5232 }
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005233 if (event_type & SUB_RETRY_SEND) {
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005234 sw = param;
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005235 if (h2s->send_wait == sw) {
5236 LIST_DEL(&h2s->list);
5237 LIST_INIT(&h2s->list);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005238 sw->events &= ~SUB_RETRY_SEND;
Olivier Houchard998410a2019-04-15 19:23:37 +02005239 /* We were about to send, make sure it does not happen */
5240 if (!LIST_ISEMPTY(&h2s->sending_list) &&
5241 h2s->send_wait != &h2s->wait_event) {
5242 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
5243 LIST_DEL_INIT(&h2s->sending_list);
5244 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005245 h2s->send_wait = NULL;
Olivier Houchard998410a2019-04-15 19:23:37 +02005246
Olivier Houchardd846c262018-10-19 17:24:29 +02005247 }
5248 }
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005249 return 0;
5250}
5251
5252
Olivier Houchard511efea2018-08-16 15:30:32 +02005253/* Called from the upper layer, to receive data */
5254static size_t h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
5255{
Olivier Houchard638b7992018-08-16 15:41:52 +02005256 struct h2s *h2s = cs->ctx;
Willy Tarreau082f5592018-11-25 08:03:32 +01005257 struct h2c *h2c = h2s->h2c;
Willy Tarreau86724e22018-12-01 23:19:43 +01005258 struct htx *h2s_htx = NULL;
5259 struct htx *buf_htx = NULL;
5260 struct htx_ret htx_ret;
Olivier Houchard511efea2018-08-16 15:30:32 +02005261 size_t ret = 0;
5262
5263 /* transfer possibly pending data to the upper layer */
Willy Tarreau86724e22018-12-01 23:19:43 +01005264 if (h2c->proxy->options2 & PR_O2_USE_HTX) {
5265 /* in HTX mode we ignore the count argument */
5266 h2s_htx = htx_from_buf(&h2s->rxbuf);
Olivier Houchard56b03482018-12-10 16:09:53 +01005267 if (htx_is_empty(h2s_htx)) {
Christopher Faulet37070b22019-02-14 15:12:14 +01005268 /* Here htx_to_buf() will set buffer data to 0 because
5269 * the HTX is empty.
5270 */
5271 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005272 goto end;
Olivier Houchard56b03482018-12-10 16:09:53 +01005273 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005274
5275 buf_htx = htx_from_buf(buf);
Christopher Fauleta413e952019-01-21 11:49:37 +01005276 count = htx_free_data_space(buf_htx);
5277 if (flags & CO_RFL_KEEP_RSV) {
5278 if (count <= global.tune.maxrewrite)
5279 goto end;
5280 count -= global.tune.maxrewrite;
5281 }
Willy Tarreau86724e22018-12-01 23:19:43 +01005282
Willy Tarreau0c22fa72018-12-04 15:21:35 +01005283 htx_ret = htx_xfer_blks(buf_htx, h2s_htx, count, HTX_BLK_EOM);
Willy Tarreau7196dd62019-03-05 10:51:11 +01005284
5285 if (h2s_htx->flags & HTX_FL_PARSING_ERROR)
5286 buf_htx->flags |= HTX_FL_PARSING_ERROR;
5287
Christopher Fauleteaf0d2a2019-02-18 16:04:35 +01005288 buf_htx->extra = (h2s_htx->extra ? (h2s_htx->data + h2s_htx->extra) : 0);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005289 htx_to_buf(buf_htx, buf);
5290 htx_to_buf(h2s_htx, &h2s->rxbuf);
Willy Tarreau86724e22018-12-01 23:19:43 +01005291 ret = htx_ret.ret;
5292 }
5293 else {
5294 ret = b_xfer(buf, &h2s->rxbuf, count);
5295 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005296
Christopher Faulet37070b22019-02-14 15:12:14 +01005297 end:
Olivier Houchard638b7992018-08-16 15:41:52 +02005298 if (b_data(&h2s->rxbuf))
Olivier Houchardd247be02018-12-06 16:22:29 +01005299 cs->flags |= (CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005300 else {
Olivier Houchardd247be02018-12-06 16:22:29 +01005301 cs->flags &= ~(CS_FL_RCV_MORE | CS_FL_WANT_ROOM);
Olivier Houchard511efea2018-08-16 15:30:32 +02005302 if (cs->flags & CS_FL_REOS)
5303 cs->flags |= CS_FL_EOS;
Olivier Houchard71748cb2018-12-17 14:16:46 +01005304 if (cs->flags & CS_FL_ERR_PENDING)
5305 cs->flags |= CS_FL_ERROR;
Olivier Houchard638b7992018-08-16 15:41:52 +02005306 if (b_size(&h2s->rxbuf)) {
5307 b_free(&h2s->rxbuf);
5308 offer_buffers(NULL, tasks_run_queue);
5309 }
Olivier Houchard511efea2018-08-16 15:30:32 +02005310 }
5311
Willy Tarreau082f5592018-11-25 08:03:32 +01005312 if (ret && h2c->dsi == h2s->id) {
5313 /* demux is blocking on this stream's buffer */
5314 h2c->flags &= ~H2_CF_DEM_SFULL;
Olivier Houchard3ca18bf2019-04-05 15:34:34 +02005315 h2c_restart_reading(h2c, 1);
Willy Tarreau082f5592018-11-25 08:03:32 +01005316 }
Christopher Faulet37070b22019-02-14 15:12:14 +01005317
Olivier Houchard511efea2018-08-16 15:30:32 +02005318 return ret;
5319}
5320
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005321/* stops all senders of this connection for example when the mux buffer is full.
5322 * They are moved from the sending_list to either fctl_list or send_list.
5323 */
Olivier Houchardd846c262018-10-19 17:24:29 +02005324static void h2_stop_senders(struct h2c *h2c)
5325{
5326 struct h2s *h2s, *h2s_back;
5327
Olivier Houchardd360ac62019-03-22 17:37:16 +01005328 list_for_each_entry_safe(h2s, h2s_back, &h2c->sending_list, sending_list) {
5329 LIST_DEL_INIT(&h2s->sending_list);
Willy Tarreaue73256f2019-03-25 18:02:54 +01005330 task_remove_from_tasklet_list((struct task *)h2s->send_wait->task);
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005331 h2s->send_wait->events |= SUB_RETRY_SEND;
Olivier Houchardd846c262018-10-19 17:24:29 +02005332 }
5333}
5334
Willy Tarreau749f5ca2019-03-21 19:19:36 +01005335/* Called from the upper layer, to send data from buffer <buf> for no more than
5336 * <count> bytes. Returns the number of bytes effectively sent. Some status
5337 * flags may be updated on the conn_stream.
5338 */
Christopher Fauletd44a9b32018-07-27 11:59:41 +02005339static size_t h2_snd_buf(struct conn_stream *cs, struct buffer *buf, size_t count, int flags)
Willy Tarreau62f52692017-10-08 23:01:42 +02005340{
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005341 struct h2s *h2s = cs->ctx;
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005342 size_t orig_count = count;
Willy Tarreau1dc41e72018-06-14 13:21:28 +02005343 size_t total = 0;
Willy Tarreau5dd17352018-06-14 13:33:30 +02005344 size_t ret;
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005345 struct htx *htx;
5346 struct htx_blk *blk;
5347 enum htx_blk_type btype;
5348 uint32_t bsize;
5349 int32_t idx;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005350
Olivier Houchardd360ac62019-03-22 17:37:16 +01005351 /* If we were not just woken because we wanted to send but couldn't,
5352 * and there's somebody else that is waiting to send, do nothing,
5353 * we will subscribe later and be put at the end of the list
5354 */
Olivier Houchard998410a2019-04-15 19:23:37 +02005355 if (LIST_ISEMPTY(&h2s->sending_list) &&
Olivier Houchardd360ac62019-03-22 17:37:16 +01005356 (!LIST_ISEMPTY(&h2s->h2c->send_list) || !LIST_ISEMPTY(&h2s->h2c->fctl_list)))
5357 return 0;
Olivier Houchard998410a2019-04-15 19:23:37 +02005358 LIST_DEL_INIT(&h2s->sending_list);
Olivier Houchardd360ac62019-03-22 17:37:16 +01005359
Olivier Houchard998410a2019-04-15 19:23:37 +02005360 /* We couldn't set it to NULL before, because we needed it in case
5361 * we had to cancel the tasklet
5362 */
5363 h2s->send_wait = NULL;
5364
Willy Tarreau6bf641a2018-10-08 09:43:03 +02005365 if (h2s->h2c->st0 < H2_CS_FRAME_H)
5366 return 0;
5367
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005368 /* htx will be enough to decide if we're using HTX or legacy */
5369 htx = (h2s->h2c->proxy->options2 & PR_O2_USE_HTX) ? htx_from_buf(buf) : NULL;
5370
Willy Tarreau0bad0432018-06-14 16:54:01 +02005371 if (!(h2s->flags & H2_SF_OUTGOING_DATA) && count)
Willy Tarreauc4312d32017-11-07 12:01:53 +01005372 h2s->flags |= H2_SF_OUTGOING_DATA;
5373
Willy Tarreau751f2d02018-10-05 09:35:00 +02005374 if (h2s->id == 0) {
5375 int32_t id = h2c_get_next_sid(h2s->h2c);
5376
5377 if (id < 0) {
Willy Tarreau751f2d02018-10-05 09:35:00 +02005378 cs->flags |= CS_FL_ERROR;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005379 return 0;
5380 }
5381
5382 eb32_delete(&h2s->by_id);
5383 h2s->by_id.key = h2s->id = id;
5384 h2s->h2c->max_id = id;
Willy Tarreaud64a3eb2019-01-23 10:22:21 +01005385 h2s->h2c->nb_reserved--;
Willy Tarreau751f2d02018-10-05 09:35:00 +02005386 eb32_insert(&h2s->h2c->streams_by_id, &h2s->by_id);
5387 }
5388
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005389 if (htx) {
Willy Tarreau2b778482019-05-06 15:00:22 +02005390 while (h2s->st < H2_SS_HLOC && !(h2s->flags & H2_SF_BLK_ANY) &&
Willy Tarreauc14999b2018-12-06 14:09:09 +01005391 count && !htx_is_empty(htx)) {
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005392 idx = htx_get_head(htx);
5393 blk = htx_get_blk(htx, idx);
5394 btype = htx_get_blk_type(blk);
5395 bsize = htx_get_blksz(blk);
5396
5397 switch (btype) {
Willy Tarreau80739692018-10-05 11:35:57 +02005398 case HTX_BLK_REQ_SL:
5399 /* start-line before headers */
5400 ret = h2s_htx_bck_make_req_headers(h2s, htx);
5401 if (ret > 0) {
5402 total += ret;
5403 count -= ret;
5404 if (ret < bsize)
5405 goto done;
5406 }
5407 break;
5408
Willy Tarreau115e83b2018-12-01 19:17:53 +01005409 case HTX_BLK_RES_SL:
5410 /* start-line before headers */
5411 ret = h2s_htx_frt_make_resp_headers(h2s, htx);
5412 if (ret > 0) {
5413 total += ret;
5414 count -= ret;
5415 if (ret < bsize)
5416 goto done;
5417 }
5418 break;
5419
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005420 case HTX_BLK_DATA:
5421 case HTX_BLK_EOD:
5422 case HTX_BLK_EOM:
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005423 /* all these cause the emission of a DATA frame (possibly empty).
5424 * This EOM necessarily is one before trailers, as the EOM following
5425 * trailers would have been consumed by the trailers parser.
5426 */
Willy Tarreau98de12a2018-12-12 07:03:00 +01005427 ret = h2s_htx_frt_make_resp_data(h2s, buf, count);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005428 if (ret > 0) {
Willy Tarreau98de12a2018-12-12 07:03:00 +01005429 htx = htx_from_buf(buf);
Willy Tarreau0c535fd2018-12-01 19:25:56 +01005430 total += ret;
5431 count -= ret;
5432 if (ret < bsize)
5433 goto done;
5434 }
5435 break;
5436
Willy Tarreau1bb812f2019-01-04 10:56:26 +01005437 case HTX_BLK_TLR:
5438 /* This is the first trailers block, all the subsequent ones AND
5439 * the EOM will be swallowed by the parser.
5440 */
5441 ret = h2s_htx_make_trailers(h2s, htx);
5442 if (ret > 0) {
5443 total += ret;
5444 count -= ret;
5445 if (ret < bsize)
5446 goto done;
5447 }
5448 break;
5449
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005450 default:
5451 htx_remove_blk(htx, blk);
5452 total += bsize;
5453 count -= bsize;
5454 break;
5455 }
5456 }
5457 goto done;
5458 }
5459
5460 /* legacy transfer mode */
Willy Tarreaua40704a2018-09-11 13:52:04 +02005461 while (h2s->h1m.state < H1_MSG_DONE && count) {
Willy Tarreau001823c2018-09-12 17:25:32 +02005462 if (h2s->h1m.state <= H1_MSG_LAST_LF) {
Willy Tarreau80739692018-10-05 11:35:57 +02005463 if (h2s->h2c->flags & H2_CF_IS_BACK)
5464 ret = -1;
5465 else
5466 ret = h2s_frt_make_resp_headers(h2s, buf, total, count);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005467 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005468 else if (h2s->h1m.state < H1_MSG_TRAILERS) {
Willy Tarreau0bad0432018-06-14 16:54:01 +02005469 ret = h2s_frt_make_resp_data(h2s, buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005470 }
Willy Tarreaua40704a2018-09-11 13:52:04 +02005471 else if (h2s->h1m.state == H1_MSG_TRAILERS) {
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005472 /* consume the trailers if any (we don't forward them for now) */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005473 ret = h1_measure_trailers(buf, total, count);
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005474
Willy Tarreau5dd17352018-06-14 13:33:30 +02005475 if (unlikely((int)ret <= 0)) {
5476 if ((int)ret < 0)
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005477 h2s_error(h2s, H2_ERR_INTERNAL_ERROR);
5478 break;
5479 }
Willy Tarreau35a62702018-02-27 15:37:25 +01005480 // trim any possibly pending data (eg: extra CR-LF, ...)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005481 total += count;
5482 count = 0;
Willy Tarreaua40704a2018-09-11 13:52:04 +02005483 h2s->h1m.state = H1_MSG_DONE;
Willy Tarreau9d89ac82017-10-31 17:15:59 +01005484 break;
Willy Tarreauc652dbd2017-10-19 11:16:37 +02005485 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005486 else {
Willy Tarreauec988c72018-12-19 18:00:29 +01005487 cs_set_error(cs);
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005488 break;
5489 }
Willy Tarreau0bad0432018-06-14 16:54:01 +02005490
5491 total += ret;
5492 count -= ret;
5493
Willy Tarreau2b778482019-05-06 15:00:22 +02005494 if (h2s->st >= H2_SS_HLOC)
Willy Tarreau0bad0432018-06-14 16:54:01 +02005495 break;
5496
5497 if (h2s->flags & H2_SF_BLK_ANY)
5498 break;
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005499 }
5500
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005501 done:
Willy Tarreau2b778482019-05-06 15:00:22 +02005502 if (h2s->st >= H2_SS_HLOC) {
Willy Tarreau00610962018-07-19 10:58:28 +02005503 /* trim any possibly pending data after we close (extra CR-LF,
5504 * unprocessed trailers, abnormal extra data, ...)
5505 */
Willy Tarreau0bad0432018-06-14 16:54:01 +02005506 total += count;
5507 count = 0;
Willy Tarreau00610962018-07-19 10:58:28 +02005508 }
5509
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005510 /* RST are sent similarly to frame acks */
Willy Tarreau02492192017-12-07 15:59:29 +01005511 if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) {
Willy Tarreauec988c72018-12-19 18:00:29 +01005512 cs_set_error(cs);
Willy Tarreau8c0ea7d2017-11-10 10:05:24 +01005513 if (h2s_send_rst_stream(h2s->h2c, h2s) > 0)
Willy Tarreau00dd0782018-03-01 16:31:34 +01005514 h2s_close(h2s);
Willy Tarreauc6795ca2017-11-07 09:43:06 +01005515 }
5516
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005517 if (htx) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005518 htx_to_buf(htx, buf);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005519 } else {
5520 b_del(buf, total);
5521 }
Olivier Houchardd846c262018-10-19 17:24:29 +02005522
5523 /* The mux is full, cancel the pending tasks */
5524 if ((h2s->h2c->flags & H2_CF_MUX_BLOCK_ANY) ||
5525 (h2s->flags & H2_SF_BLK_MBUSY))
5526 h2_stop_senders(h2s->h2c);
Willy Tarreaubcd3bb32018-12-01 18:59:00 +01005527
Olivier Houchard8122a8d2018-12-03 19:13:29 +01005528 /* If we're running HTX, and we read the whole buffer, then pretend
5529 * we read exactly what the caller specified, as with HTX the caller
5530 * will always give the buffer size, instead of the amount of data
5531 * available.
5532 */
5533 if (htx && !b_data(buf))
5534 total = orig_count;
5535
Olivier Houchard7505f942018-08-21 18:10:44 +02005536 if (total > 0) {
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005537 if (!(h2s->h2c->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005538 tasklet_wakeup(h2s->h2c->wait_event.task);
Olivier Houchardd846c262018-10-19 17:24:29 +02005539
Olivier Houchard7505f942018-08-21 18:10:44 +02005540 }
Olivier Houchard6dea2ee2018-12-19 18:16:17 +01005541 /* If we're waiting for flow control, and we got a shutr on the
5542 * connection, we will never be unlocked, so add an error on
5543 * the conn_stream.
5544 */
5545 if (conn_xprt_read0_pending(h2s->h2c->conn) &&
5546 !b_data(&h2s->h2c->dbuf) &&
5547 (h2s->flags & (H2_SF_BLK_SFCTL | H2_SF_BLK_MFCTL))) {
5548 if (cs->flags & CS_FL_EOS)
5549 cs->flags |= CS_FL_ERROR;
5550 else
5551 cs->flags |= CS_FL_ERR_PENDING;
5552 }
Olivier Houchard998410a2019-04-15 19:23:37 +02005553 if (total > 0) {
Olivier Houchardd360ac62019-03-22 17:37:16 +01005554 /* Ok we managed to send something, leave the send_list */
Olivier Houchardd360ac62019-03-22 17:37:16 +01005555 LIST_DEL_INIT(&h2s->list);
5556 }
Willy Tarreau9e5ae1d2017-10-17 19:58:20 +02005557 return total;
Willy Tarreau62f52692017-10-08 23:01:42 +02005558}
5559
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005560/* for debugging with CLI's "show fd" command */
Willy Tarreau83061a82018-07-13 11:56:34 +02005561static void h2_show_fd(struct buffer *msg, struct connection *conn)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005562{
Willy Tarreau3d2ee552018-12-19 14:12:10 +01005563 struct h2c *h2c = conn->ctx;
Willy Tarreau987c0632018-12-18 10:32:05 +01005564 struct h2s *h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005565 struct eb32_node *node;
5566 int fctl_cnt = 0;
5567 int send_cnt = 0;
5568 int tree_cnt = 0;
5569 int orph_cnt = 0;
5570
5571 if (!h2c)
5572 return;
5573
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005574 list_for_each_entry(h2s, &h2c->fctl_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005575 fctl_cnt++;
5576
Olivier Houchardfa8aa862018-10-10 18:25:41 +02005577 list_for_each_entry(h2s, &h2c->send_list, list)
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005578 send_cnt++;
5579
Willy Tarreau3af37712018-12-18 14:34:41 +01005580 h2s = NULL;
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005581 node = eb32_first(&h2c->streams_by_id);
5582 while (node) {
5583 h2s = container_of(node, struct h2s, by_id);
5584 tree_cnt++;
5585 if (!h2s->cs)
5586 orph_cnt++;
5587 node = eb32_next(node);
5588 }
5589
Willy Tarreau987c0632018-12-18 10:32:05 +01005590 chunk_appendf(msg, " h2c.st0=%d .err=%d .maxid=%d .lastid=%d .flg=0x%04x"
5591 " .nbst=%u .nbcs=%u .fctl_cnt=%d .send_cnt=%d .tree_cnt=%d"
5592 " .orph_cnt=%d .sub=%d .dsi=%d .dbuf=%u@%p+%u/%u .msi=%d .mbuf=%u@%p+%u/%u",
Willy Tarreau616ac812018-07-24 14:12:42 +02005593 h2c->st0, h2c->errcode, h2c->max_id, h2c->last_sid, h2c->flags,
5594 h2c->nb_streams, h2c->nb_cs, fctl_cnt, send_cnt, tree_cnt, orph_cnt,
Willy Tarreau4f6516d2018-12-19 13:59:17 +01005595 h2c->wait_event.events, h2c->dsi,
Willy Tarreau987c0632018-12-18 10:32:05 +01005596 (unsigned int)b_data(&h2c->dbuf), b_orig(&h2c->dbuf),
5597 (unsigned int)b_head_ofs(&h2c->dbuf), (unsigned int)b_size(&h2c->dbuf),
5598 h2c->msi,
5599 (unsigned int)b_data(&h2c->mbuf), b_orig(&h2c->mbuf),
5600 (unsigned int)b_head_ofs(&h2c->mbuf), (unsigned int)b_size(&h2c->mbuf));
5601
5602 if (h2s) {
5603 chunk_appendf(msg, " last_h2s=%p .id=%d .flg=0x%04x .rxbuf=%u@%p+%u/%u .cs=%p",
5604 h2s, h2s->id, h2s->flags,
5605 (unsigned int)b_data(&h2s->rxbuf), b_orig(&h2s->rxbuf),
5606 (unsigned int)b_head_ofs(&h2s->rxbuf), (unsigned int)b_size(&h2s->rxbuf),
5607 h2s->cs);
5608 if (h2s->cs)
5609 chunk_appendf(msg, " .cs.flg=0x%08x .cs.data=%p",
5610 h2s->cs->flags, h2s->cs->data);
5611 }
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005612}
Willy Tarreau62f52692017-10-08 23:01:42 +02005613
5614/*******************************************************/
5615/* functions below are dedicated to the config parsers */
5616/*******************************************************/
5617
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005618/* config parser for global "tune.h2.header-table-size" */
5619static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx,
5620 struct proxy *defpx, const char *file, int line,
5621 char **err)
5622{
5623 if (too_many_args(1, args, err, NULL))
5624 return -1;
5625
5626 h2_settings_header_table_size = atoi(args[1]);
5627 if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) {
5628 memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]);
5629 return -1;
5630 }
5631 return 0;
5632}
Willy Tarreau62f52692017-10-08 23:01:42 +02005633
Willy Tarreaue6baec02017-07-27 11:45:11 +02005634/* config parser for global "tune.h2.initial-window-size" */
5635static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx,
5636 struct proxy *defpx, const char *file, int line,
5637 char **err)
5638{
5639 if (too_many_args(1, args, err, NULL))
5640 return -1;
5641
5642 h2_settings_initial_window_size = atoi(args[1]);
5643 if (h2_settings_initial_window_size < 0) {
5644 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5645 return -1;
5646 }
5647 return 0;
5648}
5649
Willy Tarreau5242ef82017-07-27 11:47:28 +02005650/* config parser for global "tune.h2.max-concurrent-streams" */
5651static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx,
5652 struct proxy *defpx, const char *file, int line,
5653 char **err)
5654{
5655 if (too_many_args(1, args, err, NULL))
5656 return -1;
5657
5658 h2_settings_max_concurrent_streams = atoi(args[1]);
Willy Tarreau5a490b62019-01-31 10:39:51 +01005659 if ((int)h2_settings_max_concurrent_streams < 0) {
Willy Tarreau5242ef82017-07-27 11:47:28 +02005660 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
5661 return -1;
5662 }
5663 return 0;
5664}
5665
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005666/* config parser for global "tune.h2.max-frame-size" */
5667static int h2_parse_max_frame_size(char **args, int section_type, struct proxy *curpx,
5668 struct proxy *defpx, const char *file, int line,
5669 char **err)
5670{
5671 if (too_many_args(1, args, err, NULL))
5672 return -1;
5673
5674 h2_settings_max_frame_size = atoi(args[1]);
5675 if (h2_settings_max_frame_size < 16384 || h2_settings_max_frame_size > 16777215) {
5676 memprintf(err, "'%s' expects a numeric value between 16384 and 16777215.", args[0]);
5677 return -1;
5678 }
5679 return 0;
5680}
5681
Willy Tarreau62f52692017-10-08 23:01:42 +02005682
5683/****************************************/
5684/* MUX initialization and instanciation */
5685/***************************************/
5686
5687/* The mux operations */
Willy Tarreau680b2bd2018-11-27 07:30:17 +01005688static const struct mux_ops h2_ops = {
Willy Tarreau62f52692017-10-08 23:01:42 +02005689 .init = h2_init,
Olivier Houchard21df6cc2018-09-14 23:21:44 +02005690 .wake = h2_wake,
Willy Tarreau62f52692017-10-08 23:01:42 +02005691 .snd_buf = h2_snd_buf,
Olivier Houchard511efea2018-08-16 15:30:32 +02005692 .rcv_buf = h2_rcv_buf,
Olivier Houchard6ff20392018-07-17 18:46:31 +02005693 .subscribe = h2_subscribe,
Olivier Houchard83a0cd82018-09-28 17:57:58 +02005694 .unsubscribe = h2_unsubscribe,
Willy Tarreau62f52692017-10-08 23:01:42 +02005695 .attach = h2_attach,
Willy Tarreaufafd3982018-11-18 21:29:20 +01005696 .get_first_cs = h2_get_first_cs,
Willy Tarreau62f52692017-10-08 23:01:42 +02005697 .detach = h2_detach,
Olivier Houchard060ed432018-11-06 16:32:42 +01005698 .destroy = h2_destroy,
Olivier Houchardd540b362018-11-05 18:37:53 +01005699 .avail_streams = h2_avail_streams,
Willy Tarreau00f18a32019-01-26 12:19:01 +01005700 .used_streams = h2_used_streams,
Willy Tarreau62f52692017-10-08 23:01:42 +02005701 .shutr = h2_shutr,
5702 .shutw = h2_shutw,
Willy Tarreaue3f36cd2018-03-30 14:43:13 +02005703 .show_fd = h2_show_fd,
Willy Tarreau28f1cb92017-12-20 16:14:44 +01005704 .flags = MX_FL_CLEAN_ABRT,
Willy Tarreau62f52692017-10-08 23:01:42 +02005705 .name = "H2",
5706};
5707
Christopher Faulet32f61c02018-04-10 14:33:41 +02005708/* PROTO selection : this mux registers PROTO token "h2" */
5709static struct mux_proto_list mux_proto_h2 =
Willy Tarreauf8957272018-10-03 10:25:20 +02005710 { .token = IST("h2"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_FE, .mux = &h2_ops };
Willy Tarreau62f52692017-10-08 23:01:42 +02005711
Willy Tarreau0108d902018-11-25 19:14:37 +01005712INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2);
5713
Christopher Faulet9b579102019-04-11 22:52:25 +02005714
5715/* The mux operations */
5716static const struct mux_ops h2_htx_ops = {
5717 .init = h2_init,
5718 .wake = h2_wake,
5719 .snd_buf = h2_snd_buf,
5720 .rcv_buf = h2_rcv_buf,
5721 .subscribe = h2_subscribe,
5722 .unsubscribe = h2_unsubscribe,
5723 .attach = h2_attach,
5724 .get_first_cs = h2_get_first_cs,
5725 .detach = h2_detach,
5726 .destroy = h2_destroy,
5727 .avail_streams = h2_avail_streams,
5728 .used_streams = h2_used_streams,
5729 .shutr = h2_shutr,
5730 .shutw = h2_shutw,
5731 .show_fd = h2_show_fd,
Christopher Faulet9f38f5a2019-04-03 09:53:32 +02005732 .flags = MX_FL_CLEAN_ABRT|MX_FL_HTX,
Christopher Faulet9b579102019-04-11 22:52:25 +02005733 .name = "H2",
5734};
5735
Willy Tarreauf8957272018-10-03 10:25:20 +02005736static struct mux_proto_list mux_proto_h2_htx =
Christopher Faulet9b579102019-04-11 22:52:25 +02005737 { .token = IST("h2"), .mode = PROTO_MODE_HTX, .side = PROTO_SIDE_BOTH, .mux = &h2_htx_ops };
Willy Tarreauf8957272018-10-03 10:25:20 +02005738
5739INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h2_htx);
5740
Willy Tarreau62f52692017-10-08 23:01:42 +02005741/* config keyword parsers */
5742static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreaufe20e5b2017-07-27 11:42:14 +02005743 { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size },
Willy Tarreaue6baec02017-07-27 11:45:11 +02005744 { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size },
Willy Tarreau5242ef82017-07-27 11:47:28 +02005745 { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams },
Willy Tarreaua24b35c2019-02-21 13:24:36 +01005746 { CFG_GLOBAL, "tune.h2.max-frame-size", h2_parse_max_frame_size },
Willy Tarreau62f52692017-10-08 23:01:42 +02005747 { 0, NULL, NULL }
5748}};
5749
Willy Tarreau0108d902018-11-25 19:14:37 +01005750INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);