Home | History | Annotate | Line # | Download | only in netinet
tcp_subr.c revision 1.176
      1 /*	$NetBSD: tcp_subr.c,v 1.176 2004/12/19 06:42:24 christos 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, 2000, 2001 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. Neither the name of the University nor the names of its contributors
     82  *    may be used to endorse or promote products derived from this software
     83  *    without specific prior written permission.
     84  *
     85  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     86  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     87  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     88  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     89  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     90  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     91  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     92  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     93  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     94  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     95  * SUCH DAMAGE.
     96  *
     97  *	@(#)tcp_subr.c	8.2 (Berkeley) 5/24/95
     98  */
     99 
    100 #include <sys/cdefs.h>
    101 __KERNEL_RCSID(0, "$NetBSD: tcp_subr.c,v 1.176 2004/12/19 06:42:24 christos Exp $");
    102 
    103 #include "opt_inet.h"
    104 #include "opt_ipsec.h"
    105 #include "opt_tcp_compat_42.h"
    106 #include "opt_inet_csum.h"
    107 #include "opt_mbuftrace.h"
    108 #include "rnd.h"
    109 
    110 #include <sys/param.h>
    111 #include <sys/proc.h>
    112 #include <sys/systm.h>
    113 #include <sys/malloc.h>
    114 #include <sys/mbuf.h>
    115 #include <sys/socket.h>
    116 #include <sys/socketvar.h>
    117 #include <sys/protosw.h>
    118 #include <sys/errno.h>
    119 #include <sys/kernel.h>
    120 #include <sys/pool.h>
    121 #if NRND > 0
    122 #include <sys/md5.h>
    123 #include <sys/rnd.h>
    124 #endif
    125 
    126 #include <net/route.h>
    127 #include <net/if.h>
    128 
    129 #include <netinet/in.h>
    130 #include <netinet/in_systm.h>
    131 #include <netinet/ip.h>
    132 #include <netinet/in_pcb.h>
    133 #include <netinet/ip_var.h>
    134 #include <netinet/ip_icmp.h>
    135 
    136 #ifdef INET6
    137 #ifndef INET
    138 #include <netinet/in.h>
    139 #endif
    140 #include <netinet/ip6.h>
    141 #include <netinet6/in6_pcb.h>
    142 #include <netinet6/ip6_var.h>
    143 #include <netinet6/in6_var.h>
    144 #include <netinet6/ip6protosw.h>
    145 #include <netinet/icmp6.h>
    146 #include <netinet6/nd6.h>
    147 #endif
    148 
    149 #include <netinet/tcp.h>
    150 #include <netinet/tcp_fsm.h>
    151 #include <netinet/tcp_seq.h>
    152 #include <netinet/tcp_timer.h>
    153 #include <netinet/tcp_var.h>
    154 #include <netinet/tcpip.h>
    155 
    156 #ifdef IPSEC
    157 #include <netinet6/ipsec.h>
    158 #include <netkey/key.h>
    159 #endif /*IPSEC*/
    160 
    161 #ifdef FAST_IPSEC
    162 #include <netipsec/ipsec.h>
    163 #include <netipsec/xform.h>
    164 #ifdef INET6
    165 #include <netipsec/ipsec6.h>
    166 #endif
    167  #include <netipsec/key.h>
    168 #endif	/* FAST_IPSEC*/
    169 
    170 
    171 struct	inpcbtable tcbtable;	/* head of queue of active tcpcb's */
    172 struct	tcpstat tcpstat;	/* tcp statistics */
    173 u_int32_t tcp_now;		/* for RFC 1323 timestamps */
    174 
    175 /* patchable/settable parameters for tcp */
    176 int 	tcp_mssdflt = TCP_MSS;
    177 int 	tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ;
    178 int	tcp_do_rfc1323 = 1;	/* window scaling / timestamps (obsolete) */
    179 #if NRND > 0
    180 int	tcp_do_rfc1948 = 0;	/* ISS by cryptographic hash */
    181 #endif
    182 int	tcp_do_sack = 1;	/* selective acknowledgement */
    183 int	tcp_do_win_scale = 1;	/* RFC1323 window scaling */
    184 int	tcp_do_timestamps = 1;	/* RFC1323 timestamps */
    185 int	tcp_do_newreno = 1;	/* Use the New Reno algorithms */
    186 int	tcp_ack_on_push = 0;	/* set to enable immediate ACK-on-PUSH */
    187 #ifndef TCP_INIT_WIN
    188 #define	TCP_INIT_WIN	0	/* initial slow start window */
    189 #endif
    190 #ifndef TCP_INIT_WIN_LOCAL
    191 #define	TCP_INIT_WIN_LOCAL 4	/* initial slow start window for local nets */
    192 #endif
    193 int	tcp_init_win = TCP_INIT_WIN;
    194 int	tcp_init_win_local = TCP_INIT_WIN_LOCAL;
    195 int	tcp_mss_ifmtu = 0;
    196 #ifdef TCP_COMPAT_42
    197 int	tcp_compat_42 = 1;
    198 #else
    199 int	tcp_compat_42 = 0;
    200 #endif
    201 int	tcp_rst_ppslim = 100;	/* 100pps */
    202 int	tcp_ackdrop_ppslim = 100;	/* 100pps */
    203 int	tcp_do_loopback_cksum = 0;
    204 
    205 /* tcb hash */
    206 #ifndef TCBHASHSIZE
    207 #define	TCBHASHSIZE	128
    208 #endif
    209 int	tcbhashsize = TCBHASHSIZE;
    210 
    211 /* syn hash parameters */
    212 #define	TCP_SYN_HASH_SIZE	293
    213 #define	TCP_SYN_BUCKET_SIZE	35
    214 int	tcp_syn_cache_size = TCP_SYN_HASH_SIZE;
    215 int	tcp_syn_cache_limit = TCP_SYN_HASH_SIZE*TCP_SYN_BUCKET_SIZE;
    216 int	tcp_syn_bucket_limit = 3*TCP_SYN_BUCKET_SIZE;
    217 struct	syn_cache_head tcp_syn_cache[TCP_SYN_HASH_SIZE];
    218 
    219 int	tcp_freeq __P((struct tcpcb *));
    220 
    221 #ifdef INET
    222 void	tcp_mtudisc_callback __P((struct in_addr));
    223 #endif
    224 #ifdef INET6
    225 void	tcp6_mtudisc_callback __P((struct in6_addr *));
    226 #endif
    227 
    228 void	tcp_mtudisc __P((struct inpcb *, int));
    229 #ifdef INET6
    230 void	tcp6_mtudisc __P((struct in6pcb *, int));
    231 #endif
    232 
    233 POOL_INIT(tcpcb_pool, sizeof(struct tcpcb), 0, 0, 0, "tcpcbpl", NULL);
    234 
    235 #ifdef TCP_CSUM_COUNTERS
    236 #include <sys/device.h>
    237 
    238 struct evcnt tcp_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    239     NULL, "tcp", "hwcsum bad");
    240 struct evcnt tcp_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    241     NULL, "tcp", "hwcsum ok");
    242 struct evcnt tcp_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    243     NULL, "tcp", "hwcsum data");
    244 struct evcnt tcp_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    245     NULL, "tcp", "swcsum");
    246 
    247 EVCNT_ATTACH_STATIC(tcp_hwcsum_bad);
    248 EVCNT_ATTACH_STATIC(tcp_hwcsum_ok);
    249 EVCNT_ATTACH_STATIC(tcp_hwcsum_data);
    250 EVCNT_ATTACH_STATIC(tcp_swcsum);
    251 #endif /* TCP_CSUM_COUNTERS */
    252 
    253 
    254 #ifdef TCP_OUTPUT_COUNTERS
    255 #include <sys/device.h>
    256 
    257 struct evcnt tcp_output_bigheader = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    258     NULL, "tcp", "output big header");
    259 struct evcnt tcp_output_predict_hit = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    260     NULL, "tcp", "output predict hit");
    261 struct evcnt tcp_output_predict_miss = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    262     NULL, "tcp", "output predict miss");
    263 struct evcnt tcp_output_copysmall = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    264     NULL, "tcp", "output copy small");
    265 struct evcnt tcp_output_copybig = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    266     NULL, "tcp", "output copy big");
    267 struct evcnt tcp_output_refbig = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    268     NULL, "tcp", "output reference big");
    269 
    270 EVCNT_ATTACH_STATIC(tcp_output_bigheader);
    271 EVCNT_ATTACH_STATIC(tcp_output_predict_hit);
    272 EVCNT_ATTACH_STATIC(tcp_output_predict_miss);
    273 EVCNT_ATTACH_STATIC(tcp_output_copysmall);
    274 EVCNT_ATTACH_STATIC(tcp_output_copybig);
    275 EVCNT_ATTACH_STATIC(tcp_output_refbig);
    276 
    277 #endif /* TCP_OUTPUT_COUNTERS */
    278 
    279 #ifdef TCP_REASS_COUNTERS
    280 #include <sys/device.h>
    281 
    282 struct evcnt tcp_reass_ = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    283     NULL, "tcp_reass", "calls");
    284 struct evcnt tcp_reass_empty = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    285     &tcp_reass_, "tcp_reass", "insert into empty queue");
    286 struct evcnt tcp_reass_iteration[8] = {
    287     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", ">7 iterations"),
    288     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "1 iteration"),
    289     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "2 iterations"),
    290     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "3 iterations"),
    291     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "4 iterations"),
    292     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "5 iterations"),
    293     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "6 iterations"),
    294     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, &tcp_reass_, "tcp_reass", "7 iterations"),
    295 };
    296 struct evcnt tcp_reass_prependfirst = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    297     &tcp_reass_, "tcp_reass", "prepend to first");
    298 struct evcnt tcp_reass_prepend = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    299     &tcp_reass_, "tcp_reass", "prepend");
    300 struct evcnt tcp_reass_insert = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    301     &tcp_reass_, "tcp_reass", "insert");
    302 struct evcnt tcp_reass_inserttail = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    303     &tcp_reass_, "tcp_reass", "insert at tail");
    304 struct evcnt tcp_reass_append = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    305     &tcp_reass_, "tcp_reass", "append");
    306 struct evcnt tcp_reass_appendtail = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    307     &tcp_reass_, "tcp_reass", "append to tail fragment");
    308 struct evcnt tcp_reass_overlaptail = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    309     &tcp_reass_, "tcp_reass", "overlap at end");
    310 struct evcnt tcp_reass_overlapfront = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    311     &tcp_reass_, "tcp_reass", "overlap at start");
    312 struct evcnt tcp_reass_segdup = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    313     &tcp_reass_, "tcp_reass", "duplicate segment");
    314 struct evcnt tcp_reass_fragdup = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    315     &tcp_reass_, "tcp_reass", "duplicate fragment");
    316 
    317 EVCNT_ATTACH_STATIC(tcp_reass_);
    318 EVCNT_ATTACH_STATIC(tcp_reass_empty);
    319 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 0);
    320 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 1);
    321 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 2);
    322 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 3);
    323 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 4);
    324 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 5);
    325 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 6);
    326 EVCNT_ATTACH_STATIC2(tcp_reass_iteration, 7);
    327 EVCNT_ATTACH_STATIC(tcp_reass_prependfirst);
    328 EVCNT_ATTACH_STATIC(tcp_reass_prepend);
    329 EVCNT_ATTACH_STATIC(tcp_reass_insert);
    330 EVCNT_ATTACH_STATIC(tcp_reass_inserttail);
    331 EVCNT_ATTACH_STATIC(tcp_reass_append);
    332 EVCNT_ATTACH_STATIC(tcp_reass_appendtail);
    333 EVCNT_ATTACH_STATIC(tcp_reass_overlaptail);
    334 EVCNT_ATTACH_STATIC(tcp_reass_overlapfront);
    335 EVCNT_ATTACH_STATIC(tcp_reass_segdup);
    336 EVCNT_ATTACH_STATIC(tcp_reass_fragdup);
    337 
    338 #endif /* TCP_REASS_COUNTERS */
    339 
    340 #ifdef MBUFTRACE
    341 struct mowner tcp_mowner = { "tcp" };
    342 struct mowner tcp_rx_mowner = { "tcp", "rx" };
    343 struct mowner tcp_tx_mowner = { "tcp", "tx" };
    344 #endif
    345 
    346 /*
    347  * Tcp initialization
    348  */
    349 void
    350 tcp_init()
    351 {
    352 	int hlen;
    353 
    354 	/* Initialize the TCPCB template. */
    355 	tcp_tcpcb_template();
    356 
    357 	in_pcbinit(&tcbtable, tcbhashsize, tcbhashsize);
    358 
    359 	hlen = sizeof(struct ip) + sizeof(struct tcphdr);
    360 #ifdef INET6
    361 	if (sizeof(struct ip) < sizeof(struct ip6_hdr))
    362 		hlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
    363 #endif
    364 	if (max_protohdr < hlen)
    365 		max_protohdr = hlen;
    366 	if (max_linkhdr + hlen > MHLEN)
    367 		panic("tcp_init");
    368 
    369 #ifdef INET
    370 	icmp_mtudisc_callback_register(tcp_mtudisc_callback);
    371 #endif
    372 #ifdef INET6
    373 	icmp6_mtudisc_callback_register(tcp6_mtudisc_callback);
    374 #endif
    375 
    376 	/* Initialize timer state. */
    377 	tcp_timer_init();
    378 
    379 	/* Initialize the compressed state engine. */
    380 	syn_cache_init();
    381 
    382 	MOWNER_ATTACH(&tcp_tx_mowner);
    383 	MOWNER_ATTACH(&tcp_rx_mowner);
    384 	MOWNER_ATTACH(&tcp_mowner);
    385 }
    386 
    387 /*
    388  * Create template to be used to send tcp packets on a connection.
    389  * Call after host entry created, allocates an mbuf and fills
    390  * in a skeletal tcp/ip header, minimizing the amount of work
    391  * necessary when the connection is used.
    392  */
    393 struct mbuf *
    394 tcp_template(tp)
    395 	struct tcpcb *tp;
    396 {
    397 	struct inpcb *inp = tp->t_inpcb;
    398 #ifdef INET6
    399 	struct in6pcb *in6p = tp->t_in6pcb;
    400 #endif
    401 	struct tcphdr *n;
    402 	struct mbuf *m;
    403 	int hlen;
    404 
    405 	switch (tp->t_family) {
    406 	case AF_INET:
    407 		hlen = sizeof(struct ip);
    408 		if (inp)
    409 			break;
    410 #ifdef INET6
    411 		if (in6p) {
    412 			/* mapped addr case */
    413 			if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
    414 			 && IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr))
    415 				break;
    416 		}
    417 #endif
    418 		return NULL;	/*EINVAL*/
    419 #ifdef INET6
    420 	case AF_INET6:
    421 		hlen = sizeof(struct ip6_hdr);
    422 		if (in6p) {
    423 			/* more sainty check? */
    424 			break;
    425 		}
    426 		return NULL;	/*EINVAL*/
    427 #endif
    428 	default:
    429 		hlen = 0;	/*pacify gcc*/
    430 		return NULL;	/*EAFNOSUPPORT*/
    431 	}
    432 #ifdef DIAGNOSTIC
    433 	if (hlen + sizeof(struct tcphdr) > MCLBYTES)
    434 		panic("mclbytes too small for t_template");
    435 #endif
    436 	m = tp->t_template;
    437 	if (m && m->m_len == hlen + sizeof(struct tcphdr))
    438 		;
    439 	else {
    440 		if (m)
    441 			m_freem(m);
    442 		m = tp->t_template = NULL;
    443 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
    444 		if (m && hlen + sizeof(struct tcphdr) > MHLEN) {
    445 			MCLGET(m, M_DONTWAIT);
    446 			if ((m->m_flags & M_EXT) == 0) {
    447 				m_free(m);
    448 				m = NULL;
    449 			}
    450 		}
    451 		if (m == NULL)
    452 			return NULL;
    453 		MCLAIM(m, &tcp_mowner);
    454 		m->m_pkthdr.len = m->m_len = hlen + sizeof(struct tcphdr);
    455 	}
    456 
    457 	bzero(mtod(m, caddr_t), m->m_len);
    458 
    459 	n = (struct tcphdr *)(mtod(m, caddr_t) + hlen);
    460 
    461 	switch (tp->t_family) {
    462 	case AF_INET:
    463 	    {
    464 		struct ipovly *ipov;
    465 		mtod(m, struct ip *)->ip_v = 4;
    466 		mtod(m, struct ip *)->ip_hl = hlen >> 2;
    467 		ipov = mtod(m, struct ipovly *);
    468 		ipov->ih_pr = IPPROTO_TCP;
    469 		ipov->ih_len = htons(sizeof(struct tcphdr));
    470 		if (inp) {
    471 			ipov->ih_src = inp->inp_laddr;
    472 			ipov->ih_dst = inp->inp_faddr;
    473 		}
    474 #ifdef INET6
    475 		else if (in6p) {
    476 			/* mapped addr case */
    477 			bcopy(&in6p->in6p_laddr.s6_addr32[3], &ipov->ih_src,
    478 				sizeof(ipov->ih_src));
    479 			bcopy(&in6p->in6p_faddr.s6_addr32[3], &ipov->ih_dst,
    480 				sizeof(ipov->ih_dst));
    481 		}
    482 #endif
    483 		/*
    484 		 * Compute the pseudo-header portion of the checksum
    485 		 * now.  We incrementally add in the TCP option and
    486 		 * payload lengths later, and then compute the TCP
    487 		 * checksum right before the packet is sent off onto
    488 		 * the wire.
    489 		 */
    490 		n->th_sum = in_cksum_phdr(ipov->ih_src.s_addr,
    491 		    ipov->ih_dst.s_addr,
    492 		    htons(sizeof(struct tcphdr) + IPPROTO_TCP));
    493 		break;
    494 	    }
    495 #ifdef INET6
    496 	case AF_INET6:
    497 	    {
    498 		struct ip6_hdr *ip6;
    499 		mtod(m, struct ip *)->ip_v = 6;
    500 		ip6 = mtod(m, struct ip6_hdr *);
    501 		ip6->ip6_nxt = IPPROTO_TCP;
    502 		ip6->ip6_plen = htons(sizeof(struct tcphdr));
    503 		ip6->ip6_src = in6p->in6p_laddr;
    504 		ip6->ip6_dst = in6p->in6p_faddr;
    505 		ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
    506 		if (ip6_auto_flowlabel) {
    507 			ip6->ip6_flow &= ~IPV6_FLOWLABEL_MASK;
    508 			ip6->ip6_flow |=
    509 			    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
    510 		}
    511 		ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
    512 		ip6->ip6_vfc |= IPV6_VERSION;
    513 
    514 		/*
    515 		 * Compute the pseudo-header portion of the checksum
    516 		 * now.  We incrementally add in the TCP option and
    517 		 * payload lengths later, and then compute the TCP
    518 		 * checksum right before the packet is sent off onto
    519 		 * the wire.
    520 		 */
    521 		n->th_sum = in6_cksum_phdr(&in6p->in6p_laddr,
    522 		    &in6p->in6p_faddr, htonl(sizeof(struct tcphdr)),
    523 		    htonl(IPPROTO_TCP));
    524 		break;
    525 	    }
    526 #endif
    527 	}
    528 	if (inp) {
    529 		n->th_sport = inp->inp_lport;
    530 		n->th_dport = inp->inp_fport;
    531 	}
    532 #ifdef INET6
    533 	else if (in6p) {
    534 		n->th_sport = in6p->in6p_lport;
    535 		n->th_dport = in6p->in6p_fport;
    536 	}
    537 #endif
    538 	n->th_seq = 0;
    539 	n->th_ack = 0;
    540 	n->th_x2 = 0;
    541 	n->th_off = 5;
    542 	n->th_flags = 0;
    543 	n->th_win = 0;
    544 	n->th_urp = 0;
    545 	return (m);
    546 }
    547 
    548 /*
    549  * Send a single message to the TCP at address specified by
    550  * the given TCP/IP header.  If m == 0, then we make a copy
    551  * of the tcpiphdr at ti and send directly to the addressed host.
    552  * This is used to force keep alive messages out using the TCP
    553  * template for a connection tp->t_template.  If flags are given
    554  * then we send a message back to the TCP which originated the
    555  * segment ti, and discard the mbuf containing it and any other
    556  * attached mbufs.
    557  *
    558  * In any case the ack and sequence number of the transmitted
    559  * segment are as specified by the parameters.
    560  */
    561 int
    562 tcp_respond(tp, template, m, th0, ack, seq, flags)
    563 	struct tcpcb *tp;
    564 	struct mbuf *template;
    565 	struct mbuf *m;
    566 	struct tcphdr *th0;
    567 	tcp_seq ack, seq;
    568 	int flags;
    569 {
    570 	struct route *ro;
    571 	int error, tlen, win = 0;
    572 	int hlen;
    573 	struct ip *ip;
    574 #ifdef INET6
    575 	struct ip6_hdr *ip6;
    576 #endif
    577 	int family;	/* family on packet, not inpcb/in6pcb! */
    578 	struct tcphdr *th;
    579 	struct socket *so;
    580 
    581 	if (tp != NULL && (flags & TH_RST) == 0) {
    582 #ifdef DIAGNOSTIC
    583 		if (tp->t_inpcb && tp->t_in6pcb)
    584 			panic("tcp_respond: both t_inpcb and t_in6pcb are set");
    585 #endif
    586 #ifdef INET
    587 		if (tp->t_inpcb)
    588 			win = sbspace(&tp->t_inpcb->inp_socket->so_rcv);
    589 #endif
    590 #ifdef INET6
    591 		if (tp->t_in6pcb)
    592 			win = sbspace(&tp->t_in6pcb->in6p_socket->so_rcv);
    593 #endif
    594 	}
    595 
    596 	th = NULL;	/* Quell uninitialized warning */
    597 	ip = NULL;
    598 #ifdef INET6
    599 	ip6 = NULL;
    600 #endif
    601 	if (m == 0) {
    602 		if (!template)
    603 			return EINVAL;
    604 
    605 		/* get family information from template */
    606 		switch (mtod(template, struct ip *)->ip_v) {
    607 		case 4:
    608 			family = AF_INET;
    609 			hlen = sizeof(struct ip);
    610 			break;
    611 #ifdef INET6
    612 		case 6:
    613 			family = AF_INET6;
    614 			hlen = sizeof(struct ip6_hdr);
    615 			break;
    616 #endif
    617 		default:
    618 			return EAFNOSUPPORT;
    619 		}
    620 
    621 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
    622 		if (m) {
    623 			MCLAIM(m, &tcp_tx_mowner);
    624 			MCLGET(m, M_DONTWAIT);
    625 			if ((m->m_flags & M_EXT) == 0) {
    626 				m_free(m);
    627 				m = NULL;
    628 			}
    629 		}
    630 		if (m == NULL)
    631 			return (ENOBUFS);
    632 
    633 		if (tcp_compat_42)
    634 			tlen = 1;
    635 		else
    636 			tlen = 0;
    637 
    638 		m->m_data += max_linkhdr;
    639 		bcopy(mtod(template, caddr_t), mtod(m, caddr_t),
    640 			template->m_len);
    641 		switch (family) {
    642 		case AF_INET:
    643 			ip = mtod(m, struct ip *);
    644 			th = (struct tcphdr *)(ip + 1);
    645 			break;
    646 #ifdef INET6
    647 		case AF_INET6:
    648 			ip6 = mtod(m, struct ip6_hdr *);
    649 			th = (struct tcphdr *)(ip6 + 1);
    650 			break;
    651 #endif
    652 #if 0
    653 		default:
    654 			/* noone will visit here */
    655 			m_freem(m);
    656 			return EAFNOSUPPORT;
    657 #endif
    658 		}
    659 		flags = TH_ACK;
    660 	} else {
    661 
    662 		if ((m->m_flags & M_PKTHDR) == 0) {
    663 #if 0
    664 			printf("non PKTHDR to tcp_respond\n");
    665 #endif
    666 			m_freem(m);
    667 			return EINVAL;
    668 		}
    669 #ifdef DIAGNOSTIC
    670 		if (!th0)
    671 			panic("th0 == NULL in tcp_respond");
    672 #endif
    673 
    674 		/* get family information from m */
    675 		switch (mtod(m, struct ip *)->ip_v) {
    676 		case 4:
    677 			family = AF_INET;
    678 			hlen = sizeof(struct ip);
    679 			ip = mtod(m, struct ip *);
    680 			break;
    681 #ifdef INET6
    682 		case 6:
    683 			family = AF_INET6;
    684 			hlen = sizeof(struct ip6_hdr);
    685 			ip6 = mtod(m, struct ip6_hdr *);
    686 			break;
    687 #endif
    688 		default:
    689 			m_freem(m);
    690 			return EAFNOSUPPORT;
    691 		}
    692 		if ((flags & TH_SYN) == 0 || sizeof(*th0) > (th0->th_off << 2))
    693 			tlen = sizeof(*th0);
    694 		else
    695 			tlen = th0->th_off << 2;
    696 
    697 		if (m->m_len > hlen + tlen && (m->m_flags & M_EXT) == 0 &&
    698 		    mtod(m, caddr_t) + hlen == (caddr_t)th0) {
    699 			m->m_len = hlen + tlen;
    700 			m_freem(m->m_next);
    701 			m->m_next = NULL;
    702 		} else {
    703 			struct mbuf *n;
    704 
    705 #ifdef DIAGNOSTIC
    706 			if (max_linkhdr + hlen + tlen > MCLBYTES) {
    707 				m_freem(m);
    708 				return EMSGSIZE;
    709 			}
    710 #endif
    711 			MGETHDR(n, M_DONTWAIT, MT_HEADER);
    712 			if (n && max_linkhdr + hlen + tlen > MHLEN) {
    713 				MCLGET(n, M_DONTWAIT);
    714 				if ((n->m_flags & M_EXT) == 0) {
    715 					m_freem(n);
    716 					n = NULL;
    717 				}
    718 			}
    719 			if (!n) {
    720 				m_freem(m);
    721 				return ENOBUFS;
    722 			}
    723 
    724 			MCLAIM(n, &tcp_tx_mowner);
    725 			n->m_data += max_linkhdr;
    726 			n->m_len = hlen + tlen;
    727 			m_copyback(n, 0, hlen, mtod(m, caddr_t));
    728 			m_copyback(n, hlen, tlen, (caddr_t)th0);
    729 
    730 			m_freem(m);
    731 			m = n;
    732 			n = NULL;
    733 		}
    734 
    735 #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
    736 		switch (family) {
    737 		case AF_INET:
    738 			ip = mtod(m, struct ip *);
    739 			th = (struct tcphdr *)(ip + 1);
    740 			ip->ip_p = IPPROTO_TCP;
    741 			xchg(ip->ip_dst, ip->ip_src, struct in_addr);
    742 			ip->ip_p = IPPROTO_TCP;
    743 			break;
    744 #ifdef INET6
    745 		case AF_INET6:
    746 			ip6 = mtod(m, struct ip6_hdr *);
    747 			th = (struct tcphdr *)(ip6 + 1);
    748 			ip6->ip6_nxt = IPPROTO_TCP;
    749 			xchg(ip6->ip6_dst, ip6->ip6_src, struct in6_addr);
    750 			ip6->ip6_nxt = IPPROTO_TCP;
    751 			break;
    752 #endif
    753 #if 0
    754 		default:
    755 			/* noone will visit here */
    756 			m_freem(m);
    757 			return EAFNOSUPPORT;
    758 #endif
    759 		}
    760 		xchg(th->th_dport, th->th_sport, u_int16_t);
    761 #undef xchg
    762 		tlen = 0;	/*be friendly with the following code*/
    763 	}
    764 	th->th_seq = htonl(seq);
    765 	th->th_ack = htonl(ack);
    766 	th->th_x2 = 0;
    767 	if ((flags & TH_SYN) == 0) {
    768 		if (tp)
    769 			win >>= tp->rcv_scale;
    770 		if (win > TCP_MAXWIN)
    771 			win = TCP_MAXWIN;
    772 		th->th_win = htons((u_int16_t)win);
    773 		th->th_off = sizeof (struct tcphdr) >> 2;
    774 		tlen += sizeof(*th);
    775 	} else
    776 		tlen += th->th_off << 2;
    777 	m->m_len = hlen + tlen;
    778 	m->m_pkthdr.len = hlen + tlen;
    779 	m->m_pkthdr.rcvif = (struct ifnet *) 0;
    780 	th->th_flags = flags;
    781 	th->th_urp = 0;
    782 
    783 	switch (family) {
    784 #ifdef INET
    785 	case AF_INET:
    786 	    {
    787 		struct ipovly *ipov = (struct ipovly *)ip;
    788 		bzero(ipov->ih_x1, sizeof ipov->ih_x1);
    789 		ipov->ih_len = htons((u_int16_t)tlen);
    790 
    791 		th->th_sum = 0;
    792 		th->th_sum = in_cksum(m, hlen + tlen);
    793 		ip->ip_len = htons(hlen + tlen);
    794 		ip->ip_ttl = ip_defttl;
    795 		break;
    796 	    }
    797 #endif
    798 #ifdef INET6
    799 	case AF_INET6:
    800 	    {
    801 		th->th_sum = 0;
    802 		th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
    803 				tlen);
    804 		ip6->ip6_plen = ntohs(tlen);
    805 		if (tp && tp->t_in6pcb) {
    806 			struct ifnet *oifp;
    807 			ro = (struct route *)&tp->t_in6pcb->in6p_route;
    808 			oifp = ro->ro_rt ? ro->ro_rt->rt_ifp : NULL;
    809 			ip6->ip6_hlim = in6_selecthlim(tp->t_in6pcb, oifp);
    810 		} else
    811 			ip6->ip6_hlim = ip6_defhlim;
    812 		ip6->ip6_flow &= ~IPV6_FLOWINFO_MASK;
    813 		if (ip6_auto_flowlabel) {
    814 			ip6->ip6_flow |=
    815 			    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
    816 		}
    817 		break;
    818 	    }
    819 #endif
    820 	}
    821 
    822 	if (tp && tp->t_inpcb)
    823 		so = tp->t_inpcb->inp_socket;
    824 #ifdef INET6
    825 	else if (tp && tp->t_in6pcb)
    826 		so = tp->t_in6pcb->in6p_socket;
    827 #endif
    828 	else
    829 		so = NULL;
    830 
    831 	if (tp != NULL && tp->t_inpcb != NULL) {
    832 		ro = &tp->t_inpcb->inp_route;
    833 #ifdef DIAGNOSTIC
    834 		if (family != AF_INET)
    835 			panic("tcp_respond: address family mismatch");
    836 		if (!in_hosteq(ip->ip_dst, tp->t_inpcb->inp_faddr)) {
    837 			panic("tcp_respond: ip_dst %x != inp_faddr %x",
    838 			    ntohl(ip->ip_dst.s_addr),
    839 			    ntohl(tp->t_inpcb->inp_faddr.s_addr));
    840 		}
    841 #endif
    842 	}
    843 #ifdef INET6
    844 	else if (tp != NULL && tp->t_in6pcb != NULL) {
    845 		ro = (struct route *)&tp->t_in6pcb->in6p_route;
    846 #ifdef DIAGNOSTIC
    847 		if (family == AF_INET) {
    848 			if (!IN6_IS_ADDR_V4MAPPED(&tp->t_in6pcb->in6p_faddr))
    849 				panic("tcp_respond: not mapped addr");
    850 			if (bcmp(&ip->ip_dst,
    851 			    &tp->t_in6pcb->in6p_faddr.s6_addr32[3],
    852 			    sizeof(ip->ip_dst)) != 0) {
    853 				panic("tcp_respond: ip_dst != in6p_faddr");
    854 			}
    855 		} else if (family == AF_INET6) {
    856 			if (!IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
    857 			    &tp->t_in6pcb->in6p_faddr))
    858 				panic("tcp_respond: ip6_dst != in6p_faddr");
    859 		} else
    860 			panic("tcp_respond: address family mismatch");
    861 #endif
    862 	}
    863 #endif
    864 	else
    865 		ro = NULL;
    866 
    867 	switch (family) {
    868 #ifdef INET
    869 	case AF_INET:
    870 		error = ip_output(m, NULL, ro,
    871 		    (tp && tp->t_mtudisc ? IP_MTUDISC : 0),
    872 		    (struct ip_moptions *)0, so);
    873 		break;
    874 #endif
    875 #ifdef INET6
    876 	case AF_INET6:
    877 		error = ip6_output(m, NULL, (struct route_in6 *)ro, 0,
    878 		    (struct ip6_moptions *)0, so, NULL);
    879 		break;
    880 #endif
    881 	default:
    882 		error = EAFNOSUPPORT;
    883 		break;
    884 	}
    885 
    886 	return (error);
    887 }
    888 
    889 /*
    890  * Template TCPCB.  Rather than zeroing a new TCPCB and initializing
    891  * a bunch of members individually, we maintain this template for the
    892  * static and mostly-static components of the TCPCB, and copy it into
    893  * the new TCPCB instead.
    894  */
    895 static struct tcpcb tcpcb_template = {
    896 	/*
    897 	 * If TCP_NTIMERS ever changes, we'll need to update this
    898 	 * initializer.
    899 	 */
    900 	.t_timer = {
    901 		CALLOUT_INITIALIZER,
    902 		CALLOUT_INITIALIZER,
    903 		CALLOUT_INITIALIZER,
    904 		CALLOUT_INITIALIZER,
    905 	},
    906 	.t_delack_ch = CALLOUT_INITIALIZER,
    907 
    908 	.t_srtt = TCPTV_SRTTBASE,
    909 	.t_rttmin = TCPTV_MIN,
    910 
    911 	.snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT,
    912 	.snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT,
    913 };
    914 
    915 /*
    916  * Updates the TCPCB template whenever a parameter that would affect
    917  * the template is changed.
    918  */
    919 void
    920 tcp_tcpcb_template(void)
    921 {
    922 	struct tcpcb *tp = &tcpcb_template;
    923 	int flags;
    924 
    925 	tp->t_peermss = tcp_mssdflt;
    926 	tp->t_ourmss = tcp_mssdflt;
    927 	tp->t_segsz = tcp_mssdflt;
    928 
    929 	flags = 0;
    930 	if (tcp_do_rfc1323 && tcp_do_win_scale)
    931 		flags |= TF_REQ_SCALE;
    932 	if (tcp_do_rfc1323 && tcp_do_timestamps)
    933 		flags |= TF_REQ_TSTMP;
    934 	if (tcp_do_sack == 2)
    935 		flags |= TF_WILL_SACK;
    936 	else if (tcp_do_sack == 1)
    937 		flags |= TF_WILL_SACK|TF_IGNR_RXSACK;
    938 	flags |= TF_CANT_TXSACK;
    939 	tp->t_flags = flags;
    940 
    941 	/*
    942 	 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
    943 	 * rtt estimate.  Set rttvar so that srtt + 2 * rttvar gives
    944 	 * reasonable initial retransmit time.
    945 	 */
    946 	tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << (TCP_RTTVAR_SHIFT + 2 - 1);
    947 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
    948 	    TCPTV_MIN, TCPTV_REXMTMAX);
    949 }
    950 
    951 /*
    952  * Create a new TCP control block, making an
    953  * empty reassembly queue and hooking it to the argument
    954  * protocol control block.
    955  */
    956 struct tcpcb *
    957 tcp_newtcpcb(family, aux)
    958 	int family;	/* selects inpcb, or in6pcb */
    959 	void *aux;
    960 {
    961 	struct tcpcb *tp;
    962 	int i;
    963 
    964 	/* XXX Consider using a pool_cache for speed. */
    965 	tp = pool_get(&tcpcb_pool, PR_NOWAIT);
    966 	if (tp == NULL)
    967 		return (NULL);
    968 	memcpy(tp, &tcpcb_template, sizeof(*tp));
    969 	TAILQ_INIT(&tp->segq);
    970 	TAILQ_INIT(&tp->timeq);
    971 	tp->t_family = family;		/* may be overridden later on */
    972 	LIST_INIT(&tp->t_sc);		/* XXX can template this */
    973 
    974 	/* Don't sweat this loop; hopefully the compiler will unroll it. */
    975 	for (i = 0; i < TCPT_NTIMERS; i++)
    976 		TCP_TIMER_INIT(tp, i);
    977 
    978 	switch (family) {
    979 	case AF_INET:
    980 	    {
    981 		struct inpcb *inp = (struct inpcb *)aux;
    982 
    983 		inp->inp_ip.ip_ttl = ip_defttl;
    984 		inp->inp_ppcb = (caddr_t)tp;
    985 
    986 		tp->t_inpcb = inp;
    987 		tp->t_mtudisc = ip_mtudisc;
    988 		break;
    989 	    }
    990 #ifdef INET6
    991 	case AF_INET6:
    992 	    {
    993 		struct in6pcb *in6p = (struct in6pcb *)aux;
    994 
    995 		in6p->in6p_ip6.ip6_hlim = in6_selecthlim(in6p,
    996 			in6p->in6p_route.ro_rt ? in6p->in6p_route.ro_rt->rt_ifp
    997 					       : NULL);
    998 		in6p->in6p_ppcb = (caddr_t)tp;
    999 
   1000 		tp->t_in6pcb = in6p;
   1001 		/* for IPv6, always try to run path MTU discovery */
   1002 		tp->t_mtudisc = 1;
   1003 		break;
   1004 	    }
   1005 #endif /* INET6 */
   1006 	default:
   1007 		pool_put(&tcpcb_pool, tp);
   1008 		return (NULL);
   1009 	}
   1010 
   1011 	/*
   1012 	 * Initialize our timebase.  When we send timestamps, we take
   1013 	 * the delta from tcp_now -- this means each connection always
   1014 	 * gets a timebase of 0, which makes it, among other things,
   1015 	 * more difficult to determine how long a system has been up,
   1016 	 * and thus how many TCP sequence increments have occurred.
   1017 	 */
   1018 	tp->ts_timebase = tcp_now;
   1019 
   1020 	return (tp);
   1021 }
   1022 
   1023 /*
   1024  * Drop a TCP connection, reporting
   1025  * the specified error.  If connection is synchronized,
   1026  * then send a RST to peer.
   1027  */
   1028 struct tcpcb *
   1029 tcp_drop(tp, errno)
   1030 	struct tcpcb *tp;
   1031 	int errno;
   1032 {
   1033 	struct socket *so = NULL;
   1034 
   1035 #ifdef DIAGNOSTIC
   1036 	if (tp->t_inpcb && tp->t_in6pcb)
   1037 		panic("tcp_drop: both t_inpcb and t_in6pcb are set");
   1038 #endif
   1039 #ifdef INET
   1040 	if (tp->t_inpcb)
   1041 		so = tp->t_inpcb->inp_socket;
   1042 #endif
   1043 #ifdef INET6
   1044 	if (tp->t_in6pcb)
   1045 		so = tp->t_in6pcb->in6p_socket;
   1046 #endif
   1047 	if (!so)
   1048 		return NULL;
   1049 
   1050 	if (TCPS_HAVERCVDSYN(tp->t_state)) {
   1051 		tp->t_state = TCPS_CLOSED;
   1052 		(void) tcp_output(tp);
   1053 		tcpstat.tcps_drops++;
   1054 	} else
   1055 		tcpstat.tcps_conndrops++;
   1056 	if (errno == ETIMEDOUT && tp->t_softerror)
   1057 		errno = tp->t_softerror;
   1058 	so->so_error = errno;
   1059 	return (tcp_close(tp));
   1060 }
   1061 
   1062 /*
   1063  * Return whether this tcpcb is marked as dead, indicating
   1064  * to the calling timer function that no further action should
   1065  * be taken, as we are about to release this tcpcb.  The release
   1066  * of the storage will be done if this is the last timer running.
   1067  *
   1068  * This should be called from the callout handler function after
   1069  * callout_ack() is done, so that the number of invoking timer
   1070  * functions is 0.
   1071  */
   1072 int
   1073 tcp_isdead(tp)
   1074 	struct tcpcb *tp;
   1075 {
   1076 	int dead = (tp->t_flags & TF_DEAD);
   1077 
   1078 	if (__predict_false(dead)) {
   1079 		if (tcp_timers_invoking(tp) > 0)
   1080 				/* not quite there yet -- count separately? */
   1081 			return dead;
   1082 		tcpstat.tcps_delayed_free++;
   1083 		pool_put(&tcpcb_pool, tp);
   1084 	}
   1085 	return dead;
   1086 }
   1087 
   1088 /*
   1089  * Close a TCP control block:
   1090  *	discard all space held by the tcp
   1091  *	discard internet protocol block
   1092  *	wake up any sleepers
   1093  */
   1094 struct tcpcb *
   1095 tcp_close(tp)
   1096 	struct tcpcb *tp;
   1097 {
   1098 	struct inpcb *inp;
   1099 #ifdef INET6
   1100 	struct in6pcb *in6p;
   1101 #endif
   1102 	struct socket *so;
   1103 #ifdef RTV_RTT
   1104 	struct rtentry *rt;
   1105 #endif
   1106 	struct route *ro;
   1107 
   1108 	inp = tp->t_inpcb;
   1109 #ifdef INET6
   1110 	in6p = tp->t_in6pcb;
   1111 #endif
   1112 	so = NULL;
   1113 	ro = NULL;
   1114 	if (inp) {
   1115 		so = inp->inp_socket;
   1116 		ro = &inp->inp_route;
   1117 	}
   1118 #ifdef INET6
   1119 	else if (in6p) {
   1120 		so = in6p->in6p_socket;
   1121 		ro = (struct route *)&in6p->in6p_route;
   1122 	}
   1123 #endif
   1124 
   1125 #ifdef RTV_RTT
   1126 	/*
   1127 	 * If we sent enough data to get some meaningful characteristics,
   1128 	 * save them in the routing entry.  'Enough' is arbitrarily
   1129 	 * defined as the sendpipesize (default 4K) * 16.  This would
   1130 	 * give us 16 rtt samples assuming we only get one sample per
   1131 	 * window (the usual case on a long haul net).  16 samples is
   1132 	 * enough for the srtt filter to converge to within 5% of the correct
   1133 	 * value; fewer samples and we could save a very bogus rtt.
   1134 	 *
   1135 	 * Don't update the default route's characteristics and don't
   1136 	 * update anything that the user "locked".
   1137 	 */
   1138 	if (SEQ_LT(tp->iss + so->so_snd.sb_hiwat * 16, tp->snd_max) &&
   1139 	    ro && (rt = ro->ro_rt) &&
   1140 	    !in_nullhost(satosin(rt_key(rt))->sin_addr)) {
   1141 		u_long i = 0;
   1142 
   1143 		if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) {
   1144 			i = tp->t_srtt *
   1145 			    ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2));
   1146 			if (rt->rt_rmx.rmx_rtt && i)
   1147 				/*
   1148 				 * filter this update to half the old & half
   1149 				 * the new values, converting scale.
   1150 				 * See route.h and tcp_var.h for a
   1151 				 * description of the scaling constants.
   1152 				 */
   1153 				rt->rt_rmx.rmx_rtt =
   1154 				    (rt->rt_rmx.rmx_rtt + i) / 2;
   1155 			else
   1156 				rt->rt_rmx.rmx_rtt = i;
   1157 		}
   1158 		if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) {
   1159 			i = tp->t_rttvar *
   1160 			    ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTTVAR_SHIFT + 2));
   1161 			if (rt->rt_rmx.rmx_rttvar && i)
   1162 				rt->rt_rmx.rmx_rttvar =
   1163 				    (rt->rt_rmx.rmx_rttvar + i) / 2;
   1164 			else
   1165 				rt->rt_rmx.rmx_rttvar = i;
   1166 		}
   1167 		/*
   1168 		 * update the pipelimit (ssthresh) if it has been updated
   1169 		 * already or if a pipesize was specified & the threshhold
   1170 		 * got below half the pipesize.  I.e., wait for bad news
   1171 		 * before we start updating, then update on both good
   1172 		 * and bad news.
   1173 		 */
   1174 		if (((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 &&
   1175 		    (i = tp->snd_ssthresh) && rt->rt_rmx.rmx_ssthresh) ||
   1176 		    i < (rt->rt_rmx.rmx_sendpipe / 2)) {
   1177 			/*
   1178 			 * convert the limit from user data bytes to
   1179 			 * packets then to packet data bytes.
   1180 			 */
   1181 			i = (i + tp->t_segsz / 2) / tp->t_segsz;
   1182 			if (i < 2)
   1183 				i = 2;
   1184 			i *= (u_long)(tp->t_segsz + sizeof (struct tcpiphdr));
   1185 			if (rt->rt_rmx.rmx_ssthresh)
   1186 				rt->rt_rmx.rmx_ssthresh =
   1187 				    (rt->rt_rmx.rmx_ssthresh + i) / 2;
   1188 			else
   1189 				rt->rt_rmx.rmx_ssthresh = i;
   1190 		}
   1191 	}
   1192 #endif /* RTV_RTT */
   1193 	/* free the reassembly queue, if any */
   1194 	TCP_REASS_LOCK(tp);
   1195 	(void) tcp_freeq(tp);
   1196 	TCP_REASS_UNLOCK(tp);
   1197 
   1198 	tcp_canceltimers(tp);
   1199 	TCP_CLEAR_DELACK(tp);
   1200 	syn_cache_cleanup(tp);
   1201 
   1202 	if (tp->t_template) {
   1203 		m_free(tp->t_template);
   1204 		tp->t_template = NULL;
   1205 	}
   1206 	if (tcp_timers_invoking(tp))
   1207 		tp->t_flags |= TF_DEAD;
   1208 	else
   1209 		pool_put(&tcpcb_pool, tp);
   1210 
   1211 	if (inp) {
   1212 		inp->inp_ppcb = 0;
   1213 		soisdisconnected(so);
   1214 		in_pcbdetach(inp);
   1215 	}
   1216 #ifdef INET6
   1217 	else if (in6p) {
   1218 		in6p->in6p_ppcb = 0;
   1219 		soisdisconnected(so);
   1220 		in6_pcbdetach(in6p);
   1221 	}
   1222 #endif
   1223 	tcpstat.tcps_closed++;
   1224 	return ((struct tcpcb *)0);
   1225 }
   1226 
   1227 int
   1228 tcp_freeq(tp)
   1229 	struct tcpcb *tp;
   1230 {
   1231 	struct ipqent *qe;
   1232 	int rv = 0;
   1233 #ifdef TCPREASS_DEBUG
   1234 	int i = 0;
   1235 #endif
   1236 
   1237 	TCP_REASS_LOCK_CHECK(tp);
   1238 
   1239 	while ((qe = TAILQ_FIRST(&tp->segq)) != NULL) {
   1240 #ifdef TCPREASS_DEBUG
   1241 		printf("tcp_freeq[%p,%d]: %u:%u(%u) 0x%02x\n",
   1242 			tp, i++, qe->ipqe_seq, qe->ipqe_seq + qe->ipqe_len,
   1243 			qe->ipqe_len, qe->ipqe_flags & (TH_SYN|TH_FIN|TH_RST));
   1244 #endif
   1245 		TAILQ_REMOVE(&tp->segq, qe, ipqe_q);
   1246 		TAILQ_REMOVE(&tp->timeq, qe, ipqe_timeq);
   1247 		m_freem(qe->ipqe_m);
   1248 		pool_put(&tcpipqent_pool, qe);
   1249 		rv = 1;
   1250 	}
   1251 	return (rv);
   1252 }
   1253 
   1254 /*
   1255  * Protocol drain routine.  Called when memory is in short supply.
   1256  */
   1257 void
   1258 tcp_drain()
   1259 {
   1260 	struct inpcb_hdr *inph;
   1261 	struct tcpcb *tp;
   1262 
   1263 	/*
   1264 	 * Free the sequence queue of all TCP connections.
   1265 	 */
   1266 	CIRCLEQ_FOREACH(inph, &tcbtable.inpt_queue, inph_queue) {
   1267 		switch (inph->inph_af) {
   1268 		case AF_INET:
   1269 			tp = intotcpcb((struct inpcb *)inph);
   1270 			break;
   1271 #ifdef INET6
   1272 		case AF_INET6:
   1273 			tp = in6totcpcb((struct in6pcb *)inph);
   1274 			break;
   1275 #endif
   1276 		default:
   1277 			tp = NULL;
   1278 			break;
   1279 		}
   1280 		if (tp != NULL) {
   1281 			/*
   1282 			 * We may be called from a device's interrupt
   1283 			 * context.  If the tcpcb is already busy,
   1284 			 * just bail out now.
   1285 			 */
   1286 			if (tcp_reass_lock_try(tp) == 0)
   1287 				continue;
   1288 			if (tcp_freeq(tp))
   1289 				tcpstat.tcps_connsdrained++;
   1290 			TCP_REASS_UNLOCK(tp);
   1291 		}
   1292 	}
   1293 }
   1294 
   1295 /*
   1296  * Notify a tcp user of an asynchronous error;
   1297  * store error as soft error, but wake up user
   1298  * (for now, won't do anything until can select for soft error).
   1299  */
   1300 void
   1301 tcp_notify(inp, error)
   1302 	struct inpcb *inp;
   1303 	int error;
   1304 {
   1305 	struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb;
   1306 	struct socket *so = inp->inp_socket;
   1307 
   1308 	/*
   1309 	 * Ignore some errors if we are hooked up.
   1310 	 * If connection hasn't completed, has retransmitted several times,
   1311 	 * and receives a second error, give up now.  This is better
   1312 	 * than waiting a long time to establish a connection that
   1313 	 * can never complete.
   1314 	 */
   1315 	if (tp->t_state == TCPS_ESTABLISHED &&
   1316 	     (error == EHOSTUNREACH || error == ENETUNREACH ||
   1317 	      error == EHOSTDOWN)) {
   1318 		return;
   1319 	} else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 &&
   1320 	    tp->t_rxtshift > 3 && tp->t_softerror)
   1321 		so->so_error = error;
   1322 	else
   1323 		tp->t_softerror = error;
   1324 	wakeup((caddr_t) &so->so_timeo);
   1325 	sorwakeup(so);
   1326 	sowwakeup(so);
   1327 }
   1328 
   1329 #ifdef INET6
   1330 void
   1331 tcp6_notify(in6p, error)
   1332 	struct in6pcb *in6p;
   1333 	int error;
   1334 {
   1335 	struct tcpcb *tp = (struct tcpcb *)in6p->in6p_ppcb;
   1336 	struct socket *so = in6p->in6p_socket;
   1337 
   1338 	/*
   1339 	 * Ignore some errors if we are hooked up.
   1340 	 * If connection hasn't completed, has retransmitted several times,
   1341 	 * and receives a second error, give up now.  This is better
   1342 	 * than waiting a long time to establish a connection that
   1343 	 * can never complete.
   1344 	 */
   1345 	if (tp->t_state == TCPS_ESTABLISHED &&
   1346 	     (error == EHOSTUNREACH || error == ENETUNREACH ||
   1347 	      error == EHOSTDOWN)) {
   1348 		return;
   1349 	} else if (TCPS_HAVEESTABLISHED(tp->t_state) == 0 &&
   1350 	    tp->t_rxtshift > 3 && tp->t_softerror)
   1351 		so->so_error = error;
   1352 	else
   1353 		tp->t_softerror = error;
   1354 	wakeup((caddr_t) &so->so_timeo);
   1355 	sorwakeup(so);
   1356 	sowwakeup(so);
   1357 }
   1358 #endif
   1359 
   1360 #ifdef INET6
   1361 void
   1362 tcp6_ctlinput(cmd, sa, d)
   1363 	int cmd;
   1364 	struct sockaddr *sa;
   1365 	void *d;
   1366 {
   1367 	struct tcphdr th;
   1368 	void (*notify) __P((struct in6pcb *, int)) = tcp6_notify;
   1369 	int nmatch;
   1370 	struct ip6_hdr *ip6;
   1371 	const struct sockaddr_in6 *sa6_src = NULL;
   1372 	struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
   1373 	struct mbuf *m;
   1374 	int off;
   1375 
   1376 	if (sa->sa_family != AF_INET6 ||
   1377 	    sa->sa_len != sizeof(struct sockaddr_in6))
   1378 		return;
   1379 	if ((unsigned)cmd >= PRC_NCMDS)
   1380 		return;
   1381 	else if (cmd == PRC_QUENCH) {
   1382 		/* XXX there's no PRC_QUENCH in IPv6 */
   1383 		notify = tcp6_quench;
   1384 	} else if (PRC_IS_REDIRECT(cmd))
   1385 		notify = in6_rtchange, d = NULL;
   1386 	else if (cmd == PRC_MSGSIZE)
   1387 		; /* special code is present, see below */
   1388 	else if (cmd == PRC_HOSTDEAD)
   1389 		d = NULL;
   1390 	else if (inet6ctlerrmap[cmd] == 0)
   1391 		return;
   1392 
   1393 	/* if the parameter is from icmp6, decode it. */
   1394 	if (d != NULL) {
   1395 		struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d;
   1396 		m = ip6cp->ip6c_m;
   1397 		ip6 = ip6cp->ip6c_ip6;
   1398 		off = ip6cp->ip6c_off;
   1399 		sa6_src = ip6cp->ip6c_src;
   1400 	} else {
   1401 		m = NULL;
   1402 		ip6 = NULL;
   1403 		sa6_src = &sa6_any;
   1404 		off = 0;
   1405 	}
   1406 
   1407 	if (ip6) {
   1408 		/*
   1409 		 * XXX: We assume that when ip6 is non NULL,
   1410 		 * M and OFF are valid.
   1411 		 */
   1412 
   1413 		/* check if we can safely examine src and dst ports */
   1414 		if (m->m_pkthdr.len < off + sizeof(th)) {
   1415 			if (cmd == PRC_MSGSIZE)
   1416 				icmp6_mtudisc_update((struct ip6ctlparam *)d, 0);
   1417 			return;
   1418 		}
   1419 
   1420 		bzero(&th, sizeof(th));
   1421 		m_copydata(m, off, sizeof(th), (caddr_t)&th);
   1422 
   1423 		if (cmd == PRC_MSGSIZE) {
   1424 			int valid = 0;
   1425 
   1426 			/*
   1427 			 * Check to see if we have a valid TCP connection
   1428 			 * corresponding to the address in the ICMPv6 message
   1429 			 * payload.
   1430 			 */
   1431 			if (in6_pcblookup_connect(&tcbtable, &sa6->sin6_addr,
   1432 			    th.th_dport, (struct in6_addr *)&sa6_src->sin6_addr,
   1433 			    th.th_sport, 0))
   1434 				valid++;
   1435 
   1436 			/*
   1437 			 * Depending on the value of "valid" and routing table
   1438 			 * size (mtudisc_{hi,lo}wat), we will:
   1439 			 * - recalcurate the new MTU and create the
   1440 			 *   corresponding routing entry, or
   1441 			 * - ignore the MTU change notification.
   1442 			 */
   1443 			icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
   1444 
   1445 			/*
   1446 			 * no need to call in6_pcbnotify, it should have been
   1447 			 * called via callback if necessary
   1448 			 */
   1449 			return;
   1450 		}
   1451 
   1452 		nmatch = in6_pcbnotify(&tcbtable, sa, th.th_dport,
   1453 		    (struct sockaddr *)sa6_src, th.th_sport, cmd, NULL, notify);
   1454 		if (nmatch == 0 && syn_cache_count &&
   1455 		    (inet6ctlerrmap[cmd] == EHOSTUNREACH ||
   1456 		     inet6ctlerrmap[cmd] == ENETUNREACH ||
   1457 		     inet6ctlerrmap[cmd] == EHOSTDOWN))
   1458 			syn_cache_unreach((struct sockaddr *)sa6_src,
   1459 					  sa, &th);
   1460 	} else {
   1461 		(void) in6_pcbnotify(&tcbtable, sa, 0,
   1462 		    (struct sockaddr *)sa6_src, 0, cmd, NULL, notify);
   1463 	}
   1464 }
   1465 #endif
   1466 
   1467 #ifdef INET
   1468 /* assumes that ip header and tcp header are contiguous on mbuf */
   1469 void *
   1470 tcp_ctlinput(cmd, sa, v)
   1471 	int cmd;
   1472 	struct sockaddr *sa;
   1473 	void *v;
   1474 {
   1475 	struct ip *ip = v;
   1476 	struct tcphdr *th;
   1477 	struct icmp *icp;
   1478 	extern const int inetctlerrmap[];
   1479 	void (*notify) __P((struct inpcb *, int)) = tcp_notify;
   1480 	int errno;
   1481 	int nmatch;
   1482 #ifdef INET6
   1483 	struct in6_addr src6, dst6;
   1484 #endif
   1485 
   1486 	if (sa->sa_family != AF_INET ||
   1487 	    sa->sa_len != sizeof(struct sockaddr_in))
   1488 		return NULL;
   1489 	if ((unsigned)cmd >= PRC_NCMDS)
   1490 		return NULL;
   1491 	errno = inetctlerrmap[cmd];
   1492 	if (cmd == PRC_QUENCH)
   1493 		notify = tcp_quench;
   1494 	else if (PRC_IS_REDIRECT(cmd))
   1495 		notify = in_rtchange, ip = 0;
   1496 	else if (cmd == PRC_MSGSIZE && ip && ip->ip_v == 4) {
   1497 		/*
   1498 		 * Check to see if we have a valid TCP connection
   1499 		 * corresponding to the address in the ICMP message
   1500 		 * payload.
   1501 		 *
   1502 		 * Boundary check is made in icmp_input(), with ICMP_ADVLENMIN.
   1503 		 */
   1504 		th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
   1505 #ifdef INET6
   1506 		memset(&src6, 0, sizeof(src6));
   1507 		memset(&dst6, 0, sizeof(dst6));
   1508 		src6.s6_addr16[5] = dst6.s6_addr16[5] = 0xffff;
   1509 		memcpy(&src6.s6_addr32[3], &ip->ip_src, sizeof(struct in_addr));
   1510 		memcpy(&dst6.s6_addr32[3], &ip->ip_dst, sizeof(struct in_addr));
   1511 #endif
   1512 		if (in_pcblookup_connect(&tcbtable, ip->ip_dst, th->th_dport,
   1513 		    ip->ip_src, th->th_sport) != NULL)
   1514 			;
   1515 #ifdef INET6
   1516 		else if (in6_pcblookup_connect(&tcbtable, &dst6,
   1517 		    th->th_dport, &src6, th->th_sport, 0) != NULL)
   1518 			;
   1519 #endif
   1520 		else
   1521 			return NULL;
   1522 
   1523 		/*
   1524 		 * Now that we've validated that we are actually communicating
   1525 		 * with the host indicated in the ICMP message, locate the
   1526 		 * ICMP header, recalculate the new MTU, and create the
   1527 		 * corresponding routing entry.
   1528 		 */
   1529 		icp = (struct icmp *)((caddr_t)ip -
   1530 		    offsetof(struct icmp, icmp_ip));
   1531 		icmp_mtudisc(icp, ip->ip_dst);
   1532 
   1533 		return NULL;
   1534 	} else if (cmd == PRC_HOSTDEAD)
   1535 		ip = 0;
   1536 	else if (errno == 0)
   1537 		return NULL;
   1538 	if (ip && ip->ip_v == 4 && sa->sa_family == AF_INET) {
   1539 		th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
   1540 		nmatch = in_pcbnotify(&tcbtable, satosin(sa)->sin_addr,
   1541 		    th->th_dport, ip->ip_src, th->th_sport, errno, notify);
   1542 		if (nmatch == 0 && syn_cache_count &&
   1543 		    (inetctlerrmap[cmd] == EHOSTUNREACH ||
   1544 		    inetctlerrmap[cmd] == ENETUNREACH ||
   1545 		    inetctlerrmap[cmd] == EHOSTDOWN)) {
   1546 			struct sockaddr_in sin;
   1547 			bzero(&sin, sizeof(sin));
   1548 			sin.sin_len = sizeof(sin);
   1549 			sin.sin_family = AF_INET;
   1550 			sin.sin_port = th->th_sport;
   1551 			sin.sin_addr = ip->ip_src;
   1552 			syn_cache_unreach((struct sockaddr *)&sin, sa, th);
   1553 		}
   1554 
   1555 		/* XXX mapped address case */
   1556 	} else
   1557 		in_pcbnotifyall(&tcbtable, satosin(sa)->sin_addr, errno,
   1558 		    notify);
   1559 	return NULL;
   1560 }
   1561 
   1562 /*
   1563  * When a source quence is received, we are being notifed of congestion.
   1564  * Close the congestion window down to the Loss Window (one segment).
   1565  * We will gradually open it again as we proceed.
   1566  */
   1567 void
   1568 tcp_quench(inp, errno)
   1569 	struct inpcb *inp;
   1570 	int errno;
   1571 {
   1572 	struct tcpcb *tp = intotcpcb(inp);
   1573 
   1574 	if (tp)
   1575 		tp->snd_cwnd = tp->t_segsz;
   1576 }
   1577 #endif
   1578 
   1579 #ifdef INET6
   1580 void
   1581 tcp6_quench(in6p, errno)
   1582 	struct in6pcb *in6p;
   1583 	int errno;
   1584 {
   1585 	struct tcpcb *tp = in6totcpcb(in6p);
   1586 
   1587 	if (tp)
   1588 		tp->snd_cwnd = tp->t_segsz;
   1589 }
   1590 #endif
   1591 
   1592 #ifdef INET
   1593 /*
   1594  * Path MTU Discovery handlers.
   1595  */
   1596 void
   1597 tcp_mtudisc_callback(faddr)
   1598 	struct in_addr faddr;
   1599 {
   1600 #ifdef INET6
   1601 	struct in6_addr in6;
   1602 #endif
   1603 
   1604 	in_pcbnotifyall(&tcbtable, faddr, EMSGSIZE, tcp_mtudisc);
   1605 #ifdef INET6
   1606 	memset(&in6, 0, sizeof(in6));
   1607 	in6.s6_addr16[5] = 0xffff;
   1608 	memcpy(&in6.s6_addr32[3], &faddr, sizeof(struct in_addr));
   1609 	tcp6_mtudisc_callback(&in6);
   1610 #endif
   1611 }
   1612 
   1613 /*
   1614  * On receipt of path MTU corrections, flush old route and replace it
   1615  * with the new one.  Retransmit all unacknowledged packets, to ensure
   1616  * that all packets will be received.
   1617  */
   1618 void
   1619 tcp_mtudisc(inp, errno)
   1620 	struct inpcb *inp;
   1621 	int errno;
   1622 {
   1623 	struct tcpcb *tp = intotcpcb(inp);
   1624 	struct rtentry *rt = in_pcbrtentry(inp);
   1625 
   1626 	if (tp != 0) {
   1627 		if (rt != 0) {
   1628 			/*
   1629 			 * If this was not a host route, remove and realloc.
   1630 			 */
   1631 			if ((rt->rt_flags & RTF_HOST) == 0) {
   1632 				in_rtchange(inp, errno);
   1633 				if ((rt = in_pcbrtentry(inp)) == 0)
   1634 					return;
   1635 			}
   1636 
   1637 			/*
   1638 			 * Slow start out of the error condition.  We
   1639 			 * use the MTU because we know it's smaller
   1640 			 * than the previously transmitted segment.
   1641 			 *
   1642 			 * Note: This is more conservative than the
   1643 			 * suggestion in draft-floyd-incr-init-win-03.
   1644 			 */
   1645 			if (rt->rt_rmx.rmx_mtu != 0)
   1646 				tp->snd_cwnd =
   1647 				    TCP_INITIAL_WINDOW(tcp_init_win,
   1648 				    rt->rt_rmx.rmx_mtu);
   1649 		}
   1650 
   1651 		/*
   1652 		 * Resend unacknowledged packets.
   1653 		 */
   1654 		tp->snd_nxt = tp->snd_una;
   1655 		tcp_output(tp);
   1656 	}
   1657 }
   1658 #endif
   1659 
   1660 #ifdef INET6
   1661 /*
   1662  * Path MTU Discovery handlers.
   1663  */
   1664 void
   1665 tcp6_mtudisc_callback(faddr)
   1666 	struct in6_addr *faddr;
   1667 {
   1668 	struct sockaddr_in6 sin6;
   1669 
   1670 	bzero(&sin6, sizeof(sin6));
   1671 	sin6.sin6_family = AF_INET6;
   1672 	sin6.sin6_len = sizeof(struct sockaddr_in6);
   1673 	sin6.sin6_addr = *faddr;
   1674 	(void) in6_pcbnotify(&tcbtable, (struct sockaddr *)&sin6, 0,
   1675 	    (struct sockaddr *)&sa6_any, 0, PRC_MSGSIZE, NULL, tcp6_mtudisc);
   1676 }
   1677 
   1678 void
   1679 tcp6_mtudisc(in6p, errno)
   1680 	struct in6pcb *in6p;
   1681 	int errno;
   1682 {
   1683 	struct tcpcb *tp = in6totcpcb(in6p);
   1684 	struct rtentry *rt = in6_pcbrtentry(in6p);
   1685 
   1686 	if (tp != 0) {
   1687 		if (rt != 0) {
   1688 			/*
   1689 			 * If this was not a host route, remove and realloc.
   1690 			 */
   1691 			if ((rt->rt_flags & RTF_HOST) == 0) {
   1692 				in6_rtchange(in6p, errno);
   1693 				if ((rt = in6_pcbrtentry(in6p)) == 0)
   1694 					return;
   1695 			}
   1696 
   1697 			/*
   1698 			 * Slow start out of the error condition.  We
   1699 			 * use the MTU because we know it's smaller
   1700 			 * than the previously transmitted segment.
   1701 			 *
   1702 			 * Note: This is more conservative than the
   1703 			 * suggestion in draft-floyd-incr-init-win-03.
   1704 			 */
   1705 			if (rt->rt_rmx.rmx_mtu != 0)
   1706 				tp->snd_cwnd =
   1707 				    TCP_INITIAL_WINDOW(tcp_init_win,
   1708 				    rt->rt_rmx.rmx_mtu);
   1709 		}
   1710 
   1711 		/*
   1712 		 * Resend unacknowledged packets.
   1713 		 */
   1714 		tp->snd_nxt = tp->snd_una;
   1715 		tcp_output(tp);
   1716 	}
   1717 }
   1718 #endif /* INET6 */
   1719 
   1720 /*
   1721  * Compute the MSS to advertise to the peer.  Called only during
   1722  * the 3-way handshake.  If we are the server (peer initiated
   1723  * connection), we are called with a pointer to the interface
   1724  * on which the SYN packet arrived.  If we are the client (we
   1725  * initiated connection), we are called with a pointer to the
   1726  * interface out which this connection should go.
   1727  *
   1728  * NOTE: Do not subtract IP option/extension header size nor IPsec
   1729  * header size from MSS advertisement.  MSS option must hold the maximum
   1730  * segment size we can accept, so it must always be:
   1731  *	 max(if mtu) - ip header - tcp header
   1732  */
   1733 u_long
   1734 tcp_mss_to_advertise(ifp, af)
   1735 	const struct ifnet *ifp;
   1736 	int af;
   1737 {
   1738 	extern u_long in_maxmtu;
   1739 	u_long mss = 0;
   1740 	u_long hdrsiz;
   1741 
   1742 	/*
   1743 	 * In order to avoid defeating path MTU discovery on the peer,
   1744 	 * we advertise the max MTU of all attached networks as our MSS,
   1745 	 * per RFC 1191, section 3.1.
   1746 	 *
   1747 	 * We provide the option to advertise just the MTU of
   1748 	 * the interface on which we hope this connection will
   1749 	 * be receiving.  If we are responding to a SYN, we
   1750 	 * will have a pretty good idea about this, but when
   1751 	 * initiating a connection there is a bit more doubt.
   1752 	 *
   1753 	 * We also need to ensure that loopback has a large enough
   1754 	 * MSS, as the loopback MTU is never included in in_maxmtu.
   1755 	 */
   1756 
   1757 	if (ifp != NULL)
   1758 		switch (af) {
   1759 		case AF_INET:
   1760 			mss = ifp->if_mtu;
   1761 			break;
   1762 #ifdef INET6
   1763 		case AF_INET6:
   1764 			mss = IN6_LINKMTU(ifp);
   1765 			break;
   1766 #endif
   1767 		}
   1768 
   1769 	if (tcp_mss_ifmtu == 0)
   1770 		switch (af) {
   1771 		case AF_INET:
   1772 			mss = max(in_maxmtu, mss);
   1773 			break;
   1774 #ifdef INET6
   1775 		case AF_INET6:
   1776 			mss = max(in6_maxmtu, mss);
   1777 			break;
   1778 #endif
   1779 		}
   1780 
   1781 	switch (af) {
   1782 	case AF_INET:
   1783 		hdrsiz = sizeof(struct ip);
   1784 		break;
   1785 #ifdef INET6
   1786 	case AF_INET6:
   1787 		hdrsiz = sizeof(struct ip6_hdr);
   1788 		break;
   1789 #endif
   1790 	default:
   1791 		hdrsiz = 0;
   1792 		break;
   1793 	}
   1794 	hdrsiz += sizeof(struct tcphdr);
   1795 	if (mss > hdrsiz)
   1796 		mss -= hdrsiz;
   1797 
   1798 	mss = max(tcp_mssdflt, mss);
   1799 	return (mss);
   1800 }
   1801 
   1802 /*
   1803  * Set connection variables based on the peer's advertised MSS.
   1804  * We are passed the TCPCB for the actual connection.  If we
   1805  * are the server, we are called by the compressed state engine
   1806  * when the 3-way handshake is complete.  If we are the client,
   1807  * we are called when we receive the SYN,ACK from the server.
   1808  *
   1809  * NOTE: Our advertised MSS value must be initialized in the TCPCB
   1810  * before this routine is called!
   1811  */
   1812 void
   1813 tcp_mss_from_peer(tp, offer)
   1814 	struct tcpcb *tp;
   1815 	int offer;
   1816 {
   1817 	struct socket *so;
   1818 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
   1819 	struct rtentry *rt;
   1820 #endif
   1821 	u_long bufsize;
   1822 	int mss;
   1823 
   1824 #ifdef DIAGNOSTIC
   1825 	if (tp->t_inpcb && tp->t_in6pcb)
   1826 		panic("tcp_mss_from_peer: both t_inpcb and t_in6pcb are set");
   1827 #endif
   1828 	so = NULL;
   1829 	rt = NULL;
   1830 #ifdef INET
   1831 	if (tp->t_inpcb) {
   1832 		so = tp->t_inpcb->inp_socket;
   1833 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
   1834 		rt = in_pcbrtentry(tp->t_inpcb);
   1835 #endif
   1836 	}
   1837 #endif
   1838 #ifdef INET6
   1839 	if (tp->t_in6pcb) {
   1840 		so = tp->t_in6pcb->in6p_socket;
   1841 #if defined(RTV_SPIPE) || defined(RTV_SSTHRESH)
   1842 		rt = in6_pcbrtentry(tp->t_in6pcb);
   1843 #endif
   1844 	}
   1845 #endif
   1846 
   1847 	/*
   1848 	 * As per RFC1122, use the default MSS value, unless they
   1849 	 * sent us an offer.  Do not accept offers less than 256 bytes.
   1850 	 */
   1851 	mss = tcp_mssdflt;
   1852 	if (offer)
   1853 		mss = offer;
   1854 	mss = max(mss, 256);		/* sanity */
   1855 	tp->t_peermss = mss;
   1856 	mss -= tcp_optlen(tp);
   1857 #ifdef INET
   1858 	if (tp->t_inpcb)
   1859 		mss -= ip_optlen(tp->t_inpcb);
   1860 #endif
   1861 #ifdef INET6
   1862 	if (tp->t_in6pcb)
   1863 		mss -= ip6_optlen(tp->t_in6pcb);
   1864 #endif
   1865 
   1866 	/*
   1867 	 * If there's a pipesize, change the socket buffer to that size.
   1868 	 * Make the socket buffer an integral number of MSS units.  If
   1869 	 * the MSS is larger than the socket buffer, artificially decrease
   1870 	 * the MSS.
   1871 	 */
   1872 #ifdef RTV_SPIPE
   1873 	if (rt != NULL && rt->rt_rmx.rmx_sendpipe != 0)
   1874 		bufsize = rt->rt_rmx.rmx_sendpipe;
   1875 	else
   1876 #endif
   1877 		bufsize = so->so_snd.sb_hiwat;
   1878 	if (bufsize < mss)
   1879 		mss = bufsize;
   1880 	else {
   1881 		bufsize = roundup(bufsize, mss);
   1882 		if (bufsize > sb_max)
   1883 			bufsize = sb_max;
   1884 		(void) sbreserve(&so->so_snd, bufsize, so);
   1885 	}
   1886 	tp->t_segsz = mss;
   1887 
   1888 #ifdef RTV_SSTHRESH
   1889 	if (rt != NULL && rt->rt_rmx.rmx_ssthresh) {
   1890 		/*
   1891 		 * There's some sort of gateway or interface buffer
   1892 		 * limit on the path.  Use this to set the slow
   1893 		 * start threshold, but set the threshold to no less
   1894 		 * than 2 * MSS.
   1895 		 */
   1896 		tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
   1897 	}
   1898 #endif
   1899 }
   1900 
   1901 /*
   1902  * Processing necessary when a TCP connection is established.
   1903  */
   1904 void
   1905 tcp_established(tp)
   1906 	struct tcpcb *tp;
   1907 {
   1908 	struct socket *so;
   1909 #ifdef RTV_RPIPE
   1910 	struct rtentry *rt;
   1911 #endif
   1912 	u_long bufsize;
   1913 
   1914 #ifdef DIAGNOSTIC
   1915 	if (tp->t_inpcb && tp->t_in6pcb)
   1916 		panic("tcp_established: both t_inpcb and t_in6pcb are set");
   1917 #endif
   1918 	so = NULL;
   1919 	rt = NULL;
   1920 #ifdef INET
   1921 	if (tp->t_inpcb) {
   1922 		so = tp->t_inpcb->inp_socket;
   1923 #if defined(RTV_RPIPE)
   1924 		rt = in_pcbrtentry(tp->t_inpcb);
   1925 #endif
   1926 	}
   1927 #endif
   1928 #ifdef INET6
   1929 	if (tp->t_in6pcb) {
   1930 		so = tp->t_in6pcb->in6p_socket;
   1931 #if defined(RTV_RPIPE)
   1932 		rt = in6_pcbrtentry(tp->t_in6pcb);
   1933 #endif
   1934 	}
   1935 #endif
   1936 
   1937 	tp->t_state = TCPS_ESTABLISHED;
   1938 	TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle);
   1939 
   1940 #ifdef RTV_RPIPE
   1941 	if (rt != NULL && rt->rt_rmx.rmx_recvpipe != 0)
   1942 		bufsize = rt->rt_rmx.rmx_recvpipe;
   1943 	else
   1944 #endif
   1945 		bufsize = so->so_rcv.sb_hiwat;
   1946 	if (bufsize > tp->t_ourmss) {
   1947 		bufsize = roundup(bufsize, tp->t_ourmss);
   1948 		if (bufsize > sb_max)
   1949 			bufsize = sb_max;
   1950 		(void) sbreserve(&so->so_rcv, bufsize, so);
   1951 	}
   1952 }
   1953 
   1954 /*
   1955  * Check if there's an initial rtt or rttvar.  Convert from the
   1956  * route-table units to scaled multiples of the slow timeout timer.
   1957  * Called only during the 3-way handshake.
   1958  */
   1959 void
   1960 tcp_rmx_rtt(tp)
   1961 	struct tcpcb *tp;
   1962 {
   1963 #ifdef RTV_RTT
   1964 	struct rtentry *rt = NULL;
   1965 	int rtt;
   1966 
   1967 #ifdef DIAGNOSTIC
   1968 	if (tp->t_inpcb && tp->t_in6pcb)
   1969 		panic("tcp_rmx_rtt: both t_inpcb and t_in6pcb are set");
   1970 #endif
   1971 #ifdef INET
   1972 	if (tp->t_inpcb)
   1973 		rt = in_pcbrtentry(tp->t_inpcb);
   1974 #endif
   1975 #ifdef INET6
   1976 	if (tp->t_in6pcb)
   1977 		rt = in6_pcbrtentry(tp->t_in6pcb);
   1978 #endif
   1979 	if (rt == NULL)
   1980 		return;
   1981 
   1982 	if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
   1983 		/*
   1984 		 * XXX The lock bit for MTU indicates that the value
   1985 		 * is also a minimum value; this is subject to time.
   1986 		 */
   1987 		if (rt->rt_rmx.rmx_locks & RTV_RTT)
   1988 			TCPT_RANGESET(tp->t_rttmin,
   1989 			    rtt / (RTM_RTTUNIT / PR_SLOWHZ),
   1990 			    TCPTV_MIN, TCPTV_REXMTMAX);
   1991 		tp->t_srtt = rtt /
   1992 		    ((RTM_RTTUNIT / PR_SLOWHZ) >> (TCP_RTT_SHIFT + 2));
   1993 		if (rt->rt_rmx.rmx_rttvar) {
   1994 			tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
   1995 			    ((RTM_RTTUNIT / PR_SLOWHZ) >>
   1996 				(TCP_RTTVAR_SHIFT + 2));
   1997 		} else {
   1998 			/* Default variation is +- 1 rtt */
   1999 			tp->t_rttvar =
   2000 			    tp->t_srtt >> (TCP_RTT_SHIFT - TCP_RTTVAR_SHIFT);
   2001 		}
   2002 		TCPT_RANGESET(tp->t_rxtcur,
   2003 		    ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2),
   2004 		    tp->t_rttmin, TCPTV_REXMTMAX);
   2005 	}
   2006 #endif
   2007 }
   2008 
   2009 tcp_seq	 tcp_iss_seq = 0;	/* tcp initial seq # */
   2010 #if NRND > 0
   2011 u_int8_t tcp_iss_secret[16];	/* 128 bits; should be plenty */
   2012 #endif
   2013 
   2014 /*
   2015  * Get a new sequence value given a tcp control block
   2016  */
   2017 tcp_seq
   2018 tcp_new_iss(struct tcpcb *tp, tcp_seq addin)
   2019 {
   2020 
   2021 #ifdef INET
   2022 	if (tp->t_inpcb != NULL) {
   2023 		return (tcp_new_iss1(&tp->t_inpcb->inp_laddr,
   2024 		    &tp->t_inpcb->inp_faddr, tp->t_inpcb->inp_lport,
   2025 		    tp->t_inpcb->inp_fport, sizeof(tp->t_inpcb->inp_laddr),
   2026 		    addin));
   2027 	}
   2028 #endif
   2029 #ifdef INET6
   2030 	if (tp->t_in6pcb != NULL) {
   2031 		return (tcp_new_iss1(&tp->t_in6pcb->in6p_laddr,
   2032 		    &tp->t_in6pcb->in6p_faddr, tp->t_in6pcb->in6p_lport,
   2033 		    tp->t_in6pcb->in6p_fport, sizeof(tp->t_in6pcb->in6p_laddr),
   2034 		    addin));
   2035 	}
   2036 #endif
   2037 	/* Not possible. */
   2038 	panic("tcp_new_iss");
   2039 }
   2040 
   2041 /*
   2042  * This routine actually generates a new TCP initial sequence number.
   2043  */
   2044 tcp_seq
   2045 tcp_new_iss1(void *laddr, void *faddr, u_int16_t lport, u_int16_t fport,
   2046     size_t addrsz, tcp_seq addin)
   2047 {
   2048 	tcp_seq tcp_iss;
   2049 
   2050 #if NRND > 0
   2051 	static int beenhere;
   2052 
   2053 	/*
   2054 	 * If we haven't been here before, initialize our cryptographic
   2055 	 * hash secret.
   2056 	 */
   2057 	if (beenhere == 0) {
   2058 		rnd_extract_data(tcp_iss_secret, sizeof(tcp_iss_secret),
   2059 		    RND_EXTRACT_ANY);
   2060 		beenhere = 1;
   2061 	}
   2062 
   2063 	if (tcp_do_rfc1948) {
   2064 		MD5_CTX ctx;
   2065 		u_int8_t hash[16];	/* XXX MD5 knowledge */
   2066 
   2067 		/*
   2068 		 * Compute the base value of the ISS.  It is a hash
   2069 		 * of (saddr, sport, daddr, dport, secret).
   2070 		 */
   2071 		MD5Init(&ctx);
   2072 
   2073 		MD5Update(&ctx, (u_char *) laddr, addrsz);
   2074 		MD5Update(&ctx, (u_char *) &lport, sizeof(lport));
   2075 
   2076 		MD5Update(&ctx, (u_char *) faddr, addrsz);
   2077 		MD5Update(&ctx, (u_char *) &fport, sizeof(fport));
   2078 
   2079 		MD5Update(&ctx, tcp_iss_secret, sizeof(tcp_iss_secret));
   2080 
   2081 		MD5Final(hash, &ctx);
   2082 
   2083 		memcpy(&tcp_iss, hash, sizeof(tcp_iss));
   2084 
   2085 		/*
   2086 		 * Now increment our "timer", and add it in to
   2087 		 * the computed value.
   2088 		 *
   2089 		 * XXX Use `addin'?
   2090 		 * XXX TCP_ISSINCR too large to use?
   2091 		 */
   2092 		tcp_iss_seq += TCP_ISSINCR;
   2093 #ifdef TCPISS_DEBUG
   2094 		printf("ISS hash 0x%08x, ", tcp_iss);
   2095 #endif
   2096 		tcp_iss += tcp_iss_seq + addin;
   2097 #ifdef TCPISS_DEBUG
   2098 		printf("new ISS 0x%08x\n", tcp_iss);
   2099 #endif
   2100 	} else
   2101 #endif /* NRND > 0 */
   2102 	{
   2103 		/*
   2104 		 * Randomize.
   2105 		 */
   2106 #if NRND > 0
   2107 		rnd_extract_data(&tcp_iss, sizeof(tcp_iss), RND_EXTRACT_ANY);
   2108 #else
   2109 		tcp_iss = arc4random();
   2110 #endif
   2111 
   2112 		/*
   2113 		 * If we were asked to add some amount to a known value,
   2114 		 * we will take a random value obtained above, mask off
   2115 		 * the upper bits, and add in the known value.  We also
   2116 		 * add in a constant to ensure that we are at least a
   2117 		 * certain distance from the original value.
   2118 		 *
   2119 		 * This is used when an old connection is in timed wait
   2120 		 * and we have a new one coming in, for instance.
   2121 		 */
   2122 		if (addin != 0) {
   2123 #ifdef TCPISS_DEBUG
   2124 			printf("Random %08x, ", tcp_iss);
   2125 #endif
   2126 			tcp_iss &= TCP_ISS_RANDOM_MASK;
   2127 			tcp_iss += addin + TCP_ISSINCR;
   2128 #ifdef TCPISS_DEBUG
   2129 			printf("Old ISS %08x, ISS %08x\n", addin, tcp_iss);
   2130 #endif
   2131 		} else {
   2132 			tcp_iss &= TCP_ISS_RANDOM_MASK;
   2133 			tcp_iss += tcp_iss_seq;
   2134 			tcp_iss_seq += TCP_ISSINCR;
   2135 #ifdef TCPISS_DEBUG
   2136 			printf("ISS %08x\n", tcp_iss);
   2137 #endif
   2138 		}
   2139 	}
   2140 
   2141 	if (tcp_compat_42) {
   2142 		/*
   2143 		 * Limit it to the positive range for really old TCP
   2144 		 * implementations.
   2145 		 * Just AND off the top bit instead of checking if
   2146 		 * is set first - saves a branch 50% of the time.
   2147 		 */
   2148 		tcp_iss &= 0x7fffffff;		/* XXX */
   2149 	}
   2150 
   2151 	return (tcp_iss);
   2152 }
   2153 
   2154 #if defined(IPSEC) || defined(FAST_IPSEC)
   2155 /* compute ESP/AH header size for TCP, including outer IP header. */
   2156 size_t
   2157 ipsec4_hdrsiz_tcp(tp)
   2158 	struct tcpcb *tp;
   2159 {
   2160 	struct inpcb *inp;
   2161 	size_t hdrsiz;
   2162 
   2163 	/* XXX mapped addr case (tp->t_in6pcb) */
   2164 	if (!tp || !tp->t_template || !(inp = tp->t_inpcb))
   2165 		return 0;
   2166 	switch (tp->t_family) {
   2167 	case AF_INET:
   2168 		/* XXX: should use currect direction. */
   2169 		hdrsiz = ipsec4_hdrsiz(tp->t_template, IPSEC_DIR_OUTBOUND, inp);
   2170 		break;
   2171 	default:
   2172 		hdrsiz = 0;
   2173 		break;
   2174 	}
   2175 
   2176 	return hdrsiz;
   2177 }
   2178 
   2179 #ifdef INET6
   2180 size_t
   2181 ipsec6_hdrsiz_tcp(tp)
   2182 	struct tcpcb *tp;
   2183 {
   2184 	struct in6pcb *in6p;
   2185 	size_t hdrsiz;
   2186 
   2187 	if (!tp || !tp->t_template || !(in6p = tp->t_in6pcb))
   2188 		return 0;
   2189 	switch (tp->t_family) {
   2190 	case AF_INET6:
   2191 		/* XXX: should use currect direction. */
   2192 		hdrsiz = ipsec6_hdrsiz(tp->t_template, IPSEC_DIR_OUTBOUND, in6p);
   2193 		break;
   2194 	case AF_INET:
   2195 		/* mapped address case - tricky */
   2196 	default:
   2197 		hdrsiz = 0;
   2198 		break;
   2199 	}
   2200 
   2201 	return hdrsiz;
   2202 }
   2203 #endif
   2204 #endif /*IPSEC*/
   2205 
   2206 /*
   2207  * Determine the length of the TCP options for this connection.
   2208  *
   2209  * XXX:  What do we do for SACK, when we add that?  Just reserve
   2210  *       all of the space?  Otherwise we can't exactly be incrementing
   2211  *       cwnd by an amount that varies depending on the amount we last
   2212  *       had to SACK!
   2213  */
   2214 
   2215 u_int
   2216 tcp_optlen(tp)
   2217 	struct tcpcb *tp;
   2218 {
   2219 	u_int optlen;
   2220 
   2221 	optlen = 0;
   2222 	if ((tp->t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP|TF_NOOPT)) ==
   2223 	    (TF_REQ_TSTMP | TF_RCVD_TSTMP))
   2224 		optlen += TCPOLEN_TSTAMP_APPA;
   2225 
   2226 #ifdef TCP_SIGNATURE
   2227 #if defined(INET6) && defined(FAST_IPSEC)
   2228 	if (tp->t_family == AF_INET)
   2229 #endif
   2230 	if (tp->t_flags & TF_SIGNATURE)
   2231 		optlen += TCPOLEN_SIGNATURE + 2;
   2232 #endif /* TCP_SIGNATURE */
   2233 
   2234 	return optlen;
   2235 }
   2236