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