lsquic_parse.h revision 06b2a236
1/* Copyright (c) 2017 - 2021 LiteSpeed Technologies Inc. See LICENSE. */ 2#ifndef LSQUIC_PARSE_H 3#define LSQUIC_PARSE_H 1 4 5#include <stdint.h> 6 7#include "lsquic_packet_common.h" 8#include "lsquic_packet_gquic.h" 9 10struct lsquic_conn; 11struct lsquic_packet_in; 12struct lsquic_packet_out; 13struct packin_parse_state; 14struct stream_frame; 15struct lsquic_cid; 16enum packet_out_flags; 17enum lsquic_version; 18enum stream_dir; 19 20 21struct ack_info 22{ 23 enum packnum_space pns; 24 enum { 25 AI_ECN = 1 << 0, /* ecn_counts[1,2,3] contain ECN counts */ 26 AI_TRUNCATED = 1 << 1, /* There were more ranges to parse, but we 27 * ran out of elements in `ranges'. 28 */ 29 } flags; 30 unsigned n_ranges; /* This is at least 1 */ 31 /* Largest acked is ack_info.ranges[0].high */ 32 lsquic_time_t lack_delta; 33 uint64_t ecn_counts[4]; 34 struct lsquic_packno_range ranges[256]; 35}; 36 37#define largest_acked(acki) (+(acki)->ranges[0].high) 38 39#define smallest_acked(acki) (+(acki)->ranges[(acki)->n_ranges - 1].low) 40 41/* Chrome may send an empty ACK frame when it closes a connection. 42 * We do not know why it occurs -- perhaps a bug in Chrome. 43 */ 44/* This macro cannot be used in IETF QUIC as zero is a valid packet number. 45 * Hopefully the Chrome bug will have been fixed by then. 46 */ 47#define empty_ack_frame(acki) (largest_acked(acki) == 0) 48 49/* gaf_: generate ACK frame */ 50struct lsquic_packno_range; 51typedef const struct lsquic_packno_range * 52 (*gaf_rechist_first_f) (void *rechist); 53typedef const struct lsquic_packno_range * 54 (*gaf_rechist_next_f) (void *rechist); 55typedef lsquic_time_t 56 (*gaf_rechist_largest_recv_f) (void *rechist); 57 58/* gsf_: generate stream frame */ 59typedef size_t (*gsf_read_f) (void *stream, void *buf, size_t len, int *fin); 60 61/* This structure contains functions that parse and generate packets and 62 * frames in version-specific manner. To begin with, there is difference 63 * between GQUIC's little-endian (Q038 and lower) and big-endian formats 64 * (Q039 and higher). Q046 and higher uses different format for packet headers. 65 */ 66struct parse_funcs 67{ 68 /* Return buf length */ 69 int 70 (*pf_gen_reg_pkt_header) (const struct lsquic_conn *, 71 const struct lsquic_packet_out *, unsigned char *, size_t, 72 /* In Q050 and IETF QUIC, these are set: */ 73 unsigned *packno_off, unsigned *packno_len); 74 void 75 (*pf_parse_packet_in_finish) (struct lsquic_packet_in *packet_in, 76 struct packin_parse_state *); 77 enum quic_frame_type 78 (*pf_parse_frame_type) (const unsigned char *, size_t); 79 /* Return used buffer length or a negative value if there was not enough 80 * room to write the stream frame. In the latter case, the negative of 81 * the negative return value is the number of bytes required. The 82 * exception is -1, which is a generic error code, as we always need 83 * more than 1 byte to write a STREAM frame. 84 */ 85 /* pf_gen_stream_frame and pf_gen_crypto_frame must be adjacent so that 86 * they can be cast to an array. 87 */ 88 int 89 (*pf_gen_stream_frame) (unsigned char *buf, size_t bufsz, 90 lsquic_stream_id_t stream_id, uint64_t offset, 91 int fin, size_t size, gsf_read_f, void *stream); 92 /* The two "UNUSED" parameters are here so that it matches 93 * pf_gen_stream_frame. 94 */ 95 int 96 (*pf_gen_crypto_frame) (unsigned char *buf, size_t bufsz, 97 lsquic_stream_id_t UNUSED_1, uint64_t offset, 98 int UNUSED_2, size_t size, gsf_read_f, void *stream); 99 /* pf_parse_stream_frame and pf_parse_crypto_frame must be adjacent so that 100 * they can be cast to an array. 101 */ 102 int 103 (*pf_parse_stream_frame) (const unsigned char *buf, size_t rem_packet_sz, 104 struct stream_frame *); 105 int 106 (*pf_parse_crypto_frame) (const unsigned char *buf, size_t rem_packet_sz, 107 struct stream_frame *); 108 /* Return true if STREAM frame extends to the end of the packet and thus 109 * does not contain a Length field (no update). 110 */ 111 int 112 (*pf_dec_stream_frame_size) (unsigned char *buf, size_t new_size); 113 int 114 (*pf_parse_ack_frame) (const unsigned char *buf, size_t buf_len, 115 struct ack_info *ack_info, uint8_t exp); 116 int 117 (*pf_gen_ack_frame) (unsigned char *outbuf, size_t outbuf_sz, 118 gaf_rechist_first_f, gaf_rechist_next_f, 119 gaf_rechist_largest_recv_f, void *rechist, lsquic_time_t now, 120 int *has_missing, lsquic_packno_t *largest_received, 121 const uint64_t *ecn_counts); 122 int 123 (*pf_gen_stop_waiting_frame) (unsigned char *buf, size_t buf_len, 124 lsquic_packno_t cur_packno, enum packno_bits, 125 lsquic_packno_t least_unacked_packno); 126 int 127 (*pf_parse_stop_waiting_frame) (const unsigned char *buf, size_t buf_len, 128 lsquic_packno_t cur_packno, enum packno_bits, 129 lsquic_packno_t *least_unacked); 130 int 131 (*pf_skip_stop_waiting_frame) (size_t buf_len, enum packno_bits); 132 int 133 (*pf_gen_window_update_frame) (unsigned char *buf, int buf_len, 134 lsquic_stream_id_t stream_id, uint64_t offset); 135 int 136 (*pf_parse_window_update_frame) (const unsigned char *buf, size_t buf_len, 137 lsquic_stream_id_t *stream_id, uint64_t *offset); 138 /* The third argument for pf_gen_blocked_frame() and pf_parse_blocked_frame() 139 * is Stream ID for GQUIC and offset for IETF QUIC. Since both of these are 140 * uint64_t, we'll use the same function pointer. Just have to be a little 141 * careful here. 142 */ 143 int 144 (*pf_gen_blocked_frame) (unsigned char *buf, size_t buf_len, 145 lsquic_stream_id_t stream_id); 146 int 147 (*pf_parse_blocked_frame) (const unsigned char *buf, size_t buf_len, 148 /* TODO: rename third argument when dropping GQUIC */ 149 lsquic_stream_id_t *stream_id); 150 unsigned 151 (*pf_blocked_frame_size) (uint64_t); 152 unsigned 153 (*pf_rst_frame_size) (lsquic_stream_id_t stream_id, uint64_t final_size, 154 uint64_t error_code); 155 int 156 (*pf_gen_rst_frame) (unsigned char *buf, size_t buf_len, 157 lsquic_stream_id_t stream_id, uint64_t offset, uint64_t error_code); 158 int 159 (*pf_parse_rst_frame) (const unsigned char *buf, size_t buf_len, 160 lsquic_stream_id_t *stream_id, uint64_t *offset, uint64_t *error_code); 161 int 162 (*pf_parse_stop_sending_frame) (const unsigned char *buf, size_t buf_len, 163 lsquic_stream_id_t *stream_id, uint64_t *error_code); 164 unsigned 165 (*pf_stop_sending_frame_size) (lsquic_stream_id_t, uint64_t); 166 int 167 (*pf_gen_stop_sending_frame) (unsigned char *buf, size_t buf_len, 168 lsquic_stream_id_t, uint64_t error_code); 169 size_t 170 (*pf_connect_close_frame_size) (int app_error, unsigned error_code, 171 unsigned frame_type, size_t reason_len); 172 int 173 (*pf_gen_connect_close_frame) (unsigned char *buf, size_t buf_len, 174 int app_error, unsigned error_code, const char *reason, int reason_len); 175 int 176 (*pf_parse_connect_close_frame) (const unsigned char *buf, size_t buf_len, 177 int *app_error, uint64_t *error_code, uint16_t *reason_length, 178 uint8_t *reason_offset); 179 int 180 (*pf_gen_goaway_frame) (unsigned char *buf, size_t buf_len, 181 uint32_t error_code, lsquic_stream_id_t last_good_stream_id, 182 const char *reason, size_t reason_len); 183 int 184 (*pf_parse_goaway_frame) (const unsigned char *buf, size_t buf_len, 185 uint32_t *error_code, lsquic_stream_id_t *last_good_stream_id, 186 uint16_t *reason_length, const char **reason); 187 int 188 (*pf_gen_ping_frame) (unsigned char *buf, int buf_len); 189 int 190 (*pf_parse_path_chal_frame) (const unsigned char *buf, size_t, 191 uint64_t *chal); 192 int 193 (*pf_parse_path_resp_frame) (const unsigned char *buf, size_t, 194 uint64_t *resp); 195#ifndef NDEBUG 196 /* These float reading and writing functions assume `mem' has at least 197 * 2 bytes. 198 */ 199 void 200 (*pf_write_float_time16) (lsquic_time_t time_us, void *mem); 201 uint64_t 202 (*pf_read_float_time16) (const void *mem); 203#endif 204 ssize_t 205 (*pf_generate_simple_prst) (const lsquic_cid_t *cid, 206 unsigned char *, size_t); 207 size_t 208 (*pf_calc_stream_frame_header_sz) (lsquic_stream_id_t stream_id, 209 uint64_t offset, unsigned data_sz); 210 size_t 211 (*pf_calc_crypto_frame_header_sz) (uint64_t offset, unsigned data_sz); 212 void 213 (*pf_turn_on_fin) (unsigned char *); 214 215 size_t 216 (*pf_packout_size) (const struct lsquic_conn *, 217 const struct lsquic_packet_out *); 218 219 /* This returns the high estimate of the header size. Note that it 220 * cannot account for the size of the token in the IETF QUIC Initial 221 * packets as it does not know it. 222 */ 223 size_t 224 (*pf_packout_max_header_size) (const struct lsquic_conn *, 225 enum packet_out_flags, size_t dcid_len, enum header_type); 226 227 enum packno_bits 228 (*pf_calc_packno_bits) (lsquic_packno_t packno, 229 lsquic_packno_t least_unacked, uint64_t n_in_flight); 230 unsigned 231 (*pf_packno_bits2len) (enum packno_bits); 232 233 int 234 (*pf_parse_max_data) (const unsigned char *, size_t, uint64_t *); 235 int 236 (*pf_gen_max_data_frame) (unsigned char *, size_t, uint64_t); 237 unsigned 238 (*pf_max_data_frame_size) (uint64_t); 239 /* 240 * Returns number of bytes parsed on success or negative value on error: 241 * -1 Out of input buffer 242 * -2 Invalid CID length value 243 */ 244 int 245 (*pf_parse_new_conn_id) (const unsigned char *, size_t, uint64_t *, 246 uint64_t *, lsquic_cid_t *, const unsigned char **); 247 unsigned 248 (*pf_stream_blocked_frame_size) (lsquic_stream_id_t, uint64_t); 249 int 250 (*pf_gen_stream_blocked_frame) (unsigned char *buf, size_t, 251 lsquic_stream_id_t, uint64_t); 252 int 253 (*pf_parse_stream_blocked_frame) (const unsigned char *buf, size_t, 254 lsquic_stream_id_t *, uint64_t *); 255 unsigned 256 (*pf_max_stream_data_frame_size) (lsquic_stream_id_t, uint64_t); 257 int 258 (*pf_gen_max_stream_data_frame) (unsigned char *buf, size_t, 259 lsquic_stream_id_t, uint64_t); 260 int 261 (*pf_parse_max_stream_data_frame) (const unsigned char *buf, size_t, 262 lsquic_stream_id_t *, uint64_t *); 263 int 264 (*pf_parse_new_token_frame) (const unsigned char *buf, size_t, 265 const unsigned char **token, size_t *token_size); 266 size_t 267 (*pf_new_connection_id_frame_size) (unsigned seqno, unsigned cid_len); 268 int 269 (*pf_gen_new_connection_id_frame) (unsigned char *buf, size_t, 270 unsigned seqno, const struct lsquic_cid *, 271 const unsigned char *token, size_t); 272 size_t 273 (*pf_retire_cid_frame_size) (uint64_t); 274 int 275 (*pf_gen_retire_cid_frame) (unsigned char *buf, size_t, uint64_t); 276 int 277 (*pf_parse_retire_cid_frame) (const unsigned char *buf, size_t, uint64_t *); 278 size_t 279 (*pf_new_token_frame_size) (size_t); 280 int 281 (*pf_gen_new_token_frame) (unsigned char *buf, size_t, 282 const unsigned char *token, size_t); 283 int 284 (*pf_gen_streams_blocked_frame) (unsigned char *buf, size_t buf_len, 285 enum stream_dir, uint64_t); 286 int 287 (*pf_parse_streams_blocked_frame) (const unsigned char *buf, size_t buf_len, 288 enum stream_dir *, uint64_t *); 289 unsigned 290 (*pf_streams_blocked_frame_size) (uint64_t); 291 int 292 (*pf_gen_max_streams_frame) (unsigned char *buf, size_t buf_len, 293 enum stream_dir, uint64_t); 294 int 295 (*pf_parse_max_streams_frame) (const unsigned char *buf, size_t buf_len, 296 enum stream_dir *, uint64_t *); 297 unsigned 298 (*pf_max_streams_frame_size) (uint64_t); 299 unsigned 300 (*pf_path_chal_frame_size) (void); 301 int 302 (*pf_gen_path_chal_frame) (unsigned char *, size_t, uint64_t chal); 303 unsigned 304 (*pf_path_resp_frame_size) (void); 305 int 306 (*pf_gen_path_resp_frame) (unsigned char *, size_t, uint64_t resp); 307 int 308 (*pf_gen_handshake_done_frame) (unsigned char *buf, size_t buf_len); 309 int 310 (*pf_parse_handshake_done_frame) (const unsigned char *buf, size_t buf_len); 311 unsigned 312 (*pf_handshake_done_frame_size) (void); 313 int 314 (*pf_gen_ack_frequency_frame) (unsigned char *buf, size_t buf_len, 315 uint64_t seqno, uint64_t pack_tol, uint64_t upd_mad, int ignore); 316 int 317 (*pf_parse_ack_frequency_frame) (const unsigned char *buf, size_t buf_len, 318 uint64_t *seqno, uint64_t *pack_tol, uint64_t *upd_mad, int *ignore); 319 unsigned 320 (*pf_ack_frequency_frame_size) (uint64_t seqno, uint64_t pack_tol, 321 uint64_t upd_mad /* Don't need to pass `ignore' */); 322 int 323 (*pf_gen_timestamp_frame) (unsigned char *buf, size_t buf_len, uint64_t); 324 int 325 (*pf_parse_timestamp_frame) (const unsigned char *buf, size_t, uint64_t *); 326 int 327 (*pf_parse_datagram_frame) (const unsigned char *buf, size_t, const void **, 328 size_t *); 329 int 330 (*pf_gen_datagram_frame) (unsigned char *, size_t bufsz, size_t min_sz, 331 size_t max_sz, ssize_t (*)(struct lsquic_conn *, void *, size_t), 332 struct lsquic_conn *); 333 unsigned 334 (*pf_datagram_frame_size) (size_t); 335}; 336 337 338extern const struct parse_funcs lsquic_parse_funcs_gquic_Q043; 339extern const struct parse_funcs lsquic_parse_funcs_gquic_Q046; 340extern const struct parse_funcs lsquic_parse_funcs_gquic_Q050; 341extern const struct parse_funcs lsquic_parse_funcs_ietf_v1; 342 343#define select_pf_by_ver(ver) ( \ 344 (1 << (ver)) & (1 << LSQVER_043) ? \ 345 &lsquic_parse_funcs_gquic_Q043 : \ 346 (1 << (ver)) & (1 << LSQVER_046) ? \ 347 &lsquic_parse_funcs_gquic_Q046 : \ 348 (1 << (ver)) & ((1 << LSQVER_050)|LSQUIC_EXPERIMENTAL_Q098) ? \ 349 &lsquic_parse_funcs_gquic_Q050 : \ 350 &lsquic_parse_funcs_ietf_v1) 351 352/* This function is gQUIC-version independent */ 353int 354lsquic_gquic_parse_packet_in_begin (struct lsquic_packet_in *, size_t length, 355 int is_server, unsigned cid_len, struct packin_parse_state *); 356 357int 358lsquic_Q046_parse_packet_in_short_begin (struct lsquic_packet_in *, size_t length, 359 int is_server, unsigned, struct packin_parse_state *); 360 361int 362lsquic_Q046_parse_packet_in_long_begin (struct lsquic_packet_in *, size_t length, 363 int is_server, unsigned, struct packin_parse_state *); 364 365int 366lsquic_Q050_parse_packet_in_long_begin (struct lsquic_packet_in *, size_t length, 367 int is_server, unsigned, struct packin_parse_state *); 368 369enum quic_frame_type 370lsquic_parse_frame_type_gquic_Q035_thru_Q046 (const unsigned char *, size_t); 371 372extern const enum quic_frame_type lsquic_iquic_byte2type[0x40]; 373 374size_t 375lsquic_calc_stream_frame_header_sz_gquic (lsquic_stream_id_t stream_id, 376 uint64_t offset, unsigned); 377 378size_t 379lsquic_gquic_packout_size (const struct lsquic_conn *, 380 const struct lsquic_packet_out *); 381 382size_t 383lsquic_gquic_packout_header_size (const struct lsquic_conn *conn, 384 enum packet_out_flags flags, size_t unused, enum header_type); 385 386size_t 387lsquic_gquic_po_header_sz (enum packet_out_flags flags); 388 389size_t 390lsquic_gquic_packout_size (const struct lsquic_conn *, 391 const struct lsquic_packet_out *); 392 393size_t 394lsquic_gquic_po_header_sz (enum packet_out_flags flags); 395 396/* This maps two bits as follows: 397 * 00 -> 1 398 * 01 -> 2 399 * 10 -> 4 400 * 11 -> 6 401 * 402 * Assumes that only two low bits are set. 403 */ 404#define twobit_to_1246(bits) ((bits) * 2 + !(bits)) 405 406/* This maps two bits as follows: 407 * 00 -> 1 408 * 01 -> 2 409 * 10 -> 4 410 * 11 -> 8 411 * 412 * Assumes that only two low bits are set. 413 */ 414#define twobit_to_1248(bits) (1 << (bits)) 415 416#define ECN_COUNTS_STR " ECT(0): 01234567879012345678790;" \ 417 " ECT(1): 01234567879012345678790;" \ 418 " CE: 01234567879012345678790" 419#define RANGES_TRUNCATED_STR " ranges truncated! " 420 421#define MAX_ACKI_STR_SZ (256 * (3 /* [-] */ + 20 /* ~0ULL */ * 2) \ 422 + sizeof(ECN_COUNTS_STR) + sizeof(RANGES_TRUNCATED_STR)) 423 424void 425lsquic_acki2str (const struct ack_info *acki, char *, size_t); 426 427void 428lsquic_turn_on_fin_Q035_thru_Q046 (unsigned char *); 429 430enum packno_bits 431lsquic_gquic_calc_packno_bits (lsquic_packno_t packno, 432 lsquic_packno_t least_unacked, uint64_t n_in_flight); 433 434unsigned 435lsquic_gquic_packno_bits2len (enum packno_bits); 436 437int 438lsquic_merge_acks (struct ack_info *dst, const struct ack_info *src); 439 440#endif 441