xref: /freebsd/sys/netinet/tcp_input.c (revision 88c39af35f768256d6eb461bfd1a2272fb8b064e)
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"
400cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
41e46cd3d4SDag-Erling Smørgrav #include "opt_tcp_input.h"
420cc12cc5SJoerg Wunsch 
43df8bae1dSRodney W. Grimes #include <sys/param.h>
4498163b98SPoul-Henning Kamp #include <sys/kernel.h>
45df8bae1dSRodney W. Grimes #include <sys/malloc.h>
46df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
47a29f300eSGarrett Wollman #include <sys/proc.h>		/* for proc0 declaration */
48df8bae1dSRodney W. Grimes #include <sys/protosw.h>
49960ed29cSSeigo Tanimura #include <sys/signalvar.h>
50df8bae1dSRodney W. Grimes #include <sys/socket.h>
51df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
52960ed29cSSeigo Tanimura #include <sys/sysctl.h>
53816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
54960ed29cSSeigo Tanimura #include <sys/systm.h>
55df8bae1dSRodney W. Grimes 
56e79adb8eSGarrett Wollman #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
57e79adb8eSGarrett Wollman 
58df8bae1dSRodney W. Grimes #include <net/if.h>
59df8bae1dSRodney W. Grimes #include <net/route.h>
60df8bae1dSRodney W. Grimes 
61df8bae1dSRodney W. Grimes #include <netinet/in.h>
62960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
63df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
64960ed29cSSeigo Tanimura #include <netinet/in_var.h>
65df8bae1dSRodney W. Grimes #include <netinet/ip.h>
66686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_icmp.h>	/* for ICMP_BANDLIM		*/
67686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM		*/
68df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
69fb59c426SYoshinobu Inoue #ifdef INET6
70686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
71686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
72686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
73960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h>
74960ed29cSSeigo Tanimura #include <netinet6/nd6.h>
75fb59c426SYoshinobu Inoue #endif
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 #ifdef INET6
82fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h>
83fb59c426SYoshinobu Inoue #endif
84df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
85610ee2f9SDavid Greenman #ifdef TCPDEBUG
86df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
87fb59c426SYoshinobu Inoue 
88fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */
89fb59c426SYoshinobu Inoue 
90fb59c426SYoshinobu Inoue #ifdef IPSEC
91fb59c426SYoshinobu Inoue #include <netinet6/ipsec.h>
923a2a9f79SYoshinobu Inoue #ifdef INET6
933a2a9f79SYoshinobu Inoue #include <netinet6/ipsec6.h>
943a2a9f79SYoshinobu Inoue #endif
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 
1020312fbe9SPoul-Henning Kamp static 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 
12815bd2b43SDavid Greenman struct inpcbhead tcb;
129fb59c426SYoshinobu Inoue #define	tcb6	tcb  /* for KAME src sync over BSD*'s */
13015bd2b43SDavid Greenman struct inpcbinfo tcbinfo;
131f76fcf6dSJeffrey Hsu struct mtx	*tcbinfo_mtx;
132df8bae1dSRodney W. Grimes 
1334d77a549SAlfred Perlstein static void	 tcp_dooptions(struct tcpopt *, u_char *, int, int);
1344d77a549SAlfred Perlstein static void	 tcp_pulloutofband(struct socket *,
1354d77a549SAlfred Perlstein 		     struct tcphdr *, struct mbuf *, int);
1364d77a549SAlfred Perlstein static int	 tcp_reass(struct tcpcb *, struct tcphdr *, int *,
1374d77a549SAlfred Perlstein 		     struct mbuf *);
1384d77a549SAlfred Perlstein static void	 tcp_xmit_timer(struct tcpcb *, int);
1394d77a549SAlfred Perlstein static int	 tcp_newreno(struct tcpcb *, struct tcphdr *);
1400312fbe9SPoul-Henning Kamp 
141fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */
142fb59c426SYoshinobu Inoue #ifdef INET6
143fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \
144fb59c426SYoshinobu Inoue do { \
145fb59c426SYoshinobu Inoue 	if ((tp) && (tp)->t_inpcb && \
146fb59c426SYoshinobu Inoue 	    ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0 && \
147fb59c426SYoshinobu Inoue 	    (tp)->t_inpcb->in6p_route.ro_rt) \
148686cdd19SJun-ichiro itojun Hagino 		nd6_nud_hint((tp)->t_inpcb->in6p_route.ro_rt, NULL, 0); \
149fb59c426SYoshinobu Inoue } while (0)
150fb59c426SYoshinobu Inoue #else
151fb59c426SYoshinobu Inoue #define ND6_HINT(tp)
152fb59c426SYoshinobu Inoue #endif
153df8bae1dSRodney W. Grimes 
154df8bae1dSRodney W. Grimes /*
155262c1c1aSMatthew Dillon  * Indicate whether this ack should be delayed.  We can delay the ack if
156262c1c1aSMatthew Dillon  *	- delayed acks are enabled and
157262c1c1aSMatthew Dillon  *	- there is no delayed ack timer in progress and
158262c1c1aSMatthew Dillon  *	- our last ack wasn't a 0-sized window.  We never want to delay
159262c1c1aSMatthew Dillon  *	  the ack that opens up a 0-sized window.
160d8c85a26SJonathan Lemon  */
161d8c85a26SJonathan Lemon #define DELAY_ACK(tp) \
162262c1c1aSMatthew Dillon 	(tcp_delack_enabled && !callout_pending(tp->tt_delack) && \
163262c1c1aSMatthew Dillon 	(tp->t_flags & TF_RXWIN0SENT) == 0)
164d8c85a26SJonathan Lemon 
1650312fbe9SPoul-Henning Kamp static int
166fb59c426SYoshinobu Inoue tcp_reass(tp, th, tlenp, m)
167df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
168fb59c426SYoshinobu Inoue 	register struct tcphdr *th;
169fb59c426SYoshinobu Inoue 	int *tlenp;
170df8bae1dSRodney W. Grimes 	struct mbuf *m;
171df8bae1dSRodney W. Grimes {
172fb59c426SYoshinobu Inoue 	struct tseg_qent *q;
173fb59c426SYoshinobu Inoue 	struct tseg_qent *p = NULL;
174fb59c426SYoshinobu Inoue 	struct tseg_qent *nq;
175fb59c426SYoshinobu Inoue 	struct tseg_qent *te;
176df8bae1dSRodney W. Grimes 	struct socket *so = tp->t_inpcb->inp_socket;
177df8bae1dSRodney W. Grimes 	int flags;
178df8bae1dSRodney W. Grimes 
179df8bae1dSRodney W. Grimes 	/*
180fb59c426SYoshinobu Inoue 	 * Call with th==0 after become established to
181df8bae1dSRodney W. Grimes 	 * force pre-ESTABLISHED data up to user socket.
182df8bae1dSRodney W. Grimes 	 */
183fb59c426SYoshinobu Inoue 	if (th == 0)
184df8bae1dSRodney W. Grimes 		goto present;
185df8bae1dSRodney W. Grimes 
186fb59c426SYoshinobu Inoue 	/* Allocate a new queue entry. If we can't, just drop the pkt. XXX */
187fb59c426SYoshinobu Inoue 	MALLOC(te, struct tseg_qent *, sizeof (struct tseg_qent), M_TSEGQ,
188fb59c426SYoshinobu Inoue 	       M_NOWAIT);
189fb59c426SYoshinobu Inoue 	if (te == NULL) {
190fb59c426SYoshinobu Inoue 		tcpstat.tcps_rcvmemdrop++;
191fb59c426SYoshinobu Inoue 		m_freem(m);
192fb59c426SYoshinobu Inoue 		return (0);
193fb59c426SYoshinobu Inoue 	}
1946effc713SDoug Rabson 
195df8bae1dSRodney W. Grimes 	/*
196df8bae1dSRodney W. Grimes 	 * Find a segment which begins after this one does.
197df8bae1dSRodney W. Grimes 	 */
198fb59c426SYoshinobu Inoue 	LIST_FOREACH(q, &tp->t_segq, tqe_q) {
199fb59c426SYoshinobu Inoue 		if (SEQ_GT(q->tqe_th->th_seq, th->th_seq))
200df8bae1dSRodney W. Grimes 			break;
201fb59c426SYoshinobu Inoue 		p = q;
202fb59c426SYoshinobu Inoue 	}
203df8bae1dSRodney W. Grimes 
204df8bae1dSRodney W. Grimes 	/*
205df8bae1dSRodney W. Grimes 	 * If there is a preceding segment, it may provide some of
206df8bae1dSRodney W. Grimes 	 * our data already.  If so, drop the data from the incoming
207df8bae1dSRodney W. Grimes 	 * segment.  If it provides all of our data, drop us.
208df8bae1dSRodney W. Grimes 	 */
2096effc713SDoug Rabson 	if (p != NULL) {
210df8bae1dSRodney W. Grimes 		register int i;
211df8bae1dSRodney W. Grimes 		/* conversion to int (in i) handles seq wraparound */
212fb59c426SYoshinobu Inoue 		i = p->tqe_th->th_seq + p->tqe_len - th->th_seq;
213df8bae1dSRodney W. Grimes 		if (i > 0) {
214fb59c426SYoshinobu Inoue 			if (i >= *tlenp) {
215df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvduppack++;
216fb59c426SYoshinobu Inoue 				tcpstat.tcps_rcvdupbyte += *tlenp;
217df8bae1dSRodney W. Grimes 				m_freem(m);
218fb59c426SYoshinobu Inoue 				FREE(te, M_TSEGQ);
219a0292f23SGarrett Wollman 				/*
220a0292f23SGarrett Wollman 				 * Try to present any queued data
221a0292f23SGarrett Wollman 				 * at the left window edge to the user.
222a0292f23SGarrett Wollman 				 * This is needed after the 3-WHS
223a0292f23SGarrett Wollman 				 * completes.
224a0292f23SGarrett Wollman 				 */
225a0292f23SGarrett Wollman 				goto present;	/* ??? */
226df8bae1dSRodney W. Grimes 			}
227df8bae1dSRodney W. Grimes 			m_adj(m, i);
228fb59c426SYoshinobu Inoue 			*tlenp -= i;
229fb59c426SYoshinobu Inoue 			th->th_seq += i;
230df8bae1dSRodney W. Grimes 		}
231df8bae1dSRodney W. Grimes 	}
232df8bae1dSRodney W. Grimes 	tcpstat.tcps_rcvoopack++;
233fb59c426SYoshinobu Inoue 	tcpstat.tcps_rcvoobyte += *tlenp;
234df8bae1dSRodney W. Grimes 
235df8bae1dSRodney W. Grimes 	/*
236df8bae1dSRodney W. Grimes 	 * While we overlap succeeding segments trim them or,
237df8bae1dSRodney W. Grimes 	 * if they are completely covered, dequeue them.
238df8bae1dSRodney W. Grimes 	 */
2396effc713SDoug Rabson 	while (q) {
240fb59c426SYoshinobu Inoue 		register int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq;
241df8bae1dSRodney W. Grimes 		if (i <= 0)
242df8bae1dSRodney W. Grimes 			break;
243fb59c426SYoshinobu Inoue 		if (i < q->tqe_len) {
244fb59c426SYoshinobu Inoue 			q->tqe_th->th_seq += i;
245fb59c426SYoshinobu Inoue 			q->tqe_len -= i;
246fb59c426SYoshinobu Inoue 			m_adj(q->tqe_m, i);
247df8bae1dSRodney W. Grimes 			break;
248df8bae1dSRodney W. Grimes 		}
2496effc713SDoug Rabson 
250fb59c426SYoshinobu Inoue 		nq = LIST_NEXT(q, tqe_q);
251fb59c426SYoshinobu Inoue 		LIST_REMOVE(q, tqe_q);
252fb59c426SYoshinobu Inoue 		m_freem(q->tqe_m);
253fb59c426SYoshinobu Inoue 		FREE(q, M_TSEGQ);
2546effc713SDoug Rabson 		q = nq;
255df8bae1dSRodney W. Grimes 	}
256df8bae1dSRodney W. Grimes 
257fb59c426SYoshinobu Inoue 	/* Insert the new segment queue entry into place. */
258fb59c426SYoshinobu Inoue 	te->tqe_m = m;
259fb59c426SYoshinobu Inoue 	te->tqe_th = th;
260fb59c426SYoshinobu Inoue 	te->tqe_len = *tlenp;
261fb59c426SYoshinobu Inoue 
2626effc713SDoug Rabson 	if (p == NULL) {
263fb59c426SYoshinobu Inoue 		LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q);
2646effc713SDoug Rabson 	} else {
265fb59c426SYoshinobu Inoue 		LIST_INSERT_AFTER(p, te, tqe_q);
2666effc713SDoug Rabson 	}
267df8bae1dSRodney W. Grimes 
268df8bae1dSRodney W. Grimes present:
269df8bae1dSRodney W. Grimes 	/*
270df8bae1dSRodney W. Grimes 	 * Present data to user, advancing rcv_nxt through
271df8bae1dSRodney W. Grimes 	 * completed sequence space.
272df8bae1dSRodney W. Grimes 	 */
273a0292f23SGarrett Wollman 	if (!TCPS_HAVEESTABLISHED(tp->t_state))
274df8bae1dSRodney W. Grimes 		return (0);
275fb59c426SYoshinobu Inoue 	q = LIST_FIRST(&tp->t_segq);
276fb59c426SYoshinobu Inoue 	if (!q || q->tqe_th->th_seq != tp->rcv_nxt)
277df8bae1dSRodney W. Grimes 		return (0);
278df8bae1dSRodney W. Grimes 	do {
279fb59c426SYoshinobu Inoue 		tp->rcv_nxt += q->tqe_len;
280fb59c426SYoshinobu Inoue 		flags = q->tqe_th->th_flags & TH_FIN;
281fb59c426SYoshinobu Inoue 		nq = LIST_NEXT(q, tqe_q);
282fb59c426SYoshinobu Inoue 		LIST_REMOVE(q, tqe_q);
2834cc20ab1SSeigo Tanimura 		if (so->so_state & SS_CANTRCVMORE)
284fb59c426SYoshinobu Inoue 			m_freem(q->tqe_m);
2854cc20ab1SSeigo Tanimura 		else
286fb59c426SYoshinobu Inoue 			sbappend(&so->so_rcv, q->tqe_m);
287fb59c426SYoshinobu Inoue 		FREE(q, M_TSEGQ);
2886effc713SDoug Rabson 		q = nq;
289fb59c426SYoshinobu Inoue 	} while (q && q->tqe_th->th_seq == tp->rcv_nxt);
290fb59c426SYoshinobu Inoue 	ND6_HINT(tp);
291df8bae1dSRodney W. Grimes 	sorwakeup(so);
292df8bae1dSRodney W. Grimes 	return (flags);
293df8bae1dSRodney W. Grimes }
294df8bae1dSRodney W. Grimes 
295df8bae1dSRodney W. Grimes /*
296df8bae1dSRodney W. Grimes  * TCP input routine, follows pages 65-76 of the
297df8bae1dSRodney W. Grimes  * protocol specification dated September, 1981 very closely.
298df8bae1dSRodney W. Grimes  */
299fb59c426SYoshinobu Inoue #ifdef INET6
300fb59c426SYoshinobu Inoue int
301fb59c426SYoshinobu Inoue tcp6_input(mp, offp, proto)
302fb59c426SYoshinobu Inoue 	struct mbuf **mp;
303fb59c426SYoshinobu Inoue 	int *offp, proto;
304fb59c426SYoshinobu Inoue {
305fb59c426SYoshinobu Inoue 	register struct mbuf *m = *mp;
30633841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6;
307fb59c426SYoshinobu Inoue 
308fb59c426SYoshinobu Inoue 	IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE);
309fb59c426SYoshinobu Inoue 
310fb59c426SYoshinobu Inoue 	/*
311fb59c426SYoshinobu Inoue 	 * draft-itojun-ipv6-tcp-to-anycast
312fb59c426SYoshinobu Inoue 	 * better place to put this in?
313fb59c426SYoshinobu Inoue 	 */
31433841545SHajimu UMEMOTO 	ia6 = ip6_getdstifaddr(m);
31533841545SHajimu UMEMOTO 	if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
316fb59c426SYoshinobu Inoue 		struct ip6_hdr *ip6;
317fb59c426SYoshinobu Inoue 
318fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
319fb59c426SYoshinobu Inoue 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
320fb59c426SYoshinobu Inoue 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
321fb59c426SYoshinobu Inoue 		return IPPROTO_DONE;
322fb59c426SYoshinobu Inoue 	}
323fb59c426SYoshinobu Inoue 
324f0ffb944SJulian Elischer 	tcp_input(m, *offp);
325fb59c426SYoshinobu Inoue 	return IPPROTO_DONE;
326fb59c426SYoshinobu Inoue }
327fb59c426SYoshinobu Inoue #endif
328fb59c426SYoshinobu Inoue 
329df8bae1dSRodney W. Grimes void
330f0ffb944SJulian Elischer tcp_input(m, off0)
331df8bae1dSRodney W. Grimes 	register struct mbuf *m;
332f0ffb944SJulian Elischer 	int off0;
333df8bae1dSRodney W. Grimes {
334fb59c426SYoshinobu Inoue 	register struct tcphdr *th;
335fb59c426SYoshinobu Inoue 	register struct ip *ip = NULL;
336fb59c426SYoshinobu Inoue 	register struct ipovly *ipov;
337f76fcf6dSJeffrey Hsu 	register struct inpcb *inp = NULL;
338e79adb8eSGarrett Wollman 	u_char *optp = NULL;
33926f9a767SRodney W. Grimes 	int optlen = 0;
340df8bae1dSRodney W. Grimes 	int len, tlen, off;
341fb59c426SYoshinobu Inoue 	int drop_hdrlen;
342df8bae1dSRodney W. Grimes 	register struct tcpcb *tp = 0;
343fb59c426SYoshinobu Inoue 	register int thflags;
34426f9a767SRodney W. Grimes 	struct socket *so = 0;
345df8bae1dSRodney W. Grimes 	int todrop, acked, ourfinisacked, needoutput = 0;
346a0292f23SGarrett Wollman 	u_long tiwin;
347a0292f23SGarrett Wollman 	struct tcpopt to;		/* options in this segment */
348a0292f23SGarrett Wollman 	struct rmxp_tao *taop;		/* pointer to our TAO cache entry */
349a0292f23SGarrett Wollman 	struct rmxp_tao	tao_noncached;	/* in case there's no cached entry */
350f76fcf6dSJeffrey Hsu 	int headlocked = 0;
351f76fcf6dSJeffrey Hsu 
352610ee2f9SDavid Greenman #ifdef TCPDEBUG
353410bb1bfSLuigi Rizzo 	u_char tcp_saveipgen[40];
354410bb1bfSLuigi Rizzo 		/* the size of the above must be of max ip header, now IPv6 */
355410bb1bfSLuigi Rizzo 	struct tcphdr tcp_savetcp;
356610ee2f9SDavid Greenman 	short ostate = 0;
357610ee2f9SDavid Greenman #endif
358fb59c426SYoshinobu Inoue #ifdef INET6
359fb59c426SYoshinobu Inoue 	struct ip6_hdr *ip6 = NULL;
360fb59c426SYoshinobu Inoue 	int isipv6;
361fb59c426SYoshinobu Inoue #endif /* INET6 */
3622b25acc1SLuigi Rizzo 	struct sockaddr_in *next_hop = NULL;
363a57815efSBosko Milekic 	int rstreason; /* For badport_bandlim accounting purposes */
364df8bae1dSRodney W. Grimes 
3652b25acc1SLuigi Rizzo 	/* Grab info from MT_TAG mbufs prepended to the chain. */
3662b25acc1SLuigi Rizzo 	for (;m && m->m_type == MT_TAG; m = m->m_next) {
3672b25acc1SLuigi Rizzo 		if (m->m_tag_id == PACKET_TAG_IPFORWARD)
3682b25acc1SLuigi Rizzo 			next_hop = (struct sockaddr_in *)m->m_hdr.mh_data;
3692b25acc1SLuigi Rizzo 	}
3702b25acc1SLuigi Rizzo 
371fb59c426SYoshinobu Inoue #ifdef INET6
372fb59c426SYoshinobu Inoue 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
373fb59c426SYoshinobu Inoue #endif
374a0292f23SGarrett Wollman 	bzero((char *)&to, sizeof(to));
375a0292f23SGarrett Wollman 
376df8bae1dSRodney W. Grimes 	tcpstat.tcps_rcvtotal++;
377fb59c426SYoshinobu Inoue 
378fb59c426SYoshinobu Inoue #ifdef INET6
379fb59c426SYoshinobu Inoue 	if (isipv6) {
380fb59c426SYoshinobu Inoue 		/* IP6_EXTHDR_CHECK() is already done at tcp6_input() */
381fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
382fb59c426SYoshinobu Inoue 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
383fb59c426SYoshinobu Inoue 		if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) {
384fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbadsum++;
385fb59c426SYoshinobu Inoue 			goto drop;
386fb59c426SYoshinobu Inoue 		}
387fb59c426SYoshinobu Inoue 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
38833841545SHajimu UMEMOTO 
38933841545SHajimu UMEMOTO 		/*
39033841545SHajimu UMEMOTO 		 * Be proactive about unspecified IPv6 address in source.
39133841545SHajimu UMEMOTO 		 * As we use all-zero to indicate unbounded/unconnected pcb,
39233841545SHajimu UMEMOTO 		 * unspecified IPv6 address can be used to confuse us.
39333841545SHajimu UMEMOTO 		 *
39433841545SHajimu UMEMOTO 		 * Note that packets with unspecified IPv6 destination is
39533841545SHajimu UMEMOTO 		 * already dropped in ip6_input.
39633841545SHajimu UMEMOTO 		 */
39733841545SHajimu UMEMOTO 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
39833841545SHajimu UMEMOTO 			/* XXX stat */
39933841545SHajimu UMEMOTO 			goto drop;
40033841545SHajimu UMEMOTO 		}
401fb59c426SYoshinobu Inoue 	} else
402fb59c426SYoshinobu Inoue #endif /* INET6 */
403fb59c426SYoshinobu Inoue       {
404df8bae1dSRodney W. Grimes 	/*
405df8bae1dSRodney W. Grimes 	 * Get IP and TCP header together in first mbuf.
406df8bae1dSRodney W. Grimes 	 * Note: IP leaves IP header in first mbuf.
407df8bae1dSRodney W. Grimes 	 */
408fb59c426SYoshinobu Inoue 	if (off0 > sizeof (struct ip)) {
409df8bae1dSRodney W. Grimes 		ip_stripoptions(m, (struct mbuf *)0);
410fb59c426SYoshinobu Inoue 		off0 = sizeof(struct ip);
411fb59c426SYoshinobu Inoue 	}
412df8bae1dSRodney W. Grimes 	if (m->m_len < sizeof (struct tcpiphdr)) {
413df8bae1dSRodney W. Grimes 		if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
414df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvshort++;
415df8bae1dSRodney W. Grimes 			return;
416df8bae1dSRodney W. Grimes 		}
417df8bae1dSRodney W. Grimes 	}
418fb59c426SYoshinobu Inoue 	ip = mtod(m, struct ip *);
419fb59c426SYoshinobu Inoue 	ipov = (struct ipovly *)ip;
420db4f9cc7SJonathan Lemon 	th = (struct tcphdr *)((caddr_t)ip + off0);
421db4f9cc7SJonathan Lemon 	tlen = ip->ip_len;
422df8bae1dSRodney W. Grimes 
423db4f9cc7SJonathan Lemon 	if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
424db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
425db4f9cc7SJonathan Lemon                 	th->th_sum = m->m_pkthdr.csum_data;
426db4f9cc7SJonathan Lemon 		else
427db4f9cc7SJonathan Lemon 	                th->th_sum = in_pseudo(ip->ip_src.s_addr,
428db4f9cc7SJonathan Lemon 			    ip->ip_dst.s_addr, htonl(m->m_pkthdr.csum_data +
429db4f9cc7SJonathan Lemon 			    ip->ip_len + IPPROTO_TCP));
430db4f9cc7SJonathan Lemon 		th->th_sum ^= 0xffff;
431db4f9cc7SJonathan Lemon 	} else {
432df8bae1dSRodney W. Grimes 		/*
433df8bae1dSRodney W. Grimes 		 * Checksum extended TCP header and data.
434df8bae1dSRodney W. Grimes 		 */
435df8bae1dSRodney W. Grimes 		len = sizeof (struct ip) + tlen;
436fb59c426SYoshinobu Inoue 		bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
437fb59c426SYoshinobu Inoue 		ipov->ih_len = (u_short)tlen;
438fd8e4ebcSMike Barcroft 		ipov->ih_len = htons(ipov->ih_len);
439fb59c426SYoshinobu Inoue 		th->th_sum = in_cksum(m, len);
440db4f9cc7SJonathan Lemon 	}
441fb59c426SYoshinobu Inoue 	if (th->th_sum) {
442df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvbadsum++;
443df8bae1dSRodney W. Grimes 		goto drop;
444df8bae1dSRodney W. Grimes 	}
445fb59c426SYoshinobu Inoue #ifdef INET6
446fb59c426SYoshinobu Inoue 	/* Re-initialization for later version check */
447fb59c426SYoshinobu Inoue 	ip->ip_v = IPVERSION;
448fb59c426SYoshinobu Inoue #endif
449fb59c426SYoshinobu Inoue       }
450df8bae1dSRodney W. Grimes 
451df8bae1dSRodney W. Grimes 	/*
452df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
453df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
454df8bae1dSRodney W. Grimes 	 */
455fb59c426SYoshinobu Inoue 	off = th->th_off << 2;
456df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
457df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvbadoff++;
458df8bae1dSRodney W. Grimes 		goto drop;
459df8bae1dSRodney W. Grimes 	}
460fb59c426SYoshinobu Inoue 	tlen -= off;	/* tlen is used instead of ti->ti_len */
461df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
462fb59c426SYoshinobu Inoue #ifdef INET6
463fb59c426SYoshinobu Inoue 		if (isipv6) {
464fb59c426SYoshinobu Inoue 			IP6_EXTHDR_CHECK(m, off0, off, );
465fb59c426SYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
466fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
467fb59c426SYoshinobu Inoue 		} else
468fb59c426SYoshinobu Inoue #endif /* INET6 */
469fb59c426SYoshinobu Inoue 	      {
470df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof(struct ip) + off) {
471df8bae1dSRodney W. Grimes 			if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
472df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvshort++;
473df8bae1dSRodney W. Grimes 				return;
474df8bae1dSRodney W. Grimes 			}
475fb59c426SYoshinobu Inoue 			ip = mtod(m, struct ip *);
476fb59c426SYoshinobu Inoue 			ipov = (struct ipovly *)ip;
477fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip + off0);
478fb59c426SYoshinobu Inoue 		}
479df8bae1dSRodney W. Grimes 	      }
480df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
481fb59c426SYoshinobu Inoue 		optp = (u_char *)(th + 1);
482df8bae1dSRodney W. Grimes 	}
483fb59c426SYoshinobu Inoue 	thflags = th->th_flags;
484df8bae1dSRodney W. Grimes 
485e46cd3d4SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN
486e46cd3d4SDag-Erling Smørgrav 	/*
487e46cd3d4SDag-Erling Smørgrav 	 * If the drop_synfin option is enabled, drop all packets with
488e46cd3d4SDag-Erling Smørgrav 	 * both the SYN and FIN bits set. This prevents e.g. nmap from
489e46cd3d4SDag-Erling Smørgrav 	 * identifying the TCP/IP stack.
490e46cd3d4SDag-Erling Smørgrav 	 *
491a589a70eSGarrett Wollman 	 * This is a violation of the TCP specification.
492e46cd3d4SDag-Erling Smørgrav 	 */
493fb59c426SYoshinobu Inoue 	if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN))
494e46cd3d4SDag-Erling Smørgrav 		goto drop;
495e46cd3d4SDag-Erling Smørgrav #endif
496e46cd3d4SDag-Erling Smørgrav 
497df8bae1dSRodney W. Grimes 	/*
498df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
499df8bae1dSRodney W. Grimes 	 */
500fd8e4ebcSMike Barcroft 	th->th_seq = ntohl(th->th_seq);
501fd8e4ebcSMike Barcroft 	th->th_ack = ntohl(th->th_ack);
502fd8e4ebcSMike Barcroft 	th->th_win = ntohs(th->th_win);
503fd8e4ebcSMike Barcroft 	th->th_urp = ntohs(th->th_urp);
504df8bae1dSRodney W. Grimes 
505df8bae1dSRodney W. Grimes 	/*
506fb59c426SYoshinobu Inoue 	 * Delay droping TCP, IP headers, IPv6 ext headers, and TCP options,
507fb59c426SYoshinobu Inoue 	 * until after ip6_savecontrol() is called and before other functions
508fb59c426SYoshinobu Inoue 	 * which don't want those proto headers.
509fb59c426SYoshinobu Inoue 	 * Because ip6_savecontrol() is going to parse the mbuf to
510fb59c426SYoshinobu Inoue 	 * search for data to be passed up to user-land, it wants mbuf
511fb59c426SYoshinobu Inoue 	 * parameters to be unchanged.
51288ff5695SSUZUKI Shinsuke 	 * XXX: the call of ip6_savecontrol() has been obsoleted based on
51388ff5695SSUZUKI Shinsuke 	 * latest version of the advanced API (20020110).
514755c1f07SAndras Olah 	 */
515fb59c426SYoshinobu Inoue 	drop_hdrlen = off0 + off;
516755c1f07SAndras Olah 
517755c1f07SAndras Olah 	/*
518df8bae1dSRodney W. Grimes 	 * Locate pcb for segment.
519df8bae1dSRodney W. Grimes 	 */
520f76fcf6dSJeffrey Hsu 	 INP_INFO_WLOCK(&tcbinfo);
521f76fcf6dSJeffrey Hsu 	 headlocked = 1;
522df8bae1dSRodney W. Grimes findpcb:
5232b25acc1SLuigi Rizzo 	/* IPFIREWALL_FORWARD section */
5242b25acc1SLuigi Rizzo 	if (next_hop != NULL
525fb59c426SYoshinobu Inoue #ifdef INET6
526fb59c426SYoshinobu Inoue 	    && isipv6 == NULL /* IPv6 support is not yet */
527fb59c426SYoshinobu Inoue #endif /* INET6 */
528fb59c426SYoshinobu Inoue 	    ) {
529f9e354dfSJulian Elischer 		/*
5302b25acc1SLuigi Rizzo 		 * Transparently forwarded. Pretend to be the destination.
531f9e354dfSJulian Elischer 		 * already got one like this?
532f9e354dfSJulian Elischer 		 */
533fb59c426SYoshinobu Inoue 		inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport,
534fb59c426SYoshinobu Inoue 			ip->ip_dst, th->th_dport, 0, m->m_pkthdr.rcvif);
535f9e354dfSJulian Elischer 		if (!inp) {
536f9e354dfSJulian Elischer 			/*
537f9e354dfSJulian Elischer 			 * No, then it's new. Try find the ambushing socket
538f9e354dfSJulian Elischer 			 */
5392b25acc1SLuigi Rizzo 			if (next_hop->sin_port == 0) {
540fb59c426SYoshinobu Inoue 				inp = in_pcblookup_hash(&tcbinfo, ip->ip_src,
5412b25acc1SLuigi Rizzo 				    th->th_sport, next_hop->sin_addr,
542fb59c426SYoshinobu Inoue 				    th->th_dport, 1, m->m_pkthdr.rcvif);
543f9e354dfSJulian Elischer 			} else {
544f9e354dfSJulian Elischer 				inp = in_pcblookup_hash(&tcbinfo,
545fb59c426SYoshinobu Inoue 				    ip->ip_src, th->th_sport,
5462b25acc1SLuigi Rizzo 	    			    next_hop->sin_addr,
5472b25acc1SLuigi Rizzo 				    ntohs(next_hop->sin_port), 1,
548cfa1ca9dSYoshinobu Inoue 				    m->m_pkthdr.rcvif);
549f9e354dfSJulian Elischer 			}
550f9e354dfSJulian Elischer 		}
551f9e354dfSJulian Elischer 	} else
552fb59c426SYoshinobu Inoue       {
553fb59c426SYoshinobu Inoue #ifdef INET6
554fb59c426SYoshinobu Inoue 	if (isipv6)
555fb59c426SYoshinobu Inoue 		inp = in6_pcblookup_hash(&tcbinfo, &ip6->ip6_src, th->th_sport,
556fb59c426SYoshinobu Inoue 					 &ip6->ip6_dst, th->th_dport, 1,
557fb59c426SYoshinobu Inoue 					 m->m_pkthdr.rcvif);
558fb59c426SYoshinobu Inoue 	else
559fb59c426SYoshinobu Inoue #endif /* INET6 */
560fb59c426SYoshinobu Inoue 	inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport,
561fb59c426SYoshinobu Inoue 	    ip->ip_dst, th->th_dport, 1, m->m_pkthdr.rcvif);
562fb59c426SYoshinobu Inoue       }
563f9e354dfSJulian Elischer 
564fb59c426SYoshinobu Inoue #ifdef IPSEC
565fb59c426SYoshinobu Inoue #ifdef INET6
566fb59c426SYoshinobu Inoue 	if (isipv6) {
567fb59c426SYoshinobu Inoue 		if (inp != NULL && ipsec6_in_reject_so(m, inp->inp_socket)) {
568fb59c426SYoshinobu Inoue 			ipsec6stat.in_polvio++;
569fb59c426SYoshinobu Inoue 			goto drop;
570fb59c426SYoshinobu Inoue 		}
571fb59c426SYoshinobu Inoue 	} else
572fb59c426SYoshinobu Inoue #endif /* INET6 */
573fb59c426SYoshinobu Inoue 	if (inp != NULL && ipsec4_in_reject_so(m, inp->inp_socket)) {
574fb59c426SYoshinobu Inoue 		ipsecstat.in_polvio++;
575fb59c426SYoshinobu Inoue 		goto drop;
576fb59c426SYoshinobu Inoue 	}
577fb59c426SYoshinobu Inoue #endif /*IPSEC*/
578df8bae1dSRodney W. Grimes 
579df8bae1dSRodney W. Grimes 	/*
580df8bae1dSRodney W. Grimes 	 * If the state is CLOSED (i.e., TCB does not exist) then
581df8bae1dSRodney W. Grimes 	 * all data in the incoming segment is discarded.
582df8bae1dSRodney W. Grimes 	 * If the TCB exists but is in CLOSED state, it is embryonic,
583df8bae1dSRodney W. Grimes 	 * but should either do a listen or a connect soon.
584df8bae1dSRodney W. Grimes 	 */
585816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
5862e4e1b4cSGeoff Rehmet 		if (log_in_vain) {
587fb59c426SYoshinobu Inoue #ifdef INET6
588fb59c426SYoshinobu Inoue 			char dbuf[INET6_ADDRSTRLEN], sbuf[INET6_ADDRSTRLEN];
589fb59c426SYoshinobu Inoue #else /* INET6 */
590fb59c426SYoshinobu Inoue 			char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"];
591fb59c426SYoshinobu Inoue #endif /* INET6 */
59275cfc95fSAndrey A. Chernov 
593fb59c426SYoshinobu Inoue #ifdef INET6
594fb59c426SYoshinobu Inoue 			if (isipv6) {
595fb59c426SYoshinobu Inoue 				strcpy(dbuf, ip6_sprintf(&ip6->ip6_dst));
596fb59c426SYoshinobu Inoue 				strcpy(sbuf, ip6_sprintf(&ip6->ip6_src));
597fb59c426SYoshinobu Inoue 			} else
598fb59c426SYoshinobu Inoue #endif
599fb59c426SYoshinobu Inoue 		      {
600fb59c426SYoshinobu Inoue 			strcpy(dbuf, inet_ntoa(ip->ip_dst));
601fb59c426SYoshinobu Inoue 			strcpy(sbuf, inet_ntoa(ip->ip_src));
602fb59c426SYoshinobu Inoue 		      }
6032e4e1b4cSGeoff Rehmet 			switch (log_in_vain) {
6042e4e1b4cSGeoff Rehmet 			case 1:
605fb59c426SYoshinobu Inoue 				if(thflags & TH_SYN)
606592071e8SBruce Evans 					log(LOG_INFO,
607592071e8SBruce Evans 			    		"Connection attempt to TCP %s:%d from %s:%d\n",
608fb59c426SYoshinobu Inoue 			    		dbuf, ntohs(th->th_dport),
609fb59c426SYoshinobu Inoue 					sbuf,
610fb59c426SYoshinobu Inoue 					ntohs(th->th_sport));
6112e4e1b4cSGeoff Rehmet 				break;
6122e4e1b4cSGeoff Rehmet 			case 2:
6132e4e1b4cSGeoff Rehmet 				log(LOG_INFO,
6142e4e1b4cSGeoff Rehmet 			    	"Connection attempt to TCP %s:%d from %s:%d flags:0x%x\n",
615fb59c426SYoshinobu Inoue 			    	dbuf, ntohs(th->th_dport), sbuf,
616fb59c426SYoshinobu Inoue 			    	ntohs(th->th_sport), thflags);
6172e4e1b4cSGeoff Rehmet 				break;
6182e4e1b4cSGeoff Rehmet 			default:
6192e4e1b4cSGeoff Rehmet 				break;
6202e4e1b4cSGeoff Rehmet 			}
62175cfc95fSAndrey A. Chernov 		}
6222e4e1b4cSGeoff Rehmet 		if (blackhole) {
6232e4e1b4cSGeoff Rehmet 			switch (blackhole) {
624828b7f40SGeoff Rehmet 			case 1:
625fb59c426SYoshinobu Inoue 				if (thflags & TH_SYN)
626828b7f40SGeoff Rehmet 					goto drop;
627828b7f40SGeoff Rehmet 				break;
628828b7f40SGeoff Rehmet 			case 2:
629828b7f40SGeoff Rehmet 				goto drop;
630828b7f40SGeoff Rehmet 			default:
631828b7f40SGeoff Rehmet 				goto drop;
6322e4e1b4cSGeoff Rehmet 			}
633828b7f40SGeoff Rehmet 		}
634a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
635a57815efSBosko Milekic 		goto dropwithreset;
636816a3d83SPoul-Henning Kamp 	}
637f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
638df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
63909f81a46SBosko Milekic 	if (tp == 0) {
640f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
641a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
642a57815efSBosko Milekic 		goto dropwithreset;
64309f81a46SBosko Milekic 	}
644df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_CLOSED)
645df8bae1dSRodney W. Grimes 		goto drop;
646df8bae1dSRodney W. Grimes 
647df8bae1dSRodney W. Grimes 	/* Unscale the window into a 32-bit value. */
648fb59c426SYoshinobu Inoue 	if ((thflags & TH_SYN) == 0)
649fb59c426SYoshinobu Inoue 		tiwin = th->th_win << tp->snd_scale;
650df8bae1dSRodney W. Grimes 	else
651fb59c426SYoshinobu Inoue 		tiwin = th->th_win;
652fb59c426SYoshinobu Inoue 
653df8bae1dSRodney W. Grimes 	so = inp->inp_socket;
654df8bae1dSRodney W. Grimes 	if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
655be2ac88cSJonathan Lemon 		struct in_conninfo inc;
656610ee2f9SDavid Greenman #ifdef TCPDEBUG
657df8bae1dSRodney W. Grimes 		if (so->so_options & SO_DEBUG) {
658df8bae1dSRodney W. Grimes 			ostate = tp->t_state;
659fb59c426SYoshinobu Inoue #ifdef INET6
660fb59c426SYoshinobu Inoue 			if (isipv6)
661fb59c426SYoshinobu Inoue 				bcopy((char *)ip6, (char *)tcp_saveipgen,
662fb59c426SYoshinobu Inoue 				      sizeof(*ip6));
663fb59c426SYoshinobu Inoue 			else
664fb59c426SYoshinobu Inoue #endif /* INET6 */
665fb59c426SYoshinobu Inoue 			bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
666fb59c426SYoshinobu Inoue 			tcp_savetcp = *th;
667df8bae1dSRodney W. Grimes 		}
668610ee2f9SDavid Greenman #endif
669be2ac88cSJonathan Lemon 		/* skip if this isn't a listen socket */
6704cc20ab1SSeigo Tanimura 		if ((so->so_options & SO_ACCEPTCONN) == 0)
671be2ac88cSJonathan Lemon 			goto after_listen;
672fb59c426SYoshinobu Inoue #ifdef INET6
673be2ac88cSJonathan Lemon 		inc.inc_isipv6 = isipv6;
674be2ac88cSJonathan Lemon 		if (isipv6) {
675be2ac88cSJonathan Lemon 			inc.inc6_faddr = ip6->ip6_src;
676be2ac88cSJonathan Lemon 			inc.inc6_laddr = ip6->ip6_dst;
677be2ac88cSJonathan Lemon 			inc.inc6_route.ro_rt = NULL;		/* XXX */
678fb59c426SYoshinobu Inoue 
679be2ac88cSJonathan Lemon 		} else
680be2ac88cSJonathan Lemon #endif /* INET6 */
681be2ac88cSJonathan Lemon 		{
682be2ac88cSJonathan Lemon 			inc.inc_faddr = ip->ip_src;
683be2ac88cSJonathan Lemon 			inc.inc_laddr = ip->ip_dst;
684be2ac88cSJonathan Lemon 			inc.inc_route.ro_rt = NULL;		/* XXX */
685be2ac88cSJonathan Lemon 		}
686be2ac88cSJonathan Lemon 		inc.inc_fport = th->th_sport;
687be2ac88cSJonathan Lemon 		inc.inc_lport = th->th_dport;
688be2ac88cSJonathan Lemon 
689fb59c426SYoshinobu Inoue 	        /*
690be2ac88cSJonathan Lemon 	         * If the state is LISTEN then ignore segment if it contains
691be2ac88cSJonathan Lemon 		 * a RST.  If the segment contains an ACK then it is bad and
692be2ac88cSJonathan Lemon 		 * send a RST.  If it does not contain a SYN then it is not
693be2ac88cSJonathan Lemon 		 * interesting; drop it.
694be2ac88cSJonathan Lemon 		 *
695be2ac88cSJonathan Lemon 		 * If the state is SYN_RECEIVED (syncache) and seg contains
696be2ac88cSJonathan Lemon 		 * an ACK, but not for our SYN/ACK, send a RST.  If the seg
697be2ac88cSJonathan Lemon 		 * contains a RST, check the sequence number to see if it
698be2ac88cSJonathan Lemon 		 * is a valid reset segment.
699fb59c426SYoshinobu Inoue 		 */
700fb59c426SYoshinobu Inoue 		if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
701be2ac88cSJonathan Lemon 			if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
702be2ac88cSJonathan Lemon 				if (!syncache_expand(&inc, th, &so, m)) {
7034195b4afSPaul Traina 					/*
704be2ac88cSJonathan Lemon 					 * No syncache entry, or ACK was not
705be2ac88cSJonathan Lemon 					 * for our SYN/ACK.  Send a RST.
7064195b4afSPaul Traina 					 */
707be2ac88cSJonathan Lemon 					tcpstat.tcps_badsyn++;
708be2ac88cSJonathan Lemon 					rstreason = BANDLIM_RST_OPENPORT;
709be2ac88cSJonathan Lemon 					goto dropwithreset;
710be2ac88cSJonathan Lemon 				}
711f76fcf6dSJeffrey Hsu 				if (so == NULL) {
712be2ac88cSJonathan Lemon 					/*
713be2ac88cSJonathan Lemon 					 * Could not complete 3-way handshake,
714be2ac88cSJonathan Lemon 					 * connection is being closed down, and
715be2ac88cSJonathan Lemon 					 * syncache will free mbuf.
716be2ac88cSJonathan Lemon 					 */
717f76fcf6dSJeffrey Hsu 					INP_UNLOCK(inp);
718f76fcf6dSJeffrey Hsu 					INP_INFO_WUNLOCK(&tcbinfo);
719be2ac88cSJonathan Lemon 					return;
720f76fcf6dSJeffrey Hsu 				}
721be2ac88cSJonathan Lemon 				/*
722be2ac88cSJonathan Lemon 				 * Socket is created in state SYN_RECEIVED.
723be2ac88cSJonathan Lemon 				 * Continue processing segment.
724be2ac88cSJonathan Lemon 				 */
725f76fcf6dSJeffrey Hsu 				INP_UNLOCK(inp);
726be2ac88cSJonathan Lemon 				inp = sotoinpcb(so);
727f76fcf6dSJeffrey Hsu 				INP_LOCK(inp);
728be2ac88cSJonathan Lemon 				tp = intotcpcb(inp);
729be2ac88cSJonathan Lemon 				/*
730be2ac88cSJonathan Lemon 				 * This is what would have happened in
731e6658b12SRobert Watson 				 * tcp_output() when the SYN,ACK was sent.
732be2ac88cSJonathan Lemon 				 */
733be2ac88cSJonathan Lemon 				tp->snd_up = tp->snd_una;
734be2ac88cSJonathan Lemon 				tp->snd_max = tp->snd_nxt = tp->iss + 1;
735be2ac88cSJonathan Lemon 				tp->last_ack_sent = tp->rcv_nxt;
736be2ac88cSJonathan Lemon /*
737be2ac88cSJonathan Lemon  * XXX possible bug - it doesn't appear that tp->snd_wnd is unscaled
738be2ac88cSJonathan Lemon  * until the _second_ ACK is received:
739be2ac88cSJonathan Lemon  *    rcv SYN (set wscale opts)	 --> send SYN/ACK, set snd_wnd = window.
740be2ac88cSJonathan Lemon  *    rcv ACK, calculate tiwin --> process SYN_RECEIVED, determine wscale,
741be2ac88cSJonathan Lemon  *        move to ESTAB, set snd_wnd to tiwin.
742be2ac88cSJonathan Lemon  */
743be2ac88cSJonathan Lemon 				tp->snd_wnd = tiwin;	/* unscaled */
744be2ac88cSJonathan Lemon 				goto after_listen;
745be2ac88cSJonathan Lemon 			}
746be2ac88cSJonathan Lemon 			if (thflags & TH_RST) {
747be2ac88cSJonathan Lemon 				syncache_chkrst(&inc, th);
748be2ac88cSJonathan Lemon 				goto drop;
749be2ac88cSJonathan Lemon 			}
750fb59c426SYoshinobu Inoue 			if (thflags & TH_ACK) {
751be2ac88cSJonathan Lemon 				syncache_badack(&inc);
752ebb0cbeaSPaul Traina 				tcpstat.tcps_badsyn++;
753a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
754a57815efSBosko Milekic 				goto dropwithreset;
7554195b4afSPaul Traina 			}
756ebb0cbeaSPaul Traina 			goto drop;
757ebb0cbeaSPaul Traina 		}
75833841545SHajimu UMEMOTO 
759be2ac88cSJonathan Lemon 		/*
760be2ac88cSJonathan Lemon 		 * Segment's flags are (SYN) or (SYN|FIN).
761be2ac88cSJonathan Lemon 		 */
76233841545SHajimu UMEMOTO #ifdef INET6
76333841545SHajimu UMEMOTO 		/*
76433841545SHajimu UMEMOTO 		 * If deprecated address is forbidden,
76533841545SHajimu UMEMOTO 		 * we do not accept SYN to deprecated interface
76633841545SHajimu UMEMOTO 		 * address to prevent any new inbound connection from
76733841545SHajimu UMEMOTO 		 * getting established.
76833841545SHajimu UMEMOTO 		 * When we do not accept SYN, we send a TCP RST,
76933841545SHajimu UMEMOTO 		 * with deprecated source address (instead of dropping
77033841545SHajimu UMEMOTO 		 * it).  We compromise it as it is much better for peer
77133841545SHajimu UMEMOTO 		 * to send a RST, and RST will be the final packet
77233841545SHajimu UMEMOTO 		 * for the exchange.
77333841545SHajimu UMEMOTO 		 *
77433841545SHajimu UMEMOTO 		 * If we do not forbid deprecated addresses, we accept
77533841545SHajimu UMEMOTO 		 * the SYN packet.  RFC2462 does not suggest dropping
77633841545SHajimu UMEMOTO 		 * SYN in this case.
77733841545SHajimu UMEMOTO 		 * If we decipher RFC2462 5.5.4, it says like this:
77833841545SHajimu UMEMOTO 		 * 1. use of deprecated addr with existing
77933841545SHajimu UMEMOTO 		 *    communication is okay - "SHOULD continue to be
78033841545SHajimu UMEMOTO 		 *    used"
78133841545SHajimu UMEMOTO 		 * 2. use of it with new communication:
78233841545SHajimu UMEMOTO 		 *   (2a) "SHOULD NOT be used if alternate address
78333841545SHajimu UMEMOTO 		 *        with sufficient scope is available"
78433841545SHajimu UMEMOTO 		 *   (2b) nothing mentioned otherwise.
78533841545SHajimu UMEMOTO 		 * Here we fall into (2b) case as we have no choice in
78633841545SHajimu UMEMOTO 		 * our source address selection - we must obey the peer.
78733841545SHajimu UMEMOTO 		 *
78833841545SHajimu UMEMOTO 		 * The wording in RFC2462 is confusing, and there are
78933841545SHajimu UMEMOTO 		 * multiple description text for deprecated address
79033841545SHajimu UMEMOTO 		 * handling - worse, they are not exactly the same.
79133841545SHajimu UMEMOTO 		 * I believe 5.5.4 is the best one, so we follow 5.5.4.
79233841545SHajimu UMEMOTO 		 */
79333841545SHajimu UMEMOTO 		if (isipv6 && !ip6_use_deprecated) {
79433841545SHajimu UMEMOTO 			struct in6_ifaddr *ia6;
79533841545SHajimu UMEMOTO 
79633841545SHajimu UMEMOTO 			if ((ia6 = ip6_getdstifaddr(m)) &&
79733841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
798f76fcf6dSJeffrey Hsu 				INP_UNLOCK(inp);
79933841545SHajimu UMEMOTO 				tp = NULL;
80033841545SHajimu UMEMOTO 				rstreason = BANDLIM_RST_OPENPORT;
80133841545SHajimu UMEMOTO 				goto dropwithreset;
80233841545SHajimu UMEMOTO 			}
80333841545SHajimu UMEMOTO 		}
80433841545SHajimu UMEMOTO #endif
80565f28919SJesper Skriver 		/*
806be2ac88cSJonathan Lemon 		 * If it is from this socket, drop it, it must be forged.
807be2ac88cSJonathan Lemon 		 * Don't bother responding if the destination was a broadcast.
80865f28919SJesper Skriver 		 */
809be2ac88cSJonathan Lemon 		if (th->th_dport == th->th_sport) {
810fb59c426SYoshinobu Inoue #ifdef INET6
811fb59c426SYoshinobu Inoue 			if (isipv6) {
812be2ac88cSJonathan Lemon 				if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
813be2ac88cSJonathan Lemon 						       &ip6->ip6_src))
814be2ac88cSJonathan Lemon 					goto drop;
815fb59c426SYoshinobu Inoue 			} else
816fb59c426SYoshinobu Inoue #endif /* INET6 */
817be2ac88cSJonathan Lemon 			if (ip->ip_dst.s_addr == ip->ip_src.s_addr)
818be2ac88cSJonathan Lemon 				goto drop;
819be2ac88cSJonathan Lemon 		}
820be2ac88cSJonathan Lemon 		/*
821be2ac88cSJonathan Lemon 		 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
8222ca2159fSCrist J. Clark 		 *
82393ec91baSCrist J. Clark 		 * Note that it is quite possible to receive unicast
82493ec91baSCrist J. Clark 		 * link-layer packets with a broadcast IP address. Use
82593ec91baSCrist J. Clark 		 * in_broadcast() to find them.
826be2ac88cSJonathan Lemon 		 */
827be2ac88cSJonathan Lemon 		if (m->m_flags & (M_BCAST|M_MCAST))
828be2ac88cSJonathan Lemon 			goto drop;
829be2ac88cSJonathan Lemon #ifdef INET6
830be2ac88cSJonathan Lemon 		if (isipv6) {
831be2ac88cSJonathan Lemon 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
832be2ac88cSJonathan Lemon 			    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
833be2ac88cSJonathan Lemon 				goto drop;
834be2ac88cSJonathan Lemon 		} else
835fb59c426SYoshinobu Inoue #endif
836be2ac88cSJonathan Lemon 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
837be2ac88cSJonathan Lemon 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
8382ca2159fSCrist J. Clark 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
8392ca2159fSCrist J. Clark 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
840be2ac88cSJonathan Lemon 			goto drop;
841be2ac88cSJonathan Lemon 		/*
842be2ac88cSJonathan Lemon 		 * SYN appears to be valid; create compressed TCP state
843be2ac88cSJonathan Lemon 		 * for syncache, or perform t/tcp connection.
844be2ac88cSJonathan Lemon 		 */
845be2ac88cSJonathan Lemon 		if (so->so_qlen <= so->so_qlimit) {
846be2ac88cSJonathan Lemon 			tcp_dooptions(&to, optp, optlen, 1);
847be2ac88cSJonathan Lemon 			if (!syncache_add(&inc, &to, th, &so, m))
848be2ac88cSJonathan Lemon 				goto drop;
849f76fcf6dSJeffrey Hsu 			if (so == NULL) {
850be2ac88cSJonathan Lemon 				/*
851be2ac88cSJonathan Lemon 				 * Entry added to syncache, mbuf used to
852be2ac88cSJonathan Lemon 				 * send SYN,ACK packet.
853be2ac88cSJonathan Lemon 				 */
854f76fcf6dSJeffrey Hsu 				KASSERT(headlocked, ("headlocked"));
855f76fcf6dSJeffrey Hsu 				INP_UNLOCK(inp);
856f76fcf6dSJeffrey Hsu 				INP_INFO_WUNLOCK(&tcbinfo);
857be2ac88cSJonathan Lemon 				return;
858f76fcf6dSJeffrey Hsu 			}
859be2ac88cSJonathan Lemon 			/*
860be2ac88cSJonathan Lemon 			 * Segment passed TAO tests.
861be2ac88cSJonathan Lemon 			 */
862f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
863be2ac88cSJonathan Lemon 			inp = sotoinpcb(so);
864f76fcf6dSJeffrey Hsu 			INP_LOCK(inp);
865df8bae1dSRodney W. Grimes 			tp = intotcpcb(inp);
866be2ac88cSJonathan Lemon 			tp->snd_wnd = tiwin;
867be2ac88cSJonathan Lemon 			tp->t_starttime = ticks;
868be2ac88cSJonathan Lemon 			tp->t_state = TCPS_ESTABLISHED;
869df8bae1dSRodney W. Grimes 
870be2ac88cSJonathan Lemon 			/*
871be2ac88cSJonathan Lemon 			 * If there is a FIN, or if there is data and the
872be2ac88cSJonathan Lemon 			 * connection is local, then delay SYN,ACK(SYN) in
873be2ac88cSJonathan Lemon 			 * the hope of piggy-backing it on a response
874be2ac88cSJonathan Lemon 			 * segment.  Otherwise must send ACK now in case
875be2ac88cSJonathan Lemon 			 * the other side is slow starting.
876be2ac88cSJonathan Lemon 			 */
877be2ac88cSJonathan Lemon 			if (DELAY_ACK(tp) && ((thflags & TH_FIN) ||
878be2ac88cSJonathan Lemon 			    (tlen != 0 &&
879be2ac88cSJonathan Lemon #ifdef INET6
880be2ac88cSJonathan Lemon 			      ((isipv6 && in6_localaddr(&inp->in6p_faddr))
881be2ac88cSJonathan Lemon 			      ||
882be2ac88cSJonathan Lemon 			      (!isipv6 &&
883be2ac88cSJonathan Lemon #endif
884be2ac88cSJonathan Lemon 			    in_localaddr(inp->inp_faddr)
885be2ac88cSJonathan Lemon #ifdef INET6
886be2ac88cSJonathan Lemon 			       ))
887be2ac88cSJonathan Lemon #endif
888be2ac88cSJonathan Lemon 			     ))) {
889be2ac88cSJonathan Lemon                                 callout_reset(tp->tt_delack, tcp_delacktime,
890be2ac88cSJonathan Lemon                                     tcp_timer_delack, tp);
891be2ac88cSJonathan Lemon 				tp->t_flags |= TF_NEEDSYN;
892be2ac88cSJonathan Lemon 			} else
893be2ac88cSJonathan Lemon 				tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
894be2ac88cSJonathan Lemon 
895be2ac88cSJonathan Lemon 			tcpstat.tcps_connects++;
896be2ac88cSJonathan Lemon 			soisconnected(so);
897be2ac88cSJonathan Lemon 			goto trimthenstep6;
898df8bae1dSRodney W. Grimes 		}
899be2ac88cSJonathan Lemon 		goto drop;
9004cc20ab1SSeigo Tanimura 	}
901be2ac88cSJonathan Lemon after_listen:
902be2ac88cSJonathan Lemon 
903be2ac88cSJonathan Lemon /* XXX temp debugging */
904be2ac88cSJonathan Lemon 	/* should not happen - syncache should pick up these connections */
905be2ac88cSJonathan Lemon 	if (tp->t_state == TCPS_LISTEN)
906be2ac88cSJonathan Lemon 		panic("tcp_input: TCPS_LISTEN");
907df8bae1dSRodney W. Grimes 
908df8bae1dSRodney W. Grimes 	/*
909df8bae1dSRodney W. Grimes 	 * Segment received on connection.
910df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
911df8bae1dSRodney W. Grimes 	 */
9129b8b58e0SJonathan Lemon 	tp->t_rcvtime = ticks;
9137ff19458SPaul Traina 	if (TCPS_HAVEESTABLISHED(tp->t_state))
9149b8b58e0SJonathan Lemon 		callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp);
915df8bae1dSRodney W. Grimes 
916df8bae1dSRodney W. Grimes 	/*
917be2ac88cSJonathan Lemon 	 * Process options.
918be2ac88cSJonathan Lemon 	 * XXX this is tradtitional behavior, may need to be cleaned up.
919df8bae1dSRodney W. Grimes 	 */
920be2ac88cSJonathan Lemon 	tcp_dooptions(&to, optp, optlen, thflags & TH_SYN);
921be2ac88cSJonathan Lemon 	if (thflags & TH_SYN) {
922be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_SCALE) {
923be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_SCALE;
924be2ac88cSJonathan Lemon 			tp->requested_s_scale = to.to_requested_s_scale;
925be2ac88cSJonathan Lemon 		}
926be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_TS) {
927be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_TSTMP;
928be2ac88cSJonathan Lemon 			tp->ts_recent = to.to_tsval;
929be2ac88cSJonathan Lemon 			tp->ts_recent_age = ticks;
930be2ac88cSJonathan Lemon 		}
931be2ac88cSJonathan Lemon 		if (to.to_flags & (TOF_CC|TOF_CCNEW))
932be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_CC;
933be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_MSS)
934be2ac88cSJonathan Lemon 			tcp_mss(tp, to.to_mss);
935be2ac88cSJonathan Lemon 	}
936df8bae1dSRodney W. Grimes 
937df8bae1dSRodney W. Grimes 	/*
938df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
939df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
940df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
941df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
942df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
943df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
944df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
945df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
946df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
947df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
948df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
949df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
950a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
951a0292f23SGarrett Wollman 	 * Since we check for TCPS_ESTABLISHED above, it can only
952a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
953df8bae1dSRodney W. Grimes 	 */
954df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
955fb59c426SYoshinobu Inoue 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
956a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
957be2ac88cSJonathan Lemon 	    ((to.to_flags & TOF_TS) == 0 ||
958a0292f23SGarrett Wollman 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) &&
959a0292f23SGarrett Wollman 	    /*
960a0292f23SGarrett Wollman 	     * Using the CC option is compulsory if once started:
961a0292f23SGarrett Wollman 	     *   the segment is OK if no T/TCP was negotiated or
962a0292f23SGarrett Wollman 	     *   if the segment has a CC option equal to CCrecv
963a0292f23SGarrett Wollman 	     */
964a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) ||
965be2ac88cSJonathan Lemon 	     ((to.to_flags & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) &&
966fb59c426SYoshinobu Inoue 	    th->th_seq == tp->rcv_nxt &&
967df8bae1dSRodney W. Grimes 	    tiwin && tiwin == tp->snd_wnd &&
968df8bae1dSRodney W. Grimes 	    tp->snd_nxt == tp->snd_max) {
969df8bae1dSRodney W. Grimes 
970df8bae1dSRodney W. Grimes 		/*
971df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
972df8bae1dSRodney W. Grimes 		 * record the timestamp.
973a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
974a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
975df8bae1dSRodney W. Grimes 		 */
976be2ac88cSJonathan Lemon 		if ((to.to_flags & TOF_TS) != 0 &&
977fb59c426SYoshinobu Inoue 		   SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
9789b8b58e0SJonathan Lemon 			tp->ts_recent_age = ticks;
979a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
980df8bae1dSRodney W. Grimes 		}
981df8bae1dSRodney W. Grimes 
982fb59c426SYoshinobu Inoue 		if (tlen == 0) {
983fb59c426SYoshinobu Inoue 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
984fb59c426SYoshinobu Inoue 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
985233e8c18SGarrett Wollman 			    tp->snd_cwnd >= tp->snd_wnd &&
986233e8c18SGarrett Wollman 			    tp->t_dupacks < tcprexmtthresh) {
987f76fcf6dSJeffrey Hsu 				KASSERT(headlocked, ("headlocked"));
988f76fcf6dSJeffrey Hsu 				INP_INFO_WUNLOCK(&tcbinfo);
989f76fcf6dSJeffrey Hsu 				headlocked = 0;
990df8bae1dSRodney W. Grimes 				/*
991df8bae1dSRodney W. Grimes 				 * this is a pure ack for outstanding data.
992df8bae1dSRodney W. Grimes 				 */
993df8bae1dSRodney W. Grimes 				++tcpstat.tcps_predack;
9949b8b58e0SJonathan Lemon 				/*
9959b8b58e0SJonathan Lemon 				 * "bad retransmit" recovery
9969b8b58e0SJonathan Lemon 				 */
9979b8b58e0SJonathan Lemon 				if (tp->t_rxtshift == 1 &&
9989b8b58e0SJonathan Lemon 				    ticks < tp->t_badrxtwin) {
9999b8b58e0SJonathan Lemon 					tp->snd_cwnd = tp->snd_cwnd_prev;
10009b8b58e0SJonathan Lemon 					tp->snd_ssthresh =
10019b8b58e0SJonathan Lemon 					    tp->snd_ssthresh_prev;
10029b8b58e0SJonathan Lemon 					tp->snd_nxt = tp->snd_max;
10039b8b58e0SJonathan Lemon 					tp->t_badrxtwin = 0;
10049b8b58e0SJonathan Lemon 				}
1005be2ac88cSJonathan Lemon 				if ((to.to_flags & TOF_TS) != 0)
1006a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
10079b8b58e0SJonathan Lemon 					    ticks - to.to_tsecr + 1);
10089b8b58e0SJonathan Lemon 				else if (tp->t_rtttime &&
1009fb59c426SYoshinobu Inoue 					    SEQ_GT(th->th_ack, tp->t_rtseq))
10109b8b58e0SJonathan Lemon 					tcp_xmit_timer(tp, ticks - tp->t_rtttime);
1011fb59c426SYoshinobu Inoue 				acked = th->th_ack - tp->snd_una;
1012df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvackpack++;
1013df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvackbyte += acked;
1014df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
1015fb59c426SYoshinobu Inoue 				tp->snd_una = th->th_ack;
1016df8bae1dSRodney W. Grimes 				m_freem(m);
1017fb59c426SYoshinobu Inoue 				ND6_HINT(tp); /* some progress has been done */
1018df8bae1dSRodney W. Grimes 
1019df8bae1dSRodney W. Grimes 				/*
1020df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
1021df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
1022df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
1023df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
1024df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
1025df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
1026df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
1027df8bae1dSRodney W. Grimes 				 */
1028df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
10299b8b58e0SJonathan Lemon 					callout_stop(tp->tt_rexmt);
10309b8b58e0SJonathan Lemon 				else if (!callout_active(tp->tt_persist))
10319b8b58e0SJonathan Lemon 					callout_reset(tp->tt_rexmt,
10329b8b58e0SJonathan Lemon 						      tp->t_rxtcur,
10339b8b58e0SJonathan Lemon 						      tcp_timer_rexmt, tp);
1034df8bae1dSRodney W. Grimes 
1035df8bae1dSRodney W. Grimes 				sowwakeup(so);
1036df8bae1dSRodney W. Grimes 				if (so->so_snd.sb_cc)
1037df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
1038f76fcf6dSJeffrey Hsu 				INP_UNLOCK(inp);
1039df8bae1dSRodney W. Grimes 				return;
1040df8bae1dSRodney W. Grimes 			}
1041fb59c426SYoshinobu Inoue 		} else if (th->th_ack == tp->snd_una &&
1042fb59c426SYoshinobu Inoue 		    LIST_EMPTY(&tp->t_segq) &&
1043fb59c426SYoshinobu Inoue 		    tlen <= sbspace(&so->so_rcv)) {
1044f76fcf6dSJeffrey Hsu 			KASSERT(headlocked, ("headlocked"));
1045f76fcf6dSJeffrey Hsu 			INP_INFO_WUNLOCK(&tcbinfo);
1046f76fcf6dSJeffrey Hsu 			headlocked = 0;
1047df8bae1dSRodney W. Grimes 			/*
1048df8bae1dSRodney W. Grimes 			 * this is a pure, in-sequence data packet
1049df8bae1dSRodney W. Grimes 			 * with nothing on the reassembly queue and
1050df8bae1dSRodney W. Grimes 			 * we have enough buffer space to take it.
1051df8bae1dSRodney W. Grimes 			 */
1052df8bae1dSRodney W. Grimes 			++tcpstat.tcps_preddat;
1053fb59c426SYoshinobu Inoue 			tp->rcv_nxt += tlen;
1054df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpack++;
1055fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbyte += tlen;
1056fb59c426SYoshinobu Inoue 			ND6_HINT(tp);	/* some progress has been done */
1057df8bae1dSRodney W. Grimes 			/*
1058755c1f07SAndras Olah 			 * Add data to socket buffer.
1059df8bae1dSRodney W. Grimes 			 */
1060fb59c426SYoshinobu Inoue 			m_adj(m, drop_hdrlen);	/* delayed header drop */
1061df8bae1dSRodney W. Grimes 			sbappend(&so->so_rcv, m);
1062df8bae1dSRodney W. Grimes 			sorwakeup(so);
1063d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp)) {
10649b8b58e0SJonathan Lemon 	                        callout_reset(tp->tt_delack, tcp_delacktime,
10659b8b58e0SJonathan Lemon 	                            tcp_timer_delack, tp);
1066f498eeeeSDavid Greenman 			} else {
1067e612a582SDavid Greenman 				tp->t_flags |= TF_ACKNOW;
1068e612a582SDavid Greenman 				tcp_output(tp);
1069e612a582SDavid Greenman 			}
1070f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
1071df8bae1dSRodney W. Grimes 			return;
1072df8bae1dSRodney W. Grimes 		}
1073df8bae1dSRodney W. Grimes 	}
1074df8bae1dSRodney W. Grimes 
1075df8bae1dSRodney W. Grimes 	/*
1076df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
1077df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
1078df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
1079df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
1080df8bae1dSRodney W. Grimes 	 */
1081df8bae1dSRodney W. Grimes 	{ int win;
1082df8bae1dSRodney W. Grimes 
1083df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
1084df8bae1dSRodney W. Grimes 	if (win < 0)
1085df8bae1dSRodney W. Grimes 		win = 0;
108666e39adcSJohn Polstra 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1087df8bae1dSRodney W. Grimes 	}
1088df8bae1dSRodney W. Grimes 
1089df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1090df8bae1dSRodney W. Grimes 
1091df8bae1dSRodney W. Grimes 	/*
1092764d8cefSBill Fenner 	 * If the state is SYN_RECEIVED:
1093764d8cefSBill Fenner 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
1094764d8cefSBill Fenner 	 */
1095764d8cefSBill Fenner 	case TCPS_SYN_RECEIVED:
1096fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1097fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1098a57815efSBosko Milekic 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1099a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
1100a57815efSBosko Milekic 				goto dropwithreset;
1101a57815efSBosko Milekic 		}
1102764d8cefSBill Fenner 		break;
1103764d8cefSBill Fenner 
1104764d8cefSBill Fenner 	/*
1105df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
1106df8bae1dSRodney W. Grimes 	 *	if seg contains an ACK, but not for our SYN, drop the input.
1107df8bae1dSRodney W. Grimes 	 *	if seg contains a RST, then drop the connection.
1108df8bae1dSRodney W. Grimes 	 *	if seg does not contain SYN, then drop it.
1109df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
1110df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
1111df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
1112df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1113df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
1114df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
1115df8bae1dSRodney W. Grimes 	 */
1116df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
1117be2ac88cSJonathan Lemon 		if ((taop = tcp_gettaocache(&inp->inp_inc)) == NULL) {
1118a0292f23SGarrett Wollman 			taop = &tao_noncached;
1119a0292f23SGarrett Wollman 			bzero(taop, sizeof(*taop));
1120a0292f23SGarrett Wollman 		}
1121a0292f23SGarrett Wollman 
1122fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1123fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->iss) ||
1124fb59c426SYoshinobu Inoue 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1125a0292f23SGarrett Wollman 			/*
1126a0292f23SGarrett Wollman 			 * If we have a cached CCsent for the remote host,
1127a0292f23SGarrett Wollman 			 * hence we haven't just crashed and restarted,
1128a0292f23SGarrett Wollman 			 * do not send a RST.  This may be a retransmission
1129a0292f23SGarrett Wollman 			 * from the other side after our earlier ACK was lost.
1130a0292f23SGarrett Wollman 			 * Our new SYN, when it arrives, will serve as the
1131a0292f23SGarrett Wollman 			 * needed ACK.
1132a0292f23SGarrett Wollman 			 */
1133a0292f23SGarrett Wollman 			if (taop->tao_ccsent != 0)
1134a0292f23SGarrett Wollman 				goto drop;
1135a57815efSBosko Milekic 			else {
1136a57815efSBosko Milekic 				rstreason = BANDLIM_UNLIMITED;
1137a0292f23SGarrett Wollman 				goto dropwithreset;
1138a0292f23SGarrett Wollman 			}
1139a57815efSBosko Milekic 		}
1140fb59c426SYoshinobu Inoue 		if (thflags & TH_RST) {
1141fb59c426SYoshinobu Inoue 			if (thflags & TH_ACK)
1142df8bae1dSRodney W. Grimes 				tp = tcp_drop(tp, ECONNREFUSED);
1143df8bae1dSRodney W. Grimes 			goto drop;
1144df8bae1dSRodney W. Grimes 		}
1145fb59c426SYoshinobu Inoue 		if ((thflags & TH_SYN) == 0)
1146df8bae1dSRodney W. Grimes 			goto drop;
1147fb59c426SYoshinobu Inoue 		tp->snd_wnd = th->th_win;	/* initial send window */
1148a0292f23SGarrett Wollman 		tp->cc_recv = to.to_cc;		/* foreign CC */
1149a0292f23SGarrett Wollman 
1150fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
1151df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
1152fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1153a0292f23SGarrett Wollman 			/*
1154a0292f23SGarrett Wollman 			 * Our SYN was acked.  If segment contains CC.ECHO
1155a0292f23SGarrett Wollman 			 * option, check it to make sure this segment really
1156a0292f23SGarrett Wollman 			 * matches our SYN.  If not, just drop it as old
1157a0292f23SGarrett Wollman 			 * duplicate, but send an RST if we're still playing
1158026650e5SBill Fenner 			 * by the old rules.  If no CC.ECHO option, make sure
1159026650e5SBill Fenner 			 * we don't get fooled into using T/TCP.
1160a0292f23SGarrett Wollman 			 */
1161be2ac88cSJonathan Lemon 			if (to.to_flags & TOF_CCECHO) {
1162dfd5dee1SPeter Wemm 				if (tp->cc_send != to.to_ccecho) {
1163a0292f23SGarrett Wollman 					if (taop->tao_ccsent != 0)
1164a0292f23SGarrett Wollman 						goto drop;
1165a57815efSBosko Milekic 					else {
1166a57815efSBosko Milekic 						rstreason = BANDLIM_UNLIMITED;
1167a0292f23SGarrett Wollman 						goto dropwithreset;
1168dfd5dee1SPeter Wemm 					}
1169a57815efSBosko Milekic 				}
1170026650e5SBill Fenner 			} else
1171026650e5SBill Fenner 				tp->t_flags &= ~TF_RCVD_CC;
1172845799c1SAndras Olah 			tcpstat.tcps_connects++;
1173845799c1SAndras Olah 			soisconnected(so);
1174845799c1SAndras Olah 			/* Do window scaling on this connection? */
1175845799c1SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1176845799c1SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1177845799c1SAndras Olah 				tp->snd_scale = tp->requested_s_scale;
1178845799c1SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
1179845799c1SAndras Olah 			}
1180a0292f23SGarrett Wollman 			/* Segment is acceptable, update cache if undefined. */
1181a0292f23SGarrett Wollman 			if (taop->tao_ccsent == 0)
1182a0292f23SGarrett Wollman 				taop->tao_ccsent = to.to_ccecho;
1183a0292f23SGarrett Wollman 
1184a0292f23SGarrett Wollman 			tp->rcv_adv += tp->rcv_wnd;
1185a0292f23SGarrett Wollman 			tp->snd_una++;		/* SYN is acked */
1186a0292f23SGarrett Wollman 			/*
1187a0292f23SGarrett Wollman 			 * If there's data, delay ACK; if there's also a FIN
1188a0292f23SGarrett Wollman 			 * ACKNOW will be turned on later.
1189a0292f23SGarrett Wollman 			 */
1190d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp) && tlen != 0)
11919b8b58e0SJonathan Lemon                                 callout_reset(tp->tt_delack, tcp_delacktime,
11929b8b58e0SJonathan Lemon                                     tcp_timer_delack, tp);
1193a0292f23SGarrett Wollman 			else
1194a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
1195a0292f23SGarrett Wollman 			/*
1196a0292f23SGarrett Wollman 			 * Received <SYN,ACK> in SYN_SENT[*] state.
1197a0292f23SGarrett Wollman 			 * Transitions:
1198a0292f23SGarrett Wollman 			 *	SYN_SENT  --> ESTABLISHED
1199a0292f23SGarrett Wollman 			 *	SYN_SENT* --> FIN_WAIT_1
1200a0292f23SGarrett Wollman 			 */
12019b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
1202a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDFIN) {
1203a0292f23SGarrett Wollman 				tp->t_state = TCPS_FIN_WAIT_1;
1204a0292f23SGarrett Wollman 				tp->t_flags &= ~TF_NEEDFIN;
1205fb59c426SYoshinobu Inoue 				thflags &= ~TH_SYN;
12067ff19458SPaul Traina 			} else {
1207a0292f23SGarrett Wollman 				tp->t_state = TCPS_ESTABLISHED;
12089b8b58e0SJonathan Lemon 				callout_reset(tp->tt_keep, tcp_keepidle,
12099b8b58e0SJonathan Lemon 					      tcp_timer_keep, tp);
12107ff19458SPaul Traina 			}
1211a0292f23SGarrett Wollman 		} else {
1212a0292f23SGarrett Wollman 		/*
1213a0292f23SGarrett Wollman 		 *  Received initial SYN in SYN-SENT[*] state => simul-
1214a0292f23SGarrett Wollman 		 *  taneous open.  If segment contains CC option and there is
1215a0292f23SGarrett Wollman 		 *  a cached CC, apply TAO test; if it succeeds, connection is
1216a0292f23SGarrett Wollman 		 *  half-synchronized.  Otherwise, do 3-way handshake:
1217a0292f23SGarrett Wollman 		 *        SYN-SENT -> SYN-RECEIVED
1218a0292f23SGarrett Wollman 		 *        SYN-SENT* -> SYN-RECEIVED*
1219a0292f23SGarrett Wollman 		 *  If there was no CC option, clear cached CC value.
1220a0292f23SGarrett Wollman 		 */
1221a0292f23SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
12229b8b58e0SJonathan Lemon 			callout_stop(tp->tt_rexmt);
1223be2ac88cSJonathan Lemon 			if (to.to_flags & TOF_CC) {
1224a0292f23SGarrett Wollman 				if (taop->tao_cc != 0 &&
1225a0292f23SGarrett Wollman 				    CC_GT(to.to_cc, taop->tao_cc)) {
1226a0292f23SGarrett Wollman 					/*
1227a0292f23SGarrett Wollman 					 * update cache and make transition:
1228a0292f23SGarrett Wollman 					 *        SYN-SENT -> ESTABLISHED*
1229a0292f23SGarrett Wollman 					 *        SYN-SENT* -> FIN-WAIT-1*
1230a0292f23SGarrett Wollman 					 */
1231a0292f23SGarrett Wollman 					taop->tao_cc = to.to_cc;
12329b8b58e0SJonathan Lemon 					tp->t_starttime = ticks;
1233a0292f23SGarrett Wollman 					if (tp->t_flags & TF_NEEDFIN) {
1234a0292f23SGarrett Wollman 						tp->t_state = TCPS_FIN_WAIT_1;
1235a0292f23SGarrett Wollman 						tp->t_flags &= ~TF_NEEDFIN;
12367ff19458SPaul Traina 					} else {
1237a0292f23SGarrett Wollman 						tp->t_state = TCPS_ESTABLISHED;
12389b8b58e0SJonathan Lemon 						callout_reset(tp->tt_keep,
12399b8b58e0SJonathan Lemon 							      tcp_keepidle,
12409b8b58e0SJonathan Lemon 							      tcp_timer_keep,
12419b8b58e0SJonathan Lemon 							      tp);
12427ff19458SPaul Traina 					}
1243a0292f23SGarrett Wollman 					tp->t_flags |= TF_NEEDSYN;
1244df8bae1dSRodney W. Grimes 				} else
1245df8bae1dSRodney W. Grimes 					tp->t_state = TCPS_SYN_RECEIVED;
1246a0292f23SGarrett Wollman 			} else {
1247a0292f23SGarrett Wollman 				/* CC.NEW or no option => invalidate cache */
1248a0292f23SGarrett Wollman 				taop->tao_cc = 0;
1249a0292f23SGarrett Wollman 				tp->t_state = TCPS_SYN_RECEIVED;
1250a0292f23SGarrett Wollman 			}
1251a0292f23SGarrett Wollman 		}
1252df8bae1dSRodney W. Grimes 
1253df8bae1dSRodney W. Grimes trimthenstep6:
1254df8bae1dSRodney W. Grimes 		/*
1255fb59c426SYoshinobu Inoue 		 * Advance th->th_seq to correspond to first data byte.
1256df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
1257df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
1258df8bae1dSRodney W. Grimes 		 */
1259fb59c426SYoshinobu Inoue 		th->th_seq++;
1260fb59c426SYoshinobu Inoue 		if (tlen > tp->rcv_wnd) {
1261fb59c426SYoshinobu Inoue 			todrop = tlen - tp->rcv_wnd;
1262df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
1263fb59c426SYoshinobu Inoue 			tlen = tp->rcv_wnd;
1264fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
1265df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpackafterwin++;
1266df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyteafterwin += todrop;
1267df8bae1dSRodney W. Grimes 		}
1268fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
1269fb59c426SYoshinobu Inoue 		tp->rcv_up = th->th_seq;
1270a0292f23SGarrett Wollman 		/*
1271a0292f23SGarrett Wollman 		 *  Client side of transaction: already sent SYN and data.
1272a0292f23SGarrett Wollman 		 *  If the remote host used T/TCP to validate the SYN,
1273a0292f23SGarrett Wollman 		 *  our data will be ACK'd; if so, enter normal data segment
1274a0292f23SGarrett Wollman 		 *  processing in the middle of step 5, ack processing.
1275a0292f23SGarrett Wollman 		 *  Otherwise, goto step 6.
1276a0292f23SGarrett Wollman 		 */
1277fb59c426SYoshinobu Inoue  		if (thflags & TH_ACK)
1278a0292f23SGarrett Wollman 			goto process_ACK;
1279df8bae1dSRodney W. Grimes 		goto step6;
1280a0292f23SGarrett Wollman 	/*
1281a0292f23SGarrett Wollman 	 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
1282a0292f23SGarrett Wollman 	 *	if segment contains a SYN and CC [not CC.NEW] option:
1283a0292f23SGarrett Wollman 	 *              if state == TIME_WAIT and connection duration > MSL,
1284a0292f23SGarrett Wollman 	 *                  drop packet and send RST;
1285a0292f23SGarrett Wollman 	 *
1286a0292f23SGarrett Wollman 	 *		if SEG.CC > CCrecv then is new SYN, and can implicitly
1287a0292f23SGarrett Wollman 	 *		    ack the FIN (and data) in retransmission queue.
1288a0292f23SGarrett Wollman 	 *                  Complete close and delete TCPCB.  Then reprocess
1289a0292f23SGarrett Wollman 	 *                  segment, hoping to find new TCPCB in LISTEN state;
1290a0292f23SGarrett Wollman 	 *
1291a0292f23SGarrett Wollman 	 *		else must be old SYN; drop it.
1292a0292f23SGarrett Wollman 	 *      else do normal processing.
1293a0292f23SGarrett Wollman 	 */
1294a0292f23SGarrett Wollman 	case TCPS_LAST_ACK:
1295a0292f23SGarrett Wollman 	case TCPS_CLOSING:
1296a0292f23SGarrett Wollman 	case TCPS_TIME_WAIT:
1297fb59c426SYoshinobu Inoue 		if ((thflags & TH_SYN) &&
1298be2ac88cSJonathan Lemon 		    (to.to_flags & TOF_CC) && tp->cc_recv != 0) {
1299a0292f23SGarrett Wollman 			if (tp->t_state == TCPS_TIME_WAIT &&
1300a57815efSBosko Milekic 					(ticks - tp->t_starttime) > tcp_msl) {
1301a57815efSBosko Milekic 				rstreason = BANDLIM_UNLIMITED;
1302a0292f23SGarrett Wollman 				goto dropwithreset;
1303a57815efSBosko Milekic 			}
1304a0292f23SGarrett Wollman 			if (CC_GT(to.to_cc, tp->cc_recv)) {
1305a0292f23SGarrett Wollman 				tp = tcp_close(tp);
1306a0292f23SGarrett Wollman 				goto findpcb;
1307a0292f23SGarrett Wollman 			}
1308a0292f23SGarrett Wollman 			else
1309a0292f23SGarrett Wollman 				goto drop;
1310a0292f23SGarrett Wollman 		}
1311a0292f23SGarrett Wollman  		break;  /* continue normal processing */
1312df8bae1dSRodney W. Grimes 	}
1313df8bae1dSRodney W. Grimes 
1314df8bae1dSRodney W. Grimes 	/*
1315df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
131680ab7c0eSGarrett Wollman 	 * First check the RST flag and sequence number since reset segments
131780ab7c0eSGarrett Wollman 	 * are exempt from the timestamp and connection count tests.  This
131880ab7c0eSGarrett Wollman 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
131980ab7c0eSGarrett Wollman 	 * below which allowed reset segments in half the sequence space
132080ab7c0eSGarrett Wollman 	 * to fall though and be processed (which gives forged reset
132180ab7c0eSGarrett Wollman 	 * segments with a random sequence number a 50 percent chance of
132280ab7c0eSGarrett Wollman 	 * killing a connection).
132380ab7c0eSGarrett Wollman 	 * Then check timestamp, if present.
1324a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
1325df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
1326df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
1327df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
1328df8bae1dSRodney W. Grimes 	 *
132980ab7c0eSGarrett Wollman 	 *
133080ab7c0eSGarrett Wollman 	 * If the RST bit is set, check the sequence number to see
133180ab7c0eSGarrett Wollman 	 * if this is a valid reset segment.
133280ab7c0eSGarrett Wollman 	 * RFC 793 page 37:
133380ab7c0eSGarrett Wollman 	 *   In all states except SYN-SENT, all reset (RST) segments
133480ab7c0eSGarrett Wollman 	 *   are validated by checking their SEQ-fields.  A reset is
133580ab7c0eSGarrett Wollman 	 *   valid if its sequence number is in the window.
133680ab7c0eSGarrett Wollman 	 * Note: this does not take into account delayed ACKs, so
133780ab7c0eSGarrett Wollman 	 *   we should test against last_ack_sent instead of rcv_nxt.
13381a244a61SJonathan Lemon 	 *   The sequence number in the reset segment is normally an
13391a244a61SJonathan Lemon 	 *   echo of our outgoing acknowlegement numbers, but some hosts
13401a244a61SJonathan Lemon 	 *   send a reset with the sequence number at the rightmost edge
13411a244a61SJonathan Lemon 	 *   of our receive window, and we have to handle this case.
134280ab7c0eSGarrett Wollman 	 * If we have multiple segments in flight, the intial reset
134380ab7c0eSGarrett Wollman 	 * segment sequence numbers will be to the left of last_ack_sent,
134480ab7c0eSGarrett Wollman 	 * but they will eventually catch up.
134580ab7c0eSGarrett Wollman 	 * In any case, it never made sense to trim reset segments to
134680ab7c0eSGarrett Wollman 	 * fit the receive window since RFC 1122 says:
134780ab7c0eSGarrett Wollman 	 *   4.2.2.12  RST Segment: RFC-793 Section 3.4
134880ab7c0eSGarrett Wollman 	 *
134980ab7c0eSGarrett Wollman 	 *    A TCP SHOULD allow a received RST segment to include data.
135080ab7c0eSGarrett Wollman 	 *
135180ab7c0eSGarrett Wollman 	 *    DISCUSSION
135280ab7c0eSGarrett Wollman 	 *         It has been suggested that a RST segment could contain
135380ab7c0eSGarrett Wollman 	 *         ASCII text that encoded and explained the cause of the
135480ab7c0eSGarrett Wollman 	 *         RST.  No standard has yet been established for such
135580ab7c0eSGarrett Wollman 	 *         data.
135680ab7c0eSGarrett Wollman 	 *
135780ab7c0eSGarrett Wollman 	 * If the reset segment passes the sequence number test examine
135880ab7c0eSGarrett Wollman 	 * the state:
135980ab7c0eSGarrett Wollman 	 *    SYN_RECEIVED STATE:
136080ab7c0eSGarrett Wollman 	 *	If passive open, return to LISTEN state.
136180ab7c0eSGarrett Wollman 	 *	If active open, inform user that connection was refused.
1362745bab7fSDima Dorfman 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES:
136380ab7c0eSGarrett Wollman 	 *	Inform user that connection was reset, and close tcb.
1364e9bd3a37SJonathan M. Bresler 	 *    CLOSING, LAST_ACK STATES:
136580ab7c0eSGarrett Wollman 	 *	Close the tcb.
1366e9bd3a37SJonathan M. Bresler 	 *    TIME_WAIT STATE:
136780ab7c0eSGarrett Wollman 	 *	Drop the segment - see Stevens, vol. 2, p. 964 and
136880ab7c0eSGarrett Wollman 	 *      RFC 1337.
136980ab7c0eSGarrett Wollman 	 */
1370fb59c426SYoshinobu Inoue 	if (thflags & TH_RST) {
1371fb59c426SYoshinobu Inoue 		if (SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
1372fb59c426SYoshinobu Inoue 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
137380ab7c0eSGarrett Wollman 			switch (tp->t_state) {
137480ab7c0eSGarrett Wollman 
137580ab7c0eSGarrett Wollman 			case TCPS_SYN_RECEIVED:
137680ab7c0eSGarrett Wollman 				so->so_error = ECONNREFUSED;
137780ab7c0eSGarrett Wollman 				goto close;
137880ab7c0eSGarrett Wollman 
137980ab7c0eSGarrett Wollman 			case TCPS_ESTABLISHED:
138080ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_1:
138180ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_2:
138280ab7c0eSGarrett Wollman 			case TCPS_CLOSE_WAIT:
138380ab7c0eSGarrett Wollman 				so->so_error = ECONNRESET;
138480ab7c0eSGarrett Wollman 			close:
138580ab7c0eSGarrett Wollman 				tp->t_state = TCPS_CLOSED;
138680ab7c0eSGarrett Wollman 				tcpstat.tcps_drops++;
138780ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
138880ab7c0eSGarrett Wollman 				break;
138980ab7c0eSGarrett Wollman 
139080ab7c0eSGarrett Wollman 			case TCPS_CLOSING:
139180ab7c0eSGarrett Wollman 			case TCPS_LAST_ACK:
139280ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
139380ab7c0eSGarrett Wollman 				break;
139480ab7c0eSGarrett Wollman 
139580ab7c0eSGarrett Wollman 			case TCPS_TIME_WAIT:
139680ab7c0eSGarrett Wollman 				break;
139780ab7c0eSGarrett Wollman 			}
139880ab7c0eSGarrett Wollman 		}
139980ab7c0eSGarrett Wollman 		goto drop;
140080ab7c0eSGarrett Wollman 	}
140180ab7c0eSGarrett Wollman 
140280ab7c0eSGarrett Wollman 	/*
1403df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
1404df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
1405df8bae1dSRodney W. Grimes 	 */
1406be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
140780ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
1408df8bae1dSRodney W. Grimes 
1409df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
14109b8b58e0SJonathan Lemon 		if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) {
1411df8bae1dSRodney W. Grimes 			/*
1412df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
1413df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
1414df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
1415df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
1416df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
1417df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
1418df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
1419df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
1420df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
1421df8bae1dSRodney W. Grimes 			 */
1422df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
1423df8bae1dSRodney W. Grimes 		} else {
1424df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvduppack++;
1425fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvdupbyte += tlen;
1426df8bae1dSRodney W. Grimes 			tcpstat.tcps_pawsdrop++;
1427df8bae1dSRodney W. Grimes 			goto dropafterack;
1428df8bae1dSRodney W. Grimes 		}
1429df8bae1dSRodney W. Grimes 	}
1430df8bae1dSRodney W. Grimes 
1431a0292f23SGarrett Wollman 	/*
1432a0292f23SGarrett Wollman 	 * T/TCP mechanism
1433a0292f23SGarrett Wollman 	 *   If T/TCP was negotiated and the segment doesn't have CC,
1434dc733423SDag-Erling Smørgrav 	 *   or if its CC is wrong then drop the segment.
1435a0292f23SGarrett Wollman 	 *   RST segments do not have to comply with this.
1436a0292f23SGarrett Wollman 	 */
1437a0292f23SGarrett Wollman 	if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) &&
1438be2ac88cSJonathan Lemon 	    ((to.to_flags & TOF_CC) == 0 || tp->cc_recv != to.to_cc))
1439a0292f23SGarrett Wollman  		goto dropafterack;
1440a0292f23SGarrett Wollman 
144180ab7c0eSGarrett Wollman 	/*
144280ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
144380ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
144480ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
144580ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
144680ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
144780ab7c0eSGarrett Wollman 	 */
1448a57815efSBosko Milekic 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
1449a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
1450a57815efSBosko Milekic 		goto dropwithreset;
1451a57815efSBosko Milekic 	}
145280ab7c0eSGarrett Wollman 
1453fb59c426SYoshinobu Inoue 	todrop = tp->rcv_nxt - th->th_seq;
1454df8bae1dSRodney W. Grimes 	if (todrop > 0) {
1455fb59c426SYoshinobu Inoue 		if (thflags & TH_SYN) {
1456fb59c426SYoshinobu Inoue 			thflags &= ~TH_SYN;
1457fb59c426SYoshinobu Inoue 			th->th_seq++;
1458fb59c426SYoshinobu Inoue 			if (th->th_urp > 1)
1459fb59c426SYoshinobu Inoue 				th->th_urp--;
1460df8bae1dSRodney W. Grimes 			else
1461fb59c426SYoshinobu Inoue 				thflags &= ~TH_URG;
1462df8bae1dSRodney W. Grimes 			todrop--;
1463df8bae1dSRodney W. Grimes 		}
1464df8bae1dSRodney W. Grimes 		/*
1465dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
1466df8bae1dSRodney W. Grimes 		 */
1467fb59c426SYoshinobu Inoue 		if (todrop > tlen
1468fb59c426SYoshinobu Inoue 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
1469dac20301SGarrett Wollman 			/*
1470dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
1471dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
1472dac20301SGarrett Wollman 			 * of sequence; drop it.
1473dac20301SGarrett Wollman 			 */
1474fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
1475dac20301SGarrett Wollman 
1476df8bae1dSRodney W. Grimes 			/*
1477dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
1478dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
1479df8bae1dSRodney W. Grimes 			 */
1480dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
1481fb59c426SYoshinobu Inoue 			todrop = tlen;
1482dac20301SGarrett Wollman 			tcpstat.tcps_rcvduppack++;
1483dac20301SGarrett Wollman 			tcpstat.tcps_rcvdupbyte += todrop;
1484df8bae1dSRodney W. Grimes 		} else {
1485df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpartduppack++;
1486df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpartdupbyte += todrop;
1487df8bae1dSRodney W. Grimes 		}
1488fb59c426SYoshinobu Inoue 		drop_hdrlen += todrop;	/* drop from the top afterwards */
1489fb59c426SYoshinobu Inoue 		th->th_seq += todrop;
1490fb59c426SYoshinobu Inoue 		tlen -= todrop;
1491fb59c426SYoshinobu Inoue 		if (th->th_urp > todrop)
1492fb59c426SYoshinobu Inoue 			th->th_urp -= todrop;
1493df8bae1dSRodney W. Grimes 		else {
1494fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;
1495fb59c426SYoshinobu Inoue 			th->th_urp = 0;
1496df8bae1dSRodney W. Grimes 		}
1497df8bae1dSRodney W. Grimes 	}
1498df8bae1dSRodney W. Grimes 
1499df8bae1dSRodney W. Grimes 	/*
1500df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
1501df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
1502df8bae1dSRodney W. Grimes 	 */
1503df8bae1dSRodney W. Grimes 	if ((so->so_state & SS_NOFDREF) &&
1504fb59c426SYoshinobu Inoue 	    tp->t_state > TCPS_CLOSE_WAIT && tlen) {
1505df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1506df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvafterclose++;
1507a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
1508df8bae1dSRodney W. Grimes 		goto dropwithreset;
1509df8bae1dSRodney W. Grimes 	}
1510df8bae1dSRodney W. Grimes 
1511df8bae1dSRodney W. Grimes 	/*
1512df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
1513df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
1514df8bae1dSRodney W. Grimes 	 */
1515fb59c426SYoshinobu Inoue 	todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd);
1516df8bae1dSRodney W. Grimes 	if (todrop > 0) {
1517df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvpackafterwin++;
1518fb59c426SYoshinobu Inoue 		if (todrop >= tlen) {
1519fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbyteafterwin += tlen;
1520df8bae1dSRodney W. Grimes 			/*
1521df8bae1dSRodney W. Grimes 			 * If a new connection request is received
1522df8bae1dSRodney W. Grimes 			 * while in TIME_WAIT, drop the old connection
1523df8bae1dSRodney W. Grimes 			 * and start over if the sequence numbers
1524df8bae1dSRodney W. Grimes 			 * are above the previous ones.
1525df8bae1dSRodney W. Grimes 			 */
1526fb59c426SYoshinobu Inoue 			if (thflags & TH_SYN &&
1527df8bae1dSRodney W. Grimes 			    tp->t_state == TCPS_TIME_WAIT &&
1528fb59c426SYoshinobu Inoue 			    SEQ_GT(th->th_seq, tp->rcv_nxt)) {
1529df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
1530df8bae1dSRodney W. Grimes 				goto findpcb;
1531df8bae1dSRodney W. Grimes 			}
1532df8bae1dSRodney W. Grimes 			/*
1533df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
1534df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
1535df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
1536df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
1537df8bae1dSRodney W. Grimes 			 * and ack.
1538df8bae1dSRodney W. Grimes 			 */
1539fb59c426SYoshinobu Inoue 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
1540df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
1541df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvwinprobe++;
1542df8bae1dSRodney W. Grimes 			} else
1543df8bae1dSRodney W. Grimes 				goto dropafterack;
1544df8bae1dSRodney W. Grimes 		} else
1545df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyteafterwin += todrop;
1546df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
1547fb59c426SYoshinobu Inoue 		tlen -= todrop;
1548fb59c426SYoshinobu Inoue 		thflags &= ~(TH_PUSH|TH_FIN);
1549df8bae1dSRodney W. Grimes 	}
1550df8bae1dSRodney W. Grimes 
1551df8bae1dSRodney W. Grimes 	/*
1552df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
1553df8bae1dSRodney W. Grimes 	 * record its timestamp.
1554a0292f23SGarrett Wollman 	 * NOTE that the test is modified according to the latest
1555a0292f23SGarrett Wollman 	 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1556df8bae1dSRodney W. Grimes 	 */
1557be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 &&
1558fb59c426SYoshinobu Inoue 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
15599b8b58e0SJonathan Lemon 		tp->ts_recent_age = ticks;
1560a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
1561df8bae1dSRodney W. Grimes 	}
1562df8bae1dSRodney W. Grimes 
1563df8bae1dSRodney W. Grimes 	/*
1564df8bae1dSRodney W. Grimes 	 * If a SYN is in the window, then this is an
1565df8bae1dSRodney W. Grimes 	 * error and we send an RST and drop the connection.
1566df8bae1dSRodney W. Grimes 	 */
1567fb59c426SYoshinobu Inoue 	if (thflags & TH_SYN) {
1568df8bae1dSRodney W. Grimes 		tp = tcp_drop(tp, ECONNRESET);
1569a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
1570df8bae1dSRodney W. Grimes 		goto dropwithreset;
1571df8bae1dSRodney W. Grimes 	}
1572df8bae1dSRodney W. Grimes 
1573a0292f23SGarrett Wollman 	/*
1574a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
1575a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
1576a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
1577a0292f23SGarrett Wollman 	 */
1578fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) == 0) {
1579a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
1580a0292f23SGarrett Wollman 		    (tp->t_flags & TF_NEEDSYN))
1581a0292f23SGarrett Wollman 			goto step6;
1582a0292f23SGarrett Wollman 		else
1583a0292f23SGarrett Wollman 			goto drop;
1584a0292f23SGarrett Wollman 	}
1585df8bae1dSRodney W. Grimes 
1586df8bae1dSRodney W. Grimes 	/*
1587df8bae1dSRodney W. Grimes 	 * Ack processing.
1588df8bae1dSRodney W. Grimes 	 */
1589df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1590df8bae1dSRodney W. Grimes 
1591df8bae1dSRodney W. Grimes 	/*
1592764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
1593764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
1594764d8cefSBill Fenner 	 * The ACK was checked above.
1595df8bae1dSRodney W. Grimes 	 */
1596df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1597a0292f23SGarrett Wollman 
1598df8bae1dSRodney W. Grimes 		tcpstat.tcps_connects++;
1599df8bae1dSRodney W. Grimes 		soisconnected(so);
1600df8bae1dSRodney W. Grimes 		/* Do window scaling? */
1601df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1602df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1603df8bae1dSRodney W. Grimes 			tp->snd_scale = tp->requested_s_scale;
1604df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
1605df8bae1dSRodney W. Grimes 		}
1606a0292f23SGarrett Wollman 		/*
1607a0292f23SGarrett Wollman 		 * Upon successful completion of 3-way handshake,
1608a0292f23SGarrett Wollman 		 * update cache.CC if it was undefined, pass any queued
1609a0292f23SGarrett Wollman 		 * data to the user, and advance state appropriately.
1610a0292f23SGarrett Wollman 		 */
1611be2ac88cSJonathan Lemon 		if ((taop = tcp_gettaocache(&inp->inp_inc)) != NULL &&
1612a0292f23SGarrett Wollman 		    taop->tao_cc == 0)
1613a0292f23SGarrett Wollman 			taop->tao_cc = tp->cc_recv;
1614a0292f23SGarrett Wollman 
1615a0292f23SGarrett Wollman 		/*
1616a0292f23SGarrett Wollman 		 * Make transitions:
1617a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
1618a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
1619a0292f23SGarrett Wollman 		 */
16209b8b58e0SJonathan Lemon 		tp->t_starttime = ticks;
1621a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDFIN) {
1622a0292f23SGarrett Wollman 			tp->t_state = TCPS_FIN_WAIT_1;
1623a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDFIN;
16247ff19458SPaul Traina 		} else {
1625a0292f23SGarrett Wollman 			tp->t_state = TCPS_ESTABLISHED;
16269b8b58e0SJonathan Lemon 			callout_reset(tp->tt_keep, tcp_keepidle,
16279b8b58e0SJonathan Lemon 				      tcp_timer_keep, tp);
16287ff19458SPaul Traina 		}
1629a0292f23SGarrett Wollman 		/*
1630a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
1631a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
1632a0292f23SGarrett Wollman 		 */
1633fb59c426SYoshinobu Inoue 		if (tlen == 0 && (thflags & TH_FIN) == 0)
1634fb59c426SYoshinobu Inoue 			(void) tcp_reass(tp, (struct tcphdr *)0, 0,
1635a0292f23SGarrett Wollman 			    (struct mbuf *)0);
1636fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
1637df8bae1dSRodney W. Grimes 		/* fall into ... */
1638df8bae1dSRodney W. Grimes 
1639df8bae1dSRodney W. Grimes 	/*
1640df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1641df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
1642fb59c426SYoshinobu Inoue 	 *	tp->snd_una < th->th_ack <= tp->snd_max
1643fb59c426SYoshinobu Inoue 	 * then advance tp->snd_una to th->th_ack and drop
1644df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
1645df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
1646df8bae1dSRodney W. Grimes 	 */
1647df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1648df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
1649df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
1650df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1651df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
1652df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
1653df8bae1dSRodney W. Grimes 	case TCPS_TIME_WAIT:
1654df8bae1dSRodney W. Grimes 
1655fb59c426SYoshinobu Inoue 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
1656fb59c426SYoshinobu Inoue 			if (tlen == 0 && tiwin == tp->snd_wnd) {
1657df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvdupack++;
1658df8bae1dSRodney W. Grimes 				/*
1659df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
1660df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
1661df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
1662df8bae1dSRodney W. Grimes 				 * change), the ack is the biggest we've
1663df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
1664df8bae1dSRodney W. Grimes 				 * threshhold of them, assume a packet
1665df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
1666df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
1667df8bae1dSRodney W. Grimes 				 * window so we send only this one
1668df8bae1dSRodney W. Grimes 				 * packet.
1669df8bae1dSRodney W. Grimes 				 *
1670df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
1671df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
1672df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
1673df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
1674df8bae1dSRodney W. Grimes 				 * the new ssthresh).
1675df8bae1dSRodney W. Grimes 				 *
1676df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
1677df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
1678df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
1679df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
1680df8bae1dSRodney W. Grimes 				 * network.
1681df8bae1dSRodney W. Grimes 				 */
16829b8b58e0SJonathan Lemon 				if (!callout_active(tp->tt_rexmt) ||
1683fb59c426SYoshinobu Inoue 				    th->th_ack != tp->snd_una)
1684df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
1685df8bae1dSRodney W. Grimes 				else if (++tp->t_dupacks == tcprexmtthresh) {
1686df8bae1dSRodney W. Grimes 					tcp_seq onxt = tp->snd_nxt;
1687df8bae1dSRodney W. Grimes 					u_int win =
1688df8bae1dSRodney W. Grimes 					    min(tp->snd_wnd, tp->snd_cwnd) / 2 /
1689df8bae1dSRodney W. Grimes 						tp->t_maxseg;
169046f58482SJonathan Lemon 					if (tcp_do_newreno && SEQ_LT(th->th_ack,
169146f58482SJonathan Lemon 					    tp->snd_recover)) {
169246f58482SJonathan Lemon 						/* False retransmit, should not
169346f58482SJonathan Lemon 						 * cut window
169446f58482SJonathan Lemon 						 */
169546f58482SJonathan Lemon 						tp->snd_cwnd += tp->t_maxseg;
169646f58482SJonathan Lemon 						tp->t_dupacks = 0;
169746f58482SJonathan Lemon 						(void) tcp_output(tp);
169846f58482SJonathan Lemon 						goto drop;
169946f58482SJonathan Lemon 					}
1700df8bae1dSRodney W. Grimes 					if (win < 2)
1701df8bae1dSRodney W. Grimes 						win = 2;
1702df8bae1dSRodney W. Grimes 					tp->snd_ssthresh = win * tp->t_maxseg;
170346f58482SJonathan Lemon 					tp->snd_recover = tp->snd_max;
17049b8b58e0SJonathan Lemon 					callout_stop(tp->tt_rexmt);
17059b8b58e0SJonathan Lemon 					tp->t_rtttime = 0;
1706fb59c426SYoshinobu Inoue 					tp->snd_nxt = th->th_ack;
1707df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->t_maxseg;
1708df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
1709df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
1710df8bae1dSRodney W. Grimes 					       tp->t_maxseg * tp->t_dupacks;
1711df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
1712df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
1713df8bae1dSRodney W. Grimes 					goto drop;
1714df8bae1dSRodney W. Grimes 				} else if (tp->t_dupacks > tcprexmtthresh) {
1715df8bae1dSRodney W. Grimes 					tp->snd_cwnd += tp->t_maxseg;
1716df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
1717df8bae1dSRodney W. Grimes 					goto drop;
1718df8bae1dSRodney W. Grimes 				}
1719df8bae1dSRodney W. Grimes 			} else
1720df8bae1dSRodney W. Grimes 				tp->t_dupacks = 0;
1721df8bae1dSRodney W. Grimes 			break;
1722df8bae1dSRodney W. Grimes 		}
1723df8bae1dSRodney W. Grimes 		/*
1724df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
1725df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
1726df8bae1dSRodney W. Grimes 		 */
172746f58482SJonathan Lemon 		if (tcp_do_newreno == 0) {
1728233e8c18SGarrett Wollman                         if (tp->t_dupacks >= tcprexmtthresh &&
1729df8bae1dSRodney W. Grimes                                 tp->snd_cwnd > tp->snd_ssthresh)
1730df8bae1dSRodney W. Grimes                                 tp->snd_cwnd = tp->snd_ssthresh;
1731df8bae1dSRodney W. Grimes                         tp->t_dupacks = 0;
173246f58482SJonathan Lemon                 } else if (tp->t_dupacks >= tcprexmtthresh &&
173346f58482SJonathan Lemon 		    !tcp_newreno(tp, th)) {
173446f58482SJonathan Lemon                         /*
173546f58482SJonathan Lemon                          * Window inflation should have left us with approx.
173646f58482SJonathan Lemon                          * snd_ssthresh outstanding data.  But in case we
173746f58482SJonathan Lemon                          * would be inclined to send a burst, better to do
173846f58482SJonathan Lemon                          * it via the slow start mechanism.
173946f58482SJonathan Lemon                          */
174046f58482SJonathan Lemon 			if (SEQ_GT(th->th_ack + tp->snd_ssthresh, tp->snd_max))
174146f58482SJonathan Lemon                                 tp->snd_cwnd =
174246f58482SJonathan Lemon 				    tp->snd_max - th->th_ack + tp->t_maxseg;
174346f58482SJonathan Lemon 			else
174446f58482SJonathan Lemon                         	tp->snd_cwnd = tp->snd_ssthresh;
174546f58482SJonathan Lemon                         tp->t_dupacks = 0;
174646f58482SJonathan Lemon                 }
1747e7e2b801SJayanth Vijayaraghavan 		if (tp->t_dupacks < tcprexmtthresh)
1748e7e2b801SJayanth Vijayaraghavan 			tp->t_dupacks = 0;
1749fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
1750df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvacktoomuch++;
1751df8bae1dSRodney W. Grimes 			goto dropafterack;
1752df8bae1dSRodney W. Grimes 		}
1753a0292f23SGarrett Wollman 		/*
1754a0292f23SGarrett Wollman 		 *  If we reach this point, ACK is not a duplicate,
1755a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
1756a0292f23SGarrett Wollman 		 */
1757a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
1758a0292f23SGarrett Wollman 			/*
1759a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
1760a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
176107e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
176207e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
176307e43e10SAndras Olah 			 * we can do window scaling.
1764a0292f23SGarrett Wollman 			 */
1765a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
1766a0292f23SGarrett Wollman 			tp->snd_una++;
176707e43e10SAndras Olah 			/* Do window scaling? */
176807e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
176907e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
177007e43e10SAndras Olah 				tp->snd_scale = tp->requested_s_scale;
177107e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
177207e43e10SAndras Olah 			}
1773a0292f23SGarrett Wollman 		}
1774a0292f23SGarrett Wollman 
1775a0292f23SGarrett Wollman process_ACK:
1776fb59c426SYoshinobu Inoue 		acked = th->th_ack - tp->snd_una;
1777df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvackpack++;
1778df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvackbyte += acked;
1779df8bae1dSRodney W. Grimes 
1780df8bae1dSRodney W. Grimes 		/*
17819b8b58e0SJonathan Lemon 		 * If we just performed our first retransmit, and the ACK
17829b8b58e0SJonathan Lemon 		 * arrives within our recovery window, then it was a mistake
17839b8b58e0SJonathan Lemon 		 * to do the retransmit in the first place.  Recover our
17849b8b58e0SJonathan Lemon 		 * original cwnd and ssthresh, and proceed to transmit where
17859b8b58e0SJonathan Lemon 		 * we left off.
17869b8b58e0SJonathan Lemon 		 */
17879b8b58e0SJonathan Lemon 		if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) {
1788d65bf08aSMatthew Dillon 			++tcpstat.tcps_sndrexmitbad;
17899b8b58e0SJonathan Lemon 			tp->snd_cwnd = tp->snd_cwnd_prev;
17909b8b58e0SJonathan Lemon 			tp->snd_ssthresh = tp->snd_ssthresh_prev;
17919b8b58e0SJonathan Lemon 			tp->snd_nxt = tp->snd_max;
17929b8b58e0SJonathan Lemon 			tp->t_badrxtwin = 0;	/* XXX probably not required */
17939b8b58e0SJonathan Lemon 		}
17949b8b58e0SJonathan Lemon 
17959b8b58e0SJonathan Lemon 		/*
1796df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
1797df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
1798df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
1799df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
1800df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
1801df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
1802df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
1803df8bae1dSRodney W. Grimes 		 */
1804be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_TS)
18059b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - to.to_tsecr + 1);
1806fb59c426SYoshinobu Inoue 		else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq))
18079b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
1808df8bae1dSRodney W. Grimes 
1809df8bae1dSRodney W. Grimes 		/*
1810df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
1811df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
1812df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
1813df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
1814df8bae1dSRodney W. Grimes 		 */
1815fb59c426SYoshinobu Inoue 		if (th->th_ack == tp->snd_max) {
18169b8b58e0SJonathan Lemon 			callout_stop(tp->tt_rexmt);
1817df8bae1dSRodney W. Grimes 			needoutput = 1;
18189b8b58e0SJonathan Lemon 		} else if (!callout_active(tp->tt_persist))
18199b8b58e0SJonathan Lemon 			callout_reset(tp->tt_rexmt, tp->t_rxtcur,
18209b8b58e0SJonathan Lemon 				      tcp_timer_rexmt, tp);
1821a0292f23SGarrett Wollman 
1822a0292f23SGarrett Wollman 		/*
1823a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
1824a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
1825a0292f23SGarrett Wollman 		 */
1826a0292f23SGarrett Wollman 		if (acked == 0)
1827a0292f23SGarrett Wollman 			goto step6;
1828a0292f23SGarrett Wollman 
1829df8bae1dSRodney W. Grimes 		/*
1830df8bae1dSRodney W. Grimes 		 * When new data is acked, open the congestion window.
1831df8bae1dSRodney W. Grimes 		 * If the window gives us less than ssthresh packets
1832df8bae1dSRodney W. Grimes 		 * in flight, open exponentially (maxseg per packet).
1833df8bae1dSRodney W. Grimes 		 * Otherwise open linearly: maxseg per window
183410be5648SGarrett Wollman 		 * (maxseg^2 / cwnd per packet).
1835df8bae1dSRodney W. Grimes 		 */
1836df8bae1dSRodney W. Grimes 		{
1837df8bae1dSRodney W. Grimes 		register u_int cw = tp->snd_cwnd;
1838df8bae1dSRodney W. Grimes 		register u_int incr = tp->t_maxseg;
1839df8bae1dSRodney W. Grimes 
1840df8bae1dSRodney W. Grimes 		if (cw > tp->snd_ssthresh)
184110be5648SGarrett Wollman 			incr = incr * incr / cw;
18428735719eSJonathan Lemon 		/*
18438735719eSJonathan Lemon 		 * If t_dupacks != 0 here, it indicates that we are still
18448735719eSJonathan Lemon 		 * in NewReno fast recovery mode, so we leave the congestion
18458735719eSJonathan Lemon 		 * window alone.
18468735719eSJonathan Lemon 		 */
18478735719eSJonathan Lemon 		if (tcp_do_newreno == 0 || tp->t_dupacks == 0)
1848df8bae1dSRodney W. Grimes 			tp->snd_cwnd = min(cw + incr,TCP_MAXWIN<<tp->snd_scale);
1849df8bae1dSRodney W. Grimes 		}
1850df8bae1dSRodney W. Grimes 		if (acked > so->so_snd.sb_cc) {
1851df8bae1dSRodney W. Grimes 			tp->snd_wnd -= so->so_snd.sb_cc;
1852df8bae1dSRodney W. Grimes 			sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
1853df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
1854df8bae1dSRodney W. Grimes 		} else {
1855df8bae1dSRodney W. Grimes 			sbdrop(&so->so_snd, acked);
1856df8bae1dSRodney W. Grimes 			tp->snd_wnd -= acked;
1857df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
1858df8bae1dSRodney W. Grimes 		}
1859df8bae1dSRodney W. Grimes 		sowwakeup(so);
1860fb59c426SYoshinobu Inoue 		tp->snd_una = th->th_ack;
1861df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
1862df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
1863df8bae1dSRodney W. Grimes 
1864df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
1865df8bae1dSRodney W. Grimes 
1866df8bae1dSRodney W. Grimes 		/*
1867df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
1868df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
1869df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
1870df8bae1dSRodney W. Grimes 		 */
1871df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
1872df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
1873df8bae1dSRodney W. Grimes 				/*
1874df8bae1dSRodney W. Grimes 				 * If we can't receive any more
1875df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
1876df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
1877df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
1878df8bae1dSRodney W. Grimes 				 * we'll hang forever.
1879df8bae1dSRodney W. Grimes 				 */
1880df8bae1dSRodney W. Grimes 				if (so->so_state & SS_CANTRCVMORE) {
188103e49181SSeigo Tanimura 					soisdisconnected(so);
18829b8b58e0SJonathan Lemon 					callout_reset(tp->tt_2msl, tcp_maxidle,
18839b8b58e0SJonathan Lemon 						      tcp_timer_2msl, tp);
18844cc20ab1SSeigo Tanimura 				}
1885df8bae1dSRodney W. Grimes 				tp->t_state = TCPS_FIN_WAIT_2;
1886df8bae1dSRodney W. Grimes 			}
1887df8bae1dSRodney W. Grimes 			break;
1888df8bae1dSRodney W. Grimes 
1889df8bae1dSRodney W. Grimes 	 	/*
1890df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
1891df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
1892df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
1893df8bae1dSRodney W. Grimes 		 * the segment.
1894df8bae1dSRodney W. Grimes 		 */
1895df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
1896df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
1897df8bae1dSRodney W. Grimes 				tp->t_state = TCPS_TIME_WAIT;
1898df8bae1dSRodney W. Grimes 				tcp_canceltimers(tp);
1899a0292f23SGarrett Wollman 				/* Shorten TIME_WAIT [RFC-1644, p.28] */
1900a0292f23SGarrett Wollman 				if (tp->cc_recv != 0 &&
19019b8b58e0SJonathan Lemon 				    (ticks - tp->t_starttime) < tcp_msl)
19029b8b58e0SJonathan Lemon 					callout_reset(tp->tt_2msl,
19039b8b58e0SJonathan Lemon 						      tp->t_rxtcur *
19049b8b58e0SJonathan Lemon 						      TCPTV_TWTRUNC,
19059b8b58e0SJonathan Lemon 						      tcp_timer_2msl, tp);
1906a0292f23SGarrett Wollman 				else
19079b8b58e0SJonathan Lemon 					callout_reset(tp->tt_2msl, 2 * tcp_msl,
19089b8b58e0SJonathan Lemon 						      tcp_timer_2msl, tp);
1909df8bae1dSRodney W. Grimes 				soisdisconnected(so);
1910df8bae1dSRodney W. Grimes 			}
1911df8bae1dSRodney W. Grimes 			break;
1912df8bae1dSRodney W. Grimes 
1913df8bae1dSRodney W. Grimes 		/*
1914df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
1915df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
1916df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
1917df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
1918df8bae1dSRodney W. Grimes 		 */
1919df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
1920df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
1921df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
1922df8bae1dSRodney W. Grimes 				goto drop;
1923df8bae1dSRodney W. Grimes 			}
1924df8bae1dSRodney W. Grimes 			break;
1925df8bae1dSRodney W. Grimes 
1926df8bae1dSRodney W. Grimes 		/*
1927df8bae1dSRodney W. Grimes 		 * In TIME_WAIT state the only thing that should arrive
1928df8bae1dSRodney W. Grimes 		 * is a retransmission of the remote FIN.  Acknowledge
1929df8bae1dSRodney W. Grimes 		 * it and restart the finack timer.
1930df8bae1dSRodney W. Grimes 		 */
1931df8bae1dSRodney W. Grimes 		case TCPS_TIME_WAIT:
19329b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, 2 * tcp_msl,
19339b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
1934df8bae1dSRodney W. Grimes 			goto dropafterack;
1935df8bae1dSRodney W. Grimes 		}
1936df8bae1dSRodney W. Grimes 	}
1937df8bae1dSRodney W. Grimes 
1938df8bae1dSRodney W. Grimes step6:
1939df8bae1dSRodney W. Grimes 	/*
1940df8bae1dSRodney W. Grimes 	 * Update window information.
1941df8bae1dSRodney W. Grimes 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
1942df8bae1dSRodney W. Grimes 	 */
1943fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) &&
1944fb59c426SYoshinobu Inoue 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
1945fb59c426SYoshinobu Inoue 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
1946fb59c426SYoshinobu Inoue 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
1947df8bae1dSRodney W. Grimes 		/* keep track of pure window updates */
1948fb59c426SYoshinobu Inoue 		if (tlen == 0 &&
1949fb59c426SYoshinobu Inoue 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
1950df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvwinupd++;
1951df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
1952fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq;
1953fb59c426SYoshinobu Inoue 		tp->snd_wl2 = th->th_ack;
1954df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
1955df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
1956df8bae1dSRodney W. Grimes 		needoutput = 1;
1957df8bae1dSRodney W. Grimes 	}
1958df8bae1dSRodney W. Grimes 
1959df8bae1dSRodney W. Grimes 	/*
1960df8bae1dSRodney W. Grimes 	 * Process segments with URG.
1961df8bae1dSRodney W. Grimes 	 */
1962fb59c426SYoshinobu Inoue 	if ((thflags & TH_URG) && th->th_urp &&
1963df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1964df8bae1dSRodney W. Grimes 		/*
1965df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
1966df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
1967df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
1968df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
1969df8bae1dSRodney W. Grimes 		 */
1970fb59c426SYoshinobu Inoue 		if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
1971fb59c426SYoshinobu Inoue 			th->th_urp = 0;			/* XXX */
1972fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;		/* XXX */
1973df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
1974df8bae1dSRodney W. Grimes 		}
1975df8bae1dSRodney W. Grimes 		/*
1976df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
1977df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
1978df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
1979df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
1980df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
1981df8bae1dSRodney W. Grimes 		 *
1982df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
1983df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
1984df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
1985df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
1986df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
1987df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
1988df8bae1dSRodney W. Grimes 		 */
1989fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
1990fb59c426SYoshinobu Inoue 			tp->rcv_up = th->th_seq + th->th_urp;
1991df8bae1dSRodney W. Grimes 			so->so_oobmark = so->so_rcv.sb_cc +
1992df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
19934cc20ab1SSeigo Tanimura 			if (so->so_oobmark == 0)
1994df8bae1dSRodney W. Grimes 				so->so_state |= SS_RCVATMARK;
1995df8bae1dSRodney W. Grimes 			sohasoutofband(so);
1996df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1997df8bae1dSRodney W. Grimes 		}
1998df8bae1dSRodney W. Grimes 		/*
1999df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
2000df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
2001df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
2002df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
2003df8bae1dSRodney W. Grimes 		 */
20044cc20ab1SSeigo Tanimura 		if (th->th_urp <= (u_long)tlen
2005df8bae1dSRodney W. Grimes #ifdef SO_OOBINLINE
20064cc20ab1SSeigo Tanimura 		     && (so->so_options & SO_OOBINLINE) == 0
2007df8bae1dSRodney W. Grimes #endif
20084cc20ab1SSeigo Tanimura 		     )
2009fb59c426SYoshinobu Inoue 			tcp_pulloutofband(so, th, m,
2010fb59c426SYoshinobu Inoue 				drop_hdrlen);	/* hdr drop is delayed */
2011df8bae1dSRodney W. Grimes 	} else
2012df8bae1dSRodney W. Grimes 		/*
2013df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
2014df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
2015df8bae1dSRodney W. Grimes 		 * with the receive window.
2016df8bae1dSRodney W. Grimes 		 */
2017df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
2018df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
2019df8bae1dSRodney W. Grimes dodata:							/* XXX */
2020f76fcf6dSJeffrey Hsu 	KASSERT(headlocked, ("headlocked"));
2021f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&tcbinfo);
2022f76fcf6dSJeffrey Hsu 	headlocked = 0;
2023df8bae1dSRodney W. Grimes 	/*
2024df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
2025df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
2026df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
2027df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
2028df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
2029df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
2030df8bae1dSRodney W. Grimes 	 */
2031fb59c426SYoshinobu Inoue 	if ((tlen || (thflags&TH_FIN)) &&
2032df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2033fb59c426SYoshinobu Inoue 		m_adj(m, drop_hdrlen);	/* delayed header drop */
2034e4b64281SJesper Skriver 		/*
2035e4b64281SJesper Skriver 		 * Insert segment which inludes th into reassembly queue of tcp with
2036e4b64281SJesper Skriver 		 * control block tp.  Return TH_FIN if reassembly now includes
2037e4b64281SJesper Skriver 		 * a segment with FIN.  This handle the common case inline (segment
2038e4b64281SJesper Skriver 		 * is the next to be received on an established connection, and the
2039e4b64281SJesper Skriver 		 * queue is empty), avoiding linkage into and removal from the queue
2040e4b64281SJesper Skriver 		 * and repetition of various conversions.
2041e4b64281SJesper Skriver 		 * Set DELACK for segments received in order, but ack immediately
2042e4b64281SJesper Skriver 		 * when segments are out of order (so fast retransmit can work).
2043e4b64281SJesper Skriver 		 */
2044e4b64281SJesper Skriver 		if (th->th_seq == tp->rcv_nxt &&
2045e4b64281SJesper Skriver 		    LIST_EMPTY(&tp->t_segq) &&
2046e4b64281SJesper Skriver 		    TCPS_HAVEESTABLISHED(tp->t_state)) {
2047e4b64281SJesper Skriver 			if (DELAY_ACK(tp))
2048e4b64281SJesper Skriver 				callout_reset(tp->tt_delack, tcp_delacktime,
2049e4b64281SJesper Skriver 				    tcp_timer_delack, tp);
2050e4b64281SJesper Skriver 			else
2051e4b64281SJesper Skriver 				tp->t_flags |= TF_ACKNOW;
2052e4b64281SJesper Skriver 			tp->rcv_nxt += tlen;
2053e4b64281SJesper Skriver 			thflags = th->th_flags & TH_FIN;
2054e4b64281SJesper Skriver 			tcpstat.tcps_rcvpack++;
2055e4b64281SJesper Skriver 			tcpstat.tcps_rcvbyte += tlen;
2056e4b64281SJesper Skriver 			ND6_HINT(tp);
2057e4b64281SJesper Skriver 			sbappend(&so->so_rcv, m);
2058e4b64281SJesper Skriver 			sorwakeup(so);
2059e4b64281SJesper Skriver 		} else {
2060e4b64281SJesper Skriver 			thflags = tcp_reass(tp, th, &tlen, m);
2061e4b64281SJesper Skriver 			tp->t_flags |= TF_ACKNOW;
2062e4b64281SJesper Skriver 		}
2063e4b64281SJesper Skriver 
2064df8bae1dSRodney W. Grimes 		/*
2065df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
2066df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
2067df8bae1dSRodney W. Grimes 		 * buffer size.
2068df8bae1dSRodney W. Grimes 		 */
2069df8bae1dSRodney W. Grimes 		len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
2070df8bae1dSRodney W. Grimes 	} else {
2071df8bae1dSRodney W. Grimes 		m_freem(m);
2072fb59c426SYoshinobu Inoue 		thflags &= ~TH_FIN;
2073df8bae1dSRodney W. Grimes 	}
2074df8bae1dSRodney W. Grimes 
2075df8bae1dSRodney W. Grimes 	/*
2076df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
2077df8bae1dSRodney W. Grimes 	 * that the connection is closing.
2078df8bae1dSRodney W. Grimes 	 */
2079fb59c426SYoshinobu Inoue 	if (thflags & TH_FIN) {
2080df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2081df8bae1dSRodney W. Grimes 			socantrcvmore(so);
2082a0292f23SGarrett Wollman 			/*
2083a0292f23SGarrett Wollman 			 *  If connection is half-synchronized
2084d3eede9dSAndras Olah 			 *  (ie NEEDSYN flag on) then delay ACK,
2085a0292f23SGarrett Wollman 			 *  so it may be piggybacked when SYN is sent.
2086a0292f23SGarrett Wollman 			 *  Otherwise, since we received a FIN then no
2087a0292f23SGarrett Wollman 			 *  more input can be expected, send ACK now.
2088a0292f23SGarrett Wollman 			 */
2089d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp) && (tp->t_flags & TF_NEEDSYN))
20909b8b58e0SJonathan Lemon                                 callout_reset(tp->tt_delack, tcp_delacktime,
20919b8b58e0SJonathan Lemon                                     tcp_timer_delack, tp);
2092a0292f23SGarrett Wollman 			else
2093df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
2094df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
2095df8bae1dSRodney W. Grimes 		}
2096df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2097df8bae1dSRodney W. Grimes 
2098df8bae1dSRodney W. Grimes 	 	/*
2099df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
2100df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
2101df8bae1dSRodney W. Grimes 		 */
2102df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
21039b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
21049b8b58e0SJonathan Lemon 			/*FALLTHROUGH*/
2105df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
2106df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSE_WAIT;
2107df8bae1dSRodney W. Grimes 			break;
2108df8bae1dSRodney W. Grimes 
2109df8bae1dSRodney W. Grimes 	 	/*
2110df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2111df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
2112df8bae1dSRodney W. Grimes 		 */
2113df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2114df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSING;
2115df8bae1dSRodney W. Grimes 			break;
2116df8bae1dSRodney W. Grimes 
2117df8bae1dSRodney W. Grimes 	 	/*
2118df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2119df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
2120df8bae1dSRodney W. Grimes 		 * standard timers.
2121df8bae1dSRodney W. Grimes 		 */
2122df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
2123df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_TIME_WAIT;
2124df8bae1dSRodney W. Grimes 			tcp_canceltimers(tp);
2125a0292f23SGarrett Wollman 			/* Shorten TIME_WAIT [RFC-1644, p.28] */
2126a0292f23SGarrett Wollman 			if (tp->cc_recv != 0 &&
21279b8b58e0SJonathan Lemon 			    (ticks - tp->t_starttime) < tcp_msl) {
21289b8b58e0SJonathan Lemon 				callout_reset(tp->tt_2msl,
21299b8b58e0SJonathan Lemon 					      tp->t_rxtcur * TCPTV_TWTRUNC,
21309b8b58e0SJonathan Lemon 					      tcp_timer_2msl, tp);
2131a0292f23SGarrett Wollman 				/* For transaction client, force ACK now. */
2132a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
2133a0292f23SGarrett Wollman 			}
2134a0292f23SGarrett Wollman 			else
21359b8b58e0SJonathan Lemon 				callout_reset(tp->tt_2msl, 2 * tcp_msl,
21369b8b58e0SJonathan Lemon 					      tcp_timer_2msl, tp);
2137df8bae1dSRodney W. Grimes 			soisdisconnected(so);
2138df8bae1dSRodney W. Grimes 			break;
2139df8bae1dSRodney W. Grimes 
2140df8bae1dSRodney W. Grimes 		/*
2141df8bae1dSRodney W. Grimes 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
2142df8bae1dSRodney W. Grimes 		 */
2143df8bae1dSRodney W. Grimes 		case TCPS_TIME_WAIT:
21449b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, 2 * tcp_msl,
21459b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
2146df8bae1dSRodney W. Grimes 			break;
2147df8bae1dSRodney W. Grimes 		}
2148df8bae1dSRodney W. Grimes 	}
2149610ee2f9SDavid Greenman #ifdef TCPDEBUG
21504cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
2151fb59c426SYoshinobu Inoue 		tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
2152fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2153610ee2f9SDavid Greenman #endif
2154df8bae1dSRodney W. Grimes 
2155df8bae1dSRodney W. Grimes 	/*
2156df8bae1dSRodney W. Grimes 	 * Return any desired output.
2157df8bae1dSRodney W. Grimes 	 */
2158df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
2159df8bae1dSRodney W. Grimes 		(void) tcp_output(tp);
2160f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
2161df8bae1dSRodney W. Grimes 	return;
2162df8bae1dSRodney W. Grimes 
2163df8bae1dSRodney W. Grimes dropafterack:
2164df8bae1dSRodney W. Grimes 	/*
2165df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
2166df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
216780ab7c0eSGarrett Wollman 	 *
216880ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
216980ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
217080ab7c0eSGarrett Wollman 	 * RST have been dropped.
217180ab7c0eSGarrett Wollman 	 *
217280ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
217380ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
217480ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
217580ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
217680ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
217780ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
2178df8bae1dSRodney W. Grimes 	 */
2179fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
2180fb59c426SYoshinobu Inoue 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
2181a57815efSBosko Milekic 	     SEQ_GT(th->th_ack, tp->snd_max)) ) {
2182a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2183a57815efSBosko Milekic 		goto dropwithreset;
2184a57815efSBosko Milekic 	}
2185a0292f23SGarrett Wollman #ifdef TCPDEBUG
21864cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
2187fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2188fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2189a0292f23SGarrett Wollman #endif
2190f76fcf6dSJeffrey Hsu 	if (headlocked)
2191f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&tcbinfo);
2192df8bae1dSRodney W. Grimes 	m_freem(m);
2193df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
2194df8bae1dSRodney W. Grimes 	(void) tcp_output(tp);
2195f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
2196df8bae1dSRodney W. Grimes 	return;
2197df8bae1dSRodney W. Grimes 
2198df8bae1dSRodney W. Grimes dropwithreset:
2199df8bae1dSRodney W. Grimes 	/*
2200df8bae1dSRodney W. Grimes 	 * Generate a RST, dropping incoming segment.
2201df8bae1dSRodney W. Grimes 	 * Make ACK acceptable to originator of segment.
2202df8bae1dSRodney W. Grimes 	 * Don't bother to respond if destination was broadcast/multicast.
2203df8bae1dSRodney W. Grimes 	 */
2204fb59c426SYoshinobu Inoue 	if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
2205df8bae1dSRodney W. Grimes 		goto drop;
2206fb59c426SYoshinobu Inoue #ifdef INET6
2207fb59c426SYoshinobu Inoue 	if (isipv6) {
2208173c0f9fSWarner Losh 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
2209173c0f9fSWarner Losh 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
2210fb59c426SYoshinobu Inoue 			goto drop;
2211fb59c426SYoshinobu Inoue 	} else
2212fb59c426SYoshinobu Inoue #endif /* INET6 */
2213173c0f9fSWarner Losh 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
2214173c0f9fSWarner Losh 	    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
22152ca2159fSCrist J. Clark 	    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
22162ca2159fSCrist J. Clark 	    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
2217fb59c426SYoshinobu Inoue 		goto drop;
2218fb59c426SYoshinobu Inoue 	/* IPv6 anycast check is done at tcp6_input() */
2219a57815efSBosko Milekic 
2220a57815efSBosko Milekic 	/*
2221c59319bfSDag-Erling Smørgrav 	 * Perform bandwidth limiting.
2222a57815efSBosko Milekic 	 */
2223a57815efSBosko Milekic 	if (badport_bandlim(rstreason) < 0)
2224a57815efSBosko Milekic 		goto drop;
2225a57815efSBosko Milekic 
2226a0292f23SGarrett Wollman #ifdef TCPDEBUG
22274cc20ab1SSeigo Tanimura 	if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
2228fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2229fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2230a0292f23SGarrett Wollman #endif
22316fd22cafSJeffrey Hsu 
22326fd22cafSJeffrey Hsu 	if (tp)
22336fd22cafSJeffrey Hsu 		INP_UNLOCK(inp);
22346fd22cafSJeffrey Hsu 
2235fb59c426SYoshinobu Inoue 	if (thflags & TH_ACK)
2236fb59c426SYoshinobu Inoue 		/* mtod() below is safe as long as hdr dropping is delayed */
2237fb59c426SYoshinobu Inoue 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack,
2238fb59c426SYoshinobu Inoue 			    TH_RST);
2239df8bae1dSRodney W. Grimes 	else {
2240fb59c426SYoshinobu Inoue 		if (thflags & TH_SYN)
2241fb59c426SYoshinobu Inoue 			tlen++;
2242fb59c426SYoshinobu Inoue 		/* mtod() below is safe as long as hdr dropping is delayed */
2243fb59c426SYoshinobu Inoue 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
2244fb59c426SYoshinobu Inoue 			    (tcp_seq)0, TH_RST|TH_ACK);
2245df8bae1dSRodney W. Grimes 	}
2246f76fcf6dSJeffrey Hsu 	if (headlocked)
2247f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&tcbinfo);
2248df8bae1dSRodney W. Grimes 	return;
2249df8bae1dSRodney W. Grimes 
2250df8bae1dSRodney W. Grimes drop:
2251df8bae1dSRodney W. Grimes 	/*
2252df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
2253df8bae1dSRodney W. Grimes 	 */
2254610ee2f9SDavid Greenman #ifdef TCPDEBUG
22554cc20ab1SSeigo Tanimura 	if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
2256fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2257fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2258a0292f23SGarrett Wollman #endif
2259f76fcf6dSJeffrey Hsu 	if (tp)
2260f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
2261df8bae1dSRodney W. Grimes 	m_freem(m);
2262f76fcf6dSJeffrey Hsu 	if (headlocked)
2263f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&tcbinfo);
2264df8bae1dSRodney W. Grimes 	return;
2265df8bae1dSRodney W. Grimes }
2266df8bae1dSRodney W. Grimes 
2267be2ac88cSJonathan Lemon /*
2268be2ac88cSJonathan Lemon  * Parse TCP options and place in tcpopt.
2269be2ac88cSJonathan Lemon  */
22700312fbe9SPoul-Henning Kamp static void
2271be2ac88cSJonathan Lemon tcp_dooptions(to, cp, cnt, is_syn)
2272be2ac88cSJonathan Lemon 	struct tcpopt *to;
2273df8bae1dSRodney W. Grimes 	u_char *cp;
2274df8bae1dSRodney W. Grimes 	int cnt;
2275df8bae1dSRodney W. Grimes {
2276df8bae1dSRodney W. Grimes 	int opt, optlen;
2277df8bae1dSRodney W. Grimes 
2278be2ac88cSJonathan Lemon 	to->to_flags = 0;
2279df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
2280df8bae1dSRodney W. Grimes 		opt = cp[0];
2281df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
2282df8bae1dSRodney W. Grimes 			break;
2283df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
2284df8bae1dSRodney W. Grimes 			optlen = 1;
2285df8bae1dSRodney W. Grimes 		else {
2286b474779fSJun-ichiro itojun Hagino 			if (cnt < 2)
2287b474779fSJun-ichiro itojun Hagino 				break;
2288df8bae1dSRodney W. Grimes 			optlen = cp[1];
2289b474779fSJun-ichiro itojun Hagino 			if (optlen < 2 || optlen > cnt)
2290df8bae1dSRodney W. Grimes 				break;
2291df8bae1dSRodney W. Grimes 		}
2292df8bae1dSRodney W. Grimes 		switch (opt) {
2293df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
2294df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
2295df8bae1dSRodney W. Grimes 				continue;
2296be2ac88cSJonathan Lemon 			if (!is_syn)
2297df8bae1dSRodney W. Grimes 				continue;
2298be2ac88cSJonathan Lemon 			to->to_flags |= TOF_MSS;
2299be2ac88cSJonathan Lemon 			bcopy((char *)cp + 2,
2300be2ac88cSJonathan Lemon 			    (char *)&to->to_mss, sizeof(to->to_mss));
2301fd8e4ebcSMike Barcroft 			to->to_mss = ntohs(to->to_mss);
2302df8bae1dSRodney W. Grimes 			break;
2303df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
2304df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
2305df8bae1dSRodney W. Grimes 				continue;
2306be2ac88cSJonathan Lemon 			if (! is_syn)
2307df8bae1dSRodney W. Grimes 				continue;
2308be2ac88cSJonathan Lemon 			to->to_flags |= TOF_SCALE;
2309be2ac88cSJonathan Lemon 			to->to_requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
2310df8bae1dSRodney W. Grimes 			break;
2311df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
2312df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
2313df8bae1dSRodney W. Grimes 				continue;
2314be2ac88cSJonathan Lemon 			to->to_flags |= TOF_TS;
2315a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2316a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
2317fd8e4ebcSMike Barcroft 			to->to_tsval = ntohl(to->to_tsval);
2318a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
2319a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
2320fd8e4ebcSMike Barcroft 			to->to_tsecr = ntohl(to->to_tsecr);
2321df8bae1dSRodney W. Grimes 			break;
2322a0292f23SGarrett Wollman 		case TCPOPT_CC:
2323a0292f23SGarrett Wollman 			if (optlen != TCPOLEN_CC)
2324a0292f23SGarrett Wollman 				continue;
2325be2ac88cSJonathan Lemon 			to->to_flags |= TOF_CC;
2326a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2327a0292f23SGarrett Wollman 			    (char *)&to->to_cc, sizeof(to->to_cc));
2328fd8e4ebcSMike Barcroft 			to->to_cc = ntohl(to->to_cc);
2329a0292f23SGarrett Wollman 			break;
2330a0292f23SGarrett Wollman 		case TCPOPT_CCNEW:
2331a0292f23SGarrett Wollman 			if (optlen != TCPOLEN_CC)
2332a0292f23SGarrett Wollman 				continue;
2333be2ac88cSJonathan Lemon 			if (!is_syn)
2334a0292f23SGarrett Wollman 				continue;
2335be2ac88cSJonathan Lemon 			to->to_flags |= TOF_CCNEW;
2336a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2337a0292f23SGarrett Wollman 			    (char *)&to->to_cc, sizeof(to->to_cc));
2338fd8e4ebcSMike Barcroft 			to->to_cc = ntohl(to->to_cc);
2339a0292f23SGarrett Wollman 			break;
2340a0292f23SGarrett Wollman 		case TCPOPT_CCECHO:
2341a0292f23SGarrett Wollman 			if (optlen != TCPOLEN_CC)
2342a0292f23SGarrett Wollman 				continue;
2343be2ac88cSJonathan Lemon 			if (!is_syn)
2344a0292f23SGarrett Wollman 				continue;
2345be2ac88cSJonathan Lemon 			to->to_flags |= TOF_CCECHO;
2346a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2347a0292f23SGarrett Wollman 			    (char *)&to->to_ccecho, sizeof(to->to_ccecho));
2348fd8e4ebcSMike Barcroft 			to->to_ccecho = ntohl(to->to_ccecho);
2349a0292f23SGarrett Wollman 			break;
2350be2ac88cSJonathan Lemon 		default:
2351be2ac88cSJonathan Lemon 			continue;
2352df8bae1dSRodney W. Grimes 		}
2353df8bae1dSRodney W. Grimes 	}
2354df8bae1dSRodney W. Grimes }
2355df8bae1dSRodney W. Grimes 
2356df8bae1dSRodney W. Grimes /*
2357df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
2358df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
2359df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
2360df8bae1dSRodney W. Grimes  * sequencing purposes.
2361df8bae1dSRodney W. Grimes  */
23620312fbe9SPoul-Henning Kamp static void
2363fb59c426SYoshinobu Inoue tcp_pulloutofband(so, th, m, off)
2364df8bae1dSRodney W. Grimes 	struct socket *so;
2365fb59c426SYoshinobu Inoue 	struct tcphdr *th;
2366df8bae1dSRodney W. Grimes 	register struct mbuf *m;
2367fb59c426SYoshinobu Inoue 	int off;		/* delayed to be droped hdrlen */
2368df8bae1dSRodney W. Grimes {
2369fb59c426SYoshinobu Inoue 	int cnt = off + th->th_urp - 1;
2370df8bae1dSRodney W. Grimes 
2371df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
2372df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
2373df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
2374df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
2375df8bae1dSRodney W. Grimes 
2376df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
2377df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
2378df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
2379df8bae1dSRodney W. Grimes 			m->m_len--;
238069a34685SYoshinobu Inoue 			if (m->m_flags & M_PKTHDR)
238169a34685SYoshinobu Inoue 				m->m_pkthdr.len--;
2382df8bae1dSRodney W. Grimes 			return;
2383df8bae1dSRodney W. Grimes 		}
2384df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
2385df8bae1dSRodney W. Grimes 		m = m->m_next;
2386df8bae1dSRodney W. Grimes 		if (m == 0)
2387df8bae1dSRodney W. Grimes 			break;
2388df8bae1dSRodney W. Grimes 	}
2389df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
2390df8bae1dSRodney W. Grimes }
2391df8bae1dSRodney W. Grimes 
2392df8bae1dSRodney W. Grimes /*
2393df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
2394df8bae1dSRodney W. Grimes  * and update averages and current timeout.
2395df8bae1dSRodney W. Grimes  */
23960312fbe9SPoul-Henning Kamp static void
2397df8bae1dSRodney W. Grimes tcp_xmit_timer(tp, rtt)
2398df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
23999b8b58e0SJonathan Lemon 	int rtt;
2400df8bae1dSRodney W. Grimes {
2401233e8c18SGarrett Wollman 	register int delta;
2402233e8c18SGarrett Wollman 
2403233e8c18SGarrett Wollman 	tcpstat.tcps_rttupdated++;
2404233e8c18SGarrett Wollman 	tp->t_rttupdated++;
2405233e8c18SGarrett Wollman 	if (tp->t_srtt != 0) {
2406233e8c18SGarrett Wollman 		/*
2407233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
2408233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
2409233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
2410233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
2411233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
2412233e8c18SGarrett Wollman 		 */
2413233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
2414233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
2415233e8c18SGarrett Wollman 
2416233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
2417233e8c18SGarrett Wollman 			tp->t_srtt = 1;
2418233e8c18SGarrett Wollman 
2419233e8c18SGarrett Wollman 		/*
2420233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
2421233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
2422233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
2423233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
2424233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
2425233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
2426233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
2427233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
2428233e8c18SGarrett Wollman 		 */
2429233e8c18SGarrett Wollman 		if (delta < 0)
2430233e8c18SGarrett Wollman 			delta = -delta;
2431233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
2432233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
2433233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
2434233e8c18SGarrett Wollman 	} else {
2435233e8c18SGarrett Wollman 		/*
2436233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
2437233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
2438233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
2439233e8c18SGarrett Wollman 		 */
2440233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
2441233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
2442233e8c18SGarrett Wollman 	}
24439b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
2444df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
2445df8bae1dSRodney W. Grimes 
2446df8bae1dSRodney W. Grimes 	/*
2447df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
2448df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
2449df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
2450df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
2451df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
2452df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
2453df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
2454df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
2455df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
2456df8bae1dSRodney W. Grimes 	 */
2457233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
24589e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
2459df8bae1dSRodney W. Grimes 
2460df8bae1dSRodney W. Grimes 	/*
2461df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
2462df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
2463df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
2464df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
2465df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
2466df8bae1dSRodney W. Grimes 	 */
2467df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
2468df8bae1dSRodney W. Grimes }
2469df8bae1dSRodney W. Grimes 
2470df8bae1dSRodney W. Grimes /*
2471df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
2472df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
2473df8bae1dSRodney W. Grimes  * If none, use an mss that can be handled on the outgoing
2474df8bae1dSRodney W. Grimes  * interface without forcing IP to fragment; if bigger than
2475df8bae1dSRodney W. Grimes  * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
2476df8bae1dSRodney W. Grimes  * to utilize large mbufs.  If no route is found, route has no mtu,
2477df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
2478df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
2479df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
2480df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
2481df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
2482df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
2483df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
2484a0292f23SGarrett Wollman  *
2485a0292f23SGarrett Wollman  * Also take into account the space needed for options that we
2486a0292f23SGarrett Wollman  * send regularly.  Make maxseg shorter by that amount to assure
2487a0292f23SGarrett Wollman  * that we can send maxseg amount of data even when the options
2488a0292f23SGarrett Wollman  * are present.  Store the upper limit of the length of options plus
2489a0292f23SGarrett Wollman  * data in maxopd.
2490a0292f23SGarrett Wollman  *
2491a0292f23SGarrett Wollman  * NOTE that this routine is only called when we process an incoming
2492a0292f23SGarrett Wollman  * segment, for outgoing segments only tcp_mssopt is called.
2493a0292f23SGarrett Wollman  *
2494a0292f23SGarrett Wollman  * In case of T/TCP, we call this routine during implicit connection
2495a0292f23SGarrett Wollman  * setup as well (offer = -1), to initialize maxseg from the cached
2496a0292f23SGarrett Wollman  * MSS of our peer.
2497df8bae1dSRodney W. Grimes  */
2498a0292f23SGarrett Wollman void
2499df8bae1dSRodney W. Grimes tcp_mss(tp, offer)
2500a0292f23SGarrett Wollman 	struct tcpcb *tp;
2501a0292f23SGarrett Wollman 	int offer;
2502df8bae1dSRodney W. Grimes {
2503df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
2504df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
2505df8bae1dSRodney W. Grimes 	register int rtt, mss;
2506df8bae1dSRodney W. Grimes 	u_long bufsize;
2507df8bae1dSRodney W. Grimes 	struct inpcb *inp;
2508df8bae1dSRodney W. Grimes 	struct socket *so;
2509a0292f23SGarrett Wollman 	struct rmxp_tao *taop;
2510a0292f23SGarrett Wollman 	int origoffer = offer;
2511fb59c426SYoshinobu Inoue #ifdef INET6
2512fb59c426SYoshinobu Inoue 	int isipv6;
2513fb59c426SYoshinobu Inoue 	int min_protoh;
2514fb59c426SYoshinobu Inoue #endif
2515df8bae1dSRodney W. Grimes 
2516df8bae1dSRodney W. Grimes 	inp = tp->t_inpcb;
2517fb59c426SYoshinobu Inoue #ifdef INET6
2518fb59c426SYoshinobu Inoue 	isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
2519fb59c426SYoshinobu Inoue 	min_protoh = isipv6 ? sizeof (struct ip6_hdr) + sizeof (struct tcphdr)
2520fb59c426SYoshinobu Inoue 			    : sizeof (struct tcpiphdr);
2521fb59c426SYoshinobu Inoue #else
2522fb59c426SYoshinobu Inoue #define min_protoh  (sizeof (struct tcpiphdr))
2523fb59c426SYoshinobu Inoue #endif
2524fb59c426SYoshinobu Inoue #ifdef INET6
2525fb59c426SYoshinobu Inoue 	if (isipv6)
2526be2ac88cSJonathan Lemon 		rt = tcp_rtlookup6(&inp->inp_inc);
2527fb59c426SYoshinobu Inoue 	else
2528fb59c426SYoshinobu Inoue #endif
2529be2ac88cSJonathan Lemon 	rt = tcp_rtlookup(&inp->inp_inc);
2530fb59c426SYoshinobu Inoue 	if (rt == NULL) {
2531fb59c426SYoshinobu Inoue 		tp->t_maxopd = tp->t_maxseg =
2532fb59c426SYoshinobu Inoue #ifdef INET6
2533fb59c426SYoshinobu Inoue 		isipv6 ? tcp_v6mssdflt :
2534fb59c426SYoshinobu Inoue #endif /* INET6 */
2535fb59c426SYoshinobu Inoue 		tcp_mssdflt;
2536a0292f23SGarrett Wollman 		return;
2537df8bae1dSRodney W. Grimes 	}
2538df8bae1dSRodney W. Grimes 	ifp = rt->rt_ifp;
2539df8bae1dSRodney W. Grimes 	so = inp->inp_socket;
2540df8bae1dSRodney W. Grimes 
2541a0292f23SGarrett Wollman 	taop = rmx_taop(rt->rt_rmx);
2542a0292f23SGarrett Wollman 	/*
2543a0292f23SGarrett Wollman 	 * Offer == -1 means that we didn't receive SYN yet,
2544a0292f23SGarrett Wollman 	 * use cached value in that case;
2545a0292f23SGarrett Wollman 	 */
2546a0292f23SGarrett Wollman 	if (offer == -1)
2547a0292f23SGarrett Wollman 		offer = taop->tao_mssopt;
2548a0292f23SGarrett Wollman 	/*
2549a0292f23SGarrett Wollman 	 * Offer == 0 means that there was no MSS on the SYN segment,
2550a0292f23SGarrett Wollman 	 * in this case we use tcp_mssdflt.
2551a0292f23SGarrett Wollman 	 */
2552a0292f23SGarrett Wollman 	if (offer == 0)
2553fb59c426SYoshinobu Inoue 		offer =
2554fb59c426SYoshinobu Inoue #ifdef INET6
2555fb59c426SYoshinobu Inoue 			isipv6 ? tcp_v6mssdflt :
2556fb59c426SYoshinobu Inoue #endif /* INET6 */
2557fb59c426SYoshinobu Inoue 			tcp_mssdflt;
2558a0292f23SGarrett Wollman 	else
2559a0292f23SGarrett Wollman 		/*
2560a0292f23SGarrett Wollman 		 * Sanity check: make sure that maxopd will be large
2561a0292f23SGarrett Wollman 		 * enough to allow some data on segments even is the
2562a0292f23SGarrett Wollman 		 * all the option space is used (40bytes).  Otherwise
2563a0292f23SGarrett Wollman 		 * funny things may happen in tcp_output.
2564a0292f23SGarrett Wollman 		 */
2565a0292f23SGarrett Wollman 		offer = max(offer, 64);
2566a0292f23SGarrett Wollman 	taop->tao_mssopt = offer;
2567a0292f23SGarrett Wollman 
2568df8bae1dSRodney W. Grimes 	/*
2569df8bae1dSRodney W. Grimes 	 * While we're here, check if there's an initial rtt
2570df8bae1dSRodney W. Grimes 	 * or rttvar.  Convert from the route-table units
2571df8bae1dSRodney W. Grimes 	 * to scaled multiples of the slow timeout timer.
2572df8bae1dSRodney W. Grimes 	 */
2573df8bae1dSRodney W. Grimes 	if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
2574df8bae1dSRodney W. Grimes 		/*
2575a0292f23SGarrett Wollman 		 * XXX the lock bit for RTT indicates that the value
2576df8bae1dSRodney W. Grimes 		 * is also a minimum value; this is subject to time.
2577df8bae1dSRodney W. Grimes 		 */
2578df8bae1dSRodney W. Grimes 		if (rt->rt_rmx.rmx_locks & RTV_RTT)
25799b8b58e0SJonathan Lemon 			tp->t_rttmin = rtt / (RTM_RTTUNIT / hz);
25809b8b58e0SJonathan Lemon 		tp->t_srtt = rtt / (RTM_RTTUNIT / (hz * TCP_RTT_SCALE));
2581dd224982SGarrett Wollman 		tcpstat.tcps_usedrtt++;
2582dd224982SGarrett Wollman 		if (rt->rt_rmx.rmx_rttvar) {
2583df8bae1dSRodney W. Grimes 			tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
25849b8b58e0SJonathan Lemon 			    (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE));
2585dd224982SGarrett Wollman 			tcpstat.tcps_usedrttvar++;
2586dd224982SGarrett Wollman 		} else {
2587df8bae1dSRodney W. Grimes 			/* default variation is +- 1 rtt */
2588df8bae1dSRodney W. Grimes 			tp->t_rttvar =
2589df8bae1dSRodney W. Grimes 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
2590dd224982SGarrett Wollman 		}
2591df8bae1dSRodney W. Grimes 		TCPT_RANGESET(tp->t_rxtcur,
2592df8bae1dSRodney W. Grimes 			      ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
2593df8bae1dSRodney W. Grimes 			      tp->t_rttmin, TCPTV_REXMTMAX);
2594df8bae1dSRodney W. Grimes 	}
2595df8bae1dSRodney W. Grimes 	/*
2596df8bae1dSRodney W. Grimes 	 * if there's an mtu associated with the route, use it
2597fb59c426SYoshinobu Inoue 	 * else, use the link mtu.
2598df8bae1dSRodney W. Grimes 	 */
2599df8bae1dSRodney W. Grimes 	if (rt->rt_rmx.rmx_mtu)
2600fb59c426SYoshinobu Inoue 		mss = rt->rt_rmx.rmx_mtu - min_protoh;
2601df8bae1dSRodney W. Grimes 	else
2602df8bae1dSRodney W. Grimes 	{
2603fb59c426SYoshinobu Inoue 		mss =
2604fb59c426SYoshinobu Inoue #ifdef INET6
2605fb59c426SYoshinobu Inoue 			(isipv6 ? nd_ifinfo[rt->rt_ifp->if_index].linkmtu :
2606fb59c426SYoshinobu Inoue #endif
2607fb59c426SYoshinobu Inoue 			 ifp->if_mtu
2608fb59c426SYoshinobu Inoue #ifdef INET6
2609fb59c426SYoshinobu Inoue 			 )
2610fb59c426SYoshinobu Inoue #endif
2611fb59c426SYoshinobu Inoue 			- min_protoh;
2612fb59c426SYoshinobu Inoue #ifdef INET6
2613fb59c426SYoshinobu Inoue 		if (isipv6) {
2614fb59c426SYoshinobu Inoue 			if (!in6_localaddr(&inp->in6p_faddr))
2615fb59c426SYoshinobu Inoue 				mss = min(mss, tcp_v6mssdflt);
2616fb59c426SYoshinobu Inoue 		} else
2617fb59c426SYoshinobu Inoue #endif
2618a0292f23SGarrett Wollman 		if (!in_localaddr(inp->inp_faddr))
2619a0292f23SGarrett Wollman 			mss = min(mss, tcp_mssdflt);
2620a0292f23SGarrett Wollman 	}
2621a0292f23SGarrett Wollman 	mss = min(mss, offer);
2622a0292f23SGarrett Wollman 	/*
2623a0292f23SGarrett Wollman 	 * maxopd stores the maximum length of data AND options
2624a0292f23SGarrett Wollman 	 * in a segment; maxseg is the amount of data in a normal
2625a0292f23SGarrett Wollman 	 * segment.  We need to store this value (maxopd) apart
2626a0292f23SGarrett Wollman 	 * from maxseg, because now every segment carries options
2627a0292f23SGarrett Wollman 	 * and thus we normally have somewhat less data in segments.
2628a0292f23SGarrett Wollman 	 */
2629a0292f23SGarrett Wollman 	tp->t_maxopd = mss;
2630a0292f23SGarrett Wollman 
2631a0292f23SGarrett Wollman 	/*
2632a0292f23SGarrett Wollman 	 * In case of T/TCP, origoffer==-1 indicates, that no segments
2633a0292f23SGarrett Wollman 	 * were received yet.  In this case we just guess, otherwise
2634a0292f23SGarrett Wollman 	 * we do the same as before T/TCP.
2635a0292f23SGarrett Wollman 	 */
2636a0292f23SGarrett Wollman  	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
2637a0292f23SGarrett Wollman 	    (origoffer == -1 ||
2638a0292f23SGarrett Wollman 	     (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
2639a0292f23SGarrett Wollman 		mss -= TCPOLEN_TSTAMP_APPA;
2640a0292f23SGarrett Wollman  	if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
2641a0292f23SGarrett Wollman 	    (origoffer == -1 ||
2642a0292f23SGarrett Wollman 	     (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC))
2643a0292f23SGarrett Wollman 		mss -= TCPOLEN_CC_APPA;
2644a0292f23SGarrett Wollman 
2645df8bae1dSRodney W. Grimes #if	(MCLBYTES & (MCLBYTES - 1)) == 0
2646df8bae1dSRodney W. Grimes 		if (mss > MCLBYTES)
2647df8bae1dSRodney W. Grimes 			mss &= ~(MCLBYTES-1);
2648df8bae1dSRodney W. Grimes #else
2649df8bae1dSRodney W. Grimes 		if (mss > MCLBYTES)
2650df8bae1dSRodney W. Grimes 			mss = mss / MCLBYTES * MCLBYTES;
2651df8bae1dSRodney W. Grimes #endif
2652df8bae1dSRodney W. Grimes 	/*
2653df8bae1dSRodney W. Grimes 	 * If there's a pipesize, change the socket buffer
2654df8bae1dSRodney W. Grimes 	 * to that size.  Make the socket buffers an integral
2655df8bae1dSRodney W. Grimes 	 * number of mss units; if the mss is larger than
2656df8bae1dSRodney W. Grimes 	 * the socket buffer, decrease the mss.
2657df8bae1dSRodney W. Grimes 	 */
2658df8bae1dSRodney W. Grimes #ifdef RTV_SPIPE
2659df8bae1dSRodney W. Grimes 	if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0)
2660df8bae1dSRodney W. Grimes #endif
2661df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
2662df8bae1dSRodney W. Grimes 	if (bufsize < mss)
2663df8bae1dSRodney W. Grimes 		mss = bufsize;
2664df8bae1dSRodney W. Grimes 	else {
2665df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
2666df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
2667df8bae1dSRodney W. Grimes 			bufsize = sb_max;
266888c39af3SRuslan Ermilov 		if (bufsize > so->so_snd.sb_hiwat)
2669ecf72308SBrian Feldman 			(void)sbreserve(&so->so_snd, bufsize, so, NULL);
2670df8bae1dSRodney W. Grimes 	}
2671df8bae1dSRodney W. Grimes 	tp->t_maxseg = mss;
2672df8bae1dSRodney W. Grimes 
2673df8bae1dSRodney W. Grimes #ifdef RTV_RPIPE
2674df8bae1dSRodney W. Grimes 	if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0)
2675df8bae1dSRodney W. Grimes #endif
2676df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
2677df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
2678df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
2679df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
2680df8bae1dSRodney W. Grimes 			bufsize = sb_max;
268188c39af3SRuslan Ermilov 		if (bufsize > so->so_rcv.sb_hiwat)
2682ecf72308SBrian Feldman 			(void)sbreserve(&so->so_rcv, bufsize, so, NULL);
2683df8bae1dSRodney W. Grimes 	}
26849b8b58e0SJonathan Lemon 
2685a0292f23SGarrett Wollman 	/*
26869b8b58e0SJonathan Lemon 	 * Set the slow-start flight size depending on whether this
26879b8b58e0SJonathan Lemon 	 * is a local network or not.
2688a0292f23SGarrett Wollman 	 */
2689fb59c426SYoshinobu Inoue 	if (
2690fb59c426SYoshinobu Inoue #ifdef INET6
2691fb59c426SYoshinobu Inoue 	    (isipv6 && in6_localaddr(&inp->in6p_faddr)) ||
2692fb59c426SYoshinobu Inoue 	    (!isipv6 &&
2693fb59c426SYoshinobu Inoue #endif
2694fb59c426SYoshinobu Inoue 	     in_localaddr(inp->inp_faddr)
2695fb59c426SYoshinobu Inoue #ifdef INET6
2696fb59c426SYoshinobu Inoue 	     )
2697fb59c426SYoshinobu Inoue #endif
2698fb59c426SYoshinobu Inoue 	    )
26999b8b58e0SJonathan Lemon 		tp->snd_cwnd = mss * ss_fltsz_local;
27009b8b58e0SJonathan Lemon 	else
27019b8b58e0SJonathan Lemon 		tp->snd_cwnd = mss * ss_fltsz;
2702df8bae1dSRodney W. Grimes 
2703df8bae1dSRodney W. Grimes 	if (rt->rt_rmx.rmx_ssthresh) {
2704df8bae1dSRodney W. Grimes 		/*
2705df8bae1dSRodney W. Grimes 		 * There's some sort of gateway or interface
2706df8bae1dSRodney W. Grimes 		 * buffer limit on the path.  Use this to set
2707df8bae1dSRodney W. Grimes 		 * the slow start threshhold, but set the
2708df8bae1dSRodney W. Grimes 		 * threshold to no less than 2*mss.
2709df8bae1dSRodney W. Grimes 		 */
2710df8bae1dSRodney W. Grimes 		tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
2711dd224982SGarrett Wollman 		tcpstat.tcps_usedssthresh++;
2712df8bae1dSRodney W. Grimes 	}
2713a0292f23SGarrett Wollman }
2714a0292f23SGarrett Wollman 
2715a0292f23SGarrett Wollman /*
2716a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
2717a0292f23SGarrett Wollman  */
2718a0292f23SGarrett Wollman int
2719a0292f23SGarrett Wollman tcp_mssopt(tp)
2720a0292f23SGarrett Wollman 	struct tcpcb *tp;
2721a0292f23SGarrett Wollman {
2722a0292f23SGarrett Wollman 	struct rtentry *rt;
2723fb59c426SYoshinobu Inoue #ifdef INET6
2724fb59c426SYoshinobu Inoue 	int isipv6;
2725fb59c426SYoshinobu Inoue 	int min_protoh;
2726fb59c426SYoshinobu Inoue #endif
2727a0292f23SGarrett Wollman 
2728fb59c426SYoshinobu Inoue #ifdef INET6
2729fb59c426SYoshinobu Inoue 	isipv6 = ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
2730fb59c426SYoshinobu Inoue 	min_protoh = isipv6 ? sizeof (struct ip6_hdr) + sizeof (struct tcphdr)
2731fb59c426SYoshinobu Inoue 			    : sizeof (struct tcpiphdr);
2732fb59c426SYoshinobu Inoue #else
2733fb59c426SYoshinobu Inoue #define min_protoh  (sizeof (struct tcpiphdr))
2734fb59c426SYoshinobu Inoue #endif
2735fb59c426SYoshinobu Inoue #ifdef INET6
2736fb59c426SYoshinobu Inoue 	if (isipv6)
2737be2ac88cSJonathan Lemon 		rt = tcp_rtlookup6(&tp->t_inpcb->inp_inc);
2738fb59c426SYoshinobu Inoue 	else
2739fb59c426SYoshinobu Inoue #endif /* INET6 */
2740be2ac88cSJonathan Lemon 	rt = tcp_rtlookup(&tp->t_inpcb->inp_inc);
2741a0292f23SGarrett Wollman 	if (rt == NULL)
2742fb59c426SYoshinobu Inoue 		return
2743fb59c426SYoshinobu Inoue #ifdef INET6
2744fb59c426SYoshinobu Inoue 			isipv6 ? tcp_v6mssdflt :
2745fb59c426SYoshinobu Inoue #endif /* INET6 */
2746fb59c426SYoshinobu Inoue 			tcp_mssdflt;
2747a0292f23SGarrett Wollman 
2748fb59c426SYoshinobu Inoue 	return rt->rt_ifp->if_mtu - min_protoh;
2749df8bae1dSRodney W. Grimes }
275046f58482SJonathan Lemon 
275146f58482SJonathan Lemon 
275246f58482SJonathan Lemon /*
275346f58482SJonathan Lemon  * Checks for partial ack.  If partial ack arrives, force the retransmission
275446f58482SJonathan Lemon  * of the next unacknowledged segment, do not clear tp->t_dupacks, and return
275546f58482SJonathan Lemon  * 1.  By setting snd_nxt to ti_ack, this forces retransmission timer to
275646f58482SJonathan Lemon  * be started again.  If the ack advances at least to tp->snd_recover, return 0.
275746f58482SJonathan Lemon  */
275846f58482SJonathan Lemon static int
275946f58482SJonathan Lemon tcp_newreno(tp, th)
276046f58482SJonathan Lemon 	struct tcpcb *tp;
276146f58482SJonathan Lemon 	struct tcphdr *th;
276246f58482SJonathan Lemon {
276346f58482SJonathan Lemon 	if (SEQ_LT(th->th_ack, tp->snd_recover)) {
276446f58482SJonathan Lemon 		tcp_seq onxt = tp->snd_nxt;
276546f58482SJonathan Lemon 		u_long  ocwnd = tp->snd_cwnd;
276646f58482SJonathan Lemon 
276746f58482SJonathan Lemon 		callout_stop(tp->tt_rexmt);
276846f58482SJonathan Lemon 		tp->t_rtttime = 0;
276946f58482SJonathan Lemon 		tp->snd_nxt = th->th_ack;
27706b2a5f92SJayanth Vijayaraghavan 		/*
27716b2a5f92SJayanth Vijayaraghavan 		 * Set snd_cwnd to one segment beyond acknowledged offset
27726b2a5f92SJayanth Vijayaraghavan 		 * (tp->snd_una has not yet been updated when this function
27736b2a5f92SJayanth Vijayaraghavan 		 *  is called)
27746b2a5f92SJayanth Vijayaraghavan 		 */
27756b2a5f92SJayanth Vijayaraghavan 		tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una);
27766b2a5f92SJayanth Vijayaraghavan 		(void) tcp_output(tp);
277746f58482SJonathan Lemon 		tp->snd_cwnd = ocwnd;
277846f58482SJonathan Lemon 		if (SEQ_GT(onxt, tp->snd_nxt))
277946f58482SJonathan Lemon 			tp->snd_nxt = onxt;
278046f58482SJonathan Lemon 		/*
278146f58482SJonathan Lemon 		 * Partial window deflation.  Relies on fact that tp->snd_una
278246f58482SJonathan Lemon 		 * not updated yet.
278346f58482SJonathan Lemon 		 */
278446f58482SJonathan Lemon 		tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_maxseg);
278546f58482SJonathan Lemon 		return (1);
278646f58482SJonathan Lemon 	}
278746f58482SJonathan Lemon 	return (0);
278846f58482SJonathan Lemon }
2789