xref: /freebsd/sys/netinet/tcp_input.c (revision 49a6fbe38728173da74d5b497f700178e2a6c830)
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>
531cfd4b53SBruce M Simpson #include "opt_inet.h"
54fb59c426SYoshinobu Inoue #include "opt_inet6.h"
553a2a9f79SYoshinobu Inoue #include "opt_ipsec.h"
56e5738ee0SCheng Cui #include "opt_rss.h"
570cc12cc5SJoerg Wunsch 
58df8bae1dSRodney W. Grimes #include <sys/param.h>
59adc56f5aSEdward Tomasz Napierala #include <sys/arb.h>
6098163b98SPoul-Henning Kamp #include <sys/kernel.h>
61bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
6239bc9de5SLawrence Stewart #include <sys/hhook.h>
63bd79708dSJonathan T. Looney #endif
64df8bae1dSRodney W. Grimes #include <sys/malloc.h>
65df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
66a29f300eSGarrett Wollman #include <sys/proc.h>		/* for proc0 declaration */
67df8bae1dSRodney W. Grimes #include <sys/protosw.h>
68adc56f5aSEdward Tomasz Napierala #include <sys/qmath.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>
76adc56f5aSEdward Tomasz Napierala #include <sys/stats.h>
77df8bae1dSRodney W. Grimes 
78e79adb8eSGarrett Wollman #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
79e79adb8eSGarrett Wollman 
8012e2e970SAndre Oppermann #include <vm/uma.h>
8112e2e970SAndre Oppermann 
82df8bae1dSRodney W. Grimes #include <net/if.h>
8376039bc8SGleb Smirnoff #include <net/if_var.h>
84df8bae1dSRodney W. Grimes #include <net/route.h>
85e5738ee0SCheng Cui #include <net/rss_config.h>
86530c0060SRobert Watson #include <net/vnet.h>
87df8bae1dSRodney W. Grimes 
88773673c1SAndre Oppermann #define TCPSTATES		/* for logging */
89773673c1SAndre Oppermann 
90df8bae1dSRodney W. Grimes #include <netinet/in.h>
9157f60867SMark Johnston #include <netinet/in_kdtrace.h>
92960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
93e5738ee0SCheng Cui #include <netinet/in_rss.h>
94df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
95df8bae1dSRodney W. Grimes #include <netinet/ip.h>
968e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h>	/* required for icmp_var.h */
97686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM */
98df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
99ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
100686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
101686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
102686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
103e5738ee0SCheng Cui #include <netinet6/in6_rss.h>
104b66d74c1SGleb Smirnoff #include <netinet6/in6_var.h>
105960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h>
106960ed29cSSeigo Tanimura #include <netinet6/nd6.h>
1072de3e790SGleb Smirnoff #include <netinet/tcp.h>
108df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
109df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
110df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
111df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
11269c7c811SRandall Stewart #include <netinet/tcp_log_buf.h>
113fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h>
114df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
1154644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
116c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h>
11786a996e6SHiren Panchasara #ifdef TCPPCAP
11886a996e6SHiren Panchasara #include <netinet/tcp_pcap.h>
11986a996e6SHiren Panchasara #endif
120c325962bSMike Silbersack #include <netinet/tcp_syncache.h>
12109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
12209fe6320SNavdeep Parhar #include <netinet/tcp_offload.h>
12309fe6320SNavdeep Parhar #endif
124f7220c48SRichard Scheffenegger #include <netinet/tcp_ecn.h>
1259e644c23SMichael Tuexen #include <netinet/udp.h>
126fb59c426SYoshinobu Inoue 
127fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
128b9234fafSSam Leffler 
129db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
130db4f9cc7SJonathan Lemon 
131aed55708SRobert Watson #include <security/mac/mac_framework.h>
132aed55708SRobert Watson 
133dbc42409SLawrence Stewart const int tcprexmtthresh = 3;
1340312fbe9SPoul-Henning Kamp 
135334fc582SBjoern A. Zeeb VNET_DEFINE(int, tcp_log_in_vain) = 0;
136334fc582SBjoern A. Zeeb SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_VNET | CTLFLAG_RW,
137334fc582SBjoern A. Zeeb     &VNET_NAME(tcp_log_in_vain), 0,
138eddfbb76SRobert Watson     "Log all incoming TCP segments to closed ports");
139816a3d83SPoul-Henning Kamp 
14082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, blackhole) = 0;
14182cea7e6SBjoern A. Zeeb #define	V_blackhole		VNET(blackhole)
1426df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW,
143eddfbb76SRobert Watson     &VNET_NAME(blackhole), 0,
144eddfbb76SRobert Watson     "Do not send RST on segments to closed ports");
14516f7f31fSGeoff Rehmet 
1463ea9a7cfSGleb Smirnoff VNET_DEFINE(bool, blackhole_local) = false;
1473ea9a7cfSGleb Smirnoff #define	V_blackhole_local	VNET(blackhole_local)
1483ea9a7cfSGleb Smirnoff SYSCTL_BOOL(_net_inet_tcp, OID_AUTO, blackhole_local, CTLFLAG_VNET |
1493ea9a7cfSGleb Smirnoff     CTLFLAG_RW, &VNET_NAME(blackhole_local), false,
1503ea9a7cfSGleb Smirnoff     "Enforce net.inet.tcp.blackhole for locally originated packets");
1513ea9a7cfSGleb Smirnoff 
15282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_delack_enabled) = 1;
1536df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_VNET | CTLFLAG_RW,
154eddfbb76SRobert Watson     &VNET_NAME(tcp_delack_enabled), 0,
1553d177f46SBill Fumerola     "Delay ACK to try and piggyback it onto a data packet");
156f498eeeeSDavid Greenman 
15782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, drop_synfin) = 0;
1586df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_VNET | CTLFLAG_RW,
159eddfbb76SRobert Watson     &VNET_NAME(drop_synfin), 0,
160eddfbb76SRobert Watson     "Drop TCP packets with SYN+FIN set");
161f8613305SDag-Erling Smørgrav 
1620e1d7c25SRichard Scheffenegger VNET_DEFINE(int, tcp_do_prr) = 1;
1630e1d7c25SRichard Scheffenegger SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_prr, CTLFLAG_VNET | CTLFLAG_RW,
1640e1d7c25SRichard Scheffenegger     &VNET_NAME(tcp_do_prr), 1,
1650e1d7c25SRichard Scheffenegger     "Enable Proportional Rate Reduction per RFC 6937");
1660e1d7c25SRichard Scheffenegger 
1670471a8c7SRichard Scheffenegger VNET_DEFINE(int, tcp_do_lrd) = 0;
1680471a8c7SRichard Scheffenegger SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_lrd, CTLFLAG_VNET | CTLFLAG_RW,
1690471a8c7SRichard Scheffenegger     &VNET_NAME(tcp_do_lrd), 1,
1700471a8c7SRichard Scheffenegger     "Perform Lost Retransmission Detection");
1710471a8c7SRichard Scheffenegger 
172b72e56e7SMichael Tuexen VNET_DEFINE(int, tcp_do_newcwv) = 0;
173b72e56e7SMichael Tuexen SYSCTL_INT(_net_inet_tcp, OID_AUTO, newcwv, CTLFLAG_VNET | CTLFLAG_RW,
174b72e56e7SMichael Tuexen     &VNET_NAME(tcp_do_newcwv), 0,
175b72e56e7SMichael Tuexen     "Enable New Congestion Window Validation per RFC7661");
176b72e56e7SMichael Tuexen 
17782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3042) = 1;
1786df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_VNET | CTLFLAG_RW,
179eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3042), 0,
180eddfbb76SRobert Watson     "Enable RFC 3042 (Limited Transmit)");
181582a954bSJeffrey Hsu 
18282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3390) = 1;
1836df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_VNET | CTLFLAG_RW,
184eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3390), 0,
185da3a8a1aSJeffrey Hsu     "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)");
186da3a8a1aSJeffrey Hsu 
187356c7958SHiren Panchasara VNET_DEFINE(int, tcp_initcwnd_segments) = 10;
188356c7958SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, initcwnd_segments,
189356c7958SHiren Panchasara     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_initcwnd_segments), 0,
190356c7958SHiren Panchasara     "Slow-start flight size (initial congestion window) in number of segments");
19109440655SAndre Oppermann 
19282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3465) = 1;
1936df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_VNET | CTLFLAG_RW,
194eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3465), 0,
19524cb0f22SLawrence Stewart     "Enable RFC 3465 (Appropriate Byte Counting)");
196eddfbb76SRobert Watson 
19782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_abc_l_var) = 2;
1986df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_VNET | CTLFLAG_RW,
199eddfbb76SRobert Watson     &VNET_NAME(tcp_abc_l_var), 2,
20024cb0f22SLawrence Stewart     "Cap the max cwnd increment during slow-start to this number of segments");
20124cb0f22SLawrence Stewart 
2023220a212SGleb Smirnoff VNET_DEFINE(int, tcp_insecure_syn) = 0;
2036df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_syn, CTLFLAG_VNET | CTLFLAG_RW,
2043220a212SGleb Smirnoff     &VNET_NAME(tcp_insecure_syn), 0,
2053220a212SGleb Smirnoff     "Follow RFC793 instead of RFC5961 criteria for accepting SYN packets");
2063220a212SGleb Smirnoff 
20782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_insecure_rst) = 0;
2086df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_VNET | CTLFLAG_RW,
209eddfbb76SRobert Watson     &VNET_NAME(tcp_insecure_rst), 0,
2103220a212SGleb Smirnoff     "Follow RFC793 instead of RFC5961 criteria for accepting RST packets");
211a69968eeSMike Silbersack 
212fba0cea1SBjoern A. Zeeb VNET_DEFINE(int, tcp_recvspace) = 1024*64;
213873789cbSAndre Oppermann #define	V_tcp_recvspace	VNET(tcp_recvspace)
2146df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_VNET | CTLFLAG_RW,
215873789cbSAndre Oppermann     &VNET_NAME(tcp_recvspace), 0, "Initial receive socket buffer size");
216873789cbSAndre Oppermann 
21782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autorcvbuf) = 1;
2186df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_VNET | CTLFLAG_RW,
219eddfbb76SRobert Watson     &VNET_NAME(tcp_do_autorcvbuf), 0,
2208b615593SMarko Zec     "Enable automatic receive buffer sizing");
2216741ecf5SAndre Oppermann 
222b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024;
2236df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_VNET | CTLFLAG_RW,
224eddfbb76SRobert Watson     &VNET_NAME(tcp_autorcvbuf_max), 0,
2258b615593SMarko Zec     "Max size of automatic receive buffer");
2266741ecf5SAndre Oppermann 
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 
2415923c293SGleb Smirnoff /*
2427ca6e296SMichael Tuexen  * Kernel module interface for updating tcpstat.  The first argument is an index
2435923c293SGleb Smirnoff  * into tcpstat treated as an array.
244315e3e38SRobert Watson  */
245315e3e38SRobert Watson void
2467ca6e296SMichael Tuexen kmod_tcpstat_add(int statnum, int val)
247315e3e38SRobert Watson {
248315e3e38SRobert Watson 
2497ca6e296SMichael Tuexen 	counter_u64_add(VNET(tcpstat)[statnum], val);
250315e3e38SRobert Watson }
251315e3e38SRobert Watson 
252c21b7b55SRichard Scheffenegger /*
253c21b7b55SRichard Scheffenegger  * Make sure that we only start a SACK loss recovery when
254c21b7b55SRichard Scheffenegger  * receiving a duplicate ACK with a SACK block, and also
255c21b7b55SRichard Scheffenegger  * complete SACK loss recovery in case the other end
256c21b7b55SRichard Scheffenegger  * reneges.
257c21b7b55SRichard Scheffenegger  */
258c21b7b55SRichard Scheffenegger static bool inline
259c21b7b55SRichard Scheffenegger tcp_is_sack_recovery(struct tcpcb *tp, struct tcpopt *to)
260c21b7b55SRichard Scheffenegger {
261c21b7b55SRichard Scheffenegger 	return ((tp->t_flags & TF_SACK_PERMIT) &&
262c21b7b55SRichard Scheffenegger 		((to->to_flags & TOF_SACK) ||
263c21b7b55SRichard Scheffenegger 		(!TAILQ_EMPTY(&tp->snd_holes))));
264c21b7b55SRichard Scheffenegger }
265c21b7b55SRichard Scheffenegger 
266bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
267dbc42409SLawrence Stewart /*
26839bc9de5SLawrence Stewart  * Wrapper for the TCP established input helper hook.
26939bc9de5SLawrence Stewart  */
27055bceb1eSRandall Stewart void
27139bc9de5SLawrence Stewart hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to)
27239bc9de5SLawrence Stewart {
27339bc9de5SLawrence Stewart 	struct tcp_hhook_data hhook_data;
27439bc9de5SLawrence Stewart 
27539bc9de5SLawrence Stewart 	if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) {
27639bc9de5SLawrence Stewart 		hhook_data.tp = tp;
27739bc9de5SLawrence Stewart 		hhook_data.th = th;
27839bc9de5SLawrence Stewart 		hhook_data.to = to;
27939bc9de5SLawrence Stewart 
28039bc9de5SLawrence Stewart 		hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data,
281e68b3792SGleb Smirnoff 		    &tp->t_osd);
28239bc9de5SLawrence Stewart 	}
28339bc9de5SLawrence Stewart }
284bd79708dSJonathan T. Looney #endif
28539bc9de5SLawrence Stewart 
28639bc9de5SLawrence Stewart /*
287dbc42409SLawrence Stewart  * CC wrapper hook functions
288dbc42409SLawrence Stewart  */
28955bceb1eSRandall Stewart void
2904b7b743cSLawrence Stewart cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t nsegs,
2914b7b743cSLawrence Stewart     uint16_t type)
292f2512ba1SRui Paulo {
293adc56f5aSEdward Tomasz Napierala #ifdef STATS
294adc56f5aSEdward Tomasz Napierala 	int32_t gput;
295adc56f5aSEdward Tomasz Napierala #endif
296adc56f5aSEdward Tomasz Napierala 
2979eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
298f2512ba1SRui Paulo 
299e68b3792SGleb Smirnoff 	tp->t_ccv.nsegs = nsegs;
300e68b3792SGleb Smirnoff 	tp->t_ccv.bytes_this_ack = BYTES_THIS_ACK(tp, th);
301b72e56e7SMichael Tuexen 	if ((!V_tcp_do_newcwv && (tp->snd_cwnd <= tp->snd_wnd)) ||
302b72e56e7SMichael Tuexen 	    (V_tcp_do_newcwv && (tp->snd_cwnd <= tp->snd_wnd) &&
303b72e56e7SMichael Tuexen 	     (tp->snd_cwnd < (tcp_compute_pipe(tp) * 2))))
304e68b3792SGleb Smirnoff 		tp->t_ccv.flags |= CCF_CWND_LIMITED;
305dbc42409SLawrence Stewart 	else
306e68b3792SGleb Smirnoff 		tp->t_ccv.flags &= ~CCF_CWND_LIMITED;
307dbc42409SLawrence Stewart 
308dbc42409SLawrence Stewart 	if (type == CC_ACK) {
309adc56f5aSEdward Tomasz Napierala #ifdef STATS
310adc56f5aSEdward Tomasz Napierala 		stats_voi_update_abs_s32(tp->t_stats, VOI_TCP_CALCFRWINDIFF,
311adc56f5aSEdward Tomasz Napierala 		    ((int32_t)tp->snd_cwnd) - tp->snd_wnd);
312adc56f5aSEdward Tomasz Napierala 		if (!IN_RECOVERY(tp->t_flags))
313adc56f5aSEdward Tomasz Napierala 			stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_ACKLEN,
314e68b3792SGleb Smirnoff 			   tp->t_ccv.bytes_this_ack / (tcp_maxseg(tp) * nsegs));
315adc56f5aSEdward Tomasz Napierala 		if ((tp->t_flags & TF_GPUTINPROG) &&
316adc56f5aSEdward Tomasz Napierala 		    SEQ_GEQ(th->th_ack, tp->gput_ack)) {
317adc56f5aSEdward Tomasz Napierala 			/*
318adc56f5aSEdward Tomasz Napierala 			 * Compute goodput in bits per millisecond.
319adc56f5aSEdward Tomasz Napierala 			 */
320f5766992SHans Petter Selasky 			gput = (((int64_t)SEQ_SUB(th->th_ack, tp->gput_seq)) << 3) /
321adc56f5aSEdward Tomasz Napierala 			    max(1, tcp_ts_getticks() - tp->gput_ts);
322adc56f5aSEdward Tomasz Napierala 			stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT,
323adc56f5aSEdward Tomasz Napierala 			    gput);
324adc56f5aSEdward Tomasz Napierala 			/*
325adc56f5aSEdward Tomasz Napierala 			 * XXXLAS: This is a temporary hack, and should be
326adc56f5aSEdward Tomasz Napierala 			 * chained off VOI_TCP_GPUT when stats(9) grows an API
327adc56f5aSEdward Tomasz Napierala 			 * to deal with chained VOIs.
328adc56f5aSEdward Tomasz Napierala 			 */
329adc56f5aSEdward Tomasz Napierala 			if (tp->t_stats_gput_prev > 0)
330adc56f5aSEdward Tomasz Napierala 				stats_voi_update_abs_s32(tp->t_stats,
331adc56f5aSEdward Tomasz Napierala 				    VOI_TCP_GPUT_ND,
332adc56f5aSEdward Tomasz Napierala 				    ((gput - tp->t_stats_gput_prev) * 100) /
333adc56f5aSEdward Tomasz Napierala 				    tp->t_stats_gput_prev);
334adc56f5aSEdward Tomasz Napierala 			tp->t_flags &= ~TF_GPUTINPROG;
335adc56f5aSEdward Tomasz Napierala 			tp->t_stats_gput_prev = gput;
336adc56f5aSEdward Tomasz Napierala 		}
337adc56f5aSEdward Tomasz Napierala #endif /* STATS */
338dbc42409SLawrence Stewart 		if (tp->snd_cwnd > tp->snd_ssthresh) {
339e68b3792SGleb Smirnoff 			tp->t_bytes_acked += tp->t_ccv.bytes_this_ack;
340dbc42409SLawrence Stewart 			if (tp->t_bytes_acked >= tp->snd_cwnd) {
341dbc42409SLawrence Stewart 				tp->t_bytes_acked -= tp->snd_cwnd;
342e68b3792SGleb Smirnoff 				tp->t_ccv.flags |= CCF_ABC_SENTAWND;
343dbc42409SLawrence Stewart 			}
344dbc42409SLawrence Stewart 		} else {
345e68b3792SGleb Smirnoff 				tp->t_ccv.flags &= ~CCF_ABC_SENTAWND;
346dbc42409SLawrence Stewart 				tp->t_bytes_acked = 0;
347dbc42409SLawrence Stewart 		}
348dbc42409SLawrence Stewart 	}
349dbc42409SLawrence Stewart 
350dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->ack_received != NULL) {
351dbc42409SLawrence Stewart 		/* XXXLAS: Find a way to live without this */
352e68b3792SGleb Smirnoff 		tp->t_ccv.curack = th->th_ack;
353e68b3792SGleb Smirnoff 		CC_ALGO(tp)->ack_received(&tp->t_ccv, type);
354dbc42409SLawrence Stewart 	}
355adc56f5aSEdward Tomasz Napierala #ifdef STATS
356adc56f5aSEdward Tomasz Napierala 	stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_LCWIN, tp->snd_cwnd);
357adc56f5aSEdward Tomasz Napierala #endif
358dbc42409SLawrence Stewart }
359dbc42409SLawrence Stewart 
36055bceb1eSRandall Stewart void
361dbc42409SLawrence Stewart cc_conn_init(struct tcpcb *tp)
362dbc42409SLawrence Stewart {
363dbc42409SLawrence Stewart 	struct hc_metrics_lite metrics;
3649eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
3650c39d38dSGleb Smirnoff 	u_int maxseg;
366dbc42409SLawrence Stewart 	int rtt;
367dbc42409SLawrence Stewart 
3689eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
369dbc42409SLawrence Stewart 
370dbc42409SLawrence Stewart 	tcp_hc_get(&inp->inp_inc, &metrics);
3710c39d38dSGleb Smirnoff 	maxseg = tcp_maxseg(tp);
372dbc42409SLawrence Stewart 
373dbc42409SLawrence Stewart 	if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) {
374dbc42409SLawrence Stewart 		tp->t_srtt = rtt;
375dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_usedrtt);
376dbc42409SLawrence Stewart 		if (metrics.rmx_rttvar) {
377dbc42409SLawrence Stewart 			tp->t_rttvar = metrics.rmx_rttvar;
378dbc42409SLawrence Stewart 			TCPSTAT_INC(tcps_usedrttvar);
379dbc42409SLawrence Stewart 		} else {
380dbc42409SLawrence Stewart 			/* default variation is +- 1 rtt */
381dbc42409SLawrence Stewart 			tp->t_rttvar =
382dbc42409SLawrence Stewart 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
383dbc42409SLawrence Stewart 		}
384dbc42409SLawrence Stewart 		TCPT_RANGESET(tp->t_rxtcur,
385dbc42409SLawrence Stewart 		    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
386dbc42409SLawrence Stewart 		    tp->t_rttmin, TCPTV_REXMTMAX);
387dbc42409SLawrence Stewart 	}
388dbc42409SLawrence Stewart 	if (metrics.rmx_ssthresh) {
389dbc42409SLawrence Stewart 		/*
390dbc42409SLawrence Stewart 		 * There's some sort of gateway or interface
391dbc42409SLawrence Stewart 		 * buffer limit on the path.  Use this to set
392a4641f4eSPedro F. Giffuni 		 * the slow start threshold, but set the
393dbc42409SLawrence Stewart 		 * threshold to no less than 2*mss.
394dbc42409SLawrence Stewart 		 */
3950c39d38dSGleb Smirnoff 		tp->snd_ssthresh = max(2 * maxseg, metrics.rmx_ssthresh);
396dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_usedssthresh);
397dbc42409SLawrence Stewart 	}
398dbc42409SLawrence Stewart 
399dbc42409SLawrence Stewart 	/*
4009ec4a4ccSAndre Oppermann 	 * Set the initial slow-start flight size.
401dbc42409SLawrence Stewart 	 *
402cf8f04f4SAndre Oppermann 	 * If a SYN or SYN/ACK was lost and retransmitted, we have to
403cf8f04f4SAndre Oppermann 	 * reduce the initial CWND to one segment as congestion is likely
404cf8f04f4SAndre Oppermann 	 * requiring us to be cautious.
405dbc42409SLawrence Stewart 	 */
406cf8f04f4SAndre Oppermann 	if (tp->snd_cwnd == 1)
4070c39d38dSGleb Smirnoff 		tp->snd_cwnd = maxseg;		/* SYN(-ACK) lost */
408dbc42409SLawrence Stewart 	else
4097dc90a1dSMichael Tuexen 		tp->snd_cwnd = tcp_compute_initwnd(maxseg);
410dbc42409SLawrence Stewart 
411dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->conn_init != NULL)
412e68b3792SGleb Smirnoff 		CC_ALGO(tp)->conn_init(&tp->t_ccv);
413dbc42409SLawrence Stewart }
414dbc42409SLawrence Stewart 
415dbc42409SLawrence Stewart void inline
416dbc42409SLawrence Stewart cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type)
417dbc42409SLawrence Stewart {
4189eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
419dbc42409SLawrence Stewart 
420adc56f5aSEdward Tomasz Napierala #ifdef STATS
421adc56f5aSEdward Tomasz Napierala 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_CSIG, type);
422adc56f5aSEdward Tomasz Napierala #endif
423adc56f5aSEdward Tomasz Napierala 
424dbc42409SLawrence Stewart 	switch(type) {
425dbc42409SLawrence Stewart 	case CC_NDUPACK:
426dbc42409SLawrence Stewart 		if (!IN_FASTRECOVERY(tp->t_flags)) {
427f2512ba1SRui Paulo 			tp->snd_recover = tp->snd_max;
4283cf38784SMichael Tuexen 			if (tp->t_flags2 & TF2_ECN_PERMIT)
4293cf38784SMichael Tuexen 				tp->t_flags2 |= TF2_ECN_SND_CWR;
430f2512ba1SRui Paulo 		}
431dbc42409SLawrence Stewart 		break;
432dbc42409SLawrence Stewart 	case CC_ECN:
433af2fb894SRichard Scheffenegger 		if (!IN_CONGRECOVERY(tp->t_flags) ||
434af2fb894SRichard Scheffenegger 		    /*
435af2fb894SRichard Scheffenegger 		     * Allow ECN reaction on ACK to CWR, if
436af2fb894SRichard Scheffenegger 		     * that data segment was also CE marked.
437af2fb894SRichard Scheffenegger 		     */
438af2fb894SRichard Scheffenegger 		    SEQ_GEQ(th->th_ack, tp->snd_recover)) {
439af2fb894SRichard Scheffenegger 			EXIT_CONGRECOVERY(tp->t_flags);
440dbc42409SLawrence Stewart 			TCPSTAT_INC(tcps_ecn_rcwnd);
441af2fb894SRichard Scheffenegger 			tp->snd_recover = tp->snd_max + 1;
4423cf38784SMichael Tuexen 			if (tp->t_flags2 & TF2_ECN_PERMIT)
4433cf38784SMichael Tuexen 				tp->t_flags2 |= TF2_ECN_SND_CWR;
444dbc42409SLawrence Stewart 		}
445dbc42409SLawrence Stewart 		break;
446dbc42409SLawrence Stewart 	case CC_RTO:
447dbc42409SLawrence Stewart 		tp->t_dupacks = 0;
448dbc42409SLawrence Stewart 		tp->t_bytes_acked = 0;
449dbc42409SLawrence Stewart 		EXIT_RECOVERY(tp->t_flags);
4509cc711c9SMichael Tuexen 		if (tp->t_flags2 & TF2_ECN_PERMIT)
4519cc711c9SMichael Tuexen 			tp->t_flags2 |= TF2_ECN_SND_CWR;
452dbc42409SLawrence Stewart 		break;
453dbc42409SLawrence Stewart 	case CC_RTO_ERR:
454dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_sndrexmitbad);
455dbc42409SLawrence Stewart 		/* RTO was unnecessary, so reset everything. */
456dbc42409SLawrence Stewart 		tp->snd_cwnd = tp->snd_cwnd_prev;
457dbc42409SLawrence Stewart 		tp->snd_ssthresh = tp->snd_ssthresh_prev;
458dbc42409SLawrence Stewart 		tp->snd_recover = tp->snd_recover_prev;
459dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASFRECOVERY)
460dbc42409SLawrence Stewart 			ENTER_FASTRECOVERY(tp->t_flags);
461dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASCRECOVERY)
462dbc42409SLawrence Stewart 			ENTER_CONGRECOVERY(tp->t_flags);
463dbc42409SLawrence Stewart 		tp->snd_nxt = tp->snd_max;
464672dc4aeSJohn Baldwin 		tp->t_flags &= ~TF_PREVVALID;
465dbc42409SLawrence Stewart 		tp->t_badrxtwin = 0;
466dbc42409SLawrence Stewart 		break;
467dbc42409SLawrence Stewart 	}
468dbc42409SLawrence Stewart 
469dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->cong_signal != NULL) {
470dbc42409SLawrence Stewart 		if (th != NULL)
471e68b3792SGleb Smirnoff 			tp->t_ccv.curack = th->th_ack;
472e68b3792SGleb Smirnoff 		CC_ALGO(tp)->cong_signal(&tp->t_ccv, type);
473dbc42409SLawrence Stewart 	}
474dbc42409SLawrence Stewart }
475dbc42409SLawrence Stewart 
47655bceb1eSRandall Stewart void inline
477dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th)
478dbc42409SLawrence Stewart {
4799eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
480dbc42409SLawrence Stewart 
481dbc42409SLawrence Stewart 	/* XXXLAS: KASSERT that we're in recovery? */
482dbc42409SLawrence Stewart 
483dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->post_recovery != NULL) {
484e68b3792SGleb Smirnoff 		tp->t_ccv.curack = th->th_ack;
485e68b3792SGleb Smirnoff 		CC_ALGO(tp)->post_recovery(&tp->t_ccv);
486dbc42409SLawrence Stewart 	}
487dbc42409SLawrence Stewart 	/* XXXLAS: EXIT_RECOVERY ? */
488dbc42409SLawrence Stewart 	tp->t_bytes_acked = 0;
48948be5b97SRichard Scheffenegger 	tp->sackhint.delivered_data = 0;
490e5313869SRichard Scheffenegger 	tp->sackhint.prr_out = 0;
491dbc42409SLawrence Stewart }
4920312fbe9SPoul-Henning Kamp 
493df8bae1dSRodney W. Grimes /*
494262c1c1aSMatthew Dillon  * Indicate whether this ack should be delayed.  We can delay the ack if
49585536381SHiren Panchasara  * following conditions are met:
49685536381SHiren Panchasara  *	- There is no delayed ack timer in progress.
49785536381SHiren Panchasara  *	- Our last ack wasn't a 0-sized window. We never want to delay
49885536381SHiren Panchasara  *	  the ack that opens up a 0-sized window.
49985536381SHiren Panchasara  *	- LRO wasn't used for this segment. We make sure by checking that the
50085536381SHiren Panchasara  *	  segment size is not larger than the MSS.
501d8c85a26SJonathan Lemon  */
502c1e5a6e5SAndre Oppermann #define DELAY_ACK(tp, tlen)						\
503b8152ba7SAndre Oppermann 	((!tcp_timer_active(tp, TT_DELACK) &&				\
504a14c749fSJonathan Lemon 	    (tp->t_flags & TF_RXWIN0SENT) == 0) &&			\
5050c39d38dSGleb Smirnoff 	    (tlen <= tp->t_maxseg) &&					\
506603724d3SBjoern A. Zeeb 	    (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN)))
507d8c85a26SJonathan Lemon 
5084ad24737SRandall Stewart void inline
5095d8fd932SRandall Stewart cc_ecnpkt_handler_flags(struct tcpcb *tp, uint16_t flags, uint8_t iptos)
51064807b30SHiren Panchasara {
5119eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
51264807b30SHiren Panchasara 
51364807b30SHiren Panchasara 	if (CC_ALGO(tp)->ecnpkt_handler != NULL) {
51464807b30SHiren Panchasara 		switch (iptos & IPTOS_ECN_MASK) {
51564807b30SHiren Panchasara 		case IPTOS_ECN_CE:
516e68b3792SGleb Smirnoff 			tp->t_ccv.flags |= CCF_IPHDR_CE;
51764807b30SHiren Panchasara 			break;
51864807b30SHiren Panchasara 		case IPTOS_ECN_ECT0:
51983a2839fSMichael Tuexen 			/* FALLTHROUGH */
52064807b30SHiren Panchasara 		case IPTOS_ECN_ECT1:
52183a2839fSMichael Tuexen 			/* FALLTHROUGH */
52283a2839fSMichael Tuexen 		case IPTOS_ECN_NOTECT:
523e68b3792SGleb Smirnoff 			tp->t_ccv.flags &= ~CCF_IPHDR_CE;
52464807b30SHiren Panchasara 			break;
52564807b30SHiren Panchasara 		}
52664807b30SHiren Panchasara 
5275d8fd932SRandall Stewart 		if (flags & TH_CWR)
528e68b3792SGleb Smirnoff 			tp->t_ccv.flags |= CCF_TCPHDR_CWR;
52964807b30SHiren Panchasara 		else
530e68b3792SGleb Smirnoff 			tp->t_ccv.flags &= ~CCF_TCPHDR_CWR;
53164807b30SHiren Panchasara 
532e68b3792SGleb Smirnoff 		CC_ALGO(tp)->ecnpkt_handler(&tp->t_ccv);
53364807b30SHiren Panchasara 
534e68b3792SGleb Smirnoff 		if (tp->t_ccv.flags & CCF_ACKNOW) {
53564807b30SHiren Panchasara 			tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
536e11c9783SMichael Tuexen 			tp->t_flags |= TF_ACKNOW;
537e11c9783SMichael Tuexen 		}
53864807b30SHiren Panchasara 	}
53964807b30SHiren Panchasara }
54064807b30SHiren Panchasara 
5415d8fd932SRandall Stewart void inline
5425d8fd932SRandall Stewart cc_ecnpkt_handler(struct tcpcb *tp, struct tcphdr *th, uint8_t iptos)
5435d8fd932SRandall Stewart {
5441ebf4607SRichard Scheffenegger 	cc_ecnpkt_handler_flags(tp, tcp_get_flags(th), iptos);
5455d8fd932SRandall Stewart }
5465d8fd932SRandall Stewart 
547df8bae1dSRodney W. Grimes /*
5488d573cc1SAndre Oppermann  * TCP input handling is split into multiple parts:
5498d573cc1SAndre Oppermann  *   tcp6_input is a thin wrapper around tcp_input for the extended
5508d573cc1SAndre Oppermann  *	ip6_protox[] call format in ip6_input
5518d573cc1SAndre Oppermann  *   tcp_input handles primary segment validation, inpcb lookup and
5528d573cc1SAndre Oppermann  *	SYN processing on listen sockets
5538d573cc1SAndre Oppermann  *   tcp_do_segment processes the ACK and text of the segment for
5548d573cc1SAndre Oppermann  *	establishing, established and closing connections
555df8bae1dSRodney W. Grimes  */
556fb59c426SYoshinobu Inoue #ifdef INET6
557fb59c426SYoshinobu Inoue int
5589e644c23SMichael Tuexen tcp6_input_with_port(struct mbuf **mp, int *offp, int proto, uint16_t port)
559fb59c426SYoshinobu Inoue {
560503f4e47SBjoern A. Zeeb 	struct mbuf *m;
56133841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6;
5623e88eb90SAndrey V. Elsukov 	struct ip6_hdr *ip6;
563fb59c426SYoshinobu Inoue 
564503f4e47SBjoern A. Zeeb 	m = *mp;
565a4adf6ccSBjoern A. Zeeb 	if (m->m_len < *offp + sizeof(struct tcphdr)) {
5664e619b17SBjoern A. Zeeb 		m = m_pullup(m, *offp + sizeof(struct tcphdr));
5674e619b17SBjoern A. Zeeb 		if (m == NULL) {
5684e619b17SBjoern A. Zeeb 			*mp = m;
5694e619b17SBjoern A. Zeeb 			TCPSTAT_INC(tcps_rcvshort);
5704e619b17SBjoern A. Zeeb 			return (IPPROTO_DONE);
5714e619b17SBjoern A. Zeeb 		}
572a4adf6ccSBjoern A. Zeeb 	}
573fb59c426SYoshinobu Inoue 
574fb59c426SYoshinobu Inoue 	/*
575fb59c426SYoshinobu Inoue 	 * draft-itojun-ipv6-tcp-to-anycast
576fb59c426SYoshinobu Inoue 	 * better place to put this in?
577fb59c426SYoshinobu Inoue 	 */
5783e88eb90SAndrey V. Elsukov 	ip6 = mtod(m, struct ip6_hdr *);
5798268d82cSAlexander V. Chernikov 	ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false);
58033841545SHajimu UMEMOTO 	if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
581fb59c426SYoshinobu Inoue 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
582fb59c426SYoshinobu Inoue 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
583a8fe77d8SBjoern A. Zeeb 		*mp = NULL;
5848f5a8818SKevin Lo 		return (IPPROTO_DONE);
585fb59c426SYoshinobu Inoue 	}
586fb59c426SYoshinobu Inoue 
587a8fe77d8SBjoern A. Zeeb 	*mp = m;
5889e644c23SMichael Tuexen 	return (tcp_input_with_port(mp, offp, proto, port));
5899e644c23SMichael Tuexen }
5909e644c23SMichael Tuexen 
5919e644c23SMichael Tuexen int
5929e644c23SMichael Tuexen tcp6_input(struct mbuf **mp, int *offp, int proto)
5939e644c23SMichael Tuexen {
5949e644c23SMichael Tuexen 
5959e644c23SMichael Tuexen 	return(tcp6_input_with_port(mp, offp, proto, 0));
596fb59c426SYoshinobu Inoue }
597b287c6c7SBjoern A. Zeeb #endif /* INET6 */
598fb59c426SYoshinobu Inoue 
5998f5a8818SKevin Lo int
6009e644c23SMichael Tuexen tcp_input_with_port(struct mbuf **mp, int *offp, int proto, uint16_t port)
601df8bae1dSRodney W. Grimes {
6028f5a8818SKevin Lo 	struct mbuf *m = *mp;
603b287c6c7SBjoern A. Zeeb 	struct tcphdr *th = NULL;
604ad3f9ab3SAndre Oppermann 	struct ip *ip = NULL;
605ad3f9ab3SAndre Oppermann 	struct inpcb *inp = NULL;
606302ce8d6SAndre Oppermann 	struct tcpcb *tp = NULL;
607302ce8d6SAndre Oppermann 	struct socket *so = NULL;
608e79adb8eSGarrett Wollman 	u_char *optp = NULL;
6098f5a8818SKevin Lo 	int off0;
61026f9a767SRodney W. Grimes 	int optlen = 0;
611b287c6c7SBjoern A. Zeeb #ifdef INET
612b287c6c7SBjoern A. Zeeb 	int len;
613c4556b2fSAndrey V. Elsukov 	uint8_t ipttl;
614b287c6c7SBjoern A. Zeeb #endif
615b287c6c7SBjoern A. Zeeb 	int tlen = 0, off;
616fb59c426SYoshinobu Inoue 	int drop_hdrlen;
617ad3f9ab3SAndre Oppermann 	int thflags;
618302ce8d6SAndre Oppermann 	int rstreason = 0;	/* For badport_bandlim accounting purposes */
61908d9c920SGleb Smirnoff 	int lookupflag;
6201d7ee746SKurt Lidl 	uint8_t iptos;
621c1de64a4SAndrey V. Elsukov 	struct m_tag *fwd_tag = NULL;
622c068736aSJeffrey Hsu #ifdef INET6
623302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6 = NULL;
624c068736aSJeffrey Hsu 	int isipv6;
625c068736aSJeffrey Hsu #else
626a160e630SAndre Oppermann 	const void *ip6 = NULL;
627b287c6c7SBjoern A. Zeeb #endif /* INET6 */
628302ce8d6SAndre Oppermann 	struct tcpopt to;		/* options in this segment */
629a160e630SAndre Oppermann 	char *s = NULL;			/* address and port logging */
630c068736aSJeffrey Hsu 
631d40c0d47SGleb Smirnoff 	NET_EPOCH_ASSERT();
632d40c0d47SGleb Smirnoff 
633fb59c426SYoshinobu Inoue #ifdef INET6
634fb59c426SYoshinobu Inoue 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
635fb59c426SYoshinobu Inoue #endif
636a0292f23SGarrett Wollman 
6378f5a8818SKevin Lo 	off0 = *offp;
6388f5a8818SKevin Lo 	m = *mp;
6398f5a8818SKevin Lo 	*mp = NULL;
640302ce8d6SAndre Oppermann 	to.to_flags = 0;
64178b50714SRobert Watson 	TCPSTAT_INC(tcps_rcvtotal);
642fb59c426SYoshinobu Inoue 
64304d3a452SHajimu UMEMOTO #ifdef INET6
644b287c6c7SBjoern A. Zeeb 	if (isipv6) {
645fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
64645747ba5SBjoern A. Zeeb 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
647fb59c426SYoshinobu Inoue 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
6489e644c23SMichael Tuexen 		if (port)
6499e644c23SMichael Tuexen 			goto skip6_csum;
650356ab07eSBjoern A. Zeeb 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) {
65145747ba5SBjoern A. Zeeb 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
65245747ba5SBjoern A. Zeeb 				th->th_sum = m->m_pkthdr.csum_data;
65345747ba5SBjoern A. Zeeb 			else
65445747ba5SBjoern A. Zeeb 				th->th_sum = in6_cksum_pseudo(ip6, tlen,
65545747ba5SBjoern A. Zeeb 				    IPPROTO_TCP, m->m_pkthdr.csum_data);
65645747ba5SBjoern A. Zeeb 			th->th_sum ^= 0xffff;
65745747ba5SBjoern A. Zeeb 		} else
65845747ba5SBjoern A. Zeeb 			th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen);
65945747ba5SBjoern A. Zeeb 		if (th->th_sum) {
66078b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
661fb59c426SYoshinobu Inoue 			goto drop;
662fb59c426SYoshinobu Inoue 		}
6639e644c23SMichael Tuexen 	skip6_csum:
66433841545SHajimu UMEMOTO 		/*
66533841545SHajimu UMEMOTO 		 * Be proactive about unspecified IPv6 address in source.
66633841545SHajimu UMEMOTO 		 * As we use all-zero to indicate unbounded/unconnected pcb,
66733841545SHajimu UMEMOTO 		 * unspecified IPv6 address can be used to confuse us.
66833841545SHajimu UMEMOTO 		 *
66933841545SHajimu UMEMOTO 		 * Note that packets with unspecified IPv6 destination is
67033841545SHajimu UMEMOTO 		 * already dropped in ip6_input.
67133841545SHajimu UMEMOTO 		 */
672713264f6SMark Johnston 		KASSERT(!IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst),
673713264f6SMark Johnston 		    ("%s: unspecified destination v6 address", __func__));
67433841545SHajimu UMEMOTO 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
67578e6c3aaSGleb Smirnoff 			IP6STAT_INC(ip6s_badscope); /* XXX */
67633841545SHajimu UMEMOTO 			goto drop;
67733841545SHajimu UMEMOTO 		}
678bb4a7d94SKristof Provost 		iptos = IPV6_TRAFFIC_CLASS(ip6);
679b287c6c7SBjoern A. Zeeb 	}
68004d3a452SHajimu UMEMOTO #endif
681b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
682b287c6c7SBjoern A. Zeeb 	else
683b287c6c7SBjoern A. Zeeb #endif
684b287c6c7SBjoern A. Zeeb #ifdef INET
685b287c6c7SBjoern A. Zeeb 	{
686df8bae1dSRodney W. Grimes 		/*
687df8bae1dSRodney W. Grimes 		 * Get IP and TCP header together in first mbuf.
688df8bae1dSRodney W. Grimes 		 * Note: IP leaves IP header in first mbuf.
689df8bae1dSRodney W. Grimes 		 */
690fb59c426SYoshinobu Inoue 		if (off0 > sizeof (struct ip)) {
691105bd211SGleb Smirnoff 			ip_stripoptions(m);
692fb59c426SYoshinobu Inoue 			off0 = sizeof(struct ip);
693fb59c426SYoshinobu Inoue 		}
694df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof (struct tcpiphdr)) {
6954dfdffe9SAndre Oppermann 			if ((m = m_pullup(m, sizeof (struct tcpiphdr)))
6964dfdffe9SAndre Oppermann 			    == NULL) {
69778b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvshort);
6988f5a8818SKevin Lo 				return (IPPROTO_DONE);
699df8bae1dSRodney W. Grimes 			}
700df8bae1dSRodney W. Grimes 		}
701fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
702db4f9cc7SJonathan Lemon 		th = (struct tcphdr *)((caddr_t)ip + off0);
7038ad458a4SGleb Smirnoff 		tlen = ntohs(ip->ip_len) - off0;
704df8bae1dSRodney W. Grimes 
7051d7ee746SKurt Lidl 		iptos = ip->ip_tos;
7069e644c23SMichael Tuexen 		if (port)
7079e644c23SMichael Tuexen 			goto skip_csum;
708db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
709db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
710db4f9cc7SJonathan Lemon 				th->th_sum = m->m_pkthdr.csum_data;
711db4f9cc7SJonathan Lemon 			else
712db4f9cc7SJonathan Lemon 				th->th_sum = in_pseudo(ip->ip_src.s_addr,
713c068736aSJeffrey Hsu 				    ip->ip_dst.s_addr,
7148f134647SGleb Smirnoff 				    htonl(m->m_pkthdr.csum_data + tlen +
715c068736aSJeffrey Hsu 				    IPPROTO_TCP));
716db4f9cc7SJonathan Lemon 			th->th_sum ^= 0xffff;
717db4f9cc7SJonathan Lemon 		} else {
7188f134647SGleb Smirnoff 			struct ipovly *ipov = (struct ipovly *)ip;
7198f134647SGleb Smirnoff 
720df8bae1dSRodney W. Grimes 			/*
721df8bae1dSRodney W. Grimes 			 * Checksum extended TCP header and data.
722df8bae1dSRodney W. Grimes 			 */
7238f134647SGleb Smirnoff 			len = off0 + tlen;
724c4556b2fSAndrey V. Elsukov 			ipttl = ip->ip_ttl;
725fb59c426SYoshinobu Inoue 			bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
7268f134647SGleb Smirnoff 			ipov->ih_len = htons(tlen);
727fb59c426SYoshinobu Inoue 			th->th_sum = in_cksum(m, len);
72857f60867SMark Johnston 			/* Reset length for SDT probes. */
7291d7ee746SKurt Lidl 			ip->ip_len = htons(len);
7301d7ee746SKurt Lidl 			/* Reset TOS bits */
7311d7ee746SKurt Lidl 			ip->ip_tos = iptos;
7321d7ee746SKurt Lidl 			/* Re-initialization for later version check */
733c4556b2fSAndrey V. Elsukov 			ip->ip_ttl = ipttl;
7341d7ee746SKurt Lidl 			ip->ip_v = IPVERSION;
735b2fe54bcSAndrey V. Elsukov 			ip->ip_hl = off0 >> 2;
736db4f9cc7SJonathan Lemon 		}
7379e644c23SMichael Tuexen 	skip_csum:
7389e644c23SMichael Tuexen 		if (th->th_sum && (port == 0)) {
73978b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
740df8bae1dSRodney W. Grimes 			goto drop;
741df8bae1dSRodney W. Grimes 		}
742713264f6SMark Johnston 		KASSERT(ip->ip_dst.s_addr != INADDR_ANY,
743713264f6SMark Johnston 		    ("%s: unspecified destination v4 address", __func__));
744713264f6SMark Johnston 		if (__predict_false(ip->ip_src.s_addr == INADDR_ANY)) {
74578e6c3aaSGleb Smirnoff 			IPSTAT_INC(ips_badaddr);
746713264f6SMark Johnston 			goto drop;
747713264f6SMark Johnston 		}
748fb59c426SYoshinobu Inoue 	}
749b287c6c7SBjoern A. Zeeb #endif /* INET */
750df8bae1dSRodney W. Grimes 
751df8bae1dSRodney W. Grimes 	/*
752df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
753df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
754df8bae1dSRodney W. Grimes 	 */
755fb59c426SYoshinobu Inoue 	off = th->th_off << 2;
756df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
75778b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvbadoff);
758df8bae1dSRodney W. Grimes 		goto drop;
759df8bae1dSRodney W. Grimes 	}
760fb59c426SYoshinobu Inoue 	tlen -= off;	/* tlen is used instead of ti->ti_len */
761df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
76204d3a452SHajimu UMEMOTO #ifdef INET6
763b287c6c7SBjoern A. Zeeb 		if (isipv6) {
764a4adf6ccSBjoern A. Zeeb 			if (m->m_len < off0 + off) {
7654e619b17SBjoern A. Zeeb 				m = m_pullup(m, off0 + off);
7664e619b17SBjoern A. Zeeb 				if (m == NULL) {
7674e619b17SBjoern A. Zeeb 					TCPSTAT_INC(tcps_rcvshort);
7684e619b17SBjoern A. Zeeb 					return (IPPROTO_DONE);
7694e619b17SBjoern A. Zeeb 				}
770a4adf6ccSBjoern A. Zeeb 			}
771fb59c426SYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
772fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
773b287c6c7SBjoern A. Zeeb 		}
77404d3a452SHajimu UMEMOTO #endif
775b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
776b287c6c7SBjoern A. Zeeb 		else
777b287c6c7SBjoern A. Zeeb #endif
778b287c6c7SBjoern A. Zeeb #ifdef INET
779b287c6c7SBjoern A. Zeeb 		{
780df8bae1dSRodney W. Grimes 			if (m->m_len < sizeof(struct ip) + off) {
781c068736aSJeffrey Hsu 				if ((m = m_pullup(m, sizeof (struct ip) + off))
7824dfdffe9SAndre Oppermann 				    == NULL) {
78378b50714SRobert Watson 					TCPSTAT_INC(tcps_rcvshort);
7848f5a8818SKevin Lo 					return (IPPROTO_DONE);
785df8bae1dSRodney W. Grimes 				}
786fb59c426SYoshinobu Inoue 				ip = mtod(m, struct ip *);
787fb59c426SYoshinobu Inoue 				th = (struct tcphdr *)((caddr_t)ip + off0);
788fb59c426SYoshinobu Inoue 			}
789df8bae1dSRodney W. Grimes 		}
790b287c6c7SBjoern A. Zeeb #endif
791df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
792fb59c426SYoshinobu Inoue 		optp = (u_char *)(th + 1);
793df8bae1dSRodney W. Grimes 	}
7941ebf4607SRichard Scheffenegger 	thflags = tcp_get_flags(th);
795df8bae1dSRodney W. Grimes 
796e46cd3d4SDag-Erling Smørgrav 	/*
797df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
798df8bae1dSRodney W. Grimes 	 */
7992903309aSAttilio Rao 	tcp_fields_to_host(th);
800df8bae1dSRodney W. Grimes 
801df8bae1dSRodney W. Grimes 	/*
802794235b7SAndre Oppermann 	 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options.
803755c1f07SAndras Olah 	 */
804fb59c426SYoshinobu Inoue 	drop_hdrlen = off0 + off;
805755c1f07SAndras Olah 
806755c1f07SAndras Olah 	/*
8078d573cc1SAndre Oppermann 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
8088d573cc1SAndre Oppermann 	 */
809b8056faeSGleb Smirnoff         if (
810b8056faeSGleb Smirnoff #ifdef INET6
811b8056faeSGleb Smirnoff 	    (isipv6 && (m->m_flags & M_IP6_NEXTHOP))
812b8056faeSGleb Smirnoff #ifdef INET
813b8056faeSGleb Smirnoff 	    || (!isipv6 && (m->m_flags & M_IP_NEXTHOP))
814b8056faeSGleb Smirnoff #endif
815b8056faeSGleb Smirnoff #endif
816b8056faeSGleb Smirnoff #if defined(INET) && !defined(INET6)
817b8056faeSGleb Smirnoff 	    (m->m_flags & M_IP_NEXTHOP)
818b8056faeSGleb Smirnoff #endif
819b8056faeSGleb Smirnoff 	    )
8209b932e9eSAndre Oppermann 		fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
8219b932e9eSAndre Oppermann 
82208d9c920SGleb Smirnoff 	/*
8231db08fbeSGleb Smirnoff 	 * For initial SYN packets we don't need write lock on matching
8241db08fbeSGleb Smirnoff 	 * PCB, be it a listening one or a synchronized one.  The packet
8251db08fbeSGleb Smirnoff 	 * shall not modify its state.
82608d9c920SGleb Smirnoff 	 */
827d88eb465SGleb Smirnoff 	lookupflag = INPLOOKUP_WILDCARD |
828d88eb465SGleb Smirnoff 	    ((thflags & (TH_ACK|TH_SYN)) == TH_SYN ?
829d88eb465SGleb Smirnoff 	    INPLOOKUP_RLOCKPCB : INPLOOKUP_WLOCKPCB);
830ce7ad664SEd Maste findpcb:
8318a006adbSBjoern A. Zeeb #ifdef INET6
8328a006adbSBjoern A. Zeeb 	if (isipv6 && fwd_tag != NULL) {
8338a006adbSBjoern A. Zeeb 		struct sockaddr_in6 *next_hop6;
8348a006adbSBjoern A. Zeeb 
8358a006adbSBjoern A. Zeeb 		next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
8368a006adbSBjoern A. Zeeb 		/*
8378a006adbSBjoern A. Zeeb 		 * Transparently forwarded. Pretend to be the destination.
8388a006adbSBjoern A. Zeeb 		 * Already got one like this?
8398a006adbSBjoern A. Zeeb 		 */
8408a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo,
8418a006adbSBjoern A. Zeeb 		    &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport,
842aab8c844SGleb Smirnoff 		    lookupflag & ~INPLOOKUP_WILDCARD, m->m_pkthdr.rcvif, m);
8438a006adbSBjoern A. Zeeb 		if (!inp) {
8448a006adbSBjoern A. Zeeb 			/*
8458a006adbSBjoern A. Zeeb 			 * It's new.  Try to find the ambushing socket.
8468a006adbSBjoern A. Zeeb 			 * Because we've rewritten the destination address,
8478a006adbSBjoern A. Zeeb 			 * any hardware-generated hash is ignored.
8488a006adbSBjoern A. Zeeb 			 */
8498a006adbSBjoern A. Zeeb 			inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src,
8508a006adbSBjoern A. Zeeb 			    th->th_sport, &next_hop6->sin6_addr,
8518a006adbSBjoern A. Zeeb 			    next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) :
852d88eb465SGleb Smirnoff 			    th->th_dport, lookupflag, m->m_pkthdr.rcvif);
8538a006adbSBjoern A. Zeeb 		}
854c1de64a4SAndrey V. Elsukov 	} else if (isipv6) {
8558a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src,
856d88eb465SGleb Smirnoff 		    th->th_sport, &ip6->ip6_dst, th->th_dport, lookupflag,
857d88eb465SGleb Smirnoff 		    m->m_pkthdr.rcvif, m);
8588a006adbSBjoern A. Zeeb 	}
8598a006adbSBjoern A. Zeeb #endif /* INET6 */
8608a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET)
8618a006adbSBjoern A. Zeeb 	else
8628a006adbSBjoern A. Zeeb #endif
8638a006adbSBjoern A. Zeeb #ifdef INET
8648a006adbSBjoern A. Zeeb 	if (fwd_tag != NULL) {
8659b932e9eSAndre Oppermann 		struct sockaddr_in *next_hop;
8669b932e9eSAndre Oppermann 
8679b932e9eSAndre Oppermann 		next_hop = (struct sockaddr_in *)(fwd_tag+1);
868f9e354dfSJulian Elischer 		/*
8692b25acc1SLuigi Rizzo 		 * Transparently forwarded. Pretend to be the destination.
870f9e354dfSJulian Elischer 		 * already got one like this?
871f9e354dfSJulian Elischer 		 */
872d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport,
873aab8c844SGleb Smirnoff 		    ip->ip_dst, th->th_dport, lookupflag & ~INPLOOKUP_WILDCARD,
874aab8c844SGleb Smirnoff 		    m->m_pkthdr.rcvif, m);
875fa046d87SRobert Watson 		if (!inp) {
876fa046d87SRobert Watson 			/*
877fa046d87SRobert Watson 			 * It's new.  Try to find the ambushing socket.
878d3c1f003SRobert Watson 			 * Because we've rewritten the destination address,
879d3c1f003SRobert Watson 			 * any hardware-generated hash is ignored.
880fa046d87SRobert Watson 			 */
881fa046d87SRobert Watson 			inp = in_pcblookup(&V_tcbinfo, ip->ip_src,
882fa046d87SRobert Watson 			    th->th_sport, next_hop->sin_addr,
883fa046d87SRobert Watson 			    next_hop->sin_port ? ntohs(next_hop->sin_port) :
884d88eb465SGleb Smirnoff 			    th->th_dport, lookupflag, m->m_pkthdr.rcvif);
885f9e354dfSJulian Elischer 		}
886b10fbdeaSAndre Oppermann 	} else
887d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src,
888d88eb465SGleb Smirnoff 		    th->th_sport, ip->ip_dst, th->th_dport, lookupflag,
889d88eb465SGleb Smirnoff 		    m->m_pkthdr.rcvif, m);
8908a006adbSBjoern A. Zeeb #endif /* INET */
891f9e354dfSJulian Elischer 
892df8bae1dSRodney W. Grimes 	/*
893574b6964SAndre Oppermann 	 * If the INPCB does not exist then all data in the incoming
894574b6964SAndre Oppermann 	 * segment is discarded and an appropriate RST is sent back.
8958b07e49aSJulian Elischer 	 * XXX MRT Send RST using which routing table?
896df8bae1dSRodney W. Grimes 	 */
897816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
898d88eb465SGleb Smirnoff 		if (rstreason != 0) {
899d88eb465SGleb Smirnoff 			/* We came here after second (safety) lookup. */
900d88eb465SGleb Smirnoff 			MPASS((lookupflag & INPLOOKUP_WILDCARD) == 0);
901d88eb465SGleb Smirnoff 			goto dropwithreset;
902d88eb465SGleb Smirnoff 		}
903574b6964SAndre Oppermann 		/*
904574b6964SAndre Oppermann 		 * Log communication attempts to ports that are not
905574b6964SAndre Oppermann 		 * in use.
906574b6964SAndre Oppermann 		 */
907334fc582SBjoern A. Zeeb 		if ((V_tcp_log_in_vain == 1 && (thflags & TH_SYN)) ||
908334fc582SBjoern A. Zeeb 		    V_tcp_log_in_vain == 2) {
909b7d747ecSAndre Oppermann 			if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6)))
910df541e5fSAndre Oppermann 				log(LOG_INFO, "%s; %s: Connection attempt "
911df541e5fSAndre Oppermann 				    "to closed port\n", s, __func__);
9122e4e1b4cSGeoff Rehmet 		}
913574b6964SAndre Oppermann 		/*
914574b6964SAndre Oppermann 		 * When blackholing do not respond with a RST but
915574b6964SAndre Oppermann 		 * completely ignore the segment and drop it.
916574b6964SAndre Oppermann 		 */
9173ea9a7cfSGleb Smirnoff 		if (((V_blackhole == 1 && (thflags & TH_SYN)) ||
918ad3ad064SGleb Smirnoff 		    V_blackhole == 2) && (V_blackhole_local || (
9193ea9a7cfSGleb Smirnoff #ifdef INET6
9203ea9a7cfSGleb Smirnoff 		    isipv6 ? !in6_localaddr(&ip6->ip6_src) :
9213ea9a7cfSGleb Smirnoff #endif
9223ea9a7cfSGleb Smirnoff #ifdef INET
9233ea9a7cfSGleb Smirnoff 		    !in_localip(ip->ip_src)
9243ea9a7cfSGleb Smirnoff #else
9253ea9a7cfSGleb Smirnoff 		    true
9263ea9a7cfSGleb Smirnoff #endif
927ad3ad064SGleb Smirnoff 		    )))
9281929eae1SAndre Oppermann 			goto dropunlock;
929574b6964SAndre Oppermann 
930a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
931a57815efSBosko Milekic 		goto dropwithreset;
932816a3d83SPoul-Henning Kamp 	}
93308d9c920SGleb Smirnoff 	INP_LOCK_ASSERT(inp);
934f567d55fSGleb Smirnoff 
935c2529042SHans Petter Selasky 	if ((inp->inp_flowtype == M_HASHTYPE_NONE) &&
936f71cb9f7SGleb Smirnoff 	    !SOLISTENING(inp->inp_socket)) {
937e5738ee0SCheng Cui 		if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
93880cb9f21SKip Macy 			inp->inp_flowid = m->m_pkthdr.flowid;
9392f719932SAdrian Chadd 			inp->inp_flowtype = M_HASHTYPE_GET(m);
940e5738ee0SCheng Cui #ifdef	RSS
941e5738ee0SCheng Cui 		} else {
942e5738ee0SCheng Cui 			  /* assign flowid by software RSS hash */
943e5738ee0SCheng Cui #ifdef INET6
944e5738ee0SCheng Cui 			  if (isipv6) {
945e5738ee0SCheng Cui 				rss_proto_software_hash_v6(&inp->in6p_faddr,
946e5738ee0SCheng Cui 							   &inp->in6p_laddr,
947e5738ee0SCheng Cui 							   inp->inp_fport,
948e5738ee0SCheng Cui 							   inp->inp_lport,
949e5738ee0SCheng Cui 							   IPPROTO_TCP,
950e5738ee0SCheng Cui 							   &inp->inp_flowid,
951e5738ee0SCheng Cui 							   &inp->inp_flowtype);
952e5738ee0SCheng Cui 			  } else
953e5738ee0SCheng Cui #endif	/* INET6 */
954e5738ee0SCheng Cui 			  {
955e5738ee0SCheng Cui 				rss_proto_software_hash_v4(inp->inp_faddr,
956e5738ee0SCheng Cui 							   inp->inp_laddr,
957e5738ee0SCheng Cui 							   inp->inp_fport,
958e5738ee0SCheng Cui 							   inp->inp_lport,
959e5738ee0SCheng Cui 							   IPPROTO_TCP,
960e5738ee0SCheng Cui 							   &inp->inp_flowid,
961e5738ee0SCheng Cui 							   &inp->inp_flowtype);
962e5738ee0SCheng Cui 			  }
963e5738ee0SCheng Cui #endif	/* RSS */
964e5738ee0SCheng Cui 		}
96580cb9f21SKip Macy 	}
966fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
967a2589465SKen Smith #ifdef INET6
968fcf59617SAndrey V. Elsukov 	if (isipv6 && IPSEC_ENABLED(ipv6) &&
969fcf59617SAndrey V. Elsukov 	    IPSEC_CHECK_POLICY(ipv6, m, inp) != 0) {
970a2589465SKen Smith 		goto dropunlock;
971a2589465SKen Smith 	}
972fcf59617SAndrey V. Elsukov #ifdef INET
973fcf59617SAndrey V. Elsukov 	else
974fcf59617SAndrey V. Elsukov #endif
975fcf59617SAndrey V. Elsukov #endif /* INET6 */
976fcf59617SAndrey V. Elsukov #ifdef INET
977fcf59617SAndrey V. Elsukov 	if (IPSEC_ENABLED(ipv4) &&
978fcf59617SAndrey V. Elsukov 	    IPSEC_CHECK_POLICY(ipv4, m, inp) != 0) {
979fcf59617SAndrey V. Elsukov 		goto dropunlock;
980fcf59617SAndrey V. Elsukov 	}
981fcf59617SAndrey V. Elsukov #endif /* INET */
982a2589465SKen Smith #endif /* IPSEC */
983a2589465SKen Smith 
9848d573cc1SAndre Oppermann 	/*
9858d573cc1SAndre Oppermann 	 * Check the minimum TTL for socket.
9868d573cc1SAndre Oppermann 	 */
98734f83c52SGeorge V. Neville-Neil 	if (inp->inp_ip_minttl != 0) {
98834f83c52SGeorge V. Neville-Neil #ifdef INET6
9892d8868dbSJonathan T. Looney 		if (isipv6) {
9902d8868dbSJonathan T. Looney 			if (inp->inp_ip_minttl > ip6->ip6_hlim)
991302ce8d6SAndre Oppermann 				goto dropunlock;
9922d8868dbSJonathan T. Looney 		} else
99334f83c52SGeorge V. Neville-Neil #endif
99434f83c52SGeorge V. Neville-Neil 		if (inp->inp_ip_minttl > ip->ip_ttl)
995302ce8d6SAndre Oppermann 			goto dropunlock;
99634f83c52SGeorge V. Neville-Neil 	}
997936cd18dSAndre Oppermann 
9980d744519SGleb Smirnoff 	tp = intotcpcb(inp);
9990d744519SGleb Smirnoff 	switch (tp->t_state) {
10000d744519SGleb Smirnoff 	case TCPS_TIME_WAIT:
1001340c35deSJonathan Lemon 		/*
10020d744519SGleb Smirnoff 		 * A previous connection in TIMEWAIT state is supposed to catch
10030d744519SGleb Smirnoff 		 * stray or duplicate segments arriving late.  If this segment
10040d744519SGleb Smirnoff 		 * was a legitimate new connection attempt, the old INPCB gets
10050d744519SGleb Smirnoff 		 * removed and we can try again to find a listening socket.
1006340c35deSJonathan Lemon 		 */
1007283c76c7SMichael Tuexen 		tcp_dooptions(&to, optp, optlen,
1008283c76c7SMichael Tuexen 		    (thflags & TH_SYN) ? TO_SYN : 0);
10098d573cc1SAndre Oppermann 		/*
10100d744519SGleb Smirnoff 		 * tcp_twcheck unlocks the inp always, and frees the m if fails.
10118d573cc1SAndre Oppermann 		 */
10122104448fSAndre Oppermann 		if (tcp_twcheck(inp, &to, th, m, tlen))
1013340c35deSJonathan Lemon 			goto findpcb;
10148f5a8818SKevin Lo 		return (IPPROTO_DONE);
10150d744519SGleb Smirnoff 	case TCPS_CLOSED:
1016794235b7SAndre Oppermann 		/*
1017794235b7SAndre Oppermann 		 * The TCPCB may no longer exist if the connection is winding
1018794235b7SAndre Oppermann 		 * down or it is in the CLOSED state.  Either way we drop the
1019794235b7SAndre Oppermann 		 * segment and send an appropriate response.
1020794235b7SAndre Oppermann 		 */
1021a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
1022a57815efSBosko Milekic 		goto dropwithreset;
102309f81a46SBosko Milekic 	}
1024df8bae1dSRodney W. Grimes 
10259e644c23SMichael Tuexen 	if ((tp->t_port != port) && (tp->t_state > TCPS_LISTEN)) {
10269e644c23SMichael Tuexen 		rstreason = BANDLIM_RST_CLOSEDPORT;
10279e644c23SMichael Tuexen 		goto dropwithreset;
10289e644c23SMichael Tuexen 	}
10299e644c23SMichael Tuexen 
103009fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
103109fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
103209fe6320SNavdeep Parhar 		tcp_offload_input(tp, m);
103309fe6320SNavdeep Parhar 		m = NULL;	/* consumed by the TOE driver */
103409fe6320SNavdeep Parhar 		goto dropunlock;
103509fe6320SNavdeep Parhar 	}
103609fe6320SNavdeep Parhar #endif
103709fe6320SNavdeep Parhar 
1038c488362eSRobert Watson #ifdef MAC
103930d239bcSRobert Watson 	if (mac_inpcb_check_deliver(inp, m))
1040302ce8d6SAndre Oppermann 		goto dropunlock;
1041c488362eSRobert Watson #endif
1042a557af22SRobert Watson 	so = inp->inp_socket;
1043302ce8d6SAndre Oppermann 	KASSERT(so != NULL, ("%s: so == NULL", __func__));
1044db33b3e6SAndre Oppermann 	/*
1045db33b3e6SAndre Oppermann 	 * When the socket is accepting connections (the INPCB is in LISTEN
1046db33b3e6SAndre Oppermann 	 * state) we look into the SYN cache if this is a new connection
1047a7c7f2a7SJohn Baldwin 	 * attempt or the completion of a previous one.
1048db33b3e6SAndre Oppermann 	 */
1049f4bb1869SMark Johnston 	KASSERT(tp->t_state == TCPS_LISTEN || !SOLISTENING(so),
105045274760SJonathan T. Looney 	    ("%s: so accepting but tp %p not listening", __func__, tp));
1051f4bb1869SMark Johnston 	if (tp->t_state == TCPS_LISTEN && SOLISTENING(so)) {
1052540e8b7eSJeffrey Hsu 		struct in_conninfo inc;
1053540e8b7eSJeffrey Hsu 
10548bfb1918SAndre Oppermann 		bzero(&inc, sizeof(inc));
1055302ce8d6SAndre Oppermann #ifdef INET6
1056be2ac88cSJonathan Lemon 		if (isipv6) {
1057dcdb4371SBjoern A. Zeeb 			inc.inc_flags |= INC_ISIPV6;
10585dff1c38SMichael Tuexen 			if (inp->inp_inc.inc_flags & INC_IPV6MINMTU)
10595dff1c38SMichael Tuexen 				inc.inc_flags |= INC_IPV6MINMTU;
1060be2ac88cSJonathan Lemon 			inc.inc6_faddr = ip6->ip6_src;
1061be2ac88cSJonathan Lemon 			inc.inc6_laddr = ip6->ip6_dst;
1062302ce8d6SAndre Oppermann 		} else
1063302ce8d6SAndre Oppermann #endif
1064302ce8d6SAndre Oppermann 		{
1065be2ac88cSJonathan Lemon 			inc.inc_faddr = ip->ip_src;
1066be2ac88cSJonathan Lemon 			inc.inc_laddr = ip->ip_dst;
1067be2ac88cSJonathan Lemon 		}
1068be2ac88cSJonathan Lemon 		inc.inc_fport = th->th_sport;
1069be2ac88cSJonathan Lemon 		inc.inc_lport = th->th_dport;
10707973fba3SJulian Elischer 		inc.inc_fibnum = so->so_fibnum;
1071be2ac88cSJonathan Lemon 
1072fb59c426SYoshinobu Inoue 		/*
10738d573cc1SAndre Oppermann 		 * Check for an existing connection attempt in syncache if
10748d573cc1SAndre Oppermann 		 * the flag is only ACK.  A successful lookup creates a new
10758d573cc1SAndre Oppermann 		 * socket appended to the listen queue in SYN_RECEIVED state.
1076fb59c426SYoshinobu Inoue 		 */
1077be2ac88cSJonathan Lemon 		if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
1078bf6d304aSAndre Oppermann 			/*
1079bf6d304aSAndre Oppermann 			 * Parse the TCP options here because
1080bf6d304aSAndre Oppermann 			 * syncookies need access to the reflected
1081bf6d304aSAndre Oppermann 			 * timestamp.
1082bf6d304aSAndre Oppermann 			 */
1083bf6d304aSAndre Oppermann 			tcp_dooptions(&to, optp, optlen, 0);
10849fa198beSAndre Oppermann 			/*
1085e7231d07SGleb Smirnoff 			 * NB: syncache_expand() doesn't unlock inp.
10869fa198beSAndre Oppermann 			 */
10879e644c23SMichael Tuexen 			rstreason = syncache_expand(&inc, &to, th, &so, m, port);
1088fcf59617SAndrey V. Elsukov 			if (rstreason < 0) {
1089fcf59617SAndrey V. Elsukov 				/*
1090fcf59617SAndrey V. Elsukov 				 * A failing TCP MD5 signature comparison
1091fcf59617SAndrey V. Elsukov 				 * must result in the segment being dropped
1092fcf59617SAndrey V. Elsukov 				 * and must not produce any response back
1093fcf59617SAndrey V. Elsukov 				 * to the sender.
1094fcf59617SAndrey V. Elsukov 				 */
1095fcf59617SAndrey V. Elsukov 				goto dropunlock;
1096fcf59617SAndrey V. Elsukov 			} else if (rstreason == 0) {
10974195b4afSPaul Traina 				/*
1098d88eb465SGleb Smirnoff 				 * No syncache entry, or ACK was not for our
1099d88eb465SGleb Smirnoff 				 * SYN/ACK.  Do our protection against double
1100d88eb465SGleb Smirnoff 				 * ACK.  If peer sent us 2 ACKs, then for the
1101d88eb465SGleb Smirnoff 				 * first one syncache_expand() successfully
1102d88eb465SGleb Smirnoff 				 * converted syncache entry into a socket,
1103d88eb465SGleb Smirnoff 				 * while we were waiting on the inpcb lock.  We
1104d88eb465SGleb Smirnoff 				 * don't want to sent RST for the second ACK,
1105d88eb465SGleb Smirnoff 				 * so we perform second lookup without wildcard
1106d88eb465SGleb Smirnoff 				 * match, hoping to find the new socket.  If
1107d88eb465SGleb Smirnoff 				 * the ACK is stray indeed, rstreason would
1108d88eb465SGleb Smirnoff 				 * hint the above code that the lookup was a
1109d88eb465SGleb Smirnoff 				 * second attempt.
1110d88eb465SGleb Smirnoff 				 *
11118d573cc1SAndre Oppermann 				 * NB: syncache did its own logging
11128d573cc1SAndre Oppermann 				 * of the failure cause.
11134195b4afSPaul Traina 				 */
1114d88eb465SGleb Smirnoff 				INP_WUNLOCK(inp);
1115be2ac88cSJonathan Lemon 				rstreason = BANDLIM_RST_OPENPORT;
1116d88eb465SGleb Smirnoff 				lookupflag &= ~INPLOOKUP_WILDCARD;
1117d88eb465SGleb Smirnoff 				goto findpcb;
1118be2ac88cSJonathan Lemon 			}
111909c305ebSPatrick Kelsey tfo_socket_result:
1120f76fcf6dSJeffrey Hsu 			if (so == NULL) {
1121be2ac88cSJonathan Lemon 				/*
1122e207f800SAndre Oppermann 				 * We completed the 3-way handshake
1123e207f800SAndre Oppermann 				 * but could not allocate a socket
1124e207f800SAndre Oppermann 				 * either due to memory shortage,
1125e207f800SAndre Oppermann 				 * listen queue length limits or
1126a160e630SAndre Oppermann 				 * global socket limits.  Send RST
1127a160e630SAndre Oppermann 				 * or wait and have the remote end
1128a160e630SAndre Oppermann 				 * retransmit the ACK for another
1129a160e630SAndre Oppermann 				 * try.
1130be2ac88cSJonathan Lemon 				 */
1131a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1132a160e630SAndre Oppermann 					log(LOG_DEBUG, "%s; %s: Listen socket: "
11338d573cc1SAndre Oppermann 					    "Socket allocation failed due to "
11348d573cc1SAndre Oppermann 					    "limits or memory shortage, %s\n",
1135ac957cd2SJulian Elischer 					    s, __func__,
1136ac957cd2SJulian Elischer 					    V_tcp_sc_rst_sock_fail ?
1137ac957cd2SJulian Elischer 					    "sending RST" : "try again");
1138603724d3SBjoern A. Zeeb 				if (V_tcp_sc_rst_sock_fail) {
1139e207f800SAndre Oppermann 					rstreason = BANDLIM_UNLIMITED;
1140e207f800SAndre Oppermann 					goto dropwithreset;
1141a160e630SAndre Oppermann 				} else
1142a160e630SAndre Oppermann 					goto dropunlock;
1143f76fcf6dSJeffrey Hsu 			}
1144be2ac88cSJonathan Lemon 			/*
1145be2ac88cSJonathan Lemon 			 * Socket is created in state SYN_RECEIVED.
11468d573cc1SAndre Oppermann 			 * Unlock the listen socket, lock the newly
11478d573cc1SAndre Oppermann 			 * created socket and update the tp variable.
114808d9c920SGleb Smirnoff 			 * If we came here via jump to tfo_socket_result,
114908d9c920SGleb Smirnoff 			 * then listening socket is read-locked.
1150be2ac88cSJonathan Lemon 			 */
115108d9c920SGleb Smirnoff 			INP_UNLOCK(inp);	/* listen socket */
1152be2ac88cSJonathan Lemon 			inp = sotoinpcb(so);
1153ff9b006dSJulien Charbon 			/*
1154ff9b006dSJulien Charbon 			 * New connection inpcb is already locked by
1155ff9b006dSJulien Charbon 			 * syncache_expand().
1156ff9b006dSJulien Charbon 			 */
1157ff9b006dSJulien Charbon 			INP_WLOCK_ASSERT(inp);
1158be2ac88cSJonathan Lemon 			tp = intotcpcb(inp);
11598d573cc1SAndre Oppermann 			KASSERT(tp->t_state == TCPS_SYN_RECEIVED,
11608d573cc1SAndre Oppermann 			    ("%s: ", __func__));
1161be2ac88cSJonathan Lemon 			/*
1162302ce8d6SAndre Oppermann 			 * Process the segment and the data it
1163302ce8d6SAndre Oppermann 			 * contains.  tcp_do_segment() consumes
1164302ce8d6SAndre Oppermann 			 * the mbuf chain and unlocks the inpcb.
1165302ce8d6SAndre Oppermann 			 */
11666138da62SMichael Tuexen 			TCP_PROBE5(receive, NULL, tp, m, tp, th);
116735bc0bccSGleb Smirnoff 			tp->t_fb->tfb_tcp_do_segment(tp, m, th, drop_hdrlen,
116835bc0bccSGleb Smirnoff 			    tlen, iptos);
11698f5a8818SKevin Lo 			return (IPPROTO_DONE);
1170be2ac88cSJonathan Lemon 		}
1171a160e630SAndre Oppermann 		/*
11728d573cc1SAndre Oppermann 		 * Segment flag validation for new connection attempts:
11738d573cc1SAndre Oppermann 		 *
1174a160e630SAndre Oppermann 		 * Our (SYN|ACK) response was rejected.
1175a160e630SAndre Oppermann 		 * Check with syncache and remove entry to prevent
1176a160e630SAndre Oppermann 		 * retransmits.
117719bc77c5SAndre Oppermann 		 *
117819bc77c5SAndre Oppermann 		 * NB: syncache_chkrst does its own logging of failure
117919bc77c5SAndre Oppermann 		 * causes.
1180a160e630SAndre Oppermann 		 */
1181a160e630SAndre Oppermann 		if (thflags & TH_RST) {
11829e644c23SMichael Tuexen 			syncache_chkrst(&inc, th, m, port);
1183a160e630SAndre Oppermann 			goto dropunlock;
1184a160e630SAndre Oppermann 		}
1185a160e630SAndre Oppermann 		/*
1186a160e630SAndre Oppermann 		 * We can't do anything without SYN.
1187a160e630SAndre Oppermann 		 */
1188a160e630SAndre Oppermann 		if ((thflags & TH_SYN) == 0) {
1189a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1190a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1191773673c1SAndre Oppermann 				    "SYN is missing, segment ignored\n",
11928d573cc1SAndre Oppermann 				    s, __func__);
119378b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1194a160e630SAndre Oppermann 			goto dropunlock;
1195a160e630SAndre Oppermann 		}
1196a160e630SAndre Oppermann 		/*
1197a160e630SAndre Oppermann 		 * (SYN|ACK) is bogus on a listen socket.
1198a160e630SAndre Oppermann 		 */
1199fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1200a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1201a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
12028d573cc1SAndre Oppermann 				    "SYN|ACK invalid, segment rejected\n",
12038d573cc1SAndre Oppermann 				    s, __func__);
12049e644c23SMichael Tuexen 			syncache_badack(&inc, port);	/* XXX: Not needed! */
120578b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1206a57815efSBosko Milekic 			rstreason = BANDLIM_RST_OPENPORT;
1207a57815efSBosko Milekic 			goto dropwithreset;
12084195b4afSPaul Traina 		}
1209a160e630SAndre Oppermann 		/*
1210a160e630SAndre Oppermann 		 * If the drop_synfin option is enabled, drop all
1211a160e630SAndre Oppermann 		 * segments with both the SYN and FIN bits set.
1212a160e630SAndre Oppermann 		 * This prevents e.g. nmap from identifying the
1213a160e630SAndre Oppermann 		 * TCP/IP stack.
1214a160e630SAndre Oppermann 		 * XXX: Poor reasoning.  nmap has other methods
1215a160e630SAndre Oppermann 		 * and is constantly refining its stack detection
1216a160e630SAndre Oppermann 		 * strategies.
12178d573cc1SAndre Oppermann 		 * XXX: This is a violation of the TCP specification
1218a160e630SAndre Oppermann 		 * and was used by RFC1644.
1219a160e630SAndre Oppermann 		 */
1220603724d3SBjoern A. Zeeb 		if ((thflags & TH_FIN) && V_drop_synfin) {
1221a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1222a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1223773673c1SAndre Oppermann 				    "SYN|FIN segment ignored (based on "
12248d573cc1SAndre Oppermann 				    "sysctl setting)\n", s, __func__);
122578b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1226302ce8d6SAndre Oppermann 			goto dropunlock;
1227ebb0cbeaSPaul Traina 		}
1228be2ac88cSJonathan Lemon 		/*
1229be2ac88cSJonathan Lemon 		 * Segment's flags are (SYN) or (SYN|FIN).
1230a160e630SAndre Oppermann 		 *
1231a160e630SAndre Oppermann 		 * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored
1232a160e630SAndre Oppermann 		 * as they do not affect the state of the TCP FSM.
1233a160e630SAndre Oppermann 		 * The data pointed to by TH_URG and th_urp is ignored.
1234be2ac88cSJonathan Lemon 		 */
1235a160e630SAndre Oppermann 		KASSERT((thflags & (TH_RST|TH_ACK)) == 0,
1236a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_RST or TH_ACK set", __func__));
1237a160e630SAndre Oppermann 		KASSERT(thflags & (TH_SYN),
1238a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_SYN not set", __func__));
123908d9c920SGleb Smirnoff 		INP_RLOCK_ASSERT(inp);
124033841545SHajimu UMEMOTO #ifdef INET6
124133841545SHajimu UMEMOTO 		/*
124233841545SHajimu UMEMOTO 		 * If deprecated address is forbidden,
124333841545SHajimu UMEMOTO 		 * we do not accept SYN to deprecated interface
124433841545SHajimu UMEMOTO 		 * address to prevent any new inbound connection from
124533841545SHajimu UMEMOTO 		 * getting established.
124633841545SHajimu UMEMOTO 		 * When we do not accept SYN, we send a TCP RST,
124733841545SHajimu UMEMOTO 		 * with deprecated source address (instead of dropping
124833841545SHajimu UMEMOTO 		 * it).  We compromise it as it is much better for peer
124933841545SHajimu UMEMOTO 		 * to send a RST, and RST will be the final packet
125033841545SHajimu UMEMOTO 		 * for the exchange.
125133841545SHajimu UMEMOTO 		 *
125233841545SHajimu UMEMOTO 		 * If we do not forbid deprecated addresses, we accept
125333841545SHajimu UMEMOTO 		 * the SYN packet.  RFC2462 does not suggest dropping
125433841545SHajimu UMEMOTO 		 * SYN in this case.
125533841545SHajimu UMEMOTO 		 * If we decipher RFC2462 5.5.4, it says like this:
125633841545SHajimu UMEMOTO 		 * 1. use of deprecated addr with existing
125733841545SHajimu UMEMOTO 		 *    communication is okay - "SHOULD continue to be
125833841545SHajimu UMEMOTO 		 *    used"
125933841545SHajimu UMEMOTO 		 * 2. use of it with new communication:
126033841545SHajimu UMEMOTO 		 *   (2a) "SHOULD NOT be used if alternate address
126133841545SHajimu UMEMOTO 		 *        with sufficient scope is available"
126233841545SHajimu UMEMOTO 		 *   (2b) nothing mentioned otherwise.
126333841545SHajimu UMEMOTO 		 * Here we fall into (2b) case as we have no choice in
126433841545SHajimu UMEMOTO 		 * our source address selection - we must obey the peer.
126533841545SHajimu UMEMOTO 		 *
126633841545SHajimu UMEMOTO 		 * The wording in RFC2462 is confusing, and there are
126733841545SHajimu UMEMOTO 		 * multiple description text for deprecated address
126833841545SHajimu UMEMOTO 		 * handling - worse, they are not exactly the same.
126933841545SHajimu UMEMOTO 		 * I believe 5.5.4 is the best one, so we follow 5.5.4.
127033841545SHajimu UMEMOTO 		 */
1271603724d3SBjoern A. Zeeb 		if (isipv6 && !V_ip6_use_deprecated) {
127233841545SHajimu UMEMOTO 			struct in6_ifaddr *ia6;
127333841545SHajimu UMEMOTO 
12748268d82cSAlexander V. Chernikov 			ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false);
12758c0fec80SRobert Watson 			if (ia6 != NULL &&
127633841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
1277a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1278a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12798d573cc1SAndre Oppermann 					"Connection attempt to deprecated "
12808d573cc1SAndre Oppermann 					"IPv6 address rejected\n",
1281a160e630SAndre Oppermann 					s, __func__);
128233841545SHajimu UMEMOTO 				rstreason = BANDLIM_RST_OPENPORT;
128333841545SHajimu UMEMOTO 				goto dropwithreset;
128433841545SHajimu UMEMOTO 			}
128533841545SHajimu UMEMOTO 		}
1286b287c6c7SBjoern A. Zeeb #endif /* INET6 */
128765f28919SJesper Skriver 		/*
1288db33b3e6SAndre Oppermann 		 * Basic sanity checks on incoming SYN requests:
12898d573cc1SAndre Oppermann 		 *   Don't respond if the destination is a link layer
1290db33b3e6SAndre Oppermann 		 *	broadcast according to RFC1122 4.2.3.10, p. 104.
12918d573cc1SAndre Oppermann 		 *   If it is from this socket it must be forged.
12928d573cc1SAndre Oppermann 		 *   Don't respond if the source or destination is a
12938d573cc1SAndre Oppermann 		 *	global or subnet broad- or multicast address.
129493ec91baSCrist J. Clark 		 *   Note that it is quite possible to receive unicast
129593ec91baSCrist J. Clark 		 *	link-layer packets with a broadcast IP address. Use
129693ec91baSCrist J. Clark 		 *	in_broadcast() to find them.
1297be2ac88cSJonathan Lemon 		 */
12988d573cc1SAndre Oppermann 		if (m->m_flags & (M_BCAST|M_MCAST)) {
12998d573cc1SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
13008d573cc1SAndre Oppermann 			    log(LOG_DEBUG, "%s; %s: Listen socket: "
13018d573cc1SAndre Oppermann 				"Connection attempt from broad- or multicast "
1302773673c1SAndre Oppermann 				"link layer address ignored\n", s, __func__);
1303302ce8d6SAndre Oppermann 			goto dropunlock;
13048d573cc1SAndre Oppermann 		}
1305db33b3e6SAndre Oppermann #ifdef INET6
1306b287c6c7SBjoern A. Zeeb 		if (isipv6) {
1307db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1308a160e630SAndre Oppermann 			    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) {
1309a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1310a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13118d573cc1SAndre Oppermann 					"Connection attempt to/from self "
1312773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1313302ce8d6SAndre Oppermann 				goto dropunlock;
1314a160e630SAndre Oppermann 			}
1315be2ac88cSJonathan Lemon 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1316a160e630SAndre Oppermann 			    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
1317a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1318a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13198d573cc1SAndre Oppermann 					"Connection attempt from/to multicast "
1320773673c1SAndre Oppermann 					"address ignored\n", s, __func__);
1321302ce8d6SAndre Oppermann 				goto dropunlock;
1322a160e630SAndre Oppermann 			}
1323b287c6c7SBjoern A. Zeeb 		}
1324db33b3e6SAndre Oppermann #endif
1325b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
1326b287c6c7SBjoern A. Zeeb 		else
1327b287c6c7SBjoern A. Zeeb #endif
1328b287c6c7SBjoern A. Zeeb #ifdef INET
1329b287c6c7SBjoern A. Zeeb 		{
1330db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1331a160e630SAndre Oppermann 			    ip->ip_dst.s_addr == ip->ip_src.s_addr) {
1332a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1333a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13348d573cc1SAndre Oppermann 					"Connection attempt from/to self "
1335773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1336302ce8d6SAndre Oppermann 				goto dropunlock;
1337a160e630SAndre Oppermann 			}
1338be2ac88cSJonathan Lemon 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
1339be2ac88cSJonathan Lemon 			    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
13402ca2159fSCrist J. Clark 			    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
1341a160e630SAndre Oppermann 			    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
1342a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1343a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13448d573cc1SAndre Oppermann 					"Connection attempt from/to broad- "
1345773673c1SAndre Oppermann 					"or multicast address ignored\n",
1346a160e630SAndre Oppermann 					s, __func__);
1347302ce8d6SAndre Oppermann 				goto dropunlock;
1348c068736aSJeffrey Hsu 			}
1349a160e630SAndre Oppermann 		}
1350b287c6c7SBjoern A. Zeeb #endif
1351be2ac88cSJonathan Lemon 		/*
1352db33b3e6SAndre Oppermann 		 * SYN appears to be valid.  Create compressed TCP state
1353db33b3e6SAndre Oppermann 		 * for syncache.
1354be2ac88cSJonathan Lemon 		 */
13552b9c9984SGeorge V. Neville-Neil 		TCP_PROBE3(debug__input, tp, th, m);
1356f72167f4SAndre Oppermann 		tcp_dooptions(&to, optp, optlen, TO_SYN);
13578d5719aaSGleb Smirnoff 		if ((so = syncache_add(&inc, &to, th, inp, so, m, NULL, NULL,
13589e644c23SMichael Tuexen 		    iptos, port)) != NULL)
135909c305ebSPatrick Kelsey 			goto tfo_socket_result;
136018a75309SPatrick Kelsey 
1361be2ac88cSJonathan Lemon 		/*
13624d6e7130SAndre Oppermann 		 * Entry added to syncache and mbuf consumed.
1363a7c7f2a7SJohn Baldwin 		 * Only the listen socket is unlocked by syncache_add().
1364be2ac88cSJonathan Lemon 		 */
13658f5a8818SKevin Lo 		return (IPPROTO_DONE);
1366982c1675SAndre Oppermann 	} else if (tp->t_state == TCPS_LISTEN) {
1367982c1675SAndre Oppermann 		/*
1368982c1675SAndre Oppermann 		 * When a listen socket is torn down the SO_ACCEPTCONN
1369982c1675SAndre Oppermann 		 * flag is removed first while connections are drained
1370982c1675SAndre Oppermann 		 * from the accept queue in a unlock/lock cycle of the
1371982c1675SAndre Oppermann 		 * ACCEPT_LOCK, opening a race condition allowing a SYN
1372982c1675SAndre Oppermann 		 * attempt go through unhandled.
1373982c1675SAndre Oppermann 		 */
1374982c1675SAndre Oppermann 		goto dropunlock;
1375f76fcf6dSJeffrey Hsu 	}
1376fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1377fcf59617SAndrey V. Elsukov 	if (tp->t_flags & TF_SIGNATURE) {
1378fcf59617SAndrey V. Elsukov 		tcp_dooptions(&to, optp, optlen, thflags);
1379fcf59617SAndrey V. Elsukov 		if ((to.to_flags & TOF_SIGNATURE) == 0) {
1380fcf59617SAndrey V. Elsukov 			TCPSTAT_INC(tcps_sig_err_nosigopt);
13812903309aSAttilio Rao 			goto dropunlock;
13822903309aSAttilio Rao 		}
1383fcf59617SAndrey V. Elsukov 		if (!TCPMD5_ENABLED() ||
1384fcf59617SAndrey V. Elsukov 		    TCPMD5_INPUT(m, th, to.to_signature) != 0)
1385fcf59617SAndrey V. Elsukov 			goto dropunlock;
13862903309aSAttilio Rao 	}
13872903309aSAttilio Rao #endif
13882b9c9984SGeorge V. Neville-Neil 	TCP_PROBE5(receive, NULL, tp, m, tp, th);
138957f60867SMark Johnston 
1390302ce8d6SAndre Oppermann 	/*
13918d573cc1SAndre Oppermann 	 * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later
13928d573cc1SAndre Oppermann 	 * state.  tcp_do_segment() always consumes the mbuf chain, unlocks
13938d573cc1SAndre Oppermann 	 * the inpcb, and unlocks pcbinfo.
13941db08fbeSGleb Smirnoff 	 *
13951db08fbeSGleb Smirnoff 	 * XXXGL: in case of a pure SYN arriving on existing connection
13961db08fbeSGleb Smirnoff 	 * TCP stacks won't need to modify the PCB, they would either drop
13971db08fbeSGleb Smirnoff 	 * the segment silently, or send a challenge ACK.  However, we try
13981db08fbeSGleb Smirnoff 	 * to upgrade the lock, because calling convention for stacks is
13991db08fbeSGleb Smirnoff 	 * write-lock on PCB.  If upgrade fails, drop the SYN.
1400302ce8d6SAndre Oppermann 	 */
1401d88eb465SGleb Smirnoff 	if ((lookupflag & INPLOOKUP_RLOCKPCB) && INP_TRY_UPGRADE(inp) == 0)
14021db08fbeSGleb Smirnoff 		goto dropunlock;
14031db08fbeSGleb Smirnoff 
140435bc0bccSGleb Smirnoff 	tp->t_fb->tfb_tcp_do_segment(tp, m, th, drop_hdrlen, tlen, iptos);
14058f5a8818SKevin Lo 	return (IPPROTO_DONE);
1406be2ac88cSJonathan Lemon 
1407302ce8d6SAndre Oppermann dropwithreset:
14082b9c9984SGeorge V. Neville-Neil 	TCP_PROBE5(receive, NULL, tp, m, tp, th);
140957f60867SMark Johnston 
1410014ea782SRobert Watson 	if (inp != NULL) {
1411302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
141208d9c920SGleb Smirnoff 		INP_UNLOCK(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 
14229fa198beSAndre Oppermann 	if (inp != NULL)
142308d9c920SGleb Smirnoff 		INP_UNLOCK(inp);
1424014ea782SRobert Watson 
1425302ce8d6SAndre Oppermann drop:
1426a160e630SAndre Oppermann 	if (s != NULL)
1427a160e630SAndre Oppermann 		free(s, M_TCPLOG);
1428302ce8d6SAndre Oppermann 	if (m != NULL)
1429302ce8d6SAndre Oppermann 		m_freem(m);
14308f5a8818SKevin Lo 	return (IPPROTO_DONE);
1431302ce8d6SAndre Oppermann }
1432302ce8d6SAndre Oppermann 
1433e44c1887SSteven Hartland /*
1434e44c1887SSteven Hartland  * Automatic sizing of receive socket buffer.  Often the send
1435e44c1887SSteven Hartland  * buffer size is not optimally adjusted to the actual network
1436e44c1887SSteven Hartland  * conditions at hand (delay bandwidth product).  Setting the
1437e44c1887SSteven Hartland  * buffer size too small limits throughput on links with high
1438e44c1887SSteven Hartland  * bandwidth and high delay (eg. trans-continental/oceanic links).
1439e44c1887SSteven Hartland  *
1440e44c1887SSteven Hartland  * On the receive side the socket buffer memory is only rarely
1441e44c1887SSteven Hartland  * used to any significant extent.  This allows us to be much
1442e44c1887SSteven Hartland  * more aggressive in scaling the receive socket buffer.  For
1443e44c1887SSteven Hartland  * the case that the buffer space is actually used to a large
1444e44c1887SSteven Hartland  * extent and we run out of kernel memory we can simply drop
1445e44c1887SSteven Hartland  * the new segments; TCP on the sender will just retransmit it
1446e44c1887SSteven Hartland  * later.  Setting the buffer size too big may only consume too
1447e44c1887SSteven Hartland  * much kernel memory if the application doesn't read() from
1448e44c1887SSteven Hartland  * the socket or packet loss or reordering makes use of the
1449e44c1887SSteven Hartland  * reassembly queue.
1450e44c1887SSteven Hartland  *
1451e44c1887SSteven Hartland  * The criteria to step up the receive buffer one notch are:
1452e44c1887SSteven Hartland  *  1. Application has not set receive buffer size with
1453e44c1887SSteven Hartland  *     SO_RCVBUF. Setting SO_RCVBUF clears SB_AUTOSIZE.
1454560c0586SMichael Tuexen  *  2. the number of bytes received during 1/2 of an sRTT
1455560c0586SMichael Tuexen  *     is at least 3/8 of the current socket buffer size.
1456560c0586SMichael Tuexen  *  3. receive buffer size has not hit maximal automatic size;
1457e44c1887SSteven Hartland  *
1458560c0586SMichael Tuexen  * If all of the criteria are met we increaset the socket buffer
1459560c0586SMichael Tuexen  * by a 1/2 (bounded by the max). This allows us to keep ahead
1460560c0586SMichael Tuexen  * of slow-start but also makes it so our peer never gets limited
1461560c0586SMichael Tuexen  * by our rwnd which we then open up causing a burst.
1462560c0586SMichael Tuexen  *
1463560c0586SMichael Tuexen  * This algorithm does two steps per RTT at most and only if
1464e44c1887SSteven Hartland  * we receive a bulk stream w/o packet losses or reorderings.
1465e44c1887SSteven Hartland  * Shrinking the buffer during idle times is not necessary as
1466e44c1887SSteven Hartland  * it doesn't consume any memory when idle.
1467e44c1887SSteven Hartland  *
1468e44c1887SSteven Hartland  * TODO: Only step up if the application is actually serving
1469e44c1887SSteven Hartland  * the buffer to better manage the socket buffer resources.
1470e44c1887SSteven Hartland  */
1471e44c1887SSteven Hartland int
1472e44c1887SSteven Hartland tcp_autorcvbuf(struct mbuf *m, struct tcphdr *th, struct socket *so,
1473e44c1887SSteven Hartland     struct tcpcb *tp, int tlen)
1474e44c1887SSteven Hartland {
1475e44c1887SSteven Hartland 	int newsize = 0;
1476e44c1887SSteven Hartland 
1477e44c1887SSteven Hartland 	if (V_tcp_do_autorcvbuf && (so->so_rcv.sb_flags & SB_AUTOSIZE) &&
1478e44c1887SSteven Hartland 	    tp->t_srtt != 0 && tp->rfbuf_ts != 0 &&
1479e44c1887SSteven Hartland 	    TCP_TS_TO_TICKS(tcp_ts_getticks() - tp->rfbuf_ts) >
1480560c0586SMichael Tuexen 	    ((tp->t_srtt >> TCP_RTT_SHIFT)/2)) {
1481560c0586SMichael Tuexen 		if (tp->rfbuf_cnt > ((so->so_rcv.sb_hiwat / 2)/ 4 * 3) &&
1482e44c1887SSteven Hartland 		    so->so_rcv.sb_hiwat < V_tcp_autorcvbuf_max) {
1483560c0586SMichael Tuexen 			newsize = min((so->so_rcv.sb_hiwat + (so->so_rcv.sb_hiwat/2)), V_tcp_autorcvbuf_max);
1484e44c1887SSteven Hartland 		}
1485e44c1887SSteven Hartland 		TCP_PROBE6(receive__autoresize, NULL, tp, m, tp, th, newsize);
1486e44c1887SSteven Hartland 
1487e44c1887SSteven Hartland 		/* Start over with next RTT. */
1488e44c1887SSteven Hartland 		tp->rfbuf_ts = 0;
1489e44c1887SSteven Hartland 		tp->rfbuf_cnt = 0;
1490e44c1887SSteven Hartland 	} else {
1491e44c1887SSteven Hartland 		tp->rfbuf_cnt += tlen;	/* add up */
1492e44c1887SSteven Hartland 	}
1493e44c1887SSteven Hartland 	return (newsize);
1494e44c1887SSteven Hartland }
1495e44c1887SSteven Hartland 
14969e644c23SMichael Tuexen int
14979e644c23SMichael Tuexen tcp_input(struct mbuf **mp, int *offp, int proto)
14989e644c23SMichael Tuexen {
14999e644c23SMichael Tuexen 	return(tcp_input_with_port(mp, offp, proto, 0));
15009e644c23SMichael Tuexen }
15019e644c23SMichael Tuexen 
1502c348e880SGleb Smirnoff static void
1503c348e880SGleb Smirnoff tcp_handle_wakeup(struct tcpcb *tp)
15044d0770f1SRichard Scheffenegger {
1505c348e880SGleb Smirnoff 
15069eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
1507c348e880SGleb Smirnoff 
15084d0770f1SRichard Scheffenegger 	if (tp->t_flags & TF_WAKESOR) {
15099eb0e832SGleb Smirnoff 		struct socket *so = tptosocket(tp);
1510c348e880SGleb Smirnoff 
15114d0770f1SRichard Scheffenegger 		tp->t_flags &= ~TF_WAKESOR;
1512032bf749SRichard Scheffenegger 		SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1513032bf749SRichard Scheffenegger 		sorwakeup_locked(so);
15144d0770f1SRichard Scheffenegger 	}
15154d0770f1SRichard Scheffenegger }
15164d0770f1SRichard Scheffenegger 
15174d0770f1SRichard Scheffenegger void
151835bc0bccSGleb Smirnoff tcp_do_segment(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th,
151935bc0bccSGleb Smirnoff     int drop_hdrlen, int tlen, uint8_t iptos)
1520302ce8d6SAndre Oppermann {
1521f7220c48SRichard Scheffenegger 	uint16_t thflags;
1522*49a6fbe3SRichard Scheffenegger 	int acked, ourfinisacked, needoutput = 0;
1523*49a6fbe3SRichard Scheffenegger 	sackstatus_t sack_changed;
1524b2ade6b1SRichard Scheffenegger 	int rstreason, todrop, win, incforsyn = 0;
15253ac12506SJonathan T. Looney 	uint32_t tiwin;
15264b7b743cSLawrence Stewart 	uint16_t nsegs;
152707dacf03SAndre Oppermann 	char *s;
15289eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
152935bc0bccSGleb Smirnoff 	struct socket *so = tptosocket(tp);
15309eb0e832SGleb Smirnoff 	struct in_conninfo *inc = &inp->inp_inc;
1531c11a15bfSGleb Smirnoff 	struct mbuf *mfree;
1532302ce8d6SAndre Oppermann 	struct tcpopt to;
1533281a0fd4SPatrick Kelsey 	int tfo_syn;
15343c40e1d5SRichard Scheffenegger 	u_int maxseg;
1535302ce8d6SAndre Oppermann 
15361ebf4607SRichard Scheffenegger 	thflags = tcp_get_flags(th);
1537d64a46eaSLawrence Stewart 	tp->sackhint.last_sack_ack = 0;
1538*49a6fbe3SRichard Scheffenegger 	sack_changed = SACK_NOCHANGE;
15394b7b743cSLawrence Stewart 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
1540d40c0d47SGleb Smirnoff 
1541d40c0d47SGleb Smirnoff 	NET_EPOCH_ASSERT();
15429eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
1543679d9708SAndre Oppermann 	KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
1544679d9708SAndre Oppermann 	    __func__));
1545679d9708SAndre Oppermann 	KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
1546679d9708SAndre Oppermann 	    __func__));
1547df8bae1dSRodney W. Grimes 
154886a996e6SHiren Panchasara #ifdef TCPPCAP
154986a996e6SHiren Panchasara 	/* Save segment, if requested. */
155086a996e6SHiren Panchasara 	tcp_pcap_add(th, m, &(tp->t_inpkts));
155186a996e6SHiren Panchasara #endif
15522529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0,
15532529f56eSJonathan T. Looney 	    tlen, NULL, true);
155486a996e6SHiren Panchasara 
1555013f4df6SMichael Tuexen 	if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
1556013f4df6SMichael Tuexen 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
1557013f4df6SMichael Tuexen 			log(LOG_DEBUG, "%s; %s: "
1558013f4df6SMichael Tuexen 			    "SYN|FIN segment ignored (based on "
1559013f4df6SMichael Tuexen 			    "sysctl setting)\n", s, __func__);
1560013f4df6SMichael Tuexen 			free(s, M_TCPLOG);
1561013f4df6SMichael Tuexen 		}
1562013f4df6SMichael Tuexen 		goto drop;
1563013f4df6SMichael Tuexen 	}
1564013f4df6SMichael Tuexen 
1565df8bae1dSRodney W. Grimes 	/*
1566ebfd7534SMichael Tuexen 	 * If a segment with the ACK-bit set arrives in the SYN-SENT state
1567ebfd7534SMichael Tuexen 	 * check SEQ.ACK first.
1568ebfd7534SMichael Tuexen 	 */
1569ebfd7534SMichael Tuexen 	if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) &&
1570ebfd7534SMichael Tuexen 	    (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) {
1571ebfd7534SMichael Tuexen 		rstreason = BANDLIM_UNLIMITED;
1572d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
1573ebfd7534SMichael Tuexen 		goto dropwithreset;
1574ebfd7534SMichael Tuexen 	}
1575ebfd7534SMichael Tuexen 
1576ebfd7534SMichael Tuexen 	/*
1577df8bae1dSRodney W. Grimes 	 * Segment received on connection.
1578df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
1579e8949f74SAndre Oppermann 	 * XXX: This should be done after segment
1580e8949f74SAndre Oppermann 	 * validation to ignore broken/spoofed segs.
1581df8bae1dSRodney W. Grimes 	 */
1582cd84e78fSRandall Stewart 	if  (tp->t_idle_reduce &&
1583cd84e78fSRandall Stewart 	     (tp->snd_max == tp->snd_una) &&
1584cd84e78fSRandall Stewart 	     ((ticks - tp->t_rcvtime) >= tp->t_rxtcur))
1585cd84e78fSRandall Stewart 		cc_after_idle(tp);
15869b8b58e0SJonathan Lemon 	tp->t_rcvtime = ticks;
1587df8bae1dSRodney W. Grimes 
1588d1b07f36SRandall Stewart 	if (thflags & TH_FIN)
1589d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_FIN);
1590df8bae1dSRodney W. Grimes 	/*
1591c52102ddSHiren Panchasara 	 * Scale up the window into a 32-bit value.
1592e8949f74SAndre Oppermann 	 * For the SYN_SENT state the scale is zero.
1593464fcfbcSAndre Oppermann 	 */
1594464fcfbcSAndre Oppermann 	tiwin = th->th_win << tp->snd_scale;
1595adc56f5aSEdward Tomasz Napierala #ifdef STATS
1596adc56f5aSEdward Tomasz Napierala 	stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin);
1597adc56f5aSEdward Tomasz Napierala #endif
1598464fcfbcSAndre Oppermann 
1599464fcfbcSAndre Oppermann 	/*
1600f2512ba1SRui Paulo 	 * TCP ECN processing.
1601f2512ba1SRui Paulo 	 */
1602b1258b76SRichard Scheffenegger 	if (tcp_ecn_input_segment(tp, thflags, tlen,
1603b1258b76SRichard Scheffenegger 	    tcp_packets_this_ack(tp, th->th_ack),
1604b1258b76SRichard Scheffenegger 	    iptos))
1605dbc42409SLawrence Stewart 		cc_cong_signal(tp, th, CC_ECN);
1606f2512ba1SRui Paulo 
1607f2512ba1SRui Paulo 	/*
1608f72167f4SAndre Oppermann 	 * Parse options on any incoming segment.
1609f72167f4SAndre Oppermann 	 */
1610302ce8d6SAndre Oppermann 	tcp_dooptions(&to, (u_char *)(th + 1),
1611302ce8d6SAndre Oppermann 	    (th->th_off << 2) - sizeof(struct tcphdr),
1612302ce8d6SAndre Oppermann 	    (thflags & TH_SYN) ? TO_SYN : 0);
1613f72167f4SAndre Oppermann 
1614fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1615fcf59617SAndrey V. Elsukov 	if ((tp->t_flags & TF_SIGNATURE) != 0 &&
1616fcf59617SAndrey V. Elsukov 	    (to.to_flags & TOF_SIGNATURE) == 0) {
1617fcf59617SAndrey V. Elsukov 		TCPSTAT_INC(tcps_sig_err_sigopt);
1618fcf59617SAndrey V. Elsukov 		/* XXX: should drop? */
1619fcf59617SAndrey V. Elsukov 	}
1620fcf59617SAndrey V. Elsukov #endif
1621f72167f4SAndre Oppermann 	/*
1622f72167f4SAndre Oppermann 	 * If echoed timestamp is later than the current time,
1623bf6d304aSAndre Oppermann 	 * fall back to non RFC1323 RTT calculation.  Normalize
1624bf6d304aSAndre Oppermann 	 * timestamp if syncookies were used when this connection
1625bf6d304aSAndre Oppermann 	 * was established.
1626f72167f4SAndre Oppermann 	 */
1627bf6d304aSAndre Oppermann 	if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
1628e16fa5caSJohn-Mark Gurney 		to.to_tsecr -= tp->ts_offset;
1629d8951c8aSBjoern A. Zeeb 		if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks()))
1630f72167f4SAndre Oppermann 			to.to_tsecr = 0;
16314531b345SRichard Scheffenegger 		else if (tp->t_rxtshift == 1 &&
16324531b345SRichard Scheffenegger 			 tp->t_flags & TF_PREVVALID &&
16334531b345SRichard Scheffenegger 			 tp->t_badrxtwin != 0 &&
16344531b345SRichard Scheffenegger 			 TSTMP_LT(to.to_tsecr, tp->t_badrxtwin))
163510d20c84SMatt Macy 			cc_cong_signal(tp, th, CC_RTO_ERR);
1636bf6d304aSAndre Oppermann 	}
163707dacf03SAndre Oppermann 	/*
163897d8d152SAndre Oppermann 	 * Process options only when we get SYN/ACK back. The SYN case
163997d8d152SAndre Oppermann 	 * for incoming connections is handled in tcp_syncache.
16405396d0f8SAndre Oppermann 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN>
16415396d0f8SAndre Oppermann 	 * or <SYN,ACK>) segment itself is never scaled.
164297d8d152SAndre Oppermann 	 * XXX this is traditional behavior, may need to be cleaned up.
1643df8bae1dSRodney W. Grimes 	 */
16440a389eabSSimon L. B. Nielsen 	if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
16456e16d877SRichard Scheffenegger 		/* Handle parallel SYN for ECN */
1646f7220c48SRichard Scheffenegger 		tcp_ecn_input_parallel_syn(tp, thflags, iptos);
1647464fcfbcSAndre Oppermann 		if ((to.to_flags & TOF_SCALE) &&
16482593f858SRichard Scheffenegger 		    (tp->t_flags & TF_REQ_SCALE) &&
16492593f858SRichard Scheffenegger 		    !(tp->t_flags & TF_NOOPT)) {
1650be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_SCALE;
165102a1a643SAndre Oppermann 			tp->snd_scale = to.to_wscale;
16528e051165SRichard Scheffenegger 		} else
16538e051165SRichard Scheffenegger 			tp->t_flags &= ~TF_REQ_SCALE;
16545396d0f8SAndre Oppermann 		/*
16555396d0f8SAndre Oppermann 		 * Initial send window.  It will be updated with
16565396d0f8SAndre Oppermann 		 * the next incoming segment to the scaled value.
16575396d0f8SAndre Oppermann 		 */
16585396d0f8SAndre Oppermann 		tp->snd_wnd = th->th_win;
16598e051165SRichard Scheffenegger 		if ((to.to_flags & TOF_TS) &&
16602593f858SRichard Scheffenegger 		    (tp->t_flags & TF_REQ_TSTMP) &&
16612593f858SRichard Scheffenegger 		    !(tp->t_flags & TF_NOOPT)) {
1662be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_TSTMP;
1663be2ac88cSJonathan Lemon 			tp->ts_recent = to.to_tsval;
1664d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
16658e051165SRichard Scheffenegger 		} else
16668e051165SRichard Scheffenegger 			tp->t_flags &= ~TF_REQ_TSTMP;
1667be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_MSS)
1668be2ac88cSJonathan Lemon 			tcp_mss(tp, to.to_mss);
16693529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
16702593f858SRichard Scheffenegger 		    (!(to.to_flags & TOF_SACKPERM) ||
16712593f858SRichard Scheffenegger 		    (tp->t_flags & TF_NOOPT)))
16723529149eSAndre Oppermann 			tp->t_flags &= ~TF_SACK_PERMIT;
1673c560df6fSPatrick Kelsey 		if (IS_FASTOPEN(tp->t_flags)) {
16742593f858SRichard Scheffenegger 			if ((to.to_flags & TOF_FASTOPEN) &&
16752593f858SRichard Scheffenegger 			    !(tp->t_flags & TF_NOOPT)) {
1676a026a53aSMichael Tuexen 				uint16_t mss;
1677a026a53aSMichael Tuexen 
1678a026a53aSMichael Tuexen 				if (to.to_flags & TOF_MSS)
1679a026a53aSMichael Tuexen 					mss = to.to_mss;
1680c560df6fSPatrick Kelsey 				else
16819eb0e832SGleb Smirnoff 					if ((inp->inp_vflag & INP_IPV6) != 0)
1682a026a53aSMichael Tuexen 						mss = TCP6_MSS;
1683a026a53aSMichael Tuexen 					else
1684a026a53aSMichael Tuexen 						mss = TCP_MSS;
1685a026a53aSMichael Tuexen 				tcp_fastopen_update_cache(tp, mss,
1686a026a53aSMichael Tuexen 				    to.to_tfo_len, to.to_tfo_cookie);
1687a026a53aSMichael Tuexen 			} else
1688c560df6fSPatrick Kelsey 				tcp_fastopen_disable_path(tp);
1689c560df6fSPatrick Kelsey 		}
16906d90faf3SPaul Saab 	}
16916d90faf3SPaul Saab 
1692df8bae1dSRodney W. Grimes 	/*
1693283c76c7SMichael Tuexen 	 * If timestamps were negotiated during SYN/ACK and a
1694283c76c7SMichael Tuexen 	 * segment without a timestamp is received, silently drop
1695d2b3ceddSMichael Tuexen 	 * the segment, unless it is a RST segment or missing timestamps are
1696d2b3ceddSMichael Tuexen 	 * tolerated.
1697283c76c7SMichael Tuexen 	 * See section 3.2 of RFC 7323.
16984da9052aSMichael Tuexen 	 */
16994da9052aSMichael Tuexen 	if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) {
1700d2b3ceddSMichael Tuexen 		if (((thflags & TH_RST) != 0) || V_tcp_tolerate_missing_ts) {
1701cc3c3485SMichael Tuexen 			if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
1702cc3c3485SMichael Tuexen 				log(LOG_DEBUG, "%s; %s: Timestamp missing, "
1703cc3c3485SMichael Tuexen 				    "segment processed normally\n",
1704cc3c3485SMichael Tuexen 				    s, __func__);
1705cc3c3485SMichael Tuexen 				free(s, M_TCPLOG);
1706cc3c3485SMichael Tuexen 			}
1707cc3c3485SMichael Tuexen 		} else {
17084da9052aSMichael Tuexen 			if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
17094da9052aSMichael Tuexen 				log(LOG_DEBUG, "%s; %s: Timestamp missing, "
1710283c76c7SMichael Tuexen 				    "segment silently dropped\n", s, __func__);
17114da9052aSMichael Tuexen 				free(s, M_TCPLOG);
17124da9052aSMichael Tuexen 			}
1713283c76c7SMichael Tuexen 			goto drop;
17144da9052aSMichael Tuexen 		}
1715cc3c3485SMichael Tuexen 	}
1716283c76c7SMichael Tuexen 	/*
1717283c76c7SMichael Tuexen 	 * If timestamps were not negotiated during SYN/ACK and a
171875fcd27aSMichael Tuexen 	 * segment with a timestamp is received, ignore the
1719283c76c7SMichael Tuexen 	 * timestamp and process the packet normally.
1720283c76c7SMichael Tuexen 	 * See section 3.2 of RFC 7323.
1721283c76c7SMichael Tuexen 	 */
17224da9052aSMichael Tuexen 	if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) {
17234da9052aSMichael Tuexen 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
17244da9052aSMichael Tuexen 			log(LOG_DEBUG, "%s; %s: Timestamp not expected, "
1725283c76c7SMichael Tuexen 			    "segment processed normally\n", s, __func__);
17264da9052aSMichael Tuexen 			free(s, M_TCPLOG);
17274da9052aSMichael Tuexen 		}
17284da9052aSMichael Tuexen 	}
17294da9052aSMichael Tuexen 
17304da9052aSMichael Tuexen 	/*
1731df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
1732df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
1733df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
1734df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
1735df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
1736df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
1737df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
1738df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
1739df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
1740df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
1741df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
1742df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
1743a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
1744c5ad39b9SAndre Oppermann 	 * Since we check for TCPS_ESTABLISHED first, it can only
1745a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
1746df8bae1dSRodney W. Grimes 	 */
1747df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
1748c5ad39b9SAndre Oppermann 	    th->th_seq == tp->rcv_nxt &&
1749fb59c426SYoshinobu Inoue 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1750c5ad39b9SAndre Oppermann 	    tp->snd_nxt == tp->snd_max &&
1751c5ad39b9SAndre Oppermann 	    tiwin && tiwin == tp->snd_wnd &&
1752a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
1753c28440dbSRandall Stewart 	    SEGQ_EMPTY(tp) &&
17544741bfcbSPatrick Kelsey 	    ((to.to_flags & TOF_TS) == 0 ||
1755c5ad39b9SAndre Oppermann 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) {
1756df8bae1dSRodney W. Grimes 		/*
1757df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
1758df8bae1dSRodney W. Grimes 		 * record the timestamp.
1759a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
1760a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1761df8bae1dSRodney W. Grimes 		 */
1762be2ac88cSJonathan Lemon 		if ((to.to_flags & TOF_TS) != 0 &&
1763fb59c426SYoshinobu Inoue 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1764d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
1765a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
1766df8bae1dSRodney W. Grimes 		}
1767df8bae1dSRodney W. Grimes 
1768fb59c426SYoshinobu Inoue 		if (tlen == 0) {
1769fb59c426SYoshinobu Inoue 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
1770fb59c426SYoshinobu Inoue 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
1771dbc42409SLawrence Stewart 			    !IN_RECOVERY(tp->t_flags) &&
1772fc30a251SAndre Oppermann 			    (to.to_flags & TOF_SACK) == 0 &&
1773dbc42409SLawrence Stewart 			    TAILQ_EMPTY(&tp->snd_holes)) {
1774df8bae1dSRodney W. Grimes 				/*
1775e8949f74SAndre Oppermann 				 * This is a pure ack for outstanding data.
1776df8bae1dSRodney W. Grimes 				 */
177778b50714SRobert Watson 				TCPSTAT_INC(tcps_predack);
1778252ca428SRobert Watson 
17799b8b58e0SJonathan Lemon 				/*
178010d20c84SMatt Macy 				 * "bad retransmit" recovery without timestamps.
17819b8b58e0SJonathan Lemon 				 */
178210d20c84SMatt Macy 				if ((to.to_flags & TOF_TS) == 0 &&
178310d20c84SMatt Macy 				    tp->t_rxtshift == 1 &&
1784672dc4aeSJohn Baldwin 				    tp->t_flags & TF_PREVVALID &&
17854531b345SRichard Scheffenegger 				    tp->t_badrxtwin != 0 &&
17864531b345SRichard Scheffenegger 				    TSTMP_LT(ticks, tp->t_badrxtwin)) {
1787dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_RTO_ERR);
17889b8b58e0SJonathan Lemon 				}
1789fa55172bSMatthew Dillon 
1790fa55172bSMatthew Dillon 				/*
1791fa55172bSMatthew Dillon 				 * Recalculate the transmit timer / rtt.
1792fa55172bSMatthew Dillon 				 *
1793fa55172bSMatthew Dillon 				 * Some boxes send broken timestamp replies
1794fa55172bSMatthew Dillon 				 * during the SYN+ACK phase, ignore
1795fa55172bSMatthew Dillon 				 * timestamps of 0 or we could calculate a
1796fa55172bSMatthew Dillon 				 * huge RTT and blow up the retransmit timer.
1797fa55172bSMatthew Dillon 				 */
1798fa55172bSMatthew Dillon 				if ((to.to_flags & TOF_TS) != 0 &&
1799fa55172bSMatthew Dillon 				    to.to_tsecr) {
18003ac12506SJonathan T. Looney 					uint32_t t;
1801d8951c8aSBjoern A. Zeeb 
1802d8951c8aSBjoern A. Zeeb 					t = tcp_ts_getticks() - to.to_tsecr;
1803d8951c8aSBjoern A. Zeeb 					if (!tp->t_rttlow || tp->t_rttlow > t)
1804d8951c8aSBjoern A. Zeeb 						tp->t_rttlow = t;
1805a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
1806d8951c8aSBjoern A. Zeeb 					    TCP_TS_TO_TICKS(t) + 1);
1807fa55172bSMatthew Dillon 				} else if (tp->t_rtttime &&
1808fa55172bSMatthew Dillon 				    SEQ_GT(th->th_ack, tp->t_rtseq)) {
1809eaf80179SAndre Oppermann 					if (!tp->t_rttlow ||
1810eaf80179SAndre Oppermann 					    tp->t_rttlow > ticks - tp->t_rtttime)
1811eaf80179SAndre Oppermann 						tp->t_rttlow = ticks - tp->t_rtttime;
1812c068736aSJeffrey Hsu 					tcp_xmit_timer(tp,
1813c068736aSJeffrey Hsu 							ticks - tp->t_rtttime);
1814fa55172bSMatthew Dillon 				}
1815dbc42409SLawrence Stewart 				acked = BYTES_THIS_ACK(tp, th);
181639bc9de5SLawrence Stewart 
1817bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
181839bc9de5SLawrence Stewart 				/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
181939bc9de5SLawrence Stewart 				hhook_run_tcp_est_in(tp, th, &to);
1820bd79708dSJonathan T. Looney #endif
182139bc9de5SLawrence Stewart 
18224b7b743cSLawrence Stewart 				TCPSTAT_ADD(tcps_rcvackpack, nsegs);
182378b50714SRobert Watson 				TCPSTAT_ADD(tcps_rcvackbyte, acked);
1824df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
18259d11646dSJeffrey Hsu 				if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
18269d11646dSJeffrey Hsu 				    SEQ_LEQ(th->th_ack, tp->snd_recover))
18279d11646dSJeffrey Hsu 					tp->snd_recover = th->th_ack - 1;
1828dbc42409SLawrence Stewart 
1829dbc42409SLawrence Stewart 				/*
1830dbc42409SLawrence Stewart 				 * Let the congestion control algorithm update
1831dbc42409SLawrence Stewart 				 * congestion control related information. This
1832dbc42409SLawrence Stewart 				 * typically means increasing the congestion
1833dbc42409SLawrence Stewart 				 * window.
1834dbc42409SLawrence Stewart 				 */
18354b7b743cSLawrence Stewart 				cc_ack_received(tp, th, nsegs, CC_ACK);
1836dbc42409SLawrence Stewart 
18379d11646dSJeffrey Hsu 				tp->snd_una = th->th_ack;
18381645d090SJeff Roberson 				/*
1839e8949f74SAndre Oppermann 				 * Pull snd_wl2 up to prevent seq wrap relative
18401645d090SJeff Roberson 				 * to th_ack.
18411645d090SJeff Roberson 				 */
1842cb942153SJeffrey Hsu 				tp->snd_wl2 = th->th_ack;
1843b5addd85SJeffrey Hsu 				tp->t_dupacks = 0;
1844df8bae1dSRodney W. Grimes 				m_freem(m);
1845df8bae1dSRodney W. Grimes 
1846df8bae1dSRodney W. Grimes 				/*
1847df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
1848df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
1849df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
1850df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
1851df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
1852df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
1853df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
1854e8949f74SAndre Oppermann 				 */
18552b9c9984SGeorge V. Neville-Neil 				TCP_PROBE3(debug__input, tp, th, m);
185608af8aacSRandall Stewart 				/*
185708af8aacSRandall Stewart 				 * Clear t_acktime if remote side has ACKd
185808af8aacSRandall Stewart 				 * all data in the socket buffer.
185908af8aacSRandall Stewart 				 * Otherwise, update t_acktime if we received
186008af8aacSRandall Stewart 				 * a sufficiently large ACK.
186108af8aacSRandall Stewart 				 */
186208af8aacSRandall Stewart 				if (sbavail(&so->so_snd) == 0)
186308af8aacSRandall Stewart 					tp->t_acktime = 0;
186408af8aacSRandall Stewart 				else if (acked > 1)
186508af8aacSRandall Stewart 					tp->t_acktime = ticks;
1866df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
1867b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
1868b8152ba7SAndre Oppermann 				else if (!tcp_timer_active(tp, TT_PERSIST))
1869b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT,
187008af8aacSRandall Stewart 					    TP_RXTCUR(tp));
1871032bf749SRichard Scheffenegger 				sowwakeup(so);
1872cfa6009eSGleb Smirnoff 				if (sbavail(&so->so_snd))
187340fa3e40SGleb Smirnoff 					(void) tcp_output(tp);
1874a14c749fSJonathan Lemon 				goto check_delack;
1875df8bae1dSRodney W. Grimes 			}
1876fb59c426SYoshinobu Inoue 		} else if (th->th_ack == tp->snd_una &&
1877fb59c426SYoshinobu Inoue 		    tlen <= sbspace(&so->so_rcv)) {
18786741ecf5SAndre Oppermann 			int newsize = 0;	/* automatic sockbuf scaling */
18796741ecf5SAndre Oppermann 
1880df8bae1dSRodney W. Grimes 			/*
1881252ca428SRobert Watson 			 * This is a pure, in-sequence data packet with
1882252ca428SRobert Watson 			 * nothing on the reassembly queue and we have enough
1883252ca428SRobert Watson 			 * buffer space to take it.
1884df8bae1dSRodney W. Grimes 			 */
18856d90faf3SPaul Saab 			/* Clean receiver SACK report if present */
18863529149eSAndre Oppermann 			if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks)
18876d90faf3SPaul Saab 				tcp_clean_sackreport(tp);
188878b50714SRobert Watson 			TCPSTAT_INC(tcps_preddat);
1889fb59c426SYoshinobu Inoue 			tp->rcv_nxt += tlen;
1890e854dd38SRandall Stewart 			if (tlen &&
1891e854dd38SRandall Stewart 			    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1892e854dd38SRandall Stewart 			    (tp->t_fbyte_in == 0)) {
1893e854dd38SRandall Stewart 				tp->t_fbyte_in = ticks;
1894e854dd38SRandall Stewart 				if (tp->t_fbyte_in == 0)
1895e854dd38SRandall Stewart 					tp->t_fbyte_in = 1;
1896e854dd38SRandall Stewart 				if (tp->t_fbyte_out && tp->t_fbyte_in)
1897e854dd38SRandall Stewart 					tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1898e854dd38SRandall Stewart 			}
18991645d090SJeff Roberson 			/*
19001645d090SJeff Roberson 			 * Pull snd_wl1 up to prevent seq wrap relative to
19011645d090SJeff Roberson 			 * th_seq.
19021645d090SJeff Roberson 			 */
190360ee3bb2SAndre Oppermann 			tp->snd_wl1 = th->th_seq;
19041645d090SJeff Roberson 			/*
19051645d090SJeff Roberson 			 * Pull rcv_up up to prevent seq wrap relative to
19061645d090SJeff Roberson 			 * rcv_nxt.
19071645d090SJeff Roberson 			 */
19081645d090SJeff Roberson 			tp->rcv_up = tp->rcv_nxt;
19094b7b743cSLawrence Stewart 			TCPSTAT_ADD(tcps_rcvpack, nsegs);
191078b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
19112b9c9984SGeorge V. Neville-Neil 			TCP_PROBE3(debug__input, tp, th, m);
19125d06879aSGeorge V. Neville-Neil 
1913e44c1887SSteven Hartland 			newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
19146741ecf5SAndre Oppermann 
19156741ecf5SAndre Oppermann 			/* Add data to socket buffer. */
19161e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
1917c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1918c1c36a2cSMike Silbersack 				m_freem(m);
1919c1c36a2cSMike Silbersack 			} else {
19206741ecf5SAndre Oppermann 				/*
19216741ecf5SAndre Oppermann 				 * Set new socket buffer size.
19226741ecf5SAndre Oppermann 				 * Give up when limit is reached.
19236741ecf5SAndre Oppermann 				 */
19246741ecf5SAndre Oppermann 				if (newsize)
192543283184SGleb Smirnoff 					if (!sbreserve_locked(so, SO_RCV,
192643283184SGleb Smirnoff 					    newsize, NULL))
19276741ecf5SAndre Oppermann 						so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
1928fb59c426SYoshinobu Inoue 				m_adj(m, drop_hdrlen);	/* delayed header drop */
1929651e4e6aSGleb Smirnoff 				sbappendstream_locked(&so->so_rcv, m, 0);
1930c1c36a2cSMike Silbersack 			}
1931032bf749SRichard Scheffenegger 			/* NB: sorwakeup_locked() does an implicit unlock. */
1932032bf749SRichard Scheffenegger 			sorwakeup_locked(so);
1933c1e5a6e5SAndre Oppermann 			if (DELAY_ACK(tp, tlen)) {
19343bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
1935f498eeeeSDavid Greenman 			} else {
1936e612a582SDavid Greenman 				tp->t_flags |= TF_ACKNOW;
193740fa3e40SGleb Smirnoff 				tcp_output(tp);
1938e612a582SDavid Greenman 			}
1939a14c749fSJonathan Lemon 			goto check_delack;
1940df8bae1dSRodney W. Grimes 		}
1941df8bae1dSRodney W. Grimes 	}
1942df8bae1dSRodney W. Grimes 
1943df8bae1dSRodney W. Grimes 	/*
1944df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
1945df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
1946df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
1947df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
1948df8bae1dSRodney W. Grimes 	 */
1949df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
1950df8bae1dSRodney W. Grimes 	if (win < 0)
1951df8bae1dSRodney W. Grimes 		win = 0;
195266e39adcSJohn Polstra 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1953df8bae1dSRodney W. Grimes 
1954df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1955df8bae1dSRodney W. Grimes 	/*
1956764d8cefSBill Fenner 	 * If the state is SYN_RECEIVED:
1957764d8cefSBill Fenner 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
1958764d8cefSBill Fenner 	 */
1959764d8cefSBill Fenner 	case TCPS_SYN_RECEIVED:
1960b352ef58SRichard Scheffenegger 		if (thflags & TH_RST) {
1961b352ef58SRichard Scheffenegger 			/* Handle RST segments later. */
1962b352ef58SRichard Scheffenegger 			break;
1963b352ef58SRichard Scheffenegger 		}
1964fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1965fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1966a57815efSBosko Milekic 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1967a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
1968d1b07f36SRandall Stewart 				tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
1969a57815efSBosko Milekic 				goto dropwithreset;
1970a57815efSBosko Milekic 		}
197168bd7ed1SJonathan T. Looney 		if (IS_FASTOPEN(tp->t_flags)) {
1972281a0fd4SPatrick Kelsey 			/*
1973281a0fd4SPatrick Kelsey 			 * When a TFO connection is in SYN_RECEIVED, the
1974281a0fd4SPatrick Kelsey 			 * only valid packets are the initial SYN, a
1975281a0fd4SPatrick Kelsey 			 * retransmit/copy of the initial SYN (possibly with
1976281a0fd4SPatrick Kelsey 			 * a subset of the original data), a valid ACK, a
1977281a0fd4SPatrick Kelsey 			 * FIN, or a RST.
1978281a0fd4SPatrick Kelsey 			 */
1979281a0fd4SPatrick Kelsey 			if ((thflags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) {
1980281a0fd4SPatrick Kelsey 				rstreason = BANDLIM_RST_OPENPORT;
1981d1b07f36SRandall Stewart 				tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
1982281a0fd4SPatrick Kelsey 				goto dropwithreset;
1983281a0fd4SPatrick Kelsey 			} else if (thflags & TH_SYN) {
1984281a0fd4SPatrick Kelsey 				/* non-initial SYN is ignored */
1985281a0fd4SPatrick Kelsey 				if ((tcp_timer_active(tp, TT_DELACK) ||
1986281a0fd4SPatrick Kelsey 				     tcp_timer_active(tp, TT_REXMT)))
1987281a0fd4SPatrick Kelsey 					goto drop;
1988281a0fd4SPatrick Kelsey 			} else if (!(thflags & (TH_ACK|TH_FIN|TH_RST))) {
1989281a0fd4SPatrick Kelsey 				goto drop;
1990281a0fd4SPatrick Kelsey 			}
1991281a0fd4SPatrick Kelsey 		}
1992764d8cefSBill Fenner 		break;
1993764d8cefSBill Fenner 
1994764d8cefSBill Fenner 	/*
1995df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
199698732609SMichael Tuexen 	 *	if seg contains a RST with valid ACK (SEQ.ACK has already
199798732609SMichael Tuexen 	 *	    been verified), then drop the connection.
199898732609SMichael Tuexen 	 *	if seg contains a RST without an ACK, drop the seg.
199998732609SMichael Tuexen 	 *	if seg does not contain SYN, then drop the seg.
2000df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
2001df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
2002df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
2003f2512ba1SRui Paulo 	 *	if seg contains an ECE and ECN support is enabled, the stream
2004f2512ba1SRui Paulo 	 *	    is ECN capable.
2005df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
2006df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
2007df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
2008df8bae1dSRodney W. Grimes 	 */
2009df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
201057f60867SMark Johnston 		if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) {
2011d9fae5abSAndriy Gapon 			TCP_PROBE5(connect__refused, NULL, tp,
20122b9c9984SGeorge V. Neville-Neil 			    m, tp, th);
2013d1b07f36SRandall Stewart 			tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
2014df8bae1dSRodney W. Grimes 			tp = tcp_drop(tp, ECONNREFUSED);
201557f60867SMark Johnston 		}
20160ca3f933SAndre Oppermann 		if (thflags & TH_RST)
2017df8bae1dSRodney W. Grimes 			goto drop;
20180ca3f933SAndre Oppermann 		if (!(thflags & TH_SYN))
2019df8bae1dSRodney W. Grimes 			goto drop;
2020464fcfbcSAndre Oppermann 
2021fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
2022df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
2023fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
2024c560df6fSPatrick Kelsey 			int tfo_partial_ack = 0;
2025c560df6fSPatrick Kelsey 
202678b50714SRobert Watson 			TCPSTAT_INC(tcps_connects);
2027845799c1SAndras Olah 			soisconnected(so);
2028c488362eSRobert Watson #ifdef MAC
202930d239bcSRobert Watson 			mac_socketpeer_set_from_mbuf(m, so);
2030c488362eSRobert Watson #endif
2031845799c1SAndras Olah 			/* Do window scaling on this connection? */
2032845799c1SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2033845799c1SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
2034845799c1SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2035845799c1SAndras Olah 			}
20363ac12506SJonathan T. Looney 			tp->rcv_adv += min(tp->rcv_wnd,
2037766282cbSJohn Baldwin 			    TCP_MAXWIN << tp->rcv_scale);
2038a0292f23SGarrett Wollman 			tp->snd_una++;		/* SYN is acked */
2039a0292f23SGarrett Wollman 			/*
2040c560df6fSPatrick Kelsey 			 * If not all the data that was sent in the TFO SYN
2041c560df6fSPatrick Kelsey 			 * has been acked, resend the remainder right away.
2042c560df6fSPatrick Kelsey 			 */
2043c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags) &&
2044c560df6fSPatrick Kelsey 			    (tp->snd_una != tp->snd_max)) {
2045c560df6fSPatrick Kelsey 				tp->snd_nxt = th->th_ack;
2046c560df6fSPatrick Kelsey 				tfo_partial_ack = 1;
2047c560df6fSPatrick Kelsey 			}
2048c560df6fSPatrick Kelsey 			/*
2049a0292f23SGarrett Wollman 			 * If there's data, delay ACK; if there's also a FIN
2050a0292f23SGarrett Wollman 			 * ACKNOW will be turned on later.
2051a0292f23SGarrett Wollman 			 */
2052c560df6fSPatrick Kelsey 			if (DELAY_ACK(tp, tlen) && tlen != 0 && !tfo_partial_ack)
2053b8152ba7SAndre Oppermann 				tcp_timer_activate(tp, TT_DELACK,
2054b8152ba7SAndre Oppermann 				    tcp_delacktime);
2055a0292f23SGarrett Wollman 			else
2056a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
2057f2512ba1SRui Paulo 
2058f7220c48SRichard Scheffenegger 			tcp_ecn_input_syn_sent(tp, thflags, iptos);
2059f2512ba1SRui Paulo 
2060a0292f23SGarrett Wollman 			/*
2061a0292f23SGarrett Wollman 			 * Received <SYN,ACK> in SYN_SENT[*] state.
2062a0292f23SGarrett Wollman 			 * Transitions:
2063a0292f23SGarrett Wollman 			 *	SYN_SENT  --> ESTABLISHED
2064a0292f23SGarrett Wollman 			 *	SYN_SENT* --> FIN_WAIT_1
2065a0292f23SGarrett Wollman 			 */
20669b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
2067a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDFIN) {
206808af8aacSRandall Stewart 				tp->t_acktime = ticks;
206957f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_1);
2070a0292f23SGarrett Wollman 				tp->t_flags &= ~TF_NEEDFIN;
2071fb59c426SYoshinobu Inoue 				thflags &= ~TH_SYN;
20727ff19458SPaul Traina 			} else {
207357f60867SMark Johnston 				tcp_state_change(tp, TCPS_ESTABLISHED);
2074d9fae5abSAndriy Gapon 				TCP_PROBE5(connect__established, NULL, tp,
20752b9c9984SGeorge V. Neville-Neil 				    m, tp, th);
2076dbc42409SLawrence Stewart 				cc_conn_init(tp);
20779077f387SGleb Smirnoff 				tcp_timer_activate(tp, TT_KEEP,
20789077f387SGleb Smirnoff 				    TP_KEEPIDLE(tp));
20797ff19458SPaul Traina 			}
2080a0292f23SGarrett Wollman 		} else {
2081a0292f23SGarrett Wollman 			/*
2082c068736aSJeffrey Hsu 			 * Received initial SYN in SYN-SENT[*] state =>
2083153edc50SHiren Panchasara 			 * simultaneous open.
2084c068736aSJeffrey Hsu 			 * If it succeeds, connection is * half-synchronized.
2085c068736aSJeffrey Hsu 			 * Otherwise, do 3-way handshake:
2086a0292f23SGarrett Wollman 			 *        SYN-SENT -> SYN-RECEIVED
2087a0292f23SGarrett Wollman 			 *        SYN-SENT* -> SYN-RECEIVED*
2088a0292f23SGarrett Wollman 			 */
2089493105c2SGleb Smirnoff 			tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN | TF_SONOTCONN);
2090b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
209157f60867SMark Johnston 			tcp_state_change(tp, TCPS_SYN_RECEIVED);
2092a0292f23SGarrett Wollman 		}
2093df8bae1dSRodney W. Grimes 
2094df8bae1dSRodney W. Grimes 		/*
2095fb59c426SYoshinobu Inoue 		 * Advance th->th_seq to correspond to first data byte.
2096df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
2097df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
2098df8bae1dSRodney W. Grimes 		 */
2099fb59c426SYoshinobu Inoue 		th->th_seq++;
2100fb59c426SYoshinobu Inoue 		if (tlen > tp->rcv_wnd) {
2101fb59c426SYoshinobu Inoue 			todrop = tlen - tp->rcv_wnd;
2102df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
2103fb59c426SYoshinobu Inoue 			tlen = tp->rcv_wnd;
2104fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
210578b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpackafterwin);
210678b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2107df8bae1dSRodney W. Grimes 		}
2108fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
2109fb59c426SYoshinobu Inoue 		tp->rcv_up = th->th_seq;
2110a0292f23SGarrett Wollman 		/*
2111a0292f23SGarrett Wollman 		 * Client side of transaction: already sent SYN and data.
2112a0292f23SGarrett Wollman 		 * If the remote host used T/TCP to validate the SYN,
2113a0292f23SGarrett Wollman 		 * our data will be ACK'd; if so, enter normal data segment
2114a0292f23SGarrett Wollman 		 * processing in the middle of step 5, ack processing.
2115a0292f23SGarrett Wollman 		 * Otherwise, goto step 6.
2116a0292f23SGarrett Wollman 		 */
2117fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK)
2118a0292f23SGarrett Wollman 			goto process_ACK;
2119c068736aSJeffrey Hsu 
2120df8bae1dSRodney W. Grimes 		goto step6;
2121df8bae1dSRodney W. Grimes 	}
2122df8bae1dSRodney W. Grimes 
2123df8bae1dSRodney W. Grimes 	/*
2124df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
212580ab7c0eSGarrett Wollman 	 * First check the RST flag and sequence number since reset segments
212680ab7c0eSGarrett Wollman 	 * are exempt from the timestamp and connection count tests.  This
212780ab7c0eSGarrett Wollman 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
212880ab7c0eSGarrett Wollman 	 * below which allowed reset segments in half the sequence space
212980ab7c0eSGarrett Wollman 	 * to fall though and be processed (which gives forged reset
213080ab7c0eSGarrett Wollman 	 * segments with a random sequence number a 50 percent chance of
213180ab7c0eSGarrett Wollman 	 * killing a connection).
213280ab7c0eSGarrett Wollman 	 * Then check timestamp, if present.
2133a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
2134df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
2135df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
2136df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
213780ab7c0eSGarrett Wollman 	 */
2138fb59c426SYoshinobu Inoue 	if (thflags & TH_RST) {
21393220a212SGleb Smirnoff 		/*
21403220a212SGleb Smirnoff 		 * RFC5961 Section 3.2
21413220a212SGleb Smirnoff 		 *
21423220a212SGleb Smirnoff 		 * - RST drops connection only if SEG.SEQ == RCV.NXT.
21433220a212SGleb Smirnoff 		 * - If RST is in window, we send challenge ACK.
21443220a212SGleb Smirnoff 		 *
21453220a212SGleb Smirnoff 		 * Note: to take into account delayed ACKs, we should
21463220a212SGleb Smirnoff 		 *   test against last_ack_sent instead of rcv_nxt.
21473220a212SGleb Smirnoff 		 * Note 2: we handle special case of closed window, not
21483220a212SGleb Smirnoff 		 *   covered by the RFC.
21493220a212SGleb Smirnoff 		 */
21503220a212SGleb Smirnoff 		if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
21513220a212SGleb Smirnoff 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
21523220a212SGleb Smirnoff 		    (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) {
21533220a212SGleb Smirnoff 			KASSERT(tp->t_state != TCPS_SYN_SENT,
21543220a212SGleb Smirnoff 			    ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p",
21553220a212SGleb Smirnoff 			    __func__, th, tp));
21563220a212SGleb Smirnoff 
21573220a212SGleb Smirnoff 			if (V_tcp_insecure_rst ||
21583220a212SGleb Smirnoff 			    tp->last_ack_sent == th->th_seq) {
21593220a212SGleb Smirnoff 				TCPSTAT_INC(tcps_drops);
21603220a212SGleb Smirnoff 				/* Drop the connection. */
21613220a212SGleb Smirnoff 				switch (tp->t_state) {
216280ab7c0eSGarrett Wollman 				case TCPS_SYN_RECEIVED:
216380ab7c0eSGarrett Wollman 					so->so_error = ECONNREFUSED;
216480ab7c0eSGarrett Wollman 					goto close;
216580ab7c0eSGarrett Wollman 				case TCPS_ESTABLISHED:
216680ab7c0eSGarrett Wollman 				case TCPS_FIN_WAIT_1:
216780ab7c0eSGarrett Wollman 				case TCPS_FIN_WAIT_2:
216880ab7c0eSGarrett Wollman 				case TCPS_CLOSE_WAIT:
21696779a1a1SMichael Tuexen 				case TCPS_CLOSING:
21706779a1a1SMichael Tuexen 				case TCPS_LAST_ACK:
217180ab7c0eSGarrett Wollman 					so->so_error = ECONNRESET;
217280ab7c0eSGarrett Wollman 				close:
21733220a212SGleb Smirnoff 					/* FALLTHROUGH */
21743220a212SGleb Smirnoff 				default:
2175d1b07f36SRandall Stewart 					tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_RST);
217680ab7c0eSGarrett Wollman 					tp = tcp_close(tp);
21773220a212SGleb Smirnoff 				}
21783220a212SGleb Smirnoff 			} else {
21793220a212SGleb Smirnoff 				TCPSTAT_INC(tcps_badrst);
21803220a212SGleb Smirnoff 				/* Send challenge ACK. */
21813220a212SGleb Smirnoff 				tcp_respond(tp, mtod(m, void *), th, m,
21823220a212SGleb Smirnoff 				    tp->rcv_nxt, tp->snd_nxt, TH_ACK);
21833220a212SGleb Smirnoff 				tp->last_ack_sent = tp->rcv_nxt;
21843220a212SGleb Smirnoff 				m = NULL;
21853220a212SGleb Smirnoff 			}
21863220a212SGleb Smirnoff 		}
21873220a212SGleb Smirnoff 		goto drop;
21883220a212SGleb Smirnoff 	}
218980ab7c0eSGarrett Wollman 
21903220a212SGleb Smirnoff 	/*
21913220a212SGleb Smirnoff 	 * RFC5961 Section 4.2
21923220a212SGleb Smirnoff 	 * Send challenge ACK for any SYN in synchronized state.
21933220a212SGleb Smirnoff 	 */
2194281a0fd4SPatrick Kelsey 	if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT &&
2195281a0fd4SPatrick Kelsey 	    tp->t_state != TCPS_SYN_RECEIVED) {
21963220a212SGleb Smirnoff 		TCPSTAT_INC(tcps_badsyn);
21973220a212SGleb Smirnoff 		if (V_tcp_insecure_syn &&
21983220a212SGleb Smirnoff 		    SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
21993220a212SGleb Smirnoff 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
2200d1b07f36SRandall Stewart 			tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
22013220a212SGleb Smirnoff 			tp = tcp_drop(tp, ECONNRESET);
22023220a212SGleb Smirnoff 			rstreason = BANDLIM_UNLIMITED;
22033220a212SGleb Smirnoff 		} else {
220483c1ec92SRichard Scheffenegger 			tcp_ecn_input_syn_sent(tp, thflags, iptos);
22053220a212SGleb Smirnoff 			/* Send challenge ACK. */
22063220a212SGleb Smirnoff 			tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt,
22073220a212SGleb Smirnoff 			    tp->snd_nxt, TH_ACK);
22083220a212SGleb Smirnoff 			tp->last_ack_sent = tp->rcv_nxt;
22093220a212SGleb Smirnoff 			m = NULL;
221080ab7c0eSGarrett Wollman 		}
221180ab7c0eSGarrett Wollman 		goto drop;
221280ab7c0eSGarrett Wollman 	}
221380ab7c0eSGarrett Wollman 
221480ab7c0eSGarrett Wollman 	/*
2215df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
2216df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
2217df8bae1dSRodney W. Grimes 	 */
2218be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
221980ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
2220df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
2221d8951c8aSBjoern A. Zeeb 		if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) {
2222df8bae1dSRodney W. Grimes 			/*
2223df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
2224df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
2225df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
2226df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
2227df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
2228df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
2229df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
2230df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
2231df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
2232df8bae1dSRodney W. Grimes 			 */
2233df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
2234df8bae1dSRodney W. Grimes 		} else {
223578b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
223678b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, tlen);
223778b50714SRobert Watson 			TCPSTAT_INC(tcps_pawsdrop);
223807fd333dSMatthew Dillon 			if (tlen)
2239df8bae1dSRodney W. Grimes 				goto dropafterack;
22401ab4789dSMatthew Dillon 			goto drop;
22411ab4789dSMatthew Dillon 		}
2242df8bae1dSRodney W. Grimes 	}
2243df8bae1dSRodney W. Grimes 
2244a0292f23SGarrett Wollman 	/*
224580ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
224680ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
224780ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
224880ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
224980ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
225080ab7c0eSGarrett Wollman 	 */
2251a57815efSBosko Milekic 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
2252a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2253d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
2254a57815efSBosko Milekic 		goto dropwithreset;
2255a57815efSBosko Milekic 	}
225680ab7c0eSGarrett Wollman 
2257fb59c426SYoshinobu Inoue 	todrop = tp->rcv_nxt - th->th_seq;
2258df8bae1dSRodney W. Grimes 	if (todrop > 0) {
2259831ad37eSXin LI 		if (thflags & TH_SYN) {
2260fb59c426SYoshinobu Inoue 			thflags &= ~TH_SYN;
2261fb59c426SYoshinobu Inoue 			th->th_seq++;
2262fb59c426SYoshinobu Inoue 			if (th->th_urp > 1)
2263fb59c426SYoshinobu Inoue 				th->th_urp--;
2264df8bae1dSRodney W. Grimes 			else
2265fb59c426SYoshinobu Inoue 				thflags &= ~TH_URG;
2266df8bae1dSRodney W. Grimes 			todrop--;
2267df8bae1dSRodney W. Grimes 		}
2268df8bae1dSRodney W. Grimes 		/*
2269dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
2270df8bae1dSRodney W. Grimes 		 */
2271fb59c426SYoshinobu Inoue 		if (todrop > tlen
2272fb59c426SYoshinobu Inoue 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
2273dac20301SGarrett Wollman 			/*
2274dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
2275dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
2276dac20301SGarrett Wollman 			 * of sequence; drop it.
2277dac20301SGarrett Wollman 			 */
2278fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
2279dac20301SGarrett Wollman 
2280df8bae1dSRodney W. Grimes 			/*
2281dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
2282dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
2283df8bae1dSRodney W. Grimes 			 */
2284dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
2285fb59c426SYoshinobu Inoue 			todrop = tlen;
228678b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
228778b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, todrop);
2288df8bae1dSRodney W. Grimes 		} else {
228978b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpartduppack);
229078b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop);
2291df8bae1dSRodney W. Grimes 		}
22925acfd95cSMichael Tuexen 		/*
22935acfd95cSMichael Tuexen 		 * DSACK - add SACK block for dropped range
22945acfd95cSMichael Tuexen 		 */
22958f63a52bSMichael Tuexen 		if ((todrop > 0) && (tp->t_flags & TF_SACK_PERMIT)) {
2296ecc5b1d1SMichael Tuexen 			tcp_update_sack_list(tp, th->th_seq,
2297ecc5b1d1SMichael Tuexen 			    th->th_seq + todrop);
22985acfd95cSMichael Tuexen 			/*
22995acfd95cSMichael Tuexen 			 * ACK now, as the next in-sequence segment
23005acfd95cSMichael Tuexen 			 * will clear the DSACK block again
23015acfd95cSMichael Tuexen 			 */
23025acfd95cSMichael Tuexen 			tp->t_flags |= TF_ACKNOW;
23035acfd95cSMichael Tuexen 		}
2304fb59c426SYoshinobu Inoue 		drop_hdrlen += todrop;	/* drop from the top afterwards */
2305fb59c426SYoshinobu Inoue 		th->th_seq += todrop;
2306fb59c426SYoshinobu Inoue 		tlen -= todrop;
2307fb59c426SYoshinobu Inoue 		if (th->th_urp > todrop)
2308fb59c426SYoshinobu Inoue 			th->th_urp -= todrop;
2309df8bae1dSRodney W. Grimes 		else {
2310fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;
2311fb59c426SYoshinobu Inoue 			th->th_urp = 0;
2312df8bae1dSRodney W. Grimes 		}
2313df8bae1dSRodney W. Grimes 	}
2314df8bae1dSRodney W. Grimes 
2315df8bae1dSRodney W. Grimes 	/*
2316df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
2317df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
2318df8bae1dSRodney W. Grimes 	 */
231974703901SGleb Smirnoff 	if ((tp->t_flags & TF_CLOSED) && tlen) {
232007dacf03SAndre Oppermann 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
232107dacf03SAndre Oppermann 			log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data "
232207dacf03SAndre Oppermann 			    "after socket was closed, "
232307dacf03SAndre Oppermann 			    "sending RST and removing tcpcb\n",
2324e31d8aa3SMike Silbersack 			    s, __func__, tcpstates[tp->t_state], tlen);
2325773673c1SAndre Oppermann 			free(s, M_TCPLOG);
2326773673c1SAndre Oppermann 		}
2327d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
2328d1b07f36SRandall Stewart 		/* tcp_close will kill the inp pre-log the Reset */
2329d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
2330df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
233178b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvafterclose);
2332a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
2333df8bae1dSRodney W. Grimes 		goto dropwithreset;
2334df8bae1dSRodney W. Grimes 	}
2335df8bae1dSRodney W. Grimes 
2336df8bae1dSRodney W. Grimes 	/*
2337df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
2338df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
2339df8bae1dSRodney W. Grimes 	 */
2340fb59c426SYoshinobu Inoue 	todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
2341df8bae1dSRodney W. Grimes 	if (todrop > 0) {
234278b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvpackafterwin);
2343fb59c426SYoshinobu Inoue 		if (todrop >= tlen) {
234478b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen);
2345df8bae1dSRodney W. Grimes 			/*
2346df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
2347df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
2348df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
2349df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
2350df8bae1dSRodney W. Grimes 			 * and ack.
2351df8bae1dSRodney W. Grimes 			 */
2352fb59c426SYoshinobu Inoue 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
2353df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
235478b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvwinprobe);
2355df8bae1dSRodney W. Grimes 			} else
2356df8bae1dSRodney W. Grimes 				goto dropafterack;
2357df8bae1dSRodney W. Grimes 		} else
235878b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2359df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
2360fb59c426SYoshinobu Inoue 		tlen -= todrop;
2361fb59c426SYoshinobu Inoue 		thflags &= ~(TH_PUSH|TH_FIN);
2362df8bae1dSRodney W. Grimes 	}
2363df8bae1dSRodney W. Grimes 
2364df8bae1dSRodney W. Grimes 	/*
2365df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
2366df8bae1dSRodney W. Grimes 	 * record its timestamp.
2367cf09195bSPaul Saab 	 * NOTE:
2368cf09195bSPaul Saab 	 * 1) That the test incorporates suggestions from the latest
2369a0292f23SGarrett Wollman 	 *    proposal of the tcplw@cray.com list (Braden 1993/04/26).
2370cf09195bSPaul Saab 	 * 2) That updating only on newer timestamps interferes with
2371cf09195bSPaul Saab 	 *    our earlier PAWS tests, so this check should be solely
2372cf09195bSPaul Saab 	 *    predicated on the sequence space of this segment.
2373cf09195bSPaul Saab 	 * 3) That we modify the segment boundary check to be
2374cf09195bSPaul Saab 	 *        Last.ACK.Sent <= SEG.SEQ + SEG.Len
2375cf09195bSPaul Saab 	 *    instead of RFC1323's
2376cf09195bSPaul Saab 	 *        Last.ACK.Sent < SEG.SEQ + SEG.Len,
2377cf09195bSPaul Saab 	 *    This modified check allows us to overcome RFC1323's
2378cf09195bSPaul Saab 	 *    limitations as described in Stevens TCP/IP Illustrated
2379cf09195bSPaul Saab 	 *    Vol. 2 p.869. In such cases, we can still calculate the
2380cf09195bSPaul Saab 	 *    RTT correctly when RCV.NXT == Last.ACK.Sent.
2381df8bae1dSRodney W. Grimes 	 */
2382be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 &&
2383cf09195bSPaul Saab 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
2384cf09195bSPaul Saab 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
2385cf09195bSPaul Saab 		((thflags & (TH_SYN|TH_FIN)) != 0))) {
2386d8951c8aSBjoern A. Zeeb 		tp->ts_recent_age = tcp_ts_getticks();
2387a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
2388df8bae1dSRodney W. Grimes 	}
2389df8bae1dSRodney W. Grimes 
2390df8bae1dSRodney W. Grimes 	/*
2391a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
2392a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
2393a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
2394a0292f23SGarrett Wollman 	 */
2395fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) == 0) {
2396a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
2397281a0fd4SPatrick Kelsey 		    (tp->t_flags & TF_NEEDSYN)) {
2398281a0fd4SPatrick Kelsey 			if (tp->t_state == TCPS_SYN_RECEIVED &&
239968bd7ed1SJonathan T. Looney 			    IS_FASTOPEN(tp->t_flags)) {
2400281a0fd4SPatrick Kelsey 				tp->snd_wnd = tiwin;
2401281a0fd4SPatrick Kelsey 				cc_conn_init(tp);
2402281a0fd4SPatrick Kelsey 			}
2403a0292f23SGarrett Wollman 			goto step6;
2404281a0fd4SPatrick Kelsey 		} else if (tp->t_flags & TF_ACKNOW)
24055e1aa279SDavid Malone 			goto dropafterack;
2406a0292f23SGarrett Wollman 		else
2407a0292f23SGarrett Wollman 			goto drop;
2408a0292f23SGarrett Wollman 	}
2409df8bae1dSRodney W. Grimes 
2410df8bae1dSRodney W. Grimes 	/*
2411df8bae1dSRodney W. Grimes 	 * Ack processing.
2412df8bae1dSRodney W. Grimes 	 */
2413df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2414df8bae1dSRodney W. Grimes 	/*
2415764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
2416764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
2417764d8cefSBill Fenner 	 * The ACK was checked above.
2418df8bae1dSRodney W. Grimes 	 */
2419df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2420a0292f23SGarrett Wollman 
242178b50714SRobert Watson 		TCPSTAT_INC(tcps_connects);
2422493105c2SGleb Smirnoff 		if (tp->t_flags & TF_SONOTCONN) {
2423493105c2SGleb Smirnoff 			/*
2424493105c2SGleb Smirnoff 			 * Usually SYN_RECEIVED had been created from a LISTEN,
2425493105c2SGleb Smirnoff 			 * and solisten_enqueue() has already marked the socket
2426493105c2SGleb Smirnoff 			 * layer as connected.  If it didn't, which can happen
2427493105c2SGleb Smirnoff 			 * only with an accept_filter(9), then the tp is marked
2428493105c2SGleb Smirnoff 			 * with TF_SONOTCONN.  The other reason for this mark
2429493105c2SGleb Smirnoff 			 * to be set is a simultaneous open, a SYN_RECEIVED
2430493105c2SGleb Smirnoff 			 * that had been created from SYN_SENT.
2431493105c2SGleb Smirnoff 			 */
2432493105c2SGleb Smirnoff 			tp->t_flags &= ~TF_SONOTCONN;
2433df8bae1dSRodney W. Grimes 			soisconnected(so);
2434e80062a2SGleb Smirnoff 		}
2435df8bae1dSRodney W. Grimes 		/* Do window scaling? */
2436df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2437df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
2438df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
2439df8bae1dSRodney W. Grimes 		}
2440116ef4d6SMichael Tuexen 		tp->snd_wnd = tiwin;
2441a0292f23SGarrett Wollman 		/*
2442a0292f23SGarrett Wollman 		 * Make transitions:
2443a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
2444a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
2445a0292f23SGarrett Wollman 		 */
24469b8b58e0SJonathan Lemon 		tp->t_starttime = ticks;
244718a75309SPatrick Kelsey 		if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) {
2448281a0fd4SPatrick Kelsey 			tcp_fastopen_decrement_counter(tp->t_tfo_pending);
2449281a0fd4SPatrick Kelsey 			tp->t_tfo_pending = NULL;
2450281a0fd4SPatrick Kelsey 		}
24518db239dcSMichael Tuexen 		if (tp->t_flags & TF_NEEDFIN) {
245208af8aacSRandall Stewart 			tp->t_acktime = ticks;
24538db239dcSMichael Tuexen 			tcp_state_change(tp, TCPS_FIN_WAIT_1);
24548db239dcSMichael Tuexen 			tp->t_flags &= ~TF_NEEDFIN;
24558db239dcSMichael Tuexen 		} else {
24568db239dcSMichael Tuexen 			tcp_state_change(tp, TCPS_ESTABLISHED);
24578db239dcSMichael Tuexen 			TCP_PROBE5(accept__established, NULL, tp,
24588db239dcSMichael Tuexen 			    m, tp, th);
2459281a0fd4SPatrick Kelsey 			/*
2460281a0fd4SPatrick Kelsey 			 * TFO connections call cc_conn_init() during SYN
2461281a0fd4SPatrick Kelsey 			 * processing.  Calling it again here for such
2462281a0fd4SPatrick Kelsey 			 * connections is not harmless as it would undo the
2463281a0fd4SPatrick Kelsey 			 * snd_cwnd reduction that occurs when a TFO SYN|ACK
2464281a0fd4SPatrick Kelsey 			 * is retransmitted.
2465281a0fd4SPatrick Kelsey 			 */
246668bd7ed1SJonathan T. Looney 			if (!IS_FASTOPEN(tp->t_flags))
2467dbc42409SLawrence Stewart 				cc_conn_init(tp);
24689077f387SGleb Smirnoff 			tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp));
24697ff19458SPaul Traina 		}
2470a0292f23SGarrett Wollman 		/*
2471b2ade6b1SRichard Scheffenegger 		 * Account for the ACK of our SYN prior to
2472b2ade6b1SRichard Scheffenegger 		 * regular ACK processing below, except for
2473b2ade6b1SRichard Scheffenegger 		 * simultaneous SYN, which is handled later.
2474b2ade6b1SRichard Scheffenegger 		 */
2475b2ade6b1SRichard Scheffenegger 		if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN))
2476b2ade6b1SRichard Scheffenegger 			incforsyn = 1;
2477b2ade6b1SRichard Scheffenegger 		/*
2478a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
2479a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
2480a0292f23SGarrett Wollman 		 */
2481032bf749SRichard Scheffenegger 		if (tlen == 0 && (thflags & TH_FIN) == 0) {
2482c28440dbSRandall Stewart 			(void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0,
2483a0292f23SGarrett Wollman 			    (struct mbuf *)0);
2484c348e880SGleb Smirnoff 			tcp_handle_wakeup(tp);
2485032bf749SRichard Scheffenegger 		}
248660ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq - 1;
248793b0017fSPhilippe Charnier 		/* FALLTHROUGH */
2488df8bae1dSRodney W. Grimes 
2489df8bae1dSRodney W. Grimes 	/*
2490df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
2491df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
2492fb59c426SYoshinobu Inoue 	 *	tp->snd_una < th->th_ack <= tp->snd_max
2493fb59c426SYoshinobu Inoue 	 * then advance tp->snd_una to th->th_ack and drop
2494df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
2495df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
2496df8bae1dSRodney W. Grimes 	 */
2497df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
2498df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
2499df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
2500df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
2501df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
2502df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
25035a53ca16SPaul Saab 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
250478b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvacktoomuch);
25055a53ca16SPaul Saab 			goto dropafterack;
25065a53ca16SPaul Saab 		}
2507c21b7b55SRichard Scheffenegger 		if (tcp_is_sack_recovery(tp, &to)) {
25080471a8c7SRichard Scheffenegger 			if (((sack_changed = tcp_sack_doack(tp, &to, th->th_ack)) != 0) &&
25090471a8c7SRichard Scheffenegger 			    (tp->t_flags & TF_LRD)) {
25100471a8c7SRichard Scheffenegger 				tcp_sack_lost_retransmission(tp, th);
25110471a8c7SRichard Scheffenegger 			}
25120471a8c7SRichard Scheffenegger 		} else
251312eeb81fSHiren Panchasara 			/*
251412eeb81fSHiren Panchasara 			 * Reset the value so that previous (valid) value
251512eeb81fSHiren Panchasara 			 * from the last ack with SACK doesn't get used.
251612eeb81fSHiren Panchasara 			 */
251712eeb81fSHiren Panchasara 			tp->sackhint.sacked_bytes = 0;
251839bc9de5SLawrence Stewart 
2519bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
252039bc9de5SLawrence Stewart 		/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
252139bc9de5SLawrence Stewart 		hhook_run_tcp_est_in(tp, th, &to);
2522bd79708dSJonathan T. Looney #endif
252339bc9de5SLawrence Stewart 
2524fb59c426SYoshinobu Inoue 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
25250c39d38dSGleb Smirnoff 			maxseg = tcp_maxseg(tp);
2526021eaf79SHiren Panchasara 			if (tlen == 0 &&
2527021eaf79SHiren Panchasara 			    (tiwin == tp->snd_wnd ||
2528021eaf79SHiren Panchasara 			    (tp->t_flags & TF_SACK_PERMIT))) {
252962b90589SPeter Wemm 				/*
253062b90589SPeter Wemm 				 * If this is the first time we've seen a
253162b90589SPeter Wemm 				 * FIN from the remote, this is not a
253262b90589SPeter Wemm 				 * duplicate and it needs to be processed
253362b90589SPeter Wemm 				 * normally.  This happens during a
253462b90589SPeter Wemm 				 * simultaneous close.
253562b90589SPeter Wemm 				 */
253662b90589SPeter Wemm 				if ((thflags & TH_FIN) &&
253762b90589SPeter Wemm 				    (TCPS_HAVERCVDFIN(tp->t_state) == 0)) {
253862b90589SPeter Wemm 					tp->t_dupacks = 0;
253962b90589SPeter Wemm 					break;
254062b90589SPeter Wemm 				}
254178b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvdupack);
2542df8bae1dSRodney W. Grimes 				/*
2543df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
2544df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
2545df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
25465f30ec9bSEitan Adler 				 * change and FIN isn't set),
25475f30ec9bSEitan Adler 				 * the ack is the biggest we've
2548df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
2549a4641f4eSPedro F. Giffuni 				 * threshold of them, assume a packet
2550df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
2551df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
2552df8bae1dSRodney W. Grimes 				 * window so we send only this one
2553df8bae1dSRodney W. Grimes 				 * packet.
2554df8bae1dSRodney W. Grimes 				 *
2555df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
2556df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
2557df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
2558df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
2559df8bae1dSRodney W. Grimes 				 * the new ssthresh).
2560df8bae1dSRodney W. Grimes 				 *
2561df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
2562df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
2563df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
2564df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
2565df8bae1dSRodney W. Grimes 				 * network.
2566f2512ba1SRui Paulo 				 *
2567f2512ba1SRui Paulo 				 * When using TCP ECN, notify the peer that
2568f2512ba1SRui Paulo 				 * we reduced the cwnd.
2569df8bae1dSRodney W. Grimes 				 */
2570021eaf79SHiren Panchasara 				/*
2571021eaf79SHiren Panchasara 				 * Following 2 kinds of acks should not affect
2572021eaf79SHiren Panchasara 				 * dupack counting:
2573021eaf79SHiren Panchasara 				 * 1) Old acks
2574021eaf79SHiren Panchasara 				 * 2) Acks with SACK but without any new SACK
2575021eaf79SHiren Panchasara 				 * information in them. These could result from
2576021eaf79SHiren Panchasara 				 * any anomaly in the network like a switch
2577021eaf79SHiren Panchasara 				 * duplicating packets or a possible DoS attack.
2578021eaf79SHiren Panchasara 				 */
2579021eaf79SHiren Panchasara 				if (th->th_ack != tp->snd_una ||
2580c21b7b55SRichard Scheffenegger 				    (tcp_is_sack_recovery(tp, &to) &&
2581*49a6fbe3SRichard Scheffenegger 				    (sack_changed == SACK_NOCHANGE)))
2582021eaf79SHiren Panchasara 					break;
2583021eaf79SHiren Panchasara 				else if (!tcp_timer_active(tp, TT_REXMT))
2584df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
2585cb942153SJeffrey Hsu 				else if (++tp->t_dupacks > tcprexmtthresh ||
2586dbc42409SLawrence Stewart 				     IN_FASTRECOVERY(tp->t_flags)) {
25874b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
25884b7b743cSLawrence Stewart 					    CC_DUPACK);
25890e1d7c25SRichard Scheffenegger 					if (V_tcp_do_prr &&
2590*49a6fbe3SRichard Scheffenegger 					    IN_FASTRECOVERY(tp->t_flags) &&
2591*49a6fbe3SRichard Scheffenegger 					    (tp->t_flags & TF_SACK_PERMIT)) {
2592*49a6fbe3SRichard Scheffenegger 						tcp_do_prr_ack(tp, th, &to, sack_changed);
2593c21b7b55SRichard Scheffenegger 					} else if (tcp_is_sack_recovery(tp, &to) &&
2594dbc42409SLawrence Stewart 					    IN_FASTRECOVERY(tp->t_flags)) {
2595808f11b7SPaul Saab 						int awnd;
259625e6f9edSPaul Saab 
259725e6f9edSPaul Saab 						/*
259825e6f9edSPaul Saab 						 * Compute the amount of data in flight first.
25994c3972f0SHiren Panchasara 						 * We can inject new data into the pipe iff
260025e6f9edSPaul Saab 						 * we have less than 1/2 the original window's
260125e6f9edSPaul Saab 						 * worth of data in flight.
260225e6f9edSPaul Saab 						 */
2603d1de2b05SRichard Scheffenegger 						if (V_tcp_do_newsack)
260412eeb81fSHiren Panchasara 							awnd = tcp_compute_pipe(tp);
260512eeb81fSHiren Panchasara 						else
2606808f11b7SPaul Saab 							awnd = (tp->snd_nxt - tp->snd_fack) +
26070077b016SPaul Saab 								tp->sackhint.sack_bytes_rexmit;
260812eeb81fSHiren Panchasara 
2609808f11b7SPaul Saab 						if (awnd < tp->snd_ssthresh) {
26100c39d38dSGleb Smirnoff 							tp->snd_cwnd += maxseg;
261125e6f9edSPaul Saab 							if (tp->snd_cwnd > tp->snd_ssthresh)
261225e6f9edSPaul Saab 								tp->snd_cwnd = tp->snd_ssthresh;
261325e6f9edSPaul Saab 						}
261425e6f9edSPaul Saab 					} else
26150c39d38dSGleb Smirnoff 						tp->snd_cwnd += maxseg;
261640fa3e40SGleb Smirnoff 					(void) tcp_output(tp);
261746f58482SJonathan Lemon 					goto drop;
26183c40e1d5SRichard Scheffenegger 				} else if (tp->t_dupacks == tcprexmtthresh ||
26193c40e1d5SRichard Scheffenegger 					    (tp->t_flags & TF_SACK_PERMIT &&
2620d1de2b05SRichard Scheffenegger 					     V_tcp_do_newsack &&
26213c40e1d5SRichard Scheffenegger 					     tp->sackhint.sacked_bytes >
26223c40e1d5SRichard Scheffenegger 					     (tcprexmtthresh - 1) * maxseg)) {
26233c40e1d5SRichard Scheffenegger enter_recovery:
26243c40e1d5SRichard Scheffenegger 					/*
26253c40e1d5SRichard Scheffenegger 					 * Above is the RFC6675 trigger condition of
26263c40e1d5SRichard Scheffenegger 					 * more than (dupthresh-1)*maxseg sacked data.
26273c40e1d5SRichard Scheffenegger 					 * If the count of holes in the
26283c40e1d5SRichard Scheffenegger 					 * scoreboard is >= dupthresh, we could
26293c40e1d5SRichard Scheffenegger 					 * also enter loss recovery, but don't
26303c40e1d5SRichard Scheffenegger 					 * have that value readily available.
26313c40e1d5SRichard Scheffenegger 					 */
26323c40e1d5SRichard Scheffenegger 					tp->t_dupacks = tcprexmtthresh;
2633cb942153SJeffrey Hsu 					tcp_seq onxt = tp->snd_nxt;
2634a0445c2eSJayanth Vijayaraghavan 
2635a0445c2eSJayanth Vijayaraghavan 					/*
26360e1d7c25SRichard Scheffenegger 					 * If we're doing sack, or prr, check
26370e1d7c25SRichard Scheffenegger 					 * to see if we're already in sack
2638a0445c2eSJayanth Vijayaraghavan 					 * recovery. If we're not doing sack,
2639a0445c2eSJayanth Vijayaraghavan 					 * check to see if we're in newreno
2640a0445c2eSJayanth Vijayaraghavan 					 * recovery.
2641a0445c2eSJayanth Vijayaraghavan 					 */
26420e1d7c25SRichard Scheffenegger 					if (V_tcp_do_prr ||
26430e1d7c25SRichard Scheffenegger 					    (tp->t_flags & TF_SACK_PERMIT)) {
2644dbc42409SLawrence Stewart 						if (IN_FASTRECOVERY(tp->t_flags)) {
2645a0445c2eSJayanth Vijayaraghavan 							tp->t_dupacks = 0;
2646a0445c2eSJayanth Vijayaraghavan 							break;
2647a0445c2eSJayanth Vijayaraghavan 						}
2648dbc42409SLawrence Stewart 					} else {
2649a0445c2eSJayanth Vijayaraghavan 						if (SEQ_LEQ(th->th_ack,
26509d11646dSJeffrey Hsu 						    tp->snd_recover)) {
2651cb942153SJeffrey Hsu 							tp->t_dupacks = 0;
2652cb942153SJeffrey Hsu 							break;
265346f58482SJonathan Lemon 						}
2654a0445c2eSJayanth Vijayaraghavan 					}
2655dbc42409SLawrence Stewart 					/* Congestion signal before ack. */
2656dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_NDUPACK);
26574b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
26584b7b743cSLawrence Stewart 					    CC_DUPACK);
2659b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
26609b8b58e0SJonathan Lemon 					tp->t_rtttime = 0;
26610e1d7c25SRichard Scheffenegger 					if (V_tcp_do_prr) {
26620e1d7c25SRichard Scheffenegger 						/*
26630e1d7c25SRichard Scheffenegger 						 * snd_ssthresh is already updated by
26640e1d7c25SRichard Scheffenegger 						 * cc_cong_signal.
26650e1d7c25SRichard Scheffenegger 						 */
2666c21b7b55SRichard Scheffenegger 						if (tcp_is_sack_recovery(tp, &to)) {
2667*49a6fbe3SRichard Scheffenegger 							/*
2668*49a6fbe3SRichard Scheffenegger 							 * Exclude Limited Transmit
2669*49a6fbe3SRichard Scheffenegger 							 * segments here
2670*49a6fbe3SRichard Scheffenegger 							 */
2671e9071000SRichard Scheffenegger 							tp->sackhint.prr_delivered =
2672*49a6fbe3SRichard Scheffenegger 							    maxseg;
267374d7fc87SRichard Scheffenegger 						} else {
267474d7fc87SRichard Scheffenegger 							tp->sackhint.prr_delivered =
267574d7fc87SRichard Scheffenegger 							    imin(tp->snd_max - tp->snd_una,
267674d7fc87SRichard Scheffenegger 							    imin(INT_MAX / 65536,
267774d7fc87SRichard Scheffenegger 								tp->t_dupacks) * maxseg);
267874d7fc87SRichard Scheffenegger 						}
2679bc7ee8e5SRichard Scheffenegger 						tp->sackhint.recover_fs = max(1,
2680bc7ee8e5SRichard Scheffenegger 						    tp->snd_nxt - tp->snd_una);
26810e1d7c25SRichard Scheffenegger 					}
2682c21b7b55SRichard Scheffenegger 					if (tcp_is_sack_recovery(tp, &to)) {
268378b50714SRobert Watson 						TCPSTAT_INC(
268478b50714SRobert Watson 						    tcps_sack_recovery_episode);
2685a3574665SMichael Tuexen 						tp->snd_recover = tp->snd_nxt;
26860c39d38dSGleb Smirnoff 						tp->snd_cwnd = maxseg;
268740fa3e40SGleb Smirnoff 						(void) tcp_output(tp);
26883c40e1d5SRichard Scheffenegger 						if (SEQ_GT(th->th_ack, tp->snd_una))
26893c40e1d5SRichard Scheffenegger 							goto resume_partialack;
26906d90faf3SPaul Saab 						goto drop;
26916d90faf3SPaul Saab 					}
2692fb59c426SYoshinobu Inoue 					tp->snd_nxt = th->th_ack;
26930c39d38dSGleb Smirnoff 					tp->snd_cwnd = maxseg;
269440fa3e40SGleb Smirnoff 					(void) tcp_output(tp);
269548d2549cSJeffrey Hsu 					KASSERT(tp->snd_limited <= 2,
2696302ce8d6SAndre Oppermann 					    ("%s: tp->snd_limited too big",
2697302ce8d6SAndre Oppermann 					    __func__));
2698df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
26990c39d38dSGleb Smirnoff 					     maxseg *
270048d2549cSJeffrey Hsu 					     (tp->t_dupacks - tp->snd_limited);
2701df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
2702df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
2703df8bae1dSRodney W. Grimes 					goto drop;
2704603724d3SBjoern A. Zeeb 				} else if (V_tcp_do_rfc3042) {
270562d4443fSHiren Panchasara 					/*
270662d4443fSHiren Panchasara 					 * Process first and second duplicate
270762d4443fSHiren Panchasara 					 * ACKs. Each indicates a segment
270862d4443fSHiren Panchasara 					 * leaving the network, creating room
270962d4443fSHiren Panchasara 					 * for more. Make sure we can send a
271062d4443fSHiren Panchasara 					 * packet on reception of each duplicate
271162d4443fSHiren Panchasara 					 * ACK by increasing snd_cwnd by one
271262d4443fSHiren Panchasara 					 * segment. Restore the original
271362d4443fSHiren Panchasara 					 * snd_cwnd after packet transmission.
271462d4443fSHiren Panchasara 					 */
27154b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
27164b7b743cSLawrence Stewart 					    CC_DUPACK);
27173ac12506SJonathan T. Looney 					uint32_t oldcwnd = tp->snd_cwnd;
271848d2549cSJeffrey Hsu 					tcp_seq oldsndmax = tp->snd_max;
271948d2549cSJeffrey Hsu 					u_int sent;
27205628dd08SAndre Oppermann 					int avail;
272189c02376SJeffrey Hsu 
2722582a954bSJeffrey Hsu 					KASSERT(tp->t_dupacks == 1 ||
2723582a954bSJeffrey Hsu 					    tp->t_dupacks == 2,
2724302ce8d6SAndre Oppermann 					    ("%s: dupacks not 1 or 2",
2725302ce8d6SAndre Oppermann 					    __func__));
272661a36e3dSJeffrey Hsu 					if (tp->t_dupacks == 1)
272748d2549cSJeffrey Hsu 						tp->snd_limited = 0;
272861a36e3dSJeffrey Hsu 					tp->snd_cwnd =
272961a36e3dSJeffrey Hsu 					    (tp->snd_nxt - tp->snd_una) +
273061a36e3dSJeffrey Hsu 					    (tp->t_dupacks - tp->snd_limited) *
27310c39d38dSGleb Smirnoff 					    maxseg;
27325628dd08SAndre Oppermann 					/*
27335628dd08SAndre Oppermann 					 * Only call tcp_output when there
27345ae83e0dSMichael Tuexen 					 * is new data available to be sent
27355ae83e0dSMichael Tuexen 					 * or we need to send an ACK.
27365628dd08SAndre Oppermann 					 */
27375628dd08SAndre Oppermann 					SOCKBUF_LOCK(&so->so_snd);
2738cfa6009eSGleb Smirnoff 					avail = sbavail(&so->so_snd) -
27395628dd08SAndre Oppermann 					    (tp->snd_nxt - tp->snd_una);
27405628dd08SAndre Oppermann 					SOCKBUF_UNLOCK(&so->so_snd);
27415ae83e0dSMichael Tuexen 					if (avail > 0 || tp->t_flags & TF_ACKNOW)
274240fa3e40SGleb Smirnoff 						(void) tcp_output(tp);
274348d2549cSJeffrey Hsu 					sent = tp->snd_max - oldsndmax;
27440c39d38dSGleb Smirnoff 					if (sent > maxseg) {
274589c02376SJeffrey Hsu 						KASSERT((tp->t_dupacks == 2 &&
274689c02376SJeffrey Hsu 						    tp->snd_limited == 0) ||
27470c39d38dSGleb Smirnoff 						   (sent == maxseg + 1 &&
274889c02376SJeffrey Hsu 						    tp->t_flags & TF_SENTFIN),
2749302ce8d6SAndre Oppermann 						    ("%s: sent too much",
2750302ce8d6SAndre Oppermann 						    __func__));
275148d2549cSJeffrey Hsu 						tp->snd_limited = 2;
275248d2549cSJeffrey Hsu 					} else if (sent > 0)
275348d2549cSJeffrey Hsu 						++tp->snd_limited;
2754582a954bSJeffrey Hsu 					tp->snd_cwnd = oldcwnd;
2755582a954bSJeffrey Hsu 					goto drop;
2756df8bae1dSRodney W. Grimes 				}
2757021eaf79SHiren Panchasara 			}
2758df8bae1dSRodney W. Grimes 			break;
2759021eaf79SHiren Panchasara 		} else {
2760021eaf79SHiren Panchasara 			/*
2761021eaf79SHiren Panchasara 			 * This ack is advancing the left edge, reset the
2762021eaf79SHiren Panchasara 			 * counter.
2763021eaf79SHiren Panchasara 			 */
2764021eaf79SHiren Panchasara 			tp->t_dupacks = 0;
2765021eaf79SHiren Panchasara 			/*
2766021eaf79SHiren Panchasara 			 * If this ack also has new SACK info, increment the
2767f359d6ebSRichard Scheffenegger 			 * counter as per rfc6675. The variable
2768f359d6ebSRichard Scheffenegger 			 * sack_changed tracks all changes to the SACK
2769f359d6ebSRichard Scheffenegger 			 * scoreboard, including when partial ACKs without
2770f359d6ebSRichard Scheffenegger 			 * SACK options are received, and clear the scoreboard
2771f359d6ebSRichard Scheffenegger 			 * from the left side. Such partial ACKs should not be
2772f359d6ebSRichard Scheffenegger 			 * counted as dupacks here.
2773021eaf79SHiren Panchasara 			 */
2774c21b7b55SRichard Scheffenegger 			if (tcp_is_sack_recovery(tp, &to) &&
2775*49a6fbe3SRichard Scheffenegger 			    (sack_changed != SACK_NOCHANGE)) {
2776021eaf79SHiren Panchasara 				tp->t_dupacks++;
27773c40e1d5SRichard Scheffenegger 				/* limit overhead by setting maxseg last */
27783c40e1d5SRichard Scheffenegger 				if (!IN_FASTRECOVERY(tp->t_flags) &&
27793c40e1d5SRichard Scheffenegger 				    (tp->sackhint.sacked_bytes >
27803c40e1d5SRichard Scheffenegger 				    ((tcprexmtthresh - 1) *
27813c40e1d5SRichard Scheffenegger 				    (maxseg = tcp_maxseg(tp))))) {
27823c40e1d5SRichard Scheffenegger 					goto enter_recovery;
27833c40e1d5SRichard Scheffenegger 				}
27843c40e1d5SRichard Scheffenegger 			}
2785df8bae1dSRodney W. Grimes 		}
2786cb942153SJeffrey Hsu 
27873c40e1d5SRichard Scheffenegger resume_partialack:
2788302ce8d6SAndre Oppermann 		KASSERT(SEQ_GT(th->th_ack, tp->snd_una),
2789302ce8d6SAndre Oppermann 		    ("%s: th_ack <= snd_una", __func__));
2790cb942153SJeffrey Hsu 
2791df8bae1dSRodney W. Grimes 		/*
2792df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
2793df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
2794df8bae1dSRodney W. Grimes 		 */
2795dbc42409SLawrence Stewart 		if (IN_FASTRECOVERY(tp->t_flags)) {
2796cb942153SJeffrey Hsu 			if (SEQ_LT(th->th_ack, tp->snd_recover)) {
27973529149eSAndre Oppermann 				if (tp->t_flags & TF_SACK_PERMIT)
2798eb3a59a8SRichard Scheffenegger 					if (V_tcp_do_prr && to.to_flags & TOF_SACK) {
2799eb3a59a8SRichard Scheffenegger 						tcp_timer_activate(tp, TT_REXMT, 0);
2800eb3a59a8SRichard Scheffenegger 						tp->t_rtttime = 0;
2801*49a6fbe3SRichard Scheffenegger 						tcp_do_prr_ack(tp, th, &to, sack_changed);
2802eb3a59a8SRichard Scheffenegger 						tp->t_flags |= TF_ACKNOW;
2803eb3a59a8SRichard Scheffenegger 						(void) tcp_output(tp);
2804eb3a59a8SRichard Scheffenegger 					} else
28056d90faf3SPaul Saab 						tcp_sack_partialack(tp, th);
28066d90faf3SPaul Saab 				else
2807c068736aSJeffrey Hsu 					tcp_newreno_partial_ack(tp, th);
2808dbc42409SLawrence Stewart 			} else
2809dbc42409SLawrence Stewart 				cc_post_recovery(tp, th);
2810b9f803b7SRichard Scheffenegger 		} else if (IN_CONGRECOVERY(tp->t_flags)) {
2811b9f803b7SRichard Scheffenegger 			if (SEQ_LT(th->th_ack, tp->snd_recover)) {
2812b9f803b7SRichard Scheffenegger 				if (V_tcp_do_prr) {
2813b9f803b7SRichard Scheffenegger 					tp->sackhint.delivered_data = BYTES_THIS_ACK(tp, th);
2814b9f803b7SRichard Scheffenegger 					tp->snd_fack = th->th_ack;
2815*49a6fbe3SRichard Scheffenegger 					/*
2816*49a6fbe3SRichard Scheffenegger 					 * During ECN cwnd reduction
2817*49a6fbe3SRichard Scheffenegger 					 * always use PRR-SSRB
2818*49a6fbe3SRichard Scheffenegger 					 */
2819*49a6fbe3SRichard Scheffenegger 					tcp_do_prr_ack(tp, th, &to, SACK_CHANGE);
2820b9f803b7SRichard Scheffenegger 					(void) tcp_output(tp);
2821b9f803b7SRichard Scheffenegger 				}
2822b9f803b7SRichard Scheffenegger 			} else
2823b9f803b7SRichard Scheffenegger 				cc_post_recovery(tp, th);
282446f58482SJonathan Lemon 		}
2825a0292f23SGarrett Wollman 		/*
2826a0292f23SGarrett Wollman 		 * If we reach this point, ACK is not a duplicate,
2827a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
2828a0292f23SGarrett Wollman 		 */
2829a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
2830a0292f23SGarrett Wollman 			/*
2831a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
2832a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
283307e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
283407e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
283507e43e10SAndras Olah 			 * we can do window scaling.
2836a0292f23SGarrett Wollman 			 */
2837a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
2838a0292f23SGarrett Wollman 			tp->snd_una++;
283907e43e10SAndras Olah 			/* Do window scaling? */
284007e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
284107e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
284207e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2843464fcfbcSAndre Oppermann 				/* Send window already scaled. */
284407e43e10SAndras Olah 			}
2845a0292f23SGarrett Wollman 		}
2846a0292f23SGarrett Wollman 
2847a0292f23SGarrett Wollman process_ACK:
28489eb0e832SGleb Smirnoff 		INP_WLOCK_ASSERT(inp);
28497cfc6904SRobert Watson 
2850b2ade6b1SRichard Scheffenegger 		/*
2851b2ade6b1SRichard Scheffenegger 		 * Adjust for the SYN bit in sequence space,
2852b2ade6b1SRichard Scheffenegger 		 * but don't account for it in cwnd calculations.
2853b2ade6b1SRichard Scheffenegger 		 * This is for the SYN_RECEIVED, non-simultaneous
2854b2ade6b1SRichard Scheffenegger 		 * SYN case. SYN_SENT and simultaneous SYN are
2855b2ade6b1SRichard Scheffenegger 		 * treated elsewhere.
2856b2ade6b1SRichard Scheffenegger 		 */
2857b2ade6b1SRichard Scheffenegger 		if (incforsyn)
2858b2ade6b1SRichard Scheffenegger 			tp->snd_una++;
2859dbc42409SLawrence Stewart 		acked = BYTES_THIS_ACK(tp, th);
2860b8c2cd15SJonathan T. Looney 		KASSERT(acked >= 0, ("%s: acked unexepectedly negative "
2861b8c2cd15SJonathan T. Looney 		    "(tp->snd_una=%u, th->th_ack=%u, tp=%p, m=%p)", __func__,
2862b8c2cd15SJonathan T. Looney 		    tp->snd_una, th->th_ack, tp, m));
28634b7b743cSLawrence Stewart 		TCPSTAT_ADD(tcps_rcvackpack, nsegs);
286478b50714SRobert Watson 		TCPSTAT_ADD(tcps_rcvackbyte, acked);
2865df8bae1dSRodney W. Grimes 
2866df8bae1dSRodney W. Grimes 		/*
28679b8b58e0SJonathan Lemon 		 * If we just performed our first retransmit, and the ACK
28689b8b58e0SJonathan Lemon 		 * arrives within our recovery window, then it was a mistake
28699b8b58e0SJonathan Lemon 		 * to do the retransmit in the first place.  Recover our
28709b8b58e0SJonathan Lemon 		 * original cwnd and ssthresh, and proceed to transmit where
28719b8b58e0SJonathan Lemon 		 * we left off.
28729b8b58e0SJonathan Lemon 		 */
287310d20c84SMatt Macy 		if (tp->t_rxtshift == 1 &&
287410d20c84SMatt Macy 		    tp->t_flags & TF_PREVVALID &&
28754531b345SRichard Scheffenegger 		    tp->t_badrxtwin != 0 &&
28764531b345SRichard Scheffenegger 		    to.to_flags & TOF_TS &&
28774531b345SRichard Scheffenegger 		    to.to_tsecr != 0 &&
28784531b345SRichard Scheffenegger 		    TSTMP_LT(to.to_tsecr, tp->t_badrxtwin))
2879dbc42409SLawrence Stewart 			cc_cong_signal(tp, th, CC_RTO_ERR);
28809b8b58e0SJonathan Lemon 
28819b8b58e0SJonathan Lemon 		/*
2882df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
2883df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
2884df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
2885df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
2886df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
2887df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
2888df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
2889fa55172bSMatthew Dillon 		 *
2890fa55172bSMatthew Dillon 		 * Some boxes send broken timestamp replies
2891fa55172bSMatthew Dillon 		 * during the SYN+ACK phase, ignore
2892fa55172bSMatthew Dillon 		 * timestamps of 0 or we could calculate a
2893fa55172bSMatthew Dillon 		 * huge RTT and blow up the retransmit timer.
2894df8bae1dSRodney W. Grimes 		 */
2895d8951c8aSBjoern A. Zeeb 		if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) {
28963ac12506SJonathan T. Looney 			uint32_t t;
2897d8951c8aSBjoern A. Zeeb 
2898d8951c8aSBjoern A. Zeeb 			t = tcp_ts_getticks() - to.to_tsecr;
2899d8951c8aSBjoern A. Zeeb 			if (!tp->t_rttlow || tp->t_rttlow > t)
2900d8951c8aSBjoern A. Zeeb 				tp->t_rttlow = t;
2901d8951c8aSBjoern A. Zeeb 			tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1);
2902fa55172bSMatthew Dillon 		} else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
2903eaf80179SAndre Oppermann 			if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime)
2904eaf80179SAndre Oppermann 				tp->t_rttlow = ticks - tp->t_rtttime;
29059b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
2906fa55172bSMatthew Dillon 		}
2907df8bae1dSRodney W. Grimes 
290808af8aacSRandall Stewart 		SOCKBUF_LOCK(&so->so_snd);
290908af8aacSRandall Stewart 		/*
291008af8aacSRandall Stewart 		 * Clear t_acktime if remote side has ACKd all data in the
291108af8aacSRandall Stewart 		 * socket buffer and FIN (if applicable).
291208af8aacSRandall Stewart 		 * Otherwise, update t_acktime if we received a sufficiently
291308af8aacSRandall Stewart 		 * large ACK.
291408af8aacSRandall Stewart 		 */
291508af8aacSRandall Stewart 		if ((tp->t_state <= TCPS_CLOSE_WAIT &&
291608af8aacSRandall Stewart 		    acked == sbavail(&so->so_snd)) ||
291708af8aacSRandall Stewart 		    acked > sbavail(&so->so_snd))
291808af8aacSRandall Stewart 			tp->t_acktime = 0;
291908af8aacSRandall Stewart 		else if (acked > 1)
292008af8aacSRandall Stewart 			tp->t_acktime = ticks;
292108af8aacSRandall Stewart 
2922df8bae1dSRodney W. Grimes 		/*
2923df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
2924df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
2925df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
2926df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
2927df8bae1dSRodney W. Grimes 		 */
2928fb59c426SYoshinobu Inoue 		if (th->th_ack == tp->snd_max) {
2929b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
2930df8bae1dSRodney W. Grimes 			needoutput = 1;
2931b8152ba7SAndre Oppermann 		} else if (!tcp_timer_active(tp, TT_PERSIST))
293208af8aacSRandall Stewart 			tcp_timer_activate(tp, TT_REXMT, TP_RXTCUR(tp));
2933a0292f23SGarrett Wollman 
2934a0292f23SGarrett Wollman 		/*
2935a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
2936a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
2937a0292f23SGarrett Wollman 		 */
293808af8aacSRandall Stewart 		if (acked == 0) {
293908af8aacSRandall Stewart 			SOCKBUF_UNLOCK(&so->so_snd);
2940a0292f23SGarrett Wollman 			goto step6;
294108af8aacSRandall Stewart 		}
2942a0292f23SGarrett Wollman 
2943df8bae1dSRodney W. Grimes 		/*
2944dbc42409SLawrence Stewart 		 * Let the congestion control algorithm update congestion
2945dbc42409SLawrence Stewart 		 * control related information. This typically means increasing
2946dbc42409SLawrence Stewart 		 * the congestion window.
2947df8bae1dSRodney W. Grimes 		 */
29484b7b743cSLawrence Stewart 		cc_ack_received(tp, th, nsegs, CC_ACK);
2949dbc42409SLawrence Stewart 
2950cfa6009eSGleb Smirnoff 		if (acked > sbavail(&so->so_snd)) {
2951b8c2cd15SJonathan T. Looney 			if (tp->snd_wnd >= sbavail(&so->so_snd))
2952cfa6009eSGleb Smirnoff 				tp->snd_wnd -= sbavail(&so->so_snd);
2953b8c2cd15SJonathan T. Looney 			else
2954b8c2cd15SJonathan T. Looney 				tp->snd_wnd = 0;
2955c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd,
2956cfa6009eSGleb Smirnoff 			    (int)sbavail(&so->so_snd));
2957df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
2958df8bae1dSRodney W. Grimes 		} else {
2959c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd, acked);
29603ac12506SJonathan T. Looney 			if (tp->snd_wnd >= (uint32_t) acked)
296160ee3bb2SAndre Oppermann 				tp->snd_wnd -= acked;
2962b8c2cd15SJonathan T. Looney 			else
2963b8c2cd15SJonathan T. Looney 				tp->snd_wnd = 0;
2964df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
2965df8bae1dSRodney W. Grimes 		}
2966032bf749SRichard Scheffenegger 		/* NB: sowwakeup_locked() does an implicit unlock. */
2967032bf749SRichard Scheffenegger 		sowwakeup_locked(so);
2968c11a15bfSGleb Smirnoff 		m_freem(mfree);
2969e8949f74SAndre Oppermann 		/* Detect una wraparound. */
2970dbc42409SLawrence Stewart 		if (!IN_RECOVERY(tp->t_flags) &&
29719d11646dSJeffrey Hsu 		    SEQ_GT(tp->snd_una, tp->snd_recover) &&
29729d11646dSJeffrey Hsu 		    SEQ_LEQ(th->th_ack, tp->snd_recover))
29739d11646dSJeffrey Hsu 			tp->snd_recover = th->th_ack - 1;
2974dbc42409SLawrence Stewart 		/* XXXLAS: Can this be moved up into cc_post_recovery? */
2975dbc42409SLawrence Stewart 		if (IN_RECOVERY(tp->t_flags) &&
297624cb0f22SLawrence Stewart 		    SEQ_GEQ(th->th_ack, tp->snd_recover)) {
2977dbc42409SLawrence Stewart 			EXIT_RECOVERY(tp->t_flags);
297824cb0f22SLawrence Stewart 		}
2979fb59c426SYoshinobu Inoue 		tp->snd_una = th->th_ack;
29803529149eSAndre Oppermann 		if (tp->t_flags & TF_SACK_PERMIT) {
29816d90faf3SPaul Saab 			if (SEQ_GT(tp->snd_una, tp->snd_recover))
29826d90faf3SPaul Saab 				tp->snd_recover = tp->snd_una;
29836d90faf3SPaul Saab 		}
2984df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2985df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
2986df8bae1dSRodney W. Grimes 
2987df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2988df8bae1dSRodney W. Grimes 		/*
2989df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
2990df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
2991df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
2992df8bae1dSRodney W. Grimes 		 */
2993df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2994df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2995df8bae1dSRodney W. Grimes 				/*
2996df8bae1dSRodney W. Grimes 				 * If we can't receive any more
2997df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
2998df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
2999df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
3000df8bae1dSRodney W. Grimes 				 * we'll hang forever.
3001340c35deSJonathan Lemon 				 */
3002c0b99ffaSRobert Watson 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
300303e49181SSeigo Tanimura 					soisdisconnected(so);
30049077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
30059077f387SGleb Smirnoff 					    (tcp_fast_finwait2_recycle ?
30069077f387SGleb Smirnoff 					    tcp_finwait2_timeout :
30079077f387SGleb Smirnoff 					    TP_MAXIDLE(tp)));
30084cc20ab1SSeigo Tanimura 				}
300957f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_2);
3010df8bae1dSRodney W. Grimes 			}
3011df8bae1dSRodney W. Grimes 			break;
3012df8bae1dSRodney W. Grimes 
3013df8bae1dSRodney W. Grimes 		/*
3014df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
3015df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
3016df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
3017df8bae1dSRodney W. Grimes 		 * the segment.
3018df8bae1dSRodney W. Grimes 		 */
3019df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
3020df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
302111a20fb8SJeffrey Hsu 				tcp_twstart(tp);
3022340c35deSJonathan Lemon 				m_freem(m);
3023679d9708SAndre Oppermann 				return;
3024df8bae1dSRodney W. Grimes 			}
3025df8bae1dSRodney W. Grimes 			break;
3026df8bae1dSRodney W. Grimes 
3027df8bae1dSRodney W. Grimes 		/*
3028df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
3029df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
3030df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
3031df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
3032df8bae1dSRodney W. Grimes 		 */
3033df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
3034df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
3035df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
3036df8bae1dSRodney W. Grimes 				goto drop;
3037df8bae1dSRodney W. Grimes 			}
3038df8bae1dSRodney W. Grimes 			break;
3039df8bae1dSRodney W. Grimes 		}
3040df8bae1dSRodney W. Grimes 	}
3041df8bae1dSRodney W. Grimes 
3042df8bae1dSRodney W. Grimes step6:
30439eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
30447cfc6904SRobert Watson 
3045df8bae1dSRodney W. Grimes 	/*
304660ee3bb2SAndre Oppermann 	 * Update window information.
304760ee3bb2SAndre Oppermann 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
3048df8bae1dSRodney W. Grimes 	 */
304960ee3bb2SAndre Oppermann 	if ((thflags & TH_ACK) &&
305060ee3bb2SAndre Oppermann 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
305160ee3bb2SAndre Oppermann 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
305260ee3bb2SAndre Oppermann 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
305360ee3bb2SAndre Oppermann 		/* keep track of pure window updates */
305460ee3bb2SAndre Oppermann 		if (tlen == 0 &&
305560ee3bb2SAndre Oppermann 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
305678b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvwinupd);
3057df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
305860ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq;
305960ee3bb2SAndre Oppermann 		tp->snd_wl2 = th->th_ack;
3060df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
3061df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
306260ee3bb2SAndre Oppermann 		needoutput = 1;
3063df8bae1dSRodney W. Grimes 	}
3064df8bae1dSRodney W. Grimes 
3065df8bae1dSRodney W. Grimes 	/*
3066df8bae1dSRodney W. Grimes 	 * Process segments with URG.
3067df8bae1dSRodney W. Grimes 	 */
3068fb59c426SYoshinobu Inoue 	if ((thflags & TH_URG) && th->th_urp &&
3069df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
3070df8bae1dSRodney W. Grimes 		/*
3071df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
3072df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
3073df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
3074df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
3075df8bae1dSRodney W. Grimes 		 */
307618ad5842SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
3077cfa6009eSGleb Smirnoff 		if (th->th_urp + sbavail(&so->so_rcv) > sb_max) {
3078fb59c426SYoshinobu Inoue 			th->th_urp = 0;			/* XXX */
3079fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;		/* XXX */
308018ad5842SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);	/* XXX */
3081df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
3082df8bae1dSRodney W. Grimes 		}
3083df8bae1dSRodney W. Grimes 		/*
3084df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
3085df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
3086df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
3087df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
3088df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
3089df8bae1dSRodney W. Grimes 		 *
3090df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
3091df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
3092df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
3093df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
3094df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
3095df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
3096df8bae1dSRodney W. Grimes 		 */
3097fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
3098fb59c426SYoshinobu Inoue 			tp->rcv_up = th->th_seq + th->th_urp;
3099cfa6009eSGleb Smirnoff 			so->so_oobmark = sbavail(&so->so_rcv) +
3100df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
3101927c5ceaSRobert Watson 			if (so->so_oobmark == 0)
3102c0b99ffaSRobert Watson 				so->so_rcv.sb_state |= SBS_RCVATMARK;
3103df8bae1dSRodney W. Grimes 			sohasoutofband(so);
3104df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
3105df8bae1dSRodney W. Grimes 		}
310618ad5842SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
3107df8bae1dSRodney W. Grimes 		/*
3108df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
3109df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
3110df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
3111df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
3112df8bae1dSRodney W. Grimes 		 */
31133ac12506SJonathan T. Looney 		if (th->th_urp <= (uint32_t)tlen &&
311430613f56SJeffrey Hsu 		    !(so->so_options & SO_OOBINLINE)) {
311530613f56SJeffrey Hsu 			/* hdr drop is delayed */
311630613f56SJeffrey Hsu 			tcp_pulloutofband(so, th, m, drop_hdrlen);
311730613f56SJeffrey Hsu 		}
3118c068736aSJeffrey Hsu 	} else {
3119df8bae1dSRodney W. Grimes 		/*
3120df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
3121df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
3122df8bae1dSRodney W. Grimes 		 * with the receive window.
3123df8bae1dSRodney W. Grimes 		 */
3124df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
3125df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
3126c068736aSJeffrey Hsu 	}
3127df8bae1dSRodney W. Grimes dodata:							/* XXX */
31289eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
31297cfc6904SRobert Watson 
3130df8bae1dSRodney W. Grimes 	/*
3131df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
3132df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
3133df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
3134df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
3135df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
3136df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
3137df8bae1dSRodney W. Grimes 	 */
3138281a0fd4SPatrick Kelsey 	tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
313968bd7ed1SJonathan T. Looney 		   IS_FASTOPEN(tp->t_flags));
3140f1ea4e41SRandall Stewart 	if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) &&
3141df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
314269e03620SPaul Saab 		tcp_seq save_start = th->th_seq;
31435acfd95cSMichael Tuexen 		tcp_seq save_rnxt  = tp->rcv_nxt;
31445acfd95cSMichael Tuexen 		int     save_tlen  = tlen;
3145fb59c426SYoshinobu Inoue 		m_adj(m, drop_hdrlen);	/* delayed header drop */
3146e4b64281SJesper Skriver 		/*
3147c068736aSJeffrey Hsu 		 * Insert segment which includes th into TCP reassembly queue
3148c068736aSJeffrey Hsu 		 * with control block tp.  Set thflags to whether reassembly now
3149c068736aSJeffrey Hsu 		 * includes a segment with FIN.  This handles the common case
3150c068736aSJeffrey Hsu 		 * inline (segment is the next to be received on an established
3151c068736aSJeffrey Hsu 		 * connection, and the queue is empty), avoiding linkage into
3152c068736aSJeffrey Hsu 		 * and removal from the queue and repetition of various
3153c068736aSJeffrey Hsu 		 * conversions.
3154c068736aSJeffrey Hsu 		 * Set DELACK for segments received in order, but ack
3155c068736aSJeffrey Hsu 		 * immediately when segments are out of order (so
3156c068736aSJeffrey Hsu 		 * fast retransmit can work).
3157e4b64281SJesper Skriver 		 */
31584741bfcbSPatrick Kelsey 		if (th->th_seq == tp->rcv_nxt &&
3159c28440dbSRandall Stewart 		    SEGQ_EMPTY(tp) &&
3160281a0fd4SPatrick Kelsey 		    (TCPS_HAVEESTABLISHED(tp->t_state) ||
3161281a0fd4SPatrick Kelsey 		     tfo_syn)) {
3162281a0fd4SPatrick Kelsey 			if (DELAY_ACK(tp, tlen) || tfo_syn)
31633bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
3164e4b64281SJesper Skriver 			else
3165e4b64281SJesper Skriver 				tp->t_flags |= TF_ACKNOW;
3166e4b64281SJesper Skriver 			tp->rcv_nxt += tlen;
3167e854dd38SRandall Stewart 			if (tlen &&
3168e854dd38SRandall Stewart 			    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
3169e854dd38SRandall Stewart 			    (tp->t_fbyte_in == 0)) {
3170e854dd38SRandall Stewart 				tp->t_fbyte_in = ticks;
3171e854dd38SRandall Stewart 				if (tp->t_fbyte_in == 0)
3172e854dd38SRandall Stewart 					tp->t_fbyte_in = 1;
3173e854dd38SRandall Stewart 				if (tp->t_fbyte_out && tp->t_fbyte_in)
3174e854dd38SRandall Stewart 					tp->t_flags2 |= TF2_FBYTES_COMPLETE;
3175e854dd38SRandall Stewart 			}
31761ebf4607SRichard Scheffenegger 			thflags = tcp_get_flags(th) & TH_FIN;
317778b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpack);
317878b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
31791e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
3180c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
3181c1c36a2cSMike Silbersack 				m_freem(m);
3182c1c36a2cSMike Silbersack 			else
3183651e4e6aSGleb Smirnoff 				sbappendstream_locked(&so->so_rcv, m, 0);
31844d0770f1SRichard Scheffenegger 			tp->t_flags |= TF_WAKESOR;
3185e4b64281SJesper Skriver 		} else {
3186e8949f74SAndre Oppermann 			/*
3187e8949f74SAndre Oppermann 			 * XXX: Due to the header drop above "th" is
3188e8949f74SAndre Oppermann 			 * theoretically invalid by now.  Fortunately
3189e8949f74SAndre Oppermann 			 * m_adj() doesn't actually frees any mbufs
3190e8949f74SAndre Oppermann 			 * when trimming from the head.
3191e8949f74SAndre Oppermann 			 */
31925acfd95cSMichael Tuexen 			tcp_seq temp = save_start;
31934747500dSRandall Stewart 
31945acfd95cSMichael Tuexen 			thflags = tcp_reass(tp, th, &temp, &tlen, m);
3195e4b64281SJesper Skriver 			tp->t_flags |= TF_ACKNOW;
3196e4b64281SJesper Skriver 		}
319740f41eceSMichael Tuexen 		if ((tp->t_flags & TF_SACK_PERMIT) &&
319840f41eceSMichael Tuexen 		    (save_tlen > 0) &&
319940f41eceSMichael Tuexen 		    TCPS_HAVEESTABLISHED(tp->t_state)) {
32006d261981SMichael Tuexen 			if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) {
3201b5a154d8SMichael Tuexen 				/*
3202b5a154d8SMichael Tuexen 				 * DSACK actually handled in the fastpath
3203b5a154d8SMichael Tuexen 				 * above.
3204b5a154d8SMichael Tuexen 				 */
3205ecc5b1d1SMichael Tuexen 				tcp_update_sack_list(tp, save_start,
3206ecc5b1d1SMichael Tuexen 				    save_start + save_tlen);
3207ecc5b1d1SMichael Tuexen 			} else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) {
3208ecc5b1d1SMichael Tuexen 				if ((tp->rcv_numsacks >= 1) &&
3209ecc5b1d1SMichael Tuexen 				    (tp->sackblks[0].end == save_start)) {
32106d261981SMichael Tuexen 					/*
32116d261981SMichael Tuexen 					 * Partial overlap, recorded at todrop
32126d261981SMichael Tuexen 					 * above.
32136d261981SMichael Tuexen 					 */
32146d261981SMichael Tuexen 					tcp_update_sack_list(tp,
32156d261981SMichael Tuexen 					    tp->sackblks[0].start,
3216ecc5b1d1SMichael Tuexen 					    tp->sackblks[0].end);
3217ecc5b1d1SMichael Tuexen 				} else {
3218ecc5b1d1SMichael Tuexen 					tcp_update_dsack_list(tp, save_start,
3219ecc5b1d1SMichael Tuexen 					    save_start + save_tlen);
3220ecc5b1d1SMichael Tuexen 				}
32216d261981SMichael Tuexen 			} else if (tlen >= save_tlen) {
3222b5a154d8SMichael Tuexen 				/* Update of sackblks. */
3223ecc5b1d1SMichael Tuexen 				tcp_update_dsack_list(tp, save_start,
3224ecc5b1d1SMichael Tuexen 				    save_start + save_tlen);
3225ecc5b1d1SMichael Tuexen 			} else if (tlen > 0) {
3226ecc5b1d1SMichael Tuexen 				tcp_update_dsack_list(tp, save_start,
3227ecc5b1d1SMichael Tuexen 				    save_start + tlen);
32285acfd95cSMichael Tuexen 			}
32295acfd95cSMichael Tuexen 		}
3230c348e880SGleb Smirnoff 		tcp_handle_wakeup(tp);
3231302ce8d6SAndre Oppermann #if 0
3232df8bae1dSRodney W. Grimes 		/*
3233df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
3234df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
3235df8bae1dSRodney W. Grimes 		 * buffer size.
3236302ce8d6SAndre Oppermann 		 * XXX: Unused.
3237df8bae1dSRodney W. Grimes 		 */
3238f701e30dSJohn Baldwin 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt))
3239df8bae1dSRodney W. Grimes 			len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
3240f701e30dSJohn Baldwin 		else
3241f701e30dSJohn Baldwin 			len = so->so_rcv.sb_hiwat;
3242302ce8d6SAndre Oppermann #endif
3243df8bae1dSRodney W. Grimes 	} else {
3244df8bae1dSRodney W. Grimes 		m_freem(m);
3245fb59c426SYoshinobu Inoue 		thflags &= ~TH_FIN;
3246df8bae1dSRodney W. Grimes 	}
3247df8bae1dSRodney W. Grimes 
3248df8bae1dSRodney W. Grimes 	/*
3249df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
3250df8bae1dSRodney W. Grimes 	 * that the connection is closing.
3251df8bae1dSRodney W. Grimes 	 */
3252fb59c426SYoshinobu Inoue 	if (thflags & TH_FIN) {
3253df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
32544d0770f1SRichard Scheffenegger 			/* The socket upcall is handled by socantrcvmore. */
3255032bf749SRichard Scheffenegger 			socantrcvmore(so);
3256a0292f23SGarrett Wollman 			/*
3257a0292f23SGarrett Wollman 			 * If connection is half-synchronized
3258d3eede9dSAndras Olah 			 * (ie NEEDSYN flag on) then delay ACK,
3259a0292f23SGarrett Wollman 			 * so it may be piggybacked when SYN is sent.
3260a0292f23SGarrett Wollman 			 * Otherwise, since we received a FIN then no
3261a0292f23SGarrett Wollman 			 * more input can be expected, send ACK now.
3262a0292f23SGarrett Wollman 			 */
32633bfd6421SJonathan Lemon 			if (tp->t_flags & TF_NEEDSYN)
32643bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
3265a0292f23SGarrett Wollman 			else
3266df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
3267df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
3268df8bae1dSRodney W. Grimes 		}
3269df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
3270df8bae1dSRodney W. Grimes 		/*
3271df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
3272df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
3273df8bae1dSRodney W. Grimes 		 */
3274df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
32759b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
32769b8b58e0SJonathan Lemon 			/* FALLTHROUGH */
3277df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
327857f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSE_WAIT);
3279df8bae1dSRodney W. Grimes 			break;
3280df8bae1dSRodney W. Grimes 
3281df8bae1dSRodney W. Grimes 		/*
3282df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
3283df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
3284df8bae1dSRodney W. Grimes 		 */
3285df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
328657f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSING);
3287df8bae1dSRodney W. Grimes 			break;
3288df8bae1dSRodney W. Grimes 
3289df8bae1dSRodney W. Grimes 		/*
3290df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
3291df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
3292df8bae1dSRodney W. Grimes 		 * standard timers.
3293df8bae1dSRodney W. Grimes 		 */
3294df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
3295340c35deSJonathan Lemon 			tcp_twstart(tp);
3296679d9708SAndre Oppermann 			return;
3297df8bae1dSRodney W. Grimes 		}
3298df8bae1dSRodney W. Grimes 	}
32992b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
3300df8bae1dSRodney W. Grimes 
3301df8bae1dSRodney W. Grimes 	/*
3302df8bae1dSRodney W. Grimes 	 * Return any desired output.
3303df8bae1dSRodney W. Grimes 	 */
3304df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
330540fa3e40SGleb Smirnoff 		(void) tcp_output(tp);
33067792ea27SJeffrey Hsu 
3307a14c749fSJonathan Lemon check_delack:
33089eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
3309252ca428SRobert Watson 
3310a14c749fSJonathan Lemon 	if (tp->t_flags & TF_DELACK) {
33113bfd6421SJonathan Lemon 		tp->t_flags &= ~TF_DELACK;
3312b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
33133bfd6421SJonathan Lemon 	}
33149eb0e832SGleb Smirnoff 	INP_WUNLOCK(inp);
3315679d9708SAndre Oppermann 	return;
3316df8bae1dSRodney W. Grimes 
3317df8bae1dSRodney W. Grimes dropafterack:
3318df8bae1dSRodney W. Grimes 	/*
3319df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
3320df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
332180ab7c0eSGarrett Wollman 	 *
332280ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
332380ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
332480ab7c0eSGarrett Wollman 	 * RST have been dropped.
332580ab7c0eSGarrett Wollman 	 *
332680ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
332780ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
332880ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
332980ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
333080ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
333180ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
3332df8bae1dSRodney W. Grimes 	 */
3333fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
3334fb59c426SYoshinobu Inoue 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
3335a57815efSBosko Milekic 	     SEQ_GT(th->th_ack, tp->snd_max)) ) {
3336a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
3337d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
3338a57815efSBosko Milekic 		goto dropwithreset;
3339a57815efSBosko Milekic 	}
33402b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
3341df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
334240fa3e40SGleb Smirnoff 	(void) tcp_output(tp);
33439eb0e832SGleb Smirnoff 	INP_WUNLOCK(inp);
3344d6915262SRobert Watson 	m_freem(m);
3345679d9708SAndre Oppermann 	return;
3346df8bae1dSRodney W. Grimes 
3347df8bae1dSRodney W. Grimes dropwithreset:
3348a0ca0871SRobert Watson 	if (tp != NULL) {
3349302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
33509eb0e832SGleb Smirnoff 		INP_WUNLOCK(inp);
3351dd8ac7f9SRobert Watson 	} else
3352a0ca0871SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
3353679d9708SAndre Oppermann 	return;
3354df8bae1dSRodney W. Grimes 
3355df8bae1dSRodney W. Grimes drop:
3356df8bae1dSRodney W. Grimes 	/*
3357df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
3358df8bae1dSRodney W. Grimes 	 */
33592b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
33604d0770f1SRichard Scheffenegger 	if (tp != NULL) {
33619eb0e832SGleb Smirnoff 		INP_WUNLOCK(inp);
33624d0770f1SRichard Scheffenegger 	}
3363d6915262SRobert Watson 	m_freem(m);
3364302ce8d6SAndre Oppermann }
3365302ce8d6SAndre Oppermann 
3366302ce8d6SAndre Oppermann /*
33670ca3f933SAndre Oppermann  * Issue RST and make ACK acceptable to originator of segment.
33680ca3f933SAndre Oppermann  * The mbuf must still include the original packet header.
33690ca3f933SAndre Oppermann  * tp may be NULL.
3370302ce8d6SAndre Oppermann  */
337155bceb1eSRandall Stewart void
3372302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp,
3373302ce8d6SAndre Oppermann     int tlen, int rstreason)
3374302ce8d6SAndre Oppermann {
3375b287c6c7SBjoern A. Zeeb #ifdef INET
3376302ce8d6SAndre Oppermann 	struct ip *ip;
3377b287c6c7SBjoern A. Zeeb #endif
3378302ce8d6SAndre Oppermann #ifdef INET6
3379302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6;
3380302ce8d6SAndre Oppermann #endif
33817a3244ccSRobert Watson 
33827a3244ccSRobert Watson 	if (tp != NULL) {
33839eb0e832SGleb Smirnoff 		INP_LOCK_ASSERT(tptoinpcb(tp));
33847a3244ccSRobert Watson 	}
33857a3244ccSRobert Watson 
33860ca3f933SAndre Oppermann 	/* Don't bother if destination was broadcast/multicast. */
33871ebf4607SRichard Scheffenegger 	if ((tcp_get_flags(th) & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
3388302ce8d6SAndre Oppermann 		goto drop;
3389302ce8d6SAndre Oppermann #ifdef INET6
3390302ce8d6SAndre Oppermann 	if (mtod(m, struct ip *)->ip_v == 6) {
3391302ce8d6SAndre Oppermann 		ip6 = mtod(m, struct ip6_hdr *);
3392302ce8d6SAndre Oppermann 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
3393302ce8d6SAndre Oppermann 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
3394302ce8d6SAndre Oppermann 			goto drop;
3395302ce8d6SAndre Oppermann 		/* IPv6 anycast check is done at tcp6_input() */
3396b287c6c7SBjoern A. Zeeb 	}
3397302ce8d6SAndre Oppermann #endif
3398b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3399b287c6c7SBjoern A. Zeeb 	else
3400b287c6c7SBjoern A. Zeeb #endif
3401b287c6c7SBjoern A. Zeeb #ifdef INET
3402302ce8d6SAndre Oppermann 	{
3403302ce8d6SAndre Oppermann 		ip = mtod(m, struct ip *);
3404302ce8d6SAndre Oppermann 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
3405302ce8d6SAndre Oppermann 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
3406302ce8d6SAndre Oppermann 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
3407302ce8d6SAndre Oppermann 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
3408302ce8d6SAndre Oppermann 			goto drop;
3409302ce8d6SAndre Oppermann 	}
3410b287c6c7SBjoern A. Zeeb #endif
3411302ce8d6SAndre Oppermann 
3412302ce8d6SAndre Oppermann 	/* Perform bandwidth limiting. */
3413302ce8d6SAndre Oppermann 	if (badport_bandlim(rstreason) < 0)
3414302ce8d6SAndre Oppermann 		goto drop;
3415302ce8d6SAndre Oppermann 
3416302ce8d6SAndre Oppermann 	/* tcp_respond consumes the mbuf chain. */
34171ebf4607SRichard Scheffenegger 	if (tcp_get_flags(th) & TH_ACK) {
3418302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0,
3419302ce8d6SAndre Oppermann 		    th->th_ack, TH_RST);
3420302ce8d6SAndre Oppermann 	} else {
34211ebf4607SRichard Scheffenegger 		if (tcp_get_flags(th) & TH_SYN)
3422302ce8d6SAndre Oppermann 			tlen++;
34231ebf4607SRichard Scheffenegger 		if (tcp_get_flags(th) & TH_FIN)
34244ddd5aadSMichael Tuexen 			tlen++;
3425302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
3426302ce8d6SAndre Oppermann 		    (tcp_seq)0, TH_RST|TH_ACK);
3427302ce8d6SAndre Oppermann 	}
3428302ce8d6SAndre Oppermann 	return;
3429302ce8d6SAndre Oppermann drop:
3430302ce8d6SAndre Oppermann 	m_freem(m);
3431df8bae1dSRodney W. Grimes }
3432df8bae1dSRodney W. Grimes 
3433be2ac88cSJonathan Lemon /*
3434be2ac88cSJonathan Lemon  * Parse TCP options and place in tcpopt.
3435be2ac88cSJonathan Lemon  */
343655bceb1eSRandall Stewart void
3437ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags)
3438df8bae1dSRodney W. Grimes {
3439df8bae1dSRodney W. Grimes 	int opt, optlen;
3440df8bae1dSRodney W. Grimes 
3441be2ac88cSJonathan Lemon 	to->to_flags = 0;
3442df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
3443df8bae1dSRodney W. Grimes 		opt = cp[0];
3444df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
3445df8bae1dSRodney W. Grimes 			break;
3446df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
3447df8bae1dSRodney W. Grimes 			optlen = 1;
3448df8bae1dSRodney W. Grimes 		else {
3449b474779fSJun-ichiro itojun Hagino 			if (cnt < 2)
3450b474779fSJun-ichiro itojun Hagino 				break;
3451df8bae1dSRodney W. Grimes 			optlen = cp[1];
3452b474779fSJun-ichiro itojun Hagino 			if (optlen < 2 || optlen > cnt)
3453df8bae1dSRodney W. Grimes 				break;
3454df8bae1dSRodney W. Grimes 		}
3455df8bae1dSRodney W. Grimes 		switch (opt) {
3456df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
3457df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
3458df8bae1dSRodney W. Grimes 				continue;
3459f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3460df8bae1dSRodney W. Grimes 				continue;
3461be2ac88cSJonathan Lemon 			to->to_flags |= TOF_MSS;
3462be2ac88cSJonathan Lemon 			bcopy((char *)cp + 2,
3463be2ac88cSJonathan Lemon 			    (char *)&to->to_mss, sizeof(to->to_mss));
3464fd8e4ebcSMike Barcroft 			to->to_mss = ntohs(to->to_mss);
3465df8bae1dSRodney W. Grimes 			break;
3466df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
3467df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
3468df8bae1dSRodney W. Grimes 				continue;
3469f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3470df8bae1dSRodney W. Grimes 				continue;
3471be2ac88cSJonathan Lemon 			to->to_flags |= TOF_SCALE;
347202a1a643SAndre Oppermann 			to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT);
3473df8bae1dSRodney W. Grimes 			break;
3474df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
3475df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
3476df8bae1dSRodney W. Grimes 				continue;
3477be2ac88cSJonathan Lemon 			to->to_flags |= TOF_TS;
3478a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
3479a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
3480fd8e4ebcSMike Barcroft 			to->to_tsval = ntohl(to->to_tsval);
3481a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
3482a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
3483fd8e4ebcSMike Barcroft 			to->to_tsecr = ntohl(to->to_tsecr);
3484df8bae1dSRodney W. Grimes 			break;
34851cfd4b53SBruce M Simpson 		case TCPOPT_SIGNATURE:
3486fcf59617SAndrey V. Elsukov 			/*
3487fcf59617SAndrey V. Elsukov 			 * In order to reply to a host which has set the
3488fcf59617SAndrey V. Elsukov 			 * TCP_SIGNATURE option in its initial SYN, we have
3489fcf59617SAndrey V. Elsukov 			 * to record the fact that the option was observed
3490fcf59617SAndrey V. Elsukov 			 * here for the syncache code to perform the correct
3491fcf59617SAndrey V. Elsukov 			 * response.
3492fcf59617SAndrey V. Elsukov 			 */
34931cfd4b53SBruce M Simpson 			if (optlen != TCPOLEN_SIGNATURE)
34941cfd4b53SBruce M Simpson 				continue;
3495df47e437SAndre Oppermann 			to->to_flags |= TOF_SIGNATURE;
3496df47e437SAndre Oppermann 			to->to_signature = cp + 2;
34971cfd4b53SBruce M Simpson 			break;
34986d90faf3SPaul Saab 		case TCPOPT_SACK_PERMITTED:
3499f72167f4SAndre Oppermann 			if (optlen != TCPOLEN_SACK_PERMITTED)
35006d90faf3SPaul Saab 				continue;
3501f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3502f72167f4SAndre Oppermann 				continue;
3503603724d3SBjoern A. Zeeb 			if (!V_tcp_do_sack)
3504f72167f4SAndre Oppermann 				continue;
3505fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACKPERM;
35066d90faf3SPaul Saab 			break;
35076d90faf3SPaul Saab 		case TCPOPT_SACK:
35085a53ca16SPaul Saab 			if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
35096d90faf3SPaul Saab 				continue;
3510b728e902SAndre Oppermann 			if (flags & TO_SYN)
3511b728e902SAndre Oppermann 				continue;
3512fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACK;
35135a53ca16SPaul Saab 			to->to_nsacks = (optlen - 2) / TCPOLEN_SACK;
35145a53ca16SPaul Saab 			to->to_sacks = cp + 2;
351578b50714SRobert Watson 			TCPSTAT_INC(tcps_sack_rcv_blocks);
35166d90faf3SPaul Saab 			break;
3517281a0fd4SPatrick Kelsey 		case TCPOPT_FAST_OPEN:
3518c560df6fSPatrick Kelsey 			/*
3519c560df6fSPatrick Kelsey 			 * Cookie length validation is performed by the
3520c560df6fSPatrick Kelsey 			 * server side cookie checking code or the client
3521c560df6fSPatrick Kelsey 			 * side cookie cache update code.
3522c560df6fSPatrick Kelsey 			 */
3523281a0fd4SPatrick Kelsey 			if (!(flags & TO_SYN))
3524281a0fd4SPatrick Kelsey 				continue;
3525c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
3526c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
3527281a0fd4SPatrick Kelsey 				continue;
3528281a0fd4SPatrick Kelsey 			to->to_flags |= TOF_FASTOPEN;
3529281a0fd4SPatrick Kelsey 			to->to_tfo_len = optlen - 2;
3530281a0fd4SPatrick Kelsey 			to->to_tfo_cookie = to->to_tfo_len ? cp + 2 : NULL;
3531281a0fd4SPatrick Kelsey 			break;
3532be2ac88cSJonathan Lemon 		default:
3533be2ac88cSJonathan Lemon 			continue;
3534df8bae1dSRodney W. Grimes 		}
3535df8bae1dSRodney W. Grimes 	}
3536df8bae1dSRodney W. Grimes }
3537df8bae1dSRodney W. Grimes 
3538df8bae1dSRodney W. Grimes /*
3539df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
3540df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
3541df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
3542df8bae1dSRodney W. Grimes  * sequencing purposes.
3543df8bae1dSRodney W. Grimes  */
354455bceb1eSRandall Stewart void
3545ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m,
3546ad3f9ab3SAndre Oppermann     int off)
3547df8bae1dSRodney W. Grimes {
3548fb59c426SYoshinobu Inoue 	int cnt = off + th->th_urp - 1;
3549df8bae1dSRodney W. Grimes 
3550df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
3551df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
3552df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
3553df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
3554df8bae1dSRodney W. Grimes 
35559eb0e832SGleb Smirnoff 			INP_WLOCK_ASSERT(tptoinpcb(tp));
35567a3244ccSRobert Watson 
3557df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
3558df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
3559df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
3560df8bae1dSRodney W. Grimes 			m->m_len--;
356169a34685SYoshinobu Inoue 			if (m->m_flags & M_PKTHDR)
356269a34685SYoshinobu Inoue 				m->m_pkthdr.len--;
3563df8bae1dSRodney W. Grimes 			return;
3564df8bae1dSRodney W. Grimes 		}
3565df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
3566df8bae1dSRodney W. Grimes 		m = m->m_next;
35674dfdffe9SAndre Oppermann 		if (m == NULL)
3568df8bae1dSRodney W. Grimes 			break;
3569df8bae1dSRodney W. Grimes 	}
3570df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
3571df8bae1dSRodney W. Grimes }
3572df8bae1dSRodney W. Grimes 
3573df8bae1dSRodney W. Grimes /*
3574df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
3575df8bae1dSRodney W. Grimes  * and update averages and current timeout.
3576df8bae1dSRodney W. Grimes  */
357755bceb1eSRandall Stewart void
3578ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt)
3579df8bae1dSRodney W. Grimes {
3580ad3f9ab3SAndre Oppermann 	int delta;
3581233e8c18SGarrett Wollman 
35829eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
35832be3bf22SRobert Watson 
358478b50714SRobert Watson 	TCPSTAT_INC(tcps_rttupdated);
358518b83b62SRichard Scheffenegger 	if (tp->t_rttupdated < UCHAR_MAX)
3586233e8c18SGarrett Wollman 		tp->t_rttupdated++;
3587adc56f5aSEdward Tomasz Napierala #ifdef STATS
3588adc56f5aSEdward Tomasz Napierala 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT,
3589adc56f5aSEdward Tomasz Napierala 	    imax(0, rtt * 1000 / hz));
3590adc56f5aSEdward Tomasz Napierala #endif
35915ede40dcSRyan Stone 	if ((tp->t_srtt != 0) && (tp->t_rxtshift <= TCP_RTT_INVALIDATE)) {
3592233e8c18SGarrett Wollman 		/*
3593233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
3594233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
3595233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
3596233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
3597233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
3598233e8c18SGarrett Wollman 		 */
3599233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
3600233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
3601233e8c18SGarrett Wollman 
3602233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
3603233e8c18SGarrett Wollman 			tp->t_srtt = 1;
3604233e8c18SGarrett Wollman 
3605233e8c18SGarrett Wollman 		/*
3606233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
3607233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
3608233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
3609233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
3610233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
3611233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
3612233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
3613233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
3614233e8c18SGarrett Wollman 		 */
3615233e8c18SGarrett Wollman 		if (delta < 0)
3616233e8c18SGarrett Wollman 			delta = -delta;
3617233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
3618233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
3619233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
3620233e8c18SGarrett Wollman 	} else {
3621233e8c18SGarrett Wollman 		/*
3622233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
3623233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
3624233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
3625233e8c18SGarrett Wollman 		 */
3626233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
3627233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
3628233e8c18SGarrett Wollman 	}
36299b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
3630df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
3631df8bae1dSRodney W. Grimes 
3632df8bae1dSRodney W. Grimes 	/*
3633df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
3634df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
3635df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
3636df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
3637df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
3638df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
3639df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
3640df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
3641df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
3642df8bae1dSRodney W. Grimes 	 */
3643233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
36449e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
3645df8bae1dSRodney W. Grimes 
3646df8bae1dSRodney W. Grimes 	/*
3647df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
3648df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
3649df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
3650df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
3651df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
3652df8bae1dSRodney W. Grimes 	 */
3653df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
3654df8bae1dSRodney W. Grimes }
3655df8bae1dSRodney W. Grimes 
3656df8bae1dSRodney W. Grimes /*
3657df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
3658df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
36594faaea55SAndre Oppermann  * If none, use an mss that can be handled on the outgoing interface
36604faaea55SAndre Oppermann  * without forcing IP to fragment.  If no route is found, route has no mtu,
3661df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
3662df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
3663df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
3664df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
3665df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
3666df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
3667df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
3668a0292f23SGarrett Wollman  *
36690c39d38dSGleb Smirnoff  * NOTE that resulting t_maxseg doesn't include space for TCP options or
36700c39d38dSGleb Smirnoff  * IP options, e.g. IPSEC data, since length of this data may vary, and
36710c39d38dSGleb Smirnoff  * thus it is calculated for every segment separately in tcp_output().
3672a0292f23SGarrett Wollman  *
367397d8d152SAndre Oppermann  * NOTE that this routine is only called when we process an incoming
3674ef341ee1SGleb Smirnoff  * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS
3675ef341ee1SGleb Smirnoff  * settings are handled in tcp_mssopt().
3676df8bae1dSRodney W. Grimes  */
3677a0292f23SGarrett Wollman void
3678ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer,
36793c914c54SAndre Oppermann     struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap)
3680df8bae1dSRodney W. Grimes {
3681b287c6c7SBjoern A. Zeeb 	int mss = 0;
36823ac12506SJonathan T. Looney 	uint32_t maxmtu = 0;
36839eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
368497d8d152SAndre Oppermann 	struct hc_metrics_lite metrics;
3685fb59c426SYoshinobu Inoue #ifdef INET6
3686c068736aSJeffrey Hsu 	int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
3687c068736aSJeffrey Hsu 	size_t min_protoh = isipv6 ?
3688c068736aSJeffrey Hsu 			    sizeof (struct ip6_hdr) + sizeof (struct tcphdr) :
3689c068736aSJeffrey Hsu 			    sizeof (struct tcpiphdr);
3690c068736aSJeffrey Hsu #else
36919e644c23SMichael Tuexen 	 size_t min_protoh = sizeof(struct tcpiphdr);
3692fb59c426SYoshinobu Inoue #endif
3693df8bae1dSRodney W. Grimes 
36949eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
36957a3244ccSRobert Watson 
36969e644c23SMichael Tuexen 	if (tp->t_port)
36979e644c23SMichael Tuexen 		min_protoh += V_tcp_udp_tunneling_overhead;
3698ef341ee1SGleb Smirnoff 	if (mtuoffer != -1) {
3699ef341ee1SGleb Smirnoff 		KASSERT(offer == -1, ("%s: conflict", __func__));
3700ef341ee1SGleb Smirnoff 		offer = mtuoffer - min_protoh;
3701ef341ee1SGleb Smirnoff 	}
3702ef341ee1SGleb Smirnoff 
3703e8949f74SAndre Oppermann 	/* Initialize. */
370497d8d152SAndre Oppermann #ifdef INET6
370597d8d152SAndre Oppermann 	if (isipv6) {
37063c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu6(&inp->inp_inc, cap);
37070c39d38dSGleb Smirnoff 		tp->t_maxseg = V_tcp_v6mssdflt;
3708b287c6c7SBjoern A. Zeeb 	}
370997d8d152SAndre Oppermann #endif
3710b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3711b287c6c7SBjoern A. Zeeb 	else
3712b287c6c7SBjoern A. Zeeb #endif
3713b287c6c7SBjoern A. Zeeb #ifdef INET
371497d8d152SAndre Oppermann 	{
37153c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu(&inp->inp_inc, cap);
37160c39d38dSGleb Smirnoff 		tp->t_maxseg = V_tcp_mssdflt;
3717df8bae1dSRodney W. Grimes 	}
3718b287c6c7SBjoern A. Zeeb #endif
3719df8bae1dSRodney W. Grimes 
372097d8d152SAndre Oppermann 	/*
3721e8949f74SAndre Oppermann 	 * No route to sender, stay with default mss and return.
372297d8d152SAndre Oppermann 	 */
37236f01cac6SBjoern A. Zeeb 	if (maxmtu == 0) {
37246f01cac6SBjoern A. Zeeb 		/*
37258e5c87f4SBjoern A. Zeeb 		 * In case we return early we need to initialize metrics
37266f01cac6SBjoern A. Zeeb 		 * to a defined state as tcp_hc_get() would do for us
37276f01cac6SBjoern A. Zeeb 		 * if there was no cache hit.
37286f01cac6SBjoern A. Zeeb 		 */
37296f01cac6SBjoern A. Zeeb 		if (metricptr != NULL)
37306f01cac6SBjoern A. Zeeb 			bzero(metricptr, sizeof(struct hc_metrics_lite));
373197d8d152SAndre Oppermann 		return;
37326f01cac6SBjoern A. Zeeb 	}
373397d8d152SAndre Oppermann 
3734e8949f74SAndre Oppermann 	/* What have we got? */
373597d8d152SAndre Oppermann 	switch (offer) {
373697d8d152SAndre Oppermann 		case 0:
373797d8d152SAndre Oppermann 			/*
373897d8d152SAndre Oppermann 			 * Offer == 0 means that there was no MSS on the SYN
3739c3b02504SBjoern A. Zeeb 			 * segment, in this case we use tcp_mssdflt as
37400c39d38dSGleb Smirnoff 			 * already assigned to t_maxseg above.
374197d8d152SAndre Oppermann 			 */
37420c39d38dSGleb Smirnoff 			offer = tp->t_maxseg;
374397d8d152SAndre Oppermann 			break;
374497d8d152SAndre Oppermann 
374597d8d152SAndre Oppermann 		case -1:
3746a0292f23SGarrett Wollman 			/*
3747c94c54e4SAndre Oppermann 			 * Offer == -1 means that we didn't receive SYN yet.
3748a0292f23SGarrett Wollman 			 */
374997d8d152SAndre Oppermann 			/* FALLTHROUGH */
375097d8d152SAndre Oppermann 
375197d8d152SAndre Oppermann 		default:
3752a0292f23SGarrett Wollman 			/*
375353369ac9SAndre Oppermann 			 * Prevent DoS attack with too small MSS. Round up
375453369ac9SAndre Oppermann 			 * to at least minmss.
375553369ac9SAndre Oppermann 			 */
3756603724d3SBjoern A. Zeeb 			offer = max(offer, V_tcp_minmss);
375797d8d152SAndre Oppermann 	}
3758a0292f23SGarrett Wollman 
3759df8bae1dSRodney W. Grimes 	/*
3760e8949f74SAndre Oppermann 	 * rmx information is now retrieved from tcp_hostcache.
3761df8bae1dSRodney W. Grimes 	 */
376297d8d152SAndre Oppermann 	tcp_hc_get(&inp->inp_inc, &metrics);
37633cee92e0SBjoern A. Zeeb 	if (metricptr != NULL)
37643cee92e0SBjoern A. Zeeb 		bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite));
376597d8d152SAndre Oppermann 
3766df8bae1dSRodney W. Grimes 	/*
3767cc412412SHiren Panchasara 	 * If there's a discovered mtu in tcp hostcache, use it.
3768cc412412SHiren Panchasara 	 * Else, use the link mtu.
3769df8bae1dSRodney W. Grimes 	 */
377097d8d152SAndre Oppermann 	if (metrics.rmx_mtu)
37712d166c02SAndre Oppermann 		mss = min(metrics.rmx_mtu, maxmtu) - min_protoh;
3772c068736aSJeffrey Hsu 	else {
377331b3783cSHajimu UMEMOTO #ifdef INET6
3774fb59c426SYoshinobu Inoue 		if (isipv6) {
377597d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3776603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
377797d8d152SAndre Oppermann 			    !in6_localaddr(&inp->in6p_faddr))
3778603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_v6mssdflt);
3779b287c6c7SBjoern A. Zeeb 		}
3780b3399803SHajimu UMEMOTO #endif
3781b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3782b287c6c7SBjoern A. Zeeb 		else
3783b287c6c7SBjoern A. Zeeb #endif
3784b287c6c7SBjoern A. Zeeb #ifdef INET
378597d8d152SAndre Oppermann 		{
378697d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3787603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
378897d8d152SAndre Oppermann 			    !in_localaddr(inp->inp_faddr))
3789603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_mssdflt);
3790a0292f23SGarrett Wollman 		}
3791b287c6c7SBjoern A. Zeeb #endif
37923cee92e0SBjoern A. Zeeb 		/*
37933cee92e0SBjoern A. Zeeb 		 * XXX - The above conditional (mss = maxmtu - min_protoh)
37943cee92e0SBjoern A. Zeeb 		 * probably violates the TCP spec.
37953cee92e0SBjoern A. Zeeb 		 * The problem is that, since we don't know the
37963cee92e0SBjoern A. Zeeb 		 * other end's MSS, we are supposed to use a conservative
37973cee92e0SBjoern A. Zeeb 		 * default.  But, if we do that, then MTU discovery will
37983cee92e0SBjoern A. Zeeb 		 * never actually take place, because the conservative
37993cee92e0SBjoern A. Zeeb 		 * default is much less than the MTUs typically seen
38003cee92e0SBjoern A. Zeeb 		 * on the Internet today.  For the moment, we'll sweep
38013cee92e0SBjoern A. Zeeb 		 * this under the carpet.
38023cee92e0SBjoern A. Zeeb 		 *
38033cee92e0SBjoern A. Zeeb 		 * The conservative default might not actually be a problem
38043cee92e0SBjoern A. Zeeb 		 * if the only case this occurs is when sending an initial
38053cee92e0SBjoern A. Zeeb 		 * SYN with options and data to a host we've never talked
38063cee92e0SBjoern A. Zeeb 		 * to before.  Then, they will reply with an MSS value which
38073cee92e0SBjoern A. Zeeb 		 * will get recorded and the new parameters should get
38083cee92e0SBjoern A. Zeeb 		 * recomputed.  For Further Study.
38093cee92e0SBjoern A. Zeeb 		 */
381097d8d152SAndre Oppermann 	}
3811a0292f23SGarrett Wollman 	mss = min(mss, offer);
381297d8d152SAndre Oppermann 
3813a0292f23SGarrett Wollman 	/*
38140c39d38dSGleb Smirnoff 	 * Sanity check: make sure that maxseg will be large
38153cee92e0SBjoern A. Zeeb 	 * enough to allow some data on segments even if the
38163cee92e0SBjoern A. Zeeb 	 * all the option space is used (40bytes).  Otherwise
38173cee92e0SBjoern A. Zeeb 	 * funny things may happen in tcp_output.
38180c39d38dSGleb Smirnoff 	 *
38190c39d38dSGleb Smirnoff 	 * XXXGL: shouldn't we reserve space for IP/IPv6 options?
38203cee92e0SBjoern A. Zeeb 	 */
38213cee92e0SBjoern A. Zeeb 	mss = max(mss, 64);
38223cee92e0SBjoern A. Zeeb 
382397d8d152SAndre Oppermann 	tp->t_maxseg = mss;
38243cee92e0SBjoern A. Zeeb }
38253cee92e0SBjoern A. Zeeb 
38263cee92e0SBjoern A. Zeeb void
38273cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer)
38283cee92e0SBjoern A. Zeeb {
3829dbc42409SLawrence Stewart 	int mss;
38303ac12506SJonathan T. Looney 	uint32_t bufsize;
38319eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
38323cee92e0SBjoern A. Zeeb 	struct socket *so;
38333cee92e0SBjoern A. Zeeb 	struct hc_metrics_lite metrics;
38343c914c54SAndre Oppermann 	struct tcp_ifcap cap;
3835dbc42409SLawrence Stewart 
38363cee92e0SBjoern A. Zeeb 	KASSERT(tp != NULL, ("%s: tp == NULL", __func__));
38373cee92e0SBjoern A. Zeeb 
38383c914c54SAndre Oppermann 	bzero(&cap, sizeof(cap));
38393c914c54SAndre Oppermann 	tcp_mss_update(tp, offer, -1, &metrics, &cap);
38403cee92e0SBjoern A. Zeeb 
38413cee92e0SBjoern A. Zeeb 	mss = tp->t_maxseg;
384297d8d152SAndre Oppermann 
3843df8bae1dSRodney W. Grimes 	/*
384497d8d152SAndre Oppermann 	 * If there's a pipesize, change the socket buffer to that size,
384597d8d152SAndre Oppermann 	 * don't change if sb_hiwat is different than default (then it
384697d8d152SAndre Oppermann 	 * has been changed on purpose with setsockopt).
384797d8d152SAndre Oppermann 	 * Make the socket buffers an integral number of mss units;
384897d8d152SAndre Oppermann 	 * if the mss is larger than the socket buffer, decrease the mss.
3849df8bae1dSRodney W. Grimes 	 */
3850c3b02504SBjoern A. Zeeb 	so = inp->inp_socket;
38513f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
3852e233e2acSAndre Oppermann 	if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe)
385397d8d152SAndre Oppermann 		bufsize = metrics.rmx_sendpipe;
385497d8d152SAndre Oppermann 	else
3855df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
3856df8bae1dSRodney W. Grimes 	if (bufsize < mss)
3857df8bae1dSRodney W. Grimes 		mss = bufsize;
3858df8bae1dSRodney W. Grimes 	else {
3859df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3860df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3861df8bae1dSRodney W. Grimes 			bufsize = sb_max;
386288c39af3SRuslan Ermilov 		if (bufsize > so->so_snd.sb_hiwat)
386343283184SGleb Smirnoff 			(void)sbreserve_locked(so, SO_SND, bufsize, NULL);
3864df8bae1dSRodney W. Grimes 	}
38653f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
3866784ce8faSHiren Panchasara 	/*
3867784ce8faSHiren Panchasara 	 * Sanity check: make sure that maxseg will be large
3868784ce8faSHiren Panchasara 	 * enough to allow some data on segments even if the
3869784ce8faSHiren Panchasara 	 * all the option space is used (40bytes).  Otherwise
3870784ce8faSHiren Panchasara 	 * funny things may happen in tcp_output.
3871784ce8faSHiren Panchasara 	 *
3872784ce8faSHiren Panchasara 	 * XXXGL: shouldn't we reserve space for IP/IPv6 options?
3873784ce8faSHiren Panchasara 	 */
3874784ce8faSHiren Panchasara 	tp->t_maxseg = max(mss, 64);
3875df8bae1dSRodney W. Grimes 
38763f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
3877e233e2acSAndre Oppermann 	if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe)
387897d8d152SAndre Oppermann 		bufsize = metrics.rmx_recvpipe;
387997d8d152SAndre Oppermann 	else
3880df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
3881df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
3882df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3883df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3884df8bae1dSRodney W. Grimes 			bufsize = sb_max;
388588c39af3SRuslan Ermilov 		if (bufsize > so->so_rcv.sb_hiwat)
388643283184SGleb Smirnoff 			(void)sbreserve_locked(so, SO_RCV, bufsize, NULL);
3887df8bae1dSRodney W. Grimes 	}
38883f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
388991d6cfa6SBjoern A. Zeeb 
389091d6cfa6SBjoern A. Zeeb 	/* Check the interface for TSO capabilities. */
38913c914c54SAndre Oppermann 	if (cap.ifcap & CSUM_TSO) {
389291d6cfa6SBjoern A. Zeeb 		tp->t_flags |= TF_TSO;
38933c914c54SAndre Oppermann 		tp->t_tsomax = cap.tsomax;
38949fd573c3SHans Petter Selasky 		tp->t_tsomaxsegcount = cap.tsomaxsegcount;
38959fd573c3SHans Petter Selasky 		tp->t_tsomaxsegsize = cap.tsomaxsegsize;
38963c914c54SAndre Oppermann 	}
3897a0292f23SGarrett Wollman }
3898a0292f23SGarrett Wollman 
3899a0292f23SGarrett Wollman /*
3900a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
3901a0292f23SGarrett Wollman  */
3902a0292f23SGarrett Wollman int
3903ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc)
3904a0292f23SGarrett Wollman {
390597d8d152SAndre Oppermann 	int mss = 0;
39063ac12506SJonathan T. Looney 	uint32_t thcmtu = 0;
39073ac12506SJonathan T. Looney 	uint32_t maxmtu = 0;
390897d8d152SAndre Oppermann 	size_t min_protoh;
3909a0292f23SGarrett Wollman 
391097d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer"));
3911a0292f23SGarrett Wollman 
391231b3783cSHajimu UMEMOTO #ifdef INET6
3913dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
3914603724d3SBjoern A. Zeeb 		mss = V_tcp_v6mssdflt;
3915233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu6(inc, NULL);
391697d8d152SAndre Oppermann 		min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
3917b287c6c7SBjoern A. Zeeb 	}
391831b3783cSHajimu UMEMOTO #endif
3919b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3920b287c6c7SBjoern A. Zeeb 	else
3921b287c6c7SBjoern A. Zeeb #endif
3922b287c6c7SBjoern A. Zeeb #ifdef INET
392397d8d152SAndre Oppermann 	{
3924603724d3SBjoern A. Zeeb 		mss = V_tcp_mssdflt;
3925233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu(inc, NULL);
392697d8d152SAndre Oppermann 		min_protoh = sizeof(struct tcpiphdr);
392797d8d152SAndre Oppermann 	}
3928b287c6c7SBjoern A. Zeeb #endif
39293a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET)
39303a9391deSBjoern A. Zeeb 	thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
39313a9391deSBjoern A. Zeeb #endif
39323a9391deSBjoern A. Zeeb 
393397d8d152SAndre Oppermann 	if (maxmtu && thcmtu)
393497d8d152SAndre Oppermann 		mss = min(maxmtu, thcmtu) - min_protoh;
393597d8d152SAndre Oppermann 	else if (maxmtu || thcmtu)
393697d8d152SAndre Oppermann 		mss = max(maxmtu, thcmtu) - min_protoh;
393797d8d152SAndre Oppermann 
393897d8d152SAndre Oppermann 	return (mss);
3939df8bae1dSRodney W. Grimes }
394046f58482SJonathan Lemon 
39410e1d7c25SRichard Scheffenegger void
3942*49a6fbe3SRichard Scheffenegger tcp_do_prr_ack(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to, sackstatus_t sack_changed)
39430e1d7c25SRichard Scheffenegger {
394448396dc7SRichard Scheffenegger 	int snd_cnt = 0, limit = 0, del_data = 0, pipe = 0;
39450e1d7c25SRichard Scheffenegger 	int maxseg = tcp_maxseg(tp);
39460e1d7c25SRichard Scheffenegger 
39479eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
39480e1d7c25SRichard Scheffenegger 
39490e1d7c25SRichard Scheffenegger 	/*
39500e1d7c25SRichard Scheffenegger 	 * Compute the amount of data that this ACK is indicating
39510e1d7c25SRichard Scheffenegger 	 * (del_data) and an estimate of how many bytes are in the
39520e1d7c25SRichard Scheffenegger 	 * network.
39530e1d7c25SRichard Scheffenegger 	 */
3954c21b7b55SRichard Scheffenegger 	if (tcp_is_sack_recovery(tp, to) ||
395574d7fc87SRichard Scheffenegger 	    (IN_CONGRECOVERY(tp->t_flags) &&
395674d7fc87SRichard Scheffenegger 	     !IN_FASTRECOVERY(tp->t_flags))) {
395731d7a27cSRichard Scheffenegger 		del_data = tp->sackhint.delivered_data;
3958d1de2b05SRichard Scheffenegger 		if (V_tcp_do_newsack)
3959a8e431e1SRichard Scheffenegger 			pipe = tcp_compute_pipe(tp);
3960a8e431e1SRichard Scheffenegger 		else
396174d7fc87SRichard Scheffenegger 			pipe = (tp->snd_nxt - tp->snd_fack) +
396274d7fc87SRichard Scheffenegger 				tp->sackhint.sack_bytes_rexmit;
396374d7fc87SRichard Scheffenegger 	} else {
396474d7fc87SRichard Scheffenegger 		if (tp->sackhint.prr_delivered < (tcprexmtthresh * maxseg +
396574d7fc87SRichard Scheffenegger 					     tp->snd_recover - tp->snd_una))
396674d7fc87SRichard Scheffenegger 			del_data = maxseg;
396774d7fc87SRichard Scheffenegger 		pipe = imax(0, tp->snd_max - tp->snd_una -
396874d7fc87SRichard Scheffenegger 			    imin(INT_MAX / 65536, tp->t_dupacks) * maxseg);
396974d7fc87SRichard Scheffenegger 	}
39700e1d7c25SRichard Scheffenegger 	tp->sackhint.prr_delivered += del_data;
39710e1d7c25SRichard Scheffenegger 	/*
39720e1d7c25SRichard Scheffenegger 	 * Proportional Rate Reduction
39730e1d7c25SRichard Scheffenegger 	 */
3974e9071000SRichard Scheffenegger 	if (pipe >= tp->snd_ssthresh) {
39756a376af0SRichard Scheffenegger 		if (tp->sackhint.recover_fs == 0)
39766a376af0SRichard Scheffenegger 			tp->sackhint.recover_fs =
397748396dc7SRichard Scheffenegger 			    imax(1, tp->snd_nxt - tp->snd_una);
397848396dc7SRichard Scheffenegger 		snd_cnt = howmany((long)tp->sackhint.prr_delivered *
397948396dc7SRichard Scheffenegger 			    tp->snd_ssthresh, tp->sackhint.recover_fs) -
3980e5313869SRichard Scheffenegger 			    tp->sackhint.prr_out;
39816a376af0SRichard Scheffenegger 	} else {
3982*49a6fbe3SRichard Scheffenegger 		/*
3983*49a6fbe3SRichard Scheffenegger 		 * PRR 6937bis heuristic:
3984*49a6fbe3SRichard Scheffenegger 		 * - A partial ack without SACK block beneath snd_recover
3985*49a6fbe3SRichard Scheffenegger 		 * indicates further loss.
3986*49a6fbe3SRichard Scheffenegger 		 * - An SACK scoreboard update adding a new hole indicates
3987*49a6fbe3SRichard Scheffenegger 		 * further loss, so be conservative and send at most one
3988*49a6fbe3SRichard Scheffenegger 		 * segment.
3989*49a6fbe3SRichard Scheffenegger 		 * - Prevent ACK splitting attacks, by being conservative
3990*49a6fbe3SRichard Scheffenegger 		 * when no new data is acked.
3991*49a6fbe3SRichard Scheffenegger 		 */
3992*49a6fbe3SRichard Scheffenegger 		if ((sack_changed == SACK_NEWLOSS) || (del_data == 0))
399384761f3dSRichard Scheffenegger 			limit = tp->sackhint.prr_delivered -
3994e5313869SRichard Scheffenegger 				tp->sackhint.prr_out;
39950e1d7c25SRichard Scheffenegger 		else
399648396dc7SRichard Scheffenegger 			limit = imax(tp->sackhint.prr_delivered -
3997e5313869SRichard Scheffenegger 				    tp->sackhint.prr_out, del_data) +
3998e5313869SRichard Scheffenegger 				    maxseg;
399948396dc7SRichard Scheffenegger 		snd_cnt = imin((tp->snd_ssthresh - pipe), limit);
40000e1d7c25SRichard Scheffenegger 	}
400148396dc7SRichard Scheffenegger 	snd_cnt = imax(snd_cnt, 0) / maxseg;
40020e1d7c25SRichard Scheffenegger 	/*
40030e1d7c25SRichard Scheffenegger 	 * Send snd_cnt new data into the network in response to this ack.
40040e1d7c25SRichard Scheffenegger 	 * If there is going to be a SACK retransmission, adjust snd_cwnd
40050e1d7c25SRichard Scheffenegger 	 * accordingly.
40060e1d7c25SRichard Scheffenegger 	 */
4007b9f803b7SRichard Scheffenegger 	if (IN_FASTRECOVERY(tp->t_flags)) {
4008c21b7b55SRichard Scheffenegger 		if (tcp_is_sack_recovery(tp, to)) {
400974d7fc87SRichard Scheffenegger 			tp->snd_cwnd = tp->snd_nxt - tp->snd_recover +
401074d7fc87SRichard Scheffenegger 					    tp->sackhint.sack_bytes_rexmit +
401174d7fc87SRichard Scheffenegger 					    (snd_cnt * maxseg);
401274d7fc87SRichard Scheffenegger 		} else {
401374d7fc87SRichard Scheffenegger 			tp->snd_cwnd = (tp->snd_max - tp->snd_una) +
401474d7fc87SRichard Scheffenegger 					    (snd_cnt * maxseg);
401574d7fc87SRichard Scheffenegger 		}
4016b9f803b7SRichard Scheffenegger 	} else if (IN_CONGRECOVERY(tp->t_flags))
401774d7fc87SRichard Scheffenegger 		tp->snd_cwnd = pipe - del_data + (snd_cnt * maxseg);
401874d7fc87SRichard Scheffenegger 	tp->snd_cwnd = imax(maxseg, tp->snd_cwnd);
40190e1d7c25SRichard Scheffenegger }
40200e1d7c25SRichard Scheffenegger 
402146f58482SJonathan Lemon /*
4022c068736aSJeffrey Hsu  * On a partial ack arrives, force the retransmission of the
4023c068736aSJeffrey Hsu  * next unacknowledged segment.  Do not clear tp->t_dupacks.
4024c068736aSJeffrey Hsu  * By setting snd_nxt to ti_ack, this forces retransmission timer to
4025c068736aSJeffrey Hsu  * be started again.
402646f58482SJonathan Lemon  */
402755bceb1eSRandall Stewart void
4028ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th)
402946f58482SJonathan Lemon {
403046f58482SJonathan Lemon 	tcp_seq onxt = tp->snd_nxt;
40313ac12506SJonathan T. Looney 	uint32_t ocwnd = tp->snd_cwnd;
40320c39d38dSGleb Smirnoff 	u_int maxseg = tcp_maxseg(tp);
403346f58482SJonathan Lemon 
40349eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
40357a3244ccSRobert Watson 
4036b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_REXMT, 0);
403746f58482SJonathan Lemon 	tp->t_rtttime = 0;
403846f58482SJonathan Lemon 	tp->snd_nxt = th->th_ack;
40396b2a5f92SJayanth Vijayaraghavan 	/*
4040c068736aSJeffrey Hsu 	 * Set snd_cwnd to one segment beyond acknowledged offset.
4041c068736aSJeffrey Hsu 	 * (tp->snd_una has not yet been updated when this function is called.)
40426b2a5f92SJayanth Vijayaraghavan 	 */
40430c39d38dSGleb Smirnoff 	tp->snd_cwnd = maxseg + BYTES_THIS_ACK(tp, th);
4044a84db8f4SMatthew Dillon 	tp->t_flags |= TF_ACKNOW;
404540fa3e40SGleb Smirnoff 	(void) tcp_output(tp);
404646f58482SJonathan Lemon 	tp->snd_cwnd = ocwnd;
404746f58482SJonathan Lemon 	if (SEQ_GT(onxt, tp->snd_nxt))
404846f58482SJonathan Lemon 		tp->snd_nxt = onxt;
404946f58482SJonathan Lemon 	/*
405046f58482SJonathan Lemon 	 * Partial window deflation.  Relies on fact that tp->snd_una
405146f58482SJonathan Lemon 	 * not updated yet.
405246f58482SJonathan Lemon 	 */
4053dbc42409SLawrence Stewart 	if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th))
4054dbc42409SLawrence Stewart 		tp->snd_cwnd -= BYTES_THIS_ACK(tp, th);
4055d7587117SPaul Saab 	else
4056d7587117SPaul Saab 		tp->snd_cwnd = 0;
40570c39d38dSGleb Smirnoff 	tp->snd_cwnd += maxseg;
405846f58482SJonathan Lemon }
405912eeb81fSHiren Panchasara 
406012eeb81fSHiren Panchasara int
406112eeb81fSHiren Panchasara tcp_compute_pipe(struct tcpcb *tp)
406212eeb81fSHiren Panchasara {
4063e5049a17SRandall Stewart 	if (tp->t_fb->tfb_compute_pipe == NULL) {
406412eeb81fSHiren Panchasara 		return (tp->snd_max - tp->snd_una +
406512eeb81fSHiren Panchasara 			tp->sackhint.sack_bytes_rexmit -
4066e2c6a6d2SRichard Scheffenegger 			tp->sackhint.sacked_bytes -
4067e2c6a6d2SRichard Scheffenegger 			tp->sackhint.lost_bytes);
4068e5049a17SRandall Stewart 	} else {
4069e5049a17SRandall Stewart 		return((*tp->t_fb->tfb_compute_pipe)(tp));
4070e5049a17SRandall Stewart 	}
407112eeb81fSHiren Panchasara }
40727dc90a1dSMichael Tuexen 
40737dc90a1dSMichael Tuexen uint32_t
40747dc90a1dSMichael Tuexen tcp_compute_initwnd(uint32_t maxseg)
40757dc90a1dSMichael Tuexen {
40767dc90a1dSMichael Tuexen 	/*
40777dc90a1dSMichael Tuexen 	 * Calculate the Initial Window, also used as Restart Window
40787dc90a1dSMichael Tuexen 	 *
40797dc90a1dSMichael Tuexen 	 * RFC5681 Section 3.1 specifies the default conservative values.
40807dc90a1dSMichael Tuexen 	 * RFC3390 specifies slightly more aggressive values.
40817dc90a1dSMichael Tuexen 	 * RFC6928 increases it to ten segments.
40827dc90a1dSMichael Tuexen 	 * Support for user specified value for initial flight size.
40837dc90a1dSMichael Tuexen 	 */
40847dc90a1dSMichael Tuexen 	if (V_tcp_initcwnd_segments)
40857dc90a1dSMichael Tuexen 		return min(V_tcp_initcwnd_segments * maxseg,
40867dc90a1dSMichael Tuexen 		    max(2 * maxseg, V_tcp_initcwnd_segments * 1460));
40877dc90a1dSMichael Tuexen 	else if (V_tcp_do_rfc3390)
40887dc90a1dSMichael Tuexen 		return min(4 * maxseg, max(2 * maxseg, 4380));
40897dc90a1dSMichael Tuexen 	else {
40907dc90a1dSMichael Tuexen 		/* Per RFC5681 Section 3.1 */
40917dc90a1dSMichael Tuexen 		if (maxseg > 2190)
40927dc90a1dSMichael Tuexen 			return (2 * maxseg);
40937dc90a1dSMichael Tuexen 		else if (maxseg > 1095)
40947dc90a1dSMichael Tuexen 			return (3 * maxseg);
40957dc90a1dSMichael Tuexen 		else
40967dc90a1dSMichael Tuexen 			return (4 * maxseg);
40977dc90a1dSMichael Tuexen 	}
40987dc90a1dSMichael Tuexen }
4099