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