Willy Tarreau | ffca736 | 2016-12-13 18:25:15 +0100 | [diff] [blame] | 1 | /* |
| 2 | * include/common/h2.h |
| 3 | * This file contains types and macros used for the HTTP/2 protocol |
| 4 | * |
| 5 | * Copyright (C) 2000-2017 Willy Tarreau - w@1wt.eu |
| 6 | * Copyright (C) 2017 HAProxy Technologies |
| 7 | * |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 8 | * Permission is hereby granted, free of charge, to any person obtaining |
| 9 | * a copy of this software and associated documentation files (the |
| 10 | * "Software"), to deal in the Software without restriction, including |
| 11 | * without limitation the rights to use, copy, modify, merge, publish, |
| 12 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 13 | * permit persons to whom the Software is furnished to do so, subject to |
| 14 | * the following conditions: |
Willy Tarreau | ffca736 | 2016-12-13 18:25:15 +0100 | [diff] [blame] | 15 | * |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 16 | * The above copyright notice and this permission notice shall be |
| 17 | * included in all copies or substantial portions of the Software. |
Willy Tarreau | ffca736 | 2016-12-13 18:25:15 +0100 | [diff] [blame] | 18 | * |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 21 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 22 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 23 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 24 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 25 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 26 | * OTHER DEALINGS IN THE SOFTWARE. |
Willy Tarreau | ffca736 | 2016-12-13 18:25:15 +0100 | [diff] [blame] | 27 | */ |
| 28 | |
| 29 | #ifndef _COMMON_H2_H |
| 30 | #define _COMMON_H2_H |
| 31 | |
| 32 | #include <common/config.h> |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 33 | #include <common/http-hdr.h> |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 34 | #include <common/htx.h> |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 35 | #include <common/ist.h> |
Willy Tarreau | ffca736 | 2016-12-13 18:25:15 +0100 | [diff] [blame] | 36 | |
| 37 | |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 38 | /* indexes of most important pseudo headers can be simplified to an almost |
| 39 | * linear array by dividing the index by 2 for all values from 1 to 9, and |
| 40 | * caping to 4 for values up to 14 ; thus it fits in a single 24-bit array |
| 41 | * shifted by 3 times the index value/2, or a 32-bit array shifted by 4x. |
| 42 | * Don't change these values, they are assumed by hpack_idx_to_phdr(). There |
| 43 | * is an entry for the Host header field which is not a pseudo-header but |
| 44 | * needs to be tracked as we should only use :authority if it's absent. |
| 45 | */ |
| 46 | enum { |
| 47 | H2_PHDR_IDX_NONE = 0, |
| 48 | H2_PHDR_IDX_AUTH = 1, /* :authority = 1 */ |
| 49 | H2_PHDR_IDX_METH = 2, /* :method = 2..3 */ |
| 50 | H2_PHDR_IDX_PATH = 3, /* :path = 4..5 */ |
| 51 | H2_PHDR_IDX_SCHM = 4, /* :scheme = 6..7 */ |
| 52 | H2_PHDR_IDX_STAT = 5, /* :status = 8..14 */ |
| 53 | H2_PHDR_IDX_HOST = 6, /* Host, never returned, just a place-holder */ |
| 54 | H2_PHDR_NUM_ENTRIES /* must be last */ |
| 55 | }; |
| 56 | |
| 57 | /* bit fields indicating the pseudo-headers found. It also covers the HOST |
| 58 | * header field as well as any non-pseudo-header field (NONE). |
| 59 | */ |
| 60 | enum { |
| 61 | H2_PHDR_FND_NONE = 1 << H2_PHDR_IDX_NONE, /* found a regular header */ |
| 62 | H2_PHDR_FND_AUTH = 1 << H2_PHDR_IDX_AUTH, |
| 63 | H2_PHDR_FND_METH = 1 << H2_PHDR_IDX_METH, |
| 64 | H2_PHDR_FND_PATH = 1 << H2_PHDR_IDX_PATH, |
| 65 | H2_PHDR_FND_SCHM = 1 << H2_PHDR_IDX_SCHM, |
| 66 | H2_PHDR_FND_STAT = 1 << H2_PHDR_IDX_STAT, |
| 67 | H2_PHDR_FND_HOST = 1 << H2_PHDR_IDX_HOST, |
| 68 | }; |
| 69 | |
Willy Tarreau | ffca736 | 2016-12-13 18:25:15 +0100 | [diff] [blame] | 70 | /* frame types, from the standard */ |
| 71 | enum h2_ft { |
| 72 | H2_FT_DATA = 0x00, // RFC7540 #6.1 |
| 73 | H2_FT_HEADERS = 0x01, // RFC7540 #6.2 |
| 74 | H2_FT_PRIORITY = 0x02, // RFC7540 #6.3 |
| 75 | H2_FT_RST_STREAM = 0x03, // RFC7540 #6.4 |
| 76 | H2_FT_SETTINGS = 0x04, // RFC7540 #6.5 |
| 77 | H2_FT_PUSH_PROMISE = 0x05, // RFC7540 #6.6 |
| 78 | H2_FT_PING = 0x06, // RFC7540 #6.7 |
| 79 | H2_FT_GOAWAY = 0x07, // RFC7540 #6.8 |
| 80 | H2_FT_WINDOW_UPDATE = 0x08, // RFC7540 #6.9 |
| 81 | H2_FT_CONTINUATION = 0x09, // RFC7540 #6.10 |
| 82 | H2_FT_ENTRIES /* must be last */ |
| 83 | } __attribute__((packed)); |
| 84 | |
Willy Tarreau | deab244 | 2018-12-21 14:56:57 +0100 | [diff] [blame] | 85 | /* frame types, turned to bits or bit fields */ |
| 86 | enum { |
| 87 | /* one bit per frame type */ |
| 88 | H2_FT_DATA_BIT = 1U << H2_FT_DATA, |
| 89 | H2_FT_HEADERS_BIT = 1U << H2_FT_HEADERS, |
| 90 | H2_FT_PRIORITY_BIT = 1U << H2_FT_PRIORITY, |
| 91 | H2_FT_RST_STREAM_BIT = 1U << H2_FT_RST_STREAM, |
| 92 | H2_FT_SETTINGS_BIT = 1U << H2_FT_SETTINGS, |
| 93 | H2_FT_PUSH_PROMISE_BIT = 1U << H2_FT_PUSH_PROMISE, |
| 94 | H2_FT_PING_BIT = 1U << H2_FT_PING, |
| 95 | H2_FT_GOAWAY_BIT = 1U << H2_FT_GOAWAY, |
| 96 | H2_FT_WINDOW_UPDATE_BIT = 1U << H2_FT_WINDOW_UPDATE, |
| 97 | H2_FT_CONTINUATION_BIT = 1U << H2_FT_CONTINUATION, |
| 98 | /* padded frames */ |
Willy Tarreau | 71c3811 | 2019-01-24 09:31:40 +0100 | [diff] [blame] | 99 | H2_FT_PADDED_MASK = H2_FT_DATA_BIT | H2_FT_HEADERS_BIT | H2_FT_PUSH_PROMISE_BIT, |
Willy Tarreau | deab244 | 2018-12-21 14:56:57 +0100 | [diff] [blame] | 100 | /* flow controlled frames */ |
Willy Tarreau | 71c3811 | 2019-01-24 09:31:40 +0100 | [diff] [blame] | 101 | H2_FT_FC_MASK = H2_FT_DATA_BIT, |
| 102 | /* header frames */ |
| 103 | H2_FT_HDR_MASK = H2_FT_HEADERS_BIT | H2_FT_PUSH_PROMISE_BIT | H2_FT_CONTINUATION_BIT, |
| 104 | /* frames allowed to arrive late on a stream */ |
| 105 | H2_FT_LATE_MASK = H2_FT_WINDOW_UPDATE_BIT | H2_FT_RST_STREAM_BIT | H2_FT_PRIORITY_BIT, |
Willy Tarreau | deab244 | 2018-12-21 14:56:57 +0100 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | |
Willy Tarreau | ffca736 | 2016-12-13 18:25:15 +0100 | [diff] [blame] | 109 | /* flags defined for each frame type */ |
| 110 | |
| 111 | // RFC7540 #6.1 |
| 112 | #define H2_F_DATA_END_STREAM 0x01 |
| 113 | #define H2_F_DATA_PADDED 0x08 |
| 114 | |
| 115 | // RFC7540 #6.2 |
| 116 | #define H2_F_HEADERS_END_STREAM 0x01 |
| 117 | #define H2_F_HEADERS_END_HEADERS 0x04 |
| 118 | #define H2_F_HEADERS_PADDED 0x08 |
| 119 | #define H2_F_HEADERS_PRIORITY 0x20 |
| 120 | |
| 121 | // RFC7540 #6.3 : PRIORITY defines no flags |
| 122 | // RFC7540 #6.4 : RST_STREAM defines no flags |
| 123 | |
| 124 | // RFC7540 #6.5 |
| 125 | #define H2_F_SETTINGS_ACK 0x01 |
| 126 | |
| 127 | // RFC7540 #6.6 |
| 128 | #define H2_F_PUSH_PROMISE_END_HEADERS 0x04 |
| 129 | #define H2_F_PUSH_PROMISE_PADDED 0x08 |
| 130 | |
| 131 | // RFC7540 #6.7 |
| 132 | #define H2_F_PING_ACK 0x01 |
| 133 | |
| 134 | // RFC7540 #6.8 : GOAWAY defines no flags |
| 135 | // RFC7540 #6.9 : WINDOW_UPDATE defines no flags |
| 136 | |
Willy Tarreau | deab244 | 2018-12-21 14:56:57 +0100 | [diff] [blame] | 137 | // PADDED is the exact same among DATA, HEADERS and PUSH_PROMISE (8) |
| 138 | #define H2_F_PADDED 0x08 |
| 139 | |
Willy Tarreau | ffca736 | 2016-12-13 18:25:15 +0100 | [diff] [blame] | 140 | /* HTTP/2 error codes - RFC7540 #7 */ |
| 141 | enum h2_err { |
| 142 | H2_ERR_NO_ERROR = 0x0, |
| 143 | H2_ERR_PROTOCOL_ERROR = 0x1, |
| 144 | H2_ERR_INTERNAL_ERROR = 0x2, |
| 145 | H2_ERR_FLOW_CONTROL_ERROR = 0x3, |
| 146 | H2_ERR_SETTINGS_TIMEOUT = 0x4, |
| 147 | H2_ERR_STREAM_CLOSED = 0x5, |
| 148 | H2_ERR_FRAME_SIZE_ERROR = 0x6, |
| 149 | H2_ERR_REFUSED_STREAM = 0x7, |
| 150 | H2_ERR_CANCEL = 0x8, |
| 151 | H2_ERR_COMPRESSION_ERROR = 0x9, |
| 152 | H2_ERR_CONNECT_ERROR = 0xa, |
| 153 | H2_ERR_ENHANCE_YOUR_CALM = 0xb, |
| 154 | H2_ERR_INADEQUATE_SECURITY = 0xc, |
| 155 | H2_ERR_HTTP_1_1_REQUIRED = 0xd, |
| 156 | } __attribute__((packed)); |
| 157 | |
| 158 | // RFC7540 #11.3 : Settings Registry |
| 159 | #define H2_SETTINGS_HEADER_TABLE_SIZE 0x0001 |
| 160 | #define H2_SETTINGS_ENABLE_PUSH 0x0002 |
| 161 | #define H2_SETTINGS_MAX_CONCURRENT_STREAMS 0x0003 |
| 162 | #define H2_SETTINGS_INITIAL_WINDOW_SIZE 0x0004 |
| 163 | #define H2_SETTINGS_MAX_FRAME_SIZE 0x0005 |
| 164 | #define H2_SETTINGS_MAX_HEADER_LIST_SIZE 0x0006 |
| 165 | |
| 166 | |
| 167 | /* some protocol constants */ |
| 168 | |
| 169 | // PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n |
| 170 | #define H2_CONN_PREFACE \ |
| 171 | "\x50\x52\x49\x20\x2a\x20\x48\x54" \ |
| 172 | "\x54\x50\x2f\x32\x2e\x30\x0d\x0a" \ |
| 173 | "\x0d\x0a\x53\x4d\x0d\x0a\x0d\x0a" |
| 174 | |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 175 | |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 176 | /* some flags related to protocol parsing */ |
| 177 | #define H2_MSGF_BODY 0x0001 // a body is present |
| 178 | #define H2_MSGF_BODY_CL 0x0002 // content-length is present |
| 179 | #define H2_MSGF_BODY_TUNNEL 0x0004 // a tunnel is in use (CONNECT) |
Christopher Faulet | 0b46548 | 2019-02-19 15:14:23 +0100 | [diff] [blame] | 180 | #define H2_MSGF_RSP_1XX 0x0010 // a 1xx ( != 101) HEADERS frame was received |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 181 | |
Willy Tarreau | 9c84d82 | 2019-01-30 15:09:21 +0100 | [diff] [blame] | 182 | #define H2_MAX_STREAM_ID ((1U << 31) - 1) |
| 183 | #define H2_MAX_FRAME_LEN ((1U << 24) - 1) |
| 184 | #define H2_DIR_REQ 1 |
| 185 | #define H2_DIR_RES 2 |
| 186 | #define H2_DIR_BOTH 3 |
| 187 | |
| 188 | /* constraints imposed by the protocol on each frame type, in terms of stream |
| 189 | * ID values, frame sizes, and direction so that most connection-level checks |
| 190 | * can be centralized regardless of the frame's acceptance. |
| 191 | */ |
| 192 | struct h2_frame_definition { |
| 193 | int32_t dir; /* 0=none, 1=request, 2=response, 3=both */ |
| 194 | int32_t min_id; /* minimum allowed stream ID */ |
| 195 | int32_t max_id; /* maximum allowed stream ID */ |
| 196 | int32_t min_len; /* minimum frame length */ |
| 197 | int32_t max_len; /* maximum frame length */ |
| 198 | }; |
| 199 | |
| 200 | extern struct h2_frame_definition h2_frame_definition[H2_FT_ENTRIES]; |
Willy Tarreau | 174b06a | 2018-04-25 18:13:58 +0200 | [diff] [blame] | 201 | |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 202 | /* various protocol processing functions */ |
| 203 | |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 204 | int h2_make_h1_request(struct http_hdr *list, char *out, int osize, unsigned int *msgf, unsigned long long *body_len); |
Willy Tarreau | 9d953e7 | 2019-01-03 16:18:14 +0100 | [diff] [blame] | 205 | int h2_make_h1_trailers(struct http_hdr *list, char *out, int osize); |
Willy Tarreau | beefaee | 2018-12-19 13:08:08 +0100 | [diff] [blame] | 206 | int h2_parse_cont_len_header(unsigned int *msgf, struct ist *value, unsigned long long *body_len); |
Willy Tarreau | 4790f7c | 2019-01-24 11:33:02 +0100 | [diff] [blame] | 207 | int h2_make_htx_request(struct http_hdr *list, struct htx *htx, unsigned int *msgf, unsigned long long *body_len); |
| 208 | int h2_make_htx_response(struct http_hdr *list, struct htx *htx, unsigned int *msgf, unsigned long long *body_len); |
Willy Tarreau | 1e1f27c | 2019-01-03 18:39:54 +0100 | [diff] [blame] | 209 | int h2_make_htx_trailers(struct http_hdr *list, struct htx *htx); |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 210 | |
Willy Tarreau | ffca736 | 2016-12-13 18:25:15 +0100 | [diff] [blame] | 211 | /* |
| 212 | * Some helpful debugging functions. |
| 213 | */ |
| 214 | |
Willy Tarreau | deab244 | 2018-12-21 14:56:57 +0100 | [diff] [blame] | 215 | /* returns a bit corresponding to the frame type */ |
| 216 | static inline unsigned int h2_ft_bit(enum h2_ft ft) |
| 217 | { |
Willy Tarreau | 09960b0 | 2020-09-18 07:43:38 +0200 | [diff] [blame] | 218 | if (ft >= H2_FT_ENTRIES) |
| 219 | return 0; |
Willy Tarreau | deab244 | 2018-12-21 14:56:57 +0100 | [diff] [blame] | 220 | return 1U << ft; |
| 221 | } |
| 222 | |
Willy Tarreau | ffca736 | 2016-12-13 18:25:15 +0100 | [diff] [blame] | 223 | /* returns the frame type as a string */ |
| 224 | static inline const char *h2_ft_str(int type) |
| 225 | { |
| 226 | switch (type) { |
| 227 | case H2_FT_DATA : return "DATA"; |
| 228 | case H2_FT_HEADERS : return "HEADERS"; |
| 229 | case H2_FT_PRIORITY : return "PRIORITY"; |
| 230 | case H2_FT_RST_STREAM : return "RST_STREAM"; |
| 231 | case H2_FT_SETTINGS : return "SETTINGS"; |
| 232 | case H2_FT_PUSH_PROMISE : return "PUSH_PROMISE"; |
| 233 | case H2_FT_PING : return "PING"; |
| 234 | case H2_FT_GOAWAY : return "GOAWAY"; |
| 235 | case H2_FT_WINDOW_UPDATE : return "WINDOW_UPDATE"; |
| 236 | default : return "_UNKNOWN_"; |
| 237 | } |
| 238 | } |
| 239 | |
Willy Tarreau | 9c84d82 | 2019-01-30 15:09:21 +0100 | [diff] [blame] | 240 | /* Returns an error code if the frame is valid protocol-wise, otherwise 0. <ft> |
| 241 | * is the frame type (H2_FT_*), <dir> is the direction (1=req, 2=res), <id> is |
| 242 | * the stream ID from the frame header, <len> is the frame length from the |
| 243 | * header. The purpose is to be able to quickly return a PROTOCOL_ERROR or |
| 244 | * FRAME_SIZE_ERROR connection error even for situations where the frame will |
| 245 | * be ignored. <mfs> must be the max frame size currently in place for the |
| 246 | * protocol. |
| 247 | */ |
| 248 | static inline int h2_frame_check(enum h2_ft ft, int dir, int32_t id, int32_t len, int32_t mfs) |
| 249 | { |
| 250 | struct h2_frame_definition *fd; |
| 251 | |
| 252 | if (ft >= H2_FT_ENTRIES) |
| 253 | return H2_ERR_NO_ERROR; // ignore unhandled frame types |
| 254 | |
| 255 | fd = &h2_frame_definition[ft]; |
| 256 | |
| 257 | if (!(dir & fd->dir)) |
| 258 | return H2_ERR_PROTOCOL_ERROR; |
| 259 | |
| 260 | if (id < fd->min_id || id > fd->max_id) |
| 261 | return H2_ERR_PROTOCOL_ERROR; |
| 262 | |
| 263 | if (len < fd->min_len || len > fd->max_len) |
| 264 | return H2_ERR_FRAME_SIZE_ERROR; |
| 265 | |
| 266 | if (len > mfs) |
| 267 | return H2_ERR_FRAME_SIZE_ERROR; |
| 268 | |
| 269 | if (ft == H2_FT_SETTINGS && (len % 6) != 0) |
| 270 | return H2_ERR_FRAME_SIZE_ERROR; // RFC7540#6.5 |
| 271 | |
| 272 | return H2_ERR_NO_ERROR; |
| 273 | } |
| 274 | |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 275 | /* returns the pseudo-header <str> corresponds to among H2_PHDR_IDX_*, 0 if not a |
| 276 | * pseudo-header, or -1 if not a valid pseudo-header. |
| 277 | */ |
| 278 | static inline int h2_str_to_phdr(const struct ist str) |
| 279 | { |
| 280 | if (*str.ptr == ':') { |
| 281 | if (isteq(str, ist(":path"))) return H2_PHDR_IDX_PATH; |
| 282 | else if (isteq(str, ist(":method"))) return H2_PHDR_IDX_METH; |
| 283 | else if (isteq(str, ist(":scheme"))) return H2_PHDR_IDX_SCHM; |
| 284 | else if (isteq(str, ist(":status"))) return H2_PHDR_IDX_STAT; |
| 285 | else if (isteq(str, ist(":authority"))) return H2_PHDR_IDX_AUTH; |
| 286 | |
| 287 | /* all other names starting with ':' */ |
| 288 | return -1; |
| 289 | } |
| 290 | |
| 291 | /* not a pseudo header */ |
| 292 | return 0; |
| 293 | } |
| 294 | |
Willy Tarreau | 3083276 | 2017-12-30 14:39:09 +0100 | [diff] [blame] | 295 | /* returns the pseudo-header name <num> as a string, or ":UNKNOWN" if unknown */ |
| 296 | static inline const char *h2_phdr_to_str(int phdr) |
| 297 | { |
| 298 | switch (phdr) { |
| 299 | case H2_PHDR_IDX_NONE: return ":NONE"; |
| 300 | case H2_PHDR_IDX_AUTH: return ":authority"; |
| 301 | case H2_PHDR_IDX_METH: return ":method"; |
| 302 | case H2_PHDR_IDX_PATH: return ":path"; |
| 303 | case H2_PHDR_IDX_SCHM: return ":scheme"; |
| 304 | case H2_PHDR_IDX_STAT: return ":status"; |
| 305 | case H2_PHDR_IDX_HOST: return "Host"; |
| 306 | default: return ":UNKNOWN"; |
| 307 | } |
| 308 | } |
Willy Tarreau | f24ea8e | 2017-11-21 19:55:27 +0100 | [diff] [blame] | 309 | |
Willy Tarreau | ffca736 | 2016-12-13 18:25:15 +0100 | [diff] [blame] | 310 | #endif /* _COMMON_H2_H */ |
| 311 | |
| 312 | /* |
| 313 | * Local variables: |
| 314 | * c-indent-level: 8 |
| 315 | * c-basic-offset: 8 |
| 316 | * End: |
| 317 | */ |