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