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