Home | History | Annotate | Line # | Download | only in netinet
tcp_subr.c revision 1.97
      1 /*	$NetBSD: tcp_subr.c,v 1.97 2000/10/18 07:21:10 itojun Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
     34  * All rights reserved.
     35  *
     36  * This code is derived from software contributed to The NetBSD Foundation
     37  * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
     38  * Facility, NASA Ames Research Center.
     39  *
     40  * Redistribution and use in source and binary forms, with or without
     41  * modification, are permitted provided that the following conditions
     42  * are met:
     43  * 1. Redistributions of source code must retain the above copyright
     44  *    notice, this list of conditions and the following disclaimer.
     45  * 2. Redistributions in binary form must reproduce the above copyright
     46  *    notice, this list of conditions and the following disclaimer in the
     47  *    documentation and/or other materials provided with the distribution.
     48  * 3. All advertising materials mentioning features or use of this software
     49  *    must display the following acknowledgement:
     50  *	This product includes software developed by the NetBSD
     51  *	Foundation, Inc. and its contributors.
     52  * 4. Neither the name of The NetBSD Foundation nor the names of its
     53  *    contributors may be used to endorse or promote products derived
     54  *    from this software without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     57  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     58  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     59  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     60  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     61  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     62  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     63  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     64  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     65  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     66  * POSSIBILITY OF SUCH DAMAGE.
     67  */
     68 
     69 /*
     70  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
     71  *	The Regents of the University of California.  All rights reserved.
     72  *
     73  * Redistribution and use in source and binary forms, with or without
     74  * modification, are permitted provided that the following conditions
     75  * are met:
     76  * 1. Redistributions of source code must retain the above copyright
     77  *    notice, this list of conditions and the following disclaimer.
     78  * 2. Redistributions in binary form must reproduce the above copyright
     79  *    notice, this list of conditions and the following disclaimer in the
     80  *    documentation and/or other materials provided with the distribution.
     81  * 3. All advertising materials mentioning features or use of this software
     82  *    must display the following acknowledgement:
     83  *	This product includes software developed by the University of
     84  *	California, Berkeley and its contributors.
     85  * 4. Neither the name of the University nor the names of its contributors
     86  *    may be used to endorse or promote products derived from this software
     87  *    without specific prior written permission.
     88  *
     89  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     90  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     91  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     92  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     93  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     94  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     95  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     96  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     97  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     98  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     99  * SUCH DAMAGE.
    100  *
    101  *	@(#)tcp_subr.c	8.2 (Berkeley) 5/24/95
    102  */
    103 
    104 #include "opt_inet.h"
    105 #include "opt_ipsec.h"
    106 #include "opt_tcp_compat_42.h"
    107 #include "rnd.h"
    108 
    109 #include <sys/param.h>
    110 #include <sys/proc.h>
    111 #include <sys/systm.h>
    112 #include <sys/malloc.h>
    113 #include <sys/mbuf.h>
    114 #include <sys/socket.h>
    115 #include <sys/socketvar.h>
    116 #include <sys/protosw.h>
    117 #include <sys/errno.h>
    118 #include <sys/kernel.h>
    119 #include <sys/pool.h>
    120 #if NRND > 0
    121 #include <sys/rnd.h>
    122 #endif
    123 
    124 #include <net/route.h>
    125 #include <net/if.h>
    126 
    127 #include <netinet/in.h>
    128 #include <netinet/in_systm.h>
    129 #include <netinet/ip.h>
    130 #include <netinet/in_pcb.h>
    131 #include <netinet/ip_var.h>
    132 #include <netinet/ip_icmp.h>
    133 
    134 #ifdef INET6
    135 #ifndef INET
    136 #include <netinet/in.h>
    137 #endif
    138 #include <netinet/ip6.h>
    139 #include <netinet6/in6_pcb.h>
    140 #include <netinet6/ip6_var.h>
    141 #include <netinet6/in6_var.h>
    142 #include <netinet6/ip6protosw.h>
    143 #endif
    144 
    145 #include <netinet/tcp.h>
    146 #include <netinet/tcp_fsm.h>
    147 #include <netinet/tcp_seq.h>
    148 #include <netinet/tcp_timer.h>
    149 #include <netinet/tcp_var.h>
    150 #include <netinet/tcpip.h>
    151 
    152 #ifdef IPSEC
    153 #include <netinet6/ipsec.h>
    154 #endif /*IPSEC*/
    155 
    156 #ifdef INET6
    157 struct in6pcb tcb6;
    158 #endif
    159 
    160 /* patchable/settable parameters for tcp */
    161 int 	tcp_mssdflt = TCP_MSS;
    162 int 	tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ;
    163 int	tcp_do_rfc1323 = 1;	/* window scaling / timestamps (obsolete) */
    164 int	tcp_do_sack = 1;	/* selective acknowledgement */
    165 int	tcp_do_win_scale = 1;	/* RFC1323 window scaling */
    166 int	tcp_do_timestamps = 1;	/* RFC1323 timestamps */
    167 int	tcp_do_newreno = 0;	/* Use the New Reno algorithms */
    168 int	tcp_ack_on_push = 0;	/* set to enable immediate ACK-on-PUSH */
    169 int	tcp_init_win = 1;
    170 int	tcp_mss_ifmtu = 0;
    171 #ifdef TCP_COMPAT_42
    172 int	tcp_compat_42 = 1;
    173 #else
    174 int	tcp_compat_42 = 0;
    175 #endif
    176 int	tcp_rst_ppslim = 100;	/* 100pps */
    177 
    178 /* tcb hash */
    179 #ifndef TCBHASHSIZE
    180 #define	TCBHASHSIZE	128
    181 #endif
    182 int	tcbhashsize = TCBHASHSIZE;
    183 
    184 /* syn hash parameters */
    185 #define	TCP_SYN_HASH_SIZE	293
    186 #define	TCP_SYN_BUCKET_SIZE	35
    187 int	tcp_syn_cache_size = TCP_SYN_HASH_SIZE;
    188 int	tcp_syn_cache_limit = TCP_SYN_HASH_SIZE*TCP_SYN_BUCKET_SIZE;
    189 int	tcp_syn_bucket_limit = 3*TCP_SYN_BUCKET_SIZE;
    190 struct	syn_cache_head tcp_syn_cache[TCP_SYN_HASH_SIZE];
    191 int	tcp_syn_cache_interval = 1;	/* runs timer twice a second */
    192 
    193 int	tcp_freeq __P((struct tcpcb *));
    194 
    195 struct pool tcpcb_pool;
    196 
    197 /*
    198  * Tcp initialization
    199  */
    200 void
    201 tcp_init()
    202 {
    203 	int hlen;
    204 
    205 	pool_init(&tcpcb_pool, sizeof(struct tcpcb), 0, 0, 0, "tcpcbpl",
    206 	    0, NULL, NULL, M_PCB);
    207 	in_pcbinit(&tcbtable, tcbhashsize, tcbhashsize);
    208 #ifdef INET6
    209 	tcb6.in6p_next = tcb6.in6p_prev = &tcb6;
    210 #endif
    211 	LIST_INIT(&tcp_delacks);
    212 
    213 	hlen = sizeof(struct ip) + sizeof(struct tcphdr);
    214 #ifdef INET6
    215 	if (sizeof(struct ip) < sizeof(struct ip6_hdr))
    216 		hlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
    217 #endif
    218 	if (max_protohdr < hlen)
    219 		max_protohdr = hlen;
    220 	if (max_linkhdr + hlen > MHLEN)
    221 		panic("tcp_init");
    222 
    223 	/* Initialize the compressed state engine. */
    224 	syn_cache_init();
    225 }
    226 
    227 /*
    228  * Create template to be used to send tcp packets on a connection.
    229  * Call after host entry created, allocates an mbuf and fills
    230  * in a skeletal tcp/ip header, minimizing the amount of work
    231  * necessary when the connection is used.
    232  */
    233 struct mbuf *
    234 tcp_template(tp)
    235 	struct tcpcb *tp;
    236 {
    237 	struct inpcb *inp = tp->t_inpcb;
    238 #ifdef INET6
    239 	struct in6pcb *in6p = tp->t_in6pcb;
    240 #endif
    241 	struct tcphdr *n;
    242 	struct mbuf *m;
    243 	int hlen;
    244 
    245 	switch (tp->t_family) {
    246 	case AF_INET:
    247 		hlen = sizeof(struct ip);
    248 		if (inp)
    249 			break;
    250 #ifdef INET6
    251 		if (in6p) {
    252 			/* mapped addr case */
    253 			if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
    254 			 && IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr))
    255 				break;
    256 		}
    257 #endif
    258 		return NULL;	/*EINVAL*/
    259 #ifdef INET6
    260 	case AF_INET6:
    261 		hlen = sizeof(struct ip6_hdr);
    262 		if (in6p) {
    263 			/* more sainty check? */
    264 			break;
    265 		}
    266 		return NULL;	/*EINVAL*/
    267 #endif
    268 	default:
    269 		hlen = 0;	/*pacify gcc*/
    270 		return NULL;	/*EAFNOSUPPORT*/
    271 	}
    272 #ifdef DIAGNOSTIC
    273 	if (hlen + sizeof(struct tcphdr) > MCLBYTES)
    274 		panic("mclbytes too small for t_template");
    275 #endif
    276 	m = tp->t_template;
    277 	if (m && m->m_len == hlen + sizeof(struct tcphdr))
    278 		;
    279 	else {
    280 		if (m)
    281 			m_freem(m);
    282 		m = tp->t_template = NULL;
    283 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
    284 		if (m && hlen + sizeof(struct tcphdr) > MHLEN) {
    285 			MCLGET(m, M_DONTWAIT);
    286 			if ((m->m_flags & M_EXT) == 0) {
    287 				m_free(m);
    288 				m = NULL;
    289 			}
    290 		}
    291 		if (m == NULL)
    292 			return NULL;
    293 		m->m_pkthdr.len = m->m_len = hlen + sizeof(struct tcphdr);
    294 	}
    295 	bzero(mtod(m, caddr_t), m->m_len);
    296 	switch (tp->t_family) {
    297 	case AF_INET:
    298 	    {
    299 		struct ipovly *ipov;
    300 		mtod(m, struct ip *)->ip_v = 4;
    301 		ipov = mtod(m, struct ipovly *);
    302 		ipov->ih_pr = IPPROTO_TCP;
    303 		ipov->ih_len = htons(sizeof(struct tcphdr));
    304 		if (inp) {
    305 			ipov->ih_src = inp->inp_laddr;
    306 			ipov->ih_dst = inp->inp_faddr;
    307 		}
    308 #ifdef INET6
    309 		else if (in6p) {
    310 			/* mapped addr case */
    311 			bcopy(&in6p->in6p_laddr.s6_addr32[3], &ipov->ih_src,
    312 				sizeof(ipov->ih_src));
    313 			bcopy(&in6p->in6p_faddr.s6_addr32[3], &ipov->ih_dst,
    314 				sizeof(ipov->ih_dst));
    315 		}
    316 #endif
    317 		break;
    318 	    }
    319 #ifdef INET6
    320 	case AF_INET6:
    321 	    {
    322 		struct ip6_hdr *ip6;
    323 		mtod(m, struct ip *)->ip_v = 6;
    324 		ip6 = mtod(m, struct ip6_hdr *);
    325 		ip6->ip6_nxt = IPPROTO_TCP;
    326 		ip6->ip6_plen = htons(sizeof(struct tcphdr));
    327 		ip6->ip6_src = in6p->in6p_laddr;
    328 		ip6->ip6_dst = in6p->in6p_faddr;
    329 		ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
    330 		if (ip6_auto_flowlabel) {
    331 			ip6->ip6_flow &= ~IPV6_FLOWLABEL_MASK;
    332 			ip6->ip6_flow |=
    333 				(htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
    334 		}
    335 		ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
    336 		ip6->ip6_vfc |= IPV6_VERSION;
    337 		break;
    338 	    }
    339 #endif
    340 	}
    341 	n = (struct tcphdr *)(mtod(m, caddr_t) + hlen);
    342 	if (inp) {
    343 		n->th_sport = inp->inp_lport;
    344 		n->th_dport = inp->inp_fport;
    345 	}
    346 #ifdef INET6
    347 	else if (in6p) {
    348 		n->th_sport = in6p->in6p_lport;
    349 		n->th_dport = in6p->in6p_fport;
    350 	}
    351 #endif
    352 	n->th_seq = 0;
    353 	n->th_ack = 0;
    354 	n->th_x2 = 0;
    355 	n->th_off = 5;
    356 	n->th_flags = 0;
    357 	n->th_win = 0;
    358 	n->th_sum = 0;
    359 	n->th_urp = 0;
    360 	return (m);
    361 }
    362 
    363 /*
    364  * Send a single message to the TCP at address specified by
    365  * the given TCP/IP header.  If m == 0, then we make a copy
    366  * of the tcpiphdr at ti and send directly to the addressed host.
    367  * This is used to force keep alive messages out using the TCP
    368  * template for a connection tp->t_template.  If flags are given
    369  * then we send a message back to the TCP which originated the
    370  * segment ti, and discard the mbuf containing it and any other
    371  * attached mbufs.
    372  *
    373  * In any case the ack and sequence number of the transmitted
    374  * segment are as specified by the parameters.
    375  */
    376 int
    377 tcp_respond(tp, template, m, th0, ack, seq, flags)
    378 	struct tcpcb *tp;
    379 	struct mbuf *template;
    380 	struct mbuf *m;
    381 	struct tcphdr *th0;
    382 	tcp_seq ack, seq;
    383 	int flags;
    384 {
    385 	struct route *ro;
    386 	int error, tlen, win = 0;
    387 	int hlen;
    388 	struct ip *ip;
    389 #ifdef INET6
    390 	struct ip6_hdr *ip6;
    391 #endif
    392 	int family;	/* family on packet, not inpcb/in6pcb! */
    393 	struct tcphdr *th;
    394 
    395 	if (tp != NULL && (flags & TH_RST) == 0) {
    396 #ifdef DIAGNOSTIC
    397 		if (tp->t_inpcb && tp->t_in6pcb)
    398 			panic("tcp_respond: both t_inpcb and t_in6pcb are set");
    399 #endif
    400 #ifdef INET
    401 		if (tp->t_inpcb)
    402 			win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
    403 #endif
    404 #ifdef INET6
    405 		if (tp->t_in6pcb)
    406 			win = sbspace(&tp->t_in6pcb->in6p_socket->so_rcv);
    407 #endif
    408 	}
    409 
    410 	ip = NULL;
    411 #ifdef INET6
    412 	ip6 = NULL;
    413 #endif
    414 	if (m == 0) {
    415 		if (!template)
    416 			return EINVAL;
    417 
    418 		/* get family information from template */
    419 		switch (mtod(template, struct ip *)->ip_v) {
    420 		case 4:
    421 			family = AF_INET;
    422 			hlen = sizeof(struct ip);
    423 			break;
    424 #ifdef INET6
    425 		case 6:
    426 			family = AF_INET6;
    427 			hlen = sizeof(struct ip6_hdr);
    428 			break;
    429 #endif
    430 		default:
    431 			return EAFNOSUPPORT;
    432 		}
    433 
    434 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
    435 		if (m) {
    436 			MCLGET(m, M_DONTWAIT);
    437 			if ((m->m_flags & M_EXT) == 0) {
    438 				m_free(m);
    439 				m = NULL;
    440 			}
    441 		}
    442 		if (m == NULL)
    443 			return (ENOBUFS);
    444 
    445 		if (tcp_compat_42)
    446 			tlen = 1;
    447 		else
    448 			tlen = 0;
    449 
    450 		m->m_data += max_linkhdr;
    451 		bcopy(mtod(template, caddr_t), mtod(m, caddr_t),
    452 			template->m_len);
    453 		switch (family) {
    454 		case AF_INET:
    455 			ip = mtod(m, struct ip *);
    456 			th = (struct tcphdr *)(ip + 1);
    457 			break;
    458 #ifdef INET6
    459 		case AF_INET6:
    460 			ip6 = mtod(m, struct ip6_hdr *);
    461 			th = (struct tcphdr *)(ip6 + 1);
    462 			break;
    463 #endif
    464 #if 0
    465 		default:
    466 			/* noone will visit here */
    467 			m_freem(m);
    468 			return EAFNOSUPPORT;
    469 #endif
    470 		}
    471 		flags = TH_ACK;
    472 	} else {
    473 
    474 		if ((m->m_flags & M_PKTHDR) == 0) {
    475 #if 0
    476 			printf("non PKTHDR to tcp_respond\n");
    477 #endif
    478 			m_freem(m);
    479 			return EINVAL;
    480 		}
    481 #ifdef DIAGNOSTIC
    482 		if (!th0)
    483 			panic("th0 == NULL in tcp_respond");
    484 #endif
    485 
    486 		/* get family information from m */
    487 		switch (mtod(m, struct ip *)->ip_v) {
    488 		case 4:
    489 			family = AF_INET;
    490 			hlen = sizeof(struct ip);
    491 			ip = mtod(m, struct ip *);
    492 			break;
    493 #ifdef INET6
    494 		case 6:
    495 			family = AF_INET6;
    496 			hlen = sizeof(struct ip6_hdr);
    497 			ip6 = mtod(m, struct ip6_hdr *);
    498 			break;
    499 #endif
    500 		default:
    501 			m_freem(m);
    502 			return EAFNOSUPPORT;
    503 		}
    504 		if ((flags & TH_SYN) == 0 || sizeof(*th0) > (th0->th_off << 2))
    505 			tlen = sizeof(*th0);
    506 		else
    507 			tlen = th0->th_off << 2;
    508 
    509 		if (m->m_len > hlen + tlen && (m->m_flags & M_EXT) == 0 &&
    510 		    mtod(m, caddr_t) + hlen == (caddr_t)th0) {
    511 			m->m_len = hlen + tlen;
    512 			m_freem(m->m_next);
    513 			m->m_next = NULL;
    514 		} else {
    515 			struct mbuf *n;
    516 
    517 #ifdef DIAGNOSTIC
    518 			if (max_linkhdr + hlen + tlen > MCLBYTES) {
    519 				m_freem(m);
    520 				return EMSGSIZE;
    521 			}
    522 #endif
    523 			MGETHDR(n, M_DONTWAIT, MT_HEADER);
    524 			if (n && max_linkhdr + hlen + tlen > MHLEN) {
    525 				MCLGET(n, M_DONTWAIT);
    526 				if ((n->m_flags & M_EXT) == 0) {
    527 					m_freem(n);
    528 					n = NULL;
    529 				}
    530 			}
    531 			if (!n) {
    532 				m_freem(m);
    533 				return ENOBUFS;
    534 			}
    535 
    536 			n->m_data += max_linkhdr;
    537 			n->m_len = hlen + tlen;
    538 			m_copyback(n, 0, hlen, mtod(m, caddr_t));
    539 			m_copyback(n, hlen, tlen, (caddr_t)th0);
    540 
    541 			m_freem(m);
    542 			m = n;
    543 			n = NULL;
    544 		}
    545 
    546 #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
    547 		switch (family) {
    548 		case AF_INET:
    549 			ip = mtod(m, struct ip *);
    550 			th = (struct tcphdr *)(ip + 1);
    551 			ip->ip_p = IPPROTO_TCP;
    552 			xchg(ip->ip_dst, ip->ip_src, struct in_addr);
    553 			ip->ip_p = IPPROTO_TCP;
    554 			break;
    555 #ifdef INET6
    556 		case AF_INET6:
    557 			ip6 = mtod(m, struct ip6_hdr *);
    558 			th = (struct tcphdr *)(ip6 + 1);
    559 			ip6->ip6_nxt = IPPROTO_TCP;
    560 			xchg(ip6->ip6_dst, ip6->ip6_src, struct in6_addr);
    561 			ip6->ip6_nxt = IPPROTO_TCP;
    562 			break;
    563 #endif
    564 #if 0
    565 		default:
    566 			/* noone will visit here */
    567 			m_freem(m);
    568 			return EAFNOSUPPORT;
    569 #endif
    570 		}
    571 		xchg(th->th_dport, th->th_sport, u_int16_t);
    572 #undef xchg
    573 		tlen = 0;	/*be friendly with the following code*/
    574 	}
    575 	th->th_seq = htonl(seq);
    576 	th->th_ack = htonl(ack);
    577 	th->th_x2 = 0;
    578 	if ((flags & TH_SYN) == 0) {
    579 		if (tp)
    580 			win >>= tp->rcv_scale;
    581 		if (win > TCP_MAXWIN)
    582 			win = TCP_MAXWIN;
    583 		th->th_win = htons((u_int16_t)win);
    584 		th->th_off = sizeof (struct tcphdr) >> 2;
    585 		tlen += sizeof(*th);
    586 	} else
    587 		tlen += th->th_off << 2;
    588 	m->m_len = hlen + tlen;
    589 	m->m_pkthdr.len = hlen + tlen;
    590 	m->m_pkthdr.rcvif = (struct ifnet *) 0;
    591 	th->th_flags = flags;
    592 	th->th_urp = 0;
    593 
    594 	switch (family) {
    595 #ifdef INET
    596 	case AF_INET:
    597 	    {
    598 		struct ipovly *ipov = (struct ipovly *)ip;
    599 		bzero(ipov->ih_x1, sizeof ipov->ih_x1);
    600 		ipov->ih_len = htons((u_int16_t)tlen);
    601 
    602 		th->th_sum = 0;
    603 		th->th_sum = in_cksum(m, hlen + tlen);
    604 		ip->ip_len = hlen + tlen;	/*will be flipped on output*/
    605 		ip->ip_ttl = ip_defttl;
    606 		break;
    607 	    }
    608 #endif
    609 #ifdef INET6
    610 	case AF_INET6:
    611 	    {
    612 		th->th_sum = 0;
    613 		th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
    614 				tlen);
    615 		ip6->ip6_plen = ntohs(tlen);
    616 		if (tp && tp->t_in6pcb) {
    617 			struct ifnet *oifp;
    618 			ro = (struct route *)&tp->t_in6pcb->in6p_route;
    619 			oifp = ro->ro_rt ? ro->ro_rt->rt_ifp : NULL;
    620 			ip6->ip6_hlim = in6_selecthlim(tp->t_in6pcb, oifp);
    621 		} else
    622 			ip6->ip6_hlim = ip6_defhlim;
    623 		ip6->ip6_flow &= ~IPV6_FLOWINFO_MASK;
    624 		if (ip6_auto_flowlabel) {
    625 			ip6->ip6_flow |=
    626 				(htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
    627 		}
    628 		break;
    629 	    }
    630 #endif
    631 	}
    632 
    633 #ifdef IPSEC
    634 	ipsec_setsocket(m, NULL);
    635 #endif /*IPSEC*/
    636 
    637 	if (tp != NULL && tp->t_inpcb != NULL) {
    638 		ro = &tp->t_inpcb->inp_route;
    639 #ifdef IPSEC
    640 		ipsec_setsocket(m, tp->t_inpcb->inp_socket);
    641 #endif
    642 #ifdef DIAGNOSTIC
    643 		if (family != AF_INET)
    644 			panic("tcp_respond: address family mismatch");
    645 		if (!in_hosteq(ip->ip_dst, tp->t_inpcb->inp_faddr)) {
    646 			panic("tcp_respond: ip_dst %x != inp_faddr %x",
    647 			    ntohl(ip->ip_dst.s_addr),
    648 			    ntohl(tp->t_inpcb->inp_faddr.s_addr));
    649 		}
    650 #endif
    651 	}
    652 #ifdef INET6
    653 	else if (tp != NULL && tp->t_in6pcb != NULL) {
    654 		ro = (struct route *)&tp->t_in6pcb->in6p_route;
    655 #ifdef IPSEC
    656 		ipsec_setsocket(m, tp->t_in6pcb->in6p_socket);
    657 #endif
    658 #ifdef DIAGNOSTIC
    659 		if (family == AF_INET) {
    660 			if (!IN6_IS_ADDR_V4MAPPED(&tp->t_in6pcb->in6p_faddr))
    661 				panic("tcp_respond: not mapped addr");
    662 			if (bcmp(&ip->ip_dst,
    663 					&tp->t_in6pcb->in6p_faddr.s6_addr32[3],
    664 					sizeof(ip->ip_dst)) != 0) {
    665 				panic("tcp_respond: ip_dst != in6p_faddr");
    666 			}
    667 		} else if (family == AF_INET6) {
    668 			if (!IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &tp->t_in6pcb->in6p_faddr))
    669 				panic("tcp_respond: ip6_dst != in6p_faddr");
    670 		} else
    671 			panic("tcp_respond: address family mismatch");
    672 #endif
    673 	}
    674 #endif
    675 	else
    676 		ro = NULL;
    677 
    678 	switch (family) {
    679 #ifdef INET
    680 	case AF_INET:
    681 		error = ip_output(m, NULL, ro,
    682 		    (ip_mtudisc ? IP_MTUDISC : 0),
    683 		    NULL);
    684 		break;
    685 #endif
    686 #ifdef INET6
    687 	case AF_INET6:
    688 		error = ip6_output(m, NULL, (struct route_in6 *)ro, 0, NULL,
    689 			NULL);
    690 		break;
    691 #endif
    692 	default:
    693 		error = EAFNOSUPPORT;
    694 		break;
    695 	}
    696 
    697 	return (error);
    698 }
    699 
    700 /*
    701  * Create a new TCP control block, making an
    702  * empty reassembly queue and hooking it to the argument
    703  * protocol control block.
    704  */
    705 struct tcpcb *
    706 tcp_newtcpcb(family, aux)
    707 	int family;	/* selects inpcb, or in6pcb */
    708 	void *aux;
    709 {
    710 	struct tcpcb *tp;
    711 
    712 	switch (family) {
    713 	case PF_INET:
    714 		break;
    715 #ifdef INET6
    716 	case PF_INET6:
    717 		break;
    718 #endif
    719 	default:
    720 		return NULL;
    721 	}
    722 
    723 	tp = pool_get(&tcpcb_pool, PR_NOWAIT);
    724 	if (tp == NULL)
    725 		return (NULL);
    726 	bzero((caddr_t)tp, sizeof(struct tcpcb));
    727 	LIST_INIT(&tp->segq);
    728 	LIST_INIT(&tp->timeq);
    729 	tp->t_family = family;		/* may be overridden later on */
    730 	tp->t_peermss = tcp_mssdflt;
    731 	tp->t_ourmss = tcp_mssdflt;
    732 	tp->t_segsz = tcp_mssdflt;
    733 	LIST_INIT(&tp->t_sc);
    734 
    735 	tp->t_flags = 0;
    736 	if (tcp_do_rfc1323 && tcp_do_win_scale)
    737 		tp->t_flags |= TF_REQ_SCALE;
    738 	if (tcp_do_rfc1323 && tcp_do_timestamps)
    739 		tp->t_flags |= TF_REQ_TSTMP;
    740 	if (tcp_do_sack == 2)
    741 		tp->t_flags |= TF_WILL_SACK;
    742 	else if (tcp_do_sack == 1)
    743 		tp->t_flags |= TF_WILL_SACK|TF_IGNR_RXSACK;
    744 	tp->t_flags |= TF_CANT_TXSACK;
    745 	switch (family) {
    746 	case PF_INET:
    747 		tp->t_inpcb = (struct inpcb *)aux;
    748 		break;
    749 #ifdef INET6
    750 	case PF_INET6:
    751 		tp->t_in6pcb = (struct in6pcb *)aux;
    752 		break;
    753 #endif
    754 	}
    755 	/*
    756 	 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
    757 	 * rtt estimate.  Set rttvar so that srtt + 2 * rttvar gives
    758 	 * reasonable initial retransmit time.
    759 	 */
    760 	tp->t_srtt = TCPTV_SRTTBASE;
    761 	tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << (TCP_RTTVAR_SHIFT + 2 - 1);
    762 	tp->t_rttmin = TCPTV_MIN;
    763 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
    764 	    TCPTV_MIN, TCPTV_REXMTMAX);
    765 	tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
    766 	tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
    767 	if (family == AF_INET) {
    768 		struct inpcb *inp = (struct inpcb *)aux;
    769 		inp->inp_ip.ip_ttl = ip_defttl;
    770 		inp->inp_ppcb = (caddr_t)tp;
    771 	}
    772 #ifdef INET6
    773 	else if (family == AF_INET6) {
    774 		struct in6pcb *in6p = (struct in6pcb *)aux;
    775 		in6p->in6p_ip6.ip6_hlim = in6_selecthlim(in6p,
    776 			in6p->in6p_route.ro_rt ? in6p->in6p_route.ro_rt->rt_ifp
    777 					       : NULL);
    778 		in6p->in6p_ppcb = (caddr_t)tp;
    779 	}
    780 #endif
    781 	return (tp);
    782 }
    783 
    784 /*
    785  * Drop a TCP connection, reporting
    786  * the specified error.  If connection is synchronized,
    787  * then send a RST to peer.
    788  */
    789 struct tcpcb *
    790 tcp_drop(tp, errno)
    791 	struct tcpcb *tp;
    792 	int errno;
    793 {
    794 	struct socket *so;
    795 
    796 #ifdef DIAGNOSTIC
    797 	if (tp->t_inpcb && tp->t_in6pcb)
    798 		panic("tcp_drop: both t_inpcb and t_in6pcb are set");
    799 #endif
    800 #ifdef INET
    801 	if (tp->t_inpcb)
    802 		so = tp->t_inpcb->inp_socket;
    803 #endif
    804 #ifdef INET6
    805 	if (tp->t_in6pcb)
    806 		so = tp->t_in6pcb->in6p_socket;
    807 #endif
    808 	else
    809 		return NULL;
    810 
    811 	if (TCPS_HAVERCVDSYN(tp->t_state)) {
    812 		tp->t_state = TCPS_CLOSED;
    813 		(void) tcp_output(tp);
    814 		tcpstat.tcps_drops++;
    815 	} else
    816 		tcpstat.tcps_conndrops++;
    817 	if (errno == ETIMEDOUT && tp->t_softerror)
    818 		errno = tp->t_softerror;
    819 	so->so_error = errno;
    820 	return (tcp_close(tp));
    821 }
    822 
    823 /*
    824  * Close a TCP control block:
    825  *	discard all space held by the tcp
    826  *	discard internet protocol block
    827  *	wake up any sleepers
    828  */
    829 struct tcpcb *
    830 tcp_close(tp)
    831 	struct tcpcb *tp;
    832 {
    833 	struct inpcb *inp;
    834 #ifdef INET6
    835 	struct in6pcb *in6p;
    836 #endif
    837 	struct socket *so;
    838 #ifdef RTV_RTT
    839 	struct rtentry *rt;
    840 #endif
    841 	struct route *ro;
    842 
    843 	inp = tp->t_inpcb;
    844 #ifdef INET6
    845 	in6p = tp->t_in6pcb;
    846 #endif
    847 	so = NULL;
    848 	ro = NULL;
    849 	if (inp) {
    850 		so = inp->inp_socket;
    851 		ro = &inp->inp_route;
    852 	}
    853 #ifdef INET6
    854 	else if (in6p) {
    855 		so = in6p->in6p_socket;
    856 		ro = (struct route *)&in6p->in6p_route;
    857 	}
    858 #endif
    859 
    860 #ifdef RTV_RTT
    861 	/*
    862 	 * If we sent enough data to get some meaningful characteristics,
    863 	 * save them in the routing entry.  'Enough' is arbitrarily
    864 	 * defined as the sendpipesize (default 4K) * 16.  This would
    865 	 * give us 16 rtt samples assuming we only get one sample per
    866 	 * window (the usual case on a long haul net).  16 samples is
    867 	 * enough for the srtt filter to converge to within 5% of the correct
    868 	 * value; fewer samples and we could save a very bogus rtt.
    869 	 *
    870 	 * Don't update the default route's characteristics and don't
    871 	 * update anything that the user "locked".
    872 	 */
    873 	if (SEQ_LT(tp->iss + so->so_snd.sb_hiwat * 16, tp->snd_max) &&
    874 	    ro && (rt = ro->ro_rt) &&
    875 	    !in_nullhost(satosin(rt_key(rt))->sin_addr)) {
    876 		u_long i = 0;
    877 
    878 		if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) {
    879 			i = tp->t_srtt *
    880 			    ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2));
    881 			if (rt->rt_rmx.rmx_rtt && i)
    882 				/*
    883 				 * filter this update to half the old & half
    884 				 * the new values, converting scale.
    885 				 * See route.h and tcp_var.h for a
    886 				 * description of the scaling constants.
    887 				 */
    888 				rt->rt_rmx.rmx_rtt =
    889 				    (rt->rt_rmx.rmx_rtt + i) / 2;
    890 			else
    891 				rt->rt_rmx.rmx_rtt = i;
    892 		}
    893 		if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) {
    894 			i = tp->t_rttvar *
    895 			    ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTTVAR_SHIFT + 2));
    896 			if (rt->rt_rmx.rmx_rttvar && i)
    897 				rt->rt_rmx.rmx_rttvar =
    898 				    (rt->rt_rmx.rmx_rttvar + i) / 2;
    899 			else
    900 				rt->rt_rmx.rmx_rttvar = i;
    901 		}
    902 		/*
    903 		 * update the pipelimit (ssthresh) if it has been updated
    904 		 * already or if a pipesize was specified & the threshhold
    905 		 * got below half the pipesize.  I.e., wait for bad news
    906 		 * before we start updating, then update on both good
    907 		 * and bad news.
    908 		 */
    909 		if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 &&
    910 		    (i = tp->snd_ssthresh) && rt->rt_rmx.rmx_ssthresh) ||
    911 		    i < (rt->rt_rmx.rmx_sendpipe / 2)) {
    912 			/*
    913 			 * convert the limit from user data bytes to
    914 			 * packets then to packet data bytes.
    915 			 */
    916 			i = (i + tp->t_segsz / 2) / tp->t_segsz;
    917 			if (i < 2)
    918 				i = 2;
    919 			i *= (u_long)(tp->t_segsz + sizeof (struct tcpiphdr));
    920 			if (rt->rt_rmx.rmx_ssthresh)
    921 				rt->rt_rmx.rmx_ssthresh =
    922 				    (rt->rt_rmx.rmx_ssthresh + i) / 2;
    923 			else
    924 				rt->rt_rmx.rmx_ssthresh = i;
    925 		}
    926 	}
    927 #endif /* RTV_RTT */
    928 	/* free the reassembly queue, if any */
    929 	TCP_REASS_LOCK(tp);
    930 	(void) tcp_freeq(tp);
    931 	TCP_REASS_UNLOCK(tp);
    932 
    933 	TCP_CLEAR_DELACK(tp);
    934 	syn_cache_cleanup(tp);
    935 
    936 	if (tp->t_template) {
    937 		m_free(tp->t_template);
    938 		tp->t_template = NULL;
    939 	}
    940 	pool_put(&tcpcb_pool, tp);
    941 	if (inp) {
    942 		inp->inp_ppcb = 0;
    943 		soisdisconnected(so);
    944 		in_pcbdetach(inp);
    945 	}
    946 #ifdef INET6
    947 	else if (in6p) {
    948 		in6p->in6p_ppcb = 0;
    949 		soisdisconnected(so);
    950 		in6_pcbdetach(in6p);
    951 	}
    952 #endif
    953 	tcpstat.tcps_closed++;
    954 	return ((struct tcpcb *)0);
    955 }
    956 
    957 int
    958 tcp_freeq(tp)
    959 	struct tcpcb *tp;
    960 {
    961 	struct ipqent *qe;
    962 	int rv = 0;
    963 #ifdef TCPREASS_DEBUG
    964 	int i = 0;
    965 #endif
    966 
    967 	TCP_REASS_LOCK_CHECK(tp);
    968 
    969 	while ((qe = tp->segq.lh_first) != NULL) {
    970 #ifdef TCPREASS_DEBUG
    971 		printf("tcp_freeq[%p,%d]: %u:%u(%u) 0x%02x\n",
    972 			tp, i++, qe->ipqe_seq, qe->ipqe_seq + qe->ipqe_len,
    973 			qe->ipqe_len, qe->ipqe_flags & (TH_SYN|TH_FIN|TH_RST));
    974 #endif
    975 		LIST_REMOVE(qe, ipqe_q);
    976 		LIST_REMOVE(qe, ipqe_timeq);
    977 		m_freem(qe->ipqe_m);
    978 		pool_put(&ipqent_pool, qe);
    979 		rv = 1;
    980 	}
    981 	return (rv);
    982 }
    983 
    984 /*
    985  * Protocol drain routine.  Called when memory is in short supply.
    986  */
    987 void
    988 tcp_drain()
    989 {
    990 	struct inpcb *inp;
    991 	struct tcpcb *tp;
    992 
    993 	/*
    994 	 * Free the sequence queue of all TCP connections.
    995 	 */
    996 	inp = tcbtable.inpt_queue.cqh_first;
    997 	if (inp)						/* XXX */
    998 	for (; inp != (struct inpcb *)&tcbtable.inpt_queue;
    999 	    inp = inp->inp_queue.cqe_next) {
   1000 		if ((tp = intotcpcb(inp)) != NULL) {
   1001 			/*
   1002 			 * We may be called from a device's interrupt
   1003 			 * context.  If the tcpcb is already busy,
   1004 			 * just bail out now.
   1005 			 */
   1006 			if (tcp_reass_lock_try(tp) == 0)
   1007 				continue;
   1008 			if (tcp_freeq(tp))
   1009 				tcpstat.tcps_connsdrained++;
   1010 			TCP_REASS_UNLOCK(tp);
   1011 		}
   1012 	}
   1013 }
   1014 
   1015 /*
   1016  * Notify a tcp user of an asynchronous error;
   1017  * store error as soft error, but wake up user
   1018  * (for now, won't do anything until can select for soft error).
   1019  */
   1020 void
   1021 tcp_notify(inp, error)
   1022 	struct inpcb *inp;
   1023 	int error;
   1024 {
   1025 	struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb;
   1026 	struct socket *so = inp->inp_socket;
   1027 
   1028 	/*
   1029 	 * Ignore some errors if we are hooked up.
   1030 	 * If connection hasn't completed, has retransmitted several times,
   1031 	 * and receives a second error, give up now.  This is better
   1032 	 * than waiting a long time to establish a connection that
   1033 	 * can never complete.
   1034 	 */
   1035 	if (tp->t_state == TCPS_ESTABLISHED &&
   1036 	     (error == EHOSTUNREACH || error == ENETUNREACH ||
   1037 	      error == EHOSTDOWN)) {
   1038 		return;
   1039 	} else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 &&
   1040 	    tp->t_rxtshift > 3 && tp->t_softerror)
   1041 		so->so_error = error;
   1042 	else
   1043 		tp->t_softerror = error;
   1044 	wakeup((caddr_t) &so->so_timeo);
   1045 	sorwakeup(so);
   1046 	sowwakeup(so);
   1047 }
   1048 
   1049 #if defined(INET6) && !defined(TCP6)
   1050 void
   1051 tcp6_notify(in6p, error)
   1052 	struct in6pcb *in6p;
   1053 	int error;
   1054 {
   1055 	struct tcpcb *tp = (struct tcpcb *)in6p->in6p_ppcb;
   1056 	struct socket *so = in6p->in6p_socket;
   1057 
   1058 	/*
   1059 	 * Ignore some errors if we are hooked up.
   1060 	 * If connection hasn't completed, has retransmitted several times,
   1061 	 * and receives a second error, give up now.  This is better
   1062 	 * than waiting a long time to establish a connection that
   1063 	 * can never complete.
   1064 	 */
   1065 	if (tp->t_state == TCPS_ESTABLISHED &&
   1066 	     (error == EHOSTUNREACH || error == ENETUNREACH ||
   1067 	      error == EHOSTDOWN)) {
   1068 		return;
   1069 	} else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 &&
   1070 	    tp->t_rxtshift > 3 && tp->t_softerror)
   1071 		so->so_error = error;
   1072 	else
   1073 		tp->t_softerror = error;
   1074 	wakeup((caddr_t) &so->so_timeo);
   1075 	sorwakeup(so);
   1076 	sowwakeup(so);
   1077 }
   1078 #endif
   1079 
   1080 #if defined(INET6) && !defined(TCP6)
   1081 void
   1082 tcp6_ctlinput(cmd, sa, d)
   1083 	int cmd;
   1084 	struct sockaddr *sa;
   1085 	void *d;
   1086 {
   1087 	struct tcphdr *thp;
   1088 	struct tcphdr th;
   1089 	void (*notify) __P((struct in6pcb *, int)) = tcp6_notify;
   1090 	int nmatch;
   1091 	struct sockaddr_in6 sa6;
   1092 	struct ip6_hdr *ip6;
   1093 	struct mbuf *m;
   1094 	int off;
   1095 
   1096 	if (sa->sa_family != AF_INET6 ||
   1097 	    sa->sa_len != sizeof(struct sockaddr_in6))
   1098 		return;
   1099 	if ((unsigned)cmd >= PRC_NCMDS)
   1100 		return;
   1101 	else if (cmd == PRC_QUENCH) {
   1102 		/* XXX there's no PRC_QUENCH in IPv6 */
   1103 		notify = tcp6_quench;
   1104 	} else if (PRC_IS_REDIRECT(cmd))
   1105 		notify = in6_rtchange, d = NULL;
   1106 	else if (cmd == PRC_MSGSIZE)
   1107 		notify = tcp6_mtudisc, d = NULL;
   1108 	else if (cmd == PRC_HOSTDEAD)
   1109 		d = NULL;
   1110 	else if (inet6ctlerrmap[cmd] == 0)
   1111 		return;
   1112 
   1113 	/* if the parameter is from icmp6, decode it. */
   1114 	if (d != NULL) {
   1115 		struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d;
   1116 		m = ip6cp->ip6c_m;
   1117 		ip6 = ip6cp->ip6c_ip6;
   1118 		off = ip6cp->ip6c_off;
   1119 	} else {
   1120 		m = NULL;
   1121 		ip6 = NULL;
   1122 	}
   1123 
   1124 	/* translate addresses into internal form */
   1125 	sa6 = *(struct sockaddr_in6 *)sa;
   1126 	if (IN6_IS_ADDR_LINKLOCAL(&sa6.sin6_addr) && m && m->m_pkthdr.rcvif)
   1127 		sa6.sin6_addr.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
   1128 
   1129 	if (ip6) {
   1130 		/*
   1131 		 * XXX: We assume that when ip6 is non NULL,
   1132 		 * M and OFF are valid.
   1133 		 */
   1134 		struct in6_addr s;
   1135 
   1136 		/* translate addresses into internal form */
   1137 		memcpy(&s, &ip6->ip6_src, sizeof(s));
   1138 		if (IN6_IS_ADDR_LINKLOCAL(&s))
   1139 			s.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
   1140 
   1141 		/* check if we can safely examine src and dst ports */
   1142 		if (m->m_pkthdr.len < off + sizeof(th))
   1143 			return;
   1144 
   1145 		if (m->m_len < off + sizeof(th)) {
   1146 			/*
   1147 			 * this should be rare case,
   1148 			 * so we compromise on this copy...
   1149 			 */
   1150 			m_copydata(m, off, sizeof(th), (caddr_t)&th);
   1151 			thp = &th;
   1152 		} else
   1153 			thp = (struct tcphdr *)(mtod(m, caddr_t) + off);
   1154 		nmatch = in6_pcbnotify(&tcb6, (struct sockaddr *)&sa6,
   1155 		    thp->th_dport, &s, thp->th_sport, cmd, notify);
   1156 		if (nmatch == 0 && syn_cache_count &&
   1157 		    (inet6ctlerrmap[cmd] == EHOSTUNREACH ||
   1158 		     inet6ctlerrmap[cmd] == ENETUNREACH ||
   1159 		     inet6ctlerrmap[cmd] == EHOSTDOWN)) {
   1160 			struct sockaddr_in6 sin6;
   1161 			bzero(&sin6, sizeof(sin6));
   1162 			sin6.sin6_len = sizeof(sin6);
   1163 			sin6.sin6_family = AF_INET6;
   1164 			sin6.sin6_port = thp->th_sport;
   1165 			sin6.sin6_addr = s;
   1166 			syn_cache_unreach((struct sockaddr *)&sin6, sa, thp);
   1167 		}
   1168 	} else {
   1169 		(void) in6_pcbnotify(&tcb6, (struct sockaddr *)&sa6, 0,
   1170 				     &zeroin6_addr, 0, cmd, notify);
   1171 	}
   1172 }
   1173 #endif
   1174 
   1175 #ifdef INET
   1176 /* assumes that ip header and tcp header are contiguous on mbuf */
   1177 void *
   1178 tcp_ctlinput(cmd, sa, v)
   1179 	int cmd;
   1180 	struct sockaddr *sa;
   1181 	void *v;
   1182 {
   1183 	struct ip *ip = v;
   1184 	struct tcphdr *th;
   1185 	extern int inetctlerrmap[];
   1186 	void (*notify) __P((struct inpcb *, int)) = tcp_notify;
   1187 	int errno;
   1188 	int nmatch;
   1189 
   1190 	if (sa->sa_family != AF_INET ||
   1191 	    sa->sa_len != sizeof(struct sockaddr_in))
   1192 		return NULL;
   1193 	if ((unsigned)cmd >= PRC_NCMDS)
   1194 		return NULL;
   1195 	errno = inetctlerrmap[cmd];
   1196 	if (cmd == PRC_QUENCH)
   1197 		notify = tcp_quench;
   1198 	else if (PRC_IS_REDIRECT(cmd))
   1199 		notify = in_rtchange, ip = 0;
   1200 	else if (cmd == PRC_MSGSIZE && ip_mtudisc)
   1201 		notify = tcp_mtudisc, ip = 0;
   1202 	else if (cmd == PRC_HOSTDEAD)
   1203 		ip = 0;
   1204 	else if (errno == 0)
   1205 		return NULL;
   1206 	if (ip && ip->ip_v == 4 && sa->sa_family == AF_INET) {
   1207 		th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
   1208 		nmatch = in_pcbnotify(&tcbtable, satosin(sa)->sin_addr,
   1209 		    th->th_dport, ip->ip_src, th->th_sport, errno, notify);
   1210 		if (nmatch == 0 && syn_cache_count &&
   1211 		    (inetctlerrmap[cmd] == EHOSTUNREACH ||
   1212 		    inetctlerrmap[cmd] == ENETUNREACH ||
   1213 		    inetctlerrmap[cmd] == EHOSTDOWN)) {
   1214 			struct sockaddr_in sin;
   1215 			bzero(&sin, sizeof(sin));
   1216 			sin.sin_len = sizeof(sin);
   1217 			sin.sin_family = AF_INET;
   1218 			sin.sin_port = th->th_sport;
   1219 			sin.sin_addr = ip->ip_src;
   1220 			syn_cache_unreach((struct sockaddr *)&sin, sa, th);
   1221 		}
   1222 
   1223 		/* XXX mapped address case */
   1224 	}
   1225 	else {
   1226 		(void)in_pcbnotifyall(&tcbtable, satosin(sa)->sin_addr, errno,
   1227 		    notify);
   1228 	}
   1229 	return NULL;
   1230 }
   1231 
   1232 /*
   1233  * When a source quence is received, we are being notifed of congestion.
   1234  * Close the congestion window down to the Loss Window (one segment).
   1235  * We will gradually open it again as we proceed.
   1236  */
   1237 void
   1238 tcp_quench(inp, errno)
   1239 	struct inpcb *inp;
   1240 	int errno;
   1241 {
   1242 	struct tcpcb *tp = intotcpcb(inp);
   1243 
   1244 	if (tp)
   1245 		tp->snd_cwnd = tp->t_segsz;
   1246 }
   1247 #endif
   1248 
   1249 #if defined(INET6) && !defined(TCP6)
   1250 void
   1251 tcp6_quench(in6p, errno)
   1252 	struct in6pcb *in6p;
   1253 	int errno;
   1254 {
   1255 	struct tcpcb *tp = in6totcpcb(in6p);
   1256 
   1257 	if (tp)
   1258 		tp->snd_cwnd = tp->t_segsz;
   1259 }
   1260 #endif
   1261 
   1262 /*
   1263  * On receipt of path MTU corrections, flush old route and replace it
   1264  * with the new one.  Retransmit all unacknowledged packets, to ensure
   1265  * that all packets will be received.
   1266  */
   1267 void
   1268 tcp_mtudisc(inp, errno)
   1269 	struct inpcb *inp;
   1270 	int errno;
   1271 {
   1272 	struct tcpcb *tp = intotcpcb(inp);
   1273 	struct rtentry *rt = in_pcbrtentry(inp);
   1274 
   1275 	if (tp != 0) {
   1276 		if (rt != 0) {
   1277 			/*
   1278 			 * If this was not a host route, remove and realloc.
   1279 			 */
   1280 			if ((rt->rt_flags & RTF_HOST) == 0) {
   1281 				in_rtchange(inp, errno);
   1282 				if ((rt = in_pcbrtentry(inp)) == 0)
   1283 					return;
   1284 			}
   1285 
   1286 			/*
   1287 			 * Slow start out of the error condition.  We
   1288 			 * use the MTU because we know it's smaller
   1289 			 * than the previously transmitted segment.
   1290 			 *
   1291 			 * Note: This is more conservative than the
   1292 			 * suggestion in draft-floyd-incr-init-win-03.
   1293 			 */
   1294 			if (rt->rt_rmx.rmx_mtu != 0)
   1295 				tp->snd_cwnd =
   1296 				    TCP_INITIAL_WINDOW(tcp_init_win,
   1297 				    rt->rt_rmx.rmx_mtu);
   1298 		}
   1299 
   1300 		/*
   1301 		 * Resend unacknowledged packets.
   1302 		 */
   1303 		tp->snd_nxt = tp->snd_una;
   1304 		tcp_output(tp);
   1305 	}
   1306 }
   1307 
   1308 #if defined(INET6) && !defined(TCP6)
   1309 void
   1310 tcp6_mtudisc(in6p, errno)
   1311 	struct in6pcb *in6p;
   1312 	int errno;
   1313 {
   1314 	struct tcpcb *tp = in6totcpcb(in6p);
   1315 	struct rtentry *rt = in6_pcbrtentry(in6p);
   1316 
   1317 	if (tp != 0) {
   1318 		if (rt != 0) {
   1319 			/*
   1320 			 * If this was not a host route, remove and realloc.
   1321 			 */
   1322 			if ((rt->rt_flags & RTF_HOST) == 0) {
   1323 				in6_rtchange(in6p, errno);
   1324 				if ((rt = in6_pcbrtentry(in6p)) == 0)
   1325 					return;
   1326 			}
   1327 
   1328 			/*
   1329 			 * Slow start out of the error condition.  We
   1330 			 * use the MTU because we know it's smaller
   1331 			 * than the previously transmitted segment.
   1332 			 *
   1333 			 * Note: This is more conservative than the
   1334 			 * suggestion in draft-floyd-incr-init-win-03.
   1335 			 */
   1336 			if (rt->rt_rmx.rmx_mtu != 0)
   1337 				tp->snd_cwnd =
   1338 				    TCP_INITIAL_WINDOW(tcp_init_win,
   1339 				    rt->rt_rmx.rmx_mtu);
   1340 		}
   1341 
   1342 		/*
   1343 		 * Resend unacknowledged packets.
   1344 		 */
   1345 		tp->snd_nxt = tp->snd_una;
   1346 		tcp_output(tp);
   1347 	}
   1348 }
   1349 #endif
   1350 
   1351 /*
   1352  * Compute the MSS to advertise to the peer.  Called only during
   1353  * the 3-way handshake.  If we are the server (peer initiated
   1354  * connection), we are called with a pointer to the interface
   1355  * on which the SYN packet arrived.  If we are the client (we
   1356  * initiated connection), we are called with a pointer to the
   1357  * interface out which this connection should go.
   1358  *
   1359  * NOTE: Do not subtract IP option/extension header size nor IPsec
   1360  * header size from MSS advertisement.  MSS option must hold the maximum
   1361  * segment size we can accept, so it must always be:
   1362  *	 max(if mtu) - ip header - tcp header
   1363  */
   1364 u_long
   1365 tcp_mss_to_advertise(ifp, af)
   1366 	const struct ifnet *ifp;
   1367 	int af;
   1368 {
   1369 	extern u_long in_maxmtu;
   1370 	u_long mss = 0;
   1371 	u_long hdrsiz;
   1372 
   1373 	/*
   1374 	 * In order to avoid defeating path MTU discovery on the peer,
   1375 	 * we advertise the max MTU of all attached networks as our MSS,
   1376 	 * per RFC 1191, section 3.1.
   1377 	 *
   1378 	 * We provide the option to advertise just the MTU of
   1379 	 * the interface on which we hope this connection will
   1380 	 * be receiving.  If we are responding to a SYN, we
   1381 	 * will have a pretty good idea about this, but when
   1382 	 * initiating a connection there is a bit more doubt.
   1383 	 *
   1384 	 * We also need to ensure that loopback has a large enough
   1385 	 * MSS, as the loopback MTU is never included in in_maxmtu.
   1386 	 */
   1387 
   1388 	if (ifp != NULL)
   1389 		mss = ifp->if_mtu;
   1390 
   1391 	if (tcp_mss_ifmtu == 0)
   1392 		mss = max(in_maxmtu, mss);
   1393 
   1394 	switch (af) {
   1395 	case AF_INET:
   1396 		hdrsiz = sizeof(struct ip);
   1397 		break;
   1398 #ifdef INET6
   1399 	case AF_INET6:
   1400 		hdrsiz = sizeof(struct ip6_hdr);
   1401 		break;
   1402 #endif
   1403 	default:
   1404 		hdrsiz = 0;
   1405 		break;
   1406 	}
   1407 	hdrsiz += sizeof(struct tcphdr);
   1408 	if (mss > hdrsiz)
   1409 		mss -= hdrsiz;
   1410 
   1411 	mss = max(tcp_mssdflt, mss);
   1412 	return (mss);
   1413 }
   1414 
   1415 /*
   1416  * Set connection variables based on the peer's advertised MSS.
   1417  * We are passed the TCPCB for the actual connection.  If we
   1418  * are the server, we are called by the compressed state engine
   1419  * when the 3-way handshake is complete.  If we are the client,
   1420  * we are called when we recieve the SYN,ACK from the server.
   1421  *
   1422  * NOTE: Our advertised MSS value must be initialized in the TCPCB
   1423  * before this routine is called!
   1424  */
   1425 void
   1426 tcp_mss_from_peer(tp, offer)
   1427 	struct tcpcb *tp;
   1428 	int offer;
   1429 {
   1430 	struct socket *so;
   1431 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
   1432 	struct rtentry *rt;
   1433 #endif
   1434 	u_long bufsize;
   1435 	int mss;
   1436 
   1437 #ifdef DIAGNOSTIC
   1438 	if (tp->t_inpcb && tp->t_in6pcb)
   1439 		panic("tcp_mss_from_peer: both t_inpcb and t_in6pcb are set");
   1440 #endif
   1441 	so = NULL;
   1442 	rt = NULL;
   1443 #ifdef INET
   1444 	if (tp->t_inpcb) {
   1445 		so = tp->t_inpcb->inp_socket;
   1446 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
   1447 		rt = in_pcbrtentry(tp->t_inpcb);
   1448 #endif
   1449 	}
   1450 #endif
   1451 #ifdef INET6
   1452 	if (tp->t_in6pcb) {
   1453 		so = tp->t_in6pcb->in6p_socket;
   1454 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
   1455 #ifdef TCP6
   1456 		rt = NULL;
   1457 #else
   1458 		rt = in6_pcbrtentry(tp->t_in6pcb);
   1459 #endif
   1460 #endif
   1461 	}
   1462 #endif
   1463 
   1464 	/*
   1465 	 * As per RFC1122, use the default MSS value, unless they
   1466 	 * sent us an offer.  Do not accept offers less than 32 bytes.
   1467 	 */
   1468 	mss = tcp_mssdflt;
   1469 	if (offer)
   1470 		mss = offer;
   1471 	mss = max(mss, 32);		/* sanity */
   1472 	tp->t_peermss = mss;
   1473 	mss -= tcp_optlen(tp);
   1474 #ifdef INET
   1475 	if (tp->t_inpcb)
   1476 		mss -= ip_optlen(tp->t_inpcb);
   1477 #endif
   1478 #ifdef INET6
   1479 	if (tp->t_in6pcb)
   1480 		mss -= ip6_optlen(tp->t_in6pcb);
   1481 #endif
   1482 
   1483 	/*
   1484 	 * If there's a pipesize, change the socket buffer to that size.
   1485 	 * Make the socket buffer an integral number of MSS units.  If
   1486 	 * the MSS is larger than the socket buffer, artificially decrease
   1487 	 * the MSS.
   1488 	 */
   1489 #ifdef RTV_SPIPE
   1490 	if (rt != NULL && rt->rt_rmx.rmx_sendpipe != 0)
   1491 		bufsize = rt->rt_rmx.rmx_sendpipe;
   1492 	else
   1493 #endif
   1494 		bufsize = so->so_snd.sb_hiwat;
   1495 	if (bufsize < mss)
   1496 		mss = bufsize;
   1497 	else {
   1498 		bufsize = roundup(bufsize, mss);
   1499 		if (bufsize > sb_max)
   1500 			bufsize = sb_max;
   1501 		(void) sbreserve(&so->so_snd, bufsize);
   1502 	}
   1503 	tp->t_segsz = mss;
   1504 
   1505 #ifdef RTV_SSTHRESH
   1506 	if (rt != NULL && rt->rt_rmx.rmx_ssthresh) {
   1507 		/*
   1508 		 * There's some sort of gateway or interface buffer
   1509 		 * limit on the path.  Use this to set the slow
   1510 		 * start threshold, but set the threshold to no less
   1511 		 * than 2 * MSS.
   1512 		 */
   1513 		tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
   1514 	}
   1515 #endif
   1516 }
   1517 
   1518 /*
   1519  * Processing necessary when a TCP connection is established.
   1520  */
   1521 void
   1522 tcp_established(tp)
   1523 	struct tcpcb *tp;
   1524 {
   1525 	struct socket *so;
   1526 #ifdef RTV_RPIPE
   1527 	struct rtentry *rt;
   1528 #endif
   1529 	u_long bufsize;
   1530 
   1531 #ifdef DIAGNOSTIC
   1532 	if (tp->t_inpcb && tp->t_in6pcb)
   1533 		panic("tcp_established: both t_inpcb and t_in6pcb are set");
   1534 #endif
   1535 	so = NULL;
   1536 	rt = NULL;
   1537 #ifdef INET
   1538 	if (tp->t_inpcb) {
   1539 		so = tp->t_inpcb->inp_socket;
   1540 #if defined(RTV_RPIPE)
   1541 		rt = in_pcbrtentry(tp->t_inpcb);
   1542 #endif
   1543 	}
   1544 #endif
   1545 #ifdef INET6
   1546 	if (tp->t_in6pcb) {
   1547 		so = tp->t_in6pcb->in6p_socket;
   1548 #if defined(RTV_RPIPE)
   1549 #ifdef TCP6
   1550 		rt = NULL;
   1551 #else
   1552 		rt = in6_pcbrtentry(tp->t_in6pcb);
   1553 #endif
   1554 #endif
   1555 	}
   1556 #endif
   1557 
   1558 	tp->t_state = TCPS_ESTABLISHED;
   1559 	TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle);
   1560 
   1561 #ifdef RTV_RPIPE
   1562 	if (rt != NULL && rt->rt_rmx.rmx_recvpipe != 0)
   1563 		bufsize = rt->rt_rmx.rmx_recvpipe;
   1564 	else
   1565 #endif
   1566 		bufsize = so->so_rcv.sb_hiwat;
   1567 	if (bufsize > tp->t_ourmss) {
   1568 		bufsize = roundup(bufsize, tp->t_ourmss);
   1569 		if (bufsize > sb_max)
   1570 			bufsize = sb_max;
   1571 		(void) sbreserve(&so->so_rcv, bufsize);
   1572 	}
   1573 }
   1574 
   1575 /*
   1576  * Check if there's an initial rtt or rttvar.  Convert from the
   1577  * route-table units to scaled multiples of the slow timeout timer.
   1578  * Called only during the 3-way handshake.
   1579  */
   1580 void
   1581 tcp_rmx_rtt(tp)
   1582 	struct tcpcb *tp;
   1583 {
   1584 #ifdef RTV_RTT
   1585 	struct rtentry *rt = NULL;
   1586 	int rtt;
   1587 
   1588 #ifdef DIAGNOSTIC
   1589 	if (tp->t_inpcb && tp->t_in6pcb)
   1590 		panic("tcp_rmx_rtt: both t_inpcb and t_in6pcb are set");
   1591 #endif
   1592 #ifdef INET
   1593 	if (tp->t_inpcb)
   1594 		rt = in_pcbrtentry(tp->t_inpcb);
   1595 #endif
   1596 #ifdef INET6
   1597 	if (tp->t_in6pcb) {
   1598 #ifdef TCP6
   1599 		rt = NULL;
   1600 #else
   1601 		rt = in6_pcbrtentry(tp->t_in6pcb);
   1602 #endif
   1603 	}
   1604 #endif
   1605 	if (rt == NULL)
   1606 		return;
   1607 
   1608 	if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
   1609 		/*
   1610 		 * XXX The lock bit for MTU indicates that the value
   1611 		 * is also a minimum value; this is subject to time.
   1612 		 */
   1613 		if (rt->rt_rmx.rmx_locks & RTV_RTT)
   1614 			TCPT_RANGESET(tp->t_rttmin,
   1615 			    rtt / (RTM_RTTUNIT / PR_SLOWHZ),
   1616 			    TCPTV_MIN, TCPTV_REXMTMAX);
   1617 		tp->t_srtt = rtt /
   1618 		    ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2));
   1619 		if (rt->rt_rmx.rmx_rttvar) {
   1620 			tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
   1621 			    ((RTM_RTTUNIT / PR_SLOWHZ) >>
   1622 				(TCP_RTTVAR_SHIFT + 2));
   1623 		} else {
   1624 			/* Default variation is +- 1 rtt */
   1625 			tp->t_rttvar =
   1626 			    tp->t_srtt >> (TCP_RTT_SHIFT - TCP_RTTVAR_SHIFT);
   1627 		}
   1628 		TCPT_RANGESET(tp->t_rxtcur,
   1629 		    ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2),
   1630 		    tp->t_rttmin, TCPTV_REXMTMAX);
   1631 	}
   1632 #endif
   1633 }
   1634 
   1635 tcp_seq	 tcp_iss_seq = 0;	/* tcp initial seq # */
   1636 
   1637 /*
   1638  * Get a new sequence value given a tcp control block
   1639  */
   1640 tcp_seq
   1641 tcp_new_iss(tp, len, addin)
   1642 	void            *tp;
   1643 	u_long           len;
   1644 	tcp_seq		 addin;
   1645 {
   1646 	tcp_seq          tcp_iss;
   1647 
   1648 	/*
   1649 	 * Randomize.
   1650 	 */
   1651 #if NRND > 0
   1652 	rnd_extract_data(&tcp_iss, sizeof(tcp_iss), RND_EXTRACT_ANY);
   1653 #else
   1654 	tcp_iss = random();
   1655 #endif
   1656 
   1657 	/*
   1658 	 * If we were asked to add some amount to a known value,
   1659 	 * we will take a random value obtained above, mask off the upper
   1660 	 * bits, and add in the known value.  We also add in a constant to
   1661 	 * ensure that we are at least a certain distance from the original
   1662 	 * value.
   1663 	 *
   1664 	 * This is used when an old connection is in timed wait
   1665 	 * and we have a new one coming in, for instance.
   1666 	 */
   1667 	if (addin != 0) {
   1668 #ifdef TCPISS_DEBUG
   1669 		printf("Random %08x, ", tcp_iss);
   1670 #endif
   1671 		tcp_iss &= TCP_ISS_RANDOM_MASK;
   1672 		tcp_iss += addin + TCP_ISSINCR;
   1673 #ifdef TCPISS_DEBUG
   1674 		printf("Old ISS %08x, ISS %08x\n", addin, tcp_iss);
   1675 #endif
   1676 	} else {
   1677 		tcp_iss &= TCP_ISS_RANDOM_MASK;
   1678 		tcp_iss += tcp_iss_seq;
   1679 		tcp_iss_seq += TCP_ISSINCR;
   1680 #ifdef TCPISS_DEBUG
   1681 		printf("ISS %08x\n", tcp_iss);
   1682 #endif
   1683 	}
   1684 
   1685 	if (tcp_compat_42) {
   1686 		/*
   1687 		 * Limit it to the positive range for really old TCP
   1688 		 * implementations.
   1689 		 */
   1690 		if (tcp_iss >= 0x80000000)
   1691 			tcp_iss &= 0x7fffffff;		/* XXX */
   1692 	}
   1693 
   1694 	return tcp_iss;
   1695 }
   1696 
   1697 #ifdef IPSEC
   1698 /* compute ESP/AH header size for TCP, including outer IP header. */
   1699 size_t
   1700 ipsec4_hdrsiz_tcp(tp)
   1701 	struct tcpcb *tp;
   1702 {
   1703 	struct inpcb *inp;
   1704 	size_t hdrsiz;
   1705 
   1706 	/* XXX mapped addr case (tp->t_in6pcb) */
   1707 	if (!tp || !tp->t_template || !(inp = tp->t_inpcb))
   1708 		return 0;
   1709 	switch (tp->t_family) {
   1710 	case AF_INET:
   1711 		/* XXX: should use currect direction. */
   1712 		hdrsiz = ipsec4_hdrsiz(tp->t_template, IPSEC_DIR_OUTBOUND, inp);
   1713 		break;
   1714 	default:
   1715 		hdrsiz = 0;
   1716 		break;
   1717 	}
   1718 
   1719 	return hdrsiz;
   1720 }
   1721 
   1722 #if defined(INET6) && !defined(TCP6)
   1723 size_t
   1724 ipsec6_hdrsiz_tcp(tp)
   1725 	struct tcpcb *tp;
   1726 {
   1727 	struct in6pcb *in6p;
   1728 	size_t hdrsiz;
   1729 
   1730 	if (!tp || !tp->t_template || !(in6p = tp->t_in6pcb))
   1731 		return 0;
   1732 	switch (tp->t_family) {
   1733 	case AF_INET6:
   1734 		/* XXX: should use currect direction. */
   1735 		hdrsiz = ipsec6_hdrsiz(tp->t_template, IPSEC_DIR_OUTBOUND, in6p);
   1736 		break;
   1737 	case AF_INET:
   1738 		/* mapped address case - tricky */
   1739 	default:
   1740 		hdrsiz = 0;
   1741 		break;
   1742 	}
   1743 
   1744 	return hdrsiz;
   1745 }
   1746 #endif
   1747 #endif /*IPSEC*/
   1748 
   1749 /*
   1750  * Determine the length of the TCP options for this connection.
   1751  *
   1752  * XXX:  What do we do for SACK, when we add that?  Just reserve
   1753  *       all of the space?  Otherwise we can't exactly be incrementing
   1754  *       cwnd by an amount that varies depending on the amount we last
   1755  *       had to SACK!
   1756  */
   1757 
   1758 u_int
   1759 tcp_optlen(tp)
   1760 	struct tcpcb *tp;
   1761 {
   1762 	if ((tp->t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP|TF_NOOPT)) ==
   1763 	    (TF_REQ_TSTMP | TF_RCVD_TSTMP))
   1764 		return TCPOLEN_TSTAMP_APPA;
   1765 	else
   1766 		return 0;
   1767 }
   1768