lsquic_mini_conn_ietf.c revision 7483dee0
1/* Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc. See LICENSE. */ 2/* 3 * lsquic_mini_conn_ietf.c -- Mini connection used by the IETF QUIC 4 */ 5 6#include <assert.h> 7#include <errno.h> 8#include <inttypes.h> 9#include <stddef.h> 10#include <stdint.h> 11#include <string.h> 12#include <sys/queue.h> 13#include <stdlib.h> 14 15#include "lsquic.h" 16#include "lsquic_int_types.h" 17#include "lsquic_sizes.h" 18#include "lsquic_hash.h" 19#include "lsquic_conn.h" 20#include "lsquic_mm.h" 21#include "lsquic_malo.h" 22#include "lsquic_engine_public.h" 23#include "lsquic_packet_common.h" 24#include "lsquic_packet_in.h" 25#include "lsquic_packet_out.h" 26#include "lsquic_parse.h" 27#include "lsquic_rtt.h" 28#include "lsquic_util.h" 29#include "lsquic_enc_sess.h" 30#include "lsquic_mini_conn_ietf.h" 31#include "lsquic_ev_log.h" 32#include "lsquic_trans_params.h" 33#include "lsquic_ietf.h" 34#include "lsquic_packet_ietf.h" 35#include "lsquic_attq.h" 36#include "lsquic_alarmset.h" 37 38#define LSQUIC_LOGGER_MODULE LSQLM_MINI_CONN 39#define LSQUIC_LOG_CONN_ID lsquic_conn_log_cid(&conn->imc_conn) 40#include "lsquic_logger.h" 41 42#define MIN(a, b) ((a) < (b) ? (a) : (b)) 43#define MAX(a, b) ((a) > (b) ? (a) : (b)) 44 45static const struct conn_iface mini_conn_ietf_iface; 46 47static unsigned highest_bit_set (unsigned long long); 48 49static int 50imico_can_send (const struct ietf_mini_conn *, size_t); 51 52 53static const enum header_type el2hety[] = 54{ 55 [ENC_LEV_INIT] = HETY_HANDSHAKE, 56 [ENC_LEV_CLEAR] = HETY_INITIAL, 57 [ENC_LEV_FORW] = HETY_NOT_SET, 58 [ENC_LEV_EARLY] = 0, /* Invalid */ 59}; 60 61 62static void 63imico_destroy_packet (struct ietf_mini_conn *conn, 64 struct lsquic_packet_out *packet_out) 65{ 66 lsquic_packet_out_destroy(packet_out, conn->imc_enpub, 67 conn->imc_path.np_peer_ctx); 68} 69 70 71int 72lsquic_mini_conn_ietf_ecn_ok (const struct ietf_mini_conn *conn) 73{ 74 packno_set_t acked; 75 76 /* First flight has only Initial and Handshake packets */ 77 acked = conn->imc_acked_packnos[PNS_INIT] 78 | conn->imc_acked_packnos[PNS_HSK] 79 ; 80 return 0 != (conn->imc_ecn_packnos & acked); 81} 82 83 84#define imico_ecn_ok lsquic_mini_conn_ietf_ecn_ok 85 86 87static enum ecn 88imico_get_ecn (struct ietf_mini_conn *conn) 89{ 90 if (!conn->imc_enpub->enp_settings.es_ecn) 91 return ECN_NOT_ECT; 92 else if (!conn->imc_sent_packnos /* We set ECT0 in first flight */ 93 || imico_ecn_ok(conn)) 94 return ECN_ECT0; 95 else 96 return ECN_NOT_ECT; 97} 98 99 100static struct lsquic_packet_out * 101imico_get_packet_out (struct ietf_mini_conn *conn, 102 enum header_type header_type, size_t need) 103{ 104 struct lsquic_packet_out *packet_out; 105 enum ecn ecn; 106 107 if (need) 108 TAILQ_FOREACH(packet_out, &conn->imc_packets_out, po_next) 109 if (!(packet_out->po_flags & PO_SENT) 110 && packet_out->po_header_type == header_type 111 && lsquic_packet_out_avail(packet_out) >= need) 112 return packet_out; 113 114 if (conn->imc_next_packno >= MAX_PACKETS) 115 { 116 LSQ_DEBUG("ran out of outgoing packet numbers, won't allocate packet"); 117 return NULL; 118 } 119 120 packet_out = lsquic_packet_out_new(&conn->imc_enpub->enp_mm, NULL, 1, 121 &conn->imc_conn, IQUIC_PACKNO_LEN_1, NULL, NULL, &conn->imc_path); 122 if (!packet_out) 123 { 124 LSQ_WARN("could not allocate packet: %s", strerror(errno)); 125 return NULL; 126 } 127 128 packet_out->po_header_type = header_type; 129 packet_out->po_packno = conn->imc_next_packno++; 130 packet_out->po_flags |= PO_MINI; 131 lsquic_packet_out_set_pns(packet_out, lsquic_hety2pns[header_type]); 132 ecn = imico_get_ecn(conn); 133 packet_out->po_lflags |= ecn << POECN_SHIFT; 134 TAILQ_INSERT_TAIL(&conn->imc_packets_out, packet_out, po_next); 135 packet_out->po_loss_chain = packet_out; 136 return packet_out; 137} 138 139 140static struct ietf_mini_conn * 141cryst_get_conn (const struct mini_crypto_stream *cryst) 142{ 143 return (void *) 144 ((unsigned char *) (cryst - cryst->mcs_enc_level) 145 - offsetof(struct ietf_mini_conn, imc_streams)); 146} 147 148 149struct msg_ctx 150{ 151 const unsigned char *buf; 152 const unsigned char *const end; 153}; 154 155 156static size_t 157read_from_msg_ctx (void *ctx, void *buf, size_t len) 158{ 159 struct msg_ctx *msg_ctx = ctx; 160 if (len > (uintptr_t) (msg_ctx->end - msg_ctx->buf)) 161 len = msg_ctx->end - msg_ctx->buf; 162 memcpy(buf, msg_ctx->buf, len); 163 msg_ctx->buf += len; 164 return len; 165} 166 167 168static int 169imico_chlo_has_been_consumed (const struct ietf_mini_conn *conn) 170{ 171 return conn->imc_streams[ENC_LEV_CLEAR].mcs_read_off > 3 172 && conn->imc_streams[ENC_LEV_CLEAR].mcs_read_off >= conn->imc_ch_len; 173} 174 175 176static int 177imico_maybe_process_params (struct ietf_mini_conn *conn) 178{ 179 const struct transport_params *params; 180 181 if (imico_chlo_has_been_consumed(conn) 182 && (conn->imc_flags & (IMC_ENC_SESS_INITED|IMC_HAVE_TP)) 183 == IMC_ENC_SESS_INITED) 184 { 185 params = conn->imc_conn.cn_esf.i->esfi_get_peer_transport_params( 186 conn->imc_conn.cn_enc_session); 187 if (params) 188 { 189 conn->imc_flags |= IMC_HAVE_TP; 190 conn->imc_ack_exp = params->tp_ack_delay_exponent; 191 if (params->tp_set & (1 << TPI_MAX_UDP_PAYLOAD_SIZE)) 192 { 193 if (params->tp_numerics[TPI_MAX_UDP_PAYLOAD_SIZE] 194 < conn->imc_path.np_pack_size) 195 conn->imc_path.np_pack_size = 196 params->tp_numerics[TPI_MAX_UDP_PAYLOAD_SIZE]; 197 } 198 LSQ_DEBUG("read transport params, packet size is set to %hu bytes", 199 conn->imc_path.np_pack_size); 200 } 201 else 202 { 203 conn->imc_flags |= IMC_BAD_TRANS_PARAMS; 204 return -1; 205 } 206 } 207 208 return 0; 209} 210 211 212static ssize_t 213imico_stream_write (void *stream, const void *bufp, size_t bufsz) 214{ 215 struct mini_crypto_stream *const cryst = stream; 216 struct ietf_mini_conn *const conn = cryst_get_conn(cryst); 217 struct lsquic_conn *const lconn = &conn->imc_conn; 218 const struct parse_funcs *const pf = lconn->cn_pf; 219 struct msg_ctx msg_ctx = { bufp, (unsigned char *) bufp + bufsz, }; 220 struct lsquic_packet_out *packet_out; 221 size_t header_sz, need; 222 const unsigned char *p; 223 int len; 224 225 if (0 != imico_maybe_process_params(conn)) 226 return -1; 227 228 if (PNS_INIT == lsquic_enclev2pns[ cryst->mcs_enc_level ] 229 && (conn->imc_flags & IMC_IGNORE_INIT)) 230 { 231 LSQ_WARN("trying to write at the ignored Initial level"); 232 return bufsz; 233 } 234 235 while (msg_ctx.buf < msg_ctx.end) 236 { 237 header_sz = lconn->cn_pf->pf_calc_crypto_frame_header_sz( 238 cryst->mcs_write_off, msg_ctx.end - msg_ctx.buf); 239 need = header_sz + 1; 240 packet_out = imico_get_packet_out(conn, 241 el2hety[ cryst->mcs_enc_level ], need); 242 if (!packet_out) 243 return -1; 244 245 p = msg_ctx.buf; 246 len = pf->pf_gen_crypto_frame(packet_out->po_data + packet_out->po_data_sz, 247 lsquic_packet_out_avail(packet_out), cryst->mcs_write_off, 248 msg_ctx.end - msg_ctx.buf, read_from_msg_ctx, &msg_ctx); 249 if (len < 0) 250 return len; 251 EV_LOG_GENERATED_CRYPTO_FRAME(LSQUIC_LOG_CONN_ID, pf, 252 packet_out->po_data + packet_out->po_data_sz, len); 253 packet_out->po_data_sz += len; 254 packet_out->po_frame_types |= 1 << QUIC_FRAME_CRYPTO; 255 packet_out->po_flags |= PO_HELLO; 256 cryst->mcs_write_off += msg_ctx.buf - p; 257 } 258 259 assert(msg_ctx.buf == msg_ctx.end); 260 return bufsz; 261} 262 263 264static int 265imico_stream_flush (void *stream) 266{ 267 return 0; 268} 269 270 271static struct stream_frame * 272imico_find_stream_frame (const struct ietf_mini_conn *conn, 273 enum enc_level enc_level, unsigned read_off) 274{ 275 struct stream_frame *frame; 276 277 if (conn->imc_last_in.frame && enc_level == conn->imc_last_in.enc_level 278 && read_off == DF_ROFF(conn->imc_last_in.frame)) 279 return conn->imc_last_in.frame; 280 281 TAILQ_FOREACH(frame, &conn->imc_crypto_frames, next_frame) 282 if (enc_level == frame->stream_id && read_off == DF_ROFF(frame)) 283 return frame; 284 285 return NULL; 286} 287 288 289static void 290imico_read_chlo_size (struct ietf_mini_conn *conn, const unsigned char *buf, 291 size_t sz) 292{ 293 const unsigned char *const end = buf + sz; 294 295 assert(conn->imc_streams[ENC_LEV_CLEAR].mcs_read_off < 4); 296 switch (conn->imc_streams[ENC_LEV_CLEAR].mcs_read_off) 297 { 298 case 0: 299 if (buf == end) 300 return; 301 if (*buf != 1) 302 { 303 LSQ_DEBUG("Does not begin with ClientHello"); 304 conn->imc_flags |= IMC_ERROR; 305 return; 306 } 307 ++buf; 308 /* fall-through */ 309 case 1: 310 if (buf == end) 311 return; 312 if (*buf != 0) 313 { 314 LSQ_DEBUG("ClientHello larger than 16K"); 315 conn->imc_flags |= IMC_ERROR; 316 return; 317 } 318 ++buf; 319 /* fall-through */ 320 case 2: 321 if (buf == end) 322 return; 323 conn->imc_ch_len = *buf << 8; 324 ++buf; 325 /* fall-through */ 326 default: 327 if (buf == end) 328 return; 329 conn->imc_ch_len |= *buf; 330 } 331} 332 333 334static ssize_t 335imico_stream_readf (void *stream, 336 size_t (*readf)(void *, const unsigned char *, size_t, int), void *ctx) 337{ 338 struct mini_crypto_stream *const cryst = stream; 339 struct ietf_mini_conn *const conn = cryst_get_conn(cryst); 340 struct stream_frame *frame; 341 const unsigned char *buf; 342 size_t nread, total_read; 343 unsigned avail; 344 345 total_read = 0; 346 while ((frame = imico_find_stream_frame(conn, cryst->mcs_enc_level, 347 cryst->mcs_read_off))) 348 { 349 avail = DF_SIZE(frame) - frame->data_frame.df_read_off; 350 buf = frame->data_frame.df_data + frame->data_frame.df_read_off; 351 nread = readf(ctx, buf, avail, DF_FIN(frame)); 352 if (cryst->mcs_enc_level == ENC_LEV_CLEAR && cryst->mcs_read_off < 4) 353 imico_read_chlo_size(conn, buf, nread); 354 total_read += nread; 355 cryst->mcs_read_off += nread; 356 frame->data_frame.df_read_off += nread; 357 LSQ_DEBUG("read %zu bytes at offset %"PRIu64" on enc level %u", nread, 358 DF_ROFF(frame), cryst->mcs_enc_level); 359 if (DF_END(frame) == DF_ROFF(frame)) 360 { 361 if (frame == conn->imc_last_in.frame) 362 conn->imc_last_in.frame = NULL; 363 else 364 { 365 TAILQ_REMOVE(&conn->imc_crypto_frames, frame, next_frame); 366 --conn->imc_n_crypto_frames; 367 conn->imc_crypto_frames_sz -= DF_SIZE(frame); 368 lsquic_packet_in_put(&conn->imc_enpub->enp_mm, 369 frame->packet_in); 370 lsquic_malo_put(frame); 371 } 372 } 373 if (nread < avail) 374 break; 375 } 376 377 if (total_read > 0) 378 return total_read; 379 else 380 { 381 /* CRYPTO streams never end, so zero bytes read always means 382 * EWOULDBLOCK 383 */ 384 errno = EWOULDBLOCK; 385 return -1; 386 } 387} 388 389 390static int 391imico_stream_wantX (struct mini_crypto_stream *cryst, int bit, int is_want) 392{ 393 int old; 394 395 old = (cryst->mcs_flags & (1 << bit)) > 0; 396 cryst->mcs_flags &= ~(1 << bit); 397 cryst->mcs_flags |= !!is_want << bit; 398 return old; 399} 400 401 402static int 403imico_stream_wantwrite (void *stream, int is_want) 404{ 405 return imico_stream_wantX(stream, MCSBIT_WANTWRITE, is_want); 406} 407 408 409static int 410imico_stream_wantread (void *stream, int is_want) 411{ 412 return imico_stream_wantX(stream, MCSBIT_WANTREAD, is_want); 413} 414 415 416static enum enc_level 417imico_stream_enc_level (void *stream) 418{ 419 struct mini_crypto_stream *const cryst = stream; 420 return cryst->mcs_enc_level; 421} 422 423 424static const struct crypto_stream_if crypto_stream_if = 425{ 426 .csi_write = imico_stream_write, 427 .csi_flush = imico_stream_flush, 428 .csi_readf = imico_stream_readf, 429 .csi_wantwrite = imico_stream_wantwrite, 430 .csi_wantread = imico_stream_wantread, 431 .csi_enc_level = imico_stream_enc_level, 432}; 433 434 435static int 436is_first_packet_ok (const struct lsquic_packet_in *packet_in, 437 size_t udp_payload_size) 438{ 439 if (udp_payload_size < IQUIC_MIN_INIT_PACKET_SZ) 440 { 441 /* [draft-ietf-quic-transport-24] Section 14 */ 442 LSQ_LOG1(LSQ_LOG_DEBUG, "incoming UDP payload too small: %zu bytes", 443 udp_payload_size); 444 return 0; 445 } 446 /* TODO: Move decryption of the first packet into this function? */ 447 return 1; /* TODO */ 448} 449 450 451struct lsquic_conn * 452lsquic_mini_conn_ietf_new (struct lsquic_engine_public *enpub, 453 const struct lsquic_packet_in *packet_in, 454 enum lsquic_version version, int is_ipv4, const lsquic_cid_t *odcid, 455 size_t udp_payload_size) 456{ 457 struct ietf_mini_conn *conn; 458 enc_session_t *enc_sess; 459 enum enc_level i; 460 const struct enc_session_funcs_iquic *esfi; 461 462 if (!is_first_packet_ok(packet_in, udp_payload_size)) 463 return NULL; 464 465 conn = lsquic_malo_get(enpub->enp_mm.malo.mini_conn_ietf); 466 if (!conn) 467 { 468 LSQ_LOG1(LSQ_LOG_WARN, "cannot allocate mini connection: %s", 469 strerror(errno)); 470 return NULL; 471 } 472 memset(conn, 0, sizeof(*conn)); 473 conn->imc_conn.cn_if = &mini_conn_ietf_iface; 474 conn->imc_conn.cn_cces = conn->imc_cces; 475 conn->imc_conn.cn_n_cces = sizeof(conn->imc_cces) 476 / sizeof(conn->imc_cces[0]); 477 conn->imc_cces[0].cce_cid = packet_in->pi_dcid; 478 conn->imc_cces[0].cce_flags = CCE_USED; 479 conn->imc_conn.cn_cces_mask = 1; 480 lsquic_scid_from_packet_in(packet_in, &conn->imc_path.np_dcid); 481 LSQ_DEBUGC("recv SCID from client %"CID_FMT, CID_BITS(&conn->imc_cces[0].cce_cid)); 482 LSQ_DEBUGC("recv DCID from client %"CID_FMT, CID_BITS(&conn->imc_path.np_dcid)); 483 484 /* Generate new SCID. Since is not the original SCID, it is given 485 * a sequence number (0) and therefore can be retired by the client. 486 */ 487 lsquic_generate_cid(&conn->imc_conn.cn_cces[1].cce_cid, 488 enpub->enp_settings.es_scid_len); 489 LSQ_DEBUGC("generated SCID %"CID_FMT" at index %u, switching to it", 490 CID_BITS(&conn->imc_conn.cn_cces[1].cce_cid), 1); 491 conn->imc_conn.cn_cces[1].cce_flags = CCE_SEQNO | CCE_USED; 492 conn->imc_conn.cn_cces_mask |= 1u << 1; 493 conn->imc_conn.cn_cur_cce_idx = 1; 494 495 conn->imc_conn.cn_flags = LSCONN_MINI|LSCONN_IETF|LSCONN_SERVER; 496 conn->imc_conn.cn_version = version; 497 498 for (i = 0; i < N_ENC_LEVS; ++i) 499 { 500 conn->imc_streams[i].mcs_enc_level = i; 501 conn->imc_stream_ps[i] = &conn->imc_streams[i]; 502 } 503 504 esfi = select_esf_iquic_by_ver(version); 505 enc_sess = esfi->esfi_create_server(enpub, &conn->imc_conn, 506 &packet_in->pi_dcid, conn->imc_stream_ps, &crypto_stream_if, 507 &conn->imc_cces[0].cce_cid, &conn->imc_path.np_dcid); 508 if (!enc_sess) 509 { 510 lsquic_malo_put(conn); 511 return NULL; 512 } 513 514 conn->imc_enpub = enpub; 515 conn->imc_created = packet_in->pi_received; 516 conn->imc_path.np_pack_size = is_ipv4 ? IQUIC_MAX_IPv4_PACKET_SZ 517 : IQUIC_MAX_IPv6_PACKET_SZ; 518#ifndef NDEBUG 519 if (getenv("LSQUIC_CN_PACK_SIZE")) 520 conn->imc_path.np_pack_size = atoi(getenv("LSQUIC_CN_PACK_SIZE")); 521#endif 522 conn->imc_conn.cn_pf = select_pf_by_ver(version); 523 conn->imc_conn.cn_esf.i = esfi; 524 conn->imc_conn.cn_enc_session = enc_sess; 525 conn->imc_conn.cn_esf_c = select_esf_common_by_ver(version); 526 TAILQ_INIT(&conn->imc_packets_out); 527 TAILQ_INIT(&conn->imc_app_packets); 528 TAILQ_INIT(&conn->imc_crypto_frames); 529 if (odcid) 530 conn->imc_flags |= IMC_ADDR_VALIDATED; 531 532 LSQ_DEBUG("created mini connection object %p; max packet size=%hu", 533 conn, conn->imc_path.np_pack_size); 534 return &conn->imc_conn; 535} 536 537 538static void 539ietf_mini_conn_ci_client_call_on_new (struct lsquic_conn *lconn) 540{ 541 assert(0); 542} 543 544 545static void 546ietf_mini_conn_ci_destroy (struct lsquic_conn *lconn) 547{ 548 struct ietf_mini_conn *conn = (struct ietf_mini_conn *) lconn; 549 struct lsquic_packet_out *packet_out; 550 struct lsquic_packet_in *packet_in; 551 struct stream_frame *frame; 552 553 while ((packet_out = TAILQ_FIRST(&conn->imc_packets_out))) 554 { 555 TAILQ_REMOVE(&conn->imc_packets_out, packet_out, po_next); 556 imico_destroy_packet(conn, packet_out); 557 } 558 while ((packet_in = TAILQ_FIRST(&conn->imc_app_packets))) 559 { 560 TAILQ_REMOVE(&conn->imc_app_packets, packet_in, pi_next); 561 lsquic_packet_in_put(&conn->imc_enpub->enp_mm, packet_in); 562 } 563 while ((frame = TAILQ_FIRST(&conn->imc_crypto_frames))) 564 { 565 TAILQ_REMOVE(&conn->imc_crypto_frames, frame, next_frame); 566 lsquic_packet_in_put(&conn->imc_enpub->enp_mm, frame->packet_in); 567 lsquic_malo_put(frame); 568 } 569 if (lconn->cn_enc_session) 570 lconn->cn_esf.i->esfi_destroy(lconn->cn_enc_session); 571 LSQ_DEBUG("ietf_mini_conn_ci_destroyed"); 572 lsquic_malo_put(conn); 573} 574 575 576static struct lsquic_engine * 577ietf_mini_conn_ci_get_engine (struct lsquic_conn *lconn) 578{ 579 struct ietf_mini_conn *conn = (struct ietf_mini_conn *) lconn; 580 return conn->imc_enpub->enp_engine; 581} 582 583 584static void 585ietf_mini_conn_ci_hsk_done (struct lsquic_conn *lconn, 586 enum lsquic_hsk_status status) 587{ 588 struct ietf_mini_conn *conn = (struct ietf_mini_conn *) lconn; 589 590 switch (status) 591 { 592 case LSQ_HSK_OK: 593 case LSQ_HSK_RESUMED_OK: 594 conn->imc_flags |= IMC_HSK_OK; 595 conn->imc_conn.cn_flags |= LSCONN_HANDSHAKE_DONE; 596 LSQ_DEBUG("handshake OK"); 597 break; 598 default: 599 assert(0); 600 /* fall-through */ 601 case LSQ_HSK_FAIL: 602 conn->imc_flags |= IMC_HSK_FAILED|IMC_ERROR; 603 LSQ_INFO("handshake failed"); 604 break; 605 } 606} 607 608 609static void 610ietf_mini_conn_ci_tls_alert (struct lsquic_conn *lconn, uint8_t alert) 611{ 612 struct ietf_mini_conn *conn = (struct ietf_mini_conn *) lconn; 613 LSQ_DEBUG("got TLS alert %"PRIu8, alert); 614 conn->imc_flags |= IMC_ERROR|IMC_TLS_ALERT; 615 conn->imc_tls_alert = alert; 616} 617 618 619/* A mini connection is only tickable if it has unsent packets. This can 620 * occur when packet sending is delayed. 621 * 622 * Otherwise, a mini connection is not tickable: Either there are incoming 623 * packets, in which case, the connection is going to be ticked, or there is 624 * an alarm pending, in which case it will be handled via the attq. 625 */ 626static int 627ietf_mini_conn_ci_is_tickable (struct lsquic_conn *lconn) 628{ 629 struct ietf_mini_conn *const conn = (struct ietf_mini_conn *) lconn; 630 const struct lsquic_packet_out *packet_out; 631 size_t packet_size; 632 633 if (conn->imc_enpub->enp_flags & ENPUB_CAN_SEND) 634 TAILQ_FOREACH(packet_out, &conn->imc_packets_out, po_next) 635 if (!(packet_out->po_flags & PO_SENT)) 636 { 637 packet_size = lsquic_packet_out_total_sz(lconn, packet_out); 638 return imico_can_send(conn, packet_size); 639 } 640 641 return 0; 642} 643 644 645static int 646imico_can_send (const struct ietf_mini_conn *conn, size_t size) 647{ 648 return (conn->imc_flags & IMC_ADDR_VALIDATED) 649 || conn->imc_bytes_in * 3 >= conn->imc_bytes_out + size 650 ; 651} 652 653 654static struct lsquic_packet_out * 655ietf_mini_conn_ci_next_packet_to_send (struct lsquic_conn *lconn, size_t size) 656{ 657 struct ietf_mini_conn *conn = (struct ietf_mini_conn *) lconn; 658 struct lsquic_packet_out *packet_out; 659 size_t packet_size; 660 661 TAILQ_FOREACH(packet_out, &conn->imc_packets_out, po_next) 662 { 663 if (packet_out->po_flags & PO_SENT) 664 continue; 665 packet_size = lsquic_packet_out_total_sz(lconn, packet_out); 666 if (size == 0 || packet_size + size <= conn->imc_path.np_pack_size) 667 { 668 if (!imico_can_send(conn, packet_size)) 669 { 670 LSQ_DEBUG("cannot send packet %"PRIu64" of size %zu: client " 671 "address has not been validated", packet_out->po_packno, 672 packet_size); 673 return NULL; 674 } 675 packet_out->po_flags |= PO_SENT; 676 conn->imc_bytes_out += packet_size; 677 if (size == 0) 678 LSQ_DEBUG("packet_to_send: %"PRIu64, packet_out->po_packno); 679 else 680 LSQ_DEBUG("packet_to_send: %"PRIu64" (coalesced)", 681 packet_out->po_packno); 682 return packet_out; 683 } 684 else 685 return NULL; 686 } 687 688 return NULL; 689} 690 691 692static int 693imico_calc_retx_timeout (const struct ietf_mini_conn *conn) 694{ 695 lsquic_time_t to; 696 to = lsquic_rtt_stats_get_srtt(&conn->imc_rtt_stats); 697 if (to) 698 { 699 to += to / 2; 700 if (to < 10000) 701 to = 10000; 702 } 703 else 704 to = 300000; 705 return to << conn->imc_hsk_count; 706} 707 708 709static lsquic_time_t 710ietf_mini_conn_ci_next_tick_time (struct lsquic_conn *lconn, unsigned *why) 711{ 712 struct ietf_mini_conn *conn = (struct ietf_mini_conn *) lconn; 713 const struct lsquic_packet_out *packet_out; 714 lsquic_time_t exp_time, retx_time; 715 716 exp_time = conn->imc_created + 717 conn->imc_enpub->enp_settings.es_handshake_to; 718 719 TAILQ_FOREACH(packet_out, &conn->imc_packets_out, po_next) 720 if (packet_out->po_flags & PO_SENT) 721 { 722 retx_time = packet_out->po_sent + imico_calc_retx_timeout(conn); 723 if (retx_time < exp_time) 724 { 725 *why = N_AEWS + AL_RETX_HSK; 726 return retx_time; 727 } 728 else 729 { 730 *why = AEW_MINI_EXPIRE; 731 return exp_time; 732 } 733 } 734 735 *why = AEW_MINI_EXPIRE; 736 return exp_time; 737} 738 739 740#define IMICO_PROC_FRAME_ARGS \ 741 struct ietf_mini_conn *conn, struct lsquic_packet_in *packet_in, \ 742 const unsigned char *p, size_t len 743 744 745static void 746imico_dispatch_stream_events (struct ietf_mini_conn *conn) 747{ 748 enum enc_level i; 749 750 for (i = 0; i < N_ENC_LEVS; ++i) 751 if ((conn->imc_streams[i].mcs_flags & (MCS_CREATED|MCS_WANTREAD)) 752 == (MCS_CREATED|MCS_WANTREAD)) 753 { 754 LSQ_DEBUG("dispatch read events on level #%u", i); 755 lsquic_mini_cry_sm_if.on_read((void *) &conn->imc_streams[i], 756 conn->imc_conn.cn_enc_session); 757 } 758 759 for (i = 0; i < N_ENC_LEVS; ++i) 760 if ((conn->imc_streams[i].mcs_flags & (MCS_CREATED|MCS_WANTWRITE)) 761 == (MCS_CREATED|MCS_WANTWRITE)) 762 { 763 LSQ_DEBUG("dispatch write events on level #%u", i); 764 lsquic_mini_cry_sm_if.on_write((void *) &conn->imc_streams[i], 765 conn->imc_conn.cn_enc_session); 766 } 767} 768 769 770static int 771imico_stash_stream_frame (struct ietf_mini_conn *conn, 772 enum enc_level enc_level, struct lsquic_packet_in *packet_in, 773 const struct stream_frame *frame) 774{ 775 struct stream_frame *copy; 776 777 if (conn->imc_n_crypto_frames >= IMICO_MAX_STASHED_FRAMES) 778 { 779 LSQ_INFO("cannot stash more CRYPTO frames, at %hhu already, while max " 780 "is %u", conn->imc_n_crypto_frames, IMICO_MAX_STASHED_FRAMES); 781 return -1; 782 } 783 784 if (conn->imc_crypto_frames_sz + DF_SIZE(frame) > IMICO_MAX_BUFFERED_CRYPTO) 785 { 786 LSQ_INFO("cannot stash more than %u bytes of CRYPTO frames", 787 IMICO_MAX_BUFFERED_CRYPTO); 788 return -1; 789 } 790 791 copy = lsquic_malo_get(conn->imc_enpub->enp_mm.malo.stream_frame); 792 if (!copy) 793 { 794 LSQ_INFO("could not allocate stream frame for stashing"); 795 return -1; 796 } 797 798 *copy = *frame; 799 copy->packet_in = lsquic_packet_in_get(packet_in); 800 copy->stream_id = enc_level; 801 TAILQ_INSERT_TAIL(&conn->imc_crypto_frames, copy, next_frame); 802 ++conn->imc_n_crypto_frames; 803 conn->imc_crypto_frames_sz += DF_SIZE(frame); 804 return 0; 805} 806 807 808static unsigned 809imico_process_crypto_frame (IMICO_PROC_FRAME_ARGS) 810{ 811 int parsed_len; 812 enum enc_level enc_level, i; 813 struct stream_frame stream_frame; 814 815 parsed_len = conn->imc_conn.cn_pf->pf_parse_crypto_frame(p, len, 816 &stream_frame); 817 if (parsed_len < 0) 818 { 819 conn->imc_flags |= IMC_PARSE_FAILED; 820 return 0; 821 } 822 823 enc_level = lsquic_packet_in_enc_level(packet_in); 824 EV_LOG_CRYPTO_FRAME_IN(LSQUIC_LOG_CONN_ID, &stream_frame, enc_level); 825 826 if (conn->imc_streams[enc_level].mcs_read_off >= DF_OFF(&stream_frame) 827 && conn->imc_streams[enc_level].mcs_read_off < DF_END(&stream_frame)) 828 LSQ_DEBUG("Got CRYPTO frame for enc level #%u", enc_level); 829 else if (conn->imc_streams[enc_level].mcs_read_off < DF_OFF(&stream_frame)) 830 { 831 LSQ_DEBUG("Can't read CRYPTO frame on enc level #%u at offset %"PRIu64 832 " yet -- stash", enc_level, DF_OFF(&stream_frame)); 833 if (0 == imico_stash_stream_frame(conn, enc_level, packet_in, 834 &stream_frame)) 835 return parsed_len; 836 else 837 return 0; 838 } 839 else 840 { 841 LSQ_DEBUG("Got duplicate CRYPTO frame for enc level #%u -- ignore", 842 enc_level); 843 return parsed_len; 844 } 845 846 if (!(conn->imc_flags & IMC_ENC_SESS_INITED)) 847 { 848 if (0 != conn->imc_conn.cn_esf.i->esfi_init_server( 849 conn->imc_conn.cn_enc_session)) 850 return -1; 851 conn->imc_flags |= IMC_ENC_SESS_INITED; 852 } 853 854 if (!(conn->imc_streams[enc_level].mcs_flags & MCS_CREATED)) 855 { 856 LSQ_DEBUG("creating stream on level #%u", enc_level); 857 conn->imc_streams[enc_level].mcs_flags |= MCS_CREATED; 858 lsquic_mini_cry_sm_if.on_new_stream(conn->imc_conn.cn_enc_session, 859 (void *) &conn->imc_streams[enc_level]); 860 } 861 862 /* Assume that receiving a CRYPTO frame at a higher level means that we 863 * no longer want to read from a lower level. 864 */ 865 for (i = 0; i < enc_level; ++i) 866 conn->imc_streams[i].mcs_flags &= ~MCS_WANTREAD; 867 868 conn->imc_last_in.frame = &stream_frame; 869 conn->imc_last_in.enc_level = enc_level; 870 imico_dispatch_stream_events(conn); 871 conn->imc_last_in.frame = NULL; 872 873 if (DF_ROFF(&stream_frame) < DF_END(&stream_frame)) 874 { 875 /* This is an odd condition, but let's handle it just in case */ 876 LSQ_DEBUG("New CRYPTO frame on enc level #%u not fully read -- stash", 877 enc_level); 878 if (0 != imico_stash_stream_frame(conn, enc_level, packet_in, 879 &stream_frame)) 880 return 0; 881 } 882 883 884 return parsed_len; 885} 886 887 888static ptrdiff_t 889imico_count_zero_bytes (const unsigned char *p, size_t len) 890{ 891 const unsigned char *const end = p + len; 892 while (p < end && 0 == *p) 893 ++p; 894 return len - (end - p); 895} 896 897 898static unsigned 899imico_process_padding_frame (IMICO_PROC_FRAME_ARGS) 900{ 901 len = (size_t) imico_count_zero_bytes(p, len); 902 EV_LOG_PADDING_FRAME_IN(LSQUIC_LOG_CONN_ID, len); 903 return len; 904} 905 906 907static void 908imico_take_rtt_sample (struct ietf_mini_conn *conn, 909 const struct lsquic_packet_out *packet_out, 910 lsquic_time_t now, lsquic_time_t lack_delta) 911{ 912 assert(packet_out->po_sent); 913 lsquic_time_t measured_rtt = now - packet_out->po_sent; 914 if (lack_delta < measured_rtt) 915 { 916 lsquic_rtt_stats_update(&conn->imc_rtt_stats, measured_rtt, lack_delta); 917 LSQ_DEBUG("srtt: %"PRIu64" usec, var: %"PRIu64, 918 lsquic_rtt_stats_get_srtt(&conn->imc_rtt_stats), 919 lsquic_rtt_stats_get_rttvar(&conn->imc_rtt_stats)); 920 } 921} 922 923 924static unsigned 925imico_process_ack_frame (IMICO_PROC_FRAME_ARGS) 926{ 927 int parsed_len; 928 unsigned n; 929 lsquic_packet_out_t *packet_out, *next; 930 struct ack_info *acki; 931 lsquic_packno_t packno; 932 lsquic_time_t warn_time; 933 packno_set_t acked; 934 enum packnum_space pns; 935 uint8_t ack_exp; 936 937 if (conn->imc_flags & IMC_HAVE_TP) 938 ack_exp = conn->imc_ack_exp; 939 else 940 ack_exp = TP_DEF_ACK_DELAY_EXP; /* Odd: no transport params yet? */ 941 acki = conn->imc_enpub->enp_mm.acki; 942 parsed_len = conn->imc_conn.cn_pf->pf_parse_ack_frame(p, len, acki, 943 ack_exp); 944 if (parsed_len < 0) 945 { 946 conn->imc_flags |= IMC_PARSE_FAILED; 947 return 0; 948 } 949 950 pns = lsquic_hety2pns[ packet_in->pi_header_type ]; 951 acked = 0; 952 953 for (n = 0; n < acki->n_ranges; ++n) 954 { 955 if (acki->ranges[n].high <= MAX_PACKETS) 956 { 957 acked |= (1ULL << acki->ranges[n].high) 958 | ((1ULL << acki->ranges[n].high) - 1); 959 acked &= ~((1ULL << acki->ranges[n].low) - 1); 960 } 961 else 962 { 963 packno = acki->ranges[n].high; 964 goto err_never_sent; 965 } 966 } 967 if (acked & ~conn->imc_sent_packnos) 968 { 969 packno = highest_bit_set(acked & ~conn->imc_sent_packnos); 970 goto err_never_sent; 971 } 972 973 EV_LOG_ACK_FRAME_IN(LSQUIC_LOG_CONN_ID, acki); 974 for (packet_out = TAILQ_FIRST(&conn->imc_packets_out); packet_out; 975 packet_out = next) 976 { 977 next = TAILQ_NEXT(packet_out, po_next); 978 if ((1ULL << packet_out->po_packno) & acked) 979 { 980 assert(lsquic_packet_out_pns(packet_out) == pns); 981 LSQ_DEBUG("Got ACK for packet %"PRIu64, packet_out->po_packno); 982 if (packet_out->po_packno == largest_acked(acki)) 983 imico_take_rtt_sample(conn, packet_out, 984 packet_in->pi_received, acki->lack_delta); 985 TAILQ_REMOVE(&conn->imc_packets_out, packet_out, po_next); 986 imico_destroy_packet(conn, packet_out); 987 } 988 } 989 990 if (conn->imc_sent_packnos & ~conn->imc_acked_packnos[pns] & acked) 991 { 992 LSQ_DEBUG("Newly acked packets, reset handshake count"); 993 conn->imc_hsk_count = 0; 994 } 995 996 conn->imc_acked_packnos[pns] |= acked; 997 998 return parsed_len; 999 1000 err_never_sent: 1001 warn_time = lsquic_time_now(); 1002 if (0 == conn->imc_enpub->enp_last_warning[WT_ACKPARSE_MINI] 1003 || conn->imc_enpub->enp_last_warning[WT_ACKPARSE_MINI] 1004 + WARNING_INTERVAL < warn_time) 1005 { 1006 conn->imc_enpub->enp_last_warning[WT_ACKPARSE_MINI] = warn_time; 1007 LSQ_WARN("packet %"PRIu64" (pns: %u) was never sent", packno, pns); 1008 } 1009 else 1010 LSQ_DEBUG("packet %"PRIu64" (pns: %u) was never sent", packno, pns); 1011 return 0; 1012} 1013 1014 1015static unsigned 1016imico_process_ping_frame (IMICO_PROC_FRAME_ARGS) 1017{ 1018 LSQ_DEBUG("got a PING frame, do nothing"); 1019 return 1; 1020} 1021 1022 1023static unsigned 1024imico_process_connection_close_frame (IMICO_PROC_FRAME_ARGS) 1025{ 1026 struct lsquic_packet_out *packet_out; 1027 uint64_t error_code; 1028 uint16_t reason_len; 1029 uint8_t reason_off; 1030 int parsed_len, app_error; 1031 1032 while ((packet_out = TAILQ_FIRST(&conn->imc_packets_out))) 1033 { 1034 TAILQ_REMOVE(&conn->imc_packets_out, packet_out, po_next); 1035 imico_destroy_packet(conn, packet_out); 1036 } 1037 conn->imc_flags |= IMC_CLOSE_RECVD; 1038 parsed_len = conn->imc_conn.cn_pf->pf_parse_connect_close_frame(p, len, 1039 &app_error, &error_code, &reason_len, &reason_off); 1040 if (parsed_len < 0) 1041 { 1042 conn->imc_flags |= IMC_PARSE_FAILED; 1043 return 0; 1044 } 1045 EV_LOG_CONNECTION_CLOSE_FRAME_IN(LSQUIC_LOG_CONN_ID, error_code, 1046 (int) reason_len, (const char *) p + reason_off); 1047 LSQ_INFO("Received CONNECTION_CLOSE frame (%s-level code: %"PRIu64"; " 1048 "reason: %.*s)", app_error ? "application" : "transport", 1049 error_code, (int) reason_len, (const char *) p + reason_off); 1050 return 0; /* This shuts down the connection */ 1051} 1052 1053 1054static unsigned 1055imico_process_invalid_frame (IMICO_PROC_FRAME_ARGS) 1056{ 1057 LSQ_DEBUG("invalid frame %u (%s)", p[0], 1058 frame_type_2_str[ conn->imc_conn.cn_pf->pf_parse_frame_type(p, len) ]); 1059 return 0; 1060} 1061 1062 1063static unsigned (*const imico_process_frames[N_QUIC_FRAMES]) 1064 (IMICO_PROC_FRAME_ARGS) = 1065{ 1066 [QUIC_FRAME_PADDING] = imico_process_padding_frame, 1067 [QUIC_FRAME_CRYPTO] = imico_process_crypto_frame, 1068 [QUIC_FRAME_ACK] = imico_process_ack_frame, 1069 [QUIC_FRAME_PING] = imico_process_ping_frame, 1070 [QUIC_FRAME_CONNECTION_CLOSE] = imico_process_connection_close_frame, 1071 /* Some of them are invalid, while others are unexpected. We treat 1072 * them the same: handshake cannot proceed. 1073 */ 1074 [QUIC_FRAME_RST_STREAM] = imico_process_invalid_frame, 1075 [QUIC_FRAME_MAX_DATA] = imico_process_invalid_frame, 1076 [QUIC_FRAME_MAX_STREAM_DATA] = imico_process_invalid_frame, 1077 [QUIC_FRAME_MAX_STREAMS] = imico_process_invalid_frame, 1078 [QUIC_FRAME_BLOCKED] = imico_process_invalid_frame, 1079 [QUIC_FRAME_STREAM_BLOCKED] = imico_process_invalid_frame, 1080 [QUIC_FRAME_STREAMS_BLOCKED] = imico_process_invalid_frame, 1081 [QUIC_FRAME_NEW_CONNECTION_ID] = imico_process_invalid_frame, 1082 [QUIC_FRAME_STOP_SENDING] = imico_process_invalid_frame, 1083 [QUIC_FRAME_PATH_CHALLENGE] = imico_process_invalid_frame, 1084 [QUIC_FRAME_PATH_RESPONSE] = imico_process_invalid_frame, 1085 /* STREAM frame can only come in the App PNS and we delay those packets: */ 1086 [QUIC_FRAME_STREAM] = imico_process_invalid_frame, 1087 [QUIC_FRAME_HANDSHAKE_DONE] = imico_process_invalid_frame, 1088 [QUIC_FRAME_ACK_FREQUENCY] = imico_process_invalid_frame, 1089 [QUIC_FRAME_TIMESTAMP] = imico_process_invalid_frame, 1090}; 1091 1092 1093static unsigned 1094imico_process_packet_frame (struct ietf_mini_conn *conn, 1095 struct lsquic_packet_in *packet_in, const unsigned char *p, size_t len) 1096{ 1097 enum enc_level enc_level; 1098 enum quic_frame_type type; 1099 1100 enc_level = lsquic_packet_in_enc_level(packet_in); 1101 type = conn->imc_conn.cn_pf->pf_parse_frame_type(p, len); 1102 if (lsquic_legal_frames_by_level[conn->imc_conn.cn_version][enc_level] 1103 & (1 << type)) 1104 { 1105 packet_in->pi_frame_types |= 1 << type; 1106 return imico_process_frames[type](conn, packet_in, p, len); 1107 } 1108 else 1109 { 1110 LSQ_DEBUG("invalid frame %u at encryption level %s", type, 1111 lsquic_enclev2str[enc_level]); 1112 return 0; 1113 } 1114} 1115 1116 1117static int 1118imico_parse_regular_packet (struct ietf_mini_conn *conn, 1119 struct lsquic_packet_in *packet_in) 1120{ 1121 const unsigned char *p, *pend; 1122 unsigned len; 1123 1124 p = packet_in->pi_data + packet_in->pi_header_sz; 1125 pend = packet_in->pi_data + packet_in->pi_data_sz; 1126 1127 while (p < pend) 1128 { 1129 len = imico_process_packet_frame(conn, packet_in, p, pend - p); 1130 if (len > 0) 1131 p += len; 1132 else 1133 return -1; 1134 } 1135 1136 return 0; 1137} 1138 1139 1140static unsigned 1141highest_bit_set (unsigned long long sz) 1142{ 1143#if __GNUC__ 1144 unsigned clz = __builtin_clzll(sz); 1145 return 63 - clz; 1146#else 1147 unsigned long y; 1148 unsigned n; 1149 n = 64; 1150 y = sz >> 32; if (y) { n -= 32; sz = y; } 1151 y = sz >> 16; if (y) { n -= 16; sz = y; } 1152 y = sz >> 8; if (y) { n -= 8; sz = y; } 1153 y = sz >> 4; if (y) { n -= 4; sz = y; } 1154 y = sz >> 2; if (y) { n -= 2; sz = y; } 1155 y = sz >> 1; if (y) return 63 - n + 2; 1156 return 63 - n + sz; 1157#endif 1158} 1159 1160 1161static void 1162ignore_init (struct ietf_mini_conn *conn) 1163{ 1164 struct lsquic_packet_out *packet_out, *next; 1165 unsigned count; 1166 1167 conn->imc_flags |= IMC_IGNORE_INIT; 1168 conn->imc_flags &= ~(IMC_QUEUED_ACK_INIT << PNS_INIT); 1169 1170 count = 0; 1171 for (packet_out = TAILQ_FIRST(&conn->imc_packets_out); packet_out; 1172 packet_out = next) 1173 { 1174 next = TAILQ_NEXT(packet_out, po_next); 1175 if (PNS_INIT == lsquic_packet_out_pns(packet_out)) 1176 { 1177 TAILQ_REMOVE(&conn->imc_packets_out, packet_out, po_next); 1178 imico_destroy_packet(conn, packet_out); 1179 ++count; 1180 } 1181 } 1182 1183 LSQ_DEBUG("henceforth, no Initial packets shall be sent or received; " 1184 "destroyed %u packet%.*s", count, count != 1, "s"); 1185} 1186 1187 1188static void 1189imico_maybe_delay_processing (struct ietf_mini_conn *conn, 1190 struct lsquic_packet_in *packet_in) 1191{ 1192 unsigned max_delayed; 1193 1194 if (conn->imc_flags & IMC_ADDR_VALIDATED) 1195 max_delayed = IMICO_MAX_DELAYED_PACKETS_VALIDATED; 1196 else 1197 max_delayed = IMICO_MAX_DELAYED_PACKETS_UNVALIDATED; 1198 1199 if (conn->imc_delayed_packets_count < max_delayed) 1200 { 1201 ++conn->imc_delayed_packets_count; 1202 lsquic_packet_in_upref(packet_in); 1203 TAILQ_INSERT_TAIL(&conn->imc_app_packets, packet_in, pi_next); 1204 LSQ_DEBUG("delay processing of packet (now delayed %hhu)", 1205 conn->imc_delayed_packets_count); 1206 } 1207 else 1208 LSQ_DEBUG("drop packet, already delayed %hhu packets", 1209 conn->imc_delayed_packets_count); 1210} 1211 1212 1213/* Only a single packet is supported */ 1214static void 1215ietf_mini_conn_ci_packet_in (struct lsquic_conn *lconn, 1216 struct lsquic_packet_in *packet_in) 1217{ 1218 struct ietf_mini_conn *conn = (struct ietf_mini_conn *) lconn; 1219 enum dec_packin dec_packin; 1220 enum packnum_space pns; 1221 1222 /* Update "bytes in" count as early as possible. From 1223 * [draft-ietf-quic-transport-28] Section 8.1: 1224 " For the purposes of 1225 " avoiding amplification prior to address validation, servers MUST 1226 " count all of the payload bytes received in datagrams that are 1227 " uniquely attributed to a single connection. This includes datagrams 1228 " that contain packets that are successfully processed and datagrams 1229 " that contain packets that are all discarded. 1230 */ 1231 conn->imc_bytes_in += packet_in->pi_data_sz; 1232 1233 if (conn->imc_flags & IMC_ERROR) 1234 { 1235 LSQ_DEBUG("ignore incoming packet: connection is in error state"); 1236 return; 1237 } 1238 1239 pns = lsquic_hety2pns[ packet_in->pi_header_type ]; 1240 if (pns == PNS_INIT && (conn->imc_flags & IMC_IGNORE_INIT)) 1241 { 1242 LSQ_DEBUG("ignore init packet"); /* Don't bother decrypting */ 1243 return; 1244 } 1245 1246 dec_packin = lconn->cn_esf_c->esf_decrypt_packet(lconn->cn_enc_session, 1247 conn->imc_enpub, &conn->imc_conn, packet_in); 1248 if (dec_packin != DECPI_OK) 1249 { 1250 LSQ_DEBUG("could not decrypt packet"); 1251 if (DECPI_NOT_YET == dec_packin) 1252 imico_maybe_delay_processing(conn, packet_in); 1253 return; 1254 } 1255 1256 EV_LOG_PACKET_IN(LSQUIC_LOG_CONN_ID, packet_in); 1257 1258 if (pns == PNS_APP) 1259 { 1260 imico_maybe_delay_processing(conn, packet_in); 1261 return; 1262 } 1263 else if (pns == PNS_HSK) 1264 conn->imc_flags |= IMC_ADDR_VALIDATED; 1265 1266 if (((conn->imc_flags >> IMCBIT_PNS_BIT_SHIFT) & 3) < pns) 1267 { 1268 conn->imc_flags &= ~(3 << IMCBIT_PNS_BIT_SHIFT); 1269 conn->imc_flags |= pns << IMCBIT_PNS_BIT_SHIFT; 1270 } 1271 1272 if (pns == PNS_HSK && !(conn->imc_flags & IMC_IGNORE_INIT)) 1273 ignore_init(conn); 1274 1275 if (conn->imc_recvd_packnos[pns] & (1ULL << packet_in->pi_packno)) 1276 { 1277 LSQ_DEBUG("duplicate packet %"PRIu64, packet_in->pi_packno); 1278 return; 1279 } 1280 1281 /* Update receive history before processing the packet: if there is an 1282 * error, the connection is terminated and recording this packet number 1283 * is helpful when it is printed along with other diagnostics in dtor. 1284 */ 1285 if (0 == conn->imc_recvd_packnos[pns] || 1286 packet_in->pi_packno > highest_bit_set(conn->imc_recvd_packnos[pns])) 1287 conn->imc_largest_recvd[pns] = packet_in->pi_received; 1288 conn->imc_recvd_packnos[pns] |= 1ULL << packet_in->pi_packno; 1289 1290 if (0 != imico_parse_regular_packet(conn, packet_in)) 1291 { 1292 LSQ_DEBUG("connection is now in error state"); 1293 conn->imc_flags |= IMC_ERROR; 1294 return; 1295 } 1296 1297 conn->imc_flags |= IMC_QUEUED_ACK_INIT << pns; 1298 ++conn->imc_ecn_counts_in[pns][ lsquic_packet_in_ecn(packet_in) ]; 1299 conn->imc_incoming_ecn <<= 1; 1300 conn->imc_incoming_ecn |= lsquic_packet_in_ecn(packet_in) != ECN_NOT_ECT; 1301} 1302 1303 1304static void 1305ietf_mini_conn_ci_packet_sent (struct lsquic_conn *lconn, 1306 struct lsquic_packet_out *packet_out) 1307{ 1308 struct ietf_mini_conn *conn = (struct ietf_mini_conn *) lconn; 1309 conn->imc_sent_packnos |= 1ULL << packet_out->po_packno; 1310 conn->imc_ecn_packnos |= !!lsquic_packet_out_ecn(packet_out) 1311 << packet_out->po_packno; 1312#if 0 1313 if (packet_out->po_frame_types & (1 << QUIC_FRAME_ACK)) 1314 { 1315 assert(mc->mc_flags & MC_UNSENT_ACK); 1316 mc->mc_flags &= ~MC_UNSENT_ACK; 1317 } 1318#endif 1319 ++conn->imc_ecn_counts_out[ lsquic_packet_out_pns(packet_out) ] 1320 [ lsquic_packet_out_ecn(packet_out) ]; 1321 if (packet_out->po_header_type == HETY_HANDSHAKE) 1322 conn->imc_flags |= IMC_HSK_PACKET_SENT; 1323 LSQ_DEBUG("%s: packet %"PRIu64" sent", __func__, packet_out->po_packno); 1324} 1325 1326 1327static void 1328ietf_mini_conn_ci_packet_not_sent (struct lsquic_conn *lconn, 1329 struct lsquic_packet_out *packet_out) 1330{ 1331 struct ietf_mini_conn *conn = (struct ietf_mini_conn *) lconn; 1332 size_t packet_size; 1333 1334 packet_out->po_flags &= ~PO_SENT; 1335 packet_size = lsquic_packet_out_total_sz(lconn, packet_out); 1336 conn->imc_bytes_out -= packet_size; 1337 LSQ_DEBUG("%s: packet %"PRIu64" not sent", __func__, packet_out->po_packno); 1338} 1339 1340 1341static void 1342imico_return_enc_data (struct ietf_mini_conn *conn, 1343 struct lsquic_packet_out *packet_out) 1344{ 1345 conn->imc_enpub->enp_pmi->pmi_return(conn->imc_enpub->enp_pmi_ctx, 1346 conn->imc_path.np_peer_ctx, packet_out->po_enc_data, 1347 lsquic_packet_out_ipv6(packet_out)); 1348 packet_out->po_flags &= ~PO_ENCRYPTED; 1349 packet_out->po_enc_data = NULL; 1350} 1351 1352 1353static int 1354imico_repackage_packet (struct ietf_mini_conn *conn, 1355 struct lsquic_packet_out *packet_out) 1356{ 1357 const lsquic_packno_t oldno = packet_out->po_packno; 1358 const lsquic_packno_t packno = conn->imc_next_packno++; 1359 if (packno > MAX_PACKETS) 1360 return -1; 1361 1362 LSQ_DEBUG("Packet %"PRIu64" repackaged for resending as packet %"PRIu64, 1363 oldno, packno); 1364 EV_LOG_CONN_EVENT(LSQUIC_LOG_CONN_ID, "packet %"PRIu64" repackaged for " 1365 "resending as packet %"PRIu64, oldno, packno); 1366 packet_out->po_packno = packno; 1367 packet_out->po_flags &= ~PO_SENT; 1368 lsquic_packet_out_set_ecn(packet_out, imico_get_ecn(conn)); 1369 if (packet_out->po_flags & PO_ENCRYPTED) 1370 imico_return_enc_data(conn, packet_out); 1371 TAILQ_INSERT_TAIL(&conn->imc_packets_out, packet_out, po_next); 1372 return 0; 1373} 1374 1375 1376static int 1377imico_handle_losses_and_have_unsent (struct ietf_mini_conn *conn, 1378 lsquic_time_t now) 1379{ 1380 TAILQ_HEAD(, lsquic_packet_out) lost_packets = 1381 TAILQ_HEAD_INITIALIZER(lost_packets); 1382 const struct lsquic_conn *const lconn = &conn->imc_conn; 1383 lsquic_packet_out_t *packet_out, *next; 1384 lsquic_time_t retx_to = 0; 1385 unsigned n_to_send = 0; 1386 size_t packet_size; 1387 1388 for (packet_out = TAILQ_FIRST(&conn->imc_packets_out); packet_out; 1389 packet_out = next) 1390 { 1391 next = TAILQ_NEXT(packet_out, po_next); 1392 if (packet_out->po_flags & PO_SENT) 1393 { 1394 if (0 == retx_to) 1395 retx_to = imico_calc_retx_timeout(conn); 1396 if (packet_out->po_sent + retx_to < now) 1397 { 1398 LSQ_DEBUG("packet %"PRIu64" has been lost (rto: %"PRIu64")", 1399 packet_out->po_packno, retx_to); 1400 TAILQ_REMOVE(&conn->imc_packets_out, packet_out, po_next); 1401 TAILQ_INSERT_TAIL(&lost_packets, packet_out, po_next); 1402 } 1403 } 1404 else if (packet_size = lsquic_packet_out_total_sz(lconn, packet_out), 1405 imico_can_send(conn, packet_size)) 1406 ++n_to_send; 1407 else 1408 break; 1409 } 1410 1411 conn->imc_hsk_count += !TAILQ_EMPTY(&lost_packets); 1412 1413 while ((packet_out = TAILQ_FIRST(&lost_packets))) 1414 { 1415 TAILQ_REMOVE(&lost_packets, packet_out, po_next); 1416 if ((packet_out->po_frame_types & IQUIC_FRAME_RETX_MASK) 1417 && 0 == imico_repackage_packet(conn, packet_out)) 1418 { 1419 packet_size = lsquic_packet_out_total_sz(lconn, packet_out); 1420 if (imico_can_send(conn, packet_size)) 1421 ++n_to_send; 1422 } 1423 else 1424 imico_destroy_packet(conn, packet_out); 1425 } 1426 1427 return n_to_send > 0; 1428} 1429 1430 1431static int 1432imico_have_packets_to_send (struct ietf_mini_conn *conn, lsquic_time_t now) 1433{ 1434 return imico_handle_losses_and_have_unsent(conn, now); 1435} 1436 1437 1438struct ietf_mini_rechist 1439{ 1440 const struct ietf_mini_conn *conn; 1441 packno_set_t cur_set; 1442 struct lsquic_packno_range range; /* We return a pointer to this */ 1443 int cur_idx; 1444 enum packnum_space pns; 1445}; 1446 1447 1448static void 1449imico_rechist_init (struct ietf_mini_rechist *rechist, 1450 const struct ietf_mini_conn *conn, enum packnum_space pns) 1451{ 1452 rechist->conn = conn; 1453 rechist->pns = pns; 1454 rechist->cur_set = 0; 1455 rechist->cur_idx = 0; 1456} 1457 1458 1459static lsquic_time_t 1460imico_rechist_largest_recv (void *rechist_ctx) 1461{ 1462 struct ietf_mini_rechist *rechist = rechist_ctx; 1463 return rechist->conn->imc_largest_recvd[ rechist->pns ]; 1464} 1465 1466 1467static const struct lsquic_packno_range * 1468imico_rechist_next (void *rechist_ctx) 1469{ 1470 struct ietf_mini_rechist *rechist = rechist_ctx; 1471 const struct ietf_mini_conn *conn = rechist->conn; 1472 packno_set_t packnos; 1473 int i; 1474 1475 packnos = rechist->cur_set; 1476 if (0 == packnos) 1477 return NULL; 1478 1479 /* There may be a faster way to do this, but for now, we just want 1480 * correctness. 1481 */ 1482 for (i = rechist->cur_idx; i >= 0; --i) 1483 if (packnos & (1ULL << i)) 1484 { 1485 rechist->range.low = i; 1486 rechist->range.high = i; 1487 break; 1488 } 1489 assert(i >= 0); /* We must have hit at least one bit */ 1490 --i; 1491 for ( ; i >= 0 && (packnos & (1ULL << i)); --i) 1492 rechist->range.low = i; 1493 if (i >= 0) 1494 { 1495 rechist->cur_set = packnos & ((1ULL << i) - 1); 1496 rechist->cur_idx = i; 1497 } 1498 else 1499 rechist->cur_set = 0; 1500 LSQ_DEBUG("%s: return [%"PRIu64", %"PRIu64"]", __func__, 1501 rechist->range.low, rechist->range.high); 1502 return &rechist->range; 1503} 1504 1505 1506static const struct lsquic_packno_range * 1507imico_rechist_first (void *rechist_ctx) 1508{ 1509 struct ietf_mini_rechist *rechist = rechist_ctx; 1510 rechist->cur_set = rechist->conn->imc_recvd_packnos[ rechist->pns ]; 1511 rechist->cur_idx = highest_bit_set(rechist->cur_set); 1512 return imico_rechist_next(rechist_ctx); 1513} 1514 1515 1516static const enum header_type pns2hety[] = 1517{ 1518 [PNS_INIT] = HETY_INITIAL, 1519 [PNS_HSK] = HETY_HANDSHAKE, 1520 [PNS_APP] = HETY_NOT_SET, 1521}; 1522 1523 1524static int 1525imico_generate_ack (struct ietf_mini_conn *conn, enum packnum_space pns, 1526 lsquic_time_t now) 1527{ 1528 struct lsquic_packet_out *packet_out; 1529 enum header_type header_type; 1530 struct ietf_mini_rechist rechist; 1531 int not_used_has_missing, len; 1532 uint64_t ecn_counts_buf[4]; 1533 const uint64_t *ecn_counts; 1534 1535 header_type = pns2hety[pns]; 1536 1537 if (conn->imc_incoming_ecn) 1538 { 1539 ecn_counts_buf[0] = conn->imc_ecn_counts_in[pns][0]; 1540 ecn_counts_buf[1] = conn->imc_ecn_counts_in[pns][1]; 1541 ecn_counts_buf[2] = conn->imc_ecn_counts_in[pns][2]; 1542 ecn_counts_buf[3] = conn->imc_ecn_counts_in[pns][3]; 1543 ecn_counts = ecn_counts_buf; 1544 } 1545 else 1546 ecn_counts = NULL; 1547 1548 packet_out = imico_get_packet_out(conn, header_type, 0); 1549 if (!packet_out) 1550 return -1; 1551 1552 /* Generate ACK frame */ 1553 imico_rechist_init(&rechist, conn, pns); 1554 len = conn->imc_conn.cn_pf->pf_gen_ack_frame( 1555 packet_out->po_data + packet_out->po_data_sz, 1556 lsquic_packet_out_avail(packet_out), imico_rechist_first, 1557 imico_rechist_next, imico_rechist_largest_recv, &rechist, 1558 now, ¬_used_has_missing, &packet_out->po_ack2ed, ecn_counts); 1559 if (len < 0) 1560 { 1561 LSQ_WARN("could not generate ACK frame"); 1562 return -1; 1563 } 1564 EV_LOG_GENERATED_ACK_FRAME(LSQUIC_LOG_CONN_ID, conn->imc_conn.cn_pf, 1565 packet_out->po_data + packet_out->po_data_sz, len); 1566 packet_out->po_frame_types |= 1 << QUIC_FRAME_ACK; 1567 packet_out->po_data_sz += len; 1568 packet_out->po_regen_sz += len; 1569 conn->imc_flags &= ~(IMC_QUEUED_ACK_INIT << pns); 1570 LSQ_DEBUG("wrote ACK frame of size %d", len); 1571 return 0; 1572} 1573 1574 1575static int 1576imico_generate_acks (struct ietf_mini_conn *conn, lsquic_time_t now) 1577{ 1578 enum packnum_space pns; 1579 1580 for (pns = PNS_INIT; pns < N_PNS; ++pns) 1581 if (conn->imc_flags & (IMC_QUEUED_ACK_INIT << pns) 1582 && !(pns == PNS_INIT && (conn->imc_flags & IMC_IGNORE_INIT))) 1583 if (0 != imico_generate_ack(conn, pns, now)) 1584 return -1; 1585 1586 return 0; 1587} 1588 1589 1590static void 1591imico_generate_conn_close (struct ietf_mini_conn *conn) 1592{ 1593 struct lsquic_packet_out *packet_out; 1594 enum header_type header_type; 1595 enum packnum_space pns, pns_max; 1596 unsigned error_code; 1597 const char *reason; 1598 size_t need; 1599 int sz, rlen, is_app; 1600 char reason_buf[0x20]; 1601 1602 if (conn->imc_flags & IMC_ABORT_ERROR) 1603 { 1604 is_app = !!(conn->imc_flags & IMC_ABORT_ISAPP); 1605 error_code = conn->imc_error_code; 1606 reason = NULL; 1607 rlen = 0; 1608 } 1609 else if (conn->imc_flags & IMC_TLS_ALERT) 1610 { 1611 is_app = 0; 1612 error_code = 0x100 + conn->imc_tls_alert; 1613 if (ALERT_NO_APPLICATION_PROTOCOL == conn->imc_tls_alert) 1614 reason = "no suitable application protocol"; 1615 else 1616 { 1617 snprintf(reason_buf, sizeof(reason_buf), "TLS alert %"PRIu8, 1618 conn->imc_tls_alert); 1619 reason = reason_buf; 1620 } 1621 rlen = strlen(reason); 1622 } 1623 else if (conn->imc_flags & IMC_BAD_TRANS_PARAMS) 1624 { 1625 is_app = 0; 1626 error_code = TEC_TRANSPORT_PARAMETER_ERROR; 1627 reason = "bad transport parameters"; 1628 rlen = 24; 1629 } 1630 else if (conn->imc_flags & IMC_HSK_FAILED) 1631 { 1632 is_app = 0; 1633 error_code = TEC_NO_ERROR; 1634 reason = "handshake failed"; 1635 rlen = 16; 1636 } 1637 else if (conn->imc_flags & IMC_PARSE_FAILED) 1638 { 1639 is_app = 0; 1640 error_code = TEC_FRAME_ENCODING_ERROR; 1641 reason = "cannot decode frame"; 1642 rlen = 19; 1643 } 1644 else 1645 { 1646 is_app = 0; 1647 error_code = TEC_INTERNAL_ERROR; 1648 reason = NULL; 1649 rlen = 0; 1650 } 1651 1652 1653/* [draft-ietf-quic-transport-28] Section 10.3.1: 1654 * 1655 " A client will always know whether the server has Handshake keys (see 1656 " Section 17.2.2.1), but it is possible that a server does not know 1657 " whether the client has Handshake keys. Under these circumstances, a 1658 " server SHOULD send a CONNECTION_CLOSE frame in both Handshake and 1659 " Initial packets to ensure that at least one of them is processable by 1660 " the client. 1661--- 8< --- 1662 " Sending a CONNECTION_CLOSE of type 0x1d in an Initial or Handshake 1663 " packet could expose application state or be used to alter application 1664 " state. A CONNECTION_CLOSE of type 0x1d MUST be replaced by a 1665 " CONNECTION_CLOSE of type 0x1c when sending the frame in Initial or 1666 " Handshake packets. Otherwise, information about the application 1667 " state might be revealed. Endpoints MUST clear the value of the 1668 " Reason Phrase field and SHOULD use the APPLICATION_ERROR code when 1669 " converting to a CONNECTION_CLOSE of type 0x1c. 1670 */ 1671 LSQ_DEBUG("sending CONNECTION_CLOSE, is_app: %d, error code: %u, " 1672 "reason: %.*s", is_app, error_code, rlen, reason); 1673 if (is_app && conn->imc_conn.cn_version > LSQVER_ID27) 1674 { 1675 LSQ_DEBUG("convert to 0x1C, replace code and reason"); 1676 is_app = 0; 1677 error_code = TEC_APPLICATION_ERROR; 1678 rlen = 0; 1679 } 1680 1681 pns = (conn->imc_flags >> IMCBIT_PNS_BIT_SHIFT) & 3; 1682 switch ((!!(conn->imc_flags & IMC_HSK_PACKET_SENT) << 1) 1683 | (pns == PNS_HSK) /* Handshake packet received */) 1684 { 1685 case (0 << 1) | 0: 1686 pns = PNS_INIT; 1687 pns_max = PNS_INIT; 1688 break; 1689 case (1 << 1) | 0: 1690 pns = PNS_INIT; 1691 pns_max = PNS_HSK; 1692 break; 1693 default: 1694 pns = PNS_HSK; 1695 pns_max = PNS_HSK; 1696 break; 1697 } 1698 1699 need = conn->imc_conn.cn_pf->pf_connect_close_frame_size(is_app, 1700 error_code, 0, rlen); 1701 LSQ_DEBUG("will generate %u CONNECTION_CLOSE frame%.*s", 1702 pns_max - pns + 1, pns_max > pns, "s"); 1703 do 1704 { 1705 header_type = pns2hety[pns]; 1706 packet_out = imico_get_packet_out(conn, header_type, need); 1707 if (!packet_out) 1708 return; 1709 sz = conn->imc_conn.cn_pf->pf_gen_connect_close_frame( 1710 packet_out->po_data + packet_out->po_data_sz, 1711 lsquic_packet_out_avail(packet_out), is_app, error_code, reason, 1712 rlen); 1713 if (sz >= 0) 1714 { 1715 packet_out->po_frame_types |= 1 << QUIC_FRAME_CONNECTION_CLOSE; 1716 packet_out->po_data_sz += sz; 1717 LSQ_DEBUG("generated CONNECTION_CLOSE frame"); 1718 } 1719 else 1720 LSQ_WARN("could not generate CONNECTION_CLOSE frame"); 1721 ++pns; 1722 } 1723 while (pns <= pns_max); 1724} 1725 1726 1727static int 1728imico_generate_handshake_done (struct ietf_mini_conn *conn) 1729{ 1730 struct lsquic_packet_out *packet_out; 1731 unsigned need; 1732 int sz; 1733 1734 need = conn->imc_conn.cn_pf->pf_handshake_done_frame_size(); 1735 packet_out = imico_get_packet_out(conn, HETY_NOT_SET, need); 1736 if (!packet_out) 1737 return -1; 1738 sz = conn->imc_conn.cn_pf->pf_gen_handshake_done_frame( 1739 packet_out->po_data + packet_out->po_data_sz, 1740 lsquic_packet_out_avail(packet_out)); 1741 if (sz < 0) 1742 { 1743 LSQ_WARN("could not generate HANDSHAKE_DONE frame"); 1744 return -1; 1745 } 1746 1747 packet_out->po_frame_types |= 1 << QUIC_FRAME_HANDSHAKE_DONE; 1748 packet_out->po_data_sz += sz; 1749 LSQ_DEBUG("generated HANDSHAKE_DONE frame"); 1750 conn->imc_flags |= IMC_HSK_DONE_SENT; 1751 1752 return 0; 1753} 1754 1755 1756static enum tick_st 1757ietf_mini_conn_ci_tick (struct lsquic_conn *lconn, lsquic_time_t now) 1758{ 1759 struct ietf_mini_conn *conn = (struct ietf_mini_conn *) lconn; 1760 enum tick_st tick; 1761 1762 if (conn->imc_created + conn->imc_enpub->enp_settings.es_handshake_to < now) 1763 { 1764 LSQ_DEBUG("connection expired: closing"); 1765 return TICK_CLOSE; 1766 } 1767 1768 1769 if (conn->imc_flags & 1770 (IMC_QUEUED_ACK_INIT|IMC_QUEUED_ACK_HSK|IMC_QUEUED_ACK_APP)) 1771 { 1772 if (0 != imico_generate_acks(conn, now)) 1773 { 1774 conn->imc_flags |= IMC_ERROR; 1775 return TICK_CLOSE; 1776 } 1777 } 1778 1779 1780 tick = 0; 1781 1782 if (conn->imc_flags & IMC_ERROR) 1783 { 1784 close_on_error: 1785 if (!(conn->imc_flags & IMC_CLOSE_RECVD)) 1786 imico_generate_conn_close(conn); 1787 tick |= TICK_CLOSE; 1788 } 1789 else if (conn->imc_flags & IMC_HSK_OK) 1790 { 1791 if (lconn->cn_esf.i->esfi_in_init(lconn->cn_enc_session)) 1792 LSQ_DEBUG("still in init, defer HANDSHAKE_DONE"); 1793 else if (0 != imico_generate_handshake_done(conn)) 1794 goto close_on_error; 1795 tick |= TICK_PROMOTE; 1796 } 1797 1798 if (imico_have_packets_to_send(conn, now)) 1799 tick |= TICK_SEND; 1800 else 1801 tick |= TICK_QUIET; 1802 1803 LSQ_DEBUG("Return TICK %d", tick); 1804 return tick; 1805} 1806 1807 1808static void 1809ietf_mini_conn_ci_internal_error (struct lsquic_conn *lconn, 1810 const char *format, ...) 1811{ 1812 struct ietf_mini_conn *conn = (struct ietf_mini_conn *) lconn; 1813 LSQ_INFO("internal error reported"); 1814 conn->imc_flags |= IMC_ERROR; 1815} 1816 1817 1818static void 1819ietf_mini_conn_ci_abort_error (struct lsquic_conn *lconn, int is_app, 1820 unsigned error_code, const char *fmt, ...) 1821{ 1822 struct ietf_mini_conn *conn = (struct ietf_mini_conn *) lconn; 1823 va_list ap; 1824 const char *err_str, *percent; 1825 char err_buf[0x100]; 1826 1827 percent = strchr(fmt, '%'); 1828 if (percent) 1829 { 1830 va_start(ap, fmt); 1831 vsnprintf(err_buf, sizeof(err_buf), fmt, ap); 1832 va_end(ap); 1833 err_str = err_buf; 1834 } 1835 else 1836 err_str = fmt; 1837 LSQ_INFO("abort error: is_app: %d; error code: %u; error str: %s", 1838 is_app, error_code, err_str); 1839 conn->imc_flags |= IMC_ERROR|IMC_ABORT_ERROR; 1840 if (is_app) 1841 conn->imc_flags |= IMC_ABORT_ISAPP; 1842 conn->imc_error_code = error_code; 1843} 1844 1845 1846static struct network_path * 1847ietf_mini_conn_ci_get_path (struct lsquic_conn *lconn, 1848 const struct sockaddr *sa) 1849{ 1850 struct ietf_mini_conn *conn = (struct ietf_mini_conn *) lconn; 1851 1852 return &conn->imc_path; 1853} 1854 1855 1856static const lsquic_cid_t * 1857ietf_mini_conn_ci_get_log_cid (const struct lsquic_conn *lconn) 1858{ 1859 struct ietf_mini_conn *conn = (struct ietf_mini_conn *) lconn; 1860 1861 if (conn->imc_path.np_dcid.len) 1862 return &conn->imc_path.np_dcid; 1863 else 1864 return CN_SCID(lconn); 1865} 1866 1867 1868static unsigned char 1869ietf_mini_conn_ci_record_addrs (struct lsquic_conn *lconn, void *peer_ctx, 1870 const struct sockaddr *local_sa, const struct sockaddr *peer_sa) 1871{ 1872 struct ietf_mini_conn *conn = (struct ietf_mini_conn *) lconn; 1873 const struct sockaddr *orig_peer_sa; 1874 struct lsquic_packet_out *packet_out; 1875 size_t len; 1876 char path_str[4][INET6_ADDRSTRLEN + sizeof(":65535")]; 1877 1878 if (NP_IS_IPv6(&conn->imc_path) != (AF_INET6 == peer_sa->sa_family)) 1879 TAILQ_FOREACH(packet_out, &conn->imc_packets_out, po_next) 1880 if ((packet_out->po_flags & (PO_SENT|PO_ENCRYPTED)) == PO_ENCRYPTED) 1881 imico_return_enc_data(conn, packet_out); 1882 1883 orig_peer_sa = NP_PEER_SA(&conn->imc_path); 1884 if (orig_peer_sa->sa_family == 0) 1885 LSQ_DEBUG("connection to %s from %s", SA2STR(local_sa, path_str[0]), 1886 SA2STR(peer_sa, path_str[1])); 1887 else if (!(lsquic_sockaddr_eq(NP_PEER_SA(&conn->imc_path), peer_sa) 1888 && lsquic_sockaddr_eq(NP_LOCAL_SA(&conn->imc_path), local_sa))) 1889 { 1890 LSQ_DEBUG("path changed from (%s - %s) to (%s - %s)", 1891 SA2STR(NP_LOCAL_SA(&conn->imc_path), path_str[0]), 1892 SA2STR(NP_PEER_SA(&conn->imc_path), path_str[1]), 1893 SA2STR(local_sa, path_str[2]), 1894 SA2STR(peer_sa, path_str[3])); 1895 conn->imc_flags |= IMC_PATH_CHANGED; 1896 } 1897 1898 len = local_sa->sa_family == AF_INET ? sizeof(struct sockaddr_in) 1899 : sizeof(struct sockaddr_in6); 1900 1901 memcpy(conn->imc_path.np_peer_addr, peer_sa, len); 1902 memcpy(conn->imc_path.np_local_addr, local_sa, len); 1903 conn->imc_path.np_peer_ctx = peer_ctx; 1904 return 0; 1905} 1906 1907 1908void 1909ietf_mini_conn_ci_count_garbage (struct lsquic_conn *lconn, size_t garbage_sz) 1910{ 1911 struct ietf_mini_conn *conn = (struct ietf_mini_conn *) lconn; 1912 1913 conn->imc_bytes_in += garbage_sz; 1914 LSQ_DEBUG("count %zd bytes of garbage, new value: %u bytes", garbage_sz, 1915 conn->imc_bytes_in); 1916} 1917 1918 1919static const struct conn_iface mini_conn_ietf_iface = { 1920 .ci_abort_error = ietf_mini_conn_ci_abort_error, 1921 .ci_client_call_on_new = ietf_mini_conn_ci_client_call_on_new, 1922 .ci_count_garbage = ietf_mini_conn_ci_count_garbage, 1923 .ci_destroy = ietf_mini_conn_ci_destroy, 1924 .ci_get_engine = ietf_mini_conn_ci_get_engine, 1925 .ci_get_log_cid = ietf_mini_conn_ci_get_log_cid, 1926 .ci_get_path = ietf_mini_conn_ci_get_path, 1927 .ci_hsk_done = ietf_mini_conn_ci_hsk_done, 1928 .ci_internal_error = ietf_mini_conn_ci_internal_error, 1929 .ci_is_tickable = ietf_mini_conn_ci_is_tickable, 1930 .ci_next_packet_to_send = ietf_mini_conn_ci_next_packet_to_send, 1931 .ci_next_tick_time = ietf_mini_conn_ci_next_tick_time, 1932 .ci_packet_in = ietf_mini_conn_ci_packet_in, 1933 .ci_packet_not_sent = ietf_mini_conn_ci_packet_not_sent, 1934 .ci_packet_sent = ietf_mini_conn_ci_packet_sent, 1935 .ci_record_addrs = ietf_mini_conn_ci_record_addrs, 1936 .ci_tick = ietf_mini_conn_ci_tick, 1937 .ci_tls_alert = ietf_mini_conn_ci_tls_alert, 1938}; 1939