tcp_timewait.c revision 78100
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 78064 2001-06-11 12:39:29Z ume $
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#include <sys/random.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(_net_inet_tcp, OID_AUTO, do_tcpdrain, CTLFLAG_RW, &do_tcpdrain, 0,
133     "Enable tcp_drain routine for extra help when low on mbufs");
134
135SYSCTL_INT(_net_inet_tcp, OID_AUTO, pcbcount, CTLFLAG_RD,
136    &tcbinfo.ipi_count, 0, "Number of active PCBs");
137
138static int	icmp_may_rst = 1;
139SYSCTL_INT(_net_inet_tcp, OID_AUTO, icmp_may_rst, CTLFLAG_RW, &icmp_may_rst, 0,
140    "Certain ICMP unreachable messages may abort connections in SYN_SENT");
141
142static void	tcp_cleartaocache __P((void));
143static void	tcp_notify __P((struct inpcb *, int));
144
145/*
146 * Target size of TCP PCB hash tables. Must be a power of two.
147 *
148 * Note that this can be overridden by the kernel environment
149 * variable net.inet.tcp.tcbhashsize
150 */
151#ifndef TCBHASHSIZE
152#define TCBHASHSIZE	512
153#endif
154
155/*
156 * This is the actual shape of what we allocate using the zone
157 * allocator.  Doing it this way allows us to protect both structures
158 * using the same generation count, and also eliminates the overhead
159 * of allocating tcpcbs separately.  By hiding the structure here,
160 * we avoid changing most of the rest of the code (although it needs
161 * to be changed, eventually, for greater efficiency).
162 */
163#define	ALIGNMENT	32
164#define	ALIGNM1		(ALIGNMENT - 1)
165struct	inp_tp {
166	union {
167		struct	inpcb inp;
168		char	align[(sizeof(struct inpcb) + ALIGNM1) & ~ALIGNM1];
169	} inp_tp_u;
170	struct	tcpcb tcb;
171	struct	callout inp_tp_rexmt, inp_tp_persist, inp_tp_keep, inp_tp_2msl;
172	struct	callout inp_tp_delack;
173};
174#undef ALIGNMENT
175#undef ALIGNM1
176
177/*
178 * Tcp initialization
179 */
180void
181tcp_init()
182{
183	int hashsize = TCBHASHSIZE;
184
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", &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		tlen = 0;
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	if (ipsec_setsocket(m, tp ? tp->t_inpcb->inp_socket : NULL) != 0) {
447		m_freem(m);
448		return;
449	}
450#endif
451#ifdef INET6
452	if (isipv6) {
453		(void)ip6_output(m, NULL, ro6, ipflags, NULL, NULL);
454		if (ro6 == &sro6 && ro6->ro_rt) {
455			RTFREE(ro6->ro_rt);
456			ro6->ro_rt = NULL;
457		}
458	} else
459#endif /* INET6 */
460      {
461	(void) ip_output(m, NULL, ro, ipflags, NULL);
462	if (ro == &sro && ro->ro_rt) {
463		RTFREE(ro->ro_rt);
464		ro->ro_rt = NULL;
465	}
466      }
467}
468
469/*
470 * Create a new TCP control block, making an
471 * empty reassembly queue and hooking it to the argument
472 * protocol control block.  The `inp' parameter must have
473 * come from the zone allocator set up in tcp_init().
474 */
475struct tcpcb *
476tcp_newtcpcb(inp)
477	struct inpcb *inp;
478{
479	struct inp_tp *it;
480	register struct tcpcb *tp;
481#ifdef INET6
482	int isipv6 = (inp->inp_vflag & INP_IPV6) != 0;
483#endif /* INET6 */
484
485	it = (struct inp_tp *)inp;
486	tp = &it->tcb;
487	bzero((char *) tp, sizeof(struct tcpcb));
488	LIST_INIT(&tp->t_segq);
489	tp->t_maxseg = tp->t_maxopd =
490#ifdef INET6
491		isipv6 ? tcp_v6mssdflt :
492#endif /* INET6 */
493		tcp_mssdflt;
494
495	/* Set up our timeouts. */
496	callout_init(tp->tt_rexmt = &it->inp_tp_rexmt, 0);
497	callout_init(tp->tt_persist = &it->inp_tp_persist, 0);
498	callout_init(tp->tt_keep = &it->inp_tp_keep, 0);
499	callout_init(tp->tt_2msl = &it->inp_tp_2msl, 0);
500	callout_init(tp->tt_delack = &it->inp_tp_delack, 0);
501
502	if (tcp_do_rfc1323)
503		tp->t_flags = (TF_REQ_SCALE|TF_REQ_TSTMP);
504	if (tcp_do_rfc1644)
505		tp->t_flags |= TF_REQ_CC;
506	tp->t_inpcb = inp;	/* XXX */
507	/*
508	 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
509	 * rtt estimate.  Set rttvar so that srtt + 4 * rttvar gives
510	 * reasonable initial retransmit time.
511	 */
512	tp->t_srtt = TCPTV_SRTTBASE;
513	tp->t_rttvar = ((TCPTV_RTOBASE - TCPTV_SRTTBASE) << TCP_RTTVAR_SHIFT) / 4;
514	tp->t_rttmin = TCPTV_MIN;
515	tp->t_rxtcur = TCPTV_RTOBASE;
516	tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
517	tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
518	tp->t_rcvtime = ticks;
519        /*
520	 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
521	 * because the socket may be bound to an IPv6 wildcard address,
522	 * which may match an IPv4-mapped IPv6 address.
523	 */
524	inp->inp_ip_ttl = ip_defttl;
525	inp->inp_ppcb = (caddr_t)tp;
526	return (tp);		/* XXX */
527}
528
529/*
530 * Drop a TCP connection, reporting
531 * the specified error.  If connection is synchronized,
532 * then send a RST to peer.
533 */
534struct tcpcb *
535tcp_drop(tp, errno)
536	register struct tcpcb *tp;
537	int errno;
538{
539	struct socket *so = tp->t_inpcb->inp_socket;
540
541	if (TCPS_HAVERCVDSYN(tp->t_state)) {
542		tp->t_state = TCPS_CLOSED;
543		(void) tcp_output(tp);
544		tcpstat.tcps_drops++;
545	} else
546		tcpstat.tcps_conndrops++;
547	if (errno == ETIMEDOUT && tp->t_softerror)
548		errno = tp->t_softerror;
549	so->so_error = errno;
550	return (tcp_close(tp));
551}
552
553/*
554 * Close a TCP control block:
555 *	discard all space held by the tcp
556 *	discard internet protocol block
557 *	wake up any sleepers
558 */
559struct tcpcb *
560tcp_close(tp)
561	register struct tcpcb *tp;
562{
563	register struct tseg_qent *q;
564	struct inpcb *inp = tp->t_inpcb;
565	struct socket *so = inp->inp_socket;
566#ifdef INET6
567	int isipv6 = (inp->inp_vflag & INP_IPV6) != 0;
568#endif /* INET6 */
569	register struct rtentry *rt;
570	int dosavessthresh;
571
572	/*
573	 * Make sure that all of our timers are stopped before we
574	 * delete the PCB.
575	 */
576	callout_stop(tp->tt_rexmt);
577	callout_stop(tp->tt_persist);
578	callout_stop(tp->tt_keep);
579	callout_stop(tp->tt_2msl);
580	callout_stop(tp->tt_delack);
581
582	/*
583	 * If we got enough samples through the srtt filter,
584	 * save the rtt and rttvar in the routing entry.
585	 * 'Enough' is arbitrarily defined as the 16 samples.
586	 * 16 samples is enough for the srtt filter to converge
587	 * to within 5% of the correct value; fewer samples and
588	 * we could save a very bogus rtt.
589	 *
590	 * Don't update the default route's characteristics and don't
591	 * update anything that the user "locked".
592	 */
593	if (tp->t_rttupdated >= 16) {
594		register u_long i = 0;
595#ifdef INET6
596		if (isipv6) {
597			struct sockaddr_in6 *sin6;
598
599			if ((rt = inp->in6p_route.ro_rt) == NULL)
600				goto no_valid_rt;
601			sin6 = (struct sockaddr_in6 *)rt_key(rt);
602			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
603				goto no_valid_rt;
604		}
605		else
606#endif /* INET6 */
607		if ((rt = inp->inp_route.ro_rt) == NULL ||
608		    ((struct sockaddr_in *)rt_key(rt))->sin_addr.s_addr
609		    == INADDR_ANY)
610			goto no_valid_rt;
611
612		if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) {
613			i = tp->t_srtt *
614			    (RTM_RTTUNIT / (hz * TCP_RTT_SCALE));
615			if (rt->rt_rmx.rmx_rtt && i)
616				/*
617				 * filter this update to half the old & half
618				 * the new values, converting scale.
619				 * See route.h and tcp_var.h for a
620				 * description of the scaling constants.
621				 */
622				rt->rt_rmx.rmx_rtt =
623				    (rt->rt_rmx.rmx_rtt + i) / 2;
624			else
625				rt->rt_rmx.rmx_rtt = i;
626			tcpstat.tcps_cachedrtt++;
627		}
628		if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) {
629			i = tp->t_rttvar *
630			    (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE));
631			if (rt->rt_rmx.rmx_rttvar && i)
632				rt->rt_rmx.rmx_rttvar =
633				    (rt->rt_rmx.rmx_rttvar + i) / 2;
634			else
635				rt->rt_rmx.rmx_rttvar = i;
636			tcpstat.tcps_cachedrttvar++;
637		}
638		/*
639		 * The old comment here said:
640		 * update the pipelimit (ssthresh) if it has been updated
641		 * already or if a pipesize was specified & the threshhold
642		 * got below half the pipesize.  I.e., wait for bad news
643		 * before we start updating, then update on both good
644		 * and bad news.
645		 *
646		 * But we want to save the ssthresh even if no pipesize is
647		 * specified explicitly in the route, because such
648		 * connections still have an implicit pipesize specified
649		 * by the global tcp_sendspace.  In the absence of a reliable
650		 * way to calculate the pipesize, it will have to do.
651		 */
652		i = tp->snd_ssthresh;
653		if (rt->rt_rmx.rmx_sendpipe != 0)
654			dosavessthresh = (i < rt->rt_rmx.rmx_sendpipe / 2);
655		else
656			dosavessthresh = (i < so->so_snd.sb_hiwat / 2);
657		if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 &&
658		     i != 0 && rt->rt_rmx.rmx_ssthresh != 0)
659		    || dosavessthresh) {
660			/*
661			 * convert the limit from user data bytes to
662			 * packets then to packet data bytes.
663			 */
664			i = (i + tp->t_maxseg / 2) / tp->t_maxseg;
665			if (i < 2)
666				i = 2;
667			i *= (u_long)(tp->t_maxseg +
668#ifdef INET6
669				      (isipv6 ? sizeof (struct ip6_hdr) +
670					       sizeof (struct tcphdr) :
671#endif
672				       sizeof (struct tcpiphdr)
673#ifdef INET6
674				       )
675#endif
676				      );
677			if (rt->rt_rmx.rmx_ssthresh)
678				rt->rt_rmx.rmx_ssthresh =
679				    (rt->rt_rmx.rmx_ssthresh + i) / 2;
680			else
681				rt->rt_rmx.rmx_ssthresh = i;
682			tcpstat.tcps_cachedssthresh++;
683		}
684	}
685	rt = inp->inp_route.ro_rt;
686	if (rt) {
687		/*
688		 * mark route for deletion if no information is
689		 * cached.
690		 */
691		if ((tp->t_flags & TF_LQ_OVERFLOW) &&
692		    ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0)){
693			if (rt->rt_rmx.rmx_rtt == 0)
694				rt->rt_flags |= RTF_DELCLONE;
695		}
696	}
697    no_valid_rt:
698	/* free the reassembly queue, if any */
699	while((q = LIST_FIRST(&tp->t_segq)) != NULL) {
700		LIST_REMOVE(q, tqe_q);
701		m_freem(q->tqe_m);
702		FREE(q, M_TSEGQ);
703	}
704	if (tp->t_template)
705		(void) m_free(dtom(tp->t_template));
706	inp->inp_ppcb = NULL;
707	soisdisconnected(so);
708#ifdef INET6
709	if (INP_CHECK_SOCKAF(so, AF_INET6))
710		in6_pcbdetach(inp);
711	else
712#endif /* INET6 */
713	in_pcbdetach(inp);
714	tcpstat.tcps_closed++;
715	return ((struct tcpcb *)0);
716}
717
718void
719tcp_drain()
720{
721	if (do_tcpdrain)
722	{
723		struct inpcb *inpb;
724		struct tcpcb *tcpb;
725		struct tseg_qent *te;
726
727	/*
728	 * Walk the tcpbs, if existing, and flush the reassembly queue,
729	 * if there is one...
730	 * XXX: The "Net/3" implementation doesn't imply that the TCP
731	 *      reassembly queue should be flushed, but in a situation
732	 * 	where we're really low on mbufs, this is potentially
733	 *  	usefull.
734	 */
735		LIST_FOREACH(inpb, tcbinfo.listhead, inp_list) {
736			if ((tcpb = intotcpcb(inpb))) {
737				while ((te = LIST_FIRST(&tcpb->t_segq))
738			            != NULL) {
739					LIST_REMOVE(te, tqe_q);
740					m_freem(te->tqe_m);
741					FREE(te, M_TSEGQ);
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 *
753 * Do not wake up user since there currently is no mechanism for
754 * reporting soft errors (yet - a kqueue filter may be added).
755 */
756static void
757tcp_notify(inp, error)
758	struct inpcb *inp;
759	int error;
760{
761	struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb;
762
763	/*
764	 * Ignore some errors if we are hooked up.
765	 * If connection hasn't completed, has retransmitted several times,
766	 * and receives a second error, give up now.  This is better
767	 * than waiting a long time to establish a connection that
768	 * can never complete.
769	 */
770	if (tp->t_state == TCPS_ESTABLISHED &&
771	     (error == EHOSTUNREACH || error == ENETUNREACH ||
772	      error == EHOSTDOWN)) {
773		return;
774	} else if (tp->t_state < TCPS_ESTABLISHED && tp->t_rxtshift > 3 &&
775	    tp->t_softerror)
776		tcp_drop(tp, error);
777	else
778		tp->t_softerror = error;
779#if 0
780	wakeup((caddr_t) &so->so_timeo);
781	sorwakeup(so);
782	sowwakeup(so);
783#endif
784}
785
786static int
787tcp_pcblist(SYSCTL_HANDLER_ARGS)
788{
789	int error, i, n, s;
790	struct inpcb *inp, **inp_list;
791	inp_gen_t gencnt;
792	struct xinpgen xig;
793
794	/*
795	 * The process of preparing the TCB list is too time-consuming and
796	 * resource-intensive to repeat twice on every request.
797	 */
798	if (req->oldptr == 0) {
799		n = tcbinfo.ipi_count;
800		req->oldidx = 2 * (sizeof xig)
801			+ (n + n/8) * sizeof(struct xtcpcb);
802		return 0;
803	}
804
805	if (req->newptr != 0)
806		return EPERM;
807
808	/*
809	 * OK, now we're committed to doing something.
810	 */
811	s = splnet();
812	gencnt = tcbinfo.ipi_gencnt;
813	n = tcbinfo.ipi_count;
814	splx(s);
815
816	xig.xig_len = sizeof xig;
817	xig.xig_count = n;
818	xig.xig_gen = gencnt;
819	xig.xig_sogen = so_gencnt;
820	error = SYSCTL_OUT(req, &xig, sizeof xig);
821	if (error)
822		return error;
823
824	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
825	if (inp_list == 0)
826		return ENOMEM;
827
828	s = splnet();
829	for (inp = LIST_FIRST(tcbinfo.listhead), i = 0; inp && i < n;
830	     inp = LIST_NEXT(inp, inp_list)) {
831		if (inp->inp_gencnt <= gencnt && !prison_xinpcb(req->p, inp))
832			inp_list[i++] = inp;
833	}
834	splx(s);
835	n = i;
836
837	error = 0;
838	for (i = 0; i < n; i++) {
839		inp = inp_list[i];
840		if (inp->inp_gencnt <= gencnt) {
841			struct xtcpcb xt;
842			caddr_t inp_ppcb;
843			xt.xt_len = sizeof xt;
844			/* XXX should avoid extra copy */
845			bcopy(inp, &xt.xt_inp, sizeof *inp);
846			inp_ppcb = inp->inp_ppcb;
847			if (inp_ppcb != NULL)
848				bcopy(inp_ppcb, &xt.xt_tp, sizeof xt.xt_tp);
849			else
850				bzero((char *) &xt.xt_tp, sizeof xt.xt_tp);
851			if (inp->inp_socket)
852				sotoxsocket(inp->inp_socket, &xt.xt_socket);
853			error = SYSCTL_OUT(req, &xt, sizeof xt);
854		}
855	}
856	if (!error) {
857		/*
858		 * Give the user an updated idea of our state.
859		 * If the generation differs from what we told
860		 * her before, she knows that something happened
861		 * while we were processing this request, and it
862		 * might be necessary to retry.
863		 */
864		s = splnet();
865		xig.xig_gen = tcbinfo.ipi_gencnt;
866		xig.xig_sogen = so_gencnt;
867		xig.xig_count = tcbinfo.ipi_count;
868		splx(s);
869		error = SYSCTL_OUT(req, &xig, sizeof xig);
870	}
871	free(inp_list, M_TEMP);
872	return error;
873}
874
875SYSCTL_PROC(_net_inet_tcp, TCPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
876	    tcp_pcblist, "S,xtcpcb", "List of active TCP connections");
877
878static int
879tcp_getcred(SYSCTL_HANDLER_ARGS)
880{
881	struct xucred xuc;
882	struct sockaddr_in addrs[2];
883	struct inpcb *inp;
884	int error, s;
885
886	error = suser(req->p);
887	if (error)
888		return (error);
889	error = SYSCTL_IN(req, addrs, sizeof(addrs));
890	if (error)
891		return (error);
892	s = splnet();
893	inp = in_pcblookup_hash(&tcbinfo, addrs[1].sin_addr, addrs[1].sin_port,
894	    addrs[0].sin_addr, addrs[0].sin_port, 0, NULL);
895	if (inp == NULL || inp->inp_socket == NULL) {
896		error = ENOENT;
897		goto out;
898	}
899	bzero(&xuc, sizeof(xuc));
900	xuc.cr_uid = inp->inp_socket->so_cred->cr_uid;
901	xuc.cr_ngroups = inp->inp_socket->so_cred->cr_ngroups;
902	bcopy(inp->inp_socket->so_cred->cr_groups, xuc.cr_groups,
903	    sizeof(xuc.cr_groups));
904	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
905out:
906	splx(s);
907	return (error);
908}
909
910SYSCTL_PROC(_net_inet_tcp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
911    0, 0, tcp_getcred, "S,xucred", "Get the xucred of a TCP connection");
912
913#ifdef INET6
914static int
915tcp6_getcred(SYSCTL_HANDLER_ARGS)
916{
917	struct xucred xuc;
918	struct sockaddr_in6 addrs[2];
919	struct inpcb *inp;
920	int error, s, mapped = 0;
921
922	error = suser(req->p);
923	if (error)
924		return (error);
925	error = SYSCTL_IN(req, addrs, sizeof(addrs));
926	if (error)
927		return (error);
928	if (IN6_IS_ADDR_V4MAPPED(&addrs[0].sin6_addr)) {
929		if (IN6_IS_ADDR_V4MAPPED(&addrs[1].sin6_addr))
930			mapped = 1;
931		else
932			return (EINVAL);
933	}
934	s = splnet();
935	if (mapped == 1)
936		inp = in_pcblookup_hash(&tcbinfo,
937			*(struct in_addr *)&addrs[1].sin6_addr.s6_addr[12],
938			addrs[1].sin6_port,
939			*(struct in_addr *)&addrs[0].sin6_addr.s6_addr[12],
940			addrs[0].sin6_port,
941			0, NULL);
942	else
943		inp = in6_pcblookup_hash(&tcbinfo, &addrs[1].sin6_addr,
944				 addrs[1].sin6_port,
945				 &addrs[0].sin6_addr, addrs[0].sin6_port,
946				 0, NULL);
947	if (inp == NULL || inp->inp_socket == NULL) {
948		error = ENOENT;
949		goto out;
950	}
951	bzero(&xuc, sizeof(xuc));
952	xuc.cr_uid = inp->inp_socket->so_cred->cr_uid;
953	xuc.cr_ngroups = inp->inp_socket->so_cred->cr_ngroups;
954	bcopy(inp->inp_socket->so_cred->cr_groups, xuc.cr_groups,
955	    sizeof(xuc.cr_groups));
956	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
957out:
958	splx(s);
959	return (error);
960}
961
962SYSCTL_PROC(_net_inet6_tcp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
963	    0, 0,
964	    tcp6_getcred, "S,xucred", "Get the xucred of a TCP6 connection");
965#endif
966
967
968void
969tcp_ctlinput(cmd, sa, vip)
970	int cmd;
971	struct sockaddr *sa;
972	void *vip;
973{
974	struct ip *ip = vip;
975	struct tcphdr *th;
976	struct in_addr faddr;
977	struct inpcb *inp;
978	struct tcpcb *tp;
979	void (*notify) __P((struct inpcb *, int)) = tcp_notify;
980	tcp_seq icmp_seq;
981	int s;
982
983	faddr = ((struct sockaddr_in *)sa)->sin_addr;
984	if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
985		return;
986
987	if (cmd == PRC_QUENCH)
988		notify = tcp_quench;
989	else if (icmp_may_rst && (cmd == PRC_UNREACH_ADMIN_PROHIB ||
990		cmd == PRC_UNREACH_PORT) && 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	struct tcphdr th;
1027	void (*notify) __P((struct inpcb *, int)) = tcp_notify;
1028	struct ip6_hdr *ip6;
1029	struct mbuf *m;
1030	struct ip6ctlparam *ip6cp = NULL;
1031	const struct sockaddr_in6 *sa6_src = NULL;
1032	int off;
1033	struct tcp_portonly {
1034		u_int16_t th_sport;
1035		u_int16_t th_dport;
1036	} *thp;
1037
1038	if (sa->sa_family != AF_INET6 ||
1039	    sa->sa_len != sizeof(struct sockaddr_in6))
1040		return;
1041
1042	if (cmd == PRC_QUENCH)
1043		notify = tcp_quench;
1044	else if (cmd == PRC_MSGSIZE)
1045		notify = tcp_mtudisc;
1046	else if (!PRC_IS_REDIRECT(cmd) &&
1047		 ((unsigned)cmd > PRC_NCMDS || inet6ctlerrmap[cmd] == 0))
1048		return;
1049
1050	/* if the parameter is from icmp6, decode it. */
1051	if (d != NULL) {
1052		ip6cp = (struct ip6ctlparam *)d;
1053		m = ip6cp->ip6c_m;
1054		ip6 = ip6cp->ip6c_ip6;
1055		off = ip6cp->ip6c_off;
1056		sa6_src = ip6cp->ip6c_src;
1057	} else {
1058		m = NULL;
1059		ip6 = NULL;
1060		off = 0;	/* fool gcc */
1061		sa6_src = &sa6_any;
1062	}
1063
1064	if (ip6) {
1065		/*
1066		 * XXX: We assume that when IPV6 is non NULL,
1067		 * M and OFF are valid.
1068		 */
1069
1070		/* check if we can safely examine src and dst ports */
1071		if (m->m_pkthdr.len < off + sizeof(*thp))
1072			return;
1073
1074		bzero(&th, sizeof(th));
1075		m_copydata(m, off, sizeof(*thp), (caddr_t)&th);
1076
1077		in6_pcbnotify(&tcb, sa, th.th_dport,
1078		    (struct sockaddr *)ip6cp->ip6c_src,
1079		    th.th_sport, cmd, notify);
1080	} else
1081		in6_pcbnotify(&tcb, sa, 0, (struct sockaddr *)sa6_src,
1082			      0, cmd, notify);
1083}
1084#endif /* INET6 */
1085
1086#define TCP_RNDISS_ROUNDS	16
1087#define TCP_RNDISS_OUT	7200
1088#define TCP_RNDISS_MAX	30000
1089
1090u_int8_t tcp_rndiss_sbox[128];
1091u_int16_t tcp_rndiss_msb;
1092u_int16_t tcp_rndiss_cnt;
1093long tcp_rndiss_reseed;
1094
1095u_int16_t
1096tcp_rndiss_encrypt(val)
1097	u_int16_t val;
1098{
1099	u_int16_t sum = 0, i;
1100
1101	for (i = 0; i < TCP_RNDISS_ROUNDS; i++) {
1102		sum += 0x79b9;
1103		val ^= ((u_int16_t)tcp_rndiss_sbox[(val^sum) & 0x7f]) << 7;
1104		val = ((val & 0xff) << 7) | (val >> 8);
1105	}
1106
1107	return val;
1108}
1109
1110void
1111tcp_rndiss_init()
1112{
1113	struct timeval time;
1114
1115	getmicrotime(&time);
1116	read_random(tcp_rndiss_sbox, sizeof(tcp_rndiss_sbox));
1117
1118	tcp_rndiss_reseed = time.tv_sec + TCP_RNDISS_OUT;
1119	tcp_rndiss_msb = tcp_rndiss_msb == 0x8000 ? 0 : 0x8000;
1120	tcp_rndiss_cnt = 0;
1121}
1122
1123tcp_seq
1124tcp_rndiss_next()
1125{
1126	u_int16_t tmp;
1127	struct timeval time;
1128
1129	getmicrotime(&time);
1130
1131        if (tcp_rndiss_cnt >= TCP_RNDISS_MAX ||
1132	    time.tv_sec > tcp_rndiss_reseed)
1133                tcp_rndiss_init();
1134
1135	read_random(&tmp, sizeof(tmp));
1136
1137	/* (tmp & 0x7fff) ensures a 32768 byte gap between ISS */
1138	return ((tcp_rndiss_encrypt(tcp_rndiss_cnt++) | tcp_rndiss_msb) <<16) |
1139		(tmp & 0x7fff);
1140}
1141
1142
1143/*
1144 * When a source quench is received, close congestion window
1145 * to one segment.  We will gradually open it again as we proceed.
1146 */
1147void
1148tcp_quench(inp, errno)
1149	struct inpcb *inp;
1150	int errno;
1151{
1152	struct tcpcb *tp = intotcpcb(inp);
1153
1154	if (tp)
1155		tp->snd_cwnd = tp->t_maxseg;
1156}
1157
1158/*
1159 * When a specific ICMP unreachable message is received and the
1160 * connection state is SYN-SENT, drop the connection.  This behavior
1161 * is controlled by the icmp_may_rst sysctl.
1162 */
1163void
1164tcp_drop_syn_sent(inp, errno)
1165	struct inpcb *inp;
1166	int errno;
1167{
1168	struct tcpcb *tp = intotcpcb(inp);
1169
1170	if (tp && tp->t_state == TCPS_SYN_SENT)
1171		tcp_drop(tp, errno);
1172}
1173
1174/*
1175 * When `need fragmentation' ICMP is received, update our idea of the MSS
1176 * based on the new value in the route.  Also nudge TCP to send something,
1177 * since we know the packet we just sent was dropped.
1178 * This duplicates some code in the tcp_mss() function in tcp_input.c.
1179 */
1180void
1181tcp_mtudisc(inp, errno)
1182	struct inpcb *inp;
1183	int errno;
1184{
1185	struct tcpcb *tp = intotcpcb(inp);
1186	struct rtentry *rt;
1187	struct rmxp_tao *taop;
1188	struct socket *so = inp->inp_socket;
1189	int offered;
1190	int mss;
1191#ifdef INET6
1192	int isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
1193#endif /* INET6 */
1194
1195	if (tp) {
1196#ifdef INET6
1197		if (isipv6)
1198			rt = tcp_rtlookup6(inp);
1199		else
1200#endif /* INET6 */
1201		rt = tcp_rtlookup(inp);
1202		if (!rt || !rt->rt_rmx.rmx_mtu) {
1203			tp->t_maxopd = tp->t_maxseg =
1204#ifdef INET6
1205				isipv6 ? tcp_v6mssdflt :
1206#endif /* INET6 */
1207				tcp_mssdflt;
1208			return;
1209		}
1210		taop = rmx_taop(rt->rt_rmx);
1211		offered = taop->tao_mssopt;
1212		mss = rt->rt_rmx.rmx_mtu -
1213#ifdef INET6
1214			(isipv6 ?
1215			 sizeof(struct ip6_hdr) + sizeof(struct tcphdr) :
1216#endif /* INET6 */
1217			 sizeof(struct tcpiphdr)
1218#ifdef INET6
1219			 )
1220#endif /* INET6 */
1221			;
1222
1223		if (offered)
1224			mss = min(mss, offered);
1225		/*
1226		 * XXX - The above conditional probably violates the TCP
1227		 * spec.  The problem is that, since we don't know the
1228		 * other end's MSS, we are supposed to use a conservative
1229		 * default.  But, if we do that, then MTU discovery will
1230		 * never actually take place, because the conservative
1231		 * default is much less than the MTUs typically seen
1232		 * on the Internet today.  For the moment, we'll sweep
1233		 * this under the carpet.
1234		 *
1235		 * The conservative default might not actually be a problem
1236		 * if the only case this occurs is when sending an initial
1237		 * SYN with options and data to a host we've never talked
1238		 * to before.  Then, they will reply with an MSS value which
1239		 * will get recorded and the new parameters should get
1240		 * recomputed.  For Further Study.
1241		 */
1242		if (tp->t_maxopd <= mss)
1243			return;
1244		tp->t_maxopd = mss;
1245
1246		if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
1247		    (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)
1248			mss -= TCPOLEN_TSTAMP_APPA;
1249		if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
1250		    (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC)
1251			mss -= TCPOLEN_CC_APPA;
1252#if	(MCLBYTES & (MCLBYTES - 1)) == 0
1253		if (mss > MCLBYTES)
1254			mss &= ~(MCLBYTES-1);
1255#else
1256		if (mss > MCLBYTES)
1257			mss = mss / MCLBYTES * MCLBYTES;
1258#endif
1259		if (so->so_snd.sb_hiwat < mss)
1260			mss = so->so_snd.sb_hiwat;
1261
1262		tp->t_maxseg = mss;
1263
1264		tcpstat.tcps_mturesent++;
1265		tp->t_rtttime = 0;
1266		tp->snd_nxt = tp->snd_una;
1267		tcp_output(tp);
1268	}
1269}
1270
1271/*
1272 * Look-up the routing entry to the peer of this inpcb.  If no route
1273 * is found and it cannot be allocated the return NULL.  This routine
1274 * is called by TCP routines that access the rmx structure and by tcp_mss
1275 * to get the interface MTU.
1276 */
1277struct rtentry *
1278tcp_rtlookup(inp)
1279	struct inpcb *inp;
1280{
1281	struct route *ro;
1282	struct rtentry *rt;
1283
1284	ro = &inp->inp_route;
1285	rt = ro->ro_rt;
1286	if (rt == NULL || !(rt->rt_flags & RTF_UP)) {
1287		/* No route yet, so try to acquire one */
1288		if (inp->inp_faddr.s_addr != INADDR_ANY) {
1289			ro->ro_dst.sa_family = AF_INET;
1290			ro->ro_dst.sa_len = sizeof(ro->ro_dst);
1291			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
1292				inp->inp_faddr;
1293			rtalloc(ro);
1294			rt = ro->ro_rt;
1295		}
1296	}
1297	return rt;
1298}
1299
1300#ifdef INET6
1301struct rtentry *
1302tcp_rtlookup6(inp)
1303	struct inpcb *inp;
1304{
1305	struct route_in6 *ro6;
1306	struct rtentry *rt;
1307
1308	ro6 = &inp->in6p_route;
1309	rt = ro6->ro_rt;
1310	if (rt == NULL || !(rt->rt_flags & RTF_UP)) {
1311		/* No route yet, so try to acquire one */
1312		if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1313			struct sockaddr_in6 *dst6;
1314
1315			dst6 = (struct sockaddr_in6 *)&ro6->ro_dst;
1316			dst6->sin6_family = AF_INET6;
1317			dst6->sin6_len = sizeof(ro6->ro_dst);
1318			dst6->sin6_addr = inp->in6p_faddr;
1319			rtalloc((struct route *)ro6);
1320			rt = ro6->ro_rt;
1321		}
1322	}
1323	return rt;
1324}
1325#endif /* INET6 */
1326
1327#ifdef IPSEC
1328/* compute ESP/AH header size for TCP, including outer IP header. */
1329size_t
1330ipsec_hdrsiz_tcp(tp)
1331	struct tcpcb *tp;
1332{
1333	struct inpcb *inp;
1334	struct mbuf *m;
1335	size_t hdrsiz;
1336	struct ip *ip;
1337#ifdef INET6
1338	struct ip6_hdr *ip6;
1339#endif /* INET6 */
1340	struct tcphdr *th;
1341
1342	if (!tp || !tp->t_template || !(inp = tp->t_inpcb))
1343		return 0;
1344	MGETHDR(m, M_DONTWAIT, MT_DATA);
1345	if (!m)
1346		return 0;
1347
1348#ifdef INET6
1349	if ((inp->inp_vflag & INP_IPV6) != 0) {
1350		ip6 = mtod(m, struct ip6_hdr *);
1351		th = (struct tcphdr *)(ip6 + 1);
1352		m->m_pkthdr.len = m->m_len =
1353			sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
1354		bcopy((caddr_t)tp->t_template->tt_ipgen, (caddr_t)ip6,
1355		      sizeof(struct ip6_hdr));
1356		bcopy((caddr_t)&tp->t_template->tt_t, (caddr_t)th,
1357		      sizeof(struct tcphdr));
1358		hdrsiz = ipsec6_hdrsiz(m, IPSEC_DIR_OUTBOUND, inp);
1359	} else
1360#endif /* INET6 */
1361      {
1362	ip = mtod(m, struct ip *);
1363	th = (struct tcphdr *)(ip + 1);
1364	m->m_pkthdr.len = m->m_len = sizeof(struct tcpiphdr);
1365	bcopy((caddr_t)tp->t_template->tt_ipgen, (caddr_t)ip,
1366	      sizeof(struct ip));
1367	bcopy((caddr_t)&tp->t_template->tt_t, (caddr_t)th,
1368	      sizeof(struct tcphdr));
1369	ip->ip_vhl = IP_VHL_BORING;
1370	hdrsiz = ipsec4_hdrsiz(m, IPSEC_DIR_OUTBOUND, inp);
1371      }
1372
1373	m_free(m);
1374	return hdrsiz;
1375}
1376#endif /*IPSEC*/
1377
1378/*
1379 * Return a pointer to the cached information about the remote host.
1380 * The cached information is stored in the protocol specific part of
1381 * the route metrics.
1382 */
1383struct rmxp_tao *
1384tcp_gettaocache(inp)
1385	struct inpcb *inp;
1386{
1387	struct rtentry *rt;
1388
1389#ifdef INET6
1390	if ((inp->inp_vflag & INP_IPV6) != 0)
1391		rt = tcp_rtlookup6(inp);
1392	else
1393#endif /* INET6 */
1394	rt = tcp_rtlookup(inp);
1395
1396	/* Make sure this is a host route and is up. */
1397	if (rt == NULL ||
1398	    (rt->rt_flags & (RTF_UP|RTF_HOST)) != (RTF_UP|RTF_HOST))
1399		return NULL;
1400
1401	return rmx_taop(rt->rt_rmx);
1402}
1403
1404/*
1405 * Clear all the TAO cache entries, called from tcp_init.
1406 *
1407 * XXX
1408 * This routine is just an empty one, because we assume that the routing
1409 * routing tables are initialized at the same time when TCP, so there is
1410 * nothing in the cache left over.
1411 */
1412static void
1413tcp_cleartaocache()
1414{
1415}
1416