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