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