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