xref: /freebsd/sys/netinet/tcp_input.c (revision f71cb9f74808facc9d3e6854f96da2afbed8a1e3)
1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4e79adb8eSGarrett Wollman  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6dbc42409SLawrence Stewart  * Copyright (c) 2007-2008,2010
7dbc42409SLawrence Stewart  *	Swinburne University of Technology, Melbourne, Australia.
8dbc42409SLawrence Stewart  * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org>
9dbc42409SLawrence Stewart  * Copyright (c) 2010 The FreeBSD Foundation
10fa046d87SRobert Watson  * Copyright (c) 2010-2011 Juniper Networks, Inc.
11dbc42409SLawrence Stewart  * All rights reserved.
12dbc42409SLawrence Stewart  *
13dbc42409SLawrence Stewart  * Portions of this software were developed at the Centre for Advanced Internet
14891b8ed4SLawrence Stewart  * Architectures, Swinburne University of Technology, by Lawrence Stewart,
15891b8ed4SLawrence Stewart  * James Healy and David Hayes, made possible in part by a grant from the Cisco
16891b8ed4SLawrence Stewart  * University Research Program Fund at Community Foundation Silicon Valley.
17dbc42409SLawrence Stewart  *
18dbc42409SLawrence Stewart  * Portions of this software were developed at the Centre for Advanced
19dbc42409SLawrence Stewart  * Internet Architectures, Swinburne University of Technology, Melbourne,
20dbc42409SLawrence Stewart  * Australia by David Hayes under sponsorship from the FreeBSD Foundation.
21df8bae1dSRodney W. Grimes  *
22fa046d87SRobert Watson  * Portions of this software were developed by Robert N. M. Watson under
23fa046d87SRobert Watson  * contract to Juniper Networks, Inc.
24fa046d87SRobert Watson  *
25df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
26df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
27df8bae1dSRodney W. Grimes  * are met:
28df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
29df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
30df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
31df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
32df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
33fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
34df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
35df8bae1dSRodney W. Grimes  *    without specific prior written permission.
36df8bae1dSRodney W. Grimes  *
37df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
38df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
41df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
48df8bae1dSRodney W. Grimes  *
49e79adb8eSGarrett Wollman  *	@(#)tcp_input.c	8.12 (Berkeley) 5/24/95
50df8bae1dSRodney W. Grimes  */
51df8bae1dSRodney W. Grimes 
524b421e2dSMike Silbersack #include <sys/cdefs.h>
534b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
544b421e2dSMike Silbersack 
551cfd4b53SBruce M Simpson #include "opt_inet.h"
56fb59c426SYoshinobu Inoue #include "opt_inet6.h"
573a2a9f79SYoshinobu Inoue #include "opt_ipsec.h"
580cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
590cc12cc5SJoerg Wunsch 
60df8bae1dSRodney W. Grimes #include <sys/param.h>
61adc56f5aSEdward Tomasz Napierala #include <sys/arb.h>
6298163b98SPoul-Henning Kamp #include <sys/kernel.h>
63bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
6439bc9de5SLawrence Stewart #include <sys/hhook.h>
65bd79708dSJonathan T. Looney #endif
66df8bae1dSRodney W. Grimes #include <sys/malloc.h>
67df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
68a29f300eSGarrett Wollman #include <sys/proc.h>		/* for proc0 declaration */
69df8bae1dSRodney W. Grimes #include <sys/protosw.h>
70adc56f5aSEdward Tomasz Napierala #include <sys/qmath.h>
7157f60867SMark Johnston #include <sys/sdt.h>
72960ed29cSSeigo Tanimura #include <sys/signalvar.h>
73df8bae1dSRodney W. Grimes #include <sys/socket.h>
74df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
75960ed29cSSeigo Tanimura #include <sys/sysctl.h>
76816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
77960ed29cSSeigo Tanimura #include <sys/systm.h>
78adc56f5aSEdward Tomasz Napierala #include <sys/stats.h>
79df8bae1dSRodney W. Grimes 
80e79adb8eSGarrett Wollman #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
81e79adb8eSGarrett Wollman 
8212e2e970SAndre Oppermann #include <vm/uma.h>
8312e2e970SAndre Oppermann 
84df8bae1dSRodney W. Grimes #include <net/if.h>
8576039bc8SGleb Smirnoff #include <net/if_var.h>
86df8bae1dSRodney W. Grimes #include <net/route.h>
87530c0060SRobert Watson #include <net/vnet.h>
88df8bae1dSRodney W. Grimes 
89773673c1SAndre Oppermann #define TCPSTATES		/* for logging */
90773673c1SAndre Oppermann 
91df8bae1dSRodney W. Grimes #include <netinet/in.h>
9257f60867SMark Johnston #include <netinet/in_kdtrace.h>
93960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
94df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
95df8bae1dSRodney W. Grimes #include <netinet/ip.h>
968e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h>	/* required for icmp_var.h */
97686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM */
98df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
99ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
100686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
101686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
102686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
103b66d74c1SGleb Smirnoff #include <netinet6/in6_var.h>
104960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h>
105960ed29cSSeigo Tanimura #include <netinet6/nd6.h>
1062de3e790SGleb Smirnoff #include <netinet/tcp.h>
107df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
1082529f56eSJonathan T. Looney #include <netinet/tcp_log_buf.h>
109df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
110df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
111df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
112fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h>
113df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
1144644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
115c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h>
11686a996e6SHiren Panchasara #ifdef TCPPCAP
11786a996e6SHiren Panchasara #include <netinet/tcp_pcap.h>
11886a996e6SHiren Panchasara #endif
119c325962bSMike Silbersack #include <netinet/tcp_syncache.h>
120df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.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_conservative) = 0;
1630e1d7c25SRichard Scheffenegger SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_prr_conservative, CTLFLAG_VNET | CTLFLAG_RW,
1640e1d7c25SRichard Scheffenegger     &VNET_NAME(tcp_do_prr_conservative), 0,
1650e1d7c25SRichard Scheffenegger     "Do conservative Proportional Rate Reduction");
1660e1d7c25SRichard Scheffenegger 
1670e1d7c25SRichard Scheffenegger VNET_DEFINE(int, tcp_do_prr) = 1;
1680e1d7c25SRichard Scheffenegger SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_prr, CTLFLAG_VNET | CTLFLAG_RW,
1690e1d7c25SRichard Scheffenegger     &VNET_NAME(tcp_do_prr), 1,
1700e1d7c25SRichard Scheffenegger     "Enable Proportional Rate Reduction per RFC 6937");
1710e1d7c25SRichard Scheffenegger 
1720471a8c7SRichard Scheffenegger VNET_DEFINE(int, tcp_do_lrd) = 0;
1730471a8c7SRichard Scheffenegger SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_lrd, CTLFLAG_VNET | CTLFLAG_RW,
1740471a8c7SRichard Scheffenegger     &VNET_NAME(tcp_do_lrd), 1,
1750471a8c7SRichard Scheffenegger     "Perform Lost Retransmission Detection");
1760471a8c7SRichard Scheffenegger 
177b72e56e7SMichael Tuexen VNET_DEFINE(int, tcp_do_newcwv) = 0;
178b72e56e7SMichael Tuexen SYSCTL_INT(_net_inet_tcp, OID_AUTO, newcwv, CTLFLAG_VNET | CTLFLAG_RW,
179b72e56e7SMichael Tuexen     &VNET_NAME(tcp_do_newcwv), 0,
180b72e56e7SMichael Tuexen     "Enable New Congestion Window Validation per RFC7661");
181b72e56e7SMichael Tuexen 
18282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3042) = 1;
1836df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_VNET | CTLFLAG_RW,
184eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3042), 0,
185eddfbb76SRobert Watson     "Enable RFC 3042 (Limited Transmit)");
186582a954bSJeffrey Hsu 
18782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3390) = 1;
1886df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_VNET | CTLFLAG_RW,
189eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3390), 0,
190da3a8a1aSJeffrey Hsu     "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)");
191da3a8a1aSJeffrey Hsu 
192356c7958SHiren Panchasara VNET_DEFINE(int, tcp_initcwnd_segments) = 10;
193356c7958SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, initcwnd_segments,
194356c7958SHiren Panchasara     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_initcwnd_segments), 0,
195356c7958SHiren Panchasara     "Slow-start flight size (initial congestion window) in number of segments");
19609440655SAndre Oppermann 
19782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3465) = 1;
1986df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_VNET | CTLFLAG_RW,
199eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3465), 0,
20024cb0f22SLawrence Stewart     "Enable RFC 3465 (Appropriate Byte Counting)");
201eddfbb76SRobert Watson 
20282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_abc_l_var) = 2;
2036df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_VNET | CTLFLAG_RW,
204eddfbb76SRobert Watson     &VNET_NAME(tcp_abc_l_var), 2,
20524cb0f22SLawrence Stewart     "Cap the max cwnd increment during slow-start to this number of segments");
20624cb0f22SLawrence Stewart 
2073220a212SGleb Smirnoff VNET_DEFINE(int, tcp_insecure_syn) = 0;
2086df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_syn, CTLFLAG_VNET | CTLFLAG_RW,
2093220a212SGleb Smirnoff     &VNET_NAME(tcp_insecure_syn), 0,
2103220a212SGleb Smirnoff     "Follow RFC793 instead of RFC5961 criteria for accepting SYN packets");
2113220a212SGleb Smirnoff 
21282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_insecure_rst) = 0;
2136df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_VNET | CTLFLAG_RW,
214eddfbb76SRobert Watson     &VNET_NAME(tcp_insecure_rst), 0,
2153220a212SGleb Smirnoff     "Follow RFC793 instead of RFC5961 criteria for accepting RST packets");
216a69968eeSMike Silbersack 
217fba0cea1SBjoern A. Zeeb VNET_DEFINE(int, tcp_recvspace) = 1024*64;
218873789cbSAndre Oppermann #define	V_tcp_recvspace	VNET(tcp_recvspace)
2196df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_VNET | CTLFLAG_RW,
220873789cbSAndre Oppermann     &VNET_NAME(tcp_recvspace), 0, "Initial receive socket buffer size");
221873789cbSAndre Oppermann 
22282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autorcvbuf) = 1;
2236df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_VNET | CTLFLAG_RW,
224eddfbb76SRobert Watson     &VNET_NAME(tcp_do_autorcvbuf), 0,
2258b615593SMarko Zec     "Enable automatic receive buffer sizing");
2266741ecf5SAndre Oppermann 
227b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024;
2286df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_VNET | CTLFLAG_RW,
229eddfbb76SRobert Watson     &VNET_NAME(tcp_autorcvbuf_max), 0,
2308b615593SMarko Zec     "Max size of automatic receive buffer");
2316741ecf5SAndre Oppermann 
23282cea7e6SBjoern A. Zeeb VNET_DEFINE(struct inpcbinfo, tcbinfo);
233df8bae1dSRodney W. Grimes 
234315e3e38SRobert Watson /*
235bf840a17SGleb Smirnoff  * TCP statistics are stored in an array of counter(9)s, which size matches
236bf840a17SGleb Smirnoff  * size of struct tcpstat.  TCP running connection count is a regular array.
2375923c293SGleb Smirnoff  */
2385da0521fSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct tcpstat, tcpstat);
2395da0521fSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_tcp, TCPCTL_STATS, stats, struct tcpstat,
2405da0521fSAndrey V. Elsukov     tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
241bf840a17SGleb Smirnoff VNET_DEFINE(counter_u64_t, tcps_states[TCP_NSTATES]);
242bf840a17SGleb Smirnoff SYSCTL_COUNTER_U64_ARRAY(_net_inet_tcp, TCPCTL_STATES, states, CTLFLAG_RD |
243d4d32b9fSHans Petter Selasky     CTLFLAG_VNET, &VNET_NAME(tcps_states)[0], TCP_NSTATES,
244bf840a17SGleb Smirnoff     "TCP connection counts by TCP state");
245bf840a17SGleb Smirnoff 
2465923c293SGleb Smirnoff /*
2477ca6e296SMichael Tuexen  * Kernel module interface for updating tcpstat.  The first argument is an index
2485923c293SGleb Smirnoff  * into tcpstat treated as an array.
249315e3e38SRobert Watson  */
250315e3e38SRobert Watson void
2517ca6e296SMichael Tuexen kmod_tcpstat_add(int statnum, int val)
252315e3e38SRobert Watson {
253315e3e38SRobert Watson 
2547ca6e296SMichael Tuexen 	counter_u64_add(VNET(tcpstat)[statnum], val);
255315e3e38SRobert Watson }
256315e3e38SRobert Watson 
257c21b7b55SRichard Scheffenegger /*
258c21b7b55SRichard Scheffenegger  * Make sure that we only start a SACK loss recovery when
259c21b7b55SRichard Scheffenegger  * receiving a duplicate ACK with a SACK block, and also
260c21b7b55SRichard Scheffenegger  * complete SACK loss recovery in case the other end
261c21b7b55SRichard Scheffenegger  * reneges.
262c21b7b55SRichard Scheffenegger  */
263c21b7b55SRichard Scheffenegger static bool inline
264c21b7b55SRichard Scheffenegger tcp_is_sack_recovery(struct tcpcb *tp, struct tcpopt *to)
265c21b7b55SRichard Scheffenegger {
266c21b7b55SRichard Scheffenegger 	return ((tp->t_flags & TF_SACK_PERMIT) &&
267c21b7b55SRichard Scheffenegger 		((to->to_flags & TOF_SACK) ||
268c21b7b55SRichard Scheffenegger 		(!TAILQ_EMPTY(&tp->snd_holes))));
269c21b7b55SRichard Scheffenegger }
270c21b7b55SRichard Scheffenegger 
271bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
272dbc42409SLawrence Stewart /*
27339bc9de5SLawrence Stewart  * Wrapper for the TCP established input helper hook.
27439bc9de5SLawrence Stewart  */
27555bceb1eSRandall Stewart void
27639bc9de5SLawrence Stewart hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to)
27739bc9de5SLawrence Stewart {
27839bc9de5SLawrence Stewart 	struct tcp_hhook_data hhook_data;
27939bc9de5SLawrence Stewart 
28039bc9de5SLawrence Stewart 	if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) {
28139bc9de5SLawrence Stewart 		hhook_data.tp = tp;
28239bc9de5SLawrence Stewart 		hhook_data.th = th;
28339bc9de5SLawrence Stewart 		hhook_data.to = to;
28439bc9de5SLawrence Stewart 
28539bc9de5SLawrence Stewart 		hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data,
28639bc9de5SLawrence Stewart 		    tp->osd);
28739bc9de5SLawrence Stewart 	}
28839bc9de5SLawrence Stewart }
289bd79708dSJonathan T. Looney #endif
29039bc9de5SLawrence Stewart 
29139bc9de5SLawrence Stewart /*
292dbc42409SLawrence Stewart  * CC wrapper hook functions
293dbc42409SLawrence Stewart  */
29455bceb1eSRandall Stewart void
2954b7b743cSLawrence Stewart cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t nsegs,
2964b7b743cSLawrence Stewart     uint16_t type)
297f2512ba1SRui Paulo {
298adc56f5aSEdward Tomasz Napierala #ifdef STATS
299adc56f5aSEdward Tomasz Napierala 	int32_t gput;
300adc56f5aSEdward Tomasz Napierala #endif
301adc56f5aSEdward Tomasz Napierala 
302dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
303f2512ba1SRui Paulo 
3044b7b743cSLawrence Stewart 	tp->ccv->nsegs = nsegs;
305dbc42409SLawrence Stewart 	tp->ccv->bytes_this_ack = BYTES_THIS_ACK(tp, th);
306b72e56e7SMichael Tuexen 	if ((!V_tcp_do_newcwv && (tp->snd_cwnd <= tp->snd_wnd)) ||
307b72e56e7SMichael Tuexen 	    (V_tcp_do_newcwv && (tp->snd_cwnd <= tp->snd_wnd) &&
308b72e56e7SMichael Tuexen 	     (tp->snd_cwnd < (tcp_compute_pipe(tp) * 2))))
309dbc42409SLawrence Stewart 		tp->ccv->flags |= CCF_CWND_LIMITED;
310dbc42409SLawrence Stewart 	else
311dbc42409SLawrence Stewart 		tp->ccv->flags &= ~CCF_CWND_LIMITED;
312dbc42409SLawrence Stewart 
313dbc42409SLawrence Stewart 	if (type == CC_ACK) {
314adc56f5aSEdward Tomasz Napierala #ifdef STATS
315adc56f5aSEdward Tomasz Napierala 		stats_voi_update_abs_s32(tp->t_stats, VOI_TCP_CALCFRWINDIFF,
316adc56f5aSEdward Tomasz Napierala 		    ((int32_t)tp->snd_cwnd) - tp->snd_wnd);
317adc56f5aSEdward Tomasz Napierala 		if (!IN_RECOVERY(tp->t_flags))
318adc56f5aSEdward Tomasz Napierala 			stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_ACKLEN,
319adc56f5aSEdward Tomasz Napierala 			   tp->ccv->bytes_this_ack / (tcp_maxseg(tp) * nsegs));
320adc56f5aSEdward Tomasz Napierala 		if ((tp->t_flags & TF_GPUTINPROG) &&
321adc56f5aSEdward Tomasz Napierala 		    SEQ_GEQ(th->th_ack, tp->gput_ack)) {
322adc56f5aSEdward Tomasz Napierala 			/*
323adc56f5aSEdward Tomasz Napierala 			 * Compute goodput in bits per millisecond.
324adc56f5aSEdward Tomasz Napierala 			 */
325f5766992SHans Petter Selasky 			gput = (((int64_t)SEQ_SUB(th->th_ack, tp->gput_seq)) << 3) /
326adc56f5aSEdward Tomasz Napierala 			    max(1, tcp_ts_getticks() - tp->gput_ts);
327adc56f5aSEdward Tomasz Napierala 			stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT,
328adc56f5aSEdward Tomasz Napierala 			    gput);
329adc56f5aSEdward Tomasz Napierala 			/*
330adc56f5aSEdward Tomasz Napierala 			 * XXXLAS: This is a temporary hack, and should be
331adc56f5aSEdward Tomasz Napierala 			 * chained off VOI_TCP_GPUT when stats(9) grows an API
332adc56f5aSEdward Tomasz Napierala 			 * to deal with chained VOIs.
333adc56f5aSEdward Tomasz Napierala 			 */
334adc56f5aSEdward Tomasz Napierala 			if (tp->t_stats_gput_prev > 0)
335adc56f5aSEdward Tomasz Napierala 				stats_voi_update_abs_s32(tp->t_stats,
336adc56f5aSEdward Tomasz Napierala 				    VOI_TCP_GPUT_ND,
337adc56f5aSEdward Tomasz Napierala 				    ((gput - tp->t_stats_gput_prev) * 100) /
338adc56f5aSEdward Tomasz Napierala 				    tp->t_stats_gput_prev);
339adc56f5aSEdward Tomasz Napierala 			tp->t_flags &= ~TF_GPUTINPROG;
340adc56f5aSEdward Tomasz Napierala 			tp->t_stats_gput_prev = gput;
341adc56f5aSEdward Tomasz Napierala 		}
342adc56f5aSEdward Tomasz Napierala #endif /* STATS */
343dbc42409SLawrence Stewart 		if (tp->snd_cwnd > tp->snd_ssthresh) {
3441951fa79SMichael Tuexen 			tp->t_bytes_acked += tp->ccv->bytes_this_ack;
345dbc42409SLawrence Stewart 			if (tp->t_bytes_acked >= tp->snd_cwnd) {
346dbc42409SLawrence Stewart 				tp->t_bytes_acked -= tp->snd_cwnd;
347dbc42409SLawrence Stewart 				tp->ccv->flags |= CCF_ABC_SENTAWND;
348dbc42409SLawrence Stewart 			}
349dbc42409SLawrence Stewart 		} else {
350dbc42409SLawrence Stewart 				tp->ccv->flags &= ~CCF_ABC_SENTAWND;
351dbc42409SLawrence Stewart 				tp->t_bytes_acked = 0;
352dbc42409SLawrence Stewart 		}
353dbc42409SLawrence Stewart 	}
354dbc42409SLawrence Stewart 
355dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->ack_received != NULL) {
356dbc42409SLawrence Stewart 		/* XXXLAS: Find a way to live without this */
357dbc42409SLawrence Stewart 		tp->ccv->curack = th->th_ack;
358dbc42409SLawrence Stewart 		CC_ALGO(tp)->ack_received(tp->ccv, type);
359dbc42409SLawrence Stewart 	}
360adc56f5aSEdward Tomasz Napierala #ifdef STATS
361adc56f5aSEdward Tomasz Napierala 	stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_LCWIN, tp->snd_cwnd);
362adc56f5aSEdward Tomasz Napierala #endif
363dbc42409SLawrence Stewart }
364dbc42409SLawrence Stewart 
36555bceb1eSRandall Stewart void
366dbc42409SLawrence Stewart cc_conn_init(struct tcpcb *tp)
367dbc42409SLawrence Stewart {
368dbc42409SLawrence Stewart 	struct hc_metrics_lite metrics;
369dbc42409SLawrence Stewart 	struct inpcb *inp = tp->t_inpcb;
3700c39d38dSGleb Smirnoff 	u_int maxseg;
371dbc42409SLawrence Stewart 	int rtt;
372dbc42409SLawrence Stewart 
373dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
374dbc42409SLawrence Stewart 
375dbc42409SLawrence Stewart 	tcp_hc_get(&inp->inp_inc, &metrics);
3760c39d38dSGleb Smirnoff 	maxseg = tcp_maxseg(tp);
377dbc42409SLawrence Stewart 
378dbc42409SLawrence Stewart 	if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) {
379dbc42409SLawrence Stewart 		tp->t_srtt = rtt;
380dbc42409SLawrence Stewart 		tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE;
381dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_usedrtt);
382dbc42409SLawrence Stewart 		if (metrics.rmx_rttvar) {
383dbc42409SLawrence Stewart 			tp->t_rttvar = metrics.rmx_rttvar;
384dbc42409SLawrence Stewart 			TCPSTAT_INC(tcps_usedrttvar);
385dbc42409SLawrence Stewart 		} else {
386dbc42409SLawrence Stewart 			/* default variation is +- 1 rtt */
387dbc42409SLawrence Stewart 			tp->t_rttvar =
388dbc42409SLawrence Stewart 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
389dbc42409SLawrence Stewart 		}
390dbc42409SLawrence Stewart 		TCPT_RANGESET(tp->t_rxtcur,
391dbc42409SLawrence Stewart 		    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
392dbc42409SLawrence Stewart 		    tp->t_rttmin, TCPTV_REXMTMAX);
393dbc42409SLawrence Stewart 	}
394dbc42409SLawrence Stewart 	if (metrics.rmx_ssthresh) {
395dbc42409SLawrence Stewart 		/*
396dbc42409SLawrence Stewart 		 * There's some sort of gateway or interface
397dbc42409SLawrence Stewart 		 * buffer limit on the path.  Use this to set
398a4641f4eSPedro F. Giffuni 		 * the slow start threshold, but set the
399dbc42409SLawrence Stewart 		 * threshold to no less than 2*mss.
400dbc42409SLawrence Stewart 		 */
4010c39d38dSGleb Smirnoff 		tp->snd_ssthresh = max(2 * maxseg, metrics.rmx_ssthresh);
402dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_usedssthresh);
403dbc42409SLawrence Stewart 	}
404dbc42409SLawrence Stewart 
405dbc42409SLawrence Stewart 	/*
4069ec4a4ccSAndre Oppermann 	 * Set the initial slow-start flight size.
407dbc42409SLawrence Stewart 	 *
408cf8f04f4SAndre Oppermann 	 * If a SYN or SYN/ACK was lost and retransmitted, we have to
409cf8f04f4SAndre Oppermann 	 * reduce the initial CWND to one segment as congestion is likely
410cf8f04f4SAndre Oppermann 	 * requiring us to be cautious.
411dbc42409SLawrence Stewart 	 */
412cf8f04f4SAndre Oppermann 	if (tp->snd_cwnd == 1)
4130c39d38dSGleb Smirnoff 		tp->snd_cwnd = maxseg;		/* SYN(-ACK) lost */
414dbc42409SLawrence Stewart 	else
4157dc90a1dSMichael Tuexen 		tp->snd_cwnd = tcp_compute_initwnd(maxseg);
416dbc42409SLawrence Stewart 
417dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->conn_init != NULL)
418dbc42409SLawrence Stewart 		CC_ALGO(tp)->conn_init(tp->ccv);
419dbc42409SLawrence Stewart }
420dbc42409SLawrence Stewart 
421dbc42409SLawrence Stewart void inline
422dbc42409SLawrence Stewart cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type)
423dbc42409SLawrence Stewart {
424dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
425dbc42409SLawrence Stewart 
426adc56f5aSEdward Tomasz Napierala #ifdef STATS
427adc56f5aSEdward Tomasz Napierala 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_CSIG, type);
428adc56f5aSEdward Tomasz Napierala #endif
429adc56f5aSEdward Tomasz Napierala 
430dbc42409SLawrence Stewart 	switch(type) {
431dbc42409SLawrence Stewart 	case CC_NDUPACK:
432dbc42409SLawrence Stewart 		if (!IN_FASTRECOVERY(tp->t_flags)) {
433f2512ba1SRui Paulo 			tp->snd_recover = tp->snd_max;
4343cf38784SMichael Tuexen 			if (tp->t_flags2 & TF2_ECN_PERMIT)
4353cf38784SMichael Tuexen 				tp->t_flags2 |= TF2_ECN_SND_CWR;
436f2512ba1SRui Paulo 		}
437dbc42409SLawrence Stewart 		break;
438dbc42409SLawrence Stewart 	case CC_ECN:
439af2fb894SRichard Scheffenegger 		if (!IN_CONGRECOVERY(tp->t_flags) ||
440af2fb894SRichard Scheffenegger 		    /*
441af2fb894SRichard Scheffenegger 		     * Allow ECN reaction on ACK to CWR, if
442af2fb894SRichard Scheffenegger 		     * that data segment was also CE marked.
443af2fb894SRichard Scheffenegger 		     */
444af2fb894SRichard Scheffenegger 		    SEQ_GEQ(th->th_ack, tp->snd_recover)) {
445af2fb894SRichard Scheffenegger 			EXIT_CONGRECOVERY(tp->t_flags);
446dbc42409SLawrence Stewart 			TCPSTAT_INC(tcps_ecn_rcwnd);
447af2fb894SRichard Scheffenegger 			tp->snd_recover = tp->snd_max + 1;
4483cf38784SMichael Tuexen 			if (tp->t_flags2 & TF2_ECN_PERMIT)
4493cf38784SMichael Tuexen 				tp->t_flags2 |= TF2_ECN_SND_CWR;
450dbc42409SLawrence Stewart 		}
451dbc42409SLawrence Stewart 		break;
452dbc42409SLawrence Stewart 	case CC_RTO:
453dbc42409SLawrence Stewart 		tp->t_dupacks = 0;
454dbc42409SLawrence Stewart 		tp->t_bytes_acked = 0;
455dbc42409SLawrence Stewart 		EXIT_RECOVERY(tp->t_flags);
4569cc711c9SMichael Tuexen 		if (tp->t_flags2 & TF2_ECN_PERMIT)
4579cc711c9SMichael Tuexen 			tp->t_flags2 |= TF2_ECN_SND_CWR;
458dbc42409SLawrence Stewart 		break;
459dbc42409SLawrence Stewart 	case CC_RTO_ERR:
460dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_sndrexmitbad);
461dbc42409SLawrence Stewart 		/* RTO was unnecessary, so reset everything. */
462dbc42409SLawrence Stewart 		tp->snd_cwnd = tp->snd_cwnd_prev;
463dbc42409SLawrence Stewart 		tp->snd_ssthresh = tp->snd_ssthresh_prev;
464dbc42409SLawrence Stewart 		tp->snd_recover = tp->snd_recover_prev;
465dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASFRECOVERY)
466dbc42409SLawrence Stewart 			ENTER_FASTRECOVERY(tp->t_flags);
467dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASCRECOVERY)
468dbc42409SLawrence Stewart 			ENTER_CONGRECOVERY(tp->t_flags);
469dbc42409SLawrence Stewart 		tp->snd_nxt = tp->snd_max;
470672dc4aeSJohn Baldwin 		tp->t_flags &= ~TF_PREVVALID;
471dbc42409SLawrence Stewart 		tp->t_badrxtwin = 0;
472dbc42409SLawrence Stewart 		break;
473dbc42409SLawrence Stewart 	}
474dbc42409SLawrence Stewart 
475dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->cong_signal != NULL) {
476dbc42409SLawrence Stewart 		if (th != NULL)
477dbc42409SLawrence Stewart 			tp->ccv->curack = th->th_ack;
478dbc42409SLawrence Stewart 		CC_ALGO(tp)->cong_signal(tp->ccv, type);
479dbc42409SLawrence Stewart 	}
480dbc42409SLawrence Stewart }
481dbc42409SLawrence Stewart 
48255bceb1eSRandall Stewart void inline
483dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th)
484dbc42409SLawrence Stewart {
485dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
486dbc42409SLawrence Stewart 
487dbc42409SLawrence Stewart 	/* XXXLAS: KASSERT that we're in recovery? */
488dbc42409SLawrence Stewart 
489dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->post_recovery != NULL) {
490dbc42409SLawrence Stewart 		tp->ccv->curack = th->th_ack;
491dbc42409SLawrence Stewart 		CC_ALGO(tp)->post_recovery(tp->ccv);
492dbc42409SLawrence Stewart 	}
493dbc42409SLawrence Stewart 	/* XXXLAS: EXIT_RECOVERY ? */
494dbc42409SLawrence Stewart 	tp->t_bytes_acked = 0;
49548be5b97SRichard Scheffenegger 	tp->sackhint.delivered_data = 0;
496e5313869SRichard Scheffenegger 	tp->sackhint.prr_out = 0;
497dbc42409SLawrence Stewart }
4980312fbe9SPoul-Henning Kamp 
499df8bae1dSRodney W. Grimes /*
500262c1c1aSMatthew Dillon  * Indicate whether this ack should be delayed.  We can delay the ack if
50185536381SHiren Panchasara  * following conditions are met:
50285536381SHiren Panchasara  *	- There is no delayed ack timer in progress.
50385536381SHiren Panchasara  *	- Our last ack wasn't a 0-sized window. We never want to delay
50485536381SHiren Panchasara  *	  the ack that opens up a 0-sized window.
50585536381SHiren Panchasara  *	- LRO wasn't used for this segment. We make sure by checking that the
50685536381SHiren Panchasara  *	  segment size is not larger than the MSS.
507d8c85a26SJonathan Lemon  */
508c1e5a6e5SAndre Oppermann #define DELAY_ACK(tp, tlen)						\
509b8152ba7SAndre Oppermann 	((!tcp_timer_active(tp, TT_DELACK) &&				\
510a14c749fSJonathan Lemon 	    (tp->t_flags & TF_RXWIN0SENT) == 0) &&			\
5110c39d38dSGleb Smirnoff 	    (tlen <= tp->t_maxseg) &&					\
512603724d3SBjoern A. Zeeb 	    (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN)))
513d8c85a26SJonathan Lemon 
5144ad24737SRandall Stewart void inline
5155d8fd932SRandall Stewart cc_ecnpkt_handler_flags(struct tcpcb *tp, uint16_t flags, uint8_t iptos)
51664807b30SHiren Panchasara {
51764807b30SHiren Panchasara 	INP_WLOCK_ASSERT(tp->t_inpcb);
51864807b30SHiren Panchasara 
51964807b30SHiren Panchasara 	if (CC_ALGO(tp)->ecnpkt_handler != NULL) {
52064807b30SHiren Panchasara 		switch (iptos & IPTOS_ECN_MASK) {
52164807b30SHiren Panchasara 		case IPTOS_ECN_CE:
52264807b30SHiren Panchasara 			tp->ccv->flags |= CCF_IPHDR_CE;
52364807b30SHiren Panchasara 			break;
52464807b30SHiren Panchasara 		case IPTOS_ECN_ECT0:
52583a2839fSMichael Tuexen 			/* FALLTHROUGH */
52664807b30SHiren Panchasara 		case IPTOS_ECN_ECT1:
52783a2839fSMichael Tuexen 			/* FALLTHROUGH */
52883a2839fSMichael Tuexen 		case IPTOS_ECN_NOTECT:
52964807b30SHiren Panchasara 			tp->ccv->flags &= ~CCF_IPHDR_CE;
53064807b30SHiren Panchasara 			break;
53164807b30SHiren Panchasara 		}
53264807b30SHiren Panchasara 
5335d8fd932SRandall Stewart 		if (flags & TH_CWR)
53464807b30SHiren Panchasara 			tp->ccv->flags |= CCF_TCPHDR_CWR;
53564807b30SHiren Panchasara 		else
53664807b30SHiren Panchasara 			tp->ccv->flags &= ~CCF_TCPHDR_CWR;
53764807b30SHiren Panchasara 
53864807b30SHiren Panchasara 		CC_ALGO(tp)->ecnpkt_handler(tp->ccv);
53964807b30SHiren Panchasara 
540e11c9783SMichael Tuexen 		if (tp->ccv->flags & CCF_ACKNOW) {
54164807b30SHiren Panchasara 			tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
542e11c9783SMichael Tuexen 			tp->t_flags |= TF_ACKNOW;
543e11c9783SMichael Tuexen 		}
54464807b30SHiren Panchasara 	}
54564807b30SHiren Panchasara }
54664807b30SHiren Panchasara 
5475d8fd932SRandall Stewart void inline
5485d8fd932SRandall Stewart cc_ecnpkt_handler(struct tcpcb *tp, struct tcphdr *th, uint8_t iptos)
5495d8fd932SRandall Stewart {
5501ebf4607SRichard Scheffenegger 	cc_ecnpkt_handler_flags(tp, tcp_get_flags(th), iptos);
5515d8fd932SRandall Stewart }
5525d8fd932SRandall Stewart 
553df8bae1dSRodney W. Grimes /*
5548d573cc1SAndre Oppermann  * TCP input handling is split into multiple parts:
5558d573cc1SAndre Oppermann  *   tcp6_input is a thin wrapper around tcp_input for the extended
5568d573cc1SAndre Oppermann  *	ip6_protox[] call format in ip6_input
5578d573cc1SAndre Oppermann  *   tcp_input handles primary segment validation, inpcb lookup and
5588d573cc1SAndre Oppermann  *	SYN processing on listen sockets
5598d573cc1SAndre Oppermann  *   tcp_do_segment processes the ACK and text of the segment for
5608d573cc1SAndre Oppermann  *	establishing, established and closing connections
561df8bae1dSRodney W. Grimes  */
562fb59c426SYoshinobu Inoue #ifdef INET6
563fb59c426SYoshinobu Inoue int
5649e644c23SMichael Tuexen tcp6_input_with_port(struct mbuf **mp, int *offp, int proto, uint16_t port)
565fb59c426SYoshinobu Inoue {
566503f4e47SBjoern A. Zeeb 	struct mbuf *m;
56733841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6;
5683e88eb90SAndrey V. Elsukov 	struct ip6_hdr *ip6;
569fb59c426SYoshinobu Inoue 
570503f4e47SBjoern A. Zeeb 	m = *mp;
571a4adf6ccSBjoern A. Zeeb 	if (m->m_len < *offp + sizeof(struct tcphdr)) {
5724e619b17SBjoern A. Zeeb 		m = m_pullup(m, *offp + sizeof(struct tcphdr));
5734e619b17SBjoern A. Zeeb 		if (m == NULL) {
5744e619b17SBjoern A. Zeeb 			*mp = m;
5754e619b17SBjoern A. Zeeb 			TCPSTAT_INC(tcps_rcvshort);
5764e619b17SBjoern A. Zeeb 			return (IPPROTO_DONE);
5774e619b17SBjoern A. Zeeb 		}
578a4adf6ccSBjoern A. Zeeb 	}
579fb59c426SYoshinobu Inoue 
580fb59c426SYoshinobu Inoue 	/*
581fb59c426SYoshinobu Inoue 	 * draft-itojun-ipv6-tcp-to-anycast
582fb59c426SYoshinobu Inoue 	 * better place to put this in?
583fb59c426SYoshinobu Inoue 	 */
5843e88eb90SAndrey V. Elsukov 	ip6 = mtod(m, struct ip6_hdr *);
5858268d82cSAlexander V. Chernikov 	ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false);
58633841545SHajimu UMEMOTO 	if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
587fb59c426SYoshinobu Inoue 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
588fb59c426SYoshinobu Inoue 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
589a8fe77d8SBjoern A. Zeeb 		*mp = NULL;
5908f5a8818SKevin Lo 		return (IPPROTO_DONE);
591fb59c426SYoshinobu Inoue 	}
592fb59c426SYoshinobu Inoue 
593a8fe77d8SBjoern A. Zeeb 	*mp = m;
5949e644c23SMichael Tuexen 	return (tcp_input_with_port(mp, offp, proto, port));
5959e644c23SMichael Tuexen }
5969e644c23SMichael Tuexen 
5979e644c23SMichael Tuexen int
5989e644c23SMichael Tuexen tcp6_input(struct mbuf **mp, int *offp, int proto)
5999e644c23SMichael Tuexen {
6009e644c23SMichael Tuexen 
6019e644c23SMichael Tuexen 	return(tcp6_input_with_port(mp, offp, proto, 0));
602fb59c426SYoshinobu Inoue }
603b287c6c7SBjoern A. Zeeb #endif /* INET6 */
604fb59c426SYoshinobu Inoue 
6058f5a8818SKevin Lo int
6069e644c23SMichael Tuexen tcp_input_with_port(struct mbuf **mp, int *offp, int proto, uint16_t port)
607df8bae1dSRodney W. Grimes {
6088f5a8818SKevin Lo 	struct mbuf *m = *mp;
609b287c6c7SBjoern A. Zeeb 	struct tcphdr *th = NULL;
610ad3f9ab3SAndre Oppermann 	struct ip *ip = NULL;
611ad3f9ab3SAndre Oppermann 	struct inpcb *inp = NULL;
612302ce8d6SAndre Oppermann 	struct tcpcb *tp = NULL;
613302ce8d6SAndre Oppermann 	struct socket *so = NULL;
614e79adb8eSGarrett Wollman 	u_char *optp = NULL;
6158f5a8818SKevin Lo 	int off0;
61626f9a767SRodney W. Grimes 	int optlen = 0;
617b287c6c7SBjoern A. Zeeb #ifdef INET
618b287c6c7SBjoern A. Zeeb 	int len;
619c4556b2fSAndrey V. Elsukov 	uint8_t ipttl;
620b287c6c7SBjoern A. Zeeb #endif
621b287c6c7SBjoern A. Zeeb 	int tlen = 0, off;
622fb59c426SYoshinobu Inoue 	int drop_hdrlen;
623ad3f9ab3SAndre Oppermann 	int thflags;
624302ce8d6SAndre Oppermann 	int rstreason = 0;	/* For badport_bandlim accounting purposes */
62508d9c920SGleb Smirnoff 	int lookupflag;
6261d7ee746SKurt Lidl 	uint8_t iptos;
627c1de64a4SAndrey V. Elsukov 	struct m_tag *fwd_tag = NULL;
628c068736aSJeffrey Hsu #ifdef INET6
629302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6 = NULL;
630c068736aSJeffrey Hsu 	int isipv6;
631c068736aSJeffrey Hsu #else
632a160e630SAndre Oppermann 	const void *ip6 = NULL;
633b287c6c7SBjoern A. Zeeb #endif /* INET6 */
634302ce8d6SAndre Oppermann 	struct tcpopt to;		/* options in this segment */
635a160e630SAndre Oppermann 	char *s = NULL;			/* address and port logging */
636610ee2f9SDavid Greenman #ifdef TCPDEBUG
637c068736aSJeffrey Hsu 	/*
638c068736aSJeffrey Hsu 	 * The size of tcp_saveipgen must be the size of the max ip header,
639c068736aSJeffrey Hsu 	 * now IPv6.
640c068736aSJeffrey Hsu 	 */
64114739780SMaxim Konovalov 	u_char tcp_saveipgen[IP6_HDR_LEN];
642410bb1bfSLuigi Rizzo 	struct tcphdr tcp_savetcp;
643610ee2f9SDavid Greenman 	short ostate = 0;
644610ee2f9SDavid Greenman #endif
645c068736aSJeffrey Hsu 
646d40c0d47SGleb Smirnoff 	NET_EPOCH_ASSERT();
647d40c0d47SGleb Smirnoff 
648fb59c426SYoshinobu Inoue #ifdef INET6
649fb59c426SYoshinobu Inoue 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
650fb59c426SYoshinobu Inoue #endif
651a0292f23SGarrett Wollman 
6528f5a8818SKevin Lo 	off0 = *offp;
6538f5a8818SKevin Lo 	m = *mp;
6548f5a8818SKevin Lo 	*mp = NULL;
655302ce8d6SAndre Oppermann 	to.to_flags = 0;
65678b50714SRobert Watson 	TCPSTAT_INC(tcps_rcvtotal);
657fb59c426SYoshinobu Inoue 
65804d3a452SHajimu UMEMOTO #ifdef INET6
659b287c6c7SBjoern A. Zeeb 	if (isipv6) {
660fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
66145747ba5SBjoern A. Zeeb 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
662fb59c426SYoshinobu Inoue 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
6639e644c23SMichael Tuexen 		if (port)
6649e644c23SMichael Tuexen 			goto skip6_csum;
665356ab07eSBjoern A. Zeeb 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) {
66645747ba5SBjoern A. Zeeb 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
66745747ba5SBjoern A. Zeeb 				th->th_sum = m->m_pkthdr.csum_data;
66845747ba5SBjoern A. Zeeb 			else
66945747ba5SBjoern A. Zeeb 				th->th_sum = in6_cksum_pseudo(ip6, tlen,
67045747ba5SBjoern A. Zeeb 				    IPPROTO_TCP, m->m_pkthdr.csum_data);
67145747ba5SBjoern A. Zeeb 			th->th_sum ^= 0xffff;
67245747ba5SBjoern A. Zeeb 		} else
67345747ba5SBjoern A. Zeeb 			th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen);
67445747ba5SBjoern A. Zeeb 		if (th->th_sum) {
67578b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
676fb59c426SYoshinobu Inoue 			goto drop;
677fb59c426SYoshinobu Inoue 		}
6789e644c23SMichael Tuexen 	skip6_csum:
67933841545SHajimu UMEMOTO 		/*
68033841545SHajimu UMEMOTO 		 * Be proactive about unspecified IPv6 address in source.
68133841545SHajimu UMEMOTO 		 * As we use all-zero to indicate unbounded/unconnected pcb,
68233841545SHajimu UMEMOTO 		 * unspecified IPv6 address can be used to confuse us.
68333841545SHajimu UMEMOTO 		 *
68433841545SHajimu UMEMOTO 		 * Note that packets with unspecified IPv6 destination is
68533841545SHajimu UMEMOTO 		 * already dropped in ip6_input.
68633841545SHajimu UMEMOTO 		 */
68733841545SHajimu UMEMOTO 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
68833841545SHajimu UMEMOTO 			/* XXX stat */
68933841545SHajimu UMEMOTO 			goto drop;
69033841545SHajimu UMEMOTO 		}
691bb4a7d94SKristof Provost 		iptos = IPV6_TRAFFIC_CLASS(ip6);
692b287c6c7SBjoern A. Zeeb 	}
69304d3a452SHajimu UMEMOTO #endif
694b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
695b287c6c7SBjoern A. Zeeb 	else
696b287c6c7SBjoern A. Zeeb #endif
697b287c6c7SBjoern A. Zeeb #ifdef INET
698b287c6c7SBjoern A. Zeeb 	{
699df8bae1dSRodney W. Grimes 		/*
700df8bae1dSRodney W. Grimes 		 * Get IP and TCP header together in first mbuf.
701df8bae1dSRodney W. Grimes 		 * Note: IP leaves IP header in first mbuf.
702df8bae1dSRodney W. Grimes 		 */
703fb59c426SYoshinobu Inoue 		if (off0 > sizeof (struct ip)) {
704105bd211SGleb Smirnoff 			ip_stripoptions(m);
705fb59c426SYoshinobu Inoue 			off0 = sizeof(struct ip);
706fb59c426SYoshinobu Inoue 		}
707df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof (struct tcpiphdr)) {
7084dfdffe9SAndre Oppermann 			if ((m = m_pullup(m, sizeof (struct tcpiphdr)))
7094dfdffe9SAndre Oppermann 			    == NULL) {
71078b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvshort);
7118f5a8818SKevin Lo 				return (IPPROTO_DONE);
712df8bae1dSRodney W. Grimes 			}
713df8bae1dSRodney W. Grimes 		}
714fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
715db4f9cc7SJonathan Lemon 		th = (struct tcphdr *)((caddr_t)ip + off0);
7168ad458a4SGleb Smirnoff 		tlen = ntohs(ip->ip_len) - off0;
717df8bae1dSRodney W. Grimes 
7181d7ee746SKurt Lidl 		iptos = ip->ip_tos;
7199e644c23SMichael Tuexen 		if (port)
7209e644c23SMichael Tuexen 			goto skip_csum;
721db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
722db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
723db4f9cc7SJonathan Lemon 				th->th_sum = m->m_pkthdr.csum_data;
724db4f9cc7SJonathan Lemon 			else
725db4f9cc7SJonathan Lemon 				th->th_sum = in_pseudo(ip->ip_src.s_addr,
726c068736aSJeffrey Hsu 				    ip->ip_dst.s_addr,
7278f134647SGleb Smirnoff 				    htonl(m->m_pkthdr.csum_data + tlen +
728c068736aSJeffrey Hsu 				    IPPROTO_TCP));
729db4f9cc7SJonathan Lemon 			th->th_sum ^= 0xffff;
730db4f9cc7SJonathan Lemon 		} else {
7318f134647SGleb Smirnoff 			struct ipovly *ipov = (struct ipovly *)ip;
7328f134647SGleb Smirnoff 
733df8bae1dSRodney W. Grimes 			/*
734df8bae1dSRodney W. Grimes 			 * Checksum extended TCP header and data.
735df8bae1dSRodney W. Grimes 			 */
7368f134647SGleb Smirnoff 			len = off0 + tlen;
737c4556b2fSAndrey V. Elsukov 			ipttl = ip->ip_ttl;
738fb59c426SYoshinobu Inoue 			bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
7398f134647SGleb Smirnoff 			ipov->ih_len = htons(tlen);
740fb59c426SYoshinobu Inoue 			th->th_sum = in_cksum(m, len);
74157f60867SMark Johnston 			/* Reset length for SDT probes. */
7421d7ee746SKurt Lidl 			ip->ip_len = htons(len);
7431d7ee746SKurt Lidl 			/* Reset TOS bits */
7441d7ee746SKurt Lidl 			ip->ip_tos = iptos;
7451d7ee746SKurt Lidl 			/* Re-initialization for later version check */
746c4556b2fSAndrey V. Elsukov 			ip->ip_ttl = ipttl;
7471d7ee746SKurt Lidl 			ip->ip_v = IPVERSION;
748b2fe54bcSAndrey V. Elsukov 			ip->ip_hl = off0 >> 2;
749db4f9cc7SJonathan Lemon 		}
7509e644c23SMichael Tuexen 	skip_csum:
7519e644c23SMichael Tuexen 		if (th->th_sum && (port == 0)) {
75278b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
753df8bae1dSRodney W. Grimes 			goto drop;
754df8bae1dSRodney W. Grimes 		}
755fb59c426SYoshinobu Inoue 	}
756b287c6c7SBjoern A. Zeeb #endif /* INET */
757df8bae1dSRodney W. Grimes 
758df8bae1dSRodney W. Grimes 	/*
759df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
760df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
761df8bae1dSRodney W. Grimes 	 */
762fb59c426SYoshinobu Inoue 	off = th->th_off << 2;
763df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
76478b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvbadoff);
765df8bae1dSRodney W. Grimes 		goto drop;
766df8bae1dSRodney W. Grimes 	}
767fb59c426SYoshinobu Inoue 	tlen -= off;	/* tlen is used instead of ti->ti_len */
768df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
76904d3a452SHajimu UMEMOTO #ifdef INET6
770b287c6c7SBjoern A. Zeeb 		if (isipv6) {
771a4adf6ccSBjoern A. Zeeb 			if (m->m_len < off0 + off) {
7724e619b17SBjoern A. Zeeb 				m = m_pullup(m, off0 + off);
7734e619b17SBjoern A. Zeeb 				if (m == NULL) {
7744e619b17SBjoern A. Zeeb 					TCPSTAT_INC(tcps_rcvshort);
7754e619b17SBjoern A. Zeeb 					return (IPPROTO_DONE);
7764e619b17SBjoern A. Zeeb 				}
777a4adf6ccSBjoern A. Zeeb 			}
778fb59c426SYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
779fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
780b287c6c7SBjoern A. Zeeb 		}
78104d3a452SHajimu UMEMOTO #endif
782b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
783b287c6c7SBjoern A. Zeeb 		else
784b287c6c7SBjoern A. Zeeb #endif
785b287c6c7SBjoern A. Zeeb #ifdef INET
786b287c6c7SBjoern A. Zeeb 		{
787df8bae1dSRodney W. Grimes 			if (m->m_len < sizeof(struct ip) + off) {
788c068736aSJeffrey Hsu 				if ((m = m_pullup(m, sizeof (struct ip) + off))
7894dfdffe9SAndre Oppermann 				    == NULL) {
79078b50714SRobert Watson 					TCPSTAT_INC(tcps_rcvshort);
7918f5a8818SKevin Lo 					return (IPPROTO_DONE);
792df8bae1dSRodney W. Grimes 				}
793fb59c426SYoshinobu Inoue 				ip = mtod(m, struct ip *);
794fb59c426SYoshinobu Inoue 				th = (struct tcphdr *)((caddr_t)ip + off0);
795fb59c426SYoshinobu Inoue 			}
796df8bae1dSRodney W. Grimes 		}
797b287c6c7SBjoern A. Zeeb #endif
798df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
799fb59c426SYoshinobu Inoue 		optp = (u_char *)(th + 1);
800df8bae1dSRodney W. Grimes 	}
8011ebf4607SRichard Scheffenegger 	thflags = tcp_get_flags(th);
802df8bae1dSRodney W. Grimes 
803e46cd3d4SDag-Erling Smørgrav 	/*
804df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
805df8bae1dSRodney W. Grimes 	 */
8062903309aSAttilio Rao 	tcp_fields_to_host(th);
807df8bae1dSRodney W. Grimes 
808df8bae1dSRodney W. Grimes 	/*
809794235b7SAndre Oppermann 	 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options.
810755c1f07SAndras Olah 	 */
811fb59c426SYoshinobu Inoue 	drop_hdrlen = off0 + off;
812755c1f07SAndras Olah 
813755c1f07SAndras Olah 	/*
8148d573cc1SAndre Oppermann 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
8158d573cc1SAndre Oppermann 	 */
816b8056faeSGleb Smirnoff         if (
817b8056faeSGleb Smirnoff #ifdef INET6
818b8056faeSGleb Smirnoff 	    (isipv6 && (m->m_flags & M_IP6_NEXTHOP))
819b8056faeSGleb Smirnoff #ifdef INET
820b8056faeSGleb Smirnoff 	    || (!isipv6 && (m->m_flags & M_IP_NEXTHOP))
821b8056faeSGleb Smirnoff #endif
822b8056faeSGleb Smirnoff #endif
823b8056faeSGleb Smirnoff #if defined(INET) && !defined(INET6)
824b8056faeSGleb Smirnoff 	    (m->m_flags & M_IP_NEXTHOP)
825b8056faeSGleb Smirnoff #endif
826b8056faeSGleb Smirnoff 	    )
8279b932e9eSAndre Oppermann 		fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
8289b932e9eSAndre Oppermann 
82908d9c920SGleb Smirnoff 	/*
8301db08fbeSGleb Smirnoff 	 * For initial SYN packets we don't need write lock on matching
8311db08fbeSGleb Smirnoff 	 * PCB, be it a listening one or a synchronized one.  The packet
8321db08fbeSGleb Smirnoff 	 * shall not modify its state.
83308d9c920SGleb Smirnoff 	 */
834d88eb465SGleb Smirnoff 	lookupflag = INPLOOKUP_WILDCARD |
835d88eb465SGleb Smirnoff 	    ((thflags & (TH_ACK|TH_SYN)) == TH_SYN ?
836d88eb465SGleb Smirnoff 	    INPLOOKUP_RLOCKPCB : INPLOOKUP_WLOCKPCB);
837ce7ad664SEd Maste findpcb:
8388a006adbSBjoern A. Zeeb #ifdef INET6
8398a006adbSBjoern A. Zeeb 	if (isipv6 && fwd_tag != NULL) {
8408a006adbSBjoern A. Zeeb 		struct sockaddr_in6 *next_hop6;
8418a006adbSBjoern A. Zeeb 
8428a006adbSBjoern A. Zeeb 		next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
8438a006adbSBjoern A. Zeeb 		/*
8448a006adbSBjoern A. Zeeb 		 * Transparently forwarded. Pretend to be the destination.
8458a006adbSBjoern A. Zeeb 		 * Already got one like this?
8468a006adbSBjoern A. Zeeb 		 */
8478a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo,
8488a006adbSBjoern A. Zeeb 		    &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport,
84908d9c920SGleb Smirnoff 		    lookupflag, m->m_pkthdr.rcvif, m);
8508a006adbSBjoern A. Zeeb 		if (!inp) {
8518a006adbSBjoern A. Zeeb 			/*
8528a006adbSBjoern A. Zeeb 			 * It's new.  Try to find the ambushing socket.
8538a006adbSBjoern A. Zeeb 			 * Because we've rewritten the destination address,
8548a006adbSBjoern A. Zeeb 			 * any hardware-generated hash is ignored.
8558a006adbSBjoern A. Zeeb 			 */
8568a006adbSBjoern A. Zeeb 			inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src,
8578a006adbSBjoern A. Zeeb 			    th->th_sport, &next_hop6->sin6_addr,
8588a006adbSBjoern A. Zeeb 			    next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) :
859d88eb465SGleb Smirnoff 			    th->th_dport, lookupflag, m->m_pkthdr.rcvif);
8608a006adbSBjoern A. Zeeb 		}
861c1de64a4SAndrey V. Elsukov 	} else if (isipv6) {
8628a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src,
863d88eb465SGleb Smirnoff 		    th->th_sport, &ip6->ip6_dst, th->th_dport, lookupflag,
864d88eb465SGleb Smirnoff 		    m->m_pkthdr.rcvif, m);
8658a006adbSBjoern A. Zeeb 	}
8668a006adbSBjoern A. Zeeb #endif /* INET6 */
8678a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET)
8688a006adbSBjoern A. Zeeb 	else
8698a006adbSBjoern A. Zeeb #endif
8708a006adbSBjoern A. Zeeb #ifdef INET
8718a006adbSBjoern A. Zeeb 	if (fwd_tag != NULL) {
8729b932e9eSAndre Oppermann 		struct sockaddr_in *next_hop;
8739b932e9eSAndre Oppermann 
8749b932e9eSAndre Oppermann 		next_hop = (struct sockaddr_in *)(fwd_tag+1);
875f9e354dfSJulian Elischer 		/*
8762b25acc1SLuigi Rizzo 		 * Transparently forwarded. Pretend to be the destination.
877f9e354dfSJulian Elischer 		 * already got one like this?
878f9e354dfSJulian Elischer 		 */
879d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport,
88008d9c920SGleb Smirnoff 		    ip->ip_dst, th->th_dport, lookupflag, m->m_pkthdr.rcvif, m);
881fa046d87SRobert Watson 		if (!inp) {
882fa046d87SRobert Watson 			/*
883fa046d87SRobert Watson 			 * It's new.  Try to find the ambushing socket.
884d3c1f003SRobert Watson 			 * Because we've rewritten the destination address,
885d3c1f003SRobert Watson 			 * any hardware-generated hash is ignored.
886fa046d87SRobert Watson 			 */
887fa046d87SRobert Watson 			inp = in_pcblookup(&V_tcbinfo, ip->ip_src,
888fa046d87SRobert Watson 			    th->th_sport, next_hop->sin_addr,
889fa046d87SRobert Watson 			    next_hop->sin_port ? ntohs(next_hop->sin_port) :
890d88eb465SGleb Smirnoff 			    th->th_dport, lookupflag, m->m_pkthdr.rcvif);
891f9e354dfSJulian Elischer 		}
892b10fbdeaSAndre Oppermann 	} else
893d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src,
894d88eb465SGleb Smirnoff 		    th->th_sport, ip->ip_dst, th->th_dport, lookupflag,
895d88eb465SGleb Smirnoff 		    m->m_pkthdr.rcvif, m);
8968a006adbSBjoern A. Zeeb #endif /* INET */
897f9e354dfSJulian Elischer 
898df8bae1dSRodney W. Grimes 	/*
899574b6964SAndre Oppermann 	 * If the INPCB does not exist then all data in the incoming
900574b6964SAndre Oppermann 	 * segment is discarded and an appropriate RST is sent back.
9018b07e49aSJulian Elischer 	 * XXX MRT Send RST using which routing table?
902df8bae1dSRodney W. Grimes 	 */
903816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
904d88eb465SGleb Smirnoff 		if (rstreason != 0) {
905d88eb465SGleb Smirnoff 			/* We came here after second (safety) lookup. */
906d88eb465SGleb Smirnoff 			MPASS((lookupflag & INPLOOKUP_WILDCARD) == 0);
907d88eb465SGleb Smirnoff 			goto dropwithreset;
908d88eb465SGleb Smirnoff 		}
909574b6964SAndre Oppermann 		/*
910574b6964SAndre Oppermann 		 * Log communication attempts to ports that are not
911574b6964SAndre Oppermann 		 * in use.
912574b6964SAndre Oppermann 		 */
913334fc582SBjoern A. Zeeb 		if ((V_tcp_log_in_vain == 1 && (thflags & TH_SYN)) ||
914334fc582SBjoern A. Zeeb 		    V_tcp_log_in_vain == 2) {
915b7d747ecSAndre Oppermann 			if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6)))
916df541e5fSAndre Oppermann 				log(LOG_INFO, "%s; %s: Connection attempt "
917df541e5fSAndre Oppermann 				    "to closed port\n", s, __func__);
9182e4e1b4cSGeoff Rehmet 		}
919574b6964SAndre Oppermann 		/*
920574b6964SAndre Oppermann 		 * When blackholing do not respond with a RST but
921574b6964SAndre Oppermann 		 * completely ignore the segment and drop it.
922574b6964SAndre Oppermann 		 */
9233ea9a7cfSGleb Smirnoff 		if (((V_blackhole == 1 && (thflags & TH_SYN)) ||
924ad3ad064SGleb Smirnoff 		    V_blackhole == 2) && (V_blackhole_local || (
9253ea9a7cfSGleb Smirnoff #ifdef INET6
9263ea9a7cfSGleb Smirnoff 		    isipv6 ? !in6_localaddr(&ip6->ip6_src) :
9273ea9a7cfSGleb Smirnoff #endif
9283ea9a7cfSGleb Smirnoff #ifdef INET
9293ea9a7cfSGleb Smirnoff 		    !in_localip(ip->ip_src)
9303ea9a7cfSGleb Smirnoff #else
9313ea9a7cfSGleb Smirnoff 		    true
9323ea9a7cfSGleb Smirnoff #endif
933ad3ad064SGleb Smirnoff 		    )))
9341929eae1SAndre Oppermann 			goto dropunlock;
935574b6964SAndre Oppermann 
936a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
937a57815efSBosko Milekic 		goto dropwithreset;
938816a3d83SPoul-Henning Kamp 	}
93908d9c920SGleb Smirnoff 	INP_LOCK_ASSERT(inp);
940f567d55fSGleb Smirnoff 
941c2529042SHans Petter Selasky 	if ((inp->inp_flowtype == M_HASHTYPE_NONE) &&
942c2529042SHans Petter Selasky 	    (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) &&
943*f71cb9f7SGleb Smirnoff 	    !SOLISTENING(inp->inp_socket)) {
94480cb9f21SKip Macy 		inp->inp_flowid = m->m_pkthdr.flowid;
9452f719932SAdrian Chadd 		inp->inp_flowtype = M_HASHTYPE_GET(m);
94680cb9f21SKip Macy 	}
947fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
948a2589465SKen Smith #ifdef INET6
949fcf59617SAndrey V. Elsukov 	if (isipv6 && IPSEC_ENABLED(ipv6) &&
950fcf59617SAndrey V. Elsukov 	    IPSEC_CHECK_POLICY(ipv6, m, inp) != 0) {
951a2589465SKen Smith 		goto dropunlock;
952a2589465SKen Smith 	}
953fcf59617SAndrey V. Elsukov #ifdef INET
954fcf59617SAndrey V. Elsukov 	else
955fcf59617SAndrey V. Elsukov #endif
956fcf59617SAndrey V. Elsukov #endif /* INET6 */
957fcf59617SAndrey V. Elsukov #ifdef INET
958fcf59617SAndrey V. Elsukov 	if (IPSEC_ENABLED(ipv4) &&
959fcf59617SAndrey V. Elsukov 	    IPSEC_CHECK_POLICY(ipv4, m, inp) != 0) {
960fcf59617SAndrey V. Elsukov 		goto dropunlock;
961fcf59617SAndrey V. Elsukov 	}
962fcf59617SAndrey V. Elsukov #endif /* INET */
963a2589465SKen Smith #endif /* IPSEC */
964a2589465SKen Smith 
9658d573cc1SAndre Oppermann 	/*
9668d573cc1SAndre Oppermann 	 * Check the minimum TTL for socket.
9678d573cc1SAndre Oppermann 	 */
96834f83c52SGeorge V. Neville-Neil 	if (inp->inp_ip_minttl != 0) {
96934f83c52SGeorge V. Neville-Neil #ifdef INET6
9702d8868dbSJonathan T. Looney 		if (isipv6) {
9712d8868dbSJonathan T. Looney 			if (inp->inp_ip_minttl > ip6->ip6_hlim)
972302ce8d6SAndre Oppermann 				goto dropunlock;
9732d8868dbSJonathan T. Looney 		} else
97434f83c52SGeorge V. Neville-Neil #endif
97534f83c52SGeorge V. Neville-Neil 		if (inp->inp_ip_minttl > ip->ip_ttl)
976302ce8d6SAndre Oppermann 			goto dropunlock;
97734f83c52SGeorge V. Neville-Neil 	}
978936cd18dSAndre Oppermann 
9790d744519SGleb Smirnoff 	tp = intotcpcb(inp);
9800d744519SGleb Smirnoff 	switch (tp->t_state) {
9810d744519SGleb Smirnoff 	case TCPS_TIME_WAIT:
982340c35deSJonathan Lemon 		/*
9830d744519SGleb Smirnoff 		 * A previous connection in TIMEWAIT state is supposed to catch
9840d744519SGleb Smirnoff 		 * stray or duplicate segments arriving late.  If this segment
9850d744519SGleb Smirnoff 		 * was a legitimate new connection attempt, the old INPCB gets
9860d744519SGleb Smirnoff 		 * removed and we can try again to find a listening socket.
987340c35deSJonathan Lemon 		 */
988283c76c7SMichael Tuexen 		tcp_dooptions(&to, optp, optlen,
989283c76c7SMichael Tuexen 		    (thflags & TH_SYN) ? TO_SYN : 0);
9908d573cc1SAndre Oppermann 		/*
9910d744519SGleb Smirnoff 		 * tcp_twcheck unlocks the inp always, and frees the m if fails.
9928d573cc1SAndre Oppermann 		 */
9932104448fSAndre Oppermann 		if (tcp_twcheck(inp, &to, th, m, tlen))
994340c35deSJonathan Lemon 			goto findpcb;
9958f5a8818SKevin Lo 		return (IPPROTO_DONE);
9960d744519SGleb Smirnoff 	case TCPS_CLOSED:
997794235b7SAndre Oppermann 		/*
998794235b7SAndre Oppermann 		 * The TCPCB may no longer exist if the connection is winding
999794235b7SAndre Oppermann 		 * down or it is in the CLOSED state.  Either way we drop the
1000794235b7SAndre Oppermann 		 * segment and send an appropriate response.
1001794235b7SAndre Oppermann 		 */
1002a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
1003a57815efSBosko Milekic 		goto dropwithreset;
100409f81a46SBosko Milekic 	}
1005df8bae1dSRodney W. Grimes 
10069e644c23SMichael Tuexen 	if ((tp->t_port != port) && (tp->t_state > TCPS_LISTEN)) {
10079e644c23SMichael Tuexen 		rstreason = BANDLIM_RST_CLOSEDPORT;
10089e644c23SMichael Tuexen 		goto dropwithreset;
10099e644c23SMichael Tuexen 	}
10109e644c23SMichael Tuexen 
101109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
101209fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
101309fe6320SNavdeep Parhar 		tcp_offload_input(tp, m);
101409fe6320SNavdeep Parhar 		m = NULL;	/* consumed by the TOE driver */
101509fe6320SNavdeep Parhar 		goto dropunlock;
101609fe6320SNavdeep Parhar 	}
101709fe6320SNavdeep Parhar #endif
101809fe6320SNavdeep Parhar 
1019c488362eSRobert Watson #ifdef MAC
102030d239bcSRobert Watson 	if (mac_inpcb_check_deliver(inp, m))
1021302ce8d6SAndre Oppermann 		goto dropunlock;
1022c488362eSRobert Watson #endif
1023a557af22SRobert Watson 	so = inp->inp_socket;
1024302ce8d6SAndre Oppermann 	KASSERT(so != NULL, ("%s: so == NULL", __func__));
1025610ee2f9SDavid Greenman #ifdef TCPDEBUG
1026df8bae1dSRodney W. Grimes 	if (so->so_options & SO_DEBUG) {
1027df8bae1dSRodney W. Grimes 		ostate = tp->t_state;
102810fe523eSMaxim Konovalov #ifdef INET6
10296f697424SBjoern A. Zeeb 		if (isipv6) {
1030540e8b7eSJeffrey Hsu 			bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6));
1031d30d90dcSMaxim Konovalov 		} else
10326f697424SBjoern A. Zeeb #endif
1033540e8b7eSJeffrey Hsu 			bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
1034fb59c426SYoshinobu Inoue 		tcp_savetcp = *th;
1035df8bae1dSRodney W. Grimes 	}
1036b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */
1037db33b3e6SAndre Oppermann 	/*
1038db33b3e6SAndre Oppermann 	 * When the socket is accepting connections (the INPCB is in LISTEN
1039db33b3e6SAndre Oppermann 	 * state) we look into the SYN cache if this is a new connection
1040a7c7f2a7SJohn Baldwin 	 * attempt or the completion of a previous one.
1041db33b3e6SAndre Oppermann 	 */
1042f4bb1869SMark Johnston 	KASSERT(tp->t_state == TCPS_LISTEN || !SOLISTENING(so),
104345274760SJonathan T. Looney 	    ("%s: so accepting but tp %p not listening", __func__, tp));
1044f4bb1869SMark Johnston 	if (tp->t_state == TCPS_LISTEN && SOLISTENING(so)) {
1045540e8b7eSJeffrey Hsu 		struct in_conninfo inc;
1046540e8b7eSJeffrey Hsu 
10478bfb1918SAndre Oppermann 		bzero(&inc, sizeof(inc));
1048302ce8d6SAndre Oppermann #ifdef INET6
1049be2ac88cSJonathan Lemon 		if (isipv6) {
1050dcdb4371SBjoern A. Zeeb 			inc.inc_flags |= INC_ISIPV6;
10515dff1c38SMichael Tuexen 			if (inp->inp_inc.inc_flags & INC_IPV6MINMTU)
10525dff1c38SMichael Tuexen 				inc.inc_flags |= INC_IPV6MINMTU;
1053be2ac88cSJonathan Lemon 			inc.inc6_faddr = ip6->ip6_src;
1054be2ac88cSJonathan Lemon 			inc.inc6_laddr = ip6->ip6_dst;
1055302ce8d6SAndre Oppermann 		} else
1056302ce8d6SAndre Oppermann #endif
1057302ce8d6SAndre Oppermann 		{
1058be2ac88cSJonathan Lemon 			inc.inc_faddr = ip->ip_src;
1059be2ac88cSJonathan Lemon 			inc.inc_laddr = ip->ip_dst;
1060be2ac88cSJonathan Lemon 		}
1061be2ac88cSJonathan Lemon 		inc.inc_fport = th->th_sport;
1062be2ac88cSJonathan Lemon 		inc.inc_lport = th->th_dport;
10637973fba3SJulian Elischer 		inc.inc_fibnum = so->so_fibnum;
1064be2ac88cSJonathan Lemon 
1065fb59c426SYoshinobu Inoue 		/*
10668d573cc1SAndre Oppermann 		 * Check for an existing connection attempt in syncache if
10678d573cc1SAndre Oppermann 		 * the flag is only ACK.  A successful lookup creates a new
10688d573cc1SAndre Oppermann 		 * socket appended to the listen queue in SYN_RECEIVED state.
1069fb59c426SYoshinobu Inoue 		 */
1070be2ac88cSJonathan Lemon 		if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
1071bf6d304aSAndre Oppermann 			/*
1072bf6d304aSAndre Oppermann 			 * Parse the TCP options here because
1073bf6d304aSAndre Oppermann 			 * syncookies need access to the reflected
1074bf6d304aSAndre Oppermann 			 * timestamp.
1075bf6d304aSAndre Oppermann 			 */
1076bf6d304aSAndre Oppermann 			tcp_dooptions(&to, optp, optlen, 0);
10779fa198beSAndre Oppermann 			/*
1078e7231d07SGleb Smirnoff 			 * NB: syncache_expand() doesn't unlock inp.
10799fa198beSAndre Oppermann 			 */
10809e644c23SMichael Tuexen 			rstreason = syncache_expand(&inc, &to, th, &so, m, port);
1081fcf59617SAndrey V. Elsukov 			if (rstreason < 0) {
1082fcf59617SAndrey V. Elsukov 				/*
1083fcf59617SAndrey V. Elsukov 				 * A failing TCP MD5 signature comparison
1084fcf59617SAndrey V. Elsukov 				 * must result in the segment being dropped
1085fcf59617SAndrey V. Elsukov 				 * and must not produce any response back
1086fcf59617SAndrey V. Elsukov 				 * to the sender.
1087fcf59617SAndrey V. Elsukov 				 */
1088fcf59617SAndrey V. Elsukov 				goto dropunlock;
1089fcf59617SAndrey V. Elsukov 			} else if (rstreason == 0) {
10904195b4afSPaul Traina 				/*
1091d88eb465SGleb Smirnoff 				 * No syncache entry, or ACK was not for our
1092d88eb465SGleb Smirnoff 				 * SYN/ACK.  Do our protection against double
1093d88eb465SGleb Smirnoff 				 * ACK.  If peer sent us 2 ACKs, then for the
1094d88eb465SGleb Smirnoff 				 * first one syncache_expand() successfully
1095d88eb465SGleb Smirnoff 				 * converted syncache entry into a socket,
1096d88eb465SGleb Smirnoff 				 * while we were waiting on the inpcb lock.  We
1097d88eb465SGleb Smirnoff 				 * don't want to sent RST for the second ACK,
1098d88eb465SGleb Smirnoff 				 * so we perform second lookup without wildcard
1099d88eb465SGleb Smirnoff 				 * match, hoping to find the new socket.  If
1100d88eb465SGleb Smirnoff 				 * the ACK is stray indeed, rstreason would
1101d88eb465SGleb Smirnoff 				 * hint the above code that the lookup was a
1102d88eb465SGleb Smirnoff 				 * second attempt.
1103d88eb465SGleb Smirnoff 				 *
11048d573cc1SAndre Oppermann 				 * NB: syncache did its own logging
11058d573cc1SAndre Oppermann 				 * of the failure cause.
11064195b4afSPaul Traina 				 */
1107d88eb465SGleb Smirnoff 				INP_WUNLOCK(inp);
1108be2ac88cSJonathan Lemon 				rstreason = BANDLIM_RST_OPENPORT;
1109d88eb465SGleb Smirnoff 				lookupflag &= ~INPLOOKUP_WILDCARD;
1110d88eb465SGleb Smirnoff 				goto findpcb;
1111be2ac88cSJonathan Lemon 			}
111209c305ebSPatrick Kelsey tfo_socket_result:
1113f76fcf6dSJeffrey Hsu 			if (so == NULL) {
1114be2ac88cSJonathan Lemon 				/*
1115e207f800SAndre Oppermann 				 * We completed the 3-way handshake
1116e207f800SAndre Oppermann 				 * but could not allocate a socket
1117e207f800SAndre Oppermann 				 * either due to memory shortage,
1118e207f800SAndre Oppermann 				 * listen queue length limits or
1119a160e630SAndre Oppermann 				 * global socket limits.  Send RST
1120a160e630SAndre Oppermann 				 * or wait and have the remote end
1121a160e630SAndre Oppermann 				 * retransmit the ACK for another
1122a160e630SAndre Oppermann 				 * try.
1123be2ac88cSJonathan Lemon 				 */
1124a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1125a160e630SAndre Oppermann 					log(LOG_DEBUG, "%s; %s: Listen socket: "
11268d573cc1SAndre Oppermann 					    "Socket allocation failed due to "
11278d573cc1SAndre Oppermann 					    "limits or memory shortage, %s\n",
1128ac957cd2SJulian Elischer 					    s, __func__,
1129ac957cd2SJulian Elischer 					    V_tcp_sc_rst_sock_fail ?
1130ac957cd2SJulian Elischer 					    "sending RST" : "try again");
1131603724d3SBjoern A. Zeeb 				if (V_tcp_sc_rst_sock_fail) {
1132e207f800SAndre Oppermann 					rstreason = BANDLIM_UNLIMITED;
1133e207f800SAndre Oppermann 					goto dropwithreset;
1134a160e630SAndre Oppermann 				} else
1135a160e630SAndre Oppermann 					goto dropunlock;
1136f76fcf6dSJeffrey Hsu 			}
1137be2ac88cSJonathan Lemon 			/*
1138be2ac88cSJonathan Lemon 			 * Socket is created in state SYN_RECEIVED.
11398d573cc1SAndre Oppermann 			 * Unlock the listen socket, lock the newly
11408d573cc1SAndre Oppermann 			 * created socket and update the tp variable.
114108d9c920SGleb Smirnoff 			 * If we came here via jump to tfo_socket_result,
114208d9c920SGleb Smirnoff 			 * then listening socket is read-locked.
1143be2ac88cSJonathan Lemon 			 */
114408d9c920SGleb Smirnoff 			INP_UNLOCK(inp);	/* listen socket */
1145be2ac88cSJonathan Lemon 			inp = sotoinpcb(so);
1146ff9b006dSJulien Charbon 			/*
1147ff9b006dSJulien Charbon 			 * New connection inpcb is already locked by
1148ff9b006dSJulien Charbon 			 * syncache_expand().
1149ff9b006dSJulien Charbon 			 */
1150ff9b006dSJulien Charbon 			INP_WLOCK_ASSERT(inp);
1151be2ac88cSJonathan Lemon 			tp = intotcpcb(inp);
11528d573cc1SAndre Oppermann 			KASSERT(tp->t_state == TCPS_SYN_RECEIVED,
11538d573cc1SAndre Oppermann 			    ("%s: ", __func__));
1154be2ac88cSJonathan Lemon 			/*
1155302ce8d6SAndre Oppermann 			 * Process the segment and the data it
1156302ce8d6SAndre Oppermann 			 * contains.  tcp_do_segment() consumes
1157302ce8d6SAndre Oppermann 			 * the mbuf chain and unlocks the inpcb.
1158302ce8d6SAndre Oppermann 			 */
11596138da62SMichael Tuexen 			TCP_PROBE5(receive, NULL, tp, m, tp, th);
116055bceb1eSRandall Stewart 			tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen,
11616573d758SMatt Macy 			    iptos);
11628f5a8818SKevin Lo 			return (IPPROTO_DONE);
1163be2ac88cSJonathan Lemon 		}
1164a160e630SAndre Oppermann 		/*
11658d573cc1SAndre Oppermann 		 * Segment flag validation for new connection attempts:
11668d573cc1SAndre Oppermann 		 *
1167a160e630SAndre Oppermann 		 * Our (SYN|ACK) response was rejected.
1168a160e630SAndre Oppermann 		 * Check with syncache and remove entry to prevent
1169a160e630SAndre Oppermann 		 * retransmits.
117019bc77c5SAndre Oppermann 		 *
117119bc77c5SAndre Oppermann 		 * NB: syncache_chkrst does its own logging of failure
117219bc77c5SAndre Oppermann 		 * causes.
1173a160e630SAndre Oppermann 		 */
1174a160e630SAndre Oppermann 		if (thflags & TH_RST) {
11759e644c23SMichael Tuexen 			syncache_chkrst(&inc, th, m, port);
1176a160e630SAndre Oppermann 			goto dropunlock;
1177a160e630SAndre Oppermann 		}
1178a160e630SAndre Oppermann 		/*
1179a160e630SAndre Oppermann 		 * We can't do anything without SYN.
1180a160e630SAndre Oppermann 		 */
1181a160e630SAndre Oppermann 		if ((thflags & TH_SYN) == 0) {
1182a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1183a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1184773673c1SAndre Oppermann 				    "SYN is missing, segment ignored\n",
11858d573cc1SAndre Oppermann 				    s, __func__);
118678b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1187a160e630SAndre Oppermann 			goto dropunlock;
1188a160e630SAndre Oppermann 		}
1189a160e630SAndre Oppermann 		/*
1190a160e630SAndre Oppermann 		 * (SYN|ACK) is bogus on a listen socket.
1191a160e630SAndre Oppermann 		 */
1192fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1193a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1194a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
11958d573cc1SAndre Oppermann 				    "SYN|ACK invalid, segment rejected\n",
11968d573cc1SAndre Oppermann 				    s, __func__);
11979e644c23SMichael Tuexen 			syncache_badack(&inc, port);	/* XXX: Not needed! */
119878b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1199a57815efSBosko Milekic 			rstreason = BANDLIM_RST_OPENPORT;
1200a57815efSBosko Milekic 			goto dropwithreset;
12014195b4afSPaul Traina 		}
1202a160e630SAndre Oppermann 		/*
1203a160e630SAndre Oppermann 		 * If the drop_synfin option is enabled, drop all
1204a160e630SAndre Oppermann 		 * segments with both the SYN and FIN bits set.
1205a160e630SAndre Oppermann 		 * This prevents e.g. nmap from identifying the
1206a160e630SAndre Oppermann 		 * TCP/IP stack.
1207a160e630SAndre Oppermann 		 * XXX: Poor reasoning.  nmap has other methods
1208a160e630SAndre Oppermann 		 * and is constantly refining its stack detection
1209a160e630SAndre Oppermann 		 * strategies.
12108d573cc1SAndre Oppermann 		 * XXX: This is a violation of the TCP specification
1211a160e630SAndre Oppermann 		 * and was used by RFC1644.
1212a160e630SAndre Oppermann 		 */
1213603724d3SBjoern A. Zeeb 		if ((thflags & TH_FIN) && V_drop_synfin) {
1214a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1215a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1216773673c1SAndre Oppermann 				    "SYN|FIN segment ignored (based on "
12178d573cc1SAndre Oppermann 				    "sysctl setting)\n", s, __func__);
121878b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1219302ce8d6SAndre Oppermann 			goto dropunlock;
1220ebb0cbeaSPaul Traina 		}
1221be2ac88cSJonathan Lemon 		/*
1222be2ac88cSJonathan Lemon 		 * Segment's flags are (SYN) or (SYN|FIN).
1223a160e630SAndre Oppermann 		 *
1224a160e630SAndre Oppermann 		 * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored
1225a160e630SAndre Oppermann 		 * as they do not affect the state of the TCP FSM.
1226a160e630SAndre Oppermann 		 * The data pointed to by TH_URG and th_urp is ignored.
1227be2ac88cSJonathan Lemon 		 */
1228a160e630SAndre Oppermann 		KASSERT((thflags & (TH_RST|TH_ACK)) == 0,
1229a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_RST or TH_ACK set", __func__));
1230a160e630SAndre Oppermann 		KASSERT(thflags & (TH_SYN),
1231a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_SYN not set", __func__));
123208d9c920SGleb Smirnoff 		INP_RLOCK_ASSERT(inp);
123333841545SHajimu UMEMOTO #ifdef INET6
123433841545SHajimu UMEMOTO 		/*
123533841545SHajimu UMEMOTO 		 * If deprecated address is forbidden,
123633841545SHajimu UMEMOTO 		 * we do not accept SYN to deprecated interface
123733841545SHajimu UMEMOTO 		 * address to prevent any new inbound connection from
123833841545SHajimu UMEMOTO 		 * getting established.
123933841545SHajimu UMEMOTO 		 * When we do not accept SYN, we send a TCP RST,
124033841545SHajimu UMEMOTO 		 * with deprecated source address (instead of dropping
124133841545SHajimu UMEMOTO 		 * it).  We compromise it as it is much better for peer
124233841545SHajimu UMEMOTO 		 * to send a RST, and RST will be the final packet
124333841545SHajimu UMEMOTO 		 * for the exchange.
124433841545SHajimu UMEMOTO 		 *
124533841545SHajimu UMEMOTO 		 * If we do not forbid deprecated addresses, we accept
124633841545SHajimu UMEMOTO 		 * the SYN packet.  RFC2462 does not suggest dropping
124733841545SHajimu UMEMOTO 		 * SYN in this case.
124833841545SHajimu UMEMOTO 		 * If we decipher RFC2462 5.5.4, it says like this:
124933841545SHajimu UMEMOTO 		 * 1. use of deprecated addr with existing
125033841545SHajimu UMEMOTO 		 *    communication is okay - "SHOULD continue to be
125133841545SHajimu UMEMOTO 		 *    used"
125233841545SHajimu UMEMOTO 		 * 2. use of it with new communication:
125333841545SHajimu UMEMOTO 		 *   (2a) "SHOULD NOT be used if alternate address
125433841545SHajimu UMEMOTO 		 *        with sufficient scope is available"
125533841545SHajimu UMEMOTO 		 *   (2b) nothing mentioned otherwise.
125633841545SHajimu UMEMOTO 		 * Here we fall into (2b) case as we have no choice in
125733841545SHajimu UMEMOTO 		 * our source address selection - we must obey the peer.
125833841545SHajimu UMEMOTO 		 *
125933841545SHajimu UMEMOTO 		 * The wording in RFC2462 is confusing, and there are
126033841545SHajimu UMEMOTO 		 * multiple description text for deprecated address
126133841545SHajimu UMEMOTO 		 * handling - worse, they are not exactly the same.
126233841545SHajimu UMEMOTO 		 * I believe 5.5.4 is the best one, so we follow 5.5.4.
126333841545SHajimu UMEMOTO 		 */
1264603724d3SBjoern A. Zeeb 		if (isipv6 && !V_ip6_use_deprecated) {
126533841545SHajimu UMEMOTO 			struct in6_ifaddr *ia6;
126633841545SHajimu UMEMOTO 
12678268d82cSAlexander V. Chernikov 			ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false);
12688c0fec80SRobert Watson 			if (ia6 != NULL &&
126933841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
1270a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1271a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12728d573cc1SAndre Oppermann 					"Connection attempt to deprecated "
12738d573cc1SAndre Oppermann 					"IPv6 address rejected\n",
1274a160e630SAndre Oppermann 					s, __func__);
127533841545SHajimu UMEMOTO 				rstreason = BANDLIM_RST_OPENPORT;
127633841545SHajimu UMEMOTO 				goto dropwithreset;
127733841545SHajimu UMEMOTO 			}
127833841545SHajimu UMEMOTO 		}
1279b287c6c7SBjoern A. Zeeb #endif /* INET6 */
128065f28919SJesper Skriver 		/*
1281db33b3e6SAndre Oppermann 		 * Basic sanity checks on incoming SYN requests:
12828d573cc1SAndre Oppermann 		 *   Don't respond if the destination is a link layer
1283db33b3e6SAndre Oppermann 		 *	broadcast according to RFC1122 4.2.3.10, p. 104.
12848d573cc1SAndre Oppermann 		 *   If it is from this socket it must be forged.
12858d573cc1SAndre Oppermann 		 *   Don't respond if the source or destination is a
12868d573cc1SAndre Oppermann 		 *	global or subnet broad- or multicast address.
128793ec91baSCrist J. Clark 		 *   Note that it is quite possible to receive unicast
128893ec91baSCrist J. Clark 		 *	link-layer packets with a broadcast IP address. Use
128993ec91baSCrist J. Clark 		 *	in_broadcast() to find them.
1290be2ac88cSJonathan Lemon 		 */
12918d573cc1SAndre Oppermann 		if (m->m_flags & (M_BCAST|M_MCAST)) {
12928d573cc1SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
12938d573cc1SAndre Oppermann 			    log(LOG_DEBUG, "%s; %s: Listen socket: "
12948d573cc1SAndre Oppermann 				"Connection attempt from broad- or multicast "
1295773673c1SAndre Oppermann 				"link layer address ignored\n", s, __func__);
1296302ce8d6SAndre Oppermann 			goto dropunlock;
12978d573cc1SAndre Oppermann 		}
1298db33b3e6SAndre Oppermann #ifdef INET6
1299b287c6c7SBjoern A. Zeeb 		if (isipv6) {
1300db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1301a160e630SAndre Oppermann 			    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) {
1302a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1303a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13048d573cc1SAndre Oppermann 					"Connection attempt to/from self "
1305773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1306302ce8d6SAndre Oppermann 				goto dropunlock;
1307a160e630SAndre Oppermann 			}
1308be2ac88cSJonathan Lemon 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1309a160e630SAndre Oppermann 			    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
1310a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1311a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13128d573cc1SAndre Oppermann 					"Connection attempt from/to multicast "
1313773673c1SAndre Oppermann 					"address ignored\n", s, __func__);
1314302ce8d6SAndre Oppermann 				goto dropunlock;
1315a160e630SAndre Oppermann 			}
1316b287c6c7SBjoern A. Zeeb 		}
1317db33b3e6SAndre Oppermann #endif
1318b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
1319b287c6c7SBjoern A. Zeeb 		else
1320b287c6c7SBjoern A. Zeeb #endif
1321b287c6c7SBjoern A. Zeeb #ifdef INET
1322b287c6c7SBjoern A. Zeeb 		{
1323db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1324a160e630SAndre Oppermann 			    ip->ip_dst.s_addr == ip->ip_src.s_addr) {
1325a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1326a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13278d573cc1SAndre Oppermann 					"Connection attempt from/to self "
1328773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1329302ce8d6SAndre Oppermann 				goto dropunlock;
1330a160e630SAndre Oppermann 			}
1331be2ac88cSJonathan Lemon 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
1332be2ac88cSJonathan Lemon 			    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
13332ca2159fSCrist J. Clark 			    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
1334a160e630SAndre Oppermann 			    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
1335a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1336a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13378d573cc1SAndre Oppermann 					"Connection attempt from/to broad- "
1338773673c1SAndre Oppermann 					"or multicast address ignored\n",
1339a160e630SAndre Oppermann 					s, __func__);
1340302ce8d6SAndre Oppermann 				goto dropunlock;
1341c068736aSJeffrey Hsu 			}
1342a160e630SAndre Oppermann 		}
1343b287c6c7SBjoern A. Zeeb #endif
1344be2ac88cSJonathan Lemon 		/*
1345db33b3e6SAndre Oppermann 		 * SYN appears to be valid.  Create compressed TCP state
1346db33b3e6SAndre Oppermann 		 * for syncache.
1347be2ac88cSJonathan Lemon 		 */
13483c653157SHartmut Brandt #ifdef TCPDEBUG
13493c653157SHartmut Brandt 		if (so->so_options & SO_DEBUG)
13503c653157SHartmut Brandt 			tcp_trace(TA_INPUT, ostate, tp,
13513c653157SHartmut Brandt 			    (void *)tcp_saveipgen, &tcp_savetcp, 0);
13523c653157SHartmut Brandt #endif
13532b9c9984SGeorge V. Neville-Neil 		TCP_PROBE3(debug__input, tp, th, m);
1354f72167f4SAndre Oppermann 		tcp_dooptions(&to, optp, optlen, TO_SYN);
13558d5719aaSGleb Smirnoff 		if ((so = syncache_add(&inc, &to, th, inp, so, m, NULL, NULL,
13569e644c23SMichael Tuexen 		    iptos, port)) != NULL)
135709c305ebSPatrick Kelsey 			goto tfo_socket_result;
135818a75309SPatrick Kelsey 
1359be2ac88cSJonathan Lemon 		/*
13604d6e7130SAndre Oppermann 		 * Entry added to syncache and mbuf consumed.
1361a7c7f2a7SJohn Baldwin 		 * Only the listen socket is unlocked by syncache_add().
1362be2ac88cSJonathan Lemon 		 */
13638f5a8818SKevin Lo 		return (IPPROTO_DONE);
1364982c1675SAndre Oppermann 	} else if (tp->t_state == TCPS_LISTEN) {
1365982c1675SAndre Oppermann 		/*
1366982c1675SAndre Oppermann 		 * When a listen socket is torn down the SO_ACCEPTCONN
1367982c1675SAndre Oppermann 		 * flag is removed first while connections are drained
1368982c1675SAndre Oppermann 		 * from the accept queue in a unlock/lock cycle of the
1369982c1675SAndre Oppermann 		 * ACCEPT_LOCK, opening a race condition allowing a SYN
1370982c1675SAndre Oppermann 		 * attempt go through unhandled.
1371982c1675SAndre Oppermann 		 */
1372982c1675SAndre Oppermann 		goto dropunlock;
1373f76fcf6dSJeffrey Hsu 	}
1374fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1375fcf59617SAndrey V. Elsukov 	if (tp->t_flags & TF_SIGNATURE) {
1376fcf59617SAndrey V. Elsukov 		tcp_dooptions(&to, optp, optlen, thflags);
1377fcf59617SAndrey V. Elsukov 		if ((to.to_flags & TOF_SIGNATURE) == 0) {
1378fcf59617SAndrey V. Elsukov 			TCPSTAT_INC(tcps_sig_err_nosigopt);
13792903309aSAttilio Rao 			goto dropunlock;
13802903309aSAttilio Rao 		}
1381fcf59617SAndrey V. Elsukov 		if (!TCPMD5_ENABLED() ||
1382fcf59617SAndrey V. Elsukov 		    TCPMD5_INPUT(m, th, to.to_signature) != 0)
1383fcf59617SAndrey V. Elsukov 			goto dropunlock;
13842903309aSAttilio Rao 	}
13852903309aSAttilio Rao #endif
13862b9c9984SGeorge V. Neville-Neil 	TCP_PROBE5(receive, NULL, tp, m, tp, th);
138757f60867SMark Johnston 
1388302ce8d6SAndre Oppermann 	/*
13898d573cc1SAndre Oppermann 	 * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later
13908d573cc1SAndre Oppermann 	 * state.  tcp_do_segment() always consumes the mbuf chain, unlocks
13918d573cc1SAndre Oppermann 	 * the inpcb, and unlocks pcbinfo.
13921db08fbeSGleb Smirnoff 	 *
13931db08fbeSGleb Smirnoff 	 * XXXGL: in case of a pure SYN arriving on existing connection
13941db08fbeSGleb Smirnoff 	 * TCP stacks won't need to modify the PCB, they would either drop
13951db08fbeSGleb Smirnoff 	 * the segment silently, or send a challenge ACK.  However, we try
13961db08fbeSGleb Smirnoff 	 * to upgrade the lock, because calling convention for stacks is
13971db08fbeSGleb Smirnoff 	 * write-lock on PCB.  If upgrade fails, drop the SYN.
1398302ce8d6SAndre Oppermann 	 */
1399d88eb465SGleb Smirnoff 	if ((lookupflag & INPLOOKUP_RLOCKPCB) && INP_TRY_UPGRADE(inp) == 0)
14001db08fbeSGleb Smirnoff 		goto dropunlock;
14011db08fbeSGleb Smirnoff 
14026573d758SMatt Macy 	tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos);
14038f5a8818SKevin Lo 	return (IPPROTO_DONE);
1404be2ac88cSJonathan Lemon 
1405302ce8d6SAndre Oppermann dropwithreset:
14062b9c9984SGeorge V. Neville-Neil 	TCP_PROBE5(receive, NULL, tp, m, tp, th);
140757f60867SMark Johnston 
1408014ea782SRobert Watson 	if (inp != NULL) {
1409302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
141008d9c920SGleb Smirnoff 		INP_UNLOCK(inp);
1411dd8ac7f9SRobert Watson 	} else
1412014ea782SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
1413302ce8d6SAndre Oppermann 	m = NULL;	/* mbuf chain got consumed. */
1414014ea782SRobert Watson 	goto drop;
1415014ea782SRobert Watson 
1416302ce8d6SAndre Oppermann dropunlock:
141757f60867SMark Johnston 	if (m != NULL)
14182b9c9984SGeorge V. Neville-Neil 		TCP_PROBE5(receive, NULL, tp, m, tp, th);
141957f60867SMark Johnston 
14209fa198beSAndre Oppermann 	if (inp != NULL)
142108d9c920SGleb Smirnoff 		INP_UNLOCK(inp);
1422014ea782SRobert Watson 
1423302ce8d6SAndre Oppermann drop:
1424a160e630SAndre Oppermann 	if (s != NULL)
1425a160e630SAndre Oppermann 		free(s, M_TCPLOG);
1426302ce8d6SAndre Oppermann 	if (m != NULL)
1427302ce8d6SAndre Oppermann 		m_freem(m);
14288f5a8818SKevin Lo 	return (IPPROTO_DONE);
1429302ce8d6SAndre Oppermann }
1430302ce8d6SAndre Oppermann 
1431e44c1887SSteven Hartland /*
1432e44c1887SSteven Hartland  * Automatic sizing of receive socket buffer.  Often the send
1433e44c1887SSteven Hartland  * buffer size is not optimally adjusted to the actual network
1434e44c1887SSteven Hartland  * conditions at hand (delay bandwidth product).  Setting the
1435e44c1887SSteven Hartland  * buffer size too small limits throughput on links with high
1436e44c1887SSteven Hartland  * bandwidth and high delay (eg. trans-continental/oceanic links).
1437e44c1887SSteven Hartland  *
1438e44c1887SSteven Hartland  * On the receive side the socket buffer memory is only rarely
1439e44c1887SSteven Hartland  * used to any significant extent.  This allows us to be much
1440e44c1887SSteven Hartland  * more aggressive in scaling the receive socket buffer.  For
1441e44c1887SSteven Hartland  * the case that the buffer space is actually used to a large
1442e44c1887SSteven Hartland  * extent and we run out of kernel memory we can simply drop
1443e44c1887SSteven Hartland  * the new segments; TCP on the sender will just retransmit it
1444e44c1887SSteven Hartland  * later.  Setting the buffer size too big may only consume too
1445e44c1887SSteven Hartland  * much kernel memory if the application doesn't read() from
1446e44c1887SSteven Hartland  * the socket or packet loss or reordering makes use of the
1447e44c1887SSteven Hartland  * reassembly queue.
1448e44c1887SSteven Hartland  *
1449e44c1887SSteven Hartland  * The criteria to step up the receive buffer one notch are:
1450e44c1887SSteven Hartland  *  1. Application has not set receive buffer size with
1451e44c1887SSteven Hartland  *     SO_RCVBUF. Setting SO_RCVBUF clears SB_AUTOSIZE.
1452560c0586SMichael Tuexen  *  2. the number of bytes received during 1/2 of an sRTT
1453560c0586SMichael Tuexen  *     is at least 3/8 of the current socket buffer size.
1454560c0586SMichael Tuexen  *  3. receive buffer size has not hit maximal automatic size;
1455e44c1887SSteven Hartland  *
1456560c0586SMichael Tuexen  * If all of the criteria are met we increaset the socket buffer
1457560c0586SMichael Tuexen  * by a 1/2 (bounded by the max). This allows us to keep ahead
1458560c0586SMichael Tuexen  * of slow-start but also makes it so our peer never gets limited
1459560c0586SMichael Tuexen  * by our rwnd which we then open up causing a burst.
1460560c0586SMichael Tuexen  *
1461560c0586SMichael Tuexen  * This algorithm does two steps per RTT at most and only if
1462e44c1887SSteven Hartland  * we receive a bulk stream w/o packet losses or reorderings.
1463e44c1887SSteven Hartland  * Shrinking the buffer during idle times is not necessary as
1464e44c1887SSteven Hartland  * it doesn't consume any memory when idle.
1465e44c1887SSteven Hartland  *
1466e44c1887SSteven Hartland  * TODO: Only step up if the application is actually serving
1467e44c1887SSteven Hartland  * the buffer to better manage the socket buffer resources.
1468e44c1887SSteven Hartland  */
1469e44c1887SSteven Hartland int
1470e44c1887SSteven Hartland tcp_autorcvbuf(struct mbuf *m, struct tcphdr *th, struct socket *so,
1471e44c1887SSteven Hartland     struct tcpcb *tp, int tlen)
1472e44c1887SSteven Hartland {
1473e44c1887SSteven Hartland 	int newsize = 0;
1474e44c1887SSteven Hartland 
1475e44c1887SSteven Hartland 	if (V_tcp_do_autorcvbuf && (so->so_rcv.sb_flags & SB_AUTOSIZE) &&
1476e44c1887SSteven Hartland 	    tp->t_srtt != 0 && tp->rfbuf_ts != 0 &&
1477e44c1887SSteven Hartland 	    TCP_TS_TO_TICKS(tcp_ts_getticks() - tp->rfbuf_ts) >
1478560c0586SMichael Tuexen 	    ((tp->t_srtt >> TCP_RTT_SHIFT)/2)) {
1479560c0586SMichael Tuexen 		if (tp->rfbuf_cnt > ((so->so_rcv.sb_hiwat / 2)/ 4 * 3) &&
1480e44c1887SSteven Hartland 		    so->so_rcv.sb_hiwat < V_tcp_autorcvbuf_max) {
1481560c0586SMichael Tuexen 			newsize = min((so->so_rcv.sb_hiwat + (so->so_rcv.sb_hiwat/2)), V_tcp_autorcvbuf_max);
1482e44c1887SSteven Hartland 		}
1483e44c1887SSteven Hartland 		TCP_PROBE6(receive__autoresize, NULL, tp, m, tp, th, newsize);
1484e44c1887SSteven Hartland 
1485e44c1887SSteven Hartland 		/* Start over with next RTT. */
1486e44c1887SSteven Hartland 		tp->rfbuf_ts = 0;
1487e44c1887SSteven Hartland 		tp->rfbuf_cnt = 0;
1488e44c1887SSteven Hartland 	} else {
1489e44c1887SSteven Hartland 		tp->rfbuf_cnt += tlen;	/* add up */
1490e44c1887SSteven Hartland 	}
1491e44c1887SSteven Hartland 	return (newsize);
1492e44c1887SSteven Hartland }
1493e44c1887SSteven Hartland 
14949e644c23SMichael Tuexen int
14959e644c23SMichael Tuexen tcp_input(struct mbuf **mp, int *offp, int proto)
14969e644c23SMichael Tuexen {
14979e644c23SMichael Tuexen 	return(tcp_input_with_port(mp, offp, proto, 0));
14989e644c23SMichael Tuexen }
14999e644c23SMichael Tuexen 
1500c348e880SGleb Smirnoff static void
1501c348e880SGleb Smirnoff tcp_handle_wakeup(struct tcpcb *tp)
15024d0770f1SRichard Scheffenegger {
1503c348e880SGleb Smirnoff 
1504c348e880SGleb Smirnoff 	INP_WLOCK_ASSERT(tp->t_inpcb);
1505c348e880SGleb Smirnoff 
15064d0770f1SRichard Scheffenegger 	if (tp->t_flags & TF_WAKESOR) {
1507c348e880SGleb Smirnoff 		struct socket *so = tp->t_inpcb->inp_socket;
1508c348e880SGleb Smirnoff 
15094d0770f1SRichard Scheffenegger 		tp->t_flags &= ~TF_WAKESOR;
1510032bf749SRichard Scheffenegger 		SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1511032bf749SRichard Scheffenegger 		sorwakeup_locked(so);
15124d0770f1SRichard Scheffenegger 	}
15134d0770f1SRichard Scheffenegger }
15144d0770f1SRichard Scheffenegger 
15154d0770f1SRichard Scheffenegger void
1516302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so,
15176573d758SMatt Macy     struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos)
1518302ce8d6SAndre Oppermann {
1519f7220c48SRichard Scheffenegger 	uint16_t thflags;
1520f7220c48SRichard Scheffenegger 	int acked, ourfinisacked, needoutput = 0, sack_changed;
1521b2ade6b1SRichard Scheffenegger 	int rstreason, todrop, win, incforsyn = 0;
15223ac12506SJonathan T. Looney 	uint32_t tiwin;
15234b7b743cSLawrence Stewart 	uint16_t nsegs;
152407dacf03SAndre Oppermann 	char *s;
152507dacf03SAndre Oppermann 	struct in_conninfo *inc;
1526c11a15bfSGleb Smirnoff 	struct mbuf *mfree;
1527302ce8d6SAndre Oppermann 	struct tcpopt to;
1528281a0fd4SPatrick Kelsey 	int tfo_syn;
15293c40e1d5SRichard Scheffenegger 	u_int maxseg;
1530302ce8d6SAndre Oppermann 
153114739780SMaxim Konovalov #ifdef TCPDEBUG
153214739780SMaxim Konovalov 	/*
153314739780SMaxim Konovalov 	 * The size of tcp_saveipgen must be the size of the max ip header,
153414739780SMaxim Konovalov 	 * now IPv6.
153514739780SMaxim Konovalov 	 */
153614739780SMaxim Konovalov 	u_char tcp_saveipgen[IP6_HDR_LEN];
153714739780SMaxim Konovalov 	struct tcphdr tcp_savetcp;
153814739780SMaxim Konovalov 	short ostate = 0;
153914739780SMaxim Konovalov #endif
15401ebf4607SRichard Scheffenegger 	thflags = tcp_get_flags(th);
154107dacf03SAndre Oppermann 	inc = &tp->t_inpcb->inp_inc;
1542d64a46eaSLawrence Stewart 	tp->sackhint.last_sack_ack = 0;
1543021eaf79SHiren Panchasara 	sack_changed = 0;
15444b7b743cSLawrence Stewart 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
1545d40c0d47SGleb Smirnoff 
1546d40c0d47SGleb Smirnoff 	NET_EPOCH_ASSERT();
15478501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1548679d9708SAndre Oppermann 	KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
1549679d9708SAndre Oppermann 	    __func__));
1550679d9708SAndre Oppermann 	KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
1551679d9708SAndre Oppermann 	    __func__));
1552df8bae1dSRodney W. Grimes 
155386a996e6SHiren Panchasara #ifdef TCPPCAP
155486a996e6SHiren Panchasara 	/* Save segment, if requested. */
155586a996e6SHiren Panchasara 	tcp_pcap_add(th, m, &(tp->t_inpkts));
155686a996e6SHiren Panchasara #endif
15572529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0,
15582529f56eSJonathan T. Looney 	    tlen, NULL, true);
155986a996e6SHiren Panchasara 
1560013f4df6SMichael Tuexen 	if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
1561013f4df6SMichael Tuexen 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
1562013f4df6SMichael Tuexen 			log(LOG_DEBUG, "%s; %s: "
1563013f4df6SMichael Tuexen 			    "SYN|FIN segment ignored (based on "
1564013f4df6SMichael Tuexen 			    "sysctl setting)\n", s, __func__);
1565013f4df6SMichael Tuexen 			free(s, M_TCPLOG);
1566013f4df6SMichael Tuexen 		}
1567013f4df6SMichael Tuexen 		goto drop;
1568013f4df6SMichael Tuexen 	}
1569013f4df6SMichael Tuexen 
1570df8bae1dSRodney W. Grimes 	/*
1571ebfd7534SMichael Tuexen 	 * If a segment with the ACK-bit set arrives in the SYN-SENT state
1572ebfd7534SMichael Tuexen 	 * check SEQ.ACK first.
1573ebfd7534SMichael Tuexen 	 */
1574ebfd7534SMichael Tuexen 	if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) &&
1575ebfd7534SMichael Tuexen 	    (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) {
1576ebfd7534SMichael Tuexen 		rstreason = BANDLIM_UNLIMITED;
1577d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
1578ebfd7534SMichael Tuexen 		goto dropwithreset;
1579ebfd7534SMichael Tuexen 	}
1580ebfd7534SMichael Tuexen 
1581ebfd7534SMichael Tuexen 	/*
1582df8bae1dSRodney W. Grimes 	 * Segment received on connection.
1583df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
1584e8949f74SAndre Oppermann 	 * XXX: This should be done after segment
1585e8949f74SAndre Oppermann 	 * validation to ignore broken/spoofed segs.
1586df8bae1dSRodney W. Grimes 	 */
1587cd84e78fSRandall Stewart 	if  (tp->t_idle_reduce &&
1588cd84e78fSRandall Stewart 	     (tp->snd_max == tp->snd_una) &&
1589cd84e78fSRandall Stewart 	     ((ticks - tp->t_rcvtime) >= tp->t_rxtcur))
1590cd84e78fSRandall Stewart 		cc_after_idle(tp);
15919b8b58e0SJonathan Lemon 	tp->t_rcvtime = ticks;
1592df8bae1dSRodney W. Grimes 
1593d1b07f36SRandall Stewart 	if (thflags & TH_FIN)
1594d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_FIN);
1595df8bae1dSRodney W. Grimes 	/*
1596c52102ddSHiren Panchasara 	 * Scale up the window into a 32-bit value.
1597e8949f74SAndre Oppermann 	 * For the SYN_SENT state the scale is zero.
1598464fcfbcSAndre Oppermann 	 */
1599464fcfbcSAndre Oppermann 	tiwin = th->th_win << tp->snd_scale;
1600adc56f5aSEdward Tomasz Napierala #ifdef STATS
1601adc56f5aSEdward Tomasz Napierala 	stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin);
1602adc56f5aSEdward Tomasz Napierala #endif
1603464fcfbcSAndre Oppermann 
1604464fcfbcSAndre Oppermann 	/*
1605f2512ba1SRui Paulo 	 * TCP ECN processing.
1606f2512ba1SRui Paulo 	 */
1607b1258b76SRichard Scheffenegger 	if (tcp_ecn_input_segment(tp, thflags, tlen,
1608b1258b76SRichard Scheffenegger 	    tcp_packets_this_ack(tp, th->th_ack),
1609b1258b76SRichard Scheffenegger 	    iptos))
1610dbc42409SLawrence Stewart 		cc_cong_signal(tp, th, CC_ECN);
1611f2512ba1SRui Paulo 
1612f2512ba1SRui Paulo 	/*
1613f72167f4SAndre Oppermann 	 * Parse options on any incoming segment.
1614f72167f4SAndre Oppermann 	 */
1615302ce8d6SAndre Oppermann 	tcp_dooptions(&to, (u_char *)(th + 1),
1616302ce8d6SAndre Oppermann 	    (th->th_off << 2) - sizeof(struct tcphdr),
1617302ce8d6SAndre Oppermann 	    (thflags & TH_SYN) ? TO_SYN : 0);
1618f72167f4SAndre Oppermann 
1619fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1620fcf59617SAndrey V. Elsukov 	if ((tp->t_flags & TF_SIGNATURE) != 0 &&
1621fcf59617SAndrey V. Elsukov 	    (to.to_flags & TOF_SIGNATURE) == 0) {
1622fcf59617SAndrey V. Elsukov 		TCPSTAT_INC(tcps_sig_err_sigopt);
1623fcf59617SAndrey V. Elsukov 		/* XXX: should drop? */
1624fcf59617SAndrey V. Elsukov 	}
1625fcf59617SAndrey V. Elsukov #endif
1626f72167f4SAndre Oppermann 	/*
1627f72167f4SAndre Oppermann 	 * If echoed timestamp is later than the current time,
1628bf6d304aSAndre Oppermann 	 * fall back to non RFC1323 RTT calculation.  Normalize
1629bf6d304aSAndre Oppermann 	 * timestamp if syncookies were used when this connection
1630bf6d304aSAndre Oppermann 	 * was established.
1631f72167f4SAndre Oppermann 	 */
1632bf6d304aSAndre Oppermann 	if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
1633e16fa5caSJohn-Mark Gurney 		to.to_tsecr -= tp->ts_offset;
1634d8951c8aSBjoern A. Zeeb 		if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks()))
1635f72167f4SAndre Oppermann 			to.to_tsecr = 0;
16364531b345SRichard Scheffenegger 		else if (tp->t_rxtshift == 1 &&
16374531b345SRichard Scheffenegger 			 tp->t_flags & TF_PREVVALID &&
16384531b345SRichard Scheffenegger 			 tp->t_badrxtwin != 0 &&
16394531b345SRichard Scheffenegger 			 TSTMP_LT(to.to_tsecr, tp->t_badrxtwin))
164010d20c84SMatt Macy 			cc_cong_signal(tp, th, CC_RTO_ERR);
1641bf6d304aSAndre Oppermann 	}
164207dacf03SAndre Oppermann 	/*
164397d8d152SAndre Oppermann 	 * Process options only when we get SYN/ACK back. The SYN case
164497d8d152SAndre Oppermann 	 * for incoming connections is handled in tcp_syncache.
16455396d0f8SAndre Oppermann 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN>
16465396d0f8SAndre Oppermann 	 * or <SYN,ACK>) segment itself is never scaled.
164797d8d152SAndre Oppermann 	 * XXX this is traditional behavior, may need to be cleaned up.
1648df8bae1dSRodney W. Grimes 	 */
16490a389eabSSimon L. B. Nielsen 	if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
16506e16d877SRichard Scheffenegger 		/* Handle parallel SYN for ECN */
1651f7220c48SRichard Scheffenegger 		tcp_ecn_input_parallel_syn(tp, thflags, iptos);
1652464fcfbcSAndre Oppermann 		if ((to.to_flags & TOF_SCALE) &&
16532593f858SRichard Scheffenegger 		    (tp->t_flags & TF_REQ_SCALE) &&
16542593f858SRichard Scheffenegger 		    !(tp->t_flags & TF_NOOPT)) {
1655be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_SCALE;
165602a1a643SAndre Oppermann 			tp->snd_scale = to.to_wscale;
16578e051165SRichard Scheffenegger 		} else
16588e051165SRichard Scheffenegger 			tp->t_flags &= ~TF_REQ_SCALE;
16595396d0f8SAndre Oppermann 		/*
16605396d0f8SAndre Oppermann 		 * Initial send window.  It will be updated with
16615396d0f8SAndre Oppermann 		 * the next incoming segment to the scaled value.
16625396d0f8SAndre Oppermann 		 */
16635396d0f8SAndre Oppermann 		tp->snd_wnd = th->th_win;
16648e051165SRichard Scheffenegger 		if ((to.to_flags & TOF_TS) &&
16652593f858SRichard Scheffenegger 		    (tp->t_flags & TF_REQ_TSTMP) &&
16662593f858SRichard Scheffenegger 		    !(tp->t_flags & TF_NOOPT)) {
1667be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_TSTMP;
1668be2ac88cSJonathan Lemon 			tp->ts_recent = to.to_tsval;
1669d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
16708e051165SRichard Scheffenegger 		} else
16718e051165SRichard Scheffenegger 			tp->t_flags &= ~TF_REQ_TSTMP;
1672be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_MSS)
1673be2ac88cSJonathan Lemon 			tcp_mss(tp, to.to_mss);
16743529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
16752593f858SRichard Scheffenegger 		    (!(to.to_flags & TOF_SACKPERM) ||
16762593f858SRichard Scheffenegger 		    (tp->t_flags & TF_NOOPT)))
16773529149eSAndre Oppermann 			tp->t_flags &= ~TF_SACK_PERMIT;
1678c560df6fSPatrick Kelsey 		if (IS_FASTOPEN(tp->t_flags)) {
16792593f858SRichard Scheffenegger 			if ((to.to_flags & TOF_FASTOPEN) &&
16802593f858SRichard Scheffenegger 			    !(tp->t_flags & TF_NOOPT)) {
1681a026a53aSMichael Tuexen 				uint16_t mss;
1682a026a53aSMichael Tuexen 
1683a026a53aSMichael Tuexen 				if (to.to_flags & TOF_MSS)
1684a026a53aSMichael Tuexen 					mss = to.to_mss;
1685c560df6fSPatrick Kelsey 				else
1686a026a53aSMichael Tuexen 					if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
1687a026a53aSMichael Tuexen 						mss = TCP6_MSS;
1688a026a53aSMichael Tuexen 					else
1689a026a53aSMichael Tuexen 						mss = TCP_MSS;
1690a026a53aSMichael Tuexen 				tcp_fastopen_update_cache(tp, mss,
1691a026a53aSMichael Tuexen 				    to.to_tfo_len, to.to_tfo_cookie);
1692a026a53aSMichael Tuexen 			} else
1693c560df6fSPatrick Kelsey 				tcp_fastopen_disable_path(tp);
1694c560df6fSPatrick Kelsey 		}
16956d90faf3SPaul Saab 	}
16966d90faf3SPaul Saab 
1697df8bae1dSRodney W. Grimes 	/*
1698283c76c7SMichael Tuexen 	 * If timestamps were negotiated during SYN/ACK and a
1699283c76c7SMichael Tuexen 	 * segment without a timestamp is received, silently drop
1700d2b3ceddSMichael Tuexen 	 * the segment, unless it is a RST segment or missing timestamps are
1701d2b3ceddSMichael Tuexen 	 * tolerated.
1702283c76c7SMichael Tuexen 	 * See section 3.2 of RFC 7323.
17034da9052aSMichael Tuexen 	 */
17044da9052aSMichael Tuexen 	if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) {
1705d2b3ceddSMichael Tuexen 		if (((thflags & TH_RST) != 0) || V_tcp_tolerate_missing_ts) {
1706cc3c3485SMichael Tuexen 			if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
1707cc3c3485SMichael Tuexen 				log(LOG_DEBUG, "%s; %s: Timestamp missing, "
1708cc3c3485SMichael Tuexen 				    "segment processed normally\n",
1709cc3c3485SMichael Tuexen 				    s, __func__);
1710cc3c3485SMichael Tuexen 				free(s, M_TCPLOG);
1711cc3c3485SMichael Tuexen 			}
1712cc3c3485SMichael Tuexen 		} else {
17134da9052aSMichael Tuexen 			if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
17144da9052aSMichael Tuexen 				log(LOG_DEBUG, "%s; %s: Timestamp missing, "
1715283c76c7SMichael Tuexen 				    "segment silently dropped\n", s, __func__);
17164da9052aSMichael Tuexen 				free(s, M_TCPLOG);
17174da9052aSMichael Tuexen 			}
1718283c76c7SMichael Tuexen 			goto drop;
17194da9052aSMichael Tuexen 		}
1720cc3c3485SMichael Tuexen 	}
1721283c76c7SMichael Tuexen 	/*
1722283c76c7SMichael Tuexen 	 * If timestamps were not negotiated during SYN/ACK and a
172375fcd27aSMichael Tuexen 	 * segment with a timestamp is received, ignore the
1724283c76c7SMichael Tuexen 	 * timestamp and process the packet normally.
1725283c76c7SMichael Tuexen 	 * See section 3.2 of RFC 7323.
1726283c76c7SMichael Tuexen 	 */
17274da9052aSMichael Tuexen 	if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) {
17284da9052aSMichael Tuexen 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
17294da9052aSMichael Tuexen 			log(LOG_DEBUG, "%s; %s: Timestamp not expected, "
1730283c76c7SMichael Tuexen 			    "segment processed normally\n", s, __func__);
17314da9052aSMichael Tuexen 			free(s, M_TCPLOG);
17324da9052aSMichael Tuexen 		}
17334da9052aSMichael Tuexen 	}
17344da9052aSMichael Tuexen 
17354da9052aSMichael Tuexen 	/*
1736df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
1737df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
1738df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
1739df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
1740df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
1741df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
1742df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
1743df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
1744df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
1745df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
1746df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
1747df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
1748a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
1749c5ad39b9SAndre Oppermann 	 * Since we check for TCPS_ESTABLISHED first, it can only
1750a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
1751df8bae1dSRodney W. Grimes 	 */
1752df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
1753c5ad39b9SAndre Oppermann 	    th->th_seq == tp->rcv_nxt &&
1754fb59c426SYoshinobu Inoue 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1755c5ad39b9SAndre Oppermann 	    tp->snd_nxt == tp->snd_max &&
1756c5ad39b9SAndre Oppermann 	    tiwin && tiwin == tp->snd_wnd &&
1757a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
1758c28440dbSRandall Stewart 	    SEGQ_EMPTY(tp) &&
17594741bfcbSPatrick Kelsey 	    ((to.to_flags & TOF_TS) == 0 ||
1760c5ad39b9SAndre Oppermann 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) {
1761df8bae1dSRodney W. Grimes 		/*
1762df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
1763df8bae1dSRodney W. Grimes 		 * record the timestamp.
1764a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
1765a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1766df8bae1dSRodney W. Grimes 		 */
1767be2ac88cSJonathan Lemon 		if ((to.to_flags & TOF_TS) != 0 &&
1768fb59c426SYoshinobu Inoue 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1769d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
1770a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
1771df8bae1dSRodney W. Grimes 		}
1772df8bae1dSRodney W. Grimes 
1773fb59c426SYoshinobu Inoue 		if (tlen == 0) {
1774fb59c426SYoshinobu Inoue 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
1775fb59c426SYoshinobu Inoue 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
1776dbc42409SLawrence Stewart 			    !IN_RECOVERY(tp->t_flags) &&
1777fc30a251SAndre Oppermann 			    (to.to_flags & TOF_SACK) == 0 &&
1778dbc42409SLawrence Stewart 			    TAILQ_EMPTY(&tp->snd_holes)) {
1779df8bae1dSRodney W. Grimes 				/*
1780e8949f74SAndre Oppermann 				 * This is a pure ack for outstanding data.
1781df8bae1dSRodney W. Grimes 				 */
178278b50714SRobert Watson 				TCPSTAT_INC(tcps_predack);
1783252ca428SRobert Watson 
17849b8b58e0SJonathan Lemon 				/*
178510d20c84SMatt Macy 				 * "bad retransmit" recovery without timestamps.
17869b8b58e0SJonathan Lemon 				 */
178710d20c84SMatt Macy 				if ((to.to_flags & TOF_TS) == 0 &&
178810d20c84SMatt Macy 				    tp->t_rxtshift == 1 &&
1789672dc4aeSJohn Baldwin 				    tp->t_flags & TF_PREVVALID &&
17904531b345SRichard Scheffenegger 				    tp->t_badrxtwin != 0 &&
17914531b345SRichard Scheffenegger 				    TSTMP_LT(ticks, tp->t_badrxtwin)) {
1792dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_RTO_ERR);
17939b8b58e0SJonathan Lemon 				}
1794fa55172bSMatthew Dillon 
1795fa55172bSMatthew Dillon 				/*
1796fa55172bSMatthew Dillon 				 * Recalculate the transmit timer / rtt.
1797fa55172bSMatthew Dillon 				 *
1798fa55172bSMatthew Dillon 				 * Some boxes send broken timestamp replies
1799fa55172bSMatthew Dillon 				 * during the SYN+ACK phase, ignore
1800fa55172bSMatthew Dillon 				 * timestamps of 0 or we could calculate a
1801fa55172bSMatthew Dillon 				 * huge RTT and blow up the retransmit timer.
1802fa55172bSMatthew Dillon 				 */
1803fa55172bSMatthew Dillon 				if ((to.to_flags & TOF_TS) != 0 &&
1804fa55172bSMatthew Dillon 				    to.to_tsecr) {
18053ac12506SJonathan T. Looney 					uint32_t t;
1806d8951c8aSBjoern A. Zeeb 
1807d8951c8aSBjoern A. Zeeb 					t = tcp_ts_getticks() - to.to_tsecr;
1808d8951c8aSBjoern A. Zeeb 					if (!tp->t_rttlow || tp->t_rttlow > t)
1809d8951c8aSBjoern A. Zeeb 						tp->t_rttlow = t;
1810a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
1811d8951c8aSBjoern A. Zeeb 					    TCP_TS_TO_TICKS(t) + 1);
1812fa55172bSMatthew Dillon 				} else if (tp->t_rtttime &&
1813fa55172bSMatthew Dillon 				    SEQ_GT(th->th_ack, tp->t_rtseq)) {
1814eaf80179SAndre Oppermann 					if (!tp->t_rttlow ||
1815eaf80179SAndre Oppermann 					    tp->t_rttlow > ticks - tp->t_rtttime)
1816eaf80179SAndre Oppermann 						tp->t_rttlow = ticks - tp->t_rtttime;
1817c068736aSJeffrey Hsu 					tcp_xmit_timer(tp,
1818c068736aSJeffrey Hsu 							ticks - tp->t_rtttime);
1819fa55172bSMatthew Dillon 				}
1820dbc42409SLawrence Stewart 				acked = BYTES_THIS_ACK(tp, th);
182139bc9de5SLawrence Stewart 
1822bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
182339bc9de5SLawrence Stewart 				/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
182439bc9de5SLawrence Stewart 				hhook_run_tcp_est_in(tp, th, &to);
1825bd79708dSJonathan T. Looney #endif
182639bc9de5SLawrence Stewart 
18274b7b743cSLawrence Stewart 				TCPSTAT_ADD(tcps_rcvackpack, nsegs);
182878b50714SRobert Watson 				TCPSTAT_ADD(tcps_rcvackbyte, acked);
1829df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
18309d11646dSJeffrey Hsu 				if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
18319d11646dSJeffrey Hsu 				    SEQ_LEQ(th->th_ack, tp->snd_recover))
18329d11646dSJeffrey Hsu 					tp->snd_recover = th->th_ack - 1;
1833dbc42409SLawrence Stewart 
1834dbc42409SLawrence Stewart 				/*
1835dbc42409SLawrence Stewart 				 * Let the congestion control algorithm update
1836dbc42409SLawrence Stewart 				 * congestion control related information. This
1837dbc42409SLawrence Stewart 				 * typically means increasing the congestion
1838dbc42409SLawrence Stewart 				 * window.
1839dbc42409SLawrence Stewart 				 */
18404b7b743cSLawrence Stewart 				cc_ack_received(tp, th, nsegs, CC_ACK);
1841dbc42409SLawrence Stewart 
18429d11646dSJeffrey Hsu 				tp->snd_una = th->th_ack;
18431645d090SJeff Roberson 				/*
1844e8949f74SAndre Oppermann 				 * Pull snd_wl2 up to prevent seq wrap relative
18451645d090SJeff Roberson 				 * to th_ack.
18461645d090SJeff Roberson 				 */
1847cb942153SJeffrey Hsu 				tp->snd_wl2 = th->th_ack;
1848b5addd85SJeffrey Hsu 				tp->t_dupacks = 0;
1849df8bae1dSRodney W. Grimes 				m_freem(m);
1850df8bae1dSRodney W. Grimes 
1851df8bae1dSRodney W. Grimes 				/*
1852df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
1853df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
1854df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
1855df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
1856df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
1857df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
1858df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
1859e8949f74SAndre Oppermann 				 */
18603c653157SHartmut Brandt #ifdef TCPDEBUG
18613c653157SHartmut Brandt 				if (so->so_options & SO_DEBUG)
18623c653157SHartmut Brandt 					tcp_trace(TA_INPUT, ostate, tp,
18633c653157SHartmut Brandt 					    (void *)tcp_saveipgen,
18643c653157SHartmut Brandt 					    &tcp_savetcp, 0);
18653c653157SHartmut Brandt #endif
18662b9c9984SGeorge V. Neville-Neil 				TCP_PROBE3(debug__input, tp, th, m);
186708af8aacSRandall Stewart 				/*
186808af8aacSRandall Stewart 				 * Clear t_acktime if remote side has ACKd
186908af8aacSRandall Stewart 				 * all data in the socket buffer.
187008af8aacSRandall Stewart 				 * Otherwise, update t_acktime if we received
187108af8aacSRandall Stewart 				 * a sufficiently large ACK.
187208af8aacSRandall Stewart 				 */
187308af8aacSRandall Stewart 				if (sbavail(&so->so_snd) == 0)
187408af8aacSRandall Stewart 					tp->t_acktime = 0;
187508af8aacSRandall Stewart 				else if (acked > 1)
187608af8aacSRandall Stewart 					tp->t_acktime = ticks;
1877df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
1878b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
1879b8152ba7SAndre Oppermann 				else if (!tcp_timer_active(tp, TT_PERSIST))
1880b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT,
188108af8aacSRandall Stewart 					    TP_RXTCUR(tp));
1882032bf749SRichard Scheffenegger 				sowwakeup(so);
1883cfa6009eSGleb Smirnoff 				if (sbavail(&so->so_snd))
188440fa3e40SGleb Smirnoff 					(void) tcp_output(tp);
1885a14c749fSJonathan Lemon 				goto check_delack;
1886df8bae1dSRodney W. Grimes 			}
1887fb59c426SYoshinobu Inoue 		} else if (th->th_ack == tp->snd_una &&
1888fb59c426SYoshinobu Inoue 		    tlen <= sbspace(&so->so_rcv)) {
18896741ecf5SAndre Oppermann 			int newsize = 0;	/* automatic sockbuf scaling */
18906741ecf5SAndre Oppermann 
1891df8bae1dSRodney W. Grimes 			/*
1892252ca428SRobert Watson 			 * This is a pure, in-sequence data packet with
1893252ca428SRobert Watson 			 * nothing on the reassembly queue and we have enough
1894252ca428SRobert Watson 			 * buffer space to take it.
1895df8bae1dSRodney W. Grimes 			 */
18966d90faf3SPaul Saab 			/* Clean receiver SACK report if present */
18973529149eSAndre Oppermann 			if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks)
18986d90faf3SPaul Saab 				tcp_clean_sackreport(tp);
189978b50714SRobert Watson 			TCPSTAT_INC(tcps_preddat);
1900fb59c426SYoshinobu Inoue 			tp->rcv_nxt += tlen;
1901e854dd38SRandall Stewart 			if (tlen &&
1902e854dd38SRandall Stewart 			    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1903e854dd38SRandall Stewart 			    (tp->t_fbyte_in == 0)) {
1904e854dd38SRandall Stewart 				tp->t_fbyte_in = ticks;
1905e854dd38SRandall Stewart 				if (tp->t_fbyte_in == 0)
1906e854dd38SRandall Stewart 					tp->t_fbyte_in = 1;
1907e854dd38SRandall Stewart 				if (tp->t_fbyte_out && tp->t_fbyte_in)
1908e854dd38SRandall Stewart 					tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1909e854dd38SRandall Stewart 			}
19101645d090SJeff Roberson 			/*
19111645d090SJeff Roberson 			 * Pull snd_wl1 up to prevent seq wrap relative to
19121645d090SJeff Roberson 			 * th_seq.
19131645d090SJeff Roberson 			 */
191460ee3bb2SAndre Oppermann 			tp->snd_wl1 = th->th_seq;
19151645d090SJeff Roberson 			/*
19161645d090SJeff Roberson 			 * Pull rcv_up up to prevent seq wrap relative to
19171645d090SJeff Roberson 			 * rcv_nxt.
19181645d090SJeff Roberson 			 */
19191645d090SJeff Roberson 			tp->rcv_up = tp->rcv_nxt;
19204b7b743cSLawrence Stewart 			TCPSTAT_ADD(tcps_rcvpack, nsegs);
192178b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
19223c653157SHartmut Brandt #ifdef TCPDEBUG
19233c653157SHartmut Brandt 			if (so->so_options & SO_DEBUG)
19243c653157SHartmut Brandt 				tcp_trace(TA_INPUT, ostate, tp,
19253c653157SHartmut Brandt 				    (void *)tcp_saveipgen, &tcp_savetcp, 0);
19263c653157SHartmut Brandt #endif
19272b9c9984SGeorge V. Neville-Neil 			TCP_PROBE3(debug__input, tp, th, m);
19285d06879aSGeorge V. Neville-Neil 
1929e44c1887SSteven Hartland 			newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
19306741ecf5SAndre Oppermann 
19316741ecf5SAndre Oppermann 			/* Add data to socket buffer. */
19321e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
1933c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1934c1c36a2cSMike Silbersack 				m_freem(m);
1935c1c36a2cSMike Silbersack 			} else {
19366741ecf5SAndre Oppermann 				/*
19376741ecf5SAndre Oppermann 				 * Set new socket buffer size.
19386741ecf5SAndre Oppermann 				 * Give up when limit is reached.
19396741ecf5SAndre Oppermann 				 */
19406741ecf5SAndre Oppermann 				if (newsize)
194143283184SGleb Smirnoff 					if (!sbreserve_locked(so, SO_RCV,
194243283184SGleb Smirnoff 					    newsize, NULL))
19436741ecf5SAndre Oppermann 						so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
1944fb59c426SYoshinobu Inoue 				m_adj(m, drop_hdrlen);	/* delayed header drop */
1945651e4e6aSGleb Smirnoff 				sbappendstream_locked(&so->so_rcv, m, 0);
1946c1c36a2cSMike Silbersack 			}
1947032bf749SRichard Scheffenegger 			/* NB: sorwakeup_locked() does an implicit unlock. */
1948032bf749SRichard Scheffenegger 			sorwakeup_locked(so);
1949c1e5a6e5SAndre Oppermann 			if (DELAY_ACK(tp, tlen)) {
19503bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
1951f498eeeeSDavid Greenman 			} else {
1952e612a582SDavid Greenman 				tp->t_flags |= TF_ACKNOW;
195340fa3e40SGleb Smirnoff 				tcp_output(tp);
1954e612a582SDavid Greenman 			}
1955a14c749fSJonathan Lemon 			goto check_delack;
1956df8bae1dSRodney W. Grimes 		}
1957df8bae1dSRodney W. Grimes 	}
1958df8bae1dSRodney W. Grimes 
1959df8bae1dSRodney W. Grimes 	/*
1960df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
1961df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
1962df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
1963df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
1964df8bae1dSRodney W. Grimes 	 */
1965df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
1966df8bae1dSRodney W. Grimes 	if (win < 0)
1967df8bae1dSRodney W. Grimes 		win = 0;
196866e39adcSJohn Polstra 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1969df8bae1dSRodney W. Grimes 
1970df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1971df8bae1dSRodney W. Grimes 	/*
1972764d8cefSBill Fenner 	 * If the state is SYN_RECEIVED:
1973764d8cefSBill Fenner 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
1974764d8cefSBill Fenner 	 */
1975764d8cefSBill Fenner 	case TCPS_SYN_RECEIVED:
1976fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1977fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1978a57815efSBosko Milekic 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1979a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
1980d1b07f36SRandall Stewart 				tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
1981a57815efSBosko Milekic 				goto dropwithreset;
1982a57815efSBosko Milekic 		}
198368bd7ed1SJonathan T. Looney 		if (IS_FASTOPEN(tp->t_flags)) {
1984281a0fd4SPatrick Kelsey 			/*
1985281a0fd4SPatrick Kelsey 			 * When a TFO connection is in SYN_RECEIVED, the
1986281a0fd4SPatrick Kelsey 			 * only valid packets are the initial SYN, a
1987281a0fd4SPatrick Kelsey 			 * retransmit/copy of the initial SYN (possibly with
1988281a0fd4SPatrick Kelsey 			 * a subset of the original data), a valid ACK, a
1989281a0fd4SPatrick Kelsey 			 * FIN, or a RST.
1990281a0fd4SPatrick Kelsey 			 */
1991281a0fd4SPatrick Kelsey 			if ((thflags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) {
1992281a0fd4SPatrick Kelsey 				rstreason = BANDLIM_RST_OPENPORT;
1993d1b07f36SRandall Stewart 				tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
1994281a0fd4SPatrick Kelsey 				goto dropwithreset;
1995281a0fd4SPatrick Kelsey 			} else if (thflags & TH_SYN) {
1996281a0fd4SPatrick Kelsey 				/* non-initial SYN is ignored */
1997281a0fd4SPatrick Kelsey 				if ((tcp_timer_active(tp, TT_DELACK) ||
1998281a0fd4SPatrick Kelsey 				     tcp_timer_active(tp, TT_REXMT)))
1999281a0fd4SPatrick Kelsey 					goto drop;
2000281a0fd4SPatrick Kelsey 			} else if (!(thflags & (TH_ACK|TH_FIN|TH_RST))) {
2001281a0fd4SPatrick Kelsey 				goto drop;
2002281a0fd4SPatrick Kelsey 			}
2003281a0fd4SPatrick Kelsey 		}
2004764d8cefSBill Fenner 		break;
2005764d8cefSBill Fenner 
2006764d8cefSBill Fenner 	/*
2007df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
200898732609SMichael Tuexen 	 *	if seg contains a RST with valid ACK (SEQ.ACK has already
200998732609SMichael Tuexen 	 *	    been verified), then drop the connection.
201098732609SMichael Tuexen 	 *	if seg contains a RST without an ACK, drop the seg.
201198732609SMichael Tuexen 	 *	if seg does not contain SYN, then drop the seg.
2012df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
2013df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
2014df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
2015f2512ba1SRui Paulo 	 *	if seg contains an ECE and ECN support is enabled, the stream
2016f2512ba1SRui Paulo 	 *	    is ECN capable.
2017df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
2018df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
2019df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
2020df8bae1dSRodney W. Grimes 	 */
2021df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
202257f60867SMark Johnston 		if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) {
2023d9fae5abSAndriy Gapon 			TCP_PROBE5(connect__refused, NULL, tp,
20242b9c9984SGeorge V. Neville-Neil 			    m, tp, th);
2025d1b07f36SRandall Stewart 			tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
2026df8bae1dSRodney W. Grimes 			tp = tcp_drop(tp, ECONNREFUSED);
202757f60867SMark Johnston 		}
20280ca3f933SAndre Oppermann 		if (thflags & TH_RST)
2029df8bae1dSRodney W. Grimes 			goto drop;
20300ca3f933SAndre Oppermann 		if (!(thflags & TH_SYN))
2031df8bae1dSRodney W. Grimes 			goto drop;
2032464fcfbcSAndre Oppermann 
2033fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
2034df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
2035fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
2036c560df6fSPatrick Kelsey 			int tfo_partial_ack = 0;
2037c560df6fSPatrick Kelsey 
203878b50714SRobert Watson 			TCPSTAT_INC(tcps_connects);
2039845799c1SAndras Olah 			soisconnected(so);
2040c488362eSRobert Watson #ifdef MAC
204130d239bcSRobert Watson 			mac_socketpeer_set_from_mbuf(m, so);
2042c488362eSRobert Watson #endif
2043845799c1SAndras Olah 			/* Do window scaling on this connection? */
2044845799c1SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2045845799c1SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
2046845799c1SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2047845799c1SAndras Olah 			}
20483ac12506SJonathan T. Looney 			tp->rcv_adv += min(tp->rcv_wnd,
2049766282cbSJohn Baldwin 			    TCP_MAXWIN << tp->rcv_scale);
2050a0292f23SGarrett Wollman 			tp->snd_una++;		/* SYN is acked */
2051a0292f23SGarrett Wollman 			/*
2052c560df6fSPatrick Kelsey 			 * If not all the data that was sent in the TFO SYN
2053c560df6fSPatrick Kelsey 			 * has been acked, resend the remainder right away.
2054c560df6fSPatrick Kelsey 			 */
2055c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags) &&
2056c560df6fSPatrick Kelsey 			    (tp->snd_una != tp->snd_max)) {
2057c560df6fSPatrick Kelsey 				tp->snd_nxt = th->th_ack;
2058c560df6fSPatrick Kelsey 				tfo_partial_ack = 1;
2059c560df6fSPatrick Kelsey 			}
2060c560df6fSPatrick Kelsey 			/*
2061a0292f23SGarrett Wollman 			 * If there's data, delay ACK; if there's also a FIN
2062a0292f23SGarrett Wollman 			 * ACKNOW will be turned on later.
2063a0292f23SGarrett Wollman 			 */
2064c560df6fSPatrick Kelsey 			if (DELAY_ACK(tp, tlen) && tlen != 0 && !tfo_partial_ack)
2065b8152ba7SAndre Oppermann 				tcp_timer_activate(tp, TT_DELACK,
2066b8152ba7SAndre Oppermann 				    tcp_delacktime);
2067a0292f23SGarrett Wollman 			else
2068a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
2069f2512ba1SRui Paulo 
2070f7220c48SRichard Scheffenegger 			tcp_ecn_input_syn_sent(tp, thflags, iptos);
2071f2512ba1SRui Paulo 
2072a0292f23SGarrett Wollman 			/*
2073a0292f23SGarrett Wollman 			 * Received <SYN,ACK> in SYN_SENT[*] state.
2074a0292f23SGarrett Wollman 			 * Transitions:
2075a0292f23SGarrett Wollman 			 *	SYN_SENT  --> ESTABLISHED
2076a0292f23SGarrett Wollman 			 *	SYN_SENT* --> FIN_WAIT_1
2077a0292f23SGarrett Wollman 			 */
20789b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
2079a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDFIN) {
208008af8aacSRandall Stewart 				tp->t_acktime = ticks;
208157f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_1);
2082a0292f23SGarrett Wollman 				tp->t_flags &= ~TF_NEEDFIN;
2083fb59c426SYoshinobu Inoue 				thflags &= ~TH_SYN;
20847ff19458SPaul Traina 			} else {
208557f60867SMark Johnston 				tcp_state_change(tp, TCPS_ESTABLISHED);
2086d9fae5abSAndriy Gapon 				TCP_PROBE5(connect__established, NULL, tp,
20872b9c9984SGeorge V. Neville-Neil 				    m, tp, th);
2088dbc42409SLawrence Stewart 				cc_conn_init(tp);
20899077f387SGleb Smirnoff 				tcp_timer_activate(tp, TT_KEEP,
20909077f387SGleb Smirnoff 				    TP_KEEPIDLE(tp));
20917ff19458SPaul Traina 			}
2092a0292f23SGarrett Wollman 		} else {
2093a0292f23SGarrett Wollman 			/*
2094c068736aSJeffrey Hsu 			 * Received initial SYN in SYN-SENT[*] state =>
2095153edc50SHiren Panchasara 			 * simultaneous open.
2096c068736aSJeffrey Hsu 			 * If it succeeds, connection is * half-synchronized.
2097c068736aSJeffrey Hsu 			 * Otherwise, do 3-way handshake:
2098a0292f23SGarrett Wollman 			 *        SYN-SENT -> SYN-RECEIVED
2099a0292f23SGarrett Wollman 			 *        SYN-SENT* -> SYN-RECEIVED*
2100a0292f23SGarrett Wollman 			 */
2101493105c2SGleb Smirnoff 			tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN | TF_SONOTCONN);
2102b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
210357f60867SMark Johnston 			tcp_state_change(tp, TCPS_SYN_RECEIVED);
2104a0292f23SGarrett Wollman 		}
2105df8bae1dSRodney W. Grimes 
2106df8bae1dSRodney W. Grimes 		/*
2107fb59c426SYoshinobu Inoue 		 * Advance th->th_seq to correspond to first data byte.
2108df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
2109df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
2110df8bae1dSRodney W. Grimes 		 */
2111fb59c426SYoshinobu Inoue 		th->th_seq++;
2112fb59c426SYoshinobu Inoue 		if (tlen > tp->rcv_wnd) {
2113fb59c426SYoshinobu Inoue 			todrop = tlen - tp->rcv_wnd;
2114df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
2115fb59c426SYoshinobu Inoue 			tlen = tp->rcv_wnd;
2116fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
211778b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpackafterwin);
211878b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2119df8bae1dSRodney W. Grimes 		}
2120fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
2121fb59c426SYoshinobu Inoue 		tp->rcv_up = th->th_seq;
2122a0292f23SGarrett Wollman 		/*
2123a0292f23SGarrett Wollman 		 * Client side of transaction: already sent SYN and data.
2124a0292f23SGarrett Wollman 		 * If the remote host used T/TCP to validate the SYN,
2125a0292f23SGarrett Wollman 		 * our data will be ACK'd; if so, enter normal data segment
2126a0292f23SGarrett Wollman 		 * processing in the middle of step 5, ack processing.
2127a0292f23SGarrett Wollman 		 * Otherwise, goto step 6.
2128a0292f23SGarrett Wollman 		 */
2129fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK)
2130a0292f23SGarrett Wollman 			goto process_ACK;
2131c068736aSJeffrey Hsu 
2132df8bae1dSRodney W. Grimes 		goto step6;
2133df8bae1dSRodney W. Grimes 	}
2134df8bae1dSRodney W. Grimes 
2135df8bae1dSRodney W. Grimes 	/*
2136df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
213780ab7c0eSGarrett Wollman 	 * First check the RST flag and sequence number since reset segments
213880ab7c0eSGarrett Wollman 	 * are exempt from the timestamp and connection count tests.  This
213980ab7c0eSGarrett Wollman 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
214080ab7c0eSGarrett Wollman 	 * below which allowed reset segments in half the sequence space
214180ab7c0eSGarrett Wollman 	 * to fall though and be processed (which gives forged reset
214280ab7c0eSGarrett Wollman 	 * segments with a random sequence number a 50 percent chance of
214380ab7c0eSGarrett Wollman 	 * killing a connection).
214480ab7c0eSGarrett Wollman 	 * Then check timestamp, if present.
2145a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
2146df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
2147df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
2148df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
214980ab7c0eSGarrett Wollman 	 */
2150fb59c426SYoshinobu Inoue 	if (thflags & TH_RST) {
21513220a212SGleb Smirnoff 		/*
21523220a212SGleb Smirnoff 		 * RFC5961 Section 3.2
21533220a212SGleb Smirnoff 		 *
21543220a212SGleb Smirnoff 		 * - RST drops connection only if SEG.SEQ == RCV.NXT.
21553220a212SGleb Smirnoff 		 * - If RST is in window, we send challenge ACK.
21563220a212SGleb Smirnoff 		 *
21573220a212SGleb Smirnoff 		 * Note: to take into account delayed ACKs, we should
21583220a212SGleb Smirnoff 		 *   test against last_ack_sent instead of rcv_nxt.
21593220a212SGleb Smirnoff 		 * Note 2: we handle special case of closed window, not
21603220a212SGleb Smirnoff 		 *   covered by the RFC.
21613220a212SGleb Smirnoff 		 */
21623220a212SGleb Smirnoff 		if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
21633220a212SGleb Smirnoff 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
21643220a212SGleb Smirnoff 		    (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) {
21653220a212SGleb Smirnoff 			KASSERT(tp->t_state != TCPS_SYN_SENT,
21663220a212SGleb Smirnoff 			    ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p",
21673220a212SGleb Smirnoff 			    __func__, th, tp));
21683220a212SGleb Smirnoff 
21693220a212SGleb Smirnoff 			if (V_tcp_insecure_rst ||
21703220a212SGleb Smirnoff 			    tp->last_ack_sent == th->th_seq) {
21713220a212SGleb Smirnoff 				TCPSTAT_INC(tcps_drops);
21723220a212SGleb Smirnoff 				/* Drop the connection. */
21733220a212SGleb Smirnoff 				switch (tp->t_state) {
217480ab7c0eSGarrett Wollman 				case TCPS_SYN_RECEIVED:
217580ab7c0eSGarrett Wollman 					so->so_error = ECONNREFUSED;
217680ab7c0eSGarrett Wollman 					goto close;
217780ab7c0eSGarrett Wollman 				case TCPS_ESTABLISHED:
217880ab7c0eSGarrett Wollman 				case TCPS_FIN_WAIT_1:
217980ab7c0eSGarrett Wollman 				case TCPS_FIN_WAIT_2:
218080ab7c0eSGarrett Wollman 				case TCPS_CLOSE_WAIT:
21816779a1a1SMichael Tuexen 				case TCPS_CLOSING:
21826779a1a1SMichael Tuexen 				case TCPS_LAST_ACK:
218380ab7c0eSGarrett Wollman 					so->so_error = ECONNRESET;
218480ab7c0eSGarrett Wollman 				close:
21853220a212SGleb Smirnoff 					/* FALLTHROUGH */
21863220a212SGleb Smirnoff 				default:
2187d1b07f36SRandall Stewart 					tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_RST);
218880ab7c0eSGarrett Wollman 					tp = tcp_close(tp);
21893220a212SGleb Smirnoff 				}
21903220a212SGleb Smirnoff 			} else {
21913220a212SGleb Smirnoff 				TCPSTAT_INC(tcps_badrst);
21923220a212SGleb Smirnoff 				/* Send challenge ACK. */
21933220a212SGleb Smirnoff 				tcp_respond(tp, mtod(m, void *), th, m,
21943220a212SGleb Smirnoff 				    tp->rcv_nxt, tp->snd_nxt, TH_ACK);
21953220a212SGleb Smirnoff 				tp->last_ack_sent = tp->rcv_nxt;
21963220a212SGleb Smirnoff 				m = NULL;
21973220a212SGleb Smirnoff 			}
21983220a212SGleb Smirnoff 		}
21993220a212SGleb Smirnoff 		goto drop;
22003220a212SGleb Smirnoff 	}
220180ab7c0eSGarrett Wollman 
22023220a212SGleb Smirnoff 	/*
22033220a212SGleb Smirnoff 	 * RFC5961 Section 4.2
22043220a212SGleb Smirnoff 	 * Send challenge ACK for any SYN in synchronized state.
22053220a212SGleb Smirnoff 	 */
2206281a0fd4SPatrick Kelsey 	if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT &&
2207281a0fd4SPatrick Kelsey 	    tp->t_state != TCPS_SYN_RECEIVED) {
22083220a212SGleb Smirnoff 		TCPSTAT_INC(tcps_badsyn);
22093220a212SGleb Smirnoff 		if (V_tcp_insecure_syn &&
22103220a212SGleb Smirnoff 		    SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
22113220a212SGleb Smirnoff 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
2212d1b07f36SRandall Stewart 			tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
22133220a212SGleb Smirnoff 			tp = tcp_drop(tp, ECONNRESET);
22143220a212SGleb Smirnoff 			rstreason = BANDLIM_UNLIMITED;
22153220a212SGleb Smirnoff 		} else {
221683c1ec92SRichard Scheffenegger 			tcp_ecn_input_syn_sent(tp, thflags, iptos);
22173220a212SGleb Smirnoff 			/* Send challenge ACK. */
22183220a212SGleb Smirnoff 			tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt,
22193220a212SGleb Smirnoff 			    tp->snd_nxt, TH_ACK);
22203220a212SGleb Smirnoff 			tp->last_ack_sent = tp->rcv_nxt;
22213220a212SGleb Smirnoff 			m = NULL;
222280ab7c0eSGarrett Wollman 		}
222380ab7c0eSGarrett Wollman 		goto drop;
222480ab7c0eSGarrett Wollman 	}
222580ab7c0eSGarrett Wollman 
222680ab7c0eSGarrett Wollman 	/*
2227df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
2228df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
2229df8bae1dSRodney W. Grimes 	 */
2230be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
223180ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
2232df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
2233d8951c8aSBjoern A. Zeeb 		if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) {
2234df8bae1dSRodney W. Grimes 			/*
2235df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
2236df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
2237df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
2238df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
2239df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
2240df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
2241df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
2242df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
2243df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
2244df8bae1dSRodney W. Grimes 			 */
2245df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
2246df8bae1dSRodney W. Grimes 		} else {
224778b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
224878b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, tlen);
224978b50714SRobert Watson 			TCPSTAT_INC(tcps_pawsdrop);
225007fd333dSMatthew Dillon 			if (tlen)
2251df8bae1dSRodney W. Grimes 				goto dropafterack;
22521ab4789dSMatthew Dillon 			goto drop;
22531ab4789dSMatthew Dillon 		}
2254df8bae1dSRodney W. Grimes 	}
2255df8bae1dSRodney W. Grimes 
2256a0292f23SGarrett Wollman 	/*
225780ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
225880ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
225980ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
226080ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
226180ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
226280ab7c0eSGarrett Wollman 	 */
2263a57815efSBosko Milekic 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
2264a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2265d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
2266a57815efSBosko Milekic 		goto dropwithreset;
2267a57815efSBosko Milekic 	}
226880ab7c0eSGarrett Wollman 
2269fb59c426SYoshinobu Inoue 	todrop = tp->rcv_nxt - th->th_seq;
2270df8bae1dSRodney W. Grimes 	if (todrop > 0) {
2271831ad37eSXin LI 		if (thflags & TH_SYN) {
2272fb59c426SYoshinobu Inoue 			thflags &= ~TH_SYN;
2273fb59c426SYoshinobu Inoue 			th->th_seq++;
2274fb59c426SYoshinobu Inoue 			if (th->th_urp > 1)
2275fb59c426SYoshinobu Inoue 				th->th_urp--;
2276df8bae1dSRodney W. Grimes 			else
2277fb59c426SYoshinobu Inoue 				thflags &= ~TH_URG;
2278df8bae1dSRodney W. Grimes 			todrop--;
2279df8bae1dSRodney W. Grimes 		}
2280df8bae1dSRodney W. Grimes 		/*
2281dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
2282df8bae1dSRodney W. Grimes 		 */
2283fb59c426SYoshinobu Inoue 		if (todrop > tlen
2284fb59c426SYoshinobu Inoue 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
2285dac20301SGarrett Wollman 			/*
2286dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
2287dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
2288dac20301SGarrett Wollman 			 * of sequence; drop it.
2289dac20301SGarrett Wollman 			 */
2290fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
2291dac20301SGarrett Wollman 
2292df8bae1dSRodney W. Grimes 			/*
2293dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
2294dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
2295df8bae1dSRodney W. Grimes 			 */
2296dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
2297fb59c426SYoshinobu Inoue 			todrop = tlen;
229878b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
229978b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, todrop);
2300df8bae1dSRodney W. Grimes 		} else {
230178b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpartduppack);
230278b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop);
2303df8bae1dSRodney W. Grimes 		}
23045acfd95cSMichael Tuexen 		/*
23055acfd95cSMichael Tuexen 		 * DSACK - add SACK block for dropped range
23065acfd95cSMichael Tuexen 		 */
23078f63a52bSMichael Tuexen 		if ((todrop > 0) && (tp->t_flags & TF_SACK_PERMIT)) {
2308ecc5b1d1SMichael Tuexen 			tcp_update_sack_list(tp, th->th_seq,
2309ecc5b1d1SMichael Tuexen 			    th->th_seq + todrop);
23105acfd95cSMichael Tuexen 			/*
23115acfd95cSMichael Tuexen 			 * ACK now, as the next in-sequence segment
23125acfd95cSMichael Tuexen 			 * will clear the DSACK block again
23135acfd95cSMichael Tuexen 			 */
23145acfd95cSMichael Tuexen 			tp->t_flags |= TF_ACKNOW;
23155acfd95cSMichael Tuexen 		}
2316fb59c426SYoshinobu Inoue 		drop_hdrlen += todrop;	/* drop from the top afterwards */
2317fb59c426SYoshinobu Inoue 		th->th_seq += todrop;
2318fb59c426SYoshinobu Inoue 		tlen -= todrop;
2319fb59c426SYoshinobu Inoue 		if (th->th_urp > todrop)
2320fb59c426SYoshinobu Inoue 			th->th_urp -= todrop;
2321df8bae1dSRodney W. Grimes 		else {
2322fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;
2323fb59c426SYoshinobu Inoue 			th->th_urp = 0;
2324df8bae1dSRodney W. Grimes 		}
2325df8bae1dSRodney W. Grimes 	}
2326df8bae1dSRodney W. Grimes 
2327df8bae1dSRodney W. Grimes 	/*
2328df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
2329df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
2330df8bae1dSRodney W. Grimes 	 */
233174703901SGleb Smirnoff 	if ((tp->t_flags & TF_CLOSED) && tlen) {
233207dacf03SAndre Oppermann 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
233307dacf03SAndre Oppermann 			log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data "
233407dacf03SAndre Oppermann 			    "after socket was closed, "
233507dacf03SAndre Oppermann 			    "sending RST and removing tcpcb\n",
2336e31d8aa3SMike Silbersack 			    s, __func__, tcpstates[tp->t_state], tlen);
2337773673c1SAndre Oppermann 			free(s, M_TCPLOG);
2338773673c1SAndre Oppermann 		}
2339d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
2340d1b07f36SRandall Stewart 		/* tcp_close will kill the inp pre-log the Reset */
2341d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
2342df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
234378b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvafterclose);
2344a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
2345df8bae1dSRodney W. Grimes 		goto dropwithreset;
2346df8bae1dSRodney W. Grimes 	}
2347df8bae1dSRodney W. Grimes 
2348df8bae1dSRodney W. Grimes 	/*
2349df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
2350df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
2351df8bae1dSRodney W. Grimes 	 */
2352fb59c426SYoshinobu Inoue 	todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
2353df8bae1dSRodney W. Grimes 	if (todrop > 0) {
235478b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvpackafterwin);
2355fb59c426SYoshinobu Inoue 		if (todrop >= tlen) {
235678b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen);
2357df8bae1dSRodney W. Grimes 			/*
2358df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
2359df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
2360df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
2361df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
2362df8bae1dSRodney W. Grimes 			 * and ack.
2363df8bae1dSRodney W. Grimes 			 */
2364fb59c426SYoshinobu Inoue 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
2365df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
236678b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvwinprobe);
2367df8bae1dSRodney W. Grimes 			} else
2368df8bae1dSRodney W. Grimes 				goto dropafterack;
2369df8bae1dSRodney W. Grimes 		} else
237078b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2371df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
2372fb59c426SYoshinobu Inoue 		tlen -= todrop;
2373fb59c426SYoshinobu Inoue 		thflags &= ~(TH_PUSH|TH_FIN);
2374df8bae1dSRodney W. Grimes 	}
2375df8bae1dSRodney W. Grimes 
2376df8bae1dSRodney W. Grimes 	/*
2377df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
2378df8bae1dSRodney W. Grimes 	 * record its timestamp.
2379cf09195bSPaul Saab 	 * NOTE:
2380cf09195bSPaul Saab 	 * 1) That the test incorporates suggestions from the latest
2381a0292f23SGarrett Wollman 	 *    proposal of the tcplw@cray.com list (Braden 1993/04/26).
2382cf09195bSPaul Saab 	 * 2) That updating only on newer timestamps interferes with
2383cf09195bSPaul Saab 	 *    our earlier PAWS tests, so this check should be solely
2384cf09195bSPaul Saab 	 *    predicated on the sequence space of this segment.
2385cf09195bSPaul Saab 	 * 3) That we modify the segment boundary check to be
2386cf09195bSPaul Saab 	 *        Last.ACK.Sent <= SEG.SEQ + SEG.Len
2387cf09195bSPaul Saab 	 *    instead of RFC1323's
2388cf09195bSPaul Saab 	 *        Last.ACK.Sent < SEG.SEQ + SEG.Len,
2389cf09195bSPaul Saab 	 *    This modified check allows us to overcome RFC1323's
2390cf09195bSPaul Saab 	 *    limitations as described in Stevens TCP/IP Illustrated
2391cf09195bSPaul Saab 	 *    Vol. 2 p.869. In such cases, we can still calculate the
2392cf09195bSPaul Saab 	 *    RTT correctly when RCV.NXT == Last.ACK.Sent.
2393df8bae1dSRodney W. Grimes 	 */
2394be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 &&
2395cf09195bSPaul Saab 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
2396cf09195bSPaul Saab 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
2397cf09195bSPaul Saab 		((thflags & (TH_SYN|TH_FIN)) != 0))) {
2398d8951c8aSBjoern A. Zeeb 		tp->ts_recent_age = tcp_ts_getticks();
2399a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
2400df8bae1dSRodney W. Grimes 	}
2401df8bae1dSRodney W. Grimes 
2402df8bae1dSRodney W. Grimes 	/*
2403a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
2404a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
2405a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
2406a0292f23SGarrett Wollman 	 */
2407fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) == 0) {
2408a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
2409281a0fd4SPatrick Kelsey 		    (tp->t_flags & TF_NEEDSYN)) {
2410281a0fd4SPatrick Kelsey 			if (tp->t_state == TCPS_SYN_RECEIVED &&
241168bd7ed1SJonathan T. Looney 			    IS_FASTOPEN(tp->t_flags)) {
2412281a0fd4SPatrick Kelsey 				tp->snd_wnd = tiwin;
2413281a0fd4SPatrick Kelsey 				cc_conn_init(tp);
2414281a0fd4SPatrick Kelsey 			}
2415a0292f23SGarrett Wollman 			goto step6;
2416281a0fd4SPatrick Kelsey 		} else if (tp->t_flags & TF_ACKNOW)
24175e1aa279SDavid Malone 			goto dropafterack;
2418a0292f23SGarrett Wollman 		else
2419a0292f23SGarrett Wollman 			goto drop;
2420a0292f23SGarrett Wollman 	}
2421df8bae1dSRodney W. Grimes 
2422df8bae1dSRodney W. Grimes 	/*
2423df8bae1dSRodney W. Grimes 	 * Ack processing.
2424df8bae1dSRodney W. Grimes 	 */
2425df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2426df8bae1dSRodney W. Grimes 	/*
2427764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
2428764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
2429764d8cefSBill Fenner 	 * The ACK was checked above.
2430df8bae1dSRodney W. Grimes 	 */
2431df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2432a0292f23SGarrett Wollman 
243378b50714SRobert Watson 		TCPSTAT_INC(tcps_connects);
2434493105c2SGleb Smirnoff 		if (tp->t_flags & TF_SONOTCONN) {
2435493105c2SGleb Smirnoff 			/*
2436493105c2SGleb Smirnoff 			 * Usually SYN_RECEIVED had been created from a LISTEN,
2437493105c2SGleb Smirnoff 			 * and solisten_enqueue() has already marked the socket
2438493105c2SGleb Smirnoff 			 * layer as connected.  If it didn't, which can happen
2439493105c2SGleb Smirnoff 			 * only with an accept_filter(9), then the tp is marked
2440493105c2SGleb Smirnoff 			 * with TF_SONOTCONN.  The other reason for this mark
2441493105c2SGleb Smirnoff 			 * to be set is a simultaneous open, a SYN_RECEIVED
2442493105c2SGleb Smirnoff 			 * that had been created from SYN_SENT.
2443493105c2SGleb Smirnoff 			 */
2444493105c2SGleb Smirnoff 			tp->t_flags &= ~TF_SONOTCONN;
2445df8bae1dSRodney W. Grimes 			soisconnected(so);
2446e80062a2SGleb Smirnoff 		}
2447df8bae1dSRodney W. Grimes 		/* Do window scaling? */
2448df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2449df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
2450df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
2451df8bae1dSRodney W. Grimes 		}
2452116ef4d6SMichael Tuexen 		tp->snd_wnd = tiwin;
2453a0292f23SGarrett Wollman 		/*
2454a0292f23SGarrett Wollman 		 * Make transitions:
2455a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
2456a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
2457a0292f23SGarrett Wollman 		 */
24589b8b58e0SJonathan Lemon 		tp->t_starttime = ticks;
245918a75309SPatrick Kelsey 		if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) {
2460281a0fd4SPatrick Kelsey 			tcp_fastopen_decrement_counter(tp->t_tfo_pending);
2461281a0fd4SPatrick Kelsey 			tp->t_tfo_pending = NULL;
2462281a0fd4SPatrick Kelsey 		}
24638db239dcSMichael Tuexen 		if (tp->t_flags & TF_NEEDFIN) {
246408af8aacSRandall Stewart 			tp->t_acktime = ticks;
24658db239dcSMichael Tuexen 			tcp_state_change(tp, TCPS_FIN_WAIT_1);
24668db239dcSMichael Tuexen 			tp->t_flags &= ~TF_NEEDFIN;
24678db239dcSMichael Tuexen 		} else {
24688db239dcSMichael Tuexen 			tcp_state_change(tp, TCPS_ESTABLISHED);
24698db239dcSMichael Tuexen 			TCP_PROBE5(accept__established, NULL, tp,
24708db239dcSMichael Tuexen 			    m, tp, th);
2471281a0fd4SPatrick Kelsey 			/*
2472281a0fd4SPatrick Kelsey 			 * TFO connections call cc_conn_init() during SYN
2473281a0fd4SPatrick Kelsey 			 * processing.  Calling it again here for such
2474281a0fd4SPatrick Kelsey 			 * connections is not harmless as it would undo the
2475281a0fd4SPatrick Kelsey 			 * snd_cwnd reduction that occurs when a TFO SYN|ACK
2476281a0fd4SPatrick Kelsey 			 * is retransmitted.
2477281a0fd4SPatrick Kelsey 			 */
247868bd7ed1SJonathan T. Looney 			if (!IS_FASTOPEN(tp->t_flags))
2479dbc42409SLawrence Stewart 				cc_conn_init(tp);
24809077f387SGleb Smirnoff 			tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp));
24817ff19458SPaul Traina 		}
2482a0292f23SGarrett Wollman 		/*
2483b2ade6b1SRichard Scheffenegger 		 * Account for the ACK of our SYN prior to
2484b2ade6b1SRichard Scheffenegger 		 * regular ACK processing below, except for
2485b2ade6b1SRichard Scheffenegger 		 * simultaneous SYN, which is handled later.
2486b2ade6b1SRichard Scheffenegger 		 */
2487b2ade6b1SRichard Scheffenegger 		if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN))
2488b2ade6b1SRichard Scheffenegger 			incforsyn = 1;
2489b2ade6b1SRichard Scheffenegger 		/*
2490a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
2491a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
2492a0292f23SGarrett Wollman 		 */
2493032bf749SRichard Scheffenegger 		if (tlen == 0 && (thflags & TH_FIN) == 0) {
2494c28440dbSRandall Stewart 			(void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0,
2495a0292f23SGarrett Wollman 			    (struct mbuf *)0);
2496c348e880SGleb Smirnoff 			tcp_handle_wakeup(tp);
2497032bf749SRichard Scheffenegger 		}
249860ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq - 1;
249993b0017fSPhilippe Charnier 		/* FALLTHROUGH */
2500df8bae1dSRodney W. Grimes 
2501df8bae1dSRodney W. Grimes 	/*
2502df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
2503df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
2504fb59c426SYoshinobu Inoue 	 *	tp->snd_una < th->th_ack <= tp->snd_max
2505fb59c426SYoshinobu Inoue 	 * then advance tp->snd_una to th->th_ack and drop
2506df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
2507df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
2508df8bae1dSRodney W. Grimes 	 */
2509df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
2510df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
2511df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
2512df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
2513df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
2514df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
25155a53ca16SPaul Saab 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
251678b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvacktoomuch);
25175a53ca16SPaul Saab 			goto dropafterack;
25185a53ca16SPaul Saab 		}
2519c21b7b55SRichard Scheffenegger 		if (tcp_is_sack_recovery(tp, &to)) {
25200471a8c7SRichard Scheffenegger 			if (((sack_changed = tcp_sack_doack(tp, &to, th->th_ack)) != 0) &&
25210471a8c7SRichard Scheffenegger 			    (tp->t_flags & TF_LRD)) {
25220471a8c7SRichard Scheffenegger 				tcp_sack_lost_retransmission(tp, th);
25230471a8c7SRichard Scheffenegger 			}
25240471a8c7SRichard Scheffenegger 		} else
252512eeb81fSHiren Panchasara 			/*
252612eeb81fSHiren Panchasara 			 * Reset the value so that previous (valid) value
252712eeb81fSHiren Panchasara 			 * from the last ack with SACK doesn't get used.
252812eeb81fSHiren Panchasara 			 */
252912eeb81fSHiren Panchasara 			tp->sackhint.sacked_bytes = 0;
253039bc9de5SLawrence Stewart 
2531bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
253239bc9de5SLawrence Stewart 		/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
253339bc9de5SLawrence Stewart 		hhook_run_tcp_est_in(tp, th, &to);
2534bd79708dSJonathan T. Looney #endif
253539bc9de5SLawrence Stewart 
2536fb59c426SYoshinobu Inoue 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
25370c39d38dSGleb Smirnoff 			maxseg = tcp_maxseg(tp);
2538021eaf79SHiren Panchasara 			if (tlen == 0 &&
2539021eaf79SHiren Panchasara 			    (tiwin == tp->snd_wnd ||
2540021eaf79SHiren Panchasara 			    (tp->t_flags & TF_SACK_PERMIT))) {
254162b90589SPeter Wemm 				/*
254262b90589SPeter Wemm 				 * If this is the first time we've seen a
254362b90589SPeter Wemm 				 * FIN from the remote, this is not a
254462b90589SPeter Wemm 				 * duplicate and it needs to be processed
254562b90589SPeter Wemm 				 * normally.  This happens during a
254662b90589SPeter Wemm 				 * simultaneous close.
254762b90589SPeter Wemm 				 */
254862b90589SPeter Wemm 				if ((thflags & TH_FIN) &&
254962b90589SPeter Wemm 				    (TCPS_HAVERCVDFIN(tp->t_state) == 0)) {
255062b90589SPeter Wemm 					tp->t_dupacks = 0;
255162b90589SPeter Wemm 					break;
255262b90589SPeter Wemm 				}
255378b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvdupack);
2554df8bae1dSRodney W. Grimes 				/*
2555df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
2556df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
2557df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
25585f30ec9bSEitan Adler 				 * change and FIN isn't set),
25595f30ec9bSEitan Adler 				 * the ack is the biggest we've
2560df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
2561a4641f4eSPedro F. Giffuni 				 * threshold of them, assume a packet
2562df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
2563df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
2564df8bae1dSRodney W. Grimes 				 * window so we send only this one
2565df8bae1dSRodney W. Grimes 				 * packet.
2566df8bae1dSRodney W. Grimes 				 *
2567df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
2568df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
2569df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
2570df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
2571df8bae1dSRodney W. Grimes 				 * the new ssthresh).
2572df8bae1dSRodney W. Grimes 				 *
2573df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
2574df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
2575df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
2576df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
2577df8bae1dSRodney W. Grimes 				 * network.
2578f2512ba1SRui Paulo 				 *
2579f2512ba1SRui Paulo 				 * When using TCP ECN, notify the peer that
2580f2512ba1SRui Paulo 				 * we reduced the cwnd.
2581df8bae1dSRodney W. Grimes 				 */
2582021eaf79SHiren Panchasara 				/*
2583021eaf79SHiren Panchasara 				 * Following 2 kinds of acks should not affect
2584021eaf79SHiren Panchasara 				 * dupack counting:
2585021eaf79SHiren Panchasara 				 * 1) Old acks
2586021eaf79SHiren Panchasara 				 * 2) Acks with SACK but without any new SACK
2587021eaf79SHiren Panchasara 				 * information in them. These could result from
2588021eaf79SHiren Panchasara 				 * any anomaly in the network like a switch
2589021eaf79SHiren Panchasara 				 * duplicating packets or a possible DoS attack.
2590021eaf79SHiren Panchasara 				 */
2591021eaf79SHiren Panchasara 				if (th->th_ack != tp->snd_una ||
2592c21b7b55SRichard Scheffenegger 				    (tcp_is_sack_recovery(tp, &to) &&
2593021eaf79SHiren Panchasara 				    !sack_changed))
2594021eaf79SHiren Panchasara 					break;
2595021eaf79SHiren Panchasara 				else if (!tcp_timer_active(tp, TT_REXMT))
2596df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
2597cb942153SJeffrey Hsu 				else if (++tp->t_dupacks > tcprexmtthresh ||
2598dbc42409SLawrence Stewart 				     IN_FASTRECOVERY(tp->t_flags)) {
25994b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
26004b7b743cSLawrence Stewart 					    CC_DUPACK);
26010e1d7c25SRichard Scheffenegger 					if (V_tcp_do_prr &&
260274d7fc87SRichard Scheffenegger 					    IN_FASTRECOVERY(tp->t_flags)) {
260390cca08eSRichard Scheffenegger 						tcp_do_prr_ack(tp, th, &to);
2604c21b7b55SRichard Scheffenegger 					} else if (tcp_is_sack_recovery(tp, &to) &&
2605dbc42409SLawrence Stewart 					    IN_FASTRECOVERY(tp->t_flags)) {
2606808f11b7SPaul Saab 						int awnd;
260725e6f9edSPaul Saab 
260825e6f9edSPaul Saab 						/*
260925e6f9edSPaul Saab 						 * Compute the amount of data in flight first.
26104c3972f0SHiren Panchasara 						 * We can inject new data into the pipe iff
261125e6f9edSPaul Saab 						 * we have less than 1/2 the original window's
261225e6f9edSPaul Saab 						 * worth of data in flight.
261325e6f9edSPaul Saab 						 */
2614d1de2b05SRichard Scheffenegger 						if (V_tcp_do_newsack)
261512eeb81fSHiren Panchasara 							awnd = tcp_compute_pipe(tp);
261612eeb81fSHiren Panchasara 						else
2617808f11b7SPaul Saab 							awnd = (tp->snd_nxt - tp->snd_fack) +
26180077b016SPaul Saab 								tp->sackhint.sack_bytes_rexmit;
261912eeb81fSHiren Panchasara 
2620808f11b7SPaul Saab 						if (awnd < tp->snd_ssthresh) {
26210c39d38dSGleb Smirnoff 							tp->snd_cwnd += maxseg;
262225e6f9edSPaul Saab 							if (tp->snd_cwnd > tp->snd_ssthresh)
262325e6f9edSPaul Saab 								tp->snd_cwnd = tp->snd_ssthresh;
262425e6f9edSPaul Saab 						}
262525e6f9edSPaul Saab 					} else
26260c39d38dSGleb Smirnoff 						tp->snd_cwnd += maxseg;
262740fa3e40SGleb Smirnoff 					(void) tcp_output(tp);
262846f58482SJonathan Lemon 					goto drop;
26293c40e1d5SRichard Scheffenegger 				} else if (tp->t_dupacks == tcprexmtthresh ||
26303c40e1d5SRichard Scheffenegger 					    (tp->t_flags & TF_SACK_PERMIT &&
2631d1de2b05SRichard Scheffenegger 					     V_tcp_do_newsack &&
26323c40e1d5SRichard Scheffenegger 					     tp->sackhint.sacked_bytes >
26333c40e1d5SRichard Scheffenegger 					     (tcprexmtthresh - 1) * maxseg)) {
26343c40e1d5SRichard Scheffenegger enter_recovery:
26353c40e1d5SRichard Scheffenegger 					/*
26363c40e1d5SRichard Scheffenegger 					 * Above is the RFC6675 trigger condition of
26373c40e1d5SRichard Scheffenegger 					 * more than (dupthresh-1)*maxseg sacked data.
26383c40e1d5SRichard Scheffenegger 					 * If the count of holes in the
26393c40e1d5SRichard Scheffenegger 					 * scoreboard is >= dupthresh, we could
26403c40e1d5SRichard Scheffenegger 					 * also enter loss recovery, but don't
26413c40e1d5SRichard Scheffenegger 					 * have that value readily available.
26423c40e1d5SRichard Scheffenegger 					 */
26433c40e1d5SRichard Scheffenegger 					tp->t_dupacks = tcprexmtthresh;
2644cb942153SJeffrey Hsu 					tcp_seq onxt = tp->snd_nxt;
2645a0445c2eSJayanth Vijayaraghavan 
2646a0445c2eSJayanth Vijayaraghavan 					/*
26470e1d7c25SRichard Scheffenegger 					 * If we're doing sack, or prr, check
26480e1d7c25SRichard Scheffenegger 					 * to see if we're already in sack
2649a0445c2eSJayanth Vijayaraghavan 					 * recovery. If we're not doing sack,
2650a0445c2eSJayanth Vijayaraghavan 					 * check to see if we're in newreno
2651a0445c2eSJayanth Vijayaraghavan 					 * recovery.
2652a0445c2eSJayanth Vijayaraghavan 					 */
26530e1d7c25SRichard Scheffenegger 					if (V_tcp_do_prr ||
26540e1d7c25SRichard Scheffenegger 					    (tp->t_flags & TF_SACK_PERMIT)) {
2655dbc42409SLawrence Stewart 						if (IN_FASTRECOVERY(tp->t_flags)) {
2656a0445c2eSJayanth Vijayaraghavan 							tp->t_dupacks = 0;
2657a0445c2eSJayanth Vijayaraghavan 							break;
2658a0445c2eSJayanth Vijayaraghavan 						}
2659dbc42409SLawrence Stewart 					} else {
2660a0445c2eSJayanth Vijayaraghavan 						if (SEQ_LEQ(th->th_ack,
26619d11646dSJeffrey Hsu 						    tp->snd_recover)) {
2662cb942153SJeffrey Hsu 							tp->t_dupacks = 0;
2663cb942153SJeffrey Hsu 							break;
266446f58482SJonathan Lemon 						}
2665a0445c2eSJayanth Vijayaraghavan 					}
2666dbc42409SLawrence Stewart 					/* Congestion signal before ack. */
2667dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_NDUPACK);
26684b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
26694b7b743cSLawrence Stewart 					    CC_DUPACK);
2670b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
26719b8b58e0SJonathan Lemon 					tp->t_rtttime = 0;
26720e1d7c25SRichard Scheffenegger 					if (V_tcp_do_prr) {
26730e1d7c25SRichard Scheffenegger 						/*
26740e1d7c25SRichard Scheffenegger 						 * snd_ssthresh is already updated by
26750e1d7c25SRichard Scheffenegger 						 * cc_cong_signal.
26760e1d7c25SRichard Scheffenegger 						 */
2677c21b7b55SRichard Scheffenegger 						if (tcp_is_sack_recovery(tp, &to)) {
2678e9071000SRichard Scheffenegger 							tp->sackhint.prr_delivered =
2679e9071000SRichard Scheffenegger 							    tp->sackhint.sacked_bytes;
268074d7fc87SRichard Scheffenegger 						} else {
268174d7fc87SRichard Scheffenegger 							tp->sackhint.prr_delivered =
268274d7fc87SRichard Scheffenegger 							    imin(tp->snd_max - tp->snd_una,
268374d7fc87SRichard Scheffenegger 							    imin(INT_MAX / 65536,
268474d7fc87SRichard Scheffenegger 								tp->t_dupacks) * maxseg);
268574d7fc87SRichard Scheffenegger 						}
2686bc7ee8e5SRichard Scheffenegger 						tp->sackhint.recover_fs = max(1,
2687bc7ee8e5SRichard Scheffenegger 						    tp->snd_nxt - tp->snd_una);
26880e1d7c25SRichard Scheffenegger 					}
2689c21b7b55SRichard Scheffenegger 					if (tcp_is_sack_recovery(tp, &to)) {
269078b50714SRobert Watson 						TCPSTAT_INC(
269178b50714SRobert Watson 						    tcps_sack_recovery_episode);
2692a3574665SMichael Tuexen 						tp->snd_recover = tp->snd_nxt;
26930c39d38dSGleb Smirnoff 						tp->snd_cwnd = maxseg;
269440fa3e40SGleb Smirnoff 						(void) tcp_output(tp);
26953c40e1d5SRichard Scheffenegger 						if (SEQ_GT(th->th_ack, tp->snd_una))
26963c40e1d5SRichard Scheffenegger 							goto resume_partialack;
26976d90faf3SPaul Saab 						goto drop;
26986d90faf3SPaul Saab 					}
2699fb59c426SYoshinobu Inoue 					tp->snd_nxt = th->th_ack;
27000c39d38dSGleb Smirnoff 					tp->snd_cwnd = maxseg;
270140fa3e40SGleb Smirnoff 					(void) tcp_output(tp);
270248d2549cSJeffrey Hsu 					KASSERT(tp->snd_limited <= 2,
2703302ce8d6SAndre Oppermann 					    ("%s: tp->snd_limited too big",
2704302ce8d6SAndre Oppermann 					    __func__));
2705df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
27060c39d38dSGleb Smirnoff 					     maxseg *
270748d2549cSJeffrey Hsu 					     (tp->t_dupacks - tp->snd_limited);
2708df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
2709df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
2710df8bae1dSRodney W. Grimes 					goto drop;
2711603724d3SBjoern A. Zeeb 				} else if (V_tcp_do_rfc3042) {
271262d4443fSHiren Panchasara 					/*
271362d4443fSHiren Panchasara 					 * Process first and second duplicate
271462d4443fSHiren Panchasara 					 * ACKs. Each indicates a segment
271562d4443fSHiren Panchasara 					 * leaving the network, creating room
271662d4443fSHiren Panchasara 					 * for more. Make sure we can send a
271762d4443fSHiren Panchasara 					 * packet on reception of each duplicate
271862d4443fSHiren Panchasara 					 * ACK by increasing snd_cwnd by one
271962d4443fSHiren Panchasara 					 * segment. Restore the original
272062d4443fSHiren Panchasara 					 * snd_cwnd after packet transmission.
272162d4443fSHiren Panchasara 					 */
27224b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
27234b7b743cSLawrence Stewart 					    CC_DUPACK);
27243ac12506SJonathan T. Looney 					uint32_t oldcwnd = tp->snd_cwnd;
272548d2549cSJeffrey Hsu 					tcp_seq oldsndmax = tp->snd_max;
272648d2549cSJeffrey Hsu 					u_int sent;
27275628dd08SAndre Oppermann 					int avail;
272889c02376SJeffrey Hsu 
2729582a954bSJeffrey Hsu 					KASSERT(tp->t_dupacks == 1 ||
2730582a954bSJeffrey Hsu 					    tp->t_dupacks == 2,
2731302ce8d6SAndre Oppermann 					    ("%s: dupacks not 1 or 2",
2732302ce8d6SAndre Oppermann 					    __func__));
273361a36e3dSJeffrey Hsu 					if (tp->t_dupacks == 1)
273448d2549cSJeffrey Hsu 						tp->snd_limited = 0;
273561a36e3dSJeffrey Hsu 					tp->snd_cwnd =
273661a36e3dSJeffrey Hsu 					    (tp->snd_nxt - tp->snd_una) +
273761a36e3dSJeffrey Hsu 					    (tp->t_dupacks - tp->snd_limited) *
27380c39d38dSGleb Smirnoff 					    maxseg;
27395628dd08SAndre Oppermann 					/*
27405628dd08SAndre Oppermann 					 * Only call tcp_output when there
27415ae83e0dSMichael Tuexen 					 * is new data available to be sent
27425ae83e0dSMichael Tuexen 					 * or we need to send an ACK.
27435628dd08SAndre Oppermann 					 */
27445628dd08SAndre Oppermann 					SOCKBUF_LOCK(&so->so_snd);
2745cfa6009eSGleb Smirnoff 					avail = sbavail(&so->so_snd) -
27465628dd08SAndre Oppermann 					    (tp->snd_nxt - tp->snd_una);
27475628dd08SAndre Oppermann 					SOCKBUF_UNLOCK(&so->so_snd);
27485ae83e0dSMichael Tuexen 					if (avail > 0 || tp->t_flags & TF_ACKNOW)
274940fa3e40SGleb Smirnoff 						(void) tcp_output(tp);
275048d2549cSJeffrey Hsu 					sent = tp->snd_max - oldsndmax;
27510c39d38dSGleb Smirnoff 					if (sent > maxseg) {
275289c02376SJeffrey Hsu 						KASSERT((tp->t_dupacks == 2 &&
275389c02376SJeffrey Hsu 						    tp->snd_limited == 0) ||
27540c39d38dSGleb Smirnoff 						   (sent == maxseg + 1 &&
275589c02376SJeffrey Hsu 						    tp->t_flags & TF_SENTFIN),
2756302ce8d6SAndre Oppermann 						    ("%s: sent too much",
2757302ce8d6SAndre Oppermann 						    __func__));
275848d2549cSJeffrey Hsu 						tp->snd_limited = 2;
275948d2549cSJeffrey Hsu 					} else if (sent > 0)
276048d2549cSJeffrey Hsu 						++tp->snd_limited;
2761582a954bSJeffrey Hsu 					tp->snd_cwnd = oldcwnd;
2762582a954bSJeffrey Hsu 					goto drop;
2763df8bae1dSRodney W. Grimes 				}
2764021eaf79SHiren Panchasara 			}
2765df8bae1dSRodney W. Grimes 			break;
2766021eaf79SHiren Panchasara 		} else {
2767021eaf79SHiren Panchasara 			/*
2768021eaf79SHiren Panchasara 			 * This ack is advancing the left edge, reset the
2769021eaf79SHiren Panchasara 			 * counter.
2770021eaf79SHiren Panchasara 			 */
2771021eaf79SHiren Panchasara 			tp->t_dupacks = 0;
2772021eaf79SHiren Panchasara 			/*
2773021eaf79SHiren Panchasara 			 * If this ack also has new SACK info, increment the
2774f359d6ebSRichard Scheffenegger 			 * counter as per rfc6675. The variable
2775f359d6ebSRichard Scheffenegger 			 * sack_changed tracks all changes to the SACK
2776f359d6ebSRichard Scheffenegger 			 * scoreboard, including when partial ACKs without
2777f359d6ebSRichard Scheffenegger 			 * SACK options are received, and clear the scoreboard
2778f359d6ebSRichard Scheffenegger 			 * from the left side. Such partial ACKs should not be
2779f359d6ebSRichard Scheffenegger 			 * counted as dupacks here.
2780021eaf79SHiren Panchasara 			 */
2781c21b7b55SRichard Scheffenegger 			if (tcp_is_sack_recovery(tp, &to) &&
27823c40e1d5SRichard Scheffenegger 			    sack_changed) {
2783021eaf79SHiren Panchasara 				tp->t_dupacks++;
27843c40e1d5SRichard Scheffenegger 				/* limit overhead by setting maxseg last */
27853c40e1d5SRichard Scheffenegger 				if (!IN_FASTRECOVERY(tp->t_flags) &&
27863c40e1d5SRichard Scheffenegger 				    (tp->sackhint.sacked_bytes >
27873c40e1d5SRichard Scheffenegger 				    ((tcprexmtthresh - 1) *
27883c40e1d5SRichard Scheffenegger 				    (maxseg = tcp_maxseg(tp))))) {
27893c40e1d5SRichard Scheffenegger 					goto enter_recovery;
27903c40e1d5SRichard Scheffenegger 				}
27913c40e1d5SRichard Scheffenegger 			}
2792df8bae1dSRodney W. Grimes 		}
2793cb942153SJeffrey Hsu 
27943c40e1d5SRichard Scheffenegger resume_partialack:
2795302ce8d6SAndre Oppermann 		KASSERT(SEQ_GT(th->th_ack, tp->snd_una),
2796302ce8d6SAndre Oppermann 		    ("%s: th_ack <= snd_una", __func__));
2797cb942153SJeffrey Hsu 
2798df8bae1dSRodney W. Grimes 		/*
2799df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
2800df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
2801df8bae1dSRodney W. Grimes 		 */
2802dbc42409SLawrence Stewart 		if (IN_FASTRECOVERY(tp->t_flags)) {
2803cb942153SJeffrey Hsu 			if (SEQ_LT(th->th_ack, tp->snd_recover)) {
28043529149eSAndre Oppermann 				if (tp->t_flags & TF_SACK_PERMIT)
2805eb3a59a8SRichard Scheffenegger 					if (V_tcp_do_prr && to.to_flags & TOF_SACK) {
2806eb3a59a8SRichard Scheffenegger 						tcp_timer_activate(tp, TT_REXMT, 0);
2807eb3a59a8SRichard Scheffenegger 						tp->t_rtttime = 0;
280890cca08eSRichard Scheffenegger 						tcp_do_prr_ack(tp, th, &to);
2809eb3a59a8SRichard Scheffenegger 						tp->t_flags |= TF_ACKNOW;
2810eb3a59a8SRichard Scheffenegger 						(void) tcp_output(tp);
2811eb3a59a8SRichard Scheffenegger 					} else
28126d90faf3SPaul Saab 						tcp_sack_partialack(tp, th);
28136d90faf3SPaul Saab 				else
2814c068736aSJeffrey Hsu 					tcp_newreno_partial_ack(tp, th);
2815dbc42409SLawrence Stewart 			} else
2816dbc42409SLawrence Stewart 				cc_post_recovery(tp, th);
2817b9f803b7SRichard Scheffenegger 		} else if (IN_CONGRECOVERY(tp->t_flags)) {
2818b9f803b7SRichard Scheffenegger 			if (SEQ_LT(th->th_ack, tp->snd_recover)) {
2819b9f803b7SRichard Scheffenegger 				if (V_tcp_do_prr) {
2820b9f803b7SRichard Scheffenegger 					tp->sackhint.delivered_data = BYTES_THIS_ACK(tp, th);
2821b9f803b7SRichard Scheffenegger 					tp->snd_fack = th->th_ack;
282290cca08eSRichard Scheffenegger 					tcp_do_prr_ack(tp, th, &to);
2823b9f803b7SRichard Scheffenegger 					(void) tcp_output(tp);
2824b9f803b7SRichard Scheffenegger 				}
2825b9f803b7SRichard Scheffenegger 			} else
2826b9f803b7SRichard Scheffenegger 				cc_post_recovery(tp, th);
282746f58482SJonathan Lemon 		}
2828a0292f23SGarrett Wollman 		/*
2829a0292f23SGarrett Wollman 		 * If we reach this point, ACK is not a duplicate,
2830a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
2831a0292f23SGarrett Wollman 		 */
2832a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
2833a0292f23SGarrett Wollman 			/*
2834a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
2835a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
283607e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
283707e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
283807e43e10SAndras Olah 			 * we can do window scaling.
2839a0292f23SGarrett Wollman 			 */
2840a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
2841a0292f23SGarrett Wollman 			tp->snd_una++;
284207e43e10SAndras Olah 			/* Do window scaling? */
284307e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
284407e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
284507e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2846464fcfbcSAndre Oppermann 				/* Send window already scaled. */
284707e43e10SAndras Olah 			}
2848a0292f23SGarrett Wollman 		}
2849a0292f23SGarrett Wollman 
2850a0292f23SGarrett Wollman process_ACK:
28518501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
28527cfc6904SRobert Watson 
2853b2ade6b1SRichard Scheffenegger 		/*
2854b2ade6b1SRichard Scheffenegger 		 * Adjust for the SYN bit in sequence space,
2855b2ade6b1SRichard Scheffenegger 		 * but don't account for it in cwnd calculations.
2856b2ade6b1SRichard Scheffenegger 		 * This is for the SYN_RECEIVED, non-simultaneous
2857b2ade6b1SRichard Scheffenegger 		 * SYN case. SYN_SENT and simultaneous SYN are
2858b2ade6b1SRichard Scheffenegger 		 * treated elsewhere.
2859b2ade6b1SRichard Scheffenegger 		 */
2860b2ade6b1SRichard Scheffenegger 		if (incforsyn)
2861b2ade6b1SRichard Scheffenegger 			tp->snd_una++;
2862dbc42409SLawrence Stewart 		acked = BYTES_THIS_ACK(tp, th);
2863b8c2cd15SJonathan T. Looney 		KASSERT(acked >= 0, ("%s: acked unexepectedly negative "
2864b8c2cd15SJonathan T. Looney 		    "(tp->snd_una=%u, th->th_ack=%u, tp=%p, m=%p)", __func__,
2865b8c2cd15SJonathan T. Looney 		    tp->snd_una, th->th_ack, tp, m));
28664b7b743cSLawrence Stewart 		TCPSTAT_ADD(tcps_rcvackpack, nsegs);
286778b50714SRobert Watson 		TCPSTAT_ADD(tcps_rcvackbyte, acked);
2868df8bae1dSRodney W. Grimes 
2869df8bae1dSRodney W. Grimes 		/*
28709b8b58e0SJonathan Lemon 		 * If we just performed our first retransmit, and the ACK
28719b8b58e0SJonathan Lemon 		 * arrives within our recovery window, then it was a mistake
28729b8b58e0SJonathan Lemon 		 * to do the retransmit in the first place.  Recover our
28739b8b58e0SJonathan Lemon 		 * original cwnd and ssthresh, and proceed to transmit where
28749b8b58e0SJonathan Lemon 		 * we left off.
28759b8b58e0SJonathan Lemon 		 */
287610d20c84SMatt Macy 		if (tp->t_rxtshift == 1 &&
287710d20c84SMatt Macy 		    tp->t_flags & TF_PREVVALID &&
28784531b345SRichard Scheffenegger 		    tp->t_badrxtwin != 0 &&
28794531b345SRichard Scheffenegger 		    to.to_flags & TOF_TS &&
28804531b345SRichard Scheffenegger 		    to.to_tsecr != 0 &&
28814531b345SRichard Scheffenegger 		    TSTMP_LT(to.to_tsecr, tp->t_badrxtwin))
2882dbc42409SLawrence Stewart 			cc_cong_signal(tp, th, CC_RTO_ERR);
28839b8b58e0SJonathan Lemon 
28849b8b58e0SJonathan Lemon 		/*
2885df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
2886df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
2887df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
2888df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
2889df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
2890df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
2891df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
2892fa55172bSMatthew Dillon 		 *
2893fa55172bSMatthew Dillon 		 * Some boxes send broken timestamp replies
2894fa55172bSMatthew Dillon 		 * during the SYN+ACK phase, ignore
2895fa55172bSMatthew Dillon 		 * timestamps of 0 or we could calculate a
2896fa55172bSMatthew Dillon 		 * huge RTT and blow up the retransmit timer.
2897df8bae1dSRodney W. Grimes 		 */
2898d8951c8aSBjoern A. Zeeb 		if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) {
28993ac12506SJonathan T. Looney 			uint32_t t;
2900d8951c8aSBjoern A. Zeeb 
2901d8951c8aSBjoern A. Zeeb 			t = tcp_ts_getticks() - to.to_tsecr;
2902d8951c8aSBjoern A. Zeeb 			if (!tp->t_rttlow || tp->t_rttlow > t)
2903d8951c8aSBjoern A. Zeeb 				tp->t_rttlow = t;
2904d8951c8aSBjoern A. Zeeb 			tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1);
2905fa55172bSMatthew Dillon 		} else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
2906eaf80179SAndre Oppermann 			if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime)
2907eaf80179SAndre Oppermann 				tp->t_rttlow = ticks - tp->t_rtttime;
29089b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
2909fa55172bSMatthew Dillon 		}
2910df8bae1dSRodney W. Grimes 
291108af8aacSRandall Stewart 		SOCKBUF_LOCK(&so->so_snd);
291208af8aacSRandall Stewart 		/*
291308af8aacSRandall Stewart 		 * Clear t_acktime if remote side has ACKd all data in the
291408af8aacSRandall Stewart 		 * socket buffer and FIN (if applicable).
291508af8aacSRandall Stewart 		 * Otherwise, update t_acktime if we received a sufficiently
291608af8aacSRandall Stewart 		 * large ACK.
291708af8aacSRandall Stewart 		 */
291808af8aacSRandall Stewart 		if ((tp->t_state <= TCPS_CLOSE_WAIT &&
291908af8aacSRandall Stewart 		    acked == sbavail(&so->so_snd)) ||
292008af8aacSRandall Stewart 		    acked > sbavail(&so->so_snd))
292108af8aacSRandall Stewart 			tp->t_acktime = 0;
292208af8aacSRandall Stewart 		else if (acked > 1)
292308af8aacSRandall Stewart 			tp->t_acktime = ticks;
292408af8aacSRandall Stewart 
2925df8bae1dSRodney W. Grimes 		/*
2926df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
2927df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
2928df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
2929df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
2930df8bae1dSRodney W. Grimes 		 */
2931fb59c426SYoshinobu Inoue 		if (th->th_ack == tp->snd_max) {
2932b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
2933df8bae1dSRodney W. Grimes 			needoutput = 1;
2934b8152ba7SAndre Oppermann 		} else if (!tcp_timer_active(tp, TT_PERSIST))
293508af8aacSRandall Stewart 			tcp_timer_activate(tp, TT_REXMT, TP_RXTCUR(tp));
2936a0292f23SGarrett Wollman 
2937a0292f23SGarrett Wollman 		/*
2938a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
2939a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
2940a0292f23SGarrett Wollman 		 */
294108af8aacSRandall Stewart 		if (acked == 0) {
294208af8aacSRandall Stewart 			SOCKBUF_UNLOCK(&so->so_snd);
2943a0292f23SGarrett Wollman 			goto step6;
294408af8aacSRandall Stewart 		}
2945a0292f23SGarrett Wollman 
2946df8bae1dSRodney W. Grimes 		/*
2947dbc42409SLawrence Stewart 		 * Let the congestion control algorithm update congestion
2948dbc42409SLawrence Stewart 		 * control related information. This typically means increasing
2949dbc42409SLawrence Stewart 		 * the congestion window.
2950df8bae1dSRodney W. Grimes 		 */
29514b7b743cSLawrence Stewart 		cc_ack_received(tp, th, nsegs, CC_ACK);
2952dbc42409SLawrence Stewart 
2953cfa6009eSGleb Smirnoff 		if (acked > sbavail(&so->so_snd)) {
2954b8c2cd15SJonathan T. Looney 			if (tp->snd_wnd >= sbavail(&so->so_snd))
2955cfa6009eSGleb Smirnoff 				tp->snd_wnd -= sbavail(&so->so_snd);
2956b8c2cd15SJonathan T. Looney 			else
2957b8c2cd15SJonathan T. Looney 				tp->snd_wnd = 0;
2958c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd,
2959cfa6009eSGleb Smirnoff 			    (int)sbavail(&so->so_snd));
2960df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
2961df8bae1dSRodney W. Grimes 		} else {
2962c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd, acked);
29633ac12506SJonathan T. Looney 			if (tp->snd_wnd >= (uint32_t) acked)
296460ee3bb2SAndre Oppermann 				tp->snd_wnd -= acked;
2965b8c2cd15SJonathan T. Looney 			else
2966b8c2cd15SJonathan T. Looney 				tp->snd_wnd = 0;
2967df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
2968df8bae1dSRodney W. Grimes 		}
2969032bf749SRichard Scheffenegger 		/* NB: sowwakeup_locked() does an implicit unlock. */
2970032bf749SRichard Scheffenegger 		sowwakeup_locked(so);
2971c11a15bfSGleb Smirnoff 		m_freem(mfree);
2972e8949f74SAndre Oppermann 		/* Detect una wraparound. */
2973dbc42409SLawrence Stewart 		if (!IN_RECOVERY(tp->t_flags) &&
29749d11646dSJeffrey Hsu 		    SEQ_GT(tp->snd_una, tp->snd_recover) &&
29759d11646dSJeffrey Hsu 		    SEQ_LEQ(th->th_ack, tp->snd_recover))
29769d11646dSJeffrey Hsu 			tp->snd_recover = th->th_ack - 1;
2977dbc42409SLawrence Stewart 		/* XXXLAS: Can this be moved up into cc_post_recovery? */
2978dbc42409SLawrence Stewart 		if (IN_RECOVERY(tp->t_flags) &&
297924cb0f22SLawrence Stewart 		    SEQ_GEQ(th->th_ack, tp->snd_recover)) {
2980dbc42409SLawrence Stewart 			EXIT_RECOVERY(tp->t_flags);
298124cb0f22SLawrence Stewart 		}
2982fb59c426SYoshinobu Inoue 		tp->snd_una = th->th_ack;
29833529149eSAndre Oppermann 		if (tp->t_flags & TF_SACK_PERMIT) {
29846d90faf3SPaul Saab 			if (SEQ_GT(tp->snd_una, tp->snd_recover))
29856d90faf3SPaul Saab 				tp->snd_recover = tp->snd_una;
29866d90faf3SPaul Saab 		}
2987df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2988df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
2989df8bae1dSRodney W. Grimes 
2990df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2991df8bae1dSRodney W. Grimes 		/*
2992df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
2993df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
2994df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
2995df8bae1dSRodney W. Grimes 		 */
2996df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2997df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2998df8bae1dSRodney W. Grimes 				/*
2999df8bae1dSRodney W. Grimes 				 * If we can't receive any more
3000df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
3001df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
3002df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
3003df8bae1dSRodney W. Grimes 				 * we'll hang forever.
3004340c35deSJonathan Lemon 				 */
3005c0b99ffaSRobert Watson 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
300603e49181SSeigo Tanimura 					soisdisconnected(so);
30079077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
30089077f387SGleb Smirnoff 					    (tcp_fast_finwait2_recycle ?
30099077f387SGleb Smirnoff 					    tcp_finwait2_timeout :
30109077f387SGleb Smirnoff 					    TP_MAXIDLE(tp)));
30114cc20ab1SSeigo Tanimura 				}
301257f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_2);
3013df8bae1dSRodney W. Grimes 			}
3014df8bae1dSRodney W. Grimes 			break;
3015df8bae1dSRodney W. Grimes 
3016df8bae1dSRodney W. Grimes 		/*
3017df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
3018df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
3019df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
3020df8bae1dSRodney W. Grimes 		 * the segment.
3021df8bae1dSRodney W. Grimes 		 */
3022df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
3023df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
302411a20fb8SJeffrey Hsu 				tcp_twstart(tp);
3025340c35deSJonathan Lemon 				m_freem(m);
3026679d9708SAndre Oppermann 				return;
3027df8bae1dSRodney W. Grimes 			}
3028df8bae1dSRodney W. Grimes 			break;
3029df8bae1dSRodney W. Grimes 
3030df8bae1dSRodney W. Grimes 		/*
3031df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
3032df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
3033df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
3034df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
3035df8bae1dSRodney W. Grimes 		 */
3036df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
3037df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
3038df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
3039df8bae1dSRodney W. Grimes 				goto drop;
3040df8bae1dSRodney W. Grimes 			}
3041df8bae1dSRodney W. Grimes 			break;
3042df8bae1dSRodney W. Grimes 		}
3043df8bae1dSRodney W. Grimes 	}
3044df8bae1dSRodney W. Grimes 
3045df8bae1dSRodney W. Grimes step6:
30468501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
30477cfc6904SRobert Watson 
3048df8bae1dSRodney W. Grimes 	/*
304960ee3bb2SAndre Oppermann 	 * Update window information.
305060ee3bb2SAndre Oppermann 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
3051df8bae1dSRodney W. Grimes 	 */
305260ee3bb2SAndre Oppermann 	if ((thflags & TH_ACK) &&
305360ee3bb2SAndre Oppermann 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
305460ee3bb2SAndre Oppermann 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
305560ee3bb2SAndre Oppermann 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
305660ee3bb2SAndre Oppermann 		/* keep track of pure window updates */
305760ee3bb2SAndre Oppermann 		if (tlen == 0 &&
305860ee3bb2SAndre Oppermann 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
305978b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvwinupd);
3060df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
306160ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq;
306260ee3bb2SAndre Oppermann 		tp->snd_wl2 = th->th_ack;
3063df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
3064df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
306560ee3bb2SAndre Oppermann 		needoutput = 1;
3066df8bae1dSRodney W. Grimes 	}
3067df8bae1dSRodney W. Grimes 
3068df8bae1dSRodney W. Grimes 	/*
3069df8bae1dSRodney W. Grimes 	 * Process segments with URG.
3070df8bae1dSRodney W. Grimes 	 */
3071fb59c426SYoshinobu Inoue 	if ((thflags & TH_URG) && th->th_urp &&
3072df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
3073df8bae1dSRodney W. Grimes 		/*
3074df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
3075df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
3076df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
3077df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
3078df8bae1dSRodney W. Grimes 		 */
307918ad5842SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
3080cfa6009eSGleb Smirnoff 		if (th->th_urp + sbavail(&so->so_rcv) > sb_max) {
3081fb59c426SYoshinobu Inoue 			th->th_urp = 0;			/* XXX */
3082fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;		/* XXX */
308318ad5842SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);	/* XXX */
3084df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
3085df8bae1dSRodney W. Grimes 		}
3086df8bae1dSRodney W. Grimes 		/*
3087df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
3088df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
3089df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
3090df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
3091df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
3092df8bae1dSRodney W. Grimes 		 *
3093df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
3094df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
3095df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
3096df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
3097df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
3098df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
3099df8bae1dSRodney W. Grimes 		 */
3100fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
3101fb59c426SYoshinobu Inoue 			tp->rcv_up = th->th_seq + th->th_urp;
3102cfa6009eSGleb Smirnoff 			so->so_oobmark = sbavail(&so->so_rcv) +
3103df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
3104927c5ceaSRobert Watson 			if (so->so_oobmark == 0)
3105c0b99ffaSRobert Watson 				so->so_rcv.sb_state |= SBS_RCVATMARK;
3106df8bae1dSRodney W. Grimes 			sohasoutofband(so);
3107df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
3108df8bae1dSRodney W. Grimes 		}
310918ad5842SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
3110df8bae1dSRodney W. Grimes 		/*
3111df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
3112df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
3113df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
3114df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
3115df8bae1dSRodney W. Grimes 		 */
31163ac12506SJonathan T. Looney 		if (th->th_urp <= (uint32_t)tlen &&
311730613f56SJeffrey Hsu 		    !(so->so_options & SO_OOBINLINE)) {
311830613f56SJeffrey Hsu 			/* hdr drop is delayed */
311930613f56SJeffrey Hsu 			tcp_pulloutofband(so, th, m, drop_hdrlen);
312030613f56SJeffrey Hsu 		}
3121c068736aSJeffrey Hsu 	} else {
3122df8bae1dSRodney W. Grimes 		/*
3123df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
3124df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
3125df8bae1dSRodney W. Grimes 		 * with the receive window.
3126df8bae1dSRodney W. Grimes 		 */
3127df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
3128df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
3129c068736aSJeffrey Hsu 	}
3130df8bae1dSRodney W. Grimes dodata:							/* XXX */
31318501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
31327cfc6904SRobert Watson 
3133df8bae1dSRodney W. Grimes 	/*
3134df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
3135df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
3136df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
3137df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
3138df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
3139df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
3140df8bae1dSRodney W. Grimes 	 */
3141281a0fd4SPatrick Kelsey 	tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
314268bd7ed1SJonathan T. Looney 		   IS_FASTOPEN(tp->t_flags));
3143f1ea4e41SRandall Stewart 	if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) &&
3144df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
314569e03620SPaul Saab 		tcp_seq save_start = th->th_seq;
31465acfd95cSMichael Tuexen 		tcp_seq save_rnxt  = tp->rcv_nxt;
31475acfd95cSMichael Tuexen 		int     save_tlen  = tlen;
3148fb59c426SYoshinobu Inoue 		m_adj(m, drop_hdrlen);	/* delayed header drop */
3149e4b64281SJesper Skriver 		/*
3150c068736aSJeffrey Hsu 		 * Insert segment which includes th into TCP reassembly queue
3151c068736aSJeffrey Hsu 		 * with control block tp.  Set thflags to whether reassembly now
3152c068736aSJeffrey Hsu 		 * includes a segment with FIN.  This handles the common case
3153c068736aSJeffrey Hsu 		 * inline (segment is the next to be received on an established
3154c068736aSJeffrey Hsu 		 * connection, and the queue is empty), avoiding linkage into
3155c068736aSJeffrey Hsu 		 * and removal from the queue and repetition of various
3156c068736aSJeffrey Hsu 		 * conversions.
3157c068736aSJeffrey Hsu 		 * Set DELACK for segments received in order, but ack
3158c068736aSJeffrey Hsu 		 * immediately when segments are out of order (so
3159c068736aSJeffrey Hsu 		 * fast retransmit can work).
3160e4b64281SJesper Skriver 		 */
31614741bfcbSPatrick Kelsey 		if (th->th_seq == tp->rcv_nxt &&
3162c28440dbSRandall Stewart 		    SEGQ_EMPTY(tp) &&
3163281a0fd4SPatrick Kelsey 		    (TCPS_HAVEESTABLISHED(tp->t_state) ||
3164281a0fd4SPatrick Kelsey 		     tfo_syn)) {
3165281a0fd4SPatrick Kelsey 			if (DELAY_ACK(tp, tlen) || tfo_syn)
31663bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
3167e4b64281SJesper Skriver 			else
3168e4b64281SJesper Skriver 				tp->t_flags |= TF_ACKNOW;
3169e4b64281SJesper Skriver 			tp->rcv_nxt += tlen;
3170e854dd38SRandall Stewart 			if (tlen &&
3171e854dd38SRandall Stewart 			    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
3172e854dd38SRandall Stewart 			    (tp->t_fbyte_in == 0)) {
3173e854dd38SRandall Stewart 				tp->t_fbyte_in = ticks;
3174e854dd38SRandall Stewart 				if (tp->t_fbyte_in == 0)
3175e854dd38SRandall Stewart 					tp->t_fbyte_in = 1;
3176e854dd38SRandall Stewart 				if (tp->t_fbyte_out && tp->t_fbyte_in)
3177e854dd38SRandall Stewart 					tp->t_flags2 |= TF2_FBYTES_COMPLETE;
3178e854dd38SRandall Stewart 			}
31791ebf4607SRichard Scheffenegger 			thflags = tcp_get_flags(th) & TH_FIN;
318078b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpack);
318178b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
31821e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
3183c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
3184c1c36a2cSMike Silbersack 				m_freem(m);
3185c1c36a2cSMike Silbersack 			else
3186651e4e6aSGleb Smirnoff 				sbappendstream_locked(&so->so_rcv, m, 0);
31874d0770f1SRichard Scheffenegger 			tp->t_flags |= TF_WAKESOR;
3188e4b64281SJesper Skriver 		} else {
3189e8949f74SAndre Oppermann 			/*
3190e8949f74SAndre Oppermann 			 * XXX: Due to the header drop above "th" is
3191e8949f74SAndre Oppermann 			 * theoretically invalid by now.  Fortunately
3192e8949f74SAndre Oppermann 			 * m_adj() doesn't actually frees any mbufs
3193e8949f74SAndre Oppermann 			 * when trimming from the head.
3194e8949f74SAndre Oppermann 			 */
31955acfd95cSMichael Tuexen 			tcp_seq temp = save_start;
31964747500dSRandall Stewart 
31975acfd95cSMichael Tuexen 			thflags = tcp_reass(tp, th, &temp, &tlen, m);
3198e4b64281SJesper Skriver 			tp->t_flags |= TF_ACKNOW;
3199e4b64281SJesper Skriver 		}
320040f41eceSMichael Tuexen 		if ((tp->t_flags & TF_SACK_PERMIT) &&
320140f41eceSMichael Tuexen 		    (save_tlen > 0) &&
320240f41eceSMichael Tuexen 		    TCPS_HAVEESTABLISHED(tp->t_state)) {
32036d261981SMichael Tuexen 			if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) {
3204b5a154d8SMichael Tuexen 				/*
3205b5a154d8SMichael Tuexen 				 * DSACK actually handled in the fastpath
3206b5a154d8SMichael Tuexen 				 * above.
3207b5a154d8SMichael Tuexen 				 */
3208ecc5b1d1SMichael Tuexen 				tcp_update_sack_list(tp, save_start,
3209ecc5b1d1SMichael Tuexen 				    save_start + save_tlen);
3210ecc5b1d1SMichael Tuexen 			} else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) {
3211ecc5b1d1SMichael Tuexen 				if ((tp->rcv_numsacks >= 1) &&
3212ecc5b1d1SMichael Tuexen 				    (tp->sackblks[0].end == save_start)) {
32136d261981SMichael Tuexen 					/*
32146d261981SMichael Tuexen 					 * Partial overlap, recorded at todrop
32156d261981SMichael Tuexen 					 * above.
32166d261981SMichael Tuexen 					 */
32176d261981SMichael Tuexen 					tcp_update_sack_list(tp,
32186d261981SMichael Tuexen 					    tp->sackblks[0].start,
3219ecc5b1d1SMichael Tuexen 					    tp->sackblks[0].end);
3220ecc5b1d1SMichael Tuexen 				} else {
3221ecc5b1d1SMichael Tuexen 					tcp_update_dsack_list(tp, save_start,
3222ecc5b1d1SMichael Tuexen 					    save_start + save_tlen);
3223ecc5b1d1SMichael Tuexen 				}
32246d261981SMichael Tuexen 			} else if (tlen >= save_tlen) {
3225b5a154d8SMichael Tuexen 				/* Update of sackblks. */
3226ecc5b1d1SMichael Tuexen 				tcp_update_dsack_list(tp, save_start,
3227ecc5b1d1SMichael Tuexen 				    save_start + save_tlen);
3228ecc5b1d1SMichael Tuexen 			} else if (tlen > 0) {
3229ecc5b1d1SMichael Tuexen 				tcp_update_dsack_list(tp, save_start,
3230ecc5b1d1SMichael Tuexen 				    save_start + tlen);
32315acfd95cSMichael Tuexen 			}
32325acfd95cSMichael Tuexen 		}
3233c348e880SGleb Smirnoff 		tcp_handle_wakeup(tp);
3234302ce8d6SAndre Oppermann #if 0
3235df8bae1dSRodney W. Grimes 		/*
3236df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
3237df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
3238df8bae1dSRodney W. Grimes 		 * buffer size.
3239302ce8d6SAndre Oppermann 		 * XXX: Unused.
3240df8bae1dSRodney W. Grimes 		 */
3241f701e30dSJohn Baldwin 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt))
3242df8bae1dSRodney W. Grimes 			len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
3243f701e30dSJohn Baldwin 		else
3244f701e30dSJohn Baldwin 			len = so->so_rcv.sb_hiwat;
3245302ce8d6SAndre Oppermann #endif
3246df8bae1dSRodney W. Grimes 	} else {
3247df8bae1dSRodney W. Grimes 		m_freem(m);
3248fb59c426SYoshinobu Inoue 		thflags &= ~TH_FIN;
3249df8bae1dSRodney W. Grimes 	}
3250df8bae1dSRodney W. Grimes 
3251df8bae1dSRodney W. Grimes 	/*
3252df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
3253df8bae1dSRodney W. Grimes 	 * that the connection is closing.
3254df8bae1dSRodney W. Grimes 	 */
3255fb59c426SYoshinobu Inoue 	if (thflags & TH_FIN) {
3256df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
32574d0770f1SRichard Scheffenegger 			/* The socket upcall is handled by socantrcvmore. */
3258032bf749SRichard Scheffenegger 			socantrcvmore(so);
3259a0292f23SGarrett Wollman 			/*
3260a0292f23SGarrett Wollman 			 * If connection is half-synchronized
3261d3eede9dSAndras Olah 			 * (ie NEEDSYN flag on) then delay ACK,
3262a0292f23SGarrett Wollman 			 * so it may be piggybacked when SYN is sent.
3263a0292f23SGarrett Wollman 			 * Otherwise, since we received a FIN then no
3264a0292f23SGarrett Wollman 			 * more input can be expected, send ACK now.
3265a0292f23SGarrett Wollman 			 */
32663bfd6421SJonathan Lemon 			if (tp->t_flags & TF_NEEDSYN)
32673bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
3268a0292f23SGarrett Wollman 			else
3269df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
3270df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
3271df8bae1dSRodney W. Grimes 		}
3272df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
3273df8bae1dSRodney W. Grimes 		/*
3274df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
3275df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
3276df8bae1dSRodney W. Grimes 		 */
3277df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
32789b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
32799b8b58e0SJonathan Lemon 			/* FALLTHROUGH */
3280df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
328157f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSE_WAIT);
3282df8bae1dSRodney W. Grimes 			break;
3283df8bae1dSRodney W. Grimes 
3284df8bae1dSRodney W. Grimes 		/*
3285df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
3286df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
3287df8bae1dSRodney W. Grimes 		 */
3288df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
328957f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSING);
3290df8bae1dSRodney W. Grimes 			break;
3291df8bae1dSRodney W. Grimes 
3292df8bae1dSRodney W. Grimes 		/*
3293df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
3294df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
3295df8bae1dSRodney W. Grimes 		 * standard timers.
3296df8bae1dSRodney W. Grimes 		 */
3297df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
3298340c35deSJonathan Lemon 			tcp_twstart(tp);
3299679d9708SAndre Oppermann 			return;
3300df8bae1dSRodney W. Grimes 		}
3301df8bae1dSRodney W. Grimes 	}
3302610ee2f9SDavid Greenman #ifdef TCPDEBUG
33034cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
3304fb59c426SYoshinobu Inoue 		tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
3305fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3306610ee2f9SDavid Greenman #endif
33072b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
3308df8bae1dSRodney W. Grimes 
3309df8bae1dSRodney W. Grimes 	/*
3310df8bae1dSRodney W. Grimes 	 * Return any desired output.
3311df8bae1dSRodney W. Grimes 	 */
3312df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
331340fa3e40SGleb Smirnoff 		(void) tcp_output(tp);
33147792ea27SJeffrey Hsu 
3315a14c749fSJonathan Lemon check_delack:
33168501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
3317252ca428SRobert Watson 
3318a14c749fSJonathan Lemon 	if (tp->t_flags & TF_DELACK) {
33193bfd6421SJonathan Lemon 		tp->t_flags &= ~TF_DELACK;
3320b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
33213bfd6421SJonathan Lemon 	}
33228501a69cSRobert Watson 	INP_WUNLOCK(tp->t_inpcb);
3323679d9708SAndre Oppermann 	return;
3324df8bae1dSRodney W. Grimes 
3325df8bae1dSRodney W. Grimes dropafterack:
3326df8bae1dSRodney W. Grimes 	/*
3327df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
3328df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
332980ab7c0eSGarrett Wollman 	 *
333080ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
333180ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
333280ab7c0eSGarrett Wollman 	 * RST have been dropped.
333380ab7c0eSGarrett Wollman 	 *
333480ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
333580ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
333680ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
333780ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
333880ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
333980ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
3340df8bae1dSRodney W. Grimes 	 */
3341fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
3342fb59c426SYoshinobu Inoue 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
3343a57815efSBosko Milekic 	     SEQ_GT(th->th_ack, tp->snd_max)) ) {
3344a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
3345d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
3346a57815efSBosko Milekic 		goto dropwithreset;
3347a57815efSBosko Milekic 	}
3348a0292f23SGarrett Wollman #ifdef TCPDEBUG
33494cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
3350fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
3351fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3352a0292f23SGarrett Wollman #endif
33532b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
3354df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
335540fa3e40SGleb Smirnoff 	(void) tcp_output(tp);
33568501a69cSRobert Watson 	INP_WUNLOCK(tp->t_inpcb);
3357d6915262SRobert Watson 	m_freem(m);
3358679d9708SAndre Oppermann 	return;
3359df8bae1dSRodney W. Grimes 
3360df8bae1dSRodney W. Grimes dropwithreset:
3361a0ca0871SRobert Watson 	if (tp != NULL) {
3362302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
33638501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
3364dd8ac7f9SRobert Watson 	} else
3365a0ca0871SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
3366679d9708SAndre Oppermann 	return;
3367df8bae1dSRodney W. Grimes 
3368df8bae1dSRodney W. Grimes drop:
3369df8bae1dSRodney W. Grimes 	/*
3370df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
3371df8bae1dSRodney W. Grimes 	 */
3372610ee2f9SDavid Greenman #ifdef TCPDEBUG
33731c53f806SRobert Watson 	if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
3374fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
3375fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3376a0292f23SGarrett Wollman #endif
33772b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
33784d0770f1SRichard Scheffenegger 	if (tp != NULL) {
33798501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
33804d0770f1SRichard Scheffenegger 	}
3381d6915262SRobert Watson 	m_freem(m);
3382302ce8d6SAndre Oppermann }
3383302ce8d6SAndre Oppermann 
3384302ce8d6SAndre Oppermann /*
33850ca3f933SAndre Oppermann  * Issue RST and make ACK acceptable to originator of segment.
33860ca3f933SAndre Oppermann  * The mbuf must still include the original packet header.
33870ca3f933SAndre Oppermann  * tp may be NULL.
3388302ce8d6SAndre Oppermann  */
338955bceb1eSRandall Stewart void
3390302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp,
3391302ce8d6SAndre Oppermann     int tlen, int rstreason)
3392302ce8d6SAndre Oppermann {
3393b287c6c7SBjoern A. Zeeb #ifdef INET
3394302ce8d6SAndre Oppermann 	struct ip *ip;
3395b287c6c7SBjoern A. Zeeb #endif
3396302ce8d6SAndre Oppermann #ifdef INET6
3397302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6;
3398302ce8d6SAndre Oppermann #endif
33997a3244ccSRobert Watson 
34007a3244ccSRobert Watson 	if (tp != NULL) {
340108d9c920SGleb Smirnoff 		INP_LOCK_ASSERT(tp->t_inpcb);
34027a3244ccSRobert Watson 	}
34037a3244ccSRobert Watson 
34040ca3f933SAndre Oppermann 	/* Don't bother if destination was broadcast/multicast. */
34051ebf4607SRichard Scheffenegger 	if ((tcp_get_flags(th) & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
3406302ce8d6SAndre Oppermann 		goto drop;
3407302ce8d6SAndre Oppermann #ifdef INET6
3408302ce8d6SAndre Oppermann 	if (mtod(m, struct ip *)->ip_v == 6) {
3409302ce8d6SAndre Oppermann 		ip6 = mtod(m, struct ip6_hdr *);
3410302ce8d6SAndre Oppermann 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
3411302ce8d6SAndre Oppermann 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
3412302ce8d6SAndre Oppermann 			goto drop;
3413302ce8d6SAndre Oppermann 		/* IPv6 anycast check is done at tcp6_input() */
3414b287c6c7SBjoern A. Zeeb 	}
3415302ce8d6SAndre Oppermann #endif
3416b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3417b287c6c7SBjoern A. Zeeb 	else
3418b287c6c7SBjoern A. Zeeb #endif
3419b287c6c7SBjoern A. Zeeb #ifdef INET
3420302ce8d6SAndre Oppermann 	{
3421302ce8d6SAndre Oppermann 		ip = mtod(m, struct ip *);
3422302ce8d6SAndre Oppermann 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
3423302ce8d6SAndre Oppermann 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
3424302ce8d6SAndre Oppermann 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
3425302ce8d6SAndre Oppermann 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
3426302ce8d6SAndre Oppermann 			goto drop;
3427302ce8d6SAndre Oppermann 	}
3428b287c6c7SBjoern A. Zeeb #endif
3429302ce8d6SAndre Oppermann 
3430302ce8d6SAndre Oppermann 	/* Perform bandwidth limiting. */
3431302ce8d6SAndre Oppermann 	if (badport_bandlim(rstreason) < 0)
3432302ce8d6SAndre Oppermann 		goto drop;
3433302ce8d6SAndre Oppermann 
3434302ce8d6SAndre Oppermann 	/* tcp_respond consumes the mbuf chain. */
34351ebf4607SRichard Scheffenegger 	if (tcp_get_flags(th) & TH_ACK) {
3436302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0,
3437302ce8d6SAndre Oppermann 		    th->th_ack, TH_RST);
3438302ce8d6SAndre Oppermann 	} else {
34391ebf4607SRichard Scheffenegger 		if (tcp_get_flags(th) & TH_SYN)
3440302ce8d6SAndre Oppermann 			tlen++;
34411ebf4607SRichard Scheffenegger 		if (tcp_get_flags(th) & TH_FIN)
34424ddd5aadSMichael Tuexen 			tlen++;
3443302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
3444302ce8d6SAndre Oppermann 		    (tcp_seq)0, TH_RST|TH_ACK);
3445302ce8d6SAndre Oppermann 	}
3446302ce8d6SAndre Oppermann 	return;
3447302ce8d6SAndre Oppermann drop:
3448302ce8d6SAndre Oppermann 	m_freem(m);
3449df8bae1dSRodney W. Grimes }
3450df8bae1dSRodney W. Grimes 
3451be2ac88cSJonathan Lemon /*
3452be2ac88cSJonathan Lemon  * Parse TCP options and place in tcpopt.
3453be2ac88cSJonathan Lemon  */
345455bceb1eSRandall Stewart void
3455ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags)
3456df8bae1dSRodney W. Grimes {
3457df8bae1dSRodney W. Grimes 	int opt, optlen;
3458df8bae1dSRodney W. Grimes 
3459be2ac88cSJonathan Lemon 	to->to_flags = 0;
3460df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
3461df8bae1dSRodney W. Grimes 		opt = cp[0];
3462df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
3463df8bae1dSRodney W. Grimes 			break;
3464df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
3465df8bae1dSRodney W. Grimes 			optlen = 1;
3466df8bae1dSRodney W. Grimes 		else {
3467b474779fSJun-ichiro itojun Hagino 			if (cnt < 2)
3468b474779fSJun-ichiro itojun Hagino 				break;
3469df8bae1dSRodney W. Grimes 			optlen = cp[1];
3470b474779fSJun-ichiro itojun Hagino 			if (optlen < 2 || optlen > cnt)
3471df8bae1dSRodney W. Grimes 				break;
3472df8bae1dSRodney W. Grimes 		}
3473df8bae1dSRodney W. Grimes 		switch (opt) {
3474df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
3475df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
3476df8bae1dSRodney W. Grimes 				continue;
3477f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3478df8bae1dSRodney W. Grimes 				continue;
3479be2ac88cSJonathan Lemon 			to->to_flags |= TOF_MSS;
3480be2ac88cSJonathan Lemon 			bcopy((char *)cp + 2,
3481be2ac88cSJonathan Lemon 			    (char *)&to->to_mss, sizeof(to->to_mss));
3482fd8e4ebcSMike Barcroft 			to->to_mss = ntohs(to->to_mss);
3483df8bae1dSRodney W. Grimes 			break;
3484df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
3485df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
3486df8bae1dSRodney W. Grimes 				continue;
3487f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3488df8bae1dSRodney W. Grimes 				continue;
3489be2ac88cSJonathan Lemon 			to->to_flags |= TOF_SCALE;
349002a1a643SAndre Oppermann 			to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT);
3491df8bae1dSRodney W. Grimes 			break;
3492df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
3493df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
3494df8bae1dSRodney W. Grimes 				continue;
3495be2ac88cSJonathan Lemon 			to->to_flags |= TOF_TS;
3496a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
3497a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
3498fd8e4ebcSMike Barcroft 			to->to_tsval = ntohl(to->to_tsval);
3499a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
3500a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
3501fd8e4ebcSMike Barcroft 			to->to_tsecr = ntohl(to->to_tsecr);
3502df8bae1dSRodney W. Grimes 			break;
35031cfd4b53SBruce M Simpson 		case TCPOPT_SIGNATURE:
3504fcf59617SAndrey V. Elsukov 			/*
3505fcf59617SAndrey V. Elsukov 			 * In order to reply to a host which has set the
3506fcf59617SAndrey V. Elsukov 			 * TCP_SIGNATURE option in its initial SYN, we have
3507fcf59617SAndrey V. Elsukov 			 * to record the fact that the option was observed
3508fcf59617SAndrey V. Elsukov 			 * here for the syncache code to perform the correct
3509fcf59617SAndrey V. Elsukov 			 * response.
3510fcf59617SAndrey V. Elsukov 			 */
35111cfd4b53SBruce M Simpson 			if (optlen != TCPOLEN_SIGNATURE)
35121cfd4b53SBruce M Simpson 				continue;
3513df47e437SAndre Oppermann 			to->to_flags |= TOF_SIGNATURE;
3514df47e437SAndre Oppermann 			to->to_signature = cp + 2;
35151cfd4b53SBruce M Simpson 			break;
35166d90faf3SPaul Saab 		case TCPOPT_SACK_PERMITTED:
3517f72167f4SAndre Oppermann 			if (optlen != TCPOLEN_SACK_PERMITTED)
35186d90faf3SPaul Saab 				continue;
3519f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3520f72167f4SAndre Oppermann 				continue;
3521603724d3SBjoern A. Zeeb 			if (!V_tcp_do_sack)
3522f72167f4SAndre Oppermann 				continue;
3523fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACKPERM;
35246d90faf3SPaul Saab 			break;
35256d90faf3SPaul Saab 		case TCPOPT_SACK:
35265a53ca16SPaul Saab 			if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
35276d90faf3SPaul Saab 				continue;
3528b728e902SAndre Oppermann 			if (flags & TO_SYN)
3529b728e902SAndre Oppermann 				continue;
3530fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACK;
35315a53ca16SPaul Saab 			to->to_nsacks = (optlen - 2) / TCPOLEN_SACK;
35325a53ca16SPaul Saab 			to->to_sacks = cp + 2;
353378b50714SRobert Watson 			TCPSTAT_INC(tcps_sack_rcv_blocks);
35346d90faf3SPaul Saab 			break;
3535281a0fd4SPatrick Kelsey 		case TCPOPT_FAST_OPEN:
3536c560df6fSPatrick Kelsey 			/*
3537c560df6fSPatrick Kelsey 			 * Cookie length validation is performed by the
3538c560df6fSPatrick Kelsey 			 * server side cookie checking code or the client
3539c560df6fSPatrick Kelsey 			 * side cookie cache update code.
3540c560df6fSPatrick Kelsey 			 */
3541281a0fd4SPatrick Kelsey 			if (!(flags & TO_SYN))
3542281a0fd4SPatrick Kelsey 				continue;
3543c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
3544c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
3545281a0fd4SPatrick Kelsey 				continue;
3546281a0fd4SPatrick Kelsey 			to->to_flags |= TOF_FASTOPEN;
3547281a0fd4SPatrick Kelsey 			to->to_tfo_len = optlen - 2;
3548281a0fd4SPatrick Kelsey 			to->to_tfo_cookie = to->to_tfo_len ? cp + 2 : NULL;
3549281a0fd4SPatrick Kelsey 			break;
3550be2ac88cSJonathan Lemon 		default:
3551be2ac88cSJonathan Lemon 			continue;
3552df8bae1dSRodney W. Grimes 		}
3553df8bae1dSRodney W. Grimes 	}
3554df8bae1dSRodney W. Grimes }
3555df8bae1dSRodney W. Grimes 
3556df8bae1dSRodney W. Grimes /*
3557df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
3558df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
3559df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
3560df8bae1dSRodney W. Grimes  * sequencing purposes.
3561df8bae1dSRodney W. Grimes  */
356255bceb1eSRandall Stewart void
3563ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m,
3564ad3f9ab3SAndre Oppermann     int off)
3565df8bae1dSRodney W. Grimes {
3566fb59c426SYoshinobu Inoue 	int cnt = off + th->th_urp - 1;
3567df8bae1dSRodney W. Grimes 
3568df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
3569df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
3570df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
3571df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
3572df8bae1dSRodney W. Grimes 
35738501a69cSRobert Watson 			INP_WLOCK_ASSERT(tp->t_inpcb);
35747a3244ccSRobert Watson 
3575df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
3576df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
3577df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
3578df8bae1dSRodney W. Grimes 			m->m_len--;
357969a34685SYoshinobu Inoue 			if (m->m_flags & M_PKTHDR)
358069a34685SYoshinobu Inoue 				m->m_pkthdr.len--;
3581df8bae1dSRodney W. Grimes 			return;
3582df8bae1dSRodney W. Grimes 		}
3583df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
3584df8bae1dSRodney W. Grimes 		m = m->m_next;
35854dfdffe9SAndre Oppermann 		if (m == NULL)
3586df8bae1dSRodney W. Grimes 			break;
3587df8bae1dSRodney W. Grimes 	}
3588df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
3589df8bae1dSRodney W. Grimes }
3590df8bae1dSRodney W. Grimes 
3591df8bae1dSRodney W. Grimes /*
3592df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
3593df8bae1dSRodney W. Grimes  * and update averages and current timeout.
3594df8bae1dSRodney W. Grimes  */
359555bceb1eSRandall Stewart void
3596ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt)
3597df8bae1dSRodney W. Grimes {
3598ad3f9ab3SAndre Oppermann 	int delta;
3599233e8c18SGarrett Wollman 
36008501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
36012be3bf22SRobert Watson 
360278b50714SRobert Watson 	TCPSTAT_INC(tcps_rttupdated);
3603233e8c18SGarrett Wollman 	tp->t_rttupdated++;
3604adc56f5aSEdward Tomasz Napierala #ifdef STATS
3605adc56f5aSEdward Tomasz Napierala 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT,
3606adc56f5aSEdward Tomasz Napierala 	    imax(0, rtt * 1000 / hz));
3607adc56f5aSEdward Tomasz Napierala #endif
36085ede40dcSRyan Stone 	if ((tp->t_srtt != 0) && (tp->t_rxtshift <= TCP_RTT_INVALIDATE)) {
3609233e8c18SGarrett Wollman 		/*
3610233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
3611233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
3612233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
3613233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
3614233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
3615233e8c18SGarrett Wollman 		 */
3616233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
3617233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
3618233e8c18SGarrett Wollman 
3619233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
3620233e8c18SGarrett Wollman 			tp->t_srtt = 1;
3621233e8c18SGarrett Wollman 
3622233e8c18SGarrett Wollman 		/*
3623233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
3624233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
3625233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
3626233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
3627233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
3628233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
3629233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
3630233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
3631233e8c18SGarrett Wollman 		 */
3632233e8c18SGarrett Wollman 		if (delta < 0)
3633233e8c18SGarrett Wollman 			delta = -delta;
3634233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
3635233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
3636233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
36371fcc99b5SMatthew Dillon 		if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
36381fcc99b5SMatthew Dillon 		    tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3639233e8c18SGarrett Wollman 	} else {
3640233e8c18SGarrett Wollman 		/*
3641233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
3642233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
3643233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
3644233e8c18SGarrett Wollman 		 */
3645233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
3646233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
36471fcc99b5SMatthew Dillon 		tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3648233e8c18SGarrett Wollman 	}
36499b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
3650df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
3651df8bae1dSRodney W. Grimes 
3652df8bae1dSRodney W. Grimes 	/*
3653df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
3654df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
3655df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
3656df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
3657df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
3658df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
3659df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
3660df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
3661df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
3662df8bae1dSRodney W. Grimes 	 */
3663233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
36649e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
3665df8bae1dSRodney W. Grimes 
3666df8bae1dSRodney W. Grimes 	/*
3667df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
3668df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
3669df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
3670df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
3671df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
3672df8bae1dSRodney W. Grimes 	 */
3673df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
3674df8bae1dSRodney W. Grimes }
3675df8bae1dSRodney W. Grimes 
3676df8bae1dSRodney W. Grimes /*
3677df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
3678df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
36794faaea55SAndre Oppermann  * If none, use an mss that can be handled on the outgoing interface
36804faaea55SAndre Oppermann  * without forcing IP to fragment.  If no route is found, route has no mtu,
3681df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
3682df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
3683df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
3684df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
3685df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
3686df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
3687df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
3688a0292f23SGarrett Wollman  *
36890c39d38dSGleb Smirnoff  * NOTE that resulting t_maxseg doesn't include space for TCP options or
36900c39d38dSGleb Smirnoff  * IP options, e.g. IPSEC data, since length of this data may vary, and
36910c39d38dSGleb Smirnoff  * thus it is calculated for every segment separately in tcp_output().
3692a0292f23SGarrett Wollman  *
369397d8d152SAndre Oppermann  * NOTE that this routine is only called when we process an incoming
3694ef341ee1SGleb Smirnoff  * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS
3695ef341ee1SGleb Smirnoff  * settings are handled in tcp_mssopt().
3696df8bae1dSRodney W. Grimes  */
3697a0292f23SGarrett Wollman void
3698ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer,
36993c914c54SAndre Oppermann     struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap)
3700df8bae1dSRodney W. Grimes {
3701b287c6c7SBjoern A. Zeeb 	int mss = 0;
37023ac12506SJonathan T. Looney 	uint32_t maxmtu = 0;
3703c068736aSJeffrey Hsu 	struct inpcb *inp = tp->t_inpcb;
370497d8d152SAndre Oppermann 	struct hc_metrics_lite metrics;
3705fb59c426SYoshinobu Inoue #ifdef INET6
3706c068736aSJeffrey Hsu 	int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
3707c068736aSJeffrey Hsu 	size_t min_protoh = isipv6 ?
3708c068736aSJeffrey Hsu 			    sizeof (struct ip6_hdr) + sizeof (struct tcphdr) :
3709c068736aSJeffrey Hsu 			    sizeof (struct tcpiphdr);
3710c068736aSJeffrey Hsu #else
37119e644c23SMichael Tuexen 	 size_t min_protoh = sizeof(struct tcpiphdr);
3712fb59c426SYoshinobu Inoue #endif
3713df8bae1dSRodney W. Grimes 
37148501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
37157a3244ccSRobert Watson 
37169e644c23SMichael Tuexen 	if (tp->t_port)
37179e644c23SMichael Tuexen 		min_protoh += V_tcp_udp_tunneling_overhead;
3718ef341ee1SGleb Smirnoff 	if (mtuoffer != -1) {
3719ef341ee1SGleb Smirnoff 		KASSERT(offer == -1, ("%s: conflict", __func__));
3720ef341ee1SGleb Smirnoff 		offer = mtuoffer - min_protoh;
3721ef341ee1SGleb Smirnoff 	}
3722ef341ee1SGleb Smirnoff 
3723e8949f74SAndre Oppermann 	/* Initialize. */
372497d8d152SAndre Oppermann #ifdef INET6
372597d8d152SAndre Oppermann 	if (isipv6) {
37263c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu6(&inp->inp_inc, cap);
37270c39d38dSGleb Smirnoff 		tp->t_maxseg = V_tcp_v6mssdflt;
3728b287c6c7SBjoern A. Zeeb 	}
372997d8d152SAndre Oppermann #endif
3730b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3731b287c6c7SBjoern A. Zeeb 	else
3732b287c6c7SBjoern A. Zeeb #endif
3733b287c6c7SBjoern A. Zeeb #ifdef INET
373497d8d152SAndre Oppermann 	{
37353c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu(&inp->inp_inc, cap);
37360c39d38dSGleb Smirnoff 		tp->t_maxseg = V_tcp_mssdflt;
3737df8bae1dSRodney W. Grimes 	}
3738b287c6c7SBjoern A. Zeeb #endif
3739df8bae1dSRodney W. Grimes 
374097d8d152SAndre Oppermann 	/*
3741e8949f74SAndre Oppermann 	 * No route to sender, stay with default mss and return.
374297d8d152SAndre Oppermann 	 */
37436f01cac6SBjoern A. Zeeb 	if (maxmtu == 0) {
37446f01cac6SBjoern A. Zeeb 		/*
37458e5c87f4SBjoern A. Zeeb 		 * In case we return early we need to initialize metrics
37466f01cac6SBjoern A. Zeeb 		 * to a defined state as tcp_hc_get() would do for us
37476f01cac6SBjoern A. Zeeb 		 * if there was no cache hit.
37486f01cac6SBjoern A. Zeeb 		 */
37496f01cac6SBjoern A. Zeeb 		if (metricptr != NULL)
37506f01cac6SBjoern A. Zeeb 			bzero(metricptr, sizeof(struct hc_metrics_lite));
375197d8d152SAndre Oppermann 		return;
37526f01cac6SBjoern A. Zeeb 	}
375397d8d152SAndre Oppermann 
3754e8949f74SAndre Oppermann 	/* What have we got? */
375597d8d152SAndre Oppermann 	switch (offer) {
375697d8d152SAndre Oppermann 		case 0:
375797d8d152SAndre Oppermann 			/*
375897d8d152SAndre Oppermann 			 * Offer == 0 means that there was no MSS on the SYN
3759c3b02504SBjoern A. Zeeb 			 * segment, in this case we use tcp_mssdflt as
37600c39d38dSGleb Smirnoff 			 * already assigned to t_maxseg above.
376197d8d152SAndre Oppermann 			 */
37620c39d38dSGleb Smirnoff 			offer = tp->t_maxseg;
376397d8d152SAndre Oppermann 			break;
376497d8d152SAndre Oppermann 
376597d8d152SAndre Oppermann 		case -1:
3766a0292f23SGarrett Wollman 			/*
3767c94c54e4SAndre Oppermann 			 * Offer == -1 means that we didn't receive SYN yet.
3768a0292f23SGarrett Wollman 			 */
376997d8d152SAndre Oppermann 			/* FALLTHROUGH */
377097d8d152SAndre Oppermann 
377197d8d152SAndre Oppermann 		default:
3772a0292f23SGarrett Wollman 			/*
377353369ac9SAndre Oppermann 			 * Prevent DoS attack with too small MSS. Round up
377453369ac9SAndre Oppermann 			 * to at least minmss.
377553369ac9SAndre Oppermann 			 */
3776603724d3SBjoern A. Zeeb 			offer = max(offer, V_tcp_minmss);
377797d8d152SAndre Oppermann 	}
3778a0292f23SGarrett Wollman 
3779df8bae1dSRodney W. Grimes 	/*
3780e8949f74SAndre Oppermann 	 * rmx information is now retrieved from tcp_hostcache.
3781df8bae1dSRodney W. Grimes 	 */
378297d8d152SAndre Oppermann 	tcp_hc_get(&inp->inp_inc, &metrics);
37833cee92e0SBjoern A. Zeeb 	if (metricptr != NULL)
37843cee92e0SBjoern A. Zeeb 		bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite));
378597d8d152SAndre Oppermann 
3786df8bae1dSRodney W. Grimes 	/*
3787cc412412SHiren Panchasara 	 * If there's a discovered mtu in tcp hostcache, use it.
3788cc412412SHiren Panchasara 	 * Else, use the link mtu.
3789df8bae1dSRodney W. Grimes 	 */
379097d8d152SAndre Oppermann 	if (metrics.rmx_mtu)
37912d166c02SAndre Oppermann 		mss = min(metrics.rmx_mtu, maxmtu) - min_protoh;
3792c068736aSJeffrey Hsu 	else {
379331b3783cSHajimu UMEMOTO #ifdef INET6
3794fb59c426SYoshinobu Inoue 		if (isipv6) {
379597d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3796603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
379797d8d152SAndre Oppermann 			    !in6_localaddr(&inp->in6p_faddr))
3798603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_v6mssdflt);
3799b287c6c7SBjoern A. Zeeb 		}
3800b3399803SHajimu UMEMOTO #endif
3801b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3802b287c6c7SBjoern A. Zeeb 		else
3803b287c6c7SBjoern A. Zeeb #endif
3804b287c6c7SBjoern A. Zeeb #ifdef INET
380597d8d152SAndre Oppermann 		{
380697d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3807603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
380897d8d152SAndre Oppermann 			    !in_localaddr(inp->inp_faddr))
3809603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_mssdflt);
3810a0292f23SGarrett Wollman 		}
3811b287c6c7SBjoern A. Zeeb #endif
38123cee92e0SBjoern A. Zeeb 		/*
38133cee92e0SBjoern A. Zeeb 		 * XXX - The above conditional (mss = maxmtu - min_protoh)
38143cee92e0SBjoern A. Zeeb 		 * probably violates the TCP spec.
38153cee92e0SBjoern A. Zeeb 		 * The problem is that, since we don't know the
38163cee92e0SBjoern A. Zeeb 		 * other end's MSS, we are supposed to use a conservative
38173cee92e0SBjoern A. Zeeb 		 * default.  But, if we do that, then MTU discovery will
38183cee92e0SBjoern A. Zeeb 		 * never actually take place, because the conservative
38193cee92e0SBjoern A. Zeeb 		 * default is much less than the MTUs typically seen
38203cee92e0SBjoern A. Zeeb 		 * on the Internet today.  For the moment, we'll sweep
38213cee92e0SBjoern A. Zeeb 		 * this under the carpet.
38223cee92e0SBjoern A. Zeeb 		 *
38233cee92e0SBjoern A. Zeeb 		 * The conservative default might not actually be a problem
38243cee92e0SBjoern A. Zeeb 		 * if the only case this occurs is when sending an initial
38253cee92e0SBjoern A. Zeeb 		 * SYN with options and data to a host we've never talked
38263cee92e0SBjoern A. Zeeb 		 * to before.  Then, they will reply with an MSS value which
38273cee92e0SBjoern A. Zeeb 		 * will get recorded and the new parameters should get
38283cee92e0SBjoern A. Zeeb 		 * recomputed.  For Further Study.
38293cee92e0SBjoern A. Zeeb 		 */
383097d8d152SAndre Oppermann 	}
3831a0292f23SGarrett Wollman 	mss = min(mss, offer);
383297d8d152SAndre Oppermann 
3833a0292f23SGarrett Wollman 	/*
38340c39d38dSGleb Smirnoff 	 * Sanity check: make sure that maxseg will be large
38353cee92e0SBjoern A. Zeeb 	 * enough to allow some data on segments even if the
38363cee92e0SBjoern A. Zeeb 	 * all the option space is used (40bytes).  Otherwise
38373cee92e0SBjoern A. Zeeb 	 * funny things may happen in tcp_output.
38380c39d38dSGleb Smirnoff 	 *
38390c39d38dSGleb Smirnoff 	 * XXXGL: shouldn't we reserve space for IP/IPv6 options?
38403cee92e0SBjoern A. Zeeb 	 */
38413cee92e0SBjoern A. Zeeb 	mss = max(mss, 64);
38423cee92e0SBjoern A. Zeeb 
384397d8d152SAndre Oppermann 	tp->t_maxseg = mss;
38443cee92e0SBjoern A. Zeeb }
38453cee92e0SBjoern A. Zeeb 
38463cee92e0SBjoern A. Zeeb void
38473cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer)
38483cee92e0SBjoern A. Zeeb {
3849dbc42409SLawrence Stewart 	int mss;
38503ac12506SJonathan T. Looney 	uint32_t bufsize;
38513cee92e0SBjoern A. Zeeb 	struct inpcb *inp;
38523cee92e0SBjoern A. Zeeb 	struct socket *so;
38533cee92e0SBjoern A. Zeeb 	struct hc_metrics_lite metrics;
38543c914c54SAndre Oppermann 	struct tcp_ifcap cap;
3855dbc42409SLawrence Stewart 
38563cee92e0SBjoern A. Zeeb 	KASSERT(tp != NULL, ("%s: tp == NULL", __func__));
38573cee92e0SBjoern A. Zeeb 
38583c914c54SAndre Oppermann 	bzero(&cap, sizeof(cap));
38593c914c54SAndre Oppermann 	tcp_mss_update(tp, offer, -1, &metrics, &cap);
38603cee92e0SBjoern A. Zeeb 
38613cee92e0SBjoern A. Zeeb 	mss = tp->t_maxseg;
38623cee92e0SBjoern A. Zeeb 	inp = tp->t_inpcb;
386397d8d152SAndre Oppermann 
3864df8bae1dSRodney W. Grimes 	/*
386597d8d152SAndre Oppermann 	 * If there's a pipesize, change the socket buffer to that size,
386697d8d152SAndre Oppermann 	 * don't change if sb_hiwat is different than default (then it
386797d8d152SAndre Oppermann 	 * has been changed on purpose with setsockopt).
386897d8d152SAndre Oppermann 	 * Make the socket buffers an integral number of mss units;
386997d8d152SAndre Oppermann 	 * if the mss is larger than the socket buffer, decrease the mss.
3870df8bae1dSRodney W. Grimes 	 */
3871c3b02504SBjoern A. Zeeb 	so = inp->inp_socket;
38723f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
3873e233e2acSAndre Oppermann 	if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe)
387497d8d152SAndre Oppermann 		bufsize = metrics.rmx_sendpipe;
387597d8d152SAndre Oppermann 	else
3876df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
3877df8bae1dSRodney W. Grimes 	if (bufsize < mss)
3878df8bae1dSRodney W. Grimes 		mss = bufsize;
3879df8bae1dSRodney W. Grimes 	else {
3880df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3881df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3882df8bae1dSRodney W. Grimes 			bufsize = sb_max;
388388c39af3SRuslan Ermilov 		if (bufsize > so->so_snd.sb_hiwat)
388443283184SGleb Smirnoff 			(void)sbreserve_locked(so, SO_SND, bufsize, NULL);
3885df8bae1dSRodney W. Grimes 	}
38863f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
3887784ce8faSHiren Panchasara 	/*
3888784ce8faSHiren Panchasara 	 * Sanity check: make sure that maxseg will be large
3889784ce8faSHiren Panchasara 	 * enough to allow some data on segments even if the
3890784ce8faSHiren Panchasara 	 * all the option space is used (40bytes).  Otherwise
3891784ce8faSHiren Panchasara 	 * funny things may happen in tcp_output.
3892784ce8faSHiren Panchasara 	 *
3893784ce8faSHiren Panchasara 	 * XXXGL: shouldn't we reserve space for IP/IPv6 options?
3894784ce8faSHiren Panchasara 	 */
3895784ce8faSHiren Panchasara 	tp->t_maxseg = max(mss, 64);
3896df8bae1dSRodney W. Grimes 
38973f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
3898e233e2acSAndre Oppermann 	if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe)
389997d8d152SAndre Oppermann 		bufsize = metrics.rmx_recvpipe;
390097d8d152SAndre Oppermann 	else
3901df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
3902df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
3903df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3904df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3905df8bae1dSRodney W. Grimes 			bufsize = sb_max;
390688c39af3SRuslan Ermilov 		if (bufsize > so->so_rcv.sb_hiwat)
390743283184SGleb Smirnoff 			(void)sbreserve_locked(so, SO_RCV, bufsize, NULL);
3908df8bae1dSRodney W. Grimes 	}
39093f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
391091d6cfa6SBjoern A. Zeeb 
391191d6cfa6SBjoern A. Zeeb 	/* Check the interface for TSO capabilities. */
39123c914c54SAndre Oppermann 	if (cap.ifcap & CSUM_TSO) {
391391d6cfa6SBjoern A. Zeeb 		tp->t_flags |= TF_TSO;
39143c914c54SAndre Oppermann 		tp->t_tsomax = cap.tsomax;
39159fd573c3SHans Petter Selasky 		tp->t_tsomaxsegcount = cap.tsomaxsegcount;
39169fd573c3SHans Petter Selasky 		tp->t_tsomaxsegsize = cap.tsomaxsegsize;
39173c914c54SAndre Oppermann 	}
3918a0292f23SGarrett Wollman }
3919a0292f23SGarrett Wollman 
3920a0292f23SGarrett Wollman /*
3921a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
3922a0292f23SGarrett Wollman  */
3923a0292f23SGarrett Wollman int
3924ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc)
3925a0292f23SGarrett Wollman {
392697d8d152SAndre Oppermann 	int mss = 0;
39273ac12506SJonathan T. Looney 	uint32_t thcmtu = 0;
39283ac12506SJonathan T. Looney 	uint32_t maxmtu = 0;
392997d8d152SAndre Oppermann 	size_t min_protoh;
3930a0292f23SGarrett Wollman 
393197d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer"));
3932a0292f23SGarrett Wollman 
393331b3783cSHajimu UMEMOTO #ifdef INET6
3934dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
3935603724d3SBjoern A. Zeeb 		mss = V_tcp_v6mssdflt;
3936233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu6(inc, NULL);
393797d8d152SAndre Oppermann 		min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
3938b287c6c7SBjoern A. Zeeb 	}
393931b3783cSHajimu UMEMOTO #endif
3940b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3941b287c6c7SBjoern A. Zeeb 	else
3942b287c6c7SBjoern A. Zeeb #endif
3943b287c6c7SBjoern A. Zeeb #ifdef INET
394497d8d152SAndre Oppermann 	{
3945603724d3SBjoern A. Zeeb 		mss = V_tcp_mssdflt;
3946233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu(inc, NULL);
394797d8d152SAndre Oppermann 		min_protoh = sizeof(struct tcpiphdr);
394897d8d152SAndre Oppermann 	}
3949b287c6c7SBjoern A. Zeeb #endif
39503a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET)
39513a9391deSBjoern A. Zeeb 	thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
39523a9391deSBjoern A. Zeeb #endif
39533a9391deSBjoern A. Zeeb 
395497d8d152SAndre Oppermann 	if (maxmtu && thcmtu)
395597d8d152SAndre Oppermann 		mss = min(maxmtu, thcmtu) - min_protoh;
395697d8d152SAndre Oppermann 	else if (maxmtu || thcmtu)
395797d8d152SAndre Oppermann 		mss = max(maxmtu, thcmtu) - min_protoh;
395897d8d152SAndre Oppermann 
395997d8d152SAndre Oppermann 	return (mss);
3960df8bae1dSRodney W. Grimes }
396146f58482SJonathan Lemon 
39620e1d7c25SRichard Scheffenegger void
396390cca08eSRichard Scheffenegger tcp_do_prr_ack(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to)
39640e1d7c25SRichard Scheffenegger {
396548396dc7SRichard Scheffenegger 	int snd_cnt = 0, limit = 0, del_data = 0, pipe = 0;
39660e1d7c25SRichard Scheffenegger 	int maxseg = tcp_maxseg(tp);
39670e1d7c25SRichard Scheffenegger 
39680e1d7c25SRichard Scheffenegger 	INP_WLOCK_ASSERT(tp->t_inpcb);
39690e1d7c25SRichard Scheffenegger 
39700e1d7c25SRichard Scheffenegger 	/*
39710e1d7c25SRichard Scheffenegger 	 * Compute the amount of data that this ACK is indicating
39720e1d7c25SRichard Scheffenegger 	 * (del_data) and an estimate of how many bytes are in the
39730e1d7c25SRichard Scheffenegger 	 * network.
39740e1d7c25SRichard Scheffenegger 	 */
3975c21b7b55SRichard Scheffenegger 	if (tcp_is_sack_recovery(tp, to) ||
397674d7fc87SRichard Scheffenegger 	    (IN_CONGRECOVERY(tp->t_flags) &&
397774d7fc87SRichard Scheffenegger 	     !IN_FASTRECOVERY(tp->t_flags))) {
397831d7a27cSRichard Scheffenegger 		del_data = tp->sackhint.delivered_data;
3979d1de2b05SRichard Scheffenegger 		if (V_tcp_do_newsack)
3980a8e431e1SRichard Scheffenegger 			pipe = tcp_compute_pipe(tp);
3981a8e431e1SRichard Scheffenegger 		else
398274d7fc87SRichard Scheffenegger 			pipe = (tp->snd_nxt - tp->snd_fack) +
398374d7fc87SRichard Scheffenegger 				tp->sackhint.sack_bytes_rexmit;
398474d7fc87SRichard Scheffenegger 	} else {
398574d7fc87SRichard Scheffenegger 		if (tp->sackhint.prr_delivered < (tcprexmtthresh * maxseg +
398674d7fc87SRichard Scheffenegger 					     tp->snd_recover - tp->snd_una))
398774d7fc87SRichard Scheffenegger 			del_data = maxseg;
398874d7fc87SRichard Scheffenegger 		pipe = imax(0, tp->snd_max - tp->snd_una -
398974d7fc87SRichard Scheffenegger 			    imin(INT_MAX / 65536, tp->t_dupacks) * maxseg);
399074d7fc87SRichard Scheffenegger 	}
39910e1d7c25SRichard Scheffenegger 	tp->sackhint.prr_delivered += del_data;
39920e1d7c25SRichard Scheffenegger 	/*
39930e1d7c25SRichard Scheffenegger 	 * Proportional Rate Reduction
39940e1d7c25SRichard Scheffenegger 	 */
3995e9071000SRichard Scheffenegger 	if (pipe >= tp->snd_ssthresh) {
39966a376af0SRichard Scheffenegger 		if (tp->sackhint.recover_fs == 0)
39976a376af0SRichard Scheffenegger 			tp->sackhint.recover_fs =
399848396dc7SRichard Scheffenegger 			    imax(1, tp->snd_nxt - tp->snd_una);
399948396dc7SRichard Scheffenegger 		snd_cnt = howmany((long)tp->sackhint.prr_delivered *
400048396dc7SRichard Scheffenegger 			    tp->snd_ssthresh, tp->sackhint.recover_fs) -
4001e5313869SRichard Scheffenegger 			    tp->sackhint.prr_out;
40026a376af0SRichard Scheffenegger 	} else {
400374d7fc87SRichard Scheffenegger 		if (V_tcp_do_prr_conservative || (del_data == 0))
400484761f3dSRichard Scheffenegger 			limit = tp->sackhint.prr_delivered -
4005e5313869SRichard Scheffenegger 				tp->sackhint.prr_out;
40060e1d7c25SRichard Scheffenegger 		else
400748396dc7SRichard Scheffenegger 			limit = imax(tp->sackhint.prr_delivered -
4008e5313869SRichard Scheffenegger 				    tp->sackhint.prr_out, del_data) +
4009e5313869SRichard Scheffenegger 				    maxseg;
401048396dc7SRichard Scheffenegger 		snd_cnt = imin((tp->snd_ssthresh - pipe), limit);
40110e1d7c25SRichard Scheffenegger 	}
401248396dc7SRichard Scheffenegger 	snd_cnt = imax(snd_cnt, 0) / maxseg;
40130e1d7c25SRichard Scheffenegger 	/*
40140e1d7c25SRichard Scheffenegger 	 * Send snd_cnt new data into the network in response to this ack.
40150e1d7c25SRichard Scheffenegger 	 * If there is going to be a SACK retransmission, adjust snd_cwnd
40160e1d7c25SRichard Scheffenegger 	 * accordingly.
40170e1d7c25SRichard Scheffenegger 	 */
4018b9f803b7SRichard Scheffenegger 	if (IN_FASTRECOVERY(tp->t_flags)) {
4019c21b7b55SRichard Scheffenegger 		if (tcp_is_sack_recovery(tp, to)) {
402074d7fc87SRichard Scheffenegger 			tp->snd_cwnd = tp->snd_nxt - tp->snd_recover +
402174d7fc87SRichard Scheffenegger 					    tp->sackhint.sack_bytes_rexmit +
402274d7fc87SRichard Scheffenegger 					    (snd_cnt * maxseg);
402374d7fc87SRichard Scheffenegger 		} else {
402474d7fc87SRichard Scheffenegger 			tp->snd_cwnd = (tp->snd_max - tp->snd_una) +
402574d7fc87SRichard Scheffenegger 					    (snd_cnt * maxseg);
402674d7fc87SRichard Scheffenegger 		}
4027b9f803b7SRichard Scheffenegger 	} else if (IN_CONGRECOVERY(tp->t_flags))
402874d7fc87SRichard Scheffenegger 		tp->snd_cwnd = pipe - del_data + (snd_cnt * maxseg);
402974d7fc87SRichard Scheffenegger 	tp->snd_cwnd = imax(maxseg, tp->snd_cwnd);
40300e1d7c25SRichard Scheffenegger }
40310e1d7c25SRichard Scheffenegger 
403246f58482SJonathan Lemon /*
4033c068736aSJeffrey Hsu  * On a partial ack arrives, force the retransmission of the
4034c068736aSJeffrey Hsu  * next unacknowledged segment.  Do not clear tp->t_dupacks.
4035c068736aSJeffrey Hsu  * By setting snd_nxt to ti_ack, this forces retransmission timer to
4036c068736aSJeffrey Hsu  * be started again.
403746f58482SJonathan Lemon  */
403855bceb1eSRandall Stewart void
4039ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th)
404046f58482SJonathan Lemon {
404146f58482SJonathan Lemon 	tcp_seq onxt = tp->snd_nxt;
40423ac12506SJonathan T. Looney 	uint32_t ocwnd = tp->snd_cwnd;
40430c39d38dSGleb Smirnoff 	u_int maxseg = tcp_maxseg(tp);
404446f58482SJonathan Lemon 
40458501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
40467a3244ccSRobert Watson 
4047b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_REXMT, 0);
404846f58482SJonathan Lemon 	tp->t_rtttime = 0;
404946f58482SJonathan Lemon 	tp->snd_nxt = th->th_ack;
40506b2a5f92SJayanth Vijayaraghavan 	/*
4051c068736aSJeffrey Hsu 	 * Set snd_cwnd to one segment beyond acknowledged offset.
4052c068736aSJeffrey Hsu 	 * (tp->snd_una has not yet been updated when this function is called.)
40536b2a5f92SJayanth Vijayaraghavan 	 */
40540c39d38dSGleb Smirnoff 	tp->snd_cwnd = maxseg + BYTES_THIS_ACK(tp, th);
4055a84db8f4SMatthew Dillon 	tp->t_flags |= TF_ACKNOW;
405640fa3e40SGleb Smirnoff 	(void) tcp_output(tp);
405746f58482SJonathan Lemon 	tp->snd_cwnd = ocwnd;
405846f58482SJonathan Lemon 	if (SEQ_GT(onxt, tp->snd_nxt))
405946f58482SJonathan Lemon 		tp->snd_nxt = onxt;
406046f58482SJonathan Lemon 	/*
406146f58482SJonathan Lemon 	 * Partial window deflation.  Relies on fact that tp->snd_una
406246f58482SJonathan Lemon 	 * not updated yet.
406346f58482SJonathan Lemon 	 */
4064dbc42409SLawrence Stewart 	if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th))
4065dbc42409SLawrence Stewart 		tp->snd_cwnd -= BYTES_THIS_ACK(tp, th);
4066d7587117SPaul Saab 	else
4067d7587117SPaul Saab 		tp->snd_cwnd = 0;
40680c39d38dSGleb Smirnoff 	tp->snd_cwnd += maxseg;
406946f58482SJonathan Lemon }
407012eeb81fSHiren Panchasara 
407112eeb81fSHiren Panchasara int
407212eeb81fSHiren Panchasara tcp_compute_pipe(struct tcpcb *tp)
407312eeb81fSHiren Panchasara {
4074e5049a17SRandall Stewart 	if (tp->t_fb->tfb_compute_pipe == NULL) {
407512eeb81fSHiren Panchasara 		return (tp->snd_max - tp->snd_una +
407612eeb81fSHiren Panchasara 			tp->sackhint.sack_bytes_rexmit -
407712eeb81fSHiren Panchasara 			tp->sackhint.sacked_bytes);
4078e5049a17SRandall Stewart 	} else {
4079e5049a17SRandall Stewart 		return((*tp->t_fb->tfb_compute_pipe)(tp));
4080e5049a17SRandall Stewart 	}
408112eeb81fSHiren Panchasara }
40827dc90a1dSMichael Tuexen 
40837dc90a1dSMichael Tuexen uint32_t
40847dc90a1dSMichael Tuexen tcp_compute_initwnd(uint32_t maxseg)
40857dc90a1dSMichael Tuexen {
40867dc90a1dSMichael Tuexen 	/*
40877dc90a1dSMichael Tuexen 	 * Calculate the Initial Window, also used as Restart Window
40887dc90a1dSMichael Tuexen 	 *
40897dc90a1dSMichael Tuexen 	 * RFC5681 Section 3.1 specifies the default conservative values.
40907dc90a1dSMichael Tuexen 	 * RFC3390 specifies slightly more aggressive values.
40917dc90a1dSMichael Tuexen 	 * RFC6928 increases it to ten segments.
40927dc90a1dSMichael Tuexen 	 * Support for user specified value for initial flight size.
40937dc90a1dSMichael Tuexen 	 */
40947dc90a1dSMichael Tuexen 	if (V_tcp_initcwnd_segments)
40957dc90a1dSMichael Tuexen 		return min(V_tcp_initcwnd_segments * maxseg,
40967dc90a1dSMichael Tuexen 		    max(2 * maxseg, V_tcp_initcwnd_segments * 1460));
40977dc90a1dSMichael Tuexen 	else if (V_tcp_do_rfc3390)
40987dc90a1dSMichael Tuexen 		return min(4 * maxseg, max(2 * maxseg, 4380));
40997dc90a1dSMichael Tuexen 	else {
41007dc90a1dSMichael Tuexen 		/* Per RFC5681 Section 3.1 */
41017dc90a1dSMichael Tuexen 		if (maxseg > 2190)
41027dc90a1dSMichael Tuexen 			return (2 * maxseg);
41037dc90a1dSMichael Tuexen 		else if (maxseg > 1095)
41047dc90a1dSMichael Tuexen 			return (3 * maxseg);
41057dc90a1dSMichael Tuexen 		else
41067dc90a1dSMichael Tuexen 			return (4 * maxseg);
41077dc90a1dSMichael Tuexen 	}
41087dc90a1dSMichael Tuexen }
4109