xref: /freebsd/sys/netinet/tcp_input.c (revision 99e8005137088aafb1350e23b113d69b01b0820f)
1 /*
2  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)tcp_input.c	8.12 (Berkeley) 5/24/95
34  * $FreeBSD$
35  */
36 
37 #include "opt_ipfw.h"		/* for ipfw_fwd		*/
38 #include "opt_inet6.h"
39 #include "opt_ipsec.h"
40 #include "opt_tcpdebug.h"
41 #include "opt_tcp_input.h"
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/sysctl.h>
47 #include <sys/malloc.h>
48 #include <sys/mbuf.h>
49 #include <sys/proc.h>		/* for proc0 declaration */
50 #include <sys/protosw.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/syslog.h>
54 
55 #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
56 
57 #include <net/if.h>
58 #include <net/route.h>
59 
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/ip.h>
63 #include <netinet/ip_icmp.h>	/* for ICMP_BANDLIM		*/
64 #include <netinet/in_var.h>
65 #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM		*/
66 #include <netinet/in_pcb.h>
67 #include <netinet/ip_var.h>
68 #ifdef INET6
69 #include <netinet/ip6.h>
70 #include <netinet/icmp6.h>
71 #include <netinet6/nd6.h>
72 #include <netinet6/ip6_var.h>
73 #include <netinet6/in6_pcb.h>
74 #endif
75 #include <netinet/tcp.h>
76 #include <netinet/tcp_fsm.h>
77 #include <netinet/tcp_seq.h>
78 #include <netinet/tcp_timer.h>
79 #include <netinet/tcp_var.h>
80 #ifdef INET6
81 #include <netinet6/tcp6_var.h>
82 #endif
83 #include <netinet/tcpip.h>
84 #ifdef TCPDEBUG
85 #include <netinet/tcp_debug.h>
86 
87 u_char tcp_saveipgen[40]; /* the size must be of max ip header, now IPv6 */
88 struct tcphdr tcp_savetcp;
89 #endif /* TCPDEBUG */
90 
91 #ifdef IPSEC
92 #include <netinet6/ipsec.h>
93 #ifdef INET6
94 #include <netinet6/ipsec6.h>
95 #endif
96 #include <netkey/key.h>
97 #endif /*IPSEC*/
98 
99 #include <machine/in_cksum.h>
100 
101 MALLOC_DEFINE(M_TSEGQ, "tseg_qent", "TCP segment queue entry");
102 
103 static int	tcprexmtthresh = 3;
104 tcp_seq	tcp_iss;
105 tcp_cc	tcp_ccgen;
106 
107 struct	tcpstat tcpstat;
108 SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RD,
109     &tcpstat , tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
110 
111 static int log_in_vain = 0;
112 SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW,
113     &log_in_vain, 0, "Log all incoming TCP connections");
114 
115 static int blackhole = 0;
116 SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW,
117 	&blackhole, 0, "Do not send RST when dropping refused connections");
118 
119 int tcp_delack_enabled = 1;
120 SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW,
121     &tcp_delack_enabled, 0,
122     "Delay ACK to try and piggyback it onto a data packet");
123 
124 int tcp_lq_overflow = 1;
125 SYSCTL_INT(_net_inet_tcp, OID_AUTO, tcp_lq_overflow, CTLFLAG_RW,
126     &tcp_lq_overflow, 0,
127     "Listen Queue Overflow");
128 
129 #ifdef TCP_DROP_SYNFIN
130 static int drop_synfin = 0;
131 SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW,
132     &drop_synfin, 0, "Drop TCP packets with SYN+FIN set");
133 #endif
134 
135 struct inpcbhead tcb;
136 #define	tcb6	tcb  /* for KAME src sync over BSD*'s */
137 struct inpcbinfo tcbinfo;
138 
139 static void	 tcp_dooptions __P((struct tcpcb *,
140 	    u_char *, int, struct tcphdr *, struct tcpopt *));
141 static void	 tcp_pulloutofband __P((struct socket *,
142 	    struct tcphdr *, struct mbuf *, int));
143 static int	 tcp_reass __P((struct tcpcb *, struct tcphdr *, int *,
144 				struct mbuf *));
145 static void	 tcp_xmit_timer __P((struct tcpcb *, int));
146 static int	 tcp_newreno __P((struct tcpcb *, struct tcphdr *));
147 
148 /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */
149 #ifdef INET6
150 #define ND6_HINT(tp) \
151 do { \
152 	if ((tp) && (tp)->t_inpcb && \
153 	    ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0 && \
154 	    (tp)->t_inpcb->in6p_route.ro_rt) \
155 		nd6_nud_hint((tp)->t_inpcb->in6p_route.ro_rt, NULL, 0); \
156 } while (0)
157 #else
158 #define ND6_HINT(tp)
159 #endif
160 
161 /*
162  * Indicate whether this ack should be delayed.
163  */
164 #define DELAY_ACK(tp) \
165 	(tcp_delack_enabled && !callout_pending(tp->tt_delack))
166 
167 /*
168  * Insert segment which inludes th into reassembly queue of tcp with
169  * control block tp.  Return TH_FIN if reassembly now includes
170  * a segment with FIN.  The macro form does the common case inline
171  * (segment is the next to be received on an established connection,
172  * and the queue is empty), avoiding linkage into and removal
173  * from the queue and repetition of various conversions.
174  * Set DELACK for segments received in order, but ack immediately
175  * when segments are out of order (so fast retransmit can work).
176  */
177 #define	TCP_REASS(tp, th, tlenp, m, so, flags) { \
178 	if ((th)->th_seq == (tp)->rcv_nxt && \
179 	    LIST_EMPTY(&(tp)->t_segq) && \
180 	    (tp)->t_state == TCPS_ESTABLISHED) { \
181 		if (DELAY_ACK(tp)) \
182 			callout_reset(tp->tt_delack, tcp_delacktime, \
183 			    tcp_timer_delack, tp); \
184 		else \
185 			tp->t_flags |= TF_ACKNOW; \
186 		(tp)->rcv_nxt += *(tlenp); \
187 		flags = (th)->th_flags & TH_FIN; \
188 		tcpstat.tcps_rcvpack++;\
189 		tcpstat.tcps_rcvbyte += *(tlenp);\
190 		ND6_HINT(tp); \
191 		sbappend(&(so)->so_rcv, (m)); \
192 		sorwakeup(so); \
193 	} else { \
194 		(flags) = tcp_reass((tp), (th), (tlenp), (m)); \
195 		tp->t_flags |= TF_ACKNOW; \
196 	} \
197 }
198 
199 static int
200 tcp_reass(tp, th, tlenp, m)
201 	register struct tcpcb *tp;
202 	register struct tcphdr *th;
203 	int *tlenp;
204 	struct mbuf *m;
205 {
206 	struct tseg_qent *q;
207 	struct tseg_qent *p = NULL;
208 	struct tseg_qent *nq;
209 	struct tseg_qent *te;
210 	struct socket *so = tp->t_inpcb->inp_socket;
211 	int flags;
212 
213 	/*
214 	 * Call with th==0 after become established to
215 	 * force pre-ESTABLISHED data up to user socket.
216 	 */
217 	if (th == 0)
218 		goto present;
219 
220 	/* Allocate a new queue entry. If we can't, just drop the pkt. XXX */
221 	MALLOC(te, struct tseg_qent *, sizeof (struct tseg_qent), M_TSEGQ,
222 	       M_NOWAIT);
223 	if (te == NULL) {
224 		tcpstat.tcps_rcvmemdrop++;
225 		m_freem(m);
226 		return (0);
227 	}
228 
229 	/*
230 	 * Find a segment which begins after this one does.
231 	 */
232 	LIST_FOREACH(q, &tp->t_segq, tqe_q) {
233 		if (SEQ_GT(q->tqe_th->th_seq, th->th_seq))
234 			break;
235 		p = q;
236 	}
237 
238 	/*
239 	 * If there is a preceding segment, it may provide some of
240 	 * our data already.  If so, drop the data from the incoming
241 	 * segment.  If it provides all of our data, drop us.
242 	 */
243 	if (p != NULL) {
244 		register int i;
245 		/* conversion to int (in i) handles seq wraparound */
246 		i = p->tqe_th->th_seq + p->tqe_len - th->th_seq;
247 		if (i > 0) {
248 			if (i >= *tlenp) {
249 				tcpstat.tcps_rcvduppack++;
250 				tcpstat.tcps_rcvdupbyte += *tlenp;
251 				m_freem(m);
252 				FREE(te, M_TSEGQ);
253 				/*
254 				 * Try to present any queued data
255 				 * at the left window edge to the user.
256 				 * This is needed after the 3-WHS
257 				 * completes.
258 				 */
259 				goto present;	/* ??? */
260 			}
261 			m_adj(m, i);
262 			*tlenp -= i;
263 			th->th_seq += i;
264 		}
265 	}
266 	tcpstat.tcps_rcvoopack++;
267 	tcpstat.tcps_rcvoobyte += *tlenp;
268 
269 	/*
270 	 * While we overlap succeeding segments trim them or,
271 	 * if they are completely covered, dequeue them.
272 	 */
273 	while (q) {
274 		register int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq;
275 		if (i <= 0)
276 			break;
277 		if (i < q->tqe_len) {
278 			q->tqe_th->th_seq += i;
279 			q->tqe_len -= i;
280 			m_adj(q->tqe_m, i);
281 			break;
282 		}
283 
284 		nq = LIST_NEXT(q, tqe_q);
285 		LIST_REMOVE(q, tqe_q);
286 		m_freem(q->tqe_m);
287 		FREE(q, M_TSEGQ);
288 		q = nq;
289 	}
290 
291 	/* Insert the new segment queue entry into place. */
292 	te->tqe_m = m;
293 	te->tqe_th = th;
294 	te->tqe_len = *tlenp;
295 
296 	if (p == NULL) {
297 		LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q);
298 	} else {
299 		LIST_INSERT_AFTER(p, te, tqe_q);
300 	}
301 
302 present:
303 	/*
304 	 * Present data to user, advancing rcv_nxt through
305 	 * completed sequence space.
306 	 */
307 	if (!TCPS_HAVEESTABLISHED(tp->t_state))
308 		return (0);
309 	q = LIST_FIRST(&tp->t_segq);
310 	if (!q || q->tqe_th->th_seq != tp->rcv_nxt)
311 		return (0);
312 	do {
313 		tp->rcv_nxt += q->tqe_len;
314 		flags = q->tqe_th->th_flags & TH_FIN;
315 		nq = LIST_NEXT(q, tqe_q);
316 		LIST_REMOVE(q, tqe_q);
317 		if (so->so_state & SS_CANTRCVMORE)
318 			m_freem(q->tqe_m);
319 		else
320 			sbappend(&so->so_rcv, q->tqe_m);
321 		FREE(q, M_TSEGQ);
322 		q = nq;
323 	} while (q && q->tqe_th->th_seq == tp->rcv_nxt);
324 	ND6_HINT(tp);
325 	sorwakeup(so);
326 	return (flags);
327 }
328 
329 /*
330  * TCP input routine, follows pages 65-76 of the
331  * protocol specification dated September, 1981 very closely.
332  */
333 #ifdef INET6
334 int
335 tcp6_input(mp, offp, proto)
336 	struct mbuf **mp;
337 	int *offp, proto;
338 {
339 	register struct mbuf *m = *mp;
340 
341 	IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE);
342 
343 	/*
344 	 * draft-itojun-ipv6-tcp-to-anycast
345 	 * better place to put this in?
346 	 */
347 	if (m->m_flags & M_ANYCAST6) {
348 		struct ip6_hdr *ip6;
349 
350 		ip6 = mtod(m, struct ip6_hdr *);
351 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
352 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
353 		return IPPROTO_DONE;
354 	}
355 
356 	tcp_input(m, *offp, proto);
357 	return IPPROTO_DONE;
358 }
359 #endif
360 
361 void
362 tcp_input(m, off0, proto)
363 	register struct mbuf *m;
364 	int off0, proto;
365 {
366 	register struct tcphdr *th;
367 	register struct ip *ip = NULL;
368 	register struct ipovly *ipov;
369 	register struct inpcb *inp;
370 	u_char *optp = NULL;
371 	int optlen = 0;
372 	int len, tlen, off;
373 	int drop_hdrlen;
374 	register struct tcpcb *tp = 0;
375 	register int thflags;
376 	struct socket *so = 0;
377 	int todrop, acked, ourfinisacked, needoutput = 0;
378 	struct in_addr laddr;
379 #ifdef INET6
380 	struct in6_addr laddr6;
381 #endif
382 	int dropsocket = 0;
383 	int iss = 0;
384 	u_long tiwin;
385 	struct tcpopt to;		/* options in this segment */
386 	struct rmxp_tao *taop;		/* pointer to our TAO cache entry */
387 	struct rmxp_tao	tao_noncached;	/* in case there's no cached entry */
388 #ifdef TCPDEBUG
389 	short ostate = 0;
390 #endif
391 #ifdef INET6
392 	struct ip6_hdr *ip6 = NULL;
393 	int isipv6;
394 #endif /* INET6 */
395 	int rstreason; /* For badport_bandlim accounting purposes */
396 
397 #ifdef INET6
398 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
399 #endif
400 	bzero((char *)&to, sizeof(to));
401 
402 	tcpstat.tcps_rcvtotal++;
403 
404 #ifdef INET6
405 	if (isipv6) {
406 		/* IP6_EXTHDR_CHECK() is already done at tcp6_input() */
407 		ip6 = mtod(m, struct ip6_hdr *);
408 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
409 		if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) {
410 			tcpstat.tcps_rcvbadsum++;
411 			goto drop;
412 		}
413 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
414 	} else
415 #endif /* INET6 */
416       {
417 	/*
418 	 * Get IP and TCP header together in first mbuf.
419 	 * Note: IP leaves IP header in first mbuf.
420 	 */
421 	if (off0 > sizeof (struct ip)) {
422 		ip_stripoptions(m, (struct mbuf *)0);
423 		off0 = sizeof(struct ip);
424 	}
425 	if (m->m_len < sizeof (struct tcpiphdr)) {
426 		if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
427 			tcpstat.tcps_rcvshort++;
428 			return;
429 		}
430 	}
431 	ip = mtod(m, struct ip *);
432 	ipov = (struct ipovly *)ip;
433 	th = (struct tcphdr *)((caddr_t)ip + off0);
434 	tlen = ip->ip_len;
435 
436 	if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
437 		if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
438                 	th->th_sum = m->m_pkthdr.csum_data;
439 		else
440 	                th->th_sum = in_pseudo(ip->ip_src.s_addr,
441 			    ip->ip_dst.s_addr, htonl(m->m_pkthdr.csum_data +
442 			    ip->ip_len + IPPROTO_TCP));
443 		th->th_sum ^= 0xffff;
444 	} else {
445 		/*
446 		 * Checksum extended TCP header and data.
447 		 */
448 		len = sizeof (struct ip) + tlen;
449 		bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
450 		ipov->ih_len = (u_short)tlen;
451 		HTONS(ipov->ih_len);
452 		th->th_sum = in_cksum(m, len);
453 	}
454 	if (th->th_sum) {
455 		tcpstat.tcps_rcvbadsum++;
456 		goto drop;
457 	}
458 #ifdef INET6
459 	/* Re-initialization for later version check */
460 	ip->ip_v = IPVERSION;
461 #endif
462       }
463 
464 	/*
465 	 * Check that TCP offset makes sense,
466 	 * pull out TCP options and adjust length.		XXX
467 	 */
468 	off = th->th_off << 2;
469 	if (off < sizeof (struct tcphdr) || off > tlen) {
470 		tcpstat.tcps_rcvbadoff++;
471 		goto drop;
472 	}
473 	tlen -= off;	/* tlen is used instead of ti->ti_len */
474 	if (off > sizeof (struct tcphdr)) {
475 #ifdef INET6
476 		if (isipv6) {
477 			IP6_EXTHDR_CHECK(m, off0, off, );
478 			ip6 = mtod(m, struct ip6_hdr *);
479 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
480 		} else
481 #endif /* INET6 */
482 	      {
483 		if (m->m_len < sizeof(struct ip) + off) {
484 			if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
485 				tcpstat.tcps_rcvshort++;
486 				return;
487 			}
488 			ip = mtod(m, struct ip *);
489 			ipov = (struct ipovly *)ip;
490 			th = (struct tcphdr *)((caddr_t)ip + off0);
491 		}
492 	      }
493 		optlen = off - sizeof (struct tcphdr);
494 		optp = (u_char *)(th + 1);
495 	}
496 	thflags = th->th_flags;
497 
498 #ifdef TCP_DROP_SYNFIN
499 	/*
500 	 * If the drop_synfin option is enabled, drop all packets with
501 	 * both the SYN and FIN bits set. This prevents e.g. nmap from
502 	 * identifying the TCP/IP stack.
503 	 *
504 	 * This is a violation of the TCP specification.
505 	 */
506 	if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN))
507 		goto drop;
508 #endif
509 
510 	/*
511 	 * Convert TCP protocol specific fields to host format.
512 	 */
513 	NTOHL(th->th_seq);
514 	NTOHL(th->th_ack);
515 	NTOHS(th->th_win);
516 	NTOHS(th->th_urp);
517 
518 	/*
519 	 * Delay droping TCP, IP headers, IPv6 ext headers, and TCP options,
520 	 * until after ip6_savecontrol() is called and before other functions
521 	 * which don't want those proto headers.
522 	 * Because ip6_savecontrol() is going to parse the mbuf to
523 	 * search for data to be passed up to user-land, it wants mbuf
524 	 * parameters to be unchanged.
525 	 */
526 	drop_hdrlen = off0 + off;
527 
528 	/*
529 	 * Locate pcb for segment.
530 	 */
531 findpcb:
532 #ifdef IPFIREWALL_FORWARD
533 	if (ip_fw_fwd_addr != NULL
534 #ifdef INET6
535 	    && isipv6 == NULL /* IPv6 support is not yet */
536 #endif /* INET6 */
537 	    ) {
538 		/*
539 		 * Diverted. Pretend to be the destination.
540 		 * already got one like this?
541 		 */
542 		inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport,
543 			ip->ip_dst, th->th_dport, 0, m->m_pkthdr.rcvif);
544 		if (!inp) {
545 			/*
546 			 * No, then it's new. Try find the ambushing socket
547 			 */
548 			if (!ip_fw_fwd_addr->sin_port) {
549 				inp = in_pcblookup_hash(&tcbinfo, ip->ip_src,
550 				    th->th_sport, ip_fw_fwd_addr->sin_addr,
551 				    th->th_dport, 1, m->m_pkthdr.rcvif);
552 			} else {
553 				inp = in_pcblookup_hash(&tcbinfo,
554 				    ip->ip_src, th->th_sport,
555 	    			    ip_fw_fwd_addr->sin_addr,
556 				    ntohs(ip_fw_fwd_addr->sin_port), 1,
557 				    m->m_pkthdr.rcvif);
558 			}
559 		}
560 		ip_fw_fwd_addr = NULL;
561 	} else
562 #endif	/* IPFIREWALL_FORWARD */
563       {
564 #ifdef INET6
565 	if (isipv6)
566 		inp = in6_pcblookup_hash(&tcbinfo, &ip6->ip6_src, th->th_sport,
567 					 &ip6->ip6_dst, th->th_dport, 1,
568 					 m->m_pkthdr.rcvif);
569 	else
570 #endif /* INET6 */
571 	inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport,
572 	    ip->ip_dst, th->th_dport, 1, m->m_pkthdr.rcvif);
573       }
574 
575 #ifdef IPSEC
576 #ifdef INET6
577 	if (isipv6) {
578 		if (inp != NULL && ipsec6_in_reject_so(m, inp->inp_socket)) {
579 			ipsec6stat.in_polvio++;
580 			goto drop;
581 		}
582 	} else
583 #endif /* INET6 */
584 	if (inp != NULL && ipsec4_in_reject_so(m, inp->inp_socket)) {
585 		ipsecstat.in_polvio++;
586 		goto drop;
587 	}
588 #endif /*IPSEC*/
589 
590 	/*
591 	 * If the state is CLOSED (i.e., TCB does not exist) then
592 	 * all data in the incoming segment is discarded.
593 	 * If the TCB exists but is in CLOSED state, it is embryonic,
594 	 * but should either do a listen or a connect soon.
595 	 */
596 	if (inp == NULL) {
597 		if (log_in_vain) {
598 #ifdef INET6
599 			char dbuf[INET6_ADDRSTRLEN], sbuf[INET6_ADDRSTRLEN];
600 #else /* INET6 */
601 			char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"];
602 #endif /* INET6 */
603 
604 #ifdef INET6
605 			if (isipv6) {
606 				strcpy(dbuf, ip6_sprintf(&ip6->ip6_dst));
607 				strcpy(sbuf, ip6_sprintf(&ip6->ip6_src));
608 			} else
609 #endif
610 		      {
611 			strcpy(dbuf, inet_ntoa(ip->ip_dst));
612 			strcpy(sbuf, inet_ntoa(ip->ip_src));
613 		      }
614 			switch (log_in_vain) {
615 			case 1:
616 				if(thflags & TH_SYN)
617 					log(LOG_INFO,
618 			    		"Connection attempt to TCP %s:%d from %s:%d\n",
619 			    		dbuf, ntohs(th->th_dport),
620 					sbuf,
621 					ntohs(th->th_sport));
622 				break;
623 			case 2:
624 				log(LOG_INFO,
625 			    	"Connection attempt to TCP %s:%d from %s:%d flags:0x%x\n",
626 			    	dbuf, ntohs(th->th_dport), sbuf,
627 			    	ntohs(th->th_sport), thflags);
628 				break;
629 			default:
630 				break;
631 			}
632 		}
633 		if (blackhole) {
634 			switch (blackhole) {
635 			case 1:
636 				if (thflags & TH_SYN)
637 					goto drop;
638 				break;
639 			case 2:
640 				goto drop;
641 			default:
642 				goto drop;
643 			}
644 		}
645 		rstreason = BANDLIM_RST_CLOSEDPORT;
646 		goto dropwithreset;
647 	}
648 	tp = intotcpcb(inp);
649 	if (tp == 0) {
650 		rstreason = BANDLIM_RST_CLOSEDPORT;
651 		goto dropwithreset;
652 	}
653 	if (tp->t_state == TCPS_CLOSED)
654 		goto drop;
655 
656 	/* Unscale the window into a 32-bit value. */
657 	if ((thflags & TH_SYN) == 0)
658 		tiwin = th->th_win << tp->snd_scale;
659 	else
660 		tiwin = th->th_win;
661 
662 #ifdef INET6
663 	/* save packet options if user wanted */
664 	if (isipv6 && inp->in6p_flags & INP_CONTROLOPTS) {
665 		if (inp->in6p_options) {
666 			m_freem(inp->in6p_options);
667 			inp->in6p_options = 0;
668 		}
669 		ip6_savecontrol(inp, &inp->in6p_options, ip6, m);
670 	}
671         /* else, should also do ip_srcroute() here? */
672 #endif /* INET6 */
673 
674 	so = inp->inp_socket;
675 	if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
676 #ifdef TCPDEBUG
677 		if (so->so_options & SO_DEBUG) {
678 			ostate = tp->t_state;
679 #ifdef INET6
680 			if (isipv6)
681 				bcopy((char *)ip6, (char *)tcp_saveipgen,
682 				      sizeof(*ip6));
683 			else
684 #endif /* INET6 */
685 			bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
686 			tcp_savetcp = *th;
687 		}
688 #endif
689 		if (so->so_options & SO_ACCEPTCONN) {
690 			register struct tcpcb *tp0 = tp;
691 			struct socket *so2;
692 #ifdef IPSEC
693 			struct socket *oso;
694 #endif
695 #ifdef INET6
696 			struct inpcb *oinp = sotoinpcb(so);
697 #endif /* INET6 */
698 
699 #ifndef IPSEC
700 			/*
701 			 * Current IPsec implementation makes incorrect IPsec
702 			 * cache if this check is done here.
703 			 * So delay this until duplicated socket is created.
704 			 */
705 			if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
706 				/*
707 				 * Note: dropwithreset makes sure we don't
708 				 * send a RST in response to a RST.
709 				 */
710 				if (thflags & TH_ACK) {
711 					tcpstat.tcps_badsyn++;
712 					rstreason = BANDLIM_RST_OPENPORT;
713 					goto dropwithreset;
714 				}
715 				goto drop;
716 			}
717 #endif
718 			so2 = sonewconn(so, 0);
719 			if (so2 == 0) {
720 				tcpstat.tcps_listendrop++;
721 				so2 = sodropablereq(so);
722 				if (so2) {
723 					if (tcp_lq_overflow)
724 						sototcpcb(so2)->t_flags |=
725 						    TF_LQ_OVERFLOW;
726 					tcp_drop(sototcpcb(so2), ETIMEDOUT);
727 					so2 = sonewconn(so, 0);
728 				}
729 				if (!so2)
730 					goto drop;
731 			}
732 #ifdef IPSEC
733 			oso = so;
734 #endif
735 			so = so2;
736 			/*
737 			 * This is ugly, but ....
738 			 *
739 			 * Mark socket as temporary until we're
740 			 * committed to keeping it.  The code at
741 			 * ``drop'' and ``dropwithreset'' check the
742 			 * flag dropsocket to see if the temporary
743 			 * socket created here should be discarded.
744 			 * We mark the socket as discardable until
745 			 * we're committed to it below in TCPS_LISTEN.
746 			 */
747 			dropsocket++;
748 			inp = (struct inpcb *)so->so_pcb;
749 #ifdef INET6
750 			if (isipv6)
751 				inp->in6p_laddr = ip6->ip6_dst;
752 			else {
753 				if ((inp->inp_flags & IN6P_BINDV6ONLY) == 0) {
754 					inp->inp_vflag &= ~INP_IPV6;
755 					inp->inp_vflag |= INP_IPV4;
756 				}
757 #endif /* INET6 */
758 			inp->inp_laddr = ip->ip_dst;
759 #ifdef INET6
760 			}
761 #endif /* INET6 */
762 			inp->inp_lport = th->th_dport;
763 			if (in_pcbinshash(inp) != 0) {
764 				/*
765 				 * Undo the assignments above if we failed to
766 				 * put the PCB on the hash lists.
767 				 */
768 #ifdef INET6
769 				if (isipv6)
770 					inp->in6p_laddr = in6addr_any;
771 				else
772 #endif /* INET6 */
773 				inp->inp_laddr.s_addr = INADDR_ANY;
774 				inp->inp_lport = 0;
775 				goto drop;
776 			}
777 #ifdef IPSEC
778 			/*
779 			 * To avoid creating incorrectly cached IPsec
780 			 * association, this is need to be done here.
781 			 *
782 			 * Subject: (KAME-snap 748)
783 			 * From: Wayne Knowles <w.knowles@niwa.cri.nz>
784 			 * ftp://ftp.kame.net/pub/mail-list/snap-users/748
785 			 */
786 			if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
787 				/*
788 				 * Note: dropwithreset makes sure we don't
789 				 * send a RST in response to a RST.
790 				 */
791 				if (thflags & TH_ACK) {
792 					tcpstat.tcps_badsyn++;
793 					rstreason = BANDLIM_RST_OPENPORT;
794 					goto dropwithreset;
795 				}
796 				goto drop;
797 			}
798 #endif
799 #ifdef INET6
800 			if (isipv6) {
801 				/*
802 				 * inherit socket options from the listening
803 				 * socket.
804 				 */
805 				inp->inp_flags |=
806 					oinp->inp_flags & INP_CONTROLOPTS;
807 				if (inp->inp_flags & INP_CONTROLOPTS) {
808 					if (inp->in6p_options) {
809 						m_freem(inp->in6p_options);
810 						inp->in6p_options = 0;
811 					}
812 					ip6_savecontrol(inp,
813 							&inp->in6p_options,
814 							ip6, m);
815 				}
816 			} else
817 #endif /* INET6 */
818 			inp->inp_options = ip_srcroute();
819 #ifdef IPSEC
820 			/* copy old policy into new socket's */
821 			if (ipsec_copy_policy(sotoinpcb(oso)->inp_sp,
822 			                      inp->inp_sp))
823 				printf("tcp_input: could not copy policy\n");
824 #endif
825 			tp = intotcpcb(inp);
826 			tp->t_state = TCPS_LISTEN;
827 			tp->t_flags |= tp0->t_flags & (TF_NOPUSH|TF_NOOPT);
828 
829 			/* Compute proper scaling value from buffer space */
830 			while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
831 			   TCP_MAXWIN << tp->request_r_scale <
832 			   so->so_rcv.sb_hiwat)
833 				tp->request_r_scale++;
834 		}
835 	}
836 
837 	/*
838 	 * Segment received on connection.
839 	 * Reset idle time and keep-alive timer.
840 	 */
841 	tp->t_rcvtime = ticks;
842 	if (TCPS_HAVEESTABLISHED(tp->t_state))
843 		callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp);
844 
845 	/*
846 	 * Process options if not in LISTEN state,
847 	 * else do it below (after getting remote address).
848 	 */
849 	if (tp->t_state != TCPS_LISTEN)
850 		tcp_dooptions(tp, optp, optlen, th, &to);
851 
852 	/*
853 	 * Header prediction: check for the two common cases
854 	 * of a uni-directional data xfer.  If the packet has
855 	 * no control flags, is in-sequence, the window didn't
856 	 * change and we're not retransmitting, it's a
857 	 * candidate.  If the length is zero and the ack moved
858 	 * forward, we're the sender side of the xfer.  Just
859 	 * free the data acked & wake any higher level process
860 	 * that was blocked waiting for space.  If the length
861 	 * is non-zero and the ack didn't move, we're the
862 	 * receiver side.  If we're getting packets in-order
863 	 * (the reassembly queue is empty), add the data to
864 	 * the socket buffer and note that we need a delayed ack.
865 	 * Make sure that the hidden state-flags are also off.
866 	 * Since we check for TCPS_ESTABLISHED above, it can only
867 	 * be TH_NEEDSYN.
868 	 */
869 	if (tp->t_state == TCPS_ESTABLISHED &&
870 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
871 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
872 	    ((to.to_flag & TOF_TS) == 0 ||
873 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) &&
874 	    /*
875 	     * Using the CC option is compulsory if once started:
876 	     *   the segment is OK if no T/TCP was negotiated or
877 	     *   if the segment has a CC option equal to CCrecv
878 	     */
879 	    ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) ||
880 	     ((to.to_flag & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) &&
881 	    th->th_seq == tp->rcv_nxt &&
882 	    tiwin && tiwin == tp->snd_wnd &&
883 	    tp->snd_nxt == tp->snd_max) {
884 
885 		/*
886 		 * If last ACK falls within this segment's sequence numbers,
887 		 * record the timestamp.
888 		 * NOTE that the test is modified according to the latest
889 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
890 		 */
891 		if ((to.to_flag & TOF_TS) != 0 &&
892 		   SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
893 			tp->ts_recent_age = ticks;
894 			tp->ts_recent = to.to_tsval;
895 		}
896 
897 		if (tlen == 0) {
898 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
899 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
900 			    tp->snd_cwnd >= tp->snd_wnd &&
901 			    tp->t_dupacks < tcprexmtthresh) {
902 				/*
903 				 * this is a pure ack for outstanding data.
904 				 */
905 				++tcpstat.tcps_predack;
906 				/*
907 				 * "bad retransmit" recovery
908 				 */
909 				if (tp->t_rxtshift == 1 &&
910 				    ticks < tp->t_badrxtwin) {
911 					tp->snd_cwnd = tp->snd_cwnd_prev;
912 					tp->snd_ssthresh =
913 					    tp->snd_ssthresh_prev;
914 					tp->snd_nxt = tp->snd_max;
915 					tp->t_badrxtwin = 0;
916 				}
917 				if ((to.to_flag & TOF_TS) != 0)
918 					tcp_xmit_timer(tp,
919 					    ticks - to.to_tsecr + 1);
920 				else if (tp->t_rtttime &&
921 					    SEQ_GT(th->th_ack, tp->t_rtseq))
922 					tcp_xmit_timer(tp, ticks - tp->t_rtttime);
923 				acked = th->th_ack - tp->snd_una;
924 				tcpstat.tcps_rcvackpack++;
925 				tcpstat.tcps_rcvackbyte += acked;
926 				sbdrop(&so->so_snd, acked);
927 				tp->snd_una = th->th_ack;
928 				m_freem(m);
929 				ND6_HINT(tp); /* some progress has been done */
930 
931 				/*
932 				 * If all outstanding data are acked, stop
933 				 * retransmit timer, otherwise restart timer
934 				 * using current (possibly backed-off) value.
935 				 * If process is waiting for space,
936 				 * wakeup/selwakeup/signal.  If data
937 				 * are ready to send, let tcp_output
938 				 * decide between more output or persist.
939 				 */
940 				if (tp->snd_una == tp->snd_max)
941 					callout_stop(tp->tt_rexmt);
942 				else if (!callout_active(tp->tt_persist))
943 					callout_reset(tp->tt_rexmt,
944 						      tp->t_rxtcur,
945 						      tcp_timer_rexmt, tp);
946 
947 				sowwakeup(so);
948 				if (so->so_snd.sb_cc)
949 					(void) tcp_output(tp);
950 				return;
951 			}
952 		} else if (th->th_ack == tp->snd_una &&
953 		    LIST_EMPTY(&tp->t_segq) &&
954 		    tlen <= sbspace(&so->so_rcv)) {
955 			/*
956 			 * this is a pure, in-sequence data packet
957 			 * with nothing on the reassembly queue and
958 			 * we have enough buffer space to take it.
959 			 */
960 			++tcpstat.tcps_preddat;
961 			tp->rcv_nxt += tlen;
962 			tcpstat.tcps_rcvpack++;
963 			tcpstat.tcps_rcvbyte += tlen;
964 			ND6_HINT(tp);	/* some progress has been done */
965 			/*
966 			 * Add data to socket buffer.
967 			 */
968 			m_adj(m, drop_hdrlen);	/* delayed header drop */
969 			sbappend(&so->so_rcv, m);
970 			sorwakeup(so);
971 			if (DELAY_ACK(tp)) {
972 	                        callout_reset(tp->tt_delack, tcp_delacktime,
973 	                            tcp_timer_delack, tp);
974 			} else {
975 				tp->t_flags |= TF_ACKNOW;
976 				tcp_output(tp);
977 			}
978 			return;
979 		}
980 	}
981 
982 	/*
983 	 * Calculate amount of space in receive window,
984 	 * and then do TCP input processing.
985 	 * Receive window is amount of space in rcv queue,
986 	 * but not less than advertised window.
987 	 */
988 	{ int win;
989 
990 	win = sbspace(&so->so_rcv);
991 	if (win < 0)
992 		win = 0;
993 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
994 	}
995 
996 	switch (tp->t_state) {
997 
998 	/*
999 	 * If the state is LISTEN then ignore segment if it contains an RST.
1000 	 * If the segment contains an ACK then it is bad and send a RST.
1001 	 * If it does not contain a SYN then it is not interesting; drop it.
1002 	 * If it is from this socket, drop it, it must be forged.
1003 	 * Don't bother responding if the destination was a broadcast.
1004 	 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
1005 	 * tp->iss, and send a segment:
1006 	 *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
1007 	 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
1008 	 * Fill in remote peer address fields if not previously specified.
1009 	 * Enter SYN_RECEIVED state, and process any other fields of this
1010 	 * segment in this state.
1011 	 */
1012 	case TCPS_LISTEN: {
1013 		register struct sockaddr_in *sin;
1014 #ifdef INET6
1015 		register struct sockaddr_in6 *sin6;
1016 #endif
1017 
1018 		if (thflags & TH_RST)
1019 			goto drop;
1020 		if (thflags & TH_ACK) {
1021 			rstreason = BANDLIM_RST_OPENPORT;
1022 			goto dropwithreset;
1023 		}
1024 		if ((thflags & TH_SYN) == 0)
1025 			goto drop;
1026 		if (th->th_dport == th->th_sport) {
1027 #ifdef INET6
1028 			if (isipv6) {
1029 				if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
1030 						       &ip6->ip6_src))
1031 					goto drop;
1032 			} else
1033 #endif /* INET6 */
1034 			if (ip->ip_dst.s_addr == ip->ip_src.s_addr)
1035 				goto drop;
1036 		}
1037 		/*
1038 		 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
1039 		 * in_broadcast() should never return true on a received
1040 		 * packet with M_BCAST not set.
1041  		 *
1042  		 * Packets with a multicast source address should also
1043  		 * be discarded.
1044 		 */
1045 		if (m->m_flags & (M_BCAST|M_MCAST))
1046 			goto drop;
1047 #ifdef INET6
1048 		if (isipv6) {
1049 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1050 			    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
1051 				goto drop;
1052 		} else
1053 #endif
1054 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
1055 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
1056 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST))
1057 			goto drop;
1058 #ifdef INET6
1059 		if (isipv6) {
1060 			MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6,
1061 			       M_SONAME, M_NOWAIT | M_ZERO);
1062 			if (sin6 == NULL)
1063 				goto drop;
1064 			sin6->sin6_family = AF_INET6;
1065 			sin6->sin6_len = sizeof(*sin6);
1066 			sin6->sin6_addr = ip6->ip6_src;
1067 			sin6->sin6_port = th->th_sport;
1068 			laddr6 = inp->in6p_laddr;
1069 			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1070 				inp->in6p_laddr = ip6->ip6_dst;
1071 			if (in6_pcbconnect(inp, (struct sockaddr *)sin6,
1072 					   &proc0)) {
1073 				inp->in6p_laddr = laddr6;
1074 				FREE(sin6, M_SONAME);
1075 				goto drop;
1076 			}
1077 			FREE(sin6, M_SONAME);
1078 		} else
1079 #endif
1080 	      {
1081 		MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
1082 		       M_NOWAIT);
1083 		if (sin == NULL)
1084 			goto drop;
1085 		sin->sin_family = AF_INET;
1086 		sin->sin_len = sizeof(*sin);
1087 		sin->sin_addr = ip->ip_src;
1088 		sin->sin_port = th->th_sport;
1089 		bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero));
1090 		laddr = inp->inp_laddr;
1091 		if (inp->inp_laddr.s_addr == INADDR_ANY)
1092 			inp->inp_laddr = ip->ip_dst;
1093 		if (in_pcbconnect(inp, (struct sockaddr *)sin, &proc0)) {
1094 			inp->inp_laddr = laddr;
1095 			FREE(sin, M_SONAME);
1096 			goto drop;
1097 		}
1098 		FREE(sin, M_SONAME);
1099 	      }
1100 		tp->t_template = tcp_template(tp);
1101 		if (tp->t_template == 0) {
1102 			tp = tcp_drop(tp, ENOBUFS);
1103 			dropsocket = 0;		/* socket is already gone */
1104 			goto drop;
1105 		}
1106 		if ((taop = tcp_gettaocache(inp)) == NULL) {
1107 			taop = &tao_noncached;
1108 			bzero(taop, sizeof(*taop));
1109 		}
1110 		tcp_dooptions(tp, optp, optlen, th, &to);
1111 		if (iss)
1112 			tp->iss = iss;
1113 		else {
1114 			tp->iss = tcp_rndiss_next();
1115  		}
1116 		tp->irs = th->th_seq;
1117 		tcp_sendseqinit(tp);
1118 		tcp_rcvseqinit(tp);
1119 		tp->snd_recover = tp->snd_una;
1120 		/*
1121 		 * Initialization of the tcpcb for transaction;
1122 		 *   set SND.WND = SEG.WND,
1123 		 *   initialize CCsend and CCrecv.
1124 		 */
1125 		tp->snd_wnd = tiwin;	/* initial send-window */
1126 		tp->cc_send = CC_INC(tcp_ccgen);
1127 		tp->cc_recv = to.to_cc;
1128 		/*
1129 		 * Perform TAO test on incoming CC (SEG.CC) option, if any.
1130 		 * - compare SEG.CC against cached CC from the same host,
1131 		 *	if any.
1132 		 * - if SEG.CC > chached value, SYN must be new and is accepted
1133 		 *	immediately: save new CC in the cache, mark the socket
1134 		 *	connected, enter ESTABLISHED state, turn on flag to
1135 		 *	send a SYN in the next segment.
1136 		 *	A virtual advertised window is set in rcv_adv to
1137 		 *	initialize SWS prevention.  Then enter normal segment
1138 		 *	processing: drop SYN, process data and FIN.
1139 		 * - otherwise do a normal 3-way handshake.
1140 		 */
1141 		if ((to.to_flag & TOF_CC) != 0) {
1142 		    if (((tp->t_flags & TF_NOPUSH) != 0) &&
1143 			taop->tao_cc != 0 && CC_GT(to.to_cc, taop->tao_cc)) {
1144 
1145 			taop->tao_cc = to.to_cc;
1146 			tp->t_starttime = ticks;
1147 			tp->t_state = TCPS_ESTABLISHED;
1148 
1149 			/*
1150 			 * If there is a FIN, or if there is data and the
1151 			 * connection is local, then delay SYN,ACK(SYN) in
1152 			 * the hope of piggy-backing it on a response
1153 			 * segment.  Otherwise must send ACK now in case
1154 			 * the other side is slow starting.
1155 			 */
1156 			if (DELAY_ACK(tp) && ((thflags & TH_FIN) ||
1157 			    (tlen != 0 &&
1158 #ifdef INET6
1159 			      ((isipv6 && in6_localaddr(&inp->in6p_faddr))
1160 			      ||
1161 			      (!isipv6 &&
1162 #endif
1163 			    in_localaddr(inp->inp_faddr)
1164 #ifdef INET6
1165 			       ))
1166 #endif
1167 			     ))) {
1168                                 callout_reset(tp->tt_delack, tcp_delacktime,
1169                                     tcp_timer_delack, tp);
1170 				tp->t_flags |= TF_NEEDSYN;
1171 			} else
1172 				tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
1173 
1174 			/*
1175 			 * Limit the `virtual advertised window' to TCP_MAXWIN
1176 			 * here.  Even if we requested window scaling, it will
1177 			 * become effective only later when our SYN is acked.
1178 			 */
1179 			tp->rcv_adv += min(tp->rcv_wnd, TCP_MAXWIN);
1180 			tcpstat.tcps_connects++;
1181 			soisconnected(so);
1182 			callout_reset(tp->tt_keep, tcp_keepinit,
1183 				      tcp_timer_keep, tp);
1184 			dropsocket = 0;		/* committed to socket */
1185 			tcpstat.tcps_accepts++;
1186 			goto trimthenstep6;
1187 		    }
1188 		/* else do standard 3-way handshake */
1189 		} else {
1190 		    /*
1191 		     * No CC option, but maybe CC.NEW:
1192 		     *   invalidate cached value.
1193 		     */
1194 		     taop->tao_cc = 0;
1195 		}
1196 		/*
1197 		 * TAO test failed or there was no CC option,
1198 		 *    do a standard 3-way handshake.
1199 		 */
1200 		tp->t_flags |= TF_ACKNOW;
1201 		tp->t_state = TCPS_SYN_RECEIVED;
1202 		callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
1203 		dropsocket = 0;		/* committed to socket */
1204 		tcpstat.tcps_accepts++;
1205 		goto trimthenstep6;
1206 		}
1207 
1208 	/*
1209 	 * If the state is SYN_RECEIVED:
1210 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
1211 	 */
1212 	case TCPS_SYN_RECEIVED:
1213 		if ((thflags & TH_ACK) &&
1214 		    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1215 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1216 				rstreason = BANDLIM_RST_OPENPORT;
1217 				goto dropwithreset;
1218 		}
1219 		break;
1220 
1221 	/*
1222 	 * If the state is SYN_SENT:
1223 	 *	if seg contains an ACK, but not for our SYN, drop the input.
1224 	 *	if seg contains a RST, then drop the connection.
1225 	 *	if seg does not contain SYN, then drop it.
1226 	 * Otherwise this is an acceptable SYN segment
1227 	 *	initialize tp->rcv_nxt and tp->irs
1228 	 *	if seg contains ack then advance tp->snd_una
1229 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1230 	 *	arrange for segment to be acked (eventually)
1231 	 *	continue processing rest of data/controls, beginning with URG
1232 	 */
1233 	case TCPS_SYN_SENT:
1234 		if ((taop = tcp_gettaocache(inp)) == NULL) {
1235 			taop = &tao_noncached;
1236 			bzero(taop, sizeof(*taop));
1237 		}
1238 
1239 		if ((thflags & TH_ACK) &&
1240 		    (SEQ_LEQ(th->th_ack, tp->iss) ||
1241 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1242 			/*
1243 			 * If we have a cached CCsent for the remote host,
1244 			 * hence we haven't just crashed and restarted,
1245 			 * do not send a RST.  This may be a retransmission
1246 			 * from the other side after our earlier ACK was lost.
1247 			 * Our new SYN, when it arrives, will serve as the
1248 			 * needed ACK.
1249 			 */
1250 			if (taop->tao_ccsent != 0)
1251 				goto drop;
1252 			else {
1253 				rstreason = BANDLIM_UNLIMITED;
1254 				goto dropwithreset;
1255 			}
1256 		}
1257 		if (thflags & TH_RST) {
1258 			if (thflags & TH_ACK)
1259 				tp = tcp_drop(tp, ECONNREFUSED);
1260 			goto drop;
1261 		}
1262 		if ((thflags & TH_SYN) == 0)
1263 			goto drop;
1264 		tp->snd_wnd = th->th_win;	/* initial send window */
1265 		tp->cc_recv = to.to_cc;		/* foreign CC */
1266 
1267 		tp->irs = th->th_seq;
1268 		tcp_rcvseqinit(tp);
1269 		if (thflags & TH_ACK) {
1270 			/*
1271 			 * Our SYN was acked.  If segment contains CC.ECHO
1272 			 * option, check it to make sure this segment really
1273 			 * matches our SYN.  If not, just drop it as old
1274 			 * duplicate, but send an RST if we're still playing
1275 			 * by the old rules.  If no CC.ECHO option, make sure
1276 			 * we don't get fooled into using T/TCP.
1277 			 */
1278 			if (to.to_flag & TOF_CCECHO) {
1279 				if (tp->cc_send != to.to_ccecho) {
1280 					if (taop->tao_ccsent != 0)
1281 						goto drop;
1282 					else {
1283 						rstreason = BANDLIM_UNLIMITED;
1284 						goto dropwithreset;
1285 					}
1286 				}
1287 			} else
1288 				tp->t_flags &= ~TF_RCVD_CC;
1289 			tcpstat.tcps_connects++;
1290 			soisconnected(so);
1291 			/* Do window scaling on this connection? */
1292 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1293 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1294 				tp->snd_scale = tp->requested_s_scale;
1295 				tp->rcv_scale = tp->request_r_scale;
1296 			}
1297 			/* Segment is acceptable, update cache if undefined. */
1298 			if (taop->tao_ccsent == 0)
1299 				taop->tao_ccsent = to.to_ccecho;
1300 
1301 			tp->rcv_adv += tp->rcv_wnd;
1302 			tp->snd_una++;		/* SYN is acked */
1303 			/*
1304 			 * If there's data, delay ACK; if there's also a FIN
1305 			 * ACKNOW will be turned on later.
1306 			 */
1307 			if (DELAY_ACK(tp) && tlen != 0)
1308                                 callout_reset(tp->tt_delack, tcp_delacktime,
1309                                     tcp_timer_delack, tp);
1310 			else
1311 				tp->t_flags |= TF_ACKNOW;
1312 			/*
1313 			 * Received <SYN,ACK> in SYN_SENT[*] state.
1314 			 * Transitions:
1315 			 *	SYN_SENT  --> ESTABLISHED
1316 			 *	SYN_SENT* --> FIN_WAIT_1
1317 			 */
1318 			tp->t_starttime = ticks;
1319 			if (tp->t_flags & TF_NEEDFIN) {
1320 				tp->t_state = TCPS_FIN_WAIT_1;
1321 				tp->t_flags &= ~TF_NEEDFIN;
1322 				thflags &= ~TH_SYN;
1323 			} else {
1324 				tp->t_state = TCPS_ESTABLISHED;
1325 				callout_reset(tp->tt_keep, tcp_keepidle,
1326 					      tcp_timer_keep, tp);
1327 			}
1328 		} else {
1329 		/*
1330 		 *  Received initial SYN in SYN-SENT[*] state => simul-
1331 		 *  taneous open.  If segment contains CC option and there is
1332 		 *  a cached CC, apply TAO test; if it succeeds, connection is
1333 		 *  half-synchronized.  Otherwise, do 3-way handshake:
1334 		 *        SYN-SENT -> SYN-RECEIVED
1335 		 *        SYN-SENT* -> SYN-RECEIVED*
1336 		 *  If there was no CC option, clear cached CC value.
1337 		 */
1338 			tp->t_flags |= TF_ACKNOW;
1339 			callout_stop(tp->tt_rexmt);
1340 			if (to.to_flag & TOF_CC) {
1341 				if (taop->tao_cc != 0 &&
1342 				    CC_GT(to.to_cc, taop->tao_cc)) {
1343 					/*
1344 					 * update cache and make transition:
1345 					 *        SYN-SENT -> ESTABLISHED*
1346 					 *        SYN-SENT* -> FIN-WAIT-1*
1347 					 */
1348 					taop->tao_cc = to.to_cc;
1349 					tp->t_starttime = ticks;
1350 					if (tp->t_flags & TF_NEEDFIN) {
1351 						tp->t_state = TCPS_FIN_WAIT_1;
1352 						tp->t_flags &= ~TF_NEEDFIN;
1353 					} else {
1354 						tp->t_state = TCPS_ESTABLISHED;
1355 						callout_reset(tp->tt_keep,
1356 							      tcp_keepidle,
1357 							      tcp_timer_keep,
1358 							      tp);
1359 					}
1360 					tp->t_flags |= TF_NEEDSYN;
1361 				} else
1362 					tp->t_state = TCPS_SYN_RECEIVED;
1363 			} else {
1364 				/* CC.NEW or no option => invalidate cache */
1365 				taop->tao_cc = 0;
1366 				tp->t_state = TCPS_SYN_RECEIVED;
1367 			}
1368 		}
1369 
1370 trimthenstep6:
1371 		/*
1372 		 * Advance th->th_seq to correspond to first data byte.
1373 		 * If data, trim to stay within window,
1374 		 * dropping FIN if necessary.
1375 		 */
1376 		th->th_seq++;
1377 		if (tlen > tp->rcv_wnd) {
1378 			todrop = tlen - tp->rcv_wnd;
1379 			m_adj(m, -todrop);
1380 			tlen = tp->rcv_wnd;
1381 			thflags &= ~TH_FIN;
1382 			tcpstat.tcps_rcvpackafterwin++;
1383 			tcpstat.tcps_rcvbyteafterwin += todrop;
1384 		}
1385 		tp->snd_wl1 = th->th_seq - 1;
1386 		tp->rcv_up = th->th_seq;
1387 		/*
1388 		 *  Client side of transaction: already sent SYN and data.
1389 		 *  If the remote host used T/TCP to validate the SYN,
1390 		 *  our data will be ACK'd; if so, enter normal data segment
1391 		 *  processing in the middle of step 5, ack processing.
1392 		 *  Otherwise, goto step 6.
1393 		 */
1394  		if (thflags & TH_ACK)
1395 			goto process_ACK;
1396 		goto step6;
1397 	/*
1398 	 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
1399 	 *	if segment contains a SYN and CC [not CC.NEW] option:
1400 	 *              if state == TIME_WAIT and connection duration > MSL,
1401 	 *                  drop packet and send RST;
1402 	 *
1403 	 *		if SEG.CC > CCrecv then is new SYN, and can implicitly
1404 	 *		    ack the FIN (and data) in retransmission queue.
1405 	 *                  Complete close and delete TCPCB.  Then reprocess
1406 	 *                  segment, hoping to find new TCPCB in LISTEN state;
1407 	 *
1408 	 *		else must be old SYN; drop it.
1409 	 *      else do normal processing.
1410 	 */
1411 	case TCPS_LAST_ACK:
1412 	case TCPS_CLOSING:
1413 	case TCPS_TIME_WAIT:
1414 		if ((thflags & TH_SYN) &&
1415 		    (to.to_flag & TOF_CC) && tp->cc_recv != 0) {
1416 			if (tp->t_state == TCPS_TIME_WAIT &&
1417 					(ticks - tp->t_starttime) > tcp_msl) {
1418 				rstreason = BANDLIM_UNLIMITED;
1419 				goto dropwithreset;
1420 			}
1421 			if (CC_GT(to.to_cc, tp->cc_recv)) {
1422 				tp = tcp_close(tp);
1423 				goto findpcb;
1424 			}
1425 			else
1426 				goto drop;
1427 		}
1428  		break;  /* continue normal processing */
1429 	}
1430 
1431 	/*
1432 	 * States other than LISTEN or SYN_SENT.
1433 	 * First check the RST flag and sequence number since reset segments
1434 	 * are exempt from the timestamp and connection count tests.  This
1435 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
1436 	 * below which allowed reset segments in half the sequence space
1437 	 * to fall though and be processed (which gives forged reset
1438 	 * segments with a random sequence number a 50 percent chance of
1439 	 * killing a connection).
1440 	 * Then check timestamp, if present.
1441 	 * Then check the connection count, if present.
1442 	 * Then check that at least some bytes of segment are within
1443 	 * receive window.  If segment begins before rcv_nxt,
1444 	 * drop leading data (and SYN); if nothing left, just ack.
1445 	 *
1446 	 *
1447 	 * If the RST bit is set, check the sequence number to see
1448 	 * if this is a valid reset segment.
1449 	 * RFC 793 page 37:
1450 	 *   In all states except SYN-SENT, all reset (RST) segments
1451 	 *   are validated by checking their SEQ-fields.  A reset is
1452 	 *   valid if its sequence number is in the window.
1453 	 * Note: this does not take into account delayed ACKs, so
1454 	 *   we should test against last_ack_sent instead of rcv_nxt.
1455 	 *   The sequence number in the reset segment is normally an
1456 	 *   echo of our outgoing acknowlegement numbers, but some hosts
1457 	 *   send a reset with the sequence number at the rightmost edge
1458 	 *   of our receive window, and we have to handle this case.
1459 	 * If we have multiple segments in flight, the intial reset
1460 	 * segment sequence numbers will be to the left of last_ack_sent,
1461 	 * but they will eventually catch up.
1462 	 * In any case, it never made sense to trim reset segments to
1463 	 * fit the receive window since RFC 1122 says:
1464 	 *   4.2.2.12  RST Segment: RFC-793 Section 3.4
1465 	 *
1466 	 *    A TCP SHOULD allow a received RST segment to include data.
1467 	 *
1468 	 *    DISCUSSION
1469 	 *         It has been suggested that a RST segment could contain
1470 	 *         ASCII text that encoded and explained the cause of the
1471 	 *         RST.  No standard has yet been established for such
1472 	 *         data.
1473 	 *
1474 	 * If the reset segment passes the sequence number test examine
1475 	 * the state:
1476 	 *    SYN_RECEIVED STATE:
1477 	 *	If passive open, return to LISTEN state.
1478 	 *	If active open, inform user that connection was refused.
1479 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
1480 	 *	Inform user that connection was reset, and close tcb.
1481 	 *    CLOSING, LAST_ACK STATES:
1482 	 *	Close the tcb.
1483 	 *    TIME_WAIT STATE:
1484 	 *	Drop the segment - see Stevens, vol. 2, p. 964 and
1485 	 *      RFC 1337.
1486 	 */
1487 	if (thflags & TH_RST) {
1488 		if (SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
1489 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
1490 			switch (tp->t_state) {
1491 
1492 			case TCPS_SYN_RECEIVED:
1493 				so->so_error = ECONNREFUSED;
1494 				goto close;
1495 
1496 			case TCPS_ESTABLISHED:
1497 			case TCPS_FIN_WAIT_1:
1498 			case TCPS_FIN_WAIT_2:
1499 			case TCPS_CLOSE_WAIT:
1500 				so->so_error = ECONNRESET;
1501 			close:
1502 				tp->t_state = TCPS_CLOSED;
1503 				tcpstat.tcps_drops++;
1504 				tp = tcp_close(tp);
1505 				break;
1506 
1507 			case TCPS_CLOSING:
1508 			case TCPS_LAST_ACK:
1509 				tp = tcp_close(tp);
1510 				break;
1511 
1512 			case TCPS_TIME_WAIT:
1513 				break;
1514 			}
1515 		}
1516 		goto drop;
1517 	}
1518 
1519 	/*
1520 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
1521 	 * and it's less than ts_recent, drop it.
1522 	 */
1523 	if ((to.to_flag & TOF_TS) != 0 && tp->ts_recent &&
1524 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
1525 
1526 		/* Check to see if ts_recent is over 24 days old.  */
1527 		if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) {
1528 			/*
1529 			 * Invalidate ts_recent.  If this segment updates
1530 			 * ts_recent, the age will be reset later and ts_recent
1531 			 * will get a valid value.  If it does not, setting
1532 			 * ts_recent to zero will at least satisfy the
1533 			 * requirement that zero be placed in the timestamp
1534 			 * echo reply when ts_recent isn't valid.  The
1535 			 * age isn't reset until we get a valid ts_recent
1536 			 * because we don't want out-of-order segments to be
1537 			 * dropped when ts_recent is old.
1538 			 */
1539 			tp->ts_recent = 0;
1540 		} else {
1541 			tcpstat.tcps_rcvduppack++;
1542 			tcpstat.tcps_rcvdupbyte += tlen;
1543 			tcpstat.tcps_pawsdrop++;
1544 			goto dropafterack;
1545 		}
1546 	}
1547 
1548 	/*
1549 	 * T/TCP mechanism
1550 	 *   If T/TCP was negotiated and the segment doesn't have CC,
1551 	 *   or if its CC is wrong then drop the segment.
1552 	 *   RST segments do not have to comply with this.
1553 	 */
1554 	if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) &&
1555 	    ((to.to_flag & TOF_CC) == 0 || tp->cc_recv != to.to_cc))
1556  		goto dropafterack;
1557 
1558 	/*
1559 	 * In the SYN-RECEIVED state, validate that the packet belongs to
1560 	 * this connection before trimming the data to fit the receive
1561 	 * window.  Check the sequence number versus IRS since we know
1562 	 * the sequence numbers haven't wrapped.  This is a partial fix
1563 	 * for the "LAND" DoS attack.
1564 	 */
1565 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
1566 		rstreason = BANDLIM_RST_OPENPORT;
1567 		goto dropwithreset;
1568 	}
1569 
1570 	todrop = tp->rcv_nxt - th->th_seq;
1571 	if (todrop > 0) {
1572 		if (thflags & TH_SYN) {
1573 			thflags &= ~TH_SYN;
1574 			th->th_seq++;
1575 			if (th->th_urp > 1)
1576 				th->th_urp--;
1577 			else
1578 				thflags &= ~TH_URG;
1579 			todrop--;
1580 		}
1581 		/*
1582 		 * Following if statement from Stevens, vol. 2, p. 960.
1583 		 */
1584 		if (todrop > tlen
1585 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
1586 			/*
1587 			 * Any valid FIN must be to the left of the window.
1588 			 * At this point the FIN must be a duplicate or out
1589 			 * of sequence; drop it.
1590 			 */
1591 			thflags &= ~TH_FIN;
1592 
1593 			/*
1594 			 * Send an ACK to resynchronize and drop any data.
1595 			 * But keep on processing for RST or ACK.
1596 			 */
1597 			tp->t_flags |= TF_ACKNOW;
1598 			todrop = tlen;
1599 			tcpstat.tcps_rcvduppack++;
1600 			tcpstat.tcps_rcvdupbyte += todrop;
1601 		} else {
1602 			tcpstat.tcps_rcvpartduppack++;
1603 			tcpstat.tcps_rcvpartdupbyte += todrop;
1604 		}
1605 		drop_hdrlen += todrop;	/* drop from the top afterwards */
1606 		th->th_seq += todrop;
1607 		tlen -= todrop;
1608 		if (th->th_urp > todrop)
1609 			th->th_urp -= todrop;
1610 		else {
1611 			thflags &= ~TH_URG;
1612 			th->th_urp = 0;
1613 		}
1614 	}
1615 
1616 	/*
1617 	 * If new data are received on a connection after the
1618 	 * user processes are gone, then RST the other end.
1619 	 */
1620 	if ((so->so_state & SS_NOFDREF) &&
1621 	    tp->t_state > TCPS_CLOSE_WAIT && tlen) {
1622 		tp = tcp_close(tp);
1623 		tcpstat.tcps_rcvafterclose++;
1624 		rstreason = BANDLIM_UNLIMITED;
1625 		goto dropwithreset;
1626 	}
1627 
1628 	/*
1629 	 * If segment ends after window, drop trailing data
1630 	 * (and PUSH and FIN); if nothing left, just ACK.
1631 	 */
1632 	todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd);
1633 	if (todrop > 0) {
1634 		tcpstat.tcps_rcvpackafterwin++;
1635 		if (todrop >= tlen) {
1636 			tcpstat.tcps_rcvbyteafterwin += tlen;
1637 			/*
1638 			 * If a new connection request is received
1639 			 * while in TIME_WAIT, drop the old connection
1640 			 * and start over if the sequence numbers
1641 			 * are above the previous ones.
1642 			 */
1643 			if (thflags & TH_SYN &&
1644 			    tp->t_state == TCPS_TIME_WAIT &&
1645 			    SEQ_GT(th->th_seq, tp->rcv_nxt)) {
1646 				iss = tcp_rndiss_next();
1647 				tp = tcp_close(tp);
1648 				goto findpcb;
1649 			}
1650 			/*
1651 			 * If window is closed can only take segments at
1652 			 * window edge, and have to drop data and PUSH from
1653 			 * incoming segments.  Continue processing, but
1654 			 * remember to ack.  Otherwise, drop segment
1655 			 * and ack.
1656 			 */
1657 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
1658 				tp->t_flags |= TF_ACKNOW;
1659 				tcpstat.tcps_rcvwinprobe++;
1660 			} else
1661 				goto dropafterack;
1662 		} else
1663 			tcpstat.tcps_rcvbyteafterwin += todrop;
1664 		m_adj(m, -todrop);
1665 		tlen -= todrop;
1666 		thflags &= ~(TH_PUSH|TH_FIN);
1667 	}
1668 
1669 	/*
1670 	 * If last ACK falls within this segment's sequence numbers,
1671 	 * record its timestamp.
1672 	 * NOTE that the test is modified according to the latest
1673 	 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1674 	 */
1675 	if ((to.to_flag & TOF_TS) != 0 &&
1676 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1677 		tp->ts_recent_age = ticks;
1678 		tp->ts_recent = to.to_tsval;
1679 	}
1680 
1681 	/*
1682 	 * If a SYN is in the window, then this is an
1683 	 * error and we send an RST and drop the connection.
1684 	 */
1685 	if (thflags & TH_SYN) {
1686 		tp = tcp_drop(tp, ECONNRESET);
1687 		rstreason = BANDLIM_UNLIMITED;
1688 		goto dropwithreset;
1689 	}
1690 
1691 	/*
1692 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
1693 	 * flag is on (half-synchronized state), then queue data for
1694 	 * later processing; else drop segment and return.
1695 	 */
1696 	if ((thflags & TH_ACK) == 0) {
1697 		if (tp->t_state == TCPS_SYN_RECEIVED ||
1698 		    (tp->t_flags & TF_NEEDSYN))
1699 			goto step6;
1700 		else
1701 			goto drop;
1702 	}
1703 
1704 	/*
1705 	 * Ack processing.
1706 	 */
1707 	switch (tp->t_state) {
1708 
1709 	/*
1710 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
1711 	 * ESTABLISHED state and continue processing.
1712 	 * The ACK was checked above.
1713 	 */
1714 	case TCPS_SYN_RECEIVED:
1715 
1716 		tcpstat.tcps_connects++;
1717 		soisconnected(so);
1718 		/* Do window scaling? */
1719 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1720 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1721 			tp->snd_scale = tp->requested_s_scale;
1722 			tp->rcv_scale = tp->request_r_scale;
1723 		}
1724 		/*
1725 		 * Upon successful completion of 3-way handshake,
1726 		 * update cache.CC if it was undefined, pass any queued
1727 		 * data to the user, and advance state appropriately.
1728 		 */
1729 		if ((taop = tcp_gettaocache(inp)) != NULL &&
1730 		    taop->tao_cc == 0)
1731 			taop->tao_cc = tp->cc_recv;
1732 
1733 		/*
1734 		 * Make transitions:
1735 		 *      SYN-RECEIVED  -> ESTABLISHED
1736 		 *      SYN-RECEIVED* -> FIN-WAIT-1
1737 		 */
1738 		tp->t_starttime = ticks;
1739 		if (tp->t_flags & TF_NEEDFIN) {
1740 			tp->t_state = TCPS_FIN_WAIT_1;
1741 			tp->t_flags &= ~TF_NEEDFIN;
1742 		} else {
1743 			tp->t_state = TCPS_ESTABLISHED;
1744 			callout_reset(tp->tt_keep, tcp_keepidle,
1745 				      tcp_timer_keep, tp);
1746 		}
1747 		/*
1748 		 * If segment contains data or ACK, will call tcp_reass()
1749 		 * later; if not, do so now to pass queued data to user.
1750 		 */
1751 		if (tlen == 0 && (thflags & TH_FIN) == 0)
1752 			(void) tcp_reass(tp, (struct tcphdr *)0, 0,
1753 			    (struct mbuf *)0);
1754 		tp->snd_wl1 = th->th_seq - 1;
1755 		/* fall into ... */
1756 
1757 	/*
1758 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1759 	 * ACKs.  If the ack is in the range
1760 	 *	tp->snd_una < th->th_ack <= tp->snd_max
1761 	 * then advance tp->snd_una to th->th_ack and drop
1762 	 * data from the retransmission queue.  If this ACK reflects
1763 	 * more up to date window information we update our window information.
1764 	 */
1765 	case TCPS_ESTABLISHED:
1766 	case TCPS_FIN_WAIT_1:
1767 	case TCPS_FIN_WAIT_2:
1768 	case TCPS_CLOSE_WAIT:
1769 	case TCPS_CLOSING:
1770 	case TCPS_LAST_ACK:
1771 	case TCPS_TIME_WAIT:
1772 
1773 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
1774 			if (tlen == 0 && tiwin == tp->snd_wnd) {
1775 				tcpstat.tcps_rcvdupack++;
1776 				/*
1777 				 * If we have outstanding data (other than
1778 				 * a window probe), this is a completely
1779 				 * duplicate ack (ie, window info didn't
1780 				 * change), the ack is the biggest we've
1781 				 * seen and we've seen exactly our rexmt
1782 				 * threshhold of them, assume a packet
1783 				 * has been dropped and retransmit it.
1784 				 * Kludge snd_nxt & the congestion
1785 				 * window so we send only this one
1786 				 * packet.
1787 				 *
1788 				 * We know we're losing at the current
1789 				 * window size so do congestion avoidance
1790 				 * (set ssthresh to half the current window
1791 				 * and pull our congestion window back to
1792 				 * the new ssthresh).
1793 				 *
1794 				 * Dup acks mean that packets have left the
1795 				 * network (they're now cached at the receiver)
1796 				 * so bump cwnd by the amount in the receiver
1797 				 * to keep a constant cwnd packets in the
1798 				 * network.
1799 				 */
1800 				if (!callout_active(tp->tt_rexmt) ||
1801 				    th->th_ack != tp->snd_una)
1802 					tp->t_dupacks = 0;
1803 				else if (++tp->t_dupacks == tcprexmtthresh) {
1804 					tcp_seq onxt = tp->snd_nxt;
1805 					u_int win =
1806 					    min(tp->snd_wnd, tp->snd_cwnd) / 2 /
1807 						tp->t_maxseg;
1808 					if (tcp_do_newreno && SEQ_LT(th->th_ack,
1809 					    tp->snd_recover)) {
1810 						/* False retransmit, should not
1811 						 * cut window
1812 						 */
1813 						tp->snd_cwnd += tp->t_maxseg;
1814 						tp->t_dupacks = 0;
1815 						(void) tcp_output(tp);
1816 						goto drop;
1817 					}
1818 					if (win < 2)
1819 						win = 2;
1820 					tp->snd_ssthresh = win * tp->t_maxseg;
1821 					tp->snd_recover = tp->snd_max;
1822 					callout_stop(tp->tt_rexmt);
1823 					tp->t_rtttime = 0;
1824 					tp->snd_nxt = th->th_ack;
1825 					tp->snd_cwnd = tp->t_maxseg;
1826 					(void) tcp_output(tp);
1827 					tp->snd_cwnd = tp->snd_ssthresh +
1828 					       tp->t_maxseg * tp->t_dupacks;
1829 					if (SEQ_GT(onxt, tp->snd_nxt))
1830 						tp->snd_nxt = onxt;
1831 					goto drop;
1832 				} else if (tp->t_dupacks > tcprexmtthresh) {
1833 					tp->snd_cwnd += tp->t_maxseg;
1834 					(void) tcp_output(tp);
1835 					goto drop;
1836 				}
1837 			} else
1838 				tp->t_dupacks = 0;
1839 			break;
1840 		}
1841 		/*
1842 		 * If the congestion window was inflated to account
1843 		 * for the other side's cached packets, retract it.
1844 		 */
1845 		if (tcp_do_newreno == 0) {
1846                         if (tp->t_dupacks >= tcprexmtthresh &&
1847                                 tp->snd_cwnd > tp->snd_ssthresh)
1848                                 tp->snd_cwnd = tp->snd_ssthresh;
1849                         tp->t_dupacks = 0;
1850                 } else if (tp->t_dupacks >= tcprexmtthresh &&
1851 		    !tcp_newreno(tp, th)) {
1852                         /*
1853                          * Window inflation should have left us with approx.
1854                          * snd_ssthresh outstanding data.  But in case we
1855                          * would be inclined to send a burst, better to do
1856                          * it via the slow start mechanism.
1857                          */
1858 			if (SEQ_GT(th->th_ack + tp->snd_ssthresh, tp->snd_max))
1859                                 tp->snd_cwnd =
1860 				    tp->snd_max - th->th_ack + tp->t_maxseg;
1861 			else
1862                         	tp->snd_cwnd = tp->snd_ssthresh;
1863                         tp->t_dupacks = 0;
1864                 }
1865 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
1866 			tcpstat.tcps_rcvacktoomuch++;
1867 			goto dropafterack;
1868 		}
1869 		/*
1870 		 *  If we reach this point, ACK is not a duplicate,
1871 		 *     i.e., it ACKs something we sent.
1872 		 */
1873 		if (tp->t_flags & TF_NEEDSYN) {
1874 			/*
1875 			 * T/TCP: Connection was half-synchronized, and our
1876 			 * SYN has been ACK'd (so connection is now fully
1877 			 * synchronized).  Go to non-starred state,
1878 			 * increment snd_una for ACK of SYN, and check if
1879 			 * we can do window scaling.
1880 			 */
1881 			tp->t_flags &= ~TF_NEEDSYN;
1882 			tp->snd_una++;
1883 			/* Do window scaling? */
1884 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1885 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1886 				tp->snd_scale = tp->requested_s_scale;
1887 				tp->rcv_scale = tp->request_r_scale;
1888 			}
1889 		}
1890 
1891 process_ACK:
1892 		acked = th->th_ack - tp->snd_una;
1893 		tcpstat.tcps_rcvackpack++;
1894 		tcpstat.tcps_rcvackbyte += acked;
1895 
1896 		/*
1897 		 * If we just performed our first retransmit, and the ACK
1898 		 * arrives within our recovery window, then it was a mistake
1899 		 * to do the retransmit in the first place.  Recover our
1900 		 * original cwnd and ssthresh, and proceed to transmit where
1901 		 * we left off.
1902 		 */
1903 		if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) {
1904 			tp->snd_cwnd = tp->snd_cwnd_prev;
1905 			tp->snd_ssthresh = tp->snd_ssthresh_prev;
1906 			tp->snd_nxt = tp->snd_max;
1907 			tp->t_badrxtwin = 0;	/* XXX probably not required */
1908 		}
1909 
1910 		/*
1911 		 * If we have a timestamp reply, update smoothed
1912 		 * round trip time.  If no timestamp is present but
1913 		 * transmit timer is running and timed sequence
1914 		 * number was acked, update smoothed round trip time.
1915 		 * Since we now have an rtt measurement, cancel the
1916 		 * timer backoff (cf., Phil Karn's retransmit alg.).
1917 		 * Recompute the initial retransmit timer.
1918 		 */
1919 		if (to.to_flag & TOF_TS)
1920 			tcp_xmit_timer(tp, ticks - to.to_tsecr + 1);
1921 		else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq))
1922 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
1923 
1924 		/*
1925 		 * If all outstanding data is acked, stop retransmit
1926 		 * timer and remember to restart (more output or persist).
1927 		 * If there is more data to be acked, restart retransmit
1928 		 * timer, using current (possibly backed-off) value.
1929 		 */
1930 		if (th->th_ack == tp->snd_max) {
1931 			callout_stop(tp->tt_rexmt);
1932 			needoutput = 1;
1933 		} else if (!callout_active(tp->tt_persist))
1934 			callout_reset(tp->tt_rexmt, tp->t_rxtcur,
1935 				      tcp_timer_rexmt, tp);
1936 
1937 		/*
1938 		 * If no data (only SYN) was ACK'd,
1939 		 *    skip rest of ACK processing.
1940 		 */
1941 		if (acked == 0)
1942 			goto step6;
1943 
1944 		/*
1945 		 * When new data is acked, open the congestion window.
1946 		 * If the window gives us less than ssthresh packets
1947 		 * in flight, open exponentially (maxseg per packet).
1948 		 * Otherwise open linearly: maxseg per window
1949 		 * (maxseg^2 / cwnd per packet).
1950 		 */
1951 		{
1952 		register u_int cw = tp->snd_cwnd;
1953 		register u_int incr = tp->t_maxseg;
1954 
1955 		if (cw > tp->snd_ssthresh)
1956 			incr = incr * incr / cw;
1957 		/*
1958 		 * If t_dupacks != 0 here, it indicates that we are still
1959 		 * in NewReno fast recovery mode, so we leave the congestion
1960 		 * window alone.
1961 		 */
1962 		if (tcp_do_newreno == 0 || tp->t_dupacks == 0)
1963 			tp->snd_cwnd = min(cw + incr,TCP_MAXWIN<<tp->snd_scale);
1964 		}
1965 		if (acked > so->so_snd.sb_cc) {
1966 			tp->snd_wnd -= so->so_snd.sb_cc;
1967 			sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
1968 			ourfinisacked = 1;
1969 		} else {
1970 			sbdrop(&so->so_snd, acked);
1971 			tp->snd_wnd -= acked;
1972 			ourfinisacked = 0;
1973 		}
1974 		sowwakeup(so);
1975 		tp->snd_una = th->th_ack;
1976 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
1977 			tp->snd_nxt = tp->snd_una;
1978 
1979 		switch (tp->t_state) {
1980 
1981 		/*
1982 		 * In FIN_WAIT_1 STATE in addition to the processing
1983 		 * for the ESTABLISHED state if our FIN is now acknowledged
1984 		 * then enter FIN_WAIT_2.
1985 		 */
1986 		case TCPS_FIN_WAIT_1:
1987 			if (ourfinisacked) {
1988 				/*
1989 				 * If we can't receive any more
1990 				 * data, then closing user can proceed.
1991 				 * Starting the timer is contrary to the
1992 				 * specification, but if we don't get a FIN
1993 				 * we'll hang forever.
1994 				 */
1995 				if (so->so_state & SS_CANTRCVMORE) {
1996 					soisdisconnected(so);
1997 					callout_reset(tp->tt_2msl, tcp_maxidle,
1998 						      tcp_timer_2msl, tp);
1999 				}
2000 				tp->t_state = TCPS_FIN_WAIT_2;
2001 			}
2002 			break;
2003 
2004 	 	/*
2005 		 * In CLOSING STATE in addition to the processing for
2006 		 * the ESTABLISHED state if the ACK acknowledges our FIN
2007 		 * then enter the TIME-WAIT state, otherwise ignore
2008 		 * the segment.
2009 		 */
2010 		case TCPS_CLOSING:
2011 			if (ourfinisacked) {
2012 				tp->t_state = TCPS_TIME_WAIT;
2013 				tcp_canceltimers(tp);
2014 				/* Shorten TIME_WAIT [RFC-1644, p.28] */
2015 				if (tp->cc_recv != 0 &&
2016 				    (ticks - tp->t_starttime) < tcp_msl)
2017 					callout_reset(tp->tt_2msl,
2018 						      tp->t_rxtcur *
2019 						      TCPTV_TWTRUNC,
2020 						      tcp_timer_2msl, tp);
2021 				else
2022 					callout_reset(tp->tt_2msl, 2 * tcp_msl,
2023 						      tcp_timer_2msl, tp);
2024 				soisdisconnected(so);
2025 			}
2026 			break;
2027 
2028 		/*
2029 		 * In LAST_ACK, we may still be waiting for data to drain
2030 		 * and/or to be acked, as well as for the ack of our FIN.
2031 		 * If our FIN is now acknowledged, delete the TCB,
2032 		 * enter the closed state and return.
2033 		 */
2034 		case TCPS_LAST_ACK:
2035 			if (ourfinisacked) {
2036 				tp = tcp_close(tp);
2037 				goto drop;
2038 			}
2039 			break;
2040 
2041 		/*
2042 		 * In TIME_WAIT state the only thing that should arrive
2043 		 * is a retransmission of the remote FIN.  Acknowledge
2044 		 * it and restart the finack timer.
2045 		 */
2046 		case TCPS_TIME_WAIT:
2047 			callout_reset(tp->tt_2msl, 2 * tcp_msl,
2048 				      tcp_timer_2msl, tp);
2049 			goto dropafterack;
2050 		}
2051 	}
2052 
2053 step6:
2054 	/*
2055 	 * Update window information.
2056 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2057 	 */
2058 	if ((thflags & TH_ACK) &&
2059 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
2060 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
2061 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
2062 		/* keep track of pure window updates */
2063 		if (tlen == 0 &&
2064 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
2065 			tcpstat.tcps_rcvwinupd++;
2066 		tp->snd_wnd = tiwin;
2067 		tp->snd_wl1 = th->th_seq;
2068 		tp->snd_wl2 = th->th_ack;
2069 		if (tp->snd_wnd > tp->max_sndwnd)
2070 			tp->max_sndwnd = tp->snd_wnd;
2071 		needoutput = 1;
2072 	}
2073 
2074 	/*
2075 	 * Process segments with URG.
2076 	 */
2077 	if ((thflags & TH_URG) && th->th_urp &&
2078 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2079 		/*
2080 		 * This is a kludge, but if we receive and accept
2081 		 * random urgent pointers, we'll crash in
2082 		 * soreceive.  It's hard to imagine someone
2083 		 * actually wanting to send this much urgent data.
2084 		 */
2085 		if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
2086 			th->th_urp = 0;			/* XXX */
2087 			thflags &= ~TH_URG;		/* XXX */
2088 			goto dodata;			/* XXX */
2089 		}
2090 		/*
2091 		 * If this segment advances the known urgent pointer,
2092 		 * then mark the data stream.  This should not happen
2093 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2094 		 * a FIN has been received from the remote side.
2095 		 * In these states we ignore the URG.
2096 		 *
2097 		 * According to RFC961 (Assigned Protocols),
2098 		 * the urgent pointer points to the last octet
2099 		 * of urgent data.  We continue, however,
2100 		 * to consider it to indicate the first octet
2101 		 * of data past the urgent section as the original
2102 		 * spec states (in one of two places).
2103 		 */
2104 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
2105 			tp->rcv_up = th->th_seq + th->th_urp;
2106 			so->so_oobmark = so->so_rcv.sb_cc +
2107 			    (tp->rcv_up - tp->rcv_nxt) - 1;
2108 			if (so->so_oobmark == 0)
2109 				so->so_state |= SS_RCVATMARK;
2110 			sohasoutofband(so);
2111 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2112 		}
2113 		/*
2114 		 * Remove out of band data so doesn't get presented to user.
2115 		 * This can happen independent of advancing the URG pointer,
2116 		 * but if two URG's are pending at once, some out-of-band
2117 		 * data may creep in... ick.
2118 		 */
2119 		if (th->th_urp <= (u_long)tlen
2120 #ifdef SO_OOBINLINE
2121 		     && (so->so_options & SO_OOBINLINE) == 0
2122 #endif
2123 		     )
2124 			tcp_pulloutofband(so, th, m,
2125 				drop_hdrlen);	/* hdr drop is delayed */
2126 	} else
2127 		/*
2128 		 * If no out of band data is expected,
2129 		 * pull receive urgent pointer along
2130 		 * with the receive window.
2131 		 */
2132 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
2133 			tp->rcv_up = tp->rcv_nxt;
2134 dodata:							/* XXX */
2135 
2136 	/*
2137 	 * Process the segment text, merging it into the TCP sequencing queue,
2138 	 * and arranging for acknowledgment of receipt if necessary.
2139 	 * This process logically involves adjusting tp->rcv_wnd as data
2140 	 * is presented to the user (this happens in tcp_usrreq.c,
2141 	 * case PRU_RCVD).  If a FIN has already been received on this
2142 	 * connection then we just ignore the text.
2143 	 */
2144 	if ((tlen || (thflags&TH_FIN)) &&
2145 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2146 		m_adj(m, drop_hdrlen);	/* delayed header drop */
2147 		TCP_REASS(tp, th, &tlen, m, so, thflags);
2148 		/*
2149 		 * Note the amount of data that peer has sent into
2150 		 * our window, in order to estimate the sender's
2151 		 * buffer size.
2152 		 */
2153 		len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
2154 	} else {
2155 		m_freem(m);
2156 		thflags &= ~TH_FIN;
2157 	}
2158 
2159 	/*
2160 	 * If FIN is received ACK the FIN and let the user know
2161 	 * that the connection is closing.
2162 	 */
2163 	if (thflags & TH_FIN) {
2164 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2165 			socantrcvmore(so);
2166 			/*
2167 			 *  If connection is half-synchronized
2168 			 *  (ie NEEDSYN flag on) then delay ACK,
2169 			 *  so it may be piggybacked when SYN is sent.
2170 			 *  Otherwise, since we received a FIN then no
2171 			 *  more input can be expected, send ACK now.
2172 			 */
2173 			if (DELAY_ACK(tp) && (tp->t_flags & TF_NEEDSYN))
2174                                 callout_reset(tp->tt_delack, tcp_delacktime,
2175                                     tcp_timer_delack, tp);
2176 			else
2177 				tp->t_flags |= TF_ACKNOW;
2178 			tp->rcv_nxt++;
2179 		}
2180 		switch (tp->t_state) {
2181 
2182 	 	/*
2183 		 * In SYN_RECEIVED and ESTABLISHED STATES
2184 		 * enter the CLOSE_WAIT state.
2185 		 */
2186 		case TCPS_SYN_RECEIVED:
2187 			tp->t_starttime = ticks;
2188 			/*FALLTHROUGH*/
2189 		case TCPS_ESTABLISHED:
2190 			tp->t_state = TCPS_CLOSE_WAIT;
2191 			break;
2192 
2193 	 	/*
2194 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2195 		 * enter the CLOSING state.
2196 		 */
2197 		case TCPS_FIN_WAIT_1:
2198 			tp->t_state = TCPS_CLOSING;
2199 			break;
2200 
2201 	 	/*
2202 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2203 		 * starting the time-wait timer, turning off the other
2204 		 * standard timers.
2205 		 */
2206 		case TCPS_FIN_WAIT_2:
2207 			tp->t_state = TCPS_TIME_WAIT;
2208 			tcp_canceltimers(tp);
2209 			/* Shorten TIME_WAIT [RFC-1644, p.28] */
2210 			if (tp->cc_recv != 0 &&
2211 			    (ticks - tp->t_starttime) < tcp_msl) {
2212 				callout_reset(tp->tt_2msl,
2213 					      tp->t_rxtcur * TCPTV_TWTRUNC,
2214 					      tcp_timer_2msl, tp);
2215 				/* For transaction client, force ACK now. */
2216 				tp->t_flags |= TF_ACKNOW;
2217 			}
2218 			else
2219 				callout_reset(tp->tt_2msl, 2 * tcp_msl,
2220 					      tcp_timer_2msl, tp);
2221 			soisdisconnected(so);
2222 			break;
2223 
2224 		/*
2225 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
2226 		 */
2227 		case TCPS_TIME_WAIT:
2228 			callout_reset(tp->tt_2msl, 2 * tcp_msl,
2229 				      tcp_timer_2msl, tp);
2230 			break;
2231 		}
2232 	}
2233 #ifdef TCPDEBUG
2234 	if (so->so_options & SO_DEBUG)
2235 		tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
2236 			  &tcp_savetcp, 0);
2237 #endif
2238 
2239 	/*
2240 	 * Return any desired output.
2241 	 */
2242 	if (needoutput || (tp->t_flags & TF_ACKNOW))
2243 		(void) tcp_output(tp);
2244 	return;
2245 
2246 dropafterack:
2247 	/*
2248 	 * Generate an ACK dropping incoming segment if it occupies
2249 	 * sequence space, where the ACK reflects our state.
2250 	 *
2251 	 * We can now skip the test for the RST flag since all
2252 	 * paths to this code happen after packets containing
2253 	 * RST have been dropped.
2254 	 *
2255 	 * In the SYN-RECEIVED state, don't send an ACK unless the
2256 	 * segment we received passes the SYN-RECEIVED ACK test.
2257 	 * If it fails send a RST.  This breaks the loop in the
2258 	 * "LAND" DoS attack, and also prevents an ACK storm
2259 	 * between two listening ports that have been sent forged
2260 	 * SYN segments, each with the source address of the other.
2261 	 */
2262 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
2263 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
2264 	     SEQ_GT(th->th_ack, tp->snd_max)) ) {
2265 		rstreason = BANDLIM_RST_OPENPORT;
2266 		goto dropwithreset;
2267 	}
2268 #ifdef TCPDEBUG
2269 	if (so->so_options & SO_DEBUG)
2270 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2271 			  &tcp_savetcp, 0);
2272 #endif
2273 	m_freem(m);
2274 	tp->t_flags |= TF_ACKNOW;
2275 	(void) tcp_output(tp);
2276 	return;
2277 
2278 dropwithreset:
2279 	/*
2280 	 * Generate a RST, dropping incoming segment.
2281 	 * Make ACK acceptable to originator of segment.
2282 	 * Don't bother to respond if destination was broadcast/multicast.
2283 	 */
2284 	if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
2285 		goto drop;
2286 #ifdef INET6
2287 	if (isipv6) {
2288 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
2289 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
2290 			goto drop;
2291 	} else
2292 #endif /* INET6 */
2293 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
2294 	    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
2295 	    ip->ip_src.s_addr == htonl(INADDR_BROADCAST))
2296 		goto drop;
2297 	/* IPv6 anycast check is done at tcp6_input() */
2298 
2299 	/*
2300 	 * Perform bandwidth limiting.
2301 	 */
2302 	if (badport_bandlim(rstreason) < 0)
2303 		goto drop;
2304 
2305 #ifdef TCPDEBUG
2306 	if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
2307 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2308 			  &tcp_savetcp, 0);
2309 #endif
2310 	if (thflags & TH_ACK)
2311 		/* mtod() below is safe as long as hdr dropping is delayed */
2312 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack,
2313 			    TH_RST);
2314 	else {
2315 		if (thflags & TH_SYN)
2316 			tlen++;
2317 		/* mtod() below is safe as long as hdr dropping is delayed */
2318 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
2319 			    (tcp_seq)0, TH_RST|TH_ACK);
2320 	}
2321 	/* destroy temporarily created socket */
2322 	if (dropsocket)
2323 		(void) soabort(so);
2324 	return;
2325 
2326 drop:
2327 	/*
2328 	 * Drop space held by incoming segment and return.
2329 	 */
2330 #ifdef TCPDEBUG
2331 	if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
2332 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2333 			  &tcp_savetcp, 0);
2334 #endif
2335 	m_freem(m);
2336 	/* destroy temporarily created socket */
2337 	if (dropsocket)
2338 		(void) soabort(so);
2339 	return;
2340 }
2341 
2342 static void
2343 tcp_dooptions(tp, cp, cnt, th, to)
2344 	struct tcpcb *tp;
2345 	u_char *cp;
2346 	int cnt;
2347 	struct tcphdr *th;
2348 	struct tcpopt *to;
2349 {
2350 	u_short mss = 0;
2351 	int opt, optlen;
2352 
2353 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
2354 		opt = cp[0];
2355 		if (opt == TCPOPT_EOL)
2356 			break;
2357 		if (opt == TCPOPT_NOP)
2358 			optlen = 1;
2359 		else {
2360 			if (cnt < 2)
2361 				break;
2362 			optlen = cp[1];
2363 			if (optlen < 2 || optlen > cnt)
2364 				break;
2365 		}
2366 		switch (opt) {
2367 
2368 		default:
2369 			continue;
2370 
2371 		case TCPOPT_MAXSEG:
2372 			if (optlen != TCPOLEN_MAXSEG)
2373 				continue;
2374 			if (!(th->th_flags & TH_SYN))
2375 				continue;
2376 			bcopy((char *) cp + 2, (char *) &mss, sizeof(mss));
2377 			NTOHS(mss);
2378 			break;
2379 
2380 		case TCPOPT_WINDOW:
2381 			if (optlen != TCPOLEN_WINDOW)
2382 				continue;
2383 			if (!(th->th_flags & TH_SYN))
2384 				continue;
2385 			tp->t_flags |= TF_RCVD_SCALE;
2386 			tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
2387 			break;
2388 
2389 		case TCPOPT_TIMESTAMP:
2390 			if (optlen != TCPOLEN_TIMESTAMP)
2391 				continue;
2392 			to->to_flag |= TOF_TS;
2393 			bcopy((char *)cp + 2,
2394 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
2395 			NTOHL(to->to_tsval);
2396 			bcopy((char *)cp + 6,
2397 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
2398 			NTOHL(to->to_tsecr);
2399 
2400 			/*
2401 			 * A timestamp received in a SYN makes
2402 			 * it ok to send timestamp requests and replies.
2403 			 */
2404 			if (th->th_flags & TH_SYN) {
2405 				tp->t_flags |= TF_RCVD_TSTMP;
2406 				tp->ts_recent = to->to_tsval;
2407 				tp->ts_recent_age = ticks;
2408 			}
2409 			break;
2410 		case TCPOPT_CC:
2411 			if (optlen != TCPOLEN_CC)
2412 				continue;
2413 			to->to_flag |= TOF_CC;
2414 			bcopy((char *)cp + 2,
2415 			    (char *)&to->to_cc, sizeof(to->to_cc));
2416 			NTOHL(to->to_cc);
2417 			/*
2418 			 * A CC or CC.new option received in a SYN makes
2419 			 * it ok to send CC in subsequent segments.
2420 			 */
2421 			if (th->th_flags & TH_SYN)
2422 				tp->t_flags |= TF_RCVD_CC;
2423 			break;
2424 		case TCPOPT_CCNEW:
2425 			if (optlen != TCPOLEN_CC)
2426 				continue;
2427 			if (!(th->th_flags & TH_SYN))
2428 				continue;
2429 			to->to_flag |= TOF_CCNEW;
2430 			bcopy((char *)cp + 2,
2431 			    (char *)&to->to_cc, sizeof(to->to_cc));
2432 			NTOHL(to->to_cc);
2433 			/*
2434 			 * A CC or CC.new option received in a SYN makes
2435 			 * it ok to send CC in subsequent segments.
2436 			 */
2437 			tp->t_flags |= TF_RCVD_CC;
2438 			break;
2439 		case TCPOPT_CCECHO:
2440 			if (optlen != TCPOLEN_CC)
2441 				continue;
2442 			if (!(th->th_flags & TH_SYN))
2443 				continue;
2444 			to->to_flag |= TOF_CCECHO;
2445 			bcopy((char *)cp + 2,
2446 			    (char *)&to->to_ccecho, sizeof(to->to_ccecho));
2447 			NTOHL(to->to_ccecho);
2448 			break;
2449 		}
2450 	}
2451 	if (th->th_flags & TH_SYN)
2452 		tcp_mss(tp, mss);	/* sets t_maxseg */
2453 }
2454 
2455 /*
2456  * Pull out of band byte out of a segment so
2457  * it doesn't appear in the user's data queue.
2458  * It is still reflected in the segment length for
2459  * sequencing purposes.
2460  */
2461 static void
2462 tcp_pulloutofband(so, th, m, off)
2463 	struct socket *so;
2464 	struct tcphdr *th;
2465 	register struct mbuf *m;
2466 	int off;		/* delayed to be droped hdrlen */
2467 {
2468 	int cnt = off + th->th_urp - 1;
2469 
2470 	while (cnt >= 0) {
2471 		if (m->m_len > cnt) {
2472 			char *cp = mtod(m, caddr_t) + cnt;
2473 			struct tcpcb *tp = sototcpcb(so);
2474 
2475 			tp->t_iobc = *cp;
2476 			tp->t_oobflags |= TCPOOB_HAVEDATA;
2477 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
2478 			m->m_len--;
2479 			if (m->m_flags & M_PKTHDR)
2480 				m->m_pkthdr.len--;
2481 			return;
2482 		}
2483 		cnt -= m->m_len;
2484 		m = m->m_next;
2485 		if (m == 0)
2486 			break;
2487 	}
2488 	panic("tcp_pulloutofband");
2489 }
2490 
2491 /*
2492  * Collect new round-trip time estimate
2493  * and update averages and current timeout.
2494  */
2495 static void
2496 tcp_xmit_timer(tp, rtt)
2497 	register struct tcpcb *tp;
2498 	int rtt;
2499 {
2500 	register int delta;
2501 
2502 	tcpstat.tcps_rttupdated++;
2503 	tp->t_rttupdated++;
2504 	if (tp->t_srtt != 0) {
2505 		/*
2506 		 * srtt is stored as fixed point with 5 bits after the
2507 		 * binary point (i.e., scaled by 8).  The following magic
2508 		 * is equivalent to the smoothing algorithm in rfc793 with
2509 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
2510 		 * point).  Adjust rtt to origin 0.
2511 		 */
2512 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
2513 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
2514 
2515 		if ((tp->t_srtt += delta) <= 0)
2516 			tp->t_srtt = 1;
2517 
2518 		/*
2519 		 * We accumulate a smoothed rtt variance (actually, a
2520 		 * smoothed mean difference), then set the retransmit
2521 		 * timer to smoothed rtt + 4 times the smoothed variance.
2522 		 * rttvar is stored as fixed point with 4 bits after the
2523 		 * binary point (scaled by 16).  The following is
2524 		 * equivalent to rfc793 smoothing with an alpha of .75
2525 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
2526 		 * rfc793's wired-in beta.
2527 		 */
2528 		if (delta < 0)
2529 			delta = -delta;
2530 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
2531 		if ((tp->t_rttvar += delta) <= 0)
2532 			tp->t_rttvar = 1;
2533 	} else {
2534 		/*
2535 		 * No rtt measurement yet - use the unsmoothed rtt.
2536 		 * Set the variance to half the rtt (so our first
2537 		 * retransmit happens at 3*rtt).
2538 		 */
2539 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
2540 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
2541 	}
2542 	tp->t_rtttime = 0;
2543 	tp->t_rxtshift = 0;
2544 
2545 	/*
2546 	 * the retransmit should happen at rtt + 4 * rttvar.
2547 	 * Because of the way we do the smoothing, srtt and rttvar
2548 	 * will each average +1/2 tick of bias.  When we compute
2549 	 * the retransmit timer, we want 1/2 tick of rounding and
2550 	 * 1 extra tick because of +-1/2 tick uncertainty in the
2551 	 * firing of the timer.  The bias will give us exactly the
2552 	 * 1.5 tick we need.  But, because the bias is
2553 	 * statistical, we have to test that we don't drop below
2554 	 * the minimum feasible timer (which is 2 ticks).
2555 	 */
2556 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
2557 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
2558 
2559 	/*
2560 	 * We received an ack for a packet that wasn't retransmitted;
2561 	 * it is probably safe to discard any error indications we've
2562 	 * received recently.  This isn't quite right, but close enough
2563 	 * for now (a route might have failed after we sent a segment,
2564 	 * and the return path might not be symmetrical).
2565 	 */
2566 	tp->t_softerror = 0;
2567 }
2568 
2569 /*
2570  * Determine a reasonable value for maxseg size.
2571  * If the route is known, check route for mtu.
2572  * If none, use an mss that can be handled on the outgoing
2573  * interface without forcing IP to fragment; if bigger than
2574  * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
2575  * to utilize large mbufs.  If no route is found, route has no mtu,
2576  * or the destination isn't local, use a default, hopefully conservative
2577  * size (usually 512 or the default IP max size, but no more than the mtu
2578  * of the interface), as we can't discover anything about intervening
2579  * gateways or networks.  We also initialize the congestion/slow start
2580  * window to be a single segment if the destination isn't local.
2581  * While looking at the routing entry, we also initialize other path-dependent
2582  * parameters from pre-set or cached values in the routing entry.
2583  *
2584  * Also take into account the space needed for options that we
2585  * send regularly.  Make maxseg shorter by that amount to assure
2586  * that we can send maxseg amount of data even when the options
2587  * are present.  Store the upper limit of the length of options plus
2588  * data in maxopd.
2589  *
2590  * NOTE that this routine is only called when we process an incoming
2591  * segment, for outgoing segments only tcp_mssopt is called.
2592  *
2593  * In case of T/TCP, we call this routine during implicit connection
2594  * setup as well (offer = -1), to initialize maxseg from the cached
2595  * MSS of our peer.
2596  */
2597 void
2598 tcp_mss(tp, offer)
2599 	struct tcpcb *tp;
2600 	int offer;
2601 {
2602 	register struct rtentry *rt;
2603 	struct ifnet *ifp;
2604 	register int rtt, mss;
2605 	u_long bufsize;
2606 	struct inpcb *inp;
2607 	struct socket *so;
2608 	struct rmxp_tao *taop;
2609 	int origoffer = offer;
2610 #ifdef INET6
2611 	int isipv6;
2612 	int min_protoh;
2613 #endif
2614 
2615 	inp = tp->t_inpcb;
2616 #ifdef INET6
2617 	isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
2618 	min_protoh = isipv6 ? sizeof (struct ip6_hdr) + sizeof (struct tcphdr)
2619 			    : sizeof (struct tcpiphdr);
2620 #else
2621 #define min_protoh  (sizeof (struct tcpiphdr))
2622 #endif
2623 #ifdef INET6
2624 	if (isipv6)
2625 		rt = tcp_rtlookup6(inp);
2626 	else
2627 #endif
2628 	rt = tcp_rtlookup(inp);
2629 	if (rt == NULL) {
2630 		tp->t_maxopd = tp->t_maxseg =
2631 #ifdef INET6
2632 		isipv6 ? tcp_v6mssdflt :
2633 #endif /* INET6 */
2634 		tcp_mssdflt;
2635 		return;
2636 	}
2637 	ifp = rt->rt_ifp;
2638 	so = inp->inp_socket;
2639 
2640 	taop = rmx_taop(rt->rt_rmx);
2641 	/*
2642 	 * Offer == -1 means that we didn't receive SYN yet,
2643 	 * use cached value in that case;
2644 	 */
2645 	if (offer == -1)
2646 		offer = taop->tao_mssopt;
2647 	/*
2648 	 * Offer == 0 means that there was no MSS on the SYN segment,
2649 	 * in this case we use tcp_mssdflt.
2650 	 */
2651 	if (offer == 0)
2652 		offer =
2653 #ifdef INET6
2654 			isipv6 ? tcp_v6mssdflt :
2655 #endif /* INET6 */
2656 			tcp_mssdflt;
2657 	else
2658 		/*
2659 		 * Sanity check: make sure that maxopd will be large
2660 		 * enough to allow some data on segments even is the
2661 		 * all the option space is used (40bytes).  Otherwise
2662 		 * funny things may happen in tcp_output.
2663 		 */
2664 		offer = max(offer, 64);
2665 	taop->tao_mssopt = offer;
2666 
2667 	/*
2668 	 * While we're here, check if there's an initial rtt
2669 	 * or rttvar.  Convert from the route-table units
2670 	 * to scaled multiples of the slow timeout timer.
2671 	 */
2672 	if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
2673 		/*
2674 		 * XXX the lock bit for RTT indicates that the value
2675 		 * is also a minimum value; this is subject to time.
2676 		 */
2677 		if (rt->rt_rmx.rmx_locks & RTV_RTT)
2678 			tp->t_rttmin = rtt / (RTM_RTTUNIT / hz);
2679 		tp->t_srtt = rtt / (RTM_RTTUNIT / (hz * TCP_RTT_SCALE));
2680 		tcpstat.tcps_usedrtt++;
2681 		if (rt->rt_rmx.rmx_rttvar) {
2682 			tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
2683 			    (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE));
2684 			tcpstat.tcps_usedrttvar++;
2685 		} else {
2686 			/* default variation is +- 1 rtt */
2687 			tp->t_rttvar =
2688 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
2689 		}
2690 		TCPT_RANGESET(tp->t_rxtcur,
2691 			      ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
2692 			      tp->t_rttmin, TCPTV_REXMTMAX);
2693 	}
2694 	/*
2695 	 * if there's an mtu associated with the route, use it
2696 	 * else, use the link mtu.
2697 	 */
2698 	if (rt->rt_rmx.rmx_mtu)
2699 		mss = rt->rt_rmx.rmx_mtu - min_protoh;
2700 	else
2701 	{
2702 		mss =
2703 #ifdef INET6
2704 			(isipv6 ? nd_ifinfo[rt->rt_ifp->if_index].linkmtu :
2705 #endif
2706 			 ifp->if_mtu
2707 #ifdef INET6
2708 			 )
2709 #endif
2710 			- min_protoh;
2711 #ifdef INET6
2712 		if (isipv6) {
2713 			if (!in6_localaddr(&inp->in6p_faddr))
2714 				mss = min(mss, tcp_v6mssdflt);
2715 		} else
2716 #endif
2717 		if (!in_localaddr(inp->inp_faddr))
2718 			mss = min(mss, tcp_mssdflt);
2719 	}
2720 	mss = min(mss, offer);
2721 	/*
2722 	 * maxopd stores the maximum length of data AND options
2723 	 * in a segment; maxseg is the amount of data in a normal
2724 	 * segment.  We need to store this value (maxopd) apart
2725 	 * from maxseg, because now every segment carries options
2726 	 * and thus we normally have somewhat less data in segments.
2727 	 */
2728 	tp->t_maxopd = mss;
2729 
2730 	/*
2731 	 * In case of T/TCP, origoffer==-1 indicates, that no segments
2732 	 * were received yet.  In this case we just guess, otherwise
2733 	 * we do the same as before T/TCP.
2734 	 */
2735  	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
2736 	    (origoffer == -1 ||
2737 	     (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
2738 		mss -= TCPOLEN_TSTAMP_APPA;
2739  	if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
2740 	    (origoffer == -1 ||
2741 	     (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC))
2742 		mss -= TCPOLEN_CC_APPA;
2743 
2744 #if	(MCLBYTES & (MCLBYTES - 1)) == 0
2745 		if (mss > MCLBYTES)
2746 			mss &= ~(MCLBYTES-1);
2747 #else
2748 		if (mss > MCLBYTES)
2749 			mss = mss / MCLBYTES * MCLBYTES;
2750 #endif
2751 	/*
2752 	 * If there's a pipesize, change the socket buffer
2753 	 * to that size.  Make the socket buffers an integral
2754 	 * number of mss units; if the mss is larger than
2755 	 * the socket buffer, decrease the mss.
2756 	 */
2757 #ifdef RTV_SPIPE
2758 	if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0)
2759 #endif
2760 		bufsize = so->so_snd.sb_hiwat;
2761 	if (bufsize < mss)
2762 		mss = bufsize;
2763 	else {
2764 		bufsize = roundup(bufsize, mss);
2765 		if (bufsize > sb_max)
2766 			bufsize = sb_max;
2767 		(void)sbreserve(&so->so_snd, bufsize, so, NULL);
2768 	}
2769 	tp->t_maxseg = mss;
2770 
2771 #ifdef RTV_RPIPE
2772 	if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0)
2773 #endif
2774 		bufsize = so->so_rcv.sb_hiwat;
2775 	if (bufsize > mss) {
2776 		bufsize = roundup(bufsize, mss);
2777 		if (bufsize > sb_max)
2778 			bufsize = sb_max;
2779 		(void)sbreserve(&so->so_rcv, bufsize, so, NULL);
2780 	}
2781 
2782 	/*
2783 	 * Set the slow-start flight size depending on whether this
2784 	 * is a local network or not.
2785 	 */
2786 	if (
2787 #ifdef INET6
2788 	    (isipv6 && in6_localaddr(&inp->in6p_faddr)) ||
2789 	    (!isipv6 &&
2790 #endif
2791 	     in_localaddr(inp->inp_faddr)
2792 #ifdef INET6
2793 	     )
2794 #endif
2795 	    )
2796 		tp->snd_cwnd = mss * ss_fltsz_local;
2797 	else
2798 		tp->snd_cwnd = mss * ss_fltsz;
2799 
2800 	if (rt->rt_rmx.rmx_ssthresh) {
2801 		/*
2802 		 * There's some sort of gateway or interface
2803 		 * buffer limit on the path.  Use this to set
2804 		 * the slow start threshhold, but set the
2805 		 * threshold to no less than 2*mss.
2806 		 */
2807 		tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
2808 		tcpstat.tcps_usedssthresh++;
2809 	}
2810 }
2811 
2812 /*
2813  * Determine the MSS option to send on an outgoing SYN.
2814  */
2815 int
2816 tcp_mssopt(tp)
2817 	struct tcpcb *tp;
2818 {
2819 	struct rtentry *rt;
2820 #ifdef INET6
2821 	int isipv6;
2822 	int min_protoh;
2823 #endif
2824 
2825 #ifdef INET6
2826 	isipv6 = ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
2827 	min_protoh = isipv6 ? sizeof (struct ip6_hdr) + sizeof (struct tcphdr)
2828 			    : sizeof (struct tcpiphdr);
2829 #else
2830 #define min_protoh  (sizeof (struct tcpiphdr))
2831 #endif
2832 #ifdef INET6
2833 	if (isipv6)
2834 		rt = tcp_rtlookup6(tp->t_inpcb);
2835 	else
2836 #endif /* INET6 */
2837 	rt = tcp_rtlookup(tp->t_inpcb);
2838 	if (rt == NULL)
2839 		return
2840 #ifdef INET6
2841 			isipv6 ? tcp_v6mssdflt :
2842 #endif /* INET6 */
2843 			tcp_mssdflt;
2844 
2845 	return rt->rt_ifp->if_mtu - min_protoh;
2846 }
2847 
2848 
2849 /*
2850  * Checks for partial ack.  If partial ack arrives, force the retransmission
2851  * of the next unacknowledged segment, do not clear tp->t_dupacks, and return
2852  * 1.  By setting snd_nxt to ti_ack, this forces retransmission timer to
2853  * be started again.  If the ack advances at least to tp->snd_recover, return 0.
2854  */
2855 static int
2856 tcp_newreno(tp, th)
2857 	struct tcpcb *tp;
2858 	struct tcphdr *th;
2859 {
2860 	if (SEQ_LT(th->th_ack, tp->snd_recover)) {
2861 		tcp_seq onxt = tp->snd_nxt;
2862 		u_long  ocwnd = tp->snd_cwnd;
2863 
2864 		callout_stop(tp->tt_rexmt);
2865 		tp->t_rtttime = 0;
2866 		tp->snd_nxt = th->th_ack;
2867 		/*
2868 		 * Set snd_cwnd to one segment beyond acknowledged offset
2869 		 * (tp->snd_una has not yet been updated when this function
2870 		 *  is called)
2871 		 */
2872 		tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una);
2873 		(void) tcp_output(tp);
2874 		tp->snd_cwnd = ocwnd;
2875 		if (SEQ_GT(onxt, tp->snd_nxt))
2876 			tp->snd_nxt = onxt;
2877 		/*
2878 		 * Partial window deflation.  Relies on fact that tp->snd_una
2879 		 * not updated yet.
2880 		 */
2881 		tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_maxseg);
2882 		return (1);
2883 	}
2884 	return (0);
2885 }
2886