File:  [DragonFly] / src / sys / netinet / tcp_subr.c
Revision 1.30: download - view: text, annotated - select for diffs
Wed Apr 28 08:00:35 2004 UTC (10 years, 6 months ago) by hsu
Branches: MAIN
CVS tags: HEAD
Remember if an inpcb was entered into the wildcard table to save
some cycles when a connection is closed.

    1: /*
    2:  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
    3:  *	The Regents of the University of California.  All rights reserved.
    4:  *
    5:  * Redistribution and use in source and binary forms, with or without
    6:  * modification, are permitted provided that the following conditions
    7:  * are met:
    8:  * 1. Redistributions of source code must retain the above copyright
    9:  *    notice, this list of conditions and the following disclaimer.
   10:  * 2. Redistributions in binary form must reproduce the above copyright
   11:  *    notice, this list of conditions and the following disclaimer in the
   12:  *    documentation and/or other materials provided with the distribution.
   13:  * 3. All advertising materials mentioning features or use of this software
   14:  *    must display the following acknowledgement:
   15:  *	This product includes software developed by the University of
   16:  *	California, Berkeley and its contributors.
   17:  * 4. Neither the name of the University nor the names of its contributors
   18:  *    may be used to endorse or promote products derived from this software
   19:  *    without specific prior written permission.
   20:  *
   21:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   22:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   23:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   24:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   25:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   26:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   27:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   28:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   29:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   30:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   31:  * SUCH DAMAGE.
   32:  *
   33:  *	@(#)tcp_subr.c	8.2 (Berkeley) 5/24/95
   34:  * $FreeBSD: src/sys/netinet/tcp_subr.c,v 1.73.2.31 2003/01/24 05:11:34 sam Exp $
   35:  * $DragonFly: src/sys/netinet/tcp_subr.c,v 1.30 2004/04/28 08:00:35 hsu Exp $
   36:  */
   37: 
   38: #include "opt_compat.h"
   39: #include "opt_inet6.h"
   40: #include "opt_ipsec.h"
   41: #include "opt_tcpdebug.h"
   42: 
   43: #include <sys/param.h>
   44: #include <sys/systm.h>
   45: #include <sys/callout.h>
   46: #include <sys/kernel.h>
   47: #include <sys/sysctl.h>
   48: #include <sys/malloc.h>
   49: #include <sys/mbuf.h>
   50: #ifdef INET6
   51: #include <sys/domain.h>
   52: #endif
   53: #include <sys/proc.h>
   54: #include <sys/socket.h>
   55: #include <sys/socketvar.h>
   56: #include <sys/protosw.h>
   57: #include <sys/random.h>
   58: #include <sys/in_cksum.h>
   59: 
   60: #include <vm/vm_zone.h>
   61: 
   62: #include <net/route.h>
   63: #include <net/if.h>
   64: #include <net/netisr.h>
   65: 
   66: #define	_IP_VHL
   67: #include <netinet/in.h>
   68: #include <netinet/in_systm.h>
   69: #include <netinet/ip.h>
   70: #include <netinet/ip6.h>
   71: #include <netinet/in_pcb.h>
   72: #include <netinet6/in6_pcb.h>
   73: #include <netinet/in_var.h>
   74: #include <netinet/ip_var.h>
   75: #include <netinet6/ip6_var.h>
   76: #include <netinet/tcp.h>
   77: #include <netinet/tcp_fsm.h>
   78: #include <netinet/tcp_seq.h>
   79: #include <netinet/tcp_timer.h>
   80: #include <netinet/tcp_var.h>
   81: #include <netinet6/tcp6_var.h>
   82: #include <netinet/tcpip.h>
   83: #ifdef TCPDEBUG
   84: #include <netinet/tcp_debug.h>
   85: #endif
   86: #include <netinet6/ip6protosw.h>
   87: 
   88: #ifdef IPSEC
   89: #include <netinet6/ipsec.h>
   90: #ifdef INET6
   91: #include <netinet6/ipsec6.h>
   92: #endif
   93: #endif
   94: 
   95: #ifdef FAST_IPSEC
   96: #include <netipsec/ipsec.h>
   97: #ifdef INET6
   98: #include <netipsec/ipsec6.h>
   99: #endif
  100: #define	IPSEC
  101: #endif
  102: 
  103: #include <sys/md5.h>
  104: 
  105: #include <sys/msgport2.h>
  106: 
  107: int tcp_mssdflt = TCP_MSS;
  108: SYSCTL_INT(_net_inet_tcp, TCPCTL_MSSDFLT, mssdflt, CTLFLAG_RW, 
  109:     &tcp_mssdflt, 0, "Default TCP Maximum Segment Size");
  110: 
  111: #ifdef INET6
  112: int tcp_v6mssdflt = TCP6_MSS;
  113: SYSCTL_INT(_net_inet_tcp, TCPCTL_V6MSSDFLT, v6mssdflt, CTLFLAG_RW,
  114:     &tcp_v6mssdflt, 0, "Default TCP Maximum Segment Size for IPv6");
  115: #endif
  116: 
  117: #if 0
  118: static int tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ;
  119: SYSCTL_INT(_net_inet_tcp, TCPCTL_RTTDFLT, rttdflt, CTLFLAG_RW, 
  120:     &tcp_rttdflt, 0, "Default maximum TCP Round Trip Time");
  121: #endif
  122: 
  123: int tcp_do_rfc1323 = 1;
  124: SYSCTL_INT(_net_inet_tcp, TCPCTL_DO_RFC1323, rfc1323, CTLFLAG_RW, 
  125:     &tcp_do_rfc1323, 0, "Enable rfc1323 (high performance TCP) extensions");
  126: 
  127: int tcp_do_rfc1644 = 0;
  128: SYSCTL_INT(_net_inet_tcp, TCPCTL_DO_RFC1644, rfc1644, CTLFLAG_RW, 
  129:     &tcp_do_rfc1644, 0, "Enable rfc1644 (TTCP) extensions");
  130: 
  131: static int tcp_tcbhashsize = 0;
  132: SYSCTL_INT(_net_inet_tcp, OID_AUTO, tcbhashsize, CTLFLAG_RD,
  133:      &tcp_tcbhashsize, 0, "Size of TCP control block hashtable");
  134: 
  135: static int do_tcpdrain = 1;
  136: SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_tcpdrain, CTLFLAG_RW, &do_tcpdrain, 0,
  137:      "Enable tcp_drain routine for extra help when low on mbufs");
  138: 
  139: /* XXX JH */
  140: SYSCTL_INT(_net_inet_tcp, OID_AUTO, pcbcount, CTLFLAG_RD, 
  141:     &tcbinfo[0].ipi_count, 0, "Number of active PCBs");
  142: 
  143: static int icmp_may_rst = 1;
  144: SYSCTL_INT(_net_inet_tcp, OID_AUTO, icmp_may_rst, CTLFLAG_RW, &icmp_may_rst, 0, 
  145:     "Certain ICMP unreachable messages may abort connections in SYN_SENT");
  146: 
  147: static int tcp_isn_reseed_interval = 0;
  148: SYSCTL_INT(_net_inet_tcp, OID_AUTO, isn_reseed_interval, CTLFLAG_RW,
  149:     &tcp_isn_reseed_interval, 0, "Seconds between reseeding of ISN secret");
  150: 
  151: /*
  152:  * TCP bandwidth limiting sysctls.  Note that the default lower bound of 
  153:  * 1024 exists only for debugging.  A good production default would be 
  154:  * something like 6100.
  155:  */
  156: static int tcp_inflight_enable = 0;
  157: SYSCTL_INT(_net_inet_tcp, OID_AUTO, inflight_enable, CTLFLAG_RW,
  158:     &tcp_inflight_enable, 0, "Enable automatic TCP inflight data limiting");
  159: 
  160: static int tcp_inflight_debug = 0;
  161: SYSCTL_INT(_net_inet_tcp, OID_AUTO, inflight_debug, CTLFLAG_RW,
  162:     &tcp_inflight_debug, 0, "Debug TCP inflight calculations");
  163: 
  164: static int tcp_inflight_min = 6144;
  165: SYSCTL_INT(_net_inet_tcp, OID_AUTO, inflight_min, CTLFLAG_RW,
  166:     &tcp_inflight_min, 0, "Lower bound for TCP inflight window");
  167: 
  168: static int tcp_inflight_max = TCP_MAXWIN << TCP_MAX_WINSHIFT;
  169: SYSCTL_INT(_net_inet_tcp, OID_AUTO, inflight_max, CTLFLAG_RW,
  170:     &tcp_inflight_max, 0, "Upper bound for TCP inflight window");
  171: 
  172: static int tcp_inflight_stab = 20;
  173: SYSCTL_INT(_net_inet_tcp, OID_AUTO, inflight_stab, CTLFLAG_RW,
  174:     &tcp_inflight_stab, 0, "Slop in maximal packets / 10 (20 = 2 packets)");
  175: 
  176: static void tcp_cleartaocache (void);
  177: static void tcp_notify (struct inpcb *, int);
  178: 
  179: struct tcp_stats tcpstats_ary[MAXCPU];
  180: #ifdef SMP
  181: static int
  182: sysctl_tcpstats(SYSCTL_HANDLER_ARGS)
  183: {
  184: 	int cpu, error = 0;
  185: 
  186: 	for (cpu = 0; cpu < ncpus; ++cpu) {
  187: 		if ((error = SYSCTL_OUT(req, (void *)&tcpstats_ary[cpu],
  188: 					sizeof(struct tcp_stats))))
  189: 			break;
  190: 		if ((error = SYSCTL_IN(req, (void *)&tcpstats_ary[cpu],
  191: 				       sizeof(struct tcp_stats))))
  192: 			break;
  193: 	}
  194: 
  195: 	return (error);
  196: }
  197: SYSCTL_PROC(_net_inet_tcp, TCPCTL_STATS, stats, (CTLTYPE_OPAQUE | CTLFLAG_RW),
  198:     0, 0, sysctl_tcpstats, "S,tcp_stats", "TCP statistics");
  199: #else
  200: SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RW,
  201:     &tcpstat, tcp_stats, "TCP statistics");
  202: #endif
  203: 
  204: /*
  205:  * Target size of TCP PCB hash tables. Must be a power of two.
  206:  *
  207:  * Note that this can be overridden by the kernel environment
  208:  * variable net.inet.tcp.tcbhashsize
  209:  */
  210: #ifndef TCBHASHSIZE
  211: #define	TCBHASHSIZE	512
  212: #endif
  213: 
  214: /*
  215:  * This is the actual shape of what we allocate using the zone
  216:  * allocator.  Doing it this way allows us to protect both structures
  217:  * using the same generation count, and also eliminates the overhead
  218:  * of allocating tcpcbs separately.  By hiding the structure here,
  219:  * we avoid changing most of the rest of the code (although it needs
  220:  * to be changed, eventually, for greater efficiency).
  221:  */
  222: #define	ALIGNMENT	32
  223: #define	ALIGNM1		(ALIGNMENT - 1)
  224: struct	inp_tp {
  225: 	union {
  226: 		struct	inpcb inp;
  227: 		char	align[(sizeof(struct inpcb) + ALIGNM1) & ~ALIGNM1];
  228: 	} inp_tp_u;
  229: 	struct	tcpcb tcb;
  230: 	struct	callout inp_tp_rexmt, inp_tp_persist, inp_tp_keep, inp_tp_2msl;
  231: 	struct	callout inp_tp_delack;
  232: };
  233: #undef ALIGNMENT
  234: #undef ALIGNM1
  235: 
  236: /*
  237:  * Tcp initialization
  238:  */
  239: void
  240: tcp_init()
  241: {
  242: 	struct inpcbporthead *porthashbase;
  243: 	u_long porthashmask;
  244: 	struct vm_zone *ipi_zone;
  245: 	int hashsize = TCBHASHSIZE;
  246: 	int cpu;
  247: 
  248: 	tcp_ccgen = 1;
  249: 	tcp_cleartaocache();
  250: 
  251: 	tcp_delacktime = TCPTV_DELACK;
  252: 	tcp_keepinit = TCPTV_KEEP_INIT;
  253: 	tcp_keepidle = TCPTV_KEEP_IDLE;
  254: 	tcp_keepintvl = TCPTV_KEEPINTVL;
  255: 	tcp_maxpersistidle = TCPTV_KEEP_IDLE;
  256: 	tcp_msl = TCPTV_MSL;
  257: 	tcp_rexmit_min = TCPTV_MIN;
  258: 	tcp_rexmit_slop = TCPTV_CPU_VAR;
  259: 
  260: 	TUNABLE_INT_FETCH("net.inet.tcp.tcbhashsize", &hashsize);
  261: 	if (!powerof2(hashsize)) {
  262: 		printf("WARNING: TCB hash size not a power of 2\n");
  263: 		hashsize = 512; /* safe default */
  264: 	}
  265: 	tcp_tcbhashsize = hashsize;
  266: 	porthashbase = hashinit(hashsize, M_PCB, &porthashmask);
  267: 	ipi_zone = zinit("tcpcb", sizeof(struct inp_tp), maxsockets,
  268: 			 ZONE_INTERRUPT, 0);
  269: 
  270: 	for (cpu = 0; cpu < ncpus2; cpu++) {
  271: 		LIST_INIT(&tcbinfo[cpu].listhead);
  272: 		tcbinfo[cpu].hashbase = hashinit(hashsize, M_PCB,
  273: 		    &tcbinfo[cpu].hashmask);
  274: 		tcbinfo[cpu].porthashbase = porthashbase;
  275: 		tcbinfo[cpu].porthashmask = porthashmask;
  276: 		tcbinfo[cpu].wildcardhashbase = hashinit(hashsize, M_PCB,
  277: 		    &tcbinfo[cpu].wildcardhashmask);
  278: 		tcbinfo[cpu].ipi_zone = ipi_zone;
  279: 	}
  280: 
  281: 	tcp_reass_maxseg = nmbclusters / 16;
  282: 	TUNABLE_INT_FETCH("net.inet.tcp.reass.maxsegments", &tcp_reass_maxseg);
  283: 
  284: #ifdef INET6
  285: #define	TCP_MINPROTOHDR (sizeof(struct ip6_hdr) + sizeof(struct tcphdr))
  286: #else
  287: #define	TCP_MINPROTOHDR (sizeof(struct tcpiphdr))
  288: #endif
  289: 	if (max_protohdr < TCP_MINPROTOHDR)
  290: 		max_protohdr = TCP_MINPROTOHDR;
  291: 	if (max_linkhdr + TCP_MINPROTOHDR > MHLEN)
  292: 		panic("tcp_init");
  293: #undef TCP_MINPROTOHDR
  294: 
  295: 	/*
  296: 	 * Initialize TCP statistics.
  297: 	 *
  298: 	 * It is layed out as an array which is has one element for UP,
  299: 	 * and SMP_MAXCPU elements for SMP.  This allows us to retain
  300: 	 * the access mechanism from userland for both UP and SMP.
  301: 	 */
  302: #ifdef SMP
  303: 	for (cpu = 0; cpu < ncpus; ++cpu) {
  304: 		bzero(&tcpstats_ary[cpu], sizeof(struct tcp_stats));
  305: 	}
  306: #else
  307: 	bzero(&tcpstat, sizeof(struct tcp_stats));
  308: #endif
  309: 
  310: 	syncache_init();
  311: 	tcp_thread_init();
  312: }
  313: 
  314: /*
  315:  * Fill in the IP and TCP headers for an outgoing packet, given the tcpcb.
  316:  * tcp_template used to store this data in mbufs, but we now recopy it out
  317:  * of the tcpcb each time to conserve mbufs.
  318:  */
  319: void
  320: tcp_fillheaders(struct tcpcb *tp, void *ip_ptr, void *tcp_ptr)
  321: {
  322: 	struct inpcb *inp = tp->t_inpcb;
  323: 	struct tcphdr *tcp_hdr = (struct tcphdr *)tcp_ptr;
  324: 
  325: #ifdef INET6
  326: 	if (inp->inp_vflag & INP_IPV6) {
  327: 		struct ip6_hdr *ip6;
  328: 
  329: 		ip6 = (struct ip6_hdr *)ip_ptr;
  330: 		ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
  331: 			(inp->in6p_flowinfo & IPV6_FLOWINFO_MASK);
  332: 		ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
  333: 			(IPV6_VERSION & IPV6_VERSION_MASK);
  334: 		ip6->ip6_nxt = IPPROTO_TCP;
  335: 		ip6->ip6_plen = sizeof(struct tcphdr);
  336: 		ip6->ip6_src = inp->in6p_laddr;
  337: 		ip6->ip6_dst = inp->in6p_faddr;
  338: 		tcp_hdr->th_sum = 0;
  339: 	} else
  340: #endif
  341: 	{
  342: 		struct ip *ip = (struct ip *) ip_ptr;
  343: 
  344: 		ip->ip_vhl = IP_VHL_BORING;
  345: 		ip->ip_tos = 0;
  346: 		ip->ip_len = 0;
  347: 		ip->ip_id = 0;
  348: 		ip->ip_off = 0;
  349: 		ip->ip_ttl = 0;
  350: 		ip->ip_sum = 0;
  351: 		ip->ip_p = IPPROTO_TCP;
  352: 		ip->ip_src = inp->inp_laddr;
  353: 		ip->ip_dst = inp->inp_faddr;
  354: 		tcp_hdr->th_sum = in_pseudo(ip->ip_src.s_addr,
  355: 				    ip->ip_dst.s_addr,
  356: 				    htons(sizeof(struct tcphdr) + IPPROTO_TCP));
  357: 	}
  358: 
  359: 	tcp_hdr->th_sport = inp->inp_lport;
  360: 	tcp_hdr->th_dport = inp->inp_fport;
  361: 	tcp_hdr->th_seq = 0;
  362: 	tcp_hdr->th_ack = 0;
  363: 	tcp_hdr->th_x2 = 0;
  364: 	tcp_hdr->th_off = 5;
  365: 	tcp_hdr->th_flags = 0;
  366: 	tcp_hdr->th_win = 0;
  367: 	tcp_hdr->th_urp = 0;
  368: }
  369: 
  370: /*
  371:  * Create template to be used to send tcp packets on a connection.
  372:  * Allocates an mbuf and fills in a skeletal tcp/ip header.  The only
  373:  * use for this function is in keepalives, which use tcp_respond.
  374:  */
  375: struct tcptemp *
  376: tcp_maketemplate(struct tcpcb *tp)
  377: {
  378: 	struct mbuf *m;
  379: 	struct tcptemp *n;
  380: 
  381: 	m = m_get(M_DONTWAIT, MT_HEADER);
  382: 	if (m == NULL)
  383: 		return (NULL);
  384: 	m->m_len = sizeof(struct tcptemp);
  385: 	n = mtod(m, struct tcptemp *);
  386: 
  387: 	tcp_fillheaders(tp, (void *)&n->tt_ipgen, (void *)&n->tt_t);
  388: 	return (n);
  389: }
  390: 
  391: /*
  392:  * Send a single message to the TCP at address specified by
  393:  * the given TCP/IP header.  If m == NULL, then we make a copy
  394:  * of the tcpiphdr at ti and send directly to the addressed host.
  395:  * This is used to force keep alive messages out using the TCP
  396:  * template for a connection.  If flags are given then we send
  397:  * a message back to the TCP which originated the * segment ti,
  398:  * and discard the mbuf containing it and any other attached mbufs.
  399:  *
  400:  * In any case the ack and sequence number of the transmitted
  401:  * segment are as specified by the parameters.
  402:  *
  403:  * NOTE: If m != NULL, then ti must point to *inside* the mbuf.
  404:  */
  405: void
  406: tcp_respond(struct tcpcb *tp, void *ipgen, struct tcphdr *th, struct mbuf *m,
  407: 	    tcp_seq ack, tcp_seq seq, int flags)
  408: {
  409: 	int tlen;
  410: 	int win = 0;
  411: 	struct route *ro = NULL;
  412: 	struct route sro;
  413: 	struct ip *ip = ipgen;
  414: 	struct tcphdr *nth;
  415: 	int ipflags = 0;
  416: 	struct route_in6 *ro6 = NULL;
  417: 	struct route_in6 sro6;
  418: 	struct ip6_hdr *ip6 = ipgen;
  419: #ifdef INET6
  420: 	boolean_t isipv6 = (IP_VHL_V(ip->ip_vhl) == 6);
  421: #else
  422: 	const boolean_t isipv6 = FALSE;
  423: #endif
  424: 
  425: 	if (tp != NULL) {
  426: 		if (!(flags & TH_RST)) {
  427: 			win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
  428: 			if (win > (long)TCP_MAXWIN << tp->rcv_scale)
  429: 				win = (long)TCP_MAXWIN << tp->rcv_scale;
  430: 		}
  431: 		if (isipv6)
  432: 			ro6 = &tp->t_inpcb->in6p_route;
  433: 		else
  434: 			ro = &tp->t_inpcb->inp_route;
  435: 	} else {
  436: 		if (isipv6) {
  437: 			ro6 = &sro6;
  438: 			bzero(ro6, sizeof *ro6);
  439: 		} else {
  440: 			ro = &sro;
  441: 			bzero(ro, sizeof *ro);
  442: 		}
  443: 	}
  444: 	if (m == NULL) {
  445: 		m = m_gethdr(M_DONTWAIT, MT_HEADER);
  446: 		if (m == NULL)
  447: 			return;
  448: 		tlen = 0;
  449: 		m->m_data += max_linkhdr;
  450: 		if (isipv6) {
  451: 			bcopy(ip6, mtod(m, caddr_t), sizeof(struct ip6_hdr));
  452: 			ip6 = mtod(m, struct ip6_hdr *);
  453: 			nth = (struct tcphdr *)(ip6 + 1);
  454: 		} else {
  455: 			bcopy(ip, mtod(m, caddr_t), sizeof(struct ip));
  456: 			ip = mtod(m, struct ip *);
  457: 			nth = (struct tcphdr *)(ip + 1);
  458: 		}
  459: 		bcopy(th, nth, sizeof(struct tcphdr));
  460: 		flags = TH_ACK;
  461: 	} else {
  462: 		m_freem(m->m_next);
  463: 		m->m_next = NULL;
  464: 		m->m_data = (caddr_t)ipgen;
  465: 		/* m_len is set later */
  466: 		tlen = 0;
  467: #define	xchg(a, b, type) { type t; t = a; a = b; b = t; }
  468: 		if (isipv6) {
  469: 			xchg(ip6->ip6_dst, ip6->ip6_src, struct in6_addr);
  470: 			nth = (struct tcphdr *)(ip6 + 1);
  471: 		} else {
  472: 			xchg(ip->ip_dst.s_addr, ip->ip_src.s_addr, n_long);
  473: 			nth = (struct tcphdr *)(ip + 1);
  474: 		}
  475: 		if (th != nth) {
  476: 			/*
  477: 			 * this is usually a case when an extension header
  478: 			 * exists between the IPv6 header and the
  479: 			 * TCP header.
  480: 			 */
  481: 			nth->th_sport = th->th_sport;
  482: 			nth->th_dport = th->th_dport;
  483: 		}
  484: 		xchg(nth->th_dport, nth->th_sport, n_short);
  485: #undef xchg
  486: 	}
  487: 	if (isipv6) {
  488: 		ip6->ip6_flow = 0;
  489: 		ip6->ip6_vfc = IPV6_VERSION;
  490: 		ip6->ip6_nxt = IPPROTO_TCP;
  491: 		ip6->ip6_plen = htons((u_short)(sizeof(struct tcphdr) + tlen));
  492: 		tlen += sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
  493: 	} else {
  494: 		tlen += sizeof(struct tcpiphdr);
  495: 		ip->ip_len = tlen;
  496: 		ip->ip_ttl = ip_defttl;
  497: 	}
  498: 	m->m_len = tlen;
  499: 	m->m_pkthdr.len = tlen;
  500: 	m->m_pkthdr.rcvif = (struct ifnet *) NULL;
  501: 	nth->th_seq = htonl(seq);
  502: 	nth->th_ack = htonl(ack);
  503: 	nth->th_x2 = 0;
  504: 	nth->th_off = sizeof(struct tcphdr) >> 2;
  505: 	nth->th_flags = flags;
  506: 	if (tp != NULL)
  507: 		nth->th_win = htons((u_short) (win >> tp->rcv_scale));
  508: 	else
  509: 		nth->th_win = htons((u_short)win);
  510: 	nth->th_urp = 0;
  511: 	if (isipv6) {
  512: 		nth->th_sum = 0;
  513: 		nth->th_sum = in6_cksum(m, IPPROTO_TCP,
  514: 					sizeof(struct ip6_hdr),
  515: 					tlen - sizeof(struct ip6_hdr));
  516: 		ip6->ip6_hlim = in6_selecthlim(tp ? tp->t_inpcb : NULL,
  517: 					       (ro6 && ro6->ro_rt) ?
  518: 					           ro6->ro_rt->rt_ifp : NULL);
  519: 	} else {
  520: 		nth->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
  521: 		    htons((u_short)(tlen - sizeof(struct ip) + ip->ip_p)));
  522: 		m->m_pkthdr.csum_flags = CSUM_TCP;
  523: 		m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
  524: 	}
  525: #ifdef TCPDEBUG
  526: 	if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
  527: 		tcp_trace(TA_OUTPUT, 0, tp, mtod(m, void *), th, 0);
  528: #endif
  529: 	if (isipv6) {
  530: 		(void)ip6_output(m, NULL, ro6, ipflags, NULL, NULL,
  531: 				 tp ? tp->t_inpcb : NULL);
  532: 		if ((ro6 == &sro6) && (ro6->ro_rt != NULL)) {
  533: 			RTFREE(ro6->ro_rt);
  534: 			ro6->ro_rt = NULL;
  535: 		}
  536: 	} else {
  537: 		(void)ip_output(m, NULL, ro, ipflags, NULL,
  538: 				tp ? tp->t_inpcb : NULL);
  539: 		if ((ro == &sro) && (ro->ro_rt != NULL)) {
  540: 			RTFREE(ro->ro_rt);
  541: 			ro->ro_rt = NULL;
  542: 		}
  543: 	}
  544: }
  545: 
  546: /*
  547:  * Create a new TCP control block, making an
  548:  * empty reassembly queue and hooking it to the argument
  549:  * protocol control block.  The `inp' parameter must have
  550:  * come from the zone allocator set up in tcp_init().
  551:  */
  552: struct tcpcb *
  553: tcp_newtcpcb(struct inpcb *inp)
  554: {
  555: 	struct inp_tp *it;
  556: 	struct tcpcb *tp;
  557: #ifdef INET6
  558: 	boolean_t isipv6 = ((inp->inp_vflag & INP_IPV6) != 0);
  559: #else
  560: 	const boolean_t isipv6 = FALSE;
  561: #endif
  562: 
  563: 	it = (struct inp_tp *)inp;
  564: 	tp = &it->tcb;
  565: 	bzero(tp, sizeof(struct tcpcb));
  566: 	LIST_INIT(&tp->t_segq);
  567: 	tp->t_maxseg = tp->t_maxopd = isipv6 ? tcp_v6mssdflt : tcp_mssdflt;
  568: 
  569: 	/* Set up our timeouts. */
  570: 	callout_init(tp->tt_rexmt = &it->inp_tp_rexmt);
  571: 	callout_init(tp->tt_persist = &it->inp_tp_persist);
  572: 	callout_init(tp->tt_keep = &it->inp_tp_keep);
  573: 	callout_init(tp->tt_2msl = &it->inp_tp_2msl);
  574: 	callout_init(tp->tt_delack = &it->inp_tp_delack);
  575: 
  576: 	if (tcp_do_rfc1323)
  577: 		tp->t_flags = (TF_REQ_SCALE | TF_REQ_TSTMP);
  578: 	if (tcp_do_rfc1644)
  579: 		tp->t_flags |= TF_REQ_CC;
  580: 	tp->t_inpcb = inp;	/* XXX */
  581: 	/*
  582: 	 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
  583: 	 * rtt estimate.  Set rttvar so that srtt + 4 * rttvar gives
  584: 	 * reasonable initial retransmit time.
  585: 	 */
  586: 	tp->t_srtt = TCPTV_SRTTBASE;
  587: 	tp->t_rttvar =
  588: 	    ((TCPTV_RTOBASE - TCPTV_SRTTBASE) << TCP_RTTVAR_SHIFT) / 4;
  589: 	tp->t_rttmin = tcp_rexmit_min;
  590: 	tp->t_rxtcur = TCPTV_RTOBASE;
  591: 	tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
  592: 	tp->snd_bwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
  593: 	tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
  594: 	tp->t_rcvtime = ticks;
  595: 	tp->t_bw_rtttime = ticks;
  596: 	/*
  597: 	 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
  598: 	 * because the socket may be bound to an IPv6 wildcard address,
  599: 	 * which may match an IPv4-mapped IPv6 address.
  600: 	 */
  601: 	inp->inp_ip_ttl = ip_defttl;
  602: 	inp->inp_ppcb = (caddr_t)tp;
  603: 	return (tp);		/* XXX */
  604: }
  605: 
  606: /*
  607:  * Drop a TCP connection, reporting the specified error.
  608:  * If connection is synchronized, then send a RST to peer.
  609:  */
  610: struct tcpcb *
  611: tcp_drop(struct tcpcb *tp, int errno)
  612: {
  613: 	struct socket *so = tp->t_inpcb->inp_socket;
  614: 
  615: 	if (TCPS_HAVERCVDSYN(tp->t_state)) {
  616: 		tp->t_state = TCPS_CLOSED;
  617: 		(void) tcp_output(tp);
  618: 		tcpstat.tcps_drops++;
  619: 	} else
  620: 		tcpstat.tcps_conndrops++;
  621: 	if (errno == ETIMEDOUT && tp->t_softerror)
  622: 		errno = tp->t_softerror;
  623: 	so->so_error = errno;
  624: 	return (tcp_close(tp));
  625: }
  626: 
  627: #ifdef SMP
  628: struct netmsg_remwildcard {
  629: 	struct lwkt_msg		nm_lmsg;
  630: 	struct inpcb		*nm_inp;
  631: 	struct inpcbinfo	*nm_pcbinfo;
  632: };
  633: 
  634: static int
  635: in_pcbremwildcardhash_handler(struct lwkt_msg *msg0)
  636: {
  637: 	struct netmsg_remwildcard *msg = (struct netmsg_remwildcard *)msg0;
  638: 
  639: 	in_pcbremwildcardhash_oncpu(msg->nm_inp, msg->nm_pcbinfo);
  640: 	lwkt_replymsg(&msg->nm_lmsg, 0);
  641: 	return (EASYNC);
  642: }
  643: #endif
  644: 
  645: /*
  646:  * Close a TCP control block:
  647:  *	discard all space held by the tcp
  648:  *	discard internet protocol block
  649:  *	wake up any sleepers
  650:  */
  651: struct tcpcb *
  652: tcp_close(struct tcpcb *tp)
  653: {
  654: 	struct tseg_qent *q;
  655: 	struct inpcb *inp = tp->t_inpcb;
  656: 	struct socket *so = inp->inp_socket;
  657: 	struct rtentry *rt;
  658: 	boolean_t dosavessthresh;
  659: #ifdef SMP
  660: 	int cpu;
  661: #endif
  662: #ifdef INET6
  663: 	boolean_t isipv6 = ((inp->inp_vflag & INP_IPV6) != 0);
  664: #else
  665: 	const boolean_t isipv6 = FALSE;
  666: #endif
  667: 
  668: 	/*
  669: 	 * Make sure that all of our timers are stopped before we
  670: 	 * delete the PCB.
  671: 	 */
  672: 	callout_stop(tp->tt_rexmt);
  673: 	callout_stop(tp->tt_persist);
  674: 	callout_stop(tp->tt_keep);
  675: 	callout_stop(tp->tt_2msl);
  676: 	callout_stop(tp->tt_delack);
  677: 
  678: 	/*
  679: 	 * If we got enough samples through the srtt filter,
  680: 	 * save the rtt and rttvar in the routing entry.
  681: 	 * 'Enough' is arbitrarily defined as the 16 samples.
  682: 	 * 16 samples is enough for the srtt filter to converge
  683: 	 * to within 5% of the correct value; fewer samples and
  684: 	 * we could save a very bogus rtt.
  685: 	 *
  686: 	 * Don't update the default route's characteristics and don't
  687: 	 * update anything that the user "locked".
  688: 	 */
  689: 	if (tp->t_rttupdated >= 16) {
  690: 		u_long i = 0;
  691: 
  692: 		if (isipv6) {
  693: 			struct sockaddr_in6 *sin6;
  694: 
  695: 			if ((rt = inp->in6p_route.ro_rt) == NULL)
  696: 				goto no_valid_rt;
  697: 			sin6 = (struct sockaddr_in6 *)rt_key(rt);
  698: 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
  699: 				goto no_valid_rt;
  700: 		} else
  701: 			if ((rt = inp->inp_route.ro_rt) == NULL ||
  702: 			    ((struct sockaddr_in *)rt_key(rt))->
  703: 			     sin_addr.s_addr == INADDR_ANY)
  704: 				goto no_valid_rt;
  705: 
  706: 		if (!(rt->rt_rmx.rmx_locks & RTV_RTT)) {
  707: 			i = tp->t_srtt * (RTM_RTTUNIT / (hz * TCP_RTT_SCALE));
  708: 			if (rt->rt_rmx.rmx_rtt && i)
  709: 				/*
  710: 				 * filter this update to half the old & half
  711: 				 * the new values, converting scale.
  712: 				 * See route.h and tcp_var.h for a
  713: 				 * description of the scaling constants.
  714: 				 */
  715: 				rt->rt_rmx.rmx_rtt =
  716: 				    (rt->rt_rmx.rmx_rtt + i) / 2;
  717: 			else
  718: 				rt->rt_rmx.rmx_rtt = i;
  719: 			tcpstat.tcps_cachedrtt++;
  720: 		}
  721: 		if (!(rt->rt_rmx.rmx_locks & RTV_RTTVAR)) {
  722: 			i = tp->t_rttvar *
  723: 			    (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE));
  724: 			if (rt->rt_rmx.rmx_rttvar && i)
  725: 				rt->rt_rmx.rmx_rttvar =
  726: 				    (rt->rt_rmx.rmx_rttvar + i) / 2;
  727: 			else
  728: 				rt->rt_rmx.rmx_rttvar = i;
  729: 			tcpstat.tcps_cachedrttvar++;
  730: 		}
  731: 		/*
  732: 		 * The old comment here said:
  733: 		 * update the pipelimit (ssthresh) if it has been updated
  734: 		 * already or if a pipesize was specified & the threshhold
  735: 		 * got below half the pipesize.  I.e., wait for bad news
  736: 		 * before we start updating, then update on both good
  737: 		 * and bad news.
  738: 		 *
  739: 		 * But we want to save the ssthresh even if no pipesize is
  740: 		 * specified explicitly in the route, because such
  741: 		 * connections still have an implicit pipesize specified
  742: 		 * by the global tcp_sendspace.  In the absence of a reliable
  743: 		 * way to calculate the pipesize, it will have to do.
  744: 		 */
  745: 		i = tp->snd_ssthresh;
  746: 		if (rt->rt_rmx.rmx_sendpipe != 0)
  747: 			dosavessthresh = (i < rt->rt_rmx.rmx_sendpipe/2);
  748: 		else
  749: 			dosavessthresh = (i < so->so_snd.sb_hiwat/2);
  750: 		if (dosavessthresh ||
  751: 		    (!(rt->rt_rmx.rmx_locks & RTV_SSTHRESH) && (i != 0) &&
  752: 		     (rt->rt_rmx.rmx_ssthresh != 0))) {
  753: 			/*
  754: 			 * convert the limit from user data bytes to
  755: 			 * packets then to packet data bytes.
  756: 			 */
  757: 			i = (i + tp->t_maxseg / 2) / tp->t_maxseg;
  758: 			if (i < 2)
  759: 				i = 2;
  760: 			i *= tp->t_maxseg +
  761: 			     (isipv6 ?
  762: 			      sizeof(struct ip6_hdr) + sizeof(struct tcphdr) :
  763: 			      sizeof(struct tcpiphdr));
  764: 			if (rt->rt_rmx.rmx_ssthresh)
  765: 				rt->rt_rmx.rmx_ssthresh =
  766: 				    (rt->rt_rmx.rmx_ssthresh + i) / 2;
  767: 			else
  768: 				rt->rt_rmx.rmx_ssthresh = i;
  769: 			tcpstat.tcps_cachedssthresh++;
  770: 		}
  771: 	}
  772: 
  773: no_valid_rt:
  774: 	/* free the reassembly queue, if any */
  775: 	while((q = LIST_FIRST(&tp->t_segq)) != NULL) {
  776: 		LIST_REMOVE(q, tqe_q);
  777: 		m_freem(q->tqe_m);
  778: 		FREE(q, M_TSEGQ);
  779: 		tcp_reass_qsize--;
  780: 	}
  781: 	inp->inp_ppcb = NULL;
  782: 	soisdisconnected(so);
  783: 
  784: #ifdef SMP
  785: 	if (inp->inp_flags & INP_WILDCARD_MP) {
  786: 		for (cpu = 0; cpu < ncpus2; cpu ++) {
  787: 			struct netmsg_remwildcard *msg;
  788: 
  789: 			msg = malloc(sizeof(struct netmsg_remwildcard),
  790: 			    M_LWKTMSG, M_INTWAIT);
  791: 			lwkt_initmsg(&msg->nm_lmsg, &netisr_afree_rport, 0,
  792: 			    lwkt_cmd_func(in_pcbremwildcardhash_handler),
  793: 			    lwkt_cmd_op_none);
  794: 			msg->nm_inp = inp;
  795: 			msg->nm_pcbinfo = &tcbinfo[cpu];
  796: 			lwkt_sendmsg(tcp_cport(cpu), &msg->nm_lmsg);
  797: 		}
  798: 	}
  799: #endif
  800: 
  801: #ifdef INET6
  802: 	if (INP_CHECK_SOCKAF(so, AF_INET6))
  803: 		in6_pcbdetach(inp);
  804: 	else
  805: #endif
  806: 		in_pcbdetach(inp);
  807: 	tcpstat.tcps_closed++;
  808: 	return (NULL);
  809: }
  810: 
  811: static __inline void
  812: tcp_drain_oncpu(struct inpcbhead *head)
  813: {
  814: 	struct inpcb *inpb;
  815: 	struct tcpcb *tcpb;
  816: 	struct tseg_qent *te;
  817: 
  818: 	LIST_FOREACH(inpb, head, inp_list) {
  819: 		if ((tcpb = intotcpcb(inpb))) {
  820: 			while ((te = LIST_FIRST(&tcpb->t_segq)) != NULL) {
  821: 				LIST_REMOVE(te, tqe_q);
  822: 				m_freem(te->tqe_m);
  823: 				FREE(te, M_TSEGQ);
  824: 				tcp_reass_qsize--;
  825: 			}
  826: 		}
  827: 	}
  828: }
  829: 
  830: #ifdef SMP
  831: struct netmsg_tcp_drain {
  832: 	struct lwkt_msg		nm_lmsg;
  833: 	struct inpcbhead	*nm_head;
  834: };
  835: 
  836: static int
  837: tcp_drain_handler(lwkt_msg_t lmsg)
  838: {
  839: 	struct netmsg_tcp_drain *nm = (void *)lmsg;
  840: 
  841: 	tcp_drain_oncpu(nm->nm_head);
  842: 	lwkt_replymsg(lmsg, 0);
  843: 	return(EASYNC);
  844: }
  845: #endif
  846: 
  847: void
  848: tcp_drain()
  849: {
  850: #ifdef SMP
  851: 	int cpu;
  852: #endif
  853: 
  854: 	if (!do_tcpdrain)
  855: 		return;
  856: 
  857: 	/*
  858: 	 * Walk the tcpbs, if existing, and flush the reassembly queue,
  859: 	 * if there is one...
  860: 	 * XXX: The "Net/3" implementation doesn't imply that the TCP
  861: 	 *	reassembly queue should be flushed, but in a situation
  862: 	 *	where we're really low on mbufs, this is potentially
  863: 	 *	useful.
  864: 	 */
  865: #ifdef SMP
  866: 	for (cpu = 0; cpu < ncpus2; cpu++) {
  867: 		struct netmsg_tcp_drain *msg;
  868: 
  869: 		if (cpu == mycpu->gd_cpuid) {
  870: 			tcp_drain_oncpu(&tcbinfo[cpu].listhead);
  871: 		} else {
  872: 			msg = malloc(sizeof(struct netmsg_tcp_drain),
  873: 				    M_LWKTMSG, M_NOWAIT);
  874: 			if (msg == NULL)
  875: 				continue;
  876: 			lwkt_initmsg(&msg->nm_lmsg, &netisr_afree_rport, 0,
  877: 				lwkt_cmd_func(tcp_drain_handler),
  878: 				lwkt_cmd_op_none);
  879: 			msg->nm_head = &tcbinfo[cpu].listhead;
  880: 			lwkt_sendmsg(tcp_cport(cpu), &msg->nm_lmsg);
  881: 		}
  882: 	}
  883: #else
  884: 	tcp_drain_oncpu(&tcbinfo[0].listhead);
  885: #endif
  886: }
  887: 
  888: /*
  889:  * Notify a tcp user of an asynchronous error;
  890:  * store error as soft error, but wake up user
  891:  * (for now, won't do anything until can select for soft error).
  892:  *
  893:  * Do not wake up user since there currently is no mechanism for
  894:  * reporting soft errors (yet - a kqueue filter may be added).
  895:  */
  896: static void
  897: tcp_notify(struct inpcb *inp, int error)
  898: {
  899: 	struct tcpcb *tp = intotcpcb(inp);
  900: 
  901: 	/*
  902: 	 * Ignore some errors if we are hooked up.
  903: 	 * If connection hasn't completed, has retransmitted several times,
  904: 	 * and receives a second error, give up now.  This is better
  905: 	 * than waiting a long time to establish a connection that
  906: 	 * can never complete.
  907: 	 */
  908: 	if (tp->t_state == TCPS_ESTABLISHED &&
  909: 	     (error == EHOSTUNREACH || error == ENETUNREACH ||
  910: 	      error == EHOSTDOWN)) {
  911: 		return;
  912: 	} else if (tp->t_state < TCPS_ESTABLISHED && tp->t_rxtshift > 3 &&
  913: 	    tp->t_softerror)
  914: 		tcp_drop(tp, error);
  915: 	else
  916: 		tp->t_softerror = error;
  917: #if 0
  918: 	wakeup((caddr_t) &so->so_timeo);
  919: 	sorwakeup(so);
  920: 	sowwakeup(so);
  921: #endif
  922: }
  923: 
  924: static int
  925: tcp_pcblist(SYSCTL_HANDLER_ARGS)
  926: {
  927: 	int error, i, n, s;
  928: 	struct inpcb *inp, **inp_list;
  929: 	inp_gen_t gencnt;
  930: 	struct xinpgen xig;
  931: 
  932: 	/*
  933: 	 * The process of preparing the TCB list is too time-consuming and
  934: 	 * resource-intensive to repeat twice on every request.
  935: 	 */
  936: 	if (req->oldptr == NULL) {
  937: 		n = tcbinfo[mycpu->gd_cpuid].ipi_count;
  938: 		req->oldidx = 2 * (sizeof xig) +
  939: 			      (n + n/8) * sizeof(struct xtcpcb);
  940: 		return (0);
  941: 	}
  942: 
  943: 	if (req->newptr != NULL)
  944: 		return (EPERM);
  945: 
  946: 	/*
  947: 	 * OK, now we're committed to doing something.
  948: 	 */
  949: 	s = splnet();
  950: 	gencnt = tcbinfo[mycpu->gd_cpuid].ipi_gencnt;
  951: 	n = tcbinfo[mycpu->gd_cpuid].ipi_count;
  952: 	splx(s);
  953: 
  954: 	xig.xig_len = sizeof xig;
  955: 	xig.xig_count = n;
  956: 	xig.xig_gen = gencnt;
  957: 	xig.xig_sogen = so_gencnt;
  958: 	error = SYSCTL_OUT(req, &xig, sizeof xig);
  959: 	if (error != 0)
  960: 		return (error);
  961: 
  962: 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
  963: 	if (inp_list == NULL)
  964: 		return (ENOMEM);
  965: 
  966: 	s = splnet();
  967: 	for (inp = LIST_FIRST(&tcbinfo[mycpu->gd_cpuid].listhead), i = 0;
  968: 	    inp && i < n; inp = LIST_NEXT(inp, inp_list)) {
  969: 		if (inp->inp_gencnt <= gencnt && !prison_xinpcb(req->td, inp))
  970: 			inp_list[i++] = inp;
  971: 	}
  972: 	splx(s);
  973: 	n = i;
  974: 
  975: 	error = 0;
  976: 	for (i = 0; i < n; i++) {
  977: 		inp = inp_list[i];
  978: 		if (inp->inp_gencnt <= gencnt) {
  979: 			struct xtcpcb xt;
  980: 			caddr_t inp_ppcb;
  981: 			xt.xt_len = sizeof xt;
  982: 			/* XXX should avoid extra copy */
  983: 			bcopy(inp, &xt.xt_inp, sizeof *inp);
  984: 			inp_ppcb = inp->inp_ppcb;
  985: 			if (inp_ppcb != NULL)
  986: 				bcopy(inp_ppcb, &xt.xt_tp, sizeof xt.xt_tp);
  987: 			else
  988: 				bzero(&xt.xt_tp, sizeof xt.xt_tp);
  989: 			if (inp->inp_socket)
  990: 				sotoxsocket(inp->inp_socket, &xt.xt_socket);
  991: 			error = SYSCTL_OUT(req, &xt, sizeof xt);
  992: 		}
  993: 	}
  994: 	if (!error) {
  995: 		/*
  996: 		 * Give the user an updated idea of our state.
  997: 		 * If the generation differs from what we told
  998: 		 * her before, she knows that something happened
  999: 		 * while we were processing this request, and it
 1000: 		 * might be necessary to retry.
 1001: 		 */
 1002: 		s = splnet();
 1003: 		xig.xig_gen = tcbinfo[mycpu->gd_cpuid].ipi_gencnt;
 1004: 		xig.xig_sogen = so_gencnt;
 1005: 		xig.xig_count = tcbinfo[mycpu->gd_cpuid].ipi_count;
 1006: 		splx(s);
 1007: 		error = SYSCTL_OUT(req, &xig, sizeof xig);
 1008: 	}
 1009: 	free(inp_list, M_TEMP);
 1010: 	return (error);
 1011: }
 1012: 
 1013: SYSCTL_PROC(_net_inet_tcp, TCPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
 1014: 	    tcp_pcblist, "S,xtcpcb", "List of active TCP connections");
 1015: 
 1016: static int
 1017: tcp_getcred(SYSCTL_HANDLER_ARGS)
 1018: {
 1019: 	struct sockaddr_in addrs[2];
 1020: 	struct inpcb *inp;
 1021: 	int cpu;
 1022: 	int error, s;
 1023: 
 1024: 	error = suser(req->td);
 1025: 	if (error != 0)
 1026: 		return (error);
 1027: 	error = SYSCTL_IN(req, addrs, sizeof addrs);
 1028: 	if (error != 0)
 1029: 		return (error);
 1030: 	s = splnet();
 1031: 
 1032: 	cpu = tcp_addrcpu(addrs[1].sin_addr.s_addr, addrs[1].sin_port,
 1033: 	    addrs[0].sin_addr.s_addr, addrs[0].sin_port);
 1034: 	inp = in_pcblookup_hash(&tcbinfo[cpu], addrs[1].sin_addr,
 1035: 	    addrs[1].sin_port, addrs[0].sin_addr, addrs[0].sin_port, 0, NULL);
 1036: 	if (inp == NULL || inp->inp_socket == NULL) {
 1037: 		error = ENOENT;
 1038: 		goto out;
 1039: 	}
 1040: 	error = SYSCTL_OUT(req, inp->inp_socket->so_cred, sizeof(struct ucred));
 1041: out:
 1042: 	splx(s);
 1043: 	return (error);
 1044: }
 1045: 
 1046: SYSCTL_PROC(_net_inet_tcp, OID_AUTO, getcred, (CTLTYPE_OPAQUE | CTLFLAG_RW),
 1047:     0, 0, tcp_getcred, "S,ucred", "Get the ucred of a TCP connection");
 1048: 
 1049: #ifdef INET6
 1050: static int
 1051: tcp6_getcred(SYSCTL_HANDLER_ARGS)
 1052: {
 1053: 	struct sockaddr_in6 addrs[2];
 1054: 	struct inpcb *inp;
 1055: 	int error, s;
 1056: 	boolean_t mapped = FALSE;
 1057: 
 1058: 	error = suser(req->td);
 1059: 	if (error != 0)
 1060: 		return (error);
 1061: 	error = SYSCTL_IN(req, addrs, sizeof addrs);
 1062: 	if (error != 0)
 1063: 		return (error);
 1064: 	if (IN6_IS_ADDR_V4MAPPED(&addrs[0].sin6_addr)) {
 1065: 		if (IN6_IS_ADDR_V4MAPPED(&addrs[1].sin6_addr))
 1066: 			mapped = TRUE;
 1067: 		else
 1068: 			return (EINVAL);
 1069: 	}
 1070: 	s = splnet();
 1071: 	if (mapped) {
 1072: 		inp = in_pcblookup_hash(&tcbinfo[0],
 1073: 		    *(struct in_addr *)&addrs[1].sin6_addr.s6_addr[12],
 1074: 		    addrs[1].sin6_port,
 1075: 		    *(struct in_addr *)&addrs[0].sin6_addr.s6_addr[12],
 1076: 		    addrs[0].sin6_port,
 1077: 		    0, NULL);
 1078: 	} else {
 1079: 		inp = in6_pcblookup_hash(&tcbinfo[0],
 1080: 		    &addrs[1].sin6_addr, addrs[1].sin6_port,
 1081: 		    &addrs[0].sin6_addr, addrs[0].sin6_port,
 1082: 		    0, NULL);
 1083: 	}
 1084: 	if (inp == NULL || inp->inp_socket == NULL) {
 1085: 		error = ENOENT;
 1086: 		goto out;
 1087: 	}
 1088: 	error = SYSCTL_OUT(req, inp->inp_socket->so_cred, sizeof(struct ucred));
 1089: out:
 1090: 	splx(s);
 1091: 	return (error);
 1092: }
 1093: 
 1094: SYSCTL_PROC(_net_inet6_tcp6, OID_AUTO, getcred, (CTLTYPE_OPAQUE | CTLFLAG_RW),
 1095: 	    0, 0,
 1096: 	    tcp6_getcred, "S,ucred", "Get the ucred of a TCP6 connection");
 1097: #endif
 1098: 
 1099: void
 1100: tcp_ctlinput(int cmd, struct sockaddr *sa, void *vip)
 1101: {
 1102: 	struct ip *ip = vip;
 1103: 	struct tcphdr *th;
 1104: 	struct in_addr faddr;
 1105: 	struct inpcb *inp;
 1106: 	struct tcpcb *tp;
 1107: 	void (*notify)(struct inpcb *, int) = tcp_notify;
 1108: 	tcp_seq icmp_seq;
 1109: 	int cpu;
 1110: 	int s;
 1111: 
 1112: 	faddr = ((struct sockaddr_in *)sa)->sin_addr;
 1113: 	if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
 1114: 		return;
 1115: 
 1116: 	if (cmd == PRC_QUENCH)
 1117: 		notify = tcp_quench;
 1118: 	else if (icmp_may_rst &&
 1119: 		 (cmd == PRC_UNREACH_ADMIN_PROHIB || cmd == PRC_UNREACH_PORT ||
 1120: 		  cmd == PRC_TIMXCEED_INTRANS) &&
 1121: 		 ip != NULL)
 1122: 		notify = tcp_drop_syn_sent;
 1123: 	else if (cmd == PRC_MSGSIZE)
 1124: 		notify = tcp_mtudisc;
 1125: 	else if (PRC_IS_REDIRECT(cmd)) {
 1126: 		ip = NULL;
 1127: 		notify = in_rtchange;
 1128: 	} else if (cmd == PRC_HOSTDEAD)
 1129: 		ip = NULL;
 1130: 	else if ((unsigned)cmd > PRC_NCMDS || inetctlerrmap[cmd] == 0)
 1131: 		return;
 1132: 	if (ip != NULL) {
 1133: 		s = splnet();
 1134: 		th = (struct tcphdr *)((caddr_t)ip +
 1135: 				       (IP_VHL_HL(ip->ip_vhl) << 2));
 1136: 		cpu = tcp_addrcpu(faddr.s_addr, th->th_dport,
 1137: 				  ip->ip_src.s_addr, th->th_sport);
 1138: 		inp = in_pcblookup_hash(&tcbinfo[cpu], faddr, th->th_dport,
 1139: 					ip->ip_src, th->th_sport, 0, NULL);
 1140: 		if ((inp != NULL) && (inp->inp_socket != NULL)) {
 1141: 			icmp_seq = htonl(th->th_seq);
 1142: 			tp = intotcpcb(inp);
 1143: 			if (SEQ_GEQ(icmp_seq, tp->snd_una) &&
 1144: 			    SEQ_LT(icmp_seq, tp->snd_max))
 1145: 				(*notify)(inp, inetctlerrmap[cmd]);
 1146: 		} else {
 1147: 			struct in_conninfo inc;
 1148: 
 1149: 			inc.inc_fport = th->th_dport;
 1150: 			inc.inc_lport = th->th_sport;
 1151: 			inc.inc_faddr = faddr;
 1152: 			inc.inc_laddr = ip->ip_src;
 1153: #ifdef INET6
 1154: 			inc.inc_isipv6 = 0;
 1155: #endif
 1156: 			syncache_unreach(&inc, th);
 1157: 		}
 1158: 		splx(s);
 1159: 	} else {
 1160: 		for (cpu = 0; cpu < ncpus2; cpu++) {
 1161: 			in_pcbnotifyall(&tcbinfo[cpu].listhead, faddr,
 1162: 					inetctlerrmap[cmd], notify);
 1163: 		}
 1164: 	}
 1165: }
 1166: 
 1167: #ifdef INET6
 1168: void
 1169: tcp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
 1170: {
 1171: 	struct tcphdr th;
 1172: 	void (*notify) (struct inpcb *, int) = tcp_notify;
 1173: 	struct ip6_hdr *ip6;
 1174: 	struct mbuf *m;
 1175: 	struct ip6ctlparam *ip6cp = NULL;
 1176: 	const struct sockaddr_in6 *sa6_src = NULL;
 1177: 	int off;
 1178: 	struct tcp_portonly {
 1179: 		u_int16_t th_sport;
 1180: 		u_int16_t th_dport;
 1181: 	} *thp;
 1182: 
 1183: 	if (sa->sa_family != AF_INET6 ||
 1184: 	    sa->sa_len != sizeof(struct sockaddr_in6))
 1185: 		return;
 1186: 
 1187: 	if (cmd == PRC_QUENCH)
 1188: 		notify = tcp_quench;
 1189: 	else if (cmd == PRC_MSGSIZE)
 1190: 		notify = tcp_mtudisc;
 1191: 	else if (!PRC_IS_REDIRECT(cmd) &&
 1192: 		 ((unsigned)cmd > PRC_NCMDS || inet6ctlerrmap[cmd] == 0))
 1193: 		return;
 1194: 
 1195: 	/* if the parameter is from icmp6, decode it. */
 1196: 	if (d != NULL) {
 1197: 		ip6cp = (struct ip6ctlparam *)d;
 1198: 		m = ip6cp->ip6c_m;
 1199: 		ip6 = ip6cp->ip6c_ip6;
 1200: 		off = ip6cp->ip6c_off;
 1201: 		sa6_src = ip6cp->ip6c_src;
 1202: 	} else {
 1203: 		m = NULL;
 1204: 		ip6 = NULL;
 1205: 		off = 0;	/* fool gcc */
 1206: 		sa6_src = &sa6_any;
 1207: 	}
 1208: 
 1209: 	if (ip6 != NULL) {
 1210: 		struct in_conninfo inc;
 1211: 		/*
 1212: 		 * XXX: We assume that when IPV6 is non NULL,
 1213: 		 * M and OFF are valid.
 1214: 		 */
 1215: 
 1216: 		/* check if we can safely examine src and dst ports */
 1217: 		if (m->m_pkthdr.len < off + sizeof *thp)
 1218: 			return;
 1219: 
 1220: 		bzero(&th, sizeof th);
 1221: 		m_copydata(m, off, sizeof *thp, (caddr_t)&th);
 1222: 
 1223: 		in6_pcbnotify(&tcbinfo[0].listhead, sa, th.th_dport,
 1224: 		    (struct sockaddr *)ip6cp->ip6c_src,
 1225: 		    th.th_sport, cmd, notify);
 1226: 
 1227: 		inc.inc_fport = th.th_dport;
 1228: 		inc.inc_lport = th.th_sport;
 1229: 		inc.inc6_faddr = ((struct sockaddr_in6 *)sa)->sin6_addr;
 1230: 		inc.inc6_laddr = ip6cp->ip6c_src->sin6_addr;
 1231: 		inc.inc_isipv6 = 1;
 1232: 		syncache_unreach(&inc, &th);
 1233: 	} else
 1234: 		in6_pcbnotify(&tcbinfo[0].listhead, sa, 0,
 1235: 		    (const struct sockaddr *)sa6_src, 0, cmd, notify);
 1236: }
 1237: #endif
 1238: 
 1239: /*
 1240:  * Following is where TCP initial sequence number generation occurs.
 1241:  *
 1242:  * There are two places where we must use initial sequence numbers:
 1243:  * 1.  In SYN-ACK packets.
 1244:  * 2.  In SYN packets.
 1245:  *
 1246:  * All ISNs for SYN-ACK packets are generated by the syncache.  See
 1247:  * tcp_syncache.c for details.
 1248:  *
 1249:  * The ISNs in SYN packets must be monotonic; TIME_WAIT recycling
 1250:  * depends on this property.  In addition, these ISNs should be
 1251:  * unguessable so as to prevent connection hijacking.  To satisfy
 1252:  * the requirements of this situation, the algorithm outlined in
 1253:  * RFC 1948 is used to generate sequence numbers.
 1254:  *
 1255:  * Implementation details:
 1256:  *
 1257:  * Time is based off the system timer, and is corrected so that it
 1258:  * increases by one megabyte per second.  This allows for proper
 1259:  * recycling on high speed LANs while still leaving over an hour
 1260:  * before rollover.
 1261:  *
 1262:  * net.inet.tcp.isn_reseed_interval controls the number of seconds
 1263:  * between seeding of isn_secret.  This is normally set to zero,
 1264:  * as reseeding should not be necessary.
 1265:  *
 1266:  */
 1267: 
 1268: #define	ISN_BYTES_PER_SECOND 1048576
 1269: 
 1270: u_char isn_secret[32];
 1271: int isn_last_reseed;
 1272: MD5_CTX isn_ctx;
 1273: 
 1274: tcp_seq
 1275: tcp_new_isn(struct tcpcb *tp)
 1276: {
 1277: 	u_int32_t md5_buffer[4];
 1278: 	tcp_seq new_isn;
 1279: 
 1280: 	/* Seed if this is the first use, reseed if requested. */
 1281: 	if ((isn_last_reseed == 0) || ((tcp_isn_reseed_interval > 0) &&
 1282: 	     (((u_int)isn_last_reseed + (u_int)tcp_isn_reseed_interval*hz)
 1283: 		< (u_int)ticks))) {
 1284: 		read_random_unlimited(&isn_secret, sizeof isn_secret);
 1285: 		isn_last_reseed = ticks;
 1286: 	}
 1287: 
 1288: 	/* Compute the md5 hash and return the ISN. */
 1289: 	MD5Init(&isn_ctx);
 1290: 	MD5Update(&isn_ctx, (u_char *)&tp->t_inpcb->inp_fport, sizeof(u_short));
 1291: 	MD5Update(&isn_ctx, (u_char *)&tp->t_inpcb->inp_lport, sizeof(u_short));
 1292: #ifdef INET6
 1293: 	if (tp->t_inpcb->inp_vflag & INP_IPV6) {
 1294: 		MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->in6p_faddr,
 1295: 			  sizeof(struct in6_addr));
 1296: 		MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->in6p_laddr,
 1297: 			  sizeof(struct in6_addr));
 1298: 	} else
 1299: #endif
 1300: 	{
 1301: 		MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->inp_faddr,
 1302: 			  sizeof(struct in_addr));
 1303: 		MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->inp_laddr,
 1304: 			  sizeof(struct in_addr));
 1305: 	}
 1306: 	MD5Update(&isn_ctx, (u_char *) &isn_secret, sizeof(isn_secret));
 1307: 	MD5Final((u_char *) &md5_buffer, &isn_ctx);
 1308: 	new_isn = (tcp_seq) md5_buffer[0];
 1309: 	new_isn += ticks * (ISN_BYTES_PER_SECOND / hz);
 1310: 	return (new_isn);
 1311: }
 1312: 
 1313: /*
 1314:  * When a source quench is received, close congestion window
 1315:  * to one segment.  We will gradually open it again as we proceed.
 1316:  */
 1317: void
 1318: tcp_quench(struct inpcb *inp, int errno)
 1319: {
 1320: 	struct tcpcb *tp = intotcpcb(inp);
 1321: 
 1322: 	if (tp != NULL)
 1323: 		tp->snd_cwnd = tp->t_maxseg;
 1324: }
 1325: 
 1326: /*
 1327:  * When a specific ICMP unreachable message is received and the
 1328:  * connection state is SYN-SENT, drop the connection.  This behavior
 1329:  * is controlled by the icmp_may_rst sysctl.
 1330:  */
 1331: void
 1332: tcp_drop_syn_sent(struct inpcb *inp, int errno)
 1333: {
 1334: 	struct tcpcb *tp = intotcpcb(inp);
 1335: 
 1336: 	if ((tp != NULL) && (tp->t_state == TCPS_SYN_SENT))
 1337: 		tcp_drop(tp, errno);
 1338: }
 1339: 
 1340: /*
 1341:  * When `need fragmentation' ICMP is received, update our idea of the MSS
 1342:  * based on the new value in the route.  Also nudge TCP to send something,
 1343:  * since we know the packet we just sent was dropped.
 1344:  * This duplicates some code in the tcp_mss() function in tcp_input.c.
 1345:  */
 1346: void
 1347: tcp_mtudisc(struct inpcb *inp, int errno)
 1348: {
 1349: 	struct tcpcb *tp = intotcpcb(inp);
 1350: 	struct rtentry *rt;
 1351: 	struct rmxp_tao *taop;
 1352: 	struct socket *so = inp->inp_socket;
 1353: 	int offered;
 1354: 	int mss;
 1355: #ifdef INET6
 1356: 	boolean_t isipv6 = ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0);
 1357: #else
 1358: 	const boolean_t isipv6 = FALSE;
 1359: #endif
 1360: 
 1361: 	if (tp != NULL) {
 1362: 		if (isipv6)
 1363: 			rt = tcp_rtlookup6(&inp->inp_inc);
 1364: 		else
 1365: 			rt = tcp_rtlookup(&inp->inp_inc);
 1366: 		if (rt == NULL || rt->rt_rmx.rmx_mtu == 0) {
 1367: 			tp->t_maxopd = tp->t_maxseg =
 1368: 			    isipv6 ? tcp_v6mssdflt : tcp_mssdflt;
 1369: 			return;
 1370: 		}
 1371: 		taop = rmx_taop(rt->rt_rmx);
 1372: 		offered = taop->tao_mssopt;
 1373: 		mss = rt->rt_rmx.rmx_mtu -
 1374: 			(isipv6 ?
 1375: 			 sizeof(struct ip6_hdr) + sizeof(struct tcphdr) :
 1376: 			 sizeof(struct tcpiphdr));
 1377: 
 1378: 		if (offered != 0)
 1379: 			mss = min(mss, offered);
 1380: 		/*
 1381: 		 * XXX - The above conditional probably violates the TCP
 1382: 		 * spec.  The problem is that, since we don't know the
 1383: 		 * other end's MSS, we are supposed to use a conservative
 1384: 		 * default.  But, if we do that, then MTU discovery will
 1385: 		 * never actually take place, because the conservative
 1386: 		 * default is much less than the MTUs typically seen
 1387: 		 * on the Internet today.  For the moment, we'll sweep
 1388: 		 * this under the carpet.
 1389: 		 *
 1390: 		 * The conservative default might not actually be a problem
 1391: 		 * if the only case this occurs is when sending an initial
 1392: 		 * SYN with options and data to a host we've never talked
 1393: 		 * to before.  Then, they will reply with an MSS value which
 1394: 		 * will get recorded and the new parameters should get
 1395: 		 * recomputed.  For Further Study.
 1396: 		 */
 1397: 		if (tp->t_maxopd <= mss)
 1398: 			return;
 1399: 		tp->t_maxopd = mss;
 1400: 
 1401: 		if ((tp->t_flags & (TF_REQ_TSTMP | TF_NOOPT)) == TF_REQ_TSTMP &&
 1402: 		    (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)
 1403: 			mss -= TCPOLEN_TSTAMP_APPA;
 1404: 		if ((tp->t_flags & (TF_REQ_CC | TF_NOOPT)) == TF_REQ_CC &&
 1405: 		    (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC)
 1406: 			mss -= TCPOLEN_CC_APPA;
 1407: #if	(MCLBYTES & (MCLBYTES - 1)) == 0
 1408: 		if (mss > MCLBYTES)
 1409: 			mss &= ~(MCLBYTES - 1);
 1410: #else
 1411: 		if (mss > MCLBYTES)
 1412: 			mss = mss / MCLBYTES * MCLBYTES;
 1413: #endif
 1414: 		if (so->so_snd.sb_hiwat < mss)
 1415: 			mss = so->so_snd.sb_hiwat;
 1416: 
 1417: 		tp->t_maxseg = mss;
 1418: 
 1419: 		tcpstat.tcps_mturesent++;
 1420: 		tp->t_rtttime = 0;
 1421: 		tp->snd_nxt = tp->snd_una;
 1422: 		tcp_output(tp);
 1423: 	}
 1424: }
 1425: 
 1426: /*
 1427:  * Look-up the routing entry to the peer of this inpcb.  If no route
 1428:  * is found and it cannot be allocated the return NULL.  This routine
 1429:  * is called by TCP routines that access the rmx structure and by tcp_mss
 1430:  * to get the interface MTU.
 1431:  */
 1432: struct rtentry *
 1433: tcp_rtlookup(struct in_conninfo *inc)
 1434: {
 1435: 	struct route *ro;
 1436: 	struct rtentry *rt;
 1437: 
 1438: 	ro = &inc->inc_route;
 1439: 	rt = ro->ro_rt;
 1440: 	if (rt == NULL || !(rt->rt_flags & RTF_UP)) {
 1441: 		/* No route yet, so try to acquire one */
 1442: 		if (inc->inc_faddr.s_addr != INADDR_ANY) {
 1443: 			ro->ro_dst.sa_family = AF_INET;
 1444: 			ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
 1445: 			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
 1446: 			    inc->inc_faddr;
 1447: 			rtalloc(ro);
 1448: 			rt = ro->ro_rt;
 1449: 		}
 1450: 	}
 1451: 	return (rt);
 1452: }
 1453: 
 1454: #ifdef INET6
 1455: struct rtentry *
 1456: tcp_rtlookup6(struct in_conninfo *inc)
 1457: {
 1458: 	struct route_in6 *ro6;
 1459: 	struct rtentry *rt;
 1460: 
 1461: 	ro6 = &inc->inc6_route;
 1462: 	rt = ro6->ro_rt;
 1463: 	if (rt == NULL || !(rt->rt_flags & RTF_UP)) {
 1464: 		/* No route yet, so try to acquire one */
 1465: 		if (!IN6_IS_ADDR_UNSPECIFIED(&inc->inc6_faddr)) {
 1466: 			ro6->ro_dst.sin6_family = AF_INET6;
 1467: 			ro6->ro_dst.sin6_len = sizeof(struct sockaddr_in6);
 1468: 			ro6->ro_dst.sin6_addr = inc->inc6_faddr;
 1469: 			rtalloc((struct route *)ro6);
 1470: 			rt = ro6->ro_rt;
 1471: 		}
 1472: 	}
 1473: 	return (rt);
 1474: }
 1475: #endif
 1476: 
 1477: #ifdef IPSEC
 1478: /* compute ESP/AH header size for TCP, including outer IP header. */
 1479: size_t
 1480: ipsec_hdrsiz_tcp(struct tcpcb *tp)
 1481: {
 1482: 	struct inpcb *inp;
 1483: 	struct mbuf *m;
 1484: 	size_t hdrsiz;
 1485: 	struct ip *ip;
 1486: 	struct tcphdr *th;
 1487: 
 1488: 	if ((tp == NULL) || ((inp = tp->t_inpcb) == NULL))
 1489: 		return (0);
 1490: 	MGETHDR(m, M_DONTWAIT, MT_DATA);
 1491: 	if (!m)
 1492: 		return (0);
 1493: 
 1494: #ifdef INET6
 1495: 	if (inp->inp_vflag & INP_IPV6) {
 1496: 		struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
 1497: 
 1498: 		th = (struct tcphdr *)(ip6 + 1);
 1499: 		m->m_pkthdr.len = m->m_len =
 1500: 		    sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
 1501: 		tcp_fillheaders(tp, ip6, th);
 1502: 		hdrsiz = ipsec6_hdrsiz(m, IPSEC_DIR_OUTBOUND, inp);
 1503: 	} else
 1504: #endif
 1505: 	{
 1506: 		ip = mtod(m, struct ip *);
 1507: 		th = (struct tcphdr *)(ip + 1);
 1508: 		m->m_pkthdr.len = m->m_len = sizeof(struct tcpiphdr);
 1509: 		tcp_fillheaders(tp, ip, th);
 1510: 		hdrsiz = ipsec4_hdrsiz(m, IPSEC_DIR_OUTBOUND, inp);
 1511: 	}
 1512: 
 1513: 	m_free(m);
 1514: 	return (hdrsiz);
 1515: }
 1516: #endif
 1517: 
 1518: /*
 1519:  * Return a pointer to the cached information about the remote host.
 1520:  * The cached information is stored in the protocol specific part of
 1521:  * the route metrics.
 1522:  */
 1523: struct rmxp_tao *
 1524: tcp_gettaocache(struct in_conninfo *inc)
 1525: {
 1526: 	struct rtentry *rt;
 1527: 
 1528: #ifdef INET6
 1529: 	if (inc->inc_isipv6)
 1530: 		rt = tcp_rtlookup6(inc);
 1531: 	else
 1532: #endif
 1533: 		rt = tcp_rtlookup(inc);
 1534: 
 1535: 	/* Make sure this is a host route and is up. */
 1536: 	if (rt == NULL ||
 1537: 	    (rt->rt_flags & (RTF_UP | RTF_HOST)) != (RTF_UP | RTF_HOST))
 1538: 		return (NULL);
 1539: 
 1540: 	return (rmx_taop(rt->rt_rmx));
 1541: }
 1542: 
 1543: /*
 1544:  * Clear all the TAO cache entries, called from tcp_init.
 1545:  *
 1546:  * XXX
 1547:  * This routine is just an empty one, because we assume that the routing
 1548:  * routing tables are initialized at the same time when TCP, so there is
 1549:  * nothing in the cache left over.
 1550:  */
 1551: static void
 1552: tcp_cleartaocache()
 1553: {
 1554: }
 1555: 
 1556: /*
 1557:  * TCP BANDWIDTH DELAY PRODUCT WINDOW LIMITING
 1558:  *
 1559:  * This code attempts to calculate the bandwidth-delay product as a
 1560:  * means of determining the optimal window size to maximize bandwidth,
 1561:  * minimize RTT, and avoid the over-allocation of buffers on interfaces and
 1562:  * routers.  This code also does a fairly good job keeping RTTs in check
 1563:  * across slow links like modems.  We implement an algorithm which is very
 1564:  * similar (but not meant to be) TCP/Vegas.  The code operates on the
 1565:  * transmitter side of a TCP connection and so only effects the transmit
 1566:  * side of the connection.
 1567:  *
 1568:  * BACKGROUND:  TCP makes no provision for the management of buffer space
 1569:  * at the end points or at the intermediate routers and switches.  A TCP 
 1570:  * stream, whether using NewReno or not, will eventually buffer as
 1571:  * many packets as it is able and the only reason this typically works is
 1572:  * due to the fairly small default buffers made available for a connection
 1573:  * (typicaly 16K or 32K).  As machines use larger windows and/or window
 1574:  * scaling it is now fairly easy for even a single TCP connection to blow-out
 1575:  * all available buffer space not only on the local interface, but on 
 1576:  * intermediate routers and switches as well.  NewReno makes a misguided
 1577:  * attempt to 'solve' this problem by waiting for an actual failure to occur,
 1578:  * then backing off, then steadily increasing the window again until another
 1579:  * failure occurs, ad-infinitum.  This results in terrible oscillation that
 1580:  * is only made worse as network loads increase and the idea of intentionally
 1581:  * blowing out network buffers is, frankly, a terrible way to manage network
 1582:  * resources.
 1583:  *
 1584:  * It is far better to limit the transmit window prior to the failure
 1585:  * condition being achieved.  There are two general ways to do this:  First
 1586:  * you can 'scan' through different transmit window sizes and locate the
 1587:  * point where the RTT stops increasing, indicating that you have filled the
 1588:  * pipe, then scan backwards until you note that RTT stops decreasing, then
 1589:  * repeat ad-infinitum.  This method works in principle but has severe
 1590:  * implementation issues due to RTT variances, timer granularity, and
 1591:  * instability in the algorithm which can lead to many false positives and
 1592:  * create oscillations as well as interact badly with other TCP streams
 1593:  * implementing the same algorithm.
 1594:  *
 1595:  * The second method is to limit the window to the bandwidth delay product
 1596:  * of the link.  This is the method we implement.  RTT variances and our
 1597:  * own manipulation of the congestion window, bwnd, can potentially 
 1598:  * destabilize the algorithm.  For this reason we have to stabilize the
 1599:  * elements used to calculate the window.  We do this by using the minimum
 1600:  * observed RTT, the long term average of the observed bandwidth, and
 1601:  * by adding two segments worth of slop.  It isn't perfect but it is able
 1602:  * to react to changing conditions and gives us a very stable basis on
 1603:  * which to extend the algorithm.
 1604:  */
 1605: void
 1606: tcp_xmit_bandwidth_limit(struct tcpcb *tp, tcp_seq ack_seq)
 1607: {
 1608: 	u_long bw;
 1609: 	u_long bwnd;
 1610: 	int save_ticks;
 1611: 
 1612: 	/*
 1613: 	 * If inflight_enable is disabled in the middle of a tcp connection,
 1614: 	 * make sure snd_bwnd is effectively disabled.
 1615: 	 */
 1616: 	if (!tcp_inflight_enable) {
 1617: 		tp->snd_bwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
 1618: 		tp->snd_bandwidth = 0;
 1619: 		return;
 1620: 	}
 1621: 
 1622: 	/*
 1623: 	 * Figure out the bandwidth.  Due to the tick granularity this
 1624: 	 * is a very rough number and it MUST be averaged over a fairly
 1625: 	 * long period of time.  XXX we need to take into account a link
 1626: 	 * that is not using all available bandwidth, but for now our
 1627: 	 * slop will ramp us up if this case occurs and the bandwidth later
 1628: 	 * increases.
 1629: 	 *
 1630: 	 * Note: if ticks rollover 'bw' may wind up negative.  We must
 1631: 	 * effectively reset t_bw_rtttime for this case.
 1632: 	 */
 1633: 	save_ticks = ticks;
 1634: 	if ((u_int)(save_ticks - tp->t_bw_rtttime) < 1)
 1635: 		return;
 1636: 
 1637: 	bw = (int64_t)(ack_seq - tp->t_bw_rtseq) * hz / 
 1638: 	    (save_ticks - tp->t_bw_rtttime);
 1639: 	tp->t_bw_rtttime = save_ticks;
 1640: 	tp->t_bw_rtseq = ack_seq;
 1641: 	if (tp->t_bw_rtttime == 0 || (int)bw < 0)
 1642: 		return;
 1643: 	bw = ((int64_t)tp->snd_bandwidth * 15 + bw) >> 4;
 1644: 
 1645: 	tp->snd_bandwidth = bw;
 1646: 
 1647: 	/*
 1648: 	 * Calculate the semi-static bandwidth delay product, plus two maximal
 1649: 	 * segments.  The additional slop puts us squarely in the sweet
 1650: 	 * spot and also handles the bandwidth run-up case.  Without the
 1651: 	 * slop we could be locking ourselves into a lower bandwidth.
 1652: 	 *
 1653: 	 * Situations Handled:
 1654: 	 *	(1) Prevents over-queueing of packets on LANs, especially on
 1655: 	 *	    high speed LANs, allowing larger TCP buffers to be
 1656: 	 *	    specified, and also does a good job preventing 
 1657: 	 *	    over-queueing of packets over choke points like modems
 1658: 	 *	    (at least for the transmit side).
 1659: 	 *
 1660: 	 *	(2) Is able to handle changing network loads (bandwidth
 1661: 	 *	    drops so bwnd drops, bandwidth increases so bwnd
 1662: 	 *	    increases).
 1663: 	 *
 1664: 	 *	(3) Theoretically should stabilize in the face of multiple
 1665: 	 *	    connections implementing the same algorithm (this may need
 1666: 	 *	    a little work).
 1667: 	 *
 1668: 	 *	(4) Stability value (defaults to 20 = 2 maximal packets) can 
 1669: 	 *	    be adjusted with a sysctl but typically only needs to be on
 1670: 	 *	    very slow connections.  A value no smaller then 5 should
 1671: 	 *	    be used, but only reduce this default if you have no other
 1672: 	 *	    choice.
 1673: 	 */
 1674: 
 1675: #define	USERTT	((tp->t_srtt + tp->t_rttbest) / 2)
 1676: 	bwnd = (int64_t)bw * USERTT / (hz << TCP_RTT_SHIFT) +
 1677: 	       tcp_inflight_stab * (int)tp->t_maxseg / 10;
 1678: #undef USERTT
 1679: 
 1680: 	if (tcp_inflight_debug > 0) {
 1681: 		static int ltime;
 1682: 		if ((u_int)(ticks - ltime) >= hz / tcp_inflight_debug) {
 1683: 			ltime = ticks;
 1684: 			printf("%p bw %ld rttbest %d srtt %d bwnd %ld\n",
 1685: 				tp, bw, tp->t_rttbest, tp->t_srtt, bwnd);
 1686: 		}
 1687: 	}
 1688: 	if ((long)bwnd < tcp_inflight_min)
 1689: 		bwnd = tcp_inflight_min;
 1690: 	if (bwnd > tcp_inflight_max)
 1691: 		bwnd = tcp_inflight_max;
 1692: 	if ((long)bwnd < tp->t_maxseg * 2)
 1693: 		bwnd = tp->t_maxseg * 2;
 1694: 	tp->snd_bwnd = bwnd;
 1695: }