xref: /freebsd/sys/netinet/tcp_input.c (revision 8db239dc6b88b9f751311c5b9ac7992f0a9f639b)
1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4e79adb8eSGarrett Wollman  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6dbc42409SLawrence Stewart  * Copyright (c) 2007-2008,2010
7dbc42409SLawrence Stewart  *	Swinburne University of Technology, Melbourne, Australia.
8dbc42409SLawrence Stewart  * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org>
9dbc42409SLawrence Stewart  * Copyright (c) 2010 The FreeBSD Foundation
10fa046d87SRobert Watson  * Copyright (c) 2010-2011 Juniper Networks, Inc.
11dbc42409SLawrence Stewart  * All rights reserved.
12dbc42409SLawrence Stewart  *
13dbc42409SLawrence Stewart  * Portions of this software were developed at the Centre for Advanced Internet
14891b8ed4SLawrence Stewart  * Architectures, Swinburne University of Technology, by Lawrence Stewart,
15891b8ed4SLawrence Stewart  * James Healy and David Hayes, made possible in part by a grant from the Cisco
16891b8ed4SLawrence Stewart  * University Research Program Fund at Community Foundation Silicon Valley.
17dbc42409SLawrence Stewart  *
18dbc42409SLawrence Stewart  * Portions of this software were developed at the Centre for Advanced
19dbc42409SLawrence Stewart  * Internet Architectures, Swinburne University of Technology, Melbourne,
20dbc42409SLawrence Stewart  * Australia by David Hayes under sponsorship from the FreeBSD Foundation.
21df8bae1dSRodney W. Grimes  *
22fa046d87SRobert Watson  * Portions of this software were developed by Robert N. M. Watson under
23fa046d87SRobert Watson  * contract to Juniper Networks, Inc.
24fa046d87SRobert Watson  *
25df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
26df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
27df8bae1dSRodney W. Grimes  * are met:
28df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
29df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
30df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
31df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
32df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
33fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
34df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
35df8bae1dSRodney W. Grimes  *    without specific prior written permission.
36df8bae1dSRodney W. Grimes  *
37df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
38df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
41df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
48df8bae1dSRodney W. Grimes  *
49e79adb8eSGarrett Wollman  *	@(#)tcp_input.c	8.12 (Berkeley) 5/24/95
50df8bae1dSRodney W. Grimes  */
51df8bae1dSRodney W. Grimes 
524b421e2dSMike Silbersack #include <sys/cdefs.h>
534b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
544b421e2dSMike Silbersack 
551cfd4b53SBruce M Simpson #include "opt_inet.h"
56fb59c426SYoshinobu Inoue #include "opt_inet6.h"
573a2a9f79SYoshinobu Inoue #include "opt_ipsec.h"
580cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
590cc12cc5SJoerg Wunsch 
60df8bae1dSRodney W. Grimes #include <sys/param.h>
6198163b98SPoul-Henning Kamp #include <sys/kernel.h>
62bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
6339bc9de5SLawrence Stewart #include <sys/hhook.h>
64bd79708dSJonathan T. Looney #endif
65df8bae1dSRodney W. Grimes #include <sys/malloc.h>
66df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
67a29f300eSGarrett Wollman #include <sys/proc.h>		/* for proc0 declaration */
68df8bae1dSRodney W. Grimes #include <sys/protosw.h>
6957f60867SMark Johnston #include <sys/sdt.h>
70960ed29cSSeigo Tanimura #include <sys/signalvar.h>
71df8bae1dSRodney W. Grimes #include <sys/socket.h>
72df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
73960ed29cSSeigo Tanimura #include <sys/sysctl.h>
74816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
75960ed29cSSeigo Tanimura #include <sys/systm.h>
76df8bae1dSRodney W. Grimes 
77e79adb8eSGarrett Wollman #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
78e79adb8eSGarrett Wollman 
7912e2e970SAndre Oppermann #include <vm/uma.h>
8012e2e970SAndre Oppermann 
81df8bae1dSRodney W. Grimes #include <net/if.h>
8276039bc8SGleb Smirnoff #include <net/if_var.h>
83df8bae1dSRodney W. Grimes #include <net/route.h>
84530c0060SRobert Watson #include <net/vnet.h>
85df8bae1dSRodney W. Grimes 
86773673c1SAndre Oppermann #define TCPSTATES		/* for logging */
87773673c1SAndre Oppermann 
88df8bae1dSRodney W. Grimes #include <netinet/in.h>
8957f60867SMark Johnston #include <netinet/in_kdtrace.h>
90960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
91df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
92df8bae1dSRodney W. Grimes #include <netinet/ip.h>
938e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h>	/* required for icmp_var.h */
94686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM */
95df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
96ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
97686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
98686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
99686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
100b66d74c1SGleb Smirnoff #include <netinet6/in6_var.h>
101960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h>
102960ed29cSSeigo Tanimura #include <netinet6/nd6.h>
1032de3e790SGleb Smirnoff #include <netinet/tcp.h>
104df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
1052529f56eSJonathan T. Looney #include <netinet/tcp_log_buf.h>
106df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
107df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
108df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
109fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h>
110df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
1114644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
112c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h>
11386a996e6SHiren Panchasara #ifdef TCPPCAP
11486a996e6SHiren Panchasara #include <netinet/tcp_pcap.h>
11586a996e6SHiren Panchasara #endif
116c325962bSMike Silbersack #include <netinet/tcp_syncache.h>
117610ee2f9SDavid Greenman #ifdef TCPDEBUG
118df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
119fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */
12009fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
12109fe6320SNavdeep Parhar #include <netinet/tcp_offload.h>
12209fe6320SNavdeep Parhar #endif
123fb59c426SYoshinobu Inoue 
124fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
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;
1496df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_VNET | CTLFLAG_RW,
150eddfbb76SRobert Watson     &VNET_NAME(drop_synfin), 0,
151eddfbb76SRobert Watson     "Drop TCP packets with SYN+FIN set");
152f8613305SDag-Erling Smørgrav 
15312eeb81fSHiren Panchasara VNET_DEFINE(int, tcp_do_rfc6675_pipe) = 0;
154054d38e3SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc6675_pipe, CTLFLAG_VNET | CTLFLAG_RW,
15512eeb81fSHiren Panchasara     &VNET_NAME(tcp_do_rfc6675_pipe), 0,
15612eeb81fSHiren Panchasara     "Use calculated pipe/in-flight bytes per RFC 6675");
15712eeb81fSHiren Panchasara 
15882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3042) = 1;
1596df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_VNET | CTLFLAG_RW,
160eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3042), 0,
161eddfbb76SRobert Watson     "Enable RFC 3042 (Limited Transmit)");
162582a954bSJeffrey Hsu 
16382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3390) = 1;
1646df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_VNET | CTLFLAG_RW,
165eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3390), 0,
166da3a8a1aSJeffrey Hsu     "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)");
167da3a8a1aSJeffrey Hsu 
168356c7958SHiren Panchasara VNET_DEFINE(int, tcp_initcwnd_segments) = 10;
169356c7958SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, initcwnd_segments,
170356c7958SHiren Panchasara     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_initcwnd_segments), 0,
171356c7958SHiren Panchasara     "Slow-start flight size (initial congestion window) in number of segments");
17209440655SAndre Oppermann 
17382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3465) = 1;
1746df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_VNET | CTLFLAG_RW,
175eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3465), 0,
17624cb0f22SLawrence Stewart     "Enable RFC 3465 (Appropriate Byte Counting)");
177eddfbb76SRobert Watson 
17882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_abc_l_var) = 2;
1796df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_VNET | CTLFLAG_RW,
180eddfbb76SRobert Watson     &VNET_NAME(tcp_abc_l_var), 2,
18124cb0f22SLawrence Stewart     "Cap the max cwnd increment during slow-start to this number of segments");
18224cb0f22SLawrence Stewart 
1836472ac3dSEd Schouten static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, CTLFLAG_RW, 0, "TCP ECN");
184f2512ba1SRui Paulo 
185883054b4SDon Lewis VNET_DEFINE(int, tcp_do_ecn) = 2;
1866df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW,
187eddfbb76SRobert Watson     &VNET_NAME(tcp_do_ecn), 0,
188eddfbb76SRobert Watson     "TCP ECN support");
189eddfbb76SRobert Watson 
19082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_ecn_maxretries) = 1;
1916df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, maxretries, CTLFLAG_VNET | CTLFLAG_RW,
192eddfbb76SRobert Watson     &VNET_NAME(tcp_ecn_maxretries), 0,
193eddfbb76SRobert Watson     "Max retries before giving up on ECN");
194eddfbb76SRobert Watson 
1953220a212SGleb Smirnoff VNET_DEFINE(int, tcp_insecure_syn) = 0;
1966df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_syn, CTLFLAG_VNET | CTLFLAG_RW,
1973220a212SGleb Smirnoff     &VNET_NAME(tcp_insecure_syn), 0,
1983220a212SGleb Smirnoff     "Follow RFC793 instead of RFC5961 criteria for accepting SYN packets");
1993220a212SGleb Smirnoff 
20082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_insecure_rst) = 0;
2016df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_VNET | CTLFLAG_RW,
202eddfbb76SRobert Watson     &VNET_NAME(tcp_insecure_rst), 0,
2033220a212SGleb Smirnoff     "Follow RFC793 instead of RFC5961 criteria for accepting RST packets");
204a69968eeSMike Silbersack 
205fba0cea1SBjoern A. Zeeb VNET_DEFINE(int, tcp_recvspace) = 1024*64;
206873789cbSAndre Oppermann #define	V_tcp_recvspace	VNET(tcp_recvspace)
2076df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_VNET | CTLFLAG_RW,
208873789cbSAndre Oppermann     &VNET_NAME(tcp_recvspace), 0, "Initial receive socket buffer size");
209873789cbSAndre Oppermann 
21082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autorcvbuf) = 1;
2116df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_VNET | CTLFLAG_RW,
212eddfbb76SRobert Watson     &VNET_NAME(tcp_do_autorcvbuf), 0,
2138b615593SMarko Zec     "Enable automatic receive buffer sizing");
2146741ecf5SAndre Oppermann 
21582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_inc) = 16*1024;
2166df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_VNET | CTLFLAG_RW,
217eddfbb76SRobert Watson     &VNET_NAME(tcp_autorcvbuf_inc), 0,
2186489fe65SAndre Oppermann     "Incrementor step size of automatic receive buffer");
2196741ecf5SAndre Oppermann 
220b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024;
2216df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_VNET | CTLFLAG_RW,
222eddfbb76SRobert Watson     &VNET_NAME(tcp_autorcvbuf_max), 0,
2238b615593SMarko Zec     "Max size of automatic receive buffer");
2246741ecf5SAndre Oppermann 
225eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, tcb);
22644e33a07SMarko Zec #define	tcb6	tcb  /* for KAME src sync over BSD*'s */
22782cea7e6SBjoern A. Zeeb VNET_DEFINE(struct inpcbinfo, tcbinfo);
228df8bae1dSRodney W. Grimes 
229315e3e38SRobert Watson /*
230bf840a17SGleb Smirnoff  * TCP statistics are stored in an array of counter(9)s, which size matches
231bf840a17SGleb Smirnoff  * size of struct tcpstat.  TCP running connection count is a regular array.
2325923c293SGleb Smirnoff  */
2335da0521fSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct tcpstat, tcpstat);
2345da0521fSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_tcp, TCPCTL_STATS, stats, struct tcpstat,
2355da0521fSAndrey V. Elsukov     tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
236bf840a17SGleb Smirnoff VNET_DEFINE(counter_u64_t, tcps_states[TCP_NSTATES]);
237bf840a17SGleb Smirnoff SYSCTL_COUNTER_U64_ARRAY(_net_inet_tcp, TCPCTL_STATES, states, CTLFLAG_RD |
238d4d32b9fSHans Petter Selasky     CTLFLAG_VNET, &VNET_NAME(tcps_states)[0], TCP_NSTATES,
239bf840a17SGleb Smirnoff     "TCP connection counts by TCP state");
240bf840a17SGleb Smirnoff 
241bf840a17SGleb Smirnoff static void
242bf840a17SGleb Smirnoff tcp_vnet_init(const void *unused)
243bf840a17SGleb Smirnoff {
244bf840a17SGleb Smirnoff 
245f59d975eSGleb Smirnoff 	COUNTER_ARRAY_ALLOC(V_tcps_states, TCP_NSTATES, M_WAITOK);
246bf840a17SGleb Smirnoff 	VNET_PCPUSTAT_ALLOC(tcpstat, M_WAITOK);
247bf840a17SGleb Smirnoff }
248bf840a17SGleb Smirnoff VNET_SYSINIT(tcp_vnet_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
249bf840a17SGleb Smirnoff     tcp_vnet_init, NULL);
2505923c293SGleb Smirnoff 
2515923c293SGleb Smirnoff #ifdef VIMAGE
252bf840a17SGleb Smirnoff static void
253bf840a17SGleb Smirnoff tcp_vnet_uninit(const void *unused)
254bf840a17SGleb Smirnoff {
255bf840a17SGleb Smirnoff 
256f59d975eSGleb Smirnoff 	COUNTER_ARRAY_FREE(V_tcps_states, TCP_NSTATES);
257bf840a17SGleb Smirnoff 	VNET_PCPUSTAT_FREE(tcpstat);
258bf840a17SGleb Smirnoff }
259bf840a17SGleb Smirnoff VNET_SYSUNINIT(tcp_vnet_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
260bf840a17SGleb Smirnoff     tcp_vnet_uninit, NULL);
2615923c293SGleb Smirnoff #endif /* VIMAGE */
262bf840a17SGleb Smirnoff 
2635923c293SGleb Smirnoff /*
264315e3e38SRobert Watson  * Kernel module interface for updating tcpstat.  The argument is an index
2655923c293SGleb Smirnoff  * into tcpstat treated as an array.
266315e3e38SRobert Watson  */
267315e3e38SRobert Watson void
268315e3e38SRobert Watson kmod_tcpstat_inc(int statnum)
269315e3e38SRobert Watson {
270315e3e38SRobert Watson 
2715da0521fSAndrey V. Elsukov 	counter_u64_add(VNET(tcpstat)[statnum], 1);
272315e3e38SRobert Watson }
273315e3e38SRobert Watson 
274bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
275dbc42409SLawrence Stewart /*
27639bc9de5SLawrence Stewart  * Wrapper for the TCP established input helper hook.
27739bc9de5SLawrence Stewart  */
27855bceb1eSRandall Stewart void
27939bc9de5SLawrence Stewart hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to)
28039bc9de5SLawrence Stewart {
28139bc9de5SLawrence Stewart 	struct tcp_hhook_data hhook_data;
28239bc9de5SLawrence Stewart 
28339bc9de5SLawrence Stewart 	if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) {
28439bc9de5SLawrence Stewart 		hhook_data.tp = tp;
28539bc9de5SLawrence Stewart 		hhook_data.th = th;
28639bc9de5SLawrence Stewart 		hhook_data.to = to;
28739bc9de5SLawrence Stewart 
28839bc9de5SLawrence Stewart 		hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data,
28939bc9de5SLawrence Stewart 		    tp->osd);
29039bc9de5SLawrence Stewart 	}
29139bc9de5SLawrence Stewart }
292bd79708dSJonathan T. Looney #endif
29339bc9de5SLawrence Stewart 
29439bc9de5SLawrence Stewart /*
295dbc42409SLawrence Stewart  * CC wrapper hook functions
296dbc42409SLawrence Stewart  */
29755bceb1eSRandall Stewart void
2984b7b743cSLawrence Stewart cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t nsegs,
2994b7b743cSLawrence Stewart     uint16_t type)
300f2512ba1SRui Paulo {
301dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
302f2512ba1SRui Paulo 
3034b7b743cSLawrence Stewart 	tp->ccv->nsegs = nsegs;
304dbc42409SLawrence Stewart 	tp->ccv->bytes_this_ack = BYTES_THIS_ACK(tp, th);
3055b648e79SLawrence Stewart 	if (tp->snd_cwnd <= tp->snd_wnd)
306dbc42409SLawrence Stewart 		tp->ccv->flags |= CCF_CWND_LIMITED;
307dbc42409SLawrence Stewart 	else
308dbc42409SLawrence Stewart 		tp->ccv->flags &= ~CCF_CWND_LIMITED;
309dbc42409SLawrence Stewart 
310dbc42409SLawrence Stewart 	if (type == CC_ACK) {
311dbc42409SLawrence Stewart 		if (tp->snd_cwnd > tp->snd_ssthresh) {
312dbc42409SLawrence Stewart 			tp->t_bytes_acked += min(tp->ccv->bytes_this_ack,
3134b7b743cSLawrence Stewart 			     nsegs * V_tcp_abc_l_var * tcp_maxseg(tp));
314dbc42409SLawrence Stewart 			if (tp->t_bytes_acked >= tp->snd_cwnd) {
315dbc42409SLawrence Stewart 				tp->t_bytes_acked -= tp->snd_cwnd;
316dbc42409SLawrence Stewart 				tp->ccv->flags |= CCF_ABC_SENTAWND;
317dbc42409SLawrence Stewart 			}
318dbc42409SLawrence Stewart 		} else {
319dbc42409SLawrence Stewart 				tp->ccv->flags &= ~CCF_ABC_SENTAWND;
320dbc42409SLawrence Stewart 				tp->t_bytes_acked = 0;
321dbc42409SLawrence Stewart 		}
322dbc42409SLawrence Stewart 	}
323dbc42409SLawrence Stewart 
324dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->ack_received != NULL) {
325dbc42409SLawrence Stewart 		/* XXXLAS: Find a way to live without this */
326dbc42409SLawrence Stewart 		tp->ccv->curack = th->th_ack;
327dbc42409SLawrence Stewart 		CC_ALGO(tp)->ack_received(tp->ccv, type);
328dbc42409SLawrence Stewart 	}
329dbc42409SLawrence Stewart }
330dbc42409SLawrence Stewart 
33155bceb1eSRandall Stewart void
332dbc42409SLawrence Stewart cc_conn_init(struct tcpcb *tp)
333dbc42409SLawrence Stewart {
334dbc42409SLawrence Stewart 	struct hc_metrics_lite metrics;
335dbc42409SLawrence Stewart 	struct inpcb *inp = tp->t_inpcb;
3360c39d38dSGleb Smirnoff 	u_int maxseg;
337dbc42409SLawrence Stewart 	int rtt;
338dbc42409SLawrence Stewart 
339dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
340dbc42409SLawrence Stewart 
341dbc42409SLawrence Stewart 	tcp_hc_get(&inp->inp_inc, &metrics);
3420c39d38dSGleb Smirnoff 	maxseg = tcp_maxseg(tp);
343dbc42409SLawrence Stewart 
344dbc42409SLawrence Stewart 	if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) {
345dbc42409SLawrence Stewart 		tp->t_srtt = rtt;
346dbc42409SLawrence Stewart 		tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE;
347dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_usedrtt);
348dbc42409SLawrence Stewart 		if (metrics.rmx_rttvar) {
349dbc42409SLawrence Stewart 			tp->t_rttvar = metrics.rmx_rttvar;
350dbc42409SLawrence Stewart 			TCPSTAT_INC(tcps_usedrttvar);
351dbc42409SLawrence Stewart 		} else {
352dbc42409SLawrence Stewart 			/* default variation is +- 1 rtt */
353dbc42409SLawrence Stewart 			tp->t_rttvar =
354dbc42409SLawrence Stewart 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
355dbc42409SLawrence Stewart 		}
356dbc42409SLawrence Stewart 		TCPT_RANGESET(tp->t_rxtcur,
357dbc42409SLawrence Stewart 		    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
358dbc42409SLawrence Stewart 		    tp->t_rttmin, TCPTV_REXMTMAX);
359dbc42409SLawrence Stewart 	}
360dbc42409SLawrence Stewart 	if (metrics.rmx_ssthresh) {
361dbc42409SLawrence Stewart 		/*
362dbc42409SLawrence Stewart 		 * There's some sort of gateway or interface
363dbc42409SLawrence Stewart 		 * buffer limit on the path.  Use this to set
364a4641f4eSPedro F. Giffuni 		 * the slow start threshold, but set the
365dbc42409SLawrence Stewart 		 * threshold to no less than 2*mss.
366dbc42409SLawrence Stewart 		 */
3670c39d38dSGleb Smirnoff 		tp->snd_ssthresh = max(2 * maxseg, metrics.rmx_ssthresh);
368dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_usedssthresh);
369dbc42409SLawrence Stewart 	}
370dbc42409SLawrence Stewart 
371dbc42409SLawrence Stewart 	/*
3729ec4a4ccSAndre Oppermann 	 * Set the initial slow-start flight size.
373dbc42409SLawrence Stewart 	 *
374cf8f04f4SAndre Oppermann 	 * RFC5681 Section 3.1 specifies the default conservative values.
375cf8f04f4SAndre Oppermann 	 * RFC3390 specifies slightly more aggressive values.
376b8b4cfcdSSergey Kandaurov 	 * RFC6928 increases it to ten segments.
377356c7958SHiren Panchasara 	 * Support for user specified value for initial flight size.
378cf8f04f4SAndre Oppermann 	 *
379cf8f04f4SAndre Oppermann 	 * If a SYN or SYN/ACK was lost and retransmitted, we have to
380cf8f04f4SAndre Oppermann 	 * reduce the initial CWND to one segment as congestion is likely
381cf8f04f4SAndre Oppermann 	 * requiring us to be cautious.
382dbc42409SLawrence Stewart 	 */
383cf8f04f4SAndre Oppermann 	if (tp->snd_cwnd == 1)
3840c39d38dSGleb Smirnoff 		tp->snd_cwnd = maxseg;		/* SYN(-ACK) lost */
385356c7958SHiren Panchasara 	else if (V_tcp_initcwnd_segments)
3860c39d38dSGleb Smirnoff 		tp->snd_cwnd = min(V_tcp_initcwnd_segments * maxseg,
3870c39d38dSGleb Smirnoff 		    max(2 * maxseg, V_tcp_initcwnd_segments * 1460));
388cf8f04f4SAndre Oppermann 	else if (V_tcp_do_rfc3390)
3890c39d38dSGleb Smirnoff 		tp->snd_cwnd = min(4 * maxseg, max(2 * maxseg, 4380));
39022efabd4SAndre Oppermann 	else {
39122efabd4SAndre Oppermann 		/* Per RFC5681 Section 3.1 */
3920c39d38dSGleb Smirnoff 		if (maxseg > 2190)
3930c39d38dSGleb Smirnoff 			tp->snd_cwnd = 2 * maxseg;
3940c39d38dSGleb Smirnoff 		else if (maxseg > 1095)
3950c39d38dSGleb Smirnoff 			tp->snd_cwnd = 3 * maxseg;
396dbc42409SLawrence Stewart 		else
3970c39d38dSGleb Smirnoff 			tp->snd_cwnd = 4 * maxseg;
39822efabd4SAndre Oppermann 	}
399dbc42409SLawrence Stewart 
400dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->conn_init != NULL)
401dbc42409SLawrence Stewart 		CC_ALGO(tp)->conn_init(tp->ccv);
402dbc42409SLawrence Stewart }
403dbc42409SLawrence Stewart 
404dbc42409SLawrence Stewart void inline
405dbc42409SLawrence Stewart cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type)
406dbc42409SLawrence Stewart {
4070c39d38dSGleb Smirnoff 	u_int maxseg;
4080c39d38dSGleb Smirnoff 
409dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
410dbc42409SLawrence Stewart 
411dbc42409SLawrence Stewart 	switch(type) {
412dbc42409SLawrence Stewart 	case CC_NDUPACK:
413dbc42409SLawrence Stewart 		if (!IN_FASTRECOVERY(tp->t_flags)) {
414f2512ba1SRui Paulo 			tp->snd_recover = tp->snd_max;
415f2512ba1SRui Paulo 			if (tp->t_flags & TF_ECN_PERMIT)
416f2512ba1SRui Paulo 				tp->t_flags |= TF_ECN_SND_CWR;
417f2512ba1SRui Paulo 		}
418dbc42409SLawrence Stewart 		break;
419dbc42409SLawrence Stewart 	case CC_ECN:
420dbc42409SLawrence Stewart 		if (!IN_CONGRECOVERY(tp->t_flags)) {
421dbc42409SLawrence Stewart 			TCPSTAT_INC(tcps_ecn_rcwnd);
422dbc42409SLawrence Stewart 			tp->snd_recover = tp->snd_max;
423dbc42409SLawrence Stewart 			if (tp->t_flags & TF_ECN_PERMIT)
424dbc42409SLawrence Stewart 				tp->t_flags |= TF_ECN_SND_CWR;
425dbc42409SLawrence Stewart 		}
426dbc42409SLawrence Stewart 		break;
427dbc42409SLawrence Stewart 	case CC_RTO:
4280c39d38dSGleb Smirnoff 		maxseg = tcp_maxseg(tp);
429dbc42409SLawrence Stewart 		tp->t_dupacks = 0;
430dbc42409SLawrence Stewart 		tp->t_bytes_acked = 0;
431dbc42409SLawrence Stewart 		EXIT_RECOVERY(tp->t_flags);
43243053c12SSean Bruno 		tp->snd_ssthresh = max(2, min(tp->snd_wnd, tp->snd_cwnd) / 2 /
43343053c12SSean Bruno 		    maxseg) * maxseg;
4340c39d38dSGleb Smirnoff 		tp->snd_cwnd = maxseg;
435dbc42409SLawrence Stewart 		break;
436dbc42409SLawrence Stewart 	case CC_RTO_ERR:
437dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_sndrexmitbad);
438dbc42409SLawrence Stewart 		/* RTO was unnecessary, so reset everything. */
439dbc42409SLawrence Stewart 		tp->snd_cwnd = tp->snd_cwnd_prev;
440dbc42409SLawrence Stewart 		tp->snd_ssthresh = tp->snd_ssthresh_prev;
441dbc42409SLawrence Stewart 		tp->snd_recover = tp->snd_recover_prev;
442dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASFRECOVERY)
443dbc42409SLawrence Stewart 			ENTER_FASTRECOVERY(tp->t_flags);
444dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASCRECOVERY)
445dbc42409SLawrence Stewart 			ENTER_CONGRECOVERY(tp->t_flags);
446dbc42409SLawrence Stewart 		tp->snd_nxt = tp->snd_max;
447672dc4aeSJohn Baldwin 		tp->t_flags &= ~TF_PREVVALID;
448dbc42409SLawrence Stewart 		tp->t_badrxtwin = 0;
449dbc42409SLawrence Stewart 		break;
450dbc42409SLawrence Stewart 	}
451dbc42409SLawrence Stewart 
452dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->cong_signal != NULL) {
453dbc42409SLawrence Stewart 		if (th != NULL)
454dbc42409SLawrence Stewart 			tp->ccv->curack = th->th_ack;
455dbc42409SLawrence Stewart 		CC_ALGO(tp)->cong_signal(tp->ccv, type);
456dbc42409SLawrence Stewart 	}
457dbc42409SLawrence Stewart }
458dbc42409SLawrence Stewart 
45955bceb1eSRandall Stewart void inline
460dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th)
461dbc42409SLawrence Stewart {
462dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
463dbc42409SLawrence Stewart 
464dbc42409SLawrence Stewart 	/* XXXLAS: KASSERT that we're in recovery? */
465dbc42409SLawrence Stewart 
466dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->post_recovery != NULL) {
467dbc42409SLawrence Stewart 		tp->ccv->curack = th->th_ack;
468dbc42409SLawrence Stewart 		CC_ALGO(tp)->post_recovery(tp->ccv);
469dbc42409SLawrence Stewart 	}
470dbc42409SLawrence Stewart 	/* XXXLAS: EXIT_RECOVERY ? */
471dbc42409SLawrence Stewart 	tp->t_bytes_acked = 0;
472dbc42409SLawrence Stewart }
4730312fbe9SPoul-Henning Kamp 
474df8bae1dSRodney W. Grimes /*
475262c1c1aSMatthew Dillon  * Indicate whether this ack should be delayed.  We can delay the ack if
47685536381SHiren Panchasara  * following conditions are met:
47785536381SHiren Panchasara  *	- There is no delayed ack timer in progress.
47885536381SHiren Panchasara  *	- Our last ack wasn't a 0-sized window. We never want to delay
47985536381SHiren Panchasara  *	  the ack that opens up a 0-sized window.
48085536381SHiren Panchasara  *	- LRO wasn't used for this segment. We make sure by checking that the
48185536381SHiren Panchasara  *	  segment size is not larger than the MSS.
482d8c85a26SJonathan Lemon  */
483c1e5a6e5SAndre Oppermann #define DELAY_ACK(tp, tlen)						\
484b8152ba7SAndre Oppermann 	((!tcp_timer_active(tp, TT_DELACK) &&				\
485a14c749fSJonathan Lemon 	    (tp->t_flags & TF_RXWIN0SENT) == 0) &&			\
4860c39d38dSGleb Smirnoff 	    (tlen <= tp->t_maxseg) &&					\
487603724d3SBjoern A. Zeeb 	    (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN)))
488d8c85a26SJonathan Lemon 
48964807b30SHiren Panchasara static void inline
49064807b30SHiren Panchasara cc_ecnpkt_handler(struct tcpcb *tp, struct tcphdr *th, uint8_t iptos)
49164807b30SHiren Panchasara {
49264807b30SHiren Panchasara 	INP_WLOCK_ASSERT(tp->t_inpcb);
49364807b30SHiren Panchasara 
49464807b30SHiren Panchasara 	if (CC_ALGO(tp)->ecnpkt_handler != NULL) {
49564807b30SHiren Panchasara 		switch (iptos & IPTOS_ECN_MASK) {
49664807b30SHiren Panchasara 		case IPTOS_ECN_CE:
49764807b30SHiren Panchasara 		    tp->ccv->flags |= CCF_IPHDR_CE;
49864807b30SHiren Panchasara 		    break;
49964807b30SHiren Panchasara 		case IPTOS_ECN_ECT0:
50064807b30SHiren Panchasara 		    tp->ccv->flags &= ~CCF_IPHDR_CE;
50164807b30SHiren Panchasara 		    break;
50264807b30SHiren Panchasara 		case IPTOS_ECN_ECT1:
50364807b30SHiren Panchasara 		    tp->ccv->flags &= ~CCF_IPHDR_CE;
50464807b30SHiren Panchasara 		    break;
50564807b30SHiren Panchasara 		}
50664807b30SHiren Panchasara 
50764807b30SHiren Panchasara 		if (th->th_flags & TH_CWR)
50864807b30SHiren Panchasara 			tp->ccv->flags |= CCF_TCPHDR_CWR;
50964807b30SHiren Panchasara 		else
51064807b30SHiren Panchasara 			tp->ccv->flags &= ~CCF_TCPHDR_CWR;
51164807b30SHiren Panchasara 
51264807b30SHiren Panchasara 		if (tp->t_flags & TF_DELACK)
51364807b30SHiren Panchasara 			tp->ccv->flags |= CCF_DELACK;
51464807b30SHiren Panchasara 		else
51564807b30SHiren Panchasara 			tp->ccv->flags &= ~CCF_DELACK;
51664807b30SHiren Panchasara 
51764807b30SHiren Panchasara 		CC_ALGO(tp)->ecnpkt_handler(tp->ccv);
51864807b30SHiren Panchasara 
51964807b30SHiren Panchasara 		if (tp->ccv->flags & CCF_ACKNOW)
52064807b30SHiren Panchasara 			tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
52164807b30SHiren Panchasara 	}
52264807b30SHiren Panchasara }
52364807b30SHiren Panchasara 
524df8bae1dSRodney W. Grimes /*
5258d573cc1SAndre Oppermann  * TCP input handling is split into multiple parts:
5268d573cc1SAndre Oppermann  *   tcp6_input is a thin wrapper around tcp_input for the extended
5278d573cc1SAndre Oppermann  *	ip6_protox[] call format in ip6_input
5288d573cc1SAndre Oppermann  *   tcp_input handles primary segment validation, inpcb lookup and
5298d573cc1SAndre Oppermann  *	SYN processing on listen sockets
5308d573cc1SAndre Oppermann  *   tcp_do_segment processes the ACK and text of the segment for
5318d573cc1SAndre Oppermann  *	establishing, established and closing connections
532df8bae1dSRodney W. Grimes  */
533fb59c426SYoshinobu Inoue #ifdef INET6
534fb59c426SYoshinobu Inoue int
535ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto)
536fb59c426SYoshinobu Inoue {
537ad3f9ab3SAndre Oppermann 	struct mbuf *m = *mp;
53833841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6;
5393e88eb90SAndrey V. Elsukov 	struct ip6_hdr *ip6;
540fb59c426SYoshinobu Inoue 
541fb59c426SYoshinobu Inoue 	IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE);
542fb59c426SYoshinobu Inoue 
543fb59c426SYoshinobu Inoue 	/*
544fb59c426SYoshinobu Inoue 	 * draft-itojun-ipv6-tcp-to-anycast
545fb59c426SYoshinobu Inoue 	 * better place to put this in?
546fb59c426SYoshinobu Inoue 	 */
5473e88eb90SAndrey V. Elsukov 	ip6 = mtod(m, struct ip6_hdr *);
5483e88eb90SAndrey V. Elsukov 	ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
54933841545SHajimu UMEMOTO 	if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
550fb59c426SYoshinobu Inoue 		struct ip6_hdr *ip6;
551fb59c426SYoshinobu Inoue 
5528c0fec80SRobert Watson 		ifa_free(&ia6->ia_ifa);
553fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
554fb59c426SYoshinobu Inoue 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
555fb59c426SYoshinobu Inoue 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
5568f5a8818SKevin Lo 		return (IPPROTO_DONE);
557fb59c426SYoshinobu Inoue 	}
55877d396fdSMaksim Yevmenkin 	if (ia6)
55977d396fdSMaksim Yevmenkin 		ifa_free(&ia6->ia_ifa);
560fb59c426SYoshinobu Inoue 
5618f5a8818SKevin Lo 	return (tcp_input(mp, offp, proto));
562fb59c426SYoshinobu Inoue }
563b287c6c7SBjoern A. Zeeb #endif /* INET6 */
564fb59c426SYoshinobu Inoue 
5658f5a8818SKevin Lo int
5668f5a8818SKevin Lo tcp_input(struct mbuf **mp, int *offp, int proto)
567df8bae1dSRodney W. Grimes {
5688f5a8818SKevin Lo 	struct mbuf *m = *mp;
569b287c6c7SBjoern A. Zeeb 	struct tcphdr *th = NULL;
570ad3f9ab3SAndre Oppermann 	struct ip *ip = NULL;
571ad3f9ab3SAndre Oppermann 	struct inpcb *inp = NULL;
572302ce8d6SAndre Oppermann 	struct tcpcb *tp = NULL;
573302ce8d6SAndre Oppermann 	struct socket *so = NULL;
574e79adb8eSGarrett Wollman 	u_char *optp = NULL;
5758f5a8818SKevin Lo 	int off0;
57626f9a767SRodney W. Grimes 	int optlen = 0;
577b287c6c7SBjoern A. Zeeb #ifdef INET
578b287c6c7SBjoern A. Zeeb 	int len;
579b287c6c7SBjoern A. Zeeb #endif
580b287c6c7SBjoern A. Zeeb 	int tlen = 0, off;
581fb59c426SYoshinobu Inoue 	int drop_hdrlen;
582ad3f9ab3SAndre Oppermann 	int thflags;
583302ce8d6SAndre Oppermann 	int rstreason = 0;	/* For badport_bandlim accounting purposes */
5841d7ee746SKurt Lidl 	uint8_t iptos;
585c1de64a4SAndrey V. Elsukov 	struct m_tag *fwd_tag = NULL;
5866573d758SMatt Macy 	struct epoch_tracker et;
587c068736aSJeffrey Hsu #ifdef INET6
588302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6 = NULL;
589c068736aSJeffrey Hsu 	int isipv6;
590c068736aSJeffrey Hsu #else
591a160e630SAndre Oppermann 	const void *ip6 = NULL;
592b287c6c7SBjoern A. Zeeb #endif /* INET6 */
593302ce8d6SAndre Oppermann 	struct tcpopt to;		/* options in this segment */
594a160e630SAndre Oppermann 	char *s = NULL;			/* address and port logging */
595252ca428SRobert Watson 	int ti_locked;
596610ee2f9SDavid Greenman #ifdef TCPDEBUG
597c068736aSJeffrey Hsu 	/*
598c068736aSJeffrey Hsu 	 * The size of tcp_saveipgen must be the size of the max ip header,
599c068736aSJeffrey Hsu 	 * now IPv6.
600c068736aSJeffrey Hsu 	 */
60114739780SMaxim Konovalov 	u_char tcp_saveipgen[IP6_HDR_LEN];
602410bb1bfSLuigi Rizzo 	struct tcphdr tcp_savetcp;
603610ee2f9SDavid Greenman 	short ostate = 0;
604610ee2f9SDavid Greenman #endif
605c068736aSJeffrey Hsu 
606fb59c426SYoshinobu Inoue #ifdef INET6
607fb59c426SYoshinobu Inoue 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
608fb59c426SYoshinobu Inoue #endif
609a0292f23SGarrett Wollman 
6108f5a8818SKevin Lo 	off0 = *offp;
6118f5a8818SKevin Lo 	m = *mp;
6128f5a8818SKevin Lo 	*mp = NULL;
613302ce8d6SAndre Oppermann 	to.to_flags = 0;
61478b50714SRobert Watson 	TCPSTAT_INC(tcps_rcvtotal);
615fb59c426SYoshinobu Inoue 
61604d3a452SHajimu UMEMOTO #ifdef INET6
617b287c6c7SBjoern A. Zeeb 	if (isipv6) {
6188d573cc1SAndre Oppermann 		/* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */
61945747ba5SBjoern A. Zeeb 
62045747ba5SBjoern A. Zeeb 		if (m->m_len < (sizeof(*ip6) + sizeof(*th))) {
62145747ba5SBjoern A. Zeeb 			m = m_pullup(m, sizeof(*ip6) + sizeof(*th));
62245747ba5SBjoern A. Zeeb 			if (m == NULL) {
62345747ba5SBjoern A. Zeeb 				TCPSTAT_INC(tcps_rcvshort);
6248f5a8818SKevin Lo 				return (IPPROTO_DONE);
62545747ba5SBjoern A. Zeeb 			}
62645747ba5SBjoern A. Zeeb 		}
62745747ba5SBjoern A. Zeeb 
628fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
62945747ba5SBjoern A. Zeeb 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
630fb59c426SYoshinobu Inoue 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
631356ab07eSBjoern A. Zeeb 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) {
63245747ba5SBjoern A. Zeeb 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
63345747ba5SBjoern A. Zeeb 				th->th_sum = m->m_pkthdr.csum_data;
63445747ba5SBjoern A. Zeeb 			else
63545747ba5SBjoern A. Zeeb 				th->th_sum = in6_cksum_pseudo(ip6, tlen,
63645747ba5SBjoern A. Zeeb 				    IPPROTO_TCP, m->m_pkthdr.csum_data);
63745747ba5SBjoern A. Zeeb 			th->th_sum ^= 0xffff;
63845747ba5SBjoern A. Zeeb 		} else
63945747ba5SBjoern A. Zeeb 			th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen);
64045747ba5SBjoern A. Zeeb 		if (th->th_sum) {
64178b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
642fb59c426SYoshinobu Inoue 			goto drop;
643fb59c426SYoshinobu Inoue 		}
64433841545SHajimu UMEMOTO 
64533841545SHajimu UMEMOTO 		/*
64633841545SHajimu UMEMOTO 		 * Be proactive about unspecified IPv6 address in source.
64733841545SHajimu UMEMOTO 		 * As we use all-zero to indicate unbounded/unconnected pcb,
64833841545SHajimu UMEMOTO 		 * unspecified IPv6 address can be used to confuse us.
64933841545SHajimu UMEMOTO 		 *
65033841545SHajimu UMEMOTO 		 * Note that packets with unspecified IPv6 destination is
65133841545SHajimu UMEMOTO 		 * already dropped in ip6_input.
65233841545SHajimu UMEMOTO 		 */
65333841545SHajimu UMEMOTO 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
65433841545SHajimu UMEMOTO 			/* XXX stat */
65533841545SHajimu UMEMOTO 			goto drop;
65633841545SHajimu UMEMOTO 		}
6571d7ee746SKurt Lidl 		iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
658b287c6c7SBjoern A. Zeeb 	}
65904d3a452SHajimu UMEMOTO #endif
660b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
661b287c6c7SBjoern A. Zeeb 	else
662b287c6c7SBjoern A. Zeeb #endif
663b287c6c7SBjoern A. Zeeb #ifdef INET
664b287c6c7SBjoern A. Zeeb 	{
665df8bae1dSRodney W. Grimes 		/*
666df8bae1dSRodney W. Grimes 		 * Get IP and TCP header together in first mbuf.
667df8bae1dSRodney W. Grimes 		 * Note: IP leaves IP header in first mbuf.
668df8bae1dSRodney W. Grimes 		 */
669fb59c426SYoshinobu Inoue 		if (off0 > sizeof (struct ip)) {
670105bd211SGleb Smirnoff 			ip_stripoptions(m);
671fb59c426SYoshinobu Inoue 			off0 = sizeof(struct ip);
672fb59c426SYoshinobu Inoue 		}
673df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof (struct tcpiphdr)) {
6744dfdffe9SAndre Oppermann 			if ((m = m_pullup(m, sizeof (struct tcpiphdr)))
6754dfdffe9SAndre Oppermann 			    == NULL) {
67678b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvshort);
6778f5a8818SKevin Lo 				return (IPPROTO_DONE);
678df8bae1dSRodney W. Grimes 			}
679df8bae1dSRodney W. Grimes 		}
680fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
681db4f9cc7SJonathan Lemon 		th = (struct tcphdr *)((caddr_t)ip + off0);
6828ad458a4SGleb Smirnoff 		tlen = ntohs(ip->ip_len) - off0;
683df8bae1dSRodney W. Grimes 
6841d7ee746SKurt Lidl 		iptos = ip->ip_tos;
685db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
686db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
687db4f9cc7SJonathan Lemon 				th->th_sum = m->m_pkthdr.csum_data;
688db4f9cc7SJonathan Lemon 			else
689db4f9cc7SJonathan Lemon 				th->th_sum = in_pseudo(ip->ip_src.s_addr,
690c068736aSJeffrey Hsu 				    ip->ip_dst.s_addr,
6918f134647SGleb Smirnoff 				    htonl(m->m_pkthdr.csum_data + tlen +
692c068736aSJeffrey Hsu 				    IPPROTO_TCP));
693db4f9cc7SJonathan Lemon 			th->th_sum ^= 0xffff;
694db4f9cc7SJonathan Lemon 		} else {
6958f134647SGleb Smirnoff 			struct ipovly *ipov = (struct ipovly *)ip;
6968f134647SGleb Smirnoff 
697df8bae1dSRodney W. Grimes 			/*
698df8bae1dSRodney W. Grimes 			 * Checksum extended TCP header and data.
699df8bae1dSRodney W. Grimes 			 */
7008f134647SGleb Smirnoff 			len = off0 + tlen;
701fb59c426SYoshinobu Inoue 			bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
7028f134647SGleb Smirnoff 			ipov->ih_len = htons(tlen);
703fb59c426SYoshinobu Inoue 			th->th_sum = in_cksum(m, len);
70457f60867SMark Johnston 			/* Reset length for SDT probes. */
7051d7ee746SKurt Lidl 			ip->ip_len = htons(len);
7061d7ee746SKurt Lidl 			/* Reset TOS bits */
7071d7ee746SKurt Lidl 			ip->ip_tos = iptos;
7081d7ee746SKurt Lidl 			/* Re-initialization for later version check */
7091d7ee746SKurt Lidl 			ip->ip_v = IPVERSION;
710b2fe54bcSAndrey V. Elsukov 			ip->ip_hl = off0 >> 2;
711db4f9cc7SJonathan Lemon 		}
71257f60867SMark Johnston 
713fb59c426SYoshinobu Inoue 		if (th->th_sum) {
71478b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
715df8bae1dSRodney W. Grimes 			goto drop;
716df8bae1dSRodney W. Grimes 		}
717fb59c426SYoshinobu Inoue 	}
718b287c6c7SBjoern A. Zeeb #endif /* INET */
719df8bae1dSRodney W. Grimes 
720df8bae1dSRodney W. Grimes 	/*
721df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
722df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
723df8bae1dSRodney W. Grimes 	 */
724fb59c426SYoshinobu Inoue 	off = th->th_off << 2;
725df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
72678b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvbadoff);
727df8bae1dSRodney W. Grimes 		goto drop;
728df8bae1dSRodney W. Grimes 	}
729fb59c426SYoshinobu Inoue 	tlen -= off;	/* tlen is used instead of ti->ti_len */
730df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
73104d3a452SHajimu UMEMOTO #ifdef INET6
732b287c6c7SBjoern A. Zeeb 		if (isipv6) {
7338f5a8818SKevin Lo 			IP6_EXTHDR_CHECK(m, off0, off, IPPROTO_DONE);
734fb59c426SYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
735fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
736b287c6c7SBjoern A. Zeeb 		}
73704d3a452SHajimu UMEMOTO #endif
738b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
739b287c6c7SBjoern A. Zeeb 		else
740b287c6c7SBjoern A. Zeeb #endif
741b287c6c7SBjoern A. Zeeb #ifdef INET
742b287c6c7SBjoern A. Zeeb 		{
743df8bae1dSRodney W. Grimes 			if (m->m_len < sizeof(struct ip) + off) {
744c068736aSJeffrey Hsu 				if ((m = m_pullup(m, sizeof (struct ip) + off))
7454dfdffe9SAndre Oppermann 				    == NULL) {
74678b50714SRobert Watson 					TCPSTAT_INC(tcps_rcvshort);
7478f5a8818SKevin Lo 					return (IPPROTO_DONE);
748df8bae1dSRodney W. Grimes 				}
749fb59c426SYoshinobu Inoue 				ip = mtod(m, struct ip *);
750fb59c426SYoshinobu Inoue 				th = (struct tcphdr *)((caddr_t)ip + off0);
751fb59c426SYoshinobu Inoue 			}
752df8bae1dSRodney W. Grimes 		}
753b287c6c7SBjoern A. Zeeb #endif
754df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
755fb59c426SYoshinobu Inoue 		optp = (u_char *)(th + 1);
756df8bae1dSRodney W. Grimes 	}
757fb59c426SYoshinobu Inoue 	thflags = th->th_flags;
758df8bae1dSRodney W. Grimes 
759e46cd3d4SDag-Erling Smørgrav 	/*
760df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
761df8bae1dSRodney W. Grimes 	 */
7622903309aSAttilio Rao 	tcp_fields_to_host(th);
763df8bae1dSRodney W. Grimes 
764df8bae1dSRodney W. Grimes 	/*
765794235b7SAndre Oppermann 	 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options.
766755c1f07SAndras Olah 	 */
767fb59c426SYoshinobu Inoue 	drop_hdrlen = off0 + off;
768755c1f07SAndras Olah 
769755c1f07SAndras Olah 	/*
770fa046d87SRobert Watson 	 * Locate pcb for segment; if we're likely to add or remove a
771a7c7f2a7SJohn Baldwin 	 * connection then first acquire pcbinfo lock.  There are three cases
772fa046d87SRobert Watson 	 * where we might discover later we need a write lock despite the
773a7c7f2a7SJohn Baldwin 	 * flags: ACKs moving a connection out of the syncache, ACKs for a
774a7c7f2a7SJohn Baldwin 	 * connection in TIMEWAIT and SYNs not targeting a listening socket.
775df8bae1dSRodney W. Grimes 	 */
776a7c7f2a7SJohn Baldwin 	if ((thflags & (TH_FIN | TH_RST)) != 0) {
7776573d758SMatt Macy 		INP_INFO_RLOCK_ET(&V_tcbinfo, et);
778ff9b006dSJulien Charbon 		ti_locked = TI_RLOCKED;
779fa046d87SRobert Watson 	} else
780fa046d87SRobert Watson 		ti_locked = TI_UNLOCKED;
781252ca428SRobert Watson 
7828d573cc1SAndre Oppermann 	/*
7838d573cc1SAndre Oppermann 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
7848d573cc1SAndre Oppermann 	 */
785b8056faeSGleb Smirnoff         if (
786b8056faeSGleb Smirnoff #ifdef INET6
787b8056faeSGleb Smirnoff 	    (isipv6 && (m->m_flags & M_IP6_NEXTHOP))
788b8056faeSGleb Smirnoff #ifdef INET
789b8056faeSGleb Smirnoff 	    || (!isipv6 && (m->m_flags & M_IP_NEXTHOP))
790b8056faeSGleb Smirnoff #endif
791b8056faeSGleb Smirnoff #endif
792b8056faeSGleb Smirnoff #if defined(INET) && !defined(INET6)
793b8056faeSGleb Smirnoff 	    (m->m_flags & M_IP_NEXTHOP)
794b8056faeSGleb Smirnoff #endif
795b8056faeSGleb Smirnoff 	    )
7969b932e9eSAndre Oppermann 		fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
7979b932e9eSAndre Oppermann 
798ce7ad664SEd Maste findpcb:
799ce7ad664SEd Maste #ifdef INVARIANTS
800ff9b006dSJulien Charbon 	if (ti_locked == TI_RLOCKED) {
801ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
802ce7ad664SEd Maste 	} else {
803ce7ad664SEd Maste 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
804ce7ad664SEd Maste 	}
805ce7ad664SEd Maste #endif
8068a006adbSBjoern A. Zeeb #ifdef INET6
8078a006adbSBjoern A. Zeeb 	if (isipv6 && fwd_tag != NULL) {
8088a006adbSBjoern A. Zeeb 		struct sockaddr_in6 *next_hop6;
8098a006adbSBjoern A. Zeeb 
8108a006adbSBjoern A. Zeeb 		next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
8118a006adbSBjoern A. Zeeb 		/*
8128a006adbSBjoern A. Zeeb 		 * Transparently forwarded. Pretend to be the destination.
8138a006adbSBjoern A. Zeeb 		 * Already got one like this?
8148a006adbSBjoern A. Zeeb 		 */
8158a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo,
8168a006adbSBjoern A. Zeeb 		    &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport,
8178a006adbSBjoern A. Zeeb 		    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif, m);
8188a006adbSBjoern A. Zeeb 		if (!inp) {
8198a006adbSBjoern A. Zeeb 			/*
8208a006adbSBjoern A. Zeeb 			 * It's new.  Try to find the ambushing socket.
8218a006adbSBjoern A. Zeeb 			 * Because we've rewritten the destination address,
8228a006adbSBjoern A. Zeeb 			 * any hardware-generated hash is ignored.
8238a006adbSBjoern A. Zeeb 			 */
8248a006adbSBjoern A. Zeeb 			inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src,
8258a006adbSBjoern A. Zeeb 			    th->th_sport, &next_hop6->sin6_addr,
8268a006adbSBjoern A. Zeeb 			    next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) :
8278a006adbSBjoern A. Zeeb 			    th->th_dport, INPLOOKUP_WILDCARD |
8288a006adbSBjoern A. Zeeb 			    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif);
8298a006adbSBjoern A. Zeeb 		}
830c1de64a4SAndrey V. Elsukov 	} else if (isipv6) {
8318a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src,
8328a006adbSBjoern A. Zeeb 		    th->th_sport, &ip6->ip6_dst, th->th_dport,
8338a006adbSBjoern A. Zeeb 		    INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB,
8348a006adbSBjoern A. Zeeb 		    m->m_pkthdr.rcvif, m);
8358a006adbSBjoern A. Zeeb 	}
8368a006adbSBjoern A. Zeeb #endif /* INET6 */
8378a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET)
8388a006adbSBjoern A. Zeeb 	else
8398a006adbSBjoern A. Zeeb #endif
8408a006adbSBjoern A. Zeeb #ifdef INET
8418a006adbSBjoern A. Zeeb 	if (fwd_tag != NULL) {
8429b932e9eSAndre Oppermann 		struct sockaddr_in *next_hop;
8439b932e9eSAndre Oppermann 
8449b932e9eSAndre Oppermann 		next_hop = (struct sockaddr_in *)(fwd_tag+1);
845f9e354dfSJulian Elischer 		/*
8462b25acc1SLuigi Rizzo 		 * Transparently forwarded. Pretend to be the destination.
847f9e354dfSJulian Elischer 		 * already got one like this?
848f9e354dfSJulian Elischer 		 */
849d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport,
850fa046d87SRobert Watson 		    ip->ip_dst, th->th_dport, INPLOOKUP_WLOCKPCB,
851d3c1f003SRobert Watson 		    m->m_pkthdr.rcvif, m);
852fa046d87SRobert Watson 		if (!inp) {
853fa046d87SRobert Watson 			/*
854fa046d87SRobert Watson 			 * It's new.  Try to find the ambushing socket.
855d3c1f003SRobert Watson 			 * Because we've rewritten the destination address,
856d3c1f003SRobert Watson 			 * any hardware-generated hash is ignored.
857fa046d87SRobert Watson 			 */
858fa046d87SRobert Watson 			inp = in_pcblookup(&V_tcbinfo, ip->ip_src,
859fa046d87SRobert Watson 			    th->th_sport, next_hop->sin_addr,
860fa046d87SRobert Watson 			    next_hop->sin_port ? ntohs(next_hop->sin_port) :
861fa046d87SRobert Watson 			    th->th_dport, INPLOOKUP_WILDCARD |
862fa046d87SRobert Watson 			    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif);
863f9e354dfSJulian Elischer 		}
864b10fbdeaSAndre Oppermann 	} else
865d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src,
866fa046d87SRobert Watson 		    th->th_sport, ip->ip_dst, th->th_dport,
867fa046d87SRobert Watson 		    INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB,
868d3c1f003SRobert Watson 		    m->m_pkthdr.rcvif, m);
8698a006adbSBjoern A. Zeeb #endif /* INET */
870f9e354dfSJulian Elischer 
871df8bae1dSRodney W. Grimes 	/*
872574b6964SAndre Oppermann 	 * If the INPCB does not exist then all data in the incoming
873574b6964SAndre Oppermann 	 * segment is discarded and an appropriate RST is sent back.
8748b07e49aSJulian Elischer 	 * XXX MRT Send RST using which routing table?
875df8bae1dSRodney W. Grimes 	 */
876816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
877574b6964SAndre Oppermann 		/*
878574b6964SAndre Oppermann 		 * Log communication attempts to ports that are not
879574b6964SAndre Oppermann 		 * in use.
880574b6964SAndre Oppermann 		 */
881574b6964SAndre Oppermann 		if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) ||
882574b6964SAndre Oppermann 		    tcp_log_in_vain == 2) {
883b7d747ecSAndre Oppermann 			if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6)))
884df541e5fSAndre Oppermann 				log(LOG_INFO, "%s; %s: Connection attempt "
885df541e5fSAndre Oppermann 				    "to closed port\n", s, __func__);
8862e4e1b4cSGeoff Rehmet 		}
887574b6964SAndre Oppermann 		/*
888574b6964SAndre Oppermann 		 * When blackholing do not respond with a RST but
889574b6964SAndre Oppermann 		 * completely ignore the segment and drop it.
890574b6964SAndre Oppermann 		 */
891603724d3SBjoern A. Zeeb 		if ((V_blackhole == 1 && (thflags & TH_SYN)) ||
892603724d3SBjoern A. Zeeb 		    V_blackhole == 2)
8931929eae1SAndre Oppermann 			goto dropunlock;
894574b6964SAndre Oppermann 
895a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
896a57815efSBosko Milekic 		goto dropwithreset;
897816a3d83SPoul-Henning Kamp 	}
898fa046d87SRobert Watson 	INP_WLOCK_ASSERT(inp);
899f5cf1e5fSJulien Charbon 	/*
900f5cf1e5fSJulien Charbon 	 * While waiting for inp lock during the lookup, another thread
901f5cf1e5fSJulien Charbon 	 * can have dropped the inpcb, in which case we need to loop back
902f5cf1e5fSJulien Charbon 	 * and try to find a new inpcb to deliver to.
903f5cf1e5fSJulien Charbon 	 */
904f5cf1e5fSJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
905f5cf1e5fSJulien Charbon 		INP_WUNLOCK(inp);
906f5cf1e5fSJulien Charbon 		inp = NULL;
907f5cf1e5fSJulien Charbon 		goto findpcb;
908f5cf1e5fSJulien Charbon 	}
909c2529042SHans Petter Selasky 	if ((inp->inp_flowtype == M_HASHTYPE_NONE) &&
910c2529042SHans Petter Selasky 	    (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) &&
911c2529042SHans Petter Selasky 	    ((inp->inp_socket == NULL) ||
912c2529042SHans Petter Selasky 	    (inp->inp_socket->so_options & SO_ACCEPTCONN) == 0)) {
91380cb9f21SKip Macy 		inp->inp_flowid = m->m_pkthdr.flowid;
9142f719932SAdrian Chadd 		inp->inp_flowtype = M_HASHTYPE_GET(m);
91580cb9f21SKip Macy 	}
916fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
917a2589465SKen Smith #ifdef INET6
918fcf59617SAndrey V. Elsukov 	if (isipv6 && IPSEC_ENABLED(ipv6) &&
919fcf59617SAndrey V. Elsukov 	    IPSEC_CHECK_POLICY(ipv6, m, inp) != 0) {
920a2589465SKen Smith 		goto dropunlock;
921a2589465SKen Smith 	}
922fcf59617SAndrey V. Elsukov #ifdef INET
923fcf59617SAndrey V. Elsukov 	else
924fcf59617SAndrey V. Elsukov #endif
925fcf59617SAndrey V. Elsukov #endif /* INET6 */
926fcf59617SAndrey V. Elsukov #ifdef INET
927fcf59617SAndrey V. Elsukov 	if (IPSEC_ENABLED(ipv4) &&
928fcf59617SAndrey V. Elsukov 	    IPSEC_CHECK_POLICY(ipv4, m, inp) != 0) {
929fcf59617SAndrey V. Elsukov 		goto dropunlock;
930fcf59617SAndrey V. Elsukov 	}
931fcf59617SAndrey V. Elsukov #endif /* INET */
932a2589465SKen Smith #endif /* IPSEC */
933a2589465SKen Smith 
9348d573cc1SAndre Oppermann 	/*
9358d573cc1SAndre Oppermann 	 * Check the minimum TTL for socket.
9368d573cc1SAndre Oppermann 	 */
93734f83c52SGeorge V. Neville-Neil 	if (inp->inp_ip_minttl != 0) {
93834f83c52SGeorge V. Neville-Neil #ifdef INET6
9392d8868dbSJonathan T. Looney 		if (isipv6) {
9402d8868dbSJonathan T. Looney 			if (inp->inp_ip_minttl > ip6->ip6_hlim)
941302ce8d6SAndre Oppermann 				goto dropunlock;
9422d8868dbSJonathan T. Looney 		} else
94334f83c52SGeorge V. Neville-Neil #endif
94434f83c52SGeorge V. Neville-Neil 		if (inp->inp_ip_minttl > ip->ip_ttl)
945302ce8d6SAndre Oppermann 			goto dropunlock;
94634f83c52SGeorge V. Neville-Neil 	}
947936cd18dSAndre Oppermann 
948340c35deSJonathan Lemon 	/*
949252ca428SRobert Watson 	 * A previous connection in TIMEWAIT state is supposed to catch stray
950252ca428SRobert Watson 	 * or duplicate segments arriving late.  If this segment was a
9510989f56cSRobert Watson 	 * legitimate new connection attempt, the old INPCB gets removed and
952252ca428SRobert Watson 	 * we can try again to find a listening socket.
953252ca428SRobert Watson 	 *
954fa046d87SRobert Watson 	 * At this point, due to earlier optimism, we may hold only an inpcb
955fa046d87SRobert Watson 	 * lock, and not the inpcbinfo write lock.  If so, we need to try to
956fa046d87SRobert Watson 	 * acquire it, or if that fails, acquire a reference on the inpcb,
957fa046d87SRobert Watson 	 * drop all locks, acquire a global write lock, and then re-acquire
958fa046d87SRobert Watson 	 * the inpcb lock.  We may at that point discover that another thread
959fa046d87SRobert Watson 	 * has tried to free the inpcb, in which case we need to loop back
960fa046d87SRobert Watson 	 * and try to find a new inpcb to deliver to.
961fa046d87SRobert Watson 	 *
962fa046d87SRobert Watson 	 * XXXRW: It may be time to rethink timewait locking.
963340c35deSJonathan Lemon 	 */
964ad71fe3cSRobert Watson 	if (inp->inp_flags & INP_TIMEWAIT) {
965fa046d87SRobert Watson 		if (ti_locked == TI_UNLOCKED) {
9666573d758SMatt Macy 			INP_INFO_RLOCK_ET(&V_tcbinfo, et);
967ff9b006dSJulien Charbon 			ti_locked = TI_RLOCKED;
968252ca428SRobert Watson 		}
969ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
970252ca428SRobert Watson 
971340c35deSJonathan Lemon 		if (thflags & TH_SYN)
972f72167f4SAndre Oppermann 			tcp_dooptions(&to, optp, optlen, TO_SYN);
9738d573cc1SAndre Oppermann 		/*
9748d573cc1SAndre Oppermann 		 * NB: tcp_twcheck unlocks the INP and frees the mbuf.
9758d573cc1SAndre Oppermann 		 */
9762104448fSAndre Oppermann 		if (tcp_twcheck(inp, &to, th, m, tlen))
977340c35deSJonathan Lemon 			goto findpcb;
9786573d758SMatt Macy 		INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
9798f5a8818SKevin Lo 		return (IPPROTO_DONE);
980340c35deSJonathan Lemon 	}
981794235b7SAndre Oppermann 	/*
982794235b7SAndre Oppermann 	 * The TCPCB may no longer exist if the connection is winding
983794235b7SAndre Oppermann 	 * down or it is in the CLOSED state.  Either way we drop the
984794235b7SAndre Oppermann 	 * segment and send an appropriate response.
985794235b7SAndre Oppermann 	 */
986df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
987a160e630SAndre Oppermann 	if (tp == NULL || tp->t_state == TCPS_CLOSED) {
988a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
989a57815efSBosko Milekic 		goto dropwithreset;
99009f81a46SBosko Milekic 	}
991df8bae1dSRodney W. Grimes 
99209fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
99309fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
99409fe6320SNavdeep Parhar 		tcp_offload_input(tp, m);
99509fe6320SNavdeep Parhar 		m = NULL;	/* consumed by the TOE driver */
99609fe6320SNavdeep Parhar 		goto dropunlock;
99709fe6320SNavdeep Parhar 	}
99809fe6320SNavdeep Parhar #endif
99909fe6320SNavdeep Parhar 
1000252ca428SRobert Watson 	/*
1001252ca428SRobert Watson 	 * We've identified a valid inpcb, but it could be that we need an
1002fa046d87SRobert Watson 	 * inpcbinfo write lock but don't hold it.  In this case, attempt to
1003fa046d87SRobert Watson 	 * acquire using the same strategy as the TIMEWAIT case above.  If we
1004fa046d87SRobert Watson 	 * relock, we have to jump back to 'relocked' as the connection might
1005fa046d87SRobert Watson 	 * now be in TIMEWAIT.
1006252ca428SRobert Watson 	 */
1007fa046d87SRobert Watson #ifdef INVARIANTS
1008a7c7f2a7SJohn Baldwin 	if ((thflags & (TH_FIN | TH_RST)) != 0)
1009ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1010fa046d87SRobert Watson #endif
1011a7c7f2a7SJohn Baldwin 	if (!((tp->t_state == TCPS_ESTABLISHED && (thflags & TH_SYN) == 0) ||
1012281a0fd4SPatrick Kelsey 	      (tp->t_state == TCPS_LISTEN && (thflags & TH_SYN) &&
101368bd7ed1SJonathan T. Looney 	       !IS_FASTOPEN(tp->t_flags)))) {
1014fa046d87SRobert Watson 		if (ti_locked == TI_UNLOCKED) {
10156573d758SMatt Macy 			INP_INFO_RLOCK_ET(&V_tcbinfo, et);
1016ff9b006dSJulien Charbon 			ti_locked = TI_RLOCKED;
1017252ca428SRobert Watson 		}
1018ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1019252ca428SRobert Watson 	}
1020252ca428SRobert Watson 
1021c488362eSRobert Watson #ifdef MAC
10228501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
102330d239bcSRobert Watson 	if (mac_inpcb_check_deliver(inp, m))
1024302ce8d6SAndre Oppermann 		goto dropunlock;
1025c488362eSRobert Watson #endif
1026a557af22SRobert Watson 	so = inp->inp_socket;
1027302ce8d6SAndre Oppermann 	KASSERT(so != NULL, ("%s: so == NULL", __func__));
1028610ee2f9SDavid Greenman #ifdef TCPDEBUG
1029df8bae1dSRodney W. Grimes 	if (so->so_options & SO_DEBUG) {
1030df8bae1dSRodney W. Grimes 		ostate = tp->t_state;
103110fe523eSMaxim Konovalov #ifdef INET6
10326f697424SBjoern A. Zeeb 		if (isipv6) {
1033540e8b7eSJeffrey Hsu 			bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6));
1034d30d90dcSMaxim Konovalov 		} else
10356f697424SBjoern A. Zeeb #endif
1036540e8b7eSJeffrey Hsu 			bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
1037fb59c426SYoshinobu Inoue 		tcp_savetcp = *th;
1038df8bae1dSRodney W. Grimes 	}
1039b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */
1040db33b3e6SAndre Oppermann 	/*
1041db33b3e6SAndre Oppermann 	 * When the socket is accepting connections (the INPCB is in LISTEN
1042db33b3e6SAndre Oppermann 	 * state) we look into the SYN cache if this is a new connection
1043a7c7f2a7SJohn Baldwin 	 * attempt or the completion of a previous one.
1044db33b3e6SAndre Oppermann 	 */
104545274760SJonathan T. Looney 	KASSERT(tp->t_state == TCPS_LISTEN || !(so->so_options & SO_ACCEPTCONN),
104645274760SJonathan T. Looney 	    ("%s: so accepting but tp %p not listening", __func__, tp));
104745274760SJonathan T. Looney 	if (tp->t_state == TCPS_LISTEN && (so->so_options & SO_ACCEPTCONN)) {
1048540e8b7eSJeffrey Hsu 		struct in_conninfo inc;
1049540e8b7eSJeffrey Hsu 
10508bfb1918SAndre Oppermann 		bzero(&inc, sizeof(inc));
1051302ce8d6SAndre Oppermann #ifdef INET6
1052be2ac88cSJonathan Lemon 		if (isipv6) {
1053dcdb4371SBjoern A. Zeeb 			inc.inc_flags |= INC_ISIPV6;
1054be2ac88cSJonathan Lemon 			inc.inc6_faddr = ip6->ip6_src;
1055be2ac88cSJonathan Lemon 			inc.inc6_laddr = ip6->ip6_dst;
1056302ce8d6SAndre Oppermann 		} else
1057302ce8d6SAndre Oppermann #endif
1058302ce8d6SAndre Oppermann 		{
1059be2ac88cSJonathan Lemon 			inc.inc_faddr = ip->ip_src;
1060be2ac88cSJonathan Lemon 			inc.inc_laddr = ip->ip_dst;
1061be2ac88cSJonathan Lemon 		}
1062be2ac88cSJonathan Lemon 		inc.inc_fport = th->th_sport;
1063be2ac88cSJonathan Lemon 		inc.inc_lport = th->th_dport;
10647973fba3SJulian Elischer 		inc.inc_fibnum = so->so_fibnum;
1065be2ac88cSJonathan Lemon 
1066fb59c426SYoshinobu Inoue 		/*
10678d573cc1SAndre Oppermann 		 * Check for an existing connection attempt in syncache if
10688d573cc1SAndre Oppermann 		 * the flag is only ACK.  A successful lookup creates a new
10698d573cc1SAndre Oppermann 		 * socket appended to the listen queue in SYN_RECEIVED state.
1070fb59c426SYoshinobu Inoue 		 */
1071be2ac88cSJonathan Lemon 		if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
1072a7c7f2a7SJohn Baldwin 
1073ff9b006dSJulien Charbon 			INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1074bf6d304aSAndre Oppermann 			/*
1075bf6d304aSAndre Oppermann 			 * Parse the TCP options here because
1076bf6d304aSAndre Oppermann 			 * syncookies need access to the reflected
1077bf6d304aSAndre Oppermann 			 * timestamp.
1078bf6d304aSAndre Oppermann 			 */
1079bf6d304aSAndre Oppermann 			tcp_dooptions(&to, optp, optlen, 0);
10809fa198beSAndre Oppermann 			/*
10819fa198beSAndre Oppermann 			 * NB: syncache_expand() doesn't unlock
10829fa198beSAndre Oppermann 			 * inp and tcpinfo locks.
10839fa198beSAndre Oppermann 			 */
1084fcf59617SAndrey V. Elsukov 			rstreason = syncache_expand(&inc, &to, th, &so, m);
1085fcf59617SAndrey V. Elsukov 			if (rstreason < 0) {
1086fcf59617SAndrey V. Elsukov 				/*
1087fcf59617SAndrey V. Elsukov 				 * A failing TCP MD5 signature comparison
1088fcf59617SAndrey V. Elsukov 				 * must result in the segment being dropped
1089fcf59617SAndrey V. Elsukov 				 * and must not produce any response back
1090fcf59617SAndrey V. Elsukov 				 * to the sender.
1091fcf59617SAndrey V. Elsukov 				 */
1092fcf59617SAndrey V. Elsukov 				goto dropunlock;
1093fcf59617SAndrey V. Elsukov 			} else if (rstreason == 0) {
10944195b4afSPaul Traina 				/*
1095e207f800SAndre Oppermann 				 * No syncache entry or ACK was not
1096be2ac88cSJonathan Lemon 				 * for our SYN/ACK.  Send a RST.
10978d573cc1SAndre Oppermann 				 * NB: syncache did its own logging
10988d573cc1SAndre Oppermann 				 * of the failure cause.
10994195b4afSPaul Traina 				 */
1100be2ac88cSJonathan Lemon 				rstreason = BANDLIM_RST_OPENPORT;
1101be2ac88cSJonathan Lemon 				goto dropwithreset;
1102be2ac88cSJonathan Lemon 			}
110309c305ebSPatrick Kelsey tfo_socket_result:
1104f76fcf6dSJeffrey Hsu 			if (so == NULL) {
1105be2ac88cSJonathan Lemon 				/*
1106e207f800SAndre Oppermann 				 * We completed the 3-way handshake
1107e207f800SAndre Oppermann 				 * but could not allocate a socket
1108e207f800SAndre Oppermann 				 * either due to memory shortage,
1109e207f800SAndre Oppermann 				 * listen queue length limits or
1110a160e630SAndre Oppermann 				 * global socket limits.  Send RST
1111a160e630SAndre Oppermann 				 * or wait and have the remote end
1112a160e630SAndre Oppermann 				 * retransmit the ACK for another
1113a160e630SAndre Oppermann 				 * try.
1114be2ac88cSJonathan Lemon 				 */
1115a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1116a160e630SAndre Oppermann 					log(LOG_DEBUG, "%s; %s: Listen socket: "
11178d573cc1SAndre Oppermann 					    "Socket allocation failed due to "
11188d573cc1SAndre Oppermann 					    "limits or memory shortage, %s\n",
1119ac957cd2SJulian Elischer 					    s, __func__,
1120ac957cd2SJulian Elischer 					    V_tcp_sc_rst_sock_fail ?
1121ac957cd2SJulian Elischer 					    "sending RST" : "try again");
1122603724d3SBjoern A. Zeeb 				if (V_tcp_sc_rst_sock_fail) {
1123e207f800SAndre Oppermann 					rstreason = BANDLIM_UNLIMITED;
1124e207f800SAndre Oppermann 					goto dropwithreset;
1125a160e630SAndre Oppermann 				} else
1126a160e630SAndre Oppermann 					goto dropunlock;
1127f76fcf6dSJeffrey Hsu 			}
1128be2ac88cSJonathan Lemon 			/*
1129be2ac88cSJonathan Lemon 			 * Socket is created in state SYN_RECEIVED.
11308d573cc1SAndre Oppermann 			 * Unlock the listen socket, lock the newly
11318d573cc1SAndre Oppermann 			 * created socket and update the tp variable.
1132be2ac88cSJonathan Lemon 			 */
11338501a69cSRobert Watson 			INP_WUNLOCK(inp);	/* listen socket */
1134be2ac88cSJonathan Lemon 			inp = sotoinpcb(so);
1135ff9b006dSJulien Charbon 			/*
1136ff9b006dSJulien Charbon 			 * New connection inpcb is already locked by
1137ff9b006dSJulien Charbon 			 * syncache_expand().
1138ff9b006dSJulien Charbon 			 */
1139ff9b006dSJulien Charbon 			INP_WLOCK_ASSERT(inp);
1140be2ac88cSJonathan Lemon 			tp = intotcpcb(inp);
11418d573cc1SAndre Oppermann 			KASSERT(tp->t_state == TCPS_SYN_RECEIVED,
11428d573cc1SAndre Oppermann 			    ("%s: ", __func__));
1143be2ac88cSJonathan Lemon 			/*
1144302ce8d6SAndre Oppermann 			 * Process the segment and the data it
1145302ce8d6SAndre Oppermann 			 * contains.  tcp_do_segment() consumes
1146302ce8d6SAndre Oppermann 			 * the mbuf chain and unlocks the inpcb.
1147302ce8d6SAndre Oppermann 			 */
11486138da62SMichael Tuexen 			TCP_PROBE5(receive, NULL, tp, m, tp, th);
114955bceb1eSRandall Stewart 			tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen,
11506573d758SMatt Macy 			    iptos);
11516573d758SMatt Macy 			if (ti_locked == TI_RLOCKED)
11526573d758SMatt Macy 				INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
11538f5a8818SKevin Lo 			return (IPPROTO_DONE);
1154be2ac88cSJonathan Lemon 		}
1155a160e630SAndre Oppermann 		/*
11568d573cc1SAndre Oppermann 		 * Segment flag validation for new connection attempts:
11578d573cc1SAndre Oppermann 		 *
1158a160e630SAndre Oppermann 		 * Our (SYN|ACK) response was rejected.
1159a160e630SAndre Oppermann 		 * Check with syncache and remove entry to prevent
1160a160e630SAndre Oppermann 		 * retransmits.
116119bc77c5SAndre Oppermann 		 *
116219bc77c5SAndre Oppermann 		 * NB: syncache_chkrst does its own logging of failure
116319bc77c5SAndre Oppermann 		 * causes.
1164a160e630SAndre Oppermann 		 */
1165a160e630SAndre Oppermann 		if (thflags & TH_RST) {
116619bc77c5SAndre Oppermann 			syncache_chkrst(&inc, th);
1167a160e630SAndre Oppermann 			goto dropunlock;
1168a160e630SAndre Oppermann 		}
1169a160e630SAndre Oppermann 		/*
1170a160e630SAndre Oppermann 		 * We can't do anything without SYN.
1171a160e630SAndre Oppermann 		 */
1172a160e630SAndre Oppermann 		if ((thflags & TH_SYN) == 0) {
1173a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1174a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1175773673c1SAndre Oppermann 				    "SYN is missing, segment ignored\n",
11768d573cc1SAndre Oppermann 				    s, __func__);
117778b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1178a160e630SAndre Oppermann 			goto dropunlock;
1179a160e630SAndre Oppermann 		}
1180a160e630SAndre Oppermann 		/*
1181a160e630SAndre Oppermann 		 * (SYN|ACK) is bogus on a listen socket.
1182a160e630SAndre Oppermann 		 */
1183fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1184a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1185a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
11868d573cc1SAndre Oppermann 				    "SYN|ACK invalid, segment rejected\n",
11878d573cc1SAndre Oppermann 				    s, __func__);
1188a160e630SAndre Oppermann 			syncache_badack(&inc);	/* XXX: Not needed! */
118978b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1190a57815efSBosko Milekic 			rstreason = BANDLIM_RST_OPENPORT;
1191a57815efSBosko Milekic 			goto dropwithreset;
11924195b4afSPaul Traina 		}
1193a160e630SAndre Oppermann 		/*
1194a160e630SAndre Oppermann 		 * If the drop_synfin option is enabled, drop all
1195a160e630SAndre Oppermann 		 * segments with both the SYN and FIN bits set.
1196a160e630SAndre Oppermann 		 * This prevents e.g. nmap from identifying the
1197a160e630SAndre Oppermann 		 * TCP/IP stack.
1198a160e630SAndre Oppermann 		 * XXX: Poor reasoning.  nmap has other methods
1199a160e630SAndre Oppermann 		 * and is constantly refining its stack detection
1200a160e630SAndre Oppermann 		 * strategies.
12018d573cc1SAndre Oppermann 		 * XXX: This is a violation of the TCP specification
1202a160e630SAndre Oppermann 		 * and was used by RFC1644.
1203a160e630SAndre Oppermann 		 */
1204603724d3SBjoern A. Zeeb 		if ((thflags & TH_FIN) && V_drop_synfin) {
1205a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1206a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1207773673c1SAndre Oppermann 				    "SYN|FIN segment ignored (based on "
12088d573cc1SAndre Oppermann 				    "sysctl setting)\n", s, __func__);
120978b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1210302ce8d6SAndre Oppermann 			goto dropunlock;
1211ebb0cbeaSPaul Traina 		}
1212be2ac88cSJonathan Lemon 		/*
1213be2ac88cSJonathan Lemon 		 * Segment's flags are (SYN) or (SYN|FIN).
1214a160e630SAndre Oppermann 		 *
1215a160e630SAndre Oppermann 		 * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored
1216a160e630SAndre Oppermann 		 * as they do not affect the state of the TCP FSM.
1217a160e630SAndre Oppermann 		 * The data pointed to by TH_URG and th_urp is ignored.
1218be2ac88cSJonathan Lemon 		 */
1219a160e630SAndre Oppermann 		KASSERT((thflags & (TH_RST|TH_ACK)) == 0,
1220a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_RST or TH_ACK set", __func__));
1221a160e630SAndre Oppermann 		KASSERT(thflags & (TH_SYN),
1222a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_SYN not set", __func__));
122333841545SHajimu UMEMOTO #ifdef INET6
122433841545SHajimu UMEMOTO 		/*
122533841545SHajimu UMEMOTO 		 * If deprecated address is forbidden,
122633841545SHajimu UMEMOTO 		 * we do not accept SYN to deprecated interface
122733841545SHajimu UMEMOTO 		 * address to prevent any new inbound connection from
122833841545SHajimu UMEMOTO 		 * getting established.
122933841545SHajimu UMEMOTO 		 * When we do not accept SYN, we send a TCP RST,
123033841545SHajimu UMEMOTO 		 * with deprecated source address (instead of dropping
123133841545SHajimu UMEMOTO 		 * it).  We compromise it as it is much better for peer
123233841545SHajimu UMEMOTO 		 * to send a RST, and RST will be the final packet
123333841545SHajimu UMEMOTO 		 * for the exchange.
123433841545SHajimu UMEMOTO 		 *
123533841545SHajimu UMEMOTO 		 * If we do not forbid deprecated addresses, we accept
123633841545SHajimu UMEMOTO 		 * the SYN packet.  RFC2462 does not suggest dropping
123733841545SHajimu UMEMOTO 		 * SYN in this case.
123833841545SHajimu UMEMOTO 		 * If we decipher RFC2462 5.5.4, it says like this:
123933841545SHajimu UMEMOTO 		 * 1. use of deprecated addr with existing
124033841545SHajimu UMEMOTO 		 *    communication is okay - "SHOULD continue to be
124133841545SHajimu UMEMOTO 		 *    used"
124233841545SHajimu UMEMOTO 		 * 2. use of it with new communication:
124333841545SHajimu UMEMOTO 		 *   (2a) "SHOULD NOT be used if alternate address
124433841545SHajimu UMEMOTO 		 *        with sufficient scope is available"
124533841545SHajimu UMEMOTO 		 *   (2b) nothing mentioned otherwise.
124633841545SHajimu UMEMOTO 		 * Here we fall into (2b) case as we have no choice in
124733841545SHajimu UMEMOTO 		 * our source address selection - we must obey the peer.
124833841545SHajimu UMEMOTO 		 *
124933841545SHajimu UMEMOTO 		 * The wording in RFC2462 is confusing, and there are
125033841545SHajimu UMEMOTO 		 * multiple description text for deprecated address
125133841545SHajimu UMEMOTO 		 * handling - worse, they are not exactly the same.
125233841545SHajimu UMEMOTO 		 * I believe 5.5.4 is the best one, so we follow 5.5.4.
125333841545SHajimu UMEMOTO 		 */
1254603724d3SBjoern A. Zeeb 		if (isipv6 && !V_ip6_use_deprecated) {
125533841545SHajimu UMEMOTO 			struct in6_ifaddr *ia6;
125633841545SHajimu UMEMOTO 
12573e88eb90SAndrey V. Elsukov 			ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
12588c0fec80SRobert Watson 			if (ia6 != NULL &&
125933841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
12608c0fec80SRobert Watson 				ifa_free(&ia6->ia_ifa);
1261a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1262a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12638d573cc1SAndre Oppermann 					"Connection attempt to deprecated "
12648d573cc1SAndre Oppermann 					"IPv6 address rejected\n",
1265a160e630SAndre Oppermann 					s, __func__);
126633841545SHajimu UMEMOTO 				rstreason = BANDLIM_RST_OPENPORT;
126733841545SHajimu UMEMOTO 				goto dropwithreset;
126833841545SHajimu UMEMOTO 			}
126977d396fdSMaksim Yevmenkin 			if (ia6)
12708c0fec80SRobert Watson 				ifa_free(&ia6->ia_ifa);
127133841545SHajimu UMEMOTO 		}
1272b287c6c7SBjoern A. Zeeb #endif /* INET6 */
127365f28919SJesper Skriver 		/*
1274db33b3e6SAndre Oppermann 		 * Basic sanity checks on incoming SYN requests:
12758d573cc1SAndre Oppermann 		 *   Don't respond if the destination is a link layer
1276db33b3e6SAndre Oppermann 		 *	broadcast according to RFC1122 4.2.3.10, p. 104.
12778d573cc1SAndre Oppermann 		 *   If it is from this socket it must be forged.
12788d573cc1SAndre Oppermann 		 *   Don't respond if the source or destination is a
12798d573cc1SAndre Oppermann 		 *	global or subnet broad- or multicast address.
128093ec91baSCrist J. Clark 		 *   Note that it is quite possible to receive unicast
128193ec91baSCrist J. Clark 		 *	link-layer packets with a broadcast IP address. Use
128293ec91baSCrist J. Clark 		 *	in_broadcast() to find them.
1283be2ac88cSJonathan Lemon 		 */
12848d573cc1SAndre Oppermann 		if (m->m_flags & (M_BCAST|M_MCAST)) {
12858d573cc1SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
12868d573cc1SAndre Oppermann 			    log(LOG_DEBUG, "%s; %s: Listen socket: "
12878d573cc1SAndre Oppermann 				"Connection attempt from broad- or multicast "
1288773673c1SAndre Oppermann 				"link layer address ignored\n", s, __func__);
1289302ce8d6SAndre Oppermann 			goto dropunlock;
12908d573cc1SAndre Oppermann 		}
1291db33b3e6SAndre Oppermann #ifdef INET6
1292b287c6c7SBjoern A. Zeeb 		if (isipv6) {
1293db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1294a160e630SAndre Oppermann 			    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) {
1295a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1296a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12978d573cc1SAndre Oppermann 					"Connection attempt to/from self "
1298773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1299302ce8d6SAndre Oppermann 				goto dropunlock;
1300a160e630SAndre Oppermann 			}
1301be2ac88cSJonathan Lemon 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1302a160e630SAndre Oppermann 			    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
1303a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1304a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13058d573cc1SAndre Oppermann 					"Connection attempt from/to multicast "
1306773673c1SAndre Oppermann 					"address ignored\n", s, __func__);
1307302ce8d6SAndre Oppermann 				goto dropunlock;
1308a160e630SAndre Oppermann 			}
1309b287c6c7SBjoern A. Zeeb 		}
1310db33b3e6SAndre Oppermann #endif
1311b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
1312b287c6c7SBjoern A. Zeeb 		else
1313b287c6c7SBjoern A. Zeeb #endif
1314b287c6c7SBjoern A. Zeeb #ifdef INET
1315b287c6c7SBjoern A. Zeeb 		{
1316db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1317a160e630SAndre Oppermann 			    ip->ip_dst.s_addr == ip->ip_src.s_addr) {
1318a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1319a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13208d573cc1SAndre Oppermann 					"Connection attempt from/to self "
1321773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1322302ce8d6SAndre Oppermann 				goto dropunlock;
1323a160e630SAndre Oppermann 			}
1324be2ac88cSJonathan Lemon 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
1325be2ac88cSJonathan Lemon 			    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
13262ca2159fSCrist J. Clark 			    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
1327a160e630SAndre Oppermann 			    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
1328a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1329a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13308d573cc1SAndre Oppermann 					"Connection attempt from/to broad- "
1331773673c1SAndre Oppermann 					"or multicast address ignored\n",
1332a160e630SAndre Oppermann 					s, __func__);
1333302ce8d6SAndre Oppermann 				goto dropunlock;
1334c068736aSJeffrey Hsu 			}
1335a160e630SAndre Oppermann 		}
1336b287c6c7SBjoern A. Zeeb #endif
1337be2ac88cSJonathan Lemon 		/*
1338db33b3e6SAndre Oppermann 		 * SYN appears to be valid.  Create compressed TCP state
1339db33b3e6SAndre Oppermann 		 * for syncache.
1340be2ac88cSJonathan Lemon 		 */
13413c653157SHartmut Brandt #ifdef TCPDEBUG
13423c653157SHartmut Brandt 		if (so->so_options & SO_DEBUG)
13433c653157SHartmut Brandt 			tcp_trace(TA_INPUT, ostate, tp,
13443c653157SHartmut Brandt 			    (void *)tcp_saveipgen, &tcp_savetcp, 0);
13453c653157SHartmut Brandt #endif
13462b9c9984SGeorge V. Neville-Neil 		TCP_PROBE3(debug__input, tp, th, m);
1347f72167f4SAndre Oppermann 		tcp_dooptions(&to, optp, optlen, TO_SYN);
1348281a0fd4SPatrick Kelsey 		if (syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL))
134909c305ebSPatrick Kelsey 			goto tfo_socket_result;
135018a75309SPatrick Kelsey 
1351be2ac88cSJonathan Lemon 		/*
13524d6e7130SAndre Oppermann 		 * Entry added to syncache and mbuf consumed.
1353a7c7f2a7SJohn Baldwin 		 * Only the listen socket is unlocked by syncache_add().
1354be2ac88cSJonathan Lemon 		 */
1355ff9b006dSJulien Charbon 		if (ti_locked == TI_RLOCKED) {
13566573d758SMatt Macy 			INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
1357a7c7f2a7SJohn Baldwin 			ti_locked = TI_UNLOCKED;
1358a7c7f2a7SJohn Baldwin 		}
1359603724d3SBjoern A. Zeeb 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
13608f5a8818SKevin Lo 		return (IPPROTO_DONE);
1361982c1675SAndre Oppermann 	} else if (tp->t_state == TCPS_LISTEN) {
1362982c1675SAndre Oppermann 		/*
1363982c1675SAndre Oppermann 		 * When a listen socket is torn down the SO_ACCEPTCONN
1364982c1675SAndre Oppermann 		 * flag is removed first while connections are drained
1365982c1675SAndre Oppermann 		 * from the accept queue in a unlock/lock cycle of the
1366982c1675SAndre Oppermann 		 * ACCEPT_LOCK, opening a race condition allowing a SYN
1367982c1675SAndre Oppermann 		 * attempt go through unhandled.
1368982c1675SAndre Oppermann 		 */
1369982c1675SAndre Oppermann 		goto dropunlock;
1370f76fcf6dSJeffrey Hsu 	}
1371fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1372fcf59617SAndrey V. Elsukov 	if (tp->t_flags & TF_SIGNATURE) {
1373fcf59617SAndrey V. Elsukov 		tcp_dooptions(&to, optp, optlen, thflags);
1374fcf59617SAndrey V. Elsukov 		if ((to.to_flags & TOF_SIGNATURE) == 0) {
1375fcf59617SAndrey V. Elsukov 			TCPSTAT_INC(tcps_sig_err_nosigopt);
13762903309aSAttilio Rao 			goto dropunlock;
13772903309aSAttilio Rao 		}
1378fcf59617SAndrey V. Elsukov 		if (!TCPMD5_ENABLED() ||
1379fcf59617SAndrey V. Elsukov 		    TCPMD5_INPUT(m, th, to.to_signature) != 0)
1380fcf59617SAndrey V. Elsukov 			goto dropunlock;
13812903309aSAttilio Rao 	}
13822903309aSAttilio Rao #endif
13832b9c9984SGeorge V. Neville-Neil 	TCP_PROBE5(receive, NULL, tp, m, tp, th);
138457f60867SMark Johnston 
1385302ce8d6SAndre Oppermann 	/*
13868d573cc1SAndre Oppermann 	 * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later
13878d573cc1SAndre Oppermann 	 * state.  tcp_do_segment() always consumes the mbuf chain, unlocks
13888d573cc1SAndre Oppermann 	 * the inpcb, and unlocks pcbinfo.
1389302ce8d6SAndre Oppermann 	 */
13906573d758SMatt Macy 	tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos);
13916573d758SMatt Macy 	if (ti_locked == TI_RLOCKED)
13926573d758SMatt Macy 		INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
13938f5a8818SKevin Lo 	return (IPPROTO_DONE);
1394be2ac88cSJonathan Lemon 
1395302ce8d6SAndre Oppermann dropwithreset:
13962b9c9984SGeorge V. Neville-Neil 	TCP_PROBE5(receive, NULL, tp, m, tp, th);
139757f60867SMark Johnston 
1398ff9b006dSJulien Charbon 	if (ti_locked == TI_RLOCKED) {
13996573d758SMatt Macy 		INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
1400252ca428SRobert Watson 		ti_locked = TI_UNLOCKED;
1401fa046d87SRobert Watson 	}
1402fa046d87SRobert Watson #ifdef INVARIANTS
1403fa046d87SRobert Watson 	else {
1404fa046d87SRobert Watson 		KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropwithreset "
1405fa046d87SRobert Watson 		    "ti_locked: %d", __func__, ti_locked));
1406fa046d87SRobert Watson 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1407fa046d87SRobert Watson 	}
1408fa046d87SRobert Watson #endif
1409014ea782SRobert Watson 
1410014ea782SRobert Watson 	if (inp != NULL) {
1411302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
1412014ea782SRobert Watson 		INP_WUNLOCK(inp);
1413dd8ac7f9SRobert Watson 	} else
1414014ea782SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
1415302ce8d6SAndre Oppermann 	m = NULL;	/* mbuf chain got consumed. */
1416014ea782SRobert Watson 	goto drop;
1417014ea782SRobert Watson 
1418302ce8d6SAndre Oppermann dropunlock:
141957f60867SMark Johnston 	if (m != NULL)
14202b9c9984SGeorge V. Neville-Neil 		TCP_PROBE5(receive, NULL, tp, m, tp, th);
142157f60867SMark Johnston 
1422ff9b006dSJulien Charbon 	if (ti_locked == TI_RLOCKED) {
14236573d758SMatt Macy 		INP_INFO_RUNLOCK_ET(&V_tcbinfo, et);
1424252ca428SRobert Watson 		ti_locked = TI_UNLOCKED;
1425fa046d87SRobert Watson 	}
1426fa046d87SRobert Watson #ifdef INVARIANTS
1427fa046d87SRobert Watson 	else {
1428fa046d87SRobert Watson 		KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropunlock "
1429fa046d87SRobert Watson 		    "ti_locked: %d", __func__, ti_locked));
1430fa046d87SRobert Watson 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1431fa046d87SRobert Watson 	}
1432fa046d87SRobert Watson #endif
1433252ca428SRobert Watson 
14349fa198beSAndre Oppermann 	if (inp != NULL)
14358501a69cSRobert Watson 		INP_WUNLOCK(inp);
1436014ea782SRobert Watson 
1437302ce8d6SAndre Oppermann drop:
1438603724d3SBjoern A. Zeeb 	INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1439a160e630SAndre Oppermann 	if (s != NULL)
1440a160e630SAndre Oppermann 		free(s, M_TCPLOG);
1441302ce8d6SAndre Oppermann 	if (m != NULL)
1442302ce8d6SAndre Oppermann 		m_freem(m);
14438f5a8818SKevin Lo 	return (IPPROTO_DONE);
1444302ce8d6SAndre Oppermann }
1445302ce8d6SAndre Oppermann 
1446e44c1887SSteven Hartland /*
1447e44c1887SSteven Hartland  * Automatic sizing of receive socket buffer.  Often the send
1448e44c1887SSteven Hartland  * buffer size is not optimally adjusted to the actual network
1449e44c1887SSteven Hartland  * conditions at hand (delay bandwidth product).  Setting the
1450e44c1887SSteven Hartland  * buffer size too small limits throughput on links with high
1451e44c1887SSteven Hartland  * bandwidth and high delay (eg. trans-continental/oceanic links).
1452e44c1887SSteven Hartland  *
1453e44c1887SSteven Hartland  * On the receive side the socket buffer memory is only rarely
1454e44c1887SSteven Hartland  * used to any significant extent.  This allows us to be much
1455e44c1887SSteven Hartland  * more aggressive in scaling the receive socket buffer.  For
1456e44c1887SSteven Hartland  * the case that the buffer space is actually used to a large
1457e44c1887SSteven Hartland  * extent and we run out of kernel memory we can simply drop
1458e44c1887SSteven Hartland  * the new segments; TCP on the sender will just retransmit it
1459e44c1887SSteven Hartland  * later.  Setting the buffer size too big may only consume too
1460e44c1887SSteven Hartland  * much kernel memory if the application doesn't read() from
1461e44c1887SSteven Hartland  * the socket or packet loss or reordering makes use of the
1462e44c1887SSteven Hartland  * reassembly queue.
1463e44c1887SSteven Hartland  *
1464e44c1887SSteven Hartland  * The criteria to step up the receive buffer one notch are:
1465e44c1887SSteven Hartland  *  1. Application has not set receive buffer size with
1466e44c1887SSteven Hartland  *     SO_RCVBUF. Setting SO_RCVBUF clears SB_AUTOSIZE.
1467e44c1887SSteven Hartland  *  2. the number of bytes received during the time it takes
1468e44c1887SSteven Hartland  *     one timestamp to be reflected back to us (the RTT);
1469e44c1887SSteven Hartland  *  3. received bytes per RTT is within seven eighth of the
1470e44c1887SSteven Hartland  *     current socket buffer size;
1471e44c1887SSteven Hartland  *  4. receive buffer size has not hit maximal automatic size;
1472e44c1887SSteven Hartland  *
1473e44c1887SSteven Hartland  * This algorithm does one step per RTT at most and only if
1474e44c1887SSteven Hartland  * we receive a bulk stream w/o packet losses or reorderings.
1475e44c1887SSteven Hartland  * Shrinking the buffer during idle times is not necessary as
1476e44c1887SSteven Hartland  * it doesn't consume any memory when idle.
1477e44c1887SSteven Hartland  *
1478e44c1887SSteven Hartland  * TODO: Only step up if the application is actually serving
1479e44c1887SSteven Hartland  * the buffer to better manage the socket buffer resources.
1480e44c1887SSteven Hartland  */
1481e44c1887SSteven Hartland int
1482e44c1887SSteven Hartland tcp_autorcvbuf(struct mbuf *m, struct tcphdr *th, struct socket *so,
1483e44c1887SSteven Hartland     struct tcpcb *tp, int tlen)
1484e44c1887SSteven Hartland {
1485e44c1887SSteven Hartland 	int newsize = 0;
1486e44c1887SSteven Hartland 
1487e44c1887SSteven Hartland 	if (V_tcp_do_autorcvbuf && (so->so_rcv.sb_flags & SB_AUTOSIZE) &&
1488e44c1887SSteven Hartland 	    tp->t_srtt != 0 && tp->rfbuf_ts != 0 &&
1489e44c1887SSteven Hartland 	    TCP_TS_TO_TICKS(tcp_ts_getticks() - tp->rfbuf_ts) >
1490e44c1887SSteven Hartland 	    (tp->t_srtt >> TCP_RTT_SHIFT)) {
1491e44c1887SSteven Hartland 		if (tp->rfbuf_cnt > (so->so_rcv.sb_hiwat / 8 * 7) &&
1492e44c1887SSteven Hartland 		    so->so_rcv.sb_hiwat < V_tcp_autorcvbuf_max) {
1493e44c1887SSteven Hartland 			newsize = min(so->so_rcv.sb_hiwat +
1494e44c1887SSteven Hartland 			    V_tcp_autorcvbuf_inc, V_tcp_autorcvbuf_max);
1495e44c1887SSteven Hartland 		}
1496e44c1887SSteven Hartland 		TCP_PROBE6(receive__autoresize, NULL, tp, m, tp, th, newsize);
1497e44c1887SSteven Hartland 
1498e44c1887SSteven Hartland 		/* Start over with next RTT. */
1499e44c1887SSteven Hartland 		tp->rfbuf_ts = 0;
1500e44c1887SSteven Hartland 		tp->rfbuf_cnt = 0;
1501e44c1887SSteven Hartland 	} else {
1502e44c1887SSteven Hartland 		tp->rfbuf_cnt += tlen;	/* add up */
1503e44c1887SSteven Hartland 	}
1504e44c1887SSteven Hartland 
1505e44c1887SSteven Hartland 	return (newsize);
1506e44c1887SSteven Hartland }
1507e44c1887SSteven Hartland 
150855bceb1eSRandall Stewart void
1509302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so,
15106573d758SMatt Macy     struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos)
1511302ce8d6SAndre Oppermann {
1512021eaf79SHiren Panchasara 	int thflags, acked, ourfinisacked, needoutput = 0, sack_changed;
1513302ce8d6SAndre Oppermann 	int rstreason, todrop, win;
15143ac12506SJonathan T. Looney 	uint32_t tiwin;
15154b7b743cSLawrence Stewart 	uint16_t nsegs;
151607dacf03SAndre Oppermann 	char *s;
151707dacf03SAndre Oppermann 	struct in_conninfo *inc;
1518c11a15bfSGleb Smirnoff 	struct mbuf *mfree;
1519302ce8d6SAndre Oppermann 	struct tcpopt to;
1520281a0fd4SPatrick Kelsey 	int tfo_syn;
1521302ce8d6SAndre Oppermann 
152214739780SMaxim Konovalov #ifdef TCPDEBUG
152314739780SMaxim Konovalov 	/*
152414739780SMaxim Konovalov 	 * The size of tcp_saveipgen must be the size of the max ip header,
152514739780SMaxim Konovalov 	 * now IPv6.
152614739780SMaxim Konovalov 	 */
152714739780SMaxim Konovalov 	u_char tcp_saveipgen[IP6_HDR_LEN];
152814739780SMaxim Konovalov 	struct tcphdr tcp_savetcp;
152914739780SMaxim Konovalov 	short ostate = 0;
153014739780SMaxim Konovalov #endif
1531302ce8d6SAndre Oppermann 	thflags = th->th_flags;
153207dacf03SAndre Oppermann 	inc = &tp->t_inpcb->inp_inc;
1533d64a46eaSLawrence Stewart 	tp->sackhint.last_sack_ack = 0;
1534021eaf79SHiren Panchasara 	sack_changed = 0;
15354b7b743cSLawrence Stewart 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
1536252ca428SRobert Watson 	/*
1537252ca428SRobert Watson 	 * If this is either a state-changing packet or current state isn't
1538252ca428SRobert Watson 	 * established, we require a write lock on tcbinfo.  Otherwise, we
15390989f56cSRobert Watson 	 * allow the tcbinfo to be in either alocked or unlocked, as the
15400989f56cSRobert Watson 	 * caller may have unnecessarily acquired a write lock due to a race.
1541252ca428SRobert Watson 	 */
1542252ca428SRobert Watson 	if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 ||
1543252ca428SRobert Watson 	    tp->t_state != TCPS_ESTABLISHED) {
1544ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1545252ca428SRobert Watson 	}
15468501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1547679d9708SAndre Oppermann 	KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
1548679d9708SAndre Oppermann 	    __func__));
1549679d9708SAndre Oppermann 	KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
1550679d9708SAndre Oppermann 	    __func__));
1551df8bae1dSRodney W. Grimes 
155286a996e6SHiren Panchasara #ifdef TCPPCAP
155386a996e6SHiren Panchasara 	/* Save segment, if requested. */
155486a996e6SHiren Panchasara 	tcp_pcap_add(th, m, &(tp->t_inpkts));
155586a996e6SHiren Panchasara #endif
15562529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0,
15572529f56eSJonathan T. Looney 	    tlen, NULL, true);
155886a996e6SHiren Panchasara 
1559013f4df6SMichael Tuexen 	if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
1560013f4df6SMichael Tuexen 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
1561013f4df6SMichael Tuexen 			log(LOG_DEBUG, "%s; %s: "
1562013f4df6SMichael Tuexen 			    "SYN|FIN segment ignored (based on "
1563013f4df6SMichael Tuexen 			    "sysctl setting)\n", s, __func__);
1564013f4df6SMichael Tuexen 			free(s, M_TCPLOG);
1565013f4df6SMichael Tuexen 		}
1566013f4df6SMichael Tuexen 		goto drop;
1567013f4df6SMichael Tuexen 	}
1568013f4df6SMichael Tuexen 
1569df8bae1dSRodney W. Grimes 	/*
1570ebfd7534SMichael Tuexen 	 * If a segment with the ACK-bit set arrives in the SYN-SENT state
1571ebfd7534SMichael Tuexen 	 * check SEQ.ACK first.
1572ebfd7534SMichael Tuexen 	 */
1573ebfd7534SMichael Tuexen 	if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) &&
1574ebfd7534SMichael Tuexen 	    (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) {
1575ebfd7534SMichael Tuexen 		rstreason = BANDLIM_UNLIMITED;
1576ebfd7534SMichael Tuexen 		goto dropwithreset;
1577ebfd7534SMichael Tuexen 	}
1578ebfd7534SMichael Tuexen 
1579ebfd7534SMichael Tuexen 	/*
1580df8bae1dSRodney W. Grimes 	 * Segment received on connection.
1581df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
1582e8949f74SAndre Oppermann 	 * XXX: This should be done after segment
1583e8949f74SAndre Oppermann 	 * validation to ignore broken/spoofed segs.
1584df8bae1dSRodney W. Grimes 	 */
15859b8b58e0SJonathan Lemon 	tp->t_rcvtime = ticks;
1586df8bae1dSRodney W. Grimes 
1587df8bae1dSRodney W. Grimes 	/*
1588c52102ddSHiren Panchasara 	 * Scale up the window into a 32-bit value.
1589e8949f74SAndre Oppermann 	 * For the SYN_SENT state the scale is zero.
1590464fcfbcSAndre Oppermann 	 */
1591464fcfbcSAndre Oppermann 	tiwin = th->th_win << tp->snd_scale;
1592464fcfbcSAndre Oppermann 
1593464fcfbcSAndre Oppermann 	/*
1594f2512ba1SRui Paulo 	 * TCP ECN processing.
1595f2512ba1SRui Paulo 	 */
1596f2512ba1SRui Paulo 	if (tp->t_flags & TF_ECN_PERMIT) {
15979c251892SRui Paulo 		if (thflags & TH_CWR)
15989c251892SRui Paulo 			tp->t_flags &= ~TF_ECN_SND_ECE;
1599f2512ba1SRui Paulo 		switch (iptos & IPTOS_ECN_MASK) {
1600f2512ba1SRui Paulo 		case IPTOS_ECN_CE:
1601f2512ba1SRui Paulo 			tp->t_flags |= TF_ECN_SND_ECE;
160278b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ce);
1603f2512ba1SRui Paulo 			break;
1604f2512ba1SRui Paulo 		case IPTOS_ECN_ECT0:
160578b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ect0);
1606f2512ba1SRui Paulo 			break;
1607f2512ba1SRui Paulo 		case IPTOS_ECN_ECT1:
160878b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ect1);
1609f2512ba1SRui Paulo 			break;
1610f2512ba1SRui Paulo 		}
161164807b30SHiren Panchasara 
161264807b30SHiren Panchasara 		/* Process a packet differently from RFC3168. */
161364807b30SHiren Panchasara 		cc_ecnpkt_handler(tp, th, iptos);
161464807b30SHiren Panchasara 
1615dbc42409SLawrence Stewart 		/* Congestion experienced. */
1616dbc42409SLawrence Stewart 		if (thflags & TH_ECE) {
1617dbc42409SLawrence Stewart 			cc_cong_signal(tp, th, CC_ECN);
1618f2512ba1SRui Paulo 		}
1619f2512ba1SRui Paulo 	}
1620f2512ba1SRui Paulo 
1621f2512ba1SRui Paulo 	/*
1622f72167f4SAndre Oppermann 	 * Parse options on any incoming segment.
1623f72167f4SAndre Oppermann 	 */
1624302ce8d6SAndre Oppermann 	tcp_dooptions(&to, (u_char *)(th + 1),
1625302ce8d6SAndre Oppermann 	    (th->th_off << 2) - sizeof(struct tcphdr),
1626302ce8d6SAndre Oppermann 	    (thflags & TH_SYN) ? TO_SYN : 0);
1627f72167f4SAndre Oppermann 
1628fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1629fcf59617SAndrey V. Elsukov 	if ((tp->t_flags & TF_SIGNATURE) != 0 &&
1630fcf59617SAndrey V. Elsukov 	    (to.to_flags & TOF_SIGNATURE) == 0) {
1631fcf59617SAndrey V. Elsukov 		TCPSTAT_INC(tcps_sig_err_sigopt);
1632fcf59617SAndrey V. Elsukov 		/* XXX: should drop? */
1633fcf59617SAndrey V. Elsukov 	}
1634fcf59617SAndrey V. Elsukov #endif
1635f72167f4SAndre Oppermann 	/*
1636f72167f4SAndre Oppermann 	 * If echoed timestamp is later than the current time,
1637bf6d304aSAndre Oppermann 	 * fall back to non RFC1323 RTT calculation.  Normalize
1638bf6d304aSAndre Oppermann 	 * timestamp if syncookies were used when this connection
1639bf6d304aSAndre Oppermann 	 * was established.
1640f72167f4SAndre Oppermann 	 */
1641bf6d304aSAndre Oppermann 	if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
1642e16fa5caSJohn-Mark Gurney 		to.to_tsecr -= tp->ts_offset;
1643d8951c8aSBjoern A. Zeeb 		if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks()))
1644f72167f4SAndre Oppermann 			to.to_tsecr = 0;
164510d20c84SMatt Macy 		else if (tp->t_flags & TF_PREVVALID &&
164610d20c84SMatt Macy 			 tp->t_badrxtwin != 0 && SEQ_LT(to.to_tsecr, tp->t_badrxtwin))
164710d20c84SMatt Macy 			cc_cong_signal(tp, th, CC_RTO_ERR);
1648bf6d304aSAndre Oppermann 	}
164907dacf03SAndre Oppermann 	/*
165097d8d152SAndre Oppermann 	 * Process options only when we get SYN/ACK back. The SYN case
165197d8d152SAndre Oppermann 	 * for incoming connections is handled in tcp_syncache.
16525396d0f8SAndre Oppermann 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN>
16535396d0f8SAndre Oppermann 	 * or <SYN,ACK>) segment itself is never scaled.
165497d8d152SAndre Oppermann 	 * XXX this is traditional behavior, may need to be cleaned up.
1655df8bae1dSRodney W. Grimes 	 */
16560a389eabSSimon L. B. Nielsen 	if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
1657464fcfbcSAndre Oppermann 		if ((to.to_flags & TOF_SCALE) &&
1658464fcfbcSAndre Oppermann 		    (tp->t_flags & TF_REQ_SCALE)) {
1659be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_SCALE;
166002a1a643SAndre Oppermann 			tp->snd_scale = to.to_wscale;
1661be2ac88cSJonathan Lemon 		}
16625396d0f8SAndre Oppermann 		/*
16635396d0f8SAndre Oppermann 		 * Initial send window.  It will be updated with
16645396d0f8SAndre Oppermann 		 * the next incoming segment to the scaled value.
16655396d0f8SAndre Oppermann 		 */
16665396d0f8SAndre Oppermann 		tp->snd_wnd = th->th_win;
1667be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_TS) {
1668be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_TSTMP;
1669be2ac88cSJonathan Lemon 			tp->ts_recent = to.to_tsval;
1670d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
1671be2ac88cSJonathan Lemon 		}
1672be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_MSS)
1673be2ac88cSJonathan Lemon 			tcp_mss(tp, to.to_mss);
16743529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
16753529149eSAndre Oppermann 		    (to.to_flags & TOF_SACKPERM) == 0)
16763529149eSAndre Oppermann 			tp->t_flags &= ~TF_SACK_PERMIT;
1677c560df6fSPatrick Kelsey 		if (IS_FASTOPEN(tp->t_flags)) {
1678a026a53aSMichael Tuexen 			if (to.to_flags & TOF_FASTOPEN) {
1679a026a53aSMichael Tuexen 				uint16_t mss;
1680a026a53aSMichael Tuexen 
1681a026a53aSMichael Tuexen 				if (to.to_flags & TOF_MSS)
1682a026a53aSMichael Tuexen 					mss = to.to_mss;
1683c560df6fSPatrick Kelsey 				else
1684a026a53aSMichael Tuexen 					if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
1685a026a53aSMichael Tuexen 						mss = TCP6_MSS;
1686a026a53aSMichael Tuexen 					else
1687a026a53aSMichael Tuexen 						mss = TCP_MSS;
1688a026a53aSMichael Tuexen 				tcp_fastopen_update_cache(tp, mss,
1689a026a53aSMichael Tuexen 				    to.to_tfo_len, to.to_tfo_cookie);
1690a026a53aSMichael Tuexen 			} else
1691c560df6fSPatrick Kelsey 				tcp_fastopen_disable_path(tp);
1692c560df6fSPatrick Kelsey 		}
16936d90faf3SPaul Saab 	}
16946d90faf3SPaul Saab 
1695df8bae1dSRodney W. Grimes 	/*
16964da9052aSMichael Tuexen 	 * If timestamps were negotiated during SYN/ACK they should
16974da9052aSMichael Tuexen 	 * appear on every segment during this session and vice versa.
16984da9052aSMichael Tuexen 	 */
16994da9052aSMichael Tuexen 	if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) {
17004da9052aSMichael Tuexen 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
17014da9052aSMichael Tuexen 			log(LOG_DEBUG, "%s; %s: Timestamp missing, "
17024da9052aSMichael Tuexen 			    "no action\n", s, __func__);
17034da9052aSMichael Tuexen 			free(s, M_TCPLOG);
17044da9052aSMichael Tuexen 		}
17054da9052aSMichael Tuexen 	}
17064da9052aSMichael Tuexen 	if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) {
17074da9052aSMichael Tuexen 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
17084da9052aSMichael Tuexen 			log(LOG_DEBUG, "%s; %s: Timestamp not expected, "
17094da9052aSMichael Tuexen 			    "no action\n", s, __func__);
17104da9052aSMichael Tuexen 			free(s, M_TCPLOG);
17114da9052aSMichael Tuexen 		}
17124da9052aSMichael Tuexen 	}
17134da9052aSMichael Tuexen 
17144da9052aSMichael Tuexen 	/*
1715df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
1716df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
1717df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
1718df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
1719df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
1720df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
1721df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
1722df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
1723df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
1724df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
1725df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
1726df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
1727a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
1728c5ad39b9SAndre Oppermann 	 * Since we check for TCPS_ESTABLISHED first, it can only
1729a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
1730df8bae1dSRodney W. Grimes 	 */
1731df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
1732c5ad39b9SAndre Oppermann 	    th->th_seq == tp->rcv_nxt &&
1733fb59c426SYoshinobu Inoue 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1734c5ad39b9SAndre Oppermann 	    tp->snd_nxt == tp->snd_max &&
1735c5ad39b9SAndre Oppermann 	    tiwin && tiwin == tp->snd_wnd &&
1736a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
17374741bfcbSPatrick Kelsey 	    LIST_EMPTY(&tp->t_segq) &&
17384741bfcbSPatrick Kelsey 	    ((to.to_flags & TOF_TS) == 0 ||
1739c5ad39b9SAndre Oppermann 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) {
1740df8bae1dSRodney W. Grimes 
1741df8bae1dSRodney W. Grimes 		/*
1742df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
1743df8bae1dSRodney W. Grimes 		 * record the timestamp.
1744a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
1745a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1746df8bae1dSRodney W. Grimes 		 */
1747be2ac88cSJonathan Lemon 		if ((to.to_flags & TOF_TS) != 0 &&
1748fb59c426SYoshinobu Inoue 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1749d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
1750a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
1751df8bae1dSRodney W. Grimes 		}
1752df8bae1dSRodney W. Grimes 
1753fb59c426SYoshinobu Inoue 		if (tlen == 0) {
1754fb59c426SYoshinobu Inoue 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
1755fb59c426SYoshinobu Inoue 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
1756dbc42409SLawrence Stewart 			    !IN_RECOVERY(tp->t_flags) &&
1757fc30a251SAndre Oppermann 			    (to.to_flags & TOF_SACK) == 0 &&
1758dbc42409SLawrence Stewart 			    TAILQ_EMPTY(&tp->snd_holes)) {
1759df8bae1dSRodney W. Grimes 				/*
1760e8949f74SAndre Oppermann 				 * This is a pure ack for outstanding data.
1761df8bae1dSRodney W. Grimes 				 */
176278b50714SRobert Watson 				TCPSTAT_INC(tcps_predack);
1763252ca428SRobert Watson 
17649b8b58e0SJonathan Lemon 				/*
176510d20c84SMatt Macy 				 * "bad retransmit" recovery without timestamps.
17669b8b58e0SJonathan Lemon 				 */
176710d20c84SMatt Macy 				if ((to.to_flags & TOF_TS) == 0 &&
176810d20c84SMatt Macy 				    tp->t_rxtshift == 1 &&
1769672dc4aeSJohn Baldwin 				    tp->t_flags & TF_PREVVALID &&
17706dfb8b31SJohn Baldwin 				    (int)(ticks - tp->t_badrxtwin) < 0) {
1771dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_RTO_ERR);
17729b8b58e0SJonathan Lemon 				}
1773fa55172bSMatthew Dillon 
1774fa55172bSMatthew Dillon 				/*
1775fa55172bSMatthew Dillon 				 * Recalculate the transmit timer / rtt.
1776fa55172bSMatthew Dillon 				 *
1777fa55172bSMatthew Dillon 				 * Some boxes send broken timestamp replies
1778fa55172bSMatthew Dillon 				 * during the SYN+ACK phase, ignore
1779fa55172bSMatthew Dillon 				 * timestamps of 0 or we could calculate a
1780fa55172bSMatthew Dillon 				 * huge RTT and blow up the retransmit timer.
1781fa55172bSMatthew Dillon 				 */
1782fa55172bSMatthew Dillon 				if ((to.to_flags & TOF_TS) != 0 &&
1783fa55172bSMatthew Dillon 				    to.to_tsecr) {
17843ac12506SJonathan T. Looney 					uint32_t t;
1785d8951c8aSBjoern A. Zeeb 
1786d8951c8aSBjoern A. Zeeb 					t = tcp_ts_getticks() - to.to_tsecr;
1787d8951c8aSBjoern A. Zeeb 					if (!tp->t_rttlow || tp->t_rttlow > t)
1788d8951c8aSBjoern A. Zeeb 						tp->t_rttlow = t;
1789a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
1790d8951c8aSBjoern A. Zeeb 					    TCP_TS_TO_TICKS(t) + 1);
1791fa55172bSMatthew Dillon 				} else if (tp->t_rtttime &&
1792fa55172bSMatthew Dillon 				    SEQ_GT(th->th_ack, tp->t_rtseq)) {
1793eaf80179SAndre Oppermann 					if (!tp->t_rttlow ||
1794eaf80179SAndre Oppermann 					    tp->t_rttlow > ticks - tp->t_rtttime)
1795eaf80179SAndre Oppermann 						tp->t_rttlow = ticks - tp->t_rtttime;
1796c068736aSJeffrey Hsu 					tcp_xmit_timer(tp,
1797c068736aSJeffrey Hsu 							ticks - tp->t_rtttime);
1798fa55172bSMatthew Dillon 				}
1799dbc42409SLawrence Stewart 				acked = BYTES_THIS_ACK(tp, th);
180039bc9de5SLawrence Stewart 
1801bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
180239bc9de5SLawrence Stewart 				/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
180339bc9de5SLawrence Stewart 				hhook_run_tcp_est_in(tp, th, &to);
1804bd79708dSJonathan T. Looney #endif
180539bc9de5SLawrence Stewart 
18064b7b743cSLawrence Stewart 				TCPSTAT_ADD(tcps_rcvackpack, nsegs);
180778b50714SRobert Watson 				TCPSTAT_ADD(tcps_rcvackbyte, acked);
1808df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
18099d11646dSJeffrey Hsu 				if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
18109d11646dSJeffrey Hsu 				    SEQ_LEQ(th->th_ack, tp->snd_recover))
18119d11646dSJeffrey Hsu 					tp->snd_recover = th->th_ack - 1;
1812dbc42409SLawrence Stewart 
1813dbc42409SLawrence Stewart 				/*
1814dbc42409SLawrence Stewart 				 * Let the congestion control algorithm update
1815dbc42409SLawrence Stewart 				 * congestion control related information. This
1816dbc42409SLawrence Stewart 				 * typically means increasing the congestion
1817dbc42409SLawrence Stewart 				 * window.
1818dbc42409SLawrence Stewart 				 */
18194b7b743cSLawrence Stewart 				cc_ack_received(tp, th, nsegs, CC_ACK);
1820dbc42409SLawrence Stewart 
18219d11646dSJeffrey Hsu 				tp->snd_una = th->th_ack;
18221645d090SJeff Roberson 				/*
1823e8949f74SAndre Oppermann 				 * Pull snd_wl2 up to prevent seq wrap relative
18241645d090SJeff Roberson 				 * to th_ack.
18251645d090SJeff Roberson 				 */
1826cb942153SJeffrey Hsu 				tp->snd_wl2 = th->th_ack;
1827b5addd85SJeffrey Hsu 				tp->t_dupacks = 0;
1828df8bae1dSRodney W. Grimes 				m_freem(m);
1829df8bae1dSRodney W. Grimes 
1830df8bae1dSRodney W. Grimes 				/*
1831df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
1832df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
1833df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
1834df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
1835df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
1836df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
1837df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
1838e8949f74SAndre Oppermann 				 */
18393c653157SHartmut Brandt #ifdef TCPDEBUG
18403c653157SHartmut Brandt 				if (so->so_options & SO_DEBUG)
18413c653157SHartmut Brandt 					tcp_trace(TA_INPUT, ostate, tp,
18423c653157SHartmut Brandt 					    (void *)tcp_saveipgen,
18433c653157SHartmut Brandt 					    &tcp_savetcp, 0);
18443c653157SHartmut Brandt #endif
18452b9c9984SGeorge V. Neville-Neil 				TCP_PROBE3(debug__input, tp, th, m);
1846df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
1847b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
1848b8152ba7SAndre Oppermann 				else if (!tcp_timer_active(tp, TT_PERSIST))
1849b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT,
1850b8152ba7SAndre Oppermann 						      tp->t_rxtcur);
1851df8bae1dSRodney W. Grimes 				sowwakeup(so);
1852cfa6009eSGleb Smirnoff 				if (sbavail(&so->so_snd))
185355bceb1eSRandall Stewart 					(void) tp->t_fb->tfb_tcp_output(tp);
1854a14c749fSJonathan Lemon 				goto check_delack;
1855df8bae1dSRodney W. Grimes 			}
1856fb59c426SYoshinobu Inoue 		} else if (th->th_ack == tp->snd_una &&
1857fb59c426SYoshinobu Inoue 		    tlen <= sbspace(&so->so_rcv)) {
18586741ecf5SAndre Oppermann 			int newsize = 0;	/* automatic sockbuf scaling */
18596741ecf5SAndre Oppermann 
1860df8bae1dSRodney W. Grimes 			/*
1861252ca428SRobert Watson 			 * This is a pure, in-sequence data packet with
1862252ca428SRobert Watson 			 * nothing on the reassembly queue and we have enough
1863252ca428SRobert Watson 			 * buffer space to take it.
1864df8bae1dSRodney W. Grimes 			 */
18656d90faf3SPaul Saab 			/* Clean receiver SACK report if present */
18663529149eSAndre Oppermann 			if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks)
18676d90faf3SPaul Saab 				tcp_clean_sackreport(tp);
186878b50714SRobert Watson 			TCPSTAT_INC(tcps_preddat);
1869fb59c426SYoshinobu Inoue 			tp->rcv_nxt += tlen;
18701645d090SJeff Roberson 			/*
18711645d090SJeff Roberson 			 * Pull snd_wl1 up to prevent seq wrap relative to
18721645d090SJeff Roberson 			 * th_seq.
18731645d090SJeff Roberson 			 */
187460ee3bb2SAndre Oppermann 			tp->snd_wl1 = th->th_seq;
18751645d090SJeff Roberson 			/*
18761645d090SJeff Roberson 			 * Pull rcv_up up to prevent seq wrap relative to
18771645d090SJeff Roberson 			 * rcv_nxt.
18781645d090SJeff Roberson 			 */
18791645d090SJeff Roberson 			tp->rcv_up = tp->rcv_nxt;
18804b7b743cSLawrence Stewart 			TCPSTAT_ADD(tcps_rcvpack, nsegs);
188178b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
18823c653157SHartmut Brandt #ifdef TCPDEBUG
18833c653157SHartmut Brandt 			if (so->so_options & SO_DEBUG)
18843c653157SHartmut Brandt 				tcp_trace(TA_INPUT, ostate, tp,
18853c653157SHartmut Brandt 				    (void *)tcp_saveipgen, &tcp_savetcp, 0);
18863c653157SHartmut Brandt #endif
18872b9c9984SGeorge V. Neville-Neil 			TCP_PROBE3(debug__input, tp, th, m);
18885d06879aSGeorge V. Neville-Neil 
1889e44c1887SSteven Hartland 			newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
18906741ecf5SAndre Oppermann 
18916741ecf5SAndre Oppermann 			/* Add data to socket buffer. */
18921e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
1893c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1894c1c36a2cSMike Silbersack 				m_freem(m);
1895c1c36a2cSMike Silbersack 			} else {
18966741ecf5SAndre Oppermann 				/*
18976741ecf5SAndre Oppermann 				 * Set new socket buffer size.
18986741ecf5SAndre Oppermann 				 * Give up when limit is reached.
18996741ecf5SAndre Oppermann 				 */
19006741ecf5SAndre Oppermann 				if (newsize)
19016741ecf5SAndre Oppermann 					if (!sbreserve_locked(&so->so_rcv,
19026c8286e4SRobert Watson 					    newsize, so, NULL))
19036741ecf5SAndre Oppermann 						so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
1904fb59c426SYoshinobu Inoue 				m_adj(m, drop_hdrlen);	/* delayed header drop */
1905651e4e6aSGleb Smirnoff 				sbappendstream_locked(&so->so_rcv, m, 0);
1906c1c36a2cSMike Silbersack 			}
1907e8949f74SAndre Oppermann 			/* NB: sorwakeup_locked() does an implicit unlock. */
19081e4d7da7SRobert Watson 			sorwakeup_locked(so);
1909c1e5a6e5SAndre Oppermann 			if (DELAY_ACK(tp, tlen)) {
19103bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
1911f498eeeeSDavid Greenman 			} else {
1912e612a582SDavid Greenman 				tp->t_flags |= TF_ACKNOW;
191355bceb1eSRandall Stewart 				tp->t_fb->tfb_tcp_output(tp);
1914e612a582SDavid Greenman 			}
1915a14c749fSJonathan Lemon 			goto check_delack;
1916df8bae1dSRodney W. Grimes 		}
1917df8bae1dSRodney W. Grimes 	}
1918df8bae1dSRodney W. Grimes 
1919df8bae1dSRodney W. Grimes 	/*
1920df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
1921df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
1922df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
1923df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
1924df8bae1dSRodney W. Grimes 	 */
1925df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
1926df8bae1dSRodney W. Grimes 	if (win < 0)
1927df8bae1dSRodney W. Grimes 		win = 0;
192866e39adcSJohn Polstra 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1929df8bae1dSRodney W. Grimes 
1930df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1931df8bae1dSRodney W. Grimes 
1932df8bae1dSRodney W. Grimes 	/*
1933764d8cefSBill Fenner 	 * If the state is SYN_RECEIVED:
1934764d8cefSBill Fenner 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
1935764d8cefSBill Fenner 	 */
1936764d8cefSBill Fenner 	case TCPS_SYN_RECEIVED:
1937fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1938fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1939a57815efSBosko Milekic 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1940a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
1941a57815efSBosko Milekic 				goto dropwithreset;
1942a57815efSBosko Milekic 		}
194368bd7ed1SJonathan T. Looney 		if (IS_FASTOPEN(tp->t_flags)) {
1944281a0fd4SPatrick Kelsey 			/*
1945281a0fd4SPatrick Kelsey 			 * When a TFO connection is in SYN_RECEIVED, the
1946281a0fd4SPatrick Kelsey 			 * only valid packets are the initial SYN, a
1947281a0fd4SPatrick Kelsey 			 * retransmit/copy of the initial SYN (possibly with
1948281a0fd4SPatrick Kelsey 			 * a subset of the original data), a valid ACK, a
1949281a0fd4SPatrick Kelsey 			 * FIN, or a RST.
1950281a0fd4SPatrick Kelsey 			 */
1951281a0fd4SPatrick Kelsey 			if ((thflags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) {
1952281a0fd4SPatrick Kelsey 				rstreason = BANDLIM_RST_OPENPORT;
1953281a0fd4SPatrick Kelsey 				goto dropwithreset;
1954281a0fd4SPatrick Kelsey 			} else if (thflags & TH_SYN) {
1955281a0fd4SPatrick Kelsey 				/* non-initial SYN is ignored */
1956281a0fd4SPatrick Kelsey 				if ((tcp_timer_active(tp, TT_DELACK) ||
1957281a0fd4SPatrick Kelsey 				     tcp_timer_active(tp, TT_REXMT)))
1958281a0fd4SPatrick Kelsey 					goto drop;
1959281a0fd4SPatrick Kelsey 			} else if (!(thflags & (TH_ACK|TH_FIN|TH_RST))) {
1960281a0fd4SPatrick Kelsey 				goto drop;
1961281a0fd4SPatrick Kelsey 			}
1962281a0fd4SPatrick Kelsey 		}
1963764d8cefSBill Fenner 		break;
1964764d8cefSBill Fenner 
1965764d8cefSBill Fenner 	/*
1966df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
196798732609SMichael Tuexen 	 *	if seg contains a RST with valid ACK (SEQ.ACK has already
196898732609SMichael Tuexen 	 *	    been verified), then drop the connection.
196998732609SMichael Tuexen 	 *	if seg contains a RST without an ACK, drop the seg.
197098732609SMichael Tuexen 	 *	if seg does not contain SYN, then drop the seg.
1971df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
1972df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
1973df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
1974f2512ba1SRui Paulo 	 *	if seg contains an ECE and ECN support is enabled, the stream
1975f2512ba1SRui Paulo 	 *	    is ECN capable.
1976df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1977df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
1978df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
1979df8bae1dSRodney W. Grimes 	 */
1980df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
198157f60867SMark Johnston 		if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) {
1982d9fae5abSAndriy Gapon 			TCP_PROBE5(connect__refused, NULL, tp,
19832b9c9984SGeorge V. Neville-Neil 			    m, tp, th);
1984df8bae1dSRodney W. Grimes 			tp = tcp_drop(tp, ECONNREFUSED);
198557f60867SMark Johnston 		}
19860ca3f933SAndre Oppermann 		if (thflags & TH_RST)
1987df8bae1dSRodney W. Grimes 			goto drop;
19880ca3f933SAndre Oppermann 		if (!(thflags & TH_SYN))
1989df8bae1dSRodney W. Grimes 			goto drop;
1990464fcfbcSAndre Oppermann 
1991fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
1992df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
1993fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1994c560df6fSPatrick Kelsey 			int tfo_partial_ack = 0;
1995c560df6fSPatrick Kelsey 
199678b50714SRobert Watson 			TCPSTAT_INC(tcps_connects);
1997845799c1SAndras Olah 			soisconnected(so);
1998c488362eSRobert Watson #ifdef MAC
199930d239bcSRobert Watson 			mac_socketpeer_set_from_mbuf(m, so);
2000c488362eSRobert Watson #endif
2001845799c1SAndras Olah 			/* Do window scaling on this connection? */
2002845799c1SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2003845799c1SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
2004845799c1SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2005845799c1SAndras Olah 			}
20063ac12506SJonathan T. Looney 			tp->rcv_adv += min(tp->rcv_wnd,
2007766282cbSJohn Baldwin 			    TCP_MAXWIN << tp->rcv_scale);
2008a0292f23SGarrett Wollman 			tp->snd_una++;		/* SYN is acked */
2009a0292f23SGarrett Wollman 			/*
2010c560df6fSPatrick Kelsey 			 * If not all the data that was sent in the TFO SYN
2011c560df6fSPatrick Kelsey 			 * has been acked, resend the remainder right away.
2012c560df6fSPatrick Kelsey 			 */
2013c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags) &&
2014c560df6fSPatrick Kelsey 			    (tp->snd_una != tp->snd_max)) {
2015c560df6fSPatrick Kelsey 				tp->snd_nxt = th->th_ack;
2016c560df6fSPatrick Kelsey 				tfo_partial_ack = 1;
2017c560df6fSPatrick Kelsey 			}
2018c560df6fSPatrick Kelsey 			/*
2019a0292f23SGarrett Wollman 			 * If there's data, delay ACK; if there's also a FIN
2020a0292f23SGarrett Wollman 			 * ACKNOW will be turned on later.
2021a0292f23SGarrett Wollman 			 */
2022c560df6fSPatrick Kelsey 			if (DELAY_ACK(tp, tlen) && tlen != 0 && !tfo_partial_ack)
2023b8152ba7SAndre Oppermann 				tcp_timer_activate(tp, TT_DELACK,
2024b8152ba7SAndre Oppermann 				    tcp_delacktime);
2025a0292f23SGarrett Wollman 			else
2026a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
2027f2512ba1SRui Paulo 
2028603724d3SBjoern A. Zeeb 			if ((thflags & TH_ECE) && V_tcp_do_ecn) {
2029f2512ba1SRui Paulo 				tp->t_flags |= TF_ECN_PERMIT;
203078b50714SRobert Watson 				TCPSTAT_INC(tcps_ecn_shs);
2031f2512ba1SRui Paulo 			}
2032f2512ba1SRui Paulo 
2033a0292f23SGarrett Wollman 			/*
2034a0292f23SGarrett Wollman 			 * Received <SYN,ACK> in SYN_SENT[*] state.
2035a0292f23SGarrett Wollman 			 * Transitions:
2036a0292f23SGarrett Wollman 			 *	SYN_SENT  --> ESTABLISHED
2037a0292f23SGarrett Wollman 			 *	SYN_SENT* --> FIN_WAIT_1
2038a0292f23SGarrett Wollman 			 */
20399b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
2040a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDFIN) {
204157f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_1);
2042a0292f23SGarrett Wollman 				tp->t_flags &= ~TF_NEEDFIN;
2043fb59c426SYoshinobu Inoue 				thflags &= ~TH_SYN;
20447ff19458SPaul Traina 			} else {
204557f60867SMark Johnston 				tcp_state_change(tp, TCPS_ESTABLISHED);
2046d9fae5abSAndriy Gapon 				TCP_PROBE5(connect__established, NULL, tp,
20472b9c9984SGeorge V. Neville-Neil 				    m, tp, th);
2048dbc42409SLawrence Stewart 				cc_conn_init(tp);
20499077f387SGleb Smirnoff 				tcp_timer_activate(tp, TT_KEEP,
20509077f387SGleb Smirnoff 				    TP_KEEPIDLE(tp));
20517ff19458SPaul Traina 			}
2052a0292f23SGarrett Wollman 		} else {
2053a0292f23SGarrett Wollman 			/*
2054c068736aSJeffrey Hsu 			 * Received initial SYN in SYN-SENT[*] state =>
2055153edc50SHiren Panchasara 			 * simultaneous open.
2056c068736aSJeffrey Hsu 			 * If it succeeds, connection is * half-synchronized.
2057c068736aSJeffrey Hsu 			 * Otherwise, do 3-way handshake:
2058a0292f23SGarrett Wollman 			 *        SYN-SENT -> SYN-RECEIVED
2059a0292f23SGarrett Wollman 			 *        SYN-SENT* -> SYN-RECEIVED*
2060a0292f23SGarrett Wollman 			 */
20614b8e98d6SQing Li 			tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
2062b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
206357f60867SMark Johnston 			tcp_state_change(tp, TCPS_SYN_RECEIVED);
2064a0292f23SGarrett Wollman 		}
2065df8bae1dSRodney W. Grimes 
2066ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
20678501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
20687cfc6904SRobert Watson 
2069df8bae1dSRodney W. Grimes 		/*
2070fb59c426SYoshinobu Inoue 		 * Advance th->th_seq to correspond to first data byte.
2071df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
2072df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
2073df8bae1dSRodney W. Grimes 		 */
2074fb59c426SYoshinobu Inoue 		th->th_seq++;
2075fb59c426SYoshinobu Inoue 		if (tlen > tp->rcv_wnd) {
2076fb59c426SYoshinobu Inoue 			todrop = tlen - tp->rcv_wnd;
2077df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
2078fb59c426SYoshinobu Inoue 			tlen = tp->rcv_wnd;
2079fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
208078b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpackafterwin);
208178b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2082df8bae1dSRodney W. Grimes 		}
2083fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
2084fb59c426SYoshinobu Inoue 		tp->rcv_up = th->th_seq;
2085a0292f23SGarrett Wollman 		/*
2086a0292f23SGarrett Wollman 		 * Client side of transaction: already sent SYN and data.
2087a0292f23SGarrett Wollman 		 * If the remote host used T/TCP to validate the SYN,
2088a0292f23SGarrett Wollman 		 * our data will be ACK'd; if so, enter normal data segment
2089a0292f23SGarrett Wollman 		 * processing in the middle of step 5, ack processing.
2090a0292f23SGarrett Wollman 		 * Otherwise, goto step 6.
2091a0292f23SGarrett Wollman 		 */
2092fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK)
2093a0292f23SGarrett Wollman 			goto process_ACK;
2094c068736aSJeffrey Hsu 
2095df8bae1dSRodney W. Grimes 		goto step6;
2096c068736aSJeffrey Hsu 
2097a0292f23SGarrett Wollman 	/*
2098a0292f23SGarrett Wollman 	 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
2099c94c54e4SAndre Oppermann 	 *      do normal processing.
2100a0292f23SGarrett Wollman 	 *
2101c94c54e4SAndre Oppermann 	 * NB: Leftover from RFC1644 T/TCP.  Cases to be reused later.
2102a0292f23SGarrett Wollman 	 */
2103a0292f23SGarrett Wollman 	case TCPS_LAST_ACK:
2104a0292f23SGarrett Wollman 	case TCPS_CLOSING:
2105a0292f23SGarrett Wollman 		break;  /* continue normal processing */
2106df8bae1dSRodney W. Grimes 	}
2107df8bae1dSRodney W. Grimes 
2108df8bae1dSRodney W. Grimes 	/*
2109df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
211080ab7c0eSGarrett Wollman 	 * First check the RST flag and sequence number since reset segments
211180ab7c0eSGarrett Wollman 	 * are exempt from the timestamp and connection count tests.  This
211280ab7c0eSGarrett Wollman 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
211380ab7c0eSGarrett Wollman 	 * below which allowed reset segments in half the sequence space
211480ab7c0eSGarrett Wollman 	 * to fall though and be processed (which gives forged reset
211580ab7c0eSGarrett Wollman 	 * segments with a random sequence number a 50 percent chance of
211680ab7c0eSGarrett Wollman 	 * killing a connection).
211780ab7c0eSGarrett Wollman 	 * Then check timestamp, if present.
2118a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
2119df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
2120df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
2121df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
212280ab7c0eSGarrett Wollman 	 */
2123fb59c426SYoshinobu Inoue 	if (thflags & TH_RST) {
21243220a212SGleb Smirnoff 		/*
21253220a212SGleb Smirnoff 		 * RFC5961 Section 3.2
21263220a212SGleb Smirnoff 		 *
21273220a212SGleb Smirnoff 		 * - RST drops connection only if SEG.SEQ == RCV.NXT.
21283220a212SGleb Smirnoff 		 * - If RST is in window, we send challenge ACK.
21293220a212SGleb Smirnoff 		 *
21303220a212SGleb Smirnoff 		 * Note: to take into account delayed ACKs, we should
21313220a212SGleb Smirnoff 		 *   test against last_ack_sent instead of rcv_nxt.
21323220a212SGleb Smirnoff 		 * Note 2: we handle special case of closed window, not
21333220a212SGleb Smirnoff 		 *   covered by the RFC.
21343220a212SGleb Smirnoff 		 */
21353220a212SGleb Smirnoff 		if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
21363220a212SGleb Smirnoff 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
21373220a212SGleb Smirnoff 		    (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) {
213880ab7c0eSGarrett Wollman 
2139ff9b006dSJulien Charbon 			INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
21403220a212SGleb Smirnoff 			KASSERT(tp->t_state != TCPS_SYN_SENT,
21413220a212SGleb Smirnoff 			    ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p",
21423220a212SGleb Smirnoff 			    __func__, th, tp));
21433220a212SGleb Smirnoff 
21443220a212SGleb Smirnoff 			if (V_tcp_insecure_rst ||
21453220a212SGleb Smirnoff 			    tp->last_ack_sent == th->th_seq) {
21463220a212SGleb Smirnoff 				TCPSTAT_INC(tcps_drops);
21473220a212SGleb Smirnoff 				/* Drop the connection. */
21483220a212SGleb Smirnoff 				switch (tp->t_state) {
214980ab7c0eSGarrett Wollman 				case TCPS_SYN_RECEIVED:
215080ab7c0eSGarrett Wollman 					so->so_error = ECONNREFUSED;
215180ab7c0eSGarrett Wollman 					goto close;
215280ab7c0eSGarrett Wollman 				case TCPS_ESTABLISHED:
215380ab7c0eSGarrett Wollman 				case TCPS_FIN_WAIT_1:
215480ab7c0eSGarrett Wollman 				case TCPS_FIN_WAIT_2:
215580ab7c0eSGarrett Wollman 				case TCPS_CLOSE_WAIT:
21566779a1a1SMichael Tuexen 				case TCPS_CLOSING:
21576779a1a1SMichael Tuexen 				case TCPS_LAST_ACK:
215880ab7c0eSGarrett Wollman 					so->so_error = ECONNRESET;
215980ab7c0eSGarrett Wollman 				close:
21603220a212SGleb Smirnoff 					/* FALLTHROUGH */
21613220a212SGleb Smirnoff 				default:
216280ab7c0eSGarrett Wollman 					tp = tcp_close(tp);
21633220a212SGleb Smirnoff 				}
21643220a212SGleb Smirnoff 			} else {
21653220a212SGleb Smirnoff 				TCPSTAT_INC(tcps_badrst);
21663220a212SGleb Smirnoff 				/* Send challenge ACK. */
21673220a212SGleb Smirnoff 				tcp_respond(tp, mtod(m, void *), th, m,
21683220a212SGleb Smirnoff 				    tp->rcv_nxt, tp->snd_nxt, TH_ACK);
21693220a212SGleb Smirnoff 				tp->last_ack_sent = tp->rcv_nxt;
21703220a212SGleb Smirnoff 				m = NULL;
21713220a212SGleb Smirnoff 			}
21723220a212SGleb Smirnoff 		}
21733220a212SGleb Smirnoff 		goto drop;
21743220a212SGleb Smirnoff 	}
217580ab7c0eSGarrett Wollman 
21763220a212SGleb Smirnoff 	/*
21773220a212SGleb Smirnoff 	 * RFC5961 Section 4.2
21783220a212SGleb Smirnoff 	 * Send challenge ACK for any SYN in synchronized state.
21793220a212SGleb Smirnoff 	 */
2180281a0fd4SPatrick Kelsey 	if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT &&
2181281a0fd4SPatrick Kelsey 	    tp->t_state != TCPS_SYN_RECEIVED) {
2182ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2183252ca428SRobert Watson 
21843220a212SGleb Smirnoff 		TCPSTAT_INC(tcps_badsyn);
21853220a212SGleb Smirnoff 		if (V_tcp_insecure_syn &&
21863220a212SGleb Smirnoff 		    SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
21873220a212SGleb Smirnoff 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
21883220a212SGleb Smirnoff 			tp = tcp_drop(tp, ECONNRESET);
21893220a212SGleb Smirnoff 			rstreason = BANDLIM_UNLIMITED;
21903220a212SGleb Smirnoff 		} else {
21913220a212SGleb Smirnoff 			/* Send challenge ACK. */
21923220a212SGleb Smirnoff 			tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt,
21933220a212SGleb Smirnoff 			    tp->snd_nxt, TH_ACK);
21943220a212SGleb Smirnoff 			tp->last_ack_sent = tp->rcv_nxt;
21953220a212SGleb Smirnoff 			m = NULL;
219680ab7c0eSGarrett Wollman 		}
219780ab7c0eSGarrett Wollman 		goto drop;
219880ab7c0eSGarrett Wollman 	}
219980ab7c0eSGarrett Wollman 
220080ab7c0eSGarrett Wollman 	/*
2201df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
2202df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
2203df8bae1dSRodney W. Grimes 	 */
2204be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
220580ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
2206df8bae1dSRodney W. Grimes 
2207df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
2208d8951c8aSBjoern A. Zeeb 		if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) {
2209df8bae1dSRodney W. Grimes 			/*
2210df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
2211df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
2212df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
2213df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
2214df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
2215df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
2216df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
2217df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
2218df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
2219df8bae1dSRodney W. Grimes 			 */
2220df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
2221df8bae1dSRodney W. Grimes 		} else {
222278b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
222378b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, tlen);
222478b50714SRobert Watson 			TCPSTAT_INC(tcps_pawsdrop);
222507fd333dSMatthew Dillon 			if (tlen)
2226df8bae1dSRodney W. Grimes 				goto dropafterack;
22271ab4789dSMatthew Dillon 			goto drop;
22281ab4789dSMatthew Dillon 		}
2229df8bae1dSRodney W. Grimes 	}
2230df8bae1dSRodney W. Grimes 
2231a0292f23SGarrett Wollman 	/*
223280ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
223380ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
223480ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
223580ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
223680ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
223780ab7c0eSGarrett Wollman 	 */
2238a57815efSBosko Milekic 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
2239a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2240a57815efSBosko Milekic 		goto dropwithreset;
2241a57815efSBosko Milekic 	}
224280ab7c0eSGarrett Wollman 
2243fb59c426SYoshinobu Inoue 	todrop = tp->rcv_nxt - th->th_seq;
2244df8bae1dSRodney W. Grimes 	if (todrop > 0) {
2245831ad37eSXin LI 		if (thflags & TH_SYN) {
2246fb59c426SYoshinobu Inoue 			thflags &= ~TH_SYN;
2247fb59c426SYoshinobu Inoue 			th->th_seq++;
2248fb59c426SYoshinobu Inoue 			if (th->th_urp > 1)
2249fb59c426SYoshinobu Inoue 				th->th_urp--;
2250df8bae1dSRodney W. Grimes 			else
2251fb59c426SYoshinobu Inoue 				thflags &= ~TH_URG;
2252df8bae1dSRodney W. Grimes 			todrop--;
2253df8bae1dSRodney W. Grimes 		}
2254df8bae1dSRodney W. Grimes 		/*
2255dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
2256df8bae1dSRodney W. Grimes 		 */
2257fb59c426SYoshinobu Inoue 		if (todrop > tlen
2258fb59c426SYoshinobu Inoue 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
2259dac20301SGarrett Wollman 			/*
2260dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
2261dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
2262dac20301SGarrett Wollman 			 * of sequence; drop it.
2263dac20301SGarrett Wollman 			 */
2264fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
2265dac20301SGarrett Wollman 
2266df8bae1dSRodney W. Grimes 			/*
2267dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
2268dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
2269df8bae1dSRodney W. Grimes 			 */
2270dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
2271fb59c426SYoshinobu Inoue 			todrop = tlen;
227278b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
227378b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, todrop);
2274df8bae1dSRodney W. Grimes 		} else {
227578b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpartduppack);
227678b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop);
2277df8bae1dSRodney W. Grimes 		}
2278fb59c426SYoshinobu Inoue 		drop_hdrlen += todrop;	/* drop from the top afterwards */
2279fb59c426SYoshinobu Inoue 		th->th_seq += todrop;
2280fb59c426SYoshinobu Inoue 		tlen -= todrop;
2281fb59c426SYoshinobu Inoue 		if (th->th_urp > todrop)
2282fb59c426SYoshinobu Inoue 			th->th_urp -= todrop;
2283df8bae1dSRodney W. Grimes 		else {
2284fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;
2285fb59c426SYoshinobu Inoue 			th->th_urp = 0;
2286df8bae1dSRodney W. Grimes 		}
2287df8bae1dSRodney W. Grimes 	}
2288df8bae1dSRodney W. Grimes 
2289df8bae1dSRodney W. Grimes 	/*
2290df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
2291df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
2292df8bae1dSRodney W. Grimes 	 */
2293df8bae1dSRodney W. Grimes 	if ((so->so_state & SS_NOFDREF) &&
2294fb59c426SYoshinobu Inoue 	    tp->t_state > TCPS_CLOSE_WAIT && tlen) {
2295ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2296252ca428SRobert Watson 
229707dacf03SAndre Oppermann 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
229807dacf03SAndre Oppermann 			log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data "
229907dacf03SAndre Oppermann 			    "after socket was closed, "
230007dacf03SAndre Oppermann 			    "sending RST and removing tcpcb\n",
2301e31d8aa3SMike Silbersack 			    s, __func__, tcpstates[tp->t_state], tlen);
2302773673c1SAndre Oppermann 			free(s, M_TCPLOG);
2303773673c1SAndre Oppermann 		}
2304df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
230578b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvafterclose);
2306a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
2307df8bae1dSRodney W. Grimes 		goto dropwithreset;
2308df8bae1dSRodney W. Grimes 	}
2309df8bae1dSRodney W. Grimes 
2310df8bae1dSRodney W. Grimes 	/*
2311df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
2312df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
2313df8bae1dSRodney W. Grimes 	 */
2314fb59c426SYoshinobu Inoue 	todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
2315df8bae1dSRodney W. Grimes 	if (todrop > 0) {
231678b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvpackafterwin);
2317fb59c426SYoshinobu Inoue 		if (todrop >= tlen) {
231878b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen);
2319df8bae1dSRodney W. Grimes 			/*
2320df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
2321df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
2322df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
2323df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
2324df8bae1dSRodney W. Grimes 			 * and ack.
2325df8bae1dSRodney W. Grimes 			 */
2326fb59c426SYoshinobu Inoue 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
2327df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
232878b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvwinprobe);
2329df8bae1dSRodney W. Grimes 			} else
2330df8bae1dSRodney W. Grimes 				goto dropafterack;
2331df8bae1dSRodney W. Grimes 		} else
233278b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2333df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
2334fb59c426SYoshinobu Inoue 		tlen -= todrop;
2335fb59c426SYoshinobu Inoue 		thflags &= ~(TH_PUSH|TH_FIN);
2336df8bae1dSRodney W. Grimes 	}
2337df8bae1dSRodney W. Grimes 
2338df8bae1dSRodney W. Grimes 	/*
2339df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
2340df8bae1dSRodney W. Grimes 	 * record its timestamp.
2341cf09195bSPaul Saab 	 * NOTE:
2342cf09195bSPaul Saab 	 * 1) That the test incorporates suggestions from the latest
2343a0292f23SGarrett Wollman 	 *    proposal of the tcplw@cray.com list (Braden 1993/04/26).
2344cf09195bSPaul Saab 	 * 2) That updating only on newer timestamps interferes with
2345cf09195bSPaul Saab 	 *    our earlier PAWS tests, so this check should be solely
2346cf09195bSPaul Saab 	 *    predicated on the sequence space of this segment.
2347cf09195bSPaul Saab 	 * 3) That we modify the segment boundary check to be
2348cf09195bSPaul Saab 	 *        Last.ACK.Sent <= SEG.SEQ + SEG.Len
2349cf09195bSPaul Saab 	 *    instead of RFC1323's
2350cf09195bSPaul Saab 	 *        Last.ACK.Sent < SEG.SEQ + SEG.Len,
2351cf09195bSPaul Saab 	 *    This modified check allows us to overcome RFC1323's
2352cf09195bSPaul Saab 	 *    limitations as described in Stevens TCP/IP Illustrated
2353cf09195bSPaul Saab 	 *    Vol. 2 p.869. In such cases, we can still calculate the
2354cf09195bSPaul Saab 	 *    RTT correctly when RCV.NXT == Last.ACK.Sent.
2355df8bae1dSRodney W. Grimes 	 */
2356be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 &&
2357cf09195bSPaul Saab 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
2358cf09195bSPaul Saab 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
2359cf09195bSPaul Saab 		((thflags & (TH_SYN|TH_FIN)) != 0))) {
2360d8951c8aSBjoern A. Zeeb 		tp->ts_recent_age = tcp_ts_getticks();
2361a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
2362df8bae1dSRodney W. Grimes 	}
2363df8bae1dSRodney W. Grimes 
2364df8bae1dSRodney W. Grimes 	/*
2365a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
2366a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
2367a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
2368a0292f23SGarrett Wollman 	 */
2369fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) == 0) {
2370a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
2371281a0fd4SPatrick Kelsey 		    (tp->t_flags & TF_NEEDSYN)) {
2372281a0fd4SPatrick Kelsey 			if (tp->t_state == TCPS_SYN_RECEIVED &&
237368bd7ed1SJonathan T. Looney 			    IS_FASTOPEN(tp->t_flags)) {
2374281a0fd4SPatrick Kelsey 				tp->snd_wnd = tiwin;
2375281a0fd4SPatrick Kelsey 				cc_conn_init(tp);
2376281a0fd4SPatrick Kelsey 			}
2377a0292f23SGarrett Wollman 			goto step6;
2378281a0fd4SPatrick Kelsey 		} else if (tp->t_flags & TF_ACKNOW)
23795e1aa279SDavid Malone 			goto dropafterack;
2380a0292f23SGarrett Wollman 		else
2381a0292f23SGarrett Wollman 			goto drop;
2382a0292f23SGarrett Wollman 	}
2383df8bae1dSRodney W. Grimes 
2384df8bae1dSRodney W. Grimes 	/*
2385df8bae1dSRodney W. Grimes 	 * Ack processing.
2386df8bae1dSRodney W. Grimes 	 */
2387df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2388df8bae1dSRodney W. Grimes 
2389df8bae1dSRodney W. Grimes 	/*
2390764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
2391764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
2392764d8cefSBill Fenner 	 * The ACK was checked above.
2393df8bae1dSRodney W. Grimes 	 */
2394df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2395a0292f23SGarrett Wollman 
239678b50714SRobert Watson 		TCPSTAT_INC(tcps_connects);
2397df8bae1dSRodney W. Grimes 		soisconnected(so);
2398df8bae1dSRodney W. Grimes 		/* Do window scaling? */
2399df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2400df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
2401df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
2402464fcfbcSAndre Oppermann 			tp->snd_wnd = tiwin;
2403df8bae1dSRodney W. Grimes 		}
2404a0292f23SGarrett Wollman 		/*
2405a0292f23SGarrett Wollman 		 * Make transitions:
2406a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
2407a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
2408a0292f23SGarrett Wollman 		 */
24099b8b58e0SJonathan Lemon 		tp->t_starttime = ticks;
241018a75309SPatrick Kelsey 		if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) {
2411281a0fd4SPatrick Kelsey 			tcp_fastopen_decrement_counter(tp->t_tfo_pending);
2412281a0fd4SPatrick Kelsey 			tp->t_tfo_pending = NULL;
2413281a0fd4SPatrick Kelsey 
2414281a0fd4SPatrick Kelsey 			/*
2415281a0fd4SPatrick Kelsey 			 * Account for the ACK of our SYN prior to
2416281a0fd4SPatrick Kelsey 			 * regular ACK processing below.
2417281a0fd4SPatrick Kelsey 			 */
2418281a0fd4SPatrick Kelsey 			tp->snd_una++;
2419281a0fd4SPatrick Kelsey 		}
2420*8db239dcSMichael Tuexen 		if (tp->t_flags & TF_NEEDFIN) {
2421*8db239dcSMichael Tuexen 			tcp_state_change(tp, TCPS_FIN_WAIT_1);
2422*8db239dcSMichael Tuexen 			tp->t_flags &= ~TF_NEEDFIN;
2423*8db239dcSMichael Tuexen 		} else {
2424*8db239dcSMichael Tuexen 			tcp_state_change(tp, TCPS_ESTABLISHED);
2425*8db239dcSMichael Tuexen 			TCP_PROBE5(accept__established, NULL, tp,
2426*8db239dcSMichael Tuexen 			    m, tp, th);
2427281a0fd4SPatrick Kelsey 			/*
2428281a0fd4SPatrick Kelsey 			 * TFO connections call cc_conn_init() during SYN
2429281a0fd4SPatrick Kelsey 			 * processing.  Calling it again here for such
2430281a0fd4SPatrick Kelsey 			 * connections is not harmless as it would undo the
2431281a0fd4SPatrick Kelsey 			 * snd_cwnd reduction that occurs when a TFO SYN|ACK
2432281a0fd4SPatrick Kelsey 			 * is retransmitted.
2433281a0fd4SPatrick Kelsey 			 */
243468bd7ed1SJonathan T. Looney 			if (!IS_FASTOPEN(tp->t_flags))
2435dbc42409SLawrence Stewart 				cc_conn_init(tp);
24369077f387SGleb Smirnoff 			tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp));
24377ff19458SPaul Traina 		}
2438a0292f23SGarrett Wollman 		/*
2439a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
2440a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
2441a0292f23SGarrett Wollman 		 */
2442fb59c426SYoshinobu Inoue 		if (tlen == 0 && (thflags & TH_FIN) == 0)
2443fb59c426SYoshinobu Inoue 			(void) tcp_reass(tp, (struct tcphdr *)0, 0,
2444a0292f23SGarrett Wollman 			    (struct mbuf *)0);
244560ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq - 1;
244693b0017fSPhilippe Charnier 		/* FALLTHROUGH */
2447df8bae1dSRodney W. Grimes 
2448df8bae1dSRodney W. Grimes 	/*
2449df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
2450df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
2451fb59c426SYoshinobu Inoue 	 *	tp->snd_una < th->th_ack <= tp->snd_max
2452fb59c426SYoshinobu Inoue 	 * then advance tp->snd_una to th->th_ack and drop
2453df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
2454df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
2455df8bae1dSRodney W. Grimes 	 */
2456df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
2457df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
2458df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
2459df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
2460df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
2461df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
24625a53ca16SPaul Saab 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
246378b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvacktoomuch);
24645a53ca16SPaul Saab 			goto dropafterack;
24655a53ca16SPaul Saab 		}
24663529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
2467fc30a251SAndre Oppermann 		    ((to.to_flags & TOF_SACK) ||
2468fc30a251SAndre Oppermann 		     !TAILQ_EMPTY(&tp->snd_holes)))
2469021eaf79SHiren Panchasara 			sack_changed = tcp_sack_doack(tp, &to, th->th_ack);
247012eeb81fSHiren Panchasara 		else
247112eeb81fSHiren Panchasara 			/*
247212eeb81fSHiren Panchasara 			 * Reset the value so that previous (valid) value
247312eeb81fSHiren Panchasara 			 * from the last ack with SACK doesn't get used.
247412eeb81fSHiren Panchasara 			 */
247512eeb81fSHiren Panchasara 			tp->sackhint.sacked_bytes = 0;
247639bc9de5SLawrence Stewart 
2477bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
247839bc9de5SLawrence Stewart 		/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
247939bc9de5SLawrence Stewart 		hhook_run_tcp_est_in(tp, th, &to);
2480bd79708dSJonathan T. Looney #endif
248139bc9de5SLawrence Stewart 
2482fb59c426SYoshinobu Inoue 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
24830c39d38dSGleb Smirnoff 			u_int maxseg;
24840c39d38dSGleb Smirnoff 
24850c39d38dSGleb Smirnoff 			maxseg = tcp_maxseg(tp);
2486021eaf79SHiren Panchasara 			if (tlen == 0 &&
2487021eaf79SHiren Panchasara 			    (tiwin == tp->snd_wnd ||
2488021eaf79SHiren Panchasara 			    (tp->t_flags & TF_SACK_PERMIT))) {
248962b90589SPeter Wemm 				/*
249062b90589SPeter Wemm 				 * If this is the first time we've seen a
249162b90589SPeter Wemm 				 * FIN from the remote, this is not a
249262b90589SPeter Wemm 				 * duplicate and it needs to be processed
249362b90589SPeter Wemm 				 * normally.  This happens during a
249462b90589SPeter Wemm 				 * simultaneous close.
249562b90589SPeter Wemm 				 */
249662b90589SPeter Wemm 				if ((thflags & TH_FIN) &&
249762b90589SPeter Wemm 				    (TCPS_HAVERCVDFIN(tp->t_state) == 0)) {
249862b90589SPeter Wemm 					tp->t_dupacks = 0;
249962b90589SPeter Wemm 					break;
250062b90589SPeter Wemm 				}
250178b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvdupack);
2502df8bae1dSRodney W. Grimes 				/*
2503df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
2504df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
2505df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
25065f30ec9bSEitan Adler 				 * change and FIN isn't set),
25075f30ec9bSEitan Adler 				 * the ack is the biggest we've
2508df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
2509a4641f4eSPedro F. Giffuni 				 * threshold of them, assume a packet
2510df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
2511df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
2512df8bae1dSRodney W. Grimes 				 * window so we send only this one
2513df8bae1dSRodney W. Grimes 				 * packet.
2514df8bae1dSRodney W. Grimes 				 *
2515df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
2516df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
2517df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
2518df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
2519df8bae1dSRodney W. Grimes 				 * the new ssthresh).
2520df8bae1dSRodney W. Grimes 				 *
2521df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
2522df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
2523df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
2524df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
2525df8bae1dSRodney W. Grimes 				 * network.
2526f2512ba1SRui Paulo 				 *
2527f2512ba1SRui Paulo 				 * When using TCP ECN, notify the peer that
2528f2512ba1SRui Paulo 				 * we reduced the cwnd.
2529df8bae1dSRodney W. Grimes 				 */
2530021eaf79SHiren Panchasara 				/*
2531021eaf79SHiren Panchasara 				 * Following 2 kinds of acks should not affect
2532021eaf79SHiren Panchasara 				 * dupack counting:
2533021eaf79SHiren Panchasara 				 * 1) Old acks
2534021eaf79SHiren Panchasara 				 * 2) Acks with SACK but without any new SACK
2535021eaf79SHiren Panchasara 				 * information in them. These could result from
2536021eaf79SHiren Panchasara 				 * any anomaly in the network like a switch
2537021eaf79SHiren Panchasara 				 * duplicating packets or a possible DoS attack.
2538021eaf79SHiren Panchasara 				 */
2539021eaf79SHiren Panchasara 				if (th->th_ack != tp->snd_una ||
2540021eaf79SHiren Panchasara 				    ((tp->t_flags & TF_SACK_PERMIT) &&
2541021eaf79SHiren Panchasara 				    !sack_changed))
2542021eaf79SHiren Panchasara 					break;
2543021eaf79SHiren Panchasara 				else if (!tcp_timer_active(tp, TT_REXMT))
2544df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
2545cb942153SJeffrey Hsu 				else if (++tp->t_dupacks > tcprexmtthresh ||
2546dbc42409SLawrence Stewart 				     IN_FASTRECOVERY(tp->t_flags)) {
25474b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
25484b7b743cSLawrence Stewart 					    CC_DUPACK);
25493529149eSAndre Oppermann 					if ((tp->t_flags & TF_SACK_PERMIT) &&
2550dbc42409SLawrence Stewart 					    IN_FASTRECOVERY(tp->t_flags)) {
2551808f11b7SPaul Saab 						int awnd;
255225e6f9edSPaul Saab 
255325e6f9edSPaul Saab 						/*
255425e6f9edSPaul Saab 						 * Compute the amount of data in flight first.
25554c3972f0SHiren Panchasara 						 * We can inject new data into the pipe iff
255625e6f9edSPaul Saab 						 * we have less than 1/2 the original window's
255725e6f9edSPaul Saab 						 * worth of data in flight.
255825e6f9edSPaul Saab 						 */
255912eeb81fSHiren Panchasara 						if (V_tcp_do_rfc6675_pipe)
256012eeb81fSHiren Panchasara 							awnd = tcp_compute_pipe(tp);
256112eeb81fSHiren Panchasara 						else
2562808f11b7SPaul Saab 							awnd = (tp->snd_nxt - tp->snd_fack) +
25630077b016SPaul Saab 								tp->sackhint.sack_bytes_rexmit;
256412eeb81fSHiren Panchasara 
2565808f11b7SPaul Saab 						if (awnd < tp->snd_ssthresh) {
25660c39d38dSGleb Smirnoff 							tp->snd_cwnd += maxseg;
256725e6f9edSPaul Saab 							if (tp->snd_cwnd > tp->snd_ssthresh)
256825e6f9edSPaul Saab 								tp->snd_cwnd = tp->snd_ssthresh;
256925e6f9edSPaul Saab 						}
257025e6f9edSPaul Saab 					} else
25710c39d38dSGleb Smirnoff 						tp->snd_cwnd += maxseg;
257255bceb1eSRandall Stewart 					(void) tp->t_fb->tfb_tcp_output(tp);
257346f58482SJonathan Lemon 					goto drop;
2574cb942153SJeffrey Hsu 				} else if (tp->t_dupacks == tcprexmtthresh) {
2575cb942153SJeffrey Hsu 					tcp_seq onxt = tp->snd_nxt;
2576a0445c2eSJayanth Vijayaraghavan 
2577a0445c2eSJayanth Vijayaraghavan 					/*
2578a0445c2eSJayanth Vijayaraghavan 					 * If we're doing sack, check to
2579a0445c2eSJayanth Vijayaraghavan 					 * see if we're already in sack
2580a0445c2eSJayanth Vijayaraghavan 					 * recovery. If we're not doing sack,
2581a0445c2eSJayanth Vijayaraghavan 					 * check to see if we're in newreno
2582a0445c2eSJayanth Vijayaraghavan 					 * recovery.
2583a0445c2eSJayanth Vijayaraghavan 					 */
25843529149eSAndre Oppermann 					if (tp->t_flags & TF_SACK_PERMIT) {
2585dbc42409SLawrence Stewart 						if (IN_FASTRECOVERY(tp->t_flags)) {
2586a0445c2eSJayanth Vijayaraghavan 							tp->t_dupacks = 0;
2587a0445c2eSJayanth Vijayaraghavan 							break;
2588a0445c2eSJayanth Vijayaraghavan 						}
2589dbc42409SLawrence Stewart 					} else {
2590a0445c2eSJayanth Vijayaraghavan 						if (SEQ_LEQ(th->th_ack,
25919d11646dSJeffrey Hsu 						    tp->snd_recover)) {
2592cb942153SJeffrey Hsu 							tp->t_dupacks = 0;
2593cb942153SJeffrey Hsu 							break;
259446f58482SJonathan Lemon 						}
2595a0445c2eSJayanth Vijayaraghavan 					}
2596dbc42409SLawrence Stewart 					/* Congestion signal before ack. */
2597dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_NDUPACK);
25984b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
25994b7b743cSLawrence Stewart 					    CC_DUPACK);
2600b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
26019b8b58e0SJonathan Lemon 					tp->t_rtttime = 0;
26023529149eSAndre Oppermann 					if (tp->t_flags & TF_SACK_PERMIT) {
260378b50714SRobert Watson 						TCPSTAT_INC(
260478b50714SRobert Watson 						    tcps_sack_recovery_episode);
2605a55db2b6SPaul Saab 						tp->sack_newdata = tp->snd_nxt;
26060c39d38dSGleb Smirnoff 						tp->snd_cwnd = maxseg;
260755bceb1eSRandall Stewart 						(void) tp->t_fb->tfb_tcp_output(tp);
26086d90faf3SPaul Saab 						goto drop;
26096d90faf3SPaul Saab 					}
2610fb59c426SYoshinobu Inoue 					tp->snd_nxt = th->th_ack;
26110c39d38dSGleb Smirnoff 					tp->snd_cwnd = maxseg;
261255bceb1eSRandall Stewart 					(void) tp->t_fb->tfb_tcp_output(tp);
261348d2549cSJeffrey Hsu 					KASSERT(tp->snd_limited <= 2,
2614302ce8d6SAndre Oppermann 					    ("%s: tp->snd_limited too big",
2615302ce8d6SAndre Oppermann 					    __func__));
2616df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
26170c39d38dSGleb Smirnoff 					     maxseg *
261848d2549cSJeffrey Hsu 					     (tp->t_dupacks - tp->snd_limited);
2619df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
2620df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
2621df8bae1dSRodney W. Grimes 					goto drop;
2622603724d3SBjoern A. Zeeb 				} else if (V_tcp_do_rfc3042) {
262362d4443fSHiren Panchasara 					/*
262462d4443fSHiren Panchasara 					 * Process first and second duplicate
262562d4443fSHiren Panchasara 					 * ACKs. Each indicates a segment
262662d4443fSHiren Panchasara 					 * leaving the network, creating room
262762d4443fSHiren Panchasara 					 * for more. Make sure we can send a
262862d4443fSHiren Panchasara 					 * packet on reception of each duplicate
262962d4443fSHiren Panchasara 					 * ACK by increasing snd_cwnd by one
263062d4443fSHiren Panchasara 					 * segment. Restore the original
263162d4443fSHiren Panchasara 					 * snd_cwnd after packet transmission.
263262d4443fSHiren Panchasara 					 */
26334b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
26344b7b743cSLawrence Stewart 					    CC_DUPACK);
26353ac12506SJonathan T. Looney 					uint32_t oldcwnd = tp->snd_cwnd;
263648d2549cSJeffrey Hsu 					tcp_seq oldsndmax = tp->snd_max;
263748d2549cSJeffrey Hsu 					u_int sent;
26385628dd08SAndre Oppermann 					int avail;
263989c02376SJeffrey Hsu 
2640582a954bSJeffrey Hsu 					KASSERT(tp->t_dupacks == 1 ||
2641582a954bSJeffrey Hsu 					    tp->t_dupacks == 2,
2642302ce8d6SAndre Oppermann 					    ("%s: dupacks not 1 or 2",
2643302ce8d6SAndre Oppermann 					    __func__));
264461a36e3dSJeffrey Hsu 					if (tp->t_dupacks == 1)
264548d2549cSJeffrey Hsu 						tp->snd_limited = 0;
264661a36e3dSJeffrey Hsu 					tp->snd_cwnd =
264761a36e3dSJeffrey Hsu 					    (tp->snd_nxt - tp->snd_una) +
264861a36e3dSJeffrey Hsu 					    (tp->t_dupacks - tp->snd_limited) *
26490c39d38dSGleb Smirnoff 					    maxseg;
26505628dd08SAndre Oppermann 					/*
26515628dd08SAndre Oppermann 					 * Only call tcp_output when there
26525628dd08SAndre Oppermann 					 * is new data available to be sent.
26535628dd08SAndre Oppermann 					 * Otherwise we would send pure ACKs.
26545628dd08SAndre Oppermann 					 */
26555628dd08SAndre Oppermann 					SOCKBUF_LOCK(&so->so_snd);
2656cfa6009eSGleb Smirnoff 					avail = sbavail(&so->so_snd) -
26575628dd08SAndre Oppermann 					    (tp->snd_nxt - tp->snd_una);
26585628dd08SAndre Oppermann 					SOCKBUF_UNLOCK(&so->so_snd);
26595628dd08SAndre Oppermann 					if (avail > 0)
266055bceb1eSRandall Stewart 						(void) tp->t_fb->tfb_tcp_output(tp);
266148d2549cSJeffrey Hsu 					sent = tp->snd_max - oldsndmax;
26620c39d38dSGleb Smirnoff 					if (sent > maxseg) {
266389c02376SJeffrey Hsu 						KASSERT((tp->t_dupacks == 2 &&
266489c02376SJeffrey Hsu 						    tp->snd_limited == 0) ||
26650c39d38dSGleb Smirnoff 						   (sent == maxseg + 1 &&
266689c02376SJeffrey Hsu 						    tp->t_flags & TF_SENTFIN),
2667302ce8d6SAndre Oppermann 						    ("%s: sent too much",
2668302ce8d6SAndre Oppermann 						    __func__));
266948d2549cSJeffrey Hsu 						tp->snd_limited = 2;
267048d2549cSJeffrey Hsu 					} else if (sent > 0)
267148d2549cSJeffrey Hsu 						++tp->snd_limited;
2672582a954bSJeffrey Hsu 					tp->snd_cwnd = oldcwnd;
2673582a954bSJeffrey Hsu 					goto drop;
2674df8bae1dSRodney W. Grimes 				}
2675021eaf79SHiren Panchasara 			}
2676df8bae1dSRodney W. Grimes 			break;
2677021eaf79SHiren Panchasara 		} else {
2678021eaf79SHiren Panchasara 			/*
2679021eaf79SHiren Panchasara 			 * This ack is advancing the left edge, reset the
2680021eaf79SHiren Panchasara 			 * counter.
2681021eaf79SHiren Panchasara 			 */
2682021eaf79SHiren Panchasara 			tp->t_dupacks = 0;
2683021eaf79SHiren Panchasara 			/*
2684021eaf79SHiren Panchasara 			 * If this ack also has new SACK info, increment the
2685021eaf79SHiren Panchasara 			 * counter as per rfc6675.
2686021eaf79SHiren Panchasara 			 */
2687021eaf79SHiren Panchasara 			if ((tp->t_flags & TF_SACK_PERMIT) && sack_changed)
2688021eaf79SHiren Panchasara 				tp->t_dupacks++;
2689df8bae1dSRodney W. Grimes 		}
2690cb942153SJeffrey Hsu 
2691302ce8d6SAndre Oppermann 		KASSERT(SEQ_GT(th->th_ack, tp->snd_una),
2692302ce8d6SAndre Oppermann 		    ("%s: th_ack <= snd_una", __func__));
2693cb942153SJeffrey Hsu 
2694df8bae1dSRodney W. Grimes 		/*
2695df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
2696df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
2697df8bae1dSRodney W. Grimes 		 */
2698dbc42409SLawrence Stewart 		if (IN_FASTRECOVERY(tp->t_flags)) {
2699cb942153SJeffrey Hsu 			if (SEQ_LT(th->th_ack, tp->snd_recover)) {
27003529149eSAndre Oppermann 				if (tp->t_flags & TF_SACK_PERMIT)
27016d90faf3SPaul Saab 					tcp_sack_partialack(tp, th);
27026d90faf3SPaul Saab 				else
2703c068736aSJeffrey Hsu 					tcp_newreno_partial_ack(tp, th);
2704dbc42409SLawrence Stewart 			} else
2705dbc42409SLawrence Stewart 				cc_post_recovery(tp, th);
270646f58482SJonathan Lemon 		}
2707a0292f23SGarrett Wollman 		/*
2708a0292f23SGarrett Wollman 		 * If we reach this point, ACK is not a duplicate,
2709a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
2710a0292f23SGarrett Wollman 		 */
2711a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
2712a0292f23SGarrett Wollman 			/*
2713a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
2714a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
271507e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
271607e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
271707e43e10SAndras Olah 			 * we can do window scaling.
2718a0292f23SGarrett Wollman 			 */
2719a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
2720a0292f23SGarrett Wollman 			tp->snd_una++;
272107e43e10SAndras Olah 			/* Do window scaling? */
272207e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
272307e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
272407e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2725464fcfbcSAndre Oppermann 				/* Send window already scaled. */
272607e43e10SAndras Olah 			}
2727a0292f23SGarrett Wollman 		}
2728a0292f23SGarrett Wollman 
2729a0292f23SGarrett Wollman process_ACK:
27308501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
27317cfc6904SRobert Watson 
2732dbc42409SLawrence Stewart 		acked = BYTES_THIS_ACK(tp, th);
2733b8c2cd15SJonathan T. Looney 		KASSERT(acked >= 0, ("%s: acked unexepectedly negative "
2734b8c2cd15SJonathan T. Looney 		    "(tp->snd_una=%u, th->th_ack=%u, tp=%p, m=%p)", __func__,
2735b8c2cd15SJonathan T. Looney 		    tp->snd_una, th->th_ack, tp, m));
27364b7b743cSLawrence Stewart 		TCPSTAT_ADD(tcps_rcvackpack, nsegs);
273778b50714SRobert Watson 		TCPSTAT_ADD(tcps_rcvackbyte, acked);
2738df8bae1dSRodney W. Grimes 
2739df8bae1dSRodney W. Grimes 		/*
27409b8b58e0SJonathan Lemon 		 * If we just performed our first retransmit, and the ACK
27419b8b58e0SJonathan Lemon 		 * arrives within our recovery window, then it was a mistake
27429b8b58e0SJonathan Lemon 		 * to do the retransmit in the first place.  Recover our
27439b8b58e0SJonathan Lemon 		 * original cwnd and ssthresh, and proceed to transmit where
27449b8b58e0SJonathan Lemon 		 * we left off.
27459b8b58e0SJonathan Lemon 		 */
274610d20c84SMatt Macy 		if (tp->t_rxtshift == 1 &&
274710d20c84SMatt Macy 		    tp->t_flags & TF_PREVVALID &&
274810d20c84SMatt Macy 		    tp->t_badrxtwin &&
274910d20c84SMatt Macy 		    SEQ_LT(to.to_tsecr, tp->t_badrxtwin))
2750dbc42409SLawrence Stewart 			cc_cong_signal(tp, th, CC_RTO_ERR);
27519b8b58e0SJonathan Lemon 
27529b8b58e0SJonathan Lemon 		/*
2753df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
2754df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
2755df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
2756df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
2757df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
2758df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
2759df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
2760fa55172bSMatthew Dillon 		 *
2761fa55172bSMatthew Dillon 		 * Some boxes send broken timestamp replies
2762fa55172bSMatthew Dillon 		 * during the SYN+ACK phase, ignore
2763fa55172bSMatthew Dillon 		 * timestamps of 0 or we could calculate a
2764fa55172bSMatthew Dillon 		 * huge RTT and blow up the retransmit timer.
2765df8bae1dSRodney W. Grimes 		 */
2766d8951c8aSBjoern A. Zeeb 		if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) {
27673ac12506SJonathan T. Looney 			uint32_t t;
2768d8951c8aSBjoern A. Zeeb 
2769d8951c8aSBjoern A. Zeeb 			t = tcp_ts_getticks() - to.to_tsecr;
2770d8951c8aSBjoern A. Zeeb 			if (!tp->t_rttlow || tp->t_rttlow > t)
2771d8951c8aSBjoern A. Zeeb 				tp->t_rttlow = t;
2772d8951c8aSBjoern A. Zeeb 			tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1);
2773fa55172bSMatthew Dillon 		} else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
2774eaf80179SAndre Oppermann 			if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime)
2775eaf80179SAndre Oppermann 				tp->t_rttlow = ticks - tp->t_rtttime;
27769b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
2777fa55172bSMatthew Dillon 		}
2778df8bae1dSRodney W. Grimes 
2779df8bae1dSRodney W. Grimes 		/*
2780df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
2781df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
2782df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
2783df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
2784df8bae1dSRodney W. Grimes 		 */
2785fb59c426SYoshinobu Inoue 		if (th->th_ack == tp->snd_max) {
2786b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
2787df8bae1dSRodney W. Grimes 			needoutput = 1;
2788b8152ba7SAndre Oppermann 		} else if (!tcp_timer_active(tp, TT_PERSIST))
2789b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
2790a0292f23SGarrett Wollman 
2791a0292f23SGarrett Wollman 		/*
2792a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
2793a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
2794a0292f23SGarrett Wollman 		 */
2795a0292f23SGarrett Wollman 		if (acked == 0)
2796a0292f23SGarrett Wollman 			goto step6;
2797a0292f23SGarrett Wollman 
2798df8bae1dSRodney W. Grimes 		/*
2799dbc42409SLawrence Stewart 		 * Let the congestion control algorithm update congestion
2800dbc42409SLawrence Stewart 		 * control related information. This typically means increasing
2801dbc42409SLawrence Stewart 		 * the congestion window.
2802df8bae1dSRodney W. Grimes 		 */
28034b7b743cSLawrence Stewart 		cc_ack_received(tp, th, nsegs, CC_ACK);
2804dbc42409SLawrence Stewart 
28055905999bSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
2806cfa6009eSGleb Smirnoff 		if (acked > sbavail(&so->so_snd)) {
2807b8c2cd15SJonathan T. Looney 			if (tp->snd_wnd >= sbavail(&so->so_snd))
2808cfa6009eSGleb Smirnoff 				tp->snd_wnd -= sbavail(&so->so_snd);
2809b8c2cd15SJonathan T. Looney 			else
2810b8c2cd15SJonathan T. Looney 				tp->snd_wnd = 0;
2811c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd,
2812cfa6009eSGleb Smirnoff 			    (int)sbavail(&so->so_snd));
2813df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
2814df8bae1dSRodney W. Grimes 		} else {
2815c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd, acked);
28163ac12506SJonathan T. Looney 			if (tp->snd_wnd >= (uint32_t) acked)
281760ee3bb2SAndre Oppermann 				tp->snd_wnd -= acked;
2818b8c2cd15SJonathan T. Looney 			else
2819b8c2cd15SJonathan T. Looney 				tp->snd_wnd = 0;
2820df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
2821df8bae1dSRodney W. Grimes 		}
2822564aab1fSAndre Oppermann 		/* NB: sowwakeup_locked() does an implicit unlock. */
28231e4d7da7SRobert Watson 		sowwakeup_locked(so);
2824c11a15bfSGleb Smirnoff 		m_freem(mfree);
2825e8949f74SAndre Oppermann 		/* Detect una wraparound. */
2826dbc42409SLawrence Stewart 		if (!IN_RECOVERY(tp->t_flags) &&
28279d11646dSJeffrey Hsu 		    SEQ_GT(tp->snd_una, tp->snd_recover) &&
28289d11646dSJeffrey Hsu 		    SEQ_LEQ(th->th_ack, tp->snd_recover))
28299d11646dSJeffrey Hsu 			tp->snd_recover = th->th_ack - 1;
2830dbc42409SLawrence Stewart 		/* XXXLAS: Can this be moved up into cc_post_recovery? */
2831dbc42409SLawrence Stewart 		if (IN_RECOVERY(tp->t_flags) &&
283224cb0f22SLawrence Stewart 		    SEQ_GEQ(th->th_ack, tp->snd_recover)) {
2833dbc42409SLawrence Stewart 			EXIT_RECOVERY(tp->t_flags);
283424cb0f22SLawrence Stewart 		}
2835fb59c426SYoshinobu Inoue 		tp->snd_una = th->th_ack;
28363529149eSAndre Oppermann 		if (tp->t_flags & TF_SACK_PERMIT) {
28376d90faf3SPaul Saab 			if (SEQ_GT(tp->snd_una, tp->snd_recover))
28386d90faf3SPaul Saab 				tp->snd_recover = tp->snd_una;
28396d90faf3SPaul Saab 		}
2840df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2841df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
2842df8bae1dSRodney W. Grimes 
2843df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2844df8bae1dSRodney W. Grimes 
2845df8bae1dSRodney W. Grimes 		/*
2846df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
2847df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
2848df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
2849df8bae1dSRodney W. Grimes 		 */
2850df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2851df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2852df8bae1dSRodney W. Grimes 				/*
2853df8bae1dSRodney W. Grimes 				 * If we can't receive any more
2854df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
2855df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
2856df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
2857df8bae1dSRodney W. Grimes 				 * we'll hang forever.
2858e8949f74SAndre Oppermann 				 *
2859e8949f74SAndre Oppermann 				 * XXXjl:
2860340c35deSJonathan Lemon 				 * we should release the tp also, and use a
2861340c35deSJonathan Lemon 				 * compressed state.
2862340c35deSJonathan Lemon 				 */
2863c0b99ffaSRobert Watson 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
286403e49181SSeigo Tanimura 					soisdisconnected(so);
28659077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
28669077f387SGleb Smirnoff 					    (tcp_fast_finwait2_recycle ?
28679077f387SGleb Smirnoff 					    tcp_finwait2_timeout :
28689077f387SGleb Smirnoff 					    TP_MAXIDLE(tp)));
28694cc20ab1SSeigo Tanimura 				}
287057f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_2);
2871df8bae1dSRodney W. Grimes 			}
2872df8bae1dSRodney W. Grimes 			break;
2873df8bae1dSRodney W. Grimes 
2874df8bae1dSRodney W. Grimes 		/*
2875df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
2876df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
2877df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
2878df8bae1dSRodney W. Grimes 		 * the segment.
2879df8bae1dSRodney W. Grimes 		 */
2880df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
2881df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2882ff9b006dSJulien Charbon 				INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
288311a20fb8SJeffrey Hsu 				tcp_twstart(tp);
2884340c35deSJonathan Lemon 				m_freem(m);
2885679d9708SAndre Oppermann 				return;
2886df8bae1dSRodney W. Grimes 			}
2887df8bae1dSRodney W. Grimes 			break;
2888df8bae1dSRodney W. Grimes 
2889df8bae1dSRodney W. Grimes 		/*
2890df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
2891df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
2892df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
2893df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
2894df8bae1dSRodney W. Grimes 		 */
2895df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
2896df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2897ff9b006dSJulien Charbon 				INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2898df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
2899df8bae1dSRodney W. Grimes 				goto drop;
2900df8bae1dSRodney W. Grimes 			}
2901df8bae1dSRodney W. Grimes 			break;
2902df8bae1dSRodney W. Grimes 		}
2903df8bae1dSRodney W. Grimes 	}
2904df8bae1dSRodney W. Grimes 
2905df8bae1dSRodney W. Grimes step6:
29068501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
29077cfc6904SRobert Watson 
2908df8bae1dSRodney W. Grimes 	/*
290960ee3bb2SAndre Oppermann 	 * Update window information.
291060ee3bb2SAndre Oppermann 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2911df8bae1dSRodney W. Grimes 	 */
291260ee3bb2SAndre Oppermann 	if ((thflags & TH_ACK) &&
291360ee3bb2SAndre Oppermann 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
291460ee3bb2SAndre Oppermann 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
291560ee3bb2SAndre Oppermann 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
291660ee3bb2SAndre Oppermann 		/* keep track of pure window updates */
291760ee3bb2SAndre Oppermann 		if (tlen == 0 &&
291860ee3bb2SAndre Oppermann 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
291978b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvwinupd);
2920df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
292160ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq;
292260ee3bb2SAndre Oppermann 		tp->snd_wl2 = th->th_ack;
2923df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
2924df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
292560ee3bb2SAndre Oppermann 		needoutput = 1;
2926df8bae1dSRodney W. Grimes 	}
2927df8bae1dSRodney W. Grimes 
2928df8bae1dSRodney W. Grimes 	/*
2929df8bae1dSRodney W. Grimes 	 * Process segments with URG.
2930df8bae1dSRodney W. Grimes 	 */
2931fb59c426SYoshinobu Inoue 	if ((thflags & TH_URG) && th->th_urp &&
2932df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2933df8bae1dSRodney W. Grimes 		/*
2934df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
2935df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
2936df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
2937df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
2938df8bae1dSRodney W. Grimes 		 */
293918ad5842SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
2940cfa6009eSGleb Smirnoff 		if (th->th_urp + sbavail(&so->so_rcv) > sb_max) {
2941fb59c426SYoshinobu Inoue 			th->th_urp = 0;			/* XXX */
2942fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;		/* XXX */
294318ad5842SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);	/* XXX */
2944df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
2945df8bae1dSRodney W. Grimes 		}
2946df8bae1dSRodney W. Grimes 		/*
2947df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
2948df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
2949df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2950df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
2951df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
2952df8bae1dSRodney W. Grimes 		 *
2953df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
2954df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
2955df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
2956df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
2957df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
2958df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
2959df8bae1dSRodney W. Grimes 		 */
2960fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
2961fb59c426SYoshinobu Inoue 			tp->rcv_up = th->th_seq + th->th_urp;
2962cfa6009eSGleb Smirnoff 			so->so_oobmark = sbavail(&so->so_rcv) +
2963df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
2964927c5ceaSRobert Watson 			if (so->so_oobmark == 0)
2965c0b99ffaSRobert Watson 				so->so_rcv.sb_state |= SBS_RCVATMARK;
2966df8bae1dSRodney W. Grimes 			sohasoutofband(so);
2967df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2968df8bae1dSRodney W. Grimes 		}
296918ad5842SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
2970df8bae1dSRodney W. Grimes 		/*
2971df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
2972df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
2973df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
2974df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
2975df8bae1dSRodney W. Grimes 		 */
29763ac12506SJonathan T. Looney 		if (th->th_urp <= (uint32_t)tlen &&
297730613f56SJeffrey Hsu 		    !(so->so_options & SO_OOBINLINE)) {
297830613f56SJeffrey Hsu 			/* hdr drop is delayed */
297930613f56SJeffrey Hsu 			tcp_pulloutofband(so, th, m, drop_hdrlen);
298030613f56SJeffrey Hsu 		}
2981c068736aSJeffrey Hsu 	} else {
2982df8bae1dSRodney W. Grimes 		/*
2983df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
2984df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
2985df8bae1dSRodney W. Grimes 		 * with the receive window.
2986df8bae1dSRodney W. Grimes 		 */
2987df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
2988df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
2989c068736aSJeffrey Hsu 	}
2990df8bae1dSRodney W. Grimes dodata:							/* XXX */
29918501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
29927cfc6904SRobert Watson 
2993df8bae1dSRodney W. Grimes 	/*
2994df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
2995df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
2996df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
2997df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
2998df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
2999df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
3000df8bae1dSRodney W. Grimes 	 */
3001281a0fd4SPatrick Kelsey 	tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
300268bd7ed1SJonathan T. Looney 		   IS_FASTOPEN(tp->t_flags));
3003281a0fd4SPatrick Kelsey 	if ((tlen || (thflags & TH_FIN) || tfo_syn) &&
3004df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
300569e03620SPaul Saab 		tcp_seq save_start = th->th_seq;
3006fb59c426SYoshinobu Inoue 		m_adj(m, drop_hdrlen);	/* delayed header drop */
3007e4b64281SJesper Skriver 		/*
3008c068736aSJeffrey Hsu 		 * Insert segment which includes th into TCP reassembly queue
3009c068736aSJeffrey Hsu 		 * with control block tp.  Set thflags to whether reassembly now
3010c068736aSJeffrey Hsu 		 * includes a segment with FIN.  This handles the common case
3011c068736aSJeffrey Hsu 		 * inline (segment is the next to be received on an established
3012c068736aSJeffrey Hsu 		 * connection, and the queue is empty), avoiding linkage into
3013c068736aSJeffrey Hsu 		 * and removal from the queue and repetition of various
3014c068736aSJeffrey Hsu 		 * conversions.
3015c068736aSJeffrey Hsu 		 * Set DELACK for segments received in order, but ack
3016c068736aSJeffrey Hsu 		 * immediately when segments are out of order (so
3017c068736aSJeffrey Hsu 		 * fast retransmit can work).
3018e4b64281SJesper Skriver 		 */
30194741bfcbSPatrick Kelsey 		if (th->th_seq == tp->rcv_nxt &&
30204741bfcbSPatrick Kelsey 		    LIST_EMPTY(&tp->t_segq) &&
3021281a0fd4SPatrick Kelsey 		    (TCPS_HAVEESTABLISHED(tp->t_state) ||
3022281a0fd4SPatrick Kelsey 		     tfo_syn)) {
3023281a0fd4SPatrick Kelsey 			if (DELAY_ACK(tp, tlen) || tfo_syn)
30243bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
3025e4b64281SJesper Skriver 			else
3026e4b64281SJesper Skriver 				tp->t_flags |= TF_ACKNOW;
3027e4b64281SJesper Skriver 			tp->rcv_nxt += tlen;
3028e4b64281SJesper Skriver 			thflags = th->th_flags & TH_FIN;
302978b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpack);
303078b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
30311e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
3032c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
3033c1c36a2cSMike Silbersack 				m_freem(m);
3034c1c36a2cSMike Silbersack 			else
3035651e4e6aSGleb Smirnoff 				sbappendstream_locked(&so->so_rcv, m, 0);
3036e8949f74SAndre Oppermann 			/* NB: sorwakeup_locked() does an implicit unlock. */
30371e4d7da7SRobert Watson 			sorwakeup_locked(so);
3038e4b64281SJesper Skriver 		} else {
3039e8949f74SAndre Oppermann 			/*
3040e8949f74SAndre Oppermann 			 * XXX: Due to the header drop above "th" is
3041e8949f74SAndre Oppermann 			 * theoretically invalid by now.  Fortunately
3042e8949f74SAndre Oppermann 			 * m_adj() doesn't actually frees any mbufs
3043e8949f74SAndre Oppermann 			 * when trimming from the head.
3044e8949f74SAndre Oppermann 			 */
3045e4b64281SJesper Skriver 			thflags = tcp_reass(tp, th, &tlen, m);
3046e4b64281SJesper Skriver 			tp->t_flags |= TF_ACKNOW;
3047e4b64281SJesper Skriver 		}
30483529149eSAndre Oppermann 		if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT))
3049f194524fSAndre Oppermann 			tcp_update_sack_list(tp, save_start, save_start + tlen);
3050302ce8d6SAndre Oppermann #if 0
3051df8bae1dSRodney W. Grimes 		/*
3052df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
3053df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
3054df8bae1dSRodney W. Grimes 		 * buffer size.
3055302ce8d6SAndre Oppermann 		 * XXX: Unused.
3056df8bae1dSRodney W. Grimes 		 */
3057f701e30dSJohn Baldwin 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt))
3058df8bae1dSRodney W. Grimes 			len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
3059f701e30dSJohn Baldwin 		else
3060f701e30dSJohn Baldwin 			len = so->so_rcv.sb_hiwat;
3061302ce8d6SAndre Oppermann #endif
3062df8bae1dSRodney W. Grimes 	} else {
3063df8bae1dSRodney W. Grimes 		m_freem(m);
3064fb59c426SYoshinobu Inoue 		thflags &= ~TH_FIN;
3065df8bae1dSRodney W. Grimes 	}
3066df8bae1dSRodney W. Grimes 
3067df8bae1dSRodney W. Grimes 	/*
3068df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
3069df8bae1dSRodney W. Grimes 	 * that the connection is closing.
3070df8bae1dSRodney W. Grimes 	 */
3071fb59c426SYoshinobu Inoue 	if (thflags & TH_FIN) {
3072df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
3073df8bae1dSRodney W. Grimes 			socantrcvmore(so);
3074a0292f23SGarrett Wollman 			/*
3075a0292f23SGarrett Wollman 			 * If connection is half-synchronized
3076d3eede9dSAndras Olah 			 * (ie NEEDSYN flag on) then delay ACK,
3077a0292f23SGarrett Wollman 			 * so it may be piggybacked when SYN is sent.
3078a0292f23SGarrett Wollman 			 * Otherwise, since we received a FIN then no
3079a0292f23SGarrett Wollman 			 * more input can be expected, send ACK now.
3080a0292f23SGarrett Wollman 			 */
30813bfd6421SJonathan Lemon 			if (tp->t_flags & TF_NEEDSYN)
30823bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
3083a0292f23SGarrett Wollman 			else
3084df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
3085df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
3086df8bae1dSRodney W. Grimes 		}
3087df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
3088df8bae1dSRodney W. Grimes 
3089df8bae1dSRodney W. Grimes 		/*
3090df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
3091df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
3092df8bae1dSRodney W. Grimes 		 */
3093df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
30949b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
30959b8b58e0SJonathan Lemon 			/* FALLTHROUGH */
3096df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
309757f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSE_WAIT);
3098df8bae1dSRodney W. Grimes 			break;
3099df8bae1dSRodney W. Grimes 
3100df8bae1dSRodney W. Grimes 		/*
3101df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
3102df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
3103df8bae1dSRodney W. Grimes 		 */
3104df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
310557f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSING);
3106df8bae1dSRodney W. Grimes 			break;
3107df8bae1dSRodney W. Grimes 
3108df8bae1dSRodney W. Grimes 		/*
3109df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
3110df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
3111df8bae1dSRodney W. Grimes 		 * standard timers.
3112df8bae1dSRodney W. Grimes 		 */
3113df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
3114ff9b006dSJulien Charbon 			INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
3115252ca428SRobert Watson 
3116340c35deSJonathan Lemon 			tcp_twstart(tp);
3117679d9708SAndre Oppermann 			return;
3118df8bae1dSRodney W. Grimes 		}
3119df8bae1dSRodney W. Grimes 	}
3120610ee2f9SDavid Greenman #ifdef TCPDEBUG
31214cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
3122fb59c426SYoshinobu Inoue 		tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
3123fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3124610ee2f9SDavid Greenman #endif
31252b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
3126df8bae1dSRodney W. Grimes 
3127df8bae1dSRodney W. Grimes 	/*
3128df8bae1dSRodney W. Grimes 	 * Return any desired output.
3129df8bae1dSRodney W. Grimes 	 */
3130df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
313155bceb1eSRandall Stewart 		(void) tp->t_fb->tfb_tcp_output(tp);
31327792ea27SJeffrey Hsu 
3133a14c749fSJonathan Lemon check_delack:
31348501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
3135252ca428SRobert Watson 
3136a14c749fSJonathan Lemon 	if (tp->t_flags & TF_DELACK) {
31373bfd6421SJonathan Lemon 		tp->t_flags &= ~TF_DELACK;
3138b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
31393bfd6421SJonathan Lemon 	}
31408501a69cSRobert Watson 	INP_WUNLOCK(tp->t_inpcb);
3141679d9708SAndre Oppermann 	return;
3142df8bae1dSRodney W. Grimes 
3143df8bae1dSRodney W. Grimes dropafterack:
3144df8bae1dSRodney W. Grimes 	/*
3145df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
3146df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
314780ab7c0eSGarrett Wollman 	 *
314880ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
314980ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
315080ab7c0eSGarrett Wollman 	 * RST have been dropped.
315180ab7c0eSGarrett Wollman 	 *
315280ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
315380ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
315480ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
315580ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
315680ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
315780ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
3158df8bae1dSRodney W. Grimes 	 */
3159fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
3160fb59c426SYoshinobu Inoue 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
3161a57815efSBosko Milekic 	     SEQ_GT(th->th_ack, tp->snd_max)) ) {
3162a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
3163a57815efSBosko Milekic 		goto dropwithreset;
3164a57815efSBosko Milekic 	}
3165a0292f23SGarrett Wollman #ifdef TCPDEBUG
31664cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
3167fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
3168fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3169a0292f23SGarrett Wollman #endif
31702b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
3171df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
317255bceb1eSRandall Stewart 	(void) tp->t_fb->tfb_tcp_output(tp);
31738501a69cSRobert Watson 	INP_WUNLOCK(tp->t_inpcb);
3174d6915262SRobert Watson 	m_freem(m);
3175679d9708SAndre Oppermann 	return;
3176df8bae1dSRodney W. Grimes 
3177df8bae1dSRodney W. Grimes dropwithreset:
3178a0ca0871SRobert Watson 	if (tp != NULL) {
3179302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
31808501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
3181dd8ac7f9SRobert Watson 	} else
3182a0ca0871SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
3183679d9708SAndre Oppermann 	return;
3184df8bae1dSRodney W. Grimes 
3185df8bae1dSRodney W. Grimes drop:
3186df8bae1dSRodney W. Grimes 	/*
3187df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
3188df8bae1dSRodney W. Grimes 	 */
3189610ee2f9SDavid Greenman #ifdef TCPDEBUG
31901c53f806SRobert Watson 	if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
3191fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
3192fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3193a0292f23SGarrett Wollman #endif
31942b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
31951c53f806SRobert Watson 	if (tp != NULL)
31968501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
3197d6915262SRobert Watson 	m_freem(m);
3198302ce8d6SAndre Oppermann }
3199302ce8d6SAndre Oppermann 
3200302ce8d6SAndre Oppermann /*
32010ca3f933SAndre Oppermann  * Issue RST and make ACK acceptable to originator of segment.
32020ca3f933SAndre Oppermann  * The mbuf must still include the original packet header.
32030ca3f933SAndre Oppermann  * tp may be NULL.
3204302ce8d6SAndre Oppermann  */
320555bceb1eSRandall Stewart void
3206302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp,
3207302ce8d6SAndre Oppermann     int tlen, int rstreason)
3208302ce8d6SAndre Oppermann {
3209b287c6c7SBjoern A. Zeeb #ifdef INET
3210302ce8d6SAndre Oppermann 	struct ip *ip;
3211b287c6c7SBjoern A. Zeeb #endif
3212302ce8d6SAndre Oppermann #ifdef INET6
3213302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6;
3214302ce8d6SAndre Oppermann #endif
32157a3244ccSRobert Watson 
32167a3244ccSRobert Watson 	if (tp != NULL) {
32178501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
32187a3244ccSRobert Watson 	}
32197a3244ccSRobert Watson 
32200ca3f933SAndre Oppermann 	/* Don't bother if destination was broadcast/multicast. */
3221302ce8d6SAndre Oppermann 	if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
3222302ce8d6SAndre Oppermann 		goto drop;
3223302ce8d6SAndre Oppermann #ifdef INET6
3224302ce8d6SAndre Oppermann 	if (mtod(m, struct ip *)->ip_v == 6) {
3225302ce8d6SAndre Oppermann 		ip6 = mtod(m, struct ip6_hdr *);
3226302ce8d6SAndre Oppermann 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
3227302ce8d6SAndre Oppermann 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
3228302ce8d6SAndre Oppermann 			goto drop;
3229302ce8d6SAndre Oppermann 		/* IPv6 anycast check is done at tcp6_input() */
3230b287c6c7SBjoern A. Zeeb 	}
3231302ce8d6SAndre Oppermann #endif
3232b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3233b287c6c7SBjoern A. Zeeb 	else
3234b287c6c7SBjoern A. Zeeb #endif
3235b287c6c7SBjoern A. Zeeb #ifdef INET
3236302ce8d6SAndre Oppermann 	{
3237302ce8d6SAndre Oppermann 		ip = mtod(m, struct ip *);
3238302ce8d6SAndre Oppermann 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
3239302ce8d6SAndre Oppermann 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
3240302ce8d6SAndre Oppermann 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
3241302ce8d6SAndre Oppermann 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
3242302ce8d6SAndre Oppermann 			goto drop;
3243302ce8d6SAndre Oppermann 	}
3244b287c6c7SBjoern A. Zeeb #endif
3245302ce8d6SAndre Oppermann 
3246302ce8d6SAndre Oppermann 	/* Perform bandwidth limiting. */
3247302ce8d6SAndre Oppermann 	if (badport_bandlim(rstreason) < 0)
3248302ce8d6SAndre Oppermann 		goto drop;
3249302ce8d6SAndre Oppermann 
3250302ce8d6SAndre Oppermann 	/* tcp_respond consumes the mbuf chain. */
3251302ce8d6SAndre Oppermann 	if (th->th_flags & TH_ACK) {
3252302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0,
3253302ce8d6SAndre Oppermann 		    th->th_ack, TH_RST);
3254302ce8d6SAndre Oppermann 	} else {
3255302ce8d6SAndre Oppermann 		if (th->th_flags & TH_SYN)
3256302ce8d6SAndre Oppermann 			tlen++;
32574ddd5aadSMichael Tuexen 		if (th->th_flags & TH_FIN)
32584ddd5aadSMichael Tuexen 			tlen++;
3259302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
3260302ce8d6SAndre Oppermann 		    (tcp_seq)0, TH_RST|TH_ACK);
3261302ce8d6SAndre Oppermann 	}
3262302ce8d6SAndre Oppermann 	return;
3263302ce8d6SAndre Oppermann drop:
3264302ce8d6SAndre Oppermann 	m_freem(m);
3265df8bae1dSRodney W. Grimes }
3266df8bae1dSRodney W. Grimes 
3267be2ac88cSJonathan Lemon /*
3268be2ac88cSJonathan Lemon  * Parse TCP options and place in tcpopt.
3269be2ac88cSJonathan Lemon  */
327055bceb1eSRandall Stewart void
3271ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags)
3272df8bae1dSRodney W. Grimes {
3273df8bae1dSRodney W. Grimes 	int opt, optlen;
3274df8bae1dSRodney W. Grimes 
3275be2ac88cSJonathan Lemon 	to->to_flags = 0;
3276df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
3277df8bae1dSRodney W. Grimes 		opt = cp[0];
3278df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
3279df8bae1dSRodney W. Grimes 			break;
3280df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
3281df8bae1dSRodney W. Grimes 			optlen = 1;
3282df8bae1dSRodney W. Grimes 		else {
3283b474779fSJun-ichiro itojun Hagino 			if (cnt < 2)
3284b474779fSJun-ichiro itojun Hagino 				break;
3285df8bae1dSRodney W. Grimes 			optlen = cp[1];
3286b474779fSJun-ichiro itojun Hagino 			if (optlen < 2 || optlen > cnt)
3287df8bae1dSRodney W. Grimes 				break;
3288df8bae1dSRodney W. Grimes 		}
3289df8bae1dSRodney W. Grimes 		switch (opt) {
3290df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
3291df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
3292df8bae1dSRodney W. Grimes 				continue;
3293f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3294df8bae1dSRodney W. Grimes 				continue;
3295be2ac88cSJonathan Lemon 			to->to_flags |= TOF_MSS;
3296be2ac88cSJonathan Lemon 			bcopy((char *)cp + 2,
3297be2ac88cSJonathan Lemon 			    (char *)&to->to_mss, sizeof(to->to_mss));
3298fd8e4ebcSMike Barcroft 			to->to_mss = ntohs(to->to_mss);
3299df8bae1dSRodney W. Grimes 			break;
3300df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
3301df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
3302df8bae1dSRodney W. Grimes 				continue;
3303f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3304df8bae1dSRodney W. Grimes 				continue;
3305be2ac88cSJonathan Lemon 			to->to_flags |= TOF_SCALE;
330602a1a643SAndre Oppermann 			to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT);
3307df8bae1dSRodney W. Grimes 			break;
3308df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
3309df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
3310df8bae1dSRodney W. Grimes 				continue;
3311be2ac88cSJonathan Lemon 			to->to_flags |= TOF_TS;
3312a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
3313a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
3314fd8e4ebcSMike Barcroft 			to->to_tsval = ntohl(to->to_tsval);
3315a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
3316a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
3317fd8e4ebcSMike Barcroft 			to->to_tsecr = ntohl(to->to_tsecr);
3318df8bae1dSRodney W. Grimes 			break;
33191cfd4b53SBruce M Simpson 		case TCPOPT_SIGNATURE:
3320fcf59617SAndrey V. Elsukov 			/*
3321fcf59617SAndrey V. Elsukov 			 * In order to reply to a host which has set the
3322fcf59617SAndrey V. Elsukov 			 * TCP_SIGNATURE option in its initial SYN, we have
3323fcf59617SAndrey V. Elsukov 			 * to record the fact that the option was observed
3324fcf59617SAndrey V. Elsukov 			 * here for the syncache code to perform the correct
3325fcf59617SAndrey V. Elsukov 			 * response.
3326fcf59617SAndrey V. Elsukov 			 */
33271cfd4b53SBruce M Simpson 			if (optlen != TCPOLEN_SIGNATURE)
33281cfd4b53SBruce M Simpson 				continue;
3329df47e437SAndre Oppermann 			to->to_flags |= TOF_SIGNATURE;
3330df47e437SAndre Oppermann 			to->to_signature = cp + 2;
33311cfd4b53SBruce M Simpson 			break;
33326d90faf3SPaul Saab 		case TCPOPT_SACK_PERMITTED:
3333f72167f4SAndre Oppermann 			if (optlen != TCPOLEN_SACK_PERMITTED)
33346d90faf3SPaul Saab 				continue;
3335f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3336f72167f4SAndre Oppermann 				continue;
3337603724d3SBjoern A. Zeeb 			if (!V_tcp_do_sack)
3338f72167f4SAndre Oppermann 				continue;
3339fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACKPERM;
33406d90faf3SPaul Saab 			break;
33416d90faf3SPaul Saab 		case TCPOPT_SACK:
33425a53ca16SPaul Saab 			if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
33436d90faf3SPaul Saab 				continue;
3344b728e902SAndre Oppermann 			if (flags & TO_SYN)
3345b728e902SAndre Oppermann 				continue;
3346fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACK;
33475a53ca16SPaul Saab 			to->to_nsacks = (optlen - 2) / TCPOLEN_SACK;
33485a53ca16SPaul Saab 			to->to_sacks = cp + 2;
334978b50714SRobert Watson 			TCPSTAT_INC(tcps_sack_rcv_blocks);
33506d90faf3SPaul Saab 			break;
3351281a0fd4SPatrick Kelsey 		case TCPOPT_FAST_OPEN:
3352c560df6fSPatrick Kelsey 			/*
3353c560df6fSPatrick Kelsey 			 * Cookie length validation is performed by the
3354c560df6fSPatrick Kelsey 			 * server side cookie checking code or the client
3355c560df6fSPatrick Kelsey 			 * side cookie cache update code.
3356c560df6fSPatrick Kelsey 			 */
3357281a0fd4SPatrick Kelsey 			if (!(flags & TO_SYN))
3358281a0fd4SPatrick Kelsey 				continue;
3359c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
3360c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
3361281a0fd4SPatrick Kelsey 				continue;
3362281a0fd4SPatrick Kelsey 			to->to_flags |= TOF_FASTOPEN;
3363281a0fd4SPatrick Kelsey 			to->to_tfo_len = optlen - 2;
3364281a0fd4SPatrick Kelsey 			to->to_tfo_cookie = to->to_tfo_len ? cp + 2 : NULL;
3365281a0fd4SPatrick Kelsey 			break;
3366be2ac88cSJonathan Lemon 		default:
3367be2ac88cSJonathan Lemon 			continue;
3368df8bae1dSRodney W. Grimes 		}
3369df8bae1dSRodney W. Grimes 	}
3370df8bae1dSRodney W. Grimes }
3371df8bae1dSRodney W. Grimes 
3372df8bae1dSRodney W. Grimes /*
3373df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
3374df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
3375df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
3376df8bae1dSRodney W. Grimes  * sequencing purposes.
3377df8bae1dSRodney W. Grimes  */
337855bceb1eSRandall Stewart void
3379ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m,
3380ad3f9ab3SAndre Oppermann     int off)
3381df8bae1dSRodney W. Grimes {
3382fb59c426SYoshinobu Inoue 	int cnt = off + th->th_urp - 1;
3383df8bae1dSRodney W. Grimes 
3384df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
3385df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
3386df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
3387df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
3388df8bae1dSRodney W. Grimes 
33898501a69cSRobert Watson 			INP_WLOCK_ASSERT(tp->t_inpcb);
33907a3244ccSRobert Watson 
3391df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
3392df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
3393df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
3394df8bae1dSRodney W. Grimes 			m->m_len--;
339569a34685SYoshinobu Inoue 			if (m->m_flags & M_PKTHDR)
339669a34685SYoshinobu Inoue 				m->m_pkthdr.len--;
3397df8bae1dSRodney W. Grimes 			return;
3398df8bae1dSRodney W. Grimes 		}
3399df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
3400df8bae1dSRodney W. Grimes 		m = m->m_next;
34014dfdffe9SAndre Oppermann 		if (m == NULL)
3402df8bae1dSRodney W. Grimes 			break;
3403df8bae1dSRodney W. Grimes 	}
3404df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
3405df8bae1dSRodney W. Grimes }
3406df8bae1dSRodney W. Grimes 
3407df8bae1dSRodney W. Grimes /*
3408df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
3409df8bae1dSRodney W. Grimes  * and update averages and current timeout.
3410df8bae1dSRodney W. Grimes  */
341155bceb1eSRandall Stewart void
3412ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt)
3413df8bae1dSRodney W. Grimes {
3414ad3f9ab3SAndre Oppermann 	int delta;
3415233e8c18SGarrett Wollman 
34168501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
34172be3bf22SRobert Watson 
341878b50714SRobert Watson 	TCPSTAT_INC(tcps_rttupdated);
3419233e8c18SGarrett Wollman 	tp->t_rttupdated++;
34205ede40dcSRyan Stone 	if ((tp->t_srtt != 0) && (tp->t_rxtshift <= TCP_RTT_INVALIDATE)) {
3421233e8c18SGarrett Wollman 		/*
3422233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
3423233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
3424233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
3425233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
3426233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
3427233e8c18SGarrett Wollman 		 */
3428233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
3429233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
3430233e8c18SGarrett Wollman 
3431233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
3432233e8c18SGarrett Wollman 			tp->t_srtt = 1;
3433233e8c18SGarrett Wollman 
3434233e8c18SGarrett Wollman 		/*
3435233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
3436233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
3437233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
3438233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
3439233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
3440233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
3441233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
3442233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
3443233e8c18SGarrett Wollman 		 */
3444233e8c18SGarrett Wollman 		if (delta < 0)
3445233e8c18SGarrett Wollman 			delta = -delta;
3446233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
3447233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
3448233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
34491fcc99b5SMatthew Dillon 		if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
34501fcc99b5SMatthew Dillon 		    tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3451233e8c18SGarrett Wollman 	} else {
3452233e8c18SGarrett Wollman 		/*
3453233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
3454233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
3455233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
3456233e8c18SGarrett Wollman 		 */
3457233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
3458233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
34591fcc99b5SMatthew Dillon 		tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3460233e8c18SGarrett Wollman 	}
34619b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
3462df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
3463df8bae1dSRodney W. Grimes 
3464df8bae1dSRodney W. Grimes 	/*
3465df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
3466df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
3467df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
3468df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
3469df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
3470df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
3471df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
3472df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
3473df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
3474df8bae1dSRodney W. Grimes 	 */
3475233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
34769e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
3477df8bae1dSRodney W. Grimes 
3478df8bae1dSRodney W. Grimes 	/*
3479df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
3480df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
3481df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
3482df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
3483df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
3484df8bae1dSRodney W. Grimes 	 */
3485df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
3486df8bae1dSRodney W. Grimes }
3487df8bae1dSRodney W. Grimes 
3488df8bae1dSRodney W. Grimes /*
3489df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
3490df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
34914faaea55SAndre Oppermann  * If none, use an mss that can be handled on the outgoing interface
34924faaea55SAndre Oppermann  * without forcing IP to fragment.  If no route is found, route has no mtu,
3493df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
3494df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
3495df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
3496df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
3497df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
3498df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
3499df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
3500a0292f23SGarrett Wollman  *
35010c39d38dSGleb Smirnoff  * NOTE that resulting t_maxseg doesn't include space for TCP options or
35020c39d38dSGleb Smirnoff  * IP options, e.g. IPSEC data, since length of this data may vary, and
35030c39d38dSGleb Smirnoff  * thus it is calculated for every segment separately in tcp_output().
3504a0292f23SGarrett Wollman  *
350597d8d152SAndre Oppermann  * NOTE that this routine is only called when we process an incoming
3506ef341ee1SGleb Smirnoff  * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS
3507ef341ee1SGleb Smirnoff  * settings are handled in tcp_mssopt().
3508df8bae1dSRodney W. Grimes  */
3509a0292f23SGarrett Wollman void
3510ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer,
35113c914c54SAndre Oppermann     struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap)
3512df8bae1dSRodney W. Grimes {
3513b287c6c7SBjoern A. Zeeb 	int mss = 0;
35143ac12506SJonathan T. Looney 	uint32_t maxmtu = 0;
3515c068736aSJeffrey Hsu 	struct inpcb *inp = tp->t_inpcb;
351697d8d152SAndre Oppermann 	struct hc_metrics_lite metrics;
3517fb59c426SYoshinobu Inoue #ifdef INET6
3518c068736aSJeffrey Hsu 	int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
3519c068736aSJeffrey Hsu 	size_t min_protoh = isipv6 ?
3520c068736aSJeffrey Hsu 			    sizeof (struct ip6_hdr) + sizeof (struct tcphdr) :
3521c068736aSJeffrey Hsu 			    sizeof (struct tcpiphdr);
3522c068736aSJeffrey Hsu #else
3523c068736aSJeffrey Hsu 	const size_t min_protoh = sizeof(struct tcpiphdr);
3524fb59c426SYoshinobu Inoue #endif
3525df8bae1dSRodney W. Grimes 
35268501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
35277a3244ccSRobert Watson 
3528ef341ee1SGleb Smirnoff 	if (mtuoffer != -1) {
3529ef341ee1SGleb Smirnoff 		KASSERT(offer == -1, ("%s: conflict", __func__));
3530ef341ee1SGleb Smirnoff 		offer = mtuoffer - min_protoh;
3531ef341ee1SGleb Smirnoff 	}
3532ef341ee1SGleb Smirnoff 
3533e8949f74SAndre Oppermann 	/* Initialize. */
353497d8d152SAndre Oppermann #ifdef INET6
353597d8d152SAndre Oppermann 	if (isipv6) {
35363c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu6(&inp->inp_inc, cap);
35370c39d38dSGleb Smirnoff 		tp->t_maxseg = V_tcp_v6mssdflt;
3538b287c6c7SBjoern A. Zeeb 	}
353997d8d152SAndre Oppermann #endif
3540b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3541b287c6c7SBjoern A. Zeeb 	else
3542b287c6c7SBjoern A. Zeeb #endif
3543b287c6c7SBjoern A. Zeeb #ifdef INET
354497d8d152SAndre Oppermann 	{
35453c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu(&inp->inp_inc, cap);
35460c39d38dSGleb Smirnoff 		tp->t_maxseg = V_tcp_mssdflt;
3547df8bae1dSRodney W. Grimes 	}
3548b287c6c7SBjoern A. Zeeb #endif
3549df8bae1dSRodney W. Grimes 
355097d8d152SAndre Oppermann 	/*
3551e8949f74SAndre Oppermann 	 * No route to sender, stay with default mss and return.
355297d8d152SAndre Oppermann 	 */
35536f01cac6SBjoern A. Zeeb 	if (maxmtu == 0) {
35546f01cac6SBjoern A. Zeeb 		/*
35558e5c87f4SBjoern A. Zeeb 		 * In case we return early we need to initialize metrics
35566f01cac6SBjoern A. Zeeb 		 * to a defined state as tcp_hc_get() would do for us
35576f01cac6SBjoern A. Zeeb 		 * if there was no cache hit.
35586f01cac6SBjoern A. Zeeb 		 */
35596f01cac6SBjoern A. Zeeb 		if (metricptr != NULL)
35606f01cac6SBjoern A. Zeeb 			bzero(metricptr, sizeof(struct hc_metrics_lite));
356197d8d152SAndre Oppermann 		return;
35626f01cac6SBjoern A. Zeeb 	}
356397d8d152SAndre Oppermann 
3564e8949f74SAndre Oppermann 	/* What have we got? */
356597d8d152SAndre Oppermann 	switch (offer) {
356697d8d152SAndre Oppermann 		case 0:
356797d8d152SAndre Oppermann 			/*
356897d8d152SAndre Oppermann 			 * Offer == 0 means that there was no MSS on the SYN
3569c3b02504SBjoern A. Zeeb 			 * segment, in this case we use tcp_mssdflt as
35700c39d38dSGleb Smirnoff 			 * already assigned to t_maxseg above.
357197d8d152SAndre Oppermann 			 */
35720c39d38dSGleb Smirnoff 			offer = tp->t_maxseg;
357397d8d152SAndre Oppermann 			break;
357497d8d152SAndre Oppermann 
357597d8d152SAndre Oppermann 		case -1:
3576a0292f23SGarrett Wollman 			/*
3577c94c54e4SAndre Oppermann 			 * Offer == -1 means that we didn't receive SYN yet.
3578a0292f23SGarrett Wollman 			 */
357997d8d152SAndre Oppermann 			/* FALLTHROUGH */
358097d8d152SAndre Oppermann 
358197d8d152SAndre Oppermann 		default:
3582a0292f23SGarrett Wollman 			/*
358353369ac9SAndre Oppermann 			 * Prevent DoS attack with too small MSS. Round up
358453369ac9SAndre Oppermann 			 * to at least minmss.
358553369ac9SAndre Oppermann 			 */
3586603724d3SBjoern A. Zeeb 			offer = max(offer, V_tcp_minmss);
358797d8d152SAndre Oppermann 	}
3588a0292f23SGarrett Wollman 
3589df8bae1dSRodney W. Grimes 	/*
3590e8949f74SAndre Oppermann 	 * rmx information is now retrieved from tcp_hostcache.
3591df8bae1dSRodney W. Grimes 	 */
359297d8d152SAndre Oppermann 	tcp_hc_get(&inp->inp_inc, &metrics);
35933cee92e0SBjoern A. Zeeb 	if (metricptr != NULL)
35943cee92e0SBjoern A. Zeeb 		bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite));
359597d8d152SAndre Oppermann 
3596df8bae1dSRodney W. Grimes 	/*
3597cc412412SHiren Panchasara 	 * If there's a discovered mtu in tcp hostcache, use it.
3598cc412412SHiren Panchasara 	 * Else, use the link mtu.
3599df8bae1dSRodney W. Grimes 	 */
360097d8d152SAndre Oppermann 	if (metrics.rmx_mtu)
36012d166c02SAndre Oppermann 		mss = min(metrics.rmx_mtu, maxmtu) - min_protoh;
3602c068736aSJeffrey Hsu 	else {
360331b3783cSHajimu UMEMOTO #ifdef INET6
3604fb59c426SYoshinobu Inoue 		if (isipv6) {
360597d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3606603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
360797d8d152SAndre Oppermann 			    !in6_localaddr(&inp->in6p_faddr))
3608603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_v6mssdflt);
3609b287c6c7SBjoern A. Zeeb 		}
3610b3399803SHajimu UMEMOTO #endif
3611b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3612b287c6c7SBjoern A. Zeeb 		else
3613b287c6c7SBjoern A. Zeeb #endif
3614b287c6c7SBjoern A. Zeeb #ifdef INET
361597d8d152SAndre Oppermann 		{
361697d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3617603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
361897d8d152SAndre Oppermann 			    !in_localaddr(inp->inp_faddr))
3619603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_mssdflt);
3620a0292f23SGarrett Wollman 		}
3621b287c6c7SBjoern A. Zeeb #endif
36223cee92e0SBjoern A. Zeeb 		/*
36233cee92e0SBjoern A. Zeeb 		 * XXX - The above conditional (mss = maxmtu - min_protoh)
36243cee92e0SBjoern A. Zeeb 		 * probably violates the TCP spec.
36253cee92e0SBjoern A. Zeeb 		 * The problem is that, since we don't know the
36263cee92e0SBjoern A. Zeeb 		 * other end's MSS, we are supposed to use a conservative
36273cee92e0SBjoern A. Zeeb 		 * default.  But, if we do that, then MTU discovery will
36283cee92e0SBjoern A. Zeeb 		 * never actually take place, because the conservative
36293cee92e0SBjoern A. Zeeb 		 * default is much less than the MTUs typically seen
36303cee92e0SBjoern A. Zeeb 		 * on the Internet today.  For the moment, we'll sweep
36313cee92e0SBjoern A. Zeeb 		 * this under the carpet.
36323cee92e0SBjoern A. Zeeb 		 *
36333cee92e0SBjoern A. Zeeb 		 * The conservative default might not actually be a problem
36343cee92e0SBjoern A. Zeeb 		 * if the only case this occurs is when sending an initial
36353cee92e0SBjoern A. Zeeb 		 * SYN with options and data to a host we've never talked
36363cee92e0SBjoern A. Zeeb 		 * to before.  Then, they will reply with an MSS value which
36373cee92e0SBjoern A. Zeeb 		 * will get recorded and the new parameters should get
36383cee92e0SBjoern A. Zeeb 		 * recomputed.  For Further Study.
36393cee92e0SBjoern A. Zeeb 		 */
364097d8d152SAndre Oppermann 	}
3641a0292f23SGarrett Wollman 	mss = min(mss, offer);
364297d8d152SAndre Oppermann 
3643a0292f23SGarrett Wollman 	/*
36440c39d38dSGleb Smirnoff 	 * Sanity check: make sure that maxseg will be large
36453cee92e0SBjoern A. Zeeb 	 * enough to allow some data on segments even if the
36463cee92e0SBjoern A. Zeeb 	 * all the option space is used (40bytes).  Otherwise
36473cee92e0SBjoern A. Zeeb 	 * funny things may happen in tcp_output.
36480c39d38dSGleb Smirnoff 	 *
36490c39d38dSGleb Smirnoff 	 * XXXGL: shouldn't we reserve space for IP/IPv6 options?
36503cee92e0SBjoern A. Zeeb 	 */
36513cee92e0SBjoern A. Zeeb 	mss = max(mss, 64);
36523cee92e0SBjoern A. Zeeb 
365397d8d152SAndre Oppermann 	tp->t_maxseg = mss;
36543cee92e0SBjoern A. Zeeb }
36553cee92e0SBjoern A. Zeeb 
36563cee92e0SBjoern A. Zeeb void
36573cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer)
36583cee92e0SBjoern A. Zeeb {
3659dbc42409SLawrence Stewart 	int mss;
36603ac12506SJonathan T. Looney 	uint32_t bufsize;
36613cee92e0SBjoern A. Zeeb 	struct inpcb *inp;
36623cee92e0SBjoern A. Zeeb 	struct socket *so;
36633cee92e0SBjoern A. Zeeb 	struct hc_metrics_lite metrics;
36643c914c54SAndre Oppermann 	struct tcp_ifcap cap;
3665dbc42409SLawrence Stewart 
36663cee92e0SBjoern A. Zeeb 	KASSERT(tp != NULL, ("%s: tp == NULL", __func__));
36673cee92e0SBjoern A. Zeeb 
36683c914c54SAndre Oppermann 	bzero(&cap, sizeof(cap));
36693c914c54SAndre Oppermann 	tcp_mss_update(tp, offer, -1, &metrics, &cap);
36703cee92e0SBjoern A. Zeeb 
36713cee92e0SBjoern A. Zeeb 	mss = tp->t_maxseg;
36723cee92e0SBjoern A. Zeeb 	inp = tp->t_inpcb;
367397d8d152SAndre Oppermann 
3674df8bae1dSRodney W. Grimes 	/*
367597d8d152SAndre Oppermann 	 * If there's a pipesize, change the socket buffer to that size,
367697d8d152SAndre Oppermann 	 * don't change if sb_hiwat is different than default (then it
367797d8d152SAndre Oppermann 	 * has been changed on purpose with setsockopt).
367897d8d152SAndre Oppermann 	 * Make the socket buffers an integral number of mss units;
367997d8d152SAndre Oppermann 	 * if the mss is larger than the socket buffer, decrease the mss.
3680df8bae1dSRodney W. Grimes 	 */
3681c3b02504SBjoern A. Zeeb 	so = inp->inp_socket;
36823f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
3683e233e2acSAndre Oppermann 	if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe)
368497d8d152SAndre Oppermann 		bufsize = metrics.rmx_sendpipe;
368597d8d152SAndre Oppermann 	else
3686df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
3687df8bae1dSRodney W. Grimes 	if (bufsize < mss)
3688df8bae1dSRodney W. Grimes 		mss = bufsize;
3689df8bae1dSRodney W. Grimes 	else {
3690df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3691df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3692df8bae1dSRodney W. Grimes 			bufsize = sb_max;
369388c39af3SRuslan Ermilov 		if (bufsize > so->so_snd.sb_hiwat)
36943f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_snd, bufsize, so, NULL);
3695df8bae1dSRodney W. Grimes 	}
36963f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
3697784ce8faSHiren Panchasara 	/*
3698784ce8faSHiren Panchasara 	 * Sanity check: make sure that maxseg will be large
3699784ce8faSHiren Panchasara 	 * enough to allow some data on segments even if the
3700784ce8faSHiren Panchasara 	 * all the option space is used (40bytes).  Otherwise
3701784ce8faSHiren Panchasara 	 * funny things may happen in tcp_output.
3702784ce8faSHiren Panchasara 	 *
3703784ce8faSHiren Panchasara 	 * XXXGL: shouldn't we reserve space for IP/IPv6 options?
3704784ce8faSHiren Panchasara 	 */
3705784ce8faSHiren Panchasara 	tp->t_maxseg = max(mss, 64);
3706df8bae1dSRodney W. Grimes 
37073f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
3708e233e2acSAndre Oppermann 	if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe)
370997d8d152SAndre Oppermann 		bufsize = metrics.rmx_recvpipe;
371097d8d152SAndre Oppermann 	else
3711df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
3712df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
3713df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3714df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3715df8bae1dSRodney W. Grimes 			bufsize = sb_max;
371688c39af3SRuslan Ermilov 		if (bufsize > so->so_rcv.sb_hiwat)
37173f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL);
3718df8bae1dSRodney W. Grimes 	}
37193f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
372091d6cfa6SBjoern A. Zeeb 
372191d6cfa6SBjoern A. Zeeb 	/* Check the interface for TSO capabilities. */
37223c914c54SAndre Oppermann 	if (cap.ifcap & CSUM_TSO) {
372391d6cfa6SBjoern A. Zeeb 		tp->t_flags |= TF_TSO;
37243c914c54SAndre Oppermann 		tp->t_tsomax = cap.tsomax;
37259fd573c3SHans Petter Selasky 		tp->t_tsomaxsegcount = cap.tsomaxsegcount;
37269fd573c3SHans Petter Selasky 		tp->t_tsomaxsegsize = cap.tsomaxsegsize;
37273c914c54SAndre Oppermann 	}
3728a0292f23SGarrett Wollman }
3729a0292f23SGarrett Wollman 
3730a0292f23SGarrett Wollman /*
3731a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
3732a0292f23SGarrett Wollman  */
3733a0292f23SGarrett Wollman int
3734ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc)
3735a0292f23SGarrett Wollman {
373697d8d152SAndre Oppermann 	int mss = 0;
37373ac12506SJonathan T. Looney 	uint32_t thcmtu = 0;
37383ac12506SJonathan T. Looney 	uint32_t maxmtu = 0;
373997d8d152SAndre Oppermann 	size_t min_protoh;
3740a0292f23SGarrett Wollman 
374197d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer"));
3742a0292f23SGarrett Wollman 
374331b3783cSHajimu UMEMOTO #ifdef INET6
3744dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
3745603724d3SBjoern A. Zeeb 		mss = V_tcp_v6mssdflt;
3746233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu6(inc, NULL);
374797d8d152SAndre Oppermann 		min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
3748b287c6c7SBjoern A. Zeeb 	}
374931b3783cSHajimu UMEMOTO #endif
3750b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3751b287c6c7SBjoern A. Zeeb 	else
3752b287c6c7SBjoern A. Zeeb #endif
3753b287c6c7SBjoern A. Zeeb #ifdef INET
375497d8d152SAndre Oppermann 	{
3755603724d3SBjoern A. Zeeb 		mss = V_tcp_mssdflt;
3756233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu(inc, NULL);
375797d8d152SAndre Oppermann 		min_protoh = sizeof(struct tcpiphdr);
375897d8d152SAndre Oppermann 	}
3759b287c6c7SBjoern A. Zeeb #endif
37603a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET)
37613a9391deSBjoern A. Zeeb 	thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
37623a9391deSBjoern A. Zeeb #endif
37633a9391deSBjoern A. Zeeb 
376497d8d152SAndre Oppermann 	if (maxmtu && thcmtu)
376597d8d152SAndre Oppermann 		mss = min(maxmtu, thcmtu) - min_protoh;
376697d8d152SAndre Oppermann 	else if (maxmtu || thcmtu)
376797d8d152SAndre Oppermann 		mss = max(maxmtu, thcmtu) - min_protoh;
376897d8d152SAndre Oppermann 
376997d8d152SAndre Oppermann 	return (mss);
3770df8bae1dSRodney W. Grimes }
377146f58482SJonathan Lemon 
377246f58482SJonathan Lemon 
377346f58482SJonathan Lemon /*
3774c068736aSJeffrey Hsu  * On a partial ack arrives, force the retransmission of the
3775c068736aSJeffrey Hsu  * next unacknowledged segment.  Do not clear tp->t_dupacks.
3776c068736aSJeffrey Hsu  * By setting snd_nxt to ti_ack, this forces retransmission timer to
3777c068736aSJeffrey Hsu  * be started again.
377846f58482SJonathan Lemon  */
377955bceb1eSRandall Stewart void
3780ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th)
378146f58482SJonathan Lemon {
378246f58482SJonathan Lemon 	tcp_seq onxt = tp->snd_nxt;
37833ac12506SJonathan T. Looney 	uint32_t ocwnd = tp->snd_cwnd;
37840c39d38dSGleb Smirnoff 	u_int maxseg = tcp_maxseg(tp);
378546f58482SJonathan Lemon 
37868501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
37877a3244ccSRobert Watson 
3788b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_REXMT, 0);
378946f58482SJonathan Lemon 	tp->t_rtttime = 0;
379046f58482SJonathan Lemon 	tp->snd_nxt = th->th_ack;
37916b2a5f92SJayanth Vijayaraghavan 	/*
3792c068736aSJeffrey Hsu 	 * Set snd_cwnd to one segment beyond acknowledged offset.
3793c068736aSJeffrey Hsu 	 * (tp->snd_una has not yet been updated when this function is called.)
37946b2a5f92SJayanth Vijayaraghavan 	 */
37950c39d38dSGleb Smirnoff 	tp->snd_cwnd = maxseg + BYTES_THIS_ACK(tp, th);
3796a84db8f4SMatthew Dillon 	tp->t_flags |= TF_ACKNOW;
379755bceb1eSRandall Stewart 	(void) tp->t_fb->tfb_tcp_output(tp);
379846f58482SJonathan Lemon 	tp->snd_cwnd = ocwnd;
379946f58482SJonathan Lemon 	if (SEQ_GT(onxt, tp->snd_nxt))
380046f58482SJonathan Lemon 		tp->snd_nxt = onxt;
380146f58482SJonathan Lemon 	/*
380246f58482SJonathan Lemon 	 * Partial window deflation.  Relies on fact that tp->snd_una
380346f58482SJonathan Lemon 	 * not updated yet.
380446f58482SJonathan Lemon 	 */
3805dbc42409SLawrence Stewart 	if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th))
3806dbc42409SLawrence Stewart 		tp->snd_cwnd -= BYTES_THIS_ACK(tp, th);
3807d7587117SPaul Saab 	else
3808d7587117SPaul Saab 		tp->snd_cwnd = 0;
38090c39d38dSGleb Smirnoff 	tp->snd_cwnd += maxseg;
381046f58482SJonathan Lemon }
381112eeb81fSHiren Panchasara 
381212eeb81fSHiren Panchasara int
381312eeb81fSHiren Panchasara tcp_compute_pipe(struct tcpcb *tp)
381412eeb81fSHiren Panchasara {
381512eeb81fSHiren Panchasara 	return (tp->snd_max - tp->snd_una +
381612eeb81fSHiren Panchasara 		tp->sackhint.sack_bytes_rexmit -
381712eeb81fSHiren Panchasara 		tp->sackhint.sacked_bytes);
381812eeb81fSHiren Panchasara }
3819