Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 15 | #include <common/h2.h> |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 16 | #include <common/hpack-dec.h> |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 17 | #include <common/hpack-enc.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 18 | #include <common/hpack-tbl.h> |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 19 | #include <common/net_helper.h> |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 20 | #include <proto/applet.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 21 | #include <proto/connection.h> |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 22 | #include <proto/h1.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 23 | #include <proto/stream.h> |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 24 | #include <types/session.h> |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 25 | #include <eb32tree.h> |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 26 | |
| 27 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 28 | /* dummy streams returned for idle and closed states */ |
| 29 | static const struct h2s *h2_closed_stream; |
| 30 | static const struct h2s *h2_idle_stream; |
| 31 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 32 | /* the h2c connection pool */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 33 | static struct pool_head *pool_head_h2c; |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 34 | /* the h2s stream pool */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 35 | static struct pool_head *pool_head_h2s; |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 36 | |
| 37 | /* Connection flags (32 bit), in h2c->flags */ |
| 38 | #define H2_CF_NONE 0x00000000 |
| 39 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 40 | /* Flags indicating why writing to the mux is blocked. */ |
| 41 | #define H2_CF_MUX_MALLOC 0x00000001 // mux blocked on lack of connection's mux buffer |
| 42 | #define H2_CF_MUX_MFULL 0x00000002 // mux blocked on connection's mux buffer full |
| 43 | #define H2_CF_MUX_BLOCK_ANY 0x00000003 // aggregate of the mux flags above |
| 44 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 45 | /* Flags indicating why writing to the demux is blocked. |
| 46 | * The first two ones directly affect the ability for the mux to receive data |
| 47 | * from the connection. The other ones affect the mux's ability to demux |
| 48 | * received data. |
| 49 | */ |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 50 | #define H2_CF_DEM_DALLOC 0x00000004 // demux blocked on lack of connection's demux buffer |
| 51 | #define H2_CF_DEM_DFULL 0x00000008 // demux blocked on connection's demux buffer full |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 52 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 53 | #define H2_CF_DEM_MBUSY 0x00000010 // demux blocked on connection's mux side busy |
| 54 | #define H2_CF_DEM_MROOM 0x00000020 // demux blocked on lack of room in mux buffer |
| 55 | #define H2_CF_DEM_SALLOC 0x00000040 // demux blocked on lack of stream's request buffer |
| 56 | #define H2_CF_DEM_SFULL 0x00000080 // demux blocked on stream request buffer full |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 57 | #define H2_CF_DEM_BLOCK_ANY 0x000000F0 // aggregate of the demux flags above except DALLOC/DFULL |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 58 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 59 | /* other flags */ |
| 60 | #define H2_CF_GOAWAY_SENT 0x00000100 // a GOAWAY frame was successfully sent |
| 61 | #define H2_CF_GOAWAY_FAILED 0x00000200 // a GOAWAY frame failed to be sent |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 62 | #define H2_CF_WAIT_FOR_HS 0x00000400 // We did check that at least a stream was waiting for handshake |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 63 | |
| 64 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 65 | /* H2 connection state, in h2c->st0 */ |
| 66 | enum h2_cs { |
| 67 | H2_CS_PREFACE, // init done, waiting for connection preface |
| 68 | H2_CS_SETTINGS1, // preface OK, waiting for first settings frame |
| 69 | H2_CS_FRAME_H, // first settings frame ok, waiting for frame header |
| 70 | H2_CS_FRAME_P, // frame header OK, waiting for frame payload |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 71 | H2_CS_FRAME_A, // frame payload OK, trying to send ACK frame |
| 72 | H2_CS_FRAME_E, // frame payload OK, trying to send RST frame |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 73 | H2_CS_ERROR, // send GOAWAY(errcode) and close the connection ASAP |
| 74 | H2_CS_ERROR2, // GOAWAY(errcode) sent, close the connection ASAP |
| 75 | H2_CS_ENTRIES // must be last |
| 76 | } __attribute__((packed)); |
| 77 | |
| 78 | /* H2 connection descriptor */ |
| 79 | struct h2c { |
| 80 | struct connection *conn; |
| 81 | |
| 82 | enum h2_cs st0; /* mux state */ |
| 83 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 84 | |
| 85 | /* 16 bit hole here */ |
| 86 | uint32_t flags; /* connection flags: H2_CF_* */ |
| 87 | int32_t max_id; /* highest ID known on this connection, <0 before preface */ |
| 88 | uint32_t rcvd_c; /* newly received data to ACK for the connection */ |
| 89 | uint32_t rcvd_s; /* newly received data to ACK for the current stream (dsi) */ |
| 90 | |
| 91 | /* states for the demux direction */ |
| 92 | struct hpack_dht *ddht; /* demux dynamic header table */ |
| 93 | struct buffer *dbuf; /* demux buffer */ |
| 94 | |
| 95 | int32_t dsi; /* demux stream ID (<0 = idle) */ |
| 96 | int32_t dfl; /* demux frame length (if dsi >= 0) */ |
| 97 | int8_t dft; /* demux frame type (if dsi >= 0) */ |
| 98 | int8_t dff; /* demux frame flags (if dsi >= 0) */ |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 99 | uint8_t dpl; /* demux pad length (part of dfl), init to 0 */ |
| 100 | /* 8 bit hole here */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 101 | int32_t last_sid; /* last processed stream ID for GOAWAY, <0 before preface */ |
| 102 | |
| 103 | /* states for the mux direction */ |
| 104 | struct buffer *mbuf; /* mux buffer */ |
| 105 | int32_t msi; /* mux stream ID (<0 = idle) */ |
| 106 | int32_t mfl; /* mux frame length (if dsi >= 0) */ |
| 107 | int8_t mft; /* mux frame type (if dsi >= 0) */ |
| 108 | int8_t mff; /* mux frame flags (if dsi >= 0) */ |
| 109 | /* 16 bit hole here */ |
| 110 | int32_t miw; /* mux initial window size for all new streams */ |
| 111 | int32_t mws; /* mux window size. Can be negative. */ |
| 112 | int32_t mfs; /* mux's max frame size */ |
| 113 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 114 | int timeout; /* idle timeout duration in ticks */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 115 | int shut_timeout; /* idle timeout duration in ticks after GOAWAY was sent */ |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 116 | unsigned int nb_streams; /* number of streams in the tree */ |
| 117 | /* 32 bit hole here */ |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 118 | struct task *task; /* timeout management task */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 119 | struct eb_root streams_by_id; /* all active streams by their ID */ |
| 120 | struct list send_list; /* list of blocked streams requesting to send */ |
| 121 | struct list fctl_list; /* list of streams blocked by connection's fctl */ |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 122 | struct buffer_wait buf_wait; /* wait list for buffer allocations */ |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 123 | }; |
| 124 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 125 | /* H2 stream state, in h2s->st */ |
| 126 | enum h2_ss { |
| 127 | H2_SS_IDLE = 0, // idle |
| 128 | H2_SS_RLOC, // reserved(local) |
| 129 | H2_SS_RREM, // reserved(remote) |
| 130 | H2_SS_OPEN, // open |
| 131 | H2_SS_HREM, // half-closed(remote) |
| 132 | H2_SS_HLOC, // half-closed(local) |
Willy Tarreau | 96060ba | 2017-10-16 18:34:34 +0200 | [diff] [blame] | 133 | H2_SS_ERROR, // an error needs to be sent using RST_STREAM |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 134 | H2_SS_CLOSED, // closed |
| 135 | H2_SS_ENTRIES // must be last |
| 136 | } __attribute__((packed)); |
| 137 | |
| 138 | /* HTTP/2 stream flags (32 bit), in h2s->flags */ |
| 139 | #define H2_SF_NONE 0x00000000 |
| 140 | #define H2_SF_ES_RCVD 0x00000001 |
| 141 | #define H2_SF_ES_SENT 0x00000002 |
| 142 | |
| 143 | #define H2_SF_RST_RCVD 0x00000004 // received RST_STREAM |
| 144 | #define H2_SF_RST_SENT 0x00000008 // sent RST_STREAM |
| 145 | |
Willy Tarreau | 2e5b60e | 2017-09-25 11:49:03 +0200 | [diff] [blame] | 146 | /* stream flags indicating the reason the stream is blocked */ |
| 147 | #define H2_SF_BLK_MBUSY 0x00000010 // blocked waiting for mux access (transient) |
| 148 | #define H2_SF_BLK_MROOM 0x00000020 // blocked waiting for room in the mux |
| 149 | #define H2_SF_BLK_MFCTL 0x00000040 // blocked due to mux fctl |
| 150 | #define H2_SF_BLK_SFCTL 0x00000080 // blocked due to stream fctl |
| 151 | #define H2_SF_BLK_ANY 0x000000F0 // any of the reasons above |
| 152 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 153 | /* stream flags indicating how data is supposed to be sent */ |
| 154 | #define H2_SF_DATA_CLEN 0x00000100 // data sent using content-length |
| 155 | #define H2_SF_DATA_CHNK 0x00000200 // data sent using chunked-encoding |
| 156 | |
| 157 | /* step we're currently in when sending chunks. This is needed because we may |
| 158 | * have to transfer chunks as large as a full buffer so there's no room left |
| 159 | * for size nor crlf around. |
| 160 | */ |
| 161 | #define H2_SF_CHNK_SIZE 0x00000000 // trying to send chunk size |
| 162 | #define H2_SF_CHNK_DATA 0x00000400 // trying to send chunk data |
| 163 | #define H2_SF_CHNK_CRLF 0x00000800 // trying to send chunk crlf after data |
| 164 | |
| 165 | #define H2_SF_CHNK_MASK 0x00000C00 // trying to send chunk size |
| 166 | |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 167 | #define H2_SF_HEADERS_SENT 0x00001000 // a HEADERS frame was sent for this stream |
Willy Tarreau | c4312d3 | 2017-11-07 12:01:53 +0100 | [diff] [blame] | 168 | #define H2_SF_OUTGOING_DATA 0x00002000 // set whenever we've seen outgoing data |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 169 | |
Willy Tarreau | 1831264 | 2017-10-11 07:57:07 +0200 | [diff] [blame] | 170 | /* H2 stream descriptor, describing the stream as it appears in the H2C, and as |
| 171 | * it is being processed in the internal HTTP representation (H1 for now). |
| 172 | */ |
| 173 | struct h2s { |
| 174 | struct conn_stream *cs; |
| 175 | struct h2c *h2c; |
| 176 | struct h1m req, res; /* request and response parser state for H1 */ |
| 177 | struct eb32_node by_id; /* place in h2c's streams_by_id */ |
| 178 | struct list list; /* position in active/blocked lists if blocked>0 */ |
| 179 | int32_t id; /* stream ID */ |
| 180 | uint32_t flags; /* H2_SF_* */ |
| 181 | int mws; /* mux window size for this stream */ |
| 182 | enum h2_err errcode; /* H2 err code (H2_ERR_*) */ |
| 183 | enum h2_ss st; |
| 184 | }; |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 185 | |
Willy Tarreau | c640514 | 2017-09-21 20:23:50 +0200 | [diff] [blame] | 186 | /* descriptor for an h2 frame header */ |
| 187 | struct h2_fh { |
| 188 | uint32_t len; /* length, host order, 24 bits */ |
| 189 | uint32_t sid; /* stream id, host order, 31 bits */ |
| 190 | uint8_t ft; /* frame type */ |
| 191 | uint8_t ff; /* frame flags */ |
| 192 | }; |
| 193 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 194 | /* a few settings from the global section */ |
| 195 | static int h2_settings_header_table_size = 4096; /* initial value */ |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 196 | static int h2_settings_initial_window_size = 65535; /* initial value */ |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 197 | static int h2_settings_max_concurrent_streams = 100; |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 198 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 199 | /* a dmumy closed stream */ |
| 200 | static const struct h2s *h2_closed_stream = &(const struct h2s){ |
| 201 | .cs = NULL, |
| 202 | .h2c = NULL, |
| 203 | .st = H2_SS_CLOSED, |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 204 | .errcode = H2_ERR_STREAM_CLOSED, |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 205 | .flags = H2_SF_RST_RCVD, |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 206 | .id = 0, |
| 207 | }; |
| 208 | |
| 209 | /* and a dummy idle stream for use with any unannounced stream */ |
| 210 | static const struct h2s *h2_idle_stream = &(const struct h2s){ |
| 211 | .cs = NULL, |
| 212 | .h2c = NULL, |
| 213 | .st = H2_SS_IDLE, |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 214 | .errcode = H2_ERR_STREAM_CLOSED, |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 215 | .id = 0, |
| 216 | }; |
| 217 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 218 | static struct task *h2_timeout_task(struct task *t); |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 219 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 220 | /*****************************************************/ |
| 221 | /* functions below are for dynamic buffer management */ |
| 222 | /*****************************************************/ |
| 223 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 224 | /* indicates whether or not the we may call the h2_recv() function to attempt |
| 225 | * to receive data into the buffer and/or demux pending data. The condition is |
| 226 | * a bit complex due to some API limits for now. The rules are the following : |
| 227 | * - if an error or a shutdown was detected on the connection and the buffer |
| 228 | * is empty, we must not attempt to receive |
| 229 | * - if the demux buf failed to be allocated, we must not try to receive and |
| 230 | * we know there is nothing pending |
Willy Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 231 | * - if no flag indicates a blocking condition, we may attempt to receive, |
| 232 | * regardless of whether the demux buffer is full or not, so that only |
| 233 | * de demux part decides whether or not to block. This is needed because |
| 234 | * the connection API indeed prevents us from re-enabling receipt that is |
| 235 | * already enabled in a polled state, so we must always immediately stop |
| 236 | * as soon as the demux can't proceed so as never to hit an end of read |
| 237 | * with data pending in the buffers. |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 238 | * - otherwise must may not attempt |
| 239 | */ |
| 240 | static inline int h2_recv_allowed(const struct h2c *h2c) |
| 241 | { |
| 242 | if (h2c->dbuf->i == 0 && |
| 243 | (h2c->st0 >= H2_CS_ERROR || |
| 244 | h2c->conn->flags & CO_FL_ERROR || |
| 245 | conn_xprt_read0_pending(h2c->conn))) |
| 246 | return 0; |
| 247 | |
| 248 | if (!(h2c->flags & H2_CF_DEM_DALLOC) && |
Willy Tarreau | 6042aeb | 2017-12-12 11:01:44 +0100 | [diff] [blame] | 249 | !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 250 | return 1; |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 255 | /* Tries to grab a buffer and to re-enable processing on mux <target>. The h2c |
| 256 | * flags are used to figure what buffer was requested. It returns 1 if the |
| 257 | * allocation succeeds, in which case the connection is woken up, or 0 if it's |
| 258 | * impossible to wake up and we prefer to be woken up later. |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 259 | */ |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 260 | static int h2_buf_available(void *target) |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 261 | { |
| 262 | struct h2c *h2c = target; |
| 263 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 264 | if ((h2c->flags & H2_CF_DEM_DALLOC) && b_alloc_margin(&h2c->dbuf, 0)) { |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 265 | h2c->flags &= ~H2_CF_DEM_DALLOC; |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 266 | if (h2_recv_allowed(h2c)) |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 267 | conn_xprt_want_recv(h2c->conn); |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 268 | return 1; |
| 269 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 270 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 271 | if ((h2c->flags & H2_CF_MUX_MALLOC) && b_alloc_margin(&h2c->mbuf, 0)) { |
| 272 | h2c->flags &= ~H2_CF_MUX_MALLOC; |
| 273 | if (!(h2c->flags & H2_CF_MUX_BLOCK_ANY)) |
| 274 | conn_xprt_want_send(h2c->conn); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 275 | |
| 276 | if (h2c->flags & H2_CF_DEM_MROOM) { |
| 277 | h2c->flags &= ~H2_CF_DEM_MROOM; |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 278 | if (h2_recv_allowed(h2c)) |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 279 | conn_xprt_want_recv(h2c->conn); |
| 280 | } |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 281 | return 1; |
| 282 | } |
| 283 | return 0; |
| 284 | } |
| 285 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 286 | static inline struct buffer *h2_get_buf(struct h2c *h2c, struct buffer **bptr) |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 287 | { |
| 288 | struct buffer *buf = NULL; |
| 289 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 290 | if (likely(LIST_ISEMPTY(&h2c->buf_wait.list)) && |
| 291 | unlikely((buf = b_alloc_margin(bptr, 0)) == NULL)) { |
| 292 | h2c->buf_wait.target = h2c; |
| 293 | h2c->buf_wait.wakeup_cb = h2_buf_available; |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 294 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 295 | LIST_ADDQ(&buffer_wq, &h2c->buf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 296 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 297 | __conn_xprt_stop_recv(h2c->conn); |
| 298 | } |
| 299 | return buf; |
| 300 | } |
| 301 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 302 | static inline void h2_release_buf(struct h2c *h2c, struct buffer **bptr) |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 303 | { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 304 | if ((*bptr)->size) { |
| 305 | b_free(bptr); |
| 306 | offer_buffers(h2c->buf_wait.target, |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 307 | tasks_run_queue + applets_active_queue); |
| 308 | } |
| 309 | } |
| 310 | |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 311 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 312 | /*****************************************************************/ |
| 313 | /* functions below are dedicated to the mux setup and management */ |
| 314 | /*****************************************************************/ |
| 315 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 316 | /* tries to initialize the inbound h2c mux. Returns < 0 in case of failure. */ |
| 317 | static int h2c_frt_init(struct connection *conn) |
| 318 | { |
| 319 | struct h2c *h2c; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 320 | struct task *t = NULL; |
| 321 | struct session *sess = conn->owner; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 322 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 323 | h2c = pool_alloc(pool_head_h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 324 | if (!h2c) |
| 325 | goto fail; |
| 326 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 327 | |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 328 | h2c->shut_timeout = h2c->timeout = sess->fe->timeout.client; |
| 329 | if (tick_isset(sess->fe->timeout.clientfin)) |
| 330 | h2c->shut_timeout = sess->fe->timeout.clientfin; |
| 331 | |
Willy Tarreau | 3340029 | 2017-11-05 11:23:40 +0100 | [diff] [blame] | 332 | h2c->task = NULL; |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 333 | if (tick_isset(h2c->timeout)) { |
| 334 | t = task_new(tid_bit); |
| 335 | if (!t) |
| 336 | goto fail; |
| 337 | |
| 338 | h2c->task = t; |
| 339 | t->process = h2_timeout_task; |
| 340 | t->context = h2c; |
| 341 | t->expire = tick_add(now_ms, h2c->timeout); |
| 342 | } |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 343 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 344 | h2c->ddht = hpack_dht_alloc(h2_settings_header_table_size); |
| 345 | if (!h2c->ddht) |
| 346 | goto fail; |
| 347 | |
| 348 | /* Initialise the context. */ |
| 349 | h2c->st0 = H2_CS_PREFACE; |
| 350 | h2c->conn = conn; |
| 351 | h2c->max_id = -1; |
| 352 | h2c->errcode = H2_ERR_NO_ERROR; |
| 353 | h2c->flags = H2_CF_NONE; |
| 354 | h2c->rcvd_c = 0; |
| 355 | h2c->rcvd_s = 0; |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 356 | h2c->nb_streams = 0; |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 357 | |
| 358 | h2c->dbuf = &buf_empty; |
| 359 | h2c->dsi = -1; |
| 360 | h2c->msi = -1; |
| 361 | h2c->last_sid = -1; |
| 362 | |
| 363 | h2c->mbuf = &buf_empty; |
| 364 | h2c->miw = 65535; /* mux initial window size */ |
| 365 | h2c->mws = 65535; /* mux window size */ |
| 366 | h2c->mfs = 16384; /* initial max frame size */ |
| 367 | h2c->streams_by_id = EB_ROOT_UNIQUE; |
| 368 | LIST_INIT(&h2c->send_list); |
| 369 | LIST_INIT(&h2c->fctl_list); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 370 | LIST_INIT(&h2c->buf_wait.list); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 371 | conn->mux_ctx = h2c; |
| 372 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 373 | if (t) |
| 374 | task_queue(t); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 375 | conn_xprt_want_recv(conn); |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 376 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 377 | /* mux->wake will be called soon to complete the operation */ |
| 378 | return 0; |
| 379 | fail: |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 380 | if (t) |
| 381 | task_free(t); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 382 | pool_free(pool_head_h2c, h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 383 | return -1; |
| 384 | } |
| 385 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 386 | /* Initialize the mux once it's attached. For outgoing connections, the context |
| 387 | * is already initialized before installing the mux, so we detect incoming |
| 388 | * connections from the fact that the context is still NULL. Returns < 0 on |
| 389 | * error. |
| 390 | */ |
| 391 | static int h2_init(struct connection *conn) |
| 392 | { |
| 393 | if (conn->mux_ctx) { |
| 394 | /* we don't support outgoing connections for now */ |
| 395 | return -1; |
| 396 | } |
| 397 | |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 398 | return h2c_frt_init(conn); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 399 | } |
| 400 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 401 | /* returns the stream associated with id <id> or NULL if not found */ |
| 402 | static inline struct h2s *h2c_st_by_id(struct h2c *h2c, int id) |
| 403 | { |
| 404 | struct eb32_node *node; |
| 405 | |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 406 | if (id > h2c->max_id) |
| 407 | return (struct h2s *)h2_idle_stream; |
| 408 | |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 409 | node = eb32_lookup(&h2c->streams_by_id, id); |
| 410 | if (!node) |
Willy Tarreau | 2a85618 | 2017-05-16 15:20:39 +0200 | [diff] [blame] | 411 | return (struct h2s *)h2_closed_stream; |
Willy Tarreau | 2373acc | 2017-10-12 17:35:14 +0200 | [diff] [blame] | 412 | |
| 413 | return container_of(node, struct h2s, by_id); |
| 414 | } |
| 415 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 416 | /* release function for a connection. This one should be called to free all |
| 417 | * resources allocated to the mux. |
| 418 | */ |
| 419 | static void h2_release(struct connection *conn) |
| 420 | { |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 421 | struct h2c *h2c = conn->mux_ctx; |
| 422 | |
| 423 | LIST_DEL(&conn->list); |
| 424 | |
| 425 | if (h2c) { |
| 426 | hpack_dht_free(h2c->ddht); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 427 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 428 | HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 429 | LIST_DEL(&h2c->buf_wait.list); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 430 | HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock); |
Willy Tarreau | 1439812 | 2017-09-22 14:26:04 +0200 | [diff] [blame] | 431 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 432 | h2_release_buf(h2c, &h2c->dbuf); |
| 433 | h2_release_buf(h2c, &h2c->mbuf); |
| 434 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 435 | if (h2c->task) { |
| 436 | task_delete(h2c->task); |
| 437 | task_free(h2c->task); |
| 438 | h2c->task = NULL; |
| 439 | } |
| 440 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 441 | pool_free(pool_head_h2c, h2c); |
Willy Tarreau | 32218eb | 2017-09-22 08:07:25 +0200 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | conn->mux = NULL; |
| 445 | conn->mux_ctx = NULL; |
| 446 | |
| 447 | conn_stop_tracking(conn); |
| 448 | conn_full_close(conn); |
| 449 | if (conn->destroy_cb) |
| 450 | conn->destroy_cb(conn); |
| 451 | conn_free(conn); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 455 | /******************************************************/ |
| 456 | /* functions below are for the H2 protocol processing */ |
| 457 | /******************************************************/ |
| 458 | |
| 459 | /* returns the stream if of stream <h2s> or 0 if <h2s> is NULL */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 460 | static inline __maybe_unused int h2s_id(const struct h2s *h2s) |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 461 | { |
| 462 | return h2s ? h2s->id : 0; |
| 463 | } |
| 464 | |
Willy Tarreau | 5b5e687 | 2017-09-25 16:17:25 +0200 | [diff] [blame] | 465 | /* returns true of the mux is currently busy as seen from stream <h2s> */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 466 | static inline __maybe_unused int h2c_mux_busy(const struct h2c *h2c, const struct h2s *h2s) |
Willy Tarreau | 5b5e687 | 2017-09-25 16:17:25 +0200 | [diff] [blame] | 467 | { |
| 468 | if (h2c->msi < 0) |
| 469 | return 0; |
| 470 | |
| 471 | if (h2c->msi == h2s_id(h2s)) |
| 472 | return 0; |
| 473 | |
| 474 | return 1; |
| 475 | } |
| 476 | |
Willy Tarreau | 741d6df | 2017-10-17 08:00:59 +0200 | [diff] [blame] | 477 | /* marks an error on the connection */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 478 | static inline __maybe_unused void h2c_error(struct h2c *h2c, enum h2_err err) |
Willy Tarreau | 741d6df | 2017-10-17 08:00:59 +0200 | [diff] [blame] | 479 | { |
| 480 | h2c->errcode = err; |
| 481 | h2c->st0 = H2_CS_ERROR; |
| 482 | } |
| 483 | |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 484 | /* marks an error on the stream */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 485 | static inline __maybe_unused void h2s_error(struct h2s *h2s, enum h2_err err) |
Willy Tarreau | 2e43f08 | 2017-10-17 08:03:59 +0200 | [diff] [blame] | 486 | { |
| 487 | if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_ERROR) { |
| 488 | h2s->errcode = err; |
| 489 | h2s->st = H2_SS_ERROR; |
| 490 | if (h2s->cs) |
| 491 | h2s->cs->flags |= CS_FL_ERROR; |
| 492 | } |
| 493 | } |
| 494 | |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 495 | /* writes the 24-bit frame size <len> at address <frame> */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 496 | static inline __maybe_unused void h2_set_frame_size(void *frame, uint32_t len) |
Willy Tarreau | e482074 | 2017-07-27 13:37:23 +0200 | [diff] [blame] | 497 | { |
| 498 | uint8_t *out = frame; |
| 499 | |
| 500 | *out = len >> 16; |
| 501 | write_n16(out + 1, len); |
| 502 | } |
| 503 | |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 504 | /* reads <bytes> bytes from buffer <b> starting at relative offset <o> from the |
| 505 | * current pointer, dealing with wrapping, and stores the result in <dst>. It's |
| 506 | * the caller's responsibility to verify that there are at least <bytes> bytes |
| 507 | * available in the buffer's input prior to calling this function. |
| 508 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 509 | static inline __maybe_unused void h2_get_buf_bytes(void *dst, size_t bytes, |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 510 | const struct buffer *b, int o) |
| 511 | { |
| 512 | readv_bytes(dst, bytes, b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data); |
| 513 | } |
| 514 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 515 | static inline __maybe_unused uint16_t h2_get_n16(const struct buffer *b, int o) |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 516 | { |
| 517 | return readv_n16(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data); |
| 518 | } |
| 519 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 520 | static inline __maybe_unused uint32_t h2_get_n32(const struct buffer *b, int o) |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 521 | { |
| 522 | return readv_n32(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data); |
| 523 | } |
| 524 | |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 525 | static inline __maybe_unused uint64_t h2_get_n64(const struct buffer *b, int o) |
Willy Tarreau | 54c1506 | 2017-10-10 17:10:03 +0200 | [diff] [blame] | 526 | { |
| 527 | return readv_n64(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data); |
| 528 | } |
| 529 | |
| 530 | |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 531 | /* Peeks an H2 frame header from buffer <b> into descriptor <h>. The algorithm |
| 532 | * is not obvious. It turns out that H2 headers are neither aligned nor do they |
| 533 | * use regular sizes. And to add to the trouble, the buffer may wrap so each |
| 534 | * byte read must be checked. The header is formed like this : |
| 535 | * |
| 536 | * b0 b1 b2 b3 b4 b5..b8 |
| 537 | * +----------+---------+--------+----+----+----------------------+ |
| 538 | * |len[23:16]|len[15:8]|len[7:0]|type|flag|sid[31:0] (big endian)| |
| 539 | * +----------+---------+--------+----+----+----------------------+ |
| 540 | * |
| 541 | * Here we read a big-endian 64 bit word from h[1]. This way in a single read |
| 542 | * we get the sid properly aligned and ordered, and 16 bits of len properly |
| 543 | * ordered as well. The type and flags can be extracted using bit shifts from |
| 544 | * the word, and only one extra read is needed to fetch len[16:23]. |
| 545 | * Returns zero if some bytes are missing, otherwise non-zero on success. |
| 546 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 547 | static __maybe_unused int h2_peek_frame_hdr(const struct buffer *b, struct h2_fh *h) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 548 | { |
| 549 | uint64_t w; |
| 550 | |
| 551 | if (b->i < 9) |
| 552 | return 0; |
| 553 | |
| 554 | w = readv_n64(b_ptr(b,1), b_end(b) - b_ptr(b,1), b->data); |
| 555 | h->len = *b->p << 16; |
| 556 | h->sid = w & 0x7FFFFFFF; /* RFC7540#4.1: R bit must be ignored */ |
| 557 | h->ff = w >> 32; |
| 558 | h->ft = w >> 40; |
| 559 | h->len += w >> 48; |
| 560 | return 1; |
| 561 | } |
| 562 | |
| 563 | /* skip the next 9 bytes corresponding to the frame header possibly parsed by |
| 564 | * h2_peek_frame_hdr() above. |
| 565 | */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 566 | static inline __maybe_unused void h2_skip_frame_hdr(struct buffer *b) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 567 | { |
| 568 | bi_del(b, 9); |
| 569 | } |
| 570 | |
| 571 | /* same as above, automatically advances the buffer on success */ |
Willy Tarreau | 1f09467 | 2017-11-20 21:27:45 +0100 | [diff] [blame] | 572 | static inline __maybe_unused int h2_get_frame_hdr(struct buffer *b, struct h2_fh *h) |
Willy Tarreau | 715d531 | 2017-07-11 15:20:24 +0200 | [diff] [blame] | 573 | { |
| 574 | int ret; |
| 575 | |
| 576 | ret = h2_peek_frame_hdr(b, h); |
| 577 | if (ret > 0) |
| 578 | h2_skip_frame_hdr(b); |
| 579 | return ret; |
| 580 | } |
| 581 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 582 | /* marks stream <h2s> as CLOSED and decrement the number of active streams for |
| 583 | * its connection if the stream was not yet closed. Please use this exclusively |
| 584 | * before closing a stream to ensure stream count is well maintained. |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 585 | */ |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 586 | static inline void h2s_close(struct h2s *h2s) |
Willy Tarreau | 91bfdd7 | 2017-12-14 12:00:14 +0100 | [diff] [blame] | 587 | { |
| 588 | if (h2s->st != H2_SS_CLOSED) |
| 589 | h2s->h2c->nb_streams--; |
| 590 | h2s->st = H2_SS_CLOSED; |
| 591 | } |
| 592 | |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 593 | /* detaches an H2 stream from its H2C. */ |
| 594 | static void h2s_detach(struct h2s *h2s) |
| 595 | { |
| 596 | h2s_close(h2s); |
| 597 | eb32_delete(&h2s->by_id); |
| 598 | } |
| 599 | |
| 600 | /* releases an H2 stream back to the pool, and detaches it from the h2c. */ |
| 601 | static void h2s_free(struct h2s *h2s) |
| 602 | { |
| 603 | pool_free(pool_head_h2s, h2s); |
| 604 | } |
| 605 | |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 606 | /* creates a new stream <id> on the h2c connection and returns it, or NULL in |
| 607 | * case of memory allocation error. |
| 608 | */ |
| 609 | static struct h2s *h2c_stream_new(struct h2c *h2c, int id) |
| 610 | { |
| 611 | struct conn_stream *cs; |
| 612 | struct h2s *h2s; |
| 613 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 614 | h2s = pool_alloc(pool_head_h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 615 | if (!h2s) |
| 616 | goto out; |
| 617 | |
| 618 | h2s->h2c = h2c; |
| 619 | h2s->mws = h2c->miw; |
| 620 | h2s->flags = H2_SF_NONE; |
| 621 | h2s->errcode = H2_ERR_NO_ERROR; |
| 622 | h2s->st = H2_SS_IDLE; |
| 623 | h1m_init(&h2s->req); |
| 624 | h1m_init(&h2s->res); |
| 625 | h2s->by_id.key = h2s->id = id; |
| 626 | h2c->max_id = id; |
| 627 | LIST_INIT(&h2s->list); |
| 628 | |
| 629 | eb32_insert(&h2c->streams_by_id, &h2s->by_id); |
Willy Tarreau | 4974561 | 2017-12-03 18:56:02 +0100 | [diff] [blame] | 630 | h2c->nb_streams++; |
| 631 | if (h2c->nb_streams > h2_settings_max_concurrent_streams) |
| 632 | goto out_close; |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 633 | |
| 634 | cs = cs_new(h2c->conn); |
| 635 | if (!cs) |
| 636 | goto out_close; |
| 637 | |
| 638 | h2s->cs = cs; |
| 639 | cs->ctx = h2s; |
| 640 | |
| 641 | if (stream_create_from_cs(cs) < 0) |
| 642 | goto out_free_cs; |
| 643 | |
| 644 | /* OK done, the stream lives its own life now */ |
| 645 | return h2s; |
| 646 | |
| 647 | out_free_cs: |
| 648 | cs_free(cs); |
| 649 | out_close: |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 650 | h2s_detach(h2s); |
| 651 | h2s_free(h2s); |
Willy Tarreau | 3ccf4b2 | 2017-10-13 19:07:26 +0200 | [diff] [blame] | 652 | h2s = NULL; |
| 653 | out: |
| 654 | return h2s; |
| 655 | } |
| 656 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 657 | /* try to send a settings frame on the connection. Returns > 0 on success, 0 if |
| 658 | * it couldn't do anything. It may return an error in h2c. See RFC7540#11.3 for |
| 659 | * the various settings codes. |
| 660 | */ |
| 661 | static int h2c_snd_settings(struct h2c *h2c) |
| 662 | { |
| 663 | struct buffer *res; |
| 664 | char buf_data[100]; // enough for 15 settings |
| 665 | struct chunk buf; |
| 666 | int ret; |
| 667 | |
| 668 | if (h2c_mux_busy(h2c, NULL)) { |
| 669 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 670 | return 0; |
| 671 | } |
| 672 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 673 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 674 | if (!res) { |
| 675 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 676 | h2c->flags |= H2_CF_DEM_MROOM; |
| 677 | return 0; |
| 678 | } |
| 679 | |
| 680 | chunk_init(&buf, buf_data, sizeof(buf_data)); |
| 681 | chunk_memcpy(&buf, |
| 682 | "\x00\x00\x00" /* length : 0 for now */ |
| 683 | "\x04\x00" /* type : 4 (settings), flags : 0 */ |
| 684 | "\x00\x00\x00\x00", /* stream ID : 0 */ |
| 685 | 9); |
| 686 | |
| 687 | if (h2_settings_header_table_size != 4096) { |
| 688 | char str[6] = "\x00\x01"; /* header_table_size */ |
| 689 | |
| 690 | write_n32(str + 2, h2_settings_header_table_size); |
| 691 | chunk_memcat(&buf, str, 6); |
| 692 | } |
| 693 | |
| 694 | if (h2_settings_initial_window_size != 65535) { |
| 695 | char str[6] = "\x00\x04"; /* initial_window_size */ |
| 696 | |
| 697 | write_n32(str + 2, h2_settings_initial_window_size); |
| 698 | chunk_memcat(&buf, str, 6); |
| 699 | } |
| 700 | |
| 701 | if (h2_settings_max_concurrent_streams != 0) { |
| 702 | char str[6] = "\x00\x03"; /* max_concurrent_streams */ |
| 703 | |
| 704 | /* Note: 0 means "unlimited" for haproxy's config but not for |
| 705 | * the protocol, so never send this value! |
| 706 | */ |
| 707 | write_n32(str + 2, h2_settings_max_concurrent_streams); |
| 708 | chunk_memcat(&buf, str, 6); |
| 709 | } |
| 710 | |
| 711 | if (global.tune.bufsize != 16384) { |
| 712 | char str[6] = "\x00\x05"; /* max_frame_size */ |
| 713 | |
| 714 | /* note: similarly we could also emit MAX_HEADER_LIST_SIZE to |
| 715 | * match bufsize - rewrite size, but at the moment it seems |
| 716 | * that clients don't take care of it. |
| 717 | */ |
| 718 | write_n32(str + 2, global.tune.bufsize); |
| 719 | chunk_memcat(&buf, str, 6); |
| 720 | } |
| 721 | |
| 722 | h2_set_frame_size(buf.str, buf.len - 9); |
| 723 | ret = bo_istput(res, ist2(buf.str, buf.len)); |
| 724 | if (unlikely(ret <= 0)) { |
| 725 | if (!ret) { |
| 726 | h2c->flags |= H2_CF_MUX_MFULL; |
| 727 | h2c->flags |= H2_CF_DEM_MROOM; |
| 728 | return 0; |
| 729 | } |
| 730 | else { |
| 731 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 732 | return 0; |
| 733 | } |
| 734 | } |
| 735 | return ret; |
| 736 | } |
| 737 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 738 | /* Try to receive a connection preface, then upon success try to send our |
| 739 | * preface which is a SETTINGS frame. Returns > 0 on success or zero on |
| 740 | * missing data. It may return an error in h2c. |
| 741 | */ |
| 742 | static int h2c_frt_recv_preface(struct h2c *h2c) |
| 743 | { |
| 744 | int ret1; |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 745 | int ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 746 | |
| 747 | ret1 = b_isteq(h2c->dbuf, 0, h2c->dbuf->i, ist(H2_CONN_PREFACE)); |
| 748 | |
| 749 | if (unlikely(ret1 <= 0)) { |
| 750 | if (ret1 < 0 || conn_xprt_read0_pending(h2c->conn)) |
| 751 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 752 | return 0; |
| 753 | } |
| 754 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 755 | ret2 = h2c_snd_settings(h2c); |
| 756 | if (ret2 > 0) |
| 757 | bi_del(h2c->dbuf, ret1); |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 758 | |
Willy Tarreau | be5b715 | 2017-09-25 16:25:39 +0200 | [diff] [blame] | 759 | return ret2; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 760 | } |
| 761 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 762 | /* try to send a GOAWAY frame on the connection to report an error or a graceful |
| 763 | * shutdown, with h2c->errcode as the error code. Returns > 0 on success or zero |
| 764 | * if nothing was done. It uses h2c->last_sid as the advertised ID, or copies it |
| 765 | * from h2c->max_id if it's not set yet (<0). In case of lack of room to write |
| 766 | * the message, it subscribes the requester (either <h2s> or <h2c>) to future |
| 767 | * notifications. It sets H2_CF_GOAWAY_SENT on success, and H2_CF_GOAWAY_FAILED |
| 768 | * on unrecoverable failure. It will not attempt to send one again in this last |
| 769 | * case so that it is safe to use h2c_error() to report such errors. |
| 770 | */ |
| 771 | static int h2c_send_goaway_error(struct h2c *h2c, struct h2s *h2s) |
| 772 | { |
| 773 | struct buffer *res; |
| 774 | char str[17]; |
| 775 | int ret; |
| 776 | |
| 777 | if (h2c->flags & H2_CF_GOAWAY_FAILED) |
| 778 | return 1; // claim that it worked |
| 779 | |
| 780 | if (h2c_mux_busy(h2c, h2s)) { |
| 781 | if (h2s) |
| 782 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 783 | else |
| 784 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 785 | return 0; |
| 786 | } |
| 787 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 788 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 789 | if (!res) { |
| 790 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 791 | if (h2s) |
| 792 | h2s->flags |= H2_SF_BLK_MROOM; |
| 793 | else |
| 794 | h2c->flags |= H2_CF_DEM_MROOM; |
| 795 | return 0; |
| 796 | } |
| 797 | |
| 798 | /* len: 8, type: 7, flags: none, sid: 0 */ |
| 799 | memcpy(str, "\x00\x00\x08\x07\x00\x00\x00\x00\x00", 9); |
| 800 | |
| 801 | if (h2c->last_sid < 0) |
| 802 | h2c->last_sid = h2c->max_id; |
| 803 | |
| 804 | write_n32(str + 9, h2c->last_sid); |
| 805 | write_n32(str + 13, h2c->errcode); |
| 806 | ret = bo_istput(res, ist2(str, 17)); |
| 807 | if (unlikely(ret <= 0)) { |
| 808 | if (!ret) { |
| 809 | h2c->flags |= H2_CF_MUX_MFULL; |
| 810 | if (h2s) |
| 811 | h2s->flags |= H2_SF_BLK_MROOM; |
| 812 | else |
| 813 | h2c->flags |= H2_CF_DEM_MROOM; |
| 814 | return 0; |
| 815 | } |
| 816 | else { |
| 817 | /* we cannot report this error using GOAWAY, so we mark |
| 818 | * it and claim a success. |
| 819 | */ |
| 820 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 821 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 822 | return 1; |
| 823 | } |
| 824 | } |
| 825 | h2c->flags |= H2_CF_GOAWAY_SENT; |
| 826 | return ret; |
| 827 | } |
| 828 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 829 | /* Try to send an RST_STREAM frame on the connection for the indicated stream |
| 830 | * during mux operations. This stream must be valid and cannot be closed |
| 831 | * already. h2s->id will be used for the stream ID and h2s->errcode will be |
| 832 | * used for the error code. h2s->st will be update to H2_SS_CLOSED if it was |
| 833 | * not yet. |
| 834 | * |
| 835 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 836 | * to write the message, it subscribes the stream to future notifications. |
| 837 | */ |
| 838 | static int h2s_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 839 | { |
| 840 | struct buffer *res; |
| 841 | char str[13]; |
| 842 | int ret; |
| 843 | |
| 844 | if (!h2s || h2s->st == H2_SS_CLOSED) |
| 845 | return 1; |
| 846 | |
| 847 | if (h2c_mux_busy(h2c, h2s)) { |
| 848 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 849 | return 0; |
| 850 | } |
| 851 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 852 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 853 | if (!res) { |
| 854 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 855 | h2s->flags |= H2_SF_BLK_MROOM; |
| 856 | return 0; |
| 857 | } |
| 858 | |
| 859 | /* len: 4, type: 3, flags: none */ |
| 860 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
| 861 | write_n32(str + 5, h2s->id); |
| 862 | write_n32(str + 9, h2s->errcode); |
| 863 | ret = bo_istput(res, ist2(str, 13)); |
| 864 | |
| 865 | if (unlikely(ret <= 0)) { |
| 866 | if (!ret) { |
| 867 | h2c->flags |= H2_CF_MUX_MFULL; |
| 868 | h2s->flags |= H2_SF_BLK_MROOM; |
| 869 | return 0; |
| 870 | } |
| 871 | else { |
| 872 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 873 | return 0; |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 878 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 879 | return ret; |
| 880 | } |
| 881 | |
| 882 | /* Try to send an RST_STREAM frame on the connection for the stream being |
| 883 | * demuxed using h2c->dsi for the stream ID. It will use h2s->errcode as the |
| 884 | * error code unless the stream's state already is IDLE or CLOSED in which |
| 885 | * case STREAM_CLOSED will be used, and will update h2s->st to H2_SS_CLOSED if |
| 886 | * it was not yet. |
| 887 | * |
| 888 | * Returns > 0 on success or zero if nothing was done. In case of lack of room |
| 889 | * to write the message, it blocks the demuxer and subscribes it to future |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 890 | * notifications. It's worth mentionning that an RST may even be sent for a |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 891 | * closed stream. |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 892 | */ |
| 893 | static int h2c_send_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 894 | { |
| 895 | struct buffer *res; |
| 896 | char str[13]; |
| 897 | int ret; |
| 898 | |
| 899 | if (h2c_mux_busy(h2c, h2s)) { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 900 | h2c->flags |= H2_CF_DEM_MBUSY; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 901 | return 0; |
| 902 | } |
| 903 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 904 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 905 | if (!res) { |
| 906 | h2c->flags |= H2_CF_MUX_MALLOC; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 907 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 908 | return 0; |
| 909 | } |
| 910 | |
| 911 | /* len: 4, type: 3, flags: none */ |
| 912 | memcpy(str, "\x00\x00\x04\x03\x00", 5); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 913 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 914 | write_n32(str + 5, h2c->dsi); |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 915 | write_n32(str + 9, (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) ? |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 916 | h2s->errcode : H2_ERR_STREAM_CLOSED); |
| 917 | ret = bo_istput(res, ist2(str, 13)); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 918 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 919 | if (unlikely(ret <= 0)) { |
| 920 | if (!ret) { |
| 921 | h2c->flags |= H2_CF_MUX_MFULL; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 922 | h2c->flags |= H2_CF_DEM_MROOM; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 923 | return 0; |
| 924 | } |
| 925 | else { |
| 926 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 927 | return 0; |
| 928 | } |
| 929 | } |
| 930 | |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 931 | if (h2s->st > H2_SS_IDLE && h2s->st < H2_SS_CLOSED) { |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 932 | h2s->flags |= H2_SF_RST_SENT; |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 933 | h2s_close(h2s); |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 934 | } |
| 935 | |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 936 | return ret; |
| 937 | } |
| 938 | |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 939 | /* try to send an empty DATA frame with the ES flag set to notify about the |
| 940 | * end of stream and match a shutdown(write). If an ES was already sent as |
| 941 | * indicated by HLOC/ERROR/RESET/CLOSED states, nothing is done. Returns > 0 |
| 942 | * on success or zero if nothing was done. In case of lack of room to write the |
| 943 | * message, it subscribes the requesting stream to future notifications. |
| 944 | */ |
| 945 | static int h2_send_empty_data_es(struct h2s *h2s) |
| 946 | { |
| 947 | struct h2c *h2c = h2s->h2c; |
| 948 | struct buffer *res; |
| 949 | char str[9]; |
| 950 | int ret; |
| 951 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 952 | if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED) |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 953 | return 1; |
| 954 | |
| 955 | if (h2c_mux_busy(h2c, h2s)) { |
| 956 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 957 | return 0; |
| 958 | } |
| 959 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 960 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 961 | if (!res) { |
| 962 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 963 | h2s->flags |= H2_SF_BLK_MROOM; |
| 964 | return 0; |
| 965 | } |
| 966 | |
| 967 | /* len: 0x000000, type: 0(DATA), flags: ES=1 */ |
| 968 | memcpy(str, "\x00\x00\x00\x00\x01", 5); |
| 969 | write_n32(str + 5, h2s->id); |
| 970 | ret = bo_istput(res, ist2(str, 9)); |
Willy Tarreau | 6d8b682 | 2017-11-07 14:39:09 +0100 | [diff] [blame] | 971 | if (likely(ret > 0)) { |
| 972 | h2s->flags |= H2_SF_ES_SENT; |
| 973 | } |
| 974 | else if (!ret) { |
| 975 | h2c->flags |= H2_CF_MUX_MFULL; |
| 976 | h2s->flags |= H2_SF_BLK_MROOM; |
| 977 | return 0; |
| 978 | } |
| 979 | else { |
| 980 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 981 | return 0; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 982 | } |
| 983 | return ret; |
| 984 | } |
| 985 | |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 986 | /* wake the streams attached to the connection, whose id is greater than <last>, |
| 987 | * and assign their conn_stream the CS_FL_* flags <flags> in addition to |
| 988 | * CS_FL_ERROR in case of error and CS_FL_EOS in case of closed connection. The |
| 989 | * stream's state is automatically updated accordingly. |
| 990 | */ |
| 991 | static void h2_wake_some_streams(struct h2c *h2c, int last, uint32_t flags) |
| 992 | { |
| 993 | struct eb32_node *node; |
| 994 | struct h2s *h2s; |
| 995 | |
| 996 | if (h2c->st0 >= H2_CS_ERROR || h2c->conn->flags & CO_FL_ERROR) |
| 997 | flags |= CS_FL_ERROR; |
| 998 | |
| 999 | if (conn_xprt_read0_pending(h2c->conn)) |
| 1000 | flags |= CS_FL_EOS; |
| 1001 | |
| 1002 | node = eb32_lookup_ge(&h2c->streams_by_id, last + 1); |
| 1003 | while (node) { |
| 1004 | h2s = container_of(node, struct h2s, by_id); |
| 1005 | if (h2s->id <= last) |
| 1006 | break; |
| 1007 | node = eb32_next(node); |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1008 | |
| 1009 | if (!h2s->cs) { |
| 1010 | /* this stream was already orphaned */ |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 1011 | h2s_detach(h2s); |
| 1012 | h2s_free(h2s); |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1013 | continue; |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1014 | } |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 1015 | |
| 1016 | h2s->cs->flags |= flags; |
| 1017 | /* recv is used to force to detect CS_FL_EOS that wake() |
| 1018 | * doesn't handle in the stream int code. |
| 1019 | */ |
| 1020 | h2s->cs->data_cb->recv(h2s->cs); |
| 1021 | h2s->cs->data_cb->wake(h2s->cs); |
| 1022 | |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1023 | if (flags & CS_FL_ERROR && h2s->st < H2_SS_ERROR) |
| 1024 | h2s->st = H2_SS_ERROR; |
| 1025 | else if (flags & CS_FL_EOS && h2s->st == H2_SS_OPEN) |
| 1026 | h2s->st = H2_SS_HREM; |
| 1027 | else if (flags & CS_FL_EOS && h2s->st == H2_SS_HLOC) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1028 | h2s_close(h2s); |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 1029 | } |
| 1030 | } |
| 1031 | |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1032 | /* Increase all streams' outgoing window size by the difference passed in |
| 1033 | * argument. This is needed upon receipt of the settings frame if the initial |
| 1034 | * window size is different. The difference may be negative and the resulting |
| 1035 | * window size as well, for the time it takes to receive some window updates. |
| 1036 | */ |
| 1037 | static void h2c_update_all_ws(struct h2c *h2c, int diff) |
| 1038 | { |
| 1039 | struct h2s *h2s; |
| 1040 | struct eb32_node *node; |
| 1041 | |
| 1042 | if (!diff) |
| 1043 | return; |
| 1044 | |
| 1045 | node = eb32_first(&h2c->streams_by_id); |
| 1046 | while (node) { |
| 1047 | h2s = container_of(node, struct h2s, by_id); |
| 1048 | h2s->mws += diff; |
| 1049 | node = eb32_next(node); |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | /* processes a SETTINGS frame whose payload is <payload> for <plen> bytes, and |
| 1054 | * ACKs it if needed. Returns > 0 on success or zero on missing data. It may |
| 1055 | * return an error in h2c. Described in RFC7540#6.5. |
| 1056 | */ |
| 1057 | static int h2c_handle_settings(struct h2c *h2c) |
| 1058 | { |
| 1059 | unsigned int offset; |
| 1060 | int error; |
| 1061 | |
| 1062 | if (h2c->dff & H2_F_SETTINGS_ACK) { |
| 1063 | if (h2c->dfl) { |
| 1064 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1065 | goto fail; |
| 1066 | } |
| 1067 | return 1; |
| 1068 | } |
| 1069 | |
| 1070 | if (h2c->dsi != 0) { |
| 1071 | error = H2_ERR_PROTOCOL_ERROR; |
| 1072 | goto fail; |
| 1073 | } |
| 1074 | |
| 1075 | if (h2c->dfl % 6) { |
| 1076 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1077 | goto fail; |
| 1078 | } |
| 1079 | |
| 1080 | /* that's the limit we can process */ |
| 1081 | if (h2c->dfl > global.tune.bufsize) { |
| 1082 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1083 | goto fail; |
| 1084 | } |
| 1085 | |
| 1086 | /* process full frame only */ |
| 1087 | if (h2c->dbuf->i < h2c->dfl) |
| 1088 | return 0; |
| 1089 | |
| 1090 | /* parse the frame */ |
| 1091 | for (offset = 0; offset < h2c->dfl; offset += 6) { |
| 1092 | uint16_t type = h2_get_n16(h2c->dbuf, offset); |
| 1093 | int32_t arg = h2_get_n32(h2c->dbuf, offset + 2); |
| 1094 | |
| 1095 | switch (type) { |
| 1096 | case H2_SETTINGS_INITIAL_WINDOW_SIZE: |
| 1097 | /* we need to update all existing streams with the |
| 1098 | * difference from the previous iws. |
| 1099 | */ |
| 1100 | if (arg < 0) { // RFC7540#6.5.2 |
| 1101 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1102 | goto fail; |
| 1103 | } |
| 1104 | h2c_update_all_ws(h2c, arg - h2c->miw); |
| 1105 | h2c->miw = arg; |
| 1106 | break; |
| 1107 | case H2_SETTINGS_MAX_FRAME_SIZE: |
| 1108 | if (arg < 16384 || arg > 16777215) { // RFC7540#6.5.2 |
| 1109 | error = H2_ERR_PROTOCOL_ERROR; |
| 1110 | goto fail; |
| 1111 | } |
| 1112 | h2c->mfs = arg; |
| 1113 | break; |
Willy Tarreau | 1b38b46 | 2017-12-03 19:02:28 +0100 | [diff] [blame] | 1114 | case H2_SETTINGS_ENABLE_PUSH: |
| 1115 | if (arg < 0 || arg > 1) { // RFC7540#6.5.2 |
| 1116 | error = H2_ERR_PROTOCOL_ERROR; |
| 1117 | goto fail; |
| 1118 | } |
| 1119 | break; |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | /* need to ACK this frame now */ |
| 1124 | h2c->st0 = H2_CS_FRAME_A; |
| 1125 | return 1; |
| 1126 | fail: |
| 1127 | h2c_error(h2c, error); |
| 1128 | return 0; |
| 1129 | } |
| 1130 | |
| 1131 | /* try to send an ACK for a settings frame on the connection. Returns > 0 on |
| 1132 | * success or one of the h2_status values. |
| 1133 | */ |
| 1134 | static int h2c_ack_settings(struct h2c *h2c) |
| 1135 | { |
| 1136 | struct buffer *res; |
| 1137 | char str[9]; |
| 1138 | int ret = -1; |
| 1139 | |
| 1140 | if (h2c_mux_busy(h2c, NULL)) { |
| 1141 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1142 | return 0; |
| 1143 | } |
| 1144 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1145 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1146 | if (!res) { |
| 1147 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1148 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1149 | return 0; |
| 1150 | } |
| 1151 | |
| 1152 | memcpy(str, |
| 1153 | "\x00\x00\x00" /* length : 0 (no data) */ |
| 1154 | "\x04" "\x01" /* type : 4, flags : ACK */ |
| 1155 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1156 | |
| 1157 | ret = bo_istput(res, ist2(str, 9)); |
| 1158 | if (unlikely(ret <= 0)) { |
| 1159 | if (!ret) { |
| 1160 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1161 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1162 | return 0; |
| 1163 | } |
| 1164 | else { |
| 1165 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1166 | return 0; |
| 1167 | } |
| 1168 | } |
| 1169 | return ret; |
| 1170 | } |
| 1171 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1172 | /* processes a PING frame and schedules an ACK if needed. The caller must pass |
| 1173 | * the pointer to the payload in <payload>. Returns > 0 on success or zero on |
| 1174 | * missing data. It may return an error in h2c. |
| 1175 | */ |
| 1176 | static int h2c_handle_ping(struct h2c *h2c) |
| 1177 | { |
| 1178 | /* frame length must be exactly 8 */ |
| 1179 | if (h2c->dfl != 8) { |
| 1180 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 1181 | return 0; |
| 1182 | } |
| 1183 | |
| 1184 | /* schedule a response */ |
Willy Tarreau | 68ed641 | 2017-12-03 18:15:56 +0100 | [diff] [blame] | 1185 | if (!(h2c->dff & H2_F_PING_ACK)) |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1186 | h2c->st0 = H2_CS_FRAME_A; |
| 1187 | return 1; |
| 1188 | } |
| 1189 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1190 | /* Try to send a window update for stream id <sid> and value <increment>. |
| 1191 | * Returns > 0 on success or zero on missing room or failure. It may return an |
| 1192 | * error in h2c. |
| 1193 | */ |
| 1194 | static int h2c_send_window_update(struct h2c *h2c, int sid, uint32_t increment) |
| 1195 | { |
| 1196 | struct buffer *res; |
| 1197 | char str[13]; |
| 1198 | int ret = -1; |
| 1199 | |
| 1200 | if (h2c_mux_busy(h2c, NULL)) { |
| 1201 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1202 | return 0; |
| 1203 | } |
| 1204 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1205 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1206 | if (!res) { |
| 1207 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1208 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1209 | return 0; |
| 1210 | } |
| 1211 | |
| 1212 | /* length: 4, type: 8, flags: none */ |
| 1213 | memcpy(str, "\x00\x00\x04\x08\x00", 5); |
| 1214 | write_n32(str + 5, sid); |
| 1215 | write_n32(str + 9, increment); |
| 1216 | |
| 1217 | ret = bo_istput(res, ist2(str, 13)); |
| 1218 | |
| 1219 | if (unlikely(ret <= 0)) { |
| 1220 | if (!ret) { |
| 1221 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1222 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1223 | return 0; |
| 1224 | } |
| 1225 | else { |
| 1226 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1227 | return 0; |
| 1228 | } |
| 1229 | } |
| 1230 | return ret; |
| 1231 | } |
| 1232 | |
| 1233 | /* try to send pending window update for the connection. It's safe to call it |
| 1234 | * with no pending updates. Returns > 0 on success or zero on missing room or |
| 1235 | * failure. It may return an error in h2c. |
| 1236 | */ |
| 1237 | static int h2c_send_conn_wu(struct h2c *h2c) |
| 1238 | { |
| 1239 | int ret = 1; |
| 1240 | |
| 1241 | if (h2c->rcvd_c <= 0) |
| 1242 | return 1; |
| 1243 | |
| 1244 | /* send WU for the connection */ |
| 1245 | ret = h2c_send_window_update(h2c, 0, h2c->rcvd_c); |
| 1246 | if (ret > 0) |
| 1247 | h2c->rcvd_c = 0; |
| 1248 | |
| 1249 | return ret; |
| 1250 | } |
| 1251 | |
| 1252 | /* try to send pending window update for the current dmux stream. It's safe to |
| 1253 | * call it with no pending updates. Returns > 0 on success or zero on missing |
| 1254 | * room or failure. It may return an error in h2c. |
| 1255 | */ |
| 1256 | static int h2c_send_strm_wu(struct h2c *h2c) |
| 1257 | { |
| 1258 | int ret = 1; |
| 1259 | |
| 1260 | if (h2c->rcvd_s <= 0) |
| 1261 | return 1; |
| 1262 | |
| 1263 | /* send WU for the stream */ |
| 1264 | ret = h2c_send_window_update(h2c, h2c->dsi, h2c->rcvd_s); |
| 1265 | if (ret > 0) |
| 1266 | h2c->rcvd_s = 0; |
| 1267 | |
| 1268 | return ret; |
| 1269 | } |
| 1270 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1271 | /* try to send an ACK for a ping frame on the connection. Returns > 0 on |
| 1272 | * success, 0 on missing data or one of the h2_status values. |
| 1273 | */ |
| 1274 | static int h2c_ack_ping(struct h2c *h2c) |
| 1275 | { |
| 1276 | struct buffer *res; |
| 1277 | char str[17]; |
| 1278 | int ret = -1; |
| 1279 | |
| 1280 | if (h2c->dbuf->i < 8) |
| 1281 | return 0; |
| 1282 | |
| 1283 | if (h2c_mux_busy(h2c, NULL)) { |
| 1284 | h2c->flags |= H2_CF_DEM_MBUSY; |
| 1285 | return 0; |
| 1286 | } |
| 1287 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 1288 | res = h2_get_buf(h2c, &h2c->mbuf); |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1289 | if (!res) { |
| 1290 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 1291 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1292 | return 0; |
| 1293 | } |
| 1294 | |
| 1295 | memcpy(str, |
| 1296 | "\x00\x00\x08" /* length : 8 (same payload) */ |
| 1297 | "\x06" "\x01" /* type : 6, flags : ACK */ |
| 1298 | "\x00\x00\x00\x00" /* stream ID */, 9); |
| 1299 | |
| 1300 | /* copy the original payload */ |
| 1301 | h2_get_buf_bytes(str + 9, 8, h2c->dbuf, 0); |
| 1302 | |
| 1303 | ret = bo_istput(res, ist2(str, 17)); |
| 1304 | if (unlikely(ret <= 0)) { |
| 1305 | if (!ret) { |
| 1306 | h2c->flags |= H2_CF_MUX_MFULL; |
| 1307 | h2c->flags |= H2_CF_DEM_MROOM; |
| 1308 | return 0; |
| 1309 | } |
| 1310 | else { |
| 1311 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 1312 | return 0; |
| 1313 | } |
| 1314 | } |
| 1315 | return ret; |
| 1316 | } |
| 1317 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1318 | /* processes a WINDOW_UPDATE frame whose payload is <payload> for <plen> bytes. |
| 1319 | * Returns > 0 on success or zero on missing data. It may return an error in |
| 1320 | * h2c or h2s. Described in RFC7540#6.9. |
| 1321 | */ |
| 1322 | static int h2c_handle_window_update(struct h2c *h2c, struct h2s *h2s) |
| 1323 | { |
| 1324 | int32_t inc; |
| 1325 | int error; |
| 1326 | |
| 1327 | if (h2c->dfl != 4) { |
| 1328 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1329 | goto conn_err; |
| 1330 | } |
| 1331 | |
| 1332 | /* process full frame only */ |
| 1333 | if (h2c->dbuf->i < h2c->dfl) |
| 1334 | return 0; |
| 1335 | |
| 1336 | inc = h2_get_n32(h2c->dbuf, 0); |
| 1337 | |
| 1338 | if (h2c->dsi != 0) { |
| 1339 | /* stream window update */ |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1340 | |
| 1341 | /* it's not an error to receive WU on a closed stream */ |
| 1342 | if (h2s->st == H2_SS_CLOSED) |
| 1343 | return 1; |
| 1344 | |
| 1345 | if (!inc) { |
| 1346 | error = H2_ERR_PROTOCOL_ERROR; |
| 1347 | goto strm_err; |
| 1348 | } |
| 1349 | |
| 1350 | if (h2s->mws >= 0 && h2s->mws + inc < 0) { |
| 1351 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1352 | goto strm_err; |
| 1353 | } |
| 1354 | |
| 1355 | h2s->mws += inc; |
| 1356 | if (h2s->mws > 0 && (h2s->flags & H2_SF_BLK_SFCTL)) { |
| 1357 | h2s->flags &= ~H2_SF_BLK_SFCTL; |
| 1358 | if (h2s->cs && LIST_ISEMPTY(&h2s->list) && |
| 1359 | (h2s->cs->flags & CS_FL_DATA_WR_ENA)) { |
| 1360 | /* This stream wanted to send but could not due to its |
| 1361 | * own flow control. We can put it back into the send |
| 1362 | * list now, it will be handled upon next send() call. |
| 1363 | */ |
| 1364 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 1365 | } |
| 1366 | } |
| 1367 | } |
| 1368 | else { |
| 1369 | /* connection window update */ |
| 1370 | if (!inc) { |
| 1371 | error = H2_ERR_PROTOCOL_ERROR; |
| 1372 | goto conn_err; |
| 1373 | } |
| 1374 | |
| 1375 | if (h2c->mws >= 0 && h2c->mws + inc < 0) { |
| 1376 | error = H2_ERR_FLOW_CONTROL_ERROR; |
| 1377 | goto conn_err; |
| 1378 | } |
| 1379 | |
| 1380 | h2c->mws += inc; |
| 1381 | } |
| 1382 | |
| 1383 | return 1; |
| 1384 | |
| 1385 | conn_err: |
| 1386 | h2c_error(h2c, error); |
| 1387 | return 0; |
| 1388 | |
| 1389 | strm_err: |
| 1390 | if (h2s) { |
| 1391 | h2s_error(h2s, error); |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1392 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1393 | } |
| 1394 | else |
| 1395 | h2c_error(h2c, error); |
| 1396 | return 0; |
| 1397 | } |
| 1398 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1399 | /* processes a GOAWAY frame, and signals all streams whose ID is greater than |
| 1400 | * the last ID. Returns > 0 on success or zero on missing data. It may return |
| 1401 | * an error in h2c. Described in RFC7540#6.8. |
| 1402 | */ |
| 1403 | static int h2c_handle_goaway(struct h2c *h2c) |
| 1404 | { |
| 1405 | int error; |
| 1406 | int last; |
| 1407 | |
| 1408 | if (h2c->dsi != 0) { |
| 1409 | error = H2_ERR_PROTOCOL_ERROR; |
| 1410 | goto conn_err; |
| 1411 | } |
| 1412 | |
| 1413 | if (h2c->dfl < 8) { |
| 1414 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1415 | goto conn_err; |
| 1416 | } |
| 1417 | |
| 1418 | /* process full frame only */ |
| 1419 | if (h2c->dbuf->i < h2c->dfl) |
| 1420 | return 0; |
| 1421 | |
| 1422 | last = h2_get_n32(h2c->dbuf, 0); |
| 1423 | h2c->errcode = h2_get_n32(h2c->dbuf, 4); |
| 1424 | h2_wake_some_streams(h2c, last, CS_FL_ERROR); |
Willy Tarreau | 11cc2d6 | 2017-12-03 10:27:47 +0100 | [diff] [blame] | 1425 | if (h2c->last_sid < 0) |
| 1426 | h2c->last_sid = last; |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1427 | return 1; |
| 1428 | |
| 1429 | conn_err: |
| 1430 | h2c_error(h2c, error); |
| 1431 | return 0; |
| 1432 | } |
| 1433 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1434 | /* processes a PRIORITY frame, and either skips it or rejects if it is |
| 1435 | * invalid. Returns > 0 on success or zero on missing data. It may return |
| 1436 | * an error in h2c. Described in RFC7540#6.3. |
| 1437 | */ |
| 1438 | static int h2c_handle_priority(struct h2c *h2c) |
| 1439 | { |
| 1440 | int error; |
| 1441 | |
| 1442 | if (h2c->dsi == 0) { |
| 1443 | error = H2_ERR_PROTOCOL_ERROR; |
| 1444 | goto conn_err; |
| 1445 | } |
| 1446 | |
| 1447 | if (h2c->dfl != 5) { |
| 1448 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1449 | goto conn_err; |
| 1450 | } |
| 1451 | |
| 1452 | /* process full frame only */ |
| 1453 | if (h2c->dbuf->i < h2c->dfl) |
| 1454 | return 0; |
| 1455 | |
| 1456 | if (h2_get_n32(h2c->dbuf, 0) == h2c->dsi) { |
| 1457 | /* 7540#5.3 : can't depend on itself */ |
| 1458 | error = H2_ERR_PROTOCOL_ERROR; |
| 1459 | goto conn_err; |
| 1460 | } |
| 1461 | return 1; |
| 1462 | |
| 1463 | conn_err: |
| 1464 | h2c_error(h2c, error); |
| 1465 | return 0; |
| 1466 | } |
| 1467 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1468 | /* processes an RST_STREAM frame, and sets the 32-bit error code on the stream. |
| 1469 | * Returns > 0 on success or zero on missing data. It may return an error in |
| 1470 | * h2c. Described in RFC7540#6.4. |
| 1471 | */ |
| 1472 | static int h2c_handle_rst_stream(struct h2c *h2c, struct h2s *h2s) |
| 1473 | { |
| 1474 | int error; |
| 1475 | |
| 1476 | if (h2c->dsi == 0) { |
| 1477 | error = H2_ERR_PROTOCOL_ERROR; |
| 1478 | goto conn_err; |
| 1479 | } |
| 1480 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1481 | if (h2c->dfl != 4) { |
| 1482 | error = H2_ERR_FRAME_SIZE_ERROR; |
| 1483 | goto conn_err; |
| 1484 | } |
| 1485 | |
| 1486 | /* process full frame only */ |
| 1487 | if (h2c->dbuf->i < h2c->dfl) |
| 1488 | return 0; |
| 1489 | |
| 1490 | /* late RST, already handled */ |
| 1491 | if (h2s->st == H2_SS_CLOSED) |
| 1492 | return 1; |
| 1493 | |
| 1494 | h2s->errcode = h2_get_n32(h2c->dbuf, 0); |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 1495 | h2s_close(h2s); |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1496 | |
| 1497 | if (h2s->cs) { |
Willy Tarreau | 2153d3c | 2017-12-15 11:56:29 +0100 | [diff] [blame] | 1498 | h2s->cs->flags |= CS_FL_EOS | CS_FL_ERROR; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1499 | /* recv is used to force to detect CS_FL_EOS that wake() |
| 1500 | * doesn't handle in the stream-int code. |
| 1501 | */ |
| 1502 | h2s->cs->data_cb->recv(h2s->cs); |
| 1503 | h2s->cs->data_cb->wake(h2s->cs); |
| 1504 | } |
| 1505 | |
| 1506 | h2s->flags |= H2_SF_RST_RCVD; |
| 1507 | return 1; |
| 1508 | |
| 1509 | conn_err: |
| 1510 | h2c_error(h2c, error); |
| 1511 | return 0; |
| 1512 | } |
| 1513 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1514 | /* processes a HEADERS frame. Returns > 0 on success or zero on missing data. |
| 1515 | * It may return an error in h2c or h2s. Described in RFC7540#6.2. Most of the |
| 1516 | * errors here are reported as connection errors since it's impossible to |
| 1517 | * recover from such errors after the compression context has been altered. |
| 1518 | */ |
| 1519 | static int h2c_frt_handle_headers(struct h2c *h2c, struct h2s *h2s) |
| 1520 | { |
| 1521 | int error; |
| 1522 | |
| 1523 | if (!h2c->dfl) { |
| 1524 | error = H2_ERR_PROTOCOL_ERROR; // empty headers frame! |
| 1525 | goto strm_err; |
| 1526 | } |
| 1527 | |
| 1528 | if (!h2c->dbuf->size) |
| 1529 | return 0; // empty buffer |
| 1530 | |
| 1531 | if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size) |
| 1532 | return 0; // incomplete frame |
| 1533 | |
| 1534 | /* now either the frame is complete or the buffer is complete */ |
| 1535 | if (h2s->st != H2_SS_IDLE) { |
| 1536 | /* FIXME: stream already exists, this is only allowed for |
| 1537 | * trailers (not supported for now). |
| 1538 | */ |
| 1539 | error = H2_ERR_PROTOCOL_ERROR; |
| 1540 | goto conn_err; |
| 1541 | } |
| 1542 | else if (h2c->dsi <= h2c->max_id || !(h2c->dsi & 1)) { |
| 1543 | /* RFC7540#5.1.1 stream id > prev ones, and must be odd here */ |
| 1544 | error = H2_ERR_PROTOCOL_ERROR; |
| 1545 | goto conn_err; |
| 1546 | } |
| 1547 | |
| 1548 | h2s = h2c_stream_new(h2c, h2c->dsi); |
| 1549 | if (!h2s) { |
| 1550 | error = H2_ERR_INTERNAL_ERROR; |
| 1551 | goto conn_err; |
| 1552 | } |
| 1553 | |
| 1554 | h2s->st = H2_SS_OPEN; |
| 1555 | if (h2c->dff & H2_F_HEADERS_END_STREAM) { |
| 1556 | h2s->st = H2_SS_HREM; |
| 1557 | h2s->flags |= H2_SF_ES_RCVD; |
| 1558 | } |
| 1559 | |
| 1560 | /* call the upper layers to process the frame, then let the upper layer |
| 1561 | * notify the stream about any change. |
| 1562 | */ |
| 1563 | h2s->cs->data_cb->recv(h2s->cs); |
| 1564 | |
| 1565 | if (h2s->cs->data_cb->wake(h2s->cs) < 0) { |
| 1566 | /* FIXME: cs has already been destroyed, but we have to kill h2s. */ |
| 1567 | error = H2_ERR_INTERNAL_ERROR; |
| 1568 | goto conn_err; |
| 1569 | } |
| 1570 | |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 1571 | if (h2c->st0 >= H2_CS_ERROR) |
| 1572 | return 0; |
| 1573 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 1574 | if (h2s->st >= H2_SS_ERROR) { |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1575 | /* stream error : send RST_STREAM */ |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1576 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1577 | } |
| 1578 | else { |
| 1579 | /* update the max stream ID if the request is being processed */ |
| 1580 | if (h2s->id > h2c->max_id) |
| 1581 | h2c->max_id = h2s->id; |
| 1582 | } |
| 1583 | |
| 1584 | return 1; |
| 1585 | |
| 1586 | conn_err: |
| 1587 | h2c_error(h2c, error); |
| 1588 | return 0; |
| 1589 | |
| 1590 | strm_err: |
| 1591 | if (h2s) { |
| 1592 | h2s_error(h2s, error); |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1593 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1594 | } |
| 1595 | else |
| 1596 | h2c_error(h2c, error); |
| 1597 | return 0; |
| 1598 | } |
| 1599 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1600 | /* processes a DATA frame. Returns > 0 on success or zero on missing data. |
| 1601 | * It may return an error in h2c or h2s. Described in RFC7540#6.1. |
| 1602 | */ |
| 1603 | static int h2c_frt_handle_data(struct h2c *h2c, struct h2s *h2s) |
| 1604 | { |
| 1605 | int error; |
| 1606 | |
| 1607 | /* note that empty DATA frames are perfectly valid and sometimes used |
| 1608 | * to signal an end of stream (with the ES flag). |
| 1609 | */ |
| 1610 | |
| 1611 | if (!h2c->dbuf->size && h2c->dfl) |
| 1612 | return 0; // empty buffer |
| 1613 | |
| 1614 | if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size) |
| 1615 | return 0; // incomplete frame |
| 1616 | |
| 1617 | /* now either the frame is complete or the buffer is complete */ |
| 1618 | |
| 1619 | if (!h2c->dsi) { |
| 1620 | /* RFC7540#6.1 */ |
| 1621 | error = H2_ERR_PROTOCOL_ERROR; |
| 1622 | goto conn_err; |
| 1623 | } |
| 1624 | |
| 1625 | if (h2s->st != H2_SS_OPEN && h2s->st != H2_SS_HLOC) { |
| 1626 | /* RFC7540#6.1 */ |
| 1627 | error = H2_ERR_STREAM_CLOSED; |
| 1628 | goto strm_err; |
| 1629 | } |
| 1630 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1631 | /* call the upper layers to process the frame, then let the upper layer |
| 1632 | * notify the stream about any change. |
| 1633 | */ |
| 1634 | if (!h2s->cs) { |
| 1635 | error = H2_ERR_STREAM_CLOSED; |
| 1636 | goto strm_err; |
| 1637 | } |
| 1638 | |
| 1639 | h2s->cs->data_cb->recv(h2s->cs); |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 1640 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1641 | if (h2s->cs->data_cb->wake(h2s->cs) < 0) { |
| 1642 | /* cs has just been destroyed, we have to kill h2s. */ |
| 1643 | error = H2_ERR_STREAM_CLOSED; |
| 1644 | goto strm_err; |
| 1645 | } |
| 1646 | |
Willy Tarreau | 8f650c3 | 2017-11-21 19:36:21 +0100 | [diff] [blame] | 1647 | if (h2c->st0 >= H2_CS_ERROR) |
| 1648 | return 0; |
| 1649 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 1650 | if (h2s->st >= H2_SS_ERROR) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1651 | /* stream error : send RST_STREAM */ |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1652 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | /* check for completion : the callee will change this to FRAME_A or |
| 1656 | * FRAME_H once done. |
| 1657 | */ |
| 1658 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1659 | return 0; |
| 1660 | |
Willy Tarreau | c4134ba | 2017-12-11 18:45:08 +0100 | [diff] [blame] | 1661 | |
| 1662 | /* last frame */ |
| 1663 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
| 1664 | h2s->st = H2_SS_HREM; |
| 1665 | h2s->flags |= H2_SF_ES_RCVD; |
| 1666 | } |
| 1667 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1668 | return 1; |
| 1669 | |
| 1670 | conn_err: |
| 1671 | h2c_error(h2c, error); |
| 1672 | return 0; |
| 1673 | |
| 1674 | strm_err: |
| 1675 | if (h2s) { |
| 1676 | h2s_error(h2s, error); |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1677 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1678 | } |
| 1679 | else |
| 1680 | h2c_error(h2c, error); |
| 1681 | return 0; |
| 1682 | } |
| 1683 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 1684 | /* process Rx frames to be demultiplexed */ |
| 1685 | static void h2_process_demux(struct h2c *h2c) |
| 1686 | { |
Willy Tarreau | f3ee069 | 2017-10-17 08:18:25 +0200 | [diff] [blame] | 1687 | struct h2s *h2s; |
| 1688 | |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 1689 | if (h2c->st0 >= H2_CS_ERROR) |
| 1690 | return; |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1691 | |
| 1692 | if (unlikely(h2c->st0 < H2_CS_FRAME_H)) { |
| 1693 | if (h2c->st0 == H2_CS_PREFACE) { |
| 1694 | if (unlikely(h2c_frt_recv_preface(h2c) <= 0)) { |
| 1695 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 1696 | if (h2c->st0 == H2_CS_ERROR) |
| 1697 | h2c->st0 = H2_CS_ERROR2; |
| 1698 | goto fail; |
| 1699 | } |
| 1700 | |
| 1701 | h2c->max_id = 0; |
| 1702 | h2c->st0 = H2_CS_SETTINGS1; |
| 1703 | } |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 1704 | |
| 1705 | if (h2c->st0 == H2_CS_SETTINGS1) { |
| 1706 | struct h2_fh hdr; |
| 1707 | |
| 1708 | /* ensure that what is pending is a valid SETTINGS frame |
| 1709 | * without an ACK. |
| 1710 | */ |
| 1711 | if (!h2_get_frame_hdr(h2c->dbuf, &hdr)) { |
| 1712 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 1713 | if (h2c->st0 == H2_CS_ERROR) |
| 1714 | h2c->st0 = H2_CS_ERROR2; |
| 1715 | goto fail; |
| 1716 | } |
| 1717 | |
| 1718 | if (hdr.sid || hdr.ft != H2_FT_SETTINGS || hdr.ff & H2_F_SETTINGS_ACK) { |
| 1719 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 1720 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1721 | h2c->st0 = H2_CS_ERROR2; |
| 1722 | goto fail; |
| 1723 | } |
| 1724 | |
| 1725 | if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) { |
| 1726 | /* RFC7540#3.5: a GOAWAY frame MAY be omitted */ |
| 1727 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 1728 | h2c->st0 = H2_CS_ERROR2; |
| 1729 | goto fail; |
| 1730 | } |
| 1731 | |
| 1732 | /* that's OK, switch to FRAME_P to process it */ |
| 1733 | h2c->dfl = hdr.len; |
| 1734 | h2c->dsi = hdr.sid; |
| 1735 | h2c->dft = hdr.ft; |
| 1736 | h2c->dff = hdr.ff; |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 1737 | h2c->dpl = 0; |
Willy Tarreau | 4c3690b | 2017-10-10 15:16:55 +0200 | [diff] [blame] | 1738 | h2c->st0 = H2_CS_FRAME_P; |
| 1739 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1740 | } |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 1741 | |
| 1742 | /* process as many incoming frames as possible below */ |
| 1743 | while (h2c->dbuf->i) { |
| 1744 | int ret = 0; |
| 1745 | |
| 1746 | if (h2c->st0 >= H2_CS_ERROR) |
| 1747 | break; |
| 1748 | |
| 1749 | if (h2c->st0 == H2_CS_FRAME_H) { |
| 1750 | struct h2_fh hdr; |
| 1751 | |
| 1752 | if (!h2_peek_frame_hdr(h2c->dbuf, &hdr)) |
| 1753 | break; |
| 1754 | |
| 1755 | if ((int)hdr.len < 0 || (int)hdr.len > h2c->mfs) { |
| 1756 | h2c_error(h2c, H2_ERR_FRAME_SIZE_ERROR); |
| 1757 | h2c->st0 = H2_CS_ERROR; |
| 1758 | break; |
| 1759 | } |
| 1760 | |
| 1761 | h2c->dfl = hdr.len; |
| 1762 | h2c->dsi = hdr.sid; |
| 1763 | h2c->dft = hdr.ft; |
| 1764 | h2c->dff = hdr.ff; |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 1765 | h2c->dpl = 0; |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 1766 | h2c->st0 = H2_CS_FRAME_P; |
| 1767 | h2_skip_frame_hdr(h2c->dbuf); |
| 1768 | } |
| 1769 | |
| 1770 | /* Only H2_CS_FRAME_P and H2_CS_FRAME_A here */ |
Willy Tarreau | f3ee069 | 2017-10-17 08:18:25 +0200 | [diff] [blame] | 1771 | h2s = h2c_st_by_id(h2c, h2c->dsi); |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 1772 | |
Willy Tarreau | d790143 | 2017-12-29 11:34:40 +0100 | [diff] [blame] | 1773 | if (h2c->st0 == H2_CS_FRAME_E) |
| 1774 | goto strm_err; |
| 1775 | |
Willy Tarreau | f65b80d | 2017-10-30 11:46:49 +0100 | [diff] [blame] | 1776 | if (h2s->st == H2_SS_IDLE && |
| 1777 | h2c->dft != H2_FT_HEADERS && h2c->dft != H2_FT_PRIORITY) { |
| 1778 | /* RFC7540#5.1: any frame other than HEADERS or PRIORITY in |
| 1779 | * this state MUST be treated as a connection error |
| 1780 | */ |
| 1781 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 1782 | h2c->st0 = H2_CS_ERROR; |
| 1783 | break; |
| 1784 | } |
| 1785 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 1786 | if (h2s->st == H2_SS_HREM && h2c->dft != H2_FT_WINDOW_UPDATE && |
| 1787 | h2c->dft != H2_FT_RST_STREAM && h2c->dft != H2_FT_PRIORITY) { |
| 1788 | /* RFC7540#5.1: any frame other than WU/PRIO/RST in |
| 1789 | * this state MUST be treated as a stream error |
| 1790 | */ |
| 1791 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1792 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 1793 | goto strm_err; |
| 1794 | } |
| 1795 | |
Willy Tarreau | ab83750 | 2017-12-27 15:07:30 +0100 | [diff] [blame] | 1796 | /* Below the management of frames received in closed state is a |
| 1797 | * bit hackish because the spec makes strong differences between |
| 1798 | * streams closed by receiving RST, sending RST, and seeing ES |
| 1799 | * in both directions. In addition to this, the creation of a |
| 1800 | * new stream reusing the identifier of a closed one will be |
| 1801 | * detected here. Given that we cannot keep track of all closed |
| 1802 | * streams forever, we consider that unknown closed streams were |
| 1803 | * closed on RST received, which allows us to respond with an |
| 1804 | * RST without breaking the connection (eg: to abort a transfer). |
| 1805 | * Some frames have to be silently ignored as well. |
| 1806 | */ |
| 1807 | if (h2s->st == H2_SS_CLOSED && h2c->dsi) { |
| 1808 | if (h2c->dft == H2_FT_HEADERS || h2c->dft == H2_FT_PUSH_PROMISE) { |
| 1809 | /* #5.1.1: The identifier of a newly |
| 1810 | * established stream MUST be numerically |
| 1811 | * greater than all streams that the initiating |
| 1812 | * endpoint has opened or reserved. This |
| 1813 | * governs streams that are opened using a |
| 1814 | * HEADERS frame and streams that are reserved |
| 1815 | * using PUSH_PROMISE. An endpoint that |
| 1816 | * receives an unexpected stream identifier |
| 1817 | * MUST respond with a connection error. |
| 1818 | */ |
| 1819 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 1820 | goto strm_err; |
| 1821 | } |
| 1822 | |
| 1823 | if (h2s->flags & H2_SF_RST_RCVD) { |
| 1824 | /* RFC7540#5.1:closed: an endpoint that |
| 1825 | * receives any frame other than PRIORITY after |
| 1826 | * receiving a RST_STREAM MUST treat that as a |
| 1827 | * stream error of type STREAM_CLOSED. |
| 1828 | * |
| 1829 | * Note that old streams fall into this category |
| 1830 | * and will lead to an RST being sent. |
| 1831 | */ |
| 1832 | h2s_error(h2s, H2_ERR_STREAM_CLOSED); |
| 1833 | h2c->st0 = H2_CS_FRAME_E; |
| 1834 | goto strm_err; |
| 1835 | } |
| 1836 | |
| 1837 | /* RFC7540#5.1:closed: if this state is reached as a |
| 1838 | * result of sending a RST_STREAM frame, the peer that |
| 1839 | * receives the RST_STREAM might have already sent |
| 1840 | * frames on the stream that cannot be withdrawn. An |
| 1841 | * endpoint MUST ignore frames that it receives on |
| 1842 | * closed streams after it has sent a RST_STREAM |
| 1843 | * frame. An endpoint MAY choose to limit the period |
| 1844 | * over which it ignores frames and treat frames that |
| 1845 | * arrive after this time as being in error. |
| 1846 | */ |
| 1847 | if (!(h2s->flags & H2_SF_RST_SENT)) { |
| 1848 | /* RFC7540#5.1:closed: any frame other than |
| 1849 | * PRIO/WU/RST in this state MUST be treated as |
| 1850 | * a connection error |
| 1851 | */ |
| 1852 | if (h2c->dft != H2_FT_RST_STREAM && |
| 1853 | h2c->dft != H2_FT_PRIORITY && |
| 1854 | h2c->dft != H2_FT_WINDOW_UPDATE) { |
| 1855 | h2c_error(h2c, H2_ERR_STREAM_CLOSED); |
| 1856 | goto strm_err; |
| 1857 | } |
| 1858 | } |
| 1859 | } |
| 1860 | |
Willy Tarreau | c0da196 | 2017-10-30 18:38:00 +0100 | [diff] [blame] | 1861 | #if 0 |
| 1862 | // problem below: it is not possible to completely ignore such |
| 1863 | // streams as we need to maintain the compression state as well |
| 1864 | // and for this we need to completely process these frames (eg: |
| 1865 | // HEADERS frames) as well as counting DATA frames to emit |
| 1866 | // proper WINDOW UPDATES and ensure the connection doesn't stall. |
| 1867 | // This is a typical case of layer violation where the |
| 1868 | // transported contents are critical to the connection's |
| 1869 | // validity and must be ignored at the same time :-( |
| 1870 | |
| 1871 | /* graceful shutdown, ignore streams whose ID is higher than |
| 1872 | * the one advertised in GOAWAY. RFC7540#6.8. |
| 1873 | */ |
| 1874 | if (unlikely(h2c->last_sid >= 0) && h2c->dsi > h2c->last_sid) { |
| 1875 | ret = MIN(h2c->dbuf->i, h2c->dfl); |
| 1876 | bi_del(h2c->dbuf, ret); |
| 1877 | h2c->dfl -= ret; |
| 1878 | ret = h2c->dfl == 0; |
| 1879 | goto strm_err; |
| 1880 | } |
| 1881 | #endif |
| 1882 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 1883 | switch (h2c->dft) { |
Willy Tarreau | 3421aba | 2017-07-27 15:41:03 +0200 | [diff] [blame] | 1884 | case H2_FT_SETTINGS: |
| 1885 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1886 | ret = h2c_handle_settings(h2c); |
| 1887 | |
| 1888 | if (h2c->st0 == H2_CS_FRAME_A) |
| 1889 | ret = h2c_ack_settings(h2c); |
| 1890 | break; |
| 1891 | |
Willy Tarreau | cf68c78 | 2017-10-10 17:11:41 +0200 | [diff] [blame] | 1892 | case H2_FT_PING: |
| 1893 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1894 | ret = h2c_handle_ping(h2c); |
| 1895 | |
| 1896 | if (h2c->st0 == H2_CS_FRAME_A) |
| 1897 | ret = h2c_ack_ping(h2c); |
| 1898 | break; |
| 1899 | |
Willy Tarreau | 26f9595 | 2017-07-27 17:18:30 +0200 | [diff] [blame] | 1900 | case H2_FT_WINDOW_UPDATE: |
| 1901 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1902 | ret = h2c_handle_window_update(h2c, h2s); |
| 1903 | break; |
| 1904 | |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 1905 | case H2_FT_CONTINUATION: |
| 1906 | /* we currently don't support CONTINUATION frames since |
| 1907 | * we have nowhere to store the partial HEADERS frame. |
| 1908 | * Let's abort the stream on an INTERNAL_ERROR here. |
| 1909 | */ |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1910 | if (h2c->st0 == H2_CS_FRAME_P) { |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 1911 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1912 | h2c->st0 = H2_CS_FRAME_E; |
| 1913 | } |
Willy Tarreau | 61290ec | 2017-10-17 08:19:21 +0200 | [diff] [blame] | 1914 | break; |
| 1915 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 1916 | case H2_FT_HEADERS: |
| 1917 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1918 | ret = h2c_frt_handle_headers(h2c, h2s); |
| 1919 | break; |
| 1920 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 1921 | case H2_FT_DATA: |
| 1922 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1923 | ret = h2c_frt_handle_data(h2c, h2s); |
| 1924 | |
| 1925 | if (h2c->st0 == H2_CS_FRAME_A) |
| 1926 | ret = h2c_send_strm_wu(h2c); |
| 1927 | break; |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1928 | |
Willy Tarreau | 92153fc | 2017-12-03 19:46:19 +0100 | [diff] [blame] | 1929 | case H2_FT_PRIORITY: |
| 1930 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1931 | ret = h2c_handle_priority(h2c); |
| 1932 | break; |
| 1933 | |
Willy Tarreau | cd234e9 | 2017-08-18 10:59:39 +0200 | [diff] [blame] | 1934 | case H2_FT_RST_STREAM: |
| 1935 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1936 | ret = h2c_handle_rst_stream(h2c, h2s); |
| 1937 | break; |
| 1938 | |
Willy Tarreau | e96b092 | 2017-10-30 00:28:29 +0100 | [diff] [blame] | 1939 | case H2_FT_GOAWAY: |
| 1940 | if (h2c->st0 == H2_CS_FRAME_P) |
| 1941 | ret = h2c_handle_goaway(h2c); |
| 1942 | break; |
| 1943 | |
Willy Tarreau | 1c66198 | 2017-10-30 13:52:01 +0100 | [diff] [blame] | 1944 | case H2_FT_PUSH_PROMISE: |
| 1945 | /* not permitted here, RFC7540#5.1 */ |
| 1946 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 1c66198 | 2017-10-30 13:52:01 +0100 | [diff] [blame] | 1947 | break; |
| 1948 | |
| 1949 | /* implement all extra frame types here */ |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 1950 | default: |
| 1951 | /* drop frames that we ignore. They may be larger than |
| 1952 | * the buffer so we drain all of their contents until |
| 1953 | * we reach the end. |
| 1954 | */ |
| 1955 | ret = MIN(h2c->dbuf->i, h2c->dfl); |
| 1956 | bi_del(h2c->dbuf, ret); |
| 1957 | h2c->dfl -= ret; |
| 1958 | ret = h2c->dfl == 0; |
| 1959 | } |
| 1960 | |
Willy Tarreau | f182a9a | 2017-10-30 12:03:50 +0100 | [diff] [blame] | 1961 | strm_err: |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1962 | /* We may have to send an RST if not done yet */ |
| 1963 | if (h2s->st == H2_SS_ERROR) |
| 1964 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1965 | |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 1966 | if (h2c->st0 == H2_CS_FRAME_E) |
| 1967 | ret = h2c_send_rst_stream(h2c, h2s); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 1968 | |
Willy Tarreau | 7e98c05 | 2017-10-10 15:56:59 +0200 | [diff] [blame] | 1969 | /* error or missing data condition met above ? */ |
| 1970 | if (ret <= 0) |
| 1971 | break; |
| 1972 | |
| 1973 | if (h2c->st0 != H2_CS_FRAME_H) { |
| 1974 | bi_del(h2c->dbuf, h2c->dfl); |
| 1975 | h2c->st0 = H2_CS_FRAME_H; |
| 1976 | } |
| 1977 | } |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1978 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1979 | if (h2c->rcvd_c > 0 && |
| 1980 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM))) |
| 1981 | h2c_send_conn_wu(h2c); |
| 1982 | |
Willy Tarreau | 52eed75 | 2017-09-22 15:05:09 +0200 | [diff] [blame] | 1983 | fail: |
| 1984 | /* we can go here on missing data, blocked response or error */ |
| 1985 | return; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 1986 | } |
| 1987 | |
| 1988 | /* process Tx frames from streams to be multiplexed. Returns > 0 if it reached |
| 1989 | * the end. |
| 1990 | */ |
| 1991 | static int h2_process_mux(struct h2c *h2c) |
| 1992 | { |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 1993 | struct h2s *h2s, *h2s_back; |
| 1994 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 1995 | /* start by sending possibly pending window updates */ |
| 1996 | if (h2c->rcvd_c > 0 && |
| 1997 | !(h2c->flags & (H2_CF_MUX_MFULL | H2_CF_MUX_MALLOC)) && |
| 1998 | h2c_send_conn_wu(h2c) < 0) |
| 1999 | goto fail; |
| 2000 | |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2001 | /* First we always process the flow control list because the streams |
| 2002 | * waiting there were already elected for immediate emission but were |
| 2003 | * blocked just on this. |
| 2004 | */ |
| 2005 | |
| 2006 | list_for_each_entry_safe(h2s, h2s_back, &h2c->fctl_list, list) { |
| 2007 | if (h2c->mws <= 0 || h2c->flags & H2_CF_MUX_BLOCK_ANY || |
| 2008 | h2c->st0 >= H2_CS_ERROR) |
| 2009 | break; |
| 2010 | |
| 2011 | /* In theory it's possible that h2s->cs == NULL here : |
| 2012 | * - client sends crap that causes a parse error |
| 2013 | * - RST_STREAM is produced and CS_FL_ERROR at the same time |
| 2014 | * - RST_STREAM cannot be emitted because mux is busy/full |
| 2015 | * - stream gets notified, detaches and quits |
| 2016 | * - mux buffer gets ready and wakes pending streams up |
| 2017 | * - bam! |
| 2018 | */ |
| 2019 | h2s->flags &= ~H2_SF_BLK_ANY; |
| 2020 | |
| 2021 | if (h2s->cs) { |
| 2022 | h2s->cs->data_cb->send(h2s->cs); |
| 2023 | h2s->cs->data_cb->wake(h2s->cs); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2024 | } else { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 2025 | h2s_send_rst_stream(h2c, h2s); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | /* depending on callee's blocking reasons, we may queue in send |
| 2029 | * list or completely dequeue. |
| 2030 | */ |
| 2031 | if ((h2s->flags & H2_SF_BLK_MFCTL) == 0) { |
| 2032 | if (h2s->flags & H2_SF_BLK_ANY) { |
| 2033 | LIST_DEL(&h2s->list); |
| 2034 | LIST_ADDQ(&h2c->send_list, &h2s->list); |
| 2035 | } |
| 2036 | else { |
| 2037 | LIST_DEL(&h2s->list); |
| 2038 | LIST_INIT(&h2s->list); |
| 2039 | if (h2s->cs) |
| 2040 | h2s->cs->flags &= ~CS_FL_DATA_WR_ENA; |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 2041 | else { |
| 2042 | /* just sent the last frame for this orphaned stream */ |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 2043 | h2s_detach(h2s); |
| 2044 | h2s_free(h2s); |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 2045 | } |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2046 | } |
| 2047 | } |
| 2048 | } |
| 2049 | |
| 2050 | list_for_each_entry_safe(h2s, h2s_back, &h2c->send_list, list) { |
| 2051 | if (h2c->st0 >= H2_CS_ERROR || h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2052 | break; |
| 2053 | |
| 2054 | /* In theory it's possible that h2s->cs == NULL here : |
| 2055 | * - client sends crap that causes a parse error |
| 2056 | * - RST_STREAM is produced and CS_FL_ERROR at the same time |
| 2057 | * - RST_STREAM cannot be emitted because mux is busy/full |
| 2058 | * - stream gets notified, detaches and quits |
| 2059 | * - mux buffer gets ready and wakes pending streams up |
| 2060 | * - bam! |
| 2061 | */ |
| 2062 | h2s->flags &= ~H2_SF_BLK_ANY; |
| 2063 | |
| 2064 | if (h2s->cs) { |
| 2065 | h2s->cs->data_cb->send(h2s->cs); |
| 2066 | h2s->cs->data_cb->wake(h2s->cs); |
Willy Tarreau | 27a84c9 | 2017-10-17 08:10:17 +0200 | [diff] [blame] | 2067 | } else { |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 2068 | h2s_send_rst_stream(h2c, h2s); |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2069 | } |
| 2070 | /* depending on callee's blocking reasons, we may queue in fctl |
| 2071 | * list or completely dequeue. |
| 2072 | */ |
| 2073 | if (h2s->flags & H2_SF_BLK_MFCTL) { |
| 2074 | /* stream hit the connection's flow control */ |
| 2075 | LIST_DEL(&h2s->list); |
| 2076 | LIST_ADDQ(&h2c->fctl_list, &h2s->list); |
| 2077 | } |
| 2078 | else if (!(h2s->flags & H2_SF_BLK_ANY)) { |
| 2079 | LIST_DEL(&h2s->list); |
| 2080 | LIST_INIT(&h2s->list); |
| 2081 | if (h2s->cs) |
| 2082 | h2s->cs->flags &= ~CS_FL_DATA_WR_ENA; |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 2083 | else { |
| 2084 | /* just sent the last frame for this orphaned stream */ |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 2085 | h2s_detach(h2s); |
| 2086 | h2s_free(h2s); |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 2087 | } |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2088 | } |
| 2089 | } |
| 2090 | |
Willy Tarreau | cc0b8c3 | 2017-10-26 16:55:59 +0200 | [diff] [blame] | 2091 | fail: |
Willy Tarreau | 3eabe9b | 2017-11-07 11:03:01 +0100 | [diff] [blame] | 2092 | if (unlikely(h2c->st0 >= H2_CS_ERROR)) { |
Willy Tarreau | 081d472 | 2017-05-16 21:51:05 +0200 | [diff] [blame] | 2093 | if (h2c->st0 == H2_CS_ERROR) { |
| 2094 | if (h2c->max_id >= 0) { |
| 2095 | h2c_send_goaway_error(h2c, NULL); |
| 2096 | if (h2c->flags & H2_CF_MUX_BLOCK_ANY) |
| 2097 | return 0; |
| 2098 | } |
| 2099 | |
| 2100 | h2c->st0 = H2_CS_ERROR2; // sent (or failed hard) ! |
| 2101 | } |
| 2102 | return 1; |
| 2103 | } |
Willy Tarreau | bacdf5a | 2017-10-17 10:57:04 +0200 | [diff] [blame] | 2104 | return (h2c->mws <= 0 || LIST_ISEMPTY(&h2c->fctl_list)) && LIST_ISEMPTY(&h2c->send_list); |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2105 | } |
| 2106 | |
Willy Tarreau | 7168117 | 2017-10-23 14:39:06 +0200 | [diff] [blame] | 2107 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2108 | /*********************************************************/ |
| 2109 | /* functions below are I/O callbacks from the connection */ |
| 2110 | /*********************************************************/ |
| 2111 | |
| 2112 | /* callback called on recv event by the connection handler */ |
| 2113 | static void h2_recv(struct connection *conn) |
| 2114 | { |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2115 | struct h2c *h2c = conn->mux_ctx; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2116 | struct buffer *buf; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2117 | int max; |
| 2118 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2119 | if (!h2_recv_allowed(h2c)) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2120 | return; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2121 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2122 | buf = h2_get_buf(h2c, &h2c->dbuf); |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2123 | if (!buf) { |
| 2124 | h2c->flags |= H2_CF_DEM_DALLOC; |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2125 | return; |
Willy Tarreau | 1b62c5c | 2017-09-25 11:55:01 +0200 | [diff] [blame] | 2126 | } |
Willy Tarreau | 35dbd5d | 2017-09-22 09:13:49 +0200 | [diff] [blame] | 2127 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2128 | /* note: buf->o == 0 */ |
| 2129 | max = buf->size - buf->i; |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2130 | if (max) |
| 2131 | conn->xprt->rcv_buf(conn, buf, max); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2132 | |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2133 | if (!buf->i) { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2134 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2135 | return; |
| 2136 | } |
| 2137 | |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2138 | if (buf->i == buf->size) |
| 2139 | h2c->flags |= H2_CF_DEM_DFULL; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2140 | return; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2141 | } |
| 2142 | |
| 2143 | /* callback called on send event by the connection handler */ |
| 2144 | static void h2_send(struct connection *conn) |
| 2145 | { |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2146 | struct h2c *h2c = conn->mux_ctx; |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2147 | int done; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2148 | |
| 2149 | if (conn->flags & CO_FL_ERROR) |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2150 | return; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2151 | |
| 2152 | if (conn->flags & (CO_FL_HANDSHAKE|CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) { |
| 2153 | /* a handshake was requested */ |
| 2154 | return; |
| 2155 | } |
| 2156 | |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2157 | /* This loop is quite simple : it tries to fill as much as it can from |
| 2158 | * pending streams into the existing buffer until it's reportedly full |
| 2159 | * or the end of send requests is reached. Then it tries to send this |
| 2160 | * buffer's contents out, marks it not full if at least one byte could |
| 2161 | * be sent, and tries again. |
| 2162 | * |
| 2163 | * The snd_buf() function normally takes a "flags" argument which may |
| 2164 | * be made of a combination of CO_SFL_MSG_MORE to indicate that more |
| 2165 | * data immediately comes and CO_SFL_STREAMER to indicate that the |
| 2166 | * connection is streaming lots of data (used to increase TLS record |
| 2167 | * size at the expense of latency). The former can be sent any time |
| 2168 | * there's a buffer full flag, as it indicates at least one stream |
| 2169 | * attempted to send and failed so there are pending data. An |
| 2170 | * alternative would be to set it as long as there's an active stream |
| 2171 | * but that would be problematic for ACKs until we have an absolute |
| 2172 | * guarantee that all waiters have at least one byte to send. The |
| 2173 | * latter should possibly not be set for now. |
| 2174 | */ |
| 2175 | |
| 2176 | done = 0; |
| 2177 | while (!done) { |
| 2178 | unsigned int flags = 0; |
| 2179 | |
| 2180 | /* fill as much as we can into the current buffer */ |
| 2181 | while (((h2c->flags & (H2_CF_MUX_MFULL|H2_CF_MUX_MALLOC)) == 0) && !done) |
| 2182 | done = h2_process_mux(h2c); |
| 2183 | |
| 2184 | if (conn->flags & CO_FL_ERROR) |
| 2185 | break; |
| 2186 | |
| 2187 | if (h2c->flags & (H2_CF_MUX_MFULL | H2_CF_DEM_MBUSY | H2_CF_DEM_MROOM)) |
| 2188 | flags |= CO_SFL_MSG_MORE; |
| 2189 | |
Willy Tarreau | 319994a | 2017-11-07 11:03:56 +0100 | [diff] [blame] | 2190 | if (h2c->mbuf->o && conn->xprt->snd_buf(conn, h2c->mbuf, flags) <= 0) |
Willy Tarreau | bc93393 | 2017-10-09 16:21:43 +0200 | [diff] [blame] | 2191 | break; |
| 2192 | |
| 2193 | /* wrote at least one byte, the buffer is not full anymore */ |
| 2194 | h2c->flags &= ~(H2_CF_MUX_MFULL | H2_CF_DEM_MROOM); |
| 2195 | } |
| 2196 | |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2197 | if (conn->flags & CO_FL_SOCK_WR_SH) { |
| 2198 | /* output closed, nothing to send, clear the buffer to release it */ |
| 2199 | h2c->mbuf->o = 0; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2200 | } |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2201 | } |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2202 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2203 | /* callback called on any event by the connection handler. |
| 2204 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 2205 | * destruction of the connection (which normally doesn not happen in h2). |
| 2206 | */ |
| 2207 | static int h2_wake(struct connection *conn) |
| 2208 | { |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2209 | struct h2c *h2c = conn->mux_ctx; |
Willy Tarreau | 8ec1406 | 2017-12-30 18:08:13 +0100 | [diff] [blame] | 2210 | struct session *sess = conn->owner; |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2211 | |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2212 | if (h2c->dbuf->i && !(h2c->flags & H2_CF_DEM_BLOCK_ANY)) { |
| 2213 | h2_process_demux(h2c); |
| 2214 | |
| 2215 | if (h2c->st0 >= H2_CS_ERROR || conn->flags & CO_FL_ERROR) |
| 2216 | h2c->dbuf->i = 0; |
| 2217 | |
| 2218 | if (h2c->dbuf->i != h2c->dbuf->size) |
| 2219 | h2c->flags &= ~H2_CF_DEM_DFULL; |
| 2220 | } |
| 2221 | |
Willy Tarreau | 8ec1406 | 2017-12-30 18:08:13 +0100 | [diff] [blame] | 2222 | if (sess && unlikely(sess->fe->state == PR_STSTOPPED)) { |
| 2223 | /* frontend is stopping, reload likely in progress, let's try |
| 2224 | * to announce a graceful shutdown if not yet done. We don't |
| 2225 | * care if it fails, it will be tried again later. |
| 2226 | */ |
| 2227 | if (!(h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED))) { |
| 2228 | if (h2c->last_sid < 0) |
| 2229 | h2c->last_sid = (1U << 31) - 1; |
| 2230 | h2c_send_goaway_error(h2c, NULL); |
| 2231 | } |
| 2232 | } |
| 2233 | |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2234 | /* |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2235 | * If we received early data, and the handshake is done, wake |
| 2236 | * any stream that was waiting for it. |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2237 | */ |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2238 | if (!(h2c->flags & H2_CF_WAIT_FOR_HS) && |
| 2239 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE | CO_FL_EARLY_DATA)) == CO_FL_EARLY_DATA) { |
| 2240 | struct eb32_node *node; |
| 2241 | struct h2s *h2s; |
| 2242 | |
| 2243 | h2c->flags |= H2_CF_WAIT_FOR_HS; |
| 2244 | node = eb32_lookup_ge(&h2c->streams_by_id, 1); |
| 2245 | |
| 2246 | while (node) { |
| 2247 | h2s = container_of(node, struct h2s, by_id); |
| 2248 | if (h2s->cs->flags & CS_FL_WAIT_FOR_HS) |
| 2249 | h2s->cs->data_cb->wake(h2s->cs); |
| 2250 | node = eb32_next(node); |
| 2251 | } |
Olivier Houchard | 7fc96d5 | 2017-11-23 18:25:47 +0100 | [diff] [blame] | 2252 | } |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 2253 | |
Willy Tarreau | 26bd761 | 2017-10-09 16:47:04 +0200 | [diff] [blame] | 2254 | if (conn->flags & CO_FL_ERROR || conn_xprt_read0_pending(conn) || |
Willy Tarreau | 29a9824 | 2017-10-31 06:59:15 +0100 | [diff] [blame] | 2255 | h2c->st0 == H2_CS_ERROR2 || h2c->flags & H2_CF_GOAWAY_FAILED || |
| 2256 | (eb_is_empty(&h2c->streams_by_id) && h2c->last_sid >= 0 && |
| 2257 | h2c->max_id >= h2c->last_sid)) { |
Willy Tarreau | 23b92aa | 2017-10-30 00:26:54 +0100 | [diff] [blame] | 2258 | h2_wake_some_streams(h2c, 0, 0); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2259 | |
| 2260 | if (eb_is_empty(&h2c->streams_by_id)) { |
| 2261 | /* no more stream, kill the connection now */ |
| 2262 | h2_release(conn); |
| 2263 | return -1; |
| 2264 | } |
| 2265 | else { |
| 2266 | /* some streams still there, we need to signal them all and |
| 2267 | * wait for their departure. |
| 2268 | */ |
| 2269 | __conn_xprt_stop_recv(conn); |
| 2270 | __conn_xprt_stop_send(conn); |
| 2271 | return 0; |
| 2272 | } |
| 2273 | } |
| 2274 | |
| 2275 | if (!h2c->dbuf->i) |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2276 | h2_release_buf(h2c, &h2c->dbuf); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2277 | |
| 2278 | /* stop being notified of incoming data if we can't process them */ |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2279 | if (!h2_recv_allowed(h2c)) { |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2280 | __conn_xprt_stop_recv(conn); |
| 2281 | } |
| 2282 | else { |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2283 | __conn_xprt_want_recv(conn); |
| 2284 | } |
| 2285 | |
| 2286 | /* adjust output polling */ |
Willy Tarreau | 5160683 | 2017-10-17 15:30:07 +0200 | [diff] [blame] | 2287 | if (!(conn->flags & CO_FL_SOCK_WR_SH) && |
| 2288 | (h2c->st0 == H2_CS_ERROR || |
| 2289 | h2c->mbuf->o || |
| 2290 | (h2c->mws > 0 && !LIST_ISEMPTY(&h2c->fctl_list)) || |
| 2291 | (!(h2c->flags & H2_CF_MUX_BLOCK_ANY) && !LIST_ISEMPTY(&h2c->send_list)))) { |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2292 | __conn_xprt_want_send(conn); |
| 2293 | } |
| 2294 | else { |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2295 | h2_release_buf(h2c, &h2c->mbuf); |
Willy Tarreau | fbe3b4f | 2017-10-09 15:14:19 +0200 | [diff] [blame] | 2296 | __conn_xprt_stop_send(conn); |
Willy Tarreau | a2af512 | 2017-10-09 11:56:46 +0200 | [diff] [blame] | 2297 | } |
| 2298 | |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 2299 | if (h2c->task) { |
Willy Tarreau | 84b118f | 2018-03-05 16:10:54 +0100 | [diff] [blame] | 2300 | if (eb_is_empty(&h2c->streams_by_id) || h2c->mbuf->o) { |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 2301 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
Willy Tarreau | 3f13357 | 2017-10-31 19:21:06 +0100 | [diff] [blame] | 2302 | task_queue(h2c->task); |
| 2303 | } |
| 2304 | else |
| 2305 | h2c->task->expire = TICK_ETERNITY; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2306 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2307 | return 0; |
| 2308 | } |
| 2309 | |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2310 | /* Connection timeout management. The principle is that if there's no receipt |
| 2311 | * nor sending for a certain amount of time, the connection is closed. If the |
| 2312 | * MUX buffer still has lying data or is not allocatable, the connection is |
| 2313 | * immediately killed. If it's allocatable and empty, we attempt to send a |
| 2314 | * GOAWAY frame. |
| 2315 | */ |
| 2316 | static struct task *h2_timeout_task(struct task *t) |
| 2317 | { |
| 2318 | struct h2c *h2c = t->context; |
| 2319 | int expired = tick_is_expired(t->expire, now_ms); |
| 2320 | |
| 2321 | if (!expired) |
| 2322 | return t; |
| 2323 | |
| 2324 | h2c_error(h2c, H2_ERR_NO_ERROR); |
| 2325 | h2_wake_some_streams(h2c, 0, 0); |
| 2326 | |
| 2327 | if (h2c->mbuf->o) { |
| 2328 | /* don't even try to send a GOAWAY, the buffer is stuck */ |
| 2329 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 2330 | } |
| 2331 | |
| 2332 | /* try to send but no need to insist */ |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 2333 | h2c->last_sid = h2c->max_id; |
Willy Tarreau | ea39282 | 2017-10-31 10:02:25 +0100 | [diff] [blame] | 2334 | if (h2c_send_goaway_error(h2c, NULL) <= 0) |
| 2335 | h2c->flags |= H2_CF_GOAWAY_FAILED; |
| 2336 | |
| 2337 | if (h2c->mbuf->o && !(h2c->flags & H2_CF_GOAWAY_FAILED) && conn_xprt_ready(h2c->conn)) |
| 2338 | h2c->conn->xprt->snd_buf(h2c->conn, h2c->mbuf, 0); |
| 2339 | |
| 2340 | if (!eb_is_empty(&h2c->streams_by_id)) |
| 2341 | goto wait; |
| 2342 | |
| 2343 | h2_release(h2c->conn); |
| 2344 | return NULL; |
| 2345 | |
| 2346 | wait: |
| 2347 | /* the streams have been notified, we must let them finish and close */ |
| 2348 | h2c->task = NULL; |
| 2349 | task_delete(t); |
| 2350 | task_free(t); |
| 2351 | return NULL; |
| 2352 | } |
| 2353 | |
| 2354 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2355 | /*******************************************/ |
| 2356 | /* functions below are used by the streams */ |
| 2357 | /*******************************************/ |
| 2358 | |
| 2359 | /* |
| 2360 | * Attach a new stream to a connection |
| 2361 | * (Used for outgoing connections) |
| 2362 | */ |
| 2363 | static struct conn_stream *h2_attach(struct connection *conn) |
| 2364 | { |
| 2365 | return NULL; |
| 2366 | } |
| 2367 | |
| 2368 | /* callback used to update the mux's polling flags after changing a cs' status. |
| 2369 | * The caller (cs_update_mux_polling) will take care of propagating any changes |
| 2370 | * to the transport layer. |
| 2371 | */ |
| 2372 | static void h2_update_poll(struct conn_stream *cs) |
| 2373 | { |
Willy Tarreau | 1d39322 | 2017-10-17 10:26:19 +0200 | [diff] [blame] | 2374 | struct h2s *h2s = cs->ctx; |
| 2375 | |
| 2376 | if (!h2s) |
| 2377 | return; |
| 2378 | |
Willy Tarreau | d7739c8 | 2017-10-30 15:38:23 +0100 | [diff] [blame] | 2379 | /* we may unblock a blocked read */ |
| 2380 | |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2381 | if (cs->flags & CS_FL_DATA_RD_ENA) { |
| 2382 | /* the stream indicates it's willing to read */ |
Willy Tarreau | d7739c8 | 2017-10-30 15:38:23 +0100 | [diff] [blame] | 2383 | h2s->h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2384 | if (h2s->h2c->dsi == h2s->id) { |
Willy Tarreau | 315d807 | 2017-12-10 22:17:57 +0100 | [diff] [blame] | 2385 | conn_xprt_want_recv(cs->conn); |
Willy Tarreau | d13bf27 | 2017-12-14 10:34:52 +0100 | [diff] [blame] | 2386 | conn_xprt_want_send(cs->conn); |
| 2387 | } |
Willy Tarreau | d7739c8 | 2017-10-30 15:38:23 +0100 | [diff] [blame] | 2388 | } |
| 2389 | |
Willy Tarreau | 1d39322 | 2017-10-17 10:26:19 +0200 | [diff] [blame] | 2390 | /* Note: the stream and stream-int code doesn't allow us to perform a |
| 2391 | * synchronous send() here unfortunately, because this code is called |
| 2392 | * as si_update() from the process_stream() context. This means that |
| 2393 | * we have to queue the current cs and defer its processing after the |
| 2394 | * connection's cs list is processed anyway. |
| 2395 | */ |
| 2396 | |
| 2397 | if (cs->flags & CS_FL_DATA_WR_ENA) { |
| 2398 | if (LIST_ISEMPTY(&h2s->list)) { |
| 2399 | if (LIST_ISEMPTY(&h2s->h2c->send_list) && |
| 2400 | !h2s->h2c->mbuf->o && // not yet subscribed |
| 2401 | !(cs->conn->flags & CO_FL_SOCK_WR_SH)) |
| 2402 | conn_xprt_want_send(cs->conn); |
| 2403 | LIST_ADDQ(&h2s->h2c->send_list, &h2s->list); |
| 2404 | } |
| 2405 | } |
| 2406 | else if (!LIST_ISEMPTY(&h2s->list)) { |
| 2407 | LIST_DEL(&h2s->list); |
| 2408 | LIST_INIT(&h2s->list); |
| 2409 | h2s->flags &= ~(H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL); |
| 2410 | } |
| 2411 | |
| 2412 | /* this can happen from within si_chk_snd() */ |
| 2413 | if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA)) |
| 2414 | conn_xprt_want_send(cs->conn); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2415 | } |
| 2416 | |
| 2417 | /* |
| 2418 | * Detach the stream from the connection and possibly release the connection. |
| 2419 | */ |
| 2420 | static void h2_detach(struct conn_stream *cs) |
| 2421 | { |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2422 | struct h2s *h2s = cs->ctx; |
| 2423 | struct h2c *h2c; |
| 2424 | |
| 2425 | cs->ctx = NULL; |
| 2426 | if (!h2s) |
| 2427 | return; |
| 2428 | |
| 2429 | h2c = h2s->h2c; |
| 2430 | h2s->cs = NULL; |
| 2431 | |
Willy Tarreau | 22cf59b | 2017-11-10 11:42:33 +0100 | [diff] [blame] | 2432 | /* this stream may be blocked waiting for some data to leave (possibly |
| 2433 | * an ES or RST frame), so orphan it in this case. |
| 2434 | */ |
| 2435 | if (h2s->flags & (H2_SF_BLK_MBUSY | H2_SF_BLK_MROOM | H2_SF_BLK_MFCTL)) |
| 2436 | return; |
| 2437 | |
Willy Tarreau | 541dd82 | 2017-11-23 18:12:50 +0100 | [diff] [blame] | 2438 | /* the stream could be in the send list */ |
| 2439 | LIST_DEL(&h2s->list); |
| 2440 | |
Willy Tarreau | 45f752e | 2017-10-30 15:44:59 +0100 | [diff] [blame] | 2441 | if ((h2c->flags & H2_CF_DEM_BLOCK_ANY && h2s->id == h2c->dsi) || |
| 2442 | (h2c->flags & H2_CF_MUX_BLOCK_ANY && h2s->id == h2c->msi)) { |
| 2443 | /* unblock the connection if it was blocked on this |
| 2444 | * stream. |
| 2445 | */ |
| 2446 | h2c->flags &= ~H2_CF_DEM_BLOCK_ANY; |
| 2447 | h2c->flags &= ~H2_CF_MUX_BLOCK_ANY; |
| 2448 | conn_xprt_want_recv(cs->conn); |
| 2449 | conn_xprt_want_send(cs->conn); |
| 2450 | } |
| 2451 | |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2452 | if (h2s->by_id.node.leaf_p) { |
| 2453 | /* h2s still attached to the h2c */ |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 2454 | h2s_detach(h2s); |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2455 | |
| 2456 | /* We don't want to close right now unless we're removing the |
| 2457 | * last stream, and either the connection is in error, or it |
| 2458 | * reached the ID already specified in a GOAWAY frame received |
Willy Tarreau | e6ae77f | 2017-11-07 11:59:51 +0100 | [diff] [blame] | 2459 | * or sent (as seen by last_sid >= 0). |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2460 | */ |
Willy Tarreau | 83906c2 | 2017-11-07 11:48:46 +0100 | [diff] [blame] | 2461 | if (eb_is_empty(&h2c->streams_by_id) && /* don't close if streams exist */ |
| 2462 | ((h2c->conn->flags & CO_FL_ERROR) || /* errors close immediately */ |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2463 | (h2c->flags & H2_CF_GOAWAY_FAILED) || |
Willy Tarreau | 83906c2 | 2017-11-07 11:48:46 +0100 | [diff] [blame] | 2464 | (!h2c->mbuf->o && /* mux buffer empty, also process clean events below */ |
| 2465 | (conn_xprt_read0_pending(h2c->conn) || |
| 2466 | (h2c->last_sid >= 0 && h2c->max_id >= h2c->last_sid))))) { |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2467 | /* no more stream will come, kill it now */ |
| 2468 | h2_release(h2c->conn); |
| 2469 | } |
Willy Tarreau | e6ae77f | 2017-11-07 11:59:51 +0100 | [diff] [blame] | 2470 | else if (h2c->task) { |
Willy Tarreau | 84b118f | 2018-03-05 16:10:54 +0100 | [diff] [blame] | 2471 | if (eb_is_empty(&h2c->streams_by_id) || h2c->mbuf->o) { |
Willy Tarreau | 599391a | 2017-11-24 10:16:00 +0100 | [diff] [blame] | 2472 | h2c->task->expire = tick_add(now_ms, h2c->last_sid < 0 ? h2c->timeout : h2c->shut_timeout); |
Willy Tarreau | e6ae77f | 2017-11-07 11:59:51 +0100 | [diff] [blame] | 2473 | task_queue(h2c->task); |
| 2474 | } |
| 2475 | else |
| 2476 | h2c->task->expire = TICK_ETERNITY; |
| 2477 | } |
Willy Tarreau | 6093514 | 2017-10-16 18:11:19 +0200 | [diff] [blame] | 2478 | } |
Willy Tarreau | 0a10de6 | 2018-03-01 16:27:53 +0100 | [diff] [blame] | 2479 | h2s_free(h2s); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2480 | } |
| 2481 | |
| 2482 | static void h2_shutr(struct conn_stream *cs, enum cs_shr_mode mode) |
| 2483 | { |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2484 | struct h2s *h2s = cs->ctx; |
| 2485 | |
| 2486 | if (!mode) |
| 2487 | return; |
| 2488 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 2489 | if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED) |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2490 | return; |
| 2491 | |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 2492 | /* if no outgoing data was seen on this stream, it means it was |
| 2493 | * closed with a "tcp-request content" rule that is normally |
| 2494 | * used to kill the connection ASAP (eg: limit abuse). In this |
| 2495 | * case we send a goaway to close the connection. |
| 2496 | */ |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 2497 | if (!(h2s->flags & H2_SF_RST_SENT) && |
| 2498 | h2s_send_rst_stream(h2s->h2c, h2s) <= 0) |
| 2499 | return; |
| 2500 | |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 2501 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && |
| 2502 | !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) && |
| 2503 | h2c_send_goaway_error(h2s->h2c, h2s) <= 0) |
| 2504 | return; |
| 2505 | |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2506 | if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA)) |
| 2507 | conn_xprt_want_send(cs->conn); |
| 2508 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 2509 | h2s_close(h2s); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2510 | } |
| 2511 | |
| 2512 | static void h2_shutw(struct conn_stream *cs, enum cs_shw_mode mode) |
| 2513 | { |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2514 | struct h2s *h2s = cs->ctx; |
| 2515 | |
Willy Tarreau | 721c974 | 2017-11-07 11:05:42 +0100 | [diff] [blame] | 2516 | if (h2s->st == H2_SS_HLOC || h2s->st == H2_SS_ERROR || h2s->st == H2_SS_CLOSED) |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2517 | return; |
| 2518 | |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 2519 | if (h2s->flags & H2_SF_HEADERS_SENT) { |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 2520 | /* we can cleanly close using an empty data frame only after headers */ |
| 2521 | |
| 2522 | if (!(h2s->flags & (H2_SF_ES_SENT|H2_SF_RST_SENT)) && |
| 2523 | h2_send_empty_data_es(h2s) <= 0) |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2524 | return; |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 2525 | |
| 2526 | if (h2s->st == H2_SS_HREM) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 2527 | h2s_close(h2s); |
Willy Tarreau | 58e3208 | 2017-11-07 14:41:09 +0100 | [diff] [blame] | 2528 | else |
| 2529 | h2s->st = H2_SS_HLOC; |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2530 | } else { |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 2531 | /* if no outgoing data was seen on this stream, it means it was |
| 2532 | * closed with a "tcp-request content" rule that is normally |
| 2533 | * used to kill the connection ASAP (eg: limit abuse). In this |
| 2534 | * case we send a goaway to close the connection. |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 2535 | */ |
Willy Tarreau | 90c3232 | 2017-11-24 08:00:30 +0100 | [diff] [blame] | 2536 | if (!(h2s->flags & H2_SF_RST_SENT) && |
| 2537 | h2s_send_rst_stream(h2s->h2c, h2s) <= 0) |
| 2538 | return; |
| 2539 | |
Willy Tarreau | 926fa4c | 2017-11-07 14:42:12 +0100 | [diff] [blame] | 2540 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && |
| 2541 | !(h2s->h2c->flags & (H2_CF_GOAWAY_SENT|H2_CF_GOAWAY_FAILED)) && |
Willy Tarreau | a1349f0 | 2017-10-31 07:41:55 +0100 | [diff] [blame] | 2542 | h2c_send_goaway_error(h2s->h2c, h2s) <= 0) |
| 2543 | return; |
| 2544 | |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 2545 | h2s_close(h2s); |
Willy Tarreau | c7576ea | 2017-10-29 22:00:09 +0100 | [diff] [blame] | 2546 | } |
| 2547 | |
| 2548 | if (h2s->h2c->mbuf->o && !(cs->conn->flags & CO_FL_XPRT_WR_ENA)) |
| 2549 | conn_xprt_want_send(cs->conn); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2550 | } |
| 2551 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2552 | /* Decode the payload of a HEADERS frame and produce the equivalent HTTP/1 |
| 2553 | * request. Returns the number of bytes emitted if > 0, or 0 if it couldn't |
| 2554 | * proceed. Stream errors are reported in h2s->errcode and connection errors |
Willy Tarreau | 6847262 | 2017-12-11 18:36:37 +0100 | [diff] [blame] | 2555 | * in h2c->errcode. |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2556 | */ |
| 2557 | static int h2_frt_decode_headers(struct h2s *h2s, struct buffer *buf, int count) |
| 2558 | { |
| 2559 | struct h2c *h2c = h2s->h2c; |
| 2560 | const uint8_t *hdrs = (uint8_t *)h2c->dbuf->p; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 2561 | struct chunk *tmp = get_trash_chunk(); |
| 2562 | struct http_hdr list[MAX_HTTP_HDR * 2]; |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 2563 | struct chunk *copy = NULL; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2564 | int flen = h2c->dfl; |
| 2565 | int outlen = 0; |
| 2566 | int wrap; |
| 2567 | int try; |
| 2568 | |
| 2569 | if (!h2c->dfl) { |
| 2570 | h2s_error(h2s, H2_ERR_PROTOCOL_ERROR); // empty headers frame! |
Willy Tarreau | a20a519 | 2017-12-27 11:02:06 +0100 | [diff] [blame] | 2571 | h2c->st0 = H2_CS_FRAME_E; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2572 | return 0; |
| 2573 | } |
| 2574 | |
Willy Tarreau | 6847262 | 2017-12-11 18:36:37 +0100 | [diff] [blame] | 2575 | if (h2c->dbuf->i < h2c->dfl && h2c->dbuf->i < h2c->dbuf->size) |
| 2576 | return 0; // incomplete input frame |
| 2577 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2578 | /* if the input buffer wraps, take a temporary copy of it (rare) */ |
| 2579 | wrap = h2c->dbuf->data + h2c->dbuf->size - h2c->dbuf->p; |
| 2580 | if (wrap < h2c->dfl) { |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 2581 | copy = alloc_trash_chunk(); |
| 2582 | if (!copy) { |
| 2583 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
| 2584 | goto fail; |
| 2585 | } |
| 2586 | memcpy(copy->str, h2c->dbuf->p, wrap); |
| 2587 | memcpy(copy->str + wrap, h2c->dbuf->data, h2c->dfl - wrap); |
| 2588 | hdrs = (uint8_t *)copy->str; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2589 | } |
| 2590 | |
| 2591 | /* The padlen is the first byte before data, and the padding appears |
| 2592 | * after data. padlen+data+padding are included in flen. |
| 2593 | */ |
| 2594 | if (h2c->dff & H2_F_HEADERS_PADDED) { |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 2595 | h2c->dpl = *hdrs; |
| 2596 | if (h2c->dpl >= flen) { |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2597 | /* RFC7540#6.2 : pad length = length of frame payload or greater */ |
| 2598 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2599 | return 0; |
| 2600 | } |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 2601 | flen -= h2c->dpl + 1; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2602 | hdrs += 1; // skip Pad Length |
| 2603 | } |
| 2604 | |
| 2605 | /* Skip StreamDep and weight for now (we don't support PRIORITY) */ |
| 2606 | if (h2c->dff & H2_F_HEADERS_PRIORITY) { |
Willy Tarreau | 18b86cd | 2017-12-03 19:24:50 +0100 | [diff] [blame] | 2607 | if (read_n32(hdrs) == h2s->id) { |
| 2608 | /* RFC7540#5.3.1 : stream dep may not depend on itself */ |
| 2609 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
| 2610 | return 0;//goto fail_stream; |
| 2611 | } |
| 2612 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2613 | hdrs += 5; // stream dep = 4, weight = 1 |
| 2614 | flen -= 5; |
| 2615 | } |
| 2616 | |
| 2617 | /* FIXME: lack of END_HEADERS means there's a continuation frame, we |
| 2618 | * don't support this for now and can't even decompress so we have to |
| 2619 | * break the connection. |
| 2620 | */ |
| 2621 | if (!(h2c->dff & H2_F_HEADERS_END_HEADERS)) { |
| 2622 | h2c_error(h2c, H2_ERR_INTERNAL_ERROR); |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 2623 | goto fail; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2624 | } |
| 2625 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 2626 | /* we can't retry a failed decompression operation so we must be very |
| 2627 | * careful not to take any risks. In practice the output buffer is |
| 2628 | * always empty except maybe for trailers, so these operations almost |
| 2629 | * never happen. |
| 2630 | */ |
| 2631 | if (unlikely(buf->o)) { |
| 2632 | /* need to let the output buffer flush and |
| 2633 | * mark the buffer for later wake up. |
| 2634 | */ |
| 2635 | goto fail; |
| 2636 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2637 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 2638 | if (unlikely(buffer_space_wraps(buf))) { |
| 2639 | /* it doesn't fit and the buffer is fragmented, |
| 2640 | * so let's defragment it and try again. |
| 2641 | */ |
| 2642 | buffer_slow_realign(buf); |
| 2643 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2644 | |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 2645 | /* first check if we have some room after p+i */ |
| 2646 | try = buf->data + buf->size - (buf->p + buf->i); |
| 2647 | |
| 2648 | /* otherwise continue between data and p-o */ |
| 2649 | if (try <= 0) { |
| 2650 | try = buf->p - (buf->data + buf->o); |
| 2651 | if (try <= 0) |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 2652 | goto fail; |
Willy Tarreau | 59a10fb | 2017-11-21 20:03:02 +0100 | [diff] [blame] | 2653 | } |
| 2654 | if (try > count) |
| 2655 | try = count; |
| 2656 | |
| 2657 | outlen = hpack_decode_frame(h2c->ddht, hdrs, flen, list, |
| 2658 | sizeof(list)/sizeof(list[0]), tmp); |
| 2659 | if (outlen < 0) { |
| 2660 | h2c_error(h2c, H2_ERR_COMPRESSION_ERROR); |
| 2661 | goto fail; |
| 2662 | } |
| 2663 | |
| 2664 | /* OK now we have our header list in <list> */ |
| 2665 | outlen = h2_make_h1_request(list, bi_end(buf), try); |
| 2666 | |
| 2667 | if (outlen < 0) { |
| 2668 | h2c_error(h2c, H2_ERR_COMPRESSION_ERROR); |
| 2669 | goto fail; |
| 2670 | } |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2671 | |
| 2672 | /* now consume the input data */ |
| 2673 | bi_del(h2c->dbuf, h2c->dfl); |
| 2674 | h2c->st0 = H2_CS_FRAME_H; |
| 2675 | buf->i += outlen; |
| 2676 | |
| 2677 | /* don't send it before returning data! |
| 2678 | * FIXME: should we instead try to send it much later, after the |
| 2679 | * response ? This would require that we keep a copy of it in h2s. |
| 2680 | */ |
| 2681 | if (h2c->dff & H2_F_HEADERS_END_STREAM) { |
| 2682 | h2s->cs->flags |= CS_FL_EOS; |
| 2683 | h2s->flags |= H2_SF_ES_RCVD; |
| 2684 | } |
| 2685 | |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 2686 | leave: |
| 2687 | free_trash_chunk(copy); |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2688 | return outlen; |
Willy Tarreau | 68dd985 | 2017-07-03 14:44:26 +0200 | [diff] [blame] | 2689 | fail: |
| 2690 | outlen = 0; |
| 2691 | goto leave; |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2692 | } |
| 2693 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2694 | /* Transfer the payload of a DATA frame to the HTTP/1 side. When content-length |
| 2695 | * or a tunnel is used, the contents are copied as-is. When chunked encoding is |
| 2696 | * in use, a new chunk is emitted for each frame. This is supposed to fit |
| 2697 | * because the smallest chunk takes 1 byte for the size, 2 for CRLF, X for the |
| 2698 | * data, 2 for the extra CRLF, so that's 5+X, while on the H2 side the smallest |
| 2699 | * frame will be 9+X bytes based on the same buffer size. The HTTP/2 frame |
| 2700 | * parser state is automatically updated. Returns the number of bytes emitted |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2701 | * if > 0, or 0 if it couldn't proceed, in which case CS_FL_RCV_MORE must be |
| 2702 | * checked to know if some data remain pending (an empty DATA frame can return |
| 2703 | * 0 as a valid result). Stream errors are reported in h2s->errcode and |
| 2704 | * connection errors in h2c->errcode. The caller must already have checked the |
| 2705 | * frame header and ensured that the frame was complete or the buffer full. It |
| 2706 | * changes the frame state to FRAME_A once done. |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2707 | */ |
| 2708 | static int h2_frt_transfer_data(struct h2s *h2s, struct buffer *buf, int count) |
| 2709 | { |
| 2710 | struct h2c *h2c = h2s->h2c; |
| 2711 | int block1, block2; |
| 2712 | unsigned int flen = h2c->dfl; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2713 | |
Willy Tarreau | c9ede6c | 2017-12-10 21:28:43 +0100 | [diff] [blame] | 2714 | h2s->cs->flags &= ~CS_FL_RCV_MORE; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2715 | h2c->flags &= ~H2_CF_DEM_SFULL; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2716 | |
| 2717 | /* The padlen is the first byte before data, and the padding appears |
| 2718 | * after data. padlen+data+padding are included in flen. |
| 2719 | */ |
Willy Tarreau | 7912781 | 2017-12-03 21:06:59 +0100 | [diff] [blame] | 2720 | if (h2c->dff & H2_F_DATA_PADDED) { |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2721 | if (h2c->dbuf->i < 1) |
| 2722 | return 0; |
| 2723 | |
Willy Tarreau | 05e5daf | 2017-12-11 15:17:36 +0100 | [diff] [blame] | 2724 | h2c->dpl = *(uint8_t *)bi_ptr(h2c->dbuf); |
| 2725 | if (h2c->dpl >= h2c->dfl) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2726 | /* RFC7540#6.1 : pad length = length of frame payload or greater */ |
| 2727 | h2c_error(h2c, H2_ERR_PROTOCOL_ERROR); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2728 | return 0; |
| 2729 | } |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2730 | |
| 2731 | /* skip the padlen byte */ |
| 2732 | bi_del(h2c->dbuf, 1); |
| 2733 | h2c->dfl--; |
| 2734 | h2c->rcvd_c++; h2c->rcvd_s++; |
| 2735 | h2c->dff &= ~H2_F_DATA_PADDED; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2736 | } |
| 2737 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2738 | flen = h2c->dfl - h2c->dpl; |
| 2739 | if (!flen) |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 2740 | goto end_transfer; |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2741 | |
| 2742 | if (flen > h2c->dbuf->i) { |
| 2743 | flen = h2c->dbuf->i; |
| 2744 | if (!flen) |
| 2745 | return 0; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2746 | } |
| 2747 | |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2748 | /* does it fit in output buffer or should we wait ? */ |
| 2749 | if (flen > count) { |
| 2750 | flen = count; |
| 2751 | if (!flen) { |
| 2752 | h2c->flags |= H2_CF_DEM_SFULL; |
| 2753 | h2s->cs->flags |= CS_FL_RCV_MORE; |
| 2754 | return 0; |
| 2755 | } |
| 2756 | } |
| 2757 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2758 | /* Block1 is the length of the first block before the buffer wraps, |
| 2759 | * block2 is the optional second block to reach the end of the frame. |
| 2760 | */ |
| 2761 | block1 = bi_contig_data(h2c->dbuf); |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2762 | if (block1 > flen) |
| 2763 | block1 = flen; |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2764 | block2 = flen - block1; |
| 2765 | |
| 2766 | if (block1) |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2767 | bi_putblk(buf, b_ptr(h2c->dbuf, 0), block1); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2768 | |
| 2769 | if (block2) |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2770 | bi_putblk(buf, b_ptr(h2c->dbuf, block1), block2); |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2771 | |
| 2772 | /* now mark the input data as consumed (will be deleted from the buffer |
| 2773 | * by the caller when seeing FRAME_A after sending the window update). |
| 2774 | */ |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2775 | bi_del(h2c->dbuf, flen); |
| 2776 | h2c->dfl -= flen; |
| 2777 | h2c->rcvd_c += flen; |
| 2778 | h2c->rcvd_s += flen; // warning, this can also affect the closed streams! |
| 2779 | |
| 2780 | if (h2c->dfl > h2c->dpl) { |
| 2781 | /* more data available, transfer stalled on stream full */ |
| 2782 | h2c->flags |= H2_CF_DEM_SFULL; |
| 2783 | h2s->cs->flags |= CS_FL_RCV_MORE; |
| 2784 | return flen; |
| 2785 | } |
| 2786 | |
Willy Tarreau | 4a28da1 | 2018-01-04 14:41:00 +0100 | [diff] [blame] | 2787 | end_transfer: |
Willy Tarreau | 8fc016d | 2017-12-11 18:27:15 +0100 | [diff] [blame] | 2788 | /* here we're done with the frame, all the payload (except padding) was |
| 2789 | * transferred. |
| 2790 | */ |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2791 | h2c->st0 = H2_CS_FRAME_A; // send the corresponding window update |
| 2792 | |
| 2793 | /* don't send it before returning data! |
| 2794 | * FIXME: should we instead try to send it much later, after the |
| 2795 | * response ? This would require that we keep a copy of it in h2s. |
| 2796 | */ |
Willy Tarreau | 7912781 | 2017-12-03 21:06:59 +0100 | [diff] [blame] | 2797 | if (h2c->dff & H2_F_DATA_END_STREAM) { |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2798 | h2s->cs->flags |= CS_FL_EOS; |
| 2799 | h2s->flags |= H2_SF_ES_RCVD; |
| 2800 | } |
| 2801 | |
| 2802 | return flen; |
| 2803 | } |
| 2804 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2805 | /* |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2806 | * Called from the upper layer to get more data, up to <count> bytes. The |
| 2807 | * caller is responsible for never asking for more data than what is available |
| 2808 | * in the buffer. |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2809 | */ |
| 2810 | static int h2_rcv_buf(struct conn_stream *cs, struct buffer *buf, int count) |
| 2811 | { |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2812 | struct h2s *h2s = cs->ctx; |
| 2813 | struct h2c *h2c = h2s->h2c; |
| 2814 | int ret = 0; |
| 2815 | |
| 2816 | if (h2c->st0 != H2_CS_FRAME_P) |
| 2817 | return 0; // no pre-parsed frame yet |
| 2818 | |
| 2819 | if (h2c->dsi != h2s->id) |
| 2820 | return 0; // not for us |
| 2821 | |
| 2822 | if (!h2c->dbuf->size) |
| 2823 | return 0; // empty buffer |
| 2824 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2825 | switch (h2c->dft) { |
| 2826 | case H2_FT_HEADERS: |
| 2827 | ret = h2_frt_decode_headers(h2s, buf, count); |
| 2828 | break; |
| 2829 | |
Willy Tarreau | 454f905 | 2017-10-26 19:40:35 +0200 | [diff] [blame] | 2830 | case H2_FT_DATA: |
| 2831 | ret = h2_frt_transfer_data(h2s, buf, count); |
| 2832 | break; |
| 2833 | |
Willy Tarreau | 13278b4 | 2017-10-13 19:23:14 +0200 | [diff] [blame] | 2834 | default: |
| 2835 | ret = 0; |
| 2836 | } |
| 2837 | return ret; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 2838 | } |
| 2839 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 2840 | /* Try to send a HEADERS frame matching HTTP/1 response present in buffer <buf> |
| 2841 | * for the H2 stream <h2s>. Returns 0 if not possible yet, <0 on error (one of |
| 2842 | * the H2_ERR* or h2_status codes), >0 on success in which case it corresponds |
| 2843 | * to the number of buffer bytes consumed. |
| 2844 | */ |
| 2845 | static int h2s_frt_make_resp_headers(struct h2s *h2s, struct buffer *buf) |
| 2846 | { |
| 2847 | struct http_hdr list[MAX_HTTP_HDR]; |
| 2848 | struct h2c *h2c = h2s->h2c; |
| 2849 | struct h1m *h1m = &h2s->res; |
| 2850 | struct chunk outbuf; |
| 2851 | int es_now = 0; |
| 2852 | int ret = 0; |
| 2853 | int hdr; |
| 2854 | |
| 2855 | if (h2c_mux_busy(h2c, h2s)) { |
| 2856 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 2857 | return 0; |
| 2858 | } |
| 2859 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 2860 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 2861 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 2862 | h2s->flags |= H2_SF_BLK_MROOM; |
| 2863 | return 0; |
| 2864 | } |
| 2865 | |
| 2866 | /* First, try to parse the H1 response and index it into <list>. |
| 2867 | * NOTE! Since it comes from haproxy, we *know* that a response header |
| 2868 | * block does not wrap and we can safely read it this way without |
| 2869 | * having to realign the buffer. |
| 2870 | */ |
| 2871 | ret = h1_headers_to_hdr_list(bo_ptr(buf), bo_ptr(buf) + buf->o, |
| 2872 | list, sizeof(list)/sizeof(list[0]), h1m); |
| 2873 | if (ret <= 0) { |
Willy Tarreau | f13ef96 | 2017-11-02 15:14:19 +0100 | [diff] [blame] | 2874 | /* incomplete or invalid response, this is abnormal coming from |
| 2875 | * haproxy and may only result in a bad errorfile or bad Lua code |
| 2876 | * so that won't be fixed, raise an error now. |
| 2877 | * |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 2878 | * FIXME: we should instead add the ability to only return a |
| 2879 | * 502 bad gateway. But in theory this is not supposed to |
| 2880 | * happen. |
| 2881 | */ |
| 2882 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 2883 | ret = 0; |
| 2884 | goto end; |
| 2885 | } |
| 2886 | |
| 2887 | chunk_reset(&outbuf); |
| 2888 | |
| 2889 | while (1) { |
| 2890 | outbuf.str = bo_end(h2c->mbuf); |
| 2891 | outbuf.size = bo_contig_space(h2c->mbuf); |
| 2892 | outbuf.len = 0; |
| 2893 | |
| 2894 | if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf)) |
| 2895 | break; |
| 2896 | realign_again: |
| 2897 | buffer_slow_realign(h2c->mbuf); |
| 2898 | } |
| 2899 | |
| 2900 | if (outbuf.size < 9) { |
| 2901 | h2c->flags |= H2_CF_MUX_MFULL; |
| 2902 | h2s->flags |= H2_SF_BLK_MROOM; |
| 2903 | ret = 0; |
| 2904 | goto end; |
| 2905 | } |
| 2906 | |
| 2907 | /* len: 0x000000 (fill later), type: 1(HEADERS), flags: ENDH=4 */ |
| 2908 | memcpy(outbuf.str, "\x00\x00\x00\x01\x04", 5); |
| 2909 | write_n32(outbuf.str + 5, h2s->id); // 4 bytes |
| 2910 | outbuf.len = 9; |
| 2911 | |
| 2912 | /* encode status, which necessarily is the first one */ |
| 2913 | if (outbuf.len < outbuf.size && h1m->status == 200) |
| 2914 | outbuf.str[outbuf.len++] = 0x88; // indexed field : idx[08]=(":status", "200") |
| 2915 | else if (outbuf.len < outbuf.size && h1m->status == 304) |
| 2916 | outbuf.str[outbuf.len++] = 0x8b; // indexed field : idx[11]=(":status", "304") |
Willy Tarreau | a87f202 | 2017-11-09 11:23:00 +0100 | [diff] [blame] | 2917 | else if (unlikely(list[0].v.len != 3)) { |
| 2918 | /* this is an unparsable response */ |
| 2919 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 2920 | ret = 0; |
| 2921 | goto end; |
| 2922 | } |
| 2923 | else if (unlikely(outbuf.len + 2 + 3 <= outbuf.size)) { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 2924 | /* basic encoding of the status code */ |
| 2925 | outbuf.str[outbuf.len++] = 0x48; // indexed name -- name=":status" (idx 8) |
| 2926 | outbuf.str[outbuf.len++] = 0x03; // 3 bytes status |
| 2927 | outbuf.str[outbuf.len++] = list[0].v.ptr[0]; |
| 2928 | outbuf.str[outbuf.len++] = list[0].v.ptr[1]; |
| 2929 | outbuf.str[outbuf.len++] = list[0].v.ptr[2]; |
| 2930 | } |
| 2931 | else { |
| 2932 | if (buffer_space_wraps(h2c->mbuf)) |
| 2933 | goto realign_again; |
| 2934 | |
| 2935 | h2c->flags |= H2_CF_MUX_MFULL; |
| 2936 | h2s->flags |= H2_SF_BLK_MROOM; |
| 2937 | ret = 0; |
| 2938 | goto end; |
| 2939 | } |
| 2940 | |
| 2941 | /* encode all headers, stop at empty name */ |
| 2942 | for (hdr = 1; hdr < sizeof(list)/sizeof(list[0]); hdr++) { |
Willy Tarreau | a76e4c2 | 2017-11-24 08:17:28 +0100 | [diff] [blame] | 2943 | /* these ones do not exist in H2 and must be dropped. */ |
| 2944 | if (isteq(list[hdr].n, ist("connection")) || |
| 2945 | isteq(list[hdr].n, ist("proxy-connection")) || |
| 2946 | isteq(list[hdr].n, ist("keep-alive")) || |
| 2947 | isteq(list[hdr].n, ist("upgrade")) || |
| 2948 | isteq(list[hdr].n, ist("transfer-encoding"))) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 2949 | continue; |
| 2950 | |
| 2951 | if (isteq(list[hdr].n, ist(""))) |
| 2952 | break; // end |
| 2953 | |
| 2954 | if (!hpack_encode_header(&outbuf, list[hdr].n, list[hdr].v)) { |
| 2955 | /* output full */ |
| 2956 | if (buffer_space_wraps(h2c->mbuf)) |
| 2957 | goto realign_again; |
| 2958 | |
| 2959 | h2c->flags |= H2_CF_MUX_MFULL; |
| 2960 | h2s->flags |= H2_SF_BLK_MROOM; |
| 2961 | ret = 0; |
| 2962 | goto end; |
| 2963 | } |
| 2964 | } |
| 2965 | |
| 2966 | /* we may need to add END_STREAM */ |
| 2967 | if (((h1m->flags & H1_MF_CLEN) && !h1m->body_len) || h2s->cs->flags & CS_FL_SHW) |
| 2968 | es_now = 1; |
| 2969 | |
| 2970 | /* update the frame's size */ |
| 2971 | h2_set_frame_size(outbuf.str, outbuf.len - 9); |
| 2972 | |
| 2973 | if (es_now) |
| 2974 | outbuf.str[4] |= H2_F_HEADERS_END_STREAM; |
| 2975 | |
| 2976 | /* consume incoming H1 response */ |
| 2977 | bo_del(buf, ret); |
| 2978 | |
| 2979 | /* commit the H2 response */ |
| 2980 | h2c->mbuf->o += outbuf.len; |
| 2981 | h2c->mbuf->p = b_ptr(h2c->mbuf, outbuf.len); |
Willy Tarreau | 6743420 | 2017-11-06 20:20:51 +0100 | [diff] [blame] | 2982 | h2s->flags |= H2_SF_HEADERS_SENT; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 2983 | |
| 2984 | /* for now we don't implemented CONTINUATION, so we wait for a |
| 2985 | * body or directly end in TRL2. |
| 2986 | */ |
| 2987 | if (es_now) { |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 2988 | // trim any possibly pending data (eg: inconsistent content-length) |
| 2989 | bo_del(buf, buf->o); |
| 2990 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 2991 | h1m->state = HTTP_MSG_DONE; |
| 2992 | h2s->flags |= H2_SF_ES_SENT; |
| 2993 | if (h2s->st == H2_SS_OPEN) |
| 2994 | h2s->st = H2_SS_HLOC; |
| 2995 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 2996 | h2s_close(h2s); |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 2997 | } |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 2998 | else if (h1m->status >= 100 && h1m->status < 200) { |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 2999 | /* we'll let the caller check if it has more headers to send */ |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 3000 | h1m->state = HTTP_MSG_RPBEFORE; |
| 3001 | h1m->status = 0; |
| 3002 | h1m->flags = 0; |
Willy Tarreau | 8728559 | 2017-11-29 15:41:32 +0100 | [diff] [blame] | 3003 | goto end; |
Willy Tarreau | c199faf | 2017-10-31 08:35:27 +0100 | [diff] [blame] | 3004 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3005 | else |
Willy Tarreau | 13e4e94 | 2017-12-14 10:55:21 +0100 | [diff] [blame] | 3006 | h1m->state = (h1m->flags & H1_MF_CHNK) ? HTTP_MSG_CHUNK_SIZE : HTTP_MSG_BODY; |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3007 | |
| 3008 | end: |
| 3009 | //fprintf(stderr, "[%d] sent simple H2 response (sid=%d) = %d bytes (%d in, ep=%u, es=%s)\n", h2c->st0, h2s->id, outbuf.len, ret, h1m->err_pos, h1_msg_state_str(h1m->err_state)); |
| 3010 | return ret; |
| 3011 | } |
| 3012 | |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3013 | /* Try to send a DATA frame matching HTTP/1 response present in the response |
| 3014 | * buffer <buf>, for stream <h2s>. Returns 0 if not possible yet, <0 on error |
| 3015 | * (one of the H2_ERR* or h2_status codes), >0 on success in which case it |
| 3016 | * corresponds to the number of buffer bytes consumed. |
| 3017 | */ |
| 3018 | static int h2s_frt_make_resp_data(struct h2s *h2s, struct buffer *buf) |
| 3019 | { |
| 3020 | struct h2c *h2c = h2s->h2c; |
| 3021 | struct h1m *h1m = &h2s->res; |
| 3022 | struct chunk outbuf; |
| 3023 | int ret = 0; |
| 3024 | int total = 0; |
| 3025 | int es_now = 0; |
| 3026 | int size = 0; |
| 3027 | char *blk1, *blk2; |
| 3028 | int len1, len2; |
| 3029 | |
| 3030 | if (h2c_mux_busy(h2c, h2s)) { |
| 3031 | h2s->flags |= H2_SF_BLK_MBUSY; |
| 3032 | goto end; |
| 3033 | } |
| 3034 | |
Willy Tarreau | 44e973f | 2018-03-01 17:49:30 +0100 | [diff] [blame] | 3035 | if (!h2_get_buf(h2c, &h2c->mbuf)) { |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3036 | h2c->flags |= H2_CF_MUX_MALLOC; |
| 3037 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3038 | goto end; |
| 3039 | } |
| 3040 | |
| 3041 | new_frame: |
| 3042 | if (!buf->o) |
| 3043 | goto end; |
| 3044 | |
| 3045 | chunk_reset(&outbuf); |
| 3046 | |
| 3047 | while (1) { |
| 3048 | outbuf.str = bo_end(h2c->mbuf); |
| 3049 | outbuf.size = bo_contig_space(h2c->mbuf); |
| 3050 | outbuf.len = 0; |
| 3051 | |
| 3052 | if (outbuf.size >= 9 || !buffer_space_wraps(h2c->mbuf)) |
| 3053 | break; |
| 3054 | realign_again: |
| 3055 | buffer_slow_realign(h2c->mbuf); |
| 3056 | } |
| 3057 | |
| 3058 | if (outbuf.size < 9) { |
| 3059 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3060 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3061 | goto end; |
| 3062 | } |
| 3063 | |
| 3064 | /* len: 0x000000 (fill later), type: 0(DATA), flags: none=0 */ |
| 3065 | memcpy(outbuf.str, "\x00\x00\x00\x00\x00", 5); |
| 3066 | write_n32(outbuf.str + 5, h2s->id); // 4 bytes |
| 3067 | outbuf.len = 9; |
| 3068 | |
| 3069 | switch (h1m->flags & (H1_MF_CLEN|H1_MF_CHNK)) { |
| 3070 | case 0: /* no content length, read till SHUTW */ |
| 3071 | size = buf->o; |
Willy Tarreau | 13e4e94 | 2017-12-14 10:55:21 +0100 | [diff] [blame] | 3072 | h1m->curr_len = size; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3073 | break; |
| 3074 | case H1_MF_CLEN: /* content-length: read only h2m->body_len */ |
| 3075 | size = buf->o; |
| 3076 | if ((long long)size > h1m->curr_len) |
| 3077 | size = h1m->curr_len; |
| 3078 | break; |
| 3079 | default: /* te:chunked : parse chunks */ |
| 3080 | if (h1m->state == HTTP_MSG_CHUNK_CRLF) { |
| 3081 | ret = h1_skip_chunk_crlf(buf, -buf->o, 0); |
| 3082 | if (!ret) |
| 3083 | goto end; |
| 3084 | |
| 3085 | if (ret < 0) { |
| 3086 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
| 3087 | h1m->err_pos = ret; |
| 3088 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3089 | goto end; |
| 3090 | } |
| 3091 | bo_del(buf, ret); |
| 3092 | total += ret; |
| 3093 | h1m->state = HTTP_MSG_CHUNK_SIZE; |
| 3094 | } |
| 3095 | |
| 3096 | if (h1m->state == HTTP_MSG_CHUNK_SIZE) { |
| 3097 | unsigned int chunk; |
| 3098 | |
| 3099 | ret = h1_parse_chunk_size(buf, -buf->o, 0, &chunk); |
| 3100 | if (!ret) |
| 3101 | goto end; |
| 3102 | |
| 3103 | if (ret < 0) { |
| 3104 | /* FIXME: bad contents. how to proceed here when we're in H2 ? */ |
| 3105 | h1m->err_pos = ret; |
| 3106 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3107 | goto end; |
| 3108 | } |
| 3109 | |
| 3110 | size = chunk; |
| 3111 | h1m->curr_len = chunk; |
| 3112 | h1m->body_len += chunk; |
| 3113 | bo_del(buf, ret); |
| 3114 | total += ret; |
| 3115 | h1m->state = size ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
| 3116 | if (!size) |
| 3117 | goto send_empty; |
| 3118 | } |
| 3119 | |
| 3120 | /* in MSG_DATA state, continue below */ |
| 3121 | size = h1m->curr_len; |
| 3122 | break; |
| 3123 | } |
| 3124 | |
| 3125 | /* we have in <size> the exact number of bytes we need to copy from |
| 3126 | * the H1 buffer. We need to check this against the connection's and |
| 3127 | * the stream's send windows, and to ensure that this fits in the max |
| 3128 | * frame size and in the buffer's available space minus 9 bytes (for |
| 3129 | * the frame header). The connection's flow control is applied last so |
| 3130 | * that we can use a separate list of streams which are immediately |
| 3131 | * unblocked on window opening. Note: we don't implement padding. |
| 3132 | */ |
| 3133 | |
| 3134 | if (size > buf->o) |
| 3135 | size = buf->o; |
| 3136 | |
| 3137 | if (size > h2s->mws) |
| 3138 | size = h2s->mws; |
| 3139 | |
| 3140 | if (size <= 0) { |
| 3141 | h2s->flags |= H2_SF_BLK_SFCTL; |
| 3142 | goto end; |
| 3143 | } |
| 3144 | |
| 3145 | if (h2c->mfs && size > h2c->mfs) |
| 3146 | size = h2c->mfs; |
| 3147 | |
| 3148 | if (size + 9 > outbuf.size) { |
| 3149 | /* we have an opportunity for enlarging the too small |
| 3150 | * available space, let's try. |
| 3151 | */ |
| 3152 | if (buffer_space_wraps(h2c->mbuf)) |
| 3153 | goto realign_again; |
| 3154 | size = outbuf.size - 9; |
| 3155 | } |
| 3156 | |
| 3157 | if (size <= 0) { |
| 3158 | h2c->flags |= H2_CF_MUX_MFULL; |
| 3159 | h2s->flags |= H2_SF_BLK_MROOM; |
| 3160 | goto end; |
| 3161 | } |
| 3162 | |
| 3163 | if (size > h2c->mws) |
| 3164 | size = h2c->mws; |
| 3165 | |
| 3166 | if (size <= 0) { |
| 3167 | h2s->flags |= H2_SF_BLK_MFCTL; |
| 3168 | goto end; |
| 3169 | } |
| 3170 | |
| 3171 | /* copy whatever we can */ |
| 3172 | blk1 = blk2 = NULL; // silence a maybe-uninitialized warning |
| 3173 | ret = bo_getblk_nc(buf, &blk1, &len1, &blk2, &len2); |
| 3174 | if (ret == 1) |
| 3175 | len2 = 0; |
| 3176 | |
| 3177 | if (!ret || len1 + len2 < size) { |
| 3178 | /* FIXME: must normally never happen */ |
| 3179 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3180 | goto end; |
| 3181 | } |
| 3182 | |
| 3183 | /* limit len1/len2 to size */ |
| 3184 | if (len1 + len2 > size) { |
| 3185 | int sub = len1 + len2 - size; |
| 3186 | |
| 3187 | if (len2 > sub) |
| 3188 | len2 -= sub; |
| 3189 | else { |
| 3190 | sub -= len2; |
| 3191 | len2 = 0; |
| 3192 | len1 -= sub; |
| 3193 | } |
| 3194 | } |
| 3195 | |
| 3196 | /* now let's copy this this into the output buffer */ |
| 3197 | memcpy(outbuf.str + 9, blk1, len1); |
| 3198 | if (len2) |
| 3199 | memcpy(outbuf.str + 9 + len1, blk2, len2); |
| 3200 | |
| 3201 | send_empty: |
| 3202 | /* we may need to add END_STREAM */ |
| 3203 | /* FIXME: we should also detect shutdown(w) below, but how ? Maybe we |
| 3204 | * could rely on the MSG_MORE flag as a hint for this ? |
| 3205 | */ |
| 3206 | if (((h1m->flags & H1_MF_CLEN) && !(h1m->curr_len - size)) || |
| 3207 | !h1m->curr_len || h1m->state >= HTTP_MSG_DONE) |
| 3208 | es_now = 1; |
| 3209 | |
| 3210 | /* update the frame's size */ |
| 3211 | h2_set_frame_size(outbuf.str, size); |
| 3212 | |
| 3213 | if (es_now) |
| 3214 | outbuf.str[4] |= H2_F_DATA_END_STREAM; |
| 3215 | |
| 3216 | /* commit the H2 response */ |
| 3217 | h2c->mbuf->o += size + 9; |
| 3218 | h2c->mbuf->p = b_ptr(h2c->mbuf, size + 9); |
| 3219 | |
| 3220 | /* consume incoming H1 response */ |
| 3221 | if (size > 0) { |
| 3222 | bo_del(buf, size); |
| 3223 | total += size; |
| 3224 | h1m->curr_len -= size; |
| 3225 | h2s->mws -= size; |
| 3226 | h2c->mws -= size; |
| 3227 | |
| 3228 | if (size && !h1m->curr_len && (h1m->flags & H1_MF_CHNK)) { |
| 3229 | h1m->state = HTTP_MSG_CHUNK_CRLF; |
| 3230 | goto new_frame; |
| 3231 | } |
| 3232 | } |
| 3233 | |
| 3234 | if (es_now) { |
| 3235 | if (h2s->st == H2_SS_OPEN) |
| 3236 | h2s->st = H2_SS_HLOC; |
| 3237 | else |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3238 | h2s_close(h2s); |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 3239 | |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3240 | if (!(h1m->flags & H1_MF_CHNK)) { |
| 3241 | // trim any possibly pending data (eg: inconsistent content-length) |
| 3242 | bo_del(buf, buf->o); |
| 3243 | |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 3244 | h1m->state = HTTP_MSG_DONE; |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3245 | } |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 3246 | |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3247 | h2s->flags |= H2_SF_ES_SENT; |
| 3248 | } |
| 3249 | |
| 3250 | end: |
| 3251 | trace("[%d] sent simple H2 DATA response (sid=%d) = %d bytes out (%d in, st=%s, ep=%u, es=%s, h2cws=%d h2sws=%d) buf->o=%d", h2c->st0, h2s->id, size+9, total, h1_msg_state_str(h1m->state), h1m->err_pos, h1_msg_state_str(h1m->err_state), h2c->mws, h2s->mws, buf->o); |
| 3252 | return total; |
| 3253 | } |
| 3254 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3255 | /* Called from the upper layer, to send data */ |
| 3256 | static int h2_snd_buf(struct conn_stream *cs, struct buffer *buf, int flags) |
| 3257 | { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3258 | struct h2s *h2s = cs->ctx; |
| 3259 | int total = 0; |
| 3260 | |
Willy Tarreau | c4312d3 | 2017-11-07 12:01:53 +0100 | [diff] [blame] | 3261 | if (!(h2s->flags & H2_SF_OUTGOING_DATA) && buf->o) |
| 3262 | h2s->flags |= H2_SF_OUTGOING_DATA; |
| 3263 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3264 | while (h2s->res.state < HTTP_MSG_DONE && buf->o) { |
| 3265 | if (h2s->res.state < HTTP_MSG_BODY) { |
| 3266 | total += h2s_frt_make_resp_headers(h2s, buf); |
| 3267 | |
Willy Tarreau | 9470d2c | 2017-12-03 10:42:59 +0100 | [diff] [blame] | 3268 | if (h2s->st >= H2_SS_ERROR) |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3269 | break; |
| 3270 | |
| 3271 | if (h2s->flags & H2_SF_BLK_ANY) |
| 3272 | break; |
| 3273 | } |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3274 | else if (h2s->res.state < HTTP_MSG_TRAILERS) { |
| 3275 | total += h2s_frt_make_resp_data(h2s, buf); |
| 3276 | |
Willy Tarreau | 9470d2c | 2017-12-03 10:42:59 +0100 | [diff] [blame] | 3277 | if (h2s->st >= H2_SS_ERROR) |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3278 | break; |
| 3279 | |
| 3280 | if (h2s->flags & H2_SF_BLK_ANY) |
| 3281 | break; |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 3282 | } |
| 3283 | else if (h2s->res.state == HTTP_MSG_TRAILERS) { |
| 3284 | /* consume the trailers if any (we don't forward them for now) */ |
| 3285 | int count = h1_measure_trailers(buf); |
| 3286 | |
| 3287 | if (unlikely(count <= 0)) { |
| 3288 | if (count < 0) |
| 3289 | h2s_error(h2s, H2_ERR_INTERNAL_ERROR); |
| 3290 | break; |
| 3291 | } |
| 3292 | total += count; |
| 3293 | bo_del(buf, count); |
Willy Tarreau | 35a6270 | 2018-02-27 15:37:25 +0100 | [diff] [blame] | 3294 | |
| 3295 | // trim any possibly pending data (eg: extra CR-LF, ...) |
| 3296 | bo_del(buf, buf->o); |
| 3297 | |
Willy Tarreau | 9d89ac8 | 2017-10-31 17:15:59 +0100 | [diff] [blame] | 3298 | h2s->res.state = HTTP_MSG_DONE; |
| 3299 | break; |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3300 | } |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3301 | else { |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3302 | cs->flags |= CS_FL_ERROR; |
| 3303 | break; |
| 3304 | } |
| 3305 | } |
| 3306 | |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 3307 | /* RST are sent similarly to frame acks */ |
Willy Tarreau | 0249219 | 2017-12-07 15:59:29 +0100 | [diff] [blame] | 3308 | if (h2s->st == H2_SS_ERROR || h2s->flags & H2_SF_RST_RCVD) { |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 3309 | cs->flags |= CS_FL_ERROR; |
Willy Tarreau | 8c0ea7d | 2017-11-10 10:05:24 +0100 | [diff] [blame] | 3310 | if (h2s_send_rst_stream(h2s->h2c, h2s) > 0) |
Willy Tarreau | 00dd078 | 2018-03-01 16:31:34 +0100 | [diff] [blame] | 3311 | h2s_close(h2s); |
Willy Tarreau | c6795ca | 2017-11-07 09:43:06 +0100 | [diff] [blame] | 3312 | } |
| 3313 | |
Willy Tarreau | c652dbd | 2017-10-19 11:16:37 +0200 | [diff] [blame] | 3314 | if (h2s->flags & H2_SF_BLK_SFCTL) { |
| 3315 | /* stream flow control, quit the list */ |
| 3316 | LIST_DEL(&h2s->list); |
| 3317 | LIST_INIT(&h2s->list); |
| 3318 | } |
| 3319 | |
Willy Tarreau | 9e5ae1d | 2017-10-17 19:58:20 +0200 | [diff] [blame] | 3320 | return total; |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3321 | } |
| 3322 | |
| 3323 | |
| 3324 | /*******************************************************/ |
| 3325 | /* functions below are dedicated to the config parsers */ |
| 3326 | /*******************************************************/ |
| 3327 | |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 3328 | /* config parser for global "tune.h2.header-table-size" */ |
| 3329 | static int h2_parse_header_table_size(char **args, int section_type, struct proxy *curpx, |
| 3330 | struct proxy *defpx, const char *file, int line, |
| 3331 | char **err) |
| 3332 | { |
| 3333 | if (too_many_args(1, args, err, NULL)) |
| 3334 | return -1; |
| 3335 | |
| 3336 | h2_settings_header_table_size = atoi(args[1]); |
| 3337 | if (h2_settings_header_table_size < 4096 || h2_settings_header_table_size > 65536) { |
| 3338 | memprintf(err, "'%s' expects a numeric value between 4096 and 65536.", args[0]); |
| 3339 | return -1; |
| 3340 | } |
| 3341 | return 0; |
| 3342 | } |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3343 | |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 3344 | /* config parser for global "tune.h2.initial-window-size" */ |
| 3345 | static int h2_parse_initial_window_size(char **args, int section_type, struct proxy *curpx, |
| 3346 | struct proxy *defpx, const char *file, int line, |
| 3347 | char **err) |
| 3348 | { |
| 3349 | if (too_many_args(1, args, err, NULL)) |
| 3350 | return -1; |
| 3351 | |
| 3352 | h2_settings_initial_window_size = atoi(args[1]); |
| 3353 | if (h2_settings_initial_window_size < 0) { |
| 3354 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 3355 | return -1; |
| 3356 | } |
| 3357 | return 0; |
| 3358 | } |
| 3359 | |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 3360 | /* config parser for global "tune.h2.max-concurrent-streams" */ |
| 3361 | static int h2_parse_max_concurrent_streams(char **args, int section_type, struct proxy *curpx, |
| 3362 | struct proxy *defpx, const char *file, int line, |
| 3363 | char **err) |
| 3364 | { |
| 3365 | if (too_many_args(1, args, err, NULL)) |
| 3366 | return -1; |
| 3367 | |
| 3368 | h2_settings_max_concurrent_streams = atoi(args[1]); |
| 3369 | if (h2_settings_max_concurrent_streams < 0) { |
| 3370 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 3371 | return -1; |
| 3372 | } |
| 3373 | return 0; |
| 3374 | } |
| 3375 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3376 | |
| 3377 | /****************************************/ |
| 3378 | /* MUX initialization and instanciation */ |
| 3379 | /***************************************/ |
| 3380 | |
| 3381 | /* The mux operations */ |
| 3382 | const struct mux_ops h2_ops = { |
| 3383 | .init = h2_init, |
| 3384 | .recv = h2_recv, |
| 3385 | .send = h2_send, |
| 3386 | .wake = h2_wake, |
| 3387 | .update_poll = h2_update_poll, |
| 3388 | .rcv_buf = h2_rcv_buf, |
| 3389 | .snd_buf = h2_snd_buf, |
| 3390 | .attach = h2_attach, |
| 3391 | .detach = h2_detach, |
| 3392 | .shutr = h2_shutr, |
| 3393 | .shutw = h2_shutw, |
Willy Tarreau | 28f1cb9 | 2017-12-20 16:14:44 +0100 | [diff] [blame] | 3394 | .flags = MX_FL_CLEAN_ABRT, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3395 | .name = "H2", |
| 3396 | }; |
| 3397 | |
| 3398 | /* ALPN selection : this mux registers ALPN tolen "h2" */ |
| 3399 | static struct alpn_mux_list alpn_mux_h2 = |
| 3400 | { .token = IST("h2"), .mode = ALPN_MODE_HTTP, .mux = &h2_ops }; |
| 3401 | |
| 3402 | /* config keyword parsers */ |
| 3403 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | fe20e5b | 2017-07-27 11:42:14 +0200 | [diff] [blame] | 3404 | { CFG_GLOBAL, "tune.h2.header-table-size", h2_parse_header_table_size }, |
Willy Tarreau | e6baec0 | 2017-07-27 11:45:11 +0200 | [diff] [blame] | 3405 | { CFG_GLOBAL, "tune.h2.initial-window-size", h2_parse_initial_window_size }, |
Willy Tarreau | 5242ef8 | 2017-07-27 11:47:28 +0200 | [diff] [blame] | 3406 | { CFG_GLOBAL, "tune.h2.max-concurrent-streams", h2_parse_max_concurrent_streams }, |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3407 | { 0, NULL, NULL } |
| 3408 | }}; |
| 3409 | |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 3410 | static void __h2_deinit(void) |
| 3411 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3412 | pool_destroy(pool_head_h2s); |
| 3413 | pool_destroy(pool_head_h2c); |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 3414 | } |
| 3415 | |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3416 | __attribute__((constructor)) |
| 3417 | static void __h2_init(void) |
| 3418 | { |
| 3419 | alpn_register_mux(&alpn_mux_h2); |
| 3420 | cfg_register_keywords(&cfg_kws); |
Willy Tarreau | 5ab6b57 | 2017-09-22 08:05:00 +0200 | [diff] [blame] | 3421 | hap_register_post_deinit(__h2_deinit); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3422 | pool_head_h2c = create_pool("h2c", sizeof(struct h2c), MEM_F_SHARED); |
| 3423 | pool_head_h2s = create_pool("h2s", sizeof(struct h2s), MEM_F_SHARED); |
Willy Tarreau | 62f5269 | 2017-10-08 23:01:42 +0200 | [diff] [blame] | 3424 | } |