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