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