tcp_timewait.c revision 74362
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 74362 2001-03-16 20:00:53Z phk $
35 */
36
37#include "opt_compat.h"
38#include "opt_inet6.h"
39#include "opt_ipsec.h"
40#include "opt_tcpdebug.h"
41
42#include <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 int	icmp_may_rst = 1;
138SYSCTL_INT(_net_inet_tcp, OID_AUTO, icmp_may_rst, CTLFLAG_RW, &icmp_may_rst, 0,
139    "Certain ICMP unreachable messages may abort connections in SYN_SENT");
140
141static void	tcp_cleartaocache __P((void));
142static void	tcp_notify __P((struct inpcb *, int));
143
144/*
145 * Target size of TCP PCB hash tables. Must be a power of two.
146 *
147 * Note that this can be overridden by the kernel environment
148 * variable net.inet.tcp.tcbhashsize
149 */
150#ifndef TCBHASHSIZE
151#define TCBHASHSIZE	512
152#endif
153
154/*
155 * This is the actual shape of what we allocate using the zone
156 * allocator.  Doing it this way allows us to protect both structures
157 * using the same generation count, and also eliminates the overhead
158 * of allocating tcpcbs separately.  By hiding the structure here,
159 * we avoid changing most of the rest of the code (although it needs
160 * to be changed, eventually, for greater efficiency).
161 */
162#define	ALIGNMENT	32
163#define	ALIGNM1		(ALIGNMENT - 1)
164struct	inp_tp {
165	union {
166		struct	inpcb inp;
167		char	align[(sizeof(struct inpcb) + ALIGNM1) & ~ALIGNM1];
168	} inp_tp_u;
169	struct	tcpcb tcb;
170	struct	callout inp_tp_rexmt, inp_tp_persist, inp_tp_keep, inp_tp_2msl;
171	struct	callout inp_tp_delack;
172};
173#undef ALIGNMENT
174#undef ALIGNM1
175
176/*
177 * Tcp initialization
178 */
179void
180tcp_init()
181{
182	int hashsize;
183
184	tcp_iss = arc4random();	/* wrong, but better than a constant */
185	tcp_ccgen = 1;
186	tcp_cleartaocache();
187
188	tcp_delacktime = TCPTV_DELACK;
189	tcp_keepinit = TCPTV_KEEP_INIT;
190	tcp_keepidle = TCPTV_KEEP_IDLE;
191	tcp_keepintvl = TCPTV_KEEPINTVL;
192	tcp_maxpersistidle = TCPTV_KEEP_IDLE;
193	tcp_msl = TCPTV_MSL;
194
195	LIST_INIT(&tcb);
196	tcbinfo.listhead = &tcb;
197	TUNABLE_INT_FETCH("net.inet.tcp.tcbhashsize", TCBHASHSIZE, hashsize);
198	if (!powerof2(hashsize)) {
199		printf("WARNING: TCB hash size not a power of 2\n");
200		hashsize = 512; /* safe default */
201	}
202	tcp_tcbhashsize = hashsize;
203	tcbinfo.hashbase = hashinit(hashsize, M_PCB, &tcbinfo.hashmask);
204	tcbinfo.porthashbase = hashinit(hashsize, M_PCB,
205					&tcbinfo.porthashmask);
206	tcbinfo.ipi_zone = zinit("tcpcb", sizeof(struct inp_tp), maxsockets,
207				 ZONE_INTERRUPT, 0);
208#ifdef INET6
209#define TCP_MINPROTOHDR (sizeof(struct ip6_hdr) + sizeof(struct tcphdr))
210#else /* INET6 */
211#define TCP_MINPROTOHDR (sizeof(struct tcpiphdr))
212#endif /* INET6 */
213	if (max_protohdr < TCP_MINPROTOHDR)
214		max_protohdr = TCP_MINPROTOHDR;
215	if (max_linkhdr + TCP_MINPROTOHDR > MHLEN)
216		panic("tcp_init");
217#undef TCP_MINPROTOHDR
218}
219
220/*
221 * Create template to be used to send tcp packets on a connection.
222 * Call after host entry created, allocates an mbuf and fills
223 * in a skeletal tcp/ip header, minimizing the amount of work
224 * necessary when the connection is used.
225 */
226struct tcptemp *
227tcp_template(tp)
228	struct tcpcb *tp;
229{
230	register struct inpcb *inp = tp->t_inpcb;
231	register struct mbuf *m;
232	register struct tcptemp *n;
233
234	if ((n = tp->t_template) == 0) {
235		m = m_get(M_DONTWAIT, MT_HEADER);
236		if (m == NULL)
237			return (0);
238		m->m_len = sizeof (struct tcptemp);
239		n = mtod(m, struct tcptemp *);
240	}
241#ifdef INET6
242	if ((inp->inp_vflag & INP_IPV6) != 0) {
243		register struct ip6_hdr *ip6;
244
245		ip6 = (struct ip6_hdr *)n->tt_ipgen;
246		ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
247			(inp->in6p_flowinfo & IPV6_FLOWINFO_MASK);
248		ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
249			(IPV6_VERSION & IPV6_VERSION_MASK);
250		ip6->ip6_nxt = IPPROTO_TCP;
251		ip6->ip6_plen = sizeof(struct tcphdr);
252		ip6->ip6_src = inp->in6p_laddr;
253		ip6->ip6_dst = inp->in6p_faddr;
254		n->tt_t.th_sum = 0;
255	} else
256#endif
257      {
258	struct ip *ip = (struct ip *)n->tt_ipgen;
259
260	bzero(ip, sizeof(struct ip));		/* XXX overkill? */
261	ip->ip_vhl = IP_VHL_BORING;
262	ip->ip_p = IPPROTO_TCP;
263	ip->ip_src = inp->inp_laddr;
264	ip->ip_dst = inp->inp_faddr;
265	n->tt_t.th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
266	    htons(sizeof(struct tcphdr) + IPPROTO_TCP));
267      }
268	n->tt_t.th_sport = inp->inp_lport;
269	n->tt_t.th_dport = inp->inp_fport;
270	n->tt_t.th_seq = 0;
271	n->tt_t.th_ack = 0;
272	n->tt_t.th_x2 = 0;
273	n->tt_t.th_off = 5;
274	n->tt_t.th_flags = 0;
275	n->tt_t.th_win = 0;
276	n->tt_t.th_urp = 0;
277	return (n);
278}
279
280/*
281 * Send a single message to the TCP at address specified by
282 * the given TCP/IP header.  If m == 0, then we make a copy
283 * of the tcpiphdr at ti and send directly to the addressed host.
284 * This is used to force keep alive messages out using the TCP
285 * template for a connection tp->t_template.  If flags are given
286 * then we send a message back to the TCP which originated the
287 * segment ti, and discard the mbuf containing it and any other
288 * attached mbufs.
289 *
290 * In any case the ack and sequence number of the transmitted
291 * segment are as specified by the parameters.
292 *
293 * NOTE: If m != NULL, then ti must point to *inside* the mbuf.
294 */
295void
296tcp_respond(tp, ipgen, th, m, ack, seq, flags)
297	struct tcpcb *tp;
298	void *ipgen;
299	register struct tcphdr *th;
300	register struct mbuf *m;
301	tcp_seq ack, seq;
302	int flags;
303{
304	register int tlen;
305	int win = 0;
306	struct route *ro = 0;
307	struct route sro;
308	struct ip *ip;
309	struct tcphdr *nth;
310#ifdef INET6
311	struct route_in6 *ro6 = 0;
312	struct route_in6 sro6;
313	struct ip6_hdr *ip6;
314	int isipv6;
315#endif /* INET6 */
316	int ipflags = 0;
317
318#ifdef INET6
319	isipv6 = IP_VHL_V(((struct ip *)ipgen)->ip_vhl) == 6;
320	ip6 = ipgen;
321#endif /* INET6 */
322	ip = ipgen;
323
324	if (tp) {
325		if (!(flags & TH_RST)) {
326			win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
327			if (win > (long)TCP_MAXWIN << tp->rcv_scale)
328				win = (long)TCP_MAXWIN << tp->rcv_scale;
329		}
330#ifdef INET6
331		if (isipv6)
332			ro6 = &tp->t_inpcb->in6p_route;
333		else
334#endif /* INET6 */
335		ro = &tp->t_inpcb->inp_route;
336	} else {
337#ifdef INET6
338		if (isipv6) {
339			ro6 = &sro6;
340			bzero(ro6, sizeof *ro6);
341		} else
342#endif /* INET6 */
343	      {
344		ro = &sro;
345		bzero(ro, sizeof *ro);
346	      }
347	}
348	if (m == 0) {
349		m = m_gethdr(M_DONTWAIT, MT_HEADER);
350		if (m == NULL)
351			return;
352#ifdef TCP_COMPAT_42
353		tlen = 1;
354#else
355		tlen = 0;
356#endif
357		m->m_data += max_linkhdr;
358#ifdef INET6
359		if (isipv6) {
360			bcopy((caddr_t)ip6, mtod(m, caddr_t),
361			      sizeof(struct ip6_hdr));
362			ip6 = mtod(m, struct ip6_hdr *);
363			nth = (struct tcphdr *)(ip6 + 1);
364		} else
365#endif /* INET6 */
366	      {
367		bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
368		ip = mtod(m, struct ip *);
369		nth = (struct tcphdr *)(ip + 1);
370	      }
371		bcopy((caddr_t)th, (caddr_t)nth, sizeof(struct tcphdr));
372		flags = TH_ACK;
373	} else {
374		m_freem(m->m_next);
375		m->m_next = 0;
376		m->m_data = (caddr_t)ipgen;
377		/* m_len is set later */
378		tlen = 0;
379#define xchg(a,b,type) { type t; t=a; a=b; b=t; }
380#ifdef INET6
381		if (isipv6) {
382			xchg(ip6->ip6_dst, ip6->ip6_src, struct in6_addr);
383			nth = (struct tcphdr *)(ip6 + 1);
384		} else
385#endif /* INET6 */
386	      {
387		xchg(ip->ip_dst.s_addr, ip->ip_src.s_addr, n_long);
388		nth = (struct tcphdr *)(ip + 1);
389	      }
390		if (th != nth) {
391			/*
392			 * this is usually a case when an extension header
393			 * exists between the IPv6 header and the
394			 * TCP header.
395			 */
396			nth->th_sport = th->th_sport;
397			nth->th_dport = th->th_dport;
398		}
399		xchg(nth->th_dport, nth->th_sport, n_short);
400#undef xchg
401	}
402#ifdef INET6
403	if (isipv6) {
404		ip6->ip6_plen = htons((u_short)(sizeof (struct tcphdr) +
405						tlen));
406		tlen += sizeof (struct ip6_hdr) + sizeof (struct tcphdr);
407	} else
408#endif
409      {
410	tlen += sizeof (struct tcpiphdr);
411	ip->ip_len = tlen;
412	ip->ip_ttl = ip_defttl;
413      }
414	m->m_len = tlen;
415	m->m_pkthdr.len = tlen;
416	m->m_pkthdr.rcvif = (struct ifnet *) 0;
417	nth->th_seq = htonl(seq);
418	nth->th_ack = htonl(ack);
419	nth->th_x2 = 0;
420	nth->th_off = sizeof (struct tcphdr) >> 2;
421	nth->th_flags = flags;
422	if (tp)
423		nth->th_win = htons((u_short) (win >> tp->rcv_scale));
424	else
425		nth->th_win = htons((u_short)win);
426	nth->th_urp = 0;
427#ifdef INET6
428	if (isipv6) {
429		nth->th_sum = 0;
430		nth->th_sum = in6_cksum(m, IPPROTO_TCP,
431					sizeof(struct ip6_hdr),
432					tlen - sizeof(struct ip6_hdr));
433		ip6->ip6_hlim = in6_selecthlim(tp ? tp->t_inpcb : NULL,
434					       ro6 && ro6->ro_rt ?
435					       ro6->ro_rt->rt_ifp :
436					       NULL);
437	} else
438#endif /* INET6 */
439      {
440        nth->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
441	    htons((u_short)(tlen - sizeof(struct ip) + ip->ip_p)));
442        m->m_pkthdr.csum_flags = CSUM_TCP;
443        m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
444      }
445#ifdef TCPDEBUG
446	if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
447		tcp_trace(TA_OUTPUT, 0, tp, mtod(m, void *), th, 0);
448#endif
449#ifdef IPSEC
450	ipsec_setsocket(m, tp ? tp->t_inpcb->inp_socket : NULL);
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			ro6->ro_rt = NULL;
458		}
459	} else
460#endif /* INET6 */
461      {
462	(void) ip_output(m, NULL, ro, ipflags, NULL);
463	if (ro == &sro && ro->ro_rt) {
464		RTFREE(ro->ro_rt);
465		ro->ro_rt = NULL;
466	}
467      }
468}
469
470/*
471 * Create a new TCP control block, making an
472 * empty reassembly queue and hooking it to the argument
473 * protocol control block.  The `inp' parameter must have
474 * come from the zone allocator set up in tcp_init().
475 */
476struct tcpcb *
477tcp_newtcpcb(inp)
478	struct inpcb *inp;
479{
480	struct inp_tp *it;
481	register struct tcpcb *tp;
482#ifdef INET6
483	int isipv6 = (inp->inp_vflag & INP_IPV6) != 0;
484#endif /* INET6 */
485
486	it = (struct inp_tp *)inp;
487	tp = &it->tcb;
488	bzero((char *) tp, sizeof(struct tcpcb));
489	LIST_INIT(&tp->t_segq);
490	tp->t_maxseg = tp->t_maxopd =
491#ifdef INET6
492		isipv6 ? tcp_v6mssdflt :
493#endif /* INET6 */
494		tcp_mssdflt;
495
496	/* Set up our timeouts. */
497	callout_init(tp->tt_rexmt = &it->inp_tp_rexmt, 0);
498	callout_init(tp->tt_persist = &it->inp_tp_persist, 0);
499	callout_init(tp->tt_keep = &it->inp_tp_keep, 0);
500	callout_init(tp->tt_2msl = &it->inp_tp_2msl, 0);
501	callout_init(tp->tt_delack = &it->inp_tp_delack, 0);
502
503	if (tcp_do_rfc1323)
504		tp->t_flags = (TF_REQ_SCALE|TF_REQ_TSTMP);
505	if (tcp_do_rfc1644)
506		tp->t_flags |= TF_REQ_CC;
507	tp->t_inpcb = inp;	/* XXX */
508	/*
509	 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
510	 * rtt estimate.  Set rttvar so that srtt + 4 * rttvar gives
511	 * reasonable initial retransmit time.
512	 */
513	tp->t_srtt = TCPTV_SRTTBASE;
514	tp->t_rttvar = ((TCPTV_RTOBASE - TCPTV_SRTTBASE) << TCP_RTTVAR_SHIFT) / 4;
515	tp->t_rttmin = TCPTV_MIN;
516	tp->t_rxtcur = TCPTV_RTOBASE;
517	tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
518	tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
519	tp->t_rcvtime = ticks;
520        /*
521	 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
522	 * because the socket may be bound to an IPv6 wildcard address,
523	 * which may match an IPv4-mapped IPv6 address.
524	 */
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	rt = inp->inp_route.ro_rt;
687	if (rt) {
688		/*
689		 * mark route for deletion if no information is
690		 * cached.
691		 */
692		if ((tp->t_flags & TF_LQ_OVERFLOW) &&
693		    ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0)){
694			if (rt->rt_rmx.rmx_rtt == 0)
695				rt->rt_flags |= RTF_DELCLONE;
696		}
697	}
698    no_valid_rt:
699	/* free the reassembly queue, if any */
700	while((q = LIST_FIRST(&tp->t_segq)) != NULL) {
701		LIST_REMOVE(q, tqe_q);
702		m_freem(q->tqe_m);
703		FREE(q, M_TSEGQ);
704	}
705	if (tp->t_template)
706		(void) m_free(dtom(tp->t_template));
707	inp->inp_ppcb = NULL;
708	soisdisconnected(so);
709#ifdef INET6
710	if (INP_CHECK_SOCKAF(so, AF_INET6))
711		in6_pcbdetach(inp);
712	else
713#endif /* INET6 */
714	in_pcbdetach(inp);
715	tcpstat.tcps_closed++;
716	return ((struct tcpcb *)0);
717}
718
719void
720tcp_drain()
721{
722	if (do_tcpdrain)
723	{
724		struct inpcb *inpb;
725		struct tcpcb *tcpb;
726		struct tseg_qent *te;
727
728	/*
729	 * Walk the tcpbs, if existing, and flush the reassembly queue,
730	 * if there is one...
731	 * XXX: The "Net/3" implementation doesn't imply that the TCP
732	 *      reassembly queue should be flushed, but in a situation
733	 * 	where we're really low on mbufs, this is potentially
734	 *  	usefull.
735	 */
736		LIST_FOREACH(inpb, tcbinfo.listhead, inp_list) {
737			if ((tcpb = intotcpcb(inpb))) {
738				while ((te = LIST_FIRST(&tcpb->t_segq))
739			            != NULL) {
740					LIST_REMOVE(te, tqe_q);
741					m_freem(te->tqe_m);
742					FREE(te, M_TSEGQ);
743				}
744			}
745		}
746	}
747}
748
749/*
750 * Notify a tcp user of an asynchronous error;
751 * store error as soft error, but wake up user
752 * (for now, won't do anything until can select for soft error).
753 *
754 * Do not wake up user since there currently is no mechanism for
755 * reporting soft errors (yet - a kqueue filter may be added).
756 */
757static void
758tcp_notify(inp, error)
759	struct inpcb *inp;
760	int error;
761{
762	struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb;
763
764	/*
765	 * Ignore some errors if we are hooked up.
766	 * If connection hasn't completed, has retransmitted several times,
767	 * and receives a second error, give up now.  This is better
768	 * than waiting a long time to establish a connection that
769	 * can never complete.
770	 */
771	if (tp->t_state == TCPS_ESTABLISHED &&
772	     (error == EHOSTUNREACH || error == ENETUNREACH ||
773	      error == EHOSTDOWN)) {
774		return;
775	} else if (tp->t_state < TCPS_ESTABLISHED && tp->t_rxtshift > 3 &&
776	    tp->t_softerror)
777		tcp_drop(tp, error);
778	else
779		tp->t_softerror = error;
780#if 0
781	wakeup((caddr_t) &so->so_timeo);
782	sorwakeup(so);
783	sowwakeup(so);
784#endif
785}
786
787static int
788tcp_pcblist(SYSCTL_HANDLER_ARGS)
789{
790	int error, i, n, s;
791	struct inpcb *inp, **inp_list;
792	inp_gen_t gencnt;
793	struct xinpgen xig;
794
795	/*
796	 * The process of preparing the TCB list is too time-consuming and
797	 * resource-intensive to repeat twice on every request.
798	 */
799	if (req->oldptr == 0) {
800		n = tcbinfo.ipi_count;
801		req->oldidx = 2 * (sizeof xig)
802			+ (n + n/8) * sizeof(struct xtcpcb);
803		return 0;
804	}
805
806	if (req->newptr != 0)
807		return EPERM;
808
809	/*
810	 * OK, now we're committed to doing something.
811	 */
812	s = splnet();
813	gencnt = tcbinfo.ipi_gencnt;
814	n = tcbinfo.ipi_count;
815	splx(s);
816
817	xig.xig_len = sizeof xig;
818	xig.xig_count = n;
819	xig.xig_gen = gencnt;
820	xig.xig_sogen = so_gencnt;
821	error = SYSCTL_OUT(req, &xig, sizeof xig);
822	if (error)
823		return error;
824
825	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
826	if (inp_list == 0)
827		return ENOMEM;
828
829	s = splnet();
830	for (inp = LIST_FIRST(tcbinfo.listhead), i = 0; inp && i < n;
831	     inp = LIST_NEXT(inp, inp_list)) {
832		if (inp->inp_gencnt <= gencnt && !prison_xinpcb(req->p, inp))
833			inp_list[i++] = inp;
834	}
835	splx(s);
836	n = i;
837
838	error = 0;
839	for (i = 0; i < n; i++) {
840		inp = inp_list[i];
841		if (inp->inp_gencnt <= gencnt) {
842			struct xtcpcb xt;
843			caddr_t inp_ppcb;
844			xt.xt_len = sizeof xt;
845			/* XXX should avoid extra copy */
846			bcopy(inp, &xt.xt_inp, sizeof *inp);
847			inp_ppcb = inp->inp_ppcb;
848			if (inp_ppcb != NULL)
849				bcopy(inp_ppcb, &xt.xt_tp, sizeof xt.xt_tp);
850			else
851				bzero((char *) &xt.xt_tp, sizeof xt.xt_tp);
852			if (inp->inp_socket)
853				sotoxsocket(inp->inp_socket, &xt.xt_socket);
854			error = SYSCTL_OUT(req, &xt, sizeof xt);
855		}
856	}
857	if (!error) {
858		/*
859		 * Give the user an updated idea of our state.
860		 * If the generation differs from what we told
861		 * her before, she knows that something happened
862		 * while we were processing this request, and it
863		 * might be necessary to retry.
864		 */
865		s = splnet();
866		xig.xig_gen = tcbinfo.ipi_gencnt;
867		xig.xig_sogen = so_gencnt;
868		xig.xig_count = tcbinfo.ipi_count;
869		splx(s);
870		error = SYSCTL_OUT(req, &xig, sizeof xig);
871	}
872	free(inp_list, M_TEMP);
873	return error;
874}
875
876SYSCTL_PROC(_net_inet_tcp, TCPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
877	    tcp_pcblist, "S,xtcpcb", "List of active TCP connections");
878
879static int
880tcp_getcred(SYSCTL_HANDLER_ARGS)
881{
882	struct xucred xuc;
883	struct sockaddr_in addrs[2];
884	struct inpcb *inp;
885	int error, s;
886
887	error = suser(req->p);
888	if (error)
889		return (error);
890	error = SYSCTL_IN(req, addrs, sizeof(addrs));
891	if (error)
892		return (error);
893	s = splnet();
894	inp = in_pcblookup_hash(&tcbinfo, addrs[1].sin_addr, addrs[1].sin_port,
895	    addrs[0].sin_addr, addrs[0].sin_port, 0, NULL);
896	if (inp == NULL || inp->inp_socket == NULL) {
897		error = ENOENT;
898		goto out;
899	}
900	bzero(&xuc, sizeof(xuc));
901	xuc.cr_uid = inp->inp_socket->so_cred->cr_uid;
902	xuc.cr_ngroups = inp->inp_socket->so_cred->cr_ngroups;
903	bcopy(inp->inp_socket->so_cred->cr_groups, xuc.cr_groups,
904	    sizeof(xuc.cr_groups));
905	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
906out:
907	splx(s);
908	return (error);
909}
910
911SYSCTL_PROC(_net_inet_tcp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
912    0, 0, tcp_getcred, "S,xucred", "Get the xucred of a TCP connection");
913
914#ifdef INET6
915static int
916tcp6_getcred(SYSCTL_HANDLER_ARGS)
917{
918	struct xucred xuc;
919	struct sockaddr_in6 addrs[2];
920	struct inpcb *inp;
921	int error, s, mapped = 0;
922
923	error = suser(req->p);
924	if (error)
925		return (error);
926	error = SYSCTL_IN(req, addrs, sizeof(addrs));
927	if (error)
928		return (error);
929	if (IN6_IS_ADDR_V4MAPPED(&addrs[0].sin6_addr)) {
930		if (IN6_IS_ADDR_V4MAPPED(&addrs[1].sin6_addr))
931			mapped = 1;
932		else
933			return (EINVAL);
934	}
935	s = splnet();
936	if (mapped == 1)
937		inp = in_pcblookup_hash(&tcbinfo,
938			*(struct in_addr *)&addrs[1].sin6_addr.s6_addr[12],
939			addrs[1].sin6_port,
940			*(struct in_addr *)&addrs[0].sin6_addr.s6_addr[12],
941			addrs[0].sin6_port,
942			0, NULL);
943	else
944		inp = in6_pcblookup_hash(&tcbinfo, &addrs[1].sin6_addr,
945				 addrs[1].sin6_port,
946				 &addrs[0].sin6_addr, addrs[0].sin6_port,
947				 0, NULL);
948	if (inp == NULL || inp->inp_socket == NULL) {
949		error = ENOENT;
950		goto out;
951	}
952	bzero(&xuc, sizeof(xuc));
953	xuc.cr_uid = inp->inp_socket->so_cred->cr_uid;
954	xuc.cr_ngroups = inp->inp_socket->so_cred->cr_ngroups;
955	bcopy(inp->inp_socket->so_cred->cr_groups, xuc.cr_groups,
956	    sizeof(xuc.cr_groups));
957	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
958out:
959	splx(s);
960	return (error);
961}
962
963SYSCTL_PROC(_net_inet6_tcp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
964	    0, 0,
965	    tcp6_getcred, "S,xucred", "Get the xucred of a TCP6 connection");
966#endif
967
968
969void
970tcp_ctlinput(cmd, sa, vip)
971	int cmd;
972	struct sockaddr *sa;
973	void *vip;
974{
975	struct ip *ip = vip;
976	struct tcphdr *th;
977	struct in_addr faddr;
978	struct inpcb *inp;
979	struct tcpcb *tp;
980	void (*notify) __P((struct inpcb *, int)) = tcp_notify;
981	tcp_seq icmp_seq;
982	int s;
983
984	faddr = ((struct sockaddr_in *)sa)->sin_addr;
985	if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
986		return;
987
988	if (cmd == PRC_QUENCH)
989		notify = tcp_quench;
990	else if (icmp_may_rst && cmd == PRC_UNREACH_ADMIN_PROHIB && ip)
991		notify = tcp_drop_syn_sent;
992	else if (cmd == PRC_MSGSIZE)
993		notify = tcp_mtudisc;
994	else if (PRC_IS_REDIRECT(cmd)) {
995		ip = 0;
996		notify = in_rtchange;
997	} else if (cmd == PRC_HOSTDEAD)
998		ip = 0;
999	else if ((unsigned)cmd > PRC_NCMDS || inetctlerrmap[cmd] == 0)
1000		return;
1001	if (ip) {
1002		s = splnet();
1003		th = (struct tcphdr *)((caddr_t)ip
1004				       + (IP_VHL_HL(ip->ip_vhl) << 2));
1005		inp = in_pcblookup_hash(&tcbinfo, faddr, th->th_dport,
1006		    ip->ip_src, th->th_sport, 0, NULL);
1007		if (inp != NULL && inp->inp_socket != NULL) {
1008			icmp_seq = htonl(th->th_seq);
1009			tp = intotcpcb(inp);
1010			if (SEQ_GEQ(icmp_seq, tp->snd_una) &&
1011			    SEQ_LT(icmp_seq, tp->snd_max))
1012				(*notify)(inp, inetctlerrmap[cmd]);
1013		}
1014		splx(s);
1015	} else
1016		in_pcbnotifyall(&tcb, faddr, inetctlerrmap[cmd], notify);
1017}
1018
1019#ifdef INET6
1020void
1021tcp6_ctlinput(cmd, sa, d)
1022	int cmd;
1023	struct sockaddr *sa;
1024	void *d;
1025{
1026	register struct tcphdr *thp;
1027	struct tcphdr th;
1028	void (*notify) __P((struct inpcb *, int)) = tcp_notify;
1029	struct sockaddr_in6 sa6;
1030	struct ip6_hdr *ip6;
1031	struct mbuf *m;
1032	int off;
1033
1034	if (sa->sa_family != AF_INET6 ||
1035	    sa->sa_len != sizeof(struct sockaddr_in6))
1036		return;
1037
1038	if (cmd == PRC_QUENCH)
1039		notify = tcp_quench;
1040	else if (cmd == PRC_MSGSIZE)
1041		notify = tcp_mtudisc;
1042	else if (!PRC_IS_REDIRECT(cmd) &&
1043		 ((unsigned)cmd > PRC_NCMDS || inet6ctlerrmap[cmd] == 0))
1044		return;
1045
1046	/* if the parameter is from icmp6, decode it. */
1047	if (d != NULL) {
1048		struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d;
1049		m = ip6cp->ip6c_m;
1050		ip6 = ip6cp->ip6c_ip6;
1051		off = ip6cp->ip6c_off;
1052	} else {
1053		m = NULL;
1054		ip6 = NULL;
1055		off = 0;	/* fool gcc */
1056	}
1057
1058	/*
1059	 * Translate addresses into internal form.
1060	 * Sa check if it is AF_INET6 is done at the top of this funciton.
1061	 */
1062	sa6 = *(struct sockaddr_in6 *)sa;
1063	if (IN6_IS_ADDR_LINKLOCAL(&sa6.sin6_addr) != 0 && m != NULL &&
1064	    m->m_pkthdr.rcvif != NULL)
1065		sa6.sin6_addr.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
1066
1067	if (ip6) {
1068		/*
1069		 * XXX: We assume that when IPV6 is non NULL,
1070		 * M and OFF are valid.
1071		 */
1072		struct in6_addr s;
1073
1074		/* translate addresses into internal form */
1075		memcpy(&s, &ip6->ip6_src, sizeof(s));
1076		if (IN6_IS_ADDR_LINKLOCAL(&s) != 0 && m != NULL &&
1077		    m->m_pkthdr.rcvif != NULL)
1078			s.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
1079
1080		/* check if we can safely examine src and dst ports */
1081		if (m->m_pkthdr.len < off + sizeof(th))
1082			return;
1083
1084		if (m->m_len < off + sizeof(th)) {
1085			/*
1086			 * this should be rare case
1087			 * because now MINCLSIZE is "(MHLEN + 1)",
1088			 * so we compromise on this copy...
1089			 */
1090			m_copydata(m, off, sizeof(th), (caddr_t)&th);
1091			thp = &th;
1092		} else
1093			thp = (struct tcphdr *)(mtod(m, caddr_t) + off);
1094		in6_pcbnotify(&tcb, (struct sockaddr *)&sa6, thp->th_dport,
1095			      &s, thp->th_sport, cmd, notify);
1096	} else
1097		in6_pcbnotify(&tcb, (struct sockaddr *)&sa6, 0, &zeroin6_addr,
1098			      0, cmd, notify);
1099}
1100#endif /* INET6 */
1101
1102/*
1103 * When a source quench is received, close congestion window
1104 * to one segment.  We will gradually open it again as we proceed.
1105 */
1106void
1107tcp_quench(inp, errno)
1108	struct inpcb *inp;
1109	int errno;
1110{
1111	struct tcpcb *tp = intotcpcb(inp);
1112
1113	if (tp)
1114		tp->snd_cwnd = tp->t_maxseg;
1115}
1116
1117/*
1118 * When a specific ICMP unreachable message is received and the
1119 * connection state is SYN-SENT, drop the connection.  This behavior
1120 * is controlled by the icmp_may_rst sysctl.
1121 */
1122void
1123tcp_drop_syn_sent(inp, errno)
1124	struct inpcb *inp;
1125	int errno;
1126{
1127	struct tcpcb *tp = intotcpcb(inp);
1128
1129	if (tp && tp->t_state == TCPS_SYN_SENT)
1130		tcp_drop(tp, errno);
1131}
1132
1133/*
1134 * When `need fragmentation' ICMP is received, update our idea of the MSS
1135 * based on the new value in the route.  Also nudge TCP to send something,
1136 * since we know the packet we just sent was dropped.
1137 * This duplicates some code in the tcp_mss() function in tcp_input.c.
1138 */
1139void
1140tcp_mtudisc(inp, errno)
1141	struct inpcb *inp;
1142	int errno;
1143{
1144	struct tcpcb *tp = intotcpcb(inp);
1145	struct rtentry *rt;
1146	struct rmxp_tao *taop;
1147	struct socket *so = inp->inp_socket;
1148	int offered;
1149	int mss;
1150#ifdef INET6
1151	int isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
1152#endif /* INET6 */
1153
1154	if (tp) {
1155#ifdef INET6
1156		if (isipv6)
1157			rt = tcp_rtlookup6(inp);
1158		else
1159#endif /* INET6 */
1160		rt = tcp_rtlookup(inp);
1161		if (!rt || !rt->rt_rmx.rmx_mtu) {
1162			tp->t_maxopd = tp->t_maxseg =
1163#ifdef INET6
1164				isipv6 ? tcp_v6mssdflt :
1165#endif /* INET6 */
1166				tcp_mssdflt;
1167			return;
1168		}
1169		taop = rmx_taop(rt->rt_rmx);
1170		offered = taop->tao_mssopt;
1171		mss = rt->rt_rmx.rmx_mtu -
1172#ifdef INET6
1173			(isipv6 ?
1174			 sizeof(struct ip6_hdr) + sizeof(struct tcphdr) :
1175#endif /* INET6 */
1176			 sizeof(struct tcpiphdr)
1177#ifdef INET6
1178			 )
1179#endif /* INET6 */
1180			;
1181
1182		if (offered)
1183			mss = min(mss, offered);
1184		/*
1185		 * XXX - The above conditional probably violates the TCP
1186		 * spec.  The problem is that, since we don't know the
1187		 * other end's MSS, we are supposed to use a conservative
1188		 * default.  But, if we do that, then MTU discovery will
1189		 * never actually take place, because the conservative
1190		 * default is much less than the MTUs typically seen
1191		 * on the Internet today.  For the moment, we'll sweep
1192		 * this under the carpet.
1193		 *
1194		 * The conservative default might not actually be a problem
1195		 * if the only case this occurs is when sending an initial
1196		 * SYN with options and data to a host we've never talked
1197		 * to before.  Then, they will reply with an MSS value which
1198		 * will get recorded and the new parameters should get
1199		 * recomputed.  For Further Study.
1200		 */
1201		if (tp->t_maxopd <= mss)
1202			return;
1203		tp->t_maxopd = mss;
1204
1205		if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
1206		    (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)
1207			mss -= TCPOLEN_TSTAMP_APPA;
1208		if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
1209		    (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC)
1210			mss -= TCPOLEN_CC_APPA;
1211#if	(MCLBYTES & (MCLBYTES - 1)) == 0
1212		if (mss > MCLBYTES)
1213			mss &= ~(MCLBYTES-1);
1214#else
1215		if (mss > MCLBYTES)
1216			mss = mss / MCLBYTES * MCLBYTES;
1217#endif
1218		if (so->so_snd.sb_hiwat < mss)
1219			mss = so->so_snd.sb_hiwat;
1220
1221		tp->t_maxseg = mss;
1222
1223		tcpstat.tcps_mturesent++;
1224		tp->t_rtttime = 0;
1225		tp->snd_nxt = tp->snd_una;
1226		tcp_output(tp);
1227	}
1228}
1229
1230/*
1231 * Look-up the routing entry to the peer of this inpcb.  If no route
1232 * is found and it cannot be allocated the return NULL.  This routine
1233 * is called by TCP routines that access the rmx structure and by tcp_mss
1234 * to get the interface MTU.
1235 */
1236struct rtentry *
1237tcp_rtlookup(inp)
1238	struct inpcb *inp;
1239{
1240	struct route *ro;
1241	struct rtentry *rt;
1242
1243	ro = &inp->inp_route;
1244	rt = ro->ro_rt;
1245	if (rt == NULL || !(rt->rt_flags & RTF_UP)) {
1246		/* No route yet, so try to acquire one */
1247		if (inp->inp_faddr.s_addr != INADDR_ANY) {
1248			ro->ro_dst.sa_family = AF_INET;
1249			ro->ro_dst.sa_len = sizeof(ro->ro_dst);
1250			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
1251				inp->inp_faddr;
1252			rtalloc(ro);
1253			rt = ro->ro_rt;
1254		}
1255	}
1256	return rt;
1257}
1258
1259#ifdef INET6
1260struct rtentry *
1261tcp_rtlookup6(inp)
1262	struct inpcb *inp;
1263{
1264	struct route_in6 *ro6;
1265	struct rtentry *rt;
1266
1267	ro6 = &inp->in6p_route;
1268	rt = ro6->ro_rt;
1269	if (rt == NULL || !(rt->rt_flags & RTF_UP)) {
1270		/* No route yet, so try to acquire one */
1271		if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1272			ro6->ro_dst.sin6_family = AF_INET6;
1273			ro6->ro_dst.sin6_len = sizeof(ro6->ro_dst);
1274			ro6->ro_dst.sin6_addr = inp->in6p_faddr;
1275			rtalloc((struct route *)ro6);
1276			rt = ro6->ro_rt;
1277		}
1278	}
1279	return rt;
1280}
1281#endif /* INET6 */
1282
1283#ifdef IPSEC
1284/* compute ESP/AH header size for TCP, including outer IP header. */
1285size_t
1286ipsec_hdrsiz_tcp(tp)
1287	struct tcpcb *tp;
1288{
1289	struct inpcb *inp;
1290	struct mbuf *m;
1291	size_t hdrsiz;
1292	struct ip *ip;
1293#ifdef INET6
1294	struct ip6_hdr *ip6;
1295#endif /* INET6 */
1296	struct tcphdr *th;
1297
1298	if (!tp || !tp->t_template || !(inp = tp->t_inpcb))
1299		return 0;
1300	MGETHDR(m, M_DONTWAIT, MT_DATA);
1301	if (!m)
1302		return 0;
1303
1304#ifdef INET6
1305	if ((inp->inp_vflag & INP_IPV6) != 0) {
1306		ip6 = mtod(m, struct ip6_hdr *);
1307		th = (struct tcphdr *)(ip6 + 1);
1308		m->m_pkthdr.len = m->m_len =
1309			sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
1310		bcopy((caddr_t)tp->t_template->tt_ipgen, (caddr_t)ip6,
1311		      sizeof(struct ip6_hdr));
1312		bcopy((caddr_t)&tp->t_template->tt_t, (caddr_t)th,
1313		      sizeof(struct tcphdr));
1314		hdrsiz = ipsec6_hdrsiz(m, IPSEC_DIR_OUTBOUND, inp);
1315	} else
1316#endif /* INET6 */
1317      {
1318	ip = mtod(m, struct ip *);
1319	th = (struct tcphdr *)(ip + 1);
1320	m->m_pkthdr.len = m->m_len = sizeof(struct tcpiphdr);
1321	bcopy((caddr_t)tp->t_template->tt_ipgen, (caddr_t)ip,
1322	      sizeof(struct ip));
1323	bcopy((caddr_t)&tp->t_template->tt_t, (caddr_t)th,
1324	      sizeof(struct tcphdr));
1325	hdrsiz = ipsec4_hdrsiz(m, IPSEC_DIR_OUTBOUND, inp);
1326      }
1327
1328	m_free(m);
1329	return hdrsiz;
1330}
1331#endif /*IPSEC*/
1332
1333/*
1334 * Return a pointer to the cached information about the remote host.
1335 * The cached information is stored in the protocol specific part of
1336 * the route metrics.
1337 */
1338struct rmxp_tao *
1339tcp_gettaocache(inp)
1340	struct inpcb *inp;
1341{
1342	struct rtentry *rt;
1343
1344#ifdef INET6
1345	if ((inp->inp_vflag & INP_IPV6) != 0)
1346		rt = tcp_rtlookup6(inp);
1347	else
1348#endif /* INET6 */
1349	rt = tcp_rtlookup(inp);
1350
1351	/* Make sure this is a host route and is up. */
1352	if (rt == NULL ||
1353	    (rt->rt_flags & (RTF_UP|RTF_HOST)) != (RTF_UP|RTF_HOST))
1354		return NULL;
1355
1356	return rmx_taop(rt->rt_rmx);
1357}
1358
1359/*
1360 * Clear all the TAO cache entries, called from tcp_init.
1361 *
1362 * XXX
1363 * This routine is just an empty one, because we assume that the routing
1364 * routing tables are initialized at the same time when TCP, so there is
1365 * nothing in the cache left over.
1366 */
1367static void
1368tcp_cleartaocache()
1369{
1370}
1371