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