xref: /freebsd/sys/netinet/tcp_input.c (revision 3ac125068a211377f0b3817c37cf1db95a87e8fb)
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.
31df8bae1dSRodney W. Grimes  * 4. 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>
6039bc9de5SLawrence Stewart #include <sys/hhook.h>
61df8bae1dSRodney W. Grimes #include <sys/malloc.h>
62df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
63a29f300eSGarrett Wollman #include <sys/proc.h>		/* for proc0 declaration */
64df8bae1dSRodney W. Grimes #include <sys/protosw.h>
6557f60867SMark Johnston #include <sys/sdt.h>
66960ed29cSSeigo Tanimura #include <sys/signalvar.h>
67df8bae1dSRodney W. Grimes #include <sys/socket.h>
68df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
69960ed29cSSeigo Tanimura #include <sys/sysctl.h>
70816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
71960ed29cSSeigo Tanimura #include <sys/systm.h>
72df8bae1dSRodney W. Grimes 
73e79adb8eSGarrett Wollman #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
74e79adb8eSGarrett Wollman 
7512e2e970SAndre Oppermann #include <vm/uma.h>
7612e2e970SAndre Oppermann 
77df8bae1dSRodney W. Grimes #include <net/if.h>
7876039bc8SGleb Smirnoff #include <net/if_var.h>
79df8bae1dSRodney W. Grimes #include <net/route.h>
80530c0060SRobert Watson #include <net/vnet.h>
81df8bae1dSRodney W. Grimes 
82773673c1SAndre Oppermann #define TCPSTATES		/* for logging */
83773673c1SAndre Oppermann 
84df8bae1dSRodney W. Grimes #include <netinet/in.h>
8557f60867SMark Johnston #include <netinet/in_kdtrace.h>
86960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
87df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
88df8bae1dSRodney W. Grimes #include <netinet/ip.h>
898e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h>	/* required for icmp_var.h */
90686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM */
91df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
92ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
93686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
94686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
95686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
96b66d74c1SGleb Smirnoff #include <netinet6/in6_var.h>
97960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h>
98960ed29cSSeigo Tanimura #include <netinet6/nd6.h>
99281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
100281a0fd4SPatrick Kelsey #include <netinet/tcp_fastopen.h>
101281a0fd4SPatrick Kelsey #endif
1022de3e790SGleb Smirnoff #include <netinet/tcp.h>
103df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
104df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
105df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
106df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
107fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h>
108df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
1094644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
11086a996e6SHiren Panchasara #ifdef TCPPCAP
11186a996e6SHiren Panchasara #include <netinet/tcp_pcap.h>
11286a996e6SHiren Panchasara #endif
113c325962bSMike Silbersack #include <netinet/tcp_syncache.h>
114610ee2f9SDavid Greenman #ifdef TCPDEBUG
115df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
116fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */
11709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
11809fe6320SNavdeep Parhar #include <netinet/tcp_offload.h>
11909fe6320SNavdeep Parhar #endif
120fb59c426SYoshinobu Inoue 
121b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
122b9234fafSSam Leffler #include <netipsec/ipsec.h>
123b9234fafSSam Leffler #include <netipsec/ipsec6.h>
124b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
125b9234fafSSam Leffler 
126db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
127db4f9cc7SJonathan Lemon 
128aed55708SRobert Watson #include <security/mac/mac_framework.h>
129aed55708SRobert Watson 
130dbc42409SLawrence Stewart const int tcprexmtthresh = 3;
1310312fbe9SPoul-Henning Kamp 
132773673c1SAndre Oppermann int tcp_log_in_vain = 0;
133816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW,
134eddfbb76SRobert Watson     &tcp_log_in_vain, 0,
135eddfbb76SRobert Watson     "Log all incoming TCP segments to closed ports");
136816a3d83SPoul-Henning Kamp 
13782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, blackhole) = 0;
13882cea7e6SBjoern A. Zeeb #define	V_blackhole		VNET(blackhole)
1396df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW,
140eddfbb76SRobert Watson     &VNET_NAME(blackhole), 0,
141eddfbb76SRobert Watson     "Do not send RST on segments to closed ports");
14216f7f31fSGeoff Rehmet 
14382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_delack_enabled) = 1;
1446df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_VNET | CTLFLAG_RW,
145eddfbb76SRobert Watson     &VNET_NAME(tcp_delack_enabled), 0,
1463d177f46SBill Fumerola     "Delay ACK to try and piggyback it onto a data packet");
147f498eeeeSDavid Greenman 
14882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, drop_synfin) = 0;
14982cea7e6SBjoern A. Zeeb #define	V_drop_synfin		VNET(drop_synfin)
1506df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_VNET | CTLFLAG_RW,
151eddfbb76SRobert Watson     &VNET_NAME(drop_synfin), 0,
152eddfbb76SRobert Watson     "Drop TCP packets with SYN+FIN set");
153f8613305SDag-Erling Smørgrav 
15412eeb81fSHiren Panchasara VNET_DEFINE(int, tcp_do_rfc6675_pipe) = 0;
155054d38e3SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc6675_pipe, CTLFLAG_VNET | CTLFLAG_RW,
15612eeb81fSHiren Panchasara     &VNET_NAME(tcp_do_rfc6675_pipe), 0,
15712eeb81fSHiren Panchasara     "Use calculated pipe/in-flight bytes per RFC 6675");
15812eeb81fSHiren Panchasara 
15982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3042) = 1;
16082cea7e6SBjoern A. Zeeb #define	V_tcp_do_rfc3042	VNET(tcp_do_rfc3042)
1616df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_VNET | CTLFLAG_RW,
162eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3042), 0,
163eddfbb76SRobert Watson     "Enable RFC 3042 (Limited Transmit)");
164582a954bSJeffrey Hsu 
16582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3390) = 1;
1666df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_VNET | CTLFLAG_RW,
167eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3390), 0,
168da3a8a1aSJeffrey Hsu     "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)");
169da3a8a1aSJeffrey Hsu 
170356c7958SHiren Panchasara VNET_DEFINE(int, tcp_initcwnd_segments) = 10;
171356c7958SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, initcwnd_segments,
172356c7958SHiren Panchasara     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_initcwnd_segments), 0,
173356c7958SHiren Panchasara     "Slow-start flight size (initial congestion window) in number of segments");
17409440655SAndre Oppermann 
17582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3465) = 1;
1766df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_VNET | CTLFLAG_RW,
177eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3465), 0,
17824cb0f22SLawrence Stewart     "Enable RFC 3465 (Appropriate Byte Counting)");
179eddfbb76SRobert Watson 
18082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_abc_l_var) = 2;
1816df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_VNET | CTLFLAG_RW,
182eddfbb76SRobert Watson     &VNET_NAME(tcp_abc_l_var), 2,
18324cb0f22SLawrence Stewart     "Cap the max cwnd increment during slow-start to this number of segments");
18424cb0f22SLawrence Stewart 
1856472ac3dSEd Schouten static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, CTLFLAG_RW, 0, "TCP ECN");
186f2512ba1SRui Paulo 
187883054b4SDon Lewis VNET_DEFINE(int, tcp_do_ecn) = 2;
1886df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW,
189eddfbb76SRobert Watson     &VNET_NAME(tcp_do_ecn), 0,
190eddfbb76SRobert Watson     "TCP ECN support");
191eddfbb76SRobert Watson 
19282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_ecn_maxretries) = 1;
1936df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, maxretries, CTLFLAG_VNET | CTLFLAG_RW,
194eddfbb76SRobert Watson     &VNET_NAME(tcp_ecn_maxretries), 0,
195eddfbb76SRobert Watson     "Max retries before giving up on ECN");
196eddfbb76SRobert Watson 
1973220a212SGleb Smirnoff VNET_DEFINE(int, tcp_insecure_syn) = 0;
1983220a212SGleb Smirnoff #define	V_tcp_insecure_syn	VNET(tcp_insecure_syn)
1996df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_syn, CTLFLAG_VNET | CTLFLAG_RW,
2003220a212SGleb Smirnoff     &VNET_NAME(tcp_insecure_syn), 0,
2013220a212SGleb Smirnoff     "Follow RFC793 instead of RFC5961 criteria for accepting SYN packets");
2023220a212SGleb Smirnoff 
20382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_insecure_rst) = 0;
20482cea7e6SBjoern A. Zeeb #define	V_tcp_insecure_rst	VNET(tcp_insecure_rst)
2056df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_VNET | CTLFLAG_RW,
206eddfbb76SRobert Watson     &VNET_NAME(tcp_insecure_rst), 0,
2073220a212SGleb Smirnoff     "Follow RFC793 instead of RFC5961 criteria for accepting RST packets");
208a69968eeSMike Silbersack 
209fba0cea1SBjoern A. Zeeb VNET_DEFINE(int, tcp_recvspace) = 1024*64;
210873789cbSAndre Oppermann #define	V_tcp_recvspace	VNET(tcp_recvspace)
2116df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_VNET | CTLFLAG_RW,
212873789cbSAndre Oppermann     &VNET_NAME(tcp_recvspace), 0, "Initial receive socket buffer size");
213873789cbSAndre Oppermann 
21482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autorcvbuf) = 1;
21582cea7e6SBjoern A. Zeeb #define	V_tcp_do_autorcvbuf	VNET(tcp_do_autorcvbuf)
2166df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_VNET | CTLFLAG_RW,
217eddfbb76SRobert Watson     &VNET_NAME(tcp_do_autorcvbuf), 0,
2188b615593SMarko Zec     "Enable automatic receive buffer sizing");
2196741ecf5SAndre Oppermann 
22082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_inc) = 16*1024;
22182cea7e6SBjoern A. Zeeb #define	V_tcp_autorcvbuf_inc	VNET(tcp_autorcvbuf_inc)
2226df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_VNET | CTLFLAG_RW,
223eddfbb76SRobert Watson     &VNET_NAME(tcp_autorcvbuf_inc), 0,
2246489fe65SAndre Oppermann     "Incrementor step size of automatic receive buffer");
2256741ecf5SAndre Oppermann 
226b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024;
22782cea7e6SBjoern A. Zeeb #define	V_tcp_autorcvbuf_max	VNET(tcp_autorcvbuf_max)
2286df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_VNET | CTLFLAG_RW,
229eddfbb76SRobert Watson     &VNET_NAME(tcp_autorcvbuf_max), 0,
2308b615593SMarko Zec     "Max size of automatic receive buffer");
2316741ecf5SAndre Oppermann 
232eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, tcb);
23344e33a07SMarko Zec #define	tcb6	tcb  /* for KAME src sync over BSD*'s */
23482cea7e6SBjoern A. Zeeb VNET_DEFINE(struct inpcbinfo, tcbinfo);
235df8bae1dSRodney W. Grimes 
236315e3e38SRobert Watson /*
237bf840a17SGleb Smirnoff  * TCP statistics are stored in an array of counter(9)s, which size matches
238bf840a17SGleb Smirnoff  * size of struct tcpstat.  TCP running connection count is a regular array.
2395923c293SGleb Smirnoff  */
2405da0521fSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct tcpstat, tcpstat);
2415da0521fSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_tcp, TCPCTL_STATS, stats, struct tcpstat,
2425da0521fSAndrey V. Elsukov     tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
243bf840a17SGleb Smirnoff VNET_DEFINE(counter_u64_t, tcps_states[TCP_NSTATES]);
244bf840a17SGleb Smirnoff SYSCTL_COUNTER_U64_ARRAY(_net_inet_tcp, TCPCTL_STATES, states, CTLFLAG_RD |
245d4d32b9fSHans Petter Selasky     CTLFLAG_VNET, &VNET_NAME(tcps_states)[0], TCP_NSTATES,
246bf840a17SGleb Smirnoff     "TCP connection counts by TCP state");
247bf840a17SGleb Smirnoff 
248bf840a17SGleb Smirnoff static void
249bf840a17SGleb Smirnoff tcp_vnet_init(const void *unused)
250bf840a17SGleb Smirnoff {
251bf840a17SGleb Smirnoff 
252f59d975eSGleb Smirnoff 	COUNTER_ARRAY_ALLOC(V_tcps_states, TCP_NSTATES, M_WAITOK);
253bf840a17SGleb Smirnoff 	VNET_PCPUSTAT_ALLOC(tcpstat, M_WAITOK);
254bf840a17SGleb Smirnoff }
255bf840a17SGleb Smirnoff VNET_SYSINIT(tcp_vnet_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
256bf840a17SGleb Smirnoff     tcp_vnet_init, NULL);
2575923c293SGleb Smirnoff 
2585923c293SGleb Smirnoff #ifdef VIMAGE
259bf840a17SGleb Smirnoff static void
260bf840a17SGleb Smirnoff tcp_vnet_uninit(const void *unused)
261bf840a17SGleb Smirnoff {
262bf840a17SGleb Smirnoff 
263f59d975eSGleb Smirnoff 	COUNTER_ARRAY_FREE(V_tcps_states, TCP_NSTATES);
264bf840a17SGleb Smirnoff 	VNET_PCPUSTAT_FREE(tcpstat);
265bf840a17SGleb Smirnoff }
266bf840a17SGleb Smirnoff VNET_SYSUNINIT(tcp_vnet_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
267bf840a17SGleb Smirnoff     tcp_vnet_uninit, NULL);
2685923c293SGleb Smirnoff #endif /* VIMAGE */
269bf840a17SGleb Smirnoff 
2705923c293SGleb Smirnoff /*
271315e3e38SRobert Watson  * Kernel module interface for updating tcpstat.  The argument is an index
2725923c293SGleb Smirnoff  * into tcpstat treated as an array.
273315e3e38SRobert Watson  */
274315e3e38SRobert Watson void
275315e3e38SRobert Watson kmod_tcpstat_inc(int statnum)
276315e3e38SRobert Watson {
277315e3e38SRobert Watson 
2785da0521fSAndrey V. Elsukov 	counter_u64_add(VNET(tcpstat)[statnum], 1);
279315e3e38SRobert Watson }
280315e3e38SRobert Watson 
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 }
29839bc9de5SLawrence Stewart 
29939bc9de5SLawrence Stewart /*
300dbc42409SLawrence Stewart  * CC wrapper hook functions
301dbc42409SLawrence Stewart  */
30255bceb1eSRandall Stewart void
3034b7b743cSLawrence Stewart cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t nsegs,
3044b7b743cSLawrence Stewart     uint16_t type)
305f2512ba1SRui Paulo {
306dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
307f2512ba1SRui Paulo 
3084b7b743cSLawrence Stewart 	tp->ccv->nsegs = nsegs;
309dbc42409SLawrence Stewart 	tp->ccv->bytes_this_ack = BYTES_THIS_ACK(tp, th);
3105b648e79SLawrence Stewart 	if (tp->snd_cwnd <= tp->snd_wnd)
311dbc42409SLawrence Stewart 		tp->ccv->flags |= CCF_CWND_LIMITED;
312dbc42409SLawrence Stewart 	else
313dbc42409SLawrence Stewart 		tp->ccv->flags &= ~CCF_CWND_LIMITED;
314dbc42409SLawrence Stewart 
315dbc42409SLawrence Stewart 	if (type == CC_ACK) {
316dbc42409SLawrence Stewart 		if (tp->snd_cwnd > tp->snd_ssthresh) {
317dbc42409SLawrence Stewart 			tp->t_bytes_acked += min(tp->ccv->bytes_this_ack,
3184b7b743cSLawrence Stewart 			     nsegs * V_tcp_abc_l_var * tcp_maxseg(tp));
319dbc42409SLawrence Stewart 			if (tp->t_bytes_acked >= tp->snd_cwnd) {
320dbc42409SLawrence Stewart 				tp->t_bytes_acked -= tp->snd_cwnd;
321dbc42409SLawrence Stewart 				tp->ccv->flags |= CCF_ABC_SENTAWND;
322dbc42409SLawrence Stewart 			}
323dbc42409SLawrence Stewart 		} else {
324dbc42409SLawrence Stewart 				tp->ccv->flags &= ~CCF_ABC_SENTAWND;
325dbc42409SLawrence Stewart 				tp->t_bytes_acked = 0;
326dbc42409SLawrence Stewart 		}
327dbc42409SLawrence Stewart 	}
328dbc42409SLawrence Stewart 
329dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->ack_received != NULL) {
330dbc42409SLawrence Stewart 		/* XXXLAS: Find a way to live without this */
331dbc42409SLawrence Stewart 		tp->ccv->curack = th->th_ack;
332dbc42409SLawrence Stewart 		CC_ALGO(tp)->ack_received(tp->ccv, type);
333dbc42409SLawrence Stewart 	}
334dbc42409SLawrence Stewart }
335dbc42409SLawrence Stewart 
33655bceb1eSRandall Stewart void
337dbc42409SLawrence Stewart cc_conn_init(struct tcpcb *tp)
338dbc42409SLawrence Stewart {
339dbc42409SLawrence Stewart 	struct hc_metrics_lite metrics;
340dbc42409SLawrence Stewart 	struct inpcb *inp = tp->t_inpcb;
3410c39d38dSGleb Smirnoff 	u_int maxseg;
342dbc42409SLawrence Stewart 	int rtt;
343dbc42409SLawrence Stewart 
344dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
345dbc42409SLawrence Stewart 
346dbc42409SLawrence Stewart 	tcp_hc_get(&inp->inp_inc, &metrics);
3470c39d38dSGleb Smirnoff 	maxseg = tcp_maxseg(tp);
348dbc42409SLawrence Stewart 
349dbc42409SLawrence Stewart 	if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) {
350dbc42409SLawrence Stewart 		tp->t_srtt = rtt;
351dbc42409SLawrence Stewart 		tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE;
352dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_usedrtt);
353dbc42409SLawrence Stewart 		if (metrics.rmx_rttvar) {
354dbc42409SLawrence Stewart 			tp->t_rttvar = metrics.rmx_rttvar;
355dbc42409SLawrence Stewart 			TCPSTAT_INC(tcps_usedrttvar);
356dbc42409SLawrence Stewart 		} else {
357dbc42409SLawrence Stewart 			/* default variation is +- 1 rtt */
358dbc42409SLawrence Stewart 			tp->t_rttvar =
359dbc42409SLawrence Stewart 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
360dbc42409SLawrence Stewart 		}
361dbc42409SLawrence Stewart 		TCPT_RANGESET(tp->t_rxtcur,
362dbc42409SLawrence Stewart 		    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
363dbc42409SLawrence Stewart 		    tp->t_rttmin, TCPTV_REXMTMAX);
364dbc42409SLawrence Stewart 	}
365dbc42409SLawrence Stewart 	if (metrics.rmx_ssthresh) {
366dbc42409SLawrence Stewart 		/*
367dbc42409SLawrence Stewart 		 * There's some sort of gateway or interface
368dbc42409SLawrence Stewart 		 * buffer limit on the path.  Use this to set
369a4641f4eSPedro F. Giffuni 		 * the slow start threshold, but set the
370dbc42409SLawrence Stewart 		 * threshold to no less than 2*mss.
371dbc42409SLawrence Stewart 		 */
3720c39d38dSGleb Smirnoff 		tp->snd_ssthresh = max(2 * maxseg, metrics.rmx_ssthresh);
373dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_usedssthresh);
374dbc42409SLawrence Stewart 	}
375dbc42409SLawrence Stewart 
376dbc42409SLawrence Stewart 	/*
3779ec4a4ccSAndre Oppermann 	 * Set the initial slow-start flight size.
378dbc42409SLawrence Stewart 	 *
379cf8f04f4SAndre Oppermann 	 * RFC5681 Section 3.1 specifies the default conservative values.
380cf8f04f4SAndre Oppermann 	 * RFC3390 specifies slightly more aggressive values.
381b8b4cfcdSSergey Kandaurov 	 * RFC6928 increases it to ten segments.
382356c7958SHiren Panchasara 	 * Support for user specified value for initial flight size.
383cf8f04f4SAndre Oppermann 	 *
384cf8f04f4SAndre Oppermann 	 * If a SYN or SYN/ACK was lost and retransmitted, we have to
385cf8f04f4SAndre Oppermann 	 * reduce the initial CWND to one segment as congestion is likely
386cf8f04f4SAndre Oppermann 	 * requiring us to be cautious.
387dbc42409SLawrence Stewart 	 */
388cf8f04f4SAndre Oppermann 	if (tp->snd_cwnd == 1)
3890c39d38dSGleb Smirnoff 		tp->snd_cwnd = maxseg;		/* SYN(-ACK) lost */
390356c7958SHiren Panchasara 	else if (V_tcp_initcwnd_segments)
3910c39d38dSGleb Smirnoff 		tp->snd_cwnd = min(V_tcp_initcwnd_segments * maxseg,
3920c39d38dSGleb Smirnoff 		    max(2 * maxseg, V_tcp_initcwnd_segments * 1460));
393cf8f04f4SAndre Oppermann 	else if (V_tcp_do_rfc3390)
3940c39d38dSGleb Smirnoff 		tp->snd_cwnd = min(4 * maxseg, max(2 * maxseg, 4380));
39522efabd4SAndre Oppermann 	else {
39622efabd4SAndre Oppermann 		/* Per RFC5681 Section 3.1 */
3970c39d38dSGleb Smirnoff 		if (maxseg > 2190)
3980c39d38dSGleb Smirnoff 			tp->snd_cwnd = 2 * maxseg;
3990c39d38dSGleb Smirnoff 		else if (maxseg > 1095)
4000c39d38dSGleb Smirnoff 			tp->snd_cwnd = 3 * maxseg;
401dbc42409SLawrence Stewart 		else
4020c39d38dSGleb Smirnoff 			tp->snd_cwnd = 4 * maxseg;
40322efabd4SAndre Oppermann 	}
404dbc42409SLawrence Stewart 
405dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->conn_init != NULL)
406dbc42409SLawrence Stewart 		CC_ALGO(tp)->conn_init(tp->ccv);
407dbc42409SLawrence Stewart }
408dbc42409SLawrence Stewart 
409dbc42409SLawrence Stewart void inline
410dbc42409SLawrence Stewart cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type)
411dbc42409SLawrence Stewart {
4120c39d38dSGleb Smirnoff 	u_int maxseg;
4130c39d38dSGleb Smirnoff 
414dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
415dbc42409SLawrence Stewart 
416dbc42409SLawrence Stewart 	switch(type) {
417dbc42409SLawrence Stewart 	case CC_NDUPACK:
418dbc42409SLawrence Stewart 		if (!IN_FASTRECOVERY(tp->t_flags)) {
419f2512ba1SRui Paulo 			tp->snd_recover = tp->snd_max;
420f2512ba1SRui Paulo 			if (tp->t_flags & TF_ECN_PERMIT)
421f2512ba1SRui Paulo 				tp->t_flags |= TF_ECN_SND_CWR;
422f2512ba1SRui Paulo 		}
423dbc42409SLawrence Stewart 		break;
424dbc42409SLawrence Stewart 	case CC_ECN:
425dbc42409SLawrence Stewart 		if (!IN_CONGRECOVERY(tp->t_flags)) {
426dbc42409SLawrence Stewart 			TCPSTAT_INC(tcps_ecn_rcwnd);
427dbc42409SLawrence Stewart 			tp->snd_recover = tp->snd_max;
428dbc42409SLawrence Stewart 			if (tp->t_flags & TF_ECN_PERMIT)
429dbc42409SLawrence Stewart 				tp->t_flags |= TF_ECN_SND_CWR;
430dbc42409SLawrence Stewart 		}
431dbc42409SLawrence Stewart 		break;
432dbc42409SLawrence Stewart 	case CC_RTO:
4330c39d38dSGleb Smirnoff 		maxseg = tcp_maxseg(tp);
434dbc42409SLawrence Stewart 		tp->t_dupacks = 0;
435dbc42409SLawrence Stewart 		tp->t_bytes_acked = 0;
436dbc42409SLawrence Stewart 		EXIT_RECOVERY(tp->t_flags);
4376157935fSLawrence Stewart 		tp->snd_ssthresh = max(2, min(tp->snd_wnd, tp->snd_cwnd) / 2 /
4380c39d38dSGleb Smirnoff 		    maxseg) * maxseg;
4390c39d38dSGleb Smirnoff 		tp->snd_cwnd = maxseg;
440dbc42409SLawrence Stewart 		break;
441dbc42409SLawrence Stewart 	case CC_RTO_ERR:
442dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_sndrexmitbad);
443dbc42409SLawrence Stewart 		/* RTO was unnecessary, so reset everything. */
444dbc42409SLawrence Stewart 		tp->snd_cwnd = tp->snd_cwnd_prev;
445dbc42409SLawrence Stewart 		tp->snd_ssthresh = tp->snd_ssthresh_prev;
446dbc42409SLawrence Stewart 		tp->snd_recover = tp->snd_recover_prev;
447dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASFRECOVERY)
448dbc42409SLawrence Stewart 			ENTER_FASTRECOVERY(tp->t_flags);
449dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASCRECOVERY)
450dbc42409SLawrence Stewart 			ENTER_CONGRECOVERY(tp->t_flags);
451dbc42409SLawrence Stewart 		tp->snd_nxt = tp->snd_max;
452672dc4aeSJohn Baldwin 		tp->t_flags &= ~TF_PREVVALID;
453dbc42409SLawrence Stewart 		tp->t_badrxtwin = 0;
454dbc42409SLawrence Stewart 		break;
455dbc42409SLawrence Stewart 	}
456dbc42409SLawrence Stewart 
457dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->cong_signal != NULL) {
458dbc42409SLawrence Stewart 		if (th != NULL)
459dbc42409SLawrence Stewart 			tp->ccv->curack = th->th_ack;
460dbc42409SLawrence Stewart 		CC_ALGO(tp)->cong_signal(tp->ccv, type);
461dbc42409SLawrence Stewart 	}
462dbc42409SLawrence Stewart }
463dbc42409SLawrence Stewart 
46455bceb1eSRandall Stewart void inline
465dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th)
466dbc42409SLawrence Stewart {
467dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
468dbc42409SLawrence Stewart 
469dbc42409SLawrence Stewart 	/* XXXLAS: KASSERT that we're in recovery? */
470dbc42409SLawrence Stewart 
471dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->post_recovery != NULL) {
472dbc42409SLawrence Stewart 		tp->ccv->curack = th->th_ack;
473dbc42409SLawrence Stewart 		CC_ALGO(tp)->post_recovery(tp->ccv);
474dbc42409SLawrence Stewart 	}
475dbc42409SLawrence Stewart 	/* XXXLAS: EXIT_RECOVERY ? */
476dbc42409SLawrence Stewart 	tp->t_bytes_acked = 0;
477dbc42409SLawrence Stewart }
4780312fbe9SPoul-Henning Kamp 
4792903309aSAttilio Rao #ifdef TCP_SIGNATURE
4802903309aSAttilio Rao static inline int
4812903309aSAttilio Rao tcp_signature_verify_input(struct mbuf *m, int off0, int tlen, int optlen,
4822903309aSAttilio Rao     struct tcpopt *to, struct tcphdr *th, u_int tcpbflag)
4832903309aSAttilio Rao {
4842903309aSAttilio Rao 	int ret;
4852903309aSAttilio Rao 
4862903309aSAttilio Rao 	tcp_fields_to_net(th);
4872903309aSAttilio Rao 	ret = tcp_signature_verify(m, off0, tlen, optlen, to, th, tcpbflag);
4882903309aSAttilio Rao 	tcp_fields_to_host(th);
4892903309aSAttilio Rao 	return (ret);
4902903309aSAttilio Rao }
4912903309aSAttilio Rao #endif
4922903309aSAttilio Rao 
493df8bae1dSRodney W. Grimes /*
494262c1c1aSMatthew Dillon  * Indicate whether this ack should be delayed.  We can delay the ack if
49585536381SHiren Panchasara  * following conditions are met:
49685536381SHiren Panchasara  *	- There is no delayed ack timer in progress.
49785536381SHiren Panchasara  *	- Our last ack wasn't a 0-sized window. We never want to delay
49885536381SHiren Panchasara  *	  the ack that opens up a 0-sized window.
49985536381SHiren Panchasara  *	- LRO wasn't used for this segment. We make sure by checking that the
50085536381SHiren Panchasara  *	  segment size is not larger than the MSS.
501d8c85a26SJonathan Lemon  */
502c1e5a6e5SAndre Oppermann #define DELAY_ACK(tp, tlen)						\
503b8152ba7SAndre Oppermann 	((!tcp_timer_active(tp, TT_DELACK) &&				\
504a14c749fSJonathan Lemon 	    (tp->t_flags & TF_RXWIN0SENT) == 0) &&			\
5050c39d38dSGleb Smirnoff 	    (tlen <= tp->t_maxseg) &&					\
506603724d3SBjoern A. Zeeb 	    (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN)))
507d8c85a26SJonathan Lemon 
50864807b30SHiren Panchasara static void inline
50964807b30SHiren Panchasara cc_ecnpkt_handler(struct tcpcb *tp, struct tcphdr *th, uint8_t iptos)
51064807b30SHiren Panchasara {
51164807b30SHiren Panchasara 	INP_WLOCK_ASSERT(tp->t_inpcb);
51264807b30SHiren Panchasara 
51364807b30SHiren Panchasara 	if (CC_ALGO(tp)->ecnpkt_handler != NULL) {
51464807b30SHiren Panchasara 		switch (iptos & IPTOS_ECN_MASK) {
51564807b30SHiren Panchasara 		case IPTOS_ECN_CE:
51664807b30SHiren Panchasara 		    tp->ccv->flags |= CCF_IPHDR_CE;
51764807b30SHiren Panchasara 		    break;
51864807b30SHiren Panchasara 		case IPTOS_ECN_ECT0:
51964807b30SHiren Panchasara 		    tp->ccv->flags &= ~CCF_IPHDR_CE;
52064807b30SHiren Panchasara 		    break;
52164807b30SHiren Panchasara 		case IPTOS_ECN_ECT1:
52264807b30SHiren Panchasara 		    tp->ccv->flags &= ~CCF_IPHDR_CE;
52364807b30SHiren Panchasara 		    break;
52464807b30SHiren Panchasara 		}
52564807b30SHiren Panchasara 
52664807b30SHiren Panchasara 		if (th->th_flags & TH_CWR)
52764807b30SHiren Panchasara 			tp->ccv->flags |= CCF_TCPHDR_CWR;
52864807b30SHiren Panchasara 		else
52964807b30SHiren Panchasara 			tp->ccv->flags &= ~CCF_TCPHDR_CWR;
53064807b30SHiren Panchasara 
53164807b30SHiren Panchasara 		if (tp->t_flags & TF_DELACK)
53264807b30SHiren Panchasara 			tp->ccv->flags |= CCF_DELACK;
53364807b30SHiren Panchasara 		else
53464807b30SHiren Panchasara 			tp->ccv->flags &= ~CCF_DELACK;
53564807b30SHiren Panchasara 
53664807b30SHiren Panchasara 		CC_ALGO(tp)->ecnpkt_handler(tp->ccv);
53764807b30SHiren Panchasara 
53864807b30SHiren Panchasara 		if (tp->ccv->flags & CCF_ACKNOW)
53964807b30SHiren Panchasara 			tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
54064807b30SHiren Panchasara 	}
54164807b30SHiren Panchasara }
54264807b30SHiren Panchasara 
543df8bae1dSRodney W. Grimes /*
5448d573cc1SAndre Oppermann  * TCP input handling is split into multiple parts:
5458d573cc1SAndre Oppermann  *   tcp6_input is a thin wrapper around tcp_input for the extended
5468d573cc1SAndre Oppermann  *	ip6_protox[] call format in ip6_input
5478d573cc1SAndre Oppermann  *   tcp_input handles primary segment validation, inpcb lookup and
5488d573cc1SAndre Oppermann  *	SYN processing on listen sockets
5498d573cc1SAndre Oppermann  *   tcp_do_segment processes the ACK and text of the segment for
5508d573cc1SAndre Oppermann  *	establishing, established and closing connections
551df8bae1dSRodney W. Grimes  */
552fb59c426SYoshinobu Inoue #ifdef INET6
553fb59c426SYoshinobu Inoue int
554ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto)
555fb59c426SYoshinobu Inoue {
556ad3f9ab3SAndre Oppermann 	struct mbuf *m = *mp;
55733841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6;
5583e88eb90SAndrey V. Elsukov 	struct ip6_hdr *ip6;
559fb59c426SYoshinobu Inoue 
560fb59c426SYoshinobu Inoue 	IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE);
561fb59c426SYoshinobu Inoue 
562fb59c426SYoshinobu Inoue 	/*
563fb59c426SYoshinobu Inoue 	 * draft-itojun-ipv6-tcp-to-anycast
564fb59c426SYoshinobu Inoue 	 * better place to put this in?
565fb59c426SYoshinobu Inoue 	 */
5663e88eb90SAndrey V. Elsukov 	ip6 = mtod(m, struct ip6_hdr *);
5673e88eb90SAndrey V. Elsukov 	ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
56833841545SHajimu UMEMOTO 	if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
569fb59c426SYoshinobu Inoue 		struct ip6_hdr *ip6;
570fb59c426SYoshinobu Inoue 
5718c0fec80SRobert Watson 		ifa_free(&ia6->ia_ifa);
572fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
573fb59c426SYoshinobu Inoue 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
574fb59c426SYoshinobu Inoue 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
5758f5a8818SKevin Lo 		return (IPPROTO_DONE);
576fb59c426SYoshinobu Inoue 	}
57777d396fdSMaksim Yevmenkin 	if (ia6)
57877d396fdSMaksim Yevmenkin 		ifa_free(&ia6->ia_ifa);
579fb59c426SYoshinobu Inoue 
5808f5a8818SKevin Lo 	return (tcp_input(mp, offp, proto));
581fb59c426SYoshinobu Inoue }
582b287c6c7SBjoern A. Zeeb #endif /* INET6 */
583fb59c426SYoshinobu Inoue 
5848f5a8818SKevin Lo int
5858f5a8818SKevin Lo tcp_input(struct mbuf **mp, int *offp, int proto)
586df8bae1dSRodney W. Grimes {
5878f5a8818SKevin Lo 	struct mbuf *m = *mp;
588b287c6c7SBjoern A. Zeeb 	struct tcphdr *th = NULL;
589ad3f9ab3SAndre Oppermann 	struct ip *ip = NULL;
590ad3f9ab3SAndre Oppermann 	struct inpcb *inp = NULL;
591302ce8d6SAndre Oppermann 	struct tcpcb *tp = NULL;
592302ce8d6SAndre Oppermann 	struct socket *so = NULL;
593e79adb8eSGarrett Wollman 	u_char *optp = NULL;
5948f5a8818SKevin Lo 	int off0;
59526f9a767SRodney W. Grimes 	int optlen = 0;
596b287c6c7SBjoern A. Zeeb #ifdef INET
597b287c6c7SBjoern A. Zeeb 	int len;
598b287c6c7SBjoern A. Zeeb #endif
599b287c6c7SBjoern A. Zeeb 	int tlen = 0, off;
600fb59c426SYoshinobu Inoue 	int drop_hdrlen;
601ad3f9ab3SAndre Oppermann 	int thflags;
602302ce8d6SAndre Oppermann 	int rstreason = 0;	/* For badport_bandlim accounting purposes */
6032903309aSAttilio Rao #ifdef TCP_SIGNATURE
6042903309aSAttilio Rao 	uint8_t sig_checked = 0;
6052903309aSAttilio Rao #endif
6061d7ee746SKurt Lidl 	uint8_t iptos;
607c1de64a4SAndrey V. Elsukov 	struct m_tag *fwd_tag = NULL;
608c068736aSJeffrey Hsu #ifdef INET6
609302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6 = NULL;
610c068736aSJeffrey Hsu 	int isipv6;
611c068736aSJeffrey Hsu #else
612a160e630SAndre Oppermann 	const void *ip6 = NULL;
613b287c6c7SBjoern A. Zeeb #endif /* INET6 */
614302ce8d6SAndre Oppermann 	struct tcpopt to;		/* options in this segment */
615a160e630SAndre Oppermann 	char *s = NULL;			/* address and port logging */
616252ca428SRobert Watson 	int ti_locked;
617610ee2f9SDavid Greenman #ifdef TCPDEBUG
618c068736aSJeffrey Hsu 	/*
619c068736aSJeffrey Hsu 	 * The size of tcp_saveipgen must be the size of the max ip header,
620c068736aSJeffrey Hsu 	 * now IPv6.
621c068736aSJeffrey Hsu 	 */
62214739780SMaxim Konovalov 	u_char tcp_saveipgen[IP6_HDR_LEN];
623410bb1bfSLuigi Rizzo 	struct tcphdr tcp_savetcp;
624610ee2f9SDavid Greenman 	short ostate = 0;
625610ee2f9SDavid Greenman #endif
626c068736aSJeffrey Hsu 
627fb59c426SYoshinobu Inoue #ifdef INET6
628fb59c426SYoshinobu Inoue 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
629fb59c426SYoshinobu Inoue #endif
630a0292f23SGarrett Wollman 
6318f5a8818SKevin Lo 	off0 = *offp;
6328f5a8818SKevin Lo 	m = *mp;
6338f5a8818SKevin Lo 	*mp = NULL;
634302ce8d6SAndre Oppermann 	to.to_flags = 0;
63578b50714SRobert Watson 	TCPSTAT_INC(tcps_rcvtotal);
636fb59c426SYoshinobu Inoue 
63704d3a452SHajimu UMEMOTO #ifdef INET6
638b287c6c7SBjoern A. Zeeb 	if (isipv6) {
6398d573cc1SAndre Oppermann 		/* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */
64045747ba5SBjoern A. Zeeb 
64145747ba5SBjoern A. Zeeb 		if (m->m_len < (sizeof(*ip6) + sizeof(*th))) {
64245747ba5SBjoern A. Zeeb 			m = m_pullup(m, sizeof(*ip6) + sizeof(*th));
64345747ba5SBjoern A. Zeeb 			if (m == NULL) {
64445747ba5SBjoern A. Zeeb 				TCPSTAT_INC(tcps_rcvshort);
6458f5a8818SKevin Lo 				return (IPPROTO_DONE);
64645747ba5SBjoern A. Zeeb 			}
64745747ba5SBjoern A. Zeeb 		}
64845747ba5SBjoern A. Zeeb 
649fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
65045747ba5SBjoern A. Zeeb 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
651fb59c426SYoshinobu Inoue 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
652356ab07eSBjoern A. Zeeb 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) {
65345747ba5SBjoern A. Zeeb 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
65445747ba5SBjoern A. Zeeb 				th->th_sum = m->m_pkthdr.csum_data;
65545747ba5SBjoern A. Zeeb 			else
65645747ba5SBjoern A. Zeeb 				th->th_sum = in6_cksum_pseudo(ip6, tlen,
65745747ba5SBjoern A. Zeeb 				    IPPROTO_TCP, m->m_pkthdr.csum_data);
65845747ba5SBjoern A. Zeeb 			th->th_sum ^= 0xffff;
65945747ba5SBjoern A. Zeeb 		} else
66045747ba5SBjoern A. Zeeb 			th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen);
66145747ba5SBjoern A. Zeeb 		if (th->th_sum) {
66278b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
663fb59c426SYoshinobu Inoue 			goto drop;
664fb59c426SYoshinobu Inoue 		}
66533841545SHajimu UMEMOTO 
66633841545SHajimu UMEMOTO 		/*
66733841545SHajimu UMEMOTO 		 * Be proactive about unspecified IPv6 address in source.
66833841545SHajimu UMEMOTO 		 * As we use all-zero to indicate unbounded/unconnected pcb,
66933841545SHajimu UMEMOTO 		 * unspecified IPv6 address can be used to confuse us.
67033841545SHajimu UMEMOTO 		 *
67133841545SHajimu UMEMOTO 		 * Note that packets with unspecified IPv6 destination is
67233841545SHajimu UMEMOTO 		 * already dropped in ip6_input.
67333841545SHajimu UMEMOTO 		 */
67433841545SHajimu UMEMOTO 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
67533841545SHajimu UMEMOTO 			/* XXX stat */
67633841545SHajimu UMEMOTO 			goto drop;
67733841545SHajimu UMEMOTO 		}
6781d7ee746SKurt Lidl 		iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
679b287c6c7SBjoern A. Zeeb 	}
68004d3a452SHajimu UMEMOTO #endif
681b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
682b287c6c7SBjoern A. Zeeb 	else
683b287c6c7SBjoern A. Zeeb #endif
684b287c6c7SBjoern A. Zeeb #ifdef INET
685b287c6c7SBjoern A. Zeeb 	{
686df8bae1dSRodney W. Grimes 		/*
687df8bae1dSRodney W. Grimes 		 * Get IP and TCP header together in first mbuf.
688df8bae1dSRodney W. Grimes 		 * Note: IP leaves IP header in first mbuf.
689df8bae1dSRodney W. Grimes 		 */
690fb59c426SYoshinobu Inoue 		if (off0 > sizeof (struct ip)) {
691105bd211SGleb Smirnoff 			ip_stripoptions(m);
692fb59c426SYoshinobu Inoue 			off0 = sizeof(struct ip);
693fb59c426SYoshinobu Inoue 		}
694df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof (struct tcpiphdr)) {
6954dfdffe9SAndre Oppermann 			if ((m = m_pullup(m, sizeof (struct tcpiphdr)))
6964dfdffe9SAndre Oppermann 			    == NULL) {
69778b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvshort);
6988f5a8818SKevin Lo 				return (IPPROTO_DONE);
699df8bae1dSRodney W. Grimes 			}
700df8bae1dSRodney W. Grimes 		}
701fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
702db4f9cc7SJonathan Lemon 		th = (struct tcphdr *)((caddr_t)ip + off0);
7038ad458a4SGleb Smirnoff 		tlen = ntohs(ip->ip_len) - off0;
704df8bae1dSRodney W. Grimes 
7051d7ee746SKurt Lidl 		iptos = ip->ip_tos;
706db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
707db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
708db4f9cc7SJonathan Lemon 				th->th_sum = m->m_pkthdr.csum_data;
709db4f9cc7SJonathan Lemon 			else
710db4f9cc7SJonathan Lemon 				th->th_sum = in_pseudo(ip->ip_src.s_addr,
711c068736aSJeffrey Hsu 				    ip->ip_dst.s_addr,
7128f134647SGleb Smirnoff 				    htonl(m->m_pkthdr.csum_data + tlen +
713c068736aSJeffrey Hsu 				    IPPROTO_TCP));
714db4f9cc7SJonathan Lemon 			th->th_sum ^= 0xffff;
715db4f9cc7SJonathan Lemon 		} else {
7168f134647SGleb Smirnoff 			struct ipovly *ipov = (struct ipovly *)ip;
7178f134647SGleb Smirnoff 
718df8bae1dSRodney W. Grimes 			/*
719df8bae1dSRodney W. Grimes 			 * Checksum extended TCP header and data.
720df8bae1dSRodney W. Grimes 			 */
7218f134647SGleb Smirnoff 			len = off0 + tlen;
722fb59c426SYoshinobu Inoue 			bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
7238f134647SGleb Smirnoff 			ipov->ih_len = htons(tlen);
724fb59c426SYoshinobu Inoue 			th->th_sum = in_cksum(m, len);
72557f60867SMark Johnston 			/* Reset length for SDT probes. */
7261d7ee746SKurt Lidl 			ip->ip_len = htons(len);
7271d7ee746SKurt Lidl 			/* Reset TOS bits */
7281d7ee746SKurt Lidl 			ip->ip_tos = iptos;
7291d7ee746SKurt Lidl 			/* Re-initialization for later version check */
7301d7ee746SKurt Lidl 			ip->ip_v = IPVERSION;
731db4f9cc7SJonathan Lemon 		}
73257f60867SMark Johnston 
733fb59c426SYoshinobu Inoue 		if (th->th_sum) {
73478b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
735df8bae1dSRodney W. Grimes 			goto drop;
736df8bae1dSRodney W. Grimes 		}
737fb59c426SYoshinobu Inoue 	}
738b287c6c7SBjoern A. Zeeb #endif /* INET */
739df8bae1dSRodney W. Grimes 
740df8bae1dSRodney W. Grimes 	/*
741df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
742df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
743df8bae1dSRodney W. Grimes 	 */
744fb59c426SYoshinobu Inoue 	off = th->th_off << 2;
745df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
74678b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvbadoff);
747df8bae1dSRodney W. Grimes 		goto drop;
748df8bae1dSRodney W. Grimes 	}
749fb59c426SYoshinobu Inoue 	tlen -= off;	/* tlen is used instead of ti->ti_len */
750df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
75104d3a452SHajimu UMEMOTO #ifdef INET6
752b287c6c7SBjoern A. Zeeb 		if (isipv6) {
7538f5a8818SKevin Lo 			IP6_EXTHDR_CHECK(m, off0, off, IPPROTO_DONE);
754fb59c426SYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
755fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
756b287c6c7SBjoern A. Zeeb 		}
75704d3a452SHajimu UMEMOTO #endif
758b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
759b287c6c7SBjoern A. Zeeb 		else
760b287c6c7SBjoern A. Zeeb #endif
761b287c6c7SBjoern A. Zeeb #ifdef INET
762b287c6c7SBjoern A. Zeeb 		{
763df8bae1dSRodney W. Grimes 			if (m->m_len < sizeof(struct ip) + off) {
764c068736aSJeffrey Hsu 				if ((m = m_pullup(m, sizeof (struct ip) + off))
7654dfdffe9SAndre Oppermann 				    == NULL) {
76678b50714SRobert Watson 					TCPSTAT_INC(tcps_rcvshort);
7678f5a8818SKevin Lo 					return (IPPROTO_DONE);
768df8bae1dSRodney W. Grimes 				}
769fb59c426SYoshinobu Inoue 				ip = mtod(m, struct ip *);
770fb59c426SYoshinobu Inoue 				th = (struct tcphdr *)((caddr_t)ip + off0);
771fb59c426SYoshinobu Inoue 			}
772df8bae1dSRodney W. Grimes 		}
773b287c6c7SBjoern A. Zeeb #endif
774df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
775fb59c426SYoshinobu Inoue 		optp = (u_char *)(th + 1);
776df8bae1dSRodney W. Grimes 	}
777fb59c426SYoshinobu Inoue 	thflags = th->th_flags;
778df8bae1dSRodney W. Grimes 
779e46cd3d4SDag-Erling Smørgrav 	/*
780df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
781df8bae1dSRodney W. Grimes 	 */
7822903309aSAttilio Rao 	tcp_fields_to_host(th);
783df8bae1dSRodney W. Grimes 
784df8bae1dSRodney W. Grimes 	/*
785794235b7SAndre Oppermann 	 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options.
786755c1f07SAndras Olah 	 */
787fb59c426SYoshinobu Inoue 	drop_hdrlen = off0 + off;
788755c1f07SAndras Olah 
789755c1f07SAndras Olah 	/*
790fa046d87SRobert Watson 	 * Locate pcb for segment; if we're likely to add or remove a
791a7c7f2a7SJohn Baldwin 	 * connection then first acquire pcbinfo lock.  There are three cases
792fa046d87SRobert Watson 	 * where we might discover later we need a write lock despite the
793a7c7f2a7SJohn Baldwin 	 * flags: ACKs moving a connection out of the syncache, ACKs for a
794a7c7f2a7SJohn Baldwin 	 * connection in TIMEWAIT and SYNs not targeting a listening socket.
795df8bae1dSRodney W. Grimes 	 */
796a7c7f2a7SJohn Baldwin 	if ((thflags & (TH_FIN | TH_RST)) != 0) {
797ff9b006dSJulien Charbon 		INP_INFO_RLOCK(&V_tcbinfo);
798ff9b006dSJulien Charbon 		ti_locked = TI_RLOCKED;
799fa046d87SRobert Watson 	} else
800fa046d87SRobert Watson 		ti_locked = TI_UNLOCKED;
801252ca428SRobert Watson 
8028d573cc1SAndre Oppermann 	/*
8038d573cc1SAndre Oppermann 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
8048d573cc1SAndre Oppermann 	 */
805b8056faeSGleb Smirnoff         if (
806b8056faeSGleb Smirnoff #ifdef INET6
807b8056faeSGleb Smirnoff 	    (isipv6 && (m->m_flags & M_IP6_NEXTHOP))
808b8056faeSGleb Smirnoff #ifdef INET
809b8056faeSGleb Smirnoff 	    || (!isipv6 && (m->m_flags & M_IP_NEXTHOP))
810b8056faeSGleb Smirnoff #endif
811b8056faeSGleb Smirnoff #endif
812b8056faeSGleb Smirnoff #if defined(INET) && !defined(INET6)
813b8056faeSGleb Smirnoff 	    (m->m_flags & M_IP_NEXTHOP)
814b8056faeSGleb Smirnoff #endif
815b8056faeSGleb Smirnoff 	    )
8169b932e9eSAndre Oppermann 		fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
8179b932e9eSAndre Oppermann 
818ce7ad664SEd Maste findpcb:
819ce7ad664SEd Maste #ifdef INVARIANTS
820ff9b006dSJulien Charbon 	if (ti_locked == TI_RLOCKED) {
821ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
822ce7ad664SEd Maste 	} else {
823ce7ad664SEd Maste 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
824ce7ad664SEd Maste 	}
825ce7ad664SEd Maste #endif
8268a006adbSBjoern A. Zeeb #ifdef INET6
8278a006adbSBjoern A. Zeeb 	if (isipv6 && fwd_tag != NULL) {
8288a006adbSBjoern A. Zeeb 		struct sockaddr_in6 *next_hop6;
8298a006adbSBjoern A. Zeeb 
8308a006adbSBjoern A. Zeeb 		next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
8318a006adbSBjoern A. Zeeb 		/*
8328a006adbSBjoern A. Zeeb 		 * Transparently forwarded. Pretend to be the destination.
8338a006adbSBjoern A. Zeeb 		 * Already got one like this?
8348a006adbSBjoern A. Zeeb 		 */
8358a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo,
8368a006adbSBjoern A. Zeeb 		    &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport,
8378a006adbSBjoern A. Zeeb 		    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif, m);
8388a006adbSBjoern A. Zeeb 		if (!inp) {
8398a006adbSBjoern A. Zeeb 			/*
8408a006adbSBjoern A. Zeeb 			 * It's new.  Try to find the ambushing socket.
8418a006adbSBjoern A. Zeeb 			 * Because we've rewritten the destination address,
8428a006adbSBjoern A. Zeeb 			 * any hardware-generated hash is ignored.
8438a006adbSBjoern A. Zeeb 			 */
8448a006adbSBjoern A. Zeeb 			inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src,
8458a006adbSBjoern A. Zeeb 			    th->th_sport, &next_hop6->sin6_addr,
8468a006adbSBjoern A. Zeeb 			    next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) :
8478a006adbSBjoern A. Zeeb 			    th->th_dport, INPLOOKUP_WILDCARD |
8488a006adbSBjoern A. Zeeb 			    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif);
8498a006adbSBjoern A. Zeeb 		}
850c1de64a4SAndrey V. Elsukov 	} else if (isipv6) {
8518a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src,
8528a006adbSBjoern A. Zeeb 		    th->th_sport, &ip6->ip6_dst, th->th_dport,
8538a006adbSBjoern A. Zeeb 		    INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB,
8548a006adbSBjoern A. Zeeb 		    m->m_pkthdr.rcvif, m);
8558a006adbSBjoern A. Zeeb 	}
8568a006adbSBjoern A. Zeeb #endif /* INET6 */
8578a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET)
8588a006adbSBjoern A. Zeeb 	else
8598a006adbSBjoern A. Zeeb #endif
8608a006adbSBjoern A. Zeeb #ifdef INET
8618a006adbSBjoern A. Zeeb 	if (fwd_tag != NULL) {
8629b932e9eSAndre Oppermann 		struct sockaddr_in *next_hop;
8639b932e9eSAndre Oppermann 
8649b932e9eSAndre Oppermann 		next_hop = (struct sockaddr_in *)(fwd_tag+1);
865f9e354dfSJulian Elischer 		/*
8662b25acc1SLuigi Rizzo 		 * Transparently forwarded. Pretend to be the destination.
867f9e354dfSJulian Elischer 		 * already got one like this?
868f9e354dfSJulian Elischer 		 */
869d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport,
870fa046d87SRobert Watson 		    ip->ip_dst, th->th_dport, INPLOOKUP_WLOCKPCB,
871d3c1f003SRobert Watson 		    m->m_pkthdr.rcvif, m);
872fa046d87SRobert Watson 		if (!inp) {
873fa046d87SRobert Watson 			/*
874fa046d87SRobert Watson 			 * It's new.  Try to find the ambushing socket.
875d3c1f003SRobert Watson 			 * Because we've rewritten the destination address,
876d3c1f003SRobert Watson 			 * any hardware-generated hash is ignored.
877fa046d87SRobert Watson 			 */
878fa046d87SRobert Watson 			inp = in_pcblookup(&V_tcbinfo, ip->ip_src,
879fa046d87SRobert Watson 			    th->th_sport, next_hop->sin_addr,
880fa046d87SRobert Watson 			    next_hop->sin_port ? ntohs(next_hop->sin_port) :
881fa046d87SRobert Watson 			    th->th_dport, INPLOOKUP_WILDCARD |
882fa046d87SRobert Watson 			    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif);
883f9e354dfSJulian Elischer 		}
884b10fbdeaSAndre Oppermann 	} else
885d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src,
886fa046d87SRobert Watson 		    th->th_sport, ip->ip_dst, th->th_dport,
887fa046d87SRobert Watson 		    INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB,
888d3c1f003SRobert Watson 		    m->m_pkthdr.rcvif, m);
8898a006adbSBjoern A. Zeeb #endif /* INET */
890f9e354dfSJulian Elischer 
891df8bae1dSRodney W. Grimes 	/*
892574b6964SAndre Oppermann 	 * If the INPCB does not exist then all data in the incoming
893574b6964SAndre Oppermann 	 * segment is discarded and an appropriate RST is sent back.
8948b07e49aSJulian Elischer 	 * XXX MRT Send RST using which routing table?
895df8bae1dSRodney W. Grimes 	 */
896816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
897574b6964SAndre Oppermann 		/*
898574b6964SAndre Oppermann 		 * Log communication attempts to ports that are not
899574b6964SAndre Oppermann 		 * in use.
900574b6964SAndre Oppermann 		 */
901574b6964SAndre Oppermann 		if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) ||
902574b6964SAndre Oppermann 		    tcp_log_in_vain == 2) {
903b7d747ecSAndre Oppermann 			if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6)))
904df541e5fSAndre Oppermann 				log(LOG_INFO, "%s; %s: Connection attempt "
905df541e5fSAndre Oppermann 				    "to closed port\n", s, __func__);
9062e4e1b4cSGeoff Rehmet 		}
907574b6964SAndre Oppermann 		/*
908574b6964SAndre Oppermann 		 * When blackholing do not respond with a RST but
909574b6964SAndre Oppermann 		 * completely ignore the segment and drop it.
910574b6964SAndre Oppermann 		 */
911603724d3SBjoern A. Zeeb 		if ((V_blackhole == 1 && (thflags & TH_SYN)) ||
912603724d3SBjoern A. Zeeb 		    V_blackhole == 2)
9131929eae1SAndre Oppermann 			goto dropunlock;
914574b6964SAndre Oppermann 
915a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
916a57815efSBosko Milekic 		goto dropwithreset;
917816a3d83SPoul-Henning Kamp 	}
918fa046d87SRobert Watson 	INP_WLOCK_ASSERT(inp);
919c2529042SHans Petter Selasky 	if ((inp->inp_flowtype == M_HASHTYPE_NONE) &&
920c2529042SHans Petter Selasky 	    (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) &&
921c2529042SHans Petter Selasky 	    ((inp->inp_socket == NULL) ||
922c2529042SHans Petter Selasky 	    (inp->inp_socket->so_options & SO_ACCEPTCONN) == 0)) {
92380cb9f21SKip Macy 		inp->inp_flowid = m->m_pkthdr.flowid;
9242f719932SAdrian Chadd 		inp->inp_flowtype = M_HASHTYPE_GET(m);
92580cb9f21SKip Macy 	}
926a2589465SKen Smith #ifdef IPSEC
927a2589465SKen Smith #ifdef INET6
928a2589465SKen Smith 	if (isipv6 && ipsec6_in_reject(m, inp)) {
929a2589465SKen Smith 		goto dropunlock;
930a2589465SKen Smith 	} else
931a2589465SKen Smith #endif /* INET6 */
932a2589465SKen Smith 	if (ipsec4_in_reject(m, inp) != 0) {
933a2589465SKen Smith 		goto dropunlock;
934a2589465SKen Smith 	}
935a2589465SKen Smith #endif /* IPSEC */
936a2589465SKen Smith 
9378d573cc1SAndre Oppermann 	/*
9388d573cc1SAndre Oppermann 	 * Check the minimum TTL for socket.
9398d573cc1SAndre Oppermann 	 */
94034f83c52SGeorge V. Neville-Neil 	if (inp->inp_ip_minttl != 0) {
94134f83c52SGeorge V. Neville-Neil #ifdef INET6
9422d8868dbSJonathan T. Looney 		if (isipv6) {
9432d8868dbSJonathan T. Looney 			if (inp->inp_ip_minttl > ip6->ip6_hlim)
944302ce8d6SAndre Oppermann 				goto dropunlock;
9452d8868dbSJonathan T. Looney 		} else
94634f83c52SGeorge V. Neville-Neil #endif
94734f83c52SGeorge V. Neville-Neil 		if (inp->inp_ip_minttl > ip->ip_ttl)
948302ce8d6SAndre Oppermann 			goto dropunlock;
94934f83c52SGeorge V. Neville-Neil 	}
950936cd18dSAndre Oppermann 
951340c35deSJonathan Lemon 	/*
952252ca428SRobert Watson 	 * A previous connection in TIMEWAIT state is supposed to catch stray
953252ca428SRobert Watson 	 * or duplicate segments arriving late.  If this segment was a
9540989f56cSRobert Watson 	 * legitimate new connection attempt, the old INPCB gets removed and
955252ca428SRobert Watson 	 * we can try again to find a listening socket.
956252ca428SRobert Watson 	 *
957fa046d87SRobert Watson 	 * At this point, due to earlier optimism, we may hold only an inpcb
958fa046d87SRobert Watson 	 * lock, and not the inpcbinfo write lock.  If so, we need to try to
959fa046d87SRobert Watson 	 * acquire it, or if that fails, acquire a reference on the inpcb,
960fa046d87SRobert Watson 	 * drop all locks, acquire a global write lock, and then re-acquire
961fa046d87SRobert Watson 	 * the inpcb lock.  We may at that point discover that another thread
962fa046d87SRobert Watson 	 * has tried to free the inpcb, in which case we need to loop back
963fa046d87SRobert Watson 	 * and try to find a new inpcb to deliver to.
964fa046d87SRobert Watson 	 *
965fa046d87SRobert Watson 	 * XXXRW: It may be time to rethink timewait locking.
966340c35deSJonathan Lemon 	 */
967883e9bc4SRobert Watson relocked:
968ad71fe3cSRobert Watson 	if (inp->inp_flags & INP_TIMEWAIT) {
969fa046d87SRobert Watson 		if (ti_locked == TI_UNLOCKED) {
970ff9b006dSJulien Charbon 			if (INP_INFO_TRY_RLOCK(&V_tcbinfo) == 0) {
971252ca428SRobert Watson 				in_pcbref(inp);
972252ca428SRobert Watson 				INP_WUNLOCK(inp);
973ff9b006dSJulien Charbon 				INP_INFO_RLOCK(&V_tcbinfo);
974ff9b006dSJulien Charbon 				ti_locked = TI_RLOCKED;
975252ca428SRobert Watson 				INP_WLOCK(inp);
976fa046d87SRobert Watson 				if (in_pcbrele_wlocked(inp)) {
977252ca428SRobert Watson 					inp = NULL;
978252ca428SRobert Watson 					goto findpcb;
979252ca428SRobert Watson 				}
980f681a5fdSRobert Watson 			} else
981ff9b006dSJulien Charbon 				ti_locked = TI_RLOCKED;
982252ca428SRobert Watson 		}
983ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
984252ca428SRobert Watson 
985340c35deSJonathan Lemon 		if (thflags & TH_SYN)
986f72167f4SAndre Oppermann 			tcp_dooptions(&to, optp, optlen, TO_SYN);
9878d573cc1SAndre Oppermann 		/*
9888d573cc1SAndre Oppermann 		 * NB: tcp_twcheck unlocks the INP and frees the mbuf.
9898d573cc1SAndre Oppermann 		 */
9902104448fSAndre Oppermann 		if (tcp_twcheck(inp, &to, th, m, tlen))
991340c35deSJonathan Lemon 			goto findpcb;
992ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
9938f5a8818SKevin Lo 		return (IPPROTO_DONE);
994340c35deSJonathan Lemon 	}
995794235b7SAndre Oppermann 	/*
996794235b7SAndre Oppermann 	 * The TCPCB may no longer exist if the connection is winding
997794235b7SAndre Oppermann 	 * down or it is in the CLOSED state.  Either way we drop the
998794235b7SAndre Oppermann 	 * segment and send an appropriate response.
999794235b7SAndre Oppermann 	 */
1000df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
1001a160e630SAndre Oppermann 	if (tp == NULL || tp->t_state == TCPS_CLOSED) {
1002a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
1003a57815efSBosko Milekic 		goto dropwithreset;
100409f81a46SBosko Milekic 	}
1005df8bae1dSRodney W. Grimes 
100609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
100709fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
100809fe6320SNavdeep Parhar 		tcp_offload_input(tp, m);
100909fe6320SNavdeep Parhar 		m = NULL;	/* consumed by the TOE driver */
101009fe6320SNavdeep Parhar 		goto dropunlock;
101109fe6320SNavdeep Parhar 	}
101209fe6320SNavdeep Parhar #endif
101309fe6320SNavdeep Parhar 
1014252ca428SRobert Watson 	/*
1015252ca428SRobert Watson 	 * We've identified a valid inpcb, but it could be that we need an
1016fa046d87SRobert Watson 	 * inpcbinfo write lock but don't hold it.  In this case, attempt to
1017fa046d87SRobert Watson 	 * acquire using the same strategy as the TIMEWAIT case above.  If we
1018fa046d87SRobert Watson 	 * relock, we have to jump back to 'relocked' as the connection might
1019fa046d87SRobert Watson 	 * now be in TIMEWAIT.
1020252ca428SRobert Watson 	 */
1021fa046d87SRobert Watson #ifdef INVARIANTS
1022a7c7f2a7SJohn Baldwin 	if ((thflags & (TH_FIN | TH_RST)) != 0)
1023ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1024fa046d87SRobert Watson #endif
1025a7c7f2a7SJohn Baldwin 	if (!((tp->t_state == TCPS_ESTABLISHED && (thflags & TH_SYN) == 0) ||
1026281a0fd4SPatrick Kelsey 	      (tp->t_state == TCPS_LISTEN && (thflags & TH_SYN) &&
1027281a0fd4SPatrick Kelsey 	       !(tp->t_flags & TF_FASTOPEN)))) {
1028fa046d87SRobert Watson 		if (ti_locked == TI_UNLOCKED) {
1029ff9b006dSJulien Charbon 			if (INP_INFO_TRY_RLOCK(&V_tcbinfo) == 0) {
1030252ca428SRobert Watson 				in_pcbref(inp);
1031252ca428SRobert Watson 				INP_WUNLOCK(inp);
1032ff9b006dSJulien Charbon 				INP_INFO_RLOCK(&V_tcbinfo);
1033ff9b006dSJulien Charbon 				ti_locked = TI_RLOCKED;
1034252ca428SRobert Watson 				INP_WLOCK(inp);
1035fa046d87SRobert Watson 				if (in_pcbrele_wlocked(inp)) {
1036252ca428SRobert Watson 					inp = NULL;
1037252ca428SRobert Watson 					goto findpcb;
1038252ca428SRobert Watson 				}
1039883e9bc4SRobert Watson 				goto relocked;
1040f681a5fdSRobert Watson 			} else
1041ff9b006dSJulien Charbon 				ti_locked = TI_RLOCKED;
1042252ca428SRobert Watson 		}
1043ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1044252ca428SRobert Watson 	}
1045252ca428SRobert Watson 
1046c488362eSRobert Watson #ifdef MAC
10478501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
104830d239bcSRobert Watson 	if (mac_inpcb_check_deliver(inp, m))
1049302ce8d6SAndre Oppermann 		goto dropunlock;
1050c488362eSRobert Watson #endif
1051a557af22SRobert Watson 	so = inp->inp_socket;
1052302ce8d6SAndre Oppermann 	KASSERT(so != NULL, ("%s: so == NULL", __func__));
1053610ee2f9SDavid Greenman #ifdef TCPDEBUG
1054df8bae1dSRodney W. Grimes 	if (so->so_options & SO_DEBUG) {
1055df8bae1dSRodney W. Grimes 		ostate = tp->t_state;
105610fe523eSMaxim Konovalov #ifdef INET6
10576f697424SBjoern A. Zeeb 		if (isipv6) {
1058540e8b7eSJeffrey Hsu 			bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6));
1059d30d90dcSMaxim Konovalov 		} else
10606f697424SBjoern A. Zeeb #endif
1061540e8b7eSJeffrey Hsu 			bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
1062fb59c426SYoshinobu Inoue 		tcp_savetcp = *th;
1063df8bae1dSRodney W. Grimes 	}
1064b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */
1065db33b3e6SAndre Oppermann 	/*
1066db33b3e6SAndre Oppermann 	 * When the socket is accepting connections (the INPCB is in LISTEN
1067db33b3e6SAndre Oppermann 	 * state) we look into the SYN cache if this is a new connection
1068a7c7f2a7SJohn Baldwin 	 * attempt or the completion of a previous one.
1069db33b3e6SAndre Oppermann 	 */
1070540e8b7eSJeffrey Hsu 	if (so->so_options & SO_ACCEPTCONN) {
1071540e8b7eSJeffrey Hsu 		struct in_conninfo inc;
1072540e8b7eSJeffrey Hsu 
10737824d002SAndre Oppermann 		KASSERT(tp->t_state == TCPS_LISTEN, ("%s: so accepting but "
10747824d002SAndre Oppermann 		    "tp not listening", __func__));
10758bfb1918SAndre Oppermann 		bzero(&inc, sizeof(inc));
1076302ce8d6SAndre Oppermann #ifdef INET6
1077be2ac88cSJonathan Lemon 		if (isipv6) {
1078dcdb4371SBjoern A. Zeeb 			inc.inc_flags |= INC_ISIPV6;
1079be2ac88cSJonathan Lemon 			inc.inc6_faddr = ip6->ip6_src;
1080be2ac88cSJonathan Lemon 			inc.inc6_laddr = ip6->ip6_dst;
1081302ce8d6SAndre Oppermann 		} else
1082302ce8d6SAndre Oppermann #endif
1083302ce8d6SAndre Oppermann 		{
1084be2ac88cSJonathan Lemon 			inc.inc_faddr = ip->ip_src;
1085be2ac88cSJonathan Lemon 			inc.inc_laddr = ip->ip_dst;
1086be2ac88cSJonathan Lemon 		}
1087be2ac88cSJonathan Lemon 		inc.inc_fport = th->th_sport;
1088be2ac88cSJonathan Lemon 		inc.inc_lport = th->th_dport;
10897973fba3SJulian Elischer 		inc.inc_fibnum = so->so_fibnum;
1090be2ac88cSJonathan Lemon 
1091fb59c426SYoshinobu Inoue 		/*
10928d573cc1SAndre Oppermann 		 * Check for an existing connection attempt in syncache if
10938d573cc1SAndre Oppermann 		 * the flag is only ACK.  A successful lookup creates a new
10948d573cc1SAndre Oppermann 		 * socket appended to the listen queue in SYN_RECEIVED state.
1095fb59c426SYoshinobu Inoue 		 */
1096be2ac88cSJonathan Lemon 		if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
1097a7c7f2a7SJohn Baldwin 
1098ff9b006dSJulien Charbon 			INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1099bf6d304aSAndre Oppermann 			/*
1100bf6d304aSAndre Oppermann 			 * Parse the TCP options here because
1101bf6d304aSAndre Oppermann 			 * syncookies need access to the reflected
1102bf6d304aSAndre Oppermann 			 * timestamp.
1103bf6d304aSAndre Oppermann 			 */
1104bf6d304aSAndre Oppermann 			tcp_dooptions(&to, optp, optlen, 0);
11059fa198beSAndre Oppermann 			/*
11069fa198beSAndre Oppermann 			 * NB: syncache_expand() doesn't unlock
11079fa198beSAndre Oppermann 			 * inp and tcpinfo locks.
11089fa198beSAndre Oppermann 			 */
1109bf6d304aSAndre Oppermann 			if (!syncache_expand(&inc, &to, th, &so, m)) {
11104195b4afSPaul Traina 				/*
1111e207f800SAndre Oppermann 				 * No syncache entry or ACK was not
1112be2ac88cSJonathan Lemon 				 * for our SYN/ACK.  Send a RST.
11138d573cc1SAndre Oppermann 				 * NB: syncache did its own logging
11148d573cc1SAndre Oppermann 				 * of the failure cause.
11154195b4afSPaul Traina 				 */
1116be2ac88cSJonathan Lemon 				rstreason = BANDLIM_RST_OPENPORT;
1117be2ac88cSJonathan Lemon 				goto dropwithreset;
1118be2ac88cSJonathan Lemon 			}
1119281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
1120281a0fd4SPatrick Kelsey new_tfo_socket:
1121281a0fd4SPatrick Kelsey #endif
1122f76fcf6dSJeffrey Hsu 			if (so == NULL) {
1123be2ac88cSJonathan Lemon 				/*
1124e207f800SAndre Oppermann 				 * We completed the 3-way handshake
1125e207f800SAndre Oppermann 				 * but could not allocate a socket
1126e207f800SAndre Oppermann 				 * either due to memory shortage,
1127e207f800SAndre Oppermann 				 * listen queue length limits or
1128a160e630SAndre Oppermann 				 * global socket limits.  Send RST
1129a160e630SAndre Oppermann 				 * or wait and have the remote end
1130a160e630SAndre Oppermann 				 * retransmit the ACK for another
1131a160e630SAndre Oppermann 				 * try.
1132be2ac88cSJonathan Lemon 				 */
1133a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1134a160e630SAndre Oppermann 					log(LOG_DEBUG, "%s; %s: Listen socket: "
11358d573cc1SAndre Oppermann 					    "Socket allocation failed due to "
11368d573cc1SAndre Oppermann 					    "limits or memory shortage, %s\n",
1137ac957cd2SJulian Elischer 					    s, __func__,
1138ac957cd2SJulian Elischer 					    V_tcp_sc_rst_sock_fail ?
1139ac957cd2SJulian Elischer 					    "sending RST" : "try again");
1140603724d3SBjoern A. Zeeb 				if (V_tcp_sc_rst_sock_fail) {
1141e207f800SAndre Oppermann 					rstreason = BANDLIM_UNLIMITED;
1142e207f800SAndre Oppermann 					goto dropwithreset;
1143a160e630SAndre Oppermann 				} else
1144a160e630SAndre Oppermann 					goto dropunlock;
1145f76fcf6dSJeffrey Hsu 			}
1146be2ac88cSJonathan Lemon 			/*
1147be2ac88cSJonathan Lemon 			 * Socket is created in state SYN_RECEIVED.
11488d573cc1SAndre Oppermann 			 * Unlock the listen socket, lock the newly
11498d573cc1SAndre Oppermann 			 * created socket and update the tp variable.
1150be2ac88cSJonathan Lemon 			 */
11518501a69cSRobert Watson 			INP_WUNLOCK(inp);	/* listen socket */
1152be2ac88cSJonathan Lemon 			inp = sotoinpcb(so);
1153ff9b006dSJulien Charbon 			/*
1154ff9b006dSJulien Charbon 			 * New connection inpcb is already locked by
1155ff9b006dSJulien Charbon 			 * syncache_expand().
1156ff9b006dSJulien Charbon 			 */
1157ff9b006dSJulien Charbon 			INP_WLOCK_ASSERT(inp);
1158be2ac88cSJonathan Lemon 			tp = intotcpcb(inp);
11598d573cc1SAndre Oppermann 			KASSERT(tp->t_state == TCPS_SYN_RECEIVED,
11608d573cc1SAndre Oppermann 			    ("%s: ", __func__));
11612903309aSAttilio Rao #ifdef TCP_SIGNATURE
11622903309aSAttilio Rao 			if (sig_checked == 0)  {
11632903309aSAttilio Rao 				tcp_dooptions(&to, optp, optlen,
11642903309aSAttilio Rao 				    (thflags & TH_SYN) ? TO_SYN : 0);
11652903309aSAttilio Rao 				if (!tcp_signature_verify_input(m, off0, tlen,
11662903309aSAttilio Rao 				    optlen, &to, th, tp->t_flags)) {
11672903309aSAttilio Rao 
11682903309aSAttilio Rao 					/*
11692903309aSAttilio Rao 					 * In SYN_SENT state if it receives an
11702903309aSAttilio Rao 					 * RST, it is allowed for further
11712903309aSAttilio Rao 					 * processing.
11722903309aSAttilio Rao 					 */
11732903309aSAttilio Rao 					if ((thflags & TH_RST) == 0 ||
11742903309aSAttilio Rao 					    (tp->t_state == TCPS_SYN_SENT) == 0)
11752903309aSAttilio Rao 						goto dropunlock;
11762903309aSAttilio Rao 				}
11772903309aSAttilio Rao 				sig_checked = 1;
11782903309aSAttilio Rao 			}
11792903309aSAttilio Rao #endif
11802903309aSAttilio Rao 
1181be2ac88cSJonathan Lemon 			/*
1182302ce8d6SAndre Oppermann 			 * Process the segment and the data it
1183302ce8d6SAndre Oppermann 			 * contains.  tcp_do_segment() consumes
1184302ce8d6SAndre Oppermann 			 * the mbuf chain and unlocks the inpcb.
1185302ce8d6SAndre Oppermann 			 */
118655bceb1eSRandall Stewart 			tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen,
1187252ca428SRobert Watson 			    iptos, ti_locked);
1188603724d3SBjoern A. Zeeb 			INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
11898f5a8818SKevin Lo 			return (IPPROTO_DONE);
1190be2ac88cSJonathan Lemon 		}
1191a160e630SAndre Oppermann 		/*
11928d573cc1SAndre Oppermann 		 * Segment flag validation for new connection attempts:
11938d573cc1SAndre Oppermann 		 *
1194a160e630SAndre Oppermann 		 * Our (SYN|ACK) response was rejected.
1195a160e630SAndre Oppermann 		 * Check with syncache and remove entry to prevent
1196a160e630SAndre Oppermann 		 * retransmits.
119719bc77c5SAndre Oppermann 		 *
119819bc77c5SAndre Oppermann 		 * NB: syncache_chkrst does its own logging of failure
119919bc77c5SAndre Oppermann 		 * causes.
1200a160e630SAndre Oppermann 		 */
1201a160e630SAndre Oppermann 		if (thflags & TH_RST) {
120219bc77c5SAndre Oppermann 			syncache_chkrst(&inc, th);
1203a160e630SAndre Oppermann 			goto dropunlock;
1204a160e630SAndre Oppermann 		}
1205a160e630SAndre Oppermann 		/*
1206a160e630SAndre Oppermann 		 * We can't do anything without SYN.
1207a160e630SAndre Oppermann 		 */
1208a160e630SAndre Oppermann 		if ((thflags & TH_SYN) == 0) {
1209a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1210a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1211773673c1SAndre Oppermann 				    "SYN is missing, segment ignored\n",
12128d573cc1SAndre Oppermann 				    s, __func__);
121378b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1214a160e630SAndre Oppermann 			goto dropunlock;
1215a160e630SAndre Oppermann 		}
1216a160e630SAndre Oppermann 		/*
1217a160e630SAndre Oppermann 		 * (SYN|ACK) is bogus on a listen socket.
1218a160e630SAndre Oppermann 		 */
1219fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1220a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1221a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
12228d573cc1SAndre Oppermann 				    "SYN|ACK invalid, segment rejected\n",
12238d573cc1SAndre Oppermann 				    s, __func__);
1224a160e630SAndre Oppermann 			syncache_badack(&inc);	/* XXX: Not needed! */
122578b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1226a57815efSBosko Milekic 			rstreason = BANDLIM_RST_OPENPORT;
1227a57815efSBosko Milekic 			goto dropwithreset;
12284195b4afSPaul Traina 		}
1229a160e630SAndre Oppermann 		/*
1230a160e630SAndre Oppermann 		 * If the drop_synfin option is enabled, drop all
1231a160e630SAndre Oppermann 		 * segments with both the SYN and FIN bits set.
1232a160e630SAndre Oppermann 		 * This prevents e.g. nmap from identifying the
1233a160e630SAndre Oppermann 		 * TCP/IP stack.
1234a160e630SAndre Oppermann 		 * XXX: Poor reasoning.  nmap has other methods
1235a160e630SAndre Oppermann 		 * and is constantly refining its stack detection
1236a160e630SAndre Oppermann 		 * strategies.
12378d573cc1SAndre Oppermann 		 * XXX: This is a violation of the TCP specification
1238a160e630SAndre Oppermann 		 * and was used by RFC1644.
1239a160e630SAndre Oppermann 		 */
1240603724d3SBjoern A. Zeeb 		if ((thflags & TH_FIN) && V_drop_synfin) {
1241a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1242a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1243773673c1SAndre Oppermann 				    "SYN|FIN segment ignored (based on "
12448d573cc1SAndre Oppermann 				    "sysctl setting)\n", s, __func__);
124578b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1246302ce8d6SAndre Oppermann 			goto dropunlock;
1247ebb0cbeaSPaul Traina 		}
1248be2ac88cSJonathan Lemon 		/*
1249be2ac88cSJonathan Lemon 		 * Segment's flags are (SYN) or (SYN|FIN).
1250a160e630SAndre Oppermann 		 *
1251a160e630SAndre Oppermann 		 * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored
1252a160e630SAndre Oppermann 		 * as they do not affect the state of the TCP FSM.
1253a160e630SAndre Oppermann 		 * The data pointed to by TH_URG and th_urp is ignored.
1254be2ac88cSJonathan Lemon 		 */
1255a160e630SAndre Oppermann 		KASSERT((thflags & (TH_RST|TH_ACK)) == 0,
1256a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_RST or TH_ACK set", __func__));
1257a160e630SAndre Oppermann 		KASSERT(thflags & (TH_SYN),
1258a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_SYN not set", __func__));
125933841545SHajimu UMEMOTO #ifdef INET6
126033841545SHajimu UMEMOTO 		/*
126133841545SHajimu UMEMOTO 		 * If deprecated address is forbidden,
126233841545SHajimu UMEMOTO 		 * we do not accept SYN to deprecated interface
126333841545SHajimu UMEMOTO 		 * address to prevent any new inbound connection from
126433841545SHajimu UMEMOTO 		 * getting established.
126533841545SHajimu UMEMOTO 		 * When we do not accept SYN, we send a TCP RST,
126633841545SHajimu UMEMOTO 		 * with deprecated source address (instead of dropping
126733841545SHajimu UMEMOTO 		 * it).  We compromise it as it is much better for peer
126833841545SHajimu UMEMOTO 		 * to send a RST, and RST will be the final packet
126933841545SHajimu UMEMOTO 		 * for the exchange.
127033841545SHajimu UMEMOTO 		 *
127133841545SHajimu UMEMOTO 		 * If we do not forbid deprecated addresses, we accept
127233841545SHajimu UMEMOTO 		 * the SYN packet.  RFC2462 does not suggest dropping
127333841545SHajimu UMEMOTO 		 * SYN in this case.
127433841545SHajimu UMEMOTO 		 * If we decipher RFC2462 5.5.4, it says like this:
127533841545SHajimu UMEMOTO 		 * 1. use of deprecated addr with existing
127633841545SHajimu UMEMOTO 		 *    communication is okay - "SHOULD continue to be
127733841545SHajimu UMEMOTO 		 *    used"
127833841545SHajimu UMEMOTO 		 * 2. use of it with new communication:
127933841545SHajimu UMEMOTO 		 *   (2a) "SHOULD NOT be used if alternate address
128033841545SHajimu UMEMOTO 		 *        with sufficient scope is available"
128133841545SHajimu UMEMOTO 		 *   (2b) nothing mentioned otherwise.
128233841545SHajimu UMEMOTO 		 * Here we fall into (2b) case as we have no choice in
128333841545SHajimu UMEMOTO 		 * our source address selection - we must obey the peer.
128433841545SHajimu UMEMOTO 		 *
128533841545SHajimu UMEMOTO 		 * The wording in RFC2462 is confusing, and there are
128633841545SHajimu UMEMOTO 		 * multiple description text for deprecated address
128733841545SHajimu UMEMOTO 		 * handling - worse, they are not exactly the same.
128833841545SHajimu UMEMOTO 		 * I believe 5.5.4 is the best one, so we follow 5.5.4.
128933841545SHajimu UMEMOTO 		 */
1290603724d3SBjoern A. Zeeb 		if (isipv6 && !V_ip6_use_deprecated) {
129133841545SHajimu UMEMOTO 			struct in6_ifaddr *ia6;
129233841545SHajimu UMEMOTO 
12933e88eb90SAndrey V. Elsukov 			ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
12948c0fec80SRobert Watson 			if (ia6 != NULL &&
129533841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
12968c0fec80SRobert Watson 				ifa_free(&ia6->ia_ifa);
1297a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1298a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12998d573cc1SAndre Oppermann 					"Connection attempt to deprecated "
13008d573cc1SAndre Oppermann 					"IPv6 address rejected\n",
1301a160e630SAndre Oppermann 					s, __func__);
130233841545SHajimu UMEMOTO 				rstreason = BANDLIM_RST_OPENPORT;
130333841545SHajimu UMEMOTO 				goto dropwithreset;
130433841545SHajimu UMEMOTO 			}
130577d396fdSMaksim Yevmenkin 			if (ia6)
13068c0fec80SRobert Watson 				ifa_free(&ia6->ia_ifa);
130733841545SHajimu UMEMOTO 		}
1308b287c6c7SBjoern A. Zeeb #endif /* INET6 */
130965f28919SJesper Skriver 		/*
1310db33b3e6SAndre Oppermann 		 * Basic sanity checks on incoming SYN requests:
13118d573cc1SAndre Oppermann 		 *   Don't respond if the destination is a link layer
1312db33b3e6SAndre Oppermann 		 *	broadcast according to RFC1122 4.2.3.10, p. 104.
13138d573cc1SAndre Oppermann 		 *   If it is from this socket it must be forged.
13148d573cc1SAndre Oppermann 		 *   Don't respond if the source or destination is a
13158d573cc1SAndre Oppermann 		 *	global or subnet broad- or multicast address.
131693ec91baSCrist J. Clark 		 *   Note that it is quite possible to receive unicast
131793ec91baSCrist J. Clark 		 *	link-layer packets with a broadcast IP address. Use
131893ec91baSCrist J. Clark 		 *	in_broadcast() to find them.
1319be2ac88cSJonathan Lemon 		 */
13208d573cc1SAndre Oppermann 		if (m->m_flags & (M_BCAST|M_MCAST)) {
13218d573cc1SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
13228d573cc1SAndre Oppermann 			    log(LOG_DEBUG, "%s; %s: Listen socket: "
13238d573cc1SAndre Oppermann 				"Connection attempt from broad- or multicast "
1324773673c1SAndre Oppermann 				"link layer address ignored\n", s, __func__);
1325302ce8d6SAndre Oppermann 			goto dropunlock;
13268d573cc1SAndre Oppermann 		}
1327db33b3e6SAndre Oppermann #ifdef INET6
1328b287c6c7SBjoern A. Zeeb 		if (isipv6) {
1329db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1330a160e630SAndre Oppermann 			    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) {
1331a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1332a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13338d573cc1SAndre Oppermann 					"Connection attempt to/from self "
1334773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1335302ce8d6SAndre Oppermann 				goto dropunlock;
1336a160e630SAndre Oppermann 			}
1337be2ac88cSJonathan Lemon 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1338a160e630SAndre Oppermann 			    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
1339a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1340a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13418d573cc1SAndre Oppermann 					"Connection attempt from/to multicast "
1342773673c1SAndre Oppermann 					"address ignored\n", s, __func__);
1343302ce8d6SAndre Oppermann 				goto dropunlock;
1344a160e630SAndre Oppermann 			}
1345b287c6c7SBjoern A. Zeeb 		}
1346db33b3e6SAndre Oppermann #endif
1347b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
1348b287c6c7SBjoern A. Zeeb 		else
1349b287c6c7SBjoern A. Zeeb #endif
1350b287c6c7SBjoern A. Zeeb #ifdef INET
1351b287c6c7SBjoern A. Zeeb 		{
1352db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1353a160e630SAndre Oppermann 			    ip->ip_dst.s_addr == ip->ip_src.s_addr) {
1354a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1355a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13568d573cc1SAndre Oppermann 					"Connection attempt from/to self "
1357773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1358302ce8d6SAndre Oppermann 				goto dropunlock;
1359a160e630SAndre Oppermann 			}
1360be2ac88cSJonathan Lemon 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
1361be2ac88cSJonathan Lemon 			    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
13622ca2159fSCrist J. Clark 			    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
1363a160e630SAndre Oppermann 			    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
1364a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1365a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13668d573cc1SAndre Oppermann 					"Connection attempt from/to broad- "
1367773673c1SAndre Oppermann 					"or multicast address ignored\n",
1368a160e630SAndre Oppermann 					s, __func__);
1369302ce8d6SAndre Oppermann 				goto dropunlock;
1370c068736aSJeffrey Hsu 			}
1371a160e630SAndre Oppermann 		}
1372b287c6c7SBjoern A. Zeeb #endif
1373be2ac88cSJonathan Lemon 		/*
1374db33b3e6SAndre Oppermann 		 * SYN appears to be valid.  Create compressed TCP state
1375db33b3e6SAndre Oppermann 		 * for syncache.
1376be2ac88cSJonathan Lemon 		 */
13773c653157SHartmut Brandt #ifdef TCPDEBUG
13783c653157SHartmut Brandt 		if (so->so_options & SO_DEBUG)
13793c653157SHartmut Brandt 			tcp_trace(TA_INPUT, ostate, tp,
13803c653157SHartmut Brandt 			    (void *)tcp_saveipgen, &tcp_savetcp, 0);
13813c653157SHartmut Brandt #endif
13825d06879aSGeorge V. Neville-Neil 		TCP_PROBE3(debug__input, tp, th, mtod(m, const char *));
1383f72167f4SAndre Oppermann 		tcp_dooptions(&to, optp, optlen, TO_SYN);
1384281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
1385281a0fd4SPatrick Kelsey 		if (syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL))
1386281a0fd4SPatrick Kelsey 			goto new_tfo_socket;
1387281a0fd4SPatrick Kelsey #else
138809fe6320SNavdeep Parhar 		syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL);
1389281a0fd4SPatrick Kelsey #endif
1390be2ac88cSJonathan Lemon 		/*
13914d6e7130SAndre Oppermann 		 * Entry added to syncache and mbuf consumed.
1392a7c7f2a7SJohn Baldwin 		 * Only the listen socket is unlocked by syncache_add().
1393be2ac88cSJonathan Lemon 		 */
1394ff9b006dSJulien Charbon 		if (ti_locked == TI_RLOCKED) {
1395ff9b006dSJulien Charbon 			INP_INFO_RUNLOCK(&V_tcbinfo);
1396a7c7f2a7SJohn Baldwin 			ti_locked = TI_UNLOCKED;
1397a7c7f2a7SJohn Baldwin 		}
1398603724d3SBjoern A. Zeeb 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
13998f5a8818SKevin Lo 		return (IPPROTO_DONE);
1400982c1675SAndre Oppermann 	} else if (tp->t_state == TCPS_LISTEN) {
1401982c1675SAndre Oppermann 		/*
1402982c1675SAndre Oppermann 		 * When a listen socket is torn down the SO_ACCEPTCONN
1403982c1675SAndre Oppermann 		 * flag is removed first while connections are drained
1404982c1675SAndre Oppermann 		 * from the accept queue in a unlock/lock cycle of the
1405982c1675SAndre Oppermann 		 * ACCEPT_LOCK, opening a race condition allowing a SYN
1406982c1675SAndre Oppermann 		 * attempt go through unhandled.
1407982c1675SAndre Oppermann 		 */
1408982c1675SAndre Oppermann 		goto dropunlock;
1409f76fcf6dSJeffrey Hsu 	}
1410db33b3e6SAndre Oppermann 
14112903309aSAttilio Rao #ifdef TCP_SIGNATURE
14122903309aSAttilio Rao 	if (sig_checked == 0)  {
14132903309aSAttilio Rao 		tcp_dooptions(&to, optp, optlen,
14142903309aSAttilio Rao 		    (thflags & TH_SYN) ? TO_SYN : 0);
14152903309aSAttilio Rao 		if (!tcp_signature_verify_input(m, off0, tlen, optlen, &to,
14162903309aSAttilio Rao 		    th, tp->t_flags)) {
14172903309aSAttilio Rao 
14182903309aSAttilio Rao 			/*
14192903309aSAttilio Rao 			 * In SYN_SENT state if it receives an RST, it is
14202903309aSAttilio Rao 			 * allowed for further processing.
14212903309aSAttilio Rao 			 */
14222903309aSAttilio Rao 			if ((thflags & TH_RST) == 0 ||
14232903309aSAttilio Rao 			    (tp->t_state == TCPS_SYN_SENT) == 0)
14242903309aSAttilio Rao 				goto dropunlock;
14252903309aSAttilio Rao 		}
14262903309aSAttilio Rao 		sig_checked = 1;
14272903309aSAttilio Rao 	}
14282903309aSAttilio Rao #endif
14292903309aSAttilio Rao 
1430fa22ce15SAdrian Chadd 	TCP_PROBE5(receive, NULL, tp, mtod(m, const char *), tp, th);
143157f60867SMark Johnston 
1432302ce8d6SAndre Oppermann 	/*
14338d573cc1SAndre Oppermann 	 * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later
14348d573cc1SAndre Oppermann 	 * state.  tcp_do_segment() always consumes the mbuf chain, unlocks
14358d573cc1SAndre Oppermann 	 * the inpcb, and unlocks pcbinfo.
1436302ce8d6SAndre Oppermann 	 */
143755bceb1eSRandall Stewart 	tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos, ti_locked);
1438603724d3SBjoern A. Zeeb 	INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
14398f5a8818SKevin Lo 	return (IPPROTO_DONE);
1440be2ac88cSJonathan Lemon 
1441302ce8d6SAndre Oppermann dropwithreset:
1442fa22ce15SAdrian Chadd 	TCP_PROBE5(receive, NULL, tp, mtod(m, const char *), tp, th);
144357f60867SMark Johnston 
1444ff9b006dSJulien Charbon 	if (ti_locked == TI_RLOCKED) {
1445ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
1446252ca428SRobert Watson 		ti_locked = TI_UNLOCKED;
1447fa046d87SRobert Watson 	}
1448fa046d87SRobert Watson #ifdef INVARIANTS
1449fa046d87SRobert Watson 	else {
1450fa046d87SRobert Watson 		KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropwithreset "
1451fa046d87SRobert Watson 		    "ti_locked: %d", __func__, ti_locked));
1452fa046d87SRobert Watson 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1453fa046d87SRobert Watson 	}
1454fa046d87SRobert Watson #endif
1455014ea782SRobert Watson 
1456014ea782SRobert Watson 	if (inp != NULL) {
1457302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
1458014ea782SRobert Watson 		INP_WUNLOCK(inp);
1459dd8ac7f9SRobert Watson 	} else
1460014ea782SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
1461302ce8d6SAndre Oppermann 	m = NULL;	/* mbuf chain got consumed. */
1462014ea782SRobert Watson 	goto drop;
1463014ea782SRobert Watson 
1464302ce8d6SAndre Oppermann dropunlock:
146557f60867SMark Johnston 	if (m != NULL)
1466fa22ce15SAdrian Chadd 		TCP_PROBE5(receive, NULL, tp, mtod(m, const char *), tp, th);
146757f60867SMark Johnston 
1468ff9b006dSJulien Charbon 	if (ti_locked == TI_RLOCKED) {
1469ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
1470252ca428SRobert Watson 		ti_locked = TI_UNLOCKED;
1471fa046d87SRobert Watson 	}
1472fa046d87SRobert Watson #ifdef INVARIANTS
1473fa046d87SRobert Watson 	else {
1474fa046d87SRobert Watson 		KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropunlock "
1475fa046d87SRobert Watson 		    "ti_locked: %d", __func__, ti_locked));
1476fa046d87SRobert Watson 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1477fa046d87SRobert Watson 	}
1478fa046d87SRobert Watson #endif
1479252ca428SRobert Watson 
14809fa198beSAndre Oppermann 	if (inp != NULL)
14818501a69cSRobert Watson 		INP_WUNLOCK(inp);
1482014ea782SRobert Watson 
1483302ce8d6SAndre Oppermann drop:
1484603724d3SBjoern A. Zeeb 	INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1485a160e630SAndre Oppermann 	if (s != NULL)
1486a160e630SAndre Oppermann 		free(s, M_TCPLOG);
1487302ce8d6SAndre Oppermann 	if (m != NULL)
1488302ce8d6SAndre Oppermann 		m_freem(m);
14898f5a8818SKevin Lo 	return (IPPROTO_DONE);
1490302ce8d6SAndre Oppermann }
1491302ce8d6SAndre Oppermann 
149255bceb1eSRandall Stewart void
1493302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so,
1494252ca428SRobert Watson     struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos,
1495252ca428SRobert Watson     int ti_locked)
1496302ce8d6SAndre Oppermann {
1497021eaf79SHiren Panchasara 	int thflags, acked, ourfinisacked, needoutput = 0, sack_changed;
1498302ce8d6SAndre Oppermann 	int rstreason, todrop, win;
1499*3ac12506SJonathan T. Looney 	uint32_t tiwin;
15004b7b743cSLawrence Stewart 	uint16_t nsegs;
150107dacf03SAndre Oppermann 	char *s;
150207dacf03SAndre Oppermann 	struct in_conninfo *inc;
1503c11a15bfSGleb Smirnoff 	struct mbuf *mfree;
1504302ce8d6SAndre Oppermann 	struct tcpopt to;
1505281a0fd4SPatrick Kelsey 	int tfo_syn;
1506302ce8d6SAndre Oppermann 
150714739780SMaxim Konovalov #ifdef TCPDEBUG
150814739780SMaxim Konovalov 	/*
150914739780SMaxim Konovalov 	 * The size of tcp_saveipgen must be the size of the max ip header,
151014739780SMaxim Konovalov 	 * now IPv6.
151114739780SMaxim Konovalov 	 */
151214739780SMaxim Konovalov 	u_char tcp_saveipgen[IP6_HDR_LEN];
151314739780SMaxim Konovalov 	struct tcphdr tcp_savetcp;
151414739780SMaxim Konovalov 	short ostate = 0;
151514739780SMaxim Konovalov #endif
1516302ce8d6SAndre Oppermann 	thflags = th->th_flags;
151707dacf03SAndre Oppermann 	inc = &tp->t_inpcb->inp_inc;
1518d64a46eaSLawrence Stewart 	tp->sackhint.last_sack_ack = 0;
1519021eaf79SHiren Panchasara 	sack_changed = 0;
15204b7b743cSLawrence Stewart 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
1521302ce8d6SAndre Oppermann 
1522252ca428SRobert Watson 	/*
1523252ca428SRobert Watson 	 * If this is either a state-changing packet or current state isn't
1524252ca428SRobert Watson 	 * established, we require a write lock on tcbinfo.  Otherwise, we
15250989f56cSRobert Watson 	 * allow the tcbinfo to be in either alocked or unlocked, as the
15260989f56cSRobert Watson 	 * caller may have unnecessarily acquired a write lock due to a race.
1527252ca428SRobert Watson 	 */
1528252ca428SRobert Watson 	if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 ||
1529252ca428SRobert Watson 	    tp->t_state != TCPS_ESTABLISHED) {
1530ff9b006dSJulien Charbon 		KASSERT(ti_locked == TI_RLOCKED, ("%s ti_locked %d for "
1531252ca428SRobert Watson 		    "SYN/FIN/RST/!EST", __func__, ti_locked));
1532ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1533252ca428SRobert Watson 	} else {
1534252ca428SRobert Watson #ifdef INVARIANTS
1535ff9b006dSJulien Charbon 		if (ti_locked == TI_RLOCKED)
1536ff9b006dSJulien Charbon 			INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1537fa046d87SRobert Watson 		else {
1538fa046d87SRobert Watson 			KASSERT(ti_locked == TI_UNLOCKED, ("%s: EST "
1539fa046d87SRobert Watson 			    "ti_locked: %d", __func__, ti_locked));
1540fa046d87SRobert Watson 			INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1541fa046d87SRobert Watson 		}
1542252ca428SRobert Watson #endif
1543252ca428SRobert Watson 	}
15448501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1545679d9708SAndre Oppermann 	KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
1546679d9708SAndre Oppermann 	    __func__));
1547679d9708SAndre Oppermann 	KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
1548679d9708SAndre Oppermann 	    __func__));
1549df8bae1dSRodney W. Grimes 
155086a996e6SHiren Panchasara #ifdef TCPPCAP
155186a996e6SHiren Panchasara 	/* Save segment, if requested. */
155286a996e6SHiren Panchasara 	tcp_pcap_add(th, m, &(tp->t_inpkts));
155386a996e6SHiren Panchasara #endif
155486a996e6SHiren Panchasara 
1555df8bae1dSRodney W. Grimes 	/*
1556df8bae1dSRodney W. Grimes 	 * Segment received on connection.
1557df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
1558e8949f74SAndre Oppermann 	 * XXX: This should be done after segment
1559e8949f74SAndre Oppermann 	 * validation to ignore broken/spoofed segs.
1560df8bae1dSRodney W. Grimes 	 */
15619b8b58e0SJonathan Lemon 	tp->t_rcvtime = ticks;
15627ff19458SPaul Traina 	if (TCPS_HAVEESTABLISHED(tp->t_state))
15639077f387SGleb Smirnoff 		tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp));
1564df8bae1dSRodney W. Grimes 
1565df8bae1dSRodney W. Grimes 	/*
1566c52102ddSHiren Panchasara 	 * Scale up the window into a 32-bit value.
1567e8949f74SAndre Oppermann 	 * For the SYN_SENT state the scale is zero.
1568464fcfbcSAndre Oppermann 	 */
1569464fcfbcSAndre Oppermann 	tiwin = th->th_win << tp->snd_scale;
1570464fcfbcSAndre Oppermann 
1571464fcfbcSAndre Oppermann 	/*
1572f2512ba1SRui Paulo 	 * TCP ECN processing.
1573f2512ba1SRui Paulo 	 */
1574f2512ba1SRui Paulo 	if (tp->t_flags & TF_ECN_PERMIT) {
15759c251892SRui Paulo 		if (thflags & TH_CWR)
15769c251892SRui Paulo 			tp->t_flags &= ~TF_ECN_SND_ECE;
1577f2512ba1SRui Paulo 		switch (iptos & IPTOS_ECN_MASK) {
1578f2512ba1SRui Paulo 		case IPTOS_ECN_CE:
1579f2512ba1SRui Paulo 			tp->t_flags |= TF_ECN_SND_ECE;
158078b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ce);
1581f2512ba1SRui Paulo 			break;
1582f2512ba1SRui Paulo 		case IPTOS_ECN_ECT0:
158378b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ect0);
1584f2512ba1SRui Paulo 			break;
1585f2512ba1SRui Paulo 		case IPTOS_ECN_ECT1:
158678b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ect1);
1587f2512ba1SRui Paulo 			break;
1588f2512ba1SRui Paulo 		}
158964807b30SHiren Panchasara 
159064807b30SHiren Panchasara 		/* Process a packet differently from RFC3168. */
159164807b30SHiren Panchasara 		cc_ecnpkt_handler(tp, th, iptos);
159264807b30SHiren Panchasara 
1593dbc42409SLawrence Stewart 		/* Congestion experienced. */
1594dbc42409SLawrence Stewart 		if (thflags & TH_ECE) {
1595dbc42409SLawrence Stewart 			cc_cong_signal(tp, th, CC_ECN);
1596f2512ba1SRui Paulo 		}
1597f2512ba1SRui Paulo 	}
1598f2512ba1SRui Paulo 
1599f2512ba1SRui Paulo 	/*
1600f72167f4SAndre Oppermann 	 * Parse options on any incoming segment.
1601f72167f4SAndre Oppermann 	 */
1602302ce8d6SAndre Oppermann 	tcp_dooptions(&to, (u_char *)(th + 1),
1603302ce8d6SAndre Oppermann 	    (th->th_off << 2) - sizeof(struct tcphdr),
1604302ce8d6SAndre Oppermann 	    (thflags & TH_SYN) ? TO_SYN : 0);
1605f72167f4SAndre Oppermann 
1606f72167f4SAndre Oppermann 	/*
1607f72167f4SAndre Oppermann 	 * If echoed timestamp is later than the current time,
1608bf6d304aSAndre Oppermann 	 * fall back to non RFC1323 RTT calculation.  Normalize
1609bf6d304aSAndre Oppermann 	 * timestamp if syncookies were used when this connection
1610bf6d304aSAndre Oppermann 	 * was established.
1611f72167f4SAndre Oppermann 	 */
1612bf6d304aSAndre Oppermann 	if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
1613e16fa5caSJohn-Mark Gurney 		to.to_tsecr -= tp->ts_offset;
1614d8951c8aSBjoern A. Zeeb 		if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks()))
1615f72167f4SAndre Oppermann 			to.to_tsecr = 0;
1616bf6d304aSAndre Oppermann 	}
161707dacf03SAndre Oppermann 	/*
161807dacf03SAndre Oppermann 	 * If timestamps were negotiated during SYN/ACK they should
161907dacf03SAndre Oppermann 	 * appear on every segment during this session and vice versa.
162007dacf03SAndre Oppermann 	 */
162107dacf03SAndre Oppermann 	if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) {
162207dacf03SAndre Oppermann 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
162307dacf03SAndre Oppermann 			log(LOG_DEBUG, "%s; %s: Timestamp missing, "
162407dacf03SAndre Oppermann 			    "no action\n", s, __func__);
162507dacf03SAndre Oppermann 			free(s, M_TCPLOG);
162607dacf03SAndre Oppermann 		}
162707dacf03SAndre Oppermann 	}
162807dacf03SAndre Oppermann 	if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) {
162907dacf03SAndre Oppermann 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
163007dacf03SAndre Oppermann 			log(LOG_DEBUG, "%s; %s: Timestamp not expected, "
163107dacf03SAndre Oppermann 			    "no action\n", s, __func__);
163207dacf03SAndre Oppermann 			free(s, M_TCPLOG);
163307dacf03SAndre Oppermann 		}
163407dacf03SAndre Oppermann 	}
1635f72167f4SAndre Oppermann 
1636f72167f4SAndre Oppermann 	/*
163797d8d152SAndre Oppermann 	 * Process options only when we get SYN/ACK back. The SYN case
163897d8d152SAndre Oppermann 	 * for incoming connections is handled in tcp_syncache.
16395396d0f8SAndre Oppermann 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN>
16405396d0f8SAndre Oppermann 	 * or <SYN,ACK>) segment itself is never scaled.
164197d8d152SAndre Oppermann 	 * XXX this is traditional behavior, may need to be cleaned up.
1642df8bae1dSRodney W. Grimes 	 */
16430a389eabSSimon L. B. Nielsen 	if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
1644464fcfbcSAndre Oppermann 		if ((to.to_flags & TOF_SCALE) &&
1645464fcfbcSAndre Oppermann 		    (tp->t_flags & TF_REQ_SCALE)) {
1646be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_SCALE;
164702a1a643SAndre Oppermann 			tp->snd_scale = to.to_wscale;
1648be2ac88cSJonathan Lemon 		}
16495396d0f8SAndre Oppermann 		/*
16505396d0f8SAndre Oppermann 		 * Initial send window.  It will be updated with
16515396d0f8SAndre Oppermann 		 * the next incoming segment to the scaled value.
16525396d0f8SAndre Oppermann 		 */
16535396d0f8SAndre Oppermann 		tp->snd_wnd = th->th_win;
1654be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_TS) {
1655be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_TSTMP;
1656be2ac88cSJonathan Lemon 			tp->ts_recent = to.to_tsval;
1657d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
1658be2ac88cSJonathan Lemon 		}
1659be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_MSS)
1660be2ac88cSJonathan Lemon 			tcp_mss(tp, to.to_mss);
16613529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
16623529149eSAndre Oppermann 		    (to.to_flags & TOF_SACKPERM) == 0)
16633529149eSAndre Oppermann 			tp->t_flags &= ~TF_SACK_PERMIT;
16646d90faf3SPaul Saab 	}
16656d90faf3SPaul Saab 
1666df8bae1dSRodney W. Grimes 	/*
1667df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
1668df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
1669df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
1670df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
1671df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
1672df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
1673df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
1674df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
1675df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
1676df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
1677df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
1678df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
1679a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
1680c5ad39b9SAndre Oppermann 	 * Since we check for TCPS_ESTABLISHED first, it can only
1681a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
1682df8bae1dSRodney W. Grimes 	 */
1683df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
1684c5ad39b9SAndre Oppermann 	    th->th_seq == tp->rcv_nxt &&
1685fb59c426SYoshinobu Inoue 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1686c5ad39b9SAndre Oppermann 	    tp->snd_nxt == tp->snd_max &&
1687c5ad39b9SAndre Oppermann 	    tiwin && tiwin == tp->snd_wnd &&
1688a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
16894741bfcbSPatrick Kelsey 	    LIST_EMPTY(&tp->t_segq) &&
16904741bfcbSPatrick Kelsey 	    ((to.to_flags & TOF_TS) == 0 ||
1691c5ad39b9SAndre Oppermann 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) {
1692df8bae1dSRodney W. Grimes 
1693df8bae1dSRodney W. Grimes 		/*
1694df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
1695df8bae1dSRodney W. Grimes 		 * record the timestamp.
1696a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
1697a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1698df8bae1dSRodney W. Grimes 		 */
1699be2ac88cSJonathan Lemon 		if ((to.to_flags & TOF_TS) != 0 &&
1700fb59c426SYoshinobu Inoue 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1701d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
1702a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
1703df8bae1dSRodney W. Grimes 		}
1704df8bae1dSRodney W. Grimes 
1705fb59c426SYoshinobu Inoue 		if (tlen == 0) {
1706fb59c426SYoshinobu Inoue 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
1707fb59c426SYoshinobu Inoue 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
1708dbc42409SLawrence Stewart 			    !IN_RECOVERY(tp->t_flags) &&
1709fc30a251SAndre Oppermann 			    (to.to_flags & TOF_SACK) == 0 &&
1710dbc42409SLawrence Stewart 			    TAILQ_EMPTY(&tp->snd_holes)) {
1711df8bae1dSRodney W. Grimes 				/*
1712e8949f74SAndre Oppermann 				 * This is a pure ack for outstanding data.
1713df8bae1dSRodney W. Grimes 				 */
1714ff9b006dSJulien Charbon 				if (ti_locked == TI_RLOCKED)
1715ff9b006dSJulien Charbon 					INP_INFO_RUNLOCK(&V_tcbinfo);
1716252ca428SRobert Watson 				ti_locked = TI_UNLOCKED;
1717252ca428SRobert Watson 
171878b50714SRobert Watson 				TCPSTAT_INC(tcps_predack);
1719252ca428SRobert Watson 
17209b8b58e0SJonathan Lemon 				/*
1721e8949f74SAndre Oppermann 				 * "bad retransmit" recovery.
17229b8b58e0SJonathan Lemon 				 */
17239b8b58e0SJonathan Lemon 				if (tp->t_rxtshift == 1 &&
1724672dc4aeSJohn Baldwin 				    tp->t_flags & TF_PREVVALID &&
17256dfb8b31SJohn Baldwin 				    (int)(ticks - tp->t_badrxtwin) < 0) {
1726dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_RTO_ERR);
17279b8b58e0SJonathan Lemon 				}
1728fa55172bSMatthew Dillon 
1729fa55172bSMatthew Dillon 				/*
1730fa55172bSMatthew Dillon 				 * Recalculate the transmit timer / rtt.
1731fa55172bSMatthew Dillon 				 *
1732fa55172bSMatthew Dillon 				 * Some boxes send broken timestamp replies
1733fa55172bSMatthew Dillon 				 * during the SYN+ACK phase, ignore
1734fa55172bSMatthew Dillon 				 * timestamps of 0 or we could calculate a
1735fa55172bSMatthew Dillon 				 * huge RTT and blow up the retransmit timer.
1736fa55172bSMatthew Dillon 				 */
1737fa55172bSMatthew Dillon 				if ((to.to_flags & TOF_TS) != 0 &&
1738fa55172bSMatthew Dillon 				    to.to_tsecr) {
1739*3ac12506SJonathan T. Looney 					uint32_t t;
1740d8951c8aSBjoern A. Zeeb 
1741d8951c8aSBjoern A. Zeeb 					t = tcp_ts_getticks() - to.to_tsecr;
1742d8951c8aSBjoern A. Zeeb 					if (!tp->t_rttlow || tp->t_rttlow > t)
1743d8951c8aSBjoern A. Zeeb 						tp->t_rttlow = t;
1744a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
1745d8951c8aSBjoern A. Zeeb 					    TCP_TS_TO_TICKS(t) + 1);
1746fa55172bSMatthew Dillon 				} else if (tp->t_rtttime &&
1747fa55172bSMatthew Dillon 				    SEQ_GT(th->th_ack, tp->t_rtseq)) {
1748eaf80179SAndre Oppermann 					if (!tp->t_rttlow ||
1749eaf80179SAndre Oppermann 					    tp->t_rttlow > ticks - tp->t_rtttime)
1750eaf80179SAndre Oppermann 						tp->t_rttlow = ticks - tp->t_rtttime;
1751c068736aSJeffrey Hsu 					tcp_xmit_timer(tp,
1752c068736aSJeffrey Hsu 							ticks - tp->t_rtttime);
1753fa55172bSMatthew Dillon 				}
1754dbc42409SLawrence Stewart 				acked = BYTES_THIS_ACK(tp, th);
175539bc9de5SLawrence Stewart 
175639bc9de5SLawrence Stewart 				/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
175739bc9de5SLawrence Stewart 				hhook_run_tcp_est_in(tp, th, &to);
175839bc9de5SLawrence Stewart 
17594b7b743cSLawrence Stewart 				TCPSTAT_ADD(tcps_rcvackpack, nsegs);
176078b50714SRobert Watson 				TCPSTAT_ADD(tcps_rcvackbyte, acked);
1761df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
17629d11646dSJeffrey Hsu 				if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
17639d11646dSJeffrey Hsu 				    SEQ_LEQ(th->th_ack, tp->snd_recover))
17649d11646dSJeffrey Hsu 					tp->snd_recover = th->th_ack - 1;
1765dbc42409SLawrence Stewart 
1766dbc42409SLawrence Stewart 				/*
1767dbc42409SLawrence Stewart 				 * Let the congestion control algorithm update
1768dbc42409SLawrence Stewart 				 * congestion control related information. This
1769dbc42409SLawrence Stewart 				 * typically means increasing the congestion
1770dbc42409SLawrence Stewart 				 * window.
1771dbc42409SLawrence Stewart 				 */
17724b7b743cSLawrence Stewart 				cc_ack_received(tp, th, nsegs, CC_ACK);
1773dbc42409SLawrence Stewart 
17749d11646dSJeffrey Hsu 				tp->snd_una = th->th_ack;
17751645d090SJeff Roberson 				/*
1776e8949f74SAndre Oppermann 				 * Pull snd_wl2 up to prevent seq wrap relative
17771645d090SJeff Roberson 				 * to th_ack.
17781645d090SJeff Roberson 				 */
1779cb942153SJeffrey Hsu 				tp->snd_wl2 = th->th_ack;
1780b5addd85SJeffrey Hsu 				tp->t_dupacks = 0;
1781df8bae1dSRodney W. Grimes 				m_freem(m);
1782df8bae1dSRodney W. Grimes 
1783df8bae1dSRodney W. Grimes 				/*
1784df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
1785df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
1786df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
1787df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
1788df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
1789df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
1790df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
1791e8949f74SAndre Oppermann 				 */
17923c653157SHartmut Brandt #ifdef TCPDEBUG
17933c653157SHartmut Brandt 				if (so->so_options & SO_DEBUG)
17943c653157SHartmut Brandt 					tcp_trace(TA_INPUT, ostate, tp,
17953c653157SHartmut Brandt 					    (void *)tcp_saveipgen,
17963c653157SHartmut Brandt 					    &tcp_savetcp, 0);
17973c653157SHartmut Brandt #endif
17985d06879aSGeorge V. Neville-Neil 				TCP_PROBE3(debug__input, tp, th,
17995d06879aSGeorge V. Neville-Neil 					mtod(m, const char *));
1800df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
1801b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
1802b8152ba7SAndre Oppermann 				else if (!tcp_timer_active(tp, TT_PERSIST))
1803b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT,
1804b8152ba7SAndre Oppermann 						      tp->t_rxtcur);
1805df8bae1dSRodney W. Grimes 				sowwakeup(so);
1806cfa6009eSGleb Smirnoff 				if (sbavail(&so->so_snd))
180755bceb1eSRandall Stewart 					(void) tp->t_fb->tfb_tcp_output(tp);
1808a14c749fSJonathan Lemon 				goto check_delack;
1809df8bae1dSRodney W. Grimes 			}
1810fb59c426SYoshinobu Inoue 		} else if (th->th_ack == tp->snd_una &&
1811fb59c426SYoshinobu Inoue 		    tlen <= sbspace(&so->so_rcv)) {
18126741ecf5SAndre Oppermann 			int newsize = 0;	/* automatic sockbuf scaling */
18136741ecf5SAndre Oppermann 
1814df8bae1dSRodney W. Grimes 			/*
1815252ca428SRobert Watson 			 * This is a pure, in-sequence data packet with
1816252ca428SRobert Watson 			 * nothing on the reassembly queue and we have enough
1817252ca428SRobert Watson 			 * buffer space to take it.
1818df8bae1dSRodney W. Grimes 			 */
1819ff9b006dSJulien Charbon 			if (ti_locked == TI_RLOCKED)
1820ff9b006dSJulien Charbon 				INP_INFO_RUNLOCK(&V_tcbinfo);
1821252ca428SRobert Watson 			ti_locked = TI_UNLOCKED;
1822252ca428SRobert Watson 
18236d90faf3SPaul Saab 			/* Clean receiver SACK report if present */
18243529149eSAndre Oppermann 			if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks)
18256d90faf3SPaul Saab 				tcp_clean_sackreport(tp);
182678b50714SRobert Watson 			TCPSTAT_INC(tcps_preddat);
1827fb59c426SYoshinobu Inoue 			tp->rcv_nxt += tlen;
18281645d090SJeff Roberson 			/*
18291645d090SJeff Roberson 			 * Pull snd_wl1 up to prevent seq wrap relative to
18301645d090SJeff Roberson 			 * th_seq.
18311645d090SJeff Roberson 			 */
183260ee3bb2SAndre Oppermann 			tp->snd_wl1 = th->th_seq;
18331645d090SJeff Roberson 			/*
18341645d090SJeff Roberson 			 * Pull rcv_up up to prevent seq wrap relative to
18351645d090SJeff Roberson 			 * rcv_nxt.
18361645d090SJeff Roberson 			 */
18371645d090SJeff Roberson 			tp->rcv_up = tp->rcv_nxt;
18384b7b743cSLawrence Stewart 			TCPSTAT_ADD(tcps_rcvpack, nsegs);
183978b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
18403c653157SHartmut Brandt #ifdef TCPDEBUG
18413c653157SHartmut Brandt 			if (so->so_options & SO_DEBUG)
18423c653157SHartmut Brandt 				tcp_trace(TA_INPUT, ostate, tp,
18433c653157SHartmut Brandt 				    (void *)tcp_saveipgen, &tcp_savetcp, 0);
18443c653157SHartmut Brandt #endif
18455d06879aSGeorge V. Neville-Neil 			TCP_PROBE3(debug__input, tp, th, mtod(m, const char *));
18465d06879aSGeorge V. Neville-Neil 
18476741ecf5SAndre Oppermann 		/*
18486741ecf5SAndre Oppermann 		 * Automatic sizing of receive socket buffer.  Often the send
18496741ecf5SAndre Oppermann 		 * buffer size is not optimally adjusted to the actual network
18506741ecf5SAndre Oppermann 		 * conditions at hand (delay bandwidth product).  Setting the
18516741ecf5SAndre Oppermann 		 * buffer size too small limits throughput on links with high
18526741ecf5SAndre Oppermann 		 * bandwidth and high delay (eg. trans-continental/oceanic links).
18536741ecf5SAndre Oppermann 		 *
18546741ecf5SAndre Oppermann 		 * On the receive side the socket buffer memory is only rarely
18556741ecf5SAndre Oppermann 		 * used to any significant extent.  This allows us to be much
18566741ecf5SAndre Oppermann 		 * more aggressive in scaling the receive socket buffer.  For
18576741ecf5SAndre Oppermann 		 * the case that the buffer space is actually used to a large
18586741ecf5SAndre Oppermann 		 * extent and we run out of kernel memory we can simply drop
18596741ecf5SAndre Oppermann 		 * the new segments; TCP on the sender will just retransmit it
18606741ecf5SAndre Oppermann 		 * later.  Setting the buffer size too big may only consume too
18616741ecf5SAndre Oppermann 		 * much kernel memory if the application doesn't read() from
18626741ecf5SAndre Oppermann 		 * the socket or packet loss or reordering makes use of the
18636741ecf5SAndre Oppermann 		 * reassembly queue.
18646741ecf5SAndre Oppermann 		 *
18656741ecf5SAndre Oppermann 		 * The criteria to step up the receive buffer one notch are:
1866f7469d3eSHiren Panchasara 		 *  1. Application has not set receive buffer size with
1867f7469d3eSHiren Panchasara 		 *     SO_RCVBUF. Setting SO_RCVBUF clears SB_AUTOSIZE.
1868f7469d3eSHiren Panchasara 		 *  2. the number of bytes received during the time it takes
18696741ecf5SAndre Oppermann 		 *     one timestamp to be reflected back to us (the RTT);
1870f7469d3eSHiren Panchasara 		 *  3. received bytes per RTT is within seven eighth of the
18716741ecf5SAndre Oppermann 		 *     current socket buffer size;
1872f7469d3eSHiren Panchasara 		 *  4. receive buffer size has not hit maximal automatic size;
18736741ecf5SAndre Oppermann 		 *
18746741ecf5SAndre Oppermann 		 * This algorithm does one step per RTT at most and only if
18756741ecf5SAndre Oppermann 		 * we receive a bulk stream w/o packet losses or reorderings.
18766741ecf5SAndre Oppermann 		 * Shrinking the buffer during idle times is not necessary as
18776741ecf5SAndre Oppermann 		 * it doesn't consume any memory when idle.
18786741ecf5SAndre Oppermann 		 *
18796741ecf5SAndre Oppermann 		 * TODO: Only step up if the application is actually serving
18806741ecf5SAndre Oppermann 		 * the buffer to better manage the socket buffer resources.
1881df8bae1dSRodney W. Grimes 		 */
1882603724d3SBjoern A. Zeeb 			if (V_tcp_do_autorcvbuf &&
1883d57724fdSPatrick Kelsey 			    (to.to_flags & TOF_TS) &&
18846741ecf5SAndre Oppermann 			    to.to_tsecr &&
18856741ecf5SAndre Oppermann 			    (so->so_rcv.sb_flags & SB_AUTOSIZE)) {
18868502ec25SAndre Oppermann 				if (TSTMP_GT(to.to_tsecr, tp->rfbuf_ts) &&
18876741ecf5SAndre Oppermann 				    to.to_tsecr - tp->rfbuf_ts < hz) {
18886741ecf5SAndre Oppermann 					if (tp->rfbuf_cnt >
18896741ecf5SAndre Oppermann 					    (so->so_rcv.sb_hiwat / 8 * 7) &&
18906741ecf5SAndre Oppermann 					    so->so_rcv.sb_hiwat <
1891603724d3SBjoern A. Zeeb 					    V_tcp_autorcvbuf_max) {
18926741ecf5SAndre Oppermann 						newsize =
18936741ecf5SAndre Oppermann 						    min(so->so_rcv.sb_hiwat +
1894603724d3SBjoern A. Zeeb 						    V_tcp_autorcvbuf_inc,
1895603724d3SBjoern A. Zeeb 						    V_tcp_autorcvbuf_max);
18966741ecf5SAndre Oppermann 					}
18976741ecf5SAndre Oppermann 					/* Start over with next RTT. */
18986741ecf5SAndre Oppermann 					tp->rfbuf_ts = 0;
18996741ecf5SAndre Oppermann 					tp->rfbuf_cnt = 0;
19006741ecf5SAndre Oppermann 				} else
19016741ecf5SAndre Oppermann 					tp->rfbuf_cnt += tlen;	/* add up */
19026741ecf5SAndre Oppermann 			}
19036741ecf5SAndre Oppermann 
19046741ecf5SAndre Oppermann 			/* Add data to socket buffer. */
19051e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
1906c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1907c1c36a2cSMike Silbersack 				m_freem(m);
1908c1c36a2cSMike Silbersack 			} else {
19096741ecf5SAndre Oppermann 				/*
19106741ecf5SAndre Oppermann 				 * Set new socket buffer size.
19116741ecf5SAndre Oppermann 				 * Give up when limit is reached.
19126741ecf5SAndre Oppermann 				 */
19136741ecf5SAndre Oppermann 				if (newsize)
19146741ecf5SAndre Oppermann 					if (!sbreserve_locked(&so->so_rcv,
19156c8286e4SRobert Watson 					    newsize, so, NULL))
19166741ecf5SAndre Oppermann 						so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
1917fb59c426SYoshinobu Inoue 				m_adj(m, drop_hdrlen);	/* delayed header drop */
1918651e4e6aSGleb Smirnoff 				sbappendstream_locked(&so->so_rcv, m, 0);
1919c1c36a2cSMike Silbersack 			}
1920e8949f74SAndre Oppermann 			/* NB: sorwakeup_locked() does an implicit unlock. */
19211e4d7da7SRobert Watson 			sorwakeup_locked(so);
1922c1e5a6e5SAndre Oppermann 			if (DELAY_ACK(tp, tlen)) {
19233bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
1924f498eeeeSDavid Greenman 			} else {
1925e612a582SDavid Greenman 				tp->t_flags |= TF_ACKNOW;
192655bceb1eSRandall Stewart 				tp->t_fb->tfb_tcp_output(tp);
1927e612a582SDavid Greenman 			}
1928a14c749fSJonathan Lemon 			goto check_delack;
1929df8bae1dSRodney W. Grimes 		}
1930df8bae1dSRodney W. Grimes 	}
1931df8bae1dSRodney W. Grimes 
1932df8bae1dSRodney W. Grimes 	/*
1933df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
1934df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
1935df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
1936df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
1937df8bae1dSRodney W. Grimes 	 */
1938df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
1939df8bae1dSRodney W. Grimes 	if (win < 0)
1940df8bae1dSRodney W. Grimes 		win = 0;
194166e39adcSJohn Polstra 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1942df8bae1dSRodney W. Grimes 
19436741ecf5SAndre Oppermann 	/* Reset receive buffer auto scaling when not in bulk receive mode. */
19446741ecf5SAndre Oppermann 	tp->rfbuf_ts = 0;
19456741ecf5SAndre Oppermann 	tp->rfbuf_cnt = 0;
19466741ecf5SAndre Oppermann 
1947df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1948df8bae1dSRodney W. Grimes 
1949df8bae1dSRodney W. Grimes 	/*
1950764d8cefSBill Fenner 	 * If the state is SYN_RECEIVED:
1951764d8cefSBill Fenner 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
1952764d8cefSBill Fenner 	 */
1953764d8cefSBill Fenner 	case TCPS_SYN_RECEIVED:
1954fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1955fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1956a57815efSBosko Milekic 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1957a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
1958a57815efSBosko Milekic 				goto dropwithreset;
1959a57815efSBosko Milekic 		}
1960281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
1961281a0fd4SPatrick Kelsey 		if (tp->t_flags & TF_FASTOPEN) {
1962281a0fd4SPatrick Kelsey 			/*
1963281a0fd4SPatrick Kelsey 			 * When a TFO connection is in SYN_RECEIVED, the
1964281a0fd4SPatrick Kelsey 			 * only valid packets are the initial SYN, a
1965281a0fd4SPatrick Kelsey 			 * retransmit/copy of the initial SYN (possibly with
1966281a0fd4SPatrick Kelsey 			 * a subset of the original data), a valid ACK, a
1967281a0fd4SPatrick Kelsey 			 * FIN, or a RST.
1968281a0fd4SPatrick Kelsey 			 */
1969281a0fd4SPatrick Kelsey 			if ((thflags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) {
1970281a0fd4SPatrick Kelsey 				rstreason = BANDLIM_RST_OPENPORT;
1971281a0fd4SPatrick Kelsey 				goto dropwithreset;
1972281a0fd4SPatrick Kelsey 			} else if (thflags & TH_SYN) {
1973281a0fd4SPatrick Kelsey 				/* non-initial SYN is ignored */
1974281a0fd4SPatrick Kelsey 				if ((tcp_timer_active(tp, TT_DELACK) ||
1975281a0fd4SPatrick Kelsey 				     tcp_timer_active(tp, TT_REXMT)))
1976281a0fd4SPatrick Kelsey 					goto drop;
1977281a0fd4SPatrick Kelsey 			} else if (!(thflags & (TH_ACK|TH_FIN|TH_RST))) {
1978281a0fd4SPatrick Kelsey 				goto drop;
1979281a0fd4SPatrick Kelsey 			}
1980281a0fd4SPatrick Kelsey 		}
1981281a0fd4SPatrick Kelsey #endif
1982764d8cefSBill Fenner 		break;
1983764d8cefSBill Fenner 
1984764d8cefSBill Fenner 	/*
1985df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
1986df8bae1dSRodney W. Grimes 	 *	if seg contains an ACK, but not for our SYN, drop the input.
1987df8bae1dSRodney W. Grimes 	 *	if seg contains a RST, then drop the connection.
1988df8bae1dSRodney W. Grimes 	 *	if seg does not contain SYN, then drop it.
1989df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
1990df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
1991df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
1992f2512ba1SRui Paulo 	 *	if seg contains an ECE and ECN support is enabled, the stream
1993f2512ba1SRui Paulo 	 *	    is ECN capable.
1994df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1995df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
1996df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
1997df8bae1dSRodney W. Grimes 	 */
1998df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
1999fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
2000fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->iss) ||
2001fb59c426SYoshinobu Inoue 		     SEQ_GT(th->th_ack, tp->snd_max))) {
2002a57815efSBosko Milekic 			rstreason = BANDLIM_UNLIMITED;
2003a0292f23SGarrett Wollman 			goto dropwithreset;
2004a0292f23SGarrett Wollman 		}
200557f60867SMark Johnston 		if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) {
2006d9fae5abSAndriy Gapon 			TCP_PROBE5(connect__refused, NULL, tp,
2007fa22ce15SAdrian Chadd 			    mtod(m, const char *), tp, th);
2008df8bae1dSRodney W. Grimes 			tp = tcp_drop(tp, ECONNREFUSED);
200957f60867SMark Johnston 		}
20100ca3f933SAndre Oppermann 		if (thflags & TH_RST)
2011df8bae1dSRodney W. Grimes 			goto drop;
20120ca3f933SAndre Oppermann 		if (!(thflags & TH_SYN))
2013df8bae1dSRodney W. Grimes 			goto drop;
2014464fcfbcSAndre Oppermann 
2015fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
2016df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
2017fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
201878b50714SRobert Watson 			TCPSTAT_INC(tcps_connects);
2019845799c1SAndras Olah 			soisconnected(so);
2020c488362eSRobert Watson #ifdef MAC
202130d239bcSRobert Watson 			mac_socketpeer_set_from_mbuf(m, so);
2022c488362eSRobert Watson #endif
2023845799c1SAndras Olah 			/* Do window scaling on this connection? */
2024845799c1SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2025845799c1SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
2026845799c1SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2027845799c1SAndras Olah 			}
2028*3ac12506SJonathan T. Looney 			tp->rcv_adv += min(tp->rcv_wnd,
2029766282cbSJohn Baldwin 			    TCP_MAXWIN << tp->rcv_scale);
2030a0292f23SGarrett Wollman 			tp->snd_una++;		/* SYN is acked */
2031a0292f23SGarrett Wollman 			/*
2032a0292f23SGarrett Wollman 			 * If there's data, delay ACK; if there's also a FIN
2033a0292f23SGarrett Wollman 			 * ACKNOW will be turned on later.
2034a0292f23SGarrett Wollman 			 */
2035c1e5a6e5SAndre Oppermann 			if (DELAY_ACK(tp, tlen) && tlen != 0)
2036b8152ba7SAndre Oppermann 				tcp_timer_activate(tp, TT_DELACK,
2037b8152ba7SAndre Oppermann 				    tcp_delacktime);
2038a0292f23SGarrett Wollman 			else
2039a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
2040f2512ba1SRui Paulo 
2041603724d3SBjoern A. Zeeb 			if ((thflags & TH_ECE) && V_tcp_do_ecn) {
2042f2512ba1SRui Paulo 				tp->t_flags |= TF_ECN_PERMIT;
204378b50714SRobert Watson 				TCPSTAT_INC(tcps_ecn_shs);
2044f2512ba1SRui Paulo 			}
2045f2512ba1SRui Paulo 
2046a0292f23SGarrett Wollman 			/*
2047a0292f23SGarrett Wollman 			 * Received <SYN,ACK> in SYN_SENT[*] state.
2048a0292f23SGarrett Wollman 			 * Transitions:
2049a0292f23SGarrett Wollman 			 *	SYN_SENT  --> ESTABLISHED
2050a0292f23SGarrett Wollman 			 *	SYN_SENT* --> FIN_WAIT_1
2051a0292f23SGarrett Wollman 			 */
20529b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
2053a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDFIN) {
205457f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_1);
2055a0292f23SGarrett Wollman 				tp->t_flags &= ~TF_NEEDFIN;
2056fb59c426SYoshinobu Inoue 				thflags &= ~TH_SYN;
20577ff19458SPaul Traina 			} else {
205857f60867SMark Johnston 				tcp_state_change(tp, TCPS_ESTABLISHED);
2059d9fae5abSAndriy Gapon 				TCP_PROBE5(connect__established, NULL, tp,
2060fa22ce15SAdrian Chadd 				    mtod(m, const char *), tp, th);
2061dbc42409SLawrence Stewart 				cc_conn_init(tp);
20629077f387SGleb Smirnoff 				tcp_timer_activate(tp, TT_KEEP,
20639077f387SGleb Smirnoff 				    TP_KEEPIDLE(tp));
20647ff19458SPaul Traina 			}
2065a0292f23SGarrett Wollman 		} else {
2066a0292f23SGarrett Wollman 			/*
2067c068736aSJeffrey Hsu 			 * Received initial SYN in SYN-SENT[*] state =>
2068153edc50SHiren Panchasara 			 * simultaneous open.
2069c068736aSJeffrey Hsu 			 * If it succeeds, connection is * half-synchronized.
2070c068736aSJeffrey Hsu 			 * Otherwise, do 3-way handshake:
2071a0292f23SGarrett Wollman 			 *        SYN-SENT -> SYN-RECEIVED
2072a0292f23SGarrett Wollman 			 *        SYN-SENT* -> SYN-RECEIVED*
2073a0292f23SGarrett Wollman 			 */
20744b8e98d6SQing Li 			tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
2075b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
207657f60867SMark Johnston 			tcp_state_change(tp, TCPS_SYN_RECEIVED);
2077a0292f23SGarrett Wollman 		}
2078df8bae1dSRodney W. Grimes 
2079ff9b006dSJulien Charbon 		KASSERT(ti_locked == TI_RLOCKED, ("%s: trimthenstep6: "
2080252ca428SRobert Watson 		    "ti_locked %d", __func__, ti_locked));
2081ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
20828501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
20837cfc6904SRobert Watson 
2084df8bae1dSRodney W. Grimes 		/*
2085fb59c426SYoshinobu Inoue 		 * Advance th->th_seq to correspond to first data byte.
2086df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
2087df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
2088df8bae1dSRodney W. Grimes 		 */
2089fb59c426SYoshinobu Inoue 		th->th_seq++;
2090fb59c426SYoshinobu Inoue 		if (tlen > tp->rcv_wnd) {
2091fb59c426SYoshinobu Inoue 			todrop = tlen - tp->rcv_wnd;
2092df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
2093fb59c426SYoshinobu Inoue 			tlen = tp->rcv_wnd;
2094fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
209578b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpackafterwin);
209678b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2097df8bae1dSRodney W. Grimes 		}
2098fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
2099fb59c426SYoshinobu Inoue 		tp->rcv_up = th->th_seq;
2100a0292f23SGarrett Wollman 		/*
2101a0292f23SGarrett Wollman 		 * Client side of transaction: already sent SYN and data.
2102a0292f23SGarrett Wollman 		 * If the remote host used T/TCP to validate the SYN,
2103a0292f23SGarrett Wollman 		 * our data will be ACK'd; if so, enter normal data segment
2104a0292f23SGarrett Wollman 		 * processing in the middle of step 5, ack processing.
2105a0292f23SGarrett Wollman 		 * Otherwise, goto step 6.
2106a0292f23SGarrett Wollman 		 */
2107fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK)
2108a0292f23SGarrett Wollman 			goto process_ACK;
2109c068736aSJeffrey Hsu 
2110df8bae1dSRodney W. Grimes 		goto step6;
2111c068736aSJeffrey Hsu 
2112a0292f23SGarrett Wollman 	/*
2113a0292f23SGarrett Wollman 	 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
2114c94c54e4SAndre Oppermann 	 *      do normal processing.
2115a0292f23SGarrett Wollman 	 *
2116c94c54e4SAndre Oppermann 	 * NB: Leftover from RFC1644 T/TCP.  Cases to be reused later.
2117a0292f23SGarrett Wollman 	 */
2118a0292f23SGarrett Wollman 	case TCPS_LAST_ACK:
2119a0292f23SGarrett Wollman 	case TCPS_CLOSING:
2120a0292f23SGarrett Wollman 		break;  /* continue normal processing */
2121df8bae1dSRodney W. Grimes 	}
2122df8bae1dSRodney W. Grimes 
2123df8bae1dSRodney W. Grimes 	/*
2124df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
212580ab7c0eSGarrett Wollman 	 * First check the RST flag and sequence number since reset segments
212680ab7c0eSGarrett Wollman 	 * are exempt from the timestamp and connection count tests.  This
212780ab7c0eSGarrett Wollman 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
212880ab7c0eSGarrett Wollman 	 * below which allowed reset segments in half the sequence space
212980ab7c0eSGarrett Wollman 	 * to fall though and be processed (which gives forged reset
213080ab7c0eSGarrett Wollman 	 * segments with a random sequence number a 50 percent chance of
213180ab7c0eSGarrett Wollman 	 * killing a connection).
213280ab7c0eSGarrett Wollman 	 * Then check timestamp, if present.
2133a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
2134df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
2135df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
2136df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
213780ab7c0eSGarrett Wollman 	 */
2138fb59c426SYoshinobu Inoue 	if (thflags & TH_RST) {
21393220a212SGleb Smirnoff 		/*
21403220a212SGleb Smirnoff 		 * RFC5961 Section 3.2
21413220a212SGleb Smirnoff 		 *
21423220a212SGleb Smirnoff 		 * - RST drops connection only if SEG.SEQ == RCV.NXT.
21433220a212SGleb Smirnoff 		 * - If RST is in window, we send challenge ACK.
21443220a212SGleb Smirnoff 		 *
21453220a212SGleb Smirnoff 		 * Note: to take into account delayed ACKs, we should
21463220a212SGleb Smirnoff 		 *   test against last_ack_sent instead of rcv_nxt.
21473220a212SGleb Smirnoff 		 * Note 2: we handle special case of closed window, not
21483220a212SGleb Smirnoff 		 *   covered by the RFC.
21493220a212SGleb Smirnoff 		 */
21503220a212SGleb Smirnoff 		if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
21513220a212SGleb Smirnoff 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
21523220a212SGleb Smirnoff 		    (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) {
215380ab7c0eSGarrett Wollman 
2154ff9b006dSJulien Charbon 			INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2155ff9b006dSJulien Charbon 			KASSERT(ti_locked == TI_RLOCKED,
21563220a212SGleb Smirnoff 			    ("%s: TH_RST ti_locked %d, th %p tp %p",
21573220a212SGleb Smirnoff 			    __func__, ti_locked, th, tp));
21583220a212SGleb Smirnoff 			KASSERT(tp->t_state != TCPS_SYN_SENT,
21593220a212SGleb Smirnoff 			    ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p",
21603220a212SGleb Smirnoff 			    __func__, th, tp));
21613220a212SGleb Smirnoff 
21623220a212SGleb Smirnoff 			if (V_tcp_insecure_rst ||
21633220a212SGleb Smirnoff 			    tp->last_ack_sent == th->th_seq) {
21643220a212SGleb Smirnoff 				TCPSTAT_INC(tcps_drops);
21653220a212SGleb Smirnoff 				/* Drop the connection. */
21663220a212SGleb Smirnoff 				switch (tp->t_state) {
216780ab7c0eSGarrett Wollman 				case TCPS_SYN_RECEIVED:
216880ab7c0eSGarrett Wollman 					so->so_error = ECONNREFUSED;
216980ab7c0eSGarrett Wollman 					goto close;
217080ab7c0eSGarrett Wollman 				case TCPS_ESTABLISHED:
217180ab7c0eSGarrett Wollman 				case TCPS_FIN_WAIT_1:
217280ab7c0eSGarrett Wollman 				case TCPS_FIN_WAIT_2:
217380ab7c0eSGarrett Wollman 				case TCPS_CLOSE_WAIT:
217480ab7c0eSGarrett Wollman 					so->so_error = ECONNRESET;
217580ab7c0eSGarrett Wollman 				close:
217657f60867SMark Johnston 					tcp_state_change(tp, TCPS_CLOSED);
21773220a212SGleb Smirnoff 					/* FALLTHROUGH */
21783220a212SGleb Smirnoff 				default:
217980ab7c0eSGarrett Wollman 					tp = tcp_close(tp);
21803220a212SGleb Smirnoff 				}
21813220a212SGleb Smirnoff 			} else {
21823220a212SGleb Smirnoff 				TCPSTAT_INC(tcps_badrst);
21833220a212SGleb Smirnoff 				/* Send challenge ACK. */
21843220a212SGleb Smirnoff 				tcp_respond(tp, mtod(m, void *), th, m,
21853220a212SGleb Smirnoff 				    tp->rcv_nxt, tp->snd_nxt, TH_ACK);
21863220a212SGleb Smirnoff 				tp->last_ack_sent = tp->rcv_nxt;
21873220a212SGleb Smirnoff 				m = NULL;
21883220a212SGleb Smirnoff 			}
21893220a212SGleb Smirnoff 		}
21903220a212SGleb Smirnoff 		goto drop;
21913220a212SGleb Smirnoff 	}
219280ab7c0eSGarrett Wollman 
21933220a212SGleb Smirnoff 	/*
21943220a212SGleb Smirnoff 	 * RFC5961 Section 4.2
21953220a212SGleb Smirnoff 	 * Send challenge ACK for any SYN in synchronized state.
21963220a212SGleb Smirnoff 	 */
2197281a0fd4SPatrick Kelsey 	if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT &&
2198281a0fd4SPatrick Kelsey 	    tp->t_state != TCPS_SYN_RECEIVED) {
2199ff9b006dSJulien Charbon 		KASSERT(ti_locked == TI_RLOCKED,
22003220a212SGleb Smirnoff 		    ("tcp_do_segment: TH_SYN ti_locked %d", ti_locked));
2201ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2202252ca428SRobert Watson 
22033220a212SGleb Smirnoff 		TCPSTAT_INC(tcps_badsyn);
22043220a212SGleb Smirnoff 		if (V_tcp_insecure_syn &&
22053220a212SGleb Smirnoff 		    SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
22063220a212SGleb Smirnoff 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
22073220a212SGleb Smirnoff 			tp = tcp_drop(tp, ECONNRESET);
22083220a212SGleb Smirnoff 			rstreason = BANDLIM_UNLIMITED;
22093220a212SGleb Smirnoff 		} else {
22103220a212SGleb Smirnoff 			/* Send challenge ACK. */
22113220a212SGleb Smirnoff 			tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt,
22123220a212SGleb Smirnoff 			    tp->snd_nxt, TH_ACK);
22133220a212SGleb Smirnoff 			tp->last_ack_sent = tp->rcv_nxt;
22143220a212SGleb Smirnoff 			m = NULL;
221580ab7c0eSGarrett Wollman 		}
221680ab7c0eSGarrett Wollman 		goto drop;
221780ab7c0eSGarrett Wollman 	}
221880ab7c0eSGarrett Wollman 
221980ab7c0eSGarrett Wollman 	/*
2220df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
2221df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
2222df8bae1dSRodney W. Grimes 	 */
2223be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
222480ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
2225df8bae1dSRodney W. Grimes 
2226df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
2227d8951c8aSBjoern A. Zeeb 		if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) {
2228df8bae1dSRodney W. Grimes 			/*
2229df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
2230df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
2231df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
2232df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
2233df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
2234df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
2235df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
2236df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
2237df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
2238df8bae1dSRodney W. Grimes 			 */
2239df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
2240df8bae1dSRodney W. Grimes 		} else {
224178b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
224278b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, tlen);
224378b50714SRobert Watson 			TCPSTAT_INC(tcps_pawsdrop);
224407fd333dSMatthew Dillon 			if (tlen)
2245df8bae1dSRodney W. Grimes 				goto dropafterack;
22461ab4789dSMatthew Dillon 			goto drop;
22471ab4789dSMatthew Dillon 		}
2248df8bae1dSRodney W. Grimes 	}
2249df8bae1dSRodney W. Grimes 
2250a0292f23SGarrett Wollman 	/*
225180ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
225280ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
225380ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
225480ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
225580ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
225680ab7c0eSGarrett Wollman 	 */
2257a57815efSBosko Milekic 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
2258a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2259a57815efSBosko Milekic 		goto dropwithreset;
2260a57815efSBosko Milekic 	}
226180ab7c0eSGarrett Wollman 
2262fb59c426SYoshinobu Inoue 	todrop = tp->rcv_nxt - th->th_seq;
2263df8bae1dSRodney W. Grimes 	if (todrop > 0) {
2264831ad37eSXin LI 		if (thflags & TH_SYN) {
2265fb59c426SYoshinobu Inoue 			thflags &= ~TH_SYN;
2266fb59c426SYoshinobu Inoue 			th->th_seq++;
2267fb59c426SYoshinobu Inoue 			if (th->th_urp > 1)
2268fb59c426SYoshinobu Inoue 				th->th_urp--;
2269df8bae1dSRodney W. Grimes 			else
2270fb59c426SYoshinobu Inoue 				thflags &= ~TH_URG;
2271df8bae1dSRodney W. Grimes 			todrop--;
2272df8bae1dSRodney W. Grimes 		}
2273df8bae1dSRodney W. Grimes 		/*
2274dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
2275df8bae1dSRodney W. Grimes 		 */
2276fb59c426SYoshinobu Inoue 		if (todrop > tlen
2277fb59c426SYoshinobu Inoue 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
2278dac20301SGarrett Wollman 			/*
2279dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
2280dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
2281dac20301SGarrett Wollman 			 * of sequence; drop it.
2282dac20301SGarrett Wollman 			 */
2283fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
2284dac20301SGarrett Wollman 
2285df8bae1dSRodney W. Grimes 			/*
2286dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
2287dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
2288df8bae1dSRodney W. Grimes 			 */
2289dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
2290fb59c426SYoshinobu Inoue 			todrop = tlen;
229178b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
229278b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, todrop);
2293df8bae1dSRodney W. Grimes 		} else {
229478b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpartduppack);
229578b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop);
2296df8bae1dSRodney W. Grimes 		}
2297fb59c426SYoshinobu Inoue 		drop_hdrlen += todrop;	/* drop from the top afterwards */
2298fb59c426SYoshinobu Inoue 		th->th_seq += todrop;
2299fb59c426SYoshinobu Inoue 		tlen -= todrop;
2300fb59c426SYoshinobu Inoue 		if (th->th_urp > todrop)
2301fb59c426SYoshinobu Inoue 			th->th_urp -= todrop;
2302df8bae1dSRodney W. Grimes 		else {
2303fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;
2304fb59c426SYoshinobu Inoue 			th->th_urp = 0;
2305df8bae1dSRodney W. Grimes 		}
2306df8bae1dSRodney W. Grimes 	}
2307df8bae1dSRodney W. Grimes 
2308df8bae1dSRodney W. Grimes 	/*
2309df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
2310df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
2311df8bae1dSRodney W. Grimes 	 */
2312df8bae1dSRodney W. Grimes 	if ((so->so_state & SS_NOFDREF) &&
2313fb59c426SYoshinobu Inoue 	    tp->t_state > TCPS_CLOSE_WAIT && tlen) {
2314ff9b006dSJulien Charbon 		KASSERT(ti_locked == TI_RLOCKED, ("%s: SS_NOFDEREF && "
2315252ca428SRobert Watson 		    "CLOSE_WAIT && tlen ti_locked %d", __func__, ti_locked));
2316ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2317252ca428SRobert Watson 
231807dacf03SAndre Oppermann 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
231907dacf03SAndre Oppermann 			log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data "
232007dacf03SAndre Oppermann 			    "after socket was closed, "
232107dacf03SAndre Oppermann 			    "sending RST and removing tcpcb\n",
2322e31d8aa3SMike Silbersack 			    s, __func__, tcpstates[tp->t_state], tlen);
2323773673c1SAndre Oppermann 			free(s, M_TCPLOG);
2324773673c1SAndre Oppermann 		}
2325df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
232678b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvafterclose);
2327a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
2328df8bae1dSRodney W. Grimes 		goto dropwithreset;
2329df8bae1dSRodney W. Grimes 	}
2330df8bae1dSRodney W. Grimes 
2331df8bae1dSRodney W. Grimes 	/*
2332df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
2333df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
2334df8bae1dSRodney W. Grimes 	 */
2335fb59c426SYoshinobu Inoue 	todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
2336df8bae1dSRodney W. Grimes 	if (todrop > 0) {
233778b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvpackafterwin);
2338fb59c426SYoshinobu Inoue 		if (todrop >= tlen) {
233978b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen);
2340df8bae1dSRodney W. Grimes 			/*
2341df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
2342df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
2343df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
2344df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
2345df8bae1dSRodney W. Grimes 			 * and ack.
2346df8bae1dSRodney W. Grimes 			 */
2347fb59c426SYoshinobu Inoue 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
2348df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
234978b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvwinprobe);
2350df8bae1dSRodney W. Grimes 			} else
2351df8bae1dSRodney W. Grimes 				goto dropafterack;
2352df8bae1dSRodney W. Grimes 		} else
235378b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2354df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
2355fb59c426SYoshinobu Inoue 		tlen -= todrop;
2356fb59c426SYoshinobu Inoue 		thflags &= ~(TH_PUSH|TH_FIN);
2357df8bae1dSRodney W. Grimes 	}
2358df8bae1dSRodney W. Grimes 
2359df8bae1dSRodney W. Grimes 	/*
2360df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
2361df8bae1dSRodney W. Grimes 	 * record its timestamp.
2362cf09195bSPaul Saab 	 * NOTE:
2363cf09195bSPaul Saab 	 * 1) That the test incorporates suggestions from the latest
2364a0292f23SGarrett Wollman 	 *    proposal of the tcplw@cray.com list (Braden 1993/04/26).
2365cf09195bSPaul Saab 	 * 2) That updating only on newer timestamps interferes with
2366cf09195bSPaul Saab 	 *    our earlier PAWS tests, so this check should be solely
2367cf09195bSPaul Saab 	 *    predicated on the sequence space of this segment.
2368cf09195bSPaul Saab 	 * 3) That we modify the segment boundary check to be
2369cf09195bSPaul Saab 	 *        Last.ACK.Sent <= SEG.SEQ + SEG.Len
2370cf09195bSPaul Saab 	 *    instead of RFC1323's
2371cf09195bSPaul Saab 	 *        Last.ACK.Sent < SEG.SEQ + SEG.Len,
2372cf09195bSPaul Saab 	 *    This modified check allows us to overcome RFC1323's
2373cf09195bSPaul Saab 	 *    limitations as described in Stevens TCP/IP Illustrated
2374cf09195bSPaul Saab 	 *    Vol. 2 p.869. In such cases, we can still calculate the
2375cf09195bSPaul Saab 	 *    RTT correctly when RCV.NXT == Last.ACK.Sent.
2376df8bae1dSRodney W. Grimes 	 */
2377be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 &&
2378cf09195bSPaul Saab 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
2379cf09195bSPaul Saab 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
2380cf09195bSPaul Saab 		((thflags & (TH_SYN|TH_FIN)) != 0))) {
2381d8951c8aSBjoern A. Zeeb 		tp->ts_recent_age = tcp_ts_getticks();
2382a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
2383df8bae1dSRodney W. Grimes 	}
2384df8bae1dSRodney W. Grimes 
2385df8bae1dSRodney W. Grimes 	/*
2386a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
2387a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
2388a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
2389a0292f23SGarrett Wollman 	 */
2390fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) == 0) {
2391a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
2392281a0fd4SPatrick Kelsey 		    (tp->t_flags & TF_NEEDSYN)) {
2393281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
2394281a0fd4SPatrick Kelsey 			if (tp->t_state == TCPS_SYN_RECEIVED &&
2395281a0fd4SPatrick Kelsey 			    tp->t_flags & TF_FASTOPEN) {
2396281a0fd4SPatrick Kelsey 				tp->snd_wnd = tiwin;
2397281a0fd4SPatrick Kelsey 				cc_conn_init(tp);
2398281a0fd4SPatrick Kelsey 			}
2399281a0fd4SPatrick Kelsey #endif
2400a0292f23SGarrett Wollman 			goto step6;
2401281a0fd4SPatrick Kelsey 		} else if (tp->t_flags & TF_ACKNOW)
24025e1aa279SDavid Malone 			goto dropafterack;
2403a0292f23SGarrett Wollman 		else
2404a0292f23SGarrett Wollman 			goto drop;
2405a0292f23SGarrett Wollman 	}
2406df8bae1dSRodney W. Grimes 
2407df8bae1dSRodney W. Grimes 	/*
2408df8bae1dSRodney W. Grimes 	 * Ack processing.
2409df8bae1dSRodney W. Grimes 	 */
2410df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2411df8bae1dSRodney W. Grimes 
2412df8bae1dSRodney W. Grimes 	/*
2413764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
2414764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
2415764d8cefSBill Fenner 	 * The ACK was checked above.
2416df8bae1dSRodney W. Grimes 	 */
2417df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2418a0292f23SGarrett Wollman 
241978b50714SRobert Watson 		TCPSTAT_INC(tcps_connects);
2420df8bae1dSRodney W. Grimes 		soisconnected(so);
2421df8bae1dSRodney W. Grimes 		/* Do window scaling? */
2422df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2423df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
2424df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
2425464fcfbcSAndre Oppermann 			tp->snd_wnd = tiwin;
2426df8bae1dSRodney W. Grimes 		}
2427a0292f23SGarrett Wollman 		/*
2428a0292f23SGarrett Wollman 		 * Make transitions:
2429a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
2430a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
2431a0292f23SGarrett Wollman 		 */
24329b8b58e0SJonathan Lemon 		tp->t_starttime = ticks;
2433a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDFIN) {
243457f60867SMark Johnston 			tcp_state_change(tp, TCPS_FIN_WAIT_1);
2435a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDFIN;
24367ff19458SPaul Traina 		} else {
243757f60867SMark Johnston 			tcp_state_change(tp, TCPS_ESTABLISHED);
2438d9fae5abSAndriy Gapon 			TCP_PROBE5(accept__established, NULL, tp,
2439fa22ce15SAdrian Chadd 			    mtod(m, const char *), tp, th);
2440281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
2441281a0fd4SPatrick Kelsey 			if (tp->t_tfo_pending) {
2442281a0fd4SPatrick Kelsey 				tcp_fastopen_decrement_counter(tp->t_tfo_pending);
2443281a0fd4SPatrick Kelsey 				tp->t_tfo_pending = NULL;
2444281a0fd4SPatrick Kelsey 
2445281a0fd4SPatrick Kelsey 				/*
2446281a0fd4SPatrick Kelsey 				 * Account for the ACK of our SYN prior to
2447281a0fd4SPatrick Kelsey 				 * regular ACK processing below.
2448281a0fd4SPatrick Kelsey 				 */
2449281a0fd4SPatrick Kelsey 				tp->snd_una++;
2450281a0fd4SPatrick Kelsey 			}
2451281a0fd4SPatrick Kelsey 			/*
2452281a0fd4SPatrick Kelsey 			 * TFO connections call cc_conn_init() during SYN
2453281a0fd4SPatrick Kelsey 			 * processing.  Calling it again here for such
2454281a0fd4SPatrick Kelsey 			 * connections is not harmless as it would undo the
2455281a0fd4SPatrick Kelsey 			 * snd_cwnd reduction that occurs when a TFO SYN|ACK
2456281a0fd4SPatrick Kelsey 			 * is retransmitted.
2457281a0fd4SPatrick Kelsey 			 */
2458281a0fd4SPatrick Kelsey 			if (!(tp->t_flags & TF_FASTOPEN))
2459281a0fd4SPatrick Kelsey #endif
2460dbc42409SLawrence Stewart 				cc_conn_init(tp);
24619077f387SGleb Smirnoff 			tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp));
24627ff19458SPaul Traina 		}
2463a0292f23SGarrett Wollman 		/*
2464a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
2465a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
2466a0292f23SGarrett Wollman 		 */
2467fb59c426SYoshinobu Inoue 		if (tlen == 0 && (thflags & TH_FIN) == 0)
2468fb59c426SYoshinobu Inoue 			(void) tcp_reass(tp, (struct tcphdr *)0, 0,
2469a0292f23SGarrett Wollman 			    (struct mbuf *)0);
247060ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq - 1;
247193b0017fSPhilippe Charnier 		/* FALLTHROUGH */
2472df8bae1dSRodney W. Grimes 
2473df8bae1dSRodney W. Grimes 	/*
2474df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
2475df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
2476fb59c426SYoshinobu Inoue 	 *	tp->snd_una < th->th_ack <= tp->snd_max
2477fb59c426SYoshinobu Inoue 	 * then advance tp->snd_una to th->th_ack and drop
2478df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
2479df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
2480df8bae1dSRodney W. Grimes 	 */
2481df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
2482df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
2483df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
2484df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
2485df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
2486df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
24875a53ca16SPaul Saab 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
248878b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvacktoomuch);
24895a53ca16SPaul Saab 			goto dropafterack;
24905a53ca16SPaul Saab 		}
24913529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
2492fc30a251SAndre Oppermann 		    ((to.to_flags & TOF_SACK) ||
2493fc30a251SAndre Oppermann 		     !TAILQ_EMPTY(&tp->snd_holes)))
2494021eaf79SHiren Panchasara 			sack_changed = tcp_sack_doack(tp, &to, th->th_ack);
249512eeb81fSHiren Panchasara 		else
249612eeb81fSHiren Panchasara 			/*
249712eeb81fSHiren Panchasara 			 * Reset the value so that previous (valid) value
249812eeb81fSHiren Panchasara 			 * from the last ack with SACK doesn't get used.
249912eeb81fSHiren Panchasara 			 */
250012eeb81fSHiren Panchasara 			tp->sackhint.sacked_bytes = 0;
250139bc9de5SLawrence Stewart 
250239bc9de5SLawrence Stewart 		/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
250339bc9de5SLawrence Stewart 		hhook_run_tcp_est_in(tp, th, &to);
250439bc9de5SLawrence Stewart 
2505fb59c426SYoshinobu Inoue 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
25060c39d38dSGleb Smirnoff 			u_int maxseg;
25070c39d38dSGleb Smirnoff 
25080c39d38dSGleb Smirnoff 			maxseg = tcp_maxseg(tp);
2509021eaf79SHiren Panchasara 			if (tlen == 0 &&
2510021eaf79SHiren Panchasara 			    (tiwin == tp->snd_wnd ||
2511021eaf79SHiren Panchasara 			    (tp->t_flags & TF_SACK_PERMIT))) {
251262b90589SPeter Wemm 				/*
251362b90589SPeter Wemm 				 * If this is the first time we've seen a
251462b90589SPeter Wemm 				 * FIN from the remote, this is not a
251562b90589SPeter Wemm 				 * duplicate and it needs to be processed
251662b90589SPeter Wemm 				 * normally.  This happens during a
251762b90589SPeter Wemm 				 * simultaneous close.
251862b90589SPeter Wemm 				 */
251962b90589SPeter Wemm 				if ((thflags & TH_FIN) &&
252062b90589SPeter Wemm 				    (TCPS_HAVERCVDFIN(tp->t_state) == 0)) {
252162b90589SPeter Wemm 					tp->t_dupacks = 0;
252262b90589SPeter Wemm 					break;
252362b90589SPeter Wemm 				}
252478b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvdupack);
2525df8bae1dSRodney W. Grimes 				/*
2526df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
2527df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
2528df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
25295f30ec9bSEitan Adler 				 * change and FIN isn't set),
25305f30ec9bSEitan Adler 				 * the ack is the biggest we've
2531df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
2532a4641f4eSPedro F. Giffuni 				 * threshold of them, assume a packet
2533df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
2534df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
2535df8bae1dSRodney W. Grimes 				 * window so we send only this one
2536df8bae1dSRodney W. Grimes 				 * packet.
2537df8bae1dSRodney W. Grimes 				 *
2538df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
2539df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
2540df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
2541df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
2542df8bae1dSRodney W. Grimes 				 * the new ssthresh).
2543df8bae1dSRodney W. Grimes 				 *
2544df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
2545df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
2546df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
2547df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
2548df8bae1dSRodney W. Grimes 				 * network.
2549f2512ba1SRui Paulo 				 *
2550f2512ba1SRui Paulo 				 * When using TCP ECN, notify the peer that
2551f2512ba1SRui Paulo 				 * we reduced the cwnd.
2552df8bae1dSRodney W. Grimes 				 */
2553021eaf79SHiren Panchasara 				/*
2554021eaf79SHiren Panchasara 				 * Following 2 kinds of acks should not affect
2555021eaf79SHiren Panchasara 				 * dupack counting:
2556021eaf79SHiren Panchasara 				 * 1) Old acks
2557021eaf79SHiren Panchasara 				 * 2) Acks with SACK but without any new SACK
2558021eaf79SHiren Panchasara 				 * information in them. These could result from
2559021eaf79SHiren Panchasara 				 * any anomaly in the network like a switch
2560021eaf79SHiren Panchasara 				 * duplicating packets or a possible DoS attack.
2561021eaf79SHiren Panchasara 				 */
2562021eaf79SHiren Panchasara 				if (th->th_ack != tp->snd_una ||
2563021eaf79SHiren Panchasara 				    ((tp->t_flags & TF_SACK_PERMIT) &&
2564021eaf79SHiren Panchasara 				    !sack_changed))
2565021eaf79SHiren Panchasara 					break;
2566021eaf79SHiren Panchasara 				else if (!tcp_timer_active(tp, TT_REXMT))
2567df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
2568cb942153SJeffrey Hsu 				else if (++tp->t_dupacks > tcprexmtthresh ||
2569dbc42409SLawrence Stewart 				     IN_FASTRECOVERY(tp->t_flags)) {
25704b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
25714b7b743cSLawrence Stewart 					    CC_DUPACK);
25723529149eSAndre Oppermann 					if ((tp->t_flags & TF_SACK_PERMIT) &&
2573dbc42409SLawrence Stewart 					    IN_FASTRECOVERY(tp->t_flags)) {
2574808f11b7SPaul Saab 						int awnd;
257525e6f9edSPaul Saab 
257625e6f9edSPaul Saab 						/*
257725e6f9edSPaul Saab 						 * Compute the amount of data in flight first.
25784c3972f0SHiren Panchasara 						 * We can inject new data into the pipe iff
257925e6f9edSPaul Saab 						 * we have less than 1/2 the original window's
258025e6f9edSPaul Saab 						 * worth of data in flight.
258125e6f9edSPaul Saab 						 */
258212eeb81fSHiren Panchasara 						if (V_tcp_do_rfc6675_pipe)
258312eeb81fSHiren Panchasara 							awnd = tcp_compute_pipe(tp);
258412eeb81fSHiren Panchasara 						else
2585808f11b7SPaul Saab 							awnd = (tp->snd_nxt - tp->snd_fack) +
25860077b016SPaul Saab 								tp->sackhint.sack_bytes_rexmit;
258712eeb81fSHiren Panchasara 
2588808f11b7SPaul Saab 						if (awnd < tp->snd_ssthresh) {
25890c39d38dSGleb Smirnoff 							tp->snd_cwnd += maxseg;
259025e6f9edSPaul Saab 							if (tp->snd_cwnd > tp->snd_ssthresh)
259125e6f9edSPaul Saab 								tp->snd_cwnd = tp->snd_ssthresh;
259225e6f9edSPaul Saab 						}
259325e6f9edSPaul Saab 					} else
25940c39d38dSGleb Smirnoff 						tp->snd_cwnd += maxseg;
259555bceb1eSRandall Stewart 					(void) tp->t_fb->tfb_tcp_output(tp);
259646f58482SJonathan Lemon 					goto drop;
2597cb942153SJeffrey Hsu 				} else if (tp->t_dupacks == tcprexmtthresh) {
2598cb942153SJeffrey Hsu 					tcp_seq onxt = tp->snd_nxt;
2599a0445c2eSJayanth Vijayaraghavan 
2600a0445c2eSJayanth Vijayaraghavan 					/*
2601a0445c2eSJayanth Vijayaraghavan 					 * If we're doing sack, check to
2602a0445c2eSJayanth Vijayaraghavan 					 * see if we're already in sack
2603a0445c2eSJayanth Vijayaraghavan 					 * recovery. If we're not doing sack,
2604a0445c2eSJayanth Vijayaraghavan 					 * check to see if we're in newreno
2605a0445c2eSJayanth Vijayaraghavan 					 * recovery.
2606a0445c2eSJayanth Vijayaraghavan 					 */
26073529149eSAndre Oppermann 					if (tp->t_flags & TF_SACK_PERMIT) {
2608dbc42409SLawrence Stewart 						if (IN_FASTRECOVERY(tp->t_flags)) {
2609a0445c2eSJayanth Vijayaraghavan 							tp->t_dupacks = 0;
2610a0445c2eSJayanth Vijayaraghavan 							break;
2611a0445c2eSJayanth Vijayaraghavan 						}
2612dbc42409SLawrence Stewart 					} else {
2613a0445c2eSJayanth Vijayaraghavan 						if (SEQ_LEQ(th->th_ack,
26149d11646dSJeffrey Hsu 						    tp->snd_recover)) {
2615cb942153SJeffrey Hsu 							tp->t_dupacks = 0;
2616cb942153SJeffrey Hsu 							break;
261746f58482SJonathan Lemon 						}
2618a0445c2eSJayanth Vijayaraghavan 					}
2619dbc42409SLawrence Stewart 					/* Congestion signal before ack. */
2620dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_NDUPACK);
26214b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
26224b7b743cSLawrence Stewart 					    CC_DUPACK);
2623b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
26249b8b58e0SJonathan Lemon 					tp->t_rtttime = 0;
26253529149eSAndre Oppermann 					if (tp->t_flags & TF_SACK_PERMIT) {
262678b50714SRobert Watson 						TCPSTAT_INC(
262778b50714SRobert Watson 						    tcps_sack_recovery_episode);
2628a55db2b6SPaul Saab 						tp->sack_newdata = tp->snd_nxt;
26290c39d38dSGleb Smirnoff 						tp->snd_cwnd = maxseg;
263055bceb1eSRandall Stewart 						(void) tp->t_fb->tfb_tcp_output(tp);
26316d90faf3SPaul Saab 						goto drop;
26326d90faf3SPaul Saab 					}
2633fb59c426SYoshinobu Inoue 					tp->snd_nxt = th->th_ack;
26340c39d38dSGleb Smirnoff 					tp->snd_cwnd = maxseg;
263555bceb1eSRandall Stewart 					(void) tp->t_fb->tfb_tcp_output(tp);
263648d2549cSJeffrey Hsu 					KASSERT(tp->snd_limited <= 2,
2637302ce8d6SAndre Oppermann 					    ("%s: tp->snd_limited too big",
2638302ce8d6SAndre Oppermann 					    __func__));
2639df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
26400c39d38dSGleb Smirnoff 					     maxseg *
264148d2549cSJeffrey Hsu 					     (tp->t_dupacks - tp->snd_limited);
2642df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
2643df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
2644df8bae1dSRodney W. Grimes 					goto drop;
2645603724d3SBjoern A. Zeeb 				} else if (V_tcp_do_rfc3042) {
264662d4443fSHiren Panchasara 					/*
264762d4443fSHiren Panchasara 					 * Process first and second duplicate
264862d4443fSHiren Panchasara 					 * ACKs. Each indicates a segment
264962d4443fSHiren Panchasara 					 * leaving the network, creating room
265062d4443fSHiren Panchasara 					 * for more. Make sure we can send a
265162d4443fSHiren Panchasara 					 * packet on reception of each duplicate
265262d4443fSHiren Panchasara 					 * ACK by increasing snd_cwnd by one
265362d4443fSHiren Panchasara 					 * segment. Restore the original
265462d4443fSHiren Panchasara 					 * snd_cwnd after packet transmission.
265562d4443fSHiren Panchasara 					 */
26564b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
26574b7b743cSLawrence Stewart 					    CC_DUPACK);
2658*3ac12506SJonathan T. Looney 					uint32_t oldcwnd = tp->snd_cwnd;
265948d2549cSJeffrey Hsu 					tcp_seq oldsndmax = tp->snd_max;
266048d2549cSJeffrey Hsu 					u_int sent;
26615628dd08SAndre Oppermann 					int avail;
266289c02376SJeffrey Hsu 
2663582a954bSJeffrey Hsu 					KASSERT(tp->t_dupacks == 1 ||
2664582a954bSJeffrey Hsu 					    tp->t_dupacks == 2,
2665302ce8d6SAndre Oppermann 					    ("%s: dupacks not 1 or 2",
2666302ce8d6SAndre Oppermann 					    __func__));
266761a36e3dSJeffrey Hsu 					if (tp->t_dupacks == 1)
266848d2549cSJeffrey Hsu 						tp->snd_limited = 0;
266961a36e3dSJeffrey Hsu 					tp->snd_cwnd =
267061a36e3dSJeffrey Hsu 					    (tp->snd_nxt - tp->snd_una) +
267161a36e3dSJeffrey Hsu 					    (tp->t_dupacks - tp->snd_limited) *
26720c39d38dSGleb Smirnoff 					    maxseg;
26735628dd08SAndre Oppermann 					/*
26745628dd08SAndre Oppermann 					 * Only call tcp_output when there
26755628dd08SAndre Oppermann 					 * is new data available to be sent.
26765628dd08SAndre Oppermann 					 * Otherwise we would send pure ACKs.
26775628dd08SAndre Oppermann 					 */
26785628dd08SAndre Oppermann 					SOCKBUF_LOCK(&so->so_snd);
2679cfa6009eSGleb Smirnoff 					avail = sbavail(&so->so_snd) -
26805628dd08SAndre Oppermann 					    (tp->snd_nxt - tp->snd_una);
26815628dd08SAndre Oppermann 					SOCKBUF_UNLOCK(&so->so_snd);
26825628dd08SAndre Oppermann 					if (avail > 0)
268355bceb1eSRandall Stewart 						(void) tp->t_fb->tfb_tcp_output(tp);
268448d2549cSJeffrey Hsu 					sent = tp->snd_max - oldsndmax;
26850c39d38dSGleb Smirnoff 					if (sent > maxseg) {
268689c02376SJeffrey Hsu 						KASSERT((tp->t_dupacks == 2 &&
268789c02376SJeffrey Hsu 						    tp->snd_limited == 0) ||
26880c39d38dSGleb Smirnoff 						   (sent == maxseg + 1 &&
268989c02376SJeffrey Hsu 						    tp->t_flags & TF_SENTFIN),
2690302ce8d6SAndre Oppermann 						    ("%s: sent too much",
2691302ce8d6SAndre Oppermann 						    __func__));
269248d2549cSJeffrey Hsu 						tp->snd_limited = 2;
269348d2549cSJeffrey Hsu 					} else if (sent > 0)
269448d2549cSJeffrey Hsu 						++tp->snd_limited;
2695582a954bSJeffrey Hsu 					tp->snd_cwnd = oldcwnd;
2696582a954bSJeffrey Hsu 					goto drop;
2697df8bae1dSRodney W. Grimes 				}
2698021eaf79SHiren Panchasara 			}
2699df8bae1dSRodney W. Grimes 			break;
2700021eaf79SHiren Panchasara 		} else {
2701021eaf79SHiren Panchasara 			/*
2702021eaf79SHiren Panchasara 			 * This ack is advancing the left edge, reset the
2703021eaf79SHiren Panchasara 			 * counter.
2704021eaf79SHiren Panchasara 			 */
2705021eaf79SHiren Panchasara 			tp->t_dupacks = 0;
2706021eaf79SHiren Panchasara 			/*
2707021eaf79SHiren Panchasara 			 * If this ack also has new SACK info, increment the
2708021eaf79SHiren Panchasara 			 * counter as per rfc6675.
2709021eaf79SHiren Panchasara 			 */
2710021eaf79SHiren Panchasara 			if ((tp->t_flags & TF_SACK_PERMIT) && sack_changed)
2711021eaf79SHiren Panchasara 				tp->t_dupacks++;
2712df8bae1dSRodney W. Grimes 		}
2713cb942153SJeffrey Hsu 
2714302ce8d6SAndre Oppermann 		KASSERT(SEQ_GT(th->th_ack, tp->snd_una),
2715302ce8d6SAndre Oppermann 		    ("%s: th_ack <= snd_una", __func__));
2716cb942153SJeffrey Hsu 
2717df8bae1dSRodney W. Grimes 		/*
2718df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
2719df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
2720df8bae1dSRodney W. Grimes 		 */
2721dbc42409SLawrence Stewart 		if (IN_FASTRECOVERY(tp->t_flags)) {
2722cb942153SJeffrey Hsu 			if (SEQ_LT(th->th_ack, tp->snd_recover)) {
27233529149eSAndre Oppermann 				if (tp->t_flags & TF_SACK_PERMIT)
27246d90faf3SPaul Saab 					tcp_sack_partialack(tp, th);
27256d90faf3SPaul Saab 				else
2726c068736aSJeffrey Hsu 					tcp_newreno_partial_ack(tp, th);
2727dbc42409SLawrence Stewart 			} else
2728dbc42409SLawrence Stewart 				cc_post_recovery(tp, th);
272946f58482SJonathan Lemon 		}
2730a0292f23SGarrett Wollman 		/*
2731a0292f23SGarrett Wollman 		 * If we reach this point, ACK is not a duplicate,
2732a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
2733a0292f23SGarrett Wollman 		 */
2734a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
2735a0292f23SGarrett Wollman 			/*
2736a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
2737a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
273807e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
273907e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
274007e43e10SAndras Olah 			 * we can do window scaling.
2741a0292f23SGarrett Wollman 			 */
2742a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
2743a0292f23SGarrett Wollman 			tp->snd_una++;
274407e43e10SAndras Olah 			/* Do window scaling? */
274507e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
274607e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
274707e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2748464fcfbcSAndre Oppermann 				/* Send window already scaled. */
274907e43e10SAndras Olah 			}
2750a0292f23SGarrett Wollman 		}
2751a0292f23SGarrett Wollman 
2752a0292f23SGarrett Wollman process_ACK:
27538501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
27547cfc6904SRobert Watson 
2755dbc42409SLawrence Stewart 		acked = BYTES_THIS_ACK(tp, th);
2756b8c2cd15SJonathan T. Looney 		KASSERT(acked >= 0, ("%s: acked unexepectedly negative "
2757b8c2cd15SJonathan T. Looney 		    "(tp->snd_una=%u, th->th_ack=%u, tp=%p, m=%p)", __func__,
2758b8c2cd15SJonathan T. Looney 		    tp->snd_una, th->th_ack, tp, m));
27594b7b743cSLawrence Stewart 		TCPSTAT_ADD(tcps_rcvackpack, nsegs);
276078b50714SRobert Watson 		TCPSTAT_ADD(tcps_rcvackbyte, acked);
2761df8bae1dSRodney W. Grimes 
2762df8bae1dSRodney W. Grimes 		/*
27639b8b58e0SJonathan Lemon 		 * If we just performed our first retransmit, and the ACK
27649b8b58e0SJonathan Lemon 		 * arrives within our recovery window, then it was a mistake
27659b8b58e0SJonathan Lemon 		 * to do the retransmit in the first place.  Recover our
27669b8b58e0SJonathan Lemon 		 * original cwnd and ssthresh, and proceed to transmit where
27679b8b58e0SJonathan Lemon 		 * we left off.
27689b8b58e0SJonathan Lemon 		 */
2769672dc4aeSJohn Baldwin 		if (tp->t_rxtshift == 1 && tp->t_flags & TF_PREVVALID &&
2770672dc4aeSJohn Baldwin 		    (int)(ticks - tp->t_badrxtwin) < 0)
2771dbc42409SLawrence Stewart 			cc_cong_signal(tp, th, CC_RTO_ERR);
27729b8b58e0SJonathan Lemon 
27739b8b58e0SJonathan Lemon 		/*
2774df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
2775df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
2776df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
2777df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
2778df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
2779df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
2780df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
2781fa55172bSMatthew Dillon 		 *
2782fa55172bSMatthew Dillon 		 * Some boxes send broken timestamp replies
2783fa55172bSMatthew Dillon 		 * during the SYN+ACK phase, ignore
2784fa55172bSMatthew Dillon 		 * timestamps of 0 or we could calculate a
2785fa55172bSMatthew Dillon 		 * huge RTT and blow up the retransmit timer.
2786df8bae1dSRodney W. Grimes 		 */
2787d8951c8aSBjoern A. Zeeb 		if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) {
2788*3ac12506SJonathan T. Looney 			uint32_t t;
2789d8951c8aSBjoern A. Zeeb 
2790d8951c8aSBjoern A. Zeeb 			t = tcp_ts_getticks() - to.to_tsecr;
2791d8951c8aSBjoern A. Zeeb 			if (!tp->t_rttlow || tp->t_rttlow > t)
2792d8951c8aSBjoern A. Zeeb 				tp->t_rttlow = t;
2793d8951c8aSBjoern A. Zeeb 			tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1);
2794fa55172bSMatthew Dillon 		} else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
2795eaf80179SAndre Oppermann 			if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime)
2796eaf80179SAndre Oppermann 				tp->t_rttlow = ticks - tp->t_rtttime;
27979b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
2798fa55172bSMatthew Dillon 		}
2799df8bae1dSRodney W. Grimes 
2800df8bae1dSRodney W. Grimes 		/*
2801df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
2802df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
2803df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
2804df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
2805df8bae1dSRodney W. Grimes 		 */
2806fb59c426SYoshinobu Inoue 		if (th->th_ack == tp->snd_max) {
2807b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
2808df8bae1dSRodney W. Grimes 			needoutput = 1;
2809b8152ba7SAndre Oppermann 		} else if (!tcp_timer_active(tp, TT_PERSIST))
2810b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
2811a0292f23SGarrett Wollman 
2812a0292f23SGarrett Wollman 		/*
2813a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
2814a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
2815a0292f23SGarrett Wollman 		 */
2816a0292f23SGarrett Wollman 		if (acked == 0)
2817a0292f23SGarrett Wollman 			goto step6;
2818a0292f23SGarrett Wollman 
2819df8bae1dSRodney W. Grimes 		/*
2820dbc42409SLawrence Stewart 		 * Let the congestion control algorithm update congestion
2821dbc42409SLawrence Stewart 		 * control related information. This typically means increasing
2822dbc42409SLawrence Stewart 		 * the congestion window.
2823df8bae1dSRodney W. Grimes 		 */
28244b7b743cSLawrence Stewart 		cc_ack_received(tp, th, nsegs, CC_ACK);
2825dbc42409SLawrence Stewart 
28265905999bSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
2827cfa6009eSGleb Smirnoff 		if (acked > sbavail(&so->so_snd)) {
2828b8c2cd15SJonathan T. Looney 			if (tp->snd_wnd >= sbavail(&so->so_snd))
2829cfa6009eSGleb Smirnoff 				tp->snd_wnd -= sbavail(&so->so_snd);
2830b8c2cd15SJonathan T. Looney 			else
2831b8c2cd15SJonathan T. Looney 				tp->snd_wnd = 0;
2832c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd,
2833cfa6009eSGleb Smirnoff 			    (int)sbavail(&so->so_snd));
2834df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
2835df8bae1dSRodney W. Grimes 		} else {
2836c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd, acked);
2837*3ac12506SJonathan T. Looney 			if (tp->snd_wnd >= (uint32_t) acked)
283860ee3bb2SAndre Oppermann 				tp->snd_wnd -= acked;
2839b8c2cd15SJonathan T. Looney 			else
2840b8c2cd15SJonathan T. Looney 				tp->snd_wnd = 0;
2841df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
2842df8bae1dSRodney W. Grimes 		}
2843564aab1fSAndre Oppermann 		/* NB: sowwakeup_locked() does an implicit unlock. */
28441e4d7da7SRobert Watson 		sowwakeup_locked(so);
2845c11a15bfSGleb Smirnoff 		m_freem(mfree);
2846e8949f74SAndre Oppermann 		/* Detect una wraparound. */
2847dbc42409SLawrence Stewart 		if (!IN_RECOVERY(tp->t_flags) &&
28489d11646dSJeffrey Hsu 		    SEQ_GT(tp->snd_una, tp->snd_recover) &&
28499d11646dSJeffrey Hsu 		    SEQ_LEQ(th->th_ack, tp->snd_recover))
28509d11646dSJeffrey Hsu 			tp->snd_recover = th->th_ack - 1;
2851dbc42409SLawrence Stewart 		/* XXXLAS: Can this be moved up into cc_post_recovery? */
2852dbc42409SLawrence Stewart 		if (IN_RECOVERY(tp->t_flags) &&
285324cb0f22SLawrence Stewart 		    SEQ_GEQ(th->th_ack, tp->snd_recover)) {
2854dbc42409SLawrence Stewart 			EXIT_RECOVERY(tp->t_flags);
285524cb0f22SLawrence Stewart 		}
2856fb59c426SYoshinobu Inoue 		tp->snd_una = th->th_ack;
28573529149eSAndre Oppermann 		if (tp->t_flags & TF_SACK_PERMIT) {
28586d90faf3SPaul Saab 			if (SEQ_GT(tp->snd_una, tp->snd_recover))
28596d90faf3SPaul Saab 				tp->snd_recover = tp->snd_una;
28606d90faf3SPaul Saab 		}
2861df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2862df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
2863df8bae1dSRodney W. Grimes 
2864df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2865df8bae1dSRodney W. Grimes 
2866df8bae1dSRodney W. Grimes 		/*
2867df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
2868df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
2869df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
2870df8bae1dSRodney W. Grimes 		 */
2871df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2872df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2873df8bae1dSRodney W. Grimes 				/*
2874df8bae1dSRodney W. Grimes 				 * If we can't receive any more
2875df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
2876df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
2877df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
2878df8bae1dSRodney W. Grimes 				 * we'll hang forever.
2879e8949f74SAndre Oppermann 				 *
2880e8949f74SAndre Oppermann 				 * XXXjl:
2881340c35deSJonathan Lemon 				 * we should release the tp also, and use a
2882340c35deSJonathan Lemon 				 * compressed state.
2883340c35deSJonathan Lemon 				 */
2884c0b99ffaSRobert Watson 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
288503e49181SSeigo Tanimura 					soisdisconnected(so);
28869077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
28879077f387SGleb Smirnoff 					    (tcp_fast_finwait2_recycle ?
28889077f387SGleb Smirnoff 					    tcp_finwait2_timeout :
28899077f387SGleb Smirnoff 					    TP_MAXIDLE(tp)));
28904cc20ab1SSeigo Tanimura 				}
289157f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_2);
2892df8bae1dSRodney W. Grimes 			}
2893df8bae1dSRodney W. Grimes 			break;
2894df8bae1dSRodney W. Grimes 
2895df8bae1dSRodney W. Grimes 		/*
2896df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
2897df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
2898df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
2899df8bae1dSRodney W. Grimes 		 * the segment.
2900df8bae1dSRodney W. Grimes 		 */
2901df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
2902df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2903ff9b006dSJulien Charbon 				INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
290411a20fb8SJeffrey Hsu 				tcp_twstart(tp);
2905ff9b006dSJulien Charbon 				INP_INFO_RUNLOCK(&V_tcbinfo);
2906340c35deSJonathan Lemon 				m_freem(m);
2907679d9708SAndre Oppermann 				return;
2908df8bae1dSRodney W. Grimes 			}
2909df8bae1dSRodney W. Grimes 			break;
2910df8bae1dSRodney W. Grimes 
2911df8bae1dSRodney W. Grimes 		/*
2912df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
2913df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
2914df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
2915df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
2916df8bae1dSRodney W. Grimes 		 */
2917df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
2918df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2919ff9b006dSJulien Charbon 				INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2920df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
2921df8bae1dSRodney W. Grimes 				goto drop;
2922df8bae1dSRodney W. Grimes 			}
2923df8bae1dSRodney W. Grimes 			break;
2924df8bae1dSRodney W. Grimes 		}
2925df8bae1dSRodney W. Grimes 	}
2926df8bae1dSRodney W. Grimes 
2927df8bae1dSRodney W. Grimes step6:
29288501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
29297cfc6904SRobert Watson 
2930df8bae1dSRodney W. Grimes 	/*
293160ee3bb2SAndre Oppermann 	 * Update window information.
293260ee3bb2SAndre Oppermann 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2933df8bae1dSRodney W. Grimes 	 */
293460ee3bb2SAndre Oppermann 	if ((thflags & TH_ACK) &&
293560ee3bb2SAndre Oppermann 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
293660ee3bb2SAndre Oppermann 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
293760ee3bb2SAndre Oppermann 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
293860ee3bb2SAndre Oppermann 		/* keep track of pure window updates */
293960ee3bb2SAndre Oppermann 		if (tlen == 0 &&
294060ee3bb2SAndre Oppermann 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
294178b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvwinupd);
2942df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
294360ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq;
294460ee3bb2SAndre Oppermann 		tp->snd_wl2 = th->th_ack;
2945df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
2946df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
294760ee3bb2SAndre Oppermann 		needoutput = 1;
2948df8bae1dSRodney W. Grimes 	}
2949df8bae1dSRodney W. Grimes 
2950df8bae1dSRodney W. Grimes 	/*
2951df8bae1dSRodney W. Grimes 	 * Process segments with URG.
2952df8bae1dSRodney W. Grimes 	 */
2953fb59c426SYoshinobu Inoue 	if ((thflags & TH_URG) && th->th_urp &&
2954df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2955df8bae1dSRodney W. Grimes 		/*
2956df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
2957df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
2958df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
2959df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
2960df8bae1dSRodney W. Grimes 		 */
296118ad5842SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
2962cfa6009eSGleb Smirnoff 		if (th->th_urp + sbavail(&so->so_rcv) > sb_max) {
2963fb59c426SYoshinobu Inoue 			th->th_urp = 0;			/* XXX */
2964fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;		/* XXX */
296518ad5842SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);	/* XXX */
2966df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
2967df8bae1dSRodney W. Grimes 		}
2968df8bae1dSRodney W. Grimes 		/*
2969df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
2970df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
2971df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2972df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
2973df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
2974df8bae1dSRodney W. Grimes 		 *
2975df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
2976df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
2977df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
2978df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
2979df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
2980df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
2981df8bae1dSRodney W. Grimes 		 */
2982fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
2983fb59c426SYoshinobu Inoue 			tp->rcv_up = th->th_seq + th->th_urp;
2984cfa6009eSGleb Smirnoff 			so->so_oobmark = sbavail(&so->so_rcv) +
2985df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
2986927c5ceaSRobert Watson 			if (so->so_oobmark == 0)
2987c0b99ffaSRobert Watson 				so->so_rcv.sb_state |= SBS_RCVATMARK;
2988df8bae1dSRodney W. Grimes 			sohasoutofband(so);
2989df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2990df8bae1dSRodney W. Grimes 		}
299118ad5842SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
2992df8bae1dSRodney W. Grimes 		/*
2993df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
2994df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
2995df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
2996df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
2997df8bae1dSRodney W. Grimes 		 */
2998*3ac12506SJonathan T. Looney 		if (th->th_urp <= (uint32_t)tlen &&
299930613f56SJeffrey Hsu 		    !(so->so_options & SO_OOBINLINE)) {
300030613f56SJeffrey Hsu 			/* hdr drop is delayed */
300130613f56SJeffrey Hsu 			tcp_pulloutofband(so, th, m, drop_hdrlen);
300230613f56SJeffrey Hsu 		}
3003c068736aSJeffrey Hsu 	} else {
3004df8bae1dSRodney W. Grimes 		/*
3005df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
3006df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
3007df8bae1dSRodney W. Grimes 		 * with the receive window.
3008df8bae1dSRodney W. Grimes 		 */
3009df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
3010df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
3011c068736aSJeffrey Hsu 	}
3012df8bae1dSRodney W. Grimes dodata:							/* XXX */
30138501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
30147cfc6904SRobert Watson 
3015df8bae1dSRodney W. Grimes 	/*
3016df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
3017df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
3018df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
3019df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
3020df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
3021df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
3022df8bae1dSRodney W. Grimes 	 */
3023281a0fd4SPatrick Kelsey 	tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
3024281a0fd4SPatrick Kelsey 		   (tp->t_flags & TF_FASTOPEN));
3025281a0fd4SPatrick Kelsey 	if ((tlen || (thflags & TH_FIN) || tfo_syn) &&
3026df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
302769e03620SPaul Saab 		tcp_seq save_start = th->th_seq;
3028fb59c426SYoshinobu Inoue 		m_adj(m, drop_hdrlen);	/* delayed header drop */
3029e4b64281SJesper Skriver 		/*
3030c068736aSJeffrey Hsu 		 * Insert segment which includes th into TCP reassembly queue
3031c068736aSJeffrey Hsu 		 * with control block tp.  Set thflags to whether reassembly now
3032c068736aSJeffrey Hsu 		 * includes a segment with FIN.  This handles the common case
3033c068736aSJeffrey Hsu 		 * inline (segment is the next to be received on an established
3034c068736aSJeffrey Hsu 		 * connection, and the queue is empty), avoiding linkage into
3035c068736aSJeffrey Hsu 		 * and removal from the queue and repetition of various
3036c068736aSJeffrey Hsu 		 * conversions.
3037c068736aSJeffrey Hsu 		 * Set DELACK for segments received in order, but ack
3038c068736aSJeffrey Hsu 		 * immediately when segments are out of order (so
3039c068736aSJeffrey Hsu 		 * fast retransmit can work).
3040e4b64281SJesper Skriver 		 */
30414741bfcbSPatrick Kelsey 		if (th->th_seq == tp->rcv_nxt &&
30424741bfcbSPatrick Kelsey 		    LIST_EMPTY(&tp->t_segq) &&
3043281a0fd4SPatrick Kelsey 		    (TCPS_HAVEESTABLISHED(tp->t_state) ||
3044281a0fd4SPatrick Kelsey 		     tfo_syn)) {
3045281a0fd4SPatrick Kelsey 			if (DELAY_ACK(tp, tlen) || tfo_syn)
30463bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
3047e4b64281SJesper Skriver 			else
3048e4b64281SJesper Skriver 				tp->t_flags |= TF_ACKNOW;
3049e4b64281SJesper Skriver 			tp->rcv_nxt += tlen;
3050e4b64281SJesper Skriver 			thflags = th->th_flags & TH_FIN;
305178b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpack);
305278b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
30531e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
3054c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
3055c1c36a2cSMike Silbersack 				m_freem(m);
3056c1c36a2cSMike Silbersack 			else
3057651e4e6aSGleb Smirnoff 				sbappendstream_locked(&so->so_rcv, m, 0);
3058e8949f74SAndre Oppermann 			/* NB: sorwakeup_locked() does an implicit unlock. */
30591e4d7da7SRobert Watson 			sorwakeup_locked(so);
3060e4b64281SJesper Skriver 		} else {
3061e8949f74SAndre Oppermann 			/*
3062e8949f74SAndre Oppermann 			 * XXX: Due to the header drop above "th" is
3063e8949f74SAndre Oppermann 			 * theoretically invalid by now.  Fortunately
3064e8949f74SAndre Oppermann 			 * m_adj() doesn't actually frees any mbufs
3065e8949f74SAndre Oppermann 			 * when trimming from the head.
3066e8949f74SAndre Oppermann 			 */
3067e4b64281SJesper Skriver 			thflags = tcp_reass(tp, th, &tlen, m);
3068e4b64281SJesper Skriver 			tp->t_flags |= TF_ACKNOW;
3069e4b64281SJesper Skriver 		}
30703529149eSAndre Oppermann 		if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT))
3071f194524fSAndre Oppermann 			tcp_update_sack_list(tp, save_start, save_start + tlen);
3072302ce8d6SAndre Oppermann #if 0
3073df8bae1dSRodney W. Grimes 		/*
3074df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
3075df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
3076df8bae1dSRodney W. Grimes 		 * buffer size.
3077302ce8d6SAndre Oppermann 		 * XXX: Unused.
3078df8bae1dSRodney W. Grimes 		 */
3079f701e30dSJohn Baldwin 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt))
3080df8bae1dSRodney W. Grimes 			len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
3081f701e30dSJohn Baldwin 		else
3082f701e30dSJohn Baldwin 			len = so->so_rcv.sb_hiwat;
3083302ce8d6SAndre Oppermann #endif
3084df8bae1dSRodney W. Grimes 	} else {
3085df8bae1dSRodney W. Grimes 		m_freem(m);
3086fb59c426SYoshinobu Inoue 		thflags &= ~TH_FIN;
3087df8bae1dSRodney W. Grimes 	}
3088df8bae1dSRodney W. Grimes 
3089df8bae1dSRodney W. Grimes 	/*
3090df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
3091df8bae1dSRodney W. Grimes 	 * that the connection is closing.
3092df8bae1dSRodney W. Grimes 	 */
3093fb59c426SYoshinobu Inoue 	if (thflags & TH_FIN) {
3094df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
3095df8bae1dSRodney W. Grimes 			socantrcvmore(so);
3096a0292f23SGarrett Wollman 			/*
3097a0292f23SGarrett Wollman 			 * If connection is half-synchronized
3098d3eede9dSAndras Olah 			 * (ie NEEDSYN flag on) then delay ACK,
3099a0292f23SGarrett Wollman 			 * so it may be piggybacked when SYN is sent.
3100a0292f23SGarrett Wollman 			 * Otherwise, since we received a FIN then no
3101a0292f23SGarrett Wollman 			 * more input can be expected, send ACK now.
3102a0292f23SGarrett Wollman 			 */
31033bfd6421SJonathan Lemon 			if (tp->t_flags & TF_NEEDSYN)
31043bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
3105a0292f23SGarrett Wollman 			else
3106df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
3107df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
3108df8bae1dSRodney W. Grimes 		}
3109df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
3110df8bae1dSRodney W. Grimes 
3111df8bae1dSRodney W. Grimes 		/*
3112df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
3113df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
3114df8bae1dSRodney W. Grimes 		 */
3115df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
31169b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
31179b8b58e0SJonathan Lemon 			/* FALLTHROUGH */
3118df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
311957f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSE_WAIT);
3120df8bae1dSRodney W. Grimes 			break;
3121df8bae1dSRodney W. Grimes 
3122df8bae1dSRodney W. Grimes 		/*
3123df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
3124df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
3125df8bae1dSRodney W. Grimes 		 */
3126df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
312757f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSING);
3128df8bae1dSRodney W. Grimes 			break;
3129df8bae1dSRodney W. Grimes 
3130df8bae1dSRodney W. Grimes 		/*
3131df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
3132df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
3133df8bae1dSRodney W. Grimes 		 * standard timers.
3134df8bae1dSRodney W. Grimes 		 */
3135df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
3136ff9b006dSJulien Charbon 			INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
3137ff9b006dSJulien Charbon 			KASSERT(ti_locked == TI_RLOCKED, ("%s: dodata "
3138252ca428SRobert Watson 			    "TCP_FIN_WAIT_2 ti_locked: %d", __func__,
3139252ca428SRobert Watson 			    ti_locked));
3140252ca428SRobert Watson 
3141340c35deSJonathan Lemon 			tcp_twstart(tp);
3142ff9b006dSJulien Charbon 			INP_INFO_RUNLOCK(&V_tcbinfo);
3143679d9708SAndre Oppermann 			return;
3144df8bae1dSRodney W. Grimes 		}
3145df8bae1dSRodney W. Grimes 	}
3146ff9b006dSJulien Charbon 	if (ti_locked == TI_RLOCKED)
3147ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
3148252ca428SRobert Watson 	ti_locked = TI_UNLOCKED;
3149252ca428SRobert Watson 
3150610ee2f9SDavid Greenman #ifdef TCPDEBUG
31514cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
3152fb59c426SYoshinobu Inoue 		tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
3153fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3154610ee2f9SDavid Greenman #endif
31555d06879aSGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, mtod(m, const char *));
3156df8bae1dSRodney W. Grimes 
3157df8bae1dSRodney W. Grimes 	/*
3158df8bae1dSRodney W. Grimes 	 * Return any desired output.
3159df8bae1dSRodney W. Grimes 	 */
3160df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
316155bceb1eSRandall Stewart 		(void) tp->t_fb->tfb_tcp_output(tp);
31627792ea27SJeffrey Hsu 
3163a14c749fSJonathan Lemon check_delack:
3164252ca428SRobert Watson 	KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d",
3165252ca428SRobert Watson 	    __func__, ti_locked));
3166603724d3SBjoern A. Zeeb 	INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
31678501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
3168252ca428SRobert Watson 
3169a14c749fSJonathan Lemon 	if (tp->t_flags & TF_DELACK) {
31703bfd6421SJonathan Lemon 		tp->t_flags &= ~TF_DELACK;
3171b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
31723bfd6421SJonathan Lemon 	}
31738501a69cSRobert Watson 	INP_WUNLOCK(tp->t_inpcb);
3174679d9708SAndre Oppermann 	return;
3175df8bae1dSRodney W. Grimes 
3176df8bae1dSRodney W. Grimes dropafterack:
3177df8bae1dSRodney W. Grimes 	/*
3178df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
3179df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
318080ab7c0eSGarrett Wollman 	 *
318180ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
318280ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
318380ab7c0eSGarrett Wollman 	 * RST have been dropped.
318480ab7c0eSGarrett Wollman 	 *
318580ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
318680ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
318780ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
318880ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
318980ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
319080ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
3191df8bae1dSRodney W. Grimes 	 */
3192fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
3193fb59c426SYoshinobu Inoue 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
3194a57815efSBosko Milekic 	     SEQ_GT(th->th_ack, tp->snd_max)) ) {
3195a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
3196a57815efSBosko Milekic 		goto dropwithreset;
3197a57815efSBosko Milekic 	}
3198a0292f23SGarrett Wollman #ifdef TCPDEBUG
31994cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
3200fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
3201fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3202a0292f23SGarrett Wollman #endif
32035d06879aSGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, mtod(m, const char *));
3204ff9b006dSJulien Charbon 	if (ti_locked == TI_RLOCKED)
3205ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
3206252ca428SRobert Watson 	ti_locked = TI_UNLOCKED;
3207252ca428SRobert Watson 
3208df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
320955bceb1eSRandall Stewart 	(void) tp->t_fb->tfb_tcp_output(tp);
32108501a69cSRobert Watson 	INP_WUNLOCK(tp->t_inpcb);
3211d6915262SRobert Watson 	m_freem(m);
3212679d9708SAndre Oppermann 	return;
3213df8bae1dSRodney W. Grimes 
3214df8bae1dSRodney W. Grimes dropwithreset:
3215ff9b006dSJulien Charbon 	if (ti_locked == TI_RLOCKED)
3216ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
3217252ca428SRobert Watson 	ti_locked = TI_UNLOCKED;
3218a57815efSBosko Milekic 
3219a0ca0871SRobert Watson 	if (tp != NULL) {
3220302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
32218501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
3222dd8ac7f9SRobert Watson 	} else
3223a0ca0871SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
3224679d9708SAndre Oppermann 	return;
3225df8bae1dSRodney W. Grimes 
3226df8bae1dSRodney W. Grimes drop:
3227ff9b006dSJulien Charbon 	if (ti_locked == TI_RLOCKED) {
3228ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
3229fa046d87SRobert Watson 		ti_locked = TI_UNLOCKED;
3230fa046d87SRobert Watson 	}
3231252ca428SRobert Watson #ifdef INVARIANTS
3232252ca428SRobert Watson 	else
3233252ca428SRobert Watson 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
3234252ca428SRobert Watson #endif
3235252ca428SRobert Watson 
3236df8bae1dSRodney W. Grimes 	/*
3237df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
3238df8bae1dSRodney W. Grimes 	 */
3239610ee2f9SDavid Greenman #ifdef TCPDEBUG
32401c53f806SRobert Watson 	if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
3241fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
3242fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3243a0292f23SGarrett Wollman #endif
32445d06879aSGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, mtod(m, const char *));
32451c53f806SRobert Watson 	if (tp != NULL)
32468501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
3247d6915262SRobert Watson 	m_freem(m);
3248302ce8d6SAndre Oppermann }
3249302ce8d6SAndre Oppermann 
3250302ce8d6SAndre Oppermann /*
32510ca3f933SAndre Oppermann  * Issue RST and make ACK acceptable to originator of segment.
32520ca3f933SAndre Oppermann  * The mbuf must still include the original packet header.
32530ca3f933SAndre Oppermann  * tp may be NULL.
3254302ce8d6SAndre Oppermann  */
325555bceb1eSRandall Stewart void
3256302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp,
3257302ce8d6SAndre Oppermann     int tlen, int rstreason)
3258302ce8d6SAndre Oppermann {
3259b287c6c7SBjoern A. Zeeb #ifdef INET
3260302ce8d6SAndre Oppermann 	struct ip *ip;
3261b287c6c7SBjoern A. Zeeb #endif
3262302ce8d6SAndre Oppermann #ifdef INET6
3263302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6;
3264302ce8d6SAndre Oppermann #endif
32657a3244ccSRobert Watson 
32667a3244ccSRobert Watson 	if (tp != NULL) {
32678501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
32687a3244ccSRobert Watson 	}
32697a3244ccSRobert Watson 
32700ca3f933SAndre Oppermann 	/* Don't bother if destination was broadcast/multicast. */
3271302ce8d6SAndre Oppermann 	if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
3272302ce8d6SAndre Oppermann 		goto drop;
3273302ce8d6SAndre Oppermann #ifdef INET6
3274302ce8d6SAndre Oppermann 	if (mtod(m, struct ip *)->ip_v == 6) {
3275302ce8d6SAndre Oppermann 		ip6 = mtod(m, struct ip6_hdr *);
3276302ce8d6SAndre Oppermann 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
3277302ce8d6SAndre Oppermann 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
3278302ce8d6SAndre Oppermann 			goto drop;
3279302ce8d6SAndre Oppermann 		/* IPv6 anycast check is done at tcp6_input() */
3280b287c6c7SBjoern A. Zeeb 	}
3281302ce8d6SAndre Oppermann #endif
3282b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3283b287c6c7SBjoern A. Zeeb 	else
3284b287c6c7SBjoern A. Zeeb #endif
3285b287c6c7SBjoern A. Zeeb #ifdef INET
3286302ce8d6SAndre Oppermann 	{
3287302ce8d6SAndre Oppermann 		ip = mtod(m, struct ip *);
3288302ce8d6SAndre Oppermann 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
3289302ce8d6SAndre Oppermann 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
3290302ce8d6SAndre Oppermann 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
3291302ce8d6SAndre Oppermann 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
3292302ce8d6SAndre Oppermann 			goto drop;
3293302ce8d6SAndre Oppermann 	}
3294b287c6c7SBjoern A. Zeeb #endif
3295302ce8d6SAndre Oppermann 
3296302ce8d6SAndre Oppermann 	/* Perform bandwidth limiting. */
3297302ce8d6SAndre Oppermann 	if (badport_bandlim(rstreason) < 0)
3298302ce8d6SAndre Oppermann 		goto drop;
3299302ce8d6SAndre Oppermann 
3300302ce8d6SAndre Oppermann 	/* tcp_respond consumes the mbuf chain. */
3301302ce8d6SAndre Oppermann 	if (th->th_flags & TH_ACK) {
3302302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0,
3303302ce8d6SAndre Oppermann 		    th->th_ack, TH_RST);
3304302ce8d6SAndre Oppermann 	} else {
3305302ce8d6SAndre Oppermann 		if (th->th_flags & TH_SYN)
3306302ce8d6SAndre Oppermann 			tlen++;
3307302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
3308302ce8d6SAndre Oppermann 		    (tcp_seq)0, TH_RST|TH_ACK);
3309302ce8d6SAndre Oppermann 	}
3310302ce8d6SAndre Oppermann 	return;
3311302ce8d6SAndre Oppermann drop:
3312302ce8d6SAndre Oppermann 	m_freem(m);
3313df8bae1dSRodney W. Grimes }
3314df8bae1dSRodney W. Grimes 
3315be2ac88cSJonathan Lemon /*
3316be2ac88cSJonathan Lemon  * Parse TCP options and place in tcpopt.
3317be2ac88cSJonathan Lemon  */
331855bceb1eSRandall Stewart void
3319ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags)
3320df8bae1dSRodney W. Grimes {
3321df8bae1dSRodney W. Grimes 	int opt, optlen;
3322df8bae1dSRodney W. Grimes 
3323be2ac88cSJonathan Lemon 	to->to_flags = 0;
3324df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
3325df8bae1dSRodney W. Grimes 		opt = cp[0];
3326df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
3327df8bae1dSRodney W. Grimes 			break;
3328df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
3329df8bae1dSRodney W. Grimes 			optlen = 1;
3330df8bae1dSRodney W. Grimes 		else {
3331b474779fSJun-ichiro itojun Hagino 			if (cnt < 2)
3332b474779fSJun-ichiro itojun Hagino 				break;
3333df8bae1dSRodney W. Grimes 			optlen = cp[1];
3334b474779fSJun-ichiro itojun Hagino 			if (optlen < 2 || optlen > cnt)
3335df8bae1dSRodney W. Grimes 				break;
3336df8bae1dSRodney W. Grimes 		}
3337df8bae1dSRodney W. Grimes 		switch (opt) {
3338df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
3339df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
3340df8bae1dSRodney W. Grimes 				continue;
3341f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3342df8bae1dSRodney W. Grimes 				continue;
3343be2ac88cSJonathan Lemon 			to->to_flags |= TOF_MSS;
3344be2ac88cSJonathan Lemon 			bcopy((char *)cp + 2,
3345be2ac88cSJonathan Lemon 			    (char *)&to->to_mss, sizeof(to->to_mss));
3346fd8e4ebcSMike Barcroft 			to->to_mss = ntohs(to->to_mss);
3347df8bae1dSRodney W. Grimes 			break;
3348df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
3349df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
3350df8bae1dSRodney W. Grimes 				continue;
3351f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3352df8bae1dSRodney W. Grimes 				continue;
3353be2ac88cSJonathan Lemon 			to->to_flags |= TOF_SCALE;
335402a1a643SAndre Oppermann 			to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT);
3355df8bae1dSRodney W. Grimes 			break;
3356df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
3357df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
3358df8bae1dSRodney W. Grimes 				continue;
3359be2ac88cSJonathan Lemon 			to->to_flags |= TOF_TS;
3360a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
3361a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
3362fd8e4ebcSMike Barcroft 			to->to_tsval = ntohl(to->to_tsval);
3363a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
3364a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
3365fd8e4ebcSMike Barcroft 			to->to_tsecr = ntohl(to->to_tsecr);
3366df8bae1dSRodney W. Grimes 			break;
33671cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
33681cfd4b53SBruce M Simpson 		/*
33691cfd4b53SBruce M Simpson 		 * XXX In order to reply to a host which has set the
33701cfd4b53SBruce M Simpson 		 * TCP_SIGNATURE option in its initial SYN, we have to
33711cfd4b53SBruce M Simpson 		 * record the fact that the option was observed here
33721cfd4b53SBruce M Simpson 		 * for the syncache code to perform the correct response.
33731cfd4b53SBruce M Simpson 		 */
33741cfd4b53SBruce M Simpson 		case TCPOPT_SIGNATURE:
33751cfd4b53SBruce M Simpson 			if (optlen != TCPOLEN_SIGNATURE)
33761cfd4b53SBruce M Simpson 				continue;
3377df47e437SAndre Oppermann 			to->to_flags |= TOF_SIGNATURE;
3378df47e437SAndre Oppermann 			to->to_signature = cp + 2;
33791cfd4b53SBruce M Simpson 			break;
3380265ed012SBruce M Simpson #endif
33816d90faf3SPaul Saab 		case TCPOPT_SACK_PERMITTED:
3382f72167f4SAndre Oppermann 			if (optlen != TCPOLEN_SACK_PERMITTED)
33836d90faf3SPaul Saab 				continue;
3384f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3385f72167f4SAndre Oppermann 				continue;
3386603724d3SBjoern A. Zeeb 			if (!V_tcp_do_sack)
3387f72167f4SAndre Oppermann 				continue;
3388fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACKPERM;
33896d90faf3SPaul Saab 			break;
33906d90faf3SPaul Saab 		case TCPOPT_SACK:
33915a53ca16SPaul Saab 			if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
33926d90faf3SPaul Saab 				continue;
3393b728e902SAndre Oppermann 			if (flags & TO_SYN)
3394b728e902SAndre Oppermann 				continue;
3395fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACK;
33965a53ca16SPaul Saab 			to->to_nsacks = (optlen - 2) / TCPOLEN_SACK;
33975a53ca16SPaul Saab 			to->to_sacks = cp + 2;
339878b50714SRobert Watson 			TCPSTAT_INC(tcps_sack_rcv_blocks);
33996d90faf3SPaul Saab 			break;
3400281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
3401281a0fd4SPatrick Kelsey 		case TCPOPT_FAST_OPEN:
3402281a0fd4SPatrick Kelsey 			if ((optlen != TCPOLEN_FAST_OPEN_EMPTY) &&
3403281a0fd4SPatrick Kelsey 			    (optlen < TCPOLEN_FAST_OPEN_MIN) &&
3404281a0fd4SPatrick Kelsey 			    (optlen > TCPOLEN_FAST_OPEN_MAX))
3405281a0fd4SPatrick Kelsey 				continue;
3406281a0fd4SPatrick Kelsey 			if (!(flags & TO_SYN))
3407281a0fd4SPatrick Kelsey 				continue;
3408281a0fd4SPatrick Kelsey 			if (!V_tcp_fastopen_enabled)
3409281a0fd4SPatrick Kelsey 				continue;
3410281a0fd4SPatrick Kelsey 			to->to_flags |= TOF_FASTOPEN;
3411281a0fd4SPatrick Kelsey 			to->to_tfo_len = optlen - 2;
3412281a0fd4SPatrick Kelsey 			to->to_tfo_cookie = to->to_tfo_len ? cp + 2 : NULL;
3413281a0fd4SPatrick Kelsey 			break;
3414281a0fd4SPatrick Kelsey #endif
3415be2ac88cSJonathan Lemon 		default:
3416be2ac88cSJonathan Lemon 			continue;
3417df8bae1dSRodney W. Grimes 		}
3418df8bae1dSRodney W. Grimes 	}
3419df8bae1dSRodney W. Grimes }
3420df8bae1dSRodney W. Grimes 
3421df8bae1dSRodney W. Grimes /*
3422df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
3423df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
3424df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
3425df8bae1dSRodney W. Grimes  * sequencing purposes.
3426df8bae1dSRodney W. Grimes  */
342755bceb1eSRandall Stewart void
3428ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m,
3429ad3f9ab3SAndre Oppermann     int off)
3430df8bae1dSRodney W. Grimes {
3431fb59c426SYoshinobu Inoue 	int cnt = off + th->th_urp - 1;
3432df8bae1dSRodney W. Grimes 
3433df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
3434df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
3435df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
3436df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
3437df8bae1dSRodney W. Grimes 
34388501a69cSRobert Watson 			INP_WLOCK_ASSERT(tp->t_inpcb);
34397a3244ccSRobert Watson 
3440df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
3441df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
3442df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
3443df8bae1dSRodney W. Grimes 			m->m_len--;
344469a34685SYoshinobu Inoue 			if (m->m_flags & M_PKTHDR)
344569a34685SYoshinobu Inoue 				m->m_pkthdr.len--;
3446df8bae1dSRodney W. Grimes 			return;
3447df8bae1dSRodney W. Grimes 		}
3448df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
3449df8bae1dSRodney W. Grimes 		m = m->m_next;
34504dfdffe9SAndre Oppermann 		if (m == NULL)
3451df8bae1dSRodney W. Grimes 			break;
3452df8bae1dSRodney W. Grimes 	}
3453df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
3454df8bae1dSRodney W. Grimes }
3455df8bae1dSRodney W. Grimes 
3456df8bae1dSRodney W. Grimes /*
3457df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
3458df8bae1dSRodney W. Grimes  * and update averages and current timeout.
3459df8bae1dSRodney W. Grimes  */
346055bceb1eSRandall Stewart void
3461ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt)
3462df8bae1dSRodney W. Grimes {
3463ad3f9ab3SAndre Oppermann 	int delta;
3464233e8c18SGarrett Wollman 
34658501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
34662be3bf22SRobert Watson 
346778b50714SRobert Watson 	TCPSTAT_INC(tcps_rttupdated);
3468233e8c18SGarrett Wollman 	tp->t_rttupdated++;
3469233e8c18SGarrett Wollman 	if (tp->t_srtt != 0) {
3470233e8c18SGarrett Wollman 		/*
3471233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
3472233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
3473233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
3474233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
3475233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
3476233e8c18SGarrett Wollman 		 */
3477233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
3478233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
3479233e8c18SGarrett Wollman 
3480233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
3481233e8c18SGarrett Wollman 			tp->t_srtt = 1;
3482233e8c18SGarrett Wollman 
3483233e8c18SGarrett Wollman 		/*
3484233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
3485233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
3486233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
3487233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
3488233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
3489233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
3490233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
3491233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
3492233e8c18SGarrett Wollman 		 */
3493233e8c18SGarrett Wollman 		if (delta < 0)
3494233e8c18SGarrett Wollman 			delta = -delta;
3495233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
3496233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
3497233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
34981fcc99b5SMatthew Dillon 		if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
34991fcc99b5SMatthew Dillon 		    tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3500233e8c18SGarrett Wollman 	} else {
3501233e8c18SGarrett Wollman 		/*
3502233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
3503233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
3504233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
3505233e8c18SGarrett Wollman 		 */
3506233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
3507233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
35081fcc99b5SMatthew Dillon 		tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3509233e8c18SGarrett Wollman 	}
35109b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
3511df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
3512df8bae1dSRodney W. Grimes 
3513df8bae1dSRodney W. Grimes 	/*
3514df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
3515df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
3516df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
3517df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
3518df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
3519df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
3520df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
3521df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
3522df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
3523df8bae1dSRodney W. Grimes 	 */
3524233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
35259e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
3526df8bae1dSRodney W. Grimes 
3527df8bae1dSRodney W. Grimes 	/*
3528df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
3529df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
3530df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
3531df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
3532df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
3533df8bae1dSRodney W. Grimes 	 */
3534df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
3535df8bae1dSRodney W. Grimes }
3536df8bae1dSRodney W. Grimes 
3537df8bae1dSRodney W. Grimes /*
3538df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
3539df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
35404faaea55SAndre Oppermann  * If none, use an mss that can be handled on the outgoing interface
35414faaea55SAndre Oppermann  * without forcing IP to fragment.  If no route is found, route has no mtu,
3542df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
3543df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
3544df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
3545df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
3546df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
3547df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
3548df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
3549a0292f23SGarrett Wollman  *
35500c39d38dSGleb Smirnoff  * NOTE that resulting t_maxseg doesn't include space for TCP options or
35510c39d38dSGleb Smirnoff  * IP options, e.g. IPSEC data, since length of this data may vary, and
35520c39d38dSGleb Smirnoff  * thus it is calculated for every segment separately in tcp_output().
3553a0292f23SGarrett Wollman  *
355497d8d152SAndre Oppermann  * NOTE that this routine is only called when we process an incoming
3555ef341ee1SGleb Smirnoff  * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS
3556ef341ee1SGleb Smirnoff  * settings are handled in tcp_mssopt().
3557df8bae1dSRodney W. Grimes  */
3558a0292f23SGarrett Wollman void
3559ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer,
35603c914c54SAndre Oppermann     struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap)
3561df8bae1dSRodney W. Grimes {
3562b287c6c7SBjoern A. Zeeb 	int mss = 0;
3563*3ac12506SJonathan T. Looney 	uint32_t maxmtu = 0;
3564c068736aSJeffrey Hsu 	struct inpcb *inp = tp->t_inpcb;
356597d8d152SAndre Oppermann 	struct hc_metrics_lite metrics;
3566fb59c426SYoshinobu Inoue #ifdef INET6
3567c068736aSJeffrey Hsu 	int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
3568c068736aSJeffrey Hsu 	size_t min_protoh = isipv6 ?
3569c068736aSJeffrey Hsu 			    sizeof (struct ip6_hdr) + sizeof (struct tcphdr) :
3570c068736aSJeffrey Hsu 			    sizeof (struct tcpiphdr);
3571c068736aSJeffrey Hsu #else
3572c068736aSJeffrey Hsu 	const size_t min_protoh = sizeof(struct tcpiphdr);
3573fb59c426SYoshinobu Inoue #endif
3574df8bae1dSRodney W. Grimes 
35758501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
35767a3244ccSRobert Watson 
3577ef341ee1SGleb Smirnoff 	if (mtuoffer != -1) {
3578ef341ee1SGleb Smirnoff 		KASSERT(offer == -1, ("%s: conflict", __func__));
3579ef341ee1SGleb Smirnoff 		offer = mtuoffer - min_protoh;
3580ef341ee1SGleb Smirnoff 	}
3581ef341ee1SGleb Smirnoff 
3582e8949f74SAndre Oppermann 	/* Initialize. */
358397d8d152SAndre Oppermann #ifdef INET6
358497d8d152SAndre Oppermann 	if (isipv6) {
35853c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu6(&inp->inp_inc, cap);
35860c39d38dSGleb Smirnoff 		tp->t_maxseg = V_tcp_v6mssdflt;
3587b287c6c7SBjoern A. Zeeb 	}
358897d8d152SAndre Oppermann #endif
3589b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3590b287c6c7SBjoern A. Zeeb 	else
3591b287c6c7SBjoern A. Zeeb #endif
3592b287c6c7SBjoern A. Zeeb #ifdef INET
359397d8d152SAndre Oppermann 	{
35943c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu(&inp->inp_inc, cap);
35950c39d38dSGleb Smirnoff 		tp->t_maxseg = V_tcp_mssdflt;
3596df8bae1dSRodney W. Grimes 	}
3597b287c6c7SBjoern A. Zeeb #endif
3598df8bae1dSRodney W. Grimes 
359997d8d152SAndre Oppermann 	/*
3600e8949f74SAndre Oppermann 	 * No route to sender, stay with default mss and return.
360197d8d152SAndre Oppermann 	 */
36026f01cac6SBjoern A. Zeeb 	if (maxmtu == 0) {
36036f01cac6SBjoern A. Zeeb 		/*
36048e5c87f4SBjoern A. Zeeb 		 * In case we return early we need to initialize metrics
36056f01cac6SBjoern A. Zeeb 		 * to a defined state as tcp_hc_get() would do for us
36066f01cac6SBjoern A. Zeeb 		 * if there was no cache hit.
36076f01cac6SBjoern A. Zeeb 		 */
36086f01cac6SBjoern A. Zeeb 		if (metricptr != NULL)
36096f01cac6SBjoern A. Zeeb 			bzero(metricptr, sizeof(struct hc_metrics_lite));
361097d8d152SAndre Oppermann 		return;
36116f01cac6SBjoern A. Zeeb 	}
361297d8d152SAndre Oppermann 
3613e8949f74SAndre Oppermann 	/* What have we got? */
361497d8d152SAndre Oppermann 	switch (offer) {
361597d8d152SAndre Oppermann 		case 0:
361697d8d152SAndre Oppermann 			/*
361797d8d152SAndre Oppermann 			 * Offer == 0 means that there was no MSS on the SYN
3618c3b02504SBjoern A. Zeeb 			 * segment, in this case we use tcp_mssdflt as
36190c39d38dSGleb Smirnoff 			 * already assigned to t_maxseg above.
362097d8d152SAndre Oppermann 			 */
36210c39d38dSGleb Smirnoff 			offer = tp->t_maxseg;
362297d8d152SAndre Oppermann 			break;
362397d8d152SAndre Oppermann 
362497d8d152SAndre Oppermann 		case -1:
3625a0292f23SGarrett Wollman 			/*
3626c94c54e4SAndre Oppermann 			 * Offer == -1 means that we didn't receive SYN yet.
3627a0292f23SGarrett Wollman 			 */
362897d8d152SAndre Oppermann 			/* FALLTHROUGH */
362997d8d152SAndre Oppermann 
363097d8d152SAndre Oppermann 		default:
3631a0292f23SGarrett Wollman 			/*
363253369ac9SAndre Oppermann 			 * Prevent DoS attack with too small MSS. Round up
363353369ac9SAndre Oppermann 			 * to at least minmss.
363453369ac9SAndre Oppermann 			 */
3635603724d3SBjoern A. Zeeb 			offer = max(offer, V_tcp_minmss);
363697d8d152SAndre Oppermann 	}
3637a0292f23SGarrett Wollman 
3638df8bae1dSRodney W. Grimes 	/*
3639e8949f74SAndre Oppermann 	 * rmx information is now retrieved from tcp_hostcache.
3640df8bae1dSRodney W. Grimes 	 */
364197d8d152SAndre Oppermann 	tcp_hc_get(&inp->inp_inc, &metrics);
36423cee92e0SBjoern A. Zeeb 	if (metricptr != NULL)
36433cee92e0SBjoern A. Zeeb 		bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite));
364497d8d152SAndre Oppermann 
3645df8bae1dSRodney W. Grimes 	/*
3646cc412412SHiren Panchasara 	 * If there's a discovered mtu in tcp hostcache, use it.
3647cc412412SHiren Panchasara 	 * Else, use the link mtu.
3648df8bae1dSRodney W. Grimes 	 */
364997d8d152SAndre Oppermann 	if (metrics.rmx_mtu)
36502d166c02SAndre Oppermann 		mss = min(metrics.rmx_mtu, maxmtu) - min_protoh;
3651c068736aSJeffrey Hsu 	else {
365231b3783cSHajimu UMEMOTO #ifdef INET6
3653fb59c426SYoshinobu Inoue 		if (isipv6) {
365497d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3655603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
365697d8d152SAndre Oppermann 			    !in6_localaddr(&inp->in6p_faddr))
3657603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_v6mssdflt);
3658b287c6c7SBjoern A. Zeeb 		}
3659b3399803SHajimu UMEMOTO #endif
3660b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3661b287c6c7SBjoern A. Zeeb 		else
3662b287c6c7SBjoern A. Zeeb #endif
3663b287c6c7SBjoern A. Zeeb #ifdef INET
366497d8d152SAndre Oppermann 		{
366597d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3666603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
366797d8d152SAndre Oppermann 			    !in_localaddr(inp->inp_faddr))
3668603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_mssdflt);
3669a0292f23SGarrett Wollman 		}
3670b287c6c7SBjoern A. Zeeb #endif
36713cee92e0SBjoern A. Zeeb 		/*
36723cee92e0SBjoern A. Zeeb 		 * XXX - The above conditional (mss = maxmtu - min_protoh)
36733cee92e0SBjoern A. Zeeb 		 * probably violates the TCP spec.
36743cee92e0SBjoern A. Zeeb 		 * The problem is that, since we don't know the
36753cee92e0SBjoern A. Zeeb 		 * other end's MSS, we are supposed to use a conservative
36763cee92e0SBjoern A. Zeeb 		 * default.  But, if we do that, then MTU discovery will
36773cee92e0SBjoern A. Zeeb 		 * never actually take place, because the conservative
36783cee92e0SBjoern A. Zeeb 		 * default is much less than the MTUs typically seen
36793cee92e0SBjoern A. Zeeb 		 * on the Internet today.  For the moment, we'll sweep
36803cee92e0SBjoern A. Zeeb 		 * this under the carpet.
36813cee92e0SBjoern A. Zeeb 		 *
36823cee92e0SBjoern A. Zeeb 		 * The conservative default might not actually be a problem
36833cee92e0SBjoern A. Zeeb 		 * if the only case this occurs is when sending an initial
36843cee92e0SBjoern A. Zeeb 		 * SYN with options and data to a host we've never talked
36853cee92e0SBjoern A. Zeeb 		 * to before.  Then, they will reply with an MSS value which
36863cee92e0SBjoern A. Zeeb 		 * will get recorded and the new parameters should get
36873cee92e0SBjoern A. Zeeb 		 * recomputed.  For Further Study.
36883cee92e0SBjoern A. Zeeb 		 */
368997d8d152SAndre Oppermann 	}
3690a0292f23SGarrett Wollman 	mss = min(mss, offer);
369197d8d152SAndre Oppermann 
3692a0292f23SGarrett Wollman 	/*
36930c39d38dSGleb Smirnoff 	 * Sanity check: make sure that maxseg will be large
36943cee92e0SBjoern A. Zeeb 	 * enough to allow some data on segments even if the
36953cee92e0SBjoern A. Zeeb 	 * all the option space is used (40bytes).  Otherwise
36963cee92e0SBjoern A. Zeeb 	 * funny things may happen in tcp_output.
36970c39d38dSGleb Smirnoff 	 *
36980c39d38dSGleb Smirnoff 	 * XXXGL: shouldn't we reserve space for IP/IPv6 options?
36993cee92e0SBjoern A. Zeeb 	 */
37003cee92e0SBjoern A. Zeeb 	mss = max(mss, 64);
37013cee92e0SBjoern A. Zeeb 
370297d8d152SAndre Oppermann 	tp->t_maxseg = mss;
37033cee92e0SBjoern A. Zeeb }
37043cee92e0SBjoern A. Zeeb 
37053cee92e0SBjoern A. Zeeb void
37063cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer)
37073cee92e0SBjoern A. Zeeb {
3708dbc42409SLawrence Stewart 	int mss;
3709*3ac12506SJonathan T. Looney 	uint32_t bufsize;
37103cee92e0SBjoern A. Zeeb 	struct inpcb *inp;
37113cee92e0SBjoern A. Zeeb 	struct socket *so;
37123cee92e0SBjoern A. Zeeb 	struct hc_metrics_lite metrics;
37133c914c54SAndre Oppermann 	struct tcp_ifcap cap;
3714dbc42409SLawrence Stewart 
37153cee92e0SBjoern A. Zeeb 	KASSERT(tp != NULL, ("%s: tp == NULL", __func__));
37163cee92e0SBjoern A. Zeeb 
37173c914c54SAndre Oppermann 	bzero(&cap, sizeof(cap));
37183c914c54SAndre Oppermann 	tcp_mss_update(tp, offer, -1, &metrics, &cap);
37193cee92e0SBjoern A. Zeeb 
37203cee92e0SBjoern A. Zeeb 	mss = tp->t_maxseg;
37213cee92e0SBjoern A. Zeeb 	inp = tp->t_inpcb;
372297d8d152SAndre Oppermann 
3723df8bae1dSRodney W. Grimes 	/*
372497d8d152SAndre Oppermann 	 * If there's a pipesize, change the socket buffer to that size,
372597d8d152SAndre Oppermann 	 * don't change if sb_hiwat is different than default (then it
372697d8d152SAndre Oppermann 	 * has been changed on purpose with setsockopt).
372797d8d152SAndre Oppermann 	 * Make the socket buffers an integral number of mss units;
372897d8d152SAndre Oppermann 	 * if the mss is larger than the socket buffer, decrease the mss.
3729df8bae1dSRodney W. Grimes 	 */
3730c3b02504SBjoern A. Zeeb 	so = inp->inp_socket;
37313f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
3732e233e2acSAndre Oppermann 	if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe)
373397d8d152SAndre Oppermann 		bufsize = metrics.rmx_sendpipe;
373497d8d152SAndre Oppermann 	else
3735df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
3736df8bae1dSRodney W. Grimes 	if (bufsize < mss)
3737df8bae1dSRodney W. Grimes 		mss = bufsize;
3738df8bae1dSRodney W. Grimes 	else {
3739df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3740df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3741df8bae1dSRodney W. Grimes 			bufsize = sb_max;
374288c39af3SRuslan Ermilov 		if (bufsize > so->so_snd.sb_hiwat)
37433f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_snd, bufsize, so, NULL);
3744df8bae1dSRodney W. Grimes 	}
37453f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
3746df8bae1dSRodney W. Grimes 	tp->t_maxseg = mss;
3747df8bae1dSRodney W. Grimes 
37483f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
3749e233e2acSAndre Oppermann 	if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe)
375097d8d152SAndre Oppermann 		bufsize = metrics.rmx_recvpipe;
375197d8d152SAndre Oppermann 	else
3752df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
3753df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
3754df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3755df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3756df8bae1dSRodney W. Grimes 			bufsize = sb_max;
375788c39af3SRuslan Ermilov 		if (bufsize > so->so_rcv.sb_hiwat)
37583f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL);
3759df8bae1dSRodney W. Grimes 	}
37603f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
376191d6cfa6SBjoern A. Zeeb 
376291d6cfa6SBjoern A. Zeeb 	/* Check the interface for TSO capabilities. */
37633c914c54SAndre Oppermann 	if (cap.ifcap & CSUM_TSO) {
376491d6cfa6SBjoern A. Zeeb 		tp->t_flags |= TF_TSO;
37653c914c54SAndre Oppermann 		tp->t_tsomax = cap.tsomax;
37669fd573c3SHans Petter Selasky 		tp->t_tsomaxsegcount = cap.tsomaxsegcount;
37679fd573c3SHans Petter Selasky 		tp->t_tsomaxsegsize = cap.tsomaxsegsize;
37683c914c54SAndre Oppermann 	}
3769a0292f23SGarrett Wollman }
3770a0292f23SGarrett Wollman 
3771a0292f23SGarrett Wollman /*
3772a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
3773a0292f23SGarrett Wollman  */
3774a0292f23SGarrett Wollman int
3775ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc)
3776a0292f23SGarrett Wollman {
377797d8d152SAndre Oppermann 	int mss = 0;
3778*3ac12506SJonathan T. Looney 	uint32_t thcmtu = 0;
3779*3ac12506SJonathan T. Looney 	uint32_t maxmtu = 0;
378097d8d152SAndre Oppermann 	size_t min_protoh;
3781a0292f23SGarrett Wollman 
378297d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer"));
3783a0292f23SGarrett Wollman 
378431b3783cSHajimu UMEMOTO #ifdef INET6
3785dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
3786603724d3SBjoern A. Zeeb 		mss = V_tcp_v6mssdflt;
3787233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu6(inc, NULL);
378897d8d152SAndre Oppermann 		min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
3789b287c6c7SBjoern A. Zeeb 	}
379031b3783cSHajimu UMEMOTO #endif
3791b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3792b287c6c7SBjoern A. Zeeb 	else
3793b287c6c7SBjoern A. Zeeb #endif
3794b287c6c7SBjoern A. Zeeb #ifdef INET
379597d8d152SAndre Oppermann 	{
3796603724d3SBjoern A. Zeeb 		mss = V_tcp_mssdflt;
3797233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu(inc, NULL);
379897d8d152SAndre Oppermann 		min_protoh = sizeof(struct tcpiphdr);
379997d8d152SAndre Oppermann 	}
3800b287c6c7SBjoern A. Zeeb #endif
38013a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET)
38023a9391deSBjoern A. Zeeb 	thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
38033a9391deSBjoern A. Zeeb #endif
38043a9391deSBjoern A. Zeeb 
380597d8d152SAndre Oppermann 	if (maxmtu && thcmtu)
380697d8d152SAndre Oppermann 		mss = min(maxmtu, thcmtu) - min_protoh;
380797d8d152SAndre Oppermann 	else if (maxmtu || thcmtu)
380897d8d152SAndre Oppermann 		mss = max(maxmtu, thcmtu) - min_protoh;
380997d8d152SAndre Oppermann 
381097d8d152SAndre Oppermann 	return (mss);
3811df8bae1dSRodney W. Grimes }
381246f58482SJonathan Lemon 
381346f58482SJonathan Lemon 
381446f58482SJonathan Lemon /*
3815c068736aSJeffrey Hsu  * On a partial ack arrives, force the retransmission of the
3816c068736aSJeffrey Hsu  * next unacknowledged segment.  Do not clear tp->t_dupacks.
3817c068736aSJeffrey Hsu  * By setting snd_nxt to ti_ack, this forces retransmission timer to
3818c068736aSJeffrey Hsu  * be started again.
381946f58482SJonathan Lemon  */
382055bceb1eSRandall Stewart void
3821ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th)
382246f58482SJonathan Lemon {
382346f58482SJonathan Lemon 	tcp_seq onxt = tp->snd_nxt;
3824*3ac12506SJonathan T. Looney 	uint32_t ocwnd = tp->snd_cwnd;
38250c39d38dSGleb Smirnoff 	u_int maxseg = tcp_maxseg(tp);
382646f58482SJonathan Lemon 
38278501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
38287a3244ccSRobert Watson 
3829b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_REXMT, 0);
383046f58482SJonathan Lemon 	tp->t_rtttime = 0;
383146f58482SJonathan Lemon 	tp->snd_nxt = th->th_ack;
38326b2a5f92SJayanth Vijayaraghavan 	/*
3833c068736aSJeffrey Hsu 	 * Set snd_cwnd to one segment beyond acknowledged offset.
3834c068736aSJeffrey Hsu 	 * (tp->snd_una has not yet been updated when this function is called.)
38356b2a5f92SJayanth Vijayaraghavan 	 */
38360c39d38dSGleb Smirnoff 	tp->snd_cwnd = maxseg + BYTES_THIS_ACK(tp, th);
3837a84db8f4SMatthew Dillon 	tp->t_flags |= TF_ACKNOW;
383855bceb1eSRandall Stewart 	(void) tp->t_fb->tfb_tcp_output(tp);
383946f58482SJonathan Lemon 	tp->snd_cwnd = ocwnd;
384046f58482SJonathan Lemon 	if (SEQ_GT(onxt, tp->snd_nxt))
384146f58482SJonathan Lemon 		tp->snd_nxt = onxt;
384246f58482SJonathan Lemon 	/*
384346f58482SJonathan Lemon 	 * Partial window deflation.  Relies on fact that tp->snd_una
384446f58482SJonathan Lemon 	 * not updated yet.
384546f58482SJonathan Lemon 	 */
3846dbc42409SLawrence Stewart 	if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th))
3847dbc42409SLawrence Stewart 		tp->snd_cwnd -= BYTES_THIS_ACK(tp, th);
3848d7587117SPaul Saab 	else
3849d7587117SPaul Saab 		tp->snd_cwnd = 0;
38500c39d38dSGleb Smirnoff 	tp->snd_cwnd += maxseg;
385146f58482SJonathan Lemon }
385212eeb81fSHiren Panchasara 
385312eeb81fSHiren Panchasara int
385412eeb81fSHiren Panchasara tcp_compute_pipe(struct tcpcb *tp)
385512eeb81fSHiren Panchasara {
385612eeb81fSHiren Panchasara 	return (tp->snd_max - tp->snd_una +
385712eeb81fSHiren Panchasara 		tp->sackhint.sack_bytes_rexmit -
385812eeb81fSHiren Panchasara 		tp->sackhint.sacked_bytes);
385912eeb81fSHiren Panchasara }
3860