tcp_timewait.c revision 55679
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 55679 2000-01-09 19:17:30Z 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	}
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_plen = htons((u_short)tlen);
422		ip6->ip6_hlim = in6_selecthlim(tp ? tp->t_inpcb : NULL,
423					       ro6 && ro6->ro_rt ?
424					       ro6->ro_rt->rt_ifp :
425					       NULL);
426	} else
427#endif /* INET6 */
428      {
429	bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
430	nth->th_sum = in_cksum(m, tlen);
431#ifdef INET6
432	/* Re-initialization for later version check */
433	ip->ip_vhl = IP_MAKE_VHL(IPVERSION, 0);
434#endif /* INET6 */
435	ip->ip_len = tlen;
436	ip->ip_ttl = ip_defttl;
437      }
438#ifdef TCPDEBUG
439	if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
440		tcp_trace(TA_OUTPUT, 0, tp, mtod(m, void *), th, 0);
441#endif
442#ifdef IPSEC
443	if (tp != NULL) {
444		m->m_pkthdr.rcvif = (struct ifnet *)tp->t_inpcb->inp_socket;
445		ipflags |=
446#ifdef INET6
447			isipv6 ? IPV6_SOCKINMRCVIF :
448#endif
449			IP_SOCKINMRCVIF;
450	}
451#endif
452#ifdef INET6
453	if (isipv6) {
454		(void)ip6_output(m, NULL, ro6, ipflags, NULL, NULL);
455		if (ro6 == &sro6 && ro6->ro_rt)
456			RTFREE(ro6->ro_rt);
457	} else
458#endif /* INET6 */
459      {
460	(void) ip_output(m, NULL, ro, ipflags, NULL);
461	if (ro == &sro && ro->ro_rt) {
462		RTFREE(ro->ro_rt);
463	}
464      }
465}
466
467/*
468 * Create a new TCP control block, making an
469 * empty reassembly queue and hooking it to the argument
470 * protocol control block.  The `inp' parameter must have
471 * come from the zone allocator set up in tcp_init().
472 */
473struct tcpcb *
474tcp_newtcpcb(inp)
475	struct inpcb *inp;
476{
477	struct inp_tp *it;
478	register struct tcpcb *tp;
479#ifdef INET6
480	int isipv6 = (inp->inp_vflag & INP_IPV6) != 0;
481#endif /* INET6 */
482
483	it = (struct inp_tp *)inp;
484	tp = &it->tcb;
485	bzero((char *) tp, sizeof(struct tcpcb));
486	LIST_INIT(&tp->t_segq);
487	tp->t_maxseg = tp->t_maxopd =
488#ifdef INET6
489		isipv6 ? tcp_v6mssdflt :
490#endif /* INET6 */
491		tcp_mssdflt;
492
493	/* Set up our timeouts. */
494	callout_init(tp->tt_rexmt = &it->inp_tp_rexmt);
495	callout_init(tp->tt_persist = &it->inp_tp_persist);
496	callout_init(tp->tt_keep = &it->inp_tp_keep);
497	callout_init(tp->tt_2msl = &it->inp_tp_2msl);
498	callout_init(tp->tt_delack = &it->inp_tp_delack);
499
500	if (tcp_do_rfc1323)
501		tp->t_flags = (TF_REQ_SCALE|TF_REQ_TSTMP);
502	if (tcp_do_rfc1644)
503		tp->t_flags |= TF_REQ_CC;
504	tp->t_inpcb = inp;	/* XXX */
505	/*
506	 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
507	 * rtt estimate.  Set rttvar so that srtt + 4 * rttvar gives
508	 * reasonable initial retransmit time.
509	 */
510	tp->t_srtt = TCPTV_SRTTBASE;
511	tp->t_rttvar = ((TCPTV_RTOBASE - TCPTV_SRTTBASE) << TCP_RTTVAR_SHIFT) / 4;
512	tp->t_rttmin = TCPTV_MIN;
513	tp->t_rxtcur = TCPTV_RTOBASE;
514	tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
515	tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
516	tp->t_rcvtime = ticks;
517#ifdef INET6
518	if (isipv6 != 0)
519		inp->in6p_ip6_hlim = in6_selecthlim(inp,
520						    inp->in6p_route.ro_rt ?
521						    inp->in6p_route.ro_rt->rt_ifp :
522						    NULL);
523	else
524#endif
525	inp->inp_ip_ttl = ip_defttl;
526	inp->inp_ppcb = (caddr_t)tp;
527	return (tp);		/* XXX */
528}
529
530/*
531 * Drop a TCP connection, reporting
532 * the specified error.  If connection is synchronized,
533 * then send a RST to peer.
534 */
535struct tcpcb *
536tcp_drop(tp, errno)
537	register struct tcpcb *tp;
538	int errno;
539{
540	struct socket *so = tp->t_inpcb->inp_socket;
541
542	if (TCPS_HAVERCVDSYN(tp->t_state)) {
543		tp->t_state = TCPS_CLOSED;
544		(void) tcp_output(tp);
545		tcpstat.tcps_drops++;
546	} else
547		tcpstat.tcps_conndrops++;
548	if (errno == ETIMEDOUT && tp->t_softerror)
549		errno = tp->t_softerror;
550	so->so_error = errno;
551	return (tcp_close(tp));
552}
553
554/*
555 * Close a TCP control block:
556 *	discard all space held by the tcp
557 *	discard internet protocol block
558 *	wake up any sleepers
559 */
560struct tcpcb *
561tcp_close(tp)
562	register struct tcpcb *tp;
563{
564	register struct tseg_qent *q;
565	struct inpcb *inp = tp->t_inpcb;
566	struct socket *so = inp->inp_socket;
567#ifdef INET6
568	int isipv6 = (inp->inp_vflag & INP_IPV6) != 0;
569#endif /* INET6 */
570	register struct rtentry *rt;
571	int dosavessthresh;
572
573	/*
574	 * Make sure that all of our timers are stopped before we
575	 * delete the PCB.
576	 */
577	callout_stop(tp->tt_rexmt);
578	callout_stop(tp->tt_persist);
579	callout_stop(tp->tt_keep);
580	callout_stop(tp->tt_2msl);
581	callout_stop(tp->tt_delack);
582
583	/*
584	 * If we got enough samples through the srtt filter,
585	 * save the rtt and rttvar in the routing entry.
586	 * 'Enough' is arbitrarily defined as the 16 samples.
587	 * 16 samples is enough for the srtt filter to converge
588	 * to within 5% of the correct value; fewer samples and
589	 * we could save a very bogus rtt.
590	 *
591	 * Don't update the default route's characteristics and don't
592	 * update anything that the user "locked".
593	 */
594	if (tp->t_rttupdated >= 16) {
595		register u_long i = 0;
596#ifdef INET6
597		if (isipv6) {
598			struct sockaddr_in6 *sin6;
599
600			if ((rt = inp->in6p_route.ro_rt) == NULL)
601				goto no_valid_rt;
602			sin6 = (struct sockaddr_in6 *)rt_key(rt);
603			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
604				goto no_valid_rt;
605		}
606		else
607#endif /* INET6 */
608		if ((rt = inp->inp_route.ro_rt) == NULL ||
609		    ((struct sockaddr_in *)rt_key(rt))->sin_addr.s_addr
610		    == INADDR_ANY)
611			goto no_valid_rt;
612
613		if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) {
614			i = tp->t_srtt *
615			    (RTM_RTTUNIT / (hz * TCP_RTT_SCALE));
616			if (rt->rt_rmx.rmx_rtt && i)
617				/*
618				 * filter this update to half the old & half
619				 * the new values, converting scale.
620				 * See route.h and tcp_var.h for a
621				 * description of the scaling constants.
622				 */
623				rt->rt_rmx.rmx_rtt =
624				    (rt->rt_rmx.rmx_rtt + i) / 2;
625			else
626				rt->rt_rmx.rmx_rtt = i;
627			tcpstat.tcps_cachedrtt++;
628		}
629		if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) {
630			i = tp->t_rttvar *
631			    (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE));
632			if (rt->rt_rmx.rmx_rttvar && i)
633				rt->rt_rmx.rmx_rttvar =
634				    (rt->rt_rmx.rmx_rttvar + i) / 2;
635			else
636				rt->rt_rmx.rmx_rttvar = i;
637			tcpstat.tcps_cachedrttvar++;
638		}
639		/*
640		 * The old comment here said:
641		 * update the pipelimit (ssthresh) if it has been updated
642		 * already or if a pipesize was specified & the threshhold
643		 * got below half the pipesize.  I.e., wait for bad news
644		 * before we start updating, then update on both good
645		 * and bad news.
646		 *
647		 * But we want to save the ssthresh even if no pipesize is
648		 * specified explicitly in the route, because such
649		 * connections still have an implicit pipesize specified
650		 * by the global tcp_sendspace.  In the absence of a reliable
651		 * way to calculate the pipesize, it will have to do.
652		 */
653		i = tp->snd_ssthresh;
654		if (rt->rt_rmx.rmx_sendpipe != 0)
655			dosavessthresh = (i < rt->rt_rmx.rmx_sendpipe / 2);
656		else
657			dosavessthresh = (i < so->so_snd.sb_hiwat / 2);
658		if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 &&
659		     i != 0 && rt->rt_rmx.rmx_ssthresh != 0)
660		    || dosavessthresh) {
661			/*
662			 * convert the limit from user data bytes to
663			 * packets then to packet data bytes.
664			 */
665			i = (i + tp->t_maxseg / 2) / tp->t_maxseg;
666			if (i < 2)
667				i = 2;
668			i *= (u_long)(tp->t_maxseg +
669#ifdef INET6
670				      (isipv6 ? sizeof (struct ip6_hdr) +
671					       sizeof (struct tcphdr) :
672#endif
673				       sizeof (struct tcpiphdr)
674#ifdef INET6
675				       )
676#endif
677				      );
678			if (rt->rt_rmx.rmx_ssthresh)
679				rt->rt_rmx.rmx_ssthresh =
680				    (rt->rt_rmx.rmx_ssthresh + i) / 2;
681			else
682				rt->rt_rmx.rmx_ssthresh = i;
683			tcpstat.tcps_cachedssthresh++;
684		}
685	}
686    no_valid_rt:
687	/* free the reassembly queue, if any */
688	while((q = LIST_FIRST(&tp->t_segq)) != NULL) {
689		LIST_REMOVE(q, tqe_q);
690		m_freem(q->tqe_m);
691		FREE(q, M_TSEGQ);
692	}
693	if (tp->t_template)
694		(void) m_free(dtom(tp->t_template));
695	inp->inp_ppcb = NULL;
696	soisdisconnected(so);
697#ifdef INET6
698	if (INP_CHECK_SOCKAF(so, AF_INET6))
699		in6_pcbdetach(inp);
700	else
701#endif /* INET6 */
702	in_pcbdetach(inp);
703	tcpstat.tcps_closed++;
704	return ((struct tcpcb *)0);
705}
706
707void
708tcp_drain()
709{
710	if (do_tcpdrain)
711	{
712		struct inpcb *inpb;
713		struct tcpcb *tcpb;
714		struct tseg_qent *te;
715
716	/*
717	 * Walk the tcpbs, if existing, and flush the reassembly queue,
718	 * if there is one...
719	 * XXX: The "Net/3" implementation doesn't imply that the TCP
720	 *      reassembly queue should be flushed, but in a situation
721	 * 	where we're really low on mbufs, this is potentially
722	 *  	usefull.
723	 */
724		for (inpb = tcbinfo.listhead->lh_first; inpb;
725	    		inpb = inpb->inp_list.le_next) {
726				if ((tcpb = intotcpcb(inpb))) {
727					while ((te = LIST_FIRST(&tcpb->t_segq))
728					       != NULL) {
729					LIST_REMOVE(te, tqe_q);
730					m_freem(te->tqe_m);
731					FREE(te, M_TSEGQ);
732				}
733			}
734		}
735
736	}
737}
738
739/*
740 * Notify a tcp user of an asynchronous error;
741 * store error as soft error, but wake up user
742 * (for now, won't do anything until can select for soft error).
743 */
744static void
745tcp_notify(inp, error)
746	struct inpcb *inp;
747	int error;
748{
749	register struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb;
750	register struct socket *so = inp->inp_socket;
751
752	/*
753	 * Ignore some errors if we are hooked up.
754	 * If connection hasn't completed, has retransmitted several times,
755	 * and receives a second error, give up now.  This is better
756	 * than waiting a long time to establish a connection that
757	 * can never complete.
758	 */
759	if (tp->t_state == TCPS_ESTABLISHED &&
760	     (error == EHOSTUNREACH || error == ENETUNREACH ||
761	      error == EHOSTDOWN)) {
762		return;
763	} else if (tp->t_state < TCPS_ESTABLISHED && tp->t_rxtshift > 3 &&
764	    tp->t_softerror)
765		so->so_error = error;
766	else
767		tp->t_softerror = error;
768	wakeup((caddr_t) &so->so_timeo);
769	sorwakeup(so);
770	sowwakeup(so);
771}
772
773static int
774tcp_pcblist SYSCTL_HANDLER_ARGS
775{
776	int error, i, n, s;
777	struct inpcb *inp, **inp_list;
778	inp_gen_t gencnt;
779	struct xinpgen xig;
780
781	/*
782	 * The process of preparing the TCB list is too time-consuming and
783	 * resource-intensive to repeat twice on every request.
784	 */
785	if (req->oldptr == 0) {
786		n = tcbinfo.ipi_count;
787		req->oldidx = 2 * (sizeof xig)
788			+ (n + n/8) * sizeof(struct xtcpcb);
789		return 0;
790	}
791
792	if (req->newptr != 0)
793		return EPERM;
794
795	/*
796	 * OK, now we're committed to doing something.
797	 */
798	s = splnet();
799	gencnt = tcbinfo.ipi_gencnt;
800	n = tcbinfo.ipi_count;
801	splx(s);
802
803	xig.xig_len = sizeof xig;
804	xig.xig_count = n;
805	xig.xig_gen = gencnt;
806	xig.xig_sogen = so_gencnt;
807	error = SYSCTL_OUT(req, &xig, sizeof xig);
808	if (error)
809		return error;
810
811	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
812	if (inp_list == 0)
813		return ENOMEM;
814
815	s = splnet();
816	for (inp = tcbinfo.listhead->lh_first, i = 0; inp && i < n;
817	     inp = inp->inp_list.le_next) {
818		if (inp->inp_gencnt <= gencnt && !prison_xinpcb(req->p, inp))
819			inp_list[i++] = inp;
820	}
821	splx(s);
822	n = i;
823
824	error = 0;
825	for (i = 0; i < n; i++) {
826		inp = inp_list[i];
827		if (inp->inp_gencnt <= gencnt) {
828			struct xtcpcb xt;
829			caddr_t inp_ppcb;
830			xt.xt_len = sizeof xt;
831			/* XXX should avoid extra copy */
832			bcopy(inp, &xt.xt_inp, sizeof *inp);
833			inp_ppcb = inp->inp_ppcb;
834			if (inp_ppcb != NULL)
835				bcopy(inp_ppcb, &xt.xt_tp, sizeof xt.xt_tp);
836			else
837				bzero((char *) &xt.xt_tp, sizeof xt.xt_tp);
838			if (inp->inp_socket)
839				sotoxsocket(inp->inp_socket, &xt.xt_socket);
840			error = SYSCTL_OUT(req, &xt, sizeof xt);
841		}
842	}
843	if (!error) {
844		/*
845		 * Give the user an updated idea of our state.
846		 * If the generation differs from what we told
847		 * her before, she knows that something happened
848		 * while we were processing this request, and it
849		 * might be necessary to retry.
850		 */
851		s = splnet();
852		xig.xig_gen = tcbinfo.ipi_gencnt;
853		xig.xig_sogen = so_gencnt;
854		xig.xig_count = tcbinfo.ipi_count;
855		splx(s);
856		error = SYSCTL_OUT(req, &xig, sizeof xig);
857	}
858	free(inp_list, M_TEMP);
859	return error;
860}
861
862SYSCTL_PROC(_net_inet_tcp, TCPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
863	    tcp_pcblist, "S,xtcpcb", "List of active TCP connections");
864
865static int
866tcp_getcred SYSCTL_HANDLER_ARGS
867{
868	struct sockaddr_in addrs[2];
869	struct inpcb *inp;
870	int error, s;
871
872	error = suser(req->p);
873	if (error)
874		return (error);
875	error = SYSCTL_IN(req, addrs, sizeof(addrs));
876	if (error)
877		return (error);
878	s = splnet();
879	inp = in_pcblookup_hash(&tcbinfo, addrs[1].sin_addr, addrs[1].sin_port,
880	    addrs[0].sin_addr, addrs[0].sin_port, 0, NULL);
881	if (inp == NULL || inp->inp_socket == NULL) {
882		error = ENOENT;
883		goto out;
884	}
885	error = SYSCTL_OUT(req, inp->inp_socket->so_cred, sizeof(struct ucred));
886out:
887	splx(s);
888	return (error);
889}
890
891SYSCTL_PROC(_net_inet_tcp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
892    0, 0, tcp_getcred, "S,ucred", "Get the ucred of a TCP connection");
893
894#ifdef INET6
895static int
896tcp6_getcred SYSCTL_HANDLER_ARGS
897{
898	struct sockaddr_in6 addrs[2];
899	struct inpcb *inp;
900	int error, s, mapped = 0;
901
902	error = suser(req->p);
903	if (error)
904		return (error);
905	error = SYSCTL_IN(req, addrs, sizeof(addrs));
906	if (error)
907		return (error);
908	if (IN6_IS_ADDR_V4MAPPED(&addrs[0].sin6_addr)) {
909		if (IN6_IS_ADDR_V4MAPPED(&addrs[1].sin6_addr))
910			mapped = 1;
911		else
912			return (EINVAL);
913	}
914	s = splnet();
915	if (mapped == 1)
916		inp = in_pcblookup_hash(&tcbinfo,
917			*(struct in_addr *)&addrs[1].sin6_addr.s6_addr[12],
918			addrs[1].sin6_port,
919			*(struct in_addr *)&addrs[0].sin6_addr.s6_addr[12],
920			addrs[0].sin6_port,
921			0, NULL);
922	else
923		inp = in6_pcblookup_hash(&tcbinfo, &addrs[1].sin6_addr,
924				 addrs[1].sin6_port,
925				 &addrs[0].sin6_addr, addrs[0].sin6_port,
926				 0, NULL);
927	if (inp == NULL || inp->inp_socket == NULL) {
928		error = ENOENT;
929		goto out;
930	}
931	error = SYSCTL_OUT(req, inp->inp_socket->so_cred,
932			   sizeof(struct ucred));
933out:
934	splx(s);
935	return (error);
936}
937
938SYSCTL_PROC(_net_inet6_tcp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
939	    0, 0,
940	    tcp6_getcred, "S,ucred", "Get the ucred of a TCP6 connection");
941#endif
942
943
944void
945tcp_ctlinput(cmd, sa, vip)
946	int cmd;
947	struct sockaddr *sa;
948	void *vip;
949{
950	register struct ip *ip = vip;
951	register struct tcphdr *th;
952	void (*notify) __P((struct inpcb *, int)) = tcp_notify;
953
954	if (cmd == PRC_QUENCH)
955		notify = tcp_quench;
956	else if (cmd == PRC_MSGSIZE)
957		notify = tcp_mtudisc;
958	else if (!PRC_IS_REDIRECT(cmd) &&
959		 ((unsigned)cmd > PRC_NCMDS || inetctlerrmap[cmd] == 0))
960		return;
961	if (ip) {
962		th = (struct tcphdr *)((caddr_t)ip
963				       + (IP_VHL_HL(ip->ip_vhl) << 2));
964		in_pcbnotify(&tcb, sa, th->th_dport, ip->ip_src, th->th_sport,
965			cmd, notify);
966	} else
967		in_pcbnotify(&tcb, sa, 0, zeroin_addr, 0, cmd, notify);
968}
969
970#ifdef INET6
971void
972tcp6_ctlinput(cmd, sa, d)
973	int cmd;
974	struct sockaddr *sa;
975	void *d;
976{
977	register struct tcphdr *thp;
978	struct tcphdr th;
979	void (*notify) __P((struct inpcb *, int)) = tcp_notify;
980	struct sockaddr_in6 sa6;
981	struct ip6_hdr *ip6;
982	struct mbuf *m;
983	int off;
984
985	if (sa->sa_family != AF_INET6 ||
986	    sa->sa_len != sizeof(struct sockaddr_in6))
987		return;
988
989	if (cmd == PRC_QUENCH)
990		notify = tcp_quench;
991	else if (cmd == PRC_MSGSIZE)
992		notify = tcp_mtudisc;
993	else if (!PRC_IS_REDIRECT(cmd) &&
994		 ((unsigned)cmd > PRC_NCMDS || inet6ctlerrmap[cmd] == 0))
995		return;
996
997	/* if the parameter is from icmp6, decode it. */
998	if (d != NULL) {
999		struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d;
1000		m = ip6cp->ip6c_m;
1001		ip6 = ip6cp->ip6c_ip6;
1002		off = ip6cp->ip6c_off;
1003	} else {
1004		m = NULL;
1005		ip6 = NULL;
1006	}
1007
1008	/*
1009	 * Translate addresses into internal form.
1010	 * Sa check if it is AF_INET6 is done at the top of this funciton.
1011	 */
1012	sa6 = *(struct sockaddr_in6 *)sa;
1013	if (IN6_IS_ADDR_LINKLOCAL(&sa6.sin6_addr) != 0 && m != NULL &&
1014	    m->m_pkthdr.rcvif != NULL)
1015		sa6.sin6_addr.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
1016
1017	if (ip6) {
1018		/*
1019		 * XXX: We assume that when IPV6 is non NULL,
1020		 * M and OFF are valid.
1021		 */
1022		struct in6_addr s;
1023
1024		/* translate addresses into internal form */
1025		memcpy(&s, &ip6->ip6_src, sizeof(s));
1026		if (IN6_IS_ADDR_LINKLOCAL(&s) != 0 && m != NULL &&
1027		    m->m_pkthdr.rcvif != NULL)
1028			s.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
1029
1030		if (m->m_len < off + sizeof(*thp)) {
1031			/*
1032			 * this should be rare case
1033			 * because now MINCLSIZE is "(MHLEN + 1)",
1034			 * so we compromise on this copy...
1035			 */
1036			m_copydata(m, off, sizeof(th), (caddr_t)&th);
1037			thp = &th;
1038		} else
1039			thp = (struct tcphdr *)(mtod(m, caddr_t) + off);
1040		in6_pcbnotify(&tcb, (struct sockaddr *)&sa6, thp->th_dport,
1041			      &s, thp->th_sport, cmd, notify);
1042	} else
1043		in6_pcbnotify(&tcb, (struct sockaddr *)&sa6, 0, &zeroin6_addr,
1044			      0, cmd, notify);
1045}
1046#endif /* INET6 */
1047
1048/*
1049 * When a source quench is received, close congestion window
1050 * to one segment.  We will gradually open it again as we proceed.
1051 */
1052void
1053tcp_quench(inp, errno)
1054	struct inpcb *inp;
1055	int errno;
1056{
1057	struct tcpcb *tp = intotcpcb(inp);
1058
1059	if (tp)
1060		tp->snd_cwnd = tp->t_maxseg;
1061}
1062
1063/*
1064 * When `need fragmentation' ICMP is received, update our idea of the MSS
1065 * based on the new value in the route.  Also nudge TCP to send something,
1066 * since we know the packet we just sent was dropped.
1067 * This duplicates some code in the tcp_mss() function in tcp_input.c.
1068 */
1069void
1070tcp_mtudisc(inp, errno)
1071	struct inpcb *inp;
1072	int errno;
1073{
1074	struct tcpcb *tp = intotcpcb(inp);
1075	struct rtentry *rt;
1076	struct rmxp_tao *taop;
1077	struct socket *so = inp->inp_socket;
1078	int offered;
1079	int mss;
1080#ifdef INET6
1081	int isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
1082#endif /* INET6 */
1083
1084	if (tp) {
1085#ifdef INET6
1086		if (isipv6)
1087			rt = tcp_rtlookup6(inp);
1088		else
1089#endif /* INET6 */
1090		rt = tcp_rtlookup(inp);
1091		if (!rt || !rt->rt_rmx.rmx_mtu) {
1092			tp->t_maxopd = tp->t_maxseg =
1093#ifdef INET6
1094				isipv6 ? tcp_v6mssdflt :
1095#endif /* INET6 */
1096				tcp_mssdflt;
1097			return;
1098		}
1099		taop = rmx_taop(rt->rt_rmx);
1100		offered = taop->tao_mssopt;
1101		mss = rt->rt_rmx.rmx_mtu -
1102#ifdef INET6
1103			(isipv6 ?
1104			 sizeof(struct ip6_hdr) + sizeof(struct tcphdr) :
1105#endif /* INET6 */
1106			 sizeof(struct tcpiphdr)
1107#ifdef INET6
1108			 )
1109#endif /* INET6 */
1110			;
1111
1112		if (offered)
1113			mss = min(mss, offered);
1114		/*
1115		 * XXX - The above conditional probably violates the TCP
1116		 * spec.  The problem is that, since we don't know the
1117		 * other end's MSS, we are supposed to use a conservative
1118		 * default.  But, if we do that, then MTU discovery will
1119		 * never actually take place, because the conservative
1120		 * default is much less than the MTUs typically seen
1121		 * on the Internet today.  For the moment, we'll sweep
1122		 * this under the carpet.
1123		 *
1124		 * The conservative default might not actually be a problem
1125		 * if the only case this occurs is when sending an initial
1126		 * SYN with options and data to a host we've never talked
1127		 * to before.  Then, they will reply with an MSS value which
1128		 * will get recorded and the new parameters should get
1129		 * recomputed.  For Further Study.
1130		 */
1131		if (tp->t_maxopd <= mss)
1132			return;
1133		tp->t_maxopd = mss;
1134
1135		if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
1136		    (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)
1137			mss -= TCPOLEN_TSTAMP_APPA;
1138		if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
1139		    (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC)
1140			mss -= TCPOLEN_CC_APPA;
1141#if	(MCLBYTES & (MCLBYTES - 1)) == 0
1142		if (mss > MCLBYTES)
1143			mss &= ~(MCLBYTES-1);
1144#else
1145		if (mss > MCLBYTES)
1146			mss = mss / MCLBYTES * MCLBYTES;
1147#endif
1148		if (so->so_snd.sb_hiwat < mss)
1149			mss = so->so_snd.sb_hiwat;
1150
1151		tp->t_maxseg = mss;
1152
1153		tcpstat.tcps_mturesent++;
1154		tp->t_rtttime = 0;
1155		tp->snd_nxt = tp->snd_una;
1156		tcp_output(tp);
1157	}
1158}
1159
1160/*
1161 * Look-up the routing entry to the peer of this inpcb.  If no route
1162 * is found and it cannot be allocated the return NULL.  This routine
1163 * is called by TCP routines that access the rmx structure and by tcp_mss
1164 * to get the interface MTU.
1165 */
1166struct rtentry *
1167tcp_rtlookup(inp)
1168	struct inpcb *inp;
1169{
1170	struct route *ro;
1171	struct rtentry *rt;
1172
1173	ro = &inp->inp_route;
1174	rt = ro->ro_rt;
1175	if (rt == NULL || !(rt->rt_flags & RTF_UP)) {
1176		/* No route yet, so try to acquire one */
1177		if (inp->inp_faddr.s_addr != INADDR_ANY) {
1178			ro->ro_dst.sa_family = AF_INET;
1179			ro->ro_dst.sa_len = sizeof(ro->ro_dst);
1180			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
1181				inp->inp_faddr;
1182			rtalloc(ro);
1183			rt = ro->ro_rt;
1184		}
1185	}
1186	return rt;
1187}
1188
1189#ifdef INET6
1190struct rtentry *
1191tcp_rtlookup6(inp)
1192	struct inpcb *inp;
1193{
1194	struct route_in6 *ro6;
1195	struct rtentry *rt;
1196
1197	ro6 = &inp->in6p_route;
1198	rt = ro6->ro_rt;
1199	if (rt == NULL || !(rt->rt_flags & RTF_UP)) {
1200		/* No route yet, so try to acquire one */
1201		if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1202			ro6->ro_dst.sin6_family = AF_INET6;
1203			ro6->ro_dst.sin6_len = sizeof(ro6->ro_dst);
1204			ro6->ro_dst.sin6_addr = inp->in6p_faddr;
1205			rtalloc((struct route *)ro6);
1206			rt = ro6->ro_rt;
1207		}
1208	}
1209	return rt;
1210}
1211#endif /* INET6 */
1212
1213#ifdef IPSEC
1214/* compute ESP/AH header size for TCP, including outer IP header. */
1215size_t
1216ipsec_hdrsiz_tcp(tp)
1217	struct tcpcb *tp;
1218{
1219	struct inpcb *inp;
1220	struct mbuf *m;
1221	size_t hdrsiz;
1222	struct ip *ip;
1223#ifdef INET6
1224	struct ip6_hdr *ip6;
1225#endif /* INET6 */
1226	struct tcphdr *th;
1227
1228	if (!tp || !tp->t_template || !(inp = tp->t_inpcb))
1229		return 0;
1230	MGETHDR(m, M_DONTWAIT, MT_DATA);
1231	if (!m)
1232		return 0;
1233
1234#ifdef INET6
1235	if ((inp->inp_vflag & INP_IPV6) != 0) {
1236		ip6 = mtod(m, struct ip6_hdr *);
1237		th = (struct tcphdr *)(ip6 + 1);
1238		m->m_pkthdr.len = m->m_len =
1239			sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
1240		bcopy((caddr_t)&tp->t_template->tt_ipgen, (caddr_t)ip6,
1241		      sizeof(struct ip6_hdr));
1242		bcopy((caddr_t)&tp->t_template->tt_t, (caddr_t)th,
1243		      sizeof(struct tcphdr));
1244		hdrsiz = ipsec6_hdrsiz(m, IPSEC_DIR_OUTBOUND, inp);
1245	} else
1246#endif /* INET6 */
1247      {
1248	ip = mtod(m, struct ip *);
1249	th = (struct tcphdr *)(ip + 1);
1250	m->m_pkthdr.len = m->m_len = sizeof(struct tcpiphdr);
1251	bcopy((caddr_t)&tp->t_template->tt_ipgen, (caddr_t)ip,
1252	      sizeof(struct ip));
1253	bcopy((caddr_t)&tp->t_template->tt_t, (caddr_t)th,
1254	      sizeof(struct tcphdr));
1255	hdrsiz = ipsec4_hdrsiz(m, IPSEC_DIR_OUTBOUND, inp);
1256      }
1257
1258	m_free(m);
1259	return hdrsiz;
1260}
1261#endif /*IPSEC*/
1262
1263/*
1264 * Return a pointer to the cached information about the remote host.
1265 * The cached information is stored in the protocol specific part of
1266 * the route metrics.
1267 */
1268struct rmxp_tao *
1269tcp_gettaocache(inp)
1270	struct inpcb *inp;
1271{
1272	struct rtentry *rt;
1273
1274#ifdef INET6
1275	if ((inp->inp_vflag & INP_IPV6) != 0)
1276		rt = tcp_rtlookup6(inp);
1277	else
1278#endif /* INET6 */
1279	rt = tcp_rtlookup(inp);
1280
1281	/* Make sure this is a host route and is up. */
1282	if (rt == NULL ||
1283	    (rt->rt_flags & (RTF_UP|RTF_HOST)) != (RTF_UP|RTF_HOST))
1284		return NULL;
1285
1286	return rmx_taop(rt->rt_rmx);
1287}
1288
1289/*
1290 * Clear all the TAO cache entries, called from tcp_init.
1291 *
1292 * XXX
1293 * This routine is just an empty one, because we assume that the routing
1294 * routing tables are initialized at the same time when TCP, so there is
1295 * nothing in the cache left over.
1296 */
1297static void
1298tcp_cleartaocache()
1299{
1300}
1301