tcp_timewait.c revision 157927
1139823Simp/*- 211150Swollman * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * 51541Srgrimes * Redistribution and use in source and binary forms, with or without 61541Srgrimes * modification, are permitted provided that the following conditions 71541Srgrimes * are met: 81541Srgrimes * 1. Redistributions of source code must retain the above copyright 91541Srgrimes * notice, this list of conditions and the following disclaimer. 101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111541Srgrimes * notice, this list of conditions and the following disclaimer in the 121541Srgrimes * documentation and/or other materials provided with the distribution. 131541Srgrimes * 4. Neither the name of the University nor the names of its contributors 141541Srgrimes * may be used to endorse or promote products derived from this software 151541Srgrimes * without specific prior written permission. 161541Srgrimes * 171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271541Srgrimes * SUCH DAMAGE. 281541Srgrimes * 2911150Swollman * @(#)tcp_subr.c 8.2 (Berkeley) 5/24/95 3050477Speter * $FreeBSD: head/sys/netinet/tcp_timewait.c 157927 2006-04-21 09:25:40Z ps $ 311541Srgrimes */ 321541Srgrimes 3332752Seivind#include "opt_compat.h" 34125680Sbms#include "opt_inet.h" 3554263Sshin#include "opt_inet6.h" 3656041Sshin#include "opt_ipsec.h" 37101106Srwatson#include "opt_mac.h" 3829514Sjoerg#include "opt_tcpdebug.h" 39130989Sps#include "opt_tcp_sack.h" 4029514Sjoerg 411541Srgrimes#include <sys/param.h> 421541Srgrimes#include <sys/systm.h> 4350673Sjlemon#include <sys/callout.h> 4412172Sphk#include <sys/kernel.h> 4512172Sphk#include <sys/sysctl.h> 46101106Srwatson#include <sys/mac.h> 471541Srgrimes#include <sys/malloc.h> 481541Srgrimes#include <sys/mbuf.h> 4955679Sshin#ifdef INET6 5055679Sshin#include <sys/domain.h> 5155679Sshin#endif 5248758Sgreen#include <sys/proc.h> 531541Srgrimes#include <sys/socket.h> 541541Srgrimes#include <sys/socketvar.h> 551541Srgrimes#include <sys/protosw.h> 5675619Skris#include <sys/random.h> 5734923Sbde 5892760Sjeff#include <vm/uma.h> 591541Srgrimes 601541Srgrimes#include <net/route.h> 611541Srgrimes#include <net/if.h> 621541Srgrimes 631541Srgrimes#include <netinet/in.h> 641541Srgrimes#include <netinet/in_systm.h> 651541Srgrimes#include <netinet/ip.h> 6655679Sshin#ifdef INET6 6755679Sshin#include <netinet/ip6.h> 6855679Sshin#endif 691541Srgrimes#include <netinet/in_pcb.h> 7055679Sshin#ifdef INET6 7155679Sshin#include <netinet6/in6_pcb.h> 7255679Sshin#endif 737090Sbde#include <netinet/in_var.h> 741541Srgrimes#include <netinet/ip_var.h> 7555679Sshin#ifdef INET6 7655679Sshin#include <netinet6/ip6_var.h> 77148385Sume#include <netinet6/scope6_var.h> 78122922Sandre#include <netinet6/nd6.h> 7955679Sshin#endif 80145360Sandre#include <netinet/ip_icmp.h> 811541Srgrimes#include <netinet/tcp.h> 821541Srgrimes#include <netinet/tcp_fsm.h> 831541Srgrimes#include <netinet/tcp_seq.h> 841541Srgrimes#include <netinet/tcp_timer.h> 851541Srgrimes#include <netinet/tcp_var.h> 8655679Sshin#ifdef INET6 8755679Sshin#include <netinet6/tcp6_var.h> 8855679Sshin#endif 891541Srgrimes#include <netinet/tcpip.h> 906283Swollman#ifdef TCPDEBUG 916283Swollman#include <netinet/tcp_debug.h> 926283Swollman#endif 9355679Sshin#include <netinet6/ip6protosw.h> 941541Srgrimes 9555679Sshin#ifdef IPSEC 9655679Sshin#include <netinet6/ipsec.h> 9762587Sitojun#ifdef INET6 9862587Sitojun#include <netinet6/ipsec6.h> 9962587Sitojun#endif 100137396Ssuz#include <netkey/key.h> 10155679Sshin#endif /*IPSEC*/ 10255679Sshin 103105199Ssam#ifdef FAST_IPSEC 104105199Ssam#include <netipsec/ipsec.h> 105125680Sbms#include <netipsec/xform.h> 106105199Ssam#ifdef INET6 107105199Ssam#include <netipsec/ipsec6.h> 108105199Ssam#endif 109125680Sbms#include <netipsec/key.h> 110105199Ssam#define IPSEC 111105199Ssam#endif /*FAST_IPSEC*/ 112105199Ssam 11358698Sjlemon#include <machine/in_cksum.h> 11482122Ssilby#include <sys/md5.h> 11558698Sjlemon 116133874Srwatsonint tcp_mssdflt = TCP_MSS; 117133874SrwatsonSYSCTL_INT(_net_inet_tcp, TCPCTL_MSSDFLT, mssdflt, CTLFLAG_RW, 11846381Sbillf &tcp_mssdflt , 0, "Default TCP Maximum Segment Size"); 11912296Sphk 12052904Sshin#ifdef INET6 12152904Sshinint tcp_v6mssdflt = TCP6_MSS; 12252904SshinSYSCTL_INT(_net_inet_tcp, TCPCTL_V6MSSDFLT, v6mssdflt, 12355679Sshin CTLFLAG_RW, &tcp_v6mssdflt , 0, 12455679Sshin "Default TCP Maximum Segment Size for IPv6"); 12552904Sshin#endif 12652904Sshin 127124258Sandre/* 128124258Sandre * Minimum MSS we accept and use. This prevents DoS attacks where 129124258Sandre * we are forced to a ridiculous low MSS like 20 and send hundreds 130124258Sandre * of packets instead of one. The effect scales with the available 131124258Sandre * bandwidth and quickly saturates the CPU and network interface 132124258Sandre * with packet generation and sending. Set to zero to disable MINMSS 133124258Sandre * checking. This setting prevents us from sending too small packets. 134124258Sandre */ 135124258Sandreint tcp_minmss = TCP_MINMSS; 136124258SandreSYSCTL_INT(_net_inet_tcp, OID_AUTO, minmss, CTLFLAG_RW, 137124258Sandre &tcp_minmss , 0, "Minmum TCP Maximum Segment Size"); 138124258Sandre/* 139124258Sandre * Number of TCP segments per second we accept from remote host 140124258Sandre * before we start to calculate average segment size. If average 141124258Sandre * segment size drops below the minimum TCP MSS we assume a DoS 142124258Sandre * attack and reset+drop the connection. Care has to be taken not to 143124258Sandre * set this value too small to not kill interactive type connections 144124258Sandre * (telnet, SSH) which send many small packets. 145124258Sandre */ 146124258Sandreint tcp_minmssoverload = TCP_MINMSSOVERLOAD; 147124258SandreSYSCTL_INT(_net_inet_tcp, OID_AUTO, minmssoverload, CTLFLAG_RW, 148124258Sandre &tcp_minmssoverload , 0, "Number of TCP Segments per Second allowed to" 149124258Sandre "be under the MINMSS Size"); 150124258Sandre 15150673Sjlemon#if 0 152133874Srwatsonstatic int tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ; 153133874SrwatsonSYSCTL_INT(_net_inet_tcp, TCPCTL_RTTDFLT, rttdflt, CTLFLAG_RW, 15446381Sbillf &tcp_rttdflt , 0, "Default maximum TCP Round Trip Time"); 15550673Sjlemon#endif 15612296Sphk 15786764Sjlemonint tcp_do_rfc1323 = 1; 158133874SrwatsonSYSCTL_INT(_net_inet_tcp, TCPCTL_DO_RFC1323, rfc1323, CTLFLAG_RW, 15946381Sbillf &tcp_do_rfc1323 , 0, "Enable rfc1323 (high performance TCP) extensions"); 16012296Sphk 16150426Sjlemonstatic int tcp_tcbhashsize = 0; 162121307SsilbySYSCTL_INT(_net_inet_tcp, OID_AUTO, tcbhashsize, CTLFLAG_RDTUN, 16350426Sjlemon &tcp_tcbhashsize, 0, "Size of TCP control-block hashtable"); 16450426Sjlemon 16555198Smsmithstatic int do_tcpdrain = 1; 16666376SbmilekicSYSCTL_INT(_net_inet_tcp, OID_AUTO, do_tcpdrain, CTLFLAG_RW, &do_tcpdrain, 0, 16766376Sbmilekic "Enable tcp_drain routine for extra help when low on mbufs"); 16855198Smsmith 169133874SrwatsonSYSCTL_INT(_net_inet_tcp, OID_AUTO, pcbcount, CTLFLAG_RD, 17046381Sbillf &tcbinfo.ipi_count, 0, "Number of active PCBs"); 17136079Swollman 17272959Sjlemonstatic int icmp_may_rst = 1; 173133874SrwatsonSYSCTL_INT(_net_inet_tcp, OID_AUTO, icmp_may_rst, CTLFLAG_RW, &icmp_may_rst, 0, 17472959Sjlemon "Certain ICMP unreachable messages may abort connections in SYN_SENT"); 17570103Sphk 17682122Ssilbystatic int tcp_isn_reseed_interval = 0; 17782122SsilbySYSCTL_INT(_net_inet_tcp, OID_AUTO, isn_reseed_interval, CTLFLAG_RW, 17882122Ssilby &tcp_isn_reseed_interval, 0, "Seconds between reseeding of ISN secret"); 17982122Ssilby 180157478Sglebiusstatic int maxtcptw; 181157478SglebiusSYSCTL_INT(_net_inet_tcp, OID_AUTO, maxtcptw, CTLFLAG_RDTUN, 182157478Sglebius &maxtcptw, 0, "Maximum number of compressed TCP TIME_WAIT entries"); 183157478Sglebius 184102017Sdillon/* 185133874Srwatson * TCP bandwidth limiting sysctls. Note that the default lower bound of 186133874Srwatson * 1024 exists only for debugging. A good production default would be 187102017Sdillon * something like 6100. 188102017Sdillon */ 189133072SandreSYSCTL_NODE(_net_inet_tcp, OID_AUTO, inflight, CTLFLAG_RW, 0, 190133072Sandre "TCP inflight data limiting"); 191133072Sandre 192124199Sandrestatic int tcp_inflight_enable = 1; 193133072SandreSYSCTL_INT(_net_inet_tcp_inflight, OID_AUTO, enable, CTLFLAG_RW, 194102017Sdillon &tcp_inflight_enable, 0, "Enable automatic TCP inflight data limiting"); 195102017Sdillon 196104825Sdillonstatic int tcp_inflight_debug = 0; 197133072SandreSYSCTL_INT(_net_inet_tcp_inflight, OID_AUTO, debug, CTLFLAG_RW, 198102017Sdillon &tcp_inflight_debug, 0, "Debug TCP inflight calculations"); 199102017Sdillon 200155767Sandrestatic int tcp_inflight_rttthresh; 201155767SandreSYSCTL_PROC(_net_inet_tcp_inflight, OID_AUTO, rttthresh, CTLTYPE_INT|CTLFLAG_RW, 202155767Sandre &tcp_inflight_rttthresh, 0, sysctl_msec_to_ticks, "I", 203155767Sandre "RTT threshold below which inflight will deactivate itself"); 204155767Sandre 205107881Sdillonstatic int tcp_inflight_min = 6144; 206133072SandreSYSCTL_INT(_net_inet_tcp_inflight, OID_AUTO, min, CTLFLAG_RW, 207102017Sdillon &tcp_inflight_min, 0, "Lower-bound for TCP inflight window"); 208102017Sdillon 209102017Sdillonstatic int tcp_inflight_max = TCP_MAXWIN << TCP_MAX_WINSHIFT; 210133072SandreSYSCTL_INT(_net_inet_tcp_inflight, OID_AUTO, max, CTLFLAG_RW, 211102017Sdillon &tcp_inflight_max, 0, "Upper-bound for TCP inflight window"); 212133072Sandre 213107881Sdillonstatic int tcp_inflight_stab = 20; 214133072SandreSYSCTL_INT(_net_inet_tcp_inflight, OID_AUTO, stab, CTLFLAG_RW, 215107881Sdillon &tcp_inflight_stab, 0, "Inflight Algorithm Stabilization 20 = 2 packets"); 216102017Sdillon 217130989Spsuma_zone_t sack_hole_zone; 218130989Sps 21998211Shsustatic struct inpcb *tcp_notify(struct inpcb *, int); 220128452Ssilbystatic void tcp_isn_tick(void *); 22112296Sphk 2227684Sdg/* 22332821Sdg * Target size of TCP PCB hash tables. Must be a power of two. 22443562Smsmith * 22543562Smsmith * Note that this can be overridden by the kernel environment 22643562Smsmith * variable net.inet.tcp.tcbhashsize 2277684Sdg */ 2287684Sdg#ifndef TCBHASHSIZE 22932821Sdg#define TCBHASHSIZE 512 2307684Sdg#endif 2311541Srgrimes 2321541Srgrimes/* 233111145Sjlemon * XXX 234111145Sjlemon * Callouts should be moved into struct tcp directly. They are currently 235123608Sjhb * separate because the tcpcb structure is exported to userland for sysctl 236111145Sjlemon * parsing purposes, which do not know about callouts. 23734881Swollman */ 238111145Sjlemonstruct tcpcb_mem { 23934881Swollman struct tcpcb tcb; 240111145Sjlemon struct callout tcpcb_mem_rexmt, tcpcb_mem_persist, tcpcb_mem_keep; 241111145Sjlemon struct callout tcpcb_mem_2msl, tcpcb_mem_delack; 24234881Swollman}; 24334881Swollman 244111145Sjlemonstatic uma_zone_t tcpcb_zone; 245111145Sjlemonstatic uma_zone_t tcptw_zone; 246128452Ssilbystruct callout isn_callout; 247111145Sjlemon 24834881Swollman/* 249157431Srwatson * TCP initialization. 2501541Srgrimes */ 251157927Spsstatic void 252157927Spstcp_zone_change(void *tag) 253157927Sps{ 254157927Sps 255157927Sps uma_zone_set_max(tcbinfo.ipi_zone, maxsockets); 256157927Sps uma_zone_set_max(tcpcb_zone, maxsockets); 257157927Sps uma_zone_set_max(tcptw_zone, maxsockets / 5); 258157927Sps} 259157927Sps 2601541Srgrimesvoid 261157431Srwatsontcp_init(void) 2621541Srgrimes{ 26377843Speter int hashsize = TCBHASHSIZE; 264133874Srwatson 26550673Sjlemon tcp_delacktime = TCPTV_DELACK; 26650673Sjlemon tcp_keepinit = TCPTV_KEEP_INIT; 26750673Sjlemon tcp_keepidle = TCPTV_KEEP_IDLE; 26850673Sjlemon tcp_keepintvl = TCPTV_KEEPINTVL; 26950673Sjlemon tcp_maxpersistidle = TCPTV_KEEP_IDLE; 27050673Sjlemon tcp_msl = TCPTV_MSL; 271100335Sdillon tcp_rexmit_min = TCPTV_MIN; 272100335Sdillon tcp_rexmit_slop = TCPTV_CPU_VAR; 273155767Sandre tcp_inflight_rttthresh = TCPTV_INFLIGHT_RTTTHRESH; 27450673Sjlemon 27598102Shsu INP_INFO_LOCK_INIT(&tcbinfo, "tcp"); 2767684Sdg LIST_INIT(&tcb); 2777684Sdg tcbinfo.listhead = &tcb; 27877900Speter TUNABLE_INT_FETCH("net.inet.tcp.tcbhashsize", &hashsize); 27943576Smsmith if (!powerof2(hashsize)) { 28043562Smsmith printf("WARNING: TCB hash size not a power of 2\n"); 28143562Smsmith hashsize = 512; /* safe default */ 28243562Smsmith } 28350426Sjlemon tcp_tcbhashsize = hashsize; 28443562Smsmith tcbinfo.hashbase = hashinit(hashsize, M_PCB, &tcbinfo.hashmask); 28543562Smsmith tcbinfo.porthashbase = hashinit(hashsize, M_PCB, 28634923Sbde &tcbinfo.porthashmask); 287133874Srwatson tcbinfo.ipi_zone = uma_zcreate("inpcb", sizeof(struct inpcb), 288133517Sandre NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 28992760Sjeff uma_zone_set_max(tcbinfo.ipi_zone, maxsockets); 29055679Sshin#ifdef INET6 29155679Sshin#define TCP_MINPROTOHDR (sizeof(struct ip6_hdr) + sizeof(struct tcphdr)) 29255679Sshin#else /* INET6 */ 29355679Sshin#define TCP_MINPROTOHDR (sizeof(struct tcpiphdr)) 29455679Sshin#endif /* INET6 */ 29555679Sshin if (max_protohdr < TCP_MINPROTOHDR) 29655679Sshin max_protohdr = TCP_MINPROTOHDR; 29755679Sshin if (max_linkhdr + TCP_MINPROTOHDR > MHLEN) 2981541Srgrimes panic("tcp_init"); 29955679Sshin#undef TCP_MINPROTOHDR 300111145Sjlemon /* 301111145Sjlemon * These have to be type stable for the benefit of the timers. 302111145Sjlemon */ 303133874Srwatson tcpcb_zone = uma_zcreate("tcpcb", sizeof(struct tcpcb_mem), 304133517Sandre NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 305111145Sjlemon uma_zone_set_max(tcpcb_zone, maxsockets); 306157478Sglebius TUNABLE_INT_FETCH("net.inet.tcp.maxtcptw", &maxtcptw); 307157478Sglebius if (maxtcptw == 0) 308157478Sglebius maxtcptw = maxsockets / 5; 309133874Srwatson tcptw_zone = uma_zcreate("tcptw", sizeof(struct tcptw), 310133517Sandre NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 311157478Sglebius uma_zone_set_max(tcptw_zone, maxtcptw); 312112009Sjlemon tcp_timer_init(); 31386764Sjlemon syncache_init(); 314122922Sandre tcp_hc_init(); 315126193Sandre tcp_reass_init(); 316128452Ssilby callout_init(&isn_callout, CALLOUT_MPSAFE); 317128452Ssilby tcp_isn_tick(NULL); 318128452Ssilby EVENTHANDLER_REGISTER(shutdown_pre_sync, tcp_fini, NULL, 319128452Ssilby SHUTDOWN_PRI_DEFAULT); 320133874Srwatson sack_hole_zone = uma_zcreate("sackhole", sizeof(struct sackhole), 321133517Sandre NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 322157927Sps EVENTHANDLER_REGISTER(maxsockets_change, tcp_zone_change, NULL, 323157927Sps EVENTHANDLER_PRI_ANY); 3241541Srgrimes} 3251541Srgrimes 326128452Ssilbyvoid 327157431Srwatsontcp_fini(void *xtp) 328128452Ssilby{ 329157431Srwatson 330128452Ssilby callout_stop(&isn_callout); 331128452Ssilby} 332128452Ssilby 3331541Srgrimes/* 33478642Ssilby * Fill in the IP and TCP headers for an outgoing packet, given the tcpcb. 33578642Ssilby * tcp_template used to store this data in mbufs, but we now recopy it out 33678642Ssilby * of the tcpcb each time to conserve mbufs. 3371541Srgrimes */ 33878642Ssilbyvoid 339157431Srwatsontcpip_fillheaders(struct inpcb *inp, void *ip_ptr, void *tcp_ptr) 3401541Srgrimes{ 341111144Sjlemon struct tcphdr *th = (struct tcphdr *)tcp_ptr; 3421541Srgrimes 343138410Srwatson INP_LOCK_ASSERT(inp); 344138410Srwatson 34555679Sshin#ifdef INET6 34655679Sshin if ((inp->inp_vflag & INP_IPV6) != 0) { 34778642Ssilby struct ip6_hdr *ip6; 34855679Sshin 34978642Ssilby ip6 = (struct ip6_hdr *)ip_ptr; 35055679Sshin ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) | 35155679Sshin (inp->in6p_flowinfo & IPV6_FLOWINFO_MASK); 35255679Sshin ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) | 35355679Sshin (IPV6_VERSION & IPV6_VERSION_MASK); 35455679Sshin ip6->ip6_nxt = IPPROTO_TCP; 35555679Sshin ip6->ip6_plen = sizeof(struct tcphdr); 35655679Sshin ip6->ip6_src = inp->in6p_laddr; 35755679Sshin ip6->ip6_dst = inp->in6p_faddr; 35855679Sshin } else 35955679Sshin#endif 36078642Ssilby { 361111144Sjlemon struct ip *ip; 36255679Sshin 363111144Sjlemon ip = (struct ip *)ip_ptr; 364111144Sjlemon ip->ip_v = IPVERSION; 365111144Sjlemon ip->ip_hl = 5; 366111144Sjlemon ip->ip_tos = inp->inp_ip_tos; 367111144Sjlemon ip->ip_len = 0; 368111144Sjlemon ip->ip_id = 0; 369111144Sjlemon ip->ip_off = 0; 370111144Sjlemon ip->ip_ttl = inp->inp_ip_ttl; 371111144Sjlemon ip->ip_sum = 0; 372111144Sjlemon ip->ip_p = IPPROTO_TCP; 373111144Sjlemon ip->ip_src = inp->inp_laddr; 374111144Sjlemon ip->ip_dst = inp->inp_faddr; 37578642Ssilby } 376111144Sjlemon th->th_sport = inp->inp_lport; 377111144Sjlemon th->th_dport = inp->inp_fport; 378111144Sjlemon th->th_seq = 0; 379111144Sjlemon th->th_ack = 0; 380111144Sjlemon th->th_x2 = 0; 381111144Sjlemon th->th_off = 5; 382111144Sjlemon th->th_flags = 0; 383111144Sjlemon th->th_win = 0; 384111144Sjlemon th->th_urp = 0; 385111144Sjlemon th->th_sum = 0; /* in_pseudo() is called later for ipv4 */ 38678642Ssilby} 38778642Ssilby 38878642Ssilby/* 38978642Ssilby * Create template to be used to send tcp packets on a connection. 39078642Ssilby * Allocates an mbuf and fills in a skeletal tcp/ip header. The only 39178642Ssilby * use for this function is in keepalives, which use tcp_respond. 39278642Ssilby */ 39378642Ssilbystruct tcptemp * 394157431Srwatsontcpip_maketemplate(struct inpcb *inp) 39578642Ssilby{ 39678642Ssilby struct mbuf *m; 39778642Ssilby struct tcptemp *n; 39878642Ssilby 399149635Sandre m = m_get(M_DONTWAIT, MT_DATA); 40078642Ssilby if (m == NULL) 40178642Ssilby return (0); 40278642Ssilby m->m_len = sizeof(struct tcptemp); 40378642Ssilby n = mtod(m, struct tcptemp *); 40478642Ssilby 405111144Sjlemon tcpip_fillheaders(inp, (void *)&n->tt_ipgen, (void *)&n->tt_t); 4061541Srgrimes return (n); 4071541Srgrimes} 4081541Srgrimes 4091541Srgrimes/* 4101541Srgrimes * Send a single message to the TCP at address specified by 411127870Srwatson * the given TCP/IP header. If m == NULL, then we make a copy 4121541Srgrimes * of the tcpiphdr at ti and send directly to the addressed host. 4131541Srgrimes * This is used to force keep alive messages out using the TCP 41478642Ssilby * template for a connection. If flags are given then we send 41578642Ssilby * a message back to the TCP which originated the * segment ti, 41678642Ssilby * and discard the mbuf containing it and any other attached mbufs. 4171541Srgrimes * 4181541Srgrimes * In any case the ack and sequence number of the transmitted 4191541Srgrimes * segment are as specified by the parameters. 42031848Sjulian * 42131848Sjulian * NOTE: If m != NULL, then ti must point to *inside* the mbuf. 4221541Srgrimes */ 4231541Srgrimesvoid 424157431Srwatsontcp_respond(struct tcpcb *tp, void *ipgen, register struct tcphdr *th, 425157431Srwatson register struct mbuf *m, tcp_seq ack, tcp_seq seq, int flags) 4261541Srgrimes{ 4271541Srgrimes register int tlen; 4281541Srgrimes int win = 0; 42955679Sshin struct ip *ip; 43055679Sshin struct tcphdr *nth; 43155679Sshin#ifdef INET6 43255679Sshin struct ip6_hdr *ip6; 43355679Sshin int isipv6; 43455679Sshin#endif /* INET6 */ 43555679Sshin int ipflags = 0; 436128905Srwatson struct inpcb *inp; 4371541Srgrimes 438101137Srwatson KASSERT(tp != NULL || m != NULL, ("tcp_respond: tp and m both NULL")); 439101137Srwatson 44055679Sshin#ifdef INET6 441105586Sphk isipv6 = ((struct ip *)ipgen)->ip_v == 6; 44255679Sshin ip6 = ipgen; 44355679Sshin#endif /* INET6 */ 44455679Sshin ip = ipgen; 44555679Sshin 446127871Srwatson if (tp != NULL) { 447122327Ssam inp = tp->t_inpcb; 448122327Ssam KASSERT(inp != NULL, ("tcp control block w/o inpcb")); 449122327Ssam INP_INFO_WLOCK_ASSERT(&tcbinfo); 450122327Ssam INP_LOCK_ASSERT(inp); 451128905Srwatson } else 452128905Srwatson inp = NULL; 453128905Srwatson 454128905Srwatson if (tp != NULL) { 45557576Sps if (!(flags & TH_RST)) { 456122327Ssam win = sbspace(&inp->inp_socket->so_rcv); 45757576Sps if (win > (long)TCP_MAXWIN << tp->rcv_scale) 45857576Sps win = (long)TCP_MAXWIN << tp->rcv_scale; 45957576Sps } 4601541Srgrimes } 461127870Srwatson if (m == NULL) { 462151967Sandre m = m_gethdr(M_DONTWAIT, MT_DATA); 4631541Srgrimes if (m == NULL) 4641541Srgrimes return; 4651541Srgrimes tlen = 0; 4661541Srgrimes m->m_data += max_linkhdr; 46755679Sshin#ifdef INET6 46855679Sshin if (isipv6) { 469133874Srwatson bcopy((caddr_t)ip6, mtod(m, caddr_t), 47055679Sshin sizeof(struct ip6_hdr)); 47155679Sshin ip6 = mtod(m, struct ip6_hdr *); 47255679Sshin nth = (struct tcphdr *)(ip6 + 1); 47355679Sshin } else 47455679Sshin#endif /* INET6 */ 47555679Sshin { 47655679Sshin bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip)); 47755679Sshin ip = mtod(m, struct ip *); 47855679Sshin nth = (struct tcphdr *)(ip + 1); 47955679Sshin } 48055679Sshin bcopy((caddr_t)th, (caddr_t)nth, sizeof(struct tcphdr)); 4811541Srgrimes flags = TH_ACK; 4821541Srgrimes } else { 4831541Srgrimes m_freem(m->m_next); 484127870Srwatson m->m_next = NULL; 48555679Sshin m->m_data = (caddr_t)ipgen; 48655679Sshin /* m_len is set later */ 4871541Srgrimes tlen = 0; 4881541Srgrimes#define xchg(a,b,type) { type t; t=a; a=b; b=t; } 48955679Sshin#ifdef INET6 49055679Sshin if (isipv6) { 49155679Sshin xchg(ip6->ip6_dst, ip6->ip6_src, struct in6_addr); 49255679Sshin nth = (struct tcphdr *)(ip6 + 1); 49355679Sshin } else 49455679Sshin#endif /* INET6 */ 49555679Sshin { 49655679Sshin xchg(ip->ip_dst.s_addr, ip->ip_src.s_addr, n_long); 49755679Sshin nth = (struct tcphdr *)(ip + 1); 49855679Sshin } 49955679Sshin if (th != nth) { 50055679Sshin /* 50155679Sshin * this is usually a case when an extension header 50255679Sshin * exists between the IPv6 header and the 50355679Sshin * TCP header. 50455679Sshin */ 50555679Sshin nth->th_sport = th->th_sport; 50655679Sshin nth->th_dport = th->th_dport; 50755679Sshin } 50855679Sshin xchg(nth->th_dport, nth->th_sport, n_short); 5091541Srgrimes#undef xchg 5101541Srgrimes } 51155679Sshin#ifdef INET6 51255679Sshin if (isipv6) { 51390198Sume ip6->ip6_flow = 0; 51490198Sume ip6->ip6_vfc = IPV6_VERSION; 51590198Sume ip6->ip6_nxt = IPPROTO_TCP; 51655679Sshin ip6->ip6_plen = htons((u_short)(sizeof (struct tcphdr) + 51755679Sshin tlen)); 51855679Sshin tlen += sizeof (struct ip6_hdr) + sizeof (struct tcphdr); 51956039Sshin } else 52055679Sshin#endif 521133874Srwatson { 522133874Srwatson tlen += sizeof (struct tcpiphdr); 523133874Srwatson ip->ip_len = tlen; 524133874Srwatson ip->ip_ttl = ip_defttl; 525133874Srwatson if (path_mtu_discovery) 526133874Srwatson ip->ip_off |= IP_DF; 527133874Srwatson } 5281541Srgrimes m->m_len = tlen; 5291541Srgrimes m->m_pkthdr.len = tlen; 530127870Srwatson m->m_pkthdr.rcvif = NULL; 531101106Srwatson#ifdef MAC 532122327Ssam if (inp != NULL) { 533101106Srwatson /* 534101106Srwatson * Packet is associated with a socket, so allow the 535101106Srwatson * label of the response to reflect the socket label. 536101106Srwatson */ 537128905Srwatson INP_LOCK_ASSERT(inp); 538128905Srwatson mac_create_mbuf_from_inpcb(inp, m); 539101106Srwatson } else { 540101106Srwatson /* 541119245Srwatson * Packet is not associated with a socket, so possibly 542119245Srwatson * update the label in place. 543101106Srwatson */ 544119245Srwatson mac_reflect_mbuf_tcp(m); 545101106Srwatson } 546101106Srwatson#endif 54755679Sshin nth->th_seq = htonl(seq); 54855679Sshin nth->th_ack = htonl(ack); 54955679Sshin nth->th_x2 = 0; 55055679Sshin nth->th_off = sizeof (struct tcphdr) >> 2; 55155679Sshin nth->th_flags = flags; 552127870Srwatson if (tp != NULL) 55355679Sshin nth->th_win = htons((u_short) (win >> tp->rcv_scale)); 5541541Srgrimes else 55555679Sshin nth->th_win = htons((u_short)win); 55655679Sshin nth->th_urp = 0; 55755679Sshin#ifdef INET6 55855679Sshin if (isipv6) { 55959392Sshin nth->th_sum = 0; 56055679Sshin nth->th_sum = in6_cksum(m, IPPROTO_TCP, 56155679Sshin sizeof(struct ip6_hdr), 56255679Sshin tlen - sizeof(struct ip6_hdr)); 563127870Srwatson ip6->ip6_hlim = in6_selecthlim(tp != NULL ? tp->t_inpcb : 564127870Srwatson NULL, NULL); 56555679Sshin } else 56655679Sshin#endif /* INET6 */ 567133874Srwatson { 568133874Srwatson nth->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 569133874Srwatson htons((u_short)(tlen - sizeof(struct ip) + ip->ip_p))); 570133874Srwatson m->m_pkthdr.csum_flags = CSUM_TCP; 571133874Srwatson m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 572133874Srwatson } 5736283Swollman#ifdef TCPDEBUG 574122327Ssam if (tp == NULL || (inp->inp_socket->so_options & SO_DEBUG)) 57555679Sshin tcp_trace(TA_OUTPUT, 0, tp, mtod(m, void *), th, 0); 5766283Swollman#endif 57755679Sshin#ifdef INET6 578122922Sandre if (isipv6) 579122922Sandre (void) ip6_output(m, NULL, NULL, ipflags, NULL, NULL, inp); 580122922Sandre else 58155679Sshin#endif /* INET6 */ 582122922Sandre (void) ip_output(m, NULL, NULL, ipflags, NULL, inp); 5831541Srgrimes} 5841541Srgrimes 5851541Srgrimes/* 5861541Srgrimes * Create a new TCP control block, making an 5871541Srgrimes * empty reassembly queue and hooking it to the argument 58834881Swollman * protocol control block. The `inp' parameter must have 58934881Swollman * come from the zone allocator set up in tcp_init(). 5901541Srgrimes */ 5911541Srgrimesstruct tcpcb * 592157431Srwatsontcp_newtcpcb(struct inpcb *inp) 5931541Srgrimes{ 594111145Sjlemon struct tcpcb_mem *tm; 595111145Sjlemon struct tcpcb *tp; 59655679Sshin#ifdef INET6 59755679Sshin int isipv6 = (inp->inp_vflag & INP_IPV6) != 0; 59855679Sshin#endif /* INET6 */ 5991541Srgrimes 600111145Sjlemon tm = uma_zalloc(tcpcb_zone, M_NOWAIT | M_ZERO); 601111145Sjlemon if (tm == NULL) 602111145Sjlemon return (NULL); 603111145Sjlemon tp = &tm->tcb; 604111145Sjlemon /* LIST_INIT(&tp->t_segq); */ /* XXX covered by M_ZERO */ 60555679Sshin tp->t_maxseg = tp->t_maxopd = 60655679Sshin#ifdef INET6 60755679Sshin isipv6 ? tcp_v6mssdflt : 60855679Sshin#endif /* INET6 */ 60955679Sshin tcp_mssdflt; 6101541Srgrimes 61150673Sjlemon /* Set up our timeouts. */ 612142906Sglebius callout_init(tp->tt_rexmt = &tm->tcpcb_mem_rexmt, NET_CALLOUT_MPSAFE); 613142906Sglebius callout_init(tp->tt_persist = &tm->tcpcb_mem_persist, NET_CALLOUT_MPSAFE); 614142906Sglebius callout_init(tp->tt_keep = &tm->tcpcb_mem_keep, NET_CALLOUT_MPSAFE); 615142906Sglebius callout_init(tp->tt_2msl = &tm->tcpcb_mem_2msl, NET_CALLOUT_MPSAFE); 616142906Sglebius callout_init(tp->tt_delack = &tm->tcpcb_mem_delack, NET_CALLOUT_MPSAFE); 61750673Sjlemon 6186283Swollman if (tcp_do_rfc1323) 6196283Swollman tp->t_flags = (TF_REQ_SCALE|TF_REQ_TSTMP); 620130989Sps tp->sack_enable = tcp_do_sack; 621147735Sps TAILQ_INIT(&tp->snd_holes); 62234881Swollman tp->t_inpcb = inp; /* XXX */ 6231541Srgrimes /* 6241541Srgrimes * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no 62516367Swollman * rtt estimate. Set rttvar so that srtt + 4 * rttvar gives 6261541Srgrimes * reasonable initial retransmit time. 6271541Srgrimes */ 6281541Srgrimes tp->t_srtt = TCPTV_SRTTBASE; 62916367Swollman tp->t_rttvar = ((TCPTV_RTOBASE - TCPTV_SRTTBASE) << TCP_RTTVAR_SHIFT) / 4; 630100335Sdillon tp->t_rttmin = tcp_rexmit_min; 63116367Swollman tp->t_rxtcur = TCPTV_RTOBASE; 6321541Srgrimes tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT; 633102017Sdillon tp->snd_bwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT; 6341541Srgrimes tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT; 63550673Sjlemon tp->t_rcvtime = ticks; 636102017Sdillon tp->t_bw_rtttime = ticks; 637133874Srwatson /* 63856564Sshin * IPv4 TTL initialization is necessary for an IPv6 socket as well, 63956564Sshin * because the socket may be bound to an IPv6 wildcard address, 64056564Sshin * which may match an IPv4-mapped IPv6 address. 64156564Sshin */ 64224570Sdg inp->inp_ip_ttl = ip_defttl; 643157432Srwatson inp->inp_ppcb = tp; 64434881Swollman return (tp); /* XXX */ 6451541Srgrimes} 6461541Srgrimes 6471541Srgrimes/* 6481541Srgrimes * Drop a TCP connection, reporting 6491541Srgrimes * the specified error. If connection is synchronized, 6501541Srgrimes * then send a RST to peer. 6511541Srgrimes */ 6521541Srgrimesstruct tcpcb * 653157431Srwatsontcp_drop(struct tcpcb *tp, int errno) 6541541Srgrimes{ 6551541Srgrimes struct socket *so = tp->t_inpcb->inp_socket; 6561541Srgrimes 657146864Srwatson INP_INFO_WLOCK_ASSERT(&tcbinfo); 658138019Srwatson INP_LOCK_ASSERT(tp->t_inpcb); 659146864Srwatson 6601541Srgrimes if (TCPS_HAVERCVDSYN(tp->t_state)) { 6611541Srgrimes tp->t_state = TCPS_CLOSED; 6621541Srgrimes (void) tcp_output(tp); 6631541Srgrimes tcpstat.tcps_drops++; 6641541Srgrimes } else 6651541Srgrimes tcpstat.tcps_conndrops++; 6661541Srgrimes if (errno == ETIMEDOUT && tp->t_softerror) 6671541Srgrimes errno = tp->t_softerror; 6681541Srgrimes so->so_error = errno; 6691541Srgrimes return (tcp_close(tp)); 6701541Srgrimes} 6711541Srgrimes 672157376Srwatsonvoid 673157431Srwatsontcp_discardcb(struct tcpcb *tp) 6741541Srgrimes{ 675111145Sjlemon struct tseg_qent *q; 6761541Srgrimes struct inpcb *inp = tp->t_inpcb; 6771541Srgrimes struct socket *so = inp->inp_socket; 67855679Sshin#ifdef INET6 67955679Sshin int isipv6 = (inp->inp_vflag & INP_IPV6) != 0; 68055679Sshin#endif /* INET6 */ 6811541Srgrimes 682157376Srwatson /* 683157376Srwatson * XXXRW: This is all very well and good, but actually, we might be 684157376Srwatson * discarding the tcpcb after the socket is gone, so we can't do 685157376Srwatson * this: 686157376Srwatson KASSERT(so != NULL, ("tcp_discardcb: so == NULL")); 687157376Srwatson */ 688138410Srwatson INP_LOCK_ASSERT(inp); 689138410Srwatson 6901541Srgrimes /* 69150673Sjlemon * Make sure that all of our timers are stopped before we 69250673Sjlemon * delete the PCB. 69350673Sjlemon */ 69450673Sjlemon callout_stop(tp->tt_rexmt); 69550673Sjlemon callout_stop(tp->tt_persist); 69650673Sjlemon callout_stop(tp->tt_keep); 69750673Sjlemon callout_stop(tp->tt_2msl); 69850673Sjlemon callout_stop(tp->tt_delack); 69950673Sjlemon 70050673Sjlemon /* 7019373Swollman * If we got enough samples through the srtt filter, 7029373Swollman * save the rtt and rttvar in the routing entry. 703122922Sandre * 'Enough' is arbitrarily defined as 4 rtt samples. 704122922Sandre * 4 samples is enough for the srtt filter to converge 705122922Sandre * to within enough % of the correct value; fewer samples 706122922Sandre * and we could save a bogus rtt. The danger is not high 707122922Sandre * as tcp quickly recovers from everything. 708122922Sandre * XXX: Works very well but needs some more statistics! 7091541Srgrimes */ 710122922Sandre if (tp->t_rttupdated >= 4) { 711122922Sandre struct hc_metrics_lite metrics; 712122922Sandre u_long ssthresh; 7131541Srgrimes 714122922Sandre bzero(&metrics, sizeof(metrics)); 7151541Srgrimes /* 716122922Sandre * Update the ssthresh always when the conditions below 717122922Sandre * are satisfied. This gives us better new start value 718122922Sandre * for the congestion avoidance for new connections. 719122922Sandre * ssthresh is only set if packet loss occured on a session. 7201541Srgrimes */ 721122922Sandre ssthresh = tp->snd_ssthresh; 722122922Sandre if (ssthresh != 0 && ssthresh < so->so_snd.sb_hiwat / 2) { 7231541Srgrimes /* 7241541Srgrimes * convert the limit from user data bytes to 7251541Srgrimes * packets then to packet data bytes. 7261541Srgrimes */ 727122922Sandre ssthresh = (ssthresh + tp->t_maxseg / 2) / tp->t_maxseg; 728122922Sandre if (ssthresh < 2) 729122922Sandre ssthresh = 2; 730122922Sandre ssthresh *= (u_long)(tp->t_maxseg + 73155679Sshin#ifdef INET6 73255679Sshin (isipv6 ? sizeof (struct ip6_hdr) + 73355679Sshin sizeof (struct tcphdr) : 73455679Sshin#endif 73555679Sshin sizeof (struct tcpiphdr) 73655679Sshin#ifdef INET6 73755679Sshin ) 73855679Sshin#endif 73955679Sshin ); 740122922Sandre } else 741122922Sandre ssthresh = 0; 742122922Sandre metrics.rmx_ssthresh = ssthresh; 743122922Sandre 744122922Sandre metrics.rmx_rtt = tp->t_srtt; 745122922Sandre metrics.rmx_rttvar = tp->t_rttvar; 746122922Sandre /* XXX: This wraps if the pipe is more than 4 Gbit per second */ 747122922Sandre metrics.rmx_bandwidth = tp->snd_bandwidth; 748122922Sandre metrics.rmx_cwnd = tp->snd_cwnd; 749133874Srwatson metrics.rmx_sendpipe = 0; 750122922Sandre metrics.rmx_recvpipe = 0; 751122922Sandre 752122922Sandre tcp_hc_update(&inp->inp_inc, &metrics); 7531541Srgrimes } 754122922Sandre 7551541Srgrimes /* free the reassembly queue, if any */ 756111145Sjlemon while ((q = LIST_FIRST(&tp->t_segq)) != NULL) { 75755679Sshin LIST_REMOVE(q, tqe_q); 75855679Sshin m_freem(q->tqe_m); 759126193Sandre uma_zfree(tcp_reass_zone, q); 760126193Sandre tp->t_segqlen--; 761126193Sandre tcp_reass_qsize--; 7621541Srgrimes } 763130989Sps tcp_free_sackholes(tp); 76432821Sdg inp->inp_ppcb = NULL; 765108265Shsu tp->t_inpcb = NULL; 766111145Sjlemon uma_zfree(tcpcb_zone, tp); 767157376Srwatson 768157376Srwatson /* 769157376Srwatson * XXXRW: This seems a bit unclean. 770157376Srwatson */ 771157376Srwatson if (so != NULL) 772157376Srwatson soisdisconnected(so); 773111145Sjlemon} 774111145Sjlemon 775111145Sjlemon/* 776157430Srwatson * Attempt to close a TCP control block, marking it as dropped, and freeing 777157430Srwatson * the socket if we hold the only reference. 778111145Sjlemon */ 779111145Sjlemonstruct tcpcb * 780157431Srwatsontcp_close(struct tcpcb *tp) 781111145Sjlemon{ 782111145Sjlemon struct inpcb *inp = tp->t_inpcb; 783157376Srwatson struct socket *so; 784111145Sjlemon 785146864Srwatson INP_INFO_WLOCK_ASSERT(&tcbinfo); 786138410Srwatson INP_LOCK_ASSERT(inp); 787138410Srwatson 788157376Srwatson inp->inp_vflag |= INP_DROPPED; 789157376Srwatson 790157376Srwatson tcpstat.tcps_closed++; 791157376Srwatson KASSERT(inp->inp_socket != NULL, ("tcp_close: inp_socket NULL")); 792157376Srwatson so = inp->inp_socket; 793157376Srwatson soisdisconnected(so); 794157376Srwatson if (inp->inp_vflag & INP_SOCKREF) { 795157376Srwatson KASSERT(so->so_state & SS_PROTOREF, 796157376Srwatson ("tcp_close: !SS_PROTOREF")); 797157376Srwatson inp->inp_vflag &= ~INP_SOCKREF; 798157376Srwatson tcp_discardcb(tp); 79955679Sshin#ifdef INET6 800157376Srwatson if (inp->inp_vflag & INP_IPV6PROTO) { 801157376Srwatson in6_pcbdetach(inp); 802157376Srwatson in6_pcbfree(inp); 803157376Srwatson } else { 804111145Sjlemon#endif 805157376Srwatson in_pcbdetach(inp); 806157376Srwatson in_pcbfree(inp); 807157376Srwatson#ifdef INET6 808157376Srwatson } 809157376Srwatson#endif 810157376Srwatson ACCEPT_LOCK(); 811157376Srwatson SOCK_LOCK(so); 812157376Srwatson so->so_state &= ~SS_PROTOREF; 813157376Srwatson sofree(so); 814157376Srwatson return (NULL); 815157376Srwatson } 816157376Srwatson return (tp); 8171541Srgrimes} 8181541Srgrimes 8191541Srgrimesvoid 820157431Srwatsontcp_drain(void) 8211541Srgrimes{ 822157431Srwatson 823157431Srwatson if (do_tcpdrain) { 82455198Smsmith struct inpcb *inpb; 82555198Smsmith struct tcpcb *tcpb; 82655679Sshin struct tseg_qent *te; 8271541Srgrimes 82855198Smsmith /* 82955198Smsmith * Walk the tcpbs, if existing, and flush the reassembly queue, 83055198Smsmith * if there is one... 83155198Smsmith * XXX: The "Net/3" implementation doesn't imply that the TCP 83255198Smsmith * reassembly queue should be flushed, but in a situation 833133874Srwatson * where we're really low on mbufs, this is potentially 834133874Srwatson * usefull. 83555198Smsmith */ 83698102Shsu INP_INFO_RLOCK(&tcbinfo); 83774362Sphk LIST_FOREACH(inpb, tcbinfo.listhead, inp_list) { 838111145Sjlemon if (inpb->inp_vflag & INP_TIMEWAIT) 839111145Sjlemon continue; 84098102Shsu INP_LOCK(inpb); 841127870Srwatson if ((tcpb = intotcpcb(inpb)) != NULL) { 84274362Sphk while ((te = LIST_FIRST(&tcpb->t_segq)) 84374362Sphk != NULL) { 84455679Sshin LIST_REMOVE(te, tqe_q); 84555679Sshin m_freem(te->tqe_m); 846126193Sandre uma_zfree(tcp_reass_zone, te); 847126193Sandre tcpb->t_segqlen--; 848126193Sandre tcp_reass_qsize--; 84955198Smsmith } 850144857Sps tcp_clean_sackreport(tcpb); 85155198Smsmith } 85298102Shsu INP_UNLOCK(inpb); 85355198Smsmith } 85498102Shsu INP_INFO_RUNLOCK(&tcbinfo); 85555198Smsmith } 8561541Srgrimes} 8571541Srgrimes 8581541Srgrimes/* 8591541Srgrimes * Notify a tcp user of an asynchronous error; 8601541Srgrimes * store error as soft error, but wake up user 8611541Srgrimes * (for now, won't do anything until can select for soft error). 86272960Sjlemon * 86372960Sjlemon * Do not wake up user since there currently is no mechanism for 86472960Sjlemon * reporting soft errors (yet - a kqueue filter may be added). 8651541Srgrimes */ 86698211Shsustatic struct inpcb * 867157431Srwatsontcp_notify(struct inpcb *inp, int error) 8681541Srgrimes{ 869157432Srwatson struct tcpcb *tp; 8701541Srgrimes 871146864Srwatson INP_INFO_WLOCK_ASSERT(&tcbinfo); 872138410Srwatson INP_LOCK_ASSERT(inp); 873157433Srwatson 874157433Srwatson if ((inp->inp_vflag & INP_TIMEWAIT) || 875157433Srwatson (inp->inp_vflag & INP_DROPPED)) 876157433Srwatson return (inp); 877157433Srwatson 878157432Srwatson tp = intotcpcb(inp); 879157433Srwatson KASSERT(tp != NULL, ("tcp_notify: tp == NULL")); 880138410Srwatson 8811541Srgrimes /* 8821541Srgrimes * Ignore some errors if we are hooked up. 8831541Srgrimes * If connection hasn't completed, has retransmitted several times, 8841541Srgrimes * and receives a second error, give up now. This is better 8851541Srgrimes * than waiting a long time to establish a connection that 8861541Srgrimes * can never complete. 8871541Srgrimes */ 8881541Srgrimes if (tp->t_state == TCPS_ESTABLISHED && 889110896Shsu (error == EHOSTUNREACH || error == ENETUNREACH || 890110896Shsu error == EHOSTDOWN)) { 891139222Srwatson return (inp); 8921541Srgrimes } else if (tp->t_state < TCPS_ESTABLISHED && tp->t_rxtshift > 3 && 89398211Shsu tp->t_softerror) { 894157376Srwatson tp = tcp_drop(tp, error); 895157376Srwatson if (tp != NULL) 896157376Srwatson return (inp); 897157376Srwatson else 898157376Srwatson return (NULL); 89998211Shsu } else { 9001541Srgrimes tp->t_softerror = error; 901139222Srwatson return (inp); 90298211Shsu } 90372960Sjlemon#if 0 904111748Sdes wakeup( &so->so_timeo); 9051541Srgrimes sorwakeup(so); 9061541Srgrimes sowwakeup(so); 90772960Sjlemon#endif 9081541Srgrimes} 9091541Srgrimes 91036079Swollmanstatic int 91162573Sphktcp_pcblist(SYSCTL_HANDLER_ARGS) 91236079Swollman{ 913148156Srwatson int error, i, n; 91436079Swollman struct inpcb *inp, **inp_list; 91536079Swollman inp_gen_t gencnt; 91636079Swollman struct xinpgen xig; 91736079Swollman 91836079Swollman /* 91936079Swollman * The process of preparing the TCB list is too time-consuming and 92036079Swollman * resource-intensive to repeat twice on every request. 92136079Swollman */ 922127870Srwatson if (req->oldptr == NULL) { 92336079Swollman n = tcbinfo.ipi_count; 92436079Swollman req->oldidx = 2 * (sizeof xig) 92536079Swollman + (n + n/8) * sizeof(struct xtcpcb); 926139222Srwatson return (0); 92736079Swollman } 92836079Swollman 929127870Srwatson if (req->newptr != NULL) 930139222Srwatson return (EPERM); 93136079Swollman 93236079Swollman /* 93336079Swollman * OK, now we're committed to doing something. 93436079Swollman */ 93598102Shsu INP_INFO_RLOCK(&tcbinfo); 93636079Swollman gencnt = tcbinfo.ipi_gencnt; 93736079Swollman n = tcbinfo.ipi_count; 93898102Shsu INP_INFO_RUNLOCK(&tcbinfo); 93936079Swollman 940126253Struckman error = sysctl_wire_old_buffer(req, 2 * (sizeof xig) 941100831Struckman + n * sizeof(struct xtcpcb)); 942126253Struckman if (error != 0) 943126253Struckman return (error); 944100831Struckman 94536079Swollman xig.xig_len = sizeof xig; 94636079Swollman xig.xig_count = n; 94736079Swollman xig.xig_gen = gencnt; 94836079Swollman xig.xig_sogen = so_gencnt; 94936079Swollman error = SYSCTL_OUT(req, &xig, sizeof xig); 95036079Swollman if (error) 951139222Srwatson return (error); 95236079Swollman 953111119Simp inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 954127870Srwatson if (inp_list == NULL) 955139222Srwatson return (ENOMEM); 956133874Srwatson 95798102Shsu INP_INFO_RLOCK(&tcbinfo); 958127870Srwatson for (inp = LIST_FIRST(tcbinfo.listhead), i = 0; inp != NULL && i < n; 95971999Sphk inp = LIST_NEXT(inp, inp_list)) { 96098102Shsu INP_LOCK(inp); 961113345Srwatson if (inp->inp_gencnt <= gencnt) { 962113345Srwatson /* 963113345Srwatson * XXX: This use of cr_cansee(), introduced with 964113345Srwatson * TCP state changes, is not quite right, but for 965113345Srwatson * now, better than nothing. 966113345Srwatson */ 967157474Srwatson if (inp->inp_vflag & INP_TIMEWAIT) { 968157474Srwatson if (intotw(inp) != NULL) 969157474Srwatson error = cr_cansee(req->td->td_ucred, 970157474Srwatson intotw(inp)->tw_cred); 971157474Srwatson else 972157474Srwatson error = EINVAL; /* Skip this inp. */ 973157474Srwatson } else 974113345Srwatson error = cr_canseesocket(req->td->td_ucred, 975113345Srwatson inp->inp_socket); 976113345Srwatson if (error == 0) 977113345Srwatson inp_list[i++] = inp; 978113345Srwatson } 97998102Shsu INP_UNLOCK(inp); 98036079Swollman } 98198102Shsu INP_INFO_RUNLOCK(&tcbinfo); 98236079Swollman n = i; 98336079Swollman 98436079Swollman error = 0; 98536079Swollman for (i = 0; i < n; i++) { 98636079Swollman inp = inp_list[i]; 98736079Swollman if (inp->inp_gencnt <= gencnt) { 98836079Swollman struct xtcpcb xt; 989157432Srwatson void *inp_ppcb; 990145978Scperciva 991145978Scperciva bzero(&xt, sizeof(xt)); 99236079Swollman xt.xt_len = sizeof xt; 99336079Swollman /* XXX should avoid extra copy */ 99436079Swollman bcopy(inp, &xt.xt_inp, sizeof *inp); 99547960Stegge inp_ppcb = inp->inp_ppcb; 996111145Sjlemon if (inp_ppcb == NULL) 997111145Sjlemon bzero((char *) &xt.xt_tp, sizeof xt.xt_tp); 998111145Sjlemon else if (inp->inp_vflag & INP_TIMEWAIT) { 999111145Sjlemon bzero((char *) &xt.xt_tp, sizeof xt.xt_tp); 1000111145Sjlemon xt.xt_tp.t_state = TCPS_TIME_WAIT; 1001111145Sjlemon } else 100247960Stegge bcopy(inp_ppcb, &xt.xt_tp, sizeof xt.xt_tp); 1003127870Srwatson if (inp->inp_socket != NULL) 100436079Swollman sotoxsocket(inp->inp_socket, &xt.xt_socket); 1005111145Sjlemon else { 1006111145Sjlemon bzero(&xt.xt_socket, sizeof xt.xt_socket); 1007111145Sjlemon xt.xt_socket.xso_protocol = IPPROTO_TCP; 1008111145Sjlemon } 1009110896Shsu xt.xt_inp.inp_gencnt = inp->inp_gencnt; 101036079Swollman error = SYSCTL_OUT(req, &xt, sizeof xt); 101136079Swollman } 101236079Swollman } 101336079Swollman if (!error) { 101436079Swollman /* 101536079Swollman * Give the user an updated idea of our state. 101636079Swollman * If the generation differs from what we told 101736079Swollman * her before, she knows that something happened 101836079Swollman * while we were processing this request, and it 101936079Swollman * might be necessary to retry. 102036079Swollman */ 102198102Shsu INP_INFO_RLOCK(&tcbinfo); 102236079Swollman xig.xig_gen = tcbinfo.ipi_gencnt; 102336079Swollman xig.xig_sogen = so_gencnt; 102436079Swollman xig.xig_count = tcbinfo.ipi_count; 102598102Shsu INP_INFO_RUNLOCK(&tcbinfo); 102636079Swollman error = SYSCTL_OUT(req, &xig, sizeof xig); 102736079Swollman } 102836079Swollman free(inp_list, M_TEMP); 1029139222Srwatson return (error); 103036079Swollman} 103136079Swollman 103236079SwollmanSYSCTL_PROC(_net_inet_tcp, TCPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0, 103336079Swollman tcp_pcblist, "S,xtcpcb", "List of active TCP connections"); 103436079Swollman 103548758Sgreenstatic int 103662573Sphktcp_getcred(SYSCTL_HANDLER_ARGS) 103748758Sgreen{ 103872650Sgreen struct xucred xuc; 103948758Sgreen struct sockaddr_in addrs[2]; 104048758Sgreen struct inpcb *inp; 1041148156Srwatson int error; 104248758Sgreen 1043132653Scperciva error = suser_cred(req->td->td_ucred, SUSER_ALLOWJAIL); 104448758Sgreen if (error) 104548758Sgreen return (error); 104648758Sgreen error = SYSCTL_IN(req, addrs, sizeof(addrs)); 104748758Sgreen if (error) 104848758Sgreen return (error); 104998102Shsu INP_INFO_RLOCK(&tcbinfo); 105048758Sgreen inp = in_pcblookup_hash(&tcbinfo, addrs[1].sin_addr, addrs[1].sin_port, 105154263Sshin addrs[0].sin_addr, addrs[0].sin_port, 0, NULL); 105298102Shsu if (inp == NULL) { 105348758Sgreen error = ENOENT; 105498102Shsu goto outunlocked; 105548758Sgreen } 105699837Struckman INP_LOCK(inp); 105799837Struckman if (inp->inp_socket == NULL) { 105899837Struckman error = ENOENT; 105999837Struckman goto out; 106099837Struckman } 106192976Srwatson error = cr_canseesocket(req->td->td_ucred, inp->inp_socket); 106278697Sdwmalone if (error) 106378697Sdwmalone goto out; 106491354Sdd cru2x(inp->inp_socket->so_cred, &xuc); 106548758Sgreenout: 106698102Shsu INP_UNLOCK(inp); 106798102Shsuoutunlocked: 106898102Shsu INP_INFO_RUNLOCK(&tcbinfo); 106999838Struckman if (error == 0) 107099838Struckman error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 107148758Sgreen return (error); 107248758Sgreen} 107348758Sgreen 107478697SdwmaloneSYSCTL_PROC(_net_inet_tcp, OID_AUTO, getcred, 107578697Sdwmalone CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0, 107678697Sdwmalone tcp_getcred, "S,xucred", "Get the xucred of a TCP connection"); 107748758Sgreen 107855679Sshin#ifdef INET6 107955679Sshinstatic int 108062573Sphktcp6_getcred(SYSCTL_HANDLER_ARGS) 108155679Sshin{ 108272650Sgreen struct xucred xuc; 108355679Sshin struct sockaddr_in6 addrs[2]; 108455679Sshin struct inpcb *inp; 1085148156Srwatson int error, mapped = 0; 108655679Sshin 1087132653Scperciva error = suser_cred(req->td->td_ucred, SUSER_ALLOWJAIL); 108855679Sshin if (error) 108955679Sshin return (error); 109055679Sshin error = SYSCTL_IN(req, addrs, sizeof(addrs)); 109155679Sshin if (error) 109255679Sshin return (error); 1093148385Sume if ((error = sa6_embedscope(&addrs[0], ip6_use_defzone)) != 0 || 1094148385Sume (error = sa6_embedscope(&addrs[1], ip6_use_defzone)) != 0) { 1095148385Sume return (error); 1096148385Sume } 109755679Sshin if (IN6_IS_ADDR_V4MAPPED(&addrs[0].sin6_addr)) { 109855679Sshin if (IN6_IS_ADDR_V4MAPPED(&addrs[1].sin6_addr)) 109955679Sshin mapped = 1; 110055679Sshin else 110155679Sshin return (EINVAL); 110255679Sshin } 1103148385Sume 110498102Shsu INP_INFO_RLOCK(&tcbinfo); 110555679Sshin if (mapped == 1) 110655679Sshin inp = in_pcblookup_hash(&tcbinfo, 110755679Sshin *(struct in_addr *)&addrs[1].sin6_addr.s6_addr[12], 110855679Sshin addrs[1].sin6_port, 110955679Sshin *(struct in_addr *)&addrs[0].sin6_addr.s6_addr[12], 111055679Sshin addrs[0].sin6_port, 111155679Sshin 0, NULL); 111255679Sshin else 1113151254Sphilip inp = in6_pcblookup_hash(&tcbinfo, 1114151254Sphilip &addrs[1].sin6_addr, addrs[1].sin6_port, 1115151254Sphilip &addrs[0].sin6_addr, addrs[0].sin6_port, 0, NULL); 111698102Shsu if (inp == NULL) { 111755679Sshin error = ENOENT; 111898102Shsu goto outunlocked; 111955679Sshin } 112099837Struckman INP_LOCK(inp); 112199837Struckman if (inp->inp_socket == NULL) { 112299837Struckman error = ENOENT; 112399837Struckman goto out; 112499837Struckman } 112592976Srwatson error = cr_canseesocket(req->td->td_ucred, inp->inp_socket); 112678697Sdwmalone if (error) 112778697Sdwmalone goto out; 112891354Sdd cru2x(inp->inp_socket->so_cred, &xuc); 112955679Sshinout: 113098102Shsu INP_UNLOCK(inp); 113198102Shsuoutunlocked: 113298102Shsu INP_INFO_RUNLOCK(&tcbinfo); 113399838Struckman if (error == 0) 113499838Struckman error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 113555679Sshin return (error); 113655679Sshin} 113755679Sshin 113878697SdwmaloneSYSCTL_PROC(_net_inet6_tcp6, OID_AUTO, getcred, 113978697Sdwmalone CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0, 114078697Sdwmalone tcp6_getcred, "S,xucred", "Get the xucred of a TCP6 connection"); 114155679Sshin#endif 114255679Sshin 114355679Sshin 11441541Srgrimesvoid 1145157431Srwatsontcp_ctlinput(int cmd, struct sockaddr *sa, void *vip) 11461541Srgrimes{ 114772959Sjlemon struct ip *ip = vip; 114872959Sjlemon struct tcphdr *th; 114973109Sjlemon struct in_addr faddr; 115073109Sjlemon struct inpcb *inp; 115173109Sjlemon struct tcpcb *tp; 115298211Shsu struct inpcb *(*notify)(struct inpcb *, int) = tcp_notify; 1153145360Sandre struct icmp *icp; 1154145360Sandre struct in_conninfo inc; 1155145360Sandre tcp_seq icmp_tcp_seq; 1156148156Srwatson int mtu; 11571541Srgrimes 115873109Sjlemon faddr = ((struct sockaddr_in *)sa)->sin_addr; 115973109Sjlemon if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 116073109Sjlemon return; 116173109Sjlemon 1162145355Sandre if (cmd == PRC_MSGSIZE) 1163145355Sandre notify = tcp_mtudisc; 116474937Sjesper else if (icmp_may_rst && (cmd == PRC_UNREACH_ADMIN_PROHIB || 116599156Sjesper cmd == PRC_UNREACH_PORT || cmd == PRC_TIMXCEED_INTRANS) && ip) 116672959Sjlemon notify = tcp_drop_syn_sent; 1167122922Sandre /* 1168122922Sandre * Redirects don't need to be handled up here. 1169122922Sandre */ 1170122922Sandre else if (PRC_IS_REDIRECT(cmd)) 1171122922Sandre return; 1172122922Sandre /* 1173145355Sandre * Source quench is depreciated. 1174145355Sandre */ 1175145355Sandre else if (cmd == PRC_QUENCH) 1176145355Sandre return; 1177145355Sandre /* 1178122922Sandre * Hostdead is ugly because it goes linearly through all PCBs. 1179122922Sandre * XXX: We never get this from ICMP, otherwise it makes an 1180122922Sandre * excellent DoS attack on machines with many connections. 1181122922Sandre */ 1182122922Sandre else if (cmd == PRC_HOSTDEAD) 1183127870Srwatson ip = NULL; 1184119995Sru else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) 11851541Srgrimes return; 1186127870Srwatson if (ip != NULL) { 1187145360Sandre icp = (struct icmp *)((caddr_t)ip 1188145360Sandre - offsetof(struct icmp, icmp_ip)); 1189133874Srwatson th = (struct tcphdr *)((caddr_t)ip 1190105586Sphk + (ip->ip_hl << 2)); 119198596Shsu INP_INFO_WLOCK(&tcbinfo); 119273109Sjlemon inp = in_pcblookup_hash(&tcbinfo, faddr, th->th_dport, 119373109Sjlemon ip->ip_src, th->th_sport, 0, NULL); 119498102Shsu if (inp != NULL) { 119598102Shsu INP_LOCK(inp); 1196157433Srwatson if (!(inp->inp_vflag & INP_TIMEWAIT) && 1197157433Srwatson !(inp->inp_vflag & INP_DROPPED) && 1198157433Srwatson !(inp->inp_socket == NULL)) { 1199145360Sandre icmp_tcp_seq = htonl(th->th_seq); 120098102Shsu tp = intotcpcb(inp); 1201145360Sandre if (SEQ_GEQ(icmp_tcp_seq, tp->snd_una) && 1202145360Sandre SEQ_LT(icmp_tcp_seq, tp->snd_max)) { 1203145360Sandre if (cmd == PRC_MSGSIZE) { 1204145360Sandre /* 1205145360Sandre * MTU discovery: 1206145360Sandre * If we got a needfrag set the MTU 1207145360Sandre * in the route to the suggested new 1208145360Sandre * value (if given) and then notify. 1209145360Sandre */ 1210145360Sandre bzero(&inc, sizeof(inc)); 1211145360Sandre inc.inc_flags = 0; /* IPv4 */ 1212145360Sandre inc.inc_faddr = faddr; 1213145360Sandre 1214145360Sandre mtu = ntohs(icp->icmp_nextmtu); 1215145869Sandre /* 1216145869Sandre * If no alternative MTU was 1217145869Sandre * proposed, try the next smaller 1218149929Sandre * one. ip->ip_len has already 1219149929Sandre * been swapped in icmp_input(). 1220145869Sandre */ 1221145360Sandre if (!mtu) 1222149929Sandre mtu = ip_next_mtu(ip->ip_len, 1223145869Sandre 1); 1224145869Sandre if (mtu < max(296, (tcp_minmss) 1225145869Sandre + sizeof(struct tcpiphdr))) 1226145869Sandre mtu = 0; 1227145869Sandre if (!mtu) 1228145869Sandre mtu = tcp_mssdflt 1229145869Sandre + sizeof(struct tcpiphdr); 1230145869Sandre /* 1231145869Sandre * Only cache the the MTU if it 1232145869Sandre * is smaller than the interface 1233145869Sandre * or route MTU. tcp_mtudisc() 1234145869Sandre * will do right thing by itself. 1235145869Sandre */ 1236145869Sandre if (mtu <= tcp_maxmtu(&inc)) 1237145360Sandre tcp_hc_updatemtu(&inc, mtu); 1238145360Sandre } 1239145360Sandre 124098211Shsu inp = (*notify)(inp, inetctlerrmap[cmd]); 1241145360Sandre } 124298102Shsu } 1243127870Srwatson if (inp != NULL) 124498211Shsu INP_UNLOCK(inp); 124586764Sjlemon } else { 124686764Sjlemon inc.inc_fport = th->th_dport; 124786764Sjlemon inc.inc_lport = th->th_sport; 124886764Sjlemon inc.inc_faddr = faddr; 124986764Sjlemon inc.inc_laddr = ip->ip_src; 125086764Sjlemon#ifdef INET6 125186764Sjlemon inc.inc_isipv6 = 0; 125286764Sjlemon#endif 125386764Sjlemon syncache_unreach(&inc, th); 125473109Sjlemon } 125598596Shsu INP_INFO_WUNLOCK(&tcbinfo); 12561541Srgrimes } else 125798102Shsu in_pcbnotifyall(&tcbinfo, faddr, inetctlerrmap[cmd], notify); 12581541Srgrimes} 12591541Srgrimes 126055679Sshin#ifdef INET6 126155679Sshinvoid 1262157431Srwatsontcp6_ctlinput(int cmd, struct sockaddr *sa, void *d) 126355679Sshin{ 126455679Sshin struct tcphdr th; 126598211Shsu struct inpcb *(*notify)(struct inpcb *, int) = tcp_notify; 126655679Sshin struct ip6_hdr *ip6; 126755679Sshin struct mbuf *m; 126878064Sume struct ip6ctlparam *ip6cp = NULL; 126978064Sume const struct sockaddr_in6 *sa6_src = NULL; 127055679Sshin int off; 127178064Sume struct tcp_portonly { 127278064Sume u_int16_t th_sport; 127378064Sume u_int16_t th_dport; 127478064Sume } *thp; 127555679Sshin 127655679Sshin if (sa->sa_family != AF_INET6 || 127755679Sshin sa->sa_len != sizeof(struct sockaddr_in6)) 127855679Sshin return; 127955679Sshin 1280145355Sandre if (cmd == PRC_MSGSIZE) 128155679Sshin notify = tcp_mtudisc; 128255679Sshin else if (!PRC_IS_REDIRECT(cmd) && 1283119995Sru ((unsigned)cmd >= PRC_NCMDS || inet6ctlerrmap[cmd] == 0)) 128455679Sshin return; 1285145355Sandre /* Source quench is depreciated. */ 1286145355Sandre else if (cmd == PRC_QUENCH) 1287145355Sandre return; 128855679Sshin 128955679Sshin /* if the parameter is from icmp6, decode it. */ 129055679Sshin if (d != NULL) { 129178064Sume ip6cp = (struct ip6ctlparam *)d; 129255679Sshin m = ip6cp->ip6c_m; 129355679Sshin ip6 = ip6cp->ip6c_ip6; 129455679Sshin off = ip6cp->ip6c_off; 129578064Sume sa6_src = ip6cp->ip6c_src; 129655679Sshin } else { 129755679Sshin m = NULL; 129855679Sshin ip6 = NULL; 129967456Sitojun off = 0; /* fool gcc */ 130078064Sume sa6_src = &sa6_any; 130155679Sshin } 130255679Sshin 1303127870Srwatson if (ip6 != NULL) { 130486764Sjlemon struct in_conninfo inc; 130555679Sshin /* 130655679Sshin * XXX: We assume that when IPV6 is non NULL, 130755679Sshin * M and OFF are valid. 130855679Sshin */ 130955679Sshin 131067456Sitojun /* check if we can safely examine src and dst ports */ 131178064Sume if (m->m_pkthdr.len < off + sizeof(*thp)) 131267456Sitojun return; 131367456Sitojun 131478064Sume bzero(&th, sizeof(th)); 131578064Sume m_copydata(m, off, sizeof(*thp), (caddr_t)&th); 131678064Sume 1317133192Srwatson in6_pcbnotify(&tcbinfo, sa, th.th_dport, 131878064Sume (struct sockaddr *)ip6cp->ip6c_src, 1319125776Sume th.th_sport, cmd, NULL, notify); 132086764Sjlemon 132186764Sjlemon inc.inc_fport = th.th_dport; 132286764Sjlemon inc.inc_lport = th.th_sport; 132386764Sjlemon inc.inc6_faddr = ((struct sockaddr_in6 *)sa)->sin6_addr; 132486764Sjlemon inc.inc6_laddr = ip6cp->ip6c_src->sin6_addr; 132586764Sjlemon inc.inc_isipv6 = 1; 1326133591Sdwmalone INP_INFO_WLOCK(&tcbinfo); 132786764Sjlemon syncache_unreach(&inc, &th); 1328133591Sdwmalone INP_INFO_WUNLOCK(&tcbinfo); 132955679Sshin } else 1330133192Srwatson in6_pcbnotify(&tcbinfo, sa, 0, (const struct sockaddr *)sa6_src, 1331125776Sume 0, cmd, NULL, notify); 133255679Sshin} 133355679Sshin#endif /* INET6 */ 133455679Sshin 133580428Speter 133682122Ssilby/* 133782122Ssilby * Following is where TCP initial sequence number generation occurs. 133882122Ssilby * 133982122Ssilby * There are two places where we must use initial sequence numbers: 134082122Ssilby * 1. In SYN-ACK packets. 134182122Ssilby * 2. In SYN packets. 134282122Ssilby * 134394390Ssilby * All ISNs for SYN-ACK packets are generated by the syncache. See 134494390Ssilby * tcp_syncache.c for details. 134582122Ssilby * 134682122Ssilby * The ISNs in SYN packets must be monotonic; TIME_WAIT recycling 134782122Ssilby * depends on this property. In addition, these ISNs should be 134882122Ssilby * unguessable so as to prevent connection hijacking. To satisfy 134982122Ssilby * the requirements of this situation, the algorithm outlined in 1350133874Srwatson * RFC 1948 is used, with only small modifications. 135182122Ssilby * 135282122Ssilby * Implementation details: 135382122Ssilby * 135482122Ssilby * Time is based off the system timer, and is corrected so that it 135582122Ssilby * increases by one megabyte per second. This allows for proper 135682122Ssilby * recycling on high speed LANs while still leaving over an hour 135782122Ssilby * before rollover. 135882122Ssilby * 1359128452Ssilby * As reading the *exact* system time is too expensive to be done 1360128452Ssilby * whenever setting up a TCP connection, we increment the time 1361128452Ssilby * offset in two ways. First, a small random positive increment 1362128452Ssilby * is added to isn_offset for each connection that is set up. 1363128452Ssilby * Second, the function tcp_isn_tick fires once per clock tick 1364128452Ssilby * and increments isn_offset as necessary so that sequence numbers 1365128452Ssilby * are incremented at approximately ISN_BYTES_PER_SECOND. The 1366128452Ssilby * random positive increments serve only to ensure that the same 1367128452Ssilby * exact sequence number is never sent out twice (as could otherwise 1368128452Ssilby * happen when a port is recycled in less than the system tick 1369128452Ssilby * interval.) 1370128452Ssilby * 137182122Ssilby * net.inet.tcp.isn_reseed_interval controls the number of seconds 137282122Ssilby * between seeding of isn_secret. This is normally set to zero, 137382122Ssilby * as reseeding should not be necessary. 137482122Ssilby * 1375138018Srwatson * Locking of the global variables isn_secret, isn_last_reseed, isn_offset, 1376138018Srwatson * isn_offset_old, and isn_ctx is performed using the TCP pcbinfo lock. In 1377138018Srwatson * general, this means holding an exclusive (write) lock. 137882122Ssilby */ 137979413Ssilby 138082122Ssilby#define ISN_BYTES_PER_SECOND 1048576 1381128452Ssilby#define ISN_STATIC_INCREMENT 4096 1382128452Ssilby#define ISN_RANDOM_INCREMENT (4096 - 1) 138379413Ssilby 1384138018Srwatsonstatic u_char isn_secret[32]; 1385138018Srwatsonstatic int isn_last_reseed; 1386138018Srwatsonstatic u_int32_t isn_offset, isn_offset_old; 1387138018Srwatsonstatic MD5_CTX isn_ctx; 138875619Skris 138975619Skristcp_seq 1390157431Srwatsontcp_new_isn(struct tcpcb *tp) 139175619Skris{ 139282122Ssilby u_int32_t md5_buffer[4]; 139382122Ssilby tcp_seq new_isn; 139475619Skris 1395138018Srwatson INP_INFO_WLOCK_ASSERT(&tcbinfo); 1396138410Srwatson INP_LOCK_ASSERT(tp->t_inpcb); 1397138018Srwatson 139882122Ssilby /* Seed if this is the first use, reseed if requested. */ 139994390Ssilby if ((isn_last_reseed == 0) || ((tcp_isn_reseed_interval > 0) && 140082122Ssilby (((u_int)isn_last_reseed + (u_int)tcp_isn_reseed_interval*hz) 140182122Ssilby < (u_int)ticks))) { 140282122Ssilby read_random(&isn_secret, sizeof(isn_secret)); 140382122Ssilby isn_last_reseed = ticks; 140482122Ssilby } 1405133874Srwatson 140682122Ssilby /* Compute the md5 hash and return the ISN. */ 140782122Ssilby MD5Init(&isn_ctx); 140882122Ssilby MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->inp_fport, sizeof(u_short)); 140982122Ssilby MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->inp_lport, sizeof(u_short)); 141082122Ssilby#ifdef INET6 141182122Ssilby if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) { 141282122Ssilby MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->in6p_faddr, 141382122Ssilby sizeof(struct in6_addr)); 141482122Ssilby MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->in6p_laddr, 141582122Ssilby sizeof(struct in6_addr)); 141682122Ssilby } else 141782122Ssilby#endif 141882122Ssilby { 141982122Ssilby MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->inp_faddr, 142082122Ssilby sizeof(struct in_addr)); 142182122Ssilby MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->inp_laddr, 142282122Ssilby sizeof(struct in_addr)); 142382122Ssilby } 142482122Ssilby MD5Update(&isn_ctx, (u_char *) &isn_secret, sizeof(isn_secret)); 142582122Ssilby MD5Final((u_char *) &md5_buffer, &isn_ctx); 142682122Ssilby new_isn = (tcp_seq) md5_buffer[0]; 1427128452Ssilby isn_offset += ISN_STATIC_INCREMENT + 1428128452Ssilby (arc4random() & ISN_RANDOM_INCREMENT); 1429128452Ssilby new_isn += isn_offset; 1430139222Srwatson return (new_isn); 143175619Skris} 143275619Skris 14331541Srgrimes/* 1434128452Ssilby * Increment the offset to the next ISN_BYTES_PER_SECOND / hz boundary 1435128452Ssilby * to keep time flowing at a relatively constant rate. If the random 1436128452Ssilby * increments have already pushed us past the projected offset, do nothing. 1437128452Ssilby */ 1438128452Ssilbystatic void 1439157431Srwatsontcp_isn_tick(void *xtp) 1440128452Ssilby{ 1441128452Ssilby u_int32_t projected_offset; 1442133874Srwatson 1443138018Srwatson INP_INFO_WLOCK(&tcbinfo); 1444141078Srwatson projected_offset = isn_offset_old + ISN_BYTES_PER_SECOND / 100; 1445128452Ssilby 1446128452Ssilby if (projected_offset > isn_offset) 1447128452Ssilby isn_offset = projected_offset; 1448128452Ssilby 1449128452Ssilby isn_offset_old = isn_offset; 1450141072Srwatson callout_reset(&isn_callout, hz/100, tcp_isn_tick, NULL); 1451138018Srwatson INP_INFO_WUNLOCK(&tcbinfo); 1452128452Ssilby} 1453128452Ssilby 1454128452Ssilby/* 145572959Sjlemon * When a specific ICMP unreachable message is received and the 145672959Sjlemon * connection state is SYN-SENT, drop the connection. This behavior 145772959Sjlemon * is controlled by the icmp_may_rst sysctl. 145870103Sphk */ 145998211Shsustruct inpcb * 1460157431Srwatsontcp_drop_syn_sent(struct inpcb *inp, int errno) 146170103Sphk{ 1462157432Srwatson struct tcpcb *tp; 146370103Sphk 1464146864Srwatson INP_INFO_WLOCK_ASSERT(&tcbinfo); 1465138019Srwatson INP_LOCK_ASSERT(inp); 1466157433Srwatson 1467157433Srwatson if ((inp->inp_vflag & INP_TIMEWAIT) || 1468157433Srwatson (inp->inp_vflag & INP_DROPPED)) 1469157433Srwatson return (inp); 1470157433Srwatson 1471157432Srwatson tp = intotcpcb(inp); 1472157433Srwatson if (tp->t_state != TCPS_SYN_SENT) 1473157433Srwatson return (inp); 1474146864Srwatson 1475157433Srwatson tp = tcp_drop(tp, errno); 1476157433Srwatson if (tp != NULL) 1477157433Srwatson return (inp); 1478157433Srwatson else 1479157433Srwatson return (NULL); 148072638Sphk} 148172638Sphk 148272638Sphk/* 148310881Swollman * When `need fragmentation' ICMP is received, update our idea of the MSS 148410881Swollman * based on the new value in the route. Also nudge TCP to send something, 148510881Swollman * since we know the packet we just sent was dropped. 148610930Swollman * This duplicates some code in the tcp_mss() function in tcp_input.c. 148710881Swollman */ 148898211Shsustruct inpcb * 1489157431Srwatsontcp_mtudisc(struct inpcb *inp, int errno) 149010881Swollman{ 1491157432Srwatson struct tcpcb *tp; 149210930Swollman struct socket *so = inp->inp_socket; 1493122922Sandre u_int maxmtu; 1494122922Sandre u_int romtu; 149510930Swollman int mss; 149655679Sshin#ifdef INET6 1497134041Sandre int isipv6; 149855679Sshin#endif /* INET6 */ 149910881Swollman 1500138019Srwatson INP_LOCK_ASSERT(inp); 1501157433Srwatson if ((inp->inp_vflag & INP_TIMEWAIT) || 1502157433Srwatson (inp->inp_vflag & INP_DROPPED)) 1503157433Srwatson return (inp); 1504157433Srwatson 1505157432Srwatson tp = intotcpcb(inp); 1506157433Srwatson KASSERT(tp != NULL, ("tcp_mtudisc: tp == NULL")); 1507157433Srwatson 1508134041Sandre#ifdef INET6 1509157433Srwatson isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0; 1510134041Sandre#endif 1511157433Srwatson maxmtu = tcp_hc_getmtu(&inp->inp_inc); /* IPv4 and IPv6 */ 1512157433Srwatson romtu = 151355679Sshin#ifdef INET6 1514157433Srwatson isipv6 ? tcp_maxmtu6(&inp->inp_inc) : 1515122922Sandre#endif /* INET6 */ 1516157433Srwatson tcp_maxmtu(&inp->inp_inc); 1517157433Srwatson if (!maxmtu) 1518157433Srwatson maxmtu = romtu; 1519157433Srwatson else 1520157433Srwatson maxmtu = min(maxmtu, romtu); 1521157433Srwatson if (!maxmtu) { 1522157433Srwatson tp->t_maxopd = tp->t_maxseg = 152355679Sshin#ifdef INET6 1524157433Srwatson isipv6 ? tcp_v6mssdflt : 152555679Sshin#endif /* INET6 */ 1526157433Srwatson tcp_mssdflt; 1527157433Srwatson return (inp); 1528157433Srwatson } 1529157433Srwatson mss = maxmtu - 153055679Sshin#ifdef INET6 1531157433Srwatson (isipv6 ? sizeof(struct ip6_hdr) + sizeof(struct tcphdr) : 153255679Sshin#endif /* INET6 */ 1533157433Srwatson sizeof(struct tcpiphdr) 153455679Sshin#ifdef INET6 1535157433Srwatson ) 153655679Sshin#endif /* INET6 */ 1537157433Srwatson ; 153855679Sshin 1539157433Srwatson /* 1540157433Srwatson * XXX - The above conditional probably violates the TCP 1541157433Srwatson * spec. The problem is that, since we don't know the 1542157433Srwatson * other end's MSS, we are supposed to use a conservative 1543157433Srwatson * default. But, if we do that, then MTU discovery will 1544157433Srwatson * never actually take place, because the conservative 1545157433Srwatson * default is much less than the MTUs typically seen 1546157433Srwatson * on the Internet today. For the moment, we'll sweep 1547157433Srwatson * this under the carpet. 1548157433Srwatson * 1549157433Srwatson * The conservative default might not actually be a problem 1550157433Srwatson * if the only case this occurs is when sending an initial 1551157433Srwatson * SYN with options and data to a host we've never talked 1552157433Srwatson * to before. Then, they will reply with an MSS value which 1553157433Srwatson * will get recorded and the new parameters should get 1554157433Srwatson * recomputed. For Further Study. 1555157433Srwatson */ 1556157433Srwatson if (tp->t_maxopd <= mss) 1557157433Srwatson return (inp); 1558157433Srwatson tp->t_maxopd = mss; 155910930Swollman 1560157433Srwatson if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 1561157433Srwatson (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP) 1562157433Srwatson mss -= TCPOLEN_TSTAMP_APPA; 156310930Swollman#if (MCLBYTES & (MCLBYTES - 1)) == 0 1564157433Srwatson if (mss > MCLBYTES) 1565157433Srwatson mss &= ~(MCLBYTES-1); 156610930Swollman#else 1567157433Srwatson if (mss > MCLBYTES) 1568157433Srwatson mss = mss / MCLBYTES * MCLBYTES; 156910881Swollman#endif 1570157433Srwatson if (so->so_snd.sb_hiwat < mss) 1571157433Srwatson mss = so->so_snd.sb_hiwat; 157210930Swollman 1573157433Srwatson tp->t_maxseg = mss; 157410930Swollman 1575157433Srwatson tcpstat.tcps_mturesent++; 1576157433Srwatson tp->t_rtttime = 0; 1577157433Srwatson tp->snd_nxt = tp->snd_una; 1578157433Srwatson tcp_output(tp); 1579139222Srwatson return (inp); 158010881Swollman} 158110881Swollman 158210881Swollman/* 15836283Swollman * Look-up the routing entry to the peer of this inpcb. If no route 1584108265Shsu * is found and it cannot be allocated, then return NULL. This routine 15856283Swollman * is called by TCP routines that access the rmx structure and by tcp_mss 15866283Swollman * to get the interface MTU. 15876283Swollman */ 1588133874Srwatsonu_long 1589157431Srwatsontcp_maxmtu(struct in_conninfo *inc) 15906283Swollman{ 1591122922Sandre struct route sro; 1592122922Sandre struct sockaddr_in *dst; 1593122922Sandre struct ifnet *ifp; 1594122922Sandre u_long maxmtu = 0; 15956283Swollman 1596122922Sandre KASSERT(inc != NULL, ("tcp_maxmtu with NULL in_conninfo pointer")); 1597122922Sandre 1598122996Sandre bzero(&sro, sizeof(sro)); 1599122922Sandre if (inc->inc_faddr.s_addr != INADDR_ANY) { 1600122922Sandre dst = (struct sockaddr_in *)&sro.ro_dst; 1601122922Sandre dst->sin_family = AF_INET; 1602122922Sandre dst->sin_len = sizeof(*dst); 1603122922Sandre dst->sin_addr = inc->inc_faddr; 1604122922Sandre rtalloc_ign(&sro, RTF_CLONING); 16056283Swollman } 1606122922Sandre if (sro.ro_rt != NULL) { 1607122922Sandre ifp = sro.ro_rt->rt_ifp; 1608122922Sandre if (sro.ro_rt->rt_rmx.rmx_mtu == 0) 1609122922Sandre maxmtu = ifp->if_mtu; 1610122922Sandre else 1611122922Sandre maxmtu = min(sro.ro_rt->rt_rmx.rmx_mtu, ifp->if_mtu); 1612122922Sandre RTFREE(sro.ro_rt); 1613122922Sandre } 1614122922Sandre return (maxmtu); 16156283Swollman} 16166283Swollman 161755679Sshin#ifdef INET6 1618122922Sandreu_long 1619157431Srwatsontcp_maxmtu6(struct in_conninfo *inc) 162055679Sshin{ 1621122922Sandre struct route_in6 sro6; 1622122922Sandre struct ifnet *ifp; 1623122922Sandre u_long maxmtu = 0; 162455679Sshin 1625122922Sandre KASSERT(inc != NULL, ("tcp_maxmtu6 with NULL in_conninfo pointer")); 1626122922Sandre 1627122996Sandre bzero(&sro6, sizeof(sro6)); 1628122922Sandre if (!IN6_IS_ADDR_UNSPECIFIED(&inc->inc6_faddr)) { 1629122922Sandre sro6.ro_dst.sin6_family = AF_INET6; 1630122922Sandre sro6.ro_dst.sin6_len = sizeof(struct sockaddr_in6); 1631122922Sandre sro6.ro_dst.sin6_addr = inc->inc6_faddr; 1632122922Sandre rtalloc_ign((struct route *)&sro6, RTF_CLONING); 163355679Sshin } 1634122922Sandre if (sro6.ro_rt != NULL) { 1635122922Sandre ifp = sro6.ro_rt->rt_ifp; 1636122922Sandre if (sro6.ro_rt->rt_rmx.rmx_mtu == 0) 1637122922Sandre maxmtu = IN6_LINKMTU(sro6.ro_rt->rt_ifp); 1638122922Sandre else 1639122922Sandre maxmtu = min(sro6.ro_rt->rt_rmx.rmx_mtu, 1640122922Sandre IN6_LINKMTU(sro6.ro_rt->rt_ifp)); 1641122922Sandre RTFREE(sro6.ro_rt); 1642122922Sandre } 1643122922Sandre 1644122922Sandre return (maxmtu); 164555679Sshin} 164655679Sshin#endif /* INET6 */ 164755679Sshin 164855679Sshin#ifdef IPSEC 164955679Sshin/* compute ESP/AH header size for TCP, including outer IP header. */ 165055679Sshinsize_t 1651157431Srwatsonipsec_hdrsiz_tcp(struct tcpcb *tp) 165255679Sshin{ 165355679Sshin struct inpcb *inp; 165455679Sshin struct mbuf *m; 165555679Sshin size_t hdrsiz; 165655679Sshin struct ip *ip; 165755679Sshin#ifdef INET6 165855679Sshin struct ip6_hdr *ip6; 1659111145Sjlemon#endif 166055679Sshin struct tcphdr *th; 166155679Sshin 166278642Ssilby if ((tp == NULL) || ((inp = tp->t_inpcb) == NULL)) 1663139222Srwatson return (0); 1664111119Simp MGETHDR(m, M_DONTWAIT, MT_DATA); 166555679Sshin if (!m) 1666139222Srwatson return (0); 166755679Sshin 166855679Sshin#ifdef INET6 166955679Sshin if ((inp->inp_vflag & INP_IPV6) != 0) { 167055679Sshin ip6 = mtod(m, struct ip6_hdr *); 167155679Sshin th = (struct tcphdr *)(ip6 + 1); 167255679Sshin m->m_pkthdr.len = m->m_len = 167355679Sshin sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 1674111144Sjlemon tcpip_fillheaders(inp, ip6, th); 167555679Sshin hdrsiz = ipsec6_hdrsiz(m, IPSEC_DIR_OUTBOUND, inp); 167655679Sshin } else 167755679Sshin#endif /* INET6 */ 1678133874Srwatson { 1679133874Srwatson ip = mtod(m, struct ip *); 1680133874Srwatson th = (struct tcphdr *)(ip + 1); 1681133874Srwatson m->m_pkthdr.len = m->m_len = sizeof(struct tcpiphdr); 1682133874Srwatson tcpip_fillheaders(inp, ip, th); 1683133874Srwatson hdrsiz = ipsec4_hdrsiz(m, IPSEC_DIR_OUTBOUND, inp); 1684133874Srwatson } 168555679Sshin 168655679Sshin m_free(m); 1687139222Srwatson return (hdrsiz); 168855679Sshin} 168955679Sshin#endif /*IPSEC*/ 169055679Sshin 16916283Swollman/* 1692111145Sjlemon * Move a TCP connection into TIME_WAIT state. 1693138410Srwatson * tcbinfo is locked. 1694111145Sjlemon * inp is locked, and is unlocked before returning. 1695111145Sjlemon */ 1696111145Sjlemonvoid 1697157431Srwatsontcp_twstart(struct tcpcb *tp) 1698111145Sjlemon{ 1699111145Sjlemon struct tcptw *tw; 1700111145Sjlemon struct inpcb *inp; 1701111145Sjlemon int tw_time, acknow; 1702111145Sjlemon struct socket *so; 1703111145Sjlemon 1704138025Srwatson INP_INFO_WLOCK_ASSERT(&tcbinfo); /* tcp_timer_2msl_reset(). */ 1705138020Srwatson INP_LOCK_ASSERT(tp->t_inpcb); 1706138020Srwatson 1707112009Sjlemon tw = uma_zalloc(tcptw_zone, M_NOWAIT); 1708112009Sjlemon if (tw == NULL) { 1709112009Sjlemon tw = tcp_timer_2msl_tw(1); 1710112009Sjlemon if (tw == NULL) { 1711157376Srwatson tp = tcp_close(tp); 1712157376Srwatson if (tp != NULL) 1713157376Srwatson INP_UNLOCK(tp->t_inpcb); 1714112009Sjlemon return; 1715112009Sjlemon } 1716112009Sjlemon } 1717111145Sjlemon inp = tp->t_inpcb; 1718111145Sjlemon tw->tw_inpcb = inp; 1719111145Sjlemon 1720111145Sjlemon /* 1721111145Sjlemon * Recover last window size sent. 1722111145Sjlemon */ 1723111145Sjlemon tw->last_win = (tp->rcv_adv - tp->rcv_nxt) >> tp->rcv_scale; 1724111145Sjlemon 1725111145Sjlemon /* 1726111145Sjlemon * Set t_recent if timestamps are used on the connection. 1727111145Sjlemon */ 1728133874Srwatson if ((tp->t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP|TF_NOOPT)) == 1729133874Srwatson (TF_REQ_TSTMP|TF_RCVD_TSTMP)) 1730111145Sjlemon tw->t_recent = tp->ts_recent; 1731111145Sjlemon else 1732111145Sjlemon tw->t_recent = 0; 1733111145Sjlemon 1734111145Sjlemon tw->snd_nxt = tp->snd_nxt; 1735111145Sjlemon tw->rcv_nxt = tp->rcv_nxt; 1736121850Ssilby tw->iss = tp->iss; 1737121884Ssilby tw->irs = tp->irs; 1738111145Sjlemon tw->t_starttime = tp->t_starttime; 1739112009Sjlemon tw->tw_time = 0; 1740111145Sjlemon 1741111145Sjlemon/* XXX 1742111145Sjlemon * If this code will 1743111145Sjlemon * be used for fin-wait-2 state also, then we may need 1744111145Sjlemon * a ts_recent from the last segment. 1745111145Sjlemon */ 1746137139Sandre tw_time = 2 * tcp_msl; 1747137139Sandre acknow = tp->t_flags & TF_ACKNOW; 1748157376Srwatson 1749157376Srwatson /* 1750157376Srwatson * First, discard tcpcb state, which includes stopping its timers and 1751157376Srwatson * freeing it. tcp_discardcb() used to also release the inpcb, but 1752157376Srwatson * that work is now done in the caller. 1753157376Srwatson */ 1754111145Sjlemon tcp_discardcb(tp); 1755111145Sjlemon so = inp->inp_socket; 1756130387Srwatson SOCK_LOCK(so); 1757111145Sjlemon tw->tw_cred = crhold(so->so_cred); 1758111145Sjlemon tw->tw_so_options = so->so_options; 1759157376Srwatson SOCK_UNLOCK(so); 1760114794Srwatson if (acknow) 1761126351Srwatson tcp_twrespond(tw, TH_ACK); 1762157432Srwatson inp->inp_ppcb = tw; 1763111145Sjlemon inp->inp_vflag |= INP_TIMEWAIT; 1764112009Sjlemon tcp_timer_2msl_reset(tw, tw_time); 1765157376Srwatson 1766157376Srwatson /* 1767157376Srwatson * If the inpcb owns the sole reference to the socket, then we can 1768157376Srwatson * detach and free the socket as it is not needed in time wait. 1769157376Srwatson */ 1770157376Srwatson if (inp->inp_vflag & INP_SOCKREF) { 1771157376Srwatson KASSERT(so->so_state & SS_PROTOREF, 1772157376Srwatson ("tcp_twstart: !SS_PROTOREF")); 1773157376Srwatson inp->inp_vflag &= ~INP_SOCKREF; 1774157376Srwatson#ifdef INET6 1775157376Srwatson if (inp->inp_vflag & INP_IPV6PROTO) 1776157376Srwatson in6_pcbdetach(inp); 1777157376Srwatson else 1778157376Srwatson#endif 1779157376Srwatson in_pcbdetach(inp); 1780157376Srwatson INP_UNLOCK(inp); 1781157376Srwatson ACCEPT_LOCK(); 1782157376Srwatson SOCK_LOCK(so); 1783157376Srwatson so->so_state &= ~SS_PROTOREF; 1784157376Srwatson sofree(so); 1785157376Srwatson } else 1786157376Srwatson INP_UNLOCK(inp); 1787111145Sjlemon} 1788111145Sjlemon 1789121850Ssilby/* 1790121884Ssilby * The appromixate rate of ISN increase of Microsoft TCP stacks; 1791121884Ssilby * the actual rate is slightly higher due to the addition of 1792121884Ssilby * random positive increments. 1793121884Ssilby * 1794121884Ssilby * Most other new OSes use semi-randomized ISN values, so we 1795121884Ssilby * do not need to worry about them. 1796121884Ssilby */ 1797121884Ssilby#define MS_ISN_BYTES_PER_SECOND 250000 1798121884Ssilby 1799121884Ssilby/* 1800121850Ssilby * Determine if the ISN we will generate has advanced beyond the last 1801121850Ssilby * sequence number used by the previous connection. If so, indicate 1802121850Ssilby * that it is safe to recycle this tw socket by returning 1. 1803138020Srwatson * 1804138020Srwatson * XXXRW: This function should assert the inpcb lock as it does multiple 1805139221Srwatson * non-atomic reads from the tcptw, but is currently called without it from 1806138020Srwatson * in_pcb.c:in_pcblookup_local(). 1807121850Ssilby */ 1808121850Ssilbyint 1809121850Ssilbytcp_twrecycleable(struct tcptw *tw) 1810121850Ssilby{ 1811121884Ssilby tcp_seq new_iss = tw->iss; 1812121884Ssilby tcp_seq new_irs = tw->irs; 1813121850Ssilby 1814121884Ssilby new_iss += (ticks - tw->t_starttime) * (ISN_BYTES_PER_SECOND / hz); 1815121884Ssilby new_irs += (ticks - tw->t_starttime) * (MS_ISN_BYTES_PER_SECOND / hz); 1816133874Srwatson 1817121884Ssilby if (SEQ_GT(new_iss, tw->snd_nxt) && SEQ_GT(new_irs, tw->rcv_nxt)) 1818139222Srwatson return (1); 1819121850Ssilby else 1820139222Srwatson return (0); 1821121850Ssilby} 1822121850Ssilby 1823157376Srwatsonvoid 1824112009Sjlemontcp_twclose(struct tcptw *tw, int reuse) 1825111145Sjlemon{ 1826157376Srwatson struct socket *so; 1827111145Sjlemon struct inpcb *inp; 1828111145Sjlemon 1829157376Srwatson /* 1830157386Srwatson * At this point, we are in one of two situations: 1831157376Srwatson * 1832157386Srwatson * (1) We have no socket, just an inpcb<->twtcp pair. Release it all 1833157386Srwatson * after validating. 1834157386Srwatson * 1835157386Srwatson * (2) We have a socket, which we may or may now own the reference 1836157386Srwatson * for. If we own the reference, release all the state after 1837157386Srwatson * validating. If not, leave it for the socket close to clean up. 1838157376Srwatson */ 1839111145Sjlemon inp = tw->tw_inpcb; 1840157376Srwatson KASSERT((inp->inp_vflag & INP_TIMEWAIT), ("tcp_twclose: !timewait")); 1841157432Srwatson KASSERT(intotw(inp) == tw, ("tcp_twclose: inp_ppcb != tw")); 1842138025Srwatson INP_INFO_WLOCK_ASSERT(&tcbinfo); /* tcp_timer_2msl_stop(). */ 1843138020Srwatson INP_LOCK_ASSERT(inp); 1844138020Srwatson 1845111145Sjlemon tw->tw_inpcb = NULL; 1846112009Sjlemon tcp_timer_2msl_stop(tw); 1847111145Sjlemon inp->inp_ppcb = NULL; 1848157376Srwatson inp->inp_vflag |= INP_DROPPED; 1849157376Srwatson 1850157376Srwatson so = inp->inp_socket; 1851157386Srwatson if (so != NULL) { 1852157386Srwatson if (inp->inp_vflag & INP_SOCKREF) { 1853157386Srwatson /* 1854157386Srwatson * If a socket is present, and we own the only 1855157386Srwatson * reference, we need to tear down the socket and the 1856157386Srwatson * inpcb. 1857157386Srwatson */ 1858157386Srwatson inp->inp_vflag &= ~INP_SOCKREF; 1859111145Sjlemon#ifdef INET6 1860157386Srwatson if (inp->inp_vflag & INP_IPV6PROTO) { 1861157386Srwatson in6_pcbdetach(inp); 1862157386Srwatson in6_pcbfree(inp); 1863157386Srwatson } else { 1864157386Srwatson in_pcbdetach(inp); 1865157386Srwatson in_pcbfree(inp); 1866157386Srwatson } 1867157386Srwatson#endif 1868157386Srwatson ACCEPT_LOCK(); 1869157386Srwatson SOCK_LOCK(so); 1870157386Srwatson KASSERT(so->so_state & SS_PROTOREF, 1871157386Srwatson ("tcp_twclose: INP_SOCKREF && !SS_PROTOREF")); 1872157386Srwatson so->so_state &= ~SS_PROTOREF; 1873157386Srwatson sofree(so); 1874157376Srwatson } else { 1875157386Srwatson /* 1876157386Srwatson * If we don't own the only reference, the socket and 1877157386Srwatson * inpcb need to be left around to be handled by 1878157386Srwatson * tcp_usr_detach() later. 1879157386Srwatson */ 1880157386Srwatson INP_UNLOCK(inp); 1881157376Srwatson } 1882157386Srwatson } else { 1883157376Srwatson#ifdef INET6 1884157376Srwatson if (inp->inp_vflag & INP_IPV6PROTO) 1885157376Srwatson in6_pcbfree(inp); 1886157376Srwatson else 1887157376Srwatson#endif 1888157376Srwatson in_pcbfree(inp); 1889157386Srwatson } 1890111145Sjlemon tcpstat.tcps_closed++; 1891126002Spjd crfree(tw->tw_cred); 1892126002Spjd tw->tw_cred = NULL; 1893112009Sjlemon if (reuse) 1894157376Srwatson return; 1895112009Sjlemon uma_zfree(tcptw_zone, tw); 1896111145Sjlemon} 1897111145Sjlemon 1898111145Sjlemonint 1899126351Srwatsontcp_twrespond(struct tcptw *tw, int flags) 1900111145Sjlemon{ 1901111145Sjlemon struct inpcb *inp = tw->tw_inpcb; 1902111145Sjlemon struct tcphdr *th; 1903111145Sjlemon struct mbuf *m; 1904111145Sjlemon struct ip *ip = NULL; 1905111145Sjlemon u_int8_t *optp; 1906111145Sjlemon u_int hdrlen, optlen; 1907111145Sjlemon int error; 1908111145Sjlemon#ifdef INET6 1909111145Sjlemon struct ip6_hdr *ip6 = NULL; 1910111145Sjlemon int isipv6 = inp->inp_inc.inc_isipv6; 1911111145Sjlemon#endif 1912111145Sjlemon 1913138020Srwatson INP_LOCK_ASSERT(inp); 1914138020Srwatson 1915151967Sandre m = m_gethdr(M_DONTWAIT, MT_DATA); 1916111145Sjlemon if (m == NULL) 1917111145Sjlemon return (ENOBUFS); 1918111145Sjlemon m->m_data += max_linkhdr; 1919111145Sjlemon 1920114794Srwatson#ifdef MAC 1921123607Srwatson mac_create_mbuf_from_inpcb(inp, m); 1922114794Srwatson#endif 1923114794Srwatson 1924111153Sjlemon#ifdef INET6 1925111145Sjlemon if (isipv6) { 1926111145Sjlemon hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 1927111145Sjlemon ip6 = mtod(m, struct ip6_hdr *); 1928111145Sjlemon th = (struct tcphdr *)(ip6 + 1); 1929111145Sjlemon tcpip_fillheaders(inp, ip6, th); 1930111153Sjlemon } else 1931111153Sjlemon#endif 1932111153Sjlemon { 1933111145Sjlemon hdrlen = sizeof(struct tcpiphdr); 1934111145Sjlemon ip = mtod(m, struct ip *); 1935111145Sjlemon th = (struct tcphdr *)(ip + 1); 1936111145Sjlemon tcpip_fillheaders(inp, ip, th); 1937111145Sjlemon } 1938111145Sjlemon optp = (u_int8_t *)(th + 1); 1939133874Srwatson 1940133874Srwatson /* 1941111145Sjlemon * Send a timestamp and echo-reply if both our side and our peer 1942111145Sjlemon * have sent timestamps in our SYN's and this is not a RST. 1943133874Srwatson */ 1944111145Sjlemon if (tw->t_recent && flags == TH_ACK) { 1945111145Sjlemon u_int32_t *lp = (u_int32_t *)optp; 1946111145Sjlemon 1947133874Srwatson /* Form timestamp option as shown in appendix A of RFC 1323. */ 1948133874Srwatson *lp++ = htonl(TCPOPT_TSTAMP_HDR); 1949133874Srwatson *lp++ = htonl(ticks); 1950133874Srwatson *lp = htonl(tw->t_recent); 1951133874Srwatson optp += TCPOLEN_TSTAMP_APPA; 1952133874Srwatson } 1953111145Sjlemon 1954111145Sjlemon optlen = optp - (u_int8_t *)(th + 1); 1955111145Sjlemon 1956111145Sjlemon m->m_len = hdrlen + optlen; 1957111145Sjlemon m->m_pkthdr.len = m->m_len; 1958111145Sjlemon 1959111145Sjlemon KASSERT(max_linkhdr + m->m_len <= MHLEN, ("tcptw: mbuf too small")); 1960111145Sjlemon 1961111145Sjlemon th->th_seq = htonl(tw->snd_nxt); 1962111145Sjlemon th->th_ack = htonl(tw->rcv_nxt); 1963111145Sjlemon th->th_off = (sizeof(struct tcphdr) + optlen) >> 2; 1964111145Sjlemon th->th_flags = flags; 1965111145Sjlemon th->th_win = htons(tw->last_win); 1966111145Sjlemon 1967111153Sjlemon#ifdef INET6 1968111145Sjlemon if (isipv6) { 1969111145Sjlemon th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr), 1970111145Sjlemon sizeof(struct tcphdr) + optlen); 1971122922Sandre ip6->ip6_hlim = in6_selecthlim(inp, NULL); 1972122922Sandre error = ip6_output(m, inp->in6p_outputopts, NULL, 1973111145Sjlemon (tw->tw_so_options & SO_DONTROUTE), NULL, NULL, inp); 1974111153Sjlemon } else 1975111153Sjlemon#endif 1976111153Sjlemon { 1977111145Sjlemon th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 1978133874Srwatson htons(sizeof(struct tcphdr) + optlen + IPPROTO_TCP)); 1979111145Sjlemon m->m_pkthdr.csum_flags = CSUM_TCP; 1980111145Sjlemon m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 1981111145Sjlemon ip->ip_len = m->m_pkthdr.len; 1982124248Sandre if (path_mtu_discovery) 1983124248Sandre ip->ip_off |= IP_DF; 1984122922Sandre error = ip_output(m, inp->inp_options, NULL, 1985134793Sjmg ((tw->tw_so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 1986134793Sjmg NULL, inp); 1987111145Sjlemon } 1988111145Sjlemon if (flags & TH_ACK) 1989111145Sjlemon tcpstat.tcps_sndacks++; 1990111145Sjlemon else 1991111145Sjlemon tcpstat.tcps_sndctrl++; 1992111145Sjlemon tcpstat.tcps_sndtotal++; 1993111145Sjlemon return (error); 1994111145Sjlemon} 1995111145Sjlemon 1996111145Sjlemon/* 1997102017Sdillon * TCP BANDWIDTH DELAY PRODUCT WINDOW LIMITING 1998102017Sdillon * 1999102017Sdillon * This code attempts to calculate the bandwidth-delay product as a 2000102017Sdillon * means of determining the optimal window size to maximize bandwidth, 2001102017Sdillon * minimize RTT, and avoid the over-allocation of buffers on interfaces and 2002102017Sdillon * routers. This code also does a fairly good job keeping RTTs in check 2003102017Sdillon * across slow links like modems. We implement an algorithm which is very 2004102017Sdillon * similar (but not meant to be) TCP/Vegas. The code operates on the 2005102017Sdillon * transmitter side of a TCP connection and so only effects the transmit 2006102017Sdillon * side of the connection. 2007102017Sdillon * 2008102017Sdillon * BACKGROUND: TCP makes no provision for the management of buffer space 2009133874Srwatson * at the end points or at the intermediate routers and switches. A TCP 2010102017Sdillon * stream, whether using NewReno or not, will eventually buffer as 2011102017Sdillon * many packets as it is able and the only reason this typically works is 2012102017Sdillon * due to the fairly small default buffers made available for a connection 2013102017Sdillon * (typicaly 16K or 32K). As machines use larger windows and/or window 2014102017Sdillon * scaling it is now fairly easy for even a single TCP connection to blow-out 2015133874Srwatson * all available buffer space not only on the local interface, but on 2016102017Sdillon * intermediate routers and switches as well. NewReno makes a misguided 2017102017Sdillon * attempt to 'solve' this problem by waiting for an actual failure to occur, 2018102017Sdillon * then backing off, then steadily increasing the window again until another 2019102017Sdillon * failure occurs, ad-infinitum. This results in terrible oscillation that 2020102017Sdillon * is only made worse as network loads increase and the idea of intentionally 2021102017Sdillon * blowing out network buffers is, frankly, a terrible way to manage network 2022102017Sdillon * resources. 2023102017Sdillon * 2024102017Sdillon * It is far better to limit the transmit window prior to the failure 2025102017Sdillon * condition being achieved. There are two general ways to do this: First 2026102017Sdillon * you can 'scan' through different transmit window sizes and locate the 2027102017Sdillon * point where the RTT stops increasing, indicating that you have filled the 2028102017Sdillon * pipe, then scan backwards until you note that RTT stops decreasing, then 2029102017Sdillon * repeat ad-infinitum. This method works in principle but has severe 2030102017Sdillon * implementation issues due to RTT variances, timer granularity, and 2031102017Sdillon * instability in the algorithm which can lead to many false positives and 2032102017Sdillon * create oscillations as well as interact badly with other TCP streams 2033102017Sdillon * implementing the same algorithm. 2034102017Sdillon * 2035102017Sdillon * The second method is to limit the window to the bandwidth delay product 2036102017Sdillon * of the link. This is the method we implement. RTT variances and our 2037133874Srwatson * own manipulation of the congestion window, bwnd, can potentially 2038102017Sdillon * destabilize the algorithm. For this reason we have to stabilize the 2039102017Sdillon * elements used to calculate the window. We do this by using the minimum 2040102017Sdillon * observed RTT, the long term average of the observed bandwidth, and 2041102017Sdillon * by adding two segments worth of slop. It isn't perfect but it is able 2042102017Sdillon * to react to changing conditions and gives us a very stable basis on 2043102017Sdillon * which to extend the algorithm. 2044102017Sdillon */ 2045102017Sdillonvoid 2046102017Sdillontcp_xmit_bandwidth_limit(struct tcpcb *tp, tcp_seq ack_seq) 2047102017Sdillon{ 2048102017Sdillon u_long bw; 2049102017Sdillon u_long bwnd; 2050102017Sdillon int save_ticks; 2051102017Sdillon 2052138410Srwatson INP_LOCK_ASSERT(tp->t_inpcb); 2053138410Srwatson 2054102017Sdillon /* 2055102017Sdillon * If inflight_enable is disabled in the middle of a tcp connection, 2056102017Sdillon * make sure snd_bwnd is effectively disabled. 2057102017Sdillon */ 2058155767Sandre if (tcp_inflight_enable == 0 || tp->t_rttlow < tcp_inflight_rttthresh) { 2059102017Sdillon tp->snd_bwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT; 2060102017Sdillon tp->snd_bandwidth = 0; 2061102017Sdillon return; 2062102017Sdillon } 2063102017Sdillon 2064102017Sdillon /* 2065102017Sdillon * Figure out the bandwidth. Due to the tick granularity this 2066102017Sdillon * is a very rough number and it MUST be averaged over a fairly 2067102017Sdillon * long period of time. XXX we need to take into account a link 2068102017Sdillon * that is not using all available bandwidth, but for now our 2069102017Sdillon * slop will ramp us up if this case occurs and the bandwidth later 2070102017Sdillon * increases. 2071102368Sdillon * 2072102368Sdillon * Note: if ticks rollover 'bw' may wind up negative. We must 2073102368Sdillon * effectively reset t_bw_rtttime for this case. 2074102017Sdillon */ 2075102017Sdillon save_ticks = ticks; 2076102017Sdillon if ((u_int)(save_ticks - tp->t_bw_rtttime) < 1) 2077102017Sdillon return; 2078102017Sdillon 2079133874Srwatson bw = (int64_t)(ack_seq - tp->t_bw_rtseq) * hz / 2080102017Sdillon (save_ticks - tp->t_bw_rtttime); 2081102017Sdillon tp->t_bw_rtttime = save_ticks; 2082102017Sdillon tp->t_bw_rtseq = ack_seq; 2083102368Sdillon if (tp->t_bw_rtttime == 0 || (int)bw < 0) 2084102017Sdillon return; 2085102017Sdillon bw = ((int64_t)tp->snd_bandwidth * 15 + bw) >> 4; 2086102017Sdillon 2087102017Sdillon tp->snd_bandwidth = bw; 2088102017Sdillon 2089102017Sdillon /* 2090102017Sdillon * Calculate the semi-static bandwidth delay product, plus two maximal 2091102017Sdillon * segments. The additional slop puts us squarely in the sweet 2092107881Sdillon * spot and also handles the bandwidth run-up case and stabilization. 2093107881Sdillon * Without the slop we could be locking ourselves into a lower 2094107881Sdillon * bandwidth. 2095102017Sdillon * 2096102017Sdillon * Situations Handled: 2097102017Sdillon * (1) Prevents over-queueing of packets on LANs, especially on 2098102017Sdillon * high speed LANs, allowing larger TCP buffers to be 2099133874Srwatson * specified, and also does a good job preventing 2100102017Sdillon * over-queueing of packets over choke points like modems 2101102017Sdillon * (at least for the transmit side). 2102102017Sdillon * 2103102017Sdillon * (2) Is able to handle changing network loads (bandwidth 2104102017Sdillon * drops so bwnd drops, bandwidth increases so bwnd 2105102017Sdillon * increases). 2106102017Sdillon * 2107102017Sdillon * (3) Theoretically should stabilize in the face of multiple 2108102017Sdillon * connections implementing the same algorithm (this may need 2109102017Sdillon * a little work). 2110107881Sdillon * 2111107881Sdillon * (4) Stability value (defaults to 20 = 2 maximal packets) can 2112107881Sdillon * be adjusted with a sysctl but typically only needs to be 2113107881Sdillon * on very slow connections. A value no smaller then 5 2114107881Sdillon * should be used, but only reduce this default if you have 2115107881Sdillon * no other choice. 2116102017Sdillon */ 2117102017Sdillon#define USERTT ((tp->t_srtt + tp->t_rttbest) / 2) 2118107881Sdillon bwnd = (int64_t)bw * USERTT / (hz << TCP_RTT_SHIFT) + tcp_inflight_stab * tp->t_maxseg / 10; 2119102368Sdillon#undef USERTT 2120102017Sdillon 2121102017Sdillon if (tcp_inflight_debug > 0) { 2122102017Sdillon static int ltime; 2123102017Sdillon if ((u_int)(ticks - ltime) >= hz / tcp_inflight_debug) { 2124102017Sdillon ltime = ticks; 2125102017Sdillon printf("%p bw %ld rttbest %d srtt %d bwnd %ld\n", 2126102017Sdillon tp, 2127102017Sdillon bw, 2128102017Sdillon tp->t_rttbest, 2129102017Sdillon tp->t_srtt, 2130102017Sdillon bwnd 2131102017Sdillon ); 2132102017Sdillon } 2133102017Sdillon } 2134102017Sdillon if ((long)bwnd < tcp_inflight_min) 2135102017Sdillon bwnd = tcp_inflight_min; 2136102017Sdillon if (bwnd > tcp_inflight_max) 2137102017Sdillon bwnd = tcp_inflight_max; 2138102017Sdillon if ((long)bwnd < tp->t_maxseg * 2) 2139102017Sdillon bwnd = tp->t_maxseg * 2; 2140102017Sdillon tp->snd_bwnd = bwnd; 2141102017Sdillon} 2142102017Sdillon 2143125680Sbms#ifdef TCP_SIGNATURE 2144125680Sbms/* 2145125783Sbms * Callback function invoked by m_apply() to digest TCP segment data 2146125783Sbms * contained within an mbuf chain. 2147125783Sbms */ 2148125783Sbmsstatic int 2149125783Sbmstcp_signature_apply(void *fstate, void *data, u_int len) 2150125783Sbms{ 2151125783Sbms 2152125819Sbms MD5Update(fstate, (u_char *)data, len); 2153125783Sbms return (0); 2154125783Sbms} 2155125783Sbms 2156125783Sbms/* 2157125680Sbms * Compute TCP-MD5 hash of a TCPv4 segment. (RFC2385) 2158125680Sbms * 2159125741Sbms * Parameters: 2160125741Sbms * m pointer to head of mbuf chain 2161125741Sbms * off0 offset to TCP header within the mbuf chain 2162125741Sbms * len length of TCP segment data, excluding options 2163125741Sbms * optlen length of TCP segment options 2164125741Sbms * buf pointer to storage for computed MD5 digest 2165125741Sbms * direction direction of flow (IPSEC_DIR_INBOUND or OUTBOUND) 2166125741Sbms * 2167125680Sbms * We do this over ip, tcphdr, segment data, and the key in the SADB. 2168125680Sbms * When called from tcp_input(), we can be sure that th_sum has been 2169125680Sbms * zeroed out and verified already. 2170125680Sbms * 2171125680Sbms * This function is for IPv4 use only. Calling this function with an 2172125680Sbms * IPv6 packet in the mbuf chain will yield undefined results. 2173125680Sbms * 2174125680Sbms * Return 0 if successful, otherwise return -1. 2175125680Sbms * 2176125680Sbms * XXX The key is retrieved from the system's PF_KEY SADB, by keying a 2177125680Sbms * search with the destination IP address, and a 'magic SPI' to be 2178125680Sbms * determined by the application. This is hardcoded elsewhere to 1179 2179125680Sbms * right now. Another branch of this code exists which uses the SPD to 2180125680Sbms * specify per-application flows but it is unstable. 2181125680Sbms */ 2182125680Sbmsint 2183125783Sbmstcp_signature_compute(struct mbuf *m, int off0, int len, int optlen, 2184125741Sbms u_char *buf, u_int direction) 2185125680Sbms{ 2186125680Sbms union sockaddr_union dst; 2187125680Sbms struct ippseudo ippseudo; 2188125680Sbms MD5_CTX ctx; 2189125680Sbms int doff; 2190125680Sbms struct ip *ip; 2191125680Sbms struct ipovly *ipovly; 2192125680Sbms struct secasvar *sav; 2193125680Sbms struct tcphdr *th; 2194125680Sbms u_short savecsum; 2195125680Sbms 2196125741Sbms KASSERT(m != NULL, ("NULL mbuf chain")); 2197125741Sbms KASSERT(buf != NULL, ("NULL signature pointer")); 2198125741Sbms 2199125741Sbms /* Extract the destination from the IP header in the mbuf. */ 2200125680Sbms ip = mtod(m, struct ip *); 2201125680Sbms bzero(&dst, sizeof(union sockaddr_union)); 2202125680Sbms dst.sa.sa_len = sizeof(struct sockaddr_in); 2203125680Sbms dst.sa.sa_family = AF_INET; 2204125680Sbms dst.sin.sin_addr = (direction == IPSEC_DIR_INBOUND) ? 2205125680Sbms ip->ip_src : ip->ip_dst; 2206125741Sbms 2207125741Sbms /* Look up an SADB entry which matches the address of the peer. */ 2208125680Sbms sav = KEY_ALLOCSA(&dst, IPPROTO_TCP, htonl(TCP_SIG_SPI)); 2209125680Sbms if (sav == NULL) { 2210125680Sbms printf("%s: SADB lookup failed for %s\n", __func__, 2211125680Sbms inet_ntoa(dst.sin.sin_addr)); 2212125680Sbms return (EINVAL); 2213125680Sbms } 2214125741Sbms 2215125680Sbms MD5Init(&ctx); 2216125680Sbms ipovly = (struct ipovly *)ip; 2217125680Sbms th = (struct tcphdr *)((u_char *)ip + off0); 2218125680Sbms doff = off0 + sizeof(struct tcphdr) + optlen; 2219125741Sbms 2220125680Sbms /* 2221125680Sbms * Step 1: Update MD5 hash with IP pseudo-header. 2222125680Sbms * 2223125680Sbms * XXX The ippseudo header MUST be digested in network byte order, 2224125680Sbms * or else we'll fail the regression test. Assume all fields we've 2225125680Sbms * been doing arithmetic on have been in host byte order. 2226125680Sbms * XXX One cannot depend on ipovly->ih_len here. When called from 2227125680Sbms * tcp_output(), the underlying ip_len member has not yet been set. 2228125680Sbms */ 2229125680Sbms ippseudo.ippseudo_src = ipovly->ih_src; 2230125680Sbms ippseudo.ippseudo_dst = ipovly->ih_dst; 2231125680Sbms ippseudo.ippseudo_pad = 0; 2232125680Sbms ippseudo.ippseudo_p = IPPROTO_TCP; 2233125680Sbms ippseudo.ippseudo_len = htons(len + sizeof(struct tcphdr) + optlen); 2234125680Sbms MD5Update(&ctx, (char *)&ippseudo, sizeof(struct ippseudo)); 2235125741Sbms 2236125680Sbms /* 2237125680Sbms * Step 2: Update MD5 hash with TCP header, excluding options. 2238125680Sbms * The TCP checksum must be set to zero. 2239125680Sbms */ 2240125680Sbms savecsum = th->th_sum; 2241125680Sbms th->th_sum = 0; 2242125680Sbms MD5Update(&ctx, (char *)th, sizeof(struct tcphdr)); 2243125680Sbms th->th_sum = savecsum; 2244125741Sbms 2245125680Sbms /* 2246125680Sbms * Step 3: Update MD5 hash with TCP segment data. 2247125680Sbms * Use m_apply() to avoid an early m_pullup(). 2248125680Sbms */ 2249125680Sbms if (len > 0) 2250125783Sbms m_apply(m, doff, len, tcp_signature_apply, &ctx); 2251125741Sbms 2252125680Sbms /* 2253125680Sbms * Step 4: Update MD5 hash with shared secret. 2254125680Sbms */ 2255125680Sbms MD5Update(&ctx, _KEYBUF(sav->key_auth), _KEYLEN(sav->key_auth)); 2256125680Sbms MD5Final(buf, &ctx); 2257125741Sbms 2258125680Sbms key_sa_recordxfer(sav, m); 2259125680Sbms KEY_FREESAV(&sav); 2260125680Sbms return (0); 2261125680Sbms} 2262125680Sbms#endif /* TCP_SIGNATURE */ 2263141886Smaxim 2264141886Smaximstatic int 2265141886Smaximsysctl_drop(SYSCTL_HANDLER_ARGS) 2266141886Smaxim{ 2267141886Smaxim /* addrs[0] is a foreign socket, addrs[1] is a local one. */ 2268141886Smaxim struct sockaddr_storage addrs[2]; 2269141886Smaxim struct inpcb *inp; 2270141886Smaxim struct tcpcb *tp; 2271150804Smaxim struct tcptw *tw; 2272141886Smaxim struct sockaddr_in *fin, *lin; 2273141886Smaxim#ifdef INET6 2274141886Smaxim struct sockaddr_in6 *fin6, *lin6; 2275141886Smaxim struct in6_addr f6, l6; 2276141886Smaxim#endif 2277141886Smaxim int error; 2278141886Smaxim 2279141886Smaxim inp = NULL; 2280141886Smaxim fin = lin = NULL; 2281141886Smaxim#ifdef INET6 2282141886Smaxim fin6 = lin6 = NULL; 2283141886Smaxim#endif 2284141886Smaxim error = 0; 2285141886Smaxim 2286141886Smaxim if (req->oldptr != NULL || req->oldlen != 0) 2287141886Smaxim return (EINVAL); 2288141886Smaxim if (req->newptr == NULL) 2289141886Smaxim return (EPERM); 2290141886Smaxim if (req->newlen < sizeof(addrs)) 2291141886Smaxim return (ENOMEM); 2292141886Smaxim error = SYSCTL_IN(req, &addrs, sizeof(addrs)); 2293141886Smaxim if (error) 2294141886Smaxim return (error); 2295141886Smaxim 2296141886Smaxim switch (addrs[0].ss_family) { 2297141886Smaxim#ifdef INET6 2298141886Smaxim case AF_INET6: 2299141886Smaxim fin6 = (struct sockaddr_in6 *)&addrs[0]; 2300141886Smaxim lin6 = (struct sockaddr_in6 *)&addrs[1]; 2301141886Smaxim if (fin6->sin6_len != sizeof(struct sockaddr_in6) || 2302141886Smaxim lin6->sin6_len != sizeof(struct sockaddr_in6)) 2303141886Smaxim return (EINVAL); 2304141886Smaxim if (IN6_IS_ADDR_V4MAPPED(&fin6->sin6_addr)) { 2305141886Smaxim if (!IN6_IS_ADDR_V4MAPPED(&lin6->sin6_addr)) 2306141886Smaxim return (EINVAL); 2307141886Smaxim in6_sin6_2_sin_in_sock((struct sockaddr *)&addrs[0]); 2308141886Smaxim in6_sin6_2_sin_in_sock((struct sockaddr *)&addrs[1]); 2309141886Smaxim fin = (struct sockaddr_in *)&addrs[0]; 2310141886Smaxim lin = (struct sockaddr_in *)&addrs[1]; 2311141886Smaxim break; 2312141886Smaxim } 2313148385Sume error = sa6_embedscope(fin6, ip6_use_defzone); 2314141886Smaxim if (error) 2315148385Sume return (error); 2316148385Sume error = sa6_embedscope(lin6, ip6_use_defzone); 2317148616Sume if (error) 2318148385Sume return (error); 2319141886Smaxim break; 2320141886Smaxim#endif 2321141886Smaxim case AF_INET: 2322141886Smaxim fin = (struct sockaddr_in *)&addrs[0]; 2323141886Smaxim lin = (struct sockaddr_in *)&addrs[1]; 2324141886Smaxim if (fin->sin_len != sizeof(struct sockaddr_in) || 2325141886Smaxim lin->sin_len != sizeof(struct sockaddr_in)) 2326141886Smaxim return (EINVAL); 2327141886Smaxim break; 2328141886Smaxim default: 2329141886Smaxim return (EINVAL); 2330141886Smaxim } 2331141886Smaxim INP_INFO_WLOCK(&tcbinfo); 2332141886Smaxim switch (addrs[0].ss_family) { 2333141886Smaxim#ifdef INET6 2334141886Smaxim case AF_INET6: 2335141886Smaxim inp = in6_pcblookup_hash(&tcbinfo, &f6, fin6->sin6_port, 2336141886Smaxim &l6, lin6->sin6_port, 0, NULL); 2337141886Smaxim break; 2338141886Smaxim#endif 2339141886Smaxim case AF_INET: 2340141886Smaxim inp = in_pcblookup_hash(&tcbinfo, fin->sin_addr, fin->sin_port, 2341141886Smaxim lin->sin_addr, lin->sin_port, 0, NULL); 2342141886Smaxim break; 2343141886Smaxim } 2344141886Smaxim if (inp != NULL) { 2345141886Smaxim INP_LOCK(inp); 2346157427Srwatson if (inp->inp_vflag & INP_TIMEWAIT) { 2347157474Srwatson /* 2348157474Srwatson * XXXRW: There currently exists a state where an 2349157474Srwatson * inpcb is present, but its timewait state has been 2350157474Srwatson * discarded. For now, don't allow dropping of this 2351157474Srwatson * type of inpcb. 2352157474Srwatson */ 2353157427Srwatson tw = intotw(inp); 2354157474Srwatson if (tw != NULL) 2355157474Srwatson tcp_twclose(tw, 0); 2356157427Srwatson } else if (!(inp->inp_vflag & INP_DROPPED) && 2357157427Srwatson !(inp->inp_socket->so_options & SO_ACCEPTCONN)) { 2358157427Srwatson tp = intotcpcb(inp); 2359157427Srwatson tcp_drop(tp, ECONNABORTED); 2360157427Srwatson } 2361157427Srwatson INP_UNLOCK(inp); 2362141886Smaxim } else 2363141886Smaxim error = ESRCH; 2364141886Smaxim INP_INFO_WUNLOCK(&tcbinfo); 2365141886Smaxim return (error); 2366141886Smaxim} 2367141886Smaxim 2368141886SmaximSYSCTL_PROC(_net_inet_tcp, TCPCTL_DROP, drop, 2369141886Smaxim CTLTYPE_STRUCT|CTLFLAG_WR|CTLFLAG_SKIP, NULL, 2370141886Smaxim 0, sysctl_drop, "", "Drop TCP connection"); 2371