lsquic_mini_conn.c revision eea99896
1/* Copyright (c) 2017 - 2021 LiteSpeed Technologies Inc. See LICENSE. */ 2/* 3 * lsquic_mini_conn.c -- Mini connection. 4 * 5 * Mini connection is only used in server mode -- this assumption is relied 6 * upon by the code in this file. 7 * 8 * The purpose of this connection is to process incoming handshakes using 9 * minimal amount of resources until we confirm that the client is sending 10 * valid data. Here, we only process Stream 1 data; other packets are 11 * spooled, if necessary. When mini connection is promoted to full 12 * connection, the state, including spooled incoming packets, is transferred 13 * to the full connection. 14 */ 15 16 17#include <assert.h> 18#include <errno.h> 19#include <inttypes.h> 20#include <limits.h> 21#include <stdlib.h> 22#include <string.h> 23#include <sys/queue.h> 24#include <time.h> 25 26#include "lsquic.h" 27#include "lsquic_int_types.h" 28#include "lsquic_hash.h" 29#include "lsquic_conn.h" 30#include "lsquic_rtt.h" 31#include "lsquic_mini_conn.h" 32#include "lsquic_mm.h" 33#include "lsquic_malo.h" 34#include "lsquic_packet_common.h" 35#include "lsquic_packet_gquic.h" 36#include "lsquic_packet_ietf.h" 37#include "lsquic_packet_in.h" 38#include "lsquic_packet_out.h" 39#include "lsquic_util.h" 40#include "lsquic_str.h" 41#include "lsquic_enc_sess.h" 42#include "lsquic_parse.h" 43#include "lsquic_engine_public.h" 44#include "lsquic_sfcw.h" 45#include "lsquic_varint.h" 46#include "lsquic_hq.h" 47#include "lsquic_stream.h" 48#include "lsquic_rechist.h" 49#include "lsquic_ev_log.h" 50#include "lsquic_qtags.h" 51#include "lsquic_attq.h" 52#include "lsquic_alarmset.h" 53 54#define LSQUIC_LOGGER_MODULE LSQLM_MINI_CONN 55#define LSQUIC_LOG_CONN_ID lsquic_conn_log_cid(&mc->mc_conn) 56#include "lsquic_logger.h" 57 58 59static const struct conn_iface mini_conn_iface_standard; 60static const struct conn_iface mini_conn_iface_standard_Q050; 61 62#if LSQUIC_KEEP_MINICONN_HISTORY 63 64static void 65mchist_append (struct mini_conn *mc, enum miniconn_history_event mh_event) 66{ 67 enum miniconn_history_event prev_event; 68 mchist_idx_t idx; 69 int plus; 70 71 idx = (mc->mc_hist_idx - 1) & MCHIST_MASK; 72 plus = MCHE_PLUS == mc->mc_hist_buf[ idx ]; 73 idx = (idx - plus) & MCHIST_MASK; 74 prev_event = mc->mc_hist_buf[ idx ]; 75 76 if (!(prev_event == mh_event && plus)) 77 { 78 if (prev_event == mh_event) 79 mh_event = MCHE_PLUS; 80 mc->mc_hist_buf[ MCHIST_MASK & mc->mc_hist_idx++ ] = mh_event; 81 } 82} 83 84 85# define MCHIST_APPEND(mc, event) mchist_append(mc, event) 86#else 87# define MCHIST_APPEND(mc, event) 88#endif 89 90static void 91process_deferred_packets (struct mini_conn *mc); 92 93 94/* If this is not true, highest_bit_set() may be broken */ 95typedef char packno_set_is_unsigned_long[ 96 sizeof(unsigned long long) == sizeof(mconn_packno_set_t) ? 1 : -1 ]; 97 98static unsigned 99highest_bit_set (unsigned long long sz) 100{ 101#if __GNUC__ 102 unsigned clz = __builtin_clzll(sz); 103 return 63 - clz; 104#else 105 unsigned long y; 106 unsigned n; 107 n = 64; 108 y = sz >> 32; if (y) { n -= 32; sz = y; } 109 y = sz >> 16; if (y) { n -= 16; sz = y; } 110 y = sz >> 8; if (y) { n -= 8; sz = y; } 111 y = sz >> 4; if (y) { n -= 4; sz = y; } 112 y = sz >> 2; if (y) { n -= 2; sz = y; } 113 y = sz >> 1; if (y) return 63 - n + 2; 114 return 63 - n + sz; 115#endif 116} 117 118 119static unsigned 120lowest_bit_set (unsigned v) 121{ 122#if __GNUC__ 123 return __builtin_ctz(v); 124#else 125 unsigned n; 126 n = 0; 127 if (0 == (v & ((1 << 16) - 1))) { n += 16; v >>= 16; } 128 if (0 == (v & ((1 << 8) - 1))) { n += 8; v >>= 8; } 129 if (0 == (v & ((1 << 4) - 1))) { n += 4; v >>= 4; } 130 if (0 == (v & ((1 << 2) - 1))) { n += 2; v >>= 2; } 131 if (0 == (v & ((1 << 1) - 1))) { n += 1; } 132 return n; 133#endif 134} 135 136 137static int 138is_handshake_stream_id (const struct mini_conn *conn, 139 lsquic_stream_id_t stream_id) 140{ 141 return conn->mc_conn.cn_version < LSQVER_050 && stream_id == 1; 142} 143 144 145static void 146mini_destroy_packet (struct mini_conn *mc, struct lsquic_packet_out *packet_out) 147{ 148 lsquic_packet_out_destroy(packet_out, mc->mc_enpub, 149 mc->mc_path.np_peer_ctx); 150} 151 152 153static int 154packet_in_is_ok (enum lsquic_version version, 155 const struct lsquic_packet_in *packet_in) 156{ 157 size_t min_size; 158 159 if (packet_in->pi_data_sz > GQUIC_MAX_PACKET_SZ) 160 { 161 LSQ_LOG1(LSQ_LOG_DEBUG, "incoming packet too large: %hu bytes", 162 packet_in->pi_data_sz); 163 return 0; 164 } 165 166 if ((1 << version) & LSQUIC_GQUIC_HEADER_VERSIONS) 167 /* This is a very lax number, it allows the server to send 168 * 64 * 200 = 12KB of output (REJ and SHLO). 169 */ 170 min_size = 200; 171 else 172 /* Chrome enforces 1200-byte minimum initial packet limit */ 173 min_size = IQUIC_MIN_INIT_PACKET_SZ; 174 175 if (packet_in->pi_data_sz < min_size) 176 { 177 LSQ_LOG1(LSQ_LOG_DEBUG, "incoming packet too small: %hu bytes", 178 packet_in->pi_data_sz); 179 return 0; 180 } 181 return 1; 182} 183 184 185lsquic_conn_t * 186lsquic_mini_conn_new (struct lsquic_engine_public *enp, 187 const struct lsquic_packet_in *packet_in, 188 enum lsquic_version version) 189{ 190 struct mini_conn *mc; 191 const struct conn_iface *conn_iface; 192 193 if (!packet_in_is_ok(version, packet_in)) 194 return NULL; 195 switch (version) 196 { 197 case LSQVER_050: 198 conn_iface = &mini_conn_iface_standard_Q050; 199 break; 200 default: 201 conn_iface = &mini_conn_iface_standard; 202 break; 203 } 204 205 mc = lsquic_malo_get(enp->enp_mm.malo.mini_conn); 206 if (!mc) 207 { 208 LSQ_LOG1(LSQ_LOG_WARN, "cannot allocate mini connection: %s", 209 strerror(errno)); 210 return NULL; 211 } 212 213 memset(mc, 0, sizeof(*mc)); 214 TAILQ_INIT(&mc->mc_deferred); 215 TAILQ_INIT(&mc->mc_packets_in); 216 TAILQ_INIT(&mc->mc_packets_out); 217 mc->mc_enpub = enp; 218 mc->mc_created = packet_in->pi_received; 219 mc->mc_path.np_pack_size = packet_in->pi_data_sz; 220 mc->mc_conn.cn_cces = mc->mc_cces; 221 mc->mc_conn.cn_cces_mask = 1; 222 mc->mc_conn.cn_n_cces = sizeof(mc->mc_cces) / sizeof(mc->mc_cces[0]); 223 mc->mc_conn.cn_version = version; 224 mc->mc_conn.cn_pf = select_pf_by_ver(version); 225 mc->mc_conn.cn_esf_c = select_esf_common_by_ver(version); 226 mc->mc_conn.cn_esf.g = select_esf_gquic_by_ver(version); 227 mc->mc_conn.cn_cid = packet_in->pi_conn_id; 228 mc->mc_conn.cn_flags = LSCONN_MINI | LSCONN_SERVER; 229 mc->mc_conn.cn_if = conn_iface; 230 LSQ_DEBUG("created mini connection object"); 231 MCHIST_APPEND(mc, MCHE_CREATED); 232 return &mc->mc_conn; 233} 234 235 236static int 237in_acked_range (const struct ack_info *acki, lsquic_packno_t n) /* This is a copy */ 238{ 239 int in_range = 0; 240 unsigned i; 241 for (i = 0; i < acki->n_ranges; ++i) 242 in_range += acki->ranges[i].high >= n 243 && acki->ranges[i].low <= n; 244 return in_range > 0; 245} 246 247 248static void 249take_rtt_sample (struct mini_conn *mc, const lsquic_packet_out_t *packet_out, 250 lsquic_time_t now, lsquic_time_t lack_delta) 251{ 252 assert(packet_out->po_sent); 253 lsquic_time_t measured_rtt = now - packet_out->po_sent; 254 if (lack_delta < measured_rtt) 255 { 256 lsquic_rtt_stats_update(&mc->mc_rtt_stats, measured_rtt, lack_delta); 257 LSQ_DEBUG("srtt: %"PRIu64" usec, var: %"PRIu64, 258 lsquic_rtt_stats_get_srtt(&mc->mc_rtt_stats), 259 lsquic_rtt_stats_get_rttvar(&mc->mc_rtt_stats)); 260 } 261} 262 263 264static unsigned 265process_ack_frame (struct mini_conn *mc, lsquic_packet_in_t *packet_in, 266 const unsigned char *p, size_t len) 267{ 268 int parsed_len; 269 int n_newly_acked; 270 unsigned n; 271 lsquic_packet_out_t *packet_out, *next; 272 struct ack_info *acki; 273 lsquic_packno_t packno; 274 lsquic_time_t warn_time; 275 char buf[200]; 276 277 acki = mc->mc_enpub->enp_mm.acki; 278 parsed_len = mc->mc_conn.cn_pf->pf_parse_ack_frame(p, len, acki, 0); 279 if (parsed_len < 0) 280 return 0; 281 if (empty_ack_frame(acki)) 282 { 283 LSQ_DEBUG("Ignore empty ACK frame"); 284 return parsed_len; 285 } 286 if (packet_in->pi_packno <= mc->mc_max_ack_packno) 287 { 288 LSQ_DEBUG("Ignore old ack (max %u)", mc->mc_max_ack_packno); 289 return parsed_len; 290 } 291 if (packet_in->pi_packno <= UCHAR_MAX) 292 mc->mc_max_ack_packno = packet_in->pi_packno; 293 294 /* Verify ACK frame and update list of acked packet numbers: */ 295 for (n = 0; n < acki->n_ranges; ++n) 296 for (packno = acki->ranges[n].low; packno <= acki->ranges[n].high; 297 ++packno) 298 if (packno > MINICONN_MAX_PACKETS || 299 0 == (MCONN_PACKET_MASK(packno) & mc->mc_sent_packnos)) 300 { 301 warn_time = lsquic_time_now(); 302 if (0 == mc->mc_enpub->enp_last_warning[WT_ACKPARSE_MINI] 303 || mc->mc_enpub->enp_last_warning[WT_ACKPARSE_MINI] 304 + WARNING_INTERVAL < warn_time) 305 { 306 mc->mc_enpub->enp_last_warning[WT_ACKPARSE_MINI] 307 = warn_time; 308 lsquic_hexdump(p, len, buf, sizeof(buf)); 309 LSQ_WARN("packet %"PRIu64" was never sent; ACK " 310 "frame:\n%s", packno, buf); 311 } 312 else 313 LSQ_DEBUG("packet %"PRIu64" was never sent", packno); 314 MCHIST_APPEND(mc, MCHE_UNSENT_ACKED); 315 return 0; 316 } 317 else 318 mc->mc_acked_packnos |= MCONN_PACKET_MASK(packno); 319 320 EV_LOG_ACK_FRAME_IN(LSQUIC_LOG_CONN_ID, acki); 321 n_newly_acked = 0; 322 for (packet_out = TAILQ_FIRST(&mc->mc_packets_out); packet_out; 323 packet_out = next) 324 { 325 next = TAILQ_NEXT(packet_out, po_next); 326 if (in_acked_range(acki, packet_out->po_packno)) 327 { 328 ++n_newly_acked; 329 LSQ_DEBUG("Got ACK for packet %"PRIu64, packet_out->po_packno); 330 if (packet_out->po_packno == largest_acked(acki)) 331 take_rtt_sample(mc, packet_out, packet_in->pi_received, 332 acki->lack_delta); 333 TAILQ_REMOVE(&mc->mc_packets_out, packet_out, po_next); 334 mini_destroy_packet(mc, packet_out); 335 } 336 } 337 338 if (n_newly_acked > 0) 339 mc->mc_hsk_count = 0; 340 341 return parsed_len; 342} 343 344 345static unsigned 346process_blocked_frame (struct mini_conn *mc, lsquic_packet_in_t *packet_in, 347 const unsigned char *p, size_t len) 348{ 349 lsquic_stream_id_t stream_id; 350 int parsed_len; 351 parsed_len = mc->mc_conn.cn_pf->pf_parse_blocked_frame(p, len, &stream_id); 352 if (parsed_len < 0) 353 return 0; 354 EV_LOG_BLOCKED_FRAME_IN(LSQUIC_LOG_CONN_ID, stream_id); 355 LSQ_DEBUG("Peer reports stream %"PRIu64" as blocked", stream_id); 356 return parsed_len; 357} 358 359 360static mconn_packno_set_t 361drop_packets_out (struct mini_conn *mc) 362{ 363 struct lsquic_packet_out *packet_out; 364 mconn_packno_set_t in_flight = 0; 365 366 while ((packet_out = TAILQ_FIRST(&mc->mc_packets_out))) 367 { 368 TAILQ_REMOVE(&mc->mc_packets_out, packet_out, po_next); 369 if (packet_out->po_flags & PO_SENT) 370 in_flight |= MCONN_PACKET_MASK(packet_out->po_packno); 371 mini_destroy_packet(mc, packet_out); 372 } 373 374 return in_flight; 375} 376 377 378static unsigned 379process_connection_close_frame (struct mini_conn *mc, 380 lsquic_packet_in_t *packet_in, const unsigned char *p, size_t len) 381{ 382 uint64_t error_code; 383 uint16_t reason_len; 384 uint8_t reason_off; 385 int parsed_len; 386 387 (void) drop_packets_out(mc); 388 parsed_len = mc->mc_conn.cn_pf->pf_parse_connect_close_frame(p, len, 389 NULL, &error_code, &reason_len, &reason_off); 390 if (parsed_len < 0) 391 return 0; 392 mc->mc_error_code = (uint64_t) error_code; 393 EV_LOG_CONNECTION_CLOSE_FRAME_IN(LSQUIC_LOG_CONN_ID, error_code, 394 (int) reason_len, (const char *) p + reason_off); 395 if (error_code != 25 /* No recent network activity */ 396 && error_code != 62 /* An active session exists for the given IP */ 397 && error_code != 27 ) /* Write failed with error: -142 (Unknown error)*/ 398 { 399 LSQ_WARN("Received CONNECTION_CLOSE frame (code: %"PRIu64"; reason: %.*s)", 400 error_code, (int) reason_len, (const char *) p + reason_off); 401 } 402 MCHIST_APPEND(mc, MCHE_CONN_CLOSE); 403 return 0; /* This shuts down the connection */ 404} 405 406 407static unsigned 408process_goaway_frame (struct mini_conn *mc, lsquic_packet_in_t *packet_in, 409 const unsigned char *p, size_t len) 410{ 411 lsquic_stream_id_t stream_id; 412 uint32_t error_code; 413 uint16_t reason_length; 414 const char *reason; 415 int parsed_len; 416 parsed_len = mc->mc_conn.cn_pf->pf_parse_goaway_frame(p, len, &error_code, &stream_id, 417 &reason_length, &reason); 418 if (parsed_len < 0) 419 return 0; 420 EV_LOG_GOAWAY_FRAME_IN(LSQUIC_LOG_CONN_ID, error_code, stream_id, 421 reason_length, reason); 422 LSQ_DEBUG("received GOAWAY frame, last good stream ID: %"PRIu64", " 423 "error code: 0x%X, reason: `%.*s'", stream_id, error_code, 424 reason_length, reason); 425 if (stream_id != 0) /* This is odd. We warn: */ 426 LSQ_WARN("stream ID is %"PRIu64" in GOAWAY frame", stream_id); 427 mc->mc_conn.cn_flags |= LSCONN_PEER_GOING_AWAY; 428 return parsed_len; 429} 430 431 432static unsigned 433process_invalid_frame (struct mini_conn *mc, lsquic_packet_in_t *packet_in, 434 const unsigned char *p, size_t len) 435{ 436 LSQ_INFO("invalid frame"); 437 MCHIST_APPEND(mc, MCHE_INVALID_FRAME); 438 return 0; 439} 440 441 442static unsigned 443count_zero_bytes (const unsigned char *p, size_t len) 444{ 445 const unsigned char *const end = p + len; 446 while (p < end && 0 == *p) 447 ++p; 448 return len - (end - p); 449} 450 451 452static unsigned 453process_padding_frame (struct mini_conn *mc, lsquic_packet_in_t *packet_in, 454 const unsigned char *p, size_t len) 455{ 456 len = (size_t) count_zero_bytes(p, len); 457 EV_LOG_PADDING_FRAME_IN(LSQUIC_LOG_CONN_ID, len); 458 return len; 459} 460 461 462static unsigned 463process_ping_frame (struct mini_conn *mc, lsquic_packet_in_t *packet_in, 464 const unsigned char *p, size_t len) 465{ 466 EV_LOG_PING_FRAME_IN(LSQUIC_LOG_CONN_ID); 467 return 1; 468} 469 470 471static unsigned 472process_rst_stream_frame (struct mini_conn *mc, lsquic_packet_in_t *packet_in, 473 const unsigned char *p, size_t len) 474{ 475 lsquic_stream_id_t stream_id; 476 uint64_t offset, error_code; 477 int parsed_len; 478 parsed_len = mc->mc_conn.cn_pf->pf_parse_rst_frame(p, len, &stream_id, &offset, &error_code); 479 if (parsed_len < 0) 480 return 0; 481 EV_LOG_RST_STREAM_FRAME_IN(LSQUIC_LOG_CONN_ID, stream_id, offset, 482 error_code); 483 LSQ_DEBUG("Got RST_STREAM; stream: %"PRIu64"; offset: 0x%"PRIX64, stream_id, 484 offset); 485 if (is_handshake_stream_id(mc, stream_id)) 486 { 487 LSQ_INFO("handshake stream reset, closing connection"); 488 return 0; 489 } 490 else 491 return parsed_len; 492} 493 494 495static unsigned 496process_stop_waiting_frame (struct mini_conn *mc, lsquic_packet_in_t *packet_in, 497 const unsigned char *p, size_t len) 498{ 499 lsquic_packno_t least; 500 enum packno_bits bits = lsquic_packet_in_packno_bits(packet_in); 501 int parsed_len; 502 parsed_len = mc->mc_conn.cn_pf->pf_parse_stop_waiting_frame(p, len, packet_in->pi_packno, bits, 503 &least); 504 if (parsed_len < 0) 505 return 0; 506 EV_LOG_STOP_WAITING_FRAME_IN(LSQUIC_LOG_CONN_ID, least); 507 LSQ_DEBUG("Got STOP_WAITING frame, least unacked: %"PRIu64, least); 508 if (least > MINICONN_MAX_PACKETS) 509 return 0; 510 else 511 { 512 mc->mc_cutoff = least; 513 return parsed_len; 514 } 515} 516 517 518static unsigned 519process_stream_frame (struct mini_conn *mc, lsquic_packet_in_t *packet_in, 520 const unsigned char *p, size_t len) 521{ 522 stream_frame_t stream_frame; 523 int parsed_len; 524 parsed_len = mc->mc_conn.cn_pf->pf_parse_stream_frame(p, len, &stream_frame); 525 if (parsed_len < 0) 526 return 0; 527 EV_LOG_STREAM_FRAME_IN(LSQUIC_LOG_CONN_ID, &stream_frame); 528 LSQ_DEBUG("Got stream frame for stream #%"PRIu64, stream_frame.stream_id); 529 if (is_handshake_stream_id(mc, stream_frame.stream_id)) 530 { 531 if (packet_in->pi_flags & PI_HSK_STREAM) 532 { /* This is not supported for simplicity. The spec recommends 533 * not putting more than one stream frame from the same stream 534 * into a single packet. If this changes and clients actually 535 * do that, we can revisit this code. 536 */ 537 LSQ_INFO("two handshake stream frames in single incoming packet"); 538 MCHIST_APPEND(mc, MCHE_2HSK_1STREAM); 539 return 0; 540 } 541 if (stream_frame.data_frame.df_offset >= mc->mc_read_off) 542 { 543 packet_in->pi_flags |= PI_HSK_STREAM; 544 packet_in->pi_hsk_stream = p - packet_in->pi_data; 545 mc->mc_flags |= MC_HAVE_NEW_HSK; 546 MCHIST_APPEND(mc, MCHE_NEW_HSK); 547 if (0 == stream_frame.data_frame.df_offset) 548 { 549 /* First CHLO message: update maximum packet size */ 550 mc->mc_path.np_pack_size = packet_in->pi_data_sz; 551 LSQ_DEBUG("update packet size to %hu", 552 mc->mc_path.np_pack_size); 553 } 554 } 555 else 556 { 557 LSQ_DEBUG("drop duplicate frame"); 558 MCHIST_APPEND(mc, MCHE_DUP_HSK); 559 } 560 } 561 return parsed_len; 562} 563 564 565static unsigned 566process_crypto_frame (struct mini_conn *mc, struct lsquic_packet_in *packet_in, 567 const unsigned char *p, size_t len) 568{ 569 stream_frame_t stream_frame; 570 int parsed_len; 571 parsed_len = mc->mc_conn.cn_pf->pf_parse_crypto_frame(p, len, 572 &stream_frame); 573 if (parsed_len < 0) 574 return 0; 575 EV_LOG_CRYPTO_FRAME_IN(LSQUIC_LOG_CONN_ID, &stream_frame, 576 lsquic_packet_in_enc_level(packet_in)); 577 LSQ_DEBUG("Got CRYPTO frame at encryption level %s", 578 lsquic_enclev2str[lsquic_packet_in_enc_level(packet_in)]); 579 if (packet_in->pi_flags & PI_HSK_STREAM) 580 { /* This is not supported for simplicity: assume a single CRYPTO frame 581 * per packet. If this changes, we can revisit this code. 582 */ 583 LSQ_INFO("two CRYPTO frames in single incoming packet"); 584 MCHIST_APPEND(mc, MCHE_2HSK_1STREAM); 585 return 0; 586 } 587 if (stream_frame.data_frame.df_offset >= mc->mc_read_off) 588 { 589 packet_in->pi_flags |= PI_HSK_STREAM; 590 packet_in->pi_hsk_stream = p - packet_in->pi_data; 591 mc->mc_flags |= MC_HAVE_NEW_HSK; 592 MCHIST_APPEND(mc, MCHE_NEW_HSK); 593 if (0 == stream_frame.data_frame.df_offset) 594 { 595 /* First CHLO message: update maximum packet size */ 596 mc->mc_path.np_pack_size = packet_in->pi_data_sz 597 /* Q050 and later adjust pi_data_sz of Initial packets during 598 * decryption, here we have to add the tag length back: 599 */ 600 + mc->mc_conn.cn_esf_c->esf_tag_len; 601 LSQ_DEBUG("update packet size to %hu", mc->mc_path.np_pack_size); 602 } 603 } 604 else 605 { 606 LSQ_DEBUG("drop duplicate frame"); 607 MCHIST_APPEND(mc, MCHE_DUP_HSK); 608 } 609 return parsed_len; 610} 611 612 613static unsigned 614process_window_update_frame (struct mini_conn *mc, 615 lsquic_packet_in_t *packet_in, const unsigned char *p, size_t len) 616{ 617 lsquic_stream_id_t stream_id; 618 uint64_t offset; 619 int parsed_len; 620 parsed_len = mc->mc_conn.cn_pf->pf_parse_window_update_frame(p, len, &stream_id, &offset); 621 if (parsed_len < 0) 622 return 0; 623 EV_LOG_WINDOW_UPDATE_FRAME_IN(LSQUIC_LOG_CONN_ID, stream_id, offset); 624 if (is_handshake_stream_id(mc, stream_id)) 625 /* This should not happen: why would the client send us WINDOW_UPDATE 626 * on stream 1? 627 */ 628 LSQ_WARN("client sent WINDOW_UPDATE for handshake stream, " 629 "offset %"PRIu64, offset); 630 return parsed_len; 631} 632 633 634typedef unsigned (*process_frame_f)( 635 struct mini_conn *, lsquic_packet_in_t *, const unsigned char *p, size_t); 636 637 638static process_frame_f const process_frames[N_QUIC_FRAMES] = 639{ 640 [QUIC_FRAME_ACK] = process_ack_frame, 641 [QUIC_FRAME_BLOCKED] = process_blocked_frame, 642 [QUIC_FRAME_CONNECTION_CLOSE] = process_connection_close_frame, 643 [QUIC_FRAME_CRYPTO] = process_crypto_frame, 644 [QUIC_FRAME_GOAWAY] = process_goaway_frame, 645 [QUIC_FRAME_INVALID] = process_invalid_frame, 646 [QUIC_FRAME_PADDING] = process_padding_frame, 647 [QUIC_FRAME_PING] = process_ping_frame, 648 [QUIC_FRAME_RST_STREAM] = process_rst_stream_frame, 649 [QUIC_FRAME_STOP_WAITING] = process_stop_waiting_frame, 650 [QUIC_FRAME_STREAM] = process_stream_frame, 651 [QUIC_FRAME_WINDOW_UPDATE] = process_window_update_frame, 652}; 653 654 655static unsigned 656process_packet_frame (struct mini_conn *mc, lsquic_packet_in_t *packet_in, 657 const unsigned char *p, size_t len) 658{ 659 enum quic_frame_type type = mc->mc_conn.cn_pf->pf_parse_frame_type(p, len); 660 packet_in->pi_frame_types |= 1 << type; 661 return process_frames[type](mc, packet_in, p, len); 662} 663 664 665static void 666record_largest_recv (struct mini_conn *mc, lsquic_time_t t) 667{ 668 if (t < mc->mc_created) 669 { 670 LSQ_WARN("largest received predates creation"); 671 return; 672 } 673 t -= mc->mc_created; 674 mc->mc_largest_recv[0] = t; 675 mc->mc_largest_recv[1] = t >> 8; 676 mc->mc_largest_recv[2] = t >> 16; 677 LSQ_DEBUG("recorded largest received timestamp as %"PRIu64" usec since " 678 "creation", t); 679} 680 681 682static enum dec_packin 683conn_decrypt_packet (struct mini_conn *conn, lsquic_packet_in_t *packet_in) 684{ 685 return conn->mc_conn.cn_esf_c->esf_decrypt_packet( 686 conn->mc_conn.cn_enc_session, conn->mc_enpub, 687 &conn->mc_conn, packet_in); 688} 689 690 691/* PRP: Process Regular Packet */ 692enum proc_rp { PRP_KEEP, PRP_DEFER, PRP_DROP, PRP_ERROR, }; 693 694 695static enum proc_rp 696conn_decrypt_packet_or (struct mini_conn *mc, 697 struct lsquic_packet_in *packet_in) 698{ 699 if (DECPI_OK == conn_decrypt_packet(mc, packet_in)) 700 { 701 MCHIST_APPEND(mc, MCHE_DECRYPTED); 702 return PRP_KEEP; 703 } 704 else if (mc->mc_conn.cn_esf.g->esf_have_key_gt_one( 705 mc->mc_conn.cn_enc_session)) 706 { 707 LSQ_INFO("could not decrypt packet: drop"); 708 mc->mc_dropped_packnos |= MCONN_PACKET_MASK(packet_in->pi_packno); 709 MCHIST_APPEND(mc, MCHE_UNDECR_DROP); 710 return PRP_DROP; 711 } 712 else if ((packet_in->pi_flags & PI_OWN_DATA) || 713 0 == lsquic_conn_copy_and_release_pi_data(&mc->mc_conn, 714 mc->mc_enpub, packet_in)) 715 { 716 assert(packet_in->pi_flags & PI_OWN_DATA); 717 LSQ_INFO("could not decrypt packet: defer"); 718 mc->mc_deferred_packnos |= MCONN_PACKET_MASK(packet_in->pi_packno); 719 MCHIST_APPEND(mc, MCHE_UNDECR_DEFER); 720 return PRP_DEFER; 721 } 722 else 723 { 724 MCHIST_APPEND(mc, MCHE_ENOMEM); 725 return PRP_ERROR; /* Memory allocation must have failed */ 726 } 727} 728 729 730static enum proc_rp 731process_regular_packet (struct mini_conn *mc, lsquic_packet_in_t *packet_in) 732{ 733 const unsigned char *p, *pend; 734 enum proc_rp prp; 735 unsigned len; 736 737 /* Decrypt packet if necessary */ 738 if (0 == (packet_in->pi_flags & PI_DECRYPTED)) 739 { 740 prp = conn_decrypt_packet_or(mc, packet_in); 741 if (prp != PRP_KEEP) 742 return prp; 743 } 744 745 /* Update receive history before processing the packet: if there is an 746 * error, the connection is terminated and recording this packet number 747 * is helpful when it is printed along with other diagnostics in dtor. 748 */ 749 if (0 == mc->mc_received_packnos || 750 packet_in->pi_packno > highest_bit_set(mc->mc_received_packnos) + 1) 751 record_largest_recv(mc, packet_in->pi_received); 752 mc->mc_received_packnos |= MCONN_PACKET_MASK(packet_in->pi_packno); 753 754 /* Parse and process frames */ 755 p = packet_in->pi_data + packet_in->pi_header_sz; 756 pend = packet_in->pi_data + packet_in->pi_data_sz; 757 while (p < pend) 758 { 759 len = process_packet_frame(mc, packet_in, p, pend - p); 760 if (len > 0) 761 p += len; 762 else 763 { 764 if (mc->mc_conn.cn_pf->pf_parse_frame_type(p, pend - p) != 765 QUIC_FRAME_CONNECTION_CLOSE) 766 LSQ_WARN("error parsing frame: packno %"PRIu64"; sz: %u; type: " 767 "0x%X", packet_in->pi_packno, packet_in->pi_data_sz, p[0]); 768 MCHIST_APPEND(mc, MCHE_EFRAME); 769 return PRP_ERROR; 770 } 771 } 772 773 mc->mc_flags |= MC_GEN_ACK; 774 775 return PRP_KEEP; 776} 777 778 779struct hsk_chunk 780{ 781 lsquic_packet_in_t *hsk_packet_in; 782 const unsigned char *hsk_data; 783 unsigned hsk_off; 784 unsigned hsk_sz; 785}; 786 787 788static int 789compare_hsk_chunks (const void *ap, const void *bp) 790{ 791 const struct hsk_chunk *a = ap; 792 const struct hsk_chunk *b = bp; 793 return (a->hsk_off > b->hsk_off) - (b->hsk_off > a->hsk_off); 794} 795 796 797struct mini_stream_ctx 798{ 799 const unsigned char *buf; 800 size_t bufsz; 801 size_t off; 802}; 803 804 805static int 806mini_stream_has_data (const struct mini_stream_ctx *ms_ctx) 807{ 808 return ms_ctx->off < ms_ctx->bufsz; 809} 810 811 812static size_t 813mini_stream_read (void *stream, void *buf, size_t len, int *reached_fin) 814{ 815 struct mini_stream_ctx *ms_ctx = stream; 816 size_t avail = ms_ctx->bufsz - ms_ctx->off; 817 if (avail < len) 818 len = avail; 819 memcpy(buf, ms_ctx->buf + ms_ctx->off, len); 820 ms_ctx->off += len; 821 *reached_fin = 0; 822 return len; 823} 824 825 826/* Wrapper to throw out reached_fin */ 827static size_t 828mini_stream_read_for_crypto (void *stream, void *buf, size_t len, int *fin) 829{ 830 size_t retval; 831 int reached_fin; 832 833 retval = mini_stream_read(stream, buf, len, &reached_fin); 834 return retval; 835} 836 837 838static size_t 839mini_stream_size (void *stream) 840{ 841 struct mini_stream_ctx *ms_ctx = stream; 842 size_t avail = ms_ctx->bufsz - ms_ctx->off; 843 return avail; 844} 845 846 847static int 848mini_stream_fin (void *stream) 849{ /* There is never a FIN on the handshake stream */ 850 return 0; 851} 852 853 854static lsquic_packno_t 855next_packno (struct mini_conn *mc) 856{ 857 if (mc->mc_cur_packno < MINICONN_MAX_PACKETS) 858 { 859 return ++mc->mc_cur_packno; 860 } 861 else 862 { 863 if (!(mc->mc_flags & MC_OO_PACKNOS)) 864 { 865 MCHIST_APPEND(mc, MCHE_OUT_OF_PACKNOS); 866 mc->mc_flags |= MC_OO_PACKNOS; 867 LSQ_DEBUG("ran out of outgoing packet numbers"); 868 } 869 return MINICONN_MAX_PACKETS + 1; 870 } 871} 872 873 874static lsquic_packet_out_t * 875allocate_packet_out (struct mini_conn *mc, const unsigned char *nonce) 876{ 877 lsquic_packet_out_t *packet_out; 878 lsquic_packno_t packno; 879 packno = next_packno(mc); 880 if (packno > MINICONN_MAX_PACKETS) 881 { 882 LSQ_DEBUG("ran out of outgoing packet numbers, won't allocate packet"); 883 return NULL; 884 } 885 packet_out = lsquic_packet_out_new(&mc->mc_enpub->enp_mm, NULL, 1, 886 &mc->mc_conn, GQUIC_PACKNO_LEN_1, NULL, nonce, &mc->mc_path, 887 HETY_NOT_SET); 888 if (!packet_out) 889 { 890 LSQ_WARN("could not allocate packet: %s", strerror(errno)); 891 return NULL; 892 } 893 packet_out->po_loss_chain = packet_out; 894 packet_out->po_packno = packno; 895 packet_out->po_flags |= PO_MINI; 896 if (mc->mc_flags & MC_HAVE_SHLO) 897 { 898 packet_out->po_flags |= PO_HELLO; 899 packet_out->po_header_type = HETY_0RTT; 900 } 901 if (mc->mc_conn.cn_version >= LSQVER_050) 902 { 903 if (nonce) 904 packet_out->po_header_type = HETY_0RTT; 905 else 906 packet_out->po_header_type = HETY_INITIAL; 907 } 908 lsquic_packet_out_set_pns(packet_out, PNS_APP); 909 TAILQ_INSERT_TAIL(&mc->mc_packets_out, packet_out, po_next); 910 LSQ_DEBUG("allocated packet #%"PRIu64", nonce: %d", packno, !!nonce); 911 MCHIST_APPEND(mc, MCHE_NEW_PACKET_OUT); 912 EV_LOG_PACKET_CREATED(LSQUIC_LOG_CONN_ID, packet_out); 913 return packet_out; 914} 915 916 917static struct lsquic_packet_out * 918to_packet_pre_Q050 (struct mini_conn *mc, struct mini_stream_ctx *ms_ctx, 919 const unsigned char *nonce) 920{ 921 struct lsquic_packet_out *packet_out; 922 size_t cur_off; 923 int len; 924 925 packet_out = allocate_packet_out(mc, nonce); 926 if (!packet_out) 927 return NULL; 928 cur_off = ms_ctx->off; 929 len = mc->mc_conn.cn_pf->pf_gen_stream_frame( 930 packet_out->po_data + packet_out->po_data_sz, 931 lsquic_packet_out_avail(packet_out), 932 1, mc->mc_write_off, mini_stream_fin(ms_ctx), 933 mini_stream_size(ms_ctx), mini_stream_read, ms_ctx); 934 if (len < 0) 935 { 936 LSQ_WARN("cannot generate STREAM frame (avail: %u)", 937 lsquic_packet_out_avail(packet_out)); 938 return NULL; 939 } 940 mc->mc_write_off += ms_ctx->off - cur_off; 941 EV_LOG_GENERATED_STREAM_FRAME(LSQUIC_LOG_CONN_ID, mc->mc_conn.cn_pf, 942 packet_out->po_data + packet_out->po_data_sz, len); 943 packet_out->po_data_sz += len; 944 packet_out->po_frame_types |= 1 << QUIC_FRAME_STREAM; 945 if (0 == lsquic_packet_out_avail(packet_out)) 946 packet_out->po_flags |= PO_STREAM_END; 947 948 return packet_out; 949} 950 951 952static struct lsquic_packet_out * 953to_packet_Q050plus (struct mini_conn *mc, struct mini_stream_ctx *ms_ctx, 954 const unsigned char *nonce) 955{ 956 struct lsquic_packet_out *packet_out; 957 size_t cur_off; 958 int len; 959 960 if (nonce && !(mc->mc_flags & MC_WR_OFF_RESET)) 961 { 962 mc->mc_write_off = 0; 963 mc->mc_flags |= MC_WR_OFF_RESET; 964 } 965 966 packet_out = allocate_packet_out(mc, nonce); 967 if (!packet_out) 968 return NULL; 969 cur_off = ms_ctx->off; 970 len = mc->mc_conn.cn_pf->pf_gen_crypto_frame( 971 packet_out->po_data + packet_out->po_data_sz, 972 lsquic_packet_out_avail(packet_out), 0, mc->mc_write_off, 0, 973 mini_stream_size(ms_ctx), mini_stream_read_for_crypto, ms_ctx); 974 if (len < 0) 975 { 976 LSQ_WARN("cannot generate CRYPTO frame (avail: %u)", 977 lsquic_packet_out_avail(packet_out)); 978 return NULL; 979 } 980 mc->mc_write_off += ms_ctx->off - cur_off; 981 EV_LOG_GENERATED_CRYPTO_FRAME(LSQUIC_LOG_CONN_ID, mc->mc_conn.cn_pf, 982 packet_out->po_data + packet_out->po_data_sz, len); 983 packet_out->po_data_sz += len; 984 packet_out->po_frame_types |= 1 << QUIC_FRAME_CRYPTO; 985 986 return packet_out; 987} 988 989 990static int 991packetize_response (struct mini_conn *mc, const unsigned char *buf, 992 size_t bufsz, const unsigned char *nonce) 993{ 994 struct mini_stream_ctx ms_ctx; 995 lsquic_packet_out_t *packet_out; 996 struct lsquic_packet_out * (*const to_packet) (struct mini_conn *, 997 struct mini_stream_ctx *, const unsigned char *) 998 = mc->mc_conn.cn_version < LSQVER_050 999 ? to_packet_pre_Q050 : to_packet_Q050plus; 1000 1001 LSQ_DEBUG("Packetizing %zd bytes of handshake response", bufsz); 1002 1003 ms_ctx.buf = buf; 1004 ms_ctx.bufsz = bufsz; 1005 ms_ctx.off = 0; 1006 1007 do 1008 { 1009 packet_out = to_packet(mc, &ms_ctx, nonce); 1010 if (!packet_out) 1011 return -1; 1012 } 1013 while (mini_stream_has_data(&ms_ctx)); 1014 1015 /* PAD the last packet with NULs. ACK and STOP_WAITING go into a separate 1016 * packet. 1017 */ 1018 if (lsquic_packet_out_avail(packet_out)) 1019 { 1020 EV_LOG_CONN_EVENT(LSQUIC_LOG_CONN_ID, "generated PADDING frame %u " 1021 "bytes long", lsquic_packet_out_avail(packet_out)); 1022 memset(packet_out->po_data + packet_out->po_data_sz, 0, 1023 lsquic_packet_out_avail(packet_out)); 1024 packet_out->po_data_sz += lsquic_packet_out_avail(packet_out); 1025 packet_out->po_frame_types |= 1 << QUIC_FRAME_PADDING; 1026 } 1027 1028 return 0; 1029} 1030 1031 1032static int 1033continue_handshake (struct mini_conn *mc) 1034{ 1035 lsquic_packet_in_t *packet_in; 1036 unsigned n_hsk_chunks = 0, n_contig, n, bufsz, off; 1037 int s, rv; 1038 size_t out_len; 1039 enum handshake_error he; 1040 unsigned char *buf_in_16k, *buf_out; 1041 const unsigned char *buf_in; 1042 time_t t; 1043 stream_frame_t frame; 1044 struct hsk_chunk hsk_chunks[MINICONN_MAX_PACKETS], *hsk_chunk; 1045 unsigned char nonce_buf[32]; 1046 int nonce_set = 0; 1047 int (*parse_frame)(const unsigned char *, size_t, struct stream_frame *) 1048 = mc->mc_conn.cn_version < LSQVER_050 1049 ? mc->mc_conn.cn_pf->pf_parse_stream_frame 1050 : mc->mc_conn.cn_pf->pf_parse_crypto_frame; 1051 1052 /* Get handshake stream data from each packet that contains a handshake 1053 * stream frame and place them into `hsk_chunks' array. 1054 */ 1055 TAILQ_FOREACH(packet_in, &mc->mc_packets_in, pi_next) 1056 { 1057 assert(n_hsk_chunks < sizeof(hsk_chunks) / sizeof(hsk_chunks[0])); 1058 if (0 == (packet_in->pi_flags & PI_HSK_STREAM)) 1059 continue; 1060 s = parse_frame(packet_in->pi_data + packet_in->pi_hsk_stream, 1061 packet_in->pi_data_sz - packet_in->pi_hsk_stream, &frame); 1062 if (-1 == s) 1063 { 1064 LSQ_WARN("cannot process hsk stream frame in packet %"PRIu64, 1065 packet_in->pi_packno); 1066 return -1; 1067 } 1068 hsk_chunk = &hsk_chunks[ n_hsk_chunks++ ]; 1069 hsk_chunk->hsk_packet_in = packet_in; 1070 hsk_chunk->hsk_data = frame.data_frame.df_data; 1071 hsk_chunk->hsk_off = frame.data_frame.df_offset; 1072 hsk_chunk->hsk_sz = frame.data_frame.df_size; 1073 } 1074 assert(n_hsk_chunks > 0); 1075 1076 if (n_hsk_chunks > 1) 1077 { 1078 /* Sort handshake stream data */ 1079 qsort(hsk_chunks, n_hsk_chunks, sizeof(hsk_chunks[0]), 1080 compare_hsk_chunks); 1081 /* Figure out how many packets contain handshake stream data in a 1082 * contiguous buffer and how large this data is. 1083 */ 1084 for (n = 1, n_contig = 1, bufsz = hsk_chunks[0].hsk_sz; 1085 n < n_hsk_chunks; ++n) 1086 if (hsk_chunks[n - 1].hsk_off + hsk_chunks[n - 1].hsk_sz == 1087 hsk_chunks[n].hsk_off) 1088 { 1089 ++n_contig; 1090 bufsz += hsk_chunks[n].hsk_sz; 1091 } 1092 else 1093 break; 1094 } 1095 else 1096 { 1097 n_contig = 1; 1098 bufsz = hsk_chunks[0].hsk_sz; 1099 } 1100 1101 /* Handshake handler expects to start reading at a particular offset. 1102 */ 1103 if (hsk_chunks[0].hsk_off != mc->mc_read_off) 1104 { 1105 LSQ_DEBUG("smallest hsk offset is %u, need %hu", 1106 hsk_chunks[0].hsk_off, mc->mc_read_off); 1107 MCHIST_APPEND(mc, MCHE_HELLO_HOLE); 1108 return 0; 1109 } 1110 1111 LSQ_DEBUG("# of contiguous stream frames: %u out of %u; offset: %u; " 1112 "total size: %u", n_contig, n_hsk_chunks, hsk_chunks[0].hsk_off, bufsz); 1113 1114 if (bufsz > 16 * 1024) 1115 { 1116 LSQ_INFO("too much contiguous handshake data (%u bytes); max: %u", 1117 bufsz, 16 * 1024); 1118 MCHIST_APPEND(mc, MCHE_HELLO_TOO_MUCH); 1119 return -1; 1120 } 1121 1122 /* From here on, since we need to clean up, we use `rv' and `goto end' 1123 * to handle error conditions and cleanup. 1124 */ 1125 rv = -1; 1126 if (n_contig > 1) 1127 { 1128 buf_in = buf_in_16k = lsquic_mm_get_16k(&mc->mc_enpub->enp_mm); 1129 if (!buf_in) 1130 { 1131 LSQ_WARN("could not allocate in buffer: %s", strerror(errno)); 1132 buf_out = NULL; 1133 goto end; 1134 } 1135 /* Create a single contiguous buffer to pass to lsquic_enc_session_handle_chlo */ 1136 off = 0; 1137 for (n = 0; n < n_contig; ++n) 1138 { 1139 memcpy(buf_in_16k + off, hsk_chunks[n].hsk_data, 1140 hsk_chunks[n].hsk_sz); 1141 off += hsk_chunks[n].hsk_sz; 1142 } 1143 assert(off == bufsz); 1144 } 1145 else 1146 { 1147 buf_in_16k = NULL; 1148 buf_in = hsk_chunks[0].hsk_data; 1149 } 1150 1151 buf_out = lsquic_mm_get_16k(&mc->mc_enpub->enp_mm); 1152 if (!buf_out) 1153 { 1154 LSQ_WARN("could not allocate out buffer: %s", strerror(errno)); 1155 goto end; 1156 } 1157 out_len = 16 * 1024; 1158 1159 /* Allocate enc_session for the server if first time around: */ 1160 if (!mc->mc_conn.cn_enc_session) 1161 { 1162 mc->mc_conn.cn_enc_session = 1163 mc->mc_conn.cn_esf.g->esf_create_server(&mc->mc_conn, 1164 mc->mc_conn.cn_cid, mc->mc_enpub); 1165 if (!mc->mc_conn.cn_enc_session) 1166 { 1167 LSQ_WARN("cannot create new enc session"); 1168 goto end; 1169 } 1170 MCHIST_APPEND(mc, MCHE_NEW_ENC_SESS); 1171 } 1172 1173 t = time(NULL); 1174 he = mc->mc_conn.cn_esf.g->esf_handle_chlo(mc->mc_conn.cn_enc_session, 1175 mc->mc_conn.cn_version, 1176 buf_in, bufsz, t, NP_PEER_SA(&mc->mc_path), 1177 NP_LOCAL_SA(&mc->mc_path), 1178 buf_out, &out_len, nonce_buf, &nonce_set); 1179 1180 if (HS_SHLO == he) 1181 mc->mc_flags |= MC_HAVE_SHLO; 1182 else 1183 mc->mc_flags &= ~MC_HAVE_SHLO; 1184 1185 MCHIST_APPEND(mc, he == DATA_NOT_ENOUGH ? MCHE_HANDLE_NOT_ENOUGH : 1186 he == HS_SHLO ? MCHE_HANDLE_SHLO : 1187 he == HS_1RTT ? MCHE_HANDLE_1RTT : 1188 he == HS_SREJ ? MCHE_HANDLE_SREJ : 1189 he == HS_ERROR ? MCHE_HANDLE_ERROR : 1190 MCHE_HAHDLE_UNKNOWN); 1191 1192 if ((HS_SHLO == he || HS_1RTT == he) && !mc->mc_rtt_stats.srtt) 1193 { 1194 uint32_t irtt; 1195 if (0 == mc->mc_conn.cn_esf.g->esf_get_peer_setting( 1196 mc->mc_conn.cn_enc_session, QTAG_IRTT, &irtt)) 1197 { 1198 /* Do not allow the client to specify unreasonable values: 1199 * smaller than 10ms or larger than 15s. Per reference 1200 * implementation. 1201 */ 1202 if (irtt > 15 * 1000 * 1000) 1203 irtt = 15 * 1000 * 1000; 1204 else if (irtt < 10 * 1000) 1205 irtt = 10 * 1000; 1206 lsquic_rtt_stats_update(&mc->mc_rtt_stats, irtt, 0); 1207 LSQ_DEBUG("Set initial SRTT to %"PRIu32" usec based on client-" 1208 "supplied IRTT value", irtt); 1209 } 1210 } 1211 1212 switch (he) 1213 { 1214 case DATA_NOT_ENOUGH: 1215 LSQ_DEBUG("lsquic_enc_session_handle_chlo needs more data"); 1216 break; 1217 case HS_SHLO: 1218 mc->mc_conn.cn_flags |= LSCONN_HANDSHAKE_DONE; 1219 mc->mc_flags |= MC_PROMOTE; 1220 LSQ_DEBUG("lsquic_enc_session_handle_chlo returned %d, promote", he); 1221 /* Fall through */ 1222 case HS_1RTT: 1223 assert(out_len > 0); 1224 if (mc->mc_conn.cn_version < LSQVER_046 1225 && !mc->mc_conn.cn_esf.g->esf_get_peer_option( 1226 mc->mc_conn.cn_enc_session, QTAG_NSTP)) 1227 mc->mc_flags |= MC_STOP_WAIT_ON; 1228 if (0 != packetize_response(mc, buf_out, out_len, 1229 nonce_set ? nonce_buf : NULL)) 1230 goto end; 1231 mc->mc_read_off += bufsz; 1232 for (n = 0; n < n_contig; ++n) 1233 hsk_chunks[n].hsk_packet_in->pi_flags &= ~PI_HSK_STREAM; 1234 LSQ_DEBUG("read offset is now %hu", mc->mc_read_off); 1235 break; 1236 default: 1237 LSQ_WARN("unexpected return value from lsquic_enc_session_handle_chlo: %u", he); 1238 /* fallthru */ 1239 case HS_ERROR: 1240#if !LSQUIC_KEEP_ENC_SESS_HISTORY 1241 mc->mc_conn.cn_esf.g->esf_destroy(mc->mc_conn.cn_enc_session); 1242 mc->mc_conn.cn_enc_session = NULL; 1243#endif 1244 mc->mc_flags |= MC_HSK_ERR; 1245 LSQ_INFO("lsquic_enc_session_handle_chlo returned an error (%d)", he); 1246 goto end; 1247 } 1248 1249 rv = 0; 1250 1251 end: 1252 mc->mc_flags &= ~MC_HAVE_SHLO; 1253 if (buf_in_16k) 1254 lsquic_mm_put_16k(&mc->mc_enpub->enp_mm, buf_in_16k); 1255 if (buf_out) 1256 lsquic_mm_put_16k(&mc->mc_enpub->enp_mm, buf_out); 1257 return rv; 1258} 1259 1260 1261struct mini_rechist 1262{ 1263 const struct mini_conn *mc; 1264 mconn_packno_set_t cur_set; 1265 int cur_idx; 1266 struct lsquic_packno_range range; /* We return a pointer to this */ 1267}; 1268 1269 1270static void 1271mini_rechist_init (struct mini_rechist *rechist, const struct mini_conn *mc) 1272{ 1273 rechist->mc = mc; 1274 rechist->cur_set = 0; 1275 rechist->cur_idx = 0; 1276} 1277 1278 1279static lsquic_time_t 1280mini_rechist_largest_recv (void *rechist_ctx) 1281{ 1282 struct mini_rechist *rechist = rechist_ctx; 1283 const struct mini_conn *mc = rechist->mc; 1284 lsquic_time_t delta = mc->mc_largest_recv[0] 1285 + (mc->mc_largest_recv[1] << 8) 1286 + (mc->mc_largest_recv[2] << 16); 1287 LSQ_DEBUG("%s: largest received: %"PRIu64" usec since creation", 1288 __func__, delta); 1289 return mc->mc_created + delta; 1290} 1291 1292 1293static const struct lsquic_packno_range * 1294mini_rechist_next (void *rechist_ctx) 1295{ 1296 struct mini_rechist *rechist = rechist_ctx; 1297 const struct mini_conn *mc = rechist->mc; 1298 mconn_packno_set_t packnos; 1299 int i; 1300 1301 packnos = rechist->cur_set; 1302 if (0 == packnos) 1303 return NULL; 1304 1305 /* There may be a faster way to do this, but for now, we just want 1306 * correctness. 1307 */ 1308 for (i = rechist->cur_idx; i >= 0; --i) 1309 if (packnos & (1ULL << i)) 1310 { 1311 rechist->range.low = i + 1; 1312 rechist->range.high = i + 1; 1313 break; 1314 } 1315 assert(i >= 0); /* We must have hit at least one bit */ 1316 --i; 1317 for ( ; i >= 0 && (packnos & (1ULL << i)); --i) 1318 rechist->range.low = i + 1; 1319 if (i >= 0) 1320 { 1321 rechist->cur_set = packnos & ((1ULL << i) - 1); 1322 rechist->cur_idx = i; 1323 } 1324 else 1325 rechist->cur_set = 0; 1326 LSQ_DEBUG("%s: return [%"PRIu64", %"PRIu64"]", __func__, 1327 rechist->range.low, rechist->range.high); 1328 return &rechist->range; 1329} 1330 1331 1332static const struct lsquic_packno_range * 1333mini_rechist_first (void *rechist_ctx) 1334{ 1335 struct mini_rechist *rechist = rechist_ctx; 1336 rechist->cur_set = rechist->mc->mc_received_packnos; 1337 rechist->cur_idx = highest_bit_set(rechist->cur_set); 1338 return mini_rechist_next(rechist_ctx); 1339} 1340 1341 1342static lsquic_packno_t 1343least_unacked (const struct mini_conn *mc) 1344{ 1345 mconn_packno_set_t unacked; 1346 lsquic_packno_t packno; 1347 unacked = mc->mc_sent_packnos & ~mc->mc_acked_packnos; 1348 if (unacked) 1349 packno = lowest_bit_set(unacked) + 1; 1350 else 1351 packno = highest_bit_set(mc->mc_sent_packnos) + 2; 1352 LSQ_DEBUG("%s: least unacked: %"PRIu64, __func__, packno); 1353 return packno; 1354} 1355 1356 1357static int 1358generate_ack_and_stop_waiting (struct mini_conn *mc, lsquic_time_t now) 1359{ 1360 lsquic_packet_out_t *packet_out; 1361 struct mini_rechist rechist; 1362 int len, not_used_has_missing; 1363 lsquic_packno_t lunack; 1364 1365 /* Chrome's quic_server places ACK and STOP_WAITING frames into a separate 1366 * packet. 1367 */ 1368 packet_out = allocate_packet_out(mc, NULL); 1369 if (!packet_out) 1370 return -1; 1371 1372 /* Generate ACK frame */ 1373 mini_rechist_init(&rechist, mc); 1374 len = mc->mc_conn.cn_pf->pf_gen_ack_frame(packet_out->po_data + packet_out->po_data_sz, 1375 lsquic_packet_out_avail(packet_out), mini_rechist_first, 1376 mini_rechist_next, mini_rechist_largest_recv, &rechist, 1377 now, ¬_used_has_missing, &packet_out->po_ack2ed, NULL); 1378 if (len < 0) 1379 { 1380 LSQ_WARN("could not generate ACK frame"); 1381 return -1; 1382 } 1383 EV_LOG_GENERATED_ACK_FRAME(LSQUIC_LOG_CONN_ID, mc->mc_conn.cn_pf, 1384 packet_out->po_data + packet_out->po_data_sz, len); 1385 packet_out->po_frame_types |= 1 << QUIC_FRAME_ACK; 1386 packet_out->po_data_sz += len; 1387 packet_out->po_regen_sz += len; 1388 LSQ_DEBUG("wrote ACK frame of size %d", len); 1389 1390 /* Generate STOP_WAITING frame */ 1391 if ((mc->mc_flags & MC_STOP_WAIT_ON) && mc->mc_sent_packnos) 1392 { 1393 lunack = least_unacked(mc); 1394 len = mc->mc_conn.cn_pf->pf_gen_stop_waiting_frame(packet_out->po_data + 1395 packet_out->po_data_sz, 1396 lsquic_packet_out_avail(packet_out), packet_out->po_packno, 1397 lsquic_packet_out_packno_bits(packet_out), lunack); 1398 if (len < 0) 1399 { 1400 LSQ_WARN("could not generate STOP_WAITING frame"); 1401 return -1; 1402 } 1403 packet_out->po_data_sz += len; 1404 packet_out->po_regen_sz += len; 1405 packet_out->po_frame_types |= 1 << QUIC_FRAME_STOP_WAITING; 1406 LSQ_DEBUG("wrote STOP_WAITING frame of size %d", len); 1407 EV_LOG_GENERATED_STOP_WAITING_FRAME(LSQUIC_LOG_CONN_ID, lunack); 1408 } 1409 else if (mc->mc_flags & MC_STOP_WAIT_ON) 1410 LSQ_DEBUG("nothing sent: no need to generate STOP_WAITING frame"); 1411 1412 mc->mc_flags |= MC_UNSENT_ACK; 1413 return 0; 1414} 1415 1416 1417static int 1418calc_retx_timeout (const struct mini_conn *mc) 1419{ 1420 lsquic_time_t to; 1421 to = lsquic_rtt_stats_get_srtt(&mc->mc_rtt_stats); 1422 if (to) 1423 { 1424 to += to / 2; 1425 if (to < 10000) 1426 to = 10000; 1427 } 1428 else 1429 to = 300000; 1430 return to << mc->mc_hsk_count; 1431} 1432 1433 1434static void 1435return_enc_data (struct mini_conn *mc, struct lsquic_packet_out *packet_out) 1436{ 1437 mc->mc_enpub->enp_pmi->pmi_return(mc->mc_enpub->enp_pmi_ctx, 1438 mc->mc_path.np_peer_ctx, packet_out->po_enc_data, 1439 lsquic_packet_out_ipv6(packet_out)); 1440 packet_out->po_flags &= ~PO_ENCRYPTED; 1441 packet_out->po_enc_data = NULL; 1442} 1443 1444 1445static int 1446repackage_packet (struct mini_conn *mc, lsquic_packet_out_t *packet_out) 1447{ 1448 const lsquic_packno_t oldno = packet_out->po_packno; 1449 const lsquic_packno_t packno = next_packno(mc); 1450 if (packno > MINICONN_MAX_PACKETS) 1451 return -1; 1452 1453 LSQ_DEBUG("Packet %"PRIu64" repackaged for resending as packet %"PRIu64, 1454 oldno, packno); 1455 EV_LOG_CONN_EVENT(LSQUIC_LOG_CONN_ID, "packet %"PRIu64" repackaged for " 1456 "resending as packet %"PRIu64, oldno, packno); 1457 packet_out->po_packno = packno; 1458 packet_out->po_flags &= ~PO_SENT; 1459 if (packet_out->po_flags & PO_ENCRYPTED) 1460 return_enc_data(mc, packet_out); 1461 TAILQ_INSERT_TAIL(&mc->mc_packets_out, packet_out, po_next); 1462 return 0; 1463} 1464 1465 1466static int 1467handle_losses_and_have_unsent (struct mini_conn *mc, lsquic_time_t now) 1468{ 1469 TAILQ_HEAD(, lsquic_packet_out) lost_packets = 1470 TAILQ_HEAD_INITIALIZER(lost_packets); 1471 lsquic_packet_out_t *packet_out, *next; 1472 lsquic_time_t retx_to = 0; 1473 unsigned n_to_send = 0; 1474 1475 for (packet_out = TAILQ_FIRST(&mc->mc_packets_out); packet_out; 1476 packet_out = next) 1477 { 1478 next = TAILQ_NEXT(packet_out, po_next); 1479 if (packet_out->po_flags & PO_SENT) 1480 { 1481 if (0 == retx_to) 1482 retx_to = calc_retx_timeout(mc); 1483 if (packet_out->po_sent + retx_to < now) 1484 { 1485 LSQ_DEBUG("packet %"PRIu64" has been lost (rto: %"PRIu64")", 1486 packet_out->po_packno, retx_to); 1487 TAILQ_REMOVE(&mc->mc_packets_out, packet_out, po_next); 1488 TAILQ_INSERT_TAIL(&lost_packets, packet_out, po_next); 1489 mc->mc_lost_packnos |= MCONN_PACKET_MASK(packet_out->po_packno); 1490 MCHIST_APPEND(mc, MCHE_PACKET_LOST); 1491 } 1492 } 1493 else 1494 ++n_to_send; 1495 } 1496 1497 mc->mc_hsk_count += !TAILQ_EMPTY(&lost_packets); 1498 1499 while ((packet_out = TAILQ_FIRST(&lost_packets))) 1500 { 1501 TAILQ_REMOVE(&lost_packets, packet_out, po_next); 1502 if ((packet_out->po_frame_types & GQUIC_FRAME_RETRANSMITTABLE_MASK) 1503 && 0 == repackage_packet(mc, packet_out)) 1504 ++n_to_send; 1505 else 1506 mini_destroy_packet(mc, packet_out); 1507 } 1508 1509 return n_to_send > 0; 1510} 1511 1512 1513static int 1514warning_is_warranted (const struct mini_conn *mc) 1515{ 1516 return (mc->mc_flags & (MC_HSK_ERR|MC_OO_PACKNOS)) 1517 || 0x1C /* QUIC_HANDSHAKE_FAILED */ == mc->mc_error_code 1518 || 0x1D /* QUIC_CRYPTO_TAGS_OUT_OF_ORDER */ == mc->mc_error_code 1519 || 0x1E /* QUIC_CRYPTO_TOO_MANY_ENTRIES */ == mc->mc_error_code 1520 || 0x1F /* QUIC_CRYPTO_INVALID_VALUE_LENGTH */ == mc->mc_error_code 1521 || 0x21 /* QUIC_INVALID_CRYPTO_MESSAGE_TYPE */ == mc->mc_error_code 1522 || 0x22 /* QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER */ == mc->mc_error_code 1523 || 0x23 /* QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND */ == mc->mc_error_code 1524 || 0x24 /* QUIC_CRYPTO_MESSAGE_PARAMETER_NO_OVERLAP */ == mc->mc_error_code 1525 || 0x29 /* QUIC_CRYPTO_TOO_MANY_REJECTS */ == mc->mc_error_code 1526 || 0x2A /* QUIC_PROOF_INVALID */ == mc->mc_error_code 1527 || 0x2B /* QUIC_CRYPTO_DUPLICATE_TAG */ == mc->mc_error_code 1528 || 0x2C /* QUIC_CRYPTO_ENCRYPTION_LEVEL_INCORRECT */ == mc->mc_error_code 1529 || 0x2D /* QUIC_CRYPTO_SERVER_CONFIG_EXPIRED */ == mc->mc_error_code 1530 || 0x35 /* QUIC_CRYPTO_SYMMETRIC_KEY_SETUP_FAILED */ == mc->mc_error_code 1531 ; 1532} 1533 1534 1535#if LSQUIC_KEEP_ENC_SESS_HISTORY 1536static void 1537maybe_log_enc_sess_history (const struct mini_conn *mc) 1538{ 1539 char eshist[ESHIST_STR_SIZE]; 1540 enum lsq_log_level log_level; 1541 const char *ua; 1542 1543 if (warning_is_warranted(mc)) 1544 log_level = LSQ_LOG_WARN; 1545 else 1546 log_level = LSQ_LOG_DEBUG; 1547 1548 if (mc->mc_conn.cn_enc_session) 1549 { 1550 mc->mc_conn.cn_esf.g->esf_get_hist(mc->mc_conn.cn_enc_session, eshist); 1551 ua = mc->mc_conn.cn_esf.g->esf_get_ua(mc->mc_conn.cn_enc_session); 1552 LSQ_LOG1(log_level, "enc hist %s; User-Agent: %s", eshist, 1553 ua ? ua : "<not set>"); 1554 } 1555 else 1556 LSQ_LOG1(log_level, "enc session gone: no history to log"); 1557} 1558 1559 1560#endif 1561 1562 1563 1564 1565static int 1566have_packets_to_send (struct mini_conn *mc, lsquic_time_t now) 1567{ 1568 return handle_losses_and_have_unsent(mc, now); 1569} 1570 1571 1572static enum tick_st 1573mini_conn_ci_tick (struct lsquic_conn *lconn, lsquic_time_t now) 1574{ 1575 struct mini_conn *mc = (struct mini_conn *) lconn; 1576 enum tick_st tick; 1577 1578 ++mc->mc_n_ticks; 1579 1580 if (mc->mc_created + mc->mc_enpub->enp_settings.es_handshake_to < now) 1581 { 1582 LSQ_DEBUG("connection expired: closing"); 1583 tick = TICK_CLOSE; 1584 goto end; 1585 } 1586 1587 if (mc->mc_flags & MC_ERROR) 1588 { 1589 tick = TICK_CLOSE; 1590 goto end; 1591 } 1592 1593 1594 if ((mc->mc_flags & (MC_UNSENT_ACK|MC_GEN_ACK)) == MC_GEN_ACK) 1595 { 1596 if (0 != generate_ack_and_stop_waiting(mc, now)) 1597 { 1598 mc->mc_flags |= MC_ERROR; 1599 tick = TICK_CLOSE; 1600 goto end; 1601 } 1602 else 1603 mc->mc_flags &= ~MC_GEN_ACK; 1604 } 1605 1606 if (have_packets_to_send(mc, now)) 1607 tick = TICK_SEND; 1608 else 1609 tick = TICK_QUIET; 1610 1611 if (mc->mc_flags & MC_PROMOTE) 1612 tick |= TICK_PROMOTE; 1613 1614 end: 1615#if LSQUIC_KEEP_ENC_SESS_HISTORY 1616 if (tick & (TICK_CLOSE|TICK_PROMOTE)) 1617 maybe_log_enc_sess_history(mc); 1618#endif 1619 1620 return tick; 1621} 1622 1623 1624static void 1625process_packet (struct mini_conn *mc, struct lsquic_packet_in *packet_in) 1626{ 1627 switch (process_regular_packet(mc, packet_in)) 1628 { 1629 case PRP_KEEP: 1630 assert(packet_in->pi_flags & PI_OWN_DATA); 1631 lsquic_packet_in_upref(packet_in); 1632 TAILQ_INSERT_TAIL(&mc->mc_packets_in, packet_in, pi_next); 1633 if (mc->mc_flags & MC_HAVE_NEW_HSK) 1634 { 1635 if (0 != continue_handshake(mc)) 1636 mc->mc_flags |= MC_ERROR; 1637 mc->mc_flags &= ~MC_HAVE_NEW_HSK; 1638 } 1639 break; 1640 case PRP_DEFER: 1641 assert(packet_in->pi_flags & PI_OWN_DATA); 1642 lsquic_packet_in_upref(packet_in); 1643 if (mc->mc_n_deferred < MINI_CONN_MAX_DEFERRED) 1644 { 1645 TAILQ_INSERT_TAIL(&mc->mc_deferred, packet_in, pi_next); 1646 ++mc->mc_n_deferred; 1647 } 1648 else 1649 LSQ_DEBUG("won't defer more than %u packets: drop", 1650 MINI_CONN_MAX_DEFERRED); 1651 break; 1652 case PRP_ERROR: 1653 mc->mc_flags |= MC_ERROR; 1654 break; 1655 case PRP_DROP: 1656 break; 1657 } 1658} 1659 1660 1661/* Keep deferred list ordered by packet number, so that we can process all 1662 * of them in a single pass. 1663 */ 1664static void 1665insert_into_deferred (struct mini_conn *mc, lsquic_packet_in_t *new_packet) 1666{ 1667 lsquic_packet_in_t *packet_in; 1668 1669 lsquic_packet_in_upref(new_packet); 1670 1671 TAILQ_FOREACH(packet_in, &mc->mc_deferred, pi_next) 1672 if (packet_in->pi_packno > new_packet->pi_packno) 1673 break; 1674 1675 if (packet_in) 1676 TAILQ_INSERT_BEFORE(packet_in, new_packet, pi_next); 1677 else 1678 TAILQ_INSERT_TAIL(&mc->mc_deferred, new_packet, pi_next); 1679 ++mc->mc_n_deferred; 1680} 1681 1682 1683static void 1684process_deferred_packets (struct mini_conn *mc) 1685{ 1686 lsquic_packet_in_t *last, *packet_in; 1687 int reached_last; 1688 1689 last = TAILQ_LAST(&mc->mc_deferred, head_packet_in); 1690 do 1691 { 1692 packet_in = TAILQ_FIRST(&mc->mc_deferred); 1693 TAILQ_REMOVE(&mc->mc_deferred, packet_in, pi_next); 1694 --mc->mc_n_deferred; 1695 process_packet(mc, packet_in); 1696 reached_last = packet_in == last; 1697 lsquic_packet_in_put(&mc->mc_enpub->enp_mm, packet_in); 1698 } 1699 while (!reached_last); 1700} 1701 1702 1703#if LSQUIC_RECORD_INORD_HIST 1704/* FIXME This does not work for Q050, where 0 is a valid packet number. */ 1705/* Packet number is encoded as a sequence of 1-bits and stored in mc_inord_hist 1706 * separated by 0 bits. For example, sequence of packet numbers 3, 2, 1 would 1707 * be encoded as (starting with LSB) 1110110100000000... This is not the most 1708 * space-efficient scheme, but it is simple to implement and should suffice for 1709 * our purposes. 1710 */ 1711static void 1712record_inord_packno (struct mini_conn *mc, lsquic_packno_t packno) 1713{ 1714 int n_avail; 1715 lsquic_packno_t mask; 1716 1717 for ( ; mc->mc_inord_idx < sizeof(mc->mc_inord_hist) / 1718 sizeof(mc->mc_inord_hist[0]); ++mc->mc_inord_idx) 1719 { 1720 if (mc->mc_inord_hist[ mc->mc_inord_idx ]) 1721 n_avail = __builtin_clzll(mc->mc_inord_hist[ mc->mc_inord_idx ]) - 1; 1722 else 1723 n_avail = sizeof(mc->mc_inord_hist[ mc->mc_inord_idx ]) * 8; 1724 if (n_avail >= (int) packno) 1725 { 1726 mask = (1ULL << (int) packno) - 1; 1727 mask <<= sizeof(mc->mc_inord_hist[ mc->mc_inord_idx ]) * 8 - n_avail; 1728 mc->mc_inord_hist[ mc->mc_inord_idx ] |= mask; 1729 return; /* Success */ 1730 } 1731 } 1732} 1733 1734 1735#if __GNUC__ 1736# define ctz __builtin_ctzll 1737#else 1738static unsigned 1739ctz (unsigned long long x) 1740{ 1741 unsigned n = 0; 1742 if (0 == (x & ((1ULL << 32) - 1))) { n += 32; x >>= 32; } 1743 if (0 == (x & ((1ULL << 16) - 1))) { n += 16; x >>= 16; } 1744 if (0 == (x & ((1ULL << 8) - 1))) { n += 8; x >>= 8; } 1745 if (0 == (x & ((1ULL << 4) - 1))) { n += 4; x >>= 4; } 1746 if (0 == (x & ((1ULL << 2) - 1))) { n += 2; x >>= 2; } 1747 if (0 == (x & ((1ULL << 1) - 1))) { n += 1; x >>= 1; } 1748 return n; 1749} 1750 1751 1752#endif 1753 1754 1755static void 1756inord_to_str (const struct mini_conn *mc, char *buf, size_t bufsz) 1757{ 1758 unsigned long long hist; 1759 size_t off; 1760 ssize_t nw; 1761 unsigned n; 1762 int n_trail; 1763 1764 off = 0; 1765 for (n = 0; n < sizeof(mc->mc_inord_hist) / 1766 sizeof(mc->mc_inord_hist[0]); ++n) 1767 { 1768 hist = mc->mc_inord_hist[n]; 1769 while (hist) 1770 { 1771 n_trail = ctz(~hist); 1772 nw = snprintf(buf + off, bufsz - off, 1773 /* No spaces are included on purpose: this makes it a single 1774 * field and thus easy to process log using standard command- 1775 * line tools, such as sork -k, for example. 1776 */ 1777 (off ? ",%d" : "%d"), n_trail); 1778 if ((size_t) nw > bufsz - off || nw < 0) 1779 break; 1780 off += nw; 1781 hist >>= n_trail + 1; 1782 } 1783 } 1784 buf[ bufsz - 1 ] = '\0'; /* CYA */ 1785} 1786 1787 1788#endif 1789 1790 1791static void 1792mini_conn_ci_packet_in (struct lsquic_conn *lconn, 1793 struct lsquic_packet_in *packet_in) 1794{ 1795 struct mini_conn *mc = (struct mini_conn *) lconn; 1796 1797#if LSQUIC_RECORD_INORD_HIST 1798 record_inord_packno(mc, packet_in->pi_packno); 1799#endif 1800#if 0 1801 /* A convenient way to test lsquic_is_valid_hs_packet(): */ 1802 if (!(mc->mc_sent_packnos)) 1803 assert(lsquic_is_valid_hs_packet(NULL, packet_in->pi_data, 1804 packet_in->pi_data_sz)); 1805#endif 1806 1807 if (mc->mc_flags & MC_ERROR) 1808 { 1809 LSQ_DEBUG("error state: ignore packet %"PRIu64, packet_in->pi_packno); 1810 return; 1811 } 1812 1813 if (lsquic_packet_in_is_gquic_prst(packet_in)) 1814 { 1815 LSQ_INFO("received reset packet"); 1816 mc->mc_flags |= MC_ERROR; 1817 MCHIST_APPEND(mc, MCHE_PRST_IN); 1818 return; 1819 } 1820 1821 LSQ_DEBUG("packet in: %"PRIu64, packet_in->pi_packno); 1822 EV_LOG_PACKET_IN(LSQUIC_LOG_CONN_ID, packet_in); 1823 1824 1825 /* Check receive history */ 1826 if (0 == packet_in->pi_packno) 1827 { 1828 LSQ_DEBUG("invalid packet number 0"); 1829 mc->mc_flags |= MC_ERROR; 1830 MCHIST_APPEND(mc, MCHE_PACKET0_IN); 1831 return; 1832 } 1833 if (packet_in->pi_packno > MINICONN_MAX_PACKETS) 1834 { 1835 LSQ_DEBUG("packet number %"PRIu64" is too large (max %zd)", 1836 packet_in->pi_packno, MINICONN_MAX_PACKETS); 1837 mc->mc_flags |= MC_ERROR; 1838 MCHIST_APPEND(mc, MCHE_PACKET2LARGE_IN); 1839 return; 1840 } 1841 if (MCONN_PACKET_MASK(packet_in->pi_packno) & mc->mc_received_packnos) 1842 { 1843 LSQ_DEBUG("duplicate packet %"PRIu64", ignoring", packet_in->pi_packno); 1844 MCHIST_APPEND(mc, MCHE_PACKET_DUP_IN); 1845 return; 1846 } 1847 1848 if (TAILQ_EMPTY(&mc->mc_deferred)) 1849 process_packet(mc, packet_in); 1850 else if (mc->mc_n_deferred < MINI_CONN_MAX_DEFERRED) 1851 { 1852 insert_into_deferred(mc, packet_in); 1853 process_deferred_packets(mc); 1854 } 1855 else 1856 LSQ_DEBUG("won't defer more than %u packets: drop", 1857 MINI_CONN_MAX_DEFERRED); 1858} 1859 1860 1861/* Q050 is different is that packet numbers are not known until after the 1862 * packet is decrypted, so we have to follow different logic here. 1863 */ 1864static void 1865mini_conn_ci_Q050_packet_in (struct lsquic_conn *lconn, 1866 struct lsquic_packet_in *packet_in) 1867{ 1868 struct mini_conn *mc = (struct mini_conn *) lconn; 1869 enum proc_rp prp; 1870 1871 if (mc->mc_flags & MC_ERROR) 1872 { 1873 LSQ_DEBUG("error state: ignore packet"); 1874 return; 1875 } 1876 1877 1878 if (!mc->mc_conn.cn_enc_session) 1879 { 1880 mc->mc_conn.cn_enc_session = 1881 mc->mc_conn.cn_esf.g->esf_create_server(&mc->mc_conn, 1882 mc->mc_conn.cn_cid, mc->mc_enpub); 1883 if (!mc->mc_conn.cn_enc_session) 1884 { 1885 LSQ_WARN("cannot create new enc session"); 1886 mc->mc_flags |= MC_ERROR; 1887 return; 1888 } 1889 MCHIST_APPEND(mc, MCHE_NEW_ENC_SESS); 1890 } 1891 1892 assert(!(packet_in->pi_flags & PI_DECRYPTED)); 1893 prp = conn_decrypt_packet_or(mc, packet_in); 1894 switch (prp) 1895 { 1896 case PRP_KEEP: 1897 break; 1898 case PRP_DROP: 1899 return; 1900 case PRP_ERROR: 1901 mc->mc_flags |= MC_ERROR; 1902 return; 1903 default: 1904 if (mc->mc_n_deferred >= MINI_CONN_MAX_DEFERRED) 1905 { 1906 LSQ_DEBUG("won't defer more than %u packets: drop", 1907 MINI_CONN_MAX_DEFERRED); 1908 return; 1909 } 1910 assert(prp == PRP_DEFER); 1911 assert(packet_in->pi_flags & PI_OWN_DATA); 1912 lsquic_packet_in_upref(packet_in); 1913 TAILQ_INSERT_TAIL(&mc->mc_deferred, packet_in, pi_next); 1914 ++mc->mc_n_deferred; 1915 return; 1916 } 1917 1918 assert(prp == PRP_KEEP); 1919 process_packet(mc, packet_in); 1920} 1921 1922 1923static struct lsquic_packet_out * 1924mini_conn_ci_next_packet_to_send (struct lsquic_conn *lconn, 1925 const struct to_coal *to_coal_UNUSED) 1926{ 1927 struct mini_conn *mc = (struct mini_conn *) lconn; 1928 lsquic_packet_out_t *packet_out; 1929 1930 assert(NULL == to_coal_UNUSED); 1931 TAILQ_FOREACH(packet_out, &mc->mc_packets_out, po_next) 1932 { 1933 if (packet_out->po_flags & PO_SENT) 1934 continue; 1935 packet_out->po_flags |= PO_SENT; 1936 LSQ_DEBUG("packet_to_send: %"PRIu64, packet_out->po_packno); 1937 return packet_out; 1938 } 1939 return NULL; 1940} 1941 1942 1943static void 1944mini_conn_ci_packet_sent (struct lsquic_conn *lconn, 1945 struct lsquic_packet_out *packet_out) 1946{ 1947 struct mini_conn *mc = (struct mini_conn *) lconn; 1948 mc->mc_sent_packnos |= MCONN_PACKET_MASK(packet_out->po_packno); 1949 if (packet_out->po_frame_types & (1 << QUIC_FRAME_ACK)) 1950 { 1951 assert(mc->mc_flags & MC_UNSENT_ACK); 1952 mc->mc_flags &= ~MC_UNSENT_ACK; 1953 } 1954 LSQ_DEBUG("%s: packet %"PRIu64" sent", __func__, packet_out->po_packno); 1955 MCHIST_APPEND(mc, MCHE_PACKET_SENT); 1956} 1957 1958 1959static void 1960mini_conn_ci_packet_not_sent (struct lsquic_conn *lconn, 1961 struct lsquic_packet_out *packet_out) 1962{ 1963 struct mini_conn *mc = (struct mini_conn *) lconn; 1964 packet_out->po_flags &= ~PO_SENT; 1965 LSQ_DEBUG("%s: packet %"PRIu64" not sent", __func__, packet_out->po_packno); 1966 MCHIST_APPEND(mc, MCHE_PACKET_DELAYED); 1967} 1968 1969 1970static void 1971mini_conn_ci_destroy (struct lsquic_conn *lconn) 1972{ 1973 assert(!(lconn->cn_flags & LSCONN_HASHED)); 1974 struct mini_conn *mc = (struct mini_conn *) lconn; 1975 lsquic_packet_in_t *packet_in; 1976 mconn_packno_set_t still_deferred = 0, in_flight; 1977 enum lsq_log_level log_level; 1978#if LSQUIC_RECORD_INORD_HIST 1979 char inord_str[0x100]; 1980#endif 1981 while ((packet_in = TAILQ_FIRST(&mc->mc_packets_in))) 1982 { 1983 TAILQ_REMOVE(&mc->mc_packets_in, packet_in, pi_next); 1984 lsquic_packet_in_put(&mc->mc_enpub->enp_mm, packet_in); 1985 } 1986 while ((packet_in = TAILQ_FIRST(&mc->mc_deferred))) 1987 { 1988 TAILQ_REMOVE(&mc->mc_deferred, packet_in, pi_next); 1989 --mc->mc_n_deferred; 1990 still_deferred |= MCONN_PACKET_MASK(packet_in->pi_packno); 1991 lsquic_packet_in_put(&mc->mc_enpub->enp_mm, packet_in); 1992 } 1993 if (TAILQ_EMPTY(&mc->mc_packets_out)) 1994 in_flight = ~0ull; /* Indicates that packets were dropped before */ 1995 else 1996 in_flight = drop_packets_out(mc); 1997 if (mc->mc_conn.cn_enc_session) 1998 mc->mc_conn.cn_esf.g->esf_destroy(mc->mc_conn.cn_enc_session); 1999 log_level = warning_is_warranted(mc) ? LSQ_LOG_WARN : LSQ_LOG_DEBUG; 2000#if LSQUIC_RECORD_INORD_HIST 2001 if (LSQ_LOG_ENABLED(log_level)) 2002 inord_to_str(mc, inord_str, sizeof(inord_str)); 2003#endif 2004#if LSQUIC_KEEP_MINICONN_HISTORY 2005 const unsigned hist_idx = MCHIST_MASK & mc->mc_hist_idx; 2006 if (MCHE_EMPTY == mc->mc_hist_buf[ hist_idx ]) 2007 LSQ_LOG(log_level, "destroyed. Diagnostics: conn flags: 0x%X, " 2008 "mc flags: 0x%X, " 2009#if LSQUIC_RECORD_INORD_HIST 2010 "incoming-history (trunc: %d) %s, " 2011#endif 2012 "received: %"PRIX64", sent: %"PRIX64", lost: %"PRIX64", " 2013 "deferred: %"PRIX64", still-deferred: %"PRIX64", " 2014 "dropped: %"PRIX64", in-flight: %"PRIX64", acked: %"PRIX64", " 2015 "error_code: 0x%X, ticks: %hu, pack size: %hu, " 2016 "lifetime: %"PRIu64" usec, version: %s, " 2017 "mc hist: %.*s", mc->mc_conn.cn_flags, 2018 mc->mc_flags, 2019#if LSQUIC_RECORD_INORD_HIST 2020 mc->mc_inord_idx >= sizeof(mc->mc_inord_hist) / 2021 sizeof(mc->mc_inord_hist[0]), inord_str, 2022#endif 2023 mc->mc_received_packnos, mc->mc_sent_packnos, mc->mc_lost_packnos, 2024 mc->mc_deferred_packnos, still_deferred, 2025 mc->mc_dropped_packnos, in_flight, mc->mc_acked_packnos, 2026 mc->mc_error_code, mc->mc_n_ticks, mc->mc_path.np_pack_size, 2027 lsquic_time_now() - mc->mc_created, 2028 lsquic_ver2str[mc->mc_conn.cn_version], 2029 (int) hist_idx, mc->mc_hist_buf); 2030 else 2031 LSQ_LOG(log_level, "destroyed. Diagnostics: conn flags: 0x%X, " 2032 "mc flags: 0x%X, " 2033#if LSQUIC_RECORD_INORD_HIST 2034 "incoming-history (trunc: %d) %s, " 2035#endif 2036 "received: %"PRIX64", sent: %"PRIX64", lost: %"PRIX64", " 2037 "deferred: %"PRIX64", still-deferred: %"PRIX64", " 2038 "dropped: %"PRIX64", in-flight: %"PRIX64", acked: %"PRIX64", " 2039 "error_code: 0x%X, ticks: %hu, pack size: %hu, " 2040 "lifetime: %"PRIu64" usec, version: %s, " 2041 "mc hist: %.*s%.*s", mc->mc_conn.cn_flags, 2042 mc->mc_flags, 2043#if LSQUIC_RECORD_INORD_HIST 2044 mc->mc_inord_idx >= sizeof(mc->mc_inord_hist) / 2045 sizeof(mc->mc_inord_hist[0]), inord_str, 2046#endif 2047 mc->mc_received_packnos, mc->mc_sent_packnos, mc->mc_lost_packnos, 2048 mc->mc_deferred_packnos, still_deferred, 2049 mc->mc_dropped_packnos, in_flight, mc->mc_acked_packnos, 2050 mc->mc_error_code, mc->mc_n_ticks, mc->mc_path.np_pack_size, 2051 lsquic_time_now() - mc->mc_created, 2052 lsquic_ver2str[mc->mc_conn.cn_version], 2053 (int) (sizeof(mc->mc_hist_buf) - hist_idx), 2054 mc->mc_hist_buf + hist_idx, (int) hist_idx, mc->mc_hist_buf); 2055#else 2056 if (LSQ_LOG_ENABLED(log_level)) 2057 lsquic_logger_log2(log_level, LSQUIC_LOGGER_MODULE, 2058 LSQUIC_LOG_CONN_ID, 2059 "destroyed. Diagnostics: conn flags: 0x%X, " 2060 "mc flags: 0x%X, " 2061#if LSQUIC_RECORD_INORD_HIST 2062 "incoming-history (trunc: %d) %s, " 2063#endif 2064 "received: %"PRIX64", sent: %"PRIX64", lost: %"PRIX64", " 2065 "deferred: %"PRIX64", still-deferred: %"PRIX64", " 2066 "dropped: %"PRIX64", in-flight: %"PRIX64", acked: %"PRIX64", " 2067 "error_code: 0x%X, ticks: %hu, pack size: %hu, " 2068 "lifetime: %"PRIu64" usec", 2069 mc->mc_conn.cn_flags, 2070 mc->mc_flags, 2071#if LSQUIC_RECORD_INORD_HIST 2072 mc->mc_inord_idx >= sizeof(mc->mc_inord_hist) / 2073 sizeof(mc->mc_inord_hist[0]), inord_str, 2074#endif 2075 mc->mc_received_packnos, mc->mc_sent_packnos, mc->mc_lost_packnos, 2076 mc->mc_deferred_packnos, still_deferred, 2077 mc->mc_dropped_packnos, in_flight, mc->mc_acked_packnos, 2078 mc->mc_error_code, mc->mc_n_ticks, mc->mc_path.np_pack_size, 2079 lsquic_time_now() - mc->mc_created); 2080#endif 2081 EV_LOG_CONN_EVENT(LSQUIC_LOG_CONN_ID, "mini connection destroyed"); 2082 lsquic_malo_put(mc); 2083} 2084 2085 2086static struct lsquic_engine * 2087mini_conn_ci_get_engine (struct lsquic_conn *lconn) 2088{ 2089 struct mini_conn *mc = (struct mini_conn *) lconn; 2090 return mc->mc_enpub->enp_engine; 2091} 2092 2093 2094static void 2095mini_conn_ci_hsk_done (struct lsquic_conn *lconn, enum lsquic_hsk_status status) 2096{ 2097 assert(0); 2098} 2099 2100 2101/* A mini connection is only tickable if it has unsent packets. This can 2102 * occur when packet sending is delayed. 2103 * 2104 * Otherwise, a mini connection is not tickable: Either there are incoming 2105 * packets, in which case, the connection is going to be ticked, or there is 2106 * an alarm pending, in which case it will be handled via the attq. 2107 */ 2108static int 2109mini_conn_ci_is_tickable (struct lsquic_conn *lconn) 2110{ 2111 struct mini_conn *const mc = (struct mini_conn *) lconn; 2112 const struct lsquic_packet_out *packet_out; 2113 2114 if (mc->mc_enpub->enp_flags & ENPUB_CAN_SEND) 2115 TAILQ_FOREACH(packet_out, &mc->mc_packets_out, po_next) 2116 if (!(packet_out->po_flags & PO_SENT)) 2117 return 1; 2118 2119 return 0; 2120} 2121 2122 2123static lsquic_time_t 2124mini_conn_ci_next_tick_time (struct lsquic_conn *lconn, unsigned *why) 2125{ 2126 struct mini_conn *mc = (struct mini_conn *) lconn; 2127 lsquic_packet_out_t *packet_out; 2128 lsquic_time_t exp_time, retx_time; 2129 2130 exp_time = mc->mc_created + mc->mc_enpub->enp_settings.es_handshake_to; 2131 2132 TAILQ_FOREACH(packet_out, &mc->mc_packets_out, po_next) 2133 if (packet_out->po_flags & PO_SENT) 2134 { 2135 retx_time = packet_out->po_sent + calc_retx_timeout(mc); 2136 if (retx_time < exp_time) 2137 { 2138 *why = N_AEWS + AL_RETX_HSK; 2139 return retx_time; 2140 } 2141 else 2142 { 2143 *why = AEW_MINI_EXPIRE; 2144 return exp_time; 2145 } 2146 } 2147 2148 *why = AEW_MINI_EXPIRE; 2149 return exp_time; 2150} 2151 2152 2153static void 2154mini_conn_ci_client_call_on_new (struct lsquic_conn *lconn) 2155{ 2156 assert(0); 2157} 2158 2159 2160static void 2161mini_conn_ci_internal_error (struct lsquic_conn *lconn, 2162 const char *format, ...) 2163{ 2164 struct mini_conn *mc = (struct mini_conn *) lconn; 2165 LSQ_INFO("internal error reported"); 2166 mc->mc_flags |= MC_ERROR; 2167} 2168 2169 2170/* This function should not be called, as this is specific to IETF QUIC */ 2171static void 2172mini_conn_ci_abort_error (struct lsquic_conn *lconn, int is_app, 2173 unsigned error_code, const char *fmt, ...) 2174{ 2175 struct mini_conn *mc = (struct mini_conn *) lconn; 2176 assert(0); 2177 LSQ_WARN("(GQUIC) abort error is called unexpectedly"); 2178 mc->mc_flags |= MC_ERROR; 2179} 2180 2181 2182static void 2183mini_conn_ci_tls_alert (struct lsquic_conn *lconn, uint8_t alert) 2184{ 2185 assert(0); 2186} 2187 2188 2189static unsigned char 2190mini_conn_ci_record_addrs (struct lsquic_conn *lconn, void *peer_ctx, 2191 const struct sockaddr *local_sa, const struct sockaddr *peer_sa) 2192{ 2193 struct mini_conn *mc = (struct mini_conn *) lconn; 2194 struct lsquic_packet_out *packet_out; 2195 size_t len; 2196 2197 2198 if (NP_IS_IPv6(&mc->mc_path) != (AF_INET6 == peer_sa->sa_family)) 2199 TAILQ_FOREACH(packet_out, &mc->mc_packets_out, po_next) 2200 if ((packet_out->po_flags & (PO_SENT|PO_ENCRYPTED)) == PO_ENCRYPTED) 2201 return_enc_data(mc, packet_out); 2202 2203 len = local_sa->sa_family == AF_INET ? sizeof(struct sockaddr_in) 2204 : sizeof(struct sockaddr_in6); 2205 2206 memcpy(mc->mc_path.np_peer_addr, peer_sa, len); 2207 memcpy(mc->mc_path.np_local_addr, local_sa, len); 2208 mc->mc_path.np_peer_ctx = peer_ctx; 2209 return 0; 2210} 2211 2212 2213static struct network_path * 2214mini_conn_ci_get_path (struct lsquic_conn *lconn, const struct sockaddr *sa) 2215{ 2216 struct mini_conn *mc = (struct mini_conn *) lconn; 2217 2218 return &mc->mc_path; 2219} 2220 2221 2222static const struct conn_iface mini_conn_iface_standard = { 2223 .ci_abort_error = mini_conn_ci_abort_error, 2224 .ci_client_call_on_new = mini_conn_ci_client_call_on_new, 2225 .ci_destroy = mini_conn_ci_destroy, 2226 .ci_get_engine = mini_conn_ci_get_engine, 2227 .ci_get_path = mini_conn_ci_get_path, 2228 .ci_hsk_done = mini_conn_ci_hsk_done, 2229 .ci_internal_error = mini_conn_ci_internal_error, 2230 .ci_is_tickable = mini_conn_ci_is_tickable, 2231 .ci_next_packet_to_send = mini_conn_ci_next_packet_to_send, 2232 .ci_next_tick_time = mini_conn_ci_next_tick_time, 2233 .ci_packet_in = mini_conn_ci_packet_in, 2234 .ci_packet_not_sent = mini_conn_ci_packet_not_sent, 2235 .ci_packet_sent = mini_conn_ci_packet_sent, 2236 .ci_record_addrs = mini_conn_ci_record_addrs, 2237 .ci_tick = mini_conn_ci_tick, 2238 .ci_tls_alert = mini_conn_ci_tls_alert, 2239}; 2240 2241 2242static const struct conn_iface mini_conn_iface_standard_Q050 = { 2243 .ci_abort_error = mini_conn_ci_abort_error, 2244 .ci_client_call_on_new = mini_conn_ci_client_call_on_new, 2245 .ci_destroy = mini_conn_ci_destroy, 2246 .ci_get_engine = mini_conn_ci_get_engine, 2247 .ci_get_path = mini_conn_ci_get_path, 2248 .ci_hsk_done = mini_conn_ci_hsk_done, 2249 .ci_internal_error = mini_conn_ci_internal_error, 2250 .ci_is_tickable = mini_conn_ci_is_tickable, 2251 .ci_next_packet_to_send = mini_conn_ci_next_packet_to_send, 2252 .ci_next_tick_time = mini_conn_ci_next_tick_time, 2253 .ci_packet_in = mini_conn_ci_Q050_packet_in, 2254 .ci_packet_not_sent = mini_conn_ci_packet_not_sent, 2255 .ci_packet_sent = mini_conn_ci_packet_sent, 2256 .ci_record_addrs = mini_conn_ci_record_addrs, 2257 .ci_tick = mini_conn_ci_tick, 2258 .ci_tls_alert = mini_conn_ci_tls_alert, 2259}; 2260 2261 2262typedef char largest_recv_holds_at_least_16_seconds[ 2263 ((1 << (sizeof(((struct mini_conn *) 0)->mc_largest_recv) * 8)) / 1000000 2264 >= 16) ? 1 : -1]; 2265 2266typedef char max_lifespan_smaller_than_largest_recv[ 2267 ((1 << (sizeof(((struct mini_conn *) 0)->mc_largest_recv) * 8)) > 2268 MAX_MINI_CONN_LIFESPAN_IN_USEC) ? 1 : -1]; 2269