tcp_timewait.c revision 162084
1/*- 2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)tcp_subr.c 8.2 (Berkeley) 5/24/95 30 * $FreeBSD: head/sys/netinet/tcp_timewait.c 162084 2006-09-06 21:51:59Z andre $ 31 */ 32 33#include "opt_compat.h" 34#include "opt_inet.h" 35#include "opt_inet6.h" 36#include "opt_ipsec.h" 37#include "opt_mac.h" 38#include "opt_tcpdebug.h" 39#include "opt_tcp_sack.h" 40 41#include <sys/param.h> 42#include <sys/systm.h> 43#include <sys/callout.h> 44#include <sys/kernel.h> 45#include <sys/sysctl.h> 46#include <sys/mac.h> 47#include <sys/malloc.h> 48#include <sys/mbuf.h> 49#ifdef INET6 50#include <sys/domain.h> 51#endif 52#include <sys/proc.h> 53#include <sys/socket.h> 54#include <sys/socketvar.h> 55#include <sys/protosw.h> 56#include <sys/random.h> 57 58#include <vm/uma.h> 59 60#include <net/route.h> 61#include <net/if.h> 62 63#include <netinet/in.h> 64#include <netinet/in_systm.h> 65#include <netinet/ip.h> 66#ifdef INET6 67#include <netinet/ip6.h> 68#endif 69#include <netinet/in_pcb.h> 70#ifdef INET6 71#include <netinet6/in6_pcb.h> 72#endif 73#include <netinet/in_var.h> 74#include <netinet/ip_var.h> 75#ifdef INET6 76#include <netinet6/ip6_var.h> 77#include <netinet6/scope6_var.h> 78#include <netinet6/nd6.h> 79#endif 80#include <netinet/ip_icmp.h> 81#include <netinet/tcp.h> 82#include <netinet/tcp_fsm.h> 83#include <netinet/tcp_seq.h> 84#include <netinet/tcp_timer.h> 85#include <netinet/tcp_var.h> 86#ifdef INET6 87#include <netinet6/tcp6_var.h> 88#endif 89#include <netinet/tcpip.h> 90#ifdef TCPDEBUG 91#include <netinet/tcp_debug.h> 92#endif 93#include <netinet6/ip6protosw.h> 94 95#ifdef IPSEC 96#include <netinet6/ipsec.h> 97#ifdef INET6 98#include <netinet6/ipsec6.h> 99#endif 100#include <netkey/key.h> 101#endif /*IPSEC*/ 102 103#ifdef FAST_IPSEC 104#include <netipsec/ipsec.h> 105#include <netipsec/xform.h> 106#ifdef INET6 107#include <netipsec/ipsec6.h> 108#endif 109#include <netipsec/key.h> 110#define IPSEC 111#endif /*FAST_IPSEC*/ 112 113#include <machine/in_cksum.h> 114#include <sys/md5.h> 115 116int tcp_mssdflt = TCP_MSS; 117SYSCTL_INT(_net_inet_tcp, TCPCTL_MSSDFLT, mssdflt, CTLFLAG_RW, 118 &tcp_mssdflt , 0, "Default TCP Maximum Segment Size"); 119 120#ifdef INET6 121int tcp_v6mssdflt = TCP6_MSS; 122SYSCTL_INT(_net_inet_tcp, TCPCTL_V6MSSDFLT, v6mssdflt, 123 CTLFLAG_RW, &tcp_v6mssdflt , 0, 124 "Default TCP Maximum Segment Size for IPv6"); 125#endif 126 127/* 128 * Minimum MSS we accept and use. This prevents DoS attacks where 129 * we are forced to a ridiculous low MSS like 20 and send hundreds 130 * of packets instead of one. The effect scales with the available 131 * bandwidth and quickly saturates the CPU and network interface 132 * with packet generation and sending. Set to zero to disable MINMSS 133 * checking. This setting prevents us from sending too small packets. 134 */ 135int tcp_minmss = TCP_MINMSS; 136SYSCTL_INT(_net_inet_tcp, OID_AUTO, minmss, CTLFLAG_RW, 137 &tcp_minmss , 0, "Minmum TCP Maximum Segment Size"); 138/* 139 * Number of TCP segments per second we accept from remote host 140 * before we start to calculate average segment size. If average 141 * segment size drops below the minimum TCP MSS we assume a DoS 142 * attack and reset+drop the connection. Care has to be taken not to 143 * set this value too small to not kill interactive type connections 144 * (telnet, SSH) which send many small packets. 145 */ 146int tcp_minmssoverload = TCP_MINMSSOVERLOAD; 147SYSCTL_INT(_net_inet_tcp, OID_AUTO, minmssoverload, CTLFLAG_RW, 148 &tcp_minmssoverload , 0, "Number of TCP Segments per Second allowed to" 149 "be under the MINMSS Size"); 150 151#if 0 152static int tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ; 153SYSCTL_INT(_net_inet_tcp, TCPCTL_RTTDFLT, rttdflt, CTLFLAG_RW, 154 &tcp_rttdflt , 0, "Default maximum TCP Round Trip Time"); 155#endif 156 157int tcp_do_rfc1323 = 1; 158SYSCTL_INT(_net_inet_tcp, TCPCTL_DO_RFC1323, rfc1323, CTLFLAG_RW, 159 &tcp_do_rfc1323 , 0, "Enable rfc1323 (high performance TCP) extensions"); 160 161static int tcp_tcbhashsize = 0; 162SYSCTL_INT(_net_inet_tcp, OID_AUTO, tcbhashsize, CTLFLAG_RDTUN, 163 &tcp_tcbhashsize, 0, "Size of TCP control-block hashtable"); 164 165static int do_tcpdrain = 1; 166SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_tcpdrain, CTLFLAG_RW, &do_tcpdrain, 0, 167 "Enable tcp_drain routine for extra help when low on mbufs"); 168 169SYSCTL_INT(_net_inet_tcp, OID_AUTO, pcbcount, CTLFLAG_RD, 170 &tcbinfo.ipi_count, 0, "Number of active PCBs"); 171 172static int icmp_may_rst = 1; 173SYSCTL_INT(_net_inet_tcp, OID_AUTO, icmp_may_rst, CTLFLAG_RW, &icmp_may_rst, 0, 174 "Certain ICMP unreachable messages may abort connections in SYN_SENT"); 175 176static int tcp_isn_reseed_interval = 0; 177SYSCTL_INT(_net_inet_tcp, OID_AUTO, isn_reseed_interval, CTLFLAG_RW, 178 &tcp_isn_reseed_interval, 0, "Seconds between reseeding of ISN secret"); 179 180static uma_zone_t tcptw_zone; 181static int maxtcptw; 182static int 183sysctl_maxtcptw(SYSCTL_HANDLER_ARGS) 184{ 185 int error, new; 186 187 if (maxtcptw == 0) 188 new = maxsockets / 5; 189 else 190 new = maxtcptw; 191 error = sysctl_handle_int(oidp, &new, sizeof(int), req); 192 if (error == 0 && req->newptr) { 193 if (new > maxtcptw) { 194 maxtcptw = new; 195 uma_zone_set_max(tcptw_zone, maxtcptw); 196 } else 197 error = EINVAL; 198 } 199 return (error); 200} 201SYSCTL_PROC(_net_inet_tcp, OID_AUTO, maxtcptw, CTLTYPE_INT|CTLFLAG_RW, 202 &maxtcptw, 0, sysctl_maxtcptw, "IU", 203 "Maximum number of compressed TCP TIME_WAIT entries"); 204 205/* 206 * TCP bandwidth limiting sysctls. Note that the default lower bound of 207 * 1024 exists only for debugging. A good production default would be 208 * something like 6100. 209 */ 210SYSCTL_NODE(_net_inet_tcp, OID_AUTO, inflight, CTLFLAG_RW, 0, 211 "TCP inflight data limiting"); 212 213static int tcp_inflight_enable = 1; 214SYSCTL_INT(_net_inet_tcp_inflight, OID_AUTO, enable, CTLFLAG_RW, 215 &tcp_inflight_enable, 0, "Enable automatic TCP inflight data limiting"); 216 217static int tcp_inflight_debug = 0; 218SYSCTL_INT(_net_inet_tcp_inflight, OID_AUTO, debug, CTLFLAG_RW, 219 &tcp_inflight_debug, 0, "Debug TCP inflight calculations"); 220 221static int tcp_inflight_rttthresh; 222SYSCTL_PROC(_net_inet_tcp_inflight, OID_AUTO, rttthresh, CTLTYPE_INT|CTLFLAG_RW, 223 &tcp_inflight_rttthresh, 0, sysctl_msec_to_ticks, "I", 224 "RTT threshold below which inflight will deactivate itself"); 225 226static int tcp_inflight_min = 6144; 227SYSCTL_INT(_net_inet_tcp_inflight, OID_AUTO, min, CTLFLAG_RW, 228 &tcp_inflight_min, 0, "Lower-bound for TCP inflight window"); 229 230static int tcp_inflight_max = TCP_MAXWIN << TCP_MAX_WINSHIFT; 231SYSCTL_INT(_net_inet_tcp_inflight, OID_AUTO, max, CTLFLAG_RW, 232 &tcp_inflight_max, 0, "Upper-bound for TCP inflight window"); 233 234static int tcp_inflight_stab = 20; 235SYSCTL_INT(_net_inet_tcp_inflight, OID_AUTO, stab, CTLFLAG_RW, 236 &tcp_inflight_stab, 0, "Inflight Algorithm Stabilization 20 = 2 packets"); 237 238uma_zone_t sack_hole_zone; 239 240static struct inpcb *tcp_notify(struct inpcb *, int); 241static void tcp_isn_tick(void *); 242 243/* 244 * Target size of TCP PCB hash tables. Must be a power of two. 245 * 246 * Note that this can be overridden by the kernel environment 247 * variable net.inet.tcp.tcbhashsize 248 */ 249#ifndef TCBHASHSIZE 250#define TCBHASHSIZE 512 251#endif 252 253/* 254 * XXX 255 * Callouts should be moved into struct tcp directly. They are currently 256 * separate because the tcpcb structure is exported to userland for sysctl 257 * parsing purposes, which do not know about callouts. 258 */ 259struct tcpcb_mem { 260 struct tcpcb tcb; 261 struct callout tcpcb_mem_rexmt, tcpcb_mem_persist, tcpcb_mem_keep; 262 struct callout tcpcb_mem_2msl, tcpcb_mem_delack; 263}; 264 265static uma_zone_t tcpcb_zone; 266struct callout isn_callout; 267static struct mtx isn_mtx; 268 269#define ISN_LOCK_INIT() mtx_init(&isn_mtx, "isn_mtx", NULL, MTX_DEF) 270#define ISN_LOCK() mtx_lock(&isn_mtx) 271#define ISN_UNLOCK() mtx_unlock(&isn_mtx) 272 273/* 274 * TCP initialization. 275 */ 276static void 277tcp_zone_change(void *tag) 278{ 279 280 uma_zone_set_max(tcbinfo.ipi_zone, maxsockets); 281 uma_zone_set_max(tcpcb_zone, maxsockets); 282 if (maxtcptw == 0) 283 uma_zone_set_max(tcptw_zone, maxsockets / 5); 284} 285 286static int 287tcp_inpcb_init(void *mem, int size, int flags) 288{ 289 struct inpcb *inp = (struct inpcb *) mem; 290 INP_LOCK_INIT(inp, "inp", "tcpinp"); 291 return (0); 292} 293 294void 295tcp_init(void) 296{ 297 int hashsize = TCBHASHSIZE; 298 299 tcp_delacktime = TCPTV_DELACK; 300 tcp_keepinit = TCPTV_KEEP_INIT; 301 tcp_keepidle = TCPTV_KEEP_IDLE; 302 tcp_keepintvl = TCPTV_KEEPINTVL; 303 tcp_maxpersistidle = TCPTV_KEEP_IDLE; 304 tcp_msl = TCPTV_MSL; 305 tcp_rexmit_min = TCPTV_MIN; 306 tcp_rexmit_slop = TCPTV_CPU_VAR; 307 tcp_inflight_rttthresh = TCPTV_INFLIGHT_RTTTHRESH; 308 309 INP_INFO_LOCK_INIT(&tcbinfo, "tcp"); 310 LIST_INIT(&tcb); 311 tcbinfo.listhead = &tcb; 312 TUNABLE_INT_FETCH("net.inet.tcp.tcbhashsize", &hashsize); 313 if (!powerof2(hashsize)) { 314 printf("WARNING: TCB hash size not a power of 2\n"); 315 hashsize = 512; /* safe default */ 316 } 317 tcp_tcbhashsize = hashsize; 318 tcbinfo.hashbase = hashinit(hashsize, M_PCB, &tcbinfo.hashmask); 319 tcbinfo.porthashbase = hashinit(hashsize, M_PCB, 320 &tcbinfo.porthashmask); 321 tcbinfo.ipi_zone = uma_zcreate("inpcb", sizeof(struct inpcb), 322 NULL, NULL, tcp_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 323 uma_zone_set_max(tcbinfo.ipi_zone, maxsockets); 324#ifdef INET6 325#define TCP_MINPROTOHDR (sizeof(struct ip6_hdr) + sizeof(struct tcphdr)) 326#else /* INET6 */ 327#define TCP_MINPROTOHDR (sizeof(struct tcpiphdr)) 328#endif /* INET6 */ 329 if (max_protohdr < TCP_MINPROTOHDR) 330 max_protohdr = TCP_MINPROTOHDR; 331 if (max_linkhdr + TCP_MINPROTOHDR > MHLEN) 332 panic("tcp_init"); 333#undef TCP_MINPROTOHDR 334 /* 335 * These have to be type stable for the benefit of the timers. 336 */ 337 tcpcb_zone = uma_zcreate("tcpcb", sizeof(struct tcpcb_mem), 338 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 339 uma_zone_set_max(tcpcb_zone, maxsockets); 340 tcptw_zone = uma_zcreate("tcptw", sizeof(struct tcptw), 341 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 342 TUNABLE_INT_FETCH("net.inet.tcp.maxtcptw", &maxtcptw); 343 if (maxtcptw == 0) 344 uma_zone_set_max(tcptw_zone, maxsockets / 5); 345 else 346 uma_zone_set_max(tcptw_zone, maxtcptw); 347 tcp_timer_init(); 348 syncache_init(); 349 tcp_hc_init(); 350 tcp_reass_init(); 351 ISN_LOCK_INIT(); 352 callout_init(&isn_callout, CALLOUT_MPSAFE); 353 tcp_isn_tick(NULL); 354 EVENTHANDLER_REGISTER(shutdown_pre_sync, tcp_fini, NULL, 355 SHUTDOWN_PRI_DEFAULT); 356 sack_hole_zone = uma_zcreate("sackhole", sizeof(struct sackhole), 357 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 358 EVENTHANDLER_REGISTER(maxsockets_change, tcp_zone_change, NULL, 359 EVENTHANDLER_PRI_ANY); 360} 361 362void 363tcp_fini(void *xtp) 364{ 365 366 callout_stop(&isn_callout); 367} 368 369/* 370 * Fill in the IP and TCP headers for an outgoing packet, given the tcpcb. 371 * tcp_template used to store this data in mbufs, but we now recopy it out 372 * of the tcpcb each time to conserve mbufs. 373 */ 374void 375tcpip_fillheaders(struct inpcb *inp, void *ip_ptr, void *tcp_ptr) 376{ 377 struct tcphdr *th = (struct tcphdr *)tcp_ptr; 378 379 INP_LOCK_ASSERT(inp); 380 381#ifdef INET6 382 if ((inp->inp_vflag & INP_IPV6) != 0) { 383 struct ip6_hdr *ip6; 384 385 ip6 = (struct ip6_hdr *)ip_ptr; 386 ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) | 387 (inp->in6p_flowinfo & IPV6_FLOWINFO_MASK); 388 ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) | 389 (IPV6_VERSION & IPV6_VERSION_MASK); 390 ip6->ip6_nxt = IPPROTO_TCP; 391 ip6->ip6_plen = sizeof(struct tcphdr); 392 ip6->ip6_src = inp->in6p_laddr; 393 ip6->ip6_dst = inp->in6p_faddr; 394 } else 395#endif 396 { 397 struct ip *ip; 398 399 ip = (struct ip *)ip_ptr; 400 ip->ip_v = IPVERSION; 401 ip->ip_hl = 5; 402 ip->ip_tos = inp->inp_ip_tos; 403 ip->ip_len = 0; 404 ip->ip_id = 0; 405 ip->ip_off = 0; 406 ip->ip_ttl = inp->inp_ip_ttl; 407 ip->ip_sum = 0; 408 ip->ip_p = IPPROTO_TCP; 409 ip->ip_src = inp->inp_laddr; 410 ip->ip_dst = inp->inp_faddr; 411 } 412 th->th_sport = inp->inp_lport; 413 th->th_dport = inp->inp_fport; 414 th->th_seq = 0; 415 th->th_ack = 0; 416 th->th_x2 = 0; 417 th->th_off = 5; 418 th->th_flags = 0; 419 th->th_win = 0; 420 th->th_urp = 0; 421 th->th_sum = 0; /* in_pseudo() is called later for ipv4 */ 422} 423 424/* 425 * Create template to be used to send tcp packets on a connection. 426 * Allocates an mbuf and fills in a skeletal tcp/ip header. The only 427 * use for this function is in keepalives, which use tcp_respond. 428 */ 429struct tcptemp * 430tcpip_maketemplate(struct inpcb *inp) 431{ 432 struct mbuf *m; 433 struct tcptemp *n; 434 435 m = m_get(M_DONTWAIT, MT_DATA); 436 if (m == NULL) 437 return (0); 438 m->m_len = sizeof(struct tcptemp); 439 n = mtod(m, struct tcptemp *); 440 441 tcpip_fillheaders(inp, (void *)&n->tt_ipgen, (void *)&n->tt_t); 442 return (n); 443} 444 445/* 446 * Send a single message to the TCP at address specified by 447 * the given TCP/IP header. If m == NULL, then we make a copy 448 * of the tcpiphdr at ti and send directly to the addressed host. 449 * This is used to force keep alive messages out using the TCP 450 * template for a connection. If flags are given then we send 451 * a message back to the TCP which originated the * segment ti, 452 * and discard the mbuf containing it and any other attached mbufs. 453 * 454 * In any case the ack and sequence number of the transmitted 455 * segment are as specified by the parameters. 456 * 457 * NOTE: If m != NULL, then ti must point to *inside* the mbuf. 458 */ 459void 460tcp_respond(struct tcpcb *tp, void *ipgen, register struct tcphdr *th, 461 register struct mbuf *m, tcp_seq ack, tcp_seq seq, int flags) 462{ 463 register int tlen; 464 int win = 0; 465 struct ip *ip; 466 struct tcphdr *nth; 467#ifdef INET6 468 struct ip6_hdr *ip6; 469 int isipv6; 470#endif /* INET6 */ 471 int ipflags = 0; 472 struct inpcb *inp; 473 474 KASSERT(tp != NULL || m != NULL, ("tcp_respond: tp and m both NULL")); 475 476#ifdef INET6 477 isipv6 = ((struct ip *)ipgen)->ip_v == 6; 478 ip6 = ipgen; 479#endif /* INET6 */ 480 ip = ipgen; 481 482 if (tp != NULL) { 483 inp = tp->t_inpcb; 484 KASSERT(inp != NULL, ("tcp control block w/o inpcb")); 485 INP_INFO_WLOCK_ASSERT(&tcbinfo); 486 INP_LOCK_ASSERT(inp); 487 } else 488 inp = NULL; 489 490 if (tp != NULL) { 491 if (!(flags & TH_RST)) { 492 win = sbspace(&inp->inp_socket->so_rcv); 493 if (win > (long)TCP_MAXWIN << tp->rcv_scale) 494 win = (long)TCP_MAXWIN << tp->rcv_scale; 495 } 496 } 497 if (m == NULL) { 498 m = m_gethdr(M_DONTWAIT, MT_DATA); 499 if (m == NULL) 500 return; 501 tlen = 0; 502 m->m_data += max_linkhdr; 503#ifdef INET6 504 if (isipv6) { 505 bcopy((caddr_t)ip6, mtod(m, caddr_t), 506 sizeof(struct ip6_hdr)); 507 ip6 = mtod(m, struct ip6_hdr *); 508 nth = (struct tcphdr *)(ip6 + 1); 509 } else 510#endif /* INET6 */ 511 { 512 bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip)); 513 ip = mtod(m, struct ip *); 514 nth = (struct tcphdr *)(ip + 1); 515 } 516 bcopy((caddr_t)th, (caddr_t)nth, sizeof(struct tcphdr)); 517 flags = TH_ACK; 518 } else { 519 m_freem(m->m_next); 520 m->m_next = NULL; 521 m->m_data = (caddr_t)ipgen; 522 /* m_len is set later */ 523 tlen = 0; 524#define xchg(a,b,type) { type t; t=a; a=b; b=t; } 525#ifdef INET6 526 if (isipv6) { 527 xchg(ip6->ip6_dst, ip6->ip6_src, struct in6_addr); 528 nth = (struct tcphdr *)(ip6 + 1); 529 } else 530#endif /* INET6 */ 531 { 532 xchg(ip->ip_dst.s_addr, ip->ip_src.s_addr, n_long); 533 nth = (struct tcphdr *)(ip + 1); 534 } 535 if (th != nth) { 536 /* 537 * this is usually a case when an extension header 538 * exists between the IPv6 header and the 539 * TCP header. 540 */ 541 nth->th_sport = th->th_sport; 542 nth->th_dport = th->th_dport; 543 } 544 xchg(nth->th_dport, nth->th_sport, n_short); 545#undef xchg 546 } 547#ifdef INET6 548 if (isipv6) { 549 ip6->ip6_flow = 0; 550 ip6->ip6_vfc = IPV6_VERSION; 551 ip6->ip6_nxt = IPPROTO_TCP; 552 ip6->ip6_plen = htons((u_short)(sizeof (struct tcphdr) + 553 tlen)); 554 tlen += sizeof (struct ip6_hdr) + sizeof (struct tcphdr); 555 } else 556#endif 557 { 558 tlen += sizeof (struct tcpiphdr); 559 ip->ip_len = tlen; 560 ip->ip_ttl = ip_defttl; 561 if (path_mtu_discovery) 562 ip->ip_off |= IP_DF; 563 } 564 m->m_len = tlen; 565 m->m_pkthdr.len = tlen; 566 m->m_pkthdr.rcvif = NULL; 567#ifdef MAC 568 if (inp != NULL) { 569 /* 570 * Packet is associated with a socket, so allow the 571 * label of the response to reflect the socket label. 572 */ 573 INP_LOCK_ASSERT(inp); 574 mac_create_mbuf_from_inpcb(inp, m); 575 } else { 576 /* 577 * Packet is not associated with a socket, so possibly 578 * update the label in place. 579 */ 580 mac_reflect_mbuf_tcp(m); 581 } 582#endif 583 nth->th_seq = htonl(seq); 584 nth->th_ack = htonl(ack); 585 nth->th_x2 = 0; 586 nth->th_off = sizeof (struct tcphdr) >> 2; 587 nth->th_flags = flags; 588 if (tp != NULL) 589 nth->th_win = htons((u_short) (win >> tp->rcv_scale)); 590 else 591 nth->th_win = htons((u_short)win); 592 nth->th_urp = 0; 593#ifdef INET6 594 if (isipv6) { 595 nth->th_sum = 0; 596 nth->th_sum = in6_cksum(m, IPPROTO_TCP, 597 sizeof(struct ip6_hdr), 598 tlen - sizeof(struct ip6_hdr)); 599 ip6->ip6_hlim = in6_selecthlim(tp != NULL ? tp->t_inpcb : 600 NULL, NULL); 601 } else 602#endif /* INET6 */ 603 { 604 nth->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 605 htons((u_short)(tlen - sizeof(struct ip) + ip->ip_p))); 606 m->m_pkthdr.csum_flags = CSUM_TCP; 607 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 608 } 609#ifdef TCPDEBUG 610 if (tp == NULL || (inp->inp_socket->so_options & SO_DEBUG)) 611 tcp_trace(TA_OUTPUT, 0, tp, mtod(m, void *), th, 0); 612#endif 613#ifdef INET6 614 if (isipv6) 615 (void) ip6_output(m, NULL, NULL, ipflags, NULL, NULL, inp); 616 else 617#endif /* INET6 */ 618 (void) ip_output(m, NULL, NULL, ipflags, NULL, inp); 619} 620 621/* 622 * Create a new TCP control block, making an 623 * empty reassembly queue and hooking it to the argument 624 * protocol control block. The `inp' parameter must have 625 * come from the zone allocator set up in tcp_init(). 626 */ 627struct tcpcb * 628tcp_newtcpcb(struct inpcb *inp) 629{ 630 struct tcpcb_mem *tm; 631 struct tcpcb *tp; 632#ifdef INET6 633 int isipv6 = (inp->inp_vflag & INP_IPV6) != 0; 634#endif /* INET6 */ 635 636 tm = uma_zalloc(tcpcb_zone, M_NOWAIT | M_ZERO); 637 if (tm == NULL) 638 return (NULL); 639 tp = &tm->tcb; 640 /* LIST_INIT(&tp->t_segq); */ /* XXX covered by M_ZERO */ 641 tp->t_maxseg = tp->t_maxopd = 642#ifdef INET6 643 isipv6 ? tcp_v6mssdflt : 644#endif /* INET6 */ 645 tcp_mssdflt; 646 647 /* Set up our timeouts. */ 648 callout_init(tp->tt_rexmt = &tm->tcpcb_mem_rexmt, NET_CALLOUT_MPSAFE); 649 callout_init(tp->tt_persist = &tm->tcpcb_mem_persist, NET_CALLOUT_MPSAFE); 650 callout_init(tp->tt_keep = &tm->tcpcb_mem_keep, NET_CALLOUT_MPSAFE); 651 callout_init(tp->tt_2msl = &tm->tcpcb_mem_2msl, NET_CALLOUT_MPSAFE); 652 callout_init(tp->tt_delack = &tm->tcpcb_mem_delack, NET_CALLOUT_MPSAFE); 653 654 if (tcp_do_rfc1323) 655 tp->t_flags = (TF_REQ_SCALE|TF_REQ_TSTMP); 656 tp->sack_enable = tcp_do_sack; 657 TAILQ_INIT(&tp->snd_holes); 658 tp->t_inpcb = inp; /* XXX */ 659 /* 660 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no 661 * rtt estimate. Set rttvar so that srtt + 4 * rttvar gives 662 * reasonable initial retransmit time. 663 */ 664 tp->t_srtt = TCPTV_SRTTBASE; 665 tp->t_rttvar = ((TCPTV_RTOBASE - TCPTV_SRTTBASE) << TCP_RTTVAR_SHIFT) / 4; 666 tp->t_rttmin = tcp_rexmit_min; 667 tp->t_rxtcur = TCPTV_RTOBASE; 668 tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT; 669 tp->snd_bwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT; 670 tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT; 671 tp->t_rcvtime = ticks; 672 tp->t_bw_rtttime = ticks; 673 /* 674 * IPv4 TTL initialization is necessary for an IPv6 socket as well, 675 * because the socket may be bound to an IPv6 wildcard address, 676 * which may match an IPv4-mapped IPv6 address. 677 */ 678 inp->inp_ip_ttl = ip_defttl; 679 inp->inp_ppcb = tp; 680 return (tp); /* XXX */ 681} 682 683/* 684 * Drop a TCP connection, reporting 685 * the specified error. If connection is synchronized, 686 * then send a RST to peer. 687 */ 688struct tcpcb * 689tcp_drop(struct tcpcb *tp, int errno) 690{ 691 struct socket *so = tp->t_inpcb->inp_socket; 692 693 INP_INFO_WLOCK_ASSERT(&tcbinfo); 694 INP_LOCK_ASSERT(tp->t_inpcb); 695 696 if (TCPS_HAVERCVDSYN(tp->t_state)) { 697 tp->t_state = TCPS_CLOSED; 698 (void) tcp_output(tp); 699 tcpstat.tcps_drops++; 700 } else 701 tcpstat.tcps_conndrops++; 702 if (errno == ETIMEDOUT && tp->t_softerror) 703 errno = tp->t_softerror; 704 so->so_error = errno; 705 return (tcp_close(tp)); 706} 707 708void 709tcp_discardcb(struct tcpcb *tp) 710{ 711 struct tseg_qent *q; 712 struct inpcb *inp = tp->t_inpcb; 713 struct socket *so = inp->inp_socket; 714#ifdef INET6 715 int isipv6 = (inp->inp_vflag & INP_IPV6) != 0; 716#endif /* INET6 */ 717 718 INP_LOCK_ASSERT(inp); 719 720 /* 721 * Make sure that all of our timers are stopped before we 722 * delete the PCB. 723 */ 724 callout_stop(tp->tt_rexmt); 725 callout_stop(tp->tt_persist); 726 callout_stop(tp->tt_keep); 727 callout_stop(tp->tt_2msl); 728 callout_stop(tp->tt_delack); 729 730 /* 731 * If we got enough samples through the srtt filter, 732 * save the rtt and rttvar in the routing entry. 733 * 'Enough' is arbitrarily defined as 4 rtt samples. 734 * 4 samples is enough for the srtt filter to converge 735 * to within enough % of the correct value; fewer samples 736 * and we could save a bogus rtt. The danger is not high 737 * as tcp quickly recovers from everything. 738 * XXX: Works very well but needs some more statistics! 739 */ 740 if (tp->t_rttupdated >= 4) { 741 struct hc_metrics_lite metrics; 742 u_long ssthresh; 743 744 bzero(&metrics, sizeof(metrics)); 745 /* 746 * Update the ssthresh always when the conditions below 747 * are satisfied. This gives us better new start value 748 * for the congestion avoidance for new connections. 749 * ssthresh is only set if packet loss occured on a session. 750 * 751 * XXXRW: 'so' may be NULL here, and/or socket buffer may be 752 * being torn down. Ideally this code would not use 'so'. 753 */ 754 ssthresh = tp->snd_ssthresh; 755 if (ssthresh != 0 && ssthresh < so->so_snd.sb_hiwat / 2) { 756 /* 757 * convert the limit from user data bytes to 758 * packets then to packet data bytes. 759 */ 760 ssthresh = (ssthresh + tp->t_maxseg / 2) / tp->t_maxseg; 761 if (ssthresh < 2) 762 ssthresh = 2; 763 ssthresh *= (u_long)(tp->t_maxseg + 764#ifdef INET6 765 (isipv6 ? sizeof (struct ip6_hdr) + 766 sizeof (struct tcphdr) : 767#endif 768 sizeof (struct tcpiphdr) 769#ifdef INET6 770 ) 771#endif 772 ); 773 } else 774 ssthresh = 0; 775 metrics.rmx_ssthresh = ssthresh; 776 777 metrics.rmx_rtt = tp->t_srtt; 778 metrics.rmx_rttvar = tp->t_rttvar; 779 /* XXX: This wraps if the pipe is more than 4 Gbit per second */ 780 metrics.rmx_bandwidth = tp->snd_bandwidth; 781 metrics.rmx_cwnd = tp->snd_cwnd; 782 metrics.rmx_sendpipe = 0; 783 metrics.rmx_recvpipe = 0; 784 785 tcp_hc_update(&inp->inp_inc, &metrics); 786 } 787 788 /* free the reassembly queue, if any */ 789 while ((q = LIST_FIRST(&tp->t_segq)) != NULL) { 790 LIST_REMOVE(q, tqe_q); 791 m_freem(q->tqe_m); 792 uma_zfree(tcp_reass_zone, q); 793 tp->t_segqlen--; 794 tcp_reass_qsize--; 795 } 796 tcp_free_sackholes(tp); 797 inp->inp_ppcb = NULL; 798 tp->t_inpcb = NULL; 799 uma_zfree(tcpcb_zone, tp); 800} 801 802/* 803 * Attempt to close a TCP control block, marking it as dropped, and freeing 804 * the socket if we hold the only reference. 805 */ 806struct tcpcb * 807tcp_close(struct tcpcb *tp) 808{ 809 struct inpcb *inp = tp->t_inpcb; 810 struct socket *so; 811 812 INP_INFO_WLOCK_ASSERT(&tcbinfo); 813 INP_LOCK_ASSERT(inp); 814 815 in_pcbdrop(inp); 816 tcpstat.tcps_closed++; 817 KASSERT(inp->inp_socket != NULL, ("tcp_close: inp_socket NULL")); 818 so = inp->inp_socket; 819 soisdisconnected(so); 820 if (inp->inp_vflag & INP_SOCKREF) { 821 KASSERT(so->so_state & SS_PROTOREF, 822 ("tcp_close: !SS_PROTOREF")); 823 inp->inp_vflag &= ~INP_SOCKREF; 824 INP_UNLOCK(inp); 825 ACCEPT_LOCK(); 826 SOCK_LOCK(so); 827 so->so_state &= ~SS_PROTOREF; 828 sofree(so); 829 return (NULL); 830 } 831 return (tp); 832} 833 834void 835tcp_drain(void) 836{ 837 838 if (do_tcpdrain) { 839 struct inpcb *inpb; 840 struct tcpcb *tcpb; 841 struct tseg_qent *te; 842 843 /* 844 * Walk the tcpbs, if existing, and flush the reassembly queue, 845 * if there is one... 846 * XXX: The "Net/3" implementation doesn't imply that the TCP 847 * reassembly queue should be flushed, but in a situation 848 * where we're really low on mbufs, this is potentially 849 * usefull. 850 */ 851 INP_INFO_RLOCK(&tcbinfo); 852 LIST_FOREACH(inpb, tcbinfo.listhead, inp_list) { 853 if (inpb->inp_vflag & INP_TIMEWAIT) 854 continue; 855 INP_LOCK(inpb); 856 if ((tcpb = intotcpcb(inpb)) != NULL) { 857 while ((te = LIST_FIRST(&tcpb->t_segq)) 858 != NULL) { 859 LIST_REMOVE(te, tqe_q); 860 m_freem(te->tqe_m); 861 uma_zfree(tcp_reass_zone, te); 862 tcpb->t_segqlen--; 863 tcp_reass_qsize--; 864 } 865 tcp_clean_sackreport(tcpb); 866 } 867 INP_UNLOCK(inpb); 868 } 869 INP_INFO_RUNLOCK(&tcbinfo); 870 } 871} 872 873/* 874 * Notify a tcp user of an asynchronous error; 875 * store error as soft error, but wake up user 876 * (for now, won't do anything until can select for soft error). 877 * 878 * Do not wake up user since there currently is no mechanism for 879 * reporting soft errors (yet - a kqueue filter may be added). 880 */ 881static struct inpcb * 882tcp_notify(struct inpcb *inp, int error) 883{ 884 struct tcpcb *tp; 885 886 INP_INFO_WLOCK_ASSERT(&tcbinfo); 887 INP_LOCK_ASSERT(inp); 888 889 if ((inp->inp_vflag & INP_TIMEWAIT) || 890 (inp->inp_vflag & INP_DROPPED)) 891 return (inp); 892 893 tp = intotcpcb(inp); 894 KASSERT(tp != NULL, ("tcp_notify: tp == NULL")); 895 896 /* 897 * Ignore some errors if we are hooked up. 898 * If connection hasn't completed, has retransmitted several times, 899 * and receives a second error, give up now. This is better 900 * than waiting a long time to establish a connection that 901 * can never complete. 902 */ 903 if (tp->t_state == TCPS_ESTABLISHED && 904 (error == EHOSTUNREACH || error == ENETUNREACH || 905 error == EHOSTDOWN)) { 906 return (inp); 907 } else if (tp->t_state < TCPS_ESTABLISHED && tp->t_rxtshift > 3 && 908 tp->t_softerror) { 909 tp = tcp_drop(tp, error); 910 if (tp != NULL) 911 return (inp); 912 else 913 return (NULL); 914 } else { 915 tp->t_softerror = error; 916 return (inp); 917 } 918#if 0 919 wakeup( &so->so_timeo); 920 sorwakeup(so); 921 sowwakeup(so); 922#endif 923} 924 925static int 926tcp_pcblist(SYSCTL_HANDLER_ARGS) 927{ 928 int error, i, n; 929 struct inpcb *inp, **inp_list; 930 inp_gen_t gencnt; 931 struct xinpgen xig; 932 933 /* 934 * The process of preparing the TCB list is too time-consuming and 935 * resource-intensive to repeat twice on every request. 936 */ 937 if (req->oldptr == NULL) { 938 n = tcbinfo.ipi_count; 939 req->oldidx = 2 * (sizeof xig) 940 + (n + n/8) * sizeof(struct xtcpcb); 941 return (0); 942 } 943 944 if (req->newptr != NULL) 945 return (EPERM); 946 947 /* 948 * OK, now we're committed to doing something. 949 */ 950 INP_INFO_RLOCK(&tcbinfo); 951 gencnt = tcbinfo.ipi_gencnt; 952 n = tcbinfo.ipi_count; 953 INP_INFO_RUNLOCK(&tcbinfo); 954 955 error = sysctl_wire_old_buffer(req, 2 * (sizeof xig) 956 + n * sizeof(struct xtcpcb)); 957 if (error != 0) 958 return (error); 959 960 xig.xig_len = sizeof xig; 961 xig.xig_count = n; 962 xig.xig_gen = gencnt; 963 xig.xig_sogen = so_gencnt; 964 error = SYSCTL_OUT(req, &xig, sizeof xig); 965 if (error) 966 return (error); 967 968 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 969 if (inp_list == NULL) 970 return (ENOMEM); 971 972 INP_INFO_RLOCK(&tcbinfo); 973 for (inp = LIST_FIRST(tcbinfo.listhead), i = 0; inp != NULL && i < n; 974 inp = LIST_NEXT(inp, inp_list)) { 975 INP_LOCK(inp); 976 if (inp->inp_gencnt <= gencnt) { 977 /* 978 * XXX: This use of cr_cansee(), introduced with 979 * TCP state changes, is not quite right, but for 980 * now, better than nothing. 981 */ 982 if (inp->inp_vflag & INP_TIMEWAIT) { 983 if (intotw(inp) != NULL) 984 error = cr_cansee(req->td->td_ucred, 985 intotw(inp)->tw_cred); 986 else 987 error = EINVAL; /* Skip this inp. */ 988 } else 989 error = cr_canseesocket(req->td->td_ucred, 990 inp->inp_socket); 991 if (error == 0) 992 inp_list[i++] = inp; 993 } 994 INP_UNLOCK(inp); 995 } 996 INP_INFO_RUNLOCK(&tcbinfo); 997 n = i; 998 999 error = 0; 1000 for (i = 0; i < n; i++) { 1001 inp = inp_list[i]; 1002 INP_LOCK(inp); 1003 if (inp->inp_gencnt <= gencnt) { 1004 struct xtcpcb xt; 1005 void *inp_ppcb; 1006 1007 bzero(&xt, sizeof(xt)); 1008 xt.xt_len = sizeof xt; 1009 /* XXX should avoid extra copy */ 1010 bcopy(inp, &xt.xt_inp, sizeof *inp); 1011 inp_ppcb = inp->inp_ppcb; 1012 if (inp_ppcb == NULL) 1013 bzero((char *) &xt.xt_tp, sizeof xt.xt_tp); 1014 else if (inp->inp_vflag & INP_TIMEWAIT) { 1015 bzero((char *) &xt.xt_tp, sizeof xt.xt_tp); 1016 xt.xt_tp.t_state = TCPS_TIME_WAIT; 1017 } else 1018 bcopy(inp_ppcb, &xt.xt_tp, sizeof xt.xt_tp); 1019 if (inp->inp_socket != NULL) 1020 sotoxsocket(inp->inp_socket, &xt.xt_socket); 1021 else { 1022 bzero(&xt.xt_socket, sizeof xt.xt_socket); 1023 xt.xt_socket.xso_protocol = IPPROTO_TCP; 1024 } 1025 xt.xt_inp.inp_gencnt = inp->inp_gencnt; 1026 INP_UNLOCK(inp); 1027 error = SYSCTL_OUT(req, &xt, sizeof xt); 1028 } else 1029 INP_UNLOCK(inp); 1030 1031 } 1032 if (!error) { 1033 /* 1034 * Give the user an updated idea of our state. 1035 * If the generation differs from what we told 1036 * her before, she knows that something happened 1037 * while we were processing this request, and it 1038 * might be necessary to retry. 1039 */ 1040 INP_INFO_RLOCK(&tcbinfo); 1041 xig.xig_gen = tcbinfo.ipi_gencnt; 1042 xig.xig_sogen = so_gencnt; 1043 xig.xig_count = tcbinfo.ipi_count; 1044 INP_INFO_RUNLOCK(&tcbinfo); 1045 error = SYSCTL_OUT(req, &xig, sizeof xig); 1046 } 1047 free(inp_list, M_TEMP); 1048 return (error); 1049} 1050 1051SYSCTL_PROC(_net_inet_tcp, TCPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0, 1052 tcp_pcblist, "S,xtcpcb", "List of active TCP connections"); 1053 1054static int 1055tcp_getcred(SYSCTL_HANDLER_ARGS) 1056{ 1057 struct xucred xuc; 1058 struct sockaddr_in addrs[2]; 1059 struct inpcb *inp; 1060 int error; 1061 1062 error = suser_cred(req->td->td_ucred, SUSER_ALLOWJAIL); 1063 if (error) 1064 return (error); 1065 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 1066 if (error) 1067 return (error); 1068 INP_INFO_RLOCK(&tcbinfo); 1069 inp = in_pcblookup_hash(&tcbinfo, addrs[1].sin_addr, addrs[1].sin_port, 1070 addrs[0].sin_addr, addrs[0].sin_port, 0, NULL); 1071 if (inp == NULL) { 1072 error = ENOENT; 1073 goto outunlocked; 1074 } 1075 INP_LOCK(inp); 1076 if (inp->inp_socket == NULL) { 1077 error = ENOENT; 1078 goto out; 1079 } 1080 error = cr_canseesocket(req->td->td_ucred, inp->inp_socket); 1081 if (error) 1082 goto out; 1083 cru2x(inp->inp_socket->so_cred, &xuc); 1084out: 1085 INP_UNLOCK(inp); 1086outunlocked: 1087 INP_INFO_RUNLOCK(&tcbinfo); 1088 if (error == 0) 1089 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 1090 return (error); 1091} 1092 1093SYSCTL_PROC(_net_inet_tcp, OID_AUTO, getcred, 1094 CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0, 1095 tcp_getcred, "S,xucred", "Get the xucred of a TCP connection"); 1096 1097#ifdef INET6 1098static int 1099tcp6_getcred(SYSCTL_HANDLER_ARGS) 1100{ 1101 struct xucred xuc; 1102 struct sockaddr_in6 addrs[2]; 1103 struct inpcb *inp; 1104 int error, mapped = 0; 1105 1106 error = suser_cred(req->td->td_ucred, SUSER_ALLOWJAIL); 1107 if (error) 1108 return (error); 1109 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 1110 if (error) 1111 return (error); 1112 if ((error = sa6_embedscope(&addrs[0], ip6_use_defzone)) != 0 || 1113 (error = sa6_embedscope(&addrs[1], ip6_use_defzone)) != 0) { 1114 return (error); 1115 } 1116 if (IN6_IS_ADDR_V4MAPPED(&addrs[0].sin6_addr)) { 1117 if (IN6_IS_ADDR_V4MAPPED(&addrs[1].sin6_addr)) 1118 mapped = 1; 1119 else 1120 return (EINVAL); 1121 } 1122 1123 INP_INFO_RLOCK(&tcbinfo); 1124 if (mapped == 1) 1125 inp = in_pcblookup_hash(&tcbinfo, 1126 *(struct in_addr *)&addrs[1].sin6_addr.s6_addr[12], 1127 addrs[1].sin6_port, 1128 *(struct in_addr *)&addrs[0].sin6_addr.s6_addr[12], 1129 addrs[0].sin6_port, 1130 0, NULL); 1131 else 1132 inp = in6_pcblookup_hash(&tcbinfo, 1133 &addrs[1].sin6_addr, addrs[1].sin6_port, 1134 &addrs[0].sin6_addr, addrs[0].sin6_port, 0, NULL); 1135 if (inp == NULL) { 1136 error = ENOENT; 1137 goto outunlocked; 1138 } 1139 INP_LOCK(inp); 1140 if (inp->inp_socket == NULL) { 1141 error = ENOENT; 1142 goto out; 1143 } 1144 error = cr_canseesocket(req->td->td_ucred, inp->inp_socket); 1145 if (error) 1146 goto out; 1147 cru2x(inp->inp_socket->so_cred, &xuc); 1148out: 1149 INP_UNLOCK(inp); 1150outunlocked: 1151 INP_INFO_RUNLOCK(&tcbinfo); 1152 if (error == 0) 1153 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 1154 return (error); 1155} 1156 1157SYSCTL_PROC(_net_inet6_tcp6, OID_AUTO, getcred, 1158 CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0, 1159 tcp6_getcred, "S,xucred", "Get the xucred of a TCP6 connection"); 1160#endif 1161 1162 1163void 1164tcp_ctlinput(int cmd, struct sockaddr *sa, void *vip) 1165{ 1166 struct ip *ip = vip; 1167 struct tcphdr *th; 1168 struct in_addr faddr; 1169 struct inpcb *inp; 1170 struct tcpcb *tp; 1171 struct inpcb *(*notify)(struct inpcb *, int) = tcp_notify; 1172 struct icmp *icp; 1173 struct in_conninfo inc; 1174 tcp_seq icmp_tcp_seq; 1175 int mtu; 1176 1177 faddr = ((struct sockaddr_in *)sa)->sin_addr; 1178 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 1179 return; 1180 1181 if (cmd == PRC_MSGSIZE) 1182 notify = tcp_mtudisc; 1183 else if (icmp_may_rst && (cmd == PRC_UNREACH_ADMIN_PROHIB || 1184 cmd == PRC_UNREACH_PORT || cmd == PRC_TIMXCEED_INTRANS) && ip) 1185 notify = tcp_drop_syn_sent; 1186 /* 1187 * Redirects don't need to be handled up here. 1188 */ 1189 else if (PRC_IS_REDIRECT(cmd)) 1190 return; 1191 /* 1192 * Source quench is depreciated. 1193 */ 1194 else if (cmd == PRC_QUENCH) 1195 return; 1196 /* 1197 * Hostdead is ugly because it goes linearly through all PCBs. 1198 * XXX: We never get this from ICMP, otherwise it makes an 1199 * excellent DoS attack on machines with many connections. 1200 */ 1201 else if (cmd == PRC_HOSTDEAD) 1202 ip = NULL; 1203 else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) 1204 return; 1205 if (ip != NULL) { 1206 icp = (struct icmp *)((caddr_t)ip 1207 - offsetof(struct icmp, icmp_ip)); 1208 th = (struct tcphdr *)((caddr_t)ip 1209 + (ip->ip_hl << 2)); 1210 INP_INFO_WLOCK(&tcbinfo); 1211 inp = in_pcblookup_hash(&tcbinfo, faddr, th->th_dport, 1212 ip->ip_src, th->th_sport, 0, NULL); 1213 if (inp != NULL) { 1214 INP_LOCK(inp); 1215 if (!(inp->inp_vflag & INP_TIMEWAIT) && 1216 !(inp->inp_vflag & INP_DROPPED) && 1217 !(inp->inp_socket == NULL)) { 1218 icmp_tcp_seq = htonl(th->th_seq); 1219 tp = intotcpcb(inp); 1220 if (SEQ_GEQ(icmp_tcp_seq, tp->snd_una) && 1221 SEQ_LT(icmp_tcp_seq, tp->snd_max)) { 1222 if (cmd == PRC_MSGSIZE) { 1223 /* 1224 * MTU discovery: 1225 * If we got a needfrag set the MTU 1226 * in the route to the suggested new 1227 * value (if given) and then notify. 1228 */ 1229 bzero(&inc, sizeof(inc)); 1230 inc.inc_flags = 0; /* IPv4 */ 1231 inc.inc_faddr = faddr; 1232 1233 mtu = ntohs(icp->icmp_nextmtu); 1234 /* 1235 * If no alternative MTU was 1236 * proposed, try the next smaller 1237 * one. ip->ip_len has already 1238 * been swapped in icmp_input(). 1239 */ 1240 if (!mtu) 1241 mtu = ip_next_mtu(ip->ip_len, 1242 1); 1243 if (mtu < max(296, (tcp_minmss) 1244 + sizeof(struct tcpiphdr))) 1245 mtu = 0; 1246 if (!mtu) 1247 mtu = tcp_mssdflt 1248 + sizeof(struct tcpiphdr); 1249 /* 1250 * Only cache the the MTU if it 1251 * is smaller than the interface 1252 * or route MTU. tcp_mtudisc() 1253 * will do right thing by itself. 1254 */ 1255 if (mtu <= tcp_maxmtu(&inc, NULL)) 1256 tcp_hc_updatemtu(&inc, mtu); 1257 } 1258 1259 inp = (*notify)(inp, inetctlerrmap[cmd]); 1260 } 1261 } 1262 if (inp != NULL) 1263 INP_UNLOCK(inp); 1264 } else { 1265 inc.inc_fport = th->th_dport; 1266 inc.inc_lport = th->th_sport; 1267 inc.inc_faddr = faddr; 1268 inc.inc_laddr = ip->ip_src; 1269#ifdef INET6 1270 inc.inc_isipv6 = 0; 1271#endif 1272 syncache_unreach(&inc, th); 1273 } 1274 INP_INFO_WUNLOCK(&tcbinfo); 1275 } else 1276 in_pcbnotifyall(&tcbinfo, faddr, inetctlerrmap[cmd], notify); 1277} 1278 1279#ifdef INET6 1280void 1281tcp6_ctlinput(int cmd, struct sockaddr *sa, void *d) 1282{ 1283 struct tcphdr th; 1284 struct inpcb *(*notify)(struct inpcb *, int) = tcp_notify; 1285 struct ip6_hdr *ip6; 1286 struct mbuf *m; 1287 struct ip6ctlparam *ip6cp = NULL; 1288 const struct sockaddr_in6 *sa6_src = NULL; 1289 int off; 1290 struct tcp_portonly { 1291 u_int16_t th_sport; 1292 u_int16_t th_dport; 1293 } *thp; 1294 1295 if (sa->sa_family != AF_INET6 || 1296 sa->sa_len != sizeof(struct sockaddr_in6)) 1297 return; 1298 1299 if (cmd == PRC_MSGSIZE) 1300 notify = tcp_mtudisc; 1301 else if (!PRC_IS_REDIRECT(cmd) && 1302 ((unsigned)cmd >= PRC_NCMDS || inet6ctlerrmap[cmd] == 0)) 1303 return; 1304 /* Source quench is depreciated. */ 1305 else if (cmd == PRC_QUENCH) 1306 return; 1307 1308 /* if the parameter is from icmp6, decode it. */ 1309 if (d != NULL) { 1310 ip6cp = (struct ip6ctlparam *)d; 1311 m = ip6cp->ip6c_m; 1312 ip6 = ip6cp->ip6c_ip6; 1313 off = ip6cp->ip6c_off; 1314 sa6_src = ip6cp->ip6c_src; 1315 } else { 1316 m = NULL; 1317 ip6 = NULL; 1318 off = 0; /* fool gcc */ 1319 sa6_src = &sa6_any; 1320 } 1321 1322 if (ip6 != NULL) { 1323 struct in_conninfo inc; 1324 /* 1325 * XXX: We assume that when IPV6 is non NULL, 1326 * M and OFF are valid. 1327 */ 1328 1329 /* check if we can safely examine src and dst ports */ 1330 if (m->m_pkthdr.len < off + sizeof(*thp)) 1331 return; 1332 1333 bzero(&th, sizeof(th)); 1334 m_copydata(m, off, sizeof(*thp), (caddr_t)&th); 1335 1336 in6_pcbnotify(&tcbinfo, sa, th.th_dport, 1337 (struct sockaddr *)ip6cp->ip6c_src, 1338 th.th_sport, cmd, NULL, notify); 1339 1340 inc.inc_fport = th.th_dport; 1341 inc.inc_lport = th.th_sport; 1342 inc.inc6_faddr = ((struct sockaddr_in6 *)sa)->sin6_addr; 1343 inc.inc6_laddr = ip6cp->ip6c_src->sin6_addr; 1344 inc.inc_isipv6 = 1; 1345 INP_INFO_WLOCK(&tcbinfo); 1346 syncache_unreach(&inc, &th); 1347 INP_INFO_WUNLOCK(&tcbinfo); 1348 } else 1349 in6_pcbnotify(&tcbinfo, sa, 0, (const struct sockaddr *)sa6_src, 1350 0, cmd, NULL, notify); 1351} 1352#endif /* INET6 */ 1353 1354 1355/* 1356 * Following is where TCP initial sequence number generation occurs. 1357 * 1358 * There are two places where we must use initial sequence numbers: 1359 * 1. In SYN-ACK packets. 1360 * 2. In SYN packets. 1361 * 1362 * All ISNs for SYN-ACK packets are generated by the syncache. See 1363 * tcp_syncache.c for details. 1364 * 1365 * The ISNs in SYN packets must be monotonic; TIME_WAIT recycling 1366 * depends on this property. In addition, these ISNs should be 1367 * unguessable so as to prevent connection hijacking. To satisfy 1368 * the requirements of this situation, the algorithm outlined in 1369 * RFC 1948 is used, with only small modifications. 1370 * 1371 * Implementation details: 1372 * 1373 * Time is based off the system timer, and is corrected so that it 1374 * increases by one megabyte per second. This allows for proper 1375 * recycling on high speed LANs while still leaving over an hour 1376 * before rollover. 1377 * 1378 * As reading the *exact* system time is too expensive to be done 1379 * whenever setting up a TCP connection, we increment the time 1380 * offset in two ways. First, a small random positive increment 1381 * is added to isn_offset for each connection that is set up. 1382 * Second, the function tcp_isn_tick fires once per clock tick 1383 * and increments isn_offset as necessary so that sequence numbers 1384 * are incremented at approximately ISN_BYTES_PER_SECOND. The 1385 * random positive increments serve only to ensure that the same 1386 * exact sequence number is never sent out twice (as could otherwise 1387 * happen when a port is recycled in less than the system tick 1388 * interval.) 1389 * 1390 * net.inet.tcp.isn_reseed_interval controls the number of seconds 1391 * between seeding of isn_secret. This is normally set to zero, 1392 * as reseeding should not be necessary. 1393 * 1394 * Locking of the global variables isn_secret, isn_last_reseed, isn_offset, 1395 * isn_offset_old, and isn_ctx is performed using the TCP pcbinfo lock. In 1396 * general, this means holding an exclusive (write) lock. 1397 */ 1398 1399#define ISN_BYTES_PER_SECOND 1048576 1400#define ISN_STATIC_INCREMENT 4096 1401#define ISN_RANDOM_INCREMENT (4096 - 1) 1402 1403static u_char isn_secret[32]; 1404static int isn_last_reseed; 1405static u_int32_t isn_offset, isn_offset_old; 1406static MD5_CTX isn_ctx; 1407 1408tcp_seq 1409tcp_new_isn(struct tcpcb *tp) 1410{ 1411 u_int32_t md5_buffer[4]; 1412 tcp_seq new_isn; 1413 1414 INP_LOCK_ASSERT(tp->t_inpcb); 1415 1416 ISN_LOCK(); 1417 /* Seed if this is the first use, reseed if requested. */ 1418 if ((isn_last_reseed == 0) || ((tcp_isn_reseed_interval > 0) && 1419 (((u_int)isn_last_reseed + (u_int)tcp_isn_reseed_interval*hz) 1420 < (u_int)ticks))) { 1421 read_random(&isn_secret, sizeof(isn_secret)); 1422 isn_last_reseed = ticks; 1423 } 1424 1425 /* Compute the md5 hash and return the ISN. */ 1426 MD5Init(&isn_ctx); 1427 MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->inp_fport, sizeof(u_short)); 1428 MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->inp_lport, sizeof(u_short)); 1429#ifdef INET6 1430 if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) { 1431 MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->in6p_faddr, 1432 sizeof(struct in6_addr)); 1433 MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->in6p_laddr, 1434 sizeof(struct in6_addr)); 1435 } else 1436#endif 1437 { 1438 MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->inp_faddr, 1439 sizeof(struct in_addr)); 1440 MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->inp_laddr, 1441 sizeof(struct in_addr)); 1442 } 1443 MD5Update(&isn_ctx, (u_char *) &isn_secret, sizeof(isn_secret)); 1444 MD5Final((u_char *) &md5_buffer, &isn_ctx); 1445 new_isn = (tcp_seq) md5_buffer[0]; 1446 isn_offset += ISN_STATIC_INCREMENT + 1447 (arc4random() & ISN_RANDOM_INCREMENT); 1448 new_isn += isn_offset; 1449 ISN_UNLOCK(); 1450 return (new_isn); 1451} 1452 1453/* 1454 * Increment the offset to the next ISN_BYTES_PER_SECOND / hz boundary 1455 * to keep time flowing at a relatively constant rate. If the random 1456 * increments have already pushed us past the projected offset, do nothing. 1457 */ 1458static void 1459tcp_isn_tick(void *xtp) 1460{ 1461 u_int32_t projected_offset; 1462 1463 ISN_LOCK(); 1464 projected_offset = isn_offset_old + ISN_BYTES_PER_SECOND / 100; 1465 1466 if (projected_offset > isn_offset) 1467 isn_offset = projected_offset; 1468 1469 isn_offset_old = isn_offset; 1470 callout_reset(&isn_callout, hz/100, tcp_isn_tick, NULL); 1471 ISN_UNLOCK(); 1472} 1473 1474/* 1475 * When a specific ICMP unreachable message is received and the 1476 * connection state is SYN-SENT, drop the connection. This behavior 1477 * is controlled by the icmp_may_rst sysctl. 1478 */ 1479struct inpcb * 1480tcp_drop_syn_sent(struct inpcb *inp, int errno) 1481{ 1482 struct tcpcb *tp; 1483 1484 INP_INFO_WLOCK_ASSERT(&tcbinfo); 1485 INP_LOCK_ASSERT(inp); 1486 1487 if ((inp->inp_vflag & INP_TIMEWAIT) || 1488 (inp->inp_vflag & INP_DROPPED)) 1489 return (inp); 1490 1491 tp = intotcpcb(inp); 1492 if (tp->t_state != TCPS_SYN_SENT) 1493 return (inp); 1494 1495 tp = tcp_drop(tp, errno); 1496 if (tp != NULL) 1497 return (inp); 1498 else 1499 return (NULL); 1500} 1501 1502/* 1503 * When `need fragmentation' ICMP is received, update our idea of the MSS 1504 * based on the new value in the route. Also nudge TCP to send something, 1505 * since we know the packet we just sent was dropped. 1506 * This duplicates some code in the tcp_mss() function in tcp_input.c. 1507 */ 1508struct inpcb * 1509tcp_mtudisc(struct inpcb *inp, int errno) 1510{ 1511 struct tcpcb *tp; 1512 struct socket *so = inp->inp_socket; 1513 u_int maxmtu; 1514 u_int romtu; 1515 int mss; 1516#ifdef INET6 1517 int isipv6; 1518#endif /* INET6 */ 1519 1520 INP_LOCK_ASSERT(inp); 1521 if ((inp->inp_vflag & INP_TIMEWAIT) || 1522 (inp->inp_vflag & INP_DROPPED)) 1523 return (inp); 1524 1525 tp = intotcpcb(inp); 1526 KASSERT(tp != NULL, ("tcp_mtudisc: tp == NULL")); 1527 1528#ifdef INET6 1529 isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0; 1530#endif 1531 maxmtu = tcp_hc_getmtu(&inp->inp_inc); /* IPv4 and IPv6 */ 1532 romtu = 1533#ifdef INET6 1534 isipv6 ? tcp_maxmtu6(&inp->inp_inc, NULL) : 1535#endif /* INET6 */ 1536 tcp_maxmtu(&inp->inp_inc, NULL); 1537 if (!maxmtu) 1538 maxmtu = romtu; 1539 else 1540 maxmtu = min(maxmtu, romtu); 1541 if (!maxmtu) { 1542 tp->t_maxopd = tp->t_maxseg = 1543#ifdef INET6 1544 isipv6 ? tcp_v6mssdflt : 1545#endif /* INET6 */ 1546 tcp_mssdflt; 1547 return (inp); 1548 } 1549 mss = maxmtu - 1550#ifdef INET6 1551 (isipv6 ? sizeof(struct ip6_hdr) + sizeof(struct tcphdr) : 1552#endif /* INET6 */ 1553 sizeof(struct tcpiphdr) 1554#ifdef INET6 1555 ) 1556#endif /* INET6 */ 1557 ; 1558 1559 /* 1560 * XXX - The above conditional probably violates the TCP 1561 * spec. The problem is that, since we don't know the 1562 * other end's MSS, we are supposed to use a conservative 1563 * default. But, if we do that, then MTU discovery will 1564 * never actually take place, because the conservative 1565 * default is much less than the MTUs typically seen 1566 * on the Internet today. For the moment, we'll sweep 1567 * this under the carpet. 1568 * 1569 * The conservative default might not actually be a problem 1570 * if the only case this occurs is when sending an initial 1571 * SYN with options and data to a host we've never talked 1572 * to before. Then, they will reply with an MSS value which 1573 * will get recorded and the new parameters should get 1574 * recomputed. For Further Study. 1575 */ 1576 if (tp->t_maxopd <= mss) 1577 return (inp); 1578 tp->t_maxopd = mss; 1579 1580 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 1581 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP) 1582 mss -= TCPOLEN_TSTAMP_APPA; 1583#if (MCLBYTES & (MCLBYTES - 1)) == 0 1584 if (mss > MCLBYTES) 1585 mss &= ~(MCLBYTES-1); 1586#else 1587 if (mss > MCLBYTES) 1588 mss = mss / MCLBYTES * MCLBYTES; 1589#endif 1590 if (so->so_snd.sb_hiwat < mss) 1591 mss = so->so_snd.sb_hiwat; 1592 1593 tp->t_maxseg = mss; 1594 1595 tcpstat.tcps_mturesent++; 1596 tp->t_rtttime = 0; 1597 tp->snd_nxt = tp->snd_una; 1598 tcp_free_sackholes(tp); 1599 tp->snd_recover = tp->snd_max; 1600 if (tp->sack_enable) 1601 EXIT_FASTRECOVERY(tp); 1602 tcp_output(tp); 1603 return (inp); 1604} 1605 1606/* 1607 * Look-up the routing entry to the peer of this inpcb. If no route 1608 * is found and it cannot be allocated, then return NULL. This routine 1609 * is called by TCP routines that access the rmx structure and by tcp_mss 1610 * to get the interface MTU. 1611 */ 1612u_long 1613tcp_maxmtu(struct in_conninfo *inc, int *flags) 1614{ 1615 struct route sro; 1616 struct sockaddr_in *dst; 1617 struct ifnet *ifp; 1618 u_long maxmtu = 0; 1619 1620 KASSERT(inc != NULL, ("tcp_maxmtu with NULL in_conninfo pointer")); 1621 1622 bzero(&sro, sizeof(sro)); 1623 if (inc->inc_faddr.s_addr != INADDR_ANY) { 1624 dst = (struct sockaddr_in *)&sro.ro_dst; 1625 dst->sin_family = AF_INET; 1626 dst->sin_len = sizeof(*dst); 1627 dst->sin_addr = inc->inc_faddr; 1628 rtalloc_ign(&sro, RTF_CLONING); 1629 } 1630 if (sro.ro_rt != NULL) { 1631 ifp = sro.ro_rt->rt_ifp; 1632 if (sro.ro_rt->rt_rmx.rmx_mtu == 0) 1633 maxmtu = ifp->if_mtu; 1634 else 1635 maxmtu = min(sro.ro_rt->rt_rmx.rmx_mtu, ifp->if_mtu); 1636 1637 /* Report additional interface capabilities. */ 1638 if (flags != NULL) { 1639 if (ifp->if_capenable & IFCAP_TSO4 && 1640 ifp->if_hwassist & CSUM_TSO) 1641 *flags |= CSUM_TSO; 1642 } 1643 RTFREE(sro.ro_rt); 1644 } 1645 return (maxmtu); 1646} 1647 1648#ifdef INET6 1649u_long 1650tcp_maxmtu6(struct in_conninfo *inc, int *flags) 1651{ 1652 struct route_in6 sro6; 1653 struct ifnet *ifp; 1654 u_long maxmtu = 0; 1655 1656 KASSERT(inc != NULL, ("tcp_maxmtu6 with NULL in_conninfo pointer")); 1657 1658 bzero(&sro6, sizeof(sro6)); 1659 if (!IN6_IS_ADDR_UNSPECIFIED(&inc->inc6_faddr)) { 1660 sro6.ro_dst.sin6_family = AF_INET6; 1661 sro6.ro_dst.sin6_len = sizeof(struct sockaddr_in6); 1662 sro6.ro_dst.sin6_addr = inc->inc6_faddr; 1663 rtalloc_ign((struct route *)&sro6, RTF_CLONING); 1664 } 1665 if (sro6.ro_rt != NULL) { 1666 ifp = sro6.ro_rt->rt_ifp; 1667 if (sro6.ro_rt->rt_rmx.rmx_mtu == 0) 1668 maxmtu = IN6_LINKMTU(sro6.ro_rt->rt_ifp); 1669 else 1670 maxmtu = min(sro6.ro_rt->rt_rmx.rmx_mtu, 1671 IN6_LINKMTU(sro6.ro_rt->rt_ifp)); 1672 1673 /* Report additional interface capabilities. */ 1674 if (flags != NULL) { 1675 if (ifp->if_capenable & IFCAP_TSO6 && 1676 ifp->if_hwassist & CSUM_TSO) 1677 *flags |= CSUM_TSO; 1678 } 1679 RTFREE(sro6.ro_rt); 1680 } 1681 1682 return (maxmtu); 1683} 1684#endif /* INET6 */ 1685 1686#ifdef IPSEC 1687/* compute ESP/AH header size for TCP, including outer IP header. */ 1688size_t 1689ipsec_hdrsiz_tcp(struct tcpcb *tp) 1690{ 1691 struct inpcb *inp; 1692 struct mbuf *m; 1693 size_t hdrsiz; 1694 struct ip *ip; 1695#ifdef INET6 1696 struct ip6_hdr *ip6; 1697#endif 1698 struct tcphdr *th; 1699 1700 if ((tp == NULL) || ((inp = tp->t_inpcb) == NULL)) 1701 return (0); 1702 MGETHDR(m, M_DONTWAIT, MT_DATA); 1703 if (!m) 1704 return (0); 1705 1706#ifdef INET6 1707 if ((inp->inp_vflag & INP_IPV6) != 0) { 1708 ip6 = mtod(m, struct ip6_hdr *); 1709 th = (struct tcphdr *)(ip6 + 1); 1710 m->m_pkthdr.len = m->m_len = 1711 sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 1712 tcpip_fillheaders(inp, ip6, th); 1713 hdrsiz = ipsec6_hdrsiz(m, IPSEC_DIR_OUTBOUND, inp); 1714 } else 1715#endif /* INET6 */ 1716 { 1717 ip = mtod(m, struct ip *); 1718 th = (struct tcphdr *)(ip + 1); 1719 m->m_pkthdr.len = m->m_len = sizeof(struct tcpiphdr); 1720 tcpip_fillheaders(inp, ip, th); 1721 hdrsiz = ipsec4_hdrsiz(m, IPSEC_DIR_OUTBOUND, inp); 1722 } 1723 1724 m_free(m); 1725 return (hdrsiz); 1726} 1727#endif /*IPSEC*/ 1728 1729/* 1730 * Move a TCP connection into TIME_WAIT state. 1731 * tcbinfo is locked. 1732 * inp is locked, and is unlocked before returning. 1733 */ 1734void 1735tcp_twstart(struct tcpcb *tp) 1736{ 1737 struct tcptw *tw; 1738 struct inpcb *inp; 1739 int tw_time, acknow; 1740 struct socket *so; 1741 1742 INP_INFO_WLOCK_ASSERT(&tcbinfo); /* tcp_timer_2msl_reset(). */ 1743 INP_LOCK_ASSERT(tp->t_inpcb); 1744 1745 tw = uma_zalloc(tcptw_zone, M_NOWAIT); 1746 if (tw == NULL) { 1747 tw = tcp_timer_2msl_tw(1); 1748 if (tw == NULL) { 1749 tp = tcp_close(tp); 1750 if (tp != NULL) 1751 INP_UNLOCK(tp->t_inpcb); 1752 return; 1753 } 1754 } 1755 inp = tp->t_inpcb; 1756 tw->tw_inpcb = inp; 1757 1758 /* 1759 * Recover last window size sent. 1760 */ 1761 tw->last_win = (tp->rcv_adv - tp->rcv_nxt) >> tp->rcv_scale; 1762 1763 /* 1764 * Set t_recent if timestamps are used on the connection. 1765 */ 1766 if ((tp->t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP|TF_NOOPT)) == 1767 (TF_REQ_TSTMP|TF_RCVD_TSTMP)) 1768 tw->t_recent = tp->ts_recent; 1769 else 1770 tw->t_recent = 0; 1771 1772 tw->snd_nxt = tp->snd_nxt; 1773 tw->rcv_nxt = tp->rcv_nxt; 1774 tw->iss = tp->iss; 1775 tw->irs = tp->irs; 1776 tw->t_starttime = tp->t_starttime; 1777 tw->tw_time = 0; 1778 1779/* XXX 1780 * If this code will 1781 * be used for fin-wait-2 state also, then we may need 1782 * a ts_recent from the last segment. 1783 */ 1784 tw_time = 2 * tcp_msl; 1785 acknow = tp->t_flags & TF_ACKNOW; 1786 1787 /* 1788 * First, discard tcpcb state, which includes stopping its timers and 1789 * freeing it. tcp_discardcb() used to also release the inpcb, but 1790 * that work is now done in the caller. 1791 * 1792 * Note: soisdisconnected() call used to be made in tcp_discardcb(), 1793 * and might not be needed here any longer. 1794 */ 1795 tcp_discardcb(tp); 1796 so = inp->inp_socket; 1797 soisdisconnected(so); 1798 SOCK_LOCK(so); 1799 tw->tw_cred = crhold(so->so_cred); 1800 tw->tw_so_options = so->so_options; 1801 SOCK_UNLOCK(so); 1802 if (acknow) 1803 tcp_twrespond(tw, TH_ACK); 1804 inp->inp_ppcb = tw; 1805 inp->inp_vflag |= INP_TIMEWAIT; 1806 tcp_timer_2msl_reset(tw, tw_time, 0); 1807 1808 /* 1809 * If the inpcb owns the sole reference to the socket, then we can 1810 * detach and free the socket as it is not needed in time wait. 1811 */ 1812 if (inp->inp_vflag & INP_SOCKREF) { 1813 KASSERT(so->so_state & SS_PROTOREF, 1814 ("tcp_twstart: !SS_PROTOREF")); 1815 inp->inp_vflag &= ~INP_SOCKREF; 1816 INP_UNLOCK(inp); 1817 ACCEPT_LOCK(); 1818 SOCK_LOCK(so); 1819 so->so_state &= ~SS_PROTOREF; 1820 sofree(so); 1821 } else 1822 INP_UNLOCK(inp); 1823} 1824 1825#if 0 1826/* 1827 * The appromixate rate of ISN increase of Microsoft TCP stacks; 1828 * the actual rate is slightly higher due to the addition of 1829 * random positive increments. 1830 * 1831 * Most other new OSes use semi-randomized ISN values, so we 1832 * do not need to worry about them. 1833 */ 1834#define MS_ISN_BYTES_PER_SECOND 250000 1835 1836/* 1837 * Determine if the ISN we will generate has advanced beyond the last 1838 * sequence number used by the previous connection. If so, indicate 1839 * that it is safe to recycle this tw socket by returning 1. 1840 */ 1841int 1842tcp_twrecycleable(struct tcptw *tw) 1843{ 1844 tcp_seq new_iss = tw->iss; 1845 tcp_seq new_irs = tw->irs; 1846 1847 INP_INFO_WLOCK_ASSERT(&tcbinfo); 1848 new_iss += (ticks - tw->t_starttime) * (ISN_BYTES_PER_SECOND / hz); 1849 new_irs += (ticks - tw->t_starttime) * (MS_ISN_BYTES_PER_SECOND / hz); 1850 1851 if (SEQ_GT(new_iss, tw->snd_nxt) && SEQ_GT(new_irs, tw->rcv_nxt)) 1852 return (1); 1853 else 1854 return (0); 1855} 1856#endif 1857 1858void 1859tcp_twclose(struct tcptw *tw, int reuse) 1860{ 1861 struct socket *so; 1862 struct inpcb *inp; 1863 1864 /* 1865 * At this point, we are in one of two situations: 1866 * 1867 * (1) We have no socket, just an inpcb<->twtcp pair. We can free 1868 * all state. 1869 * 1870 * (2) We have a socket -- if we own a reference, release it and 1871 * notify the socket layer. 1872 */ 1873 inp = tw->tw_inpcb; 1874 KASSERT((inp->inp_vflag & INP_TIMEWAIT), ("tcp_twclose: !timewait")); 1875 KASSERT(intotw(inp) == tw, ("tcp_twclose: inp_ppcb != tw")); 1876 INP_INFO_WLOCK_ASSERT(&tcbinfo); /* tcp_timer_2msl_stop(). */ 1877 INP_LOCK_ASSERT(inp); 1878 1879 tw->tw_inpcb = NULL; 1880 tcp_timer_2msl_stop(tw); 1881 inp->inp_ppcb = NULL; 1882 in_pcbdrop(inp); 1883 1884 so = inp->inp_socket; 1885 if (so != NULL) { 1886 /* 1887 * If there's a socket, handle two cases: first, we own a 1888 * strong reference, which we will now release, or we don't 1889 * in which case another reference exists (XXXRW: think 1890 * about this more), and we don't need to take action. 1891 */ 1892 if (inp->inp_vflag & INP_SOCKREF) { 1893 inp->inp_vflag &= ~INP_SOCKREF; 1894 INP_UNLOCK(inp); 1895 ACCEPT_LOCK(); 1896 SOCK_LOCK(so); 1897 KASSERT(so->so_state & SS_PROTOREF, 1898 ("tcp_twclose: INP_SOCKREF && !SS_PROTOREF")); 1899 so->so_state &= ~SS_PROTOREF; 1900 sofree(so); 1901 } else { 1902 /* 1903 * If we don't own the only reference, the socket and 1904 * inpcb need to be left around to be handled by 1905 * tcp_usr_detach() later. 1906 */ 1907 INP_UNLOCK(inp); 1908 } 1909 } else { 1910#ifdef INET6 1911 if (inp->inp_vflag & INP_IPV6PROTO) 1912 in6_pcbfree(inp); 1913 else 1914#endif 1915 in_pcbfree(inp); 1916 } 1917 tcpstat.tcps_closed++; 1918 crfree(tw->tw_cred); 1919 tw->tw_cred = NULL; 1920 if (reuse) 1921 return; 1922 uma_zfree(tcptw_zone, tw); 1923} 1924 1925int 1926tcp_twrespond(struct tcptw *tw, int flags) 1927{ 1928 struct inpcb *inp = tw->tw_inpcb; 1929 struct tcphdr *th; 1930 struct mbuf *m; 1931 struct ip *ip = NULL; 1932 u_int8_t *optp; 1933 u_int hdrlen, optlen; 1934 int error; 1935#ifdef INET6 1936 struct ip6_hdr *ip6 = NULL; 1937 int isipv6 = inp->inp_inc.inc_isipv6; 1938#endif 1939 1940 INP_LOCK_ASSERT(inp); 1941 1942 m = m_gethdr(M_DONTWAIT, MT_DATA); 1943 if (m == NULL) 1944 return (ENOBUFS); 1945 m->m_data += max_linkhdr; 1946 1947#ifdef MAC 1948 mac_create_mbuf_from_inpcb(inp, m); 1949#endif 1950 1951#ifdef INET6 1952 if (isipv6) { 1953 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 1954 ip6 = mtod(m, struct ip6_hdr *); 1955 th = (struct tcphdr *)(ip6 + 1); 1956 tcpip_fillheaders(inp, ip6, th); 1957 } else 1958#endif 1959 { 1960 hdrlen = sizeof(struct tcpiphdr); 1961 ip = mtod(m, struct ip *); 1962 th = (struct tcphdr *)(ip + 1); 1963 tcpip_fillheaders(inp, ip, th); 1964 } 1965 optp = (u_int8_t *)(th + 1); 1966 1967 /* 1968 * Send a timestamp and echo-reply if both our side and our peer 1969 * have sent timestamps in our SYN's and this is not a RST. 1970 */ 1971 if (tw->t_recent && flags == TH_ACK) { 1972 u_int32_t *lp = (u_int32_t *)optp; 1973 1974 /* Form timestamp option as shown in appendix A of RFC 1323. */ 1975 *lp++ = htonl(TCPOPT_TSTAMP_HDR); 1976 *lp++ = htonl(ticks); 1977 *lp = htonl(tw->t_recent); 1978 optp += TCPOLEN_TSTAMP_APPA; 1979 } 1980 1981 optlen = optp - (u_int8_t *)(th + 1); 1982 1983 m->m_len = hdrlen + optlen; 1984 m->m_pkthdr.len = m->m_len; 1985 1986 KASSERT(max_linkhdr + m->m_len <= MHLEN, ("tcptw: mbuf too small")); 1987 1988 th->th_seq = htonl(tw->snd_nxt); 1989 th->th_ack = htonl(tw->rcv_nxt); 1990 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2; 1991 th->th_flags = flags; 1992 th->th_win = htons(tw->last_win); 1993 1994#ifdef INET6 1995 if (isipv6) { 1996 th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr), 1997 sizeof(struct tcphdr) + optlen); 1998 ip6->ip6_hlim = in6_selecthlim(inp, NULL); 1999 error = ip6_output(m, inp->in6p_outputopts, NULL, 2000 (tw->tw_so_options & SO_DONTROUTE), NULL, NULL, inp); 2001 } else 2002#endif 2003 { 2004 th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 2005 htons(sizeof(struct tcphdr) + optlen + IPPROTO_TCP)); 2006 m->m_pkthdr.csum_flags = CSUM_TCP; 2007 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 2008 ip->ip_len = m->m_pkthdr.len; 2009 if (path_mtu_discovery) 2010 ip->ip_off |= IP_DF; 2011 error = ip_output(m, inp->inp_options, NULL, 2012 ((tw->tw_so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 2013 NULL, inp); 2014 } 2015 if (flags & TH_ACK) 2016 tcpstat.tcps_sndacks++; 2017 else 2018 tcpstat.tcps_sndctrl++; 2019 tcpstat.tcps_sndtotal++; 2020 return (error); 2021} 2022 2023/* 2024 * TCP BANDWIDTH DELAY PRODUCT WINDOW LIMITING 2025 * 2026 * This code attempts to calculate the bandwidth-delay product as a 2027 * means of determining the optimal window size to maximize bandwidth, 2028 * minimize RTT, and avoid the over-allocation of buffers on interfaces and 2029 * routers. This code also does a fairly good job keeping RTTs in check 2030 * across slow links like modems. We implement an algorithm which is very 2031 * similar (but not meant to be) TCP/Vegas. The code operates on the 2032 * transmitter side of a TCP connection and so only effects the transmit 2033 * side of the connection. 2034 * 2035 * BACKGROUND: TCP makes no provision for the management of buffer space 2036 * at the end points or at the intermediate routers and switches. A TCP 2037 * stream, whether using NewReno or not, will eventually buffer as 2038 * many packets as it is able and the only reason this typically works is 2039 * due to the fairly small default buffers made available for a connection 2040 * (typicaly 16K or 32K). As machines use larger windows and/or window 2041 * scaling it is now fairly easy for even a single TCP connection to blow-out 2042 * all available buffer space not only on the local interface, but on 2043 * intermediate routers and switches as well. NewReno makes a misguided 2044 * attempt to 'solve' this problem by waiting for an actual failure to occur, 2045 * then backing off, then steadily increasing the window again until another 2046 * failure occurs, ad-infinitum. This results in terrible oscillation that 2047 * is only made worse as network loads increase and the idea of intentionally 2048 * blowing out network buffers is, frankly, a terrible way to manage network 2049 * resources. 2050 * 2051 * It is far better to limit the transmit window prior to the failure 2052 * condition being achieved. There are two general ways to do this: First 2053 * you can 'scan' through different transmit window sizes and locate the 2054 * point where the RTT stops increasing, indicating that you have filled the 2055 * pipe, then scan backwards until you note that RTT stops decreasing, then 2056 * repeat ad-infinitum. This method works in principle but has severe 2057 * implementation issues due to RTT variances, timer granularity, and 2058 * instability in the algorithm which can lead to many false positives and 2059 * create oscillations as well as interact badly with other TCP streams 2060 * implementing the same algorithm. 2061 * 2062 * The second method is to limit the window to the bandwidth delay product 2063 * of the link. This is the method we implement. RTT variances and our 2064 * own manipulation of the congestion window, bwnd, can potentially 2065 * destabilize the algorithm. For this reason we have to stabilize the 2066 * elements used to calculate the window. We do this by using the minimum 2067 * observed RTT, the long term average of the observed bandwidth, and 2068 * by adding two segments worth of slop. It isn't perfect but it is able 2069 * to react to changing conditions and gives us a very stable basis on 2070 * which to extend the algorithm. 2071 */ 2072void 2073tcp_xmit_bandwidth_limit(struct tcpcb *tp, tcp_seq ack_seq) 2074{ 2075 u_long bw; 2076 u_long bwnd; 2077 int save_ticks; 2078 2079 INP_LOCK_ASSERT(tp->t_inpcb); 2080 2081 /* 2082 * If inflight_enable is disabled in the middle of a tcp connection, 2083 * make sure snd_bwnd is effectively disabled. 2084 */ 2085 if (tcp_inflight_enable == 0 || tp->t_rttlow < tcp_inflight_rttthresh) { 2086 tp->snd_bwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT; 2087 tp->snd_bandwidth = 0; 2088 return; 2089 } 2090 2091 /* 2092 * Figure out the bandwidth. Due to the tick granularity this 2093 * is a very rough number and it MUST be averaged over a fairly 2094 * long period of time. XXX we need to take into account a link 2095 * that is not using all available bandwidth, but for now our 2096 * slop will ramp us up if this case occurs and the bandwidth later 2097 * increases. 2098 * 2099 * Note: if ticks rollover 'bw' may wind up negative. We must 2100 * effectively reset t_bw_rtttime for this case. 2101 */ 2102 save_ticks = ticks; 2103 if ((u_int)(save_ticks - tp->t_bw_rtttime) < 1) 2104 return; 2105 2106 bw = (int64_t)(ack_seq - tp->t_bw_rtseq) * hz / 2107 (save_ticks - tp->t_bw_rtttime); 2108 tp->t_bw_rtttime = save_ticks; 2109 tp->t_bw_rtseq = ack_seq; 2110 if (tp->t_bw_rtttime == 0 || (int)bw < 0) 2111 return; 2112 bw = ((int64_t)tp->snd_bandwidth * 15 + bw) >> 4; 2113 2114 tp->snd_bandwidth = bw; 2115 2116 /* 2117 * Calculate the semi-static bandwidth delay product, plus two maximal 2118 * segments. The additional slop puts us squarely in the sweet 2119 * spot and also handles the bandwidth run-up case and stabilization. 2120 * Without the slop we could be locking ourselves into a lower 2121 * bandwidth. 2122 * 2123 * Situations Handled: 2124 * (1) Prevents over-queueing of packets on LANs, especially on 2125 * high speed LANs, allowing larger TCP buffers to be 2126 * specified, and also does a good job preventing 2127 * over-queueing of packets over choke points like modems 2128 * (at least for the transmit side). 2129 * 2130 * (2) Is able to handle changing network loads (bandwidth 2131 * drops so bwnd drops, bandwidth increases so bwnd 2132 * increases). 2133 * 2134 * (3) Theoretically should stabilize in the face of multiple 2135 * connections implementing the same algorithm (this may need 2136 * a little work). 2137 * 2138 * (4) Stability value (defaults to 20 = 2 maximal packets) can 2139 * be adjusted with a sysctl but typically only needs to be 2140 * on very slow connections. A value no smaller then 5 2141 * should be used, but only reduce this default if you have 2142 * no other choice. 2143 */ 2144#define USERTT ((tp->t_srtt + tp->t_rttbest) / 2) 2145 bwnd = (int64_t)bw * USERTT / (hz << TCP_RTT_SHIFT) + tcp_inflight_stab * tp->t_maxseg / 10; 2146#undef USERTT 2147 2148 if (tcp_inflight_debug > 0) { 2149 static int ltime; 2150 if ((u_int)(ticks - ltime) >= hz / tcp_inflight_debug) { 2151 ltime = ticks; 2152 printf("%p bw %ld rttbest %d srtt %d bwnd %ld\n", 2153 tp, 2154 bw, 2155 tp->t_rttbest, 2156 tp->t_srtt, 2157 bwnd 2158 ); 2159 } 2160 } 2161 if ((long)bwnd < tcp_inflight_min) 2162 bwnd = tcp_inflight_min; 2163 if (bwnd > tcp_inflight_max) 2164 bwnd = tcp_inflight_max; 2165 if ((long)bwnd < tp->t_maxseg * 2) 2166 bwnd = tp->t_maxseg * 2; 2167 tp->snd_bwnd = bwnd; 2168} 2169 2170#ifdef TCP_SIGNATURE 2171/* 2172 * Callback function invoked by m_apply() to digest TCP segment data 2173 * contained within an mbuf chain. 2174 */ 2175static int 2176tcp_signature_apply(void *fstate, void *data, u_int len) 2177{ 2178 2179 MD5Update(fstate, (u_char *)data, len); 2180 return (0); 2181} 2182 2183/* 2184 * Compute TCP-MD5 hash of a TCPv4 segment. (RFC2385) 2185 * 2186 * Parameters: 2187 * m pointer to head of mbuf chain 2188 * off0 offset to TCP header within the mbuf chain 2189 * len length of TCP segment data, excluding options 2190 * optlen length of TCP segment options 2191 * buf pointer to storage for computed MD5 digest 2192 * direction direction of flow (IPSEC_DIR_INBOUND or OUTBOUND) 2193 * 2194 * We do this over ip, tcphdr, segment data, and the key in the SADB. 2195 * When called from tcp_input(), we can be sure that th_sum has been 2196 * zeroed out and verified already. 2197 * 2198 * This function is for IPv4 use only. Calling this function with an 2199 * IPv6 packet in the mbuf chain will yield undefined results. 2200 * 2201 * Return 0 if successful, otherwise return -1. 2202 * 2203 * XXX The key is retrieved from the system's PF_KEY SADB, by keying a 2204 * search with the destination IP address, and a 'magic SPI' to be 2205 * determined by the application. This is hardcoded elsewhere to 1179 2206 * right now. Another branch of this code exists which uses the SPD to 2207 * specify per-application flows but it is unstable. 2208 */ 2209int 2210tcp_signature_compute(struct mbuf *m, int off0, int len, int optlen, 2211 u_char *buf, u_int direction) 2212{ 2213 union sockaddr_union dst; 2214 struct ippseudo ippseudo; 2215 MD5_CTX ctx; 2216 int doff; 2217 struct ip *ip; 2218 struct ipovly *ipovly; 2219 struct secasvar *sav; 2220 struct tcphdr *th; 2221 u_short savecsum; 2222 2223 KASSERT(m != NULL, ("NULL mbuf chain")); 2224 KASSERT(buf != NULL, ("NULL signature pointer")); 2225 2226 /* Extract the destination from the IP header in the mbuf. */ 2227 ip = mtod(m, struct ip *); 2228 bzero(&dst, sizeof(union sockaddr_union)); 2229 dst.sa.sa_len = sizeof(struct sockaddr_in); 2230 dst.sa.sa_family = AF_INET; 2231 dst.sin.sin_addr = (direction == IPSEC_DIR_INBOUND) ? 2232 ip->ip_src : ip->ip_dst; 2233 2234 /* Look up an SADB entry which matches the address of the peer. */ 2235 sav = KEY_ALLOCSA(&dst, IPPROTO_TCP, htonl(TCP_SIG_SPI)); 2236 if (sav == NULL) { 2237 printf("%s: SADB lookup failed for %s\n", __func__, 2238 inet_ntoa(dst.sin.sin_addr)); 2239 return (EINVAL); 2240 } 2241 2242 MD5Init(&ctx); 2243 ipovly = (struct ipovly *)ip; 2244 th = (struct tcphdr *)((u_char *)ip + off0); 2245 doff = off0 + sizeof(struct tcphdr) + optlen; 2246 2247 /* 2248 * Step 1: Update MD5 hash with IP pseudo-header. 2249 * 2250 * XXX The ippseudo header MUST be digested in network byte order, 2251 * or else we'll fail the regression test. Assume all fields we've 2252 * been doing arithmetic on have been in host byte order. 2253 * XXX One cannot depend on ipovly->ih_len here. When called from 2254 * tcp_output(), the underlying ip_len member has not yet been set. 2255 */ 2256 ippseudo.ippseudo_src = ipovly->ih_src; 2257 ippseudo.ippseudo_dst = ipovly->ih_dst; 2258 ippseudo.ippseudo_pad = 0; 2259 ippseudo.ippseudo_p = IPPROTO_TCP; 2260 ippseudo.ippseudo_len = htons(len + sizeof(struct tcphdr) + optlen); 2261 MD5Update(&ctx, (char *)&ippseudo, sizeof(struct ippseudo)); 2262 2263 /* 2264 * Step 2: Update MD5 hash with TCP header, excluding options. 2265 * The TCP checksum must be set to zero. 2266 */ 2267 savecsum = th->th_sum; 2268 th->th_sum = 0; 2269 MD5Update(&ctx, (char *)th, sizeof(struct tcphdr)); 2270 th->th_sum = savecsum; 2271 2272 /* 2273 * Step 3: Update MD5 hash with TCP segment data. 2274 * Use m_apply() to avoid an early m_pullup(). 2275 */ 2276 if (len > 0) 2277 m_apply(m, doff, len, tcp_signature_apply, &ctx); 2278 2279 /* 2280 * Step 4: Update MD5 hash with shared secret. 2281 */ 2282 MD5Update(&ctx, _KEYBUF(sav->key_auth), _KEYLEN(sav->key_auth)); 2283 MD5Final(buf, &ctx); 2284 2285 key_sa_recordxfer(sav, m); 2286 KEY_FREESAV(&sav); 2287 return (0); 2288} 2289#endif /* TCP_SIGNATURE */ 2290 2291static int 2292sysctl_drop(SYSCTL_HANDLER_ARGS) 2293{ 2294 /* addrs[0] is a foreign socket, addrs[1] is a local one. */ 2295 struct sockaddr_storage addrs[2]; 2296 struct inpcb *inp; 2297 struct tcpcb *tp; 2298 struct tcptw *tw; 2299 struct sockaddr_in *fin, *lin; 2300#ifdef INET6 2301 struct sockaddr_in6 *fin6, *lin6; 2302 struct in6_addr f6, l6; 2303#endif 2304 int error; 2305 2306 inp = NULL; 2307 fin = lin = NULL; 2308#ifdef INET6 2309 fin6 = lin6 = NULL; 2310#endif 2311 error = 0; 2312 2313 if (req->oldptr != NULL || req->oldlen != 0) 2314 return (EINVAL); 2315 if (req->newptr == NULL) 2316 return (EPERM); 2317 if (req->newlen < sizeof(addrs)) 2318 return (ENOMEM); 2319 error = SYSCTL_IN(req, &addrs, sizeof(addrs)); 2320 if (error) 2321 return (error); 2322 2323 switch (addrs[0].ss_family) { 2324#ifdef INET6 2325 case AF_INET6: 2326 fin6 = (struct sockaddr_in6 *)&addrs[0]; 2327 lin6 = (struct sockaddr_in6 *)&addrs[1]; 2328 if (fin6->sin6_len != sizeof(struct sockaddr_in6) || 2329 lin6->sin6_len != sizeof(struct sockaddr_in6)) 2330 return (EINVAL); 2331 if (IN6_IS_ADDR_V4MAPPED(&fin6->sin6_addr)) { 2332 if (!IN6_IS_ADDR_V4MAPPED(&lin6->sin6_addr)) 2333 return (EINVAL); 2334 in6_sin6_2_sin_in_sock((struct sockaddr *)&addrs[0]); 2335 in6_sin6_2_sin_in_sock((struct sockaddr *)&addrs[1]); 2336 fin = (struct sockaddr_in *)&addrs[0]; 2337 lin = (struct sockaddr_in *)&addrs[1]; 2338 break; 2339 } 2340 error = sa6_embedscope(fin6, ip6_use_defzone); 2341 if (error) 2342 return (error); 2343 error = sa6_embedscope(lin6, ip6_use_defzone); 2344 if (error) 2345 return (error); 2346 break; 2347#endif 2348 case AF_INET: 2349 fin = (struct sockaddr_in *)&addrs[0]; 2350 lin = (struct sockaddr_in *)&addrs[1]; 2351 if (fin->sin_len != sizeof(struct sockaddr_in) || 2352 lin->sin_len != sizeof(struct sockaddr_in)) 2353 return (EINVAL); 2354 break; 2355 default: 2356 return (EINVAL); 2357 } 2358 INP_INFO_WLOCK(&tcbinfo); 2359 switch (addrs[0].ss_family) { 2360#ifdef INET6 2361 case AF_INET6: 2362 inp = in6_pcblookup_hash(&tcbinfo, &f6, fin6->sin6_port, 2363 &l6, lin6->sin6_port, 0, NULL); 2364 break; 2365#endif 2366 case AF_INET: 2367 inp = in_pcblookup_hash(&tcbinfo, fin->sin_addr, fin->sin_port, 2368 lin->sin_addr, lin->sin_port, 0, NULL); 2369 break; 2370 } 2371 if (inp != NULL) { 2372 INP_LOCK(inp); 2373 if (inp->inp_vflag & INP_TIMEWAIT) { 2374 /* 2375 * XXXRW: There currently exists a state where an 2376 * inpcb is present, but its timewait state has been 2377 * discarded. For now, don't allow dropping of this 2378 * type of inpcb. 2379 */ 2380 tw = intotw(inp); 2381 if (tw != NULL) 2382 tcp_twclose(tw, 0); 2383 } else if (!(inp->inp_vflag & INP_DROPPED) && 2384 !(inp->inp_socket->so_options & SO_ACCEPTCONN)) { 2385 tp = intotcpcb(inp); 2386 tcp_drop(tp, ECONNABORTED); 2387 } 2388 INP_UNLOCK(inp); 2389 } else 2390 error = ESRCH; 2391 INP_INFO_WUNLOCK(&tcbinfo); 2392 return (error); 2393} 2394 2395SYSCTL_PROC(_net_inet_tcp, TCPCTL_DROP, drop, 2396 CTLTYPE_STRUCT|CTLFLAG_WR|CTLFLAG_SKIP, NULL, 2397 0, sysctl_drop, "", "Drop TCP connection"); 2398