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