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