xref: /freebsd/sys/netinet/tcp_input.c (revision 9b932e9e048ade36fd50bad5eb8b9475b0a6082d)
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  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29e79adb8eSGarrett Wollman  *	@(#)tcp_input.c	8.12 (Berkeley) 5/24/95
30c3aac50fSPeter Wemm  * $FreeBSD$
31df8bae1dSRodney W. Grimes  */
32df8bae1dSRodney W. Grimes 
33f9e354dfSJulian Elischer #include "opt_ipfw.h"		/* for ipfw_fwd		*/
341cfd4b53SBruce M Simpson #include "opt_inet.h"
35fb59c426SYoshinobu Inoue #include "opt_inet6.h"
363a2a9f79SYoshinobu Inoue #include "opt_ipsec.h"
37c488362eSRobert Watson #include "opt_mac.h"
380cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
39e46cd3d4SDag-Erling Smørgrav #include "opt_tcp_input.h"
406d90faf3SPaul Saab #include "opt_tcp_sack.h"
410cc12cc5SJoerg Wunsch 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
4398163b98SPoul-Henning Kamp #include <sys/kernel.h>
44c488362eSRobert Watson #include <sys/mac.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 
5812e2e970SAndre Oppermann #include <vm/uma.h>
5912e2e970SAndre Oppermann 
60df8bae1dSRodney W. Grimes #include <net/if.h>
61df8bae1dSRodney W. Grimes #include <net/route.h>
62df8bae1dSRodney W. Grimes 
63df8bae1dSRodney W. Grimes #include <netinet/in.h>
64960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
65df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
66960ed29cSSeigo Tanimura #include <netinet/in_var.h>
67df8bae1dSRodney W. Grimes #include <netinet/ip.h>
68686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_icmp.h>	/* for ICMP_BANDLIM		*/
69686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM		*/
70df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
71686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
72686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
73686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
74960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h>
75960ed29cSSeigo Tanimura #include <netinet6/nd6.h>
76df8bae1dSRodney W. Grimes #include <netinet/tcp.h>
77df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
78df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
79df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
80df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
81fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h>
82df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
83610ee2f9SDavid Greenman #ifdef TCPDEBUG
84df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
85fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */
86fb59c426SYoshinobu Inoue 
87b9234fafSSam Leffler #ifdef FAST_IPSEC
88b9234fafSSam Leffler #include <netipsec/ipsec.h>
89b9234fafSSam Leffler #include <netipsec/ipsec6.h>
90b9234fafSSam Leffler #endif /*FAST_IPSEC*/
91b9234fafSSam Leffler 
92fb59c426SYoshinobu Inoue #ifdef IPSEC
93fb59c426SYoshinobu Inoue #include <netinet6/ipsec.h>
943a2a9f79SYoshinobu Inoue #include <netinet6/ipsec6.h>
95fb59c426SYoshinobu Inoue #include <netkey/key.h>
96fb59c426SYoshinobu Inoue #endif /*IPSEC*/
97fb59c426SYoshinobu Inoue 
98db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
99db4f9cc7SJonathan Lemon 
100c068736aSJeffrey Hsu static const int tcprexmtthresh = 3;
1012f96f1f4SGarrett Wollman tcp_cc	tcp_ccgen;
1020312fbe9SPoul-Henning Kamp 
1032f96f1f4SGarrett Wollman struct	tcpstat tcpstat;
104c73d99b5SRuslan Ermilov SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RW,
1053d177f46SBill Fumerola     &tcpstat , tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
1060312fbe9SPoul-Henning Kamp 
107d78a37adSPaul Traina static int log_in_vain = 0;
108816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW,
1093d177f46SBill Fumerola     &log_in_vain, 0, "Log all incoming TCP connections");
110816a3d83SPoul-Henning Kamp 
11116f7f31fSGeoff Rehmet static int blackhole = 0;
11216f7f31fSGeoff Rehmet SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW,
11316f7f31fSGeoff Rehmet 	&blackhole, 0, "Do not send RST when dropping refused connections");
11416f7f31fSGeoff Rehmet 
115f498eeeeSDavid Greenman int tcp_delack_enabled = 1;
11684e33c9eSDavid Greenman SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW,
1173d177f46SBill Fumerola     &tcp_delack_enabled, 0,
1183d177f46SBill Fumerola     "Delay ACK to try and piggyback it onto a data packet");
119f498eeeeSDavid Greenman 
120f8613305SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN
121f8613305SDag-Erling Smørgrav static int drop_synfin = 0;
122f8613305SDag-Erling Smørgrav SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW,
123f8613305SDag-Erling Smørgrav     &drop_synfin, 0, "Drop TCP packets with SYN+FIN set");
124f8613305SDag-Erling Smørgrav #endif
125f8613305SDag-Erling Smørgrav 
126dba7bc6aSAndre Oppermann static int tcp_do_rfc3042 = 1;
127582a954bSJeffrey Hsu SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW,
128582a954bSJeffrey Hsu     &tcp_do_rfc3042, 0, "Enable RFC 3042 (Limited Transmit)");
129582a954bSJeffrey Hsu 
130dba7bc6aSAndre Oppermann static int tcp_do_rfc3390 = 1;
131da3a8a1aSJeffrey Hsu SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW,
132da3a8a1aSJeffrey Hsu     &tcp_do_rfc3390, 0,
133da3a8a1aSJeffrey Hsu     "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)");
134da3a8a1aSJeffrey Hsu 
13512e2e970SAndre Oppermann SYSCTL_NODE(_net_inet_tcp, OID_AUTO, reass, CTLFLAG_RW, 0,
13612e2e970SAndre Oppermann 	    "TCP Segment Reassembly Queue");
13712e2e970SAndre Oppermann 
13812e2e970SAndre Oppermann static int tcp_reass_maxseg = 0;
13912e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxsegments, CTLFLAG_RDTUN,
14012e2e970SAndre Oppermann 	   &tcp_reass_maxseg, 0,
14112e2e970SAndre Oppermann 	   "Global maximum number of TCP Segments in Reassembly Queue");
14212e2e970SAndre Oppermann 
14312e2e970SAndre Oppermann int tcp_reass_qsize = 0;
14412e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, cursegments, CTLFLAG_RD,
14512e2e970SAndre Oppermann 	   &tcp_reass_qsize, 0,
14612e2e970SAndre Oppermann 	   "Global number of TCP Segments currently in Reassembly Queue");
14712e2e970SAndre Oppermann 
14812e2e970SAndre Oppermann static int tcp_reass_maxqlen = 48;
14912e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxqlen, CTLFLAG_RW,
15012e2e970SAndre Oppermann 	   &tcp_reass_maxqlen, 0,
15112e2e970SAndre Oppermann 	   "Maximum number of TCP Segments per individual Reassembly Queue");
15212e2e970SAndre Oppermann 
15312e2e970SAndre Oppermann static int tcp_reass_overflows = 0;
15412e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, overflows, CTLFLAG_RD,
15512e2e970SAndre Oppermann 	   &tcp_reass_overflows, 0,
15612e2e970SAndre Oppermann 	   "Global number of TCP Segment Reassembly Queue Overflows");
15712e2e970SAndre Oppermann 
15815bd2b43SDavid Greenman struct inpcbhead tcb;
159fb59c426SYoshinobu Inoue #define	tcb6	tcb  /* for KAME src sync over BSD*'s */
16015bd2b43SDavid Greenman struct inpcbinfo tcbinfo;
161f76fcf6dSJeffrey Hsu struct mtx	*tcbinfo_mtx;
162df8bae1dSRodney W. Grimes 
1636d90faf3SPaul Saab static void	 tcp_dooptions(struct tcpcb *, struct tcpopt *, u_char *,
1646d90faf3SPaul Saab 		     int, int, struct tcphdr *);
1656d90faf3SPaul Saab 
1664d77a549SAlfred Perlstein static void	 tcp_pulloutofband(struct socket *,
1674d77a549SAlfred Perlstein 		     struct tcphdr *, struct mbuf *, int);
1684d77a549SAlfred Perlstein static int	 tcp_reass(struct tcpcb *, struct tcphdr *, int *,
1694d77a549SAlfred Perlstein 		     struct mbuf *);
1704d77a549SAlfred Perlstein static void	 tcp_xmit_timer(struct tcpcb *, int);
171c068736aSJeffrey Hsu static void	 tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *);
172340c35deSJonathan Lemon static int	 tcp_timewait(struct tcptw *, struct tcpopt *,
173340c35deSJonathan Lemon 		     struct tcphdr *, struct mbuf *, int);
1740312fbe9SPoul-Henning Kamp 
175fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */
176fb59c426SYoshinobu Inoue #ifdef INET6
177fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \
178fb59c426SYoshinobu Inoue do { \
179fb59c426SYoshinobu Inoue 	if ((tp) && (tp)->t_inpcb && \
18097d8d152SAndre Oppermann 	    ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \
18197d8d152SAndre Oppermann 		nd6_nud_hint(NULL, NULL, 0); \
182fb59c426SYoshinobu Inoue } while (0)
183fb59c426SYoshinobu Inoue #else
184fb59c426SYoshinobu Inoue #define ND6_HINT(tp)
185fb59c426SYoshinobu Inoue #endif
186df8bae1dSRodney W. Grimes 
187df8bae1dSRodney W. Grimes /*
188262c1c1aSMatthew Dillon  * Indicate whether this ack should be delayed.  We can delay the ack if
189262c1c1aSMatthew Dillon  *	- there is no delayed ack timer in progress and
190262c1c1aSMatthew Dillon  *	- our last ack wasn't a 0-sized window.  We never want to delay
1913bfd6421SJonathan Lemon  *	  the ack that opens up a 0-sized window and
1923bfd6421SJonathan Lemon  *		- delayed acks are enabled or
1933bfd6421SJonathan Lemon  *		- this is a half-synchronized T/TCP connection.
194d8c85a26SJonathan Lemon  */
195d8c85a26SJonathan Lemon #define DELAY_ACK(tp)							\
196a14c749fSJonathan Lemon 	((!callout_active(tp->tt_delack) &&				\
197a14c749fSJonathan Lemon 	    (tp->t_flags & TF_RXWIN0SENT) == 0) &&			\
1983bfd6421SJonathan Lemon 	    (tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN)))
199d8c85a26SJonathan Lemon 
20012e2e970SAndre Oppermann /* Initialize TCP reassembly queue */
20112e2e970SAndre Oppermann uma_zone_t	tcp_reass_zone;
20212e2e970SAndre Oppermann void
20312e2e970SAndre Oppermann tcp_reass_init()
20412e2e970SAndre Oppermann {
20512e2e970SAndre Oppermann 	tcp_reass_maxseg = nmbclusters / 16;
20612e2e970SAndre Oppermann 	TUNABLE_INT_FETCH("net.inet.tcp.reass.maxsegments",
20712e2e970SAndre Oppermann 	    &tcp_reass_maxseg);
20812e2e970SAndre Oppermann 	tcp_reass_zone = uma_zcreate("tcpreass", sizeof (struct tseg_qent),
20912e2e970SAndre Oppermann 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
21012e2e970SAndre Oppermann 	uma_zone_set_max(tcp_reass_zone, tcp_reass_maxseg);
21112e2e970SAndre Oppermann }
21212e2e970SAndre Oppermann 
2130312fbe9SPoul-Henning Kamp static int
214fb59c426SYoshinobu Inoue tcp_reass(tp, th, tlenp, m)
215df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
216fb59c426SYoshinobu Inoue 	register struct tcphdr *th;
217fb59c426SYoshinobu Inoue 	int *tlenp;
218df8bae1dSRodney W. Grimes 	struct mbuf *m;
219df8bae1dSRodney W. Grimes {
220fb59c426SYoshinobu Inoue 	struct tseg_qent *q;
221fb59c426SYoshinobu Inoue 	struct tseg_qent *p = NULL;
222fb59c426SYoshinobu Inoue 	struct tseg_qent *nq;
22312e2e970SAndre Oppermann 	struct tseg_qent *te = NULL;
224df8bae1dSRodney W. Grimes 	struct socket *so = tp->t_inpcb->inp_socket;
225df8bae1dSRodney W. Grimes 	int flags;
226df8bae1dSRodney W. Grimes 
227df8bae1dSRodney W. Grimes 	/*
22812e2e970SAndre Oppermann 	 * XXX: tcp_reass() is rather inefficient with its data structures
22912e2e970SAndre Oppermann 	 * and should be rewritten (see NetBSD for optimizations).  While
23012e2e970SAndre Oppermann 	 * doing that it should move to its own file tcp_reass.c.
23112e2e970SAndre Oppermann 	 */
23212e2e970SAndre Oppermann 
23312e2e970SAndre Oppermann 	/*
234fb59c426SYoshinobu Inoue 	 * Call with th==0 after become established to
235df8bae1dSRodney W. Grimes 	 * force pre-ESTABLISHED data up to user socket.
236df8bae1dSRodney W. Grimes 	 */
237fb59c426SYoshinobu Inoue 	if (th == 0)
238df8bae1dSRodney W. Grimes 		goto present;
239df8bae1dSRodney W. Grimes 
24012e2e970SAndre Oppermann 	/*
24112e2e970SAndre Oppermann 	 * Limit the number of segments in the reassembly queue to prevent
24212e2e970SAndre Oppermann 	 * holding on to too many segments (and thus running out of mbufs).
24312e2e970SAndre Oppermann 	 * Make sure to let the missing segment through which caused this
24412e2e970SAndre Oppermann 	 * queue.  Always keep one global queue entry spare to be able to
24512e2e970SAndre Oppermann 	 * process the missing segment.
24612e2e970SAndre Oppermann 	 */
24712e2e970SAndre Oppermann 	if (th->th_seq != tp->rcv_nxt &&
24812e2e970SAndre Oppermann 	    (tcp_reass_qsize + 1 >= tcp_reass_maxseg ||
24912e2e970SAndre Oppermann 	     tp->t_segqlen >= tcp_reass_maxqlen)) {
25012e2e970SAndre Oppermann 		tcp_reass_overflows++;
25112e2e970SAndre Oppermann 		tcpstat.tcps_rcvmemdrop++;
25212e2e970SAndre Oppermann 		m_freem(m);
25312e2e970SAndre Oppermann 		return (0);
25412e2e970SAndre Oppermann 	}
25512e2e970SAndre Oppermann 
25612e2e970SAndre Oppermann 	/*
25712e2e970SAndre Oppermann 	 * Allocate a new queue entry. If we can't, or hit the zone limit
25812e2e970SAndre Oppermann 	 * just drop the pkt.
25912e2e970SAndre Oppermann 	 */
26012e2e970SAndre Oppermann 	te = uma_zalloc(tcp_reass_zone, M_NOWAIT);
261fb59c426SYoshinobu Inoue 	if (te == NULL) {
262fb59c426SYoshinobu Inoue 		tcpstat.tcps_rcvmemdrop++;
263fb59c426SYoshinobu Inoue 		m_freem(m);
264fb59c426SYoshinobu Inoue 		return (0);
265fb59c426SYoshinobu Inoue 	}
26612e2e970SAndre Oppermann 	tp->t_segqlen++;
26712e2e970SAndre Oppermann 	tcp_reass_qsize++;
2686effc713SDoug Rabson 
269df8bae1dSRodney W. Grimes 	/*
270df8bae1dSRodney W. Grimes 	 * Find a segment which begins after this one does.
271df8bae1dSRodney W. Grimes 	 */
272fb59c426SYoshinobu Inoue 	LIST_FOREACH(q, &tp->t_segq, tqe_q) {
273fb59c426SYoshinobu Inoue 		if (SEQ_GT(q->tqe_th->th_seq, th->th_seq))
274df8bae1dSRodney W. Grimes 			break;
275fb59c426SYoshinobu Inoue 		p = q;
276fb59c426SYoshinobu Inoue 	}
277df8bae1dSRodney W. Grimes 
278df8bae1dSRodney W. Grimes 	/*
279df8bae1dSRodney W. Grimes 	 * If there is a preceding segment, it may provide some of
280df8bae1dSRodney W. Grimes 	 * our data already.  If so, drop the data from the incoming
281df8bae1dSRodney W. Grimes 	 * segment.  If it provides all of our data, drop us.
282df8bae1dSRodney W. Grimes 	 */
2836effc713SDoug Rabson 	if (p != NULL) {
284df8bae1dSRodney W. Grimes 		register int i;
285df8bae1dSRodney W. Grimes 		/* conversion to int (in i) handles seq wraparound */
286fb59c426SYoshinobu Inoue 		i = p->tqe_th->th_seq + p->tqe_len - th->th_seq;
287df8bae1dSRodney W. Grimes 		if (i > 0) {
288fb59c426SYoshinobu Inoue 			if (i >= *tlenp) {
289df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvduppack++;
290fb59c426SYoshinobu Inoue 				tcpstat.tcps_rcvdupbyte += *tlenp;
291df8bae1dSRodney W. Grimes 				m_freem(m);
29212e2e970SAndre Oppermann 				uma_zfree(tcp_reass_zone, te);
29312e2e970SAndre Oppermann 				tp->t_segqlen--;
29412e2e970SAndre Oppermann 				tcp_reass_qsize--;
295a0292f23SGarrett Wollman 				/*
296a0292f23SGarrett Wollman 				 * Try to present any queued data
297a0292f23SGarrett Wollman 				 * at the left window edge to the user.
298a0292f23SGarrett Wollman 				 * This is needed after the 3-WHS
299a0292f23SGarrett Wollman 				 * completes.
300a0292f23SGarrett Wollman 				 */
301a0292f23SGarrett Wollman 				goto present;	/* ??? */
302df8bae1dSRodney W. Grimes 			}
303df8bae1dSRodney W. Grimes 			m_adj(m, i);
304fb59c426SYoshinobu Inoue 			*tlenp -= i;
305fb59c426SYoshinobu Inoue 			th->th_seq += i;
306df8bae1dSRodney W. Grimes 		}
307df8bae1dSRodney W. Grimes 	}
308df8bae1dSRodney W. Grimes 	tcpstat.tcps_rcvoopack++;
309fb59c426SYoshinobu Inoue 	tcpstat.tcps_rcvoobyte += *tlenp;
310df8bae1dSRodney W. Grimes 
311df8bae1dSRodney W. Grimes 	/*
312df8bae1dSRodney W. Grimes 	 * While we overlap succeeding segments trim them or,
313df8bae1dSRodney W. Grimes 	 * if they are completely covered, dequeue them.
314df8bae1dSRodney W. Grimes 	 */
3156effc713SDoug Rabson 	while (q) {
316fb59c426SYoshinobu Inoue 		register int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq;
317df8bae1dSRodney W. Grimes 		if (i <= 0)
318df8bae1dSRodney W. Grimes 			break;
319fb59c426SYoshinobu Inoue 		if (i < q->tqe_len) {
320fb59c426SYoshinobu Inoue 			q->tqe_th->th_seq += i;
321fb59c426SYoshinobu Inoue 			q->tqe_len -= i;
322fb59c426SYoshinobu Inoue 			m_adj(q->tqe_m, i);
323df8bae1dSRodney W. Grimes 			break;
324df8bae1dSRodney W. Grimes 		}
3256effc713SDoug Rabson 
326fb59c426SYoshinobu Inoue 		nq = LIST_NEXT(q, tqe_q);
327fb59c426SYoshinobu Inoue 		LIST_REMOVE(q, tqe_q);
328fb59c426SYoshinobu Inoue 		m_freem(q->tqe_m);
32912e2e970SAndre Oppermann 		uma_zfree(tcp_reass_zone, q);
33012e2e970SAndre Oppermann 		tp->t_segqlen--;
33112e2e970SAndre Oppermann 		tcp_reass_qsize--;
3326effc713SDoug Rabson 		q = nq;
333df8bae1dSRodney W. Grimes 	}
334df8bae1dSRodney W. Grimes 
335fb59c426SYoshinobu Inoue 	/* Insert the new segment queue entry into place. */
336fb59c426SYoshinobu Inoue 	te->tqe_m = m;
337fb59c426SYoshinobu Inoue 	te->tqe_th = th;
338fb59c426SYoshinobu Inoue 	te->tqe_len = *tlenp;
339fb59c426SYoshinobu Inoue 
3406effc713SDoug Rabson 	if (p == NULL) {
341fb59c426SYoshinobu Inoue 		LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q);
3426effc713SDoug Rabson 	} else {
343fb59c426SYoshinobu Inoue 		LIST_INSERT_AFTER(p, te, tqe_q);
3446effc713SDoug Rabson 	}
345df8bae1dSRodney W. Grimes 
346df8bae1dSRodney W. Grimes present:
347df8bae1dSRodney W. Grimes 	/*
348df8bae1dSRodney W. Grimes 	 * Present data to user, advancing rcv_nxt through
349df8bae1dSRodney W. Grimes 	 * completed sequence space.
350df8bae1dSRodney W. Grimes 	 */
351a0292f23SGarrett Wollman 	if (!TCPS_HAVEESTABLISHED(tp->t_state))
352df8bae1dSRodney W. Grimes 		return (0);
353fb59c426SYoshinobu Inoue 	q = LIST_FIRST(&tp->t_segq);
354fb59c426SYoshinobu Inoue 	if (!q || q->tqe_th->th_seq != tp->rcv_nxt)
355df8bae1dSRodney W. Grimes 		return (0);
3561e4d7da7SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
357df8bae1dSRodney W. Grimes 	do {
358fb59c426SYoshinobu Inoue 		tp->rcv_nxt += q->tqe_len;
359fb59c426SYoshinobu Inoue 		flags = q->tqe_th->th_flags & TH_FIN;
360fb59c426SYoshinobu Inoue 		nq = LIST_NEXT(q, tqe_q);
361fb59c426SYoshinobu Inoue 		LIST_REMOVE(q, tqe_q);
3621e4d7da7SRobert Watson 		/* Unlocked read. */
363c0b99ffaSRobert Watson 		if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
364fb59c426SYoshinobu Inoue 			m_freem(q->tqe_m);
3654cc20ab1SSeigo Tanimura 		else
3661e4d7da7SRobert Watson 			sbappendstream_locked(&so->so_rcv, q->tqe_m);
36712e2e970SAndre Oppermann 		uma_zfree(tcp_reass_zone, q);
36812e2e970SAndre Oppermann 		tp->t_segqlen--;
36912e2e970SAndre Oppermann 		tcp_reass_qsize--;
3706effc713SDoug Rabson 		q = nq;
371fb59c426SYoshinobu Inoue 	} while (q && q->tqe_th->th_seq == tp->rcv_nxt);
372fb59c426SYoshinobu Inoue 	ND6_HINT(tp);
3731e4d7da7SRobert Watson 	sorwakeup_locked(so);
374df8bae1dSRodney W. Grimes 	return (flags);
375df8bae1dSRodney W. Grimes }
376df8bae1dSRodney W. Grimes 
377df8bae1dSRodney W. Grimes /*
378df8bae1dSRodney W. Grimes  * TCP input routine, follows pages 65-76 of the
379df8bae1dSRodney W. Grimes  * protocol specification dated September, 1981 very closely.
380df8bae1dSRodney W. Grimes  */
381fb59c426SYoshinobu Inoue #ifdef INET6
382fb59c426SYoshinobu Inoue int
383fb59c426SYoshinobu Inoue tcp6_input(mp, offp, proto)
384fb59c426SYoshinobu Inoue 	struct mbuf **mp;
385fb59c426SYoshinobu Inoue 	int *offp, proto;
386fb59c426SYoshinobu Inoue {
387fb59c426SYoshinobu Inoue 	register struct mbuf *m = *mp;
38833841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6;
389fb59c426SYoshinobu Inoue 
390fb59c426SYoshinobu Inoue 	IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE);
391fb59c426SYoshinobu Inoue 
392fb59c426SYoshinobu Inoue 	/*
393fb59c426SYoshinobu Inoue 	 * draft-itojun-ipv6-tcp-to-anycast
394fb59c426SYoshinobu Inoue 	 * better place to put this in?
395fb59c426SYoshinobu Inoue 	 */
39633841545SHajimu UMEMOTO 	ia6 = ip6_getdstifaddr(m);
39733841545SHajimu UMEMOTO 	if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
398fb59c426SYoshinobu Inoue 		struct ip6_hdr *ip6;
399fb59c426SYoshinobu Inoue 
400fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
401fb59c426SYoshinobu Inoue 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
402fb59c426SYoshinobu Inoue 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
403fb59c426SYoshinobu Inoue 		return IPPROTO_DONE;
404fb59c426SYoshinobu Inoue 	}
405fb59c426SYoshinobu Inoue 
406f0ffb944SJulian Elischer 	tcp_input(m, *offp);
407fb59c426SYoshinobu Inoue 	return IPPROTO_DONE;
408fb59c426SYoshinobu Inoue }
409fb59c426SYoshinobu Inoue #endif
410fb59c426SYoshinobu Inoue 
411df8bae1dSRodney W. Grimes void
412f0ffb944SJulian Elischer tcp_input(m, off0)
413df8bae1dSRodney W. Grimes 	register struct mbuf *m;
414f0ffb944SJulian Elischer 	int off0;
415df8bae1dSRodney W. Grimes {
416fb59c426SYoshinobu Inoue 	register struct tcphdr *th;
417fb59c426SYoshinobu Inoue 	register struct ip *ip = NULL;
418fb59c426SYoshinobu Inoue 	register struct ipovly *ipov;
419f76fcf6dSJeffrey Hsu 	register struct inpcb *inp = NULL;
420e79adb8eSGarrett Wollman 	u_char *optp = NULL;
42126f9a767SRodney W. Grimes 	int optlen = 0;
422a0194ef1SBruce M Simpson 	int len, tlen, off;
423fb59c426SYoshinobu Inoue 	int drop_hdrlen;
424df8bae1dSRodney W. Grimes 	register struct tcpcb *tp = 0;
425fb59c426SYoshinobu Inoue 	register int thflags;
42626f9a767SRodney W. Grimes 	struct socket *so = 0;
427df8bae1dSRodney W. Grimes 	int todrop, acked, ourfinisacked, needoutput = 0;
428a0292f23SGarrett Wollman 	u_long tiwin;
429a0292f23SGarrett Wollman 	struct tcpopt to;		/* options in this segment */
43097d8d152SAndre Oppermann 	struct rmxp_tao tao;		/* our TAO cache entry */
431f76fcf6dSJeffrey Hsu 	int headlocked = 0;
4329b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
4339b932e9eSAndre Oppermann 	struct m_tag *fwd_tag;
4349b932e9eSAndre Oppermann #endif
435c068736aSJeffrey Hsu 	int rstreason; /* For badport_bandlim accounting purposes */
436c068736aSJeffrey Hsu 
437c068736aSJeffrey Hsu 	struct ip6_hdr *ip6 = NULL;
438c068736aSJeffrey Hsu #ifdef INET6
439c068736aSJeffrey Hsu 	int isipv6;
440c068736aSJeffrey Hsu #else
441c068736aSJeffrey Hsu 	const int isipv6 = 0;
442c068736aSJeffrey Hsu #endif
443f76fcf6dSJeffrey Hsu 
444610ee2f9SDavid Greenman #ifdef TCPDEBUG
445c068736aSJeffrey Hsu 	/*
446c068736aSJeffrey Hsu 	 * The size of tcp_saveipgen must be the size of the max ip header,
447c068736aSJeffrey Hsu 	 * now IPv6.
448c068736aSJeffrey Hsu 	 */
449410bb1bfSLuigi Rizzo 	u_char tcp_saveipgen[40];
450410bb1bfSLuigi Rizzo 	struct tcphdr tcp_savetcp;
451610ee2f9SDavid Greenman 	short ostate = 0;
452610ee2f9SDavid Greenman #endif
453c068736aSJeffrey Hsu 
454fb59c426SYoshinobu Inoue #ifdef INET6
455fb59c426SYoshinobu Inoue 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
456fb59c426SYoshinobu Inoue #endif
45797d8d152SAndre Oppermann 	bzero(&tao, sizeof(tao));
458a0292f23SGarrett Wollman 	bzero((char *)&to, sizeof(to));
459a0292f23SGarrett Wollman 
460df8bae1dSRodney W. Grimes 	tcpstat.tcps_rcvtotal++;
461fb59c426SYoshinobu Inoue 
462fb59c426SYoshinobu Inoue 	if (isipv6) {
46304d3a452SHajimu UMEMOTO #ifdef INET6
464fb59c426SYoshinobu Inoue 		/* IP6_EXTHDR_CHECK() is already done at tcp6_input() */
465fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
466fb59c426SYoshinobu Inoue 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
467fb59c426SYoshinobu Inoue 		if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) {
468fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbadsum++;
469fb59c426SYoshinobu Inoue 			goto drop;
470fb59c426SYoshinobu Inoue 		}
471fb59c426SYoshinobu Inoue 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
47233841545SHajimu UMEMOTO 
47333841545SHajimu UMEMOTO 		/*
47433841545SHajimu UMEMOTO 		 * Be proactive about unspecified IPv6 address in source.
47533841545SHajimu UMEMOTO 		 * As we use all-zero to indicate unbounded/unconnected pcb,
47633841545SHajimu UMEMOTO 		 * unspecified IPv6 address can be used to confuse us.
47733841545SHajimu UMEMOTO 		 *
47833841545SHajimu UMEMOTO 		 * Note that packets with unspecified IPv6 destination is
47933841545SHajimu UMEMOTO 		 * already dropped in ip6_input.
48033841545SHajimu UMEMOTO 		 */
48133841545SHajimu UMEMOTO 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
48233841545SHajimu UMEMOTO 			/* XXX stat */
48333841545SHajimu UMEMOTO 			goto drop;
48433841545SHajimu UMEMOTO 		}
48504d3a452SHajimu UMEMOTO #else
48604d3a452SHajimu UMEMOTO 		th = NULL;		/* XXX: avoid compiler warning */
48704d3a452SHajimu UMEMOTO #endif
488c068736aSJeffrey Hsu 	} else {
489df8bae1dSRodney W. Grimes 		/*
490df8bae1dSRodney W. Grimes 		 * Get IP and TCP header together in first mbuf.
491df8bae1dSRodney W. Grimes 		 * Note: IP leaves IP header in first mbuf.
492df8bae1dSRodney W. Grimes 		 */
493fb59c426SYoshinobu Inoue 		if (off0 > sizeof (struct ip)) {
494df8bae1dSRodney W. Grimes 			ip_stripoptions(m, (struct mbuf *)0);
495fb59c426SYoshinobu Inoue 			off0 = sizeof(struct ip);
496fb59c426SYoshinobu Inoue 		}
497df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof (struct tcpiphdr)) {
498df8bae1dSRodney W. Grimes 			if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
499df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvshort++;
500df8bae1dSRodney W. Grimes 				return;
501df8bae1dSRodney W. Grimes 			}
502df8bae1dSRodney W. Grimes 		}
503fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
504fb59c426SYoshinobu Inoue 		ipov = (struct ipovly *)ip;
505db4f9cc7SJonathan Lemon 		th = (struct tcphdr *)((caddr_t)ip + off0);
506db4f9cc7SJonathan Lemon 		tlen = ip->ip_len;
507df8bae1dSRodney W. Grimes 
508db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
509db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
510db4f9cc7SJonathan Lemon 				th->th_sum = m->m_pkthdr.csum_data;
511db4f9cc7SJonathan Lemon 			else
512db4f9cc7SJonathan Lemon 				th->th_sum = in_pseudo(ip->ip_src.s_addr,
513c068736aSJeffrey Hsu 						ip->ip_dst.s_addr,
514c068736aSJeffrey Hsu 						htonl(m->m_pkthdr.csum_data +
515c068736aSJeffrey Hsu 							ip->ip_len +
516c068736aSJeffrey Hsu 							IPPROTO_TCP));
517db4f9cc7SJonathan Lemon 			th->th_sum ^= 0xffff;
5183c653157SHartmut Brandt #ifdef TCPDEBUG
5193c653157SHartmut Brandt 			ipov->ih_len = (u_short)tlen;
5203c653157SHartmut Brandt 			ipov->ih_len = htons(ipov->ih_len);
5213c653157SHartmut Brandt #endif
522db4f9cc7SJonathan Lemon 		} else {
523df8bae1dSRodney W. Grimes 			/*
524df8bae1dSRodney W. Grimes 			 * Checksum extended TCP header and data.
525df8bae1dSRodney W. Grimes 			 */
526df8bae1dSRodney W. Grimes 			len = sizeof (struct ip) + tlen;
527fb59c426SYoshinobu Inoue 			bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
528fb59c426SYoshinobu Inoue 			ipov->ih_len = (u_short)tlen;
529fd8e4ebcSMike Barcroft 			ipov->ih_len = htons(ipov->ih_len);
530fb59c426SYoshinobu Inoue 			th->th_sum = in_cksum(m, len);
531db4f9cc7SJonathan Lemon 		}
532fb59c426SYoshinobu Inoue 		if (th->th_sum) {
533df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbadsum++;
534df8bae1dSRodney W. Grimes 			goto drop;
535df8bae1dSRodney W. Grimes 		}
536fb59c426SYoshinobu Inoue #ifdef INET6
537fb59c426SYoshinobu Inoue 		/* Re-initialization for later version check */
538fb59c426SYoshinobu Inoue 		ip->ip_v = IPVERSION;
539fb59c426SYoshinobu Inoue #endif
540fb59c426SYoshinobu Inoue 	}
541df8bae1dSRodney W. Grimes 
542df8bae1dSRodney W. Grimes 	/*
543df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
544df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
545df8bae1dSRodney W. Grimes 	 */
546fb59c426SYoshinobu Inoue 	off = th->th_off << 2;
547df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
548df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvbadoff++;
549df8bae1dSRodney W. Grimes 		goto drop;
550df8bae1dSRodney W. Grimes 	}
551fb59c426SYoshinobu Inoue 	tlen -= off;	/* tlen is used instead of ti->ti_len */
552df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
553fb59c426SYoshinobu Inoue 		if (isipv6) {
55404d3a452SHajimu UMEMOTO #ifdef INET6
555fb59c426SYoshinobu Inoue 			IP6_EXTHDR_CHECK(m, off0, off, );
556fb59c426SYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
557fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
55804d3a452SHajimu UMEMOTO #endif
559c068736aSJeffrey Hsu 		} else {
560df8bae1dSRodney W. Grimes 			if (m->m_len < sizeof(struct ip) + off) {
561c068736aSJeffrey Hsu 				if ((m = m_pullup(m, sizeof (struct ip) + off))
562c068736aSJeffrey Hsu 						== 0) {
563df8bae1dSRodney W. Grimes 					tcpstat.tcps_rcvshort++;
564df8bae1dSRodney W. Grimes 					return;
565df8bae1dSRodney W. Grimes 				}
566fb59c426SYoshinobu Inoue 				ip = mtod(m, struct ip *);
567fb59c426SYoshinobu Inoue 				ipov = (struct ipovly *)ip;
568fb59c426SYoshinobu Inoue 				th = (struct tcphdr *)((caddr_t)ip + off0);
569fb59c426SYoshinobu Inoue 			}
570df8bae1dSRodney W. Grimes 		}
571df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
572fb59c426SYoshinobu Inoue 		optp = (u_char *)(th + 1);
573df8bae1dSRodney W. Grimes 	}
574fb59c426SYoshinobu Inoue 	thflags = th->th_flags;
575df8bae1dSRodney W. Grimes 
576e46cd3d4SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN
577e46cd3d4SDag-Erling Smørgrav 	/*
578e46cd3d4SDag-Erling Smørgrav 	 * If the drop_synfin option is enabled, drop all packets with
579e46cd3d4SDag-Erling Smørgrav 	 * both the SYN and FIN bits set. This prevents e.g. nmap from
580e46cd3d4SDag-Erling Smørgrav 	 * identifying the TCP/IP stack.
581e46cd3d4SDag-Erling Smørgrav 	 *
582a589a70eSGarrett Wollman 	 * This is a violation of the TCP specification.
583e46cd3d4SDag-Erling Smørgrav 	 */
584fb59c426SYoshinobu Inoue 	if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN))
585e46cd3d4SDag-Erling Smørgrav 		goto drop;
586e46cd3d4SDag-Erling Smørgrav #endif
587e46cd3d4SDag-Erling Smørgrav 
588df8bae1dSRodney W. Grimes 	/*
589df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
590df8bae1dSRodney W. Grimes 	 */
591fd8e4ebcSMike Barcroft 	th->th_seq = ntohl(th->th_seq);
592fd8e4ebcSMike Barcroft 	th->th_ack = ntohl(th->th_ack);
593fd8e4ebcSMike Barcroft 	th->th_win = ntohs(th->th_win);
594fd8e4ebcSMike Barcroft 	th->th_urp = ntohs(th->th_urp);
595df8bae1dSRodney W. Grimes 
596df8bae1dSRodney W. Grimes 	/*
5973bfd6421SJonathan Lemon 	 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options,
598fb59c426SYoshinobu Inoue 	 * until after ip6_savecontrol() is called and before other functions
599fb59c426SYoshinobu Inoue 	 * which don't want those proto headers.
600fb59c426SYoshinobu Inoue 	 * Because ip6_savecontrol() is going to parse the mbuf to
601fb59c426SYoshinobu Inoue 	 * search for data to be passed up to user-land, it wants mbuf
602fb59c426SYoshinobu Inoue 	 * parameters to be unchanged.
60388ff5695SSUZUKI Shinsuke 	 * XXX: the call of ip6_savecontrol() has been obsoleted based on
60488ff5695SSUZUKI Shinsuke 	 * latest version of the advanced API (20020110).
605755c1f07SAndras Olah 	 */
606fb59c426SYoshinobu Inoue 	drop_hdrlen = off0 + off;
607755c1f07SAndras Olah 
608755c1f07SAndras Olah 	/*
609df8bae1dSRodney W. Grimes 	 * Locate pcb for segment.
610df8bae1dSRodney W. Grimes 	 */
611f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&tcbinfo);
612f76fcf6dSJeffrey Hsu 	headlocked = 1;
613df8bae1dSRodney W. Grimes findpcb:
6149b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
6159b932e9eSAndre Oppermann 	/* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */
6169b932e9eSAndre Oppermann 	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
6179b932e9eSAndre Oppermann 
6189b932e9eSAndre Oppermann 	if (fwd_tag != NULL && isipv6 == 0) {	/* IPv6 support is not yet */
6199b932e9eSAndre Oppermann 		struct sockaddr_in *next_hop;
6209b932e9eSAndre Oppermann 
6219b932e9eSAndre Oppermann 		next_hop = (struct sockaddr_in *)(fwd_tag+1);
622f9e354dfSJulian Elischer 		/*
6232b25acc1SLuigi Rizzo 		 * Transparently forwarded. Pretend to be the destination.
624f9e354dfSJulian Elischer 		 * already got one like this?
625f9e354dfSJulian Elischer 		 */
6269b932e9eSAndre Oppermann 		inp = in_pcblookup_hash(&tcbinfo,
6279b932e9eSAndre Oppermann 					ip->ip_src, th->th_sport,
628c068736aSJeffrey Hsu 					ip->ip_dst, th->th_dport,
629c068736aSJeffrey Hsu 					0, m->m_pkthdr.rcvif);
630f9e354dfSJulian Elischer 		if (!inp) {
6319b932e9eSAndre Oppermann 			/* It's new.  Try to find the ambushing socket. */
632f9e354dfSJulian Elischer 			inp = in_pcblookup_hash(&tcbinfo,
633fb59c426SYoshinobu Inoue 						ip->ip_src, th->th_sport,
6342b25acc1SLuigi Rizzo 						next_hop->sin_addr,
635c068736aSJeffrey Hsu 						next_hop->sin_port ?
636c068736aSJeffrey Hsu 						    ntohs(next_hop->sin_port) :
637c068736aSJeffrey Hsu 						    th->th_dport,
638c068736aSJeffrey Hsu 						1, m->m_pkthdr.rcvif);
639f9e354dfSJulian Elischer 		}
6409b932e9eSAndre Oppermann 		/* Remove the tag from the packet.  We don't need it anymore. */
6419b932e9eSAndre Oppermann 		m_tag_delete(m, fwd_tag);
642c068736aSJeffrey Hsu 	} else {
6439b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */
64404d3a452SHajimu UMEMOTO 		if (isipv6) {
64504d3a452SHajimu UMEMOTO #ifdef INET6
646c068736aSJeffrey Hsu 			inp = in6_pcblookup_hash(&tcbinfo,
647c068736aSJeffrey Hsu 						 &ip6->ip6_src, th->th_sport,
648c068736aSJeffrey Hsu 						 &ip6->ip6_dst, th->th_dport,
649c068736aSJeffrey Hsu 						 1, m->m_pkthdr.rcvif);
65004d3a452SHajimu UMEMOTO #endif
65104d3a452SHajimu UMEMOTO 		} else
652c068736aSJeffrey Hsu 			inp = in_pcblookup_hash(&tcbinfo,
653c068736aSJeffrey Hsu 						ip->ip_src, th->th_sport,
654c068736aSJeffrey Hsu 						ip->ip_dst, th->th_dport,
655c068736aSJeffrey Hsu 						1, m->m_pkthdr.rcvif);
6569b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
657fb59c426SYoshinobu Inoue 	}
6589b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */
659f9e354dfSJulian Elischer 
660da0f4099SHajimu UMEMOTO #if defined(IPSEC) || defined(FAST_IPSEC)
661d420fcdaSBruce M Simpson #ifdef INET6
662da0f4099SHajimu UMEMOTO 	if (isipv6) {
663da0f4099SHajimu UMEMOTO 		if (inp != NULL && ipsec6_in_reject(m, inp)) {
664fb59c426SYoshinobu Inoue #ifdef IPSEC
665fb59c426SYoshinobu Inoue 			ipsec6stat.in_polvio++;
666d420fcdaSBruce M Simpson #endif
667fb59c426SYoshinobu Inoue 			goto drop;
668fb59c426SYoshinobu Inoue 		}
669d420fcdaSBruce M Simpson 	} else
670d420fcdaSBruce M Simpson #endif /* INET6 */
671d420fcdaSBruce M Simpson 	if (inp != NULL && ipsec4_in_reject(m, inp)) {
672da0f4099SHajimu UMEMOTO #ifdef IPSEC
673fb59c426SYoshinobu Inoue 		ipsecstat.in_polvio++;
674d420fcdaSBruce M Simpson #endif
675fb59c426SYoshinobu Inoue 		goto drop;
676fb59c426SYoshinobu Inoue 	}
677da0f4099SHajimu UMEMOTO #endif /*IPSEC || FAST_IPSEC*/
678df8bae1dSRodney W. Grimes 
679df8bae1dSRodney W. Grimes 	/*
680df8bae1dSRodney W. Grimes 	 * If the state is CLOSED (i.e., TCB does not exist) then
681df8bae1dSRodney W. Grimes 	 * all data in the incoming segment is discarded.
682df8bae1dSRodney W. Grimes 	 * If the TCB exists but is in CLOSED state, it is embryonic,
683df8bae1dSRodney W. Grimes 	 * but should either do a listen or a connect soon.
684df8bae1dSRodney W. Grimes 	 */
685816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
6862e4e1b4cSGeoff Rehmet 		if (log_in_vain) {
687fb59c426SYoshinobu Inoue #ifdef INET6
688ded7008aSJuli Mallett 			char dbuf[INET6_ADDRSTRLEN+2], sbuf[INET6_ADDRSTRLEN+2];
689c068736aSJeffrey Hsu #else
690fb59c426SYoshinobu Inoue 			char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"];
691c068736aSJeffrey Hsu #endif
69275cfc95fSAndrey A. Chernov 
693fb59c426SYoshinobu Inoue 			if (isipv6) {
69404d3a452SHajimu UMEMOTO #ifdef INET6
695ded7008aSJuli Mallett 				strcpy(dbuf, "[");
696ded7008aSJuli Mallett 				strcpy(sbuf, "[");
697ded7008aSJuli Mallett 				strcat(dbuf, ip6_sprintf(&ip6->ip6_dst));
698ded7008aSJuli Mallett 				strcat(sbuf, ip6_sprintf(&ip6->ip6_src));
699ded7008aSJuli Mallett 				strcat(dbuf, "]");
700ded7008aSJuli Mallett 				strcat(sbuf, "]");
70104d3a452SHajimu UMEMOTO #endif
702c068736aSJeffrey Hsu 			} else {
703fb59c426SYoshinobu Inoue 				strcpy(dbuf, inet_ntoa(ip->ip_dst));
704fb59c426SYoshinobu Inoue 				strcpy(sbuf, inet_ntoa(ip->ip_src));
705fb59c426SYoshinobu Inoue 			}
7062e4e1b4cSGeoff Rehmet 			switch (log_in_vain) {
7072e4e1b4cSGeoff Rehmet 			case 1:
70839eb27a4SCrist J. Clark 				if ((thflags & TH_SYN) == 0)
7092e4e1b4cSGeoff Rehmet 					break;
710e4d2978dSPoul-Henning Kamp 				/* FALLTHROUGH */
7112e4e1b4cSGeoff Rehmet 			case 2:
7122e4e1b4cSGeoff Rehmet 				log(LOG_INFO,
713c068736aSJeffrey Hsu 				    "Connection attempt to TCP %s:%d "
71439eb27a4SCrist J. Clark 				    "from %s:%d flags:0x%02x\n",
715fb59c426SYoshinobu Inoue 				    dbuf, ntohs(th->th_dport), sbuf,
716fb59c426SYoshinobu Inoue 				    ntohs(th->th_sport), thflags);
7172e4e1b4cSGeoff Rehmet 				break;
7182e4e1b4cSGeoff Rehmet 			default:
7192e4e1b4cSGeoff Rehmet 				break;
7202e4e1b4cSGeoff Rehmet 			}
72175cfc95fSAndrey A. Chernov 		}
7222e4e1b4cSGeoff Rehmet 		if (blackhole) {
7232e4e1b4cSGeoff Rehmet 			switch (blackhole) {
724828b7f40SGeoff Rehmet 			case 1:
725fb59c426SYoshinobu Inoue 				if (thflags & TH_SYN)
726828b7f40SGeoff Rehmet 					goto drop;
727828b7f40SGeoff Rehmet 				break;
728828b7f40SGeoff Rehmet 			case 2:
729828b7f40SGeoff Rehmet 				goto drop;
730828b7f40SGeoff Rehmet 			default:
731828b7f40SGeoff Rehmet 				goto drop;
7322e4e1b4cSGeoff Rehmet 			}
733828b7f40SGeoff Rehmet 		}
734a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
735a57815efSBosko Milekic 		goto dropwithreset;
736816a3d83SPoul-Henning Kamp 	}
737f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
738340c35deSJonathan Lemon 	if (inp->inp_vflag & INP_TIMEWAIT) {
739340c35deSJonathan Lemon 		/*
740340c35deSJonathan Lemon 		 * The only option of relevance is TOF_CC, and only if
741340c35deSJonathan Lemon 		 * present in a SYN segment.  See tcp_timewait().
742340c35deSJonathan Lemon 		 */
743340c35deSJonathan Lemon 		if (thflags & TH_SYN)
7446d90faf3SPaul Saab 			tcp_dooptions((struct tcpcb *)NULL, &to, optp, optlen, 1, th);
745340c35deSJonathan Lemon 		if (tcp_timewait((struct tcptw *)inp->inp_ppcb,
746340c35deSJonathan Lemon 		    &to, th, m, tlen))
747340c35deSJonathan Lemon 			goto findpcb;
748340c35deSJonathan Lemon 		/*
749340c35deSJonathan Lemon 		 * tcp_timewait unlocks inp.
750340c35deSJonathan Lemon 		 */
751340c35deSJonathan Lemon 		INP_INFO_WUNLOCK(&tcbinfo);
752340c35deSJonathan Lemon 		return;
753340c35deSJonathan Lemon 	}
754df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
75509f81a46SBosko Milekic 	if (tp == 0) {
756f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
757a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
758a57815efSBosko Milekic 		goto dropwithreset;
75909f81a46SBosko Milekic 	}
760df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_CLOSED)
761df8bae1dSRodney W. Grimes 		goto drop;
762df8bae1dSRodney W. Grimes 
763df8bae1dSRodney W. Grimes 	/* Unscale the window into a 32-bit value. */
764fb59c426SYoshinobu Inoue 	if ((thflags & TH_SYN) == 0)
765fb59c426SYoshinobu Inoue 		tiwin = th->th_win << tp->snd_scale;
766df8bae1dSRodney W. Grimes 	else
767fb59c426SYoshinobu Inoue 		tiwin = th->th_win;
768fb59c426SYoshinobu Inoue 
769c488362eSRobert Watson #ifdef MAC
7701f82efb3SRobert Watson 	INP_LOCK_ASSERT(inp);
771a557af22SRobert Watson 	if (mac_check_inpcb_deliver(inp, m))
772c488362eSRobert Watson 		goto drop;
773c488362eSRobert Watson #endif
774a557af22SRobert Watson 	so = inp->inp_socket;
775610ee2f9SDavid Greenman #ifdef TCPDEBUG
776df8bae1dSRodney W. Grimes 	if (so->so_options & SO_DEBUG) {
777df8bae1dSRodney W. Grimes 		ostate = tp->t_state;
778fb59c426SYoshinobu Inoue 		if (isipv6)
779540e8b7eSJeffrey Hsu 			bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6));
780fb59c426SYoshinobu Inoue 		else
781540e8b7eSJeffrey Hsu 			bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
782fb59c426SYoshinobu Inoue 		tcp_savetcp = *th;
783df8bae1dSRodney W. Grimes 	}
784610ee2f9SDavid Greenman #endif
785540e8b7eSJeffrey Hsu 	if (so->so_options & SO_ACCEPTCONN) {
786540e8b7eSJeffrey Hsu 		struct in_conninfo inc;
787540e8b7eSJeffrey Hsu 
788fb59c426SYoshinobu Inoue #ifdef INET6
789be2ac88cSJonathan Lemon 		inc.inc_isipv6 = isipv6;
790c068736aSJeffrey Hsu #endif
791be2ac88cSJonathan Lemon 		if (isipv6) {
792be2ac88cSJonathan Lemon 			inc.inc6_faddr = ip6->ip6_src;
793be2ac88cSJonathan Lemon 			inc.inc6_laddr = ip6->ip6_dst;
794c068736aSJeffrey Hsu 		} else {
795be2ac88cSJonathan Lemon 			inc.inc_faddr = ip->ip_src;
796be2ac88cSJonathan Lemon 			inc.inc_laddr = ip->ip_dst;
797be2ac88cSJonathan Lemon 		}
798be2ac88cSJonathan Lemon 		inc.inc_fport = th->th_sport;
799be2ac88cSJonathan Lemon 		inc.inc_lport = th->th_dport;
800be2ac88cSJonathan Lemon 
801fb59c426SYoshinobu Inoue 	        /*
802be2ac88cSJonathan Lemon 	         * If the state is LISTEN then ignore segment if it contains
803be2ac88cSJonathan Lemon 		 * a RST.  If the segment contains an ACK then it is bad and
804be2ac88cSJonathan Lemon 		 * send a RST.  If it does not contain a SYN then it is not
805be2ac88cSJonathan Lemon 		 * interesting; drop it.
806be2ac88cSJonathan Lemon 		 *
807be2ac88cSJonathan Lemon 		 * If the state is SYN_RECEIVED (syncache) and seg contains
808be2ac88cSJonathan Lemon 		 * an ACK, but not for our SYN/ACK, send a RST.  If the seg
809be2ac88cSJonathan Lemon 		 * contains a RST, check the sequence number to see if it
810be2ac88cSJonathan Lemon 		 * is a valid reset segment.
811fb59c426SYoshinobu Inoue 		 */
812fb59c426SYoshinobu Inoue 		if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
813be2ac88cSJonathan Lemon 			if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
814be2ac88cSJonathan Lemon 				if (!syncache_expand(&inc, th, &so, m)) {
8154195b4afSPaul Traina 					/*
816be2ac88cSJonathan Lemon 					 * No syncache entry, or ACK was not
817be2ac88cSJonathan Lemon 					 * for our SYN/ACK.  Send a RST.
8184195b4afSPaul Traina 					 */
819be2ac88cSJonathan Lemon 					tcpstat.tcps_badsyn++;
820be2ac88cSJonathan Lemon 					rstreason = BANDLIM_RST_OPENPORT;
821be2ac88cSJonathan Lemon 					goto dropwithreset;
822be2ac88cSJonathan Lemon 				}
823f76fcf6dSJeffrey Hsu 				if (so == NULL) {
824be2ac88cSJonathan Lemon 					/*
825be2ac88cSJonathan Lemon 					 * Could not complete 3-way handshake,
826be2ac88cSJonathan Lemon 					 * connection is being closed down, and
827be2ac88cSJonathan Lemon 					 * syncache will free mbuf.
828be2ac88cSJonathan Lemon 					 */
829f76fcf6dSJeffrey Hsu 					INP_UNLOCK(inp);
830f76fcf6dSJeffrey Hsu 					INP_INFO_WUNLOCK(&tcbinfo);
831be2ac88cSJonathan Lemon 					return;
832f76fcf6dSJeffrey Hsu 				}
833be2ac88cSJonathan Lemon 				/*
834be2ac88cSJonathan Lemon 				 * Socket is created in state SYN_RECEIVED.
835be2ac88cSJonathan Lemon 				 * Continue processing segment.
836be2ac88cSJonathan Lemon 				 */
837f76fcf6dSJeffrey Hsu 				INP_UNLOCK(inp);
838be2ac88cSJonathan Lemon 				inp = sotoinpcb(so);
839f76fcf6dSJeffrey Hsu 				INP_LOCK(inp);
840be2ac88cSJonathan Lemon 				tp = intotcpcb(inp);
841be2ac88cSJonathan Lemon 				/*
842be2ac88cSJonathan Lemon 				 * This is what would have happened in
843e6658b12SRobert Watson 				 * tcp_output() when the SYN,ACK was sent.
844be2ac88cSJonathan Lemon 				 */
845be2ac88cSJonathan Lemon 				tp->snd_up = tp->snd_una;
846be2ac88cSJonathan Lemon 				tp->snd_max = tp->snd_nxt = tp->iss + 1;
847be2ac88cSJonathan Lemon 				tp->last_ack_sent = tp->rcv_nxt;
848be2ac88cSJonathan Lemon 				/*
84941446225SJonathan Lemon 				 * RFC1323: The window in SYN & SYN/ACK
85041446225SJonathan Lemon 				 * segments is never scaled.
851be2ac88cSJonathan Lemon 				 */
852be2ac88cSJonathan Lemon 				tp->snd_wnd = tiwin;	/* unscaled */
853be2ac88cSJonathan Lemon 				goto after_listen;
854be2ac88cSJonathan Lemon 			}
855be2ac88cSJonathan Lemon 			if (thflags & TH_RST) {
856be2ac88cSJonathan Lemon 				syncache_chkrst(&inc, th);
857be2ac88cSJonathan Lemon 				goto drop;
858be2ac88cSJonathan Lemon 			}
859fb59c426SYoshinobu Inoue 			if (thflags & TH_ACK) {
860be2ac88cSJonathan Lemon 				syncache_badack(&inc);
861ebb0cbeaSPaul Traina 				tcpstat.tcps_badsyn++;
862a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
863a57815efSBosko Milekic 				goto dropwithreset;
8644195b4afSPaul Traina 			}
865ebb0cbeaSPaul Traina 			goto drop;
866ebb0cbeaSPaul Traina 		}
86733841545SHajimu UMEMOTO 
868be2ac88cSJonathan Lemon 		/*
869be2ac88cSJonathan Lemon 		 * Segment's flags are (SYN) or (SYN|FIN).
870be2ac88cSJonathan Lemon 		 */
87133841545SHajimu UMEMOTO #ifdef INET6
87233841545SHajimu UMEMOTO 		/*
87333841545SHajimu UMEMOTO 		 * If deprecated address is forbidden,
87433841545SHajimu UMEMOTO 		 * we do not accept SYN to deprecated interface
87533841545SHajimu UMEMOTO 		 * address to prevent any new inbound connection from
87633841545SHajimu UMEMOTO 		 * getting established.
87733841545SHajimu UMEMOTO 		 * When we do not accept SYN, we send a TCP RST,
87833841545SHajimu UMEMOTO 		 * with deprecated source address (instead of dropping
87933841545SHajimu UMEMOTO 		 * it).  We compromise it as it is much better for peer
88033841545SHajimu UMEMOTO 		 * to send a RST, and RST will be the final packet
88133841545SHajimu UMEMOTO 		 * for the exchange.
88233841545SHajimu UMEMOTO 		 *
88333841545SHajimu UMEMOTO 		 * If we do not forbid deprecated addresses, we accept
88433841545SHajimu UMEMOTO 		 * the SYN packet.  RFC2462 does not suggest dropping
88533841545SHajimu UMEMOTO 		 * SYN in this case.
88633841545SHajimu UMEMOTO 		 * If we decipher RFC2462 5.5.4, it says like this:
88733841545SHajimu UMEMOTO 		 * 1. use of deprecated addr with existing
88833841545SHajimu UMEMOTO 		 *    communication is okay - "SHOULD continue to be
88933841545SHajimu UMEMOTO 		 *    used"
89033841545SHajimu UMEMOTO 		 * 2. use of it with new communication:
89133841545SHajimu UMEMOTO 		 *   (2a) "SHOULD NOT be used if alternate address
89233841545SHajimu UMEMOTO 		 *        with sufficient scope is available"
89333841545SHajimu UMEMOTO 		 *   (2b) nothing mentioned otherwise.
89433841545SHajimu UMEMOTO 		 * Here we fall into (2b) case as we have no choice in
89533841545SHajimu UMEMOTO 		 * our source address selection - we must obey the peer.
89633841545SHajimu UMEMOTO 		 *
89733841545SHajimu UMEMOTO 		 * The wording in RFC2462 is confusing, and there are
89833841545SHajimu UMEMOTO 		 * multiple description text for deprecated address
89933841545SHajimu UMEMOTO 		 * handling - worse, they are not exactly the same.
90033841545SHajimu UMEMOTO 		 * I believe 5.5.4 is the best one, so we follow 5.5.4.
90133841545SHajimu UMEMOTO 		 */
90233841545SHajimu UMEMOTO 		if (isipv6 && !ip6_use_deprecated) {
90333841545SHajimu UMEMOTO 			struct in6_ifaddr *ia6;
90433841545SHajimu UMEMOTO 
90533841545SHajimu UMEMOTO 			if ((ia6 = ip6_getdstifaddr(m)) &&
90633841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
907f76fcf6dSJeffrey Hsu 				INP_UNLOCK(inp);
90833841545SHajimu UMEMOTO 				tp = NULL;
90933841545SHajimu UMEMOTO 				rstreason = BANDLIM_RST_OPENPORT;
91033841545SHajimu UMEMOTO 				goto dropwithreset;
91133841545SHajimu UMEMOTO 			}
91233841545SHajimu UMEMOTO 		}
91333841545SHajimu UMEMOTO #endif
91465f28919SJesper Skriver 		/*
915be2ac88cSJonathan Lemon 		 * If it is from this socket, drop it, it must be forged.
916be2ac88cSJonathan Lemon 		 * Don't bother responding if the destination was a broadcast.
91765f28919SJesper Skriver 		 */
918be2ac88cSJonathan Lemon 		if (th->th_dport == th->th_sport) {
919fb59c426SYoshinobu Inoue 			if (isipv6) {
920be2ac88cSJonathan Lemon 				if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
921be2ac88cSJonathan Lemon 						       &ip6->ip6_src))
922be2ac88cSJonathan Lemon 					goto drop;
923c068736aSJeffrey Hsu 			} else {
924be2ac88cSJonathan Lemon 				if (ip->ip_dst.s_addr == ip->ip_src.s_addr)
925be2ac88cSJonathan Lemon 					goto drop;
926be2ac88cSJonathan Lemon 			}
927c068736aSJeffrey Hsu 		}
928be2ac88cSJonathan Lemon 		/*
929be2ac88cSJonathan Lemon 		 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
9302ca2159fSCrist J. Clark 		 *
93193ec91baSCrist J. Clark 		 * Note that it is quite possible to receive unicast
93293ec91baSCrist J. Clark 		 * link-layer packets with a broadcast IP address. Use
93393ec91baSCrist J. Clark 		 * in_broadcast() to find them.
934be2ac88cSJonathan Lemon 		 */
935be2ac88cSJonathan Lemon 		if (m->m_flags & (M_BCAST|M_MCAST))
936be2ac88cSJonathan Lemon 			goto drop;
937be2ac88cSJonathan Lemon 		if (isipv6) {
938be2ac88cSJonathan Lemon 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
939be2ac88cSJonathan Lemon 			    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
940be2ac88cSJonathan Lemon 				goto drop;
941c068736aSJeffrey Hsu 		} else {
942be2ac88cSJonathan Lemon 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
943be2ac88cSJonathan Lemon 			    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
9442ca2159fSCrist J. Clark 			    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
9452ca2159fSCrist J. Clark 			    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
946be2ac88cSJonathan Lemon 				goto drop;
947c068736aSJeffrey Hsu 		}
948be2ac88cSJonathan Lemon 		/*
949be2ac88cSJonathan Lemon 		 * SYN appears to be valid; create compressed TCP state
950be2ac88cSJonathan Lemon 		 * for syncache, or perform t/tcp connection.
951be2ac88cSJonathan Lemon 		 */
952be2ac88cSJonathan Lemon 		if (so->so_qlen <= so->so_qlimit) {
9533c653157SHartmut Brandt #ifdef TCPDEBUG
9543c653157SHartmut Brandt 			if (so->so_options & SO_DEBUG)
9553c653157SHartmut Brandt 				tcp_trace(TA_INPUT, ostate, tp,
9563c653157SHartmut Brandt 				    (void *)tcp_saveipgen, &tcp_savetcp, 0);
9573c653157SHartmut Brandt #endif
9586d90faf3SPaul Saab 			tcp_dooptions(tp, &to, optp, optlen, 1, th);
959be2ac88cSJonathan Lemon 			if (!syncache_add(&inc, &to, th, &so, m))
960be2ac88cSJonathan Lemon 				goto drop;
961f76fcf6dSJeffrey Hsu 			if (so == NULL) {
962be2ac88cSJonathan Lemon 				/*
963be2ac88cSJonathan Lemon 				 * Entry added to syncache, mbuf used to
964be2ac88cSJonathan Lemon 				 * send SYN,ACK packet.
965be2ac88cSJonathan Lemon 				 */
966f76fcf6dSJeffrey Hsu 				KASSERT(headlocked, ("headlocked"));
967f76fcf6dSJeffrey Hsu 				INP_UNLOCK(inp);
968f76fcf6dSJeffrey Hsu 				INP_INFO_WUNLOCK(&tcbinfo);
969be2ac88cSJonathan Lemon 				return;
970f76fcf6dSJeffrey Hsu 			}
971be2ac88cSJonathan Lemon 			/*
972be2ac88cSJonathan Lemon 			 * Segment passed TAO tests.
973be2ac88cSJonathan Lemon 			 */
974f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
975be2ac88cSJonathan Lemon 			inp = sotoinpcb(so);
976f76fcf6dSJeffrey Hsu 			INP_LOCK(inp);
977df8bae1dSRodney W. Grimes 			tp = intotcpcb(inp);
978be2ac88cSJonathan Lemon 			tp->snd_wnd = tiwin;
979be2ac88cSJonathan Lemon 			tp->t_starttime = ticks;
980be2ac88cSJonathan Lemon 			tp->t_state = TCPS_ESTABLISHED;
981df8bae1dSRodney W. Grimes 
982be2ac88cSJonathan Lemon 			/*
9833bfd6421SJonathan Lemon 			 * T/TCP logic:
9843bfd6421SJonathan Lemon 			 * If there is a FIN or if there is data, then
9853bfd6421SJonathan Lemon 			 * delay SYN,ACK(SYN) in the hope of piggy-backing
9863bfd6421SJonathan Lemon 			 * it on a response segment.  Otherwise must send
9873bfd6421SJonathan Lemon 			 * ACK now in case the other side is slow starting.
988be2ac88cSJonathan Lemon 			 */
9893bfd6421SJonathan Lemon 			if (thflags & TH_FIN || tlen != 0)
9903bfd6421SJonathan Lemon 				tp->t_flags |= (TF_DELACK | TF_NEEDSYN);
9913bfd6421SJonathan Lemon 			else
992f243998bSJonathan Lemon 				tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
993be2ac88cSJonathan Lemon 			tcpstat.tcps_connects++;
994be2ac88cSJonathan Lemon 			soisconnected(so);
995be2ac88cSJonathan Lemon 			goto trimthenstep6;
996df8bae1dSRodney W. Grimes 		}
997be2ac88cSJonathan Lemon 		goto drop;
9984cc20ab1SSeigo Tanimura 	}
999be2ac88cSJonathan Lemon after_listen:
10007cfc6904SRobert Watson 	KASSERT(headlocked, ("tcp_input(): after_listen head is not locked"));
10017cfc6904SRobert Watson 	INP_LOCK_ASSERT(inp);
1002be2ac88cSJonathan Lemon 
1003be2ac88cSJonathan Lemon 	/* XXX temp debugging */
1004be2ac88cSJonathan Lemon 	/* should not happen - syncache should pick up these connections */
1005be2ac88cSJonathan Lemon 	if (tp->t_state == TCPS_LISTEN)
1006be2ac88cSJonathan Lemon 		panic("tcp_input: TCPS_LISTEN");
1007df8bae1dSRodney W. Grimes 
1008df8bae1dSRodney W. Grimes 	/*
100953369ac9SAndre Oppermann 	 * This is the second part of the MSS DoS prevention code (after
101053369ac9SAndre Oppermann 	 * minmss on the sending side) and it deals with too many too small
101153369ac9SAndre Oppermann 	 * tcp packets in a too short timeframe (1 second).
101253369ac9SAndre Oppermann 	 *
101353369ac9SAndre Oppermann 	 * For every full second we count the number of received packets
101453369ac9SAndre Oppermann 	 * and bytes. If we get a lot of packets per second for this connection
101553369ac9SAndre Oppermann 	 * (tcp_minmssoverload) we take a closer look at it and compute the
101653369ac9SAndre Oppermann 	 * average packet size for the past second. If that is less than
101753369ac9SAndre Oppermann 	 * tcp_minmss we get too many packets with very small payload which
101853369ac9SAndre Oppermann 	 * is not good and burdens our system (and every packet generates
101953369ac9SAndre Oppermann 	 * a wakeup to the process connected to our socket). We can reasonable
102053369ac9SAndre Oppermann 	 * expect this to be small packet DoS attack to exhaust our CPU
102153369ac9SAndre Oppermann 	 * cycles.
102253369ac9SAndre Oppermann 	 *
102353369ac9SAndre Oppermann 	 * Care has to be taken for the minimum packet overload value. This
102453369ac9SAndre Oppermann 	 * value defines the minimum number of packets per second before we
102553369ac9SAndre Oppermann 	 * start to worry. This must not be too low to avoid killing for
102653369ac9SAndre Oppermann 	 * example interactive connections with many small packets like
102753369ac9SAndre Oppermann 	 * telnet or SSH.
102853369ac9SAndre Oppermann 	 *
102953369ac9SAndre Oppermann 	 * Setting either tcp_minmssoverload or tcp_minmss to "0" disables
103053369ac9SAndre Oppermann 	 * this check.
103153369ac9SAndre Oppermann 	 *
103253369ac9SAndre Oppermann 	 * Account for packet if payload packet, skip over ACK, etc.
103353369ac9SAndre Oppermann 	 */
103453369ac9SAndre Oppermann 	if (tcp_minmss && tcp_minmssoverload &&
103553369ac9SAndre Oppermann 	    tp->t_state == TCPS_ESTABLISHED && tlen > 0) {
103653369ac9SAndre Oppermann 		if (tp->rcv_second > ticks) {
103753369ac9SAndre Oppermann 			tp->rcv_pps++;
103853369ac9SAndre Oppermann 			tp->rcv_byps += tlen + off;
103953369ac9SAndre Oppermann 			if (tp->rcv_pps > tcp_minmssoverload) {
104053369ac9SAndre Oppermann 				if ((tp->rcv_byps / tp->rcv_pps) < tcp_minmss) {
104153369ac9SAndre Oppermann 					printf("too many small tcp packets from "
104253369ac9SAndre Oppermann 					       "%s:%u, av. %lubyte/packet, "
104353369ac9SAndre Oppermann 					       "dropping connection\n",
104453369ac9SAndre Oppermann #ifdef INET6
104553369ac9SAndre Oppermann 						isipv6 ?
104653369ac9SAndre Oppermann 						ip6_sprintf(&inp->inp_inc.inc6_faddr) :
104753369ac9SAndre Oppermann #endif
104853369ac9SAndre Oppermann 						inet_ntoa(inp->inp_inc.inc_faddr),
104953369ac9SAndre Oppermann 						inp->inp_inc.inc_fport,
105053369ac9SAndre Oppermann 						tp->rcv_byps / tp->rcv_pps);
105153369ac9SAndre Oppermann 					tp = tcp_drop(tp, ECONNRESET);
105253369ac9SAndre Oppermann 					tcpstat.tcps_minmssdrops++;
105353369ac9SAndre Oppermann 					goto drop;
105453369ac9SAndre Oppermann 				}
105553369ac9SAndre Oppermann 			}
105653369ac9SAndre Oppermann 		} else {
105753369ac9SAndre Oppermann 			tp->rcv_second = ticks + hz;
105853369ac9SAndre Oppermann 			tp->rcv_pps = 1;
105953369ac9SAndre Oppermann 			tp->rcv_byps = tlen + off;
106053369ac9SAndre Oppermann 		}
106153369ac9SAndre Oppermann 	}
106253369ac9SAndre Oppermann 
106353369ac9SAndre Oppermann 	/*
1064df8bae1dSRodney W. Grimes 	 * Segment received on connection.
1065df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
1066df8bae1dSRodney W. Grimes 	 */
10679b8b58e0SJonathan Lemon 	tp->t_rcvtime = ticks;
10687ff19458SPaul Traina 	if (TCPS_HAVEESTABLISHED(tp->t_state))
10699b8b58e0SJonathan Lemon 		callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp);
1070df8bae1dSRodney W. Grimes 
1071df8bae1dSRodney W. Grimes 	/*
107297d8d152SAndre Oppermann 	 * Process options only when we get SYN/ACK back. The SYN case
107397d8d152SAndre Oppermann 	 * for incoming connections is handled in tcp_syncache.
107497d8d152SAndre Oppermann 	 * XXX this is traditional behavior, may need to be cleaned up.
1075df8bae1dSRodney W. Grimes 	 */
10766d90faf3SPaul Saab 	tcp_dooptions(tp, &to, optp, optlen, thflags & TH_SYN, th);
1077be2ac88cSJonathan Lemon 	if (thflags & TH_SYN) {
1078be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_SCALE) {
1079be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_SCALE;
1080be2ac88cSJonathan Lemon 			tp->requested_s_scale = to.to_requested_s_scale;
1081be2ac88cSJonathan Lemon 		}
1082be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_TS) {
1083be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_TSTMP;
1084be2ac88cSJonathan Lemon 			tp->ts_recent = to.to_tsval;
1085be2ac88cSJonathan Lemon 			tp->ts_recent_age = ticks;
1086be2ac88cSJonathan Lemon 		}
1087be2ac88cSJonathan Lemon 		if (to.to_flags & (TOF_CC|TOF_CCNEW))
1088be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_CC;
1089be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_MSS)
1090be2ac88cSJonathan Lemon 			tcp_mss(tp, to.to_mss);
10916d90faf3SPaul Saab 		if (tp->sack_enable) {
10926d90faf3SPaul Saab 			if (!(to.to_flags & TOF_SACK))
10936d90faf3SPaul Saab 				tp->sack_enable = 0;
10946d90faf3SPaul Saab 			else
10956d90faf3SPaul Saab 				tp->t_flags |= TF_SACK_PERMIT;
10966d90faf3SPaul Saab 		}
10976d90faf3SPaul Saab 
10986d90faf3SPaul Saab 	}
10996d90faf3SPaul Saab 
11006d90faf3SPaul Saab 	if (tp->sack_enable) {
11016d90faf3SPaul Saab 		/* Delete stale (cumulatively acked) SACK holes */
11026d90faf3SPaul Saab 		tcp_del_sackholes(tp, th);
1103652178a1SPaul Saab 		tp->rcv_laststart = th->th_seq; /* last recv'd segment*/
11046d90faf3SPaul Saab 		tp->rcv_lastend = th->th_seq + tlen;
1105be2ac88cSJonathan Lemon 	}
1106df8bae1dSRodney W. Grimes 
1107df8bae1dSRodney W. Grimes 	/*
1108df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
1109df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
1110df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
1111df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
1112df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
1113df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
1114df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
1115df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
1116df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
1117df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
1118df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
1119df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
1120a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
1121a0292f23SGarrett Wollman 	 * Since we check for TCPS_ESTABLISHED above, it can only
1122a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
1123df8bae1dSRodney W. Grimes 	 */
1124df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
1125fb59c426SYoshinobu Inoue 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1126a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
1127be2ac88cSJonathan Lemon 	    ((to.to_flags & TOF_TS) == 0 ||
1128a0292f23SGarrett Wollman 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) &&
1129a0292f23SGarrett Wollman 	    /*
1130a0292f23SGarrett Wollman 	     * Using the CC option is compulsory if once started:
1131a0292f23SGarrett Wollman 	     *   the segment is OK if no T/TCP was negotiated or
1132a0292f23SGarrett Wollman 	     *   if the segment has a CC option equal to CCrecv
1133a0292f23SGarrett Wollman 	     */
1134a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) ||
1135be2ac88cSJonathan Lemon 	     ((to.to_flags & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) &&
1136fb59c426SYoshinobu Inoue 	    th->th_seq == tp->rcv_nxt &&
1137df8bae1dSRodney W. Grimes 	    tiwin && tiwin == tp->snd_wnd &&
1138df8bae1dSRodney W. Grimes 	    tp->snd_nxt == tp->snd_max) {
1139df8bae1dSRodney W. Grimes 
1140df8bae1dSRodney W. Grimes 		/*
1141df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
1142df8bae1dSRodney W. Grimes 		 * record the timestamp.
1143a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
1144a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1145df8bae1dSRodney W. Grimes 		 */
1146be2ac88cSJonathan Lemon 		if ((to.to_flags & TOF_TS) != 0 &&
1147fb59c426SYoshinobu Inoue 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
11489b8b58e0SJonathan Lemon 			tp->ts_recent_age = ticks;
1149a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
1150df8bae1dSRodney W. Grimes 		}
1151df8bae1dSRodney W. Grimes 
1152fb59c426SYoshinobu Inoue 		if (tlen == 0) {
1153fb59c426SYoshinobu Inoue 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
1154fb59c426SYoshinobu Inoue 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
1155233e8c18SGarrett Wollman 			    tp->snd_cwnd >= tp->snd_wnd &&
11566d90faf3SPaul Saab 			    ((!tcp_do_newreno && !tp->sack_enable &&
1157cb942153SJeffrey Hsu 			      tp->t_dupacks < tcprexmtthresh) ||
11586d90faf3SPaul Saab 			     ((tcp_do_newreno || tp->sack_enable) &&
11596d90faf3SPaul Saab 			      !IN_FASTRECOVERY(tp)))) {
1160f76fcf6dSJeffrey Hsu 				KASSERT(headlocked, ("headlocked"));
1161f76fcf6dSJeffrey Hsu 				INP_INFO_WUNLOCK(&tcbinfo);
1162df8bae1dSRodney W. Grimes 				/*
1163df8bae1dSRodney W. Grimes 				 * this is a pure ack for outstanding data.
1164df8bae1dSRodney W. Grimes 				 */
1165df8bae1dSRodney W. Grimes 				++tcpstat.tcps_predack;
11669b8b58e0SJonathan Lemon 				/*
11679b8b58e0SJonathan Lemon 				 * "bad retransmit" recovery
11689b8b58e0SJonathan Lemon 				 */
11699b8b58e0SJonathan Lemon 				if (tp->t_rxtshift == 1 &&
11709b8b58e0SJonathan Lemon 				    ticks < tp->t_badrxtwin) {
1171cb942153SJeffrey Hsu 					++tcpstat.tcps_sndrexmitbad;
11729b8b58e0SJonathan Lemon 					tp->snd_cwnd = tp->snd_cwnd_prev;
11739b8b58e0SJonathan Lemon 					tp->snd_ssthresh =
11749b8b58e0SJonathan Lemon 					    tp->snd_ssthresh_prev;
11759d11646dSJeffrey Hsu 					tp->snd_recover = tp->snd_recover_prev;
11769d11646dSJeffrey Hsu 					if (tp->t_flags & TF_WASFRECOVERY)
11779d11646dSJeffrey Hsu 					    ENTER_FASTRECOVERY(tp);
11789b8b58e0SJonathan Lemon 					tp->snd_nxt = tp->snd_max;
11799b8b58e0SJonathan Lemon 					tp->t_badrxtwin = 0;
11809b8b58e0SJonathan Lemon 				}
1181fa55172bSMatthew Dillon 
1182fa55172bSMatthew Dillon 				/*
1183fa55172bSMatthew Dillon 				 * Recalculate the transmit timer / rtt.
1184fa55172bSMatthew Dillon 				 *
1185fa55172bSMatthew Dillon 				 * Some boxes send broken timestamp replies
1186fa55172bSMatthew Dillon 				 * during the SYN+ACK phase, ignore
1187fa55172bSMatthew Dillon 				 * timestamps of 0 or we could calculate a
1188fa55172bSMatthew Dillon 				 * huge RTT and blow up the retransmit timer.
1189fa55172bSMatthew Dillon 				 */
1190fa55172bSMatthew Dillon 				if ((to.to_flags & TOF_TS) != 0 &&
1191fa55172bSMatthew Dillon 				    to.to_tsecr) {
1192a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
11939b8b58e0SJonathan Lemon 					    ticks - to.to_tsecr + 1);
1194fa55172bSMatthew Dillon 				} else if (tp->t_rtttime &&
1195fa55172bSMatthew Dillon 					    SEQ_GT(th->th_ack, tp->t_rtseq)) {
1196c068736aSJeffrey Hsu 					tcp_xmit_timer(tp,
1197c068736aSJeffrey Hsu 							ticks - tp->t_rtttime);
1198fa55172bSMatthew Dillon 				}
11991fcc99b5SMatthew Dillon 				tcp_xmit_bandwidth_limit(tp, th->th_ack);
1200fb59c426SYoshinobu Inoue 				acked = th->th_ack - tp->snd_una;
1201df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvackpack++;
1202df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvackbyte += acked;
1203df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
12049d11646dSJeffrey Hsu 				if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
12059d11646dSJeffrey Hsu 				    SEQ_LEQ(th->th_ack, tp->snd_recover))
12069d11646dSJeffrey Hsu 					tp->snd_recover = th->th_ack - 1;
12079d11646dSJeffrey Hsu 				tp->snd_una = th->th_ack;
12081645d090SJeff Roberson 				/*
12091645d090SJeff Roberson 				 * pull snd_wl2 up to prevent seq wrap relative
12101645d090SJeff Roberson 				 * to th_ack.
12111645d090SJeff Roberson 				 */
1212cb942153SJeffrey Hsu 				tp->snd_wl2 = th->th_ack;
1213b5addd85SJeffrey Hsu 				tp->t_dupacks = 0;
1214df8bae1dSRodney W. Grimes 				m_freem(m);
1215fb59c426SYoshinobu Inoue 				ND6_HINT(tp); /* some progress has been done */
1216df8bae1dSRodney W. Grimes 
1217df8bae1dSRodney W. Grimes 				/*
1218df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
1219df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
1220df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
1221df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
1222df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
1223df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
1224df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
12253c653157SHartmut Brandt 
12263c653157SHartmut Brandt #ifdef TCPDEBUG
12273c653157SHartmut Brandt 				if (so->so_options & SO_DEBUG)
12283c653157SHartmut Brandt 					tcp_trace(TA_INPUT, ostate, tp,
12293c653157SHartmut Brandt 					    (void *)tcp_saveipgen,
12303c653157SHartmut Brandt 					    &tcp_savetcp, 0);
12313c653157SHartmut Brandt #endif
1232df8bae1dSRodney W. Grimes 				 */
1233df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
12349b8b58e0SJonathan Lemon 					callout_stop(tp->tt_rexmt);
12359b8b58e0SJonathan Lemon 				else if (!callout_active(tp->tt_persist))
12369b8b58e0SJonathan Lemon 					callout_reset(tp->tt_rexmt,
12379b8b58e0SJonathan Lemon 						      tp->t_rxtcur,
12389b8b58e0SJonathan Lemon 						      tcp_timer_rexmt, tp);
1239df8bae1dSRodney W. Grimes 
1240df8bae1dSRodney W. Grimes 				sowwakeup(so);
1241df8bae1dSRodney W. Grimes 				if (so->so_snd.sb_cc)
1242df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
1243a14c749fSJonathan Lemon 				goto check_delack;
1244df8bae1dSRodney W. Grimes 			}
1245fb59c426SYoshinobu Inoue 		} else if (th->th_ack == tp->snd_una &&
1246fb59c426SYoshinobu Inoue 		    LIST_EMPTY(&tp->t_segq) &&
1247fb59c426SYoshinobu Inoue 		    tlen <= sbspace(&so->so_rcv)) {
1248f76fcf6dSJeffrey Hsu 			KASSERT(headlocked, ("headlocked"));
1249f76fcf6dSJeffrey Hsu 			INP_INFO_WUNLOCK(&tcbinfo);
1250df8bae1dSRodney W. Grimes 			/*
1251df8bae1dSRodney W. Grimes 			 * this is a pure, in-sequence data packet
1252df8bae1dSRodney W. Grimes 			 * with nothing on the reassembly queue and
1253df8bae1dSRodney W. Grimes 			 * we have enough buffer space to take it.
1254df8bae1dSRodney W. Grimes 			 */
12556d90faf3SPaul Saab 			/* Clean receiver SACK report if present */
12566d90faf3SPaul Saab 			if (tp->sack_enable && tp->rcv_numsacks)
12576d90faf3SPaul Saab 				tcp_clean_sackreport(tp);
1258df8bae1dSRodney W. Grimes 			++tcpstat.tcps_preddat;
1259fb59c426SYoshinobu Inoue 			tp->rcv_nxt += tlen;
12601645d090SJeff Roberson 			/*
12611645d090SJeff Roberson 			 * Pull snd_wl1 up to prevent seq wrap relative to
12621645d090SJeff Roberson 			 * th_seq.
12631645d090SJeff Roberson 			 */
12641645d090SJeff Roberson 			tp->snd_wl1 = th->th_seq;
12651645d090SJeff Roberson 			/*
12661645d090SJeff Roberson 			 * Pull rcv_up up to prevent seq wrap relative to
12671645d090SJeff Roberson 			 * rcv_nxt.
12681645d090SJeff Roberson 			 */
12691645d090SJeff Roberson 			tp->rcv_up = tp->rcv_nxt;
1270df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpack++;
1271fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbyte += tlen;
1272fb59c426SYoshinobu Inoue 			ND6_HINT(tp);	/* some progress has been done */
1273df8bae1dSRodney W. Grimes 			/*
12743c653157SHartmut Brandt #ifdef TCPDEBUG
12753c653157SHartmut Brandt 			if (so->so_options & SO_DEBUG)
12763c653157SHartmut Brandt 				tcp_trace(TA_INPUT, ostate, tp,
12773c653157SHartmut Brandt 				    (void *)tcp_saveipgen, &tcp_savetcp, 0);
12783c653157SHartmut Brandt #endif
1279755c1f07SAndras Olah 			 * Add data to socket buffer.
1280df8bae1dSRodney W. Grimes 			 */
12811e4d7da7SRobert Watson 			/* Unlocked read. */
12821e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
1283c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1284c1c36a2cSMike Silbersack 				m_freem(m);
1285c1c36a2cSMike Silbersack 			} else {
1286fb59c426SYoshinobu Inoue 				m_adj(m, drop_hdrlen);	/* delayed header drop */
12871e4d7da7SRobert Watson 				sbappendstream_locked(&so->so_rcv, m);
1288c1c36a2cSMike Silbersack 			}
12891e4d7da7SRobert Watson 			sorwakeup_locked(so);
1290d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp)) {
12913bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
1292f498eeeeSDavid Greenman 			} else {
1293e612a582SDavid Greenman 				tp->t_flags |= TF_ACKNOW;
1294e612a582SDavid Greenman 				tcp_output(tp);
1295e612a582SDavid Greenman 			}
1296a14c749fSJonathan Lemon 			goto check_delack;
1297df8bae1dSRodney W. Grimes 		}
1298df8bae1dSRodney W. Grimes 	}
1299df8bae1dSRodney W. Grimes 
1300df8bae1dSRodney W. Grimes 	/*
1301df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
1302df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
1303df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
1304df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
1305df8bae1dSRodney W. Grimes 	 */
1306df8bae1dSRodney W. Grimes 	{ int win;
1307df8bae1dSRodney W. Grimes 
1308df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
1309df8bae1dSRodney W. Grimes 	if (win < 0)
1310df8bae1dSRodney W. Grimes 		win = 0;
131166e39adcSJohn Polstra 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1312df8bae1dSRodney W. Grimes 	}
1313df8bae1dSRodney W. Grimes 
1314df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1315df8bae1dSRodney W. Grimes 
1316df8bae1dSRodney W. Grimes 	/*
1317764d8cefSBill Fenner 	 * If the state is SYN_RECEIVED:
1318764d8cefSBill Fenner 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
1319764d8cefSBill Fenner 	 */
1320764d8cefSBill Fenner 	case TCPS_SYN_RECEIVED:
1321fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1322fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1323a57815efSBosko Milekic 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1324a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
1325a57815efSBosko Milekic 				goto dropwithreset;
1326a57815efSBosko Milekic 		}
1327764d8cefSBill Fenner 		break;
1328764d8cefSBill Fenner 
1329764d8cefSBill Fenner 	/*
1330df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
1331df8bae1dSRodney W. Grimes 	 *	if seg contains an ACK, but not for our SYN, drop the input.
1332df8bae1dSRodney W. Grimes 	 *	if seg contains a RST, then drop the connection.
1333df8bae1dSRodney W. Grimes 	 *	if seg does not contain SYN, then drop it.
1334df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
1335df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
1336df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
1337df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1338df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
1339df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
1340df8bae1dSRodney W. Grimes 	 */
1341df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
134297d8d152SAndre Oppermann 		if (tcp_do_rfc1644)
134397d8d152SAndre Oppermann 			tcp_hc_gettao(&inp->inp_inc, &tao);
1344a0292f23SGarrett Wollman 
1345fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1346fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->iss) ||
1347fb59c426SYoshinobu Inoue 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1348a0292f23SGarrett Wollman 			/*
1349a0292f23SGarrett Wollman 			 * If we have a cached CCsent for the remote host,
1350a0292f23SGarrett Wollman 			 * hence we haven't just crashed and restarted,
1351a0292f23SGarrett Wollman 			 * do not send a RST.  This may be a retransmission
1352a0292f23SGarrett Wollman 			 * from the other side after our earlier ACK was lost.
1353a0292f23SGarrett Wollman 			 * Our new SYN, when it arrives, will serve as the
1354a0292f23SGarrett Wollman 			 * needed ACK.
1355a0292f23SGarrett Wollman 			 */
135697d8d152SAndre Oppermann 			if (tao.tao_ccsent != 0)
1357a0292f23SGarrett Wollman 				goto drop;
1358a57815efSBosko Milekic 			else {
1359a57815efSBosko Milekic 				rstreason = BANDLIM_UNLIMITED;
1360a0292f23SGarrett Wollman 				goto dropwithreset;
1361a0292f23SGarrett Wollman 			}
1362a57815efSBosko Milekic 		}
1363fb59c426SYoshinobu Inoue 		if (thflags & TH_RST) {
1364fb59c426SYoshinobu Inoue 			if (thflags & TH_ACK)
1365df8bae1dSRodney W. Grimes 				tp = tcp_drop(tp, ECONNREFUSED);
1366df8bae1dSRodney W. Grimes 			goto drop;
1367df8bae1dSRodney W. Grimes 		}
1368fb59c426SYoshinobu Inoue 		if ((thflags & TH_SYN) == 0)
1369df8bae1dSRodney W. Grimes 			goto drop;
1370fb59c426SYoshinobu Inoue 		tp->snd_wnd = th->th_win;	/* initial send window */
1371a0292f23SGarrett Wollman 		tp->cc_recv = to.to_cc;		/* foreign CC */
1372a0292f23SGarrett Wollman 
1373fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
1374df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
1375fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1376a0292f23SGarrett Wollman 			/*
1377a0292f23SGarrett Wollman 			 * Our SYN was acked.  If segment contains CC.ECHO
1378a0292f23SGarrett Wollman 			 * option, check it to make sure this segment really
1379a0292f23SGarrett Wollman 			 * matches our SYN.  If not, just drop it as old
1380a0292f23SGarrett Wollman 			 * duplicate, but send an RST if we're still playing
1381026650e5SBill Fenner 			 * by the old rules.  If no CC.ECHO option, make sure
1382026650e5SBill Fenner 			 * we don't get fooled into using T/TCP.
1383a0292f23SGarrett Wollman 			 */
1384be2ac88cSJonathan Lemon 			if (to.to_flags & TOF_CCECHO) {
1385dfd5dee1SPeter Wemm 				if (tp->cc_send != to.to_ccecho) {
138697d8d152SAndre Oppermann 					if (tao.tao_ccsent != 0)
1387a0292f23SGarrett Wollman 						goto drop;
1388a57815efSBosko Milekic 					else {
1389a57815efSBosko Milekic 						rstreason = BANDLIM_UNLIMITED;
1390a0292f23SGarrett Wollman 						goto dropwithreset;
1391dfd5dee1SPeter Wemm 					}
1392a57815efSBosko Milekic 				}
1393026650e5SBill Fenner 			} else
1394026650e5SBill Fenner 				tp->t_flags &= ~TF_RCVD_CC;
1395845799c1SAndras Olah 			tcpstat.tcps_connects++;
1396845799c1SAndras Olah 			soisconnected(so);
1397c488362eSRobert Watson #ifdef MAC
1398310e7cebSRobert Watson 			SOCK_LOCK(so);
1399c488362eSRobert Watson 			mac_set_socket_peer_from_mbuf(m, so);
1400310e7cebSRobert Watson 			SOCK_UNLOCK(so);
1401c488362eSRobert Watson #endif
1402845799c1SAndras Olah 			/* Do window scaling on this connection? */
1403845799c1SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1404845799c1SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1405845799c1SAndras Olah 				tp->snd_scale = tp->requested_s_scale;
1406845799c1SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
1407845799c1SAndras Olah 			}
1408a0292f23SGarrett Wollman 			/* Segment is acceptable, update cache if undefined. */
140997d8d152SAndre Oppermann 			if (tao.tao_ccsent == 0 && tcp_do_rfc1644)
141097d8d152SAndre Oppermann 				tcp_hc_updatetao(&inp->inp_inc, TCP_HC_TAO_CCSENT, to.to_ccecho, 0);
1411a0292f23SGarrett Wollman 
1412a0292f23SGarrett Wollman 			tp->rcv_adv += tp->rcv_wnd;
1413a0292f23SGarrett Wollman 			tp->snd_una++;		/* SYN is acked */
1414a0292f23SGarrett Wollman 			/*
1415a0292f23SGarrett Wollman 			 * If there's data, delay ACK; if there's also a FIN
1416a0292f23SGarrett Wollman 			 * ACKNOW will be turned on later.
1417a0292f23SGarrett Wollman 			 */
1418d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp) && tlen != 0)
14199b8b58e0SJonathan Lemon 				callout_reset(tp->tt_delack, tcp_delacktime,
14209b8b58e0SJonathan Lemon 				    tcp_timer_delack, tp);
1421a0292f23SGarrett Wollman 			else
1422a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
1423a0292f23SGarrett Wollman 			/*
1424a0292f23SGarrett Wollman 			 * Received <SYN,ACK> in SYN_SENT[*] state.
1425a0292f23SGarrett Wollman 			 * Transitions:
1426a0292f23SGarrett Wollman 			 *	SYN_SENT  --> ESTABLISHED
1427a0292f23SGarrett Wollman 			 *	SYN_SENT* --> FIN_WAIT_1
1428a0292f23SGarrett Wollman 			 */
14299b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
1430a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDFIN) {
1431a0292f23SGarrett Wollman 				tp->t_state = TCPS_FIN_WAIT_1;
1432a0292f23SGarrett Wollman 				tp->t_flags &= ~TF_NEEDFIN;
1433fb59c426SYoshinobu Inoue 				thflags &= ~TH_SYN;
14347ff19458SPaul Traina 			} else {
1435a0292f23SGarrett Wollman 				tp->t_state = TCPS_ESTABLISHED;
14369b8b58e0SJonathan Lemon 				callout_reset(tp->tt_keep, tcp_keepidle,
14379b8b58e0SJonathan Lemon 					      tcp_timer_keep, tp);
14387ff19458SPaul Traina 			}
1439a0292f23SGarrett Wollman 		} else {
1440a0292f23SGarrett Wollman 			/*
1441c068736aSJeffrey Hsu 			 * Received initial SYN in SYN-SENT[*] state =>
1442c068736aSJeffrey Hsu 			 * simultaneous open.  If segment contains CC option
1443c068736aSJeffrey Hsu 			 * and there is a cached CC, apply TAO test.
1444c068736aSJeffrey Hsu 			 * If it succeeds, connection is * half-synchronized.
1445c068736aSJeffrey Hsu 			 * Otherwise, do 3-way handshake:
1446a0292f23SGarrett Wollman 			 *        SYN-SENT -> SYN-RECEIVED
1447a0292f23SGarrett Wollman 			 *        SYN-SENT* -> SYN-RECEIVED*
1448a0292f23SGarrett Wollman 			 * If there was no CC option, clear cached CC value.
1449a0292f23SGarrett Wollman 			 */
1450a0292f23SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
14519b8b58e0SJonathan Lemon 			callout_stop(tp->tt_rexmt);
1452be2ac88cSJonathan Lemon 			if (to.to_flags & TOF_CC) {
145397d8d152SAndre Oppermann 				if (tao.tao_cc != 0 &&
145497d8d152SAndre Oppermann 				    CC_GT(to.to_cc, tao.tao_cc)) {
1455a0292f23SGarrett Wollman 					/*
1456a0292f23SGarrett Wollman 					 * update cache and make transition:
1457a0292f23SGarrett Wollman 					 *        SYN-SENT -> ESTABLISHED*
1458a0292f23SGarrett Wollman 					 *        SYN-SENT* -> FIN-WAIT-1*
1459a0292f23SGarrett Wollman 					 */
146097d8d152SAndre Oppermann 					tao.tao_cc = to.to_cc;
146197d8d152SAndre Oppermann 					tcp_hc_updatetao(&inp->inp_inc,
146297d8d152SAndre Oppermann 						TCP_HC_TAO_CC, to.to_cc, 0);
14639b8b58e0SJonathan Lemon 					tp->t_starttime = ticks;
1464a0292f23SGarrett Wollman 					if (tp->t_flags & TF_NEEDFIN) {
1465a0292f23SGarrett Wollman 						tp->t_state = TCPS_FIN_WAIT_1;
1466a0292f23SGarrett Wollman 						tp->t_flags &= ~TF_NEEDFIN;
14677ff19458SPaul Traina 					} else {
1468a0292f23SGarrett Wollman 						tp->t_state = TCPS_ESTABLISHED;
14699b8b58e0SJonathan Lemon 						callout_reset(tp->tt_keep,
14709b8b58e0SJonathan Lemon 							      tcp_keepidle,
14719b8b58e0SJonathan Lemon 							      tcp_timer_keep,
14729b8b58e0SJonathan Lemon 							      tp);
14737ff19458SPaul Traina 					}
1474a0292f23SGarrett Wollman 					tp->t_flags |= TF_NEEDSYN;
1475df8bae1dSRodney W. Grimes 				} else
1476df8bae1dSRodney W. Grimes 					tp->t_state = TCPS_SYN_RECEIVED;
1477a0292f23SGarrett Wollman 			} else {
147897d8d152SAndre Oppermann 				if (tcp_do_rfc1644) {
1479a0292f23SGarrett Wollman 					/* CC.NEW or no option => invalidate cache */
148097d8d152SAndre Oppermann 					tao.tao_cc = 0;
148197d8d152SAndre Oppermann 					tcp_hc_updatetao(&inp->inp_inc,
148297d8d152SAndre Oppermann 						TCP_HC_TAO_CC, to.to_cc, 0);
148397d8d152SAndre Oppermann 				}
1484a0292f23SGarrett Wollman 				tp->t_state = TCPS_SYN_RECEIVED;
1485a0292f23SGarrett Wollman 			}
1486a0292f23SGarrett Wollman 		}
1487df8bae1dSRodney W. Grimes 
1488df8bae1dSRodney W. Grimes trimthenstep6:
14897cfc6904SRobert Watson 		KASSERT(headlocked,
14907cfc6904SRobert Watson 		    ("tcp_input(): trimthenstep6 head is not locked"));
14917cfc6904SRobert Watson 		INP_LOCK_ASSERT(inp);
14927cfc6904SRobert Watson 
1493df8bae1dSRodney W. Grimes 		/*
1494fb59c426SYoshinobu Inoue 		 * Advance th->th_seq to correspond to first data byte.
1495df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
1496df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
1497df8bae1dSRodney W. Grimes 		 */
1498fb59c426SYoshinobu Inoue 		th->th_seq++;
1499fb59c426SYoshinobu Inoue 		if (tlen > tp->rcv_wnd) {
1500fb59c426SYoshinobu Inoue 			todrop = tlen - tp->rcv_wnd;
1501df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
1502fb59c426SYoshinobu Inoue 			tlen = tp->rcv_wnd;
1503fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
1504df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpackafterwin++;
1505df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyteafterwin += todrop;
1506df8bae1dSRodney W. Grimes 		}
1507fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
1508fb59c426SYoshinobu Inoue 		tp->rcv_up = th->th_seq;
1509a0292f23SGarrett Wollman 		/*
1510a0292f23SGarrett Wollman 		 * Client side of transaction: already sent SYN and data.
1511a0292f23SGarrett Wollman 		 * If the remote host used T/TCP to validate the SYN,
1512a0292f23SGarrett Wollman 		 * our data will be ACK'd; if so, enter normal data segment
1513a0292f23SGarrett Wollman 		 * processing in the middle of step 5, ack processing.
1514a0292f23SGarrett Wollman 		 * Otherwise, goto step 6.
1515a0292f23SGarrett Wollman 		 */
1516fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK)
1517a0292f23SGarrett Wollman 			goto process_ACK;
1518c068736aSJeffrey Hsu 
1519df8bae1dSRodney W. Grimes 		goto step6;
1520c068736aSJeffrey Hsu 
1521a0292f23SGarrett Wollman 	/*
1522a0292f23SGarrett Wollman 	 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
1523a0292f23SGarrett Wollman 	 *	if segment contains a SYN and CC [not CC.NEW] option:
1524a0292f23SGarrett Wollman 	 *              if state == TIME_WAIT and connection duration > MSL,
1525a0292f23SGarrett Wollman 	 *                  drop packet and send RST;
1526a0292f23SGarrett Wollman 	 *
1527a0292f23SGarrett Wollman 	 *		if SEG.CC > CCrecv then is new SYN, and can implicitly
1528a0292f23SGarrett Wollman 	 *		    ack the FIN (and data) in retransmission queue.
1529a0292f23SGarrett Wollman 	 *                  Complete close and delete TCPCB.  Then reprocess
1530a0292f23SGarrett Wollman 	 *                  segment, hoping to find new TCPCB in LISTEN state;
1531a0292f23SGarrett Wollman 	 *
1532a0292f23SGarrett Wollman 	 *		else must be old SYN; drop it.
1533a0292f23SGarrett Wollman 	 *      else do normal processing.
1534a0292f23SGarrett Wollman 	 */
1535a0292f23SGarrett Wollman 	case TCPS_LAST_ACK:
1536a0292f23SGarrett Wollman 	case TCPS_CLOSING:
1537a0292f23SGarrett Wollman 	case TCPS_TIME_WAIT:
1538340c35deSJonathan Lemon 		KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
1539fb59c426SYoshinobu Inoue 		if ((thflags & TH_SYN) &&
1540be2ac88cSJonathan Lemon 		    (to.to_flags & TOF_CC) && tp->cc_recv != 0) {
1541a0292f23SGarrett Wollman 			if (tp->t_state == TCPS_TIME_WAIT &&
1542a57815efSBosko Milekic 					(ticks - tp->t_starttime) > tcp_msl) {
1543a57815efSBosko Milekic 				rstreason = BANDLIM_UNLIMITED;
1544a0292f23SGarrett Wollman 				goto dropwithreset;
1545a57815efSBosko Milekic 			}
1546a0292f23SGarrett Wollman 			if (CC_GT(to.to_cc, tp->cc_recv)) {
1547a0292f23SGarrett Wollman 				tp = tcp_close(tp);
1548a0292f23SGarrett Wollman 				goto findpcb;
1549a0292f23SGarrett Wollman 			}
1550a0292f23SGarrett Wollman 			else
1551a0292f23SGarrett Wollman 				goto drop;
1552a0292f23SGarrett Wollman 		}
1553a0292f23SGarrett Wollman 		break;  /* continue normal processing */
1554df8bae1dSRodney W. Grimes 	}
1555df8bae1dSRodney W. Grimes 
1556df8bae1dSRodney W. Grimes 	/*
1557df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
155880ab7c0eSGarrett Wollman 	 * First check the RST flag and sequence number since reset segments
155980ab7c0eSGarrett Wollman 	 * are exempt from the timestamp and connection count tests.  This
156080ab7c0eSGarrett Wollman 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
156180ab7c0eSGarrett Wollman 	 * below which allowed reset segments in half the sequence space
156280ab7c0eSGarrett Wollman 	 * to fall though and be processed (which gives forged reset
156380ab7c0eSGarrett Wollman 	 * segments with a random sequence number a 50 percent chance of
156480ab7c0eSGarrett Wollman 	 * killing a connection).
156580ab7c0eSGarrett Wollman 	 * Then check timestamp, if present.
1566a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
1567df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
1568df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
1569df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
1570df8bae1dSRodney W. Grimes 	 *
157180ab7c0eSGarrett Wollman 	 *
157280ab7c0eSGarrett Wollman 	 * If the RST bit is set, check the sequence number to see
157380ab7c0eSGarrett Wollman 	 * if this is a valid reset segment.
157480ab7c0eSGarrett Wollman 	 * RFC 793 page 37:
157580ab7c0eSGarrett Wollman 	 *   In all states except SYN-SENT, all reset (RST) segments
157680ab7c0eSGarrett Wollman 	 *   are validated by checking their SEQ-fields.  A reset is
157780ab7c0eSGarrett Wollman 	 *   valid if its sequence number is in the window.
157880ab7c0eSGarrett Wollman 	 * Note: this does not take into account delayed ACKs, so
157980ab7c0eSGarrett Wollman 	 *   we should test against last_ack_sent instead of rcv_nxt.
15801a244a61SJonathan Lemon 	 *   The sequence number in the reset segment is normally an
15811a244a61SJonathan Lemon 	 *   echo of our outgoing acknowlegement numbers, but some hosts
15821a244a61SJonathan Lemon 	 *   send a reset with the sequence number at the rightmost edge
15831a244a61SJonathan Lemon 	 *   of our receive window, and we have to handle this case.
158480dd2a81SMike Silbersack 	 * Note 2: Paul Watson's paper "Slipping in the Window" has shown
158580dd2a81SMike Silbersack 	 *   that brute force RST attacks are possible.  To combat this,
158680dd2a81SMike Silbersack 	 *   we use a much stricter check while in the ESTABLISHED state,
158780dd2a81SMike Silbersack 	 *   only accepting RSTs where the sequence number is equal to
158880dd2a81SMike Silbersack 	 *   last_ack_sent.  In all other states (the states in which a
158980dd2a81SMike Silbersack 	 *   RST is more likely), the more permissive check is used.
159080ab7c0eSGarrett Wollman 	 * If we have multiple segments in flight, the intial reset
159180ab7c0eSGarrett Wollman 	 * segment sequence numbers will be to the left of last_ack_sent,
159280ab7c0eSGarrett Wollman 	 * but they will eventually catch up.
159380ab7c0eSGarrett Wollman 	 * In any case, it never made sense to trim reset segments to
159480ab7c0eSGarrett Wollman 	 * fit the receive window since RFC 1122 says:
159580ab7c0eSGarrett Wollman 	 *   4.2.2.12  RST Segment: RFC-793 Section 3.4
159680ab7c0eSGarrett Wollman 	 *
159780ab7c0eSGarrett Wollman 	 *    A TCP SHOULD allow a received RST segment to include data.
159880ab7c0eSGarrett Wollman 	 *
159980ab7c0eSGarrett Wollman 	 *    DISCUSSION
160080ab7c0eSGarrett Wollman 	 *         It has been suggested that a RST segment could contain
160180ab7c0eSGarrett Wollman 	 *         ASCII text that encoded and explained the cause of the
160280ab7c0eSGarrett Wollman 	 *         RST.  No standard has yet been established for such
160380ab7c0eSGarrett Wollman 	 *         data.
160480ab7c0eSGarrett Wollman 	 *
160580ab7c0eSGarrett Wollman 	 * If the reset segment passes the sequence number test examine
160680ab7c0eSGarrett Wollman 	 * the state:
160780ab7c0eSGarrett Wollman 	 *    SYN_RECEIVED STATE:
160880ab7c0eSGarrett Wollman 	 *	If passive open, return to LISTEN state.
160980ab7c0eSGarrett Wollman 	 *	If active open, inform user that connection was refused.
1610745bab7fSDima Dorfman 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES:
161180ab7c0eSGarrett Wollman 	 *	Inform user that connection was reset, and close tcb.
1612e9bd3a37SJonathan M. Bresler 	 *    CLOSING, LAST_ACK STATES:
161380ab7c0eSGarrett Wollman 	 *	Close the tcb.
1614e9bd3a37SJonathan M. Bresler 	 *    TIME_WAIT STATE:
161580ab7c0eSGarrett Wollman 	 *	Drop the segment - see Stevens, vol. 2, p. 964 and
161680ab7c0eSGarrett Wollman 	 *      RFC 1337.
161780ab7c0eSGarrett Wollman 	 */
1618fb59c426SYoshinobu Inoue 	if (thflags & TH_RST) {
1619fb59c426SYoshinobu Inoue 		if (SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
1620fb59c426SYoshinobu Inoue 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
162180ab7c0eSGarrett Wollman 			switch (tp->t_state) {
162280ab7c0eSGarrett Wollman 
162380ab7c0eSGarrett Wollman 			case TCPS_SYN_RECEIVED:
162480ab7c0eSGarrett Wollman 				so->so_error = ECONNREFUSED;
162580ab7c0eSGarrett Wollman 				goto close;
162680ab7c0eSGarrett Wollman 
162780ab7c0eSGarrett Wollman 			case TCPS_ESTABLISHED:
162880dd2a81SMike Silbersack 				if (tp->last_ack_sent != th->th_seq) {
162980dd2a81SMike Silbersack 					tcpstat.tcps_badrst++;
163080dd2a81SMike Silbersack 					goto drop;
163180dd2a81SMike Silbersack 				}
163280ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_1:
163380ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_2:
163480ab7c0eSGarrett Wollman 			case TCPS_CLOSE_WAIT:
163580ab7c0eSGarrett Wollman 				so->so_error = ECONNRESET;
163680ab7c0eSGarrett Wollman 			close:
163780ab7c0eSGarrett Wollman 				tp->t_state = TCPS_CLOSED;
163880ab7c0eSGarrett Wollman 				tcpstat.tcps_drops++;
163980ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
164080ab7c0eSGarrett Wollman 				break;
164180ab7c0eSGarrett Wollman 
164280ab7c0eSGarrett Wollman 			case TCPS_CLOSING:
164380ab7c0eSGarrett Wollman 			case TCPS_LAST_ACK:
164480ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
164580ab7c0eSGarrett Wollman 				break;
164680ab7c0eSGarrett Wollman 
164780ab7c0eSGarrett Wollman 			case TCPS_TIME_WAIT:
1648340c35deSJonathan Lemon 				KASSERT(tp->t_state != TCPS_TIME_WAIT,
1649340c35deSJonathan Lemon 				    ("timewait"));
165080ab7c0eSGarrett Wollman 				break;
165180ab7c0eSGarrett Wollman 			}
165280ab7c0eSGarrett Wollman 		}
165380ab7c0eSGarrett Wollman 		goto drop;
165480ab7c0eSGarrett Wollman 	}
165580ab7c0eSGarrett Wollman 
165680ab7c0eSGarrett Wollman 	/*
1657df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
1658df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
1659df8bae1dSRodney W. Grimes 	 */
1660be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
166180ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
1662df8bae1dSRodney W. Grimes 
1663df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
16649b8b58e0SJonathan Lemon 		if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) {
1665df8bae1dSRodney W. Grimes 			/*
1666df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
1667df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
1668df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
1669df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
1670df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
1671df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
1672df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
1673df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
1674df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
1675df8bae1dSRodney W. Grimes 			 */
1676df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
1677df8bae1dSRodney W. Grimes 		} else {
1678df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvduppack++;
1679fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvdupbyte += tlen;
1680df8bae1dSRodney W. Grimes 			tcpstat.tcps_pawsdrop++;
168107fd333dSMatthew Dillon 			if (tlen)
1682df8bae1dSRodney W. Grimes 				goto dropafterack;
16831ab4789dSMatthew Dillon 			goto drop;
16841ab4789dSMatthew Dillon 		}
1685df8bae1dSRodney W. Grimes 	}
1686df8bae1dSRodney W. Grimes 
1687a0292f23SGarrett Wollman 	/*
1688a0292f23SGarrett Wollman 	 * T/TCP mechanism
1689a0292f23SGarrett Wollman 	 *   If T/TCP was negotiated and the segment doesn't have CC,
1690dc733423SDag-Erling Smørgrav 	 *   or if its CC is wrong then drop the segment.
1691a0292f23SGarrett Wollman 	 *   RST segments do not have to comply with this.
1692a0292f23SGarrett Wollman 	 */
1693a0292f23SGarrett Wollman 	if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) &&
1694be2ac88cSJonathan Lemon 	    ((to.to_flags & TOF_CC) == 0 || tp->cc_recv != to.to_cc))
1695a0292f23SGarrett Wollman 		goto dropafterack;
1696a0292f23SGarrett Wollman 
169780ab7c0eSGarrett Wollman 	/*
169880ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
169980ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
170080ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
170180ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
170280ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
170380ab7c0eSGarrett Wollman 	 */
1704a57815efSBosko Milekic 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
1705a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
1706a57815efSBosko Milekic 		goto dropwithreset;
1707a57815efSBosko Milekic 	}
170880ab7c0eSGarrett Wollman 
1709fb59c426SYoshinobu Inoue 	todrop = tp->rcv_nxt - th->th_seq;
1710df8bae1dSRodney W. Grimes 	if (todrop > 0) {
1711fb59c426SYoshinobu Inoue 		if (thflags & TH_SYN) {
1712fb59c426SYoshinobu Inoue 			thflags &= ~TH_SYN;
1713fb59c426SYoshinobu Inoue 			th->th_seq++;
1714fb59c426SYoshinobu Inoue 			if (th->th_urp > 1)
1715fb59c426SYoshinobu Inoue 				th->th_urp--;
1716df8bae1dSRodney W. Grimes 			else
1717fb59c426SYoshinobu Inoue 				thflags &= ~TH_URG;
1718df8bae1dSRodney W. Grimes 			todrop--;
1719df8bae1dSRodney W. Grimes 		}
1720df8bae1dSRodney W. Grimes 		/*
1721dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
1722df8bae1dSRodney W. Grimes 		 */
1723fb59c426SYoshinobu Inoue 		if (todrop > tlen
1724fb59c426SYoshinobu Inoue 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
1725dac20301SGarrett Wollman 			/*
1726dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
1727dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
1728dac20301SGarrett Wollman 			 * of sequence; drop it.
1729dac20301SGarrett Wollman 			 */
1730fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
1731dac20301SGarrett Wollman 
1732df8bae1dSRodney W. Grimes 			/*
1733dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
1734dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
1735df8bae1dSRodney W. Grimes 			 */
1736dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
1737fb59c426SYoshinobu Inoue 			todrop = tlen;
1738dac20301SGarrett Wollman 			tcpstat.tcps_rcvduppack++;
1739dac20301SGarrett Wollman 			tcpstat.tcps_rcvdupbyte += todrop;
1740df8bae1dSRodney W. Grimes 		} else {
1741df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpartduppack++;
1742df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpartdupbyte += todrop;
1743df8bae1dSRodney W. Grimes 		}
1744fb59c426SYoshinobu Inoue 		drop_hdrlen += todrop;	/* drop from the top afterwards */
1745fb59c426SYoshinobu Inoue 		th->th_seq += todrop;
1746fb59c426SYoshinobu Inoue 		tlen -= todrop;
1747fb59c426SYoshinobu Inoue 		if (th->th_urp > todrop)
1748fb59c426SYoshinobu Inoue 			th->th_urp -= todrop;
1749df8bae1dSRodney W. Grimes 		else {
1750fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;
1751fb59c426SYoshinobu Inoue 			th->th_urp = 0;
1752df8bae1dSRodney W. Grimes 		}
1753df8bae1dSRodney W. Grimes 	}
1754df8bae1dSRodney W. Grimes 
1755df8bae1dSRodney W. Grimes 	/*
1756df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
1757df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
1758df8bae1dSRodney W. Grimes 	 */
1759df8bae1dSRodney W. Grimes 	if ((so->so_state & SS_NOFDREF) &&
1760fb59c426SYoshinobu Inoue 	    tp->t_state > TCPS_CLOSE_WAIT && tlen) {
1761df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1762df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvafterclose++;
1763a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
1764df8bae1dSRodney W. Grimes 		goto dropwithreset;
1765df8bae1dSRodney W. Grimes 	}
1766df8bae1dSRodney W. Grimes 
1767df8bae1dSRodney W. Grimes 	/*
1768df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
1769df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
1770df8bae1dSRodney W. Grimes 	 */
1771fb59c426SYoshinobu Inoue 	todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd);
1772df8bae1dSRodney W. Grimes 	if (todrop > 0) {
1773df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvpackafterwin++;
1774fb59c426SYoshinobu Inoue 		if (todrop >= tlen) {
1775fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbyteafterwin += tlen;
1776df8bae1dSRodney W. Grimes 			/*
1777df8bae1dSRodney W. Grimes 			 * If a new connection request is received
1778df8bae1dSRodney W. Grimes 			 * while in TIME_WAIT, drop the old connection
1779df8bae1dSRodney W. Grimes 			 * and start over if the sequence numbers
1780df8bae1dSRodney W. Grimes 			 * are above the previous ones.
1781df8bae1dSRodney W. Grimes 			 */
1782340c35deSJonathan Lemon 			KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
1783fb59c426SYoshinobu Inoue 			if (thflags & TH_SYN &&
1784df8bae1dSRodney W. Grimes 			    tp->t_state == TCPS_TIME_WAIT &&
1785fb59c426SYoshinobu Inoue 			    SEQ_GT(th->th_seq, tp->rcv_nxt)) {
1786df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
1787df8bae1dSRodney W. Grimes 				goto findpcb;
1788df8bae1dSRodney W. Grimes 			}
1789df8bae1dSRodney W. Grimes 			/*
1790df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
1791df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
1792df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
1793df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
1794df8bae1dSRodney W. Grimes 			 * and ack.
1795df8bae1dSRodney W. Grimes 			 */
1796fb59c426SYoshinobu Inoue 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
1797df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
1798df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvwinprobe++;
1799df8bae1dSRodney W. Grimes 			} else
1800df8bae1dSRodney W. Grimes 				goto dropafterack;
1801df8bae1dSRodney W. Grimes 		} else
1802df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyteafterwin += todrop;
1803df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
1804fb59c426SYoshinobu Inoue 		tlen -= todrop;
1805fb59c426SYoshinobu Inoue 		thflags &= ~(TH_PUSH|TH_FIN);
1806df8bae1dSRodney W. Grimes 	}
1807df8bae1dSRodney W. Grimes 
1808df8bae1dSRodney W. Grimes 	/*
1809df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
1810df8bae1dSRodney W. Grimes 	 * record its timestamp.
1811a0292f23SGarrett Wollman 	 * NOTE that the test is modified according to the latest
1812a0292f23SGarrett Wollman 	 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1813df8bae1dSRodney W. Grimes 	 */
1814be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 &&
1815fb59c426SYoshinobu Inoue 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
18169b8b58e0SJonathan Lemon 		tp->ts_recent_age = ticks;
1817a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
1818df8bae1dSRodney W. Grimes 	}
1819df8bae1dSRodney W. Grimes 
1820df8bae1dSRodney W. Grimes 	/*
1821df8bae1dSRodney W. Grimes 	 * If a SYN is in the window, then this is an
1822df8bae1dSRodney W. Grimes 	 * error and we send an RST and drop the connection.
1823df8bae1dSRodney W. Grimes 	 */
1824fb59c426SYoshinobu Inoue 	if (thflags & TH_SYN) {
1825df8bae1dSRodney W. Grimes 		tp = tcp_drop(tp, ECONNRESET);
1826a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
1827122aad88SAndre Oppermann 		goto drop;
1828df8bae1dSRodney W. Grimes 	}
1829df8bae1dSRodney W. Grimes 
1830a0292f23SGarrett Wollman 	/*
1831a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
1832a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
1833a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
1834a0292f23SGarrett Wollman 	 */
1835fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) == 0) {
1836a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
1837a0292f23SGarrett Wollman 		    (tp->t_flags & TF_NEEDSYN))
1838a0292f23SGarrett Wollman 			goto step6;
1839a0292f23SGarrett Wollman 		else
1840a0292f23SGarrett Wollman 			goto drop;
1841a0292f23SGarrett Wollman 	}
1842df8bae1dSRodney W. Grimes 
1843df8bae1dSRodney W. Grimes 	/*
1844df8bae1dSRodney W. Grimes 	 * Ack processing.
1845df8bae1dSRodney W. Grimes 	 */
1846df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1847df8bae1dSRodney W. Grimes 
1848df8bae1dSRodney W. Grimes 	/*
1849764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
1850764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
1851764d8cefSBill Fenner 	 * The ACK was checked above.
1852df8bae1dSRodney W. Grimes 	 */
1853df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1854a0292f23SGarrett Wollman 
1855df8bae1dSRodney W. Grimes 		tcpstat.tcps_connects++;
1856df8bae1dSRodney W. Grimes 		soisconnected(so);
1857df8bae1dSRodney W. Grimes 		/* Do window scaling? */
1858df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1859df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1860df8bae1dSRodney W. Grimes 			tp->snd_scale = tp->requested_s_scale;
1861df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
1862df8bae1dSRodney W. Grimes 		}
1863a0292f23SGarrett Wollman 		/*
1864a0292f23SGarrett Wollman 		 * Upon successful completion of 3-way handshake,
186597d8d152SAndre Oppermann 		 * update cache.CC, pass any queued data to the user,
186697d8d152SAndre Oppermann 		 * and advance state appropriately.
1867a0292f23SGarrett Wollman 		 */
186897d8d152SAndre Oppermann 		if (tcp_do_rfc1644) {
186997d8d152SAndre Oppermann 			tao.tao_cc = tp->cc_recv;
187097d8d152SAndre Oppermann 			tcp_hc_updatetao(&inp->inp_inc, TCP_HC_TAO_CC,
187197d8d152SAndre Oppermann 					 tp->cc_recv, 0);
187297d8d152SAndre Oppermann 		}
1873a0292f23SGarrett Wollman 		/*
1874a0292f23SGarrett Wollman 		 * Make transitions:
1875a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
1876a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
1877a0292f23SGarrett Wollman 		 */
18789b8b58e0SJonathan Lemon 		tp->t_starttime = ticks;
1879a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDFIN) {
1880a0292f23SGarrett Wollman 			tp->t_state = TCPS_FIN_WAIT_1;
1881a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDFIN;
18827ff19458SPaul Traina 		} else {
1883a0292f23SGarrett Wollman 			tp->t_state = TCPS_ESTABLISHED;
18849b8b58e0SJonathan Lemon 			callout_reset(tp->tt_keep, tcp_keepidle,
18859b8b58e0SJonathan Lemon 				      tcp_timer_keep, tp);
18867ff19458SPaul Traina 		}
1887a0292f23SGarrett Wollman 		/*
1888a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
1889a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
1890a0292f23SGarrett Wollman 		 */
1891fb59c426SYoshinobu Inoue 		if (tlen == 0 && (thflags & TH_FIN) == 0)
1892fb59c426SYoshinobu Inoue 			(void) tcp_reass(tp, (struct tcphdr *)0, 0,
1893a0292f23SGarrett Wollman 			    (struct mbuf *)0);
1894fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
189593b0017fSPhilippe Charnier 		/* FALLTHROUGH */
1896df8bae1dSRodney W. Grimes 
1897df8bae1dSRodney W. Grimes 	/*
1898df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1899df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
1900fb59c426SYoshinobu Inoue 	 *	tp->snd_una < th->th_ack <= tp->snd_max
1901fb59c426SYoshinobu Inoue 	 * then advance tp->snd_una to th->th_ack and drop
1902df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
1903df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
1904df8bae1dSRodney W. Grimes 	 */
1905df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1906df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
1907df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
1908df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1909df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
1910df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
1911df8bae1dSRodney W. Grimes 	case TCPS_TIME_WAIT:
1912340c35deSJonathan Lemon 		KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
1913fb59c426SYoshinobu Inoue 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
1914fb59c426SYoshinobu Inoue 			if (tlen == 0 && tiwin == tp->snd_wnd) {
1915df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvdupack++;
1916df8bae1dSRodney W. Grimes 				/*
1917df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
1918df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
1919df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
1920df8bae1dSRodney W. Grimes 				 * change), the ack is the biggest we've
1921df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
1922df8bae1dSRodney W. Grimes 				 * threshhold of them, assume a packet
1923df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
1924df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
1925df8bae1dSRodney W. Grimes 				 * window so we send only this one
1926df8bae1dSRodney W. Grimes 				 * packet.
1927df8bae1dSRodney W. Grimes 				 *
1928df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
1929df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
1930df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
1931df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
1932df8bae1dSRodney W. Grimes 				 * the new ssthresh).
1933df8bae1dSRodney W. Grimes 				 *
1934df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
1935df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
1936df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
1937df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
1938df8bae1dSRodney W. Grimes 				 * network.
1939df8bae1dSRodney W. Grimes 				 */
19409b8b58e0SJonathan Lemon 				if (!callout_active(tp->tt_rexmt) ||
1941fb59c426SYoshinobu Inoue 				    th->th_ack != tp->snd_una)
1942df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
1943cb942153SJeffrey Hsu 				else if (++tp->t_dupacks > tcprexmtthresh ||
19446d90faf3SPaul Saab 					 ((tcp_do_newreno || tp->sack_enable) &&
19459d11646dSJeffrey Hsu 					  IN_FASTRECOVERY(tp))) {
194646f58482SJonathan Lemon 					tp->snd_cwnd += tp->t_maxseg;
194746f58482SJonathan Lemon 					(void) tcp_output(tp);
194846f58482SJonathan Lemon 					goto drop;
1949cb942153SJeffrey Hsu 				} else if (tp->t_dupacks == tcprexmtthresh) {
1950cb942153SJeffrey Hsu 					tcp_seq onxt = tp->snd_nxt;
1951cb942153SJeffrey Hsu 					u_int win;
1952a0445c2eSJayanth Vijayaraghavan 
1953a0445c2eSJayanth Vijayaraghavan 					/*
1954a0445c2eSJayanth Vijayaraghavan 					 * If we're doing sack, check to
1955a0445c2eSJayanth Vijayaraghavan 					 * see if we're already in sack
1956a0445c2eSJayanth Vijayaraghavan 					 * recovery. If we're not doing sack,
1957a0445c2eSJayanth Vijayaraghavan 					 * check to see if we're in newreno
1958a0445c2eSJayanth Vijayaraghavan 					 * recovery.
1959a0445c2eSJayanth Vijayaraghavan 					 */
1960a0445c2eSJayanth Vijayaraghavan 					if (tp->sack_enable) {
1961a0445c2eSJayanth Vijayaraghavan 						if (IN_FASTRECOVERY(tp)) {
1962a0445c2eSJayanth Vijayaraghavan 							tp->t_dupacks = 0;
1963a0445c2eSJayanth Vijayaraghavan 							break;
1964a0445c2eSJayanth Vijayaraghavan 						}
1965a0445c2eSJayanth Vijayaraghavan 					} else if (tcp_do_newreno) {
1966a0445c2eSJayanth Vijayaraghavan 						if (SEQ_LEQ(th->th_ack,
19679d11646dSJeffrey Hsu 						    tp->snd_recover)) {
1968cb942153SJeffrey Hsu 							tp->t_dupacks = 0;
1969cb942153SJeffrey Hsu 							break;
197046f58482SJonathan Lemon 						}
1971a0445c2eSJayanth Vijayaraghavan 					}
1972cb942153SJeffrey Hsu 					win = min(tp->snd_wnd, tp->snd_cwnd) /
1973cb942153SJeffrey Hsu 					    2 / tp->t_maxseg;
1974df8bae1dSRodney W. Grimes 					if (win < 2)
1975df8bae1dSRodney W. Grimes 						win = 2;
1976df8bae1dSRodney W. Grimes 					tp->snd_ssthresh = win * tp->t_maxseg;
19779d11646dSJeffrey Hsu 					ENTER_FASTRECOVERY(tp);
197846f58482SJonathan Lemon 					tp->snd_recover = tp->snd_max;
19799b8b58e0SJonathan Lemon 					callout_stop(tp->tt_rexmt);
19809b8b58e0SJonathan Lemon 					tp->t_rtttime = 0;
19816d90faf3SPaul Saab 					if (tp->sack_enable) {
19826d90faf3SPaul Saab 						tcpstat.tcps_sack_recovery_episode++;
19836d90faf3SPaul Saab 						tp->snd_cwnd =
19846d90faf3SPaul Saab 						    tp->t_maxseg *
19856d90faf3SPaul Saab 						    tp->t_dupacks;
19866d90faf3SPaul Saab 						(void) tcp_output(tp);
1987a0445c2eSJayanth Vijayaraghavan 						tp->snd_cwnd +=
19886d90faf3SPaul Saab 						    tp->snd_ssthresh;
19896d90faf3SPaul Saab 						goto drop;
19906d90faf3SPaul Saab 					}
19916d90faf3SPaul Saab 
1992fb59c426SYoshinobu Inoue 					tp->snd_nxt = th->th_ack;
1993df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->t_maxseg;
1994df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
199548d2549cSJeffrey Hsu 					KASSERT(tp->snd_limited <= 2,
199648d2549cSJeffrey Hsu 					    ("tp->snd_limited too big"));
1997df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
199848d2549cSJeffrey Hsu 					     tp->t_maxseg *
199948d2549cSJeffrey Hsu 					     (tp->t_dupacks - tp->snd_limited);
2000df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
2001df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
2002df8bae1dSRodney W. Grimes 					goto drop;
2003582a954bSJeffrey Hsu 				} else if (tcp_do_rfc3042) {
2004582a954bSJeffrey Hsu 					u_long oldcwnd = tp->snd_cwnd;
200548d2549cSJeffrey Hsu 					tcp_seq oldsndmax = tp->snd_max;
200648d2549cSJeffrey Hsu 					u_int sent;
200789c02376SJeffrey Hsu 
2008582a954bSJeffrey Hsu 					KASSERT(tp->t_dupacks == 1 ||
2009582a954bSJeffrey Hsu 					    tp->t_dupacks == 2,
2010582a954bSJeffrey Hsu 					    ("dupacks not 1 or 2"));
201161a36e3dSJeffrey Hsu 					if (tp->t_dupacks == 1)
201248d2549cSJeffrey Hsu 						tp->snd_limited = 0;
201361a36e3dSJeffrey Hsu 					tp->snd_cwnd =
201461a36e3dSJeffrey Hsu 					    (tp->snd_nxt - tp->snd_una) +
201561a36e3dSJeffrey Hsu 					    (tp->t_dupacks - tp->snd_limited) *
201661a36e3dSJeffrey Hsu 					    tp->t_maxseg;
2017582a954bSJeffrey Hsu 					(void) tcp_output(tp);
201848d2549cSJeffrey Hsu 					sent = tp->snd_max - oldsndmax;
201948d2549cSJeffrey Hsu 					if (sent > tp->t_maxseg) {
202089c02376SJeffrey Hsu 						KASSERT((tp->t_dupacks == 2 &&
202189c02376SJeffrey Hsu 						    tp->snd_limited == 0) ||
202289c02376SJeffrey Hsu 						   (sent == tp->t_maxseg + 1 &&
202389c02376SJeffrey Hsu 						    tp->t_flags & TF_SENTFIN),
202448d2549cSJeffrey Hsu 						    ("sent too much"));
202548d2549cSJeffrey Hsu 						tp->snd_limited = 2;
202648d2549cSJeffrey Hsu 					} else if (sent > 0)
202748d2549cSJeffrey Hsu 						++tp->snd_limited;
2028582a954bSJeffrey Hsu 					tp->snd_cwnd = oldcwnd;
2029582a954bSJeffrey Hsu 					goto drop;
2030df8bae1dSRodney W. Grimes 				}
2031df8bae1dSRodney W. Grimes 			} else
2032df8bae1dSRodney W. Grimes 				tp->t_dupacks = 0;
2033df8bae1dSRodney W. Grimes 			break;
2034df8bae1dSRodney W. Grimes 		}
2035cb942153SJeffrey Hsu 
2036cb942153SJeffrey Hsu 		KASSERT(SEQ_GT(th->th_ack, tp->snd_una), ("th_ack <= snd_una"));
2037cb942153SJeffrey Hsu 
2038df8bae1dSRodney W. Grimes 		/*
2039df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
2040df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
2041df8bae1dSRodney W. Grimes 		 */
20426d90faf3SPaul Saab 		if (tcp_do_newreno || tp->sack_enable) {
20439d11646dSJeffrey Hsu 			if (IN_FASTRECOVERY(tp)) {
2044cb942153SJeffrey Hsu 				if (SEQ_LT(th->th_ack, tp->snd_recover)) {
20456d90faf3SPaul Saab 					if (tp->sack_enable)
20466d90faf3SPaul Saab 						tcp_sack_partialack(tp, th);
20476d90faf3SPaul Saab 					else
2048c068736aSJeffrey Hsu 						tcp_newreno_partial_ack(tp, th);
2049c068736aSJeffrey Hsu 				} else {
2050c068736aSJeffrey Hsu 					/*
20516d90faf3SPaul Saab 					 * Out of fast recovery.
2052c068736aSJeffrey Hsu 					 * Window inflation should have left us
2053c068736aSJeffrey Hsu 					 * with approximately snd_ssthresh
2054c068736aSJeffrey Hsu 					 * outstanding data.
2055c068736aSJeffrey Hsu 					 * But in case we would be inclined to
2056c068736aSJeffrey Hsu 					 * send a burst, better to do it via
2057c068736aSJeffrey Hsu 					 * the slow start mechanism.
2058c068736aSJeffrey Hsu 					 */
2059c068736aSJeffrey Hsu 					if (SEQ_GT(th->th_ack +
2060c068736aSJeffrey Hsu 							tp->snd_ssthresh,
2061c068736aSJeffrey Hsu 						   tp->snd_max))
2062c068736aSJeffrey Hsu 						tp->snd_cwnd = tp->snd_max -
2063c068736aSJeffrey Hsu 								th->th_ack +
2064c068736aSJeffrey Hsu 								tp->t_maxseg;
2065c068736aSJeffrey Hsu 					else
2066c068736aSJeffrey Hsu 						tp->snd_cwnd = tp->snd_ssthresh;
2067c068736aSJeffrey Hsu 				}
2068c068736aSJeffrey Hsu 			}
2069c068736aSJeffrey Hsu 		} else {
2070233e8c18SGarrett Wollman 			if (tp->t_dupacks >= tcprexmtthresh &&
2071df8bae1dSRodney W. Grimes 			    tp->snd_cwnd > tp->snd_ssthresh)
2072df8bae1dSRodney W. Grimes 				tp->snd_cwnd = tp->snd_ssthresh;
207346f58482SJonathan Lemon 		}
2074cb942153SJeffrey Hsu 		tp->t_dupacks = 0;
2075fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
2076df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvacktoomuch++;
2077df8bae1dSRodney W. Grimes 			goto dropafterack;
2078df8bae1dSRodney W. Grimes 		}
2079a0292f23SGarrett Wollman 		/*
2080a0292f23SGarrett Wollman 		 * If we reach this point, ACK is not a duplicate,
2081a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
2082a0292f23SGarrett Wollman 		 */
2083a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
2084a0292f23SGarrett Wollman 			/*
2085a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
2086a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
208707e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
208807e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
208907e43e10SAndras Olah 			 * we can do window scaling.
2090a0292f23SGarrett Wollman 			 */
2091a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
2092a0292f23SGarrett Wollman 			tp->snd_una++;
209307e43e10SAndras Olah 			/* Do window scaling? */
209407e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
209507e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
209607e43e10SAndras Olah 				tp->snd_scale = tp->requested_s_scale;
209707e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
209807e43e10SAndras Olah 			}
2099a0292f23SGarrett Wollman 		}
2100a0292f23SGarrett Wollman 
2101a0292f23SGarrett Wollman process_ACK:
21027cfc6904SRobert Watson 		KASSERT(headlocked,
21037cfc6904SRobert Watson 		    ("tcp_input(): process_ACK head is not locked"));
21047cfc6904SRobert Watson 		INP_LOCK_ASSERT(inp);
21057cfc6904SRobert Watson 
2106fb59c426SYoshinobu Inoue 		acked = th->th_ack - tp->snd_una;
2107df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvackpack++;
2108df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvackbyte += acked;
2109df8bae1dSRodney W. Grimes 
2110df8bae1dSRodney W. Grimes 		/*
21119b8b58e0SJonathan Lemon 		 * If we just performed our first retransmit, and the ACK
21129b8b58e0SJonathan Lemon 		 * arrives within our recovery window, then it was a mistake
21139b8b58e0SJonathan Lemon 		 * to do the retransmit in the first place.  Recover our
21149b8b58e0SJonathan Lemon 		 * original cwnd and ssthresh, and proceed to transmit where
21159b8b58e0SJonathan Lemon 		 * we left off.
21169b8b58e0SJonathan Lemon 		 */
21179b8b58e0SJonathan Lemon 		if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) {
2118d65bf08aSMatthew Dillon 			++tcpstat.tcps_sndrexmitbad;
21199b8b58e0SJonathan Lemon 			tp->snd_cwnd = tp->snd_cwnd_prev;
21209b8b58e0SJonathan Lemon 			tp->snd_ssthresh = tp->snd_ssthresh_prev;
21219d11646dSJeffrey Hsu 			tp->snd_recover = tp->snd_recover_prev;
21229d11646dSJeffrey Hsu 			if (tp->t_flags & TF_WASFRECOVERY)
21239d11646dSJeffrey Hsu 				ENTER_FASTRECOVERY(tp);
21249b8b58e0SJonathan Lemon 			tp->snd_nxt = tp->snd_max;
21259b8b58e0SJonathan Lemon 			tp->t_badrxtwin = 0;	/* XXX probably not required */
21269b8b58e0SJonathan Lemon 		}
21279b8b58e0SJonathan Lemon 
21289b8b58e0SJonathan Lemon 		/*
2129df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
2130df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
2131df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
2132df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
2133df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
2134df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
2135df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
2136fa55172bSMatthew Dillon 		 *
2137fa55172bSMatthew Dillon 		 * Some boxes send broken timestamp replies
2138fa55172bSMatthew Dillon 		 * during the SYN+ACK phase, ignore
2139fa55172bSMatthew Dillon 		 * timestamps of 0 or we could calculate a
2140fa55172bSMatthew Dillon 		 * huge RTT and blow up the retransmit timer.
2141df8bae1dSRodney W. Grimes 		 */
2142fa55172bSMatthew Dillon 		if ((to.to_flags & TOF_TS) != 0 &&
2143fa55172bSMatthew Dillon 		    to.to_tsecr) {
21449b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - to.to_tsecr + 1);
2145fa55172bSMatthew Dillon 		} else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
21469b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
2147fa55172bSMatthew Dillon 		}
21481fcc99b5SMatthew Dillon 		tcp_xmit_bandwidth_limit(tp, th->th_ack);
2149df8bae1dSRodney W. Grimes 
2150df8bae1dSRodney W. Grimes 		/*
2151df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
2152df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
2153df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
2154df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
2155df8bae1dSRodney W. Grimes 		 */
2156fb59c426SYoshinobu Inoue 		if (th->th_ack == tp->snd_max) {
21579b8b58e0SJonathan Lemon 			callout_stop(tp->tt_rexmt);
2158df8bae1dSRodney W. Grimes 			needoutput = 1;
21599b8b58e0SJonathan Lemon 		} else if (!callout_active(tp->tt_persist))
21609b8b58e0SJonathan Lemon 			callout_reset(tp->tt_rexmt, tp->t_rxtcur,
21619b8b58e0SJonathan Lemon 				      tcp_timer_rexmt, tp);
2162a0292f23SGarrett Wollman 
2163a0292f23SGarrett Wollman 		/*
2164a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
2165a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
2166a0292f23SGarrett Wollman 		 */
2167a0292f23SGarrett Wollman 		if (acked == 0)
2168a0292f23SGarrett Wollman 			goto step6;
2169a0292f23SGarrett Wollman 
2170df8bae1dSRodney W. Grimes 		/*
2171df8bae1dSRodney W. Grimes 		 * When new data is acked, open the congestion window.
2172df8bae1dSRodney W. Grimes 		 * If the window gives us less than ssthresh packets
2173df8bae1dSRodney W. Grimes 		 * in flight, open exponentially (maxseg per packet).
2174df8bae1dSRodney W. Grimes 		 * Otherwise open linearly: maxseg per window
217510be5648SGarrett Wollman 		 * (maxseg^2 / cwnd per packet).
2176df8bae1dSRodney W. Grimes 		 */
21776d90faf3SPaul Saab 		if ((!tcp_do_newreno && !tp->sack_enable) ||
21786d90faf3SPaul Saab 		    !IN_FASTRECOVERY(tp)) {
2179df8bae1dSRodney W. Grimes 			register u_int cw = tp->snd_cwnd;
2180df8bae1dSRodney W. Grimes 			register u_int incr = tp->t_maxseg;
2181df8bae1dSRodney W. Grimes 			if (cw > tp->snd_ssthresh)
218210be5648SGarrett Wollman 				incr = incr * incr / cw;
2183df8bae1dSRodney W. Grimes 			tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale);
2184df8bae1dSRodney W. Grimes 		}
21855905999bSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
2186df8bae1dSRodney W. Grimes 		if (acked > so->so_snd.sb_cc) {
2187df8bae1dSRodney W. Grimes 			tp->snd_wnd -= so->so_snd.sb_cc;
21885905999bSRobert Watson 			sbdrop_locked(&so->so_snd, (int)so->so_snd.sb_cc);
2189df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
2190df8bae1dSRodney W. Grimes 		} else {
21915905999bSRobert Watson 			sbdrop_locked(&so->so_snd, acked);
2192df8bae1dSRodney W. Grimes 			tp->snd_wnd -= acked;
2193df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
2194df8bae1dSRodney W. Grimes 		}
21951e4d7da7SRobert Watson 		sowwakeup_locked(so);
2196cb942153SJeffrey Hsu 		/* detect una wraparound */
21976d90faf3SPaul Saab 		if ((tcp_do_newreno || tp->sack_enable) &&
21986d90faf3SPaul Saab 		    !IN_FASTRECOVERY(tp) &&
21999d11646dSJeffrey Hsu 		    SEQ_GT(tp->snd_una, tp->snd_recover) &&
22009d11646dSJeffrey Hsu 		    SEQ_LEQ(th->th_ack, tp->snd_recover))
22019d11646dSJeffrey Hsu 			tp->snd_recover = th->th_ack - 1;
22026d90faf3SPaul Saab 		if ((tcp_do_newreno || tp->sack_enable) &&
22036d90faf3SPaul Saab 		    IN_FASTRECOVERY(tp) &&
22049d11646dSJeffrey Hsu 		    SEQ_GEQ(th->th_ack, tp->snd_recover))
22059d11646dSJeffrey Hsu 			EXIT_FASTRECOVERY(tp);
2206fb59c426SYoshinobu Inoue 		tp->snd_una = th->th_ack;
22076d90faf3SPaul Saab 		if (tp->sack_enable) {
22086d90faf3SPaul Saab 			if (SEQ_GT(tp->snd_una, tp->snd_recover))
22096d90faf3SPaul Saab 				tp->snd_recover = tp->snd_una;
22106d90faf3SPaul Saab 		}
2211df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2212df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
2213df8bae1dSRodney W. Grimes 
2214df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2215df8bae1dSRodney W. Grimes 
2216df8bae1dSRodney W. Grimes 		/*
2217df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
2218df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
2219df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
2220df8bae1dSRodney W. Grimes 		 */
2221df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2222df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2223df8bae1dSRodney W. Grimes 				/*
2224df8bae1dSRodney W. Grimes 				 * If we can't receive any more
2225df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
2226df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
2227df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
2228df8bae1dSRodney W. Grimes 				 * we'll hang forever.
2229df8bae1dSRodney W. Grimes 				 */
2230340c35deSJonathan Lemon 		/* XXXjl
2231340c35deSJonathan Lemon 		 * we should release the tp also, and use a
2232340c35deSJonathan Lemon 		 * compressed state.
2233340c35deSJonathan Lemon 		 */
2234c0b99ffaSRobert Watson 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
223503e49181SSeigo Tanimura 					soisdisconnected(so);
22369b8b58e0SJonathan Lemon 					callout_reset(tp->tt_2msl, tcp_maxidle,
22379b8b58e0SJonathan Lemon 						      tcp_timer_2msl, tp);
22384cc20ab1SSeigo Tanimura 				}
2239df8bae1dSRodney W. Grimes 				tp->t_state = TCPS_FIN_WAIT_2;
2240df8bae1dSRodney W. Grimes 			}
2241df8bae1dSRodney W. Grimes 			break;
2242df8bae1dSRodney W. Grimes 
2243df8bae1dSRodney W. Grimes 		/*
2244df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
2245df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
2246df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
2247df8bae1dSRodney W. Grimes 		 * the segment.
2248df8bae1dSRodney W. Grimes 		 */
2249df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
2250df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2251340c35deSJonathan Lemon 				KASSERT(headlocked, ("headlocked"));
225211a20fb8SJeffrey Hsu 				tcp_twstart(tp);
2253340c35deSJonathan Lemon 				INP_INFO_WUNLOCK(&tcbinfo);
2254340c35deSJonathan Lemon 				m_freem(m);
2255340c35deSJonathan Lemon 				return;
2256df8bae1dSRodney W. Grimes 			}
2257df8bae1dSRodney W. Grimes 			break;
2258df8bae1dSRodney W. Grimes 
2259df8bae1dSRodney W. Grimes 		/*
2260df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
2261df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
2262df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
2263df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
2264df8bae1dSRodney W. Grimes 		 */
2265df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
2266df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2267df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
2268df8bae1dSRodney W. Grimes 				goto drop;
2269df8bae1dSRodney W. Grimes 			}
2270df8bae1dSRodney W. Grimes 			break;
2271df8bae1dSRodney W. Grimes 
2272df8bae1dSRodney W. Grimes 		/*
2273df8bae1dSRodney W. Grimes 		 * In TIME_WAIT state the only thing that should arrive
2274df8bae1dSRodney W. Grimes 		 * is a retransmission of the remote FIN.  Acknowledge
2275df8bae1dSRodney W. Grimes 		 * it and restart the finack timer.
2276df8bae1dSRodney W. Grimes 		 */
2277df8bae1dSRodney W. Grimes 		case TCPS_TIME_WAIT:
2278340c35deSJonathan Lemon 			KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
22799b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, 2 * tcp_msl,
22809b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
2281df8bae1dSRodney W. Grimes 			goto dropafterack;
2282df8bae1dSRodney W. Grimes 		}
2283df8bae1dSRodney W. Grimes 	}
2284df8bae1dSRodney W. Grimes 
2285df8bae1dSRodney W. Grimes step6:
22867cfc6904SRobert Watson 	KASSERT(headlocked, ("tcp_input(): step6 head is not locked"));
22877cfc6904SRobert Watson 	INP_LOCK_ASSERT(inp);
22887cfc6904SRobert Watson 
2289df8bae1dSRodney W. Grimes 	/*
2290df8bae1dSRodney W. Grimes 	 * Update window information.
2291df8bae1dSRodney W. Grimes 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2292df8bae1dSRodney W. Grimes 	 */
2293fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) &&
2294fb59c426SYoshinobu Inoue 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
2295fb59c426SYoshinobu Inoue 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
2296fb59c426SYoshinobu Inoue 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
2297df8bae1dSRodney W. Grimes 		/* keep track of pure window updates */
2298fb59c426SYoshinobu Inoue 		if (tlen == 0 &&
2299fb59c426SYoshinobu Inoue 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
2300df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvwinupd++;
2301df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
2302fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq;
2303fb59c426SYoshinobu Inoue 		tp->snd_wl2 = th->th_ack;
2304df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
2305df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
2306df8bae1dSRodney W. Grimes 		needoutput = 1;
2307df8bae1dSRodney W. Grimes 	}
2308df8bae1dSRodney W. Grimes 
2309df8bae1dSRodney W. Grimes 	/*
2310df8bae1dSRodney W. Grimes 	 * Process segments with URG.
2311df8bae1dSRodney W. Grimes 	 */
2312fb59c426SYoshinobu Inoue 	if ((thflags & TH_URG) && th->th_urp &&
2313df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2314df8bae1dSRodney W. Grimes 		/*
2315df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
2316df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
2317df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
2318df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
2319df8bae1dSRodney W. Grimes 		 */
2320fb59c426SYoshinobu Inoue 		if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
2321fb59c426SYoshinobu Inoue 			th->th_urp = 0;			/* XXX */
2322fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;		/* XXX */
2323df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
2324df8bae1dSRodney W. Grimes 		}
2325df8bae1dSRodney W. Grimes 		/*
2326df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
2327df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
2328df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2329df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
2330df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
2331df8bae1dSRodney W. Grimes 		 *
2332df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
2333df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
2334df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
2335df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
2336df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
2337df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
2338df8bae1dSRodney W. Grimes 		 */
2339fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
2340fb59c426SYoshinobu Inoue 			tp->rcv_up = th->th_seq + th->th_urp;
2341927c5ceaSRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
2342df8bae1dSRodney W. Grimes 			so->so_oobmark = so->so_rcv.sb_cc +
2343df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
2344927c5ceaSRobert Watson 			if (so->so_oobmark == 0)
2345c0b99ffaSRobert Watson 				so->so_rcv.sb_state |= SBS_RCVATMARK;
23467721f5d7SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);
2347df8bae1dSRodney W. Grimes 			sohasoutofband(so);
2348df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2349df8bae1dSRodney W. Grimes 		}
2350df8bae1dSRodney W. Grimes 		/*
2351df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
2352df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
2353df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
2354df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
2355df8bae1dSRodney W. Grimes 		 */
235630613f56SJeffrey Hsu 		if (th->th_urp <= (u_long)tlen &&
235730613f56SJeffrey Hsu 		    !(so->so_options & SO_OOBINLINE)) {
235830613f56SJeffrey Hsu 			/* hdr drop is delayed */
235930613f56SJeffrey Hsu 			tcp_pulloutofband(so, th, m, drop_hdrlen);
236030613f56SJeffrey Hsu 		}
2361c068736aSJeffrey Hsu 	} else {
2362df8bae1dSRodney W. Grimes 		/*
2363df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
2364df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
2365df8bae1dSRodney W. Grimes 		 * with the receive window.
2366df8bae1dSRodney W. Grimes 		 */
2367df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
2368df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
2369c068736aSJeffrey Hsu 	}
2370df8bae1dSRodney W. Grimes dodata:							/* XXX */
23717cfc6904SRobert Watson 	KASSERT(headlocked, ("tcp_input(): dodata head is not locked"));
23727cfc6904SRobert Watson 	INP_LOCK_ASSERT(inp);
23737cfc6904SRobert Watson 
2374df8bae1dSRodney W. Grimes 	/*
2375df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
2376df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
2377df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
2378df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
2379df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
2380df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
2381df8bae1dSRodney W. Grimes 	 */
2382fb59c426SYoshinobu Inoue 	if ((tlen || (thflags & TH_FIN)) &&
2383df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2384fb59c426SYoshinobu Inoue 		m_adj(m, drop_hdrlen);	/* delayed header drop */
2385e4b64281SJesper Skriver 		/*
2386c068736aSJeffrey Hsu 		 * Insert segment which includes th into TCP reassembly queue
2387c068736aSJeffrey Hsu 		 * with control block tp.  Set thflags to whether reassembly now
2388c068736aSJeffrey Hsu 		 * includes a segment with FIN.  This handles the common case
2389c068736aSJeffrey Hsu 		 * inline (segment is the next to be received on an established
2390c068736aSJeffrey Hsu 		 * connection, and the queue is empty), avoiding linkage into
2391c068736aSJeffrey Hsu 		 * and removal from the queue and repetition of various
2392c068736aSJeffrey Hsu 		 * conversions.
2393c068736aSJeffrey Hsu 		 * Set DELACK for segments received in order, but ack
2394c068736aSJeffrey Hsu 		 * immediately when segments are out of order (so
2395c068736aSJeffrey Hsu 		 * fast retransmit can work).
2396e4b64281SJesper Skriver 		 */
2397e4b64281SJesper Skriver 		if (th->th_seq == tp->rcv_nxt &&
2398e4b64281SJesper Skriver 		    LIST_EMPTY(&tp->t_segq) &&
2399e4b64281SJesper Skriver 		    TCPS_HAVEESTABLISHED(tp->t_state)) {
2400e4b64281SJesper Skriver 			if (DELAY_ACK(tp))
24013bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
2402e4b64281SJesper Skriver 			else
2403e4b64281SJesper Skriver 				tp->t_flags |= TF_ACKNOW;
2404e4b64281SJesper Skriver 			tp->rcv_nxt += tlen;
2405e4b64281SJesper Skriver 			thflags = th->th_flags & TH_FIN;
2406e4b64281SJesper Skriver 			tcpstat.tcps_rcvpack++;
2407e4b64281SJesper Skriver 			tcpstat.tcps_rcvbyte += tlen;
2408e4b64281SJesper Skriver 			ND6_HINT(tp);
24091e4d7da7SRobert Watson 			/* Unlocked read. */
24101e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
2411c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
2412c1c36a2cSMike Silbersack 				m_freem(m);
2413c1c36a2cSMike Silbersack 			else
24141e4d7da7SRobert Watson 				sbappendstream_locked(&so->so_rcv, m);
24151e4d7da7SRobert Watson 			sorwakeup_locked(so);
2416e4b64281SJesper Skriver 		} else {
2417e4b64281SJesper Skriver 			thflags = tcp_reass(tp, th, &tlen, m);
2418e4b64281SJesper Skriver 			tp->t_flags |= TF_ACKNOW;
2419e4b64281SJesper Skriver 		}
24206d90faf3SPaul Saab 			if (tp->sack_enable)
24216d90faf3SPaul Saab 				tcp_update_sack_list(tp);
2422df8bae1dSRodney W. Grimes 		/*
2423df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
2424df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
2425df8bae1dSRodney W. Grimes 		 * buffer size.
2426df8bae1dSRodney W. Grimes 		 */
2427df8bae1dSRodney W. Grimes 		len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
2428df8bae1dSRodney W. Grimes 	} else {
2429df8bae1dSRodney W. Grimes 		m_freem(m);
2430fb59c426SYoshinobu Inoue 		thflags &= ~TH_FIN;
2431df8bae1dSRodney W. Grimes 	}
2432df8bae1dSRodney W. Grimes 
2433df8bae1dSRodney W. Grimes 	/*
2434df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
2435df8bae1dSRodney W. Grimes 	 * that the connection is closing.
2436df8bae1dSRodney W. Grimes 	 */
2437fb59c426SYoshinobu Inoue 	if (thflags & TH_FIN) {
2438df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2439df8bae1dSRodney W. Grimes 			socantrcvmore(so);
2440a0292f23SGarrett Wollman 			/*
2441a0292f23SGarrett Wollman 			 * If connection is half-synchronized
2442d3eede9dSAndras Olah 			 * (ie NEEDSYN flag on) then delay ACK,
2443a0292f23SGarrett Wollman 			 * so it may be piggybacked when SYN is sent.
2444a0292f23SGarrett Wollman 			 * Otherwise, since we received a FIN then no
2445a0292f23SGarrett Wollman 			 * more input can be expected, send ACK now.
2446a0292f23SGarrett Wollman 			 */
24473bfd6421SJonathan Lemon 			if (tp->t_flags & TF_NEEDSYN)
24483bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
2449a0292f23SGarrett Wollman 			else
2450df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
2451df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
2452df8bae1dSRodney W. Grimes 		}
2453df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2454df8bae1dSRodney W. Grimes 
2455df8bae1dSRodney W. Grimes 		/*
2456df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
2457df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
2458df8bae1dSRodney W. Grimes 		 */
2459df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
24609b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
24619b8b58e0SJonathan Lemon 			/*FALLTHROUGH*/
2462df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
2463df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSE_WAIT;
2464df8bae1dSRodney W. Grimes 			break;
2465df8bae1dSRodney W. Grimes 
2466df8bae1dSRodney W. Grimes 		/*
2467df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2468df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
2469df8bae1dSRodney W. Grimes 		 */
2470df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2471df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSING;
2472df8bae1dSRodney W. Grimes 			break;
2473df8bae1dSRodney W. Grimes 
2474df8bae1dSRodney W. Grimes 		/*
2475df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2476df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
2477df8bae1dSRodney W. Grimes 		 * standard timers.
2478df8bae1dSRodney W. Grimes 		 */
2479df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
248011a20fb8SJeffrey Hsu 			KASSERT(headlocked == 1, ("headlocked should be 1"));
2481340c35deSJonathan Lemon 			tcp_twstart(tp);
248211a20fb8SJeffrey Hsu 			INP_INFO_WUNLOCK(&tcbinfo);
2483340c35deSJonathan Lemon 			return;
2484df8bae1dSRodney W. Grimes 
2485df8bae1dSRodney W. Grimes 		/*
2486df8bae1dSRodney W. Grimes 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
2487df8bae1dSRodney W. Grimes 		 */
2488df8bae1dSRodney W. Grimes 		case TCPS_TIME_WAIT:
2489340c35deSJonathan Lemon 			KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
24909b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, 2 * tcp_msl,
24919b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
2492df8bae1dSRodney W. Grimes 			break;
2493df8bae1dSRodney W. Grimes 		}
2494df8bae1dSRodney W. Grimes 	}
24957792ea27SJeffrey Hsu 	INP_INFO_WUNLOCK(&tcbinfo);
2496610ee2f9SDavid Greenman #ifdef TCPDEBUG
24974cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
2498fb59c426SYoshinobu Inoue 		tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
2499fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2500610ee2f9SDavid Greenman #endif
2501df8bae1dSRodney W. Grimes 
2502df8bae1dSRodney W. Grimes 	/*
2503df8bae1dSRodney W. Grimes 	 * Return any desired output.
2504df8bae1dSRodney W. Grimes 	 */
2505df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
2506df8bae1dSRodney W. Grimes 		(void) tcp_output(tp);
25077792ea27SJeffrey Hsu 
2508a14c749fSJonathan Lemon check_delack:
25097cfc6904SRobert Watson 	INP_LOCK_ASSERT(inp);
2510a14c749fSJonathan Lemon 	if (tp->t_flags & TF_DELACK) {
25113bfd6421SJonathan Lemon 		tp->t_flags &= ~TF_DELACK;
25123bfd6421SJonathan Lemon 		callout_reset(tp->tt_delack, tcp_delacktime,
25133bfd6421SJonathan Lemon 		    tcp_timer_delack, tp);
25143bfd6421SJonathan Lemon 	}
2515f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
2516df8bae1dSRodney W. Grimes 	return;
2517df8bae1dSRodney W. Grimes 
2518df8bae1dSRodney W. Grimes dropafterack:
2519df8bae1dSRodney W. Grimes 	/*
2520df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
2521df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
252280ab7c0eSGarrett Wollman 	 *
252380ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
252480ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
252580ab7c0eSGarrett Wollman 	 * RST have been dropped.
252680ab7c0eSGarrett Wollman 	 *
252780ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
252880ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
252980ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
253080ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
253180ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
253280ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
2533df8bae1dSRodney W. Grimes 	 */
2534fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
2535fb59c426SYoshinobu Inoue 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
2536a57815efSBosko Milekic 	     SEQ_GT(th->th_ack, tp->snd_max)) ) {
2537a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2538a57815efSBosko Milekic 		goto dropwithreset;
2539a57815efSBosko Milekic 	}
2540a0292f23SGarrett Wollman #ifdef TCPDEBUG
25414cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
2542fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2543fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2544a0292f23SGarrett Wollman #endif
254585e8b243SJeffrey Hsu 	KASSERT(headlocked, ("headlocked should be 1"));
2546f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&tcbinfo);
2547df8bae1dSRodney W. Grimes 	m_freem(m);
2548df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
2549df8bae1dSRodney W. Grimes 	(void) tcp_output(tp);
2550f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
2551df8bae1dSRodney W. Grimes 	return;
2552df8bae1dSRodney W. Grimes 
2553df8bae1dSRodney W. Grimes dropwithreset:
2554df8bae1dSRodney W. Grimes 	/*
2555df8bae1dSRodney W. Grimes 	 * Generate a RST, dropping incoming segment.
2556df8bae1dSRodney W. Grimes 	 * Make ACK acceptable to originator of segment.
2557df8bae1dSRodney W. Grimes 	 * Don't bother to respond if destination was broadcast/multicast.
2558df8bae1dSRodney W. Grimes 	 */
2559fb59c426SYoshinobu Inoue 	if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
2560df8bae1dSRodney W. Grimes 		goto drop;
2561fb59c426SYoshinobu Inoue 	if (isipv6) {
2562173c0f9fSWarner Losh 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
2563173c0f9fSWarner Losh 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
2564fb59c426SYoshinobu Inoue 			goto drop;
2565c068736aSJeffrey Hsu 	} else {
2566173c0f9fSWarner Losh 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
2567173c0f9fSWarner Losh 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
25682ca2159fSCrist J. Clark 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
25692ca2159fSCrist J. Clark 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
2570fb59c426SYoshinobu Inoue 			goto drop;
2571c068736aSJeffrey Hsu 	}
2572fb59c426SYoshinobu Inoue 	/* IPv6 anycast check is done at tcp6_input() */
2573a57815efSBosko Milekic 
2574a57815efSBosko Milekic 	/*
2575c59319bfSDag-Erling Smørgrav 	 * Perform bandwidth limiting.
2576a57815efSBosko Milekic 	 */
2577a57815efSBosko Milekic 	if (badport_bandlim(rstreason) < 0)
2578a57815efSBosko Milekic 		goto drop;
2579a57815efSBosko Milekic 
2580a0292f23SGarrett Wollman #ifdef TCPDEBUG
25814cc20ab1SSeigo Tanimura 	if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
2582fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2583fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2584a0292f23SGarrett Wollman #endif
25856fd22cafSJeffrey Hsu 
2586fb59c426SYoshinobu Inoue 	if (thflags & TH_ACK)
2587fb59c426SYoshinobu Inoue 		/* mtod() below is safe as long as hdr dropping is delayed */
2588fb59c426SYoshinobu Inoue 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack,
2589fb59c426SYoshinobu Inoue 			    TH_RST);
2590df8bae1dSRodney W. Grimes 	else {
2591fb59c426SYoshinobu Inoue 		if (thflags & TH_SYN)
2592fb59c426SYoshinobu Inoue 			tlen++;
2593fb59c426SYoshinobu Inoue 		/* mtod() below is safe as long as hdr dropping is delayed */
2594fb59c426SYoshinobu Inoue 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
2595fb59c426SYoshinobu Inoue 			    (tcp_seq)0, TH_RST|TH_ACK);
2596df8bae1dSRodney W. Grimes 	}
2597c29afad6SSam Leffler 
2598c29afad6SSam Leffler 	if (tp)
2599c29afad6SSam Leffler 		INP_UNLOCK(inp);
2600f76fcf6dSJeffrey Hsu 	if (headlocked)
2601f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&tcbinfo);
2602df8bae1dSRodney W. Grimes 	return;
2603df8bae1dSRodney W. Grimes 
2604df8bae1dSRodney W. Grimes drop:
2605df8bae1dSRodney W. Grimes 	/*
2606df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
2607df8bae1dSRodney W. Grimes 	 */
2608610ee2f9SDavid Greenman #ifdef TCPDEBUG
26094cc20ab1SSeigo Tanimura 	if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
2610fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2611fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2612a0292f23SGarrett Wollman #endif
2613f76fcf6dSJeffrey Hsu 	if (tp)
2614f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
2615df8bae1dSRodney W. Grimes 	m_freem(m);
2616f76fcf6dSJeffrey Hsu 	if (headlocked)
2617f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&tcbinfo);
2618df8bae1dSRodney W. Grimes 	return;
2619df8bae1dSRodney W. Grimes }
2620df8bae1dSRodney W. Grimes 
2621be2ac88cSJonathan Lemon /*
2622be2ac88cSJonathan Lemon  * Parse TCP options and place in tcpopt.
2623be2ac88cSJonathan Lemon  */
26240312fbe9SPoul-Henning Kamp static void
26256d90faf3SPaul Saab tcp_dooptions(tp, to, cp, cnt, is_syn, th)
26266d90faf3SPaul Saab 	struct tcpcb *tp;
2627be2ac88cSJonathan Lemon 	struct tcpopt *to;
2628df8bae1dSRodney W. Grimes 	u_char *cp;
2629df8bae1dSRodney W. Grimes 	int cnt;
2630152385d1SDavid E. O'Brien 	int is_syn;
26316d90faf3SPaul Saab 	struct tcphdr *th;
2632df8bae1dSRodney W. Grimes {
2633df8bae1dSRodney W. Grimes 	int opt, optlen;
2634df8bae1dSRodney W. Grimes 
2635be2ac88cSJonathan Lemon 	to->to_flags = 0;
2636df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
2637df8bae1dSRodney W. Grimes 		opt = cp[0];
2638df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
2639df8bae1dSRodney W. Grimes 			break;
2640df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
2641df8bae1dSRodney W. Grimes 			optlen = 1;
2642df8bae1dSRodney W. Grimes 		else {
2643b474779fSJun-ichiro itojun Hagino 			if (cnt < 2)
2644b474779fSJun-ichiro itojun Hagino 				break;
2645df8bae1dSRodney W. Grimes 			optlen = cp[1];
2646b474779fSJun-ichiro itojun Hagino 			if (optlen < 2 || optlen > cnt)
2647df8bae1dSRodney W. Grimes 				break;
2648df8bae1dSRodney W. Grimes 		}
2649df8bae1dSRodney W. Grimes 		switch (opt) {
2650df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
2651df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
2652df8bae1dSRodney W. Grimes 				continue;
2653be2ac88cSJonathan Lemon 			if (!is_syn)
2654df8bae1dSRodney W. Grimes 				continue;
2655be2ac88cSJonathan Lemon 			to->to_flags |= TOF_MSS;
2656be2ac88cSJonathan Lemon 			bcopy((char *)cp + 2,
2657be2ac88cSJonathan Lemon 			    (char *)&to->to_mss, sizeof(to->to_mss));
2658fd8e4ebcSMike Barcroft 			to->to_mss = ntohs(to->to_mss);
2659df8bae1dSRodney W. Grimes 			break;
2660df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
2661df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
2662df8bae1dSRodney W. Grimes 				continue;
2663be2ac88cSJonathan Lemon 			if (! is_syn)
2664df8bae1dSRodney W. Grimes 				continue;
2665be2ac88cSJonathan Lemon 			to->to_flags |= TOF_SCALE;
2666be2ac88cSJonathan Lemon 			to->to_requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
2667df8bae1dSRodney W. Grimes 			break;
2668df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
2669df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
2670df8bae1dSRodney W. Grimes 				continue;
2671be2ac88cSJonathan Lemon 			to->to_flags |= TOF_TS;
2672a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2673a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
2674fd8e4ebcSMike Barcroft 			to->to_tsval = ntohl(to->to_tsval);
2675a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
2676a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
2677fd8e4ebcSMike Barcroft 			to->to_tsecr = ntohl(to->to_tsecr);
2678df8bae1dSRodney W. Grimes 			break;
2679a0292f23SGarrett Wollman 		case TCPOPT_CC:
2680a0292f23SGarrett Wollman 			if (optlen != TCPOLEN_CC)
2681a0292f23SGarrett Wollman 				continue;
2682be2ac88cSJonathan Lemon 			to->to_flags |= TOF_CC;
2683a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2684a0292f23SGarrett Wollman 			    (char *)&to->to_cc, sizeof(to->to_cc));
2685fd8e4ebcSMike Barcroft 			to->to_cc = ntohl(to->to_cc);
2686a0292f23SGarrett Wollman 			break;
2687a0292f23SGarrett Wollman 		case TCPOPT_CCNEW:
2688a0292f23SGarrett Wollman 			if (optlen != TCPOLEN_CC)
2689a0292f23SGarrett Wollman 				continue;
2690be2ac88cSJonathan Lemon 			if (!is_syn)
2691a0292f23SGarrett Wollman 				continue;
2692be2ac88cSJonathan Lemon 			to->to_flags |= TOF_CCNEW;
2693a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2694a0292f23SGarrett Wollman 			    (char *)&to->to_cc, sizeof(to->to_cc));
2695fd8e4ebcSMike Barcroft 			to->to_cc = ntohl(to->to_cc);
2696a0292f23SGarrett Wollman 			break;
2697a0292f23SGarrett Wollman 		case TCPOPT_CCECHO:
2698a0292f23SGarrett Wollman 			if (optlen != TCPOLEN_CC)
2699a0292f23SGarrett Wollman 				continue;
2700be2ac88cSJonathan Lemon 			if (!is_syn)
2701a0292f23SGarrett Wollman 				continue;
2702be2ac88cSJonathan Lemon 			to->to_flags |= TOF_CCECHO;
2703a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2704a0292f23SGarrett Wollman 			    (char *)&to->to_ccecho, sizeof(to->to_ccecho));
2705fd8e4ebcSMike Barcroft 			to->to_ccecho = ntohl(to->to_ccecho);
2706a0292f23SGarrett Wollman 			break;
27071cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
27081cfd4b53SBruce M Simpson 		/*
27091cfd4b53SBruce M Simpson 		 * XXX In order to reply to a host which has set the
27101cfd4b53SBruce M Simpson 		 * TCP_SIGNATURE option in its initial SYN, we have to
27111cfd4b53SBruce M Simpson 		 * record the fact that the option was observed here
27121cfd4b53SBruce M Simpson 		 * for the syncache code to perform the correct response.
27131cfd4b53SBruce M Simpson 		 */
27141cfd4b53SBruce M Simpson 		case TCPOPT_SIGNATURE:
27151cfd4b53SBruce M Simpson 			if (optlen != TCPOLEN_SIGNATURE)
27161cfd4b53SBruce M Simpson 				continue;
27171cfd4b53SBruce M Simpson 			to->to_flags |= (TOF_SIGNATURE | TOF_SIGLEN);
27181cfd4b53SBruce M Simpson 			break;
2719265ed012SBruce M Simpson #endif
27206d90faf3SPaul Saab 		case TCPOPT_SACK_PERMITTED:
27216d90faf3SPaul Saab 			if (!tcp_do_sack ||
27226d90faf3SPaul Saab 			    optlen != TCPOLEN_SACK_PERMITTED)
27236d90faf3SPaul Saab 				continue;
27246d90faf3SPaul Saab 			if (is_syn) {
27256d90faf3SPaul Saab 				/* MUST only be set on SYN */
27266d90faf3SPaul Saab 				to->to_flags |= TOF_SACK;
27276d90faf3SPaul Saab 			}
27286d90faf3SPaul Saab 			break;
27296d90faf3SPaul Saab 
27306d90faf3SPaul Saab 		case TCPOPT_SACK:
27316d90faf3SPaul Saab 			if (!tp || tcp_sack_option(tp, th, cp, optlen))
27326d90faf3SPaul Saab 				continue;
27336d90faf3SPaul Saab 			break;
2734be2ac88cSJonathan Lemon 		default:
2735be2ac88cSJonathan Lemon 			continue;
2736df8bae1dSRodney W. Grimes 		}
2737df8bae1dSRodney W. Grimes 	}
2738df8bae1dSRodney W. Grimes }
2739df8bae1dSRodney W. Grimes 
2740df8bae1dSRodney W. Grimes /*
2741df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
2742df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
2743df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
2744df8bae1dSRodney W. Grimes  * sequencing purposes.
2745df8bae1dSRodney W. Grimes  */
27460312fbe9SPoul-Henning Kamp static void
2747fb59c426SYoshinobu Inoue tcp_pulloutofband(so, th, m, off)
2748df8bae1dSRodney W. Grimes 	struct socket *so;
2749fb59c426SYoshinobu Inoue 	struct tcphdr *th;
2750df8bae1dSRodney W. Grimes 	register struct mbuf *m;
2751fb59c426SYoshinobu Inoue 	int off;		/* delayed to be droped hdrlen */
2752df8bae1dSRodney W. Grimes {
2753fb59c426SYoshinobu Inoue 	int cnt = off + th->th_urp - 1;
2754df8bae1dSRodney W. Grimes 
2755df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
2756df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
2757df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
2758df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
2759df8bae1dSRodney W. Grimes 
2760df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
2761df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
2762df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
2763df8bae1dSRodney W. Grimes 			m->m_len--;
276469a34685SYoshinobu Inoue 			if (m->m_flags & M_PKTHDR)
276569a34685SYoshinobu Inoue 				m->m_pkthdr.len--;
2766df8bae1dSRodney W. Grimes 			return;
2767df8bae1dSRodney W. Grimes 		}
2768df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
2769df8bae1dSRodney W. Grimes 		m = m->m_next;
2770df8bae1dSRodney W. Grimes 		if (m == 0)
2771df8bae1dSRodney W. Grimes 			break;
2772df8bae1dSRodney W. Grimes 	}
2773df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
2774df8bae1dSRodney W. Grimes }
2775df8bae1dSRodney W. Grimes 
2776df8bae1dSRodney W. Grimes /*
2777df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
2778df8bae1dSRodney W. Grimes  * and update averages and current timeout.
2779df8bae1dSRodney W. Grimes  */
27800312fbe9SPoul-Henning Kamp static void
2781df8bae1dSRodney W. Grimes tcp_xmit_timer(tp, rtt)
2782df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
27839b8b58e0SJonathan Lemon 	int rtt;
2784df8bae1dSRodney W. Grimes {
2785233e8c18SGarrett Wollman 	register int delta;
2786233e8c18SGarrett Wollman 
2787233e8c18SGarrett Wollman 	tcpstat.tcps_rttupdated++;
2788233e8c18SGarrett Wollman 	tp->t_rttupdated++;
2789233e8c18SGarrett Wollman 	if (tp->t_srtt != 0) {
2790233e8c18SGarrett Wollman 		/*
2791233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
2792233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
2793233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
2794233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
2795233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
2796233e8c18SGarrett Wollman 		 */
2797233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
2798233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
2799233e8c18SGarrett Wollman 
2800233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
2801233e8c18SGarrett Wollman 			tp->t_srtt = 1;
2802233e8c18SGarrett Wollman 
2803233e8c18SGarrett Wollman 		/*
2804233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
2805233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
2806233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
2807233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
2808233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
2809233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
2810233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
2811233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
2812233e8c18SGarrett Wollman 		 */
2813233e8c18SGarrett Wollman 		if (delta < 0)
2814233e8c18SGarrett Wollman 			delta = -delta;
2815233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
2816233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
2817233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
28181fcc99b5SMatthew Dillon 		if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
28191fcc99b5SMatthew Dillon 		    tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
2820233e8c18SGarrett Wollman 	} else {
2821233e8c18SGarrett Wollman 		/*
2822233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
2823233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
2824233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
2825233e8c18SGarrett Wollman 		 */
2826233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
2827233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
28281fcc99b5SMatthew Dillon 		tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
2829233e8c18SGarrett Wollman 	}
28309b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
2831df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
2832df8bae1dSRodney W. Grimes 
2833df8bae1dSRodney W. Grimes 	/*
2834df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
2835df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
2836df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
2837df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
2838df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
2839df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
2840df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
2841df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
2842df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
2843df8bae1dSRodney W. Grimes 	 */
2844233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
28459e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
2846df8bae1dSRodney W. Grimes 
2847df8bae1dSRodney W. Grimes 	/*
2848df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
2849df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
2850df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
2851df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
2852df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
2853df8bae1dSRodney W. Grimes 	 */
2854df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
2855df8bae1dSRodney W. Grimes }
2856df8bae1dSRodney W. Grimes 
2857df8bae1dSRodney W. Grimes /*
2858df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
2859df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
2860df8bae1dSRodney W. Grimes  * If none, use an mss that can be handled on the outgoing
2861df8bae1dSRodney W. Grimes  * interface without forcing IP to fragment; if bigger than
2862df8bae1dSRodney W. Grimes  * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
2863df8bae1dSRodney W. Grimes  * to utilize large mbufs.  If no route is found, route has no mtu,
2864df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
2865df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
2866df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
2867df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
2868df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
2869df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
2870df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
2871a0292f23SGarrett Wollman  *
2872a0292f23SGarrett Wollman  * Also take into account the space needed for options that we
2873a0292f23SGarrett Wollman  * send regularly.  Make maxseg shorter by that amount to assure
2874a0292f23SGarrett Wollman  * that we can send maxseg amount of data even when the options
2875a0292f23SGarrett Wollman  * are present.  Store the upper limit of the length of options plus
2876a0292f23SGarrett Wollman  * data in maxopd.
2877a0292f23SGarrett Wollman  *
2878a0292f23SGarrett Wollman  *
2879a0292f23SGarrett Wollman  * In case of T/TCP, we call this routine during implicit connection
2880a0292f23SGarrett Wollman  * setup as well (offer = -1), to initialize maxseg from the cached
2881a0292f23SGarrett Wollman  * MSS of our peer.
288297d8d152SAndre Oppermann  *
288397d8d152SAndre Oppermann  * NOTE that this routine is only called when we process an incoming
288497d8d152SAndre Oppermann  * segment. Outgoing SYN/ACK MSS settings are handled in tcp_mssopt().
2885df8bae1dSRodney W. Grimes  */
2886a0292f23SGarrett Wollman void
2887df8bae1dSRodney W. Grimes tcp_mss(tp, offer)
2888a0292f23SGarrett Wollman 	struct tcpcb *tp;
2889a0292f23SGarrett Wollman 	int offer;
2890df8bae1dSRodney W. Grimes {
289197d8d152SAndre Oppermann 	int rtt, mss;
2892df8bae1dSRodney W. Grimes 	u_long bufsize;
289397d8d152SAndre Oppermann 	u_long maxmtu;
2894c068736aSJeffrey Hsu 	struct inpcb *inp = tp->t_inpcb;
2895df8bae1dSRodney W. Grimes 	struct socket *so;
289697d8d152SAndre Oppermann 	struct hc_metrics_lite metrics;
289797d8d152SAndre Oppermann 	struct rmxp_tao tao;
2898a0292f23SGarrett Wollman 	int origoffer = offer;
2899fb59c426SYoshinobu Inoue #ifdef INET6
2900c068736aSJeffrey Hsu 	int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
2901c068736aSJeffrey Hsu 	size_t min_protoh = isipv6 ?
2902c068736aSJeffrey Hsu 			    sizeof (struct ip6_hdr) + sizeof (struct tcphdr) :
2903c068736aSJeffrey Hsu 			    sizeof (struct tcpiphdr);
2904c068736aSJeffrey Hsu #else
2905c068736aSJeffrey Hsu 	const size_t min_protoh = sizeof(struct tcpiphdr);
2906fb59c426SYoshinobu Inoue #endif
290797d8d152SAndre Oppermann 	bzero(&tao, sizeof(tao));
2908df8bae1dSRodney W. Grimes 
290997d8d152SAndre Oppermann 	/* initialize */
291097d8d152SAndre Oppermann #ifdef INET6
291197d8d152SAndre Oppermann 	if (isipv6) {
291297d8d152SAndre Oppermann 		maxmtu = tcp_maxmtu6(&inp->inp_inc);
291397d8d152SAndre Oppermann 		tp->t_maxopd = tp->t_maxseg = tcp_v6mssdflt;
291497d8d152SAndre Oppermann 	} else
291597d8d152SAndre Oppermann #endif
291697d8d152SAndre Oppermann 	{
291797d8d152SAndre Oppermann 		maxmtu = tcp_maxmtu(&inp->inp_inc);
291897d8d152SAndre Oppermann 		tp->t_maxopd = tp->t_maxseg = tcp_mssdflt;
2919df8bae1dSRodney W. Grimes 	}
2920df8bae1dSRodney W. Grimes 	so = inp->inp_socket;
2921df8bae1dSRodney W. Grimes 
292297d8d152SAndre Oppermann 	/*
29232d166c02SAndre Oppermann 	 * no route to sender, stay with default mss and return
292497d8d152SAndre Oppermann 	 */
292597d8d152SAndre Oppermann 	if (maxmtu == 0)
292697d8d152SAndre Oppermann 		return;
292797d8d152SAndre Oppermann 
292897d8d152SAndre Oppermann 	/* what have we got? */
292997d8d152SAndre Oppermann 	switch (offer) {
293097d8d152SAndre Oppermann 		case 0:
293197d8d152SAndre Oppermann 			/*
293297d8d152SAndre Oppermann 			 * Offer == 0 means that there was no MSS on the SYN
293397d8d152SAndre Oppermann 			 * segment, in this case we use tcp_mssdflt.
293497d8d152SAndre Oppermann 			 */
293597d8d152SAndre Oppermann 			offer =
293697d8d152SAndre Oppermann #ifdef INET6
293797d8d152SAndre Oppermann 				isipv6 ? tcp_v6mssdflt :
293897d8d152SAndre Oppermann #endif
293997d8d152SAndre Oppermann 				tcp_mssdflt;
294097d8d152SAndre Oppermann 			break;
294197d8d152SAndre Oppermann 
294297d8d152SAndre Oppermann 		case -1:
2943a0292f23SGarrett Wollman 			/*
2944a0292f23SGarrett Wollman 			 * Offer == -1 means that we didn't receive SYN yet,
2945a0292f23SGarrett Wollman 			 * use cached value in that case;
2946a0292f23SGarrett Wollman 			 */
294797d8d152SAndre Oppermann 			if (tcp_do_rfc1644)
294897d8d152SAndre Oppermann 				tcp_hc_gettao(&inp->inp_inc, &tao);
294997d8d152SAndre Oppermann 			if (tao.tao_mssopt != 0)
295097d8d152SAndre Oppermann 				offer = tao.tao_mssopt;
295197d8d152SAndre Oppermann 			/* FALLTHROUGH */
295297d8d152SAndre Oppermann 
295397d8d152SAndre Oppermann 		default:
2954a0292f23SGarrett Wollman 			/*
295553369ac9SAndre Oppermann 			 * Prevent DoS attack with too small MSS. Round up
295653369ac9SAndre Oppermann 			 * to at least minmss.
295753369ac9SAndre Oppermann 			 */
295853369ac9SAndre Oppermann 			offer = max(offer, tcp_minmss);
295953369ac9SAndre Oppermann 			/*
2960a0292f23SGarrett Wollman 			 * Sanity check: make sure that maxopd will be large
296197d8d152SAndre Oppermann 			 * enough to allow some data on segments even if the
2962a0292f23SGarrett Wollman 			 * all the option space is used (40bytes).  Otherwise
2963a0292f23SGarrett Wollman 			 * funny things may happen in tcp_output.
2964a0292f23SGarrett Wollman 			 */
2965a0292f23SGarrett Wollman 			offer = max(offer, 64);
296697d8d152SAndre Oppermann 			if (tcp_do_rfc1644)
296797d8d152SAndre Oppermann 				tcp_hc_updatetao(&inp->inp_inc,
296897d8d152SAndre Oppermann 						 TCP_HC_TAO_MSSOPT, 0, offer);
296997d8d152SAndre Oppermann 	}
2970a0292f23SGarrett Wollman 
2971df8bae1dSRodney W. Grimes 	/*
297297d8d152SAndre Oppermann 	 * rmx information is now retrieved from tcp_hostcache
2973df8bae1dSRodney W. Grimes 	 */
297497d8d152SAndre Oppermann 	tcp_hc_get(&inp->inp_inc, &metrics);
297597d8d152SAndre Oppermann 
2976df8bae1dSRodney W. Grimes 	/*
297797d8d152SAndre Oppermann 	 * if there's a discovered mtu int tcp hostcache, use it
2978fb59c426SYoshinobu Inoue 	 * else, use the link mtu.
2979df8bae1dSRodney W. Grimes 	 */
298097d8d152SAndre Oppermann 	if (metrics.rmx_mtu)
29812d166c02SAndre Oppermann 		mss = min(metrics.rmx_mtu, maxmtu) - min_protoh;
2982c068736aSJeffrey Hsu 	else {
298331b3783cSHajimu UMEMOTO #ifdef INET6
2984fb59c426SYoshinobu Inoue 		if (isipv6) {
298597d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
298697d8d152SAndre Oppermann 			if (!path_mtu_discovery &&
298797d8d152SAndre Oppermann 			    !in6_localaddr(&inp->in6p_faddr))
2988fb59c426SYoshinobu Inoue 				mss = min(mss, tcp_v6mssdflt);
2989b3399803SHajimu UMEMOTO 		} else
2990b3399803SHajimu UMEMOTO #endif
299197d8d152SAndre Oppermann 		{
299297d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
299397d8d152SAndre Oppermann 			if (!path_mtu_discovery &&
299497d8d152SAndre Oppermann 			    !in_localaddr(inp->inp_faddr))
2995a0292f23SGarrett Wollman 				mss = min(mss, tcp_mssdflt);
2996a0292f23SGarrett Wollman 		}
299797d8d152SAndre Oppermann 	}
2998a0292f23SGarrett Wollman 	mss = min(mss, offer);
299997d8d152SAndre Oppermann 
3000a0292f23SGarrett Wollman 	/*
3001a0292f23SGarrett Wollman 	 * maxopd stores the maximum length of data AND options
3002a0292f23SGarrett Wollman 	 * in a segment; maxseg is the amount of data in a normal
3003a0292f23SGarrett Wollman 	 * segment.  We need to store this value (maxopd) apart
3004a0292f23SGarrett Wollman 	 * from maxseg, because now every segment carries options
3005a0292f23SGarrett Wollman 	 * and thus we normally have somewhat less data in segments.
3006a0292f23SGarrett Wollman 	 */
3007a0292f23SGarrett Wollman 	tp->t_maxopd = mss;
3008a0292f23SGarrett Wollman 
3009a0292f23SGarrett Wollman 	/*
3010a0292f23SGarrett Wollman 	 * In case of T/TCP, origoffer==-1 indicates, that no segments
3011a0292f23SGarrett Wollman 	 * were received yet.  In this case we just guess, otherwise
3012a0292f23SGarrett Wollman 	 * we do the same as before T/TCP.
3013a0292f23SGarrett Wollman 	 */
3014a0292f23SGarrett Wollman 	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
3015a0292f23SGarrett Wollman 	    (origoffer == -1 ||
3016a0292f23SGarrett Wollman 	     (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
3017a0292f23SGarrett Wollman 		mss -= TCPOLEN_TSTAMP_APPA;
3018a0292f23SGarrett Wollman 	if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
3019a0292f23SGarrett Wollman 	    (origoffer == -1 ||
3020a0292f23SGarrett Wollman 	     (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC))
3021a0292f23SGarrett Wollman 		mss -= TCPOLEN_CC_APPA;
302297d8d152SAndre Oppermann 	tp->t_maxseg = mss;
3023a0292f23SGarrett Wollman 
3024df8bae1dSRodney W. Grimes #if	(MCLBYTES & (MCLBYTES - 1)) == 0
3025df8bae1dSRodney W. Grimes 		if (mss > MCLBYTES)
3026df8bae1dSRodney W. Grimes 			mss &= ~(MCLBYTES-1);
3027df8bae1dSRodney W. Grimes #else
3028df8bae1dSRodney W. Grimes 		if (mss > MCLBYTES)
3029df8bae1dSRodney W. Grimes 			mss = mss / MCLBYTES * MCLBYTES;
3030df8bae1dSRodney W. Grimes #endif
303197d8d152SAndre Oppermann 	tp->t_maxseg = mss;
303297d8d152SAndre Oppermann 
3033df8bae1dSRodney W. Grimes 	/*
303497d8d152SAndre Oppermann 	 * If there's a pipesize, change the socket buffer to that size,
303597d8d152SAndre Oppermann 	 * don't change if sb_hiwat is different than default (then it
303697d8d152SAndre Oppermann 	 * has been changed on purpose with setsockopt).
303797d8d152SAndre Oppermann 	 * Make the socket buffers an integral number of mss units;
303897d8d152SAndre Oppermann 	 * if the mss is larger than the socket buffer, decrease the mss.
3039df8bae1dSRodney W. Grimes 	 */
30403f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
304197d8d152SAndre Oppermann 	if ((so->so_snd.sb_hiwat == tcp_sendspace) && metrics.rmx_sendpipe)
304297d8d152SAndre Oppermann 		bufsize = metrics.rmx_sendpipe;
304397d8d152SAndre Oppermann 	else
3044df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
3045df8bae1dSRodney W. Grimes 	if (bufsize < mss)
3046df8bae1dSRodney W. Grimes 		mss = bufsize;
3047df8bae1dSRodney W. Grimes 	else {
3048df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3049df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3050df8bae1dSRodney W. Grimes 			bufsize = sb_max;
305188c39af3SRuslan Ermilov 		if (bufsize > so->so_snd.sb_hiwat)
30523f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_snd, bufsize, so, NULL);
3053df8bae1dSRodney W. Grimes 	}
30543f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
3055df8bae1dSRodney W. Grimes 	tp->t_maxseg = mss;
3056df8bae1dSRodney W. Grimes 
30573f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
305897d8d152SAndre Oppermann 	if ((so->so_rcv.sb_hiwat == tcp_recvspace) && metrics.rmx_recvpipe)
305997d8d152SAndre Oppermann 		bufsize = metrics.rmx_recvpipe;
306097d8d152SAndre Oppermann 	else
3061df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
3062df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
3063df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3064df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3065df8bae1dSRodney W. Grimes 			bufsize = sb_max;
306688c39af3SRuslan Ermilov 		if (bufsize > so->so_rcv.sb_hiwat)
30673f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL);
3068df8bae1dSRodney W. Grimes 	}
30693f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
3070a0292f23SGarrett Wollman 	/*
307197d8d152SAndre Oppermann 	 * While we're here, check the others too
3072a0292f23SGarrett Wollman 	 */
307397d8d152SAndre Oppermann 	if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) {
307497d8d152SAndre Oppermann 		tp->t_srtt = rtt;
307597d8d152SAndre Oppermann 		tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE;
307697d8d152SAndre Oppermann 		tcpstat.tcps_usedrtt++;
307797d8d152SAndre Oppermann 		if (metrics.rmx_rttvar) {
307897d8d152SAndre Oppermann 			tp->t_rttvar = metrics.rmx_rttvar;
307997d8d152SAndre Oppermann 			tcpstat.tcps_usedrttvar++;
308097d8d152SAndre Oppermann 		} else {
308197d8d152SAndre Oppermann 			/* default variation is +- 1 rtt */
308297d8d152SAndre Oppermann 			tp->t_rttvar =
308397d8d152SAndre Oppermann 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
308497d8d152SAndre Oppermann 		}
308597d8d152SAndre Oppermann 		TCPT_RANGESET(tp->t_rxtcur,
308697d8d152SAndre Oppermann 			      ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
308797d8d152SAndre Oppermann 			      tp->t_rttmin, TCPTV_REXMTMAX);
308897d8d152SAndre Oppermann 	}
308997d8d152SAndre Oppermann 	if (metrics.rmx_ssthresh) {
3090df8bae1dSRodney W. Grimes 		/*
3091df8bae1dSRodney W. Grimes 		 * There's some sort of gateway or interface
3092df8bae1dSRodney W. Grimes 		 * buffer limit on the path.  Use this to set
3093df8bae1dSRodney W. Grimes 		 * the slow start threshhold, but set the
3094df8bae1dSRodney W. Grimes 		 * threshold to no less than 2*mss.
3095df8bae1dSRodney W. Grimes 		 */
309697d8d152SAndre Oppermann 		tp->snd_ssthresh = max(2 * mss, metrics.rmx_ssthresh);
3097dd224982SGarrett Wollman 		tcpstat.tcps_usedssthresh++;
3098df8bae1dSRodney W. Grimes 	}
309997d8d152SAndre Oppermann 	if (metrics.rmx_bandwidth)
310097d8d152SAndre Oppermann 		tp->snd_bandwidth = metrics.rmx_bandwidth;
310197d8d152SAndre Oppermann 
310297d8d152SAndre Oppermann 	/*
310397d8d152SAndre Oppermann 	 * Set the slow-start flight size depending on whether this
310497d8d152SAndre Oppermann 	 * is a local network or not.
310597d8d152SAndre Oppermann 	 *
310697d8d152SAndre Oppermann 	 * Extend this so we cache the cwnd too and retrieve it here.
310797d8d152SAndre Oppermann 	 * Make cwnd even bigger than RFC3390 suggests but only if we
310897d8d152SAndre Oppermann 	 * have previous experience with the remote host. Be careful
310997d8d152SAndre Oppermann 	 * not make cwnd bigger than remote receive window or our own
311097d8d152SAndre Oppermann 	 * send socket buffer. Maybe put some additional upper bound
311197d8d152SAndre Oppermann 	 * on the retrieved cwnd. Should do incremental updates to
311297d8d152SAndre Oppermann 	 * hostcache when cwnd collapses so next connection doesn't
311397d8d152SAndre Oppermann 	 * overloads the path again.
311497d8d152SAndre Oppermann 	 *
311597d8d152SAndre Oppermann 	 * RFC3390 says only do this if SYN or SYN/ACK didn't got lost.
311697d8d152SAndre Oppermann 	 * We currently check only in syncache_socket for that.
311797d8d152SAndre Oppermann 	 */
311897d8d152SAndre Oppermann #define TCP_METRICS_CWND
311997d8d152SAndre Oppermann #ifdef TCP_METRICS_CWND
312097d8d152SAndre Oppermann 	if (metrics.rmx_cwnd)
312197d8d152SAndre Oppermann 		tp->snd_cwnd = max(mss,
312297d8d152SAndre Oppermann 				min(metrics.rmx_cwnd / 2,
312397d8d152SAndre Oppermann 				 min(tp->snd_wnd, so->so_snd.sb_hiwat)));
312497d8d152SAndre Oppermann 	else
312597d8d152SAndre Oppermann #endif
312697d8d152SAndre Oppermann 	if (tcp_do_rfc3390)
312797d8d152SAndre Oppermann 		tp->snd_cwnd = min(4 * mss, max(2 * mss, 4380));
312897d8d152SAndre Oppermann #ifdef INET6
312997d8d152SAndre Oppermann 	else if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) ||
313097d8d152SAndre Oppermann 		 (!isipv6 && in_localaddr(inp->inp_faddr)))
3131943ae302SAndre Oppermann #else
3132943ae302SAndre Oppermann 	else if (in_localaddr(inp->inp_faddr))
313397d8d152SAndre Oppermann #endif
3134943ae302SAndre Oppermann 		tp->snd_cwnd = mss * ss_fltsz_local;
313597d8d152SAndre Oppermann 	else
313697d8d152SAndre Oppermann 		tp->snd_cwnd = mss * ss_fltsz;
3137a0292f23SGarrett Wollman }
3138a0292f23SGarrett Wollman 
3139a0292f23SGarrett Wollman /*
3140a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
3141a0292f23SGarrett Wollman  */
3142a0292f23SGarrett Wollman int
314397d8d152SAndre Oppermann tcp_mssopt(inc)
314497d8d152SAndre Oppermann 	struct in_conninfo *inc;
3145a0292f23SGarrett Wollman {
314697d8d152SAndre Oppermann 	int mss = 0;
314797d8d152SAndre Oppermann 	u_long maxmtu = 0;
314897d8d152SAndre Oppermann 	u_long thcmtu = 0;
314997d8d152SAndre Oppermann 	size_t min_protoh;
3150fb59c426SYoshinobu Inoue #ifdef INET6
315197d8d152SAndre Oppermann 	int isipv6 = inc->inc_isipv6 ? 1 : 0;
3152fb59c426SYoshinobu Inoue #endif
3153a0292f23SGarrett Wollman 
315497d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer"));
3155a0292f23SGarrett Wollman 
315631b3783cSHajimu UMEMOTO #ifdef INET6
315797d8d152SAndre Oppermann 	if (isipv6) {
315897d8d152SAndre Oppermann 		mss = tcp_v6mssdflt;
315997d8d152SAndre Oppermann 		maxmtu = tcp_maxmtu6(inc);
316097d8d152SAndre Oppermann 		thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
316197d8d152SAndre Oppermann 		min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
316297d8d152SAndre Oppermann 	} else
316331b3783cSHajimu UMEMOTO #endif
316497d8d152SAndre Oppermann 	{
316597d8d152SAndre Oppermann 		mss = tcp_mssdflt;
316697d8d152SAndre Oppermann 		maxmtu = tcp_maxmtu(inc);
316797d8d152SAndre Oppermann 		thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
316897d8d152SAndre Oppermann 		min_protoh = sizeof(struct tcpiphdr);
316997d8d152SAndre Oppermann 	}
317097d8d152SAndre Oppermann 	if (maxmtu && thcmtu)
317197d8d152SAndre Oppermann 		mss = min(maxmtu, thcmtu) - min_protoh;
317297d8d152SAndre Oppermann 	else if (maxmtu || thcmtu)
317397d8d152SAndre Oppermann 		mss = max(maxmtu, thcmtu) - min_protoh;
317497d8d152SAndre Oppermann 
317597d8d152SAndre Oppermann 	return (mss);
3176df8bae1dSRodney W. Grimes }
317746f58482SJonathan Lemon 
317846f58482SJonathan Lemon 
317946f58482SJonathan Lemon /*
3180c068736aSJeffrey Hsu  * On a partial ack arrives, force the retransmission of the
3181c068736aSJeffrey Hsu  * next unacknowledged segment.  Do not clear tp->t_dupacks.
3182c068736aSJeffrey Hsu  * By setting snd_nxt to ti_ack, this forces retransmission timer to
3183c068736aSJeffrey Hsu  * be started again.
318446f58482SJonathan Lemon  */
3185c068736aSJeffrey Hsu static void
3186c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th)
318746f58482SJonathan Lemon 	struct tcpcb *tp;
318846f58482SJonathan Lemon 	struct tcphdr *th;
318946f58482SJonathan Lemon {
319046f58482SJonathan Lemon 	tcp_seq onxt = tp->snd_nxt;
319146f58482SJonathan Lemon 	u_long  ocwnd = tp->snd_cwnd;
319246f58482SJonathan Lemon 
319346f58482SJonathan Lemon 	callout_stop(tp->tt_rexmt);
319446f58482SJonathan Lemon 	tp->t_rtttime = 0;
319546f58482SJonathan Lemon 	tp->snd_nxt = th->th_ack;
31966b2a5f92SJayanth Vijayaraghavan 	/*
3197c068736aSJeffrey Hsu 	 * Set snd_cwnd to one segment beyond acknowledged offset.
3198c068736aSJeffrey Hsu 	 * (tp->snd_una has not yet been updated when this function is called.)
31996b2a5f92SJayanth Vijayaraghavan 	 */
32006b2a5f92SJayanth Vijayaraghavan 	tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una);
3201a84db8f4SMatthew Dillon 	tp->t_flags |= TF_ACKNOW;
32026b2a5f92SJayanth Vijayaraghavan 	(void) tcp_output(tp);
320346f58482SJonathan Lemon 	tp->snd_cwnd = ocwnd;
320446f58482SJonathan Lemon 	if (SEQ_GT(onxt, tp->snd_nxt))
320546f58482SJonathan Lemon 		tp->snd_nxt = onxt;
320646f58482SJonathan Lemon 	/*
320746f58482SJonathan Lemon 	 * Partial window deflation.  Relies on fact that tp->snd_una
320846f58482SJonathan Lemon 	 * not updated yet.
320946f58482SJonathan Lemon 	 */
321046f58482SJonathan Lemon 	tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_maxseg);
321146f58482SJonathan Lemon }
3212340c35deSJonathan Lemon 
3213340c35deSJonathan Lemon /*
3214340c35deSJonathan Lemon  * Returns 1 if the TIME_WAIT state was killed and we should start over,
3215340c35deSJonathan Lemon  * looking for a pcb in the listen state.  Returns 0 otherwise.
3216340c35deSJonathan Lemon  */
3217340c35deSJonathan Lemon static int
3218340c35deSJonathan Lemon tcp_timewait(tw, to, th, m, tlen)
3219340c35deSJonathan Lemon 	struct tcptw *tw;
3220340c35deSJonathan Lemon 	struct tcpopt *to;
3221340c35deSJonathan Lemon 	struct tcphdr *th;
3222340c35deSJonathan Lemon 	struct mbuf *m;
3223340c35deSJonathan Lemon 	int tlen;
3224340c35deSJonathan Lemon {
3225340c35deSJonathan Lemon 	int thflags;
3226340c35deSJonathan Lemon 	tcp_seq seq;
3227340c35deSJonathan Lemon #ifdef INET6
3228340c35deSJonathan Lemon 	int isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
3229340c35deSJonathan Lemon #else
3230340c35deSJonathan Lemon 	const int isipv6 = 0;
3231340c35deSJonathan Lemon #endif
3232340c35deSJonathan Lemon 
3233340c35deSJonathan Lemon 	thflags = th->th_flags;
3234340c35deSJonathan Lemon 
3235340c35deSJonathan Lemon 	/*
3236340c35deSJonathan Lemon 	 * NOTE: for FIN_WAIT_2 (to be added later),
3237340c35deSJonathan Lemon 	 * must validate sequence number before accepting RST
3238340c35deSJonathan Lemon 	 */
3239340c35deSJonathan Lemon 
3240340c35deSJonathan Lemon 	/*
3241340c35deSJonathan Lemon 	 * If the segment contains RST:
3242340c35deSJonathan Lemon 	 *	Drop the segment - see Stevens, vol. 2, p. 964 and
3243340c35deSJonathan Lemon 	 *      RFC 1337.
3244340c35deSJonathan Lemon 	 */
3245340c35deSJonathan Lemon 	if (thflags & TH_RST)
3246340c35deSJonathan Lemon 		goto drop;
3247340c35deSJonathan Lemon 
3248340c35deSJonathan Lemon 	/*
3249340c35deSJonathan Lemon 	 * If segment contains a SYN and CC [not CC.NEW] option:
3250340c35deSJonathan Lemon 	 *	if connection duration > MSL, drop packet and send RST;
3251340c35deSJonathan Lemon 	 *
3252340c35deSJonathan Lemon 	 *	if SEG.CC > CCrecv then is new SYN.
3253340c35deSJonathan Lemon 	 *	    Complete close and delete TCPCB.  Then reprocess
3254340c35deSJonathan Lemon 	 *	    segment, hoping to find new TCPCB in LISTEN state;
3255340c35deSJonathan Lemon 	 *
3256340c35deSJonathan Lemon 	 *	else must be old SYN; drop it.
3257340c35deSJonathan Lemon 	 * else do normal processing.
3258340c35deSJonathan Lemon 	 */
3259340c35deSJonathan Lemon 	if ((thflags & TH_SYN) && (to->to_flags & TOF_CC) && tw->cc_recv != 0) {
3260340c35deSJonathan Lemon 		if ((ticks - tw->t_starttime) > tcp_msl)
3261340c35deSJonathan Lemon 			goto reset;
3262340c35deSJonathan Lemon 		if (CC_GT(to->to_cc, tw->cc_recv)) {
3263607b0b0cSJonathan Lemon 			(void) tcp_twclose(tw, 0);
3264340c35deSJonathan Lemon 			return (1);
3265340c35deSJonathan Lemon 		}
3266340c35deSJonathan Lemon 		goto drop;
3267340c35deSJonathan Lemon 	}
3268340c35deSJonathan Lemon 
3269340c35deSJonathan Lemon #if 0
3270340c35deSJonathan Lemon /* PAWS not needed at the moment */
3271340c35deSJonathan Lemon 	/*
3272340c35deSJonathan Lemon 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
3273340c35deSJonathan Lemon 	 * and it's less than ts_recent, drop it.
3274340c35deSJonathan Lemon 	 */
3275340c35deSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
3276340c35deSJonathan Lemon 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
3277340c35deSJonathan Lemon 		if ((thflags & TH_ACK) == 0)
3278340c35deSJonathan Lemon 			goto drop;
3279340c35deSJonathan Lemon 		goto ack;
3280340c35deSJonathan Lemon 	}
3281340c35deSJonathan Lemon 	/*
3282340c35deSJonathan Lemon 	 * ts_recent is never updated because we never accept new segments.
3283340c35deSJonathan Lemon 	 */
3284340c35deSJonathan Lemon #endif
3285340c35deSJonathan Lemon 
3286340c35deSJonathan Lemon 	/*
3287340c35deSJonathan Lemon 	 * If a new connection request is received
3288340c35deSJonathan Lemon 	 * while in TIME_WAIT, drop the old connection
3289340c35deSJonathan Lemon 	 * and start over if the sequence numbers
3290340c35deSJonathan Lemon 	 * are above the previous ones.
3291340c35deSJonathan Lemon 	 */
3292340c35deSJonathan Lemon 	if ((thflags & TH_SYN) && SEQ_GT(th->th_seq, tw->rcv_nxt)) {
3293607b0b0cSJonathan Lemon 		(void) tcp_twclose(tw, 0);
3294340c35deSJonathan Lemon 		return (1);
3295340c35deSJonathan Lemon 	}
3296340c35deSJonathan Lemon 
3297340c35deSJonathan Lemon 	/*
3298340c35deSJonathan Lemon 	 * Drop the the segment if it does not contain an ACK.
3299340c35deSJonathan Lemon 	 */
3300340c35deSJonathan Lemon 	if ((thflags & TH_ACK) == 0)
3301340c35deSJonathan Lemon 		goto drop;
3302340c35deSJonathan Lemon 
3303340c35deSJonathan Lemon 	/*
3304340c35deSJonathan Lemon 	 * Reset the 2MSL timer if this is a duplicate FIN.
3305340c35deSJonathan Lemon 	 */
3306340c35deSJonathan Lemon 	if (thflags & TH_FIN) {
3307340c35deSJonathan Lemon 		seq = th->th_seq + tlen + (thflags & TH_SYN ? 1 : 0);
3308340c35deSJonathan Lemon 		if (seq + 1 == tw->rcv_nxt)
3309607b0b0cSJonathan Lemon 			tcp_timer_2msl_reset(tw, 2 * tcp_msl);
3310340c35deSJonathan Lemon 	}
3311340c35deSJonathan Lemon 
3312340c35deSJonathan Lemon 	/*
3313272c5dfeSJonathan Lemon 	 * Acknowledge the segment if it has data or is not a duplicate ACK.
3314340c35deSJonathan Lemon 	 */
3315272c5dfeSJonathan Lemon 	if (thflags != TH_ACK || tlen != 0 ||
3316272c5dfeSJonathan Lemon 	    th->th_seq != tw->rcv_nxt || th->th_ack != tw->snd_nxt)
3317a7b6a14aSRobert Watson 		tcp_twrespond(tw, TH_ACK);
3318340c35deSJonathan Lemon 	goto drop;
3319340c35deSJonathan Lemon 
3320340c35deSJonathan Lemon reset:
3321340c35deSJonathan Lemon 	/*
3322340c35deSJonathan Lemon 	 * Generate a RST, dropping incoming segment.
3323340c35deSJonathan Lemon 	 * Make ACK acceptable to originator of segment.
3324340c35deSJonathan Lemon 	 * Don't bother to respond if destination was broadcast/multicast.
3325340c35deSJonathan Lemon 	 */
3326340c35deSJonathan Lemon 	if (m->m_flags & (M_BCAST|M_MCAST))
3327340c35deSJonathan Lemon 		goto drop;
3328340c35deSJonathan Lemon 	if (isipv6) {
3329340c35deSJonathan Lemon 		struct ip6_hdr *ip6;
3330340c35deSJonathan Lemon 
3331340c35deSJonathan Lemon 		/* IPv6 anycast check is done at tcp6_input() */
3332340c35deSJonathan Lemon 		ip6 = mtod(m, struct ip6_hdr *);
3333340c35deSJonathan Lemon 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
3334340c35deSJonathan Lemon 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
3335340c35deSJonathan Lemon 			goto drop;
3336340c35deSJonathan Lemon 	} else {
3337340c35deSJonathan Lemon 		struct ip *ip;
3338340c35deSJonathan Lemon 
3339340c35deSJonathan Lemon 		ip = mtod(m, struct ip *);
3340340c35deSJonathan Lemon 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
3341340c35deSJonathan Lemon 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
3342340c35deSJonathan Lemon 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
3343340c35deSJonathan Lemon 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
3344340c35deSJonathan Lemon 			goto drop;
3345340c35deSJonathan Lemon 	}
3346340c35deSJonathan Lemon 	if (thflags & TH_ACK) {
3347340c35deSJonathan Lemon 		tcp_respond(NULL,
3348340c35deSJonathan Lemon 		    mtod(m, void *), th, m, 0, th->th_ack, TH_RST);
3349340c35deSJonathan Lemon 	} else {
3350340c35deSJonathan Lemon 		seq = th->th_seq + (thflags & TH_SYN ? 1 : 0);
3351340c35deSJonathan Lemon 		tcp_respond(NULL,
3352340c35deSJonathan Lemon 		    mtod(m, void *), th, m, seq, 0, TH_RST|TH_ACK);
3353340c35deSJonathan Lemon 	}
3354340c35deSJonathan Lemon 	INP_UNLOCK(tw->tw_inpcb);
3355340c35deSJonathan Lemon 	return (0);
3356340c35deSJonathan Lemon 
3357340c35deSJonathan Lemon drop:
3358340c35deSJonathan Lemon 	INP_UNLOCK(tw->tw_inpcb);
3359340c35deSJonathan Lemon 	m_freem(m);
3360340c35deSJonathan Lemon 	return (0);
3361340c35deSJonathan Lemon }
3362