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