Frédéric Lécaille | 0c4e3b0 | 2020-11-23 14:10:37 +0100 | [diff] [blame] | 1 | /* |
| 2 | * include/haproxy/xprt_quic-t.h |
| 3 | * This file contains applet function prototypes |
| 4 | * |
| 5 | * Copyright 2019 HAProxy Technologies, Frédéric Lécaille <flecaille@haproxy.com> |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #ifndef _HAPROXY_XPRT_QUIC_T_H |
| 23 | #define _HAPROXY_XPRT_QUIC_T_H |
| 24 | #ifdef USE_QUIC |
| 25 | #ifndef USE_OPENSSL |
| 26 | #error "Must define USE_OPENSSL" |
| 27 | #endif |
| 28 | |
| 29 | #include <sys/socket.h> |
| 30 | #include <openssl/ssl.h> |
| 31 | |
| 32 | #include <haproxy/list.h> |
| 33 | |
| 34 | #include <haproxy/quic_cc-t.h> |
| 35 | #include <haproxy/quic_frame-t.h> |
| 36 | #include <haproxy/quic_tls-t.h> |
| 37 | #include <haproxy/quic_loss-t.h> |
| 38 | #include <haproxy/task.h> |
| 39 | |
| 40 | #include <import/eb64tree.h> |
| 41 | #include <import/ebmbtree.h> |
| 42 | |
| 43 | #define QUIC_PROTOCOL_VERSION_DRAFT_28 0xff00001c /* draft-28 */ |
| 44 | |
| 45 | #define QUIC_INITIAL_IPV4_MTU 1252 /* (bytes) */ |
| 46 | #define QUIC_INITIAL_IPV6_MTU 1232 |
| 47 | /* XXX TO DO XXX */ |
| 48 | /* Maximum packet length during handshake */ |
| 49 | #define QUIC_PACKET_MAXLEN QUIC_INITIAL_IPV4_MTU |
| 50 | |
| 51 | /* The minimum length of Initial packets. */ |
| 52 | #define QUIC_INITIAL_PACKET_MINLEN 1200 |
| 53 | |
| 54 | /* |
| 55 | * QUIC CID lengths. This the length of the connection IDs for this QUIC |
| 56 | * implementation. |
| 57 | */ |
| 58 | #define QUIC_CID_LEN 8 |
| 59 | |
| 60 | /* Common definitions for short and long QUIC packet headers. */ |
| 61 | /* QUIC connection ID maximum length for version 1. */ |
| 62 | #define QUIC_CID_MAXLEN 20 /* bytes */ |
| 63 | /* |
| 64 | * All QUIC packets with long headers are made of at least (in bytes): |
| 65 | * flags(1), version(4), DCID length(1), DCID(0..20), SCID length(1), SCID(0..20) |
| 66 | */ |
| 67 | #define QUIC_LONG_PACKET_MINLEN 7 |
| 68 | /* |
| 69 | * All QUIC packets with short headers are made of at least (in bytes): |
| 70 | * flags(1), DCID length(1), DCID(0..20) |
| 71 | */ |
| 72 | #define QUIC_SHORT_PACKET_MINLEN 2 |
| 73 | /* Byte 0 of QUIC packets. */ |
| 74 | #define QUIC_PACKET_LONG_HEADER_BIT 0x80 /* Long header format if set, short if not. */ |
| 75 | #define QUIC_PACKET_FIXED_BIT 0x40 /* Must always be set for all the headers. */ |
| 76 | |
| 77 | /* |
| 78 | * 0 1 2 3 |
| 79 | * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 80 | * +-+-+-+-+-+-+-+-+ |
| 81 | * |1|1|T|T|X|X|X|X| |
| 82 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 83 | * | Version (32) | |
| 84 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 85 | * | DCID Len (8) | |
| 86 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 87 | * | Destination Connection ID (0..160) ... |
| 88 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 89 | * | SCID Len (8) | |
| 90 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 91 | * | Source Connection ID (0..160) ... |
| 92 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 93 | * Long Header Packet Format |
| 94 | */ |
| 95 | |
| 96 | /* Two bits (T) for QUIC packet types. */ |
| 97 | #define QUIC_PACKET_TYPE_BITMASK 0x03 |
| 98 | #define QUIC_PACKET_TYPE_SHIFT 4 |
| 99 | |
| 100 | enum quic_pkt_type { |
| 101 | QUIC_PACKET_TYPE_INITIAL, |
| 102 | QUIC_PACKET_TYPE_0RTT, |
| 103 | QUIC_PACKET_TYPE_HANDSHAKE, |
| 104 | QUIC_PACKET_TYPE_RETRY, |
| 105 | /* |
| 106 | * The following one is not defined by the RFC but we define it for our |
| 107 | * own convenience. |
| 108 | */ |
| 109 | QUIC_PACKET_TYPE_SHORT, |
| 110 | }; |
| 111 | |
| 112 | /* Packet number field length. */ |
| 113 | #define QUIC_PACKET_PNL_BITMASK 0x03 |
| 114 | #define QUIC_PACKET_PN_MAXLEN 4 |
| 115 | |
| 116 | /* |
| 117 | * 0 1 2 3 |
| 118 | * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 119 | * +-+-+-+-+-+-+-+-+ |
| 120 | * |0|1|S|R|R|K|P|P| |
| 121 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 122 | * | Destination Connection ID (0..160) ... |
| 123 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 124 | * | Packet Number (8/16/24/32) ... |
| 125 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 126 | * | Protected Payload (*) ... |
| 127 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 128 | * Short Header Packet Format |
| 129 | */ |
| 130 | |
| 131 | /* Bit (S) of short header. */ |
| 132 | #define QUIC_PACKET_SPIN_BIT 0x20 |
| 133 | |
| 134 | /* Reserved Bits (R): The next two bits of byte 0 are reserved. |
| 135 | * These bits are protected using header protection |
| 136 | * (see Section 5.4 of [QUIC-TLS]). The value included |
| 137 | * prior to protection MUST be set to 0. An endpoint MUST treat |
| 138 | * receipt of a packet that has a non-zero value for these bits, |
| 139 | * after removing both packet and header protection, as a connection |
| 140 | * error of type PROTOCOL_VIOLATION. Discarding such a packet after |
| 141 | * only removing header protection can expose the endpoint to attacks |
| 142 | * (see Section 9.3 of [QUIC-TLS]). |
| 143 | */ |
| 144 | #define QUIC_PACKET_RESERVED_BITS 0x18 /* (protected) */ |
| 145 | |
| 146 | #define QUIC_PACKET_KEY_PHASE_BIT 0x04 /* (protected) */ |
| 147 | |
| 148 | /* |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 149 | * Transport level error codes. |
Frédéric Lécaille | 0c4e3b0 | 2020-11-23 14:10:37 +0100 | [diff] [blame] | 150 | */ |
| 151 | #define NO_ERROR 0x00 |
| 152 | #define INTERNAL_ERROR 0x01 |
| 153 | #define CONNECTION_REFUSED_ERROR 0x02 |
| 154 | #define FLOW_CONTROL_ERROR 0x03 |
| 155 | #define STREAM_LIMIT_ERROR 0x04 |
| 156 | #define STREAM_STATE_ERROR 0x05 |
| 157 | #define FINAL_SIZE_ERROR 0x06 |
| 158 | #define FRAME_ENCODING_ERROR 0x07 |
| 159 | #define TRANSPORT_PARAMETER_ERROR 0x08 |
| 160 | #define CONNECTION_ID_LIMIT_ERROR 0x09 |
| 161 | #define PROTOCOL_VIOLATION 0x0a |
| 162 | #define INVALID_TOKEN 0x0b |
| 163 | #define APPLICATION_ERROR 0x0c |
| 164 | #define CRYPTO_BUFFER_EXCEEDED 0x0d |
| 165 | |
| 166 | /* XXX TODO: check/complete this remaining part (256 crypto reserved errors). */ |
| 167 | #define CRYPTO_ERROR 0x100 |
| 168 | |
| 169 | /* The maximum number of QUIC packets stored by the fd I/O handler by QUIC |
| 170 | * connection. Must be a power of two. |
| 171 | */ |
| 172 | #define QUIC_CONN_MAX_PACKET 64 |
| 173 | |
| 174 | #define QUIC_STATELESS_RESET_TOKEN_LEN 16 |
| 175 | |
| 176 | #define QUIC_EV_CONN_NEW (1ULL << 0) |
| 177 | #define QUIC_EV_CONN_INIT (1ULL << 1) |
| 178 | #define QUIC_EV_CONN_ISEC (1ULL << 2) |
| 179 | #define QUIC_EV_CONN_RSEC (1ULL << 3) |
| 180 | #define QUIC_EV_CONN_WSEC (1ULL << 4) |
| 181 | #define QUIC_EV_CONN_RWSEC (1ULL << 5) |
| 182 | #define QUIC_EV_CONN_LPKT (1ULL << 6) |
| 183 | #define QUIC_EV_CONN_SPKT (1ULL << 7) |
Frédéric Lécaille | f63921f | 2020-12-18 09:48:20 +0100 | [diff] [blame] | 184 | #define QUIC_EV_CONN_ENCPKT (1ULL << 8) |
Frédéric Lécaille | 0c4e3b0 | 2020-11-23 14:10:37 +0100 | [diff] [blame] | 185 | #define QUIC_EV_CONN_HPKT (1ULL << 9) |
| 186 | #define QUIC_EV_CONN_PAPKT (1ULL << 10) |
| 187 | #define QUIC_EV_CONN_PAPKTS (1ULL << 11) |
| 188 | #define QUIC_EV_CONN_HDSHK (1ULL << 12) |
| 189 | #define QUIC_EV_CONN_RMHP (1ULL << 13) |
| 190 | #define QUIC_EV_CONN_PRSHPKT (1ULL << 14) |
| 191 | #define QUIC_EV_CONN_PRSAPKT (1ULL << 15) |
| 192 | #define QUIC_EV_CONN_PRSFRM (1ULL << 16) |
| 193 | #define QUIC_EV_CONN_PRSAFRM (1ULL << 17) |
| 194 | #define QUIC_EV_CONN_BFRM (1ULL << 18) |
| 195 | #define QUIC_EV_CONN_PHPKTS (1ULL << 19) |
| 196 | #define QUIC_EV_CONN_TRMHP (1ULL << 20) |
| 197 | #define QUIC_EV_CONN_ELRMHP (1ULL << 21) |
| 198 | #define QUIC_EV_CONN_ELRXPKTS (1ULL << 22) |
| 199 | #define QUIC_EV_CONN_SSLDATA (1ULL << 23) |
| 200 | #define QUIC_EV_CONN_RXCDATA (1ULL << 24) |
| 201 | #define QUIC_EV_CONN_ADDDATA (1ULL << 25) |
| 202 | #define QUIC_EV_CONN_FFLIGHT (1ULL << 26) |
| 203 | #define QUIC_EV_CONN_SSLALERT (1ULL << 27) |
Frédéric Lécaille | 242fb1b | 2020-12-31 12:45:38 +0100 | [diff] [blame] | 204 | #define QUIC_EV_CONN_PSTRM (1ULL << 28) |
Frédéric Lécaille | 0c4e3b0 | 2020-11-23 14:10:37 +0100 | [diff] [blame] | 205 | #define QUIC_EV_CONN_RTTUPDT (1ULL << 29) |
| 206 | #define QUIC_EV_CONN_CC (1ULL << 30) |
| 207 | #define QUIC_EV_CONN_SPPKTS (1ULL << 31) |
| 208 | #define QUIC_EV_CONN_PKTLOSS (1ULL << 32) |
| 209 | #define QUIC_EV_CONN_STIMER (1ULL << 33) |
| 210 | #define QUIC_EV_CONN_PTIMER (1ULL << 34) |
| 211 | #define QUIC_EV_CONN_SPTO (1ULL << 35) |
Frédéric Lécaille | 6c1e36c | 2020-12-23 17:17:37 +0100 | [diff] [blame] | 212 | #define QUIC_EV_CONN_BCFRMS (1ULL << 36) |
Frédéric Lécaille | 0c4e3b0 | 2020-11-23 14:10:37 +0100 | [diff] [blame] | 213 | |
| 214 | /* Similar to kernel min()/max() definitions. */ |
| 215 | #define QUIC_MIN(a, b) ({ \ |
| 216 | typeof(a) _a = (a); \ |
| 217 | typeof(b) _b = (b); \ |
| 218 | (void) (&_a == &_b); \ |
| 219 | _a < _b ? _a : _b; }) |
| 220 | |
| 221 | #define QUIC_MAX(a, b) ({ \ |
| 222 | typeof(a) _a = (a); \ |
| 223 | typeof(b) _b = (b); \ |
| 224 | (void) (&_a == &_b); \ |
| 225 | _a > _b ? _a : _b; }) |
| 226 | |
| 227 | extern struct trace_source trace_quic; |
| 228 | extern struct pool_head *pool_head_quic_rx_packet; |
| 229 | extern struct pool_head *pool_head_quic_tx_packet; |
| 230 | extern struct pool_head *pool_head_quic_tx_frm; |
| 231 | |
| 232 | /* |
| 233 | * This struct is used by ebmb_node structs as last member of flexible arrays. |
| 234 | * So do not change the order of the member of quic_cid struct. |
| 235 | * <data> member must be the first one. |
| 236 | */ |
| 237 | struct quic_cid { |
| 238 | unsigned char data[QUIC_CID_MAXLEN + sizeof(in_port_t) + sizeof(struct in6_addr)]; |
| 239 | unsigned char len; |
| 240 | }; |
| 241 | |
| 242 | /* The data structure used to build a set of connection IDs for each connection. */ |
| 243 | struct quic_connection_id { |
| 244 | struct eb64_node seq_num; |
| 245 | uint64_t retire_prior_to; |
| 246 | struct quic_cid cid; |
| 247 | unsigned char stateless_reset_token[QUIC_STATELESS_RESET_TOKEN_LEN]; |
| 248 | }; |
| 249 | |
| 250 | struct preferred_address { |
| 251 | uint16_t ipv4_port; |
| 252 | uint16_t ipv6_port; |
| 253 | uint8_t ipv4_addr[4]; |
| 254 | uint8_t ipv6_addr[16]; |
| 255 | struct quic_cid cid; |
| 256 | uint8_t stateless_reset_token[QUIC_STATELESS_RESET_TOKEN_LEN]; |
| 257 | }; |
| 258 | |
| 259 | /* Default values for some of transport parameters */ |
| 260 | #define QUIC_DFLT_MAX_PACKET_SIZE 65527 |
| 261 | #define QUIC_DFLT_ACK_DELAY_COMPONENT 3 /* milliseconds */ |
| 262 | #define QUIC_DFLT_MAX_ACK_DELAY 25 /* milliseconds */ |
| 263 | |
| 264 | /* Types of QUIC transport parameters */ |
| 265 | #define QUIC_TP_ORIGINAL_DESTINATION_CONNECTION_ID 0 |
| 266 | #define QUIC_TP_IDLE_TIMEOUT 1 |
| 267 | #define QUIC_TP_STATELESS_RESET_TOKEN 2 |
| 268 | #define QUIC_TP_MAX_PACKET_SIZE 3 |
| 269 | #define QUIC_TP_INITIAL_MAX_DATA 4 |
| 270 | #define QUIC_TP_INITIAL_MAX_STREAM_DATA_BIDI_LOCAL 5 |
| 271 | #define QUIC_TP_INITIAL_MAX_STREAM_DATA_BIDI_REMOTE 6 |
| 272 | #define QUIC_TP_INITIAL_MAX_STREAM_DATA_UNI 7 |
| 273 | #define QUIC_TP_INITIAL_MAX_STREAMS_BIDI 8 |
| 274 | #define QUIC_TP_INITIAL_MAX_STREAMS_UNI 9 |
| 275 | #define QUIC_TP_ACK_DELAY_EXPONENT 10 |
| 276 | #define QUIC_TP_MAX_ACK_DELAY 11 |
| 277 | #define QUIC_TP_DISABLE_ACTIVE_MIGRATION 12 |
| 278 | #define QUIC_TP_PREFERRED_ADDRESS 13 |
| 279 | #define QUIC_TP_ACTIVE_CONNECTION_ID_LIMIT 14 |
| 280 | #define QUIC_TP_INITIAL_SOURCE_CONNECTION_ID 15 |
| 281 | |
| 282 | /* |
| 283 | * These defines are not for transport parameter type, but the maximum accepted value for |
| 284 | * transport parameter types. |
| 285 | */ |
| 286 | #define QUIC_TP_ACK_DELAY_EXPONENT_LIMIT 20 |
| 287 | #define QUIC_TP_MAX_ACK_DELAY_LIMIT (1UL << 14) |
| 288 | |
| 289 | /* The maximum length of encoded transport parameters for any QUIC peer. */ |
| 290 | #define QUIC_TP_MAX_ENCLEN 128 |
| 291 | /* |
| 292 | * QUIC transport parameters. |
| 293 | * Note that forbidden parameters sent by clients MUST generate TRANSPORT_PARAMETER_ERROR errors. |
| 294 | */ |
| 295 | struct quic_transport_params { |
| 296 | uint64_t idle_timeout; |
| 297 | uint64_t max_packet_size; /* Default: 65527 (max of UDP payload for IPv6) */ |
| 298 | uint64_t initial_max_data; |
| 299 | uint64_t initial_max_stream_data_bidi_local; |
| 300 | uint64_t initial_max_stream_data_bidi_remote; |
| 301 | uint64_t initial_max_stream_data_uni; |
| 302 | uint64_t initial_max_streams_bidi; |
| 303 | uint64_t initial_max_streams_uni; |
| 304 | uint64_t ack_delay_exponent; /* Default: 3, max: 20 */ |
| 305 | uint64_t max_ack_delay; /* Default: 3ms, max: 2^14ms*/ |
| 306 | uint64_t active_connection_id_limit; |
| 307 | |
| 308 | /* Booleans */ |
| 309 | uint8_t disable_active_migration; |
| 310 | uint8_t with_stateless_reset_token; |
| 311 | uint8_t with_preferred_address; |
| 312 | uint8_t original_destination_connection_id_present; |
| 313 | uint8_t initial_source_connection_id_present; |
| 314 | |
| 315 | uint8_t stateless_reset_token[QUIC_STATELESS_RESET_TOKEN_LEN]; /* Forbidden for clients */ |
| 316 | /* |
| 317 | * MUST be sent by servers. |
| 318 | * When received by clients, must be set to 1 if present. |
| 319 | */ |
| 320 | struct quic_cid original_destination_connection_id; /* Forbidden for clients */ |
| 321 | /* MUST be present both for servers and clients. */ |
| 322 | struct quic_cid initial_source_connection_id; |
| 323 | struct preferred_address preferred_address; /* Forbidden for clients */ |
| 324 | }; |
| 325 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 326 | /* Structure to hold a range of ACKs sent in ACK frames. */ |
| 327 | struct quic_arng { |
Frédéric Lécaille | 0c4e3b0 | 2020-11-23 14:10:37 +0100 | [diff] [blame] | 328 | int64_t first; |
| 329 | int64_t last; |
| 330 | }; |
| 331 | |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 332 | /* Structure to hold a range of ACKs to be store as a node in a tree of |
| 333 | * ACK ranges. |
| 334 | */ |
| 335 | struct quic_arng_node { |
| 336 | struct eb64_node first; |
| 337 | uint64_t last; |
| 338 | }; |
| 339 | |
| 340 | /* Structure to maintain a set of ACK ranges to be used to build ACK frames. */ |
| 341 | struct quic_arngs { |
| 342 | /* ebtree of ACK ranges organized by their first value. */ |
| 343 | struct eb_root root; |
| 344 | /* The number of ACK ranges is this tree */ |
Frédéric Lécaille | 0c4e3b0 | 2020-11-23 14:10:37 +0100 | [diff] [blame] | 345 | size_t sz; |
| 346 | /* The number of bytes required to encode this ACK ranges lists. */ |
| 347 | size_t enc_sz; |
| 348 | }; |
| 349 | |
| 350 | /* Flag the packet number space as requiring an ACK frame to be sent. */ |
| 351 | #define QUIC_FL_PKTNS_ACK_REQUIRED (1UL << 0) |
| 352 | #define QUIC_FL_PKTNS_ACK_RECEIVED (1UL << 1) |
| 353 | |
| 354 | /* The maximum number of dgrams which may be sent upon PTO expirations. */ |
| 355 | #define QUIC_MAX_NB_PTO_DGRAMS 2 |
| 356 | |
| 357 | /* QUIC packet number space */ |
| 358 | struct quic_pktns { |
| 359 | struct { |
| 360 | /* List of frames to send. */ |
| 361 | struct list frms; |
| 362 | /* Next packet number to use for transmissions. */ |
| 363 | int64_t next_pn; |
| 364 | /* Largest acked sent packet. */ |
| 365 | int64_t largest_acked_pn; |
| 366 | /* The packet which has been sent. */ |
| 367 | struct eb_root pkts; |
| 368 | /* The time the most recent ack-eliciting packer was sent. */ |
| 369 | unsigned int time_of_last_eliciting; |
| 370 | /* The time this packet number space has experienced packet loss. */ |
| 371 | unsigned int loss_time; |
| 372 | /* Boolean to denote if we must send probe packet. */ |
| 373 | unsigned int pto_probe; |
| 374 | /* In flight bytes for this packet number space. */ |
| 375 | size_t in_flight; |
| 376 | } tx; |
| 377 | struct { |
| 378 | /* Largest packet number */ |
| 379 | int64_t largest_pn; |
| 380 | /* Number of ack-eliciting packets. */ |
| 381 | size_t nb_ack_eliciting; |
Frédéric Lécaille | 8090b51 | 2020-11-30 16:19:22 +0100 | [diff] [blame] | 382 | struct quic_arngs arngs; |
Frédéric Lécaille | 0c4e3b0 | 2020-11-23 14:10:37 +0100 | [diff] [blame] | 383 | } rx; |
| 384 | unsigned int flags; |
| 385 | }; |
| 386 | |
| 387 | /* The QUIC packet numbers are 62-bits integers */ |
| 388 | #define QUIC_MAX_PACKET_NUM ((1ULL << 62) - 1) |
| 389 | |
| 390 | /* Default QUIC connection transport parameters */ |
| 391 | extern struct quic_transport_params quic_dflt_transport_params; |
| 392 | |
| 393 | /* Flag a received packet as being an ack-eliciting packet. */ |
| 394 | #define QUIC_FL_RX_PACKET_ACK_ELICITING (1UL << 0) |
| 395 | |
| 396 | struct quic_rx_packet { |
| 397 | struct list list; |
| 398 | struct list rx_list; |
| 399 | struct quic_conn *qc; |
| 400 | unsigned char type; |
| 401 | uint32_t version; |
| 402 | /* Initial desctination connection ID. */ |
| 403 | struct quic_cid dcid; |
| 404 | struct quic_cid scid; |
| 405 | size_t odcid_len; |
| 406 | size_t pn_offset; |
| 407 | /* Packet number */ |
| 408 | int64_t pn; |
| 409 | /* Packet number length */ |
| 410 | uint32_t pnl; |
| 411 | uint64_t token_len; |
| 412 | /* Packet length */ |
| 413 | uint64_t len; |
| 414 | /* Additional authenticated data length */ |
| 415 | size_t aad_len; |
| 416 | unsigned char data[QUIC_PACKET_MAXLEN]; |
| 417 | struct eb64_node pn_node; |
| 418 | volatile unsigned int refcnt; |
| 419 | /* Source address of this packet. */ |
| 420 | struct sockaddr_storage saddr; |
| 421 | unsigned int flags; |
| 422 | }; |
| 423 | |
| 424 | /* UDP datagram context used by the I/O handler receiver callbacks. |
| 425 | * Useful to store the connection |
| 426 | */ |
| 427 | struct quic_dgram_ctx { |
| 428 | struct quic_conn *qc; |
| 429 | struct ebmb_node *dcid_node; |
| 430 | void *owner; |
| 431 | }; |
| 432 | |
| 433 | /* QUIC packet reader. */ |
| 434 | typedef ssize_t qpkt_read_func(unsigned char **buf, |
| 435 | const unsigned char *end, |
| 436 | struct quic_rx_packet *qpkt, |
| 437 | struct quic_dgram_ctx *dgram_ctx, |
| 438 | struct sockaddr_storage *saddr); |
| 439 | |
| 440 | /* Structure to store enough information about the RX CRYPTO frames. */ |
| 441 | struct quic_rx_crypto_frm { |
| 442 | struct eb64_node offset_node; |
| 443 | uint64_t len; |
| 444 | const unsigned char *data; |
| 445 | struct quic_rx_packet *pkt; |
| 446 | }; |
| 447 | |
| 448 | /* Flag a sent packet as being an ack-eliciting packet. */ |
| 449 | #define QUIC_FL_TX_PACKET_ACK_ELICITING (1UL << 0) |
| 450 | /* Flag a sent packet as containing a PADDING frame. */ |
| 451 | #define QUIC_FL_TX_PACKET_PADDING (1UL << 1) |
| 452 | /* Flag a sent packet as being in flight. */ |
| 453 | #define QUIC_FL_TX_PACKET_IN_FLIGHT (QUIC_FL_TX_PACKET_ACK_ELICITING | QUIC_FL_TX_PACKET_PADDING) |
| 454 | |
| 455 | /* Structure to store enough information about TX QUIC packets. */ |
| 456 | struct quic_tx_packet { |
| 457 | /* List entry point. */ |
| 458 | struct list list; |
| 459 | /* This is not the packet length but the length of outstanding data |
| 460 | * for in flight TX packet. |
| 461 | */ |
| 462 | size_t in_flight_len; |
| 463 | struct eb64_node pn_node; |
| 464 | /* The number of bytes of CRYPTO data in this packet. */ |
| 465 | unsigned int cdata_len; |
| 466 | /* The list of frames of this packet. */ |
| 467 | struct list frms; |
| 468 | /* The time this packet was sent (usec). */ |
| 469 | unsigned int time_sent; |
| 470 | /* Packet number spakce. */ |
| 471 | struct quic_pktns *pktns; |
| 472 | /* Flags. */ |
| 473 | unsigned int flags; |
| 474 | }; |
| 475 | |
| 476 | /* Structure to stora enough information about the TX frames. */ |
| 477 | struct quic_tx_frm { |
| 478 | struct list list; |
| 479 | unsigned char type; |
| 480 | union { |
| 481 | struct quic_crypto crypto; |
| 482 | struct quic_new_connection_id new_connection_id; |
| 483 | }; |
| 484 | }; |
| 485 | |
| 486 | |
| 487 | #define QUIC_CRYPTO_BUF_SHIFT 10 |
| 488 | #define QUIC_CRYPTO_BUF_MASK ((1UL << QUIC_CRYPTO_BUF_SHIFT) - 1) |
| 489 | /* The maximum allowed size of CRYPTO data buffer provided by the TLS stack. */ |
| 490 | #define QUIC_CRYPTO_BUF_SZ (1UL << QUIC_CRYPTO_BUF_SHIFT) /* 1 KB */ |
| 491 | |
| 492 | /* The maximum number of bytes of CRYPTO data in flight during handshakes. */ |
| 493 | #define QUIC_CRYPTO_IN_FLIGHT_MAX 4096 |
| 494 | |
| 495 | /* |
| 496 | * CRYPTO buffer struct. |
| 497 | * Such buffers are used to send CRYPTO data. |
| 498 | */ |
| 499 | struct quic_crypto_buf { |
| 500 | unsigned char data[QUIC_CRYPTO_BUF_SZ]; |
| 501 | size_t sz; |
| 502 | }; |
| 503 | |
| 504 | /* QUIC buffer structure used to build outgoing packets. */ |
| 505 | struct q_buf { |
| 506 | /* Points to the data in this buffer. */ |
| 507 | unsigned char *area; |
| 508 | /* Points to the current position to write into this buffer. */ |
| 509 | unsigned char *pos; |
| 510 | /* Point to the end of this buffer past one. */ |
| 511 | const unsigned char *end; |
| 512 | /* The number of data bytes in this buffer. */ |
| 513 | size_t data; |
| 514 | /* The list of packets attached to this buffer which have not been already sent. */ |
| 515 | struct list pkts; |
| 516 | }; |
| 517 | |
| 518 | struct quic_enc_level { |
| 519 | enum ssl_encryption_level_t level; |
| 520 | struct quic_tls_ctx tls_ctx; |
| 521 | struct { |
| 522 | /* The packets received by the listener I/O handler |
| 523 | with header protection removed. */ |
| 524 | struct eb_root pkts; |
| 525 | /* Liste of QUIC packets with protected header. */ |
| 526 | struct list pqpkts; |
| 527 | /* Crypto frames */ |
| 528 | struct { |
| 529 | uint64_t offset; |
| 530 | struct eb_root frms; /* XXX TO CHECK XXX */ |
| 531 | } crypto; |
| 532 | } rx; |
| 533 | struct { |
| 534 | struct { |
| 535 | struct quic_crypto_buf **bufs; |
| 536 | /* The number of element in use in the previous array. */ |
| 537 | size_t nb_buf; |
| 538 | /* The total size of the CRYPTO data stored in the CRYPTO buffers. */ |
| 539 | size_t sz; |
| 540 | /* The offset of the CRYPT0 data stream. */ |
| 541 | uint64_t offset; |
| 542 | } crypto; |
| 543 | } tx; |
| 544 | struct quic_pktns *pktns; |
| 545 | }; |
| 546 | |
| 547 | struct quic_path { |
| 548 | /* Control congestion. */ |
| 549 | struct quic_cc cc; |
| 550 | /* Packet loss detection information. */ |
| 551 | struct quic_loss loss; |
| 552 | |
| 553 | /* MTU. */ |
| 554 | size_t mtu; |
| 555 | /* Congestion window. */ |
| 556 | uint64_t cwnd; |
| 557 | /* Minimum congestion window. */ |
| 558 | uint64_t min_cwnd; |
| 559 | /* Prepared data to be sent (in bytes). */ |
| 560 | uint64_t prep_in_flight; |
| 561 | /* Outstanding data (in bytes). */ |
| 562 | uint64_t in_flight; |
| 563 | /* Number of in flight ack-eliciting packets. */ |
Frédéric Lécaille | f7e0b8d | 2020-12-16 17:33:11 +0100 | [diff] [blame] | 564 | uint64_t ifae_pkts; |
Frédéric Lécaille | 0c4e3b0 | 2020-11-23 14:10:37 +0100 | [diff] [blame] | 565 | }; |
| 566 | |
| 567 | /* The number of buffers for outgoing packets (must be a power of two). */ |
| 568 | #define QUIC_CONN_TX_BUFS_NB 8 |
| 569 | #define QUIC_CONN_TX_BUF_SZ QUIC_PACKET_MAXLEN |
| 570 | |
| 571 | struct quic_conn { |
| 572 | uint32_t version; |
| 573 | |
| 574 | /* Transport parameters. */ |
| 575 | struct quic_transport_params params; |
| 576 | unsigned char enc_params[QUIC_TP_MAX_ENCLEN]; /* encoded QUIC transport parameters */ |
| 577 | size_t enc_params_len; |
| 578 | |
| 579 | /* |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 580 | * Original Destination Connection ID (coming with first client Initial packets). |
Frédéric Lécaille | 0c4e3b0 | 2020-11-23 14:10:37 +0100 | [diff] [blame] | 581 | * Used only by servers. |
| 582 | */ |
| 583 | struct ebmb_node odcid_node; |
| 584 | struct quic_cid odcid; |
| 585 | |
| 586 | struct quic_cid dcid; |
| 587 | struct ebmb_node scid_node; |
| 588 | struct quic_cid scid; |
| 589 | struct eb_root cids; |
| 590 | |
| 591 | struct quic_enc_level els[QUIC_TLS_ENC_LEVEL_MAX]; |
| 592 | |
| 593 | struct quic_transport_params rx_tps; |
| 594 | |
| 595 | struct quic_pktns pktns[QUIC_TLS_PKTNS_MAX]; |
| 596 | |
| 597 | /* Used only to reach the tasklet for the I/O handler from this quic_conn object. */ |
| 598 | struct connection *conn; |
| 599 | /* Output buffer used during the handshakes. */ |
| 600 | struct { |
| 601 | unsigned char data[QUIC_PACKET_MAXLEN]; |
| 602 | unsigned char *pos; |
| 603 | } obuf; |
| 604 | |
| 605 | struct { |
| 606 | /* The remaining frames to send. */ |
| 607 | struct list frms_to_send; |
| 608 | |
| 609 | /* Array of buffers. */ |
| 610 | struct q_buf **bufs; |
| 611 | /* The size of the previous array. */ |
| 612 | size_t nb_buf; |
| 613 | /* Writer index. */ |
| 614 | int wbuf; |
| 615 | /* Reader index. */ |
| 616 | int rbuf; |
| 617 | /* Number of sent bytes. */ |
| 618 | uint64_t bytes; |
| 619 | /* The number of datagrams which may be sent |
| 620 | * when sending probe packets. |
| 621 | */ |
| 622 | int nb_pto_dgrams; |
| 623 | } tx; |
| 624 | struct { |
| 625 | /* Number of received bytes. */ |
| 626 | uint64_t bytes; |
| 627 | } rx; |
Frédéric Lécaille | 0c4e3b0 | 2020-11-23 14:10:37 +0100 | [diff] [blame] | 628 | unsigned int max_ack_delay; |
| 629 | struct quic_path paths[1]; |
| 630 | struct quic_path *path; |
| 631 | |
| 632 | struct task *timer_task; |
| 633 | unsigned int timer; |
| 634 | }; |
| 635 | |
| 636 | #endif /* USE_QUIC */ |
| 637 | #endif /* _HAPROXY_XPRT_QUIC_T_H */ |