Home | History | Annotate | Line # | Download | only in netinet
tcp_input.c revision 1.65
      1 /*	$NetBSD: tcp_input.c,v 1.65 1998/09/10 10:46:59 mouse Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
      9  * Facility, NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
     42  *	The Regents of the University of California.  All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. All advertising materials mentioning features or use of this software
     53  *    must display the following acknowledgement:
     54  *	This product includes software developed by the University of
     55  *	California, Berkeley and its contributors.
     56  * 4. Neither the name of the University nor the names of its contributors
     57  *    may be used to endorse or promote products derived from this software
     58  *    without specific prior written permission.
     59  *
     60  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70  * SUCH DAMAGE.
     71  *
     72  *	@(#)tcp_input.c	8.12 (Berkeley) 5/24/95
     73  */
     74 
     75 /*
     76  *	TODO list for SYN cache stuff:
     77  *
     78  *	Find room for a "state" field, which is needed to keep a
     79  *	compressed state for TIME_WAIT TCBs.  It's been noted already
     80  *	that this is fairly important for very high-volume web and
     81  *	mail servers, which use a large number of short-lived
     82  *	connections.
     83  */
     84 
     85 #include <sys/param.h>
     86 #include <sys/systm.h>
     87 #include <sys/malloc.h>
     88 #include <sys/mbuf.h>
     89 #include <sys/protosw.h>
     90 #include <sys/socket.h>
     91 #include <sys/socketvar.h>
     92 #include <sys/errno.h>
     93 #include <sys/syslog.h>
     94 #include <sys/pool.h>
     95 
     96 #include <net/if.h>
     97 #include <net/route.h>
     98 
     99 #include <netinet/in.h>
    100 #include <netinet/in_systm.h>
    101 #include <netinet/ip.h>
    102 #include <netinet/in_pcb.h>
    103 #include <netinet/ip_var.h>
    104 #include <netinet/tcp.h>
    105 #include <netinet/tcp_fsm.h>
    106 #include <netinet/tcp_seq.h>
    107 #include <netinet/tcp_timer.h>
    108 #include <netinet/tcp_var.h>
    109 #include <netinet/tcpip.h>
    110 #include <netinet/tcp_debug.h>
    111 
    112 #include <machine/stdarg.h>
    113 
    114 int	tcprexmtthresh = 3;
    115 struct	tcpiphdr tcp_saveti;
    116 
    117 extern u_long sb_max;
    118 
    119 #define TCP_PAWS_IDLE	(24 * 24 * 60 * 60 * PR_SLOWHZ)
    120 
    121 /* for modulo comparisons of timestamps */
    122 #define TSTMP_LT(a,b)	((int)((a)-(b)) < 0)
    123 #define TSTMP_GEQ(a,b)	((int)((a)-(b)) >= 0)
    124 
    125 /*
    126  * Macro to compute ACK transmission behavior.  Delay the ACK unless
    127  * we have already delayed an ACK (must send an ACK every two segments).
    128  * We also ACK immediately if we received a PUSH and the ACK-on-PUSH
    129  * option is enabled.
    130  */
    131 #define	TCP_SETUP_ACK(tp, ti) \
    132 do { \
    133 	if ((tp)->t_flags & TF_DELACK || \
    134 	    (tcp_ack_on_push && (ti)->ti_flags & TH_PUSH)) \
    135 		tp->t_flags |= TF_ACKNOW; \
    136 	else \
    137 		TCP_SET_DELACK(tp); \
    138 } while (0)
    139 
    140 /*
    141  * Insert segment ti into reassembly queue of tcp with
    142  * control block tp.  Return TH_FIN if reassembly now includes
    143  * a segment with FIN.  The macro form does the common case inline
    144  * (segment is the next to be received on an established connection,
    145  * and the queue is empty), avoiding linkage into and removal
    146  * from the queue and repetition of various conversions.
    147  * Set DELACK for segments received in order, but ack immediately
    148  * when segments are out of order (so fast retransmit can work).
    149  */
    150 #define	TCP_REASS(tp, ti, m, so, flags) { \
    151 	if ((ti)->ti_seq == (tp)->rcv_nxt && \
    152 	    (tp)->segq.lh_first == NULL && \
    153 	    (tp)->t_state == TCPS_ESTABLISHED) { \
    154 		TCP_SETUP_ACK(tp, ti); \
    155 		(tp)->rcv_nxt += (ti)->ti_len; \
    156 		flags = (ti)->ti_flags & TH_FIN; \
    157 		tcpstat.tcps_rcvpack++;\
    158 		tcpstat.tcps_rcvbyte += (ti)->ti_len;\
    159 		sbappend(&(so)->so_rcv, (m)); \
    160 		sorwakeup(so); \
    161 	} else { \
    162 		(flags) = tcp_reass((tp), (ti), (m)); \
    163 		tp->t_flags |= TF_ACKNOW; \
    164 	} \
    165 }
    166 
    167 int
    168 tcp_reass(tp, ti, m)
    169 	register struct tcpcb *tp;
    170 	register struct tcpiphdr *ti;
    171 	struct mbuf *m;
    172 {
    173 	register struct ipqent *p, *q, *nq, *tiqe = NULL;
    174 	struct socket *so = tp->t_inpcb->inp_socket;
    175 	int pkt_flags;
    176 	tcp_seq pkt_seq;
    177 	unsigned pkt_len;
    178 	u_long rcvpartdupbyte = 0;
    179 	u_long rcvoobyte;
    180 
    181 	/*
    182 	 * Call with ti==0 after become established to
    183 	 * force pre-ESTABLISHED data up to user socket.
    184 	 */
    185 	if (ti == 0)
    186 		goto present;
    187 
    188 	rcvoobyte = ti->ti_len;
    189 	/*
    190 	 * Copy these to local variables because the tcpiphdr
    191 	 * gets munged while we are collapsing mbufs.
    192 	 */
    193 	pkt_seq = ti->ti_seq;
    194 	pkt_len = ti->ti_len;
    195 	pkt_flags = ti->ti_flags;
    196 	/*
    197 	 * Find a segment which begins after this one does.
    198 	 */
    199 	for (p = NULL, q = tp->segq.lh_first; q != NULL; q = nq) {
    200 		nq = q->ipqe_q.le_next;
    201 		/*
    202 		 * If the received segment is just right after this
    203 		 * fragment, merge the two together and then check
    204 		 * for further overlaps.
    205 		 */
    206 		if (q->ipqe_seq + q->ipqe_len == pkt_seq) {
    207 #ifdef TCPREASS_DEBUG
    208 			printf("tcp_reass[%p]: concat %u:%u(%u) to %u:%u(%u)\n",
    209 			       tp, pkt_seq, pkt_seq + pkt_len, pkt_len,
    210 			       q->ipqe_seq, q->ipqe_seq + q->ipqe_len, q->ipqe_len);
    211 #endif
    212 			pkt_len += q->ipqe_len;
    213 			pkt_flags |= q->ipqe_flags;
    214 			pkt_seq = q->ipqe_seq;
    215 			m_cat(q->ipqe_m, m);
    216 			m = q->ipqe_m;
    217 			goto free_ipqe;
    218 		}
    219 		/*
    220 		 * If the received segment is completely past this
    221 		 * fragment, we need to go the next fragment.
    222 		 */
    223 		if (SEQ_LT(q->ipqe_seq + q->ipqe_len, pkt_seq)) {
    224 			p = q;
    225 			continue;
    226 		}
    227 		/*
    228 		 * If the fragment is past the received segment,
    229 		 * it (or any following) can't be concatenated.
    230 		 */
    231 		if (SEQ_GT(q->ipqe_seq, pkt_seq + pkt_len))
    232 			break;
    233 		/*
    234 		 * We've received all the data in this segment before.
    235 		 * mark it as a duplicate and return.
    236 		 */
    237 		if (SEQ_LEQ(q->ipqe_seq, pkt_seq) &&
    238 		    SEQ_GEQ(q->ipqe_seq + q->ipqe_len, pkt_seq + pkt_len)) {
    239 			tcpstat.tcps_rcvduppack++;
    240 			tcpstat.tcps_rcvdupbyte += pkt_len;
    241 			m_freem(m);
    242 			if (tiqe != NULL)
    243 				FREE(tiqe, M_IPQ);
    244 			return (0);
    245 		}
    246 		/*
    247 		 * Received segment completely overlaps this fragment
    248 		 * so we drop the fragment (this keeps the temporal
    249 		 * ordering of segments correct).
    250 		 */
    251 		if (SEQ_GEQ(q->ipqe_seq, pkt_seq) &&
    252 		    SEQ_LEQ(q->ipqe_seq + q->ipqe_len, pkt_seq + pkt_len)) {
    253 			rcvpartdupbyte += q->ipqe_len;
    254 			m_freem(q->ipqe_m);
    255 			goto free_ipqe;
    256 		}
    257 		/*
    258 		 * RX'ed segment extends past the end of the
    259 		 * fragment.  Drop the overlapping bytes.  Then
    260 		 * merge the fragment and segment then treat as
    261 		 * a longer received packet.
    262 		 */
    263 		if (SEQ_LT(q->ipqe_seq, pkt_seq)
    264 		    && SEQ_GT(q->ipqe_seq + q->ipqe_len, pkt_seq))  {
    265 			int overlap = q->ipqe_seq + q->ipqe_len - pkt_seq;
    266 #ifdef TCPREASS_DEBUG
    267 			printf("tcp_reass[%p]: trim starting %d bytes of %u:%u(%u)\n",
    268 			       tp, overlap,
    269 			       pkt_seq, pkt_seq + pkt_len, pkt_len);
    270 #endif
    271 			m_adj(m, overlap);
    272 			rcvpartdupbyte += overlap;
    273 			m_cat(q->ipqe_m, m);
    274 			m = q->ipqe_m;
    275 			pkt_seq = q->ipqe_seq;
    276 			pkt_len += q->ipqe_len - overlap;
    277 			rcvoobyte -= overlap;
    278 			goto free_ipqe;
    279 		}
    280 		/*
    281 		 * RX'ed segment extends past the front of the
    282 		 * fragment.  Drop the overlapping bytes on the
    283 		 * received packet.  The packet will then be
    284 		 * contatentated with this fragment a bit later.
    285 		 */
    286 		if (SEQ_GT(q->ipqe_seq, pkt_seq)
    287 		    && SEQ_LT(q->ipqe_seq, pkt_seq + pkt_len))  {
    288 			int overlap = pkt_seq + pkt_len - q->ipqe_seq;
    289 #ifdef TCPREASS_DEBUG
    290 			printf("tcp_reass[%p]: trim trailing %d bytes of %u:%u(%u)\n",
    291 			       tp, overlap,
    292 			       pkt_seq, pkt_seq + pkt_len, pkt_len);
    293 #endif
    294 			m_adj(m, -overlap);
    295 			pkt_len -= overlap;
    296 			rcvpartdupbyte += overlap;
    297 			rcvoobyte -= overlap;
    298 		}
    299 		/*
    300 		 * If the received segment immediates precedes this
    301 		 * fragment then tack the fragment onto this segment
    302 		 * and reinsert the data.
    303 		 */
    304 		if (q->ipqe_seq == pkt_seq + pkt_len) {
    305 #ifdef TCPREASS_DEBUG
    306 			printf("tcp_reass[%p]: append %u:%u(%u) to %u:%u(%u)\n",
    307 			       tp, q->ipqe_seq, q->ipqe_seq + q->ipqe_len, q->ipqe_len,
    308 			       pkt_seq, pkt_seq + pkt_len, pkt_len);
    309 #endif
    310 			pkt_len += q->ipqe_len;
    311 			pkt_flags |= q->ipqe_flags;
    312 			m_cat(m, q->ipqe_m);
    313 			LIST_REMOVE(q, ipqe_q);
    314 			LIST_REMOVE(q, ipqe_timeq);
    315 			if (tiqe == NULL) {
    316 			    tiqe = q;
    317 			} else {
    318 			    FREE(q, M_IPQ);
    319 			}
    320 			break;
    321 		}
    322 		/*
    323 		 * If the fragment is before the segment, remember it.
    324 		 * When this loop is terminated, p will contain the
    325 		 * pointer to fragment that is right before the received
    326 		 * segment.
    327 		 */
    328 		if (SEQ_LEQ(q->ipqe_seq, pkt_seq))
    329 			p = q;
    330 
    331 		continue;
    332 
    333 		/*
    334 		 * This is a common operation.  It also will allow
    335 		 * to save doing a malloc/free in most instances.
    336 		 */
    337 	  free_ipqe:
    338 		LIST_REMOVE(q, ipqe_q);
    339 		LIST_REMOVE(q, ipqe_timeq);
    340 		if (tiqe == NULL) {
    341 		    tiqe = q;
    342 		} else {
    343 		    FREE(q, M_IPQ);
    344 		}
    345 	}
    346 
    347 	/*
    348 	 * Allocate a new queue entry since the received segment did not
    349 	 * collapse onto any other out-of-order block; thus we are allocating
    350 	 * a new block.  If it had collapsed, tiqe would not be NULL and
    351 	 * we would be reusing it.
    352 	 * XXX If we can't, just drop the packet.  XXX
    353 	 */
    354 	if (tiqe == NULL) {
    355 		MALLOC(tiqe, struct ipqent *, sizeof (struct ipqent), M_IPQ, M_NOWAIT);
    356 		if (tiqe == NULL) {
    357 			tcpstat.tcps_rcvmemdrop++;
    358 			m_freem(m);
    359 			return (0);
    360 		}
    361 	}
    362 
    363 	/*
    364 	 * Update the counters.
    365 	 */
    366 	tcpstat.tcps_rcvoopack++;
    367 	tcpstat.tcps_rcvoobyte += rcvoobyte;
    368 	if (rcvpartdupbyte) {
    369 	    tcpstat.tcps_rcvpartduppack++;
    370 	    tcpstat.tcps_rcvpartdupbyte += rcvpartdupbyte;
    371 	}
    372 
    373 	/*
    374 	 * Insert the new fragment queue entry into both queues.
    375 	 */
    376 	tiqe->ipqe_m = m;
    377 	tiqe->ipqe_seq = pkt_seq;
    378 	tiqe->ipqe_len = pkt_len;
    379 	tiqe->ipqe_flags = pkt_flags;
    380 	if (p == NULL) {
    381 		LIST_INSERT_HEAD(&tp->segq, tiqe, ipqe_q);
    382 #ifdef TCPREASS_DEBUG
    383 		if (tiqe->ipqe_seq != tp->rcv_nxt)
    384 			printf("tcp_reass[%p]: insert %u:%u(%u) at front\n",
    385 			       tp, pkt_seq, pkt_seq + pkt_len, pkt_len);
    386 #endif
    387 	} else {
    388 		LIST_INSERT_AFTER(p, tiqe, ipqe_q);
    389 #ifdef TCPREASS_DEBUG
    390 		printf("tcp_reass[%p]: insert %u:%u(%u) after %u:%u(%u)\n",
    391 		       tp, pkt_seq, pkt_seq + pkt_len, pkt_len,
    392 		       p->ipqe_seq, p->ipqe_seq + p->ipqe_len, p->ipqe_len);
    393 #endif
    394 	}
    395 
    396 	LIST_INSERT_HEAD(&tp->timeq, tiqe, ipqe_timeq);
    397 
    398 present:
    399 	/*
    400 	 * Present data to user, advancing rcv_nxt through
    401 	 * completed sequence space.
    402 	 */
    403 	if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
    404 		return (0);
    405 	q = tp->segq.lh_first;
    406 	if (q == NULL || q->ipqe_seq != tp->rcv_nxt)
    407 		return (0);
    408 	if (tp->t_state == TCPS_SYN_RECEIVED && q->ipqe_len)
    409 		return (0);
    410 
    411 	tp->rcv_nxt += q->ipqe_len;
    412 	pkt_flags = q->ipqe_flags & TH_FIN;
    413 
    414 	LIST_REMOVE(q, ipqe_q);
    415 	LIST_REMOVE(q, ipqe_timeq);
    416 	if (so->so_state & SS_CANTRCVMORE)
    417 		m_freem(q->ipqe_m);
    418 	else
    419 		sbappend(&so->so_rcv, q->ipqe_m);
    420 	FREE(q, M_IPQ);
    421 	sorwakeup(so);
    422 	return (pkt_flags);
    423 }
    424 
    425 /*
    426  * TCP input routine, follows pages 65-76 of the
    427  * protocol specification dated September, 1981 very closely.
    428  */
    429 void
    430 #if __STDC__
    431 tcp_input(struct mbuf *m, ...)
    432 #else
    433 tcp_input(m, va_alist)
    434 	register struct mbuf *m;
    435 #endif
    436 {
    437 	register struct tcpiphdr *ti;
    438 	register struct inpcb *inp;
    439 	caddr_t optp = NULL;
    440 	int optlen = 0;
    441 	int len, tlen, off, hdroptlen;
    442 	register struct tcpcb *tp = 0;
    443 	register int tiflags;
    444 	struct socket *so = NULL;
    445 	int todrop, acked, ourfinisacked, needoutput = 0;
    446 	short ostate = 0;
    447 	int iss = 0;
    448 	u_long tiwin;
    449 	struct tcp_opt_info opti;
    450 	int iphlen;
    451 	va_list ap;
    452 
    453 	va_start(ap, m);
    454 	iphlen = va_arg(ap, int);
    455 	va_end(ap);
    456 
    457 	tcpstat.tcps_rcvtotal++;
    458 
    459 	opti.ts_present = 0;
    460 	opti.maxseg = 0;
    461 
    462 	/*
    463 	 * Get IP and TCP header together in first mbuf.
    464 	 * Note: IP leaves IP header in first mbuf.
    465 	 */
    466 	ti = mtod(m, struct tcpiphdr *);
    467 	if (iphlen > sizeof (struct ip))
    468 		ip_stripoptions(m, (struct mbuf *)0);
    469 	if (m->m_len < sizeof (struct tcpiphdr)) {
    470 		if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
    471 			tcpstat.tcps_rcvshort++;
    472 			return;
    473 		}
    474 		ti = mtod(m, struct tcpiphdr *);
    475 	}
    476 
    477 	/*
    478 	 * Checksum extended TCP header and data.
    479 	 */
    480 	tlen = ((struct ip *)ti)->ip_len;
    481 	len = sizeof (struct ip) + tlen;
    482 	bzero(ti->ti_x1, sizeof ti->ti_x1);
    483 	ti->ti_len = (u_int16_t)tlen;
    484 	HTONS(ti->ti_len);
    485 	if ((ti->ti_sum = in_cksum(m, len)) != 0) {
    486 		tcpstat.tcps_rcvbadsum++;
    487 		goto drop;
    488 	}
    489 
    490 	/*
    491 	 * Check that TCP offset makes sense,
    492 	 * pull out TCP options and adjust length.		XXX
    493 	 */
    494 	off = ti->ti_off << 2;
    495 	if (off < sizeof (struct tcphdr) || off > tlen) {
    496 		tcpstat.tcps_rcvbadoff++;
    497 		goto drop;
    498 	}
    499 	tlen -= off;
    500 	ti->ti_len = tlen;
    501 	if (off > sizeof (struct tcphdr)) {
    502 		if (m->m_len < sizeof(struct ip) + off) {
    503 			if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
    504 				tcpstat.tcps_rcvshort++;
    505 				return;
    506 			}
    507 			ti = mtod(m, struct tcpiphdr *);
    508 		}
    509 		optlen = off - sizeof (struct tcphdr);
    510 		optp = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
    511 		/*
    512 		 * Do quick retrieval of timestamp options ("options
    513 		 * prediction?").  If timestamp is the only option and it's
    514 		 * formatted as recommended in RFC 1323 appendix A, we
    515 		 * quickly get the values now and not bother calling
    516 		 * tcp_dooptions(), etc.
    517 		 */
    518 		if ((optlen == TCPOLEN_TSTAMP_APPA ||
    519 		     (optlen > TCPOLEN_TSTAMP_APPA &&
    520 			optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) &&
    521 		     *(u_int32_t *)optp == htonl(TCPOPT_TSTAMP_HDR) &&
    522 		     (ti->ti_flags & TH_SYN) == 0) {
    523 			opti.ts_present = 1;
    524 			opti.ts_val = ntohl(*(u_int32_t *)(optp + 4));
    525 			opti.ts_ecr = ntohl(*(u_int32_t *)(optp + 8));
    526 			optp = NULL;	/* we've parsed the options */
    527 		}
    528 	}
    529 	tiflags = ti->ti_flags;
    530 
    531 	/*
    532 	 * Convert TCP protocol specific fields to host format.
    533 	 */
    534 	NTOHL(ti->ti_seq);
    535 	NTOHL(ti->ti_ack);
    536 	NTOHS(ti->ti_win);
    537 	NTOHS(ti->ti_urp);
    538 
    539 	/*
    540 	 * Locate pcb for segment.
    541 	 */
    542 findpcb:
    543 	inp = in_pcblookup_connect(&tcbtable, ti->ti_src, ti->ti_sport,
    544 	    ti->ti_dst, ti->ti_dport);
    545 	if (inp == 0) {
    546 		++tcpstat.tcps_pcbhashmiss;
    547 		inp = in_pcblookup_bind(&tcbtable, ti->ti_dst, ti->ti_dport);
    548 		if (inp == 0) {
    549 			++tcpstat.tcps_noport;
    550 			goto dropwithreset;
    551 		}
    552 	}
    553 
    554 	/*
    555 	 * If the state is CLOSED (i.e., TCB does not exist) then
    556 	 * all data in the incoming segment is discarded.
    557 	 * If the TCB exists but is in CLOSED state, it is embryonic,
    558 	 * but should either do a listen or a connect soon.
    559 	 */
    560 	tp = intotcpcb(inp);
    561 	if (tp == 0)
    562 		goto dropwithreset;
    563 	if (tp->t_state == TCPS_CLOSED)
    564 		goto drop;
    565 
    566 	/* Unscale the window into a 32-bit value. */
    567 	if ((tiflags & TH_SYN) == 0)
    568 		tiwin = ti->ti_win << tp->snd_scale;
    569 	else
    570 		tiwin = ti->ti_win;
    571 
    572 	so = inp->inp_socket;
    573 	if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
    574 		if (so->so_options & SO_DEBUG) {
    575 			ostate = tp->t_state;
    576 			tcp_saveti = *ti;
    577 		}
    578 		if (so->so_options & SO_ACCEPTCONN) {
    579   			if ((tiflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
    580 				if (tiflags & TH_RST) {
    581 					syn_cache_reset(ti);
    582 				} else if ((tiflags & (TH_ACK|TH_SYN)) ==
    583 				    (TH_ACK|TH_SYN)) {
    584 					/*
    585 					 * Received a SYN,ACK.  This should
    586 					 * never happen while we are in
    587 					 * LISTEN.  Send an RST.
    588 					 */
    589 					goto badsyn;
    590 				} else if (tiflags & TH_ACK) {
    591 					so = syn_cache_get(so, m);
    592 					if (so == NULL) {
    593 						/*
    594 						 * We don't have a SYN for
    595 						 * this ACK; send an RST.
    596 						 */
    597 						goto badsyn;
    598 					} else if (so ==
    599 					    (struct socket *)(-1)) {
    600 						/*
    601 						 * We were unable to create
    602 						 * the connection.  If the
    603 						 * 3-way handshake was
    604 						 * completeed, and RST has
    605 						 * been sent to the peer.
    606 						 * Since the mbuf might be
    607 						 * in use for the reply,
    608 						 * do not free it.
    609 						 */
    610 						m = NULL;
    611 					} else {
    612 						/*
    613 						 * We have created a
    614 						 * full-blown connection.
    615 						 */
    616 						inp = sotoinpcb(so);
    617 						tp = intotcpcb(inp);
    618 						tiwin <<= tp->snd_scale;
    619 						goto after_listen;
    620 					}
    621   				}
    622   			} else {
    623 				/*
    624 				 * Received a SYN.
    625 				 */
    626 				if (in_hosteq(ti->ti_src, ti->ti_dst) &&
    627 				    ti->ti_sport == ti->ti_dport) {
    628 					/*
    629 					 * LISTEN socket received a SYN
    630 					 * from itself?  This can't possibly
    631 					 * be valid; drop the packet.
    632 					 */
    633 					tcpstat.tcps_badsyn++;
    634 					goto drop;
    635 				}
    636 				/*
    637 				 * SYN looks ok; create compressed TCP
    638 				 * state for it.
    639 				 */
    640 				if (so->so_qlen <= so->so_qlimit &&
    641 				    syn_cache_add(so, m, optp, optlen, &opti))
    642 					m = NULL;
    643 			}
    644 			goto drop;
    645 		}
    646 	}
    647 
    648 after_listen:
    649 #ifdef DIAGNOSTIC
    650 	/*
    651 	 * Should not happen now that all embryonic connections
    652 	 * are handled with compressed state.
    653 	 */
    654 	if (tp->t_state == TCPS_LISTEN)
    655 		panic("tcp_input: TCPS_LISTEN");
    656 #endif
    657 
    658 	/*
    659 	 * Segment received on connection.
    660 	 * Reset idle time and keep-alive timer.
    661 	 */
    662 	tp->t_idle = 0;
    663 	if (TCPS_HAVEESTABLISHED(tp->t_state))
    664 		TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle);
    665 
    666 	/*
    667 	 * Process options.
    668 	 */
    669 	if (optp)
    670 		tcp_dooptions(tp, optp, optlen, ti, &opti);
    671 
    672 	/*
    673 	 * Header prediction: check for the two common cases
    674 	 * of a uni-directional data xfer.  If the packet has
    675 	 * no control flags, is in-sequence, the window didn't
    676 	 * change and we're not retransmitting, it's a
    677 	 * candidate.  If the length is zero and the ack moved
    678 	 * forward, we're the sender side of the xfer.  Just
    679 	 * free the data acked & wake any higher level process
    680 	 * that was blocked waiting for space.  If the length
    681 	 * is non-zero and the ack didn't move, we're the
    682 	 * receiver side.  If we're getting packets in-order
    683 	 * (the reassembly queue is empty), add the data to
    684 	 * the socket buffer and note that we need a delayed ack.
    685 	 */
    686 	if (tp->t_state == TCPS_ESTABLISHED &&
    687 	    (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
    688 	    (!opti.ts_present || TSTMP_GEQ(opti.ts_val, tp->ts_recent)) &&
    689 	    ti->ti_seq == tp->rcv_nxt &&
    690 	    tiwin && tiwin == tp->snd_wnd &&
    691 	    tp->snd_nxt == tp->snd_max) {
    692 
    693 		/*
    694 		 * If last ACK falls within this segment's sequence numbers,
    695 		 *  record the timestamp.
    696 		 */
    697 		if (opti.ts_present &&
    698 		    SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
    699 		    SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len)) {
    700 			tp->ts_recent_age = tcp_now;
    701 			tp->ts_recent = opti.ts_val;
    702 		}
    703 
    704 		if (ti->ti_len == 0) {
    705 			if (SEQ_GT(ti->ti_ack, tp->snd_una) &&
    706 			    SEQ_LEQ(ti->ti_ack, tp->snd_max) &&
    707 			    tp->snd_cwnd >= tp->snd_wnd &&
    708 			    tp->t_dupacks < tcprexmtthresh) {
    709 				/*
    710 				 * this is a pure ack for outstanding data.
    711 				 */
    712 				++tcpstat.tcps_predack;
    713 				if (opti.ts_present)
    714 					tcp_xmit_timer(tp,
    715 					    tcp_now-opti.ts_ecr+1);
    716 				else if (tp->t_rtt &&
    717 				    SEQ_GT(ti->ti_ack, tp->t_rtseq))
    718 					tcp_xmit_timer(tp, tp->t_rtt);
    719 				acked = ti->ti_ack - tp->snd_una;
    720 				tcpstat.tcps_rcvackpack++;
    721 				tcpstat.tcps_rcvackbyte += acked;
    722 				sbdrop(&so->so_snd, acked);
    723 				tp->snd_una = ti->ti_ack;
    724 				m_freem(m);
    725 
    726 				/*
    727 				 * If all outstanding data are acked, stop
    728 				 * retransmit timer, otherwise restart timer
    729 				 * using current (possibly backed-off) value.
    730 				 * If process is waiting for space,
    731 				 * wakeup/selwakeup/signal.  If data
    732 				 * are ready to send, let tcp_output
    733 				 * decide between more output or persist.
    734 				 */
    735 				if (tp->snd_una == tp->snd_max)
    736 					TCP_TIMER_DISARM(tp, TCPT_REXMT);
    737 				else if (TCP_TIMER_ISARMED(tp,
    738 				    TCPT_PERSIST) == 0)
    739 					TCP_TIMER_ARM(tp, TCPT_REXMT,
    740 					    tp->t_rxtcur);
    741 
    742 				sowwakeup(so);
    743 				if (so->so_snd.sb_cc)
    744 					(void) tcp_output(tp);
    745 				return;
    746 			}
    747 		} else if (ti->ti_ack == tp->snd_una &&
    748 		    tp->segq.lh_first == NULL &&
    749 		    ti->ti_len <= sbspace(&so->so_rcv)) {
    750 			/*
    751 			 * this is a pure, in-sequence data packet
    752 			 * with nothing on the reassembly queue and
    753 			 * we have enough buffer space to take it.
    754 			 */
    755 			++tcpstat.tcps_preddat;
    756 			tp->rcv_nxt += ti->ti_len;
    757 			tcpstat.tcps_rcvpack++;
    758 			tcpstat.tcps_rcvbyte += ti->ti_len;
    759 			/*
    760 			 * Drop TCP, IP headers and TCP options then add data
    761 			 * to socket buffer.
    762 			 */
    763 			m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
    764 			m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
    765 			sbappend(&so->so_rcv, m);
    766 			sorwakeup(so);
    767 			TCP_SETUP_ACK(tp, ti);
    768 			if (tp->t_flags & TF_ACKNOW)
    769 				(void) tcp_output(tp);
    770 			return;
    771 		}
    772 	}
    773 
    774 	/*
    775 	 * Drop TCP, IP headers and TCP options.
    776 	 */
    777 	hdroptlen  = sizeof(struct tcpiphdr) + off - sizeof(struct tcphdr);
    778 	m->m_data += hdroptlen;
    779 	m->m_len  -= hdroptlen;
    780 
    781 	/*
    782 	 * Calculate amount of space in receive window,
    783 	 * and then do TCP input processing.
    784 	 * Receive window is amount of space in rcv queue,
    785 	 * but not less than advertised window.
    786 	 */
    787 	{ int win;
    788 
    789 	win = sbspace(&so->so_rcv);
    790 	if (win < 0)
    791 		win = 0;
    792 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
    793 	}
    794 
    795 	switch (tp->t_state) {
    796 
    797 	/*
    798 	 * If the state is SYN_SENT:
    799 	 *	if seg contains an ACK, but not for our SYN, drop the input.
    800 	 *	if seg contains a RST, then drop the connection.
    801 	 *	if seg does not contain SYN, then drop it.
    802 	 * Otherwise this is an acceptable SYN segment
    803 	 *	initialize tp->rcv_nxt and tp->irs
    804 	 *	if seg contains ack then advance tp->snd_una
    805 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
    806 	 *	arrange for segment to be acked (eventually)
    807 	 *	continue processing rest of data/controls, beginning with URG
    808 	 */
    809 	case TCPS_SYN_SENT:
    810 		if ((tiflags & TH_ACK) &&
    811 		    (SEQ_LEQ(ti->ti_ack, tp->iss) ||
    812 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
    813 			goto dropwithreset;
    814 		if (tiflags & TH_RST) {
    815 			if (tiflags & TH_ACK)
    816 				tp = tcp_drop(tp, ECONNREFUSED);
    817 			goto drop;
    818 		}
    819 		if ((tiflags & TH_SYN) == 0)
    820 			goto drop;
    821 		if (tiflags & TH_ACK) {
    822 			tp->snd_una = ti->ti_ack;
    823 			if (SEQ_LT(tp->snd_nxt, tp->snd_una))
    824 				tp->snd_nxt = tp->snd_una;
    825 		}
    826 		TCP_TIMER_DISARM(tp, TCPT_REXMT);
    827 		tp->irs = ti->ti_seq;
    828 		tcp_rcvseqinit(tp);
    829 		tp->t_flags |= TF_ACKNOW;
    830 		tcp_mss_from_peer(tp, opti.maxseg);
    831 
    832 		/*
    833 		 * Initialize the initial congestion window.  If we
    834 		 * had to retransmit the SYN, we must initialize cwnd
    835 		 * to 1 segment (i.e. the Loss Window).
    836 		 */
    837 		if (tp->t_flags & TF_SYN_REXMT)
    838 			tp->snd_cwnd = tp->t_peermss;
    839 		else
    840 			tp->snd_cwnd = TCP_INITIAL_WINDOW(tcp_init_win,
    841 			    tp->t_peermss);
    842 
    843 		tcp_rmx_rtt(tp);
    844 		if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
    845 			tcpstat.tcps_connects++;
    846 			soisconnected(so);
    847 			tcp_established(tp);
    848 			/* Do window scaling on this connection? */
    849 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
    850 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
    851 				tp->snd_scale = tp->requested_s_scale;
    852 				tp->rcv_scale = tp->request_r_scale;
    853 			}
    854 			(void) tcp_reass(tp, (struct tcpiphdr *)0,
    855 				(struct mbuf *)0);
    856 			/*
    857 			 * if we didn't have to retransmit the SYN,
    858 			 * use its rtt as our initial srtt & rtt var.
    859 			 */
    860 			if (tp->t_rtt)
    861 				tcp_xmit_timer(tp, tp->t_rtt);
    862 		} else
    863 			tp->t_state = TCPS_SYN_RECEIVED;
    864 
    865 		/*
    866 		 * Advance ti->ti_seq to correspond to first data byte.
    867 		 * If data, trim to stay within window,
    868 		 * dropping FIN if necessary.
    869 		 */
    870 		ti->ti_seq++;
    871 		if (ti->ti_len > tp->rcv_wnd) {
    872 			todrop = ti->ti_len - tp->rcv_wnd;
    873 			m_adj(m, -todrop);
    874 			ti->ti_len = tp->rcv_wnd;
    875 			tiflags &= ~TH_FIN;
    876 			tcpstat.tcps_rcvpackafterwin++;
    877 			tcpstat.tcps_rcvbyteafterwin += todrop;
    878 		}
    879 		tp->snd_wl1 = ti->ti_seq - 1;
    880 		tp->rcv_up = ti->ti_seq;
    881 		goto step6;
    882 
    883 	/*
    884 	 * If the state is SYN_RECEIVED:
    885 	 *	If seg contains an ACK, but not for our SYN, drop the input
    886 	 *	and generate an RST.  See page 36, rfc793
    887 	 */
    888 	case TCPS_SYN_RECEIVED:
    889 		if ((tiflags & TH_ACK) &&
    890 		    (SEQ_LEQ(ti->ti_ack, tp->iss) ||
    891 		     SEQ_GT(ti->ti_ack, tp->snd_max)))
    892 			goto dropwithreset;
    893 		break;
    894 	}
    895 
    896 	/*
    897 	 * States other than LISTEN or SYN_SENT.
    898 	 * First check timestamp, if present.
    899 	 * Then check that at least some bytes of segment are within
    900 	 * receive window.  If segment begins before rcv_nxt,
    901 	 * drop leading data (and SYN); if nothing left, just ack.
    902 	 *
    903 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
    904 	 * and it's less than ts_recent, drop it.
    905 	 */
    906 	if (opti.ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent &&
    907 	    TSTMP_LT(opti.ts_val, tp->ts_recent)) {
    908 
    909 		/* Check to see if ts_recent is over 24 days old.  */
    910 		if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) {
    911 			/*
    912 			 * Invalidate ts_recent.  If this segment updates
    913 			 * ts_recent, the age will be reset later and ts_recent
    914 			 * will get a valid value.  If it does not, setting
    915 			 * ts_recent to zero will at least satisfy the
    916 			 * requirement that zero be placed in the timestamp
    917 			 * echo reply when ts_recent isn't valid.  The
    918 			 * age isn't reset until we get a valid ts_recent
    919 			 * because we don't want out-of-order segments to be
    920 			 * dropped when ts_recent is old.
    921 			 */
    922 			tp->ts_recent = 0;
    923 		} else {
    924 			tcpstat.tcps_rcvduppack++;
    925 			tcpstat.tcps_rcvdupbyte += ti->ti_len;
    926 			tcpstat.tcps_pawsdrop++;
    927 			goto dropafterack;
    928 		}
    929 	}
    930 
    931 	todrop = tp->rcv_nxt - ti->ti_seq;
    932 	if (todrop > 0) {
    933 		if (tiflags & TH_SYN) {
    934 			tiflags &= ~TH_SYN;
    935 			ti->ti_seq++;
    936 			if (ti->ti_urp > 1)
    937 				ti->ti_urp--;
    938 			else {
    939 				tiflags &= ~TH_URG;
    940 				ti->ti_urp = 0;
    941 			}
    942 			todrop--;
    943 		}
    944 		if (todrop > ti->ti_len ||
    945 		    (todrop == ti->ti_len && (tiflags & TH_FIN) == 0)) {
    946 			/*
    947 			 * Any valid FIN must be to the left of the window.
    948 			 * At this point the FIN must be a duplicate or
    949 			 * out of sequence; drop it.
    950 			 */
    951 			tiflags &= ~TH_FIN;
    952 			/*
    953 			 * Send an ACK to resynchronize and drop any data.
    954 			 * But keep on processing for RST or ACK.
    955 			 */
    956 			tp->t_flags |= TF_ACKNOW;
    957 			todrop = ti->ti_len;
    958 			tcpstat.tcps_rcvdupbyte += todrop;
    959 			tcpstat.tcps_rcvduppack++;
    960 		} else {
    961 			tcpstat.tcps_rcvpartduppack++;
    962 			tcpstat.tcps_rcvpartdupbyte += todrop;
    963 		}
    964 		m_adj(m, todrop);
    965 		ti->ti_seq += todrop;
    966 		ti->ti_len -= todrop;
    967 		if (ti->ti_urp > todrop)
    968 			ti->ti_urp -= todrop;
    969 		else {
    970 			tiflags &= ~TH_URG;
    971 			ti->ti_urp = 0;
    972 		}
    973 	}
    974 
    975 	/*
    976 	 * If new data are received on a connection after the
    977 	 * user processes are gone, then RST the other end.
    978 	 */
    979 	if ((so->so_state & SS_NOFDREF) &&
    980 	    tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
    981 		tp = tcp_close(tp);
    982 		tcpstat.tcps_rcvafterclose++;
    983 		goto dropwithreset;
    984 	}
    985 
    986 	/*
    987 	 * If segment ends after window, drop trailing data
    988 	 * (and PUSH and FIN); if nothing left, just ACK.
    989 	 */
    990 	todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
    991 	if (todrop > 0) {
    992 		tcpstat.tcps_rcvpackafterwin++;
    993 		if (todrop >= ti->ti_len) {
    994 			tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
    995 			/*
    996 			 * If a new connection request is received
    997 			 * while in TIME_WAIT, drop the old connection
    998 			 * and start over if the sequence numbers
    999 			 * are above the previous ones.
   1000 			 */
   1001 			if (tiflags & TH_SYN &&
   1002 			    tp->t_state == TCPS_TIME_WAIT &&
   1003 			    SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
   1004 				iss = tcp_new_iss(tp, sizeof(struct tcpcb),
   1005 						  tp->rcv_nxt);
   1006 				tp = tcp_close(tp);
   1007 				/*
   1008 				 * We have already advanced the mbuf
   1009 				 * pointers past the IP+TCP headers and
   1010 				 * options.  Restore those pointers before
   1011 				 * attempting to use the TCP header again.
   1012 				 */
   1013 				m->m_data -= hdroptlen;
   1014 				m->m_len  += hdroptlen;
   1015 				goto findpcb;
   1016 			}
   1017 			/*
   1018 			 * If window is closed can only take segments at
   1019 			 * window edge, and have to drop data and PUSH from
   1020 			 * incoming segments.  Continue processing, but
   1021 			 * remember to ack.  Otherwise, drop segment
   1022 			 * and ack.
   1023 			 */
   1024 			if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
   1025 				tp->t_flags |= TF_ACKNOW;
   1026 				tcpstat.tcps_rcvwinprobe++;
   1027 			} else
   1028 				goto dropafterack;
   1029 		} else
   1030 			tcpstat.tcps_rcvbyteafterwin += todrop;
   1031 		m_adj(m, -todrop);
   1032 		ti->ti_len -= todrop;
   1033 		tiflags &= ~(TH_PUSH|TH_FIN);
   1034 	}
   1035 
   1036 	/*
   1037 	 * If last ACK falls within this segment's sequence numbers,
   1038 	 * and the timestamp is newer, record it.
   1039 	 */
   1040 	if (opti.ts_present && TSTMP_GEQ(opti.ts_val, tp->ts_recent) &&
   1041 	    SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
   1042 	    SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len +
   1043 		   ((tiflags & (TH_SYN|TH_FIN)) != 0))) {
   1044 		tp->ts_recent_age = tcp_now;
   1045 		tp->ts_recent = opti.ts_val;
   1046 	}
   1047 
   1048 	/*
   1049 	 * If the RST bit is set examine the state:
   1050 	 *    SYN_RECEIVED STATE:
   1051 	 *	If passive open, return to LISTEN state.
   1052 	 *	If active open, inform user that connection was refused.
   1053 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
   1054 	 *	Inform user that connection was reset, and close tcb.
   1055 	 *    CLOSING, LAST_ACK, TIME_WAIT STATES
   1056 	 *	Close the tcb.
   1057 	 */
   1058 	if (tiflags&TH_RST) switch (tp->t_state) {
   1059 
   1060 	case TCPS_SYN_RECEIVED:
   1061 		so->so_error = ECONNREFUSED;
   1062 		goto close;
   1063 
   1064 	case TCPS_ESTABLISHED:
   1065 	case TCPS_FIN_WAIT_1:
   1066 	case TCPS_FIN_WAIT_2:
   1067 	case TCPS_CLOSE_WAIT:
   1068 		so->so_error = ECONNRESET;
   1069 	close:
   1070 		tp->t_state = TCPS_CLOSED;
   1071 		tcpstat.tcps_drops++;
   1072 		tp = tcp_close(tp);
   1073 		goto drop;
   1074 
   1075 	case TCPS_CLOSING:
   1076 	case TCPS_LAST_ACK:
   1077 	case TCPS_TIME_WAIT:
   1078 		tp = tcp_close(tp);
   1079 		goto drop;
   1080 	}
   1081 
   1082 	/*
   1083 	 * If a SYN is in the window, then this is an
   1084 	 * error and we send an RST and drop the connection.
   1085 	 */
   1086 	if (tiflags & TH_SYN) {
   1087 		tp = tcp_drop(tp, ECONNRESET);
   1088 		goto dropwithreset;
   1089 	}
   1090 
   1091 	/*
   1092 	 * If the ACK bit is off we drop the segment and return.
   1093 	 */
   1094 	if ((tiflags & TH_ACK) == 0)
   1095 		goto drop;
   1096 
   1097 	/*
   1098 	 * Ack processing.
   1099 	 */
   1100 	switch (tp->t_state) {
   1101 
   1102 	/*
   1103 	 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
   1104 	 * ESTABLISHED state and continue processing, otherwise
   1105 	 * send an RST.
   1106 	 */
   1107 	case TCPS_SYN_RECEIVED:
   1108 		if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
   1109 		    SEQ_GT(ti->ti_ack, tp->snd_max))
   1110 			goto dropwithreset;
   1111 		tcpstat.tcps_connects++;
   1112 		soisconnected(so);
   1113 		tcp_established(tp);
   1114 		/* Do window scaling? */
   1115 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
   1116 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
   1117 			tp->snd_scale = tp->requested_s_scale;
   1118 			tp->rcv_scale = tp->request_r_scale;
   1119 		}
   1120 		(void) tcp_reass(tp, (struct tcpiphdr *)0, (struct mbuf *)0);
   1121 		tp->snd_wl1 = ti->ti_seq - 1;
   1122 		/* fall into ... */
   1123 
   1124 	/*
   1125 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
   1126 	 * ACKs.  If the ack is in the range
   1127 	 *	tp->snd_una < ti->ti_ack <= tp->snd_max
   1128 	 * then advance tp->snd_una to ti->ti_ack and drop
   1129 	 * data from the retransmission queue.  If this ACK reflects
   1130 	 * more up to date window information we update our window information.
   1131 	 */
   1132 	case TCPS_ESTABLISHED:
   1133 	case TCPS_FIN_WAIT_1:
   1134 	case TCPS_FIN_WAIT_2:
   1135 	case TCPS_CLOSE_WAIT:
   1136 	case TCPS_CLOSING:
   1137 	case TCPS_LAST_ACK:
   1138 	case TCPS_TIME_WAIT:
   1139 
   1140 		if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
   1141 			if (ti->ti_len == 0 && tiwin == tp->snd_wnd) {
   1142 				tcpstat.tcps_rcvdupack++;
   1143 				/*
   1144 				 * If we have outstanding data (other than
   1145 				 * a window probe), this is a completely
   1146 				 * duplicate ack (ie, window info didn't
   1147 				 * change), the ack is the biggest we've
   1148 				 * seen and we've seen exactly our rexmt
   1149 				 * threshhold of them, assume a packet
   1150 				 * has been dropped and retransmit it.
   1151 				 * Kludge snd_nxt & the congestion
   1152 				 * window so we send only this one
   1153 				 * packet.
   1154 				 *
   1155 				 * We know we're losing at the current
   1156 				 * window size so do congestion avoidance
   1157 				 * (set ssthresh to half the current window
   1158 				 * and pull our congestion window back to
   1159 				 * the new ssthresh).
   1160 				 *
   1161 				 * Dup acks mean that packets have left the
   1162 				 * network (they're now cached at the receiver)
   1163 				 * so bump cwnd by the amount in the receiver
   1164 				 * to keep a constant cwnd packets in the
   1165 				 * network.
   1166 				 */
   1167 				if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 ||
   1168 				    ti->ti_ack != tp->snd_una)
   1169 					tp->t_dupacks = 0;
   1170 				else if (++tp->t_dupacks == tcprexmtthresh) {
   1171 					tcp_seq onxt = tp->snd_nxt;
   1172 					u_int win =
   1173 					    min(tp->snd_wnd, tp->snd_cwnd) /
   1174 					    2 /	tp->t_segsz;
   1175 
   1176 					if (win < 2)
   1177 						win = 2;
   1178 					tp->snd_ssthresh = win * tp->t_segsz;
   1179 					TCP_TIMER_DISARM(tp, TCPT_REXMT);
   1180 					tp->t_rtt = 0;
   1181 					tp->snd_nxt = ti->ti_ack;
   1182 					tp->snd_cwnd = tp->t_segsz;
   1183 					(void) tcp_output(tp);
   1184 					tp->snd_cwnd = tp->snd_ssthresh +
   1185 					       tp->t_segsz * tp->t_dupacks;
   1186 					if (SEQ_GT(onxt, tp->snd_nxt))
   1187 						tp->snd_nxt = onxt;
   1188 					goto drop;
   1189 				} else if (tp->t_dupacks > tcprexmtthresh) {
   1190 					tp->snd_cwnd += tp->t_segsz;
   1191 					(void) tcp_output(tp);
   1192 					goto drop;
   1193 				}
   1194 			} else
   1195 				tp->t_dupacks = 0;
   1196 			break;
   1197 		}
   1198 		/*
   1199 		 * If the congestion window was inflated to account
   1200 		 * for the other side's cached packets, retract it.
   1201 		 */
   1202 		if (tp->t_dupacks >= tcprexmtthresh &&
   1203 		    tp->snd_cwnd > tp->snd_ssthresh)
   1204 			tp->snd_cwnd = tp->snd_ssthresh;
   1205 		tp->t_dupacks = 0;
   1206 		if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
   1207 			tcpstat.tcps_rcvacktoomuch++;
   1208 			goto dropafterack;
   1209 		}
   1210 		acked = ti->ti_ack - tp->snd_una;
   1211 		tcpstat.tcps_rcvackpack++;
   1212 		tcpstat.tcps_rcvackbyte += acked;
   1213 
   1214 		/*
   1215 		 * If we have a timestamp reply, update smoothed
   1216 		 * round trip time.  If no timestamp is present but
   1217 		 * transmit timer is running and timed sequence
   1218 		 * number was acked, update smoothed round trip time.
   1219 		 * Since we now have an rtt measurement, cancel the
   1220 		 * timer backoff (cf., Phil Karn's retransmit alg.).
   1221 		 * Recompute the initial retransmit timer.
   1222 		 */
   1223 		if (opti.ts_present)
   1224 			tcp_xmit_timer(tp, tcp_now - opti.ts_ecr + 1);
   1225 		else if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
   1226 			tcp_xmit_timer(tp,tp->t_rtt);
   1227 
   1228 		/*
   1229 		 * If all outstanding data is acked, stop retransmit
   1230 		 * timer and remember to restart (more output or persist).
   1231 		 * If there is more data to be acked, restart retransmit
   1232 		 * timer, using current (possibly backed-off) value.
   1233 		 */
   1234 		if (ti->ti_ack == tp->snd_max) {
   1235 			TCP_TIMER_DISARM(tp, TCPT_REXMT);
   1236 			needoutput = 1;
   1237 		} else if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0)
   1238 			TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
   1239 		/*
   1240 		 * When new data is acked, open the congestion window.
   1241 		 * If the window gives us less than ssthresh packets
   1242 		 * in flight, open exponentially (segsz per packet).
   1243 		 * Otherwise open linearly: segsz per window
   1244 		 * (segsz^2 / cwnd per packet), plus a constant
   1245 		 * fraction of a packet (segsz/8) to help larger windows
   1246 		 * open quickly enough.
   1247 		 */
   1248 		{
   1249 		register u_int cw = tp->snd_cwnd;
   1250 		register u_int incr = tp->t_segsz;
   1251 
   1252 		if (cw > tp->snd_ssthresh)
   1253 			incr = incr * incr / cw;
   1254 		tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale);
   1255 		}
   1256 		if (acked > so->so_snd.sb_cc) {
   1257 			tp->snd_wnd -= so->so_snd.sb_cc;
   1258 			sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
   1259 			ourfinisacked = 1;
   1260 		} else {
   1261 			sbdrop(&so->so_snd, acked);
   1262 			tp->snd_wnd -= acked;
   1263 			ourfinisacked = 0;
   1264 		}
   1265 		sowwakeup(so);
   1266 		tp->snd_una = ti->ti_ack;
   1267 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
   1268 			tp->snd_nxt = tp->snd_una;
   1269 
   1270 		switch (tp->t_state) {
   1271 
   1272 		/*
   1273 		 * In FIN_WAIT_1 STATE in addition to the processing
   1274 		 * for the ESTABLISHED state if our FIN is now acknowledged
   1275 		 * then enter FIN_WAIT_2.
   1276 		 */
   1277 		case TCPS_FIN_WAIT_1:
   1278 			if (ourfinisacked) {
   1279 				/*
   1280 				 * If we can't receive any more
   1281 				 * data, then closing user can proceed.
   1282 				 * Starting the timer is contrary to the
   1283 				 * specification, but if we don't get a FIN
   1284 				 * we'll hang forever.
   1285 				 */
   1286 				if (so->so_state & SS_CANTRCVMORE) {
   1287 					soisdisconnected(so);
   1288 					if (tcp_maxidle > 0)
   1289 						TCP_TIMER_ARM(tp, TCPT_2MSL,
   1290 						    tcp_maxidle);
   1291 				}
   1292 				tp->t_state = TCPS_FIN_WAIT_2;
   1293 			}
   1294 			break;
   1295 
   1296 	 	/*
   1297 		 * In CLOSING STATE in addition to the processing for
   1298 		 * the ESTABLISHED state if the ACK acknowledges our FIN
   1299 		 * then enter the TIME-WAIT state, otherwise ignore
   1300 		 * the segment.
   1301 		 */
   1302 		case TCPS_CLOSING:
   1303 			if (ourfinisacked) {
   1304 				tp->t_state = TCPS_TIME_WAIT;
   1305 				tcp_canceltimers(tp);
   1306 				TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
   1307 				soisdisconnected(so);
   1308 			}
   1309 			break;
   1310 
   1311 		/*
   1312 		 * In LAST_ACK, we may still be waiting for data to drain
   1313 		 * and/or to be acked, as well as for the ack of our FIN.
   1314 		 * If our FIN is now acknowledged, delete the TCB,
   1315 		 * enter the closed state and return.
   1316 		 */
   1317 		case TCPS_LAST_ACK:
   1318 			if (ourfinisacked) {
   1319 				tp = tcp_close(tp);
   1320 				goto drop;
   1321 			}
   1322 			break;
   1323 
   1324 		/*
   1325 		 * In TIME_WAIT state the only thing that should arrive
   1326 		 * is a retransmission of the remote FIN.  Acknowledge
   1327 		 * it and restart the finack timer.
   1328 		 */
   1329 		case TCPS_TIME_WAIT:
   1330 			TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
   1331 			goto dropafterack;
   1332 		}
   1333 	}
   1334 
   1335 step6:
   1336 	/*
   1337 	 * Update window information.
   1338 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
   1339 	 */
   1340 	if (((tiflags & TH_ACK) && SEQ_LT(tp->snd_wl1, ti->ti_seq)) ||
   1341 	    (tp->snd_wl1 == ti->ti_seq && SEQ_LT(tp->snd_wl2, ti->ti_ack)) ||
   1342 	    (tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd)) {
   1343 		/* keep track of pure window updates */
   1344 		if (ti->ti_len == 0 &&
   1345 		    tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd)
   1346 			tcpstat.tcps_rcvwinupd++;
   1347 		tp->snd_wnd = tiwin;
   1348 		tp->snd_wl1 = ti->ti_seq;
   1349 		tp->snd_wl2 = ti->ti_ack;
   1350 		if (tp->snd_wnd > tp->max_sndwnd)
   1351 			tp->max_sndwnd = tp->snd_wnd;
   1352 		needoutput = 1;
   1353 	}
   1354 
   1355 	/*
   1356 	 * Process segments with URG.
   1357 	 */
   1358 	if ((tiflags & TH_URG) && ti->ti_urp &&
   1359 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
   1360 		/*
   1361 		 * This is a kludge, but if we receive and accept
   1362 		 * random urgent pointers, we'll crash in
   1363 		 * soreceive.  It's hard to imagine someone
   1364 		 * actually wanting to send this much urgent data.
   1365 		 */
   1366 		if (ti->ti_urp + so->so_rcv.sb_cc > sb_max) {
   1367 			ti->ti_urp = 0;			/* XXX */
   1368 			tiflags &= ~TH_URG;		/* XXX */
   1369 			goto dodata;			/* XXX */
   1370 		}
   1371 		/*
   1372 		 * If this segment advances the known urgent pointer,
   1373 		 * then mark the data stream.  This should not happen
   1374 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
   1375 		 * a FIN has been received from the remote side.
   1376 		 * In these states we ignore the URG.
   1377 		 *
   1378 		 * According to RFC961 (Assigned Protocols),
   1379 		 * the urgent pointer points to the last octet
   1380 		 * of urgent data.  We continue, however,
   1381 		 * to consider it to indicate the first octet
   1382 		 * of data past the urgent section as the original
   1383 		 * spec states (in one of two places).
   1384 		 */
   1385 		if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
   1386 			tp->rcv_up = ti->ti_seq + ti->ti_urp;
   1387 			so->so_oobmark = so->so_rcv.sb_cc +
   1388 			    (tp->rcv_up - tp->rcv_nxt) - 1;
   1389 			if (so->so_oobmark == 0)
   1390 				so->so_state |= SS_RCVATMARK;
   1391 			sohasoutofband(so);
   1392 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
   1393 		}
   1394 		/*
   1395 		 * Remove out of band data so doesn't get presented to user.
   1396 		 * This can happen independent of advancing the URG pointer,
   1397 		 * but if two URG's are pending at once, some out-of-band
   1398 		 * data may creep in... ick.
   1399 		 */
   1400 		if (ti->ti_urp <= (u_int16_t) ti->ti_len
   1401 #ifdef SO_OOBINLINE
   1402 		     && (so->so_options & SO_OOBINLINE) == 0
   1403 #endif
   1404 		     )
   1405 			tcp_pulloutofband(so, ti, m);
   1406 	} else
   1407 		/*
   1408 		 * If no out of band data is expected,
   1409 		 * pull receive urgent pointer along
   1410 		 * with the receive window.
   1411 		 */
   1412 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
   1413 			tp->rcv_up = tp->rcv_nxt;
   1414 dodata:							/* XXX */
   1415 
   1416 	/*
   1417 	 * Process the segment text, merging it into the TCP sequencing queue,
   1418 	 * and arranging for acknowledgment of receipt if necessary.
   1419 	 * This process logically involves adjusting tp->rcv_wnd as data
   1420 	 * is presented to the user (this happens in tcp_usrreq.c,
   1421 	 * case PRU_RCVD).  If a FIN has already been received on this
   1422 	 * connection then we just ignore the text.
   1423 	 */
   1424 	if ((ti->ti_len || (tiflags & TH_FIN)) &&
   1425 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
   1426 		TCP_REASS(tp, ti, m, so, tiflags);
   1427 		/*
   1428 		 * Note the amount of data that peer has sent into
   1429 		 * our window, in order to estimate the sender's
   1430 		 * buffer size.
   1431 		 */
   1432 		len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
   1433 	} else {
   1434 		m_freem(m);
   1435 		tiflags &= ~TH_FIN;
   1436 	}
   1437 
   1438 	/*
   1439 	 * If FIN is received ACK the FIN and let the user know
   1440 	 * that the connection is closing.  Ignore a FIN received before
   1441 	 * the connection is fully established.
   1442 	 */
   1443 	if ((tiflags & TH_FIN) && TCPS_HAVEESTABLISHED(tp->t_state)) {
   1444 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
   1445 			socantrcvmore(so);
   1446 			tp->t_flags |= TF_ACKNOW;
   1447 			tp->rcv_nxt++;
   1448 		}
   1449 		switch (tp->t_state) {
   1450 
   1451 	 	/*
   1452 		 * In ESTABLISHED STATE enter the CLOSE_WAIT state.
   1453 		 */
   1454 		case TCPS_ESTABLISHED:
   1455 			tp->t_state = TCPS_CLOSE_WAIT;
   1456 			break;
   1457 
   1458 	 	/*
   1459 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
   1460 		 * enter the CLOSING state.
   1461 		 */
   1462 		case TCPS_FIN_WAIT_1:
   1463 			tp->t_state = TCPS_CLOSING;
   1464 			break;
   1465 
   1466 	 	/*
   1467 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
   1468 		 * starting the time-wait timer, turning off the other
   1469 		 * standard timers.
   1470 		 */
   1471 		case TCPS_FIN_WAIT_2:
   1472 			tp->t_state = TCPS_TIME_WAIT;
   1473 			tcp_canceltimers(tp);
   1474 			TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
   1475 			soisdisconnected(so);
   1476 			break;
   1477 
   1478 		/*
   1479 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
   1480 		 */
   1481 		case TCPS_TIME_WAIT:
   1482 			TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
   1483 			break;
   1484 		}
   1485 	}
   1486 	if (so->so_options & SO_DEBUG)
   1487 		tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
   1488 
   1489 	/*
   1490 	 * Return any desired output.
   1491 	 */
   1492 	if (needoutput || (tp->t_flags & TF_ACKNOW))
   1493 		(void) tcp_output(tp);
   1494 	return;
   1495 
   1496 badsyn:
   1497 	/*
   1498 	 * Received a bad SYN.  Increment counters and dropwithreset.
   1499 	 */
   1500 	tcpstat.tcps_badsyn++;
   1501 	tp = NULL;
   1502 	goto dropwithreset;
   1503 
   1504 dropafterack:
   1505 	/*
   1506 	 * Generate an ACK dropping incoming segment if it occupies
   1507 	 * sequence space, where the ACK reflects our state.
   1508 	 */
   1509 	if (tiflags & TH_RST)
   1510 		goto drop;
   1511 	m_freem(m);
   1512 	tp->t_flags |= TF_ACKNOW;
   1513 	(void) tcp_output(tp);
   1514 	return;
   1515 
   1516 dropwithreset:
   1517 	/*
   1518 	 * Generate a RST, dropping incoming segment.
   1519 	 * Make ACK acceptable to originator of segment.
   1520 	 * Don't bother to respond if destination was broadcast/multicast.
   1521 	 */
   1522 	if ((tiflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST) ||
   1523 	    IN_MULTICAST(ti->ti_dst.s_addr))
   1524 		goto drop;
   1525 	if (tiflags & TH_ACK)
   1526 		(void)tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
   1527 	else {
   1528 		if (tiflags & TH_SYN)
   1529 			ti->ti_len++;
   1530 		(void)tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
   1531 		    TH_RST|TH_ACK);
   1532 	}
   1533 	return;
   1534 
   1535 drop:
   1536 	/*
   1537 	 * Drop space held by incoming segment and return.
   1538 	 */
   1539 	if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
   1540 		tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
   1541 	m_freem(m);
   1542 	return;
   1543 }
   1544 
   1545 void
   1546 tcp_dooptions(tp, cp, cnt, ti, oi)
   1547 	struct tcpcb *tp;
   1548 	u_char *cp;
   1549 	int cnt;
   1550 	struct tcpiphdr *ti;
   1551 	struct tcp_opt_info *oi;
   1552 {
   1553 	u_int16_t mss;
   1554 	int opt, optlen;
   1555 
   1556 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
   1557 		opt = cp[0];
   1558 		if (opt == TCPOPT_EOL)
   1559 			break;
   1560 		if (opt == TCPOPT_NOP)
   1561 			optlen = 1;
   1562 		else {
   1563 			optlen = cp[1];
   1564 			if (optlen <= 0)
   1565 				break;
   1566 		}
   1567 		switch (opt) {
   1568 
   1569 		default:
   1570 			continue;
   1571 
   1572 		case TCPOPT_MAXSEG:
   1573 			if (optlen != TCPOLEN_MAXSEG)
   1574 				continue;
   1575 			if (!(ti->ti_flags & TH_SYN))
   1576 				continue;
   1577 			bcopy(cp + 2, &mss, sizeof(mss));
   1578 			oi->maxseg = ntohs(mss);
   1579 			break;
   1580 
   1581 		case TCPOPT_WINDOW:
   1582 			if (optlen != TCPOLEN_WINDOW)
   1583 				continue;
   1584 			if (!(ti->ti_flags & TH_SYN))
   1585 				continue;
   1586 			tp->t_flags |= TF_RCVD_SCALE;
   1587 			tp->requested_s_scale = cp[2];
   1588 			if (tp->requested_s_scale > TCP_MAX_WINSHIFT) {
   1589 				log(LOG_ERR, "TCP: invalid wscale %d from "
   1590 				    "0x%08x, assuming %d\n",
   1591 				    tp->requested_s_scale,
   1592 				    ntohl(ti->ti_src.s_addr),
   1593 				    TCP_MAX_WINSHIFT);
   1594 				tp->requested_s_scale = TCP_MAX_WINSHIFT;
   1595 			}
   1596 			break;
   1597 
   1598 		case TCPOPT_TIMESTAMP:
   1599 			if (optlen != TCPOLEN_TIMESTAMP)
   1600 				continue;
   1601 			oi->ts_present = 1;
   1602 			bcopy(cp + 2, &oi->ts_val, sizeof(oi->ts_val));
   1603 			NTOHL(oi->ts_val);
   1604 			bcopy(cp + 6, &oi->ts_ecr, sizeof(oi->ts_ecr));
   1605 			NTOHL(oi->ts_ecr);
   1606 
   1607 			/*
   1608 			 * A timestamp received in a SYN makes
   1609 			 * it ok to send timestamp requests and replies.
   1610 			 */
   1611 			if (ti->ti_flags & TH_SYN) {
   1612 				tp->t_flags |= TF_RCVD_TSTMP;
   1613 				tp->ts_recent = oi->ts_val;
   1614 				tp->ts_recent_age = tcp_now;
   1615 			}
   1616 			break;
   1617 		case TCPOPT_SACK_PERMITTED:
   1618 			if (optlen != TCPOLEN_SACK_PERMITTED)
   1619 				continue;
   1620 			if (!(ti->ti_flags & TH_SYN))
   1621 				continue;
   1622 			tp->t_flags &= ~TF_CANT_TXSACK;
   1623 			break;
   1624 
   1625 		case TCPOPT_SACK:
   1626 			if (tp->t_flags & TF_IGNR_RXSACK)
   1627 				continue;
   1628 			if (optlen % 8 != 2 || optlen < 10)
   1629 				continue;
   1630 			cp += 2;
   1631 			optlen -= 2;
   1632 			for (; optlen > 0; cp -= 8, optlen -= 8) {
   1633 				tcp_seq lwe, rwe;
   1634 				bcopy((char *)cp, (char *) &lwe, sizeof(lwe));
   1635 				NTOHL(lwe);
   1636 				bcopy((char *)cp, (char *) &rwe, sizeof(rwe));
   1637 				NTOHL(rwe);
   1638 				/* tcp_mark_sacked(tp, lwe, rwe); */
   1639 			}
   1640 			break;
   1641 		}
   1642 	}
   1643 }
   1644 
   1645 /*
   1646  * Pull out of band byte out of a segment so
   1647  * it doesn't appear in the user's data queue.
   1648  * It is still reflected in the segment length for
   1649  * sequencing purposes.
   1650  */
   1651 void
   1652 tcp_pulloutofband(so, ti, m)
   1653 	struct socket *so;
   1654 	struct tcpiphdr *ti;
   1655 	register struct mbuf *m;
   1656 {
   1657 	int cnt = ti->ti_urp - 1;
   1658 
   1659 	while (cnt >= 0) {
   1660 		if (m->m_len > cnt) {
   1661 			char *cp = mtod(m, caddr_t) + cnt;
   1662 			struct tcpcb *tp = sototcpcb(so);
   1663 
   1664 			tp->t_iobc = *cp;
   1665 			tp->t_oobflags |= TCPOOB_HAVEDATA;
   1666 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
   1667 			m->m_len--;
   1668 			return;
   1669 		}
   1670 		cnt -= m->m_len;
   1671 		m = m->m_next;
   1672 		if (m == 0)
   1673 			break;
   1674 	}
   1675 	panic("tcp_pulloutofband");
   1676 }
   1677 
   1678 /*
   1679  * Collect new round-trip time estimate
   1680  * and update averages and current timeout.
   1681  */
   1682 void
   1683 tcp_xmit_timer(tp, rtt)
   1684 	register struct tcpcb *tp;
   1685 	short rtt;
   1686 {
   1687 	register short delta;
   1688 	short rttmin;
   1689 
   1690 	tcpstat.tcps_rttupdated++;
   1691 	--rtt;
   1692 	if (tp->t_srtt != 0) {
   1693 		/*
   1694 		 * srtt is stored as fixed point with 3 bits after the
   1695 		 * binary point (i.e., scaled by 8).  The following magic
   1696 		 * is equivalent to the smoothing algorithm in rfc793 with
   1697 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
   1698 		 * point).  Adjust rtt to origin 0.
   1699 		 */
   1700 		delta = (rtt << 2) - (tp->t_srtt >> TCP_RTT_SHIFT);
   1701 		if ((tp->t_srtt += delta) <= 0)
   1702 			tp->t_srtt = 1 << 2;
   1703 		/*
   1704 		 * We accumulate a smoothed rtt variance (actually, a
   1705 		 * smoothed mean difference), then set the retransmit
   1706 		 * timer to smoothed rtt + 4 times the smoothed variance.
   1707 		 * rttvar is stored as fixed point with 2 bits after the
   1708 		 * binary point (scaled by 4).  The following is
   1709 		 * equivalent to rfc793 smoothing with an alpha of .75
   1710 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
   1711 		 * rfc793's wired-in beta.
   1712 		 */
   1713 		if (delta < 0)
   1714 			delta = -delta;
   1715 		delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
   1716 		if ((tp->t_rttvar += delta) <= 0)
   1717 			tp->t_rttvar = 1 << 2;
   1718 	} else {
   1719 		/*
   1720 		 * No rtt measurement yet - use the unsmoothed rtt.
   1721 		 * Set the variance to half the rtt (so our first
   1722 		 * retransmit happens at 3*rtt).
   1723 		 */
   1724 		tp->t_srtt = rtt << (TCP_RTT_SHIFT + 2);
   1725 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT + 2 - 1);
   1726 	}
   1727 	tp->t_rtt = 0;
   1728 	tp->t_rxtshift = 0;
   1729 
   1730 	/*
   1731 	 * the retransmit should happen at rtt + 4 * rttvar.
   1732 	 * Because of the way we do the smoothing, srtt and rttvar
   1733 	 * will each average +1/2 tick of bias.  When we compute
   1734 	 * the retransmit timer, we want 1/2 tick of rounding and
   1735 	 * 1 extra tick because of +-1/2 tick uncertainty in the
   1736 	 * firing of the timer.  The bias will give us exactly the
   1737 	 * 1.5 tick we need.  But, because the bias is
   1738 	 * statistical, we have to test that we don't drop below
   1739 	 * the minimum feasible timer (which is 2 ticks).
   1740 	 */
   1741 	if (tp->t_rttmin > rtt + 2)
   1742 		rttmin = tp->t_rttmin;
   1743 	else
   1744 		rttmin = rtt + 2;
   1745 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), rttmin, TCPTV_REXMTMAX);
   1746 
   1747 	/*
   1748 	 * We received an ack for a packet that wasn't retransmitted;
   1749 	 * it is probably safe to discard any error indications we've
   1750 	 * received recently.  This isn't quite right, but close enough
   1751 	 * for now (a route might have failed after we sent a segment,
   1752 	 * and the return path might not be symmetrical).
   1753 	 */
   1754 	tp->t_softerror = 0;
   1755 }
   1756 
   1757 /*
   1758  * TCP compressed state engine.  Currently used to hold compressed
   1759  * state for SYN_RECEIVED.
   1760  */
   1761 
   1762 u_long	syn_cache_count;
   1763 u_int32_t syn_hash1, syn_hash2;
   1764 
   1765 #define SYN_HASH(sa, sp, dp) \
   1766 	((((sa)->s_addr^syn_hash1)*(((((u_int32_t)(dp))<<16) + \
   1767 				     ((u_int32_t)(sp)))^syn_hash2)))
   1768 
   1769 LIST_HEAD(, syn_cache_head) tcp_syn_cache_queue;
   1770 
   1771 #define	SYN_CACHE_RM(sc, scp)						\
   1772 do {									\
   1773 	TAILQ_REMOVE(&(scp)->sch_queue, (sc), sc_queue);		\
   1774 	if (--(scp)->sch_length	== 0)					\
   1775 		LIST_REMOVE((scp), sch_headq);				\
   1776 	syn_cache_count--;						\
   1777 } while (0)
   1778 
   1779 struct pool syn_cache_pool;
   1780 
   1781 void
   1782 syn_cache_init()
   1783 {
   1784 	int i;
   1785 
   1786 	/* Initialize the hash bucket queues. */
   1787 	for (i = 0; i < tcp_syn_cache_size; i++)
   1788 		TAILQ_INIT(&tcp_syn_cache[i].sch_queue);
   1789 
   1790 	/* Initialize the active hash bucket cache. */
   1791 	LIST_INIT(&tcp_syn_cache_queue);
   1792 
   1793 	/* Initialize the syn cache pool. */
   1794 	pool_init(&syn_cache_pool, sizeof(struct syn_cache), 0, 0, 0,
   1795 	    "synpl", 0, NULL, NULL, M_PCB);
   1796 }
   1797 
   1798 void
   1799 syn_cache_insert(sc)
   1800 	struct syn_cache *sc;
   1801 {
   1802 	struct syn_cache_head *scp, *scp2, *sce;
   1803 	struct syn_cache *sc2;
   1804 	int s;
   1805 
   1806 	/*
   1807 	 * If there are no entries in the hash table, reinitialize
   1808 	 * the hash secrets.
   1809 	 */
   1810 	if (syn_cache_count == 0) {
   1811 		struct timeval tv;
   1812 		microtime(&tv);
   1813 		syn_hash1 = random() ^ (u_long)&sc;
   1814 		syn_hash2 = random() ^ tv.tv_usec;
   1815 	}
   1816 
   1817 	sc->sc_hash = SYN_HASH(&sc->sc_src, sc->sc_sport, sc->sc_dport);
   1818 	scp = &tcp_syn_cache[sc->sc_hash % tcp_syn_cache_size];
   1819 
   1820 	/*
   1821 	 * Make sure that we don't overflow the per-bucket
   1822 	 * limit or the total cache size limit.
   1823 	 */
   1824 	s = splsoftnet();
   1825 	if (scp->sch_length >= tcp_syn_bucket_limit) {
   1826 		tcpstat.tcps_sc_bucketoverflow++;
   1827 		/*
   1828 		 * The bucket is full.  Toss the first (i.e. oldest)
   1829 		 * element in this bucket.
   1830 		 */
   1831 		sc2 = TAILQ_FIRST(&scp->sch_queue);
   1832 		SYN_CACHE_RM(sc2, scp);
   1833 		if (sc2->sc_ipopts)
   1834 			(void) m_free(sc2->sc_ipopts);
   1835 		pool_put(&syn_cache_pool, sc2);
   1836 	} else if (syn_cache_count >= tcp_syn_cache_limit) {
   1837 		tcpstat.tcps_sc_overflowed++;
   1838 		/*
   1839 		 * The cache is full.  Toss the first (i.e. oldest)
   1840 		 * element in the first non-empty bucket we can find.
   1841 		 */
   1842 		scp2 = scp;
   1843 		if (TAILQ_FIRST(&scp2->sch_queue) == NULL) {
   1844 			sce = &tcp_syn_cache[tcp_syn_cache_size];
   1845 			for (++scp2; scp2 != scp; scp2++) {
   1846 				if (scp2 >= sce)
   1847 					scp2 = &tcp_syn_cache[0];
   1848 				if (TAILQ_FIRST(&scp2->sch_queue) != NULL)
   1849 					break;
   1850 			}
   1851 		}
   1852 		sc2 = TAILQ_FIRST(&scp2->sch_queue);
   1853 		if (sc2 == NULL) {
   1854 			if (sc->sc_ipopts)
   1855 				(void) m_free(sc->sc_ipopts);
   1856 			pool_put(&syn_cache_pool, sc);
   1857 			return;
   1858 		}
   1859 		SYN_CACHE_RM(sc2, scp2);
   1860 		if (sc2->sc_ipopts)
   1861 			(void) m_free(sc2->sc_ipopts);
   1862 		pool_put(&syn_cache_pool, sc2);
   1863 	}
   1864 
   1865 	/* Set entry's timer. */
   1866 	PRT_SLOW_ARM(sc->sc_timer, tcp_syn_cache_timeo);
   1867 
   1868 	/* Put it into the bucket. */
   1869 	TAILQ_INSERT_TAIL(&scp->sch_queue, sc, sc_queue);
   1870 	if (++scp->sch_length == 1)
   1871 		LIST_INSERT_HEAD(&tcp_syn_cache_queue, scp, sch_headq);
   1872 	syn_cache_count++;
   1873 
   1874 	tcpstat.tcps_sc_added++;
   1875 	splx(s);
   1876 }
   1877 
   1878 /*
   1879  * Walk down the cache list, looking for expired entries in each bucket.
   1880  */
   1881 void
   1882 syn_cache_timer()
   1883 {
   1884 	struct syn_cache_head *scp, *nscp;
   1885 	struct syn_cache *sc, *nsc;
   1886 	int s;
   1887 
   1888 	s = splsoftnet();
   1889 	for (scp = LIST_FIRST(&tcp_syn_cache_queue); scp != NULL; scp = nscp) {
   1890 #ifdef DIAGNOSTIC
   1891 		if (TAILQ_FIRST(&scp->sch_queue) == NULL)
   1892 			panic("syn_cache_timer: queue inconsistency");
   1893 #endif
   1894 		nscp = LIST_NEXT(scp, sch_headq);
   1895 		for (sc = TAILQ_FIRST(&scp->sch_queue);
   1896 		     sc != NULL && PRT_SLOW_ISEXPIRED(sc->sc_timer);
   1897 		     sc = nsc) {
   1898 			nsc = TAILQ_NEXT(sc, sc_queue);
   1899 			tcpstat.tcps_sc_timed_out++;
   1900 			SYN_CACHE_RM(sc, scp);
   1901 			if (sc->sc_ipopts)
   1902 				(void) m_free(sc->sc_ipopts);
   1903 			pool_put(&syn_cache_pool, sc);
   1904 		}
   1905 	}
   1906 	splx(s);
   1907 }
   1908 
   1909 /*
   1910  * Find an entry in the syn cache.
   1911  */
   1912 struct syn_cache *
   1913 syn_cache_lookup(ti, headp)
   1914 	struct tcpiphdr *ti;
   1915 	struct syn_cache_head **headp;
   1916 {
   1917 	struct syn_cache *sc;
   1918 	struct syn_cache_head *scp;
   1919 	u_int32_t hash;
   1920 	int s;
   1921 
   1922 	hash = SYN_HASH(&ti->ti_src, ti->ti_sport, ti->ti_dport);
   1923 
   1924 	scp = &tcp_syn_cache[hash % tcp_syn_cache_size];
   1925 	*headp = scp;
   1926 	s = splsoftnet();
   1927 	for (sc = TAILQ_FIRST(&scp->sch_queue); sc != NULL;
   1928 	     sc = TAILQ_NEXT(sc, sc_queue)) {
   1929 		if (sc->sc_hash != hash)
   1930 			continue;
   1931 		if (sc->sc_src.s_addr == ti->ti_src.s_addr &&
   1932 		    sc->sc_sport == ti->ti_sport &&
   1933 		    sc->sc_dport == ti->ti_dport &&
   1934 		    sc->sc_dst.s_addr == ti->ti_dst.s_addr) {
   1935 			splx(s);
   1936 			return (sc);
   1937 		}
   1938 	}
   1939 	splx(s);
   1940 	return (NULL);
   1941 }
   1942 
   1943 /*
   1944  * This function gets called when we receive an ACK for a
   1945  * socket in the LISTEN state.  We look up the connection
   1946  * in the syn cache, and if its there, we pull it out of
   1947  * the cache and turn it into a full-blown connection in
   1948  * the SYN-RECEIVED state.
   1949  *
   1950  * The return values may not be immediately obvious, and their effects
   1951  * can be subtle, so here they are:
   1952  *
   1953  *	NULL	SYN was not found in cache; caller should drop the
   1954  *		packet and send an RST.
   1955  *
   1956  *	-1	We were unable to create the new connection, and are
   1957  *		aborting it.  An ACK,RST is being sent to the peer
   1958  *		(unless we got screwey sequence numbners; see below),
   1959  *		because the 3-way handshake has been completed.  Caller
   1960  *		should not free the mbuf, since we may be using it.  If
   1961  *		we are not, we will free it.
   1962  *
   1963  *	Otherwise, the return value is a pointer to the new socket
   1964  *	associated with the connection.
   1965  */
   1966 struct socket *
   1967 syn_cache_get(so, m)
   1968 	struct socket *so;
   1969 	struct mbuf *m;
   1970 {
   1971 	struct syn_cache *sc;
   1972 	struct syn_cache_head *scp;
   1973 	register struct inpcb *inp;
   1974 	register struct tcpcb *tp = 0;
   1975 	register struct tcpiphdr *ti;
   1976 	struct sockaddr_in *sin;
   1977 	struct mbuf *am;
   1978 	long win;
   1979 	int s;
   1980 
   1981 	ti = mtod(m, struct tcpiphdr *);
   1982 	s = splsoftnet();
   1983 	if ((sc = syn_cache_lookup(ti, &scp)) == NULL) {
   1984 		splx(s);
   1985 		return (NULL);
   1986 	}
   1987 
   1988 	win = sbspace(&so->so_rcv);
   1989 	if (win > TCP_MAXWIN)
   1990 		win = TCP_MAXWIN;
   1991 
   1992 	/*
   1993 	 * Verify the sequence and ack numbers.
   1994 	 */
   1995 	if ((ti->ti_ack != sc->sc_iss + 1) ||
   1996 	    SEQ_LEQ(ti->ti_seq, sc->sc_irs) ||
   1997 	    SEQ_GT(ti->ti_seq, sc->sc_irs + 1 + win)) {
   1998 		(void) syn_cache_respond(sc, m, ti, win, 0);
   1999 		splx(s);
   2000 		return ((struct socket *)(-1));
   2001 	}
   2002 
   2003 	/* Remove this cache entry */
   2004 	SYN_CACHE_RM(sc, scp);
   2005 	splx(s);
   2006 
   2007 	/*
   2008 	 * Ok, create the full blown connection, and set things up
   2009 	 * as they would have been set up if we had created the
   2010 	 * connection when the SYN arrived.  If we can't create
   2011 	 * the connection, abort it.
   2012 	 */
   2013 	so = sonewconn(so, SS_ISCONNECTED);
   2014 	if (so == NULL)
   2015 		goto resetandabort;
   2016 
   2017 	inp = sotoinpcb(so);
   2018 	inp->inp_laddr = sc->sc_dst;
   2019 	inp->inp_lport = sc->sc_dport;
   2020 	in_pcbstate(inp, INP_BOUND);
   2021 	inp->inp_options = ip_srcroute();
   2022 	if (inp->inp_options == NULL) {
   2023 		inp->inp_options = sc->sc_ipopts;
   2024 		sc->sc_ipopts = NULL;
   2025 	}
   2026 
   2027 	am = m_get(M_DONTWAIT, MT_SONAME);	/* XXX */
   2028 	if (am == NULL)
   2029 		goto resetandabort;
   2030 	am->m_len = sizeof(struct sockaddr_in);
   2031 	sin = mtod(am, struct sockaddr_in *);
   2032 	sin->sin_family = AF_INET;
   2033 	sin->sin_len = sizeof(*sin);
   2034 	sin->sin_addr = sc->sc_src;
   2035 	sin->sin_port = sc->sc_sport;
   2036 	bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero));
   2037 	if (in_pcbconnect(inp, am)) {
   2038 		(void) m_free(am);
   2039 		goto resetandabort;
   2040 	}
   2041 	(void) m_free(am);
   2042 
   2043 	tp = intotcpcb(inp);
   2044 	if (sc->sc_request_r_scale != 15) {
   2045 		tp->requested_s_scale = sc->sc_requested_s_scale;
   2046 		tp->request_r_scale = sc->sc_request_r_scale;
   2047 		tp->snd_scale = sc->sc_requested_s_scale;
   2048 		tp->rcv_scale = sc->sc_request_r_scale;
   2049 		tp->t_flags |= TF_RCVD_SCALE;
   2050 	}
   2051 	if (sc->sc_flags & SCF_TIMESTAMP)
   2052 		tp->t_flags |= TF_RCVD_TSTMP;
   2053 
   2054 	tp->t_template = tcp_template(tp);
   2055 	if (tp->t_template == 0) {
   2056 		tp = tcp_drop(tp, ENOBUFS);	/* destroys socket */
   2057 		so = NULL;
   2058 		m_freem(m);
   2059 		goto abort;
   2060 	}
   2061 
   2062 	tp->iss = sc->sc_iss;
   2063 	tp->irs = sc->sc_irs;
   2064 	tcp_sendseqinit(tp);
   2065 	tcp_rcvseqinit(tp);
   2066 	tp->t_state = TCPS_SYN_RECEIVED;
   2067 	TCP_TIMER_ARM(tp, TCPT_KEEP, TCPTV_KEEP_INIT);
   2068 	tcpstat.tcps_accepts++;
   2069 
   2070 	/* Initialize tp->t_ourmss before we deal with the peer's! */
   2071 	tp->t_ourmss = sc->sc_ourmaxseg;
   2072 	tcp_mss_from_peer(tp, sc->sc_peermaxseg);
   2073 
   2074 	/*
   2075 	 * Initialize the initial congestion window.  If we
   2076 	 * had to retransmit the SYN,ACK, we must initialize cwnd
   2077 	 * to 1 segment (i.e. the Loss Window).
   2078 	 */
   2079 	if (sc->sc_rexmt_count)
   2080 		tp->snd_cwnd = tp->t_peermss;
   2081 	else
   2082 		tp->snd_cwnd = TCP_INITIAL_WINDOW(tcp_init_win, tp->t_peermss);
   2083 
   2084 	tcp_rmx_rtt(tp);
   2085 	tp->snd_wl1 = sc->sc_irs;
   2086 	tp->rcv_up = sc->sc_irs + 1;
   2087 
   2088 	/*
   2089 	 * This is what whould have happened in tcp_ouput() when
   2090 	 * the SYN,ACK was sent.
   2091 	 */
   2092 	tp->snd_up = tp->snd_una;
   2093 	tp->snd_max = tp->snd_nxt = tp->iss+1;
   2094 	TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
   2095 	if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
   2096 		tp->rcv_adv = tp->rcv_nxt + win;
   2097 	tp->last_ack_sent = tp->rcv_nxt;
   2098 
   2099 	tcpstat.tcps_sc_completed++;
   2100 	if (sc->sc_ipopts)
   2101 		(void) m_free(sc->sc_ipopts);
   2102 	pool_put(&syn_cache_pool, sc);
   2103 	return (so);
   2104 
   2105 resetandabort:
   2106 	(void) tcp_respond(NULL, ti, m, ti->ti_seq+ti->ti_len,
   2107 	    (tcp_seq)0, TH_RST|TH_ACK);
   2108 abort:
   2109 	if (so != NULL)
   2110 		(void) soabort(so);
   2111 	if (sc->sc_ipopts)
   2112 		(void) m_free(sc->sc_ipopts);
   2113 	pool_put(&syn_cache_pool, sc);
   2114 	tcpstat.tcps_sc_aborted++;
   2115 	return ((struct socket *)(-1));
   2116 }
   2117 
   2118 /*
   2119  * This function is called when we get a RST for a
   2120  * non-existant connection, so that we can see if the
   2121  * connection is in the syn cache.  If it is, zap it.
   2122  */
   2123 
   2124 void
   2125 syn_cache_reset(ti)
   2126 	register struct tcpiphdr *ti;
   2127 {
   2128 	struct syn_cache *sc;
   2129 	struct syn_cache_head *scp;
   2130 	int s = splsoftnet();
   2131 
   2132 	if ((sc = syn_cache_lookup(ti, &scp)) == NULL) {
   2133 		splx(s);
   2134 		return;
   2135 	}
   2136 	if (SEQ_LT(ti->ti_seq,sc->sc_irs) ||
   2137 	    SEQ_GT(ti->ti_seq, sc->sc_irs+1)) {
   2138 		splx(s);
   2139 		return;
   2140 	}
   2141 	SYN_CACHE_RM(sc, scp);
   2142 	splx(s);
   2143 	tcpstat.tcps_sc_reset++;
   2144 	if (sc->sc_ipopts)
   2145 		(void) m_free(sc->sc_ipopts);
   2146 	pool_put(&syn_cache_pool, sc);
   2147 }
   2148 
   2149 void
   2150 syn_cache_unreach(ip, th)
   2151 	struct ip *ip;
   2152 	struct tcphdr *th;
   2153 {
   2154 	struct syn_cache *sc;
   2155 	struct syn_cache_head *scp;
   2156 	struct tcpiphdr ti2;
   2157 	int s;
   2158 
   2159 	ti2.ti_src.s_addr = ip->ip_dst.s_addr;
   2160 	ti2.ti_dst.s_addr = ip->ip_src.s_addr;
   2161 	ti2.ti_sport = th->th_dport;
   2162 	ti2.ti_dport = th->th_sport;
   2163 
   2164 	s = splsoftnet();
   2165 	if ((sc = syn_cache_lookup(&ti2, &scp)) == NULL) {
   2166 		splx(s);
   2167 		return;
   2168 	}
   2169 	/* If the sequence number != sc_iss, then it's a bogus ICMP msg */
   2170 	if (ntohl (th->th_seq) != sc->sc_iss) {
   2171 		splx(s);
   2172 		return;
   2173 	}
   2174 
   2175 	/*
   2176 	 * If we've rertransmitted 3 times and this is our second error,
   2177 	 * we remove the entry.  Otherwise, we allow it to continue on.
   2178 	 * This prevents us from incorrectly nuking an entry during a
   2179 	 * spurious network outage.
   2180 	 *
   2181 	 * See tcp_notify().
   2182 	 */
   2183 	if ((sc->sc_flags & SCF_UNREACH) == 0 || sc->sc_rexmt_count < 3) {
   2184 		sc->sc_flags |= SCF_UNREACH;
   2185 		splx(s);
   2186 		return;
   2187 	}
   2188 
   2189 	SYN_CACHE_RM(sc, scp);
   2190 	splx(s);
   2191 	tcpstat.tcps_sc_unreach++;
   2192 	if (sc->sc_ipopts)
   2193 		(void) m_free(sc->sc_ipopts);
   2194 	pool_put(&syn_cache_pool, sc);
   2195 }
   2196 
   2197 /*
   2198  * Given a LISTEN socket and an inbound SYN request, add
   2199  * this to the syn cache, and send back a segment:
   2200  *	<SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
   2201  * to the source.
   2202  *
   2203  * IMPORTANT NOTE: We do _NOT_ ACK data that might accompany the SYN.
   2204  * Doing so would require that we hold onto the data and deliver it
   2205  * to the application.  However, if we are the target of a SYN-flood
   2206  * DoS attack, an attacker could send data which would eventually
   2207  * consume all available buffer space if it were ACKed.  By not ACKing
   2208  * the data, we avoid this DoS scenario.
   2209  */
   2210 
   2211 int
   2212 syn_cache_add(so, m, optp, optlen, oi)
   2213 	struct socket *so;
   2214 	struct mbuf *m;
   2215 	u_char *optp;
   2216 	int optlen;
   2217 	struct tcp_opt_info *oi;
   2218 {
   2219 	register struct tcpiphdr *ti;
   2220 	struct tcpcb tb, *tp;
   2221 	long win;
   2222 	struct syn_cache *sc;
   2223 	struct syn_cache_head *scp;
   2224 	struct mbuf *ipopts;
   2225 
   2226 	tp = sototcpcb(so);
   2227 	ti = mtod(m, struct tcpiphdr *);
   2228 
   2229 	/*
   2230 	 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
   2231 	 * in_broadcast() should never return true on a received
   2232 	 * packet with M_BCAST not set.
   2233 	 */
   2234 	if (m->m_flags & (M_BCAST|M_MCAST) ||
   2235 	    IN_MULTICAST(ti->ti_src.s_addr) ||
   2236 	    IN_MULTICAST(ti->ti_dst.s_addr))
   2237 		return (0);
   2238 
   2239 	/*
   2240 	 * Initialize some local state.
   2241 	 */
   2242 	win = sbspace(&so->so_rcv);
   2243 	if (win > TCP_MAXWIN)
   2244 		win = TCP_MAXWIN;
   2245 
   2246 	/*
   2247 	 * Remember the IP options, if any.
   2248 	 */
   2249 	ipopts = ip_srcroute();
   2250 
   2251 	if (optp) {
   2252 		tb.t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0;
   2253 		tcp_dooptions(&tb, optp, optlen, ti, oi);
   2254 	} else
   2255 		tb.t_flags = 0;
   2256 
   2257 	/*
   2258 	 * See if we already have an entry for this connection.
   2259 	 * If we do, resend the SYN,ACK, and remember since the
   2260 	 * initial congestion window must be initialized to 1
   2261 	 * segment when the connection completes.
   2262 	 */
   2263 	if ((sc = syn_cache_lookup(ti, &scp)) != NULL) {
   2264 		tcpstat.tcps_sc_dupesyn++;
   2265 		sc->sc_rexmt_count++;
   2266 		if (sc->sc_rexmt_count == 0) {
   2267 			/*
   2268 			 * Eeek!  We rolled the counter.  Just set it
   2269 			 * to the max value.  This shouldn't ever happen,
   2270 			 * but there's no real reason to panic here, since
   2271 			 * the count doesn't have to be very precise.
   2272 			 */
   2273 			sc->sc_rexmt_count = USHRT_MAX;
   2274 		}
   2275 
   2276 		if (ipopts) {
   2277 			/*
   2278 			 * If we were remembering a previous source route,
   2279 			 * forget it and use the new one we've been given.
   2280 			 */
   2281 			if (sc->sc_ipopts)
   2282 				(void) m_free(sc->sc_ipopts);
   2283 			sc->sc_ipopts = ipopts;
   2284 		}
   2285 
   2286 		if (syn_cache_respond(sc, m, ti, win, tb.ts_recent) == 0) {
   2287 			tcpstat.tcps_sndacks++;
   2288 			tcpstat.tcps_sndtotal++;
   2289 		}
   2290 		return (1);
   2291 	}
   2292 
   2293 	sc = pool_get(&syn_cache_pool, PR_NOWAIT);
   2294 	if (sc == NULL) {
   2295 		if (ipopts)
   2296 			(void) m_free(ipopts);
   2297 		return (0);
   2298 	}
   2299 
   2300 	/*
   2301 	 * Fill in the cache, and put the necessary IP and TCP
   2302 	 * options into the reply.
   2303 	 */
   2304 	sc->sc_src.s_addr = ti->ti_src.s_addr;
   2305 	sc->sc_dst.s_addr = ti->ti_dst.s_addr;
   2306 	sc->sc_sport = ti->ti_sport;
   2307 	sc->sc_dport = ti->ti_dport;
   2308 	sc->sc_flags = 0;
   2309 	sc->sc_ipopts = ipopts;
   2310 	sc->sc_irs = ti->ti_seq;
   2311 	sc->sc_iss = tcp_new_iss(sc, sizeof(struct syn_cache), 0);
   2312 	sc->sc_peermaxseg = oi->maxseg;
   2313 	sc->sc_ourmaxseg = tcp_mss_to_advertise(m->m_flags & M_PKTHDR ?
   2314 						m->m_pkthdr.rcvif : NULL);
   2315 	if (tcp_do_rfc1323 && (tb.t_flags & TF_RCVD_TSTMP))
   2316 		sc->sc_flags |= SCF_TIMESTAMP;
   2317 	if ((tb.t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
   2318 	    (TF_RCVD_SCALE|TF_REQ_SCALE)) {
   2319 		sc->sc_requested_s_scale = tb.requested_s_scale;
   2320 		sc->sc_request_r_scale = 0;
   2321 		while (sc->sc_request_r_scale < TCP_MAX_WINSHIFT &&
   2322 		    TCP_MAXWIN << sc->sc_request_r_scale <
   2323 		    so->so_rcv.sb_hiwat)
   2324 			sc->sc_request_r_scale++;
   2325 	} else {
   2326 		sc->sc_requested_s_scale = 15;
   2327 		sc->sc_request_r_scale = 15;
   2328 	}
   2329 	if (syn_cache_respond(sc, m, ti, win, tb.ts_recent) == 0) {
   2330 		syn_cache_insert(sc);
   2331 		tcpstat.tcps_sndacks++;
   2332 		tcpstat.tcps_sndtotal++;
   2333 	} else {
   2334 		if (sc->sc_ipopts)
   2335 			(void) m_free(sc->sc_ipopts);
   2336 		pool_put(&syn_cache_pool, sc);
   2337 		tcpstat.tcps_sc_dropped++;
   2338 	}
   2339 	return (1);
   2340 }
   2341 
   2342 int
   2343 syn_cache_respond(sc, m, ti, win, ts)
   2344 	struct syn_cache *sc;
   2345 	struct mbuf *m;
   2346 	register struct tcpiphdr *ti;
   2347 	long win;
   2348 	u_long ts;
   2349 {
   2350 	u_int8_t *optp;
   2351 	int optlen;
   2352 
   2353 	/*
   2354 	 * Tack on the TCP options.  If there isn't enough trailing
   2355 	 * space for them, move up the fixed header to make space.
   2356 	 */
   2357 	optlen = 4 + (sc->sc_request_r_scale != 15 ? 4 : 0) +
   2358 	    ((sc->sc_flags & SCF_TIMESTAMP) ? TCPOLEN_TSTAMP_APPA : 0);
   2359 	if (optlen > M_TRAILINGSPACE(m)) {
   2360 		if (M_LEADINGSPACE(m) >= optlen) {
   2361 			m->m_data -= optlen;
   2362 			m->m_len += optlen;
   2363 		} else {
   2364 			struct mbuf *m0 = m;
   2365 			if ((m = m_gethdr(M_DONTWAIT, MT_HEADER)) == NULL) {
   2366 				m_freem(m0);
   2367 				return (ENOBUFS);
   2368 			}
   2369 			MH_ALIGN(m, sizeof(*ti) + optlen);
   2370 			m->m_next = m0; /* this gets freed below */
   2371 		}
   2372 		bcopy((caddr_t)ti, mtod(m, caddr_t), sizeof(*ti));
   2373 		ti = mtod(m, struct tcpiphdr *);
   2374 	}
   2375 
   2376 	optp = (u_int8_t *)(ti + 1);
   2377 	optp[0] = TCPOPT_MAXSEG;
   2378 	optp[1] = 4;
   2379 	optp[2] = (sc->sc_ourmaxseg >> 8) & 0xff;
   2380 	optp[3] = sc->sc_ourmaxseg & 0xff;
   2381 	optlen = 4;
   2382 
   2383 	if (sc->sc_request_r_scale != 15) {
   2384 		*((u_int32_t *)(optp + optlen)) = htonl(TCPOPT_NOP << 24 |
   2385 		    TCPOPT_WINDOW << 16 | TCPOLEN_WINDOW << 8 |
   2386 		    sc->sc_request_r_scale);
   2387 		optlen += 4;
   2388 	}
   2389 
   2390 	if (sc->sc_flags & SCF_TIMESTAMP) {
   2391 		u_int32_t *lp = (u_int32_t *)(optp + optlen);
   2392 		/* Form timestamp option as shown in appendix A of RFC 1323. */
   2393 		*lp++ = htonl(TCPOPT_TSTAMP_HDR);
   2394 		*lp++ = htonl(tcp_now);
   2395 		*lp   = htonl(ts);
   2396 		optlen += TCPOLEN_TSTAMP_APPA;
   2397 	}
   2398 
   2399 	/*
   2400 	 * Toss any trailing mbufs.  No need to worry about
   2401 	 * m_len and m_pkthdr.len, since tcp_respond() will
   2402 	 * unconditionally set them.
   2403 	 */
   2404 	if (m->m_next) {
   2405 		m_freem(m->m_next);
   2406 		m->m_next = NULL;
   2407   	}
   2408 
   2409 	/*
   2410 	 * Fill in the fields that tcp_respond() will not touch, and
   2411 	 * then send the response.
   2412 	 */
   2413 	ti->ti_off = (sizeof(struct tcphdr) + optlen) >> 2;
   2414 	ti->ti_win = htons(win);
   2415 	return (tcp_respond(NULL, ti, m, sc->sc_irs + 1, sc->sc_iss,
   2416 	    TH_SYN|TH_ACK));
   2417 }
   2418