Home | History | Annotate | Line # | Download | only in netinet
tcp_input.c revision 1.264
      1 /*	$NetBSD: tcp_input.c,v 1.264 2007/05/02 20:40:25 dyoung Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  *      @(#)COPYRIGHT   1.1 (NRL) 17 January 1995
     34  *
     35  * NRL grants permission for redistribution and use in source and binary
     36  * forms, with or without modification, of the software and documentation
     37  * created at NRL provided that the following conditions are met:
     38  *
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. All advertising materials mentioning features or use of this software
     45  *    must display the following acknowledgements:
     46  *      This product includes software developed by the University of
     47  *      California, Berkeley and its contributors.
     48  *      This product includes software developed at the Information
     49  *      Technology Division, US Naval Research Laboratory.
     50  * 4. Neither the name of the NRL nor the names of its contributors
     51  *    may be used to endorse or promote products derived from this software
     52  *    without specific prior written permission.
     53  *
     54  * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
     55  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     56  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     57  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL NRL OR
     58  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     59  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     60  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     61  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     62  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     63  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     64  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     65  *
     66  * The views and conclusions contained in the software and documentation
     67  * are those of the authors and should not be interpreted as representing
     68  * official policies, either expressed or implied, of the US Naval
     69  * Research Laboratory (NRL).
     70  */
     71 
     72 /*-
     73  * Copyright (c) 1997, 1998, 1999, 2001, 2005, 2006 The NetBSD Foundation, Inc.
     74  * All rights reserved.
     75  *
     76  * This code is derived from software contributed to The NetBSD Foundation
     77  * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
     78  * Facility, NASA Ames Research Center.
     79  * This code is derived from software contributed to The NetBSD Foundation
     80  * by Charles M. Hannum.
     81  * This code is derived from software contributed to The NetBSD Foundation
     82  * by Rui Paulo.
     83  *
     84  * Redistribution and use in source and binary forms, with or without
     85  * modification, are permitted provided that the following conditions
     86  * are met:
     87  * 1. Redistributions of source code must retain the above copyright
     88  *    notice, this list of conditions and the following disclaimer.
     89  * 2. Redistributions in binary form must reproduce the above copyright
     90  *    notice, this list of conditions and the following disclaimer in the
     91  *    documentation and/or other materials provided with the distribution.
     92  * 3. All advertising materials mentioning features or use of this software
     93  *    must display the following acknowledgement:
     94  *	This product includes software developed by the NetBSD
     95  *	Foundation, Inc. and its contributors.
     96  * 4. Neither the name of The NetBSD Foundation nor the names of its
     97  *    contributors may be used to endorse or promote products derived
     98  *    from this software without specific prior written permission.
     99  *
    100  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
    101  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
    102  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    103  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
    104  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    105  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    106  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    107  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    108  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    109  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    110  * POSSIBILITY OF SUCH DAMAGE.
    111  */
    112 
    113 /*
    114  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
    115  *	The Regents of the University of California.  All rights reserved.
    116  *
    117  * Redistribution and use in source and binary forms, with or without
    118  * modification, are permitted provided that the following conditions
    119  * are met:
    120  * 1. Redistributions of source code must retain the above copyright
    121  *    notice, this list of conditions and the following disclaimer.
    122  * 2. Redistributions in binary form must reproduce the above copyright
    123  *    notice, this list of conditions and the following disclaimer in the
    124  *    documentation and/or other materials provided with the distribution.
    125  * 3. Neither the name of the University nor the names of its contributors
    126  *    may be used to endorse or promote products derived from this software
    127  *    without specific prior written permission.
    128  *
    129  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
    130  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    131  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    132  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    133  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    134  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    135  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    136  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    137  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    138  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    139  * SUCH DAMAGE.
    140  *
    141  *	@(#)tcp_input.c	8.12 (Berkeley) 5/24/95
    142  */
    143 
    144 /*
    145  *	TODO list for SYN cache stuff:
    146  *
    147  *	Find room for a "state" field, which is needed to keep a
    148  *	compressed state for TIME_WAIT TCBs.  It's been noted already
    149  *	that this is fairly important for very high-volume web and
    150  *	mail servers, which use a large number of short-lived
    151  *	connections.
    152  */
    153 
    154 #include <sys/cdefs.h>
    155 __KERNEL_RCSID(0, "$NetBSD: tcp_input.c,v 1.264 2007/05/02 20:40:25 dyoung Exp $");
    156 
    157 #include "opt_inet.h"
    158 #include "opt_ipsec.h"
    159 #include "opt_inet_csum.h"
    160 #include "opt_tcp_debug.h"
    161 
    162 #include <sys/param.h>
    163 #include <sys/systm.h>
    164 #include <sys/malloc.h>
    165 #include <sys/mbuf.h>
    166 #include <sys/protosw.h>
    167 #include <sys/socket.h>
    168 #include <sys/socketvar.h>
    169 #include <sys/errno.h>
    170 #include <sys/syslog.h>
    171 #include <sys/pool.h>
    172 #include <sys/domain.h>
    173 #include <sys/kernel.h>
    174 #ifdef TCP_SIGNATURE
    175 #include <sys/md5.h>
    176 #endif
    177 
    178 #include <net/if.h>
    179 #include <net/route.h>
    180 #include <net/if_types.h>
    181 
    182 #include <netinet/in.h>
    183 #include <netinet/in_systm.h>
    184 #include <netinet/ip.h>
    185 #include <netinet/in_pcb.h>
    186 #include <netinet/in_var.h>
    187 #include <netinet/ip_var.h>
    188 #include <netinet/in_offload.h>
    189 
    190 #ifdef INET6
    191 #ifndef INET
    192 #include <netinet/in.h>
    193 #endif
    194 #include <netinet/ip6.h>
    195 #include <netinet6/ip6_var.h>
    196 #include <netinet6/in6_pcb.h>
    197 #include <netinet6/ip6_var.h>
    198 #include <netinet6/in6_var.h>
    199 #include <netinet/icmp6.h>
    200 #include <netinet6/nd6.h>
    201 #ifdef TCP_SIGNATURE
    202 #include <netinet6/scope6_var.h>
    203 #endif
    204 #endif
    205 
    206 #ifndef INET6
    207 /* always need ip6.h for IP6_EXTHDR_GET */
    208 #include <netinet/ip6.h>
    209 #endif
    210 
    211 #include <netinet/tcp.h>
    212 #include <netinet/tcp_fsm.h>
    213 #include <netinet/tcp_seq.h>
    214 #include <netinet/tcp_timer.h>
    215 #include <netinet/tcp_var.h>
    216 #include <netinet/tcpip.h>
    217 #include <netinet/tcp_congctl.h>
    218 #include <netinet/tcp_debug.h>
    219 
    220 #include <machine/stdarg.h>
    221 
    222 #ifdef IPSEC
    223 #include <netinet6/ipsec.h>
    224 #include <netkey/key.h>
    225 #endif /*IPSEC*/
    226 #ifdef INET6
    227 #include "faith.h"
    228 #if defined(NFAITH) && NFAITH > 0
    229 #include <net/if_faith.h>
    230 #endif
    231 #endif	/* IPSEC */
    232 
    233 #ifdef FAST_IPSEC
    234 #include <netipsec/ipsec.h>
    235 #include <netipsec/ipsec_var.h>			/* XXX ipsecstat namespace */
    236 #include <netipsec/key.h>
    237 #ifdef INET6
    238 #include <netipsec/ipsec6.h>
    239 #endif
    240 #endif	/* FAST_IPSEC*/
    241 
    242 int	tcprexmtthresh = 3;
    243 int	tcp_log_refused;
    244 
    245 static int tcp_rst_ppslim_count = 0;
    246 static struct timeval tcp_rst_ppslim_last;
    247 static int tcp_ackdrop_ppslim_count = 0;
    248 static struct timeval tcp_ackdrop_ppslim_last;
    249 
    250 #define TCP_PAWS_IDLE	(24U * 24 * 60 * 60 * PR_SLOWHZ)
    251 
    252 /* for modulo comparisons of timestamps */
    253 #define TSTMP_LT(a,b)	((int)((a)-(b)) < 0)
    254 #define TSTMP_GEQ(a,b)	((int)((a)-(b)) >= 0)
    255 
    256 /*
    257  * Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint.
    258  */
    259 #ifdef INET6
    260 #define ND6_HINT(tp) \
    261 do { \
    262 	if (tp && tp->t_in6pcb && tp->t_family == AF_INET6 && \
    263 	    tp->t_in6pcb->in6p_route.ro_rt) { \
    264 		nd6_nud_hint(tp->t_in6pcb->in6p_route.ro_rt, NULL, 0); \
    265 	} \
    266 } while (/*CONSTCOND*/ 0)
    267 #else
    268 #define ND6_HINT(tp)
    269 #endif
    270 
    271 /*
    272  * Macro to compute ACK transmission behavior.  Delay the ACK unless
    273  * we have already delayed an ACK (must send an ACK every two segments).
    274  * We also ACK immediately if we received a PUSH and the ACK-on-PUSH
    275  * option is enabled.
    276  */
    277 #define	TCP_SETUP_ACK(tp, th) \
    278 do { \
    279 	if ((tp)->t_flags & TF_DELACK || \
    280 	    (tcp_ack_on_push && (th)->th_flags & TH_PUSH)) \
    281 		tp->t_flags |= TF_ACKNOW; \
    282 	else \
    283 		TCP_SET_DELACK(tp); \
    284 } while (/*CONSTCOND*/ 0)
    285 
    286 #define ICMP_CHECK(tp, th, acked) \
    287 do { \
    288 	/* \
    289 	 * If we had a pending ICMP message that \
    290 	 * refers to data that have just been  \
    291 	 * acknowledged, disregard the recorded ICMP \
    292 	 * message. \
    293 	 */ \
    294 	if (((tp)->t_flags & TF_PMTUD_PEND) && \
    295 	    SEQ_GT((th)->th_ack, (tp)->t_pmtud_th_seq)) \
    296 		(tp)->t_flags &= ~TF_PMTUD_PEND; \
    297 \
    298 	/* \
    299 	 * Keep track of the largest chunk of data \
    300 	 * acknowledged since last PMTU update \
    301 	 */ \
    302 	if ((tp)->t_pmtud_mss_acked < (acked)) \
    303 		(tp)->t_pmtud_mss_acked = (acked); \
    304 } while (/*CONSTCOND*/ 0)
    305 
    306 /*
    307  * Convert TCP protocol fields to host order for easier processing.
    308  */
    309 #define	TCP_FIELDS_TO_HOST(th)						\
    310 do {									\
    311 	NTOHL((th)->th_seq);						\
    312 	NTOHL((th)->th_ack);						\
    313 	NTOHS((th)->th_win);						\
    314 	NTOHS((th)->th_urp);						\
    315 } while (/*CONSTCOND*/ 0)
    316 
    317 /*
    318  * ... and reverse the above.
    319  */
    320 #define	TCP_FIELDS_TO_NET(th)						\
    321 do {									\
    322 	HTONL((th)->th_seq);						\
    323 	HTONL((th)->th_ack);						\
    324 	HTONS((th)->th_win);						\
    325 	HTONS((th)->th_urp);						\
    326 } while (/*CONSTCOND*/ 0)
    327 
    328 #ifdef TCP_CSUM_COUNTERS
    329 #include <sys/device.h>
    330 
    331 #if defined(INET)
    332 extern struct evcnt tcp_hwcsum_ok;
    333 extern struct evcnt tcp_hwcsum_bad;
    334 extern struct evcnt tcp_hwcsum_data;
    335 extern struct evcnt tcp_swcsum;
    336 #endif /* defined(INET) */
    337 #if defined(INET6)
    338 extern struct evcnt tcp6_hwcsum_ok;
    339 extern struct evcnt tcp6_hwcsum_bad;
    340 extern struct evcnt tcp6_hwcsum_data;
    341 extern struct evcnt tcp6_swcsum;
    342 #endif /* defined(INET6) */
    343 
    344 #define	TCP_CSUM_COUNTER_INCR(ev)	(ev)->ev_count++
    345 
    346 #else
    347 
    348 #define	TCP_CSUM_COUNTER_INCR(ev)	/* nothing */
    349 
    350 #endif /* TCP_CSUM_COUNTERS */
    351 
    352 #ifdef TCP_REASS_COUNTERS
    353 #include <sys/device.h>
    354 
    355 extern struct evcnt tcp_reass_;
    356 extern struct evcnt tcp_reass_empty;
    357 extern struct evcnt tcp_reass_iteration[8];
    358 extern struct evcnt tcp_reass_prependfirst;
    359 extern struct evcnt tcp_reass_prepend;
    360 extern struct evcnt tcp_reass_insert;
    361 extern struct evcnt tcp_reass_inserttail;
    362 extern struct evcnt tcp_reass_append;
    363 extern struct evcnt tcp_reass_appendtail;
    364 extern struct evcnt tcp_reass_overlaptail;
    365 extern struct evcnt tcp_reass_overlapfront;
    366 extern struct evcnt tcp_reass_segdup;
    367 extern struct evcnt tcp_reass_fragdup;
    368 
    369 #define	TCP_REASS_COUNTER_INCR(ev)	(ev)->ev_count++
    370 
    371 #else
    372 
    373 #define	TCP_REASS_COUNTER_INCR(ev)	/* nothing */
    374 
    375 #endif /* TCP_REASS_COUNTERS */
    376 
    377 static int tcp_reass(struct tcpcb *, const struct tcphdr *, struct mbuf *,
    378     int *);
    379 static int tcp_dooptions(struct tcpcb *, const u_char *, int,
    380     const struct tcphdr *, struct mbuf *, int, struct tcp_opt_info *);
    381 
    382 #ifdef INET
    383 static void tcp4_log_refused(const struct ip *, const struct tcphdr *);
    384 #endif
    385 #ifdef INET6
    386 static void tcp6_log_refused(const struct ip6_hdr *, const struct tcphdr *);
    387 #endif
    388 
    389 #define	TRAVERSE(x) while ((x)->m_next) (x) = (x)->m_next
    390 
    391 #if defined(MBUFTRACE)
    392 struct mowner tcp_reass_mowner = MOWNER_INIT("tcp", "reass");
    393 #endif /* defined(MBUFTRACE) */
    394 
    395 static POOL_INIT(tcpipqent_pool, sizeof(struct ipqent), 0, 0, 0, "tcpipqepl",
    396     NULL, IPL_VM);
    397 
    398 struct ipqent *
    399 tcpipqent_alloc()
    400 {
    401 	struct ipqent *ipqe;
    402 	int s;
    403 
    404 	s = splvm();
    405 	ipqe = pool_get(&tcpipqent_pool, PR_NOWAIT);
    406 	splx(s);
    407 
    408 	return ipqe;
    409 }
    410 
    411 void
    412 tcpipqent_free(struct ipqent *ipqe)
    413 {
    414 	int s;
    415 
    416 	s = splvm();
    417 	pool_put(&tcpipqent_pool, ipqe);
    418 	splx(s);
    419 }
    420 
    421 static int
    422 tcp_reass(struct tcpcb *tp, const struct tcphdr *th, struct mbuf *m, int *tlen)
    423 {
    424 	struct ipqent *p, *q, *nq, *tiqe = NULL;
    425 	struct socket *so = NULL;
    426 	int pkt_flags;
    427 	tcp_seq pkt_seq;
    428 	unsigned pkt_len;
    429 	u_long rcvpartdupbyte = 0;
    430 	u_long rcvoobyte;
    431 #ifdef TCP_REASS_COUNTERS
    432 	u_int count = 0;
    433 #endif
    434 
    435 	if (tp->t_inpcb)
    436 		so = tp->t_inpcb->inp_socket;
    437 #ifdef INET6
    438 	else if (tp->t_in6pcb)
    439 		so = tp->t_in6pcb->in6p_socket;
    440 #endif
    441 
    442 	TCP_REASS_LOCK_CHECK(tp);
    443 
    444 	/*
    445 	 * Call with th==0 after become established to
    446 	 * force pre-ESTABLISHED data up to user socket.
    447 	 */
    448 	if (th == 0)
    449 		goto present;
    450 
    451 	m_claimm(m, &tcp_reass_mowner);
    452 
    453 	rcvoobyte = *tlen;
    454 	/*
    455 	 * Copy these to local variables because the tcpiphdr
    456 	 * gets munged while we are collapsing mbufs.
    457 	 */
    458 	pkt_seq = th->th_seq;
    459 	pkt_len = *tlen;
    460 	pkt_flags = th->th_flags;
    461 
    462 	TCP_REASS_COUNTER_INCR(&tcp_reass_);
    463 
    464 	if ((p = TAILQ_LAST(&tp->segq, ipqehead)) != NULL) {
    465 		/*
    466 		 * When we miss a packet, the vast majority of time we get
    467 		 * packets that follow it in order.  So optimize for that.
    468 		 */
    469 		if (pkt_seq == p->ipqe_seq + p->ipqe_len) {
    470 			p->ipqe_len += pkt_len;
    471 			p->ipqe_flags |= pkt_flags;
    472 			m_cat(p->ipre_mlast, m);
    473 			TRAVERSE(p->ipre_mlast);
    474 			m = NULL;
    475 			tiqe = p;
    476 			TAILQ_REMOVE(&tp->timeq, p, ipqe_timeq);
    477 			TCP_REASS_COUNTER_INCR(&tcp_reass_appendtail);
    478 			goto skip_replacement;
    479 		}
    480 		/*
    481 		 * While we're here, if the pkt is completely beyond
    482 		 * anything we have, just insert it at the tail.
    483 		 */
    484 		if (SEQ_GT(pkt_seq, p->ipqe_seq + p->ipqe_len)) {
    485 			TCP_REASS_COUNTER_INCR(&tcp_reass_inserttail);
    486 			goto insert_it;
    487 		}
    488 	}
    489 
    490 	q = TAILQ_FIRST(&tp->segq);
    491 
    492 	if (q != NULL) {
    493 		/*
    494 		 * If this segment immediately precedes the first out-of-order
    495 		 * block, simply slap the segment in front of it and (mostly)
    496 		 * skip the complicated logic.
    497 		 */
    498 		if (pkt_seq + pkt_len == q->ipqe_seq) {
    499 			q->ipqe_seq = pkt_seq;
    500 			q->ipqe_len += pkt_len;
    501 			q->ipqe_flags |= pkt_flags;
    502 			m_cat(m, q->ipqe_m);
    503 			q->ipqe_m = m;
    504 			q->ipre_mlast = m; /* last mbuf may have changed */
    505 			TRAVERSE(q->ipre_mlast);
    506 			tiqe = q;
    507 			TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq);
    508 			TCP_REASS_COUNTER_INCR(&tcp_reass_prependfirst);
    509 			goto skip_replacement;
    510 		}
    511 	} else {
    512 		TCP_REASS_COUNTER_INCR(&tcp_reass_empty);
    513 	}
    514 
    515 	/*
    516 	 * Find a segment which begins after this one does.
    517 	 */
    518 	for (p = NULL; q != NULL; q = nq) {
    519 		nq = TAILQ_NEXT(q, ipqe_q);
    520 #ifdef TCP_REASS_COUNTERS
    521 		count++;
    522 #endif
    523 		/*
    524 		 * If the received segment is just right after this
    525 		 * fragment, merge the two together and then check
    526 		 * for further overlaps.
    527 		 */
    528 		if (q->ipqe_seq + q->ipqe_len == pkt_seq) {
    529 #ifdef TCPREASS_DEBUG
    530 			printf("tcp_reass[%p]: concat %u:%u(%u) to %u:%u(%u)\n",
    531 			       tp, pkt_seq, pkt_seq + pkt_len, pkt_len,
    532 			       q->ipqe_seq, q->ipqe_seq + q->ipqe_len, q->ipqe_len);
    533 #endif
    534 			pkt_len += q->ipqe_len;
    535 			pkt_flags |= q->ipqe_flags;
    536 			pkt_seq = q->ipqe_seq;
    537 			m_cat(q->ipre_mlast, m);
    538 			TRAVERSE(q->ipre_mlast);
    539 			m = q->ipqe_m;
    540 			TCP_REASS_COUNTER_INCR(&tcp_reass_append);
    541 			goto free_ipqe;
    542 		}
    543 		/*
    544 		 * If the received segment is completely past this
    545 		 * fragment, we need to go the next fragment.
    546 		 */
    547 		if (SEQ_LT(q->ipqe_seq + q->ipqe_len, pkt_seq)) {
    548 			p = q;
    549 			continue;
    550 		}
    551 		/*
    552 		 * If the fragment is past the received segment,
    553 		 * it (or any following) can't be concatenated.
    554 		 */
    555 		if (SEQ_GT(q->ipqe_seq, pkt_seq + pkt_len)) {
    556 			TCP_REASS_COUNTER_INCR(&tcp_reass_insert);
    557 			break;
    558 		}
    559 
    560 		/*
    561 		 * We've received all the data in this segment before.
    562 		 * mark it as a duplicate and return.
    563 		 */
    564 		if (SEQ_LEQ(q->ipqe_seq, pkt_seq) &&
    565 		    SEQ_GEQ(q->ipqe_seq + q->ipqe_len, pkt_seq + pkt_len)) {
    566 			tcpstat.tcps_rcvduppack++;
    567 			tcpstat.tcps_rcvdupbyte += pkt_len;
    568 			tcp_new_dsack(tp, pkt_seq, pkt_len);
    569 			m_freem(m);
    570 			if (tiqe != NULL) {
    571 				tcpipqent_free(tiqe);
    572 			}
    573 			TCP_REASS_COUNTER_INCR(&tcp_reass_segdup);
    574 			return (0);
    575 		}
    576 		/*
    577 		 * Received segment completely overlaps this fragment
    578 		 * so we drop the fragment (this keeps the temporal
    579 		 * ordering of segments correct).
    580 		 */
    581 		if (SEQ_GEQ(q->ipqe_seq, pkt_seq) &&
    582 		    SEQ_LEQ(q->ipqe_seq + q->ipqe_len, pkt_seq + pkt_len)) {
    583 			rcvpartdupbyte += q->ipqe_len;
    584 			m_freem(q->ipqe_m);
    585 			TCP_REASS_COUNTER_INCR(&tcp_reass_fragdup);
    586 			goto free_ipqe;
    587 		}
    588 		/*
    589 		 * RX'ed segment extends past the end of the
    590 		 * fragment.  Drop the overlapping bytes.  Then
    591 		 * merge the fragment and segment then treat as
    592 		 * a longer received packet.
    593 		 */
    594 		if (SEQ_LT(q->ipqe_seq, pkt_seq) &&
    595 		    SEQ_GT(q->ipqe_seq + q->ipqe_len, pkt_seq))  {
    596 			int overlap = q->ipqe_seq + q->ipqe_len - pkt_seq;
    597 #ifdef TCPREASS_DEBUG
    598 			printf("tcp_reass[%p]: trim starting %d bytes of %u:%u(%u)\n",
    599 			       tp, overlap,
    600 			       pkt_seq, pkt_seq + pkt_len, pkt_len);
    601 #endif
    602 			m_adj(m, overlap);
    603 			rcvpartdupbyte += overlap;
    604 			m_cat(q->ipre_mlast, m);
    605 			TRAVERSE(q->ipre_mlast);
    606 			m = q->ipqe_m;
    607 			pkt_seq = q->ipqe_seq;
    608 			pkt_len += q->ipqe_len - overlap;
    609 			rcvoobyte -= overlap;
    610 			TCP_REASS_COUNTER_INCR(&tcp_reass_overlaptail);
    611 			goto free_ipqe;
    612 		}
    613 		/*
    614 		 * RX'ed segment extends past the front of the
    615 		 * fragment.  Drop the overlapping bytes on the
    616 		 * received packet.  The packet will then be
    617 		 * contatentated with this fragment a bit later.
    618 		 */
    619 		if (SEQ_GT(q->ipqe_seq, pkt_seq) &&
    620 		    SEQ_LT(q->ipqe_seq, pkt_seq + pkt_len))  {
    621 			int overlap = pkt_seq + pkt_len - q->ipqe_seq;
    622 #ifdef TCPREASS_DEBUG
    623 			printf("tcp_reass[%p]: trim trailing %d bytes of %u:%u(%u)\n",
    624 			       tp, overlap,
    625 			       pkt_seq, pkt_seq + pkt_len, pkt_len);
    626 #endif
    627 			m_adj(m, -overlap);
    628 			pkt_len -= overlap;
    629 			rcvpartdupbyte += overlap;
    630 			TCP_REASS_COUNTER_INCR(&tcp_reass_overlapfront);
    631 			rcvoobyte -= overlap;
    632 		}
    633 		/*
    634 		 * If the received segment immediates precedes this
    635 		 * fragment then tack the fragment onto this segment
    636 		 * and reinsert the data.
    637 		 */
    638 		if (q->ipqe_seq == pkt_seq + pkt_len) {
    639 #ifdef TCPREASS_DEBUG
    640 			printf("tcp_reass[%p]: append %u:%u(%u) to %u:%u(%u)\n",
    641 			       tp, q->ipqe_seq, q->ipqe_seq + q->ipqe_len, q->ipqe_len,
    642 			       pkt_seq, pkt_seq + pkt_len, pkt_len);
    643 #endif
    644 			pkt_len += q->ipqe_len;
    645 			pkt_flags |= q->ipqe_flags;
    646 			m_cat(m, q->ipqe_m);
    647 			TAILQ_REMOVE(&tp->segq, q, ipqe_q);
    648 			TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq);
    649 			tp->t_segqlen--;
    650 			KASSERT(tp->t_segqlen >= 0);
    651 			KASSERT(tp->t_segqlen != 0 ||
    652 			    (TAILQ_EMPTY(&tp->segq) &&
    653 			    TAILQ_EMPTY(&tp->timeq)));
    654 			if (tiqe == NULL) {
    655 				tiqe = q;
    656 			} else {
    657 				tcpipqent_free(q);
    658 			}
    659 			TCP_REASS_COUNTER_INCR(&tcp_reass_prepend);
    660 			break;
    661 		}
    662 		/*
    663 		 * If the fragment is before the segment, remember it.
    664 		 * When this loop is terminated, p will contain the
    665 		 * pointer to fragment that is right before the received
    666 		 * segment.
    667 		 */
    668 		if (SEQ_LEQ(q->ipqe_seq, pkt_seq))
    669 			p = q;
    670 
    671 		continue;
    672 
    673 		/*
    674 		 * This is a common operation.  It also will allow
    675 		 * to save doing a malloc/free in most instances.
    676 		 */
    677 	  free_ipqe:
    678 		TAILQ_REMOVE(&tp->segq, q, ipqe_q);
    679 		TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq);
    680 		tp->t_segqlen--;
    681 		KASSERT(tp->t_segqlen >= 0);
    682 		KASSERT(tp->t_segqlen != 0 ||
    683 		    (TAILQ_EMPTY(&tp->segq) && TAILQ_EMPTY(&tp->timeq)));
    684 		if (tiqe == NULL) {
    685 			tiqe = q;
    686 		} else {
    687 			tcpipqent_free(q);
    688 		}
    689 	}
    690 
    691 #ifdef TCP_REASS_COUNTERS
    692 	if (count > 7)
    693 		TCP_REASS_COUNTER_INCR(&tcp_reass_iteration[0]);
    694 	else if (count > 0)
    695 		TCP_REASS_COUNTER_INCR(&tcp_reass_iteration[count]);
    696 #endif
    697 
    698     insert_it:
    699 
    700 	/*
    701 	 * Allocate a new queue entry since the received segment did not
    702 	 * collapse onto any other out-of-order block; thus we are allocating
    703 	 * a new block.  If it had collapsed, tiqe would not be NULL and
    704 	 * we would be reusing it.
    705 	 * XXX If we can't, just drop the packet.  XXX
    706 	 */
    707 	if (tiqe == NULL) {
    708 		tiqe = tcpipqent_alloc();
    709 		if (tiqe == NULL) {
    710 			tcpstat.tcps_rcvmemdrop++;
    711 			m_freem(m);
    712 			return (0);
    713 		}
    714 	}
    715 
    716 	/*
    717 	 * Update the counters.
    718 	 */
    719 	tcpstat.tcps_rcvoopack++;
    720 	tcpstat.tcps_rcvoobyte += rcvoobyte;
    721 	if (rcvpartdupbyte) {
    722 	    tcpstat.tcps_rcvpartduppack++;
    723 	    tcpstat.tcps_rcvpartdupbyte += rcvpartdupbyte;
    724 	}
    725 
    726 	/*
    727 	 * Insert the new fragment queue entry into both queues.
    728 	 */
    729 	tiqe->ipqe_m = m;
    730 	tiqe->ipre_mlast = m;
    731 	tiqe->ipqe_seq = pkt_seq;
    732 	tiqe->ipqe_len = pkt_len;
    733 	tiqe->ipqe_flags = pkt_flags;
    734 	if (p == NULL) {
    735 		TAILQ_INSERT_HEAD(&tp->segq, tiqe, ipqe_q);
    736 #ifdef TCPREASS_DEBUG
    737 		if (tiqe->ipqe_seq != tp->rcv_nxt)
    738 			printf("tcp_reass[%p]: insert %u:%u(%u) at front\n",
    739 			       tp, pkt_seq, pkt_seq + pkt_len, pkt_len);
    740 #endif
    741 	} else {
    742 		TAILQ_INSERT_AFTER(&tp->segq, p, tiqe, ipqe_q);
    743 #ifdef TCPREASS_DEBUG
    744 		printf("tcp_reass[%p]: insert %u:%u(%u) after %u:%u(%u)\n",
    745 		       tp, pkt_seq, pkt_seq + pkt_len, pkt_len,
    746 		       p->ipqe_seq, p->ipqe_seq + p->ipqe_len, p->ipqe_len);
    747 #endif
    748 	}
    749 	tp->t_segqlen++;
    750 
    751 skip_replacement:
    752 
    753 	TAILQ_INSERT_HEAD(&tp->timeq, tiqe, ipqe_timeq);
    754 
    755 present:
    756 	/*
    757 	 * Present data to user, advancing rcv_nxt through
    758 	 * completed sequence space.
    759 	 */
    760 	if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
    761 		return (0);
    762 	q = TAILQ_FIRST(&tp->segq);
    763 	if (q == NULL || q->ipqe_seq != tp->rcv_nxt)
    764 		return (0);
    765 	if (tp->t_state == TCPS_SYN_RECEIVED && q->ipqe_len)
    766 		return (0);
    767 
    768 	tp->rcv_nxt += q->ipqe_len;
    769 	pkt_flags = q->ipqe_flags & TH_FIN;
    770 	ND6_HINT(tp);
    771 
    772 	TAILQ_REMOVE(&tp->segq, q, ipqe_q);
    773 	TAILQ_REMOVE(&tp->timeq, q, ipqe_timeq);
    774 	tp->t_segqlen--;
    775 	KASSERT(tp->t_segqlen >= 0);
    776 	KASSERT(tp->t_segqlen != 0 ||
    777 	    (TAILQ_EMPTY(&tp->segq) && TAILQ_EMPTY(&tp->timeq)));
    778 	if (so->so_state & SS_CANTRCVMORE)
    779 		m_freem(q->ipqe_m);
    780 	else
    781 		sbappendstream(&so->so_rcv, q->ipqe_m);
    782 	tcpipqent_free(q);
    783 	sorwakeup(so);
    784 	return (pkt_flags);
    785 }
    786 
    787 #ifdef INET6
    788 int
    789 tcp6_input(struct mbuf **mp, int *offp, int proto)
    790 {
    791 	struct mbuf *m = *mp;
    792 
    793 	/*
    794 	 * draft-itojun-ipv6-tcp-to-anycast
    795 	 * better place to put this in?
    796 	 */
    797 	if (m->m_flags & M_ANYCAST6) {
    798 		struct ip6_hdr *ip6;
    799 		if (m->m_len < sizeof(struct ip6_hdr)) {
    800 			if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
    801 				tcpstat.tcps_rcvshort++;
    802 				return IPPROTO_DONE;
    803 			}
    804 		}
    805 		ip6 = mtod(m, struct ip6_hdr *);
    806 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
    807 		    (char *)&ip6->ip6_dst - (char *)ip6);
    808 		return IPPROTO_DONE;
    809 	}
    810 
    811 	tcp_input(m, *offp, proto);
    812 	return IPPROTO_DONE;
    813 }
    814 #endif
    815 
    816 #ifdef INET
    817 static void
    818 tcp4_log_refused(const struct ip *ip, const struct tcphdr *th)
    819 {
    820 	char src[4*sizeof "123"];
    821 	char dst[4*sizeof "123"];
    822 
    823 	if (ip) {
    824 		strlcpy(src, inet_ntoa(ip->ip_src), sizeof(src));
    825 		strlcpy(dst, inet_ntoa(ip->ip_dst), sizeof(dst));
    826 	}
    827 	else {
    828 		strlcpy(src, "(unknown)", sizeof(src));
    829 		strlcpy(dst, "(unknown)", sizeof(dst));
    830 	}
    831 	log(LOG_INFO,
    832 	    "Connection attempt to TCP %s:%d from %s:%d\n",
    833 	    dst, ntohs(th->th_dport),
    834 	    src, ntohs(th->th_sport));
    835 }
    836 #endif
    837 
    838 #ifdef INET6
    839 static void
    840 tcp6_log_refused(const struct ip6_hdr *ip6, const struct tcphdr *th)
    841 {
    842 	char src[INET6_ADDRSTRLEN];
    843 	char dst[INET6_ADDRSTRLEN];
    844 
    845 	if (ip6) {
    846 		strlcpy(src, ip6_sprintf(&ip6->ip6_src), sizeof(src));
    847 		strlcpy(dst, ip6_sprintf(&ip6->ip6_dst), sizeof(dst));
    848 	}
    849 	else {
    850 		strlcpy(src, "(unknown v6)", sizeof(src));
    851 		strlcpy(dst, "(unknown v6)", sizeof(dst));
    852 	}
    853 	log(LOG_INFO,
    854 	    "Connection attempt to TCP [%s]:%d from [%s]:%d\n",
    855 	    dst, ntohs(th->th_dport),
    856 	    src, ntohs(th->th_sport));
    857 }
    858 #endif
    859 
    860 /*
    861  * Checksum extended TCP header and data.
    862  */
    863 int
    864 tcp_input_checksum(int af, struct mbuf *m, const struct tcphdr *th,
    865     int toff, int off, int tlen)
    866 {
    867 
    868 	/*
    869 	 * XXX it's better to record and check if this mbuf is
    870 	 * already checked.
    871 	 */
    872 
    873 	switch (af) {
    874 #ifdef INET
    875 	case AF_INET:
    876 		switch (m->m_pkthdr.csum_flags &
    877 			((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_TCPv4) |
    878 			 M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
    879 		case M_CSUM_TCPv4|M_CSUM_TCP_UDP_BAD:
    880 			TCP_CSUM_COUNTER_INCR(&tcp_hwcsum_bad);
    881 			goto badcsum;
    882 
    883 		case M_CSUM_TCPv4|M_CSUM_DATA: {
    884 			u_int32_t hw_csum = m->m_pkthdr.csum_data;
    885 
    886 			TCP_CSUM_COUNTER_INCR(&tcp_hwcsum_data);
    887 			if (m->m_pkthdr.csum_flags & M_CSUM_NO_PSEUDOHDR) {
    888 				const struct ip *ip =
    889 				    mtod(m, const struct ip *);
    890 
    891 				hw_csum = in_cksum_phdr(ip->ip_src.s_addr,
    892 				    ip->ip_dst.s_addr,
    893 				    htons(hw_csum + tlen + off + IPPROTO_TCP));
    894 			}
    895 			if ((hw_csum ^ 0xffff) != 0)
    896 				goto badcsum;
    897 			break;
    898 		}
    899 
    900 		case M_CSUM_TCPv4:
    901 			/* Checksum was okay. */
    902 			TCP_CSUM_COUNTER_INCR(&tcp_hwcsum_ok);
    903 			break;
    904 
    905 		default:
    906 			/*
    907 			 * Must compute it ourselves.  Maybe skip checksum
    908 			 * on loopback interfaces.
    909 			 */
    910 			if (__predict_true(!(m->m_pkthdr.rcvif->if_flags &
    911 					     IFF_LOOPBACK) ||
    912 					   tcp_do_loopback_cksum)) {
    913 				TCP_CSUM_COUNTER_INCR(&tcp_swcsum);
    914 				if (in4_cksum(m, IPPROTO_TCP, toff,
    915 					      tlen + off) != 0)
    916 					goto badcsum;
    917 			}
    918 			break;
    919 		}
    920 		break;
    921 #endif /* INET4 */
    922 
    923 #ifdef INET6
    924 	case AF_INET6:
    925 		switch (m->m_pkthdr.csum_flags &
    926 			((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_TCPv6) |
    927 			 M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
    928 		case M_CSUM_TCPv6|M_CSUM_TCP_UDP_BAD:
    929 			TCP_CSUM_COUNTER_INCR(&tcp6_hwcsum_bad);
    930 			goto badcsum;
    931 
    932 #if 0 /* notyet */
    933 		case M_CSUM_TCPv6|M_CSUM_DATA:
    934 #endif
    935 
    936 		case M_CSUM_TCPv6:
    937 			/* Checksum was okay. */
    938 			TCP_CSUM_COUNTER_INCR(&tcp6_hwcsum_ok);
    939 			break;
    940 
    941 		default:
    942 			/*
    943 			 * Must compute it ourselves.  Maybe skip checksum
    944 			 * on loopback interfaces.
    945 			 */
    946 			if (__predict_true((m->m_flags & M_LOOP) == 0 ||
    947 			    tcp_do_loopback_cksum)) {
    948 				TCP_CSUM_COUNTER_INCR(&tcp6_swcsum);
    949 				if (in6_cksum(m, IPPROTO_TCP, toff,
    950 				    tlen + off) != 0)
    951 					goto badcsum;
    952 			}
    953 		}
    954 		break;
    955 #endif /* INET6 */
    956 	}
    957 
    958 	return 0;
    959 
    960 badcsum:
    961 	tcpstat.tcps_rcvbadsum++;
    962 	return -1;
    963 }
    964 
    965 /*
    966  * TCP input routine, follows pages 65-76 of RFC 793 very closely.
    967  */
    968 void
    969 tcp_input(struct mbuf *m, ...)
    970 {
    971 	struct tcphdr *th;
    972 	struct ip *ip;
    973 	struct inpcb *inp;
    974 #ifdef INET6
    975 	struct ip6_hdr *ip6;
    976 	struct in6pcb *in6p;
    977 #endif
    978 	u_int8_t *optp = NULL;
    979 	int optlen = 0;
    980 	int len, tlen, toff, hdroptlen = 0;
    981 	struct tcpcb *tp = 0;
    982 	int tiflags;
    983 	struct socket *so = NULL;
    984 	int todrop, dupseg, acked, ourfinisacked, needoutput = 0;
    985 #ifdef TCP_DEBUG
    986 	short ostate = 0;
    987 #endif
    988 	u_long tiwin;
    989 	struct tcp_opt_info opti;
    990 	int off, iphlen;
    991 	va_list ap;
    992 	int af;		/* af on the wire */
    993 	struct mbuf *tcp_saveti = NULL;
    994 	uint32_t ts_rtt;
    995 	uint8_t iptos;
    996 
    997 	MCLAIM(m, &tcp_rx_mowner);
    998 	va_start(ap, m);
    999 	toff = va_arg(ap, int);
   1000 	(void)va_arg(ap, int);		/* ignore value, advance ap */
   1001 	va_end(ap);
   1002 
   1003 	tcpstat.tcps_rcvtotal++;
   1004 
   1005 	bzero(&opti, sizeof(opti));
   1006 	opti.ts_present = 0;
   1007 	opti.maxseg = 0;
   1008 
   1009 	/*
   1010 	 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN.
   1011 	 *
   1012 	 * TCP is, by definition, unicast, so we reject all
   1013 	 * multicast outright.
   1014 	 *
   1015 	 * Note, there are additional src/dst address checks in
   1016 	 * the AF-specific code below.
   1017 	 */
   1018 	if (m->m_flags & (M_BCAST|M_MCAST)) {
   1019 		/* XXX stat */
   1020 		goto drop;
   1021 	}
   1022 #ifdef INET6
   1023 	if (m->m_flags & M_ANYCAST6) {
   1024 		/* XXX stat */
   1025 		goto drop;
   1026 	}
   1027 #endif
   1028 
   1029 	/*
   1030 	 * Get IP and TCP header.
   1031 	 * Note: IP leaves IP header in first mbuf.
   1032 	 */
   1033 	ip = mtod(m, struct ip *);
   1034 #ifdef INET6
   1035 	ip6 = NULL;
   1036 #endif
   1037 	switch (ip->ip_v) {
   1038 #ifdef INET
   1039 	case 4:
   1040 		af = AF_INET;
   1041 		iphlen = sizeof(struct ip);
   1042 		ip = mtod(m, struct ip *);
   1043 		IP6_EXTHDR_GET(th, struct tcphdr *, m, toff,
   1044 			sizeof(struct tcphdr));
   1045 		if (th == NULL) {
   1046 			tcpstat.tcps_rcvshort++;
   1047 			return;
   1048 		}
   1049 		/* We do the checksum after PCB lookup... */
   1050 		len = ntohs(ip->ip_len);
   1051 		tlen = len - toff;
   1052 		iptos = ip->ip_tos;
   1053 		break;
   1054 #endif
   1055 #ifdef INET6
   1056 	case 6:
   1057 		ip = NULL;
   1058 		iphlen = sizeof(struct ip6_hdr);
   1059 		af = AF_INET6;
   1060 		ip6 = mtod(m, struct ip6_hdr *);
   1061 		IP6_EXTHDR_GET(th, struct tcphdr *, m, toff,
   1062 			sizeof(struct tcphdr));
   1063 		if (th == NULL) {
   1064 			tcpstat.tcps_rcvshort++;
   1065 			return;
   1066 		}
   1067 
   1068 		/* Be proactive about malicious use of IPv4 mapped address */
   1069 		if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
   1070 		    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
   1071 			/* XXX stat */
   1072 			goto drop;
   1073 		}
   1074 
   1075 		/*
   1076 		 * Be proactive about unspecified IPv6 address in source.
   1077 		 * As we use all-zero to indicate unbounded/unconnected pcb,
   1078 		 * unspecified IPv6 address can be used to confuse us.
   1079 		 *
   1080 		 * Note that packets with unspecified IPv6 destination is
   1081 		 * already dropped in ip6_input.
   1082 		 */
   1083 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
   1084 			/* XXX stat */
   1085 			goto drop;
   1086 		}
   1087 
   1088 		/*
   1089 		 * Make sure destination address is not multicast.
   1090 		 * Source address checked in ip6_input().
   1091 		 */
   1092 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
   1093 			/* XXX stat */
   1094 			goto drop;
   1095 		}
   1096 
   1097 		/* We do the checksum after PCB lookup... */
   1098 		len = m->m_pkthdr.len;
   1099 		tlen = len - toff;
   1100 		iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
   1101 		break;
   1102 #endif
   1103 	default:
   1104 		m_freem(m);
   1105 		return;
   1106 	}
   1107 
   1108 	KASSERT(TCP_HDR_ALIGNED_P(th));
   1109 
   1110 	/*
   1111 	 * Check that TCP offset makes sense,
   1112 	 * pull out TCP options and adjust length.		XXX
   1113 	 */
   1114 	off = th->th_off << 2;
   1115 	if (off < sizeof (struct tcphdr) || off > tlen) {
   1116 		tcpstat.tcps_rcvbadoff++;
   1117 		goto drop;
   1118 	}
   1119 	tlen -= off;
   1120 
   1121 	/*
   1122 	 * tcp_input() has been modified to use tlen to mean the TCP data
   1123 	 * length throughout the function.  Other functions can use
   1124 	 * m->m_pkthdr.len as the basis for calculating the TCP data length.
   1125 	 * rja
   1126 	 */
   1127 
   1128 	if (off > sizeof (struct tcphdr)) {
   1129 		IP6_EXTHDR_GET(th, struct tcphdr *, m, toff, off);
   1130 		if (th == NULL) {
   1131 			tcpstat.tcps_rcvshort++;
   1132 			return;
   1133 		}
   1134 		/*
   1135 		 * NOTE: ip/ip6 will not be affected by m_pulldown()
   1136 		 * (as they're before toff) and we don't need to update those.
   1137 		 */
   1138 		KASSERT(TCP_HDR_ALIGNED_P(th));
   1139 		optlen = off - sizeof (struct tcphdr);
   1140 		optp = ((u_int8_t *)th) + sizeof(struct tcphdr);
   1141 		/*
   1142 		 * Do quick retrieval of timestamp options ("options
   1143 		 * prediction?").  If timestamp is the only option and it's
   1144 		 * formatted as recommended in RFC 1323 appendix A, we
   1145 		 * quickly get the values now and not bother calling
   1146 		 * tcp_dooptions(), etc.
   1147 		 */
   1148 		if ((optlen == TCPOLEN_TSTAMP_APPA ||
   1149 		     (optlen > TCPOLEN_TSTAMP_APPA &&
   1150 			optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) &&
   1151 		     *(u_int32_t *)optp == htonl(TCPOPT_TSTAMP_HDR) &&
   1152 		     (th->th_flags & TH_SYN) == 0) {
   1153 			opti.ts_present = 1;
   1154 			opti.ts_val = ntohl(*(u_int32_t *)(optp + 4));
   1155 			opti.ts_ecr = ntohl(*(u_int32_t *)(optp + 8));
   1156 			optp = NULL;	/* we've parsed the options */
   1157 		}
   1158 	}
   1159 	tiflags = th->th_flags;
   1160 
   1161 	/*
   1162 	 * Locate pcb for segment.
   1163 	 */
   1164 findpcb:
   1165 	inp = NULL;
   1166 #ifdef INET6
   1167 	in6p = NULL;
   1168 #endif
   1169 	switch (af) {
   1170 #ifdef INET
   1171 	case AF_INET:
   1172 		inp = in_pcblookup_connect(&tcbtable, ip->ip_src, th->th_sport,
   1173 		    ip->ip_dst, th->th_dport);
   1174 		if (inp == 0) {
   1175 			++tcpstat.tcps_pcbhashmiss;
   1176 			inp = in_pcblookup_bind(&tcbtable, ip->ip_dst, th->th_dport);
   1177 		}
   1178 #ifdef INET6
   1179 		if (inp == 0) {
   1180 			struct in6_addr s, d;
   1181 
   1182 			/* mapped addr case */
   1183 			bzero(&s, sizeof(s));
   1184 			s.s6_addr16[5] = htons(0xffff);
   1185 			bcopy(&ip->ip_src, &s.s6_addr32[3], sizeof(ip->ip_src));
   1186 			bzero(&d, sizeof(d));
   1187 			d.s6_addr16[5] = htons(0xffff);
   1188 			bcopy(&ip->ip_dst, &d.s6_addr32[3], sizeof(ip->ip_dst));
   1189 			in6p = in6_pcblookup_connect(&tcbtable, &s,
   1190 			    th->th_sport, &d, th->th_dport, 0);
   1191 			if (in6p == 0) {
   1192 				++tcpstat.tcps_pcbhashmiss;
   1193 				in6p = in6_pcblookup_bind(&tcbtable, &d,
   1194 				    th->th_dport, 0);
   1195 			}
   1196 		}
   1197 #endif
   1198 #ifndef INET6
   1199 		if (inp == 0)
   1200 #else
   1201 		if (inp == 0 && in6p == 0)
   1202 #endif
   1203 		{
   1204 			++tcpstat.tcps_noport;
   1205 			if (tcp_log_refused &&
   1206 			    (tiflags & (TH_RST|TH_ACK|TH_SYN)) == TH_SYN) {
   1207 				tcp4_log_refused(ip, th);
   1208 			}
   1209 			TCP_FIELDS_TO_HOST(th);
   1210 			goto dropwithreset_ratelim;
   1211 		}
   1212 #if defined(IPSEC) || defined(FAST_IPSEC)
   1213 		if (inp && (inp->inp_socket->so_options & SO_ACCEPTCONN) == 0 &&
   1214 		    ipsec4_in_reject(m, inp)) {
   1215 			ipsecstat.in_polvio++;
   1216 			goto drop;
   1217 		}
   1218 #ifdef INET6
   1219 		else if (in6p &&
   1220 		    (in6p->in6p_socket->so_options & SO_ACCEPTCONN) == 0 &&
   1221 		    ipsec6_in_reject_so(m, in6p->in6p_socket)) {
   1222 			ipsecstat.in_polvio++;
   1223 			goto drop;
   1224 		}
   1225 #endif
   1226 #endif /*IPSEC*/
   1227 		break;
   1228 #endif /*INET*/
   1229 #ifdef INET6
   1230 	case AF_INET6:
   1231 	    {
   1232 		int faith;
   1233 
   1234 #if defined(NFAITH) && NFAITH > 0
   1235 		faith = faithprefix(&ip6->ip6_dst);
   1236 #else
   1237 		faith = 0;
   1238 #endif
   1239 		in6p = in6_pcblookup_connect(&tcbtable, &ip6->ip6_src,
   1240 		    th->th_sport, &ip6->ip6_dst, th->th_dport, faith);
   1241 		if (in6p == NULL) {
   1242 			++tcpstat.tcps_pcbhashmiss;
   1243 			in6p = in6_pcblookup_bind(&tcbtable, &ip6->ip6_dst,
   1244 				th->th_dport, faith);
   1245 		}
   1246 		if (in6p == NULL) {
   1247 			++tcpstat.tcps_noport;
   1248 			if (tcp_log_refused &&
   1249 			    (tiflags & (TH_RST|TH_ACK|TH_SYN)) == TH_SYN) {
   1250 				tcp6_log_refused(ip6, th);
   1251 			}
   1252 			TCP_FIELDS_TO_HOST(th);
   1253 			goto dropwithreset_ratelim;
   1254 		}
   1255 #if defined(IPSEC) || defined(FAST_IPSEC)
   1256 		if ((in6p->in6p_socket->so_options & SO_ACCEPTCONN) == 0 &&
   1257 		    ipsec6_in_reject(m, in6p)) {
   1258 			ipsec6stat.in_polvio++;
   1259 			goto drop;
   1260 		}
   1261 #endif /*IPSEC*/
   1262 		break;
   1263 	    }
   1264 #endif
   1265 	}
   1266 
   1267 	/*
   1268 	 * If the state is CLOSED (i.e., TCB does not exist) then
   1269 	 * all data in the incoming segment is discarded.
   1270 	 * If the TCB exists but is in CLOSED state, it is embryonic,
   1271 	 * but should either do a listen or a connect soon.
   1272 	 */
   1273 	tp = NULL;
   1274 	so = NULL;
   1275 	if (inp) {
   1276 		tp = intotcpcb(inp);
   1277 		so = inp->inp_socket;
   1278 	}
   1279 #ifdef INET6
   1280 	else if (in6p) {
   1281 		tp = in6totcpcb(in6p);
   1282 		so = in6p->in6p_socket;
   1283 	}
   1284 #endif
   1285 	if (tp == 0) {
   1286 		TCP_FIELDS_TO_HOST(th);
   1287 		goto dropwithreset_ratelim;
   1288 	}
   1289 	if (tp->t_state == TCPS_CLOSED)
   1290 		goto drop;
   1291 
   1292 	/*
   1293 	 * Checksum extended TCP header and data.
   1294 	 */
   1295 	if (tcp_input_checksum(af, m, th, toff, off, tlen))
   1296 		goto badcsum;
   1297 
   1298 	TCP_FIELDS_TO_HOST(th);
   1299 
   1300 	/* Unscale the window into a 32-bit value. */
   1301 	if ((tiflags & TH_SYN) == 0)
   1302 		tiwin = th->th_win << tp->snd_scale;
   1303 	else
   1304 		tiwin = th->th_win;
   1305 
   1306 #ifdef INET6
   1307 	/* save packet options if user wanted */
   1308 	if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS)) {
   1309 		if (in6p->in6p_options) {
   1310 			m_freem(in6p->in6p_options);
   1311 			in6p->in6p_options = 0;
   1312 		}
   1313 		KASSERT(ip6 != NULL);
   1314 		ip6_savecontrol(in6p, &in6p->in6p_options, ip6, m);
   1315 	}
   1316 #endif
   1317 
   1318 	if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
   1319 		union syn_cache_sa src;
   1320 		union syn_cache_sa dst;
   1321 
   1322 		bzero(&src, sizeof(src));
   1323 		bzero(&dst, sizeof(dst));
   1324 		switch (af) {
   1325 #ifdef INET
   1326 		case AF_INET:
   1327 			src.sin.sin_len = sizeof(struct sockaddr_in);
   1328 			src.sin.sin_family = AF_INET;
   1329 			src.sin.sin_addr = ip->ip_src;
   1330 			src.sin.sin_port = th->th_sport;
   1331 
   1332 			dst.sin.sin_len = sizeof(struct sockaddr_in);
   1333 			dst.sin.sin_family = AF_INET;
   1334 			dst.sin.sin_addr = ip->ip_dst;
   1335 			dst.sin.sin_port = th->th_dport;
   1336 			break;
   1337 #endif
   1338 #ifdef INET6
   1339 		case AF_INET6:
   1340 			src.sin6.sin6_len = sizeof(struct sockaddr_in6);
   1341 			src.sin6.sin6_family = AF_INET6;
   1342 			src.sin6.sin6_addr = ip6->ip6_src;
   1343 			src.sin6.sin6_port = th->th_sport;
   1344 
   1345 			dst.sin6.sin6_len = sizeof(struct sockaddr_in6);
   1346 			dst.sin6.sin6_family = AF_INET6;
   1347 			dst.sin6.sin6_addr = ip6->ip6_dst;
   1348 			dst.sin6.sin6_port = th->th_dport;
   1349 			break;
   1350 #endif /* INET6 */
   1351 		default:
   1352 			goto badsyn;	/*sanity*/
   1353 		}
   1354 
   1355 		if (so->so_options & SO_DEBUG) {
   1356 #ifdef TCP_DEBUG
   1357 			ostate = tp->t_state;
   1358 #endif
   1359 
   1360 			tcp_saveti = NULL;
   1361 			if (iphlen + sizeof(struct tcphdr) > MHLEN)
   1362 				goto nosave;
   1363 
   1364 			if (m->m_len > iphlen && (m->m_flags & M_EXT) == 0) {
   1365 				tcp_saveti = m_copym(m, 0, iphlen, M_DONTWAIT);
   1366 				if (!tcp_saveti)
   1367 					goto nosave;
   1368 			} else {
   1369 				MGETHDR(tcp_saveti, M_DONTWAIT, MT_HEADER);
   1370 				if (!tcp_saveti)
   1371 					goto nosave;
   1372 				MCLAIM(m, &tcp_mowner);
   1373 				tcp_saveti->m_len = iphlen;
   1374 				m_copydata(m, 0, iphlen,
   1375 				    mtod(tcp_saveti, void *));
   1376 			}
   1377 
   1378 			if (M_TRAILINGSPACE(tcp_saveti) < sizeof(struct tcphdr)) {
   1379 				m_freem(tcp_saveti);
   1380 				tcp_saveti = NULL;
   1381 			} else {
   1382 				tcp_saveti->m_len += sizeof(struct tcphdr);
   1383 				memcpy(mtod(tcp_saveti, char *) + iphlen, th,
   1384 				    sizeof(struct tcphdr));
   1385 			}
   1386 	nosave:;
   1387 		}
   1388 		if (so->so_options & SO_ACCEPTCONN) {
   1389 			if ((tiflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
   1390 				if (tiflags & TH_RST) {
   1391 					syn_cache_reset(&src.sa, &dst.sa, th);
   1392 				} else if ((tiflags & (TH_ACK|TH_SYN)) ==
   1393 				    (TH_ACK|TH_SYN)) {
   1394 					/*
   1395 					 * Received a SYN,ACK.  This should
   1396 					 * never happen while we are in
   1397 					 * LISTEN.  Send an RST.
   1398 					 */
   1399 					goto badsyn;
   1400 				} else if (tiflags & TH_ACK) {
   1401 					so = syn_cache_get(&src.sa, &dst.sa,
   1402 						th, toff, tlen, so, m);
   1403 					if (so == NULL) {
   1404 						/*
   1405 						 * We don't have a SYN for
   1406 						 * this ACK; send an RST.
   1407 						 */
   1408 						goto badsyn;
   1409 					} else if (so ==
   1410 					    (struct socket *)(-1)) {
   1411 						/*
   1412 						 * We were unable to create
   1413 						 * the connection.  If the
   1414 						 * 3-way handshake was
   1415 						 * completed, and RST has
   1416 						 * been sent to the peer.
   1417 						 * Since the mbuf might be
   1418 						 * in use for the reply,
   1419 						 * do not free it.
   1420 						 */
   1421 						m = NULL;
   1422 					} else {
   1423 						/*
   1424 						 * We have created a
   1425 						 * full-blown connection.
   1426 						 */
   1427 						tp = NULL;
   1428 						inp = NULL;
   1429 #ifdef INET6
   1430 						in6p = NULL;
   1431 #endif
   1432 						switch (so->so_proto->pr_domain->dom_family) {
   1433 #ifdef INET
   1434 						case AF_INET:
   1435 							inp = sotoinpcb(so);
   1436 							tp = intotcpcb(inp);
   1437 							break;
   1438 #endif
   1439 #ifdef INET6
   1440 						case AF_INET6:
   1441 							in6p = sotoin6pcb(so);
   1442 							tp = in6totcpcb(in6p);
   1443 							break;
   1444 #endif
   1445 						}
   1446 						if (tp == NULL)
   1447 							goto badsyn;	/*XXX*/
   1448 						tiwin <<= tp->snd_scale;
   1449 						goto after_listen;
   1450 					}
   1451 				} else {
   1452 					/*
   1453 					 * None of RST, SYN or ACK was set.
   1454 					 * This is an invalid packet for a
   1455 					 * TCB in LISTEN state.  Send a RST.
   1456 					 */
   1457 					goto badsyn;
   1458 				}
   1459 			} else {
   1460 				/*
   1461 				 * Received a SYN.
   1462 				 *
   1463 				 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
   1464 				 */
   1465 				if (m->m_flags & (M_BCAST|M_MCAST))
   1466 					goto drop;
   1467 
   1468 				switch (af) {
   1469 #ifdef INET6
   1470 				case AF_INET6:
   1471 					if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
   1472 						goto drop;
   1473 					break;
   1474 #endif /* INET6 */
   1475 				case AF_INET:
   1476 					if (IN_MULTICAST(ip->ip_dst.s_addr) ||
   1477 					    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
   1478 						goto drop;
   1479 				break;
   1480 				}
   1481 
   1482 #ifdef INET6
   1483 				/*
   1484 				 * If deprecated address is forbidden, we do
   1485 				 * not accept SYN to deprecated interface
   1486 				 * address to prevent any new inbound
   1487 				 * connection from getting established.
   1488 				 * When we do not accept SYN, we send a TCP
   1489 				 * RST, with deprecated source address (instead
   1490 				 * of dropping it).  We compromise it as it is
   1491 				 * much better for peer to send a RST, and
   1492 				 * RST will be the final packet for the
   1493 				 * exchange.
   1494 				 *
   1495 				 * If we do not forbid deprecated addresses, we
   1496 				 * accept the SYN packet.  RFC2462 does not
   1497 				 * suggest dropping SYN in this case.
   1498 				 * If we decipher RFC2462 5.5.4, it says like
   1499 				 * this:
   1500 				 * 1. use of deprecated addr with existing
   1501 				 *    communication is okay - "SHOULD continue
   1502 				 *    to be used"
   1503 				 * 2. use of it with new communication:
   1504 				 *   (2a) "SHOULD NOT be used if alternate
   1505 				 *        address with sufficient scope is
   1506 				 *        available"
   1507 				 *   (2b) nothing mentioned otherwise.
   1508 				 * Here we fall into (2b) case as we have no
   1509 				 * choice in our source address selection - we
   1510 				 * must obey the peer.
   1511 				 *
   1512 				 * The wording in RFC2462 is confusing, and
   1513 				 * there are multiple description text for
   1514 				 * deprecated address handling - worse, they
   1515 				 * are not exactly the same.  I believe 5.5.4
   1516 				 * is the best one, so we follow 5.5.4.
   1517 				 */
   1518 				if (af == AF_INET6 && !ip6_use_deprecated) {
   1519 					struct in6_ifaddr *ia6;
   1520 					if ((ia6 = in6ifa_ifpwithaddr(m->m_pkthdr.rcvif,
   1521 					    &ip6->ip6_dst)) &&
   1522 					    (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
   1523 						tp = NULL;
   1524 						goto dropwithreset;
   1525 					}
   1526 				}
   1527 #endif
   1528 
   1529 #if defined(IPSEC) || defined(FAST_IPSEC)
   1530 				switch (af) {
   1531 #ifdef INET
   1532 				case AF_INET:
   1533 					if (ipsec4_in_reject_so(m, so)) {
   1534 						ipsecstat.in_polvio++;
   1535 						tp = NULL;
   1536 						goto dropwithreset;
   1537 					}
   1538 					break;
   1539 #endif
   1540 #ifdef INET6
   1541 				case AF_INET6:
   1542 					if (ipsec6_in_reject_so(m, so)) {
   1543 						ipsec6stat.in_polvio++;
   1544 						tp = NULL;
   1545 						goto dropwithreset;
   1546 					}
   1547 					break;
   1548 #endif /*INET6*/
   1549 				}
   1550 #endif /*IPSEC*/
   1551 
   1552 				/*
   1553 				 * LISTEN socket received a SYN
   1554 				 * from itself?  This can't possibly
   1555 				 * be valid; drop the packet.
   1556 				 */
   1557 				if (th->th_sport == th->th_dport) {
   1558 					int i;
   1559 
   1560 					switch (af) {
   1561 #ifdef INET
   1562 					case AF_INET:
   1563 						i = in_hosteq(ip->ip_src, ip->ip_dst);
   1564 						break;
   1565 #endif
   1566 #ifdef INET6
   1567 					case AF_INET6:
   1568 						i = IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6->ip6_dst);
   1569 						break;
   1570 #endif
   1571 					default:
   1572 						i = 1;
   1573 					}
   1574 					if (i) {
   1575 						tcpstat.tcps_badsyn++;
   1576 						goto drop;
   1577 					}
   1578 				}
   1579 
   1580 				/*
   1581 				 * SYN looks ok; create compressed TCP
   1582 				 * state for it.
   1583 				 */
   1584 				if (so->so_qlen <= so->so_qlimit &&
   1585 				    syn_cache_add(&src.sa, &dst.sa, th, tlen,
   1586 						so, m, optp, optlen, &opti))
   1587 					m = NULL;
   1588 			}
   1589 			goto drop;
   1590 		}
   1591 	}
   1592 
   1593 after_listen:
   1594 #ifdef DIAGNOSTIC
   1595 	/*
   1596 	 * Should not happen now that all embryonic connections
   1597 	 * are handled with compressed state.
   1598 	 */
   1599 	if (tp->t_state == TCPS_LISTEN)
   1600 		panic("tcp_input: TCPS_LISTEN");
   1601 #endif
   1602 
   1603 	/*
   1604 	 * Segment received on connection.
   1605 	 * Reset idle time and keep-alive timer.
   1606 	 */
   1607 	tp->t_rcvtime = tcp_now;
   1608 	if (TCPS_HAVEESTABLISHED(tp->t_state))
   1609 		TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle);
   1610 
   1611 	/*
   1612 	 * Process options.
   1613 	 */
   1614 #ifdef TCP_SIGNATURE
   1615 	if (optp || (tp->t_flags & TF_SIGNATURE))
   1616 #else
   1617 	if (optp)
   1618 #endif
   1619 		if (tcp_dooptions(tp, optp, optlen, th, m, toff, &opti) < 0)
   1620 			goto drop;
   1621 
   1622 	if (TCP_SACK_ENABLED(tp)) {
   1623 		tcp_del_sackholes(tp, th);
   1624 	}
   1625 
   1626 	if (TCP_ECN_ALLOWED(tp)) {
   1627 		switch (iptos & IPTOS_ECN_MASK) {
   1628 		case IPTOS_ECN_CE:
   1629 			tp->t_flags |= TF_ECN_SND_ECE;
   1630 			tcpstat.tcps_ecn_ce++;
   1631 			break;
   1632 		case IPTOS_ECN_ECT0:
   1633 			tcpstat.tcps_ecn_ect++;
   1634 			break;
   1635 		case IPTOS_ECN_ECT1:
   1636 			/* XXX: ignore for now -- rpaulo */
   1637 			break;
   1638 		}
   1639 
   1640 		if (tiflags & TH_CWR)
   1641 			tp->t_flags &= ~TF_ECN_SND_ECE;
   1642 
   1643 		/*
   1644 		 * Congestion experienced.
   1645 		 * Ignore if we are already trying to recover.
   1646 		 */
   1647 		if ((tiflags & TH_ECE) && SEQ_GEQ(tp->snd_una, tp->snd_recover))
   1648 			tp->t_congctl->cong_exp(tp);
   1649 	}
   1650 
   1651 	if (opti.ts_present && opti.ts_ecr) {
   1652 		/*
   1653 		 * Calculate the RTT from the returned time stamp and the
   1654 		 * connection's time base.  If the time stamp is later than
   1655 		 * the current time, or is extremely old, fall back to non-1323
   1656 		 * RTT calculation.  Since ts_ecr is unsigned, we can test both
   1657 		 * at the same time.
   1658 		 */
   1659 		ts_rtt = TCP_TIMESTAMP(tp) - opti.ts_ecr + 1;
   1660 		if (ts_rtt > TCP_PAWS_IDLE)
   1661 			ts_rtt = 0;
   1662 	} else {
   1663 		ts_rtt = 0;
   1664 	}
   1665 
   1666 	/*
   1667 	 * Header prediction: check for the two common cases
   1668 	 * of a uni-directional data xfer.  If the packet has
   1669 	 * no control flags, is in-sequence, the window didn't
   1670 	 * change and we're not retransmitting, it's a
   1671 	 * candidate.  If the length is zero and the ack moved
   1672 	 * forward, we're the sender side of the xfer.  Just
   1673 	 * free the data acked & wake any higher level process
   1674 	 * that was blocked waiting for space.  If the length
   1675 	 * is non-zero and the ack didn't move, we're the
   1676 	 * receiver side.  If we're getting packets in-order
   1677 	 * (the reassembly queue is empty), add the data to
   1678 	 * the socket buffer and note that we need a delayed ack.
   1679 	 */
   1680 	if (tp->t_state == TCPS_ESTABLISHED &&
   1681 	    (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ECE|TH_CWR|TH_ACK))
   1682 	        == TH_ACK &&
   1683 	    (!opti.ts_present || TSTMP_GEQ(opti.ts_val, tp->ts_recent)) &&
   1684 	    th->th_seq == tp->rcv_nxt &&
   1685 	    tiwin && tiwin == tp->snd_wnd &&
   1686 	    tp->snd_nxt == tp->snd_max) {
   1687 
   1688 		/*
   1689 		 * If last ACK falls within this segment's sequence numbers,
   1690 		 *  record the timestamp.
   1691 		 * NOTE:
   1692 		 * 1) That the test incorporates suggestions from the latest
   1693 		 *    proposal of the tcplw (at) cray.com list (Braden 1993/04/26).
   1694 		 * 2) That updating only on newer timestamps interferes with
   1695 		 *    our earlier PAWS tests, so this check should be solely
   1696 		 *    predicated on the sequence space of this segment.
   1697 		 * 3) That we modify the segment boundary check to be
   1698 		 *        Last.ACK.Sent <= SEG.SEQ + SEG.Len
   1699 		 *    instead of RFC1323's
   1700 		 *        Last.ACK.Sent < SEG.SEQ + SEG.Len,
   1701 		 *    This modified check allows us to overcome RFC1323's
   1702 		 *    limitations as described in Stevens TCP/IP Illustrated
   1703 		 *    Vol. 2 p.869. In such cases, we can still calculate the
   1704 		 *    RTT correctly when RCV.NXT == Last.ACK.Sent.
   1705 		 */
   1706 		if (opti.ts_present &&
   1707 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
   1708 		    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
   1709 		    ((tiflags & (TH_SYN|TH_FIN)) != 0))) {
   1710 			tp->ts_recent_age = tcp_now;
   1711 			tp->ts_recent = opti.ts_val;
   1712 		}
   1713 
   1714 		if (tlen == 0) {
   1715 			/* Ack prediction. */
   1716 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
   1717 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
   1718 			    tp->snd_cwnd >= tp->snd_wnd &&
   1719 			    tp->t_partialacks < 0) {
   1720 				/*
   1721 				 * this is a pure ack for outstanding data.
   1722 				 */
   1723 				++tcpstat.tcps_predack;
   1724 				if (ts_rtt)
   1725 					tcp_xmit_timer(tp, ts_rtt);
   1726 				else if (tp->t_rtttime &&
   1727 				    SEQ_GT(th->th_ack, tp->t_rtseq))
   1728 					tcp_xmit_timer(tp,
   1729 					  tcp_now - tp->t_rtttime);
   1730 				acked = th->th_ack - tp->snd_una;
   1731 				tcpstat.tcps_rcvackpack++;
   1732 				tcpstat.tcps_rcvackbyte += acked;
   1733 				ND6_HINT(tp);
   1734 
   1735 				if (acked > (tp->t_lastoff - tp->t_inoff))
   1736 					tp->t_lastm = NULL;
   1737 				sbdrop(&so->so_snd, acked);
   1738 				tp->t_lastoff -= acked;
   1739 
   1740 				ICMP_CHECK(tp, th, acked);
   1741 
   1742 				tp->snd_una = th->th_ack;
   1743 				tp->snd_fack = tp->snd_una;
   1744 				if (SEQ_LT(tp->snd_high, tp->snd_una))
   1745 					tp->snd_high = tp->snd_una;
   1746 				m_freem(m);
   1747 
   1748 				/*
   1749 				 * If all outstanding data are acked, stop
   1750 				 * retransmit timer, otherwise restart timer
   1751 				 * using current (possibly backed-off) value.
   1752 				 * If process is waiting for space,
   1753 				 * wakeup/selwakeup/signal.  If data
   1754 				 * are ready to send, let tcp_output
   1755 				 * decide between more output or persist.
   1756 				 */
   1757 				if (tp->snd_una == tp->snd_max)
   1758 					TCP_TIMER_DISARM(tp, TCPT_REXMT);
   1759 				else if (TCP_TIMER_ISARMED(tp,
   1760 				    TCPT_PERSIST) == 0)
   1761 					TCP_TIMER_ARM(tp, TCPT_REXMT,
   1762 					    tp->t_rxtcur);
   1763 
   1764 				sowwakeup(so);
   1765 				if (so->so_snd.sb_cc)
   1766 					(void) tcp_output(tp);
   1767 				if (tcp_saveti)
   1768 					m_freem(tcp_saveti);
   1769 				return;
   1770 			}
   1771 		} else if (th->th_ack == tp->snd_una &&
   1772 		    TAILQ_FIRST(&tp->segq) == NULL &&
   1773 		    tlen <= sbspace(&so->so_rcv)) {
   1774 			/*
   1775 			 * this is a pure, in-sequence data packet
   1776 			 * with nothing on the reassembly queue and
   1777 			 * we have enough buffer space to take it.
   1778 			 */
   1779 			++tcpstat.tcps_preddat;
   1780 			tp->rcv_nxt += tlen;
   1781 			tcpstat.tcps_rcvpack++;
   1782 			tcpstat.tcps_rcvbyte += tlen;
   1783 			ND6_HINT(tp);
   1784 			/*
   1785 			 * Drop TCP, IP headers and TCP options then add data
   1786 			 * to socket buffer.
   1787 			 */
   1788 			if (so->so_state & SS_CANTRCVMORE)
   1789 				m_freem(m);
   1790 			else {
   1791 				m_adj(m, toff + off);
   1792 				sbappendstream(&so->so_rcv, m);
   1793 			}
   1794 			sorwakeup(so);
   1795 			TCP_SETUP_ACK(tp, th);
   1796 			if (tp->t_flags & TF_ACKNOW)
   1797 				(void) tcp_output(tp);
   1798 			if (tcp_saveti)
   1799 				m_freem(tcp_saveti);
   1800 			return;
   1801 		}
   1802 	}
   1803 
   1804 	/*
   1805 	 * Compute mbuf offset to TCP data segment.
   1806 	 */
   1807 	hdroptlen = toff + off;
   1808 
   1809 	/*
   1810 	 * Calculate amount of space in receive window,
   1811 	 * and then do TCP input processing.
   1812 	 * Receive window is amount of space in rcv queue,
   1813 	 * but not less than advertised window.
   1814 	 */
   1815 	{ int win;
   1816 
   1817 	win = sbspace(&so->so_rcv);
   1818 	if (win < 0)
   1819 		win = 0;
   1820 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
   1821 	}
   1822 
   1823 	switch (tp->t_state) {
   1824 	/*
   1825 	 * If the state is SYN_SENT:
   1826 	 *	if seg contains an ACK, but not for our SYN, drop the input.
   1827 	 *	if seg contains a RST, then drop the connection.
   1828 	 *	if seg does not contain SYN, then drop it.
   1829 	 * Otherwise this is an acceptable SYN segment
   1830 	 *	initialize tp->rcv_nxt and tp->irs
   1831 	 *	if seg contains ack then advance tp->snd_una
   1832 	 *	if seg contains a ECE and ECN support is enabled, the stream
   1833 	 *	    is ECN capable.
   1834 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
   1835 	 *	arrange for segment to be acked (eventually)
   1836 	 *	continue processing rest of data/controls, beginning with URG
   1837 	 */
   1838 	case TCPS_SYN_SENT:
   1839 		if ((tiflags & TH_ACK) &&
   1840 		    (SEQ_LEQ(th->th_ack, tp->iss) ||
   1841 		     SEQ_GT(th->th_ack, tp->snd_max)))
   1842 			goto dropwithreset;
   1843 		if (tiflags & TH_RST) {
   1844 			if (tiflags & TH_ACK)
   1845 				tp = tcp_drop(tp, ECONNREFUSED);
   1846 			goto drop;
   1847 		}
   1848 		if ((tiflags & TH_SYN) == 0)
   1849 			goto drop;
   1850 		if (tiflags & TH_ACK) {
   1851 			tp->snd_una = th->th_ack;
   1852 			if (SEQ_LT(tp->snd_nxt, tp->snd_una))
   1853 				tp->snd_nxt = tp->snd_una;
   1854 			if (SEQ_LT(tp->snd_high, tp->snd_una))
   1855 				tp->snd_high = tp->snd_una;
   1856 			TCP_TIMER_DISARM(tp, TCPT_REXMT);
   1857 
   1858 			if ((tiflags & TH_ECE) && tcp_do_ecn) {
   1859 				tp->t_flags |= TF_ECN_PERMIT;
   1860 				tcpstat.tcps_ecn_shs++;
   1861 			}
   1862 
   1863 		}
   1864 		tp->irs = th->th_seq;
   1865 		tcp_rcvseqinit(tp);
   1866 		tp->t_flags |= TF_ACKNOW;
   1867 		tcp_mss_from_peer(tp, opti.maxseg);
   1868 
   1869 		/*
   1870 		 * Initialize the initial congestion window.  If we
   1871 		 * had to retransmit the SYN, we must initialize cwnd
   1872 		 * to 1 segment (i.e. the Loss Window).
   1873 		 */
   1874 		if (tp->t_flags & TF_SYN_REXMT)
   1875 			tp->snd_cwnd = tp->t_peermss;
   1876 		else {
   1877 			int ss = tcp_init_win;
   1878 #ifdef INET
   1879 			if (inp != NULL && in_localaddr(inp->inp_faddr))
   1880 				ss = tcp_init_win_local;
   1881 #endif
   1882 #ifdef INET6
   1883 			if (in6p != NULL && in6_localaddr(&in6p->in6p_faddr))
   1884 				ss = tcp_init_win_local;
   1885 #endif
   1886 			tp->snd_cwnd = TCP_INITIAL_WINDOW(ss, tp->t_peermss);
   1887 		}
   1888 
   1889 		tcp_rmx_rtt(tp);
   1890 		if (tiflags & TH_ACK) {
   1891 			tcpstat.tcps_connects++;
   1892 			soisconnected(so);
   1893 			tcp_established(tp);
   1894 			/* Do window scaling on this connection? */
   1895 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
   1896 			    (TF_RCVD_SCALE|TF_REQ_SCALE)) {
   1897 				tp->snd_scale = tp->requested_s_scale;
   1898 				tp->rcv_scale = tp->request_r_scale;
   1899 			}
   1900 			TCP_REASS_LOCK(tp);
   1901 			(void) tcp_reass(tp, NULL, (struct mbuf *)0, &tlen);
   1902 			TCP_REASS_UNLOCK(tp);
   1903 			/*
   1904 			 * if we didn't have to retransmit the SYN,
   1905 			 * use its rtt as our initial srtt & rtt var.
   1906 			 */
   1907 			if (tp->t_rtttime)
   1908 				tcp_xmit_timer(tp, tcp_now - tp->t_rtttime);
   1909 		} else
   1910 			tp->t_state = TCPS_SYN_RECEIVED;
   1911 
   1912 		/*
   1913 		 * Advance th->th_seq to correspond to first data byte.
   1914 		 * If data, trim to stay within window,
   1915 		 * dropping FIN if necessary.
   1916 		 */
   1917 		th->th_seq++;
   1918 		if (tlen > tp->rcv_wnd) {
   1919 			todrop = tlen - tp->rcv_wnd;
   1920 			m_adj(m, -todrop);
   1921 			tlen = tp->rcv_wnd;
   1922 			tiflags &= ~TH_FIN;
   1923 			tcpstat.tcps_rcvpackafterwin++;
   1924 			tcpstat.tcps_rcvbyteafterwin += todrop;
   1925 		}
   1926 		tp->snd_wl1 = th->th_seq - 1;
   1927 		tp->rcv_up = th->th_seq;
   1928 		goto step6;
   1929 
   1930 	/*
   1931 	 * If the state is SYN_RECEIVED:
   1932 	 *	If seg contains an ACK, but not for our SYN, drop the input
   1933 	 *	and generate an RST.  See page 36, rfc793
   1934 	 */
   1935 	case TCPS_SYN_RECEIVED:
   1936 		if ((tiflags & TH_ACK) &&
   1937 		    (SEQ_LEQ(th->th_ack, tp->iss) ||
   1938 		     SEQ_GT(th->th_ack, tp->snd_max)))
   1939 			goto dropwithreset;
   1940 		break;
   1941 	}
   1942 
   1943 	/*
   1944 	 * States other than LISTEN or SYN_SENT.
   1945 	 * First check timestamp, if present.
   1946 	 * Then check that at least some bytes of segment are within
   1947 	 * receive window.  If segment begins before rcv_nxt,
   1948 	 * drop leading data (and SYN); if nothing left, just ack.
   1949 	 *
   1950 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
   1951 	 * and it's less than ts_recent, drop it.
   1952 	 */
   1953 	if (opti.ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent &&
   1954 	    TSTMP_LT(opti.ts_val, tp->ts_recent)) {
   1955 
   1956 		/* Check to see if ts_recent is over 24 days old.  */
   1957 		if (tcp_now - tp->ts_recent_age > TCP_PAWS_IDLE) {
   1958 			/*
   1959 			 * Invalidate ts_recent.  If this segment updates
   1960 			 * ts_recent, the age will be reset later and ts_recent
   1961 			 * will get a valid value.  If it does not, setting
   1962 			 * ts_recent to zero will at least satisfy the
   1963 			 * requirement that zero be placed in the timestamp
   1964 			 * echo reply when ts_recent isn't valid.  The
   1965 			 * age isn't reset until we get a valid ts_recent
   1966 			 * because we don't want out-of-order segments to be
   1967 			 * dropped when ts_recent is old.
   1968 			 */
   1969 			tp->ts_recent = 0;
   1970 		} else {
   1971 			tcpstat.tcps_rcvduppack++;
   1972 			tcpstat.tcps_rcvdupbyte += tlen;
   1973 			tcpstat.tcps_pawsdrop++;
   1974 			tcp_new_dsack(tp, th->th_seq, tlen);
   1975 			goto dropafterack;
   1976 		}
   1977 	}
   1978 
   1979 	todrop = tp->rcv_nxt - th->th_seq;
   1980 	dupseg = false;
   1981 	if (todrop > 0) {
   1982 		if (tiflags & TH_SYN) {
   1983 			tiflags &= ~TH_SYN;
   1984 			th->th_seq++;
   1985 			if (th->th_urp > 1)
   1986 				th->th_urp--;
   1987 			else {
   1988 				tiflags &= ~TH_URG;
   1989 				th->th_urp = 0;
   1990 			}
   1991 			todrop--;
   1992 		}
   1993 		if (todrop > tlen ||
   1994 		    (todrop == tlen && (tiflags & TH_FIN) == 0)) {
   1995 			/*
   1996 			 * Any valid FIN or RST must be to the left of the
   1997 			 * window.  At this point the FIN or RST must be a
   1998 			 * duplicate or out of sequence; drop it.
   1999 			 */
   2000 			if (tiflags & TH_RST)
   2001 				goto drop;
   2002 			tiflags &= ~(TH_FIN|TH_RST);
   2003 			/*
   2004 			 * Send an ACK to resynchronize and drop any data.
   2005 			 * But keep on processing for RST or ACK.
   2006 			 */
   2007 			tp->t_flags |= TF_ACKNOW;
   2008 			todrop = tlen;
   2009 			dupseg = true;
   2010 			tcpstat.tcps_rcvdupbyte += todrop;
   2011 			tcpstat.tcps_rcvduppack++;
   2012 		} else if ((tiflags & TH_RST) &&
   2013 			   th->th_seq != tp->last_ack_sent) {
   2014 			/*
   2015 			 * Test for reset before adjusting the sequence
   2016 			 * number for overlapping data.
   2017 			 */
   2018 			goto dropafterack_ratelim;
   2019 		} else {
   2020 			tcpstat.tcps_rcvpartduppack++;
   2021 			tcpstat.tcps_rcvpartdupbyte += todrop;
   2022 		}
   2023 		tcp_new_dsack(tp, th->th_seq, todrop);
   2024 		hdroptlen += todrop;	/*drop from head afterwards*/
   2025 		th->th_seq += todrop;
   2026 		tlen -= todrop;
   2027 		if (th->th_urp > todrop)
   2028 			th->th_urp -= todrop;
   2029 		else {
   2030 			tiflags &= ~TH_URG;
   2031 			th->th_urp = 0;
   2032 		}
   2033 	}
   2034 
   2035 	/*
   2036 	 * If new data are received on a connection after the
   2037 	 * user processes are gone, then RST the other end.
   2038 	 */
   2039 	if ((so->so_state & SS_NOFDREF) &&
   2040 	    tp->t_state > TCPS_CLOSE_WAIT && tlen) {
   2041 		tp = tcp_close(tp);
   2042 		tcpstat.tcps_rcvafterclose++;
   2043 		goto dropwithreset;
   2044 	}
   2045 
   2046 	/*
   2047 	 * If segment ends after window, drop trailing data
   2048 	 * (and PUSH and FIN); if nothing left, just ACK.
   2049 	 */
   2050 	todrop = (th->th_seq + tlen) - (tp->rcv_nxt+tp->rcv_wnd);
   2051 	if (todrop > 0) {
   2052 		tcpstat.tcps_rcvpackafterwin++;
   2053 		if (todrop >= tlen) {
   2054 			/*
   2055 			 * The segment actually starts after the window.
   2056 			 * th->th_seq + tlen - tp->rcv_nxt - tp->rcv_wnd >= tlen
   2057 			 * th->th_seq - tp->rcv_nxt - tp->rcv_wnd >= 0
   2058 			 * th->th_seq >= tp->rcv_nxt + tp->rcv_wnd
   2059 			 */
   2060 			tcpstat.tcps_rcvbyteafterwin += tlen;
   2061 			/*
   2062 			 * If a new connection request is received
   2063 			 * while in TIME_WAIT, drop the old connection
   2064 			 * and start over if the sequence numbers
   2065 			 * are above the previous ones.
   2066 			 *
   2067 			 * NOTE: We will checksum the packet again, and
   2068 			 * so we need to put the header fields back into
   2069 			 * network order!
   2070 			 * XXX This kind of sucks, but we don't expect
   2071 			 * XXX this to happen very often, so maybe it
   2072 			 * XXX doesn't matter so much.
   2073 			 */
   2074 			if (tiflags & TH_SYN &&
   2075 			    tp->t_state == TCPS_TIME_WAIT &&
   2076 			    SEQ_GT(th->th_seq, tp->rcv_nxt)) {
   2077 				tp = tcp_close(tp);
   2078 				TCP_FIELDS_TO_NET(th);
   2079 				goto findpcb;
   2080 			}
   2081 			/*
   2082 			 * If window is closed can only take segments at
   2083 			 * window edge, and have to drop data and PUSH from
   2084 			 * incoming segments.  Continue processing, but
   2085 			 * remember to ack.  Otherwise, drop segment
   2086 			 * and (if not RST) ack.
   2087 			 */
   2088 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
   2089 				tp->t_flags |= TF_ACKNOW;
   2090 				tcpstat.tcps_rcvwinprobe++;
   2091 			} else
   2092 				goto dropafterack;
   2093 		} else
   2094 			tcpstat.tcps_rcvbyteafterwin += todrop;
   2095 		m_adj(m, -todrop);
   2096 		tlen -= todrop;
   2097 		tiflags &= ~(TH_PUSH|TH_FIN);
   2098 	}
   2099 
   2100 	/*
   2101 	 * If last ACK falls within this segment's sequence numbers,
   2102 	 * and the timestamp is newer, record it.
   2103 	 */
   2104 	if (opti.ts_present && TSTMP_GEQ(opti.ts_val, tp->ts_recent) &&
   2105 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
   2106 	    SEQ_LT(tp->last_ack_sent, th->th_seq + tlen +
   2107 		   ((tiflags & (TH_SYN|TH_FIN)) != 0))) {
   2108 		tp->ts_recent_age = tcp_now;
   2109 		tp->ts_recent = opti.ts_val;
   2110 	}
   2111 
   2112 	/*
   2113 	 * If the RST bit is set examine the state:
   2114 	 *    SYN_RECEIVED STATE:
   2115 	 *	If passive open, return to LISTEN state.
   2116 	 *	If active open, inform user that connection was refused.
   2117 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
   2118 	 *	Inform user that connection was reset, and close tcb.
   2119 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
   2120 	 *	Close the tcb.
   2121 	 */
   2122 	if (tiflags & TH_RST) {
   2123 		if (th->th_seq != tp->last_ack_sent)
   2124 			goto dropafterack_ratelim;
   2125 
   2126 		switch (tp->t_state) {
   2127 		case TCPS_SYN_RECEIVED:
   2128 			so->so_error = ECONNREFUSED;
   2129 			goto close;
   2130 
   2131 		case TCPS_ESTABLISHED:
   2132 		case TCPS_FIN_WAIT_1:
   2133 		case TCPS_FIN_WAIT_2:
   2134 		case TCPS_CLOSE_WAIT:
   2135 			so->so_error = ECONNRESET;
   2136 		close:
   2137 			tp->t_state = TCPS_CLOSED;
   2138 			tcpstat.tcps_drops++;
   2139 			tp = tcp_close(tp);
   2140 			goto drop;
   2141 
   2142 		case TCPS_CLOSING:
   2143 		case TCPS_LAST_ACK:
   2144 		case TCPS_TIME_WAIT:
   2145 			tp = tcp_close(tp);
   2146 			goto drop;
   2147 		}
   2148 	}
   2149 
   2150 	/*
   2151 	 * Since we've covered the SYN-SENT and SYN-RECEIVED states above
   2152 	 * we must be in a synchronized state.  RFC791 states (under RST
   2153 	 * generation) that any unacceptable segment (an out-of-order SYN
   2154 	 * qualifies) received in a synchronized state must elicit only an
   2155 	 * empty acknowledgment segment ... and the connection remains in
   2156 	 * the same state.
   2157 	 */
   2158 	if (tiflags & TH_SYN) {
   2159 		if (tp->rcv_nxt == th->th_seq) {
   2160 			tcp_respond(tp, m, m, th, (tcp_seq)0, th->th_ack - 1,
   2161 			    TH_ACK);
   2162 			if (tcp_saveti)
   2163 				m_freem(tcp_saveti);
   2164 			return;
   2165 		}
   2166 
   2167 		goto dropafterack_ratelim;
   2168 	}
   2169 
   2170 	/*
   2171 	 * If the ACK bit is off we drop the segment and return.
   2172 	 */
   2173 	if ((tiflags & TH_ACK) == 0) {
   2174 		if (tp->t_flags & TF_ACKNOW)
   2175 			goto dropafterack;
   2176 		else
   2177 			goto drop;
   2178 	}
   2179 
   2180 	/*
   2181 	 * Ack processing.
   2182 	 */
   2183 	switch (tp->t_state) {
   2184 
   2185 	/*
   2186 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
   2187 	 * ESTABLISHED state and continue processing, otherwise
   2188 	 * send an RST.
   2189 	 */
   2190 	case TCPS_SYN_RECEIVED:
   2191 		if (SEQ_GT(tp->snd_una, th->th_ack) ||
   2192 		    SEQ_GT(th->th_ack, tp->snd_max))
   2193 			goto dropwithreset;
   2194 		tcpstat.tcps_connects++;
   2195 		soisconnected(so);
   2196 		tcp_established(tp);
   2197 		/* Do window scaling? */
   2198 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
   2199 		    (TF_RCVD_SCALE|TF_REQ_SCALE)) {
   2200 			tp->snd_scale = tp->requested_s_scale;
   2201 			tp->rcv_scale = tp->request_r_scale;
   2202 		}
   2203 		TCP_REASS_LOCK(tp);
   2204 		(void) tcp_reass(tp, NULL, (struct mbuf *)0, &tlen);
   2205 		TCP_REASS_UNLOCK(tp);
   2206 		tp->snd_wl1 = th->th_seq - 1;
   2207 		/* fall into ... */
   2208 
   2209 	/*
   2210 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
   2211 	 * ACKs.  If the ack is in the range
   2212 	 *	tp->snd_una < th->th_ack <= tp->snd_max
   2213 	 * then advance tp->snd_una to th->th_ack and drop
   2214 	 * data from the retransmission queue.  If this ACK reflects
   2215 	 * more up to date window information we update our window information.
   2216 	 */
   2217 	case TCPS_ESTABLISHED:
   2218 	case TCPS_FIN_WAIT_1:
   2219 	case TCPS_FIN_WAIT_2:
   2220 	case TCPS_CLOSE_WAIT:
   2221 	case TCPS_CLOSING:
   2222 	case TCPS_LAST_ACK:
   2223 	case TCPS_TIME_WAIT:
   2224 
   2225 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
   2226 			if (tlen == 0 && !dupseg && tiwin == tp->snd_wnd) {
   2227 				tcpstat.tcps_rcvdupack++;
   2228 				/*
   2229 				 * If we have outstanding data (other than
   2230 				 * a window probe), this is a completely
   2231 				 * duplicate ack (ie, window info didn't
   2232 				 * change), the ack is the biggest we've
   2233 				 * seen and we've seen exactly our rexmt
   2234 				 * threshhold of them, assume a packet
   2235 				 * has been dropped and retransmit it.
   2236 				 * Kludge snd_nxt & the congestion
   2237 				 * window so we send only this one
   2238 				 * packet.
   2239 				 */
   2240 				if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 ||
   2241 				    th->th_ack != tp->snd_una)
   2242 					tp->t_dupacks = 0;
   2243 				else if (tp->t_partialacks < 0 &&
   2244 					 ((!TCP_SACK_ENABLED(tp) &&
   2245 					 ++tp->t_dupacks == tcprexmtthresh) ||
   2246 					 TCP_FACK_FASTRECOV(tp))) {
   2247 					/*
   2248 					 * Do the fast retransmit, and adjust
   2249 					 * congestion control paramenters.
   2250 					 */
   2251 					if (tp->t_congctl->fast_retransmit(tp, th)) {
   2252 						/* False fast retransmit */
   2253 						break;
   2254 					} else
   2255 						goto drop;
   2256 				} else if (tp->t_dupacks > tcprexmtthresh) {
   2257 					tp->snd_cwnd += tp->t_segsz;
   2258 					(void) tcp_output(tp);
   2259 					goto drop;
   2260 				}
   2261 			} else {
   2262 				/*
   2263 				 * If the ack appears to be very old, only
   2264 				 * allow data that is in-sequence.  This
   2265 				 * makes it somewhat more difficult to insert
   2266 				 * forged data by guessing sequence numbers.
   2267 				 * Sent an ack to try to update the send
   2268 				 * sequence number on the other side.
   2269 				 */
   2270 				if (tlen && th->th_seq != tp->rcv_nxt &&
   2271 				    SEQ_LT(th->th_ack,
   2272 				    tp->snd_una - tp->max_sndwnd))
   2273 					goto dropafterack;
   2274 			}
   2275 			break;
   2276 		}
   2277 		/*
   2278 		 * If the congestion window was inflated to account
   2279 		 * for the other side's cached packets, retract it.
   2280 		 */
   2281 		/* XXX: make SACK have his own congestion control
   2282 		 * struct -- rpaulo */
   2283 		if (TCP_SACK_ENABLED(tp))
   2284 			tcp_sack_newack(tp, th);
   2285 		else
   2286 			tp->t_congctl->fast_retransmit_newack(tp, th);
   2287 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
   2288 			tcpstat.tcps_rcvacktoomuch++;
   2289 			goto dropafterack;
   2290 		}
   2291 		acked = th->th_ack - tp->snd_una;
   2292 		tcpstat.tcps_rcvackpack++;
   2293 		tcpstat.tcps_rcvackbyte += acked;
   2294 
   2295 		/*
   2296 		 * If we have a timestamp reply, update smoothed
   2297 		 * round trip time.  If no timestamp is present but
   2298 		 * transmit timer is running and timed sequence
   2299 		 * number was acked, update smoothed round trip time.
   2300 		 * Since we now have an rtt measurement, cancel the
   2301 		 * timer backoff (cf., Phil Karn's retransmit alg.).
   2302 		 * Recompute the initial retransmit timer.
   2303 		 */
   2304 		if (ts_rtt)
   2305 			tcp_xmit_timer(tp, ts_rtt);
   2306 		else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq))
   2307 			tcp_xmit_timer(tp, tcp_now - tp->t_rtttime);
   2308 
   2309 		/*
   2310 		 * If all outstanding data is acked, stop retransmit
   2311 		 * timer and remember to restart (more output or persist).
   2312 		 * If there is more data to be acked, restart retransmit
   2313 		 * timer, using current (possibly backed-off) value.
   2314 		 */
   2315 		if (th->th_ack == tp->snd_max) {
   2316 			TCP_TIMER_DISARM(tp, TCPT_REXMT);
   2317 			needoutput = 1;
   2318 		} else if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0)
   2319 			TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
   2320 
   2321 		/*
   2322 		 * New data has been acked, adjust the congestion window.
   2323 		 */
   2324 		tp->t_congctl->newack(tp, th);
   2325 
   2326 		ND6_HINT(tp);
   2327 		if (acked > so->so_snd.sb_cc) {
   2328 			tp->snd_wnd -= so->so_snd.sb_cc;
   2329 			sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
   2330 			ourfinisacked = 1;
   2331 		} else {
   2332 			if (acked > (tp->t_lastoff - tp->t_inoff))
   2333 				tp->t_lastm = NULL;
   2334 			sbdrop(&so->so_snd, acked);
   2335 			tp->t_lastoff -= acked;
   2336 			tp->snd_wnd -= acked;
   2337 			ourfinisacked = 0;
   2338 		}
   2339 		sowwakeup(so);
   2340 
   2341 		ICMP_CHECK(tp, th, acked);
   2342 
   2343 		tp->snd_una = th->th_ack;
   2344 		if (SEQ_GT(tp->snd_una, tp->snd_fack))
   2345 			tp->snd_fack = tp->snd_una;
   2346 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
   2347 			tp->snd_nxt = tp->snd_una;
   2348 		if (SEQ_LT(tp->snd_high, tp->snd_una))
   2349 			tp->snd_high = tp->snd_una;
   2350 
   2351 		switch (tp->t_state) {
   2352 
   2353 		/*
   2354 		 * In FIN_WAIT_1 STATE in addition to the processing
   2355 		 * for the ESTABLISHED state if our FIN is now acknowledged
   2356 		 * then enter FIN_WAIT_2.
   2357 		 */
   2358 		case TCPS_FIN_WAIT_1:
   2359 			if (ourfinisacked) {
   2360 				/*
   2361 				 * If we can't receive any more
   2362 				 * data, then closing user can proceed.
   2363 				 * Starting the timer is contrary to the
   2364 				 * specification, but if we don't get a FIN
   2365 				 * we'll hang forever.
   2366 				 */
   2367 				if (so->so_state & SS_CANTRCVMORE) {
   2368 					soisdisconnected(so);
   2369 					if (tcp_maxidle > 0)
   2370 						TCP_TIMER_ARM(tp, TCPT_2MSL,
   2371 						    tcp_maxidle);
   2372 				}
   2373 				tp->t_state = TCPS_FIN_WAIT_2;
   2374 			}
   2375 			break;
   2376 
   2377 	 	/*
   2378 		 * In CLOSING STATE in addition to the processing for
   2379 		 * the ESTABLISHED state if the ACK acknowledges our FIN
   2380 		 * then enter the TIME-WAIT state, otherwise ignore
   2381 		 * the segment.
   2382 		 */
   2383 		case TCPS_CLOSING:
   2384 			if (ourfinisacked) {
   2385 				tp->t_state = TCPS_TIME_WAIT;
   2386 				tcp_canceltimers(tp);
   2387 				TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
   2388 				soisdisconnected(so);
   2389 			}
   2390 			break;
   2391 
   2392 		/*
   2393 		 * In LAST_ACK, we may still be waiting for data to drain
   2394 		 * and/or to be acked, as well as for the ack of our FIN.
   2395 		 * If our FIN is now acknowledged, delete the TCB,
   2396 		 * enter the closed state and return.
   2397 		 */
   2398 		case TCPS_LAST_ACK:
   2399 			if (ourfinisacked) {
   2400 				tp = tcp_close(tp);
   2401 				goto drop;
   2402 			}
   2403 			break;
   2404 
   2405 		/*
   2406 		 * In TIME_WAIT state the only thing that should arrive
   2407 		 * is a retransmission of the remote FIN.  Acknowledge
   2408 		 * it and restart the finack timer.
   2409 		 */
   2410 		case TCPS_TIME_WAIT:
   2411 			TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
   2412 			goto dropafterack;
   2413 		}
   2414 	}
   2415 
   2416 step6:
   2417 	/*
   2418 	 * Update window information.
   2419 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
   2420 	 */
   2421 	if ((tiflags & TH_ACK) && (SEQ_LT(tp->snd_wl1, th->th_seq) ||
   2422 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
   2423 	    (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
   2424 		/* keep track of pure window updates */
   2425 		if (tlen == 0 &&
   2426 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
   2427 			tcpstat.tcps_rcvwinupd++;
   2428 		tp->snd_wnd = tiwin;
   2429 		tp->snd_wl1 = th->th_seq;
   2430 		tp->snd_wl2 = th->th_ack;
   2431 		if (tp->snd_wnd > tp->max_sndwnd)
   2432 			tp->max_sndwnd = tp->snd_wnd;
   2433 		needoutput = 1;
   2434 	}
   2435 
   2436 	/*
   2437 	 * Process segments with URG.
   2438 	 */
   2439 	if ((tiflags & TH_URG) && th->th_urp &&
   2440 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
   2441 		/*
   2442 		 * This is a kludge, but if we receive and accept
   2443 		 * random urgent pointers, we'll crash in
   2444 		 * soreceive.  It's hard to imagine someone
   2445 		 * actually wanting to send this much urgent data.
   2446 		 */
   2447 		if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
   2448 			th->th_urp = 0;			/* XXX */
   2449 			tiflags &= ~TH_URG;		/* XXX */
   2450 			goto dodata;			/* XXX */
   2451 		}
   2452 		/*
   2453 		 * If this segment advances the known urgent pointer,
   2454 		 * then mark the data stream.  This should not happen
   2455 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
   2456 		 * a FIN has been received from the remote side.
   2457 		 * In these states we ignore the URG.
   2458 		 *
   2459 		 * According to RFC961 (Assigned Protocols),
   2460 		 * the urgent pointer points to the last octet
   2461 		 * of urgent data.  We continue, however,
   2462 		 * to consider it to indicate the first octet
   2463 		 * of data past the urgent section as the original
   2464 		 * spec states (in one of two places).
   2465 		 */
   2466 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
   2467 			tp->rcv_up = th->th_seq + th->th_urp;
   2468 			so->so_oobmark = so->so_rcv.sb_cc +
   2469 			    (tp->rcv_up - tp->rcv_nxt) - 1;
   2470 			if (so->so_oobmark == 0)
   2471 				so->so_state |= SS_RCVATMARK;
   2472 			sohasoutofband(so);
   2473 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
   2474 		}
   2475 		/*
   2476 		 * Remove out of band data so doesn't get presented to user.
   2477 		 * This can happen independent of advancing the URG pointer,
   2478 		 * but if two URG's are pending at once, some out-of-band
   2479 		 * data may creep in... ick.
   2480 		 */
   2481 		if (th->th_urp <= (u_int16_t) tlen
   2482 #ifdef SO_OOBINLINE
   2483 		     && (so->so_options & SO_OOBINLINE) == 0
   2484 #endif
   2485 		     )
   2486 			tcp_pulloutofband(so, th, m, hdroptlen);
   2487 	} else
   2488 		/*
   2489 		 * If no out of band data is expected,
   2490 		 * pull receive urgent pointer along
   2491 		 * with the receive window.
   2492 		 */
   2493 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
   2494 			tp->rcv_up = tp->rcv_nxt;
   2495 dodata:							/* XXX */
   2496 
   2497 	/*
   2498 	 * Process the segment text, merging it into the TCP sequencing queue,
   2499 	 * and arranging for acknowledgement of receipt if necessary.
   2500 	 * This process logically involves adjusting tp->rcv_wnd as data
   2501 	 * is presented to the user (this happens in tcp_usrreq.c,
   2502 	 * case PRU_RCVD).  If a FIN has already been received on this
   2503 	 * connection then we just ignore the text.
   2504 	 */
   2505 	if ((tlen || (tiflags & TH_FIN)) &&
   2506 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
   2507 		/*
   2508 		 * Insert segment ti into reassembly queue of tcp with
   2509 		 * control block tp.  Return TH_FIN if reassembly now includes
   2510 		 * a segment with FIN.  The macro form does the common case
   2511 		 * inline (segment is the next to be received on an
   2512 		 * established connection, and the queue is empty),
   2513 		 * avoiding linkage into and removal from the queue and
   2514 		 * repetition of various conversions.
   2515 		 * Set DELACK for segments received in order, but ack
   2516 		 * immediately when segments are out of order
   2517 		 * (so fast retransmit can work).
   2518 		 */
   2519 		/* NOTE: this was TCP_REASS() macro, but used only once */
   2520 		TCP_REASS_LOCK(tp);
   2521 		if (th->th_seq == tp->rcv_nxt &&
   2522 		    TAILQ_FIRST(&tp->segq) == NULL &&
   2523 		    tp->t_state == TCPS_ESTABLISHED) {
   2524 			TCP_SETUP_ACK(tp, th);
   2525 			tp->rcv_nxt += tlen;
   2526 			tiflags = th->th_flags & TH_FIN;
   2527 			tcpstat.tcps_rcvpack++;
   2528 			tcpstat.tcps_rcvbyte += tlen;
   2529 			ND6_HINT(tp);
   2530 			if (so->so_state & SS_CANTRCVMORE)
   2531 				m_freem(m);
   2532 			else {
   2533 				m_adj(m, hdroptlen);
   2534 				sbappendstream(&(so)->so_rcv, m);
   2535 			}
   2536 			sorwakeup(so);
   2537 		} else {
   2538 			m_adj(m, hdroptlen);
   2539 			tiflags = tcp_reass(tp, th, m, &tlen);
   2540 			tp->t_flags |= TF_ACKNOW;
   2541 		}
   2542 		TCP_REASS_UNLOCK(tp);
   2543 
   2544 		/*
   2545 		 * Note the amount of data that peer has sent into
   2546 		 * our window, in order to estimate the sender's
   2547 		 * buffer size.
   2548 		 */
   2549 		len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
   2550 	} else {
   2551 		m_freem(m);
   2552 		m = NULL;
   2553 		tiflags &= ~TH_FIN;
   2554 	}
   2555 
   2556 	/*
   2557 	 * If FIN is received ACK the FIN and let the user know
   2558 	 * that the connection is closing.  Ignore a FIN received before
   2559 	 * the connection is fully established.
   2560 	 */
   2561 	if ((tiflags & TH_FIN) && TCPS_HAVEESTABLISHED(tp->t_state)) {
   2562 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
   2563 			socantrcvmore(so);
   2564 			tp->t_flags |= TF_ACKNOW;
   2565 			tp->rcv_nxt++;
   2566 		}
   2567 		switch (tp->t_state) {
   2568 
   2569 	 	/*
   2570 		 * In ESTABLISHED STATE enter the CLOSE_WAIT state.
   2571 		 */
   2572 		case TCPS_ESTABLISHED:
   2573 			tp->t_state = TCPS_CLOSE_WAIT;
   2574 			break;
   2575 
   2576 	 	/*
   2577 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
   2578 		 * enter the CLOSING state.
   2579 		 */
   2580 		case TCPS_FIN_WAIT_1:
   2581 			tp->t_state = TCPS_CLOSING;
   2582 			break;
   2583 
   2584 	 	/*
   2585 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
   2586 		 * starting the time-wait timer, turning off the other
   2587 		 * standard timers.
   2588 		 */
   2589 		case TCPS_FIN_WAIT_2:
   2590 			tp->t_state = TCPS_TIME_WAIT;
   2591 			tcp_canceltimers(tp);
   2592 			TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
   2593 			soisdisconnected(so);
   2594 			break;
   2595 
   2596 		/*
   2597 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
   2598 		 */
   2599 		case TCPS_TIME_WAIT:
   2600 			TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
   2601 			break;
   2602 		}
   2603 	}
   2604 #ifdef TCP_DEBUG
   2605 	if (so->so_options & SO_DEBUG)
   2606 		tcp_trace(TA_INPUT, ostate, tp, tcp_saveti, 0);
   2607 #endif
   2608 
   2609 	/*
   2610 	 * Return any desired output.
   2611 	 */
   2612 	if (needoutput || (tp->t_flags & TF_ACKNOW)) {
   2613 		(void) tcp_output(tp);
   2614 	}
   2615 	if (tcp_saveti)
   2616 		m_freem(tcp_saveti);
   2617 	return;
   2618 
   2619 badsyn:
   2620 	/*
   2621 	 * Received a bad SYN.  Increment counters and dropwithreset.
   2622 	 */
   2623 	tcpstat.tcps_badsyn++;
   2624 	tp = NULL;
   2625 	goto dropwithreset;
   2626 
   2627 dropafterack:
   2628 	/*
   2629 	 * Generate an ACK dropping incoming segment if it occupies
   2630 	 * sequence space, where the ACK reflects our state.
   2631 	 */
   2632 	if (tiflags & TH_RST)
   2633 		goto drop;
   2634 	goto dropafterack2;
   2635 
   2636 dropafterack_ratelim:
   2637 	/*
   2638 	 * We may want to rate-limit ACKs against SYN/RST attack.
   2639 	 */
   2640 	if (ppsratecheck(&tcp_ackdrop_ppslim_last, &tcp_ackdrop_ppslim_count,
   2641 	    tcp_ackdrop_ppslim) == 0) {
   2642 		/* XXX stat */
   2643 		goto drop;
   2644 	}
   2645 	/* ...fall into dropafterack2... */
   2646 
   2647 dropafterack2:
   2648 	m_freem(m);
   2649 	tp->t_flags |= TF_ACKNOW;
   2650 	(void) tcp_output(tp);
   2651 	if (tcp_saveti)
   2652 		m_freem(tcp_saveti);
   2653 	return;
   2654 
   2655 dropwithreset_ratelim:
   2656 	/*
   2657 	 * We may want to rate-limit RSTs in certain situations,
   2658 	 * particularly if we are sending an RST in response to
   2659 	 * an attempt to connect to or otherwise communicate with
   2660 	 * a port for which we have no socket.
   2661 	 */
   2662 	if (ppsratecheck(&tcp_rst_ppslim_last, &tcp_rst_ppslim_count,
   2663 	    tcp_rst_ppslim) == 0) {
   2664 		/* XXX stat */
   2665 		goto drop;
   2666 	}
   2667 	/* ...fall into dropwithreset... */
   2668 
   2669 dropwithreset:
   2670 	/*
   2671 	 * Generate a RST, dropping incoming segment.
   2672 	 * Make ACK acceptable to originator of segment.
   2673 	 */
   2674 	if (tiflags & TH_RST)
   2675 		goto drop;
   2676 
   2677 	switch (af) {
   2678 #ifdef INET6
   2679 	case AF_INET6:
   2680 		/* For following calls to tcp_respond */
   2681 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
   2682 			goto drop;
   2683 		break;
   2684 #endif /* INET6 */
   2685 	case AF_INET:
   2686 		if (IN_MULTICAST(ip->ip_dst.s_addr) ||
   2687 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
   2688 			goto drop;
   2689 	}
   2690 
   2691 	if (tiflags & TH_ACK)
   2692 		(void)tcp_respond(tp, m, m, th, (tcp_seq)0, th->th_ack, TH_RST);
   2693 	else {
   2694 		if (tiflags & TH_SYN)
   2695 			tlen++;
   2696 		(void)tcp_respond(tp, m, m, th, th->th_seq + tlen, (tcp_seq)0,
   2697 		    TH_RST|TH_ACK);
   2698 	}
   2699 	if (tcp_saveti)
   2700 		m_freem(tcp_saveti);
   2701 	return;
   2702 
   2703 badcsum:
   2704 drop:
   2705 	/*
   2706 	 * Drop space held by incoming segment and return.
   2707 	 */
   2708 	if (tp) {
   2709 		if (tp->t_inpcb)
   2710 			so = tp->t_inpcb->inp_socket;
   2711 #ifdef INET6
   2712 		else if (tp->t_in6pcb)
   2713 			so = tp->t_in6pcb->in6p_socket;
   2714 #endif
   2715 		else
   2716 			so = NULL;
   2717 #ifdef TCP_DEBUG
   2718 		if (so && (so->so_options & SO_DEBUG) != 0)
   2719 			tcp_trace(TA_DROP, ostate, tp, tcp_saveti, 0);
   2720 #endif
   2721 	}
   2722 	if (tcp_saveti)
   2723 		m_freem(tcp_saveti);
   2724 	m_freem(m);
   2725 	return;
   2726 }
   2727 
   2728 #ifdef TCP_SIGNATURE
   2729 int
   2730 tcp_signature_apply(void *fstate, void *data, u_int len)
   2731 {
   2732 
   2733 	MD5Update(fstate, (u_char *)data, len);
   2734 	return (0);
   2735 }
   2736 
   2737 struct secasvar *
   2738 tcp_signature_getsav(struct mbuf *m, struct tcphdr *th)
   2739 {
   2740 	struct secasvar *sav;
   2741 #ifdef FAST_IPSEC
   2742 	union sockaddr_union dst;
   2743 #endif
   2744 	struct ip *ip;
   2745 	struct ip6_hdr *ip6;
   2746 
   2747 	ip = mtod(m, struct ip *);
   2748 	switch (ip->ip_v) {
   2749 	case 4:
   2750 		ip = mtod(m, struct ip *);
   2751 		ip6 = NULL;
   2752 		break;
   2753 	case 6:
   2754 		ip = NULL;
   2755 		ip6 = mtod(m, struct ip6_hdr *);
   2756 		break;
   2757 	default:
   2758 		return (NULL);
   2759 	}
   2760 
   2761 #ifdef FAST_IPSEC
   2762 	/* Extract the destination from the IP header in the mbuf. */
   2763 	bzero(&dst, sizeof(union sockaddr_union));
   2764 	if (ip !=NULL) {
   2765 		dst.sa.sa_len = sizeof(struct sockaddr_in);
   2766 		dst.sa.sa_family = AF_INET;
   2767 		dst.sin.sin_addr = ip->ip_dst;
   2768 	} else {
   2769 		dst.sa.sa_len = sizeof(struct sockaddr_in6);
   2770 		dst.sa.sa_family = AF_INET6;
   2771 		dst.sin6.sin6_addr = ip6->ip6_dst;
   2772 	}
   2773 
   2774 	/*
   2775 	 * Look up an SADB entry which matches the address of the peer.
   2776 	 */
   2777 	sav = KEY_ALLOCSA(&dst, IPPROTO_TCP, htonl(TCP_SIG_SPI));
   2778 #else
   2779 	if (ip)
   2780 		sav = key_allocsa(AF_INET, (void *)&ip->ip_src,
   2781 		    (void *)&ip->ip_dst, IPPROTO_TCP,
   2782 		    htonl(TCP_SIG_SPI), 0, 0);
   2783 	else
   2784 		sav = key_allocsa(AF_INET6, (void *)&ip6->ip6_src,
   2785 		    (void *)&ip6->ip6_dst, IPPROTO_TCP,
   2786 		    htonl(TCP_SIG_SPI), 0, 0);
   2787 #endif
   2788 
   2789 	return (sav);	/* freesav must be performed by caller */
   2790 }
   2791 
   2792 int
   2793 tcp_signature(struct mbuf *m, struct tcphdr *th, int thoff,
   2794     struct secasvar *sav, char *sig)
   2795 {
   2796 	MD5_CTX ctx;
   2797 	struct ip *ip;
   2798 	struct ipovly *ipovly;
   2799 	struct ip6_hdr *ip6;
   2800 	struct ippseudo ippseudo;
   2801 	struct ip6_hdr_pseudo ip6pseudo;
   2802 	struct tcphdr th0;
   2803 	int l, tcphdrlen;
   2804 
   2805 	if (sav == NULL)
   2806 		return (-1);
   2807 
   2808 	tcphdrlen = th->th_off * 4;
   2809 
   2810 	switch (mtod(m, struct ip *)->ip_v) {
   2811 	case 4:
   2812 		ip = mtod(m, struct ip *);
   2813 		ip6 = NULL;
   2814 		break;
   2815 	case 6:
   2816 		ip = NULL;
   2817 		ip6 = mtod(m, struct ip6_hdr *);
   2818 		break;
   2819 	default:
   2820 		return (-1);
   2821 	}
   2822 
   2823 	MD5Init(&ctx);
   2824 
   2825 	if (ip) {
   2826 		memset(&ippseudo, 0, sizeof(ippseudo));
   2827 		ipovly = (struct ipovly *)ip;
   2828 		ippseudo.ippseudo_src = ipovly->ih_src;
   2829 		ippseudo.ippseudo_dst = ipovly->ih_dst;
   2830 		ippseudo.ippseudo_pad = 0;
   2831 		ippseudo.ippseudo_p = IPPROTO_TCP;
   2832 		ippseudo.ippseudo_len = htons(m->m_pkthdr.len - thoff);
   2833 		MD5Update(&ctx, (char *)&ippseudo, sizeof(ippseudo));
   2834 	} else {
   2835 		memset(&ip6pseudo, 0, sizeof(ip6pseudo));
   2836 		ip6pseudo.ip6ph_src = ip6->ip6_src;
   2837 		in6_clearscope(&ip6pseudo.ip6ph_src);
   2838 		ip6pseudo.ip6ph_dst = ip6->ip6_dst;
   2839 		in6_clearscope(&ip6pseudo.ip6ph_dst);
   2840 		ip6pseudo.ip6ph_len = htons(m->m_pkthdr.len - thoff);
   2841 		ip6pseudo.ip6ph_nxt = IPPROTO_TCP;
   2842 		MD5Update(&ctx, (char *)&ip6pseudo, sizeof(ip6pseudo));
   2843 	}
   2844 
   2845 	th0 = *th;
   2846 	th0.th_sum = 0;
   2847 	MD5Update(&ctx, (char *)&th0, sizeof(th0));
   2848 
   2849 	l = m->m_pkthdr.len - thoff - tcphdrlen;
   2850 	if (l > 0)
   2851 		m_apply(m, thoff + tcphdrlen,
   2852 		    m->m_pkthdr.len - thoff - tcphdrlen,
   2853 		    tcp_signature_apply, &ctx);
   2854 
   2855 	MD5Update(&ctx, _KEYBUF(sav->key_auth), _KEYLEN(sav->key_auth));
   2856 	MD5Final(sig, &ctx);
   2857 
   2858 	return (0);
   2859 }
   2860 #endif
   2861 
   2862 static int
   2863 tcp_dooptions(struct tcpcb *tp, const u_char *cp, int cnt,
   2864     const struct tcphdr *th,
   2865     struct mbuf *m, int toff, struct tcp_opt_info *oi)
   2866 {
   2867 	u_int16_t mss;
   2868 	int opt, optlen = 0;
   2869 #ifdef TCP_SIGNATURE
   2870 	void *sigp = NULL;
   2871 	char sigbuf[TCP_SIGLEN];
   2872 	struct secasvar *sav = NULL;
   2873 #endif
   2874 
   2875 	for (; cp && cnt > 0; cnt -= optlen, cp += optlen) {
   2876 		opt = cp[0];
   2877 		if (opt == TCPOPT_EOL)
   2878 			break;
   2879 		if (opt == TCPOPT_NOP)
   2880 			optlen = 1;
   2881 		else {
   2882 			if (cnt < 2)
   2883 				break;
   2884 			optlen = cp[1];
   2885 			if (optlen < 2 || optlen > cnt)
   2886 				break;
   2887 		}
   2888 		switch (opt) {
   2889 
   2890 		default:
   2891 			continue;
   2892 
   2893 		case TCPOPT_MAXSEG:
   2894 			if (optlen != TCPOLEN_MAXSEG)
   2895 				continue;
   2896 			if (!(th->th_flags & TH_SYN))
   2897 				continue;
   2898 			if (TCPS_HAVERCVDSYN(tp->t_state))
   2899 				continue;
   2900 			bcopy(cp + 2, &mss, sizeof(mss));
   2901 			oi->maxseg = ntohs(mss);
   2902 			break;
   2903 
   2904 		case TCPOPT_WINDOW:
   2905 			if (optlen != TCPOLEN_WINDOW)
   2906 				continue;
   2907 			if (!(th->th_flags & TH_SYN))
   2908 				continue;
   2909 			if (TCPS_HAVERCVDSYN(tp->t_state))
   2910 				continue;
   2911 			tp->t_flags |= TF_RCVD_SCALE;
   2912 			tp->requested_s_scale = cp[2];
   2913 			if (tp->requested_s_scale > TCP_MAX_WINSHIFT) {
   2914 #if 0	/*XXX*/
   2915 				char *p;
   2916 
   2917 				if (ip)
   2918 					p = ntohl(ip->ip_src);
   2919 #ifdef INET6
   2920 				else if (ip6)
   2921 					p = ip6_sprintf(&ip6->ip6_src);
   2922 #endif
   2923 				else
   2924 					p = "(unknown)";
   2925 				log(LOG_ERR, "TCP: invalid wscale %d from %s, "
   2926 				    "assuming %d\n",
   2927 				    tp->requested_s_scale, p,
   2928 				    TCP_MAX_WINSHIFT);
   2929 #else
   2930 				log(LOG_ERR, "TCP: invalid wscale %d, "
   2931 				    "assuming %d\n",
   2932 				    tp->requested_s_scale,
   2933 				    TCP_MAX_WINSHIFT);
   2934 #endif
   2935 				tp->requested_s_scale = TCP_MAX_WINSHIFT;
   2936 			}
   2937 			break;
   2938 
   2939 		case TCPOPT_TIMESTAMP:
   2940 			if (optlen != TCPOLEN_TIMESTAMP)
   2941 				continue;
   2942 			oi->ts_present = 1;
   2943 			bcopy(cp + 2, &oi->ts_val, sizeof(oi->ts_val));
   2944 			NTOHL(oi->ts_val);
   2945 			bcopy(cp + 6, &oi->ts_ecr, sizeof(oi->ts_ecr));
   2946 			NTOHL(oi->ts_ecr);
   2947 
   2948 			if (!(th->th_flags & TH_SYN))
   2949 				continue;
   2950 			if (TCPS_HAVERCVDSYN(tp->t_state))
   2951 				continue;
   2952 			/*
   2953 			 * A timestamp received in a SYN makes
   2954 			 * it ok to send timestamp requests and replies.
   2955 			 */
   2956 			tp->t_flags |= TF_RCVD_TSTMP;
   2957 			tp->ts_recent = oi->ts_val;
   2958 			tp->ts_recent_age = tcp_now;
   2959                         break;
   2960 
   2961 		case TCPOPT_SACK_PERMITTED:
   2962 			if (optlen != TCPOLEN_SACK_PERMITTED)
   2963 				continue;
   2964 			if (!(th->th_flags & TH_SYN))
   2965 				continue;
   2966 			if (TCPS_HAVERCVDSYN(tp->t_state))
   2967 				continue;
   2968 			if (tcp_do_sack) {
   2969 				tp->t_flags |= TF_SACK_PERMIT;
   2970 				tp->t_flags |= TF_WILL_SACK;
   2971 			}
   2972 			break;
   2973 
   2974 		case TCPOPT_SACK:
   2975 			tcp_sack_option(tp, th, cp, optlen);
   2976 			break;
   2977 #ifdef TCP_SIGNATURE
   2978 		case TCPOPT_SIGNATURE:
   2979 			if (optlen != TCPOLEN_SIGNATURE)
   2980 				continue;
   2981 			if (sigp && bcmp(sigp, cp + 2, TCP_SIGLEN))
   2982 				return (-1);
   2983 
   2984 			sigp = sigbuf;
   2985 			memcpy(sigbuf, cp + 2, TCP_SIGLEN);
   2986 			memset(cp + 2, 0, TCP_SIGLEN);
   2987 			tp->t_flags |= TF_SIGNATURE;
   2988 			break;
   2989 #endif
   2990 		}
   2991 	}
   2992 
   2993 #ifdef TCP_SIGNATURE
   2994 	if (tp->t_flags & TF_SIGNATURE) {
   2995 
   2996 		sav = tcp_signature_getsav(m, th);
   2997 
   2998 		if (sav == NULL && tp->t_state == TCPS_LISTEN)
   2999 			return (-1);
   3000 	}
   3001 
   3002 	if ((sigp ? TF_SIGNATURE : 0) ^ (tp->t_flags & TF_SIGNATURE)) {
   3003 		if (sav == NULL)
   3004 			return (-1);
   3005 #ifdef FAST_IPSEC
   3006 		KEY_FREESAV(&sav);
   3007 #else
   3008 		key_freesav(sav);
   3009 #endif
   3010 		return (-1);
   3011 	}
   3012 
   3013 	if (sigp) {
   3014 		char sig[TCP_SIGLEN];
   3015 
   3016 		TCP_FIELDS_TO_NET(th);
   3017 		if (tcp_signature(m, th, toff, sav, sig) < 0) {
   3018 			TCP_FIELDS_TO_HOST(th);
   3019 			if (sav == NULL)
   3020 				return (-1);
   3021 #ifdef FAST_IPSEC
   3022 			KEY_FREESAV(&sav);
   3023 #else
   3024 			key_freesav(sav);
   3025 #endif
   3026 			return (-1);
   3027 		}
   3028 		TCP_FIELDS_TO_HOST(th);
   3029 
   3030 		if (bcmp(sig, sigp, TCP_SIGLEN)) {
   3031 			tcpstat.tcps_badsig++;
   3032 			if (sav == NULL)
   3033 				return (-1);
   3034 #ifdef FAST_IPSEC
   3035 			KEY_FREESAV(&sav);
   3036 #else
   3037 			key_freesav(sav);
   3038 #endif
   3039 			return (-1);
   3040 		} else
   3041 			tcpstat.tcps_goodsig++;
   3042 
   3043 		key_sa_recordxfer(sav, m);
   3044 #ifdef FAST_IPSEC
   3045 		KEY_FREESAV(&sav);
   3046 #else
   3047 		key_freesav(sav);
   3048 #endif
   3049 	}
   3050 #endif
   3051 
   3052 	return (0);
   3053 }
   3054 
   3055 /*
   3056  * Pull out of band byte out of a segment so
   3057  * it doesn't appear in the user's data queue.
   3058  * It is still reflected in the segment length for
   3059  * sequencing purposes.
   3060  */
   3061 void
   3062 tcp_pulloutofband(struct socket *so, struct tcphdr *th,
   3063     struct mbuf *m, int off)
   3064 {
   3065 	int cnt = off + th->th_urp - 1;
   3066 
   3067 	while (cnt >= 0) {
   3068 		if (m->m_len > cnt) {
   3069 			char *cp = mtod(m, char *) + cnt;
   3070 			struct tcpcb *tp = sototcpcb(so);
   3071 
   3072 			tp->t_iobc = *cp;
   3073 			tp->t_oobflags |= TCPOOB_HAVEDATA;
   3074 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
   3075 			m->m_len--;
   3076 			return;
   3077 		}
   3078 		cnt -= m->m_len;
   3079 		m = m->m_next;
   3080 		if (m == 0)
   3081 			break;
   3082 	}
   3083 	panic("tcp_pulloutofband");
   3084 }
   3085 
   3086 /*
   3087  * Collect new round-trip time estimate
   3088  * and update averages and current timeout.
   3089  */
   3090 void
   3091 tcp_xmit_timer(struct tcpcb *tp, uint32_t rtt)
   3092 {
   3093 	int32_t delta;
   3094 
   3095 	tcpstat.tcps_rttupdated++;
   3096 	if (tp->t_srtt != 0) {
   3097 		/*
   3098 		 * srtt is stored as fixed point with 3 bits after the
   3099 		 * binary point (i.e., scaled by 8).  The following magic
   3100 		 * is equivalent to the smoothing algorithm in rfc793 with
   3101 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
   3102 		 * point).  Adjust rtt to origin 0.
   3103 		 */
   3104 		delta = (rtt << 2) - (tp->t_srtt >> TCP_RTT_SHIFT);
   3105 		if ((tp->t_srtt += delta) <= 0)
   3106 			tp->t_srtt = 1 << 2;
   3107 		/*
   3108 		 * We accumulate a smoothed rtt variance (actually, a
   3109 		 * smoothed mean difference), then set the retransmit
   3110 		 * timer to smoothed rtt + 4 times the smoothed variance.
   3111 		 * rttvar is stored as fixed point with 2 bits after the
   3112 		 * binary point (scaled by 4).  The following is
   3113 		 * equivalent to rfc793 smoothing with an alpha of .75
   3114 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
   3115 		 * rfc793's wired-in beta.
   3116 		 */
   3117 		if (delta < 0)
   3118 			delta = -delta;
   3119 		delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
   3120 		if ((tp->t_rttvar += delta) <= 0)
   3121 			tp->t_rttvar = 1 << 2;
   3122 	} else {
   3123 		/*
   3124 		 * No rtt measurement yet - use the unsmoothed rtt.
   3125 		 * Set the variance to half the rtt (so our first
   3126 		 * retransmit happens at 3*rtt).
   3127 		 */
   3128 		tp->t_srtt = rtt << (TCP_RTT_SHIFT + 2);
   3129 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT + 2 - 1);
   3130 	}
   3131 	tp->t_rtttime = 0;
   3132 	tp->t_rxtshift = 0;
   3133 
   3134 	/*
   3135 	 * the retransmit should happen at rtt + 4 * rttvar.
   3136 	 * Because of the way we do the smoothing, srtt and rttvar
   3137 	 * will each average +1/2 tick of bias.  When we compute
   3138 	 * the retransmit timer, we want 1/2 tick of rounding and
   3139 	 * 1 extra tick because of +-1/2 tick uncertainty in the
   3140 	 * firing of the timer.  The bias will give us exactly the
   3141 	 * 1.5 tick we need.  But, because the bias is
   3142 	 * statistical, we have to test that we don't drop below
   3143 	 * the minimum feasible timer (which is 2 ticks).
   3144 	 */
   3145 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
   3146 	    max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
   3147 
   3148 	/*
   3149 	 * We received an ack for a packet that wasn't retransmitted;
   3150 	 * it is probably safe to discard any error indications we've
   3151 	 * received recently.  This isn't quite right, but close enough
   3152 	 * for now (a route might have failed after we sent a segment,
   3153 	 * and the return path might not be symmetrical).
   3154 	 */
   3155 	tp->t_softerror = 0;
   3156 }
   3157 
   3158 
   3159 /*
   3160  * TCP compressed state engine.  Currently used to hold compressed
   3161  * state for SYN_RECEIVED.
   3162  */
   3163 
   3164 u_long	syn_cache_count;
   3165 u_int32_t syn_hash1, syn_hash2;
   3166 
   3167 #define SYN_HASH(sa, sp, dp) \
   3168 	((((sa)->s_addr^syn_hash1)*(((((u_int32_t)(dp))<<16) + \
   3169 				     ((u_int32_t)(sp)))^syn_hash2)))
   3170 #ifndef INET6
   3171 #define	SYN_HASHALL(hash, src, dst) \
   3172 do {									\
   3173 	hash = SYN_HASH(&((const struct sockaddr_in *)(src))->sin_addr,	\
   3174 		((const struct sockaddr_in *)(src))->sin_port,		\
   3175 		((const struct sockaddr_in *)(dst))->sin_port);		\
   3176 } while (/*CONSTCOND*/ 0)
   3177 #else
   3178 #define SYN_HASH6(sa, sp, dp) \
   3179 	((((sa)->s6_addr32[0] ^ (sa)->s6_addr32[3] ^ syn_hash1) * \
   3180 	  (((((u_int32_t)(dp))<<16) + ((u_int32_t)(sp)))^syn_hash2)) \
   3181 	 & 0x7fffffff)
   3182 
   3183 #define SYN_HASHALL(hash, src, dst) \
   3184 do {									\
   3185 	switch ((src)->sa_family) {					\
   3186 	case AF_INET:							\
   3187 		hash = SYN_HASH(&((const struct sockaddr_in *)(src))->sin_addr, \
   3188 			((const struct sockaddr_in *)(src))->sin_port,	\
   3189 			((const struct sockaddr_in *)(dst))->sin_port);	\
   3190 		break;							\
   3191 	case AF_INET6:							\
   3192 		hash = SYN_HASH6(&((const struct sockaddr_in6 *)(src))->sin6_addr, \
   3193 			((const struct sockaddr_in6 *)(src))->sin6_port,	\
   3194 			((const struct sockaddr_in6 *)(dst))->sin6_port);	\
   3195 		break;							\
   3196 	default:							\
   3197 		hash = 0;						\
   3198 	}								\
   3199 } while (/*CONSTCOND*/0)
   3200 #endif /* INET6 */
   3201 
   3202 #define	SYN_CACHE_RM(sc)						\
   3203 do {									\
   3204 	TAILQ_REMOVE(&tcp_syn_cache[(sc)->sc_bucketidx].sch_bucket,	\
   3205 	    (sc), sc_bucketq);						\
   3206 	(sc)->sc_tp = NULL;						\
   3207 	LIST_REMOVE((sc), sc_tpq);					\
   3208 	tcp_syn_cache[(sc)->sc_bucketidx].sch_length--;			\
   3209 	callout_stop(&(sc)->sc_timer);					\
   3210 	syn_cache_count--;						\
   3211 } while (/*CONSTCOND*/0)
   3212 
   3213 #define	SYN_CACHE_PUT(sc)						\
   3214 do {									\
   3215 	if ((sc)->sc_ipopts)						\
   3216 		(void) m_free((sc)->sc_ipopts);				\
   3217 	rtcache_free(&(sc)->sc_route);					\
   3218 	if (callout_invoking(&(sc)->sc_timer))				\
   3219 		(sc)->sc_flags |= SCF_DEAD;				\
   3220 	else								\
   3221 		pool_put(&syn_cache_pool, (sc));			\
   3222 } while (/*CONSTCOND*/0)
   3223 
   3224 POOL_INIT(syn_cache_pool, sizeof(struct syn_cache), 0, 0, 0, "synpl", NULL,
   3225     IPL_SOFTNET);
   3226 
   3227 /*
   3228  * We don't estimate RTT with SYNs, so each packet starts with the default
   3229  * RTT and each timer step has a fixed timeout value.
   3230  */
   3231 #define	SYN_CACHE_TIMER_ARM(sc)						\
   3232 do {									\
   3233 	TCPT_RANGESET((sc)->sc_rxtcur,					\
   3234 	    TCPTV_SRTTDFLT * tcp_backoff[(sc)->sc_rxtshift], TCPTV_MIN,	\
   3235 	    TCPTV_REXMTMAX);						\
   3236 	callout_reset(&(sc)->sc_timer,					\
   3237 	    (sc)->sc_rxtcur * (hz / PR_SLOWHZ), syn_cache_timer, (sc));	\
   3238 } while (/*CONSTCOND*/0)
   3239 
   3240 #define	SYN_CACHE_TIMESTAMP(sc)	(tcp_now - (sc)->sc_timebase)
   3241 
   3242 void
   3243 syn_cache_init(void)
   3244 {
   3245 	int i;
   3246 
   3247 	/* Initialize the hash buckets. */
   3248 	for (i = 0; i < tcp_syn_cache_size; i++)
   3249 		TAILQ_INIT(&tcp_syn_cache[i].sch_bucket);
   3250 }
   3251 
   3252 void
   3253 syn_cache_insert(struct syn_cache *sc, struct tcpcb *tp)
   3254 {
   3255 	struct syn_cache_head *scp;
   3256 	struct syn_cache *sc2;
   3257 	int s;
   3258 
   3259 	/*
   3260 	 * If there are no entries in the hash table, reinitialize
   3261 	 * the hash secrets.
   3262 	 */
   3263 	if (syn_cache_count == 0) {
   3264 		syn_hash1 = arc4random();
   3265 		syn_hash2 = arc4random();
   3266 	}
   3267 
   3268 	SYN_HASHALL(sc->sc_hash, &sc->sc_src.sa, &sc->sc_dst.sa);
   3269 	sc->sc_bucketidx = sc->sc_hash % tcp_syn_cache_size;
   3270 	scp = &tcp_syn_cache[sc->sc_bucketidx];
   3271 
   3272 	/*
   3273 	 * Make sure that we don't overflow the per-bucket
   3274 	 * limit or the total cache size limit.
   3275 	 */
   3276 	s = splsoftnet();
   3277 	if (scp->sch_length >= tcp_syn_bucket_limit) {
   3278 		tcpstat.tcps_sc_bucketoverflow++;
   3279 		/*
   3280 		 * The bucket is full.  Toss the oldest element in the
   3281 		 * bucket.  This will be the first entry in the bucket.
   3282 		 */
   3283 		sc2 = TAILQ_FIRST(&scp->sch_bucket);
   3284 #ifdef DIAGNOSTIC
   3285 		/*
   3286 		 * This should never happen; we should always find an
   3287 		 * entry in our bucket.
   3288 		 */
   3289 		if (sc2 == NULL)
   3290 			panic("syn_cache_insert: bucketoverflow: impossible");
   3291 #endif
   3292 		SYN_CACHE_RM(sc2);
   3293 		SYN_CACHE_PUT(sc2);	/* calls pool_put but see spl above */
   3294 	} else if (syn_cache_count >= tcp_syn_cache_limit) {
   3295 		struct syn_cache_head *scp2, *sce;
   3296 
   3297 		tcpstat.tcps_sc_overflowed++;
   3298 		/*
   3299 		 * The cache is full.  Toss the oldest entry in the
   3300 		 * first non-empty bucket we can find.
   3301 		 *
   3302 		 * XXX We would really like to toss the oldest
   3303 		 * entry in the cache, but we hope that this
   3304 		 * condition doesn't happen very often.
   3305 		 */
   3306 		scp2 = scp;
   3307 		if (TAILQ_EMPTY(&scp2->sch_bucket)) {
   3308 			sce = &tcp_syn_cache[tcp_syn_cache_size];
   3309 			for (++scp2; scp2 != scp; scp2++) {
   3310 				if (scp2 >= sce)
   3311 					scp2 = &tcp_syn_cache[0];
   3312 				if (! TAILQ_EMPTY(&scp2->sch_bucket))
   3313 					break;
   3314 			}
   3315 #ifdef DIAGNOSTIC
   3316 			/*
   3317 			 * This should never happen; we should always find a
   3318 			 * non-empty bucket.
   3319 			 */
   3320 			if (scp2 == scp)
   3321 				panic("syn_cache_insert: cacheoverflow: "
   3322 				    "impossible");
   3323 #endif
   3324 		}
   3325 		sc2 = TAILQ_FIRST(&scp2->sch_bucket);
   3326 		SYN_CACHE_RM(sc2);
   3327 		SYN_CACHE_PUT(sc2);	/* calls pool_put but see spl above */
   3328 	}
   3329 
   3330 	/*
   3331 	 * Initialize the entry's timer.
   3332 	 */
   3333 	sc->sc_rxttot = 0;
   3334 	sc->sc_rxtshift = 0;
   3335 	SYN_CACHE_TIMER_ARM(sc);
   3336 
   3337 	/* Link it from tcpcb entry */
   3338 	LIST_INSERT_HEAD(&tp->t_sc, sc, sc_tpq);
   3339 
   3340 	/* Put it into the bucket. */
   3341 	TAILQ_INSERT_TAIL(&scp->sch_bucket, sc, sc_bucketq);
   3342 	scp->sch_length++;
   3343 	syn_cache_count++;
   3344 
   3345 	tcpstat.tcps_sc_added++;
   3346 	splx(s);
   3347 }
   3348 
   3349 /*
   3350  * Walk the timer queues, looking for SYN,ACKs that need to be retransmitted.
   3351  * If we have retransmitted an entry the maximum number of times, expire
   3352  * that entry.
   3353  */
   3354 void
   3355 syn_cache_timer(void *arg)
   3356 {
   3357 	struct syn_cache *sc = arg;
   3358 	int s;
   3359 
   3360 	s = splsoftnet();
   3361 	callout_ack(&sc->sc_timer);
   3362 
   3363 	if (__predict_false(sc->sc_flags & SCF_DEAD)) {
   3364 		tcpstat.tcps_sc_delayed_free++;
   3365 		pool_put(&syn_cache_pool, sc);
   3366 		splx(s);
   3367 		return;
   3368 	}
   3369 
   3370 	if (__predict_false(sc->sc_rxtshift == TCP_MAXRXTSHIFT)) {
   3371 		/* Drop it -- too many retransmissions. */
   3372 		goto dropit;
   3373 	}
   3374 
   3375 	/*
   3376 	 * Compute the total amount of time this entry has
   3377 	 * been on a queue.  If this entry has been on longer
   3378 	 * than the keep alive timer would allow, expire it.
   3379 	 */
   3380 	sc->sc_rxttot += sc->sc_rxtcur;
   3381 	if (sc->sc_rxttot >= TCPTV_KEEP_INIT)
   3382 		goto dropit;
   3383 
   3384 	tcpstat.tcps_sc_retransmitted++;
   3385 	(void) syn_cache_respond(sc, NULL);
   3386 
   3387 	/* Advance the timer back-off. */
   3388 	sc->sc_rxtshift++;
   3389 	SYN_CACHE_TIMER_ARM(sc);
   3390 
   3391 	splx(s);
   3392 	return;
   3393 
   3394  dropit:
   3395 	tcpstat.tcps_sc_timed_out++;
   3396 	SYN_CACHE_RM(sc);
   3397 	SYN_CACHE_PUT(sc);	/* calls pool_put but see spl above */
   3398 	splx(s);
   3399 }
   3400 
   3401 /*
   3402  * Remove syn cache created by the specified tcb entry,
   3403  * because this does not make sense to keep them
   3404  * (if there's no tcb entry, syn cache entry will never be used)
   3405  */
   3406 void
   3407 syn_cache_cleanup(struct tcpcb *tp)
   3408 {
   3409 	struct syn_cache *sc, *nsc;
   3410 	int s;
   3411 
   3412 	s = splsoftnet();
   3413 
   3414 	for (sc = LIST_FIRST(&tp->t_sc); sc != NULL; sc = nsc) {
   3415 		nsc = LIST_NEXT(sc, sc_tpq);
   3416 
   3417 #ifdef DIAGNOSTIC
   3418 		if (sc->sc_tp != tp)
   3419 			panic("invalid sc_tp in syn_cache_cleanup");
   3420 #endif
   3421 		SYN_CACHE_RM(sc);
   3422 		SYN_CACHE_PUT(sc);	/* calls pool_put but see spl above */
   3423 	}
   3424 	/* just for safety */
   3425 	LIST_INIT(&tp->t_sc);
   3426 
   3427 	splx(s);
   3428 }
   3429 
   3430 /*
   3431  * Find an entry in the syn cache.
   3432  */
   3433 struct syn_cache *
   3434 syn_cache_lookup(const struct sockaddr *src, const struct sockaddr *dst,
   3435     struct syn_cache_head **headp)
   3436 {
   3437 	struct syn_cache *sc;
   3438 	struct syn_cache_head *scp;
   3439 	u_int32_t hash;
   3440 	int s;
   3441 
   3442 	SYN_HASHALL(hash, src, dst);
   3443 
   3444 	scp = &tcp_syn_cache[hash % tcp_syn_cache_size];
   3445 	*headp = scp;
   3446 	s = splsoftnet();
   3447 	for (sc = TAILQ_FIRST(&scp->sch_bucket); sc != NULL;
   3448 	     sc = TAILQ_NEXT(sc, sc_bucketq)) {
   3449 		if (sc->sc_hash != hash)
   3450 			continue;
   3451 		if (!bcmp(&sc->sc_src, src, src->sa_len) &&
   3452 		    !bcmp(&sc->sc_dst, dst, dst->sa_len)) {
   3453 			splx(s);
   3454 			return (sc);
   3455 		}
   3456 	}
   3457 	splx(s);
   3458 	return (NULL);
   3459 }
   3460 
   3461 /*
   3462  * This function gets called when we receive an ACK for a
   3463  * socket in the LISTEN state.  We look up the connection
   3464  * in the syn cache, and if its there, we pull it out of
   3465  * the cache and turn it into a full-blown connection in
   3466  * the SYN-RECEIVED state.
   3467  *
   3468  * The return values may not be immediately obvious, and their effects
   3469  * can be subtle, so here they are:
   3470  *
   3471  *	NULL	SYN was not found in cache; caller should drop the
   3472  *		packet and send an RST.
   3473  *
   3474  *	-1	We were unable to create the new connection, and are
   3475  *		aborting it.  An ACK,RST is being sent to the peer
   3476  *		(unless we got screwey sequence numbners; see below),
   3477  *		because the 3-way handshake has been completed.  Caller
   3478  *		should not free the mbuf, since we may be using it.  If
   3479  *		we are not, we will free it.
   3480  *
   3481  *	Otherwise, the return value is a pointer to the new socket
   3482  *	associated with the connection.
   3483  */
   3484 struct socket *
   3485 syn_cache_get(struct sockaddr *src, struct sockaddr *dst,
   3486     struct tcphdr *th, unsigned int hlen, unsigned int tlen,
   3487     struct socket *so, struct mbuf *m)
   3488 {
   3489 	struct syn_cache *sc;
   3490 	struct syn_cache_head *scp;
   3491 	struct inpcb *inp = NULL;
   3492 #ifdef INET6
   3493 	struct in6pcb *in6p = NULL;
   3494 #endif
   3495 	struct tcpcb *tp = 0;
   3496 	struct mbuf *am;
   3497 	int s;
   3498 	struct socket *oso;
   3499 
   3500 	s = splsoftnet();
   3501 	if ((sc = syn_cache_lookup(src, dst, &scp)) == NULL) {
   3502 		splx(s);
   3503 		return (NULL);
   3504 	}
   3505 
   3506 	/*
   3507 	 * Verify the sequence and ack numbers.  Try getting the correct
   3508 	 * response again.
   3509 	 */
   3510 	if ((th->th_ack != sc->sc_iss + 1) ||
   3511 	    SEQ_LEQ(th->th_seq, sc->sc_irs) ||
   3512 	    SEQ_GT(th->th_seq, sc->sc_irs + 1 + sc->sc_win)) {
   3513 		(void) syn_cache_respond(sc, m);
   3514 		splx(s);
   3515 		return ((struct socket *)(-1));
   3516 	}
   3517 
   3518 	/* Remove this cache entry */
   3519 	SYN_CACHE_RM(sc);
   3520 	splx(s);
   3521 
   3522 	/*
   3523 	 * Ok, create the full blown connection, and set things up
   3524 	 * as they would have been set up if we had created the
   3525 	 * connection when the SYN arrived.  If we can't create
   3526 	 * the connection, abort it.
   3527 	 */
   3528 	/*
   3529 	 * inp still has the OLD in_pcb stuff, set the
   3530 	 * v6-related flags on the new guy, too.   This is
   3531 	 * done particularly for the case where an AF_INET6
   3532 	 * socket is bound only to a port, and a v4 connection
   3533 	 * comes in on that port.
   3534 	 * we also copy the flowinfo from the original pcb
   3535 	 * to the new one.
   3536 	 */
   3537 	oso = so;
   3538 	so = sonewconn(so, SS_ISCONNECTED);
   3539 	if (so == NULL)
   3540 		goto resetandabort;
   3541 
   3542 	switch (so->so_proto->pr_domain->dom_family) {
   3543 #ifdef INET
   3544 	case AF_INET:
   3545 		inp = sotoinpcb(so);
   3546 		break;
   3547 #endif
   3548 #ifdef INET6
   3549 	case AF_INET6:
   3550 		in6p = sotoin6pcb(so);
   3551 		break;
   3552 #endif
   3553 	}
   3554 	switch (src->sa_family) {
   3555 #ifdef INET
   3556 	case AF_INET:
   3557 		if (inp) {
   3558 			inp->inp_laddr = ((struct sockaddr_in *)dst)->sin_addr;
   3559 			inp->inp_lport = ((struct sockaddr_in *)dst)->sin_port;
   3560 			inp->inp_options = ip_srcroute();
   3561 			in_pcbstate(inp, INP_BOUND);
   3562 			if (inp->inp_options == NULL) {
   3563 				inp->inp_options = sc->sc_ipopts;
   3564 				sc->sc_ipopts = NULL;
   3565 			}
   3566 		}
   3567 #ifdef INET6
   3568 		else if (in6p) {
   3569 			/* IPv4 packet to AF_INET6 socket */
   3570 			bzero(&in6p->in6p_laddr, sizeof(in6p->in6p_laddr));
   3571 			in6p->in6p_laddr.s6_addr16[5] = htons(0xffff);
   3572 			bcopy(&((struct sockaddr_in *)dst)->sin_addr,
   3573 				&in6p->in6p_laddr.s6_addr32[3],
   3574 				sizeof(((struct sockaddr_in *)dst)->sin_addr));
   3575 			in6p->in6p_lport = ((struct sockaddr_in *)dst)->sin_port;
   3576 			in6totcpcb(in6p)->t_family = AF_INET;
   3577 			if (sotoin6pcb(oso)->in6p_flags & IN6P_IPV6_V6ONLY)
   3578 				in6p->in6p_flags |= IN6P_IPV6_V6ONLY;
   3579 			else
   3580 				in6p->in6p_flags &= ~IN6P_IPV6_V6ONLY;
   3581 			in6_pcbstate(in6p, IN6P_BOUND);
   3582 		}
   3583 #endif
   3584 		break;
   3585 #endif
   3586 #ifdef INET6
   3587 	case AF_INET6:
   3588 		if (in6p) {
   3589 			in6p->in6p_laddr = ((struct sockaddr_in6 *)dst)->sin6_addr;
   3590 			in6p->in6p_lport = ((struct sockaddr_in6 *)dst)->sin6_port;
   3591 			in6_pcbstate(in6p, IN6P_BOUND);
   3592 		}
   3593 		break;
   3594 #endif
   3595 	}
   3596 #ifdef INET6
   3597 	if (in6p && in6totcpcb(in6p)->t_family == AF_INET6 && sotoinpcb(oso)) {
   3598 		struct in6pcb *oin6p = sotoin6pcb(oso);
   3599 		/* inherit socket options from the listening socket */
   3600 		in6p->in6p_flags |= (oin6p->in6p_flags & IN6P_CONTROLOPTS);
   3601 		if (in6p->in6p_flags & IN6P_CONTROLOPTS) {
   3602 			m_freem(in6p->in6p_options);
   3603 			in6p->in6p_options = 0;
   3604 		}
   3605 		ip6_savecontrol(in6p, &in6p->in6p_options,
   3606 			mtod(m, struct ip6_hdr *), m);
   3607 	}
   3608 #endif
   3609 
   3610 #if defined(IPSEC) || defined(FAST_IPSEC)
   3611 	/*
   3612 	 * we make a copy of policy, instead of sharing the policy,
   3613 	 * for better behavior in terms of SA lookup and dead SA removal.
   3614 	 */
   3615 	if (inp) {
   3616 		/* copy old policy into new socket's */
   3617 		if (ipsec_copy_pcbpolicy(sotoinpcb(oso)->inp_sp, inp->inp_sp))
   3618 			printf("tcp_input: could not copy policy\n");
   3619 	}
   3620 #ifdef INET6
   3621 	else if (in6p) {
   3622 		/* copy old policy into new socket's */
   3623 		if (ipsec_copy_pcbpolicy(sotoin6pcb(oso)->in6p_sp,
   3624 		    in6p->in6p_sp))
   3625 			printf("tcp_input: could not copy policy\n");
   3626 	}
   3627 #endif
   3628 #endif
   3629 
   3630 	/*
   3631 	 * Give the new socket our cached route reference.
   3632 	 */
   3633 	if (inp) {
   3634 		rtcache_copy(&inp->inp_route, &sc->sc_route);
   3635 		rtcache_free(&sc->sc_route);
   3636 	}
   3637 #ifdef INET6
   3638 	else {
   3639 		rtcache_copy(&in6p->in6p_route, &sc->sc_route);
   3640 		rtcache_free(&sc->sc_route);
   3641 	}
   3642 #endif
   3643 
   3644 	am = m_get(M_DONTWAIT, MT_SONAME);	/* XXX */
   3645 	if (am == NULL)
   3646 		goto resetandabort;
   3647 	MCLAIM(am, &tcp_mowner);
   3648 	am->m_len = src->sa_len;
   3649 	bcopy(src, mtod(am, void *), src->sa_len);
   3650 	if (inp) {
   3651 		if (in_pcbconnect(inp, am, NULL)) {
   3652 			(void) m_free(am);
   3653 			goto resetandabort;
   3654 		}
   3655 	}
   3656 #ifdef INET6
   3657 	else if (in6p) {
   3658 		if (src->sa_family == AF_INET) {
   3659 			/* IPv4 packet to AF_INET6 socket */
   3660 			struct sockaddr_in6 *sin6;
   3661 			sin6 = mtod(am, struct sockaddr_in6 *);
   3662 			am->m_len = sizeof(*sin6);
   3663 			bzero(sin6, sizeof(*sin6));
   3664 			sin6->sin6_family = AF_INET6;
   3665 			sin6->sin6_len = sizeof(*sin6);
   3666 			sin6->sin6_port = ((struct sockaddr_in *)src)->sin_port;
   3667 			sin6->sin6_addr.s6_addr16[5] = htons(0xffff);
   3668 			bcopy(&((struct sockaddr_in *)src)->sin_addr,
   3669 				&sin6->sin6_addr.s6_addr32[3],
   3670 				sizeof(sin6->sin6_addr.s6_addr32[3]));
   3671 		}
   3672 		if (in6_pcbconnect(in6p, am, NULL)) {
   3673 			(void) m_free(am);
   3674 			goto resetandabort;
   3675 		}
   3676 	}
   3677 #endif
   3678 	else {
   3679 		(void) m_free(am);
   3680 		goto resetandabort;
   3681 	}
   3682 	(void) m_free(am);
   3683 
   3684 	if (inp)
   3685 		tp = intotcpcb(inp);
   3686 #ifdef INET6
   3687 	else if (in6p)
   3688 		tp = in6totcpcb(in6p);
   3689 #endif
   3690 	else
   3691 		tp = NULL;
   3692 	tp->t_flags = sototcpcb(oso)->t_flags & TF_NODELAY;
   3693 	if (sc->sc_request_r_scale != 15) {
   3694 		tp->requested_s_scale = sc->sc_requested_s_scale;
   3695 		tp->request_r_scale = sc->sc_request_r_scale;
   3696 		tp->snd_scale = sc->sc_requested_s_scale;
   3697 		tp->rcv_scale = sc->sc_request_r_scale;
   3698 		tp->t_flags |= TF_REQ_SCALE|TF_RCVD_SCALE;
   3699 	}
   3700 	if (sc->sc_flags & SCF_TIMESTAMP)
   3701 		tp->t_flags |= TF_REQ_TSTMP|TF_RCVD_TSTMP;
   3702 	tp->ts_timebase = sc->sc_timebase;
   3703 
   3704 	tp->t_template = tcp_template(tp);
   3705 	if (tp->t_template == 0) {
   3706 		tp = tcp_drop(tp, ENOBUFS);	/* destroys socket */
   3707 		so = NULL;
   3708 		m_freem(m);
   3709 		goto abort;
   3710 	}
   3711 
   3712 	tp->iss = sc->sc_iss;
   3713 	tp->irs = sc->sc_irs;
   3714 	tcp_sendseqinit(tp);
   3715 	tcp_rcvseqinit(tp);
   3716 	tp->t_state = TCPS_SYN_RECEIVED;
   3717 	TCP_TIMER_ARM(tp, TCPT_KEEP, TCPTV_KEEP_INIT);
   3718 	tcpstat.tcps_accepts++;
   3719 
   3720 	if ((sc->sc_flags & SCF_SACK_PERMIT) && tcp_do_sack)
   3721 		tp->t_flags |= TF_WILL_SACK;
   3722 
   3723 	if ((sc->sc_flags & SCF_ECN_PERMIT) && tcp_do_ecn)
   3724 		tp->t_flags |= TF_ECN_PERMIT;
   3725 
   3726 #ifdef TCP_SIGNATURE
   3727 	if (sc->sc_flags & SCF_SIGNATURE)
   3728 		tp->t_flags |= TF_SIGNATURE;
   3729 #endif
   3730 
   3731 	/* Initialize tp->t_ourmss before we deal with the peer's! */
   3732 	tp->t_ourmss = sc->sc_ourmaxseg;
   3733 	tcp_mss_from_peer(tp, sc->sc_peermaxseg);
   3734 
   3735 	/*
   3736 	 * Initialize the initial congestion window.  If we
   3737 	 * had to retransmit the SYN,ACK, we must initialize cwnd
   3738 	 * to 1 segment (i.e. the Loss Window).
   3739 	 */
   3740 	if (sc->sc_rxtshift)
   3741 		tp->snd_cwnd = tp->t_peermss;
   3742 	else {
   3743 		int ss = tcp_init_win;
   3744 #ifdef INET
   3745 		if (inp != NULL && in_localaddr(inp->inp_faddr))
   3746 			ss = tcp_init_win_local;
   3747 #endif
   3748 #ifdef INET6
   3749 		if (in6p != NULL && in6_localaddr(&in6p->in6p_faddr))
   3750 			ss = tcp_init_win_local;
   3751 #endif
   3752 		tp->snd_cwnd = TCP_INITIAL_WINDOW(ss, tp->t_peermss);
   3753 	}
   3754 
   3755 	tcp_rmx_rtt(tp);
   3756 	tp->snd_wl1 = sc->sc_irs;
   3757 	tp->rcv_up = sc->sc_irs + 1;
   3758 
   3759 	/*
   3760 	 * This is what whould have happened in tcp_output() when
   3761 	 * the SYN,ACK was sent.
   3762 	 */
   3763 	tp->snd_up = tp->snd_una;
   3764 	tp->snd_max = tp->snd_nxt = tp->iss+1;
   3765 	TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
   3766 	if (sc->sc_win > 0 && SEQ_GT(tp->rcv_nxt + sc->sc_win, tp->rcv_adv))
   3767 		tp->rcv_adv = tp->rcv_nxt + sc->sc_win;
   3768 	tp->last_ack_sent = tp->rcv_nxt;
   3769 	tp->t_partialacks = -1;
   3770 	tp->t_dupacks = 0;
   3771 
   3772 	tcpstat.tcps_sc_completed++;
   3773 	s = splsoftnet();
   3774 	SYN_CACHE_PUT(sc);
   3775 	splx(s);
   3776 	return (so);
   3777 
   3778 resetandabort:
   3779 	(void)tcp_respond(NULL, m, m, th, (tcp_seq)0, th->th_ack, TH_RST);
   3780 abort:
   3781 	if (so != NULL)
   3782 		(void) soabort(so);
   3783 	s = splsoftnet();
   3784 	SYN_CACHE_PUT(sc);
   3785 	splx(s);
   3786 	tcpstat.tcps_sc_aborted++;
   3787 	return ((struct socket *)(-1));
   3788 }
   3789 
   3790 /*
   3791  * This function is called when we get a RST for a
   3792  * non-existent connection, so that we can see if the
   3793  * connection is in the syn cache.  If it is, zap it.
   3794  */
   3795 
   3796 void
   3797 syn_cache_reset(struct sockaddr *src, struct sockaddr *dst, struct tcphdr *th)
   3798 {
   3799 	struct syn_cache *sc;
   3800 	struct syn_cache_head *scp;
   3801 	int s = splsoftnet();
   3802 
   3803 	if ((sc = syn_cache_lookup(src, dst, &scp)) == NULL) {
   3804 		splx(s);
   3805 		return;
   3806 	}
   3807 	if (SEQ_LT(th->th_seq, sc->sc_irs) ||
   3808 	    SEQ_GT(th->th_seq, sc->sc_irs+1)) {
   3809 		splx(s);
   3810 		return;
   3811 	}
   3812 	SYN_CACHE_RM(sc);
   3813 	tcpstat.tcps_sc_reset++;
   3814 	SYN_CACHE_PUT(sc);	/* calls pool_put but see spl above */
   3815 	splx(s);
   3816 }
   3817 
   3818 void
   3819 syn_cache_unreach(const struct sockaddr *src, const struct sockaddr *dst,
   3820     struct tcphdr *th)
   3821 {
   3822 	struct syn_cache *sc;
   3823 	struct syn_cache_head *scp;
   3824 	int s;
   3825 
   3826 	s = splsoftnet();
   3827 	if ((sc = syn_cache_lookup(src, dst, &scp)) == NULL) {
   3828 		splx(s);
   3829 		return;
   3830 	}
   3831 	/* If the sequence number != sc_iss, then it's a bogus ICMP msg */
   3832 	if (ntohl (th->th_seq) != sc->sc_iss) {
   3833 		splx(s);
   3834 		return;
   3835 	}
   3836 
   3837 	/*
   3838 	 * If we've retransmitted 3 times and this is our second error,
   3839 	 * we remove the entry.  Otherwise, we allow it to continue on.
   3840 	 * This prevents us from incorrectly nuking an entry during a
   3841 	 * spurious network outage.
   3842 	 *
   3843 	 * See tcp_notify().
   3844 	 */
   3845 	if ((sc->sc_flags & SCF_UNREACH) == 0 || sc->sc_rxtshift < 3) {
   3846 		sc->sc_flags |= SCF_UNREACH;
   3847 		splx(s);
   3848 		return;
   3849 	}
   3850 
   3851 	SYN_CACHE_RM(sc);
   3852 	tcpstat.tcps_sc_unreach++;
   3853 	SYN_CACHE_PUT(sc);	/* calls pool_put but see spl above */
   3854 	splx(s);
   3855 }
   3856 
   3857 /*
   3858  * Given a LISTEN socket and an inbound SYN request, add
   3859  * this to the syn cache, and send back a segment:
   3860  *	<SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
   3861  * to the source.
   3862  *
   3863  * IMPORTANT NOTE: We do _NOT_ ACK data that might accompany the SYN.
   3864  * Doing so would require that we hold onto the data and deliver it
   3865  * to the application.  However, if we are the target of a SYN-flood
   3866  * DoS attack, an attacker could send data which would eventually
   3867  * consume all available buffer space if it were ACKed.  By not ACKing
   3868  * the data, we avoid this DoS scenario.
   3869  */
   3870 
   3871 int
   3872 syn_cache_add(struct sockaddr *src, struct sockaddr *dst, struct tcphdr *th,
   3873     unsigned int hlen, struct socket *so, struct mbuf *m, u_char *optp,
   3874     int optlen, struct tcp_opt_info *oi)
   3875 {
   3876 	struct tcpcb tb, *tp;
   3877 	long win;
   3878 	struct syn_cache *sc;
   3879 	struct syn_cache_head *scp;
   3880 	struct mbuf *ipopts;
   3881 	struct tcp_opt_info opti;
   3882 	int s;
   3883 
   3884 	tp = sototcpcb(so);
   3885 
   3886 	bzero(&opti, sizeof(opti));
   3887 
   3888 	/*
   3889 	 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
   3890 	 *
   3891 	 * Note this check is performed in tcp_input() very early on.
   3892 	 */
   3893 
   3894 	/*
   3895 	 * Initialize some local state.
   3896 	 */
   3897 	win = sbspace(&so->so_rcv);
   3898 	if (win > TCP_MAXWIN)
   3899 		win = TCP_MAXWIN;
   3900 
   3901 	switch (src->sa_family) {
   3902 #ifdef INET
   3903 	case AF_INET:
   3904 		/*
   3905 		 * Remember the IP options, if any.
   3906 		 */
   3907 		ipopts = ip_srcroute();
   3908 		break;
   3909 #endif
   3910 	default:
   3911 		ipopts = NULL;
   3912 	}
   3913 
   3914 #ifdef TCP_SIGNATURE
   3915 	if (optp || (tp->t_flags & TF_SIGNATURE))
   3916 #else
   3917 	if (optp)
   3918 #endif
   3919 	{
   3920 		tb.t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0;
   3921 #ifdef TCP_SIGNATURE
   3922 		tb.t_flags |= (tp->t_flags & TF_SIGNATURE);
   3923 #endif
   3924 		tb.t_state = TCPS_LISTEN;
   3925 		if (tcp_dooptions(&tb, optp, optlen, th, m, m->m_pkthdr.len -
   3926 		    sizeof(struct tcphdr) - optlen - hlen, oi) < 0)
   3927 			return (0);
   3928 	} else
   3929 		tb.t_flags = 0;
   3930 
   3931 	/*
   3932 	 * See if we already have an entry for this connection.
   3933 	 * If we do, resend the SYN,ACK.  We do not count this
   3934 	 * as a retransmission (XXX though maybe we should).
   3935 	 */
   3936 	if ((sc = syn_cache_lookup(src, dst, &scp)) != NULL) {
   3937 		tcpstat.tcps_sc_dupesyn++;
   3938 		if (ipopts) {
   3939 			/*
   3940 			 * If we were remembering a previous source route,
   3941 			 * forget it and use the new one we've been given.
   3942 			 */
   3943 			if (sc->sc_ipopts)
   3944 				(void) m_free(sc->sc_ipopts);
   3945 			sc->sc_ipopts = ipopts;
   3946 		}
   3947 		sc->sc_timestamp = tb.ts_recent;
   3948 		if (syn_cache_respond(sc, m) == 0) {
   3949 			tcpstat.tcps_sndacks++;
   3950 			tcpstat.tcps_sndtotal++;
   3951 		}
   3952 		return (1);
   3953 	}
   3954 
   3955 	s = splsoftnet();
   3956 	sc = pool_get(&syn_cache_pool, PR_NOWAIT);
   3957 	splx(s);
   3958 	if (sc == NULL) {
   3959 		if (ipopts)
   3960 			(void) m_free(ipopts);
   3961 		return (0);
   3962 	}
   3963 
   3964 	/*
   3965 	 * Fill in the cache, and put the necessary IP and TCP
   3966 	 * options into the reply.
   3967 	 */
   3968 	bzero(sc, sizeof(struct syn_cache));
   3969 	callout_init(&sc->sc_timer);
   3970 	bcopy(src, &sc->sc_src, src->sa_len);
   3971 	bcopy(dst, &sc->sc_dst, dst->sa_len);
   3972 	sc->sc_flags = 0;
   3973 	sc->sc_ipopts = ipopts;
   3974 	sc->sc_irs = th->th_seq;
   3975 	switch (src->sa_family) {
   3976 #ifdef INET
   3977 	case AF_INET:
   3978 	    {
   3979 		struct sockaddr_in *srcin = (void *) src;
   3980 		struct sockaddr_in *dstin = (void *) dst;
   3981 
   3982 		sc->sc_iss = tcp_new_iss1(&dstin->sin_addr,
   3983 		    &srcin->sin_addr, dstin->sin_port,
   3984 		    srcin->sin_port, sizeof(dstin->sin_addr), 0);
   3985 		break;
   3986 	    }
   3987 #endif /* INET */
   3988 #ifdef INET6
   3989 	case AF_INET6:
   3990 	    {
   3991 		struct sockaddr_in6 *srcin6 = (void *) src;
   3992 		struct sockaddr_in6 *dstin6 = (void *) dst;
   3993 
   3994 		sc->sc_iss = tcp_new_iss1(&dstin6->sin6_addr,
   3995 		    &srcin6->sin6_addr, dstin6->sin6_port,
   3996 		    srcin6->sin6_port, sizeof(dstin6->sin6_addr), 0);
   3997 		break;
   3998 	    }
   3999 #endif /* INET6 */
   4000 	}
   4001 	sc->sc_peermaxseg = oi->maxseg;
   4002 	sc->sc_ourmaxseg = tcp_mss_to_advertise(m->m_flags & M_PKTHDR ?
   4003 						m->m_pkthdr.rcvif : NULL,
   4004 						sc->sc_src.sa.sa_family);
   4005 	sc->sc_win = win;
   4006 	sc->sc_timebase = tcp_now;	/* see tcp_newtcpcb() */
   4007 	sc->sc_timestamp = tb.ts_recent;
   4008 	if ((tb.t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP)) ==
   4009 	    (TF_REQ_TSTMP|TF_RCVD_TSTMP))
   4010 		sc->sc_flags |= SCF_TIMESTAMP;
   4011 	if ((tb.t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
   4012 	    (TF_RCVD_SCALE|TF_REQ_SCALE)) {
   4013 		sc->sc_requested_s_scale = tb.requested_s_scale;
   4014 		sc->sc_request_r_scale = 0;
   4015 		while (sc->sc_request_r_scale < TCP_MAX_WINSHIFT &&
   4016 		    TCP_MAXWIN << sc->sc_request_r_scale <
   4017 		    so->so_rcv.sb_hiwat)
   4018 			sc->sc_request_r_scale++;
   4019 	} else {
   4020 		sc->sc_requested_s_scale = 15;
   4021 		sc->sc_request_r_scale = 15;
   4022 	}
   4023 	if ((tb.t_flags & TF_SACK_PERMIT) && tcp_do_sack)
   4024 		sc->sc_flags |= SCF_SACK_PERMIT;
   4025 
   4026 	/*
   4027 	 * ECN setup packet recieved.
   4028 	 */
   4029 	if ((th->th_flags & (TH_ECE|TH_CWR)) && tcp_do_ecn)
   4030 		sc->sc_flags |= SCF_ECN_PERMIT;
   4031 
   4032 #ifdef TCP_SIGNATURE
   4033 	if (tb.t_flags & TF_SIGNATURE)
   4034 		sc->sc_flags |= SCF_SIGNATURE;
   4035 #endif
   4036 	sc->sc_tp = tp;
   4037 	if (syn_cache_respond(sc, m) == 0) {
   4038 		syn_cache_insert(sc, tp);
   4039 		tcpstat.tcps_sndacks++;
   4040 		tcpstat.tcps_sndtotal++;
   4041 	} else {
   4042 		s = splsoftnet();
   4043 		SYN_CACHE_PUT(sc);
   4044 		splx(s);
   4045 		tcpstat.tcps_sc_dropped++;
   4046 	}
   4047 	return (1);
   4048 }
   4049 
   4050 int
   4051 syn_cache_respond(struct syn_cache *sc, struct mbuf *m)
   4052 {
   4053 	struct route *ro;
   4054 	u_int8_t *optp;
   4055 	int optlen, error;
   4056 	u_int16_t tlen;
   4057 	struct ip *ip = NULL;
   4058 #ifdef INET6
   4059 	struct ip6_hdr *ip6 = NULL;
   4060 #endif
   4061 	struct tcpcb *tp = NULL;
   4062 	struct tcphdr *th;
   4063 	u_int hlen;
   4064 	struct socket *so;
   4065 
   4066 	ro = &sc->sc_route;
   4067 	switch (sc->sc_src.sa.sa_family) {
   4068 	case AF_INET:
   4069 		hlen = sizeof(struct ip);
   4070 		break;
   4071 #ifdef INET6
   4072 	case AF_INET6:
   4073 		hlen = sizeof(struct ip6_hdr);
   4074 		break;
   4075 #endif
   4076 	default:
   4077 		if (m)
   4078 			m_freem(m);
   4079 		return (EAFNOSUPPORT);
   4080 	}
   4081 
   4082 	/* Compute the size of the TCP options. */
   4083 	optlen = 4 + (sc->sc_request_r_scale != 15 ? 4 : 0) +
   4084 	    ((sc->sc_flags & SCF_SACK_PERMIT) ? (TCPOLEN_SACK_PERMITTED + 2) : 0) +
   4085 #ifdef TCP_SIGNATURE
   4086 	    ((sc->sc_flags & SCF_SIGNATURE) ? (TCPOLEN_SIGNATURE + 2) : 0) +
   4087 #endif
   4088 	    ((sc->sc_flags & SCF_TIMESTAMP) ? TCPOLEN_TSTAMP_APPA : 0);
   4089 
   4090 	tlen = hlen + sizeof(struct tcphdr) + optlen;
   4091 
   4092 	/*
   4093 	 * Create the IP+TCP header from scratch.
   4094 	 */
   4095 	if (m)
   4096 		m_freem(m);
   4097 #ifdef DIAGNOSTIC
   4098 	if (max_linkhdr + tlen > MCLBYTES)
   4099 		return (ENOBUFS);
   4100 #endif
   4101 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   4102 	if (m && tlen > MHLEN) {
   4103 		MCLGET(m, M_DONTWAIT);
   4104 		if ((m->m_flags & M_EXT) == 0) {
   4105 			m_freem(m);
   4106 			m = NULL;
   4107 		}
   4108 	}
   4109 	if (m == NULL)
   4110 		return (ENOBUFS);
   4111 	MCLAIM(m, &tcp_tx_mowner);
   4112 
   4113 	/* Fixup the mbuf. */
   4114 	m->m_data += max_linkhdr;
   4115 	m->m_len = m->m_pkthdr.len = tlen;
   4116 	if (sc->sc_tp) {
   4117 		tp = sc->sc_tp;
   4118 		if (tp->t_inpcb)
   4119 			so = tp->t_inpcb->inp_socket;
   4120 #ifdef INET6
   4121 		else if (tp->t_in6pcb)
   4122 			so = tp->t_in6pcb->in6p_socket;
   4123 #endif
   4124 		else
   4125 			so = NULL;
   4126 	} else
   4127 		so = NULL;
   4128 	m->m_pkthdr.rcvif = NULL;
   4129 	memset(mtod(m, u_char *), 0, tlen);
   4130 
   4131 	switch (sc->sc_src.sa.sa_family) {
   4132 	case AF_INET:
   4133 		ip = mtod(m, struct ip *);
   4134 		ip->ip_v = 4;
   4135 		ip->ip_dst = sc->sc_src.sin.sin_addr;
   4136 		ip->ip_src = sc->sc_dst.sin.sin_addr;
   4137 		ip->ip_p = IPPROTO_TCP;
   4138 		th = (struct tcphdr *)(ip + 1);
   4139 		th->th_dport = sc->sc_src.sin.sin_port;
   4140 		th->th_sport = sc->sc_dst.sin.sin_port;
   4141 		break;
   4142 #ifdef INET6
   4143 	case AF_INET6:
   4144 		ip6 = mtod(m, struct ip6_hdr *);
   4145 		ip6->ip6_vfc = IPV6_VERSION;
   4146 		ip6->ip6_dst = sc->sc_src.sin6.sin6_addr;
   4147 		ip6->ip6_src = sc->sc_dst.sin6.sin6_addr;
   4148 		ip6->ip6_nxt = IPPROTO_TCP;
   4149 		/* ip6_plen will be updated in ip6_output() */
   4150 		th = (struct tcphdr *)(ip6 + 1);
   4151 		th->th_dport = sc->sc_src.sin6.sin6_port;
   4152 		th->th_sport = sc->sc_dst.sin6.sin6_port;
   4153 		break;
   4154 #endif
   4155 	default:
   4156 		th = NULL;
   4157 	}
   4158 
   4159 	th->th_seq = htonl(sc->sc_iss);
   4160 	th->th_ack = htonl(sc->sc_irs + 1);
   4161 	th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
   4162 	th->th_flags = TH_SYN|TH_ACK;
   4163 	th->th_win = htons(sc->sc_win);
   4164 	/* th_sum already 0 */
   4165 	/* th_urp already 0 */
   4166 
   4167 	/* Tack on the TCP options. */
   4168 	optp = (u_int8_t *)(th + 1);
   4169 	*optp++ = TCPOPT_MAXSEG;
   4170 	*optp++ = 4;
   4171 	*optp++ = (sc->sc_ourmaxseg >> 8) & 0xff;
   4172 	*optp++ = sc->sc_ourmaxseg & 0xff;
   4173 
   4174 	if (sc->sc_request_r_scale != 15) {
   4175 		*((u_int32_t *)optp) = htonl(TCPOPT_NOP << 24 |
   4176 		    TCPOPT_WINDOW << 16 | TCPOLEN_WINDOW << 8 |
   4177 		    sc->sc_request_r_scale);
   4178 		optp += 4;
   4179 	}
   4180 
   4181 	if (sc->sc_flags & SCF_TIMESTAMP) {
   4182 		u_int32_t *lp = (u_int32_t *)(optp);
   4183 		/* Form timestamp option as shown in appendix A of RFC 1323. */
   4184 		*lp++ = htonl(TCPOPT_TSTAMP_HDR);
   4185 		*lp++ = htonl(SYN_CACHE_TIMESTAMP(sc));
   4186 		*lp   = htonl(sc->sc_timestamp);
   4187 		optp += TCPOLEN_TSTAMP_APPA;
   4188 	}
   4189 
   4190 	if (sc->sc_flags & SCF_SACK_PERMIT) {
   4191 		u_int8_t *p = optp;
   4192 
   4193 		/* Let the peer know that we will SACK. */
   4194 		p[0] = TCPOPT_SACK_PERMITTED;
   4195 		p[1] = 2;
   4196 		p[2] = TCPOPT_NOP;
   4197 		p[3] = TCPOPT_NOP;
   4198 		optp += 4;
   4199 	}
   4200 
   4201 	/*
   4202 	 * Send ECN SYN-ACK setup packet.
   4203 	 * Routes can be asymetric, so, even if we receive a packet
   4204 	 * with ECE and CWR set, we must not assume no one will block
   4205 	 * the ECE packet we are about to send.
   4206 	 */
   4207 	if ((sc->sc_flags & SCF_ECN_PERMIT) && tp &&
   4208 	    SEQ_GEQ(tp->snd_nxt, tp->snd_max)) {
   4209 		th->th_flags |= TH_ECE;
   4210 		tcpstat.tcps_ecn_shs++;
   4211 
   4212 		/*
   4213 		 * draft-ietf-tcpm-ecnsyn-00.txt
   4214 		 *
   4215 		 * "[...] a TCP node MAY respond to an ECN-setup
   4216 		 * SYN packet by setting ECT in the responding
   4217 		 * ECN-setup SYN/ACK packet, indicating to routers
   4218 		 * that the SYN/ACK packet is ECN-Capable.
   4219 		 * This allows a congested router along the path
   4220 		 * to mark the packet instead of dropping the
   4221 		 * packet as an indication of congestion."
   4222 		 *
   4223 		 * "[...] There can be a great benefit in setting
   4224 		 * an ECN-capable codepoint in SYN/ACK packets [...]
   4225 		 * Congestion is  most likely to occur in
   4226 		 * the server-to-client direction.  As a result,
   4227 		 * setting an ECN-capable codepoint in SYN/ACK
   4228 		 * packets can reduce the occurence of three-second
   4229 		 * retransmit timeouts resulting from the drop
   4230 		 * of SYN/ACK packets."
   4231 		 *
   4232 		 * Page 4 and 6, January 2006.
   4233 		 */
   4234 
   4235 		switch (sc->sc_src.sa.sa_family) {
   4236 #ifdef INET
   4237 		case AF_INET:
   4238 			ip->ip_tos |= IPTOS_ECN_ECT0;
   4239 			break;
   4240 #endif
   4241 #ifdef INET6
   4242 		case AF_INET6:
   4243 			ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20);
   4244 			break;
   4245 #endif
   4246 		}
   4247 		tcpstat.tcps_ecn_ect++;
   4248 	}
   4249 
   4250 #ifdef TCP_SIGNATURE
   4251 	if (sc->sc_flags & SCF_SIGNATURE) {
   4252 		struct secasvar *sav;
   4253 		u_int8_t *sigp;
   4254 
   4255 		sav = tcp_signature_getsav(m, th);
   4256 
   4257 		if (sav == NULL) {
   4258 			if (m)
   4259 				m_freem(m);
   4260 			return (EPERM);
   4261 		}
   4262 
   4263 		*optp++ = TCPOPT_SIGNATURE;
   4264 		*optp++ = TCPOLEN_SIGNATURE;
   4265 		sigp = optp;
   4266 		bzero(optp, TCP_SIGLEN);
   4267 		optp += TCP_SIGLEN;
   4268 		*optp++ = TCPOPT_NOP;
   4269 		*optp++ = TCPOPT_EOL;
   4270 
   4271 		(void)tcp_signature(m, th, hlen, sav, sigp);
   4272 
   4273 		key_sa_recordxfer(sav, m);
   4274 #ifdef FAST_IPSEC
   4275 		KEY_FREESAV(&sav);
   4276 #else
   4277 		key_freesav(sav);
   4278 #endif
   4279 	}
   4280 #endif
   4281 
   4282 	/* Compute the packet's checksum. */
   4283 	switch (sc->sc_src.sa.sa_family) {
   4284 	case AF_INET:
   4285 		ip->ip_len = htons(tlen - hlen);
   4286 		th->th_sum = 0;
   4287 		th->th_sum = in4_cksum(m, IPPROTO_TCP, hlen, tlen - hlen);
   4288 		break;
   4289 #ifdef INET6
   4290 	case AF_INET6:
   4291 		ip6->ip6_plen = htons(tlen - hlen);
   4292 		th->th_sum = 0;
   4293 		th->th_sum = in6_cksum(m, IPPROTO_TCP, hlen, tlen - hlen);
   4294 		break;
   4295 #endif
   4296 	}
   4297 
   4298 	/*
   4299 	 * Fill in some straggling IP bits.  Note the stack expects
   4300 	 * ip_len to be in host order, for convenience.
   4301 	 */
   4302 	switch (sc->sc_src.sa.sa_family) {
   4303 #ifdef INET
   4304 	case AF_INET:
   4305 		ip->ip_len = htons(tlen);
   4306 		ip->ip_ttl = ip_defttl;
   4307 		/* XXX tos? */
   4308 		break;
   4309 #endif
   4310 #ifdef INET6
   4311 	case AF_INET6:
   4312 		ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
   4313 		ip6->ip6_vfc |= IPV6_VERSION;
   4314 		ip6->ip6_plen = htons(tlen - hlen);
   4315 		/* ip6_hlim will be initialized afterwards */
   4316 		/* XXX flowlabel? */
   4317 		break;
   4318 #endif
   4319 	}
   4320 
   4321 	/* XXX use IPsec policy on listening socket, on SYN ACK */
   4322 	tp = sc->sc_tp;
   4323 
   4324 	switch (sc->sc_src.sa.sa_family) {
   4325 #ifdef INET
   4326 	case AF_INET:
   4327 		error = ip_output(m, sc->sc_ipopts, ro,
   4328 		    (ip_mtudisc ? IP_MTUDISC : 0),
   4329 		    (struct ip_moptions *)NULL, so);
   4330 		break;
   4331 #endif
   4332 #ifdef INET6
   4333 	case AF_INET6:
   4334 		ip6->ip6_hlim = in6_selecthlim(NULL,
   4335 				ro->ro_rt ? ro->ro_rt->rt_ifp : NULL);
   4336 
   4337 		error = ip6_output(m, NULL /*XXX*/, ro, 0, NULL, so, NULL);
   4338 		break;
   4339 #endif
   4340 	default:
   4341 		error = EAFNOSUPPORT;
   4342 		break;
   4343 	}
   4344 	return (error);
   4345 }
   4346