xref: /freebsd/sys/netinet/tcp_input.c (revision 283c76c7c3f2f634f19f303a771a3f81fe890cab)
1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4e79adb8eSGarrett Wollman  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6dbc42409SLawrence Stewart  * Copyright (c) 2007-2008,2010
7dbc42409SLawrence Stewart  *	Swinburne University of Technology, Melbourne, Australia.
8dbc42409SLawrence Stewart  * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org>
9dbc42409SLawrence Stewart  * Copyright (c) 2010 The FreeBSD Foundation
10fa046d87SRobert Watson  * Copyright (c) 2010-2011 Juniper Networks, Inc.
11dbc42409SLawrence Stewart  * All rights reserved.
12dbc42409SLawrence Stewart  *
13dbc42409SLawrence Stewart  * Portions of this software were developed at the Centre for Advanced Internet
14891b8ed4SLawrence Stewart  * Architectures, Swinburne University of Technology, by Lawrence Stewart,
15891b8ed4SLawrence Stewart  * James Healy and David Hayes, made possible in part by a grant from the Cisco
16891b8ed4SLawrence Stewart  * University Research Program Fund at Community Foundation Silicon Valley.
17dbc42409SLawrence Stewart  *
18dbc42409SLawrence Stewart  * Portions of this software were developed at the Centre for Advanced
19dbc42409SLawrence Stewart  * Internet Architectures, Swinburne University of Technology, Melbourne,
20dbc42409SLawrence Stewart  * Australia by David Hayes under sponsorship from the FreeBSD Foundation.
21df8bae1dSRodney W. Grimes  *
22fa046d87SRobert Watson  * Portions of this software were developed by Robert N. M. Watson under
23fa046d87SRobert Watson  * contract to Juniper Networks, Inc.
24fa046d87SRobert Watson  *
25df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
26df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
27df8bae1dSRodney W. Grimes  * are met:
28df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
29df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
30df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
31df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
32df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
33fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
34df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
35df8bae1dSRodney W. Grimes  *    without specific prior written permission.
36df8bae1dSRodney W. Grimes  *
37df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
38df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
41df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
48df8bae1dSRodney W. Grimes  *
49e79adb8eSGarrett Wollman  *	@(#)tcp_input.c	8.12 (Berkeley) 5/24/95
50df8bae1dSRodney W. Grimes  */
51df8bae1dSRodney W. Grimes 
524b421e2dSMike Silbersack #include <sys/cdefs.h>
534b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
544b421e2dSMike Silbersack 
551cfd4b53SBruce M Simpson #include "opt_inet.h"
56fb59c426SYoshinobu Inoue #include "opt_inet6.h"
573a2a9f79SYoshinobu Inoue #include "opt_ipsec.h"
580cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
590cc12cc5SJoerg Wunsch 
60df8bae1dSRodney W. Grimes #include <sys/param.h>
61adc56f5aSEdward Tomasz Napierala #include <sys/arb.h>
6298163b98SPoul-Henning Kamp #include <sys/kernel.h>
63bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
6439bc9de5SLawrence Stewart #include <sys/hhook.h>
65bd79708dSJonathan T. Looney #endif
66df8bae1dSRodney W. Grimes #include <sys/malloc.h>
67df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
68a29f300eSGarrett Wollman #include <sys/proc.h>		/* for proc0 declaration */
69df8bae1dSRodney W. Grimes #include <sys/protosw.h>
70adc56f5aSEdward Tomasz Napierala #include <sys/qmath.h>
7157f60867SMark Johnston #include <sys/sdt.h>
72960ed29cSSeigo Tanimura #include <sys/signalvar.h>
73df8bae1dSRodney W. Grimes #include <sys/socket.h>
74df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
75960ed29cSSeigo Tanimura #include <sys/sysctl.h>
76816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
77960ed29cSSeigo Tanimura #include <sys/systm.h>
78adc56f5aSEdward Tomasz Napierala #include <sys/stats.h>
79df8bae1dSRodney W. Grimes 
80e79adb8eSGarrett Wollman #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
81e79adb8eSGarrett Wollman 
8212e2e970SAndre Oppermann #include <vm/uma.h>
8312e2e970SAndre Oppermann 
84df8bae1dSRodney W. Grimes #include <net/if.h>
8576039bc8SGleb Smirnoff #include <net/if_var.h>
86df8bae1dSRodney W. Grimes #include <net/route.h>
87530c0060SRobert Watson #include <net/vnet.h>
88df8bae1dSRodney W. Grimes 
89773673c1SAndre Oppermann #define TCPSTATES		/* for logging */
90773673c1SAndre Oppermann 
91df8bae1dSRodney W. Grimes #include <netinet/in.h>
9257f60867SMark Johnston #include <netinet/in_kdtrace.h>
93960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
94df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
95df8bae1dSRodney W. Grimes #include <netinet/ip.h>
968e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h>	/* required for icmp_var.h */
97686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM */
98df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
99ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
100686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
101686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
102686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
103b66d74c1SGleb Smirnoff #include <netinet6/in6_var.h>
104960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h>
105960ed29cSSeigo Tanimura #include <netinet6/nd6.h>
1062de3e790SGleb Smirnoff #include <netinet/tcp.h>
107df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
1082529f56eSJonathan T. Looney #include <netinet/tcp_log_buf.h>
109df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
110df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
111df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
112fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h>
113df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
1144644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
115c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h>
11686a996e6SHiren Panchasara #ifdef TCPPCAP
11786a996e6SHiren Panchasara #include <netinet/tcp_pcap.h>
11886a996e6SHiren Panchasara #endif
119c325962bSMike Silbersack #include <netinet/tcp_syncache.h>
120610ee2f9SDavid Greenman #ifdef TCPDEBUG
121df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
122fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */
12309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
12409fe6320SNavdeep Parhar #include <netinet/tcp_offload.h>
12509fe6320SNavdeep Parhar #endif
126fb59c426SYoshinobu Inoue 
127fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
128b9234fafSSam Leffler 
129db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
130db4f9cc7SJonathan Lemon 
131aed55708SRobert Watson #include <security/mac/mac_framework.h>
132aed55708SRobert Watson 
133dbc42409SLawrence Stewart const int tcprexmtthresh = 3;
1340312fbe9SPoul-Henning Kamp 
135334fc582SBjoern A. Zeeb VNET_DEFINE(int, tcp_log_in_vain) = 0;
136334fc582SBjoern A. Zeeb SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_VNET | CTLFLAG_RW,
137334fc582SBjoern A. Zeeb     &VNET_NAME(tcp_log_in_vain), 0,
138eddfbb76SRobert Watson     "Log all incoming TCP segments to closed ports");
139816a3d83SPoul-Henning Kamp 
14082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, blackhole) = 0;
14182cea7e6SBjoern A. Zeeb #define	V_blackhole		VNET(blackhole)
1426df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW,
143eddfbb76SRobert Watson     &VNET_NAME(blackhole), 0,
144eddfbb76SRobert Watson     "Do not send RST on segments to closed ports");
14516f7f31fSGeoff Rehmet 
14682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_delack_enabled) = 1;
1476df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_VNET | CTLFLAG_RW,
148eddfbb76SRobert Watson     &VNET_NAME(tcp_delack_enabled), 0,
1493d177f46SBill Fumerola     "Delay ACK to try and piggyback it onto a data packet");
150f498eeeeSDavid Greenman 
15182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, drop_synfin) = 0;
1526df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_VNET | CTLFLAG_RW,
153eddfbb76SRobert Watson     &VNET_NAME(drop_synfin), 0,
154eddfbb76SRobert Watson     "Drop TCP packets with SYN+FIN set");
155f8613305SDag-Erling Smørgrav 
156b72e56e7SMichael Tuexen VNET_DEFINE(int, tcp_do_newcwv) = 0;
157b72e56e7SMichael Tuexen SYSCTL_INT(_net_inet_tcp, OID_AUTO, newcwv, CTLFLAG_VNET | CTLFLAG_RW,
158b72e56e7SMichael Tuexen     &VNET_NAME(tcp_do_newcwv), 0,
159b72e56e7SMichael Tuexen     "Enable New Congestion Window Validation per RFC7661");
160b72e56e7SMichael Tuexen 
16112eeb81fSHiren Panchasara VNET_DEFINE(int, tcp_do_rfc6675_pipe) = 0;
162054d38e3SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc6675_pipe, CTLFLAG_VNET | CTLFLAG_RW,
16312eeb81fSHiren Panchasara     &VNET_NAME(tcp_do_rfc6675_pipe), 0,
16412eeb81fSHiren Panchasara     "Use calculated pipe/in-flight bytes per RFC 6675");
16512eeb81fSHiren Panchasara 
16682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3042) = 1;
1676df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_VNET | CTLFLAG_RW,
168eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3042), 0,
169eddfbb76SRobert Watson     "Enable RFC 3042 (Limited Transmit)");
170582a954bSJeffrey Hsu 
17182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3390) = 1;
1726df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_VNET | CTLFLAG_RW,
173eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3390), 0,
174da3a8a1aSJeffrey Hsu     "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)");
175da3a8a1aSJeffrey Hsu 
176356c7958SHiren Panchasara VNET_DEFINE(int, tcp_initcwnd_segments) = 10;
177356c7958SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, initcwnd_segments,
178356c7958SHiren Panchasara     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_initcwnd_segments), 0,
179356c7958SHiren Panchasara     "Slow-start flight size (initial congestion window) in number of segments");
18009440655SAndre Oppermann 
18182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3465) = 1;
1826df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_VNET | CTLFLAG_RW,
183eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3465), 0,
18424cb0f22SLawrence Stewart     "Enable RFC 3465 (Appropriate Byte Counting)");
185eddfbb76SRobert Watson 
18682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_abc_l_var) = 2;
1876df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_VNET | CTLFLAG_RW,
188eddfbb76SRobert Watson     &VNET_NAME(tcp_abc_l_var), 2,
18924cb0f22SLawrence Stewart     "Cap the max cwnd increment during slow-start to this number of segments");
19024cb0f22SLawrence Stewart 
1917029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn,
1927029da5cSPawel Biernacki     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1937029da5cSPawel Biernacki     "TCP ECN");
194f2512ba1SRui Paulo 
195883054b4SDon Lewis VNET_DEFINE(int, tcp_do_ecn) = 2;
1966df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW,
197eddfbb76SRobert Watson     &VNET_NAME(tcp_do_ecn), 0,
198eddfbb76SRobert Watson     "TCP ECN support");
199eddfbb76SRobert Watson 
20082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_ecn_maxretries) = 1;
2016df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, maxretries, CTLFLAG_VNET | CTLFLAG_RW,
202eddfbb76SRobert Watson     &VNET_NAME(tcp_ecn_maxretries), 0,
203eddfbb76SRobert Watson     "Max retries before giving up on ECN");
204eddfbb76SRobert Watson 
2053220a212SGleb Smirnoff VNET_DEFINE(int, tcp_insecure_syn) = 0;
2066df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_syn, CTLFLAG_VNET | CTLFLAG_RW,
2073220a212SGleb Smirnoff     &VNET_NAME(tcp_insecure_syn), 0,
2083220a212SGleb Smirnoff     "Follow RFC793 instead of RFC5961 criteria for accepting SYN packets");
2093220a212SGleb Smirnoff 
21082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_insecure_rst) = 0;
2116df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_VNET | CTLFLAG_RW,
212eddfbb76SRobert Watson     &VNET_NAME(tcp_insecure_rst), 0,
2133220a212SGleb Smirnoff     "Follow RFC793 instead of RFC5961 criteria for accepting RST packets");
214a69968eeSMike Silbersack 
215fba0cea1SBjoern A. Zeeb VNET_DEFINE(int, tcp_recvspace) = 1024*64;
216873789cbSAndre Oppermann #define	V_tcp_recvspace	VNET(tcp_recvspace)
2176df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_VNET | CTLFLAG_RW,
218873789cbSAndre Oppermann     &VNET_NAME(tcp_recvspace), 0, "Initial receive socket buffer size");
219873789cbSAndre Oppermann 
22082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autorcvbuf) = 1;
2216df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_VNET | CTLFLAG_RW,
222eddfbb76SRobert Watson     &VNET_NAME(tcp_do_autorcvbuf), 0,
2238b615593SMarko Zec     "Enable automatic receive buffer sizing");
2246741ecf5SAndre Oppermann 
225b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024;
2266df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_VNET | CTLFLAG_RW,
227eddfbb76SRobert Watson     &VNET_NAME(tcp_autorcvbuf_max), 0,
2288b615593SMarko Zec     "Max size of automatic receive buffer");
2296741ecf5SAndre Oppermann 
230eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, tcb);
23144e33a07SMarko Zec #define	tcb6	tcb  /* for KAME src sync over BSD*'s */
23282cea7e6SBjoern A. Zeeb VNET_DEFINE(struct inpcbinfo, tcbinfo);
233df8bae1dSRodney W. Grimes 
234315e3e38SRobert Watson /*
235bf840a17SGleb Smirnoff  * TCP statistics are stored in an array of counter(9)s, which size matches
236bf840a17SGleb Smirnoff  * size of struct tcpstat.  TCP running connection count is a regular array.
2375923c293SGleb Smirnoff  */
2385da0521fSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct tcpstat, tcpstat);
2395da0521fSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_tcp, TCPCTL_STATS, stats, struct tcpstat,
2405da0521fSAndrey V. Elsukov     tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
241bf840a17SGleb Smirnoff VNET_DEFINE(counter_u64_t, tcps_states[TCP_NSTATES]);
242bf840a17SGleb Smirnoff SYSCTL_COUNTER_U64_ARRAY(_net_inet_tcp, TCPCTL_STATES, states, CTLFLAG_RD |
243d4d32b9fSHans Petter Selasky     CTLFLAG_VNET, &VNET_NAME(tcps_states)[0], TCP_NSTATES,
244bf840a17SGleb Smirnoff     "TCP connection counts by TCP state");
245bf840a17SGleb Smirnoff 
246bf840a17SGleb Smirnoff static void
247bf840a17SGleb Smirnoff tcp_vnet_init(const void *unused)
248bf840a17SGleb Smirnoff {
249bf840a17SGleb Smirnoff 
250f59d975eSGleb Smirnoff 	COUNTER_ARRAY_ALLOC(V_tcps_states, TCP_NSTATES, M_WAITOK);
251bf840a17SGleb Smirnoff 	VNET_PCPUSTAT_ALLOC(tcpstat, M_WAITOK);
252bf840a17SGleb Smirnoff }
253bf840a17SGleb Smirnoff VNET_SYSINIT(tcp_vnet_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
254bf840a17SGleb Smirnoff     tcp_vnet_init, NULL);
2555923c293SGleb Smirnoff 
2565923c293SGleb Smirnoff #ifdef VIMAGE
257bf840a17SGleb Smirnoff static void
258bf840a17SGleb Smirnoff tcp_vnet_uninit(const void *unused)
259bf840a17SGleb Smirnoff {
260bf840a17SGleb Smirnoff 
261f59d975eSGleb Smirnoff 	COUNTER_ARRAY_FREE(V_tcps_states, TCP_NSTATES);
262bf840a17SGleb Smirnoff 	VNET_PCPUSTAT_FREE(tcpstat);
263bf840a17SGleb Smirnoff }
264bf840a17SGleb Smirnoff VNET_SYSUNINIT(tcp_vnet_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
265bf840a17SGleb Smirnoff     tcp_vnet_uninit, NULL);
2665923c293SGleb Smirnoff #endif /* VIMAGE */
267bf840a17SGleb Smirnoff 
2685923c293SGleb Smirnoff /*
2697ca6e296SMichael Tuexen  * Kernel module interface for updating tcpstat.  The first argument is an index
2705923c293SGleb Smirnoff  * into tcpstat treated as an array.
271315e3e38SRobert Watson  */
272315e3e38SRobert Watson void
2737ca6e296SMichael Tuexen kmod_tcpstat_add(int statnum, int val)
274315e3e38SRobert Watson {
275315e3e38SRobert Watson 
2767ca6e296SMichael Tuexen 	counter_u64_add(VNET(tcpstat)[statnum], val);
277315e3e38SRobert Watson }
278315e3e38SRobert Watson 
279bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
280dbc42409SLawrence Stewart /*
28139bc9de5SLawrence Stewart  * Wrapper for the TCP established input helper hook.
28239bc9de5SLawrence Stewart  */
28355bceb1eSRandall Stewart void
28439bc9de5SLawrence Stewart hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to)
28539bc9de5SLawrence Stewart {
28639bc9de5SLawrence Stewart 	struct tcp_hhook_data hhook_data;
28739bc9de5SLawrence Stewart 
28839bc9de5SLawrence Stewart 	if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) {
28939bc9de5SLawrence Stewart 		hhook_data.tp = tp;
29039bc9de5SLawrence Stewart 		hhook_data.th = th;
29139bc9de5SLawrence Stewart 		hhook_data.to = to;
29239bc9de5SLawrence Stewart 
29339bc9de5SLawrence Stewart 		hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data,
29439bc9de5SLawrence Stewart 		    tp->osd);
29539bc9de5SLawrence Stewart 	}
29639bc9de5SLawrence Stewart }
297bd79708dSJonathan T. Looney #endif
29839bc9de5SLawrence Stewart 
29939bc9de5SLawrence Stewart /*
300dbc42409SLawrence Stewart  * CC wrapper hook functions
301dbc42409SLawrence Stewart  */
30255bceb1eSRandall Stewart void
3034b7b743cSLawrence Stewart cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t nsegs,
3044b7b743cSLawrence Stewart     uint16_t type)
305f2512ba1SRui Paulo {
306adc56f5aSEdward Tomasz Napierala #ifdef STATS
307adc56f5aSEdward Tomasz Napierala 	int32_t gput;
308adc56f5aSEdward Tomasz Napierala #endif
309adc56f5aSEdward Tomasz Napierala 
310dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
311f2512ba1SRui Paulo 
3124b7b743cSLawrence Stewart 	tp->ccv->nsegs = nsegs;
313dbc42409SLawrence Stewart 	tp->ccv->bytes_this_ack = BYTES_THIS_ACK(tp, th);
314b72e56e7SMichael Tuexen 	if ((!V_tcp_do_newcwv && (tp->snd_cwnd <= tp->snd_wnd)) ||
315b72e56e7SMichael Tuexen 	    (V_tcp_do_newcwv && (tp->snd_cwnd <= tp->snd_wnd) &&
316b72e56e7SMichael Tuexen 	     (tp->snd_cwnd < (tcp_compute_pipe(tp) * 2))))
317dbc42409SLawrence Stewart 		tp->ccv->flags |= CCF_CWND_LIMITED;
318dbc42409SLawrence Stewart 	else
319dbc42409SLawrence Stewart 		tp->ccv->flags &= ~CCF_CWND_LIMITED;
320dbc42409SLawrence Stewart 
321dbc42409SLawrence Stewart 	if (type == CC_ACK) {
322adc56f5aSEdward Tomasz Napierala #ifdef STATS
323adc56f5aSEdward Tomasz Napierala 		stats_voi_update_abs_s32(tp->t_stats, VOI_TCP_CALCFRWINDIFF,
324adc56f5aSEdward Tomasz Napierala 		    ((int32_t)tp->snd_cwnd) - tp->snd_wnd);
325adc56f5aSEdward Tomasz Napierala 		if (!IN_RECOVERY(tp->t_flags))
326adc56f5aSEdward Tomasz Napierala 			stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_ACKLEN,
327adc56f5aSEdward Tomasz Napierala 			   tp->ccv->bytes_this_ack / (tcp_maxseg(tp) * nsegs));
328adc56f5aSEdward Tomasz Napierala 		if ((tp->t_flags & TF_GPUTINPROG) &&
329adc56f5aSEdward Tomasz Napierala 		    SEQ_GEQ(th->th_ack, tp->gput_ack)) {
330adc56f5aSEdward Tomasz Napierala 			/*
331adc56f5aSEdward Tomasz Napierala 			 * Compute goodput in bits per millisecond.
332adc56f5aSEdward Tomasz Napierala 			 */
333adc56f5aSEdward Tomasz Napierala 			gput = (((int64_t)(th->th_ack - tp->gput_seq)) << 3) /
334adc56f5aSEdward Tomasz Napierala 			    max(1, tcp_ts_getticks() - tp->gput_ts);
335adc56f5aSEdward Tomasz Napierala 			stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT,
336adc56f5aSEdward Tomasz Napierala 			    gput);
337adc56f5aSEdward Tomasz Napierala 			/*
338adc56f5aSEdward Tomasz Napierala 			 * XXXLAS: This is a temporary hack, and should be
339adc56f5aSEdward Tomasz Napierala 			 * chained off VOI_TCP_GPUT when stats(9) grows an API
340adc56f5aSEdward Tomasz Napierala 			 * to deal with chained VOIs.
341adc56f5aSEdward Tomasz Napierala 			 */
342adc56f5aSEdward Tomasz Napierala 			if (tp->t_stats_gput_prev > 0)
343adc56f5aSEdward Tomasz Napierala 				stats_voi_update_abs_s32(tp->t_stats,
344adc56f5aSEdward Tomasz Napierala 				    VOI_TCP_GPUT_ND,
345adc56f5aSEdward Tomasz Napierala 				    ((gput - tp->t_stats_gput_prev) * 100) /
346adc56f5aSEdward Tomasz Napierala 				    tp->t_stats_gput_prev);
347adc56f5aSEdward Tomasz Napierala 			tp->t_flags &= ~TF_GPUTINPROG;
348adc56f5aSEdward Tomasz Napierala 			tp->t_stats_gput_prev = gput;
349adc56f5aSEdward Tomasz Napierala 		}
350adc56f5aSEdward Tomasz Napierala #endif /* STATS */
351dbc42409SLawrence Stewart 		if (tp->snd_cwnd > tp->snd_ssthresh) {
3521951fa79SMichael Tuexen 			tp->t_bytes_acked += tp->ccv->bytes_this_ack;
353dbc42409SLawrence Stewart 			if (tp->t_bytes_acked >= tp->snd_cwnd) {
354dbc42409SLawrence Stewart 				tp->t_bytes_acked -= tp->snd_cwnd;
355dbc42409SLawrence Stewart 				tp->ccv->flags |= CCF_ABC_SENTAWND;
356dbc42409SLawrence Stewart 			}
357dbc42409SLawrence Stewart 		} else {
358dbc42409SLawrence Stewart 				tp->ccv->flags &= ~CCF_ABC_SENTAWND;
359dbc42409SLawrence Stewart 				tp->t_bytes_acked = 0;
360dbc42409SLawrence Stewart 		}
361dbc42409SLawrence Stewart 	}
362dbc42409SLawrence Stewart 
363dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->ack_received != NULL) {
364dbc42409SLawrence Stewart 		/* XXXLAS: Find a way to live without this */
365dbc42409SLawrence Stewart 		tp->ccv->curack = th->th_ack;
366dbc42409SLawrence Stewart 		CC_ALGO(tp)->ack_received(tp->ccv, type);
367dbc42409SLawrence Stewart 	}
368adc56f5aSEdward Tomasz Napierala #ifdef STATS
369adc56f5aSEdward Tomasz Napierala 	stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_LCWIN, tp->snd_cwnd);
370adc56f5aSEdward Tomasz Napierala #endif
371dbc42409SLawrence Stewart }
372dbc42409SLawrence Stewart 
37355bceb1eSRandall Stewart void
374dbc42409SLawrence Stewart cc_conn_init(struct tcpcb *tp)
375dbc42409SLawrence Stewart {
376dbc42409SLawrence Stewart 	struct hc_metrics_lite metrics;
377dbc42409SLawrence Stewart 	struct inpcb *inp = tp->t_inpcb;
3780c39d38dSGleb Smirnoff 	u_int maxseg;
379dbc42409SLawrence Stewart 	int rtt;
380dbc42409SLawrence Stewart 
381dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
382dbc42409SLawrence Stewart 
383dbc42409SLawrence Stewart 	tcp_hc_get(&inp->inp_inc, &metrics);
3840c39d38dSGleb Smirnoff 	maxseg = tcp_maxseg(tp);
385dbc42409SLawrence Stewart 
386dbc42409SLawrence Stewart 	if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) {
387dbc42409SLawrence Stewart 		tp->t_srtt = rtt;
388dbc42409SLawrence Stewart 		tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE;
389dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_usedrtt);
390dbc42409SLawrence Stewart 		if (metrics.rmx_rttvar) {
391dbc42409SLawrence Stewart 			tp->t_rttvar = metrics.rmx_rttvar;
392dbc42409SLawrence Stewart 			TCPSTAT_INC(tcps_usedrttvar);
393dbc42409SLawrence Stewart 		} else {
394dbc42409SLawrence Stewart 			/* default variation is +- 1 rtt */
395dbc42409SLawrence Stewart 			tp->t_rttvar =
396dbc42409SLawrence Stewart 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
397dbc42409SLawrence Stewart 		}
398dbc42409SLawrence Stewart 		TCPT_RANGESET(tp->t_rxtcur,
399dbc42409SLawrence Stewart 		    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
400dbc42409SLawrence Stewart 		    tp->t_rttmin, TCPTV_REXMTMAX);
401dbc42409SLawrence Stewart 	}
402dbc42409SLawrence Stewart 	if (metrics.rmx_ssthresh) {
403dbc42409SLawrence Stewart 		/*
404dbc42409SLawrence Stewart 		 * There's some sort of gateway or interface
405dbc42409SLawrence Stewart 		 * buffer limit on the path.  Use this to set
406a4641f4eSPedro F. Giffuni 		 * the slow start threshold, but set the
407dbc42409SLawrence Stewart 		 * threshold to no less than 2*mss.
408dbc42409SLawrence Stewart 		 */
4090c39d38dSGleb Smirnoff 		tp->snd_ssthresh = max(2 * maxseg, metrics.rmx_ssthresh);
410dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_usedssthresh);
411dbc42409SLawrence Stewart 	}
412dbc42409SLawrence Stewart 
413dbc42409SLawrence Stewart 	/*
4149ec4a4ccSAndre Oppermann 	 * Set the initial slow-start flight size.
415dbc42409SLawrence Stewart 	 *
416cf8f04f4SAndre Oppermann 	 * If a SYN or SYN/ACK was lost and retransmitted, we have to
417cf8f04f4SAndre Oppermann 	 * reduce the initial CWND to one segment as congestion is likely
418cf8f04f4SAndre Oppermann 	 * requiring us to be cautious.
419dbc42409SLawrence Stewart 	 */
420cf8f04f4SAndre Oppermann 	if (tp->snd_cwnd == 1)
4210c39d38dSGleb Smirnoff 		tp->snd_cwnd = maxseg;		/* SYN(-ACK) lost */
422dbc42409SLawrence Stewart 	else
4237dc90a1dSMichael Tuexen 		tp->snd_cwnd = tcp_compute_initwnd(maxseg);
424dbc42409SLawrence Stewart 
425dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->conn_init != NULL)
426dbc42409SLawrence Stewart 		CC_ALGO(tp)->conn_init(tp->ccv);
427dbc42409SLawrence Stewart }
428dbc42409SLawrence Stewart 
429dbc42409SLawrence Stewart void inline
430dbc42409SLawrence Stewart cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type)
431dbc42409SLawrence Stewart {
432dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
433dbc42409SLawrence Stewart 
434adc56f5aSEdward Tomasz Napierala #ifdef STATS
435adc56f5aSEdward Tomasz Napierala 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_CSIG, type);
436adc56f5aSEdward Tomasz Napierala #endif
437adc56f5aSEdward Tomasz Napierala 
438dbc42409SLawrence Stewart 	switch(type) {
439dbc42409SLawrence Stewart 	case CC_NDUPACK:
440dbc42409SLawrence Stewart 		if (!IN_FASTRECOVERY(tp->t_flags)) {
441f2512ba1SRui Paulo 			tp->snd_recover = tp->snd_max;
4423cf38784SMichael Tuexen 			if (tp->t_flags2 & TF2_ECN_PERMIT)
4433cf38784SMichael Tuexen 				tp->t_flags2 |= TF2_ECN_SND_CWR;
444f2512ba1SRui Paulo 		}
445dbc42409SLawrence Stewart 		break;
446dbc42409SLawrence Stewart 	case CC_ECN:
447af2fb894SRichard Scheffenegger 		if (!IN_CONGRECOVERY(tp->t_flags) ||
448af2fb894SRichard Scheffenegger 		    /*
449af2fb894SRichard Scheffenegger 		     * Allow ECN reaction on ACK to CWR, if
450af2fb894SRichard Scheffenegger 		     * that data segment was also CE marked.
451af2fb894SRichard Scheffenegger 		     */
452af2fb894SRichard Scheffenegger 		    SEQ_GEQ(th->th_ack, tp->snd_recover)) {
453af2fb894SRichard Scheffenegger 			EXIT_CONGRECOVERY(tp->t_flags);
454dbc42409SLawrence Stewart 			TCPSTAT_INC(tcps_ecn_rcwnd);
455af2fb894SRichard Scheffenegger 			tp->snd_recover = tp->snd_max + 1;
4563cf38784SMichael Tuexen 			if (tp->t_flags2 & TF2_ECN_PERMIT)
4573cf38784SMichael Tuexen 				tp->t_flags2 |= TF2_ECN_SND_CWR;
458dbc42409SLawrence Stewart 		}
459dbc42409SLawrence Stewart 		break;
460dbc42409SLawrence Stewart 	case CC_RTO:
461dbc42409SLawrence Stewart 		tp->t_dupacks = 0;
462dbc42409SLawrence Stewart 		tp->t_bytes_acked = 0;
463dbc42409SLawrence Stewart 		EXIT_RECOVERY(tp->t_flags);
4649cc711c9SMichael Tuexen 		if (tp->t_flags2 & TF2_ECN_PERMIT)
4659cc711c9SMichael Tuexen 			tp->t_flags2 |= TF2_ECN_SND_CWR;
466dbc42409SLawrence Stewart 		break;
467dbc42409SLawrence Stewart 	case CC_RTO_ERR:
468dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_sndrexmitbad);
469dbc42409SLawrence Stewart 		/* RTO was unnecessary, so reset everything. */
470dbc42409SLawrence Stewart 		tp->snd_cwnd = tp->snd_cwnd_prev;
471dbc42409SLawrence Stewart 		tp->snd_ssthresh = tp->snd_ssthresh_prev;
472dbc42409SLawrence Stewart 		tp->snd_recover = tp->snd_recover_prev;
473dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASFRECOVERY)
474dbc42409SLawrence Stewart 			ENTER_FASTRECOVERY(tp->t_flags);
475dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASCRECOVERY)
476dbc42409SLawrence Stewart 			ENTER_CONGRECOVERY(tp->t_flags);
477dbc42409SLawrence Stewart 		tp->snd_nxt = tp->snd_max;
478672dc4aeSJohn Baldwin 		tp->t_flags &= ~TF_PREVVALID;
479dbc42409SLawrence Stewart 		tp->t_badrxtwin = 0;
480dbc42409SLawrence Stewart 		break;
481dbc42409SLawrence Stewart 	}
482dbc42409SLawrence Stewart 
483dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->cong_signal != NULL) {
484dbc42409SLawrence Stewart 		if (th != NULL)
485dbc42409SLawrence Stewart 			tp->ccv->curack = th->th_ack;
486dbc42409SLawrence Stewart 		CC_ALGO(tp)->cong_signal(tp->ccv, type);
487dbc42409SLawrence Stewart 	}
488dbc42409SLawrence Stewart }
489dbc42409SLawrence Stewart 
49055bceb1eSRandall Stewart void inline
491dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th)
492dbc42409SLawrence Stewart {
493dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
494dbc42409SLawrence Stewart 
495dbc42409SLawrence Stewart 	/* XXXLAS: KASSERT that we're in recovery? */
496dbc42409SLawrence Stewart 
497dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->post_recovery != NULL) {
498dbc42409SLawrence Stewart 		tp->ccv->curack = th->th_ack;
499dbc42409SLawrence Stewart 		CC_ALGO(tp)->post_recovery(tp->ccv);
500dbc42409SLawrence Stewart 	}
501dbc42409SLawrence Stewart 	/* XXXLAS: EXIT_RECOVERY ? */
502dbc42409SLawrence Stewart 	tp->t_bytes_acked = 0;
503dbc42409SLawrence Stewart }
5040312fbe9SPoul-Henning Kamp 
505df8bae1dSRodney W. Grimes /*
506262c1c1aSMatthew Dillon  * Indicate whether this ack should be delayed.  We can delay the ack if
50785536381SHiren Panchasara  * following conditions are met:
50885536381SHiren Panchasara  *	- There is no delayed ack timer in progress.
50985536381SHiren Panchasara  *	- Our last ack wasn't a 0-sized window. We never want to delay
51085536381SHiren Panchasara  *	  the ack that opens up a 0-sized window.
51185536381SHiren Panchasara  *	- LRO wasn't used for this segment. We make sure by checking that the
51285536381SHiren Panchasara  *	  segment size is not larger than the MSS.
513d8c85a26SJonathan Lemon  */
514c1e5a6e5SAndre Oppermann #define DELAY_ACK(tp, tlen)						\
515b8152ba7SAndre Oppermann 	((!tcp_timer_active(tp, TT_DELACK) &&				\
516a14c749fSJonathan Lemon 	    (tp->t_flags & TF_RXWIN0SENT) == 0) &&			\
5170c39d38dSGleb Smirnoff 	    (tlen <= tp->t_maxseg) &&					\
518603724d3SBjoern A. Zeeb 	    (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN)))
519d8c85a26SJonathan Lemon 
5204ad24737SRandall Stewart void inline
52164807b30SHiren Panchasara cc_ecnpkt_handler(struct tcpcb *tp, struct tcphdr *th, uint8_t iptos)
52264807b30SHiren Panchasara {
52364807b30SHiren Panchasara 	INP_WLOCK_ASSERT(tp->t_inpcb);
52464807b30SHiren Panchasara 
52564807b30SHiren Panchasara 	if (CC_ALGO(tp)->ecnpkt_handler != NULL) {
52664807b30SHiren Panchasara 		switch (iptos & IPTOS_ECN_MASK) {
52764807b30SHiren Panchasara 		case IPTOS_ECN_CE:
52864807b30SHiren Panchasara 			tp->ccv->flags |= CCF_IPHDR_CE;
52964807b30SHiren Panchasara 			break;
53064807b30SHiren Panchasara 		case IPTOS_ECN_ECT0:
53183a2839fSMichael Tuexen 			/* FALLTHROUGH */
53264807b30SHiren Panchasara 		case IPTOS_ECN_ECT1:
53383a2839fSMichael Tuexen 			/* FALLTHROUGH */
53483a2839fSMichael Tuexen 		case IPTOS_ECN_NOTECT:
53564807b30SHiren Panchasara 			tp->ccv->flags &= ~CCF_IPHDR_CE;
53664807b30SHiren Panchasara 			break;
53764807b30SHiren Panchasara 		}
53864807b30SHiren Panchasara 
53964807b30SHiren Panchasara 		if (th->th_flags & TH_CWR)
54064807b30SHiren Panchasara 			tp->ccv->flags |= CCF_TCPHDR_CWR;
54164807b30SHiren Panchasara 		else
54264807b30SHiren Panchasara 			tp->ccv->flags &= ~CCF_TCPHDR_CWR;
54364807b30SHiren Panchasara 
54464807b30SHiren Panchasara 		CC_ALGO(tp)->ecnpkt_handler(tp->ccv);
54564807b30SHiren Panchasara 
546e11c9783SMichael Tuexen 		if (tp->ccv->flags & CCF_ACKNOW) {
54764807b30SHiren Panchasara 			tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
548e11c9783SMichael Tuexen 			tp->t_flags |= TF_ACKNOW;
549e11c9783SMichael Tuexen 		}
55064807b30SHiren Panchasara 	}
55164807b30SHiren Panchasara }
55264807b30SHiren Panchasara 
553df8bae1dSRodney W. Grimes /*
5548d573cc1SAndre Oppermann  * TCP input handling is split into multiple parts:
5558d573cc1SAndre Oppermann  *   tcp6_input is a thin wrapper around tcp_input for the extended
5568d573cc1SAndre Oppermann  *	ip6_protox[] call format in ip6_input
5578d573cc1SAndre Oppermann  *   tcp_input handles primary segment validation, inpcb lookup and
5588d573cc1SAndre Oppermann  *	SYN processing on listen sockets
5598d573cc1SAndre Oppermann  *   tcp_do_segment processes the ACK and text of the segment for
5608d573cc1SAndre Oppermann  *	establishing, established and closing connections
561df8bae1dSRodney W. Grimes  */
562fb59c426SYoshinobu Inoue #ifdef INET6
563fb59c426SYoshinobu Inoue int
564ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto)
565fb59c426SYoshinobu Inoue {
566503f4e47SBjoern A. Zeeb 	struct mbuf *m;
56733841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6;
5683e88eb90SAndrey V. Elsukov 	struct ip6_hdr *ip6;
569fb59c426SYoshinobu Inoue 
570503f4e47SBjoern A. Zeeb 	m = *mp;
571a4adf6ccSBjoern A. Zeeb 	if (m->m_len < *offp + sizeof(struct tcphdr)) {
5724e619b17SBjoern A. Zeeb 		m = m_pullup(m, *offp + sizeof(struct tcphdr));
5734e619b17SBjoern A. Zeeb 		if (m == NULL) {
5744e619b17SBjoern A. Zeeb 			*mp = m;
5754e619b17SBjoern A. Zeeb 			TCPSTAT_INC(tcps_rcvshort);
5764e619b17SBjoern A. Zeeb 			return (IPPROTO_DONE);
5774e619b17SBjoern A. Zeeb 		}
578a4adf6ccSBjoern A. Zeeb 	}
579fb59c426SYoshinobu Inoue 
580fb59c426SYoshinobu Inoue 	/*
581fb59c426SYoshinobu Inoue 	 * draft-itojun-ipv6-tcp-to-anycast
582fb59c426SYoshinobu Inoue 	 * better place to put this in?
583fb59c426SYoshinobu Inoue 	 */
5843e88eb90SAndrey V. Elsukov 	ip6 = mtod(m, struct ip6_hdr *);
5853e88eb90SAndrey V. Elsukov 	ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
58633841545SHajimu UMEMOTO 	if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
5878c0fec80SRobert Watson 		ifa_free(&ia6->ia_ifa);
588fb59c426SYoshinobu Inoue 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
589fb59c426SYoshinobu Inoue 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
590a8fe77d8SBjoern A. Zeeb 		*mp = NULL;
5918f5a8818SKevin Lo 		return (IPPROTO_DONE);
592fb59c426SYoshinobu Inoue 	}
59377d396fdSMaksim Yevmenkin 	if (ia6)
59477d396fdSMaksim Yevmenkin 		ifa_free(&ia6->ia_ifa);
595fb59c426SYoshinobu Inoue 
596a8fe77d8SBjoern A. Zeeb 	*mp = m;
5978f5a8818SKevin Lo 	return (tcp_input(mp, offp, proto));
598fb59c426SYoshinobu Inoue }
599b287c6c7SBjoern A. Zeeb #endif /* INET6 */
600fb59c426SYoshinobu Inoue 
6018f5a8818SKevin Lo int
6028f5a8818SKevin Lo tcp_input(struct mbuf **mp, int *offp, int proto)
603df8bae1dSRodney W. Grimes {
6048f5a8818SKevin Lo 	struct mbuf *m = *mp;
605b287c6c7SBjoern A. Zeeb 	struct tcphdr *th = NULL;
606ad3f9ab3SAndre Oppermann 	struct ip *ip = NULL;
607ad3f9ab3SAndre Oppermann 	struct inpcb *inp = NULL;
608302ce8d6SAndre Oppermann 	struct tcpcb *tp = NULL;
609302ce8d6SAndre Oppermann 	struct socket *so = NULL;
610e79adb8eSGarrett Wollman 	u_char *optp = NULL;
6118f5a8818SKevin Lo 	int off0;
61226f9a767SRodney W. Grimes 	int optlen = 0;
613b287c6c7SBjoern A. Zeeb #ifdef INET
614b287c6c7SBjoern A. Zeeb 	int len;
615c4556b2fSAndrey V. Elsukov 	uint8_t ipttl;
616b287c6c7SBjoern A. Zeeb #endif
617b287c6c7SBjoern A. Zeeb 	int tlen = 0, off;
618fb59c426SYoshinobu Inoue 	int drop_hdrlen;
619ad3f9ab3SAndre Oppermann 	int thflags;
620302ce8d6SAndre Oppermann 	int rstreason = 0;	/* For badport_bandlim accounting purposes */
6211d7ee746SKurt Lidl 	uint8_t iptos;
622c1de64a4SAndrey V. Elsukov 	struct m_tag *fwd_tag = NULL;
623c068736aSJeffrey Hsu #ifdef INET6
624302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6 = NULL;
625c068736aSJeffrey Hsu 	int isipv6;
626c068736aSJeffrey Hsu #else
627a160e630SAndre Oppermann 	const void *ip6 = NULL;
628b287c6c7SBjoern A. Zeeb #endif /* INET6 */
629302ce8d6SAndre Oppermann 	struct tcpopt to;		/* options in this segment */
630a160e630SAndre Oppermann 	char *s = NULL;			/* address and port logging */
631610ee2f9SDavid Greenman #ifdef TCPDEBUG
632c068736aSJeffrey Hsu 	/*
633c068736aSJeffrey Hsu 	 * The size of tcp_saveipgen must be the size of the max ip header,
634c068736aSJeffrey Hsu 	 * now IPv6.
635c068736aSJeffrey Hsu 	 */
63614739780SMaxim Konovalov 	u_char tcp_saveipgen[IP6_HDR_LEN];
637410bb1bfSLuigi Rizzo 	struct tcphdr tcp_savetcp;
638610ee2f9SDavid Greenman 	short ostate = 0;
639610ee2f9SDavid Greenman #endif
640c068736aSJeffrey Hsu 
641d40c0d47SGleb Smirnoff 	NET_EPOCH_ASSERT();
642d40c0d47SGleb Smirnoff 
643fb59c426SYoshinobu Inoue #ifdef INET6
644fb59c426SYoshinobu Inoue 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
645fb59c426SYoshinobu Inoue #endif
646a0292f23SGarrett Wollman 
6478f5a8818SKevin Lo 	off0 = *offp;
6488f5a8818SKevin Lo 	m = *mp;
6498f5a8818SKevin Lo 	*mp = NULL;
650302ce8d6SAndre Oppermann 	to.to_flags = 0;
65178b50714SRobert Watson 	TCPSTAT_INC(tcps_rcvtotal);
652fb59c426SYoshinobu Inoue 
65304d3a452SHajimu UMEMOTO #ifdef INET6
654b287c6c7SBjoern A. Zeeb 	if (isipv6) {
655fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
65645747ba5SBjoern A. Zeeb 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
657fb59c426SYoshinobu Inoue 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
658356ab07eSBjoern A. Zeeb 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) {
65945747ba5SBjoern A. Zeeb 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
66045747ba5SBjoern A. Zeeb 				th->th_sum = m->m_pkthdr.csum_data;
66145747ba5SBjoern A. Zeeb 			else
66245747ba5SBjoern A. Zeeb 				th->th_sum = in6_cksum_pseudo(ip6, tlen,
66345747ba5SBjoern A. Zeeb 				    IPPROTO_TCP, m->m_pkthdr.csum_data);
66445747ba5SBjoern A. Zeeb 			th->th_sum ^= 0xffff;
66545747ba5SBjoern A. Zeeb 		} else
66645747ba5SBjoern A. Zeeb 			th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen);
66745747ba5SBjoern A. Zeeb 		if (th->th_sum) {
66878b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
669fb59c426SYoshinobu Inoue 			goto drop;
670fb59c426SYoshinobu Inoue 		}
67133841545SHajimu UMEMOTO 
67233841545SHajimu UMEMOTO 		/*
67333841545SHajimu UMEMOTO 		 * Be proactive about unspecified IPv6 address in source.
67433841545SHajimu UMEMOTO 		 * As we use all-zero to indicate unbounded/unconnected pcb,
67533841545SHajimu UMEMOTO 		 * unspecified IPv6 address can be used to confuse us.
67633841545SHajimu UMEMOTO 		 *
67733841545SHajimu UMEMOTO 		 * Note that packets with unspecified IPv6 destination is
67833841545SHajimu UMEMOTO 		 * already dropped in ip6_input.
67933841545SHajimu UMEMOTO 		 */
68033841545SHajimu UMEMOTO 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
68133841545SHajimu UMEMOTO 			/* XXX stat */
68233841545SHajimu UMEMOTO 			goto drop;
68333841545SHajimu UMEMOTO 		}
6841d7ee746SKurt Lidl 		iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
685b287c6c7SBjoern A. Zeeb 	}
68604d3a452SHajimu UMEMOTO #endif
687b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
688b287c6c7SBjoern A. Zeeb 	else
689b287c6c7SBjoern A. Zeeb #endif
690b287c6c7SBjoern A. Zeeb #ifdef INET
691b287c6c7SBjoern A. Zeeb 	{
692df8bae1dSRodney W. Grimes 		/*
693df8bae1dSRodney W. Grimes 		 * Get IP and TCP header together in first mbuf.
694df8bae1dSRodney W. Grimes 		 * Note: IP leaves IP header in first mbuf.
695df8bae1dSRodney W. Grimes 		 */
696fb59c426SYoshinobu Inoue 		if (off0 > sizeof (struct ip)) {
697105bd211SGleb Smirnoff 			ip_stripoptions(m);
698fb59c426SYoshinobu Inoue 			off0 = sizeof(struct ip);
699fb59c426SYoshinobu Inoue 		}
700df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof (struct tcpiphdr)) {
7014dfdffe9SAndre Oppermann 			if ((m = m_pullup(m, sizeof (struct tcpiphdr)))
7024dfdffe9SAndre Oppermann 			    == NULL) {
70378b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvshort);
7048f5a8818SKevin Lo 				return (IPPROTO_DONE);
705df8bae1dSRodney W. Grimes 			}
706df8bae1dSRodney W. Grimes 		}
707fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
708db4f9cc7SJonathan Lemon 		th = (struct tcphdr *)((caddr_t)ip + off0);
7098ad458a4SGleb Smirnoff 		tlen = ntohs(ip->ip_len) - off0;
710df8bae1dSRodney W. Grimes 
7111d7ee746SKurt Lidl 		iptos = ip->ip_tos;
712db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
713db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
714db4f9cc7SJonathan Lemon 				th->th_sum = m->m_pkthdr.csum_data;
715db4f9cc7SJonathan Lemon 			else
716db4f9cc7SJonathan Lemon 				th->th_sum = in_pseudo(ip->ip_src.s_addr,
717c068736aSJeffrey Hsu 				    ip->ip_dst.s_addr,
7188f134647SGleb Smirnoff 				    htonl(m->m_pkthdr.csum_data + tlen +
719c068736aSJeffrey Hsu 				    IPPROTO_TCP));
720db4f9cc7SJonathan Lemon 			th->th_sum ^= 0xffff;
721db4f9cc7SJonathan Lemon 		} else {
7228f134647SGleb Smirnoff 			struct ipovly *ipov = (struct ipovly *)ip;
7238f134647SGleb Smirnoff 
724df8bae1dSRodney W. Grimes 			/*
725df8bae1dSRodney W. Grimes 			 * Checksum extended TCP header and data.
726df8bae1dSRodney W. Grimes 			 */
7278f134647SGleb Smirnoff 			len = off0 + tlen;
728c4556b2fSAndrey V. Elsukov 			ipttl = ip->ip_ttl;
729fb59c426SYoshinobu Inoue 			bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
7308f134647SGleb Smirnoff 			ipov->ih_len = htons(tlen);
731fb59c426SYoshinobu Inoue 			th->th_sum = in_cksum(m, len);
73257f60867SMark Johnston 			/* Reset length for SDT probes. */
7331d7ee746SKurt Lidl 			ip->ip_len = htons(len);
7341d7ee746SKurt Lidl 			/* Reset TOS bits */
7351d7ee746SKurt Lidl 			ip->ip_tos = iptos;
7361d7ee746SKurt Lidl 			/* Re-initialization for later version check */
737c4556b2fSAndrey V. Elsukov 			ip->ip_ttl = ipttl;
7381d7ee746SKurt Lidl 			ip->ip_v = IPVERSION;
739b2fe54bcSAndrey V. Elsukov 			ip->ip_hl = off0 >> 2;
740db4f9cc7SJonathan Lemon 		}
74157f60867SMark Johnston 
742fb59c426SYoshinobu Inoue 		if (th->th_sum) {
74378b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
744df8bae1dSRodney W. Grimes 			goto drop;
745df8bae1dSRodney W. Grimes 		}
746fb59c426SYoshinobu Inoue 	}
747b287c6c7SBjoern A. Zeeb #endif /* INET */
748df8bae1dSRodney W. Grimes 
749df8bae1dSRodney W. Grimes 	/*
750df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
751df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
752df8bae1dSRodney W. Grimes 	 */
753fb59c426SYoshinobu Inoue 	off = th->th_off << 2;
754df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
75578b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvbadoff);
756df8bae1dSRodney W. Grimes 		goto drop;
757df8bae1dSRodney W. Grimes 	}
758fb59c426SYoshinobu Inoue 	tlen -= off;	/* tlen is used instead of ti->ti_len */
759df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
76004d3a452SHajimu UMEMOTO #ifdef INET6
761b287c6c7SBjoern A. Zeeb 		if (isipv6) {
762a4adf6ccSBjoern A. Zeeb 			if (m->m_len < off0 + off) {
7634e619b17SBjoern A. Zeeb 				m = m_pullup(m, off0 + off);
7644e619b17SBjoern A. Zeeb 				if (m == NULL) {
7654e619b17SBjoern A. Zeeb 					TCPSTAT_INC(tcps_rcvshort);
7664e619b17SBjoern A. Zeeb 					return (IPPROTO_DONE);
7674e619b17SBjoern A. Zeeb 				}
768a4adf6ccSBjoern A. Zeeb 			}
769fb59c426SYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
770fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
771b287c6c7SBjoern A. Zeeb 		}
77204d3a452SHajimu UMEMOTO #endif
773b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
774b287c6c7SBjoern A. Zeeb 		else
775b287c6c7SBjoern A. Zeeb #endif
776b287c6c7SBjoern A. Zeeb #ifdef INET
777b287c6c7SBjoern A. Zeeb 		{
778df8bae1dSRodney W. Grimes 			if (m->m_len < sizeof(struct ip) + off) {
779c068736aSJeffrey Hsu 				if ((m = m_pullup(m, sizeof (struct ip) + off))
7804dfdffe9SAndre Oppermann 				    == NULL) {
78178b50714SRobert Watson 					TCPSTAT_INC(tcps_rcvshort);
7828f5a8818SKevin Lo 					return (IPPROTO_DONE);
783df8bae1dSRodney W. Grimes 				}
784fb59c426SYoshinobu Inoue 				ip = mtod(m, struct ip *);
785fb59c426SYoshinobu Inoue 				th = (struct tcphdr *)((caddr_t)ip + off0);
786fb59c426SYoshinobu Inoue 			}
787df8bae1dSRodney W. Grimes 		}
788b287c6c7SBjoern A. Zeeb #endif
789df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
790fb59c426SYoshinobu Inoue 		optp = (u_char *)(th + 1);
791df8bae1dSRodney W. Grimes 	}
792fb59c426SYoshinobu Inoue 	thflags = th->th_flags;
793df8bae1dSRodney W. Grimes 
794e46cd3d4SDag-Erling Smørgrav 	/*
795df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
796df8bae1dSRodney W. Grimes 	 */
7972903309aSAttilio Rao 	tcp_fields_to_host(th);
798df8bae1dSRodney W. Grimes 
799df8bae1dSRodney W. Grimes 	/*
800794235b7SAndre Oppermann 	 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options.
801755c1f07SAndras Olah 	 */
802fb59c426SYoshinobu Inoue 	drop_hdrlen = off0 + off;
803755c1f07SAndras Olah 
804755c1f07SAndras Olah 	/*
8058d573cc1SAndre Oppermann 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
8068d573cc1SAndre Oppermann 	 */
807b8056faeSGleb Smirnoff         if (
808b8056faeSGleb Smirnoff #ifdef INET6
809b8056faeSGleb Smirnoff 	    (isipv6 && (m->m_flags & M_IP6_NEXTHOP))
810b8056faeSGleb Smirnoff #ifdef INET
811b8056faeSGleb Smirnoff 	    || (!isipv6 && (m->m_flags & M_IP_NEXTHOP))
812b8056faeSGleb Smirnoff #endif
813b8056faeSGleb Smirnoff #endif
814b8056faeSGleb Smirnoff #if defined(INET) && !defined(INET6)
815b8056faeSGleb Smirnoff 	    (m->m_flags & M_IP_NEXTHOP)
816b8056faeSGleb Smirnoff #endif
817b8056faeSGleb Smirnoff 	    )
8189b932e9eSAndre Oppermann 		fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
8199b932e9eSAndre Oppermann 
820ce7ad664SEd Maste findpcb:
8218a006adbSBjoern A. Zeeb #ifdef INET6
8228a006adbSBjoern A. Zeeb 	if (isipv6 && fwd_tag != NULL) {
8238a006adbSBjoern A. Zeeb 		struct sockaddr_in6 *next_hop6;
8248a006adbSBjoern A. Zeeb 
8258a006adbSBjoern A. Zeeb 		next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
8268a006adbSBjoern A. Zeeb 		/*
8278a006adbSBjoern A. Zeeb 		 * Transparently forwarded. Pretend to be the destination.
8288a006adbSBjoern A. Zeeb 		 * Already got one like this?
8298a006adbSBjoern A. Zeeb 		 */
8308a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo,
8318a006adbSBjoern A. Zeeb 		    &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport,
8328a006adbSBjoern A. Zeeb 		    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif, m);
8338a006adbSBjoern A. Zeeb 		if (!inp) {
8348a006adbSBjoern A. Zeeb 			/*
8358a006adbSBjoern A. Zeeb 			 * It's new.  Try to find the ambushing socket.
8368a006adbSBjoern A. Zeeb 			 * Because we've rewritten the destination address,
8378a006adbSBjoern A. Zeeb 			 * any hardware-generated hash is ignored.
8388a006adbSBjoern A. Zeeb 			 */
8398a006adbSBjoern A. Zeeb 			inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src,
8408a006adbSBjoern A. Zeeb 			    th->th_sport, &next_hop6->sin6_addr,
8418a006adbSBjoern A. Zeeb 			    next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) :
8428a006adbSBjoern A. Zeeb 			    th->th_dport, INPLOOKUP_WILDCARD |
8438a006adbSBjoern A. Zeeb 			    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif);
8448a006adbSBjoern A. Zeeb 		}
845c1de64a4SAndrey V. Elsukov 	} else if (isipv6) {
8468a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src,
8478a006adbSBjoern A. Zeeb 		    th->th_sport, &ip6->ip6_dst, th->th_dport,
8488a006adbSBjoern A. Zeeb 		    INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB,
8498a006adbSBjoern A. Zeeb 		    m->m_pkthdr.rcvif, m);
8508a006adbSBjoern A. Zeeb 	}
8518a006adbSBjoern A. Zeeb #endif /* INET6 */
8528a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET)
8538a006adbSBjoern A. Zeeb 	else
8548a006adbSBjoern A. Zeeb #endif
8558a006adbSBjoern A. Zeeb #ifdef INET
8568a006adbSBjoern A. Zeeb 	if (fwd_tag != NULL) {
8579b932e9eSAndre Oppermann 		struct sockaddr_in *next_hop;
8589b932e9eSAndre Oppermann 
8599b932e9eSAndre Oppermann 		next_hop = (struct sockaddr_in *)(fwd_tag+1);
860f9e354dfSJulian Elischer 		/*
8612b25acc1SLuigi Rizzo 		 * Transparently forwarded. Pretend to be the destination.
862f9e354dfSJulian Elischer 		 * already got one like this?
863f9e354dfSJulian Elischer 		 */
864d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport,
865fa046d87SRobert Watson 		    ip->ip_dst, th->th_dport, INPLOOKUP_WLOCKPCB,
866d3c1f003SRobert Watson 		    m->m_pkthdr.rcvif, m);
867fa046d87SRobert Watson 		if (!inp) {
868fa046d87SRobert Watson 			/*
869fa046d87SRobert Watson 			 * It's new.  Try to find the ambushing socket.
870d3c1f003SRobert Watson 			 * Because we've rewritten the destination address,
871d3c1f003SRobert Watson 			 * any hardware-generated hash is ignored.
872fa046d87SRobert Watson 			 */
873fa046d87SRobert Watson 			inp = in_pcblookup(&V_tcbinfo, ip->ip_src,
874fa046d87SRobert Watson 			    th->th_sport, next_hop->sin_addr,
875fa046d87SRobert Watson 			    next_hop->sin_port ? ntohs(next_hop->sin_port) :
876fa046d87SRobert Watson 			    th->th_dport, INPLOOKUP_WILDCARD |
877fa046d87SRobert Watson 			    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif);
878f9e354dfSJulian Elischer 		}
879b10fbdeaSAndre Oppermann 	} else
880d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src,
881fa046d87SRobert Watson 		    th->th_sport, ip->ip_dst, th->th_dport,
882fa046d87SRobert Watson 		    INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB,
883d3c1f003SRobert Watson 		    m->m_pkthdr.rcvif, m);
8848a006adbSBjoern A. Zeeb #endif /* INET */
885f9e354dfSJulian Elischer 
886df8bae1dSRodney W. Grimes 	/*
887574b6964SAndre Oppermann 	 * If the INPCB does not exist then all data in the incoming
888574b6964SAndre Oppermann 	 * segment is discarded and an appropriate RST is sent back.
8898b07e49aSJulian Elischer 	 * XXX MRT Send RST using which routing table?
890df8bae1dSRodney W. Grimes 	 */
891816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
892574b6964SAndre Oppermann 		/*
893574b6964SAndre Oppermann 		 * Log communication attempts to ports that are not
894574b6964SAndre Oppermann 		 * in use.
895574b6964SAndre Oppermann 		 */
896334fc582SBjoern A. Zeeb 		if ((V_tcp_log_in_vain == 1 && (thflags & TH_SYN)) ||
897334fc582SBjoern A. Zeeb 		    V_tcp_log_in_vain == 2) {
898b7d747ecSAndre Oppermann 			if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6)))
899df541e5fSAndre Oppermann 				log(LOG_INFO, "%s; %s: Connection attempt "
900df541e5fSAndre Oppermann 				    "to closed port\n", s, __func__);
9012e4e1b4cSGeoff Rehmet 		}
902574b6964SAndre Oppermann 		/*
903574b6964SAndre Oppermann 		 * When blackholing do not respond with a RST but
904574b6964SAndre Oppermann 		 * completely ignore the segment and drop it.
905574b6964SAndre Oppermann 		 */
906603724d3SBjoern A. Zeeb 		if ((V_blackhole == 1 && (thflags & TH_SYN)) ||
907603724d3SBjoern A. Zeeb 		    V_blackhole == 2)
9081929eae1SAndre Oppermann 			goto dropunlock;
909574b6964SAndre Oppermann 
910a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
911a57815efSBosko Milekic 		goto dropwithreset;
912816a3d83SPoul-Henning Kamp 	}
913fa046d87SRobert Watson 	INP_WLOCK_ASSERT(inp);
914f5cf1e5fSJulien Charbon 	/*
915f5cf1e5fSJulien Charbon 	 * While waiting for inp lock during the lookup, another thread
916f5cf1e5fSJulien Charbon 	 * can have dropped the inpcb, in which case we need to loop back
917f5cf1e5fSJulien Charbon 	 * and try to find a new inpcb to deliver to.
918f5cf1e5fSJulien Charbon 	 */
919f5cf1e5fSJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
920f5cf1e5fSJulien Charbon 		INP_WUNLOCK(inp);
921f5cf1e5fSJulien Charbon 		inp = NULL;
922f5cf1e5fSJulien Charbon 		goto findpcb;
923f5cf1e5fSJulien Charbon 	}
924c2529042SHans Petter Selasky 	if ((inp->inp_flowtype == M_HASHTYPE_NONE) &&
925c2529042SHans Petter Selasky 	    (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) &&
926c2529042SHans Petter Selasky 	    ((inp->inp_socket == NULL) ||
927c2529042SHans Petter Selasky 	    (inp->inp_socket->so_options & SO_ACCEPTCONN) == 0)) {
92880cb9f21SKip Macy 		inp->inp_flowid = m->m_pkthdr.flowid;
9292f719932SAdrian Chadd 		inp->inp_flowtype = M_HASHTYPE_GET(m);
93080cb9f21SKip Macy 	}
931fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
932a2589465SKen Smith #ifdef INET6
933fcf59617SAndrey V. Elsukov 	if (isipv6 && IPSEC_ENABLED(ipv6) &&
934fcf59617SAndrey V. Elsukov 	    IPSEC_CHECK_POLICY(ipv6, m, inp) != 0) {
935a2589465SKen Smith 		goto dropunlock;
936a2589465SKen Smith 	}
937fcf59617SAndrey V. Elsukov #ifdef INET
938fcf59617SAndrey V. Elsukov 	else
939fcf59617SAndrey V. Elsukov #endif
940fcf59617SAndrey V. Elsukov #endif /* INET6 */
941fcf59617SAndrey V. Elsukov #ifdef INET
942fcf59617SAndrey V. Elsukov 	if (IPSEC_ENABLED(ipv4) &&
943fcf59617SAndrey V. Elsukov 	    IPSEC_CHECK_POLICY(ipv4, m, inp) != 0) {
944fcf59617SAndrey V. Elsukov 		goto dropunlock;
945fcf59617SAndrey V. Elsukov 	}
946fcf59617SAndrey V. Elsukov #endif /* INET */
947a2589465SKen Smith #endif /* IPSEC */
948a2589465SKen Smith 
9498d573cc1SAndre Oppermann 	/*
9508d573cc1SAndre Oppermann 	 * Check the minimum TTL for socket.
9518d573cc1SAndre Oppermann 	 */
95234f83c52SGeorge V. Neville-Neil 	if (inp->inp_ip_minttl != 0) {
95334f83c52SGeorge V. Neville-Neil #ifdef INET6
9542d8868dbSJonathan T. Looney 		if (isipv6) {
9552d8868dbSJonathan T. Looney 			if (inp->inp_ip_minttl > ip6->ip6_hlim)
956302ce8d6SAndre Oppermann 				goto dropunlock;
9572d8868dbSJonathan T. Looney 		} else
95834f83c52SGeorge V. Neville-Neil #endif
95934f83c52SGeorge V. Neville-Neil 		if (inp->inp_ip_minttl > ip->ip_ttl)
960302ce8d6SAndre Oppermann 			goto dropunlock;
96134f83c52SGeorge V. Neville-Neil 	}
962936cd18dSAndre Oppermann 
963340c35deSJonathan Lemon 	/*
964252ca428SRobert Watson 	 * A previous connection in TIMEWAIT state is supposed to catch stray
965252ca428SRobert Watson 	 * or duplicate segments arriving late.  If this segment was a
9660989f56cSRobert Watson 	 * legitimate new connection attempt, the old INPCB gets removed and
967252ca428SRobert Watson 	 * we can try again to find a listening socket.
968252ca428SRobert Watson 	 *
969fa046d87SRobert Watson 	 * At this point, due to earlier optimism, we may hold only an inpcb
970fa046d87SRobert Watson 	 * lock, and not the inpcbinfo write lock.  If so, we need to try to
971fa046d87SRobert Watson 	 * acquire it, or if that fails, acquire a reference on the inpcb,
972fa046d87SRobert Watson 	 * drop all locks, acquire a global write lock, and then re-acquire
973fa046d87SRobert Watson 	 * the inpcb lock.  We may at that point discover that another thread
974fa046d87SRobert Watson 	 * has tried to free the inpcb, in which case we need to loop back
975fa046d87SRobert Watson 	 * and try to find a new inpcb to deliver to.
976fa046d87SRobert Watson 	 *
977fa046d87SRobert Watson 	 * XXXRW: It may be time to rethink timewait locking.
978340c35deSJonathan Lemon 	 */
979ad71fe3cSRobert Watson 	if (inp->inp_flags & INP_TIMEWAIT) {
980*283c76c7SMichael Tuexen 		tcp_dooptions(&to, optp, optlen,
981*283c76c7SMichael Tuexen 		    (thflags & TH_SYN) ? TO_SYN : 0);
9828d573cc1SAndre Oppermann 		/*
9838d573cc1SAndre Oppermann 		 * NB: tcp_twcheck unlocks the INP and frees the mbuf.
9848d573cc1SAndre Oppermann 		 */
9852104448fSAndre Oppermann 		if (tcp_twcheck(inp, &to, th, m, tlen))
986340c35deSJonathan Lemon 			goto findpcb;
9878f5a8818SKevin Lo 		return (IPPROTO_DONE);
988340c35deSJonathan Lemon 	}
989794235b7SAndre Oppermann 	/*
990794235b7SAndre Oppermann 	 * The TCPCB may no longer exist if the connection is winding
991794235b7SAndre Oppermann 	 * down or it is in the CLOSED state.  Either way we drop the
992794235b7SAndre Oppermann 	 * segment and send an appropriate response.
993794235b7SAndre Oppermann 	 */
994df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
995a160e630SAndre Oppermann 	if (tp == NULL || tp->t_state == TCPS_CLOSED) {
996a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
997a57815efSBosko Milekic 		goto dropwithreset;
99809f81a46SBosko Milekic 	}
999df8bae1dSRodney W. Grimes 
100009fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
100109fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
100209fe6320SNavdeep Parhar 		tcp_offload_input(tp, m);
100309fe6320SNavdeep Parhar 		m = NULL;	/* consumed by the TOE driver */
100409fe6320SNavdeep Parhar 		goto dropunlock;
100509fe6320SNavdeep Parhar 	}
100609fe6320SNavdeep Parhar #endif
100709fe6320SNavdeep Parhar 
1008c488362eSRobert Watson #ifdef MAC
10098501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
101030d239bcSRobert Watson 	if (mac_inpcb_check_deliver(inp, m))
1011302ce8d6SAndre Oppermann 		goto dropunlock;
1012c488362eSRobert Watson #endif
1013a557af22SRobert Watson 	so = inp->inp_socket;
1014302ce8d6SAndre Oppermann 	KASSERT(so != NULL, ("%s: so == NULL", __func__));
1015610ee2f9SDavid Greenman #ifdef TCPDEBUG
1016df8bae1dSRodney W. Grimes 	if (so->so_options & SO_DEBUG) {
1017df8bae1dSRodney W. Grimes 		ostate = tp->t_state;
101810fe523eSMaxim Konovalov #ifdef INET6
10196f697424SBjoern A. Zeeb 		if (isipv6) {
1020540e8b7eSJeffrey Hsu 			bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6));
1021d30d90dcSMaxim Konovalov 		} else
10226f697424SBjoern A. Zeeb #endif
1023540e8b7eSJeffrey Hsu 			bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
1024fb59c426SYoshinobu Inoue 		tcp_savetcp = *th;
1025df8bae1dSRodney W. Grimes 	}
1026b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */
1027db33b3e6SAndre Oppermann 	/*
1028db33b3e6SAndre Oppermann 	 * When the socket is accepting connections (the INPCB is in LISTEN
1029db33b3e6SAndre Oppermann 	 * state) we look into the SYN cache if this is a new connection
1030a7c7f2a7SJohn Baldwin 	 * attempt or the completion of a previous one.
1031db33b3e6SAndre Oppermann 	 */
103245274760SJonathan T. Looney 	KASSERT(tp->t_state == TCPS_LISTEN || !(so->so_options & SO_ACCEPTCONN),
103345274760SJonathan T. Looney 	    ("%s: so accepting but tp %p not listening", __func__, tp));
103445274760SJonathan T. Looney 	if (tp->t_state == TCPS_LISTEN && (so->so_options & SO_ACCEPTCONN)) {
1035540e8b7eSJeffrey Hsu 		struct in_conninfo inc;
1036540e8b7eSJeffrey Hsu 
10378bfb1918SAndre Oppermann 		bzero(&inc, sizeof(inc));
1038302ce8d6SAndre Oppermann #ifdef INET6
1039be2ac88cSJonathan Lemon 		if (isipv6) {
1040dcdb4371SBjoern A. Zeeb 			inc.inc_flags |= INC_ISIPV6;
10415dff1c38SMichael Tuexen 			if (inp->inp_inc.inc_flags & INC_IPV6MINMTU)
10425dff1c38SMichael Tuexen 				inc.inc_flags |= INC_IPV6MINMTU;
1043be2ac88cSJonathan Lemon 			inc.inc6_faddr = ip6->ip6_src;
1044be2ac88cSJonathan Lemon 			inc.inc6_laddr = ip6->ip6_dst;
1045302ce8d6SAndre Oppermann 		} else
1046302ce8d6SAndre Oppermann #endif
1047302ce8d6SAndre Oppermann 		{
1048be2ac88cSJonathan Lemon 			inc.inc_faddr = ip->ip_src;
1049be2ac88cSJonathan Lemon 			inc.inc_laddr = ip->ip_dst;
1050be2ac88cSJonathan Lemon 		}
1051be2ac88cSJonathan Lemon 		inc.inc_fport = th->th_sport;
1052be2ac88cSJonathan Lemon 		inc.inc_lport = th->th_dport;
10537973fba3SJulian Elischer 		inc.inc_fibnum = so->so_fibnum;
1054be2ac88cSJonathan Lemon 
1055fb59c426SYoshinobu Inoue 		/*
10568d573cc1SAndre Oppermann 		 * Check for an existing connection attempt in syncache if
10578d573cc1SAndre Oppermann 		 * the flag is only ACK.  A successful lookup creates a new
10588d573cc1SAndre Oppermann 		 * socket appended to the listen queue in SYN_RECEIVED state.
1059fb59c426SYoshinobu Inoue 		 */
1060be2ac88cSJonathan Lemon 		if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
1061bf6d304aSAndre Oppermann 			/*
1062bf6d304aSAndre Oppermann 			 * Parse the TCP options here because
1063bf6d304aSAndre Oppermann 			 * syncookies need access to the reflected
1064bf6d304aSAndre Oppermann 			 * timestamp.
1065bf6d304aSAndre Oppermann 			 */
1066bf6d304aSAndre Oppermann 			tcp_dooptions(&to, optp, optlen, 0);
10679fa198beSAndre Oppermann 			/*
10689fa198beSAndre Oppermann 			 * NB: syncache_expand() doesn't unlock
10699fa198beSAndre Oppermann 			 * inp and tcpinfo locks.
10709fa198beSAndre Oppermann 			 */
1071fcf59617SAndrey V. Elsukov 			rstreason = syncache_expand(&inc, &to, th, &so, m);
1072fcf59617SAndrey V. Elsukov 			if (rstreason < 0) {
1073fcf59617SAndrey V. Elsukov 				/*
1074fcf59617SAndrey V. Elsukov 				 * A failing TCP MD5 signature comparison
1075fcf59617SAndrey V. Elsukov 				 * must result in the segment being dropped
1076fcf59617SAndrey V. Elsukov 				 * and must not produce any response back
1077fcf59617SAndrey V. Elsukov 				 * to the sender.
1078fcf59617SAndrey V. Elsukov 				 */
1079fcf59617SAndrey V. Elsukov 				goto dropunlock;
1080fcf59617SAndrey V. Elsukov 			} else if (rstreason == 0) {
10814195b4afSPaul Traina 				/*
1082e207f800SAndre Oppermann 				 * No syncache entry or ACK was not
1083be2ac88cSJonathan Lemon 				 * for our SYN/ACK.  Send a RST.
10848d573cc1SAndre Oppermann 				 * NB: syncache did its own logging
10858d573cc1SAndre Oppermann 				 * of the failure cause.
10864195b4afSPaul Traina 				 */
1087be2ac88cSJonathan Lemon 				rstreason = BANDLIM_RST_OPENPORT;
1088be2ac88cSJonathan Lemon 				goto dropwithreset;
1089be2ac88cSJonathan Lemon 			}
109009c305ebSPatrick Kelsey tfo_socket_result:
1091f76fcf6dSJeffrey Hsu 			if (so == NULL) {
1092be2ac88cSJonathan Lemon 				/*
1093e207f800SAndre Oppermann 				 * We completed the 3-way handshake
1094e207f800SAndre Oppermann 				 * but could not allocate a socket
1095e207f800SAndre Oppermann 				 * either due to memory shortage,
1096e207f800SAndre Oppermann 				 * listen queue length limits or
1097a160e630SAndre Oppermann 				 * global socket limits.  Send RST
1098a160e630SAndre Oppermann 				 * or wait and have the remote end
1099a160e630SAndre Oppermann 				 * retransmit the ACK for another
1100a160e630SAndre Oppermann 				 * try.
1101be2ac88cSJonathan Lemon 				 */
1102a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1103a160e630SAndre Oppermann 					log(LOG_DEBUG, "%s; %s: Listen socket: "
11048d573cc1SAndre Oppermann 					    "Socket allocation failed due to "
11058d573cc1SAndre Oppermann 					    "limits or memory shortage, %s\n",
1106ac957cd2SJulian Elischer 					    s, __func__,
1107ac957cd2SJulian Elischer 					    V_tcp_sc_rst_sock_fail ?
1108ac957cd2SJulian Elischer 					    "sending RST" : "try again");
1109603724d3SBjoern A. Zeeb 				if (V_tcp_sc_rst_sock_fail) {
1110e207f800SAndre Oppermann 					rstreason = BANDLIM_UNLIMITED;
1111e207f800SAndre Oppermann 					goto dropwithreset;
1112a160e630SAndre Oppermann 				} else
1113a160e630SAndre Oppermann 					goto dropunlock;
1114f76fcf6dSJeffrey Hsu 			}
1115be2ac88cSJonathan Lemon 			/*
1116be2ac88cSJonathan Lemon 			 * Socket is created in state SYN_RECEIVED.
11178d573cc1SAndre Oppermann 			 * Unlock the listen socket, lock the newly
11188d573cc1SAndre Oppermann 			 * created socket and update the tp variable.
1119be2ac88cSJonathan Lemon 			 */
11208501a69cSRobert Watson 			INP_WUNLOCK(inp);	/* listen socket */
1121be2ac88cSJonathan Lemon 			inp = sotoinpcb(so);
1122ff9b006dSJulien Charbon 			/*
1123ff9b006dSJulien Charbon 			 * New connection inpcb is already locked by
1124ff9b006dSJulien Charbon 			 * syncache_expand().
1125ff9b006dSJulien Charbon 			 */
1126ff9b006dSJulien Charbon 			INP_WLOCK_ASSERT(inp);
1127be2ac88cSJonathan Lemon 			tp = intotcpcb(inp);
11288d573cc1SAndre Oppermann 			KASSERT(tp->t_state == TCPS_SYN_RECEIVED,
11298d573cc1SAndre Oppermann 			    ("%s: ", __func__));
1130be2ac88cSJonathan Lemon 			/*
1131302ce8d6SAndre Oppermann 			 * Process the segment and the data it
1132302ce8d6SAndre Oppermann 			 * contains.  tcp_do_segment() consumes
1133302ce8d6SAndre Oppermann 			 * the mbuf chain and unlocks the inpcb.
1134302ce8d6SAndre Oppermann 			 */
11356138da62SMichael Tuexen 			TCP_PROBE5(receive, NULL, tp, m, tp, th);
113655bceb1eSRandall Stewart 			tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen,
11376573d758SMatt Macy 			    iptos);
11388f5a8818SKevin Lo 			return (IPPROTO_DONE);
1139be2ac88cSJonathan Lemon 		}
1140a160e630SAndre Oppermann 		/*
11418d573cc1SAndre Oppermann 		 * Segment flag validation for new connection attempts:
11428d573cc1SAndre Oppermann 		 *
1143a160e630SAndre Oppermann 		 * Our (SYN|ACK) response was rejected.
1144a160e630SAndre Oppermann 		 * Check with syncache and remove entry to prevent
1145a160e630SAndre Oppermann 		 * retransmits.
114619bc77c5SAndre Oppermann 		 *
114719bc77c5SAndre Oppermann 		 * NB: syncache_chkrst does its own logging of failure
114819bc77c5SAndre Oppermann 		 * causes.
1149a160e630SAndre Oppermann 		 */
1150a160e630SAndre Oppermann 		if (thflags & TH_RST) {
115193899d10SMichael Tuexen 			syncache_chkrst(&inc, th, m);
1152a160e630SAndre Oppermann 			goto dropunlock;
1153a160e630SAndre Oppermann 		}
1154a160e630SAndre Oppermann 		/*
1155a160e630SAndre Oppermann 		 * We can't do anything without SYN.
1156a160e630SAndre Oppermann 		 */
1157a160e630SAndre Oppermann 		if ((thflags & TH_SYN) == 0) {
1158a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1159a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1160773673c1SAndre Oppermann 				    "SYN is missing, segment ignored\n",
11618d573cc1SAndre Oppermann 				    s, __func__);
116278b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1163a160e630SAndre Oppermann 			goto dropunlock;
1164a160e630SAndre Oppermann 		}
1165a160e630SAndre Oppermann 		/*
1166a160e630SAndre Oppermann 		 * (SYN|ACK) is bogus on a listen socket.
1167a160e630SAndre Oppermann 		 */
1168fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1169a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1170a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
11718d573cc1SAndre Oppermann 				    "SYN|ACK invalid, segment rejected\n",
11728d573cc1SAndre Oppermann 				    s, __func__);
1173a160e630SAndre Oppermann 			syncache_badack(&inc);	/* XXX: Not needed! */
117478b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1175a57815efSBosko Milekic 			rstreason = BANDLIM_RST_OPENPORT;
1176a57815efSBosko Milekic 			goto dropwithreset;
11774195b4afSPaul Traina 		}
1178a160e630SAndre Oppermann 		/*
1179a160e630SAndre Oppermann 		 * If the drop_synfin option is enabled, drop all
1180a160e630SAndre Oppermann 		 * segments with both the SYN and FIN bits set.
1181a160e630SAndre Oppermann 		 * This prevents e.g. nmap from identifying the
1182a160e630SAndre Oppermann 		 * TCP/IP stack.
1183a160e630SAndre Oppermann 		 * XXX: Poor reasoning.  nmap has other methods
1184a160e630SAndre Oppermann 		 * and is constantly refining its stack detection
1185a160e630SAndre Oppermann 		 * strategies.
11868d573cc1SAndre Oppermann 		 * XXX: This is a violation of the TCP specification
1187a160e630SAndre Oppermann 		 * and was used by RFC1644.
1188a160e630SAndre Oppermann 		 */
1189603724d3SBjoern A. Zeeb 		if ((thflags & TH_FIN) && V_drop_synfin) {
1190a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1191a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1192773673c1SAndre Oppermann 				    "SYN|FIN segment ignored (based on "
11938d573cc1SAndre Oppermann 				    "sysctl setting)\n", s, __func__);
119478b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1195302ce8d6SAndre Oppermann 			goto dropunlock;
1196ebb0cbeaSPaul Traina 		}
1197be2ac88cSJonathan Lemon 		/*
1198be2ac88cSJonathan Lemon 		 * Segment's flags are (SYN) or (SYN|FIN).
1199a160e630SAndre Oppermann 		 *
1200a160e630SAndre Oppermann 		 * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored
1201a160e630SAndre Oppermann 		 * as they do not affect the state of the TCP FSM.
1202a160e630SAndre Oppermann 		 * The data pointed to by TH_URG and th_urp is ignored.
1203be2ac88cSJonathan Lemon 		 */
1204a160e630SAndre Oppermann 		KASSERT((thflags & (TH_RST|TH_ACK)) == 0,
1205a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_RST or TH_ACK set", __func__));
1206a160e630SAndre Oppermann 		KASSERT(thflags & (TH_SYN),
1207a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_SYN not set", __func__));
120833841545SHajimu UMEMOTO #ifdef INET6
120933841545SHajimu UMEMOTO 		/*
121033841545SHajimu UMEMOTO 		 * If deprecated address is forbidden,
121133841545SHajimu UMEMOTO 		 * we do not accept SYN to deprecated interface
121233841545SHajimu UMEMOTO 		 * address to prevent any new inbound connection from
121333841545SHajimu UMEMOTO 		 * getting established.
121433841545SHajimu UMEMOTO 		 * When we do not accept SYN, we send a TCP RST,
121533841545SHajimu UMEMOTO 		 * with deprecated source address (instead of dropping
121633841545SHajimu UMEMOTO 		 * it).  We compromise it as it is much better for peer
121733841545SHajimu UMEMOTO 		 * to send a RST, and RST will be the final packet
121833841545SHajimu UMEMOTO 		 * for the exchange.
121933841545SHajimu UMEMOTO 		 *
122033841545SHajimu UMEMOTO 		 * If we do not forbid deprecated addresses, we accept
122133841545SHajimu UMEMOTO 		 * the SYN packet.  RFC2462 does not suggest dropping
122233841545SHajimu UMEMOTO 		 * SYN in this case.
122333841545SHajimu UMEMOTO 		 * If we decipher RFC2462 5.5.4, it says like this:
122433841545SHajimu UMEMOTO 		 * 1. use of deprecated addr with existing
122533841545SHajimu UMEMOTO 		 *    communication is okay - "SHOULD continue to be
122633841545SHajimu UMEMOTO 		 *    used"
122733841545SHajimu UMEMOTO 		 * 2. use of it with new communication:
122833841545SHajimu UMEMOTO 		 *   (2a) "SHOULD NOT be used if alternate address
122933841545SHajimu UMEMOTO 		 *        with sufficient scope is available"
123033841545SHajimu UMEMOTO 		 *   (2b) nothing mentioned otherwise.
123133841545SHajimu UMEMOTO 		 * Here we fall into (2b) case as we have no choice in
123233841545SHajimu UMEMOTO 		 * our source address selection - we must obey the peer.
123333841545SHajimu UMEMOTO 		 *
123433841545SHajimu UMEMOTO 		 * The wording in RFC2462 is confusing, and there are
123533841545SHajimu UMEMOTO 		 * multiple description text for deprecated address
123633841545SHajimu UMEMOTO 		 * handling - worse, they are not exactly the same.
123733841545SHajimu UMEMOTO 		 * I believe 5.5.4 is the best one, so we follow 5.5.4.
123833841545SHajimu UMEMOTO 		 */
1239603724d3SBjoern A. Zeeb 		if (isipv6 && !V_ip6_use_deprecated) {
124033841545SHajimu UMEMOTO 			struct in6_ifaddr *ia6;
124133841545SHajimu UMEMOTO 
12423e88eb90SAndrey V. Elsukov 			ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
12438c0fec80SRobert Watson 			if (ia6 != NULL &&
124433841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
12458c0fec80SRobert Watson 				ifa_free(&ia6->ia_ifa);
1246a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1247a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12488d573cc1SAndre Oppermann 					"Connection attempt to deprecated "
12498d573cc1SAndre Oppermann 					"IPv6 address rejected\n",
1250a160e630SAndre Oppermann 					s, __func__);
125133841545SHajimu UMEMOTO 				rstreason = BANDLIM_RST_OPENPORT;
125233841545SHajimu UMEMOTO 				goto dropwithreset;
125333841545SHajimu UMEMOTO 			}
125477d396fdSMaksim Yevmenkin 			if (ia6)
12558c0fec80SRobert Watson 				ifa_free(&ia6->ia_ifa);
125633841545SHajimu UMEMOTO 		}
1257b287c6c7SBjoern A. Zeeb #endif /* INET6 */
125865f28919SJesper Skriver 		/*
1259db33b3e6SAndre Oppermann 		 * Basic sanity checks on incoming SYN requests:
12608d573cc1SAndre Oppermann 		 *   Don't respond if the destination is a link layer
1261db33b3e6SAndre Oppermann 		 *	broadcast according to RFC1122 4.2.3.10, p. 104.
12628d573cc1SAndre Oppermann 		 *   If it is from this socket it must be forged.
12638d573cc1SAndre Oppermann 		 *   Don't respond if the source or destination is a
12648d573cc1SAndre Oppermann 		 *	global or subnet broad- or multicast address.
126593ec91baSCrist J. Clark 		 *   Note that it is quite possible to receive unicast
126693ec91baSCrist J. Clark 		 *	link-layer packets with a broadcast IP address. Use
126793ec91baSCrist J. Clark 		 *	in_broadcast() to find them.
1268be2ac88cSJonathan Lemon 		 */
12698d573cc1SAndre Oppermann 		if (m->m_flags & (M_BCAST|M_MCAST)) {
12708d573cc1SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
12718d573cc1SAndre Oppermann 			    log(LOG_DEBUG, "%s; %s: Listen socket: "
12728d573cc1SAndre Oppermann 				"Connection attempt from broad- or multicast "
1273773673c1SAndre Oppermann 				"link layer address ignored\n", s, __func__);
1274302ce8d6SAndre Oppermann 			goto dropunlock;
12758d573cc1SAndre Oppermann 		}
1276db33b3e6SAndre Oppermann #ifdef INET6
1277b287c6c7SBjoern A. Zeeb 		if (isipv6) {
1278db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1279a160e630SAndre Oppermann 			    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) {
1280a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1281a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12828d573cc1SAndre Oppermann 					"Connection attempt to/from self "
1283773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1284302ce8d6SAndre Oppermann 				goto dropunlock;
1285a160e630SAndre Oppermann 			}
1286be2ac88cSJonathan Lemon 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1287a160e630SAndre Oppermann 			    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
1288a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1289a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12908d573cc1SAndre Oppermann 					"Connection attempt from/to multicast "
1291773673c1SAndre Oppermann 					"address ignored\n", s, __func__);
1292302ce8d6SAndre Oppermann 				goto dropunlock;
1293a160e630SAndre Oppermann 			}
1294b287c6c7SBjoern A. Zeeb 		}
1295db33b3e6SAndre Oppermann #endif
1296b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
1297b287c6c7SBjoern A. Zeeb 		else
1298b287c6c7SBjoern A. Zeeb #endif
1299b287c6c7SBjoern A. Zeeb #ifdef INET
1300b287c6c7SBjoern A. Zeeb 		{
1301db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1302a160e630SAndre Oppermann 			    ip->ip_dst.s_addr == ip->ip_src.s_addr) {
1303a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1304a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13058d573cc1SAndre Oppermann 					"Connection attempt from/to self "
1306773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1307302ce8d6SAndre Oppermann 				goto dropunlock;
1308a160e630SAndre Oppermann 			}
1309be2ac88cSJonathan Lemon 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
1310be2ac88cSJonathan Lemon 			    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
13112ca2159fSCrist J. Clark 			    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
1312a160e630SAndre Oppermann 			    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
1313a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1314a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13158d573cc1SAndre Oppermann 					"Connection attempt from/to broad- "
1316773673c1SAndre Oppermann 					"or multicast address ignored\n",
1317a160e630SAndre Oppermann 					s, __func__);
1318302ce8d6SAndre Oppermann 				goto dropunlock;
1319c068736aSJeffrey Hsu 			}
1320a160e630SAndre Oppermann 		}
1321b287c6c7SBjoern A. Zeeb #endif
1322be2ac88cSJonathan Lemon 		/*
1323db33b3e6SAndre Oppermann 		 * SYN appears to be valid.  Create compressed TCP state
1324db33b3e6SAndre Oppermann 		 * for syncache.
1325be2ac88cSJonathan Lemon 		 */
13263c653157SHartmut Brandt #ifdef TCPDEBUG
13273c653157SHartmut Brandt 		if (so->so_options & SO_DEBUG)
13283c653157SHartmut Brandt 			tcp_trace(TA_INPUT, ostate, tp,
13293c653157SHartmut Brandt 			    (void *)tcp_saveipgen, &tcp_savetcp, 0);
13303c653157SHartmut Brandt #endif
13312b9c9984SGeorge V. Neville-Neil 		TCP_PROBE3(debug__input, tp, th, m);
1332f72167f4SAndre Oppermann 		tcp_dooptions(&to, optp, optlen, TO_SYN);
1333fa49a964SMichael Tuexen 		if (syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL, iptos))
133409c305ebSPatrick Kelsey 			goto tfo_socket_result;
133518a75309SPatrick Kelsey 
1336be2ac88cSJonathan Lemon 		/*
13374d6e7130SAndre Oppermann 		 * Entry added to syncache and mbuf consumed.
1338a7c7f2a7SJohn Baldwin 		 * Only the listen socket is unlocked by syncache_add().
1339be2ac88cSJonathan Lemon 		 */
1340384a5c3cSAndrey V. Elsukov 		INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo);
13418f5a8818SKevin Lo 		return (IPPROTO_DONE);
1342982c1675SAndre Oppermann 	} else if (tp->t_state == TCPS_LISTEN) {
1343982c1675SAndre Oppermann 		/*
1344982c1675SAndre Oppermann 		 * When a listen socket is torn down the SO_ACCEPTCONN
1345982c1675SAndre Oppermann 		 * flag is removed first while connections are drained
1346982c1675SAndre Oppermann 		 * from the accept queue in a unlock/lock cycle of the
1347982c1675SAndre Oppermann 		 * ACCEPT_LOCK, opening a race condition allowing a SYN
1348982c1675SAndre Oppermann 		 * attempt go through unhandled.
1349982c1675SAndre Oppermann 		 */
1350982c1675SAndre Oppermann 		goto dropunlock;
1351f76fcf6dSJeffrey Hsu 	}
1352fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1353fcf59617SAndrey V. Elsukov 	if (tp->t_flags & TF_SIGNATURE) {
1354fcf59617SAndrey V. Elsukov 		tcp_dooptions(&to, optp, optlen, thflags);
1355fcf59617SAndrey V. Elsukov 		if ((to.to_flags & TOF_SIGNATURE) == 0) {
1356fcf59617SAndrey V. Elsukov 			TCPSTAT_INC(tcps_sig_err_nosigopt);
13572903309aSAttilio Rao 			goto dropunlock;
13582903309aSAttilio Rao 		}
1359fcf59617SAndrey V. Elsukov 		if (!TCPMD5_ENABLED() ||
1360fcf59617SAndrey V. Elsukov 		    TCPMD5_INPUT(m, th, to.to_signature) != 0)
1361fcf59617SAndrey V. Elsukov 			goto dropunlock;
13622903309aSAttilio Rao 	}
13632903309aSAttilio Rao #endif
13642b9c9984SGeorge V. Neville-Neil 	TCP_PROBE5(receive, NULL, tp, m, tp, th);
136557f60867SMark Johnston 
1366302ce8d6SAndre Oppermann 	/*
13678d573cc1SAndre Oppermann 	 * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later
13688d573cc1SAndre Oppermann 	 * state.  tcp_do_segment() always consumes the mbuf chain, unlocks
13698d573cc1SAndre Oppermann 	 * the inpcb, and unlocks pcbinfo.
1370302ce8d6SAndre Oppermann 	 */
13716573d758SMatt Macy 	tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos);
13728f5a8818SKevin Lo 	return (IPPROTO_DONE);
1373be2ac88cSJonathan Lemon 
1374302ce8d6SAndre Oppermann dropwithreset:
13752b9c9984SGeorge V. Neville-Neil 	TCP_PROBE5(receive, NULL, tp, m, tp, th);
137657f60867SMark Johnston 
1377014ea782SRobert Watson 	if (inp != NULL) {
1378302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
1379014ea782SRobert Watson 		INP_WUNLOCK(inp);
1380dd8ac7f9SRobert Watson 	} else
1381014ea782SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
1382302ce8d6SAndre Oppermann 	m = NULL;	/* mbuf chain got consumed. */
1383014ea782SRobert Watson 	goto drop;
1384014ea782SRobert Watson 
1385302ce8d6SAndre Oppermann dropunlock:
138657f60867SMark Johnston 	if (m != NULL)
13872b9c9984SGeorge V. Neville-Neil 		TCP_PROBE5(receive, NULL, tp, m, tp, th);
138857f60867SMark Johnston 
13899fa198beSAndre Oppermann 	if (inp != NULL)
13908501a69cSRobert Watson 		INP_WUNLOCK(inp);
1391014ea782SRobert Watson 
1392302ce8d6SAndre Oppermann drop:
1393384a5c3cSAndrey V. Elsukov 	INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo);
1394a160e630SAndre Oppermann 	if (s != NULL)
1395a160e630SAndre Oppermann 		free(s, M_TCPLOG);
1396302ce8d6SAndre Oppermann 	if (m != NULL)
1397302ce8d6SAndre Oppermann 		m_freem(m);
13988f5a8818SKevin Lo 	return (IPPROTO_DONE);
1399302ce8d6SAndre Oppermann }
1400302ce8d6SAndre Oppermann 
1401e44c1887SSteven Hartland /*
1402e44c1887SSteven Hartland  * Automatic sizing of receive socket buffer.  Often the send
1403e44c1887SSteven Hartland  * buffer size is not optimally adjusted to the actual network
1404e44c1887SSteven Hartland  * conditions at hand (delay bandwidth product).  Setting the
1405e44c1887SSteven Hartland  * buffer size too small limits throughput on links with high
1406e44c1887SSteven Hartland  * bandwidth and high delay (eg. trans-continental/oceanic links).
1407e44c1887SSteven Hartland  *
1408e44c1887SSteven Hartland  * On the receive side the socket buffer memory is only rarely
1409e44c1887SSteven Hartland  * used to any significant extent.  This allows us to be much
1410e44c1887SSteven Hartland  * more aggressive in scaling the receive socket buffer.  For
1411e44c1887SSteven Hartland  * the case that the buffer space is actually used to a large
1412e44c1887SSteven Hartland  * extent and we run out of kernel memory we can simply drop
1413e44c1887SSteven Hartland  * the new segments; TCP on the sender will just retransmit it
1414e44c1887SSteven Hartland  * later.  Setting the buffer size too big may only consume too
1415e44c1887SSteven Hartland  * much kernel memory if the application doesn't read() from
1416e44c1887SSteven Hartland  * the socket or packet loss or reordering makes use of the
1417e44c1887SSteven Hartland  * reassembly queue.
1418e44c1887SSteven Hartland  *
1419e44c1887SSteven Hartland  * The criteria to step up the receive buffer one notch are:
1420e44c1887SSteven Hartland  *  1. Application has not set receive buffer size with
1421e44c1887SSteven Hartland  *     SO_RCVBUF. Setting SO_RCVBUF clears SB_AUTOSIZE.
1422560c0586SMichael Tuexen  *  2. the number of bytes received during 1/2 of an sRTT
1423560c0586SMichael Tuexen  *     is at least 3/8 of the current socket buffer size.
1424560c0586SMichael Tuexen  *  3. receive buffer size has not hit maximal automatic size;
1425e44c1887SSteven Hartland  *
1426560c0586SMichael Tuexen  * If all of the criteria are met we increaset the socket buffer
1427560c0586SMichael Tuexen  * by a 1/2 (bounded by the max). This allows us to keep ahead
1428560c0586SMichael Tuexen  * of slow-start but also makes it so our peer never gets limited
1429560c0586SMichael Tuexen  * by our rwnd which we then open up causing a burst.
1430560c0586SMichael Tuexen  *
1431560c0586SMichael Tuexen  * This algorithm does two steps per RTT at most and only if
1432e44c1887SSteven Hartland  * we receive a bulk stream w/o packet losses or reorderings.
1433e44c1887SSteven Hartland  * Shrinking the buffer during idle times is not necessary as
1434e44c1887SSteven Hartland  * it doesn't consume any memory when idle.
1435e44c1887SSteven Hartland  *
1436e44c1887SSteven Hartland  * TODO: Only step up if the application is actually serving
1437e44c1887SSteven Hartland  * the buffer to better manage the socket buffer resources.
1438e44c1887SSteven Hartland  */
1439e44c1887SSteven Hartland int
1440e44c1887SSteven Hartland tcp_autorcvbuf(struct mbuf *m, struct tcphdr *th, struct socket *so,
1441e44c1887SSteven Hartland     struct tcpcb *tp, int tlen)
1442e44c1887SSteven Hartland {
1443e44c1887SSteven Hartland 	int newsize = 0;
1444e44c1887SSteven Hartland 
1445e44c1887SSteven Hartland 	if (V_tcp_do_autorcvbuf && (so->so_rcv.sb_flags & SB_AUTOSIZE) &&
1446e44c1887SSteven Hartland 	    tp->t_srtt != 0 && tp->rfbuf_ts != 0 &&
1447e44c1887SSteven Hartland 	    TCP_TS_TO_TICKS(tcp_ts_getticks() - tp->rfbuf_ts) >
1448560c0586SMichael Tuexen 	    ((tp->t_srtt >> TCP_RTT_SHIFT)/2)) {
1449560c0586SMichael Tuexen 		if (tp->rfbuf_cnt > ((so->so_rcv.sb_hiwat / 2)/ 4 * 3) &&
1450e44c1887SSteven Hartland 		    so->so_rcv.sb_hiwat < V_tcp_autorcvbuf_max) {
1451560c0586SMichael Tuexen 			newsize = min((so->so_rcv.sb_hiwat + (so->so_rcv.sb_hiwat/2)), V_tcp_autorcvbuf_max);
1452e44c1887SSteven Hartland 		}
1453e44c1887SSteven Hartland 		TCP_PROBE6(receive__autoresize, NULL, tp, m, tp, th, newsize);
1454e44c1887SSteven Hartland 
1455e44c1887SSteven Hartland 		/* Start over with next RTT. */
1456e44c1887SSteven Hartland 		tp->rfbuf_ts = 0;
1457e44c1887SSteven Hartland 		tp->rfbuf_cnt = 0;
1458e44c1887SSteven Hartland 	} else {
1459e44c1887SSteven Hartland 		tp->rfbuf_cnt += tlen;	/* add up */
1460e44c1887SSteven Hartland 	}
1461e44c1887SSteven Hartland 	return (newsize);
1462e44c1887SSteven Hartland }
1463e44c1887SSteven Hartland 
146455bceb1eSRandall Stewart void
14654d0770f1SRichard Scheffenegger tcp_handle_wakeup(struct tcpcb *tp, struct socket *so)
14664d0770f1SRichard Scheffenegger {
14674d0770f1SRichard Scheffenegger 	/*
14684d0770f1SRichard Scheffenegger 	 * Since tp might be gone if the session entered
14694d0770f1SRichard Scheffenegger 	 * the TIME_WAIT state before coming here, we need
14704d0770f1SRichard Scheffenegger 	 * to check if the socket is still connected.
14714d0770f1SRichard Scheffenegger 	 */
14724d0770f1SRichard Scheffenegger 	if ((so->so_state & SS_ISCONNECTED) == 0)
14734d0770f1SRichard Scheffenegger 		return;
14744d0770f1SRichard Scheffenegger 	INP_LOCK_ASSERT(tp->t_inpcb);
14754d0770f1SRichard Scheffenegger 	if (tp->t_flags & TF_WAKESOR) {
14764d0770f1SRichard Scheffenegger 		tp->t_flags &= ~TF_WAKESOR;
14774d0770f1SRichard Scheffenegger 		SOCKBUF_UNLOCK_ASSERT(&so->so_rcv);
14784d0770f1SRichard Scheffenegger 		sorwakeup(so);
14794d0770f1SRichard Scheffenegger 	}
14804d0770f1SRichard Scheffenegger 	if (tp->t_flags & TF_WAKESOW) {
14814d0770f1SRichard Scheffenegger 		tp->t_flags &= ~TF_WAKESOW;
14824d0770f1SRichard Scheffenegger 		SOCKBUF_UNLOCK_ASSERT(&so->so_snd);
14834d0770f1SRichard Scheffenegger 		sowwakeup(so);
14844d0770f1SRichard Scheffenegger 	}
14854d0770f1SRichard Scheffenegger }
14864d0770f1SRichard Scheffenegger 
14874d0770f1SRichard Scheffenegger void
1488302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so,
14896573d758SMatt Macy     struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos)
1490302ce8d6SAndre Oppermann {
1491021eaf79SHiren Panchasara 	int thflags, acked, ourfinisacked, needoutput = 0, sack_changed;
1492b2ade6b1SRichard Scheffenegger 	int rstreason, todrop, win, incforsyn = 0;
14933ac12506SJonathan T. Looney 	uint32_t tiwin;
14944b7b743cSLawrence Stewart 	uint16_t nsegs;
149507dacf03SAndre Oppermann 	char *s;
149607dacf03SAndre Oppermann 	struct in_conninfo *inc;
1497c11a15bfSGleb Smirnoff 	struct mbuf *mfree;
1498302ce8d6SAndre Oppermann 	struct tcpopt to;
1499281a0fd4SPatrick Kelsey 	int tfo_syn;
1500302ce8d6SAndre Oppermann 
150114739780SMaxim Konovalov #ifdef TCPDEBUG
150214739780SMaxim Konovalov 	/*
150314739780SMaxim Konovalov 	 * The size of tcp_saveipgen must be the size of the max ip header,
150414739780SMaxim Konovalov 	 * now IPv6.
150514739780SMaxim Konovalov 	 */
150614739780SMaxim Konovalov 	u_char tcp_saveipgen[IP6_HDR_LEN];
150714739780SMaxim Konovalov 	struct tcphdr tcp_savetcp;
150814739780SMaxim Konovalov 	short ostate = 0;
150914739780SMaxim Konovalov #endif
1510302ce8d6SAndre Oppermann 	thflags = th->th_flags;
151107dacf03SAndre Oppermann 	inc = &tp->t_inpcb->inp_inc;
1512d64a46eaSLawrence Stewart 	tp->sackhint.last_sack_ack = 0;
1513021eaf79SHiren Panchasara 	sack_changed = 0;
15144b7b743cSLawrence Stewart 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
1515d40c0d47SGleb Smirnoff 
1516d40c0d47SGleb Smirnoff 	NET_EPOCH_ASSERT();
15178501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1518679d9708SAndre Oppermann 	KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
1519679d9708SAndre Oppermann 	    __func__));
1520679d9708SAndre Oppermann 	KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
1521679d9708SAndre Oppermann 	    __func__));
1522df8bae1dSRodney W. Grimes 
152386a996e6SHiren Panchasara #ifdef TCPPCAP
152486a996e6SHiren Panchasara 	/* Save segment, if requested. */
152586a996e6SHiren Panchasara 	tcp_pcap_add(th, m, &(tp->t_inpkts));
152686a996e6SHiren Panchasara #endif
15272529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0,
15282529f56eSJonathan T. Looney 	    tlen, NULL, true);
152986a996e6SHiren Panchasara 
1530013f4df6SMichael Tuexen 	if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
1531013f4df6SMichael Tuexen 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
1532013f4df6SMichael Tuexen 			log(LOG_DEBUG, "%s; %s: "
1533013f4df6SMichael Tuexen 			    "SYN|FIN segment ignored (based on "
1534013f4df6SMichael Tuexen 			    "sysctl setting)\n", s, __func__);
1535013f4df6SMichael Tuexen 			free(s, M_TCPLOG);
1536013f4df6SMichael Tuexen 		}
1537013f4df6SMichael Tuexen 		goto drop;
1538013f4df6SMichael Tuexen 	}
1539013f4df6SMichael Tuexen 
1540df8bae1dSRodney W. Grimes 	/*
1541ebfd7534SMichael Tuexen 	 * If a segment with the ACK-bit set arrives in the SYN-SENT state
1542ebfd7534SMichael Tuexen 	 * check SEQ.ACK first.
1543ebfd7534SMichael Tuexen 	 */
1544ebfd7534SMichael Tuexen 	if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) &&
1545ebfd7534SMichael Tuexen 	    (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) {
1546ebfd7534SMichael Tuexen 		rstreason = BANDLIM_UNLIMITED;
1547ebfd7534SMichael Tuexen 		goto dropwithreset;
1548ebfd7534SMichael Tuexen 	}
1549ebfd7534SMichael Tuexen 
1550ebfd7534SMichael Tuexen 	/*
1551df8bae1dSRodney W. Grimes 	 * Segment received on connection.
1552df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
1553e8949f74SAndre Oppermann 	 * XXX: This should be done after segment
1554e8949f74SAndre Oppermann 	 * validation to ignore broken/spoofed segs.
1555df8bae1dSRodney W. Grimes 	 */
15569b8b58e0SJonathan Lemon 	tp->t_rcvtime = ticks;
1557df8bae1dSRodney W. Grimes 
1558df8bae1dSRodney W. Grimes 	/*
1559c52102ddSHiren Panchasara 	 * Scale up the window into a 32-bit value.
1560e8949f74SAndre Oppermann 	 * For the SYN_SENT state the scale is zero.
1561464fcfbcSAndre Oppermann 	 */
1562464fcfbcSAndre Oppermann 	tiwin = th->th_win << tp->snd_scale;
1563adc56f5aSEdward Tomasz Napierala #ifdef STATS
1564adc56f5aSEdward Tomasz Napierala 	stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin);
1565adc56f5aSEdward Tomasz Napierala #endif
1566464fcfbcSAndre Oppermann 
1567464fcfbcSAndre Oppermann 	/*
1568f2512ba1SRui Paulo 	 * TCP ECN processing.
1569f2512ba1SRui Paulo 	 */
15703cf38784SMichael Tuexen 	if (tp->t_flags2 & TF2_ECN_PERMIT) {
1571ee97681eSMichael Tuexen 		if (thflags & TH_CWR) {
15723cf38784SMichael Tuexen 			tp->t_flags2 &= ~TF2_ECN_SND_ECE;
1573ee97681eSMichael Tuexen 			tp->t_flags |= TF_ACKNOW;
1574ee97681eSMichael Tuexen 		}
1575f2512ba1SRui Paulo 		switch (iptos & IPTOS_ECN_MASK) {
1576f2512ba1SRui Paulo 		case IPTOS_ECN_CE:
15773cf38784SMichael Tuexen 			tp->t_flags2 |= TF2_ECN_SND_ECE;
157878b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ce);
1579f2512ba1SRui Paulo 			break;
1580f2512ba1SRui Paulo 		case IPTOS_ECN_ECT0:
158178b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ect0);
1582f2512ba1SRui Paulo 			break;
1583f2512ba1SRui Paulo 		case IPTOS_ECN_ECT1:
158478b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ect1);
1585f2512ba1SRui Paulo 			break;
1586f2512ba1SRui Paulo 		}
158764807b30SHiren Panchasara 
158864807b30SHiren Panchasara 		/* Process a packet differently from RFC3168. */
158964807b30SHiren Panchasara 		cc_ecnpkt_handler(tp, th, iptos);
159064807b30SHiren Panchasara 
1591dbc42409SLawrence Stewart 		/* Congestion experienced. */
1592dbc42409SLawrence Stewart 		if (thflags & TH_ECE) {
1593dbc42409SLawrence Stewart 			cc_cong_signal(tp, th, CC_ECN);
1594f2512ba1SRui Paulo 		}
1595f2512ba1SRui Paulo 	}
1596f2512ba1SRui Paulo 
1597f2512ba1SRui Paulo 	/*
1598f72167f4SAndre Oppermann 	 * Parse options on any incoming segment.
1599f72167f4SAndre Oppermann 	 */
1600302ce8d6SAndre Oppermann 	tcp_dooptions(&to, (u_char *)(th + 1),
1601302ce8d6SAndre Oppermann 	    (th->th_off << 2) - sizeof(struct tcphdr),
1602302ce8d6SAndre Oppermann 	    (thflags & TH_SYN) ? TO_SYN : 0);
1603f72167f4SAndre Oppermann 
1604fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1605fcf59617SAndrey V. Elsukov 	if ((tp->t_flags & TF_SIGNATURE) != 0 &&
1606fcf59617SAndrey V. Elsukov 	    (to.to_flags & TOF_SIGNATURE) == 0) {
1607fcf59617SAndrey V. Elsukov 		TCPSTAT_INC(tcps_sig_err_sigopt);
1608fcf59617SAndrey V. Elsukov 		/* XXX: should drop? */
1609fcf59617SAndrey V. Elsukov 	}
1610fcf59617SAndrey V. Elsukov #endif
1611f72167f4SAndre Oppermann 	/*
1612f72167f4SAndre Oppermann 	 * If echoed timestamp is later than the current time,
1613bf6d304aSAndre Oppermann 	 * fall back to non RFC1323 RTT calculation.  Normalize
1614bf6d304aSAndre Oppermann 	 * timestamp if syncookies were used when this connection
1615bf6d304aSAndre Oppermann 	 * was established.
1616f72167f4SAndre Oppermann 	 */
1617bf6d304aSAndre Oppermann 	if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
1618e16fa5caSJohn-Mark Gurney 		to.to_tsecr -= tp->ts_offset;
1619d8951c8aSBjoern A. Zeeb 		if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks()))
1620f72167f4SAndre Oppermann 			to.to_tsecr = 0;
162110d20c84SMatt Macy 		else if (tp->t_flags & TF_PREVVALID &&
162210d20c84SMatt Macy 			 tp->t_badrxtwin != 0 && SEQ_LT(to.to_tsecr, tp->t_badrxtwin))
162310d20c84SMatt Macy 			cc_cong_signal(tp, th, CC_RTO_ERR);
1624bf6d304aSAndre Oppermann 	}
162507dacf03SAndre Oppermann 	/*
162697d8d152SAndre Oppermann 	 * Process options only when we get SYN/ACK back. The SYN case
162797d8d152SAndre Oppermann 	 * for incoming connections is handled in tcp_syncache.
16285396d0f8SAndre Oppermann 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN>
16295396d0f8SAndre Oppermann 	 * or <SYN,ACK>) segment itself is never scaled.
163097d8d152SAndre Oppermann 	 * XXX this is traditional behavior, may need to be cleaned up.
1631df8bae1dSRodney W. Grimes 	 */
16320a389eabSSimon L. B. Nielsen 	if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
16336e16d877SRichard Scheffenegger 		/* Handle parallel SYN for ECN */
16346e16d877SRichard Scheffenegger 		if (!(thflags & TH_ACK) &&
16356e16d877SRichard Scheffenegger 		    ((thflags & (TH_CWR | TH_ECE)) == (TH_CWR | TH_ECE)) &&
16366e16d877SRichard Scheffenegger 		    ((V_tcp_do_ecn == 1) || (V_tcp_do_ecn == 2))) {
16376e16d877SRichard Scheffenegger 			tp->t_flags2 |= TF2_ECN_PERMIT;
16386e16d877SRichard Scheffenegger 			tp->t_flags2 |= TF2_ECN_SND_ECE;
16396e16d877SRichard Scheffenegger 			TCPSTAT_INC(tcps_ecn_shs);
16406e16d877SRichard Scheffenegger 		}
1641464fcfbcSAndre Oppermann 		if ((to.to_flags & TOF_SCALE) &&
1642464fcfbcSAndre Oppermann 		    (tp->t_flags & TF_REQ_SCALE)) {
1643be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_SCALE;
164402a1a643SAndre Oppermann 			tp->snd_scale = to.to_wscale;
16458e051165SRichard Scheffenegger 		} else
16468e051165SRichard Scheffenegger 			tp->t_flags &= ~TF_REQ_SCALE;
16475396d0f8SAndre Oppermann 		/*
16485396d0f8SAndre Oppermann 		 * Initial send window.  It will be updated with
16495396d0f8SAndre Oppermann 		 * the next incoming segment to the scaled value.
16505396d0f8SAndre Oppermann 		 */
16515396d0f8SAndre Oppermann 		tp->snd_wnd = th->th_win;
16528e051165SRichard Scheffenegger 		if ((to.to_flags & TOF_TS) &&
16538e051165SRichard Scheffenegger 		    (tp->t_flags & TF_REQ_TSTMP)) {
1654be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_TSTMP;
1655be2ac88cSJonathan Lemon 			tp->ts_recent = to.to_tsval;
1656d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
16578e051165SRichard Scheffenegger 		} else
16588e051165SRichard Scheffenegger 			tp->t_flags &= ~TF_REQ_TSTMP;
1659be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_MSS)
1660be2ac88cSJonathan Lemon 			tcp_mss(tp, to.to_mss);
16613529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
16623529149eSAndre Oppermann 		    (to.to_flags & TOF_SACKPERM) == 0)
16633529149eSAndre Oppermann 			tp->t_flags &= ~TF_SACK_PERMIT;
1664c560df6fSPatrick Kelsey 		if (IS_FASTOPEN(tp->t_flags)) {
1665a026a53aSMichael Tuexen 			if (to.to_flags & TOF_FASTOPEN) {
1666a026a53aSMichael Tuexen 				uint16_t mss;
1667a026a53aSMichael Tuexen 
1668a026a53aSMichael Tuexen 				if (to.to_flags & TOF_MSS)
1669a026a53aSMichael Tuexen 					mss = to.to_mss;
1670c560df6fSPatrick Kelsey 				else
1671a026a53aSMichael Tuexen 					if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
1672a026a53aSMichael Tuexen 						mss = TCP6_MSS;
1673a026a53aSMichael Tuexen 					else
1674a026a53aSMichael Tuexen 						mss = TCP_MSS;
1675a026a53aSMichael Tuexen 				tcp_fastopen_update_cache(tp, mss,
1676a026a53aSMichael Tuexen 				    to.to_tfo_len, to.to_tfo_cookie);
1677a026a53aSMichael Tuexen 			} else
1678c560df6fSPatrick Kelsey 				tcp_fastopen_disable_path(tp);
1679c560df6fSPatrick Kelsey 		}
16806d90faf3SPaul Saab 	}
16816d90faf3SPaul Saab 
1682df8bae1dSRodney W. Grimes 	/*
1683*283c76c7SMichael Tuexen 	 * If timestamps were negotiated during SYN/ACK and a
1684*283c76c7SMichael Tuexen 	 * segment without a timestamp is received, silently drop
1685*283c76c7SMichael Tuexen 	 * the segment.
1686*283c76c7SMichael Tuexen 	 * See section 3.2 of RFC 7323.
16874da9052aSMichael Tuexen 	 */
16884da9052aSMichael Tuexen 	if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) {
16894da9052aSMichael Tuexen 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
16904da9052aSMichael Tuexen 			log(LOG_DEBUG, "%s; %s: Timestamp missing, "
1691*283c76c7SMichael Tuexen 			    "segment silently dropped\n", s, __func__);
16924da9052aSMichael Tuexen 			free(s, M_TCPLOG);
16934da9052aSMichael Tuexen 		}
1694*283c76c7SMichael Tuexen 		goto drop;
16954da9052aSMichael Tuexen 	}
1696*283c76c7SMichael Tuexen 	/*
1697*283c76c7SMichael Tuexen 	 * If timestamps were not negotiated during SYN/ACK and a
1698*283c76c7SMichael Tuexen 	 * segment without a timestamp is received, ignore the
1699*283c76c7SMichael Tuexen 	 * timestamp and process the packet normally.
1700*283c76c7SMichael Tuexen 	 * See section 3.2 of RFC 7323.
1701*283c76c7SMichael Tuexen 	 */
17024da9052aSMichael Tuexen 	if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) {
17034da9052aSMichael Tuexen 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
17044da9052aSMichael Tuexen 			log(LOG_DEBUG, "%s; %s: Timestamp not expected, "
1705*283c76c7SMichael Tuexen 			    "segment processed normally\n", s, __func__);
17064da9052aSMichael Tuexen 			free(s, M_TCPLOG);
17074da9052aSMichael Tuexen 		}
17084da9052aSMichael Tuexen 	}
17094da9052aSMichael Tuexen 
17104da9052aSMichael Tuexen 	/*
1711df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
1712df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
1713df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
1714df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
1715df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
1716df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
1717df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
1718df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
1719df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
1720df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
1721df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
1722df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
1723a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
1724c5ad39b9SAndre Oppermann 	 * Since we check for TCPS_ESTABLISHED first, it can only
1725a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
1726df8bae1dSRodney W. Grimes 	 */
1727df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
1728c5ad39b9SAndre Oppermann 	    th->th_seq == tp->rcv_nxt &&
1729fb59c426SYoshinobu Inoue 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1730c5ad39b9SAndre Oppermann 	    tp->snd_nxt == tp->snd_max &&
1731c5ad39b9SAndre Oppermann 	    tiwin && tiwin == tp->snd_wnd &&
1732a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
1733c28440dbSRandall Stewart 	    SEGQ_EMPTY(tp) &&
17344741bfcbSPatrick Kelsey 	    ((to.to_flags & TOF_TS) == 0 ||
1735c5ad39b9SAndre Oppermann 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) {
1736df8bae1dSRodney W. Grimes 		/*
1737df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
1738df8bae1dSRodney W. Grimes 		 * record the timestamp.
1739a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
1740a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1741df8bae1dSRodney W. Grimes 		 */
1742be2ac88cSJonathan Lemon 		if ((to.to_flags & TOF_TS) != 0 &&
1743fb59c426SYoshinobu Inoue 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1744d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
1745a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
1746df8bae1dSRodney W. Grimes 		}
1747df8bae1dSRodney W. Grimes 
1748fb59c426SYoshinobu Inoue 		if (tlen == 0) {
1749fb59c426SYoshinobu Inoue 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
1750fb59c426SYoshinobu Inoue 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
1751dbc42409SLawrence Stewart 			    !IN_RECOVERY(tp->t_flags) &&
1752fc30a251SAndre Oppermann 			    (to.to_flags & TOF_SACK) == 0 &&
1753dbc42409SLawrence Stewart 			    TAILQ_EMPTY(&tp->snd_holes)) {
1754df8bae1dSRodney W. Grimes 				/*
1755e8949f74SAndre Oppermann 				 * This is a pure ack for outstanding data.
1756df8bae1dSRodney W. Grimes 				 */
175778b50714SRobert Watson 				TCPSTAT_INC(tcps_predack);
1758252ca428SRobert Watson 
17599b8b58e0SJonathan Lemon 				/*
176010d20c84SMatt Macy 				 * "bad retransmit" recovery without timestamps.
17619b8b58e0SJonathan Lemon 				 */
176210d20c84SMatt Macy 				if ((to.to_flags & TOF_TS) == 0 &&
176310d20c84SMatt Macy 				    tp->t_rxtshift == 1 &&
1764672dc4aeSJohn Baldwin 				    tp->t_flags & TF_PREVVALID &&
17656dfb8b31SJohn Baldwin 				    (int)(ticks - tp->t_badrxtwin) < 0) {
1766dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_RTO_ERR);
17679b8b58e0SJonathan Lemon 				}
1768fa55172bSMatthew Dillon 
1769fa55172bSMatthew Dillon 				/*
1770fa55172bSMatthew Dillon 				 * Recalculate the transmit timer / rtt.
1771fa55172bSMatthew Dillon 				 *
1772fa55172bSMatthew Dillon 				 * Some boxes send broken timestamp replies
1773fa55172bSMatthew Dillon 				 * during the SYN+ACK phase, ignore
1774fa55172bSMatthew Dillon 				 * timestamps of 0 or we could calculate a
1775fa55172bSMatthew Dillon 				 * huge RTT and blow up the retransmit timer.
1776fa55172bSMatthew Dillon 				 */
1777fa55172bSMatthew Dillon 				if ((to.to_flags & TOF_TS) != 0 &&
1778fa55172bSMatthew Dillon 				    to.to_tsecr) {
17793ac12506SJonathan T. Looney 					uint32_t t;
1780d8951c8aSBjoern A. Zeeb 
1781d8951c8aSBjoern A. Zeeb 					t = tcp_ts_getticks() - to.to_tsecr;
1782d8951c8aSBjoern A. Zeeb 					if (!tp->t_rttlow || tp->t_rttlow > t)
1783d8951c8aSBjoern A. Zeeb 						tp->t_rttlow = t;
1784a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
1785d8951c8aSBjoern A. Zeeb 					    TCP_TS_TO_TICKS(t) + 1);
1786fa55172bSMatthew Dillon 				} else if (tp->t_rtttime &&
1787fa55172bSMatthew Dillon 				    SEQ_GT(th->th_ack, tp->t_rtseq)) {
1788eaf80179SAndre Oppermann 					if (!tp->t_rttlow ||
1789eaf80179SAndre Oppermann 					    tp->t_rttlow > ticks - tp->t_rtttime)
1790eaf80179SAndre Oppermann 						tp->t_rttlow = ticks - tp->t_rtttime;
1791c068736aSJeffrey Hsu 					tcp_xmit_timer(tp,
1792c068736aSJeffrey Hsu 							ticks - tp->t_rtttime);
1793fa55172bSMatthew Dillon 				}
1794dbc42409SLawrence Stewart 				acked = BYTES_THIS_ACK(tp, th);
179539bc9de5SLawrence Stewart 
1796bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
179739bc9de5SLawrence Stewart 				/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
179839bc9de5SLawrence Stewart 				hhook_run_tcp_est_in(tp, th, &to);
1799bd79708dSJonathan T. Looney #endif
180039bc9de5SLawrence Stewart 
18014b7b743cSLawrence Stewart 				TCPSTAT_ADD(tcps_rcvackpack, nsegs);
180278b50714SRobert Watson 				TCPSTAT_ADD(tcps_rcvackbyte, acked);
1803df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
18049d11646dSJeffrey Hsu 				if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
18059d11646dSJeffrey Hsu 				    SEQ_LEQ(th->th_ack, tp->snd_recover))
18069d11646dSJeffrey Hsu 					tp->snd_recover = th->th_ack - 1;
1807dbc42409SLawrence Stewart 
1808dbc42409SLawrence Stewart 				/*
1809dbc42409SLawrence Stewart 				 * Let the congestion control algorithm update
1810dbc42409SLawrence Stewart 				 * congestion control related information. This
1811dbc42409SLawrence Stewart 				 * typically means increasing the congestion
1812dbc42409SLawrence Stewart 				 * window.
1813dbc42409SLawrence Stewart 				 */
18144b7b743cSLawrence Stewart 				cc_ack_received(tp, th, nsegs, CC_ACK);
1815dbc42409SLawrence Stewart 
18169d11646dSJeffrey Hsu 				tp->snd_una = th->th_ack;
18171645d090SJeff Roberson 				/*
1818e8949f74SAndre Oppermann 				 * Pull snd_wl2 up to prevent seq wrap relative
18191645d090SJeff Roberson 				 * to th_ack.
18201645d090SJeff Roberson 				 */
1821cb942153SJeffrey Hsu 				tp->snd_wl2 = th->th_ack;
1822b5addd85SJeffrey Hsu 				tp->t_dupacks = 0;
1823df8bae1dSRodney W. Grimes 				m_freem(m);
1824df8bae1dSRodney W. Grimes 
1825df8bae1dSRodney W. Grimes 				/*
1826df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
1827df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
1828df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
1829df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
1830df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
1831df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
1832df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
1833e8949f74SAndre Oppermann 				 */
18343c653157SHartmut Brandt #ifdef TCPDEBUG
18353c653157SHartmut Brandt 				if (so->so_options & SO_DEBUG)
18363c653157SHartmut Brandt 					tcp_trace(TA_INPUT, ostate, tp,
18373c653157SHartmut Brandt 					    (void *)tcp_saveipgen,
18383c653157SHartmut Brandt 					    &tcp_savetcp, 0);
18393c653157SHartmut Brandt #endif
18402b9c9984SGeorge V. Neville-Neil 				TCP_PROBE3(debug__input, tp, th, m);
1841df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
1842b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
1843b8152ba7SAndre Oppermann 				else if (!tcp_timer_active(tp, TT_PERSIST))
1844b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT,
1845b8152ba7SAndre Oppermann 						      tp->t_rxtcur);
18464d0770f1SRichard Scheffenegger 				tp->t_flags |= TF_WAKESOW;
1847cfa6009eSGleb Smirnoff 				if (sbavail(&so->so_snd))
184855bceb1eSRandall Stewart 					(void) tp->t_fb->tfb_tcp_output(tp);
1849a14c749fSJonathan Lemon 				goto check_delack;
1850df8bae1dSRodney W. Grimes 			}
1851fb59c426SYoshinobu Inoue 		} else if (th->th_ack == tp->snd_una &&
1852fb59c426SYoshinobu Inoue 		    tlen <= sbspace(&so->so_rcv)) {
18536741ecf5SAndre Oppermann 			int newsize = 0;	/* automatic sockbuf scaling */
18546741ecf5SAndre Oppermann 
1855df8bae1dSRodney W. Grimes 			/*
1856252ca428SRobert Watson 			 * This is a pure, in-sequence data packet with
1857252ca428SRobert Watson 			 * nothing on the reassembly queue and we have enough
1858252ca428SRobert Watson 			 * buffer space to take it.
1859df8bae1dSRodney W. Grimes 			 */
18606d90faf3SPaul Saab 			/* Clean receiver SACK report if present */
18613529149eSAndre Oppermann 			if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks)
18626d90faf3SPaul Saab 				tcp_clean_sackreport(tp);
186378b50714SRobert Watson 			TCPSTAT_INC(tcps_preddat);
1864fb59c426SYoshinobu Inoue 			tp->rcv_nxt += tlen;
1865e854dd38SRandall Stewart 			if (tlen &&
1866e854dd38SRandall Stewart 			    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1867e854dd38SRandall Stewart 			    (tp->t_fbyte_in == 0)) {
1868e854dd38SRandall Stewart 				tp->t_fbyte_in = ticks;
1869e854dd38SRandall Stewart 				if (tp->t_fbyte_in == 0)
1870e854dd38SRandall Stewart 					tp->t_fbyte_in = 1;
1871e854dd38SRandall Stewart 				if (tp->t_fbyte_out && tp->t_fbyte_in)
1872e854dd38SRandall Stewart 					tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1873e854dd38SRandall Stewart 			}
18741645d090SJeff Roberson 			/*
18751645d090SJeff Roberson 			 * Pull snd_wl1 up to prevent seq wrap relative to
18761645d090SJeff Roberson 			 * th_seq.
18771645d090SJeff Roberson 			 */
187860ee3bb2SAndre Oppermann 			tp->snd_wl1 = th->th_seq;
18791645d090SJeff Roberson 			/*
18801645d090SJeff Roberson 			 * Pull rcv_up up to prevent seq wrap relative to
18811645d090SJeff Roberson 			 * rcv_nxt.
18821645d090SJeff Roberson 			 */
18831645d090SJeff Roberson 			tp->rcv_up = tp->rcv_nxt;
18844b7b743cSLawrence Stewart 			TCPSTAT_ADD(tcps_rcvpack, nsegs);
188578b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
18863c653157SHartmut Brandt #ifdef TCPDEBUG
18873c653157SHartmut Brandt 			if (so->so_options & SO_DEBUG)
18883c653157SHartmut Brandt 				tcp_trace(TA_INPUT, ostate, tp,
18893c653157SHartmut Brandt 				    (void *)tcp_saveipgen, &tcp_savetcp, 0);
18903c653157SHartmut Brandt #endif
18912b9c9984SGeorge V. Neville-Neil 			TCP_PROBE3(debug__input, tp, th, m);
18925d06879aSGeorge V. Neville-Neil 
1893e44c1887SSteven Hartland 			newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
18946741ecf5SAndre Oppermann 
18956741ecf5SAndre Oppermann 			/* Add data to socket buffer. */
18961e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
1897c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1898c1c36a2cSMike Silbersack 				m_freem(m);
1899c1c36a2cSMike Silbersack 			} else {
19006741ecf5SAndre Oppermann 				/*
19016741ecf5SAndre Oppermann 				 * Set new socket buffer size.
19026741ecf5SAndre Oppermann 				 * Give up when limit is reached.
19036741ecf5SAndre Oppermann 				 */
19046741ecf5SAndre Oppermann 				if (newsize)
19056741ecf5SAndre Oppermann 					if (!sbreserve_locked(&so->so_rcv,
19066c8286e4SRobert Watson 					    newsize, so, NULL))
19076741ecf5SAndre Oppermann 						so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
1908fb59c426SYoshinobu Inoue 				m_adj(m, drop_hdrlen);	/* delayed header drop */
1909651e4e6aSGleb Smirnoff 				sbappendstream_locked(&so->so_rcv, m, 0);
1910c1c36a2cSMike Silbersack 			}
19114d0770f1SRichard Scheffenegger 			SOCKBUF_UNLOCK(&so->so_rcv);
19124d0770f1SRichard Scheffenegger 			tp->t_flags |= TF_WAKESOR;
1913c1e5a6e5SAndre Oppermann 			if (DELAY_ACK(tp, tlen)) {
19143bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
1915f498eeeeSDavid Greenman 			} else {
1916e612a582SDavid Greenman 				tp->t_flags |= TF_ACKNOW;
191755bceb1eSRandall Stewart 				tp->t_fb->tfb_tcp_output(tp);
1918e612a582SDavid Greenman 			}
1919a14c749fSJonathan Lemon 			goto check_delack;
1920df8bae1dSRodney W. Grimes 		}
1921df8bae1dSRodney W. Grimes 	}
1922df8bae1dSRodney W. Grimes 
1923df8bae1dSRodney W. Grimes 	/*
1924df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
1925df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
1926df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
1927df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
1928df8bae1dSRodney W. Grimes 	 */
1929df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
1930df8bae1dSRodney W. Grimes 	if (win < 0)
1931df8bae1dSRodney W. Grimes 		win = 0;
193266e39adcSJohn Polstra 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1933df8bae1dSRodney W. Grimes 
1934df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1935df8bae1dSRodney W. Grimes 	/*
1936764d8cefSBill Fenner 	 * If the state is SYN_RECEIVED:
1937764d8cefSBill Fenner 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
1938764d8cefSBill Fenner 	 */
1939764d8cefSBill Fenner 	case TCPS_SYN_RECEIVED:
1940fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1941fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1942a57815efSBosko Milekic 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1943a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
1944a57815efSBosko Milekic 				goto dropwithreset;
1945a57815efSBosko Milekic 		}
194668bd7ed1SJonathan T. Looney 		if (IS_FASTOPEN(tp->t_flags)) {
1947281a0fd4SPatrick Kelsey 			/*
1948281a0fd4SPatrick Kelsey 			 * When a TFO connection is in SYN_RECEIVED, the
1949281a0fd4SPatrick Kelsey 			 * only valid packets are the initial SYN, a
1950281a0fd4SPatrick Kelsey 			 * retransmit/copy of the initial SYN (possibly with
1951281a0fd4SPatrick Kelsey 			 * a subset of the original data), a valid ACK, a
1952281a0fd4SPatrick Kelsey 			 * FIN, or a RST.
1953281a0fd4SPatrick Kelsey 			 */
1954281a0fd4SPatrick Kelsey 			if ((thflags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) {
1955281a0fd4SPatrick Kelsey 				rstreason = BANDLIM_RST_OPENPORT;
1956281a0fd4SPatrick Kelsey 				goto dropwithreset;
1957281a0fd4SPatrick Kelsey 			} else if (thflags & TH_SYN) {
1958281a0fd4SPatrick Kelsey 				/* non-initial SYN is ignored */
1959281a0fd4SPatrick Kelsey 				if ((tcp_timer_active(tp, TT_DELACK) ||
1960281a0fd4SPatrick Kelsey 				     tcp_timer_active(tp, TT_REXMT)))
1961281a0fd4SPatrick Kelsey 					goto drop;
1962281a0fd4SPatrick Kelsey 			} else if (!(thflags & (TH_ACK|TH_FIN|TH_RST))) {
1963281a0fd4SPatrick Kelsey 				goto drop;
1964281a0fd4SPatrick Kelsey 			}
1965281a0fd4SPatrick Kelsey 		}
1966764d8cefSBill Fenner 		break;
1967764d8cefSBill Fenner 
1968764d8cefSBill Fenner 	/*
1969df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
197098732609SMichael Tuexen 	 *	if seg contains a RST with valid ACK (SEQ.ACK has already
197198732609SMichael Tuexen 	 *	    been verified), then drop the connection.
197298732609SMichael Tuexen 	 *	if seg contains a RST without an ACK, drop the seg.
197398732609SMichael Tuexen 	 *	if seg does not contain SYN, then drop the seg.
1974df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
1975df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
1976df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
1977f2512ba1SRui Paulo 	 *	if seg contains an ECE and ECN support is enabled, the stream
1978f2512ba1SRui Paulo 	 *	    is ECN capable.
1979df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1980df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
1981df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
1982df8bae1dSRodney W. Grimes 	 */
1983df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
198457f60867SMark Johnston 		if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) {
1985d9fae5abSAndriy Gapon 			TCP_PROBE5(connect__refused, NULL, tp,
19862b9c9984SGeorge V. Neville-Neil 			    m, tp, th);
1987df8bae1dSRodney W. Grimes 			tp = tcp_drop(tp, ECONNREFUSED);
198857f60867SMark Johnston 		}
19890ca3f933SAndre Oppermann 		if (thflags & TH_RST)
1990df8bae1dSRodney W. Grimes 			goto drop;
19910ca3f933SAndre Oppermann 		if (!(thflags & TH_SYN))
1992df8bae1dSRodney W. Grimes 			goto drop;
1993464fcfbcSAndre Oppermann 
1994fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
1995df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
1996fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1997c560df6fSPatrick Kelsey 			int tfo_partial_ack = 0;
1998c560df6fSPatrick Kelsey 
199978b50714SRobert Watson 			TCPSTAT_INC(tcps_connects);
2000845799c1SAndras Olah 			soisconnected(so);
2001c488362eSRobert Watson #ifdef MAC
200230d239bcSRobert Watson 			mac_socketpeer_set_from_mbuf(m, so);
2003c488362eSRobert Watson #endif
2004845799c1SAndras Olah 			/* Do window scaling on this connection? */
2005845799c1SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2006845799c1SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
2007845799c1SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2008845799c1SAndras Olah 			}
20093ac12506SJonathan T. Looney 			tp->rcv_adv += min(tp->rcv_wnd,
2010766282cbSJohn Baldwin 			    TCP_MAXWIN << tp->rcv_scale);
2011a0292f23SGarrett Wollman 			tp->snd_una++;		/* SYN is acked */
2012a0292f23SGarrett Wollman 			/*
2013c560df6fSPatrick Kelsey 			 * If not all the data that was sent in the TFO SYN
2014c560df6fSPatrick Kelsey 			 * has been acked, resend the remainder right away.
2015c560df6fSPatrick Kelsey 			 */
2016c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags) &&
2017c560df6fSPatrick Kelsey 			    (tp->snd_una != tp->snd_max)) {
2018c560df6fSPatrick Kelsey 				tp->snd_nxt = th->th_ack;
2019c560df6fSPatrick Kelsey 				tfo_partial_ack = 1;
2020c560df6fSPatrick Kelsey 			}
2021c560df6fSPatrick Kelsey 			/*
2022a0292f23SGarrett Wollman 			 * If there's data, delay ACK; if there's also a FIN
2023a0292f23SGarrett Wollman 			 * ACKNOW will be turned on later.
2024a0292f23SGarrett Wollman 			 */
2025c560df6fSPatrick Kelsey 			if (DELAY_ACK(tp, tlen) && tlen != 0 && !tfo_partial_ack)
2026b8152ba7SAndre Oppermann 				tcp_timer_activate(tp, TT_DELACK,
2027b8152ba7SAndre Oppermann 				    tcp_delacktime);
2028a0292f23SGarrett Wollman 			else
2029a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
2030f2512ba1SRui Paulo 
2031bf7fcdb1SMichael Tuexen 			if (((thflags & (TH_CWR | TH_ECE)) == TH_ECE) &&
2032a2d59694SMichael Tuexen 			    (V_tcp_do_ecn == 1)) {
20333cf38784SMichael Tuexen 				tp->t_flags2 |= TF2_ECN_PERMIT;
203478b50714SRobert Watson 				TCPSTAT_INC(tcps_ecn_shs);
2035f2512ba1SRui Paulo 			}
2036f2512ba1SRui Paulo 
2037a0292f23SGarrett Wollman 			/*
2038a0292f23SGarrett Wollman 			 * Received <SYN,ACK> in SYN_SENT[*] state.
2039a0292f23SGarrett Wollman 			 * Transitions:
2040a0292f23SGarrett Wollman 			 *	SYN_SENT  --> ESTABLISHED
2041a0292f23SGarrett Wollman 			 *	SYN_SENT* --> FIN_WAIT_1
2042a0292f23SGarrett Wollman 			 */
20439b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
2044a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDFIN) {
204557f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_1);
2046a0292f23SGarrett Wollman 				tp->t_flags &= ~TF_NEEDFIN;
2047fb59c426SYoshinobu Inoue 				thflags &= ~TH_SYN;
20487ff19458SPaul Traina 			} else {
204957f60867SMark Johnston 				tcp_state_change(tp, TCPS_ESTABLISHED);
2050d9fae5abSAndriy Gapon 				TCP_PROBE5(connect__established, NULL, tp,
20512b9c9984SGeorge V. Neville-Neil 				    m, tp, th);
2052dbc42409SLawrence Stewart 				cc_conn_init(tp);
20539077f387SGleb Smirnoff 				tcp_timer_activate(tp, TT_KEEP,
20549077f387SGleb Smirnoff 				    TP_KEEPIDLE(tp));
20557ff19458SPaul Traina 			}
2056a0292f23SGarrett Wollman 		} else {
2057a0292f23SGarrett Wollman 			/*
2058c068736aSJeffrey Hsu 			 * Received initial SYN in SYN-SENT[*] state =>
2059153edc50SHiren Panchasara 			 * simultaneous open.
2060c068736aSJeffrey Hsu 			 * If it succeeds, connection is * half-synchronized.
2061c068736aSJeffrey Hsu 			 * Otherwise, do 3-way handshake:
2062a0292f23SGarrett Wollman 			 *        SYN-SENT -> SYN-RECEIVED
2063a0292f23SGarrett Wollman 			 *        SYN-SENT* -> SYN-RECEIVED*
2064a0292f23SGarrett Wollman 			 */
20654b8e98d6SQing Li 			tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
2066b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
206757f60867SMark Johnston 			tcp_state_change(tp, TCPS_SYN_RECEIVED);
2068a0292f23SGarrett Wollman 		}
2069df8bae1dSRodney W. Grimes 
20708501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
20717cfc6904SRobert Watson 
2072df8bae1dSRodney W. Grimes 		/*
2073fb59c426SYoshinobu Inoue 		 * Advance th->th_seq to correspond to first data byte.
2074df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
2075df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
2076df8bae1dSRodney W. Grimes 		 */
2077fb59c426SYoshinobu Inoue 		th->th_seq++;
2078fb59c426SYoshinobu Inoue 		if (tlen > tp->rcv_wnd) {
2079fb59c426SYoshinobu Inoue 			todrop = tlen - tp->rcv_wnd;
2080df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
2081fb59c426SYoshinobu Inoue 			tlen = tp->rcv_wnd;
2082fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
208378b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpackafterwin);
208478b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2085df8bae1dSRodney W. Grimes 		}
2086fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
2087fb59c426SYoshinobu Inoue 		tp->rcv_up = th->th_seq;
2088a0292f23SGarrett Wollman 		/*
2089a0292f23SGarrett Wollman 		 * Client side of transaction: already sent SYN and data.
2090a0292f23SGarrett Wollman 		 * If the remote host used T/TCP to validate the SYN,
2091a0292f23SGarrett Wollman 		 * our data will be ACK'd; if so, enter normal data segment
2092a0292f23SGarrett Wollman 		 * processing in the middle of step 5, ack processing.
2093a0292f23SGarrett Wollman 		 * Otherwise, goto step 6.
2094a0292f23SGarrett Wollman 		 */
2095fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK)
2096a0292f23SGarrett Wollman 			goto process_ACK;
2097c068736aSJeffrey Hsu 
2098df8bae1dSRodney W. Grimes 		goto step6;
2099c068736aSJeffrey Hsu 
2100a0292f23SGarrett Wollman 	/*
2101a0292f23SGarrett Wollman 	 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
2102c94c54e4SAndre Oppermann 	 *      do normal processing.
2103a0292f23SGarrett Wollman 	 *
2104c94c54e4SAndre Oppermann 	 * NB: Leftover from RFC1644 T/TCP.  Cases to be reused later.
2105a0292f23SGarrett Wollman 	 */
2106a0292f23SGarrett Wollman 	case TCPS_LAST_ACK:
2107a0292f23SGarrett Wollman 	case TCPS_CLOSING:
2108a0292f23SGarrett Wollman 		break;  /* continue normal processing */
2109df8bae1dSRodney W. Grimes 	}
2110df8bae1dSRodney W. Grimes 
2111df8bae1dSRodney W. Grimes 	/*
2112df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
211380ab7c0eSGarrett Wollman 	 * First check the RST flag and sequence number since reset segments
211480ab7c0eSGarrett Wollman 	 * are exempt from the timestamp and connection count tests.  This
211580ab7c0eSGarrett Wollman 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
211680ab7c0eSGarrett Wollman 	 * below which allowed reset segments in half the sequence space
211780ab7c0eSGarrett Wollman 	 * to fall though and be processed (which gives forged reset
211880ab7c0eSGarrett Wollman 	 * segments with a random sequence number a 50 percent chance of
211980ab7c0eSGarrett Wollman 	 * killing a connection).
212080ab7c0eSGarrett Wollman 	 * Then check timestamp, if present.
2121a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
2122df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
2123df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
2124df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
212580ab7c0eSGarrett Wollman 	 */
2126fb59c426SYoshinobu Inoue 	if (thflags & TH_RST) {
21273220a212SGleb Smirnoff 		/*
21283220a212SGleb Smirnoff 		 * RFC5961 Section 3.2
21293220a212SGleb Smirnoff 		 *
21303220a212SGleb Smirnoff 		 * - RST drops connection only if SEG.SEQ == RCV.NXT.
21313220a212SGleb Smirnoff 		 * - If RST is in window, we send challenge ACK.
21323220a212SGleb Smirnoff 		 *
21333220a212SGleb Smirnoff 		 * Note: to take into account delayed ACKs, we should
21343220a212SGleb Smirnoff 		 *   test against last_ack_sent instead of rcv_nxt.
21353220a212SGleb Smirnoff 		 * Note 2: we handle special case of closed window, not
21363220a212SGleb Smirnoff 		 *   covered by the RFC.
21373220a212SGleb Smirnoff 		 */
21383220a212SGleb Smirnoff 		if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
21393220a212SGleb Smirnoff 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
21403220a212SGleb Smirnoff 		    (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) {
21413220a212SGleb Smirnoff 			KASSERT(tp->t_state != TCPS_SYN_SENT,
21423220a212SGleb Smirnoff 			    ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p",
21433220a212SGleb Smirnoff 			    __func__, th, tp));
21443220a212SGleb Smirnoff 
21453220a212SGleb Smirnoff 			if (V_tcp_insecure_rst ||
21463220a212SGleb Smirnoff 			    tp->last_ack_sent == th->th_seq) {
21473220a212SGleb Smirnoff 				TCPSTAT_INC(tcps_drops);
21483220a212SGleb Smirnoff 				/* Drop the connection. */
21493220a212SGleb Smirnoff 				switch (tp->t_state) {
215080ab7c0eSGarrett Wollman 				case TCPS_SYN_RECEIVED:
215180ab7c0eSGarrett Wollman 					so->so_error = ECONNREFUSED;
215280ab7c0eSGarrett Wollman 					goto close;
215380ab7c0eSGarrett Wollman 				case TCPS_ESTABLISHED:
215480ab7c0eSGarrett Wollman 				case TCPS_FIN_WAIT_1:
215580ab7c0eSGarrett Wollman 				case TCPS_FIN_WAIT_2:
215680ab7c0eSGarrett Wollman 				case TCPS_CLOSE_WAIT:
21576779a1a1SMichael Tuexen 				case TCPS_CLOSING:
21586779a1a1SMichael Tuexen 				case TCPS_LAST_ACK:
215980ab7c0eSGarrett Wollman 					so->so_error = ECONNRESET;
216080ab7c0eSGarrett Wollman 				close:
21613220a212SGleb Smirnoff 					/* FALLTHROUGH */
21623220a212SGleb Smirnoff 				default:
216380ab7c0eSGarrett Wollman 					tp = tcp_close(tp);
21643220a212SGleb Smirnoff 				}
21653220a212SGleb Smirnoff 			} else {
21663220a212SGleb Smirnoff 				TCPSTAT_INC(tcps_badrst);
21673220a212SGleb Smirnoff 				/* Send challenge ACK. */
21683220a212SGleb Smirnoff 				tcp_respond(tp, mtod(m, void *), th, m,
21693220a212SGleb Smirnoff 				    tp->rcv_nxt, tp->snd_nxt, TH_ACK);
21703220a212SGleb Smirnoff 				tp->last_ack_sent = tp->rcv_nxt;
21713220a212SGleb Smirnoff 				m = NULL;
21723220a212SGleb Smirnoff 			}
21733220a212SGleb Smirnoff 		}
21743220a212SGleb Smirnoff 		goto drop;
21753220a212SGleb Smirnoff 	}
217680ab7c0eSGarrett Wollman 
21773220a212SGleb Smirnoff 	/*
21783220a212SGleb Smirnoff 	 * RFC5961 Section 4.2
21793220a212SGleb Smirnoff 	 * Send challenge ACK for any SYN in synchronized state.
21803220a212SGleb Smirnoff 	 */
2181281a0fd4SPatrick Kelsey 	if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT &&
2182281a0fd4SPatrick Kelsey 	    tp->t_state != TCPS_SYN_RECEIVED) {
21833220a212SGleb Smirnoff 		TCPSTAT_INC(tcps_badsyn);
21843220a212SGleb Smirnoff 		if (V_tcp_insecure_syn &&
21853220a212SGleb Smirnoff 		    SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
21863220a212SGleb Smirnoff 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
21873220a212SGleb Smirnoff 			tp = tcp_drop(tp, ECONNRESET);
21883220a212SGleb Smirnoff 			rstreason = BANDLIM_UNLIMITED;
21893220a212SGleb Smirnoff 		} else {
21903220a212SGleb Smirnoff 			/* Send challenge ACK. */
21913220a212SGleb Smirnoff 			tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt,
21923220a212SGleb Smirnoff 			    tp->snd_nxt, TH_ACK);
21933220a212SGleb Smirnoff 			tp->last_ack_sent = tp->rcv_nxt;
21943220a212SGleb Smirnoff 			m = NULL;
219580ab7c0eSGarrett Wollman 		}
219680ab7c0eSGarrett Wollman 		goto drop;
219780ab7c0eSGarrett Wollman 	}
219880ab7c0eSGarrett Wollman 
219980ab7c0eSGarrett Wollman 	/*
2200df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
2201df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
2202df8bae1dSRodney W. Grimes 	 */
2203be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
220480ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
2205df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
2206d8951c8aSBjoern A. Zeeb 		if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) {
2207df8bae1dSRodney W. Grimes 			/*
2208df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
2209df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
2210df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
2211df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
2212df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
2213df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
2214df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
2215df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
2216df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
2217df8bae1dSRodney W. Grimes 			 */
2218df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
2219df8bae1dSRodney W. Grimes 		} else {
222078b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
222178b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, tlen);
222278b50714SRobert Watson 			TCPSTAT_INC(tcps_pawsdrop);
222307fd333dSMatthew Dillon 			if (tlen)
2224df8bae1dSRodney W. Grimes 				goto dropafterack;
22251ab4789dSMatthew Dillon 			goto drop;
22261ab4789dSMatthew Dillon 		}
2227df8bae1dSRodney W. Grimes 	}
2228df8bae1dSRodney W. Grimes 
2229a0292f23SGarrett Wollman 	/*
223080ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
223180ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
223280ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
223380ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
223480ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
223580ab7c0eSGarrett Wollman 	 */
2236a57815efSBosko Milekic 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
2237a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2238a57815efSBosko Milekic 		goto dropwithreset;
2239a57815efSBosko Milekic 	}
224080ab7c0eSGarrett Wollman 
2241fb59c426SYoshinobu Inoue 	todrop = tp->rcv_nxt - th->th_seq;
2242df8bae1dSRodney W. Grimes 	if (todrop > 0) {
2243831ad37eSXin LI 		if (thflags & TH_SYN) {
2244fb59c426SYoshinobu Inoue 			thflags &= ~TH_SYN;
2245fb59c426SYoshinobu Inoue 			th->th_seq++;
2246fb59c426SYoshinobu Inoue 			if (th->th_urp > 1)
2247fb59c426SYoshinobu Inoue 				th->th_urp--;
2248df8bae1dSRodney W. Grimes 			else
2249fb59c426SYoshinobu Inoue 				thflags &= ~TH_URG;
2250df8bae1dSRodney W. Grimes 			todrop--;
2251df8bae1dSRodney W. Grimes 		}
2252df8bae1dSRodney W. Grimes 		/*
2253dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
2254df8bae1dSRodney W. Grimes 		 */
2255fb59c426SYoshinobu Inoue 		if (todrop > tlen
2256fb59c426SYoshinobu Inoue 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
2257dac20301SGarrett Wollman 			/*
2258dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
2259dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
2260dac20301SGarrett Wollman 			 * of sequence; drop it.
2261dac20301SGarrett Wollman 			 */
2262fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
2263dac20301SGarrett Wollman 
2264df8bae1dSRodney W. Grimes 			/*
2265dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
2266dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
2267df8bae1dSRodney W. Grimes 			 */
2268dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
2269fb59c426SYoshinobu Inoue 			todrop = tlen;
227078b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
227178b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, todrop);
2272df8bae1dSRodney W. Grimes 		} else {
227378b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpartduppack);
227478b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop);
2275df8bae1dSRodney W. Grimes 		}
22765acfd95cSMichael Tuexen 		/*
22775acfd95cSMichael Tuexen 		 * DSACK - add SACK block for dropped range
22785acfd95cSMichael Tuexen 		 */
22798f63a52bSMichael Tuexen 		if ((todrop > 0) && (tp->t_flags & TF_SACK_PERMIT)) {
2280ecc5b1d1SMichael Tuexen 			tcp_update_sack_list(tp, th->th_seq,
2281ecc5b1d1SMichael Tuexen 			    th->th_seq + todrop);
22825acfd95cSMichael Tuexen 			/*
22835acfd95cSMichael Tuexen 			 * ACK now, as the next in-sequence segment
22845acfd95cSMichael Tuexen 			 * will clear the DSACK block again
22855acfd95cSMichael Tuexen 			 */
22865acfd95cSMichael Tuexen 			tp->t_flags |= TF_ACKNOW;
22875acfd95cSMichael Tuexen 		}
2288fb59c426SYoshinobu Inoue 		drop_hdrlen += todrop;	/* drop from the top afterwards */
2289fb59c426SYoshinobu Inoue 		th->th_seq += todrop;
2290fb59c426SYoshinobu Inoue 		tlen -= todrop;
2291fb59c426SYoshinobu Inoue 		if (th->th_urp > todrop)
2292fb59c426SYoshinobu Inoue 			th->th_urp -= todrop;
2293df8bae1dSRodney W. Grimes 		else {
2294fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;
2295fb59c426SYoshinobu Inoue 			th->th_urp = 0;
2296df8bae1dSRodney W. Grimes 		}
2297df8bae1dSRodney W. Grimes 	}
2298df8bae1dSRodney W. Grimes 
2299df8bae1dSRodney W. Grimes 	/*
2300df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
2301df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
2302df8bae1dSRodney W. Grimes 	 */
2303df8bae1dSRodney W. Grimes 	if ((so->so_state & SS_NOFDREF) &&
2304fb59c426SYoshinobu Inoue 	    tp->t_state > TCPS_CLOSE_WAIT && tlen) {
230507dacf03SAndre Oppermann 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
230607dacf03SAndre Oppermann 			log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data "
230707dacf03SAndre Oppermann 			    "after socket was closed, "
230807dacf03SAndre Oppermann 			    "sending RST and removing tcpcb\n",
2309e31d8aa3SMike Silbersack 			    s, __func__, tcpstates[tp->t_state], tlen);
2310773673c1SAndre Oppermann 			free(s, M_TCPLOG);
2311773673c1SAndre Oppermann 		}
2312df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
231378b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvafterclose);
2314a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
2315df8bae1dSRodney W. Grimes 		goto dropwithreset;
2316df8bae1dSRodney W. Grimes 	}
2317df8bae1dSRodney W. Grimes 
2318df8bae1dSRodney W. Grimes 	/*
2319df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
2320df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
2321df8bae1dSRodney W. Grimes 	 */
2322fb59c426SYoshinobu Inoue 	todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
2323df8bae1dSRodney W. Grimes 	if (todrop > 0) {
232478b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvpackafterwin);
2325fb59c426SYoshinobu Inoue 		if (todrop >= tlen) {
232678b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen);
2327df8bae1dSRodney W. Grimes 			/*
2328df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
2329df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
2330df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
2331df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
2332df8bae1dSRodney W. Grimes 			 * and ack.
2333df8bae1dSRodney W. Grimes 			 */
2334fb59c426SYoshinobu Inoue 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
2335df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
233678b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvwinprobe);
2337df8bae1dSRodney W. Grimes 			} else
2338df8bae1dSRodney W. Grimes 				goto dropafterack;
2339df8bae1dSRodney W. Grimes 		} else
234078b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2341df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
2342fb59c426SYoshinobu Inoue 		tlen -= todrop;
2343fb59c426SYoshinobu Inoue 		thflags &= ~(TH_PUSH|TH_FIN);
2344df8bae1dSRodney W. Grimes 	}
2345df8bae1dSRodney W. Grimes 
2346df8bae1dSRodney W. Grimes 	/*
2347df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
2348df8bae1dSRodney W. Grimes 	 * record its timestamp.
2349cf09195bSPaul Saab 	 * NOTE:
2350cf09195bSPaul Saab 	 * 1) That the test incorporates suggestions from the latest
2351a0292f23SGarrett Wollman 	 *    proposal of the tcplw@cray.com list (Braden 1993/04/26).
2352cf09195bSPaul Saab 	 * 2) That updating only on newer timestamps interferes with
2353cf09195bSPaul Saab 	 *    our earlier PAWS tests, so this check should be solely
2354cf09195bSPaul Saab 	 *    predicated on the sequence space of this segment.
2355cf09195bSPaul Saab 	 * 3) That we modify the segment boundary check to be
2356cf09195bSPaul Saab 	 *        Last.ACK.Sent <= SEG.SEQ + SEG.Len
2357cf09195bSPaul Saab 	 *    instead of RFC1323's
2358cf09195bSPaul Saab 	 *        Last.ACK.Sent < SEG.SEQ + SEG.Len,
2359cf09195bSPaul Saab 	 *    This modified check allows us to overcome RFC1323's
2360cf09195bSPaul Saab 	 *    limitations as described in Stevens TCP/IP Illustrated
2361cf09195bSPaul Saab 	 *    Vol. 2 p.869. In such cases, we can still calculate the
2362cf09195bSPaul Saab 	 *    RTT correctly when RCV.NXT == Last.ACK.Sent.
2363df8bae1dSRodney W. Grimes 	 */
2364be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 &&
2365cf09195bSPaul Saab 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
2366cf09195bSPaul Saab 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
2367cf09195bSPaul Saab 		((thflags & (TH_SYN|TH_FIN)) != 0))) {
2368d8951c8aSBjoern A. Zeeb 		tp->ts_recent_age = tcp_ts_getticks();
2369a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
2370df8bae1dSRodney W. Grimes 	}
2371df8bae1dSRodney W. Grimes 
2372df8bae1dSRodney W. Grimes 	/*
2373a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
2374a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
2375a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
2376a0292f23SGarrett Wollman 	 */
2377fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) == 0) {
2378a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
2379281a0fd4SPatrick Kelsey 		    (tp->t_flags & TF_NEEDSYN)) {
2380281a0fd4SPatrick Kelsey 			if (tp->t_state == TCPS_SYN_RECEIVED &&
238168bd7ed1SJonathan T. Looney 			    IS_FASTOPEN(tp->t_flags)) {
2382281a0fd4SPatrick Kelsey 				tp->snd_wnd = tiwin;
2383281a0fd4SPatrick Kelsey 				cc_conn_init(tp);
2384281a0fd4SPatrick Kelsey 			}
2385a0292f23SGarrett Wollman 			goto step6;
2386281a0fd4SPatrick Kelsey 		} else if (tp->t_flags & TF_ACKNOW)
23875e1aa279SDavid Malone 			goto dropafterack;
2388a0292f23SGarrett Wollman 		else
2389a0292f23SGarrett Wollman 			goto drop;
2390a0292f23SGarrett Wollman 	}
2391df8bae1dSRodney W. Grimes 
2392df8bae1dSRodney W. Grimes 	/*
2393df8bae1dSRodney W. Grimes 	 * Ack processing.
2394df8bae1dSRodney W. Grimes 	 */
2395df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2396df8bae1dSRodney W. Grimes 	/*
2397764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
2398764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
2399764d8cefSBill Fenner 	 * The ACK was checked above.
2400df8bae1dSRodney W. Grimes 	 */
2401df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2402a0292f23SGarrett Wollman 
240378b50714SRobert Watson 		TCPSTAT_INC(tcps_connects);
2404df8bae1dSRodney W. Grimes 		soisconnected(so);
2405df8bae1dSRodney W. Grimes 		/* Do window scaling? */
2406df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2407df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
2408df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
2409df8bae1dSRodney W. Grimes 		}
2410116ef4d6SMichael Tuexen 		tp->snd_wnd = tiwin;
2411a0292f23SGarrett Wollman 		/*
2412a0292f23SGarrett Wollman 		 * Make transitions:
2413a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
2414a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
2415a0292f23SGarrett Wollman 		 */
24169b8b58e0SJonathan Lemon 		tp->t_starttime = ticks;
241718a75309SPatrick Kelsey 		if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) {
2418281a0fd4SPatrick Kelsey 			tcp_fastopen_decrement_counter(tp->t_tfo_pending);
2419281a0fd4SPatrick Kelsey 			tp->t_tfo_pending = NULL;
2420281a0fd4SPatrick Kelsey 		}
24218db239dcSMichael Tuexen 		if (tp->t_flags & TF_NEEDFIN) {
24228db239dcSMichael Tuexen 			tcp_state_change(tp, TCPS_FIN_WAIT_1);
24238db239dcSMichael Tuexen 			tp->t_flags &= ~TF_NEEDFIN;
24248db239dcSMichael Tuexen 		} else {
24258db239dcSMichael Tuexen 			tcp_state_change(tp, TCPS_ESTABLISHED);
24268db239dcSMichael Tuexen 			TCP_PROBE5(accept__established, NULL, tp,
24278db239dcSMichael Tuexen 			    m, tp, th);
2428281a0fd4SPatrick Kelsey 			/*
2429281a0fd4SPatrick Kelsey 			 * TFO connections call cc_conn_init() during SYN
2430281a0fd4SPatrick Kelsey 			 * processing.  Calling it again here for such
2431281a0fd4SPatrick Kelsey 			 * connections is not harmless as it would undo the
2432281a0fd4SPatrick Kelsey 			 * snd_cwnd reduction that occurs when a TFO SYN|ACK
2433281a0fd4SPatrick Kelsey 			 * is retransmitted.
2434281a0fd4SPatrick Kelsey 			 */
243568bd7ed1SJonathan T. Looney 			if (!IS_FASTOPEN(tp->t_flags))
2436dbc42409SLawrence Stewart 				cc_conn_init(tp);
24379077f387SGleb Smirnoff 			tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp));
24387ff19458SPaul Traina 		}
2439a0292f23SGarrett Wollman 		/*
2440b2ade6b1SRichard Scheffenegger 		 * Account for the ACK of our SYN prior to
2441b2ade6b1SRichard Scheffenegger 		 * regular ACK processing below, except for
2442b2ade6b1SRichard Scheffenegger 		 * simultaneous SYN, which is handled later.
2443b2ade6b1SRichard Scheffenegger 		 */
2444b2ade6b1SRichard Scheffenegger 		if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN))
2445b2ade6b1SRichard Scheffenegger 			incforsyn = 1;
2446b2ade6b1SRichard Scheffenegger 		/*
2447a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
2448a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
2449a0292f23SGarrett Wollman 		 */
2450fb59c426SYoshinobu Inoue 		if (tlen == 0 && (thflags & TH_FIN) == 0)
2451c28440dbSRandall Stewart 			(void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0,
2452a0292f23SGarrett Wollman 			    (struct mbuf *)0);
245360ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq - 1;
245493b0017fSPhilippe Charnier 		/* FALLTHROUGH */
2455df8bae1dSRodney W. Grimes 
2456df8bae1dSRodney W. Grimes 	/*
2457df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
2458df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
2459fb59c426SYoshinobu Inoue 	 *	tp->snd_una < th->th_ack <= tp->snd_max
2460fb59c426SYoshinobu Inoue 	 * then advance tp->snd_una to th->th_ack and drop
2461df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
2462df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
2463df8bae1dSRodney W. Grimes 	 */
2464df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
2465df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
2466df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
2467df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
2468df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
2469df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
24705a53ca16SPaul Saab 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
247178b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvacktoomuch);
24725a53ca16SPaul Saab 			goto dropafterack;
24735a53ca16SPaul Saab 		}
24743529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
2475fc30a251SAndre Oppermann 		    ((to.to_flags & TOF_SACK) ||
2476fc30a251SAndre Oppermann 		     !TAILQ_EMPTY(&tp->snd_holes)))
2477021eaf79SHiren Panchasara 			sack_changed = tcp_sack_doack(tp, &to, th->th_ack);
247812eeb81fSHiren Panchasara 		else
247912eeb81fSHiren Panchasara 			/*
248012eeb81fSHiren Panchasara 			 * Reset the value so that previous (valid) value
248112eeb81fSHiren Panchasara 			 * from the last ack with SACK doesn't get used.
248212eeb81fSHiren Panchasara 			 */
248312eeb81fSHiren Panchasara 			tp->sackhint.sacked_bytes = 0;
248439bc9de5SLawrence Stewart 
2485bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
248639bc9de5SLawrence Stewart 		/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
248739bc9de5SLawrence Stewart 		hhook_run_tcp_est_in(tp, th, &to);
2488bd79708dSJonathan T. Looney #endif
248939bc9de5SLawrence Stewart 
2490fb59c426SYoshinobu Inoue 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
24910c39d38dSGleb Smirnoff 			u_int maxseg;
24920c39d38dSGleb Smirnoff 
24930c39d38dSGleb Smirnoff 			maxseg = tcp_maxseg(tp);
2494021eaf79SHiren Panchasara 			if (tlen == 0 &&
2495021eaf79SHiren Panchasara 			    (tiwin == tp->snd_wnd ||
2496021eaf79SHiren Panchasara 			    (tp->t_flags & TF_SACK_PERMIT))) {
249762b90589SPeter Wemm 				/*
249862b90589SPeter Wemm 				 * If this is the first time we've seen a
249962b90589SPeter Wemm 				 * FIN from the remote, this is not a
250062b90589SPeter Wemm 				 * duplicate and it needs to be processed
250162b90589SPeter Wemm 				 * normally.  This happens during a
250262b90589SPeter Wemm 				 * simultaneous close.
250362b90589SPeter Wemm 				 */
250462b90589SPeter Wemm 				if ((thflags & TH_FIN) &&
250562b90589SPeter Wemm 				    (TCPS_HAVERCVDFIN(tp->t_state) == 0)) {
250662b90589SPeter Wemm 					tp->t_dupacks = 0;
250762b90589SPeter Wemm 					break;
250862b90589SPeter Wemm 				}
250978b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvdupack);
2510df8bae1dSRodney W. Grimes 				/*
2511df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
2512df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
2513df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
25145f30ec9bSEitan Adler 				 * change and FIN isn't set),
25155f30ec9bSEitan Adler 				 * the ack is the biggest we've
2516df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
2517a4641f4eSPedro F. Giffuni 				 * threshold of them, assume a packet
2518df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
2519df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
2520df8bae1dSRodney W. Grimes 				 * window so we send only this one
2521df8bae1dSRodney W. Grimes 				 * packet.
2522df8bae1dSRodney W. Grimes 				 *
2523df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
2524df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
2525df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
2526df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
2527df8bae1dSRodney W. Grimes 				 * the new ssthresh).
2528df8bae1dSRodney W. Grimes 				 *
2529df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
2530df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
2531df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
2532df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
2533df8bae1dSRodney W. Grimes 				 * network.
2534f2512ba1SRui Paulo 				 *
2535f2512ba1SRui Paulo 				 * When using TCP ECN, notify the peer that
2536f2512ba1SRui Paulo 				 * we reduced the cwnd.
2537df8bae1dSRodney W. Grimes 				 */
2538021eaf79SHiren Panchasara 				/*
2539021eaf79SHiren Panchasara 				 * Following 2 kinds of acks should not affect
2540021eaf79SHiren Panchasara 				 * dupack counting:
2541021eaf79SHiren Panchasara 				 * 1) Old acks
2542021eaf79SHiren Panchasara 				 * 2) Acks with SACK but without any new SACK
2543021eaf79SHiren Panchasara 				 * information in them. These could result from
2544021eaf79SHiren Panchasara 				 * any anomaly in the network like a switch
2545021eaf79SHiren Panchasara 				 * duplicating packets or a possible DoS attack.
2546021eaf79SHiren Panchasara 				 */
2547021eaf79SHiren Panchasara 				if (th->th_ack != tp->snd_una ||
2548021eaf79SHiren Panchasara 				    ((tp->t_flags & TF_SACK_PERMIT) &&
2549021eaf79SHiren Panchasara 				    !sack_changed))
2550021eaf79SHiren Panchasara 					break;
2551021eaf79SHiren Panchasara 				else if (!tcp_timer_active(tp, TT_REXMT))
2552df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
2553cb942153SJeffrey Hsu 				else if (++tp->t_dupacks > tcprexmtthresh ||
2554dbc42409SLawrence Stewart 				     IN_FASTRECOVERY(tp->t_flags)) {
25554b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
25564b7b743cSLawrence Stewart 					    CC_DUPACK);
25573529149eSAndre Oppermann 					if ((tp->t_flags & TF_SACK_PERMIT) &&
2558dbc42409SLawrence Stewart 					    IN_FASTRECOVERY(tp->t_flags)) {
2559808f11b7SPaul Saab 						int awnd;
256025e6f9edSPaul Saab 
256125e6f9edSPaul Saab 						/*
256225e6f9edSPaul Saab 						 * Compute the amount of data in flight first.
25634c3972f0SHiren Panchasara 						 * We can inject new data into the pipe iff
256425e6f9edSPaul Saab 						 * we have less than 1/2 the original window's
256525e6f9edSPaul Saab 						 * worth of data in flight.
256625e6f9edSPaul Saab 						 */
256712eeb81fSHiren Panchasara 						if (V_tcp_do_rfc6675_pipe)
256812eeb81fSHiren Panchasara 							awnd = tcp_compute_pipe(tp);
256912eeb81fSHiren Panchasara 						else
2570808f11b7SPaul Saab 							awnd = (tp->snd_nxt - tp->snd_fack) +
25710077b016SPaul Saab 								tp->sackhint.sack_bytes_rexmit;
257212eeb81fSHiren Panchasara 
2573808f11b7SPaul Saab 						if (awnd < tp->snd_ssthresh) {
25740c39d38dSGleb Smirnoff 							tp->snd_cwnd += maxseg;
257525e6f9edSPaul Saab 							if (tp->snd_cwnd > tp->snd_ssthresh)
257625e6f9edSPaul Saab 								tp->snd_cwnd = tp->snd_ssthresh;
257725e6f9edSPaul Saab 						}
257825e6f9edSPaul Saab 					} else
25790c39d38dSGleb Smirnoff 						tp->snd_cwnd += maxseg;
258055bceb1eSRandall Stewart 					(void) tp->t_fb->tfb_tcp_output(tp);
258146f58482SJonathan Lemon 					goto drop;
2582cb942153SJeffrey Hsu 				} else if (tp->t_dupacks == tcprexmtthresh) {
2583cb942153SJeffrey Hsu 					tcp_seq onxt = tp->snd_nxt;
2584a0445c2eSJayanth Vijayaraghavan 
2585a0445c2eSJayanth Vijayaraghavan 					/*
2586a0445c2eSJayanth Vijayaraghavan 					 * If we're doing sack, check to
2587a0445c2eSJayanth Vijayaraghavan 					 * see if we're already in sack
2588a0445c2eSJayanth Vijayaraghavan 					 * recovery. If we're not doing sack,
2589a0445c2eSJayanth Vijayaraghavan 					 * check to see if we're in newreno
2590a0445c2eSJayanth Vijayaraghavan 					 * recovery.
2591a0445c2eSJayanth Vijayaraghavan 					 */
25923529149eSAndre Oppermann 					if (tp->t_flags & TF_SACK_PERMIT) {
2593dbc42409SLawrence Stewart 						if (IN_FASTRECOVERY(tp->t_flags)) {
2594a0445c2eSJayanth Vijayaraghavan 							tp->t_dupacks = 0;
2595a0445c2eSJayanth Vijayaraghavan 							break;
2596a0445c2eSJayanth Vijayaraghavan 						}
2597dbc42409SLawrence Stewart 					} else {
2598a0445c2eSJayanth Vijayaraghavan 						if (SEQ_LEQ(th->th_ack,
25999d11646dSJeffrey Hsu 						    tp->snd_recover)) {
2600cb942153SJeffrey Hsu 							tp->t_dupacks = 0;
2601cb942153SJeffrey Hsu 							break;
260246f58482SJonathan Lemon 						}
2603a0445c2eSJayanth Vijayaraghavan 					}
2604dbc42409SLawrence Stewart 					/* Congestion signal before ack. */
2605dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_NDUPACK);
26064b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
26074b7b743cSLawrence Stewart 					    CC_DUPACK);
2608b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
26099b8b58e0SJonathan Lemon 					tp->t_rtttime = 0;
26103529149eSAndre Oppermann 					if (tp->t_flags & TF_SACK_PERMIT) {
261178b50714SRobert Watson 						TCPSTAT_INC(
261278b50714SRobert Watson 						    tcps_sack_recovery_episode);
2613a3574665SMichael Tuexen 						tp->snd_recover = tp->snd_nxt;
26140c39d38dSGleb Smirnoff 						tp->snd_cwnd = maxseg;
261555bceb1eSRandall Stewart 						(void) tp->t_fb->tfb_tcp_output(tp);
26166d90faf3SPaul Saab 						goto drop;
26176d90faf3SPaul Saab 					}
2618fb59c426SYoshinobu Inoue 					tp->snd_nxt = th->th_ack;
26190c39d38dSGleb Smirnoff 					tp->snd_cwnd = maxseg;
262055bceb1eSRandall Stewart 					(void) tp->t_fb->tfb_tcp_output(tp);
262148d2549cSJeffrey Hsu 					KASSERT(tp->snd_limited <= 2,
2622302ce8d6SAndre Oppermann 					    ("%s: tp->snd_limited too big",
2623302ce8d6SAndre Oppermann 					    __func__));
2624df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
26250c39d38dSGleb Smirnoff 					     maxseg *
262648d2549cSJeffrey Hsu 					     (tp->t_dupacks - tp->snd_limited);
2627df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
2628df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
2629df8bae1dSRodney W. Grimes 					goto drop;
2630603724d3SBjoern A. Zeeb 				} else if (V_tcp_do_rfc3042) {
263162d4443fSHiren Panchasara 					/*
263262d4443fSHiren Panchasara 					 * Process first and second duplicate
263362d4443fSHiren Panchasara 					 * ACKs. Each indicates a segment
263462d4443fSHiren Panchasara 					 * leaving the network, creating room
263562d4443fSHiren Panchasara 					 * for more. Make sure we can send a
263662d4443fSHiren Panchasara 					 * packet on reception of each duplicate
263762d4443fSHiren Panchasara 					 * ACK by increasing snd_cwnd by one
263862d4443fSHiren Panchasara 					 * segment. Restore the original
263962d4443fSHiren Panchasara 					 * snd_cwnd after packet transmission.
264062d4443fSHiren Panchasara 					 */
26414b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
26424b7b743cSLawrence Stewart 					    CC_DUPACK);
26433ac12506SJonathan T. Looney 					uint32_t oldcwnd = tp->snd_cwnd;
264448d2549cSJeffrey Hsu 					tcp_seq oldsndmax = tp->snd_max;
264548d2549cSJeffrey Hsu 					u_int sent;
26465628dd08SAndre Oppermann 					int avail;
264789c02376SJeffrey Hsu 
2648582a954bSJeffrey Hsu 					KASSERT(tp->t_dupacks == 1 ||
2649582a954bSJeffrey Hsu 					    tp->t_dupacks == 2,
2650302ce8d6SAndre Oppermann 					    ("%s: dupacks not 1 or 2",
2651302ce8d6SAndre Oppermann 					    __func__));
265261a36e3dSJeffrey Hsu 					if (tp->t_dupacks == 1)
265348d2549cSJeffrey Hsu 						tp->snd_limited = 0;
265461a36e3dSJeffrey Hsu 					tp->snd_cwnd =
265561a36e3dSJeffrey Hsu 					    (tp->snd_nxt - tp->snd_una) +
265661a36e3dSJeffrey Hsu 					    (tp->t_dupacks - tp->snd_limited) *
26570c39d38dSGleb Smirnoff 					    maxseg;
26585628dd08SAndre Oppermann 					/*
26595628dd08SAndre Oppermann 					 * Only call tcp_output when there
26605628dd08SAndre Oppermann 					 * is new data available to be sent.
26615628dd08SAndre Oppermann 					 * Otherwise we would send pure ACKs.
26625628dd08SAndre Oppermann 					 */
26635628dd08SAndre Oppermann 					SOCKBUF_LOCK(&so->so_snd);
2664cfa6009eSGleb Smirnoff 					avail = sbavail(&so->so_snd) -
26655628dd08SAndre Oppermann 					    (tp->snd_nxt - tp->snd_una);
26665628dd08SAndre Oppermann 					SOCKBUF_UNLOCK(&so->so_snd);
26675628dd08SAndre Oppermann 					if (avail > 0)
266855bceb1eSRandall Stewart 						(void) tp->t_fb->tfb_tcp_output(tp);
266948d2549cSJeffrey Hsu 					sent = tp->snd_max - oldsndmax;
26700c39d38dSGleb Smirnoff 					if (sent > maxseg) {
267189c02376SJeffrey Hsu 						KASSERT((tp->t_dupacks == 2 &&
267289c02376SJeffrey Hsu 						    tp->snd_limited == 0) ||
26730c39d38dSGleb Smirnoff 						   (sent == maxseg + 1 &&
267489c02376SJeffrey Hsu 						    tp->t_flags & TF_SENTFIN),
2675302ce8d6SAndre Oppermann 						    ("%s: sent too much",
2676302ce8d6SAndre Oppermann 						    __func__));
267748d2549cSJeffrey Hsu 						tp->snd_limited = 2;
267848d2549cSJeffrey Hsu 					} else if (sent > 0)
267948d2549cSJeffrey Hsu 						++tp->snd_limited;
2680582a954bSJeffrey Hsu 					tp->snd_cwnd = oldcwnd;
2681582a954bSJeffrey Hsu 					goto drop;
2682df8bae1dSRodney W. Grimes 				}
2683021eaf79SHiren Panchasara 			}
2684df8bae1dSRodney W. Grimes 			break;
2685021eaf79SHiren Panchasara 		} else {
2686021eaf79SHiren Panchasara 			/*
2687021eaf79SHiren Panchasara 			 * This ack is advancing the left edge, reset the
2688021eaf79SHiren Panchasara 			 * counter.
2689021eaf79SHiren Panchasara 			 */
2690021eaf79SHiren Panchasara 			tp->t_dupacks = 0;
2691021eaf79SHiren Panchasara 			/*
2692021eaf79SHiren Panchasara 			 * If this ack also has new SACK info, increment the
2693f359d6ebSRichard Scheffenegger 			 * counter as per rfc6675. The variable
2694f359d6ebSRichard Scheffenegger 			 * sack_changed tracks all changes to the SACK
2695f359d6ebSRichard Scheffenegger 			 * scoreboard, including when partial ACKs without
2696f359d6ebSRichard Scheffenegger 			 * SACK options are received, and clear the scoreboard
2697f359d6ebSRichard Scheffenegger 			 * from the left side. Such partial ACKs should not be
2698f359d6ebSRichard Scheffenegger 			 * counted as dupacks here.
2699021eaf79SHiren Panchasara 			 */
2700f359d6ebSRichard Scheffenegger 			if ((tp->t_flags & TF_SACK_PERMIT) &&
2701f359d6ebSRichard Scheffenegger 			    (to.to_flags & TOF_SACK) &&
2702f359d6ebSRichard Scheffenegger 			    sack_changed)
2703021eaf79SHiren Panchasara 				tp->t_dupacks++;
2704df8bae1dSRodney W. Grimes 		}
2705cb942153SJeffrey Hsu 
2706302ce8d6SAndre Oppermann 		KASSERT(SEQ_GT(th->th_ack, tp->snd_una),
2707302ce8d6SAndre Oppermann 		    ("%s: th_ack <= snd_una", __func__));
2708cb942153SJeffrey Hsu 
2709df8bae1dSRodney W. Grimes 		/*
2710df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
2711df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
2712df8bae1dSRodney W. Grimes 		 */
2713dbc42409SLawrence Stewart 		if (IN_FASTRECOVERY(tp->t_flags)) {
2714cb942153SJeffrey Hsu 			if (SEQ_LT(th->th_ack, tp->snd_recover)) {
27153529149eSAndre Oppermann 				if (tp->t_flags & TF_SACK_PERMIT)
27166d90faf3SPaul Saab 					tcp_sack_partialack(tp, th);
27176d90faf3SPaul Saab 				else
2718c068736aSJeffrey Hsu 					tcp_newreno_partial_ack(tp, th);
2719dbc42409SLawrence Stewart 			} else
2720dbc42409SLawrence Stewart 				cc_post_recovery(tp, th);
272146f58482SJonathan Lemon 		}
2722a0292f23SGarrett Wollman 		/*
2723a0292f23SGarrett Wollman 		 * If we reach this point, ACK is not a duplicate,
2724a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
2725a0292f23SGarrett Wollman 		 */
2726a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
2727a0292f23SGarrett Wollman 			/*
2728a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
2729a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
273007e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
273107e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
273207e43e10SAndras Olah 			 * we can do window scaling.
2733a0292f23SGarrett Wollman 			 */
2734a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
2735a0292f23SGarrett Wollman 			tp->snd_una++;
273607e43e10SAndras Olah 			/* Do window scaling? */
273707e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
273807e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
273907e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2740464fcfbcSAndre Oppermann 				/* Send window already scaled. */
274107e43e10SAndras Olah 			}
2742a0292f23SGarrett Wollman 		}
2743a0292f23SGarrett Wollman 
2744a0292f23SGarrett Wollman process_ACK:
27458501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
27467cfc6904SRobert Watson 
2747b2ade6b1SRichard Scheffenegger 		/*
2748b2ade6b1SRichard Scheffenegger 		 * Adjust for the SYN bit in sequence space,
2749b2ade6b1SRichard Scheffenegger 		 * but don't account for it in cwnd calculations.
2750b2ade6b1SRichard Scheffenegger 		 * This is for the SYN_RECEIVED, non-simultaneous
2751b2ade6b1SRichard Scheffenegger 		 * SYN case. SYN_SENT and simultaneous SYN are
2752b2ade6b1SRichard Scheffenegger 		 * treated elsewhere.
2753b2ade6b1SRichard Scheffenegger 		 */
2754b2ade6b1SRichard Scheffenegger 		if (incforsyn)
2755b2ade6b1SRichard Scheffenegger 			tp->snd_una++;
2756dbc42409SLawrence Stewart 		acked = BYTES_THIS_ACK(tp, th);
2757b8c2cd15SJonathan T. Looney 		KASSERT(acked >= 0, ("%s: acked unexepectedly negative "
2758b8c2cd15SJonathan T. Looney 		    "(tp->snd_una=%u, th->th_ack=%u, tp=%p, m=%p)", __func__,
2759b8c2cd15SJonathan T. Looney 		    tp->snd_una, th->th_ack, tp, m));
27604b7b743cSLawrence Stewart 		TCPSTAT_ADD(tcps_rcvackpack, nsegs);
276178b50714SRobert Watson 		TCPSTAT_ADD(tcps_rcvackbyte, acked);
2762df8bae1dSRodney W. Grimes 
2763df8bae1dSRodney W. Grimes 		/*
27649b8b58e0SJonathan Lemon 		 * If we just performed our first retransmit, and the ACK
27659b8b58e0SJonathan Lemon 		 * arrives within our recovery window, then it was a mistake
27669b8b58e0SJonathan Lemon 		 * to do the retransmit in the first place.  Recover our
27679b8b58e0SJonathan Lemon 		 * original cwnd and ssthresh, and proceed to transmit where
27689b8b58e0SJonathan Lemon 		 * we left off.
27699b8b58e0SJonathan Lemon 		 */
277010d20c84SMatt Macy 		if (tp->t_rxtshift == 1 &&
277110d20c84SMatt Macy 		    tp->t_flags & TF_PREVVALID &&
277210d20c84SMatt Macy 		    tp->t_badrxtwin &&
277310d20c84SMatt Macy 		    SEQ_LT(to.to_tsecr, tp->t_badrxtwin))
2774dbc42409SLawrence Stewart 			cc_cong_signal(tp, th, CC_RTO_ERR);
27759b8b58e0SJonathan Lemon 
27769b8b58e0SJonathan Lemon 		/*
2777df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
2778df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
2779df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
2780df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
2781df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
2782df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
2783df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
2784fa55172bSMatthew Dillon 		 *
2785fa55172bSMatthew Dillon 		 * Some boxes send broken timestamp replies
2786fa55172bSMatthew Dillon 		 * during the SYN+ACK phase, ignore
2787fa55172bSMatthew Dillon 		 * timestamps of 0 or we could calculate a
2788fa55172bSMatthew Dillon 		 * huge RTT and blow up the retransmit timer.
2789df8bae1dSRodney W. Grimes 		 */
2790d8951c8aSBjoern A. Zeeb 		if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) {
27913ac12506SJonathan T. Looney 			uint32_t t;
2792d8951c8aSBjoern A. Zeeb 
2793d8951c8aSBjoern A. Zeeb 			t = tcp_ts_getticks() - to.to_tsecr;
2794d8951c8aSBjoern A. Zeeb 			if (!tp->t_rttlow || tp->t_rttlow > t)
2795d8951c8aSBjoern A. Zeeb 				tp->t_rttlow = t;
2796d8951c8aSBjoern A. Zeeb 			tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1);
2797fa55172bSMatthew Dillon 		} else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
2798eaf80179SAndre Oppermann 			if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime)
2799eaf80179SAndre Oppermann 				tp->t_rttlow = ticks - tp->t_rtttime;
28009b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
2801fa55172bSMatthew Dillon 		}
2802df8bae1dSRodney W. Grimes 
2803df8bae1dSRodney W. Grimes 		/*
2804df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
2805df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
2806df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
2807df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
2808df8bae1dSRodney W. Grimes 		 */
2809fb59c426SYoshinobu Inoue 		if (th->th_ack == tp->snd_max) {
2810b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
2811df8bae1dSRodney W. Grimes 			needoutput = 1;
2812b8152ba7SAndre Oppermann 		} else if (!tcp_timer_active(tp, TT_PERSIST))
2813b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
2814a0292f23SGarrett Wollman 
2815a0292f23SGarrett Wollman 		/*
2816a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
2817a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
2818a0292f23SGarrett Wollman 		 */
2819a0292f23SGarrett Wollman 		if (acked == 0)
2820a0292f23SGarrett Wollman 			goto step6;
2821a0292f23SGarrett Wollman 
2822df8bae1dSRodney W. Grimes 		/*
2823dbc42409SLawrence Stewart 		 * Let the congestion control algorithm update congestion
2824dbc42409SLawrence Stewart 		 * control related information. This typically means increasing
2825dbc42409SLawrence Stewart 		 * the congestion window.
2826df8bae1dSRodney W. Grimes 		 */
28274b7b743cSLawrence Stewart 		cc_ack_received(tp, th, nsegs, CC_ACK);
2828dbc42409SLawrence Stewart 
28295905999bSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
2830cfa6009eSGleb Smirnoff 		if (acked > sbavail(&so->so_snd)) {
2831b8c2cd15SJonathan T. Looney 			if (tp->snd_wnd >= sbavail(&so->so_snd))
2832cfa6009eSGleb Smirnoff 				tp->snd_wnd -= sbavail(&so->so_snd);
2833b8c2cd15SJonathan T. Looney 			else
2834b8c2cd15SJonathan T. Looney 				tp->snd_wnd = 0;
2835c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd,
2836cfa6009eSGleb Smirnoff 			    (int)sbavail(&so->so_snd));
2837df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
2838df8bae1dSRodney W. Grimes 		} else {
2839c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd, acked);
28403ac12506SJonathan T. Looney 			if (tp->snd_wnd >= (uint32_t) acked)
284160ee3bb2SAndre Oppermann 				tp->snd_wnd -= acked;
2842b8c2cd15SJonathan T. Looney 			else
2843b8c2cd15SJonathan T. Looney 				tp->snd_wnd = 0;
2844df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
2845df8bae1dSRodney W. Grimes 		}
28464d0770f1SRichard Scheffenegger 		SOCKBUF_UNLOCK(&so->so_snd);
28474d0770f1SRichard Scheffenegger 		tp->t_flags |= TF_WAKESOW;
2848c11a15bfSGleb Smirnoff 		m_freem(mfree);
2849e8949f74SAndre Oppermann 		/* Detect una wraparound. */
2850dbc42409SLawrence Stewart 		if (!IN_RECOVERY(tp->t_flags) &&
28519d11646dSJeffrey Hsu 		    SEQ_GT(tp->snd_una, tp->snd_recover) &&
28529d11646dSJeffrey Hsu 		    SEQ_LEQ(th->th_ack, tp->snd_recover))
28539d11646dSJeffrey Hsu 			tp->snd_recover = th->th_ack - 1;
2854dbc42409SLawrence Stewart 		/* XXXLAS: Can this be moved up into cc_post_recovery? */
2855dbc42409SLawrence Stewart 		if (IN_RECOVERY(tp->t_flags) &&
285624cb0f22SLawrence Stewart 		    SEQ_GEQ(th->th_ack, tp->snd_recover)) {
2857dbc42409SLawrence Stewart 			EXIT_RECOVERY(tp->t_flags);
285824cb0f22SLawrence Stewart 		}
2859fb59c426SYoshinobu Inoue 		tp->snd_una = th->th_ack;
28603529149eSAndre Oppermann 		if (tp->t_flags & TF_SACK_PERMIT) {
28616d90faf3SPaul Saab 			if (SEQ_GT(tp->snd_una, tp->snd_recover))
28626d90faf3SPaul Saab 				tp->snd_recover = tp->snd_una;
28636d90faf3SPaul Saab 		}
2864df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2865df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
2866df8bae1dSRodney W. Grimes 
2867df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2868df8bae1dSRodney W. Grimes 		/*
2869df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
2870df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
2871df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
2872df8bae1dSRodney W. Grimes 		 */
2873df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2874df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2875df8bae1dSRodney W. Grimes 				/*
2876df8bae1dSRodney W. Grimes 				 * If we can't receive any more
2877df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
2878df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
2879df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
2880df8bae1dSRodney W. Grimes 				 * we'll hang forever.
2881e8949f74SAndre Oppermann 				 *
2882e8949f74SAndre Oppermann 				 * XXXjl:
2883340c35deSJonathan Lemon 				 * we should release the tp also, and use a
2884340c35deSJonathan Lemon 				 * compressed state.
2885340c35deSJonathan Lemon 				 */
2886c0b99ffaSRobert Watson 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
288703e49181SSeigo Tanimura 					soisdisconnected(so);
28889077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
28899077f387SGleb Smirnoff 					    (tcp_fast_finwait2_recycle ?
28909077f387SGleb Smirnoff 					    tcp_finwait2_timeout :
28919077f387SGleb Smirnoff 					    TP_MAXIDLE(tp)));
28924cc20ab1SSeigo Tanimura 				}
289357f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_2);
2894df8bae1dSRodney W. Grimes 			}
2895df8bae1dSRodney W. Grimes 			break;
2896df8bae1dSRodney W. Grimes 
2897df8bae1dSRodney W. Grimes 		/*
2898df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
2899df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
2900df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
2901df8bae1dSRodney W. Grimes 		 * the segment.
2902df8bae1dSRodney W. Grimes 		 */
2903df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
2904df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
290511a20fb8SJeffrey Hsu 				tcp_twstart(tp);
2906340c35deSJonathan Lemon 				m_freem(m);
2907679d9708SAndre Oppermann 				return;
2908df8bae1dSRodney W. Grimes 			}
2909df8bae1dSRodney W. Grimes 			break;
2910df8bae1dSRodney W. Grimes 
2911df8bae1dSRodney W. Grimes 		/*
2912df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
2913df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
2914df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
2915df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
2916df8bae1dSRodney W. Grimes 		 */
2917df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
2918df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2919df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
2920df8bae1dSRodney W. Grimes 				goto drop;
2921df8bae1dSRodney W. Grimes 			}
2922df8bae1dSRodney W. Grimes 			break;
2923df8bae1dSRodney W. Grimes 		}
2924df8bae1dSRodney W. Grimes 	}
2925df8bae1dSRodney W. Grimes 
2926df8bae1dSRodney W. Grimes step6:
29278501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
29287cfc6904SRobert Watson 
2929df8bae1dSRodney W. Grimes 	/*
293060ee3bb2SAndre Oppermann 	 * Update window information.
293160ee3bb2SAndre Oppermann 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2932df8bae1dSRodney W. Grimes 	 */
293360ee3bb2SAndre Oppermann 	if ((thflags & TH_ACK) &&
293460ee3bb2SAndre Oppermann 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
293560ee3bb2SAndre Oppermann 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
293660ee3bb2SAndre Oppermann 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
293760ee3bb2SAndre Oppermann 		/* keep track of pure window updates */
293860ee3bb2SAndre Oppermann 		if (tlen == 0 &&
293960ee3bb2SAndre Oppermann 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
294078b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvwinupd);
2941df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
294260ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq;
294360ee3bb2SAndre Oppermann 		tp->snd_wl2 = th->th_ack;
2944df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
2945df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
294660ee3bb2SAndre Oppermann 		needoutput = 1;
2947df8bae1dSRodney W. Grimes 	}
2948df8bae1dSRodney W. Grimes 
2949df8bae1dSRodney W. Grimes 	/*
2950df8bae1dSRodney W. Grimes 	 * Process segments with URG.
2951df8bae1dSRodney W. Grimes 	 */
2952fb59c426SYoshinobu Inoue 	if ((thflags & TH_URG) && th->th_urp &&
2953df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2954df8bae1dSRodney W. Grimes 		/*
2955df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
2956df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
2957df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
2958df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
2959df8bae1dSRodney W. Grimes 		 */
296018ad5842SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
2961cfa6009eSGleb Smirnoff 		if (th->th_urp + sbavail(&so->so_rcv) > sb_max) {
2962fb59c426SYoshinobu Inoue 			th->th_urp = 0;			/* XXX */
2963fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;		/* XXX */
296418ad5842SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);	/* XXX */
2965df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
2966df8bae1dSRodney W. Grimes 		}
2967df8bae1dSRodney W. Grimes 		/*
2968df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
2969df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
2970df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2971df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
2972df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
2973df8bae1dSRodney W. Grimes 		 *
2974df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
2975df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
2976df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
2977df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
2978df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
2979df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
2980df8bae1dSRodney W. Grimes 		 */
2981fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
2982fb59c426SYoshinobu Inoue 			tp->rcv_up = th->th_seq + th->th_urp;
2983cfa6009eSGleb Smirnoff 			so->so_oobmark = sbavail(&so->so_rcv) +
2984df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
2985927c5ceaSRobert Watson 			if (so->so_oobmark == 0)
2986c0b99ffaSRobert Watson 				so->so_rcv.sb_state |= SBS_RCVATMARK;
2987df8bae1dSRodney W. Grimes 			sohasoutofband(so);
2988df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2989df8bae1dSRodney W. Grimes 		}
299018ad5842SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
2991df8bae1dSRodney W. Grimes 		/*
2992df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
2993df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
2994df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
2995df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
2996df8bae1dSRodney W. Grimes 		 */
29973ac12506SJonathan T. Looney 		if (th->th_urp <= (uint32_t)tlen &&
299830613f56SJeffrey Hsu 		    !(so->so_options & SO_OOBINLINE)) {
299930613f56SJeffrey Hsu 			/* hdr drop is delayed */
300030613f56SJeffrey Hsu 			tcp_pulloutofband(so, th, m, drop_hdrlen);
300130613f56SJeffrey Hsu 		}
3002c068736aSJeffrey Hsu 	} else {
3003df8bae1dSRodney W. Grimes 		/*
3004df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
3005df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
3006df8bae1dSRodney W. Grimes 		 * with the receive window.
3007df8bae1dSRodney W. Grimes 		 */
3008df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
3009df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
3010c068736aSJeffrey Hsu 	}
3011df8bae1dSRodney W. Grimes dodata:							/* XXX */
30128501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
30137cfc6904SRobert Watson 
3014df8bae1dSRodney W. Grimes 	/*
3015df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
3016df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
3017df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
3018df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
3019df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
3020df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
3021df8bae1dSRodney W. Grimes 	 */
3022281a0fd4SPatrick Kelsey 	tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
302368bd7ed1SJonathan T. Looney 		   IS_FASTOPEN(tp->t_flags));
3024f1ea4e41SRandall Stewart 	if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) &&
3025df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
302669e03620SPaul Saab 		tcp_seq save_start = th->th_seq;
30275acfd95cSMichael Tuexen 		tcp_seq save_rnxt  = tp->rcv_nxt;
30285acfd95cSMichael Tuexen 		int     save_tlen  = tlen;
3029fb59c426SYoshinobu Inoue 		m_adj(m, drop_hdrlen);	/* delayed header drop */
3030e4b64281SJesper Skriver 		/*
3031c068736aSJeffrey Hsu 		 * Insert segment which includes th into TCP reassembly queue
3032c068736aSJeffrey Hsu 		 * with control block tp.  Set thflags to whether reassembly now
3033c068736aSJeffrey Hsu 		 * includes a segment with FIN.  This handles the common case
3034c068736aSJeffrey Hsu 		 * inline (segment is the next to be received on an established
3035c068736aSJeffrey Hsu 		 * connection, and the queue is empty), avoiding linkage into
3036c068736aSJeffrey Hsu 		 * and removal from the queue and repetition of various
3037c068736aSJeffrey Hsu 		 * conversions.
3038c068736aSJeffrey Hsu 		 * Set DELACK for segments received in order, but ack
3039c068736aSJeffrey Hsu 		 * immediately when segments are out of order (so
3040c068736aSJeffrey Hsu 		 * fast retransmit can work).
3041e4b64281SJesper Skriver 		 */
30424741bfcbSPatrick Kelsey 		if (th->th_seq == tp->rcv_nxt &&
3043c28440dbSRandall Stewart 		    SEGQ_EMPTY(tp) &&
3044281a0fd4SPatrick Kelsey 		    (TCPS_HAVEESTABLISHED(tp->t_state) ||
3045281a0fd4SPatrick Kelsey 		     tfo_syn)) {
3046281a0fd4SPatrick Kelsey 			if (DELAY_ACK(tp, tlen) || tfo_syn)
30473bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
3048e4b64281SJesper Skriver 			else
3049e4b64281SJesper Skriver 				tp->t_flags |= TF_ACKNOW;
3050e4b64281SJesper Skriver 			tp->rcv_nxt += tlen;
3051e854dd38SRandall Stewart 			if (tlen &&
3052e854dd38SRandall Stewart 			    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
3053e854dd38SRandall Stewart 			    (tp->t_fbyte_in == 0)) {
3054e854dd38SRandall Stewart 				tp->t_fbyte_in = ticks;
3055e854dd38SRandall Stewart 				if (tp->t_fbyte_in == 0)
3056e854dd38SRandall Stewart 					tp->t_fbyte_in = 1;
3057e854dd38SRandall Stewart 				if (tp->t_fbyte_out && tp->t_fbyte_in)
3058e854dd38SRandall Stewart 					tp->t_flags2 |= TF2_FBYTES_COMPLETE;
3059e854dd38SRandall Stewart 			}
3060e4b64281SJesper Skriver 			thflags = th->th_flags & TH_FIN;
306178b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpack);
306278b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
30631e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
3064c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
3065c1c36a2cSMike Silbersack 				m_freem(m);
3066c1c36a2cSMike Silbersack 			else
3067651e4e6aSGleb Smirnoff 				sbappendstream_locked(&so->so_rcv, m, 0);
30684d0770f1SRichard Scheffenegger 			SOCKBUF_UNLOCK(&so->so_rcv);
30694d0770f1SRichard Scheffenegger 			tp->t_flags |= TF_WAKESOR;
3070e4b64281SJesper Skriver 		} else {
3071e8949f74SAndre Oppermann 			/*
3072e8949f74SAndre Oppermann 			 * XXX: Due to the header drop above "th" is
3073e8949f74SAndre Oppermann 			 * theoretically invalid by now.  Fortunately
3074e8949f74SAndre Oppermann 			 * m_adj() doesn't actually frees any mbufs
3075e8949f74SAndre Oppermann 			 * when trimming from the head.
3076e8949f74SAndre Oppermann 			 */
30775acfd95cSMichael Tuexen 			tcp_seq temp = save_start;
30785acfd95cSMichael Tuexen 			thflags = tcp_reass(tp, th, &temp, &tlen, m);
3079e4b64281SJesper Skriver 			tp->t_flags |= TF_ACKNOW;
3080e4b64281SJesper Skriver 		}
30816d261981SMichael Tuexen 		if ((tp->t_flags & TF_SACK_PERMIT) && (save_tlen > 0)) {
30826d261981SMichael Tuexen 			if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) {
3083b5a154d8SMichael Tuexen 				/*
3084b5a154d8SMichael Tuexen 				 * DSACK actually handled in the fastpath
3085b5a154d8SMichael Tuexen 				 * above.
3086b5a154d8SMichael Tuexen 				 */
3087ecc5b1d1SMichael Tuexen 				tcp_update_sack_list(tp, save_start,
3088ecc5b1d1SMichael Tuexen 				    save_start + save_tlen);
3089ecc5b1d1SMichael Tuexen 			} else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) {
3090ecc5b1d1SMichael Tuexen 				if ((tp->rcv_numsacks >= 1) &&
3091ecc5b1d1SMichael Tuexen 				    (tp->sackblks[0].end == save_start)) {
30926d261981SMichael Tuexen 					/*
30936d261981SMichael Tuexen 					 * Partial overlap, recorded at todrop
30946d261981SMichael Tuexen 					 * above.
30956d261981SMichael Tuexen 					 */
30966d261981SMichael Tuexen 					tcp_update_sack_list(tp,
30976d261981SMichael Tuexen 					    tp->sackblks[0].start,
3098ecc5b1d1SMichael Tuexen 					    tp->sackblks[0].end);
3099ecc5b1d1SMichael Tuexen 				} else {
3100ecc5b1d1SMichael Tuexen 					tcp_update_dsack_list(tp, save_start,
3101ecc5b1d1SMichael Tuexen 					    save_start + save_tlen);
3102ecc5b1d1SMichael Tuexen 				}
31036d261981SMichael Tuexen 			} else if (tlen >= save_tlen) {
3104b5a154d8SMichael Tuexen 				/* Update of sackblks. */
3105ecc5b1d1SMichael Tuexen 				tcp_update_dsack_list(tp, save_start,
3106ecc5b1d1SMichael Tuexen 				    save_start + save_tlen);
3107ecc5b1d1SMichael Tuexen 			} else if (tlen > 0) {
3108ecc5b1d1SMichael Tuexen 				tcp_update_dsack_list(tp, save_start,
3109ecc5b1d1SMichael Tuexen 				    save_start + tlen);
31105acfd95cSMichael Tuexen 			}
31115acfd95cSMichael Tuexen 		}
3112302ce8d6SAndre Oppermann #if 0
3113df8bae1dSRodney W. Grimes 		/*
3114df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
3115df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
3116df8bae1dSRodney W. Grimes 		 * buffer size.
3117302ce8d6SAndre Oppermann 		 * XXX: Unused.
3118df8bae1dSRodney W. Grimes 		 */
3119f701e30dSJohn Baldwin 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt))
3120df8bae1dSRodney W. Grimes 			len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
3121f701e30dSJohn Baldwin 		else
3122f701e30dSJohn Baldwin 			len = so->so_rcv.sb_hiwat;
3123302ce8d6SAndre Oppermann #endif
3124df8bae1dSRodney W. Grimes 	} else {
3125df8bae1dSRodney W. Grimes 		m_freem(m);
3126fb59c426SYoshinobu Inoue 		thflags &= ~TH_FIN;
3127df8bae1dSRodney W. Grimes 	}
3128df8bae1dSRodney W. Grimes 
3129df8bae1dSRodney W. Grimes 	/*
3130df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
3131df8bae1dSRodney W. Grimes 	 * that the connection is closing.
3132df8bae1dSRodney W. Grimes 	 */
3133fb59c426SYoshinobu Inoue 	if (thflags & TH_FIN) {
3134df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
3135df8bae1dSRodney W. Grimes 			socantrcvmore(so);
31364d0770f1SRichard Scheffenegger 			/* The socket upcall is handled by socantrcvmore. */
31374d0770f1SRichard Scheffenegger 			tp->t_flags &= ~TF_WAKESOR;
3138a0292f23SGarrett Wollman 			/*
3139a0292f23SGarrett Wollman 			 * If connection is half-synchronized
3140d3eede9dSAndras Olah 			 * (ie NEEDSYN flag on) then delay ACK,
3141a0292f23SGarrett Wollman 			 * so it may be piggybacked when SYN is sent.
3142a0292f23SGarrett Wollman 			 * Otherwise, since we received a FIN then no
3143a0292f23SGarrett Wollman 			 * more input can be expected, send ACK now.
3144a0292f23SGarrett Wollman 			 */
31453bfd6421SJonathan Lemon 			if (tp->t_flags & TF_NEEDSYN)
31463bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
3147a0292f23SGarrett Wollman 			else
3148df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
3149df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
3150df8bae1dSRodney W. Grimes 		}
3151df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
3152df8bae1dSRodney W. Grimes 		/*
3153df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
3154df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
3155df8bae1dSRodney W. Grimes 		 */
3156df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
31579b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
31589b8b58e0SJonathan Lemon 			/* FALLTHROUGH */
3159df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
316057f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSE_WAIT);
3161df8bae1dSRodney W. Grimes 			break;
3162df8bae1dSRodney W. Grimes 
3163df8bae1dSRodney W. Grimes 		/*
3164df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
3165df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
3166df8bae1dSRodney W. Grimes 		 */
3167df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
316857f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSING);
3169df8bae1dSRodney W. Grimes 			break;
3170df8bae1dSRodney W. Grimes 
3171df8bae1dSRodney W. Grimes 		/*
3172df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
3173df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
3174df8bae1dSRodney W. Grimes 		 * standard timers.
3175df8bae1dSRodney W. Grimes 		 */
3176df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
3177340c35deSJonathan Lemon 			tcp_twstart(tp);
3178679d9708SAndre Oppermann 			return;
3179df8bae1dSRodney W. Grimes 		}
3180df8bae1dSRodney W. Grimes 	}
3181610ee2f9SDavid Greenman #ifdef TCPDEBUG
31824cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
3183fb59c426SYoshinobu Inoue 		tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
3184fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3185610ee2f9SDavid Greenman #endif
31862b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
3187df8bae1dSRodney W. Grimes 
3188df8bae1dSRodney W. Grimes 	/*
3189df8bae1dSRodney W. Grimes 	 * Return any desired output.
3190df8bae1dSRodney W. Grimes 	 */
3191df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
319255bceb1eSRandall Stewart 		(void) tp->t_fb->tfb_tcp_output(tp);
31937792ea27SJeffrey Hsu 
3194a14c749fSJonathan Lemon check_delack:
31958501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
3196252ca428SRobert Watson 
3197a14c749fSJonathan Lemon 	if (tp->t_flags & TF_DELACK) {
31983bfd6421SJonathan Lemon 		tp->t_flags &= ~TF_DELACK;
3199b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
32003bfd6421SJonathan Lemon 	}
32014d0770f1SRichard Scheffenegger 	tcp_handle_wakeup(tp, so);
32028501a69cSRobert Watson 	INP_WUNLOCK(tp->t_inpcb);
3203679d9708SAndre Oppermann 	return;
3204df8bae1dSRodney W. Grimes 
3205df8bae1dSRodney W. Grimes dropafterack:
3206df8bae1dSRodney W. Grimes 	/*
3207df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
3208df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
320980ab7c0eSGarrett Wollman 	 *
321080ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
321180ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
321280ab7c0eSGarrett Wollman 	 * RST have been dropped.
321380ab7c0eSGarrett Wollman 	 *
321480ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
321580ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
321680ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
321780ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
321880ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
321980ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
3220df8bae1dSRodney W. Grimes 	 */
3221fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
3222fb59c426SYoshinobu Inoue 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
3223a57815efSBosko Milekic 	     SEQ_GT(th->th_ack, tp->snd_max)) ) {
3224a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
3225a57815efSBosko Milekic 		goto dropwithreset;
3226a57815efSBosko Milekic 	}
3227a0292f23SGarrett Wollman #ifdef TCPDEBUG
32284cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
3229fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
3230fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3231a0292f23SGarrett Wollman #endif
32322b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
3233df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
323455bceb1eSRandall Stewart 	(void) tp->t_fb->tfb_tcp_output(tp);
32354d0770f1SRichard Scheffenegger 	tcp_handle_wakeup(tp, so);
32368501a69cSRobert Watson 	INP_WUNLOCK(tp->t_inpcb);
3237d6915262SRobert Watson 	m_freem(m);
3238679d9708SAndre Oppermann 	return;
3239df8bae1dSRodney W. Grimes 
3240df8bae1dSRodney W. Grimes dropwithreset:
3241a0ca0871SRobert Watson 	if (tp != NULL) {
3242302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
32434d0770f1SRichard Scheffenegger 		tcp_handle_wakeup(tp, so);
32448501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
3245dd8ac7f9SRobert Watson 	} else
3246a0ca0871SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
3247679d9708SAndre Oppermann 	return;
3248df8bae1dSRodney W. Grimes 
3249df8bae1dSRodney W. Grimes drop:
3250df8bae1dSRodney W. Grimes 	/*
3251df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
3252df8bae1dSRodney W. Grimes 	 */
3253610ee2f9SDavid Greenman #ifdef TCPDEBUG
32541c53f806SRobert Watson 	if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
3255fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
3256fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3257a0292f23SGarrett Wollman #endif
32582b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
32594d0770f1SRichard Scheffenegger 	if (tp != NULL) {
32604d0770f1SRichard Scheffenegger 		tcp_handle_wakeup(tp, so);
32618501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
32624d0770f1SRichard Scheffenegger 	}
3263d6915262SRobert Watson 	m_freem(m);
3264302ce8d6SAndre Oppermann }
3265302ce8d6SAndre Oppermann 
3266302ce8d6SAndre Oppermann /*
32670ca3f933SAndre Oppermann  * Issue RST and make ACK acceptable to originator of segment.
32680ca3f933SAndre Oppermann  * The mbuf must still include the original packet header.
32690ca3f933SAndre Oppermann  * tp may be NULL.
3270302ce8d6SAndre Oppermann  */
327155bceb1eSRandall Stewart void
3272302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp,
3273302ce8d6SAndre Oppermann     int tlen, int rstreason)
3274302ce8d6SAndre Oppermann {
3275b287c6c7SBjoern A. Zeeb #ifdef INET
3276302ce8d6SAndre Oppermann 	struct ip *ip;
3277b287c6c7SBjoern A. Zeeb #endif
3278302ce8d6SAndre Oppermann #ifdef INET6
3279302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6;
3280302ce8d6SAndre Oppermann #endif
32817a3244ccSRobert Watson 
32827a3244ccSRobert Watson 	if (tp != NULL) {
32838501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
32847a3244ccSRobert Watson 	}
32857a3244ccSRobert Watson 
32860ca3f933SAndre Oppermann 	/* Don't bother if destination was broadcast/multicast. */
3287302ce8d6SAndre Oppermann 	if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
3288302ce8d6SAndre Oppermann 		goto drop;
3289302ce8d6SAndre Oppermann #ifdef INET6
3290302ce8d6SAndre Oppermann 	if (mtod(m, struct ip *)->ip_v == 6) {
3291302ce8d6SAndre Oppermann 		ip6 = mtod(m, struct ip6_hdr *);
3292302ce8d6SAndre Oppermann 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
3293302ce8d6SAndre Oppermann 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
3294302ce8d6SAndre Oppermann 			goto drop;
3295302ce8d6SAndre Oppermann 		/* IPv6 anycast check is done at tcp6_input() */
3296b287c6c7SBjoern A. Zeeb 	}
3297302ce8d6SAndre Oppermann #endif
3298b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3299b287c6c7SBjoern A. Zeeb 	else
3300b287c6c7SBjoern A. Zeeb #endif
3301b287c6c7SBjoern A. Zeeb #ifdef INET
3302302ce8d6SAndre Oppermann 	{
3303302ce8d6SAndre Oppermann 		ip = mtod(m, struct ip *);
3304302ce8d6SAndre Oppermann 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
3305302ce8d6SAndre Oppermann 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
3306302ce8d6SAndre Oppermann 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
3307302ce8d6SAndre Oppermann 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
3308302ce8d6SAndre Oppermann 			goto drop;
3309302ce8d6SAndre Oppermann 	}
3310b287c6c7SBjoern A. Zeeb #endif
3311302ce8d6SAndre Oppermann 
3312302ce8d6SAndre Oppermann 	/* Perform bandwidth limiting. */
3313302ce8d6SAndre Oppermann 	if (badport_bandlim(rstreason) < 0)
3314302ce8d6SAndre Oppermann 		goto drop;
3315302ce8d6SAndre Oppermann 
3316302ce8d6SAndre Oppermann 	/* tcp_respond consumes the mbuf chain. */
3317302ce8d6SAndre Oppermann 	if (th->th_flags & TH_ACK) {
3318302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0,
3319302ce8d6SAndre Oppermann 		    th->th_ack, TH_RST);
3320302ce8d6SAndre Oppermann 	} else {
3321302ce8d6SAndre Oppermann 		if (th->th_flags & TH_SYN)
3322302ce8d6SAndre Oppermann 			tlen++;
33234ddd5aadSMichael Tuexen 		if (th->th_flags & TH_FIN)
33244ddd5aadSMichael Tuexen 			tlen++;
3325302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
3326302ce8d6SAndre Oppermann 		    (tcp_seq)0, TH_RST|TH_ACK);
3327302ce8d6SAndre Oppermann 	}
3328302ce8d6SAndre Oppermann 	return;
3329302ce8d6SAndre Oppermann drop:
3330302ce8d6SAndre Oppermann 	m_freem(m);
3331df8bae1dSRodney W. Grimes }
3332df8bae1dSRodney W. Grimes 
3333be2ac88cSJonathan Lemon /*
3334be2ac88cSJonathan Lemon  * Parse TCP options and place in tcpopt.
3335be2ac88cSJonathan Lemon  */
333655bceb1eSRandall Stewart void
3337ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags)
3338df8bae1dSRodney W. Grimes {
3339df8bae1dSRodney W. Grimes 	int opt, optlen;
3340df8bae1dSRodney W. Grimes 
3341be2ac88cSJonathan Lemon 	to->to_flags = 0;
3342df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
3343df8bae1dSRodney W. Grimes 		opt = cp[0];
3344df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
3345df8bae1dSRodney W. Grimes 			break;
3346df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
3347df8bae1dSRodney W. Grimes 			optlen = 1;
3348df8bae1dSRodney W. Grimes 		else {
3349b474779fSJun-ichiro itojun Hagino 			if (cnt < 2)
3350b474779fSJun-ichiro itojun Hagino 				break;
3351df8bae1dSRodney W. Grimes 			optlen = cp[1];
3352b474779fSJun-ichiro itojun Hagino 			if (optlen < 2 || optlen > cnt)
3353df8bae1dSRodney W. Grimes 				break;
3354df8bae1dSRodney W. Grimes 		}
3355df8bae1dSRodney W. Grimes 		switch (opt) {
3356df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
3357df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
3358df8bae1dSRodney W. Grimes 				continue;
3359f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3360df8bae1dSRodney W. Grimes 				continue;
3361be2ac88cSJonathan Lemon 			to->to_flags |= TOF_MSS;
3362be2ac88cSJonathan Lemon 			bcopy((char *)cp + 2,
3363be2ac88cSJonathan Lemon 			    (char *)&to->to_mss, sizeof(to->to_mss));
3364fd8e4ebcSMike Barcroft 			to->to_mss = ntohs(to->to_mss);
3365df8bae1dSRodney W. Grimes 			break;
3366df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
3367df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
3368df8bae1dSRodney W. Grimes 				continue;
3369f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3370df8bae1dSRodney W. Grimes 				continue;
3371be2ac88cSJonathan Lemon 			to->to_flags |= TOF_SCALE;
337202a1a643SAndre Oppermann 			to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT);
3373df8bae1dSRodney W. Grimes 			break;
3374df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
3375df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
3376df8bae1dSRodney W. Grimes 				continue;
3377be2ac88cSJonathan Lemon 			to->to_flags |= TOF_TS;
3378a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
3379a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
3380fd8e4ebcSMike Barcroft 			to->to_tsval = ntohl(to->to_tsval);
3381a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
3382a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
3383fd8e4ebcSMike Barcroft 			to->to_tsecr = ntohl(to->to_tsecr);
3384df8bae1dSRodney W. Grimes 			break;
33851cfd4b53SBruce M Simpson 		case TCPOPT_SIGNATURE:
3386fcf59617SAndrey V. Elsukov 			/*
3387fcf59617SAndrey V. Elsukov 			 * In order to reply to a host which has set the
3388fcf59617SAndrey V. Elsukov 			 * TCP_SIGNATURE option in its initial SYN, we have
3389fcf59617SAndrey V. Elsukov 			 * to record the fact that the option was observed
3390fcf59617SAndrey V. Elsukov 			 * here for the syncache code to perform the correct
3391fcf59617SAndrey V. Elsukov 			 * response.
3392fcf59617SAndrey V. Elsukov 			 */
33931cfd4b53SBruce M Simpson 			if (optlen != TCPOLEN_SIGNATURE)
33941cfd4b53SBruce M Simpson 				continue;
3395df47e437SAndre Oppermann 			to->to_flags |= TOF_SIGNATURE;
3396df47e437SAndre Oppermann 			to->to_signature = cp + 2;
33971cfd4b53SBruce M Simpson 			break;
33986d90faf3SPaul Saab 		case TCPOPT_SACK_PERMITTED:
3399f72167f4SAndre Oppermann 			if (optlen != TCPOLEN_SACK_PERMITTED)
34006d90faf3SPaul Saab 				continue;
3401f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3402f72167f4SAndre Oppermann 				continue;
3403603724d3SBjoern A. Zeeb 			if (!V_tcp_do_sack)
3404f72167f4SAndre Oppermann 				continue;
3405fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACKPERM;
34066d90faf3SPaul Saab 			break;
34076d90faf3SPaul Saab 		case TCPOPT_SACK:
34085a53ca16SPaul Saab 			if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
34096d90faf3SPaul Saab 				continue;
3410b728e902SAndre Oppermann 			if (flags & TO_SYN)
3411b728e902SAndre Oppermann 				continue;
3412fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACK;
34135a53ca16SPaul Saab 			to->to_nsacks = (optlen - 2) / TCPOLEN_SACK;
34145a53ca16SPaul Saab 			to->to_sacks = cp + 2;
341578b50714SRobert Watson 			TCPSTAT_INC(tcps_sack_rcv_blocks);
34166d90faf3SPaul Saab 			break;
3417281a0fd4SPatrick Kelsey 		case TCPOPT_FAST_OPEN:
3418c560df6fSPatrick Kelsey 			/*
3419c560df6fSPatrick Kelsey 			 * Cookie length validation is performed by the
3420c560df6fSPatrick Kelsey 			 * server side cookie checking code or the client
3421c560df6fSPatrick Kelsey 			 * side cookie cache update code.
3422c560df6fSPatrick Kelsey 			 */
3423281a0fd4SPatrick Kelsey 			if (!(flags & TO_SYN))
3424281a0fd4SPatrick Kelsey 				continue;
3425c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
3426c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
3427281a0fd4SPatrick Kelsey 				continue;
3428281a0fd4SPatrick Kelsey 			to->to_flags |= TOF_FASTOPEN;
3429281a0fd4SPatrick Kelsey 			to->to_tfo_len = optlen - 2;
3430281a0fd4SPatrick Kelsey 			to->to_tfo_cookie = to->to_tfo_len ? cp + 2 : NULL;
3431281a0fd4SPatrick Kelsey 			break;
3432be2ac88cSJonathan Lemon 		default:
3433be2ac88cSJonathan Lemon 			continue;
3434df8bae1dSRodney W. Grimes 		}
3435df8bae1dSRodney W. Grimes 	}
3436df8bae1dSRodney W. Grimes }
3437df8bae1dSRodney W. Grimes 
3438df8bae1dSRodney W. Grimes /*
3439df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
3440df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
3441df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
3442df8bae1dSRodney W. Grimes  * sequencing purposes.
3443df8bae1dSRodney W. Grimes  */
344455bceb1eSRandall Stewart void
3445ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m,
3446ad3f9ab3SAndre Oppermann     int off)
3447df8bae1dSRodney W. Grimes {
3448fb59c426SYoshinobu Inoue 	int cnt = off + th->th_urp - 1;
3449df8bae1dSRodney W. Grimes 
3450df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
3451df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
3452df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
3453df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
3454df8bae1dSRodney W. Grimes 
34558501a69cSRobert Watson 			INP_WLOCK_ASSERT(tp->t_inpcb);
34567a3244ccSRobert Watson 
3457df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
3458df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
3459df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
3460df8bae1dSRodney W. Grimes 			m->m_len--;
346169a34685SYoshinobu Inoue 			if (m->m_flags & M_PKTHDR)
346269a34685SYoshinobu Inoue 				m->m_pkthdr.len--;
3463df8bae1dSRodney W. Grimes 			return;
3464df8bae1dSRodney W. Grimes 		}
3465df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
3466df8bae1dSRodney W. Grimes 		m = m->m_next;
34674dfdffe9SAndre Oppermann 		if (m == NULL)
3468df8bae1dSRodney W. Grimes 			break;
3469df8bae1dSRodney W. Grimes 	}
3470df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
3471df8bae1dSRodney W. Grimes }
3472df8bae1dSRodney W. Grimes 
3473df8bae1dSRodney W. Grimes /*
3474df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
3475df8bae1dSRodney W. Grimes  * and update averages and current timeout.
3476df8bae1dSRodney W. Grimes  */
347755bceb1eSRandall Stewart void
3478ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt)
3479df8bae1dSRodney W. Grimes {
3480ad3f9ab3SAndre Oppermann 	int delta;
3481233e8c18SGarrett Wollman 
34828501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
34832be3bf22SRobert Watson 
348478b50714SRobert Watson 	TCPSTAT_INC(tcps_rttupdated);
3485233e8c18SGarrett Wollman 	tp->t_rttupdated++;
3486adc56f5aSEdward Tomasz Napierala #ifdef STATS
3487adc56f5aSEdward Tomasz Napierala 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT,
3488adc56f5aSEdward Tomasz Napierala 	    imax(0, rtt * 1000 / hz));
3489adc56f5aSEdward Tomasz Napierala #endif
34905ede40dcSRyan Stone 	if ((tp->t_srtt != 0) && (tp->t_rxtshift <= TCP_RTT_INVALIDATE)) {
3491233e8c18SGarrett Wollman 		/*
3492233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
3493233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
3494233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
3495233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
3496233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
3497233e8c18SGarrett Wollman 		 */
3498233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
3499233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
3500233e8c18SGarrett Wollman 
3501233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
3502233e8c18SGarrett Wollman 			tp->t_srtt = 1;
3503233e8c18SGarrett Wollman 
3504233e8c18SGarrett Wollman 		/*
3505233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
3506233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
3507233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
3508233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
3509233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
3510233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
3511233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
3512233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
3513233e8c18SGarrett Wollman 		 */
3514233e8c18SGarrett Wollman 		if (delta < 0)
3515233e8c18SGarrett Wollman 			delta = -delta;
3516233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
3517233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
3518233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
35191fcc99b5SMatthew Dillon 		if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
35201fcc99b5SMatthew Dillon 		    tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3521233e8c18SGarrett Wollman 	} else {
3522233e8c18SGarrett Wollman 		/*
3523233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
3524233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
3525233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
3526233e8c18SGarrett Wollman 		 */
3527233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
3528233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
35291fcc99b5SMatthew Dillon 		tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3530233e8c18SGarrett Wollman 	}
35319b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
3532df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
3533df8bae1dSRodney W. Grimes 
3534df8bae1dSRodney W. Grimes 	/*
3535df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
3536df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
3537df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
3538df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
3539df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
3540df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
3541df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
3542df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
3543df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
3544df8bae1dSRodney W. Grimes 	 */
3545233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
35469e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
3547df8bae1dSRodney W. Grimes 
3548df8bae1dSRodney W. Grimes 	/*
3549df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
3550df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
3551df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
3552df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
3553df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
3554df8bae1dSRodney W. Grimes 	 */
3555df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
3556df8bae1dSRodney W. Grimes }
3557df8bae1dSRodney W. Grimes 
3558df8bae1dSRodney W. Grimes /*
3559df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
3560df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
35614faaea55SAndre Oppermann  * If none, use an mss that can be handled on the outgoing interface
35624faaea55SAndre Oppermann  * without forcing IP to fragment.  If no route is found, route has no mtu,
3563df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
3564df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
3565df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
3566df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
3567df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
3568df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
3569df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
3570a0292f23SGarrett Wollman  *
35710c39d38dSGleb Smirnoff  * NOTE that resulting t_maxseg doesn't include space for TCP options or
35720c39d38dSGleb Smirnoff  * IP options, e.g. IPSEC data, since length of this data may vary, and
35730c39d38dSGleb Smirnoff  * thus it is calculated for every segment separately in tcp_output().
3574a0292f23SGarrett Wollman  *
357597d8d152SAndre Oppermann  * NOTE that this routine is only called when we process an incoming
3576ef341ee1SGleb Smirnoff  * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS
3577ef341ee1SGleb Smirnoff  * settings are handled in tcp_mssopt().
3578df8bae1dSRodney W. Grimes  */
3579a0292f23SGarrett Wollman void
3580ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer,
35813c914c54SAndre Oppermann     struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap)
3582df8bae1dSRodney W. Grimes {
3583b287c6c7SBjoern A. Zeeb 	int mss = 0;
35843ac12506SJonathan T. Looney 	uint32_t maxmtu = 0;
3585c068736aSJeffrey Hsu 	struct inpcb *inp = tp->t_inpcb;
358697d8d152SAndre Oppermann 	struct hc_metrics_lite metrics;
3587fb59c426SYoshinobu Inoue #ifdef INET6
3588c068736aSJeffrey Hsu 	int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
3589c068736aSJeffrey Hsu 	size_t min_protoh = isipv6 ?
3590c068736aSJeffrey Hsu 			    sizeof (struct ip6_hdr) + sizeof (struct tcphdr) :
3591c068736aSJeffrey Hsu 			    sizeof (struct tcpiphdr);
3592c068736aSJeffrey Hsu #else
3593c068736aSJeffrey Hsu 	const size_t min_protoh = sizeof(struct tcpiphdr);
3594fb59c426SYoshinobu Inoue #endif
3595df8bae1dSRodney W. Grimes 
35968501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
35977a3244ccSRobert Watson 
3598ef341ee1SGleb Smirnoff 	if (mtuoffer != -1) {
3599ef341ee1SGleb Smirnoff 		KASSERT(offer == -1, ("%s: conflict", __func__));
3600ef341ee1SGleb Smirnoff 		offer = mtuoffer - min_protoh;
3601ef341ee1SGleb Smirnoff 	}
3602ef341ee1SGleb Smirnoff 
3603e8949f74SAndre Oppermann 	/* Initialize. */
360497d8d152SAndre Oppermann #ifdef INET6
360597d8d152SAndre Oppermann 	if (isipv6) {
36063c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu6(&inp->inp_inc, cap);
36070c39d38dSGleb Smirnoff 		tp->t_maxseg = V_tcp_v6mssdflt;
3608b287c6c7SBjoern A. Zeeb 	}
360997d8d152SAndre Oppermann #endif
3610b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3611b287c6c7SBjoern A. Zeeb 	else
3612b287c6c7SBjoern A. Zeeb #endif
3613b287c6c7SBjoern A. Zeeb #ifdef INET
361497d8d152SAndre Oppermann 	{
36153c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu(&inp->inp_inc, cap);
36160c39d38dSGleb Smirnoff 		tp->t_maxseg = V_tcp_mssdflt;
3617df8bae1dSRodney W. Grimes 	}
3618b287c6c7SBjoern A. Zeeb #endif
3619df8bae1dSRodney W. Grimes 
362097d8d152SAndre Oppermann 	/*
3621e8949f74SAndre Oppermann 	 * No route to sender, stay with default mss and return.
362297d8d152SAndre Oppermann 	 */
36236f01cac6SBjoern A. Zeeb 	if (maxmtu == 0) {
36246f01cac6SBjoern A. Zeeb 		/*
36258e5c87f4SBjoern A. Zeeb 		 * In case we return early we need to initialize metrics
36266f01cac6SBjoern A. Zeeb 		 * to a defined state as tcp_hc_get() would do for us
36276f01cac6SBjoern A. Zeeb 		 * if there was no cache hit.
36286f01cac6SBjoern A. Zeeb 		 */
36296f01cac6SBjoern A. Zeeb 		if (metricptr != NULL)
36306f01cac6SBjoern A. Zeeb 			bzero(metricptr, sizeof(struct hc_metrics_lite));
363197d8d152SAndre Oppermann 		return;
36326f01cac6SBjoern A. Zeeb 	}
363397d8d152SAndre Oppermann 
3634e8949f74SAndre Oppermann 	/* What have we got? */
363597d8d152SAndre Oppermann 	switch (offer) {
363697d8d152SAndre Oppermann 		case 0:
363797d8d152SAndre Oppermann 			/*
363897d8d152SAndre Oppermann 			 * Offer == 0 means that there was no MSS on the SYN
3639c3b02504SBjoern A. Zeeb 			 * segment, in this case we use tcp_mssdflt as
36400c39d38dSGleb Smirnoff 			 * already assigned to t_maxseg above.
364197d8d152SAndre Oppermann 			 */
36420c39d38dSGleb Smirnoff 			offer = tp->t_maxseg;
364397d8d152SAndre Oppermann 			break;
364497d8d152SAndre Oppermann 
364597d8d152SAndre Oppermann 		case -1:
3646a0292f23SGarrett Wollman 			/*
3647c94c54e4SAndre Oppermann 			 * Offer == -1 means that we didn't receive SYN yet.
3648a0292f23SGarrett Wollman 			 */
364997d8d152SAndre Oppermann 			/* FALLTHROUGH */
365097d8d152SAndre Oppermann 
365197d8d152SAndre Oppermann 		default:
3652a0292f23SGarrett Wollman 			/*
365353369ac9SAndre Oppermann 			 * Prevent DoS attack with too small MSS. Round up
365453369ac9SAndre Oppermann 			 * to at least minmss.
365553369ac9SAndre Oppermann 			 */
3656603724d3SBjoern A. Zeeb 			offer = max(offer, V_tcp_minmss);
365797d8d152SAndre Oppermann 	}
3658a0292f23SGarrett Wollman 
3659df8bae1dSRodney W. Grimes 	/*
3660e8949f74SAndre Oppermann 	 * rmx information is now retrieved from tcp_hostcache.
3661df8bae1dSRodney W. Grimes 	 */
366297d8d152SAndre Oppermann 	tcp_hc_get(&inp->inp_inc, &metrics);
36633cee92e0SBjoern A. Zeeb 	if (metricptr != NULL)
36643cee92e0SBjoern A. Zeeb 		bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite));
366597d8d152SAndre Oppermann 
3666df8bae1dSRodney W. Grimes 	/*
3667cc412412SHiren Panchasara 	 * If there's a discovered mtu in tcp hostcache, use it.
3668cc412412SHiren Panchasara 	 * Else, use the link mtu.
3669df8bae1dSRodney W. Grimes 	 */
367097d8d152SAndre Oppermann 	if (metrics.rmx_mtu)
36712d166c02SAndre Oppermann 		mss = min(metrics.rmx_mtu, maxmtu) - min_protoh;
3672c068736aSJeffrey Hsu 	else {
367331b3783cSHajimu UMEMOTO #ifdef INET6
3674fb59c426SYoshinobu Inoue 		if (isipv6) {
367597d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3676603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
367797d8d152SAndre Oppermann 			    !in6_localaddr(&inp->in6p_faddr))
3678603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_v6mssdflt);
3679b287c6c7SBjoern A. Zeeb 		}
3680b3399803SHajimu UMEMOTO #endif
3681b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3682b287c6c7SBjoern A. Zeeb 		else
3683b287c6c7SBjoern A. Zeeb #endif
3684b287c6c7SBjoern A. Zeeb #ifdef INET
368597d8d152SAndre Oppermann 		{
368697d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3687603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
368897d8d152SAndre Oppermann 			    !in_localaddr(inp->inp_faddr))
3689603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_mssdflt);
3690a0292f23SGarrett Wollman 		}
3691b287c6c7SBjoern A. Zeeb #endif
36923cee92e0SBjoern A. Zeeb 		/*
36933cee92e0SBjoern A. Zeeb 		 * XXX - The above conditional (mss = maxmtu - min_protoh)
36943cee92e0SBjoern A. Zeeb 		 * probably violates the TCP spec.
36953cee92e0SBjoern A. Zeeb 		 * The problem is that, since we don't know the
36963cee92e0SBjoern A. Zeeb 		 * other end's MSS, we are supposed to use a conservative
36973cee92e0SBjoern A. Zeeb 		 * default.  But, if we do that, then MTU discovery will
36983cee92e0SBjoern A. Zeeb 		 * never actually take place, because the conservative
36993cee92e0SBjoern A. Zeeb 		 * default is much less than the MTUs typically seen
37003cee92e0SBjoern A. Zeeb 		 * on the Internet today.  For the moment, we'll sweep
37013cee92e0SBjoern A. Zeeb 		 * this under the carpet.
37023cee92e0SBjoern A. Zeeb 		 *
37033cee92e0SBjoern A. Zeeb 		 * The conservative default might not actually be a problem
37043cee92e0SBjoern A. Zeeb 		 * if the only case this occurs is when sending an initial
37053cee92e0SBjoern A. Zeeb 		 * SYN with options and data to a host we've never talked
37063cee92e0SBjoern A. Zeeb 		 * to before.  Then, they will reply with an MSS value which
37073cee92e0SBjoern A. Zeeb 		 * will get recorded and the new parameters should get
37083cee92e0SBjoern A. Zeeb 		 * recomputed.  For Further Study.
37093cee92e0SBjoern A. Zeeb 		 */
371097d8d152SAndre Oppermann 	}
3711a0292f23SGarrett Wollman 	mss = min(mss, offer);
371297d8d152SAndre Oppermann 
3713a0292f23SGarrett Wollman 	/*
37140c39d38dSGleb Smirnoff 	 * Sanity check: make sure that maxseg will be large
37153cee92e0SBjoern A. Zeeb 	 * enough to allow some data on segments even if the
37163cee92e0SBjoern A. Zeeb 	 * all the option space is used (40bytes).  Otherwise
37173cee92e0SBjoern A. Zeeb 	 * funny things may happen in tcp_output.
37180c39d38dSGleb Smirnoff 	 *
37190c39d38dSGleb Smirnoff 	 * XXXGL: shouldn't we reserve space for IP/IPv6 options?
37203cee92e0SBjoern A. Zeeb 	 */
37213cee92e0SBjoern A. Zeeb 	mss = max(mss, 64);
37223cee92e0SBjoern A. Zeeb 
372397d8d152SAndre Oppermann 	tp->t_maxseg = mss;
37243cee92e0SBjoern A. Zeeb }
37253cee92e0SBjoern A. Zeeb 
37263cee92e0SBjoern A. Zeeb void
37273cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer)
37283cee92e0SBjoern A. Zeeb {
3729dbc42409SLawrence Stewart 	int mss;
37303ac12506SJonathan T. Looney 	uint32_t bufsize;
37313cee92e0SBjoern A. Zeeb 	struct inpcb *inp;
37323cee92e0SBjoern A. Zeeb 	struct socket *so;
37333cee92e0SBjoern A. Zeeb 	struct hc_metrics_lite metrics;
37343c914c54SAndre Oppermann 	struct tcp_ifcap cap;
3735dbc42409SLawrence Stewart 
37363cee92e0SBjoern A. Zeeb 	KASSERT(tp != NULL, ("%s: tp == NULL", __func__));
37373cee92e0SBjoern A. Zeeb 
37383c914c54SAndre Oppermann 	bzero(&cap, sizeof(cap));
37393c914c54SAndre Oppermann 	tcp_mss_update(tp, offer, -1, &metrics, &cap);
37403cee92e0SBjoern A. Zeeb 
37413cee92e0SBjoern A. Zeeb 	mss = tp->t_maxseg;
37423cee92e0SBjoern A. Zeeb 	inp = tp->t_inpcb;
374397d8d152SAndre Oppermann 
3744df8bae1dSRodney W. Grimes 	/*
374597d8d152SAndre Oppermann 	 * If there's a pipesize, change the socket buffer to that size,
374697d8d152SAndre Oppermann 	 * don't change if sb_hiwat is different than default (then it
374797d8d152SAndre Oppermann 	 * has been changed on purpose with setsockopt).
374897d8d152SAndre Oppermann 	 * Make the socket buffers an integral number of mss units;
374997d8d152SAndre Oppermann 	 * if the mss is larger than the socket buffer, decrease the mss.
3750df8bae1dSRodney W. Grimes 	 */
3751c3b02504SBjoern A. Zeeb 	so = inp->inp_socket;
37523f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
3753e233e2acSAndre Oppermann 	if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe)
375497d8d152SAndre Oppermann 		bufsize = metrics.rmx_sendpipe;
375597d8d152SAndre Oppermann 	else
3756df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
3757df8bae1dSRodney W. Grimes 	if (bufsize < mss)
3758df8bae1dSRodney W. Grimes 		mss = bufsize;
3759df8bae1dSRodney W. Grimes 	else {
3760df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3761df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3762df8bae1dSRodney W. Grimes 			bufsize = sb_max;
376388c39af3SRuslan Ermilov 		if (bufsize > so->so_snd.sb_hiwat)
37643f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_snd, bufsize, so, NULL);
3765df8bae1dSRodney W. Grimes 	}
37663f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
3767784ce8faSHiren Panchasara 	/*
3768784ce8faSHiren Panchasara 	 * Sanity check: make sure that maxseg will be large
3769784ce8faSHiren Panchasara 	 * enough to allow some data on segments even if the
3770784ce8faSHiren Panchasara 	 * all the option space is used (40bytes).  Otherwise
3771784ce8faSHiren Panchasara 	 * funny things may happen in tcp_output.
3772784ce8faSHiren Panchasara 	 *
3773784ce8faSHiren Panchasara 	 * XXXGL: shouldn't we reserve space for IP/IPv6 options?
3774784ce8faSHiren Panchasara 	 */
3775784ce8faSHiren Panchasara 	tp->t_maxseg = max(mss, 64);
3776df8bae1dSRodney W. Grimes 
37773f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
3778e233e2acSAndre Oppermann 	if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe)
377997d8d152SAndre Oppermann 		bufsize = metrics.rmx_recvpipe;
378097d8d152SAndre Oppermann 	else
3781df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
3782df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
3783df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3784df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3785df8bae1dSRodney W. Grimes 			bufsize = sb_max;
378688c39af3SRuslan Ermilov 		if (bufsize > so->so_rcv.sb_hiwat)
37873f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL);
3788df8bae1dSRodney W. Grimes 	}
37893f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
379091d6cfa6SBjoern A. Zeeb 
379191d6cfa6SBjoern A. Zeeb 	/* Check the interface for TSO capabilities. */
37923c914c54SAndre Oppermann 	if (cap.ifcap & CSUM_TSO) {
379391d6cfa6SBjoern A. Zeeb 		tp->t_flags |= TF_TSO;
37943c914c54SAndre Oppermann 		tp->t_tsomax = cap.tsomax;
37959fd573c3SHans Petter Selasky 		tp->t_tsomaxsegcount = cap.tsomaxsegcount;
37969fd573c3SHans Petter Selasky 		tp->t_tsomaxsegsize = cap.tsomaxsegsize;
37973c914c54SAndre Oppermann 	}
3798a0292f23SGarrett Wollman }
3799a0292f23SGarrett Wollman 
3800a0292f23SGarrett Wollman /*
3801a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
3802a0292f23SGarrett Wollman  */
3803a0292f23SGarrett Wollman int
3804ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc)
3805a0292f23SGarrett Wollman {
380697d8d152SAndre Oppermann 	int mss = 0;
38073ac12506SJonathan T. Looney 	uint32_t thcmtu = 0;
38083ac12506SJonathan T. Looney 	uint32_t maxmtu = 0;
380997d8d152SAndre Oppermann 	size_t min_protoh;
3810a0292f23SGarrett Wollman 
381197d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer"));
3812a0292f23SGarrett Wollman 
381331b3783cSHajimu UMEMOTO #ifdef INET6
3814dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
3815603724d3SBjoern A. Zeeb 		mss = V_tcp_v6mssdflt;
3816233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu6(inc, NULL);
381797d8d152SAndre Oppermann 		min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
3818b287c6c7SBjoern A. Zeeb 	}
381931b3783cSHajimu UMEMOTO #endif
3820b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3821b287c6c7SBjoern A. Zeeb 	else
3822b287c6c7SBjoern A. Zeeb #endif
3823b287c6c7SBjoern A. Zeeb #ifdef INET
382497d8d152SAndre Oppermann 	{
3825603724d3SBjoern A. Zeeb 		mss = V_tcp_mssdflt;
3826233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu(inc, NULL);
382797d8d152SAndre Oppermann 		min_protoh = sizeof(struct tcpiphdr);
382897d8d152SAndre Oppermann 	}
3829b287c6c7SBjoern A. Zeeb #endif
38303a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET)
38313a9391deSBjoern A. Zeeb 	thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
38323a9391deSBjoern A. Zeeb #endif
38333a9391deSBjoern A. Zeeb 
383497d8d152SAndre Oppermann 	if (maxmtu && thcmtu)
383597d8d152SAndre Oppermann 		mss = min(maxmtu, thcmtu) - min_protoh;
383697d8d152SAndre Oppermann 	else if (maxmtu || thcmtu)
383797d8d152SAndre Oppermann 		mss = max(maxmtu, thcmtu) - min_protoh;
383897d8d152SAndre Oppermann 
383997d8d152SAndre Oppermann 	return (mss);
3840df8bae1dSRodney W. Grimes }
384146f58482SJonathan Lemon 
384246f58482SJonathan Lemon /*
3843c068736aSJeffrey Hsu  * On a partial ack arrives, force the retransmission of the
3844c068736aSJeffrey Hsu  * next unacknowledged segment.  Do not clear tp->t_dupacks.
3845c068736aSJeffrey Hsu  * By setting snd_nxt to ti_ack, this forces retransmission timer to
3846c068736aSJeffrey Hsu  * be started again.
384746f58482SJonathan Lemon  */
384855bceb1eSRandall Stewart void
3849ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th)
385046f58482SJonathan Lemon {
385146f58482SJonathan Lemon 	tcp_seq onxt = tp->snd_nxt;
38523ac12506SJonathan T. Looney 	uint32_t ocwnd = tp->snd_cwnd;
38530c39d38dSGleb Smirnoff 	u_int maxseg = tcp_maxseg(tp);
385446f58482SJonathan Lemon 
38558501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
38567a3244ccSRobert Watson 
3857b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_REXMT, 0);
385846f58482SJonathan Lemon 	tp->t_rtttime = 0;
385946f58482SJonathan Lemon 	tp->snd_nxt = th->th_ack;
38606b2a5f92SJayanth Vijayaraghavan 	/*
3861c068736aSJeffrey Hsu 	 * Set snd_cwnd to one segment beyond acknowledged offset.
3862c068736aSJeffrey Hsu 	 * (tp->snd_una has not yet been updated when this function is called.)
38636b2a5f92SJayanth Vijayaraghavan 	 */
38640c39d38dSGleb Smirnoff 	tp->snd_cwnd = maxseg + BYTES_THIS_ACK(tp, th);
3865a84db8f4SMatthew Dillon 	tp->t_flags |= TF_ACKNOW;
386655bceb1eSRandall Stewart 	(void) tp->t_fb->tfb_tcp_output(tp);
386746f58482SJonathan Lemon 	tp->snd_cwnd = ocwnd;
386846f58482SJonathan Lemon 	if (SEQ_GT(onxt, tp->snd_nxt))
386946f58482SJonathan Lemon 		tp->snd_nxt = onxt;
387046f58482SJonathan Lemon 	/*
387146f58482SJonathan Lemon 	 * Partial window deflation.  Relies on fact that tp->snd_una
387246f58482SJonathan Lemon 	 * not updated yet.
387346f58482SJonathan Lemon 	 */
3874dbc42409SLawrence Stewart 	if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th))
3875dbc42409SLawrence Stewart 		tp->snd_cwnd -= BYTES_THIS_ACK(tp, th);
3876d7587117SPaul Saab 	else
3877d7587117SPaul Saab 		tp->snd_cwnd = 0;
38780c39d38dSGleb Smirnoff 	tp->snd_cwnd += maxseg;
387946f58482SJonathan Lemon }
388012eeb81fSHiren Panchasara 
388112eeb81fSHiren Panchasara int
388212eeb81fSHiren Panchasara tcp_compute_pipe(struct tcpcb *tp)
388312eeb81fSHiren Panchasara {
388412eeb81fSHiren Panchasara 	return (tp->snd_max - tp->snd_una +
388512eeb81fSHiren Panchasara 		tp->sackhint.sack_bytes_rexmit -
388612eeb81fSHiren Panchasara 		tp->sackhint.sacked_bytes);
388712eeb81fSHiren Panchasara }
38887dc90a1dSMichael Tuexen 
38897dc90a1dSMichael Tuexen uint32_t
38907dc90a1dSMichael Tuexen tcp_compute_initwnd(uint32_t maxseg)
38917dc90a1dSMichael Tuexen {
38927dc90a1dSMichael Tuexen 	/*
38937dc90a1dSMichael Tuexen 	 * Calculate the Initial Window, also used as Restart Window
38947dc90a1dSMichael Tuexen 	 *
38957dc90a1dSMichael Tuexen 	 * RFC5681 Section 3.1 specifies the default conservative values.
38967dc90a1dSMichael Tuexen 	 * RFC3390 specifies slightly more aggressive values.
38977dc90a1dSMichael Tuexen 	 * RFC6928 increases it to ten segments.
38987dc90a1dSMichael Tuexen 	 * Support for user specified value for initial flight size.
38997dc90a1dSMichael Tuexen 	 */
39007dc90a1dSMichael Tuexen 	if (V_tcp_initcwnd_segments)
39017dc90a1dSMichael Tuexen 		return min(V_tcp_initcwnd_segments * maxseg,
39027dc90a1dSMichael Tuexen 		    max(2 * maxseg, V_tcp_initcwnd_segments * 1460));
39037dc90a1dSMichael Tuexen 	else if (V_tcp_do_rfc3390)
39047dc90a1dSMichael Tuexen 		return min(4 * maxseg, max(2 * maxseg, 4380));
39057dc90a1dSMichael Tuexen 	else {
39067dc90a1dSMichael Tuexen 		/* Per RFC5681 Section 3.1 */
39077dc90a1dSMichael Tuexen 		if (maxseg > 2190)
39087dc90a1dSMichael Tuexen 			return (2 * maxseg);
39097dc90a1dSMichael Tuexen 		else if (maxseg > 1095)
39107dc90a1dSMichael Tuexen 			return (3 * maxseg);
39117dc90a1dSMichael Tuexen 		else
39127dc90a1dSMichael Tuexen 			return (4 * maxseg);
39137dc90a1dSMichael Tuexen 	}
39147dc90a1dSMichael Tuexen }
3915