xref: /freebsd/sys/netinet/tcp_input.c (revision 6741ecf5950f8d1ee962d0d2c18901914b79717b)
1c398230bSWarner Losh /*-
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>
44df8bae1dSRodney W. Grimes #include <sys/malloc.h>
45df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
46a29f300eSGarrett Wollman #include <sys/proc.h>		/* for proc0 declaration */
47df8bae1dSRodney W. Grimes #include <sys/protosw.h>
48960ed29cSSeigo Tanimura #include <sys/signalvar.h>
49df8bae1dSRodney W. Grimes #include <sys/socket.h>
50df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
51960ed29cSSeigo Tanimura #include <sys/sysctl.h>
52816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
53960ed29cSSeigo Tanimura #include <sys/systm.h>
54df8bae1dSRodney W. Grimes 
55e79adb8eSGarrett Wollman #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
56e79adb8eSGarrett Wollman 
5712e2e970SAndre Oppermann #include <vm/uma.h>
5812e2e970SAndre Oppermann 
59df8bae1dSRodney W. Grimes #include <net/if.h>
60df8bae1dSRodney W. Grimes #include <net/route.h>
61df8bae1dSRodney W. Grimes 
62df8bae1dSRodney W. Grimes #include <netinet/in.h>
63960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
64df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
65960ed29cSSeigo Tanimura #include <netinet/in_var.h>
66df8bae1dSRodney W. Grimes #include <netinet/ip.h>
678e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h>	/* required for icmp_var.h */
68686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM */
69df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
70ef39adf0SAndre Oppermann #include <netinet/ip_options.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 
100aed55708SRobert Watson #include <security/mac/mac_framework.h>
101aed55708SRobert Watson 
102c068736aSJeffrey Hsu static const int tcprexmtthresh = 3;
1030312fbe9SPoul-Henning Kamp 
1042f96f1f4SGarrett Wollman struct	tcpstat tcpstat;
105c73d99b5SRuslan Ermilov SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, CTLFLAG_RW,
1063d177f46SBill Fumerola     &tcpstat , tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
1070312fbe9SPoul-Henning Kamp 
108d78a37adSPaul Traina static int log_in_vain = 0;
109816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW,
1103d177f46SBill Fumerola     &log_in_vain, 0, "Log all incoming TCP connections");
111816a3d83SPoul-Henning Kamp 
11216f7f31fSGeoff Rehmet static int blackhole = 0;
11316f7f31fSGeoff Rehmet SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_RW,
11416f7f31fSGeoff Rehmet 	&blackhole, 0, "Do not send RST when dropping refused connections");
11516f7f31fSGeoff Rehmet 
116f498eeeeSDavid Greenman int tcp_delack_enabled = 1;
11784e33c9eSDavid Greenman SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW,
1183d177f46SBill Fumerola     &tcp_delack_enabled, 0,
1193d177f46SBill Fumerola     "Delay ACK to try and piggyback it onto a data packet");
120f498eeeeSDavid Greenman 
121f8613305SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN
122f8613305SDag-Erling Smørgrav static int drop_synfin = 0;
123f8613305SDag-Erling Smørgrav SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_RW,
124f8613305SDag-Erling Smørgrav     &drop_synfin, 0, "Drop TCP packets with SYN+FIN set");
125f8613305SDag-Erling Smørgrav #endif
126f8613305SDag-Erling Smørgrav 
127dba7bc6aSAndre Oppermann static int tcp_do_rfc3042 = 1;
128582a954bSJeffrey Hsu SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_RW,
129582a954bSJeffrey Hsu     &tcp_do_rfc3042, 0, "Enable RFC 3042 (Limited Transmit)");
130582a954bSJeffrey Hsu 
131dba7bc6aSAndre Oppermann static int tcp_do_rfc3390 = 1;
132da3a8a1aSJeffrey Hsu SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_RW,
133da3a8a1aSJeffrey Hsu     &tcp_do_rfc3390, 0,
134da3a8a1aSJeffrey Hsu     "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)");
135da3a8a1aSJeffrey Hsu 
136a69968eeSMike Silbersack static int tcp_insecure_rst = 0;
137a69968eeSMike Silbersack SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_RW,
138a69968eeSMike Silbersack     &tcp_insecure_rst, 0,
139a69968eeSMike Silbersack     "Follow the old (insecure) criteria for accepting RST packets.");
140a69968eeSMike Silbersack 
14112e2e970SAndre Oppermann SYSCTL_NODE(_net_inet_tcp, OID_AUTO, reass, CTLFLAG_RW, 0,
14212e2e970SAndre Oppermann 	    "TCP Segment Reassembly Queue");
14312e2e970SAndre Oppermann 
14412e2e970SAndre Oppermann static int tcp_reass_maxseg = 0;
14512e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxsegments, CTLFLAG_RDTUN,
14612e2e970SAndre Oppermann 	   &tcp_reass_maxseg, 0,
14712e2e970SAndre Oppermann 	   "Global maximum number of TCP Segments in Reassembly Queue");
14812e2e970SAndre Oppermann 
14912e2e970SAndre Oppermann int tcp_reass_qsize = 0;
15012e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, cursegments, CTLFLAG_RD,
15112e2e970SAndre Oppermann 	   &tcp_reass_qsize, 0,
15212e2e970SAndre Oppermann 	   "Global number of TCP Segments currently in Reassembly Queue");
15312e2e970SAndre Oppermann 
15412e2e970SAndre Oppermann static int tcp_reass_maxqlen = 48;
15512e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, maxqlen, CTLFLAG_RW,
15612e2e970SAndre Oppermann 	   &tcp_reass_maxqlen, 0,
15712e2e970SAndre Oppermann 	   "Maximum number of TCP Segments per individual Reassembly Queue");
15812e2e970SAndre Oppermann 
15912e2e970SAndre Oppermann static int tcp_reass_overflows = 0;
16012e2e970SAndre Oppermann SYSCTL_INT(_net_inet_tcp_reass, OID_AUTO, overflows, CTLFLAG_RD,
16112e2e970SAndre Oppermann 	   &tcp_reass_overflows, 0,
16212e2e970SAndre Oppermann 	   "Global number of TCP Segment Reassembly Queue Overflows");
16312e2e970SAndre Oppermann 
1646741ecf5SAndre Oppermann int	tcp_do_autorcvbuf = 1;
1656741ecf5SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_RW,
1666741ecf5SAndre Oppermann 	   &tcp_do_autorcvbuf, 0, "Enable automatic receive buffer sizing");
1676741ecf5SAndre Oppermann 
1686741ecf5SAndre Oppermann int	tcp_autorcvbuf_inc = 16*1024;
1696741ecf5SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_RW,
1706741ecf5SAndre Oppermann 	   &tcp_autorcvbuf_inc, 0, "Incrementor step size of automatic receive buffer");
1716741ecf5SAndre Oppermann 
1726741ecf5SAndre Oppermann int	tcp_autorcvbuf_max = 256*1024;
1736741ecf5SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_RW,
1746741ecf5SAndre Oppermann 	   &tcp_autorcvbuf_max, 0, "Max size of automatic receive buffer");
1756741ecf5SAndre Oppermann 
17615bd2b43SDavid Greenman struct inpcbhead tcb;
177fb59c426SYoshinobu Inoue #define	tcb6	tcb  /* for KAME src sync over BSD*'s */
17815bd2b43SDavid Greenman struct inpcbinfo tcbinfo;
179f76fcf6dSJeffrey Hsu struct mtx	*tcbinfo_mtx;
180df8bae1dSRodney W. Grimes 
1815a53ca16SPaul Saab static void	 tcp_dooptions(struct tcpopt *, u_char *, int, int);
1826d90faf3SPaul Saab 
1834d77a549SAlfred Perlstein static void	 tcp_pulloutofband(struct socket *,
1844d77a549SAlfred Perlstein 		     struct tcphdr *, struct mbuf *, int);
1854d77a549SAlfred Perlstein static int	 tcp_reass(struct tcpcb *, struct tcphdr *, int *,
1864d77a549SAlfred Perlstein 		     struct mbuf *);
1874d77a549SAlfred Perlstein static void	 tcp_xmit_timer(struct tcpcb *, int);
188c068736aSJeffrey Hsu static void	 tcp_newreno_partial_ack(struct tcpcb *, struct tcphdr *);
1893cbe7fafSRobert Watson static int	 tcp_timewait(struct inpcb *, struct tcpopt *,
190340c35deSJonathan Lemon 		     struct tcphdr *, struct mbuf *, int);
1910312fbe9SPoul-Henning Kamp 
192fb59c426SYoshinobu Inoue /* Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint. */
193fb59c426SYoshinobu Inoue #ifdef INET6
194fb59c426SYoshinobu Inoue #define ND6_HINT(tp) \
195fb59c426SYoshinobu Inoue do { \
196fb59c426SYoshinobu Inoue 	if ((tp) && (tp)->t_inpcb && \
19797d8d152SAndre Oppermann 	    ((tp)->t_inpcb->inp_vflag & INP_IPV6) != 0) \
19897d8d152SAndre Oppermann 		nd6_nud_hint(NULL, NULL, 0); \
199fb59c426SYoshinobu Inoue } while (0)
200fb59c426SYoshinobu Inoue #else
201fb59c426SYoshinobu Inoue #define ND6_HINT(tp)
202fb59c426SYoshinobu Inoue #endif
203df8bae1dSRodney W. Grimes 
204df8bae1dSRodney W. Grimes /*
205262c1c1aSMatthew Dillon  * Indicate whether this ack should be delayed.  We can delay the ack if
206262c1c1aSMatthew Dillon  *	- there is no delayed ack timer in progress and
207262c1c1aSMatthew Dillon  *	- our last ack wasn't a 0-sized window.  We never want to delay
2083bfd6421SJonathan Lemon  *	  the ack that opens up a 0-sized window and
2093bfd6421SJonathan Lemon  *		- delayed acks are enabled or
2103bfd6421SJonathan Lemon  *		- this is a half-synchronized T/TCP connection.
211d8c85a26SJonathan Lemon  */
212d8c85a26SJonathan Lemon #define DELAY_ACK(tp)							\
213a14c749fSJonathan Lemon 	((!callout_active(tp->tt_delack) &&				\
214a14c749fSJonathan Lemon 	    (tp->t_flags & TF_RXWIN0SENT) == 0) &&			\
2153bfd6421SJonathan Lemon 	    (tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN)))
216d8c85a26SJonathan Lemon 
21712e2e970SAndre Oppermann /* Initialize TCP reassembly queue */
2184f590175SPaul Saab static void
2194f590175SPaul Saab tcp_reass_zone_change(void *tag)
2204f590175SPaul Saab {
2214f590175SPaul Saab 
2224f590175SPaul Saab 	tcp_reass_maxseg = nmbclusters / 16;
2234f590175SPaul Saab 	uma_zone_set_max(tcp_reass_zone, tcp_reass_maxseg);
2244f590175SPaul Saab }
2254f590175SPaul Saab 
22612e2e970SAndre Oppermann uma_zone_t	tcp_reass_zone;
22712e2e970SAndre Oppermann void
22812e2e970SAndre Oppermann tcp_reass_init()
22912e2e970SAndre Oppermann {
23012e2e970SAndre Oppermann 	tcp_reass_maxseg = nmbclusters / 16;
23112e2e970SAndre Oppermann 	TUNABLE_INT_FETCH("net.inet.tcp.reass.maxsegments",
23212e2e970SAndre Oppermann 	    &tcp_reass_maxseg);
23312e2e970SAndre Oppermann 	tcp_reass_zone = uma_zcreate("tcpreass", sizeof (struct tseg_qent),
23412e2e970SAndre Oppermann 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
23512e2e970SAndre Oppermann 	uma_zone_set_max(tcp_reass_zone, tcp_reass_maxseg);
2364f590175SPaul Saab 	EVENTHANDLER_REGISTER(nmbclusters_change,
2374f590175SPaul Saab 	    tcp_reass_zone_change, NULL, EVENTHANDLER_PRI_ANY);
23812e2e970SAndre Oppermann }
23912e2e970SAndre Oppermann 
2400312fbe9SPoul-Henning Kamp static int
241fb59c426SYoshinobu Inoue tcp_reass(tp, th, tlenp, m)
242df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
243fb59c426SYoshinobu Inoue 	register struct tcphdr *th;
244fb59c426SYoshinobu Inoue 	int *tlenp;
245df8bae1dSRodney W. Grimes 	struct mbuf *m;
246df8bae1dSRodney W. Grimes {
247fb59c426SYoshinobu Inoue 	struct tseg_qent *q;
248fb59c426SYoshinobu Inoue 	struct tseg_qent *p = NULL;
249fb59c426SYoshinobu Inoue 	struct tseg_qent *nq;
25012e2e970SAndre Oppermann 	struct tseg_qent *te = NULL;
251df8bae1dSRodney W. Grimes 	struct socket *so = tp->t_inpcb->inp_socket;
252df8bae1dSRodney W. Grimes 	int flags;
253df8bae1dSRodney W. Grimes 
254de30ea13SRobert Watson 	INP_LOCK_ASSERT(tp->t_inpcb);
255de30ea13SRobert Watson 
256df8bae1dSRodney W. Grimes 	/*
25712e2e970SAndre Oppermann 	 * XXX: tcp_reass() is rather inefficient with its data structures
25812e2e970SAndre Oppermann 	 * and should be rewritten (see NetBSD for optimizations).  While
25912e2e970SAndre Oppermann 	 * doing that it should move to its own file tcp_reass.c.
26012e2e970SAndre Oppermann 	 */
26112e2e970SAndre Oppermann 
26212e2e970SAndre Oppermann 	/*
263de30ea13SRobert Watson 	 * Call with th==NULL after become established to
264df8bae1dSRodney W. Grimes 	 * force pre-ESTABLISHED data up to user socket.
265df8bae1dSRodney W. Grimes 	 */
266de30ea13SRobert Watson 	if (th == NULL)
267df8bae1dSRodney W. Grimes 		goto present;
268df8bae1dSRodney W. Grimes 
26912e2e970SAndre Oppermann 	/*
27012e2e970SAndre Oppermann 	 * Limit the number of segments in the reassembly queue to prevent
27112e2e970SAndre Oppermann 	 * holding on to too many segments (and thus running out of mbufs).
27212e2e970SAndre Oppermann 	 * Make sure to let the missing segment through which caused this
27312e2e970SAndre Oppermann 	 * queue.  Always keep one global queue entry spare to be able to
27412e2e970SAndre Oppermann 	 * process the missing segment.
27512e2e970SAndre Oppermann 	 */
27612e2e970SAndre Oppermann 	if (th->th_seq != tp->rcv_nxt &&
27712e2e970SAndre Oppermann 	    (tcp_reass_qsize + 1 >= tcp_reass_maxseg ||
27812e2e970SAndre Oppermann 	     tp->t_segqlen >= tcp_reass_maxqlen)) {
27912e2e970SAndre Oppermann 		tcp_reass_overflows++;
28012e2e970SAndre Oppermann 		tcpstat.tcps_rcvmemdrop++;
28112e2e970SAndre Oppermann 		m_freem(m);
282e346eeffSPaul Saab 		*tlenp = 0;
28312e2e970SAndre Oppermann 		return (0);
28412e2e970SAndre Oppermann 	}
28512e2e970SAndre Oppermann 
28612e2e970SAndre Oppermann 	/*
28712e2e970SAndre Oppermann 	 * Allocate a new queue entry. If we can't, or hit the zone limit
28812e2e970SAndre Oppermann 	 * just drop the pkt.
28912e2e970SAndre Oppermann 	 */
29012e2e970SAndre Oppermann 	te = uma_zalloc(tcp_reass_zone, M_NOWAIT);
291fb59c426SYoshinobu Inoue 	if (te == NULL) {
292fb59c426SYoshinobu Inoue 		tcpstat.tcps_rcvmemdrop++;
293fb59c426SYoshinobu Inoue 		m_freem(m);
294e346eeffSPaul Saab 		*tlenp = 0;
295fb59c426SYoshinobu Inoue 		return (0);
296fb59c426SYoshinobu Inoue 	}
29712e2e970SAndre Oppermann 	tp->t_segqlen++;
29812e2e970SAndre Oppermann 	tcp_reass_qsize++;
2996effc713SDoug Rabson 
300df8bae1dSRodney W. Grimes 	/*
301df8bae1dSRodney W. Grimes 	 * Find a segment which begins after this one does.
302df8bae1dSRodney W. Grimes 	 */
303fb59c426SYoshinobu Inoue 	LIST_FOREACH(q, &tp->t_segq, tqe_q) {
304fb59c426SYoshinobu Inoue 		if (SEQ_GT(q->tqe_th->th_seq, th->th_seq))
305df8bae1dSRodney W. Grimes 			break;
306fb59c426SYoshinobu Inoue 		p = q;
307fb59c426SYoshinobu Inoue 	}
308df8bae1dSRodney W. Grimes 
309df8bae1dSRodney W. Grimes 	/*
310df8bae1dSRodney W. Grimes 	 * If there is a preceding segment, it may provide some of
311df8bae1dSRodney W. Grimes 	 * our data already.  If so, drop the data from the incoming
312df8bae1dSRodney W. Grimes 	 * segment.  If it provides all of our data, drop us.
313df8bae1dSRodney W. Grimes 	 */
3146effc713SDoug Rabson 	if (p != NULL) {
315df8bae1dSRodney W. Grimes 		register int i;
316df8bae1dSRodney W. Grimes 		/* conversion to int (in i) handles seq wraparound */
317fb59c426SYoshinobu Inoue 		i = p->tqe_th->th_seq + p->tqe_len - th->th_seq;
318df8bae1dSRodney W. Grimes 		if (i > 0) {
319fb59c426SYoshinobu Inoue 			if (i >= *tlenp) {
320df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvduppack++;
321fb59c426SYoshinobu Inoue 				tcpstat.tcps_rcvdupbyte += *tlenp;
322df8bae1dSRodney W. Grimes 				m_freem(m);
32312e2e970SAndre Oppermann 				uma_zfree(tcp_reass_zone, te);
32412e2e970SAndre Oppermann 				tp->t_segqlen--;
32512e2e970SAndre Oppermann 				tcp_reass_qsize--;
326a0292f23SGarrett Wollman 				/*
327a0292f23SGarrett Wollman 				 * Try to present any queued data
328a0292f23SGarrett Wollman 				 * at the left window edge to the user.
329a0292f23SGarrett Wollman 				 * This is needed after the 3-WHS
330a0292f23SGarrett Wollman 				 * completes.
331a0292f23SGarrett Wollman 				 */
332a0292f23SGarrett Wollman 				goto present;	/* ??? */
333df8bae1dSRodney W. Grimes 			}
334df8bae1dSRodney W. Grimes 			m_adj(m, i);
335fb59c426SYoshinobu Inoue 			*tlenp -= i;
336fb59c426SYoshinobu Inoue 			th->th_seq += i;
337df8bae1dSRodney W. Grimes 		}
338df8bae1dSRodney W. Grimes 	}
339df8bae1dSRodney W. Grimes 	tcpstat.tcps_rcvoopack++;
340fb59c426SYoshinobu Inoue 	tcpstat.tcps_rcvoobyte += *tlenp;
341df8bae1dSRodney W. Grimes 
342df8bae1dSRodney W. Grimes 	/*
343df8bae1dSRodney W. Grimes 	 * While we overlap succeeding segments trim them or,
344df8bae1dSRodney W. Grimes 	 * if they are completely covered, dequeue them.
345df8bae1dSRodney W. Grimes 	 */
3466effc713SDoug Rabson 	while (q) {
347fb59c426SYoshinobu Inoue 		register int i = (th->th_seq + *tlenp) - q->tqe_th->th_seq;
348df8bae1dSRodney W. Grimes 		if (i <= 0)
349df8bae1dSRodney W. Grimes 			break;
350fb59c426SYoshinobu Inoue 		if (i < q->tqe_len) {
351fb59c426SYoshinobu Inoue 			q->tqe_th->th_seq += i;
352fb59c426SYoshinobu Inoue 			q->tqe_len -= i;
353fb59c426SYoshinobu Inoue 			m_adj(q->tqe_m, i);
354df8bae1dSRodney W. Grimes 			break;
355df8bae1dSRodney W. Grimes 		}
3566effc713SDoug Rabson 
357fb59c426SYoshinobu Inoue 		nq = LIST_NEXT(q, tqe_q);
358fb59c426SYoshinobu Inoue 		LIST_REMOVE(q, tqe_q);
359fb59c426SYoshinobu Inoue 		m_freem(q->tqe_m);
36012e2e970SAndre Oppermann 		uma_zfree(tcp_reass_zone, q);
36112e2e970SAndre Oppermann 		tp->t_segqlen--;
36212e2e970SAndre Oppermann 		tcp_reass_qsize--;
3636effc713SDoug Rabson 		q = nq;
364df8bae1dSRodney W. Grimes 	}
365df8bae1dSRodney W. Grimes 
366fb59c426SYoshinobu Inoue 	/* Insert the new segment queue entry into place. */
367fb59c426SYoshinobu Inoue 	te->tqe_m = m;
368fb59c426SYoshinobu Inoue 	te->tqe_th = th;
369fb59c426SYoshinobu Inoue 	te->tqe_len = *tlenp;
370fb59c426SYoshinobu Inoue 
3716effc713SDoug Rabson 	if (p == NULL) {
372fb59c426SYoshinobu Inoue 		LIST_INSERT_HEAD(&tp->t_segq, te, tqe_q);
3736effc713SDoug Rabson 	} else {
374fb59c426SYoshinobu Inoue 		LIST_INSERT_AFTER(p, te, tqe_q);
3756effc713SDoug Rabson 	}
376df8bae1dSRodney W. Grimes 
377df8bae1dSRodney W. Grimes present:
378df8bae1dSRodney W. Grimes 	/*
379df8bae1dSRodney W. Grimes 	 * Present data to user, advancing rcv_nxt through
380df8bae1dSRodney W. Grimes 	 * completed sequence space.
381df8bae1dSRodney W. Grimes 	 */
382a0292f23SGarrett Wollman 	if (!TCPS_HAVEESTABLISHED(tp->t_state))
383df8bae1dSRodney W. Grimes 		return (0);
384fb59c426SYoshinobu Inoue 	q = LIST_FIRST(&tp->t_segq);
385fb59c426SYoshinobu Inoue 	if (!q || q->tqe_th->th_seq != tp->rcv_nxt)
386df8bae1dSRodney W. Grimes 		return (0);
3871e4d7da7SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
388df8bae1dSRodney W. Grimes 	do {
389fb59c426SYoshinobu Inoue 		tp->rcv_nxt += q->tqe_len;
390fb59c426SYoshinobu Inoue 		flags = q->tqe_th->th_flags & TH_FIN;
391fb59c426SYoshinobu Inoue 		nq = LIST_NEXT(q, tqe_q);
392fb59c426SYoshinobu Inoue 		LIST_REMOVE(q, tqe_q);
393c0b99ffaSRobert Watson 		if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
394fb59c426SYoshinobu Inoue 			m_freem(q->tqe_m);
3954cc20ab1SSeigo Tanimura 		else
3961e4d7da7SRobert Watson 			sbappendstream_locked(&so->so_rcv, q->tqe_m);
39712e2e970SAndre Oppermann 		uma_zfree(tcp_reass_zone, q);
39812e2e970SAndre Oppermann 		tp->t_segqlen--;
39912e2e970SAndre Oppermann 		tcp_reass_qsize--;
4006effc713SDoug Rabson 		q = nq;
401fb59c426SYoshinobu Inoue 	} while (q && q->tqe_th->th_seq == tp->rcv_nxt);
402fb59c426SYoshinobu Inoue 	ND6_HINT(tp);
4031e4d7da7SRobert Watson 	sorwakeup_locked(so);
404df8bae1dSRodney W. Grimes 	return (flags);
405df8bae1dSRodney W. Grimes }
406df8bae1dSRodney W. Grimes 
407df8bae1dSRodney W. Grimes /*
408df8bae1dSRodney W. Grimes  * TCP input routine, follows pages 65-76 of the
409df8bae1dSRodney W. Grimes  * protocol specification dated September, 1981 very closely.
410df8bae1dSRodney W. Grimes  */
411fb59c426SYoshinobu Inoue #ifdef INET6
412fb59c426SYoshinobu Inoue int
413fb59c426SYoshinobu Inoue tcp6_input(mp, offp, proto)
414fb59c426SYoshinobu Inoue 	struct mbuf **mp;
415fb59c426SYoshinobu Inoue 	int *offp, proto;
416fb59c426SYoshinobu Inoue {
417fb59c426SYoshinobu Inoue 	register struct mbuf *m = *mp;
41833841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6;
419fb59c426SYoshinobu Inoue 
420fb59c426SYoshinobu Inoue 	IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE);
421fb59c426SYoshinobu Inoue 
422fb59c426SYoshinobu Inoue 	/*
423fb59c426SYoshinobu Inoue 	 * draft-itojun-ipv6-tcp-to-anycast
424fb59c426SYoshinobu Inoue 	 * better place to put this in?
425fb59c426SYoshinobu Inoue 	 */
42633841545SHajimu UMEMOTO 	ia6 = ip6_getdstifaddr(m);
42733841545SHajimu UMEMOTO 	if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
428fb59c426SYoshinobu Inoue 		struct ip6_hdr *ip6;
429fb59c426SYoshinobu Inoue 
430fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
431fb59c426SYoshinobu Inoue 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
432fb59c426SYoshinobu Inoue 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
433fb59c426SYoshinobu Inoue 		return IPPROTO_DONE;
434fb59c426SYoshinobu Inoue 	}
435fb59c426SYoshinobu Inoue 
436f0ffb944SJulian Elischer 	tcp_input(m, *offp);
437fb59c426SYoshinobu Inoue 	return IPPROTO_DONE;
438fb59c426SYoshinobu Inoue }
439fb59c426SYoshinobu Inoue #endif
440fb59c426SYoshinobu Inoue 
441df8bae1dSRodney W. Grimes void
442f0ffb944SJulian Elischer tcp_input(m, off0)
443df8bae1dSRodney W. Grimes 	register struct mbuf *m;
444f0ffb944SJulian Elischer 	int off0;
445df8bae1dSRodney W. Grimes {
446fb59c426SYoshinobu Inoue 	register struct tcphdr *th;
447fb59c426SYoshinobu Inoue 	register struct ip *ip = NULL;
448fb59c426SYoshinobu Inoue 	register struct ipovly *ipov;
449f76fcf6dSJeffrey Hsu 	register struct inpcb *inp = NULL;
450e79adb8eSGarrett Wollman 	u_char *optp = NULL;
45126f9a767SRodney W. Grimes 	int optlen = 0;
452a0194ef1SBruce M Simpson 	int len, tlen, off;
453fb59c426SYoshinobu Inoue 	int drop_hdrlen;
454df8bae1dSRodney W. Grimes 	register struct tcpcb *tp = 0;
455fb59c426SYoshinobu Inoue 	register int thflags;
45626f9a767SRodney W. Grimes 	struct socket *so = 0;
457df8bae1dSRodney W. Grimes 	int todrop, acked, ourfinisacked, needoutput = 0;
458a0292f23SGarrett Wollman 	u_long tiwin;
459a0292f23SGarrett Wollman 	struct tcpopt to;		/* options in this segment */
460f76fcf6dSJeffrey Hsu 	int headlocked = 0;
4619b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
4629b932e9eSAndre Oppermann 	struct m_tag *fwd_tag;
4639b932e9eSAndre Oppermann #endif
464c068736aSJeffrey Hsu 	int rstreason; /* For badport_bandlim accounting purposes */
465c068736aSJeffrey Hsu 
466c068736aSJeffrey Hsu 	struct ip6_hdr *ip6 = NULL;
467c068736aSJeffrey Hsu #ifdef INET6
468c068736aSJeffrey Hsu 	int isipv6;
4691d54aa3bSBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
470c068736aSJeffrey Hsu #else
471c068736aSJeffrey Hsu 	const int isipv6 = 0;
472c068736aSJeffrey Hsu #endif
473f76fcf6dSJeffrey Hsu 
474610ee2f9SDavid Greenman #ifdef TCPDEBUG
475c068736aSJeffrey Hsu 	/*
476c068736aSJeffrey Hsu 	 * The size of tcp_saveipgen must be the size of the max ip header,
477c068736aSJeffrey Hsu 	 * now IPv6.
478c068736aSJeffrey Hsu 	 */
479410bb1bfSLuigi Rizzo 	u_char tcp_saveipgen[40];
480410bb1bfSLuigi Rizzo 	struct tcphdr tcp_savetcp;
481610ee2f9SDavid Greenman 	short ostate = 0;
482610ee2f9SDavid Greenman #endif
483c068736aSJeffrey Hsu 
484fb59c426SYoshinobu Inoue #ifdef INET6
485fb59c426SYoshinobu Inoue 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
486fb59c426SYoshinobu Inoue #endif
487a0292f23SGarrett Wollman 	bzero((char *)&to, sizeof(to));
488a0292f23SGarrett Wollman 
489df8bae1dSRodney W. Grimes 	tcpstat.tcps_rcvtotal++;
490fb59c426SYoshinobu Inoue 
491fb59c426SYoshinobu Inoue 	if (isipv6) {
49204d3a452SHajimu UMEMOTO #ifdef INET6
493fb59c426SYoshinobu Inoue 		/* IP6_EXTHDR_CHECK() is already done at tcp6_input() */
494fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
495fb59c426SYoshinobu Inoue 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
496fb59c426SYoshinobu Inoue 		if (in6_cksum(m, IPPROTO_TCP, off0, tlen)) {
497fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbadsum++;
498fb59c426SYoshinobu Inoue 			goto drop;
499fb59c426SYoshinobu Inoue 		}
500fb59c426SYoshinobu Inoue 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
50133841545SHajimu UMEMOTO 
50233841545SHajimu UMEMOTO 		/*
50333841545SHajimu UMEMOTO 		 * Be proactive about unspecified IPv6 address in source.
50433841545SHajimu UMEMOTO 		 * As we use all-zero to indicate unbounded/unconnected pcb,
50533841545SHajimu UMEMOTO 		 * unspecified IPv6 address can be used to confuse us.
50633841545SHajimu UMEMOTO 		 *
50733841545SHajimu UMEMOTO 		 * Note that packets with unspecified IPv6 destination is
50833841545SHajimu UMEMOTO 		 * already dropped in ip6_input.
50933841545SHajimu UMEMOTO 		 */
51033841545SHajimu UMEMOTO 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
51133841545SHajimu UMEMOTO 			/* XXX stat */
51233841545SHajimu UMEMOTO 			goto drop;
51333841545SHajimu UMEMOTO 		}
51404d3a452SHajimu UMEMOTO #else
51504d3a452SHajimu UMEMOTO 		th = NULL;		/* XXX: avoid compiler warning */
51604d3a452SHajimu UMEMOTO #endif
517c068736aSJeffrey Hsu 	} else {
518df8bae1dSRodney W. Grimes 		/*
519df8bae1dSRodney W. Grimes 		 * Get IP and TCP header together in first mbuf.
520df8bae1dSRodney W. Grimes 		 * Note: IP leaves IP header in first mbuf.
521df8bae1dSRodney W. Grimes 		 */
522fb59c426SYoshinobu Inoue 		if (off0 > sizeof (struct ip)) {
523df8bae1dSRodney W. Grimes 			ip_stripoptions(m, (struct mbuf *)0);
524fb59c426SYoshinobu Inoue 			off0 = sizeof(struct ip);
525fb59c426SYoshinobu Inoue 		}
526df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof (struct tcpiphdr)) {
527df8bae1dSRodney W. Grimes 			if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
528df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvshort++;
529df8bae1dSRodney W. Grimes 				return;
530df8bae1dSRodney W. Grimes 			}
531df8bae1dSRodney W. Grimes 		}
532fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
533fb59c426SYoshinobu Inoue 		ipov = (struct ipovly *)ip;
534db4f9cc7SJonathan Lemon 		th = (struct tcphdr *)((caddr_t)ip + off0);
535db4f9cc7SJonathan Lemon 		tlen = ip->ip_len;
536df8bae1dSRodney W. Grimes 
537db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
538db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
539db4f9cc7SJonathan Lemon 				th->th_sum = m->m_pkthdr.csum_data;
540db4f9cc7SJonathan Lemon 			else
541db4f9cc7SJonathan Lemon 				th->th_sum = in_pseudo(ip->ip_src.s_addr,
542c068736aSJeffrey Hsu 						ip->ip_dst.s_addr,
543c068736aSJeffrey Hsu 						htonl(m->m_pkthdr.csum_data +
544c068736aSJeffrey Hsu 							ip->ip_len +
545c068736aSJeffrey Hsu 							IPPROTO_TCP));
546db4f9cc7SJonathan Lemon 			th->th_sum ^= 0xffff;
5473c653157SHartmut Brandt #ifdef TCPDEBUG
5483c653157SHartmut Brandt 			ipov->ih_len = (u_short)tlen;
5493c653157SHartmut Brandt 			ipov->ih_len = htons(ipov->ih_len);
5503c653157SHartmut Brandt #endif
551db4f9cc7SJonathan Lemon 		} else {
552df8bae1dSRodney W. Grimes 			/*
553df8bae1dSRodney W. Grimes 			 * Checksum extended TCP header and data.
554df8bae1dSRodney W. Grimes 			 */
555df8bae1dSRodney W. Grimes 			len = sizeof (struct ip) + tlen;
556fb59c426SYoshinobu Inoue 			bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
557fb59c426SYoshinobu Inoue 			ipov->ih_len = (u_short)tlen;
558fd8e4ebcSMike Barcroft 			ipov->ih_len = htons(ipov->ih_len);
559fb59c426SYoshinobu Inoue 			th->th_sum = in_cksum(m, len);
560db4f9cc7SJonathan Lemon 		}
561fb59c426SYoshinobu Inoue 		if (th->th_sum) {
562df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbadsum++;
563df8bae1dSRodney W. Grimes 			goto drop;
564df8bae1dSRodney W. Grimes 		}
565fb59c426SYoshinobu Inoue 		/* Re-initialization for later version check */
566fb59c426SYoshinobu Inoue 		ip->ip_v = IPVERSION;
567fb59c426SYoshinobu Inoue 	}
568df8bae1dSRodney W. Grimes 
569df8bae1dSRodney W. Grimes 	/*
570df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
571df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
572df8bae1dSRodney W. Grimes 	 */
573fb59c426SYoshinobu Inoue 	off = th->th_off << 2;
574df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
575df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvbadoff++;
576df8bae1dSRodney W. Grimes 		goto drop;
577df8bae1dSRodney W. Grimes 	}
578fb59c426SYoshinobu Inoue 	tlen -= off;	/* tlen is used instead of ti->ti_len */
579df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
580fb59c426SYoshinobu Inoue 		if (isipv6) {
58104d3a452SHajimu UMEMOTO #ifdef INET6
582fb59c426SYoshinobu Inoue 			IP6_EXTHDR_CHECK(m, off0, off, );
583fb59c426SYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
584fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
58504d3a452SHajimu UMEMOTO #endif
586c068736aSJeffrey Hsu 		} else {
587df8bae1dSRodney W. Grimes 			if (m->m_len < sizeof(struct ip) + off) {
588c068736aSJeffrey Hsu 				if ((m = m_pullup(m, sizeof (struct ip) + off))
589c068736aSJeffrey Hsu 						== 0) {
590df8bae1dSRodney W. Grimes 					tcpstat.tcps_rcvshort++;
591df8bae1dSRodney W. Grimes 					return;
592df8bae1dSRodney W. Grimes 				}
593fb59c426SYoshinobu Inoue 				ip = mtod(m, struct ip *);
594fb59c426SYoshinobu Inoue 				ipov = (struct ipovly *)ip;
595fb59c426SYoshinobu Inoue 				th = (struct tcphdr *)((caddr_t)ip + off0);
596fb59c426SYoshinobu Inoue 			}
597df8bae1dSRodney W. Grimes 		}
598df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
599fb59c426SYoshinobu Inoue 		optp = (u_char *)(th + 1);
600df8bae1dSRodney W. Grimes 	}
601fb59c426SYoshinobu Inoue 	thflags = th->th_flags;
602df8bae1dSRodney W. Grimes 
603e46cd3d4SDag-Erling Smørgrav #ifdef TCP_DROP_SYNFIN
604e46cd3d4SDag-Erling Smørgrav 	/*
605e46cd3d4SDag-Erling Smørgrav 	 * If the drop_synfin option is enabled, drop all packets with
606e46cd3d4SDag-Erling Smørgrav 	 * both the SYN and FIN bits set. This prevents e.g. nmap from
607e46cd3d4SDag-Erling Smørgrav 	 * identifying the TCP/IP stack.
608e46cd3d4SDag-Erling Smørgrav 	 *
609a589a70eSGarrett Wollman 	 * This is a violation of the TCP specification.
610e46cd3d4SDag-Erling Smørgrav 	 */
611fb59c426SYoshinobu Inoue 	if (drop_synfin && (thflags & (TH_SYN|TH_FIN)) == (TH_SYN|TH_FIN))
612e46cd3d4SDag-Erling Smørgrav 		goto drop;
613e46cd3d4SDag-Erling Smørgrav #endif
614e46cd3d4SDag-Erling Smørgrav 
615df8bae1dSRodney W. Grimes 	/*
616df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
617df8bae1dSRodney W. Grimes 	 */
618fd8e4ebcSMike Barcroft 	th->th_seq = ntohl(th->th_seq);
619fd8e4ebcSMike Barcroft 	th->th_ack = ntohl(th->th_ack);
620fd8e4ebcSMike Barcroft 	th->th_win = ntohs(th->th_win);
621fd8e4ebcSMike Barcroft 	th->th_urp = ntohs(th->th_urp);
622df8bae1dSRodney W. Grimes 
623df8bae1dSRodney W. Grimes 	/*
6243bfd6421SJonathan Lemon 	 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options,
625fb59c426SYoshinobu Inoue 	 * until after ip6_savecontrol() is called and before other functions
626fb59c426SYoshinobu Inoue 	 * which don't want those proto headers.
627fb59c426SYoshinobu Inoue 	 * Because ip6_savecontrol() is going to parse the mbuf to
628fb59c426SYoshinobu Inoue 	 * search for data to be passed up to user-land, it wants mbuf
629fb59c426SYoshinobu Inoue 	 * parameters to be unchanged.
63088ff5695SSUZUKI Shinsuke 	 * XXX: the call of ip6_savecontrol() has been obsoleted based on
63188ff5695SSUZUKI Shinsuke 	 * latest version of the advanced API (20020110).
632755c1f07SAndras Olah 	 */
633fb59c426SYoshinobu Inoue 	drop_hdrlen = off0 + off;
634755c1f07SAndras Olah 
635755c1f07SAndras Olah 	/*
636df8bae1dSRodney W. Grimes 	 * Locate pcb for segment.
637df8bae1dSRodney W. Grimes 	 */
638f76fcf6dSJeffrey Hsu 	INP_INFO_WLOCK(&tcbinfo);
639f76fcf6dSJeffrey Hsu 	headlocked = 1;
640df8bae1dSRodney W. Grimes findpcb:
641de30ea13SRobert Watson 	KASSERT(headlocked, ("tcp_input: findpcb: head not locked"));
6429b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
6439b932e9eSAndre Oppermann 	/* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */
6449b932e9eSAndre Oppermann 	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
6459b932e9eSAndre Oppermann 
6469b932e9eSAndre Oppermann 	if (fwd_tag != NULL && isipv6 == 0) {	/* IPv6 support is not yet */
6479b932e9eSAndre Oppermann 		struct sockaddr_in *next_hop;
6489b932e9eSAndre Oppermann 
6499b932e9eSAndre Oppermann 		next_hop = (struct sockaddr_in *)(fwd_tag+1);
650f9e354dfSJulian Elischer 		/*
6512b25acc1SLuigi Rizzo 		 * Transparently forwarded. Pretend to be the destination.
652f9e354dfSJulian Elischer 		 * already got one like this?
653f9e354dfSJulian Elischer 		 */
6549b932e9eSAndre Oppermann 		inp = in_pcblookup_hash(&tcbinfo,
6559b932e9eSAndre Oppermann 					ip->ip_src, th->th_sport,
656c068736aSJeffrey Hsu 					ip->ip_dst, th->th_dport,
657c068736aSJeffrey Hsu 					0, m->m_pkthdr.rcvif);
658f9e354dfSJulian Elischer 		if (!inp) {
6599b932e9eSAndre Oppermann 			/* It's new.  Try to find the ambushing socket. */
660f9e354dfSJulian Elischer 			inp = in_pcblookup_hash(&tcbinfo,
661fb59c426SYoshinobu Inoue 						ip->ip_src, th->th_sport,
6622b25acc1SLuigi Rizzo 						next_hop->sin_addr,
663c068736aSJeffrey Hsu 						next_hop->sin_port ?
664c068736aSJeffrey Hsu 						    ntohs(next_hop->sin_port) :
665c068736aSJeffrey Hsu 						    th->th_dport,
666421d8aa6SBjoern A. Zeeb 						INPLOOKUP_WILDCARD,
667421d8aa6SBjoern A. Zeeb 						m->m_pkthdr.rcvif);
668f9e354dfSJulian Elischer 		}
6699b932e9eSAndre Oppermann 		/* Remove the tag from the packet.  We don't need it anymore. */
6709b932e9eSAndre Oppermann 		m_tag_delete(m, fwd_tag);
671c068736aSJeffrey Hsu 	} else {
6729b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */
67304d3a452SHajimu UMEMOTO 		if (isipv6) {
67404d3a452SHajimu UMEMOTO #ifdef INET6
675c068736aSJeffrey Hsu 			inp = in6_pcblookup_hash(&tcbinfo,
676c068736aSJeffrey Hsu 						 &ip6->ip6_src, th->th_sport,
677c068736aSJeffrey Hsu 						 &ip6->ip6_dst, th->th_dport,
678421d8aa6SBjoern A. Zeeb 						 INPLOOKUP_WILDCARD,
679421d8aa6SBjoern A. Zeeb 						 m->m_pkthdr.rcvif);
68004d3a452SHajimu UMEMOTO #endif
68104d3a452SHajimu UMEMOTO 		} else
682c068736aSJeffrey Hsu 			inp = in_pcblookup_hash(&tcbinfo,
683c068736aSJeffrey Hsu 						ip->ip_src, th->th_sport,
684c068736aSJeffrey Hsu 						ip->ip_dst, th->th_dport,
685421d8aa6SBjoern A. Zeeb 						INPLOOKUP_WILDCARD,
686421d8aa6SBjoern A. Zeeb 						m->m_pkthdr.rcvif);
6879b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
688fb59c426SYoshinobu Inoue 	}
6899b932e9eSAndre Oppermann #endif /* IPFIREWALL_FORWARD */
690f9e354dfSJulian Elischer 
691da0f4099SHajimu UMEMOTO #if defined(IPSEC) || defined(FAST_IPSEC)
692d420fcdaSBruce M Simpson #ifdef INET6
693da0f4099SHajimu UMEMOTO 	if (isipv6) {
694da0f4099SHajimu UMEMOTO 		if (inp != NULL && ipsec6_in_reject(m, inp)) {
695fb59c426SYoshinobu Inoue #ifdef IPSEC
696fb59c426SYoshinobu Inoue 			ipsec6stat.in_polvio++;
697d420fcdaSBruce M Simpson #endif
698fb59c426SYoshinobu Inoue 			goto drop;
699fb59c426SYoshinobu Inoue 		}
700d420fcdaSBruce M Simpson 	} else
701d420fcdaSBruce M Simpson #endif /* INET6 */
702d420fcdaSBruce M Simpson 	if (inp != NULL && ipsec4_in_reject(m, inp)) {
703da0f4099SHajimu UMEMOTO #ifdef IPSEC
704fb59c426SYoshinobu Inoue 		ipsecstat.in_polvio++;
705d420fcdaSBruce M Simpson #endif
706fb59c426SYoshinobu Inoue 		goto drop;
707fb59c426SYoshinobu Inoue 	}
708da0f4099SHajimu UMEMOTO #endif /*IPSEC || FAST_IPSEC*/
709df8bae1dSRodney W. Grimes 
710df8bae1dSRodney W. Grimes 	/*
711df8bae1dSRodney W. Grimes 	 * If the state is CLOSED (i.e., TCB does not exist) then
712df8bae1dSRodney W. Grimes 	 * all data in the incoming segment is discarded.
713df8bae1dSRodney W. Grimes 	 * If the TCB exists but is in CLOSED state, it is embryonic,
714df8bae1dSRodney W. Grimes 	 * but should either do a listen or a connect soon.
715df8bae1dSRodney W. Grimes 	 */
716816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
7172e4e1b4cSGeoff Rehmet 		if (log_in_vain) {
718fb59c426SYoshinobu Inoue #ifdef INET6
719ded7008aSJuli Mallett 			char dbuf[INET6_ADDRSTRLEN+2], sbuf[INET6_ADDRSTRLEN+2];
720c068736aSJeffrey Hsu #else
721fb59c426SYoshinobu Inoue 			char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"];
722c068736aSJeffrey Hsu #endif
72375cfc95fSAndrey A. Chernov 
724fb59c426SYoshinobu Inoue 			if (isipv6) {
72504d3a452SHajimu UMEMOTO #ifdef INET6
726ded7008aSJuli Mallett 				strcpy(dbuf, "[");
727ded7008aSJuli Mallett 				strcpy(sbuf, "[");
7281d54aa3bSBjoern A. Zeeb 				strcat(dbuf,
7291d54aa3bSBjoern A. Zeeb 				    ip6_sprintf(ip6buf, &ip6->ip6_dst));
7301d54aa3bSBjoern A. Zeeb 				strcat(sbuf,
7311d54aa3bSBjoern A. Zeeb 				    ip6_sprintf(ip6buf, &ip6->ip6_src));
732ded7008aSJuli Mallett 				strcat(dbuf, "]");
733ded7008aSJuli Mallett 				strcat(sbuf, "]");
73404d3a452SHajimu UMEMOTO #endif
735c068736aSJeffrey Hsu 			} else {
736fb59c426SYoshinobu Inoue 				strcpy(dbuf, inet_ntoa(ip->ip_dst));
737fb59c426SYoshinobu Inoue 				strcpy(sbuf, inet_ntoa(ip->ip_src));
738fb59c426SYoshinobu Inoue 			}
7392e4e1b4cSGeoff Rehmet 			switch (log_in_vain) {
7402e4e1b4cSGeoff Rehmet 			case 1:
74139eb27a4SCrist J. Clark 				if ((thflags & TH_SYN) == 0)
7422e4e1b4cSGeoff Rehmet 					break;
743e4d2978dSPoul-Henning Kamp 				/* FALLTHROUGH */
7442e4e1b4cSGeoff Rehmet 			case 2:
7452e4e1b4cSGeoff Rehmet 				log(LOG_INFO,
746c068736aSJeffrey Hsu 				    "Connection attempt to TCP %s:%d "
74739eb27a4SCrist J. Clark 				    "from %s:%d flags:0x%02x\n",
748fb59c426SYoshinobu Inoue 				    dbuf, ntohs(th->th_dport), sbuf,
749fb59c426SYoshinobu Inoue 				    ntohs(th->th_sport), thflags);
7502e4e1b4cSGeoff Rehmet 				break;
7512e4e1b4cSGeoff Rehmet 			default:
7522e4e1b4cSGeoff Rehmet 				break;
7532e4e1b4cSGeoff Rehmet 			}
75475cfc95fSAndrey A. Chernov 		}
7552e4e1b4cSGeoff Rehmet 		if (blackhole) {
7562e4e1b4cSGeoff Rehmet 			switch (blackhole) {
757828b7f40SGeoff Rehmet 			case 1:
758fb59c426SYoshinobu Inoue 				if (thflags & TH_SYN)
759828b7f40SGeoff Rehmet 					goto drop;
760828b7f40SGeoff Rehmet 				break;
761828b7f40SGeoff Rehmet 			case 2:
762828b7f40SGeoff Rehmet 				goto drop;
763828b7f40SGeoff Rehmet 			default:
764828b7f40SGeoff Rehmet 				goto drop;
7652e4e1b4cSGeoff Rehmet 			}
766828b7f40SGeoff Rehmet 		}
767a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
768a57815efSBosko Milekic 		goto dropwithreset;
769816a3d83SPoul-Henning Kamp 	}
770f76fcf6dSJeffrey Hsu 	INP_LOCK(inp);
771936cd18dSAndre Oppermann 
772936cd18dSAndre Oppermann 	/* Check the minimum TTL for socket. */
77334f83c52SGeorge V. Neville-Neil 	if (inp->inp_ip_minttl != 0) {
77434f83c52SGeorge V. Neville-Neil #ifdef INET6
77534f83c52SGeorge V. Neville-Neil 		if (isipv6 && inp->inp_ip_minttl > ip6->ip6_hlim)
776936cd18dSAndre Oppermann 			goto drop;
77702707462SAndre Oppermann 		else
77834f83c52SGeorge V. Neville-Neil #endif
77934f83c52SGeorge V. Neville-Neil 		if (inp->inp_ip_minttl > ip->ip_ttl)
78034f83c52SGeorge V. Neville-Neil 			goto drop;
78134f83c52SGeorge V. Neville-Neil 	}
782936cd18dSAndre Oppermann 
783340c35deSJonathan Lemon 	if (inp->inp_vflag & INP_TIMEWAIT) {
784340c35deSJonathan Lemon 		/*
785340c35deSJonathan Lemon 		 * The only option of relevance is TOF_CC, and only if
786340c35deSJonathan Lemon 		 * present in a SYN segment.  See tcp_timewait().
787340c35deSJonathan Lemon 		 */
788340c35deSJonathan Lemon 		if (thflags & TH_SYN)
789f72167f4SAndre Oppermann 			tcp_dooptions(&to, optp, optlen, TO_SYN);
7903cbe7fafSRobert Watson 		if (tcp_timewait(inp, &to, th, m, tlen))
791340c35deSJonathan Lemon 			goto findpcb;
792340c35deSJonathan Lemon 		/*
793340c35deSJonathan Lemon 		 * tcp_timewait unlocks inp.
794340c35deSJonathan Lemon 		 */
795340c35deSJonathan Lemon 		INP_INFO_WUNLOCK(&tcbinfo);
796340c35deSJonathan Lemon 		return;
797340c35deSJonathan Lemon 	}
798df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
79909f81a46SBosko Milekic 	if (tp == 0) {
800f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
801a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
802a57815efSBosko Milekic 		goto dropwithreset;
80309f81a46SBosko Milekic 	}
804df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_CLOSED)
805df8bae1dSRodney W. Grimes 		goto drop;
806df8bae1dSRodney W. Grimes 
807c488362eSRobert Watson #ifdef MAC
8081f82efb3SRobert Watson 	INP_LOCK_ASSERT(inp);
809a557af22SRobert Watson 	if (mac_check_inpcb_deliver(inp, m))
810c488362eSRobert Watson 		goto drop;
811c488362eSRobert Watson #endif
812a557af22SRobert Watson 	so = inp->inp_socket;
8131c53f806SRobert Watson 	KASSERT(so != NULL, ("tcp_input: so == NULL"));
814610ee2f9SDavid Greenman #ifdef TCPDEBUG
815df8bae1dSRodney W. Grimes 	if (so->so_options & SO_DEBUG) {
816df8bae1dSRodney W. Grimes 		ostate = tp->t_state;
817fb59c426SYoshinobu Inoue 		if (isipv6)
818540e8b7eSJeffrey Hsu 			bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6));
819fb59c426SYoshinobu Inoue 		else
820540e8b7eSJeffrey Hsu 			bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
821fb59c426SYoshinobu Inoue 		tcp_savetcp = *th;
822df8bae1dSRodney W. Grimes 	}
823610ee2f9SDavid Greenman #endif
824540e8b7eSJeffrey Hsu 	if (so->so_options & SO_ACCEPTCONN) {
825540e8b7eSJeffrey Hsu 		struct in_conninfo inc;
826540e8b7eSJeffrey Hsu 
8278bfb1918SAndre Oppermann 		bzero(&inc, sizeof(inc));
828fb59c426SYoshinobu Inoue #ifdef INET6
829be2ac88cSJonathan Lemon 		inc.inc_isipv6 = isipv6;
830c068736aSJeffrey Hsu #endif
831be2ac88cSJonathan Lemon 		if (isipv6) {
832be2ac88cSJonathan Lemon 			inc.inc6_faddr = ip6->ip6_src;
833be2ac88cSJonathan Lemon 			inc.inc6_laddr = ip6->ip6_dst;
834c068736aSJeffrey Hsu 		} else {
835be2ac88cSJonathan Lemon 			inc.inc_faddr = ip->ip_src;
836be2ac88cSJonathan Lemon 			inc.inc_laddr = ip->ip_dst;
837be2ac88cSJonathan Lemon 		}
838be2ac88cSJonathan Lemon 		inc.inc_fport = th->th_sport;
839be2ac88cSJonathan Lemon 		inc.inc_lport = th->th_dport;
840be2ac88cSJonathan Lemon 
841fb59c426SYoshinobu Inoue 	        /*
842be2ac88cSJonathan Lemon 	         * If the state is LISTEN then ignore segment if it contains
843be2ac88cSJonathan Lemon 		 * a RST.  If the segment contains an ACK then it is bad and
844be2ac88cSJonathan Lemon 		 * send a RST.  If it does not contain a SYN then it is not
845be2ac88cSJonathan Lemon 		 * interesting; drop it.
846be2ac88cSJonathan Lemon 		 *
847be2ac88cSJonathan Lemon 		 * If the state is SYN_RECEIVED (syncache) and seg contains
848be2ac88cSJonathan Lemon 		 * an ACK, but not for our SYN/ACK, send a RST.  If the seg
849be2ac88cSJonathan Lemon 		 * contains a RST, check the sequence number to see if it
850be2ac88cSJonathan Lemon 		 * is a valid reset segment.
851fb59c426SYoshinobu Inoue 		 */
852fb59c426SYoshinobu Inoue 		if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
853be2ac88cSJonathan Lemon 			if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
854bf6d304aSAndre Oppermann 				/*
855bf6d304aSAndre Oppermann 				 * Parse the TCP options here because
856bf6d304aSAndre Oppermann 				 * syncookies need access to the reflected
857bf6d304aSAndre Oppermann 				 * timestamp.
858bf6d304aSAndre Oppermann 				 */
859bf6d304aSAndre Oppermann 				tcp_dooptions(&to, optp, optlen, 0);
860bf6d304aSAndre Oppermann 				if (!syncache_expand(&inc, &to, th, &so, m)) {
8614195b4afSPaul Traina 					/*
862be2ac88cSJonathan Lemon 					 * No syncache entry, or ACK was not
863be2ac88cSJonathan Lemon 					 * for our SYN/ACK.  Send a RST.
8644195b4afSPaul Traina 					 */
865be2ac88cSJonathan Lemon 					tcpstat.tcps_badsyn++;
866be2ac88cSJonathan Lemon 					rstreason = BANDLIM_RST_OPENPORT;
867be2ac88cSJonathan Lemon 					goto dropwithreset;
868be2ac88cSJonathan Lemon 				}
869f76fcf6dSJeffrey Hsu 				if (so == NULL) {
870be2ac88cSJonathan Lemon 					/*
871be2ac88cSJonathan Lemon 					 * Could not complete 3-way handshake,
872be2ac88cSJonathan Lemon 					 * connection is being closed down, and
873464fcfbcSAndre Oppermann 					 * syncache has free'd mbuf.
874be2ac88cSJonathan Lemon 					 */
875f76fcf6dSJeffrey Hsu 					INP_UNLOCK(inp);
876f76fcf6dSJeffrey Hsu 					INP_INFO_WUNLOCK(&tcbinfo);
877be2ac88cSJonathan Lemon 					return;
878f76fcf6dSJeffrey Hsu 				}
879be2ac88cSJonathan Lemon 				/*
880be2ac88cSJonathan Lemon 				 * Socket is created in state SYN_RECEIVED.
881be2ac88cSJonathan Lemon 				 * Continue processing segment.
882be2ac88cSJonathan Lemon 				 */
883f76fcf6dSJeffrey Hsu 				INP_UNLOCK(inp);
884be2ac88cSJonathan Lemon 				inp = sotoinpcb(so);
885f76fcf6dSJeffrey Hsu 				INP_LOCK(inp);
886be2ac88cSJonathan Lemon 				tp = intotcpcb(inp);
887be2ac88cSJonathan Lemon 				/*
888be2ac88cSJonathan Lemon 				 * This is what would have happened in
889e6658b12SRobert Watson 				 * tcp_output() when the SYN,ACK was sent.
890be2ac88cSJonathan Lemon 				 */
891be2ac88cSJonathan Lemon 				tp->snd_up = tp->snd_una;
892be2ac88cSJonathan Lemon 				tp->snd_max = tp->snd_nxt = tp->iss + 1;
893be2ac88cSJonathan Lemon 				tp->last_ack_sent = tp->rcv_nxt;
894be2ac88cSJonathan Lemon 				goto after_listen;
895be2ac88cSJonathan Lemon 			}
896be2ac88cSJonathan Lemon 			if (thflags & TH_RST) {
897be2ac88cSJonathan Lemon 				syncache_chkrst(&inc, th);
898be2ac88cSJonathan Lemon 				goto drop;
899be2ac88cSJonathan Lemon 			}
900fb59c426SYoshinobu Inoue 			if (thflags & TH_ACK) {
901be2ac88cSJonathan Lemon 				syncache_badack(&inc);
902ebb0cbeaSPaul Traina 				tcpstat.tcps_badsyn++;
903a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
904a57815efSBosko Milekic 				goto dropwithreset;
9054195b4afSPaul Traina 			}
906ebb0cbeaSPaul Traina 			goto drop;
907ebb0cbeaSPaul Traina 		}
90833841545SHajimu UMEMOTO 
909be2ac88cSJonathan Lemon 		/*
910be2ac88cSJonathan Lemon 		 * Segment's flags are (SYN) or (SYN|FIN).
911be2ac88cSJonathan Lemon 		 */
91233841545SHajimu UMEMOTO #ifdef INET6
91333841545SHajimu UMEMOTO 		/*
91433841545SHajimu UMEMOTO 		 * If deprecated address is forbidden,
91533841545SHajimu UMEMOTO 		 * we do not accept SYN to deprecated interface
91633841545SHajimu UMEMOTO 		 * address to prevent any new inbound connection from
91733841545SHajimu UMEMOTO 		 * getting established.
91833841545SHajimu UMEMOTO 		 * When we do not accept SYN, we send a TCP RST,
91933841545SHajimu UMEMOTO 		 * with deprecated source address (instead of dropping
92033841545SHajimu UMEMOTO 		 * it).  We compromise it as it is much better for peer
92133841545SHajimu UMEMOTO 		 * to send a RST, and RST will be the final packet
92233841545SHajimu UMEMOTO 		 * for the exchange.
92333841545SHajimu UMEMOTO 		 *
92433841545SHajimu UMEMOTO 		 * If we do not forbid deprecated addresses, we accept
92533841545SHajimu UMEMOTO 		 * the SYN packet.  RFC2462 does not suggest dropping
92633841545SHajimu UMEMOTO 		 * SYN in this case.
92733841545SHajimu UMEMOTO 		 * If we decipher RFC2462 5.5.4, it says like this:
92833841545SHajimu UMEMOTO 		 * 1. use of deprecated addr with existing
92933841545SHajimu UMEMOTO 		 *    communication is okay - "SHOULD continue to be
93033841545SHajimu UMEMOTO 		 *    used"
93133841545SHajimu UMEMOTO 		 * 2. use of it with new communication:
93233841545SHajimu UMEMOTO 		 *   (2a) "SHOULD NOT be used if alternate address
93333841545SHajimu UMEMOTO 		 *        with sufficient scope is available"
93433841545SHajimu UMEMOTO 		 *   (2b) nothing mentioned otherwise.
93533841545SHajimu UMEMOTO 		 * Here we fall into (2b) case as we have no choice in
93633841545SHajimu UMEMOTO 		 * our source address selection - we must obey the peer.
93733841545SHajimu UMEMOTO 		 *
93833841545SHajimu UMEMOTO 		 * The wording in RFC2462 is confusing, and there are
93933841545SHajimu UMEMOTO 		 * multiple description text for deprecated address
94033841545SHajimu UMEMOTO 		 * handling - worse, they are not exactly the same.
94133841545SHajimu UMEMOTO 		 * I believe 5.5.4 is the best one, so we follow 5.5.4.
94233841545SHajimu UMEMOTO 		 */
94333841545SHajimu UMEMOTO 		if (isipv6 && !ip6_use_deprecated) {
94433841545SHajimu UMEMOTO 			struct in6_ifaddr *ia6;
94533841545SHajimu UMEMOTO 
94633841545SHajimu UMEMOTO 			if ((ia6 = ip6_getdstifaddr(m)) &&
94733841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
948f76fcf6dSJeffrey Hsu 				INP_UNLOCK(inp);
94933841545SHajimu UMEMOTO 				tp = NULL;
95033841545SHajimu UMEMOTO 				rstreason = BANDLIM_RST_OPENPORT;
95133841545SHajimu UMEMOTO 				goto dropwithreset;
95233841545SHajimu UMEMOTO 			}
95333841545SHajimu UMEMOTO 		}
95433841545SHajimu UMEMOTO #endif
95565f28919SJesper Skriver 		/*
956be2ac88cSJonathan Lemon 		 * If it is from this socket, drop it, it must be forged.
957be2ac88cSJonathan Lemon 		 * Don't bother responding if the destination was a broadcast.
95865f28919SJesper Skriver 		 */
959be2ac88cSJonathan Lemon 		if (th->th_dport == th->th_sport) {
960fb59c426SYoshinobu Inoue 			if (isipv6) {
961be2ac88cSJonathan Lemon 				if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
962be2ac88cSJonathan Lemon 						       &ip6->ip6_src))
963be2ac88cSJonathan Lemon 					goto drop;
964c068736aSJeffrey Hsu 			} else {
965be2ac88cSJonathan Lemon 				if (ip->ip_dst.s_addr == ip->ip_src.s_addr)
966be2ac88cSJonathan Lemon 					goto drop;
967be2ac88cSJonathan Lemon 			}
968c068736aSJeffrey Hsu 		}
969be2ac88cSJonathan Lemon 		/*
970be2ac88cSJonathan Lemon 		 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
9712ca2159fSCrist J. Clark 		 *
97293ec91baSCrist J. Clark 		 * Note that it is quite possible to receive unicast
97393ec91baSCrist J. Clark 		 * link-layer packets with a broadcast IP address. Use
97493ec91baSCrist J. Clark 		 * in_broadcast() to find them.
975be2ac88cSJonathan Lemon 		 */
976be2ac88cSJonathan Lemon 		if (m->m_flags & (M_BCAST|M_MCAST))
977be2ac88cSJonathan Lemon 			goto drop;
978be2ac88cSJonathan Lemon 		if (isipv6) {
979be2ac88cSJonathan Lemon 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
980be2ac88cSJonathan Lemon 			    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
981be2ac88cSJonathan Lemon 				goto drop;
982c068736aSJeffrey Hsu 		} else {
983be2ac88cSJonathan Lemon 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
984be2ac88cSJonathan Lemon 			    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
9852ca2159fSCrist J. Clark 			    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
9862ca2159fSCrist J. Clark 			    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
987be2ac88cSJonathan Lemon 				goto drop;
988c068736aSJeffrey Hsu 		}
989be2ac88cSJonathan Lemon 		/*
990be2ac88cSJonathan Lemon 		 * SYN appears to be valid; create compressed TCP state
991be2ac88cSJonathan Lemon 		 * for syncache, or perform t/tcp connection.
992be2ac88cSJonathan Lemon 		 */
993be2ac88cSJonathan Lemon 		if (so->so_qlen <= so->so_qlimit) {
9943c653157SHartmut Brandt #ifdef TCPDEBUG
9953c653157SHartmut Brandt 			if (so->so_options & SO_DEBUG)
9963c653157SHartmut Brandt 				tcp_trace(TA_INPUT, ostate, tp,
9973c653157SHartmut Brandt 				    (void *)tcp_saveipgen, &tcp_savetcp, 0);
9983c653157SHartmut Brandt #endif
999f72167f4SAndre Oppermann 			tcp_dooptions(&to, optp, optlen, TO_SYN);
1000351630c4SAndre Oppermann 			if (!syncache_add(&inc, &to, th, inp, &so, m))
1001351630c4SAndre Oppermann 				goto drop;	/* XXX: does not happen */
1002f76fcf6dSJeffrey Hsu 			if (so == NULL) {
1003be2ac88cSJonathan Lemon 				/*
1004be2ac88cSJonathan Lemon 				 * Entry added to syncache, mbuf used to
1005351630c4SAndre Oppermann 				 * send SYN,ACK packet.  Everything unlocked
1006351630c4SAndre Oppermann 				 * already.
1007be2ac88cSJonathan Lemon 				 */
1008be2ac88cSJonathan Lemon 				return;
1009f76fcf6dSJeffrey Hsu 			}
1010351630c4SAndre Oppermann 			panic("T/TCP not supported at the moment");
1011351630c4SAndre Oppermann #if 0 /* T/TCP */
1012be2ac88cSJonathan Lemon 			/*
1013be2ac88cSJonathan Lemon 			 * Segment passed TAO tests.
1014464fcfbcSAndre Oppermann 			 * XXX: Can't happen at the moment.
1015be2ac88cSJonathan Lemon 			 */
1016f76fcf6dSJeffrey Hsu 			INP_UNLOCK(inp);
1017be2ac88cSJonathan Lemon 			inp = sotoinpcb(so);
1018f76fcf6dSJeffrey Hsu 			INP_LOCK(inp);
1019df8bae1dSRodney W. Grimes 			tp = intotcpcb(inp);
1020be2ac88cSJonathan Lemon 			tp->t_starttime = ticks;
1021be2ac88cSJonathan Lemon 			tp->t_state = TCPS_ESTABLISHED;
1022df8bae1dSRodney W. Grimes 
1023be2ac88cSJonathan Lemon 			/*
10243bfd6421SJonathan Lemon 			 * T/TCP logic:
10253bfd6421SJonathan Lemon 			 * If there is a FIN or if there is data, then
10263bfd6421SJonathan Lemon 			 * delay SYN,ACK(SYN) in the hope of piggy-backing
10273bfd6421SJonathan Lemon 			 * it on a response segment.  Otherwise must send
10283bfd6421SJonathan Lemon 			 * ACK now in case the other side is slow starting.
1029be2ac88cSJonathan Lemon 			 */
10303bfd6421SJonathan Lemon 			if (thflags & TH_FIN || tlen != 0)
10313bfd6421SJonathan Lemon 				tp->t_flags |= (TF_DELACK | TF_NEEDSYN);
10323bfd6421SJonathan Lemon 			else
1033f243998bSJonathan Lemon 				tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
1034464fcfbcSAndre Oppermann 			tiwin = th->th_win << tp->snd_scale;
1035be2ac88cSJonathan Lemon 			tcpstat.tcps_connects++;
1036be2ac88cSJonathan Lemon 			soisconnected(so);
1037be2ac88cSJonathan Lemon 			goto trimthenstep6;
1038351630c4SAndre Oppermann #endif	/* T/TCP */
1039df8bae1dSRodney W. Grimes 		}
1040be2ac88cSJonathan Lemon 		goto drop;
10414cc20ab1SSeigo Tanimura 	}
1042be2ac88cSJonathan Lemon after_listen:
1043de30ea13SRobert Watson 	KASSERT(headlocked, ("tcp_input: after_listen: head not locked"));
10447cfc6904SRobert Watson 	INP_LOCK_ASSERT(inp);
1045be2ac88cSJonathan Lemon 
1046a65e12b0SRobert Watson 	/* Syncache takes care of sockets in the listen state. */
1047a65e12b0SRobert Watson 	KASSERT(tp->t_state != TCPS_LISTEN, ("tcp_input: TCPS_LISTEN"));
1048df8bae1dSRodney W. Grimes 
1049df8bae1dSRodney W. Grimes 	/*
105053369ac9SAndre Oppermann 	 * This is the second part of the MSS DoS prevention code (after
105153369ac9SAndre Oppermann 	 * minmss on the sending side) and it deals with too many too small
105253369ac9SAndre Oppermann 	 * tcp packets in a too short timeframe (1 second).
105353369ac9SAndre Oppermann 	 *
105453369ac9SAndre Oppermann 	 * For every full second we count the number of received packets
105553369ac9SAndre Oppermann 	 * and bytes. If we get a lot of packets per second for this connection
105653369ac9SAndre Oppermann 	 * (tcp_minmssoverload) we take a closer look at it and compute the
105753369ac9SAndre Oppermann 	 * average packet size for the past second. If that is less than
105853369ac9SAndre Oppermann 	 * tcp_minmss we get too many packets with very small payload which
105953369ac9SAndre Oppermann 	 * is not good and burdens our system (and every packet generates
106053369ac9SAndre Oppermann 	 * a wakeup to the process connected to our socket). We can reasonable
106153369ac9SAndre Oppermann 	 * expect this to be small packet DoS attack to exhaust our CPU
106253369ac9SAndre Oppermann 	 * cycles.
106353369ac9SAndre Oppermann 	 *
106453369ac9SAndre Oppermann 	 * Care has to be taken for the minimum packet overload value. This
106553369ac9SAndre Oppermann 	 * value defines the minimum number of packets per second before we
106653369ac9SAndre Oppermann 	 * start to worry. This must not be too low to avoid killing for
106753369ac9SAndre Oppermann 	 * example interactive connections with many small packets like
106853369ac9SAndre Oppermann 	 * telnet or SSH.
106953369ac9SAndre Oppermann 	 *
107053369ac9SAndre Oppermann 	 * Setting either tcp_minmssoverload or tcp_minmss to "0" disables
107153369ac9SAndre Oppermann 	 * this check.
107253369ac9SAndre Oppermann 	 *
107353369ac9SAndre Oppermann 	 * Account for packet if payload packet, skip over ACK, etc.
107453369ac9SAndre Oppermann 	 */
107553369ac9SAndre Oppermann 	if (tcp_minmss && tcp_minmssoverload &&
107653369ac9SAndre Oppermann 	    tp->t_state == TCPS_ESTABLISHED && tlen > 0) {
107768d37625SStephan Uphoff 		if ((unsigned int)(tp->rcv_second - ticks) < hz) {
107853369ac9SAndre Oppermann 			tp->rcv_pps++;
107953369ac9SAndre Oppermann 			tp->rcv_byps += tlen + off;
108053369ac9SAndre Oppermann 			if (tp->rcv_pps > tcp_minmssoverload) {
108153369ac9SAndre Oppermann 				if ((tp->rcv_byps / tp->rcv_pps) < tcp_minmss) {
108253369ac9SAndre Oppermann 					printf("too many small tcp packets from "
108353369ac9SAndre Oppermann 					       "%s:%u, av. %lubyte/packet, "
108453369ac9SAndre Oppermann 					       "dropping connection\n",
108553369ac9SAndre Oppermann #ifdef INET6
108653369ac9SAndre Oppermann 						isipv6 ?
10871d54aa3bSBjoern A. Zeeb 						ip6_sprintf(ip6buf,
10881d54aa3bSBjoern A. Zeeb 						    &inp->inp_inc.inc6_faddr) :
108953369ac9SAndre Oppermann #endif
109053369ac9SAndre Oppermann 						inet_ntoa(inp->inp_inc.inc_faddr),
109153369ac9SAndre Oppermann 						inp->inp_inc.inc_fport,
109253369ac9SAndre Oppermann 						tp->rcv_byps / tp->rcv_pps);
10931e2d989dSRobert Watson 					KASSERT(headlocked, ("tcp_input: "
10941e2d989dSRobert Watson 					    "after_listen: tcp_drop: head "
10951e2d989dSRobert Watson 					    "not locked"));
109653369ac9SAndre Oppermann 					tp = tcp_drop(tp, ECONNRESET);
109753369ac9SAndre Oppermann 					tcpstat.tcps_minmssdrops++;
109853369ac9SAndre Oppermann 					goto drop;
109953369ac9SAndre Oppermann 				}
110053369ac9SAndre Oppermann 			}
110153369ac9SAndre Oppermann 		} else {
110253369ac9SAndre Oppermann 			tp->rcv_second = ticks + hz;
110353369ac9SAndre Oppermann 			tp->rcv_pps = 1;
110453369ac9SAndre Oppermann 			tp->rcv_byps = tlen + off;
110553369ac9SAndre Oppermann 		}
110653369ac9SAndre Oppermann 	}
110753369ac9SAndre Oppermann 
110853369ac9SAndre Oppermann 	/*
1109df8bae1dSRodney W. Grimes 	 * Segment received on connection.
1110df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
1111df8bae1dSRodney W. Grimes 	 */
11129b8b58e0SJonathan Lemon 	tp->t_rcvtime = ticks;
11137ff19458SPaul Traina 	if (TCPS_HAVEESTABLISHED(tp->t_state))
11149b8b58e0SJonathan Lemon 		callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp);
1115df8bae1dSRodney W. Grimes 
1116df8bae1dSRodney W. Grimes 	/*
1117464fcfbcSAndre Oppermann 	 * Unscale the window into a 32-bit value.
1118464fcfbcSAndre Oppermann 	 * This value is bogus for the TCPS_SYN_SENT state
1119464fcfbcSAndre Oppermann 	 * and is overwritten later.
1120464fcfbcSAndre Oppermann 	 */
1121464fcfbcSAndre Oppermann 	tiwin = th->th_win << tp->snd_scale;
1122464fcfbcSAndre Oppermann 
1123464fcfbcSAndre Oppermann 	/*
1124f72167f4SAndre Oppermann 	 * Parse options on any incoming segment.
1125f72167f4SAndre Oppermann 	 */
1126f72167f4SAndre Oppermann 	tcp_dooptions(&to, optp, optlen, (thflags & TH_SYN) ? TO_SYN : 0);
1127f72167f4SAndre Oppermann 
1128f72167f4SAndre Oppermann 	/*
1129f72167f4SAndre Oppermann 	 * If echoed timestamp is later than the current time,
1130bf6d304aSAndre Oppermann 	 * fall back to non RFC1323 RTT calculation.  Normalize
1131bf6d304aSAndre Oppermann 	 * timestamp if syncookies were used when this connection
1132bf6d304aSAndre Oppermann 	 * was established.
1133f72167f4SAndre Oppermann 	 */
1134bf6d304aSAndre Oppermann 	if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
1135e16fa5caSJohn-Mark Gurney 		to.to_tsecr -= tp->ts_offset;
1136bf6d304aSAndre Oppermann 		if (TSTMP_GT(to.to_tsecr, ticks))
1137f72167f4SAndre Oppermann 			to.to_tsecr = 0;
1138bf6d304aSAndre Oppermann 	}
1139f72167f4SAndre Oppermann 
1140f72167f4SAndre Oppermann 	/*
114197d8d152SAndre Oppermann 	 * Process options only when we get SYN/ACK back. The SYN case
114297d8d152SAndre Oppermann 	 * for incoming connections is handled in tcp_syncache.
114397d8d152SAndre Oppermann 	 * XXX this is traditional behavior, may need to be cleaned up.
1144df8bae1dSRodney W. Grimes 	 */
11450a389eabSSimon L. B. Nielsen 	if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
1146464fcfbcSAndre Oppermann 		if ((to.to_flags & TOF_SCALE) &&
1147464fcfbcSAndre Oppermann 		    (tp->t_flags & TF_REQ_SCALE)) {
1148be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_SCALE;
1149464fcfbcSAndre Oppermann 			tp->snd_scale = to.to_requested_s_scale;
1150464fcfbcSAndre Oppermann 			tp->snd_wnd = th->th_win << tp->snd_scale;
1151464fcfbcSAndre Oppermann 			tiwin = tp->snd_wnd;
1152be2ac88cSJonathan Lemon 		}
1153be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_TS) {
1154be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_TSTMP;
1155be2ac88cSJonathan Lemon 			tp->ts_recent = to.to_tsval;
1156be2ac88cSJonathan Lemon 			tp->ts_recent_age = ticks;
1157be2ac88cSJonathan Lemon 		}
1158be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_MSS)
1159be2ac88cSJonathan Lemon 			tcp_mss(tp, to.to_mss);
11606d90faf3SPaul Saab 		if (tp->sack_enable) {
11616d90faf3SPaul Saab 			if (!(to.to_flags & TOF_SACK))
11626d90faf3SPaul Saab 				tp->sack_enable = 0;
11636d90faf3SPaul Saab 			else
11646d90faf3SPaul Saab 				tp->t_flags |= TF_SACK_PERMIT;
11656d90faf3SPaul Saab 		}
11666d90faf3SPaul Saab 
11676d90faf3SPaul Saab 	}
11686d90faf3SPaul Saab 
1169df8bae1dSRodney W. Grimes 	/*
1170df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
1171df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
1172df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
1173df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
1174df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
1175df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
1176df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
1177df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
1178df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
1179df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
1180df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
1181df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
1182a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
1183a0292f23SGarrett Wollman 	 * Since we check for TCPS_ESTABLISHED above, it can only
1184a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
1185df8bae1dSRodney W. Grimes 	 */
1186df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
1187fb59c426SYoshinobu Inoue 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1188a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
1189be2ac88cSJonathan Lemon 	    ((to.to_flags & TOF_TS) == 0 ||
1190a0292f23SGarrett Wollman 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) &&
1191c94c54e4SAndre Oppermann 	     th->th_seq == tp->rcv_nxt && tiwin && tiwin == tp->snd_wnd &&
1192df8bae1dSRodney W. Grimes 	     tp->snd_nxt == tp->snd_max) {
1193df8bae1dSRodney W. Grimes 
1194df8bae1dSRodney W. Grimes 		/*
1195df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
1196df8bae1dSRodney W. Grimes 		 * record the timestamp.
1197a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
1198a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1199df8bae1dSRodney W. Grimes 		 */
1200be2ac88cSJonathan Lemon 		if ((to.to_flags & TOF_TS) != 0 &&
1201fb59c426SYoshinobu Inoue 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
12029b8b58e0SJonathan Lemon 			tp->ts_recent_age = ticks;
1203a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
1204df8bae1dSRodney W. Grimes 		}
1205df8bae1dSRodney W. Grimes 
1206fb59c426SYoshinobu Inoue 		if (tlen == 0) {
1207fb59c426SYoshinobu Inoue 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
1208fb59c426SYoshinobu Inoue 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
1209233e8c18SGarrett Wollman 			    tp->snd_cwnd >= tp->snd_wnd &&
12106d90faf3SPaul Saab 			    ((!tcp_do_newreno && !tp->sack_enable &&
1211cb942153SJeffrey Hsu 			      tp->t_dupacks < tcprexmtthresh) ||
12126d90faf3SPaul Saab 			     ((tcp_do_newreno || tp->sack_enable) &&
1213482ac968SPaul Saab 			      !IN_FASTRECOVERY(tp) && to.to_nsacks == 0 &&
1214482ac968SPaul Saab 			      TAILQ_EMPTY(&tp->snd_holes)))) {
1215f76fcf6dSJeffrey Hsu 				KASSERT(headlocked, ("headlocked"));
1216e0bef1cbSRobert Watson 				INP_INFO_WUNLOCK(&tcbinfo);
1217e0bef1cbSRobert Watson 				headlocked = 0;
1218df8bae1dSRodney W. Grimes 				/*
1219df8bae1dSRodney W. Grimes 				 * this is a pure ack for outstanding data.
1220df8bae1dSRodney W. Grimes 				 */
1221df8bae1dSRodney W. Grimes 				++tcpstat.tcps_predack;
12229b8b58e0SJonathan Lemon 				/*
12239b8b58e0SJonathan Lemon 				 * "bad retransmit" recovery
12249b8b58e0SJonathan Lemon 				 */
12259b8b58e0SJonathan Lemon 				if (tp->t_rxtshift == 1 &&
12269b8b58e0SJonathan Lemon 				    ticks < tp->t_badrxtwin) {
1227cb942153SJeffrey Hsu 					++tcpstat.tcps_sndrexmitbad;
12289b8b58e0SJonathan Lemon 					tp->snd_cwnd = tp->snd_cwnd_prev;
12299b8b58e0SJonathan Lemon 					tp->snd_ssthresh =
12309b8b58e0SJonathan Lemon 					    tp->snd_ssthresh_prev;
12319d11646dSJeffrey Hsu 					tp->snd_recover = tp->snd_recover_prev;
12329d11646dSJeffrey Hsu 					if (tp->t_flags & TF_WASFRECOVERY)
12339d11646dSJeffrey Hsu 					    ENTER_FASTRECOVERY(tp);
12349b8b58e0SJonathan Lemon 					tp->snd_nxt = tp->snd_max;
12359b8b58e0SJonathan Lemon 					tp->t_badrxtwin = 0;
12369b8b58e0SJonathan Lemon 				}
1237fa55172bSMatthew Dillon 
1238fa55172bSMatthew Dillon 				/*
1239fa55172bSMatthew Dillon 				 * Recalculate the transmit timer / rtt.
1240fa55172bSMatthew Dillon 				 *
1241fa55172bSMatthew Dillon 				 * Some boxes send broken timestamp replies
1242fa55172bSMatthew Dillon 				 * during the SYN+ACK phase, ignore
1243fa55172bSMatthew Dillon 				 * timestamps of 0 or we could calculate a
1244fa55172bSMatthew Dillon 				 * huge RTT and blow up the retransmit timer.
1245fa55172bSMatthew Dillon 				 */
1246fa55172bSMatthew Dillon 				if ((to.to_flags & TOF_TS) != 0 &&
1247fa55172bSMatthew Dillon 				    to.to_tsecr) {
1248eaf80179SAndre Oppermann 					if (!tp->t_rttlow ||
1249eaf80179SAndre Oppermann 					    tp->t_rttlow > ticks - to.to_tsecr)
1250eaf80179SAndre Oppermann 						tp->t_rttlow = ticks - to.to_tsecr;
1251a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
12529b8b58e0SJonathan Lemon 					    ticks - to.to_tsecr + 1);
1253fa55172bSMatthew Dillon 				} else if (tp->t_rtttime &&
1254fa55172bSMatthew Dillon 					    SEQ_GT(th->th_ack, tp->t_rtseq)) {
1255eaf80179SAndre Oppermann 					if (!tp->t_rttlow ||
1256eaf80179SAndre Oppermann 					    tp->t_rttlow > ticks - tp->t_rtttime)
1257eaf80179SAndre Oppermann 						tp->t_rttlow = ticks - tp->t_rtttime;
1258c068736aSJeffrey Hsu 					tcp_xmit_timer(tp,
1259c068736aSJeffrey Hsu 							ticks - tp->t_rtttime);
1260fa55172bSMatthew Dillon 				}
12611fcc99b5SMatthew Dillon 				tcp_xmit_bandwidth_limit(tp, th->th_ack);
1262fb59c426SYoshinobu Inoue 				acked = th->th_ack - tp->snd_una;
1263df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvackpack++;
1264df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvackbyte += acked;
1265df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
12669d11646dSJeffrey Hsu 				if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
12679d11646dSJeffrey Hsu 				    SEQ_LEQ(th->th_ack, tp->snd_recover))
12689d11646dSJeffrey Hsu 					tp->snd_recover = th->th_ack - 1;
12699d11646dSJeffrey Hsu 				tp->snd_una = th->th_ack;
12701645d090SJeff Roberson 				/*
12711645d090SJeff Roberson 				 * pull snd_wl2 up to prevent seq wrap relative
12721645d090SJeff Roberson 				 * to th_ack.
12731645d090SJeff Roberson 				 */
1274cb942153SJeffrey Hsu 				tp->snd_wl2 = th->th_ack;
1275b5addd85SJeffrey Hsu 				tp->t_dupacks = 0;
1276df8bae1dSRodney W. Grimes 				m_freem(m);
1277fb59c426SYoshinobu Inoue 				ND6_HINT(tp); /* some progress has been done */
1278df8bae1dSRodney W. Grimes 
1279df8bae1dSRodney W. Grimes 				/*
1280df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
1281df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
1282df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
1283df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
1284df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
1285df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
1286df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
12873c653157SHartmut Brandt 
12883c653157SHartmut Brandt #ifdef TCPDEBUG
12893c653157SHartmut Brandt 				if (so->so_options & SO_DEBUG)
12903c653157SHartmut Brandt 					tcp_trace(TA_INPUT, ostate, tp,
12913c653157SHartmut Brandt 					    (void *)tcp_saveipgen,
12923c653157SHartmut Brandt 					    &tcp_savetcp, 0);
12933c653157SHartmut Brandt #endif
1294df8bae1dSRodney W. Grimes 				 */
1295df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
12969b8b58e0SJonathan Lemon 					callout_stop(tp->tt_rexmt);
12979b8b58e0SJonathan Lemon 				else if (!callout_active(tp->tt_persist))
12989b8b58e0SJonathan Lemon 					callout_reset(tp->tt_rexmt,
12999b8b58e0SJonathan Lemon 						      tp->t_rxtcur,
13009b8b58e0SJonathan Lemon 						      tcp_timer_rexmt, tp);
1301df8bae1dSRodney W. Grimes 
1302df8bae1dSRodney W. Grimes 				sowwakeup(so);
1303df8bae1dSRodney W. Grimes 				if (so->so_snd.sb_cc)
1304df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
1305a14c749fSJonathan Lemon 				goto check_delack;
1306df8bae1dSRodney W. Grimes 			}
1307fb59c426SYoshinobu Inoue 		} else if (th->th_ack == tp->snd_una &&
1308fb59c426SYoshinobu Inoue 		    LIST_EMPTY(&tp->t_segq) &&
1309fb59c426SYoshinobu Inoue 		    tlen <= sbspace(&so->so_rcv)) {
13106741ecf5SAndre Oppermann 			int newsize = 0;	/* automatic sockbuf scaling */
13116741ecf5SAndre Oppermann 
1312f76fcf6dSJeffrey Hsu 			KASSERT(headlocked, ("headlocked"));
1313e0bef1cbSRobert Watson 			INP_INFO_WUNLOCK(&tcbinfo);
1314e0bef1cbSRobert Watson 			headlocked = 0;
1315df8bae1dSRodney W. Grimes 			/*
1316df8bae1dSRodney W. Grimes 			 * this is a pure, in-sequence data packet
1317df8bae1dSRodney W. Grimes 			 * with nothing on the reassembly queue and
1318df8bae1dSRodney W. Grimes 			 * we have enough buffer space to take it.
1319df8bae1dSRodney W. Grimes 			 */
13206d90faf3SPaul Saab 			/* Clean receiver SACK report if present */
13216d90faf3SPaul Saab 			if (tp->sack_enable && tp->rcv_numsacks)
13226d90faf3SPaul Saab 				tcp_clean_sackreport(tp);
1323df8bae1dSRodney W. Grimes 			++tcpstat.tcps_preddat;
1324fb59c426SYoshinobu Inoue 			tp->rcv_nxt += tlen;
13251645d090SJeff Roberson 			/*
13261645d090SJeff Roberson 			 * Pull snd_wl1 up to prevent seq wrap relative to
13271645d090SJeff Roberson 			 * th_seq.
13281645d090SJeff Roberson 			 */
13291645d090SJeff Roberson 			tp->snd_wl1 = th->th_seq;
13301645d090SJeff Roberson 			/*
13311645d090SJeff Roberson 			 * Pull rcv_up up to prevent seq wrap relative to
13321645d090SJeff Roberson 			 * rcv_nxt.
13331645d090SJeff Roberson 			 */
13341645d090SJeff Roberson 			tp->rcv_up = tp->rcv_nxt;
1335df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpack++;
1336fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbyte += tlen;
1337fb59c426SYoshinobu Inoue 			ND6_HINT(tp);	/* some progress has been done */
13383c653157SHartmut Brandt #ifdef TCPDEBUG
13393c653157SHartmut Brandt 			if (so->so_options & SO_DEBUG)
13403c653157SHartmut Brandt 				tcp_trace(TA_INPUT, ostate, tp,
13413c653157SHartmut Brandt 				    (void *)tcp_saveipgen, &tcp_savetcp, 0);
13423c653157SHartmut Brandt #endif
13436741ecf5SAndre Oppermann 		/*
13446741ecf5SAndre Oppermann 		 * Automatic sizing of receive socket buffer.  Often the send
13456741ecf5SAndre Oppermann 		 * buffer size is not optimally adjusted to the actual network
13466741ecf5SAndre Oppermann 		 * conditions at hand (delay bandwidth product).  Setting the
13476741ecf5SAndre Oppermann 		 * buffer size too small limits throughput on links with high
13486741ecf5SAndre Oppermann 		 * bandwidth and high delay (eg. trans-continental/oceanic links).
13496741ecf5SAndre Oppermann 		 *
13506741ecf5SAndre Oppermann 		 * On the receive side the socket buffer memory is only rarely
13516741ecf5SAndre Oppermann 		 * used to any significant extent.  This allows us to be much
13526741ecf5SAndre Oppermann 		 * more aggressive in scaling the receive socket buffer.  For
13536741ecf5SAndre Oppermann 		 * the case that the buffer space is actually used to a large
13546741ecf5SAndre Oppermann 		 * extent and we run out of kernel memory we can simply drop
13556741ecf5SAndre Oppermann 		 * the new segments; TCP on the sender will just retransmit it
13566741ecf5SAndre Oppermann 		 * later.  Setting the buffer size too big may only consume too
13576741ecf5SAndre Oppermann 		 * much kernel memory if the application doesn't read() from
13586741ecf5SAndre Oppermann 		 * the socket or packet loss or reordering makes use of the
13596741ecf5SAndre Oppermann 		 * reassembly queue.
13606741ecf5SAndre Oppermann 		 *
13616741ecf5SAndre Oppermann 		 * The criteria to step up the receive buffer one notch are:
13626741ecf5SAndre Oppermann 		 *  1. the number of bytes received during the time it takes
13636741ecf5SAndre Oppermann 		 *     one timestamp to be reflected back to us (the RTT);
13646741ecf5SAndre Oppermann 		 *  2. received bytes per RTT is within seven eighth of the
13656741ecf5SAndre Oppermann 		 *     current socket buffer size;
13666741ecf5SAndre Oppermann 		 *  3. receive buffer size has not hit maximal automatic size;
13676741ecf5SAndre Oppermann 		 *
13686741ecf5SAndre Oppermann 		 * This algorithm does one step per RTT at most and only if
13696741ecf5SAndre Oppermann 		 * we receive a bulk stream w/o packet losses or reorderings.
13706741ecf5SAndre Oppermann 		 * Shrinking the buffer during idle times is not necessary as
13716741ecf5SAndre Oppermann 		 * it doesn't consume any memory when idle.
13726741ecf5SAndre Oppermann 		 *
13736741ecf5SAndre Oppermann 		 * TODO: Only step up if the application is actually serving
13746741ecf5SAndre Oppermann 		 * the buffer to better manage the socket buffer resources.
1375df8bae1dSRodney W. Grimes 		 */
13766741ecf5SAndre Oppermann 			if (tcp_do_autorcvbuf &&
13776741ecf5SAndre Oppermann 			    to.to_tsecr &&
13786741ecf5SAndre Oppermann 			    (so->so_rcv.sb_flags & SB_AUTOSIZE)) {
13796741ecf5SAndre Oppermann 				if (to.to_tsecr > tp->rfbuf_ts &&
13806741ecf5SAndre Oppermann 				    to.to_tsecr - tp->rfbuf_ts < hz) {
13816741ecf5SAndre Oppermann 					if (tp->rfbuf_cnt >
13826741ecf5SAndre Oppermann 					    (so->so_rcv.sb_hiwat / 8 * 7) &&
13836741ecf5SAndre Oppermann 					    so->so_rcv.sb_hiwat <
13846741ecf5SAndre Oppermann 					    tcp_autorcvbuf_max) {
13856741ecf5SAndre Oppermann 						newsize =
13866741ecf5SAndre Oppermann 						    min(so->so_rcv.sb_hiwat +
13876741ecf5SAndre Oppermann 						    tcp_autorcvbuf_inc,
13886741ecf5SAndre Oppermann 						    tcp_autorcvbuf_max);
13896741ecf5SAndre Oppermann 					}
13906741ecf5SAndre Oppermann 					/* Start over with next RTT. */
13916741ecf5SAndre Oppermann 					tp->rfbuf_ts = 0;
13926741ecf5SAndre Oppermann 					tp->rfbuf_cnt = 0;
13936741ecf5SAndre Oppermann 				} else
13946741ecf5SAndre Oppermann 					tp->rfbuf_cnt += tlen;	/* add up */
13956741ecf5SAndre Oppermann 			}
13966741ecf5SAndre Oppermann 
13976741ecf5SAndre Oppermann 			/* Add data to socket buffer. */
13981e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
1399c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1400c1c36a2cSMike Silbersack 				m_freem(m);
1401c1c36a2cSMike Silbersack 			} else {
14026741ecf5SAndre Oppermann 				/*
14036741ecf5SAndre Oppermann 				 * Set new socket buffer size.
14046741ecf5SAndre Oppermann 				 * Give up when limit is reached.
14056741ecf5SAndre Oppermann 				 */
14066741ecf5SAndre Oppermann 				if (newsize)
14076741ecf5SAndre Oppermann 					if (!sbreserve_locked(&so->so_rcv,
14086741ecf5SAndre Oppermann 					    newsize, so, curthread))
14096741ecf5SAndre Oppermann 						so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
1410fb59c426SYoshinobu Inoue 				m_adj(m, drop_hdrlen);	/* delayed header drop */
14111e4d7da7SRobert Watson 				sbappendstream_locked(&so->so_rcv, m);
1412c1c36a2cSMike Silbersack 			}
14131e4d7da7SRobert Watson 			sorwakeup_locked(so);
1414d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp)) {
14153bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
1416f498eeeeSDavid Greenman 			} else {
1417e612a582SDavid Greenman 				tp->t_flags |= TF_ACKNOW;
1418e612a582SDavid Greenman 				tcp_output(tp);
1419e612a582SDavid Greenman 			}
1420a14c749fSJonathan Lemon 			goto check_delack;
1421df8bae1dSRodney W. Grimes 		}
1422df8bae1dSRodney W. Grimes 	}
1423df8bae1dSRodney W. Grimes 
1424df8bae1dSRodney W. Grimes 	/*
1425df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
1426df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
1427df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
1428df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
1429df8bae1dSRodney W. Grimes 	 */
1430df8bae1dSRodney W. Grimes 	{ int win;
1431df8bae1dSRodney W. Grimes 
1432df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
1433df8bae1dSRodney W. Grimes 	if (win < 0)
1434df8bae1dSRodney W. Grimes 		win = 0;
143566e39adcSJohn Polstra 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1436df8bae1dSRodney W. Grimes 	}
1437df8bae1dSRodney W. Grimes 
14386741ecf5SAndre Oppermann 	/* Reset receive buffer auto scaling when not in bulk receive mode. */
14396741ecf5SAndre Oppermann 	tp->rfbuf_ts = 0;
14406741ecf5SAndre Oppermann 	tp->rfbuf_cnt = 0;
14416741ecf5SAndre Oppermann 
1442df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1443df8bae1dSRodney W. Grimes 
1444df8bae1dSRodney W. Grimes 	/*
1445764d8cefSBill Fenner 	 * If the state is SYN_RECEIVED:
1446764d8cefSBill Fenner 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
1447764d8cefSBill Fenner 	 */
1448764d8cefSBill Fenner 	case TCPS_SYN_RECEIVED:
1449fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1450fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1451a57815efSBosko Milekic 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1452a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
1453a57815efSBosko Milekic 				goto dropwithreset;
1454a57815efSBosko Milekic 		}
1455764d8cefSBill Fenner 		break;
1456764d8cefSBill Fenner 
1457764d8cefSBill Fenner 	/*
1458df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
1459df8bae1dSRodney W. Grimes 	 *	if seg contains an ACK, but not for our SYN, drop the input.
1460df8bae1dSRodney W. Grimes 	 *	if seg contains a RST, then drop the connection.
1461df8bae1dSRodney W. Grimes 	 *	if seg does not contain SYN, then drop it.
1462df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
1463df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
1464df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
1465df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1466df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
1467df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
1468df8bae1dSRodney W. Grimes 	 */
1469df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
1470fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1471fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->iss) ||
1472fb59c426SYoshinobu Inoue 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1473a57815efSBosko Milekic 			rstreason = BANDLIM_UNLIMITED;
1474a0292f23SGarrett Wollman 			goto dropwithreset;
1475a0292f23SGarrett Wollman 		}
1476fb59c426SYoshinobu Inoue 		if (thflags & TH_RST) {
14771e2d989dSRobert Watson 			if (thflags & TH_ACK) {
14781e2d989dSRobert Watson 				KASSERT(headlocked, ("tcp_input: after_listen"
14791e2d989dSRobert Watson 				    ": tcp_drop.2: head not locked"));
1480df8bae1dSRodney W. Grimes 				tp = tcp_drop(tp, ECONNREFUSED);
14811e2d989dSRobert Watson 			}
1482df8bae1dSRodney W. Grimes 			goto drop;
1483df8bae1dSRodney W. Grimes 		}
1484fb59c426SYoshinobu Inoue 		if ((thflags & TH_SYN) == 0)
1485df8bae1dSRodney W. Grimes 			goto drop;
1486464fcfbcSAndre Oppermann 
1487464fcfbcSAndre Oppermann 		/* Initial send window, already scaled. */
1488464fcfbcSAndre Oppermann 		tp->snd_wnd = th->th_win;
1489a0292f23SGarrett Wollman 
1490fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
1491df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
1492fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1493845799c1SAndras Olah 			tcpstat.tcps_connects++;
1494845799c1SAndras Olah 			soisconnected(so);
1495c488362eSRobert Watson #ifdef MAC
1496310e7cebSRobert Watson 			SOCK_LOCK(so);
1497c488362eSRobert Watson 			mac_set_socket_peer_from_mbuf(m, so);
1498310e7cebSRobert Watson 			SOCK_UNLOCK(so);
1499c488362eSRobert Watson #endif
1500845799c1SAndras Olah 			/* Do window scaling on this connection? */
1501845799c1SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1502845799c1SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1503845799c1SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
1504845799c1SAndras Olah 			}
1505a0292f23SGarrett Wollman 			tp->rcv_adv += tp->rcv_wnd;
1506a0292f23SGarrett Wollman 			tp->snd_una++;		/* SYN is acked */
1507a0292f23SGarrett Wollman 			/*
1508a0292f23SGarrett Wollman 			 * If there's data, delay ACK; if there's also a FIN
1509a0292f23SGarrett Wollman 			 * ACKNOW will be turned on later.
1510a0292f23SGarrett Wollman 			 */
1511d8c85a26SJonathan Lemon 			if (DELAY_ACK(tp) && tlen != 0)
15129b8b58e0SJonathan Lemon 				callout_reset(tp->tt_delack, tcp_delacktime,
15139b8b58e0SJonathan Lemon 				    tcp_timer_delack, tp);
1514a0292f23SGarrett Wollman 			else
1515a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
1516a0292f23SGarrett Wollman 			/*
1517a0292f23SGarrett Wollman 			 * Received <SYN,ACK> in SYN_SENT[*] state.
1518a0292f23SGarrett Wollman 			 * Transitions:
1519a0292f23SGarrett Wollman 			 *	SYN_SENT  --> ESTABLISHED
1520a0292f23SGarrett Wollman 			 *	SYN_SENT* --> FIN_WAIT_1
1521a0292f23SGarrett Wollman 			 */
15229b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
1523a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDFIN) {
1524a0292f23SGarrett Wollman 				tp->t_state = TCPS_FIN_WAIT_1;
1525a0292f23SGarrett Wollman 				tp->t_flags &= ~TF_NEEDFIN;
1526fb59c426SYoshinobu Inoue 				thflags &= ~TH_SYN;
15277ff19458SPaul Traina 			} else {
1528a0292f23SGarrett Wollman 				tp->t_state = TCPS_ESTABLISHED;
15299b8b58e0SJonathan Lemon 				callout_reset(tp->tt_keep, tcp_keepidle,
15309b8b58e0SJonathan Lemon 					      tcp_timer_keep, tp);
15317ff19458SPaul Traina 			}
1532a0292f23SGarrett Wollman 		} else {
1533a0292f23SGarrett Wollman 			/*
1534c068736aSJeffrey Hsu 			 * Received initial SYN in SYN-SENT[*] state =>
1535c068736aSJeffrey Hsu 			 * simultaneous open.  If segment contains CC option
1536c068736aSJeffrey Hsu 			 * and there is a cached CC, apply TAO test.
1537c068736aSJeffrey Hsu 			 * If it succeeds, connection is * half-synchronized.
1538c068736aSJeffrey Hsu 			 * Otherwise, do 3-way handshake:
1539a0292f23SGarrett Wollman 			 *        SYN-SENT -> SYN-RECEIVED
1540a0292f23SGarrett Wollman 			 *        SYN-SENT* -> SYN-RECEIVED*
1541a0292f23SGarrett Wollman 			 * If there was no CC option, clear cached CC value.
1542a0292f23SGarrett Wollman 			 */
15434b8e98d6SQing Li 			tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
15449b8b58e0SJonathan Lemon 			callout_stop(tp->tt_rexmt);
1545df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_SYN_RECEIVED;
1546a0292f23SGarrett Wollman 		}
1547df8bae1dSRodney W. Grimes 
1548351630c4SAndre Oppermann #if 0 /* T/TCP */
1549df8bae1dSRodney W. Grimes trimthenstep6:
1550351630c4SAndre Oppermann #endif
1551de30ea13SRobert Watson 		KASSERT(headlocked, ("tcp_input: trimthenstep6: head not "
1552de30ea13SRobert Watson 		    "locked"));
15537cfc6904SRobert Watson 		INP_LOCK_ASSERT(inp);
15547cfc6904SRobert Watson 
1555df8bae1dSRodney W. Grimes 		/*
1556fb59c426SYoshinobu Inoue 		 * Advance th->th_seq to correspond to first data byte.
1557df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
1558df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
1559df8bae1dSRodney W. Grimes 		 */
1560fb59c426SYoshinobu Inoue 		th->th_seq++;
1561fb59c426SYoshinobu Inoue 		if (tlen > tp->rcv_wnd) {
1562fb59c426SYoshinobu Inoue 			todrop = tlen - tp->rcv_wnd;
1563df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
1564fb59c426SYoshinobu Inoue 			tlen = tp->rcv_wnd;
1565fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
1566df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpackafterwin++;
1567df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyteafterwin += todrop;
1568df8bae1dSRodney W. Grimes 		}
1569fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
1570fb59c426SYoshinobu Inoue 		tp->rcv_up = th->th_seq;
1571a0292f23SGarrett Wollman 		/*
1572a0292f23SGarrett Wollman 		 * Client side of transaction: already sent SYN and data.
1573a0292f23SGarrett Wollman 		 * If the remote host used T/TCP to validate the SYN,
1574a0292f23SGarrett Wollman 		 * our data will be ACK'd; if so, enter normal data segment
1575a0292f23SGarrett Wollman 		 * processing in the middle of step 5, ack processing.
1576a0292f23SGarrett Wollman 		 * Otherwise, goto step 6.
1577a0292f23SGarrett Wollman 		 */
1578fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK)
1579a0292f23SGarrett Wollman 			goto process_ACK;
1580c068736aSJeffrey Hsu 
1581df8bae1dSRodney W. Grimes 		goto step6;
1582c068736aSJeffrey Hsu 
1583a0292f23SGarrett Wollman 	/*
1584a0292f23SGarrett Wollman 	 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
1585c94c54e4SAndre Oppermann 	 *      do normal processing.
1586a0292f23SGarrett Wollman 	 *
1587c94c54e4SAndre Oppermann 	 * NB: Leftover from RFC1644 T/TCP.  Cases to be reused later.
1588a0292f23SGarrett Wollman 	 */
1589a0292f23SGarrett Wollman 	case TCPS_LAST_ACK:
1590a0292f23SGarrett Wollman 	case TCPS_CLOSING:
1591a0292f23SGarrett Wollman 	case TCPS_TIME_WAIT:
1592340c35deSJonathan Lemon 		KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
1593a0292f23SGarrett Wollman 		break;  /* continue normal processing */
1594df8bae1dSRodney W. Grimes 	}
1595df8bae1dSRodney W. Grimes 
1596df8bae1dSRodney W. Grimes 	/*
1597df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
159880ab7c0eSGarrett Wollman 	 * First check the RST flag and sequence number since reset segments
159980ab7c0eSGarrett Wollman 	 * are exempt from the timestamp and connection count tests.  This
160080ab7c0eSGarrett Wollman 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
160180ab7c0eSGarrett Wollman 	 * below which allowed reset segments in half the sequence space
160280ab7c0eSGarrett Wollman 	 * to fall though and be processed (which gives forged reset
160380ab7c0eSGarrett Wollman 	 * segments with a random sequence number a 50 percent chance of
160480ab7c0eSGarrett Wollman 	 * killing a connection).
160580ab7c0eSGarrett Wollman 	 * Then check timestamp, if present.
1606a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
1607df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
1608df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
1609df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
1610df8bae1dSRodney W. Grimes 	 *
161180ab7c0eSGarrett Wollman 	 *
161280ab7c0eSGarrett Wollman 	 * If the RST bit is set, check the sequence number to see
161380ab7c0eSGarrett Wollman 	 * if this is a valid reset segment.
161480ab7c0eSGarrett Wollman 	 * RFC 793 page 37:
161580ab7c0eSGarrett Wollman 	 *   In all states except SYN-SENT, all reset (RST) segments
161680ab7c0eSGarrett Wollman 	 *   are validated by checking their SEQ-fields.  A reset is
161780ab7c0eSGarrett Wollman 	 *   valid if its sequence number is in the window.
161880ab7c0eSGarrett Wollman 	 * Note: this does not take into account delayed ACKs, so
161980ab7c0eSGarrett Wollman 	 *   we should test against last_ack_sent instead of rcv_nxt.
16201a244a61SJonathan Lemon 	 *   The sequence number in the reset segment is normally an
16211a244a61SJonathan Lemon 	 *   echo of our outgoing acknowlegement numbers, but some hosts
16221a244a61SJonathan Lemon 	 *   send a reset with the sequence number at the rightmost edge
16231a244a61SJonathan Lemon 	 *   of our receive window, and we have to handle this case.
162480dd2a81SMike Silbersack 	 * Note 2: Paul Watson's paper "Slipping in the Window" has shown
162580dd2a81SMike Silbersack 	 *   that brute force RST attacks are possible.  To combat this,
162680dd2a81SMike Silbersack 	 *   we use a much stricter check while in the ESTABLISHED state,
162780dd2a81SMike Silbersack 	 *   only accepting RSTs where the sequence number is equal to
162880dd2a81SMike Silbersack 	 *   last_ack_sent.  In all other states (the states in which a
162980dd2a81SMike Silbersack 	 *   RST is more likely), the more permissive check is used.
163080ab7c0eSGarrett Wollman 	 * If we have multiple segments in flight, the intial reset
163180ab7c0eSGarrett Wollman 	 * segment sequence numbers will be to the left of last_ack_sent,
163280ab7c0eSGarrett Wollman 	 * but they will eventually catch up.
163380ab7c0eSGarrett Wollman 	 * In any case, it never made sense to trim reset segments to
163480ab7c0eSGarrett Wollman 	 * fit the receive window since RFC 1122 says:
163580ab7c0eSGarrett Wollman 	 *   4.2.2.12  RST Segment: RFC-793 Section 3.4
163680ab7c0eSGarrett Wollman 	 *
163780ab7c0eSGarrett Wollman 	 *    A TCP SHOULD allow a received RST segment to include data.
163880ab7c0eSGarrett Wollman 	 *
163980ab7c0eSGarrett Wollman 	 *    DISCUSSION
164080ab7c0eSGarrett Wollman 	 *         It has been suggested that a RST segment could contain
164180ab7c0eSGarrett Wollman 	 *         ASCII text that encoded and explained the cause of the
164280ab7c0eSGarrett Wollman 	 *         RST.  No standard has yet been established for such
164380ab7c0eSGarrett Wollman 	 *         data.
164480ab7c0eSGarrett Wollman 	 *
164580ab7c0eSGarrett Wollman 	 * If the reset segment passes the sequence number test examine
164680ab7c0eSGarrett Wollman 	 * the state:
164780ab7c0eSGarrett Wollman 	 *    SYN_RECEIVED STATE:
164880ab7c0eSGarrett Wollman 	 *	If passive open, return to LISTEN state.
164980ab7c0eSGarrett Wollman 	 *	If active open, inform user that connection was refused.
1650745bab7fSDima Dorfman 	 *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES:
165180ab7c0eSGarrett Wollman 	 *	Inform user that connection was reset, and close tcb.
1652e9bd3a37SJonathan M. Bresler 	 *    CLOSING, LAST_ACK STATES:
165380ab7c0eSGarrett Wollman 	 *	Close the tcb.
1654e9bd3a37SJonathan M. Bresler 	 *    TIME_WAIT STATE:
165580ab7c0eSGarrett Wollman 	 *	Drop the segment - see Stevens, vol. 2, p. 964 and
165680ab7c0eSGarrett Wollman 	 *      RFC 1337.
165780ab7c0eSGarrett Wollman 	 */
1658fb59c426SYoshinobu Inoue 	if (thflags & TH_RST) {
16596a220ed8SMike Silbersack 		if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
16606a220ed8SMike Silbersack 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
16616a220ed8SMike Silbersack 		    (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) {
166280ab7c0eSGarrett Wollman 			switch (tp->t_state) {
166380ab7c0eSGarrett Wollman 
166480ab7c0eSGarrett Wollman 			case TCPS_SYN_RECEIVED:
166580ab7c0eSGarrett Wollman 				so->so_error = ECONNREFUSED;
166680ab7c0eSGarrett Wollman 				goto close;
166780ab7c0eSGarrett Wollman 
166880ab7c0eSGarrett Wollman 			case TCPS_ESTABLISHED:
1669a69968eeSMike Silbersack 				if (tp->last_ack_sent != th->th_seq &&
1670a69968eeSMike Silbersack 			 	    tcp_insecure_rst == 0) {
167180dd2a81SMike Silbersack 					tcpstat.tcps_badrst++;
167280dd2a81SMike Silbersack 					goto drop;
167380dd2a81SMike Silbersack 				}
167480ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_1:
167580ab7c0eSGarrett Wollman 			case TCPS_FIN_WAIT_2:
167680ab7c0eSGarrett Wollman 			case TCPS_CLOSE_WAIT:
167780ab7c0eSGarrett Wollman 				so->so_error = ECONNRESET;
167880ab7c0eSGarrett Wollman 			close:
167980ab7c0eSGarrett Wollman 				tp->t_state = TCPS_CLOSED;
168080ab7c0eSGarrett Wollman 				tcpstat.tcps_drops++;
1681416738a7SRobert Watson 				KASSERT(headlocked, ("tcp_input: "
1682416738a7SRobert Watson 				    "trimthenstep6: tcp_close: head not "
1683416738a7SRobert Watson 				    "locked"));
168480ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
168580ab7c0eSGarrett Wollman 				break;
168680ab7c0eSGarrett Wollman 
168780ab7c0eSGarrett Wollman 			case TCPS_CLOSING:
168880ab7c0eSGarrett Wollman 			case TCPS_LAST_ACK:
1689416738a7SRobert Watson 				KASSERT(headlocked, ("trimthenstep6: "
1690416738a7SRobert Watson 				    "tcp_close.2: head not locked"));
169180ab7c0eSGarrett Wollman 				tp = tcp_close(tp);
169280ab7c0eSGarrett Wollman 				break;
169380ab7c0eSGarrett Wollman 
169480ab7c0eSGarrett Wollman 			case TCPS_TIME_WAIT:
1695340c35deSJonathan Lemon 				KASSERT(tp->t_state != TCPS_TIME_WAIT,
1696340c35deSJonathan Lemon 				    ("timewait"));
169780ab7c0eSGarrett Wollman 				break;
169880ab7c0eSGarrett Wollman 			}
169980ab7c0eSGarrett Wollman 		}
170080ab7c0eSGarrett Wollman 		goto drop;
170180ab7c0eSGarrett Wollman 	}
170280ab7c0eSGarrett Wollman 
170380ab7c0eSGarrett Wollman 	/*
1704df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
1705df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
1706df8bae1dSRodney W. Grimes 	 */
1707be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
170880ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
1709df8bae1dSRodney W. Grimes 
1710df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
17119b8b58e0SJonathan Lemon 		if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) {
1712df8bae1dSRodney W. Grimes 			/*
1713df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
1714df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
1715df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
1716df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
1717df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
1718df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
1719df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
1720df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
1721df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
1722df8bae1dSRodney W. Grimes 			 */
1723df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
1724df8bae1dSRodney W. Grimes 		} else {
1725df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvduppack++;
1726fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvdupbyte += tlen;
1727df8bae1dSRodney W. Grimes 			tcpstat.tcps_pawsdrop++;
172807fd333dSMatthew Dillon 			if (tlen)
1729df8bae1dSRodney W. Grimes 				goto dropafterack;
17301ab4789dSMatthew Dillon 			goto drop;
17311ab4789dSMatthew Dillon 		}
1732df8bae1dSRodney W. Grimes 	}
1733df8bae1dSRodney W. Grimes 
1734a0292f23SGarrett Wollman 	/*
173580ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
173680ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
173780ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
173880ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
173980ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
174080ab7c0eSGarrett Wollman 	 */
1741a57815efSBosko Milekic 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
1742a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
1743a57815efSBosko Milekic 		goto dropwithreset;
1744a57815efSBosko Milekic 	}
174580ab7c0eSGarrett Wollman 
1746fb59c426SYoshinobu Inoue 	todrop = tp->rcv_nxt - th->th_seq;
1747df8bae1dSRodney W. Grimes 	if (todrop > 0) {
1748fb59c426SYoshinobu Inoue 		if (thflags & TH_SYN) {
1749fb59c426SYoshinobu Inoue 			thflags &= ~TH_SYN;
1750fb59c426SYoshinobu Inoue 			th->th_seq++;
1751fb59c426SYoshinobu Inoue 			if (th->th_urp > 1)
1752fb59c426SYoshinobu Inoue 				th->th_urp--;
1753df8bae1dSRodney W. Grimes 			else
1754fb59c426SYoshinobu Inoue 				thflags &= ~TH_URG;
1755df8bae1dSRodney W. Grimes 			todrop--;
1756df8bae1dSRodney W. Grimes 		}
1757df8bae1dSRodney W. Grimes 		/*
1758dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
1759df8bae1dSRodney W. Grimes 		 */
1760fb59c426SYoshinobu Inoue 		if (todrop > tlen
1761fb59c426SYoshinobu Inoue 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
1762dac20301SGarrett Wollman 			/*
1763dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
1764dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
1765dac20301SGarrett Wollman 			 * of sequence; drop it.
1766dac20301SGarrett Wollman 			 */
1767fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
1768dac20301SGarrett Wollman 
1769df8bae1dSRodney W. Grimes 			/*
1770dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
1771dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
1772df8bae1dSRodney W. Grimes 			 */
1773dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
1774fb59c426SYoshinobu Inoue 			todrop = tlen;
1775dac20301SGarrett Wollman 			tcpstat.tcps_rcvduppack++;
1776dac20301SGarrett Wollman 			tcpstat.tcps_rcvdupbyte += todrop;
1777df8bae1dSRodney W. Grimes 		} else {
1778df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpartduppack++;
1779df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvpartdupbyte += todrop;
1780df8bae1dSRodney W. Grimes 		}
1781fb59c426SYoshinobu Inoue 		drop_hdrlen += todrop;	/* drop from the top afterwards */
1782fb59c426SYoshinobu Inoue 		th->th_seq += todrop;
1783fb59c426SYoshinobu Inoue 		tlen -= todrop;
1784fb59c426SYoshinobu Inoue 		if (th->th_urp > todrop)
1785fb59c426SYoshinobu Inoue 			th->th_urp -= todrop;
1786df8bae1dSRodney W. Grimes 		else {
1787fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;
1788fb59c426SYoshinobu Inoue 			th->th_urp = 0;
1789df8bae1dSRodney W. Grimes 		}
1790df8bae1dSRodney W. Grimes 	}
1791df8bae1dSRodney W. Grimes 
1792df8bae1dSRodney W. Grimes 	/*
1793df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
1794df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
1795df8bae1dSRodney W. Grimes 	 */
1796df8bae1dSRodney W. Grimes 	if ((so->so_state & SS_NOFDREF) &&
1797fb59c426SYoshinobu Inoue 	    tp->t_state > TCPS_CLOSE_WAIT && tlen) {
1798416738a7SRobert Watson 		KASSERT(headlocked, ("trimthenstep6: tcp_close.3: head not "
1799416738a7SRobert Watson 		    "locked"));
1800df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
1801df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvafterclose++;
1802a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
1803df8bae1dSRodney W. Grimes 		goto dropwithreset;
1804df8bae1dSRodney W. Grimes 	}
1805df8bae1dSRodney W. Grimes 
1806df8bae1dSRodney W. Grimes 	/*
1807df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
1808df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
1809df8bae1dSRodney W. Grimes 	 */
1810fb59c426SYoshinobu Inoue 	todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd);
1811df8bae1dSRodney W. Grimes 	if (todrop > 0) {
1812df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvpackafterwin++;
1813fb59c426SYoshinobu Inoue 		if (todrop >= tlen) {
1814fb59c426SYoshinobu Inoue 			tcpstat.tcps_rcvbyteafterwin += tlen;
1815df8bae1dSRodney W. Grimes 			/*
1816df8bae1dSRodney W. Grimes 			 * If a new connection request is received
1817df8bae1dSRodney W. Grimes 			 * while in TIME_WAIT, drop the old connection
1818df8bae1dSRodney W. Grimes 			 * and start over if the sequence numbers
1819df8bae1dSRodney W. Grimes 			 * are above the previous ones.
1820df8bae1dSRodney W. Grimes 			 */
1821340c35deSJonathan Lemon 			KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
1822fb59c426SYoshinobu Inoue 			if (thflags & TH_SYN &&
1823df8bae1dSRodney W. Grimes 			    tp->t_state == TCPS_TIME_WAIT &&
1824fb59c426SYoshinobu Inoue 			    SEQ_GT(th->th_seq, tp->rcv_nxt)) {
1825416738a7SRobert Watson 				KASSERT(headlocked, ("trimthenstep6: "
1826416738a7SRobert Watson 				    "tcp_close.4: head not locked"));
1827df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
1828df8bae1dSRodney W. Grimes 				goto findpcb;
1829df8bae1dSRodney W. Grimes 			}
1830df8bae1dSRodney W. Grimes 			/*
1831df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
1832df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
1833df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
1834df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
1835df8bae1dSRodney W. Grimes 			 * and ack.
1836df8bae1dSRodney W. Grimes 			 */
1837fb59c426SYoshinobu Inoue 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
1838df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
1839df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvwinprobe++;
1840df8bae1dSRodney W. Grimes 			} else
1841df8bae1dSRodney W. Grimes 				goto dropafterack;
1842df8bae1dSRodney W. Grimes 		} else
1843df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvbyteafterwin += todrop;
1844df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
1845fb59c426SYoshinobu Inoue 		tlen -= todrop;
1846fb59c426SYoshinobu Inoue 		thflags &= ~(TH_PUSH|TH_FIN);
1847df8bae1dSRodney W. Grimes 	}
1848df8bae1dSRodney W. Grimes 
1849df8bae1dSRodney W. Grimes 	/*
1850df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
1851df8bae1dSRodney W. Grimes 	 * record its timestamp.
1852cf09195bSPaul Saab 	 * NOTE:
1853cf09195bSPaul Saab 	 * 1) That the test incorporates suggestions from the latest
1854a0292f23SGarrett Wollman 	 *    proposal of the tcplw@cray.com list (Braden 1993/04/26).
1855cf09195bSPaul Saab 	 * 2) That updating only on newer timestamps interferes with
1856cf09195bSPaul Saab 	 *    our earlier PAWS tests, so this check should be solely
1857cf09195bSPaul Saab 	 *    predicated on the sequence space of this segment.
1858cf09195bSPaul Saab 	 * 3) That we modify the segment boundary check to be
1859cf09195bSPaul Saab 	 *        Last.ACK.Sent <= SEG.SEQ + SEG.Len
1860cf09195bSPaul Saab 	 *    instead of RFC1323's
1861cf09195bSPaul Saab 	 *        Last.ACK.Sent < SEG.SEQ + SEG.Len,
1862cf09195bSPaul Saab 	 *    This modified check allows us to overcome RFC1323's
1863cf09195bSPaul Saab 	 *    limitations as described in Stevens TCP/IP Illustrated
1864cf09195bSPaul Saab 	 *    Vol. 2 p.869. In such cases, we can still calculate the
1865cf09195bSPaul Saab 	 *    RTT correctly when RCV.NXT == Last.ACK.Sent.
1866df8bae1dSRodney W. Grimes 	 */
1867be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 &&
1868cf09195bSPaul Saab 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
1869cf09195bSPaul Saab 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
1870cf09195bSPaul Saab 		((thflags & (TH_SYN|TH_FIN)) != 0))) {
18719b8b58e0SJonathan Lemon 		tp->ts_recent_age = ticks;
1872a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
1873df8bae1dSRodney W. Grimes 	}
1874df8bae1dSRodney W. Grimes 
1875df8bae1dSRodney W. Grimes 	/*
1876df8bae1dSRodney W. Grimes 	 * If a SYN is in the window, then this is an
1877df8bae1dSRodney W. Grimes 	 * error and we send an RST and drop the connection.
1878df8bae1dSRodney W. Grimes 	 */
1879fb59c426SYoshinobu Inoue 	if (thflags & TH_SYN) {
18801e2d989dSRobert Watson 		KASSERT(headlocked, ("tcp_input: tcp_drop: trimthenstep6: "
18811e2d989dSRobert Watson 		    "head not locked"));
1882df8bae1dSRodney W. Grimes 		tp = tcp_drop(tp, ECONNRESET);
1883a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
1884122aad88SAndre Oppermann 		goto drop;
1885df8bae1dSRodney W. Grimes 	}
1886df8bae1dSRodney W. Grimes 
1887a0292f23SGarrett Wollman 	/*
1888a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
1889a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
1890a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
1891a0292f23SGarrett Wollman 	 */
1892fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) == 0) {
1893a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
1894a0292f23SGarrett Wollman 		    (tp->t_flags & TF_NEEDSYN))
1895a0292f23SGarrett Wollman 			goto step6;
18965e1aa279SDavid Malone 		else if (tp->t_flags & TF_ACKNOW)
18975e1aa279SDavid Malone 			goto dropafterack;
1898a0292f23SGarrett Wollman 		else
1899a0292f23SGarrett Wollman 			goto drop;
1900a0292f23SGarrett Wollman 	}
1901df8bae1dSRodney W. Grimes 
1902df8bae1dSRodney W. Grimes 	/*
1903df8bae1dSRodney W. Grimes 	 * Ack processing.
1904df8bae1dSRodney W. Grimes 	 */
1905df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1906df8bae1dSRodney W. Grimes 
1907df8bae1dSRodney W. Grimes 	/*
1908764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
1909764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
1910764d8cefSBill Fenner 	 * The ACK was checked above.
1911df8bae1dSRodney W. Grimes 	 */
1912df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
1913a0292f23SGarrett Wollman 
1914df8bae1dSRodney W. Grimes 		tcpstat.tcps_connects++;
1915df8bae1dSRodney W. Grimes 		soisconnected(so);
1916df8bae1dSRodney W. Grimes 		/* Do window scaling? */
1917df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1918df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1919df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
1920464fcfbcSAndre Oppermann 			tp->snd_wnd = tiwin;
1921df8bae1dSRodney W. Grimes 		}
1922a0292f23SGarrett Wollman 		/*
1923a0292f23SGarrett Wollman 		 * Make transitions:
1924a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
1925a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
1926a0292f23SGarrett Wollman 		 */
19279b8b58e0SJonathan Lemon 		tp->t_starttime = ticks;
1928a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDFIN) {
1929a0292f23SGarrett Wollman 			tp->t_state = TCPS_FIN_WAIT_1;
1930a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDFIN;
19317ff19458SPaul Traina 		} else {
1932a0292f23SGarrett Wollman 			tp->t_state = TCPS_ESTABLISHED;
19339b8b58e0SJonathan Lemon 			callout_reset(tp->tt_keep, tcp_keepidle,
19349b8b58e0SJonathan Lemon 				      tcp_timer_keep, tp);
19357ff19458SPaul Traina 		}
1936a0292f23SGarrett Wollman 		/*
1937a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
1938a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
1939a0292f23SGarrett Wollman 		 */
1940fb59c426SYoshinobu Inoue 		if (tlen == 0 && (thflags & TH_FIN) == 0)
1941fb59c426SYoshinobu Inoue 			(void) tcp_reass(tp, (struct tcphdr *)0, 0,
1942a0292f23SGarrett Wollman 			    (struct mbuf *)0);
1943fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
194493b0017fSPhilippe Charnier 		/* FALLTHROUGH */
1945df8bae1dSRodney W. Grimes 
1946df8bae1dSRodney W. Grimes 	/*
1947df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1948df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
1949fb59c426SYoshinobu Inoue 	 *	tp->snd_una < th->th_ack <= tp->snd_max
1950fb59c426SYoshinobu Inoue 	 * then advance tp->snd_una to th->th_ack and drop
1951df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
1952df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
1953df8bae1dSRodney W. Grimes 	 */
1954df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
1955df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
1956df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
1957df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
1958df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
1959df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
1960df8bae1dSRodney W. Grimes 	case TCPS_TIME_WAIT:
1961340c35deSJonathan Lemon 		KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
19625a53ca16SPaul Saab 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
19635a53ca16SPaul Saab 			tcpstat.tcps_rcvacktoomuch++;
19645a53ca16SPaul Saab 			goto dropafterack;
19655a53ca16SPaul Saab 		}
1966482ac968SPaul Saab 		if (tp->sack_enable &&
1967482ac968SPaul Saab 		    (to.to_nsacks > 0 || !TAILQ_EMPTY(&tp->snd_holes)))
19685a53ca16SPaul Saab 			tcp_sack_doack(tp, &to, th->th_ack);
1969fb59c426SYoshinobu Inoue 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
1970fb59c426SYoshinobu Inoue 			if (tlen == 0 && tiwin == tp->snd_wnd) {
1971df8bae1dSRodney W. Grimes 				tcpstat.tcps_rcvdupack++;
1972df8bae1dSRodney W. Grimes 				/*
1973df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
1974df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
1975df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
1976df8bae1dSRodney W. Grimes 				 * change), the ack is the biggest we've
1977df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
1978df8bae1dSRodney W. Grimes 				 * threshhold of them, assume a packet
1979df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
1980df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
1981df8bae1dSRodney W. Grimes 				 * window so we send only this one
1982df8bae1dSRodney W. Grimes 				 * packet.
1983df8bae1dSRodney W. Grimes 				 *
1984df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
1985df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
1986df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
1987df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
1988df8bae1dSRodney W. Grimes 				 * the new ssthresh).
1989df8bae1dSRodney W. Grimes 				 *
1990df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
1991df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
1992df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
1993df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
1994df8bae1dSRodney W. Grimes 				 * network.
1995df8bae1dSRodney W. Grimes 				 */
19969b8b58e0SJonathan Lemon 				if (!callout_active(tp->tt_rexmt) ||
1997fb59c426SYoshinobu Inoue 				    th->th_ack != tp->snd_una)
1998df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
1999cb942153SJeffrey Hsu 				else if (++tp->t_dupacks > tcprexmtthresh ||
20006d90faf3SPaul Saab 					 ((tcp_do_newreno || tp->sack_enable) &&
20019d11646dSJeffrey Hsu 					  IN_FASTRECOVERY(tp))) {
200225e6f9edSPaul Saab                                         if (tp->sack_enable && IN_FASTRECOVERY(tp)) {
2003808f11b7SPaul Saab 						int awnd;
200425e6f9edSPaul Saab 
200525e6f9edSPaul Saab 						/*
200625e6f9edSPaul Saab 						 * Compute the amount of data in flight first.
200725e6f9edSPaul Saab 						 * We can inject new data into the pipe iff
200825e6f9edSPaul Saab 						 * we have less than 1/2 the original window's
200925e6f9edSPaul Saab 						 * worth of data in flight.
201025e6f9edSPaul Saab 						 */
2011808f11b7SPaul Saab 						awnd = (tp->snd_nxt - tp->snd_fack) +
20120077b016SPaul Saab 							tp->sackhint.sack_bytes_rexmit;
2013808f11b7SPaul Saab 						if (awnd < tp->snd_ssthresh) {
201425e6f9edSPaul Saab 							tp->snd_cwnd += tp->t_maxseg;
201525e6f9edSPaul Saab 							if (tp->snd_cwnd > tp->snd_ssthresh)
201625e6f9edSPaul Saab 								tp->snd_cwnd = tp->snd_ssthresh;
201725e6f9edSPaul Saab 						}
201825e6f9edSPaul Saab 					} else
201946f58482SJonathan Lemon 						tp->snd_cwnd += tp->t_maxseg;
202046f58482SJonathan Lemon 					(void) tcp_output(tp);
202146f58482SJonathan Lemon 					goto drop;
2022cb942153SJeffrey Hsu 				} else if (tp->t_dupacks == tcprexmtthresh) {
2023cb942153SJeffrey Hsu 					tcp_seq onxt = tp->snd_nxt;
2024cb942153SJeffrey Hsu 					u_int win;
2025a0445c2eSJayanth Vijayaraghavan 
2026a0445c2eSJayanth Vijayaraghavan 					/*
2027a0445c2eSJayanth Vijayaraghavan 					 * If we're doing sack, check to
2028a0445c2eSJayanth Vijayaraghavan 					 * see if we're already in sack
2029a0445c2eSJayanth Vijayaraghavan 					 * recovery. If we're not doing sack,
2030a0445c2eSJayanth Vijayaraghavan 					 * check to see if we're in newreno
2031a0445c2eSJayanth Vijayaraghavan 					 * recovery.
2032a0445c2eSJayanth Vijayaraghavan 					 */
2033a0445c2eSJayanth Vijayaraghavan 					if (tp->sack_enable) {
2034a0445c2eSJayanth Vijayaraghavan 						if (IN_FASTRECOVERY(tp)) {
2035a0445c2eSJayanth Vijayaraghavan 							tp->t_dupacks = 0;
2036a0445c2eSJayanth Vijayaraghavan 							break;
2037a0445c2eSJayanth Vijayaraghavan 						}
2038a0445c2eSJayanth Vijayaraghavan 					} else if (tcp_do_newreno) {
2039a0445c2eSJayanth Vijayaraghavan 						if (SEQ_LEQ(th->th_ack,
20409d11646dSJeffrey Hsu 						    tp->snd_recover)) {
2041cb942153SJeffrey Hsu 							tp->t_dupacks = 0;
2042cb942153SJeffrey Hsu 							break;
204346f58482SJonathan Lemon 						}
2044a0445c2eSJayanth Vijayaraghavan 					}
2045cb942153SJeffrey Hsu 					win = min(tp->snd_wnd, tp->snd_cwnd) /
2046cb942153SJeffrey Hsu 					    2 / tp->t_maxseg;
2047df8bae1dSRodney W. Grimes 					if (win < 2)
2048df8bae1dSRodney W. Grimes 						win = 2;
2049df8bae1dSRodney W. Grimes 					tp->snd_ssthresh = win * tp->t_maxseg;
20509d11646dSJeffrey Hsu 					ENTER_FASTRECOVERY(tp);
205146f58482SJonathan Lemon 					tp->snd_recover = tp->snd_max;
20529b8b58e0SJonathan Lemon 					callout_stop(tp->tt_rexmt);
20539b8b58e0SJonathan Lemon 					tp->t_rtttime = 0;
20546d90faf3SPaul Saab 					if (tp->sack_enable) {
20556d90faf3SPaul Saab 						tcpstat.tcps_sack_recovery_episode++;
2056a55db2b6SPaul Saab 						tp->sack_newdata = tp->snd_nxt;
20578db456bfSPaul Saab 						tp->snd_cwnd = tp->t_maxseg;
20586d90faf3SPaul Saab 						(void) tcp_output(tp);
20596d90faf3SPaul Saab 						goto drop;
20606d90faf3SPaul Saab 					}
2061fb59c426SYoshinobu Inoue 					tp->snd_nxt = th->th_ack;
2062df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->t_maxseg;
2063df8bae1dSRodney W. Grimes 					(void) tcp_output(tp);
206448d2549cSJeffrey Hsu 					KASSERT(tp->snd_limited <= 2,
206548d2549cSJeffrey Hsu 					    ("tp->snd_limited too big"));
2066df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
206748d2549cSJeffrey Hsu 					     tp->t_maxseg *
206848d2549cSJeffrey Hsu 					     (tp->t_dupacks - tp->snd_limited);
2069df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
2070df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
2071df8bae1dSRodney W. Grimes 					goto drop;
2072582a954bSJeffrey Hsu 				} else if (tcp_do_rfc3042) {
2073582a954bSJeffrey Hsu 					u_long oldcwnd = tp->snd_cwnd;
207448d2549cSJeffrey Hsu 					tcp_seq oldsndmax = tp->snd_max;
207548d2549cSJeffrey Hsu 					u_int sent;
207689c02376SJeffrey Hsu 
2077582a954bSJeffrey Hsu 					KASSERT(tp->t_dupacks == 1 ||
2078582a954bSJeffrey Hsu 					    tp->t_dupacks == 2,
2079582a954bSJeffrey Hsu 					    ("dupacks not 1 or 2"));
208061a36e3dSJeffrey Hsu 					if (tp->t_dupacks == 1)
208148d2549cSJeffrey Hsu 						tp->snd_limited = 0;
208261a36e3dSJeffrey Hsu 					tp->snd_cwnd =
208361a36e3dSJeffrey Hsu 					    (tp->snd_nxt - tp->snd_una) +
208461a36e3dSJeffrey Hsu 					    (tp->t_dupacks - tp->snd_limited) *
208561a36e3dSJeffrey Hsu 					    tp->t_maxseg;
2086582a954bSJeffrey Hsu 					(void) tcp_output(tp);
208748d2549cSJeffrey Hsu 					sent = tp->snd_max - oldsndmax;
208848d2549cSJeffrey Hsu 					if (sent > tp->t_maxseg) {
208989c02376SJeffrey Hsu 						KASSERT((tp->t_dupacks == 2 &&
209089c02376SJeffrey Hsu 						    tp->snd_limited == 0) ||
209189c02376SJeffrey Hsu 						   (sent == tp->t_maxseg + 1 &&
209289c02376SJeffrey Hsu 						    tp->t_flags & TF_SENTFIN),
209348d2549cSJeffrey Hsu 						    ("sent too much"));
209448d2549cSJeffrey Hsu 						tp->snd_limited = 2;
209548d2549cSJeffrey Hsu 					} else if (sent > 0)
209648d2549cSJeffrey Hsu 						++tp->snd_limited;
2097582a954bSJeffrey Hsu 					tp->snd_cwnd = oldcwnd;
2098582a954bSJeffrey Hsu 					goto drop;
2099df8bae1dSRodney W. Grimes 				}
2100df8bae1dSRodney W. Grimes 			} else
2101df8bae1dSRodney W. Grimes 				tp->t_dupacks = 0;
2102df8bae1dSRodney W. Grimes 			break;
2103df8bae1dSRodney W. Grimes 		}
2104cb942153SJeffrey Hsu 
2105cb942153SJeffrey Hsu 		KASSERT(SEQ_GT(th->th_ack, tp->snd_una), ("th_ack <= snd_una"));
2106cb942153SJeffrey Hsu 
2107df8bae1dSRodney W. Grimes 		/*
2108df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
2109df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
2110df8bae1dSRodney W. Grimes 		 */
21116d90faf3SPaul Saab 		if (tcp_do_newreno || tp->sack_enable) {
21129d11646dSJeffrey Hsu 			if (IN_FASTRECOVERY(tp)) {
2113cb942153SJeffrey Hsu 				if (SEQ_LT(th->th_ack, tp->snd_recover)) {
21146d90faf3SPaul Saab 					if (tp->sack_enable)
21156d90faf3SPaul Saab 						tcp_sack_partialack(tp, th);
21166d90faf3SPaul Saab 					else
2117c068736aSJeffrey Hsu 						tcp_newreno_partial_ack(tp, th);
2118c068736aSJeffrey Hsu 				} else {
2119c068736aSJeffrey Hsu 					/*
21206d90faf3SPaul Saab 					 * Out of fast recovery.
2121c068736aSJeffrey Hsu 					 * Window inflation should have left us
2122c068736aSJeffrey Hsu 					 * with approximately snd_ssthresh
2123c068736aSJeffrey Hsu 					 * outstanding data.
2124c068736aSJeffrey Hsu 					 * But in case we would be inclined to
2125c068736aSJeffrey Hsu 					 * send a burst, better to do it via
2126c068736aSJeffrey Hsu 					 * the slow start mechanism.
2127c068736aSJeffrey Hsu 					 */
2128c068736aSJeffrey Hsu 					if (SEQ_GT(th->th_ack +
2129c068736aSJeffrey Hsu 							tp->snd_ssthresh,
2130c068736aSJeffrey Hsu 						   tp->snd_max))
2131c068736aSJeffrey Hsu 						tp->snd_cwnd = tp->snd_max -
2132c068736aSJeffrey Hsu 								th->th_ack +
2133c068736aSJeffrey Hsu 								tp->t_maxseg;
2134c068736aSJeffrey Hsu 					else
2135c068736aSJeffrey Hsu 						tp->snd_cwnd = tp->snd_ssthresh;
2136c068736aSJeffrey Hsu 				}
2137c068736aSJeffrey Hsu 			}
2138c068736aSJeffrey Hsu 		} else {
2139233e8c18SGarrett Wollman 			if (tp->t_dupacks >= tcprexmtthresh &&
2140df8bae1dSRodney W. Grimes 			    tp->snd_cwnd > tp->snd_ssthresh)
2141df8bae1dSRodney W. Grimes 				tp->snd_cwnd = tp->snd_ssthresh;
214246f58482SJonathan Lemon 		}
2143cb942153SJeffrey Hsu 		tp->t_dupacks = 0;
2144a0292f23SGarrett Wollman 		/*
2145a0292f23SGarrett Wollman 		 * If we reach this point, ACK is not a duplicate,
2146a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
2147a0292f23SGarrett Wollman 		 */
2148a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
2149a0292f23SGarrett Wollman 			/*
2150a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
2151a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
215207e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
215307e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
215407e43e10SAndras Olah 			 * we can do window scaling.
2155a0292f23SGarrett Wollman 			 */
2156a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
2157a0292f23SGarrett Wollman 			tp->snd_una++;
215807e43e10SAndras Olah 			/* Do window scaling? */
215907e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
216007e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
216107e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2162464fcfbcSAndre Oppermann 				/* Send window already scaled. */
216307e43e10SAndras Olah 			}
2164a0292f23SGarrett Wollman 		}
2165a0292f23SGarrett Wollman 
2166a0292f23SGarrett Wollman process_ACK:
2167de30ea13SRobert Watson 		KASSERT(headlocked, ("tcp_input: process_ACK: head not "
2168de30ea13SRobert Watson 		    "locked"));
21697cfc6904SRobert Watson 		INP_LOCK_ASSERT(inp);
21707cfc6904SRobert Watson 
2171fb59c426SYoshinobu Inoue 		acked = th->th_ack - tp->snd_una;
2172df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvackpack++;
2173df8bae1dSRodney W. Grimes 		tcpstat.tcps_rcvackbyte += acked;
2174df8bae1dSRodney W. Grimes 
2175df8bae1dSRodney W. Grimes 		/*
21769b8b58e0SJonathan Lemon 		 * If we just performed our first retransmit, and the ACK
21779b8b58e0SJonathan Lemon 		 * arrives within our recovery window, then it was a mistake
21789b8b58e0SJonathan Lemon 		 * to do the retransmit in the first place.  Recover our
21799b8b58e0SJonathan Lemon 		 * original cwnd and ssthresh, and proceed to transmit where
21809b8b58e0SJonathan Lemon 		 * we left off.
21819b8b58e0SJonathan Lemon 		 */
21829b8b58e0SJonathan Lemon 		if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) {
2183d65bf08aSMatthew Dillon 			++tcpstat.tcps_sndrexmitbad;
21849b8b58e0SJonathan Lemon 			tp->snd_cwnd = tp->snd_cwnd_prev;
21859b8b58e0SJonathan Lemon 			tp->snd_ssthresh = tp->snd_ssthresh_prev;
21869d11646dSJeffrey Hsu 			tp->snd_recover = tp->snd_recover_prev;
21879d11646dSJeffrey Hsu 			if (tp->t_flags & TF_WASFRECOVERY)
21889d11646dSJeffrey Hsu 				ENTER_FASTRECOVERY(tp);
21899b8b58e0SJonathan Lemon 			tp->snd_nxt = tp->snd_max;
21909b8b58e0SJonathan Lemon 			tp->t_badrxtwin = 0;	/* XXX probably not required */
21919b8b58e0SJonathan Lemon 		}
21929b8b58e0SJonathan Lemon 
21939b8b58e0SJonathan Lemon 		/*
2194df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
2195df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
2196df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
2197df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
2198df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
2199df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
2200df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
2201fa55172bSMatthew Dillon 		 *
2202fa55172bSMatthew Dillon 		 * Some boxes send broken timestamp replies
2203fa55172bSMatthew Dillon 		 * during the SYN+ACK phase, ignore
2204fa55172bSMatthew Dillon 		 * timestamps of 0 or we could calculate a
2205fa55172bSMatthew Dillon 		 * huge RTT and blow up the retransmit timer.
2206df8bae1dSRodney W. Grimes 		 */
2207fa55172bSMatthew Dillon 		if ((to.to_flags & TOF_TS) != 0 &&
2208fa55172bSMatthew Dillon 		    to.to_tsecr) {
2209eaf80179SAndre Oppermann 			if (!tp->t_rttlow || tp->t_rttlow > ticks - to.to_tsecr)
2210eaf80179SAndre Oppermann 				tp->t_rttlow = ticks - to.to_tsecr;
22119b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - to.to_tsecr + 1);
2212fa55172bSMatthew Dillon 		} else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
2213eaf80179SAndre Oppermann 			if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime)
2214eaf80179SAndre Oppermann 				tp->t_rttlow = ticks - tp->t_rtttime;
22159b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
2216fa55172bSMatthew Dillon 		}
22171fcc99b5SMatthew Dillon 		tcp_xmit_bandwidth_limit(tp, th->th_ack);
2218df8bae1dSRodney W. Grimes 
2219df8bae1dSRodney W. Grimes 		/*
2220df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
2221df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
2222df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
2223df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
2224df8bae1dSRodney W. Grimes 		 */
2225fb59c426SYoshinobu Inoue 		if (th->th_ack == tp->snd_max) {
22269b8b58e0SJonathan Lemon 			callout_stop(tp->tt_rexmt);
2227df8bae1dSRodney W. Grimes 			needoutput = 1;
22289b8b58e0SJonathan Lemon 		} else if (!callout_active(tp->tt_persist))
22299b8b58e0SJonathan Lemon 			callout_reset(tp->tt_rexmt, tp->t_rxtcur,
22309b8b58e0SJonathan Lemon 				      tcp_timer_rexmt, tp);
2231a0292f23SGarrett Wollman 
2232a0292f23SGarrett Wollman 		/*
2233a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
2234a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
2235a0292f23SGarrett Wollman 		 */
2236a0292f23SGarrett Wollman 		if (acked == 0)
2237a0292f23SGarrett Wollman 			goto step6;
2238a0292f23SGarrett Wollman 
2239df8bae1dSRodney W. Grimes 		/*
2240df8bae1dSRodney W. Grimes 		 * When new data is acked, open the congestion window.
2241df8bae1dSRodney W. Grimes 		 * If the window gives us less than ssthresh packets
2242df8bae1dSRodney W. Grimes 		 * in flight, open exponentially (maxseg per packet).
2243df8bae1dSRodney W. Grimes 		 * Otherwise open linearly: maxseg per window
224410be5648SGarrett Wollman 		 * (maxseg^2 / cwnd per packet).
2245df8bae1dSRodney W. Grimes 		 */
22466d90faf3SPaul Saab 		if ((!tcp_do_newreno && !tp->sack_enable) ||
22476d90faf3SPaul Saab 		    !IN_FASTRECOVERY(tp)) {
2248df8bae1dSRodney W. Grimes 			register u_int cw = tp->snd_cwnd;
2249df8bae1dSRodney W. Grimes 			register u_int incr = tp->t_maxseg;
2250df8bae1dSRodney W. Grimes 			if (cw > tp->snd_ssthresh)
225110be5648SGarrett Wollman 				incr = incr * incr / cw;
2252df8bae1dSRodney W. Grimes 			tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale);
2253df8bae1dSRodney W. Grimes 		}
22545905999bSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
2255df8bae1dSRodney W. Grimes 		if (acked > so->so_snd.sb_cc) {
2256df8bae1dSRodney W. Grimes 			tp->snd_wnd -= so->so_snd.sb_cc;
22575905999bSRobert Watson 			sbdrop_locked(&so->so_snd, (int)so->so_snd.sb_cc);
2258df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
2259df8bae1dSRodney W. Grimes 		} else {
22605905999bSRobert Watson 			sbdrop_locked(&so->so_snd, acked);
2261df8bae1dSRodney W. Grimes 			tp->snd_wnd -= acked;
2262df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
2263df8bae1dSRodney W. Grimes 		}
22641e4d7da7SRobert Watson 		sowwakeup_locked(so);
2265cb942153SJeffrey Hsu 		/* detect una wraparound */
22666d90faf3SPaul Saab 		if ((tcp_do_newreno || tp->sack_enable) &&
22676d90faf3SPaul Saab 		    !IN_FASTRECOVERY(tp) &&
22689d11646dSJeffrey Hsu 		    SEQ_GT(tp->snd_una, tp->snd_recover) &&
22699d11646dSJeffrey Hsu 		    SEQ_LEQ(th->th_ack, tp->snd_recover))
22709d11646dSJeffrey Hsu 			tp->snd_recover = th->th_ack - 1;
22716d90faf3SPaul Saab 		if ((tcp_do_newreno || tp->sack_enable) &&
22726d90faf3SPaul Saab 		    IN_FASTRECOVERY(tp) &&
22739d11646dSJeffrey Hsu 		    SEQ_GEQ(th->th_ack, tp->snd_recover))
22749d11646dSJeffrey Hsu 			EXIT_FASTRECOVERY(tp);
2275fb59c426SYoshinobu Inoue 		tp->snd_una = th->th_ack;
22766d90faf3SPaul Saab 		if (tp->sack_enable) {
22776d90faf3SPaul Saab 			if (SEQ_GT(tp->snd_una, tp->snd_recover))
22786d90faf3SPaul Saab 				tp->snd_recover = tp->snd_una;
22796d90faf3SPaul Saab 		}
2280df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2281df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
2282df8bae1dSRodney W. Grimes 
2283df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2284df8bae1dSRodney W. Grimes 
2285df8bae1dSRodney W. Grimes 		/*
2286df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
2287df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
2288df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
2289df8bae1dSRodney W. Grimes 		 */
2290df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2291df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2292df8bae1dSRodney W. Grimes 				/*
2293df8bae1dSRodney W. Grimes 				 * If we can't receive any more
2294df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
2295df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
2296df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
2297df8bae1dSRodney W. Grimes 				 * we'll hang forever.
2298df8bae1dSRodney W. Grimes 				 */
2299340c35deSJonathan Lemon 		/* XXXjl
2300340c35deSJonathan Lemon 		 * we should release the tp also, and use a
2301340c35deSJonathan Lemon 		 * compressed state.
2302340c35deSJonathan Lemon 		 */
2303c0b99ffaSRobert Watson 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
230403e49181SSeigo Tanimura 					soisdisconnected(so);
23059b8b58e0SJonathan Lemon 					callout_reset(tp->tt_2msl, tcp_maxidle,
23069b8b58e0SJonathan Lemon 						      tcp_timer_2msl, tp);
23074cc20ab1SSeigo Tanimura 				}
2308df8bae1dSRodney W. Grimes 				tp->t_state = TCPS_FIN_WAIT_2;
2309df8bae1dSRodney W. Grimes 			}
2310df8bae1dSRodney W. Grimes 			break;
2311df8bae1dSRodney W. Grimes 
2312df8bae1dSRodney W. Grimes 		/*
2313df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
2314df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
2315df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
2316df8bae1dSRodney W. Grimes 		 * the segment.
2317df8bae1dSRodney W. Grimes 		 */
2318df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
2319df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2320de30ea13SRobert Watson 				KASSERT(headlocked, ("tcp_input: process_ACK: "
2321de30ea13SRobert Watson 				    "head not locked"));
232211a20fb8SJeffrey Hsu 				tcp_twstart(tp);
2323340c35deSJonathan Lemon 				INP_INFO_WUNLOCK(&tcbinfo);
2324340c35deSJonathan Lemon 				m_freem(m);
2325340c35deSJonathan Lemon 				return;
2326df8bae1dSRodney W. Grimes 			}
2327df8bae1dSRodney W. Grimes 			break;
2328df8bae1dSRodney W. Grimes 
2329df8bae1dSRodney W. Grimes 		/*
2330df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
2331df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
2332df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
2333df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
2334df8bae1dSRodney W. Grimes 		 */
2335df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
2336df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2337416738a7SRobert Watson 				KASSERT(headlocked, ("tcp_input: process_ACK:"
2338416738a7SRobert Watson 				    " tcp_close: head not locked"));
2339df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
2340df8bae1dSRodney W. Grimes 				goto drop;
2341df8bae1dSRodney W. Grimes 			}
2342df8bae1dSRodney W. Grimes 			break;
2343df8bae1dSRodney W. Grimes 
2344df8bae1dSRodney W. Grimes 		/*
2345df8bae1dSRodney W. Grimes 		 * In TIME_WAIT state the only thing that should arrive
2346df8bae1dSRodney W. Grimes 		 * is a retransmission of the remote FIN.  Acknowledge
2347df8bae1dSRodney W. Grimes 		 * it and restart the finack timer.
2348df8bae1dSRodney W. Grimes 		 */
2349df8bae1dSRodney W. Grimes 		case TCPS_TIME_WAIT:
2350340c35deSJonathan Lemon 			KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
23519b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, 2 * tcp_msl,
23529b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
2353df8bae1dSRodney W. Grimes 			goto dropafterack;
2354df8bae1dSRodney W. Grimes 		}
2355df8bae1dSRodney W. Grimes 	}
2356df8bae1dSRodney W. Grimes 
2357df8bae1dSRodney W. Grimes step6:
2358de30ea13SRobert Watson 	KASSERT(headlocked, ("tcp_input: step6: head not locked"));
23597cfc6904SRobert Watson 	INP_LOCK_ASSERT(inp);
23607cfc6904SRobert Watson 
2361df8bae1dSRodney W. Grimes 	/*
2362df8bae1dSRodney W. Grimes 	 * Update window information.
2363df8bae1dSRodney W. Grimes 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2364df8bae1dSRodney W. Grimes 	 */
2365fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) &&
2366fb59c426SYoshinobu Inoue 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
2367fb59c426SYoshinobu Inoue 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
2368fb59c426SYoshinobu Inoue 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
2369df8bae1dSRodney W. Grimes 		/* keep track of pure window updates */
2370fb59c426SYoshinobu Inoue 		if (tlen == 0 &&
2371fb59c426SYoshinobu Inoue 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
2372df8bae1dSRodney W. Grimes 			tcpstat.tcps_rcvwinupd++;
2373df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
2374fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq;
2375fb59c426SYoshinobu Inoue 		tp->snd_wl2 = th->th_ack;
2376df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
2377df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
2378df8bae1dSRodney W. Grimes 		needoutput = 1;
2379df8bae1dSRodney W. Grimes 	}
2380df8bae1dSRodney W. Grimes 
2381df8bae1dSRodney W. Grimes 	/*
2382df8bae1dSRodney W. Grimes 	 * Process segments with URG.
2383df8bae1dSRodney W. Grimes 	 */
2384fb59c426SYoshinobu Inoue 	if ((thflags & TH_URG) && th->th_urp &&
2385df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2386df8bae1dSRodney W. Grimes 		/*
2387df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
2388df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
2389df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
2390df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
2391df8bae1dSRodney W. Grimes 		 */
239218ad5842SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
2393fb59c426SYoshinobu Inoue 		if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
2394fb59c426SYoshinobu Inoue 			th->th_urp = 0;			/* XXX */
2395fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;		/* XXX */
239618ad5842SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);	/* XXX */
2397df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
2398df8bae1dSRodney W. Grimes 		}
2399df8bae1dSRodney W. Grimes 		/*
2400df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
2401df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
2402df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2403df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
2404df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
2405df8bae1dSRodney W. Grimes 		 *
2406df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
2407df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
2408df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
2409df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
2410df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
2411df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
2412df8bae1dSRodney W. Grimes 		 */
2413fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
2414fb59c426SYoshinobu Inoue 			tp->rcv_up = th->th_seq + th->th_urp;
2415df8bae1dSRodney W. Grimes 			so->so_oobmark = so->so_rcv.sb_cc +
2416df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
2417927c5ceaSRobert Watson 			if (so->so_oobmark == 0)
2418c0b99ffaSRobert Watson 				so->so_rcv.sb_state |= SBS_RCVATMARK;
2419df8bae1dSRodney W. Grimes 			sohasoutofband(so);
2420df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2421df8bae1dSRodney W. Grimes 		}
242218ad5842SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
2423df8bae1dSRodney W. Grimes 		/*
2424df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
2425df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
2426df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
2427df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
2428df8bae1dSRodney W. Grimes 		 */
242930613f56SJeffrey Hsu 		if (th->th_urp <= (u_long)tlen &&
243030613f56SJeffrey Hsu 		    !(so->so_options & SO_OOBINLINE)) {
243130613f56SJeffrey Hsu 			/* hdr drop is delayed */
243230613f56SJeffrey Hsu 			tcp_pulloutofband(so, th, m, drop_hdrlen);
243330613f56SJeffrey Hsu 		}
2434c068736aSJeffrey Hsu 	} else {
2435df8bae1dSRodney W. Grimes 		/*
2436df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
2437df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
2438df8bae1dSRodney W. Grimes 		 * with the receive window.
2439df8bae1dSRodney W. Grimes 		 */
2440df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
2441df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
2442c068736aSJeffrey Hsu 	}
2443df8bae1dSRodney W. Grimes dodata:							/* XXX */
2444de30ea13SRobert Watson 	KASSERT(headlocked, ("tcp_input: dodata: head not locked"));
24457cfc6904SRobert Watson 	INP_LOCK_ASSERT(inp);
24467cfc6904SRobert Watson 
2447df8bae1dSRodney W. Grimes 	/*
2448df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
2449df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
2450df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
2451df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
2452df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
2453df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
2454df8bae1dSRodney W. Grimes 	 */
2455fb59c426SYoshinobu Inoue 	if ((tlen || (thflags & TH_FIN)) &&
2456df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
245769e03620SPaul Saab 		tcp_seq save_start = th->th_seq;
245869e03620SPaul Saab 		tcp_seq save_end = th->th_seq + tlen;
2459fb59c426SYoshinobu Inoue 		m_adj(m, drop_hdrlen);	/* delayed header drop */
2460e4b64281SJesper Skriver 		/*
2461c068736aSJeffrey Hsu 		 * Insert segment which includes th into TCP reassembly queue
2462c068736aSJeffrey Hsu 		 * with control block tp.  Set thflags to whether reassembly now
2463c068736aSJeffrey Hsu 		 * includes a segment with FIN.  This handles the common case
2464c068736aSJeffrey Hsu 		 * inline (segment is the next to be received on an established
2465c068736aSJeffrey Hsu 		 * connection, and the queue is empty), avoiding linkage into
2466c068736aSJeffrey Hsu 		 * and removal from the queue and repetition of various
2467c068736aSJeffrey Hsu 		 * conversions.
2468c068736aSJeffrey Hsu 		 * Set DELACK for segments received in order, but ack
2469c068736aSJeffrey Hsu 		 * immediately when segments are out of order (so
2470c068736aSJeffrey Hsu 		 * fast retransmit can work).
2471e4b64281SJesper Skriver 		 */
2472e4b64281SJesper Skriver 		if (th->th_seq == tp->rcv_nxt &&
2473e4b64281SJesper Skriver 		    LIST_EMPTY(&tp->t_segq) &&
2474e4b64281SJesper Skriver 		    TCPS_HAVEESTABLISHED(tp->t_state)) {
2475e4b64281SJesper Skriver 			if (DELAY_ACK(tp))
24763bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
2477e4b64281SJesper Skriver 			else
2478e4b64281SJesper Skriver 				tp->t_flags |= TF_ACKNOW;
2479e4b64281SJesper Skriver 			tp->rcv_nxt += tlen;
2480e4b64281SJesper Skriver 			thflags = th->th_flags & TH_FIN;
2481e4b64281SJesper Skriver 			tcpstat.tcps_rcvpack++;
2482e4b64281SJesper Skriver 			tcpstat.tcps_rcvbyte += tlen;
2483e4b64281SJesper Skriver 			ND6_HINT(tp);
24841e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
2485c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
2486c1c36a2cSMike Silbersack 				m_freem(m);
2487c1c36a2cSMike Silbersack 			else
24881e4d7da7SRobert Watson 				sbappendstream_locked(&so->so_rcv, m);
24891e4d7da7SRobert Watson 			sorwakeup_locked(so);
2490e4b64281SJesper Skriver 		} else {
2491e4b64281SJesper Skriver 			thflags = tcp_reass(tp, th, &tlen, m);
2492e4b64281SJesper Skriver 			tp->t_flags |= TF_ACKNOW;
2493e4b64281SJesper Skriver 		}
2494e346eeffSPaul Saab 		if (tlen > 0 && tp->sack_enable)
249569e03620SPaul Saab 			tcp_update_sack_list(tp, save_start, save_end);
2496df8bae1dSRodney W. Grimes 		/*
2497df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
2498df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
2499df8bae1dSRodney W. Grimes 		 * buffer size.
2500df8bae1dSRodney W. Grimes 		 */
2501df8bae1dSRodney W. Grimes 		len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
2502df8bae1dSRodney W. Grimes 	} else {
2503df8bae1dSRodney W. Grimes 		m_freem(m);
2504fb59c426SYoshinobu Inoue 		thflags &= ~TH_FIN;
2505df8bae1dSRodney W. Grimes 	}
2506df8bae1dSRodney W. Grimes 
2507df8bae1dSRodney W. Grimes 	/*
2508df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
2509df8bae1dSRodney W. Grimes 	 * that the connection is closing.
2510df8bae1dSRodney W. Grimes 	 */
2511fb59c426SYoshinobu Inoue 	if (thflags & TH_FIN) {
2512df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2513df8bae1dSRodney W. Grimes 			socantrcvmore(so);
2514a0292f23SGarrett Wollman 			/*
2515a0292f23SGarrett Wollman 			 * If connection is half-synchronized
2516d3eede9dSAndras Olah 			 * (ie NEEDSYN flag on) then delay ACK,
2517a0292f23SGarrett Wollman 			 * so it may be piggybacked when SYN is sent.
2518a0292f23SGarrett Wollman 			 * Otherwise, since we received a FIN then no
2519a0292f23SGarrett Wollman 			 * more input can be expected, send ACK now.
2520a0292f23SGarrett Wollman 			 */
25213bfd6421SJonathan Lemon 			if (tp->t_flags & TF_NEEDSYN)
25223bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
2523a0292f23SGarrett Wollman 			else
2524df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
2525df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
2526df8bae1dSRodney W. Grimes 		}
2527df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2528df8bae1dSRodney W. Grimes 
2529df8bae1dSRodney W. Grimes 		/*
2530df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
2531df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
2532df8bae1dSRodney W. Grimes 		 */
2533df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
25349b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
25359b8b58e0SJonathan Lemon 			/*FALLTHROUGH*/
2536df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
2537df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSE_WAIT;
2538df8bae1dSRodney W. Grimes 			break;
2539df8bae1dSRodney W. Grimes 
2540df8bae1dSRodney W. Grimes 		/*
2541df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2542df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
2543df8bae1dSRodney W. Grimes 		 */
2544df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2545df8bae1dSRodney W. Grimes 			tp->t_state = TCPS_CLOSING;
2546df8bae1dSRodney W. Grimes 			break;
2547df8bae1dSRodney W. Grimes 
2548df8bae1dSRodney W. Grimes 		/*
2549df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2550df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
2551df8bae1dSRodney W. Grimes 		 * standard timers.
2552df8bae1dSRodney W. Grimes 		 */
2553df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
2554de30ea13SRobert Watson 			KASSERT(headlocked == 1, ("tcp_input: dodata: "
2555de30ea13SRobert Watson 			    "TCP_FIN_WAIT_2: head not locked"));
2556340c35deSJonathan Lemon 			tcp_twstart(tp);
255711a20fb8SJeffrey Hsu 			INP_INFO_WUNLOCK(&tcbinfo);
2558340c35deSJonathan Lemon 			return;
2559df8bae1dSRodney W. Grimes 
2560df8bae1dSRodney W. Grimes 		/*
2561df8bae1dSRodney W. Grimes 		 * In TIME_WAIT state restart the 2 MSL time_wait timer.
2562df8bae1dSRodney W. Grimes 		 */
2563df8bae1dSRodney W. Grimes 		case TCPS_TIME_WAIT:
2564340c35deSJonathan Lemon 			KASSERT(tp->t_state != TCPS_TIME_WAIT, ("timewait"));
25659b8b58e0SJonathan Lemon 			callout_reset(tp->tt_2msl, 2 * tcp_msl,
25669b8b58e0SJonathan Lemon 				      tcp_timer_2msl, tp);
2567df8bae1dSRodney W. Grimes 			break;
2568df8bae1dSRodney W. Grimes 		}
2569df8bae1dSRodney W. Grimes 	}
2570e0bef1cbSRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
2571e0bef1cbSRobert Watson 	headlocked = 0;
2572610ee2f9SDavid Greenman #ifdef TCPDEBUG
25734cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
2574fb59c426SYoshinobu Inoue 		tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
2575fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2576610ee2f9SDavid Greenman #endif
2577df8bae1dSRodney W. Grimes 
2578df8bae1dSRodney W. Grimes 	/*
2579df8bae1dSRodney W. Grimes 	 * Return any desired output.
2580df8bae1dSRodney W. Grimes 	 */
2581df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
2582df8bae1dSRodney W. Grimes 		(void) tcp_output(tp);
25837792ea27SJeffrey Hsu 
2584a14c749fSJonathan Lemon check_delack:
2585e0bef1cbSRobert Watson 	KASSERT(headlocked == 0, ("tcp_input: check_delack: head locked"));
25867cfc6904SRobert Watson 	INP_LOCK_ASSERT(inp);
2587a14c749fSJonathan Lemon 	if (tp->t_flags & TF_DELACK) {
25883bfd6421SJonathan Lemon 		tp->t_flags &= ~TF_DELACK;
25893bfd6421SJonathan Lemon 		callout_reset(tp->tt_delack, tcp_delacktime,
25903bfd6421SJonathan Lemon 		    tcp_timer_delack, tp);
25913bfd6421SJonathan Lemon 	}
2592f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
2593df8bae1dSRodney W. Grimes 	return;
2594df8bae1dSRodney W. Grimes 
2595df8bae1dSRodney W. Grimes dropafterack:
2596de30ea13SRobert Watson 	KASSERT(headlocked, ("tcp_input: dropafterack: head not locked"));
2597df8bae1dSRodney W. Grimes 	/*
2598df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
2599df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
260080ab7c0eSGarrett Wollman 	 *
260180ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
260280ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
260380ab7c0eSGarrett Wollman 	 * RST have been dropped.
260480ab7c0eSGarrett Wollman 	 *
260580ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
260680ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
260780ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
260880ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
260980ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
261080ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
2611df8bae1dSRodney W. Grimes 	 */
2612fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
2613fb59c426SYoshinobu Inoue 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
2614a57815efSBosko Milekic 	     SEQ_GT(th->th_ack, tp->snd_max)) ) {
2615a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2616a57815efSBosko Milekic 		goto dropwithreset;
2617a57815efSBosko Milekic 	}
2618a0292f23SGarrett Wollman #ifdef TCPDEBUG
26194cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
2620fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2621fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2622a0292f23SGarrett Wollman #endif
262385e8b243SJeffrey Hsu 	KASSERT(headlocked, ("headlocked should be 1"));
262442cf3289SRobert Watson 	INP_INFO_WUNLOCK(&tcbinfo);
2625df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
2626df8bae1dSRodney W. Grimes 	(void) tcp_output(tp);
2627f76fcf6dSJeffrey Hsu 	INP_UNLOCK(inp);
2628d6915262SRobert Watson 	m_freem(m);
2629df8bae1dSRodney W. Grimes 	return;
2630df8bae1dSRodney W. Grimes 
2631df8bae1dSRodney W. Grimes dropwithreset:
2632de30ea13SRobert Watson 	KASSERT(headlocked, ("tcp_input: dropwithreset: head not locked"));
2633df8bae1dSRodney W. Grimes 	/*
2634df8bae1dSRodney W. Grimes 	 * Generate a RST, dropping incoming segment.
2635df8bae1dSRodney W. Grimes 	 * Make ACK acceptable to originator of segment.
2636df8bae1dSRodney W. Grimes 	 * Don't bother to respond if destination was broadcast/multicast.
2637df8bae1dSRodney W. Grimes 	 */
2638fb59c426SYoshinobu Inoue 	if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
2639df8bae1dSRodney W. Grimes 		goto drop;
2640fb59c426SYoshinobu Inoue 	if (isipv6) {
2641173c0f9fSWarner Losh 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
2642173c0f9fSWarner Losh 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
2643fb59c426SYoshinobu Inoue 			goto drop;
2644c068736aSJeffrey Hsu 	} else {
2645173c0f9fSWarner Losh 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
2646173c0f9fSWarner Losh 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
26472ca2159fSCrist J. Clark 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
26482ca2159fSCrist J. Clark 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
2649fb59c426SYoshinobu Inoue 			goto drop;
2650c068736aSJeffrey Hsu 	}
2651fb59c426SYoshinobu Inoue 	/* IPv6 anycast check is done at tcp6_input() */
2652a57815efSBosko Milekic 
2653a57815efSBosko Milekic 	/*
2654c59319bfSDag-Erling Smørgrav 	 * Perform bandwidth limiting.
2655a57815efSBosko Milekic 	 */
2656a57815efSBosko Milekic 	if (badport_bandlim(rstreason) < 0)
2657a57815efSBosko Milekic 		goto drop;
2658a57815efSBosko Milekic 
2659a0292f23SGarrett Wollman #ifdef TCPDEBUG
26604cc20ab1SSeigo Tanimura 	if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
2661fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2662fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2663a0292f23SGarrett Wollman #endif
26646fd22cafSJeffrey Hsu 
2665fb59c426SYoshinobu Inoue 	if (thflags & TH_ACK)
2666fb59c426SYoshinobu Inoue 		/* mtod() below is safe as long as hdr dropping is delayed */
2667fb59c426SYoshinobu Inoue 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack,
2668fb59c426SYoshinobu Inoue 			    TH_RST);
2669df8bae1dSRodney W. Grimes 	else {
2670fb59c426SYoshinobu Inoue 		if (thflags & TH_SYN)
2671fb59c426SYoshinobu Inoue 			tlen++;
2672fb59c426SYoshinobu Inoue 		/* mtod() below is safe as long as hdr dropping is delayed */
2673fb59c426SYoshinobu Inoue 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
2674fb59c426SYoshinobu Inoue 			    (tcp_seq)0, TH_RST|TH_ACK);
2675df8bae1dSRodney W. Grimes 	}
2676c29afad6SSam Leffler 
26771c53f806SRobert Watson 	if (tp != NULL)
2678c29afad6SSam Leffler 		INP_UNLOCK(inp);
2679f76fcf6dSJeffrey Hsu 	if (headlocked)
2680f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&tcbinfo);
2681df8bae1dSRodney W. Grimes 	return;
2682df8bae1dSRodney W. Grimes 
2683df8bae1dSRodney W. Grimes drop:
2684df8bae1dSRodney W. Grimes 	/*
2685df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
2686df8bae1dSRodney W. Grimes 	 */
2687610ee2f9SDavid Greenman #ifdef TCPDEBUG
26881c53f806SRobert Watson 	if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
2689fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2690fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
2691a0292f23SGarrett Wollman #endif
26921c53f806SRobert Watson 	if (tp != NULL)
2693f76fcf6dSJeffrey Hsu 		INP_UNLOCK(inp);
2694f76fcf6dSJeffrey Hsu 	if (headlocked)
2695f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&tcbinfo);
2696d6915262SRobert Watson 	m_freem(m);
2697df8bae1dSRodney W. Grimes 	return;
2698df8bae1dSRodney W. Grimes }
2699df8bae1dSRodney W. Grimes 
2700be2ac88cSJonathan Lemon /*
2701be2ac88cSJonathan Lemon  * Parse TCP options and place in tcpopt.
2702be2ac88cSJonathan Lemon  */
27030312fbe9SPoul-Henning Kamp static void
2704f72167f4SAndre Oppermann tcp_dooptions(to, cp, cnt, flags)
2705be2ac88cSJonathan Lemon 	struct tcpopt *to;
2706df8bae1dSRodney W. Grimes 	u_char *cp;
2707df8bae1dSRodney W. Grimes 	int cnt;
2708f72167f4SAndre Oppermann 	int flags;
2709df8bae1dSRodney W. Grimes {
2710df8bae1dSRodney W. Grimes 	int opt, optlen;
2711df8bae1dSRodney W. Grimes 
2712be2ac88cSJonathan Lemon 	to->to_flags = 0;
2713df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
2714df8bae1dSRodney W. Grimes 		opt = cp[0];
2715df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
2716df8bae1dSRodney W. Grimes 			break;
2717df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
2718df8bae1dSRodney W. Grimes 			optlen = 1;
2719df8bae1dSRodney W. Grimes 		else {
2720b474779fSJun-ichiro itojun Hagino 			if (cnt < 2)
2721b474779fSJun-ichiro itojun Hagino 				break;
2722df8bae1dSRodney W. Grimes 			optlen = cp[1];
2723b474779fSJun-ichiro itojun Hagino 			if (optlen < 2 || optlen > cnt)
2724df8bae1dSRodney W. Grimes 				break;
2725df8bae1dSRodney W. Grimes 		}
2726df8bae1dSRodney W. Grimes 		switch (opt) {
2727df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
2728df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
2729df8bae1dSRodney W. Grimes 				continue;
2730f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
2731df8bae1dSRodney W. Grimes 				continue;
2732be2ac88cSJonathan Lemon 			to->to_flags |= TOF_MSS;
2733be2ac88cSJonathan Lemon 			bcopy((char *)cp + 2,
2734be2ac88cSJonathan Lemon 			    (char *)&to->to_mss, sizeof(to->to_mss));
2735fd8e4ebcSMike Barcroft 			to->to_mss = ntohs(to->to_mss);
2736df8bae1dSRodney W. Grimes 			break;
2737df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
2738df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
2739df8bae1dSRodney W. Grimes 				continue;
2740f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
2741df8bae1dSRodney W. Grimes 				continue;
2742be2ac88cSJonathan Lemon 			to->to_flags |= TOF_SCALE;
2743be2ac88cSJonathan Lemon 			to->to_requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
2744df8bae1dSRodney W. Grimes 			break;
2745df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
2746df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
2747df8bae1dSRodney W. Grimes 				continue;
2748be2ac88cSJonathan Lemon 			to->to_flags |= TOF_TS;
2749a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
2750a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
2751fd8e4ebcSMike Barcroft 			to->to_tsval = ntohl(to->to_tsval);
2752a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
2753a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
2754fd8e4ebcSMike Barcroft 			to->to_tsecr = ntohl(to->to_tsecr);
2755df8bae1dSRodney W. Grimes 			break;
27561cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
27571cfd4b53SBruce M Simpson 		/*
27581cfd4b53SBruce M Simpson 		 * XXX In order to reply to a host which has set the
27591cfd4b53SBruce M Simpson 		 * TCP_SIGNATURE option in its initial SYN, we have to
27601cfd4b53SBruce M Simpson 		 * record the fact that the option was observed here
27611cfd4b53SBruce M Simpson 		 * for the syncache code to perform the correct response.
27621cfd4b53SBruce M Simpson 		 */
27631cfd4b53SBruce M Simpson 		case TCPOPT_SIGNATURE:
27641cfd4b53SBruce M Simpson 			if (optlen != TCPOLEN_SIGNATURE)
27651cfd4b53SBruce M Simpson 				continue;
27661cfd4b53SBruce M Simpson 			to->to_flags |= (TOF_SIGNATURE | TOF_SIGLEN);
27671cfd4b53SBruce M Simpson 			break;
2768265ed012SBruce M Simpson #endif
27696d90faf3SPaul Saab 		case TCPOPT_SACK_PERMITTED:
2770f72167f4SAndre Oppermann 			if (optlen != TCPOLEN_SACK_PERMITTED)
27716d90faf3SPaul Saab 				continue;
2772f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
2773f72167f4SAndre Oppermann 				continue;
2774f72167f4SAndre Oppermann 			if (!tcp_do_sack)
2775f72167f4SAndre Oppermann 				continue;
27766d90faf3SPaul Saab 			to->to_flags |= TOF_SACK;
27776d90faf3SPaul Saab 			break;
27786d90faf3SPaul Saab 		case TCPOPT_SACK:
27795a53ca16SPaul Saab 			if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
27806d90faf3SPaul Saab 				continue;
27815a53ca16SPaul Saab 			to->to_nsacks = (optlen - 2) / TCPOLEN_SACK;
27825a53ca16SPaul Saab 			to->to_sacks = cp + 2;
27835a53ca16SPaul Saab 			tcpstat.tcps_sack_rcv_blocks++;
27846d90faf3SPaul Saab 			break;
2785be2ac88cSJonathan Lemon 		default:
2786be2ac88cSJonathan Lemon 			continue;
2787df8bae1dSRodney W. Grimes 		}
2788df8bae1dSRodney W. Grimes 	}
2789df8bae1dSRodney W. Grimes }
2790df8bae1dSRodney W. Grimes 
2791df8bae1dSRodney W. Grimes /*
2792df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
2793df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
2794df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
2795df8bae1dSRodney W. Grimes  * sequencing purposes.
2796df8bae1dSRodney W. Grimes  */
27970312fbe9SPoul-Henning Kamp static void
2798fb59c426SYoshinobu Inoue tcp_pulloutofband(so, th, m, off)
2799df8bae1dSRodney W. Grimes 	struct socket *so;
2800fb59c426SYoshinobu Inoue 	struct tcphdr *th;
2801df8bae1dSRodney W. Grimes 	register struct mbuf *m;
2802fb59c426SYoshinobu Inoue 	int off;		/* delayed to be droped hdrlen */
2803df8bae1dSRodney W. Grimes {
2804fb59c426SYoshinobu Inoue 	int cnt = off + th->th_urp - 1;
2805df8bae1dSRodney W. Grimes 
2806df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
2807df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
2808df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
2809df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
2810df8bae1dSRodney W. Grimes 
2811df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
2812df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
2813df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
2814df8bae1dSRodney W. Grimes 			m->m_len--;
281569a34685SYoshinobu Inoue 			if (m->m_flags & M_PKTHDR)
281669a34685SYoshinobu Inoue 				m->m_pkthdr.len--;
2817df8bae1dSRodney W. Grimes 			return;
2818df8bae1dSRodney W. Grimes 		}
2819df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
2820df8bae1dSRodney W. Grimes 		m = m->m_next;
2821df8bae1dSRodney W. Grimes 		if (m == 0)
2822df8bae1dSRodney W. Grimes 			break;
2823df8bae1dSRodney W. Grimes 	}
2824df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
2825df8bae1dSRodney W. Grimes }
2826df8bae1dSRodney W. Grimes 
2827df8bae1dSRodney W. Grimes /*
2828df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
2829df8bae1dSRodney W. Grimes  * and update averages and current timeout.
2830df8bae1dSRodney W. Grimes  */
28310312fbe9SPoul-Henning Kamp static void
2832df8bae1dSRodney W. Grimes tcp_xmit_timer(tp, rtt)
2833df8bae1dSRodney W. Grimes 	register struct tcpcb *tp;
28349b8b58e0SJonathan Lemon 	int rtt;
2835df8bae1dSRodney W. Grimes {
2836233e8c18SGarrett Wollman 	register int delta;
2837233e8c18SGarrett Wollman 
28382be3bf22SRobert Watson 	INP_LOCK_ASSERT(tp->t_inpcb);
28392be3bf22SRobert Watson 
2840233e8c18SGarrett Wollman 	tcpstat.tcps_rttupdated++;
2841233e8c18SGarrett Wollman 	tp->t_rttupdated++;
2842233e8c18SGarrett Wollman 	if (tp->t_srtt != 0) {
2843233e8c18SGarrett Wollman 		/*
2844233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
2845233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
2846233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
2847233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
2848233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
2849233e8c18SGarrett Wollman 		 */
2850233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
2851233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
2852233e8c18SGarrett Wollman 
2853233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
2854233e8c18SGarrett Wollman 			tp->t_srtt = 1;
2855233e8c18SGarrett Wollman 
2856233e8c18SGarrett Wollman 		/*
2857233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
2858233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
2859233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
2860233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
2861233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
2862233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
2863233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
2864233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
2865233e8c18SGarrett Wollman 		 */
2866233e8c18SGarrett Wollman 		if (delta < 0)
2867233e8c18SGarrett Wollman 			delta = -delta;
2868233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
2869233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
2870233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
28711fcc99b5SMatthew Dillon 		if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
28721fcc99b5SMatthew Dillon 		    tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
2873233e8c18SGarrett Wollman 	} else {
2874233e8c18SGarrett Wollman 		/*
2875233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
2876233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
2877233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
2878233e8c18SGarrett Wollman 		 */
2879233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
2880233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
28811fcc99b5SMatthew Dillon 		tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
2882233e8c18SGarrett Wollman 	}
28839b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
2884df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
2885df8bae1dSRodney W. Grimes 
2886df8bae1dSRodney W. Grimes 	/*
2887df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
2888df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
2889df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
2890df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
2891df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
2892df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
2893df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
2894df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
2895df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
2896df8bae1dSRodney W. Grimes 	 */
2897233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
28989e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
2899df8bae1dSRodney W. Grimes 
2900df8bae1dSRodney W. Grimes 	/*
2901df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
2902df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
2903df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
2904df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
2905df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
2906df8bae1dSRodney W. Grimes 	 */
2907df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
2908df8bae1dSRodney W. Grimes }
2909df8bae1dSRodney W. Grimes 
2910df8bae1dSRodney W. Grimes /*
2911df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
2912df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
2913df8bae1dSRodney W. Grimes  * If none, use an mss that can be handled on the outgoing
2914df8bae1dSRodney W. Grimes  * interface without forcing IP to fragment; if bigger than
2915df8bae1dSRodney W. Grimes  * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
2916df8bae1dSRodney W. Grimes  * to utilize large mbufs.  If no route is found, route has no mtu,
2917df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
2918df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
2919df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
2920df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
2921df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
2922df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
2923df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
2924a0292f23SGarrett Wollman  *
2925a0292f23SGarrett Wollman  * Also take into account the space needed for options that we
2926a0292f23SGarrett Wollman  * send regularly.  Make maxseg shorter by that amount to assure
2927a0292f23SGarrett Wollman  * that we can send maxseg amount of data even when the options
2928a0292f23SGarrett Wollman  * are present.  Store the upper limit of the length of options plus
2929a0292f23SGarrett Wollman  * data in maxopd.
2930a0292f23SGarrett Wollman  *
2931a0292f23SGarrett Wollman  *
2932a0292f23SGarrett Wollman  * In case of T/TCP, we call this routine during implicit connection
2933a0292f23SGarrett Wollman  * setup as well (offer = -1), to initialize maxseg from the cached
2934a0292f23SGarrett Wollman  * MSS of our peer.
293597d8d152SAndre Oppermann  *
293697d8d152SAndre Oppermann  * NOTE that this routine is only called when we process an incoming
293797d8d152SAndre Oppermann  * segment. Outgoing SYN/ACK MSS settings are handled in tcp_mssopt().
2938df8bae1dSRodney W. Grimes  */
2939a0292f23SGarrett Wollman void
2940df8bae1dSRodney W. Grimes tcp_mss(tp, offer)
2941a0292f23SGarrett Wollman 	struct tcpcb *tp;
2942a0292f23SGarrett Wollman 	int offer;
2943df8bae1dSRodney W. Grimes {
294497d8d152SAndre Oppermann 	int rtt, mss;
2945df8bae1dSRodney W. Grimes 	u_long bufsize;
294697d8d152SAndre Oppermann 	u_long maxmtu;
2947c068736aSJeffrey Hsu 	struct inpcb *inp = tp->t_inpcb;
2948df8bae1dSRodney W. Grimes 	struct socket *so;
294997d8d152SAndre Oppermann 	struct hc_metrics_lite metrics;
2950a0292f23SGarrett Wollman 	int origoffer = offer;
2951233dcce1SAndre Oppermann 	int mtuflags = 0;
2952fb59c426SYoshinobu Inoue #ifdef INET6
2953c068736aSJeffrey Hsu 	int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
2954c068736aSJeffrey Hsu 	size_t min_protoh = isipv6 ?
2955c068736aSJeffrey Hsu 			    sizeof (struct ip6_hdr) + sizeof (struct tcphdr) :
2956c068736aSJeffrey Hsu 			    sizeof (struct tcpiphdr);
2957c068736aSJeffrey Hsu #else
2958c068736aSJeffrey Hsu 	const size_t min_protoh = sizeof(struct tcpiphdr);
2959fb59c426SYoshinobu Inoue #endif
2960df8bae1dSRodney W. Grimes 
296197d8d152SAndre Oppermann 	/* initialize */
296297d8d152SAndre Oppermann #ifdef INET6
296397d8d152SAndre Oppermann 	if (isipv6) {
2964233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu6(&inp->inp_inc, &mtuflags);
296597d8d152SAndre Oppermann 		tp->t_maxopd = tp->t_maxseg = tcp_v6mssdflt;
296697d8d152SAndre Oppermann 	} else
296797d8d152SAndre Oppermann #endif
296897d8d152SAndre Oppermann 	{
2969233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu(&inp->inp_inc, &mtuflags);
297097d8d152SAndre Oppermann 		tp->t_maxopd = tp->t_maxseg = tcp_mssdflt;
2971df8bae1dSRodney W. Grimes 	}
2972df8bae1dSRodney W. Grimes 	so = inp->inp_socket;
2973df8bae1dSRodney W. Grimes 
297497d8d152SAndre Oppermann 	/*
29752d166c02SAndre Oppermann 	 * no route to sender, stay with default mss and return
297697d8d152SAndre Oppermann 	 */
297797d8d152SAndre Oppermann 	if (maxmtu == 0)
297897d8d152SAndre Oppermann 		return;
297997d8d152SAndre Oppermann 
298097d8d152SAndre Oppermann 	/* what have we got? */
298197d8d152SAndre Oppermann 	switch (offer) {
298297d8d152SAndre Oppermann 		case 0:
298397d8d152SAndre Oppermann 			/*
298497d8d152SAndre Oppermann 			 * Offer == 0 means that there was no MSS on the SYN
298597d8d152SAndre Oppermann 			 * segment, in this case we use tcp_mssdflt.
298697d8d152SAndre Oppermann 			 */
298797d8d152SAndre Oppermann 			offer =
298897d8d152SAndre Oppermann #ifdef INET6
298997d8d152SAndre Oppermann 				isipv6 ? tcp_v6mssdflt :
299097d8d152SAndre Oppermann #endif
299197d8d152SAndre Oppermann 				tcp_mssdflt;
299297d8d152SAndre Oppermann 			break;
299397d8d152SAndre Oppermann 
299497d8d152SAndre Oppermann 		case -1:
2995a0292f23SGarrett Wollman 			/*
2996c94c54e4SAndre Oppermann 			 * Offer == -1 means that we didn't receive SYN yet.
2997a0292f23SGarrett Wollman 			 */
299897d8d152SAndre Oppermann 			/* FALLTHROUGH */
299997d8d152SAndre Oppermann 
300097d8d152SAndre Oppermann 		default:
3001a0292f23SGarrett Wollman 			/*
300253369ac9SAndre Oppermann 			 * Prevent DoS attack with too small MSS. Round up
300353369ac9SAndre Oppermann 			 * to at least minmss.
300453369ac9SAndre Oppermann 			 */
300553369ac9SAndre Oppermann 			offer = max(offer, tcp_minmss);
300653369ac9SAndre Oppermann 			/*
3007a0292f23SGarrett Wollman 			 * Sanity check: make sure that maxopd will be large
300897d8d152SAndre Oppermann 			 * enough to allow some data on segments even if the
3009a0292f23SGarrett Wollman 			 * all the option space is used (40bytes).  Otherwise
3010a0292f23SGarrett Wollman 			 * funny things may happen in tcp_output.
3011a0292f23SGarrett Wollman 			 */
3012a0292f23SGarrett Wollman 			offer = max(offer, 64);
301397d8d152SAndre Oppermann 	}
3014a0292f23SGarrett Wollman 
3015df8bae1dSRodney W. Grimes 	/*
301697d8d152SAndre Oppermann 	 * rmx information is now retrieved from tcp_hostcache
3017df8bae1dSRodney W. Grimes 	 */
301897d8d152SAndre Oppermann 	tcp_hc_get(&inp->inp_inc, &metrics);
301997d8d152SAndre Oppermann 
3020df8bae1dSRodney W. Grimes 	/*
302197d8d152SAndre Oppermann 	 * if there's a discovered mtu int tcp hostcache, use it
3022fb59c426SYoshinobu Inoue 	 * else, use the link mtu.
3023df8bae1dSRodney W. Grimes 	 */
302497d8d152SAndre Oppermann 	if (metrics.rmx_mtu)
30252d166c02SAndre Oppermann 		mss = min(metrics.rmx_mtu, maxmtu) - min_protoh;
3026c068736aSJeffrey Hsu 	else {
302731b3783cSHajimu UMEMOTO #ifdef INET6
3028fb59c426SYoshinobu Inoue 		if (isipv6) {
302997d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
303097d8d152SAndre Oppermann 			if (!path_mtu_discovery &&
303197d8d152SAndre Oppermann 			    !in6_localaddr(&inp->in6p_faddr))
3032fb59c426SYoshinobu Inoue 				mss = min(mss, tcp_v6mssdflt);
3033b3399803SHajimu UMEMOTO 		} else
3034b3399803SHajimu UMEMOTO #endif
303597d8d152SAndre Oppermann 		{
303697d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
303797d8d152SAndre Oppermann 			if (!path_mtu_discovery &&
303897d8d152SAndre Oppermann 			    !in_localaddr(inp->inp_faddr))
3039a0292f23SGarrett Wollman 				mss = min(mss, tcp_mssdflt);
3040a0292f23SGarrett Wollman 		}
304197d8d152SAndre Oppermann 	}
3042a0292f23SGarrett Wollman 	mss = min(mss, offer);
304397d8d152SAndre Oppermann 
3044a0292f23SGarrett Wollman 	/*
3045a0292f23SGarrett Wollman 	 * maxopd stores the maximum length of data AND options
3046a0292f23SGarrett Wollman 	 * in a segment; maxseg is the amount of data in a normal
3047a0292f23SGarrett Wollman 	 * segment.  We need to store this value (maxopd) apart
3048a0292f23SGarrett Wollman 	 * from maxseg, because now every segment carries options
3049a0292f23SGarrett Wollman 	 * and thus we normally have somewhat less data in segments.
3050a0292f23SGarrett Wollman 	 */
3051a0292f23SGarrett Wollman 	tp->t_maxopd = mss;
3052a0292f23SGarrett Wollman 
3053a0292f23SGarrett Wollman 	/*
3054c94c54e4SAndre Oppermann 	 * origoffer==-1 indicates, that no segments were received yet.
3055c94c54e4SAndre Oppermann 	 * In this case we just guess.
3056a0292f23SGarrett Wollman 	 */
3057a0292f23SGarrett Wollman 	if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
3058a0292f23SGarrett Wollman 	    (origoffer == -1 ||
3059a0292f23SGarrett Wollman 	     (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
3060a0292f23SGarrett Wollman 		mss -= TCPOLEN_TSTAMP_APPA;
306197d8d152SAndre Oppermann 	tp->t_maxseg = mss;
3062a0292f23SGarrett Wollman 
3063df8bae1dSRodney W. Grimes #if	(MCLBYTES & (MCLBYTES - 1)) == 0
3064df8bae1dSRodney W. Grimes 		if (mss > MCLBYTES)
3065df8bae1dSRodney W. Grimes 			mss &= ~(MCLBYTES-1);
3066df8bae1dSRodney W. Grimes #else
3067df8bae1dSRodney W. Grimes 		if (mss > MCLBYTES)
3068df8bae1dSRodney W. Grimes 			mss = mss / MCLBYTES * MCLBYTES;
3069df8bae1dSRodney W. Grimes #endif
307097d8d152SAndre Oppermann 	tp->t_maxseg = mss;
307197d8d152SAndre Oppermann 
3072df8bae1dSRodney W. Grimes 	/*
307397d8d152SAndre Oppermann 	 * If there's a pipesize, change the socket buffer to that size,
307497d8d152SAndre Oppermann 	 * don't change if sb_hiwat is different than default (then it
307597d8d152SAndre Oppermann 	 * has been changed on purpose with setsockopt).
307697d8d152SAndre Oppermann 	 * Make the socket buffers an integral number of mss units;
307797d8d152SAndre Oppermann 	 * if the mss is larger than the socket buffer, decrease the mss.
3078df8bae1dSRodney W. Grimes 	 */
30793f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
308097d8d152SAndre Oppermann 	if ((so->so_snd.sb_hiwat == tcp_sendspace) && metrics.rmx_sendpipe)
308197d8d152SAndre Oppermann 		bufsize = metrics.rmx_sendpipe;
308297d8d152SAndre Oppermann 	else
3083df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
3084df8bae1dSRodney W. Grimes 	if (bufsize < mss)
3085df8bae1dSRodney W. Grimes 		mss = bufsize;
3086df8bae1dSRodney W. Grimes 	else {
3087df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3088df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3089df8bae1dSRodney W. Grimes 			bufsize = sb_max;
309088c39af3SRuslan Ermilov 		if (bufsize > so->so_snd.sb_hiwat)
30913f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_snd, bufsize, so, NULL);
3092df8bae1dSRodney W. Grimes 	}
30933f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
3094df8bae1dSRodney W. Grimes 	tp->t_maxseg = mss;
3095df8bae1dSRodney W. Grimes 
30963f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
309797d8d152SAndre Oppermann 	if ((so->so_rcv.sb_hiwat == tcp_recvspace) && metrics.rmx_recvpipe)
309897d8d152SAndre Oppermann 		bufsize = metrics.rmx_recvpipe;
309997d8d152SAndre Oppermann 	else
3100df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
3101df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
3102df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3103df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3104df8bae1dSRodney W. Grimes 			bufsize = sb_max;
310588c39af3SRuslan Ermilov 		if (bufsize > so->so_rcv.sb_hiwat)
31063f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL);
3107df8bae1dSRodney W. Grimes 	}
31083f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
3109a0292f23SGarrett Wollman 	/*
311097d8d152SAndre Oppermann 	 * While we're here, check the others too
3111a0292f23SGarrett Wollman 	 */
311297d8d152SAndre Oppermann 	if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) {
311397d8d152SAndre Oppermann 		tp->t_srtt = rtt;
311497d8d152SAndre Oppermann 		tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE;
311597d8d152SAndre Oppermann 		tcpstat.tcps_usedrtt++;
311697d8d152SAndre Oppermann 		if (metrics.rmx_rttvar) {
311797d8d152SAndre Oppermann 			tp->t_rttvar = metrics.rmx_rttvar;
311897d8d152SAndre Oppermann 			tcpstat.tcps_usedrttvar++;
311997d8d152SAndre Oppermann 		} else {
312097d8d152SAndre Oppermann 			/* default variation is +- 1 rtt */
312197d8d152SAndre Oppermann 			tp->t_rttvar =
312297d8d152SAndre Oppermann 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
312397d8d152SAndre Oppermann 		}
312497d8d152SAndre Oppermann 		TCPT_RANGESET(tp->t_rxtcur,
312597d8d152SAndre Oppermann 			      ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
312697d8d152SAndre Oppermann 			      tp->t_rttmin, TCPTV_REXMTMAX);
312797d8d152SAndre Oppermann 	}
312897d8d152SAndre Oppermann 	if (metrics.rmx_ssthresh) {
3129df8bae1dSRodney W. Grimes 		/*
3130df8bae1dSRodney W. Grimes 		 * There's some sort of gateway or interface
3131df8bae1dSRodney W. Grimes 		 * buffer limit on the path.  Use this to set
3132df8bae1dSRodney W. Grimes 		 * the slow start threshhold, but set the
3133df8bae1dSRodney W. Grimes 		 * threshold to no less than 2*mss.
3134df8bae1dSRodney W. Grimes 		 */
313597d8d152SAndre Oppermann 		tp->snd_ssthresh = max(2 * mss, metrics.rmx_ssthresh);
3136dd224982SGarrett Wollman 		tcpstat.tcps_usedssthresh++;
3137df8bae1dSRodney W. Grimes 	}
313897d8d152SAndre Oppermann 	if (metrics.rmx_bandwidth)
313997d8d152SAndre Oppermann 		tp->snd_bandwidth = metrics.rmx_bandwidth;
314097d8d152SAndre Oppermann 
314197d8d152SAndre Oppermann 	/*
314297d8d152SAndre Oppermann 	 * Set the slow-start flight size depending on whether this
314397d8d152SAndre Oppermann 	 * is a local network or not.
314497d8d152SAndre Oppermann 	 *
314597d8d152SAndre Oppermann 	 * Extend this so we cache the cwnd too and retrieve it here.
314697d8d152SAndre Oppermann 	 * Make cwnd even bigger than RFC3390 suggests but only if we
314797d8d152SAndre Oppermann 	 * have previous experience with the remote host. Be careful
314897d8d152SAndre Oppermann 	 * not make cwnd bigger than remote receive window or our own
314997d8d152SAndre Oppermann 	 * send socket buffer. Maybe put some additional upper bound
315097d8d152SAndre Oppermann 	 * on the retrieved cwnd. Should do incremental updates to
315197d8d152SAndre Oppermann 	 * hostcache when cwnd collapses so next connection doesn't
315297d8d152SAndre Oppermann 	 * overloads the path again.
315397d8d152SAndre Oppermann 	 *
315497d8d152SAndre Oppermann 	 * RFC3390 says only do this if SYN or SYN/ACK didn't got lost.
315597d8d152SAndre Oppermann 	 * We currently check only in syncache_socket for that.
315697d8d152SAndre Oppermann 	 */
315797d8d152SAndre Oppermann #define TCP_METRICS_CWND
315897d8d152SAndre Oppermann #ifdef TCP_METRICS_CWND
315997d8d152SAndre Oppermann 	if (metrics.rmx_cwnd)
316097d8d152SAndre Oppermann 		tp->snd_cwnd = max(mss,
316197d8d152SAndre Oppermann 				min(metrics.rmx_cwnd / 2,
316297d8d152SAndre Oppermann 				 min(tp->snd_wnd, so->so_snd.sb_hiwat)));
316397d8d152SAndre Oppermann 	else
316497d8d152SAndre Oppermann #endif
316597d8d152SAndre Oppermann 	if (tcp_do_rfc3390)
316697d8d152SAndre Oppermann 		tp->snd_cwnd = min(4 * mss, max(2 * mss, 4380));
316797d8d152SAndre Oppermann #ifdef INET6
316897d8d152SAndre Oppermann 	else if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) ||
316997d8d152SAndre Oppermann 		 (!isipv6 && in_localaddr(inp->inp_faddr)))
3170943ae302SAndre Oppermann #else
3171943ae302SAndre Oppermann 	else if (in_localaddr(inp->inp_faddr))
317297d8d152SAndre Oppermann #endif
3173943ae302SAndre Oppermann 		tp->snd_cwnd = mss * ss_fltsz_local;
317497d8d152SAndre Oppermann 	else
317597d8d152SAndre Oppermann 		tp->snd_cwnd = mss * ss_fltsz;
3176233dcce1SAndre Oppermann 
3177233dcce1SAndre Oppermann 	/* Check the interface for TSO capabilities. */
3178233dcce1SAndre Oppermann 	if (mtuflags & CSUM_TSO)
3179233dcce1SAndre Oppermann 		tp->t_flags |= TF_TSO;
3180a0292f23SGarrett Wollman }
3181a0292f23SGarrett Wollman 
3182a0292f23SGarrett Wollman /*
3183a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
3184a0292f23SGarrett Wollman  */
3185a0292f23SGarrett Wollman int
318697d8d152SAndre Oppermann tcp_mssopt(inc)
318797d8d152SAndre Oppermann 	struct in_conninfo *inc;
3188a0292f23SGarrett Wollman {
318997d8d152SAndre Oppermann 	int mss = 0;
319097d8d152SAndre Oppermann 	u_long maxmtu = 0;
319197d8d152SAndre Oppermann 	u_long thcmtu = 0;
319297d8d152SAndre Oppermann 	size_t min_protoh;
3193fb59c426SYoshinobu Inoue #ifdef INET6
319497d8d152SAndre Oppermann 	int isipv6 = inc->inc_isipv6 ? 1 : 0;
3195fb59c426SYoshinobu Inoue #endif
3196a0292f23SGarrett Wollman 
319797d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer"));
3198a0292f23SGarrett Wollman 
319931b3783cSHajimu UMEMOTO #ifdef INET6
320097d8d152SAndre Oppermann 	if (isipv6) {
320197d8d152SAndre Oppermann 		mss = tcp_v6mssdflt;
3202233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu6(inc, NULL);
320397d8d152SAndre Oppermann 		thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
320497d8d152SAndre Oppermann 		min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
320597d8d152SAndre Oppermann 	} else
320631b3783cSHajimu UMEMOTO #endif
320797d8d152SAndre Oppermann 	{
320897d8d152SAndre Oppermann 		mss = tcp_mssdflt;
3209233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu(inc, NULL);
321097d8d152SAndre Oppermann 		thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
321197d8d152SAndre Oppermann 		min_protoh = sizeof(struct tcpiphdr);
321297d8d152SAndre Oppermann 	}
321397d8d152SAndre Oppermann 	if (maxmtu && thcmtu)
321497d8d152SAndre Oppermann 		mss = min(maxmtu, thcmtu) - min_protoh;
321597d8d152SAndre Oppermann 	else if (maxmtu || thcmtu)
321697d8d152SAndre Oppermann 		mss = max(maxmtu, thcmtu) - min_protoh;
321797d8d152SAndre Oppermann 
321897d8d152SAndre Oppermann 	return (mss);
3219df8bae1dSRodney W. Grimes }
322046f58482SJonathan Lemon 
322146f58482SJonathan Lemon 
322246f58482SJonathan Lemon /*
3223c068736aSJeffrey Hsu  * On a partial ack arrives, force the retransmission of the
3224c068736aSJeffrey Hsu  * next unacknowledged segment.  Do not clear tp->t_dupacks.
3225c068736aSJeffrey Hsu  * By setting snd_nxt to ti_ack, this forces retransmission timer to
3226c068736aSJeffrey Hsu  * be started again.
322746f58482SJonathan Lemon  */
3228c068736aSJeffrey Hsu static void
3229c068736aSJeffrey Hsu tcp_newreno_partial_ack(tp, th)
323046f58482SJonathan Lemon 	struct tcpcb *tp;
323146f58482SJonathan Lemon 	struct tcphdr *th;
323246f58482SJonathan Lemon {
323346f58482SJonathan Lemon 	tcp_seq onxt = tp->snd_nxt;
323446f58482SJonathan Lemon 	u_long  ocwnd = tp->snd_cwnd;
323546f58482SJonathan Lemon 
323646f58482SJonathan Lemon 	callout_stop(tp->tt_rexmt);
323746f58482SJonathan Lemon 	tp->t_rtttime = 0;
323846f58482SJonathan Lemon 	tp->snd_nxt = th->th_ack;
32396b2a5f92SJayanth Vijayaraghavan 	/*
3240c068736aSJeffrey Hsu 	 * Set snd_cwnd to one segment beyond acknowledged offset.
3241c068736aSJeffrey Hsu 	 * (tp->snd_una has not yet been updated when this function is called.)
32426b2a5f92SJayanth Vijayaraghavan 	 */
32436b2a5f92SJayanth Vijayaraghavan 	tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una);
3244a84db8f4SMatthew Dillon 	tp->t_flags |= TF_ACKNOW;
32456b2a5f92SJayanth Vijayaraghavan 	(void) tcp_output(tp);
324646f58482SJonathan Lemon 	tp->snd_cwnd = ocwnd;
324746f58482SJonathan Lemon 	if (SEQ_GT(onxt, tp->snd_nxt))
324846f58482SJonathan Lemon 		tp->snd_nxt = onxt;
324946f58482SJonathan Lemon 	/*
325046f58482SJonathan Lemon 	 * Partial window deflation.  Relies on fact that tp->snd_una
325146f58482SJonathan Lemon 	 * not updated yet.
325246f58482SJonathan Lemon 	 */
3253d7587117SPaul Saab 	if (tp->snd_cwnd > th->th_ack - tp->snd_una)
3254d7587117SPaul Saab 		tp->snd_cwnd -= th->th_ack - tp->snd_una;
3255d7587117SPaul Saab 	else
3256d7587117SPaul Saab 		tp->snd_cwnd = 0;
3257d7587117SPaul Saab 	tp->snd_cwnd += tp->t_maxseg;
325846f58482SJonathan Lemon }
3259340c35deSJonathan Lemon 
3260340c35deSJonathan Lemon /*
3261340c35deSJonathan Lemon  * Returns 1 if the TIME_WAIT state was killed and we should start over,
3262340c35deSJonathan Lemon  * looking for a pcb in the listen state.  Returns 0 otherwise.
3263340c35deSJonathan Lemon  */
3264340c35deSJonathan Lemon static int
32653cbe7fafSRobert Watson tcp_timewait(inp, to, th, m, tlen)
32663cbe7fafSRobert Watson 	struct inpcb *inp;
3267340c35deSJonathan Lemon 	struct tcpopt *to;
3268340c35deSJonathan Lemon 	struct tcphdr *th;
3269340c35deSJonathan Lemon 	struct mbuf *m;
3270340c35deSJonathan Lemon 	int tlen;
3271340c35deSJonathan Lemon {
32723cbe7fafSRobert Watson 	struct tcptw *tw;
3273340c35deSJonathan Lemon 	int thflags;
3274340c35deSJonathan Lemon 	tcp_seq seq;
3275340c35deSJonathan Lemon #ifdef INET6
3276340c35deSJonathan Lemon 	int isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
3277340c35deSJonathan Lemon #else
3278340c35deSJonathan Lemon 	const int isipv6 = 0;
3279340c35deSJonathan Lemon #endif
3280340c35deSJonathan Lemon 
3281751dea29SRuslan Ermilov 	/* tcbinfo lock required for tcp_twclose(), tcp_timer_2msl_reset(). */
32823cbe7fafSRobert Watson 	INP_INFO_WLOCK_ASSERT(&tcbinfo);
32833cbe7fafSRobert Watson 	INP_LOCK_ASSERT(inp);
32843cbe7fafSRobert Watson 
3285ae0e7143SRobert Watson 	/*
3286ae0e7143SRobert Watson 	 * XXXRW: Time wait state for inpcb has been recycled, but inpcb is
3287ae0e7143SRobert Watson 	 * still present.  This is undesirable, but temporarily necessary
3288ae0e7143SRobert Watson 	 * until we work out how to handle inpcb's who's timewait state has
3289ae0e7143SRobert Watson 	 * been removed.
3290ae0e7143SRobert Watson 	 */
32913cbe7fafSRobert Watson 	tw = intotw(inp);
3292ae0e7143SRobert Watson 	if (tw == NULL)
3293ae0e7143SRobert Watson 		goto drop;
3294ae0e7143SRobert Watson 
3295340c35deSJonathan Lemon 	thflags = th->th_flags;
3296340c35deSJonathan Lemon 
3297340c35deSJonathan Lemon 	/*
3298340c35deSJonathan Lemon 	 * NOTE: for FIN_WAIT_2 (to be added later),
3299340c35deSJonathan Lemon 	 * must validate sequence number before accepting RST
3300340c35deSJonathan Lemon 	 */
3301340c35deSJonathan Lemon 
3302340c35deSJonathan Lemon 	/*
3303340c35deSJonathan Lemon 	 * If the segment contains RST:
3304340c35deSJonathan Lemon 	 *	Drop the segment - see Stevens, vol. 2, p. 964 and
3305340c35deSJonathan Lemon 	 *      RFC 1337.
3306340c35deSJonathan Lemon 	 */
3307340c35deSJonathan Lemon 	if (thflags & TH_RST)
3308340c35deSJonathan Lemon 		goto drop;
3309340c35deSJonathan Lemon 
3310340c35deSJonathan Lemon #if 0
3311340c35deSJonathan Lemon /* PAWS not needed at the moment */
3312340c35deSJonathan Lemon 	/*
3313340c35deSJonathan Lemon 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
3314340c35deSJonathan Lemon 	 * and it's less than ts_recent, drop it.
3315340c35deSJonathan Lemon 	 */
3316340c35deSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
3317340c35deSJonathan Lemon 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
3318340c35deSJonathan Lemon 		if ((thflags & TH_ACK) == 0)
3319340c35deSJonathan Lemon 			goto drop;
3320340c35deSJonathan Lemon 		goto ack;
3321340c35deSJonathan Lemon 	}
3322340c35deSJonathan Lemon 	/*
3323340c35deSJonathan Lemon 	 * ts_recent is never updated because we never accept new segments.
3324340c35deSJonathan Lemon 	 */
3325340c35deSJonathan Lemon #endif
3326340c35deSJonathan Lemon 
3327340c35deSJonathan Lemon 	/*
3328340c35deSJonathan Lemon 	 * If a new connection request is received
3329340c35deSJonathan Lemon 	 * while in TIME_WAIT, drop the old connection
3330340c35deSJonathan Lemon 	 * and start over if the sequence numbers
3331340c35deSJonathan Lemon 	 * are above the previous ones.
3332340c35deSJonathan Lemon 	 */
3333340c35deSJonathan Lemon 	if ((thflags & TH_SYN) && SEQ_GT(th->th_seq, tw->rcv_nxt)) {
3334623dce13SRobert Watson 		tcp_twclose(tw, 0);
3335340c35deSJonathan Lemon 		return (1);
3336340c35deSJonathan Lemon 	}
3337340c35deSJonathan Lemon 
3338340c35deSJonathan Lemon 	/*
3339340c35deSJonathan Lemon 	 * Drop the the segment if it does not contain an ACK.
3340340c35deSJonathan Lemon 	 */
3341340c35deSJonathan Lemon 	if ((thflags & TH_ACK) == 0)
3342340c35deSJonathan Lemon 		goto drop;
3343340c35deSJonathan Lemon 
3344340c35deSJonathan Lemon 	/*
3345340c35deSJonathan Lemon 	 * Reset the 2MSL timer if this is a duplicate FIN.
3346340c35deSJonathan Lemon 	 */
3347340c35deSJonathan Lemon 	if (thflags & TH_FIN) {
3348340c35deSJonathan Lemon 		seq = th->th_seq + tlen + (thflags & TH_SYN ? 1 : 0);
3349340c35deSJonathan Lemon 		if (seq + 1 == tw->rcv_nxt)
3350751dea29SRuslan Ermilov 			tcp_timer_2msl_reset(tw, 1);
3351340c35deSJonathan Lemon 	}
3352340c35deSJonathan Lemon 
3353340c35deSJonathan Lemon 	/*
3354272c5dfeSJonathan Lemon 	 * Acknowledge the segment if it has data or is not a duplicate ACK.
3355340c35deSJonathan Lemon 	 */
3356272c5dfeSJonathan Lemon 	if (thflags != TH_ACK || tlen != 0 ||
3357272c5dfeSJonathan Lemon 	    th->th_seq != tw->rcv_nxt || th->th_ack != tw->snd_nxt)
3358a7b6a14aSRobert Watson 		tcp_twrespond(tw, TH_ACK);
3359340c35deSJonathan Lemon 	goto drop;
3360340c35deSJonathan Lemon 
3361340c35deSJonathan Lemon 	/*
3362340c35deSJonathan Lemon 	 * Generate a RST, dropping incoming segment.
3363340c35deSJonathan Lemon 	 * Make ACK acceptable to originator of segment.
3364340c35deSJonathan Lemon 	 * Don't bother to respond if destination was broadcast/multicast.
3365340c35deSJonathan Lemon 	 */
3366340c35deSJonathan Lemon 	if (m->m_flags & (M_BCAST|M_MCAST))
3367340c35deSJonathan Lemon 		goto drop;
3368340c35deSJonathan Lemon 	if (isipv6) {
3369340c35deSJonathan Lemon 		struct ip6_hdr *ip6;
3370340c35deSJonathan Lemon 
3371340c35deSJonathan Lemon 		/* IPv6 anycast check is done at tcp6_input() */
3372340c35deSJonathan Lemon 		ip6 = mtod(m, struct ip6_hdr *);
3373340c35deSJonathan Lemon 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
3374340c35deSJonathan Lemon 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
3375340c35deSJonathan Lemon 			goto drop;
3376340c35deSJonathan Lemon 	} else {
3377340c35deSJonathan Lemon 		struct ip *ip;
3378340c35deSJonathan Lemon 
3379340c35deSJonathan Lemon 		ip = mtod(m, struct ip *);
3380340c35deSJonathan Lemon 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
3381340c35deSJonathan Lemon 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
3382340c35deSJonathan Lemon 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
3383340c35deSJonathan Lemon 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
3384340c35deSJonathan Lemon 			goto drop;
3385340c35deSJonathan Lemon 	}
3386340c35deSJonathan Lemon 	if (thflags & TH_ACK) {
3387340c35deSJonathan Lemon 		tcp_respond(NULL,
3388340c35deSJonathan Lemon 		    mtod(m, void *), th, m, 0, th->th_ack, TH_RST);
3389340c35deSJonathan Lemon 	} else {
3390340c35deSJonathan Lemon 		seq = th->th_seq + (thflags & TH_SYN ? 1 : 0);
3391340c35deSJonathan Lemon 		tcp_respond(NULL,
3392340c35deSJonathan Lemon 		    mtod(m, void *), th, m, seq, 0, TH_RST|TH_ACK);
3393340c35deSJonathan Lemon 	}
33943cbe7fafSRobert Watson 	INP_UNLOCK(inp);
3395340c35deSJonathan Lemon 	return (0);
3396340c35deSJonathan Lemon 
3397340c35deSJonathan Lemon drop:
33983cbe7fafSRobert Watson 	INP_UNLOCK(inp);
3399340c35deSJonathan Lemon 	m_freem(m);
3400340c35deSJonathan Lemon 	return (0);
3401340c35deSJonathan Lemon }
3402