Home | History | Annotate | Line # | Download | only in netinet
tcp_input.c revision 1.109
      1 /*	$NetBSD: tcp_input.c,v 1.109 2000/06/30 16:44:33 itojun Exp $	*/
      2 
      3 /*
      4 %%% portions-copyright-nrl-95
      5 Portions of this software are Copyright 1995-1998 by Randall Atkinson,
      6 Ronald Lee, Daniel McDonald, Bao Phan, and Chris Winters. All Rights
      7 Reserved. All rights under this copyright have been assigned to the US
      8 Naval Research Laboratory (NRL). The NRL Copyright Notice and License
      9 Agreement Version 1.1 (January 17, 1995) applies to these portions of the
     10 software.
     11 You should have received a copy of the license with this software. If you
     12 didn't get a copy, you may request one from <license (at) ipv6.nrl.navy.mil>.
     13 
     14 */
     15 
     16 /*
     17  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
     18  * All rights reserved.
     19  *
     20  * Redistribution and use in source and binary forms, with or without
     21  * modification, are permitted provided that the following conditions
     22  * are met:
     23  * 1. Redistributions of source code must retain the above copyright
     24  *    notice, this list of conditions and the following disclaimer.
     25  * 2. Redistributions in binary form must reproduce the above copyright
     26  *    notice, this list of conditions and the following disclaimer in the
     27  *    documentation and/or other materials provided with the distribution.
     28  * 3. Neither the name of the project nor the names of its contributors
     29  *    may be used to endorse or promote products derived from this software
     30  *    without specific prior written permission.
     31  *
     32  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     42  * SUCH DAMAGE.
     43  */
     44 
     45 /*-
     46  * Copyright (c) 1997, 1998, 1999 The NetBSD Foundation, Inc.
     47  * All rights reserved.
     48  *
     49  * This code is derived from software contributed to The NetBSD Foundation
     50  * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
     51  * Facility, NASA Ames Research Center.
     52  *
     53  * Redistribution and use in source and binary forms, with or without
     54  * modification, are permitted provided that the following conditions
     55  * are met:
     56  * 1. Redistributions of source code must retain the above copyright
     57  *    notice, this list of conditions and the following disclaimer.
     58  * 2. Redistributions in binary form must reproduce the above copyright
     59  *    notice, this list of conditions and the following disclaimer in the
     60  *    documentation and/or other materials provided with the distribution.
     61  * 3. All advertising materials mentioning features or use of this software
     62  *    must display the following acknowledgement:
     63  *	This product includes software developed by the NetBSD
     64  *	Foundation, Inc. and its contributors.
     65  * 4. Neither the name of The NetBSD Foundation nor the names of its
     66  *    contributors may be used to endorse or promote products derived
     67  *    from this software without specific prior written permission.
     68  *
     69  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     70  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     71  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     72  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     73  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     74  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     75  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     76  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     77  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     78  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     79  * POSSIBILITY OF SUCH DAMAGE.
     80  */
     81 
     82 /*
     83  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
     84  *	The Regents of the University of California.  All rights reserved.
     85  *
     86  * Redistribution and use in source and binary forms, with or without
     87  * modification, are permitted provided that the following conditions
     88  * are met:
     89  * 1. Redistributions of source code must retain the above copyright
     90  *    notice, this list of conditions and the following disclaimer.
     91  * 2. Redistributions in binary form must reproduce the above copyright
     92  *    notice, this list of conditions and the following disclaimer in the
     93  *    documentation and/or other materials provided with the distribution.
     94  * 3. All advertising materials mentioning features or use of this software
     95  *    must display the following acknowledgement:
     96  *	This product includes software developed by the University of
     97  *	California, Berkeley and its contributors.
     98  * 4. Neither the name of the University nor the names of its contributors
     99  *    may be used to endorse or promote products derived from this software
    100  *    without specific prior written permission.
    101  *
    102  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
    103  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    104  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    105  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    106  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    107  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    108  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    109  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    110  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    111  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    112  * SUCH DAMAGE.
    113  *
    114  *	@(#)tcp_input.c	8.12 (Berkeley) 5/24/95
    115  */
    116 
    117 /*
    118  *	TODO list for SYN cache stuff:
    119  *
    120  *	Find room for a "state" field, which is needed to keep a
    121  *	compressed state for TIME_WAIT TCBs.  It's been noted already
    122  *	that this is fairly important for very high-volume web and
    123  *	mail servers, which use a large number of short-lived
    124  *	connections.
    125  */
    126 
    127 #include "opt_inet.h"
    128 #include "opt_ipsec.h"
    129 
    130 #include <sys/param.h>
    131 #include <sys/systm.h>
    132 #include <sys/malloc.h>
    133 #include <sys/mbuf.h>
    134 #include <sys/protosw.h>
    135 #include <sys/socket.h>
    136 #include <sys/socketvar.h>
    137 #include <sys/errno.h>
    138 #include <sys/syslog.h>
    139 #include <sys/pool.h>
    140 #include <sys/domain.h>
    141 
    142 #include <net/if.h>
    143 #include <net/route.h>
    144 #include <net/if_types.h>
    145 
    146 #include <netinet/in.h>
    147 #include <netinet/in_systm.h>
    148 #include <netinet/ip.h>
    149 #include <netinet/in_pcb.h>
    150 #include <netinet/ip_var.h>
    151 
    152 #ifdef INET6
    153 #ifndef INET
    154 #include <netinet/in.h>
    155 #endif
    156 #include <netinet/ip6.h>
    157 #include <netinet6/in6_pcb.h>
    158 #include <netinet6/ip6_var.h>
    159 #include <netinet6/in6_var.h>
    160 #include <netinet/icmp6.h>
    161 #include <netinet6/nd6.h>
    162 #endif
    163 
    164 #ifdef PULLDOWN_TEST
    165 #ifndef INET6
    166 /* always need ip6.h for IP6_EXTHDR_GET */
    167 #include <netinet/ip6.h>
    168 #endif
    169 #endif
    170 
    171 #include <netinet/tcp.h>
    172 #include <netinet/tcp_fsm.h>
    173 #include <netinet/tcp_seq.h>
    174 #include <netinet/tcp_timer.h>
    175 #include <netinet/tcp_var.h>
    176 #include <netinet/tcpip.h>
    177 #include <netinet/tcp_debug.h>
    178 
    179 #include <machine/stdarg.h>
    180 
    181 #ifdef IPSEC
    182 #include <netinet6/ipsec.h>
    183 #include <netkey/key.h>
    184 #include <netkey/key_debug.h>
    185 #endif /*IPSEC*/
    186 #ifdef INET6
    187 #include "faith.h"
    188 #endif
    189 
    190 int	tcprexmtthresh = 3;
    191 int	tcp_log_refused;
    192 
    193 struct timeval tcp_rst_ratelim_last;
    194 
    195 #define TCP_PAWS_IDLE	(24 * 24 * 60 * 60 * PR_SLOWHZ)
    196 
    197 /* for modulo comparisons of timestamps */
    198 #define TSTMP_LT(a,b)	((int)((a)-(b)) < 0)
    199 #define TSTMP_GEQ(a,b)	((int)((a)-(b)) >= 0)
    200 
    201 /*
    202  * Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint.
    203  */
    204 #ifdef INET6
    205 #define ND6_HINT(tp) \
    206 do { \
    207 	if (tp && tp->t_in6pcb && tp->t_family == AF_INET6 \
    208 	 && tp->t_in6pcb->in6p_route.ro_rt) { \
    209 		nd6_nud_hint(tp->t_in6pcb->in6p_route.ro_rt, NULL); \
    210 	} \
    211 } while (0)
    212 #else
    213 #define ND6_HINT(tp)
    214 #endif
    215 
    216 /*
    217  * Macro to compute ACK transmission behavior.  Delay the ACK unless
    218  * we have already delayed an ACK (must send an ACK every two segments).
    219  * We also ACK immediately if we received a PUSH and the ACK-on-PUSH
    220  * option is enabled.
    221  */
    222 #define	TCP_SETUP_ACK(tp, th) \
    223 do { \
    224 	if ((tp)->t_flags & TF_DELACK || \
    225 	    (tcp_ack_on_push && (th)->th_flags & TH_PUSH)) \
    226 		tp->t_flags |= TF_ACKNOW; \
    227 	else \
    228 		TCP_SET_DELACK(tp); \
    229 } while (0)
    230 
    231 /*
    232  * Convert TCP protocol fields to host order for easier processing.
    233  */
    234 #define	TCP_FIELDS_TO_HOST(th)						\
    235 do {									\
    236 	NTOHL((th)->th_seq);						\
    237 	NTOHL((th)->th_ack);						\
    238 	NTOHS((th)->th_win);						\
    239 	NTOHS((th)->th_urp);						\
    240 } while (0)
    241 
    242 int
    243 tcp_reass(tp, th, m, tlen)
    244 	struct tcpcb *tp;
    245 	struct tcphdr *th;
    246 	struct mbuf *m;
    247 	int *tlen;
    248 {
    249 	struct ipqent *p, *q, *nq, *tiqe = NULL;
    250 	struct socket *so = NULL;
    251 	int pkt_flags;
    252 	tcp_seq pkt_seq;
    253 	unsigned pkt_len;
    254 	u_long rcvpartdupbyte = 0;
    255 	u_long rcvoobyte;
    256 
    257 	if (tp->t_inpcb)
    258 		so = tp->t_inpcb->inp_socket;
    259 #ifdef INET6
    260 	else if (tp->t_in6pcb)
    261 		so = tp->t_in6pcb->in6p_socket;
    262 #endif
    263 
    264 	TCP_REASS_LOCK_CHECK(tp);
    265 
    266 	/*
    267 	 * Call with th==0 after become established to
    268 	 * force pre-ESTABLISHED data up to user socket.
    269 	 */
    270 	if (th == 0)
    271 		goto present;
    272 
    273 	rcvoobyte = *tlen;
    274 	/*
    275 	 * Copy these to local variables because the tcpiphdr
    276 	 * gets munged while we are collapsing mbufs.
    277 	 */
    278 	pkt_seq = th->th_seq;
    279 	pkt_len = *tlen;
    280 	pkt_flags = th->th_flags;
    281 	/*
    282 	 * Find a segment which begins after this one does.
    283 	 */
    284 	for (p = NULL, q = tp->segq.lh_first; q != NULL; q = nq) {
    285 		nq = q->ipqe_q.le_next;
    286 		/*
    287 		 * If the received segment is just right after this
    288 		 * fragment, merge the two together and then check
    289 		 * for further overlaps.
    290 		 */
    291 		if (q->ipqe_seq + q->ipqe_len == pkt_seq) {
    292 #ifdef TCPREASS_DEBUG
    293 			printf("tcp_reass[%p]: concat %u:%u(%u) to %u:%u(%u)\n",
    294 			       tp, pkt_seq, pkt_seq + pkt_len, pkt_len,
    295 			       q->ipqe_seq, q->ipqe_seq + q->ipqe_len, q->ipqe_len);
    296 #endif
    297 			pkt_len += q->ipqe_len;
    298 			pkt_flags |= q->ipqe_flags;
    299 			pkt_seq = q->ipqe_seq;
    300 			m_cat(q->ipqe_m, m);
    301 			m = q->ipqe_m;
    302 			goto free_ipqe;
    303 		}
    304 		/*
    305 		 * If the received segment is completely past this
    306 		 * fragment, we need to go the next fragment.
    307 		 */
    308 		if (SEQ_LT(q->ipqe_seq + q->ipqe_len, pkt_seq)) {
    309 			p = q;
    310 			continue;
    311 		}
    312 		/*
    313 		 * If the fragment is past the received segment,
    314 		 * it (or any following) can't be concatenated.
    315 		 */
    316 		if (SEQ_GT(q->ipqe_seq, pkt_seq + pkt_len))
    317 			break;
    318 		/*
    319 		 * We've received all the data in this segment before.
    320 		 * mark it as a duplicate and return.
    321 		 */
    322 		if (SEQ_LEQ(q->ipqe_seq, pkt_seq) &&
    323 		    SEQ_GEQ(q->ipqe_seq + q->ipqe_len, pkt_seq + pkt_len)) {
    324 			tcpstat.tcps_rcvduppack++;
    325 			tcpstat.tcps_rcvdupbyte += pkt_len;
    326 			m_freem(m);
    327 			if (tiqe != NULL)
    328 				pool_put(&ipqent_pool, tiqe);
    329 			return (0);
    330 		}
    331 		/*
    332 		 * Received segment completely overlaps this fragment
    333 		 * so we drop the fragment (this keeps the temporal
    334 		 * ordering of segments correct).
    335 		 */
    336 		if (SEQ_GEQ(q->ipqe_seq, pkt_seq) &&
    337 		    SEQ_LEQ(q->ipqe_seq + q->ipqe_len, pkt_seq + pkt_len)) {
    338 			rcvpartdupbyte += q->ipqe_len;
    339 			m_freem(q->ipqe_m);
    340 			goto free_ipqe;
    341 		}
    342 		/*
    343 		 * RX'ed segment extends past the end of the
    344 		 * fragment.  Drop the overlapping bytes.  Then
    345 		 * merge the fragment and segment then treat as
    346 		 * a longer received packet.
    347 		 */
    348 		if (SEQ_LT(q->ipqe_seq, pkt_seq)
    349 		    && SEQ_GT(q->ipqe_seq + q->ipqe_len, pkt_seq))  {
    350 			int overlap = q->ipqe_seq + q->ipqe_len - pkt_seq;
    351 #ifdef TCPREASS_DEBUG
    352 			printf("tcp_reass[%p]: trim starting %d bytes of %u:%u(%u)\n",
    353 			       tp, overlap,
    354 			       pkt_seq, pkt_seq + pkt_len, pkt_len);
    355 #endif
    356 			m_adj(m, overlap);
    357 			rcvpartdupbyte += overlap;
    358 			m_cat(q->ipqe_m, m);
    359 			m = q->ipqe_m;
    360 			pkt_seq = q->ipqe_seq;
    361 			pkt_len += q->ipqe_len - overlap;
    362 			rcvoobyte -= overlap;
    363 			goto free_ipqe;
    364 		}
    365 		/*
    366 		 * RX'ed segment extends past the front of the
    367 		 * fragment.  Drop the overlapping bytes on the
    368 		 * received packet.  The packet will then be
    369 		 * contatentated with this fragment a bit later.
    370 		 */
    371 		if (SEQ_GT(q->ipqe_seq, pkt_seq)
    372 		    && SEQ_LT(q->ipqe_seq, pkt_seq + pkt_len))  {
    373 			int overlap = pkt_seq + pkt_len - q->ipqe_seq;
    374 #ifdef TCPREASS_DEBUG
    375 			printf("tcp_reass[%p]: trim trailing %d bytes of %u:%u(%u)\n",
    376 			       tp, overlap,
    377 			       pkt_seq, pkt_seq + pkt_len, pkt_len);
    378 #endif
    379 			m_adj(m, -overlap);
    380 			pkt_len -= overlap;
    381 			rcvpartdupbyte += overlap;
    382 			rcvoobyte -= overlap;
    383 		}
    384 		/*
    385 		 * If the received segment immediates precedes this
    386 		 * fragment then tack the fragment onto this segment
    387 		 * and reinsert the data.
    388 		 */
    389 		if (q->ipqe_seq == pkt_seq + pkt_len) {
    390 #ifdef TCPREASS_DEBUG
    391 			printf("tcp_reass[%p]: append %u:%u(%u) to %u:%u(%u)\n",
    392 			       tp, q->ipqe_seq, q->ipqe_seq + q->ipqe_len, q->ipqe_len,
    393 			       pkt_seq, pkt_seq + pkt_len, pkt_len);
    394 #endif
    395 			pkt_len += q->ipqe_len;
    396 			pkt_flags |= q->ipqe_flags;
    397 			m_cat(m, q->ipqe_m);
    398 			LIST_REMOVE(q, ipqe_q);
    399 			LIST_REMOVE(q, ipqe_timeq);
    400 			if (tiqe == NULL) {
    401 			    tiqe = q;
    402 			} else {
    403 			    pool_put(&ipqent_pool, q);
    404 			}
    405 			break;
    406 		}
    407 		/*
    408 		 * If the fragment is before the segment, remember it.
    409 		 * When this loop is terminated, p will contain the
    410 		 * pointer to fragment that is right before the received
    411 		 * segment.
    412 		 */
    413 		if (SEQ_LEQ(q->ipqe_seq, pkt_seq))
    414 			p = q;
    415 
    416 		continue;
    417 
    418 		/*
    419 		 * This is a common operation.  It also will allow
    420 		 * to save doing a malloc/free in most instances.
    421 		 */
    422 	  free_ipqe:
    423 		LIST_REMOVE(q, ipqe_q);
    424 		LIST_REMOVE(q, ipqe_timeq);
    425 		if (tiqe == NULL) {
    426 		    tiqe = q;
    427 		} else {
    428 		    pool_put(&ipqent_pool, q);
    429 		}
    430 	}
    431 
    432 	/*
    433 	 * Allocate a new queue entry since the received segment did not
    434 	 * collapse onto any other out-of-order block; thus we are allocating
    435 	 * a new block.  If it had collapsed, tiqe would not be NULL and
    436 	 * we would be reusing it.
    437 	 * XXX If we can't, just drop the packet.  XXX
    438 	 */
    439 	if (tiqe == NULL) {
    440 		tiqe = pool_get(&ipqent_pool, PR_NOWAIT);
    441 		if (tiqe == NULL) {
    442 			tcpstat.tcps_rcvmemdrop++;
    443 			m_freem(m);
    444 			return (0);
    445 		}
    446 	}
    447 
    448 	/*
    449 	 * Update the counters.
    450 	 */
    451 	tcpstat.tcps_rcvoopack++;
    452 	tcpstat.tcps_rcvoobyte += rcvoobyte;
    453 	if (rcvpartdupbyte) {
    454 	    tcpstat.tcps_rcvpartduppack++;
    455 	    tcpstat.tcps_rcvpartdupbyte += rcvpartdupbyte;
    456 	}
    457 
    458 	/*
    459 	 * Insert the new fragment queue entry into both queues.
    460 	 */
    461 	tiqe->ipqe_m = m;
    462 	tiqe->ipqe_seq = pkt_seq;
    463 	tiqe->ipqe_len = pkt_len;
    464 	tiqe->ipqe_flags = pkt_flags;
    465 	if (p == NULL) {
    466 		LIST_INSERT_HEAD(&tp->segq, tiqe, ipqe_q);
    467 #ifdef TCPREASS_DEBUG
    468 		if (tiqe->ipqe_seq != tp->rcv_nxt)
    469 			printf("tcp_reass[%p]: insert %u:%u(%u) at front\n",
    470 			       tp, pkt_seq, pkt_seq + pkt_len, pkt_len);
    471 #endif
    472 	} else {
    473 		LIST_INSERT_AFTER(p, tiqe, ipqe_q);
    474 #ifdef TCPREASS_DEBUG
    475 		printf("tcp_reass[%p]: insert %u:%u(%u) after %u:%u(%u)\n",
    476 		       tp, pkt_seq, pkt_seq + pkt_len, pkt_len,
    477 		       p->ipqe_seq, p->ipqe_seq + p->ipqe_len, p->ipqe_len);
    478 #endif
    479 	}
    480 
    481 	LIST_INSERT_HEAD(&tp->timeq, tiqe, ipqe_timeq);
    482 
    483 present:
    484 	/*
    485 	 * Present data to user, advancing rcv_nxt through
    486 	 * completed sequence space.
    487 	 */
    488 	if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
    489 		return (0);
    490 	q = tp->segq.lh_first;
    491 	if (q == NULL || q->ipqe_seq != tp->rcv_nxt)
    492 		return (0);
    493 	if (tp->t_state == TCPS_SYN_RECEIVED && q->ipqe_len)
    494 		return (0);
    495 
    496 	tp->rcv_nxt += q->ipqe_len;
    497 	pkt_flags = q->ipqe_flags & TH_FIN;
    498 	ND6_HINT(tp);
    499 
    500 	LIST_REMOVE(q, ipqe_q);
    501 	LIST_REMOVE(q, ipqe_timeq);
    502 	if (so->so_state & SS_CANTRCVMORE)
    503 		m_freem(q->ipqe_m);
    504 	else
    505 		sbappend(&so->so_rcv, q->ipqe_m);
    506 	pool_put(&ipqent_pool, q);
    507 	sorwakeup(so);
    508 	return (pkt_flags);
    509 }
    510 
    511 #if defined(INET6) && !defined(TCP6)
    512 int
    513 tcp6_input(mp, offp, proto)
    514 	struct mbuf **mp;
    515 	int *offp, proto;
    516 {
    517 	struct mbuf *m = *mp;
    518 
    519 #if defined(NFAITH) && 0 < NFAITH
    520 	if (m->m_pkthdr.rcvif) {
    521 		if (m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
    522 			/* XXX send icmp6 host/port unreach? */
    523 			m_freem(m);
    524 			return IPPROTO_DONE;
    525 		}
    526 	}
    527 #endif
    528 
    529 	/*
    530 	 * draft-itojun-ipv6-tcp-to-anycast
    531 	 * better place to put this in?
    532 	 */
    533 	if (m->m_flags & M_ANYCAST6) {
    534 		struct ip6_hdr *ip6;
    535 		if (m->m_len < sizeof(struct ip6_hdr)) {
    536 			if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
    537 				tcpstat.tcps_rcvshort++;
    538 				return IPPROTO_DONE;
    539 			}
    540 		}
    541 		ip6 = mtod(m, struct ip6_hdr *);
    542 		icmp6_error(m, ICMP6_DST_UNREACH,
    543 			ICMP6_DST_UNREACH_ADDR,
    544 			(caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
    545 		return IPPROTO_DONE;
    546 	}
    547 
    548 	tcp_input(m, *offp, proto);
    549 	return IPPROTO_DONE;
    550 }
    551 #endif
    552 
    553 /*
    554  * TCP input routine, follows pages 65-76 of the
    555  * protocol specification dated September, 1981 very closely.
    556  */
    557 void
    558 #if __STDC__
    559 tcp_input(struct mbuf *m, ...)
    560 #else
    561 tcp_input(m, va_alist)
    562 	struct mbuf *m;
    563 #endif
    564 {
    565 	int proto;
    566 	struct tcphdr *th;
    567 	struct ip *ip;
    568 	struct inpcb *inp;
    569 #ifdef INET6
    570 	struct ip6_hdr *ip6;
    571 	struct in6pcb *in6p;
    572 #endif
    573 	caddr_t optp = NULL;
    574 	int optlen = 0;
    575 	int len, tlen, toff, hdroptlen = 0;
    576 	struct tcpcb *tp = 0;
    577 	int tiflags;
    578 	struct socket *so = NULL;
    579 	int todrop, acked, ourfinisacked, needoutput = 0;
    580 	short ostate = 0;
    581 	int iss = 0;
    582 	u_long tiwin;
    583 	struct tcp_opt_info opti;
    584 	int off, iphlen;
    585 	va_list ap;
    586 	int af;		/* af on the wire */
    587 	struct mbuf *tcp_saveti = NULL;
    588 
    589 	va_start(ap, m);
    590 	toff = va_arg(ap, int);
    591 	proto = va_arg(ap, int);
    592 	va_end(ap);
    593 
    594 	tcpstat.tcps_rcvtotal++;
    595 
    596 	bzero(&opti, sizeof(opti));
    597 	opti.ts_present = 0;
    598 	opti.maxseg = 0;
    599 
    600 	/*
    601 	 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN.
    602 	 *
    603 	 * TCP is, by definition, unicast, so we reject all
    604 	 * multicast outright.
    605 	 *
    606 	 * Note, there are additional src/dst address checks in
    607 	 * the AF-specific code below.
    608 	 */
    609 	if (m->m_flags & (M_BCAST|M_MCAST)) {
    610 		/* XXX stat */
    611 		goto drop;
    612 	}
    613 #ifdef INET6
    614 	if (m->m_flags & M_ANYCAST6) {
    615 		/* XXX stat */
    616 		goto drop;
    617 	}
    618 #endif
    619 
    620 	/*
    621 	 * Get IP and TCP header together in first mbuf.
    622 	 * Note: IP leaves IP header in first mbuf.
    623 	 */
    624 	ip = mtod(m, struct ip *);
    625 #ifdef INET6
    626 	ip6 = NULL;
    627 #endif
    628 	switch (ip->ip_v) {
    629 	case 4:
    630 		af = AF_INET;
    631 		iphlen = sizeof(struct ip);
    632 #ifndef PULLDOWN_TEST
    633 		/* would like to get rid of this... */
    634 		if (toff > sizeof (struct ip)) {
    635 			ip_stripoptions(m, (struct mbuf *)0);
    636 			toff = sizeof(struct ip);
    637 		}
    638 		if (m->m_len < toff + sizeof (struct tcphdr)) {
    639 			if ((m = m_pullup(m, toff + sizeof (struct tcphdr))) == 0) {
    640 				tcpstat.tcps_rcvshort++;
    641 				return;
    642 			}
    643 		}
    644 		ip = mtod(m, struct ip *);
    645 		th = (struct tcphdr *)(mtod(m, caddr_t) + toff);
    646 #else
    647 		ip = mtod(m, struct ip *);
    648 		IP6_EXTHDR_GET(th, struct tcphdr *, m, toff,
    649 			sizeof(struct tcphdr));
    650 		if (th == NULL) {
    651 			tcpstat.tcps_rcvshort++;
    652 			return;
    653 		}
    654 #endif
    655 
    656 		/*
    657 		 * Make sure destination address is not multicast.
    658 		 * Source address checked in ip_input().
    659 		 */
    660 		if (IN_MULTICAST(ip->ip_dst.s_addr)) {
    661 			/* XXX stat */
    662 			goto drop;
    663 		}
    664 
    665 		/* We do the checksum after PCB lookup... */
    666 		len = ip->ip_len;
    667 		tlen = len - toff;
    668 		break;
    669 #ifdef INET6
    670 	case 6:
    671 		ip = NULL;
    672 		iphlen = sizeof(struct ip6_hdr);
    673 		af = AF_INET6;
    674 #ifndef PULLDOWN_TEST
    675 		if (m->m_len < toff + sizeof(struct tcphdr)) {
    676 			m = m_pullup(m, toff + sizeof(struct tcphdr));	/*XXX*/
    677 			if (m == NULL) {
    678 				tcpstat.tcps_rcvshort++;
    679 				return;
    680 			}
    681 		}
    682 		ip6 = mtod(m, struct ip6_hdr *);
    683 		th = (struct tcphdr *)(mtod(m, caddr_t) + toff);
    684 #else
    685 		ip6 = mtod(m, struct ip6_hdr *);
    686 		IP6_EXTHDR_GET(th, struct tcphdr *, m, toff,
    687 			sizeof(struct tcphdr));
    688 		if (th == NULL) {
    689 			tcpstat.tcps_rcvshort++;
    690 			return;
    691 		}
    692 #endif
    693 
    694 		/* Be proactive about malicious use of IPv4 mapped address */
    695 		if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
    696 		    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
    697 			/* XXX stat */
    698 			goto drop;
    699 		}
    700 
    701 		/*
    702 		 * Make sure destination address is not multicast.
    703 		 * Source address checked in ip6_input().
    704 		 */
    705 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
    706 			/* XXX stat */
    707 			goto drop;
    708 		}
    709 
    710 		/* We do the checksum after PCB lookup... */
    711 		len = m->m_pkthdr.len;
    712 		tlen = len - toff;
    713 		break;
    714 #endif
    715 	default:
    716 		m_freem(m);
    717 		return;
    718 	}
    719 
    720 	/*
    721 	 * Check that TCP offset makes sense,
    722 	 * pull out TCP options and adjust length.		XXX
    723 	 */
    724 	off = th->th_off << 2;
    725 	if (off < sizeof (struct tcphdr) || off > tlen) {
    726 		tcpstat.tcps_rcvbadoff++;
    727 		goto drop;
    728 	}
    729 	tlen -= off;
    730 
    731 	/*
    732 	 * tcp_input() has been modified to use tlen to mean the TCP data
    733 	 * length throughout the function.  Other functions can use
    734 	 * m->m_pkthdr.len as the basis for calculating the TCP data length.
    735 	 * rja
    736 	 */
    737 
    738 	if (off > sizeof (struct tcphdr)) {
    739 #ifndef PULLDOWN_TEST
    740 		if (m->m_len < toff + off) {
    741 			if ((m = m_pullup(m, toff + off)) == 0) {
    742 				tcpstat.tcps_rcvshort++;
    743 				return;
    744 			}
    745 			switch (af) {
    746 			case AF_INET:
    747 				ip = mtod(m, struct ip *);
    748 				break;
    749 #ifdef INET6
    750 			case AF_INET6:
    751 				ip6 = mtod(m, struct ip6_hdr *);
    752 				break;
    753 #endif
    754 			}
    755 			th = (struct tcphdr *)(mtod(m, caddr_t) + toff);
    756 		}
    757 #else
    758 		IP6_EXTHDR_GET(th, struct tcphdr *, m, toff, off);
    759 		if (th == NULL) {
    760 			tcpstat.tcps_rcvshort++;
    761 			return;
    762 		}
    763 		/*
    764 		 * NOTE: ip/ip6 will not be affected by m_pulldown()
    765 		 * (as they're before toff) and we don't need to update those.
    766 		 */
    767 #endif
    768 		optlen = off - sizeof (struct tcphdr);
    769 		optp = ((caddr_t)th) + sizeof(struct tcphdr);
    770 		/*
    771 		 * Do quick retrieval of timestamp options ("options
    772 		 * prediction?").  If timestamp is the only option and it's
    773 		 * formatted as recommended in RFC 1323 appendix A, we
    774 		 * quickly get the values now and not bother calling
    775 		 * tcp_dooptions(), etc.
    776 		 */
    777 		if ((optlen == TCPOLEN_TSTAMP_APPA ||
    778 		     (optlen > TCPOLEN_TSTAMP_APPA &&
    779 			optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) &&
    780 		     *(u_int32_t *)optp == htonl(TCPOPT_TSTAMP_HDR) &&
    781 		     (th->th_flags & TH_SYN) == 0) {
    782 			opti.ts_present = 1;
    783 			opti.ts_val = ntohl(*(u_int32_t *)(optp + 4));
    784 			opti.ts_ecr = ntohl(*(u_int32_t *)(optp + 8));
    785 			optp = NULL;	/* we've parsed the options */
    786 		}
    787 	}
    788 	tiflags = th->th_flags;
    789 
    790 	/*
    791 	 * Locate pcb for segment.
    792 	 */
    793 findpcb:
    794 	inp = NULL;
    795 #ifdef INET6
    796 	in6p = NULL;
    797 #endif
    798 	switch (af) {
    799 	case AF_INET:
    800 		inp = in_pcblookup_connect(&tcbtable, ip->ip_src, th->th_sport,
    801 		    ip->ip_dst, th->th_dport);
    802 		if (inp == 0) {
    803 			++tcpstat.tcps_pcbhashmiss;
    804 			inp = in_pcblookup_bind(&tcbtable, ip->ip_dst, th->th_dport);
    805 		}
    806 #if defined(INET6) && !defined(TCP6)
    807 		if (inp == 0) {
    808 			struct in6_addr s, d;
    809 
    810 			/* mapped addr case */
    811 			bzero(&s, sizeof(s));
    812 			s.s6_addr16[5] = htons(0xffff);
    813 			bcopy(&ip->ip_src, &s.s6_addr32[3], sizeof(ip->ip_src));
    814 			bzero(&d, sizeof(d));
    815 			d.s6_addr16[5] = htons(0xffff);
    816 			bcopy(&ip->ip_dst, &d.s6_addr32[3], sizeof(ip->ip_dst));
    817 			in6p = in6_pcblookup_connect(&tcb6, &s, th->th_sport,
    818 				&d, th->th_dport, 0);
    819 			if (in6p == 0) {
    820 				++tcpstat.tcps_pcbhashmiss;
    821 				in6p = in6_pcblookup_bind(&tcb6, &d,
    822 					th->th_dport, 0);
    823 			}
    824 		}
    825 #endif
    826 #ifndef INET6
    827 		if (inp == 0)
    828 #else
    829 		if (inp == 0 && in6p == 0)
    830 #endif
    831 		{
    832 			++tcpstat.tcps_noport;
    833 			if (tcp_log_refused && (tiflags & TH_SYN)) {
    834 #ifndef INET6
    835 				char src[4*sizeof "123"];
    836 				char dst[4*sizeof "123"];
    837 #else
    838 				char src[INET6_ADDRSTRLEN];
    839 				char dst[INET6_ADDRSTRLEN];
    840 #endif
    841 				if (ip) {
    842 					strcpy(src, inet_ntoa(ip->ip_src));
    843 					strcpy(dst, inet_ntoa(ip->ip_dst));
    844 				}
    845 #ifdef INET6
    846 				else if (ip6) {
    847 					strcpy(src, ip6_sprintf(&ip6->ip6_src));
    848 					strcpy(dst, ip6_sprintf(&ip6->ip6_dst));
    849 				}
    850 #endif
    851 				else {
    852 					strcpy(src, "(unknown)");
    853 					strcpy(dst, "(unknown)");
    854 				}
    855 				log(LOG_INFO,
    856 				    "Connection attempt to TCP %s:%d from %s:%d\n",
    857 				    dst, ntohs(th->th_dport),
    858 				    src, ntohs(th->th_sport));
    859 			}
    860 			TCP_FIELDS_TO_HOST(th);
    861 			goto dropwithreset_ratelim;
    862 		}
    863 #ifdef IPSEC
    864 		if (inp && ipsec4_in_reject(m, inp)) {
    865 			ipsecstat.in_polvio++;
    866 			goto drop;
    867 		}
    868 #ifdef INET6
    869 		else if (in6p && ipsec4_in_reject_so(m, in6p->in6p_socket)) {
    870 			ipsecstat.in_polvio++;
    871 			goto drop;
    872 		}
    873 #endif
    874 #endif /*IPSEC*/
    875 		break;
    876 #if defined(INET6) && !defined(TCP6)
    877 	case AF_INET6:
    878 	    {
    879 		int faith;
    880 
    881 #if defined(NFAITH) && NFAITH > 0
    882 		if (m->m_pkthdr.rcvif
    883 		 && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
    884 			faith = 1;
    885 		} else
    886 			faith = 0;
    887 #else
    888 		faith = 0;
    889 #endif
    890 		in6p = in6_pcblookup_connect(&tcb6, &ip6->ip6_src, th->th_sport,
    891 			&ip6->ip6_dst, th->th_dport, faith);
    892 		if (in6p == NULL) {
    893 			++tcpstat.tcps_pcbhashmiss;
    894 			in6p = in6_pcblookup_bind(&tcb6, &ip6->ip6_dst,
    895 				th->th_dport, faith);
    896 		}
    897 		if (in6p == NULL) {
    898 			++tcpstat.tcps_noport;
    899 			TCP_FIELDS_TO_HOST(th);
    900 			goto dropwithreset_ratelim;
    901 		}
    902 #ifdef IPSEC
    903 		if (ipsec6_in_reject(m, in6p)) {
    904 			ipsec6stat.in_polvio++;
    905 			goto drop;
    906 		}
    907 #endif /*IPSEC*/
    908 		break;
    909 	    }
    910 #endif
    911 	}
    912 
    913 	/*
    914 	 * If the state is CLOSED (i.e., TCB does not exist) then
    915 	 * all data in the incoming segment is discarded.
    916 	 * If the TCB exists but is in CLOSED state, it is embryonic,
    917 	 * but should either do a listen or a connect soon.
    918 	 */
    919 	tp = NULL;
    920 	so = NULL;
    921 	if (inp) {
    922 		tp = intotcpcb(inp);
    923 		so = inp->inp_socket;
    924 	}
    925 #ifdef INET6
    926 	else if (in6p) {
    927 		tp = in6totcpcb(in6p);
    928 		so = in6p->in6p_socket;
    929 	}
    930 #endif
    931 	if (tp == 0) {
    932 		TCP_FIELDS_TO_HOST(th);
    933 		goto dropwithreset_ratelim;
    934 	}
    935 	if (tp->t_state == TCPS_CLOSED)
    936 		goto drop;
    937 
    938 	/*
    939 	 * Checksum extended TCP header and data.
    940 	 */
    941 	switch (af) {
    942 	case AF_INET:
    943 #ifndef PULLDOWN_TEST
    944 	    {
    945 		struct ipovly *ipov;
    946 		ipov = (struct ipovly *)ip;
    947 		bzero(ipov->ih_x1, sizeof ipov->ih_x1);
    948 		ipov->ih_len = htons(tlen + off);
    949 
    950 		if (in_cksum(m, len) != 0) {
    951 			tcpstat.tcps_rcvbadsum++;
    952 			goto drop;
    953 		}
    954 	    }
    955 #else
    956 		if (in4_cksum(m, IPPROTO_TCP, toff, tlen + off) != 0) {
    957 			tcpstat.tcps_rcvbadsum++;
    958 			goto drop;
    959 		}
    960 #endif
    961 		break;
    962 
    963 #ifdef INET6
    964 	case AF_INET6:
    965 		if (in6_cksum(m, IPPROTO_TCP, toff, tlen + off) != 0) {
    966 			tcpstat.tcps_rcvbadsum++;
    967 			goto drop;
    968 		}
    969 		break;
    970 #endif
    971 	}
    972 
    973 	TCP_FIELDS_TO_HOST(th);
    974 
    975 	/* Unscale the window into a 32-bit value. */
    976 	if ((tiflags & TH_SYN) == 0)
    977 		tiwin = th->th_win << tp->snd_scale;
    978 	else
    979 		tiwin = th->th_win;
    980 
    981 #ifdef INET6
    982 	/* save packet options if user wanted */
    983 	if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS)) {
    984 		if (in6p->in6p_options) {
    985 			m_freem(in6p->in6p_options);
    986 			in6p->in6p_options = 0;
    987 		}
    988 		ip6_savecontrol(in6p, &in6p->in6p_options, ip6, m);
    989 	}
    990 #endif
    991 
    992 	if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
    993 		union syn_cache_sa src;
    994 		union syn_cache_sa dst;
    995 
    996 		bzero(&src, sizeof(src));
    997 		bzero(&dst, sizeof(dst));
    998 		switch (af) {
    999 		case AF_INET:
   1000 			src.sin.sin_len = sizeof(struct sockaddr_in);
   1001 			src.sin.sin_family = AF_INET;
   1002 			src.sin.sin_addr = ip->ip_src;
   1003 			src.sin.sin_port = th->th_sport;
   1004 
   1005 			dst.sin.sin_len = sizeof(struct sockaddr_in);
   1006 			dst.sin.sin_family = AF_INET;
   1007 			dst.sin.sin_addr = ip->ip_dst;
   1008 			dst.sin.sin_port = th->th_dport;
   1009 			break;
   1010 #ifdef INET6
   1011 		case AF_INET6:
   1012 			src.sin6.sin6_len = sizeof(struct sockaddr_in6);
   1013 			src.sin6.sin6_family = AF_INET6;
   1014 			src.sin6.sin6_addr = ip6->ip6_src;
   1015 			src.sin6.sin6_port = th->th_sport;
   1016 
   1017 			dst.sin6.sin6_len = sizeof(struct sockaddr_in6);
   1018 			dst.sin6.sin6_family = AF_INET6;
   1019 			dst.sin6.sin6_addr = ip6->ip6_dst;
   1020 			dst.sin6.sin6_port = th->th_dport;
   1021 			break;
   1022 #endif /* INET6 */
   1023 		default:
   1024 			goto badsyn;	/*sanity*/
   1025 		}
   1026 
   1027 		if (so->so_options & SO_DEBUG) {
   1028 			ostate = tp->t_state;
   1029 
   1030 			tcp_saveti = NULL;
   1031 			if (iphlen + sizeof(struct tcphdr) > MHLEN)
   1032 				goto nosave;
   1033 
   1034 			if (m->m_len > iphlen && (m->m_flags & M_EXT) == 0) {
   1035 				tcp_saveti = m_copym(m, 0, iphlen, M_DONTWAIT);
   1036 				if (!tcp_saveti)
   1037 					goto nosave;
   1038 			} else {
   1039 				MGETHDR(tcp_saveti, M_DONTWAIT, MT_HEADER);
   1040 				if (!tcp_saveti)
   1041 					goto nosave;
   1042 				tcp_saveti->m_len = iphlen;
   1043 				m_copydata(m, 0, iphlen,
   1044 				    mtod(tcp_saveti, caddr_t));
   1045 			}
   1046 
   1047 			if (M_TRAILINGSPACE(tcp_saveti) < sizeof(struct tcphdr)) {
   1048 				m_freem(tcp_saveti);
   1049 				tcp_saveti = NULL;
   1050 			} else {
   1051 				tcp_saveti->m_len += sizeof(struct tcphdr);
   1052 				bcopy(th, mtod(tcp_saveti, caddr_t) + iphlen,
   1053 				    sizeof(struct tcphdr));
   1054 			}
   1055 			if (tcp_saveti) {
   1056 				/*
   1057 				 * need to recover version # field, which was
   1058 				 * overwritten on ip_cksum computation.
   1059 				 */
   1060 				struct ip *sip;
   1061 				sip = mtod(tcp_saveti, struct ip *);
   1062 				switch (af) {
   1063 				case AF_INET:
   1064 					sip->ip_v = 4;
   1065 					break;
   1066 #ifdef INET6
   1067 				case AF_INET6:
   1068 					sip->ip_v = 6;
   1069 					break;
   1070 #endif
   1071 				}
   1072 			}
   1073 	nosave:;
   1074 		}
   1075 		if (so->so_options & SO_ACCEPTCONN) {
   1076   			if ((tiflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
   1077 				if (tiflags & TH_RST) {
   1078 					syn_cache_reset(&src.sa, &dst.sa, th);
   1079 				} else if ((tiflags & (TH_ACK|TH_SYN)) ==
   1080 				    (TH_ACK|TH_SYN)) {
   1081 					/*
   1082 					 * Received a SYN,ACK.  This should
   1083 					 * never happen while we are in
   1084 					 * LISTEN.  Send an RST.
   1085 					 */
   1086 					goto badsyn;
   1087 				} else if (tiflags & TH_ACK) {
   1088 					so = syn_cache_get(&src.sa, &dst.sa,
   1089 						th, toff, tlen, so, m);
   1090 					if (so == NULL) {
   1091 						/*
   1092 						 * We don't have a SYN for
   1093 						 * this ACK; send an RST.
   1094 						 */
   1095 						goto badsyn;
   1096 					} else if (so ==
   1097 					    (struct socket *)(-1)) {
   1098 						/*
   1099 						 * We were unable to create
   1100 						 * the connection.  If the
   1101 						 * 3-way handshake was
   1102 						 * completed, and RST has
   1103 						 * been sent to the peer.
   1104 						 * Since the mbuf might be
   1105 						 * in use for the reply,
   1106 						 * do not free it.
   1107 						 */
   1108 						m = NULL;
   1109 					} else {
   1110 						/*
   1111 						 * We have created a
   1112 						 * full-blown connection.
   1113 						 */
   1114 						tp = NULL;
   1115 						inp = NULL;
   1116 #ifdef INET6
   1117 						in6p = NULL;
   1118 #endif
   1119 						switch (so->so_proto->pr_domain->dom_family) {
   1120 						case AF_INET:
   1121 							inp = sotoinpcb(so);
   1122 							tp = intotcpcb(inp);
   1123 							break;
   1124 #ifdef INET6
   1125 						case AF_INET6:
   1126 							in6p = sotoin6pcb(so);
   1127 							tp = in6totcpcb(in6p);
   1128 							break;
   1129 #endif
   1130 						}
   1131 						if (tp == NULL)
   1132 							goto badsyn;	/*XXX*/
   1133 						tiwin <<= tp->snd_scale;
   1134 						goto after_listen;
   1135 					}
   1136   				} else {
   1137 					/*
   1138 					 * None of RST, SYN or ACK was set.
   1139 					 * This is an invalid packet for a
   1140 					 * TCB in LISTEN state.  Send a RST.
   1141 					 */
   1142 					goto badsyn;
   1143 				}
   1144   			} else {
   1145 				/*
   1146 				 * Received a SYN.
   1147 				 */
   1148 
   1149 				/*
   1150 				 * LISTEN socket received a SYN
   1151 				 * from itself?  This can't possibly
   1152 				 * be valid; drop the packet.
   1153 				 */
   1154 				if (th->th_sport == th->th_dport) {
   1155 					int i;
   1156 
   1157 					switch (af) {
   1158 					case AF_INET:
   1159 						i = in_hosteq(ip->ip_src, ip->ip_dst);
   1160 						break;
   1161 #ifdef INET6
   1162 					case AF_INET6:
   1163 						i = IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6->ip6_dst);
   1164 						break;
   1165 #endif
   1166 					default:
   1167 						i = 1;
   1168 					}
   1169 					if (i) {
   1170 						tcpstat.tcps_badsyn++;
   1171 						goto drop;
   1172 					}
   1173 				}
   1174 
   1175 				/*
   1176 				 * SYN looks ok; create compressed TCP
   1177 				 * state for it.
   1178 				 */
   1179 				if (so->so_qlen <= so->so_qlimit &&
   1180 				    syn_cache_add(&src.sa, &dst.sa, th, tlen,
   1181 						so, m, optp, optlen, &opti))
   1182 					m = NULL;
   1183 			}
   1184 			goto drop;
   1185 		}
   1186 	}
   1187 
   1188 after_listen:
   1189 #ifdef DIAGNOSTIC
   1190 	/*
   1191 	 * Should not happen now that all embryonic connections
   1192 	 * are handled with compressed state.
   1193 	 */
   1194 	if (tp->t_state == TCPS_LISTEN)
   1195 		panic("tcp_input: TCPS_LISTEN");
   1196 #endif
   1197 
   1198 	/*
   1199 	 * Segment received on connection.
   1200 	 * Reset idle time and keep-alive timer.
   1201 	 */
   1202 	tp->t_idle = 0;
   1203 	if (TCPS_HAVEESTABLISHED(tp->t_state))
   1204 		TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle);
   1205 
   1206 	/*
   1207 	 * Process options.
   1208 	 */
   1209 	if (optp)
   1210 		tcp_dooptions(tp, optp, optlen, th, &opti);
   1211 
   1212 	/*
   1213 	 * Header prediction: check for the two common cases
   1214 	 * of a uni-directional data xfer.  If the packet has
   1215 	 * no control flags, is in-sequence, the window didn't
   1216 	 * change and we're not retransmitting, it's a
   1217 	 * candidate.  If the length is zero and the ack moved
   1218 	 * forward, we're the sender side of the xfer.  Just
   1219 	 * free the data acked & wake any higher level process
   1220 	 * that was blocked waiting for space.  If the length
   1221 	 * is non-zero and the ack didn't move, we're the
   1222 	 * receiver side.  If we're getting packets in-order
   1223 	 * (the reassembly queue is empty), add the data to
   1224 	 * the socket buffer and note that we need a delayed ack.
   1225 	 */
   1226 	if (tp->t_state == TCPS_ESTABLISHED &&
   1227 	    (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
   1228 	    (!opti.ts_present || TSTMP_GEQ(opti.ts_val, tp->ts_recent)) &&
   1229 	    th->th_seq == tp->rcv_nxt &&
   1230 	    tiwin && tiwin == tp->snd_wnd &&
   1231 	    tp->snd_nxt == tp->snd_max) {
   1232 
   1233 		/*
   1234 		 * If last ACK falls within this segment's sequence numbers,
   1235 		 *  record the timestamp.
   1236 		 */
   1237 		if (opti.ts_present &&
   1238 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
   1239 		    SEQ_LT(tp->last_ack_sent, th->th_seq + tlen)) {
   1240 			tp->ts_recent_age = tcp_now;
   1241 			tp->ts_recent = opti.ts_val;
   1242 		}
   1243 
   1244 		if (tlen == 0) {
   1245 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
   1246 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
   1247 			    tp->snd_cwnd >= tp->snd_wnd &&
   1248 			    tp->t_dupacks < tcprexmtthresh) {
   1249 				/*
   1250 				 * this is a pure ack for outstanding data.
   1251 				 */
   1252 				++tcpstat.tcps_predack;
   1253 				if (opti.ts_present && opti.ts_ecr)
   1254 					tcp_xmit_timer(tp,
   1255 					    tcp_now - opti.ts_ecr + 1);
   1256 				else if (tp->t_rtt &&
   1257 				    SEQ_GT(th->th_ack, tp->t_rtseq))
   1258 					tcp_xmit_timer(tp, tp->t_rtt);
   1259 				acked = th->th_ack - tp->snd_una;
   1260 				tcpstat.tcps_rcvackpack++;
   1261 				tcpstat.tcps_rcvackbyte += acked;
   1262 				ND6_HINT(tp);
   1263 				sbdrop(&so->so_snd, acked);
   1264 				/*
   1265 				 * We want snd_recover to track snd_una to
   1266 				 * avoid sequence wraparound problems for
   1267 				 * very large transfers.
   1268 				 */
   1269 				tp->snd_una = tp->snd_recover = th->th_ack;
   1270 				m_freem(m);
   1271 
   1272 				/*
   1273 				 * If all outstanding data are acked, stop
   1274 				 * retransmit timer, otherwise restart timer
   1275 				 * using current (possibly backed-off) value.
   1276 				 * If process is waiting for space,
   1277 				 * wakeup/selwakeup/signal.  If data
   1278 				 * are ready to send, let tcp_output
   1279 				 * decide between more output or persist.
   1280 				 */
   1281 				if (tp->snd_una == tp->snd_max)
   1282 					TCP_TIMER_DISARM(tp, TCPT_REXMT);
   1283 				else if (TCP_TIMER_ISARMED(tp,
   1284 				    TCPT_PERSIST) == 0)
   1285 					TCP_TIMER_ARM(tp, TCPT_REXMT,
   1286 					    tp->t_rxtcur);
   1287 
   1288 				sowwakeup(so);
   1289 				if (so->so_snd.sb_cc)
   1290 					(void) tcp_output(tp);
   1291 				if (tcp_saveti)
   1292 					m_freem(tcp_saveti);
   1293 				return;
   1294 			}
   1295 		} else if (th->th_ack == tp->snd_una &&
   1296 		    tp->segq.lh_first == NULL &&
   1297 		    tlen <= sbspace(&so->so_rcv)) {
   1298 			/*
   1299 			 * this is a pure, in-sequence data packet
   1300 			 * with nothing on the reassembly queue and
   1301 			 * we have enough buffer space to take it.
   1302 			 */
   1303 			++tcpstat.tcps_preddat;
   1304 			tp->rcv_nxt += tlen;
   1305 			tcpstat.tcps_rcvpack++;
   1306 			tcpstat.tcps_rcvbyte += tlen;
   1307 			ND6_HINT(tp);
   1308 			/*
   1309 			 * Drop TCP, IP headers and TCP options then add data
   1310 			 * to socket buffer.
   1311 			 */
   1312 			m_adj(m, toff + off);
   1313 			sbappend(&so->so_rcv, m);
   1314 			sorwakeup(so);
   1315 			TCP_SETUP_ACK(tp, th);
   1316 			if (tp->t_flags & TF_ACKNOW)
   1317 				(void) tcp_output(tp);
   1318 			if (tcp_saveti)
   1319 				m_freem(tcp_saveti);
   1320 			return;
   1321 		}
   1322 	}
   1323 
   1324 	/*
   1325 	 * Compute mbuf offset to TCP data segment.
   1326 	 */
   1327 	hdroptlen = toff + off;
   1328 
   1329 	/*
   1330 	 * Calculate amount of space in receive window,
   1331 	 * and then do TCP input processing.
   1332 	 * Receive window is amount of space in rcv queue,
   1333 	 * but not less than advertised window.
   1334 	 */
   1335 	{ int win;
   1336 
   1337 	win = sbspace(&so->so_rcv);
   1338 	if (win < 0)
   1339 		win = 0;
   1340 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
   1341 	}
   1342 
   1343 	switch (tp->t_state) {
   1344 
   1345 	/*
   1346 	 * If the state is SYN_SENT:
   1347 	 *	if seg contains an ACK, but not for our SYN, drop the input.
   1348 	 *	if seg contains a RST, then drop the connection.
   1349 	 *	if seg does not contain SYN, then drop it.
   1350 	 * Otherwise this is an acceptable SYN segment
   1351 	 *	initialize tp->rcv_nxt and tp->irs
   1352 	 *	if seg contains ack then advance tp->snd_una
   1353 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
   1354 	 *	arrange for segment to be acked (eventually)
   1355 	 *	continue processing rest of data/controls, beginning with URG
   1356 	 */
   1357 	case TCPS_SYN_SENT:
   1358 		if ((tiflags & TH_ACK) &&
   1359 		    (SEQ_LEQ(th->th_ack, tp->iss) ||
   1360 		     SEQ_GT(th->th_ack, tp->snd_max)))
   1361 			goto dropwithreset;
   1362 		if (tiflags & TH_RST) {
   1363 			if (tiflags & TH_ACK)
   1364 				tp = tcp_drop(tp, ECONNREFUSED);
   1365 			goto drop;
   1366 		}
   1367 		if ((tiflags & TH_SYN) == 0)
   1368 			goto drop;
   1369 		if (tiflags & TH_ACK) {
   1370 			tp->snd_una = tp->snd_recover = th->th_ack;
   1371 			if (SEQ_LT(tp->snd_nxt, tp->snd_una))
   1372 				tp->snd_nxt = tp->snd_una;
   1373 			TCP_TIMER_DISARM(tp, TCPT_REXMT);
   1374 		}
   1375 		tp->irs = th->th_seq;
   1376 		tcp_rcvseqinit(tp);
   1377 		tp->t_flags |= TF_ACKNOW;
   1378 		tcp_mss_from_peer(tp, opti.maxseg);
   1379 
   1380 		/*
   1381 		 * Initialize the initial congestion window.  If we
   1382 		 * had to retransmit the SYN, we must initialize cwnd
   1383 		 * to 1 segment (i.e. the Loss Window).
   1384 		 */
   1385 		if (tp->t_flags & TF_SYN_REXMT)
   1386 			tp->snd_cwnd = tp->t_peermss;
   1387 		else
   1388 			tp->snd_cwnd = TCP_INITIAL_WINDOW(tcp_init_win,
   1389 			    tp->t_peermss);
   1390 
   1391 		tcp_rmx_rtt(tp);
   1392 		if (tiflags & TH_ACK) {
   1393 			tcpstat.tcps_connects++;
   1394 			soisconnected(so);
   1395 			tcp_established(tp);
   1396 			/* Do window scaling on this connection? */
   1397 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
   1398 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
   1399 				tp->snd_scale = tp->requested_s_scale;
   1400 				tp->rcv_scale = tp->request_r_scale;
   1401 			}
   1402 			TCP_REASS_LOCK(tp);
   1403 			(void) tcp_reass(tp, NULL, (struct mbuf *)0, &tlen);
   1404 			TCP_REASS_UNLOCK(tp);
   1405 			/*
   1406 			 * if we didn't have to retransmit the SYN,
   1407 			 * use its rtt as our initial srtt & rtt var.
   1408 			 */
   1409 			if (tp->t_rtt)
   1410 				tcp_xmit_timer(tp, tp->t_rtt);
   1411 		} else
   1412 			tp->t_state = TCPS_SYN_RECEIVED;
   1413 
   1414 		/*
   1415 		 * Advance th->th_seq to correspond to first data byte.
   1416 		 * If data, trim to stay within window,
   1417 		 * dropping FIN if necessary.
   1418 		 */
   1419 		th->th_seq++;
   1420 		if (tlen > tp->rcv_wnd) {
   1421 			todrop = tlen - tp->rcv_wnd;
   1422 			m_adj(m, -todrop);
   1423 			tlen = tp->rcv_wnd;
   1424 			tiflags &= ~TH_FIN;
   1425 			tcpstat.tcps_rcvpackafterwin++;
   1426 			tcpstat.tcps_rcvbyteafterwin += todrop;
   1427 		}
   1428 		tp->snd_wl1 = th->th_seq - 1;
   1429 		tp->rcv_up = th->th_seq;
   1430 		goto step6;
   1431 
   1432 	/*
   1433 	 * If the state is SYN_RECEIVED:
   1434 	 *	If seg contains an ACK, but not for our SYN, drop the input
   1435 	 *	and generate an RST.  See page 36, rfc793
   1436 	 */
   1437 	case TCPS_SYN_RECEIVED:
   1438 		if ((tiflags & TH_ACK) &&
   1439 		    (SEQ_LEQ(th->th_ack, tp->iss) ||
   1440 		     SEQ_GT(th->th_ack, tp->snd_max)))
   1441 			goto dropwithreset;
   1442 		break;
   1443 	}
   1444 
   1445 	/*
   1446 	 * States other than LISTEN or SYN_SENT.
   1447 	 * First check timestamp, if present.
   1448 	 * Then check that at least some bytes of segment are within
   1449 	 * receive window.  If segment begins before rcv_nxt,
   1450 	 * drop leading data (and SYN); if nothing left, just ack.
   1451 	 *
   1452 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
   1453 	 * and it's less than ts_recent, drop it.
   1454 	 */
   1455 	if (opti.ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent &&
   1456 	    TSTMP_LT(opti.ts_val, tp->ts_recent)) {
   1457 
   1458 		/* Check to see if ts_recent is over 24 days old.  */
   1459 		if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) {
   1460 			/*
   1461 			 * Invalidate ts_recent.  If this segment updates
   1462 			 * ts_recent, the age will be reset later and ts_recent
   1463 			 * will get a valid value.  If it does not, setting
   1464 			 * ts_recent to zero will at least satisfy the
   1465 			 * requirement that zero be placed in the timestamp
   1466 			 * echo reply when ts_recent isn't valid.  The
   1467 			 * age isn't reset until we get a valid ts_recent
   1468 			 * because we don't want out-of-order segments to be
   1469 			 * dropped when ts_recent is old.
   1470 			 */
   1471 			tp->ts_recent = 0;
   1472 		} else {
   1473 			tcpstat.tcps_rcvduppack++;
   1474 			tcpstat.tcps_rcvdupbyte += tlen;
   1475 			tcpstat.tcps_pawsdrop++;
   1476 			goto dropafterack;
   1477 		}
   1478 	}
   1479 
   1480 	todrop = tp->rcv_nxt - th->th_seq;
   1481 	if (todrop > 0) {
   1482 		if (tiflags & TH_SYN) {
   1483 			tiflags &= ~TH_SYN;
   1484 			th->th_seq++;
   1485 			if (th->th_urp > 1)
   1486 				th->th_urp--;
   1487 			else {
   1488 				tiflags &= ~TH_URG;
   1489 				th->th_urp = 0;
   1490 			}
   1491 			todrop--;
   1492 		}
   1493 		if (todrop > tlen ||
   1494 		    (todrop == tlen && (tiflags & TH_FIN) == 0)) {
   1495 			/*
   1496 			 * Any valid FIN must be to the left of the window.
   1497 			 * At this point the FIN must be a duplicate or
   1498 			 * out of sequence; drop it.
   1499 			 */
   1500 			tiflags &= ~TH_FIN;
   1501 			/*
   1502 			 * Send an ACK to resynchronize and drop any data.
   1503 			 * But keep on processing for RST or ACK.
   1504 			 */
   1505 			tp->t_flags |= TF_ACKNOW;
   1506 			todrop = tlen;
   1507 			tcpstat.tcps_rcvdupbyte += todrop;
   1508 			tcpstat.tcps_rcvduppack++;
   1509 		} else {
   1510 			tcpstat.tcps_rcvpartduppack++;
   1511 			tcpstat.tcps_rcvpartdupbyte += todrop;
   1512 		}
   1513 		hdroptlen += todrop;	/*drop from head afterwards*/
   1514 		th->th_seq += todrop;
   1515 		tlen -= todrop;
   1516 		if (th->th_urp > todrop)
   1517 			th->th_urp -= todrop;
   1518 		else {
   1519 			tiflags &= ~TH_URG;
   1520 			th->th_urp = 0;
   1521 		}
   1522 	}
   1523 
   1524 	/*
   1525 	 * If new data are received on a connection after the
   1526 	 * user processes are gone, then RST the other end.
   1527 	 */
   1528 	if ((so->so_state & SS_NOFDREF) &&
   1529 	    tp->t_state > TCPS_CLOSE_WAIT && tlen) {
   1530 		tp = tcp_close(tp);
   1531 		tcpstat.tcps_rcvafterclose++;
   1532 		goto dropwithreset;
   1533 	}
   1534 
   1535 	/*
   1536 	 * If segment ends after window, drop trailing data
   1537 	 * (and PUSH and FIN); if nothing left, just ACK.
   1538 	 */
   1539 	todrop = (th->th_seq + tlen) - (tp->rcv_nxt+tp->rcv_wnd);
   1540 	if (todrop > 0) {
   1541 		tcpstat.tcps_rcvpackafterwin++;
   1542 		if (todrop >= tlen) {
   1543 			tcpstat.tcps_rcvbyteafterwin += tlen;
   1544 			/*
   1545 			 * If a new connection request is received
   1546 			 * while in TIME_WAIT, drop the old connection
   1547 			 * and start over if the sequence numbers
   1548 			 * are above the previous ones.
   1549 			 */
   1550 			if (tiflags & TH_SYN &&
   1551 			    tp->t_state == TCPS_TIME_WAIT &&
   1552 			    SEQ_GT(th->th_seq, tp->rcv_nxt)) {
   1553 				iss = tcp_new_iss(tp, sizeof(struct tcpcb),
   1554 						  tp->snd_nxt);
   1555 				tp = tcp_close(tp);
   1556 				goto findpcb;
   1557 			}
   1558 			/*
   1559 			 * If window is closed can only take segments at
   1560 			 * window edge, and have to drop data and PUSH from
   1561 			 * incoming segments.  Continue processing, but
   1562 			 * remember to ack.  Otherwise, drop segment
   1563 			 * and ack.
   1564 			 */
   1565 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
   1566 				tp->t_flags |= TF_ACKNOW;
   1567 				tcpstat.tcps_rcvwinprobe++;
   1568 			} else
   1569 				goto dropafterack;
   1570 		} else
   1571 			tcpstat.tcps_rcvbyteafterwin += todrop;
   1572 		m_adj(m, -todrop);
   1573 		tlen -= todrop;
   1574 		tiflags &= ~(TH_PUSH|TH_FIN);
   1575 	}
   1576 
   1577 	/*
   1578 	 * If last ACK falls within this segment's sequence numbers,
   1579 	 * and the timestamp is newer, record it.
   1580 	 */
   1581 	if (opti.ts_present && TSTMP_GEQ(opti.ts_val, tp->ts_recent) &&
   1582 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
   1583 	    SEQ_LT(tp->last_ack_sent, th->th_seq + tlen +
   1584 		   ((tiflags & (TH_SYN|TH_FIN)) != 0))) {
   1585 		tp->ts_recent_age = tcp_now;
   1586 		tp->ts_recent = opti.ts_val;
   1587 	}
   1588 
   1589 	/*
   1590 	 * If the RST bit is set examine the state:
   1591 	 *    SYN_RECEIVED STATE:
   1592 	 *	If passive open, return to LISTEN state.
   1593 	 *	If active open, inform user that connection was refused.
   1594 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
   1595 	 *	Inform user that connection was reset, and close tcb.
   1596 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
   1597 	 *	Close the tcb.
   1598 	 */
   1599 	if (tiflags&TH_RST) switch (tp->t_state) {
   1600 
   1601 	case TCPS_SYN_RECEIVED:
   1602 		so->so_error = ECONNREFUSED;
   1603 		goto close;
   1604 
   1605 	case TCPS_ESTABLISHED:
   1606 	case TCPS_FIN_WAIT_1:
   1607 	case TCPS_FIN_WAIT_2:
   1608 	case TCPS_CLOSE_WAIT:
   1609 		so->so_error = ECONNRESET;
   1610 	close:
   1611 		tp->t_state = TCPS_CLOSED;
   1612 		tcpstat.tcps_drops++;
   1613 		tp = tcp_close(tp);
   1614 		goto drop;
   1615 
   1616 	case TCPS_CLOSING:
   1617 	case TCPS_LAST_ACK:
   1618 	case TCPS_TIME_WAIT:
   1619 		tp = tcp_close(tp);
   1620 		goto drop;
   1621 	}
   1622 
   1623 	/*
   1624 	 * If a SYN is in the window, then this is an
   1625 	 * error and we send an RST and drop the connection.
   1626 	 */
   1627 	if (tiflags & TH_SYN) {
   1628 		tp = tcp_drop(tp, ECONNRESET);
   1629 		goto dropwithreset;
   1630 	}
   1631 
   1632 	/*
   1633 	 * If the ACK bit is off we drop the segment and return.
   1634 	 */
   1635 	if ((tiflags & TH_ACK) == 0) {
   1636 		if (tp->t_flags & TF_ACKNOW)
   1637 			goto dropafterack;
   1638 		else
   1639 			goto drop;
   1640 	}
   1641 
   1642 	/*
   1643 	 * Ack processing.
   1644 	 */
   1645 	switch (tp->t_state) {
   1646 
   1647 	/*
   1648 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
   1649 	 * ESTABLISHED state and continue processing, otherwise
   1650 	 * send an RST.
   1651 	 */
   1652 	case TCPS_SYN_RECEIVED:
   1653 		if (SEQ_GT(tp->snd_una, th->th_ack) ||
   1654 		    SEQ_GT(th->th_ack, tp->snd_max))
   1655 			goto dropwithreset;
   1656 		tcpstat.tcps_connects++;
   1657 		soisconnected(so);
   1658 		tcp_established(tp);
   1659 		/* Do window scaling? */
   1660 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
   1661 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
   1662 			tp->snd_scale = tp->requested_s_scale;
   1663 			tp->rcv_scale = tp->request_r_scale;
   1664 		}
   1665 		TCP_REASS_LOCK(tp);
   1666 		(void) tcp_reass(tp, NULL, (struct mbuf *)0, &tlen);
   1667 		TCP_REASS_UNLOCK(tp);
   1668 		tp->snd_wl1 = th->th_seq - 1;
   1669 		/* fall into ... */
   1670 
   1671 	/*
   1672 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
   1673 	 * ACKs.  If the ack is in the range
   1674 	 *	tp->snd_una < th->th_ack <= tp->snd_max
   1675 	 * then advance tp->snd_una to th->th_ack and drop
   1676 	 * data from the retransmission queue.  If this ACK reflects
   1677 	 * more up to date window information we update our window information.
   1678 	 */
   1679 	case TCPS_ESTABLISHED:
   1680 	case TCPS_FIN_WAIT_1:
   1681 	case TCPS_FIN_WAIT_2:
   1682 	case TCPS_CLOSE_WAIT:
   1683 	case TCPS_CLOSING:
   1684 	case TCPS_LAST_ACK:
   1685 	case TCPS_TIME_WAIT:
   1686 
   1687 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
   1688 			if (tlen == 0 && tiwin == tp->snd_wnd) {
   1689 				tcpstat.tcps_rcvdupack++;
   1690 				/*
   1691 				 * If we have outstanding data (other than
   1692 				 * a window probe), this is a completely
   1693 				 * duplicate ack (ie, window info didn't
   1694 				 * change), the ack is the biggest we've
   1695 				 * seen and we've seen exactly our rexmt
   1696 				 * threshhold of them, assume a packet
   1697 				 * has been dropped and retransmit it.
   1698 				 * Kludge snd_nxt & the congestion
   1699 				 * window so we send only this one
   1700 				 * packet.
   1701 				 *
   1702 				 * We know we're losing at the current
   1703 				 * window size so do congestion avoidance
   1704 				 * (set ssthresh to half the current window
   1705 				 * and pull our congestion window back to
   1706 				 * the new ssthresh).
   1707 				 *
   1708 				 * Dup acks mean that packets have left the
   1709 				 * network (they're now cached at the receiver)
   1710 				 * so bump cwnd by the amount in the receiver
   1711 				 * to keep a constant cwnd packets in the
   1712 				 * network.
   1713 				 */
   1714 				if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 ||
   1715 				    th->th_ack != tp->snd_una)
   1716 					tp->t_dupacks = 0;
   1717 				else if (++tp->t_dupacks == tcprexmtthresh) {
   1718 					tcp_seq onxt = tp->snd_nxt;
   1719 					u_int win =
   1720 					    min(tp->snd_wnd, tp->snd_cwnd) /
   1721 					    2 /	tp->t_segsz;
   1722 					if (tcp_do_newreno && SEQ_LT(th->th_ack,
   1723 					    tp->snd_recover)) {
   1724 						/*
   1725 						 * False fast retransmit after
   1726 						 * timeout.  Do not cut window.
   1727 						 */
   1728 						tp->snd_cwnd += tp->t_segsz;
   1729 						tp->t_dupacks = 0;
   1730 						(void) tcp_output(tp);
   1731 						goto drop;
   1732 					}
   1733 
   1734 					if (win < 2)
   1735 						win = 2;
   1736 					tp->snd_ssthresh = win * tp->t_segsz;
   1737 					tp->snd_recover = tp->snd_max;
   1738 					TCP_TIMER_DISARM(tp, TCPT_REXMT);
   1739 					tp->t_rtt = 0;
   1740 					tp->snd_nxt = th->th_ack;
   1741 					tp->snd_cwnd = tp->t_segsz;
   1742 					(void) tcp_output(tp);
   1743 					tp->snd_cwnd = tp->snd_ssthresh +
   1744 					       tp->t_segsz * tp->t_dupacks;
   1745 					if (SEQ_GT(onxt, tp->snd_nxt))
   1746 						tp->snd_nxt = onxt;
   1747 					goto drop;
   1748 				} else if (tp->t_dupacks > tcprexmtthresh) {
   1749 					tp->snd_cwnd += tp->t_segsz;
   1750 					(void) tcp_output(tp);
   1751 					goto drop;
   1752 				}
   1753 			} else
   1754 				tp->t_dupacks = 0;
   1755 			break;
   1756 		}
   1757 		/*
   1758 		 * If the congestion window was inflated to account
   1759 		 * for the other side's cached packets, retract it.
   1760 		 */
   1761 		if (tcp_do_newreno == 0) {
   1762 			if (tp->t_dupacks >= tcprexmtthresh &&
   1763 			    tp->snd_cwnd > tp->snd_ssthresh)
   1764 				tp->snd_cwnd = tp->snd_ssthresh;
   1765 			tp->t_dupacks = 0;
   1766 		} else if (tp->t_dupacks >= tcprexmtthresh &&
   1767 			   tcp_newreno(tp, th) == 0) {
   1768 			tp->snd_cwnd = tp->snd_ssthresh;
   1769 			/*
   1770 			 * Window inflation should have left us with approx.
   1771 			 * snd_ssthresh outstanding data.  But in case we
   1772 			 * would be inclined to send a burst, better to do
   1773 			 * it via the slow start mechanism.
   1774 			 */
   1775 			if (SEQ_SUB(tp->snd_max, th->th_ack) < tp->snd_ssthresh)
   1776 				tp->snd_cwnd = SEQ_SUB(tp->snd_max, th->th_ack)
   1777 				    + tp->t_segsz;
   1778 			tp->t_dupacks = 0;
   1779 		}
   1780 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
   1781 			tcpstat.tcps_rcvacktoomuch++;
   1782 			goto dropafterack;
   1783 		}
   1784 		acked = th->th_ack - tp->snd_una;
   1785 		tcpstat.tcps_rcvackpack++;
   1786 		tcpstat.tcps_rcvackbyte += acked;
   1787 
   1788 		/*
   1789 		 * If we have a timestamp reply, update smoothed
   1790 		 * round trip time.  If no timestamp is present but
   1791 		 * transmit timer is running and timed sequence
   1792 		 * number was acked, update smoothed round trip time.
   1793 		 * Since we now have an rtt measurement, cancel the
   1794 		 * timer backoff (cf., Phil Karn's retransmit alg.).
   1795 		 * Recompute the initial retransmit timer.
   1796 		 */
   1797 		if (opti.ts_present && opti.ts_ecr)
   1798 			tcp_xmit_timer(tp, tcp_now - opti.ts_ecr + 1);
   1799 		else if (tp->t_rtt && SEQ_GT(th->th_ack, tp->t_rtseq))
   1800 			tcp_xmit_timer(tp,tp->t_rtt);
   1801 
   1802 		/*
   1803 		 * If all outstanding data is acked, stop retransmit
   1804 		 * timer and remember to restart (more output or persist).
   1805 		 * If there is more data to be acked, restart retransmit
   1806 		 * timer, using current (possibly backed-off) value.
   1807 		 */
   1808 		if (th->th_ack == tp->snd_max) {
   1809 			TCP_TIMER_DISARM(tp, TCPT_REXMT);
   1810 			needoutput = 1;
   1811 		} else if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0)
   1812 			TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
   1813 		/*
   1814 		 * When new data is acked, open the congestion window.
   1815 		 * If the window gives us less than ssthresh packets
   1816 		 * in flight, open exponentially (segsz per packet).
   1817 		 * Otherwise open linearly: segsz per window
   1818 		 * (segsz^2 / cwnd per packet), plus a constant
   1819 		 * fraction of a packet (segsz/8) to help larger windows
   1820 		 * open quickly enough.
   1821 		 */
   1822 		{
   1823 		u_int cw = tp->snd_cwnd;
   1824 		u_int incr = tp->t_segsz;
   1825 
   1826 		if (cw > tp->snd_ssthresh)
   1827 			incr = incr * incr / cw;
   1828 		if (tcp_do_newreno == 0 || SEQ_GEQ(th->th_ack, tp->snd_recover))
   1829 			tp->snd_cwnd = min(cw + incr,
   1830 			    TCP_MAXWIN << tp->snd_scale);
   1831 		}
   1832 		ND6_HINT(tp);
   1833 		if (acked > so->so_snd.sb_cc) {
   1834 			tp->snd_wnd -= so->so_snd.sb_cc;
   1835 			sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
   1836 			ourfinisacked = 1;
   1837 		} else {
   1838 			sbdrop(&so->so_snd, acked);
   1839 			tp->snd_wnd -= acked;
   1840 			ourfinisacked = 0;
   1841 		}
   1842 		sowwakeup(so);
   1843 		/*
   1844 		 * We want snd_recover to track snd_una to
   1845 		 * avoid sequence wraparound problems for
   1846 		 * very large transfers.
   1847 		 */
   1848 		tp->snd_una = tp->snd_recover = th->th_ack;
   1849 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
   1850 			tp->snd_nxt = tp->snd_una;
   1851 
   1852 		switch (tp->t_state) {
   1853 
   1854 		/*
   1855 		 * In FIN_WAIT_1 STATE in addition to the processing
   1856 		 * for the ESTABLISHED state if our FIN is now acknowledged
   1857 		 * then enter FIN_WAIT_2.
   1858 		 */
   1859 		case TCPS_FIN_WAIT_1:
   1860 			if (ourfinisacked) {
   1861 				/*
   1862 				 * If we can't receive any more
   1863 				 * data, then closing user can proceed.
   1864 				 * Starting the timer is contrary to the
   1865 				 * specification, but if we don't get a FIN
   1866 				 * we'll hang forever.
   1867 				 */
   1868 				if (so->so_state & SS_CANTRCVMORE) {
   1869 					soisdisconnected(so);
   1870 					if (tcp_maxidle > 0)
   1871 						TCP_TIMER_ARM(tp, TCPT_2MSL,
   1872 						    tcp_maxidle);
   1873 				}
   1874 				tp->t_state = TCPS_FIN_WAIT_2;
   1875 			}
   1876 			break;
   1877 
   1878 	 	/*
   1879 		 * In CLOSING STATE in addition to the processing for
   1880 		 * the ESTABLISHED state if the ACK acknowledges our FIN
   1881 		 * then enter the TIME-WAIT state, otherwise ignore
   1882 		 * the segment.
   1883 		 */
   1884 		case TCPS_CLOSING:
   1885 			if (ourfinisacked) {
   1886 				tp->t_state = TCPS_TIME_WAIT;
   1887 				tcp_canceltimers(tp);
   1888 				TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
   1889 				soisdisconnected(so);
   1890 			}
   1891 			break;
   1892 
   1893 		/*
   1894 		 * In LAST_ACK, we may still be waiting for data to drain
   1895 		 * and/or to be acked, as well as for the ack of our FIN.
   1896 		 * If our FIN is now acknowledged, delete the TCB,
   1897 		 * enter the closed state and return.
   1898 		 */
   1899 		case TCPS_LAST_ACK:
   1900 			if (ourfinisacked) {
   1901 				tp = tcp_close(tp);
   1902 				goto drop;
   1903 			}
   1904 			break;
   1905 
   1906 		/*
   1907 		 * In TIME_WAIT state the only thing that should arrive
   1908 		 * is a retransmission of the remote FIN.  Acknowledge
   1909 		 * it and restart the finack timer.
   1910 		 */
   1911 		case TCPS_TIME_WAIT:
   1912 			TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
   1913 			goto dropafterack;
   1914 		}
   1915 	}
   1916 
   1917 step6:
   1918 	/*
   1919 	 * Update window information.
   1920 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
   1921 	 */
   1922 	if ((tiflags & TH_ACK) && (SEQ_LT(tp->snd_wl1, th->th_seq) ||
   1923 	    (tp->snd_wl1 == th->th_seq && SEQ_LT(tp->snd_wl2, th->th_ack)) ||
   1924 	    (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))) {
   1925 		/* keep track of pure window updates */
   1926 		if (tlen == 0 &&
   1927 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
   1928 			tcpstat.tcps_rcvwinupd++;
   1929 		tp->snd_wnd = tiwin;
   1930 		tp->snd_wl1 = th->th_seq;
   1931 		tp->snd_wl2 = th->th_ack;
   1932 		if (tp->snd_wnd > tp->max_sndwnd)
   1933 			tp->max_sndwnd = tp->snd_wnd;
   1934 		needoutput = 1;
   1935 	}
   1936 
   1937 	/*
   1938 	 * Process segments with URG.
   1939 	 */
   1940 	if ((tiflags & TH_URG) && th->th_urp &&
   1941 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
   1942 		/*
   1943 		 * This is a kludge, but if we receive and accept
   1944 		 * random urgent pointers, we'll crash in
   1945 		 * soreceive.  It's hard to imagine someone
   1946 		 * actually wanting to send this much urgent data.
   1947 		 */
   1948 		if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
   1949 			th->th_urp = 0;			/* XXX */
   1950 			tiflags &= ~TH_URG;		/* XXX */
   1951 			goto dodata;			/* XXX */
   1952 		}
   1953 		/*
   1954 		 * If this segment advances the known urgent pointer,
   1955 		 * then mark the data stream.  This should not happen
   1956 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
   1957 		 * a FIN has been received from the remote side.
   1958 		 * In these states we ignore the URG.
   1959 		 *
   1960 		 * According to RFC961 (Assigned Protocols),
   1961 		 * the urgent pointer points to the last octet
   1962 		 * of urgent data.  We continue, however,
   1963 		 * to consider it to indicate the first octet
   1964 		 * of data past the urgent section as the original
   1965 		 * spec states (in one of two places).
   1966 		 */
   1967 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
   1968 			tp->rcv_up = th->th_seq + th->th_urp;
   1969 			so->so_oobmark = so->so_rcv.sb_cc +
   1970 			    (tp->rcv_up - tp->rcv_nxt) - 1;
   1971 			if (so->so_oobmark == 0)
   1972 				so->so_state |= SS_RCVATMARK;
   1973 			sohasoutofband(so);
   1974 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
   1975 		}
   1976 		/*
   1977 		 * Remove out of band data so doesn't get presented to user.
   1978 		 * This can happen independent of advancing the URG pointer,
   1979 		 * but if two URG's are pending at once, some out-of-band
   1980 		 * data may creep in... ick.
   1981 		 */
   1982 		if (th->th_urp <= (u_int16_t) tlen
   1983 #ifdef SO_OOBINLINE
   1984 		     && (so->so_options & SO_OOBINLINE) == 0
   1985 #endif
   1986 		     )
   1987 			tcp_pulloutofband(so, th, m, hdroptlen);
   1988 	} else
   1989 		/*
   1990 		 * If no out of band data is expected,
   1991 		 * pull receive urgent pointer along
   1992 		 * with the receive window.
   1993 		 */
   1994 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
   1995 			tp->rcv_up = tp->rcv_nxt;
   1996 dodata:							/* XXX */
   1997 
   1998 	/*
   1999 	 * Process the segment text, merging it into the TCP sequencing queue,
   2000 	 * and arranging for acknowledgement of receipt if necessary.
   2001 	 * This process logically involves adjusting tp->rcv_wnd as data
   2002 	 * is presented to the user (this happens in tcp_usrreq.c,
   2003 	 * case PRU_RCVD).  If a FIN has already been received on this
   2004 	 * connection then we just ignore the text.
   2005 	 */
   2006 	if ((tlen || (tiflags & TH_FIN)) &&
   2007 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
   2008 		/*
   2009 		 * Insert segment ti into reassembly queue of tcp with
   2010 		 * control block tp.  Return TH_FIN if reassembly now includes
   2011 		 * a segment with FIN.  The macro form does the common case
   2012 		 * inline (segment is the next to be received on an
   2013 		 * established connection, and the queue is empty),
   2014 		 * avoiding linkage into and removal from the queue and
   2015 		 * repetition of various conversions.
   2016 		 * Set DELACK for segments received in order, but ack
   2017 		 * immediately when segments are out of order
   2018 		 * (so fast retransmit can work).
   2019 		 */
   2020 		/* NOTE: this was TCP_REASS() macro, but used only once */
   2021 		TCP_REASS_LOCK(tp);
   2022 		if (th->th_seq == tp->rcv_nxt &&
   2023 		    tp->segq.lh_first == NULL &&
   2024 		    tp->t_state == TCPS_ESTABLISHED) {
   2025 			TCP_SETUP_ACK(tp, th);
   2026 			tp->rcv_nxt += tlen;
   2027 			tiflags = th->th_flags & TH_FIN;
   2028 			tcpstat.tcps_rcvpack++;
   2029 			tcpstat.tcps_rcvbyte += tlen;
   2030 			ND6_HINT(tp);
   2031 			m_adj(m, hdroptlen);
   2032 			sbappend(&(so)->so_rcv, m);
   2033 			sorwakeup(so);
   2034 		} else {
   2035 			m_adj(m, hdroptlen);
   2036 			tiflags = tcp_reass(tp, th, m, &tlen);
   2037 			tp->t_flags |= TF_ACKNOW;
   2038 		}
   2039 		TCP_REASS_UNLOCK(tp);
   2040 
   2041 		/*
   2042 		 * Note the amount of data that peer has sent into
   2043 		 * our window, in order to estimate the sender's
   2044 		 * buffer size.
   2045 		 */
   2046 		len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
   2047 	} else {
   2048 		m_freem(m);
   2049 		m = NULL;
   2050 		tiflags &= ~TH_FIN;
   2051 	}
   2052 
   2053 	/*
   2054 	 * If FIN is received ACK the FIN and let the user know
   2055 	 * that the connection is closing.  Ignore a FIN received before
   2056 	 * the connection is fully established.
   2057 	 */
   2058 	if ((tiflags & TH_FIN) && TCPS_HAVEESTABLISHED(tp->t_state)) {
   2059 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
   2060 			socantrcvmore(so);
   2061 			tp->t_flags |= TF_ACKNOW;
   2062 			tp->rcv_nxt++;
   2063 		}
   2064 		switch (tp->t_state) {
   2065 
   2066 	 	/*
   2067 		 * In ESTABLISHED STATE enter the CLOSE_WAIT state.
   2068 		 */
   2069 		case TCPS_ESTABLISHED:
   2070 			tp->t_state = TCPS_CLOSE_WAIT;
   2071 			break;
   2072 
   2073 	 	/*
   2074 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
   2075 		 * enter the CLOSING state.
   2076 		 */
   2077 		case TCPS_FIN_WAIT_1:
   2078 			tp->t_state = TCPS_CLOSING;
   2079 			break;
   2080 
   2081 	 	/*
   2082 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
   2083 		 * starting the time-wait timer, turning off the other
   2084 		 * standard timers.
   2085 		 */
   2086 		case TCPS_FIN_WAIT_2:
   2087 			tp->t_state = TCPS_TIME_WAIT;
   2088 			tcp_canceltimers(tp);
   2089 			TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
   2090 			soisdisconnected(so);
   2091 			break;
   2092 
   2093 		/*
   2094 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
   2095 		 */
   2096 		case TCPS_TIME_WAIT:
   2097 			TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
   2098 			break;
   2099 		}
   2100 	}
   2101 	if (so->so_options & SO_DEBUG) {
   2102 		tcp_trace(TA_INPUT, ostate, tp, tcp_saveti, 0);
   2103 	}
   2104 
   2105 	/*
   2106 	 * Return any desired output.
   2107 	 */
   2108 	if (needoutput || (tp->t_flags & TF_ACKNOW))
   2109 		(void) tcp_output(tp);
   2110 	if (tcp_saveti)
   2111 		m_freem(tcp_saveti);
   2112 	return;
   2113 
   2114 badsyn:
   2115 	/*
   2116 	 * Received a bad SYN.  Increment counters and dropwithreset.
   2117 	 */
   2118 	tcpstat.tcps_badsyn++;
   2119 	tp = NULL;
   2120 	goto dropwithreset;
   2121 
   2122 dropafterack:
   2123 	/*
   2124 	 * Generate an ACK dropping incoming segment if it occupies
   2125 	 * sequence space, where the ACK reflects our state.
   2126 	 */
   2127 	if (tiflags & TH_RST)
   2128 		goto drop;
   2129 	m_freem(m);
   2130 	tp->t_flags |= TF_ACKNOW;
   2131 	(void) tcp_output(tp);
   2132 	if (tcp_saveti)
   2133 		m_freem(tcp_saveti);
   2134 	return;
   2135 
   2136 dropwithreset_ratelim:
   2137 	/*
   2138 	 * We may want to rate-limit RSTs in certain situations,
   2139 	 * particularly if we are sending an RST in response to
   2140 	 * an attempt to connect to or otherwise communicate with
   2141 	 * a port for which we have no socket.
   2142 	 */
   2143 	if (ratecheck(&tcp_rst_ratelim_last, &tcp_rst_ratelim) == 0) {
   2144 		/* XXX stat */
   2145 		goto drop;
   2146 	}
   2147 	/* ...fall into dropwithreset... */
   2148 
   2149 dropwithreset:
   2150 	/*
   2151 	 * Generate a RST, dropping incoming segment.
   2152 	 * Make ACK acceptable to originator of segment.
   2153 	 */
   2154 	if (tiflags & TH_RST)
   2155 		goto drop;
   2156     {
   2157 	/*
   2158 	 * need to recover version # field, which was overwritten on
   2159 	 * ip_cksum computation.
   2160 	 */
   2161 	struct ip *sip;
   2162 	sip = mtod(m, struct ip *);
   2163 	switch (af) {
   2164 	case AF_INET:
   2165 		sip->ip_v = 4;
   2166 		break;
   2167 #ifdef INET6
   2168 	case AF_INET6:
   2169 		sip->ip_v = 6;
   2170 		break;
   2171 #endif
   2172 	}
   2173     }
   2174 	if (tiflags & TH_ACK)
   2175 		(void)tcp_respond(tp, m, m, th, (tcp_seq)0, th->th_ack, TH_RST);
   2176 	else {
   2177 		if (tiflags & TH_SYN)
   2178 			tlen++;
   2179 		(void)tcp_respond(tp, m, m, th, th->th_seq + tlen, (tcp_seq)0,
   2180 		    TH_RST|TH_ACK);
   2181 	}
   2182 	if (tcp_saveti)
   2183 		m_freem(tcp_saveti);
   2184 	return;
   2185 
   2186 drop:
   2187 	/*
   2188 	 * Drop space held by incoming segment and return.
   2189 	 */
   2190 	if (tp) {
   2191 		if (tp->t_inpcb)
   2192 			so = tp->t_inpcb->inp_socket;
   2193 #ifdef INET6
   2194 		else if (tp->t_in6pcb)
   2195 			so = tp->t_in6pcb->in6p_socket;
   2196 #endif
   2197 		else
   2198 			so = NULL;
   2199 		if (so && (so->so_options & SO_DEBUG) != 0)
   2200 			tcp_trace(TA_DROP, ostate, tp, tcp_saveti, 0);
   2201 	}
   2202 	if (tcp_saveti)
   2203 		m_freem(tcp_saveti);
   2204 	m_freem(m);
   2205 	return;
   2206 }
   2207 
   2208 void
   2209 tcp_dooptions(tp, cp, cnt, th, oi)
   2210 	struct tcpcb *tp;
   2211 	u_char *cp;
   2212 	int cnt;
   2213 	struct tcphdr *th;
   2214 	struct tcp_opt_info *oi;
   2215 {
   2216 	u_int16_t mss;
   2217 	int opt, optlen;
   2218 
   2219 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
   2220 		opt = cp[0];
   2221 		if (opt == TCPOPT_EOL)
   2222 			break;
   2223 		if (opt == TCPOPT_NOP)
   2224 			optlen = 1;
   2225 		else {
   2226 			optlen = cp[1];
   2227 			if (optlen <= 0)
   2228 				break;
   2229 		}
   2230 		switch (opt) {
   2231 
   2232 		default:
   2233 			continue;
   2234 
   2235 		case TCPOPT_MAXSEG:
   2236 			if (optlen != TCPOLEN_MAXSEG)
   2237 				continue;
   2238 			if (!(th->th_flags & TH_SYN))
   2239 				continue;
   2240 			bcopy(cp + 2, &mss, sizeof(mss));
   2241 			oi->maxseg = ntohs(mss);
   2242 			break;
   2243 
   2244 		case TCPOPT_WINDOW:
   2245 			if (optlen != TCPOLEN_WINDOW)
   2246 				continue;
   2247 			if (!(th->th_flags & TH_SYN))
   2248 				continue;
   2249 			tp->t_flags |= TF_RCVD_SCALE;
   2250 			tp->requested_s_scale = cp[2];
   2251 			if (tp->requested_s_scale > TCP_MAX_WINSHIFT) {
   2252 #if 0	/*XXX*/
   2253 				char *p;
   2254 
   2255 				if (ip)
   2256 					p = ntohl(ip->ip_src);
   2257 #ifdef INET6
   2258 				else if (ip6)
   2259 					p = ip6_sprintf(&ip6->ip6_src);
   2260 #endif
   2261 				else
   2262 					p = "(unknown)";
   2263 				log(LOG_ERR, "TCP: invalid wscale %d from %s, "
   2264 				    "assuming %d\n",
   2265 				    tp->requested_s_scale, p,
   2266 				    TCP_MAX_WINSHIFT);
   2267 #else
   2268 				log(LOG_ERR, "TCP: invalid wscale %d, "
   2269 				    "assuming %d\n",
   2270 				    tp->requested_s_scale,
   2271 				    TCP_MAX_WINSHIFT);
   2272 #endif
   2273 				tp->requested_s_scale = TCP_MAX_WINSHIFT;
   2274 			}
   2275 			break;
   2276 
   2277 		case TCPOPT_TIMESTAMP:
   2278 			if (optlen != TCPOLEN_TIMESTAMP)
   2279 				continue;
   2280 			oi->ts_present = 1;
   2281 			bcopy(cp + 2, &oi->ts_val, sizeof(oi->ts_val));
   2282 			NTOHL(oi->ts_val);
   2283 			bcopy(cp + 6, &oi->ts_ecr, sizeof(oi->ts_ecr));
   2284 			NTOHL(oi->ts_ecr);
   2285 
   2286 			/*
   2287 			 * A timestamp received in a SYN makes
   2288 			 * it ok to send timestamp requests and replies.
   2289 			 */
   2290 			if (th->th_flags & TH_SYN) {
   2291 				tp->t_flags |= TF_RCVD_TSTMP;
   2292 				tp->ts_recent = oi->ts_val;
   2293 				tp->ts_recent_age = tcp_now;
   2294 			}
   2295 			break;
   2296 		case TCPOPT_SACK_PERMITTED:
   2297 			if (optlen != TCPOLEN_SACK_PERMITTED)
   2298 				continue;
   2299 			if (!(th->th_flags & TH_SYN))
   2300 				continue;
   2301 			tp->t_flags &= ~TF_CANT_TXSACK;
   2302 			break;
   2303 
   2304 		case TCPOPT_SACK:
   2305 			if (tp->t_flags & TF_IGNR_RXSACK)
   2306 				continue;
   2307 			if (optlen % 8 != 2 || optlen < 10)
   2308 				continue;
   2309 			cp += 2;
   2310 			optlen -= 2;
   2311 			for (; optlen > 0; cp -= 8, optlen -= 8) {
   2312 				tcp_seq lwe, rwe;
   2313 				bcopy((char *)cp, (char *) &lwe, sizeof(lwe));
   2314 				NTOHL(lwe);
   2315 				bcopy((char *)cp, (char *) &rwe, sizeof(rwe));
   2316 				NTOHL(rwe);
   2317 				/* tcp_mark_sacked(tp, lwe, rwe); */
   2318 			}
   2319 			break;
   2320 		}
   2321 	}
   2322 }
   2323 
   2324 /*
   2325  * Pull out of band byte out of a segment so
   2326  * it doesn't appear in the user's data queue.
   2327  * It is still reflected in the segment length for
   2328  * sequencing purposes.
   2329  */
   2330 void
   2331 tcp_pulloutofband(so, th, m, off)
   2332 	struct socket *so;
   2333 	struct tcphdr *th;
   2334 	struct mbuf *m;
   2335 	int off;
   2336 {
   2337 	int cnt = off + th->th_urp - 1;
   2338 
   2339 	while (cnt >= 0) {
   2340 		if (m->m_len > cnt) {
   2341 			char *cp = mtod(m, caddr_t) + cnt;
   2342 			struct tcpcb *tp = sototcpcb(so);
   2343 
   2344 			tp->t_iobc = *cp;
   2345 			tp->t_oobflags |= TCPOOB_HAVEDATA;
   2346 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
   2347 			m->m_len--;
   2348 			return;
   2349 		}
   2350 		cnt -= m->m_len;
   2351 		m = m->m_next;
   2352 		if (m == 0)
   2353 			break;
   2354 	}
   2355 	panic("tcp_pulloutofband");
   2356 }
   2357 
   2358 /*
   2359  * Collect new round-trip time estimate
   2360  * and update averages and current timeout.
   2361  */
   2362 void
   2363 tcp_xmit_timer(tp, rtt)
   2364 	struct tcpcb *tp;
   2365 	short rtt;
   2366 {
   2367 	short delta;
   2368 	short rttmin;
   2369 
   2370 	tcpstat.tcps_rttupdated++;
   2371 	--rtt;
   2372 	if (tp->t_srtt != 0) {
   2373 		/*
   2374 		 * srtt is stored as fixed point with 3 bits after the
   2375 		 * binary point (i.e., scaled by 8).  The following magic
   2376 		 * is equivalent to the smoothing algorithm in rfc793 with
   2377 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
   2378 		 * point).  Adjust rtt to origin 0.
   2379 		 */
   2380 		delta = (rtt << 2) - (tp->t_srtt >> TCP_RTT_SHIFT);
   2381 		if ((tp->t_srtt += delta) <= 0)
   2382 			tp->t_srtt = 1 << 2;
   2383 		/*
   2384 		 * We accumulate a smoothed rtt variance (actually, a
   2385 		 * smoothed mean difference), then set the retransmit
   2386 		 * timer to smoothed rtt + 4 times the smoothed variance.
   2387 		 * rttvar is stored as fixed point with 2 bits after the
   2388 		 * binary point (scaled by 4).  The following is
   2389 		 * equivalent to rfc793 smoothing with an alpha of .75
   2390 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
   2391 		 * rfc793's wired-in beta.
   2392 		 */
   2393 		if (delta < 0)
   2394 			delta = -delta;
   2395 		delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
   2396 		if ((tp->t_rttvar += delta) <= 0)
   2397 			tp->t_rttvar = 1 << 2;
   2398 	} else {
   2399 		/*
   2400 		 * No rtt measurement yet - use the unsmoothed rtt.
   2401 		 * Set the variance to half the rtt (so our first
   2402 		 * retransmit happens at 3*rtt).
   2403 		 */
   2404 		tp->t_srtt = rtt << (TCP_RTT_SHIFT + 2);
   2405 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT + 2 - 1);
   2406 	}
   2407 	tp->t_rtt = 0;
   2408 	tp->t_rxtshift = 0;
   2409 
   2410 	/*
   2411 	 * the retransmit should happen at rtt + 4 * rttvar.
   2412 	 * Because of the way we do the smoothing, srtt and rttvar
   2413 	 * will each average +1/2 tick of bias.  When we compute
   2414 	 * the retransmit timer, we want 1/2 tick of rounding and
   2415 	 * 1 extra tick because of +-1/2 tick uncertainty in the
   2416 	 * firing of the timer.  The bias will give us exactly the
   2417 	 * 1.5 tick we need.  But, because the bias is
   2418 	 * statistical, we have to test that we don't drop below
   2419 	 * the minimum feasible timer (which is 2 ticks).
   2420 	 */
   2421 	if (tp->t_rttmin > rtt + 2)
   2422 		rttmin = tp->t_rttmin;
   2423 	else
   2424 		rttmin = rtt + 2;
   2425 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), rttmin, TCPTV_REXMTMAX);
   2426 
   2427 	/*
   2428 	 * We received an ack for a packet that wasn't retransmitted;
   2429 	 * it is probably safe to discard any error indications we've
   2430 	 * received recently.  This isn't quite right, but close enough
   2431 	 * for now (a route might have failed after we sent a segment,
   2432 	 * and the return path might not be symmetrical).
   2433 	 */
   2434 	tp->t_softerror = 0;
   2435 }
   2436 
   2437 /*
   2438  * Checks for partial ack.  If partial ack arrives, force the retransmission
   2439  * of the next unacknowledged segment, do not clear tp->t_dupacks, and return
   2440  * 1.  By setting snd_nxt to th_ack, this forces retransmission timer to
   2441  * be started again.  If the ack advances at least to tp->snd_recover, return 0.
   2442  */
   2443 int
   2444 tcp_newreno(tp, th)
   2445 	struct tcpcb *tp;
   2446 	struct tcphdr *th;
   2447 {
   2448 	tcp_seq onxt = tp->snd_nxt;
   2449 	u_long ocwnd = tp->snd_cwnd;
   2450 
   2451 	if (SEQ_LT(th->th_ack, tp->snd_recover)) {
   2452 		/*
   2453 		 * snd_una has not yet been updated and the socket's send
   2454 		 * buffer has not yet drained off the ACK'd data, so we
   2455 		 * have to leave snd_una as it was to get the correct data
   2456 		 * offset in tcp_output().
   2457 		 */
   2458 		TCP_TIMER_DISARM(tp, TCPT_REXMT);
   2459 	        tp->t_rtt = 0;
   2460 	        tp->snd_nxt = th->th_ack;
   2461 		/*
   2462 		 * Set snd_cwnd to one segment beyond ACK'd offset.  snd_una
   2463 		 * is not yet updated when we're called.
   2464 		 */
   2465 		tp->snd_cwnd = tp->t_segsz + (th->th_ack - tp->snd_una);
   2466 	        (void) tcp_output(tp);
   2467 	        tp->snd_cwnd = ocwnd;
   2468 	        if (SEQ_GT(onxt, tp->snd_nxt))
   2469 	                tp->snd_nxt = onxt;
   2470 	        /*
   2471 	         * Partial window deflation.  Relies on fact that tp->snd_una
   2472 	         * not updated yet.
   2473 	         */
   2474 	        tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_segsz);
   2475 	        return 1;
   2476 	}
   2477 	return 0;
   2478 }
   2479 
   2480 
   2481 /*
   2482  * TCP compressed state engine.  Currently used to hold compressed
   2483  * state for SYN_RECEIVED.
   2484  */
   2485 
   2486 u_long	syn_cache_count;
   2487 u_int32_t syn_hash1, syn_hash2;
   2488 
   2489 #define SYN_HASH(sa, sp, dp) \
   2490 	((((sa)->s_addr^syn_hash1)*(((((u_int32_t)(dp))<<16) + \
   2491 				     ((u_int32_t)(sp)))^syn_hash2)))
   2492 #ifndef INET6
   2493 #define	SYN_HASHALL(hash, src, dst) \
   2494 do {									\
   2495 	hash = SYN_HASH(&((struct sockaddr_in *)(src))->sin_addr,	\
   2496 		((struct sockaddr_in *)(src))->sin_port,		\
   2497 		((struct sockaddr_in *)(dst))->sin_port);		\
   2498 } while (0)
   2499 #else
   2500 #define SYN_HASH6(sa, sp, dp) \
   2501 	((((sa)->s6_addr32[0] ^ (sa)->s6_addr32[3] ^ syn_hash1) * \
   2502 	  (((((u_int32_t)(dp))<<16) + ((u_int32_t)(sp)))^syn_hash2)) \
   2503 	 & 0x7fffffff)
   2504 
   2505 #define SYN_HASHALL(hash, src, dst) \
   2506 do {									\
   2507 	switch ((src)->sa_family) {					\
   2508 	case AF_INET:							\
   2509 		hash = SYN_HASH(&((struct sockaddr_in *)(src))->sin_addr, \
   2510 			((struct sockaddr_in *)(src))->sin_port,	\
   2511 			((struct sockaddr_in *)(dst))->sin_port);	\
   2512 		break;							\
   2513 	case AF_INET6:							\
   2514 		hash = SYN_HASH6(&((struct sockaddr_in6 *)(src))->sin6_addr, \
   2515 			((struct sockaddr_in6 *)(src))->sin6_port,	\
   2516 			((struct sockaddr_in6 *)(dst))->sin6_port);	\
   2517 		break;							\
   2518 	default:							\
   2519 		hash = 0;						\
   2520 	}								\
   2521 } while (0)
   2522 #endif /* INET6 */
   2523 
   2524 #define	SYN_CACHE_RM(sc)						\
   2525 do {									\
   2526 	LIST_REMOVE((sc), sc_bucketq);					\
   2527 	(sc)->sc_tp = NULL;						\
   2528 	LIST_REMOVE((sc), sc_tpq);					\
   2529 	tcp_syn_cache[(sc)->sc_bucketidx].sch_length--;			\
   2530 	TAILQ_REMOVE(&tcp_syn_cache_timeq[(sc)->sc_rxtshift], (sc), sc_timeq); \
   2531 	syn_cache_count--;						\
   2532 } while (0)
   2533 
   2534 #define	SYN_CACHE_PUT(sc)						\
   2535 do {									\
   2536 	if ((sc)->sc_ipopts)						\
   2537 		(void) m_free((sc)->sc_ipopts);				\
   2538 	if ((sc)->sc_route4.ro_rt != NULL)				\
   2539 		RTFREE((sc)->sc_route4.ro_rt);				\
   2540 	pool_put(&syn_cache_pool, (sc));				\
   2541 } while (0)
   2542 
   2543 struct pool syn_cache_pool;
   2544 
   2545 /*
   2546  * We don't estimate RTT with SYNs, so each packet starts with the default
   2547  * RTT and each timer queue has a fixed timeout value.  This allows us to
   2548  * optimize the timer queues somewhat.
   2549  */
   2550 #define	SYN_CACHE_TIMER_ARM(sc)						\
   2551 do {									\
   2552 	TCPT_RANGESET((sc)->sc_rxtcur,					\
   2553 	    TCPTV_SRTTDFLT * tcp_backoff[(sc)->sc_rxtshift], TCPTV_MIN,	\
   2554 	    TCPTV_REXMTMAX);						\
   2555 	PRT_SLOW_ARM((sc)->sc_rexmt, (sc)->sc_rxtcur);			\
   2556 } while (0)
   2557 
   2558 TAILQ_HEAD(, syn_cache) tcp_syn_cache_timeq[TCP_MAXRXTSHIFT + 1];
   2559 
   2560 void
   2561 syn_cache_init()
   2562 {
   2563 	int i;
   2564 
   2565 	/* Initialize the hash buckets. */
   2566 	for (i = 0; i < tcp_syn_cache_size; i++)
   2567 		LIST_INIT(&tcp_syn_cache[i].sch_bucket);
   2568 
   2569 	/* Initialize the timer queues. */
   2570 	for (i = 0; i <= TCP_MAXRXTSHIFT; i++)
   2571 		TAILQ_INIT(&tcp_syn_cache_timeq[i]);
   2572 
   2573 	/* Initialize the syn cache pool. */
   2574 	pool_init(&syn_cache_pool, sizeof(struct syn_cache), 0, 0, 0,
   2575 	    "synpl", 0, NULL, NULL, M_PCB);
   2576 }
   2577 
   2578 void
   2579 syn_cache_insert(sc, tp)
   2580 	struct syn_cache *sc;
   2581 	struct tcpcb *tp;
   2582 {
   2583 	struct syn_cache_head *scp;
   2584 	struct syn_cache *sc2;
   2585 	int s, i;
   2586 
   2587 	/*
   2588 	 * If there are no entries in the hash table, reinitialize
   2589 	 * the hash secrets.
   2590 	 */
   2591 	if (syn_cache_count == 0) {
   2592 		struct timeval tv;
   2593 		microtime(&tv);
   2594 		syn_hash1 = random() ^ (u_long)&sc;
   2595 		syn_hash2 = random() ^ tv.tv_usec;
   2596 	}
   2597 
   2598 	SYN_HASHALL(sc->sc_hash, &sc->sc_src.sa, &sc->sc_dst.sa);
   2599 	sc->sc_bucketidx = sc->sc_hash % tcp_syn_cache_size;
   2600 	scp = &tcp_syn_cache[sc->sc_bucketidx];
   2601 
   2602 	/*
   2603 	 * Make sure that we don't overflow the per-bucket
   2604 	 * limit or the total cache size limit.
   2605 	 */
   2606 	s = splsoftnet();
   2607 	if (scp->sch_length >= tcp_syn_bucket_limit) {
   2608 		tcpstat.tcps_sc_bucketoverflow++;
   2609 		/*
   2610 		 * The bucket is full.  Toss the oldest element in the
   2611 		 * bucket.  This will be the entry with our bucket
   2612 		 * index closest to the front of the timer queue with
   2613 		 * the largest timeout value.
   2614 		 *
   2615 		 * Note: This timer queue traversal may be expensive, so
   2616 		 * we hope that this doesn't happen very often.  It is
   2617 		 * much more likely that we'll overflow the entire
   2618 		 * cache, which is much easier to handle; see below.
   2619 		 */
   2620 		for (i = TCP_MAXRXTSHIFT; i >= 0; i--) {
   2621 			for (sc2 = TAILQ_FIRST(&tcp_syn_cache_timeq[i]);
   2622 			     sc2 != NULL;
   2623 			     sc2 = TAILQ_NEXT(sc2, sc_timeq)) {
   2624 				if (sc2->sc_bucketidx == sc->sc_bucketidx) {
   2625 					SYN_CACHE_RM(sc2);
   2626 					SYN_CACHE_PUT(sc2);
   2627 					goto insert;	/* 2 level break */
   2628 				}
   2629 			}
   2630 		}
   2631 #ifdef DIAGNOSTIC
   2632 		/*
   2633 		 * This should never happen; we should always find an
   2634 		 * entry in our bucket.
   2635 		 */
   2636 		panic("syn_cache_insert: bucketoverflow: impossible");
   2637 #endif
   2638 	} else if (syn_cache_count >= tcp_syn_cache_limit) {
   2639 		tcpstat.tcps_sc_overflowed++;
   2640 		/*
   2641 		 * The cache is full.  Toss the oldest entry in the
   2642 		 * entire cache.  This is the front entry in the
   2643 		 * first non-empty timer queue with the largest
   2644 		 * timeout value.
   2645 		 */
   2646 		for (i = TCP_MAXRXTSHIFT; i >= 0; i--) {
   2647 			sc2 = TAILQ_FIRST(&tcp_syn_cache_timeq[i]);
   2648 			if (sc2 == NULL)
   2649 				continue;
   2650 			SYN_CACHE_RM(sc2);
   2651 			SYN_CACHE_PUT(sc2);
   2652 			goto insert;		/* symmetry with above */
   2653 		}
   2654 #ifdef DIAGNOSTIC
   2655 		/*
   2656 		 * This should never happen; we should always find an
   2657 		 * entry in the cache.
   2658 		 */
   2659 		panic("syn_cache_insert: cache overflow: impossible");
   2660 #endif
   2661 	}
   2662 
   2663  insert:
   2664 	/*
   2665 	 * Initialize the entry's timer.
   2666 	 */
   2667 	sc->sc_rxttot = 0;
   2668 	sc->sc_rxtshift = 0;
   2669 	SYN_CACHE_TIMER_ARM(sc);
   2670 	TAILQ_INSERT_TAIL(&tcp_syn_cache_timeq[sc->sc_rxtshift], sc, sc_timeq);
   2671 
   2672 	/* Link it from tcpcb entry */
   2673 	LIST_INSERT_HEAD(&tp->t_sc, sc, sc_tpq);
   2674 
   2675 	/* Put it into the bucket. */
   2676 	LIST_INSERT_HEAD(&scp->sch_bucket, sc, sc_bucketq);
   2677 	scp->sch_length++;
   2678 	syn_cache_count++;
   2679 
   2680 	tcpstat.tcps_sc_added++;
   2681 	splx(s);
   2682 }
   2683 
   2684 /*
   2685  * Walk the timer queues, looking for SYN,ACKs that need to be retransmitted.
   2686  * If we have retransmitted an entry the maximum number of times, expire
   2687  * that entry.
   2688  */
   2689 void
   2690 syn_cache_timer()
   2691 {
   2692 	struct syn_cache *sc, *nsc;
   2693 	int i, s;
   2694 
   2695 	s = splsoftnet();
   2696 
   2697 	/*
   2698 	 * First, get all the entries that need to be retransmitted, or
   2699 	 * must be expired due to exceeding the initial keepalive time.
   2700 	 */
   2701 	for (i = 0; i < TCP_MAXRXTSHIFT; i++) {
   2702 		for (sc = TAILQ_FIRST(&tcp_syn_cache_timeq[i]);
   2703 		     sc != NULL && PRT_SLOW_ISEXPIRED(sc->sc_rexmt);
   2704 		     sc = nsc) {
   2705 			nsc = TAILQ_NEXT(sc, sc_timeq);
   2706 
   2707 			/*
   2708 			 * Compute the total amount of time this entry has
   2709 			 * been on a queue.  If this entry has been on longer
   2710 			 * than the keep alive timer would allow, expire it.
   2711 			 */
   2712 			sc->sc_rxttot += sc->sc_rxtcur;
   2713 			if (sc->sc_rxttot >= TCPTV_KEEP_INIT) {
   2714 				tcpstat.tcps_sc_timed_out++;
   2715 				SYN_CACHE_RM(sc);
   2716 				SYN_CACHE_PUT(sc);
   2717 				continue;
   2718 			}
   2719 
   2720 			tcpstat.tcps_sc_retransmitted++;
   2721 			(void) syn_cache_respond(sc, NULL);
   2722 
   2723 			/* Advance this entry onto the next timer queue. */
   2724 			TAILQ_REMOVE(&tcp_syn_cache_timeq[i], sc, sc_timeq);
   2725 			sc->sc_rxtshift = i + 1;
   2726 			SYN_CACHE_TIMER_ARM(sc);
   2727 			TAILQ_INSERT_TAIL(&tcp_syn_cache_timeq[sc->sc_rxtshift],
   2728 			    sc, sc_timeq);
   2729 		}
   2730 	}
   2731 
   2732 	/*
   2733 	 * Now get all the entries that are expired due to too many
   2734 	 * retransmissions.
   2735 	 */
   2736 	for (sc = TAILQ_FIRST(&tcp_syn_cache_timeq[TCP_MAXRXTSHIFT]);
   2737 	     sc != NULL && PRT_SLOW_ISEXPIRED(sc->sc_rexmt);
   2738 	     sc = nsc) {
   2739 		nsc = TAILQ_NEXT(sc, sc_timeq);
   2740 		tcpstat.tcps_sc_timed_out++;
   2741 		SYN_CACHE_RM(sc);
   2742 		SYN_CACHE_PUT(sc);
   2743 	}
   2744 	splx(s);
   2745 }
   2746 
   2747 /*
   2748  * Remove syn cache created by the specified tcb entry,
   2749  * because this does not make sense to keep them
   2750  * (if there's no tcb entry, syn cache entry will never be used)
   2751  */
   2752 void
   2753 syn_cache_cleanup(tp)
   2754 	struct tcpcb *tp;
   2755 {
   2756 	struct syn_cache *sc, *nsc;
   2757 	int s;
   2758 
   2759 	s = splsoftnet();
   2760 
   2761 	for (sc = LIST_FIRST(&tp->t_sc); sc != NULL; sc = nsc) {
   2762 		nsc = LIST_NEXT(sc, sc_tpq);
   2763 
   2764 #ifdef DIAGNOSTIC
   2765 		if (sc->sc_tp != tp)
   2766 			panic("invalid sc_tp in syn_cache_cleanup");
   2767 #endif
   2768 		SYN_CACHE_RM(sc);
   2769 		SYN_CACHE_PUT(sc);
   2770 	}
   2771 	/* just for safety */
   2772 	LIST_INIT(&tp->t_sc);
   2773 
   2774 	splx(s);
   2775 }
   2776 
   2777 /*
   2778  * Find an entry in the syn cache.
   2779  */
   2780 struct syn_cache *
   2781 syn_cache_lookup(src, dst, headp)
   2782 	struct sockaddr *src;
   2783 	struct sockaddr *dst;
   2784 	struct syn_cache_head **headp;
   2785 {
   2786 	struct syn_cache *sc;
   2787 	struct syn_cache_head *scp;
   2788 	u_int32_t hash;
   2789 	int s;
   2790 
   2791 	SYN_HASHALL(hash, src, dst);
   2792 
   2793 	scp = &tcp_syn_cache[hash % tcp_syn_cache_size];
   2794 	*headp = scp;
   2795 	s = splsoftnet();
   2796 	for (sc = LIST_FIRST(&scp->sch_bucket); sc != NULL;
   2797 	     sc = LIST_NEXT(sc, sc_bucketq)) {
   2798 		if (sc->sc_hash != hash)
   2799 			continue;
   2800 		if (!bcmp(&sc->sc_src, src, src->sa_len) &&
   2801 		    !bcmp(&sc->sc_dst, dst, dst->sa_len)) {
   2802 			splx(s);
   2803 			return (sc);
   2804 		}
   2805 	}
   2806 	splx(s);
   2807 	return (NULL);
   2808 }
   2809 
   2810 /*
   2811  * This function gets called when we receive an ACK for a
   2812  * socket in the LISTEN state.  We look up the connection
   2813  * in the syn cache, and if its there, we pull it out of
   2814  * the cache and turn it into a full-blown connection in
   2815  * the SYN-RECEIVED state.
   2816  *
   2817  * The return values may not be immediately obvious, and their effects
   2818  * can be subtle, so here they are:
   2819  *
   2820  *	NULL	SYN was not found in cache; caller should drop the
   2821  *		packet and send an RST.
   2822  *
   2823  *	-1	We were unable to create the new connection, and are
   2824  *		aborting it.  An ACK,RST is being sent to the peer
   2825  *		(unless we got screwey sequence numbners; see below),
   2826  *		because the 3-way handshake has been completed.  Caller
   2827  *		should not free the mbuf, since we may be using it.  If
   2828  *		we are not, we will free it.
   2829  *
   2830  *	Otherwise, the return value is a pointer to the new socket
   2831  *	associated with the connection.
   2832  */
   2833 struct socket *
   2834 syn_cache_get(src, dst, th, hlen, tlen, so, m)
   2835 	struct sockaddr *src;
   2836 	struct sockaddr *dst;
   2837 	struct tcphdr *th;
   2838 	unsigned int hlen, tlen;
   2839 	struct socket *so;
   2840 	struct mbuf *m;
   2841 {
   2842 	struct syn_cache *sc;
   2843 	struct syn_cache_head *scp;
   2844 	struct inpcb *inp = NULL;
   2845 #ifdef INET6
   2846 	struct in6pcb *in6p = NULL;
   2847 #endif
   2848 	struct tcpcb *tp = 0;
   2849 	struct mbuf *am;
   2850 	int s;
   2851 	struct socket *oso;
   2852 
   2853 	s = splsoftnet();
   2854 	if ((sc = syn_cache_lookup(src, dst, &scp)) == NULL) {
   2855 		splx(s);
   2856 		return (NULL);
   2857 	}
   2858 
   2859 	/*
   2860 	 * Verify the sequence and ack numbers.  Try getting the correct
   2861 	 * response again.
   2862 	 */
   2863 	if ((th->th_ack != sc->sc_iss + 1) ||
   2864 	    SEQ_LEQ(th->th_seq, sc->sc_irs) ||
   2865 	    SEQ_GT(th->th_seq, sc->sc_irs + 1 + sc->sc_win)) {
   2866 		(void) syn_cache_respond(sc, m);
   2867 		splx(s);
   2868 		return ((struct socket *)(-1));
   2869 	}
   2870 
   2871 	/* Remove this cache entry */
   2872 	SYN_CACHE_RM(sc);
   2873 	splx(s);
   2874 
   2875 	/*
   2876 	 * Ok, create the full blown connection, and set things up
   2877 	 * as they would have been set up if we had created the
   2878 	 * connection when the SYN arrived.  If we can't create
   2879 	 * the connection, abort it.
   2880 	 */
   2881 	/*
   2882 	 * inp still has the OLD in_pcb stuff, set the
   2883 	 * v6-related flags on the new guy, too.   This is
   2884 	 * done particularly for the case where an AF_INET6
   2885 	 * socket is bound only to a port, and a v4 connection
   2886 	 * comes in on that port.
   2887 	 * we also copy the flowinfo from the original pcb
   2888 	 * to the new one.
   2889 	 */
   2890     {
   2891 	struct inpcb *parentinpcb;
   2892 
   2893 	parentinpcb = (struct inpcb *)so->so_pcb;
   2894 
   2895 	oso = so;
   2896 	so = sonewconn(so, SS_ISCONNECTED);
   2897 	if (so == NULL)
   2898 		goto resetandabort;
   2899 
   2900 	switch (so->so_proto->pr_domain->dom_family) {
   2901 	case AF_INET:
   2902 		inp = sotoinpcb(so);
   2903 		break;
   2904 #ifdef INET6
   2905 	case AF_INET6:
   2906 		in6p = sotoin6pcb(so);
   2907 #if 0 /*def INET6*/
   2908 		inp->inp_flags |= (parentinpcb->inp_flags &
   2909 			(INP_IPV6 | INP_IPV6_UNDEC | INP_IPV6_MAPPED));
   2910 		if ((inp->inp_flags & INP_IPV6) &&
   2911 		   !(inp->inp_flags & INP_IPV6_MAPPED)) {
   2912 			inp->inp_ipv6.ip6_hlim = parentinpcb->inp_ipv6.ip6_hlim;
   2913 			inp->inp_ipv6.ip6_vfc = parentinpcb->inp_ipv6.ip6_vfc;
   2914 		}
   2915 #endif
   2916 		break;
   2917 #endif
   2918 	}
   2919     }
   2920 	switch (src->sa_family) {
   2921 	case AF_INET:
   2922 		if (inp) {
   2923 			inp->inp_laddr = ((struct sockaddr_in *)dst)->sin_addr;
   2924 			inp->inp_lport = ((struct sockaddr_in *)dst)->sin_port;
   2925 			inp->inp_options = ip_srcroute();
   2926 			in_pcbstate(inp, INP_BOUND);
   2927 			if (inp->inp_options == NULL) {
   2928 				inp->inp_options = sc->sc_ipopts;
   2929 				sc->sc_ipopts = NULL;
   2930 			}
   2931 		}
   2932 #ifdef INET6
   2933 		else if (in6p) {
   2934 			/* IPv4 packet to AF_INET6 socket */
   2935 			bzero(&in6p->in6p_laddr, sizeof(in6p->in6p_laddr));
   2936 			in6p->in6p_laddr.s6_addr16[5] = htons(0xffff);
   2937 			bcopy(&((struct sockaddr_in *)dst)->sin_addr,
   2938 				&in6p->in6p_laddr.s6_addr32[3],
   2939 				sizeof(((struct sockaddr_in *)dst)->sin_addr));
   2940 			in6p->in6p_lport = ((struct sockaddr_in *)dst)->sin_port;
   2941 			in6totcpcb(in6p)->t_family = AF_INET;
   2942 		}
   2943 #endif
   2944 		break;
   2945 #ifdef INET6
   2946 	case AF_INET6:
   2947 		if (in6p) {
   2948 			in6p->in6p_laddr = ((struct sockaddr_in6 *)dst)->sin6_addr;
   2949 			in6p->in6p_lport = ((struct sockaddr_in6 *)dst)->sin6_port;
   2950 #if 0
   2951 			in6p->in6p_flowinfo = ip6->ip6_flow & IPV6_FLOWINFO_MASK;
   2952 			/*inp->inp_options = ip6_srcroute();*/ /* soon. */
   2953 #endif
   2954 		}
   2955 		break;
   2956 #endif
   2957 	}
   2958 #ifdef INET6
   2959 	if (in6p && in6totcpcb(in6p)->t_family == AF_INET6 && sotoinpcb(oso)) {
   2960 		struct in6pcb *oin6p = sotoin6pcb(oso);
   2961 		/* inherit socket options from the listening socket */
   2962 		in6p->in6p_flags |= (oin6p->in6p_flags & IN6P_CONTROLOPTS);
   2963 		if (in6p->in6p_flags & IN6P_CONTROLOPTS) {
   2964 			m_freem(in6p->in6p_options);
   2965 			in6p->in6p_options = 0;
   2966 		}
   2967 		ip6_savecontrol(in6p, &in6p->in6p_options,
   2968 			mtod(m, struct ip6_hdr *), m);
   2969 	}
   2970 #endif
   2971 
   2972 #ifdef IPSEC
   2973 	/*
   2974 	 * we make a copy of policy, instead of sharing the policy,
   2975 	 * for better behavior in terms of SA lookup and dead SA removal.
   2976 	 */
   2977 	if (inp) {
   2978 		/* copy old policy into new socket's */
   2979 		if (ipsec_copy_policy(sotoinpcb(oso)->inp_sp, inp->inp_sp))
   2980 			printf("tcp_input: could not copy policy\n");
   2981 	}
   2982 #ifdef INET6
   2983 	else if (in6p) {
   2984 		/* copy old policy into new socket's */
   2985 		if (ipsec_copy_policy(sotoin6pcb(oso)->in6p_sp, in6p->in6p_sp))
   2986 			printf("tcp_input: could not copy policy\n");
   2987 	}
   2988 #endif
   2989 #endif
   2990 
   2991 	/*
   2992 	 * Give the new socket our cached route reference.
   2993 	 */
   2994 	if (inp)
   2995 		inp->inp_route = sc->sc_route4;		/* struct assignment */
   2996 #ifdef INET6
   2997 	else
   2998 		in6p->in6p_route = sc->sc_route6;
   2999 #endif
   3000 	sc->sc_route4.ro_rt = NULL;
   3001 
   3002 	am = m_get(M_DONTWAIT, MT_SONAME);	/* XXX */
   3003 	if (am == NULL)
   3004 		goto resetandabort;
   3005 	am->m_len = src->sa_len;
   3006 	bcopy(src, mtod(am, caddr_t), src->sa_len);
   3007 	if (inp) {
   3008 		if (in_pcbconnect(inp, am)) {
   3009 			(void) m_free(am);
   3010 			goto resetandabort;
   3011 		}
   3012 	}
   3013 #ifdef INET6
   3014 	else if (in6p) {
   3015 		if (src->sa_family == AF_INET) {
   3016 			/* IPv4 packet to AF_INET6 socket */
   3017 			struct sockaddr_in6 *sin6;
   3018 			sin6 = mtod(am, struct sockaddr_in6 *);
   3019 			am->m_len = sizeof(*sin6);
   3020 			bzero(sin6, sizeof(*sin6));
   3021 			sin6->sin6_family = AF_INET6;
   3022 			sin6->sin6_len = sizeof(*sin6);
   3023 			sin6->sin6_port = ((struct sockaddr_in *)src)->sin_port;
   3024 			sin6->sin6_addr.s6_addr16[5] = htons(0xffff);
   3025 			bcopy(&((struct sockaddr_in *)src)->sin_addr,
   3026 				&sin6->sin6_addr.s6_addr32[3],
   3027 				sizeof(sin6->sin6_addr.s6_addr32[3]));
   3028 		}
   3029 		if (in6_pcbconnect(in6p, am)) {
   3030 			(void) m_free(am);
   3031 			goto resetandabort;
   3032 		}
   3033 	}
   3034 #endif
   3035 	else {
   3036 		(void) m_free(am);
   3037 		goto resetandabort;
   3038 	}
   3039 	(void) m_free(am);
   3040 
   3041 	if (inp)
   3042 		tp = intotcpcb(inp);
   3043 #ifdef INET6
   3044 	else if (in6p)
   3045 		tp = in6totcpcb(in6p);
   3046 #endif
   3047 	else
   3048 		tp = NULL;
   3049 	if (sc->sc_request_r_scale != 15) {
   3050 		tp->requested_s_scale = sc->sc_requested_s_scale;
   3051 		tp->request_r_scale = sc->sc_request_r_scale;
   3052 		tp->snd_scale = sc->sc_requested_s_scale;
   3053 		tp->rcv_scale = sc->sc_request_r_scale;
   3054 		tp->t_flags |= TF_RCVD_SCALE;
   3055 	}
   3056 	if (sc->sc_flags & SCF_TIMESTAMP)
   3057 		tp->t_flags |= TF_RCVD_TSTMP;
   3058 
   3059 	tp->t_template = tcp_template(tp);
   3060 	if (tp->t_template == 0) {
   3061 		tp = tcp_drop(tp, ENOBUFS);	/* destroys socket */
   3062 		so = NULL;
   3063 		m_freem(m);
   3064 		goto abort;
   3065 	}
   3066 
   3067 	tp->iss = sc->sc_iss;
   3068 	tp->irs = sc->sc_irs;
   3069 	tcp_sendseqinit(tp);
   3070 	tcp_rcvseqinit(tp);
   3071 	tp->t_state = TCPS_SYN_RECEIVED;
   3072 	TCP_TIMER_ARM(tp, TCPT_KEEP, TCPTV_KEEP_INIT);
   3073 	tcpstat.tcps_accepts++;
   3074 
   3075 	/* Initialize tp->t_ourmss before we deal with the peer's! */
   3076 	tp->t_ourmss = sc->sc_ourmaxseg;
   3077 	tcp_mss_from_peer(tp, sc->sc_peermaxseg);
   3078 
   3079 	/*
   3080 	 * Initialize the initial congestion window.  If we
   3081 	 * had to retransmit the SYN,ACK, we must initialize cwnd
   3082 	 * to 1 segment (i.e. the Loss Window).
   3083 	 */
   3084 	if (sc->sc_rxtshift)
   3085 		tp->snd_cwnd = tp->t_peermss;
   3086 	else
   3087 		tp->snd_cwnd = TCP_INITIAL_WINDOW(tcp_init_win, tp->t_peermss);
   3088 
   3089 	tcp_rmx_rtt(tp);
   3090 	tp->snd_wl1 = sc->sc_irs;
   3091 	tp->rcv_up = sc->sc_irs + 1;
   3092 
   3093 	/*
   3094 	 * This is what whould have happened in tcp_ouput() when
   3095 	 * the SYN,ACK was sent.
   3096 	 */
   3097 	tp->snd_up = tp->snd_una;
   3098 	tp->snd_max = tp->snd_nxt = tp->iss+1;
   3099 	TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
   3100 	if (sc->sc_win > 0 && SEQ_GT(tp->rcv_nxt + sc->sc_win, tp->rcv_adv))
   3101 		tp->rcv_adv = tp->rcv_nxt + sc->sc_win;
   3102 	tp->last_ack_sent = tp->rcv_nxt;
   3103 
   3104 	tcpstat.tcps_sc_completed++;
   3105 	SYN_CACHE_PUT(sc);
   3106 	return (so);
   3107 
   3108 resetandabort:
   3109 	(void) tcp_respond(NULL, m, m, th,
   3110 			   th->th_seq + tlen, (tcp_seq)0, TH_RST|TH_ACK);
   3111 abort:
   3112 	if (so != NULL)
   3113 		(void) soabort(so);
   3114 	SYN_CACHE_PUT(sc);
   3115 	tcpstat.tcps_sc_aborted++;
   3116 	return ((struct socket *)(-1));
   3117 }
   3118 
   3119 /*
   3120  * This function is called when we get a RST for a
   3121  * non-existant connection, so that we can see if the
   3122  * connection is in the syn cache.  If it is, zap it.
   3123  */
   3124 
   3125 void
   3126 syn_cache_reset(src, dst, th)
   3127 	struct sockaddr *src;
   3128 	struct sockaddr *dst;
   3129 	struct tcphdr *th;
   3130 {
   3131 	struct syn_cache *sc;
   3132 	struct syn_cache_head *scp;
   3133 	int s = splsoftnet();
   3134 
   3135 	if ((sc = syn_cache_lookup(src, dst, &scp)) == NULL) {
   3136 		splx(s);
   3137 		return;
   3138 	}
   3139 	if (SEQ_LT(th->th_seq, sc->sc_irs) ||
   3140 	    SEQ_GT(th->th_seq, sc->sc_irs+1)) {
   3141 		splx(s);
   3142 		return;
   3143 	}
   3144 	SYN_CACHE_RM(sc);
   3145 	splx(s);
   3146 	tcpstat.tcps_sc_reset++;
   3147 	SYN_CACHE_PUT(sc);
   3148 }
   3149 
   3150 void
   3151 syn_cache_unreach(src, dst, th)
   3152 	struct sockaddr *src;
   3153 	struct sockaddr *dst;
   3154 	struct tcphdr *th;
   3155 {
   3156 	struct syn_cache *sc;
   3157 	struct syn_cache_head *scp;
   3158 	int s;
   3159 
   3160 	s = splsoftnet();
   3161 	if ((sc = syn_cache_lookup(src, dst, &scp)) == NULL) {
   3162 		splx(s);
   3163 		return;
   3164 	}
   3165 	/* If the sequence number != sc_iss, then it's a bogus ICMP msg */
   3166 	if (ntohl (th->th_seq) != sc->sc_iss) {
   3167 		splx(s);
   3168 		return;
   3169 	}
   3170 
   3171 	/*
   3172 	 * If we've rertransmitted 3 times and this is our second error,
   3173 	 * we remove the entry.  Otherwise, we allow it to continue on.
   3174 	 * This prevents us from incorrectly nuking an entry during a
   3175 	 * spurious network outage.
   3176 	 *
   3177 	 * See tcp_notify().
   3178 	 */
   3179 	if ((sc->sc_flags & SCF_UNREACH) == 0 || sc->sc_rxtshift < 3) {
   3180 		sc->sc_flags |= SCF_UNREACH;
   3181 		splx(s);
   3182 		return;
   3183 	}
   3184 
   3185 	SYN_CACHE_RM(sc);
   3186 	splx(s);
   3187 	tcpstat.tcps_sc_unreach++;
   3188 	SYN_CACHE_PUT(sc);
   3189 }
   3190 
   3191 /*
   3192  * Given a LISTEN socket and an inbound SYN request, add
   3193  * this to the syn cache, and send back a segment:
   3194  *	<SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
   3195  * to the source.
   3196  *
   3197  * IMPORTANT NOTE: We do _NOT_ ACK data that might accompany the SYN.
   3198  * Doing so would require that we hold onto the data and deliver it
   3199  * to the application.  However, if we are the target of a SYN-flood
   3200  * DoS attack, an attacker could send data which would eventually
   3201  * consume all available buffer space if it were ACKed.  By not ACKing
   3202  * the data, we avoid this DoS scenario.
   3203  */
   3204 
   3205 int
   3206 syn_cache_add(src, dst, th, hlen, so, m, optp, optlen, oi)
   3207 	struct sockaddr *src;
   3208 	struct sockaddr *dst;
   3209 	struct tcphdr *th;
   3210 	unsigned int hlen;
   3211 	struct socket *so;
   3212 	struct mbuf *m;
   3213 	u_char *optp;
   3214 	int optlen;
   3215 	struct tcp_opt_info *oi;
   3216 {
   3217 	struct tcpcb tb, *tp;
   3218 	long win;
   3219 	struct syn_cache *sc;
   3220 	struct syn_cache_head *scp;
   3221 	struct mbuf *ipopts;
   3222 
   3223 	tp = sototcpcb(so);
   3224 
   3225 	/*
   3226 	 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
   3227 	 *
   3228 	 * Note this check is performed in tcp_input() very early on.
   3229 	 */
   3230 
   3231 	/*
   3232 	 * Initialize some local state.
   3233 	 */
   3234 	win = sbspace(&so->so_rcv);
   3235 	if (win > TCP_MAXWIN)
   3236 		win = TCP_MAXWIN;
   3237 
   3238 	if (src->sa_family == AF_INET) {
   3239 		/*
   3240 		 * Remember the IP options, if any.
   3241 		 */
   3242 		ipopts = ip_srcroute();
   3243 	} else
   3244 		ipopts = NULL;
   3245 
   3246 	if (optp) {
   3247 		tb.t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0;
   3248 		tcp_dooptions(&tb, optp, optlen, th, oi);
   3249 	} else
   3250 		tb.t_flags = 0;
   3251 
   3252 	/*
   3253 	 * See if we already have an entry for this connection.
   3254 	 * If we do, resend the SYN,ACK.  We do not count this
   3255 	 * as a retransmission (XXX though maybe we should).
   3256 	 */
   3257 	if ((sc = syn_cache_lookup(src, dst, &scp)) != NULL) {
   3258 		tcpstat.tcps_sc_dupesyn++;
   3259 		if (ipopts) {
   3260 			/*
   3261 			 * If we were remembering a previous source route,
   3262 			 * forget it and use the new one we've been given.
   3263 			 */
   3264 			if (sc->sc_ipopts)
   3265 				(void) m_free(sc->sc_ipopts);
   3266 			sc->sc_ipopts = ipopts;
   3267 		}
   3268 		sc->sc_timestamp = tb.ts_recent;
   3269 		if (syn_cache_respond(sc, m) == 0) {
   3270 			tcpstat.tcps_sndacks++;
   3271 			tcpstat.tcps_sndtotal++;
   3272 		}
   3273 		return (1);
   3274 	}
   3275 
   3276 	sc = pool_get(&syn_cache_pool, PR_NOWAIT);
   3277 	if (sc == NULL) {
   3278 		if (ipopts)
   3279 			(void) m_free(ipopts);
   3280 		return (0);
   3281 	}
   3282 
   3283 	/*
   3284 	 * Fill in the cache, and put the necessary IP and TCP
   3285 	 * options into the reply.
   3286 	 */
   3287 	bzero(sc, sizeof(struct syn_cache));
   3288 	bcopy(src, &sc->sc_src, src->sa_len);
   3289 	bcopy(dst, &sc->sc_dst, dst->sa_len);
   3290 	sc->sc_flags = 0;
   3291 	sc->sc_ipopts = ipopts;
   3292 	sc->sc_irs = th->th_seq;
   3293 	sc->sc_iss = tcp_new_iss(sc, sizeof(struct syn_cache), 0);
   3294 	sc->sc_peermaxseg = oi->maxseg;
   3295 	sc->sc_ourmaxseg = tcp_mss_to_advertise(m->m_flags & M_PKTHDR ?
   3296 						m->m_pkthdr.rcvif : NULL,
   3297 						sc->sc_src.sa.sa_family);
   3298 	sc->sc_win = win;
   3299 	sc->sc_timestamp = tb.ts_recent;
   3300 	if (tcp_do_rfc1323 && (tb.t_flags & TF_RCVD_TSTMP))
   3301 		sc->sc_flags |= SCF_TIMESTAMP;
   3302 	if ((tb.t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
   3303 	    (TF_RCVD_SCALE|TF_REQ_SCALE)) {
   3304 		sc->sc_requested_s_scale = tb.requested_s_scale;
   3305 		sc->sc_request_r_scale = 0;
   3306 		while (sc->sc_request_r_scale < TCP_MAX_WINSHIFT &&
   3307 		    TCP_MAXWIN << sc->sc_request_r_scale <
   3308 		    so->so_rcv.sb_hiwat)
   3309 			sc->sc_request_r_scale++;
   3310 	} else {
   3311 		sc->sc_requested_s_scale = 15;
   3312 		sc->sc_request_r_scale = 15;
   3313 	}
   3314 	sc->sc_tp = tp;
   3315 	if (syn_cache_respond(sc, m) == 0) {
   3316 		syn_cache_insert(sc, tp);
   3317 		tcpstat.tcps_sndacks++;
   3318 		tcpstat.tcps_sndtotal++;
   3319 	} else {
   3320 		SYN_CACHE_PUT(sc);
   3321 		tcpstat.tcps_sc_dropped++;
   3322 	}
   3323 	return (1);
   3324 }
   3325 
   3326 int
   3327 syn_cache_respond(sc, m)
   3328 	struct syn_cache *sc;
   3329 	struct mbuf *m;
   3330 {
   3331 	struct route *ro;
   3332 	struct rtentry *rt;
   3333 	u_int8_t *optp;
   3334 	int optlen, error;
   3335 	u_int16_t tlen;
   3336 	struct ip *ip = NULL;
   3337 #ifdef INET6
   3338 	struct ip6_hdr *ip6 = NULL;
   3339 #endif
   3340 	struct tcphdr *th;
   3341 	u_int hlen;
   3342 
   3343 	switch (sc->sc_src.sa.sa_family) {
   3344 	case AF_INET:
   3345 		hlen = sizeof(struct ip);
   3346 		ro = &sc->sc_route4;
   3347 		break;
   3348 #ifdef INET6
   3349 	case AF_INET6:
   3350 		hlen = sizeof(struct ip6_hdr);
   3351 		ro = (struct route *)&sc->sc_route6;
   3352 		break;
   3353 #endif
   3354 	default:
   3355 		if (m)
   3356 			m_freem(m);
   3357 		return EAFNOSUPPORT;
   3358 	}
   3359 
   3360 	/* Compute the size of the TCP options. */
   3361 	optlen = 4 + (sc->sc_request_r_scale != 15 ? 4 : 0) +
   3362 	    ((sc->sc_flags & SCF_TIMESTAMP) ? TCPOLEN_TSTAMP_APPA : 0);
   3363 
   3364 	tlen = hlen + sizeof(struct tcphdr) + optlen;
   3365 
   3366 	/*
   3367 	 * Create the IP+TCP header from scratch.
   3368 	 */
   3369 	if (m)
   3370 		m_freem(m);
   3371 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   3372 	if (m && tlen > MHLEN) {
   3373 		MCLGET(m, M_DONTWAIT);
   3374 		if ((m->m_flags & M_EXT) == NULL) {
   3375 			m_freem(m);
   3376 			m = NULL;
   3377 		}
   3378 	}
   3379 	if (m == NULL)
   3380 		return (ENOBUFS);
   3381 
   3382 	/* Fixup the mbuf. */
   3383 	m->m_data += max_linkhdr;
   3384 	m->m_len = m->m_pkthdr.len = tlen;
   3385 #ifdef IPSEC
   3386 	if (sc->sc_tp) {
   3387 		struct tcpcb *tp;
   3388 		struct socket *so;
   3389 
   3390 		tp = sc->sc_tp;
   3391 		if (tp->t_inpcb)
   3392 			so = tp->t_inpcb->inp_socket;
   3393 #ifdef INET6
   3394 		else if (tp->t_in6pcb)
   3395 			so = tp->t_in6pcb->in6p_socket;
   3396 #endif
   3397 		else
   3398 			so = NULL;
   3399 		/* use IPsec policy on listening socket, on SYN ACK */
   3400 		ipsec_setsocket(m, so);
   3401 	}
   3402 #endif
   3403 	m->m_pkthdr.rcvif = NULL;
   3404 	memset(mtod(m, u_char *), 0, tlen);
   3405 
   3406 	switch (sc->sc_src.sa.sa_family) {
   3407 	case AF_INET:
   3408 		ip = mtod(m, struct ip *);
   3409 		ip->ip_dst = sc->sc_src.sin.sin_addr;
   3410 		ip->ip_src = sc->sc_dst.sin.sin_addr;
   3411 		ip->ip_p = IPPROTO_TCP;
   3412 		th = (struct tcphdr *)(ip + 1);
   3413 		th->th_dport = sc->sc_src.sin.sin_port;
   3414 		th->th_sport = sc->sc_dst.sin.sin_port;
   3415 		break;
   3416 #ifdef INET6
   3417 	case AF_INET6:
   3418 		ip6 = mtod(m, struct ip6_hdr *);
   3419 		ip6->ip6_dst = sc->sc_src.sin6.sin6_addr;
   3420 		ip6->ip6_src = sc->sc_dst.sin6.sin6_addr;
   3421 		ip6->ip6_nxt = IPPROTO_TCP;
   3422 		/* ip6_plen will be updated in ip6_output() */
   3423 		th = (struct tcphdr *)(ip6 + 1);
   3424 		th->th_dport = sc->sc_src.sin6.sin6_port;
   3425 		th->th_sport = sc->sc_dst.sin6.sin6_port;
   3426 		break;
   3427 #endif
   3428 	default:
   3429 		th = NULL;
   3430 	}
   3431 
   3432 	th->th_seq = htonl(sc->sc_iss);
   3433 	th->th_ack = htonl(sc->sc_irs + 1);
   3434 	th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
   3435 	th->th_flags = TH_SYN|TH_ACK;
   3436 	th->th_win = htons(sc->sc_win);
   3437 	/* th_sum already 0 */
   3438 	/* th_urp already 0 */
   3439 
   3440 	/* Tack on the TCP options. */
   3441 	optp = (u_int8_t *)(th + 1);
   3442 	*optp++ = TCPOPT_MAXSEG;
   3443 	*optp++ = 4;
   3444 	*optp++ = (sc->sc_ourmaxseg >> 8) & 0xff;
   3445 	*optp++ = sc->sc_ourmaxseg & 0xff;
   3446 
   3447 	if (sc->sc_request_r_scale != 15) {
   3448 		*((u_int32_t *)optp) = htonl(TCPOPT_NOP << 24 |
   3449 		    TCPOPT_WINDOW << 16 | TCPOLEN_WINDOW << 8 |
   3450 		    sc->sc_request_r_scale);
   3451 		optp += 4;
   3452 	}
   3453 
   3454 	if (sc->sc_flags & SCF_TIMESTAMP) {
   3455 		u_int32_t *lp = (u_int32_t *)(optp);
   3456 		/* Form timestamp option as shown in appendix A of RFC 1323. */
   3457 		*lp++ = htonl(TCPOPT_TSTAMP_HDR);
   3458 		*lp++ = htonl(tcp_now);
   3459 		*lp   = htonl(sc->sc_timestamp);
   3460 		optp += TCPOLEN_TSTAMP_APPA;
   3461 	}
   3462 
   3463 	/* Compute the packet's checksum. */
   3464 	switch (sc->sc_src.sa.sa_family) {
   3465 	case AF_INET:
   3466 		ip->ip_len = htons(tlen - hlen);
   3467 		th->th_sum = 0;
   3468 		th->th_sum = in_cksum(m, tlen);
   3469 		break;
   3470 #ifdef INET6
   3471 	case AF_INET6:
   3472 		ip6->ip6_plen = htons(tlen - hlen);
   3473 		th->th_sum = 0;
   3474 		th->th_sum = in6_cksum(m, IPPROTO_TCP, hlen, tlen - hlen);
   3475 		break;
   3476 #endif
   3477 	}
   3478 
   3479 	/*
   3480 	 * Fill in some straggling IP bits.  Note the stack expects
   3481 	 * ip_len to be in host order, for convenience.
   3482 	 */
   3483 	switch (sc->sc_src.sa.sa_family) {
   3484 	case AF_INET:
   3485 		ip->ip_len = tlen;
   3486 		ip->ip_ttl = ip_defttl;
   3487 		/* XXX tos? */
   3488 		break;
   3489 #ifdef INET6
   3490 	case AF_INET6:
   3491 		ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
   3492 		ip6->ip6_vfc |= IPV6_VERSION;
   3493 		ip6->ip6_plen = htons(tlen - hlen);
   3494 		/* ip6_hlim will be initialized afterwards */
   3495 		/* XXX flowlabel? */
   3496 		break;
   3497 #endif
   3498 	}
   3499 
   3500 	/*
   3501 	 * If we're doing Path MTU discovery, we need to set DF unless
   3502 	 * the route's MTU is locked.  If we don't yet know the route,
   3503 	 * look it up now.  We will copy this reference to the inpcb
   3504 	 * when we finish creating the connection.
   3505 	 */
   3506 	if ((rt = ro->ro_rt) == NULL || (rt->rt_flags & RTF_UP) == 0) {
   3507 		if (ro->ro_rt != NULL) {
   3508 			RTFREE(ro->ro_rt);
   3509 			ro->ro_rt = NULL;
   3510 		}
   3511 		bcopy(&sc->sc_src, &ro->ro_dst, sc->sc_src.sa.sa_len);
   3512 		rtalloc(ro);
   3513 		if ((rt = ro->ro_rt) == NULL) {
   3514 			m_freem(m);
   3515 			switch (sc->sc_src.sa.sa_family) {
   3516 			case AF_INET:
   3517 				ipstat.ips_noroute++;
   3518 				break;
   3519 #ifdef INET6
   3520 			case AF_INET6:
   3521 				ip6stat.ip6s_noroute++;
   3522 				break;
   3523 #endif
   3524 			}
   3525 			return (EHOSTUNREACH);
   3526 		}
   3527 	}
   3528 
   3529 	switch (sc->sc_src.sa.sa_family) {
   3530 	case AF_INET:
   3531 		if (ip_mtudisc != 0 && (rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
   3532 			ip->ip_off |= IP_DF;
   3533 
   3534 		/* ...and send it off! */
   3535 		error = ip_output(m, sc->sc_ipopts, ro, 0, NULL);
   3536 		break;
   3537 #ifdef INET6
   3538 	case AF_INET6:
   3539 		ip6->ip6_hlim = in6_selecthlim(NULL,
   3540 				ro->ro_rt ? ro->ro_rt->rt_ifp : NULL);
   3541 
   3542 		error = ip6_output(m, NULL /*XXX*/, (struct route_in6 *)ro,
   3543 			0, NULL, NULL);
   3544 		break;
   3545 #endif
   3546 	default:
   3547 		error = EAFNOSUPPORT;
   3548 		break;
   3549 	}
   3550 	return (error);
   3551 }
   3552