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