lsquic_parse.h revision feca77f5
1/* Copyright (c) 2017 - 2020 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/* gcf_: generate CRYPTO frame */ 62typedef size_t (*gcf_read_f) (void *stream, void *buf, size_t len); 63 64/* This structure contains functions that parse and generate packets and 65 * frames in version-specific manner. To begin with, there is difference 66 * between GQUIC's little-endian (Q038 and lower) and big-endian formats 67 * (Q039 and higher). Q046 and higher uses different format for packet headers. 68 */ 69struct parse_funcs 70{ 71 /* Return buf length */ 72 int 73 (*pf_gen_reg_pkt_header) (const struct lsquic_conn *, 74 const struct lsquic_packet_out *, unsigned char *, size_t); 75 void 76 (*pf_parse_packet_in_finish) (struct lsquic_packet_in *packet_in, 77 struct packin_parse_state *); 78 enum quic_frame_type 79 (*pf_parse_frame_type) (const unsigned char *, size_t); 80 /* Return used buffer length or a negative value if there was not enough 81 * room to write the stream frame. In the latter case, the negative of 82 * the negative return value is the number of bytes required. The 83 * exception is -1, which is a generic error code, as we always need 84 * more than 1 byte to write a STREAM frame. 85 */ 86 int 87 (*pf_gen_stream_frame) (unsigned char *buf, size_t bufsz, 88 lsquic_stream_id_t stream_id, uint64_t offset, 89 int fin, size_t size, gsf_read_f, void *stream); 90 int 91 (*pf_parse_stream_frame) (const unsigned char *buf, size_t rem_packet_sz, 92 struct stream_frame *); 93 int 94 (*pf_parse_crypto_frame) (const unsigned char *buf, size_t rem_packet_sz, 95 struct stream_frame *); 96 int 97 (*pf_gen_crypto_frame) (unsigned char *buf, size_t bufsz, uint64_t offset, 98 size_t size, gcf_read_f, void *stream); 99 int 100 (*pf_parse_ack_frame) (const unsigned char *buf, size_t buf_len, 101 struct ack_info *ack_info, uint8_t exp); 102 int 103 (*pf_gen_ack_frame) (unsigned char *outbuf, size_t outbuf_sz, 104 gaf_rechist_first_f, gaf_rechist_next_f, 105 gaf_rechist_largest_recv_f, void *rechist, lsquic_time_t now, 106 int *has_missing, lsquic_packno_t *largest_received, 107 const uint64_t *ecn_counts); 108 int 109 (*pf_gen_stop_waiting_frame) (unsigned char *buf, size_t buf_len, 110 lsquic_packno_t cur_packno, enum packno_bits, 111 lsquic_packno_t least_unacked_packno); 112 int 113 (*pf_parse_stop_waiting_frame) (const unsigned char *buf, size_t buf_len, 114 lsquic_packno_t cur_packno, enum packno_bits, 115 lsquic_packno_t *least_unacked); 116 int 117 (*pf_skip_stop_waiting_frame) (size_t buf_len, enum packno_bits); 118 int 119 (*pf_gen_window_update_frame) (unsigned char *buf, int buf_len, 120 lsquic_stream_id_t stream_id, uint64_t offset); 121 int 122 (*pf_parse_window_update_frame) (const unsigned char *buf, size_t buf_len, 123 lsquic_stream_id_t *stream_id, uint64_t *offset); 124 /* The third argument for pf_gen_blocked_frame() and pf_parse_blocked_frame() 125 * is Stream ID for GQUIC and offset for IETF QUIC. Since both of these are 126 * uint64_t, we'll use the same function pointer. Just have to be a little 127 * careful here. 128 */ 129 int 130 (*pf_gen_blocked_frame) (unsigned char *buf, size_t buf_len, 131 lsquic_stream_id_t stream_id); 132 int 133 (*pf_parse_blocked_frame) (const unsigned char *buf, size_t buf_len, 134 /* TODO: rename third argument when dropping GQUIC */ 135 lsquic_stream_id_t *stream_id); 136 unsigned 137 (*pf_blocked_frame_size) (uint64_t); 138 unsigned 139 (*pf_rst_frame_size) (lsquic_stream_id_t stream_id, uint64_t final_size, 140 uint64_t error_code); 141 int 142 (*pf_gen_rst_frame) (unsigned char *buf, size_t buf_len, 143 lsquic_stream_id_t stream_id, uint64_t offset, uint64_t error_code); 144 int 145 (*pf_parse_rst_frame) (const unsigned char *buf, size_t buf_len, 146 lsquic_stream_id_t *stream_id, uint64_t *offset, uint64_t *error_code); 147 int 148 (*pf_parse_stop_sending_frame) (const unsigned char *buf, size_t buf_len, 149 lsquic_stream_id_t *stream_id, uint64_t *error_code); 150 unsigned 151 (*pf_stop_sending_frame_size) (lsquic_stream_id_t, uint64_t); 152 int 153 (*pf_gen_stop_sending_frame) (unsigned char *buf, size_t buf_len, 154 lsquic_stream_id_t, uint64_t error_code); 155 size_t 156 (*pf_connect_close_frame_size) (int app_error, unsigned error_code, 157 unsigned frame_type, size_t reason_len); 158 int 159 (*pf_gen_connect_close_frame) (unsigned char *buf, size_t buf_len, 160 int app_error, unsigned error_code, const char *reason, int reason_len); 161 int 162 (*pf_parse_connect_close_frame) (const unsigned char *buf, size_t buf_len, 163 int *app_error, uint64_t *error_code, uint16_t *reason_length, 164 uint8_t *reason_offset); 165 int 166 (*pf_gen_goaway_frame) (unsigned char *buf, size_t buf_len, 167 uint32_t error_code, lsquic_stream_id_t last_good_stream_id, 168 const char *reason, size_t reason_len); 169 int 170 (*pf_parse_goaway_frame) (const unsigned char *buf, size_t buf_len, 171 uint32_t *error_code, lsquic_stream_id_t *last_good_stream_id, 172 uint16_t *reason_length, const char **reason); 173 int 174 (*pf_gen_ping_frame) (unsigned char *buf, int buf_len); 175 int 176 (*pf_parse_path_chal_frame) (const unsigned char *buf, size_t, 177 uint64_t *chal); 178 int 179 (*pf_parse_path_resp_frame) (const unsigned char *buf, size_t, 180 uint64_t *resp); 181#ifndef NDEBUG 182 /* These float reading and writing functions assume `mem' has at least 183 * 2 bytes. 184 */ 185 void 186 (*pf_write_float_time16) (lsquic_time_t time_us, void *mem); 187 uint64_t 188 (*pf_read_float_time16) (const void *mem); 189#endif 190 ssize_t 191 (*pf_generate_simple_prst) (const lsquic_cid_t *cid, 192 unsigned char *, size_t); 193 size_t 194 (*pf_calc_stream_frame_header_sz) (lsquic_stream_id_t stream_id, 195 uint64_t offset, unsigned data_sz); 196 size_t 197 (*pf_calc_crypto_frame_header_sz) (uint64_t offset, unsigned data_sz); 198 void 199 (*pf_turn_on_fin) (unsigned char *); 200 201 size_t 202 (*pf_packout_size) (const struct lsquic_conn *, 203 const struct lsquic_packet_out *); 204 205 /* This returns the high estimate of the header size. Note that it 206 * cannot account for the size of the token in the IETF QUIC Initial 207 * packets as it does not know it. 208 */ 209 size_t 210 (*pf_packout_max_header_size) (const struct lsquic_conn *, 211 enum packet_out_flags, size_t dcid_len); 212 213 enum packno_bits 214 (*pf_calc_packno_bits) (lsquic_packno_t packno, 215 lsquic_packno_t least_unacked, uint64_t n_in_flight); 216 unsigned 217 (*pf_packno_bits2len) (enum packno_bits); 218 219 /* Used by IETF QUIC and gQUIC >= Q050 */ 220 void 221 (*pf_packno_info) (const struct lsquic_conn *, 222 const struct lsquic_packet_out *, unsigned *packno_off, 223 unsigned *packno_len); 224 int 225 (*pf_parse_max_data) (const unsigned char *, size_t, uint64_t *); 226 int 227 (*pf_gen_max_data_frame) (unsigned char *, size_t, uint64_t); 228 unsigned 229 (*pf_max_data_frame_size) (uint64_t); 230 /* 231 * Returns number of bytes parsed on success or negative value on error: 232 * -1 Out of input buffer 233 * -2 Invalid CID length value 234 */ 235 int 236 (*pf_parse_new_conn_id) (const unsigned char *, size_t, uint64_t *, 237 uint64_t *, lsquic_cid_t *, const unsigned char **); 238 unsigned 239 (*pf_stream_blocked_frame_size) (lsquic_stream_id_t, uint64_t); 240 int 241 (*pf_gen_stream_blocked_frame) (unsigned char *buf, size_t, 242 lsquic_stream_id_t, uint64_t); 243 int 244 (*pf_parse_stream_blocked_frame) (const unsigned char *buf, size_t, 245 lsquic_stream_id_t *, uint64_t *); 246 unsigned 247 (*pf_max_stream_data_frame_size) (lsquic_stream_id_t, uint64_t); 248 int 249 (*pf_gen_max_stream_data_frame) (unsigned char *buf, size_t, 250 lsquic_stream_id_t, uint64_t); 251 int 252 (*pf_parse_max_stream_data_frame) (const unsigned char *buf, size_t, 253 lsquic_stream_id_t *, uint64_t *); 254 int 255 (*pf_parse_new_token_frame) (const unsigned char *buf, size_t, 256 const unsigned char **token, size_t *token_size); 257 size_t 258 (*pf_new_connection_id_frame_size) (unsigned seqno, unsigned cid_len); 259 int 260 (*pf_gen_new_connection_id_frame) (unsigned char *buf, size_t, 261 unsigned seqno, const struct lsquic_cid *, 262 const unsigned char *token, size_t); 263 size_t 264 (*pf_retire_cid_frame_size) (uint64_t); 265 int 266 (*pf_gen_retire_cid_frame) (unsigned char *buf, size_t, uint64_t); 267 int 268 (*pf_parse_retire_cid_frame) (const unsigned char *buf, size_t, uint64_t *); 269 size_t 270 (*pf_new_token_frame_size) (size_t); 271 int 272 (*pf_gen_new_token_frame) (unsigned char *buf, size_t, 273 const unsigned char *token, size_t); 274 int 275 (*pf_gen_streams_blocked_frame) (unsigned char *buf, size_t buf_len, 276 enum stream_dir, uint64_t); 277 int 278 (*pf_parse_streams_blocked_frame) (const unsigned char *buf, size_t buf_len, 279 enum stream_dir *, uint64_t *); 280 unsigned 281 (*pf_streams_blocked_frame_size) (uint64_t); 282 int 283 (*pf_gen_max_streams_frame) (unsigned char *buf, size_t buf_len, 284 enum stream_dir, uint64_t); 285 int 286 (*pf_parse_max_streams_frame) (const unsigned char *buf, size_t buf_len, 287 enum stream_dir *, uint64_t *); 288 unsigned 289 (*pf_max_streams_frame_size) (uint64_t); 290 unsigned 291 (*pf_path_chal_frame_size) (void); 292 int 293 (*pf_gen_path_chal_frame) (unsigned char *, size_t, uint64_t chal); 294 unsigned 295 (*pf_path_resp_frame_size) (void); 296 int 297 (*pf_gen_path_resp_frame) (unsigned char *, size_t, uint64_t resp); 298 int 299 (*pf_gen_handshake_done_frame) (unsigned char *buf, size_t buf_len); 300 int 301 (*pf_parse_handshake_done_frame) (const unsigned char *buf, size_t buf_len); 302 unsigned 303 (*pf_handshake_done_frame_size) (void); 304 int 305 (*pf_gen_ack_frequency_frame) (unsigned char *buf, size_t buf_len, 306 uint64_t seqno, uint64_t pack_tol, uint64_t upd_mad); 307 int 308 (*pf_parse_ack_frequency_frame) (const unsigned char *buf, size_t buf_len, 309 uint64_t *seqno, uint64_t *pack_tol, uint64_t *upd_mad); 310 unsigned 311 (*pf_ack_frequency_frame_size) (uint64_t seqno, uint64_t pack_tol, 312 uint64_t upd_mad); 313}; 314 315 316extern const struct parse_funcs lsquic_parse_funcs_gquic_Q043; 317extern const struct parse_funcs lsquic_parse_funcs_gquic_Q046; 318extern const struct parse_funcs lsquic_parse_funcs_gquic_Q050; 319extern const struct parse_funcs lsquic_parse_funcs_ietf_id24; 320extern const struct parse_funcs lsquic_parse_funcs_ietf_v1; 321 322#define select_pf_by_ver(ver) ( \ 323 (1 << (ver)) & (1 << LSQVER_043) ? \ 324 &lsquic_parse_funcs_gquic_Q043 : \ 325 (1 << (ver)) & (1 << LSQVER_046) ? \ 326 &lsquic_parse_funcs_gquic_Q046 : \ 327 (1 << (ver)) & ((1 << LSQVER_050)|LSQUIC_EXPERIMENTAL_Q098) ? \ 328 &lsquic_parse_funcs_gquic_Q050 : \ 329 (1 << (ver)) & (1 << LSQVER_ID24) ? \ 330 &lsquic_parse_funcs_ietf_id24 : \ 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 (const unsigned char *, size_t); 352 353extern const enum quic_frame_type lsquic_iquic_byte2type[0x40]; 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 397#define ECN_COUNTS_STR " ECT(0): 01234567879012345678790;" \ 398 " ECT(1): 01234567879012345678790;" \ 399 " CE: 01234567879012345678790" 400#define RANGES_TRUNCATED_STR " ranges truncated! " 401 402#define MAX_ACKI_STR_SZ (256 * (3 /* [-] */ + 20 /* ~0ULL */ * 2) \ 403 + sizeof(ECN_COUNTS_STR) + sizeof(RANGES_TRUNCATED_STR)) 404 405void 406lsquic_acki2str (const struct ack_info *acki, char *, size_t); 407 408void 409lsquic_turn_on_fin_Q035_thru_Q046 (unsigned char *); 410 411enum packno_bits 412lsquic_gquic_calc_packno_bits (lsquic_packno_t packno, 413 lsquic_packno_t least_unacked, uint64_t n_in_flight); 414 415unsigned 416lsquic_gquic_packno_bits2len (enum packno_bits); 417 418int 419lsquic_merge_acks (struct ack_info *dst, const struct ack_info *src); 420 421#endif 422