1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #include "opt_inet.h" 34 #include "opt_inet6.h" 35 #include "opt_ipsec.h" 36 #include "opt_kern_tls.h" 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/arb.h> 41 #include <sys/callout.h> 42 #include <sys/eventhandler.h> 43 #ifdef TCP_HHOOK 44 #include <sys/hhook.h> 45 #endif 46 #include <sys/kernel.h> 47 #ifdef TCP_HHOOK 48 #include <sys/khelp.h> 49 #endif 50 #ifdef KERN_TLS 51 #include <sys/ktls.h> 52 #endif 53 #include <sys/qmath.h> 54 #include <sys/stats.h> 55 #include <sys/sysctl.h> 56 #include <sys/jail.h> 57 #include <sys/malloc.h> 58 #include <sys/refcount.h> 59 #include <sys/mbuf.h> 60 #include <sys/priv.h> 61 #include <sys/proc.h> 62 #include <sys/sdt.h> 63 #include <sys/socket.h> 64 #include <sys/socketvar.h> 65 #include <sys/protosw.h> 66 #include <sys/random.h> 67 68 #include <vm/uma.h> 69 70 #include <net/route.h> 71 #include <net/route/nhop.h> 72 #include <net/if.h> 73 #include <net/if_var.h> 74 #include <net/if_private.h> 75 #include <net/vnet.h> 76 77 #include <netinet/in.h> 78 #include <netinet/in_fib.h> 79 #include <netinet/in_kdtrace.h> 80 #include <netinet/in_pcb.h> 81 #include <netinet/in_systm.h> 82 #include <netinet/in_var.h> 83 #include <netinet/ip.h> 84 #include <netinet/ip_icmp.h> 85 #include <netinet/ip_var.h> 86 #ifdef INET6 87 #include <netinet/icmp6.h> 88 #include <netinet/ip6.h> 89 #include <netinet6/in6_fib.h> 90 #include <netinet6/in6_pcb.h> 91 #include <netinet6/ip6_var.h> 92 #include <netinet6/scope6_var.h> 93 #include <netinet6/nd6.h> 94 #endif 95 96 #include <netinet/tcp.h> 97 #ifdef INVARIANTS 98 #define TCPSTATES 99 #endif 100 #include <netinet/tcp_fsm.h> 101 #include <netinet/tcp_seq.h> 102 #include <netinet/tcp_timer.h> 103 #include <netinet/tcp_var.h> 104 #include <netinet/tcp_ecn.h> 105 #include <netinet/tcp_log_buf.h> 106 #include <netinet/tcp_syncache.h> 107 #include <netinet/tcp_hpts.h> 108 #include <netinet/tcp_lro.h> 109 #include <netinet/cc/cc.h> 110 #include <netinet/tcpip.h> 111 #include <netinet/tcp_fastopen.h> 112 #include <netinet/tcp_accounting.h> 113 #ifdef TCPPCAP 114 #include <netinet/tcp_pcap.h> 115 #endif 116 #ifdef TCP_OFFLOAD 117 #include <netinet/tcp_offload.h> 118 #endif 119 #include <netinet/udp.h> 120 #include <netinet/udp_var.h> 121 #ifdef INET6 122 #include <netinet6/tcp6_var.h> 123 #endif 124 125 #include <netipsec/ipsec_support.h> 126 127 #include <machine/in_cksum.h> 128 #include <crypto/siphash/siphash.h> 129 130 #include <security/mac/mac_framework.h> 131 132 #ifdef INET6 133 static ip6proto_ctlinput_t tcp6_ctlinput; 134 static udp_tun_icmp_t tcp6_ctlinput_viaudp; 135 #endif 136 137 VNET_DEFINE(int, tcp_mssdflt) = TCP_MSS; 138 #ifdef INET6 139 VNET_DEFINE(int, tcp_v6mssdflt) = TCP6_MSS; 140 #endif 141 142 uint32_t tcp_ack_war_time_window = 1000; 143 SYSCTL_UINT(_net_inet_tcp, OID_AUTO, ack_war_timewindow, 144 CTLFLAG_RW, 145 &tcp_ack_war_time_window, 1000, 146 "If the tcp_stack does ack-war prevention how many milliseconds are in its time window?"); 147 uint32_t tcp_ack_war_cnt = 5; 148 SYSCTL_UINT(_net_inet_tcp, OID_AUTO, ack_war_cnt, 149 CTLFLAG_RW, 150 &tcp_ack_war_cnt, 5, 151 "If the tcp_stack does ack-war prevention how many acks can be sent in its time window?"); 152 153 struct rwlock tcp_function_lock; 154 155 static int 156 sysctl_net_inet_tcp_mss_check(SYSCTL_HANDLER_ARGS) 157 { 158 int error, new; 159 160 new = V_tcp_mssdflt; 161 error = sysctl_handle_int(oidp, &new, 0, req); 162 if (error == 0 && req->newptr) { 163 if (new < TCP_MINMSS) 164 error = EINVAL; 165 else 166 V_tcp_mssdflt = new; 167 } 168 return (error); 169 } 170 171 SYSCTL_PROC(_net_inet_tcp, TCPCTL_MSSDFLT, mssdflt, 172 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 173 &VNET_NAME(tcp_mssdflt), 0, &sysctl_net_inet_tcp_mss_check, "I", 174 "Default TCP Maximum Segment Size"); 175 176 #ifdef INET6 177 static int 178 sysctl_net_inet_tcp_mss_v6_check(SYSCTL_HANDLER_ARGS) 179 { 180 int error, new; 181 182 new = V_tcp_v6mssdflt; 183 error = sysctl_handle_int(oidp, &new, 0, req); 184 if (error == 0 && req->newptr) { 185 if (new < TCP_MINMSS) 186 error = EINVAL; 187 else 188 V_tcp_v6mssdflt = new; 189 } 190 return (error); 191 } 192 193 SYSCTL_PROC(_net_inet_tcp, TCPCTL_V6MSSDFLT, v6mssdflt, 194 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 195 &VNET_NAME(tcp_v6mssdflt), 0, &sysctl_net_inet_tcp_mss_v6_check, "I", 196 "Default TCP Maximum Segment Size for IPv6"); 197 #endif /* INET6 */ 198 199 /* 200 * Minimum MSS we accept and use. This prevents DoS attacks where 201 * we are forced to a ridiculous low MSS like 20 and send hundreds 202 * of packets instead of one. The effect scales with the available 203 * bandwidth and quickly saturates the CPU and network interface 204 * with packet generation and sending. Set to zero to disable MINMSS 205 * checking. This setting prevents us from sending too small packets. 206 */ 207 VNET_DEFINE(int, tcp_minmss) = TCP_MINMSS; 208 SYSCTL_INT(_net_inet_tcp, OID_AUTO, minmss, CTLFLAG_VNET | CTLFLAG_RW, 209 &VNET_NAME(tcp_minmss), 0, 210 "Minimum TCP Maximum Segment Size"); 211 212 VNET_DEFINE(int, tcp_do_rfc1323) = 1; 213 SYSCTL_INT(_net_inet_tcp, TCPCTL_DO_RFC1323, rfc1323, CTLFLAG_VNET | CTLFLAG_RW, 214 &VNET_NAME(tcp_do_rfc1323), 0, 215 "Enable rfc1323 (high performance TCP) extensions"); 216 217 /* 218 * As of June 2021, several TCP stacks violate RFC 7323 from September 2014. 219 * Some stacks negotiate TS, but never send them after connection setup. Some 220 * stacks negotiate TS, but don't send them when sending keep-alive segments. 221 * These include modern widely deployed TCP stacks. 222 * Therefore tolerating violations for now... 223 */ 224 VNET_DEFINE(int, tcp_tolerate_missing_ts) = 1; 225 SYSCTL_INT(_net_inet_tcp, OID_AUTO, tolerate_missing_ts, CTLFLAG_VNET | CTLFLAG_RW, 226 &VNET_NAME(tcp_tolerate_missing_ts), 0, 227 "Tolerate missing TCP timestamps"); 228 229 VNET_DEFINE(int, tcp_ts_offset_per_conn) = 1; 230 SYSCTL_INT(_net_inet_tcp, OID_AUTO, ts_offset_per_conn, CTLFLAG_VNET | CTLFLAG_RW, 231 &VNET_NAME(tcp_ts_offset_per_conn), 0, 232 "Initialize TCP timestamps per connection instead of per host pair"); 233 234 /* How many connections are pacing */ 235 static volatile uint32_t number_of_tcp_connections_pacing = 0; 236 static uint32_t shadow_num_connections = 0; 237 static counter_u64_t tcp_pacing_failures; 238 static counter_u64_t tcp_dgp_failures; 239 static uint32_t shadow_tcp_pacing_dgp = 0; 240 static volatile uint32_t number_of_dgp_connections = 0; 241 242 static int tcp_pacing_limit = 10000; 243 SYSCTL_INT(_net_inet_tcp, OID_AUTO, pacing_limit, CTLFLAG_RW, 244 &tcp_pacing_limit, 1000, 245 "If the TCP stack does pacing, is there a limit (-1 = no, 0 = no pacing N = number of connections)"); 246 247 static int tcp_dgp_limit = -1; 248 SYSCTL_INT(_net_inet_tcp, OID_AUTO, dgp_limit, CTLFLAG_RW, 249 &tcp_dgp_limit, -1, 250 "If the TCP stack does DGP, is there a limit (-1 = no, 0 = no dgp N = number of connections)"); 251 252 SYSCTL_UINT(_net_inet_tcp, OID_AUTO, pacing_count, CTLFLAG_RD, 253 &shadow_num_connections, 0, "Number of TCP connections being paced"); 254 255 SYSCTL_COUNTER_U64(_net_inet_tcp, OID_AUTO, pacing_failures, CTLFLAG_RD, 256 &tcp_pacing_failures, "Number of times we failed to enable pacing to avoid exceeding the limit"); 257 258 SYSCTL_COUNTER_U64(_net_inet_tcp, OID_AUTO, dgp_failures, CTLFLAG_RD, 259 &tcp_dgp_failures, "Number of times we failed to enable dgp to avoid exceeding the limit"); 260 261 static int tcp_log_debug = 0; 262 SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_debug, CTLFLAG_RW, 263 &tcp_log_debug, 0, "Log errors caused by incoming TCP segments"); 264 265 /* 266 * Target size of TCP PCB hash tables. Must be a power of two. 267 * 268 * Note that this can be overridden by the kernel environment 269 * variable net.inet.tcp.tcbhashsize 270 */ 271 #ifndef TCBHASHSIZE 272 #define TCBHASHSIZE 0 273 #endif 274 static int tcp_tcbhashsize = TCBHASHSIZE; 275 SYSCTL_INT(_net_inet_tcp, OID_AUTO, tcbhashsize, CTLFLAG_RDTUN, 276 &tcp_tcbhashsize, 0, "Size of TCP control-block hashtable"); 277 278 static int do_tcpdrain = 1; 279 SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_tcpdrain, CTLFLAG_RW, &do_tcpdrain, 0, 280 "Enable tcp_drain routine for extra help when low on mbufs"); 281 282 SYSCTL_UINT(_net_inet_tcp, OID_AUTO, pcbcount, CTLFLAG_VNET | CTLFLAG_RD, 283 &VNET_NAME(tcbinfo.ipi_count), 0, "Number of active PCBs"); 284 285 VNET_DEFINE_STATIC(int, icmp_may_rst) = 1; 286 #define V_icmp_may_rst VNET(icmp_may_rst) 287 SYSCTL_INT(_net_inet_tcp, OID_AUTO, icmp_may_rst, CTLFLAG_VNET | CTLFLAG_RW, 288 &VNET_NAME(icmp_may_rst), 0, 289 "Certain ICMP unreachable messages may abort connections in SYN_SENT"); 290 291 VNET_DEFINE_STATIC(int, tcp_isn_reseed_interval) = 0; 292 #define V_tcp_isn_reseed_interval VNET(tcp_isn_reseed_interval) 293 SYSCTL_INT(_net_inet_tcp, OID_AUTO, isn_reseed_interval, CTLFLAG_VNET | CTLFLAG_RW, 294 &VNET_NAME(tcp_isn_reseed_interval), 0, 295 "Seconds between reseeding of ISN secret"); 296 297 static int tcp_soreceive_stream; 298 SYSCTL_INT(_net_inet_tcp, OID_AUTO, soreceive_stream, CTLFLAG_RDTUN, 299 &tcp_soreceive_stream, 0, "Using soreceive_stream for TCP sockets"); 300 301 VNET_DEFINE(uma_zone_t, sack_hole_zone); 302 #define V_sack_hole_zone VNET(sack_hole_zone) 303 VNET_DEFINE(uint32_t, tcp_map_entries_limit) = 0; /* unlimited */ 304 static int 305 sysctl_net_inet_tcp_map_limit_check(SYSCTL_HANDLER_ARGS) 306 { 307 int error; 308 uint32_t new; 309 310 new = V_tcp_map_entries_limit; 311 error = sysctl_handle_int(oidp, &new, 0, req); 312 if (error == 0 && req->newptr) { 313 /* only allow "0" and value > minimum */ 314 if (new > 0 && new < TCP_MIN_MAP_ENTRIES_LIMIT) 315 error = EINVAL; 316 else 317 V_tcp_map_entries_limit = new; 318 } 319 return (error); 320 } 321 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, map_limit, 322 CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 323 &VNET_NAME(tcp_map_entries_limit), 0, 324 &sysctl_net_inet_tcp_map_limit_check, "IU", 325 "Total sendmap entries limit"); 326 327 VNET_DEFINE(uint32_t, tcp_map_split_limit) = 0; /* unlimited */ 328 SYSCTL_UINT(_net_inet_tcp, OID_AUTO, split_limit, CTLFLAG_VNET | CTLFLAG_RW, 329 &VNET_NAME(tcp_map_split_limit), 0, 330 "Total sendmap split entries limit"); 331 332 #ifdef TCP_HHOOK 333 VNET_DEFINE(struct hhook_head *, tcp_hhh[HHOOK_TCP_LAST+1]); 334 #endif 335 336 #define TS_OFFSET_SECRET_LENGTH SIPHASH_KEY_LENGTH 337 VNET_DEFINE_STATIC(u_char, ts_offset_secret[TS_OFFSET_SECRET_LENGTH]); 338 #define V_ts_offset_secret VNET(ts_offset_secret) 339 340 static int tcp_default_fb_init(struct tcpcb *tp, void **ptr); 341 static void tcp_default_fb_fini(struct tcpcb *tp, int tcb_is_purged); 342 static int tcp_default_handoff_ok(struct tcpcb *tp); 343 static struct inpcb *tcp_notify(struct inpcb *, int); 344 static struct inpcb *tcp_mtudisc_notify(struct inpcb *, int); 345 static struct inpcb *tcp_mtudisc(struct inpcb *, int); 346 static struct inpcb *tcp_drop_syn_sent(struct inpcb *, int); 347 static char * tcp_log_addr(struct in_conninfo *inc, struct tcphdr *th, 348 const void *ip4hdr, const void *ip6hdr); 349 static void tcp_default_switch_failed(struct tcpcb *tp); 350 static ipproto_ctlinput_t tcp_ctlinput; 351 static udp_tun_icmp_t tcp_ctlinput_viaudp; 352 353 static struct tcp_function_block tcp_def_funcblk = { 354 .tfb_tcp_block_name = "freebsd", 355 .tfb_tcp_output = tcp_default_output, 356 .tfb_tcp_do_segment = tcp_do_segment, 357 .tfb_tcp_ctloutput = tcp_default_ctloutput, 358 .tfb_tcp_handoff_ok = tcp_default_handoff_ok, 359 .tfb_tcp_fb_init = tcp_default_fb_init, 360 .tfb_tcp_fb_fini = tcp_default_fb_fini, 361 .tfb_switch_failed = tcp_default_switch_failed, 362 }; 363 364 static int tcp_fb_cnt = 0; 365 struct tcp_funchead t_functions; 366 VNET_DEFINE_STATIC(struct tcp_function_block *, tcp_func_set_ptr) = &tcp_def_funcblk; 367 #define V_tcp_func_set_ptr VNET(tcp_func_set_ptr) 368 369 void 370 tcp_record_dsack(struct tcpcb *tp, tcp_seq start, tcp_seq end, int tlp) 371 { 372 TCPSTAT_INC(tcps_dsack_count); 373 tp->t_dsack_pack++; 374 if (tlp == 0) { 375 if (SEQ_GT(end, start)) { 376 tp->t_dsack_bytes += (end - start); 377 TCPSTAT_ADD(tcps_dsack_bytes, (end - start)); 378 } else { 379 tp->t_dsack_tlp_bytes += (start - end); 380 TCPSTAT_ADD(tcps_dsack_bytes, (start - end)); 381 } 382 } else { 383 if (SEQ_GT(end, start)) { 384 tp->t_dsack_bytes += (end - start); 385 TCPSTAT_ADD(tcps_dsack_tlp_bytes, (end - start)); 386 } else { 387 tp->t_dsack_tlp_bytes += (start - end); 388 TCPSTAT_ADD(tcps_dsack_tlp_bytes, (start - end)); 389 } 390 } 391 } 392 393 static struct tcp_function_block * 394 find_tcp_functions_locked(struct tcp_function_set *fs) 395 { 396 struct tcp_function *f; 397 struct tcp_function_block *blk=NULL; 398 399 TAILQ_FOREACH(f, &t_functions, tf_next) { 400 if (strcmp(f->tf_name, fs->function_set_name) == 0) { 401 blk = f->tf_fb; 402 break; 403 } 404 } 405 return(blk); 406 } 407 408 static struct tcp_function_block * 409 find_tcp_fb_locked(struct tcp_function_block *blk, struct tcp_function **s) 410 { 411 struct tcp_function_block *rblk=NULL; 412 struct tcp_function *f; 413 414 TAILQ_FOREACH(f, &t_functions, tf_next) { 415 if (f->tf_fb == blk) { 416 rblk = blk; 417 if (s) { 418 *s = f; 419 } 420 break; 421 } 422 } 423 return (rblk); 424 } 425 426 struct tcp_function_block * 427 find_and_ref_tcp_functions(struct tcp_function_set *fs) 428 { 429 struct tcp_function_block *blk; 430 431 rw_rlock(&tcp_function_lock); 432 blk = find_tcp_functions_locked(fs); 433 if (blk) 434 refcount_acquire(&blk->tfb_refcnt); 435 rw_runlock(&tcp_function_lock); 436 return(blk); 437 } 438 439 struct tcp_function_block * 440 find_and_ref_tcp_fb(struct tcp_function_block *blk) 441 { 442 struct tcp_function_block *rblk; 443 444 rw_rlock(&tcp_function_lock); 445 rblk = find_tcp_fb_locked(blk, NULL); 446 if (rblk) 447 refcount_acquire(&rblk->tfb_refcnt); 448 rw_runlock(&tcp_function_lock); 449 return(rblk); 450 } 451 452 /* Find a matching alias for the given tcp_function_block. */ 453 int 454 find_tcp_function_alias(struct tcp_function_block *blk, 455 struct tcp_function_set *fs) 456 { 457 struct tcp_function *f; 458 int found; 459 460 found = 0; 461 rw_rlock(&tcp_function_lock); 462 TAILQ_FOREACH(f, &t_functions, tf_next) { 463 if ((f->tf_fb == blk) && 464 (strncmp(f->tf_name, blk->tfb_tcp_block_name, 465 TCP_FUNCTION_NAME_LEN_MAX) != 0)) { 466 /* Matching function block with different name. */ 467 strncpy(fs->function_set_name, f->tf_name, 468 TCP_FUNCTION_NAME_LEN_MAX); 469 found = 1; 470 break; 471 } 472 } 473 /* Null terminate the string appropriately. */ 474 if (found) { 475 fs->function_set_name[TCP_FUNCTION_NAME_LEN_MAX - 1] = '\0'; 476 } else { 477 fs->function_set_name[0] = '\0'; 478 } 479 rw_runlock(&tcp_function_lock); 480 return (found); 481 } 482 483 static struct tcp_function_block * 484 find_and_ref_tcp_default_fb(void) 485 { 486 struct tcp_function_block *rblk; 487 488 rw_rlock(&tcp_function_lock); 489 rblk = V_tcp_func_set_ptr; 490 refcount_acquire(&rblk->tfb_refcnt); 491 rw_runlock(&tcp_function_lock); 492 return (rblk); 493 } 494 495 void 496 tcp_switch_back_to_default(struct tcpcb *tp) 497 { 498 struct tcp_function_block *tfb; 499 void *ptr = NULL; 500 501 KASSERT(tp->t_fb != &tcp_def_funcblk, 502 ("%s: called by the built-in default stack", __func__)); 503 504 if (tp->t_fb->tfb_tcp_timer_stop_all != NULL) 505 tp->t_fb->tfb_tcp_timer_stop_all(tp); 506 507 /* 508 * Now, we'll find a new function block to use. 509 * Start by trying the current user-selected 510 * default, unless this stack is the user-selected 511 * default. 512 */ 513 tfb = find_and_ref_tcp_default_fb(); 514 if (tfb == tp->t_fb) { 515 refcount_release(&tfb->tfb_refcnt); 516 tfb = NULL; 517 } 518 /* Does the stack accept this connection? */ 519 if (tfb != NULL && tfb->tfb_tcp_handoff_ok != NULL && 520 (*tfb->tfb_tcp_handoff_ok)(tp)) { 521 refcount_release(&tfb->tfb_refcnt); 522 tfb = NULL; 523 } 524 /* Try to use that stack. */ 525 if (tfb != NULL) { 526 /* Initialize the new stack. If it succeeds, we are done. */ 527 if (tfb->tfb_tcp_fb_init == NULL || 528 (*tfb->tfb_tcp_fb_init)(tp, &ptr) == 0) { 529 /* Release the old stack */ 530 if (tp->t_fb->tfb_tcp_fb_fini != NULL) 531 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0); 532 refcount_release(&tp->t_fb->tfb_refcnt); 533 /* Now set in all the pointers */ 534 tp->t_fb = tfb; 535 tp->t_fb_ptr = ptr; 536 return; 537 } 538 /* 539 * Initialization failed. Release the reference count on 540 * the looked up default stack. 541 */ 542 refcount_release(&tfb->tfb_refcnt); 543 } 544 545 /* 546 * If that wasn't feasible, use the built-in default 547 * stack which is not allowed to reject anyone. 548 */ 549 tfb = find_and_ref_tcp_fb(&tcp_def_funcblk); 550 if (tfb == NULL) { 551 /* there always should be a default */ 552 panic("Can't refer to tcp_def_funcblk"); 553 } 554 if (tfb->tfb_tcp_handoff_ok != NULL) { 555 if ((*tfb->tfb_tcp_handoff_ok) (tp)) { 556 /* The default stack cannot say no */ 557 panic("Default stack rejects a new session?"); 558 } 559 } 560 if (tfb->tfb_tcp_fb_init != NULL && 561 (*tfb->tfb_tcp_fb_init)(tp, &ptr)) { 562 /* The default stack cannot fail */ 563 panic("Default stack initialization failed"); 564 } 565 /* Now release the old stack */ 566 if (tp->t_fb->tfb_tcp_fb_fini != NULL) 567 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 0); 568 refcount_release(&tp->t_fb->tfb_refcnt); 569 /* And set in the pointers to the new */ 570 tp->t_fb = tfb; 571 tp->t_fb_ptr = ptr; 572 } 573 574 static bool 575 tcp_recv_udp_tunneled_packet(struct mbuf *m, int off, struct inpcb *inp, 576 const struct sockaddr *sa, void *ctx) 577 { 578 struct ip *iph; 579 #ifdef INET6 580 struct ip6_hdr *ip6; 581 #endif 582 struct udphdr *uh; 583 struct tcphdr *th; 584 int thlen; 585 uint16_t port; 586 587 TCPSTAT_INC(tcps_tunneled_pkts); 588 if ((m->m_flags & M_PKTHDR) == 0) { 589 /* Can't handle one that is not a pkt hdr */ 590 TCPSTAT_INC(tcps_tunneled_errs); 591 goto out; 592 } 593 thlen = sizeof(struct tcphdr); 594 if (m->m_len < off + sizeof(struct udphdr) + thlen && 595 (m = m_pullup(m, off + sizeof(struct udphdr) + thlen)) == NULL) { 596 TCPSTAT_INC(tcps_tunneled_errs); 597 goto out; 598 } 599 iph = mtod(m, struct ip *); 600 uh = (struct udphdr *)((caddr_t)iph + off); 601 th = (struct tcphdr *)(uh + 1); 602 thlen = th->th_off << 2; 603 if (m->m_len < off + sizeof(struct udphdr) + thlen) { 604 m = m_pullup(m, off + sizeof(struct udphdr) + thlen); 605 if (m == NULL) { 606 TCPSTAT_INC(tcps_tunneled_errs); 607 goto out; 608 } else { 609 iph = mtod(m, struct ip *); 610 uh = (struct udphdr *)((caddr_t)iph + off); 611 th = (struct tcphdr *)(uh + 1); 612 } 613 } 614 m->m_pkthdr.tcp_tun_port = port = uh->uh_sport; 615 bcopy(th, uh, m->m_len - off); 616 m->m_len -= sizeof(struct udphdr); 617 m->m_pkthdr.len -= sizeof(struct udphdr); 618 /* 619 * We use the same algorithm for 620 * both UDP and TCP for c-sum. So 621 * the code in tcp_input will skip 622 * the checksum. So we do nothing 623 * with the flag (m->m_pkthdr.csum_flags). 624 */ 625 switch (iph->ip_v) { 626 #ifdef INET 627 case IPVERSION: 628 iph->ip_len = htons(ntohs(iph->ip_len) - sizeof(struct udphdr)); 629 tcp_input_with_port(&m, &off, IPPROTO_TCP, port); 630 break; 631 #endif 632 #ifdef INET6 633 case IPV6_VERSION >> 4: 634 ip6 = mtod(m, struct ip6_hdr *); 635 ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - sizeof(struct udphdr)); 636 tcp6_input_with_port(&m, &off, IPPROTO_TCP, port); 637 break; 638 #endif 639 default: 640 goto out; 641 break; 642 } 643 return (true); 644 out: 645 m_freem(m); 646 647 return (true); 648 } 649 650 static int 651 sysctl_net_inet_default_tcp_functions(SYSCTL_HANDLER_ARGS) 652 { 653 int error=ENOENT; 654 struct tcp_function_set fs; 655 struct tcp_function_block *blk; 656 657 memset(&fs, 0, sizeof(fs)); 658 rw_rlock(&tcp_function_lock); 659 blk = find_tcp_fb_locked(V_tcp_func_set_ptr, NULL); 660 if (blk) { 661 /* Found him */ 662 strcpy(fs.function_set_name, blk->tfb_tcp_block_name); 663 fs.pcbcnt = blk->tfb_refcnt; 664 } 665 rw_runlock(&tcp_function_lock); 666 error = sysctl_handle_string(oidp, fs.function_set_name, 667 sizeof(fs.function_set_name), req); 668 669 /* Check for error or no change */ 670 if (error != 0 || req->newptr == NULL) 671 return(error); 672 673 rw_wlock(&tcp_function_lock); 674 blk = find_tcp_functions_locked(&fs); 675 if ((blk == NULL) || 676 (blk->tfb_flags & TCP_FUNC_BEING_REMOVED)) { 677 error = ENOENT; 678 goto done; 679 } 680 V_tcp_func_set_ptr = blk; 681 done: 682 rw_wunlock(&tcp_function_lock); 683 return (error); 684 } 685 686 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, functions_default, 687 CTLFLAG_VNET | CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 688 NULL, 0, sysctl_net_inet_default_tcp_functions, "A", 689 "Set/get the default TCP functions"); 690 691 static int 692 sysctl_net_inet_list_available(SYSCTL_HANDLER_ARGS) 693 { 694 int error, cnt, linesz; 695 struct tcp_function *f; 696 char *buffer, *cp; 697 size_t bufsz, outsz; 698 bool alias; 699 700 cnt = 0; 701 rw_rlock(&tcp_function_lock); 702 TAILQ_FOREACH(f, &t_functions, tf_next) { 703 cnt++; 704 } 705 rw_runlock(&tcp_function_lock); 706 707 bufsz = (cnt+2) * ((TCP_FUNCTION_NAME_LEN_MAX * 2) + 13) + 1; 708 buffer = malloc(bufsz, M_TEMP, M_WAITOK); 709 710 error = 0; 711 cp = buffer; 712 713 linesz = snprintf(cp, bufsz, "\n%-32s%c %-32s %s\n", "Stack", 'D', 714 "Alias", "PCB count"); 715 cp += linesz; 716 bufsz -= linesz; 717 outsz = linesz; 718 719 rw_rlock(&tcp_function_lock); 720 TAILQ_FOREACH(f, &t_functions, tf_next) { 721 alias = (f->tf_name != f->tf_fb->tfb_tcp_block_name); 722 linesz = snprintf(cp, bufsz, "%-32s%c %-32s %u\n", 723 f->tf_fb->tfb_tcp_block_name, 724 (f->tf_fb == V_tcp_func_set_ptr) ? '*' : ' ', 725 alias ? f->tf_name : "-", 726 f->tf_fb->tfb_refcnt); 727 if (linesz >= bufsz) { 728 error = EOVERFLOW; 729 break; 730 } 731 cp += linesz; 732 bufsz -= linesz; 733 outsz += linesz; 734 } 735 rw_runlock(&tcp_function_lock); 736 if (error == 0) 737 error = sysctl_handle_string(oidp, buffer, outsz + 1, req); 738 free(buffer, M_TEMP); 739 return (error); 740 } 741 742 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, functions_available, 743 CTLFLAG_VNET | CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, 744 NULL, 0, sysctl_net_inet_list_available, "A", 745 "list available TCP Function sets"); 746 747 VNET_DEFINE(int, tcp_udp_tunneling_port) = TCP_TUNNELING_PORT_DEFAULT; 748 749 #ifdef INET 750 VNET_DEFINE(struct socket *, udp4_tun_socket) = NULL; 751 #define V_udp4_tun_socket VNET(udp4_tun_socket) 752 #endif 753 #ifdef INET6 754 VNET_DEFINE(struct socket *, udp6_tun_socket) = NULL; 755 #define V_udp6_tun_socket VNET(udp6_tun_socket) 756 #endif 757 758 static struct sx tcpoudp_lock; 759 760 static void 761 tcp_over_udp_stop(void) 762 { 763 764 sx_assert(&tcpoudp_lock, SA_XLOCKED); 765 766 #ifdef INET 767 if (V_udp4_tun_socket != NULL) { 768 soclose(V_udp4_tun_socket); 769 V_udp4_tun_socket = NULL; 770 } 771 #endif 772 #ifdef INET6 773 if (V_udp6_tun_socket != NULL) { 774 soclose(V_udp6_tun_socket); 775 V_udp6_tun_socket = NULL; 776 } 777 #endif 778 } 779 780 static int 781 tcp_over_udp_start(void) 782 { 783 uint16_t port; 784 int ret; 785 #ifdef INET 786 struct sockaddr_in sin; 787 #endif 788 #ifdef INET6 789 struct sockaddr_in6 sin6; 790 #endif 791 792 sx_assert(&tcpoudp_lock, SA_XLOCKED); 793 794 port = V_tcp_udp_tunneling_port; 795 if (ntohs(port) == 0) { 796 /* Must have a port set */ 797 return (EINVAL); 798 } 799 #ifdef INET 800 if (V_udp4_tun_socket != NULL) { 801 /* Already running -- must stop first */ 802 return (EALREADY); 803 } 804 #endif 805 #ifdef INET6 806 if (V_udp6_tun_socket != NULL) { 807 /* Already running -- must stop first */ 808 return (EALREADY); 809 } 810 #endif 811 #ifdef INET 812 if ((ret = socreate(PF_INET, &V_udp4_tun_socket, 813 SOCK_DGRAM, IPPROTO_UDP, 814 curthread->td_ucred, curthread))) { 815 tcp_over_udp_stop(); 816 return (ret); 817 } 818 /* Call the special UDP hook. */ 819 if ((ret = udp_set_kernel_tunneling(V_udp4_tun_socket, 820 tcp_recv_udp_tunneled_packet, 821 tcp_ctlinput_viaudp, 822 NULL))) { 823 tcp_over_udp_stop(); 824 return (ret); 825 } 826 /* Ok, we have a socket, bind it to the port. */ 827 memset(&sin, 0, sizeof(struct sockaddr_in)); 828 sin.sin_len = sizeof(struct sockaddr_in); 829 sin.sin_family = AF_INET; 830 sin.sin_port = htons(port); 831 if ((ret = sobind(V_udp4_tun_socket, 832 (struct sockaddr *)&sin, curthread))) { 833 tcp_over_udp_stop(); 834 return (ret); 835 } 836 #endif 837 #ifdef INET6 838 if ((ret = socreate(PF_INET6, &V_udp6_tun_socket, 839 SOCK_DGRAM, IPPROTO_UDP, 840 curthread->td_ucred, curthread))) { 841 tcp_over_udp_stop(); 842 return (ret); 843 } 844 /* Call the special UDP hook. */ 845 if ((ret = udp_set_kernel_tunneling(V_udp6_tun_socket, 846 tcp_recv_udp_tunneled_packet, 847 tcp6_ctlinput_viaudp, 848 NULL))) { 849 tcp_over_udp_stop(); 850 return (ret); 851 } 852 /* Ok, we have a socket, bind it to the port. */ 853 memset(&sin6, 0, sizeof(struct sockaddr_in6)); 854 sin6.sin6_len = sizeof(struct sockaddr_in6); 855 sin6.sin6_family = AF_INET6; 856 sin6.sin6_port = htons(port); 857 if ((ret = sobind(V_udp6_tun_socket, 858 (struct sockaddr *)&sin6, curthread))) { 859 tcp_over_udp_stop(); 860 return (ret); 861 } 862 #endif 863 return (0); 864 } 865 866 static int 867 sysctl_net_inet_tcp_udp_tunneling_port_check(SYSCTL_HANDLER_ARGS) 868 { 869 int error; 870 uint32_t old, new; 871 872 old = V_tcp_udp_tunneling_port; 873 new = old; 874 error = sysctl_handle_int(oidp, &new, 0, req); 875 if ((error == 0) && 876 (req->newptr != NULL)) { 877 if ((new < TCP_TUNNELING_PORT_MIN) || 878 (new > TCP_TUNNELING_PORT_MAX)) { 879 error = EINVAL; 880 } else { 881 sx_xlock(&tcpoudp_lock); 882 V_tcp_udp_tunneling_port = new; 883 if (old != 0) { 884 tcp_over_udp_stop(); 885 } 886 if (new != 0) { 887 error = tcp_over_udp_start(); 888 if (error != 0) { 889 V_tcp_udp_tunneling_port = 0; 890 } 891 } 892 sx_xunlock(&tcpoudp_lock); 893 } 894 } 895 return (error); 896 } 897 898 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, udp_tunneling_port, 899 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 900 &VNET_NAME(tcp_udp_tunneling_port), 901 0, &sysctl_net_inet_tcp_udp_tunneling_port_check, "IU", 902 "Tunneling port for tcp over udp"); 903 904 VNET_DEFINE(int, tcp_udp_tunneling_overhead) = TCP_TUNNELING_OVERHEAD_DEFAULT; 905 906 static int 907 sysctl_net_inet_tcp_udp_tunneling_overhead_check(SYSCTL_HANDLER_ARGS) 908 { 909 int error, new; 910 911 new = V_tcp_udp_tunneling_overhead; 912 error = sysctl_handle_int(oidp, &new, 0, req); 913 if (error == 0 && req->newptr) { 914 if ((new < TCP_TUNNELING_OVERHEAD_MIN) || 915 (new > TCP_TUNNELING_OVERHEAD_MAX)) 916 error = EINVAL; 917 else 918 V_tcp_udp_tunneling_overhead = new; 919 } 920 return (error); 921 } 922 923 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, udp_tunneling_overhead, 924 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 925 &VNET_NAME(tcp_udp_tunneling_overhead), 926 0, &sysctl_net_inet_tcp_udp_tunneling_overhead_check, "IU", 927 "MSS reduction when using tcp over udp"); 928 929 /* 930 * Exports one (struct tcp_function_info) for each alias/name. 931 */ 932 static int 933 sysctl_net_inet_list_func_info(SYSCTL_HANDLER_ARGS) 934 { 935 int cnt, error; 936 struct tcp_function *f; 937 struct tcp_function_info tfi; 938 939 /* 940 * We don't allow writes. 941 */ 942 if (req->newptr != NULL) 943 return (EINVAL); 944 945 /* 946 * Wire the old buffer so we can directly copy the functions to 947 * user space without dropping the lock. 948 */ 949 if (req->oldptr != NULL) { 950 error = sysctl_wire_old_buffer(req, 0); 951 if (error) 952 return (error); 953 } 954 955 /* 956 * Walk the list and copy out matching entries. If INVARIANTS 957 * is compiled in, also walk the list to verify the length of 958 * the list matches what we have recorded. 959 */ 960 rw_rlock(&tcp_function_lock); 961 962 cnt = 0; 963 #ifndef INVARIANTS 964 if (req->oldptr == NULL) { 965 cnt = tcp_fb_cnt; 966 goto skip_loop; 967 } 968 #endif 969 TAILQ_FOREACH(f, &t_functions, tf_next) { 970 #ifdef INVARIANTS 971 cnt++; 972 #endif 973 if (req->oldptr != NULL) { 974 bzero(&tfi, sizeof(tfi)); 975 tfi.tfi_refcnt = f->tf_fb->tfb_refcnt; 976 tfi.tfi_id = f->tf_fb->tfb_id; 977 (void)strlcpy(tfi.tfi_alias, f->tf_name, 978 sizeof(tfi.tfi_alias)); 979 (void)strlcpy(tfi.tfi_name, 980 f->tf_fb->tfb_tcp_block_name, sizeof(tfi.tfi_name)); 981 error = SYSCTL_OUT(req, &tfi, sizeof(tfi)); 982 /* 983 * Don't stop on error, as that is the 984 * mechanism we use to accumulate length 985 * information if the buffer was too short. 986 */ 987 } 988 } 989 KASSERT(cnt == tcp_fb_cnt, 990 ("%s: cnt (%d) != tcp_fb_cnt (%d)", __func__, cnt, tcp_fb_cnt)); 991 #ifndef INVARIANTS 992 skip_loop: 993 #endif 994 rw_runlock(&tcp_function_lock); 995 if (req->oldptr == NULL) 996 error = SYSCTL_OUT(req, NULL, 997 (cnt + 1) * sizeof(struct tcp_function_info)); 998 999 return (error); 1000 } 1001 1002 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, function_info, 1003 CTLTYPE_OPAQUE | CTLFLAG_SKIP | CTLFLAG_RD | CTLFLAG_MPSAFE, 1004 NULL, 0, sysctl_net_inet_list_func_info, "S,tcp_function_info", 1005 "List TCP function block name-to-ID mappings"); 1006 1007 /* 1008 * tfb_tcp_handoff_ok() function for the default stack. 1009 * Note that we'll basically try to take all comers. 1010 */ 1011 static int 1012 tcp_default_handoff_ok(struct tcpcb *tp) 1013 { 1014 1015 return (0); 1016 } 1017 1018 /* 1019 * tfb_tcp_fb_init() function for the default stack. 1020 * 1021 * This handles making sure we have appropriate timers set if you are 1022 * transitioning a socket that has some amount of setup done. 1023 * 1024 * The init() fuction from the default can *never* return non-zero i.e. 1025 * it is required to always succeed since it is the stack of last resort! 1026 */ 1027 static int 1028 tcp_default_fb_init(struct tcpcb *tp, void **ptr) 1029 { 1030 struct socket *so = tptosocket(tp); 1031 int rexmt; 1032 1033 INP_WLOCK_ASSERT(tptoinpcb(tp)); 1034 /* We don't use the pointer */ 1035 *ptr = NULL; 1036 1037 KASSERT(tp->t_state < TCPS_TIME_WAIT, 1038 ("%s: connection %p in unexpected state %d", __func__, tp, 1039 tp->t_state)); 1040 1041 /* Make sure we get no interesting mbuf queuing behavior */ 1042 /* All mbuf queue/ack compress flags should be off */ 1043 tcp_lro_features_off(tp); 1044 1045 /* Cancel the GP measurement in progress */ 1046 tp->t_flags &= ~TF_GPUTINPROG; 1047 /* Validate the timers are not in usec, if they are convert */ 1048 tcp_change_time_units(tp, TCP_TMR_GRANULARITY_TICKS); 1049 if ((tp->t_state == TCPS_SYN_SENT) || 1050 (tp->t_state == TCPS_SYN_RECEIVED)) 1051 rexmt = tcp_rexmit_initial * tcp_backoff[tp->t_rxtshift]; 1052 else 1053 rexmt = TCP_REXMTVAL(tp) * tcp_backoff[tp->t_rxtshift]; 1054 if (tp->t_rxtshift == 0) 1055 tp->t_rxtcur = rexmt; 1056 else 1057 TCPT_RANGESET(tp->t_rxtcur, rexmt, tp->t_rttmin, TCPTV_REXMTMAX); 1058 1059 /* 1060 * Nothing to do for ESTABLISHED or LISTEN states. And, we don't 1061 * know what to do for unexpected states (which includes TIME_WAIT). 1062 */ 1063 if (tp->t_state <= TCPS_LISTEN || tp->t_state >= TCPS_TIME_WAIT) 1064 return (0); 1065 1066 /* 1067 * Make sure some kind of transmission timer is set if there is 1068 * outstanding data. 1069 */ 1070 if ((!TCPS_HAVEESTABLISHED(tp->t_state) || sbavail(&so->so_snd) || 1071 tp->snd_una != tp->snd_max) && !(tcp_timer_active(tp, TT_REXMT) || 1072 tcp_timer_active(tp, TT_PERSIST))) { 1073 /* 1074 * If the session has established and it looks like it should 1075 * be in the persist state, set the persist timer. Otherwise, 1076 * set the retransmit timer. 1077 */ 1078 if (TCPS_HAVEESTABLISHED(tp->t_state) && tp->snd_wnd == 0 && 1079 (int32_t)(tp->snd_nxt - tp->snd_una) < 1080 (int32_t)sbavail(&so->so_snd)) 1081 tcp_setpersist(tp); 1082 else 1083 tcp_timer_activate(tp, TT_REXMT, TP_RXTCUR(tp)); 1084 } 1085 1086 /* All non-embryonic sessions get a keepalive timer. */ 1087 if (!tcp_timer_active(tp, TT_KEEP)) 1088 tcp_timer_activate(tp, TT_KEEP, 1089 TCPS_HAVEESTABLISHED(tp->t_state) ? TP_KEEPIDLE(tp) : 1090 TP_KEEPINIT(tp)); 1091 1092 /* 1093 * Make sure critical variables are initialized 1094 * if transitioning while in Recovery. 1095 */ 1096 if IN_FASTRECOVERY(tp->t_flags) { 1097 if (tp->sackhint.recover_fs == 0) 1098 tp->sackhint.recover_fs = max(1, 1099 tp->snd_nxt - tp->snd_una); 1100 } 1101 1102 return (0); 1103 } 1104 1105 /* 1106 * tfb_tcp_fb_fini() function for the default stack. 1107 * 1108 * This changes state as necessary (or prudent) to prepare for another stack 1109 * to assume responsibility for the connection. 1110 */ 1111 static void 1112 tcp_default_fb_fini(struct tcpcb *tp, int tcb_is_purged) 1113 { 1114 1115 INP_WLOCK_ASSERT(tptoinpcb(tp)); 1116 1117 #ifdef TCP_BLACKBOX 1118 tcp_log_flowend(tp); 1119 #endif 1120 tp->t_acktime = 0; 1121 return; 1122 } 1123 1124 MALLOC_DEFINE(M_TCPLOG, "tcplog", "TCP address and flags print buffers"); 1125 MALLOC_DEFINE(M_TCPFUNCTIONS, "tcpfunc", "TCP function set memory"); 1126 1127 static struct mtx isn_mtx; 1128 1129 #define ISN_LOCK_INIT() mtx_init(&isn_mtx, "isn_mtx", NULL, MTX_DEF) 1130 #define ISN_LOCK() mtx_lock(&isn_mtx) 1131 #define ISN_UNLOCK() mtx_unlock(&isn_mtx) 1132 1133 INPCBSTORAGE_DEFINE(tcpcbstor, tcpcb, "tcpinp", "tcp_inpcb", "tcp", "tcphash"); 1134 1135 /* 1136 * Take a value and get the next power of 2 that doesn't overflow. 1137 * Used to size the tcp_inpcb hash buckets. 1138 */ 1139 static int 1140 maketcp_hashsize(int size) 1141 { 1142 int hashsize; 1143 1144 /* 1145 * auto tune. 1146 * get the next power of 2 higher than maxsockets. 1147 */ 1148 hashsize = 1 << fls(size); 1149 /* catch overflow, and just go one power of 2 smaller */ 1150 if (hashsize < size) { 1151 hashsize = 1 << (fls(size) - 1); 1152 } 1153 return (hashsize); 1154 } 1155 1156 static volatile int next_tcp_stack_id = 1; 1157 1158 /* 1159 * Register a TCP function block with the name provided in the names 1160 * array. (Note that this function does NOT automatically register 1161 * blk->tfb_tcp_block_name as a stack name. Therefore, you should 1162 * explicitly include blk->tfb_tcp_block_name in the list of names if 1163 * you wish to register the stack with that name.) 1164 * 1165 * Either all name registrations will succeed or all will fail. If 1166 * a name registration fails, the function will update the num_names 1167 * argument to point to the array index of the name that encountered 1168 * the failure. 1169 * 1170 * Returns 0 on success, or an error code on failure. 1171 */ 1172 int 1173 register_tcp_functions_as_names(struct tcp_function_block *blk, int wait, 1174 const char *names[], int *num_names) 1175 { 1176 struct tcp_function *n; 1177 struct tcp_function_set fs; 1178 int error, i; 1179 1180 KASSERT(names != NULL && *num_names > 0, 1181 ("%s: Called with 0-length name list", __func__)); 1182 KASSERT(names != NULL, ("%s: Called with NULL name list", __func__)); 1183 KASSERT(rw_initialized(&tcp_function_lock), 1184 ("%s: called too early", __func__)); 1185 1186 if ((blk->tfb_tcp_output == NULL) || 1187 (blk->tfb_tcp_do_segment == NULL) || 1188 (blk->tfb_tcp_ctloutput == NULL) || 1189 (strlen(blk->tfb_tcp_block_name) == 0)) { 1190 /* 1191 * These functions are required and you 1192 * need a name. 1193 */ 1194 *num_names = 0; 1195 return (EINVAL); 1196 } 1197 1198 if (blk->tfb_flags & TCP_FUNC_BEING_REMOVED) { 1199 *num_names = 0; 1200 return (EINVAL); 1201 } 1202 1203 refcount_init(&blk->tfb_refcnt, 0); 1204 blk->tfb_id = atomic_fetchadd_int(&next_tcp_stack_id, 1); 1205 for (i = 0; i < *num_names; i++) { 1206 n = malloc(sizeof(struct tcp_function), M_TCPFUNCTIONS, wait); 1207 if (n == NULL) { 1208 error = ENOMEM; 1209 goto cleanup; 1210 } 1211 n->tf_fb = blk; 1212 1213 (void)strlcpy(fs.function_set_name, names[i], 1214 sizeof(fs.function_set_name)); 1215 rw_wlock(&tcp_function_lock); 1216 if (find_tcp_functions_locked(&fs) != NULL) { 1217 /* Duplicate name space not allowed */ 1218 rw_wunlock(&tcp_function_lock); 1219 free(n, M_TCPFUNCTIONS); 1220 error = EALREADY; 1221 goto cleanup; 1222 } 1223 (void)strlcpy(n->tf_name, names[i], sizeof(n->tf_name)); 1224 TAILQ_INSERT_TAIL(&t_functions, n, tf_next); 1225 tcp_fb_cnt++; 1226 rw_wunlock(&tcp_function_lock); 1227 } 1228 return(0); 1229 1230 cleanup: 1231 /* 1232 * Deregister the names we just added. Because registration failed 1233 * for names[i], we don't need to deregister that name. 1234 */ 1235 *num_names = i; 1236 rw_wlock(&tcp_function_lock); 1237 while (--i >= 0) { 1238 TAILQ_FOREACH(n, &t_functions, tf_next) { 1239 if (!strncmp(n->tf_name, names[i], 1240 TCP_FUNCTION_NAME_LEN_MAX)) { 1241 TAILQ_REMOVE(&t_functions, n, tf_next); 1242 tcp_fb_cnt--; 1243 n->tf_fb = NULL; 1244 free(n, M_TCPFUNCTIONS); 1245 break; 1246 } 1247 } 1248 } 1249 rw_wunlock(&tcp_function_lock); 1250 return (error); 1251 } 1252 1253 /* 1254 * Register a TCP function block using the name provided in the name 1255 * argument. 1256 * 1257 * Returns 0 on success, or an error code on failure. 1258 */ 1259 int 1260 register_tcp_functions_as_name(struct tcp_function_block *blk, const char *name, 1261 int wait) 1262 { 1263 const char *name_list[1]; 1264 int num_names, rv; 1265 1266 num_names = 1; 1267 if (name != NULL) 1268 name_list[0] = name; 1269 else 1270 name_list[0] = blk->tfb_tcp_block_name; 1271 rv = register_tcp_functions_as_names(blk, wait, name_list, &num_names); 1272 return (rv); 1273 } 1274 1275 /* 1276 * Register a TCP function block using the name defined in 1277 * blk->tfb_tcp_block_name. 1278 * 1279 * Returns 0 on success, or an error code on failure. 1280 */ 1281 int 1282 register_tcp_functions(struct tcp_function_block *blk, int wait) 1283 { 1284 1285 return (register_tcp_functions_as_name(blk, NULL, wait)); 1286 } 1287 1288 /* 1289 * Deregister all names associated with a function block. This 1290 * functionally removes the function block from use within the system. 1291 * 1292 * When called with a true quiesce argument, mark the function block 1293 * as being removed so no more stacks will use it and determine 1294 * whether the removal would succeed. 1295 * 1296 * When called with a false quiesce argument, actually attempt the 1297 * removal. 1298 * 1299 * When called with a force argument, attempt to switch all TCBs to 1300 * use the default stack instead of returning EBUSY. 1301 * 1302 * Returns 0 on success (or if the removal would succeed), or an error 1303 * code on failure. 1304 */ 1305 int 1306 deregister_tcp_functions(struct tcp_function_block *blk, bool quiesce, 1307 bool force) 1308 { 1309 struct tcp_function *f; 1310 VNET_ITERATOR_DECL(vnet_iter); 1311 1312 if (blk == &tcp_def_funcblk) { 1313 /* You can't un-register the default */ 1314 return (EPERM); 1315 } 1316 rw_wlock(&tcp_function_lock); 1317 VNET_LIST_RLOCK_NOSLEEP(); 1318 VNET_FOREACH(vnet_iter) { 1319 CURVNET_SET(vnet_iter); 1320 if (blk == V_tcp_func_set_ptr) { 1321 /* You can't free the current default in some vnet. */ 1322 CURVNET_RESTORE(); 1323 VNET_LIST_RUNLOCK_NOSLEEP(); 1324 rw_wunlock(&tcp_function_lock); 1325 return (EBUSY); 1326 } 1327 CURVNET_RESTORE(); 1328 } 1329 VNET_LIST_RUNLOCK_NOSLEEP(); 1330 /* Mark the block so no more stacks can use it. */ 1331 blk->tfb_flags |= TCP_FUNC_BEING_REMOVED; 1332 /* 1333 * If TCBs are still attached to the stack, attempt to switch them 1334 * to the default stack. 1335 */ 1336 if (force && blk->tfb_refcnt) { 1337 struct inpcb *inp; 1338 struct tcpcb *tp; 1339 VNET_ITERATOR_DECL(vnet_iter); 1340 1341 rw_wunlock(&tcp_function_lock); 1342 1343 VNET_LIST_RLOCK(); 1344 VNET_FOREACH(vnet_iter) { 1345 CURVNET_SET(vnet_iter); 1346 struct inpcb_iterator inpi = INP_ALL_ITERATOR(&V_tcbinfo, 1347 INPLOOKUP_WLOCKPCB); 1348 1349 while ((inp = inp_next(&inpi)) != NULL) { 1350 tp = intotcpcb(inp); 1351 if (tp == NULL || tp->t_fb != blk) 1352 continue; 1353 tcp_switch_back_to_default(tp); 1354 } 1355 CURVNET_RESTORE(); 1356 } 1357 VNET_LIST_RUNLOCK(); 1358 1359 rw_wlock(&tcp_function_lock); 1360 } 1361 if (blk->tfb_refcnt) { 1362 /* TCBs still attached. */ 1363 rw_wunlock(&tcp_function_lock); 1364 return (EBUSY); 1365 } 1366 if (quiesce) { 1367 /* Skip removal. */ 1368 rw_wunlock(&tcp_function_lock); 1369 return (0); 1370 } 1371 /* Remove any function names that map to this function block. */ 1372 while (find_tcp_fb_locked(blk, &f) != NULL) { 1373 TAILQ_REMOVE(&t_functions, f, tf_next); 1374 tcp_fb_cnt--; 1375 f->tf_fb = NULL; 1376 free(f, M_TCPFUNCTIONS); 1377 } 1378 rw_wunlock(&tcp_function_lock); 1379 return (0); 1380 } 1381 1382 static void 1383 tcp_drain(void) 1384 { 1385 struct epoch_tracker et; 1386 VNET_ITERATOR_DECL(vnet_iter); 1387 1388 if (!do_tcpdrain) 1389 return; 1390 1391 NET_EPOCH_ENTER(et); 1392 VNET_LIST_RLOCK_NOSLEEP(); 1393 VNET_FOREACH(vnet_iter) { 1394 CURVNET_SET(vnet_iter); 1395 struct inpcb_iterator inpi = INP_ALL_ITERATOR(&V_tcbinfo, 1396 INPLOOKUP_WLOCKPCB); 1397 struct inpcb *inpb; 1398 struct tcpcb *tcpb; 1399 1400 /* 1401 * Walk the tcpbs, if existing, and flush the reassembly queue, 1402 * if there is one... 1403 * XXX: The "Net/3" implementation doesn't imply that the TCP 1404 * reassembly queue should be flushed, but in a situation 1405 * where we're really low on mbufs, this is potentially 1406 * useful. 1407 */ 1408 while ((inpb = inp_next(&inpi)) != NULL) { 1409 if ((tcpb = intotcpcb(inpb)) != NULL) { 1410 tcp_reass_flush(tcpb); 1411 tcp_clean_sackreport(tcpb); 1412 #ifdef TCP_BLACKBOX 1413 tcp_log_drain(tcpb); 1414 #endif 1415 #ifdef TCPPCAP 1416 if (tcp_pcap_aggressive_free) { 1417 /* Free the TCP PCAP queues. */ 1418 tcp_pcap_drain(&(tcpb->t_inpkts)); 1419 tcp_pcap_drain(&(tcpb->t_outpkts)); 1420 } 1421 #endif 1422 } 1423 } 1424 CURVNET_RESTORE(); 1425 } 1426 VNET_LIST_RUNLOCK_NOSLEEP(); 1427 NET_EPOCH_EXIT(et); 1428 } 1429 1430 static void 1431 tcp_vnet_init(void *arg __unused) 1432 { 1433 1434 #ifdef TCP_HHOOK 1435 if (hhook_head_register(HHOOK_TYPE_TCP, HHOOK_TCP_EST_IN, 1436 &V_tcp_hhh[HHOOK_TCP_EST_IN], HHOOK_NOWAIT|HHOOK_HEADISINVNET) != 0) 1437 printf("%s: WARNING: unable to register helper hook\n", __func__); 1438 if (hhook_head_register(HHOOK_TYPE_TCP, HHOOK_TCP_EST_OUT, 1439 &V_tcp_hhh[HHOOK_TCP_EST_OUT], HHOOK_NOWAIT|HHOOK_HEADISINVNET) != 0) 1440 printf("%s: WARNING: unable to register helper hook\n", __func__); 1441 #endif 1442 #ifdef STATS 1443 if (tcp_stats_init()) 1444 printf("%s: WARNING: unable to initialise TCP stats\n", 1445 __func__); 1446 #endif 1447 in_pcbinfo_init(&V_tcbinfo, &tcpcbstor, tcp_tcbhashsize, 1448 tcp_tcbhashsize); 1449 1450 syncache_init(); 1451 tcp_hc_init(); 1452 1453 TUNABLE_INT_FETCH("net.inet.tcp.sack.enable", &V_tcp_do_sack); 1454 V_sack_hole_zone = uma_zcreate("sackhole", sizeof(struct sackhole), 1455 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1456 1457 tcp_fastopen_init(); 1458 1459 COUNTER_ARRAY_ALLOC(V_tcps_states, TCP_NSTATES, M_WAITOK); 1460 VNET_PCPUSTAT_ALLOC(tcpstat, M_WAITOK); 1461 1462 V_tcp_msl = TCPTV_MSL; 1463 } 1464 VNET_SYSINIT(tcp_vnet_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, 1465 tcp_vnet_init, NULL); 1466 1467 static void 1468 tcp_init(void *arg __unused) 1469 { 1470 int hashsize; 1471 1472 tcp_reass_global_init(); 1473 1474 /* XXX virtualize those below? */ 1475 tcp_delacktime = TCPTV_DELACK; 1476 tcp_keepinit = TCPTV_KEEP_INIT; 1477 tcp_keepidle = TCPTV_KEEP_IDLE; 1478 tcp_keepintvl = TCPTV_KEEPINTVL; 1479 tcp_maxpersistidle = TCPTV_KEEP_IDLE; 1480 tcp_rexmit_initial = TCPTV_RTOBASE; 1481 if (tcp_rexmit_initial < 1) 1482 tcp_rexmit_initial = 1; 1483 tcp_rexmit_min = TCPTV_MIN; 1484 if (tcp_rexmit_min < 1) 1485 tcp_rexmit_min = 1; 1486 tcp_persmin = TCPTV_PERSMIN; 1487 tcp_persmax = TCPTV_PERSMAX; 1488 tcp_rexmit_slop = TCPTV_CPU_VAR; 1489 tcp_finwait2_timeout = TCPTV_FINWAIT2_TIMEOUT; 1490 1491 /* Setup the tcp function block list */ 1492 TAILQ_INIT(&t_functions); 1493 rw_init(&tcp_function_lock, "tcp_func_lock"); 1494 register_tcp_functions(&tcp_def_funcblk, M_WAITOK); 1495 sx_init(&tcpoudp_lock, "TCP over UDP configuration"); 1496 #ifdef TCP_BLACKBOX 1497 /* Initialize the TCP logging data. */ 1498 tcp_log_init(); 1499 #endif 1500 arc4rand(&V_ts_offset_secret, sizeof(V_ts_offset_secret), 0); 1501 1502 if (tcp_soreceive_stream) { 1503 #ifdef INET 1504 tcp_protosw.pr_soreceive = soreceive_stream; 1505 #endif 1506 #ifdef INET6 1507 tcp6_protosw.pr_soreceive = soreceive_stream; 1508 #endif /* INET6 */ 1509 } 1510 1511 #ifdef INET6 1512 max_protohdr_grow(sizeof(struct ip6_hdr) + sizeof(struct tcphdr)); 1513 #else /* INET6 */ 1514 max_protohdr_grow(sizeof(struct tcpiphdr)); 1515 #endif /* INET6 */ 1516 1517 ISN_LOCK_INIT(); 1518 EVENTHANDLER_REGISTER(shutdown_pre_sync, tcp_fini, NULL, 1519 SHUTDOWN_PRI_DEFAULT); 1520 EVENTHANDLER_REGISTER(vm_lowmem, tcp_drain, NULL, LOWMEM_PRI_DEFAULT); 1521 EVENTHANDLER_REGISTER(mbuf_lowmem, tcp_drain, NULL, LOWMEM_PRI_DEFAULT); 1522 1523 tcp_inp_lro_direct_queue = counter_u64_alloc(M_WAITOK); 1524 tcp_inp_lro_wokeup_queue = counter_u64_alloc(M_WAITOK); 1525 tcp_inp_lro_compressed = counter_u64_alloc(M_WAITOK); 1526 tcp_inp_lro_locks_taken = counter_u64_alloc(M_WAITOK); 1527 tcp_extra_mbuf = counter_u64_alloc(M_WAITOK); 1528 tcp_would_have_but = counter_u64_alloc(M_WAITOK); 1529 tcp_comp_total = counter_u64_alloc(M_WAITOK); 1530 tcp_uncomp_total = counter_u64_alloc(M_WAITOK); 1531 tcp_bad_csums = counter_u64_alloc(M_WAITOK); 1532 tcp_pacing_failures = counter_u64_alloc(M_WAITOK); 1533 tcp_dgp_failures = counter_u64_alloc(M_WAITOK); 1534 #ifdef TCPPCAP 1535 tcp_pcap_init(); 1536 #endif 1537 1538 hashsize = tcp_tcbhashsize; 1539 if (hashsize == 0) { 1540 /* 1541 * Auto tune the hash size based on maxsockets. 1542 * A perfect hash would have a 1:1 mapping 1543 * (hashsize = maxsockets) however it's been 1544 * suggested that O(2) average is better. 1545 */ 1546 hashsize = maketcp_hashsize(maxsockets / 4); 1547 /* 1548 * Our historical default is 512, 1549 * do not autotune lower than this. 1550 */ 1551 if (hashsize < 512) 1552 hashsize = 512; 1553 if (bootverbose) 1554 printf("%s: %s auto tuned to %d\n", __func__, 1555 "net.inet.tcp.tcbhashsize", hashsize); 1556 } 1557 /* 1558 * We require a hashsize to be a power of two. 1559 * Previously if it was not a power of two we would just reset it 1560 * back to 512, which could be a nasty surprise if you did not notice 1561 * the error message. 1562 * Instead what we do is clip it to the closest power of two lower 1563 * than the specified hash value. 1564 */ 1565 if (!powerof2(hashsize)) { 1566 int oldhashsize = hashsize; 1567 1568 hashsize = maketcp_hashsize(hashsize); 1569 /* prevent absurdly low value */ 1570 if (hashsize < 16) 1571 hashsize = 16; 1572 printf("%s: WARNING: TCB hash size not a power of 2, " 1573 "clipped from %d to %d.\n", __func__, oldhashsize, 1574 hashsize); 1575 } 1576 tcp_tcbhashsize = hashsize; 1577 1578 #ifdef INET 1579 IPPROTO_REGISTER(IPPROTO_TCP, tcp_input, tcp_ctlinput); 1580 #endif 1581 #ifdef INET6 1582 IP6PROTO_REGISTER(IPPROTO_TCP, tcp6_input, tcp6_ctlinput); 1583 #endif 1584 } 1585 SYSINIT(tcp_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, tcp_init, NULL); 1586 1587 #ifdef VIMAGE 1588 static void 1589 tcp_destroy(void *unused __unused) 1590 { 1591 int n; 1592 #ifdef TCP_HHOOK 1593 int error; 1594 #endif 1595 1596 /* 1597 * All our processes are gone, all our sockets should be cleaned 1598 * up, which means, we should be past the tcp_discardcb() calls. 1599 * Sleep to let all tcpcb timers really disappear and cleanup. 1600 */ 1601 for (;;) { 1602 INP_INFO_WLOCK(&V_tcbinfo); 1603 n = V_tcbinfo.ipi_count; 1604 INP_INFO_WUNLOCK(&V_tcbinfo); 1605 if (n == 0) 1606 break; 1607 pause("tcpdes", hz / 10); 1608 } 1609 tcp_hc_destroy(); 1610 syncache_destroy(); 1611 in_pcbinfo_destroy(&V_tcbinfo); 1612 /* tcp_discardcb() clears the sack_holes up. */ 1613 uma_zdestroy(V_sack_hole_zone); 1614 1615 /* 1616 * Cannot free the zone until all tcpcbs are released as we attach 1617 * the allocations to them. 1618 */ 1619 tcp_fastopen_destroy(); 1620 1621 COUNTER_ARRAY_FREE(V_tcps_states, TCP_NSTATES); 1622 VNET_PCPUSTAT_FREE(tcpstat); 1623 1624 #ifdef TCP_HHOOK 1625 error = hhook_head_deregister(V_tcp_hhh[HHOOK_TCP_EST_IN]); 1626 if (error != 0) { 1627 printf("%s: WARNING: unable to deregister helper hook " 1628 "type=%d, id=%d: error %d returned\n", __func__, 1629 HHOOK_TYPE_TCP, HHOOK_TCP_EST_IN, error); 1630 } 1631 error = hhook_head_deregister(V_tcp_hhh[HHOOK_TCP_EST_OUT]); 1632 if (error != 0) { 1633 printf("%s: WARNING: unable to deregister helper hook " 1634 "type=%d, id=%d: error %d returned\n", __func__, 1635 HHOOK_TYPE_TCP, HHOOK_TCP_EST_OUT, error); 1636 } 1637 #endif 1638 } 1639 VNET_SYSUNINIT(tcp, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, tcp_destroy, NULL); 1640 #endif 1641 1642 void 1643 tcp_fini(void *xtp) 1644 { 1645 1646 } 1647 1648 /* 1649 * Fill in the IP and TCP headers for an outgoing packet, given the tcpcb. 1650 * tcp_template used to store this data in mbufs, but we now recopy it out 1651 * of the tcpcb each time to conserve mbufs. 1652 */ 1653 void 1654 tcpip_fillheaders(struct inpcb *inp, uint16_t port, void *ip_ptr, void *tcp_ptr) 1655 { 1656 struct tcphdr *th = (struct tcphdr *)tcp_ptr; 1657 1658 INP_WLOCK_ASSERT(inp); 1659 1660 #ifdef INET6 1661 if ((inp->inp_vflag & INP_IPV6) != 0) { 1662 struct ip6_hdr *ip6; 1663 1664 ip6 = (struct ip6_hdr *)ip_ptr; 1665 ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) | 1666 (inp->inp_flow & IPV6_FLOWINFO_MASK); 1667 ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) | 1668 (IPV6_VERSION & IPV6_VERSION_MASK); 1669 if (port == 0) 1670 ip6->ip6_nxt = IPPROTO_TCP; 1671 else 1672 ip6->ip6_nxt = IPPROTO_UDP; 1673 ip6->ip6_plen = htons(sizeof(struct tcphdr)); 1674 ip6->ip6_src = inp->in6p_laddr; 1675 ip6->ip6_dst = inp->in6p_faddr; 1676 } 1677 #endif /* INET6 */ 1678 #if defined(INET6) && defined(INET) 1679 else 1680 #endif 1681 #ifdef INET 1682 { 1683 struct ip *ip; 1684 1685 ip = (struct ip *)ip_ptr; 1686 ip->ip_v = IPVERSION; 1687 ip->ip_hl = 5; 1688 ip->ip_tos = inp->inp_ip_tos; 1689 ip->ip_len = 0; 1690 ip->ip_id = 0; 1691 ip->ip_off = 0; 1692 ip->ip_ttl = inp->inp_ip_ttl; 1693 ip->ip_sum = 0; 1694 if (port == 0) 1695 ip->ip_p = IPPROTO_TCP; 1696 else 1697 ip->ip_p = IPPROTO_UDP; 1698 ip->ip_src = inp->inp_laddr; 1699 ip->ip_dst = inp->inp_faddr; 1700 } 1701 #endif /* INET */ 1702 th->th_sport = inp->inp_lport; 1703 th->th_dport = inp->inp_fport; 1704 th->th_seq = 0; 1705 th->th_ack = 0; 1706 th->th_off = 5; 1707 tcp_set_flags(th, 0); 1708 th->th_win = 0; 1709 th->th_urp = 0; 1710 th->th_sum = 0; /* in_pseudo() is called later for ipv4 */ 1711 } 1712 1713 /* 1714 * Create template to be used to send tcp packets on a connection. 1715 * Allocates an mbuf and fills in a skeletal tcp/ip header. The only 1716 * use for this function is in keepalives, which use tcp_respond. 1717 */ 1718 struct tcptemp * 1719 tcpip_maketemplate(struct inpcb *inp) 1720 { 1721 struct tcptemp *t; 1722 1723 t = malloc(sizeof(*t), M_TEMP, M_NOWAIT); 1724 if (t == NULL) 1725 return (NULL); 1726 tcpip_fillheaders(inp, 0, (void *)&t->tt_ipgen, (void *)&t->tt_t); 1727 return (t); 1728 } 1729 1730 /* 1731 * Send a single message to the TCP at address specified by 1732 * the given TCP/IP header. If m == NULL, then we make a copy 1733 * of the tcpiphdr at th and send directly to the addressed host. 1734 * This is used to force keep alive messages out using the TCP 1735 * template for a connection. If flags are given then we send 1736 * a message back to the TCP which originated the segment th, 1737 * and discard the mbuf containing it and any other attached mbufs. 1738 * 1739 * In any case the ack and sequence number of the transmitted 1740 * segment are as specified by the parameters. 1741 * 1742 * NOTE: If m != NULL, then th must point to *inside* the mbuf. 1743 */ 1744 1745 void 1746 tcp_respond(struct tcpcb *tp, void *ipgen, struct tcphdr *th, struct mbuf *m, 1747 tcp_seq ack, tcp_seq seq, uint16_t flags) 1748 { 1749 struct tcpopt to; 1750 struct inpcb *inp; 1751 struct ip *ip; 1752 struct mbuf *optm; 1753 struct udphdr *uh = NULL; 1754 struct tcphdr *nth; 1755 struct tcp_log_buffer *lgb; 1756 u_char *optp; 1757 #ifdef INET6 1758 struct ip6_hdr *ip6; 1759 int isipv6; 1760 #endif /* INET6 */ 1761 int optlen, tlen, win, ulen; 1762 int ect = 0; 1763 bool incl_opts; 1764 uint16_t port; 1765 int output_ret; 1766 #ifdef INVARIANTS 1767 int thflags = tcp_get_flags(th); 1768 #endif 1769 1770 KASSERT(tp != NULL || m != NULL, ("tcp_respond: tp and m both NULL")); 1771 NET_EPOCH_ASSERT(); 1772 1773 #ifdef INET6 1774 isipv6 = ((struct ip *)ipgen)->ip_v == (IPV6_VERSION >> 4); 1775 ip6 = ipgen; 1776 #endif /* INET6 */ 1777 ip = ipgen; 1778 1779 if (tp != NULL) { 1780 inp = tptoinpcb(tp); 1781 INP_LOCK_ASSERT(inp); 1782 } else 1783 inp = NULL; 1784 1785 if (m != NULL) { 1786 #ifdef INET6 1787 if (isipv6 && ip6 && (ip6->ip6_nxt == IPPROTO_UDP)) 1788 port = m->m_pkthdr.tcp_tun_port; 1789 else 1790 #endif 1791 if (ip && (ip->ip_p == IPPROTO_UDP)) 1792 port = m->m_pkthdr.tcp_tun_port; 1793 else 1794 port = 0; 1795 } else 1796 port = tp->t_port; 1797 1798 incl_opts = false; 1799 win = 0; 1800 if (tp != NULL) { 1801 if (!(flags & TH_RST)) { 1802 win = sbspace(&inp->inp_socket->so_rcv); 1803 if (win > TCP_MAXWIN << tp->rcv_scale) 1804 win = TCP_MAXWIN << tp->rcv_scale; 1805 } 1806 if ((tp->t_flags & TF_NOOPT) == 0) 1807 incl_opts = true; 1808 } 1809 if (m == NULL) { 1810 m = m_gethdr(M_NOWAIT, MT_DATA); 1811 if (m == NULL) 1812 return; 1813 m->m_data += max_linkhdr; 1814 #ifdef INET6 1815 if (isipv6) { 1816 bcopy((caddr_t)ip6, mtod(m, caddr_t), 1817 sizeof(struct ip6_hdr)); 1818 ip6 = mtod(m, struct ip6_hdr *); 1819 nth = (struct tcphdr *)(ip6 + 1); 1820 if (port) { 1821 /* Insert a UDP header */ 1822 uh = (struct udphdr *)nth; 1823 uh->uh_sport = htons(V_tcp_udp_tunneling_port); 1824 uh->uh_dport = port; 1825 nth = (struct tcphdr *)(uh + 1); 1826 } 1827 } else 1828 #endif /* INET6 */ 1829 { 1830 bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip)); 1831 ip = mtod(m, struct ip *); 1832 nth = (struct tcphdr *)(ip + 1); 1833 if (port) { 1834 /* Insert a UDP header */ 1835 uh = (struct udphdr *)nth; 1836 uh->uh_sport = htons(V_tcp_udp_tunneling_port); 1837 uh->uh_dport = port; 1838 nth = (struct tcphdr *)(uh + 1); 1839 } 1840 } 1841 bcopy((caddr_t)th, (caddr_t)nth, sizeof(struct tcphdr)); 1842 flags = TH_ACK; 1843 } else if ((!M_WRITABLE(m)) || (port != 0)) { 1844 struct mbuf *n; 1845 1846 /* Can't reuse 'm', allocate a new mbuf. */ 1847 n = m_gethdr(M_NOWAIT, MT_DATA); 1848 if (n == NULL) { 1849 m_freem(m); 1850 return; 1851 } 1852 1853 if (!m_dup_pkthdr(n, m, M_NOWAIT)) { 1854 m_freem(m); 1855 m_freem(n); 1856 return; 1857 } 1858 1859 n->m_data += max_linkhdr; 1860 /* m_len is set later */ 1861 #define xchg(a,b,type) { type t; t=a; a=b; b=t; } 1862 #ifdef INET6 1863 if (isipv6) { 1864 bcopy((caddr_t)ip6, mtod(n, caddr_t), 1865 sizeof(struct ip6_hdr)); 1866 ip6 = mtod(n, struct ip6_hdr *); 1867 xchg(ip6->ip6_dst, ip6->ip6_src, struct in6_addr); 1868 nth = (struct tcphdr *)(ip6 + 1); 1869 if (port) { 1870 /* Insert a UDP header */ 1871 uh = (struct udphdr *)nth; 1872 uh->uh_sport = htons(V_tcp_udp_tunneling_port); 1873 uh->uh_dport = port; 1874 nth = (struct tcphdr *)(uh + 1); 1875 } 1876 } else 1877 #endif /* INET6 */ 1878 { 1879 bcopy((caddr_t)ip, mtod(n, caddr_t), sizeof(struct ip)); 1880 ip = mtod(n, struct ip *); 1881 xchg(ip->ip_dst.s_addr, ip->ip_src.s_addr, uint32_t); 1882 nth = (struct tcphdr *)(ip + 1); 1883 if (port) { 1884 /* Insert a UDP header */ 1885 uh = (struct udphdr *)nth; 1886 uh->uh_sport = htons(V_tcp_udp_tunneling_port); 1887 uh->uh_dport = port; 1888 nth = (struct tcphdr *)(uh + 1); 1889 } 1890 } 1891 bcopy((caddr_t)th, (caddr_t)nth, sizeof(struct tcphdr)); 1892 xchg(nth->th_dport, nth->th_sport, uint16_t); 1893 th = nth; 1894 m_freem(m); 1895 m = n; 1896 } else { 1897 /* 1898 * reuse the mbuf. 1899 * XXX MRT We inherit the FIB, which is lucky. 1900 */ 1901 m_freem(m->m_next); 1902 m->m_next = NULL; 1903 m->m_data = (caddr_t)ipgen; 1904 /* clear any receive flags for proper bpf timestamping */ 1905 m->m_flags &= ~(M_TSTMP | M_TSTMP_LRO); 1906 /* m_len is set later */ 1907 #ifdef INET6 1908 if (isipv6) { 1909 xchg(ip6->ip6_dst, ip6->ip6_src, struct in6_addr); 1910 nth = (struct tcphdr *)(ip6 + 1); 1911 } else 1912 #endif /* INET6 */ 1913 { 1914 xchg(ip->ip_dst.s_addr, ip->ip_src.s_addr, uint32_t); 1915 nth = (struct tcphdr *)(ip + 1); 1916 } 1917 if (th != nth) { 1918 /* 1919 * this is usually a case when an extension header 1920 * exists between the IPv6 header and the 1921 * TCP header. 1922 */ 1923 nth->th_sport = th->th_sport; 1924 nth->th_dport = th->th_dport; 1925 } 1926 xchg(nth->th_dport, nth->th_sport, uint16_t); 1927 #undef xchg 1928 } 1929 tlen = 0; 1930 #ifdef INET6 1931 if (isipv6) 1932 tlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr); 1933 #endif 1934 #if defined(INET) && defined(INET6) 1935 else 1936 #endif 1937 #ifdef INET 1938 tlen = sizeof (struct tcpiphdr); 1939 #endif 1940 if (port) 1941 tlen += sizeof (struct udphdr); 1942 #ifdef INVARIANTS 1943 m->m_len = 0; 1944 KASSERT(M_TRAILINGSPACE(m) >= tlen, 1945 ("Not enough trailing space for message (m=%p, need=%d, have=%ld)", 1946 m, tlen, (long)M_TRAILINGSPACE(m))); 1947 #endif 1948 m->m_len = tlen; 1949 to.to_flags = 0; 1950 if (incl_opts) { 1951 ect = tcp_ecn_output_established(tp, &flags, 0, false); 1952 /* Make sure we have room. */ 1953 if (M_TRAILINGSPACE(m) < TCP_MAXOLEN) { 1954 m->m_next = m_get(M_NOWAIT, MT_DATA); 1955 if (m->m_next) { 1956 optp = mtod(m->m_next, u_char *); 1957 optm = m->m_next; 1958 } else 1959 incl_opts = false; 1960 } else { 1961 optp = (u_char *) (nth + 1); 1962 optm = m; 1963 } 1964 } 1965 if (incl_opts) { 1966 /* Timestamps. */ 1967 if (tp->t_flags & TF_RCVD_TSTMP) { 1968 to.to_tsval = tcp_ts_getticks() + tp->ts_offset; 1969 to.to_tsecr = tp->ts_recent; 1970 to.to_flags |= TOF_TS; 1971 } 1972 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 1973 /* TCP-MD5 (RFC2385). */ 1974 if (tp->t_flags & TF_SIGNATURE) 1975 to.to_flags |= TOF_SIGNATURE; 1976 #endif 1977 /* Add the options. */ 1978 tlen += optlen = tcp_addoptions(&to, optp); 1979 1980 /* Update m_len in the correct mbuf. */ 1981 optm->m_len += optlen; 1982 } else 1983 optlen = 0; 1984 #ifdef INET6 1985 if (isipv6) { 1986 if (uh) { 1987 ulen = tlen - sizeof(struct ip6_hdr); 1988 uh->uh_ulen = htons(ulen); 1989 } 1990 ip6->ip6_flow = htonl(ect << IPV6_FLOWLABEL_LEN); 1991 ip6->ip6_vfc = IPV6_VERSION; 1992 if (port) 1993 ip6->ip6_nxt = IPPROTO_UDP; 1994 else 1995 ip6->ip6_nxt = IPPROTO_TCP; 1996 ip6->ip6_plen = htons(tlen - sizeof(*ip6)); 1997 } 1998 #endif 1999 #if defined(INET) && defined(INET6) 2000 else 2001 #endif 2002 #ifdef INET 2003 { 2004 if (uh) { 2005 ulen = tlen - sizeof(struct ip); 2006 uh->uh_ulen = htons(ulen); 2007 } 2008 ip->ip_len = htons(tlen); 2009 if (inp != NULL) { 2010 ip->ip_tos = inp->inp_ip_tos & ~IPTOS_ECN_MASK; 2011 ip->ip_ttl = inp->inp_ip_ttl; 2012 } else { 2013 ip->ip_tos = 0; 2014 ip->ip_ttl = V_ip_defttl; 2015 } 2016 ip->ip_tos |= ect; 2017 if (port) { 2018 ip->ip_p = IPPROTO_UDP; 2019 } else { 2020 ip->ip_p = IPPROTO_TCP; 2021 } 2022 if (V_path_mtu_discovery) 2023 ip->ip_off |= htons(IP_DF); 2024 } 2025 #endif 2026 m->m_pkthdr.len = tlen; 2027 m->m_pkthdr.rcvif = NULL; 2028 #ifdef MAC 2029 if (inp != NULL) { 2030 /* 2031 * Packet is associated with a socket, so allow the 2032 * label of the response to reflect the socket label. 2033 */ 2034 INP_LOCK_ASSERT(inp); 2035 mac_inpcb_create_mbuf(inp, m); 2036 } else { 2037 /* 2038 * Packet is not associated with a socket, so possibly 2039 * update the label in place. 2040 */ 2041 mac_netinet_tcp_reply(m); 2042 } 2043 #endif 2044 nth->th_seq = htonl(seq); 2045 nth->th_ack = htonl(ack); 2046 nth->th_off = (sizeof (struct tcphdr) + optlen) >> 2; 2047 tcp_set_flags(nth, flags); 2048 if (tp && (flags & TH_RST)) { 2049 /* Log the reset */ 2050 tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST); 2051 } 2052 if (tp != NULL) 2053 nth->th_win = htons((u_short) (win >> tp->rcv_scale)); 2054 else 2055 nth->th_win = htons((u_short)win); 2056 nth->th_urp = 0; 2057 2058 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 2059 if (to.to_flags & TOF_SIGNATURE) { 2060 if (!TCPMD5_ENABLED() || 2061 TCPMD5_OUTPUT(m, nth, to.to_signature) != 0) { 2062 m_freem(m); 2063 return; 2064 } 2065 } 2066 #endif 2067 2068 #ifdef INET6 2069 if (isipv6) { 2070 if (port) { 2071 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6; 2072 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 2073 uh->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0); 2074 nth->th_sum = 0; 2075 } else { 2076 m->m_pkthdr.csum_flags = CSUM_TCP_IPV6; 2077 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 2078 nth->th_sum = in6_cksum_pseudo(ip6, 2079 tlen - sizeof(struct ip6_hdr), IPPROTO_TCP, 0); 2080 } 2081 ip6->ip6_hlim = in6_selecthlim(inp, NULL); 2082 } 2083 #endif /* INET6 */ 2084 #if defined(INET6) && defined(INET) 2085 else 2086 #endif 2087 #ifdef INET 2088 { 2089 if (port) { 2090 uh->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 2091 htons(ulen + IPPROTO_UDP)); 2092 m->m_pkthdr.csum_flags = CSUM_UDP; 2093 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 2094 nth->th_sum = 0; 2095 } else { 2096 m->m_pkthdr.csum_flags = CSUM_TCP; 2097 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 2098 nth->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 2099 htons((u_short)(tlen - sizeof(struct ip) + ip->ip_p))); 2100 } 2101 } 2102 #endif /* INET */ 2103 TCP_PROBE3(debug__output, tp, th, m); 2104 if (flags & TH_RST) 2105 TCP_PROBE5(accept__refused, NULL, NULL, m, tp, nth); 2106 lgb = NULL; 2107 if ((tp != NULL) && tcp_bblogging_on(tp)) { 2108 if (INP_WLOCKED(inp)) { 2109 union tcp_log_stackspecific log; 2110 struct timeval tv; 2111 2112 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 2113 log.u_bbr.inhpts = tcp_in_hpts(tp); 2114 log.u_bbr.flex8 = 4; 2115 log.u_bbr.pkts_out = tp->t_maxseg; 2116 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 2117 log.u_bbr.delivered = 0; 2118 lgb = tcp_log_event(tp, nth, NULL, NULL, TCP_LOG_OUT, 2119 ERRNO_UNK, 0, &log, false, NULL, NULL, 0, &tv); 2120 } else { 2121 /* 2122 * We can not log the packet, since we only own the 2123 * read lock, but a write lock is needed. The read lock 2124 * is not upgraded to a write lock, since only getting 2125 * the read lock was done intentionally to improve the 2126 * handling of SYN flooding attacks. 2127 * This happens only for pure SYN segments received in 2128 * the initial CLOSED state, or received in a more 2129 * advanced state than listen and the UDP encapsulation 2130 * port is unexpected. 2131 * The incoming SYN segments do not really belong to 2132 * the TCP connection and the handling does not change 2133 * the state of the TCP connection. Therefore, the 2134 * sending of the RST segments is not logged. Please 2135 * note that also the incoming SYN segments are not 2136 * logged. 2137 * 2138 * The following code ensures that the above description 2139 * is and stays correct. 2140 */ 2141 KASSERT((thflags & (TH_ACK|TH_SYN)) == TH_SYN && 2142 (tp->t_state == TCPS_CLOSED || 2143 (tp->t_state > TCPS_LISTEN && tp->t_port != port)), 2144 ("%s: Logging of TCP segment with flags 0x%b and " 2145 "UDP encapsulation port %u skipped in state %s", 2146 __func__, thflags, PRINT_TH_FLAGS, 2147 ntohs(port), tcpstates[tp->t_state])); 2148 } 2149 } 2150 2151 if (flags & TH_ACK) 2152 TCPSTAT_INC(tcps_sndacks); 2153 else if (flags & (TH_SYN|TH_FIN|TH_RST)) 2154 TCPSTAT_INC(tcps_sndctrl); 2155 TCPSTAT_INC(tcps_sndtotal); 2156 2157 #ifdef INET6 2158 if (isipv6) { 2159 TCP_PROBE5(send, NULL, tp, ip6, tp, nth); 2160 output_ret = ip6_output(m, inp ? inp->in6p_outputopts : NULL, 2161 NULL, 0, NULL, NULL, inp); 2162 } 2163 #endif /* INET6 */ 2164 #if defined(INET) && defined(INET6) 2165 else 2166 #endif 2167 #ifdef INET 2168 { 2169 TCP_PROBE5(send, NULL, tp, ip, tp, nth); 2170 output_ret = ip_output(m, NULL, NULL, 0, NULL, inp); 2171 } 2172 #endif 2173 if (lgb != NULL) 2174 lgb->tlb_errno = output_ret; 2175 } 2176 2177 /* 2178 * Create a new TCP control block, making an empty reassembly queue and hooking 2179 * it to the argument protocol control block. The `inp' parameter must have 2180 * come from the zone allocator set up by tcpcbstor declaration. 2181 */ 2182 struct tcpcb * 2183 tcp_newtcpcb(struct inpcb *inp) 2184 { 2185 struct tcpcb *tp = intotcpcb(inp); 2186 #ifdef INET6 2187 int isipv6 = (inp->inp_vflag & INP_IPV6) != 0; 2188 #endif /* INET6 */ 2189 2190 /* 2191 * Historically allocation was done with M_ZERO. There is a lot of 2192 * code that rely on that. For now take safe approach and zero whole 2193 * tcpcb. This definitely can be optimized. 2194 */ 2195 bzero(&tp->t_start_zero, t_zero_size); 2196 2197 /* Initialise cc_var struct for this tcpcb. */ 2198 tp->t_ccv.type = IPPROTO_TCP; 2199 tp->t_ccv.ccvc.tcp = tp; 2200 rw_rlock(&tcp_function_lock); 2201 tp->t_fb = V_tcp_func_set_ptr; 2202 refcount_acquire(&tp->t_fb->tfb_refcnt); 2203 rw_runlock(&tcp_function_lock); 2204 /* 2205 * Use the current system default CC algorithm. 2206 */ 2207 cc_attach(tp, CC_DEFAULT_ALGO()); 2208 2209 if (CC_ALGO(tp)->cb_init != NULL) 2210 if (CC_ALGO(tp)->cb_init(&tp->t_ccv, NULL) > 0) { 2211 cc_detach(tp); 2212 if (tp->t_fb->tfb_tcp_fb_fini) 2213 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 1); 2214 refcount_release(&tp->t_fb->tfb_refcnt); 2215 return (NULL); 2216 } 2217 2218 #ifdef TCP_HHOOK 2219 if (khelp_init_osd(HELPER_CLASS_TCP, &tp->t_osd)) { 2220 if (tp->t_fb->tfb_tcp_fb_fini) 2221 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 1); 2222 refcount_release(&tp->t_fb->tfb_refcnt); 2223 return (NULL); 2224 } 2225 #endif 2226 2227 TAILQ_INIT(&tp->t_segq); 2228 STAILQ_INIT(&tp->t_inqueue); 2229 tp->t_maxseg = 2230 #ifdef INET6 2231 isipv6 ? V_tcp_v6mssdflt : 2232 #endif /* INET6 */ 2233 V_tcp_mssdflt; 2234 2235 /* All mbuf queue/ack compress flags should be off */ 2236 tcp_lro_features_off(tp); 2237 2238 tp->t_hpts_cpu = HPTS_CPU_NONE; 2239 tp->t_lro_cpu = HPTS_CPU_NONE; 2240 2241 callout_init_rw(&tp->t_callout, &inp->inp_lock, CALLOUT_RETURNUNLOCKED); 2242 for (int i = 0; i < TT_N; i++) 2243 tp->t_timers[i] = SBT_MAX; 2244 2245 switch (V_tcp_do_rfc1323) { 2246 case 0: 2247 break; 2248 default: 2249 case 1: 2250 tp->t_flags = (TF_REQ_SCALE|TF_REQ_TSTMP); 2251 break; 2252 case 2: 2253 tp->t_flags = TF_REQ_SCALE; 2254 break; 2255 case 3: 2256 tp->t_flags = TF_REQ_TSTMP; 2257 break; 2258 } 2259 if (V_tcp_do_sack) 2260 tp->t_flags |= TF_SACK_PERMIT; 2261 TAILQ_INIT(&tp->snd_holes); 2262 2263 /* 2264 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no 2265 * rtt estimate. Set rttvar so that srtt + 4 * rttvar gives 2266 * reasonable initial retransmit time. 2267 */ 2268 tp->t_srtt = TCPTV_SRTTBASE; 2269 tp->t_rttvar = ((tcp_rexmit_initial - TCPTV_SRTTBASE) << TCP_RTTVAR_SHIFT) / 4; 2270 tp->t_rttmin = tcp_rexmit_min; 2271 tp->t_rxtcur = tcp_rexmit_initial; 2272 tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT; 2273 tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT; 2274 tp->t_rcvtime = ticks; 2275 /* We always start with ticks granularity */ 2276 tp->t_tmr_granularity = TCP_TMR_GRANULARITY_TICKS; 2277 /* 2278 * IPv4 TTL initialization is necessary for an IPv6 socket as well, 2279 * because the socket may be bound to an IPv6 wildcard address, 2280 * which may match an IPv4-mapped IPv6 address. 2281 */ 2282 inp->inp_ip_ttl = V_ip_defttl; 2283 #ifdef TCPPCAP 2284 /* 2285 * Init the TCP PCAP queues. 2286 */ 2287 tcp_pcap_tcpcb_init(tp); 2288 #endif 2289 #ifdef TCP_BLACKBOX 2290 /* Initialize the per-TCPCB log data. */ 2291 tcp_log_tcpcbinit(tp); 2292 #endif 2293 tp->t_pacing_rate = -1; 2294 if (tp->t_fb->tfb_tcp_fb_init) { 2295 if ((*tp->t_fb->tfb_tcp_fb_init)(tp, &tp->t_fb_ptr)) { 2296 refcount_release(&tp->t_fb->tfb_refcnt); 2297 return (NULL); 2298 } 2299 } 2300 #ifdef STATS 2301 if (V_tcp_perconn_stats_enable == 1) 2302 tp->t_stats = stats_blob_alloc(V_tcp_perconn_stats_dflt_tpl, 0); 2303 #endif 2304 if (V_tcp_do_lrd) 2305 tp->t_flags |= TF_LRD; 2306 2307 return (tp); 2308 } 2309 2310 /* 2311 * Drop a TCP connection, reporting 2312 * the specified error. If connection is synchronized, 2313 * then send a RST to peer. 2314 */ 2315 struct tcpcb * 2316 tcp_drop(struct tcpcb *tp, int errno) 2317 { 2318 struct socket *so = tptosocket(tp); 2319 2320 NET_EPOCH_ASSERT(); 2321 INP_WLOCK_ASSERT(tptoinpcb(tp)); 2322 2323 if (TCPS_HAVERCVDSYN(tp->t_state)) { 2324 tcp_state_change(tp, TCPS_CLOSED); 2325 /* Don't use tcp_output() here due to possible recursion. */ 2326 (void)tcp_output_nodrop(tp); 2327 TCPSTAT_INC(tcps_drops); 2328 } else 2329 TCPSTAT_INC(tcps_conndrops); 2330 if (errno == ETIMEDOUT && tp->t_softerror) 2331 errno = tp->t_softerror; 2332 so->so_error = errno; 2333 return (tcp_close(tp)); 2334 } 2335 2336 void 2337 tcp_discardcb(struct tcpcb *tp) 2338 { 2339 struct inpcb *inp = tptoinpcb(tp); 2340 struct socket *so = tptosocket(tp); 2341 struct mbuf *m; 2342 #ifdef INET6 2343 bool isipv6 = (inp->inp_vflag & INP_IPV6) != 0; 2344 #endif 2345 2346 INP_WLOCK_ASSERT(inp); 2347 MPASS(!callout_active(&tp->t_callout)); 2348 MPASS(TAILQ_EMPTY(&tp->snd_holes)); 2349 2350 /* free the reassembly queue, if any */ 2351 tcp_reass_flush(tp); 2352 2353 #ifdef TCP_OFFLOAD 2354 /* Disconnect offload device, if any. */ 2355 if (tp->t_flags & TF_TOE) 2356 tcp_offload_detach(tp); 2357 #endif 2358 #ifdef TCPPCAP 2359 /* Free the TCP PCAP queues. */ 2360 tcp_pcap_drain(&(tp->t_inpkts)); 2361 tcp_pcap_drain(&(tp->t_outpkts)); 2362 #endif 2363 2364 /* Allow the CC algorithm to clean up after itself. */ 2365 if (CC_ALGO(tp)->cb_destroy != NULL) 2366 CC_ALGO(tp)->cb_destroy(&tp->t_ccv); 2367 CC_DATA(tp) = NULL; 2368 /* Detach from the CC algorithm */ 2369 cc_detach(tp); 2370 2371 #ifdef TCP_HHOOK 2372 khelp_destroy_osd(&tp->t_osd); 2373 #endif 2374 #ifdef STATS 2375 stats_blob_destroy(tp->t_stats); 2376 #endif 2377 2378 CC_ALGO(tp) = NULL; 2379 if ((m = STAILQ_FIRST(&tp->t_inqueue)) != NULL) { 2380 struct mbuf *prev; 2381 2382 STAILQ_INIT(&tp->t_inqueue); 2383 STAILQ_FOREACH_FROM_SAFE(m, &tp->t_inqueue, m_stailqpkt, prev) 2384 m_freem(m); 2385 } 2386 TCPSTATES_DEC(tp->t_state); 2387 2388 if (tp->t_fb->tfb_tcp_fb_fini) 2389 (*tp->t_fb->tfb_tcp_fb_fini)(tp, 1); 2390 MPASS(!tcp_in_hpts(tp)); 2391 #ifdef TCP_BLACKBOX 2392 tcp_log_tcpcbfini(tp); 2393 #endif 2394 2395 /* 2396 * If we got enough samples through the srtt filter, 2397 * save the rtt and rttvar in the routing entry. 2398 * 'Enough' is arbitrarily defined as 4 rtt samples. 2399 * 4 samples is enough for the srtt filter to converge 2400 * to within enough % of the correct value; fewer samples 2401 * and we could save a bogus rtt. The danger is not high 2402 * as tcp quickly recovers from everything. 2403 * XXX: Works very well but needs some more statistics! 2404 * 2405 * XXXRRS: Updating must be after the stack fini() since 2406 * that may be converting some internal representation of 2407 * say srtt etc into the general one used by other stacks. 2408 * Lets also at least protect against the so being NULL 2409 * as RW stated below. 2410 */ 2411 if ((tp->t_rttupdated >= 4) && (so != NULL)) { 2412 struct hc_metrics_lite metrics; 2413 uint32_t ssthresh; 2414 2415 bzero(&metrics, sizeof(metrics)); 2416 /* 2417 * Update the ssthresh always when the conditions below 2418 * are satisfied. This gives us better new start value 2419 * for the congestion avoidance for new connections. 2420 * ssthresh is only set if packet loss occurred on a session. 2421 * 2422 * XXXRW: 'so' may be NULL here, and/or socket buffer may be 2423 * being torn down. Ideally this code would not use 'so'. 2424 */ 2425 ssthresh = tp->snd_ssthresh; 2426 if (ssthresh != 0 && ssthresh < so->so_snd.sb_hiwat / 2) { 2427 /* 2428 * convert the limit from user data bytes to 2429 * packets then to packet data bytes. 2430 */ 2431 ssthresh = (ssthresh + tp->t_maxseg / 2) / tp->t_maxseg; 2432 if (ssthresh < 2) 2433 ssthresh = 2; 2434 ssthresh *= (tp->t_maxseg + 2435 #ifdef INET6 2436 (isipv6 ? sizeof (struct ip6_hdr) + 2437 sizeof (struct tcphdr) : 2438 #endif 2439 sizeof (struct tcpiphdr) 2440 #ifdef INET6 2441 ) 2442 #endif 2443 ); 2444 } else 2445 ssthresh = 0; 2446 metrics.rmx_ssthresh = ssthresh; 2447 2448 metrics.rmx_rtt = tp->t_srtt; 2449 metrics.rmx_rttvar = tp->t_rttvar; 2450 metrics.rmx_cwnd = tp->snd_cwnd; 2451 metrics.rmx_sendpipe = 0; 2452 metrics.rmx_recvpipe = 0; 2453 2454 tcp_hc_update(&inp->inp_inc, &metrics); 2455 } 2456 2457 refcount_release(&tp->t_fb->tfb_refcnt); 2458 } 2459 2460 /* 2461 * Attempt to close a TCP control block, marking it as dropped, and freeing 2462 * the socket if we hold the only reference. 2463 */ 2464 struct tcpcb * 2465 tcp_close(struct tcpcb *tp) 2466 { 2467 struct inpcb *inp = tptoinpcb(tp); 2468 struct socket *so = tptosocket(tp); 2469 2470 INP_WLOCK_ASSERT(inp); 2471 2472 #ifdef TCP_OFFLOAD 2473 if (tp->t_state == TCPS_LISTEN) 2474 tcp_offload_listen_stop(tp); 2475 #endif 2476 /* 2477 * This releases the TFO pending counter resource for TFO listen 2478 * sockets as well as passively-created TFO sockets that transition 2479 * from SYN_RECEIVED to CLOSED. 2480 */ 2481 if (tp->t_tfo_pending) { 2482 tcp_fastopen_decrement_counter(tp->t_tfo_pending); 2483 tp->t_tfo_pending = NULL; 2484 } 2485 tcp_timer_stop(tp); 2486 if (tp->t_fb->tfb_tcp_timer_stop_all != NULL) 2487 tp->t_fb->tfb_tcp_timer_stop_all(tp); 2488 in_pcbdrop(inp); 2489 TCPSTAT_INC(tcps_closed); 2490 if (tp->t_state != TCPS_CLOSED) 2491 tcp_state_change(tp, TCPS_CLOSED); 2492 KASSERT(inp->inp_socket != NULL, ("tcp_close: inp_socket NULL")); 2493 tcp_free_sackholes(tp); 2494 soisdisconnected(so); 2495 if (inp->inp_flags & INP_SOCKREF) { 2496 inp->inp_flags &= ~INP_SOCKREF; 2497 INP_WUNLOCK(inp); 2498 sorele(so); 2499 return (NULL); 2500 } 2501 return (tp); 2502 } 2503 2504 /* 2505 * Notify a tcp user of an asynchronous error; 2506 * store error as soft error, but wake up user 2507 * (for now, won't do anything until can select for soft error). 2508 * 2509 * Do not wake up user since there currently is no mechanism for 2510 * reporting soft errors (yet - a kqueue filter may be added). 2511 */ 2512 static struct inpcb * 2513 tcp_notify(struct inpcb *inp, int error) 2514 { 2515 struct tcpcb *tp; 2516 2517 INP_WLOCK_ASSERT(inp); 2518 2519 tp = intotcpcb(inp); 2520 KASSERT(tp != NULL, ("tcp_notify: tp == NULL")); 2521 2522 /* 2523 * Ignore some errors if we are hooked up. 2524 * If connection hasn't completed, has retransmitted several times, 2525 * and receives a second error, give up now. This is better 2526 * than waiting a long time to establish a connection that 2527 * can never complete. 2528 */ 2529 if (tp->t_state == TCPS_ESTABLISHED && 2530 (error == EHOSTUNREACH || error == ENETUNREACH || 2531 error == EHOSTDOWN)) { 2532 if (inp->inp_route.ro_nh) { 2533 NH_FREE(inp->inp_route.ro_nh); 2534 inp->inp_route.ro_nh = (struct nhop_object *)NULL; 2535 } 2536 return (inp); 2537 } else if (tp->t_state < TCPS_ESTABLISHED && tp->t_rxtshift > 3 && 2538 tp->t_softerror) { 2539 tp = tcp_drop(tp, error); 2540 if (tp != NULL) 2541 return (inp); 2542 else 2543 return (NULL); 2544 } else { 2545 tp->t_softerror = error; 2546 return (inp); 2547 } 2548 #if 0 2549 wakeup( &so->so_timeo); 2550 sorwakeup(so); 2551 sowwakeup(so); 2552 #endif 2553 } 2554 2555 static int 2556 tcp_pcblist(SYSCTL_HANDLER_ARGS) 2557 { 2558 struct inpcb_iterator inpi = INP_ALL_ITERATOR(&V_tcbinfo, 2559 INPLOOKUP_RLOCKPCB); 2560 struct xinpgen xig; 2561 struct inpcb *inp; 2562 int error; 2563 2564 if (req->newptr != NULL) 2565 return (EPERM); 2566 2567 if (req->oldptr == NULL) { 2568 int n; 2569 2570 n = V_tcbinfo.ipi_count + 2571 counter_u64_fetch(V_tcps_states[TCPS_SYN_RECEIVED]); 2572 n += imax(n / 8, 10); 2573 req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xtcpcb); 2574 return (0); 2575 } 2576 2577 if ((error = sysctl_wire_old_buffer(req, 0)) != 0) 2578 return (error); 2579 2580 bzero(&xig, sizeof(xig)); 2581 xig.xig_len = sizeof xig; 2582 xig.xig_count = V_tcbinfo.ipi_count + 2583 counter_u64_fetch(V_tcps_states[TCPS_SYN_RECEIVED]); 2584 xig.xig_gen = V_tcbinfo.ipi_gencnt; 2585 xig.xig_sogen = so_gencnt; 2586 error = SYSCTL_OUT(req, &xig, sizeof xig); 2587 if (error) 2588 return (error); 2589 2590 error = syncache_pcblist(req); 2591 if (error) 2592 return (error); 2593 2594 while ((inp = inp_next(&inpi)) != NULL) { 2595 if (inp->inp_gencnt <= xig.xig_gen && 2596 cr_canseeinpcb(req->td->td_ucred, inp) == 0) { 2597 struct xtcpcb xt; 2598 2599 tcp_inptoxtp(inp, &xt); 2600 error = SYSCTL_OUT(req, &xt, sizeof xt); 2601 if (error) { 2602 INP_RUNLOCK(inp); 2603 break; 2604 } else 2605 continue; 2606 } 2607 } 2608 2609 if (!error) { 2610 /* 2611 * Give the user an updated idea of our state. 2612 * If the generation differs from what we told 2613 * her before, she knows that something happened 2614 * while we were processing this request, and it 2615 * might be necessary to retry. 2616 */ 2617 xig.xig_gen = V_tcbinfo.ipi_gencnt; 2618 xig.xig_sogen = so_gencnt; 2619 xig.xig_count = V_tcbinfo.ipi_count + 2620 counter_u64_fetch(V_tcps_states[TCPS_SYN_RECEIVED]); 2621 error = SYSCTL_OUT(req, &xig, sizeof xig); 2622 } 2623 2624 return (error); 2625 } 2626 2627 SYSCTL_PROC(_net_inet_tcp, TCPCTL_PCBLIST, pcblist, 2628 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_NEEDGIANT, 2629 NULL, 0, tcp_pcblist, "S,xtcpcb", 2630 "List of active TCP connections"); 2631 2632 #ifdef INET 2633 static int 2634 tcp_getcred(SYSCTL_HANDLER_ARGS) 2635 { 2636 struct xucred xuc; 2637 struct sockaddr_in addrs[2]; 2638 struct epoch_tracker et; 2639 struct inpcb *inp; 2640 int error; 2641 2642 error = priv_check(req->td, PRIV_NETINET_GETCRED); 2643 if (error) 2644 return (error); 2645 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 2646 if (error) 2647 return (error); 2648 NET_EPOCH_ENTER(et); 2649 inp = in_pcblookup(&V_tcbinfo, addrs[1].sin_addr, addrs[1].sin_port, 2650 addrs[0].sin_addr, addrs[0].sin_port, INPLOOKUP_RLOCKPCB, NULL); 2651 NET_EPOCH_EXIT(et); 2652 if (inp != NULL) { 2653 if (error == 0) 2654 error = cr_canseeinpcb(req->td->td_ucred, inp); 2655 if (error == 0) 2656 cru2x(inp->inp_cred, &xuc); 2657 INP_RUNLOCK(inp); 2658 } else 2659 error = ENOENT; 2660 if (error == 0) 2661 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 2662 return (error); 2663 } 2664 2665 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, getcred, 2666 CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_NEEDGIANT, 2667 0, 0, tcp_getcred, "S,xucred", 2668 "Get the xucred of a TCP connection"); 2669 #endif /* INET */ 2670 2671 #ifdef INET6 2672 static int 2673 tcp6_getcred(SYSCTL_HANDLER_ARGS) 2674 { 2675 struct epoch_tracker et; 2676 struct xucred xuc; 2677 struct sockaddr_in6 addrs[2]; 2678 struct inpcb *inp; 2679 int error; 2680 #ifdef INET 2681 int mapped = 0; 2682 #endif 2683 2684 error = priv_check(req->td, PRIV_NETINET_GETCRED); 2685 if (error) 2686 return (error); 2687 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 2688 if (error) 2689 return (error); 2690 if ((error = sa6_embedscope(&addrs[0], V_ip6_use_defzone)) != 0 || 2691 (error = sa6_embedscope(&addrs[1], V_ip6_use_defzone)) != 0) { 2692 return (error); 2693 } 2694 if (IN6_IS_ADDR_V4MAPPED(&addrs[0].sin6_addr)) { 2695 #ifdef INET 2696 if (IN6_IS_ADDR_V4MAPPED(&addrs[1].sin6_addr)) 2697 mapped = 1; 2698 else 2699 #endif 2700 return (EINVAL); 2701 } 2702 2703 NET_EPOCH_ENTER(et); 2704 #ifdef INET 2705 if (mapped == 1) 2706 inp = in_pcblookup(&V_tcbinfo, 2707 *(struct in_addr *)&addrs[1].sin6_addr.s6_addr[12], 2708 addrs[1].sin6_port, 2709 *(struct in_addr *)&addrs[0].sin6_addr.s6_addr[12], 2710 addrs[0].sin6_port, INPLOOKUP_RLOCKPCB, NULL); 2711 else 2712 #endif 2713 inp = in6_pcblookup(&V_tcbinfo, 2714 &addrs[1].sin6_addr, addrs[1].sin6_port, 2715 &addrs[0].sin6_addr, addrs[0].sin6_port, 2716 INPLOOKUP_RLOCKPCB, NULL); 2717 NET_EPOCH_EXIT(et); 2718 if (inp != NULL) { 2719 if (error == 0) 2720 error = cr_canseeinpcb(req->td->td_ucred, inp); 2721 if (error == 0) 2722 cru2x(inp->inp_cred, &xuc); 2723 INP_RUNLOCK(inp); 2724 } else 2725 error = ENOENT; 2726 if (error == 0) 2727 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 2728 return (error); 2729 } 2730 2731 SYSCTL_PROC(_net_inet6_tcp6, OID_AUTO, getcred, 2732 CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_NEEDGIANT, 2733 0, 0, tcp6_getcred, "S,xucred", 2734 "Get the xucred of a TCP6 connection"); 2735 #endif /* INET6 */ 2736 2737 #ifdef INET 2738 /* Path MTU to try next when a fragmentation-needed message is received. */ 2739 static inline int 2740 tcp_next_pmtu(const struct icmp *icp, const struct ip *ip) 2741 { 2742 int mtu = ntohs(icp->icmp_nextmtu); 2743 2744 /* If no alternative MTU was proposed, try the next smaller one. */ 2745 if (!mtu) 2746 mtu = ip_next_mtu(ntohs(ip->ip_len), 1); 2747 if (mtu < V_tcp_minmss + sizeof(struct tcpiphdr)) 2748 mtu = V_tcp_minmss + sizeof(struct tcpiphdr); 2749 2750 return (mtu); 2751 } 2752 2753 static void 2754 tcp_ctlinput_with_port(struct icmp *icp, uint16_t port) 2755 { 2756 struct ip *ip; 2757 struct tcphdr *th; 2758 struct inpcb *inp; 2759 struct tcpcb *tp; 2760 struct inpcb *(*notify)(struct inpcb *, int); 2761 struct in_conninfo inc; 2762 tcp_seq icmp_tcp_seq; 2763 int errno, mtu; 2764 2765 errno = icmp_errmap(icp); 2766 switch (errno) { 2767 case 0: 2768 return; 2769 case EMSGSIZE: 2770 notify = tcp_mtudisc_notify; 2771 break; 2772 case ECONNREFUSED: 2773 if (V_icmp_may_rst) 2774 notify = tcp_drop_syn_sent; 2775 else 2776 notify = tcp_notify; 2777 break; 2778 case EHOSTUNREACH: 2779 if (V_icmp_may_rst && icp->icmp_type == ICMP_TIMXCEED) 2780 notify = tcp_drop_syn_sent; 2781 else 2782 notify = tcp_notify; 2783 break; 2784 default: 2785 notify = tcp_notify; 2786 } 2787 2788 ip = &icp->icmp_ip; 2789 th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 2790 icmp_tcp_seq = th->th_seq; 2791 inp = in_pcblookup(&V_tcbinfo, ip->ip_dst, th->th_dport, ip->ip_src, 2792 th->th_sport, INPLOOKUP_WLOCKPCB, NULL); 2793 if (inp != NULL) { 2794 tp = intotcpcb(inp); 2795 #ifdef TCP_OFFLOAD 2796 if (tp->t_flags & TF_TOE && errno == EMSGSIZE) { 2797 /* 2798 * MTU discovery for offloaded connections. Let 2799 * the TOE driver verify seq# and process it. 2800 */ 2801 mtu = tcp_next_pmtu(icp, ip); 2802 tcp_offload_pmtu_update(tp, icmp_tcp_seq, mtu); 2803 goto out; 2804 } 2805 #endif 2806 if (tp->t_port != port) 2807 goto out; 2808 if (SEQ_GEQ(ntohl(icmp_tcp_seq), tp->snd_una) && 2809 SEQ_LT(ntohl(icmp_tcp_seq), tp->snd_max)) { 2810 if (errno == EMSGSIZE) { 2811 /* 2812 * MTU discovery: we got a needfrag and 2813 * will potentially try a lower MTU. 2814 */ 2815 mtu = tcp_next_pmtu(icp, ip); 2816 2817 /* 2818 * Only process the offered MTU if it 2819 * is smaller than the current one. 2820 */ 2821 if (mtu < tp->t_maxseg + 2822 sizeof(struct tcpiphdr)) { 2823 bzero(&inc, sizeof(inc)); 2824 inc.inc_faddr = ip->ip_dst; 2825 inc.inc_fibnum = 2826 inp->inp_inc.inc_fibnum; 2827 tcp_hc_updatemtu(&inc, mtu); 2828 inp = tcp_mtudisc(inp, mtu); 2829 } 2830 } else 2831 inp = (*notify)(inp, errno); 2832 } 2833 } else { 2834 bzero(&inc, sizeof(inc)); 2835 inc.inc_fport = th->th_dport; 2836 inc.inc_lport = th->th_sport; 2837 inc.inc_faddr = ip->ip_dst; 2838 inc.inc_laddr = ip->ip_src; 2839 syncache_unreach(&inc, icmp_tcp_seq, port); 2840 } 2841 out: 2842 if (inp != NULL) 2843 INP_WUNLOCK(inp); 2844 } 2845 2846 static void 2847 tcp_ctlinput(struct icmp *icmp) 2848 { 2849 tcp_ctlinput_with_port(icmp, htons(0)); 2850 } 2851 2852 static void 2853 tcp_ctlinput_viaudp(udp_tun_icmp_param_t param) 2854 { 2855 /* Its a tunneled TCP over UDP icmp */ 2856 struct icmp *icmp = param.icmp; 2857 struct ip *outer_ip, *inner_ip; 2858 struct udphdr *udp; 2859 struct tcphdr *th, ttemp; 2860 int i_hlen, o_len; 2861 uint16_t port; 2862 2863 outer_ip = (struct ip *)((caddr_t)icmp - sizeof(struct ip)); 2864 inner_ip = &icmp->icmp_ip; 2865 i_hlen = inner_ip->ip_hl << 2; 2866 o_len = ntohs(outer_ip->ip_len); 2867 if (o_len < 2868 (sizeof(struct ip) + 8 + i_hlen + sizeof(struct udphdr) + offsetof(struct tcphdr, th_ack))) { 2869 /* Not enough data present */ 2870 return; 2871 } 2872 /* Ok lets strip out the inner udphdr header by copying up on top of it the tcp hdr */ 2873 udp = (struct udphdr *)(((caddr_t)inner_ip) + i_hlen); 2874 if (ntohs(udp->uh_sport) != V_tcp_udp_tunneling_port) { 2875 return; 2876 } 2877 port = udp->uh_dport; 2878 th = (struct tcphdr *)(udp + 1); 2879 memcpy(&ttemp, th, sizeof(struct tcphdr)); 2880 memcpy(udp, &ttemp, sizeof(struct tcphdr)); 2881 /* Now adjust down the size of the outer IP header */ 2882 o_len -= sizeof(struct udphdr); 2883 outer_ip->ip_len = htons(o_len); 2884 /* Now call in to the normal handling code */ 2885 tcp_ctlinput_with_port(icmp, port); 2886 } 2887 #endif /* INET */ 2888 2889 #ifdef INET6 2890 static inline int 2891 tcp6_next_pmtu(const struct icmp6_hdr *icmp6) 2892 { 2893 int mtu = ntohl(icmp6->icmp6_mtu); 2894 2895 /* 2896 * If no alternative MTU was proposed, or the proposed MTU was too 2897 * small, set to the min. 2898 */ 2899 if (mtu < IPV6_MMTU) 2900 mtu = IPV6_MMTU - 8; /* XXXNP: what is the adjustment for? */ 2901 return (mtu); 2902 } 2903 2904 static void 2905 tcp6_ctlinput_with_port(struct ip6ctlparam *ip6cp, uint16_t port) 2906 { 2907 struct in6_addr *dst; 2908 struct inpcb *(*notify)(struct inpcb *, int); 2909 struct ip6_hdr *ip6; 2910 struct mbuf *m; 2911 struct inpcb *inp; 2912 struct tcpcb *tp; 2913 struct icmp6_hdr *icmp6; 2914 struct in_conninfo inc; 2915 struct tcp_ports { 2916 uint16_t th_sport; 2917 uint16_t th_dport; 2918 } t_ports; 2919 tcp_seq icmp_tcp_seq; 2920 unsigned int mtu; 2921 unsigned int off; 2922 int errno; 2923 2924 icmp6 = ip6cp->ip6c_icmp6; 2925 m = ip6cp->ip6c_m; 2926 ip6 = ip6cp->ip6c_ip6; 2927 off = ip6cp->ip6c_off; 2928 dst = &ip6cp->ip6c_finaldst->sin6_addr; 2929 2930 errno = icmp6_errmap(icmp6); 2931 switch (errno) { 2932 case 0: 2933 return; 2934 case EMSGSIZE: 2935 notify = tcp_mtudisc_notify; 2936 break; 2937 case ECONNREFUSED: 2938 if (V_icmp_may_rst) 2939 notify = tcp_drop_syn_sent; 2940 else 2941 notify = tcp_notify; 2942 break; 2943 case EHOSTUNREACH: 2944 /* 2945 * There are only four ICMPs that may reset connection: 2946 * - administratively prohibited 2947 * - port unreachable 2948 * - time exceeded in transit 2949 * - unknown next header 2950 */ 2951 if (V_icmp_may_rst && 2952 ((icmp6->icmp6_type == ICMP6_DST_UNREACH && 2953 (icmp6->icmp6_code == ICMP6_DST_UNREACH_ADMIN || 2954 icmp6->icmp6_code == ICMP6_DST_UNREACH_NOPORT)) || 2955 (icmp6->icmp6_type == ICMP6_TIME_EXCEEDED && 2956 icmp6->icmp6_code == ICMP6_TIME_EXCEED_TRANSIT) || 2957 (icmp6->icmp6_type == ICMP6_PARAM_PROB && 2958 icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER))) 2959 notify = tcp_drop_syn_sent; 2960 else 2961 notify = tcp_notify; 2962 break; 2963 default: 2964 notify = tcp_notify; 2965 } 2966 2967 /* Check if we can safely get the ports from the tcp hdr */ 2968 if (m == NULL || 2969 (m->m_pkthdr.len < 2970 (int32_t) (off + sizeof(struct tcp_ports)))) { 2971 return; 2972 } 2973 bzero(&t_ports, sizeof(struct tcp_ports)); 2974 m_copydata(m, off, sizeof(struct tcp_ports), (caddr_t)&t_ports); 2975 inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_dst, t_ports.th_dport, 2976 &ip6->ip6_src, t_ports.th_sport, INPLOOKUP_WLOCKPCB, NULL); 2977 off += sizeof(struct tcp_ports); 2978 if (m->m_pkthdr.len < (int32_t) (off + sizeof(tcp_seq))) { 2979 goto out; 2980 } 2981 m_copydata(m, off, sizeof(tcp_seq), (caddr_t)&icmp_tcp_seq); 2982 if (inp != NULL) { 2983 tp = intotcpcb(inp); 2984 #ifdef TCP_OFFLOAD 2985 if (tp->t_flags & TF_TOE && errno == EMSGSIZE) { 2986 /* MTU discovery for offloaded connections. */ 2987 mtu = tcp6_next_pmtu(icmp6); 2988 tcp_offload_pmtu_update(tp, icmp_tcp_seq, mtu); 2989 goto out; 2990 } 2991 #endif 2992 if (tp->t_port != port) 2993 goto out; 2994 if (SEQ_GEQ(ntohl(icmp_tcp_seq), tp->snd_una) && 2995 SEQ_LT(ntohl(icmp_tcp_seq), tp->snd_max)) { 2996 if (errno == EMSGSIZE) { 2997 /* 2998 * MTU discovery: 2999 * If we got a needfrag set the MTU 3000 * in the route to the suggested new 3001 * value (if given) and then notify. 3002 */ 3003 mtu = tcp6_next_pmtu(icmp6); 3004 3005 bzero(&inc, sizeof(inc)); 3006 inc.inc_fibnum = M_GETFIB(m); 3007 inc.inc_flags |= INC_ISIPV6; 3008 inc.inc6_faddr = *dst; 3009 if (in6_setscope(&inc.inc6_faddr, 3010 m->m_pkthdr.rcvif, NULL)) 3011 goto out; 3012 /* 3013 * Only process the offered MTU if it 3014 * is smaller than the current one. 3015 */ 3016 if (mtu < tp->t_maxseg + 3017 sizeof (struct tcphdr) + 3018 sizeof (struct ip6_hdr)) { 3019 tcp_hc_updatemtu(&inc, mtu); 3020 tcp_mtudisc(inp, mtu); 3021 ICMP6STAT_INC(icp6s_pmtuchg); 3022 } 3023 } else 3024 inp = (*notify)(inp, errno); 3025 } 3026 } else { 3027 bzero(&inc, sizeof(inc)); 3028 inc.inc_fibnum = M_GETFIB(m); 3029 inc.inc_flags |= INC_ISIPV6; 3030 inc.inc_fport = t_ports.th_dport; 3031 inc.inc_lport = t_ports.th_sport; 3032 inc.inc6_faddr = *dst; 3033 inc.inc6_laddr = ip6->ip6_src; 3034 syncache_unreach(&inc, icmp_tcp_seq, port); 3035 } 3036 out: 3037 if (inp != NULL) 3038 INP_WUNLOCK(inp); 3039 } 3040 3041 static void 3042 tcp6_ctlinput(struct ip6ctlparam *ctl) 3043 { 3044 tcp6_ctlinput_with_port(ctl, htons(0)); 3045 } 3046 3047 static void 3048 tcp6_ctlinput_viaudp(udp_tun_icmp_param_t param) 3049 { 3050 struct ip6ctlparam *ip6cp = param.ip6cp; 3051 struct mbuf *m; 3052 struct udphdr *udp; 3053 uint16_t port; 3054 3055 m = m_pulldown(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(struct udphdr), NULL); 3056 if (m == NULL) { 3057 return; 3058 } 3059 udp = mtod(m, struct udphdr *); 3060 if (ntohs(udp->uh_sport) != V_tcp_udp_tunneling_port) { 3061 return; 3062 } 3063 port = udp->uh_dport; 3064 m_adj(m, sizeof(struct udphdr)); 3065 if ((m->m_flags & M_PKTHDR) == 0) { 3066 ip6cp->ip6c_m->m_pkthdr.len -= sizeof(struct udphdr); 3067 } 3068 /* Now call in to the normal handling code */ 3069 tcp6_ctlinput_with_port(ip6cp, port); 3070 } 3071 3072 #endif /* INET6 */ 3073 3074 static uint32_t 3075 tcp_keyed_hash(struct in_conninfo *inc, u_char *key, u_int len) 3076 { 3077 SIPHASH_CTX ctx; 3078 uint32_t hash[2]; 3079 3080 KASSERT(len >= SIPHASH_KEY_LENGTH, 3081 ("%s: keylen %u too short ", __func__, len)); 3082 SipHash24_Init(&ctx); 3083 SipHash_SetKey(&ctx, (uint8_t *)key); 3084 SipHash_Update(&ctx, &inc->inc_fport, sizeof(uint16_t)); 3085 SipHash_Update(&ctx, &inc->inc_lport, sizeof(uint16_t)); 3086 switch (inc->inc_flags & INC_ISIPV6) { 3087 #ifdef INET 3088 case 0: 3089 SipHash_Update(&ctx, &inc->inc_faddr, sizeof(struct in_addr)); 3090 SipHash_Update(&ctx, &inc->inc_laddr, sizeof(struct in_addr)); 3091 break; 3092 #endif 3093 #ifdef INET6 3094 case INC_ISIPV6: 3095 SipHash_Update(&ctx, &inc->inc6_faddr, sizeof(struct in6_addr)); 3096 SipHash_Update(&ctx, &inc->inc6_laddr, sizeof(struct in6_addr)); 3097 break; 3098 #endif 3099 } 3100 SipHash_Final((uint8_t *)hash, &ctx); 3101 3102 return (hash[0] ^ hash[1]); 3103 } 3104 3105 uint32_t 3106 tcp_new_ts_offset(struct in_conninfo *inc) 3107 { 3108 struct in_conninfo inc_store, *local_inc; 3109 3110 if (!V_tcp_ts_offset_per_conn) { 3111 memcpy(&inc_store, inc, sizeof(struct in_conninfo)); 3112 inc_store.inc_lport = 0; 3113 inc_store.inc_fport = 0; 3114 local_inc = &inc_store; 3115 } else { 3116 local_inc = inc; 3117 } 3118 return (tcp_keyed_hash(local_inc, V_ts_offset_secret, 3119 sizeof(V_ts_offset_secret))); 3120 } 3121 3122 /* 3123 * Following is where TCP initial sequence number generation occurs. 3124 * 3125 * There are two places where we must use initial sequence numbers: 3126 * 1. In SYN-ACK packets. 3127 * 2. In SYN packets. 3128 * 3129 * All ISNs for SYN-ACK packets are generated by the syncache. See 3130 * tcp_syncache.c for details. 3131 * 3132 * The ISNs in SYN packets must be monotonic; TIME_WAIT recycling 3133 * depends on this property. In addition, these ISNs should be 3134 * unguessable so as to prevent connection hijacking. To satisfy 3135 * the requirements of this situation, the algorithm outlined in 3136 * RFC 1948 is used, with only small modifications. 3137 * 3138 * Implementation details: 3139 * 3140 * Time is based off the system timer, and is corrected so that it 3141 * increases by one megabyte per second. This allows for proper 3142 * recycling on high speed LANs while still leaving over an hour 3143 * before rollover. 3144 * 3145 * As reading the *exact* system time is too expensive to be done 3146 * whenever setting up a TCP connection, we increment the time 3147 * offset in two ways. First, a small random positive increment 3148 * is added to isn_offset for each connection that is set up. 3149 * Second, the function tcp_isn_tick fires once per clock tick 3150 * and increments isn_offset as necessary so that sequence numbers 3151 * are incremented at approximately ISN_BYTES_PER_SECOND. The 3152 * random positive increments serve only to ensure that the same 3153 * exact sequence number is never sent out twice (as could otherwise 3154 * happen when a port is recycled in less than the system tick 3155 * interval.) 3156 * 3157 * net.inet.tcp.isn_reseed_interval controls the number of seconds 3158 * between seeding of isn_secret. This is normally set to zero, 3159 * as reseeding should not be necessary. 3160 * 3161 * Locking of the global variables isn_secret, isn_last_reseed, isn_offset, 3162 * isn_offset_old, and isn_ctx is performed using the ISN lock. In 3163 * general, this means holding an exclusive (write) lock. 3164 */ 3165 3166 #define ISN_BYTES_PER_SECOND 1048576 3167 #define ISN_STATIC_INCREMENT 4096 3168 #define ISN_RANDOM_INCREMENT (4096 - 1) 3169 #define ISN_SECRET_LENGTH SIPHASH_KEY_LENGTH 3170 3171 VNET_DEFINE_STATIC(u_char, isn_secret[ISN_SECRET_LENGTH]); 3172 VNET_DEFINE_STATIC(int, isn_last); 3173 VNET_DEFINE_STATIC(int, isn_last_reseed); 3174 VNET_DEFINE_STATIC(u_int32_t, isn_offset); 3175 VNET_DEFINE_STATIC(u_int32_t, isn_offset_old); 3176 3177 #define V_isn_secret VNET(isn_secret) 3178 #define V_isn_last VNET(isn_last) 3179 #define V_isn_last_reseed VNET(isn_last_reseed) 3180 #define V_isn_offset VNET(isn_offset) 3181 #define V_isn_offset_old VNET(isn_offset_old) 3182 3183 tcp_seq 3184 tcp_new_isn(struct in_conninfo *inc) 3185 { 3186 tcp_seq new_isn; 3187 u_int32_t projected_offset; 3188 3189 ISN_LOCK(); 3190 /* Seed if this is the first use, reseed if requested. */ 3191 if ((V_isn_last_reseed == 0) || ((V_tcp_isn_reseed_interval > 0) && 3192 (((u_int)V_isn_last_reseed + (u_int)V_tcp_isn_reseed_interval*hz) 3193 < (u_int)ticks))) { 3194 arc4rand(&V_isn_secret, sizeof(V_isn_secret), 0); 3195 V_isn_last_reseed = ticks; 3196 } 3197 3198 /* Compute the hash and return the ISN. */ 3199 new_isn = (tcp_seq)tcp_keyed_hash(inc, V_isn_secret, 3200 sizeof(V_isn_secret)); 3201 V_isn_offset += ISN_STATIC_INCREMENT + 3202 (arc4random() & ISN_RANDOM_INCREMENT); 3203 if (ticks != V_isn_last) { 3204 projected_offset = V_isn_offset_old + 3205 ISN_BYTES_PER_SECOND / hz * (ticks - V_isn_last); 3206 if (SEQ_GT(projected_offset, V_isn_offset)) 3207 V_isn_offset = projected_offset; 3208 V_isn_offset_old = V_isn_offset; 3209 V_isn_last = ticks; 3210 } 3211 new_isn += V_isn_offset; 3212 ISN_UNLOCK(); 3213 return (new_isn); 3214 } 3215 3216 /* 3217 * When a specific ICMP unreachable message is received and the 3218 * connection state is SYN-SENT, drop the connection. This behavior 3219 * is controlled by the icmp_may_rst sysctl. 3220 */ 3221 static struct inpcb * 3222 tcp_drop_syn_sent(struct inpcb *inp, int errno) 3223 { 3224 struct tcpcb *tp; 3225 3226 NET_EPOCH_ASSERT(); 3227 INP_WLOCK_ASSERT(inp); 3228 3229 tp = intotcpcb(inp); 3230 if (tp->t_state != TCPS_SYN_SENT) 3231 return (inp); 3232 3233 if (tp->t_flags & TF_FASTOPEN) 3234 tcp_fastopen_disable_path(tp); 3235 3236 tp = tcp_drop(tp, errno); 3237 if (tp != NULL) 3238 return (inp); 3239 else 3240 return (NULL); 3241 } 3242 3243 /* 3244 * When `need fragmentation' ICMP is received, update our idea of the MSS 3245 * based on the new value. Also nudge TCP to send something, since we 3246 * know the packet we just sent was dropped. 3247 * This duplicates some code in the tcp_mss() function in tcp_input.c. 3248 */ 3249 static struct inpcb * 3250 tcp_mtudisc_notify(struct inpcb *inp, int error) 3251 { 3252 3253 return (tcp_mtudisc(inp, -1)); 3254 } 3255 3256 static struct inpcb * 3257 tcp_mtudisc(struct inpcb *inp, int mtuoffer) 3258 { 3259 struct tcpcb *tp; 3260 struct socket *so; 3261 3262 INP_WLOCK_ASSERT(inp); 3263 3264 tp = intotcpcb(inp); 3265 KASSERT(tp != NULL, ("tcp_mtudisc: tp == NULL")); 3266 3267 tcp_mss_update(tp, -1, mtuoffer, NULL, NULL); 3268 3269 so = inp->inp_socket; 3270 SOCKBUF_LOCK(&so->so_snd); 3271 /* If the mss is larger than the socket buffer, decrease the mss. */ 3272 if (so->so_snd.sb_hiwat < tp->t_maxseg) { 3273 tp->t_maxseg = so->so_snd.sb_hiwat; 3274 if (tp->t_maxseg < V_tcp_mssdflt) { 3275 /* 3276 * The MSS is so small we should not process incoming 3277 * SACK's since we are subject to attack in such a 3278 * case. 3279 */ 3280 tp->t_flags2 |= TF2_PROC_SACK_PROHIBIT; 3281 } else { 3282 tp->t_flags2 &= ~TF2_PROC_SACK_PROHIBIT; 3283 } 3284 } 3285 SOCKBUF_UNLOCK(&so->so_snd); 3286 3287 TCPSTAT_INC(tcps_mturesent); 3288 tp->t_rtttime = 0; 3289 tp->snd_nxt = tp->snd_una; 3290 tcp_free_sackholes(tp); 3291 tp->snd_recover = tp->snd_max; 3292 if (tp->t_flags & TF_SACK_PERMIT) 3293 EXIT_FASTRECOVERY(tp->t_flags); 3294 if (tp->t_fb->tfb_tcp_mtu_chg != NULL) { 3295 /* 3296 * Conceptually the snd_nxt setting 3297 * and freeing sack holes should 3298 * be done by the default stacks 3299 * own tfb_tcp_mtu_chg(). 3300 */ 3301 tp->t_fb->tfb_tcp_mtu_chg(tp); 3302 } 3303 if (tcp_output(tp) < 0) 3304 return (NULL); 3305 else 3306 return (inp); 3307 } 3308 3309 #ifdef INET 3310 /* 3311 * Look-up the routing entry to the peer of this inpcb. If no route 3312 * is found and it cannot be allocated, then return 0. This routine 3313 * is called by TCP routines that access the rmx structure and by 3314 * tcp_mss_update to get the peer/interface MTU. 3315 */ 3316 uint32_t 3317 tcp_maxmtu(struct in_conninfo *inc, struct tcp_ifcap *cap) 3318 { 3319 struct nhop_object *nh; 3320 struct ifnet *ifp; 3321 uint32_t maxmtu = 0; 3322 3323 KASSERT(inc != NULL, ("tcp_maxmtu with NULL in_conninfo pointer")); 3324 3325 if (inc->inc_faddr.s_addr != INADDR_ANY) { 3326 nh = fib4_lookup(inc->inc_fibnum, inc->inc_faddr, 0, NHR_NONE, 0); 3327 if (nh == NULL) 3328 return (0); 3329 3330 ifp = nh->nh_ifp; 3331 maxmtu = nh->nh_mtu; 3332 3333 /* Report additional interface capabilities. */ 3334 if (cap != NULL) { 3335 if (ifp->if_capenable & IFCAP_TSO4 && 3336 ifp->if_hwassist & CSUM_TSO) { 3337 cap->ifcap |= CSUM_TSO; 3338 cap->tsomax = ifp->if_hw_tsomax; 3339 cap->tsomaxsegcount = ifp->if_hw_tsomaxsegcount; 3340 cap->tsomaxsegsize = ifp->if_hw_tsomaxsegsize; 3341 } 3342 } 3343 } 3344 return (maxmtu); 3345 } 3346 #endif /* INET */ 3347 3348 #ifdef INET6 3349 uint32_t 3350 tcp_maxmtu6(struct in_conninfo *inc, struct tcp_ifcap *cap) 3351 { 3352 struct nhop_object *nh; 3353 struct in6_addr dst6; 3354 uint32_t scopeid; 3355 struct ifnet *ifp; 3356 uint32_t maxmtu = 0; 3357 3358 KASSERT(inc != NULL, ("tcp_maxmtu6 with NULL in_conninfo pointer")); 3359 3360 if (inc->inc_flags & INC_IPV6MINMTU) 3361 return (IPV6_MMTU); 3362 3363 if (!IN6_IS_ADDR_UNSPECIFIED(&inc->inc6_faddr)) { 3364 in6_splitscope(&inc->inc6_faddr, &dst6, &scopeid); 3365 nh = fib6_lookup(inc->inc_fibnum, &dst6, scopeid, NHR_NONE, 0); 3366 if (nh == NULL) 3367 return (0); 3368 3369 ifp = nh->nh_ifp; 3370 maxmtu = nh->nh_mtu; 3371 3372 /* Report additional interface capabilities. */ 3373 if (cap != NULL) { 3374 if (ifp->if_capenable & IFCAP_TSO6 && 3375 ifp->if_hwassist & CSUM_TSO) { 3376 cap->ifcap |= CSUM_TSO; 3377 cap->tsomax = ifp->if_hw_tsomax; 3378 cap->tsomaxsegcount = ifp->if_hw_tsomaxsegcount; 3379 cap->tsomaxsegsize = ifp->if_hw_tsomaxsegsize; 3380 } 3381 } 3382 } 3383 3384 return (maxmtu); 3385 } 3386 3387 /* 3388 * Handle setsockopt(IPV6_USE_MIN_MTU) by a TCP stack. 3389 * 3390 * XXXGL: we are updating inpcb here with INC_IPV6MINMTU flag. 3391 * The right place to do that is ip6_setpktopt() that has just been 3392 * executed. By the way it just filled ip6po_minmtu for us. 3393 */ 3394 void 3395 tcp6_use_min_mtu(struct tcpcb *tp) 3396 { 3397 struct inpcb *inp = tptoinpcb(tp); 3398 3399 INP_WLOCK_ASSERT(inp); 3400 /* 3401 * In case of the IPV6_USE_MIN_MTU socket 3402 * option, the INC_IPV6MINMTU flag to announce 3403 * a corresponding MSS during the initial 3404 * handshake. If the TCP connection is not in 3405 * the front states, just reduce the MSS being 3406 * used. This avoids the sending of TCP 3407 * segments which will be fragmented at the 3408 * IPv6 layer. 3409 */ 3410 inp->inp_inc.inc_flags |= INC_IPV6MINMTU; 3411 if ((tp->t_state >= TCPS_SYN_SENT) && 3412 (inp->inp_inc.inc_flags & INC_ISIPV6)) { 3413 struct ip6_pktopts *opt; 3414 3415 opt = inp->in6p_outputopts; 3416 if (opt != NULL && opt->ip6po_minmtu == IP6PO_MINMTU_ALL && 3417 tp->t_maxseg > TCP6_MSS) { 3418 tp->t_maxseg = TCP6_MSS; 3419 if (tp->t_maxseg < V_tcp_mssdflt) { 3420 /* 3421 * The MSS is so small we should not process incoming 3422 * SACK's since we are subject to attack in such a 3423 * case. 3424 */ 3425 tp->t_flags2 |= TF2_PROC_SACK_PROHIBIT; 3426 } else { 3427 tp->t_flags2 &= ~TF2_PROC_SACK_PROHIBIT; 3428 } 3429 } 3430 } 3431 } 3432 #endif /* INET6 */ 3433 3434 /* 3435 * Calculate effective SMSS per RFC5681 definition for a given TCP 3436 * connection at its current state, taking into account SACK and etc. 3437 */ 3438 u_int 3439 tcp_maxseg(const struct tcpcb *tp) 3440 { 3441 u_int optlen; 3442 3443 if (tp->t_flags & TF_NOOPT) 3444 return (tp->t_maxseg); 3445 3446 /* 3447 * Here we have a simplified code from tcp_addoptions(), 3448 * without a proper loop, and having most of paddings hardcoded. 3449 * We might make mistakes with padding here in some edge cases, 3450 * but this is harmless, since result of tcp_maxseg() is used 3451 * only in cwnd and ssthresh estimations. 3452 */ 3453 if (TCPS_HAVEESTABLISHED(tp->t_state)) { 3454 if (tp->t_flags & TF_RCVD_TSTMP) 3455 optlen = TCPOLEN_TSTAMP_APPA; 3456 else 3457 optlen = 0; 3458 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 3459 if (tp->t_flags & TF_SIGNATURE) 3460 optlen += PADTCPOLEN(TCPOLEN_SIGNATURE); 3461 #endif 3462 if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks > 0) { 3463 optlen += TCPOLEN_SACKHDR; 3464 optlen += tp->rcv_numsacks * TCPOLEN_SACK; 3465 optlen = PADTCPOLEN(optlen); 3466 } 3467 } else { 3468 if (tp->t_flags & TF_REQ_TSTMP) 3469 optlen = TCPOLEN_TSTAMP_APPA; 3470 else 3471 optlen = PADTCPOLEN(TCPOLEN_MAXSEG); 3472 if (tp->t_flags & TF_REQ_SCALE) 3473 optlen += PADTCPOLEN(TCPOLEN_WINDOW); 3474 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 3475 if (tp->t_flags & TF_SIGNATURE) 3476 optlen += PADTCPOLEN(TCPOLEN_SIGNATURE); 3477 #endif 3478 if (tp->t_flags & TF_SACK_PERMIT) 3479 optlen += PADTCPOLEN(TCPOLEN_SACK_PERMITTED); 3480 } 3481 optlen = min(optlen, TCP_MAXOLEN); 3482 return (tp->t_maxseg - optlen); 3483 } 3484 3485 3486 u_int 3487 tcp_fixed_maxseg(const struct tcpcb *tp) 3488 { 3489 int optlen; 3490 3491 if (tp->t_flags & TF_NOOPT) 3492 return (tp->t_maxseg); 3493 3494 /* 3495 * Here we have a simplified code from tcp_addoptions(), 3496 * without a proper loop, and having most of paddings hardcoded. 3497 * We only consider fixed options that we would send every 3498 * time I.e. SACK is not considered. This is important 3499 * for cc modules to figure out what the modulo of the 3500 * cwnd should be. 3501 */ 3502 if (TCPS_HAVEESTABLISHED(tp->t_state)) { 3503 if (tp->t_flags & TF_RCVD_TSTMP) 3504 optlen = TCPOLEN_TSTAMP_APPA; 3505 else 3506 optlen = 0; 3507 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 3508 if (tp->t_flags & TF_SIGNATURE) 3509 optlen += PADTCPOLEN(TCPOLEN_SIGNATURE); 3510 #endif 3511 } else { 3512 if (tp->t_flags & TF_REQ_TSTMP) 3513 optlen = TCPOLEN_TSTAMP_APPA; 3514 else 3515 optlen = PADTCPOLEN(TCPOLEN_MAXSEG); 3516 if (tp->t_flags & TF_REQ_SCALE) 3517 optlen += PADTCPOLEN(TCPOLEN_WINDOW); 3518 #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 3519 if (tp->t_flags & TF_SIGNATURE) 3520 optlen += PADTCPOLEN(TCPOLEN_SIGNATURE); 3521 #endif 3522 if (tp->t_flags & TF_SACK_PERMIT) 3523 optlen += PADTCPOLEN(TCPOLEN_SACK_PERMITTED); 3524 } 3525 optlen = min(optlen, TCP_MAXOLEN); 3526 return (tp->t_maxseg - optlen); 3527 } 3528 3529 3530 3531 static int 3532 sysctl_drop(SYSCTL_HANDLER_ARGS) 3533 { 3534 /* addrs[0] is a foreign socket, addrs[1] is a local one. */ 3535 struct sockaddr_storage addrs[2]; 3536 struct inpcb *inp; 3537 struct tcpcb *tp; 3538 #ifdef INET 3539 struct sockaddr_in *fin = NULL, *lin = NULL; 3540 #endif 3541 struct epoch_tracker et; 3542 #ifdef INET6 3543 struct sockaddr_in6 *fin6, *lin6; 3544 #endif 3545 int error; 3546 3547 inp = NULL; 3548 #ifdef INET6 3549 fin6 = lin6 = NULL; 3550 #endif 3551 error = 0; 3552 3553 if (req->oldptr != NULL || req->oldlen != 0) 3554 return (EINVAL); 3555 if (req->newptr == NULL) 3556 return (EPERM); 3557 if (req->newlen < sizeof(addrs)) 3558 return (ENOMEM); 3559 error = SYSCTL_IN(req, &addrs, sizeof(addrs)); 3560 if (error) 3561 return (error); 3562 3563 switch (addrs[0].ss_family) { 3564 #ifdef INET6 3565 case AF_INET6: 3566 fin6 = (struct sockaddr_in6 *)&addrs[0]; 3567 lin6 = (struct sockaddr_in6 *)&addrs[1]; 3568 if (fin6->sin6_len != sizeof(struct sockaddr_in6) || 3569 lin6->sin6_len != sizeof(struct sockaddr_in6)) 3570 return (EINVAL); 3571 if (IN6_IS_ADDR_V4MAPPED(&fin6->sin6_addr)) { 3572 if (!IN6_IS_ADDR_V4MAPPED(&lin6->sin6_addr)) 3573 return (EINVAL); 3574 in6_sin6_2_sin_in_sock((struct sockaddr *)&addrs[0]); 3575 in6_sin6_2_sin_in_sock((struct sockaddr *)&addrs[1]); 3576 #ifdef INET 3577 fin = (struct sockaddr_in *)&addrs[0]; 3578 lin = (struct sockaddr_in *)&addrs[1]; 3579 #endif 3580 break; 3581 } 3582 error = sa6_embedscope(fin6, V_ip6_use_defzone); 3583 if (error) 3584 return (error); 3585 error = sa6_embedscope(lin6, V_ip6_use_defzone); 3586 if (error) 3587 return (error); 3588 break; 3589 #endif 3590 #ifdef INET 3591 case AF_INET: 3592 fin = (struct sockaddr_in *)&addrs[0]; 3593 lin = (struct sockaddr_in *)&addrs[1]; 3594 if (fin->sin_len != sizeof(struct sockaddr_in) || 3595 lin->sin_len != sizeof(struct sockaddr_in)) 3596 return (EINVAL); 3597 break; 3598 #endif 3599 default: 3600 return (EINVAL); 3601 } 3602 NET_EPOCH_ENTER(et); 3603 switch (addrs[0].ss_family) { 3604 #ifdef INET6 3605 case AF_INET6: 3606 inp = in6_pcblookup(&V_tcbinfo, &fin6->sin6_addr, 3607 fin6->sin6_port, &lin6->sin6_addr, lin6->sin6_port, 3608 INPLOOKUP_WLOCKPCB, NULL); 3609 break; 3610 #endif 3611 #ifdef INET 3612 case AF_INET: 3613 inp = in_pcblookup(&V_tcbinfo, fin->sin_addr, fin->sin_port, 3614 lin->sin_addr, lin->sin_port, INPLOOKUP_WLOCKPCB, NULL); 3615 break; 3616 #endif 3617 } 3618 if (inp != NULL) { 3619 if (!SOLISTENING(inp->inp_socket)) { 3620 tp = intotcpcb(inp); 3621 tp = tcp_drop(tp, ECONNABORTED); 3622 if (tp != NULL) 3623 INP_WUNLOCK(inp); 3624 } else 3625 INP_WUNLOCK(inp); 3626 } else 3627 error = ESRCH; 3628 NET_EPOCH_EXIT(et); 3629 return (error); 3630 } 3631 3632 SYSCTL_PROC(_net_inet_tcp, TCPCTL_DROP, drop, 3633 CTLFLAG_VNET | CTLTYPE_STRUCT | CTLFLAG_WR | CTLFLAG_SKIP | 3634 CTLFLAG_NEEDGIANT, NULL, 0, sysctl_drop, "", 3635 "Drop TCP connection"); 3636 3637 static int 3638 tcp_sysctl_setsockopt(SYSCTL_HANDLER_ARGS) 3639 { 3640 return (sysctl_setsockopt(oidp, arg1, arg2, req, &V_tcbinfo, 3641 &tcp_ctloutput_set)); 3642 } 3643 3644 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, setsockopt, 3645 CTLFLAG_VNET | CTLTYPE_STRUCT | CTLFLAG_WR | CTLFLAG_SKIP | 3646 CTLFLAG_MPSAFE, NULL, 0, tcp_sysctl_setsockopt, "", 3647 "Set socket option for TCP endpoint"); 3648 3649 #ifdef KERN_TLS 3650 static int 3651 sysctl_switch_tls(SYSCTL_HANDLER_ARGS) 3652 { 3653 /* addrs[0] is a foreign socket, addrs[1] is a local one. */ 3654 struct sockaddr_storage addrs[2]; 3655 struct inpcb *inp; 3656 #ifdef INET 3657 struct sockaddr_in *fin = NULL, *lin = NULL; 3658 #endif 3659 struct epoch_tracker et; 3660 #ifdef INET6 3661 struct sockaddr_in6 *fin6, *lin6; 3662 #endif 3663 int error; 3664 3665 inp = NULL; 3666 #ifdef INET6 3667 fin6 = lin6 = NULL; 3668 #endif 3669 error = 0; 3670 3671 if (req->oldptr != NULL || req->oldlen != 0) 3672 return (EINVAL); 3673 if (req->newptr == NULL) 3674 return (EPERM); 3675 if (req->newlen < sizeof(addrs)) 3676 return (ENOMEM); 3677 error = SYSCTL_IN(req, &addrs, sizeof(addrs)); 3678 if (error) 3679 return (error); 3680 3681 switch (addrs[0].ss_family) { 3682 #ifdef INET6 3683 case AF_INET6: 3684 fin6 = (struct sockaddr_in6 *)&addrs[0]; 3685 lin6 = (struct sockaddr_in6 *)&addrs[1]; 3686 if (fin6->sin6_len != sizeof(struct sockaddr_in6) || 3687 lin6->sin6_len != sizeof(struct sockaddr_in6)) 3688 return (EINVAL); 3689 if (IN6_IS_ADDR_V4MAPPED(&fin6->sin6_addr)) { 3690 if (!IN6_IS_ADDR_V4MAPPED(&lin6->sin6_addr)) 3691 return (EINVAL); 3692 in6_sin6_2_sin_in_sock((struct sockaddr *)&addrs[0]); 3693 in6_sin6_2_sin_in_sock((struct sockaddr *)&addrs[1]); 3694 #ifdef INET 3695 fin = (struct sockaddr_in *)&addrs[0]; 3696 lin = (struct sockaddr_in *)&addrs[1]; 3697 #endif 3698 break; 3699 } 3700 error = sa6_embedscope(fin6, V_ip6_use_defzone); 3701 if (error) 3702 return (error); 3703 error = sa6_embedscope(lin6, V_ip6_use_defzone); 3704 if (error) 3705 return (error); 3706 break; 3707 #endif 3708 #ifdef INET 3709 case AF_INET: 3710 fin = (struct sockaddr_in *)&addrs[0]; 3711 lin = (struct sockaddr_in *)&addrs[1]; 3712 if (fin->sin_len != sizeof(struct sockaddr_in) || 3713 lin->sin_len != sizeof(struct sockaddr_in)) 3714 return (EINVAL); 3715 break; 3716 #endif 3717 default: 3718 return (EINVAL); 3719 } 3720 NET_EPOCH_ENTER(et); 3721 switch (addrs[0].ss_family) { 3722 #ifdef INET6 3723 case AF_INET6: 3724 inp = in6_pcblookup(&V_tcbinfo, &fin6->sin6_addr, 3725 fin6->sin6_port, &lin6->sin6_addr, lin6->sin6_port, 3726 INPLOOKUP_WLOCKPCB, NULL); 3727 break; 3728 #endif 3729 #ifdef INET 3730 case AF_INET: 3731 inp = in_pcblookup(&V_tcbinfo, fin->sin_addr, fin->sin_port, 3732 lin->sin_addr, lin->sin_port, INPLOOKUP_WLOCKPCB, NULL); 3733 break; 3734 #endif 3735 } 3736 NET_EPOCH_EXIT(et); 3737 if (inp != NULL) { 3738 struct socket *so; 3739 3740 so = inp->inp_socket; 3741 soref(so); 3742 error = ktls_set_tx_mode(so, 3743 arg2 == 0 ? TCP_TLS_MODE_SW : TCP_TLS_MODE_IFNET); 3744 INP_WUNLOCK(inp); 3745 sorele(so); 3746 } else 3747 error = ESRCH; 3748 return (error); 3749 } 3750 3751 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, switch_to_sw_tls, 3752 CTLFLAG_VNET | CTLTYPE_STRUCT | CTLFLAG_WR | CTLFLAG_SKIP | 3753 CTLFLAG_NEEDGIANT, NULL, 0, sysctl_switch_tls, "", 3754 "Switch TCP connection to SW TLS"); 3755 SYSCTL_PROC(_net_inet_tcp, OID_AUTO, switch_to_ifnet_tls, 3756 CTLFLAG_VNET | CTLTYPE_STRUCT | CTLFLAG_WR | CTLFLAG_SKIP | 3757 CTLFLAG_NEEDGIANT, NULL, 1, sysctl_switch_tls, "", 3758 "Switch TCP connection to ifnet TLS"); 3759 #endif 3760 3761 /* 3762 * Generate a standardized TCP log line for use throughout the 3763 * tcp subsystem. Memory allocation is done with M_NOWAIT to 3764 * allow use in the interrupt context. 3765 * 3766 * NB: The caller MUST free(s, M_TCPLOG) the returned string. 3767 * NB: The function may return NULL if memory allocation failed. 3768 * 3769 * Due to header inclusion and ordering limitations the struct ip 3770 * and ip6_hdr pointers have to be passed as void pointers. 3771 */ 3772 char * 3773 tcp_log_vain(struct in_conninfo *inc, struct tcphdr *th, const void *ip4hdr, 3774 const void *ip6hdr) 3775 { 3776 3777 /* Is logging enabled? */ 3778 if (V_tcp_log_in_vain == 0) 3779 return (NULL); 3780 3781 return (tcp_log_addr(inc, th, ip4hdr, ip6hdr)); 3782 } 3783 3784 char * 3785 tcp_log_addrs(struct in_conninfo *inc, struct tcphdr *th, const void *ip4hdr, 3786 const void *ip6hdr) 3787 { 3788 3789 /* Is logging enabled? */ 3790 if (tcp_log_debug == 0) 3791 return (NULL); 3792 3793 return (tcp_log_addr(inc, th, ip4hdr, ip6hdr)); 3794 } 3795 3796 static char * 3797 tcp_log_addr(struct in_conninfo *inc, struct tcphdr *th, const void *ip4hdr, 3798 const void *ip6hdr) 3799 { 3800 char *s, *sp; 3801 size_t size; 3802 #ifdef INET 3803 const struct ip *ip = (const struct ip *)ip4hdr; 3804 #endif 3805 #ifdef INET6 3806 const struct ip6_hdr *ip6 = (const struct ip6_hdr *)ip6hdr; 3807 #endif /* INET6 */ 3808 3809 /* 3810 * The log line looks like this: 3811 * "TCP: [1.2.3.4]:50332 to [1.2.3.4]:80 tcpflags 0x2<SYN>" 3812 */ 3813 size = sizeof("TCP: []:12345 to []:12345 tcpflags 0x2<>") + 3814 sizeof(PRINT_TH_FLAGS) + 1 + 3815 #ifdef INET6 3816 2 * INET6_ADDRSTRLEN; 3817 #else 3818 2 * INET_ADDRSTRLEN; 3819 #endif /* INET6 */ 3820 3821 s = malloc(size, M_TCPLOG, M_ZERO|M_NOWAIT); 3822 if (s == NULL) 3823 return (NULL); 3824 3825 strcat(s, "TCP: ["); 3826 sp = s + strlen(s); 3827 3828 if (inc && ((inc->inc_flags & INC_ISIPV6) == 0)) { 3829 inet_ntoa_r(inc->inc_faddr, sp); 3830 sp = s + strlen(s); 3831 sprintf(sp, "]:%i to [", ntohs(inc->inc_fport)); 3832 sp = s + strlen(s); 3833 inet_ntoa_r(inc->inc_laddr, sp); 3834 sp = s + strlen(s); 3835 sprintf(sp, "]:%i", ntohs(inc->inc_lport)); 3836 #ifdef INET6 3837 } else if (inc) { 3838 ip6_sprintf(sp, &inc->inc6_faddr); 3839 sp = s + strlen(s); 3840 sprintf(sp, "]:%i to [", ntohs(inc->inc_fport)); 3841 sp = s + strlen(s); 3842 ip6_sprintf(sp, &inc->inc6_laddr); 3843 sp = s + strlen(s); 3844 sprintf(sp, "]:%i", ntohs(inc->inc_lport)); 3845 } else if (ip6 && th) { 3846 ip6_sprintf(sp, &ip6->ip6_src); 3847 sp = s + strlen(s); 3848 sprintf(sp, "]:%i to [", ntohs(th->th_sport)); 3849 sp = s + strlen(s); 3850 ip6_sprintf(sp, &ip6->ip6_dst); 3851 sp = s + strlen(s); 3852 sprintf(sp, "]:%i", ntohs(th->th_dport)); 3853 #endif /* INET6 */ 3854 #ifdef INET 3855 } else if (ip && th) { 3856 inet_ntoa_r(ip->ip_src, sp); 3857 sp = s + strlen(s); 3858 sprintf(sp, "]:%i to [", ntohs(th->th_sport)); 3859 sp = s + strlen(s); 3860 inet_ntoa_r(ip->ip_dst, sp); 3861 sp = s + strlen(s); 3862 sprintf(sp, "]:%i", ntohs(th->th_dport)); 3863 #endif /* INET */ 3864 } else { 3865 free(s, M_TCPLOG); 3866 return (NULL); 3867 } 3868 sp = s + strlen(s); 3869 if (th) 3870 sprintf(sp, " tcpflags 0x%b", tcp_get_flags(th), PRINT_TH_FLAGS); 3871 if (*(s + size - 1) != '\0') 3872 panic("%s: string too long", __func__); 3873 return (s); 3874 } 3875 3876 /* 3877 * A subroutine which makes it easy to track TCP state changes with DTrace. 3878 * This function shouldn't be called for t_state initializations that don't 3879 * correspond to actual TCP state transitions. 3880 */ 3881 void 3882 tcp_state_change(struct tcpcb *tp, int newstate) 3883 { 3884 #if defined(KDTRACE_HOOKS) 3885 int pstate = tp->t_state; 3886 #endif 3887 3888 TCPSTATES_DEC(tp->t_state); 3889 TCPSTATES_INC(newstate); 3890 tp->t_state = newstate; 3891 TCP_PROBE6(state__change, NULL, tp, NULL, tp, NULL, pstate); 3892 } 3893 3894 /* 3895 * Create an external-format (``xtcpcb'') structure using the information in 3896 * the kernel-format tcpcb structure pointed to by tp. This is done to 3897 * reduce the spew of irrelevant information over this interface, to isolate 3898 * user code from changes in the kernel structure, and potentially to provide 3899 * information-hiding if we decide that some of this information should be 3900 * hidden from users. 3901 */ 3902 void 3903 tcp_inptoxtp(const struct inpcb *inp, struct xtcpcb *xt) 3904 { 3905 struct tcpcb *tp = intotcpcb(inp); 3906 sbintime_t now; 3907 3908 bzero(xt, sizeof(*xt)); 3909 xt->t_state = tp->t_state; 3910 xt->t_logstate = tcp_get_bblog_state(tp); 3911 xt->t_flags = tp->t_flags; 3912 xt->t_sndzerowin = tp->t_sndzerowin; 3913 xt->t_sndrexmitpack = tp->t_sndrexmitpack; 3914 xt->t_rcvoopack = tp->t_rcvoopack; 3915 xt->t_rcv_wnd = tp->rcv_wnd; 3916 xt->t_snd_wnd = tp->snd_wnd; 3917 xt->t_snd_cwnd = tp->snd_cwnd; 3918 xt->t_snd_ssthresh = tp->snd_ssthresh; 3919 xt->t_dsack_bytes = tp->t_dsack_bytes; 3920 xt->t_dsack_tlp_bytes = tp->t_dsack_tlp_bytes; 3921 xt->t_dsack_pack = tp->t_dsack_pack; 3922 xt->t_maxseg = tp->t_maxseg; 3923 xt->xt_ecn = (tp->t_flags2 & TF2_ECN_PERMIT) ? 1 : 0 + 3924 (tp->t_flags2 & TF2_ACE_PERMIT) ? 2 : 0; 3925 3926 now = getsbinuptime(); 3927 #define COPYTIMER(which,where) do { \ 3928 if (tp->t_timers[which] != SBT_MAX) \ 3929 xt->where = (tp->t_timers[which] - now) / SBT_1MS; \ 3930 else \ 3931 xt->where = 0; \ 3932 } while (0) 3933 COPYTIMER(TT_DELACK, tt_delack); 3934 COPYTIMER(TT_REXMT, tt_rexmt); 3935 COPYTIMER(TT_PERSIST, tt_persist); 3936 COPYTIMER(TT_KEEP, tt_keep); 3937 COPYTIMER(TT_2MSL, tt_2msl); 3938 #undef COPYTIMER 3939 xt->t_rcvtime = 1000 * (ticks - tp->t_rcvtime) / hz; 3940 3941 xt->xt_encaps_port = tp->t_port; 3942 bcopy(tp->t_fb->tfb_tcp_block_name, xt->xt_stack, 3943 TCP_FUNCTION_NAME_LEN_MAX); 3944 bcopy(CC_ALGO(tp)->name, xt->xt_cc, TCP_CA_NAME_MAX); 3945 #ifdef TCP_BLACKBOX 3946 (void)tcp_log_get_id(tp, xt->xt_logid); 3947 #endif 3948 3949 xt->xt_len = sizeof(struct xtcpcb); 3950 in_pcbtoxinpcb(inp, &xt->xt_inp); 3951 } 3952 3953 void 3954 tcp_log_end_status(struct tcpcb *tp, uint8_t status) 3955 { 3956 uint32_t bit, i; 3957 3958 if ((tp == NULL) || 3959 (status > TCP_EI_STATUS_MAX_VALUE) || 3960 (status == 0)) { 3961 /* Invalid */ 3962 return; 3963 } 3964 if (status > (sizeof(uint32_t) * 8)) { 3965 /* Should this be a KASSERT? */ 3966 return; 3967 } 3968 bit = 1U << (status - 1); 3969 if (bit & tp->t_end_info_status) { 3970 /* already logged */ 3971 return; 3972 } 3973 for (i = 0; i < TCP_END_BYTE_INFO; i++) { 3974 if (tp->t_end_info_bytes[i] == TCP_EI_EMPTY_SLOT) { 3975 tp->t_end_info_bytes[i] = status; 3976 tp->t_end_info_status |= bit; 3977 break; 3978 } 3979 } 3980 } 3981 3982 int 3983 tcp_can_enable_pacing(void) 3984 { 3985 3986 if ((tcp_pacing_limit == -1) || 3987 (tcp_pacing_limit > number_of_tcp_connections_pacing)) { 3988 atomic_fetchadd_int(&number_of_tcp_connections_pacing, 1); 3989 shadow_num_connections = number_of_tcp_connections_pacing; 3990 return (1); 3991 } else { 3992 counter_u64_add(tcp_pacing_failures, 1); 3993 return (0); 3994 } 3995 } 3996 3997 int 3998 tcp_incr_dgp_pacing_cnt(void) 3999 { 4000 if ((tcp_dgp_limit == -1) || 4001 (tcp_dgp_limit > number_of_dgp_connections)) { 4002 atomic_fetchadd_int(&number_of_dgp_connections, 1); 4003 shadow_tcp_pacing_dgp = number_of_dgp_connections; 4004 return (1); 4005 } else { 4006 counter_u64_add(tcp_dgp_failures, 1); 4007 return (0); 4008 } 4009 } 4010 4011 static uint8_t tcp_dgp_warning = 0; 4012 4013 void 4014 tcp_dec_dgp_pacing_cnt(void) 4015 { 4016 uint32_t ret; 4017 4018 ret = atomic_fetchadd_int(&number_of_dgp_connections, -1); 4019 shadow_tcp_pacing_dgp = number_of_dgp_connections; 4020 KASSERT(ret != 0, ("number_of_dgp_connections -1 would cause wrap?")); 4021 if (ret == 0) { 4022 if (tcp_dgp_limit != -1) { 4023 printf("Warning all DGP is now disabled, count decrements invalidly!\n"); 4024 tcp_dgp_limit = 0; 4025 tcp_dgp_warning = 1; 4026 } else if (tcp_dgp_warning == 0) { 4027 printf("Warning DGP pacing is invalid, invalid decrement\n"); 4028 tcp_dgp_warning = 1; 4029 } 4030 } 4031 4032 } 4033 4034 static uint8_t tcp_pacing_warning = 0; 4035 4036 void 4037 tcp_decrement_paced_conn(void) 4038 { 4039 uint32_t ret; 4040 4041 ret = atomic_fetchadd_int(&number_of_tcp_connections_pacing, -1); 4042 shadow_num_connections = number_of_tcp_connections_pacing; 4043 KASSERT(ret != 0, ("tcp_paced_connection_exits -1 would cause wrap?")); 4044 if (ret == 0) { 4045 if (tcp_pacing_limit != -1) { 4046 printf("Warning all pacing is now disabled, count decrements invalidly!\n"); 4047 tcp_pacing_limit = 0; 4048 } else if (tcp_pacing_warning == 0) { 4049 printf("Warning pacing count is invalid, invalid decrement\n"); 4050 tcp_pacing_warning = 1; 4051 } 4052 } 4053 } 4054 4055 static void 4056 tcp_default_switch_failed(struct tcpcb *tp) 4057 { 4058 /* 4059 * If a switch fails we only need to 4060 * care about two things: 4061 * a) The t_flags2 4062 * and 4063 * b) The timer granularity. 4064 * Timeouts, at least for now, don't use the 4065 * old callout system in the other stacks so 4066 * those are hopefully safe. 4067 */ 4068 tcp_lro_features_off(tp); 4069 tcp_change_time_units(tp, TCP_TMR_GRANULARITY_TICKS); 4070 } 4071 4072 #ifdef TCP_ACCOUNTING 4073 int 4074 tcp_do_ack_accounting(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to, uint32_t tiwin, int mss) 4075 { 4076 if (SEQ_LT(th->th_ack, tp->snd_una)) { 4077 /* Do we have a SACK? */ 4078 if (to->to_flags & TOF_SACK) { 4079 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 4080 tp->tcp_cnt_counters[ACK_SACK]++; 4081 } 4082 return (ACK_SACK); 4083 } else { 4084 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 4085 tp->tcp_cnt_counters[ACK_BEHIND]++; 4086 } 4087 return (ACK_BEHIND); 4088 } 4089 } else if (th->th_ack == tp->snd_una) { 4090 /* Do we have a SACK? */ 4091 if (to->to_flags & TOF_SACK) { 4092 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 4093 tp->tcp_cnt_counters[ACK_SACK]++; 4094 } 4095 return (ACK_SACK); 4096 } else if (tiwin != tp->snd_wnd) { 4097 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 4098 tp->tcp_cnt_counters[ACK_RWND]++; 4099 } 4100 return (ACK_RWND); 4101 } else { 4102 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 4103 tp->tcp_cnt_counters[ACK_DUPACK]++; 4104 } 4105 return (ACK_DUPACK); 4106 } 4107 } else { 4108 if (!SEQ_GT(th->th_ack, tp->snd_max)) { 4109 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 4110 tp->tcp_cnt_counters[CNT_OF_ACKS_IN] += (((th->th_ack - tp->snd_una) + mss - 1)/mss); 4111 } 4112 } 4113 if (to->to_flags & TOF_SACK) { 4114 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 4115 tp->tcp_cnt_counters[ACK_CUMACK_SACK]++; 4116 } 4117 return (ACK_CUMACK_SACK); 4118 } else { 4119 if (tp->t_flags2 & TF2_TCP_ACCOUNTING) { 4120 tp->tcp_cnt_counters[ACK_CUMACK]++; 4121 } 4122 return (ACK_CUMACK); 4123 } 4124 } 4125 } 4126 #endif 4127 4128 void 4129 tcp_change_time_units(struct tcpcb *tp, int granularity) 4130 { 4131 if (tp->t_tmr_granularity == granularity) { 4132 /* We are there */ 4133 return; 4134 } 4135 if (granularity == TCP_TMR_GRANULARITY_USEC) { 4136 KASSERT((tp->t_tmr_granularity == TCP_TMR_GRANULARITY_TICKS), 4137 ("Granularity is not TICKS its %u in tp:%p", 4138 tp->t_tmr_granularity, tp)); 4139 tp->t_rttlow = TICKS_2_USEC(tp->t_rttlow); 4140 if (tp->t_srtt > 1) { 4141 uint32_t val, frac; 4142 4143 val = tp->t_srtt >> TCP_RTT_SHIFT; 4144 frac = tp->t_srtt & 0x1f; 4145 tp->t_srtt = TICKS_2_USEC(val); 4146 /* 4147 * frac is the fractional part of the srtt (if any) 4148 * but its in ticks and every bit represents 4149 * 1/32nd of a hz. 4150 */ 4151 if (frac) { 4152 if (hz == 1000) { 4153 frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_MSEC) / (uint64_t)TCP_RTT_SCALE); 4154 } else { 4155 frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_SEC) / ((uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE)); 4156 } 4157 tp->t_srtt += frac; 4158 } 4159 } 4160 if (tp->t_rttvar) { 4161 uint32_t val, frac; 4162 4163 val = tp->t_rttvar >> TCP_RTTVAR_SHIFT; 4164 frac = tp->t_rttvar & 0x1f; 4165 tp->t_rttvar = TICKS_2_USEC(val); 4166 /* 4167 * frac is the fractional part of the srtt (if any) 4168 * but its in ticks and every bit represents 4169 * 1/32nd of a hz. 4170 */ 4171 if (frac) { 4172 if (hz == 1000) { 4173 frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_MSEC) / (uint64_t)TCP_RTT_SCALE); 4174 } else { 4175 frac = (((uint64_t)frac * (uint64_t)HPTS_USEC_IN_SEC) / ((uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE)); 4176 } 4177 tp->t_rttvar += frac; 4178 } 4179 } 4180 tp->t_tmr_granularity = TCP_TMR_GRANULARITY_USEC; 4181 } else if (granularity == TCP_TMR_GRANULARITY_TICKS) { 4182 /* Convert back to ticks, with */ 4183 KASSERT((tp->t_tmr_granularity == TCP_TMR_GRANULARITY_USEC), 4184 ("Granularity is not USEC its %u in tp:%p", 4185 tp->t_tmr_granularity, tp)); 4186 if (tp->t_srtt > 1) { 4187 uint32_t val, frac; 4188 4189 val = USEC_2_TICKS(tp->t_srtt); 4190 frac = tp->t_srtt % (HPTS_USEC_IN_SEC / hz); 4191 tp->t_srtt = val << TCP_RTT_SHIFT; 4192 /* 4193 * frac is the fractional part here is left 4194 * over from converting to hz and shifting. 4195 * We need to convert this to the 5 bit 4196 * remainder. 4197 */ 4198 if (frac) { 4199 if (hz == 1000) { 4200 frac = (((uint64_t)frac * (uint64_t)TCP_RTT_SCALE) / (uint64_t)HPTS_USEC_IN_MSEC); 4201 } else { 4202 frac = (((uint64_t)frac * (uint64_t)(hz) * (uint64_t)TCP_RTT_SCALE) /(uint64_t)HPTS_USEC_IN_SEC); 4203 } 4204 tp->t_srtt += frac; 4205 } 4206 } 4207 if (tp->t_rttvar) { 4208 uint32_t val, frac; 4209 4210 val = USEC_2_TICKS(tp->t_rttvar); 4211 frac = tp->t_rttvar % (HPTS_USEC_IN_SEC / hz); 4212 tp->t_rttvar = val << TCP_RTTVAR_SHIFT; 4213 /* 4214 * frac is the fractional part here is left 4215 * over from converting to hz and shifting. 4216 * We need to convert this to the 4 bit 4217 * remainder. 4218 */ 4219 if (frac) { 4220 if (hz == 1000) { 4221 frac = (((uint64_t)frac * (uint64_t)TCP_RTTVAR_SCALE) / (uint64_t)HPTS_USEC_IN_MSEC); 4222 } else { 4223 frac = (((uint64_t)frac * (uint64_t)(hz) * (uint64_t)TCP_RTTVAR_SCALE) /(uint64_t)HPTS_USEC_IN_SEC); 4224 } 4225 tp->t_rttvar += frac; 4226 } 4227 } 4228 tp->t_rttlow = USEC_2_TICKS(tp->t_rttlow); 4229 tp->t_tmr_granularity = TCP_TMR_GRANULARITY_TICKS; 4230 } 4231 #ifdef INVARIANTS 4232 else { 4233 panic("Unknown granularity:%d tp:%p", 4234 granularity, tp); 4235 } 4236 #endif 4237 } 4238 4239 void 4240 tcp_handle_orphaned_packets(struct tcpcb *tp) 4241 { 4242 struct mbuf *save, *m, *prev; 4243 /* 4244 * Called when a stack switch is occuring from the fini() 4245 * of the old stack. We assue the init() as already been 4246 * run of the new stack and it has set the t_flags2 to 4247 * what it supports. This function will then deal with any 4248 * differences i.e. cleanup packets that maybe queued that 4249 * the newstack does not support. 4250 */ 4251 4252 if (tp->t_flags2 & TF2_MBUF_L_ACKS) 4253 return; 4254 if ((tp->t_flags2 & TF2_SUPPORTS_MBUFQ) == 0 && 4255 !STAILQ_EMPTY(&tp->t_inqueue)) { 4256 /* 4257 * It is unsafe to process the packets since a 4258 * reset may be lurking in them (its rare but it 4259 * can occur). If we were to find a RST, then we 4260 * would end up dropping the connection and the 4261 * INP lock, so when we return the caller (tcp_usrreq) 4262 * will blow up when it trys to unlock the inp. 4263 * This new stack does not do any fancy LRO features 4264 * so all we can do is toss the packets. 4265 */ 4266 m = STAILQ_FIRST(&tp->t_inqueue); 4267 STAILQ_INIT(&tp->t_inqueue); 4268 STAILQ_FOREACH_FROM_SAFE(m, &tp->t_inqueue, m_stailqpkt, save) 4269 m_freem(m); 4270 } else { 4271 /* 4272 * Here we have a stack that does mbuf queuing but 4273 * does not support compressed ack's. We must 4274 * walk all the mbufs and discard any compressed acks. 4275 */ 4276 STAILQ_FOREACH_SAFE(m, &tp->t_inqueue, m_stailqpkt, save) { 4277 if (m->m_flags & M_ACKCMP) { 4278 if (m == STAILQ_FIRST(&tp->t_inqueue)) 4279 STAILQ_REMOVE_HEAD(&tp->t_inqueue, 4280 m_stailqpkt); 4281 else 4282 STAILQ_REMOVE_AFTER(&tp->t_inqueue, 4283 prev, m_stailqpkt); 4284 m_freem(m); 4285 } else 4286 prev = m; 4287 } 4288 } 4289 } 4290 4291 #ifdef TCP_REQUEST_TRK 4292 uint32_t 4293 tcp_estimate_tls_overhead(struct socket *so, uint64_t tls_usr_bytes) 4294 { 4295 #ifdef KERN_TLS 4296 struct ktls_session *tls; 4297 uint32_t rec_oh, records; 4298 4299 tls = so->so_snd.sb_tls_info; 4300 if (tls == NULL) 4301 return (0); 4302 4303 rec_oh = tls->params.tls_hlen + tls->params.tls_tlen; 4304 records = ((tls_usr_bytes + tls->params.max_frame_len - 1)/tls->params.max_frame_len); 4305 return (records * rec_oh); 4306 #else 4307 return (0); 4308 #endif 4309 } 4310 4311 extern uint32_t tcp_stale_entry_time; 4312 uint32_t tcp_stale_entry_time = 250000; 4313 SYSCTL_UINT(_net_inet_tcp, OID_AUTO, usrlog_stale, CTLFLAG_RW, 4314 &tcp_stale_entry_time, 250000, "Time that a tcpreq entry without a sendfile ages out"); 4315 4316 void 4317 tcp_req_log_req_info(struct tcpcb *tp, struct tcp_sendfile_track *req, 4318 uint16_t slot, uint8_t val, uint64_t offset, uint64_t nbytes) 4319 { 4320 if (tcp_bblogging_on(tp)) { 4321 union tcp_log_stackspecific log; 4322 struct timeval tv; 4323 4324 memset(&log.u_bbr, 0, sizeof(log.u_bbr)); 4325 log.u_bbr.inhpts = tcp_in_hpts(tp); 4326 log.u_bbr.flex8 = val; 4327 log.u_bbr.rttProp = req->timestamp; 4328 log.u_bbr.delRate = req->start; 4329 log.u_bbr.cur_del_rate = req->end; 4330 log.u_bbr.flex1 = req->start_seq; 4331 log.u_bbr.flex2 = req->end_seq; 4332 log.u_bbr.flex3 = req->flags; 4333 log.u_bbr.flex4 = ((req->localtime >> 32) & 0x00000000ffffffff); 4334 log.u_bbr.flex5 = (req->localtime & 0x00000000ffffffff); 4335 log.u_bbr.flex7 = slot; 4336 log.u_bbr.bw_inuse = offset; 4337 /* nbytes = flex6 | epoch */ 4338 log.u_bbr.flex6 = ((nbytes >> 32) & 0x00000000ffffffff); 4339 log.u_bbr.epoch = (nbytes & 0x00000000ffffffff); 4340 /* cspr = lt_epoch | pkts_out */ 4341 log.u_bbr.lt_epoch = ((req->cspr >> 32) & 0x00000000ffffffff); 4342 log.u_bbr.pkts_out |= (req->cspr & 0x00000000ffffffff); 4343 log.u_bbr.applimited = tp->t_tcpreq_closed; 4344 log.u_bbr.applimited <<= 8; 4345 log.u_bbr.applimited |= tp->t_tcpreq_open; 4346 log.u_bbr.applimited <<= 8; 4347 log.u_bbr.applimited |= tp->t_tcpreq_req; 4348 log.u_bbr.timeStamp = tcp_get_usecs(&tv); 4349 TCP_LOG_EVENTP(tp, NULL, 4350 &tptosocket(tp)->so_rcv, 4351 &tptosocket(tp)->so_snd, 4352 TCP_LOG_REQ_T, 0, 4353 0, &log, false, &tv); 4354 } 4355 } 4356 4357 void 4358 tcp_req_free_a_slot(struct tcpcb *tp, struct tcp_sendfile_track *ent) 4359 { 4360 if (tp->t_tcpreq_req > 0) 4361 tp->t_tcpreq_req--; 4362 if (ent->flags & TCP_TRK_TRACK_FLG_OPEN) { 4363 if (tp->t_tcpreq_open > 0) 4364 tp->t_tcpreq_open--; 4365 } else { 4366 if (tp->t_tcpreq_closed > 0) 4367 tp->t_tcpreq_closed--; 4368 } 4369 ent->flags = TCP_TRK_TRACK_FLG_EMPTY; 4370 } 4371 4372 static void 4373 tcp_req_check_for_stale_entries(struct tcpcb *tp, uint64_t ts, int rm_oldest) 4374 { 4375 struct tcp_sendfile_track *ent; 4376 uint64_t time_delta, oldest_delta; 4377 int i, oldest, oldest_set = 0, cnt_rm = 0; 4378 4379 for(i = 0; i < MAX_TCP_TRK_REQ; i++) { 4380 ent = &tp->t_tcpreq_info[i]; 4381 if (ent->flags != TCP_TRK_TRACK_FLG_USED) { 4382 /* 4383 * We only care about closed end ranges 4384 * that are allocated and have no sendfile 4385 * ever touching them. They would be in 4386 * state USED. 4387 */ 4388 continue; 4389 } 4390 if (ts >= ent->localtime) 4391 time_delta = ts - ent->localtime; 4392 else 4393 time_delta = 0; 4394 if (time_delta && 4395 ((oldest_delta < time_delta) || (oldest_set == 0))) { 4396 oldest_set = 1; 4397 oldest = i; 4398 oldest_delta = time_delta; 4399 } 4400 if (tcp_stale_entry_time && (time_delta >= tcp_stale_entry_time)) { 4401 /* 4402 * No sendfile in a our time-limit 4403 * time to purge it. 4404 */ 4405 cnt_rm++; 4406 tcp_req_log_req_info(tp, &tp->t_tcpreq_info[i], i, TCP_TRK_REQ_LOG_STALE, 4407 time_delta, 0); 4408 tcp_req_free_a_slot(tp, ent); 4409 } 4410 } 4411 if ((cnt_rm == 0) && rm_oldest && oldest_set) { 4412 ent = &tp->t_tcpreq_info[oldest]; 4413 tcp_req_log_req_info(tp, &tp->t_tcpreq_info[i], i, TCP_TRK_REQ_LOG_STALE, 4414 oldest_delta, 1); 4415 tcp_req_free_a_slot(tp, ent); 4416 } 4417 } 4418 4419 int 4420 tcp_req_check_for_comp(struct tcpcb *tp, tcp_seq ack_point) 4421 { 4422 int i, ret=0; 4423 struct tcp_sendfile_track *ent; 4424 4425 /* Clean up any old closed end requests that are now completed */ 4426 if (tp->t_tcpreq_req == 0) 4427 return(0); 4428 if (tp->t_tcpreq_closed == 0) 4429 return(0); 4430 for(i = 0; i < MAX_TCP_TRK_REQ; i++) { 4431 ent = &tp->t_tcpreq_info[i]; 4432 /* Skip empty ones */ 4433 if (ent->flags == TCP_TRK_TRACK_FLG_EMPTY) 4434 continue; 4435 /* Skip open ones */ 4436 if (ent->flags & TCP_TRK_TRACK_FLG_OPEN) 4437 continue; 4438 if (SEQ_GEQ(ack_point, ent->end_seq)) { 4439 /* We are past it -- free it */ 4440 tcp_req_log_req_info(tp, ent, 4441 i, TCP_TRK_REQ_LOG_FREED, 0, 0); 4442 tcp_req_free_a_slot(tp, ent); 4443 ret++; 4444 } 4445 } 4446 return (ret); 4447 } 4448 4449 int 4450 tcp_req_is_entry_comp(struct tcpcb *tp, struct tcp_sendfile_track *ent, tcp_seq ack_point) 4451 { 4452 if (tp->t_tcpreq_req == 0) 4453 return(-1); 4454 if (tp->t_tcpreq_closed == 0) 4455 return(-1); 4456 if (ent->flags == TCP_TRK_TRACK_FLG_EMPTY) 4457 return(-1); 4458 if (SEQ_GEQ(ack_point, ent->end_seq)) { 4459 return (1); 4460 } 4461 return (0); 4462 } 4463 4464 struct tcp_sendfile_track * 4465 tcp_req_find_a_req_that_is_completed_by(struct tcpcb *tp, tcp_seq th_ack, int *ip) 4466 { 4467 /* 4468 * Given an ack point (th_ack) walk through our entries and 4469 * return the first one found that th_ack goes past the 4470 * end_seq. 4471 */ 4472 struct tcp_sendfile_track *ent; 4473 int i; 4474 4475 if (tp->t_tcpreq_req == 0) { 4476 /* none open */ 4477 return (NULL); 4478 } 4479 for(i = 0; i < MAX_TCP_TRK_REQ; i++) { 4480 ent = &tp->t_tcpreq_info[i]; 4481 if (ent->flags == TCP_TRK_TRACK_FLG_EMPTY) 4482 continue; 4483 if ((ent->flags & TCP_TRK_TRACK_FLG_OPEN) == 0) { 4484 if (SEQ_GEQ(th_ack, ent->end_seq)) { 4485 *ip = i; 4486 return (ent); 4487 } 4488 } 4489 } 4490 return (NULL); 4491 } 4492 4493 struct tcp_sendfile_track * 4494 tcp_req_find_req_for_seq(struct tcpcb *tp, tcp_seq seq) 4495 { 4496 struct tcp_sendfile_track *ent; 4497 int i; 4498 4499 if (tp->t_tcpreq_req == 0) { 4500 /* none open */ 4501 return (NULL); 4502 } 4503 for(i = 0; i < MAX_TCP_TRK_REQ; i++) { 4504 ent = &tp->t_tcpreq_info[i]; 4505 tcp_req_log_req_info(tp, ent, i, TCP_TRK_REQ_LOG_SEARCH, 4506 (uint64_t)seq, 0); 4507 if (ent->flags == TCP_TRK_TRACK_FLG_EMPTY) { 4508 continue; 4509 } 4510 if (ent->flags & TCP_TRK_TRACK_FLG_OPEN) { 4511 /* 4512 * An open end request only needs to 4513 * match the beginning seq or be 4514 * all we have (once we keep going on 4515 * a open end request we may have a seq 4516 * wrap). 4517 */ 4518 if ((SEQ_GEQ(seq, ent->start_seq)) || 4519 (tp->t_tcpreq_closed == 0)) 4520 return (ent); 4521 } else { 4522 /* 4523 * For this one we need to 4524 * be a bit more careful if its 4525 * completed at least. 4526 */ 4527 if ((SEQ_GEQ(seq, ent->start_seq)) && 4528 (SEQ_LT(seq, ent->end_seq))) { 4529 return (ent); 4530 } 4531 } 4532 } 4533 return (NULL); 4534 } 4535 4536 /* Should this be in its own file tcp_req.c ? */ 4537 struct tcp_sendfile_track * 4538 tcp_req_alloc_req_full(struct tcpcb *tp, struct tcp_snd_req *req, uint64_t ts, int rec_dups) 4539 { 4540 struct tcp_sendfile_track *fil; 4541 int i, allocated; 4542 4543 /* In case the stack does not check for completions do so now */ 4544 tcp_req_check_for_comp(tp, tp->snd_una); 4545 /* Check for stale entries */ 4546 if (tp->t_tcpreq_req) 4547 tcp_req_check_for_stale_entries(tp, ts, 4548 (tp->t_tcpreq_req >= MAX_TCP_TRK_REQ)); 4549 /* Check to see if this is a duplicate of one not started */ 4550 if (tp->t_tcpreq_req) { 4551 for(i = 0, allocated = 0; i < MAX_TCP_TRK_REQ; i++) { 4552 fil = &tp->t_tcpreq_info[i]; 4553 if ((fil->flags & TCP_TRK_TRACK_FLG_USED) == 0) 4554 continue; 4555 if ((fil->timestamp == req->timestamp) && 4556 (fil->start == req->start) && 4557 ((fil->flags & TCP_TRK_TRACK_FLG_OPEN) || 4558 (fil->end == req->end))) { 4559 /* 4560 * We already have this request 4561 * and it has not been started with sendfile. 4562 * This probably means the user was returned 4563 * a 4xx of some sort and its going to age 4564 * out, lets not duplicate it. 4565 */ 4566 return(fil); 4567 } 4568 } 4569 } 4570 /* Ok if there is no room at the inn we are in trouble */ 4571 if (tp->t_tcpreq_req >= MAX_TCP_TRK_REQ) { 4572 tcp_trace_point(tp, TCP_TP_REQ_LOG_FAIL); 4573 for(i = 0; i < MAX_TCP_TRK_REQ; i++) { 4574 tcp_req_log_req_info(tp, &tp->t_tcpreq_info[i], 4575 i, TCP_TRK_REQ_LOG_ALLOCFAIL, 0, 0); 4576 } 4577 return (NULL); 4578 } 4579 for(i = 0, allocated = 0; i < MAX_TCP_TRK_REQ; i++) { 4580 fil = &tp->t_tcpreq_info[i]; 4581 if (fil->flags == TCP_TRK_TRACK_FLG_EMPTY) { 4582 allocated = 1; 4583 fil->flags = TCP_TRK_TRACK_FLG_USED; 4584 fil->timestamp = req->timestamp; 4585 fil->playout_ms = req->playout_ms; 4586 fil->localtime = ts; 4587 fil->start = req->start; 4588 if (req->flags & TCP_LOG_HTTPD_RANGE_END) { 4589 fil->end = req->end; 4590 } else { 4591 fil->end = 0; 4592 fil->flags |= TCP_TRK_TRACK_FLG_OPEN; 4593 } 4594 /* 4595 * We can set the min boundaries to the TCP Sequence space, 4596 * but it might be found to be further up when sendfile 4597 * actually runs on this range (if it ever does). 4598 */ 4599 fil->sbcc_at_s = tptosocket(tp)->so_snd.sb_ccc; 4600 fil->start_seq = tp->snd_una + 4601 tptosocket(tp)->so_snd.sb_ccc; 4602 if (req->flags & TCP_LOG_HTTPD_RANGE_END) 4603 fil->end_seq = (fil->start_seq + ((uint32_t)(fil->end - fil->start))); 4604 else 4605 fil->end_seq = 0; 4606 if (tptosocket(tp)->so_snd.sb_tls_info) { 4607 /* 4608 * This session is doing TLS. Take a swag guess 4609 * at the overhead. 4610 */ 4611 fil->end_seq += tcp_estimate_tls_overhead( 4612 tptosocket(tp), (fil->end - fil->start)); 4613 } 4614 tp->t_tcpreq_req++; 4615 if (fil->flags & TCP_TRK_TRACK_FLG_OPEN) 4616 tp->t_tcpreq_open++; 4617 else 4618 tp->t_tcpreq_closed++; 4619 tcp_req_log_req_info(tp, fil, i, 4620 TCP_TRK_REQ_LOG_NEW, 0, 0); 4621 break; 4622 } else 4623 fil = NULL; 4624 } 4625 return (fil); 4626 } 4627 4628 void 4629 tcp_req_alloc_req(struct tcpcb *tp, union tcp_log_userdata *user, uint64_t ts) 4630 { 4631 (void)tcp_req_alloc_req_full(tp, &user->tcp_req, ts, 1); 4632 } 4633 #endif 4634 4635 void 4636 tcp_log_socket_option(struct tcpcb *tp, uint32_t option_num, uint32_t option_val, int err) 4637 { 4638 if (tcp_bblogging_on(tp)) { 4639 struct tcp_log_buffer *l; 4640 4641 l = tcp_log_event(tp, NULL, 4642 &tptosocket(tp)->so_rcv, 4643 &tptosocket(tp)->so_snd, 4644 TCP_LOG_SOCKET_OPT, 4645 err, 0, NULL, 1, 4646 NULL, NULL, 0, NULL); 4647 if (l) { 4648 l->tlb_flex1 = option_num; 4649 l->tlb_flex2 = option_val; 4650 } 4651 } 4652 } 4653 4654 uint32_t 4655 tcp_get_srtt(struct tcpcb *tp, int granularity) 4656 { 4657 uint32_t srtt; 4658 4659 KASSERT(granularity == TCP_TMR_GRANULARITY_USEC || 4660 granularity == TCP_TMR_GRANULARITY_TICKS, 4661 ("%s: called with unexpected granularity %d", __func__, 4662 granularity)); 4663 4664 srtt = tp->t_srtt; 4665 4666 /* 4667 * We only support two granularities. If the stored granularity 4668 * does not match the granularity requested by the caller, 4669 * convert the stored value to the requested unit of granularity. 4670 */ 4671 if (tp->t_tmr_granularity != granularity) { 4672 if (granularity == TCP_TMR_GRANULARITY_USEC) 4673 srtt = TICKS_2_USEC(srtt); 4674 else 4675 srtt = USEC_2_TICKS(srtt); 4676 } 4677 4678 /* 4679 * If the srtt is stored with ticks granularity, we need to 4680 * unshift to get the actual value. We do this after the 4681 * conversion above (if one was necessary) in order to maximize 4682 * precision. 4683 */ 4684 if (tp->t_tmr_granularity == TCP_TMR_GRANULARITY_TICKS) 4685 srtt = srtt >> TCP_RTT_SHIFT; 4686 4687 return (srtt); 4688 } 4689 4690 void 4691 tcp_account_for_send(struct tcpcb *tp, uint32_t len, uint8_t is_rxt, 4692 uint8_t is_tlp, bool hw_tls) 4693 { 4694 4695 if (is_tlp) { 4696 tp->t_sndtlppack++; 4697 tp->t_sndtlpbyte += len; 4698 } 4699 /* To get total bytes sent you must add t_snd_rxt_bytes to t_sndbytes */ 4700 if (is_rxt) 4701 tp->t_snd_rxt_bytes += len; 4702 else 4703 tp->t_sndbytes += len; 4704 4705 #ifdef KERN_TLS 4706 if (hw_tls && is_rxt && len != 0) { 4707 uint64_t rexmit_percent; 4708 4709 rexmit_percent = (1000ULL * tp->t_snd_rxt_bytes) / 4710 (10ULL * (tp->t_snd_rxt_bytes + tp->t_sndbytes)); 4711 if (rexmit_percent > ktls_ifnet_max_rexmit_pct) 4712 ktls_disable_ifnet(tp); 4713 } 4714 #endif 4715 } 4716