tcp_timewait.c revision 70103
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 70103 2000-12-16 19:42:06Z phk $
35 */
36
37#include "opt_compat.h"
38#include "opt_inet6.h"
39#include "opt_ipsec.h"
40#include "opt_tcpdebug.h"
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/callout.h>
45#include <sys/kernel.h>
46#include <sys/sysctl.h>
47#include <sys/malloc.h>
48#include <sys/mbuf.h>
49#ifdef INET6
50#include <sys/domain.h>
51#endif
52#include <sys/proc.h>
53#include <sys/socket.h>
54#include <sys/socketvar.h>
55#include <sys/protosw.h>
56
57#include <vm/vm_zone.h>
58
59#include <net/route.h>
60#include <net/if.h>
61
62#define _IP_VHL
63#include <netinet/in.h>
64#include <netinet/in_systm.h>
65#include <netinet/ip.h>
66#ifdef INET6
67#include <netinet/ip6.h>
68#endif
69#include <netinet/in_pcb.h>
70#ifdef INET6
71#include <netinet6/in6_pcb.h>
72#endif
73#include <netinet/in_var.h>
74#include <netinet/ip_var.h>
75#ifdef INET6
76#include <netinet6/ip6_var.h>
77#endif
78#include <netinet/tcp.h>
79#include <netinet/tcp_fsm.h>
80#include <netinet/tcp_seq.h>
81#include <netinet/tcp_timer.h>
82#include <netinet/tcp_var.h>
83#ifdef INET6
84#include <netinet6/tcp6_var.h>
85#endif
86#include <netinet/tcpip.h>
87#ifdef TCPDEBUG
88#include <netinet/tcp_debug.h>
89#endif
90#include <netinet6/ip6protosw.h>
91
92#ifdef IPSEC
93#include <netinet6/ipsec.h>
94#ifdef INET6
95#include <netinet6/ipsec6.h>
96#endif
97#endif /*IPSEC*/
98
99#include <machine/in_cksum.h>
100
101int 	tcp_mssdflt = TCP_MSS;
102SYSCTL_INT(_net_inet_tcp, TCPCTL_MSSDFLT, mssdflt, CTLFLAG_RW,
103    &tcp_mssdflt , 0, "Default TCP Maximum Segment Size");
104
105#ifdef INET6
106int	tcp_v6mssdflt = TCP6_MSS;
107SYSCTL_INT(_net_inet_tcp, TCPCTL_V6MSSDFLT, v6mssdflt,
108	CTLFLAG_RW, &tcp_v6mssdflt , 0,
109	"Default TCP Maximum Segment Size for IPv6");
110#endif
111
112#if 0
113static int 	tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ;
114SYSCTL_INT(_net_inet_tcp, TCPCTL_RTTDFLT, rttdflt, CTLFLAG_RW,
115    &tcp_rttdflt , 0, "Default maximum TCP Round Trip Time");
116#endif
117
118static int	tcp_do_rfc1323 = 1;
119SYSCTL_INT(_net_inet_tcp, TCPCTL_DO_RFC1323, rfc1323, CTLFLAG_RW,
120    &tcp_do_rfc1323 , 0, "Enable rfc1323 (high performance TCP) extensions");
121
122static int	tcp_do_rfc1644 = 0;
123SYSCTL_INT(_net_inet_tcp, TCPCTL_DO_RFC1644, rfc1644, CTLFLAG_RW,
124    &tcp_do_rfc1644 , 0, "Enable rfc1644 (TTCP) extensions");
125
126static int	tcp_tcbhashsize = 0;
127SYSCTL_INT(_net_inet_tcp, OID_AUTO, tcbhashsize, CTLFLAG_RD,
128     &tcp_tcbhashsize, 0, "Size of TCP control-block hashtable");
129
130static int	do_tcpdrain = 1;
131SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_tcpdrain, CTLFLAG_RW, &do_tcpdrain, 0,
132     "Enable tcp_drain routine for extra help when low on mbufs");
133
134SYSCTL_INT(_net_inet_tcp, OID_AUTO, pcbcount, CTLFLAG_RD,
135    &tcbinfo.ipi_count, 0, "Number of active PCBs");
136
137/*
138 * Treat ICMP administratively prohibited like a TCP RST
139 * as required by rfc1122 section 3.2.2.1
140 */
141
142static int	icmp_admin_prohib_like_rst = 0;
143SYSCTL_INT(_net_inet_tcp, OID_AUTO, icmp_admin_prohib_like_rst, CTLFLAG_RW,
144	&icmp_admin_prohib_like_rst, 0, "Treat ICMP administratively prohibited messages like TCP RST, rfc1122 section 3.2.2.1");
145
146static void	tcp_cleartaocache __P((void));
147static void	tcp_notify __P((struct inpcb *, int));
148
149/*
150 * Target size of TCP PCB hash tables. Must be a power of two.
151 *
152 * Note that this can be overridden by the kernel environment
153 * variable net.inet.tcp.tcbhashsize
154 */
155#ifndef TCBHASHSIZE
156#define TCBHASHSIZE	512
157#endif
158
159/*
160 * This is the actual shape of what we allocate using the zone
161 * allocator.  Doing it this way allows us to protect both structures
162 * using the same generation count, and also eliminates the overhead
163 * of allocating tcpcbs separately.  By hiding the structure here,
164 * we avoid changing most of the rest of the code (although it needs
165 * to be changed, eventually, for greater efficiency).
166 */
167#define	ALIGNMENT	32
168#define	ALIGNM1		(ALIGNMENT - 1)
169struct	inp_tp {
170	union {
171		struct	inpcb inp;
172		char	align[(sizeof(struct inpcb) + ALIGNM1) & ~ALIGNM1];
173	} inp_tp_u;
174	struct	tcpcb tcb;
175	struct	callout inp_tp_rexmt, inp_tp_persist, inp_tp_keep, inp_tp_2msl;
176	struct	callout inp_tp_delack;
177};
178#undef ALIGNMENT
179#undef ALIGNM1
180
181/*
182 * Tcp initialization
183 */
184void
185tcp_init()
186{
187	int hashsize;
188
189	tcp_iss = arc4random();	/* wrong, but better than a constant */
190	tcp_ccgen = 1;
191	tcp_cleartaocache();
192
193	tcp_delacktime = TCPTV_DELACK;
194	tcp_keepinit = TCPTV_KEEP_INIT;
195	tcp_keepidle = TCPTV_KEEP_IDLE;
196	tcp_keepintvl = TCPTV_KEEPINTVL;
197	tcp_maxpersistidle = TCPTV_KEEP_IDLE;
198	tcp_msl = TCPTV_MSL;
199
200	LIST_INIT(&tcb);
201	tcbinfo.listhead = &tcb;
202	TUNABLE_INT_FETCH("net.inet.tcp.tcbhashsize", TCBHASHSIZE, hashsize);
203	if (!powerof2(hashsize)) {
204		printf("WARNING: TCB hash size not a power of 2\n");
205		hashsize = 512; /* safe default */
206	}
207	tcp_tcbhashsize = hashsize;
208	tcbinfo.hashbase = hashinit(hashsize, M_PCB, &tcbinfo.hashmask);
209	tcbinfo.porthashbase = hashinit(hashsize, M_PCB,
210					&tcbinfo.porthashmask);
211	tcbinfo.ipi_zone = zinit("tcpcb", sizeof(struct inp_tp), maxsockets,
212				 ZONE_INTERRUPT, 0);
213#ifdef INET6
214#define TCP_MINPROTOHDR (sizeof(struct ip6_hdr) + sizeof(struct tcphdr))
215#else /* INET6 */
216#define TCP_MINPROTOHDR (sizeof(struct tcpiphdr))
217#endif /* INET6 */
218	if (max_protohdr < TCP_MINPROTOHDR)
219		max_protohdr = TCP_MINPROTOHDR;
220	if (max_linkhdr + TCP_MINPROTOHDR > MHLEN)
221		panic("tcp_init");
222#undef TCP_MINPROTOHDR
223}
224
225/*
226 * Create template to be used to send tcp packets on a connection.
227 * Call after host entry created, allocates an mbuf and fills
228 * in a skeletal tcp/ip header, minimizing the amount of work
229 * necessary when the connection is used.
230 */
231struct tcptemp *
232tcp_template(tp)
233	struct tcpcb *tp;
234{
235	register struct inpcb *inp = tp->t_inpcb;
236	register struct mbuf *m;
237	register struct tcptemp *n;
238
239	if ((n = tp->t_template) == 0) {
240		m = m_get(M_DONTWAIT, MT_HEADER);
241		if (m == NULL)
242			return (0);
243		m->m_len = sizeof (struct tcptemp);
244		n = mtod(m, struct tcptemp *);
245	}
246#ifdef INET6
247	if ((inp->inp_vflag & INP_IPV6) != 0) {
248		register struct ip6_hdr *ip6;
249
250		ip6 = (struct ip6_hdr *)n->tt_ipgen;
251		ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
252			(inp->in6p_flowinfo & IPV6_FLOWINFO_MASK);
253		ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
254			(IPV6_VERSION & IPV6_VERSION_MASK);
255		ip6->ip6_nxt = IPPROTO_TCP;
256		ip6->ip6_plen = sizeof(struct tcphdr);
257		ip6->ip6_src = inp->in6p_laddr;
258		ip6->ip6_dst = inp->in6p_faddr;
259		n->tt_t.th_sum = 0;
260	} else
261#endif
262      {
263	struct ip *ip = (struct ip *)n->tt_ipgen;
264
265	bzero(ip, sizeof(struct ip));		/* XXX overkill? */
266	ip->ip_vhl = IP_VHL_BORING;
267	ip->ip_p = IPPROTO_TCP;
268	ip->ip_src = inp->inp_laddr;
269	ip->ip_dst = inp->inp_faddr;
270	n->tt_t.th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
271	    htons(sizeof(struct tcphdr) + IPPROTO_TCP));
272      }
273	n->tt_t.th_sport = inp->inp_lport;
274	n->tt_t.th_dport = inp->inp_fport;
275	n->tt_t.th_seq = 0;
276	n->tt_t.th_ack = 0;
277	n->tt_t.th_x2 = 0;
278	n->tt_t.th_off = 5;
279	n->tt_t.th_flags = 0;
280	n->tt_t.th_win = 0;
281	n->tt_t.th_urp = 0;
282	return (n);
283}
284
285/*
286 * Send a single message to the TCP at address specified by
287 * the given TCP/IP header.  If m == 0, then we make a copy
288 * of the tcpiphdr at ti and send directly to the addressed host.
289 * This is used to force keep alive messages out using the TCP
290 * template for a connection tp->t_template.  If flags are given
291 * then we send a message back to the TCP which originated the
292 * segment ti, and discard the mbuf containing it and any other
293 * attached mbufs.
294 *
295 * In any case the ack and sequence number of the transmitted
296 * segment are as specified by the parameters.
297 *
298 * NOTE: If m != NULL, then ti must point to *inside* the mbuf.
299 */
300void
301tcp_respond(tp, ipgen, th, m, ack, seq, flags)
302	struct tcpcb *tp;
303	void *ipgen;
304	register struct tcphdr *th;
305	register struct mbuf *m;
306	tcp_seq ack, seq;
307	int flags;
308{
309	register int tlen;
310	int win = 0;
311	struct route *ro = 0;
312	struct route sro;
313	struct ip *ip;
314	struct tcphdr *nth;
315#ifdef INET6
316	struct route_in6 *ro6 = 0;
317	struct route_in6 sro6;
318	struct ip6_hdr *ip6;
319	int isipv6;
320#endif /* INET6 */
321	int ipflags = 0;
322
323#ifdef INET6
324	isipv6 = IP_VHL_V(((struct ip *)ipgen)->ip_vhl) == 6;
325	ip6 = ipgen;
326#endif /* INET6 */
327	ip = ipgen;
328
329	if (tp) {
330		if (!(flags & TH_RST)) {
331			win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
332			if (win > (long)TCP_MAXWIN << tp->rcv_scale)
333				win = (long)TCP_MAXWIN << tp->rcv_scale;
334		}
335#ifdef INET6
336		if (isipv6)
337			ro6 = &tp->t_inpcb->in6p_route;
338		else
339#endif /* INET6 */
340		ro = &tp->t_inpcb->inp_route;
341	} else {
342#ifdef INET6
343		if (isipv6) {
344			ro6 = &sro6;
345			bzero(ro6, sizeof *ro6);
346		} else
347#endif /* INET6 */
348	      {
349		ro = &sro;
350		bzero(ro, sizeof *ro);
351	      }
352	}
353	if (m == 0) {
354		m = m_gethdr(M_DONTWAIT, MT_HEADER);
355		if (m == NULL)
356			return;
357#ifdef TCP_COMPAT_42
358		tlen = 1;
359#else
360		tlen = 0;
361#endif
362		m->m_data += max_linkhdr;
363#ifdef INET6
364		if (isipv6) {
365			bcopy((caddr_t)ip6, mtod(m, caddr_t),
366			      sizeof(struct ip6_hdr));
367			ip6 = mtod(m, struct ip6_hdr *);
368			nth = (struct tcphdr *)(ip6 + 1);
369		} else
370#endif /* INET6 */
371	      {
372		bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
373		ip = mtod(m, struct ip *);
374		nth = (struct tcphdr *)(ip + 1);
375	      }
376		bcopy((caddr_t)th, (caddr_t)nth, sizeof(struct tcphdr));
377		flags = TH_ACK;
378	} else {
379		m_freem(m->m_next);
380		m->m_next = 0;
381		m->m_data = (caddr_t)ipgen;
382		/* m_len is set later */
383		tlen = 0;
384#define xchg(a,b,type) { type t; t=a; a=b; b=t; }
385#ifdef INET6
386		if (isipv6) {
387			xchg(ip6->ip6_dst, ip6->ip6_src, struct in6_addr);
388			nth = (struct tcphdr *)(ip6 + 1);
389		} else
390#endif /* INET6 */
391	      {
392		xchg(ip->ip_dst.s_addr, ip->ip_src.s_addr, n_long);
393		nth = (struct tcphdr *)(ip + 1);
394	      }
395		if (th != nth) {
396			/*
397			 * this is usually a case when an extension header
398			 * exists between the IPv6 header and the
399			 * TCP header.
400			 */
401			nth->th_sport = th->th_sport;
402			nth->th_dport = th->th_dport;
403		}
404		xchg(nth->th_dport, nth->th_sport, n_short);
405#undef xchg
406	}
407#ifdef INET6
408	if (isipv6) {
409		ip6->ip6_plen = htons((u_short)(sizeof (struct tcphdr) +
410						tlen));
411		tlen += sizeof (struct ip6_hdr) + sizeof (struct tcphdr);
412	} else
413#endif
414      {
415	tlen += sizeof (struct tcpiphdr);
416	ip->ip_len = tlen;
417	ip->ip_ttl = ip_defttl;
418      }
419	m->m_len = tlen;
420	m->m_pkthdr.len = tlen;
421	m->m_pkthdr.rcvif = (struct ifnet *) 0;
422	nth->th_seq = htonl(seq);
423	nth->th_ack = htonl(ack);
424	nth->th_x2 = 0;
425	nth->th_off = sizeof (struct tcphdr) >> 2;
426	nth->th_flags = flags;
427	if (tp)
428		nth->th_win = htons((u_short) (win >> tp->rcv_scale));
429	else
430		nth->th_win = htons((u_short)win);
431	nth->th_urp = 0;
432#ifdef INET6
433	if (isipv6) {
434		nth->th_sum = 0;
435		nth->th_sum = in6_cksum(m, IPPROTO_TCP,
436					sizeof(struct ip6_hdr),
437					tlen - sizeof(struct ip6_hdr));
438		ip6->ip6_hlim = in6_selecthlim(tp ? tp->t_inpcb : NULL,
439					       ro6 && ro6->ro_rt ?
440					       ro6->ro_rt->rt_ifp :
441					       NULL);
442	} else
443#endif /* INET6 */
444      {
445        nth->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
446	    htons((u_short)(tlen - sizeof(struct ip) + ip->ip_p)));
447        m->m_pkthdr.csum_flags = CSUM_TCP;
448        m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
449      }
450#ifdef TCPDEBUG
451	if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
452		tcp_trace(TA_OUTPUT, 0, tp, mtod(m, void *), th, 0);
453#endif
454#ifdef IPSEC
455	ipsec_setsocket(m, tp ? tp->t_inpcb->inp_socket : NULL);
456#endif
457#ifdef INET6
458	if (isipv6) {
459		(void)ip6_output(m, NULL, ro6, ipflags, NULL, NULL);
460		if (ro6 == &sro6 && ro6->ro_rt) {
461			RTFREE(ro6->ro_rt);
462			ro6->ro_rt = NULL;
463		}
464	} else
465#endif /* INET6 */
466      {
467	(void) ip_output(m, NULL, ro, ipflags, NULL);
468	if (ro == &sro && ro->ro_rt) {
469		RTFREE(ro->ro_rt);
470		ro->ro_rt = NULL;
471	}
472      }
473}
474
475/*
476 * Create a new TCP control block, making an
477 * empty reassembly queue and hooking it to the argument
478 * protocol control block.  The `inp' parameter must have
479 * come from the zone allocator set up in tcp_init().
480 */
481struct tcpcb *
482tcp_newtcpcb(inp)
483	struct inpcb *inp;
484{
485	struct inp_tp *it;
486	register struct tcpcb *tp;
487#ifdef INET6
488	int isipv6 = (inp->inp_vflag & INP_IPV6) != 0;
489#endif /* INET6 */
490
491	it = (struct inp_tp *)inp;
492	tp = &it->tcb;
493	bzero((char *) tp, sizeof(struct tcpcb));
494	LIST_INIT(&tp->t_segq);
495	tp->t_maxseg = tp->t_maxopd =
496#ifdef INET6
497		isipv6 ? tcp_v6mssdflt :
498#endif /* INET6 */
499		tcp_mssdflt;
500
501	/* Set up our timeouts. */
502	callout_init(tp->tt_rexmt = &it->inp_tp_rexmt, 0);
503	callout_init(tp->tt_persist = &it->inp_tp_persist, 0);
504	callout_init(tp->tt_keep = &it->inp_tp_keep, 0);
505	callout_init(tp->tt_2msl = &it->inp_tp_2msl, 0);
506	callout_init(tp->tt_delack = &it->inp_tp_delack, 0);
507
508	if (tcp_do_rfc1323)
509		tp->t_flags = (TF_REQ_SCALE|TF_REQ_TSTMP);
510	if (tcp_do_rfc1644)
511		tp->t_flags |= TF_REQ_CC;
512	tp->t_inpcb = inp;	/* XXX */
513	/*
514	 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
515	 * rtt estimate.  Set rttvar so that srtt + 4 * rttvar gives
516	 * reasonable initial retransmit time.
517	 */
518	tp->t_srtt = TCPTV_SRTTBASE;
519	tp->t_rttvar = ((TCPTV_RTOBASE - TCPTV_SRTTBASE) << TCP_RTTVAR_SHIFT) / 4;
520	tp->t_rttmin = TCPTV_MIN;
521	tp->t_rxtcur = TCPTV_RTOBASE;
522	tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
523	tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
524	tp->t_rcvtime = ticks;
525        /*
526	 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
527	 * because the socket may be bound to an IPv6 wildcard address,
528	 * which may match an IPv4-mapped IPv6 address.
529	 */
530	inp->inp_ip_ttl = ip_defttl;
531	inp->inp_ppcb = (caddr_t)tp;
532	return (tp);		/* XXX */
533}
534
535/*
536 * Drop a TCP connection, reporting
537 * the specified error.  If connection is synchronized,
538 * then send a RST to peer.
539 */
540struct tcpcb *
541tcp_drop(tp, errno)
542	register struct tcpcb *tp;
543	int errno;
544{
545	struct socket *so = tp->t_inpcb->inp_socket;
546
547	if (TCPS_HAVERCVDSYN(tp->t_state)) {
548		tp->t_state = TCPS_CLOSED;
549		(void) tcp_output(tp);
550		tcpstat.tcps_drops++;
551	} else
552		tcpstat.tcps_conndrops++;
553	if (errno == ETIMEDOUT && tp->t_softerror)
554		errno = tp->t_softerror;
555	so->so_error = errno;
556	return (tcp_close(tp));
557}
558
559/*
560 * Close a TCP control block:
561 *	discard all space held by the tcp
562 *	discard internet protocol block
563 *	wake up any sleepers
564 */
565struct tcpcb *
566tcp_close(tp)
567	register struct tcpcb *tp;
568{
569	register struct tseg_qent *q;
570	struct inpcb *inp = tp->t_inpcb;
571	struct socket *so = inp->inp_socket;
572#ifdef INET6
573	int isipv6 = (inp->inp_vflag & INP_IPV6) != 0;
574#endif /* INET6 */
575	register struct rtentry *rt;
576	int dosavessthresh;
577
578	/*
579	 * Make sure that all of our timers are stopped before we
580	 * delete the PCB.
581	 */
582	callout_stop(tp->tt_rexmt);
583	callout_stop(tp->tt_persist);
584	callout_stop(tp->tt_keep);
585	callout_stop(tp->tt_2msl);
586	callout_stop(tp->tt_delack);
587
588	/*
589	 * If we got enough samples through the srtt filter,
590	 * save the rtt and rttvar in the routing entry.
591	 * 'Enough' is arbitrarily defined as the 16 samples.
592	 * 16 samples is enough for the srtt filter to converge
593	 * to within 5% of the correct value; fewer samples and
594	 * we could save a very bogus rtt.
595	 *
596	 * Don't update the default route's characteristics and don't
597	 * update anything that the user "locked".
598	 */
599	if (tp->t_rttupdated >= 16) {
600		register u_long i = 0;
601#ifdef INET6
602		if (isipv6) {
603			struct sockaddr_in6 *sin6;
604
605			if ((rt = inp->in6p_route.ro_rt) == NULL)
606				goto no_valid_rt;
607			sin6 = (struct sockaddr_in6 *)rt_key(rt);
608			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
609				goto no_valid_rt;
610		}
611		else
612#endif /* INET6 */
613		if ((rt = inp->inp_route.ro_rt) == NULL ||
614		    ((struct sockaddr_in *)rt_key(rt))->sin_addr.s_addr
615		    == INADDR_ANY)
616			goto no_valid_rt;
617
618		if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) {
619			i = tp->t_srtt *
620			    (RTM_RTTUNIT / (hz * TCP_RTT_SCALE));
621			if (rt->rt_rmx.rmx_rtt && i)
622				/*
623				 * filter this update to half the old & half
624				 * the new values, converting scale.
625				 * See route.h and tcp_var.h for a
626				 * description of the scaling constants.
627				 */
628				rt->rt_rmx.rmx_rtt =
629				    (rt->rt_rmx.rmx_rtt + i) / 2;
630			else
631				rt->rt_rmx.rmx_rtt = i;
632			tcpstat.tcps_cachedrtt++;
633		}
634		if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) {
635			i = tp->t_rttvar *
636			    (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE));
637			if (rt->rt_rmx.rmx_rttvar && i)
638				rt->rt_rmx.rmx_rttvar =
639				    (rt->rt_rmx.rmx_rttvar + i) / 2;
640			else
641				rt->rt_rmx.rmx_rttvar = i;
642			tcpstat.tcps_cachedrttvar++;
643		}
644		/*
645		 * The old comment here said:
646		 * update the pipelimit (ssthresh) if it has been updated
647		 * already or if a pipesize was specified & the threshhold
648		 * got below half the pipesize.  I.e., wait for bad news
649		 * before we start updating, then update on both good
650		 * and bad news.
651		 *
652		 * But we want to save the ssthresh even if no pipesize is
653		 * specified explicitly in the route, because such
654		 * connections still have an implicit pipesize specified
655		 * by the global tcp_sendspace.  In the absence of a reliable
656		 * way to calculate the pipesize, it will have to do.
657		 */
658		i = tp->snd_ssthresh;
659		if (rt->rt_rmx.rmx_sendpipe != 0)
660			dosavessthresh = (i < rt->rt_rmx.rmx_sendpipe / 2);
661		else
662			dosavessthresh = (i < so->so_snd.sb_hiwat / 2);
663		if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 &&
664		     i != 0 && rt->rt_rmx.rmx_ssthresh != 0)
665		    || dosavessthresh) {
666			/*
667			 * convert the limit from user data bytes to
668			 * packets then to packet data bytes.
669			 */
670			i = (i + tp->t_maxseg / 2) / tp->t_maxseg;
671			if (i < 2)
672				i = 2;
673			i *= (u_long)(tp->t_maxseg +
674#ifdef INET6
675				      (isipv6 ? sizeof (struct ip6_hdr) +
676					       sizeof (struct tcphdr) :
677#endif
678				       sizeof (struct tcpiphdr)
679#ifdef INET6
680				       )
681#endif
682				      );
683			if (rt->rt_rmx.rmx_ssthresh)
684				rt->rt_rmx.rmx_ssthresh =
685				    (rt->rt_rmx.rmx_ssthresh + i) / 2;
686			else
687				rt->rt_rmx.rmx_ssthresh = i;
688			tcpstat.tcps_cachedssthresh++;
689		}
690	}
691	rt = inp->inp_route.ro_rt;
692	if (rt) {
693		/*
694		 * mark route for deletion if no information is
695		 * cached.
696		 */
697		if ((tp->t_flags & TF_LQ_OVERFLOW) &&
698		    ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0)){
699			if (rt->rt_rmx.rmx_rtt == 0)
700				rt->rt_flags |= RTF_DELCLONE;
701		}
702	}
703    no_valid_rt:
704	/* free the reassembly queue, if any */
705	while((q = LIST_FIRST(&tp->t_segq)) != NULL) {
706		LIST_REMOVE(q, tqe_q);
707		m_freem(q->tqe_m);
708		FREE(q, M_TSEGQ);
709	}
710	if (tp->t_template)
711		(void) m_free(dtom(tp->t_template));
712	inp->inp_ppcb = NULL;
713	soisdisconnected(so);
714#ifdef INET6
715	if (INP_CHECK_SOCKAF(so, AF_INET6))
716		in6_pcbdetach(inp);
717	else
718#endif /* INET6 */
719	in_pcbdetach(inp);
720	tcpstat.tcps_closed++;
721	return ((struct tcpcb *)0);
722}
723
724void
725tcp_drain()
726{
727	if (do_tcpdrain)
728	{
729		struct inpcb *inpb;
730		struct tcpcb *tcpb;
731		struct tseg_qent *te;
732
733	/*
734	 * Walk the tcpbs, if existing, and flush the reassembly queue,
735	 * if there is one...
736	 * XXX: The "Net/3" implementation doesn't imply that the TCP
737	 *      reassembly queue should be flushed, but in a situation
738	 * 	where we're really low on mbufs, this is potentially
739	 *  	usefull.
740	 */
741		for (inpb = tcbinfo.listhead->lh_first; inpb;
742	    		inpb = inpb->inp_list.le_next) {
743				if ((tcpb = intotcpcb(inpb))) {
744					while ((te = LIST_FIRST(&tcpb->t_segq))
745					       != NULL) {
746					LIST_REMOVE(te, tqe_q);
747					m_freem(te->tqe_m);
748					FREE(te, M_TSEGQ);
749				}
750			}
751		}
752
753	}
754}
755
756/*
757 * Notify a tcp user of an asynchronous error;
758 * store error as soft error, but wake up user
759 * (for now, won't do anything until can select for soft error).
760 */
761static void
762tcp_notify(inp, error)
763	struct inpcb *inp;
764	int error;
765{
766	register struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb;
767	register struct socket *so = inp->inp_socket;
768
769	/*
770	 * Ignore some errors if we are hooked up.
771	 * If connection hasn't completed, has retransmitted several times,
772	 * and receives a second error, give up now.  This is better
773	 * than waiting a long time to establish a connection that
774	 * can never complete.
775	 */
776	if (tp->t_state == TCPS_ESTABLISHED &&
777	     (error == EHOSTUNREACH || error == ENETUNREACH ||
778	      error == EHOSTDOWN)) {
779		return;
780	} else if (tp->t_state < TCPS_ESTABLISHED && tp->t_rxtshift > 3 &&
781	    tp->t_softerror)
782		so->so_error = error;
783	else
784		tp->t_softerror = error;
785	wakeup((caddr_t) &so->so_timeo);
786	sorwakeup(so);
787	sowwakeup(so);
788}
789
790static int
791tcp_pcblist(SYSCTL_HANDLER_ARGS)
792{
793	int error, i, n, s;
794	struct inpcb *inp, **inp_list;
795	inp_gen_t gencnt;
796	struct xinpgen xig;
797
798	/*
799	 * The process of preparing the TCB list is too time-consuming and
800	 * resource-intensive to repeat twice on every request.
801	 */
802	if (req->oldptr == 0) {
803		n = tcbinfo.ipi_count;
804		req->oldidx = 2 * (sizeof xig)
805			+ (n + n/8) * sizeof(struct xtcpcb);
806		return 0;
807	}
808
809	if (req->newptr != 0)
810		return EPERM;
811
812	/*
813	 * OK, now we're committed to doing something.
814	 */
815	s = splnet();
816	gencnt = tcbinfo.ipi_gencnt;
817	n = tcbinfo.ipi_count;
818	splx(s);
819
820	xig.xig_len = sizeof xig;
821	xig.xig_count = n;
822	xig.xig_gen = gencnt;
823	xig.xig_sogen = so_gencnt;
824	error = SYSCTL_OUT(req, &xig, sizeof xig);
825	if (error)
826		return error;
827
828	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
829	if (inp_list == 0)
830		return ENOMEM;
831
832	s = splnet();
833	for (inp = tcbinfo.listhead->lh_first, i = 0; inp && i < n;
834	     inp = inp->inp_list.le_next) {
835		if (inp->inp_gencnt <= gencnt && !prison_xinpcb(req->p, inp))
836			inp_list[i++] = inp;
837	}
838	splx(s);
839	n = i;
840
841	error = 0;
842	for (i = 0; i < n; i++) {
843		inp = inp_list[i];
844		if (inp->inp_gencnt <= gencnt) {
845			struct xtcpcb xt;
846			caddr_t inp_ppcb;
847			xt.xt_len = sizeof xt;
848			/* XXX should avoid extra copy */
849			bcopy(inp, &xt.xt_inp, sizeof *inp);
850			inp_ppcb = inp->inp_ppcb;
851			if (inp_ppcb != NULL)
852				bcopy(inp_ppcb, &xt.xt_tp, sizeof xt.xt_tp);
853			else
854				bzero((char *) &xt.xt_tp, sizeof xt.xt_tp);
855			if (inp->inp_socket)
856				sotoxsocket(inp->inp_socket, &xt.xt_socket);
857			error = SYSCTL_OUT(req, &xt, sizeof xt);
858		}
859	}
860	if (!error) {
861		/*
862		 * Give the user an updated idea of our state.
863		 * If the generation differs from what we told
864		 * her before, she knows that something happened
865		 * while we were processing this request, and it
866		 * might be necessary to retry.
867		 */
868		s = splnet();
869		xig.xig_gen = tcbinfo.ipi_gencnt;
870		xig.xig_sogen = so_gencnt;
871		xig.xig_count = tcbinfo.ipi_count;
872		splx(s);
873		error = SYSCTL_OUT(req, &xig, sizeof xig);
874	}
875	free(inp_list, M_TEMP);
876	return error;
877}
878
879SYSCTL_PROC(_net_inet_tcp, TCPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
880	    tcp_pcblist, "S,xtcpcb", "List of active TCP connections");
881
882static int
883tcp_getcred(SYSCTL_HANDLER_ARGS)
884{
885	struct sockaddr_in addrs[2];
886	struct inpcb *inp;
887	int error, s;
888
889	error = suser(req->p);
890	if (error)
891		return (error);
892	error = SYSCTL_IN(req, addrs, sizeof(addrs));
893	if (error)
894		return (error);
895	s = splnet();
896	inp = in_pcblookup_hash(&tcbinfo, addrs[1].sin_addr, addrs[1].sin_port,
897	    addrs[0].sin_addr, addrs[0].sin_port, 0, NULL);
898	if (inp == NULL || inp->inp_socket == NULL) {
899		error = ENOENT;
900		goto out;
901	}
902	error = SYSCTL_OUT(req, inp->inp_socket->so_cred, sizeof(struct ucred));
903out:
904	splx(s);
905	return (error);
906}
907
908SYSCTL_PROC(_net_inet_tcp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
909    0, 0, tcp_getcred, "S,ucred", "Get the ucred of a TCP connection");
910
911#ifdef INET6
912static int
913tcp6_getcred(SYSCTL_HANDLER_ARGS)
914{
915	struct sockaddr_in6 addrs[2];
916	struct inpcb *inp;
917	int error, s, mapped = 0;
918
919	error = suser(req->p);
920	if (error)
921		return (error);
922	error = SYSCTL_IN(req, addrs, sizeof(addrs));
923	if (error)
924		return (error);
925	if (IN6_IS_ADDR_V4MAPPED(&addrs[0].sin6_addr)) {
926		if (IN6_IS_ADDR_V4MAPPED(&addrs[1].sin6_addr))
927			mapped = 1;
928		else
929			return (EINVAL);
930	}
931	s = splnet();
932	if (mapped == 1)
933		inp = in_pcblookup_hash(&tcbinfo,
934			*(struct in_addr *)&addrs[1].sin6_addr.s6_addr[12],
935			addrs[1].sin6_port,
936			*(struct in_addr *)&addrs[0].sin6_addr.s6_addr[12],
937			addrs[0].sin6_port,
938			0, NULL);
939	else
940		inp = in6_pcblookup_hash(&tcbinfo, &addrs[1].sin6_addr,
941				 addrs[1].sin6_port,
942				 &addrs[0].sin6_addr, addrs[0].sin6_port,
943				 0, NULL);
944	if (inp == NULL || inp->inp_socket == NULL) {
945		error = ENOENT;
946		goto out;
947	}
948	error = SYSCTL_OUT(req, inp->inp_socket->so_cred,
949			   sizeof(struct ucred));
950out:
951	splx(s);
952	return (error);
953}
954
955SYSCTL_PROC(_net_inet6_tcp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
956	    0, 0,
957	    tcp6_getcred, "S,ucred", "Get the ucred of a TCP6 connection");
958#endif
959
960
961void
962tcp_ctlinput(cmd, sa, vip)
963	int cmd;
964	struct sockaddr *sa;
965	void *vip;
966{
967	register struct ip *ip = vip;
968	register struct tcphdr *th;
969	void (*notify) __P((struct inpcb *, int)) = tcp_notify;
970
971	if (cmd == PRC_QUENCH)
972		notify = tcp_quench;
973	else if ((icmp_admin_prohib_like_rst == 1) && (cmd == PRC_UNREACH_PORT) && (ip))
974		notify = tcp_drop_syn_sent;
975	else if (cmd == PRC_MSGSIZE)
976		notify = tcp_mtudisc;
977	else if (!PRC_IS_REDIRECT(cmd) &&
978		 ((unsigned)cmd > PRC_NCMDS || inetctlerrmap[cmd] == 0))
979		return;
980	if (ip) {
981		th = (struct tcphdr *)((caddr_t)ip
982				       + (IP_VHL_HL(ip->ip_vhl) << 2));
983		in_pcbnotify(&tcb, sa, th->th_dport, ip->ip_src, th->th_sport,
984			cmd, notify);
985	} else
986		in_pcbnotify(&tcb, sa, 0, zeroin_addr, 0, cmd, notify);
987}
988
989#ifdef INET6
990void
991tcp6_ctlinput(cmd, sa, d)
992	int cmd;
993	struct sockaddr *sa;
994	void *d;
995{
996	register struct tcphdr *thp;
997	struct tcphdr th;
998	void (*notify) __P((struct inpcb *, int)) = tcp_notify;
999	struct sockaddr_in6 sa6;
1000	struct ip6_hdr *ip6;
1001	struct mbuf *m;
1002	int off;
1003
1004	if (sa->sa_family != AF_INET6 ||
1005	    sa->sa_len != sizeof(struct sockaddr_in6))
1006		return;
1007
1008	if (cmd == PRC_QUENCH)
1009		notify = tcp_quench;
1010	else if (cmd == PRC_MSGSIZE)
1011		notify = tcp_mtudisc;
1012	else if (!PRC_IS_REDIRECT(cmd) &&
1013		 ((unsigned)cmd > PRC_NCMDS || inet6ctlerrmap[cmd] == 0))
1014		return;
1015
1016	/* if the parameter is from icmp6, decode it. */
1017	if (d != NULL) {
1018		struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d;
1019		m = ip6cp->ip6c_m;
1020		ip6 = ip6cp->ip6c_ip6;
1021		off = ip6cp->ip6c_off;
1022	} else {
1023		m = NULL;
1024		ip6 = NULL;
1025		off = 0;	/* fool gcc */
1026	}
1027
1028	/*
1029	 * Translate addresses into internal form.
1030	 * Sa check if it is AF_INET6 is done at the top of this funciton.
1031	 */
1032	sa6 = *(struct sockaddr_in6 *)sa;
1033	if (IN6_IS_ADDR_LINKLOCAL(&sa6.sin6_addr) != 0 && m != NULL &&
1034	    m->m_pkthdr.rcvif != NULL)
1035		sa6.sin6_addr.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
1036
1037	if (ip6) {
1038		/*
1039		 * XXX: We assume that when IPV6 is non NULL,
1040		 * M and OFF are valid.
1041		 */
1042		struct in6_addr s;
1043
1044		/* translate addresses into internal form */
1045		memcpy(&s, &ip6->ip6_src, sizeof(s));
1046		if (IN6_IS_ADDR_LINKLOCAL(&s) != 0 && m != NULL &&
1047		    m->m_pkthdr.rcvif != NULL)
1048			s.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
1049
1050		/* check if we can safely examine src and dst ports */
1051		if (m->m_pkthdr.len < off + sizeof(th))
1052			return;
1053
1054		if (m->m_len < off + sizeof(th)) {
1055			/*
1056			 * this should be rare case
1057			 * because now MINCLSIZE is "(MHLEN + 1)",
1058			 * so we compromise on this copy...
1059			 */
1060			m_copydata(m, off, sizeof(th), (caddr_t)&th);
1061			thp = &th;
1062		} else
1063			thp = (struct tcphdr *)(mtod(m, caddr_t) + off);
1064		in6_pcbnotify(&tcb, (struct sockaddr *)&sa6, thp->th_dport,
1065			      &s, thp->th_sport, cmd, notify);
1066	} else
1067		in6_pcbnotify(&tcb, (struct sockaddr *)&sa6, 0, &zeroin6_addr,
1068			      0, cmd, notify);
1069}
1070#endif /* INET6 */
1071
1072/*
1073 * When a source quench is received, close congestion window
1074 * to one segment.  We will gradually open it again as we proceed.
1075 */
1076void
1077tcp_quench(inp, errno)
1078	struct inpcb *inp;
1079	int errno;
1080{
1081	struct tcpcb *tp = intotcpcb(inp);
1082
1083	if (tp)
1084		tp->snd_cwnd = tp->t_maxseg;
1085}
1086
1087/*
1088 * When a ICMP unreachable is recieved, drop the
1089 * TCP connection, but only if in SYN_SENT
1090 */
1091void
1092tcp_drop_syn_sent(inp, errno)
1093	struct inpcb *inp;
1094	int errno;
1095{
1096	struct tcpcb *tp = intotcpcb(inp);
1097	if((tp) && (tp->t_state == TCPS_SYN_SENT))
1098			tcp_drop(tp, errno);
1099}
1100
1101/*
1102 * When `need fragmentation' ICMP is received, update our idea of the MSS
1103 * based on the new value in the route.  Also nudge TCP to send something,
1104 * since we know the packet we just sent was dropped.
1105 * This duplicates some code in the tcp_mss() function in tcp_input.c.
1106 */
1107void
1108tcp_mtudisc(inp, errno)
1109	struct inpcb *inp;
1110	int errno;
1111{
1112	struct tcpcb *tp = intotcpcb(inp);
1113	struct rtentry *rt;
1114	struct rmxp_tao *taop;
1115	struct socket *so = inp->inp_socket;
1116	int offered;
1117	int mss;
1118#ifdef INET6
1119	int isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
1120#endif /* INET6 */
1121
1122	if (tp) {
1123#ifdef INET6
1124		if (isipv6)
1125			rt = tcp_rtlookup6(inp);
1126		else
1127#endif /* INET6 */
1128		rt = tcp_rtlookup(inp);
1129		if (!rt || !rt->rt_rmx.rmx_mtu) {
1130			tp->t_maxopd = tp->t_maxseg =
1131#ifdef INET6
1132				isipv6 ? tcp_v6mssdflt :
1133#endif /* INET6 */
1134				tcp_mssdflt;
1135			return;
1136		}
1137		taop = rmx_taop(rt->rt_rmx);
1138		offered = taop->tao_mssopt;
1139		mss = rt->rt_rmx.rmx_mtu -
1140#ifdef INET6
1141			(isipv6 ?
1142			 sizeof(struct ip6_hdr) + sizeof(struct tcphdr) :
1143#endif /* INET6 */
1144			 sizeof(struct tcpiphdr)
1145#ifdef INET6
1146			 )
1147#endif /* INET6 */
1148			;
1149
1150		if (offered)
1151			mss = min(mss, offered);
1152		/*
1153		 * XXX - The above conditional probably violates the TCP
1154		 * spec.  The problem is that, since we don't know the
1155		 * other end's MSS, we are supposed to use a conservative
1156		 * default.  But, if we do that, then MTU discovery will
1157		 * never actually take place, because the conservative
1158		 * default is much less than the MTUs typically seen
1159		 * on the Internet today.  For the moment, we'll sweep
1160		 * this under the carpet.
1161		 *
1162		 * The conservative default might not actually be a problem
1163		 * if the only case this occurs is when sending an initial
1164		 * SYN with options and data to a host we've never talked
1165		 * to before.  Then, they will reply with an MSS value which
1166		 * will get recorded and the new parameters should get
1167		 * recomputed.  For Further Study.
1168		 */
1169		if (tp->t_maxopd <= mss)
1170			return;
1171		tp->t_maxopd = mss;
1172
1173		if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
1174		    (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)
1175			mss -= TCPOLEN_TSTAMP_APPA;
1176		if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
1177		    (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC)
1178			mss -= TCPOLEN_CC_APPA;
1179#if	(MCLBYTES & (MCLBYTES - 1)) == 0
1180		if (mss > MCLBYTES)
1181			mss &= ~(MCLBYTES-1);
1182#else
1183		if (mss > MCLBYTES)
1184			mss = mss / MCLBYTES * MCLBYTES;
1185#endif
1186		if (so->so_snd.sb_hiwat < mss)
1187			mss = so->so_snd.sb_hiwat;
1188
1189		tp->t_maxseg = mss;
1190
1191		tcpstat.tcps_mturesent++;
1192		tp->t_rtttime = 0;
1193		tp->snd_nxt = tp->snd_una;
1194		tcp_output(tp);
1195	}
1196}
1197
1198/*
1199 * Look-up the routing entry to the peer of this inpcb.  If no route
1200 * is found and it cannot be allocated the return NULL.  This routine
1201 * is called by TCP routines that access the rmx structure and by tcp_mss
1202 * to get the interface MTU.
1203 */
1204struct rtentry *
1205tcp_rtlookup(inp)
1206	struct inpcb *inp;
1207{
1208	struct route *ro;
1209	struct rtentry *rt;
1210
1211	ro = &inp->inp_route;
1212	rt = ro->ro_rt;
1213	if (rt == NULL || !(rt->rt_flags & RTF_UP)) {
1214		/* No route yet, so try to acquire one */
1215		if (inp->inp_faddr.s_addr != INADDR_ANY) {
1216			ro->ro_dst.sa_family = AF_INET;
1217			ro->ro_dst.sa_len = sizeof(ro->ro_dst);
1218			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
1219				inp->inp_faddr;
1220			rtalloc(ro);
1221			rt = ro->ro_rt;
1222		}
1223	}
1224	return rt;
1225}
1226
1227#ifdef INET6
1228struct rtentry *
1229tcp_rtlookup6(inp)
1230	struct inpcb *inp;
1231{
1232	struct route_in6 *ro6;
1233	struct rtentry *rt;
1234
1235	ro6 = &inp->in6p_route;
1236	rt = ro6->ro_rt;
1237	if (rt == NULL || !(rt->rt_flags & RTF_UP)) {
1238		/* No route yet, so try to acquire one */
1239		if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1240			ro6->ro_dst.sin6_family = AF_INET6;
1241			ro6->ro_dst.sin6_len = sizeof(ro6->ro_dst);
1242			ro6->ro_dst.sin6_addr = inp->in6p_faddr;
1243			rtalloc((struct route *)ro6);
1244			rt = ro6->ro_rt;
1245		}
1246	}
1247	return rt;
1248}
1249#endif /* INET6 */
1250
1251#ifdef IPSEC
1252/* compute ESP/AH header size for TCP, including outer IP header. */
1253size_t
1254ipsec_hdrsiz_tcp(tp)
1255	struct tcpcb *tp;
1256{
1257	struct inpcb *inp;
1258	struct mbuf *m;
1259	size_t hdrsiz;
1260	struct ip *ip;
1261#ifdef INET6
1262	struct ip6_hdr *ip6;
1263#endif /* INET6 */
1264	struct tcphdr *th;
1265
1266	if (!tp || !tp->t_template || !(inp = tp->t_inpcb))
1267		return 0;
1268	MGETHDR(m, M_DONTWAIT, MT_DATA);
1269	if (!m)
1270		return 0;
1271
1272#ifdef INET6
1273	if ((inp->inp_vflag & INP_IPV6) != 0) {
1274		ip6 = mtod(m, struct ip6_hdr *);
1275		th = (struct tcphdr *)(ip6 + 1);
1276		m->m_pkthdr.len = m->m_len =
1277			sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
1278		bcopy((caddr_t)tp->t_template->tt_ipgen, (caddr_t)ip6,
1279		      sizeof(struct ip6_hdr));
1280		bcopy((caddr_t)&tp->t_template->tt_t, (caddr_t)th,
1281		      sizeof(struct tcphdr));
1282		hdrsiz = ipsec6_hdrsiz(m, IPSEC_DIR_OUTBOUND, inp);
1283	} else
1284#endif /* INET6 */
1285      {
1286	ip = mtod(m, struct ip *);
1287	th = (struct tcphdr *)(ip + 1);
1288	m->m_pkthdr.len = m->m_len = sizeof(struct tcpiphdr);
1289	bcopy((caddr_t)tp->t_template->tt_ipgen, (caddr_t)ip,
1290	      sizeof(struct ip));
1291	bcopy((caddr_t)&tp->t_template->tt_t, (caddr_t)th,
1292	      sizeof(struct tcphdr));
1293	hdrsiz = ipsec4_hdrsiz(m, IPSEC_DIR_OUTBOUND, inp);
1294      }
1295
1296	m_free(m);
1297	return hdrsiz;
1298}
1299#endif /*IPSEC*/
1300
1301/*
1302 * Return a pointer to the cached information about the remote host.
1303 * The cached information is stored in the protocol specific part of
1304 * the route metrics.
1305 */
1306struct rmxp_tao *
1307tcp_gettaocache(inp)
1308	struct inpcb *inp;
1309{
1310	struct rtentry *rt;
1311
1312#ifdef INET6
1313	if ((inp->inp_vflag & INP_IPV6) != 0)
1314		rt = tcp_rtlookup6(inp);
1315	else
1316#endif /* INET6 */
1317	rt = tcp_rtlookup(inp);
1318
1319	/* Make sure this is a host route and is up. */
1320	if (rt == NULL ||
1321	    (rt->rt_flags & (RTF_UP|RTF_HOST)) != (RTF_UP|RTF_HOST))
1322		return NULL;
1323
1324	return rmx_taop(rt->rt_rmx);
1325}
1326
1327/*
1328 * Clear all the TAO cache entries, called from tcp_init.
1329 *
1330 * XXX
1331 * This routine is just an empty one, because we assume that the routing
1332 * routing tables are initialized at the same time when TCP, so there is
1333 * nothing in the cache left over.
1334 */
1335static void
1336tcp_cleartaocache()
1337{
1338}
1339