xref: /freebsd/sys/netinet/tcp_input.c (revision 98732609d56a088a82cb63109ec4363ed6cff0f4)
1c398230bSWarner Losh /*-
2e79adb8eSGarrett Wollman  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4dbc42409SLawrence Stewart  * Copyright (c) 2007-2008,2010
5dbc42409SLawrence Stewart  *	Swinburne University of Technology, Melbourne, Australia.
6dbc42409SLawrence Stewart  * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org>
7dbc42409SLawrence Stewart  * Copyright (c) 2010 The FreeBSD Foundation
8fa046d87SRobert Watson  * Copyright (c) 2010-2011 Juniper Networks, Inc.
9dbc42409SLawrence Stewart  * All rights reserved.
10dbc42409SLawrence Stewart  *
11dbc42409SLawrence Stewart  * Portions of this software were developed at the Centre for Advanced Internet
12891b8ed4SLawrence Stewart  * Architectures, Swinburne University of Technology, by Lawrence Stewart,
13891b8ed4SLawrence Stewart  * James Healy and David Hayes, made possible in part by a grant from the Cisco
14891b8ed4SLawrence Stewart  * University Research Program Fund at Community Foundation Silicon Valley.
15dbc42409SLawrence Stewart  *
16dbc42409SLawrence Stewart  * Portions of this software were developed at the Centre for Advanced
17dbc42409SLawrence Stewart  * Internet Architectures, Swinburne University of Technology, Melbourne,
18dbc42409SLawrence Stewart  * Australia by David Hayes under sponsorship from the FreeBSD Foundation.
19df8bae1dSRodney W. Grimes  *
20fa046d87SRobert Watson  * Portions of this software were developed by Robert N. M. Watson under
21fa046d87SRobert Watson  * contract to Juniper Networks, Inc.
22fa046d87SRobert Watson  *
23df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
24df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
25df8bae1dSRodney W. Grimes  * are met:
26df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
27df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
28df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
29df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
30df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
31fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
32df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
33df8bae1dSRodney W. Grimes  *    without specific prior written permission.
34df8bae1dSRodney W. Grimes  *
35df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
46df8bae1dSRodney W. Grimes  *
47e79adb8eSGarrett Wollman  *	@(#)tcp_input.c	8.12 (Berkeley) 5/24/95
48df8bae1dSRodney W. Grimes  */
49df8bae1dSRodney W. Grimes 
504b421e2dSMike Silbersack #include <sys/cdefs.h>
514b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
524b421e2dSMike Silbersack 
531cfd4b53SBruce M Simpson #include "opt_inet.h"
54fb59c426SYoshinobu Inoue #include "opt_inet6.h"
553a2a9f79SYoshinobu Inoue #include "opt_ipsec.h"
560cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
570cc12cc5SJoerg Wunsch 
58df8bae1dSRodney W. Grimes #include <sys/param.h>
5998163b98SPoul-Henning Kamp #include <sys/kernel.h>
60bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
6139bc9de5SLawrence Stewart #include <sys/hhook.h>
62bd79708dSJonathan T. Looney #endif
63df8bae1dSRodney W. Grimes #include <sys/malloc.h>
64df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
65a29f300eSGarrett Wollman #include <sys/proc.h>		/* for proc0 declaration */
66df8bae1dSRodney W. Grimes #include <sys/protosw.h>
6757f60867SMark Johnston #include <sys/sdt.h>
68960ed29cSSeigo Tanimura #include <sys/signalvar.h>
69df8bae1dSRodney W. Grimes #include <sys/socket.h>
70df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
71960ed29cSSeigo Tanimura #include <sys/sysctl.h>
72816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
73960ed29cSSeigo Tanimura #include <sys/systm.h>
74df8bae1dSRodney W. Grimes 
75e79adb8eSGarrett Wollman #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
76e79adb8eSGarrett Wollman 
7712e2e970SAndre Oppermann #include <vm/uma.h>
7812e2e970SAndre Oppermann 
79df8bae1dSRodney W. Grimes #include <net/if.h>
8076039bc8SGleb Smirnoff #include <net/if_var.h>
81df8bae1dSRodney W. Grimes #include <net/route.h>
82530c0060SRobert Watson #include <net/vnet.h>
83df8bae1dSRodney W. Grimes 
84773673c1SAndre Oppermann #define TCPSTATES		/* for logging */
85773673c1SAndre Oppermann 
86df8bae1dSRodney W. Grimes #include <netinet/in.h>
8757f60867SMark Johnston #include <netinet/in_kdtrace.h>
88960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
89df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
90df8bae1dSRodney W. Grimes #include <netinet/ip.h>
918e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h>	/* required for icmp_var.h */
92686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM */
93df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
94ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
95686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
96686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
97686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
98b66d74c1SGleb Smirnoff #include <netinet6/in6_var.h>
99960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h>
100960ed29cSSeigo Tanimura #include <netinet6/nd6.h>
101281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
102281a0fd4SPatrick Kelsey #include <netinet/tcp_fastopen.h>
103281a0fd4SPatrick Kelsey #endif
1042de3e790SGleb Smirnoff #include <netinet/tcp.h>
105df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
106df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
107df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
108df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
109fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h>
110df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
1114644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
11286a996e6SHiren Panchasara #ifdef TCPPCAP
11386a996e6SHiren Panchasara #include <netinet/tcp_pcap.h>
11486a996e6SHiren Panchasara #endif
115c325962bSMike Silbersack #include <netinet/tcp_syncache.h>
116610ee2f9SDavid Greenman #ifdef TCPDEBUG
117df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
118fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */
11909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
12009fe6320SNavdeep Parhar #include <netinet/tcp_offload.h>
12109fe6320SNavdeep Parhar #endif
122fb59c426SYoshinobu Inoue 
123fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
124b9234fafSSam Leffler 
125db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
126db4f9cc7SJonathan Lemon 
127aed55708SRobert Watson #include <security/mac/mac_framework.h>
128aed55708SRobert Watson 
129dbc42409SLawrence Stewart const int tcprexmtthresh = 3;
1300312fbe9SPoul-Henning Kamp 
131773673c1SAndre Oppermann int tcp_log_in_vain = 0;
132816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW,
133eddfbb76SRobert Watson     &tcp_log_in_vain, 0,
134eddfbb76SRobert Watson     "Log all incoming TCP segments to closed ports");
135816a3d83SPoul-Henning Kamp 
13682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, blackhole) = 0;
13782cea7e6SBjoern A. Zeeb #define	V_blackhole		VNET(blackhole)
1386df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW,
139eddfbb76SRobert Watson     &VNET_NAME(blackhole), 0,
140eddfbb76SRobert Watson     "Do not send RST on segments to closed ports");
14116f7f31fSGeoff Rehmet 
14282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_delack_enabled) = 1;
1436df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_VNET | CTLFLAG_RW,
144eddfbb76SRobert Watson     &VNET_NAME(tcp_delack_enabled), 0,
1453d177f46SBill Fumerola     "Delay ACK to try and piggyback it onto a data packet");
146f498eeeeSDavid Greenman 
14782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, drop_synfin) = 0;
14882cea7e6SBjoern A. Zeeb #define	V_drop_synfin		VNET(drop_synfin)
1496df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_VNET | CTLFLAG_RW,
150eddfbb76SRobert Watson     &VNET_NAME(drop_synfin), 0,
151eddfbb76SRobert Watson     "Drop TCP packets with SYN+FIN set");
152f8613305SDag-Erling Smørgrav 
15312eeb81fSHiren Panchasara VNET_DEFINE(int, tcp_do_rfc6675_pipe) = 0;
154054d38e3SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc6675_pipe, CTLFLAG_VNET | CTLFLAG_RW,
15512eeb81fSHiren Panchasara     &VNET_NAME(tcp_do_rfc6675_pipe), 0,
15612eeb81fSHiren Panchasara     "Use calculated pipe/in-flight bytes per RFC 6675");
15712eeb81fSHiren Panchasara 
15882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3042) = 1;
15982cea7e6SBjoern A. Zeeb #define	V_tcp_do_rfc3042	VNET(tcp_do_rfc3042)
1606df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_VNET | CTLFLAG_RW,
161eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3042), 0,
162eddfbb76SRobert Watson     "Enable RFC 3042 (Limited Transmit)");
163582a954bSJeffrey Hsu 
16482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3390) = 1;
1656df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_VNET | CTLFLAG_RW,
166eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3390), 0,
167da3a8a1aSJeffrey Hsu     "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)");
168da3a8a1aSJeffrey Hsu 
169356c7958SHiren Panchasara VNET_DEFINE(int, tcp_initcwnd_segments) = 10;
170356c7958SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, initcwnd_segments,
171356c7958SHiren Panchasara     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_initcwnd_segments), 0,
172356c7958SHiren Panchasara     "Slow-start flight size (initial congestion window) in number of segments");
17309440655SAndre Oppermann 
17482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3465) = 1;
1756df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_VNET | CTLFLAG_RW,
176eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3465), 0,
17724cb0f22SLawrence Stewart     "Enable RFC 3465 (Appropriate Byte Counting)");
178eddfbb76SRobert Watson 
17982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_abc_l_var) = 2;
1806df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_VNET | CTLFLAG_RW,
181eddfbb76SRobert Watson     &VNET_NAME(tcp_abc_l_var), 2,
18224cb0f22SLawrence Stewart     "Cap the max cwnd increment during slow-start to this number of segments");
18324cb0f22SLawrence Stewart 
1846472ac3dSEd Schouten static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, CTLFLAG_RW, 0, "TCP ECN");
185f2512ba1SRui Paulo 
186883054b4SDon Lewis VNET_DEFINE(int, tcp_do_ecn) = 2;
1876df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW,
188eddfbb76SRobert Watson     &VNET_NAME(tcp_do_ecn), 0,
189eddfbb76SRobert Watson     "TCP ECN support");
190eddfbb76SRobert Watson 
19182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_ecn_maxretries) = 1;
1926df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, maxretries, CTLFLAG_VNET | CTLFLAG_RW,
193eddfbb76SRobert Watson     &VNET_NAME(tcp_ecn_maxretries), 0,
194eddfbb76SRobert Watson     "Max retries before giving up on ECN");
195eddfbb76SRobert Watson 
1963220a212SGleb Smirnoff VNET_DEFINE(int, tcp_insecure_syn) = 0;
1973220a212SGleb Smirnoff #define	V_tcp_insecure_syn	VNET(tcp_insecure_syn)
1986df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_syn, CTLFLAG_VNET | CTLFLAG_RW,
1993220a212SGleb Smirnoff     &VNET_NAME(tcp_insecure_syn), 0,
2003220a212SGleb Smirnoff     "Follow RFC793 instead of RFC5961 criteria for accepting SYN packets");
2013220a212SGleb Smirnoff 
20282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_insecure_rst) = 0;
20382cea7e6SBjoern A. Zeeb #define	V_tcp_insecure_rst	VNET(tcp_insecure_rst)
2046df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_VNET | CTLFLAG_RW,
205eddfbb76SRobert Watson     &VNET_NAME(tcp_insecure_rst), 0,
2063220a212SGleb Smirnoff     "Follow RFC793 instead of RFC5961 criteria for accepting RST packets");
207a69968eeSMike Silbersack 
208fba0cea1SBjoern A. Zeeb VNET_DEFINE(int, tcp_recvspace) = 1024*64;
209873789cbSAndre Oppermann #define	V_tcp_recvspace	VNET(tcp_recvspace)
2106df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_VNET | CTLFLAG_RW,
211873789cbSAndre Oppermann     &VNET_NAME(tcp_recvspace), 0, "Initial receive socket buffer size");
212873789cbSAndre Oppermann 
21382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autorcvbuf) = 1;
21482cea7e6SBjoern A. Zeeb #define	V_tcp_do_autorcvbuf	VNET(tcp_do_autorcvbuf)
2156df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_VNET | CTLFLAG_RW,
216eddfbb76SRobert Watson     &VNET_NAME(tcp_do_autorcvbuf), 0,
2178b615593SMarko Zec     "Enable automatic receive buffer sizing");
2186741ecf5SAndre Oppermann 
21982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_inc) = 16*1024;
22082cea7e6SBjoern A. Zeeb #define	V_tcp_autorcvbuf_inc	VNET(tcp_autorcvbuf_inc)
2216df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_VNET | CTLFLAG_RW,
222eddfbb76SRobert Watson     &VNET_NAME(tcp_autorcvbuf_inc), 0,
2236489fe65SAndre Oppermann     "Incrementor step size of automatic receive buffer");
2246741ecf5SAndre Oppermann 
225b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024;
22682cea7e6SBjoern A. Zeeb #define	V_tcp_autorcvbuf_max	VNET(tcp_autorcvbuf_max)
2276df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_VNET | CTLFLAG_RW,
228eddfbb76SRobert Watson     &VNET_NAME(tcp_autorcvbuf_max), 0,
2298b615593SMarko Zec     "Max size of automatic receive buffer");
2306741ecf5SAndre Oppermann 
231eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, tcb);
23244e33a07SMarko Zec #define	tcb6	tcb  /* for KAME src sync over BSD*'s */
23382cea7e6SBjoern A. Zeeb VNET_DEFINE(struct inpcbinfo, tcbinfo);
234df8bae1dSRodney W. Grimes 
235315e3e38SRobert Watson /*
236bf840a17SGleb Smirnoff  * TCP statistics are stored in an array of counter(9)s, which size matches
237bf840a17SGleb Smirnoff  * size of struct tcpstat.  TCP running connection count is a regular array.
2385923c293SGleb Smirnoff  */
2395da0521fSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct tcpstat, tcpstat);
2405da0521fSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_tcp, TCPCTL_STATS, stats, struct tcpstat,
2415da0521fSAndrey V. Elsukov     tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
242bf840a17SGleb Smirnoff VNET_DEFINE(counter_u64_t, tcps_states[TCP_NSTATES]);
243bf840a17SGleb Smirnoff SYSCTL_COUNTER_U64_ARRAY(_net_inet_tcp, TCPCTL_STATES, states, CTLFLAG_RD |
244d4d32b9fSHans Petter Selasky     CTLFLAG_VNET, &VNET_NAME(tcps_states)[0], TCP_NSTATES,
245bf840a17SGleb Smirnoff     "TCP connection counts by TCP state");
246bf840a17SGleb Smirnoff 
247bf840a17SGleb Smirnoff static void
248bf840a17SGleb Smirnoff tcp_vnet_init(const void *unused)
249bf840a17SGleb Smirnoff {
250bf840a17SGleb Smirnoff 
251f59d975eSGleb Smirnoff 	COUNTER_ARRAY_ALLOC(V_tcps_states, TCP_NSTATES, M_WAITOK);
252bf840a17SGleb Smirnoff 	VNET_PCPUSTAT_ALLOC(tcpstat, M_WAITOK);
253bf840a17SGleb Smirnoff }
254bf840a17SGleb Smirnoff VNET_SYSINIT(tcp_vnet_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
255bf840a17SGleb Smirnoff     tcp_vnet_init, NULL);
2565923c293SGleb Smirnoff 
2575923c293SGleb Smirnoff #ifdef VIMAGE
258bf840a17SGleb Smirnoff static void
259bf840a17SGleb Smirnoff tcp_vnet_uninit(const void *unused)
260bf840a17SGleb Smirnoff {
261bf840a17SGleb Smirnoff 
262f59d975eSGleb Smirnoff 	COUNTER_ARRAY_FREE(V_tcps_states, TCP_NSTATES);
263bf840a17SGleb Smirnoff 	VNET_PCPUSTAT_FREE(tcpstat);
264bf840a17SGleb Smirnoff }
265bf840a17SGleb Smirnoff VNET_SYSUNINIT(tcp_vnet_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
266bf840a17SGleb Smirnoff     tcp_vnet_uninit, NULL);
2675923c293SGleb Smirnoff #endif /* VIMAGE */
268bf840a17SGleb Smirnoff 
2695923c293SGleb Smirnoff /*
270315e3e38SRobert Watson  * Kernel module interface for updating tcpstat.  The argument is an index
2715923c293SGleb Smirnoff  * into tcpstat treated as an array.
272315e3e38SRobert Watson  */
273315e3e38SRobert Watson void
274315e3e38SRobert Watson kmod_tcpstat_inc(int statnum)
275315e3e38SRobert Watson {
276315e3e38SRobert Watson 
2775da0521fSAndrey V. Elsukov 	counter_u64_add(VNET(tcpstat)[statnum], 1);
278315e3e38SRobert Watson }
279315e3e38SRobert Watson 
280bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
281dbc42409SLawrence Stewart /*
28239bc9de5SLawrence Stewart  * Wrapper for the TCP established input helper hook.
28339bc9de5SLawrence Stewart  */
28455bceb1eSRandall Stewart void
28539bc9de5SLawrence Stewart hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to)
28639bc9de5SLawrence Stewart {
28739bc9de5SLawrence Stewart 	struct tcp_hhook_data hhook_data;
28839bc9de5SLawrence Stewart 
28939bc9de5SLawrence Stewart 	if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) {
29039bc9de5SLawrence Stewart 		hhook_data.tp = tp;
29139bc9de5SLawrence Stewart 		hhook_data.th = th;
29239bc9de5SLawrence Stewart 		hhook_data.to = to;
29339bc9de5SLawrence Stewart 
29439bc9de5SLawrence Stewart 		hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data,
29539bc9de5SLawrence Stewart 		    tp->osd);
29639bc9de5SLawrence Stewart 	}
29739bc9de5SLawrence Stewart }
298bd79708dSJonathan T. Looney #endif
29939bc9de5SLawrence Stewart 
30039bc9de5SLawrence Stewart /*
301dbc42409SLawrence Stewart  * CC wrapper hook functions
302dbc42409SLawrence Stewart  */
30355bceb1eSRandall Stewart void
3044b7b743cSLawrence Stewart cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t nsegs,
3054b7b743cSLawrence Stewart     uint16_t type)
306f2512ba1SRui Paulo {
307dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
308f2512ba1SRui Paulo 
3094b7b743cSLawrence Stewart 	tp->ccv->nsegs = nsegs;
310dbc42409SLawrence Stewart 	tp->ccv->bytes_this_ack = BYTES_THIS_ACK(tp, th);
3115b648e79SLawrence Stewart 	if (tp->snd_cwnd <= tp->snd_wnd)
312dbc42409SLawrence Stewart 		tp->ccv->flags |= CCF_CWND_LIMITED;
313dbc42409SLawrence Stewart 	else
314dbc42409SLawrence Stewart 		tp->ccv->flags &= ~CCF_CWND_LIMITED;
315dbc42409SLawrence Stewart 
316dbc42409SLawrence Stewart 	if (type == CC_ACK) {
317dbc42409SLawrence Stewart 		if (tp->snd_cwnd > tp->snd_ssthresh) {
318dbc42409SLawrence Stewart 			tp->t_bytes_acked += min(tp->ccv->bytes_this_ack,
3194b7b743cSLawrence Stewart 			     nsegs * V_tcp_abc_l_var * tcp_maxseg(tp));
320dbc42409SLawrence Stewart 			if (tp->t_bytes_acked >= tp->snd_cwnd) {
321dbc42409SLawrence Stewart 				tp->t_bytes_acked -= tp->snd_cwnd;
322dbc42409SLawrence Stewart 				tp->ccv->flags |= CCF_ABC_SENTAWND;
323dbc42409SLawrence Stewart 			}
324dbc42409SLawrence Stewart 		} else {
325dbc42409SLawrence Stewart 				tp->ccv->flags &= ~CCF_ABC_SENTAWND;
326dbc42409SLawrence Stewart 				tp->t_bytes_acked = 0;
327dbc42409SLawrence Stewart 		}
328dbc42409SLawrence Stewart 	}
329dbc42409SLawrence Stewart 
330dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->ack_received != NULL) {
331dbc42409SLawrence Stewart 		/* XXXLAS: Find a way to live without this */
332dbc42409SLawrence Stewart 		tp->ccv->curack = th->th_ack;
333dbc42409SLawrence Stewart 		CC_ALGO(tp)->ack_received(tp->ccv, type);
334dbc42409SLawrence Stewart 	}
335dbc42409SLawrence Stewart }
336dbc42409SLawrence Stewart 
33755bceb1eSRandall Stewart void
338dbc42409SLawrence Stewart cc_conn_init(struct tcpcb *tp)
339dbc42409SLawrence Stewart {
340dbc42409SLawrence Stewart 	struct hc_metrics_lite metrics;
341dbc42409SLawrence Stewart 	struct inpcb *inp = tp->t_inpcb;
3420c39d38dSGleb Smirnoff 	u_int maxseg;
343dbc42409SLawrence Stewart 	int rtt;
344dbc42409SLawrence Stewart 
345dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
346dbc42409SLawrence Stewart 
347dbc42409SLawrence Stewart 	tcp_hc_get(&inp->inp_inc, &metrics);
3480c39d38dSGleb Smirnoff 	maxseg = tcp_maxseg(tp);
349dbc42409SLawrence Stewart 
350dbc42409SLawrence Stewart 	if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) {
351dbc42409SLawrence Stewart 		tp->t_srtt = rtt;
352dbc42409SLawrence Stewart 		tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE;
353dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_usedrtt);
354dbc42409SLawrence Stewart 		if (metrics.rmx_rttvar) {
355dbc42409SLawrence Stewart 			tp->t_rttvar = metrics.rmx_rttvar;
356dbc42409SLawrence Stewart 			TCPSTAT_INC(tcps_usedrttvar);
357dbc42409SLawrence Stewart 		} else {
358dbc42409SLawrence Stewart 			/* default variation is +- 1 rtt */
359dbc42409SLawrence Stewart 			tp->t_rttvar =
360dbc42409SLawrence Stewart 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
361dbc42409SLawrence Stewart 		}
362dbc42409SLawrence Stewart 		TCPT_RANGESET(tp->t_rxtcur,
363dbc42409SLawrence Stewart 		    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
364dbc42409SLawrence Stewart 		    tp->t_rttmin, TCPTV_REXMTMAX);
365dbc42409SLawrence Stewart 	}
366dbc42409SLawrence Stewart 	if (metrics.rmx_ssthresh) {
367dbc42409SLawrence Stewart 		/*
368dbc42409SLawrence Stewart 		 * There's some sort of gateway or interface
369dbc42409SLawrence Stewart 		 * buffer limit on the path.  Use this to set
370a4641f4eSPedro F. Giffuni 		 * the slow start threshold, but set the
371dbc42409SLawrence Stewart 		 * threshold to no less than 2*mss.
372dbc42409SLawrence Stewart 		 */
3730c39d38dSGleb Smirnoff 		tp->snd_ssthresh = max(2 * maxseg, metrics.rmx_ssthresh);
374dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_usedssthresh);
375dbc42409SLawrence Stewart 	}
376dbc42409SLawrence Stewart 
377dbc42409SLawrence Stewart 	/*
3789ec4a4ccSAndre Oppermann 	 * Set the initial slow-start flight size.
379dbc42409SLawrence Stewart 	 *
380cf8f04f4SAndre Oppermann 	 * RFC5681 Section 3.1 specifies the default conservative values.
381cf8f04f4SAndre Oppermann 	 * RFC3390 specifies slightly more aggressive values.
382b8b4cfcdSSergey Kandaurov 	 * RFC6928 increases it to ten segments.
383356c7958SHiren Panchasara 	 * Support for user specified value for initial flight size.
384cf8f04f4SAndre Oppermann 	 *
385cf8f04f4SAndre Oppermann 	 * If a SYN or SYN/ACK was lost and retransmitted, we have to
386cf8f04f4SAndre Oppermann 	 * reduce the initial CWND to one segment as congestion is likely
387cf8f04f4SAndre Oppermann 	 * requiring us to be cautious.
388dbc42409SLawrence Stewart 	 */
389cf8f04f4SAndre Oppermann 	if (tp->snd_cwnd == 1)
3900c39d38dSGleb Smirnoff 		tp->snd_cwnd = maxseg;		/* SYN(-ACK) lost */
391356c7958SHiren Panchasara 	else if (V_tcp_initcwnd_segments)
3920c39d38dSGleb Smirnoff 		tp->snd_cwnd = min(V_tcp_initcwnd_segments * maxseg,
3930c39d38dSGleb Smirnoff 		    max(2 * maxseg, V_tcp_initcwnd_segments * 1460));
394cf8f04f4SAndre Oppermann 	else if (V_tcp_do_rfc3390)
3950c39d38dSGleb Smirnoff 		tp->snd_cwnd = min(4 * maxseg, max(2 * maxseg, 4380));
39622efabd4SAndre Oppermann 	else {
39722efabd4SAndre Oppermann 		/* Per RFC5681 Section 3.1 */
3980c39d38dSGleb Smirnoff 		if (maxseg > 2190)
3990c39d38dSGleb Smirnoff 			tp->snd_cwnd = 2 * maxseg;
4000c39d38dSGleb Smirnoff 		else if (maxseg > 1095)
4010c39d38dSGleb Smirnoff 			tp->snd_cwnd = 3 * maxseg;
402dbc42409SLawrence Stewart 		else
4030c39d38dSGleb Smirnoff 			tp->snd_cwnd = 4 * maxseg;
40422efabd4SAndre Oppermann 	}
405dbc42409SLawrence Stewart 
406dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->conn_init != NULL)
407dbc42409SLawrence Stewart 		CC_ALGO(tp)->conn_init(tp->ccv);
408dbc42409SLawrence Stewart }
409dbc42409SLawrence Stewart 
410dbc42409SLawrence Stewart void inline
411dbc42409SLawrence Stewart cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type)
412dbc42409SLawrence Stewart {
4130c39d38dSGleb Smirnoff 	u_int maxseg;
4140c39d38dSGleb Smirnoff 
415dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
416dbc42409SLawrence Stewart 
417dbc42409SLawrence Stewart 	switch(type) {
418dbc42409SLawrence Stewart 	case CC_NDUPACK:
419dbc42409SLawrence Stewart 		if (!IN_FASTRECOVERY(tp->t_flags)) {
420f2512ba1SRui Paulo 			tp->snd_recover = tp->snd_max;
421f2512ba1SRui Paulo 			if (tp->t_flags & TF_ECN_PERMIT)
422f2512ba1SRui Paulo 				tp->t_flags |= TF_ECN_SND_CWR;
423f2512ba1SRui Paulo 		}
424dbc42409SLawrence Stewart 		break;
425dbc42409SLawrence Stewart 	case CC_ECN:
426dbc42409SLawrence Stewart 		if (!IN_CONGRECOVERY(tp->t_flags)) {
427dbc42409SLawrence Stewart 			TCPSTAT_INC(tcps_ecn_rcwnd);
428dbc42409SLawrence Stewart 			tp->snd_recover = tp->snd_max;
429dbc42409SLawrence Stewart 			if (tp->t_flags & TF_ECN_PERMIT)
430dbc42409SLawrence Stewart 				tp->t_flags |= TF_ECN_SND_CWR;
431dbc42409SLawrence Stewart 		}
432dbc42409SLawrence Stewart 		break;
433dbc42409SLawrence Stewart 	case CC_RTO:
4340c39d38dSGleb Smirnoff 		maxseg = tcp_maxseg(tp);
435dbc42409SLawrence Stewart 		tp->t_dupacks = 0;
436dbc42409SLawrence Stewart 		tp->t_bytes_acked = 0;
437dbc42409SLawrence Stewart 		EXIT_RECOVERY(tp->t_flags);
4384e7f7553SHiren Panchasara 		if (CC_ALGO(tp)->cong_signal == NULL) {
4394e7f7553SHiren Panchasara 			/*
4404e7f7553SHiren Panchasara 			 * RFC5681 Section 3.1
4414e7f7553SHiren Panchasara 			 * ssthresh = max (FlightSize / 2, 2*SMSS) eq (4)
4424e7f7553SHiren Panchasara 			 */
4434e7f7553SHiren Panchasara 			tp->snd_ssthresh =
444e04310d5SHiren Panchasara 			    max((tp->snd_max - tp->snd_una) / 2 / maxseg, 2)
445e04310d5SHiren Panchasara 				* maxseg;
4460c39d38dSGleb Smirnoff 			tp->snd_cwnd = maxseg;
4474e7f7553SHiren Panchasara 		}
448dbc42409SLawrence Stewart 		break;
449dbc42409SLawrence Stewart 	case CC_RTO_ERR:
450dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_sndrexmitbad);
451dbc42409SLawrence Stewart 		/* RTO was unnecessary, so reset everything. */
452dbc42409SLawrence Stewart 		tp->snd_cwnd = tp->snd_cwnd_prev;
453dbc42409SLawrence Stewart 		tp->snd_ssthresh = tp->snd_ssthresh_prev;
454dbc42409SLawrence Stewart 		tp->snd_recover = tp->snd_recover_prev;
455dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASFRECOVERY)
456dbc42409SLawrence Stewart 			ENTER_FASTRECOVERY(tp->t_flags);
457dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASCRECOVERY)
458dbc42409SLawrence Stewart 			ENTER_CONGRECOVERY(tp->t_flags);
459dbc42409SLawrence Stewart 		tp->snd_nxt = tp->snd_max;
460672dc4aeSJohn Baldwin 		tp->t_flags &= ~TF_PREVVALID;
461dbc42409SLawrence Stewart 		tp->t_badrxtwin = 0;
462dbc42409SLawrence Stewart 		break;
463dbc42409SLawrence Stewart 	}
464dbc42409SLawrence Stewart 
465dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->cong_signal != NULL) {
466dbc42409SLawrence Stewart 		if (th != NULL)
467dbc42409SLawrence Stewart 			tp->ccv->curack = th->th_ack;
468dbc42409SLawrence Stewart 		CC_ALGO(tp)->cong_signal(tp->ccv, type);
469dbc42409SLawrence Stewart 	}
470dbc42409SLawrence Stewart }
471dbc42409SLawrence Stewart 
47255bceb1eSRandall Stewart void inline
473dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th)
474dbc42409SLawrence Stewart {
475dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
476dbc42409SLawrence Stewart 
477dbc42409SLawrence Stewart 	/* XXXLAS: KASSERT that we're in recovery? */
478dbc42409SLawrence Stewart 
479dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->post_recovery != NULL) {
480dbc42409SLawrence Stewart 		tp->ccv->curack = th->th_ack;
481dbc42409SLawrence Stewart 		CC_ALGO(tp)->post_recovery(tp->ccv);
482dbc42409SLawrence Stewart 	}
483dbc42409SLawrence Stewart 	/* XXXLAS: EXIT_RECOVERY ? */
484dbc42409SLawrence Stewart 	tp->t_bytes_acked = 0;
485dbc42409SLawrence Stewart }
4860312fbe9SPoul-Henning Kamp 
487df8bae1dSRodney W. Grimes /*
488262c1c1aSMatthew Dillon  * Indicate whether this ack should be delayed.  We can delay the ack if
48985536381SHiren Panchasara  * following conditions are met:
49085536381SHiren Panchasara  *	- There is no delayed ack timer in progress.
49185536381SHiren Panchasara  *	- Our last ack wasn't a 0-sized window. We never want to delay
49285536381SHiren Panchasara  *	  the ack that opens up a 0-sized window.
49385536381SHiren Panchasara  *	- LRO wasn't used for this segment. We make sure by checking that the
49485536381SHiren Panchasara  *	  segment size is not larger than the MSS.
495d8c85a26SJonathan Lemon  */
496c1e5a6e5SAndre Oppermann #define DELAY_ACK(tp, tlen)						\
497b8152ba7SAndre Oppermann 	((!tcp_timer_active(tp, TT_DELACK) &&				\
498a14c749fSJonathan Lemon 	    (tp->t_flags & TF_RXWIN0SENT) == 0) &&			\
4990c39d38dSGleb Smirnoff 	    (tlen <= tp->t_maxseg) &&					\
500603724d3SBjoern A. Zeeb 	    (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN)))
501d8c85a26SJonathan Lemon 
50264807b30SHiren Panchasara static void inline
50364807b30SHiren Panchasara cc_ecnpkt_handler(struct tcpcb *tp, struct tcphdr *th, uint8_t iptos)
50464807b30SHiren Panchasara {
50564807b30SHiren Panchasara 	INP_WLOCK_ASSERT(tp->t_inpcb);
50664807b30SHiren Panchasara 
50764807b30SHiren Panchasara 	if (CC_ALGO(tp)->ecnpkt_handler != NULL) {
50864807b30SHiren Panchasara 		switch (iptos & IPTOS_ECN_MASK) {
50964807b30SHiren Panchasara 		case IPTOS_ECN_CE:
51064807b30SHiren Panchasara 		    tp->ccv->flags |= CCF_IPHDR_CE;
51164807b30SHiren Panchasara 		    break;
51264807b30SHiren Panchasara 		case IPTOS_ECN_ECT0:
51364807b30SHiren Panchasara 		    tp->ccv->flags &= ~CCF_IPHDR_CE;
51464807b30SHiren Panchasara 		    break;
51564807b30SHiren Panchasara 		case IPTOS_ECN_ECT1:
51664807b30SHiren Panchasara 		    tp->ccv->flags &= ~CCF_IPHDR_CE;
51764807b30SHiren Panchasara 		    break;
51864807b30SHiren Panchasara 		}
51964807b30SHiren Panchasara 
52064807b30SHiren Panchasara 		if (th->th_flags & TH_CWR)
52164807b30SHiren Panchasara 			tp->ccv->flags |= CCF_TCPHDR_CWR;
52264807b30SHiren Panchasara 		else
52364807b30SHiren Panchasara 			tp->ccv->flags &= ~CCF_TCPHDR_CWR;
52464807b30SHiren Panchasara 
52564807b30SHiren Panchasara 		if (tp->t_flags & TF_DELACK)
52664807b30SHiren Panchasara 			tp->ccv->flags |= CCF_DELACK;
52764807b30SHiren Panchasara 		else
52864807b30SHiren Panchasara 			tp->ccv->flags &= ~CCF_DELACK;
52964807b30SHiren Panchasara 
53064807b30SHiren Panchasara 		CC_ALGO(tp)->ecnpkt_handler(tp->ccv);
53164807b30SHiren Panchasara 
53264807b30SHiren Panchasara 		if (tp->ccv->flags & CCF_ACKNOW)
53364807b30SHiren Panchasara 			tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
53464807b30SHiren Panchasara 	}
53564807b30SHiren Panchasara }
53664807b30SHiren Panchasara 
537df8bae1dSRodney W. Grimes /*
5388d573cc1SAndre Oppermann  * TCP input handling is split into multiple parts:
5398d573cc1SAndre Oppermann  *   tcp6_input is a thin wrapper around tcp_input for the extended
5408d573cc1SAndre Oppermann  *	ip6_protox[] call format in ip6_input
5418d573cc1SAndre Oppermann  *   tcp_input handles primary segment validation, inpcb lookup and
5428d573cc1SAndre Oppermann  *	SYN processing on listen sockets
5438d573cc1SAndre Oppermann  *   tcp_do_segment processes the ACK and text of the segment for
5448d573cc1SAndre Oppermann  *	establishing, established and closing connections
545df8bae1dSRodney W. Grimes  */
546fb59c426SYoshinobu Inoue #ifdef INET6
547fb59c426SYoshinobu Inoue int
548ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto)
549fb59c426SYoshinobu Inoue {
550ad3f9ab3SAndre Oppermann 	struct mbuf *m = *mp;
55133841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6;
5523e88eb90SAndrey V. Elsukov 	struct ip6_hdr *ip6;
553fb59c426SYoshinobu Inoue 
554fb59c426SYoshinobu Inoue 	IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE);
555fb59c426SYoshinobu Inoue 
556fb59c426SYoshinobu Inoue 	/*
557fb59c426SYoshinobu Inoue 	 * draft-itojun-ipv6-tcp-to-anycast
558fb59c426SYoshinobu Inoue 	 * better place to put this in?
559fb59c426SYoshinobu Inoue 	 */
5603e88eb90SAndrey V. Elsukov 	ip6 = mtod(m, struct ip6_hdr *);
5613e88eb90SAndrey V. Elsukov 	ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
56233841545SHajimu UMEMOTO 	if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
563fb59c426SYoshinobu Inoue 		struct ip6_hdr *ip6;
564fb59c426SYoshinobu Inoue 
5658c0fec80SRobert Watson 		ifa_free(&ia6->ia_ifa);
566fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
567fb59c426SYoshinobu Inoue 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
568fb59c426SYoshinobu Inoue 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
5698f5a8818SKevin Lo 		return (IPPROTO_DONE);
570fb59c426SYoshinobu Inoue 	}
57177d396fdSMaksim Yevmenkin 	if (ia6)
57277d396fdSMaksim Yevmenkin 		ifa_free(&ia6->ia_ifa);
573fb59c426SYoshinobu Inoue 
5748f5a8818SKevin Lo 	return (tcp_input(mp, offp, proto));
575fb59c426SYoshinobu Inoue }
576b287c6c7SBjoern A. Zeeb #endif /* INET6 */
577fb59c426SYoshinobu Inoue 
5788f5a8818SKevin Lo int
5798f5a8818SKevin Lo tcp_input(struct mbuf **mp, int *offp, int proto)
580df8bae1dSRodney W. Grimes {
5818f5a8818SKevin Lo 	struct mbuf *m = *mp;
582b287c6c7SBjoern A. Zeeb 	struct tcphdr *th = NULL;
583ad3f9ab3SAndre Oppermann 	struct ip *ip = NULL;
584ad3f9ab3SAndre Oppermann 	struct inpcb *inp = NULL;
585302ce8d6SAndre Oppermann 	struct tcpcb *tp = NULL;
586302ce8d6SAndre Oppermann 	struct socket *so = NULL;
587e79adb8eSGarrett Wollman 	u_char *optp = NULL;
5888f5a8818SKevin Lo 	int off0;
58926f9a767SRodney W. Grimes 	int optlen = 0;
590b287c6c7SBjoern A. Zeeb #ifdef INET
591b287c6c7SBjoern A. Zeeb 	int len;
592b287c6c7SBjoern A. Zeeb #endif
593b287c6c7SBjoern A. Zeeb 	int tlen = 0, off;
594fb59c426SYoshinobu Inoue 	int drop_hdrlen;
595ad3f9ab3SAndre Oppermann 	int thflags;
596302ce8d6SAndre Oppermann 	int rstreason = 0;	/* For badport_bandlim accounting purposes */
5971d7ee746SKurt Lidl 	uint8_t iptos;
598c1de64a4SAndrey V. Elsukov 	struct m_tag *fwd_tag = NULL;
599c068736aSJeffrey Hsu #ifdef INET6
600302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6 = NULL;
601c068736aSJeffrey Hsu 	int isipv6;
602c068736aSJeffrey Hsu #else
603a160e630SAndre Oppermann 	const void *ip6 = NULL;
604b287c6c7SBjoern A. Zeeb #endif /* INET6 */
605302ce8d6SAndre Oppermann 	struct tcpopt to;		/* options in this segment */
606a160e630SAndre Oppermann 	char *s = NULL;			/* address and port logging */
607252ca428SRobert Watson 	int ti_locked;
608610ee2f9SDavid Greenman #ifdef TCPDEBUG
609c068736aSJeffrey Hsu 	/*
610c068736aSJeffrey Hsu 	 * The size of tcp_saveipgen must be the size of the max ip header,
611c068736aSJeffrey Hsu 	 * now IPv6.
612c068736aSJeffrey Hsu 	 */
61314739780SMaxim Konovalov 	u_char tcp_saveipgen[IP6_HDR_LEN];
614410bb1bfSLuigi Rizzo 	struct tcphdr tcp_savetcp;
615610ee2f9SDavid Greenman 	short ostate = 0;
616610ee2f9SDavid Greenman #endif
617c068736aSJeffrey Hsu 
618fb59c426SYoshinobu Inoue #ifdef INET6
619fb59c426SYoshinobu Inoue 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
620fb59c426SYoshinobu Inoue #endif
621a0292f23SGarrett Wollman 
6228f5a8818SKevin Lo 	off0 = *offp;
6238f5a8818SKevin Lo 	m = *mp;
6248f5a8818SKevin Lo 	*mp = NULL;
625302ce8d6SAndre Oppermann 	to.to_flags = 0;
62678b50714SRobert Watson 	TCPSTAT_INC(tcps_rcvtotal);
627fb59c426SYoshinobu Inoue 
62804d3a452SHajimu UMEMOTO #ifdef INET6
629b287c6c7SBjoern A. Zeeb 	if (isipv6) {
6308d573cc1SAndre Oppermann 		/* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */
63145747ba5SBjoern A. Zeeb 
63245747ba5SBjoern A. Zeeb 		if (m->m_len < (sizeof(*ip6) + sizeof(*th))) {
63345747ba5SBjoern A. Zeeb 			m = m_pullup(m, sizeof(*ip6) + sizeof(*th));
63445747ba5SBjoern A. Zeeb 			if (m == NULL) {
63545747ba5SBjoern A. Zeeb 				TCPSTAT_INC(tcps_rcvshort);
6368f5a8818SKevin Lo 				return (IPPROTO_DONE);
63745747ba5SBjoern A. Zeeb 			}
63845747ba5SBjoern A. Zeeb 		}
63945747ba5SBjoern A. Zeeb 
640fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
64145747ba5SBjoern A. Zeeb 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
642fb59c426SYoshinobu Inoue 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
643356ab07eSBjoern A. Zeeb 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) {
64445747ba5SBjoern A. Zeeb 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
64545747ba5SBjoern A. Zeeb 				th->th_sum = m->m_pkthdr.csum_data;
64645747ba5SBjoern A. Zeeb 			else
64745747ba5SBjoern A. Zeeb 				th->th_sum = in6_cksum_pseudo(ip6, tlen,
64845747ba5SBjoern A. Zeeb 				    IPPROTO_TCP, m->m_pkthdr.csum_data);
64945747ba5SBjoern A. Zeeb 			th->th_sum ^= 0xffff;
65045747ba5SBjoern A. Zeeb 		} else
65145747ba5SBjoern A. Zeeb 			th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen);
65245747ba5SBjoern A. Zeeb 		if (th->th_sum) {
65378b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
654fb59c426SYoshinobu Inoue 			goto drop;
655fb59c426SYoshinobu Inoue 		}
65633841545SHajimu UMEMOTO 
65733841545SHajimu UMEMOTO 		/*
65833841545SHajimu UMEMOTO 		 * Be proactive about unspecified IPv6 address in source.
65933841545SHajimu UMEMOTO 		 * As we use all-zero to indicate unbounded/unconnected pcb,
66033841545SHajimu UMEMOTO 		 * unspecified IPv6 address can be used to confuse us.
66133841545SHajimu UMEMOTO 		 *
66233841545SHajimu UMEMOTO 		 * Note that packets with unspecified IPv6 destination is
66333841545SHajimu UMEMOTO 		 * already dropped in ip6_input.
66433841545SHajimu UMEMOTO 		 */
66533841545SHajimu UMEMOTO 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
66633841545SHajimu UMEMOTO 			/* XXX stat */
66733841545SHajimu UMEMOTO 			goto drop;
66833841545SHajimu UMEMOTO 		}
6691d7ee746SKurt Lidl 		iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
670b287c6c7SBjoern A. Zeeb 	}
67104d3a452SHajimu UMEMOTO #endif
672b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
673b287c6c7SBjoern A. Zeeb 	else
674b287c6c7SBjoern A. Zeeb #endif
675b287c6c7SBjoern A. Zeeb #ifdef INET
676b287c6c7SBjoern A. Zeeb 	{
677df8bae1dSRodney W. Grimes 		/*
678df8bae1dSRodney W. Grimes 		 * Get IP and TCP header together in first mbuf.
679df8bae1dSRodney W. Grimes 		 * Note: IP leaves IP header in first mbuf.
680df8bae1dSRodney W. Grimes 		 */
681fb59c426SYoshinobu Inoue 		if (off0 > sizeof (struct ip)) {
682105bd211SGleb Smirnoff 			ip_stripoptions(m);
683fb59c426SYoshinobu Inoue 			off0 = sizeof(struct ip);
684fb59c426SYoshinobu Inoue 		}
685df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof (struct tcpiphdr)) {
6864dfdffe9SAndre Oppermann 			if ((m = m_pullup(m, sizeof (struct tcpiphdr)))
6874dfdffe9SAndre Oppermann 			    == NULL) {
68878b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvshort);
6898f5a8818SKevin Lo 				return (IPPROTO_DONE);
690df8bae1dSRodney W. Grimes 			}
691df8bae1dSRodney W. Grimes 		}
692fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
693db4f9cc7SJonathan Lemon 		th = (struct tcphdr *)((caddr_t)ip + off0);
6948ad458a4SGleb Smirnoff 		tlen = ntohs(ip->ip_len) - off0;
695df8bae1dSRodney W. Grimes 
6961d7ee746SKurt Lidl 		iptos = ip->ip_tos;
697db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
698db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
699db4f9cc7SJonathan Lemon 				th->th_sum = m->m_pkthdr.csum_data;
700db4f9cc7SJonathan Lemon 			else
701db4f9cc7SJonathan Lemon 				th->th_sum = in_pseudo(ip->ip_src.s_addr,
702c068736aSJeffrey Hsu 				    ip->ip_dst.s_addr,
7038f134647SGleb Smirnoff 				    htonl(m->m_pkthdr.csum_data + tlen +
704c068736aSJeffrey Hsu 				    IPPROTO_TCP));
705db4f9cc7SJonathan Lemon 			th->th_sum ^= 0xffff;
706db4f9cc7SJonathan Lemon 		} else {
7078f134647SGleb Smirnoff 			struct ipovly *ipov = (struct ipovly *)ip;
7088f134647SGleb Smirnoff 
709df8bae1dSRodney W. Grimes 			/*
710df8bae1dSRodney W. Grimes 			 * Checksum extended TCP header and data.
711df8bae1dSRodney W. Grimes 			 */
7128f134647SGleb Smirnoff 			len = off0 + tlen;
713fb59c426SYoshinobu Inoue 			bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
7148f134647SGleb Smirnoff 			ipov->ih_len = htons(tlen);
715fb59c426SYoshinobu Inoue 			th->th_sum = in_cksum(m, len);
71657f60867SMark Johnston 			/* Reset length for SDT probes. */
7171d7ee746SKurt Lidl 			ip->ip_len = htons(len);
7181d7ee746SKurt Lidl 			/* Reset TOS bits */
7191d7ee746SKurt Lidl 			ip->ip_tos = iptos;
7201d7ee746SKurt Lidl 			/* Re-initialization for later version check */
7211d7ee746SKurt Lidl 			ip->ip_v = IPVERSION;
722db4f9cc7SJonathan Lemon 		}
72357f60867SMark Johnston 
724fb59c426SYoshinobu Inoue 		if (th->th_sum) {
72578b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
726df8bae1dSRodney W. Grimes 			goto drop;
727df8bae1dSRodney W. Grimes 		}
728fb59c426SYoshinobu Inoue 	}
729b287c6c7SBjoern A. Zeeb #endif /* INET */
730df8bae1dSRodney W. Grimes 
731df8bae1dSRodney W. Grimes 	/*
732df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
733df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
734df8bae1dSRodney W. Grimes 	 */
735fb59c426SYoshinobu Inoue 	off = th->th_off << 2;
736df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
73778b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvbadoff);
738df8bae1dSRodney W. Grimes 		goto drop;
739df8bae1dSRodney W. Grimes 	}
740fb59c426SYoshinobu Inoue 	tlen -= off;	/* tlen is used instead of ti->ti_len */
741df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
74204d3a452SHajimu UMEMOTO #ifdef INET6
743b287c6c7SBjoern A. Zeeb 		if (isipv6) {
7448f5a8818SKevin Lo 			IP6_EXTHDR_CHECK(m, off0, off, IPPROTO_DONE);
745fb59c426SYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
746fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
747b287c6c7SBjoern A. Zeeb 		}
74804d3a452SHajimu UMEMOTO #endif
749b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
750b287c6c7SBjoern A. Zeeb 		else
751b287c6c7SBjoern A. Zeeb #endif
752b287c6c7SBjoern A. Zeeb #ifdef INET
753b287c6c7SBjoern A. Zeeb 		{
754df8bae1dSRodney W. Grimes 			if (m->m_len < sizeof(struct ip) + off) {
755c068736aSJeffrey Hsu 				if ((m = m_pullup(m, sizeof (struct ip) + off))
7564dfdffe9SAndre Oppermann 				    == NULL) {
75778b50714SRobert Watson 					TCPSTAT_INC(tcps_rcvshort);
7588f5a8818SKevin Lo 					return (IPPROTO_DONE);
759df8bae1dSRodney W. Grimes 				}
760fb59c426SYoshinobu Inoue 				ip = mtod(m, struct ip *);
761fb59c426SYoshinobu Inoue 				th = (struct tcphdr *)((caddr_t)ip + off0);
762fb59c426SYoshinobu Inoue 			}
763df8bae1dSRodney W. Grimes 		}
764b287c6c7SBjoern A. Zeeb #endif
765df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
766fb59c426SYoshinobu Inoue 		optp = (u_char *)(th + 1);
767df8bae1dSRodney W. Grimes 	}
768fb59c426SYoshinobu Inoue 	thflags = th->th_flags;
769df8bae1dSRodney W. Grimes 
770e46cd3d4SDag-Erling Smørgrav 	/*
771df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
772df8bae1dSRodney W. Grimes 	 */
7732903309aSAttilio Rao 	tcp_fields_to_host(th);
774df8bae1dSRodney W. Grimes 
775df8bae1dSRodney W. Grimes 	/*
776794235b7SAndre Oppermann 	 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options.
777755c1f07SAndras Olah 	 */
778fb59c426SYoshinobu Inoue 	drop_hdrlen = off0 + off;
779755c1f07SAndras Olah 
780755c1f07SAndras Olah 	/*
781fa046d87SRobert Watson 	 * Locate pcb for segment; if we're likely to add or remove a
782a7c7f2a7SJohn Baldwin 	 * connection then first acquire pcbinfo lock.  There are three cases
783fa046d87SRobert Watson 	 * where we might discover later we need a write lock despite the
784a7c7f2a7SJohn Baldwin 	 * flags: ACKs moving a connection out of the syncache, ACKs for a
785a7c7f2a7SJohn Baldwin 	 * connection in TIMEWAIT and SYNs not targeting a listening socket.
786df8bae1dSRodney W. Grimes 	 */
787a7c7f2a7SJohn Baldwin 	if ((thflags & (TH_FIN | TH_RST)) != 0) {
788ff9b006dSJulien Charbon 		INP_INFO_RLOCK(&V_tcbinfo);
789ff9b006dSJulien Charbon 		ti_locked = TI_RLOCKED;
790fa046d87SRobert Watson 	} else
791fa046d87SRobert Watson 		ti_locked = TI_UNLOCKED;
792252ca428SRobert Watson 
7938d573cc1SAndre Oppermann 	/*
7948d573cc1SAndre Oppermann 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
7958d573cc1SAndre Oppermann 	 */
796b8056faeSGleb Smirnoff         if (
797b8056faeSGleb Smirnoff #ifdef INET6
798b8056faeSGleb Smirnoff 	    (isipv6 && (m->m_flags & M_IP6_NEXTHOP))
799b8056faeSGleb Smirnoff #ifdef INET
800b8056faeSGleb Smirnoff 	    || (!isipv6 && (m->m_flags & M_IP_NEXTHOP))
801b8056faeSGleb Smirnoff #endif
802b8056faeSGleb Smirnoff #endif
803b8056faeSGleb Smirnoff #if defined(INET) && !defined(INET6)
804b8056faeSGleb Smirnoff 	    (m->m_flags & M_IP_NEXTHOP)
805b8056faeSGleb Smirnoff #endif
806b8056faeSGleb Smirnoff 	    )
8079b932e9eSAndre Oppermann 		fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
8089b932e9eSAndre Oppermann 
809ce7ad664SEd Maste findpcb:
810ce7ad664SEd Maste #ifdef INVARIANTS
811ff9b006dSJulien Charbon 	if (ti_locked == TI_RLOCKED) {
812ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
813ce7ad664SEd Maste 	} else {
814ce7ad664SEd Maste 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
815ce7ad664SEd Maste 	}
816ce7ad664SEd Maste #endif
8178a006adbSBjoern A. Zeeb #ifdef INET6
8188a006adbSBjoern A. Zeeb 	if (isipv6 && fwd_tag != NULL) {
8198a006adbSBjoern A. Zeeb 		struct sockaddr_in6 *next_hop6;
8208a006adbSBjoern A. Zeeb 
8218a006adbSBjoern A. Zeeb 		next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
8228a006adbSBjoern A. Zeeb 		/*
8238a006adbSBjoern A. Zeeb 		 * Transparently forwarded. Pretend to be the destination.
8248a006adbSBjoern A. Zeeb 		 * Already got one like this?
8258a006adbSBjoern A. Zeeb 		 */
8268a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo,
8278a006adbSBjoern A. Zeeb 		    &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport,
8288a006adbSBjoern A. Zeeb 		    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif, m);
8298a006adbSBjoern A. Zeeb 		if (!inp) {
8308a006adbSBjoern A. Zeeb 			/*
8318a006adbSBjoern A. Zeeb 			 * It's new.  Try to find the ambushing socket.
8328a006adbSBjoern A. Zeeb 			 * Because we've rewritten the destination address,
8338a006adbSBjoern A. Zeeb 			 * any hardware-generated hash is ignored.
8348a006adbSBjoern A. Zeeb 			 */
8358a006adbSBjoern A. Zeeb 			inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src,
8368a006adbSBjoern A. Zeeb 			    th->th_sport, &next_hop6->sin6_addr,
8378a006adbSBjoern A. Zeeb 			    next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) :
8388a006adbSBjoern A. Zeeb 			    th->th_dport, INPLOOKUP_WILDCARD |
8398a006adbSBjoern A. Zeeb 			    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif);
8408a006adbSBjoern A. Zeeb 		}
841c1de64a4SAndrey V. Elsukov 	} else if (isipv6) {
8428a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src,
8438a006adbSBjoern A. Zeeb 		    th->th_sport, &ip6->ip6_dst, th->th_dport,
8448a006adbSBjoern A. Zeeb 		    INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB,
8458a006adbSBjoern A. Zeeb 		    m->m_pkthdr.rcvif, m);
8468a006adbSBjoern A. Zeeb 	}
8478a006adbSBjoern A. Zeeb #endif /* INET6 */
8488a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET)
8498a006adbSBjoern A. Zeeb 	else
8508a006adbSBjoern A. Zeeb #endif
8518a006adbSBjoern A. Zeeb #ifdef INET
8528a006adbSBjoern A. Zeeb 	if (fwd_tag != NULL) {
8539b932e9eSAndre Oppermann 		struct sockaddr_in *next_hop;
8549b932e9eSAndre Oppermann 
8559b932e9eSAndre Oppermann 		next_hop = (struct sockaddr_in *)(fwd_tag+1);
856f9e354dfSJulian Elischer 		/*
8572b25acc1SLuigi Rizzo 		 * Transparently forwarded. Pretend to be the destination.
858f9e354dfSJulian Elischer 		 * already got one like this?
859f9e354dfSJulian Elischer 		 */
860d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport,
861fa046d87SRobert Watson 		    ip->ip_dst, th->th_dport, INPLOOKUP_WLOCKPCB,
862d3c1f003SRobert Watson 		    m->m_pkthdr.rcvif, m);
863fa046d87SRobert Watson 		if (!inp) {
864fa046d87SRobert Watson 			/*
865fa046d87SRobert Watson 			 * It's new.  Try to find the ambushing socket.
866d3c1f003SRobert Watson 			 * Because we've rewritten the destination address,
867d3c1f003SRobert Watson 			 * any hardware-generated hash is ignored.
868fa046d87SRobert Watson 			 */
869fa046d87SRobert Watson 			inp = in_pcblookup(&V_tcbinfo, ip->ip_src,
870fa046d87SRobert Watson 			    th->th_sport, next_hop->sin_addr,
871fa046d87SRobert Watson 			    next_hop->sin_port ? ntohs(next_hop->sin_port) :
872fa046d87SRobert Watson 			    th->th_dport, INPLOOKUP_WILDCARD |
873fa046d87SRobert Watson 			    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif);
874f9e354dfSJulian Elischer 		}
875b10fbdeaSAndre Oppermann 	} else
876d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src,
877fa046d87SRobert Watson 		    th->th_sport, ip->ip_dst, th->th_dport,
878fa046d87SRobert Watson 		    INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB,
879d3c1f003SRobert Watson 		    m->m_pkthdr.rcvif, m);
8808a006adbSBjoern A. Zeeb #endif /* INET */
881f9e354dfSJulian Elischer 
882df8bae1dSRodney W. Grimes 	/*
883574b6964SAndre Oppermann 	 * If the INPCB does not exist then all data in the incoming
884574b6964SAndre Oppermann 	 * segment is discarded and an appropriate RST is sent back.
8858b07e49aSJulian Elischer 	 * XXX MRT Send RST using which routing table?
886df8bae1dSRodney W. Grimes 	 */
887816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
888574b6964SAndre Oppermann 		/*
889574b6964SAndre Oppermann 		 * Log communication attempts to ports that are not
890574b6964SAndre Oppermann 		 * in use.
891574b6964SAndre Oppermann 		 */
892574b6964SAndre Oppermann 		if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) ||
893574b6964SAndre Oppermann 		    tcp_log_in_vain == 2) {
894b7d747ecSAndre Oppermann 			if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6)))
895df541e5fSAndre Oppermann 				log(LOG_INFO, "%s; %s: Connection attempt "
896df541e5fSAndre Oppermann 				    "to closed port\n", s, __func__);
8972e4e1b4cSGeoff Rehmet 		}
898574b6964SAndre Oppermann 		/*
899574b6964SAndre Oppermann 		 * When blackholing do not respond with a RST but
900574b6964SAndre Oppermann 		 * completely ignore the segment and drop it.
901574b6964SAndre Oppermann 		 */
902603724d3SBjoern A. Zeeb 		if ((V_blackhole == 1 && (thflags & TH_SYN)) ||
903603724d3SBjoern A. Zeeb 		    V_blackhole == 2)
9041929eae1SAndre Oppermann 			goto dropunlock;
905574b6964SAndre Oppermann 
906a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
907a57815efSBosko Milekic 		goto dropwithreset;
908816a3d83SPoul-Henning Kamp 	}
909fa046d87SRobert Watson 	INP_WLOCK_ASSERT(inp);
910f5cf1e5fSJulien Charbon 	/*
911f5cf1e5fSJulien Charbon 	 * While waiting for inp lock during the lookup, another thread
912f5cf1e5fSJulien Charbon 	 * can have dropped the inpcb, in which case we need to loop back
913f5cf1e5fSJulien Charbon 	 * and try to find a new inpcb to deliver to.
914f5cf1e5fSJulien Charbon 	 */
915f5cf1e5fSJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
916f5cf1e5fSJulien Charbon 		INP_WUNLOCK(inp);
917f5cf1e5fSJulien Charbon 		inp = NULL;
918f5cf1e5fSJulien Charbon 		goto findpcb;
919f5cf1e5fSJulien Charbon 	}
920c2529042SHans Petter Selasky 	if ((inp->inp_flowtype == M_HASHTYPE_NONE) &&
921c2529042SHans Petter Selasky 	    (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) &&
922c2529042SHans Petter Selasky 	    ((inp->inp_socket == NULL) ||
923c2529042SHans Petter Selasky 	    (inp->inp_socket->so_options & SO_ACCEPTCONN) == 0)) {
92480cb9f21SKip Macy 		inp->inp_flowid = m->m_pkthdr.flowid;
9252f719932SAdrian Chadd 		inp->inp_flowtype = M_HASHTYPE_GET(m);
92680cb9f21SKip Macy 	}
927fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
928a2589465SKen Smith #ifdef INET6
929fcf59617SAndrey V. Elsukov 	if (isipv6 && IPSEC_ENABLED(ipv6) &&
930fcf59617SAndrey V. Elsukov 	    IPSEC_CHECK_POLICY(ipv6, m, inp) != 0) {
931a2589465SKen Smith 		goto dropunlock;
932a2589465SKen Smith 	}
933fcf59617SAndrey V. Elsukov #ifdef INET
934fcf59617SAndrey V. Elsukov 	else
935fcf59617SAndrey V. Elsukov #endif
936fcf59617SAndrey V. Elsukov #endif /* INET6 */
937fcf59617SAndrey V. Elsukov #ifdef INET
938fcf59617SAndrey V. Elsukov 	if (IPSEC_ENABLED(ipv4) &&
939fcf59617SAndrey V. Elsukov 	    IPSEC_CHECK_POLICY(ipv4, m, inp) != 0) {
940fcf59617SAndrey V. Elsukov 		goto dropunlock;
941fcf59617SAndrey V. Elsukov 	}
942fcf59617SAndrey V. Elsukov #endif /* INET */
943a2589465SKen Smith #endif /* IPSEC */
944a2589465SKen Smith 
9458d573cc1SAndre Oppermann 	/*
9468d573cc1SAndre Oppermann 	 * Check the minimum TTL for socket.
9478d573cc1SAndre Oppermann 	 */
94834f83c52SGeorge V. Neville-Neil 	if (inp->inp_ip_minttl != 0) {
94934f83c52SGeorge V. Neville-Neil #ifdef INET6
9502d8868dbSJonathan T. Looney 		if (isipv6) {
9512d8868dbSJonathan T. Looney 			if (inp->inp_ip_minttl > ip6->ip6_hlim)
952302ce8d6SAndre Oppermann 				goto dropunlock;
9532d8868dbSJonathan T. Looney 		} else
95434f83c52SGeorge V. Neville-Neil #endif
95534f83c52SGeorge V. Neville-Neil 		if (inp->inp_ip_minttl > ip->ip_ttl)
956302ce8d6SAndre Oppermann 			goto dropunlock;
95734f83c52SGeorge V. Neville-Neil 	}
958936cd18dSAndre Oppermann 
959340c35deSJonathan Lemon 	/*
960252ca428SRobert Watson 	 * A previous connection in TIMEWAIT state is supposed to catch stray
961252ca428SRobert Watson 	 * or duplicate segments arriving late.  If this segment was a
9620989f56cSRobert Watson 	 * legitimate new connection attempt, the old INPCB gets removed and
963252ca428SRobert Watson 	 * we can try again to find a listening socket.
964252ca428SRobert Watson 	 *
965fa046d87SRobert Watson 	 * At this point, due to earlier optimism, we may hold only an inpcb
966fa046d87SRobert Watson 	 * lock, and not the inpcbinfo write lock.  If so, we need to try to
967fa046d87SRobert Watson 	 * acquire it, or if that fails, acquire a reference on the inpcb,
968fa046d87SRobert Watson 	 * drop all locks, acquire a global write lock, and then re-acquire
969fa046d87SRobert Watson 	 * the inpcb lock.  We may at that point discover that another thread
970fa046d87SRobert Watson 	 * has tried to free the inpcb, in which case we need to loop back
971fa046d87SRobert Watson 	 * and try to find a new inpcb to deliver to.
972fa046d87SRobert Watson 	 *
973fa046d87SRobert Watson 	 * XXXRW: It may be time to rethink timewait locking.
974340c35deSJonathan Lemon 	 */
975883e9bc4SRobert Watson relocked:
976ad71fe3cSRobert Watson 	if (inp->inp_flags & INP_TIMEWAIT) {
977fa046d87SRobert Watson 		if (ti_locked == TI_UNLOCKED) {
978ff9b006dSJulien Charbon 			if (INP_INFO_TRY_RLOCK(&V_tcbinfo) == 0) {
979252ca428SRobert Watson 				in_pcbref(inp);
980252ca428SRobert Watson 				INP_WUNLOCK(inp);
981ff9b006dSJulien Charbon 				INP_INFO_RLOCK(&V_tcbinfo);
982ff9b006dSJulien Charbon 				ti_locked = TI_RLOCKED;
983252ca428SRobert Watson 				INP_WLOCK(inp);
984fa046d87SRobert Watson 				if (in_pcbrele_wlocked(inp)) {
985252ca428SRobert Watson 					inp = NULL;
986252ca428SRobert Watson 					goto findpcb;
987f5cf1e5fSJulien Charbon 				} else if (inp->inp_flags & INP_DROPPED) {
988f5cf1e5fSJulien Charbon 					INP_WUNLOCK(inp);
989f5cf1e5fSJulien Charbon 					inp = NULL;
990f5cf1e5fSJulien Charbon 					goto findpcb;
991252ca428SRobert Watson 				}
992f681a5fdSRobert Watson 			} else
993ff9b006dSJulien Charbon 				ti_locked = TI_RLOCKED;
994252ca428SRobert Watson 		}
995ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
996252ca428SRobert Watson 
997340c35deSJonathan Lemon 		if (thflags & TH_SYN)
998f72167f4SAndre Oppermann 			tcp_dooptions(&to, optp, optlen, TO_SYN);
9998d573cc1SAndre Oppermann 		/*
10008d573cc1SAndre Oppermann 		 * NB: tcp_twcheck unlocks the INP and frees the mbuf.
10018d573cc1SAndre Oppermann 		 */
10022104448fSAndre Oppermann 		if (tcp_twcheck(inp, &to, th, m, tlen))
1003340c35deSJonathan Lemon 			goto findpcb;
1004ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
10058f5a8818SKevin Lo 		return (IPPROTO_DONE);
1006340c35deSJonathan Lemon 	}
1007794235b7SAndre Oppermann 	/*
1008794235b7SAndre Oppermann 	 * The TCPCB may no longer exist if the connection is winding
1009794235b7SAndre Oppermann 	 * down or it is in the CLOSED state.  Either way we drop the
1010794235b7SAndre Oppermann 	 * segment and send an appropriate response.
1011794235b7SAndre Oppermann 	 */
1012df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
1013a160e630SAndre Oppermann 	if (tp == NULL || tp->t_state == TCPS_CLOSED) {
1014a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
1015a57815efSBosko Milekic 		goto dropwithreset;
101609f81a46SBosko Milekic 	}
1017df8bae1dSRodney W. Grimes 
101809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
101909fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
102009fe6320SNavdeep Parhar 		tcp_offload_input(tp, m);
102109fe6320SNavdeep Parhar 		m = NULL;	/* consumed by the TOE driver */
102209fe6320SNavdeep Parhar 		goto dropunlock;
102309fe6320SNavdeep Parhar 	}
102409fe6320SNavdeep Parhar #endif
102509fe6320SNavdeep Parhar 
1026252ca428SRobert Watson 	/*
1027252ca428SRobert Watson 	 * We've identified a valid inpcb, but it could be that we need an
1028fa046d87SRobert Watson 	 * inpcbinfo write lock but don't hold it.  In this case, attempt to
1029fa046d87SRobert Watson 	 * acquire using the same strategy as the TIMEWAIT case above.  If we
1030fa046d87SRobert Watson 	 * relock, we have to jump back to 'relocked' as the connection might
1031fa046d87SRobert Watson 	 * now be in TIMEWAIT.
1032252ca428SRobert Watson 	 */
1033fa046d87SRobert Watson #ifdef INVARIANTS
1034a7c7f2a7SJohn Baldwin 	if ((thflags & (TH_FIN | TH_RST)) != 0)
1035ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1036fa046d87SRobert Watson #endif
1037a7c7f2a7SJohn Baldwin 	if (!((tp->t_state == TCPS_ESTABLISHED && (thflags & TH_SYN) == 0) ||
1038281a0fd4SPatrick Kelsey 	      (tp->t_state == TCPS_LISTEN && (thflags & TH_SYN) &&
103968bd7ed1SJonathan T. Looney 	       !IS_FASTOPEN(tp->t_flags)))) {
1040fa046d87SRobert Watson 		if (ti_locked == TI_UNLOCKED) {
1041ff9b006dSJulien Charbon 			if (INP_INFO_TRY_RLOCK(&V_tcbinfo) == 0) {
1042252ca428SRobert Watson 				in_pcbref(inp);
1043252ca428SRobert Watson 				INP_WUNLOCK(inp);
1044ff9b006dSJulien Charbon 				INP_INFO_RLOCK(&V_tcbinfo);
1045ff9b006dSJulien Charbon 				ti_locked = TI_RLOCKED;
1046252ca428SRobert Watson 				INP_WLOCK(inp);
1047fa046d87SRobert Watson 				if (in_pcbrele_wlocked(inp)) {
1048252ca428SRobert Watson 					inp = NULL;
1049252ca428SRobert Watson 					goto findpcb;
1050f5cf1e5fSJulien Charbon 				} else if (inp->inp_flags & INP_DROPPED) {
1051f5cf1e5fSJulien Charbon 					INP_WUNLOCK(inp);
1052f5cf1e5fSJulien Charbon 					inp = NULL;
1053f5cf1e5fSJulien Charbon 					goto findpcb;
1054252ca428SRobert Watson 				}
1055883e9bc4SRobert Watson 				goto relocked;
1056f681a5fdSRobert Watson 			} else
1057ff9b006dSJulien Charbon 				ti_locked = TI_RLOCKED;
1058252ca428SRobert Watson 		}
1059ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1060252ca428SRobert Watson 	}
1061252ca428SRobert Watson 
1062c488362eSRobert Watson #ifdef MAC
10638501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
106430d239bcSRobert Watson 	if (mac_inpcb_check_deliver(inp, m))
1065302ce8d6SAndre Oppermann 		goto dropunlock;
1066c488362eSRobert Watson #endif
1067a557af22SRobert Watson 	so = inp->inp_socket;
1068302ce8d6SAndre Oppermann 	KASSERT(so != NULL, ("%s: so == NULL", __func__));
1069610ee2f9SDavid Greenman #ifdef TCPDEBUG
1070df8bae1dSRodney W. Grimes 	if (so->so_options & SO_DEBUG) {
1071df8bae1dSRodney W. Grimes 		ostate = tp->t_state;
107210fe523eSMaxim Konovalov #ifdef INET6
10736f697424SBjoern A. Zeeb 		if (isipv6) {
1074540e8b7eSJeffrey Hsu 			bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6));
1075d30d90dcSMaxim Konovalov 		} else
10766f697424SBjoern A. Zeeb #endif
1077540e8b7eSJeffrey Hsu 			bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
1078fb59c426SYoshinobu Inoue 		tcp_savetcp = *th;
1079df8bae1dSRodney W. Grimes 	}
1080b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */
1081db33b3e6SAndre Oppermann 	/*
1082db33b3e6SAndre Oppermann 	 * When the socket is accepting connections (the INPCB is in LISTEN
1083db33b3e6SAndre Oppermann 	 * state) we look into the SYN cache if this is a new connection
1084a7c7f2a7SJohn Baldwin 	 * attempt or the completion of a previous one.
1085db33b3e6SAndre Oppermann 	 */
108645274760SJonathan T. Looney 	KASSERT(tp->t_state == TCPS_LISTEN || !(so->so_options & SO_ACCEPTCONN),
108745274760SJonathan T. Looney 	    ("%s: so accepting but tp %p not listening", __func__, tp));
108845274760SJonathan T. Looney 	if (tp->t_state == TCPS_LISTEN && (so->so_options & SO_ACCEPTCONN)) {
1089540e8b7eSJeffrey Hsu 		struct in_conninfo inc;
1090540e8b7eSJeffrey Hsu 
10918bfb1918SAndre Oppermann 		bzero(&inc, sizeof(inc));
1092302ce8d6SAndre Oppermann #ifdef INET6
1093be2ac88cSJonathan Lemon 		if (isipv6) {
1094dcdb4371SBjoern A. Zeeb 			inc.inc_flags |= INC_ISIPV6;
1095be2ac88cSJonathan Lemon 			inc.inc6_faddr = ip6->ip6_src;
1096be2ac88cSJonathan Lemon 			inc.inc6_laddr = ip6->ip6_dst;
1097302ce8d6SAndre Oppermann 		} else
1098302ce8d6SAndre Oppermann #endif
1099302ce8d6SAndre Oppermann 		{
1100be2ac88cSJonathan Lemon 			inc.inc_faddr = ip->ip_src;
1101be2ac88cSJonathan Lemon 			inc.inc_laddr = ip->ip_dst;
1102be2ac88cSJonathan Lemon 		}
1103be2ac88cSJonathan Lemon 		inc.inc_fport = th->th_sport;
1104be2ac88cSJonathan Lemon 		inc.inc_lport = th->th_dport;
11057973fba3SJulian Elischer 		inc.inc_fibnum = so->so_fibnum;
1106be2ac88cSJonathan Lemon 
1107fb59c426SYoshinobu Inoue 		/*
11088d573cc1SAndre Oppermann 		 * Check for an existing connection attempt in syncache if
11098d573cc1SAndre Oppermann 		 * the flag is only ACK.  A successful lookup creates a new
11108d573cc1SAndre Oppermann 		 * socket appended to the listen queue in SYN_RECEIVED state.
1111fb59c426SYoshinobu Inoue 		 */
1112be2ac88cSJonathan Lemon 		if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
1113a7c7f2a7SJohn Baldwin 
1114ff9b006dSJulien Charbon 			INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1115bf6d304aSAndre Oppermann 			/*
1116bf6d304aSAndre Oppermann 			 * Parse the TCP options here because
1117bf6d304aSAndre Oppermann 			 * syncookies need access to the reflected
1118bf6d304aSAndre Oppermann 			 * timestamp.
1119bf6d304aSAndre Oppermann 			 */
1120bf6d304aSAndre Oppermann 			tcp_dooptions(&to, optp, optlen, 0);
11219fa198beSAndre Oppermann 			/*
11229fa198beSAndre Oppermann 			 * NB: syncache_expand() doesn't unlock
11239fa198beSAndre Oppermann 			 * inp and tcpinfo locks.
11249fa198beSAndre Oppermann 			 */
1125fcf59617SAndrey V. Elsukov 			rstreason = syncache_expand(&inc, &to, th, &so, m);
1126fcf59617SAndrey V. Elsukov 			if (rstreason < 0) {
1127fcf59617SAndrey V. Elsukov 				/*
1128fcf59617SAndrey V. Elsukov 				 * A failing TCP MD5 signature comparison
1129fcf59617SAndrey V. Elsukov 				 * must result in the segment being dropped
1130fcf59617SAndrey V. Elsukov 				 * and must not produce any response back
1131fcf59617SAndrey V. Elsukov 				 * to the sender.
1132fcf59617SAndrey V. Elsukov 				 */
1133fcf59617SAndrey V. Elsukov 				goto dropunlock;
1134fcf59617SAndrey V. Elsukov 			} else if (rstreason == 0) {
11354195b4afSPaul Traina 				/*
1136e207f800SAndre Oppermann 				 * No syncache entry or ACK was not
1137be2ac88cSJonathan Lemon 				 * for our SYN/ACK.  Send a RST.
11388d573cc1SAndre Oppermann 				 * NB: syncache did its own logging
11398d573cc1SAndre Oppermann 				 * of the failure cause.
11404195b4afSPaul Traina 				 */
1141be2ac88cSJonathan Lemon 				rstreason = BANDLIM_RST_OPENPORT;
1142be2ac88cSJonathan Lemon 				goto dropwithreset;
1143be2ac88cSJonathan Lemon 			}
1144281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
114509c305ebSPatrick Kelsey tfo_socket_result:
1146281a0fd4SPatrick Kelsey #endif
1147f76fcf6dSJeffrey Hsu 			if (so == NULL) {
1148be2ac88cSJonathan Lemon 				/*
1149e207f800SAndre Oppermann 				 * We completed the 3-way handshake
1150e207f800SAndre Oppermann 				 * but could not allocate a socket
1151e207f800SAndre Oppermann 				 * either due to memory shortage,
1152e207f800SAndre Oppermann 				 * listen queue length limits or
1153a160e630SAndre Oppermann 				 * global socket limits.  Send RST
1154a160e630SAndre Oppermann 				 * or wait and have the remote end
1155a160e630SAndre Oppermann 				 * retransmit the ACK for another
1156a160e630SAndre Oppermann 				 * try.
1157be2ac88cSJonathan Lemon 				 */
1158a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1159a160e630SAndre Oppermann 					log(LOG_DEBUG, "%s; %s: Listen socket: "
11608d573cc1SAndre Oppermann 					    "Socket allocation failed due to "
11618d573cc1SAndre Oppermann 					    "limits or memory shortage, %s\n",
1162ac957cd2SJulian Elischer 					    s, __func__,
1163ac957cd2SJulian Elischer 					    V_tcp_sc_rst_sock_fail ?
1164ac957cd2SJulian Elischer 					    "sending RST" : "try again");
1165603724d3SBjoern A. Zeeb 				if (V_tcp_sc_rst_sock_fail) {
1166e207f800SAndre Oppermann 					rstreason = BANDLIM_UNLIMITED;
1167e207f800SAndre Oppermann 					goto dropwithreset;
1168a160e630SAndre Oppermann 				} else
1169a160e630SAndre Oppermann 					goto dropunlock;
1170f76fcf6dSJeffrey Hsu 			}
1171be2ac88cSJonathan Lemon 			/*
1172be2ac88cSJonathan Lemon 			 * Socket is created in state SYN_RECEIVED.
11738d573cc1SAndre Oppermann 			 * Unlock the listen socket, lock the newly
11748d573cc1SAndre Oppermann 			 * created socket and update the tp variable.
1175be2ac88cSJonathan Lemon 			 */
11768501a69cSRobert Watson 			INP_WUNLOCK(inp);	/* listen socket */
1177be2ac88cSJonathan Lemon 			inp = sotoinpcb(so);
1178ff9b006dSJulien Charbon 			/*
1179ff9b006dSJulien Charbon 			 * New connection inpcb is already locked by
1180ff9b006dSJulien Charbon 			 * syncache_expand().
1181ff9b006dSJulien Charbon 			 */
1182ff9b006dSJulien Charbon 			INP_WLOCK_ASSERT(inp);
1183be2ac88cSJonathan Lemon 			tp = intotcpcb(inp);
11848d573cc1SAndre Oppermann 			KASSERT(tp->t_state == TCPS_SYN_RECEIVED,
11858d573cc1SAndre Oppermann 			    ("%s: ", __func__));
1186be2ac88cSJonathan Lemon 			/*
1187302ce8d6SAndre Oppermann 			 * Process the segment and the data it
1188302ce8d6SAndre Oppermann 			 * contains.  tcp_do_segment() consumes
1189302ce8d6SAndre Oppermann 			 * the mbuf chain and unlocks the inpcb.
1190302ce8d6SAndre Oppermann 			 */
119155bceb1eSRandall Stewart 			tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen,
1192252ca428SRobert Watson 			    iptos, ti_locked);
1193603724d3SBjoern A. Zeeb 			INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
11948f5a8818SKevin Lo 			return (IPPROTO_DONE);
1195be2ac88cSJonathan Lemon 		}
1196a160e630SAndre Oppermann 		/*
11978d573cc1SAndre Oppermann 		 * Segment flag validation for new connection attempts:
11988d573cc1SAndre Oppermann 		 *
1199a160e630SAndre Oppermann 		 * Our (SYN|ACK) response was rejected.
1200a160e630SAndre Oppermann 		 * Check with syncache and remove entry to prevent
1201a160e630SAndre Oppermann 		 * retransmits.
120219bc77c5SAndre Oppermann 		 *
120319bc77c5SAndre Oppermann 		 * NB: syncache_chkrst does its own logging of failure
120419bc77c5SAndre Oppermann 		 * causes.
1205a160e630SAndre Oppermann 		 */
1206a160e630SAndre Oppermann 		if (thflags & TH_RST) {
120719bc77c5SAndre Oppermann 			syncache_chkrst(&inc, th);
1208a160e630SAndre Oppermann 			goto dropunlock;
1209a160e630SAndre Oppermann 		}
1210a160e630SAndre Oppermann 		/*
1211a160e630SAndre Oppermann 		 * We can't do anything without SYN.
1212a160e630SAndre Oppermann 		 */
1213a160e630SAndre Oppermann 		if ((thflags & TH_SYN) == 0) {
1214a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1215a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1216773673c1SAndre Oppermann 				    "SYN is missing, segment ignored\n",
12178d573cc1SAndre Oppermann 				    s, __func__);
121878b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1219a160e630SAndre Oppermann 			goto dropunlock;
1220a160e630SAndre Oppermann 		}
1221a160e630SAndre Oppermann 		/*
1222a160e630SAndre Oppermann 		 * (SYN|ACK) is bogus on a listen socket.
1223a160e630SAndre Oppermann 		 */
1224fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1225a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1226a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
12278d573cc1SAndre Oppermann 				    "SYN|ACK invalid, segment rejected\n",
12288d573cc1SAndre Oppermann 				    s, __func__);
1229a160e630SAndre Oppermann 			syncache_badack(&inc);	/* XXX: Not needed! */
123078b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1231a57815efSBosko Milekic 			rstreason = BANDLIM_RST_OPENPORT;
1232a57815efSBosko Milekic 			goto dropwithreset;
12334195b4afSPaul Traina 		}
1234a160e630SAndre Oppermann 		/*
1235a160e630SAndre Oppermann 		 * If the drop_synfin option is enabled, drop all
1236a160e630SAndre Oppermann 		 * segments with both the SYN and FIN bits set.
1237a160e630SAndre Oppermann 		 * This prevents e.g. nmap from identifying the
1238a160e630SAndre Oppermann 		 * TCP/IP stack.
1239a160e630SAndre Oppermann 		 * XXX: Poor reasoning.  nmap has other methods
1240a160e630SAndre Oppermann 		 * and is constantly refining its stack detection
1241a160e630SAndre Oppermann 		 * strategies.
12428d573cc1SAndre Oppermann 		 * XXX: This is a violation of the TCP specification
1243a160e630SAndre Oppermann 		 * and was used by RFC1644.
1244a160e630SAndre Oppermann 		 */
1245603724d3SBjoern A. Zeeb 		if ((thflags & TH_FIN) && V_drop_synfin) {
1246a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1247a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1248773673c1SAndre Oppermann 				    "SYN|FIN segment ignored (based on "
12498d573cc1SAndre Oppermann 				    "sysctl setting)\n", s, __func__);
125078b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1251302ce8d6SAndre Oppermann 			goto dropunlock;
1252ebb0cbeaSPaul Traina 		}
1253be2ac88cSJonathan Lemon 		/*
1254be2ac88cSJonathan Lemon 		 * Segment's flags are (SYN) or (SYN|FIN).
1255a160e630SAndre Oppermann 		 *
1256a160e630SAndre Oppermann 		 * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored
1257a160e630SAndre Oppermann 		 * as they do not affect the state of the TCP FSM.
1258a160e630SAndre Oppermann 		 * The data pointed to by TH_URG and th_urp is ignored.
1259be2ac88cSJonathan Lemon 		 */
1260a160e630SAndre Oppermann 		KASSERT((thflags & (TH_RST|TH_ACK)) == 0,
1261a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_RST or TH_ACK set", __func__));
1262a160e630SAndre Oppermann 		KASSERT(thflags & (TH_SYN),
1263a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_SYN not set", __func__));
126433841545SHajimu UMEMOTO #ifdef INET6
126533841545SHajimu UMEMOTO 		/*
126633841545SHajimu UMEMOTO 		 * If deprecated address is forbidden,
126733841545SHajimu UMEMOTO 		 * we do not accept SYN to deprecated interface
126833841545SHajimu UMEMOTO 		 * address to prevent any new inbound connection from
126933841545SHajimu UMEMOTO 		 * getting established.
127033841545SHajimu UMEMOTO 		 * When we do not accept SYN, we send a TCP RST,
127133841545SHajimu UMEMOTO 		 * with deprecated source address (instead of dropping
127233841545SHajimu UMEMOTO 		 * it).  We compromise it as it is much better for peer
127333841545SHajimu UMEMOTO 		 * to send a RST, and RST will be the final packet
127433841545SHajimu UMEMOTO 		 * for the exchange.
127533841545SHajimu UMEMOTO 		 *
127633841545SHajimu UMEMOTO 		 * If we do not forbid deprecated addresses, we accept
127733841545SHajimu UMEMOTO 		 * the SYN packet.  RFC2462 does not suggest dropping
127833841545SHajimu UMEMOTO 		 * SYN in this case.
127933841545SHajimu UMEMOTO 		 * If we decipher RFC2462 5.5.4, it says like this:
128033841545SHajimu UMEMOTO 		 * 1. use of deprecated addr with existing
128133841545SHajimu UMEMOTO 		 *    communication is okay - "SHOULD continue to be
128233841545SHajimu UMEMOTO 		 *    used"
128333841545SHajimu UMEMOTO 		 * 2. use of it with new communication:
128433841545SHajimu UMEMOTO 		 *   (2a) "SHOULD NOT be used if alternate address
128533841545SHajimu UMEMOTO 		 *        with sufficient scope is available"
128633841545SHajimu UMEMOTO 		 *   (2b) nothing mentioned otherwise.
128733841545SHajimu UMEMOTO 		 * Here we fall into (2b) case as we have no choice in
128833841545SHajimu UMEMOTO 		 * our source address selection - we must obey the peer.
128933841545SHajimu UMEMOTO 		 *
129033841545SHajimu UMEMOTO 		 * The wording in RFC2462 is confusing, and there are
129133841545SHajimu UMEMOTO 		 * multiple description text for deprecated address
129233841545SHajimu UMEMOTO 		 * handling - worse, they are not exactly the same.
129333841545SHajimu UMEMOTO 		 * I believe 5.5.4 is the best one, so we follow 5.5.4.
129433841545SHajimu UMEMOTO 		 */
1295603724d3SBjoern A. Zeeb 		if (isipv6 && !V_ip6_use_deprecated) {
129633841545SHajimu UMEMOTO 			struct in6_ifaddr *ia6;
129733841545SHajimu UMEMOTO 
12983e88eb90SAndrey V. Elsukov 			ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
12998c0fec80SRobert Watson 			if (ia6 != NULL &&
130033841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
13018c0fec80SRobert Watson 				ifa_free(&ia6->ia_ifa);
1302a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1303a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13048d573cc1SAndre Oppermann 					"Connection attempt to deprecated "
13058d573cc1SAndre Oppermann 					"IPv6 address rejected\n",
1306a160e630SAndre Oppermann 					s, __func__);
130733841545SHajimu UMEMOTO 				rstreason = BANDLIM_RST_OPENPORT;
130833841545SHajimu UMEMOTO 				goto dropwithreset;
130933841545SHajimu UMEMOTO 			}
131077d396fdSMaksim Yevmenkin 			if (ia6)
13118c0fec80SRobert Watson 				ifa_free(&ia6->ia_ifa);
131233841545SHajimu UMEMOTO 		}
1313b287c6c7SBjoern A. Zeeb #endif /* INET6 */
131465f28919SJesper Skriver 		/*
1315db33b3e6SAndre Oppermann 		 * Basic sanity checks on incoming SYN requests:
13168d573cc1SAndre Oppermann 		 *   Don't respond if the destination is a link layer
1317db33b3e6SAndre Oppermann 		 *	broadcast according to RFC1122 4.2.3.10, p. 104.
13188d573cc1SAndre Oppermann 		 *   If it is from this socket it must be forged.
13198d573cc1SAndre Oppermann 		 *   Don't respond if the source or destination is a
13208d573cc1SAndre Oppermann 		 *	global or subnet broad- or multicast address.
132193ec91baSCrist J. Clark 		 *   Note that it is quite possible to receive unicast
132293ec91baSCrist J. Clark 		 *	link-layer packets with a broadcast IP address. Use
132393ec91baSCrist J. Clark 		 *	in_broadcast() to find them.
1324be2ac88cSJonathan Lemon 		 */
13258d573cc1SAndre Oppermann 		if (m->m_flags & (M_BCAST|M_MCAST)) {
13268d573cc1SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
13278d573cc1SAndre Oppermann 			    log(LOG_DEBUG, "%s; %s: Listen socket: "
13288d573cc1SAndre Oppermann 				"Connection attempt from broad- or multicast "
1329773673c1SAndre Oppermann 				"link layer address ignored\n", s, __func__);
1330302ce8d6SAndre Oppermann 			goto dropunlock;
13318d573cc1SAndre Oppermann 		}
1332db33b3e6SAndre Oppermann #ifdef INET6
1333b287c6c7SBjoern A. Zeeb 		if (isipv6) {
1334db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1335a160e630SAndre Oppermann 			    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) {
1336a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1337a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13388d573cc1SAndre Oppermann 					"Connection attempt to/from self "
1339773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1340302ce8d6SAndre Oppermann 				goto dropunlock;
1341a160e630SAndre Oppermann 			}
1342be2ac88cSJonathan Lemon 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1343a160e630SAndre Oppermann 			    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
1344a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1345a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13468d573cc1SAndre Oppermann 					"Connection attempt from/to multicast "
1347773673c1SAndre Oppermann 					"address ignored\n", s, __func__);
1348302ce8d6SAndre Oppermann 				goto dropunlock;
1349a160e630SAndre Oppermann 			}
1350b287c6c7SBjoern A. Zeeb 		}
1351db33b3e6SAndre Oppermann #endif
1352b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
1353b287c6c7SBjoern A. Zeeb 		else
1354b287c6c7SBjoern A. Zeeb #endif
1355b287c6c7SBjoern A. Zeeb #ifdef INET
1356b287c6c7SBjoern A. Zeeb 		{
1357db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1358a160e630SAndre Oppermann 			    ip->ip_dst.s_addr == ip->ip_src.s_addr) {
1359a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1360a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13618d573cc1SAndre Oppermann 					"Connection attempt from/to self "
1362773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1363302ce8d6SAndre Oppermann 				goto dropunlock;
1364a160e630SAndre Oppermann 			}
1365be2ac88cSJonathan Lemon 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
1366be2ac88cSJonathan Lemon 			    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
13672ca2159fSCrist J. Clark 			    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
1368a160e630SAndre Oppermann 			    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
1369a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1370a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13718d573cc1SAndre Oppermann 					"Connection attempt from/to broad- "
1372773673c1SAndre Oppermann 					"or multicast address ignored\n",
1373a160e630SAndre Oppermann 					s, __func__);
1374302ce8d6SAndre Oppermann 				goto dropunlock;
1375c068736aSJeffrey Hsu 			}
1376a160e630SAndre Oppermann 		}
1377b287c6c7SBjoern A. Zeeb #endif
1378be2ac88cSJonathan Lemon 		/*
1379db33b3e6SAndre Oppermann 		 * SYN appears to be valid.  Create compressed TCP state
1380db33b3e6SAndre Oppermann 		 * for syncache.
1381be2ac88cSJonathan Lemon 		 */
13823c653157SHartmut Brandt #ifdef TCPDEBUG
13833c653157SHartmut Brandt 		if (so->so_options & SO_DEBUG)
13843c653157SHartmut Brandt 			tcp_trace(TA_INPUT, ostate, tp,
13853c653157SHartmut Brandt 			    (void *)tcp_saveipgen, &tcp_savetcp, 0);
13863c653157SHartmut Brandt #endif
13872b9c9984SGeorge V. Neville-Neil 		TCP_PROBE3(debug__input, tp, th, m);
1388f72167f4SAndre Oppermann 		tcp_dooptions(&to, optp, optlen, TO_SYN);
1389281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
1390281a0fd4SPatrick Kelsey 		if (syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL))
139109c305ebSPatrick Kelsey 			goto tfo_socket_result;
1392281a0fd4SPatrick Kelsey #else
139309fe6320SNavdeep Parhar 		syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL);
1394281a0fd4SPatrick Kelsey #endif
1395be2ac88cSJonathan Lemon 		/*
13964d6e7130SAndre Oppermann 		 * Entry added to syncache and mbuf consumed.
1397a7c7f2a7SJohn Baldwin 		 * Only the listen socket is unlocked by syncache_add().
1398be2ac88cSJonathan Lemon 		 */
1399ff9b006dSJulien Charbon 		if (ti_locked == TI_RLOCKED) {
1400ff9b006dSJulien Charbon 			INP_INFO_RUNLOCK(&V_tcbinfo);
1401a7c7f2a7SJohn Baldwin 			ti_locked = TI_UNLOCKED;
1402a7c7f2a7SJohn Baldwin 		}
1403603724d3SBjoern A. Zeeb 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
14048f5a8818SKevin Lo 		return (IPPROTO_DONE);
1405982c1675SAndre Oppermann 	} else if (tp->t_state == TCPS_LISTEN) {
1406982c1675SAndre Oppermann 		/*
1407982c1675SAndre Oppermann 		 * When a listen socket is torn down the SO_ACCEPTCONN
1408982c1675SAndre Oppermann 		 * flag is removed first while connections are drained
1409982c1675SAndre Oppermann 		 * from the accept queue in a unlock/lock cycle of the
1410982c1675SAndre Oppermann 		 * ACCEPT_LOCK, opening a race condition allowing a SYN
1411982c1675SAndre Oppermann 		 * attempt go through unhandled.
1412982c1675SAndre Oppermann 		 */
1413982c1675SAndre Oppermann 		goto dropunlock;
1414f76fcf6dSJeffrey Hsu 	}
1415fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1416fcf59617SAndrey V. Elsukov 	if (tp->t_flags & TF_SIGNATURE) {
1417fcf59617SAndrey V. Elsukov 		tcp_dooptions(&to, optp, optlen, thflags);
1418fcf59617SAndrey V. Elsukov 		if ((to.to_flags & TOF_SIGNATURE) == 0) {
1419fcf59617SAndrey V. Elsukov 			TCPSTAT_INC(tcps_sig_err_nosigopt);
14202903309aSAttilio Rao 			goto dropunlock;
14212903309aSAttilio Rao 		}
1422fcf59617SAndrey V. Elsukov 		if (!TCPMD5_ENABLED() ||
1423fcf59617SAndrey V. Elsukov 		    TCPMD5_INPUT(m, th, to.to_signature) != 0)
1424fcf59617SAndrey V. Elsukov 			goto dropunlock;
14252903309aSAttilio Rao 	}
14262903309aSAttilio Rao #endif
14272b9c9984SGeorge V. Neville-Neil 	TCP_PROBE5(receive, NULL, tp, m, tp, th);
142857f60867SMark Johnston 
1429302ce8d6SAndre Oppermann 	/*
14308d573cc1SAndre Oppermann 	 * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later
14318d573cc1SAndre Oppermann 	 * state.  tcp_do_segment() always consumes the mbuf chain, unlocks
14328d573cc1SAndre Oppermann 	 * the inpcb, and unlocks pcbinfo.
1433302ce8d6SAndre Oppermann 	 */
143455bceb1eSRandall Stewart 	tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos, ti_locked);
1435603724d3SBjoern A. Zeeb 	INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
14368f5a8818SKevin Lo 	return (IPPROTO_DONE);
1437be2ac88cSJonathan Lemon 
1438302ce8d6SAndre Oppermann dropwithreset:
14392b9c9984SGeorge V. Neville-Neil 	TCP_PROBE5(receive, NULL, tp, m, tp, th);
144057f60867SMark Johnston 
1441ff9b006dSJulien Charbon 	if (ti_locked == TI_RLOCKED) {
1442ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
1443252ca428SRobert Watson 		ti_locked = TI_UNLOCKED;
1444fa046d87SRobert Watson 	}
1445fa046d87SRobert Watson #ifdef INVARIANTS
1446fa046d87SRobert Watson 	else {
1447fa046d87SRobert Watson 		KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropwithreset "
1448fa046d87SRobert Watson 		    "ti_locked: %d", __func__, ti_locked));
1449fa046d87SRobert Watson 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1450fa046d87SRobert Watson 	}
1451fa046d87SRobert Watson #endif
1452014ea782SRobert Watson 
1453014ea782SRobert Watson 	if (inp != NULL) {
1454302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
1455014ea782SRobert Watson 		INP_WUNLOCK(inp);
1456dd8ac7f9SRobert Watson 	} else
1457014ea782SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
1458302ce8d6SAndre Oppermann 	m = NULL;	/* mbuf chain got consumed. */
1459014ea782SRobert Watson 	goto drop;
1460014ea782SRobert Watson 
1461302ce8d6SAndre Oppermann dropunlock:
146257f60867SMark Johnston 	if (m != NULL)
14632b9c9984SGeorge V. Neville-Neil 		TCP_PROBE5(receive, NULL, tp, m, tp, th);
146457f60867SMark Johnston 
1465ff9b006dSJulien Charbon 	if (ti_locked == TI_RLOCKED) {
1466ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
1467252ca428SRobert Watson 		ti_locked = TI_UNLOCKED;
1468fa046d87SRobert Watson 	}
1469fa046d87SRobert Watson #ifdef INVARIANTS
1470fa046d87SRobert Watson 	else {
1471fa046d87SRobert Watson 		KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropunlock "
1472fa046d87SRobert Watson 		    "ti_locked: %d", __func__, ti_locked));
1473fa046d87SRobert Watson 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1474fa046d87SRobert Watson 	}
1475fa046d87SRobert Watson #endif
1476252ca428SRobert Watson 
14779fa198beSAndre Oppermann 	if (inp != NULL)
14788501a69cSRobert Watson 		INP_WUNLOCK(inp);
1479014ea782SRobert Watson 
1480302ce8d6SAndre Oppermann drop:
1481603724d3SBjoern A. Zeeb 	INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1482a160e630SAndre Oppermann 	if (s != NULL)
1483a160e630SAndre Oppermann 		free(s, M_TCPLOG);
1484302ce8d6SAndre Oppermann 	if (m != NULL)
1485302ce8d6SAndre Oppermann 		m_freem(m);
14868f5a8818SKevin Lo 	return (IPPROTO_DONE);
1487302ce8d6SAndre Oppermann }
1488302ce8d6SAndre Oppermann 
1489e44c1887SSteven Hartland /*
1490e44c1887SSteven Hartland  * Automatic sizing of receive socket buffer.  Often the send
1491e44c1887SSteven Hartland  * buffer size is not optimally adjusted to the actual network
1492e44c1887SSteven Hartland  * conditions at hand (delay bandwidth product).  Setting the
1493e44c1887SSteven Hartland  * buffer size too small limits throughput on links with high
1494e44c1887SSteven Hartland  * bandwidth and high delay (eg. trans-continental/oceanic links).
1495e44c1887SSteven Hartland  *
1496e44c1887SSteven Hartland  * On the receive side the socket buffer memory is only rarely
1497e44c1887SSteven Hartland  * used to any significant extent.  This allows us to be much
1498e44c1887SSteven Hartland  * more aggressive in scaling the receive socket buffer.  For
1499e44c1887SSteven Hartland  * the case that the buffer space is actually used to a large
1500e44c1887SSteven Hartland  * extent and we run out of kernel memory we can simply drop
1501e44c1887SSteven Hartland  * the new segments; TCP on the sender will just retransmit it
1502e44c1887SSteven Hartland  * later.  Setting the buffer size too big may only consume too
1503e44c1887SSteven Hartland  * much kernel memory if the application doesn't read() from
1504e44c1887SSteven Hartland  * the socket or packet loss or reordering makes use of the
1505e44c1887SSteven Hartland  * reassembly queue.
1506e44c1887SSteven Hartland  *
1507e44c1887SSteven Hartland  * The criteria to step up the receive buffer one notch are:
1508e44c1887SSteven Hartland  *  1. Application has not set receive buffer size with
1509e44c1887SSteven Hartland  *     SO_RCVBUF. Setting SO_RCVBUF clears SB_AUTOSIZE.
1510e44c1887SSteven Hartland  *  2. the number of bytes received during the time it takes
1511e44c1887SSteven Hartland  *     one timestamp to be reflected back to us (the RTT);
1512e44c1887SSteven Hartland  *  3. received bytes per RTT is within seven eighth of the
1513e44c1887SSteven Hartland  *     current socket buffer size;
1514e44c1887SSteven Hartland  *  4. receive buffer size has not hit maximal automatic size;
1515e44c1887SSteven Hartland  *
1516e44c1887SSteven Hartland  * This algorithm does one step per RTT at most and only if
1517e44c1887SSteven Hartland  * we receive a bulk stream w/o packet losses or reorderings.
1518e44c1887SSteven Hartland  * Shrinking the buffer during idle times is not necessary as
1519e44c1887SSteven Hartland  * it doesn't consume any memory when idle.
1520e44c1887SSteven Hartland  *
1521e44c1887SSteven Hartland  * TODO: Only step up if the application is actually serving
1522e44c1887SSteven Hartland  * the buffer to better manage the socket buffer resources.
1523e44c1887SSteven Hartland  */
1524e44c1887SSteven Hartland int
1525e44c1887SSteven Hartland tcp_autorcvbuf(struct mbuf *m, struct tcphdr *th, struct socket *so,
1526e44c1887SSteven Hartland     struct tcpcb *tp, int tlen)
1527e44c1887SSteven Hartland {
1528e44c1887SSteven Hartland 	int newsize = 0;
1529e44c1887SSteven Hartland 
1530e44c1887SSteven Hartland 	if (V_tcp_do_autorcvbuf && (so->so_rcv.sb_flags & SB_AUTOSIZE) &&
1531e44c1887SSteven Hartland 	    tp->t_srtt != 0 && tp->rfbuf_ts != 0 &&
1532e44c1887SSteven Hartland 	    TCP_TS_TO_TICKS(tcp_ts_getticks() - tp->rfbuf_ts) >
1533e44c1887SSteven Hartland 	    (tp->t_srtt >> TCP_RTT_SHIFT)) {
1534e44c1887SSteven Hartland 		if (tp->rfbuf_cnt > (so->so_rcv.sb_hiwat / 8 * 7) &&
1535e44c1887SSteven Hartland 		    so->so_rcv.sb_hiwat < V_tcp_autorcvbuf_max) {
1536e44c1887SSteven Hartland 			newsize = min(so->so_rcv.sb_hiwat +
1537e44c1887SSteven Hartland 			    V_tcp_autorcvbuf_inc, V_tcp_autorcvbuf_max);
1538e44c1887SSteven Hartland 		}
1539e44c1887SSteven Hartland 		TCP_PROBE6(receive__autoresize, NULL, tp, m, tp, th, newsize);
1540e44c1887SSteven Hartland 
1541e44c1887SSteven Hartland 		/* Start over with next RTT. */
1542e44c1887SSteven Hartland 		tp->rfbuf_ts = 0;
1543e44c1887SSteven Hartland 		tp->rfbuf_cnt = 0;
1544e44c1887SSteven Hartland 	} else {
1545e44c1887SSteven Hartland 		tp->rfbuf_cnt += tlen;	/* add up */
1546e44c1887SSteven Hartland 	}
1547e44c1887SSteven Hartland 
1548e44c1887SSteven Hartland 	return (newsize);
1549e44c1887SSteven Hartland }
1550e44c1887SSteven Hartland 
155155bceb1eSRandall Stewart void
1552302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so,
1553252ca428SRobert Watson     struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos,
1554252ca428SRobert Watson     int ti_locked)
1555302ce8d6SAndre Oppermann {
1556021eaf79SHiren Panchasara 	int thflags, acked, ourfinisacked, needoutput = 0, sack_changed;
1557302ce8d6SAndre Oppermann 	int rstreason, todrop, win;
15583ac12506SJonathan T. Looney 	uint32_t tiwin;
15594b7b743cSLawrence Stewart 	uint16_t nsegs;
156007dacf03SAndre Oppermann 	char *s;
156107dacf03SAndre Oppermann 	struct in_conninfo *inc;
1562c11a15bfSGleb Smirnoff 	struct mbuf *mfree;
1563302ce8d6SAndre Oppermann 	struct tcpopt to;
156468bd7ed1SJonathan T. Looney #ifdef TCP_RFC7413
1565281a0fd4SPatrick Kelsey 	int tfo_syn;
156668bd7ed1SJonathan T. Looney #endif
1567302ce8d6SAndre Oppermann 
156814739780SMaxim Konovalov #ifdef TCPDEBUG
156914739780SMaxim Konovalov 	/*
157014739780SMaxim Konovalov 	 * The size of tcp_saveipgen must be the size of the max ip header,
157114739780SMaxim Konovalov 	 * now IPv6.
157214739780SMaxim Konovalov 	 */
157314739780SMaxim Konovalov 	u_char tcp_saveipgen[IP6_HDR_LEN];
157414739780SMaxim Konovalov 	struct tcphdr tcp_savetcp;
157514739780SMaxim Konovalov 	short ostate = 0;
157614739780SMaxim Konovalov #endif
1577302ce8d6SAndre Oppermann 	thflags = th->th_flags;
157807dacf03SAndre Oppermann 	inc = &tp->t_inpcb->inp_inc;
1579d64a46eaSLawrence Stewart 	tp->sackhint.last_sack_ack = 0;
1580021eaf79SHiren Panchasara 	sack_changed = 0;
15814b7b743cSLawrence Stewart 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
1582302ce8d6SAndre Oppermann 
1583252ca428SRobert Watson 	/*
1584252ca428SRobert Watson 	 * If this is either a state-changing packet or current state isn't
1585252ca428SRobert Watson 	 * established, we require a write lock on tcbinfo.  Otherwise, we
15860989f56cSRobert Watson 	 * allow the tcbinfo to be in either alocked or unlocked, as the
15870989f56cSRobert Watson 	 * caller may have unnecessarily acquired a write lock due to a race.
1588252ca428SRobert Watson 	 */
1589252ca428SRobert Watson 	if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 ||
1590252ca428SRobert Watson 	    tp->t_state != TCPS_ESTABLISHED) {
1591ff9b006dSJulien Charbon 		KASSERT(ti_locked == TI_RLOCKED, ("%s ti_locked %d for "
1592252ca428SRobert Watson 		    "SYN/FIN/RST/!EST", __func__, ti_locked));
1593ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1594252ca428SRobert Watson 	} else {
1595252ca428SRobert Watson #ifdef INVARIANTS
1596ff9b006dSJulien Charbon 		if (ti_locked == TI_RLOCKED)
1597ff9b006dSJulien Charbon 			INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1598fa046d87SRobert Watson 		else {
1599fa046d87SRobert Watson 			KASSERT(ti_locked == TI_UNLOCKED, ("%s: EST "
1600fa046d87SRobert Watson 			    "ti_locked: %d", __func__, ti_locked));
1601fa046d87SRobert Watson 			INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1602fa046d87SRobert Watson 		}
1603252ca428SRobert Watson #endif
1604252ca428SRobert Watson 	}
16058501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1606679d9708SAndre Oppermann 	KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
1607679d9708SAndre Oppermann 	    __func__));
1608679d9708SAndre Oppermann 	KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
1609679d9708SAndre Oppermann 	    __func__));
1610df8bae1dSRodney W. Grimes 
161186a996e6SHiren Panchasara #ifdef TCPPCAP
161286a996e6SHiren Panchasara 	/* Save segment, if requested. */
161386a996e6SHiren Panchasara 	tcp_pcap_add(th, m, &(tp->t_inpkts));
161486a996e6SHiren Panchasara #endif
161586a996e6SHiren Panchasara 
1616013f4df6SMichael Tuexen 	if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
1617013f4df6SMichael Tuexen 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
1618013f4df6SMichael Tuexen 			log(LOG_DEBUG, "%s; %s: "
1619013f4df6SMichael Tuexen 			    "SYN|FIN segment ignored (based on "
1620013f4df6SMichael Tuexen 			    "sysctl setting)\n", s, __func__);
1621013f4df6SMichael Tuexen 			free(s, M_TCPLOG);
1622013f4df6SMichael Tuexen 		}
1623013f4df6SMichael Tuexen 		goto drop;
1624013f4df6SMichael Tuexen 	}
1625013f4df6SMichael Tuexen 
1626df8bae1dSRodney W. Grimes 	/*
1627ebfd7534SMichael Tuexen 	 * If a segment with the ACK-bit set arrives in the SYN-SENT state
1628ebfd7534SMichael Tuexen 	 * check SEQ.ACK first.
1629ebfd7534SMichael Tuexen 	 */
1630ebfd7534SMichael Tuexen 	if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) &&
1631ebfd7534SMichael Tuexen 	    (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) {
1632ebfd7534SMichael Tuexen 		rstreason = BANDLIM_UNLIMITED;
1633ebfd7534SMichael Tuexen 		goto dropwithreset;
1634ebfd7534SMichael Tuexen 	}
1635ebfd7534SMichael Tuexen 
1636ebfd7534SMichael Tuexen 	/*
1637df8bae1dSRodney W. Grimes 	 * Segment received on connection.
1638df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
1639e8949f74SAndre Oppermann 	 * XXX: This should be done after segment
1640e8949f74SAndre Oppermann 	 * validation to ignore broken/spoofed segs.
1641df8bae1dSRodney W. Grimes 	 */
16429b8b58e0SJonathan Lemon 	tp->t_rcvtime = ticks;
1643df8bae1dSRodney W. Grimes 
1644df8bae1dSRodney W. Grimes 	/*
1645c52102ddSHiren Panchasara 	 * Scale up the window into a 32-bit value.
1646e8949f74SAndre Oppermann 	 * For the SYN_SENT state the scale is zero.
1647464fcfbcSAndre Oppermann 	 */
1648464fcfbcSAndre Oppermann 	tiwin = th->th_win << tp->snd_scale;
1649464fcfbcSAndre Oppermann 
1650464fcfbcSAndre Oppermann 	/*
1651f2512ba1SRui Paulo 	 * TCP ECN processing.
1652f2512ba1SRui Paulo 	 */
1653f2512ba1SRui Paulo 	if (tp->t_flags & TF_ECN_PERMIT) {
16549c251892SRui Paulo 		if (thflags & TH_CWR)
16559c251892SRui Paulo 			tp->t_flags &= ~TF_ECN_SND_ECE;
1656f2512ba1SRui Paulo 		switch (iptos & IPTOS_ECN_MASK) {
1657f2512ba1SRui Paulo 		case IPTOS_ECN_CE:
1658f2512ba1SRui Paulo 			tp->t_flags |= TF_ECN_SND_ECE;
165978b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ce);
1660f2512ba1SRui Paulo 			break;
1661f2512ba1SRui Paulo 		case IPTOS_ECN_ECT0:
166278b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ect0);
1663f2512ba1SRui Paulo 			break;
1664f2512ba1SRui Paulo 		case IPTOS_ECN_ECT1:
166578b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ect1);
1666f2512ba1SRui Paulo 			break;
1667f2512ba1SRui Paulo 		}
166864807b30SHiren Panchasara 
166964807b30SHiren Panchasara 		/* Process a packet differently from RFC3168. */
167064807b30SHiren Panchasara 		cc_ecnpkt_handler(tp, th, iptos);
167164807b30SHiren Panchasara 
1672dbc42409SLawrence Stewart 		/* Congestion experienced. */
1673dbc42409SLawrence Stewart 		if (thflags & TH_ECE) {
1674dbc42409SLawrence Stewart 			cc_cong_signal(tp, th, CC_ECN);
1675f2512ba1SRui Paulo 		}
1676f2512ba1SRui Paulo 	}
1677f2512ba1SRui Paulo 
1678f2512ba1SRui Paulo 	/*
1679f72167f4SAndre Oppermann 	 * Parse options on any incoming segment.
1680f72167f4SAndre Oppermann 	 */
1681302ce8d6SAndre Oppermann 	tcp_dooptions(&to, (u_char *)(th + 1),
1682302ce8d6SAndre Oppermann 	    (th->th_off << 2) - sizeof(struct tcphdr),
1683302ce8d6SAndre Oppermann 	    (thflags & TH_SYN) ? TO_SYN : 0);
1684f72167f4SAndre Oppermann 
1685fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1686fcf59617SAndrey V. Elsukov 	if ((tp->t_flags & TF_SIGNATURE) != 0 &&
1687fcf59617SAndrey V. Elsukov 	    (to.to_flags & TOF_SIGNATURE) == 0) {
1688fcf59617SAndrey V. Elsukov 		TCPSTAT_INC(tcps_sig_err_sigopt);
1689fcf59617SAndrey V. Elsukov 		/* XXX: should drop? */
1690fcf59617SAndrey V. Elsukov 	}
1691fcf59617SAndrey V. Elsukov #endif
1692f72167f4SAndre Oppermann 	/*
1693f72167f4SAndre Oppermann 	 * If echoed timestamp is later than the current time,
1694bf6d304aSAndre Oppermann 	 * fall back to non RFC1323 RTT calculation.  Normalize
1695bf6d304aSAndre Oppermann 	 * timestamp if syncookies were used when this connection
1696bf6d304aSAndre Oppermann 	 * was established.
1697f72167f4SAndre Oppermann 	 */
1698bf6d304aSAndre Oppermann 	if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
1699e16fa5caSJohn-Mark Gurney 		to.to_tsecr -= tp->ts_offset;
1700d8951c8aSBjoern A. Zeeb 		if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks()))
1701f72167f4SAndre Oppermann 			to.to_tsecr = 0;
1702bf6d304aSAndre Oppermann 	}
170307dacf03SAndre Oppermann 	/*
170407dacf03SAndre Oppermann 	 * If timestamps were negotiated during SYN/ACK they should
170507dacf03SAndre Oppermann 	 * appear on every segment during this session and vice versa.
170607dacf03SAndre Oppermann 	 */
170707dacf03SAndre Oppermann 	if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) {
170807dacf03SAndre Oppermann 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
170907dacf03SAndre Oppermann 			log(LOG_DEBUG, "%s; %s: Timestamp missing, "
171007dacf03SAndre Oppermann 			    "no action\n", s, __func__);
171107dacf03SAndre Oppermann 			free(s, M_TCPLOG);
171207dacf03SAndre Oppermann 		}
171307dacf03SAndre Oppermann 	}
171407dacf03SAndre Oppermann 	if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) {
171507dacf03SAndre Oppermann 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
171607dacf03SAndre Oppermann 			log(LOG_DEBUG, "%s; %s: Timestamp not expected, "
171707dacf03SAndre Oppermann 			    "no action\n", s, __func__);
171807dacf03SAndre Oppermann 			free(s, M_TCPLOG);
171907dacf03SAndre Oppermann 		}
172007dacf03SAndre Oppermann 	}
1721f72167f4SAndre Oppermann 
1722f72167f4SAndre Oppermann 	/*
172397d8d152SAndre Oppermann 	 * Process options only when we get SYN/ACK back. The SYN case
172497d8d152SAndre Oppermann 	 * for incoming connections is handled in tcp_syncache.
17255396d0f8SAndre Oppermann 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN>
17265396d0f8SAndre Oppermann 	 * or <SYN,ACK>) segment itself is never scaled.
172797d8d152SAndre Oppermann 	 * XXX this is traditional behavior, may need to be cleaned up.
1728df8bae1dSRodney W. Grimes 	 */
17290a389eabSSimon L. B. Nielsen 	if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
1730464fcfbcSAndre Oppermann 		if ((to.to_flags & TOF_SCALE) &&
1731464fcfbcSAndre Oppermann 		    (tp->t_flags & TF_REQ_SCALE)) {
1732be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_SCALE;
173302a1a643SAndre Oppermann 			tp->snd_scale = to.to_wscale;
1734be2ac88cSJonathan Lemon 		}
17355396d0f8SAndre Oppermann 		/*
17365396d0f8SAndre Oppermann 		 * Initial send window.  It will be updated with
17375396d0f8SAndre Oppermann 		 * the next incoming segment to the scaled value.
17385396d0f8SAndre Oppermann 		 */
17395396d0f8SAndre Oppermann 		tp->snd_wnd = th->th_win;
1740be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_TS) {
1741be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_TSTMP;
1742be2ac88cSJonathan Lemon 			tp->ts_recent = to.to_tsval;
1743d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
1744be2ac88cSJonathan Lemon 		}
1745be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_MSS)
1746be2ac88cSJonathan Lemon 			tcp_mss(tp, to.to_mss);
17473529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
17483529149eSAndre Oppermann 		    (to.to_flags & TOF_SACKPERM) == 0)
17493529149eSAndre Oppermann 			tp->t_flags &= ~TF_SACK_PERMIT;
17506d90faf3SPaul Saab 	}
17516d90faf3SPaul Saab 
1752df8bae1dSRodney W. Grimes 	/*
1753df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
1754df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
1755df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
1756df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
1757df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
1758df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
1759df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
1760df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
1761df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
1762df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
1763df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
1764df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
1765a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
1766c5ad39b9SAndre Oppermann 	 * Since we check for TCPS_ESTABLISHED first, it can only
1767a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
1768df8bae1dSRodney W. Grimes 	 */
1769df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
1770c5ad39b9SAndre Oppermann 	    th->th_seq == tp->rcv_nxt &&
1771fb59c426SYoshinobu Inoue 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1772c5ad39b9SAndre Oppermann 	    tp->snd_nxt == tp->snd_max &&
1773c5ad39b9SAndre Oppermann 	    tiwin && tiwin == tp->snd_wnd &&
1774a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
17754741bfcbSPatrick Kelsey 	    LIST_EMPTY(&tp->t_segq) &&
17764741bfcbSPatrick Kelsey 	    ((to.to_flags & TOF_TS) == 0 ||
1777c5ad39b9SAndre Oppermann 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) {
1778df8bae1dSRodney W. Grimes 
1779df8bae1dSRodney W. Grimes 		/*
1780df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
1781df8bae1dSRodney W. Grimes 		 * record the timestamp.
1782a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
1783a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1784df8bae1dSRodney W. Grimes 		 */
1785be2ac88cSJonathan Lemon 		if ((to.to_flags & TOF_TS) != 0 &&
1786fb59c426SYoshinobu Inoue 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1787d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
1788a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
1789df8bae1dSRodney W. Grimes 		}
1790df8bae1dSRodney W. Grimes 
1791fb59c426SYoshinobu Inoue 		if (tlen == 0) {
1792fb59c426SYoshinobu Inoue 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
1793fb59c426SYoshinobu Inoue 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
1794dbc42409SLawrence Stewart 			    !IN_RECOVERY(tp->t_flags) &&
1795fc30a251SAndre Oppermann 			    (to.to_flags & TOF_SACK) == 0 &&
1796dbc42409SLawrence Stewart 			    TAILQ_EMPTY(&tp->snd_holes)) {
1797df8bae1dSRodney W. Grimes 				/*
1798e8949f74SAndre Oppermann 				 * This is a pure ack for outstanding data.
1799df8bae1dSRodney W. Grimes 				 */
1800ff9b006dSJulien Charbon 				if (ti_locked == TI_RLOCKED)
1801ff9b006dSJulien Charbon 					INP_INFO_RUNLOCK(&V_tcbinfo);
1802252ca428SRobert Watson 				ti_locked = TI_UNLOCKED;
1803252ca428SRobert Watson 
180478b50714SRobert Watson 				TCPSTAT_INC(tcps_predack);
1805252ca428SRobert Watson 
18069b8b58e0SJonathan Lemon 				/*
1807e8949f74SAndre Oppermann 				 * "bad retransmit" recovery.
18089b8b58e0SJonathan Lemon 				 */
18099b8b58e0SJonathan Lemon 				if (tp->t_rxtshift == 1 &&
1810672dc4aeSJohn Baldwin 				    tp->t_flags & TF_PREVVALID &&
18116dfb8b31SJohn Baldwin 				    (int)(ticks - tp->t_badrxtwin) < 0) {
1812dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_RTO_ERR);
18139b8b58e0SJonathan Lemon 				}
1814fa55172bSMatthew Dillon 
1815fa55172bSMatthew Dillon 				/*
1816fa55172bSMatthew Dillon 				 * Recalculate the transmit timer / rtt.
1817fa55172bSMatthew Dillon 				 *
1818fa55172bSMatthew Dillon 				 * Some boxes send broken timestamp replies
1819fa55172bSMatthew Dillon 				 * during the SYN+ACK phase, ignore
1820fa55172bSMatthew Dillon 				 * timestamps of 0 or we could calculate a
1821fa55172bSMatthew Dillon 				 * huge RTT and blow up the retransmit timer.
1822fa55172bSMatthew Dillon 				 */
1823fa55172bSMatthew Dillon 				if ((to.to_flags & TOF_TS) != 0 &&
1824fa55172bSMatthew Dillon 				    to.to_tsecr) {
18253ac12506SJonathan T. Looney 					uint32_t t;
1826d8951c8aSBjoern A. Zeeb 
1827d8951c8aSBjoern A. Zeeb 					t = tcp_ts_getticks() - to.to_tsecr;
1828d8951c8aSBjoern A. Zeeb 					if (!tp->t_rttlow || tp->t_rttlow > t)
1829d8951c8aSBjoern A. Zeeb 						tp->t_rttlow = t;
1830a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
1831d8951c8aSBjoern A. Zeeb 					    TCP_TS_TO_TICKS(t) + 1);
1832fa55172bSMatthew Dillon 				} else if (tp->t_rtttime &&
1833fa55172bSMatthew Dillon 				    SEQ_GT(th->th_ack, tp->t_rtseq)) {
1834eaf80179SAndre Oppermann 					if (!tp->t_rttlow ||
1835eaf80179SAndre Oppermann 					    tp->t_rttlow > ticks - tp->t_rtttime)
1836eaf80179SAndre Oppermann 						tp->t_rttlow = ticks - tp->t_rtttime;
1837c068736aSJeffrey Hsu 					tcp_xmit_timer(tp,
1838c068736aSJeffrey Hsu 							ticks - tp->t_rtttime);
1839fa55172bSMatthew Dillon 				}
1840dbc42409SLawrence Stewart 				acked = BYTES_THIS_ACK(tp, th);
184139bc9de5SLawrence Stewart 
1842bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
184339bc9de5SLawrence Stewart 				/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
184439bc9de5SLawrence Stewart 				hhook_run_tcp_est_in(tp, th, &to);
1845bd79708dSJonathan T. Looney #endif
184639bc9de5SLawrence Stewart 
18474b7b743cSLawrence Stewart 				TCPSTAT_ADD(tcps_rcvackpack, nsegs);
184878b50714SRobert Watson 				TCPSTAT_ADD(tcps_rcvackbyte, acked);
1849df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
18509d11646dSJeffrey Hsu 				if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
18519d11646dSJeffrey Hsu 				    SEQ_LEQ(th->th_ack, tp->snd_recover))
18529d11646dSJeffrey Hsu 					tp->snd_recover = th->th_ack - 1;
1853dbc42409SLawrence Stewart 
1854dbc42409SLawrence Stewart 				/*
1855dbc42409SLawrence Stewart 				 * Let the congestion control algorithm update
1856dbc42409SLawrence Stewart 				 * congestion control related information. This
1857dbc42409SLawrence Stewart 				 * typically means increasing the congestion
1858dbc42409SLawrence Stewart 				 * window.
1859dbc42409SLawrence Stewart 				 */
18604b7b743cSLawrence Stewart 				cc_ack_received(tp, th, nsegs, CC_ACK);
1861dbc42409SLawrence Stewart 
18629d11646dSJeffrey Hsu 				tp->snd_una = th->th_ack;
18631645d090SJeff Roberson 				/*
1864e8949f74SAndre Oppermann 				 * Pull snd_wl2 up to prevent seq wrap relative
18651645d090SJeff Roberson 				 * to th_ack.
18661645d090SJeff Roberson 				 */
1867cb942153SJeffrey Hsu 				tp->snd_wl2 = th->th_ack;
1868b5addd85SJeffrey Hsu 				tp->t_dupacks = 0;
1869df8bae1dSRodney W. Grimes 				m_freem(m);
1870df8bae1dSRodney W. Grimes 
1871df8bae1dSRodney W. Grimes 				/*
1872df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
1873df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
1874df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
1875df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
1876df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
1877df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
1878df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
1879e8949f74SAndre Oppermann 				 */
18803c653157SHartmut Brandt #ifdef TCPDEBUG
18813c653157SHartmut Brandt 				if (so->so_options & SO_DEBUG)
18823c653157SHartmut Brandt 					tcp_trace(TA_INPUT, ostate, tp,
18833c653157SHartmut Brandt 					    (void *)tcp_saveipgen,
18843c653157SHartmut Brandt 					    &tcp_savetcp, 0);
18853c653157SHartmut Brandt #endif
18862b9c9984SGeorge V. Neville-Neil 				TCP_PROBE3(debug__input, tp, th, m);
1887df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
1888b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
1889b8152ba7SAndre Oppermann 				else if (!tcp_timer_active(tp, TT_PERSIST))
1890b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT,
1891b8152ba7SAndre Oppermann 						      tp->t_rxtcur);
1892df8bae1dSRodney W. Grimes 				sowwakeup(so);
1893cfa6009eSGleb Smirnoff 				if (sbavail(&so->so_snd))
189455bceb1eSRandall Stewart 					(void) tp->t_fb->tfb_tcp_output(tp);
1895a14c749fSJonathan Lemon 				goto check_delack;
1896df8bae1dSRodney W. Grimes 			}
1897fb59c426SYoshinobu Inoue 		} else if (th->th_ack == tp->snd_una &&
1898fb59c426SYoshinobu Inoue 		    tlen <= sbspace(&so->so_rcv)) {
18996741ecf5SAndre Oppermann 			int newsize = 0;	/* automatic sockbuf scaling */
19006741ecf5SAndre Oppermann 
1901df8bae1dSRodney W. Grimes 			/*
1902252ca428SRobert Watson 			 * This is a pure, in-sequence data packet with
1903252ca428SRobert Watson 			 * nothing on the reassembly queue and we have enough
1904252ca428SRobert Watson 			 * buffer space to take it.
1905df8bae1dSRodney W. Grimes 			 */
1906ff9b006dSJulien Charbon 			if (ti_locked == TI_RLOCKED)
1907ff9b006dSJulien Charbon 				INP_INFO_RUNLOCK(&V_tcbinfo);
1908252ca428SRobert Watson 			ti_locked = TI_UNLOCKED;
1909252ca428SRobert Watson 
19106d90faf3SPaul Saab 			/* Clean receiver SACK report if present */
19113529149eSAndre Oppermann 			if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks)
19126d90faf3SPaul Saab 				tcp_clean_sackreport(tp);
191378b50714SRobert Watson 			TCPSTAT_INC(tcps_preddat);
1914fb59c426SYoshinobu Inoue 			tp->rcv_nxt += tlen;
19151645d090SJeff Roberson 			/*
19161645d090SJeff Roberson 			 * Pull snd_wl1 up to prevent seq wrap relative to
19171645d090SJeff Roberson 			 * th_seq.
19181645d090SJeff Roberson 			 */
191960ee3bb2SAndre Oppermann 			tp->snd_wl1 = th->th_seq;
19201645d090SJeff Roberson 			/*
19211645d090SJeff Roberson 			 * Pull rcv_up up to prevent seq wrap relative to
19221645d090SJeff Roberson 			 * rcv_nxt.
19231645d090SJeff Roberson 			 */
19241645d090SJeff Roberson 			tp->rcv_up = tp->rcv_nxt;
19254b7b743cSLawrence Stewart 			TCPSTAT_ADD(tcps_rcvpack, nsegs);
192678b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
19273c653157SHartmut Brandt #ifdef TCPDEBUG
19283c653157SHartmut Brandt 			if (so->so_options & SO_DEBUG)
19293c653157SHartmut Brandt 				tcp_trace(TA_INPUT, ostate, tp,
19303c653157SHartmut Brandt 				    (void *)tcp_saveipgen, &tcp_savetcp, 0);
19313c653157SHartmut Brandt #endif
19322b9c9984SGeorge V. Neville-Neil 			TCP_PROBE3(debug__input, tp, th, m);
19335d06879aSGeorge V. Neville-Neil 
1934e44c1887SSteven Hartland 			newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
19356741ecf5SAndre Oppermann 
19366741ecf5SAndre Oppermann 			/* Add data to socket buffer. */
19371e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
1938c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1939c1c36a2cSMike Silbersack 				m_freem(m);
1940c1c36a2cSMike Silbersack 			} else {
19416741ecf5SAndre Oppermann 				/*
19426741ecf5SAndre Oppermann 				 * Set new socket buffer size.
19436741ecf5SAndre Oppermann 				 * Give up when limit is reached.
19446741ecf5SAndre Oppermann 				 */
19456741ecf5SAndre Oppermann 				if (newsize)
19466741ecf5SAndre Oppermann 					if (!sbreserve_locked(&so->so_rcv,
19476c8286e4SRobert Watson 					    newsize, so, NULL))
19486741ecf5SAndre Oppermann 						so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
1949fb59c426SYoshinobu Inoue 				m_adj(m, drop_hdrlen);	/* delayed header drop */
1950651e4e6aSGleb Smirnoff 				sbappendstream_locked(&so->so_rcv, m, 0);
1951c1c36a2cSMike Silbersack 			}
1952e8949f74SAndre Oppermann 			/* NB: sorwakeup_locked() does an implicit unlock. */
19531e4d7da7SRobert Watson 			sorwakeup_locked(so);
1954c1e5a6e5SAndre Oppermann 			if (DELAY_ACK(tp, tlen)) {
19553bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
1956f498eeeeSDavid Greenman 			} else {
1957e612a582SDavid Greenman 				tp->t_flags |= TF_ACKNOW;
195855bceb1eSRandall Stewart 				tp->t_fb->tfb_tcp_output(tp);
1959e612a582SDavid Greenman 			}
1960a14c749fSJonathan Lemon 			goto check_delack;
1961df8bae1dSRodney W. Grimes 		}
1962df8bae1dSRodney W. Grimes 	}
1963df8bae1dSRodney W. Grimes 
1964df8bae1dSRodney W. Grimes 	/*
1965df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
1966df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
1967df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
1968df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
1969df8bae1dSRodney W. Grimes 	 */
1970df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
1971df8bae1dSRodney W. Grimes 	if (win < 0)
1972df8bae1dSRodney W. Grimes 		win = 0;
197366e39adcSJohn Polstra 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1974df8bae1dSRodney W. Grimes 
1975df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1976df8bae1dSRodney W. Grimes 
1977df8bae1dSRodney W. Grimes 	/*
1978764d8cefSBill Fenner 	 * If the state is SYN_RECEIVED:
1979764d8cefSBill Fenner 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
1980764d8cefSBill Fenner 	 */
1981764d8cefSBill Fenner 	case TCPS_SYN_RECEIVED:
1982fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1983fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1984a57815efSBosko Milekic 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1985a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
1986a57815efSBosko Milekic 				goto dropwithreset;
1987a57815efSBosko Milekic 		}
1988281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
198968bd7ed1SJonathan T. Looney 		if (IS_FASTOPEN(tp->t_flags)) {
1990281a0fd4SPatrick Kelsey 			/*
1991281a0fd4SPatrick Kelsey 			 * When a TFO connection is in SYN_RECEIVED, the
1992281a0fd4SPatrick Kelsey 			 * only valid packets are the initial SYN, a
1993281a0fd4SPatrick Kelsey 			 * retransmit/copy of the initial SYN (possibly with
1994281a0fd4SPatrick Kelsey 			 * a subset of the original data), a valid ACK, a
1995281a0fd4SPatrick Kelsey 			 * FIN, or a RST.
1996281a0fd4SPatrick Kelsey 			 */
1997281a0fd4SPatrick Kelsey 			if ((thflags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) {
1998281a0fd4SPatrick Kelsey 				rstreason = BANDLIM_RST_OPENPORT;
1999281a0fd4SPatrick Kelsey 				goto dropwithreset;
2000281a0fd4SPatrick Kelsey 			} else if (thflags & TH_SYN) {
2001281a0fd4SPatrick Kelsey 				/* non-initial SYN is ignored */
2002281a0fd4SPatrick Kelsey 				if ((tcp_timer_active(tp, TT_DELACK) ||
2003281a0fd4SPatrick Kelsey 				     tcp_timer_active(tp, TT_REXMT)))
2004281a0fd4SPatrick Kelsey 					goto drop;
2005281a0fd4SPatrick Kelsey 			} else if (!(thflags & (TH_ACK|TH_FIN|TH_RST))) {
2006281a0fd4SPatrick Kelsey 				goto drop;
2007281a0fd4SPatrick Kelsey 			}
2008281a0fd4SPatrick Kelsey 		}
2009281a0fd4SPatrick Kelsey #endif
2010764d8cefSBill Fenner 		break;
2011764d8cefSBill Fenner 
2012764d8cefSBill Fenner 	/*
2013df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
2014*98732609SMichael Tuexen 	 *	if seg contains a RST with valid ACK (SEQ.ACK has already
2015*98732609SMichael Tuexen 	 *	    been verified), then drop the connection.
2016*98732609SMichael Tuexen 	 *	if seg contains a RST without an ACK, drop the seg.
2017*98732609SMichael Tuexen 	 *	if seg does not contain SYN, then drop the seg.
2018df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
2019df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
2020df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
2021f2512ba1SRui Paulo 	 *	if seg contains an ECE and ECN support is enabled, the stream
2022f2512ba1SRui Paulo 	 *	    is ECN capable.
2023df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
2024df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
2025df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
2026df8bae1dSRodney W. Grimes 	 */
2027df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
202857f60867SMark Johnston 		if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) {
2029d9fae5abSAndriy Gapon 			TCP_PROBE5(connect__refused, NULL, tp,
20302b9c9984SGeorge V. Neville-Neil 			    m, tp, th);
2031df8bae1dSRodney W. Grimes 			tp = tcp_drop(tp, ECONNREFUSED);
203257f60867SMark Johnston 		}
20330ca3f933SAndre Oppermann 		if (thflags & TH_RST)
2034df8bae1dSRodney W. Grimes 			goto drop;
20350ca3f933SAndre Oppermann 		if (!(thflags & TH_SYN))
2036df8bae1dSRodney W. Grimes 			goto drop;
2037464fcfbcSAndre Oppermann 
2038fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
2039df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
2040fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
204178b50714SRobert Watson 			TCPSTAT_INC(tcps_connects);
2042845799c1SAndras Olah 			soisconnected(so);
2043c488362eSRobert Watson #ifdef MAC
204430d239bcSRobert Watson 			mac_socketpeer_set_from_mbuf(m, so);
2045c488362eSRobert Watson #endif
2046845799c1SAndras Olah 			/* Do window scaling on this connection? */
2047845799c1SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2048845799c1SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
2049845799c1SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2050845799c1SAndras Olah 			}
20513ac12506SJonathan T. Looney 			tp->rcv_adv += min(tp->rcv_wnd,
2052766282cbSJohn Baldwin 			    TCP_MAXWIN << tp->rcv_scale);
2053a0292f23SGarrett Wollman 			tp->snd_una++;		/* SYN is acked */
2054a0292f23SGarrett Wollman 			/*
2055a0292f23SGarrett Wollman 			 * If there's data, delay ACK; if there's also a FIN
2056a0292f23SGarrett Wollman 			 * ACKNOW will be turned on later.
2057a0292f23SGarrett Wollman 			 */
2058c1e5a6e5SAndre Oppermann 			if (DELAY_ACK(tp, tlen) && tlen != 0)
2059b8152ba7SAndre Oppermann 				tcp_timer_activate(tp, TT_DELACK,
2060b8152ba7SAndre Oppermann 				    tcp_delacktime);
2061a0292f23SGarrett Wollman 			else
2062a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
2063f2512ba1SRui Paulo 
2064603724d3SBjoern A. Zeeb 			if ((thflags & TH_ECE) && V_tcp_do_ecn) {
2065f2512ba1SRui Paulo 				tp->t_flags |= TF_ECN_PERMIT;
206678b50714SRobert Watson 				TCPSTAT_INC(tcps_ecn_shs);
2067f2512ba1SRui Paulo 			}
2068f2512ba1SRui Paulo 
2069a0292f23SGarrett Wollman 			/*
2070a0292f23SGarrett Wollman 			 * Received <SYN,ACK> in SYN_SENT[*] state.
2071a0292f23SGarrett Wollman 			 * Transitions:
2072a0292f23SGarrett Wollman 			 *	SYN_SENT  --> ESTABLISHED
2073a0292f23SGarrett Wollman 			 *	SYN_SENT* --> FIN_WAIT_1
2074a0292f23SGarrett Wollman 			 */
20759b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
2076a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDFIN) {
207757f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_1);
2078a0292f23SGarrett Wollman 				tp->t_flags &= ~TF_NEEDFIN;
2079fb59c426SYoshinobu Inoue 				thflags &= ~TH_SYN;
20807ff19458SPaul Traina 			} else {
208157f60867SMark Johnston 				tcp_state_change(tp, TCPS_ESTABLISHED);
2082d9fae5abSAndriy Gapon 				TCP_PROBE5(connect__established, NULL, tp,
20832b9c9984SGeorge V. Neville-Neil 				    m, tp, th);
2084dbc42409SLawrence Stewart 				cc_conn_init(tp);
20859077f387SGleb Smirnoff 				tcp_timer_activate(tp, TT_KEEP,
20869077f387SGleb Smirnoff 				    TP_KEEPIDLE(tp));
20877ff19458SPaul Traina 			}
2088a0292f23SGarrett Wollman 		} else {
2089a0292f23SGarrett Wollman 			/*
2090c068736aSJeffrey Hsu 			 * Received initial SYN in SYN-SENT[*] state =>
2091153edc50SHiren Panchasara 			 * simultaneous open.
2092c068736aSJeffrey Hsu 			 * If it succeeds, connection is * half-synchronized.
2093c068736aSJeffrey Hsu 			 * Otherwise, do 3-way handshake:
2094a0292f23SGarrett Wollman 			 *        SYN-SENT -> SYN-RECEIVED
2095a0292f23SGarrett Wollman 			 *        SYN-SENT* -> SYN-RECEIVED*
2096a0292f23SGarrett Wollman 			 */
20974b8e98d6SQing Li 			tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
2098b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
209957f60867SMark Johnston 			tcp_state_change(tp, TCPS_SYN_RECEIVED);
2100a0292f23SGarrett Wollman 		}
2101df8bae1dSRodney W. Grimes 
2102ff9b006dSJulien Charbon 		KASSERT(ti_locked == TI_RLOCKED, ("%s: trimthenstep6: "
2103252ca428SRobert Watson 		    "ti_locked %d", __func__, ti_locked));
2104ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
21058501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
21067cfc6904SRobert Watson 
2107df8bae1dSRodney W. Grimes 		/*
2108fb59c426SYoshinobu Inoue 		 * Advance th->th_seq to correspond to first data byte.
2109df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
2110df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
2111df8bae1dSRodney W. Grimes 		 */
2112fb59c426SYoshinobu Inoue 		th->th_seq++;
2113fb59c426SYoshinobu Inoue 		if (tlen > tp->rcv_wnd) {
2114fb59c426SYoshinobu Inoue 			todrop = tlen - tp->rcv_wnd;
2115df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
2116fb59c426SYoshinobu Inoue 			tlen = tp->rcv_wnd;
2117fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
211878b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpackafterwin);
211978b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2120df8bae1dSRodney W. Grimes 		}
2121fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
2122fb59c426SYoshinobu Inoue 		tp->rcv_up = th->th_seq;
2123a0292f23SGarrett Wollman 		/*
2124a0292f23SGarrett Wollman 		 * Client side of transaction: already sent SYN and data.
2125a0292f23SGarrett Wollman 		 * If the remote host used T/TCP to validate the SYN,
2126a0292f23SGarrett Wollman 		 * our data will be ACK'd; if so, enter normal data segment
2127a0292f23SGarrett Wollman 		 * processing in the middle of step 5, ack processing.
2128a0292f23SGarrett Wollman 		 * Otherwise, goto step 6.
2129a0292f23SGarrett Wollman 		 */
2130fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK)
2131a0292f23SGarrett Wollman 			goto process_ACK;
2132c068736aSJeffrey Hsu 
2133df8bae1dSRodney W. Grimes 		goto step6;
2134c068736aSJeffrey Hsu 
2135a0292f23SGarrett Wollman 	/*
2136a0292f23SGarrett Wollman 	 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
2137c94c54e4SAndre Oppermann 	 *      do normal processing.
2138a0292f23SGarrett Wollman 	 *
2139c94c54e4SAndre Oppermann 	 * NB: Leftover from RFC1644 T/TCP.  Cases to be reused later.
2140a0292f23SGarrett Wollman 	 */
2141a0292f23SGarrett Wollman 	case TCPS_LAST_ACK:
2142a0292f23SGarrett Wollman 	case TCPS_CLOSING:
2143a0292f23SGarrett Wollman 		break;  /* continue normal processing */
2144df8bae1dSRodney W. Grimes 	}
2145df8bae1dSRodney W. Grimes 
2146df8bae1dSRodney W. Grimes 	/*
2147df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
214880ab7c0eSGarrett Wollman 	 * First check the RST flag and sequence number since reset segments
214980ab7c0eSGarrett Wollman 	 * are exempt from the timestamp and connection count tests.  This
215080ab7c0eSGarrett Wollman 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
215180ab7c0eSGarrett Wollman 	 * below which allowed reset segments in half the sequence space
215280ab7c0eSGarrett Wollman 	 * to fall though and be processed (which gives forged reset
215380ab7c0eSGarrett Wollman 	 * segments with a random sequence number a 50 percent chance of
215480ab7c0eSGarrett Wollman 	 * killing a connection).
215580ab7c0eSGarrett Wollman 	 * Then check timestamp, if present.
2156a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
2157df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
2158df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
2159df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
216080ab7c0eSGarrett Wollman 	 */
2161fb59c426SYoshinobu Inoue 	if (thflags & TH_RST) {
21623220a212SGleb Smirnoff 		/*
21633220a212SGleb Smirnoff 		 * RFC5961 Section 3.2
21643220a212SGleb Smirnoff 		 *
21653220a212SGleb Smirnoff 		 * - RST drops connection only if SEG.SEQ == RCV.NXT.
21663220a212SGleb Smirnoff 		 * - If RST is in window, we send challenge ACK.
21673220a212SGleb Smirnoff 		 *
21683220a212SGleb Smirnoff 		 * Note: to take into account delayed ACKs, we should
21693220a212SGleb Smirnoff 		 *   test against last_ack_sent instead of rcv_nxt.
21703220a212SGleb Smirnoff 		 * Note 2: we handle special case of closed window, not
21713220a212SGleb Smirnoff 		 *   covered by the RFC.
21723220a212SGleb Smirnoff 		 */
21733220a212SGleb Smirnoff 		if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
21743220a212SGleb Smirnoff 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
21753220a212SGleb Smirnoff 		    (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) {
217680ab7c0eSGarrett Wollman 
2177ff9b006dSJulien Charbon 			INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2178ff9b006dSJulien Charbon 			KASSERT(ti_locked == TI_RLOCKED,
21793220a212SGleb Smirnoff 			    ("%s: TH_RST ti_locked %d, th %p tp %p",
21803220a212SGleb Smirnoff 			    __func__, ti_locked, th, tp));
21813220a212SGleb Smirnoff 			KASSERT(tp->t_state != TCPS_SYN_SENT,
21823220a212SGleb Smirnoff 			    ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p",
21833220a212SGleb Smirnoff 			    __func__, th, tp));
21843220a212SGleb Smirnoff 
21853220a212SGleb Smirnoff 			if (V_tcp_insecure_rst ||
21863220a212SGleb Smirnoff 			    tp->last_ack_sent == th->th_seq) {
21873220a212SGleb Smirnoff 				TCPSTAT_INC(tcps_drops);
21883220a212SGleb Smirnoff 				/* Drop the connection. */
21893220a212SGleb Smirnoff 				switch (tp->t_state) {
219080ab7c0eSGarrett Wollman 				case TCPS_SYN_RECEIVED:
219180ab7c0eSGarrett Wollman 					so->so_error = ECONNREFUSED;
219280ab7c0eSGarrett Wollman 					goto close;
219380ab7c0eSGarrett Wollman 				case TCPS_ESTABLISHED:
219480ab7c0eSGarrett Wollman 				case TCPS_FIN_WAIT_1:
219580ab7c0eSGarrett Wollman 				case TCPS_FIN_WAIT_2:
219680ab7c0eSGarrett Wollman 				case TCPS_CLOSE_WAIT:
21976779a1a1SMichael Tuexen 				case TCPS_CLOSING:
21986779a1a1SMichael Tuexen 				case TCPS_LAST_ACK:
219980ab7c0eSGarrett Wollman 					so->so_error = ECONNRESET;
220080ab7c0eSGarrett Wollman 				close:
22013220a212SGleb Smirnoff 					/* FALLTHROUGH */
22023220a212SGleb Smirnoff 				default:
220380ab7c0eSGarrett Wollman 					tp = tcp_close(tp);
22043220a212SGleb Smirnoff 				}
22053220a212SGleb Smirnoff 			} else {
22063220a212SGleb Smirnoff 				TCPSTAT_INC(tcps_badrst);
22073220a212SGleb Smirnoff 				/* Send challenge ACK. */
22083220a212SGleb Smirnoff 				tcp_respond(tp, mtod(m, void *), th, m,
22093220a212SGleb Smirnoff 				    tp->rcv_nxt, tp->snd_nxt, TH_ACK);
22103220a212SGleb Smirnoff 				tp->last_ack_sent = tp->rcv_nxt;
22113220a212SGleb Smirnoff 				m = NULL;
22123220a212SGleb Smirnoff 			}
22133220a212SGleb Smirnoff 		}
22143220a212SGleb Smirnoff 		goto drop;
22153220a212SGleb Smirnoff 	}
221680ab7c0eSGarrett Wollman 
22173220a212SGleb Smirnoff 	/*
22183220a212SGleb Smirnoff 	 * RFC5961 Section 4.2
22193220a212SGleb Smirnoff 	 * Send challenge ACK for any SYN in synchronized state.
22203220a212SGleb Smirnoff 	 */
2221281a0fd4SPatrick Kelsey 	if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT &&
2222281a0fd4SPatrick Kelsey 	    tp->t_state != TCPS_SYN_RECEIVED) {
2223ff9b006dSJulien Charbon 		KASSERT(ti_locked == TI_RLOCKED,
22243220a212SGleb Smirnoff 		    ("tcp_do_segment: TH_SYN ti_locked %d", ti_locked));
2225ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2226252ca428SRobert Watson 
22273220a212SGleb Smirnoff 		TCPSTAT_INC(tcps_badsyn);
22283220a212SGleb Smirnoff 		if (V_tcp_insecure_syn &&
22293220a212SGleb Smirnoff 		    SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
22303220a212SGleb Smirnoff 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
22313220a212SGleb Smirnoff 			tp = tcp_drop(tp, ECONNRESET);
22323220a212SGleb Smirnoff 			rstreason = BANDLIM_UNLIMITED;
22333220a212SGleb Smirnoff 		} else {
22343220a212SGleb Smirnoff 			/* Send challenge ACK. */
22353220a212SGleb Smirnoff 			tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt,
22363220a212SGleb Smirnoff 			    tp->snd_nxt, TH_ACK);
22373220a212SGleb Smirnoff 			tp->last_ack_sent = tp->rcv_nxt;
22383220a212SGleb Smirnoff 			m = NULL;
223980ab7c0eSGarrett Wollman 		}
224080ab7c0eSGarrett Wollman 		goto drop;
224180ab7c0eSGarrett Wollman 	}
224280ab7c0eSGarrett Wollman 
224380ab7c0eSGarrett Wollman 	/*
2244df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
2245df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
2246df8bae1dSRodney W. Grimes 	 */
2247be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
224880ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
2249df8bae1dSRodney W. Grimes 
2250df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
2251d8951c8aSBjoern A. Zeeb 		if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) {
2252df8bae1dSRodney W. Grimes 			/*
2253df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
2254df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
2255df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
2256df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
2257df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
2258df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
2259df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
2260df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
2261df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
2262df8bae1dSRodney W. Grimes 			 */
2263df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
2264df8bae1dSRodney W. Grimes 		} else {
226578b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
226678b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, tlen);
226778b50714SRobert Watson 			TCPSTAT_INC(tcps_pawsdrop);
226807fd333dSMatthew Dillon 			if (tlen)
2269df8bae1dSRodney W. Grimes 				goto dropafterack;
22701ab4789dSMatthew Dillon 			goto drop;
22711ab4789dSMatthew Dillon 		}
2272df8bae1dSRodney W. Grimes 	}
2273df8bae1dSRodney W. Grimes 
2274a0292f23SGarrett Wollman 	/*
227580ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
227680ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
227780ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
227880ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
227980ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
228080ab7c0eSGarrett Wollman 	 */
2281a57815efSBosko Milekic 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
2282a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2283a57815efSBosko Milekic 		goto dropwithreset;
2284a57815efSBosko Milekic 	}
228580ab7c0eSGarrett Wollman 
2286fb59c426SYoshinobu Inoue 	todrop = tp->rcv_nxt - th->th_seq;
2287df8bae1dSRodney W. Grimes 	if (todrop > 0) {
2288831ad37eSXin LI 		if (thflags & TH_SYN) {
2289fb59c426SYoshinobu Inoue 			thflags &= ~TH_SYN;
2290fb59c426SYoshinobu Inoue 			th->th_seq++;
2291fb59c426SYoshinobu Inoue 			if (th->th_urp > 1)
2292fb59c426SYoshinobu Inoue 				th->th_urp--;
2293df8bae1dSRodney W. Grimes 			else
2294fb59c426SYoshinobu Inoue 				thflags &= ~TH_URG;
2295df8bae1dSRodney W. Grimes 			todrop--;
2296df8bae1dSRodney W. Grimes 		}
2297df8bae1dSRodney W. Grimes 		/*
2298dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
2299df8bae1dSRodney W. Grimes 		 */
2300fb59c426SYoshinobu Inoue 		if (todrop > tlen
2301fb59c426SYoshinobu Inoue 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
2302dac20301SGarrett Wollman 			/*
2303dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
2304dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
2305dac20301SGarrett Wollman 			 * of sequence; drop it.
2306dac20301SGarrett Wollman 			 */
2307fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
2308dac20301SGarrett Wollman 
2309df8bae1dSRodney W. Grimes 			/*
2310dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
2311dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
2312df8bae1dSRodney W. Grimes 			 */
2313dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
2314fb59c426SYoshinobu Inoue 			todrop = tlen;
231578b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
231678b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, todrop);
2317df8bae1dSRodney W. Grimes 		} else {
231878b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpartduppack);
231978b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop);
2320df8bae1dSRodney W. Grimes 		}
2321fb59c426SYoshinobu Inoue 		drop_hdrlen += todrop;	/* drop from the top afterwards */
2322fb59c426SYoshinobu Inoue 		th->th_seq += todrop;
2323fb59c426SYoshinobu Inoue 		tlen -= todrop;
2324fb59c426SYoshinobu Inoue 		if (th->th_urp > todrop)
2325fb59c426SYoshinobu Inoue 			th->th_urp -= todrop;
2326df8bae1dSRodney W. Grimes 		else {
2327fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;
2328fb59c426SYoshinobu Inoue 			th->th_urp = 0;
2329df8bae1dSRodney W. Grimes 		}
2330df8bae1dSRodney W. Grimes 	}
2331df8bae1dSRodney W. Grimes 
2332df8bae1dSRodney W. Grimes 	/*
2333df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
2334df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
2335df8bae1dSRodney W. Grimes 	 */
2336df8bae1dSRodney W. Grimes 	if ((so->so_state & SS_NOFDREF) &&
2337fb59c426SYoshinobu Inoue 	    tp->t_state > TCPS_CLOSE_WAIT && tlen) {
2338ff9b006dSJulien Charbon 		KASSERT(ti_locked == TI_RLOCKED, ("%s: SS_NOFDEREF && "
2339252ca428SRobert Watson 		    "CLOSE_WAIT && tlen ti_locked %d", __func__, ti_locked));
2340ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2341252ca428SRobert Watson 
234207dacf03SAndre Oppermann 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
234307dacf03SAndre Oppermann 			log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data "
234407dacf03SAndre Oppermann 			    "after socket was closed, "
234507dacf03SAndre Oppermann 			    "sending RST and removing tcpcb\n",
2346e31d8aa3SMike Silbersack 			    s, __func__, tcpstates[tp->t_state], tlen);
2347773673c1SAndre Oppermann 			free(s, M_TCPLOG);
2348773673c1SAndre Oppermann 		}
2349df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
235078b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvafterclose);
2351a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
2352df8bae1dSRodney W. Grimes 		goto dropwithreset;
2353df8bae1dSRodney W. Grimes 	}
2354df8bae1dSRodney W. Grimes 
2355df8bae1dSRodney W. Grimes 	/*
2356df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
2357df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
2358df8bae1dSRodney W. Grimes 	 */
2359fb59c426SYoshinobu Inoue 	todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
2360df8bae1dSRodney W. Grimes 	if (todrop > 0) {
236178b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvpackafterwin);
2362fb59c426SYoshinobu Inoue 		if (todrop >= tlen) {
236378b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen);
2364df8bae1dSRodney W. Grimes 			/*
2365df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
2366df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
2367df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
2368df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
2369df8bae1dSRodney W. Grimes 			 * and ack.
2370df8bae1dSRodney W. Grimes 			 */
2371fb59c426SYoshinobu Inoue 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
2372df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
237378b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvwinprobe);
2374df8bae1dSRodney W. Grimes 			} else
2375df8bae1dSRodney W. Grimes 				goto dropafterack;
2376df8bae1dSRodney W. Grimes 		} else
237778b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2378df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
2379fb59c426SYoshinobu Inoue 		tlen -= todrop;
2380fb59c426SYoshinobu Inoue 		thflags &= ~(TH_PUSH|TH_FIN);
2381df8bae1dSRodney W. Grimes 	}
2382df8bae1dSRodney W. Grimes 
2383df8bae1dSRodney W. Grimes 	/*
2384df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
2385df8bae1dSRodney W. Grimes 	 * record its timestamp.
2386cf09195bSPaul Saab 	 * NOTE:
2387cf09195bSPaul Saab 	 * 1) That the test incorporates suggestions from the latest
2388a0292f23SGarrett Wollman 	 *    proposal of the tcplw@cray.com list (Braden 1993/04/26).
2389cf09195bSPaul Saab 	 * 2) That updating only on newer timestamps interferes with
2390cf09195bSPaul Saab 	 *    our earlier PAWS tests, so this check should be solely
2391cf09195bSPaul Saab 	 *    predicated on the sequence space of this segment.
2392cf09195bSPaul Saab 	 * 3) That we modify the segment boundary check to be
2393cf09195bSPaul Saab 	 *        Last.ACK.Sent <= SEG.SEQ + SEG.Len
2394cf09195bSPaul Saab 	 *    instead of RFC1323's
2395cf09195bSPaul Saab 	 *        Last.ACK.Sent < SEG.SEQ + SEG.Len,
2396cf09195bSPaul Saab 	 *    This modified check allows us to overcome RFC1323's
2397cf09195bSPaul Saab 	 *    limitations as described in Stevens TCP/IP Illustrated
2398cf09195bSPaul Saab 	 *    Vol. 2 p.869. In such cases, we can still calculate the
2399cf09195bSPaul Saab 	 *    RTT correctly when RCV.NXT == Last.ACK.Sent.
2400df8bae1dSRodney W. Grimes 	 */
2401be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 &&
2402cf09195bSPaul Saab 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
2403cf09195bSPaul Saab 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
2404cf09195bSPaul Saab 		((thflags & (TH_SYN|TH_FIN)) != 0))) {
2405d8951c8aSBjoern A. Zeeb 		tp->ts_recent_age = tcp_ts_getticks();
2406a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
2407df8bae1dSRodney W. Grimes 	}
2408df8bae1dSRodney W. Grimes 
2409df8bae1dSRodney W. Grimes 	/*
2410a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
2411a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
2412a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
2413a0292f23SGarrett Wollman 	 */
2414fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) == 0) {
2415a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
2416281a0fd4SPatrick Kelsey 		    (tp->t_flags & TF_NEEDSYN)) {
2417281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
2418281a0fd4SPatrick Kelsey 			if (tp->t_state == TCPS_SYN_RECEIVED &&
241968bd7ed1SJonathan T. Looney 			    IS_FASTOPEN(tp->t_flags)) {
2420281a0fd4SPatrick Kelsey 				tp->snd_wnd = tiwin;
2421281a0fd4SPatrick Kelsey 				cc_conn_init(tp);
2422281a0fd4SPatrick Kelsey 			}
2423281a0fd4SPatrick Kelsey #endif
2424a0292f23SGarrett Wollman 			goto step6;
2425281a0fd4SPatrick Kelsey 		} else if (tp->t_flags & TF_ACKNOW)
24265e1aa279SDavid Malone 			goto dropafterack;
2427a0292f23SGarrett Wollman 		else
2428a0292f23SGarrett Wollman 			goto drop;
2429a0292f23SGarrett Wollman 	}
2430df8bae1dSRodney W. Grimes 
2431df8bae1dSRodney W. Grimes 	/*
2432df8bae1dSRodney W. Grimes 	 * Ack processing.
2433df8bae1dSRodney W. Grimes 	 */
2434df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2435df8bae1dSRodney W. Grimes 
2436df8bae1dSRodney W. Grimes 	/*
2437764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
2438764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
2439764d8cefSBill Fenner 	 * The ACK was checked above.
2440df8bae1dSRodney W. Grimes 	 */
2441df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2442a0292f23SGarrett Wollman 
244378b50714SRobert Watson 		TCPSTAT_INC(tcps_connects);
2444df8bae1dSRodney W. Grimes 		soisconnected(so);
2445df8bae1dSRodney W. Grimes 		/* Do window scaling? */
2446df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2447df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
2448df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
2449464fcfbcSAndre Oppermann 			tp->snd_wnd = tiwin;
2450df8bae1dSRodney W. Grimes 		}
2451a0292f23SGarrett Wollman 		/*
2452a0292f23SGarrett Wollman 		 * Make transitions:
2453a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
2454a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
2455a0292f23SGarrett Wollman 		 */
24569b8b58e0SJonathan Lemon 		tp->t_starttime = ticks;
2457a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDFIN) {
245857f60867SMark Johnston 			tcp_state_change(tp, TCPS_FIN_WAIT_1);
2459a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDFIN;
24607ff19458SPaul Traina 		} else {
246157f60867SMark Johnston 			tcp_state_change(tp, TCPS_ESTABLISHED);
2462d9fae5abSAndriy Gapon 			TCP_PROBE5(accept__established, NULL, tp,
24632b9c9984SGeorge V. Neville-Neil 			    m, tp, th);
2464281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
2465281a0fd4SPatrick Kelsey 			if (tp->t_tfo_pending) {
2466281a0fd4SPatrick Kelsey 				tcp_fastopen_decrement_counter(tp->t_tfo_pending);
2467281a0fd4SPatrick Kelsey 				tp->t_tfo_pending = NULL;
2468281a0fd4SPatrick Kelsey 
2469281a0fd4SPatrick Kelsey 				/*
2470281a0fd4SPatrick Kelsey 				 * Account for the ACK of our SYN prior to
2471281a0fd4SPatrick Kelsey 				 * regular ACK processing below.
2472281a0fd4SPatrick Kelsey 				 */
2473281a0fd4SPatrick Kelsey 				tp->snd_una++;
2474281a0fd4SPatrick Kelsey 			}
2475281a0fd4SPatrick Kelsey 			/*
2476281a0fd4SPatrick Kelsey 			 * TFO connections call cc_conn_init() during SYN
2477281a0fd4SPatrick Kelsey 			 * processing.  Calling it again here for such
2478281a0fd4SPatrick Kelsey 			 * connections is not harmless as it would undo the
2479281a0fd4SPatrick Kelsey 			 * snd_cwnd reduction that occurs when a TFO SYN|ACK
2480281a0fd4SPatrick Kelsey 			 * is retransmitted.
2481281a0fd4SPatrick Kelsey 			 */
248268bd7ed1SJonathan T. Looney 			if (!IS_FASTOPEN(tp->t_flags))
2483281a0fd4SPatrick Kelsey #endif
2484dbc42409SLawrence Stewart 				cc_conn_init(tp);
24859077f387SGleb Smirnoff 			tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp));
24867ff19458SPaul Traina 		}
2487a0292f23SGarrett Wollman 		/*
2488a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
2489a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
2490a0292f23SGarrett Wollman 		 */
2491fb59c426SYoshinobu Inoue 		if (tlen == 0 && (thflags & TH_FIN) == 0)
2492fb59c426SYoshinobu Inoue 			(void) tcp_reass(tp, (struct tcphdr *)0, 0,
2493a0292f23SGarrett Wollman 			    (struct mbuf *)0);
249460ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq - 1;
249593b0017fSPhilippe Charnier 		/* FALLTHROUGH */
2496df8bae1dSRodney W. Grimes 
2497df8bae1dSRodney W. Grimes 	/*
2498df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
2499df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
2500fb59c426SYoshinobu Inoue 	 *	tp->snd_una < th->th_ack <= tp->snd_max
2501fb59c426SYoshinobu Inoue 	 * then advance tp->snd_una to th->th_ack and drop
2502df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
2503df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
2504df8bae1dSRodney W. Grimes 	 */
2505df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
2506df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
2507df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
2508df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
2509df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
2510df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
25115a53ca16SPaul Saab 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
251278b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvacktoomuch);
25135a53ca16SPaul Saab 			goto dropafterack;
25145a53ca16SPaul Saab 		}
25153529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
2516fc30a251SAndre Oppermann 		    ((to.to_flags & TOF_SACK) ||
2517fc30a251SAndre Oppermann 		     !TAILQ_EMPTY(&tp->snd_holes)))
2518021eaf79SHiren Panchasara 			sack_changed = tcp_sack_doack(tp, &to, th->th_ack);
251912eeb81fSHiren Panchasara 		else
252012eeb81fSHiren Panchasara 			/*
252112eeb81fSHiren Panchasara 			 * Reset the value so that previous (valid) value
252212eeb81fSHiren Panchasara 			 * from the last ack with SACK doesn't get used.
252312eeb81fSHiren Panchasara 			 */
252412eeb81fSHiren Panchasara 			tp->sackhint.sacked_bytes = 0;
252539bc9de5SLawrence Stewart 
2526bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
252739bc9de5SLawrence Stewart 		/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
252839bc9de5SLawrence Stewart 		hhook_run_tcp_est_in(tp, th, &to);
2529bd79708dSJonathan T. Looney #endif
253039bc9de5SLawrence Stewart 
2531fb59c426SYoshinobu Inoue 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
25320c39d38dSGleb Smirnoff 			u_int maxseg;
25330c39d38dSGleb Smirnoff 
25340c39d38dSGleb Smirnoff 			maxseg = tcp_maxseg(tp);
2535021eaf79SHiren Panchasara 			if (tlen == 0 &&
2536021eaf79SHiren Panchasara 			    (tiwin == tp->snd_wnd ||
2537021eaf79SHiren Panchasara 			    (tp->t_flags & TF_SACK_PERMIT))) {
253862b90589SPeter Wemm 				/*
253962b90589SPeter Wemm 				 * If this is the first time we've seen a
254062b90589SPeter Wemm 				 * FIN from the remote, this is not a
254162b90589SPeter Wemm 				 * duplicate and it needs to be processed
254262b90589SPeter Wemm 				 * normally.  This happens during a
254362b90589SPeter Wemm 				 * simultaneous close.
254462b90589SPeter Wemm 				 */
254562b90589SPeter Wemm 				if ((thflags & TH_FIN) &&
254662b90589SPeter Wemm 				    (TCPS_HAVERCVDFIN(tp->t_state) == 0)) {
254762b90589SPeter Wemm 					tp->t_dupacks = 0;
254862b90589SPeter Wemm 					break;
254962b90589SPeter Wemm 				}
255078b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvdupack);
2551df8bae1dSRodney W. Grimes 				/*
2552df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
2553df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
2554df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
25555f30ec9bSEitan Adler 				 * change and FIN isn't set),
25565f30ec9bSEitan Adler 				 * the ack is the biggest we've
2557df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
2558a4641f4eSPedro F. Giffuni 				 * threshold of them, assume a packet
2559df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
2560df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
2561df8bae1dSRodney W. Grimes 				 * window so we send only this one
2562df8bae1dSRodney W. Grimes 				 * packet.
2563df8bae1dSRodney W. Grimes 				 *
2564df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
2565df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
2566df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
2567df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
2568df8bae1dSRodney W. Grimes 				 * the new ssthresh).
2569df8bae1dSRodney W. Grimes 				 *
2570df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
2571df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
2572df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
2573df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
2574df8bae1dSRodney W. Grimes 				 * network.
2575f2512ba1SRui Paulo 				 *
2576f2512ba1SRui Paulo 				 * When using TCP ECN, notify the peer that
2577f2512ba1SRui Paulo 				 * we reduced the cwnd.
2578df8bae1dSRodney W. Grimes 				 */
2579021eaf79SHiren Panchasara 				/*
2580021eaf79SHiren Panchasara 				 * Following 2 kinds of acks should not affect
2581021eaf79SHiren Panchasara 				 * dupack counting:
2582021eaf79SHiren Panchasara 				 * 1) Old acks
2583021eaf79SHiren Panchasara 				 * 2) Acks with SACK but without any new SACK
2584021eaf79SHiren Panchasara 				 * information in them. These could result from
2585021eaf79SHiren Panchasara 				 * any anomaly in the network like a switch
2586021eaf79SHiren Panchasara 				 * duplicating packets or a possible DoS attack.
2587021eaf79SHiren Panchasara 				 */
2588021eaf79SHiren Panchasara 				if (th->th_ack != tp->snd_una ||
2589021eaf79SHiren Panchasara 				    ((tp->t_flags & TF_SACK_PERMIT) &&
2590021eaf79SHiren Panchasara 				    !sack_changed))
2591021eaf79SHiren Panchasara 					break;
2592021eaf79SHiren Panchasara 				else if (!tcp_timer_active(tp, TT_REXMT))
2593df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
2594cb942153SJeffrey Hsu 				else if (++tp->t_dupacks > tcprexmtthresh ||
2595dbc42409SLawrence Stewart 				     IN_FASTRECOVERY(tp->t_flags)) {
25964b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
25974b7b743cSLawrence Stewart 					    CC_DUPACK);
25983529149eSAndre Oppermann 					if ((tp->t_flags & TF_SACK_PERMIT) &&
2599dbc42409SLawrence Stewart 					    IN_FASTRECOVERY(tp->t_flags)) {
2600808f11b7SPaul Saab 						int awnd;
260125e6f9edSPaul Saab 
260225e6f9edSPaul Saab 						/*
260325e6f9edSPaul Saab 						 * Compute the amount of data in flight first.
26044c3972f0SHiren Panchasara 						 * We can inject new data into the pipe iff
260525e6f9edSPaul Saab 						 * we have less than 1/2 the original window's
260625e6f9edSPaul Saab 						 * worth of data in flight.
260725e6f9edSPaul Saab 						 */
260812eeb81fSHiren Panchasara 						if (V_tcp_do_rfc6675_pipe)
260912eeb81fSHiren Panchasara 							awnd = tcp_compute_pipe(tp);
261012eeb81fSHiren Panchasara 						else
2611808f11b7SPaul Saab 							awnd = (tp->snd_nxt - tp->snd_fack) +
26120077b016SPaul Saab 								tp->sackhint.sack_bytes_rexmit;
261312eeb81fSHiren Panchasara 
2614808f11b7SPaul Saab 						if (awnd < tp->snd_ssthresh) {
26150c39d38dSGleb Smirnoff 							tp->snd_cwnd += maxseg;
26164e7f7553SHiren Panchasara 							/*
26174e7f7553SHiren Panchasara 							 * RFC5681 Section 3.2 talks about cwnd
26184e7f7553SHiren Panchasara 							 * inflation on additional dupacks and
26194e7f7553SHiren Panchasara 							 * deflation on recovering from loss.
26204e7f7553SHiren Panchasara 							 *
26214e7f7553SHiren Panchasara 							 * We keep cwnd into check so that
26224e7f7553SHiren Panchasara 							 * we don't have to 'deflate' it when we
26234e7f7553SHiren Panchasara 							 * get out of recovery.
26244e7f7553SHiren Panchasara 							 */
262525e6f9edSPaul Saab 							if (tp->snd_cwnd > tp->snd_ssthresh)
262625e6f9edSPaul Saab 								tp->snd_cwnd = tp->snd_ssthresh;
262725e6f9edSPaul Saab 						}
262825e6f9edSPaul Saab 					} else
26290c39d38dSGleb Smirnoff 						tp->snd_cwnd += maxseg;
263055bceb1eSRandall Stewart 					(void) tp->t_fb->tfb_tcp_output(tp);
263146f58482SJonathan Lemon 					goto drop;
2632cb942153SJeffrey Hsu 				} else if (tp->t_dupacks == tcprexmtthresh) {
2633cb942153SJeffrey Hsu 					tcp_seq onxt = tp->snd_nxt;
2634a0445c2eSJayanth Vijayaraghavan 
2635a0445c2eSJayanth Vijayaraghavan 					/*
2636a0445c2eSJayanth Vijayaraghavan 					 * If we're doing sack, check to
2637a0445c2eSJayanth Vijayaraghavan 					 * see if we're already in sack
2638a0445c2eSJayanth Vijayaraghavan 					 * recovery. If we're not doing sack,
2639a0445c2eSJayanth Vijayaraghavan 					 * check to see if we're in newreno
2640a0445c2eSJayanth Vijayaraghavan 					 * recovery.
2641a0445c2eSJayanth Vijayaraghavan 					 */
26423529149eSAndre Oppermann 					if (tp->t_flags & TF_SACK_PERMIT) {
2643dbc42409SLawrence Stewart 						if (IN_FASTRECOVERY(tp->t_flags)) {
2644a0445c2eSJayanth Vijayaraghavan 							tp->t_dupacks = 0;
2645a0445c2eSJayanth Vijayaraghavan 							break;
2646a0445c2eSJayanth Vijayaraghavan 						}
2647dbc42409SLawrence Stewart 					} else {
2648a0445c2eSJayanth Vijayaraghavan 						if (SEQ_LEQ(th->th_ack,
26499d11646dSJeffrey Hsu 						    tp->snd_recover)) {
2650cb942153SJeffrey Hsu 							tp->t_dupacks = 0;
2651cb942153SJeffrey Hsu 							break;
265246f58482SJonathan Lemon 						}
2653a0445c2eSJayanth Vijayaraghavan 					}
2654dbc42409SLawrence Stewart 					/* Congestion signal before ack. */
2655dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_NDUPACK);
26564b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
26574b7b743cSLawrence Stewart 					    CC_DUPACK);
2658b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
26599b8b58e0SJonathan Lemon 					tp->t_rtttime = 0;
26603529149eSAndre Oppermann 					if (tp->t_flags & TF_SACK_PERMIT) {
266178b50714SRobert Watson 						TCPSTAT_INC(
266278b50714SRobert Watson 						    tcps_sack_recovery_episode);
2663a55db2b6SPaul Saab 						tp->sack_newdata = tp->snd_nxt;
26644e7f7553SHiren Panchasara 						if (CC_ALGO(tp)->cong_signal == NULL)
26650c39d38dSGleb Smirnoff 							tp->snd_cwnd = maxseg;
266655bceb1eSRandall Stewart 						(void) tp->t_fb->tfb_tcp_output(tp);
26676d90faf3SPaul Saab 						goto drop;
26686d90faf3SPaul Saab 					}
2669fb59c426SYoshinobu Inoue 					tp->snd_nxt = th->th_ack;
26704e7f7553SHiren Panchasara 					if (CC_ALGO(tp)->cong_signal == NULL)
26710c39d38dSGleb Smirnoff 						tp->snd_cwnd = maxseg;
267255bceb1eSRandall Stewart 					(void) tp->t_fb->tfb_tcp_output(tp);
267348d2549cSJeffrey Hsu 					KASSERT(tp->snd_limited <= 2,
2674302ce8d6SAndre Oppermann 					    ("%s: tp->snd_limited too big",
2675302ce8d6SAndre Oppermann 					    __func__));
26764e7f7553SHiren Panchasara 					if (CC_ALGO(tp)->cong_signal == NULL)
2677df8bae1dSRodney W. Grimes 						tp->snd_cwnd = tp->snd_ssthresh +
26780c39d38dSGleb Smirnoff 						    maxseg *
267948d2549cSJeffrey Hsu 						    (tp->t_dupacks - tp->snd_limited);
2680df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
2681df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
2682df8bae1dSRodney W. Grimes 					goto drop;
2683603724d3SBjoern A. Zeeb 				} else if (V_tcp_do_rfc3042) {
268462d4443fSHiren Panchasara 					/*
268562d4443fSHiren Panchasara 					 * Process first and second duplicate
268662d4443fSHiren Panchasara 					 * ACKs. Each indicates a segment
268762d4443fSHiren Panchasara 					 * leaving the network, creating room
268862d4443fSHiren Panchasara 					 * for more. Make sure we can send a
268962d4443fSHiren Panchasara 					 * packet on reception of each duplicate
269062d4443fSHiren Panchasara 					 * ACK by increasing snd_cwnd by one
269162d4443fSHiren Panchasara 					 * segment. Restore the original
269262d4443fSHiren Panchasara 					 * snd_cwnd after packet transmission.
269362d4443fSHiren Panchasara 					 */
26944b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
26954b7b743cSLawrence Stewart 					    CC_DUPACK);
26963ac12506SJonathan T. Looney 					uint32_t oldcwnd = tp->snd_cwnd;
269748d2549cSJeffrey Hsu 					tcp_seq oldsndmax = tp->snd_max;
269848d2549cSJeffrey Hsu 					u_int sent;
26995628dd08SAndre Oppermann 					int avail;
270089c02376SJeffrey Hsu 
2701582a954bSJeffrey Hsu 					KASSERT(tp->t_dupacks == 1 ||
2702582a954bSJeffrey Hsu 					    tp->t_dupacks == 2,
2703302ce8d6SAndre Oppermann 					    ("%s: dupacks not 1 or 2",
2704302ce8d6SAndre Oppermann 					    __func__));
270561a36e3dSJeffrey Hsu 					if (tp->t_dupacks == 1)
270648d2549cSJeffrey Hsu 						tp->snd_limited = 0;
270761a36e3dSJeffrey Hsu 					tp->snd_cwnd =
270861a36e3dSJeffrey Hsu 					    (tp->snd_nxt - tp->snd_una) +
270961a36e3dSJeffrey Hsu 					    (tp->t_dupacks - tp->snd_limited) *
27100c39d38dSGleb Smirnoff 					    maxseg;
27115628dd08SAndre Oppermann 					/*
27125628dd08SAndre Oppermann 					 * Only call tcp_output when there
27135628dd08SAndre Oppermann 					 * is new data available to be sent.
27145628dd08SAndre Oppermann 					 * Otherwise we would send pure ACKs.
27155628dd08SAndre Oppermann 					 */
27165628dd08SAndre Oppermann 					SOCKBUF_LOCK(&so->so_snd);
2717cfa6009eSGleb Smirnoff 					avail = sbavail(&so->so_snd) -
27185628dd08SAndre Oppermann 					    (tp->snd_nxt - tp->snd_una);
27195628dd08SAndre Oppermann 					SOCKBUF_UNLOCK(&so->so_snd);
27205628dd08SAndre Oppermann 					if (avail > 0)
272155bceb1eSRandall Stewart 						(void) tp->t_fb->tfb_tcp_output(tp);
272248d2549cSJeffrey Hsu 					sent = tp->snd_max - oldsndmax;
27230c39d38dSGleb Smirnoff 					if (sent > maxseg) {
272489c02376SJeffrey Hsu 						KASSERT((tp->t_dupacks == 2 &&
272589c02376SJeffrey Hsu 						    tp->snd_limited == 0) ||
27260c39d38dSGleb Smirnoff 						   (sent == maxseg + 1 &&
272789c02376SJeffrey Hsu 						    tp->t_flags & TF_SENTFIN),
2728302ce8d6SAndre Oppermann 						    ("%s: sent too much",
2729302ce8d6SAndre Oppermann 						    __func__));
273048d2549cSJeffrey Hsu 						tp->snd_limited = 2;
273148d2549cSJeffrey Hsu 					} else if (sent > 0)
273248d2549cSJeffrey Hsu 						++tp->snd_limited;
2733582a954bSJeffrey Hsu 					tp->snd_cwnd = oldcwnd;
2734582a954bSJeffrey Hsu 					goto drop;
2735df8bae1dSRodney W. Grimes 				}
2736021eaf79SHiren Panchasara 			}
2737df8bae1dSRodney W. Grimes 			break;
2738021eaf79SHiren Panchasara 		} else {
2739021eaf79SHiren Panchasara 			/*
2740021eaf79SHiren Panchasara 			 * This ack is advancing the left edge, reset the
2741021eaf79SHiren Panchasara 			 * counter.
2742021eaf79SHiren Panchasara 			 */
2743021eaf79SHiren Panchasara 			tp->t_dupacks = 0;
2744021eaf79SHiren Panchasara 			/*
2745021eaf79SHiren Panchasara 			 * If this ack also has new SACK info, increment the
2746021eaf79SHiren Panchasara 			 * counter as per rfc6675.
2747021eaf79SHiren Panchasara 			 */
2748021eaf79SHiren Panchasara 			if ((tp->t_flags & TF_SACK_PERMIT) && sack_changed)
2749021eaf79SHiren Panchasara 				tp->t_dupacks++;
2750df8bae1dSRodney W. Grimes 		}
2751cb942153SJeffrey Hsu 
2752302ce8d6SAndre Oppermann 		KASSERT(SEQ_GT(th->th_ack, tp->snd_una),
2753302ce8d6SAndre Oppermann 		    ("%s: th_ack <= snd_una", __func__));
2754cb942153SJeffrey Hsu 
2755df8bae1dSRodney W. Grimes 		/*
2756df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
2757df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
2758df8bae1dSRodney W. Grimes 		 */
2759dbc42409SLawrence Stewart 		if (IN_FASTRECOVERY(tp->t_flags)) {
2760cb942153SJeffrey Hsu 			if (SEQ_LT(th->th_ack, tp->snd_recover)) {
27613529149eSAndre Oppermann 				if (tp->t_flags & TF_SACK_PERMIT)
27626d90faf3SPaul Saab 					tcp_sack_partialack(tp, th);
27636d90faf3SPaul Saab 				else
2764c068736aSJeffrey Hsu 					tcp_newreno_partial_ack(tp, th);
2765dbc42409SLawrence Stewart 			} else
2766dbc42409SLawrence Stewart 				cc_post_recovery(tp, th);
276746f58482SJonathan Lemon 		}
2768a0292f23SGarrett Wollman 		/*
2769a0292f23SGarrett Wollman 		 * If we reach this point, ACK is not a duplicate,
2770a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
2771a0292f23SGarrett Wollman 		 */
2772a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
2773a0292f23SGarrett Wollman 			/*
2774a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
2775a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
277607e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
277707e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
277807e43e10SAndras Olah 			 * we can do window scaling.
2779a0292f23SGarrett Wollman 			 */
2780a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
2781a0292f23SGarrett Wollman 			tp->snd_una++;
278207e43e10SAndras Olah 			/* Do window scaling? */
278307e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
278407e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
278507e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2786464fcfbcSAndre Oppermann 				/* Send window already scaled. */
278707e43e10SAndras Olah 			}
2788a0292f23SGarrett Wollman 		}
2789a0292f23SGarrett Wollman 
2790a0292f23SGarrett Wollman process_ACK:
27918501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
27927cfc6904SRobert Watson 
2793dbc42409SLawrence Stewart 		acked = BYTES_THIS_ACK(tp, th);
2794b8c2cd15SJonathan T. Looney 		KASSERT(acked >= 0, ("%s: acked unexepectedly negative "
2795b8c2cd15SJonathan T. Looney 		    "(tp->snd_una=%u, th->th_ack=%u, tp=%p, m=%p)", __func__,
2796b8c2cd15SJonathan T. Looney 		    tp->snd_una, th->th_ack, tp, m));
27974b7b743cSLawrence Stewart 		TCPSTAT_ADD(tcps_rcvackpack, nsegs);
279878b50714SRobert Watson 		TCPSTAT_ADD(tcps_rcvackbyte, acked);
2799df8bae1dSRodney W. Grimes 
2800df8bae1dSRodney W. Grimes 		/*
28019b8b58e0SJonathan Lemon 		 * If we just performed our first retransmit, and the ACK
28029b8b58e0SJonathan Lemon 		 * arrives within our recovery window, then it was a mistake
28039b8b58e0SJonathan Lemon 		 * to do the retransmit in the first place.  Recover our
28049b8b58e0SJonathan Lemon 		 * original cwnd and ssthresh, and proceed to transmit where
28059b8b58e0SJonathan Lemon 		 * we left off.
28069b8b58e0SJonathan Lemon 		 */
2807672dc4aeSJohn Baldwin 		if (tp->t_rxtshift == 1 && tp->t_flags & TF_PREVVALID &&
2808672dc4aeSJohn Baldwin 		    (int)(ticks - tp->t_badrxtwin) < 0)
2809dbc42409SLawrence Stewart 			cc_cong_signal(tp, th, CC_RTO_ERR);
28109b8b58e0SJonathan Lemon 
28119b8b58e0SJonathan Lemon 		/*
2812df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
2813df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
2814df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
2815df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
2816df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
2817df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
2818df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
2819fa55172bSMatthew Dillon 		 *
2820fa55172bSMatthew Dillon 		 * Some boxes send broken timestamp replies
2821fa55172bSMatthew Dillon 		 * during the SYN+ACK phase, ignore
2822fa55172bSMatthew Dillon 		 * timestamps of 0 or we could calculate a
2823fa55172bSMatthew Dillon 		 * huge RTT and blow up the retransmit timer.
2824df8bae1dSRodney W. Grimes 		 */
2825d8951c8aSBjoern A. Zeeb 		if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) {
28263ac12506SJonathan T. Looney 			uint32_t t;
2827d8951c8aSBjoern A. Zeeb 
2828d8951c8aSBjoern A. Zeeb 			t = tcp_ts_getticks() - to.to_tsecr;
2829d8951c8aSBjoern A. Zeeb 			if (!tp->t_rttlow || tp->t_rttlow > t)
2830d8951c8aSBjoern A. Zeeb 				tp->t_rttlow = t;
2831d8951c8aSBjoern A. Zeeb 			tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1);
2832fa55172bSMatthew Dillon 		} else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
2833eaf80179SAndre Oppermann 			if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime)
2834eaf80179SAndre Oppermann 				tp->t_rttlow = ticks - tp->t_rtttime;
28359b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
2836fa55172bSMatthew Dillon 		}
2837df8bae1dSRodney W. Grimes 
2838df8bae1dSRodney W. Grimes 		/*
2839df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
2840df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
2841df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
2842df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
2843df8bae1dSRodney W. Grimes 		 */
2844fb59c426SYoshinobu Inoue 		if (th->th_ack == tp->snd_max) {
2845b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
2846df8bae1dSRodney W. Grimes 			needoutput = 1;
2847b8152ba7SAndre Oppermann 		} else if (!tcp_timer_active(tp, TT_PERSIST))
2848b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
2849a0292f23SGarrett Wollman 
2850a0292f23SGarrett Wollman 		/*
2851a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
2852a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
2853a0292f23SGarrett Wollman 		 */
2854a0292f23SGarrett Wollman 		if (acked == 0)
2855a0292f23SGarrett Wollman 			goto step6;
2856a0292f23SGarrett Wollman 
2857df8bae1dSRodney W. Grimes 		/*
2858dbc42409SLawrence Stewart 		 * Let the congestion control algorithm update congestion
2859dbc42409SLawrence Stewart 		 * control related information. This typically means increasing
2860dbc42409SLawrence Stewart 		 * the congestion window.
2861df8bae1dSRodney W. Grimes 		 */
28624b7b743cSLawrence Stewart 		cc_ack_received(tp, th, nsegs, CC_ACK);
2863dbc42409SLawrence Stewart 
28645905999bSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
2865cfa6009eSGleb Smirnoff 		if (acked > sbavail(&so->so_snd)) {
2866b8c2cd15SJonathan T. Looney 			if (tp->snd_wnd >= sbavail(&so->so_snd))
2867cfa6009eSGleb Smirnoff 				tp->snd_wnd -= sbavail(&so->so_snd);
2868b8c2cd15SJonathan T. Looney 			else
2869b8c2cd15SJonathan T. Looney 				tp->snd_wnd = 0;
2870c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd,
2871cfa6009eSGleb Smirnoff 			    (int)sbavail(&so->so_snd));
2872df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
2873df8bae1dSRodney W. Grimes 		} else {
2874c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd, acked);
28753ac12506SJonathan T. Looney 			if (tp->snd_wnd >= (uint32_t) acked)
287660ee3bb2SAndre Oppermann 				tp->snd_wnd -= acked;
2877b8c2cd15SJonathan T. Looney 			else
2878b8c2cd15SJonathan T. Looney 				tp->snd_wnd = 0;
2879df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
2880df8bae1dSRodney W. Grimes 		}
2881564aab1fSAndre Oppermann 		/* NB: sowwakeup_locked() does an implicit unlock. */
28821e4d7da7SRobert Watson 		sowwakeup_locked(so);
2883c11a15bfSGleb Smirnoff 		m_freem(mfree);
2884e8949f74SAndre Oppermann 		/* Detect una wraparound. */
2885dbc42409SLawrence Stewart 		if (!IN_RECOVERY(tp->t_flags) &&
28869d11646dSJeffrey Hsu 		    SEQ_GT(tp->snd_una, tp->snd_recover) &&
28879d11646dSJeffrey Hsu 		    SEQ_LEQ(th->th_ack, tp->snd_recover))
28889d11646dSJeffrey Hsu 			tp->snd_recover = th->th_ack - 1;
2889dbc42409SLawrence Stewart 		/* XXXLAS: Can this be moved up into cc_post_recovery? */
2890dbc42409SLawrence Stewart 		if (IN_RECOVERY(tp->t_flags) &&
289124cb0f22SLawrence Stewart 		    SEQ_GEQ(th->th_ack, tp->snd_recover)) {
2892dbc42409SLawrence Stewart 			EXIT_RECOVERY(tp->t_flags);
289324cb0f22SLawrence Stewart 		}
2894fb59c426SYoshinobu Inoue 		tp->snd_una = th->th_ack;
28953529149eSAndre Oppermann 		if (tp->t_flags & TF_SACK_PERMIT) {
28966d90faf3SPaul Saab 			if (SEQ_GT(tp->snd_una, tp->snd_recover))
28976d90faf3SPaul Saab 				tp->snd_recover = tp->snd_una;
28986d90faf3SPaul Saab 		}
2899df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2900df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
2901df8bae1dSRodney W. Grimes 
2902df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2903df8bae1dSRodney W. Grimes 
2904df8bae1dSRodney W. Grimes 		/*
2905df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
2906df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
2907df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
2908df8bae1dSRodney W. Grimes 		 */
2909df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2910df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2911df8bae1dSRodney W. Grimes 				/*
2912df8bae1dSRodney W. Grimes 				 * If we can't receive any more
2913df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
2914df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
2915df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
2916df8bae1dSRodney W. Grimes 				 * we'll hang forever.
2917e8949f74SAndre Oppermann 				 *
2918e8949f74SAndre Oppermann 				 * XXXjl:
2919340c35deSJonathan Lemon 				 * we should release the tp also, and use a
2920340c35deSJonathan Lemon 				 * compressed state.
2921340c35deSJonathan Lemon 				 */
2922c0b99ffaSRobert Watson 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
292303e49181SSeigo Tanimura 					soisdisconnected(so);
29249077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
29259077f387SGleb Smirnoff 					    (tcp_fast_finwait2_recycle ?
29269077f387SGleb Smirnoff 					    tcp_finwait2_timeout :
29279077f387SGleb Smirnoff 					    TP_MAXIDLE(tp)));
29284cc20ab1SSeigo Tanimura 				}
292957f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_2);
2930df8bae1dSRodney W. Grimes 			}
2931df8bae1dSRodney W. Grimes 			break;
2932df8bae1dSRodney W. Grimes 
2933df8bae1dSRodney W. Grimes 		/*
2934df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
2935df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
2936df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
2937df8bae1dSRodney W. Grimes 		 * the segment.
2938df8bae1dSRodney W. Grimes 		 */
2939df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
2940df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2941ff9b006dSJulien Charbon 				INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
294211a20fb8SJeffrey Hsu 				tcp_twstart(tp);
2943ff9b006dSJulien Charbon 				INP_INFO_RUNLOCK(&V_tcbinfo);
2944340c35deSJonathan Lemon 				m_freem(m);
2945679d9708SAndre Oppermann 				return;
2946df8bae1dSRodney W. Grimes 			}
2947df8bae1dSRodney W. Grimes 			break;
2948df8bae1dSRodney W. Grimes 
2949df8bae1dSRodney W. Grimes 		/*
2950df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
2951df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
2952df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
2953df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
2954df8bae1dSRodney W. Grimes 		 */
2955df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
2956df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2957ff9b006dSJulien Charbon 				INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2958df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
2959df8bae1dSRodney W. Grimes 				goto drop;
2960df8bae1dSRodney W. Grimes 			}
2961df8bae1dSRodney W. Grimes 			break;
2962df8bae1dSRodney W. Grimes 		}
2963df8bae1dSRodney W. Grimes 	}
2964df8bae1dSRodney W. Grimes 
2965df8bae1dSRodney W. Grimes step6:
29668501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
29677cfc6904SRobert Watson 
2968df8bae1dSRodney W. Grimes 	/*
296960ee3bb2SAndre Oppermann 	 * Update window information.
297060ee3bb2SAndre Oppermann 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2971df8bae1dSRodney W. Grimes 	 */
297260ee3bb2SAndre Oppermann 	if ((thflags & TH_ACK) &&
297360ee3bb2SAndre Oppermann 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
297460ee3bb2SAndre Oppermann 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
297560ee3bb2SAndre Oppermann 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
297660ee3bb2SAndre Oppermann 		/* keep track of pure window updates */
297760ee3bb2SAndre Oppermann 		if (tlen == 0 &&
297860ee3bb2SAndre Oppermann 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
297978b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvwinupd);
2980df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
298160ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq;
298260ee3bb2SAndre Oppermann 		tp->snd_wl2 = th->th_ack;
2983df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
2984df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
298560ee3bb2SAndre Oppermann 		needoutput = 1;
2986df8bae1dSRodney W. Grimes 	}
2987df8bae1dSRodney W. Grimes 
2988df8bae1dSRodney W. Grimes 	/*
2989df8bae1dSRodney W. Grimes 	 * Process segments with URG.
2990df8bae1dSRodney W. Grimes 	 */
2991fb59c426SYoshinobu Inoue 	if ((thflags & TH_URG) && th->th_urp &&
2992df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2993df8bae1dSRodney W. Grimes 		/*
2994df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
2995df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
2996df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
2997df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
2998df8bae1dSRodney W. Grimes 		 */
299918ad5842SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
3000cfa6009eSGleb Smirnoff 		if (th->th_urp + sbavail(&so->so_rcv) > sb_max) {
3001fb59c426SYoshinobu Inoue 			th->th_urp = 0;			/* XXX */
3002fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;		/* XXX */
300318ad5842SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);	/* XXX */
3004df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
3005df8bae1dSRodney W. Grimes 		}
3006df8bae1dSRodney W. Grimes 		/*
3007df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
3008df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
3009df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
3010df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
3011df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
3012df8bae1dSRodney W. Grimes 		 *
3013df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
3014df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
3015df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
3016df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
3017df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
3018df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
3019df8bae1dSRodney W. Grimes 		 */
3020fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
3021fb59c426SYoshinobu Inoue 			tp->rcv_up = th->th_seq + th->th_urp;
3022cfa6009eSGleb Smirnoff 			so->so_oobmark = sbavail(&so->so_rcv) +
3023df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
3024927c5ceaSRobert Watson 			if (so->so_oobmark == 0)
3025c0b99ffaSRobert Watson 				so->so_rcv.sb_state |= SBS_RCVATMARK;
3026df8bae1dSRodney W. Grimes 			sohasoutofband(so);
3027df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
3028df8bae1dSRodney W. Grimes 		}
302918ad5842SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
3030df8bae1dSRodney W. Grimes 		/*
3031df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
3032df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
3033df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
3034df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
3035df8bae1dSRodney W. Grimes 		 */
30363ac12506SJonathan T. Looney 		if (th->th_urp <= (uint32_t)tlen &&
303730613f56SJeffrey Hsu 		    !(so->so_options & SO_OOBINLINE)) {
303830613f56SJeffrey Hsu 			/* hdr drop is delayed */
303930613f56SJeffrey Hsu 			tcp_pulloutofband(so, th, m, drop_hdrlen);
304030613f56SJeffrey Hsu 		}
3041c068736aSJeffrey Hsu 	} else {
3042df8bae1dSRodney W. Grimes 		/*
3043df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
3044df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
3045df8bae1dSRodney W. Grimes 		 * with the receive window.
3046df8bae1dSRodney W. Grimes 		 */
3047df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
3048df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
3049c068736aSJeffrey Hsu 	}
3050df8bae1dSRodney W. Grimes dodata:							/* XXX */
30518501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
30527cfc6904SRobert Watson 
3053df8bae1dSRodney W. Grimes 	/*
3054df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
3055df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
3056df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
3057df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
3058df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
3059df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
3060df8bae1dSRodney W. Grimes 	 */
306168bd7ed1SJonathan T. Looney #ifdef TCP_RFC7413
3062281a0fd4SPatrick Kelsey 	tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
306368bd7ed1SJonathan T. Looney 		   IS_FASTOPEN(tp->t_flags));
306468bd7ed1SJonathan T. Looney #else
306568bd7ed1SJonathan T. Looney #define	tfo_syn	(false)
306668bd7ed1SJonathan T. Looney #endif
3067281a0fd4SPatrick Kelsey 	if ((tlen || (thflags & TH_FIN) || tfo_syn) &&
3068df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
306969e03620SPaul Saab 		tcp_seq save_start = th->th_seq;
3070fb59c426SYoshinobu Inoue 		m_adj(m, drop_hdrlen);	/* delayed header drop */
3071e4b64281SJesper Skriver 		/*
3072c068736aSJeffrey Hsu 		 * Insert segment which includes th into TCP reassembly queue
3073c068736aSJeffrey Hsu 		 * with control block tp.  Set thflags to whether reassembly now
3074c068736aSJeffrey Hsu 		 * includes a segment with FIN.  This handles the common case
3075c068736aSJeffrey Hsu 		 * inline (segment is the next to be received on an established
3076c068736aSJeffrey Hsu 		 * connection, and the queue is empty), avoiding linkage into
3077c068736aSJeffrey Hsu 		 * and removal from the queue and repetition of various
3078c068736aSJeffrey Hsu 		 * conversions.
3079c068736aSJeffrey Hsu 		 * Set DELACK for segments received in order, but ack
3080c068736aSJeffrey Hsu 		 * immediately when segments are out of order (so
3081c068736aSJeffrey Hsu 		 * fast retransmit can work).
3082e4b64281SJesper Skriver 		 */
30834741bfcbSPatrick Kelsey 		if (th->th_seq == tp->rcv_nxt &&
30844741bfcbSPatrick Kelsey 		    LIST_EMPTY(&tp->t_segq) &&
3085281a0fd4SPatrick Kelsey 		    (TCPS_HAVEESTABLISHED(tp->t_state) ||
3086281a0fd4SPatrick Kelsey 		     tfo_syn)) {
3087281a0fd4SPatrick Kelsey 			if (DELAY_ACK(tp, tlen) || tfo_syn)
30883bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
3089e4b64281SJesper Skriver 			else
3090e4b64281SJesper Skriver 				tp->t_flags |= TF_ACKNOW;
3091e4b64281SJesper Skriver 			tp->rcv_nxt += tlen;
3092e4b64281SJesper Skriver 			thflags = th->th_flags & TH_FIN;
309378b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpack);
309478b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
30951e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
3096c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
3097c1c36a2cSMike Silbersack 				m_freem(m);
3098c1c36a2cSMike Silbersack 			else
3099651e4e6aSGleb Smirnoff 				sbappendstream_locked(&so->so_rcv, m, 0);
3100e8949f74SAndre Oppermann 			/* NB: sorwakeup_locked() does an implicit unlock. */
31011e4d7da7SRobert Watson 			sorwakeup_locked(so);
3102e4b64281SJesper Skriver 		} else {
3103e8949f74SAndre Oppermann 			/*
3104e8949f74SAndre Oppermann 			 * XXX: Due to the header drop above "th" is
3105e8949f74SAndre Oppermann 			 * theoretically invalid by now.  Fortunately
3106e8949f74SAndre Oppermann 			 * m_adj() doesn't actually frees any mbufs
3107e8949f74SAndre Oppermann 			 * when trimming from the head.
3108e8949f74SAndre Oppermann 			 */
3109e4b64281SJesper Skriver 			thflags = tcp_reass(tp, th, &tlen, m);
3110e4b64281SJesper Skriver 			tp->t_flags |= TF_ACKNOW;
3111e4b64281SJesper Skriver 		}
31123529149eSAndre Oppermann 		if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT))
3113f194524fSAndre Oppermann 			tcp_update_sack_list(tp, save_start, save_start + tlen);
3114302ce8d6SAndre Oppermann #if 0
3115df8bae1dSRodney W. Grimes 		/*
3116df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
3117df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
3118df8bae1dSRodney W. Grimes 		 * buffer size.
3119302ce8d6SAndre Oppermann 		 * XXX: Unused.
3120df8bae1dSRodney W. Grimes 		 */
3121f701e30dSJohn Baldwin 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt))
3122df8bae1dSRodney W. Grimes 			len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
3123f701e30dSJohn Baldwin 		else
3124f701e30dSJohn Baldwin 			len = so->so_rcv.sb_hiwat;
3125302ce8d6SAndre Oppermann #endif
3126df8bae1dSRodney W. Grimes 	} else {
3127df8bae1dSRodney W. Grimes 		m_freem(m);
3128fb59c426SYoshinobu Inoue 		thflags &= ~TH_FIN;
3129df8bae1dSRodney W. Grimes 	}
3130df8bae1dSRodney W. Grimes 
3131df8bae1dSRodney W. Grimes 	/*
3132df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
3133df8bae1dSRodney W. Grimes 	 * that the connection is closing.
3134df8bae1dSRodney W. Grimes 	 */
3135fb59c426SYoshinobu Inoue 	if (thflags & TH_FIN) {
3136df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
3137df8bae1dSRodney W. Grimes 			socantrcvmore(so);
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 		/*
3154df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
3155df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
3156df8bae1dSRodney W. Grimes 		 */
3157df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
31589b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
31599b8b58e0SJonathan Lemon 			/* FALLTHROUGH */
3160df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
316157f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSE_WAIT);
3162df8bae1dSRodney W. Grimes 			break;
3163df8bae1dSRodney W. Grimes 
3164df8bae1dSRodney W. Grimes 		/*
3165df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
3166df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
3167df8bae1dSRodney W. Grimes 		 */
3168df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
316957f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSING);
3170df8bae1dSRodney W. Grimes 			break;
3171df8bae1dSRodney W. Grimes 
3172df8bae1dSRodney W. Grimes 		/*
3173df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
3174df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
3175df8bae1dSRodney W. Grimes 		 * standard timers.
3176df8bae1dSRodney W. Grimes 		 */
3177df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
3178ff9b006dSJulien Charbon 			INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
3179ff9b006dSJulien Charbon 			KASSERT(ti_locked == TI_RLOCKED, ("%s: dodata "
3180252ca428SRobert Watson 			    "TCP_FIN_WAIT_2 ti_locked: %d", __func__,
3181252ca428SRobert Watson 			    ti_locked));
3182252ca428SRobert Watson 
3183340c35deSJonathan Lemon 			tcp_twstart(tp);
3184ff9b006dSJulien Charbon 			INP_INFO_RUNLOCK(&V_tcbinfo);
3185679d9708SAndre Oppermann 			return;
3186df8bae1dSRodney W. Grimes 		}
3187df8bae1dSRodney W. Grimes 	}
3188ff9b006dSJulien Charbon 	if (ti_locked == TI_RLOCKED)
3189ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
3190252ca428SRobert Watson 	ti_locked = TI_UNLOCKED;
3191252ca428SRobert Watson 
3192610ee2f9SDavid Greenman #ifdef TCPDEBUG
31934cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
3194fb59c426SYoshinobu Inoue 		tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
3195fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3196610ee2f9SDavid Greenman #endif
31972b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
3198df8bae1dSRodney W. Grimes 
3199df8bae1dSRodney W. Grimes 	/*
3200df8bae1dSRodney W. Grimes 	 * Return any desired output.
3201df8bae1dSRodney W. Grimes 	 */
3202df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
320355bceb1eSRandall Stewart 		(void) tp->t_fb->tfb_tcp_output(tp);
32047792ea27SJeffrey Hsu 
3205a14c749fSJonathan Lemon check_delack:
3206252ca428SRobert Watson 	KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d",
3207252ca428SRobert Watson 	    __func__, ti_locked));
3208603724d3SBjoern A. Zeeb 	INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
32098501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
3210252ca428SRobert Watson 
3211a14c749fSJonathan Lemon 	if (tp->t_flags & TF_DELACK) {
32123bfd6421SJonathan Lemon 		tp->t_flags &= ~TF_DELACK;
3213b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
32143bfd6421SJonathan Lemon 	}
32158501a69cSRobert Watson 	INP_WUNLOCK(tp->t_inpcb);
3216679d9708SAndre Oppermann 	return;
3217df8bae1dSRodney W. Grimes 
3218df8bae1dSRodney W. Grimes dropafterack:
3219df8bae1dSRodney W. Grimes 	/*
3220df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
3221df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
322280ab7c0eSGarrett Wollman 	 *
322380ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
322480ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
322580ab7c0eSGarrett Wollman 	 * RST have been dropped.
322680ab7c0eSGarrett Wollman 	 *
322780ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
322880ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
322980ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
323080ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
323180ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
323280ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
3233df8bae1dSRodney W. Grimes 	 */
3234fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
3235fb59c426SYoshinobu Inoue 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
3236a57815efSBosko Milekic 	     SEQ_GT(th->th_ack, tp->snd_max)) ) {
3237a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
3238a57815efSBosko Milekic 		goto dropwithreset;
3239a57815efSBosko Milekic 	}
3240a0292f23SGarrett Wollman #ifdef TCPDEBUG
32414cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
3242fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
3243fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3244a0292f23SGarrett Wollman #endif
32452b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
3246ff9b006dSJulien Charbon 	if (ti_locked == TI_RLOCKED)
3247ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
3248252ca428SRobert Watson 	ti_locked = TI_UNLOCKED;
3249252ca428SRobert Watson 
3250df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
325155bceb1eSRandall Stewart 	(void) tp->t_fb->tfb_tcp_output(tp);
32528501a69cSRobert Watson 	INP_WUNLOCK(tp->t_inpcb);
3253d6915262SRobert Watson 	m_freem(m);
3254679d9708SAndre Oppermann 	return;
3255df8bae1dSRodney W. Grimes 
3256df8bae1dSRodney W. Grimes dropwithreset:
3257ff9b006dSJulien Charbon 	if (ti_locked == TI_RLOCKED)
3258ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
3259252ca428SRobert Watson 	ti_locked = TI_UNLOCKED;
3260a57815efSBosko Milekic 
3261a0ca0871SRobert Watson 	if (tp != NULL) {
3262302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
32638501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
3264dd8ac7f9SRobert Watson 	} else
3265a0ca0871SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
3266679d9708SAndre Oppermann 	return;
3267df8bae1dSRodney W. Grimes 
3268df8bae1dSRodney W. Grimes drop:
3269ff9b006dSJulien Charbon 	if (ti_locked == TI_RLOCKED) {
3270ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
3271fa046d87SRobert Watson 		ti_locked = TI_UNLOCKED;
3272fa046d87SRobert Watson 	}
3273252ca428SRobert Watson #ifdef INVARIANTS
3274252ca428SRobert Watson 	else
3275252ca428SRobert Watson 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
3276252ca428SRobert Watson #endif
3277252ca428SRobert Watson 
3278df8bae1dSRodney W. Grimes 	/*
3279df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
3280df8bae1dSRodney W. Grimes 	 */
3281610ee2f9SDavid Greenman #ifdef TCPDEBUG
32821c53f806SRobert Watson 	if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
3283fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
3284fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3285a0292f23SGarrett Wollman #endif
32862b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
32871c53f806SRobert Watson 	if (tp != NULL)
32888501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
3289d6915262SRobert Watson 	m_freem(m);
329068bd7ed1SJonathan T. Looney #ifndef TCP_RFC7413
329168bd7ed1SJonathan T. Looney #undef	tfo_syn
329268bd7ed1SJonathan T. Looney #endif
3293302ce8d6SAndre Oppermann }
3294302ce8d6SAndre Oppermann 
3295302ce8d6SAndre Oppermann /*
32960ca3f933SAndre Oppermann  * Issue RST and make ACK acceptable to originator of segment.
32970ca3f933SAndre Oppermann  * The mbuf must still include the original packet header.
32980ca3f933SAndre Oppermann  * tp may be NULL.
3299302ce8d6SAndre Oppermann  */
330055bceb1eSRandall Stewart void
3301302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp,
3302302ce8d6SAndre Oppermann     int tlen, int rstreason)
3303302ce8d6SAndre Oppermann {
3304b287c6c7SBjoern A. Zeeb #ifdef INET
3305302ce8d6SAndre Oppermann 	struct ip *ip;
3306b287c6c7SBjoern A. Zeeb #endif
3307302ce8d6SAndre Oppermann #ifdef INET6
3308302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6;
3309302ce8d6SAndre Oppermann #endif
33107a3244ccSRobert Watson 
33117a3244ccSRobert Watson 	if (tp != NULL) {
33128501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
33137a3244ccSRobert Watson 	}
33147a3244ccSRobert Watson 
33150ca3f933SAndre Oppermann 	/* Don't bother if destination was broadcast/multicast. */
3316302ce8d6SAndre Oppermann 	if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
3317302ce8d6SAndre Oppermann 		goto drop;
3318302ce8d6SAndre Oppermann #ifdef INET6
3319302ce8d6SAndre Oppermann 	if (mtod(m, struct ip *)->ip_v == 6) {
3320302ce8d6SAndre Oppermann 		ip6 = mtod(m, struct ip6_hdr *);
3321302ce8d6SAndre Oppermann 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
3322302ce8d6SAndre Oppermann 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
3323302ce8d6SAndre Oppermann 			goto drop;
3324302ce8d6SAndre Oppermann 		/* IPv6 anycast check is done at tcp6_input() */
3325b287c6c7SBjoern A. Zeeb 	}
3326302ce8d6SAndre Oppermann #endif
3327b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3328b287c6c7SBjoern A. Zeeb 	else
3329b287c6c7SBjoern A. Zeeb #endif
3330b287c6c7SBjoern A. Zeeb #ifdef INET
3331302ce8d6SAndre Oppermann 	{
3332302ce8d6SAndre Oppermann 		ip = mtod(m, struct ip *);
3333302ce8d6SAndre Oppermann 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
3334302ce8d6SAndre Oppermann 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
3335302ce8d6SAndre Oppermann 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
3336302ce8d6SAndre Oppermann 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
3337302ce8d6SAndre Oppermann 			goto drop;
3338302ce8d6SAndre Oppermann 	}
3339b287c6c7SBjoern A. Zeeb #endif
3340302ce8d6SAndre Oppermann 
3341302ce8d6SAndre Oppermann 	/* Perform bandwidth limiting. */
3342302ce8d6SAndre Oppermann 	if (badport_bandlim(rstreason) < 0)
3343302ce8d6SAndre Oppermann 		goto drop;
3344302ce8d6SAndre Oppermann 
3345302ce8d6SAndre Oppermann 	/* tcp_respond consumes the mbuf chain. */
3346302ce8d6SAndre Oppermann 	if (th->th_flags & TH_ACK) {
3347302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0,
3348302ce8d6SAndre Oppermann 		    th->th_ack, TH_RST);
3349302ce8d6SAndre Oppermann 	} else {
3350302ce8d6SAndre Oppermann 		if (th->th_flags & TH_SYN)
3351302ce8d6SAndre Oppermann 			tlen++;
33524ddd5aadSMichael Tuexen 		if (th->th_flags & TH_FIN)
33534ddd5aadSMichael Tuexen 			tlen++;
3354302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
3355302ce8d6SAndre Oppermann 		    (tcp_seq)0, TH_RST|TH_ACK);
3356302ce8d6SAndre Oppermann 	}
3357302ce8d6SAndre Oppermann 	return;
3358302ce8d6SAndre Oppermann drop:
3359302ce8d6SAndre Oppermann 	m_freem(m);
3360df8bae1dSRodney W. Grimes }
3361df8bae1dSRodney W. Grimes 
3362be2ac88cSJonathan Lemon /*
3363be2ac88cSJonathan Lemon  * Parse TCP options and place in tcpopt.
3364be2ac88cSJonathan Lemon  */
336555bceb1eSRandall Stewart void
3366ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags)
3367df8bae1dSRodney W. Grimes {
3368df8bae1dSRodney W. Grimes 	int opt, optlen;
3369df8bae1dSRodney W. Grimes 
3370be2ac88cSJonathan Lemon 	to->to_flags = 0;
3371df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
3372df8bae1dSRodney W. Grimes 		opt = cp[0];
3373df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
3374df8bae1dSRodney W. Grimes 			break;
3375df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
3376df8bae1dSRodney W. Grimes 			optlen = 1;
3377df8bae1dSRodney W. Grimes 		else {
3378b474779fSJun-ichiro itojun Hagino 			if (cnt < 2)
3379b474779fSJun-ichiro itojun Hagino 				break;
3380df8bae1dSRodney W. Grimes 			optlen = cp[1];
3381b474779fSJun-ichiro itojun Hagino 			if (optlen < 2 || optlen > cnt)
3382df8bae1dSRodney W. Grimes 				break;
3383df8bae1dSRodney W. Grimes 		}
3384df8bae1dSRodney W. Grimes 		switch (opt) {
3385df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
3386df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
3387df8bae1dSRodney W. Grimes 				continue;
3388f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3389df8bae1dSRodney W. Grimes 				continue;
3390be2ac88cSJonathan Lemon 			to->to_flags |= TOF_MSS;
3391be2ac88cSJonathan Lemon 			bcopy((char *)cp + 2,
3392be2ac88cSJonathan Lemon 			    (char *)&to->to_mss, sizeof(to->to_mss));
3393fd8e4ebcSMike Barcroft 			to->to_mss = ntohs(to->to_mss);
3394df8bae1dSRodney W. Grimes 			break;
3395df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
3396df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
3397df8bae1dSRodney W. Grimes 				continue;
3398f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3399df8bae1dSRodney W. Grimes 				continue;
3400be2ac88cSJonathan Lemon 			to->to_flags |= TOF_SCALE;
340102a1a643SAndre Oppermann 			to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT);
3402df8bae1dSRodney W. Grimes 			break;
3403df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
3404df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
3405df8bae1dSRodney W. Grimes 				continue;
3406be2ac88cSJonathan Lemon 			to->to_flags |= TOF_TS;
3407a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
3408a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
3409fd8e4ebcSMike Barcroft 			to->to_tsval = ntohl(to->to_tsval);
3410a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
3411a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
3412fd8e4ebcSMike Barcroft 			to->to_tsecr = ntohl(to->to_tsecr);
3413df8bae1dSRodney W. Grimes 			break;
34141cfd4b53SBruce M Simpson 		case TCPOPT_SIGNATURE:
3415fcf59617SAndrey V. Elsukov 			/*
3416fcf59617SAndrey V. Elsukov 			 * In order to reply to a host which has set the
3417fcf59617SAndrey V. Elsukov 			 * TCP_SIGNATURE option in its initial SYN, we have
3418fcf59617SAndrey V. Elsukov 			 * to record the fact that the option was observed
3419fcf59617SAndrey V. Elsukov 			 * here for the syncache code to perform the correct
3420fcf59617SAndrey V. Elsukov 			 * response.
3421fcf59617SAndrey V. Elsukov 			 */
34221cfd4b53SBruce M Simpson 			if (optlen != TCPOLEN_SIGNATURE)
34231cfd4b53SBruce M Simpson 				continue;
3424df47e437SAndre Oppermann 			to->to_flags |= TOF_SIGNATURE;
3425df47e437SAndre Oppermann 			to->to_signature = cp + 2;
34261cfd4b53SBruce M Simpson 			break;
34276d90faf3SPaul Saab 		case TCPOPT_SACK_PERMITTED:
3428f72167f4SAndre Oppermann 			if (optlen != TCPOLEN_SACK_PERMITTED)
34296d90faf3SPaul Saab 				continue;
3430f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3431f72167f4SAndre Oppermann 				continue;
3432603724d3SBjoern A. Zeeb 			if (!V_tcp_do_sack)
3433f72167f4SAndre Oppermann 				continue;
3434fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACKPERM;
34356d90faf3SPaul Saab 			break;
34366d90faf3SPaul Saab 		case TCPOPT_SACK:
34375a53ca16SPaul Saab 			if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
34386d90faf3SPaul Saab 				continue;
3439b728e902SAndre Oppermann 			if (flags & TO_SYN)
3440b728e902SAndre Oppermann 				continue;
3441fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACK;
34425a53ca16SPaul Saab 			to->to_nsacks = (optlen - 2) / TCPOLEN_SACK;
34435a53ca16SPaul Saab 			to->to_sacks = cp + 2;
344478b50714SRobert Watson 			TCPSTAT_INC(tcps_sack_rcv_blocks);
34456d90faf3SPaul Saab 			break;
3446281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
3447281a0fd4SPatrick Kelsey 		case TCPOPT_FAST_OPEN:
3448281a0fd4SPatrick Kelsey 			if ((optlen != TCPOLEN_FAST_OPEN_EMPTY) &&
3449281a0fd4SPatrick Kelsey 			    (optlen < TCPOLEN_FAST_OPEN_MIN) &&
3450281a0fd4SPatrick Kelsey 			    (optlen > TCPOLEN_FAST_OPEN_MAX))
3451281a0fd4SPatrick Kelsey 				continue;
3452281a0fd4SPatrick Kelsey 			if (!(flags & TO_SYN))
3453281a0fd4SPatrick Kelsey 				continue;
3454281a0fd4SPatrick Kelsey 			if (!V_tcp_fastopen_enabled)
3455281a0fd4SPatrick Kelsey 				continue;
3456281a0fd4SPatrick Kelsey 			to->to_flags |= TOF_FASTOPEN;
3457281a0fd4SPatrick Kelsey 			to->to_tfo_len = optlen - 2;
3458281a0fd4SPatrick Kelsey 			to->to_tfo_cookie = to->to_tfo_len ? cp + 2 : NULL;
3459281a0fd4SPatrick Kelsey 			break;
3460281a0fd4SPatrick Kelsey #endif
3461be2ac88cSJonathan Lemon 		default:
3462be2ac88cSJonathan Lemon 			continue;
3463df8bae1dSRodney W. Grimes 		}
3464df8bae1dSRodney W. Grimes 	}
3465df8bae1dSRodney W. Grimes }
3466df8bae1dSRodney W. Grimes 
3467df8bae1dSRodney W. Grimes /*
3468df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
3469df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
3470df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
3471df8bae1dSRodney W. Grimes  * sequencing purposes.
3472df8bae1dSRodney W. Grimes  */
347355bceb1eSRandall Stewart void
3474ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m,
3475ad3f9ab3SAndre Oppermann     int off)
3476df8bae1dSRodney W. Grimes {
3477fb59c426SYoshinobu Inoue 	int cnt = off + th->th_urp - 1;
3478df8bae1dSRodney W. Grimes 
3479df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
3480df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
3481df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
3482df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
3483df8bae1dSRodney W. Grimes 
34848501a69cSRobert Watson 			INP_WLOCK_ASSERT(tp->t_inpcb);
34857a3244ccSRobert Watson 
3486df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
3487df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
3488df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
3489df8bae1dSRodney W. Grimes 			m->m_len--;
349069a34685SYoshinobu Inoue 			if (m->m_flags & M_PKTHDR)
349169a34685SYoshinobu Inoue 				m->m_pkthdr.len--;
3492df8bae1dSRodney W. Grimes 			return;
3493df8bae1dSRodney W. Grimes 		}
3494df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
3495df8bae1dSRodney W. Grimes 		m = m->m_next;
34964dfdffe9SAndre Oppermann 		if (m == NULL)
3497df8bae1dSRodney W. Grimes 			break;
3498df8bae1dSRodney W. Grimes 	}
3499df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
3500df8bae1dSRodney W. Grimes }
3501df8bae1dSRodney W. Grimes 
3502df8bae1dSRodney W. Grimes /*
3503df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
3504df8bae1dSRodney W. Grimes  * and update averages and current timeout.
3505df8bae1dSRodney W. Grimes  */
350655bceb1eSRandall Stewart void
3507ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt)
3508df8bae1dSRodney W. Grimes {
3509ad3f9ab3SAndre Oppermann 	int delta;
3510233e8c18SGarrett Wollman 
35118501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
35122be3bf22SRobert Watson 
351378b50714SRobert Watson 	TCPSTAT_INC(tcps_rttupdated);
3514233e8c18SGarrett Wollman 	tp->t_rttupdated++;
35155ede40dcSRyan Stone 	if ((tp->t_srtt != 0) && (tp->t_rxtshift <= TCP_RTT_INVALIDATE)) {
3516233e8c18SGarrett Wollman 		/*
3517233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
3518233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
3519233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
3520233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
3521233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
3522233e8c18SGarrett Wollman 		 */
3523233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
3524233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
3525233e8c18SGarrett Wollman 
3526233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
3527233e8c18SGarrett Wollman 			tp->t_srtt = 1;
3528233e8c18SGarrett Wollman 
3529233e8c18SGarrett Wollman 		/*
3530233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
3531233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
3532233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
3533233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
3534233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
3535233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
3536233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
3537233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
3538233e8c18SGarrett Wollman 		 */
3539233e8c18SGarrett Wollman 		if (delta < 0)
3540233e8c18SGarrett Wollman 			delta = -delta;
3541233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
3542233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
3543233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
35441fcc99b5SMatthew Dillon 		if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
35451fcc99b5SMatthew Dillon 		    tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3546233e8c18SGarrett Wollman 	} else {
3547233e8c18SGarrett Wollman 		/*
3548233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
3549233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
3550233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
3551233e8c18SGarrett Wollman 		 */
3552233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
3553233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
35541fcc99b5SMatthew Dillon 		tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3555233e8c18SGarrett Wollman 	}
35569b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
3557df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
3558df8bae1dSRodney W. Grimes 
3559df8bae1dSRodney W. Grimes 	/*
3560df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
3561df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
3562df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
3563df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
3564df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
3565df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
3566df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
3567df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
3568df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
3569df8bae1dSRodney W. Grimes 	 */
3570233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
35719e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
3572df8bae1dSRodney W. Grimes 
3573df8bae1dSRodney W. Grimes 	/*
3574df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
3575df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
3576df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
3577df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
3578df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
3579df8bae1dSRodney W. Grimes 	 */
3580df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
3581df8bae1dSRodney W. Grimes }
3582df8bae1dSRodney W. Grimes 
3583df8bae1dSRodney W. Grimes /*
3584df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
3585df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
35864faaea55SAndre Oppermann  * If none, use an mss that can be handled on the outgoing interface
35874faaea55SAndre Oppermann  * without forcing IP to fragment.  If no route is found, route has no mtu,
3588df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
3589df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
3590df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
3591df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
3592df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
3593df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
3594df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
3595a0292f23SGarrett Wollman  *
35960c39d38dSGleb Smirnoff  * NOTE that resulting t_maxseg doesn't include space for TCP options or
35970c39d38dSGleb Smirnoff  * IP options, e.g. IPSEC data, since length of this data may vary, and
35980c39d38dSGleb Smirnoff  * thus it is calculated for every segment separately in tcp_output().
3599a0292f23SGarrett Wollman  *
360097d8d152SAndre Oppermann  * NOTE that this routine is only called when we process an incoming
3601ef341ee1SGleb Smirnoff  * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS
3602ef341ee1SGleb Smirnoff  * settings are handled in tcp_mssopt().
3603df8bae1dSRodney W. Grimes  */
3604a0292f23SGarrett Wollman void
3605ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer,
36063c914c54SAndre Oppermann     struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap)
3607df8bae1dSRodney W. Grimes {
3608b287c6c7SBjoern A. Zeeb 	int mss = 0;
36093ac12506SJonathan T. Looney 	uint32_t maxmtu = 0;
3610c068736aSJeffrey Hsu 	struct inpcb *inp = tp->t_inpcb;
361197d8d152SAndre Oppermann 	struct hc_metrics_lite metrics;
3612fb59c426SYoshinobu Inoue #ifdef INET6
3613c068736aSJeffrey Hsu 	int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
3614c068736aSJeffrey Hsu 	size_t min_protoh = isipv6 ?
3615c068736aSJeffrey Hsu 			    sizeof (struct ip6_hdr) + sizeof (struct tcphdr) :
3616c068736aSJeffrey Hsu 			    sizeof (struct tcpiphdr);
3617c068736aSJeffrey Hsu #else
3618c068736aSJeffrey Hsu 	const size_t min_protoh = sizeof(struct tcpiphdr);
3619fb59c426SYoshinobu Inoue #endif
3620df8bae1dSRodney W. Grimes 
36218501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
36227a3244ccSRobert Watson 
3623ef341ee1SGleb Smirnoff 	if (mtuoffer != -1) {
3624ef341ee1SGleb Smirnoff 		KASSERT(offer == -1, ("%s: conflict", __func__));
3625ef341ee1SGleb Smirnoff 		offer = mtuoffer - min_protoh;
3626ef341ee1SGleb Smirnoff 	}
3627ef341ee1SGleb Smirnoff 
3628e8949f74SAndre Oppermann 	/* Initialize. */
362997d8d152SAndre Oppermann #ifdef INET6
363097d8d152SAndre Oppermann 	if (isipv6) {
36313c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu6(&inp->inp_inc, cap);
36320c39d38dSGleb Smirnoff 		tp->t_maxseg = V_tcp_v6mssdflt;
3633b287c6c7SBjoern A. Zeeb 	}
363497d8d152SAndre Oppermann #endif
3635b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3636b287c6c7SBjoern A. Zeeb 	else
3637b287c6c7SBjoern A. Zeeb #endif
3638b287c6c7SBjoern A. Zeeb #ifdef INET
363997d8d152SAndre Oppermann 	{
36403c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu(&inp->inp_inc, cap);
36410c39d38dSGleb Smirnoff 		tp->t_maxseg = V_tcp_mssdflt;
3642df8bae1dSRodney W. Grimes 	}
3643b287c6c7SBjoern A. Zeeb #endif
3644df8bae1dSRodney W. Grimes 
364597d8d152SAndre Oppermann 	/*
3646e8949f74SAndre Oppermann 	 * No route to sender, stay with default mss and return.
364797d8d152SAndre Oppermann 	 */
36486f01cac6SBjoern A. Zeeb 	if (maxmtu == 0) {
36496f01cac6SBjoern A. Zeeb 		/*
36508e5c87f4SBjoern A. Zeeb 		 * In case we return early we need to initialize metrics
36516f01cac6SBjoern A. Zeeb 		 * to a defined state as tcp_hc_get() would do for us
36526f01cac6SBjoern A. Zeeb 		 * if there was no cache hit.
36536f01cac6SBjoern A. Zeeb 		 */
36546f01cac6SBjoern A. Zeeb 		if (metricptr != NULL)
36556f01cac6SBjoern A. Zeeb 			bzero(metricptr, sizeof(struct hc_metrics_lite));
365697d8d152SAndre Oppermann 		return;
36576f01cac6SBjoern A. Zeeb 	}
365897d8d152SAndre Oppermann 
3659e8949f74SAndre Oppermann 	/* What have we got? */
366097d8d152SAndre Oppermann 	switch (offer) {
366197d8d152SAndre Oppermann 		case 0:
366297d8d152SAndre Oppermann 			/*
366397d8d152SAndre Oppermann 			 * Offer == 0 means that there was no MSS on the SYN
3664c3b02504SBjoern A. Zeeb 			 * segment, in this case we use tcp_mssdflt as
36650c39d38dSGleb Smirnoff 			 * already assigned to t_maxseg above.
366697d8d152SAndre Oppermann 			 */
36670c39d38dSGleb Smirnoff 			offer = tp->t_maxseg;
366897d8d152SAndre Oppermann 			break;
366997d8d152SAndre Oppermann 
367097d8d152SAndre Oppermann 		case -1:
3671a0292f23SGarrett Wollman 			/*
3672c94c54e4SAndre Oppermann 			 * Offer == -1 means that we didn't receive SYN yet.
3673a0292f23SGarrett Wollman 			 */
367497d8d152SAndre Oppermann 			/* FALLTHROUGH */
367597d8d152SAndre Oppermann 
367697d8d152SAndre Oppermann 		default:
3677a0292f23SGarrett Wollman 			/*
367853369ac9SAndre Oppermann 			 * Prevent DoS attack with too small MSS. Round up
367953369ac9SAndre Oppermann 			 * to at least minmss.
368053369ac9SAndre Oppermann 			 */
3681603724d3SBjoern A. Zeeb 			offer = max(offer, V_tcp_minmss);
368297d8d152SAndre Oppermann 	}
3683a0292f23SGarrett Wollman 
3684df8bae1dSRodney W. Grimes 	/*
3685e8949f74SAndre Oppermann 	 * rmx information is now retrieved from tcp_hostcache.
3686df8bae1dSRodney W. Grimes 	 */
368797d8d152SAndre Oppermann 	tcp_hc_get(&inp->inp_inc, &metrics);
36883cee92e0SBjoern A. Zeeb 	if (metricptr != NULL)
36893cee92e0SBjoern A. Zeeb 		bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite));
369097d8d152SAndre Oppermann 
3691df8bae1dSRodney W. Grimes 	/*
3692cc412412SHiren Panchasara 	 * If there's a discovered mtu in tcp hostcache, use it.
3693cc412412SHiren Panchasara 	 * Else, use the link mtu.
3694df8bae1dSRodney W. Grimes 	 */
369597d8d152SAndre Oppermann 	if (metrics.rmx_mtu)
36962d166c02SAndre Oppermann 		mss = min(metrics.rmx_mtu, maxmtu) - min_protoh;
3697c068736aSJeffrey Hsu 	else {
369831b3783cSHajimu UMEMOTO #ifdef INET6
3699fb59c426SYoshinobu Inoue 		if (isipv6) {
370097d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3701603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
370297d8d152SAndre Oppermann 			    !in6_localaddr(&inp->in6p_faddr))
3703603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_v6mssdflt);
3704b287c6c7SBjoern A. Zeeb 		}
3705b3399803SHajimu UMEMOTO #endif
3706b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3707b287c6c7SBjoern A. Zeeb 		else
3708b287c6c7SBjoern A. Zeeb #endif
3709b287c6c7SBjoern A. Zeeb #ifdef INET
371097d8d152SAndre Oppermann 		{
371197d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3712603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
371397d8d152SAndre Oppermann 			    !in_localaddr(inp->inp_faddr))
3714603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_mssdflt);
3715a0292f23SGarrett Wollman 		}
3716b287c6c7SBjoern A. Zeeb #endif
37173cee92e0SBjoern A. Zeeb 		/*
37183cee92e0SBjoern A. Zeeb 		 * XXX - The above conditional (mss = maxmtu - min_protoh)
37193cee92e0SBjoern A. Zeeb 		 * probably violates the TCP spec.
37203cee92e0SBjoern A. Zeeb 		 * The problem is that, since we don't know the
37213cee92e0SBjoern A. Zeeb 		 * other end's MSS, we are supposed to use a conservative
37223cee92e0SBjoern A. Zeeb 		 * default.  But, if we do that, then MTU discovery will
37233cee92e0SBjoern A. Zeeb 		 * never actually take place, because the conservative
37243cee92e0SBjoern A. Zeeb 		 * default is much less than the MTUs typically seen
37253cee92e0SBjoern A. Zeeb 		 * on the Internet today.  For the moment, we'll sweep
37263cee92e0SBjoern A. Zeeb 		 * this under the carpet.
37273cee92e0SBjoern A. Zeeb 		 *
37283cee92e0SBjoern A. Zeeb 		 * The conservative default might not actually be a problem
37293cee92e0SBjoern A. Zeeb 		 * if the only case this occurs is when sending an initial
37303cee92e0SBjoern A. Zeeb 		 * SYN with options and data to a host we've never talked
37313cee92e0SBjoern A. Zeeb 		 * to before.  Then, they will reply with an MSS value which
37323cee92e0SBjoern A. Zeeb 		 * will get recorded and the new parameters should get
37333cee92e0SBjoern A. Zeeb 		 * recomputed.  For Further Study.
37343cee92e0SBjoern A. Zeeb 		 */
373597d8d152SAndre Oppermann 	}
3736a0292f23SGarrett Wollman 	mss = min(mss, offer);
373797d8d152SAndre Oppermann 
3738a0292f23SGarrett Wollman 	/*
37390c39d38dSGleb Smirnoff 	 * Sanity check: make sure that maxseg will be large
37403cee92e0SBjoern A. Zeeb 	 * enough to allow some data on segments even if the
37413cee92e0SBjoern A. Zeeb 	 * all the option space is used (40bytes).  Otherwise
37423cee92e0SBjoern A. Zeeb 	 * funny things may happen in tcp_output.
37430c39d38dSGleb Smirnoff 	 *
37440c39d38dSGleb Smirnoff 	 * XXXGL: shouldn't we reserve space for IP/IPv6 options?
37453cee92e0SBjoern A. Zeeb 	 */
37463cee92e0SBjoern A. Zeeb 	mss = max(mss, 64);
37473cee92e0SBjoern A. Zeeb 
374897d8d152SAndre Oppermann 	tp->t_maxseg = mss;
37493cee92e0SBjoern A. Zeeb }
37503cee92e0SBjoern A. Zeeb 
37513cee92e0SBjoern A. Zeeb void
37523cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer)
37533cee92e0SBjoern A. Zeeb {
3754dbc42409SLawrence Stewart 	int mss;
37553ac12506SJonathan T. Looney 	uint32_t bufsize;
37563cee92e0SBjoern A. Zeeb 	struct inpcb *inp;
37573cee92e0SBjoern A. Zeeb 	struct socket *so;
37583cee92e0SBjoern A. Zeeb 	struct hc_metrics_lite metrics;
37593c914c54SAndre Oppermann 	struct tcp_ifcap cap;
3760dbc42409SLawrence Stewart 
37613cee92e0SBjoern A. Zeeb 	KASSERT(tp != NULL, ("%s: tp == NULL", __func__));
37623cee92e0SBjoern A. Zeeb 
37633c914c54SAndre Oppermann 	bzero(&cap, sizeof(cap));
37643c914c54SAndre Oppermann 	tcp_mss_update(tp, offer, -1, &metrics, &cap);
37653cee92e0SBjoern A. Zeeb 
37663cee92e0SBjoern A. Zeeb 	mss = tp->t_maxseg;
37673cee92e0SBjoern A. Zeeb 	inp = tp->t_inpcb;
376897d8d152SAndre Oppermann 
3769df8bae1dSRodney W. Grimes 	/*
377097d8d152SAndre Oppermann 	 * If there's a pipesize, change the socket buffer to that size,
377197d8d152SAndre Oppermann 	 * don't change if sb_hiwat is different than default (then it
377297d8d152SAndre Oppermann 	 * has been changed on purpose with setsockopt).
377397d8d152SAndre Oppermann 	 * Make the socket buffers an integral number of mss units;
377497d8d152SAndre Oppermann 	 * if the mss is larger than the socket buffer, decrease the mss.
3775df8bae1dSRodney W. Grimes 	 */
3776c3b02504SBjoern A. Zeeb 	so = inp->inp_socket;
37773f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
3778e233e2acSAndre Oppermann 	if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe)
377997d8d152SAndre Oppermann 		bufsize = metrics.rmx_sendpipe;
378097d8d152SAndre Oppermann 	else
3781df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
3782df8bae1dSRodney W. Grimes 	if (bufsize < mss)
3783df8bae1dSRodney W. Grimes 		mss = bufsize;
3784df8bae1dSRodney W. Grimes 	else {
3785df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3786df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3787df8bae1dSRodney W. Grimes 			bufsize = sb_max;
378888c39af3SRuslan Ermilov 		if (bufsize > so->so_snd.sb_hiwat)
37893f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_snd, bufsize, so, NULL);
3790df8bae1dSRodney W. Grimes 	}
37913f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
3792784ce8faSHiren Panchasara 	/*
3793784ce8faSHiren Panchasara 	 * Sanity check: make sure that maxseg will be large
3794784ce8faSHiren Panchasara 	 * enough to allow some data on segments even if the
3795784ce8faSHiren Panchasara 	 * all the option space is used (40bytes).  Otherwise
3796784ce8faSHiren Panchasara 	 * funny things may happen in tcp_output.
3797784ce8faSHiren Panchasara 	 *
3798784ce8faSHiren Panchasara 	 * XXXGL: shouldn't we reserve space for IP/IPv6 options?
3799784ce8faSHiren Panchasara 	 */
3800784ce8faSHiren Panchasara 	tp->t_maxseg = max(mss, 64);
3801df8bae1dSRodney W. Grimes 
38023f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
3803e233e2acSAndre Oppermann 	if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe)
380497d8d152SAndre Oppermann 		bufsize = metrics.rmx_recvpipe;
380597d8d152SAndre Oppermann 	else
3806df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
3807df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
3808df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3809df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3810df8bae1dSRodney W. Grimes 			bufsize = sb_max;
381188c39af3SRuslan Ermilov 		if (bufsize > so->so_rcv.sb_hiwat)
38123f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL);
3813df8bae1dSRodney W. Grimes 	}
38143f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
381591d6cfa6SBjoern A. Zeeb 
381691d6cfa6SBjoern A. Zeeb 	/* Check the interface for TSO capabilities. */
38173c914c54SAndre Oppermann 	if (cap.ifcap & CSUM_TSO) {
381891d6cfa6SBjoern A. Zeeb 		tp->t_flags |= TF_TSO;
38193c914c54SAndre Oppermann 		tp->t_tsomax = cap.tsomax;
38209fd573c3SHans Petter Selasky 		tp->t_tsomaxsegcount = cap.tsomaxsegcount;
38219fd573c3SHans Petter Selasky 		tp->t_tsomaxsegsize = cap.tsomaxsegsize;
38223c914c54SAndre Oppermann 	}
3823a0292f23SGarrett Wollman }
3824a0292f23SGarrett Wollman 
3825a0292f23SGarrett Wollman /*
3826a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
3827a0292f23SGarrett Wollman  */
3828a0292f23SGarrett Wollman int
3829ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc)
3830a0292f23SGarrett Wollman {
383197d8d152SAndre Oppermann 	int mss = 0;
38323ac12506SJonathan T. Looney 	uint32_t thcmtu = 0;
38333ac12506SJonathan T. Looney 	uint32_t maxmtu = 0;
383497d8d152SAndre Oppermann 	size_t min_protoh;
3835a0292f23SGarrett Wollman 
383697d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer"));
3837a0292f23SGarrett Wollman 
383831b3783cSHajimu UMEMOTO #ifdef INET6
3839dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
3840603724d3SBjoern A. Zeeb 		mss = V_tcp_v6mssdflt;
3841233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu6(inc, NULL);
384297d8d152SAndre Oppermann 		min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
3843b287c6c7SBjoern A. Zeeb 	}
384431b3783cSHajimu UMEMOTO #endif
3845b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3846b287c6c7SBjoern A. Zeeb 	else
3847b287c6c7SBjoern A. Zeeb #endif
3848b287c6c7SBjoern A. Zeeb #ifdef INET
384997d8d152SAndre Oppermann 	{
3850603724d3SBjoern A. Zeeb 		mss = V_tcp_mssdflt;
3851233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu(inc, NULL);
385297d8d152SAndre Oppermann 		min_protoh = sizeof(struct tcpiphdr);
385397d8d152SAndre Oppermann 	}
3854b287c6c7SBjoern A. Zeeb #endif
38553a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET)
38563a9391deSBjoern A. Zeeb 	thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
38573a9391deSBjoern A. Zeeb #endif
38583a9391deSBjoern A. Zeeb 
385997d8d152SAndre Oppermann 	if (maxmtu && thcmtu)
386097d8d152SAndre Oppermann 		mss = min(maxmtu, thcmtu) - min_protoh;
386197d8d152SAndre Oppermann 	else if (maxmtu || thcmtu)
386297d8d152SAndre Oppermann 		mss = max(maxmtu, thcmtu) - min_protoh;
386397d8d152SAndre Oppermann 
386497d8d152SAndre Oppermann 	return (mss);
3865df8bae1dSRodney W. Grimes }
386646f58482SJonathan Lemon 
386746f58482SJonathan Lemon 
386846f58482SJonathan Lemon /*
3869c068736aSJeffrey Hsu  * On a partial ack arrives, force the retransmission of the
3870c068736aSJeffrey Hsu  * next unacknowledged segment.  Do not clear tp->t_dupacks.
3871c068736aSJeffrey Hsu  * By setting snd_nxt to ti_ack, this forces retransmission timer to
3872c068736aSJeffrey Hsu  * be started again.
387346f58482SJonathan Lemon  */
387455bceb1eSRandall Stewart void
3875ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th)
387646f58482SJonathan Lemon {
387746f58482SJonathan Lemon 	tcp_seq onxt = tp->snd_nxt;
38783ac12506SJonathan T. Looney 	uint32_t ocwnd = tp->snd_cwnd;
38790c39d38dSGleb Smirnoff 	u_int maxseg = tcp_maxseg(tp);
388046f58482SJonathan Lemon 
38818501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
38827a3244ccSRobert Watson 
3883b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_REXMT, 0);
388446f58482SJonathan Lemon 	tp->t_rtttime = 0;
388546f58482SJonathan Lemon 	tp->snd_nxt = th->th_ack;
38866b2a5f92SJayanth Vijayaraghavan 	/*
3887c068736aSJeffrey Hsu 	 * Set snd_cwnd to one segment beyond acknowledged offset.
3888c068736aSJeffrey Hsu 	 * (tp->snd_una has not yet been updated when this function is called.)
38896b2a5f92SJayanth Vijayaraghavan 	 */
38900c39d38dSGleb Smirnoff 	tp->snd_cwnd = maxseg + BYTES_THIS_ACK(tp, th);
3891a84db8f4SMatthew Dillon 	tp->t_flags |= TF_ACKNOW;
389255bceb1eSRandall Stewart 	(void) tp->t_fb->tfb_tcp_output(tp);
389346f58482SJonathan Lemon 	tp->snd_cwnd = ocwnd;
389446f58482SJonathan Lemon 	if (SEQ_GT(onxt, tp->snd_nxt))
389546f58482SJonathan Lemon 		tp->snd_nxt = onxt;
389646f58482SJonathan Lemon 	/*
389746f58482SJonathan Lemon 	 * Partial window deflation.  Relies on fact that tp->snd_una
389846f58482SJonathan Lemon 	 * not updated yet.
389946f58482SJonathan Lemon 	 */
3900dbc42409SLawrence Stewart 	if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th))
3901dbc42409SLawrence Stewart 		tp->snd_cwnd -= BYTES_THIS_ACK(tp, th);
3902d7587117SPaul Saab 	else
3903d7587117SPaul Saab 		tp->snd_cwnd = 0;
39040c39d38dSGleb Smirnoff 	tp->snd_cwnd += maxseg;
390546f58482SJonathan Lemon }
390612eeb81fSHiren Panchasara 
390712eeb81fSHiren Panchasara int
390812eeb81fSHiren Panchasara tcp_compute_pipe(struct tcpcb *tp)
390912eeb81fSHiren Panchasara {
391012eeb81fSHiren Panchasara 	return (tp->snd_max - tp->snd_una +
391112eeb81fSHiren Panchasara 		tp->sackhint.sack_bytes_rexmit -
391212eeb81fSHiren Panchasara 		tp->sackhint.sacked_bytes);
391312eeb81fSHiren Panchasara }
3914