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