xref: /freebsd/sys/netinet/tcp_input.c (revision 61a36e3dfc9c0ae705d582a07752c26b71855c8d)
1df8bae1dSRodney W. Grimes /*
2e79adb8eSGarrett Wollman  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33e79adb8eSGarrett Wollman  *	@(#)tcp_input.c	8.12 (Berkeley) 5/24/95
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37f9e354dfSJulian Elischer #include "opt_ipfw.h"		/* for ipfw_fwd		*/
38fb59c426SYoshinobu Inoue #include "opt_inet6.h"
393a2a9f79SYoshinobu Inoue #include "opt_ipsec.h"
40c488362eSRobert Watson #include "opt_mac.h"
410cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
42e46cd3d4SDag-Erling Smørgrav #include "opt_tcp_input.h"
430cc12cc5SJoerg Wunsch 
44df8bae1dSRodney W. Grimes #include <sys/param.h>
4598163b98SPoul-Henning Kamp #include <sys/kernel.h>
46c488362eSRobert Watson #include <sys/mac.h>
47df8bae1dSRodney W. Grimes #include <sys/malloc.h>
48df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
49a29f300eSGarrett Wollman #include <sys/proc.h>		/* for proc0 declaration */
50df8bae1dSRodney W. Grimes #include <sys/protosw.h>
51960ed29cSSeigo Tanimura #include <sys/signalvar.h>
52df8bae1dSRodney W. Grimes #include <sys/socket.h>
53df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
54960ed29cSSeigo Tanimura #include <sys/sysctl.h>
55816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
56960ed29cSSeigo Tanimura #include <sys/systm.h>
57df8bae1dSRodney W. Grimes 
58e79adb8eSGarrett Wollman #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
59e79adb8eSGarrett Wollman 
60df8bae1dSRodney W. Grimes #include <net/if.h>
61df8bae1dSRodney W. Grimes #include <net/route.h>
62df8bae1dSRodney W. Grimes 
63df8bae1dSRodney W. Grimes #include <netinet/in.h>
64960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
65df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
66960ed29cSSeigo Tanimura #include <netinet/in_var.h>
67df8bae1dSRodney W. Grimes #include <netinet/ip.h>
68686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_icmp.h>	/* for ICMP_BANDLIM		*/
69686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM		*/
70df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
71686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
72686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
73686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
74960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h>
75960ed29cSSeigo Tanimura #include <netinet6/nd6.h>
76df8bae1dSRodney W. Grimes #include <netinet/tcp.h>
77df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
78df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
79df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
80df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
81fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h>
82df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
83610ee2f9SDavid Greenman #ifdef TCPDEBUG
84df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
85fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */
86fb59c426SYoshinobu Inoue 
87b9234fafSSam Leffler #ifdef FAST_IPSEC
88b9234fafSSam Leffler #include <netipsec/ipsec.h>
89b9234fafSSam Leffler #include <netipsec/ipsec6.h>
90b9234fafSSam Leffler #endif /*FAST_IPSEC*/
91b9234fafSSam Leffler 
92fb59c426SYoshinobu Inoue #ifdef IPSEC
93fb59c426SYoshinobu Inoue #include <netinet6/ipsec.h>
943a2a9f79SYoshinobu Inoue #include <netinet6/ipsec6.h>
95fb59c426SYoshinobu Inoue #include <netkey/key.h>
96fb59c426SYoshinobu Inoue #endif /*IPSEC*/
97fb59c426SYoshinobu Inoue 
98db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
99db4f9cc7SJonathan Lemon 
100fb59c426SYoshinobu Inoue MALLOC_DEFINE(M_TSEGQ, "tseg_qent", "TCP segment queue entry");
101df8bae1dSRodney W. Grimes 
102c068736aSJeffrey Hsu static const int tcprexmtthresh = 3;
1032f96f1f4SGarrett Wollman tcp_cc	tcp_ccgen;
1040312fbe9SPoul-Henning Kamp 
1052f96f1f4SGarrett Wollman struct	tcpstat tcpstat;
106c73d99b5SRuslan Ermilov SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RW,
1073d177f46SBill Fumerola     &tcpstat , tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
1080312fbe9SPoul-Henning Kamp 
109d78a37adSPaul Traina static int log_in_vain = 0;
110816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW,
1113d177f46SBill Fumerola     &log_in_vain, 0, "Log all incoming TCP connections");
112816a3d83SPoul-Henning Kamp 
11316f7f31fSGeoff Rehmet static int blackhole = 0;
11416f7f31fSGeoff Rehmet SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW,
11516f7f31fSGeoff Rehmet 	&blackhole, 0, "Do not send RST when dropping refused connections");
11616f7f31fSGeoff Rehmet 
117f498eeeeSDavid Greenman int tcp_delack_enabled = 1;
11884e33c9eSDavid Greenman SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW,
1193d177f46SBill Fumerola     &tcp_delack_enabled, 0,
1203d177f46SBill Fumerola     "Delay ACK to try and piggyback it onto a data packet");
121f498eeeeSDavid Greenman 
122f8613305SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN
123f8613305SDag-Erling Smørgrav static int drop_synfin = 0;
124f8613305SDag-Erling Smørgrav SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW,
125f8613305SDag-Erling Smørgrav     &drop_synfin, 0, "Drop TCP packets with SYN+FIN set");
126f8613305SDag-Erling Smørgrav #endif
127f8613305SDag-Erling Smørgrav 
128dba7bc6aSAndre Oppermann static int tcp_do_rfc3042 = 1;
129582a954bSJeffrey Hsu SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW,
130582a954bSJeffrey Hsu     &tcp_do_rfc3042, 0, "Enable RFC 3042 (Limited Transmit)");
131582a954bSJeffrey Hsu 
132dba7bc6aSAndre Oppermann static int tcp_do_rfc3390 = 1;
133da3a8a1aSJeffrey Hsu SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW,
134da3a8a1aSJeffrey Hsu     &tcp_do_rfc3390, 0,
135da3a8a1aSJeffrey Hsu     "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)");
136da3a8a1aSJeffrey Hsu 
13715bd2b43SDavid Greenman struct inpcbhead tcb;
138fb59c426SYoshinobu Inoue #define	tcb6	tcb  /* for KAME src sync over BSD*'s */
13915bd2b43SDavid Greenman struct inpcbinfo tcbinfo;
140f76fcf6dSJeffrey Hsu struct mtx	*tcbinfo_mtx;
141df8bae1dSRodney W. Grimes 
1424d77a549SAlfred Perlstein static void	 tcp_dooptions(struct tcpopt *, u_char *, int, int);
1434d77a549SAlfred Perlstein static void	 tcp_pulloutofband(struct socket *,
1444d77a549SAlfred Perlstein 		     struct tcphdr *, struct mbuf *, int);
1454d77a549SAlfred Perlstein static int	 tcp_reass(struct tcpcb *, struct tcphdr *, int *,
1464d77a549SAlfred Perlstein 		     struct mbuf *);
1474d77a549SAlfred Perlstein static void	 tcp_xmit_timer(struct tcpcb *, int);
148c068736aSJeffrey Hsu static void	 tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *);
149340c35deSJonathan Lemon static int	 tcp_timewait(struct tcptw *, struct tcpopt *,
150340c35deSJonathan Lemon 		     struct tcphdr *, struct mbuf *, int);
1510312fbe9SPoul-Henning Kamp 
152fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */
153fb59c426SYoshinobu Inoue #ifdef INET6
154fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \
155fb59c426SYoshinobu Inoue do { \
156fb59c426SYoshinobu Inoue 	if ((tp) && (tp)->t_inpcb && \
15797d8d152SAndre Oppermann 	    ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \
15897d8d152SAndre Oppermann 		nd6_nud_hint(NULL, NULL, 0); \
159fb59c426SYoshinobu Inoue } while (0)
160fb59c426SYoshinobu Inoue #else
161fb59c426SYoshinobu Inoue #define ND6_HINT(tp)
162fb59c426SYoshinobu Inoue #endif
163df8bae1dSRodney W. Grimes 
164df8bae1dSRodney W. Grimes /*
165262c1c1aSMatthew Dillon  * Indicate whether this ack should be delayed.  We can delay the ack if
166262c1c1aSMatthew Dillon  *	- there is no delayed ack timer in progress and
167262c1c1aSMatthew Dillon  *	- our last ack wasn't a 0-sized window.  We never want to delay
1683bfd6421SJonathan Lemon  *	  the ack that opens up a 0-sized window and
1693bfd6421SJonathan Lemon  *		- delayed acks are enabled or
1703bfd6421SJonathan Lemon  *		- this is a half-synchronized T/TCP connection.
171d8c85a26SJonathan Lemon  */
172d8c85a26SJonathan Lemon #define DELAY_ACK(tp)							\
173a14c749fSJonathan Lemon 	((!callout_active(tp->tt_delack) &&				\
174a14c749fSJonathan Lemon 	    (tp->t_flags & TF_RXWIN0SENT) == 0) &&			\
1753bfd6421SJonathan Lemon 	    (tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN)))
176d8c85a26SJonathan Lemon 
1770312fbe9SPoul-Henning Kamp static int
178fb59c426SYoshinobu Inoue tcp_reass(tp, th, tlenp, m)
179df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
180fb59c426SYoshinobu Inoue 	register struct tcphdr *th;
181fb59c426SYoshinobu Inoue 	int *tlenp;
182df8bae1dSRodney W. Grimes 	struct mbuf *m;
183df8bae1dSRodney W. Grimes {
184fb59c426SYoshinobu Inoue 	struct tseg_qent *q;
185fb59c426SYoshinobu Inoue 	struct tseg_qent *p = NULL;
186fb59c426SYoshinobu Inoue 	struct tseg_qent *nq;
187fb59c426SYoshinobu Inoue 	struct tseg_qent *te;
188df8bae1dSRodney W. Grimes 	struct socket *so = tp->t_inpcb->inp_socket;
189df8bae1dSRodney W. Grimes 	int flags;
190df8bae1dSRodney W. Grimes 
191df8bae1dSRodney W. Grimes 	/*
192fb59c426SYoshinobu Inoue 	 * Call with th==0 after become established to
193df8bae1dSRodney W. Grimes 	 * force pre-ESTABLISHED data up to user socket.
194df8bae1dSRodney W. Grimes 	 */
195fb59c426SYoshinobu Inoue 	if (th == 0)
196df8bae1dSRodney W. Grimes 		goto present;
197df8bae1dSRodney W. Grimes 
198fb59c426SYoshinobu Inoue 	/* Allocate a new queue entry. If we can't, just drop the pkt. XXX */
199fb59c426SYoshinobu Inoue 	MALLOC(te, struct tseg_qent *, sizeof (struct tseg_qent), M_TSEGQ,
200fb59c426SYoshinobu Inoue 	       M_NOWAIT);
201fb59c426SYoshinobu Inoue 	if (te == NULL) {
202fb59c426SYoshinobu Inoue 		tcpstat.tcps_rcvmemdrop++;
203fb59c426SYoshinobu Inoue 		m_freem(m);
204fb59c426SYoshinobu Inoue 		return (0);
205fb59c426SYoshinobu Inoue 	}
2066effc713SDoug Rabson 
207df8bae1dSRodney W. Grimes 	/*
208df8bae1dSRodney W. Grimes 	 * Find a segment which begins after this one does.
209df8bae1dSRodney W. Grimes 	 */
210fb59c426SYoshinobu Inoue 	LIST_FOREACH(q, &tp->t_segq, tqe_q) {
211fb59c426SYoshinobu Inoue 		if (SEQ_GT(q->tqe_th->th_seq, th->th_seq))
212df8bae1dSRodney W. Grimes 			break;
213fb59c426SYoshinobu Inoue 		p = q;
214fb59c426SYoshinobu Inoue 	}
215df8bae1dSRodney W. Grimes 
216df8bae1dSRodney W. Grimes 	/*
217df8bae1dSRodney W. Grimes 	 * If there is a preceding segment, it may provide some of
218df8bae1dSRodney W. Grimes 	 * our data already.  If so, drop the data from the incoming
219df8bae1dSRodney W. Grimes 	 * segment.  If it provides all of our data, drop us.
220df8bae1dSRodney W. Grimes 	 */
2216effc713SDoug Rabson 	if (p != NULL) {
222df8bae1dSRodney W. Grimes 		register int i;
223df8bae1dSRodney W. Grimes 		/* conversion to int (in i) handles seq wraparound */
224fb59c426SYoshinobu Inoue 		i = p->tqe_th->th_seq + p->tqe_len - th->th_seq;
225df8bae1dSRodney W. Grimes 		if (i > 0) {
226fb59c426SYoshinobu Inoue 			if (i >= *tlenp) {
227df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvduppack++;
228fb59c426SYoshinobu Inoue 				tcpstat.tcps_rcvdupbyte += *tlenp;
229df8bae1dSRodney W. Grimes 				m_freem(m);
230fb59c426SYoshinobu Inoue 				FREE(te, M_TSEGQ);
231a0292f23SGarrett Wollman 				/*
232a0292f23SGarrett Wollman 				 * Try to present any queued data
233a0292f23SGarrett Wollman 				 * at the left window edge to the user.
234a0292f23SGarrett Wollman 				 * This is needed after the 3-WHS
235a0292f23SGarrett Wollman 				 * completes.
236a0292f23SGarrett Wollman 				 */
237a0292f23SGarrett Wollman 				goto present;	/* ??? */
238df8bae1dSRodney W. Grimes 			}
239df8bae1dSRodney W. Grimes 			m_adj(m, i);
240fb59c426SYoshinobu Inoue 			*tlenp -= i;
241fb59c426SYoshinobu Inoue 			th->th_seq += i;
242df8bae1dSRodney W. Grimes 		}
243df8bae1dSRodney W. Grimes 	}
244df8bae1dSRodney W. Grimes 	tcpstat.tcps_rcvoopack++;
245fb59c426SYoshinobu Inoue 	tcpstat.tcps_rcvoobyte += *tlenp;
246df8bae1dSRodney W. Grimes 
247df8bae1dSRodney W. Grimes 	/*
248df8bae1dSRodney W. Grimes 	 * While we overlap succeeding segments trim them or,
249df8bae1dSRodney W. Grimes 	 * if they are completely covered, dequeue them.
250df8bae1dSRodney W. Grimes 	 */
2516effc713SDoug Rabson 	while (q) {
252fb59c426SYoshinobu Inoue 		register int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq;
253df8bae1dSRodney W. Grimes 		if (i <= 0)
254df8bae1dSRodney W. Grimes 			break;
255fb59c426SYoshinobu Inoue 		if (i < q->tqe_len) {
256fb59c426SYoshinobu Inoue 			q->tqe_th->th_seq += i;
257fb59c426SYoshinobu Inoue 			q->tqe_len -= i;
258fb59c426SYoshinobu Inoue 			m_adj(q->tqe_m, i);
259df8bae1dSRodney W. Grimes 			break;
260df8bae1dSRodney W. Grimes 		}
2616effc713SDoug Rabson 
262fb59c426SYoshinobu Inoue 		nq = LIST_NEXT(q, tqe_q);
263fb59c426SYoshinobu Inoue 		LIST_REMOVE(q, tqe_q);
264fb59c426SYoshinobu Inoue 		m_freem(q->tqe_m);
265fb59c426SYoshinobu Inoue 		FREE(q, M_TSEGQ);
2666effc713SDoug Rabson 		q = nq;
267df8bae1dSRodney W. Grimes 	}
268df8bae1dSRodney W. Grimes 
269fb59c426SYoshinobu Inoue 	/* Insert the new segment queue entry into place. */
270fb59c426SYoshinobu Inoue 	te->tqe_m = m;
271fb59c426SYoshinobu Inoue 	te->tqe_th = th;
272fb59c426SYoshinobu Inoue 	te->tqe_len = *tlenp;
273fb59c426SYoshinobu Inoue 
2746effc713SDoug Rabson 	if (p == NULL) {
275fb59c426SYoshinobu Inoue 		LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q);
2766effc713SDoug Rabson 	} else {
277fb59c426SYoshinobu Inoue 		LIST_INSERT_AFTER(p, te, tqe_q);
2786effc713SDoug Rabson 	}
279df8bae1dSRodney W. Grimes 
280df8bae1dSRodney W. Grimes present:
281df8bae1dSRodney W. Grimes 	/*
282df8bae1dSRodney W. Grimes 	 * Present data to user, advancing rcv_nxt through
283df8bae1dSRodney W. Grimes 	 * completed sequence space.
284df8bae1dSRodney W. Grimes 	 */
285a0292f23SGarrett Wollman 	if (!TCPS_HAVEESTABLISHED(tp->t_state))
286df8bae1dSRodney W. Grimes 		return (0);
287fb59c426SYoshinobu Inoue 	q = LIST_FIRST(&tp->t_segq);
288fb59c426SYoshinobu Inoue 	if (!q || q->tqe_th->th_seq != tp->rcv_nxt)
289df8bae1dSRodney W. Grimes 		return (0);
290df8bae1dSRodney W. Grimes 	do {
291fb59c426SYoshinobu Inoue 		tp->rcv_nxt += q->tqe_len;
292fb59c426SYoshinobu Inoue 		flags = q->tqe_th->th_flags & TH_FIN;
293fb59c426SYoshinobu Inoue 		nq = LIST_NEXT(q, tqe_q);
294fb59c426SYoshinobu Inoue 		LIST_REMOVE(q, tqe_q);
2954cc20ab1SSeigo Tanimura 		if (so->so_state & SS_CANTRCVMORE)
296fb59c426SYoshinobu Inoue 			m_freem(q->tqe_m);
2974cc20ab1SSeigo Tanimura 		else
298395bb186SSam Leffler 			sbappendstream(&so->so_rcv, q->tqe_m);
299fb59c426SYoshinobu Inoue 		FREE(q, M_TSEGQ);
3006effc713SDoug Rabson 		q = nq;
301fb59c426SYoshinobu Inoue 	} while (q && q->tqe_th->th_seq == tp->rcv_nxt);
302fb59c426SYoshinobu Inoue 	ND6_HINT(tp);
303df8bae1dSRodney W. Grimes 	sorwakeup(so);
304df8bae1dSRodney W. Grimes 	return (flags);
305df8bae1dSRodney W. Grimes }
306df8bae1dSRodney W. Grimes 
307df8bae1dSRodney W. Grimes /*
308df8bae1dSRodney W. Grimes  * TCP input routine, follows pages 65-76 of the
309df8bae1dSRodney W. Grimes  * protocol specification dated September, 1981 very closely.
310df8bae1dSRodney W. Grimes  */
311fb59c426SYoshinobu Inoue #ifdef INET6
312fb59c426SYoshinobu Inoue int
313fb59c426SYoshinobu Inoue tcp6_input(mp, offp, proto)
314fb59c426SYoshinobu Inoue 	struct mbuf **mp;
315fb59c426SYoshinobu Inoue 	int *offp, proto;
316fb59c426SYoshinobu Inoue {
317fb59c426SYoshinobu Inoue 	register struct mbuf *m = *mp;
31833841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6;
319fb59c426SYoshinobu Inoue 
320fb59c426SYoshinobu Inoue 	IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE);
321fb59c426SYoshinobu Inoue 
322fb59c426SYoshinobu Inoue 	/*
323fb59c426SYoshinobu Inoue 	 * draft-itojun-ipv6-tcp-to-anycast
324fb59c426SYoshinobu Inoue 	 * better place to put this in?
325fb59c426SYoshinobu Inoue 	 */
32633841545SHajimu UMEMOTO 	ia6 = ip6_getdstifaddr(m);
32733841545SHajimu UMEMOTO 	if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
328fb59c426SYoshinobu Inoue 		struct ip6_hdr *ip6;
329fb59c426SYoshinobu Inoue 
330fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
331fb59c426SYoshinobu Inoue 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
332fb59c426SYoshinobu Inoue 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
333fb59c426SYoshinobu Inoue 		return IPPROTO_DONE;
334fb59c426SYoshinobu Inoue 	}
335fb59c426SYoshinobu Inoue 
336f0ffb944SJulian Elischer 	tcp_input(m, *offp);
337fb59c426SYoshinobu Inoue 	return IPPROTO_DONE;
338fb59c426SYoshinobu Inoue }
339fb59c426SYoshinobu Inoue #endif
340fb59c426SYoshinobu Inoue 
341df8bae1dSRodney W. Grimes void
342f0ffb944SJulian Elischer tcp_input(m, off0)
343df8bae1dSRodney W. Grimes 	register struct mbuf *m;
344f0ffb944SJulian Elischer 	int off0;
345df8bae1dSRodney W. Grimes {
346fb59c426SYoshinobu Inoue 	register struct tcphdr *th;
347fb59c426SYoshinobu Inoue 	register struct ip *ip = NULL;
348fb59c426SYoshinobu Inoue 	register struct ipovly *ipov;
349f76fcf6dSJeffrey Hsu 	register struct inpcb *inp = NULL;
350e79adb8eSGarrett Wollman 	u_char *optp = NULL;
35126f9a767SRodney W. Grimes 	int optlen = 0;
352df8bae1dSRodney W. Grimes 	int len, tlen, off;
353fb59c426SYoshinobu Inoue 	int drop_hdrlen;
354df8bae1dSRodney W. Grimes 	register struct tcpcb *tp = 0;
355fb59c426SYoshinobu Inoue 	register int thflags;
35626f9a767SRodney W. Grimes 	struct socket *so = 0;
357df8bae1dSRodney W. Grimes 	int todrop, acked, ourfinisacked, needoutput = 0;
358a0292f23SGarrett Wollman 	u_long tiwin;
359a0292f23SGarrett Wollman 	struct tcpopt to;		/* options in this segment */
36097d8d152SAndre Oppermann 	struct rmxp_tao tao;		/* our TAO cache entry */
361f76fcf6dSJeffrey Hsu 	int headlocked = 0;
362c068736aSJeffrey Hsu 	struct sockaddr_in *next_hop = NULL;
363c068736aSJeffrey Hsu 	int rstreason; /* For badport_bandlim accounting purposes */
364c068736aSJeffrey Hsu 
365c068736aSJeffrey Hsu 	struct ip6_hdr *ip6 = NULL;
366c068736aSJeffrey Hsu #ifdef INET6
367c068736aSJeffrey Hsu 	int isipv6;
368c068736aSJeffrey Hsu #else
369c068736aSJeffrey Hsu 	const int isipv6 = 0;
370c068736aSJeffrey Hsu #endif
371f76fcf6dSJeffrey Hsu 
372610ee2f9SDavid Greenman #ifdef TCPDEBUG
373c068736aSJeffrey Hsu 	/*
374c068736aSJeffrey Hsu 	 * The size of tcp_saveipgen must be the size of the max ip header,
375c068736aSJeffrey Hsu 	 * now IPv6.
376c068736aSJeffrey Hsu 	 */
377410bb1bfSLuigi Rizzo 	u_char tcp_saveipgen[40];
378410bb1bfSLuigi Rizzo 	struct tcphdr tcp_savetcp;
379610ee2f9SDavid Greenman 	short ostate = 0;
380610ee2f9SDavid Greenman #endif
381c068736aSJeffrey Hsu 
3822b25acc1SLuigi Rizzo 	/* Grab info from MT_TAG mbufs prepended to the chain. */
3832b25acc1SLuigi Rizzo 	for (;m && m->m_type == MT_TAG; m = m->m_next) {
3845d846453SSam Leffler 		if (m->_m_tag_id == PACKET_TAG_IPFORWARD)
3852b25acc1SLuigi Rizzo 			next_hop = (struct sockaddr_in *)m->m_hdr.mh_data;
3862b25acc1SLuigi Rizzo 	}
387fb59c426SYoshinobu Inoue #ifdef INET6
388fb59c426SYoshinobu Inoue 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
389fb59c426SYoshinobu Inoue #endif
39097d8d152SAndre Oppermann 	bzero(&tao, sizeof(tao));
391a0292f23SGarrett Wollman 	bzero((char *)&to, sizeof(to));
392a0292f23SGarrett Wollman 
393df8bae1dSRodney W. Grimes 	tcpstat.tcps_rcvtotal++;
394fb59c426SYoshinobu Inoue 
395fb59c426SYoshinobu Inoue 	if (isipv6) {
396fb59c426SYoshinobu Inoue 		/* IP6_EXTHDR_CHECK() is already done at tcp6_input() */
397fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
398fb59c426SYoshinobu Inoue 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
399fb59c426SYoshinobu Inoue 		if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) {
400fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbadsum++;
401fb59c426SYoshinobu Inoue 			goto drop;
402fb59c426SYoshinobu Inoue 		}
403fb59c426SYoshinobu Inoue 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
40433841545SHajimu UMEMOTO 
40533841545SHajimu UMEMOTO 		/*
40633841545SHajimu UMEMOTO 		 * Be proactive about unspecified IPv6 address in source.
40733841545SHajimu UMEMOTO 		 * As we use all-zero to indicate unbounded/unconnected pcb,
40833841545SHajimu UMEMOTO 		 * unspecified IPv6 address can be used to confuse us.
40933841545SHajimu UMEMOTO 		 *
41033841545SHajimu UMEMOTO 		 * Note that packets with unspecified IPv6 destination is
41133841545SHajimu UMEMOTO 		 * already dropped in ip6_input.
41233841545SHajimu UMEMOTO 		 */
41333841545SHajimu UMEMOTO 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
41433841545SHajimu UMEMOTO 			/* XXX stat */
41533841545SHajimu UMEMOTO 			goto drop;
41633841545SHajimu UMEMOTO 		}
417c068736aSJeffrey Hsu 	} else {
418df8bae1dSRodney W. Grimes 		/*
419df8bae1dSRodney W. Grimes 		 * Get IP and TCP header together in first mbuf.
420df8bae1dSRodney W. Grimes 		 * Note: IP leaves IP header in first mbuf.
421df8bae1dSRodney W. Grimes 		 */
422fb59c426SYoshinobu Inoue 		if (off0 > sizeof (struct ip)) {
423df8bae1dSRodney W. Grimes 			ip_stripoptions(m, (struct mbuf *)0);
424fb59c426SYoshinobu Inoue 			off0 = sizeof(struct ip);
425fb59c426SYoshinobu Inoue 		}
426df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof (struct tcpiphdr)) {
427df8bae1dSRodney W. Grimes 			if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
428df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvshort++;
429df8bae1dSRodney W. Grimes 				return;
430df8bae1dSRodney W. Grimes 			}
431df8bae1dSRodney W. Grimes 		}
432fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
433fb59c426SYoshinobu Inoue 		ipov = (struct ipovly *)ip;
434db4f9cc7SJonathan Lemon 		th = (struct tcphdr *)((caddr_t)ip + off0);
435db4f9cc7SJonathan Lemon 		tlen = ip->ip_len;
436df8bae1dSRodney W. Grimes 
437db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
438db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
439db4f9cc7SJonathan Lemon 				th->th_sum = m->m_pkthdr.csum_data;
440db4f9cc7SJonathan Lemon 			else
441db4f9cc7SJonathan Lemon 				th->th_sum = in_pseudo(ip->ip_src.s_addr,
442c068736aSJeffrey Hsu 						ip->ip_dst.s_addr,
443c068736aSJeffrey Hsu 						htonl(m->m_pkthdr.csum_data +
444c068736aSJeffrey Hsu 							ip->ip_len +
445c068736aSJeffrey Hsu 							IPPROTO_TCP));
446db4f9cc7SJonathan Lemon 			th->th_sum ^= 0xffff;
4473c653157SHartmut Brandt #ifdef TCPDEBUG
4483c653157SHartmut Brandt 			ipov->ih_len = (u_short)tlen;
4493c653157SHartmut Brandt 			ipov->ih_len = htons(ipov->ih_len);
4503c653157SHartmut Brandt #endif
451db4f9cc7SJonathan Lemon 		} else {
452df8bae1dSRodney W. Grimes 			/*
453df8bae1dSRodney W. Grimes 			 * Checksum extended TCP header and data.
454df8bae1dSRodney W. Grimes 			 */
455df8bae1dSRodney W. Grimes 			len = sizeof (struct ip) + tlen;
456fb59c426SYoshinobu Inoue 			bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
457fb59c426SYoshinobu Inoue 			ipov->ih_len = (u_short)tlen;
458fd8e4ebcSMike Barcroft 			ipov->ih_len = htons(ipov->ih_len);
459fb59c426SYoshinobu Inoue 			th->th_sum = in_cksum(m, len);
460db4f9cc7SJonathan Lemon 		}
461fb59c426SYoshinobu Inoue 		if (th->th_sum) {
462df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbadsum++;
463df8bae1dSRodney W. Grimes 			goto drop;
464df8bae1dSRodney W. Grimes 		}
465fb59c426SYoshinobu Inoue #ifdef INET6
466fb59c426SYoshinobu Inoue 		/* Re-initialization for later version check */
467fb59c426SYoshinobu Inoue 		ip->ip_v = IPVERSION;
468fb59c426SYoshinobu Inoue #endif
469fb59c426SYoshinobu Inoue 	}
470df8bae1dSRodney W. Grimes 
471df8bae1dSRodney W. Grimes 	/*
472df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
473df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
474df8bae1dSRodney W. Grimes 	 */
475fb59c426SYoshinobu Inoue 	off = th->th_off << 2;
476df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
477df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvbadoff++;
478df8bae1dSRodney W. Grimes 		goto drop;
479df8bae1dSRodney W. Grimes 	}
480fb59c426SYoshinobu Inoue 	tlen -= off;	/* tlen is used instead of ti->ti_len */
481df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
482fb59c426SYoshinobu Inoue 		if (isipv6) {
483fb59c426SYoshinobu Inoue 			IP6_EXTHDR_CHECK(m, off0, off, );
484fb59c426SYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
485fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
486c068736aSJeffrey Hsu 		} else {
487df8bae1dSRodney W. Grimes 			if (m->m_len < sizeof(struct ip) + off) {
488c068736aSJeffrey Hsu 				if ((m = m_pullup(m, sizeof (struct ip) + off))
489c068736aSJeffrey Hsu 						== 0) {
490df8bae1dSRodney W. Grimes 					tcpstat.tcps_rcvshort++;
491df8bae1dSRodney W. Grimes 					return;
492df8bae1dSRodney W. Grimes 				}
493fb59c426SYoshinobu Inoue 				ip = mtod(m, struct ip *);
494fb59c426SYoshinobu Inoue 				ipov = (struct ipovly *)ip;
495fb59c426SYoshinobu Inoue 				th = (struct tcphdr *)((caddr_t)ip + off0);
496fb59c426SYoshinobu Inoue 			}
497df8bae1dSRodney W. Grimes 		}
498df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
499fb59c426SYoshinobu Inoue 		optp = (u_char *)(th + 1);
500df8bae1dSRodney W. Grimes 	}
501fb59c426SYoshinobu Inoue 	thflags = th->th_flags;
502df8bae1dSRodney W. Grimes 
503e46cd3d4SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN
504e46cd3d4SDag-Erling Smørgrav 	/*
505e46cd3d4SDag-Erling Smørgrav 	 * If the drop_synfin option is enabled, drop all packets with
506e46cd3d4SDag-Erling Smørgrav 	 * both the SYN and FIN bits set. This prevents e.g. nmap from
507e46cd3d4SDag-Erling Smørgrav 	 * identifying the TCP/IP stack.
508e46cd3d4SDag-Erling Smørgrav 	 *
509a589a70eSGarrett Wollman 	 * This is a violation of the TCP specification.
510e46cd3d4SDag-Erling Smørgrav 	 */
511fb59c426SYoshinobu Inoue 	if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN))
512e46cd3d4SDag-Erling Smørgrav 		goto drop;
513e46cd3d4SDag-Erling Smørgrav #endif
514e46cd3d4SDag-Erling Smørgrav 
515df8bae1dSRodney W. Grimes 	/*
516df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
517df8bae1dSRodney W. Grimes 	 */
518fd8e4ebcSMike Barcroft 	th->th_seq = ntohl(th->th_seq);
519fd8e4ebcSMike Barcroft 	th->th_ack = ntohl(th->th_ack);
520fd8e4ebcSMike Barcroft 	th->th_win = ntohs(th->th_win);
521fd8e4ebcSMike Barcroft 	th->th_urp = ntohs(th->th_urp);
522df8bae1dSRodney W. Grimes 
523df8bae1dSRodney W. Grimes 	/*
5243bfd6421SJonathan Lemon 	 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options,
525fb59c426SYoshinobu Inoue 	 * until after ip6_savecontrol() is called and before other functions
526fb59c426SYoshinobu Inoue 	 * which don't want those proto headers.
527fb59c426SYoshinobu Inoue 	 * Because ip6_savecontrol() is going to parse the mbuf to
528fb59c426SYoshinobu Inoue 	 * search for data to be passed up to user-land, it wants mbuf
529fb59c426SYoshinobu Inoue 	 * parameters to be unchanged.
53088ff5695SSUZUKI Shinsuke 	 * XXX: the call of ip6_savecontrol() has been obsoleted based on
53188ff5695SSUZUKI Shinsuke 	 * latest version of the advanced API (20020110).
532755c1f07SAndras Olah 	 */
533fb59c426SYoshinobu Inoue 	drop_hdrlen = off0 + off;
534755c1f07SAndras Olah 
535755c1f07SAndras Olah 	/*
536df8bae1dSRodney W. Grimes 	 * Locate pcb for segment.
537df8bae1dSRodney W. Grimes 	 */
538f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&tcbinfo);
539f76fcf6dSJeffrey Hsu 	headlocked = 1;
540df8bae1dSRodney W. Grimes findpcb:
5412b25acc1SLuigi Rizzo 	/* IPFIREWALL_FORWARD section */
542c068736aSJeffrey Hsu 	if (next_hop != NULL && isipv6 == 0) {	/* IPv6 support is not yet */
543f9e354dfSJulian Elischer 		/*
5442b25acc1SLuigi Rizzo 		 * Transparently forwarded. Pretend to be the destination.
545f9e354dfSJulian Elischer 		 * already got one like this?
546f9e354dfSJulian Elischer 		 */
547fb59c426SYoshinobu Inoue 		inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport,
548c068736aSJeffrey Hsu 					ip->ip_dst, th->th_dport,
549c068736aSJeffrey Hsu 					0, m->m_pkthdr.rcvif);
550f9e354dfSJulian Elischer 		if (!inp) {
551c068736aSJeffrey Hsu 			/* It's new.  Try find the ambushing socket. */
552f9e354dfSJulian Elischer 			inp = in_pcblookup_hash(&tcbinfo,
553fb59c426SYoshinobu Inoue 						ip->ip_src, th->th_sport,
5542b25acc1SLuigi Rizzo 						next_hop->sin_addr,
555c068736aSJeffrey Hsu 						next_hop->sin_port ?
556c068736aSJeffrey Hsu 						    ntohs(next_hop->sin_port) :
557c068736aSJeffrey Hsu 						    th->th_dport,
558c068736aSJeffrey Hsu 						1, m->m_pkthdr.rcvif);
559f9e354dfSJulian Elischer 		}
560c068736aSJeffrey Hsu 	} else {
561fb59c426SYoshinobu Inoue 		if (isipv6)
562c068736aSJeffrey Hsu 			inp = in6_pcblookup_hash(&tcbinfo,
563c068736aSJeffrey Hsu 						 &ip6->ip6_src, th->th_sport,
564c068736aSJeffrey Hsu 						 &ip6->ip6_dst, th->th_dport,
565c068736aSJeffrey Hsu 						 1, m->m_pkthdr.rcvif);
566fb59c426SYoshinobu Inoue 		else
567c068736aSJeffrey Hsu 			inp = in_pcblookup_hash(&tcbinfo,
568c068736aSJeffrey Hsu 						ip->ip_src, th->th_sport,
569c068736aSJeffrey Hsu 						ip->ip_dst, th->th_dport,
570c068736aSJeffrey Hsu 						1, m->m_pkthdr.rcvif);
571fb59c426SYoshinobu Inoue       }
572f9e354dfSJulian Elischer 
573fb59c426SYoshinobu Inoue #ifdef IPSEC
574fb59c426SYoshinobu Inoue 	if (isipv6) {
575fb59c426SYoshinobu Inoue 		if (inp != NULL && ipsec6_in_reject_so(m, inp->inp_socket)) {
576fb59c426SYoshinobu Inoue 			ipsec6stat.in_polvio++;
577fb59c426SYoshinobu Inoue 			goto drop;
578fb59c426SYoshinobu Inoue 		}
579c068736aSJeffrey Hsu 	} else {
580fb59c426SYoshinobu Inoue 		if (inp != NULL && ipsec4_in_reject_so(m, inp->inp_socket)) {
581fb59c426SYoshinobu Inoue 			ipsecstat.in_polvio++;
582fb59c426SYoshinobu Inoue 			goto drop;
583fb59c426SYoshinobu Inoue 		}
584c068736aSJeffrey Hsu 	}
585c068736aSJeffrey Hsu #endif
586b9234fafSSam Leffler #ifdef FAST_IPSEC
587b9234fafSSam Leffler 	if (isipv6) {
588b9234fafSSam Leffler 		if (inp != NULL && ipsec6_in_reject(m, inp)) {
589b9234fafSSam Leffler 			goto drop;
590b9234fafSSam Leffler 		}
59158fcadfcSSam Leffler 	} else
59258fcadfcSSam Leffler 	if (inp != NULL && ipsec4_in_reject(m, inp)) {
593b9234fafSSam Leffler 		goto drop;
594b9234fafSSam Leffler 	}
595b9234fafSSam Leffler #endif /*FAST_IPSEC*/
596df8bae1dSRodney W. Grimes 
597df8bae1dSRodney W. Grimes 	/*
598df8bae1dSRodney W. Grimes 	 * If the state is CLOSED (i.e., TCB does not exist) then
599df8bae1dSRodney W. Grimes 	 * all data in the incoming segment is discarded.
600df8bae1dSRodney W. Grimes 	 * If the TCB exists but is in CLOSED state, it is embryonic,
601df8bae1dSRodney W. Grimes 	 * but should either do a listen or a connect soon.
602df8bae1dSRodney W. Grimes 	 */
603816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
6042e4e1b4cSGeoff Rehmet 		if (log_in_vain) {
605fb59c426SYoshinobu Inoue #ifdef INET6
606ded7008aSJuli Mallett 			char dbuf[INET6_ADDRSTRLEN+2], sbuf[INET6_ADDRSTRLEN+2];
607c068736aSJeffrey Hsu #else
608fb59c426SYoshinobu Inoue 			char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"];
609c068736aSJeffrey Hsu #endif
61075cfc95fSAndrey A. Chernov 
611fb59c426SYoshinobu Inoue 			if (isipv6) {
612ded7008aSJuli Mallett 				strcpy(dbuf, "[");
613ded7008aSJuli Mallett 				strcpy(sbuf, "[");
614ded7008aSJuli Mallett 				strcat(dbuf, ip6_sprintf(&ip6->ip6_dst));
615ded7008aSJuli Mallett 				strcat(sbuf, ip6_sprintf(&ip6->ip6_src));
616ded7008aSJuli Mallett 				strcat(dbuf, "]");
617ded7008aSJuli Mallett 				strcat(sbuf, "]");
618c068736aSJeffrey Hsu 			} else {
619fb59c426SYoshinobu Inoue 				strcpy(dbuf, inet_ntoa(ip->ip_dst));
620fb59c426SYoshinobu Inoue 				strcpy(sbuf, inet_ntoa(ip->ip_src));
621fb59c426SYoshinobu Inoue 			}
6222e4e1b4cSGeoff Rehmet 			switch (log_in_vain) {
6232e4e1b4cSGeoff Rehmet 			case 1:
62439eb27a4SCrist J. Clark 				if ((thflags & TH_SYN) == 0)
6252e4e1b4cSGeoff Rehmet 					break;
626e4d2978dSPoul-Henning Kamp 				/* FALLTHROUGH */
6272e4e1b4cSGeoff Rehmet 			case 2:
6282e4e1b4cSGeoff Rehmet 				log(LOG_INFO,
629c068736aSJeffrey Hsu 				    "Connection attempt to TCP %s:%d "
63039eb27a4SCrist J. Clark 				    "from %s:%d flags:0x%02x\n",
631fb59c426SYoshinobu Inoue 				    dbuf, ntohs(th->th_dport), sbuf,
632fb59c426SYoshinobu Inoue 				    ntohs(th->th_sport), thflags);
6332e4e1b4cSGeoff Rehmet 				break;
6342e4e1b4cSGeoff Rehmet 			default:
6352e4e1b4cSGeoff Rehmet 				break;
6362e4e1b4cSGeoff Rehmet 			}
63775cfc95fSAndrey A. Chernov 		}
6382e4e1b4cSGeoff Rehmet 		if (blackhole) {
6392e4e1b4cSGeoff Rehmet 			switch (blackhole) {
640828b7f40SGeoff Rehmet 			case 1:
641fb59c426SYoshinobu Inoue 				if (thflags & TH_SYN)
642828b7f40SGeoff Rehmet 					goto drop;
643828b7f40SGeoff Rehmet 				break;
644828b7f40SGeoff Rehmet 			case 2:
645828b7f40SGeoff Rehmet 				goto drop;
646828b7f40SGeoff Rehmet 			default:
647828b7f40SGeoff Rehmet 				goto drop;
6482e4e1b4cSGeoff Rehmet 			}
649828b7f40SGeoff Rehmet 		}
650a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
651a57815efSBosko Milekic 		goto dropwithreset;
652816a3d83SPoul-Henning Kamp 	}
653f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
654340c35deSJonathan Lemon 	if (inp->inp_vflag & INP_TIMEWAIT) {
655340c35deSJonathan Lemon 		/*
656340c35deSJonathan Lemon 		 * The only option of relevance is TOF_CC, and only if
657340c35deSJonathan Lemon 		 * present in a SYN segment.  See tcp_timewait().
658340c35deSJonathan Lemon 		 */
659340c35deSJonathan Lemon 		if (thflags & TH_SYN)
660340c35deSJonathan Lemon 			tcp_dooptions(&to, optp, optlen, 1);
661340c35deSJonathan Lemon 		if (tcp_timewait((struct tcptw *)inp->inp_ppcb,
662340c35deSJonathan Lemon 		    &to, th, m, tlen))
663340c35deSJonathan Lemon 			goto findpcb;
664340c35deSJonathan Lemon 		/*
665340c35deSJonathan Lemon 		 * tcp_timewait unlocks inp.
666340c35deSJonathan Lemon 		 */
667340c35deSJonathan Lemon 		INP_INFO_WUNLOCK(&tcbinfo);
668340c35deSJonathan Lemon 		return;
669340c35deSJonathan Lemon 	}
670df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
67109f81a46SBosko Milekic 	if (tp == 0) {
672f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
673a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
674a57815efSBosko Milekic 		goto dropwithreset;
67509f81a46SBosko Milekic 	}
676df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_CLOSED)
677df8bae1dSRodney W. Grimes 		goto drop;
678df8bae1dSRodney W. Grimes 
679df8bae1dSRodney W. Grimes 	/* Unscale the window into a 32-bit value. */
680fb59c426SYoshinobu Inoue 	if ((thflags & TH_SYN) == 0)
681fb59c426SYoshinobu Inoue 		tiwin = th->th_win << tp->snd_scale;
682df8bae1dSRodney W. Grimes 	else
683fb59c426SYoshinobu Inoue 		tiwin = th->th_win;
684fb59c426SYoshinobu Inoue 
685c488362eSRobert Watson #ifdef MAC
686a557af22SRobert Watson 	if (mac_check_inpcb_deliver(inp, m))
687c488362eSRobert Watson 		goto drop;
688c488362eSRobert Watson #endif
689a557af22SRobert Watson 	so = inp->inp_socket;
690610ee2f9SDavid Greenman #ifdef TCPDEBUG
691df8bae1dSRodney W. Grimes 	if (so->so_options & SO_DEBUG) {
692df8bae1dSRodney W. Grimes 		ostate = tp->t_state;
693fb59c426SYoshinobu Inoue 		if (isipv6)
694540e8b7eSJeffrey Hsu 			bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6));
695fb59c426SYoshinobu Inoue 		else
696540e8b7eSJeffrey Hsu 			bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
697fb59c426SYoshinobu Inoue 		tcp_savetcp = *th;
698df8bae1dSRodney W. Grimes 	}
699610ee2f9SDavid Greenman #endif
700540e8b7eSJeffrey Hsu 	if (so->so_options & SO_ACCEPTCONN) {
701540e8b7eSJeffrey Hsu 		struct in_conninfo inc;
702540e8b7eSJeffrey Hsu 
703fb59c426SYoshinobu Inoue #ifdef INET6
704be2ac88cSJonathan Lemon 		inc.inc_isipv6 = isipv6;
705c068736aSJeffrey Hsu #endif
706be2ac88cSJonathan Lemon 		if (isipv6) {
707be2ac88cSJonathan Lemon 			inc.inc6_faddr = ip6->ip6_src;
708be2ac88cSJonathan Lemon 			inc.inc6_laddr = ip6->ip6_dst;
709c068736aSJeffrey Hsu 		} else {
710be2ac88cSJonathan Lemon 			inc.inc_faddr = ip->ip_src;
711be2ac88cSJonathan Lemon 			inc.inc_laddr = ip->ip_dst;
712be2ac88cSJonathan Lemon 		}
713be2ac88cSJonathan Lemon 		inc.inc_fport = th->th_sport;
714be2ac88cSJonathan Lemon 		inc.inc_lport = th->th_dport;
715be2ac88cSJonathan Lemon 
716fb59c426SYoshinobu Inoue 	        /*
717be2ac88cSJonathan Lemon 	         * If the state is LISTEN then ignore segment if it contains
718be2ac88cSJonathan Lemon 		 * a RST.  If the segment contains an ACK then it is bad and
719be2ac88cSJonathan Lemon 		 * send a RST.  If it does not contain a SYN then it is not
720be2ac88cSJonathan Lemon 		 * interesting; drop it.
721be2ac88cSJonathan Lemon 		 *
722be2ac88cSJonathan Lemon 		 * If the state is SYN_RECEIVED (syncache) and seg contains
723be2ac88cSJonathan Lemon 		 * an ACK, but not for our SYN/ACK, send a RST.  If the seg
724be2ac88cSJonathan Lemon 		 * contains a RST, check the sequence number to see if it
725be2ac88cSJonathan Lemon 		 * is a valid reset segment.
726fb59c426SYoshinobu Inoue 		 */
727fb59c426SYoshinobu Inoue 		if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
728be2ac88cSJonathan Lemon 			if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
729be2ac88cSJonathan Lemon 				if (!syncache_expand(&inc, th, &so, m)) {
7304195b4afSPaul Traina 					/*
731be2ac88cSJonathan Lemon 					 * No syncache entry, or ACK was not
732be2ac88cSJonathan Lemon 					 * for our SYN/ACK.  Send a RST.
7334195b4afSPaul Traina 					 */
734be2ac88cSJonathan Lemon 					tcpstat.tcps_badsyn++;
735be2ac88cSJonathan Lemon 					rstreason = BANDLIM_RST_OPENPORT;
736be2ac88cSJonathan Lemon 					goto dropwithreset;
737be2ac88cSJonathan Lemon 				}
738f76fcf6dSJeffrey Hsu 				if (so == NULL) {
739be2ac88cSJonathan Lemon 					/*
740be2ac88cSJonathan Lemon 					 * Could not complete 3-way handshake,
741be2ac88cSJonathan Lemon 					 * connection is being closed down, and
742be2ac88cSJonathan Lemon 					 * syncache will free mbuf.
743be2ac88cSJonathan Lemon 					 */
744f76fcf6dSJeffrey Hsu 					INP_UNLOCK(inp);
745f76fcf6dSJeffrey Hsu 					INP_INFO_WUNLOCK(&tcbinfo);
746be2ac88cSJonathan Lemon 					return;
747f76fcf6dSJeffrey Hsu 				}
748be2ac88cSJonathan Lemon 				/*
749be2ac88cSJonathan Lemon 				 * Socket is created in state SYN_RECEIVED.
750be2ac88cSJonathan Lemon 				 * Continue processing segment.
751be2ac88cSJonathan Lemon 				 */
752f76fcf6dSJeffrey Hsu 				INP_UNLOCK(inp);
753be2ac88cSJonathan Lemon 				inp = sotoinpcb(so);
754f76fcf6dSJeffrey Hsu 				INP_LOCK(inp);
755be2ac88cSJonathan Lemon 				tp = intotcpcb(inp);
756be2ac88cSJonathan Lemon 				/*
757be2ac88cSJonathan Lemon 				 * This is what would have happened in
758e6658b12SRobert Watson 				 * tcp_output() when the SYN,ACK was sent.
759be2ac88cSJonathan Lemon 				 */
760be2ac88cSJonathan Lemon 				tp->snd_up = tp->snd_una;
761be2ac88cSJonathan Lemon 				tp->snd_max = tp->snd_nxt = tp->iss + 1;
762be2ac88cSJonathan Lemon 				tp->last_ack_sent = tp->rcv_nxt;
763be2ac88cSJonathan Lemon 				/*
76441446225SJonathan Lemon 				 * RFC1323: The window in SYN & SYN/ACK
76541446225SJonathan Lemon 				 * segments is never scaled.
766be2ac88cSJonathan Lemon 				 */
767be2ac88cSJonathan Lemon 				tp->snd_wnd = tiwin;	/* unscaled */
768be2ac88cSJonathan Lemon 				goto after_listen;
769be2ac88cSJonathan Lemon 			}
770be2ac88cSJonathan Lemon 			if (thflags & TH_RST) {
771be2ac88cSJonathan Lemon 				syncache_chkrst(&inc, th);
772be2ac88cSJonathan Lemon 				goto drop;
773be2ac88cSJonathan Lemon 			}
774fb59c426SYoshinobu Inoue 			if (thflags & TH_ACK) {
775be2ac88cSJonathan Lemon 				syncache_badack(&inc);
776ebb0cbeaSPaul Traina 				tcpstat.tcps_badsyn++;
777a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
778a57815efSBosko Milekic 				goto dropwithreset;
7794195b4afSPaul Traina 			}
780ebb0cbeaSPaul Traina 			goto drop;
781ebb0cbeaSPaul Traina 		}
78233841545SHajimu UMEMOTO 
783be2ac88cSJonathan Lemon 		/*
784be2ac88cSJonathan Lemon 		 * Segment's flags are (SYN) or (SYN|FIN).
785be2ac88cSJonathan Lemon 		 */
78633841545SHajimu UMEMOTO #ifdef INET6
78733841545SHajimu UMEMOTO 		/*
78833841545SHajimu UMEMOTO 		 * If deprecated address is forbidden,
78933841545SHajimu UMEMOTO 		 * we do not accept SYN to deprecated interface
79033841545SHajimu UMEMOTO 		 * address to prevent any new inbound connection from
79133841545SHajimu UMEMOTO 		 * getting established.
79233841545SHajimu UMEMOTO 		 * When we do not accept SYN, we send a TCP RST,
79333841545SHajimu UMEMOTO 		 * with deprecated source address (instead of dropping
79433841545SHajimu UMEMOTO 		 * it).  We compromise it as it is much better for peer
79533841545SHajimu UMEMOTO 		 * to send a RST, and RST will be the final packet
79633841545SHajimu UMEMOTO 		 * for the exchange.
79733841545SHajimu UMEMOTO 		 *
79833841545SHajimu UMEMOTO 		 * If we do not forbid deprecated addresses, we accept
79933841545SHajimu UMEMOTO 		 * the SYN packet.  RFC2462 does not suggest dropping
80033841545SHajimu UMEMOTO 		 * SYN in this case.
80133841545SHajimu UMEMOTO 		 * If we decipher RFC2462 5.5.4, it says like this:
80233841545SHajimu UMEMOTO 		 * 1. use of deprecated addr with existing
80333841545SHajimu UMEMOTO 		 *    communication is okay - "SHOULD continue to be
80433841545SHajimu UMEMOTO 		 *    used"
80533841545SHajimu UMEMOTO 		 * 2. use of it with new communication:
80633841545SHajimu UMEMOTO 		 *   (2a) "SHOULD NOT be used if alternate address
80733841545SHajimu UMEMOTO 		 *        with sufficient scope is available"
80833841545SHajimu UMEMOTO 		 *   (2b) nothing mentioned otherwise.
80933841545SHajimu UMEMOTO 		 * Here we fall into (2b) case as we have no choice in
81033841545SHajimu UMEMOTO 		 * our source address selection - we must obey the peer.
81133841545SHajimu UMEMOTO 		 *
81233841545SHajimu UMEMOTO 		 * The wording in RFC2462 is confusing, and there are
81333841545SHajimu UMEMOTO 		 * multiple description text for deprecated address
81433841545SHajimu UMEMOTO 		 * handling - worse, they are not exactly the same.
81533841545SHajimu UMEMOTO 		 * I believe 5.5.4 is the best one, so we follow 5.5.4.
81633841545SHajimu UMEMOTO 		 */
81733841545SHajimu UMEMOTO 		if (isipv6 && !ip6_use_deprecated) {
81833841545SHajimu UMEMOTO 			struct in6_ifaddr *ia6;
81933841545SHajimu UMEMOTO 
82033841545SHajimu UMEMOTO 			if ((ia6 = ip6_getdstifaddr(m)) &&
82133841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
822f76fcf6dSJeffrey Hsu 				INP_UNLOCK(inp);
82333841545SHajimu UMEMOTO 				tp = NULL;
82433841545SHajimu UMEMOTO 				rstreason = BANDLIM_RST_OPENPORT;
82533841545SHajimu UMEMOTO 				goto dropwithreset;
82633841545SHajimu UMEMOTO 			}
82733841545SHajimu UMEMOTO 		}
82833841545SHajimu UMEMOTO #endif
82965f28919SJesper Skriver 		/*
830be2ac88cSJonathan Lemon 		 * If it is from this socket, drop it, it must be forged.
831be2ac88cSJonathan Lemon 		 * Don't bother responding if the destination was a broadcast.
83265f28919SJesper Skriver 		 */
833be2ac88cSJonathan Lemon 		if (th->th_dport == th->th_sport) {
834fb59c426SYoshinobu Inoue 			if (isipv6) {
835be2ac88cSJonathan Lemon 				if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
836be2ac88cSJonathan Lemon 						       &ip6->ip6_src))
837be2ac88cSJonathan Lemon 					goto drop;
838c068736aSJeffrey Hsu 			} else {
839be2ac88cSJonathan Lemon 				if (ip->ip_dst.s_addr == ip->ip_src.s_addr)
840be2ac88cSJonathan Lemon 					goto drop;
841be2ac88cSJonathan Lemon 			}
842c068736aSJeffrey Hsu 		}
843be2ac88cSJonathan Lemon 		/*
844be2ac88cSJonathan Lemon 		 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
8452ca2159fSCrist J. Clark 		 *
84693ec91baSCrist J. Clark 		 * Note that it is quite possible to receive unicast
84793ec91baSCrist J. Clark 		 * link-layer packets with a broadcast IP address. Use
84893ec91baSCrist J. Clark 		 * in_broadcast() to find them.
849be2ac88cSJonathan Lemon 		 */
850be2ac88cSJonathan Lemon 		if (m->m_flags & (M_BCAST|M_MCAST))
851be2ac88cSJonathan Lemon 			goto drop;
852be2ac88cSJonathan Lemon 		if (isipv6) {
853be2ac88cSJonathan Lemon 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
854be2ac88cSJonathan Lemon 			    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
855be2ac88cSJonathan Lemon 				goto drop;
856c068736aSJeffrey Hsu 		} else {
857be2ac88cSJonathan Lemon 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
858be2ac88cSJonathan Lemon 			    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
8592ca2159fSCrist J. Clark 			    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
8602ca2159fSCrist J. Clark 			    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
861be2ac88cSJonathan Lemon 				goto drop;
862c068736aSJeffrey Hsu 		}
863be2ac88cSJonathan Lemon 		/*
864be2ac88cSJonathan Lemon 		 * SYN appears to be valid; create compressed TCP state
865be2ac88cSJonathan Lemon 		 * for syncache, or perform t/tcp connection.
866be2ac88cSJonathan Lemon 		 */
867be2ac88cSJonathan Lemon 		if (so->so_qlen <= so->so_qlimit) {
8683c653157SHartmut Brandt #ifdef TCPDEBUG
8693c653157SHartmut Brandt 			if (so->so_options & SO_DEBUG)
8703c653157SHartmut Brandt 				tcp_trace(TA_INPUT, ostate, tp,
8713c653157SHartmut Brandt 				    (void *)tcp_saveipgen, &tcp_savetcp, 0);
8723c653157SHartmut Brandt #endif
873be2ac88cSJonathan Lemon 			tcp_dooptions(&to, optp, optlen, 1);
874be2ac88cSJonathan Lemon 			if (!syncache_add(&inc, &to, th, &so, m))
875be2ac88cSJonathan Lemon 				goto drop;
876f76fcf6dSJeffrey Hsu 			if (so == NULL) {
877be2ac88cSJonathan Lemon 				/*
878be2ac88cSJonathan Lemon 				 * Entry added to syncache, mbuf used to
879be2ac88cSJonathan Lemon 				 * send SYN,ACK packet.
880be2ac88cSJonathan Lemon 				 */
881f76fcf6dSJeffrey Hsu 				KASSERT(headlocked, ("headlocked"));
882f76fcf6dSJeffrey Hsu 				INP_UNLOCK(inp);
883f76fcf6dSJeffrey Hsu 				INP_INFO_WUNLOCK(&tcbinfo);
884be2ac88cSJonathan Lemon 				return;
885f76fcf6dSJeffrey Hsu 			}
886be2ac88cSJonathan Lemon 			/*
887be2ac88cSJonathan Lemon 			 * Segment passed TAO tests.
888be2ac88cSJonathan Lemon 			 */
889f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
890be2ac88cSJonathan Lemon 			inp = sotoinpcb(so);
891f76fcf6dSJeffrey Hsu 			INP_LOCK(inp);
892df8bae1dSRodney W. Grimes 			tp = intotcpcb(inp);
893be2ac88cSJonathan Lemon 			tp->snd_wnd = tiwin;
894be2ac88cSJonathan Lemon 			tp->t_starttime = ticks;
895be2ac88cSJonathan Lemon 			tp->t_state = TCPS_ESTABLISHED;
896df8bae1dSRodney W. Grimes 
897be2ac88cSJonathan Lemon 			/*
8983bfd6421SJonathan Lemon 			 * T/TCP logic:
8993bfd6421SJonathan Lemon 			 * If there is a FIN or if there is data, then
9003bfd6421SJonathan Lemon 			 * delay SYN,ACK(SYN) in the hope of piggy-backing
9013bfd6421SJonathan Lemon 			 * it on a response segment.  Otherwise must send
9023bfd6421SJonathan Lemon 			 * ACK now in case the other side is slow starting.
903be2ac88cSJonathan Lemon 			 */
9043bfd6421SJonathan Lemon 			if (thflags & TH_FIN || tlen != 0)
9053bfd6421SJonathan Lemon 				tp->t_flags |= (TF_DELACK | TF_NEEDSYN);
9063bfd6421SJonathan Lemon 			else
907f243998bSJonathan Lemon 				tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
908be2ac88cSJonathan Lemon 			tcpstat.tcps_connects++;
909be2ac88cSJonathan Lemon 			soisconnected(so);
910be2ac88cSJonathan Lemon 			goto trimthenstep6;
911df8bae1dSRodney W. Grimes 		}
912be2ac88cSJonathan Lemon 		goto drop;
9134cc20ab1SSeigo Tanimura 	}
914be2ac88cSJonathan Lemon after_listen:
915be2ac88cSJonathan Lemon 
916be2ac88cSJonathan Lemon 	/* XXX temp debugging */
917be2ac88cSJonathan Lemon 	/* should not happen - syncache should pick up these connections */
918be2ac88cSJonathan Lemon 	if (tp->t_state == TCPS_LISTEN)
919be2ac88cSJonathan Lemon 		panic("tcp_input: TCPS_LISTEN");
920df8bae1dSRodney W. Grimes 
921df8bae1dSRodney W. Grimes 	/*
92253369ac9SAndre Oppermann 	 * This is the second part of the MSS DoS prevention code (after
92353369ac9SAndre Oppermann 	 * minmss on the sending side) and it deals with too many too small
92453369ac9SAndre Oppermann 	 * tcp packets in a too short timeframe (1 second).
92553369ac9SAndre Oppermann 	 *
92653369ac9SAndre Oppermann 	 * For every full second we count the number of received packets
92753369ac9SAndre Oppermann 	 * and bytes. If we get a lot of packets per second for this connection
92853369ac9SAndre Oppermann 	 * (tcp_minmssoverload) we take a closer look at it and compute the
92953369ac9SAndre Oppermann 	 * average packet size for the past second. If that is less than
93053369ac9SAndre Oppermann 	 * tcp_minmss we get too many packets with very small payload which
93153369ac9SAndre Oppermann 	 * is not good and burdens our system (and every packet generates
93253369ac9SAndre Oppermann 	 * a wakeup to the process connected to our socket). We can reasonable
93353369ac9SAndre Oppermann 	 * expect this to be small packet DoS attack to exhaust our CPU
93453369ac9SAndre Oppermann 	 * cycles.
93553369ac9SAndre Oppermann 	 *
93653369ac9SAndre Oppermann 	 * Care has to be taken for the minimum packet overload value. This
93753369ac9SAndre Oppermann 	 * value defines the minimum number of packets per second before we
93853369ac9SAndre Oppermann 	 * start to worry. This must not be too low to avoid killing for
93953369ac9SAndre Oppermann 	 * example interactive connections with many small packets like
94053369ac9SAndre Oppermann 	 * telnet or SSH.
94153369ac9SAndre Oppermann 	 *
94253369ac9SAndre Oppermann 	 * Setting either tcp_minmssoverload or tcp_minmss to "0" disables
94353369ac9SAndre Oppermann 	 * this check.
94453369ac9SAndre Oppermann 	 *
94553369ac9SAndre Oppermann 	 * Account for packet if payload packet, skip over ACK, etc.
94653369ac9SAndre Oppermann 	 */
94753369ac9SAndre Oppermann 	if (tcp_minmss && tcp_minmssoverload &&
94853369ac9SAndre Oppermann 	    tp->t_state == TCPS_ESTABLISHED && tlen > 0) {
94953369ac9SAndre Oppermann 		if (tp->rcv_second > ticks) {
95053369ac9SAndre Oppermann 			tp->rcv_pps++;
95153369ac9SAndre Oppermann 			tp->rcv_byps += tlen + off;
95253369ac9SAndre Oppermann 			if (tp->rcv_pps > tcp_minmssoverload) {
95353369ac9SAndre Oppermann 				if ((tp->rcv_byps / tp->rcv_pps) < tcp_minmss) {
95453369ac9SAndre Oppermann 					printf("too many small tcp packets from "
95553369ac9SAndre Oppermann 					       "%s:%u, av. %lubyte/packet, "
95653369ac9SAndre Oppermann 					       "dropping connection\n",
95753369ac9SAndre Oppermann #ifdef INET6
95853369ac9SAndre Oppermann 						isipv6 ?
95953369ac9SAndre Oppermann 						ip6_sprintf(&inp->inp_inc.inc6_faddr) :
96053369ac9SAndre Oppermann #endif
96153369ac9SAndre Oppermann 						inet_ntoa(inp->inp_inc.inc_faddr),
96253369ac9SAndre Oppermann 						inp->inp_inc.inc_fport,
96353369ac9SAndre Oppermann 						tp->rcv_byps / tp->rcv_pps);
96453369ac9SAndre Oppermann 					tp = tcp_drop(tp, ECONNRESET);
96553369ac9SAndre Oppermann 					tcpstat.tcps_minmssdrops++;
96653369ac9SAndre Oppermann 					goto drop;
96753369ac9SAndre Oppermann 				}
96853369ac9SAndre Oppermann 			}
96953369ac9SAndre Oppermann 		} else {
97053369ac9SAndre Oppermann 			tp->rcv_second = ticks + hz;
97153369ac9SAndre Oppermann 			tp->rcv_pps = 1;
97253369ac9SAndre Oppermann 			tp->rcv_byps = tlen + off;
97353369ac9SAndre Oppermann 		}
97453369ac9SAndre Oppermann 	}
97553369ac9SAndre Oppermann 
97653369ac9SAndre Oppermann 	/*
977df8bae1dSRodney W. Grimes 	 * Segment received on connection.
978df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
979df8bae1dSRodney W. Grimes 	 */
9809b8b58e0SJonathan Lemon 	tp->t_rcvtime = ticks;
9817ff19458SPaul Traina 	if (TCPS_HAVEESTABLISHED(tp->t_state))
9829b8b58e0SJonathan Lemon 		callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp);
983df8bae1dSRodney W. Grimes 
984df8bae1dSRodney W. Grimes 	/*
98597d8d152SAndre Oppermann 	 * Process options only when we get SYN/ACK back. The SYN case
98697d8d152SAndre Oppermann 	 * for incoming connections is handled in tcp_syncache.
98797d8d152SAndre Oppermann 	 * XXX this is traditional behavior, may need to be cleaned up.
988df8bae1dSRodney W. Grimes 	 */
989be2ac88cSJonathan Lemon 	tcp_dooptions(&to, optp, optlen, thflags & TH_SYN);
990be2ac88cSJonathan Lemon 	if (thflags & TH_SYN) {
991be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_SCALE) {
992be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_SCALE;
993be2ac88cSJonathan Lemon 			tp->requested_s_scale = to.to_requested_s_scale;
994be2ac88cSJonathan Lemon 		}
995be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_TS) {
996be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_TSTMP;
997be2ac88cSJonathan Lemon 			tp->ts_recent = to.to_tsval;
998be2ac88cSJonathan Lemon 			tp->ts_recent_age = ticks;
999be2ac88cSJonathan Lemon 		}
1000be2ac88cSJonathan Lemon 		if (to.to_flags & (TOF_CC|TOF_CCNEW))
1001be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_CC;
1002be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_MSS)
1003be2ac88cSJonathan Lemon 			tcp_mss(tp, to.to_mss);
1004be2ac88cSJonathan Lemon 	}
1005df8bae1dSRodney W. Grimes 
1006df8bae1dSRodney W. Grimes 	/*
1007df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
1008df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
1009df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
1010df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
1011df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
1012df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
1013df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
1014df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
1015df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
1016df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
1017df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
1018df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
1019a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
1020a0292f23SGarrett Wollman 	 * Since we check for TCPS_ESTABLISHED above, it can only
1021a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
1022df8bae1dSRodney W. Grimes 	 */
1023df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
1024fb59c426SYoshinobu Inoue 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1025a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
1026be2ac88cSJonathan Lemon 	    ((to.to_flags & TOF_TS) == 0 ||
1027a0292f23SGarrett Wollman 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) &&
1028a0292f23SGarrett Wollman 	    /*
1029a0292f23SGarrett Wollman 	     * Using the CC option is compulsory if once started:
1030a0292f23SGarrett Wollman 	     *   the segment is OK if no T/TCP was negotiated or
1031a0292f23SGarrett Wollman 	     *   if the segment has a CC option equal to CCrecv
1032a0292f23SGarrett Wollman 	     */
1033a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) ||
1034be2ac88cSJonathan Lemon 	     ((to.to_flags & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) &&
1035fb59c426SYoshinobu Inoue 	    th->th_seq == tp->rcv_nxt &&
1036df8bae1dSRodney W. Grimes 	    tiwin && tiwin == tp->snd_wnd &&
1037df8bae1dSRodney W. Grimes 	    tp->snd_nxt == tp->snd_max) {
1038df8bae1dSRodney W. Grimes 
1039df8bae1dSRodney W. Grimes 		/*
1040df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
1041df8bae1dSRodney W. Grimes 		 * record the timestamp.
1042a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
1043a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1044df8bae1dSRodney W. Grimes 		 */
1045be2ac88cSJonathan Lemon 		if ((to.to_flags & TOF_TS) != 0 &&
1046fb59c426SYoshinobu Inoue 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
10479b8b58e0SJonathan Lemon 			tp->ts_recent_age = ticks;
1048a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
1049df8bae1dSRodney W. Grimes 		}
1050df8bae1dSRodney W. Grimes 
1051fb59c426SYoshinobu Inoue 		if (tlen == 0) {
1052fb59c426SYoshinobu Inoue 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
1053fb59c426SYoshinobu Inoue 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
1054233e8c18SGarrett Wollman 			    tp->snd_cwnd >= tp->snd_wnd &&
1055cb942153SJeffrey Hsu 			    ((!tcp_do_newreno &&
1056cb942153SJeffrey Hsu 			      tp->t_dupacks < tcprexmtthresh) ||
10579d11646dSJeffrey Hsu 			     (tcp_do_newreno && !IN_FASTRECOVERY(tp)))) {
1058f76fcf6dSJeffrey Hsu 				KASSERT(headlocked, ("headlocked"));
1059f76fcf6dSJeffrey Hsu 				INP_INFO_WUNLOCK(&tcbinfo);
1060df8bae1dSRodney W. Grimes 				/*
1061df8bae1dSRodney W. Grimes 				 * this is a pure ack for outstanding data.
1062df8bae1dSRodney W. Grimes 				 */
1063df8bae1dSRodney W. Grimes 				++tcpstat.tcps_predack;
10649b8b58e0SJonathan Lemon 				/*
10659b8b58e0SJonathan Lemon 				 * "bad retransmit" recovery
10669b8b58e0SJonathan Lemon 				 */
10679b8b58e0SJonathan Lemon 				if (tp->t_rxtshift == 1 &&
10689b8b58e0SJonathan Lemon 				    ticks < tp->t_badrxtwin) {
1069cb942153SJeffrey Hsu 					++tcpstat.tcps_sndrexmitbad;
10709b8b58e0SJonathan Lemon 					tp->snd_cwnd = tp->snd_cwnd_prev;
10719b8b58e0SJonathan Lemon 					tp->snd_ssthresh =
10729b8b58e0SJonathan Lemon 					    tp->snd_ssthresh_prev;
10739d11646dSJeffrey Hsu 					tp->snd_recover = tp->snd_recover_prev;
10749d11646dSJeffrey Hsu 					if (tp->t_flags & TF_WASFRECOVERY)
10759d11646dSJeffrey Hsu 					    ENTER_FASTRECOVERY(tp);
10769b8b58e0SJonathan Lemon 					tp->snd_nxt = tp->snd_max;
10779b8b58e0SJonathan Lemon 					tp->t_badrxtwin = 0;
10789b8b58e0SJonathan Lemon 				}
1079fa55172bSMatthew Dillon 
1080fa55172bSMatthew Dillon 				/*
1081fa55172bSMatthew Dillon 				 * Recalculate the transmit timer / rtt.
1082fa55172bSMatthew Dillon 				 *
1083fa55172bSMatthew Dillon 				 * Some boxes send broken timestamp replies
1084fa55172bSMatthew Dillon 				 * during the SYN+ACK phase, ignore
1085fa55172bSMatthew Dillon 				 * timestamps of 0 or we could calculate a
1086fa55172bSMatthew Dillon 				 * huge RTT and blow up the retransmit timer.
1087fa55172bSMatthew Dillon 				 */
1088fa55172bSMatthew Dillon 				if ((to.to_flags & TOF_TS) != 0 &&
1089fa55172bSMatthew Dillon 				    to.to_tsecr) {
1090a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
10919b8b58e0SJonathan Lemon 					    ticks - to.to_tsecr + 1);
1092fa55172bSMatthew Dillon 				} else if (tp->t_rtttime &&
1093fa55172bSMatthew Dillon 					    SEQ_GT(th->th_ack, tp->t_rtseq)) {
1094c068736aSJeffrey Hsu 					tcp_xmit_timer(tp,
1095c068736aSJeffrey Hsu 							ticks - tp->t_rtttime);
1096fa55172bSMatthew Dillon 				}
10971fcc99b5SMatthew Dillon 				tcp_xmit_bandwidth_limit(tp, th->th_ack);
1098fb59c426SYoshinobu Inoue 				acked = th->th_ack - tp->snd_una;
1099df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvackpack++;
1100df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvackbyte += acked;
1101df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
11029d11646dSJeffrey Hsu 				if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
11039d11646dSJeffrey Hsu 				    SEQ_LEQ(th->th_ack, tp->snd_recover))
11049d11646dSJeffrey Hsu 					tp->snd_recover = th->th_ack - 1;
11059d11646dSJeffrey Hsu 				tp->snd_una = th->th_ack;
11061645d090SJeff Roberson 				/*
11071645d090SJeff Roberson 				 * pull snd_wl2 up to prevent seq wrap relative
11081645d090SJeff Roberson 				 * to th_ack.
11091645d090SJeff Roberson 				 */
1110cb942153SJeffrey Hsu 				tp->snd_wl2 = th->th_ack;
1111b5addd85SJeffrey Hsu 				tp->t_dupacks = 0;
1112df8bae1dSRodney W. Grimes 				m_freem(m);
1113fb59c426SYoshinobu Inoue 				ND6_HINT(tp); /* some progress has been done */
1114df8bae1dSRodney W. Grimes 
1115df8bae1dSRodney W. Grimes 				/*
1116df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
1117df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
1118df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
1119df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
1120df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
1121df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
1122df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
11233c653157SHartmut Brandt 
11243c653157SHartmut Brandt #ifdef TCPDEBUG
11253c653157SHartmut Brandt 				if (so->so_options & SO_DEBUG)
11263c653157SHartmut Brandt 					tcp_trace(TA_INPUT, ostate, tp,
11273c653157SHartmut Brandt 					    (void *)tcp_saveipgen,
11283c653157SHartmut Brandt 					    &tcp_savetcp, 0);
11293c653157SHartmut Brandt #endif
1130df8bae1dSRodney W. Grimes 				 */
1131df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
11329b8b58e0SJonathan Lemon 					callout_stop(tp->tt_rexmt);
11339b8b58e0SJonathan Lemon 				else if (!callout_active(tp->tt_persist))
11349b8b58e0SJonathan Lemon 					callout_reset(tp->tt_rexmt,
11359b8b58e0SJonathan Lemon 						      tp->t_rxtcur,
11369b8b58e0SJonathan Lemon 						      tcp_timer_rexmt, tp);
1137df8bae1dSRodney W. Grimes 
1138df8bae1dSRodney W. Grimes 				sowwakeup(so);
1139df8bae1dSRodney W. Grimes 				if (so->so_snd.sb_cc)
1140df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
1141a14c749fSJonathan Lemon 				goto check_delack;
1142df8bae1dSRodney W. Grimes 			}
1143fb59c426SYoshinobu Inoue 		} else if (th->th_ack == tp->snd_una &&
1144fb59c426SYoshinobu Inoue 		    LIST_EMPTY(&tp->t_segq) &&
1145fb59c426SYoshinobu Inoue 		    tlen <= sbspace(&so->so_rcv)) {
1146f76fcf6dSJeffrey Hsu 			KASSERT(headlocked, ("headlocked"));
1147f76fcf6dSJeffrey Hsu 			INP_INFO_WUNLOCK(&tcbinfo);
1148df8bae1dSRodney W. Grimes 			/*
1149df8bae1dSRodney W. Grimes 			 * this is a pure, in-sequence data packet
1150df8bae1dSRodney W. Grimes 			 * with nothing on the reassembly queue and
1151df8bae1dSRodney W. Grimes 			 * we have enough buffer space to take it.
1152df8bae1dSRodney W. Grimes 			 */
1153df8bae1dSRodney W. Grimes 			++tcpstat.tcps_preddat;
1154fb59c426SYoshinobu Inoue 			tp->rcv_nxt += tlen;
11551645d090SJeff Roberson 			/*
11561645d090SJeff Roberson 			 * Pull snd_wl1 up to prevent seq wrap relative to
11571645d090SJeff Roberson 			 * th_seq.
11581645d090SJeff Roberson 			 */
11591645d090SJeff Roberson 			tp->snd_wl1 = th->th_seq;
11601645d090SJeff Roberson 			/*
11611645d090SJeff Roberson 			 * Pull rcv_up up to prevent seq wrap relative to
11621645d090SJeff Roberson 			 * rcv_nxt.
11631645d090SJeff Roberson 			 */
11641645d090SJeff Roberson 			tp->rcv_up = tp->rcv_nxt;
1165df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpack++;
1166fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbyte += tlen;
1167fb59c426SYoshinobu Inoue 			ND6_HINT(tp);	/* some progress has been done */
1168df8bae1dSRodney W. Grimes 			/*
11693c653157SHartmut Brandt #ifdef TCPDEBUG
11703c653157SHartmut Brandt 			if (so->so_options & SO_DEBUG)
11713c653157SHartmut Brandt 				tcp_trace(TA_INPUT, ostate, tp,
11723c653157SHartmut Brandt 				    (void *)tcp_saveipgen, &tcp_savetcp, 0);
11733c653157SHartmut Brandt #endif
1174755c1f07SAndras Olah 			 * Add data to socket buffer.
1175df8bae1dSRodney W. Grimes 			 */
1176c1c36a2cSMike Silbersack 			if (so->so_state & SS_CANTRCVMORE) {
1177c1c36a2cSMike Silbersack 				m_freem(m);
1178c1c36a2cSMike Silbersack 			} else {
1179fb59c426SYoshinobu Inoue 				m_adj(m, drop_hdrlen);	/* delayed header drop */
1180395bb186SSam Leffler 				sbappendstream(&so->so_rcv, m);
1181c1c36a2cSMike Silbersack 			}
1182df8bae1dSRodney W. Grimes 			sorwakeup(so);
1183d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp)) {
11843bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
1185f498eeeeSDavid Greenman 			} else {
1186e612a582SDavid Greenman 				tp->t_flags |= TF_ACKNOW;
1187e612a582SDavid Greenman 				tcp_output(tp);
1188e612a582SDavid Greenman 			}
1189a14c749fSJonathan Lemon 			goto check_delack;
1190df8bae1dSRodney W. Grimes 		}
1191df8bae1dSRodney W. Grimes 	}
1192df8bae1dSRodney W. Grimes 
1193df8bae1dSRodney W. Grimes 	/*
1194df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
1195df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
1196df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
1197df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
1198df8bae1dSRodney W. Grimes 	 */
1199df8bae1dSRodney W. Grimes 	{ int win;
1200df8bae1dSRodney W. Grimes 
1201df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
1202df8bae1dSRodney W. Grimes 	if (win < 0)
1203df8bae1dSRodney W. Grimes 		win = 0;
120466e39adcSJohn Polstra 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1205df8bae1dSRodney W. Grimes 	}
1206df8bae1dSRodney W. Grimes 
1207df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1208df8bae1dSRodney W. Grimes 
1209df8bae1dSRodney W. Grimes 	/*
1210764d8cefSBill Fenner 	 * If the state is SYN_RECEIVED:
1211764d8cefSBill Fenner 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
1212764d8cefSBill Fenner 	 */
1213764d8cefSBill Fenner 	case TCPS_SYN_RECEIVED:
1214fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1215fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1216a57815efSBosko Milekic 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1217a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
1218a57815efSBosko Milekic 				goto dropwithreset;
1219a57815efSBosko Milekic 		}
1220764d8cefSBill Fenner 		break;
1221764d8cefSBill Fenner 
1222764d8cefSBill Fenner 	/*
1223df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
1224df8bae1dSRodney W. Grimes 	 *	if seg contains an ACK, but not for our SYN, drop the input.
1225df8bae1dSRodney W. Grimes 	 *	if seg contains a RST, then drop the connection.
1226df8bae1dSRodney W. Grimes 	 *	if seg does not contain SYN, then drop it.
1227df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
1228df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
1229df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
1230df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1231df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
1232df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
1233df8bae1dSRodney W. Grimes 	 */
1234df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
123597d8d152SAndre Oppermann 		if (tcp_do_rfc1644)
123697d8d152SAndre Oppermann 			tcp_hc_gettao(&inp->inp_inc, &tao);
1237a0292f23SGarrett Wollman 
1238fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1239fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->iss) ||
1240fb59c426SYoshinobu Inoue 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1241a0292f23SGarrett Wollman 			/*
1242a0292f23SGarrett Wollman 			 * If we have a cached CCsent for the remote host,
1243a0292f23SGarrett Wollman 			 * hence we haven't just crashed and restarted,
1244a0292f23SGarrett Wollman 			 * do not send a RST.  This may be a retransmission
1245a0292f23SGarrett Wollman 			 * from the other side after our earlier ACK was lost.
1246a0292f23SGarrett Wollman 			 * Our new SYN, when it arrives, will serve as the
1247a0292f23SGarrett Wollman 			 * needed ACK.
1248a0292f23SGarrett Wollman 			 */
124997d8d152SAndre Oppermann 			if (tao.tao_ccsent != 0)
1250a0292f23SGarrett Wollman 				goto drop;
1251a57815efSBosko Milekic 			else {
1252a57815efSBosko Milekic 				rstreason = BANDLIM_UNLIMITED;
1253a0292f23SGarrett Wollman 				goto dropwithreset;
1254a0292f23SGarrett Wollman 			}
1255a57815efSBosko Milekic 		}
1256fb59c426SYoshinobu Inoue 		if (thflags & TH_RST) {
1257fb59c426SYoshinobu Inoue 			if (thflags & TH_ACK)
1258df8bae1dSRodney W. Grimes 				tp = tcp_drop(tp, ECONNREFUSED);
1259df8bae1dSRodney W. Grimes 			goto drop;
1260df8bae1dSRodney W. Grimes 		}
1261fb59c426SYoshinobu Inoue 		if ((thflags & TH_SYN) == 0)
1262df8bae1dSRodney W. Grimes 			goto drop;
1263fb59c426SYoshinobu Inoue 		tp->snd_wnd = th->th_win;	/* initial send window */
1264a0292f23SGarrett Wollman 		tp->cc_recv = to.to_cc;		/* foreign CC */
1265a0292f23SGarrett Wollman 
1266fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
1267df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
1268fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1269a0292f23SGarrett Wollman 			/*
1270a0292f23SGarrett Wollman 			 * Our SYN was acked.  If segment contains CC.ECHO
1271a0292f23SGarrett Wollman 			 * option, check it to make sure this segment really
1272a0292f23SGarrett Wollman 			 * matches our SYN.  If not, just drop it as old
1273a0292f23SGarrett Wollman 			 * duplicate, but send an RST if we're still playing
1274026650e5SBill Fenner 			 * by the old rules.  If no CC.ECHO option, make sure
1275026650e5SBill Fenner 			 * we don't get fooled into using T/TCP.
1276a0292f23SGarrett Wollman 			 */
1277be2ac88cSJonathan Lemon 			if (to.to_flags & TOF_CCECHO) {
1278dfd5dee1SPeter Wemm 				if (tp->cc_send != to.to_ccecho) {
127997d8d152SAndre Oppermann 					if (tao.tao_ccsent != 0)
1280a0292f23SGarrett Wollman 						goto drop;
1281a57815efSBosko Milekic 					else {
1282a57815efSBosko Milekic 						rstreason = BANDLIM_UNLIMITED;
1283a0292f23SGarrett Wollman 						goto dropwithreset;
1284dfd5dee1SPeter Wemm 					}
1285a57815efSBosko Milekic 				}
1286026650e5SBill Fenner 			} else
1287026650e5SBill Fenner 				tp->t_flags &= ~TF_RCVD_CC;
1288845799c1SAndras Olah 			tcpstat.tcps_connects++;
1289845799c1SAndras Olah 			soisconnected(so);
1290c488362eSRobert Watson #ifdef MAC
1291c488362eSRobert Watson 			mac_set_socket_peer_from_mbuf(m, so);
1292c488362eSRobert Watson #endif
1293845799c1SAndras Olah 			/* Do window scaling on this connection? */
1294845799c1SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1295845799c1SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1296845799c1SAndras Olah 				tp->snd_scale = tp->requested_s_scale;
1297845799c1SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
1298845799c1SAndras Olah 			}
1299a0292f23SGarrett Wollman 			/* Segment is acceptable, update cache if undefined. */
130097d8d152SAndre Oppermann 			if (tao.tao_ccsent == 0 && tcp_do_rfc1644)
130197d8d152SAndre Oppermann 				tcp_hc_updatetao(&inp->inp_inc, TCP_HC_TAO_CCSENT, to.to_ccecho, 0);
1302a0292f23SGarrett Wollman 
1303a0292f23SGarrett Wollman 			tp->rcv_adv += tp->rcv_wnd;
1304a0292f23SGarrett Wollman 			tp->snd_una++;		/* SYN is acked */
1305a0292f23SGarrett Wollman 			/*
1306a0292f23SGarrett Wollman 			 * If there's data, delay ACK; if there's also a FIN
1307a0292f23SGarrett Wollman 			 * ACKNOW will be turned on later.
1308a0292f23SGarrett Wollman 			 */
1309d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp) && tlen != 0)
13109b8b58e0SJonathan Lemon                                 callout_reset(tp->tt_delack, tcp_delacktime,
13119b8b58e0SJonathan Lemon                                     tcp_timer_delack, tp);
1312a0292f23SGarrett Wollman 			else
1313a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
1314a0292f23SGarrett Wollman 			/*
1315a0292f23SGarrett Wollman 			 * Received <SYN,ACK> in SYN_SENT[*] state.
1316a0292f23SGarrett Wollman 			 * Transitions:
1317a0292f23SGarrett Wollman 			 *	SYN_SENT  --> ESTABLISHED
1318a0292f23SGarrett Wollman 			 *	SYN_SENT* --> FIN_WAIT_1
1319a0292f23SGarrett Wollman 			 */
13209b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
1321a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDFIN) {
1322a0292f23SGarrett Wollman 				tp->t_state = TCPS_FIN_WAIT_1;
1323a0292f23SGarrett Wollman 				tp->t_flags &= ~TF_NEEDFIN;
1324fb59c426SYoshinobu Inoue 				thflags &= ~TH_SYN;
13257ff19458SPaul Traina 			} else {
1326a0292f23SGarrett Wollman 				tp->t_state = TCPS_ESTABLISHED;
13279b8b58e0SJonathan Lemon 				callout_reset(tp->tt_keep, tcp_keepidle,
13289b8b58e0SJonathan Lemon 					      tcp_timer_keep, tp);
13297ff19458SPaul Traina 			}
1330a0292f23SGarrett Wollman 		} else {
1331a0292f23SGarrett Wollman 			/*
1332c068736aSJeffrey Hsu 		 	 * Received initial SYN in SYN-SENT[*] state =>
1333c068736aSJeffrey Hsu 		 	 * simultaneous open.  If segment contains CC option
1334c068736aSJeffrey Hsu 		 	 * and there is a cached CC, apply TAO test.
1335c068736aSJeffrey Hsu 		 	 * If it succeeds, connection is * half-synchronized.
1336c068736aSJeffrey Hsu 		 	 * Otherwise, do 3-way handshake:
1337a0292f23SGarrett Wollman 		 	 *        SYN-SENT -> SYN-RECEIVED
1338a0292f23SGarrett Wollman 		 	 *        SYN-SENT* -> SYN-RECEIVED*
1339a0292f23SGarrett Wollman 		 	 * If there was no CC option, clear cached CC value.
1340a0292f23SGarrett Wollman 		 	 */
1341a0292f23SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
13429b8b58e0SJonathan Lemon 			callout_stop(tp->tt_rexmt);
1343be2ac88cSJonathan Lemon 			if (to.to_flags & TOF_CC) {
134497d8d152SAndre Oppermann 				if (tao.tao_cc != 0 &&
134597d8d152SAndre Oppermann 				    CC_GT(to.to_cc, tao.tao_cc)) {
1346a0292f23SGarrett Wollman 					/*
1347a0292f23SGarrett Wollman 					 * update cache and make transition:
1348a0292f23SGarrett Wollman 					 *        SYN-SENT -> ESTABLISHED*
1349a0292f23SGarrett Wollman 					 *        SYN-SENT* -> FIN-WAIT-1*
1350a0292f23SGarrett Wollman 					 */
135197d8d152SAndre Oppermann 					tao.tao_cc = to.to_cc;
135297d8d152SAndre Oppermann 					tcp_hc_updatetao(&inp->inp_inc,
135397d8d152SAndre Oppermann 						TCP_HC_TAO_CC, to.to_cc, 0);
13549b8b58e0SJonathan Lemon 					tp->t_starttime = ticks;
1355a0292f23SGarrett Wollman 					if (tp->t_flags & TF_NEEDFIN) {
1356a0292f23SGarrett Wollman 						tp->t_state = TCPS_FIN_WAIT_1;
1357a0292f23SGarrett Wollman 						tp->t_flags &= ~TF_NEEDFIN;
13587ff19458SPaul Traina 					} else {
1359a0292f23SGarrett Wollman 						tp->t_state = TCPS_ESTABLISHED;
13609b8b58e0SJonathan Lemon 						callout_reset(tp->tt_keep,
13619b8b58e0SJonathan Lemon 							      tcp_keepidle,
13629b8b58e0SJonathan Lemon 							      tcp_timer_keep,
13639b8b58e0SJonathan Lemon 							      tp);
13647ff19458SPaul Traina 					}
1365a0292f23SGarrett Wollman 					tp->t_flags |= TF_NEEDSYN;
1366df8bae1dSRodney W. Grimes 				} else
1367df8bae1dSRodney W. Grimes 					tp->t_state = TCPS_SYN_RECEIVED;
1368a0292f23SGarrett Wollman 			} else {
136997d8d152SAndre Oppermann 				if (tcp_do_rfc1644) {
1370a0292f23SGarrett Wollman 					/* CC.NEW or no option => invalidate cache */
137197d8d152SAndre Oppermann 					tao.tao_cc = 0;
137297d8d152SAndre Oppermann 					tcp_hc_updatetao(&inp->inp_inc,
137397d8d152SAndre Oppermann 						TCP_HC_TAO_CC, to.to_cc, 0);
137497d8d152SAndre Oppermann 				}
1375a0292f23SGarrett Wollman 				tp->t_state = TCPS_SYN_RECEIVED;
1376a0292f23SGarrett Wollman 			}
1377a0292f23SGarrett Wollman 		}
1378df8bae1dSRodney W. Grimes 
1379df8bae1dSRodney W. Grimes trimthenstep6:
1380df8bae1dSRodney W. Grimes 		/*
1381fb59c426SYoshinobu Inoue 		 * Advance th->th_seq to correspond to first data byte.
1382df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
1383df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
1384df8bae1dSRodney W. Grimes 		 */
1385fb59c426SYoshinobu Inoue 		th->th_seq++;
1386fb59c426SYoshinobu Inoue 		if (tlen > tp->rcv_wnd) {
1387fb59c426SYoshinobu Inoue 			todrop = tlen - tp->rcv_wnd;
1388df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
1389fb59c426SYoshinobu Inoue 			tlen = tp->rcv_wnd;
1390fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
1391df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpackafterwin++;
1392df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyteafterwin += todrop;
1393df8bae1dSRodney W. Grimes 		}
1394fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
1395fb59c426SYoshinobu Inoue 		tp->rcv_up = th->th_seq;
1396a0292f23SGarrett Wollman 		/*
1397a0292f23SGarrett Wollman 		 * Client side of transaction: already sent SYN and data.
1398a0292f23SGarrett Wollman 		 * If the remote host used T/TCP to validate the SYN,
1399a0292f23SGarrett Wollman 		 * our data will be ACK'd; if so, enter normal data segment
1400a0292f23SGarrett Wollman 		 * processing in the middle of step 5, ack processing.
1401a0292f23SGarrett Wollman 		 * Otherwise, goto step 6.
1402a0292f23SGarrett Wollman 		 */
1403fb59c426SYoshinobu Inoue  		if (thflags & TH_ACK)
1404a0292f23SGarrett Wollman 			goto process_ACK;
1405c068736aSJeffrey Hsu 
1406df8bae1dSRodney W. Grimes 		goto step6;
1407c068736aSJeffrey Hsu 
1408a0292f23SGarrett Wollman 	/*
1409a0292f23SGarrett Wollman 	 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
1410a0292f23SGarrett Wollman 	 *	if segment contains a SYN and CC [not CC.NEW] option:
1411a0292f23SGarrett Wollman 	 *              if state == TIME_WAIT and connection duration > MSL,
1412a0292f23SGarrett Wollman 	 *                  drop packet and send RST;
1413a0292f23SGarrett Wollman 	 *
1414a0292f23SGarrett Wollman 	 *		if SEG.CC > CCrecv then is new SYN, and can implicitly
1415a0292f23SGarrett Wollman 	 *		    ack the FIN (and data) in retransmission queue.
1416a0292f23SGarrett Wollman 	 *                  Complete close and delete TCPCB.  Then reprocess
1417a0292f23SGarrett Wollman 	 *                  segment, hoping to find new TCPCB in LISTEN state;
1418a0292f23SGarrett Wollman 	 *
1419a0292f23SGarrett Wollman 	 *		else must be old SYN; drop it.
1420a0292f23SGarrett Wollman 	 *      else do normal processing.
1421a0292f23SGarrett Wollman 	 */
1422a0292f23SGarrett Wollman 	case TCPS_LAST_ACK:
1423a0292f23SGarrett Wollman 	case TCPS_CLOSING:
1424a0292f23SGarrett Wollman 	case TCPS_TIME_WAIT:
1425340c35deSJonathan Lemon 		KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
1426fb59c426SYoshinobu Inoue 		if ((thflags & TH_SYN) &&
1427be2ac88cSJonathan Lemon 		    (to.to_flags & TOF_CC) && tp->cc_recv != 0) {
1428a0292f23SGarrett Wollman 			if (tp->t_state == TCPS_TIME_WAIT &&
1429a57815efSBosko Milekic 					(ticks - tp->t_starttime) > tcp_msl) {
1430a57815efSBosko Milekic 				rstreason = BANDLIM_UNLIMITED;
1431a0292f23SGarrett Wollman 				goto dropwithreset;
1432a57815efSBosko Milekic 			}
1433a0292f23SGarrett Wollman 			if (CC_GT(to.to_cc, tp->cc_recv)) {
1434a0292f23SGarrett Wollman 				tp = tcp_close(tp);
1435a0292f23SGarrett Wollman 				goto findpcb;
1436a0292f23SGarrett Wollman 			}
1437a0292f23SGarrett Wollman 			else
1438a0292f23SGarrett Wollman 				goto drop;
1439a0292f23SGarrett Wollman 		}
1440a0292f23SGarrett Wollman  		break;  /* continue normal processing */
1441df8bae1dSRodney W. Grimes 	}
1442df8bae1dSRodney W. Grimes 
1443df8bae1dSRodney W. Grimes 	/*
1444df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
144580ab7c0eSGarrett Wollman 	 * First check the RST flag and sequence number since reset segments
144680ab7c0eSGarrett Wollman 	 * are exempt from the timestamp and connection count tests.  This
144780ab7c0eSGarrett Wollman 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
144880ab7c0eSGarrett Wollman 	 * below which allowed reset segments in half the sequence space
144980ab7c0eSGarrett Wollman 	 * to fall though and be processed (which gives forged reset
145080ab7c0eSGarrett Wollman 	 * segments with a random sequence number a 50 percent chance of
145180ab7c0eSGarrett Wollman 	 * killing a connection).
145280ab7c0eSGarrett Wollman 	 * Then check timestamp, if present.
1453a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
1454df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
1455df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
1456df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
1457df8bae1dSRodney W. Grimes 	 *
145880ab7c0eSGarrett Wollman 	 *
145980ab7c0eSGarrett Wollman 	 * If the RST bit is set, check the sequence number to see
146080ab7c0eSGarrett Wollman 	 * if this is a valid reset segment.
146180ab7c0eSGarrett Wollman 	 * RFC 793 page 37:
146280ab7c0eSGarrett Wollman 	 *   In all states except SYN-SENT, all reset (RST) segments
146380ab7c0eSGarrett Wollman 	 *   are validated by checking their SEQ-fields.  A reset is
146480ab7c0eSGarrett Wollman 	 *   valid if its sequence number is in the window.
146580ab7c0eSGarrett Wollman 	 * Note: this does not take into account delayed ACKs, so
146680ab7c0eSGarrett Wollman 	 *   we should test against last_ack_sent instead of rcv_nxt.
14671a244a61SJonathan Lemon 	 *   The sequence number in the reset segment is normally an
14681a244a61SJonathan Lemon 	 *   echo of our outgoing acknowlegement numbers, but some hosts
14691a244a61SJonathan Lemon 	 *   send a reset with the sequence number at the rightmost edge
14701a244a61SJonathan Lemon 	 *   of our receive window, and we have to handle this case.
147180ab7c0eSGarrett Wollman 	 * If we have multiple segments in flight, the intial reset
147280ab7c0eSGarrett Wollman 	 * segment sequence numbers will be to the left of last_ack_sent,
147380ab7c0eSGarrett Wollman 	 * but they will eventually catch up.
147480ab7c0eSGarrett Wollman 	 * In any case, it never made sense to trim reset segments to
147580ab7c0eSGarrett Wollman 	 * fit the receive window since RFC 1122 says:
147680ab7c0eSGarrett Wollman 	 *   4.2.2.12  RST Segment: RFC-793 Section 3.4
147780ab7c0eSGarrett Wollman 	 *
147880ab7c0eSGarrett Wollman 	 *    A TCP SHOULD allow a received RST segment to include data.
147980ab7c0eSGarrett Wollman 	 *
148080ab7c0eSGarrett Wollman 	 *    DISCUSSION
148180ab7c0eSGarrett Wollman 	 *         It has been suggested that a RST segment could contain
148280ab7c0eSGarrett Wollman 	 *         ASCII text that encoded and explained the cause of the
148380ab7c0eSGarrett Wollman 	 *         RST.  No standard has yet been established for such
148480ab7c0eSGarrett Wollman 	 *         data.
148580ab7c0eSGarrett Wollman 	 *
148680ab7c0eSGarrett Wollman 	 * If the reset segment passes the sequence number test examine
148780ab7c0eSGarrett Wollman 	 * the state:
148880ab7c0eSGarrett Wollman 	 *    SYN_RECEIVED STATE:
148980ab7c0eSGarrett Wollman 	 *	If passive open, return to LISTEN state.
149080ab7c0eSGarrett Wollman 	 *	If active open, inform user that connection was refused.
1491745bab7fSDima Dorfman 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES:
149280ab7c0eSGarrett Wollman 	 *	Inform user that connection was reset, and close tcb.
1493e9bd3a37SJonathan M. Bresler 	 *    CLOSING, LAST_ACK STATES:
149480ab7c0eSGarrett Wollman 	 *	Close the tcb.
1495e9bd3a37SJonathan M. Bresler 	 *    TIME_WAIT STATE:
149680ab7c0eSGarrett Wollman 	 *	Drop the segment - see Stevens, vol. 2, p. 964 and
149780ab7c0eSGarrett Wollman 	 *      RFC 1337.
149880ab7c0eSGarrett Wollman 	 */
1499fb59c426SYoshinobu Inoue 	if (thflags & TH_RST) {
1500fb59c426SYoshinobu Inoue 		if (SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
1501fb59c426SYoshinobu Inoue 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
150280ab7c0eSGarrett Wollman 			switch (tp->t_state) {
150380ab7c0eSGarrett Wollman 
150480ab7c0eSGarrett Wollman 			case TCPS_SYN_RECEIVED:
150580ab7c0eSGarrett Wollman 				so->so_error = ECONNREFUSED;
150680ab7c0eSGarrett Wollman 				goto close;
150780ab7c0eSGarrett Wollman 
150880ab7c0eSGarrett Wollman 			case TCPS_ESTABLISHED:
150980ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_1:
151080ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_2:
151180ab7c0eSGarrett Wollman 			case TCPS_CLOSE_WAIT:
151280ab7c0eSGarrett Wollman 				so->so_error = ECONNRESET;
151380ab7c0eSGarrett Wollman 			close:
151480ab7c0eSGarrett Wollman 				tp->t_state = TCPS_CLOSED;
151580ab7c0eSGarrett Wollman 				tcpstat.tcps_drops++;
151680ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
151780ab7c0eSGarrett Wollman 				break;
151880ab7c0eSGarrett Wollman 
151980ab7c0eSGarrett Wollman 			case TCPS_CLOSING:
152080ab7c0eSGarrett Wollman 			case TCPS_LAST_ACK:
152180ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
152280ab7c0eSGarrett Wollman 				break;
152380ab7c0eSGarrett Wollman 
152480ab7c0eSGarrett Wollman 			case TCPS_TIME_WAIT:
1525340c35deSJonathan Lemon 				KASSERT(tp->t_state != TCPS_TIME_WAIT,
1526340c35deSJonathan Lemon 				    ("timewait"));
152780ab7c0eSGarrett Wollman 				break;
152880ab7c0eSGarrett Wollman 			}
152980ab7c0eSGarrett Wollman 		}
153080ab7c0eSGarrett Wollman 		goto drop;
153180ab7c0eSGarrett Wollman 	}
153280ab7c0eSGarrett Wollman 
153380ab7c0eSGarrett Wollman 	/*
1534df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
1535df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
1536df8bae1dSRodney W. Grimes 	 */
1537be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
153880ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
1539df8bae1dSRodney W. Grimes 
1540df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
15419b8b58e0SJonathan Lemon 		if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) {
1542df8bae1dSRodney W. Grimes 			/*
1543df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
1544df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
1545df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
1546df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
1547df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
1548df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
1549df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
1550df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
1551df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
1552df8bae1dSRodney W. Grimes 			 */
1553df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
1554df8bae1dSRodney W. Grimes 		} else {
1555df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvduppack++;
1556fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvdupbyte += tlen;
1557df8bae1dSRodney W. Grimes 			tcpstat.tcps_pawsdrop++;
155807fd333dSMatthew Dillon 			if (tlen)
1559df8bae1dSRodney W. Grimes 				goto dropafterack;
15601ab4789dSMatthew Dillon 			goto drop;
15611ab4789dSMatthew Dillon 		}
1562df8bae1dSRodney W. Grimes 	}
1563df8bae1dSRodney W. Grimes 
1564a0292f23SGarrett Wollman 	/*
1565a0292f23SGarrett Wollman 	 * T/TCP mechanism
1566a0292f23SGarrett Wollman 	 *   If T/TCP was negotiated and the segment doesn't have CC,
1567dc733423SDag-Erling Smørgrav 	 *   or if its CC is wrong then drop the segment.
1568a0292f23SGarrett Wollman 	 *   RST segments do not have to comply with this.
1569a0292f23SGarrett Wollman 	 */
1570a0292f23SGarrett Wollman 	if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) &&
1571be2ac88cSJonathan Lemon 	    ((to.to_flags & TOF_CC) == 0 || tp->cc_recv != to.to_cc))
1572a0292f23SGarrett Wollman  		goto dropafterack;
1573a0292f23SGarrett Wollman 
157480ab7c0eSGarrett Wollman 	/*
157580ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
157680ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
157780ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
157880ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
157980ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
158080ab7c0eSGarrett Wollman 	 */
1581a57815efSBosko Milekic 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
1582a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
1583a57815efSBosko Milekic 		goto dropwithreset;
1584a57815efSBosko Milekic 	}
158580ab7c0eSGarrett Wollman 
1586fb59c426SYoshinobu Inoue 	todrop = tp->rcv_nxt - th->th_seq;
1587df8bae1dSRodney W. Grimes 	if (todrop > 0) {
1588fb59c426SYoshinobu Inoue 		if (thflags & TH_SYN) {
1589fb59c426SYoshinobu Inoue 			thflags &= ~TH_SYN;
1590fb59c426SYoshinobu Inoue 			th->th_seq++;
1591fb59c426SYoshinobu Inoue 			if (th->th_urp > 1)
1592fb59c426SYoshinobu Inoue 				th->th_urp--;
1593df8bae1dSRodney W. Grimes 			else
1594fb59c426SYoshinobu Inoue 				thflags &= ~TH_URG;
1595df8bae1dSRodney W. Grimes 			todrop--;
1596df8bae1dSRodney W. Grimes 		}
1597df8bae1dSRodney W. Grimes 		/*
1598dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
1599df8bae1dSRodney W. Grimes 		 */
1600fb59c426SYoshinobu Inoue 		if (todrop > tlen
1601fb59c426SYoshinobu Inoue 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
1602dac20301SGarrett Wollman 			/*
1603dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
1604dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
1605dac20301SGarrett Wollman 			 * of sequence; drop it.
1606dac20301SGarrett Wollman 			 */
1607fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
1608dac20301SGarrett Wollman 
1609df8bae1dSRodney W. Grimes 			/*
1610dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
1611dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
1612df8bae1dSRodney W. Grimes 			 */
1613dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
1614fb59c426SYoshinobu Inoue 			todrop = tlen;
1615dac20301SGarrett Wollman 			tcpstat.tcps_rcvduppack++;
1616dac20301SGarrett Wollman 			tcpstat.tcps_rcvdupbyte += todrop;
1617df8bae1dSRodney W. Grimes 		} else {
1618df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpartduppack++;
1619df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpartdupbyte += todrop;
1620df8bae1dSRodney W. Grimes 		}
1621fb59c426SYoshinobu Inoue 		drop_hdrlen += todrop;	/* drop from the top afterwards */
1622fb59c426SYoshinobu Inoue 		th->th_seq += todrop;
1623fb59c426SYoshinobu Inoue 		tlen -= todrop;
1624fb59c426SYoshinobu Inoue 		if (th->th_urp > todrop)
1625fb59c426SYoshinobu Inoue 			th->th_urp -= todrop;
1626df8bae1dSRodney W. Grimes 		else {
1627fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;
1628fb59c426SYoshinobu Inoue 			th->th_urp = 0;
1629df8bae1dSRodney W. Grimes 		}
1630df8bae1dSRodney W. Grimes 	}
1631df8bae1dSRodney W. Grimes 
1632df8bae1dSRodney W. Grimes 	/*
1633df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
1634df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
1635df8bae1dSRodney W. Grimes 	 */
1636df8bae1dSRodney W. Grimes 	if ((so->so_state & SS_NOFDREF) &&
1637fb59c426SYoshinobu Inoue 	    tp->t_state > TCPS_CLOSE_WAIT && tlen) {
1638df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1639df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvafterclose++;
1640a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
1641df8bae1dSRodney W. Grimes 		goto dropwithreset;
1642df8bae1dSRodney W. Grimes 	}
1643df8bae1dSRodney W. Grimes 
1644df8bae1dSRodney W. Grimes 	/*
1645df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
1646df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
1647df8bae1dSRodney W. Grimes 	 */
1648fb59c426SYoshinobu Inoue 	todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd);
1649df8bae1dSRodney W. Grimes 	if (todrop > 0) {
1650df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvpackafterwin++;
1651fb59c426SYoshinobu Inoue 		if (todrop >= tlen) {
1652fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbyteafterwin += tlen;
1653df8bae1dSRodney W. Grimes 			/*
1654df8bae1dSRodney W. Grimes 			 * If a new connection request is received
1655df8bae1dSRodney W. Grimes 			 * while in TIME_WAIT, drop the old connection
1656df8bae1dSRodney W. Grimes 			 * and start over if the sequence numbers
1657df8bae1dSRodney W. Grimes 			 * are above the previous ones.
1658df8bae1dSRodney W. Grimes 			 */
1659340c35deSJonathan Lemon 			KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
1660fb59c426SYoshinobu Inoue 			if (thflags & TH_SYN &&
1661df8bae1dSRodney W. Grimes 			    tp->t_state == TCPS_TIME_WAIT &&
1662fb59c426SYoshinobu Inoue 			    SEQ_GT(th->th_seq, tp->rcv_nxt)) {
1663df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
1664df8bae1dSRodney W. Grimes 				goto findpcb;
1665df8bae1dSRodney W. Grimes 			}
1666df8bae1dSRodney W. Grimes 			/*
1667df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
1668df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
1669df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
1670df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
1671df8bae1dSRodney W. Grimes 			 * and ack.
1672df8bae1dSRodney W. Grimes 			 */
1673fb59c426SYoshinobu Inoue 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
1674df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
1675df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvwinprobe++;
1676df8bae1dSRodney W. Grimes 			} else
1677df8bae1dSRodney W. Grimes 				goto dropafterack;
1678df8bae1dSRodney W. Grimes 		} else
1679df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyteafterwin += todrop;
1680df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
1681fb59c426SYoshinobu Inoue 		tlen -= todrop;
1682fb59c426SYoshinobu Inoue 		thflags &= ~(TH_PUSH|TH_FIN);
1683df8bae1dSRodney W. Grimes 	}
1684df8bae1dSRodney W. Grimes 
1685df8bae1dSRodney W. Grimes 	/*
1686df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
1687df8bae1dSRodney W. Grimes 	 * record its timestamp.
1688a0292f23SGarrett Wollman 	 * NOTE that the test is modified according to the latest
1689a0292f23SGarrett Wollman 	 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1690df8bae1dSRodney W. Grimes 	 */
1691be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 &&
1692fb59c426SYoshinobu Inoue 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
16939b8b58e0SJonathan Lemon 		tp->ts_recent_age = ticks;
1694a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
1695df8bae1dSRodney W. Grimes 	}
1696df8bae1dSRodney W. Grimes 
1697df8bae1dSRodney W. Grimes 	/*
1698df8bae1dSRodney W. Grimes 	 * If a SYN is in the window, then this is an
1699df8bae1dSRodney W. Grimes 	 * error and we send an RST and drop the connection.
1700df8bae1dSRodney W. Grimes 	 */
1701fb59c426SYoshinobu Inoue 	if (thflags & TH_SYN) {
1702df8bae1dSRodney W. Grimes 		tp = tcp_drop(tp, ECONNRESET);
1703a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
1704122aad88SAndre Oppermann 		goto drop;
1705df8bae1dSRodney W. Grimes 	}
1706df8bae1dSRodney W. Grimes 
1707a0292f23SGarrett Wollman 	/*
1708a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
1709a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
1710a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
1711a0292f23SGarrett Wollman 	 */
1712fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) == 0) {
1713a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
1714a0292f23SGarrett Wollman 		    (tp->t_flags & TF_NEEDSYN))
1715a0292f23SGarrett Wollman 			goto step6;
1716a0292f23SGarrett Wollman 		else
1717a0292f23SGarrett Wollman 			goto drop;
1718a0292f23SGarrett Wollman 	}
1719df8bae1dSRodney W. Grimes 
1720df8bae1dSRodney W. Grimes 	/*
1721df8bae1dSRodney W. Grimes 	 * Ack processing.
1722df8bae1dSRodney W. Grimes 	 */
1723df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1724df8bae1dSRodney W. Grimes 
1725df8bae1dSRodney W. Grimes 	/*
1726764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
1727764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
1728764d8cefSBill Fenner 	 * The ACK was checked above.
1729df8bae1dSRodney W. Grimes 	 */
1730df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1731a0292f23SGarrett Wollman 
1732df8bae1dSRodney W. Grimes 		tcpstat.tcps_connects++;
1733df8bae1dSRodney W. Grimes 		soisconnected(so);
1734df8bae1dSRodney W. Grimes 		/* Do window scaling? */
1735df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1736df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1737df8bae1dSRodney W. Grimes 			tp->snd_scale = tp->requested_s_scale;
1738df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
1739df8bae1dSRodney W. Grimes 		}
1740a0292f23SGarrett Wollman 		/*
1741a0292f23SGarrett Wollman 		 * Upon successful completion of 3-way handshake,
174297d8d152SAndre Oppermann 		 * update cache.CC, pass any queued data to the user,
174397d8d152SAndre Oppermann 		 * and advance state appropriately.
1744a0292f23SGarrett Wollman 		 */
174597d8d152SAndre Oppermann 		if (tcp_do_rfc1644) {
174697d8d152SAndre Oppermann 			tao.tao_cc = tp->cc_recv;
174797d8d152SAndre Oppermann 			tcp_hc_updatetao(&inp->inp_inc, TCP_HC_TAO_CC,
174897d8d152SAndre Oppermann 					 tp->cc_recv, 0);
174997d8d152SAndre Oppermann 		}
1750a0292f23SGarrett Wollman 		/*
1751a0292f23SGarrett Wollman 		 * Make transitions:
1752a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
1753a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
1754a0292f23SGarrett Wollman 		 */
17559b8b58e0SJonathan Lemon 		tp->t_starttime = ticks;
1756a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDFIN) {
1757a0292f23SGarrett Wollman 			tp->t_state = TCPS_FIN_WAIT_1;
1758a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDFIN;
17597ff19458SPaul Traina 		} else {
1760a0292f23SGarrett Wollman 			tp->t_state = TCPS_ESTABLISHED;
17619b8b58e0SJonathan Lemon 			callout_reset(tp->tt_keep, tcp_keepidle,
17629b8b58e0SJonathan Lemon 				      tcp_timer_keep, tp);
17637ff19458SPaul Traina 		}
1764a0292f23SGarrett Wollman 		/*
1765a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
1766a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
1767a0292f23SGarrett Wollman 		 */
1768fb59c426SYoshinobu Inoue 		if (tlen == 0 && (thflags & TH_FIN) == 0)
1769fb59c426SYoshinobu Inoue 			(void) tcp_reass(tp, (struct tcphdr *)0, 0,
1770a0292f23SGarrett Wollman 			    (struct mbuf *)0);
1771fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
177293b0017fSPhilippe Charnier 		/* FALLTHROUGH */
1773df8bae1dSRodney W. Grimes 
1774df8bae1dSRodney W. Grimes 	/*
1775df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1776df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
1777fb59c426SYoshinobu Inoue 	 *	tp->snd_una < th->th_ack <= tp->snd_max
1778fb59c426SYoshinobu Inoue 	 * then advance tp->snd_una to th->th_ack and drop
1779df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
1780df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
1781df8bae1dSRodney W. Grimes 	 */
1782df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1783df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
1784df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
1785df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1786df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
1787df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
1788df8bae1dSRodney W. Grimes 	case TCPS_TIME_WAIT:
1789340c35deSJonathan Lemon 		KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
1790fb59c426SYoshinobu Inoue 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
1791fb59c426SYoshinobu Inoue 			if (tlen == 0 && tiwin == tp->snd_wnd) {
1792df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvdupack++;
1793df8bae1dSRodney W. Grimes 				/*
1794df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
1795df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
1796df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
1797df8bae1dSRodney W. Grimes 				 * change), the ack is the biggest we've
1798df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
1799df8bae1dSRodney W. Grimes 				 * threshhold of them, assume a packet
1800df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
1801df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
1802df8bae1dSRodney W. Grimes 				 * window so we send only this one
1803df8bae1dSRodney W. Grimes 				 * packet.
1804df8bae1dSRodney W. Grimes 				 *
1805df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
1806df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
1807df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
1808df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
1809df8bae1dSRodney W. Grimes 				 * the new ssthresh).
1810df8bae1dSRodney W. Grimes 				 *
1811df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
1812df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
1813df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
1814df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
1815df8bae1dSRodney W. Grimes 				 * network.
1816df8bae1dSRodney W. Grimes 				 */
18179b8b58e0SJonathan Lemon 				if (!callout_active(tp->tt_rexmt) ||
1818fb59c426SYoshinobu Inoue 				    th->th_ack != tp->snd_una)
1819df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
1820cb942153SJeffrey Hsu 				else if (++tp->t_dupacks > tcprexmtthresh ||
1821cb942153SJeffrey Hsu 					 (tcp_do_newreno &&
18229d11646dSJeffrey Hsu 					  IN_FASTRECOVERY(tp))) {
182346f58482SJonathan Lemon 					tp->snd_cwnd += tp->t_maxseg;
182446f58482SJonathan Lemon 					(void) tcp_output(tp);
182546f58482SJonathan Lemon 					goto drop;
1826cb942153SJeffrey Hsu 				} else if (tp->t_dupacks == tcprexmtthresh) {
1827cb942153SJeffrey Hsu 					tcp_seq onxt = tp->snd_nxt;
1828cb942153SJeffrey Hsu 					u_int win;
1829cb942153SJeffrey Hsu 					if (tcp_do_newreno &&
18309d11646dSJeffrey Hsu 					    SEQ_LEQ(th->th_ack,
18319d11646dSJeffrey Hsu 					            tp->snd_recover)) {
1832cb942153SJeffrey Hsu 						tp->t_dupacks = 0;
1833cb942153SJeffrey Hsu 						break;
183446f58482SJonathan Lemon 					}
1835cb942153SJeffrey Hsu 					win = min(tp->snd_wnd, tp->snd_cwnd) /
1836cb942153SJeffrey Hsu 					    2 / tp->t_maxseg;
1837df8bae1dSRodney W. Grimes 					if (win < 2)
1838df8bae1dSRodney W. Grimes 						win = 2;
1839df8bae1dSRodney W. Grimes 					tp->snd_ssthresh = win * tp->t_maxseg;
18409d11646dSJeffrey Hsu 					ENTER_FASTRECOVERY(tp);
184146f58482SJonathan Lemon 					tp->snd_recover = tp->snd_max;
18429b8b58e0SJonathan Lemon 					callout_stop(tp->tt_rexmt);
18439b8b58e0SJonathan Lemon 					tp->t_rtttime = 0;
1844fb59c426SYoshinobu Inoue 					tp->snd_nxt = th->th_ack;
1845df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->t_maxseg;
1846df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
184748d2549cSJeffrey Hsu 					KASSERT(tp->snd_limited <= 2,
184848d2549cSJeffrey Hsu 					    ("tp->snd_limited too big"));
1849df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
185048d2549cSJeffrey Hsu 					     tp->t_maxseg *
185148d2549cSJeffrey Hsu 					     (tp->t_dupacks - tp->snd_limited);
1852df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
1853df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
1854df8bae1dSRodney W. Grimes 					goto drop;
1855582a954bSJeffrey Hsu 				} else if (tcp_do_rfc3042) {
1856582a954bSJeffrey Hsu 					u_long oldcwnd = tp->snd_cwnd;
185748d2549cSJeffrey Hsu 					tcp_seq oldsndmax = tp->snd_max;
185848d2549cSJeffrey Hsu 					u_int sent;
1859582a954bSJeffrey Hsu 					KASSERT(tp->t_dupacks == 1 ||
1860582a954bSJeffrey Hsu 					    tp->t_dupacks == 2,
1861582a954bSJeffrey Hsu 					    ("dupacks not 1 or 2"));
186261a36e3dSJeffrey Hsu 					if (tp->t_dupacks == 1)
186348d2549cSJeffrey Hsu 						tp->snd_limited = 0;
186461a36e3dSJeffrey Hsu 					tp->snd_cwnd =
186561a36e3dSJeffrey Hsu 					    (tp->snd_nxt - tp->snd_una) +
186661a36e3dSJeffrey Hsu 					    (tp->t_dupacks - tp->snd_limited) *
186761a36e3dSJeffrey Hsu 					    tp->t_maxseg;
1868582a954bSJeffrey Hsu 					(void) tcp_output(tp);
186948d2549cSJeffrey Hsu 					sent = tp->snd_max - oldsndmax;
187048d2549cSJeffrey Hsu 					if (sent > tp->t_maxseg) {
187148d2549cSJeffrey Hsu 						KASSERT(tp->snd_limited == 0 &&
187248d2549cSJeffrey Hsu 						    tp->t_dupacks == 2,
187348d2549cSJeffrey Hsu 						    ("sent too much"));
187448d2549cSJeffrey Hsu 						tp->snd_limited = 2;
187548d2549cSJeffrey Hsu 					} else if (sent > 0)
187648d2549cSJeffrey Hsu 						++tp->snd_limited;
1877582a954bSJeffrey Hsu 					tp->snd_cwnd = oldcwnd;
1878582a954bSJeffrey Hsu 					goto drop;
1879df8bae1dSRodney W. Grimes 				}
1880df8bae1dSRodney W. Grimes 			} else
1881df8bae1dSRodney W. Grimes 				tp->t_dupacks = 0;
1882df8bae1dSRodney W. Grimes 			break;
1883df8bae1dSRodney W. Grimes 		}
1884cb942153SJeffrey Hsu 
1885cb942153SJeffrey Hsu 		KASSERT(SEQ_GT(th->th_ack, tp->snd_una), ("th_ack <= snd_una"));
1886cb942153SJeffrey Hsu 
1887df8bae1dSRodney W. Grimes 		/*
1888df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
1889df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
1890df8bae1dSRodney W. Grimes 		 */
1891c068736aSJeffrey Hsu 		if (tcp_do_newreno) {
18929d11646dSJeffrey Hsu 			if (IN_FASTRECOVERY(tp)) {
1893cb942153SJeffrey Hsu 				if (SEQ_LT(th->th_ack, tp->snd_recover)) {
1894c068736aSJeffrey Hsu 					tcp_newreno_partial_ack(tp, th);
1895c068736aSJeffrey Hsu 				} else {
1896c068736aSJeffrey Hsu 					/*
1897c068736aSJeffrey Hsu 					 * Window inflation should have left us
1898c068736aSJeffrey Hsu 					 * with approximately snd_ssthresh
1899c068736aSJeffrey Hsu 					 * outstanding data.
1900c068736aSJeffrey Hsu 					 * But in case we would be inclined to
1901c068736aSJeffrey Hsu 					 * send a burst, better to do it via
1902c068736aSJeffrey Hsu 					 * the slow start mechanism.
1903c068736aSJeffrey Hsu 					 */
1904c068736aSJeffrey Hsu 					if (SEQ_GT(th->th_ack +
1905c068736aSJeffrey Hsu 							tp->snd_ssthresh,
1906c068736aSJeffrey Hsu 						   tp->snd_max))
1907c068736aSJeffrey Hsu 						tp->snd_cwnd = tp->snd_max -
1908c068736aSJeffrey Hsu 								th->th_ack +
1909c068736aSJeffrey Hsu 								tp->t_maxseg;
1910c068736aSJeffrey Hsu 					else
1911c068736aSJeffrey Hsu 						tp->snd_cwnd = tp->snd_ssthresh;
1912c068736aSJeffrey Hsu 				}
1913c068736aSJeffrey Hsu 			}
1914c068736aSJeffrey Hsu                 } else {
1915233e8c18SGarrett Wollman                         if (tp->t_dupacks >= tcprexmtthresh &&
1916df8bae1dSRodney W. Grimes                             tp->snd_cwnd > tp->snd_ssthresh)
1917df8bae1dSRodney W. Grimes 				tp->snd_cwnd = tp->snd_ssthresh;
191846f58482SJonathan Lemon                 }
1919cb942153SJeffrey Hsu 		tp->t_dupacks = 0;
1920fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
1921df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvacktoomuch++;
1922df8bae1dSRodney W. Grimes 			goto dropafterack;
1923df8bae1dSRodney W. Grimes 		}
1924a0292f23SGarrett Wollman 		/*
1925a0292f23SGarrett Wollman 		 * If we reach this point, ACK is not a duplicate,
1926a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
1927a0292f23SGarrett Wollman 		 */
1928a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
1929a0292f23SGarrett Wollman 			/*
1930a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
1931a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
193207e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
193307e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
193407e43e10SAndras Olah 			 * we can do window scaling.
1935a0292f23SGarrett Wollman 			 */
1936a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
1937a0292f23SGarrett Wollman 			tp->snd_una++;
193807e43e10SAndras Olah 			/* Do window scaling? */
193907e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
194007e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
194107e43e10SAndras Olah 				tp->snd_scale = tp->requested_s_scale;
194207e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
194307e43e10SAndras Olah 			}
1944a0292f23SGarrett Wollman 		}
1945a0292f23SGarrett Wollman 
1946a0292f23SGarrett Wollman process_ACK:
1947fb59c426SYoshinobu Inoue 		acked = th->th_ack - tp->snd_una;
1948df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvackpack++;
1949df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvackbyte += acked;
1950df8bae1dSRodney W. Grimes 
1951df8bae1dSRodney W. Grimes 		/*
19529b8b58e0SJonathan Lemon 		 * If we just performed our first retransmit, and the ACK
19539b8b58e0SJonathan Lemon 		 * arrives within our recovery window, then it was a mistake
19549b8b58e0SJonathan Lemon 		 * to do the retransmit in the first place.  Recover our
19559b8b58e0SJonathan Lemon 		 * original cwnd and ssthresh, and proceed to transmit where
19569b8b58e0SJonathan Lemon 		 * we left off.
19579b8b58e0SJonathan Lemon 		 */
19589b8b58e0SJonathan Lemon 		if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) {
1959d65bf08aSMatthew Dillon 			++tcpstat.tcps_sndrexmitbad;
19609b8b58e0SJonathan Lemon 			tp->snd_cwnd = tp->snd_cwnd_prev;
19619b8b58e0SJonathan Lemon 			tp->snd_ssthresh = tp->snd_ssthresh_prev;
19629d11646dSJeffrey Hsu 			tp->snd_recover = tp->snd_recover_prev;
19639d11646dSJeffrey Hsu 			if (tp->t_flags & TF_WASFRECOVERY)
19649d11646dSJeffrey Hsu 				ENTER_FASTRECOVERY(tp);
19659b8b58e0SJonathan Lemon 			tp->snd_nxt = tp->snd_max;
19669b8b58e0SJonathan Lemon 			tp->t_badrxtwin = 0;	/* XXX probably not required */
19679b8b58e0SJonathan Lemon 		}
19689b8b58e0SJonathan Lemon 
19699b8b58e0SJonathan Lemon 		/*
1970df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
1971df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
1972df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
1973df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
1974df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
1975df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
1976df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
1977fa55172bSMatthew Dillon 		 *
1978fa55172bSMatthew Dillon 		 * Some boxes send broken timestamp replies
1979fa55172bSMatthew Dillon 		 * during the SYN+ACK phase, ignore
1980fa55172bSMatthew Dillon 		 * timestamps of 0 or we could calculate a
1981fa55172bSMatthew Dillon 		 * huge RTT and blow up the retransmit timer.
1982df8bae1dSRodney W. Grimes 		 */
1983fa55172bSMatthew Dillon 		if ((to.to_flags & TOF_TS) != 0 &&
1984fa55172bSMatthew Dillon 		    to.to_tsecr) {
19859b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - to.to_tsecr + 1);
1986fa55172bSMatthew Dillon 		} else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
19879b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
1988fa55172bSMatthew Dillon 		}
19891fcc99b5SMatthew Dillon 		tcp_xmit_bandwidth_limit(tp, th->th_ack);
1990df8bae1dSRodney W. Grimes 
1991df8bae1dSRodney W. Grimes 		/*
1992df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
1993df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
1994df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
1995df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
1996df8bae1dSRodney W. Grimes 		 */
1997fb59c426SYoshinobu Inoue 		if (th->th_ack == tp->snd_max) {
19989b8b58e0SJonathan Lemon 			callout_stop(tp->tt_rexmt);
1999df8bae1dSRodney W. Grimes 			needoutput = 1;
20009b8b58e0SJonathan Lemon 		} else if (!callout_active(tp->tt_persist))
20019b8b58e0SJonathan Lemon 			callout_reset(tp->tt_rexmt, tp->t_rxtcur,
20029b8b58e0SJonathan Lemon 				      tcp_timer_rexmt, tp);
2003a0292f23SGarrett Wollman 
2004a0292f23SGarrett Wollman 		/*
2005a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
2006a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
2007a0292f23SGarrett Wollman 		 */
2008a0292f23SGarrett Wollman 		if (acked == 0)
2009a0292f23SGarrett Wollman 			goto step6;
2010a0292f23SGarrett Wollman 
2011df8bae1dSRodney W. Grimes 		/*
2012df8bae1dSRodney W. Grimes 		 * When new data is acked, open the congestion window.
2013df8bae1dSRodney W. Grimes 		 * If the window gives us less than ssthresh packets
2014df8bae1dSRodney W. Grimes 		 * in flight, open exponentially (maxseg per packet).
2015df8bae1dSRodney W. Grimes 		 * Otherwise open linearly: maxseg per window
201610be5648SGarrett Wollman 		 * (maxseg^2 / cwnd per packet).
2017df8bae1dSRodney W. Grimes 		 */
20189d11646dSJeffrey Hsu 		if (!tcp_do_newreno || !IN_FASTRECOVERY(tp)) {
2019df8bae1dSRodney W. Grimes 			register u_int cw = tp->snd_cwnd;
2020df8bae1dSRodney W. Grimes 			register u_int incr = tp->t_maxseg;
2021df8bae1dSRodney W. Grimes 			if (cw > tp->snd_ssthresh)
202210be5648SGarrett Wollman 				incr = incr * incr / cw;
2023df8bae1dSRodney W. Grimes 			tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale);
2024df8bae1dSRodney W. Grimes 		}
2025df8bae1dSRodney W. Grimes 		if (acked > so->so_snd.sb_cc) {
2026df8bae1dSRodney W. Grimes 			tp->snd_wnd -= so->so_snd.sb_cc;
2027df8bae1dSRodney W. Grimes 			sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
2028df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
2029df8bae1dSRodney W. Grimes 		} else {
2030df8bae1dSRodney W. Grimes 			sbdrop(&so->so_snd, acked);
2031df8bae1dSRodney W. Grimes 			tp->snd_wnd -= acked;
2032df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
2033df8bae1dSRodney W. Grimes 		}
2034df8bae1dSRodney W. Grimes 		sowwakeup(so);
2035cb942153SJeffrey Hsu 		/* detect una wraparound */
20369d11646dSJeffrey Hsu 		if (tcp_do_newreno && !IN_FASTRECOVERY(tp) &&
20379d11646dSJeffrey Hsu 		    SEQ_GT(tp->snd_una, tp->snd_recover) &&
20389d11646dSJeffrey Hsu 		    SEQ_LEQ(th->th_ack, tp->snd_recover))
20399d11646dSJeffrey Hsu 			tp->snd_recover = th->th_ack - 1;
20409d11646dSJeffrey Hsu 		if (tcp_do_newreno && IN_FASTRECOVERY(tp) &&
20419d11646dSJeffrey Hsu 		    SEQ_GEQ(th->th_ack, tp->snd_recover))
20429d11646dSJeffrey Hsu 			EXIT_FASTRECOVERY(tp);
2043fb59c426SYoshinobu Inoue 		tp->snd_una = th->th_ack;
2044df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2045df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
2046df8bae1dSRodney W. Grimes 
2047df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2048df8bae1dSRodney W. Grimes 
2049df8bae1dSRodney W. Grimes 		/*
2050df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
2051df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
2052df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
2053df8bae1dSRodney W. Grimes 		 */
2054df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2055df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2056df8bae1dSRodney W. Grimes 				/*
2057df8bae1dSRodney W. Grimes 				 * If we can't receive any more
2058df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
2059df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
2060df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
2061df8bae1dSRodney W. Grimes 				 * we'll hang forever.
2062df8bae1dSRodney W. Grimes 				 */
2063340c35deSJonathan Lemon 		/* XXXjl
2064340c35deSJonathan Lemon 		 * we should release the tp also, and use a
2065340c35deSJonathan Lemon 		 * compressed state.
2066340c35deSJonathan Lemon 		 */
2067df8bae1dSRodney W. Grimes 				if (so->so_state & SS_CANTRCVMORE) {
206803e49181SSeigo Tanimura 					soisdisconnected(so);
20699b8b58e0SJonathan Lemon 					callout_reset(tp->tt_2msl, tcp_maxidle,
20709b8b58e0SJonathan Lemon 						      tcp_timer_2msl, tp);
20714cc20ab1SSeigo Tanimura 				}
2072df8bae1dSRodney W. Grimes 				tp->t_state = TCPS_FIN_WAIT_2;
2073df8bae1dSRodney W. Grimes 			}
2074df8bae1dSRodney W. Grimes 			break;
2075df8bae1dSRodney W. Grimes 
2076df8bae1dSRodney W. Grimes 	 	/*
2077df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
2078df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
2079df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
2080df8bae1dSRodney W. Grimes 		 * the segment.
2081df8bae1dSRodney W. Grimes 		 */
2082df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
2083df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2084340c35deSJonathan Lemon 				KASSERT(headlocked, ("headlocked"));
208511a20fb8SJeffrey Hsu 				tcp_twstart(tp);
2086340c35deSJonathan Lemon 				INP_INFO_WUNLOCK(&tcbinfo);
2087340c35deSJonathan Lemon 				m_freem(m);
2088340c35deSJonathan Lemon 				return;
2089df8bae1dSRodney W. Grimes 			}
2090df8bae1dSRodney W. Grimes 			break;
2091df8bae1dSRodney W. Grimes 
2092df8bae1dSRodney W. Grimes 		/*
2093df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
2094df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
2095df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
2096df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
2097df8bae1dSRodney W. Grimes 		 */
2098df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
2099df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2100df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
2101df8bae1dSRodney W. Grimes 				goto drop;
2102df8bae1dSRodney W. Grimes 			}
2103df8bae1dSRodney W. Grimes 			break;
2104df8bae1dSRodney W. Grimes 
2105df8bae1dSRodney W. Grimes 		/*
2106df8bae1dSRodney W. Grimes 		 * In TIME_WAIT state the only thing that should arrive
2107df8bae1dSRodney W. Grimes 		 * is a retransmission of the remote FIN.  Acknowledge
2108df8bae1dSRodney W. Grimes 		 * it and restart the finack timer.
2109df8bae1dSRodney W. Grimes 		 */
2110df8bae1dSRodney W. Grimes 		case TCPS_TIME_WAIT:
2111340c35deSJonathan Lemon 			KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
21129b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, 2 * tcp_msl,
21139b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
2114df8bae1dSRodney W. Grimes 			goto dropafterack;
2115df8bae1dSRodney W. Grimes 		}
2116df8bae1dSRodney W. Grimes 	}
2117df8bae1dSRodney W. Grimes 
2118df8bae1dSRodney W. Grimes step6:
2119df8bae1dSRodney W. Grimes 	/*
2120df8bae1dSRodney W. Grimes 	 * Update window information.
2121df8bae1dSRodney W. Grimes 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2122df8bae1dSRodney W. Grimes 	 */
2123fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) &&
2124fb59c426SYoshinobu Inoue 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
2125fb59c426SYoshinobu Inoue 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
2126fb59c426SYoshinobu Inoue 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
2127df8bae1dSRodney W. Grimes 		/* keep track of pure window updates */
2128fb59c426SYoshinobu Inoue 		if (tlen == 0 &&
2129fb59c426SYoshinobu Inoue 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
2130df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvwinupd++;
2131df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
2132fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq;
2133fb59c426SYoshinobu Inoue 		tp->snd_wl2 = th->th_ack;
2134df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
2135df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
2136df8bae1dSRodney W. Grimes 		needoutput = 1;
2137df8bae1dSRodney W. Grimes 	}
2138df8bae1dSRodney W. Grimes 
2139df8bae1dSRodney W. Grimes 	/*
2140df8bae1dSRodney W. Grimes 	 * Process segments with URG.
2141df8bae1dSRodney W. Grimes 	 */
2142fb59c426SYoshinobu Inoue 	if ((thflags & TH_URG) && th->th_urp &&
2143df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2144df8bae1dSRodney W. Grimes 		/*
2145df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
2146df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
2147df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
2148df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
2149df8bae1dSRodney W. Grimes 		 */
2150fb59c426SYoshinobu Inoue 		if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
2151fb59c426SYoshinobu Inoue 			th->th_urp = 0;			/* XXX */
2152fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;		/* XXX */
2153df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
2154df8bae1dSRodney W. Grimes 		}
2155df8bae1dSRodney W. Grimes 		/*
2156df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
2157df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
2158df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2159df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
2160df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
2161df8bae1dSRodney W. Grimes 		 *
2162df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
2163df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
2164df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
2165df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
2166df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
2167df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
2168df8bae1dSRodney W. Grimes 		 */
2169fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
2170fb59c426SYoshinobu Inoue 			tp->rcv_up = th->th_seq + th->th_urp;
2171df8bae1dSRodney W. Grimes 			so->so_oobmark = so->so_rcv.sb_cc +
2172df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
21734cc20ab1SSeigo Tanimura 			if (so->so_oobmark == 0)
2174df8bae1dSRodney W. Grimes 				so->so_state |= SS_RCVATMARK;
2175df8bae1dSRodney W. Grimes 			sohasoutofband(so);
2176df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2177df8bae1dSRodney W. Grimes 		}
2178df8bae1dSRodney W. Grimes 		/*
2179df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
2180df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
2181df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
2182df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
2183df8bae1dSRodney W. Grimes 		 */
218430613f56SJeffrey Hsu 		if (th->th_urp <= (u_long)tlen &&
218530613f56SJeffrey Hsu 		    !(so->so_options & SO_OOBINLINE)) {
218630613f56SJeffrey Hsu 			/* hdr drop is delayed */
218730613f56SJeffrey Hsu 			tcp_pulloutofband(so, th, m, drop_hdrlen);
218830613f56SJeffrey Hsu 		}
2189c068736aSJeffrey Hsu 	} else {
2190df8bae1dSRodney W. Grimes 		/*
2191df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
2192df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
2193df8bae1dSRodney W. Grimes 		 * with the receive window.
2194df8bae1dSRodney W. Grimes 		 */
2195df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
2196df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
2197c068736aSJeffrey Hsu 	}
2198df8bae1dSRodney W. Grimes dodata:							/* XXX */
2199f76fcf6dSJeffrey Hsu 	KASSERT(headlocked, ("headlocked"));
2200df8bae1dSRodney W. Grimes 	/*
2201df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
2202df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
2203df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
2204df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
2205df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
2206df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
2207df8bae1dSRodney W. Grimes 	 */
2208fb59c426SYoshinobu Inoue 	if ((tlen || (thflags & TH_FIN)) &&
2209df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2210fb59c426SYoshinobu Inoue 		m_adj(m, drop_hdrlen);	/* delayed header drop */
2211e4b64281SJesper Skriver 		/*
2212c068736aSJeffrey Hsu 		 * Insert segment which includes th into TCP reassembly queue
2213c068736aSJeffrey Hsu 		 * with control block tp.  Set thflags to whether reassembly now
2214c068736aSJeffrey Hsu 		 * includes a segment with FIN.  This handles the common case
2215c068736aSJeffrey Hsu 		 * inline (segment is the next to be received on an established
2216c068736aSJeffrey Hsu 		 * connection, and the queue is empty), avoiding linkage into
2217c068736aSJeffrey Hsu 		 * and removal from the queue and repetition of various
2218c068736aSJeffrey Hsu 		 * conversions.
2219c068736aSJeffrey Hsu 		 * Set DELACK for segments received in order, but ack
2220c068736aSJeffrey Hsu 		 * immediately when segments are out of order (so
2221c068736aSJeffrey Hsu 		 * fast retransmit can work).
2222e4b64281SJesper Skriver 		 */
2223e4b64281SJesper Skriver 		if (th->th_seq == tp->rcv_nxt &&
2224e4b64281SJesper Skriver 		    LIST_EMPTY(&tp->t_segq) &&
2225e4b64281SJesper Skriver 		    TCPS_HAVEESTABLISHED(tp->t_state)) {
2226e4b64281SJesper Skriver 			if (DELAY_ACK(tp))
22273bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
2228e4b64281SJesper Skriver 			else
2229e4b64281SJesper Skriver 				tp->t_flags |= TF_ACKNOW;
2230e4b64281SJesper Skriver 			tp->rcv_nxt += tlen;
2231e4b64281SJesper Skriver 			thflags = th->th_flags & TH_FIN;
2232e4b64281SJesper Skriver 			tcpstat.tcps_rcvpack++;
2233e4b64281SJesper Skriver 			tcpstat.tcps_rcvbyte += tlen;
2234e4b64281SJesper Skriver 			ND6_HINT(tp);
2235c1c36a2cSMike Silbersack 			if (so->so_state & SS_CANTRCVMORE)
2236c1c36a2cSMike Silbersack 				m_freem(m);
2237c1c36a2cSMike Silbersack 			else
2238395bb186SSam Leffler 				sbappendstream(&so->so_rcv, m);
2239e4b64281SJesper Skriver 			sorwakeup(so);
2240e4b64281SJesper Skriver 		} else {
2241e4b64281SJesper Skriver 			thflags = tcp_reass(tp, th, &tlen, m);
2242e4b64281SJesper Skriver 			tp->t_flags |= TF_ACKNOW;
2243e4b64281SJesper Skriver 		}
2244e4b64281SJesper Skriver 
2245df8bae1dSRodney W. Grimes 		/*
2246df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
2247df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
2248df8bae1dSRodney W. Grimes 		 * buffer size.
2249df8bae1dSRodney W. Grimes 		 */
2250df8bae1dSRodney W. Grimes 		len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
2251df8bae1dSRodney W. Grimes 	} else {
2252df8bae1dSRodney W. Grimes 		m_freem(m);
2253fb59c426SYoshinobu Inoue 		thflags &= ~TH_FIN;
2254df8bae1dSRodney W. Grimes 	}
2255df8bae1dSRodney W. Grimes 
2256df8bae1dSRodney W. Grimes 	/*
2257df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
2258df8bae1dSRodney W. Grimes 	 * that the connection is closing.
2259df8bae1dSRodney W. Grimes 	 */
2260fb59c426SYoshinobu Inoue 	if (thflags & TH_FIN) {
2261df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2262df8bae1dSRodney W. Grimes 			socantrcvmore(so);
2263a0292f23SGarrett Wollman 			/*
2264a0292f23SGarrett Wollman 			 * If connection is half-synchronized
2265d3eede9dSAndras Olah 			 * (ie NEEDSYN flag on) then delay ACK,
2266a0292f23SGarrett Wollman 			 * so it may be piggybacked when SYN is sent.
2267a0292f23SGarrett Wollman 			 * Otherwise, since we received a FIN then no
2268a0292f23SGarrett Wollman 			 * more input can be expected, send ACK now.
2269a0292f23SGarrett Wollman 			 */
22703bfd6421SJonathan Lemon 			if (tp->t_flags & TF_NEEDSYN)
22713bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
2272a0292f23SGarrett Wollman 			else
2273df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
2274df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
2275df8bae1dSRodney W. Grimes 		}
2276df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2277df8bae1dSRodney W. Grimes 
2278df8bae1dSRodney W. Grimes 	 	/*
2279df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
2280df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
2281df8bae1dSRodney W. Grimes 		 */
2282df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
22839b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
22849b8b58e0SJonathan Lemon 			/*FALLTHROUGH*/
2285df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
2286df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSE_WAIT;
2287df8bae1dSRodney W. Grimes 			break;
2288df8bae1dSRodney W. Grimes 
2289df8bae1dSRodney W. Grimes 	 	/*
2290df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2291df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
2292df8bae1dSRodney W. Grimes 		 */
2293df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2294df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSING;
2295df8bae1dSRodney W. Grimes 			break;
2296df8bae1dSRodney W. Grimes 
2297df8bae1dSRodney W. Grimes 	 	/*
2298df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2299df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
2300df8bae1dSRodney W. Grimes 		 * standard timers.
2301df8bae1dSRodney W. Grimes 		 */
2302df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
230311a20fb8SJeffrey Hsu 			KASSERT(headlocked == 1, ("headlocked should be 1"));
2304340c35deSJonathan Lemon 			tcp_twstart(tp);
230511a20fb8SJeffrey Hsu 			INP_INFO_WUNLOCK(&tcbinfo);
2306340c35deSJonathan Lemon 			return;
2307df8bae1dSRodney W. Grimes 
2308df8bae1dSRodney W. Grimes 		/*
2309df8bae1dSRodney W. Grimes 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
2310df8bae1dSRodney W. Grimes 		 */
2311df8bae1dSRodney W. Grimes 		case TCPS_TIME_WAIT:
2312340c35deSJonathan Lemon 			KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
23139b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, 2 * tcp_msl,
23149b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
2315df8bae1dSRodney W. Grimes 			break;
2316df8bae1dSRodney W. Grimes 		}
2317df8bae1dSRodney W. Grimes 	}
23187792ea27SJeffrey Hsu 	INP_INFO_WUNLOCK(&tcbinfo);
2319610ee2f9SDavid Greenman #ifdef TCPDEBUG
23204cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
2321fb59c426SYoshinobu Inoue 		tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
2322fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2323610ee2f9SDavid Greenman #endif
2324df8bae1dSRodney W. Grimes 
2325df8bae1dSRodney W. Grimes 	/*
2326df8bae1dSRodney W. Grimes 	 * Return any desired output.
2327df8bae1dSRodney W. Grimes 	 */
2328df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
2329df8bae1dSRodney W. Grimes 		(void) tcp_output(tp);
23307792ea27SJeffrey Hsu 
2331a14c749fSJonathan Lemon check_delack:
2332a14c749fSJonathan Lemon 	if (tp->t_flags & TF_DELACK) {
23333bfd6421SJonathan Lemon 		tp->t_flags &= ~TF_DELACK;
23343bfd6421SJonathan Lemon 		callout_reset(tp->tt_delack, tcp_delacktime,
23353bfd6421SJonathan Lemon 		    tcp_timer_delack, tp);
23363bfd6421SJonathan Lemon 	}
2337f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
2338df8bae1dSRodney W. Grimes 	return;
2339df8bae1dSRodney W. Grimes 
2340df8bae1dSRodney W. Grimes dropafterack:
2341df8bae1dSRodney W. Grimes 	/*
2342df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
2343df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
234480ab7c0eSGarrett Wollman 	 *
234580ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
234680ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
234780ab7c0eSGarrett Wollman 	 * RST have been dropped.
234880ab7c0eSGarrett Wollman 	 *
234980ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
235080ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
235180ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
235280ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
235380ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
235480ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
2355df8bae1dSRodney W. Grimes 	 */
2356fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
2357fb59c426SYoshinobu Inoue 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
2358a57815efSBosko Milekic 	     SEQ_GT(th->th_ack, tp->snd_max)) ) {
2359a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2360a57815efSBosko Milekic 		goto dropwithreset;
2361a57815efSBosko Milekic 	}
2362a0292f23SGarrett Wollman #ifdef TCPDEBUG
23634cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
2364fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2365fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2366a0292f23SGarrett Wollman #endif
236785e8b243SJeffrey Hsu 	KASSERT(headlocked, ("headlocked should be 1"));
2368f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&tcbinfo);
2369df8bae1dSRodney W. Grimes 	m_freem(m);
2370df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
2371df8bae1dSRodney W. Grimes 	(void) tcp_output(tp);
2372f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
2373df8bae1dSRodney W. Grimes 	return;
2374df8bae1dSRodney W. Grimes 
2375df8bae1dSRodney W. Grimes dropwithreset:
2376df8bae1dSRodney W. Grimes 	/*
2377df8bae1dSRodney W. Grimes 	 * Generate a RST, dropping incoming segment.
2378df8bae1dSRodney W. Grimes 	 * Make ACK acceptable to originator of segment.
2379df8bae1dSRodney W. Grimes 	 * Don't bother to respond if destination was broadcast/multicast.
2380df8bae1dSRodney W. Grimes 	 */
2381fb59c426SYoshinobu Inoue 	if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
2382df8bae1dSRodney W. Grimes 		goto drop;
2383fb59c426SYoshinobu Inoue 	if (isipv6) {
2384173c0f9fSWarner Losh 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
2385173c0f9fSWarner Losh 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
2386fb59c426SYoshinobu Inoue 			goto drop;
2387c068736aSJeffrey Hsu 	} else {
2388173c0f9fSWarner Losh 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
2389173c0f9fSWarner Losh 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
23902ca2159fSCrist J. Clark 	    	    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
23912ca2159fSCrist J. Clark 	    	    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
2392fb59c426SYoshinobu Inoue 			goto drop;
2393c068736aSJeffrey Hsu 	}
2394fb59c426SYoshinobu Inoue 	/* IPv6 anycast check is done at tcp6_input() */
2395a57815efSBosko Milekic 
2396a57815efSBosko Milekic 	/*
2397c59319bfSDag-Erling Smørgrav 	 * Perform bandwidth limiting.
2398a57815efSBosko Milekic 	 */
2399a57815efSBosko Milekic 	if (badport_bandlim(rstreason) < 0)
2400a57815efSBosko Milekic 		goto drop;
2401a57815efSBosko Milekic 
2402a0292f23SGarrett Wollman #ifdef TCPDEBUG
24034cc20ab1SSeigo Tanimura 	if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
2404fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2405fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2406a0292f23SGarrett Wollman #endif
24076fd22cafSJeffrey Hsu 
2408fb59c426SYoshinobu Inoue 	if (thflags & TH_ACK)
2409fb59c426SYoshinobu Inoue 		/* mtod() below is safe as long as hdr dropping is delayed */
2410fb59c426SYoshinobu Inoue 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack,
2411fb59c426SYoshinobu Inoue 			    TH_RST);
2412df8bae1dSRodney W. Grimes 	else {
2413fb59c426SYoshinobu Inoue 		if (thflags & TH_SYN)
2414fb59c426SYoshinobu Inoue 			tlen++;
2415fb59c426SYoshinobu Inoue 		/* mtod() below is safe as long as hdr dropping is delayed */
2416fb59c426SYoshinobu Inoue 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
2417fb59c426SYoshinobu Inoue 			    (tcp_seq)0, TH_RST|TH_ACK);
2418df8bae1dSRodney W. Grimes 	}
2419c29afad6SSam Leffler 
2420c29afad6SSam Leffler 	if (tp)
2421c29afad6SSam Leffler 		INP_UNLOCK(inp);
2422f76fcf6dSJeffrey Hsu 	if (headlocked)
2423f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&tcbinfo);
2424df8bae1dSRodney W. Grimes 	return;
2425df8bae1dSRodney W. Grimes 
2426df8bae1dSRodney W. Grimes drop:
2427df8bae1dSRodney W. Grimes 	/*
2428df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
2429df8bae1dSRodney W. Grimes 	 */
2430610ee2f9SDavid Greenman #ifdef TCPDEBUG
24314cc20ab1SSeigo Tanimura 	if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
2432fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2433fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2434a0292f23SGarrett Wollman #endif
2435f76fcf6dSJeffrey Hsu 	if (tp)
2436f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
2437df8bae1dSRodney W. Grimes 	m_freem(m);
2438f76fcf6dSJeffrey Hsu 	if (headlocked)
2439f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&tcbinfo);
2440df8bae1dSRodney W. Grimes 	return;
2441df8bae1dSRodney W. Grimes }
2442df8bae1dSRodney W. Grimes 
2443be2ac88cSJonathan Lemon /*
2444be2ac88cSJonathan Lemon  * Parse TCP options and place in tcpopt.
2445be2ac88cSJonathan Lemon  */
24460312fbe9SPoul-Henning Kamp static void
2447be2ac88cSJonathan Lemon tcp_dooptions(to, cp, cnt, is_syn)
2448be2ac88cSJonathan Lemon 	struct tcpopt *to;
2449df8bae1dSRodney W. Grimes 	u_char *cp;
2450df8bae1dSRodney W. Grimes 	int cnt;
2451152385d1SDavid E. O'Brien 	int is_syn;
2452df8bae1dSRodney W. Grimes {
2453df8bae1dSRodney W. Grimes 	int opt, optlen;
2454df8bae1dSRodney W. Grimes 
2455be2ac88cSJonathan Lemon 	to->to_flags = 0;
2456df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
2457df8bae1dSRodney W. Grimes 		opt = cp[0];
2458df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
2459df8bae1dSRodney W. Grimes 			break;
2460df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
2461df8bae1dSRodney W. Grimes 			optlen = 1;
2462df8bae1dSRodney W. Grimes 		else {
2463b474779fSJun-ichiro itojun Hagino 			if (cnt < 2)
2464b474779fSJun-ichiro itojun Hagino 				break;
2465df8bae1dSRodney W. Grimes 			optlen = cp[1];
2466b474779fSJun-ichiro itojun Hagino 			if (optlen < 2 || optlen > cnt)
2467df8bae1dSRodney W. Grimes 				break;
2468df8bae1dSRodney W. Grimes 		}
2469df8bae1dSRodney W. Grimes 		switch (opt) {
2470df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
2471df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
2472df8bae1dSRodney W. Grimes 				continue;
2473be2ac88cSJonathan Lemon 			if (!is_syn)
2474df8bae1dSRodney W. Grimes 				continue;
2475be2ac88cSJonathan Lemon 			to->to_flags |= TOF_MSS;
2476be2ac88cSJonathan Lemon 			bcopy((char *)cp + 2,
2477be2ac88cSJonathan Lemon 			    (char *)&to->to_mss, sizeof(to->to_mss));
2478fd8e4ebcSMike Barcroft 			to->to_mss = ntohs(to->to_mss);
2479df8bae1dSRodney W. Grimes 			break;
2480df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
2481df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
2482df8bae1dSRodney W. Grimes 				continue;
2483be2ac88cSJonathan Lemon 			if (! is_syn)
2484df8bae1dSRodney W. Grimes 				continue;
2485be2ac88cSJonathan Lemon 			to->to_flags |= TOF_SCALE;
2486be2ac88cSJonathan Lemon 			to->to_requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
2487df8bae1dSRodney W. Grimes 			break;
2488df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
2489df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
2490df8bae1dSRodney W. Grimes 				continue;
2491be2ac88cSJonathan Lemon 			to->to_flags |= TOF_TS;
2492a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2493a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
2494fd8e4ebcSMike Barcroft 			to->to_tsval = ntohl(to->to_tsval);
2495a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
2496a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
2497fd8e4ebcSMike Barcroft 			to->to_tsecr = ntohl(to->to_tsecr);
2498df8bae1dSRodney W. Grimes 			break;
2499a0292f23SGarrett Wollman 		case TCPOPT_CC:
2500a0292f23SGarrett Wollman 			if (optlen != TCPOLEN_CC)
2501a0292f23SGarrett Wollman 				continue;
2502be2ac88cSJonathan Lemon 			to->to_flags |= TOF_CC;
2503a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2504a0292f23SGarrett Wollman 			    (char *)&to->to_cc, sizeof(to->to_cc));
2505fd8e4ebcSMike Barcroft 			to->to_cc = ntohl(to->to_cc);
2506a0292f23SGarrett Wollman 			break;
2507a0292f23SGarrett Wollman 		case TCPOPT_CCNEW:
2508a0292f23SGarrett Wollman 			if (optlen != TCPOLEN_CC)
2509a0292f23SGarrett Wollman 				continue;
2510be2ac88cSJonathan Lemon 			if (!is_syn)
2511a0292f23SGarrett Wollman 				continue;
2512be2ac88cSJonathan Lemon 			to->to_flags |= TOF_CCNEW;
2513a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2514a0292f23SGarrett Wollman 			    (char *)&to->to_cc, sizeof(to->to_cc));
2515fd8e4ebcSMike Barcroft 			to->to_cc = ntohl(to->to_cc);
2516a0292f23SGarrett Wollman 			break;
2517a0292f23SGarrett Wollman 		case TCPOPT_CCECHO:
2518a0292f23SGarrett Wollman 			if (optlen != TCPOLEN_CC)
2519a0292f23SGarrett Wollman 				continue;
2520be2ac88cSJonathan Lemon 			if (!is_syn)
2521a0292f23SGarrett Wollman 				continue;
2522be2ac88cSJonathan Lemon 			to->to_flags |= TOF_CCECHO;
2523a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2524a0292f23SGarrett Wollman 			    (char *)&to->to_ccecho, sizeof(to->to_ccecho));
2525fd8e4ebcSMike Barcroft 			to->to_ccecho = ntohl(to->to_ccecho);
2526a0292f23SGarrett Wollman 			break;
2527be2ac88cSJonathan Lemon 		default:
2528be2ac88cSJonathan Lemon 			continue;
2529df8bae1dSRodney W. Grimes 		}
2530df8bae1dSRodney W. Grimes 	}
2531df8bae1dSRodney W. Grimes }
2532df8bae1dSRodney W. Grimes 
2533df8bae1dSRodney W. Grimes /*
2534df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
2535df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
2536df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
2537df8bae1dSRodney W. Grimes  * sequencing purposes.
2538df8bae1dSRodney W. Grimes  */
25390312fbe9SPoul-Henning Kamp static void
2540fb59c426SYoshinobu Inoue tcp_pulloutofband(so, th, m, off)
2541df8bae1dSRodney W. Grimes 	struct socket *so;
2542fb59c426SYoshinobu Inoue 	struct tcphdr *th;
2543df8bae1dSRodney W. Grimes 	register struct mbuf *m;
2544fb59c426SYoshinobu Inoue 	int off;		/* delayed to be droped hdrlen */
2545df8bae1dSRodney W. Grimes {
2546fb59c426SYoshinobu Inoue 	int cnt = off + th->th_urp - 1;
2547df8bae1dSRodney W. Grimes 
2548df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
2549df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
2550df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
2551df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
2552df8bae1dSRodney W. Grimes 
2553df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
2554df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
2555df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
2556df8bae1dSRodney W. Grimes 			m->m_len--;
255769a34685SYoshinobu Inoue 			if (m->m_flags & M_PKTHDR)
255869a34685SYoshinobu Inoue 				m->m_pkthdr.len--;
2559df8bae1dSRodney W. Grimes 			return;
2560df8bae1dSRodney W. Grimes 		}
2561df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
2562df8bae1dSRodney W. Grimes 		m = m->m_next;
2563df8bae1dSRodney W. Grimes 		if (m == 0)
2564df8bae1dSRodney W. Grimes 			break;
2565df8bae1dSRodney W. Grimes 	}
2566df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
2567df8bae1dSRodney W. Grimes }
2568df8bae1dSRodney W. Grimes 
2569df8bae1dSRodney W. Grimes /*
2570df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
2571df8bae1dSRodney W. Grimes  * and update averages and current timeout.
2572df8bae1dSRodney W. Grimes  */
25730312fbe9SPoul-Henning Kamp static void
2574df8bae1dSRodney W. Grimes tcp_xmit_timer(tp, rtt)
2575df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
25769b8b58e0SJonathan Lemon 	int rtt;
2577df8bae1dSRodney W. Grimes {
2578233e8c18SGarrett Wollman 	register int delta;
2579233e8c18SGarrett Wollman 
2580233e8c18SGarrett Wollman 	tcpstat.tcps_rttupdated++;
2581233e8c18SGarrett Wollman 	tp->t_rttupdated++;
2582233e8c18SGarrett Wollman 	if (tp->t_srtt != 0) {
2583233e8c18SGarrett Wollman 		/*
2584233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
2585233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
2586233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
2587233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
2588233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
2589233e8c18SGarrett Wollman 		 */
2590233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
2591233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
2592233e8c18SGarrett Wollman 
2593233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
2594233e8c18SGarrett Wollman 			tp->t_srtt = 1;
2595233e8c18SGarrett Wollman 
2596233e8c18SGarrett Wollman 		/*
2597233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
2598233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
2599233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
2600233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
2601233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
2602233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
2603233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
2604233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
2605233e8c18SGarrett Wollman 		 */
2606233e8c18SGarrett Wollman 		if (delta < 0)
2607233e8c18SGarrett Wollman 			delta = -delta;
2608233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
2609233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
2610233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
26111fcc99b5SMatthew Dillon 		if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
26121fcc99b5SMatthew Dillon 		    tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
2613233e8c18SGarrett Wollman 	} else {
2614233e8c18SGarrett Wollman 		/*
2615233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
2616233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
2617233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
2618233e8c18SGarrett Wollman 		 */
2619233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
2620233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
26211fcc99b5SMatthew Dillon 		tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
2622233e8c18SGarrett Wollman 	}
26239b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
2624df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
2625df8bae1dSRodney W. Grimes 
2626df8bae1dSRodney W. Grimes 	/*
2627df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
2628df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
2629df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
2630df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
2631df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
2632df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
2633df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
2634df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
2635df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
2636df8bae1dSRodney W. Grimes 	 */
2637233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
26389e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
2639df8bae1dSRodney W. Grimes 
2640df8bae1dSRodney W. Grimes 	/*
2641df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
2642df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
2643df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
2644df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
2645df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
2646df8bae1dSRodney W. Grimes 	 */
2647df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
2648df8bae1dSRodney W. Grimes }
2649df8bae1dSRodney W. Grimes 
2650df8bae1dSRodney W. Grimes /*
2651df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
2652df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
2653df8bae1dSRodney W. Grimes  * If none, use an mss that can be handled on the outgoing
2654df8bae1dSRodney W. Grimes  * interface without forcing IP to fragment; if bigger than
2655df8bae1dSRodney W. Grimes  * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
2656df8bae1dSRodney W. Grimes  * to utilize large mbufs.  If no route is found, route has no mtu,
2657df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
2658df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
2659df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
2660df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
2661df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
2662df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
2663df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
2664a0292f23SGarrett Wollman  *
2665a0292f23SGarrett Wollman  * Also take into account the space needed for options that we
2666a0292f23SGarrett Wollman  * send regularly.  Make maxseg shorter by that amount to assure
2667a0292f23SGarrett Wollman  * that we can send maxseg amount of data even when the options
2668a0292f23SGarrett Wollman  * are present.  Store the upper limit of the length of options plus
2669a0292f23SGarrett Wollman  * data in maxopd.
2670a0292f23SGarrett Wollman  *
2671a0292f23SGarrett Wollman  *
2672a0292f23SGarrett Wollman  * In case of T/TCP, we call this routine during implicit connection
2673a0292f23SGarrett Wollman  * setup as well (offer = -1), to initialize maxseg from the cached
2674a0292f23SGarrett Wollman  * MSS of our peer.
267597d8d152SAndre Oppermann  *
267697d8d152SAndre Oppermann  * NOTE that this routine is only called when we process an incoming
267797d8d152SAndre Oppermann  * segment. Outgoing SYN/ACK MSS settings are handled in tcp_mssopt().
2678df8bae1dSRodney W. Grimes  */
2679a0292f23SGarrett Wollman void
2680df8bae1dSRodney W. Grimes tcp_mss(tp, offer)
2681a0292f23SGarrett Wollman 	struct tcpcb *tp;
2682a0292f23SGarrett Wollman 	int offer;
2683df8bae1dSRodney W. Grimes {
268497d8d152SAndre Oppermann 	int rtt, mss;
2685df8bae1dSRodney W. Grimes 	u_long bufsize;
268697d8d152SAndre Oppermann 	u_long maxmtu;
2687c068736aSJeffrey Hsu 	struct inpcb *inp = tp->t_inpcb;
2688df8bae1dSRodney W. Grimes 	struct socket *so;
268997d8d152SAndre Oppermann 	struct hc_metrics_lite metrics;
269097d8d152SAndre Oppermann 	struct rmxp_tao tao;
2691a0292f23SGarrett Wollman 	int origoffer = offer;
2692fb59c426SYoshinobu Inoue #ifdef INET6
2693c068736aSJeffrey Hsu 	int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
2694c068736aSJeffrey Hsu 	size_t min_protoh = isipv6 ?
2695c068736aSJeffrey Hsu 			    sizeof (struct ip6_hdr) + sizeof (struct tcphdr) :
2696c068736aSJeffrey Hsu 			    sizeof (struct tcpiphdr);
2697c068736aSJeffrey Hsu #else
2698c068736aSJeffrey Hsu 	const size_t min_protoh = sizeof(struct tcpiphdr);
2699fb59c426SYoshinobu Inoue #endif
270097d8d152SAndre Oppermann 	bzero(&tao, sizeof(tao));
2701df8bae1dSRodney W. Grimes 
270297d8d152SAndre Oppermann 	/* initialize */
270397d8d152SAndre Oppermann #ifdef INET6
270497d8d152SAndre Oppermann 	if (isipv6) {
270597d8d152SAndre Oppermann 		maxmtu = tcp_maxmtu6(&inp->inp_inc);
270697d8d152SAndre Oppermann 		tp->t_maxopd = tp->t_maxseg = tcp_v6mssdflt;
270797d8d152SAndre Oppermann 	} else
270897d8d152SAndre Oppermann #endif
270997d8d152SAndre Oppermann 	{
271097d8d152SAndre Oppermann 		maxmtu = tcp_maxmtu(&inp->inp_inc);
271197d8d152SAndre Oppermann 		tp->t_maxopd = tp->t_maxseg = tcp_mssdflt;
2712df8bae1dSRodney W. Grimes 	}
2713df8bae1dSRodney W. Grimes 	so = inp->inp_socket;
2714df8bae1dSRodney W. Grimes 
271597d8d152SAndre Oppermann 	/*
271697d8d152SAndre Oppermann 	 * no route to sender, take default mss and return
271797d8d152SAndre Oppermann 	 */
271897d8d152SAndre Oppermann 	if (maxmtu == 0)
271997d8d152SAndre Oppermann 		return;
272097d8d152SAndre Oppermann 
272197d8d152SAndre Oppermann 	/* what have we got? */
272297d8d152SAndre Oppermann 	switch (offer) {
272397d8d152SAndre Oppermann 		case 0:
272497d8d152SAndre Oppermann 			/*
272597d8d152SAndre Oppermann 			 * Offer == 0 means that there was no MSS on the SYN
272697d8d152SAndre Oppermann 			 * segment, in this case we use tcp_mssdflt.
272797d8d152SAndre Oppermann 			 */
272897d8d152SAndre Oppermann 			offer =
272997d8d152SAndre Oppermann #ifdef INET6
273097d8d152SAndre Oppermann 				isipv6 ? tcp_v6mssdflt :
273197d8d152SAndre Oppermann #endif
273297d8d152SAndre Oppermann 				tcp_mssdflt;
273397d8d152SAndre Oppermann 			break;
273497d8d152SAndre Oppermann 
273597d8d152SAndre Oppermann 		case -1:
2736a0292f23SGarrett Wollman 			/*
2737a0292f23SGarrett Wollman 			 * Offer == -1 means that we didn't receive SYN yet,
2738a0292f23SGarrett Wollman 			 * use cached value in that case;
2739a0292f23SGarrett Wollman 			 */
274097d8d152SAndre Oppermann 			if (tcp_do_rfc1644)
274197d8d152SAndre Oppermann 				tcp_hc_gettao(&inp->inp_inc, &tao);
274297d8d152SAndre Oppermann 			if (tao.tao_mssopt != 0)
274397d8d152SAndre Oppermann 				offer = tao.tao_mssopt;
274497d8d152SAndre Oppermann 			/* FALLTHROUGH */
274597d8d152SAndre Oppermann 
274697d8d152SAndre Oppermann 		default:
2747a0292f23SGarrett Wollman 			/*
274853369ac9SAndre Oppermann 			 * Prevent DoS attack with too small MSS. Round up
274953369ac9SAndre Oppermann 			 * to at least minmss.
275053369ac9SAndre Oppermann 			 */
275153369ac9SAndre Oppermann 			offer = max(offer, tcp_minmss);
275253369ac9SAndre Oppermann 			/*
2753a0292f23SGarrett Wollman 			 * Sanity check: make sure that maxopd will be large
275497d8d152SAndre Oppermann 			 * enough to allow some data on segments even if the
2755a0292f23SGarrett Wollman 			 * all the option space is used (40bytes).  Otherwise
2756a0292f23SGarrett Wollman 			 * funny things may happen in tcp_output.
2757a0292f23SGarrett Wollman 			 */
2758a0292f23SGarrett Wollman 			offer = max(offer, 64);
275997d8d152SAndre Oppermann 			if (tcp_do_rfc1644)
276097d8d152SAndre Oppermann 				tcp_hc_updatetao(&inp->inp_inc,
276197d8d152SAndre Oppermann 						 TCP_HC_TAO_MSSOPT, 0, offer);
276297d8d152SAndre Oppermann 	}
2763a0292f23SGarrett Wollman 
2764df8bae1dSRodney W. Grimes 	/*
276597d8d152SAndre Oppermann 	 * rmx information is now retrieved from tcp_hostcache
2766df8bae1dSRodney W. Grimes 	 */
276797d8d152SAndre Oppermann 	tcp_hc_get(&inp->inp_inc, &metrics);
276897d8d152SAndre Oppermann 
2769df8bae1dSRodney W. Grimes 	/*
277097d8d152SAndre Oppermann 	 * if there's a discovered mtu int tcp hostcache, use it
2771fb59c426SYoshinobu Inoue 	 * else, use the link mtu.
2772df8bae1dSRodney W. Grimes 	 */
277397d8d152SAndre Oppermann 	if (metrics.rmx_mtu)
277497d8d152SAndre Oppermann 		mss = metrics.rmx_mtu - min_protoh;
2775c068736aSJeffrey Hsu 	else {
277631b3783cSHajimu UMEMOTO #ifdef INET6
2777fb59c426SYoshinobu Inoue 		if (isipv6) {
277897d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
277997d8d152SAndre Oppermann 			if (!path_mtu_discovery &&
278097d8d152SAndre Oppermann 			    !in6_localaddr(&inp->in6p_faddr))
2781fb59c426SYoshinobu Inoue 				mss = min(mss, tcp_v6mssdflt);
2782b3399803SHajimu UMEMOTO 		} else
2783b3399803SHajimu UMEMOTO #endif
278497d8d152SAndre Oppermann 		{
278597d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
278697d8d152SAndre Oppermann 			if (!path_mtu_discovery &&
278797d8d152SAndre Oppermann 			    !in_localaddr(inp->inp_faddr))
2788a0292f23SGarrett Wollman 				mss = min(mss, tcp_mssdflt);
2789a0292f23SGarrett Wollman 		}
279097d8d152SAndre Oppermann 	}
2791a0292f23SGarrett Wollman 	mss = min(mss, offer);
279297d8d152SAndre Oppermann 
2793a0292f23SGarrett Wollman 	/*
2794a0292f23SGarrett Wollman 	 * maxopd stores the maximum length of data AND options
2795a0292f23SGarrett Wollman 	 * in a segment; maxseg is the amount of data in a normal
2796a0292f23SGarrett Wollman 	 * segment.  We need to store this value (maxopd) apart
2797a0292f23SGarrett Wollman 	 * from maxseg, because now every segment carries options
2798a0292f23SGarrett Wollman 	 * and thus we normally have somewhat less data in segments.
2799a0292f23SGarrett Wollman 	 */
2800a0292f23SGarrett Wollman 	tp->t_maxopd = mss;
2801a0292f23SGarrett Wollman 
2802a0292f23SGarrett Wollman 	/*
2803a0292f23SGarrett Wollman 	 * In case of T/TCP, origoffer==-1 indicates, that no segments
2804a0292f23SGarrett Wollman 	 * were received yet.  In this case we just guess, otherwise
2805a0292f23SGarrett Wollman 	 * we do the same as before T/TCP.
2806a0292f23SGarrett Wollman 	 */
2807a0292f23SGarrett Wollman  	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
2808a0292f23SGarrett Wollman 	    (origoffer == -1 ||
2809a0292f23SGarrett Wollman 	     (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
2810a0292f23SGarrett Wollman 		mss -= TCPOLEN_TSTAMP_APPA;
2811a0292f23SGarrett Wollman  	if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
2812a0292f23SGarrett Wollman 	    (origoffer == -1 ||
2813a0292f23SGarrett Wollman 	     (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC))
2814a0292f23SGarrett Wollman 		mss -= TCPOLEN_CC_APPA;
281597d8d152SAndre Oppermann 	tp->t_maxseg = mss;
2816a0292f23SGarrett Wollman 
2817df8bae1dSRodney W. Grimes #if	(MCLBYTES & (MCLBYTES - 1)) == 0
2818df8bae1dSRodney W. Grimes 		if (mss > MCLBYTES)
2819df8bae1dSRodney W. Grimes 			mss &= ~(MCLBYTES-1);
2820df8bae1dSRodney W. Grimes #else
2821df8bae1dSRodney W. Grimes 		if (mss > MCLBYTES)
2822df8bae1dSRodney W. Grimes 			mss = mss / MCLBYTES * MCLBYTES;
2823df8bae1dSRodney W. Grimes #endif
282497d8d152SAndre Oppermann 	tp->t_maxseg = mss;
282597d8d152SAndre Oppermann 
2826df8bae1dSRodney W. Grimes 	/*
282797d8d152SAndre Oppermann 	 * If there's a pipesize, change the socket buffer to that size,
282897d8d152SAndre Oppermann 	 * don't change if sb_hiwat is different than default (then it
282997d8d152SAndre Oppermann 	 * has been changed on purpose with setsockopt).
283097d8d152SAndre Oppermann 	 * Make the socket buffers an integral number of mss units;
283197d8d152SAndre Oppermann 	 * if the mss is larger than the socket buffer, decrease the mss.
2832df8bae1dSRodney W. Grimes 	 */
283397d8d152SAndre Oppermann 	if ((so->so_snd.sb_hiwat == tcp_sendspace) && metrics.rmx_sendpipe)
283497d8d152SAndre Oppermann 		bufsize = metrics.rmx_sendpipe;
283597d8d152SAndre Oppermann 	else
2836df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
2837df8bae1dSRodney W. Grimes 	if (bufsize < mss)
2838df8bae1dSRodney W. Grimes 		mss = bufsize;
2839df8bae1dSRodney W. Grimes 	else {
2840df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
2841df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
2842df8bae1dSRodney W. Grimes 			bufsize = sb_max;
284388c39af3SRuslan Ermilov 		if (bufsize > so->so_snd.sb_hiwat)
2844ecf72308SBrian Feldman 			(void)sbreserve(&so->so_snd, bufsize, so, NULL);
2845df8bae1dSRodney W. Grimes 	}
2846df8bae1dSRodney W. Grimes 	tp->t_maxseg = mss;
2847df8bae1dSRodney W. Grimes 
284897d8d152SAndre Oppermann 	if ((so->so_rcv.sb_hiwat == tcp_recvspace) && metrics.rmx_recvpipe)
284997d8d152SAndre Oppermann 		bufsize = metrics.rmx_recvpipe;
285097d8d152SAndre Oppermann 	else
2851df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
2852df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
2853df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
2854df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
2855df8bae1dSRodney W. Grimes 			bufsize = sb_max;
285688c39af3SRuslan Ermilov 		if (bufsize > so->so_rcv.sb_hiwat)
2857ecf72308SBrian Feldman 			(void)sbreserve(&so->so_rcv, bufsize, so, NULL);
2858df8bae1dSRodney W. Grimes 	}
2859a0292f23SGarrett Wollman 	/*
286097d8d152SAndre Oppermann 	 * While we're here, check the others too
2861a0292f23SGarrett Wollman 	 */
286297d8d152SAndre Oppermann 	if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) {
286397d8d152SAndre Oppermann 		tp->t_srtt = rtt;
286497d8d152SAndre Oppermann 		tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE;
286597d8d152SAndre Oppermann 		tcpstat.tcps_usedrtt++;
286697d8d152SAndre Oppermann 		if (metrics.rmx_rttvar) {
286797d8d152SAndre Oppermann 			tp->t_rttvar = metrics.rmx_rttvar;
286897d8d152SAndre Oppermann 			tcpstat.tcps_usedrttvar++;
286997d8d152SAndre Oppermann 		} else {
287097d8d152SAndre Oppermann 			/* default variation is +- 1 rtt */
287197d8d152SAndre Oppermann 			tp->t_rttvar =
287297d8d152SAndre Oppermann 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
287397d8d152SAndre Oppermann 		}
287497d8d152SAndre Oppermann 		TCPT_RANGESET(tp->t_rxtcur,
287597d8d152SAndre Oppermann 			      ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
287697d8d152SAndre Oppermann 			      tp->t_rttmin, TCPTV_REXMTMAX);
287797d8d152SAndre Oppermann 	}
287897d8d152SAndre Oppermann 	if (metrics.rmx_ssthresh) {
2879df8bae1dSRodney W. Grimes 		/*
2880df8bae1dSRodney W. Grimes 		 * There's some sort of gateway or interface
2881df8bae1dSRodney W. Grimes 		 * buffer limit on the path.  Use this to set
2882df8bae1dSRodney W. Grimes 		 * the slow start threshhold, but set the
2883df8bae1dSRodney W. Grimes 		 * threshold to no less than 2*mss.
2884df8bae1dSRodney W. Grimes 		 */
288597d8d152SAndre Oppermann 		tp->snd_ssthresh = max(2 * mss, metrics.rmx_ssthresh);
2886dd224982SGarrett Wollman 		tcpstat.tcps_usedssthresh++;
2887df8bae1dSRodney W. Grimes 	}
288897d8d152SAndre Oppermann 	if (metrics.rmx_bandwidth)
288997d8d152SAndre Oppermann 		tp->snd_bandwidth = metrics.rmx_bandwidth;
289097d8d152SAndre Oppermann 
289197d8d152SAndre Oppermann 	/*
289297d8d152SAndre Oppermann 	 * Set the slow-start flight size depending on whether this
289397d8d152SAndre Oppermann 	 * is a local network or not.
289497d8d152SAndre Oppermann 	 *
289597d8d152SAndre Oppermann 	 * Extend this so we cache the cwnd too and retrieve it here.
289697d8d152SAndre Oppermann 	 * Make cwnd even bigger than RFC3390 suggests but only if we
289797d8d152SAndre Oppermann 	 * have previous experience with the remote host. Be careful
289897d8d152SAndre Oppermann 	 * not make cwnd bigger than remote receive window or our own
289997d8d152SAndre Oppermann 	 * send socket buffer. Maybe put some additional upper bound
290097d8d152SAndre Oppermann 	 * on the retrieved cwnd. Should do incremental updates to
290197d8d152SAndre Oppermann 	 * hostcache when cwnd collapses so next connection doesn't
290297d8d152SAndre Oppermann 	 * overloads the path again.
290397d8d152SAndre Oppermann 	 *
290497d8d152SAndre Oppermann 	 * RFC3390 says only do this if SYN or SYN/ACK didn't got lost.
290597d8d152SAndre Oppermann 	 * We currently check only in syncache_socket for that.
290697d8d152SAndre Oppermann 	 */
290797d8d152SAndre Oppermann #define TCP_METRICS_CWND
290897d8d152SAndre Oppermann #ifdef TCP_METRICS_CWND
290997d8d152SAndre Oppermann 	if (metrics.rmx_cwnd)
291097d8d152SAndre Oppermann 		tp->snd_cwnd = max(mss,
291197d8d152SAndre Oppermann 				min(metrics.rmx_cwnd / 2,
291297d8d152SAndre Oppermann 				 min(tp->snd_wnd, so->so_snd.sb_hiwat)));
291397d8d152SAndre Oppermann 	else
291497d8d152SAndre Oppermann #endif
291597d8d152SAndre Oppermann 	if (tcp_do_rfc3390)
291697d8d152SAndre Oppermann 		tp->snd_cwnd = min(4 * mss, max(2 * mss, 4380));
291797d8d152SAndre Oppermann #ifdef INET6
291897d8d152SAndre Oppermann 	else if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) ||
291997d8d152SAndre Oppermann 		 (!isipv6 && in_localaddr(inp->inp_faddr)))
2920943ae302SAndre Oppermann #else
2921943ae302SAndre Oppermann 	else if (in_localaddr(inp->inp_faddr))
292297d8d152SAndre Oppermann #endif
2923943ae302SAndre Oppermann 		tp->snd_cwnd = mss * ss_fltsz_local;
292497d8d152SAndre Oppermann 	else
292597d8d152SAndre Oppermann 		tp->snd_cwnd = mss * ss_fltsz;
2926a0292f23SGarrett Wollman }
2927a0292f23SGarrett Wollman 
2928a0292f23SGarrett Wollman /*
2929a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
2930a0292f23SGarrett Wollman  */
2931a0292f23SGarrett Wollman int
293297d8d152SAndre Oppermann tcp_mssopt(inc)
293397d8d152SAndre Oppermann 	struct in_conninfo *inc;
2934a0292f23SGarrett Wollman {
293597d8d152SAndre Oppermann 	int mss = 0;
293697d8d152SAndre Oppermann 	u_long maxmtu = 0;
293797d8d152SAndre Oppermann 	u_long thcmtu = 0;
293897d8d152SAndre Oppermann 	size_t min_protoh;
2939fb59c426SYoshinobu Inoue #ifdef INET6
294097d8d152SAndre Oppermann 	int isipv6 = inc->inc_isipv6 ? 1 : 0;
2941fb59c426SYoshinobu Inoue #endif
2942a0292f23SGarrett Wollman 
294397d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer"));
2944a0292f23SGarrett Wollman 
294531b3783cSHajimu UMEMOTO #ifdef INET6
294697d8d152SAndre Oppermann 	if (isipv6) {
294797d8d152SAndre Oppermann 		mss = tcp_v6mssdflt;
294897d8d152SAndre Oppermann 		maxmtu = tcp_maxmtu6(inc);
294997d8d152SAndre Oppermann 		thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
295097d8d152SAndre Oppermann 		min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
295197d8d152SAndre Oppermann 	} else
295231b3783cSHajimu UMEMOTO #endif
295397d8d152SAndre Oppermann 	{
295497d8d152SAndre Oppermann 		mss = tcp_mssdflt;
295597d8d152SAndre Oppermann 		maxmtu = tcp_maxmtu(inc);
295697d8d152SAndre Oppermann 		thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
295797d8d152SAndre Oppermann 		min_protoh = sizeof(struct tcpiphdr);
295897d8d152SAndre Oppermann 	}
295997d8d152SAndre Oppermann 	if (maxmtu && thcmtu)
296097d8d152SAndre Oppermann 		mss = min(maxmtu, thcmtu) - min_protoh;
296197d8d152SAndre Oppermann 	else if (maxmtu || thcmtu)
296297d8d152SAndre Oppermann 		mss = max(maxmtu, thcmtu) - min_protoh;
296397d8d152SAndre Oppermann 
296497d8d152SAndre Oppermann 	return (mss);
2965df8bae1dSRodney W. Grimes }
296646f58482SJonathan Lemon 
296746f58482SJonathan Lemon 
296846f58482SJonathan Lemon /*
2969c068736aSJeffrey Hsu  * On a partial ack arrives, force the retransmission of the
2970c068736aSJeffrey Hsu  * next unacknowledged segment.  Do not clear tp->t_dupacks.
2971c068736aSJeffrey Hsu  * By setting snd_nxt to ti_ack, this forces retransmission timer to
2972c068736aSJeffrey Hsu  * be started again.
297346f58482SJonathan Lemon  */
2974c068736aSJeffrey Hsu static void
2975c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th)
297646f58482SJonathan Lemon 	struct tcpcb *tp;
297746f58482SJonathan Lemon 	struct tcphdr *th;
297846f58482SJonathan Lemon {
297946f58482SJonathan Lemon 	tcp_seq onxt = tp->snd_nxt;
298046f58482SJonathan Lemon 	u_long  ocwnd = tp->snd_cwnd;
298146f58482SJonathan Lemon 
298246f58482SJonathan Lemon 	callout_stop(tp->tt_rexmt);
298346f58482SJonathan Lemon 	tp->t_rtttime = 0;
298446f58482SJonathan Lemon 	tp->snd_nxt = th->th_ack;
29856b2a5f92SJayanth Vijayaraghavan 	/*
2986c068736aSJeffrey Hsu 	 * Set snd_cwnd to one segment beyond acknowledged offset.
2987c068736aSJeffrey Hsu 	 * (tp->snd_una has not yet been updated when this function is called.)
29886b2a5f92SJayanth Vijayaraghavan 	 */
29896b2a5f92SJayanth Vijayaraghavan 	tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una);
2990a84db8f4SMatthew Dillon 	tp->t_flags |= TF_ACKNOW;
29916b2a5f92SJayanth Vijayaraghavan 	(void) tcp_output(tp);
299246f58482SJonathan Lemon 	tp->snd_cwnd = ocwnd;
299346f58482SJonathan Lemon 	if (SEQ_GT(onxt, tp->snd_nxt))
299446f58482SJonathan Lemon 		tp->snd_nxt = onxt;
299546f58482SJonathan Lemon 	/*
299646f58482SJonathan Lemon 	 * Partial window deflation.  Relies on fact that tp->snd_una
299746f58482SJonathan Lemon 	 * not updated yet.
299846f58482SJonathan Lemon 	 */
299946f58482SJonathan Lemon 	tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_maxseg);
300046f58482SJonathan Lemon }
3001340c35deSJonathan Lemon 
3002340c35deSJonathan Lemon /*
3003340c35deSJonathan Lemon  * Returns 1 if the TIME_WAIT state was killed and we should start over,
3004340c35deSJonathan Lemon  * looking for a pcb in the listen state.  Returns 0 otherwise.
3005340c35deSJonathan Lemon  */
3006340c35deSJonathan Lemon static int
3007340c35deSJonathan Lemon tcp_timewait(tw, to, th, m, tlen)
3008340c35deSJonathan Lemon 	struct tcptw *tw;
3009340c35deSJonathan Lemon 	struct tcpopt *to;
3010340c35deSJonathan Lemon 	struct tcphdr *th;
3011340c35deSJonathan Lemon 	struct mbuf *m;
3012340c35deSJonathan Lemon 	int tlen;
3013340c35deSJonathan Lemon {
3014340c35deSJonathan Lemon 	int thflags;
3015340c35deSJonathan Lemon 	tcp_seq seq;
3016340c35deSJonathan Lemon #ifdef INET6
3017340c35deSJonathan Lemon 	int isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
3018340c35deSJonathan Lemon #else
3019340c35deSJonathan Lemon 	const int isipv6 = 0;
3020340c35deSJonathan Lemon #endif
3021340c35deSJonathan Lemon 
3022340c35deSJonathan Lemon 	thflags = th->th_flags;
3023340c35deSJonathan Lemon 
3024340c35deSJonathan Lemon 	/*
3025340c35deSJonathan Lemon 	 * NOTE: for FIN_WAIT_2 (to be added later),
3026340c35deSJonathan Lemon 	 * must validate sequence number before accepting RST
3027340c35deSJonathan Lemon 	 */
3028340c35deSJonathan Lemon 
3029340c35deSJonathan Lemon 	/*
3030340c35deSJonathan Lemon 	 * If the segment contains RST:
3031340c35deSJonathan Lemon 	 *	Drop the segment - see Stevens, vol. 2, p. 964 and
3032340c35deSJonathan Lemon 	 *      RFC 1337.
3033340c35deSJonathan Lemon 	 */
3034340c35deSJonathan Lemon 	if (thflags & TH_RST)
3035340c35deSJonathan Lemon 		goto drop;
3036340c35deSJonathan Lemon 
3037340c35deSJonathan Lemon 	/*
3038340c35deSJonathan Lemon 	 * If segment contains a SYN and CC [not CC.NEW] option:
3039340c35deSJonathan Lemon 	 * 	if connection duration > MSL, drop packet and send RST;
3040340c35deSJonathan Lemon 	 *
3041340c35deSJonathan Lemon 	 *	if SEG.CC > CCrecv then is new SYN.
3042340c35deSJonathan Lemon 	 *	    Complete close and delete TCPCB.  Then reprocess
3043340c35deSJonathan Lemon 	 *	    segment, hoping to find new TCPCB in LISTEN state;
3044340c35deSJonathan Lemon 	 *
3045340c35deSJonathan Lemon 	 *	else must be old SYN; drop it.
3046340c35deSJonathan Lemon 	 * else do normal processing.
3047340c35deSJonathan Lemon 	 */
3048340c35deSJonathan Lemon 	if ((thflags & TH_SYN) && (to->to_flags & TOF_CC) && tw->cc_recv != 0) {
3049340c35deSJonathan Lemon 		if ((ticks - tw->t_starttime) > tcp_msl)
3050340c35deSJonathan Lemon 			goto reset;
3051340c35deSJonathan Lemon 		if (CC_GT(to->to_cc, tw->cc_recv)) {
3052607b0b0cSJonathan Lemon 			(void) tcp_twclose(tw, 0);
3053340c35deSJonathan Lemon 			return (1);
3054340c35deSJonathan Lemon 		}
3055340c35deSJonathan Lemon 		goto drop;
3056340c35deSJonathan Lemon 	}
3057340c35deSJonathan Lemon 
3058340c35deSJonathan Lemon #if 0
3059340c35deSJonathan Lemon /* PAWS not needed at the moment */
3060340c35deSJonathan Lemon 	/*
3061340c35deSJonathan Lemon 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
3062340c35deSJonathan Lemon 	 * and it's less than ts_recent, drop it.
3063340c35deSJonathan Lemon 	 */
3064340c35deSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
3065340c35deSJonathan Lemon 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
3066340c35deSJonathan Lemon 		if ((thflags & TH_ACK) == 0)
3067340c35deSJonathan Lemon 			goto drop;
3068340c35deSJonathan Lemon 		goto ack;
3069340c35deSJonathan Lemon 	}
3070340c35deSJonathan Lemon 	/*
3071340c35deSJonathan Lemon 	 * ts_recent is never updated because we never accept new segments.
3072340c35deSJonathan Lemon 	 */
3073340c35deSJonathan Lemon #endif
3074340c35deSJonathan Lemon 
3075340c35deSJonathan Lemon 	/*
3076340c35deSJonathan Lemon 	 * If a new connection request is received
3077340c35deSJonathan Lemon 	 * while in TIME_WAIT, drop the old connection
3078340c35deSJonathan Lemon 	 * and start over if the sequence numbers
3079340c35deSJonathan Lemon 	 * are above the previous ones.
3080340c35deSJonathan Lemon 	 */
3081340c35deSJonathan Lemon 	if ((thflags & TH_SYN) && SEQ_GT(th->th_seq, tw->rcv_nxt)) {
3082607b0b0cSJonathan Lemon 		(void) tcp_twclose(tw, 0);
3083340c35deSJonathan Lemon 		return (1);
3084340c35deSJonathan Lemon 	}
3085340c35deSJonathan Lemon 
3086340c35deSJonathan Lemon 	/*
3087340c35deSJonathan Lemon 	 * Drop the the segment if it does not contain an ACK.
3088340c35deSJonathan Lemon 	 */
3089340c35deSJonathan Lemon 	if ((thflags & TH_ACK) == 0)
3090340c35deSJonathan Lemon 		goto drop;
3091340c35deSJonathan Lemon 
3092340c35deSJonathan Lemon 	/*
3093340c35deSJonathan Lemon 	 * Reset the 2MSL timer if this is a duplicate FIN.
3094340c35deSJonathan Lemon 	 */
3095340c35deSJonathan Lemon 	if (thflags & TH_FIN) {
3096340c35deSJonathan Lemon 		seq = th->th_seq + tlen + (thflags & TH_SYN ? 1 : 0);
3097340c35deSJonathan Lemon 		if (seq + 1 == tw->rcv_nxt)
3098607b0b0cSJonathan Lemon 			tcp_timer_2msl_reset(tw, 2 * tcp_msl);
3099340c35deSJonathan Lemon 	}
3100340c35deSJonathan Lemon 
3101340c35deSJonathan Lemon 	/*
3102272c5dfeSJonathan Lemon 	 * Acknowledge the segment if it has data or is not a duplicate ACK.
3103340c35deSJonathan Lemon 	 */
3104272c5dfeSJonathan Lemon 	if (thflags != TH_ACK || tlen != 0 ||
3105272c5dfeSJonathan Lemon 	    th->th_seq != tw->rcv_nxt || th->th_ack != tw->snd_nxt)
3106430c6354SRobert Watson 		tcp_twrespond(tw, NULL, m, TH_ACK);
3107340c35deSJonathan Lemon 	goto drop;
3108340c35deSJonathan Lemon 
3109340c35deSJonathan Lemon reset:
3110340c35deSJonathan Lemon 	/*
3111340c35deSJonathan Lemon 	 * Generate a RST, dropping incoming segment.
3112340c35deSJonathan Lemon 	 * Make ACK acceptable to originator of segment.
3113340c35deSJonathan Lemon 	 * Don't bother to respond if destination was broadcast/multicast.
3114340c35deSJonathan Lemon 	 */
3115340c35deSJonathan Lemon 	if (m->m_flags & (M_BCAST|M_MCAST))
3116340c35deSJonathan Lemon 		goto drop;
3117340c35deSJonathan Lemon 	if (isipv6) {
3118340c35deSJonathan Lemon 		struct ip6_hdr *ip6;
3119340c35deSJonathan Lemon 
3120340c35deSJonathan Lemon 		/* IPv6 anycast check is done at tcp6_input() */
3121340c35deSJonathan Lemon 		ip6 = mtod(m, struct ip6_hdr *);
3122340c35deSJonathan Lemon 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
3123340c35deSJonathan Lemon 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
3124340c35deSJonathan Lemon 			goto drop;
3125340c35deSJonathan Lemon 	} else {
3126340c35deSJonathan Lemon 		struct ip *ip;
3127340c35deSJonathan Lemon 
3128340c35deSJonathan Lemon 		ip = mtod(m, struct ip *);
3129340c35deSJonathan Lemon 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
3130340c35deSJonathan Lemon 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
3131340c35deSJonathan Lemon 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
3132340c35deSJonathan Lemon 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
3133340c35deSJonathan Lemon 			goto drop;
3134340c35deSJonathan Lemon 	}
3135340c35deSJonathan Lemon 	if (thflags & TH_ACK) {
3136340c35deSJonathan Lemon 		tcp_respond(NULL,
3137340c35deSJonathan Lemon 		    mtod(m, void *), th, m, 0, th->th_ack, TH_RST);
3138340c35deSJonathan Lemon 	} else {
3139340c35deSJonathan Lemon 		seq = th->th_seq + (thflags & TH_SYN ? 1 : 0);
3140340c35deSJonathan Lemon 		tcp_respond(NULL,
3141340c35deSJonathan Lemon 		    mtod(m, void *), th, m, seq, 0, TH_RST|TH_ACK);
3142340c35deSJonathan Lemon 	}
3143340c35deSJonathan Lemon 	INP_UNLOCK(tw->tw_inpcb);
3144340c35deSJonathan Lemon 	return (0);
3145340c35deSJonathan Lemon 
3146340c35deSJonathan Lemon drop:
3147340c35deSJonathan Lemon 	INP_UNLOCK(tw->tw_inpcb);
3148340c35deSJonathan Lemon 	m_freem(m);
3149340c35deSJonathan Lemon 	return (0);
3150340c35deSJonathan Lemon }
3151