xref: /freebsd/sys/netinet/tcp_input.c (revision 893ed42eca32f7b5e3a5b8cef3f364be0d9cf3ef)
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  */
49df8bae1dSRodney W. Grimes 
504b421e2dSMike Silbersack #include <sys/cdefs.h>
511cfd4b53SBruce M Simpson #include "opt_inet.h"
52fb59c426SYoshinobu Inoue #include "opt_inet6.h"
533a2a9f79SYoshinobu Inoue #include "opt_ipsec.h"
54e5738ee0SCheng Cui #include "opt_rss.h"
550cc12cc5SJoerg Wunsch 
56df8bae1dSRodney W. Grimes #include <sys/param.h>
57adc56f5aSEdward Tomasz Napierala #include <sys/arb.h>
5898163b98SPoul-Henning Kamp #include <sys/kernel.h>
59bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
6039bc9de5SLawrence Stewart #include <sys/hhook.h>
61bd79708dSJonathan T. Looney #endif
62df8bae1dSRodney W. Grimes #include <sys/malloc.h>
63df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
64a29f300eSGarrett Wollman #include <sys/proc.h>		/* for proc0 declaration */
65df8bae1dSRodney W. Grimes #include <sys/protosw.h>
66adc56f5aSEdward Tomasz Napierala #include <sys/qmath.h>
6757f60867SMark Johnston #include <sys/sdt.h>
68960ed29cSSeigo Tanimura #include <sys/signalvar.h>
69df8bae1dSRodney W. Grimes #include <sys/socket.h>
70df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
71960ed29cSSeigo Tanimura #include <sys/sysctl.h>
72816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
73960ed29cSSeigo Tanimura #include <sys/systm.h>
74adc56f5aSEdward Tomasz Napierala #include <sys/stats.h>
75df8bae1dSRodney W. Grimes 
76e79adb8eSGarrett Wollman #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
77e79adb8eSGarrett Wollman 
7812e2e970SAndre Oppermann #include <vm/uma.h>
7912e2e970SAndre Oppermann 
80df8bae1dSRodney W. Grimes #include <net/if.h>
8176039bc8SGleb Smirnoff #include <net/if_var.h>
82df8bae1dSRodney W. Grimes #include <net/route.h>
83e5738ee0SCheng Cui #include <net/rss_config.h>
84530c0060SRobert Watson #include <net/vnet.h>
85df8bae1dSRodney W. Grimes 
86773673c1SAndre Oppermann #define TCPSTATES		/* for logging */
87773673c1SAndre Oppermann 
88df8bae1dSRodney W. Grimes #include <netinet/in.h>
8957f60867SMark Johnston #include <netinet/in_kdtrace.h>
90960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
91e5738ee0SCheng Cui #include <netinet/in_rss.h>
92df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
93df8bae1dSRodney W. Grimes #include <netinet/ip.h>
948e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h>	/* required for icmp_var.h */
95686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM */
96df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
97ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
98686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
99686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
100686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
101e5738ee0SCheng Cui #include <netinet6/in6_rss.h>
102b66d74c1SGleb Smirnoff #include <netinet6/in6_var.h>
103960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h>
104960ed29cSSeigo Tanimura #include <netinet6/nd6.h>
1052de3e790SGleb Smirnoff #include <netinet/tcp.h>
106df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
107df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
108df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
109df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
11069c7c811SRandall Stewart #include <netinet/tcp_log_buf.h>
111fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h>
112df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
1134644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
114c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h>
11586a996e6SHiren Panchasara #ifdef TCPPCAP
11686a996e6SHiren Panchasara #include <netinet/tcp_pcap.h>
11786a996e6SHiren Panchasara #endif
118c325962bSMike Silbersack #include <netinet/tcp_syncache.h>
11909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
12009fe6320SNavdeep Parhar #include <netinet/tcp_offload.h>
12109fe6320SNavdeep Parhar #endif
122f7220c48SRichard Scheffenegger #include <netinet/tcp_ecn.h>
1239e644c23SMichael Tuexen #include <netinet/udp.h>
124fb59c426SYoshinobu Inoue 
125fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
126b9234fafSSam Leffler 
127db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
128db4f9cc7SJonathan Lemon 
129aed55708SRobert Watson #include <security/mac/mac_framework.h>
130aed55708SRobert Watson 
131dbc42409SLawrence Stewart const int tcprexmtthresh = 3;
1320312fbe9SPoul-Henning Kamp 
133334fc582SBjoern A. Zeeb VNET_DEFINE(int, tcp_log_in_vain) = 0;
134334fc582SBjoern A. Zeeb SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_VNET | CTLFLAG_RW,
135334fc582SBjoern A. Zeeb     &VNET_NAME(tcp_log_in_vain), 0,
136eddfbb76SRobert Watson     "Log all incoming TCP segments to closed ports");
137816a3d83SPoul-Henning Kamp 
13882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, blackhole) = 0;
13982cea7e6SBjoern A. Zeeb #define	V_blackhole		VNET(blackhole)
1406df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW,
141eddfbb76SRobert Watson     &VNET_NAME(blackhole), 0,
142eddfbb76SRobert Watson     "Do not send RST on segments to closed ports");
14316f7f31fSGeoff Rehmet 
1443ea9a7cfSGleb Smirnoff VNET_DEFINE(bool, blackhole_local) = false;
1453ea9a7cfSGleb Smirnoff #define	V_blackhole_local	VNET(blackhole_local)
1463ea9a7cfSGleb Smirnoff SYSCTL_BOOL(_net_inet_tcp, OID_AUTO, blackhole_local, CTLFLAG_VNET |
1473ea9a7cfSGleb Smirnoff     CTLFLAG_RW, &VNET_NAME(blackhole_local), false,
1483ea9a7cfSGleb Smirnoff     "Enforce net.inet.tcp.blackhole for locally originated packets");
1493ea9a7cfSGleb Smirnoff 
15082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_delack_enabled) = 1;
1516df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_VNET | CTLFLAG_RW,
152eddfbb76SRobert Watson     &VNET_NAME(tcp_delack_enabled), 0,
1533d177f46SBill Fumerola     "Delay ACK to try and piggyback it onto a data packet");
154f498eeeeSDavid Greenman 
15582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, drop_synfin) = 0;
1566df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_VNET | CTLFLAG_RW,
157eddfbb76SRobert Watson     &VNET_NAME(drop_synfin), 0,
158eddfbb76SRobert Watson     "Drop TCP packets with SYN+FIN set");
159f8613305SDag-Erling Smørgrav 
1600e1d7c25SRichard Scheffenegger VNET_DEFINE(int, tcp_do_prr) = 1;
1610e1d7c25SRichard Scheffenegger SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_prr, CTLFLAG_VNET | CTLFLAG_RW,
1620e1d7c25SRichard Scheffenegger     &VNET_NAME(tcp_do_prr), 1,
1630e1d7c25SRichard Scheffenegger     "Enable Proportional Rate Reduction per RFC 6937");
1640e1d7c25SRichard Scheffenegger 
165b72e56e7SMichael Tuexen VNET_DEFINE(int, tcp_do_newcwv) = 0;
166b72e56e7SMichael Tuexen SYSCTL_INT(_net_inet_tcp, OID_AUTO, newcwv, CTLFLAG_VNET | CTLFLAG_RW,
167b72e56e7SMichael Tuexen     &VNET_NAME(tcp_do_newcwv), 0,
168b72e56e7SMichael Tuexen     "Enable New Congestion Window Validation per RFC7661");
169b72e56e7SMichael Tuexen 
17082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3042) = 1;
1716df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_VNET | CTLFLAG_RW,
172eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3042), 0,
173eddfbb76SRobert Watson     "Enable RFC 3042 (Limited Transmit)");
174582a954bSJeffrey Hsu 
17582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3390) = 1;
1766df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_VNET | CTLFLAG_RW,
177eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3390), 0,
178da3a8a1aSJeffrey Hsu     "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)");
179da3a8a1aSJeffrey Hsu 
180356c7958SHiren Panchasara VNET_DEFINE(int, tcp_initcwnd_segments) = 10;
181356c7958SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, initcwnd_segments,
182356c7958SHiren Panchasara     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_initcwnd_segments), 0,
183356c7958SHiren Panchasara     "Slow-start flight size (initial congestion window) in number of segments");
18409440655SAndre Oppermann 
18582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3465) = 1;
1866df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_VNET | CTLFLAG_RW,
187eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3465), 0,
18824cb0f22SLawrence Stewart     "Enable RFC 3465 (Appropriate Byte Counting)");
189eddfbb76SRobert Watson 
19082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_abc_l_var) = 2;
1916df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_VNET | CTLFLAG_RW,
192eddfbb76SRobert Watson     &VNET_NAME(tcp_abc_l_var), 2,
19324cb0f22SLawrence Stewart     "Cap the max cwnd increment during slow-start to this number of segments");
19424cb0f22SLawrence Stewart 
1953220a212SGleb Smirnoff VNET_DEFINE(int, tcp_insecure_syn) = 0;
1966df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_syn, CTLFLAG_VNET | CTLFLAG_RW,
1973220a212SGleb Smirnoff     &VNET_NAME(tcp_insecure_syn), 0,
1983220a212SGleb Smirnoff     "Follow RFC793 instead of RFC5961 criteria for accepting SYN packets");
1993220a212SGleb Smirnoff 
20082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_insecure_rst) = 0;
2016df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_VNET | CTLFLAG_RW,
202eddfbb76SRobert Watson     &VNET_NAME(tcp_insecure_rst), 0,
2033220a212SGleb Smirnoff     "Follow RFC793 instead of RFC5961 criteria for accepting RST packets");
204a69968eeSMike Silbersack 
205fba0cea1SBjoern A. Zeeb VNET_DEFINE(int, tcp_recvspace) = 1024*64;
206873789cbSAndre Oppermann #define	V_tcp_recvspace	VNET(tcp_recvspace)
2076df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_VNET | CTLFLAG_RW,
208873789cbSAndre Oppermann     &VNET_NAME(tcp_recvspace), 0, "Initial receive socket buffer size");
209873789cbSAndre Oppermann 
21082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autorcvbuf) = 1;
2116df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_VNET | CTLFLAG_RW,
212eddfbb76SRobert Watson     &VNET_NAME(tcp_do_autorcvbuf), 0,
2138b615593SMarko Zec     "Enable automatic receive buffer sizing");
2146741ecf5SAndre Oppermann 
215b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024;
2166df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_VNET | CTLFLAG_RW,
217eddfbb76SRobert Watson     &VNET_NAME(tcp_autorcvbuf_max), 0,
2188b615593SMarko Zec     "Max size of automatic receive buffer");
2196741ecf5SAndre Oppermann 
22082cea7e6SBjoern A. Zeeb VNET_DEFINE(struct inpcbinfo, tcbinfo);
221df8bae1dSRodney W. Grimes 
222315e3e38SRobert Watson /*
223bf840a17SGleb Smirnoff  * TCP statistics are stored in an array of counter(9)s, which size matches
224bf840a17SGleb Smirnoff  * size of struct tcpstat.  TCP running connection count is a regular array.
2255923c293SGleb Smirnoff  */
2265da0521fSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct tcpstat, tcpstat);
2275da0521fSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_tcp, TCPCTL_STATS, stats, struct tcpstat,
2285da0521fSAndrey V. Elsukov     tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
229bf840a17SGleb Smirnoff VNET_DEFINE(counter_u64_t, tcps_states[TCP_NSTATES]);
230bf840a17SGleb Smirnoff SYSCTL_COUNTER_U64_ARRAY(_net_inet_tcp, TCPCTL_STATES, states, CTLFLAG_RD |
231d4d32b9fSHans Petter Selasky     CTLFLAG_VNET, &VNET_NAME(tcps_states)[0], TCP_NSTATES,
232bf840a17SGleb Smirnoff     "TCP connection counts by TCP state");
233bf840a17SGleb Smirnoff 
2345923c293SGleb Smirnoff /*
2357ca6e296SMichael Tuexen  * Kernel module interface for updating tcpstat.  The first argument is an index
2365923c293SGleb Smirnoff  * into tcpstat treated as an array.
237315e3e38SRobert Watson  */
238315e3e38SRobert Watson void
2397ca6e296SMichael Tuexen kmod_tcpstat_add(int statnum, int val)
240315e3e38SRobert Watson {
241315e3e38SRobert Watson 
2427ca6e296SMichael Tuexen 	counter_u64_add(VNET(tcpstat)[statnum], val);
243315e3e38SRobert Watson }
244315e3e38SRobert Watson 
245c21b7b55SRichard Scheffenegger /*
246c21b7b55SRichard Scheffenegger  * Make sure that we only start a SACK loss recovery when
247c21b7b55SRichard Scheffenegger  * receiving a duplicate ACK with a SACK block, and also
248c21b7b55SRichard Scheffenegger  * complete SACK loss recovery in case the other end
249c21b7b55SRichard Scheffenegger  * reneges.
250c21b7b55SRichard Scheffenegger  */
251c21b7b55SRichard Scheffenegger static bool inline
252c21b7b55SRichard Scheffenegger tcp_is_sack_recovery(struct tcpcb *tp, struct tcpopt *to)
253c21b7b55SRichard Scheffenegger {
254c21b7b55SRichard Scheffenegger 	return ((tp->t_flags & TF_SACK_PERMIT) &&
255c21b7b55SRichard Scheffenegger 		((to->to_flags & TOF_SACK) ||
256c21b7b55SRichard Scheffenegger 		(!TAILQ_EMPTY(&tp->snd_holes))));
257c21b7b55SRichard Scheffenegger }
258c21b7b55SRichard Scheffenegger 
259bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
260dbc42409SLawrence Stewart /*
26139bc9de5SLawrence Stewart  * Wrapper for the TCP established input helper hook.
26239bc9de5SLawrence Stewart  */
26355bceb1eSRandall Stewart void
26439bc9de5SLawrence Stewart hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to)
26539bc9de5SLawrence Stewart {
26639bc9de5SLawrence Stewart 	struct tcp_hhook_data hhook_data;
26739bc9de5SLawrence Stewart 
26839bc9de5SLawrence Stewart 	if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) {
26939bc9de5SLawrence Stewart 		hhook_data.tp = tp;
27039bc9de5SLawrence Stewart 		hhook_data.th = th;
27139bc9de5SLawrence Stewart 		hhook_data.to = to;
27239bc9de5SLawrence Stewart 
27339bc9de5SLawrence Stewart 		hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data,
274e68b3792SGleb Smirnoff 		    &tp->t_osd);
27539bc9de5SLawrence Stewart 	}
27639bc9de5SLawrence Stewart }
277bd79708dSJonathan T. Looney #endif
27839bc9de5SLawrence Stewart 
27939bc9de5SLawrence Stewart /*
280dbc42409SLawrence Stewart  * CC wrapper hook functions
281dbc42409SLawrence Stewart  */
28255bceb1eSRandall Stewart void
2834b7b743cSLawrence Stewart cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t nsegs,
2844b7b743cSLawrence Stewart     uint16_t type)
285f2512ba1SRui Paulo {
286adc56f5aSEdward Tomasz Napierala #ifdef STATS
287adc56f5aSEdward Tomasz Napierala 	int32_t gput;
288adc56f5aSEdward Tomasz Napierala #endif
289adc56f5aSEdward Tomasz Napierala 
2909eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
291f2512ba1SRui Paulo 
292e68b3792SGleb Smirnoff 	tp->t_ccv.nsegs = nsegs;
293e68b3792SGleb Smirnoff 	tp->t_ccv.bytes_this_ack = BYTES_THIS_ACK(tp, th);
294b72e56e7SMichael Tuexen 	if ((!V_tcp_do_newcwv && (tp->snd_cwnd <= tp->snd_wnd)) ||
295b72e56e7SMichael Tuexen 	    (V_tcp_do_newcwv && (tp->snd_cwnd <= tp->snd_wnd) &&
296b72e56e7SMichael Tuexen 	     (tp->snd_cwnd < (tcp_compute_pipe(tp) * 2))))
297e68b3792SGleb Smirnoff 		tp->t_ccv.flags |= CCF_CWND_LIMITED;
298dbc42409SLawrence Stewart 	else
299e68b3792SGleb Smirnoff 		tp->t_ccv.flags &= ~CCF_CWND_LIMITED;
300dbc42409SLawrence Stewart 
301dbc42409SLawrence Stewart 	if (type == CC_ACK) {
302adc56f5aSEdward Tomasz Napierala #ifdef STATS
303adc56f5aSEdward Tomasz Napierala 		stats_voi_update_abs_s32(tp->t_stats, VOI_TCP_CALCFRWINDIFF,
304adc56f5aSEdward Tomasz Napierala 		    ((int32_t)tp->snd_cwnd) - tp->snd_wnd);
305adc56f5aSEdward Tomasz Napierala 		if (!IN_RECOVERY(tp->t_flags))
306adc56f5aSEdward Tomasz Napierala 			stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_ACKLEN,
307e68b3792SGleb Smirnoff 			   tp->t_ccv.bytes_this_ack / (tcp_maxseg(tp) * nsegs));
308adc56f5aSEdward Tomasz Napierala 		if ((tp->t_flags & TF_GPUTINPROG) &&
309adc56f5aSEdward Tomasz Napierala 		    SEQ_GEQ(th->th_ack, tp->gput_ack)) {
310adc56f5aSEdward Tomasz Napierala 			/*
311adc56f5aSEdward Tomasz Napierala 			 * Compute goodput in bits per millisecond.
312adc56f5aSEdward Tomasz Napierala 			 */
313f5766992SHans Petter Selasky 			gput = (((int64_t)SEQ_SUB(th->th_ack, tp->gput_seq)) << 3) /
314adc56f5aSEdward Tomasz Napierala 			    max(1, tcp_ts_getticks() - tp->gput_ts);
315adc56f5aSEdward Tomasz Napierala 			stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT,
316adc56f5aSEdward Tomasz Napierala 			    gput);
317adc56f5aSEdward Tomasz Napierala 			/*
318adc56f5aSEdward Tomasz Napierala 			 * XXXLAS: This is a temporary hack, and should be
319adc56f5aSEdward Tomasz Napierala 			 * chained off VOI_TCP_GPUT when stats(9) grows an API
320adc56f5aSEdward Tomasz Napierala 			 * to deal with chained VOIs.
321adc56f5aSEdward Tomasz Napierala 			 */
322adc56f5aSEdward Tomasz Napierala 			if (tp->t_stats_gput_prev > 0)
323adc56f5aSEdward Tomasz Napierala 				stats_voi_update_abs_s32(tp->t_stats,
324adc56f5aSEdward Tomasz Napierala 				    VOI_TCP_GPUT_ND,
325adc56f5aSEdward Tomasz Napierala 				    ((gput - tp->t_stats_gput_prev) * 100) /
326adc56f5aSEdward Tomasz Napierala 				    tp->t_stats_gput_prev);
327adc56f5aSEdward Tomasz Napierala 			tp->t_flags &= ~TF_GPUTINPROG;
328adc56f5aSEdward Tomasz Napierala 			tp->t_stats_gput_prev = gput;
329adc56f5aSEdward Tomasz Napierala 		}
330adc56f5aSEdward Tomasz Napierala #endif /* STATS */
331dbc42409SLawrence Stewart 		if (tp->snd_cwnd > tp->snd_ssthresh) {
332e68b3792SGleb Smirnoff 			tp->t_bytes_acked += tp->t_ccv.bytes_this_ack;
333dbc42409SLawrence Stewart 			if (tp->t_bytes_acked >= tp->snd_cwnd) {
334dbc42409SLawrence Stewart 				tp->t_bytes_acked -= tp->snd_cwnd;
335e68b3792SGleb Smirnoff 				tp->t_ccv.flags |= CCF_ABC_SENTAWND;
336dbc42409SLawrence Stewart 			}
337dbc42409SLawrence Stewart 		} else {
338e68b3792SGleb Smirnoff 				tp->t_ccv.flags &= ~CCF_ABC_SENTAWND;
339dbc42409SLawrence Stewart 				tp->t_bytes_acked = 0;
340dbc42409SLawrence Stewart 		}
341dbc42409SLawrence Stewart 	}
342dbc42409SLawrence Stewart 
343dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->ack_received != NULL) {
344dbc42409SLawrence Stewart 		/* XXXLAS: Find a way to live without this */
345e68b3792SGleb Smirnoff 		tp->t_ccv.curack = th->th_ack;
346e68b3792SGleb Smirnoff 		CC_ALGO(tp)->ack_received(&tp->t_ccv, type);
347dbc42409SLawrence Stewart 	}
348adc56f5aSEdward Tomasz Napierala #ifdef STATS
349adc56f5aSEdward Tomasz Napierala 	stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_LCWIN, tp->snd_cwnd);
350adc56f5aSEdward Tomasz Napierala #endif
351dbc42409SLawrence Stewart }
352dbc42409SLawrence Stewart 
35355bceb1eSRandall Stewart void
354dbc42409SLawrence Stewart cc_conn_init(struct tcpcb *tp)
355dbc42409SLawrence Stewart {
356dbc42409SLawrence Stewart 	struct hc_metrics_lite metrics;
3579eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
3580c39d38dSGleb Smirnoff 	u_int maxseg;
359dbc42409SLawrence Stewart 	int rtt;
360dbc42409SLawrence Stewart 
3619eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
362dbc42409SLawrence Stewart 
363dbc42409SLawrence Stewart 	tcp_hc_get(&inp->inp_inc, &metrics);
3640c39d38dSGleb Smirnoff 	maxseg = tcp_maxseg(tp);
365dbc42409SLawrence Stewart 
366dbc42409SLawrence Stewart 	if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) {
367dbc42409SLawrence Stewart 		tp->t_srtt = rtt;
368dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_usedrtt);
369dbc42409SLawrence Stewart 		if (metrics.rmx_rttvar) {
370dbc42409SLawrence Stewart 			tp->t_rttvar = metrics.rmx_rttvar;
371dbc42409SLawrence Stewart 			TCPSTAT_INC(tcps_usedrttvar);
372dbc42409SLawrence Stewart 		} else {
373dbc42409SLawrence Stewart 			/* default variation is +- 1 rtt */
374dbc42409SLawrence Stewart 			tp->t_rttvar =
375dbc42409SLawrence Stewart 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
376dbc42409SLawrence Stewart 		}
377dbc42409SLawrence Stewart 		TCPT_RANGESET(tp->t_rxtcur,
378dbc42409SLawrence Stewart 		    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
379dbc42409SLawrence Stewart 		    tp->t_rttmin, TCPTV_REXMTMAX);
380dbc42409SLawrence Stewart 	}
381dbc42409SLawrence Stewart 	if (metrics.rmx_ssthresh) {
382dbc42409SLawrence Stewart 		/*
383dbc42409SLawrence Stewart 		 * There's some sort of gateway or interface
384dbc42409SLawrence Stewart 		 * buffer limit on the path.  Use this to set
385a4641f4eSPedro F. Giffuni 		 * the slow start threshold, but set the
386dbc42409SLawrence Stewart 		 * threshold to no less than 2*mss.
387dbc42409SLawrence Stewart 		 */
3880c39d38dSGleb Smirnoff 		tp->snd_ssthresh = max(2 * maxseg, metrics.rmx_ssthresh);
389dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_usedssthresh);
390dbc42409SLawrence Stewart 	}
391dbc42409SLawrence Stewart 
392dbc42409SLawrence Stewart 	/*
3939ec4a4ccSAndre Oppermann 	 * Set the initial slow-start flight size.
394dbc42409SLawrence Stewart 	 *
395cf8f04f4SAndre Oppermann 	 * If a SYN or SYN/ACK was lost and retransmitted, we have to
396cf8f04f4SAndre Oppermann 	 * reduce the initial CWND to one segment as congestion is likely
397cf8f04f4SAndre Oppermann 	 * requiring us to be cautious.
398dbc42409SLawrence Stewart 	 */
399cf8f04f4SAndre Oppermann 	if (tp->snd_cwnd == 1)
4000c39d38dSGleb Smirnoff 		tp->snd_cwnd = maxseg;		/* SYN(-ACK) lost */
401dbc42409SLawrence Stewart 	else
4027dc90a1dSMichael Tuexen 		tp->snd_cwnd = tcp_compute_initwnd(maxseg);
403dbc42409SLawrence Stewart 
404dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->conn_init != NULL)
405e68b3792SGleb Smirnoff 		CC_ALGO(tp)->conn_init(&tp->t_ccv);
406dbc42409SLawrence Stewart }
407dbc42409SLawrence Stewart 
408dbc42409SLawrence Stewart void inline
409dbc42409SLawrence Stewart cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type)
410dbc42409SLawrence Stewart {
4119eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
412dbc42409SLawrence Stewart 
413adc56f5aSEdward Tomasz Napierala #ifdef STATS
414adc56f5aSEdward Tomasz Napierala 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_CSIG, type);
415adc56f5aSEdward Tomasz Napierala #endif
416adc56f5aSEdward Tomasz Napierala 
417dbc42409SLawrence Stewart 	switch(type) {
418dbc42409SLawrence Stewart 	case CC_NDUPACK:
419dbc42409SLawrence Stewart 		if (!IN_FASTRECOVERY(tp->t_flags)) {
420f2512ba1SRui Paulo 			tp->snd_recover = tp->snd_max;
4213cf38784SMichael Tuexen 			if (tp->t_flags2 & TF2_ECN_PERMIT)
4223cf38784SMichael Tuexen 				tp->t_flags2 |= TF2_ECN_SND_CWR;
423f2512ba1SRui Paulo 		}
424dbc42409SLawrence Stewart 		break;
425dbc42409SLawrence Stewart 	case CC_ECN:
426af2fb894SRichard Scheffenegger 		if (!IN_CONGRECOVERY(tp->t_flags) ||
427af2fb894SRichard Scheffenegger 		    /*
428af2fb894SRichard Scheffenegger 		     * Allow ECN reaction on ACK to CWR, if
429af2fb894SRichard Scheffenegger 		     * that data segment was also CE marked.
430af2fb894SRichard Scheffenegger 		     */
431af2fb894SRichard Scheffenegger 		    SEQ_GEQ(th->th_ack, tp->snd_recover)) {
432af2fb894SRichard Scheffenegger 			EXIT_CONGRECOVERY(tp->t_flags);
433dbc42409SLawrence Stewart 			TCPSTAT_INC(tcps_ecn_rcwnd);
434af2fb894SRichard Scheffenegger 			tp->snd_recover = tp->snd_max + 1;
4353cf38784SMichael Tuexen 			if (tp->t_flags2 & TF2_ECN_PERMIT)
4363cf38784SMichael Tuexen 				tp->t_flags2 |= TF2_ECN_SND_CWR;
437dbc42409SLawrence Stewart 		}
438dbc42409SLawrence Stewart 		break;
439dbc42409SLawrence Stewart 	case CC_RTO:
440dbc42409SLawrence Stewart 		tp->t_dupacks = 0;
441dbc42409SLawrence Stewart 		tp->t_bytes_acked = 0;
442dbc42409SLawrence Stewart 		EXIT_RECOVERY(tp->t_flags);
4439cc711c9SMichael Tuexen 		if (tp->t_flags2 & TF2_ECN_PERMIT)
4449cc711c9SMichael Tuexen 			tp->t_flags2 |= TF2_ECN_SND_CWR;
445dbc42409SLawrence Stewart 		break;
446dbc42409SLawrence Stewart 	case CC_RTO_ERR:
447dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_sndrexmitbad);
448dbc42409SLawrence Stewart 		/* RTO was unnecessary, so reset everything. */
449dbc42409SLawrence Stewart 		tp->snd_cwnd = tp->snd_cwnd_prev;
450dbc42409SLawrence Stewart 		tp->snd_ssthresh = tp->snd_ssthresh_prev;
451dbc42409SLawrence Stewart 		tp->snd_recover = tp->snd_recover_prev;
452dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASFRECOVERY)
453dbc42409SLawrence Stewart 			ENTER_FASTRECOVERY(tp->t_flags);
454dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASCRECOVERY)
455dbc42409SLawrence Stewart 			ENTER_CONGRECOVERY(tp->t_flags);
456dbc42409SLawrence Stewart 		tp->snd_nxt = tp->snd_max;
457672dc4aeSJohn Baldwin 		tp->t_flags &= ~TF_PREVVALID;
458dbc42409SLawrence Stewart 		tp->t_badrxtwin = 0;
459dbc42409SLawrence Stewart 		break;
460dbc42409SLawrence Stewart 	}
461dbc42409SLawrence Stewart 
462dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->cong_signal != NULL) {
463dbc42409SLawrence Stewart 		if (th != NULL)
464e68b3792SGleb Smirnoff 			tp->t_ccv.curack = th->th_ack;
465e68b3792SGleb Smirnoff 		CC_ALGO(tp)->cong_signal(&tp->t_ccv, type);
466dbc42409SLawrence Stewart 	}
467dbc42409SLawrence Stewart }
468dbc42409SLawrence Stewart 
46955bceb1eSRandall Stewart void inline
470dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th)
471dbc42409SLawrence Stewart {
4729eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
473dbc42409SLawrence Stewart 
474dbc42409SLawrence Stewart 	/* XXXLAS: KASSERT that we're in recovery? */
475dbc42409SLawrence Stewart 
476dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->post_recovery != NULL) {
477e68b3792SGleb Smirnoff 		tp->t_ccv.curack = th->th_ack;
478e68b3792SGleb Smirnoff 		CC_ALGO(tp)->post_recovery(&tp->t_ccv);
479dbc42409SLawrence Stewart 	}
480dbc42409SLawrence Stewart 	/* XXXLAS: EXIT_RECOVERY ? */
481dbc42409SLawrence Stewart 	tp->t_bytes_acked = 0;
48248be5b97SRichard Scheffenegger 	tp->sackhint.delivered_data = 0;
483e5313869SRichard Scheffenegger 	tp->sackhint.prr_out = 0;
484dbc42409SLawrence Stewart }
4850312fbe9SPoul-Henning Kamp 
486df8bae1dSRodney W. Grimes /*
487262c1c1aSMatthew Dillon  * Indicate whether this ack should be delayed.  We can delay the ack if
48885536381SHiren Panchasara  * following conditions are met:
48985536381SHiren Panchasara  *	- There is no delayed ack timer in progress.
49085536381SHiren Panchasara  *	- Our last ack wasn't a 0-sized window. We never want to delay
49185536381SHiren Panchasara  *	  the ack that opens up a 0-sized window.
49285536381SHiren Panchasara  *	- LRO wasn't used for this segment. We make sure by checking that the
49385536381SHiren Panchasara  *	  segment size is not larger than the MSS.
494d8c85a26SJonathan Lemon  */
495c1e5a6e5SAndre Oppermann #define DELAY_ACK(tp, tlen)						\
496b8152ba7SAndre Oppermann 	((!tcp_timer_active(tp, TT_DELACK) &&				\
497a14c749fSJonathan Lemon 	    (tp->t_flags & TF_RXWIN0SENT) == 0) &&			\
4980c39d38dSGleb Smirnoff 	    (tlen <= tp->t_maxseg) &&					\
499603724d3SBjoern A. Zeeb 	    (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN)))
500d8c85a26SJonathan Lemon 
5014ad24737SRandall Stewart void inline
5025d8fd932SRandall Stewart cc_ecnpkt_handler_flags(struct tcpcb *tp, uint16_t flags, uint8_t iptos)
50364807b30SHiren Panchasara {
5049eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
50564807b30SHiren Panchasara 
50664807b30SHiren Panchasara 	if (CC_ALGO(tp)->ecnpkt_handler != NULL) {
50764807b30SHiren Panchasara 		switch (iptos & IPTOS_ECN_MASK) {
50864807b30SHiren Panchasara 		case IPTOS_ECN_CE:
509e68b3792SGleb Smirnoff 			tp->t_ccv.flags |= CCF_IPHDR_CE;
51064807b30SHiren Panchasara 			break;
51164807b30SHiren Panchasara 		case IPTOS_ECN_ECT0:
51283a2839fSMichael Tuexen 			/* FALLTHROUGH */
51364807b30SHiren Panchasara 		case IPTOS_ECN_ECT1:
51483a2839fSMichael Tuexen 			/* FALLTHROUGH */
51583a2839fSMichael Tuexen 		case IPTOS_ECN_NOTECT:
516e68b3792SGleb Smirnoff 			tp->t_ccv.flags &= ~CCF_IPHDR_CE;
51764807b30SHiren Panchasara 			break;
51864807b30SHiren Panchasara 		}
51964807b30SHiren Panchasara 
5205d8fd932SRandall Stewart 		if (flags & TH_CWR)
521e68b3792SGleb Smirnoff 			tp->t_ccv.flags |= CCF_TCPHDR_CWR;
52264807b30SHiren Panchasara 		else
523e68b3792SGleb Smirnoff 			tp->t_ccv.flags &= ~CCF_TCPHDR_CWR;
52464807b30SHiren Panchasara 
525e68b3792SGleb Smirnoff 		CC_ALGO(tp)->ecnpkt_handler(&tp->t_ccv);
52664807b30SHiren Panchasara 
527e68b3792SGleb Smirnoff 		if (tp->t_ccv.flags & CCF_ACKNOW) {
52864807b30SHiren Panchasara 			tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
529e11c9783SMichael Tuexen 			tp->t_flags |= TF_ACKNOW;
530e11c9783SMichael Tuexen 		}
53164807b30SHiren Panchasara 	}
53264807b30SHiren Panchasara }
53364807b30SHiren Panchasara 
5345d8fd932SRandall Stewart void inline
5355d8fd932SRandall Stewart cc_ecnpkt_handler(struct tcpcb *tp, struct tcphdr *th, uint8_t iptos)
5365d8fd932SRandall Stewart {
5371ebf4607SRichard Scheffenegger 	cc_ecnpkt_handler_flags(tp, tcp_get_flags(th), iptos);
5385d8fd932SRandall Stewart }
5395d8fd932SRandall Stewart 
540df8bae1dSRodney W. Grimes /*
5418d573cc1SAndre Oppermann  * TCP input handling is split into multiple parts:
5428d573cc1SAndre Oppermann  *   tcp6_input is a thin wrapper around tcp_input for the extended
5438d573cc1SAndre Oppermann  *	ip6_protox[] call format in ip6_input
5448d573cc1SAndre Oppermann  *   tcp_input handles primary segment validation, inpcb lookup and
5458d573cc1SAndre Oppermann  *	SYN processing on listen sockets
5468d573cc1SAndre Oppermann  *   tcp_do_segment processes the ACK and text of the segment for
5478d573cc1SAndre Oppermann  *	establishing, established and closing connections
548df8bae1dSRodney W. Grimes  */
549fb59c426SYoshinobu Inoue #ifdef INET6
550fb59c426SYoshinobu Inoue int
5519e644c23SMichael Tuexen tcp6_input_with_port(struct mbuf **mp, int *offp, int proto, uint16_t port)
552fb59c426SYoshinobu Inoue {
553503f4e47SBjoern A. Zeeb 	struct mbuf *m;
55433841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6;
5553e88eb90SAndrey V. Elsukov 	struct ip6_hdr *ip6;
556fb59c426SYoshinobu Inoue 
557503f4e47SBjoern A. Zeeb 	m = *mp;
558a4adf6ccSBjoern A. Zeeb 	if (m->m_len < *offp + sizeof(struct tcphdr)) {
5594e619b17SBjoern A. Zeeb 		m = m_pullup(m, *offp + sizeof(struct tcphdr));
5604e619b17SBjoern A. Zeeb 		if (m == NULL) {
5614e619b17SBjoern A. Zeeb 			*mp = m;
5624e619b17SBjoern A. Zeeb 			TCPSTAT_INC(tcps_rcvshort);
5634e619b17SBjoern A. Zeeb 			return (IPPROTO_DONE);
5644e619b17SBjoern A. Zeeb 		}
565a4adf6ccSBjoern A. Zeeb 	}
566fb59c426SYoshinobu Inoue 
567fb59c426SYoshinobu Inoue 	/*
568fb59c426SYoshinobu Inoue 	 * draft-itojun-ipv6-tcp-to-anycast
569fb59c426SYoshinobu Inoue 	 * better place to put this in?
570fb59c426SYoshinobu Inoue 	 */
5713e88eb90SAndrey V. Elsukov 	ip6 = mtod(m, struct ip6_hdr *);
5728268d82cSAlexander V. Chernikov 	ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false);
57333841545SHajimu UMEMOTO 	if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
574fb59c426SYoshinobu Inoue 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
575fb59c426SYoshinobu Inoue 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
576a8fe77d8SBjoern A. Zeeb 		*mp = NULL;
5778f5a8818SKevin Lo 		return (IPPROTO_DONE);
578fb59c426SYoshinobu Inoue 	}
579fb59c426SYoshinobu Inoue 
580a8fe77d8SBjoern A. Zeeb 	*mp = m;
5819e644c23SMichael Tuexen 	return (tcp_input_with_port(mp, offp, proto, port));
5829e644c23SMichael Tuexen }
5839e644c23SMichael Tuexen 
5849e644c23SMichael Tuexen int
5859e644c23SMichael Tuexen tcp6_input(struct mbuf **mp, int *offp, int proto)
5869e644c23SMichael Tuexen {
5879e644c23SMichael Tuexen 
5889e644c23SMichael Tuexen 	return(tcp6_input_with_port(mp, offp, proto, 0));
589fb59c426SYoshinobu Inoue }
590b287c6c7SBjoern A. Zeeb #endif /* INET6 */
591fb59c426SYoshinobu Inoue 
5928f5a8818SKevin Lo int
5939e644c23SMichael Tuexen tcp_input_with_port(struct mbuf **mp, int *offp, int proto, uint16_t port)
594df8bae1dSRodney W. Grimes {
5958f5a8818SKevin Lo 	struct mbuf *m = *mp;
596b287c6c7SBjoern A. Zeeb 	struct tcphdr *th = NULL;
597ad3f9ab3SAndre Oppermann 	struct ip *ip = NULL;
598ad3f9ab3SAndre Oppermann 	struct inpcb *inp = NULL;
599302ce8d6SAndre Oppermann 	struct tcpcb *tp = NULL;
600302ce8d6SAndre Oppermann 	struct socket *so = NULL;
601e79adb8eSGarrett Wollman 	u_char *optp = NULL;
6028f5a8818SKevin Lo 	int off0;
60326f9a767SRodney W. Grimes 	int optlen = 0;
604b287c6c7SBjoern A. Zeeb #ifdef INET
605b287c6c7SBjoern A. Zeeb 	int len;
606c4556b2fSAndrey V. Elsukov 	uint8_t ipttl;
607b287c6c7SBjoern A. Zeeb #endif
608b287c6c7SBjoern A. Zeeb 	int tlen = 0, off;
609fb59c426SYoshinobu Inoue 	int drop_hdrlen;
610ad3f9ab3SAndre Oppermann 	int thflags;
611302ce8d6SAndre Oppermann 	int rstreason = 0;	/* For badport_bandlim accounting purposes */
61208d9c920SGleb Smirnoff 	int lookupflag;
6131d7ee746SKurt Lidl 	uint8_t iptos;
614c1de64a4SAndrey V. Elsukov 	struct m_tag *fwd_tag = NULL;
615c068736aSJeffrey Hsu #ifdef INET6
616302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6 = NULL;
617c068736aSJeffrey Hsu 	int isipv6;
618c068736aSJeffrey Hsu #else
619a160e630SAndre Oppermann 	const void *ip6 = NULL;
620b287c6c7SBjoern A. Zeeb #endif /* INET6 */
621302ce8d6SAndre Oppermann 	struct tcpopt to;		/* options in this segment */
622a160e630SAndre Oppermann 	char *s = NULL;			/* address and port logging */
623c068736aSJeffrey Hsu 
624d40c0d47SGleb Smirnoff 	NET_EPOCH_ASSERT();
625d40c0d47SGleb Smirnoff 
626fb59c426SYoshinobu Inoue #ifdef INET6
627fb59c426SYoshinobu Inoue 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
628fb59c426SYoshinobu Inoue #endif
629a0292f23SGarrett Wollman 
6308f5a8818SKevin Lo 	off0 = *offp;
6318f5a8818SKevin Lo 	m = *mp;
6328f5a8818SKevin Lo 	*mp = NULL;
633302ce8d6SAndre Oppermann 	to.to_flags = 0;
63478b50714SRobert Watson 	TCPSTAT_INC(tcps_rcvtotal);
635fb59c426SYoshinobu Inoue 
636513f2e2eSGleb Smirnoff 	m->m_pkthdr.tcp_tun_port = port;
63704d3a452SHajimu UMEMOTO #ifdef INET6
638b287c6c7SBjoern A. Zeeb 	if (isipv6) {
639fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
64045747ba5SBjoern A. Zeeb 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
641fb59c426SYoshinobu Inoue 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
6429e644c23SMichael Tuexen 		if (port)
6439e644c23SMichael Tuexen 			goto skip6_csum;
644356ab07eSBjoern A. Zeeb 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) {
64545747ba5SBjoern A. Zeeb 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
64645747ba5SBjoern A. Zeeb 				th->th_sum = m->m_pkthdr.csum_data;
64745747ba5SBjoern A. Zeeb 			else
64845747ba5SBjoern A. Zeeb 				th->th_sum = in6_cksum_pseudo(ip6, tlen,
64945747ba5SBjoern A. Zeeb 				    IPPROTO_TCP, m->m_pkthdr.csum_data);
65045747ba5SBjoern A. Zeeb 			th->th_sum ^= 0xffff;
65145747ba5SBjoern A. Zeeb 		} else
65245747ba5SBjoern A. Zeeb 			th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen);
65345747ba5SBjoern A. Zeeb 		if (th->th_sum) {
65478b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
655fb59c426SYoshinobu Inoue 			goto drop;
656fb59c426SYoshinobu Inoue 		}
6579e644c23SMichael Tuexen 	skip6_csum:
65833841545SHajimu UMEMOTO 		/*
65933841545SHajimu UMEMOTO 		 * Be proactive about unspecified IPv6 address in source.
66033841545SHajimu UMEMOTO 		 * As we use all-zero to indicate unbounded/unconnected pcb,
66133841545SHajimu UMEMOTO 		 * unspecified IPv6 address can be used to confuse us.
66233841545SHajimu UMEMOTO 		 *
66333841545SHajimu UMEMOTO 		 * Note that packets with unspecified IPv6 destination is
66433841545SHajimu UMEMOTO 		 * already dropped in ip6_input.
66533841545SHajimu UMEMOTO 		 */
666713264f6SMark Johnston 		KASSERT(!IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst),
667713264f6SMark Johnston 		    ("%s: unspecified destination v6 address", __func__));
66833841545SHajimu UMEMOTO 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
66978e6c3aaSGleb Smirnoff 			IP6STAT_INC(ip6s_badscope); /* XXX */
67033841545SHajimu UMEMOTO 			goto drop;
67133841545SHajimu UMEMOTO 		}
672bb4a7d94SKristof Provost 		iptos = IPV6_TRAFFIC_CLASS(ip6);
673b287c6c7SBjoern A. Zeeb 	}
67404d3a452SHajimu UMEMOTO #endif
675b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
676b287c6c7SBjoern A. Zeeb 	else
677b287c6c7SBjoern A. Zeeb #endif
678b287c6c7SBjoern A. Zeeb #ifdef INET
679b287c6c7SBjoern A. Zeeb 	{
680df8bae1dSRodney W. Grimes 		/*
681df8bae1dSRodney W. Grimes 		 * Get IP and TCP header together in first mbuf.
682df8bae1dSRodney W. Grimes 		 * Note: IP leaves IP header in first mbuf.
683df8bae1dSRodney W. Grimes 		 */
684fb59c426SYoshinobu Inoue 		if (off0 > sizeof (struct ip)) {
685105bd211SGleb Smirnoff 			ip_stripoptions(m);
686fb59c426SYoshinobu Inoue 			off0 = sizeof(struct ip);
687fb59c426SYoshinobu Inoue 		}
688df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof (struct tcpiphdr)) {
6894dfdffe9SAndre Oppermann 			if ((m = m_pullup(m, sizeof (struct tcpiphdr)))
6904dfdffe9SAndre Oppermann 			    == NULL) {
69178b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvshort);
6928f5a8818SKevin Lo 				return (IPPROTO_DONE);
693df8bae1dSRodney W. Grimes 			}
694df8bae1dSRodney W. Grimes 		}
695fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
696db4f9cc7SJonathan Lemon 		th = (struct tcphdr *)((caddr_t)ip + off0);
6978ad458a4SGleb Smirnoff 		tlen = ntohs(ip->ip_len) - off0;
698df8bae1dSRodney W. Grimes 
6991d7ee746SKurt Lidl 		iptos = ip->ip_tos;
7009e644c23SMichael Tuexen 		if (port)
7019e644c23SMichael Tuexen 			goto skip_csum;
702db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
703db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
704db4f9cc7SJonathan Lemon 				th->th_sum = m->m_pkthdr.csum_data;
705db4f9cc7SJonathan Lemon 			else
706db4f9cc7SJonathan Lemon 				th->th_sum = in_pseudo(ip->ip_src.s_addr,
707c068736aSJeffrey Hsu 				    ip->ip_dst.s_addr,
7088f134647SGleb Smirnoff 				    htonl(m->m_pkthdr.csum_data + tlen +
709c068736aSJeffrey Hsu 				    IPPROTO_TCP));
710db4f9cc7SJonathan Lemon 			th->th_sum ^= 0xffff;
711db4f9cc7SJonathan Lemon 		} else {
7128f134647SGleb Smirnoff 			struct ipovly *ipov = (struct ipovly *)ip;
7138f134647SGleb Smirnoff 
714df8bae1dSRodney W. Grimes 			/*
715df8bae1dSRodney W. Grimes 			 * Checksum extended TCP header and data.
716df8bae1dSRodney W. Grimes 			 */
7178f134647SGleb Smirnoff 			len = off0 + tlen;
718c4556b2fSAndrey V. Elsukov 			ipttl = ip->ip_ttl;
719fb59c426SYoshinobu Inoue 			bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
7208f134647SGleb Smirnoff 			ipov->ih_len = htons(tlen);
721fb59c426SYoshinobu Inoue 			th->th_sum = in_cksum(m, len);
72257f60867SMark Johnston 			/* Reset length for SDT probes. */
7231d7ee746SKurt Lidl 			ip->ip_len = htons(len);
7241d7ee746SKurt Lidl 			/* Reset TOS bits */
7251d7ee746SKurt Lidl 			ip->ip_tos = iptos;
7261d7ee746SKurt Lidl 			/* Re-initialization for later version check */
727c4556b2fSAndrey V. Elsukov 			ip->ip_ttl = ipttl;
7281d7ee746SKurt Lidl 			ip->ip_v = IPVERSION;
729b2fe54bcSAndrey V. Elsukov 			ip->ip_hl = off0 >> 2;
730db4f9cc7SJonathan Lemon 		}
7319e644c23SMichael Tuexen 	skip_csum:
7329e644c23SMichael Tuexen 		if (th->th_sum && (port == 0)) {
73378b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
734df8bae1dSRodney W. Grimes 			goto drop;
735df8bae1dSRodney W. Grimes 		}
736713264f6SMark Johnston 		KASSERT(ip->ip_dst.s_addr != INADDR_ANY,
737713264f6SMark Johnston 		    ("%s: unspecified destination v4 address", __func__));
738713264f6SMark Johnston 		if (__predict_false(ip->ip_src.s_addr == INADDR_ANY)) {
73978e6c3aaSGleb Smirnoff 			IPSTAT_INC(ips_badaddr);
740713264f6SMark Johnston 			goto drop;
741713264f6SMark Johnston 		}
742fb59c426SYoshinobu Inoue 	}
743b287c6c7SBjoern A. Zeeb #endif /* INET */
744df8bae1dSRodney W. Grimes 
745df8bae1dSRodney W. Grimes 	/*
746df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
747df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
748df8bae1dSRodney W. Grimes 	 */
749fb59c426SYoshinobu Inoue 	off = th->th_off << 2;
750df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
75178b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvbadoff);
752df8bae1dSRodney W. Grimes 		goto drop;
753df8bae1dSRodney W. Grimes 	}
754fb59c426SYoshinobu Inoue 	tlen -= off;	/* tlen is used instead of ti->ti_len */
755df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
75604d3a452SHajimu UMEMOTO #ifdef INET6
757b287c6c7SBjoern A. Zeeb 		if (isipv6) {
758a4adf6ccSBjoern A. Zeeb 			if (m->m_len < off0 + off) {
7594e619b17SBjoern A. Zeeb 				m = m_pullup(m, off0 + off);
7604e619b17SBjoern A. Zeeb 				if (m == NULL) {
7614e619b17SBjoern A. Zeeb 					TCPSTAT_INC(tcps_rcvshort);
7624e619b17SBjoern A. Zeeb 					return (IPPROTO_DONE);
7634e619b17SBjoern A. Zeeb 				}
764a4adf6ccSBjoern A. Zeeb 			}
765fb59c426SYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
766fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
767b287c6c7SBjoern A. Zeeb 		}
76804d3a452SHajimu UMEMOTO #endif
769b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
770b287c6c7SBjoern A. Zeeb 		else
771b287c6c7SBjoern A. Zeeb #endif
772b287c6c7SBjoern A. Zeeb #ifdef INET
773b287c6c7SBjoern A. Zeeb 		{
774df8bae1dSRodney W. Grimes 			if (m->m_len < sizeof(struct ip) + off) {
775c068736aSJeffrey Hsu 				if ((m = m_pullup(m, sizeof (struct ip) + off))
7764dfdffe9SAndre Oppermann 				    == NULL) {
77778b50714SRobert Watson 					TCPSTAT_INC(tcps_rcvshort);
7788f5a8818SKevin Lo 					return (IPPROTO_DONE);
779df8bae1dSRodney W. Grimes 				}
780fb59c426SYoshinobu Inoue 				ip = mtod(m, struct ip *);
781fb59c426SYoshinobu Inoue 				th = (struct tcphdr *)((caddr_t)ip + off0);
782fb59c426SYoshinobu Inoue 			}
783df8bae1dSRodney W. Grimes 		}
784b287c6c7SBjoern A. Zeeb #endif
785df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
786fb59c426SYoshinobu Inoue 		optp = (u_char *)(th + 1);
787df8bae1dSRodney W. Grimes 	}
7881ebf4607SRichard Scheffenegger 	thflags = tcp_get_flags(th);
789df8bae1dSRodney W. Grimes 
790e46cd3d4SDag-Erling Smørgrav 	/*
791df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
792df8bae1dSRodney W. Grimes 	 */
7932903309aSAttilio Rao 	tcp_fields_to_host(th);
794df8bae1dSRodney W. Grimes 
795df8bae1dSRodney W. Grimes 	/*
796794235b7SAndre Oppermann 	 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options.
797755c1f07SAndras Olah 	 */
798fb59c426SYoshinobu Inoue 	drop_hdrlen = off0 + off;
799755c1f07SAndras Olah 
800755c1f07SAndras Olah 	/*
8018d573cc1SAndre Oppermann 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
8028d573cc1SAndre Oppermann 	 */
803b8056faeSGleb Smirnoff         if (
804b8056faeSGleb Smirnoff #ifdef INET6
805b8056faeSGleb Smirnoff 	    (isipv6 && (m->m_flags & M_IP6_NEXTHOP))
806b8056faeSGleb Smirnoff #ifdef INET
807b8056faeSGleb Smirnoff 	    || (!isipv6 && (m->m_flags & M_IP_NEXTHOP))
808b8056faeSGleb Smirnoff #endif
809b8056faeSGleb Smirnoff #endif
810b8056faeSGleb Smirnoff #if defined(INET) && !defined(INET6)
811b8056faeSGleb Smirnoff 	    (m->m_flags & M_IP_NEXTHOP)
812b8056faeSGleb Smirnoff #endif
813b8056faeSGleb Smirnoff 	    )
8149b932e9eSAndre Oppermann 		fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
8159b932e9eSAndre Oppermann 
81608d9c920SGleb Smirnoff 	/*
8171db08fbeSGleb Smirnoff 	 * For initial SYN packets we don't need write lock on matching
8181db08fbeSGleb Smirnoff 	 * PCB, be it a listening one or a synchronized one.  The packet
8191db08fbeSGleb Smirnoff 	 * shall not modify its state.
82008d9c920SGleb Smirnoff 	 */
821d88eb465SGleb Smirnoff 	lookupflag = INPLOOKUP_WILDCARD |
822d88eb465SGleb Smirnoff 	    ((thflags & (TH_ACK|TH_SYN)) == TH_SYN ?
823d88eb465SGleb Smirnoff 	    INPLOOKUP_RLOCKPCB : INPLOOKUP_WLOCKPCB);
824ce7ad664SEd Maste findpcb:
8258a006adbSBjoern A. Zeeb #ifdef INET6
8268a006adbSBjoern A. Zeeb 	if (isipv6 && fwd_tag != NULL) {
8278a006adbSBjoern A. Zeeb 		struct sockaddr_in6 *next_hop6;
8288a006adbSBjoern A. Zeeb 
8298a006adbSBjoern A. Zeeb 		next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
8308a006adbSBjoern A. Zeeb 		/*
8318a006adbSBjoern A. Zeeb 		 * Transparently forwarded. Pretend to be the destination.
8328a006adbSBjoern A. Zeeb 		 * Already got one like this?
8338a006adbSBjoern A. Zeeb 		 */
8348a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo,
8358a006adbSBjoern A. Zeeb 		    &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport,
836aab8c844SGleb Smirnoff 		    lookupflag & ~INPLOOKUP_WILDCARD, m->m_pkthdr.rcvif, m);
8378a006adbSBjoern A. Zeeb 		if (!inp) {
8388a006adbSBjoern A. Zeeb 			/*
8398a006adbSBjoern A. Zeeb 			 * It's new.  Try to find the ambushing socket.
8408a006adbSBjoern A. Zeeb 			 * Because we've rewritten the destination address,
8418a006adbSBjoern A. Zeeb 			 * any hardware-generated hash is ignored.
8428a006adbSBjoern A. Zeeb 			 */
8438a006adbSBjoern A. Zeeb 			inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src,
8448a006adbSBjoern A. Zeeb 			    th->th_sport, &next_hop6->sin6_addr,
8458a006adbSBjoern A. Zeeb 			    next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) :
846d88eb465SGleb Smirnoff 			    th->th_dport, lookupflag, m->m_pkthdr.rcvif);
8478a006adbSBjoern A. Zeeb 		}
848c1de64a4SAndrey V. Elsukov 	} else if (isipv6) {
8498a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src,
850d88eb465SGleb Smirnoff 		    th->th_sport, &ip6->ip6_dst, th->th_dport, lookupflag,
851d88eb465SGleb Smirnoff 		    m->m_pkthdr.rcvif, m);
8528a006adbSBjoern A. Zeeb 	}
8538a006adbSBjoern A. Zeeb #endif /* INET6 */
8548a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET)
8558a006adbSBjoern A. Zeeb 	else
8568a006adbSBjoern A. Zeeb #endif
8578a006adbSBjoern A. Zeeb #ifdef INET
8588a006adbSBjoern A. Zeeb 	if (fwd_tag != NULL) {
8599b932e9eSAndre Oppermann 		struct sockaddr_in *next_hop;
8609b932e9eSAndre Oppermann 
8619b932e9eSAndre Oppermann 		next_hop = (struct sockaddr_in *)(fwd_tag+1);
862f9e354dfSJulian Elischer 		/*
8632b25acc1SLuigi Rizzo 		 * Transparently forwarded. Pretend to be the destination.
864f9e354dfSJulian Elischer 		 * already got one like this?
865f9e354dfSJulian Elischer 		 */
866d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport,
867aab8c844SGleb Smirnoff 		    ip->ip_dst, th->th_dport, lookupflag & ~INPLOOKUP_WILDCARD,
868aab8c844SGleb Smirnoff 		    m->m_pkthdr.rcvif, m);
869fa046d87SRobert Watson 		if (!inp) {
870fa046d87SRobert Watson 			/*
871fa046d87SRobert Watson 			 * It's new.  Try to find the ambushing socket.
872d3c1f003SRobert Watson 			 * Because we've rewritten the destination address,
873d3c1f003SRobert Watson 			 * any hardware-generated hash is ignored.
874fa046d87SRobert Watson 			 */
875fa046d87SRobert Watson 			inp = in_pcblookup(&V_tcbinfo, ip->ip_src,
876fa046d87SRobert Watson 			    th->th_sport, next_hop->sin_addr,
877fa046d87SRobert Watson 			    next_hop->sin_port ? ntohs(next_hop->sin_port) :
878d88eb465SGleb Smirnoff 			    th->th_dport, lookupflag, m->m_pkthdr.rcvif);
879f9e354dfSJulian Elischer 		}
880b10fbdeaSAndre Oppermann 	} else
881d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src,
882d88eb465SGleb Smirnoff 		    th->th_sport, ip->ip_dst, th->th_dport, lookupflag,
883d88eb465SGleb Smirnoff 		    m->m_pkthdr.rcvif, m);
8848a006adbSBjoern A. Zeeb #endif /* INET */
885f9e354dfSJulian Elischer 
886df8bae1dSRodney W. Grimes 	/*
887574b6964SAndre Oppermann 	 * If the INPCB does not exist then all data in the incoming
888574b6964SAndre Oppermann 	 * segment is discarded and an appropriate RST is sent back.
8898b07e49aSJulian Elischer 	 * XXX MRT Send RST using which routing table?
890df8bae1dSRodney W. Grimes 	 */
891816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
892d88eb465SGleb Smirnoff 		if (rstreason != 0) {
893d88eb465SGleb Smirnoff 			/* We came here after second (safety) lookup. */
894d88eb465SGleb Smirnoff 			MPASS((lookupflag & INPLOOKUP_WILDCARD) == 0);
895d88eb465SGleb Smirnoff 			goto dropwithreset;
896d88eb465SGleb Smirnoff 		}
897574b6964SAndre Oppermann 		/*
898574b6964SAndre Oppermann 		 * Log communication attempts to ports that are not
899574b6964SAndre Oppermann 		 * in use.
900574b6964SAndre Oppermann 		 */
901334fc582SBjoern A. Zeeb 		if ((V_tcp_log_in_vain == 1 && (thflags & TH_SYN)) ||
902334fc582SBjoern A. Zeeb 		    V_tcp_log_in_vain == 2) {
903b7d747ecSAndre Oppermann 			if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6)))
904df541e5fSAndre Oppermann 				log(LOG_INFO, "%s; %s: Connection attempt "
905df541e5fSAndre Oppermann 				    "to closed port\n", s, __func__);
9062e4e1b4cSGeoff Rehmet 		}
907574b6964SAndre Oppermann 		/*
908574b6964SAndre Oppermann 		 * When blackholing do not respond with a RST but
909574b6964SAndre Oppermann 		 * completely ignore the segment and drop it.
910574b6964SAndre Oppermann 		 */
9113ea9a7cfSGleb Smirnoff 		if (((V_blackhole == 1 && (thflags & TH_SYN)) ||
912ad3ad064SGleb Smirnoff 		    V_blackhole == 2) && (V_blackhole_local || (
9133ea9a7cfSGleb Smirnoff #ifdef INET6
9143ea9a7cfSGleb Smirnoff 		    isipv6 ? !in6_localaddr(&ip6->ip6_src) :
9153ea9a7cfSGleb Smirnoff #endif
9163ea9a7cfSGleb Smirnoff #ifdef INET
9173ea9a7cfSGleb Smirnoff 		    !in_localip(ip->ip_src)
9183ea9a7cfSGleb Smirnoff #else
9193ea9a7cfSGleb Smirnoff 		    true
9203ea9a7cfSGleb Smirnoff #endif
921ad3ad064SGleb Smirnoff 		    )))
9221929eae1SAndre Oppermann 			goto dropunlock;
923574b6964SAndre Oppermann 
924a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
925a57815efSBosko Milekic 		goto dropwithreset;
926816a3d83SPoul-Henning Kamp 	}
92708d9c920SGleb Smirnoff 	INP_LOCK_ASSERT(inp);
928f567d55fSGleb Smirnoff 
929c2529042SHans Petter Selasky 	if ((inp->inp_flowtype == M_HASHTYPE_NONE) &&
930f71cb9f7SGleb Smirnoff 	    !SOLISTENING(inp->inp_socket)) {
931e5738ee0SCheng Cui 		if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
93280cb9f21SKip Macy 			inp->inp_flowid = m->m_pkthdr.flowid;
9332f719932SAdrian Chadd 			inp->inp_flowtype = M_HASHTYPE_GET(m);
934e5738ee0SCheng Cui #ifdef	RSS
935e5738ee0SCheng Cui 		} else {
936e5738ee0SCheng Cui 			  /* assign flowid by software RSS hash */
937e5738ee0SCheng Cui #ifdef INET6
938e5738ee0SCheng Cui 			  if (isipv6) {
939e5738ee0SCheng Cui 				rss_proto_software_hash_v6(&inp->in6p_faddr,
940e5738ee0SCheng Cui 							   &inp->in6p_laddr,
941e5738ee0SCheng Cui 							   inp->inp_fport,
942e5738ee0SCheng Cui 							   inp->inp_lport,
943e5738ee0SCheng Cui 							   IPPROTO_TCP,
944e5738ee0SCheng Cui 							   &inp->inp_flowid,
945e5738ee0SCheng Cui 							   &inp->inp_flowtype);
946e5738ee0SCheng Cui 			  } else
947e5738ee0SCheng Cui #endif	/* INET6 */
948e5738ee0SCheng Cui 			  {
949e5738ee0SCheng Cui 				rss_proto_software_hash_v4(inp->inp_faddr,
950e5738ee0SCheng Cui 							   inp->inp_laddr,
951e5738ee0SCheng Cui 							   inp->inp_fport,
952e5738ee0SCheng Cui 							   inp->inp_lport,
953e5738ee0SCheng Cui 							   IPPROTO_TCP,
954e5738ee0SCheng Cui 							   &inp->inp_flowid,
955e5738ee0SCheng Cui 							   &inp->inp_flowtype);
956e5738ee0SCheng Cui 			  }
957e5738ee0SCheng Cui #endif	/* RSS */
958e5738ee0SCheng Cui 		}
95980cb9f21SKip Macy 	}
960fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
961a2589465SKen Smith #ifdef INET6
962fcf59617SAndrey V. Elsukov 	if (isipv6 && IPSEC_ENABLED(ipv6) &&
963fcf59617SAndrey V. Elsukov 	    IPSEC_CHECK_POLICY(ipv6, m, inp) != 0) {
964a2589465SKen Smith 		goto dropunlock;
965a2589465SKen Smith 	}
966fcf59617SAndrey V. Elsukov #ifdef INET
967fcf59617SAndrey V. Elsukov 	else
968fcf59617SAndrey V. Elsukov #endif
969fcf59617SAndrey V. Elsukov #endif /* INET6 */
970fcf59617SAndrey V. Elsukov #ifdef INET
971fcf59617SAndrey V. Elsukov 	if (IPSEC_ENABLED(ipv4) &&
972fcf59617SAndrey V. Elsukov 	    IPSEC_CHECK_POLICY(ipv4, m, inp) != 0) {
973fcf59617SAndrey V. Elsukov 		goto dropunlock;
974fcf59617SAndrey V. Elsukov 	}
975fcf59617SAndrey V. Elsukov #endif /* INET */
976a2589465SKen Smith #endif /* IPSEC */
977a2589465SKen Smith 
9788d573cc1SAndre Oppermann 	/*
9798d573cc1SAndre Oppermann 	 * Check the minimum TTL for socket.
9808d573cc1SAndre Oppermann 	 */
98134f83c52SGeorge V. Neville-Neil 	if (inp->inp_ip_minttl != 0) {
98234f83c52SGeorge V. Neville-Neil #ifdef INET6
9832d8868dbSJonathan T. Looney 		if (isipv6) {
9842d8868dbSJonathan T. Looney 			if (inp->inp_ip_minttl > ip6->ip6_hlim)
985302ce8d6SAndre Oppermann 				goto dropunlock;
9862d8868dbSJonathan T. Looney 		} else
98734f83c52SGeorge V. Neville-Neil #endif
98834f83c52SGeorge V. Neville-Neil 		if (inp->inp_ip_minttl > ip->ip_ttl)
989302ce8d6SAndre Oppermann 			goto dropunlock;
99034f83c52SGeorge V. Neville-Neil 	}
991936cd18dSAndre Oppermann 
9920d744519SGleb Smirnoff 	tp = intotcpcb(inp);
9930d744519SGleb Smirnoff 	switch (tp->t_state) {
9940d744519SGleb Smirnoff 	case TCPS_TIME_WAIT:
995340c35deSJonathan Lemon 		/*
9960d744519SGleb Smirnoff 		 * A previous connection in TIMEWAIT state is supposed to catch
9970d744519SGleb Smirnoff 		 * stray or duplicate segments arriving late.  If this segment
9980d744519SGleb Smirnoff 		 * was a legitimate new connection attempt, the old INPCB gets
9990d744519SGleb Smirnoff 		 * removed and we can try again to find a listening socket.
1000340c35deSJonathan Lemon 		 */
1001283c76c7SMichael Tuexen 		tcp_dooptions(&to, optp, optlen,
1002283c76c7SMichael Tuexen 		    (thflags & TH_SYN) ? TO_SYN : 0);
10038d573cc1SAndre Oppermann 		/*
10040d744519SGleb Smirnoff 		 * tcp_twcheck unlocks the inp always, and frees the m if fails.
10058d573cc1SAndre Oppermann 		 */
10062104448fSAndre Oppermann 		if (tcp_twcheck(inp, &to, th, m, tlen))
1007340c35deSJonathan Lemon 			goto findpcb;
10088f5a8818SKevin Lo 		return (IPPROTO_DONE);
10090d744519SGleb Smirnoff 	case TCPS_CLOSED:
1010794235b7SAndre Oppermann 		/*
1011794235b7SAndre Oppermann 		 * The TCPCB may no longer exist if the connection is winding
1012794235b7SAndre Oppermann 		 * down or it is in the CLOSED state.  Either way we drop the
1013794235b7SAndre Oppermann 		 * segment and send an appropriate response.
1014794235b7SAndre Oppermann 		 */
1015a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
1016a57815efSBosko Milekic 		goto dropwithreset;
101709f81a46SBosko Milekic 	}
1018df8bae1dSRodney W. Grimes 
10199e644c23SMichael Tuexen 	if ((tp->t_port != port) && (tp->t_state > TCPS_LISTEN)) {
10209e644c23SMichael Tuexen 		rstreason = BANDLIM_RST_CLOSEDPORT;
10219e644c23SMichael Tuexen 		goto dropwithreset;
10229e644c23SMichael Tuexen 	}
10239e644c23SMichael Tuexen 
102409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
102509fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
102609fe6320SNavdeep Parhar 		tcp_offload_input(tp, m);
102709fe6320SNavdeep Parhar 		m = NULL;	/* consumed by the TOE driver */
102809fe6320SNavdeep Parhar 		goto dropunlock;
102909fe6320SNavdeep Parhar 	}
103009fe6320SNavdeep Parhar #endif
103109fe6320SNavdeep Parhar 
1032c488362eSRobert Watson #ifdef MAC
103330d239bcSRobert Watson 	if (mac_inpcb_check_deliver(inp, m))
1034302ce8d6SAndre Oppermann 		goto dropunlock;
1035c488362eSRobert Watson #endif
1036a557af22SRobert Watson 	so = inp->inp_socket;
1037302ce8d6SAndre Oppermann 	KASSERT(so != NULL, ("%s: so == NULL", __func__));
1038db33b3e6SAndre Oppermann 	/*
1039db33b3e6SAndre Oppermann 	 * When the socket is accepting connections (the INPCB is in LISTEN
1040db33b3e6SAndre Oppermann 	 * state) we look into the SYN cache if this is a new connection
1041a7c7f2a7SJohn Baldwin 	 * attempt or the completion of a previous one.
1042db33b3e6SAndre Oppermann 	 */
1043f4bb1869SMark Johnston 	KASSERT(tp->t_state == TCPS_LISTEN || !SOLISTENING(so),
104445274760SJonathan T. Looney 	    ("%s: so accepting but tp %p not listening", __func__, tp));
1045f4bb1869SMark Johnston 	if (tp->t_state == TCPS_LISTEN && SOLISTENING(so)) {
1046540e8b7eSJeffrey Hsu 		struct in_conninfo inc;
1047540e8b7eSJeffrey Hsu 
10488bfb1918SAndre Oppermann 		bzero(&inc, sizeof(inc));
1049302ce8d6SAndre Oppermann #ifdef INET6
1050be2ac88cSJonathan Lemon 		if (isipv6) {
1051dcdb4371SBjoern A. Zeeb 			inc.inc_flags |= INC_ISIPV6;
10525dff1c38SMichael Tuexen 			if (inp->inp_inc.inc_flags & INC_IPV6MINMTU)
10535dff1c38SMichael Tuexen 				inc.inc_flags |= INC_IPV6MINMTU;
1054be2ac88cSJonathan Lemon 			inc.inc6_faddr = ip6->ip6_src;
1055be2ac88cSJonathan Lemon 			inc.inc6_laddr = ip6->ip6_dst;
1056302ce8d6SAndre Oppermann 		} else
1057302ce8d6SAndre Oppermann #endif
1058302ce8d6SAndre Oppermann 		{
1059be2ac88cSJonathan Lemon 			inc.inc_faddr = ip->ip_src;
1060be2ac88cSJonathan Lemon 			inc.inc_laddr = ip->ip_dst;
1061be2ac88cSJonathan Lemon 		}
1062be2ac88cSJonathan Lemon 		inc.inc_fport = th->th_sport;
1063be2ac88cSJonathan Lemon 		inc.inc_lport = th->th_dport;
10647973fba3SJulian Elischer 		inc.inc_fibnum = so->so_fibnum;
1065be2ac88cSJonathan Lemon 
1066fb59c426SYoshinobu Inoue 		/*
10678d573cc1SAndre Oppermann 		 * Check for an existing connection attempt in syncache if
10688d573cc1SAndre Oppermann 		 * the flag is only ACK.  A successful lookup creates a new
10698d573cc1SAndre Oppermann 		 * socket appended to the listen queue in SYN_RECEIVED state.
1070fb59c426SYoshinobu Inoue 		 */
1071be2ac88cSJonathan Lemon 		if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
1072bf6d304aSAndre Oppermann 			/*
1073bf6d304aSAndre Oppermann 			 * Parse the TCP options here because
1074bf6d304aSAndre Oppermann 			 * syncookies need access to the reflected
1075bf6d304aSAndre Oppermann 			 * timestamp.
1076bf6d304aSAndre Oppermann 			 */
1077bf6d304aSAndre Oppermann 			tcp_dooptions(&to, optp, optlen, 0);
10789fa198beSAndre Oppermann 			/*
1079e7231d07SGleb Smirnoff 			 * NB: syncache_expand() doesn't unlock inp.
10809fa198beSAndre Oppermann 			 */
10819e644c23SMichael Tuexen 			rstreason = syncache_expand(&inc, &to, th, &so, m, port);
1082fcf59617SAndrey V. Elsukov 			if (rstreason < 0) {
1083fcf59617SAndrey V. Elsukov 				/*
1084fcf59617SAndrey V. Elsukov 				 * A failing TCP MD5 signature comparison
1085fcf59617SAndrey V. Elsukov 				 * must result in the segment being dropped
1086fcf59617SAndrey V. Elsukov 				 * and must not produce any response back
1087fcf59617SAndrey V. Elsukov 				 * to the sender.
1088fcf59617SAndrey V. Elsukov 				 */
1089fcf59617SAndrey V. Elsukov 				goto dropunlock;
1090fcf59617SAndrey V. Elsukov 			} else if (rstreason == 0) {
10914195b4afSPaul Traina 				/*
1092d88eb465SGleb Smirnoff 				 * No syncache entry, or ACK was not for our
1093d88eb465SGleb Smirnoff 				 * SYN/ACK.  Do our protection against double
1094d88eb465SGleb Smirnoff 				 * ACK.  If peer sent us 2 ACKs, then for the
1095d88eb465SGleb Smirnoff 				 * first one syncache_expand() successfully
1096d88eb465SGleb Smirnoff 				 * converted syncache entry into a socket,
1097d88eb465SGleb Smirnoff 				 * while we were waiting on the inpcb lock.  We
1098d88eb465SGleb Smirnoff 				 * don't want to sent RST for the second ACK,
1099d88eb465SGleb Smirnoff 				 * so we perform second lookup without wildcard
1100d88eb465SGleb Smirnoff 				 * match, hoping to find the new socket.  If
1101d88eb465SGleb Smirnoff 				 * the ACK is stray indeed, rstreason would
1102d88eb465SGleb Smirnoff 				 * hint the above code that the lookup was a
1103d88eb465SGleb Smirnoff 				 * second attempt.
1104d88eb465SGleb Smirnoff 				 *
11058d573cc1SAndre Oppermann 				 * NB: syncache did its own logging
11068d573cc1SAndre Oppermann 				 * of the failure cause.
11074195b4afSPaul Traina 				 */
1108d88eb465SGleb Smirnoff 				INP_WUNLOCK(inp);
1109be2ac88cSJonathan Lemon 				rstreason = BANDLIM_RST_OPENPORT;
1110d88eb465SGleb Smirnoff 				lookupflag &= ~INPLOOKUP_WILDCARD;
1111d88eb465SGleb Smirnoff 				goto findpcb;
1112be2ac88cSJonathan Lemon 			}
111309c305ebSPatrick Kelsey tfo_socket_result:
1114f76fcf6dSJeffrey Hsu 			if (so == NULL) {
1115be2ac88cSJonathan Lemon 				/*
1116e207f800SAndre Oppermann 				 * We completed the 3-way handshake
1117e207f800SAndre Oppermann 				 * but could not allocate a socket
1118e207f800SAndre Oppermann 				 * either due to memory shortage,
1119e207f800SAndre Oppermann 				 * listen queue length limits or
1120a160e630SAndre Oppermann 				 * global socket limits.  Send RST
1121a160e630SAndre Oppermann 				 * or wait and have the remote end
1122a160e630SAndre Oppermann 				 * retransmit the ACK for another
1123a160e630SAndre Oppermann 				 * try.
1124be2ac88cSJonathan Lemon 				 */
1125a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1126a160e630SAndre Oppermann 					log(LOG_DEBUG, "%s; %s: Listen socket: "
11278d573cc1SAndre Oppermann 					    "Socket allocation failed due to "
11288d573cc1SAndre Oppermann 					    "limits or memory shortage, %s\n",
1129ac957cd2SJulian Elischer 					    s, __func__,
1130ac957cd2SJulian Elischer 					    V_tcp_sc_rst_sock_fail ?
1131ac957cd2SJulian Elischer 					    "sending RST" : "try again");
1132603724d3SBjoern A. Zeeb 				if (V_tcp_sc_rst_sock_fail) {
1133e207f800SAndre Oppermann 					rstreason = BANDLIM_UNLIMITED;
1134e207f800SAndre Oppermann 					goto dropwithreset;
1135a160e630SAndre Oppermann 				} else
1136a160e630SAndre Oppermann 					goto dropunlock;
1137f76fcf6dSJeffrey Hsu 			}
1138be2ac88cSJonathan Lemon 			/*
1139be2ac88cSJonathan Lemon 			 * Socket is created in state SYN_RECEIVED.
11408d573cc1SAndre Oppermann 			 * Unlock the listen socket, lock the newly
11418d573cc1SAndre Oppermann 			 * created socket and update the tp variable.
114208d9c920SGleb Smirnoff 			 * If we came here via jump to tfo_socket_result,
114308d9c920SGleb Smirnoff 			 * then listening socket is read-locked.
1144be2ac88cSJonathan Lemon 			 */
114508d9c920SGleb Smirnoff 			INP_UNLOCK(inp);	/* listen socket */
1146be2ac88cSJonathan Lemon 			inp = sotoinpcb(so);
1147ff9b006dSJulien Charbon 			/*
1148ff9b006dSJulien Charbon 			 * New connection inpcb is already locked by
1149ff9b006dSJulien Charbon 			 * syncache_expand().
1150ff9b006dSJulien Charbon 			 */
1151ff9b006dSJulien Charbon 			INP_WLOCK_ASSERT(inp);
1152be2ac88cSJonathan Lemon 			tp = intotcpcb(inp);
11538d573cc1SAndre Oppermann 			KASSERT(tp->t_state == TCPS_SYN_RECEIVED,
11548d573cc1SAndre Oppermann 			    ("%s: ", __func__));
1155be2ac88cSJonathan Lemon 			/*
1156302ce8d6SAndre Oppermann 			 * Process the segment and the data it
1157302ce8d6SAndre Oppermann 			 * contains.  tcp_do_segment() consumes
1158302ce8d6SAndre Oppermann 			 * the mbuf chain and unlocks the inpcb.
1159302ce8d6SAndre Oppermann 			 */
11606138da62SMichael Tuexen 			TCP_PROBE5(receive, NULL, tp, m, tp, th);
116135bc0bccSGleb Smirnoff 			tp->t_fb->tfb_tcp_do_segment(tp, m, th, drop_hdrlen,
116235bc0bccSGleb Smirnoff 			    tlen, iptos);
11638f5a8818SKevin Lo 			return (IPPROTO_DONE);
1164be2ac88cSJonathan Lemon 		}
1165a160e630SAndre Oppermann 		/*
11668d573cc1SAndre Oppermann 		 * Segment flag validation for new connection attempts:
11678d573cc1SAndre Oppermann 		 *
1168a160e630SAndre Oppermann 		 * Our (SYN|ACK) response was rejected.
1169a160e630SAndre Oppermann 		 * Check with syncache and remove entry to prevent
1170a160e630SAndre Oppermann 		 * retransmits.
117119bc77c5SAndre Oppermann 		 *
117219bc77c5SAndre Oppermann 		 * NB: syncache_chkrst does its own logging of failure
117319bc77c5SAndre Oppermann 		 * causes.
1174a160e630SAndre Oppermann 		 */
1175a160e630SAndre Oppermann 		if (thflags & TH_RST) {
11769e644c23SMichael Tuexen 			syncache_chkrst(&inc, th, m, port);
1177a160e630SAndre Oppermann 			goto dropunlock;
1178a160e630SAndre Oppermann 		}
1179a160e630SAndre Oppermann 		/*
1180a160e630SAndre Oppermann 		 * We can't do anything without SYN.
1181a160e630SAndre Oppermann 		 */
1182a160e630SAndre Oppermann 		if ((thflags & TH_SYN) == 0) {
1183a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1184a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1185773673c1SAndre Oppermann 				    "SYN is missing, segment ignored\n",
11868d573cc1SAndre Oppermann 				    s, __func__);
118778b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1188a160e630SAndre Oppermann 			goto dropunlock;
1189a160e630SAndre Oppermann 		}
1190a160e630SAndre Oppermann 		/*
1191a160e630SAndre Oppermann 		 * (SYN|ACK) is bogus on a listen socket.
1192a160e630SAndre Oppermann 		 */
1193fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1194a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1195a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
11968d573cc1SAndre Oppermann 				    "SYN|ACK invalid, segment rejected\n",
11978d573cc1SAndre Oppermann 				    s, __func__);
11989e644c23SMichael Tuexen 			syncache_badack(&inc, port);	/* XXX: Not needed! */
119978b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1200a57815efSBosko Milekic 			rstreason = BANDLIM_RST_OPENPORT;
1201a57815efSBosko Milekic 			goto dropwithreset;
12024195b4afSPaul Traina 		}
1203a160e630SAndre Oppermann 		/*
1204a160e630SAndre Oppermann 		 * If the drop_synfin option is enabled, drop all
1205a160e630SAndre Oppermann 		 * segments with both the SYN and FIN bits set.
1206a160e630SAndre Oppermann 		 * This prevents e.g. nmap from identifying the
1207a160e630SAndre Oppermann 		 * TCP/IP stack.
1208a160e630SAndre Oppermann 		 * XXX: Poor reasoning.  nmap has other methods
1209a160e630SAndre Oppermann 		 * and is constantly refining its stack detection
1210a160e630SAndre Oppermann 		 * strategies.
12118d573cc1SAndre Oppermann 		 * XXX: This is a violation of the TCP specification
1212a160e630SAndre Oppermann 		 * and was used by RFC1644.
1213a160e630SAndre Oppermann 		 */
1214603724d3SBjoern A. Zeeb 		if ((thflags & TH_FIN) && V_drop_synfin) {
1215a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1216a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1217773673c1SAndre Oppermann 				    "SYN|FIN segment ignored (based on "
12188d573cc1SAndre Oppermann 				    "sysctl setting)\n", s, __func__);
121978b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1220302ce8d6SAndre Oppermann 			goto dropunlock;
1221ebb0cbeaSPaul Traina 		}
1222be2ac88cSJonathan Lemon 		/*
1223be2ac88cSJonathan Lemon 		 * Segment's flags are (SYN) or (SYN|FIN).
1224a160e630SAndre Oppermann 		 *
1225a160e630SAndre Oppermann 		 * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored
1226a160e630SAndre Oppermann 		 * as they do not affect the state of the TCP FSM.
1227a160e630SAndre Oppermann 		 * The data pointed to by TH_URG and th_urp is ignored.
1228be2ac88cSJonathan Lemon 		 */
1229a160e630SAndre Oppermann 		KASSERT((thflags & (TH_RST|TH_ACK)) == 0,
1230a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_RST or TH_ACK set", __func__));
1231a160e630SAndre Oppermann 		KASSERT(thflags & (TH_SYN),
1232a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_SYN not set", __func__));
123308d9c920SGleb Smirnoff 		INP_RLOCK_ASSERT(inp);
123433841545SHajimu UMEMOTO #ifdef INET6
123533841545SHajimu UMEMOTO 		/*
123633841545SHajimu UMEMOTO 		 * If deprecated address is forbidden,
123733841545SHajimu UMEMOTO 		 * we do not accept SYN to deprecated interface
123833841545SHajimu UMEMOTO 		 * address to prevent any new inbound connection from
123933841545SHajimu UMEMOTO 		 * getting established.
124033841545SHajimu UMEMOTO 		 * When we do not accept SYN, we send a TCP RST,
124133841545SHajimu UMEMOTO 		 * with deprecated source address (instead of dropping
124233841545SHajimu UMEMOTO 		 * it).  We compromise it as it is much better for peer
124333841545SHajimu UMEMOTO 		 * to send a RST, and RST will be the final packet
124433841545SHajimu UMEMOTO 		 * for the exchange.
124533841545SHajimu UMEMOTO 		 *
124633841545SHajimu UMEMOTO 		 * If we do not forbid deprecated addresses, we accept
124733841545SHajimu UMEMOTO 		 * the SYN packet.  RFC2462 does not suggest dropping
124833841545SHajimu UMEMOTO 		 * SYN in this case.
124933841545SHajimu UMEMOTO 		 * If we decipher RFC2462 5.5.4, it says like this:
125033841545SHajimu UMEMOTO 		 * 1. use of deprecated addr with existing
125133841545SHajimu UMEMOTO 		 *    communication is okay - "SHOULD continue to be
125233841545SHajimu UMEMOTO 		 *    used"
125333841545SHajimu UMEMOTO 		 * 2. use of it with new communication:
125433841545SHajimu UMEMOTO 		 *   (2a) "SHOULD NOT be used if alternate address
125533841545SHajimu UMEMOTO 		 *        with sufficient scope is available"
125633841545SHajimu UMEMOTO 		 *   (2b) nothing mentioned otherwise.
125733841545SHajimu UMEMOTO 		 * Here we fall into (2b) case as we have no choice in
125833841545SHajimu UMEMOTO 		 * our source address selection - we must obey the peer.
125933841545SHajimu UMEMOTO 		 *
126033841545SHajimu UMEMOTO 		 * The wording in RFC2462 is confusing, and there are
126133841545SHajimu UMEMOTO 		 * multiple description text for deprecated address
126233841545SHajimu UMEMOTO 		 * handling - worse, they are not exactly the same.
126333841545SHajimu UMEMOTO 		 * I believe 5.5.4 is the best one, so we follow 5.5.4.
126433841545SHajimu UMEMOTO 		 */
1265603724d3SBjoern A. Zeeb 		if (isipv6 && !V_ip6_use_deprecated) {
126633841545SHajimu UMEMOTO 			struct in6_ifaddr *ia6;
126733841545SHajimu UMEMOTO 
12688268d82cSAlexander V. Chernikov 			ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false);
12698c0fec80SRobert Watson 			if (ia6 != NULL &&
127033841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
1271a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1272a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12738d573cc1SAndre Oppermann 					"Connection attempt to deprecated "
12748d573cc1SAndre Oppermann 					"IPv6 address rejected\n",
1275a160e630SAndre Oppermann 					s, __func__);
127633841545SHajimu UMEMOTO 				rstreason = BANDLIM_RST_OPENPORT;
127733841545SHajimu UMEMOTO 				goto dropwithreset;
127833841545SHajimu UMEMOTO 			}
127933841545SHajimu UMEMOTO 		}
1280b287c6c7SBjoern A. Zeeb #endif /* INET6 */
128165f28919SJesper Skriver 		/*
1282db33b3e6SAndre Oppermann 		 * Basic sanity checks on incoming SYN requests:
12838d573cc1SAndre Oppermann 		 *   Don't respond if the destination is a link layer
1284db33b3e6SAndre Oppermann 		 *	broadcast according to RFC1122 4.2.3.10, p. 104.
12858d573cc1SAndre Oppermann 		 *   If it is from this socket it must be forged.
12868d573cc1SAndre Oppermann 		 *   Don't respond if the source or destination is a
12878d573cc1SAndre Oppermann 		 *	global or subnet broad- or multicast address.
128893ec91baSCrist J. Clark 		 *   Note that it is quite possible to receive unicast
128993ec91baSCrist J. Clark 		 *	link-layer packets with a broadcast IP address. Use
129093ec91baSCrist J. Clark 		 *	in_broadcast() to find them.
1291be2ac88cSJonathan Lemon 		 */
12928d573cc1SAndre Oppermann 		if (m->m_flags & (M_BCAST|M_MCAST)) {
12938d573cc1SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
12948d573cc1SAndre Oppermann 			    log(LOG_DEBUG, "%s; %s: Listen socket: "
12958d573cc1SAndre Oppermann 				"Connection attempt from broad- or multicast "
1296773673c1SAndre Oppermann 				"link layer address ignored\n", s, __func__);
1297302ce8d6SAndre Oppermann 			goto dropunlock;
12988d573cc1SAndre Oppermann 		}
1299db33b3e6SAndre Oppermann #ifdef INET6
1300b287c6c7SBjoern A. Zeeb 		if (isipv6) {
1301db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1302a160e630SAndre Oppermann 			    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) {
1303a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1304a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13058d573cc1SAndre Oppermann 					"Connection attempt to/from self "
1306773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1307302ce8d6SAndre Oppermann 				goto dropunlock;
1308a160e630SAndre Oppermann 			}
1309be2ac88cSJonathan Lemon 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1310a160e630SAndre Oppermann 			    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
1311a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1312a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13138d573cc1SAndre Oppermann 					"Connection attempt from/to multicast "
1314773673c1SAndre Oppermann 					"address ignored\n", s, __func__);
1315302ce8d6SAndre Oppermann 				goto dropunlock;
1316a160e630SAndre Oppermann 			}
1317b287c6c7SBjoern A. Zeeb 		}
1318db33b3e6SAndre Oppermann #endif
1319b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
1320b287c6c7SBjoern A. Zeeb 		else
1321b287c6c7SBjoern A. Zeeb #endif
1322b287c6c7SBjoern A. Zeeb #ifdef INET
1323b287c6c7SBjoern A. Zeeb 		{
1324db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1325a160e630SAndre Oppermann 			    ip->ip_dst.s_addr == ip->ip_src.s_addr) {
1326a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1327a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13288d573cc1SAndre Oppermann 					"Connection attempt from/to self "
1329773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1330302ce8d6SAndre Oppermann 				goto dropunlock;
1331a160e630SAndre Oppermann 			}
1332be2ac88cSJonathan Lemon 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
1333be2ac88cSJonathan Lemon 			    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
13342ca2159fSCrist J. Clark 			    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
1335a160e630SAndre Oppermann 			    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
1336a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1337a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13388d573cc1SAndre Oppermann 					"Connection attempt from/to broad- "
1339773673c1SAndre Oppermann 					"or multicast address ignored\n",
1340a160e630SAndre Oppermann 					s, __func__);
1341302ce8d6SAndre Oppermann 				goto dropunlock;
1342c068736aSJeffrey Hsu 			}
1343a160e630SAndre Oppermann 		}
1344b287c6c7SBjoern A. Zeeb #endif
1345be2ac88cSJonathan Lemon 		/*
1346db33b3e6SAndre Oppermann 		 * SYN appears to be valid.  Create compressed TCP state
1347db33b3e6SAndre Oppermann 		 * for syncache.
1348be2ac88cSJonathan Lemon 		 */
13492b9c9984SGeorge V. Neville-Neil 		TCP_PROBE3(debug__input, tp, th, m);
1350f72167f4SAndre Oppermann 		tcp_dooptions(&to, optp, optlen, TO_SYN);
13518d5719aaSGleb Smirnoff 		if ((so = syncache_add(&inc, &to, th, inp, so, m, NULL, NULL,
13529e644c23SMichael Tuexen 		    iptos, port)) != NULL)
135309c305ebSPatrick Kelsey 			goto tfo_socket_result;
135418a75309SPatrick Kelsey 
1355be2ac88cSJonathan Lemon 		/*
13564d6e7130SAndre Oppermann 		 * Entry added to syncache and mbuf consumed.
1357a7c7f2a7SJohn Baldwin 		 * Only the listen socket is unlocked by syncache_add().
1358be2ac88cSJonathan Lemon 		 */
13598f5a8818SKevin Lo 		return (IPPROTO_DONE);
1360982c1675SAndre Oppermann 	} else if (tp->t_state == TCPS_LISTEN) {
1361982c1675SAndre Oppermann 		/*
1362982c1675SAndre Oppermann 		 * When a listen socket is torn down the SO_ACCEPTCONN
1363982c1675SAndre Oppermann 		 * flag is removed first while connections are drained
1364982c1675SAndre Oppermann 		 * from the accept queue in a unlock/lock cycle of the
1365982c1675SAndre Oppermann 		 * ACCEPT_LOCK, opening a race condition allowing a SYN
1366982c1675SAndre Oppermann 		 * attempt go through unhandled.
1367982c1675SAndre Oppermann 		 */
1368982c1675SAndre Oppermann 		goto dropunlock;
1369f76fcf6dSJeffrey Hsu 	}
1370fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1371fcf59617SAndrey V. Elsukov 	if (tp->t_flags & TF_SIGNATURE) {
1372fcf59617SAndrey V. Elsukov 		tcp_dooptions(&to, optp, optlen, thflags);
1373fcf59617SAndrey V. Elsukov 		if ((to.to_flags & TOF_SIGNATURE) == 0) {
1374fcf59617SAndrey V. Elsukov 			TCPSTAT_INC(tcps_sig_err_nosigopt);
13752903309aSAttilio Rao 			goto dropunlock;
13762903309aSAttilio Rao 		}
1377fcf59617SAndrey V. Elsukov 		if (!TCPMD5_ENABLED() ||
1378fcf59617SAndrey V. Elsukov 		    TCPMD5_INPUT(m, th, to.to_signature) != 0)
1379fcf59617SAndrey V. Elsukov 			goto dropunlock;
13802903309aSAttilio Rao 	}
13812903309aSAttilio Rao #endif
13822b9c9984SGeorge V. Neville-Neil 	TCP_PROBE5(receive, NULL, tp, m, tp, th);
138357f60867SMark Johnston 
1384302ce8d6SAndre Oppermann 	/*
13858d573cc1SAndre Oppermann 	 * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later
13868d573cc1SAndre Oppermann 	 * state.  tcp_do_segment() always consumes the mbuf chain, unlocks
13878d573cc1SAndre Oppermann 	 * the inpcb, and unlocks pcbinfo.
13881db08fbeSGleb Smirnoff 	 *
13891db08fbeSGleb Smirnoff 	 * XXXGL: in case of a pure SYN arriving on existing connection
13901db08fbeSGleb Smirnoff 	 * TCP stacks won't need to modify the PCB, they would either drop
13911db08fbeSGleb Smirnoff 	 * the segment silently, or send a challenge ACK.  However, we try
13921db08fbeSGleb Smirnoff 	 * to upgrade the lock, because calling convention for stacks is
13931db08fbeSGleb Smirnoff 	 * write-lock on PCB.  If upgrade fails, drop the SYN.
1394302ce8d6SAndre Oppermann 	 */
1395d88eb465SGleb Smirnoff 	if ((lookupflag & INPLOOKUP_RLOCKPCB) && INP_TRY_UPGRADE(inp) == 0)
13961db08fbeSGleb Smirnoff 		goto dropunlock;
13971db08fbeSGleb Smirnoff 
139835bc0bccSGleb Smirnoff 	tp->t_fb->tfb_tcp_do_segment(tp, m, th, drop_hdrlen, tlen, iptos);
13998f5a8818SKevin Lo 	return (IPPROTO_DONE);
1400be2ac88cSJonathan Lemon 
1401302ce8d6SAndre Oppermann dropwithreset:
14022b9c9984SGeorge V. Neville-Neil 	TCP_PROBE5(receive, NULL, tp, m, tp, th);
140357f60867SMark Johnston 
1404014ea782SRobert Watson 	if (inp != NULL) {
1405302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
140608d9c920SGleb Smirnoff 		INP_UNLOCK(inp);
1407dd8ac7f9SRobert Watson 	} else
1408014ea782SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
1409302ce8d6SAndre Oppermann 	m = NULL;	/* mbuf chain got consumed. */
1410014ea782SRobert Watson 	goto drop;
1411014ea782SRobert Watson 
1412302ce8d6SAndre Oppermann dropunlock:
141357f60867SMark Johnston 	if (m != NULL)
14142b9c9984SGeorge V. Neville-Neil 		TCP_PROBE5(receive, NULL, tp, m, tp, th);
141557f60867SMark Johnston 
14169fa198beSAndre Oppermann 	if (inp != NULL)
141708d9c920SGleb Smirnoff 		INP_UNLOCK(inp);
1418014ea782SRobert Watson 
1419302ce8d6SAndre Oppermann drop:
1420a160e630SAndre Oppermann 	if (s != NULL)
1421a160e630SAndre Oppermann 		free(s, M_TCPLOG);
1422302ce8d6SAndre Oppermann 	if (m != NULL)
1423302ce8d6SAndre Oppermann 		m_freem(m);
14248f5a8818SKevin Lo 	return (IPPROTO_DONE);
1425302ce8d6SAndre Oppermann }
1426302ce8d6SAndre Oppermann 
1427e44c1887SSteven Hartland /*
1428e44c1887SSteven Hartland  * Automatic sizing of receive socket buffer.  Often the send
1429e44c1887SSteven Hartland  * buffer size is not optimally adjusted to the actual network
1430e44c1887SSteven Hartland  * conditions at hand (delay bandwidth product).  Setting the
1431e44c1887SSteven Hartland  * buffer size too small limits throughput on links with high
1432e44c1887SSteven Hartland  * bandwidth and high delay (eg. trans-continental/oceanic links).
1433e44c1887SSteven Hartland  *
1434e44c1887SSteven Hartland  * On the receive side the socket buffer memory is only rarely
1435e44c1887SSteven Hartland  * used to any significant extent.  This allows us to be much
1436e44c1887SSteven Hartland  * more aggressive in scaling the receive socket buffer.  For
1437e44c1887SSteven Hartland  * the case that the buffer space is actually used to a large
1438e44c1887SSteven Hartland  * extent and we run out of kernel memory we can simply drop
1439e44c1887SSteven Hartland  * the new segments; TCP on the sender will just retransmit it
1440e44c1887SSteven Hartland  * later.  Setting the buffer size too big may only consume too
1441e44c1887SSteven Hartland  * much kernel memory if the application doesn't read() from
1442e44c1887SSteven Hartland  * the socket or packet loss or reordering makes use of the
1443e44c1887SSteven Hartland  * reassembly queue.
1444e44c1887SSteven Hartland  *
1445e44c1887SSteven Hartland  * The criteria to step up the receive buffer one notch are:
1446e44c1887SSteven Hartland  *  1. Application has not set receive buffer size with
1447e44c1887SSteven Hartland  *     SO_RCVBUF. Setting SO_RCVBUF clears SB_AUTOSIZE.
1448560c0586SMichael Tuexen  *  2. the number of bytes received during 1/2 of an sRTT
1449560c0586SMichael Tuexen  *     is at least 3/8 of the current socket buffer size.
1450560c0586SMichael Tuexen  *  3. receive buffer size has not hit maximal automatic size;
1451e44c1887SSteven Hartland  *
1452560c0586SMichael Tuexen  * If all of the criteria are met we increaset the socket buffer
1453560c0586SMichael Tuexen  * by a 1/2 (bounded by the max). This allows us to keep ahead
1454560c0586SMichael Tuexen  * of slow-start but also makes it so our peer never gets limited
1455560c0586SMichael Tuexen  * by our rwnd which we then open up causing a burst.
1456560c0586SMichael Tuexen  *
1457560c0586SMichael Tuexen  * This algorithm does two steps per RTT at most and only if
1458e44c1887SSteven Hartland  * we receive a bulk stream w/o packet losses or reorderings.
1459e44c1887SSteven Hartland  * Shrinking the buffer during idle times is not necessary as
1460e44c1887SSteven Hartland  * it doesn't consume any memory when idle.
1461e44c1887SSteven Hartland  *
1462e44c1887SSteven Hartland  * TODO: Only step up if the application is actually serving
1463e44c1887SSteven Hartland  * the buffer to better manage the socket buffer resources.
1464e44c1887SSteven Hartland  */
1465e44c1887SSteven Hartland int
1466e44c1887SSteven Hartland tcp_autorcvbuf(struct mbuf *m, struct tcphdr *th, struct socket *so,
1467e44c1887SSteven Hartland     struct tcpcb *tp, int tlen)
1468e44c1887SSteven Hartland {
1469e44c1887SSteven Hartland 	int newsize = 0;
1470e44c1887SSteven Hartland 
1471e44c1887SSteven Hartland 	if (V_tcp_do_autorcvbuf && (so->so_rcv.sb_flags & SB_AUTOSIZE) &&
1472e44c1887SSteven Hartland 	    tp->t_srtt != 0 && tp->rfbuf_ts != 0 &&
1473e44c1887SSteven Hartland 	    TCP_TS_TO_TICKS(tcp_ts_getticks() - tp->rfbuf_ts) >
1474560c0586SMichael Tuexen 	    ((tp->t_srtt >> TCP_RTT_SHIFT)/2)) {
1475560c0586SMichael Tuexen 		if (tp->rfbuf_cnt > ((so->so_rcv.sb_hiwat / 2)/ 4 * 3) &&
1476e44c1887SSteven Hartland 		    so->so_rcv.sb_hiwat < V_tcp_autorcvbuf_max) {
1477560c0586SMichael Tuexen 			newsize = min((so->so_rcv.sb_hiwat + (so->so_rcv.sb_hiwat/2)), V_tcp_autorcvbuf_max);
1478e44c1887SSteven Hartland 		}
1479e44c1887SSteven Hartland 		TCP_PROBE6(receive__autoresize, NULL, tp, m, tp, th, newsize);
1480e44c1887SSteven Hartland 
1481e44c1887SSteven Hartland 		/* Start over with next RTT. */
1482e44c1887SSteven Hartland 		tp->rfbuf_ts = 0;
1483e44c1887SSteven Hartland 		tp->rfbuf_cnt = 0;
1484e44c1887SSteven Hartland 	} else {
1485e44c1887SSteven Hartland 		tp->rfbuf_cnt += tlen;	/* add up */
1486e44c1887SSteven Hartland 	}
1487e44c1887SSteven Hartland 	return (newsize);
1488e44c1887SSteven Hartland }
1489e44c1887SSteven Hartland 
14909e644c23SMichael Tuexen int
14919e644c23SMichael Tuexen tcp_input(struct mbuf **mp, int *offp, int proto)
14929e644c23SMichael Tuexen {
14939e644c23SMichael Tuexen 	return(tcp_input_with_port(mp, offp, proto, 0));
14949e644c23SMichael Tuexen }
14959e644c23SMichael Tuexen 
1496c348e880SGleb Smirnoff static void
1497c348e880SGleb Smirnoff tcp_handle_wakeup(struct tcpcb *tp)
14984d0770f1SRichard Scheffenegger {
1499c348e880SGleb Smirnoff 
15009eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
1501c348e880SGleb Smirnoff 
15024d0770f1SRichard Scheffenegger 	if (tp->t_flags & TF_WAKESOR) {
15039eb0e832SGleb Smirnoff 		struct socket *so = tptosocket(tp);
1504c348e880SGleb Smirnoff 
15054d0770f1SRichard Scheffenegger 		tp->t_flags &= ~TF_WAKESOR;
1506032bf749SRichard Scheffenegger 		SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1507032bf749SRichard Scheffenegger 		sorwakeup_locked(so);
15084d0770f1SRichard Scheffenegger 	}
15094d0770f1SRichard Scheffenegger }
15104d0770f1SRichard Scheffenegger 
15114d0770f1SRichard Scheffenegger void
151235bc0bccSGleb Smirnoff tcp_do_segment(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th,
151335bc0bccSGleb Smirnoff     int drop_hdrlen, int tlen, uint8_t iptos)
1514302ce8d6SAndre Oppermann {
1515f7220c48SRichard Scheffenegger 	uint16_t thflags;
151649a6fbe3SRichard Scheffenegger 	int acked, ourfinisacked, needoutput = 0;
151749a6fbe3SRichard Scheffenegger 	sackstatus_t sack_changed;
1518b2ade6b1SRichard Scheffenegger 	int rstreason, todrop, win, incforsyn = 0;
15193ac12506SJonathan T. Looney 	uint32_t tiwin;
15204b7b743cSLawrence Stewart 	uint16_t nsegs;
152107dacf03SAndre Oppermann 	char *s;
15229eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
152335bc0bccSGleb Smirnoff 	struct socket *so = tptosocket(tp);
15249eb0e832SGleb Smirnoff 	struct in_conninfo *inc = &inp->inp_inc;
1525c11a15bfSGleb Smirnoff 	struct mbuf *mfree;
1526302ce8d6SAndre Oppermann 	struct tcpopt to;
1527281a0fd4SPatrick Kelsey 	int tfo_syn;
15283c40e1d5SRichard Scheffenegger 	u_int maxseg;
1529302ce8d6SAndre Oppermann 
15301ebf4607SRichard Scheffenegger 	thflags = tcp_get_flags(th);
1531d64a46eaSLawrence Stewart 	tp->sackhint.last_sack_ack = 0;
153249a6fbe3SRichard Scheffenegger 	sack_changed = SACK_NOCHANGE;
15334b7b743cSLawrence Stewart 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
1534d40c0d47SGleb Smirnoff 
1535d40c0d47SGleb Smirnoff 	NET_EPOCH_ASSERT();
15369eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
1537679d9708SAndre Oppermann 	KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
1538679d9708SAndre Oppermann 	    __func__));
1539679d9708SAndre Oppermann 	KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
1540679d9708SAndre Oppermann 	    __func__));
1541df8bae1dSRodney W. Grimes 
154286a996e6SHiren Panchasara #ifdef TCPPCAP
154386a996e6SHiren Panchasara 	/* Save segment, if requested. */
154486a996e6SHiren Panchasara 	tcp_pcap_add(th, m, &(tp->t_inpkts));
154586a996e6SHiren Panchasara #endif
15462529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0,
15472529f56eSJonathan T. Looney 	    tlen, NULL, true);
154886a996e6SHiren Panchasara 
1549013f4df6SMichael Tuexen 	if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
1550013f4df6SMichael Tuexen 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
1551013f4df6SMichael Tuexen 			log(LOG_DEBUG, "%s; %s: "
1552013f4df6SMichael Tuexen 			    "SYN|FIN segment ignored (based on "
1553013f4df6SMichael Tuexen 			    "sysctl setting)\n", s, __func__);
1554013f4df6SMichael Tuexen 			free(s, M_TCPLOG);
1555013f4df6SMichael Tuexen 		}
1556013f4df6SMichael Tuexen 		goto drop;
1557013f4df6SMichael Tuexen 	}
1558013f4df6SMichael Tuexen 
1559df8bae1dSRodney W. Grimes 	/*
1560ebfd7534SMichael Tuexen 	 * If a segment with the ACK-bit set arrives in the SYN-SENT state
1561ebfd7534SMichael Tuexen 	 * check SEQ.ACK first.
1562ebfd7534SMichael Tuexen 	 */
1563ebfd7534SMichael Tuexen 	if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) &&
1564ebfd7534SMichael Tuexen 	    (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) {
1565ebfd7534SMichael Tuexen 		rstreason = BANDLIM_UNLIMITED;
1566d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
1567ebfd7534SMichael Tuexen 		goto dropwithreset;
1568ebfd7534SMichael Tuexen 	}
1569ebfd7534SMichael Tuexen 
1570ebfd7534SMichael Tuexen 	/*
1571df8bae1dSRodney W. Grimes 	 * Segment received on connection.
1572df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
1573e8949f74SAndre Oppermann 	 * XXX: This should be done after segment
1574e8949f74SAndre Oppermann 	 * validation to ignore broken/spoofed segs.
1575df8bae1dSRodney W. Grimes 	 */
1576cd84e78fSRandall Stewart 	if  (tp->t_idle_reduce &&
1577cd84e78fSRandall Stewart 	     (tp->snd_max == tp->snd_una) &&
1578cd84e78fSRandall Stewart 	     ((ticks - tp->t_rcvtime) >= tp->t_rxtcur))
1579cd84e78fSRandall Stewart 		cc_after_idle(tp);
15809b8b58e0SJonathan Lemon 	tp->t_rcvtime = ticks;
1581df8bae1dSRodney W. Grimes 
1582d1b07f36SRandall Stewart 	if (thflags & TH_FIN)
1583d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_FIN);
1584df8bae1dSRodney W. Grimes 	/*
1585c52102ddSHiren Panchasara 	 * Scale up the window into a 32-bit value.
1586e8949f74SAndre Oppermann 	 * For the SYN_SENT state the scale is zero.
1587464fcfbcSAndre Oppermann 	 */
1588464fcfbcSAndre Oppermann 	tiwin = th->th_win << tp->snd_scale;
1589adc56f5aSEdward Tomasz Napierala #ifdef STATS
1590adc56f5aSEdward Tomasz Napierala 	stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin);
1591adc56f5aSEdward Tomasz Napierala #endif
1592464fcfbcSAndre Oppermann 
1593464fcfbcSAndre Oppermann 	/*
1594f2512ba1SRui Paulo 	 * TCP ECN processing.
1595f2512ba1SRui Paulo 	 */
1596b1258b76SRichard Scheffenegger 	if (tcp_ecn_input_segment(tp, thflags, tlen,
1597b1258b76SRichard Scheffenegger 	    tcp_packets_this_ack(tp, th->th_ack),
1598b1258b76SRichard Scheffenegger 	    iptos))
1599dbc42409SLawrence Stewart 		cc_cong_signal(tp, th, CC_ECN);
1600f2512ba1SRui Paulo 
1601f2512ba1SRui Paulo 	/*
1602f72167f4SAndre Oppermann 	 * Parse options on any incoming segment.
1603f72167f4SAndre Oppermann 	 */
1604302ce8d6SAndre Oppermann 	tcp_dooptions(&to, (u_char *)(th + 1),
1605302ce8d6SAndre Oppermann 	    (th->th_off << 2) - sizeof(struct tcphdr),
1606302ce8d6SAndre Oppermann 	    (thflags & TH_SYN) ? TO_SYN : 0);
1607f72167f4SAndre Oppermann 
1608fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1609fcf59617SAndrey V. Elsukov 	if ((tp->t_flags & TF_SIGNATURE) != 0 &&
1610fcf59617SAndrey V. Elsukov 	    (to.to_flags & TOF_SIGNATURE) == 0) {
1611fcf59617SAndrey V. Elsukov 		TCPSTAT_INC(tcps_sig_err_sigopt);
1612fcf59617SAndrey V. Elsukov 		/* XXX: should drop? */
1613fcf59617SAndrey V. Elsukov 	}
1614fcf59617SAndrey V. Elsukov #endif
1615f72167f4SAndre Oppermann 	/*
1616f72167f4SAndre Oppermann 	 * If echoed timestamp is later than the current time,
1617bf6d304aSAndre Oppermann 	 * fall back to non RFC1323 RTT calculation.  Normalize
1618bf6d304aSAndre Oppermann 	 * timestamp if syncookies were used when this connection
1619bf6d304aSAndre Oppermann 	 * was established.
1620f72167f4SAndre Oppermann 	 */
1621bf6d304aSAndre Oppermann 	if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
1622e16fa5caSJohn-Mark Gurney 		to.to_tsecr -= tp->ts_offset;
1623d8951c8aSBjoern A. Zeeb 		if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks()))
1624f72167f4SAndre Oppermann 			to.to_tsecr = 0;
16254531b345SRichard Scheffenegger 		else if (tp->t_rxtshift == 1 &&
16264531b345SRichard Scheffenegger 			 tp->t_flags & TF_PREVVALID &&
16274531b345SRichard Scheffenegger 			 tp->t_badrxtwin != 0 &&
16284531b345SRichard Scheffenegger 			 TSTMP_LT(to.to_tsecr, tp->t_badrxtwin))
162910d20c84SMatt Macy 			cc_cong_signal(tp, th, CC_RTO_ERR);
1630bf6d304aSAndre Oppermann 	}
163107dacf03SAndre Oppermann 	/*
163297d8d152SAndre Oppermann 	 * Process options only when we get SYN/ACK back. The SYN case
163397d8d152SAndre Oppermann 	 * for incoming connections is handled in tcp_syncache.
16345396d0f8SAndre Oppermann 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN>
16355396d0f8SAndre Oppermann 	 * or <SYN,ACK>) segment itself is never scaled.
163697d8d152SAndre Oppermann 	 * XXX this is traditional behavior, may need to be cleaned up.
1637df8bae1dSRodney W. Grimes 	 */
16380a389eabSSimon L. B. Nielsen 	if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
16396e16d877SRichard Scheffenegger 		/* Handle parallel SYN for ECN */
1640f7220c48SRichard Scheffenegger 		tcp_ecn_input_parallel_syn(tp, thflags, iptos);
1641464fcfbcSAndre Oppermann 		if ((to.to_flags & TOF_SCALE) &&
16422593f858SRichard Scheffenegger 		    (tp->t_flags & TF_REQ_SCALE) &&
16432593f858SRichard Scheffenegger 		    !(tp->t_flags & TF_NOOPT)) {
1644be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_SCALE;
164502a1a643SAndre Oppermann 			tp->snd_scale = to.to_wscale;
16468e051165SRichard Scheffenegger 		} else
16478e051165SRichard Scheffenegger 			tp->t_flags &= ~TF_REQ_SCALE;
16485396d0f8SAndre Oppermann 		/*
16495396d0f8SAndre Oppermann 		 * Initial send window.  It will be updated with
16505396d0f8SAndre Oppermann 		 * the next incoming segment to the scaled value.
16515396d0f8SAndre Oppermann 		 */
16525396d0f8SAndre Oppermann 		tp->snd_wnd = th->th_win;
16538e051165SRichard Scheffenegger 		if ((to.to_flags & TOF_TS) &&
16542593f858SRichard Scheffenegger 		    (tp->t_flags & TF_REQ_TSTMP) &&
16552593f858SRichard Scheffenegger 		    !(tp->t_flags & TF_NOOPT)) {
1656be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_TSTMP;
1657be2ac88cSJonathan Lemon 			tp->ts_recent = to.to_tsval;
1658d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
16598e051165SRichard Scheffenegger 		} else
16608e051165SRichard Scheffenegger 			tp->t_flags &= ~TF_REQ_TSTMP;
1661be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_MSS)
1662be2ac88cSJonathan Lemon 			tcp_mss(tp, to.to_mss);
16633529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
16642593f858SRichard Scheffenegger 		    (!(to.to_flags & TOF_SACKPERM) ||
16652593f858SRichard Scheffenegger 		    (tp->t_flags & TF_NOOPT)))
16663529149eSAndre Oppermann 			tp->t_flags &= ~TF_SACK_PERMIT;
1667c560df6fSPatrick Kelsey 		if (IS_FASTOPEN(tp->t_flags)) {
16682593f858SRichard Scheffenegger 			if ((to.to_flags & TOF_FASTOPEN) &&
16692593f858SRichard Scheffenegger 			    !(tp->t_flags & TF_NOOPT)) {
1670a026a53aSMichael Tuexen 				uint16_t mss;
1671a026a53aSMichael Tuexen 
1672a026a53aSMichael Tuexen 				if (to.to_flags & TOF_MSS)
1673a026a53aSMichael Tuexen 					mss = to.to_mss;
1674c560df6fSPatrick Kelsey 				else
16759eb0e832SGleb Smirnoff 					if ((inp->inp_vflag & INP_IPV6) != 0)
1676a026a53aSMichael Tuexen 						mss = TCP6_MSS;
1677a026a53aSMichael Tuexen 					else
1678a026a53aSMichael Tuexen 						mss = TCP_MSS;
1679a026a53aSMichael Tuexen 				tcp_fastopen_update_cache(tp, mss,
1680a026a53aSMichael Tuexen 				    to.to_tfo_len, to.to_tfo_cookie);
1681a026a53aSMichael Tuexen 			} else
1682c560df6fSPatrick Kelsey 				tcp_fastopen_disable_path(tp);
1683c560df6fSPatrick Kelsey 		}
16846d90faf3SPaul Saab 	}
16856d90faf3SPaul Saab 
1686df8bae1dSRodney W. Grimes 	/*
1687283c76c7SMichael Tuexen 	 * If timestamps were negotiated during SYN/ACK and a
1688283c76c7SMichael Tuexen 	 * segment without a timestamp is received, silently drop
1689d2b3ceddSMichael Tuexen 	 * the segment, unless it is a RST segment or missing timestamps are
1690d2b3ceddSMichael Tuexen 	 * tolerated.
1691283c76c7SMichael Tuexen 	 * See section 3.2 of RFC 7323.
16924da9052aSMichael Tuexen 	 */
16934da9052aSMichael Tuexen 	if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) {
1694d2b3ceddSMichael Tuexen 		if (((thflags & TH_RST) != 0) || V_tcp_tolerate_missing_ts) {
1695cc3c3485SMichael Tuexen 			if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
1696cc3c3485SMichael Tuexen 				log(LOG_DEBUG, "%s; %s: Timestamp missing, "
1697cc3c3485SMichael Tuexen 				    "segment processed normally\n",
1698cc3c3485SMichael Tuexen 				    s, __func__);
1699cc3c3485SMichael Tuexen 				free(s, M_TCPLOG);
1700cc3c3485SMichael Tuexen 			}
1701cc3c3485SMichael Tuexen 		} else {
17024da9052aSMichael Tuexen 			if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
17034da9052aSMichael Tuexen 				log(LOG_DEBUG, "%s; %s: Timestamp missing, "
1704283c76c7SMichael Tuexen 				    "segment silently dropped\n", s, __func__);
17054da9052aSMichael Tuexen 				free(s, M_TCPLOG);
17064da9052aSMichael Tuexen 			}
1707283c76c7SMichael Tuexen 			goto drop;
17084da9052aSMichael Tuexen 		}
1709cc3c3485SMichael Tuexen 	}
1710283c76c7SMichael Tuexen 	/*
1711283c76c7SMichael Tuexen 	 * If timestamps were not negotiated during SYN/ACK and a
171275fcd27aSMichael Tuexen 	 * segment with a timestamp is received, ignore the
1713283c76c7SMichael Tuexen 	 * timestamp and process the packet normally.
1714283c76c7SMichael Tuexen 	 * See section 3.2 of RFC 7323.
1715283c76c7SMichael Tuexen 	 */
17164da9052aSMichael Tuexen 	if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) {
17174da9052aSMichael Tuexen 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
17184da9052aSMichael Tuexen 			log(LOG_DEBUG, "%s; %s: Timestamp not expected, "
1719283c76c7SMichael Tuexen 			    "segment processed normally\n", s, __func__);
17204da9052aSMichael Tuexen 			free(s, M_TCPLOG);
17214da9052aSMichael Tuexen 		}
17224da9052aSMichael Tuexen 	}
17234da9052aSMichael Tuexen 
17244da9052aSMichael Tuexen 	/*
1725df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
1726df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
1727df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
1728df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
1729df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
1730df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
1731df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
1732df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
1733df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
1734df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
1735df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
1736df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
1737a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
1738c5ad39b9SAndre Oppermann 	 * Since we check for TCPS_ESTABLISHED first, it can only
1739a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
1740df8bae1dSRodney W. Grimes 	 */
1741df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
1742c5ad39b9SAndre Oppermann 	    th->th_seq == tp->rcv_nxt &&
1743fb59c426SYoshinobu Inoue 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1744c5ad39b9SAndre Oppermann 	    tp->snd_nxt == tp->snd_max &&
1745c5ad39b9SAndre Oppermann 	    tiwin && tiwin == tp->snd_wnd &&
1746a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
1747c28440dbSRandall Stewart 	    SEGQ_EMPTY(tp) &&
17484741bfcbSPatrick Kelsey 	    ((to.to_flags & TOF_TS) == 0 ||
1749c5ad39b9SAndre Oppermann 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) {
1750df8bae1dSRodney W. Grimes 		/*
1751df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
1752df8bae1dSRodney W. Grimes 		 * record the timestamp.
1753a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
1754a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1755df8bae1dSRodney W. Grimes 		 */
1756be2ac88cSJonathan Lemon 		if ((to.to_flags & TOF_TS) != 0 &&
1757fb59c426SYoshinobu Inoue 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1758d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
1759a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
1760df8bae1dSRodney W. Grimes 		}
1761df8bae1dSRodney W. Grimes 
1762fb59c426SYoshinobu Inoue 		if (tlen == 0) {
1763fb59c426SYoshinobu Inoue 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
1764fb59c426SYoshinobu Inoue 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
1765dbc42409SLawrence Stewart 			    !IN_RECOVERY(tp->t_flags) &&
1766fc30a251SAndre Oppermann 			    (to.to_flags & TOF_SACK) == 0 &&
1767dbc42409SLawrence Stewart 			    TAILQ_EMPTY(&tp->snd_holes)) {
1768df8bae1dSRodney W. Grimes 				/*
1769e8949f74SAndre Oppermann 				 * This is a pure ack for outstanding data.
1770df8bae1dSRodney W. Grimes 				 */
177178b50714SRobert Watson 				TCPSTAT_INC(tcps_predack);
1772252ca428SRobert Watson 
17739b8b58e0SJonathan Lemon 				/*
177410d20c84SMatt Macy 				 * "bad retransmit" recovery without timestamps.
17759b8b58e0SJonathan Lemon 				 */
177610d20c84SMatt Macy 				if ((to.to_flags & TOF_TS) == 0 &&
177710d20c84SMatt Macy 				    tp->t_rxtshift == 1 &&
1778672dc4aeSJohn Baldwin 				    tp->t_flags & TF_PREVVALID &&
17794531b345SRichard Scheffenegger 				    tp->t_badrxtwin != 0 &&
17804531b345SRichard Scheffenegger 				    TSTMP_LT(ticks, tp->t_badrxtwin)) {
1781dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_RTO_ERR);
17829b8b58e0SJonathan Lemon 				}
1783fa55172bSMatthew Dillon 
1784fa55172bSMatthew Dillon 				/*
1785fa55172bSMatthew Dillon 				 * Recalculate the transmit timer / rtt.
1786fa55172bSMatthew Dillon 				 *
1787fa55172bSMatthew Dillon 				 * Some boxes send broken timestamp replies
1788fa55172bSMatthew Dillon 				 * during the SYN+ACK phase, ignore
1789fa55172bSMatthew Dillon 				 * timestamps of 0 or we could calculate a
1790fa55172bSMatthew Dillon 				 * huge RTT and blow up the retransmit timer.
1791fa55172bSMatthew Dillon 				 */
1792fa55172bSMatthew Dillon 				if ((to.to_flags & TOF_TS) != 0 &&
1793fa55172bSMatthew Dillon 				    to.to_tsecr) {
17943ac12506SJonathan T. Looney 					uint32_t t;
1795d8951c8aSBjoern A. Zeeb 
1796d8951c8aSBjoern A. Zeeb 					t = tcp_ts_getticks() - to.to_tsecr;
1797d8951c8aSBjoern A. Zeeb 					if (!tp->t_rttlow || tp->t_rttlow > t)
1798d8951c8aSBjoern A. Zeeb 						tp->t_rttlow = t;
1799a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
1800d8951c8aSBjoern A. Zeeb 					    TCP_TS_TO_TICKS(t) + 1);
1801fa55172bSMatthew Dillon 				} else if (tp->t_rtttime &&
1802fa55172bSMatthew Dillon 				    SEQ_GT(th->th_ack, tp->t_rtseq)) {
1803eaf80179SAndre Oppermann 					if (!tp->t_rttlow ||
1804eaf80179SAndre Oppermann 					    tp->t_rttlow > ticks - tp->t_rtttime)
1805eaf80179SAndre Oppermann 						tp->t_rttlow = ticks - tp->t_rtttime;
1806c068736aSJeffrey Hsu 					tcp_xmit_timer(tp,
1807c068736aSJeffrey Hsu 							ticks - tp->t_rtttime);
1808fa55172bSMatthew Dillon 				}
1809dbc42409SLawrence Stewart 				acked = BYTES_THIS_ACK(tp, th);
181039bc9de5SLawrence Stewart 
1811bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
181239bc9de5SLawrence Stewart 				/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
181339bc9de5SLawrence Stewart 				hhook_run_tcp_est_in(tp, th, &to);
1814bd79708dSJonathan T. Looney #endif
181539bc9de5SLawrence Stewart 
18164b7b743cSLawrence Stewart 				TCPSTAT_ADD(tcps_rcvackpack, nsegs);
181778b50714SRobert Watson 				TCPSTAT_ADD(tcps_rcvackbyte, acked);
1818df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
18199d11646dSJeffrey Hsu 				if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
18209d11646dSJeffrey Hsu 				    SEQ_LEQ(th->th_ack, tp->snd_recover))
18219d11646dSJeffrey Hsu 					tp->snd_recover = th->th_ack - 1;
1822dbc42409SLawrence Stewart 
1823dbc42409SLawrence Stewart 				/*
1824dbc42409SLawrence Stewart 				 * Let the congestion control algorithm update
1825dbc42409SLawrence Stewart 				 * congestion control related information. This
1826dbc42409SLawrence Stewart 				 * typically means increasing the congestion
1827dbc42409SLawrence Stewart 				 * window.
1828dbc42409SLawrence Stewart 				 */
18294b7b743cSLawrence Stewart 				cc_ack_received(tp, th, nsegs, CC_ACK);
1830dbc42409SLawrence Stewart 
18319d11646dSJeffrey Hsu 				tp->snd_una = th->th_ack;
18321645d090SJeff Roberson 				/*
1833e8949f74SAndre Oppermann 				 * Pull snd_wl2 up to prevent seq wrap relative
18341645d090SJeff Roberson 				 * to th_ack.
18351645d090SJeff Roberson 				 */
1836cb942153SJeffrey Hsu 				tp->snd_wl2 = th->th_ack;
1837b5addd85SJeffrey Hsu 				tp->t_dupacks = 0;
1838df8bae1dSRodney W. Grimes 				m_freem(m);
1839df8bae1dSRodney W. Grimes 
1840df8bae1dSRodney W. Grimes 				/*
1841df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
1842df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
1843df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
1844df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
1845df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
1846df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
1847df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
1848e8949f74SAndre Oppermann 				 */
18492b9c9984SGeorge V. Neville-Neil 				TCP_PROBE3(debug__input, tp, th, m);
185008af8aacSRandall Stewart 				/*
185108af8aacSRandall Stewart 				 * Clear t_acktime if remote side has ACKd
185208af8aacSRandall Stewart 				 * all data in the socket buffer.
185308af8aacSRandall Stewart 				 * Otherwise, update t_acktime if we received
185408af8aacSRandall Stewart 				 * a sufficiently large ACK.
185508af8aacSRandall Stewart 				 */
185608af8aacSRandall Stewart 				if (sbavail(&so->so_snd) == 0)
185708af8aacSRandall Stewart 					tp->t_acktime = 0;
185808af8aacSRandall Stewart 				else if (acked > 1)
185908af8aacSRandall Stewart 					tp->t_acktime = ticks;
1860df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
1861b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
1862b8152ba7SAndre Oppermann 				else if (!tcp_timer_active(tp, TT_PERSIST))
1863b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT,
186408af8aacSRandall Stewart 					    TP_RXTCUR(tp));
1865032bf749SRichard Scheffenegger 				sowwakeup(so);
1866cfa6009eSGleb Smirnoff 				if (sbavail(&so->so_snd))
186740fa3e40SGleb Smirnoff 					(void) tcp_output(tp);
1868a14c749fSJonathan Lemon 				goto check_delack;
1869df8bae1dSRodney W. Grimes 			}
1870fb59c426SYoshinobu Inoue 		} else if (th->th_ack == tp->snd_una &&
1871fb59c426SYoshinobu Inoue 		    tlen <= sbspace(&so->so_rcv)) {
18726741ecf5SAndre Oppermann 			int newsize = 0;	/* automatic sockbuf scaling */
18736741ecf5SAndre Oppermann 
1874df8bae1dSRodney W. Grimes 			/*
1875252ca428SRobert Watson 			 * This is a pure, in-sequence data packet with
1876252ca428SRobert Watson 			 * nothing on the reassembly queue and we have enough
1877252ca428SRobert Watson 			 * buffer space to take it.
1878df8bae1dSRodney W. Grimes 			 */
18796d90faf3SPaul Saab 			/* Clean receiver SACK report if present */
18803529149eSAndre Oppermann 			if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks)
18816d90faf3SPaul Saab 				tcp_clean_sackreport(tp);
188278b50714SRobert Watson 			TCPSTAT_INC(tcps_preddat);
1883fb59c426SYoshinobu Inoue 			tp->rcv_nxt += tlen;
1884e854dd38SRandall Stewart 			if (tlen &&
1885e854dd38SRandall Stewart 			    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1886e854dd38SRandall Stewart 			    (tp->t_fbyte_in == 0)) {
1887e854dd38SRandall Stewart 				tp->t_fbyte_in = ticks;
1888e854dd38SRandall Stewart 				if (tp->t_fbyte_in == 0)
1889e854dd38SRandall Stewart 					tp->t_fbyte_in = 1;
1890e854dd38SRandall Stewart 				if (tp->t_fbyte_out && tp->t_fbyte_in)
1891e854dd38SRandall Stewart 					tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1892e854dd38SRandall Stewart 			}
18931645d090SJeff Roberson 			/*
18941645d090SJeff Roberson 			 * Pull snd_wl1 up to prevent seq wrap relative to
18951645d090SJeff Roberson 			 * th_seq.
18961645d090SJeff Roberson 			 */
189760ee3bb2SAndre Oppermann 			tp->snd_wl1 = th->th_seq;
18981645d090SJeff Roberson 			/*
18991645d090SJeff Roberson 			 * Pull rcv_up up to prevent seq wrap relative to
19001645d090SJeff Roberson 			 * rcv_nxt.
19011645d090SJeff Roberson 			 */
19021645d090SJeff Roberson 			tp->rcv_up = tp->rcv_nxt;
19034b7b743cSLawrence Stewart 			TCPSTAT_ADD(tcps_rcvpack, nsegs);
190478b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
19052b9c9984SGeorge V. Neville-Neil 			TCP_PROBE3(debug__input, tp, th, m);
19065d06879aSGeorge V. Neville-Neil 
1907e44c1887SSteven Hartland 			newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
19086741ecf5SAndre Oppermann 
19096741ecf5SAndre Oppermann 			/* Add data to socket buffer. */
19101e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
1911c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1912c1c36a2cSMike Silbersack 				m_freem(m);
1913c1c36a2cSMike Silbersack 			} else {
19146741ecf5SAndre Oppermann 				/*
19156741ecf5SAndre Oppermann 				 * Set new socket buffer size.
19166741ecf5SAndre Oppermann 				 * Give up when limit is reached.
19176741ecf5SAndre Oppermann 				 */
19186741ecf5SAndre Oppermann 				if (newsize)
191943283184SGleb Smirnoff 					if (!sbreserve_locked(so, SO_RCV,
192043283184SGleb Smirnoff 					    newsize, NULL))
19216741ecf5SAndre Oppermann 						so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
1922fb59c426SYoshinobu Inoue 				m_adj(m, drop_hdrlen);	/* delayed header drop */
1923651e4e6aSGleb Smirnoff 				sbappendstream_locked(&so->so_rcv, m, 0);
1924c1c36a2cSMike Silbersack 			}
1925032bf749SRichard Scheffenegger 			/* NB: sorwakeup_locked() does an implicit unlock. */
1926032bf749SRichard Scheffenegger 			sorwakeup_locked(so);
1927c1e5a6e5SAndre Oppermann 			if (DELAY_ACK(tp, tlen)) {
19283bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
1929f498eeeeSDavid Greenman 			} else {
1930e612a582SDavid Greenman 				tp->t_flags |= TF_ACKNOW;
193140fa3e40SGleb Smirnoff 				tcp_output(tp);
1932e612a582SDavid Greenman 			}
1933a14c749fSJonathan Lemon 			goto check_delack;
1934df8bae1dSRodney W. Grimes 		}
1935df8bae1dSRodney W. Grimes 	}
1936df8bae1dSRodney W. Grimes 
1937df8bae1dSRodney W. Grimes 	/*
1938df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
1939df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
1940df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
1941df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
1942df8bae1dSRodney W. Grimes 	 */
1943df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
1944df8bae1dSRodney W. Grimes 	if (win < 0)
1945df8bae1dSRodney W. Grimes 		win = 0;
194666e39adcSJohn Polstra 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1947df8bae1dSRodney W. Grimes 
1948df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1949df8bae1dSRodney W. Grimes 	/*
1950764d8cefSBill Fenner 	 * If the state is SYN_RECEIVED:
1951764d8cefSBill Fenner 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
1952764d8cefSBill Fenner 	 */
1953764d8cefSBill Fenner 	case TCPS_SYN_RECEIVED:
1954b352ef58SRichard Scheffenegger 		if (thflags & TH_RST) {
1955b352ef58SRichard Scheffenegger 			/* Handle RST segments later. */
1956b352ef58SRichard Scheffenegger 			break;
1957b352ef58SRichard Scheffenegger 		}
1958fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1959fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1960a57815efSBosko Milekic 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1961a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
1962d1b07f36SRandall Stewart 				tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
1963a57815efSBosko Milekic 				goto dropwithreset;
1964a57815efSBosko Milekic 		}
196568bd7ed1SJonathan T. Looney 		if (IS_FASTOPEN(tp->t_flags)) {
1966281a0fd4SPatrick Kelsey 			/*
1967281a0fd4SPatrick Kelsey 			 * When a TFO connection is in SYN_RECEIVED, the
1968281a0fd4SPatrick Kelsey 			 * only valid packets are the initial SYN, a
1969281a0fd4SPatrick Kelsey 			 * retransmit/copy of the initial SYN (possibly with
1970281a0fd4SPatrick Kelsey 			 * a subset of the original data), a valid ACK, a
1971281a0fd4SPatrick Kelsey 			 * FIN, or a RST.
1972281a0fd4SPatrick Kelsey 			 */
1973281a0fd4SPatrick Kelsey 			if ((thflags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) {
1974281a0fd4SPatrick Kelsey 				rstreason = BANDLIM_RST_OPENPORT;
1975d1b07f36SRandall Stewart 				tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
1976281a0fd4SPatrick Kelsey 				goto dropwithreset;
1977281a0fd4SPatrick Kelsey 			} else if (thflags & TH_SYN) {
1978281a0fd4SPatrick Kelsey 				/* non-initial SYN is ignored */
1979281a0fd4SPatrick Kelsey 				if ((tcp_timer_active(tp, TT_DELACK) ||
1980281a0fd4SPatrick Kelsey 				     tcp_timer_active(tp, TT_REXMT)))
1981281a0fd4SPatrick Kelsey 					goto drop;
1982281a0fd4SPatrick Kelsey 			} else if (!(thflags & (TH_ACK|TH_FIN|TH_RST))) {
1983281a0fd4SPatrick Kelsey 				goto drop;
1984281a0fd4SPatrick Kelsey 			}
1985281a0fd4SPatrick Kelsey 		}
1986764d8cefSBill Fenner 		break;
1987764d8cefSBill Fenner 
1988764d8cefSBill Fenner 	/*
1989df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
199098732609SMichael Tuexen 	 *	if seg contains a RST with valid ACK (SEQ.ACK has already
199198732609SMichael Tuexen 	 *	    been verified), then drop the connection.
199298732609SMichael Tuexen 	 *	if seg contains a RST without an ACK, drop the seg.
199398732609SMichael Tuexen 	 *	if seg does not contain SYN, then drop the seg.
1994df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
1995df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
1996df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
1997f2512ba1SRui Paulo 	 *	if seg contains an ECE and ECN support is enabled, the stream
1998f2512ba1SRui Paulo 	 *	    is ECN capable.
1999df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
2000df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
2001df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
2002df8bae1dSRodney W. Grimes 	 */
2003df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
200457f60867SMark Johnston 		if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) {
2005d9fae5abSAndriy Gapon 			TCP_PROBE5(connect__refused, NULL, tp,
20062b9c9984SGeorge V. Neville-Neil 			    m, tp, th);
2007d1b07f36SRandall Stewart 			tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
2008df8bae1dSRodney W. Grimes 			tp = tcp_drop(tp, ECONNREFUSED);
200957f60867SMark Johnston 		}
20100ca3f933SAndre Oppermann 		if (thflags & TH_RST)
2011df8bae1dSRodney W. Grimes 			goto drop;
20120ca3f933SAndre Oppermann 		if (!(thflags & TH_SYN))
2013df8bae1dSRodney W. Grimes 			goto drop;
2014464fcfbcSAndre Oppermann 
2015fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
2016df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
2017fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
2018c560df6fSPatrick Kelsey 			int tfo_partial_ack = 0;
2019c560df6fSPatrick Kelsey 
202078b50714SRobert Watson 			TCPSTAT_INC(tcps_connects);
2021845799c1SAndras Olah 			soisconnected(so);
2022c488362eSRobert Watson #ifdef MAC
202330d239bcSRobert Watson 			mac_socketpeer_set_from_mbuf(m, so);
2024c488362eSRobert Watson #endif
2025845799c1SAndras Olah 			/* Do window scaling on this connection? */
2026845799c1SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2027845799c1SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
2028845799c1SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2029845799c1SAndras Olah 			}
20303ac12506SJonathan T. Looney 			tp->rcv_adv += min(tp->rcv_wnd,
2031766282cbSJohn Baldwin 			    TCP_MAXWIN << tp->rcv_scale);
2032a0292f23SGarrett Wollman 			tp->snd_una++;		/* SYN is acked */
2033a0292f23SGarrett Wollman 			/*
2034c560df6fSPatrick Kelsey 			 * If not all the data that was sent in the TFO SYN
2035c560df6fSPatrick Kelsey 			 * has been acked, resend the remainder right away.
2036c560df6fSPatrick Kelsey 			 */
2037c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags) &&
2038c560df6fSPatrick Kelsey 			    (tp->snd_una != tp->snd_max)) {
2039c560df6fSPatrick Kelsey 				tp->snd_nxt = th->th_ack;
2040c560df6fSPatrick Kelsey 				tfo_partial_ack = 1;
2041c560df6fSPatrick Kelsey 			}
2042c560df6fSPatrick Kelsey 			/*
2043a0292f23SGarrett Wollman 			 * If there's data, delay ACK; if there's also a FIN
2044a0292f23SGarrett Wollman 			 * ACKNOW will be turned on later.
2045a0292f23SGarrett Wollman 			 */
2046c560df6fSPatrick Kelsey 			if (DELAY_ACK(tp, tlen) && tlen != 0 && !tfo_partial_ack)
2047b8152ba7SAndre Oppermann 				tcp_timer_activate(tp, TT_DELACK,
2048b8152ba7SAndre Oppermann 				    tcp_delacktime);
2049a0292f23SGarrett Wollman 			else
2050a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
2051f2512ba1SRui Paulo 
2052f7220c48SRichard Scheffenegger 			tcp_ecn_input_syn_sent(tp, thflags, iptos);
2053f2512ba1SRui Paulo 
2054a0292f23SGarrett Wollman 			/*
2055a0292f23SGarrett Wollman 			 * Received <SYN,ACK> in SYN_SENT[*] state.
2056a0292f23SGarrett Wollman 			 * Transitions:
2057a0292f23SGarrett Wollman 			 *	SYN_SENT  --> ESTABLISHED
2058a0292f23SGarrett Wollman 			 *	SYN_SENT* --> FIN_WAIT_1
2059a0292f23SGarrett Wollman 			 */
20609b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
2061a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDFIN) {
206208af8aacSRandall Stewart 				tp->t_acktime = ticks;
206357f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_1);
2064a0292f23SGarrett Wollman 				tp->t_flags &= ~TF_NEEDFIN;
2065fb59c426SYoshinobu Inoue 				thflags &= ~TH_SYN;
20667ff19458SPaul Traina 			} else {
206757f60867SMark Johnston 				tcp_state_change(tp, TCPS_ESTABLISHED);
2068d9fae5abSAndriy Gapon 				TCP_PROBE5(connect__established, NULL, tp,
20692b9c9984SGeorge V. Neville-Neil 				    m, tp, th);
2070dbc42409SLawrence Stewart 				cc_conn_init(tp);
20719077f387SGleb Smirnoff 				tcp_timer_activate(tp, TT_KEEP,
20729077f387SGleb Smirnoff 				    TP_KEEPIDLE(tp));
20737ff19458SPaul Traina 			}
2074a0292f23SGarrett Wollman 		} else {
2075a0292f23SGarrett Wollman 			/*
2076c068736aSJeffrey Hsu 			 * Received initial SYN in SYN-SENT[*] state =>
2077153edc50SHiren Panchasara 			 * simultaneous open.
2078c068736aSJeffrey Hsu 			 * If it succeeds, connection is * half-synchronized.
2079c068736aSJeffrey Hsu 			 * Otherwise, do 3-way handshake:
2080a0292f23SGarrett Wollman 			 *        SYN-SENT -> SYN-RECEIVED
2081a0292f23SGarrett Wollman 			 *        SYN-SENT* -> SYN-RECEIVED*
2082a0292f23SGarrett Wollman 			 */
2083493105c2SGleb Smirnoff 			tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN | TF_SONOTCONN);
2084b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
208557f60867SMark Johnston 			tcp_state_change(tp, TCPS_SYN_RECEIVED);
2086a0292f23SGarrett Wollman 		}
2087df8bae1dSRodney W. Grimes 
2088df8bae1dSRodney W. Grimes 		/*
2089fb59c426SYoshinobu Inoue 		 * Advance th->th_seq to correspond to first data byte.
2090df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
2091df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
2092df8bae1dSRodney W. Grimes 		 */
2093fb59c426SYoshinobu Inoue 		th->th_seq++;
2094fb59c426SYoshinobu Inoue 		if (tlen > tp->rcv_wnd) {
2095fb59c426SYoshinobu Inoue 			todrop = tlen - tp->rcv_wnd;
2096df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
2097fb59c426SYoshinobu Inoue 			tlen = tp->rcv_wnd;
2098fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
209978b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpackafterwin);
210078b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2101df8bae1dSRodney W. Grimes 		}
2102fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
2103fb59c426SYoshinobu Inoue 		tp->rcv_up = th->th_seq;
2104a0292f23SGarrett Wollman 		/*
2105a0292f23SGarrett Wollman 		 * Client side of transaction: already sent SYN and data.
2106a0292f23SGarrett Wollman 		 * If the remote host used T/TCP to validate the SYN,
2107a0292f23SGarrett Wollman 		 * our data will be ACK'd; if so, enter normal data segment
2108a0292f23SGarrett Wollman 		 * processing in the middle of step 5, ack processing.
2109a0292f23SGarrett Wollman 		 * Otherwise, goto step 6.
2110a0292f23SGarrett Wollman 		 */
2111fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK)
2112a0292f23SGarrett Wollman 			goto process_ACK;
2113c068736aSJeffrey Hsu 
2114df8bae1dSRodney W. Grimes 		goto step6;
2115df8bae1dSRodney W. Grimes 	}
2116df8bae1dSRodney W. Grimes 
2117df8bae1dSRodney W. Grimes 	/*
2118df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
211980ab7c0eSGarrett Wollman 	 * First check the RST flag and sequence number since reset segments
212080ab7c0eSGarrett Wollman 	 * are exempt from the timestamp and connection count tests.  This
212180ab7c0eSGarrett Wollman 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
212280ab7c0eSGarrett Wollman 	 * below which allowed reset segments in half the sequence space
212380ab7c0eSGarrett Wollman 	 * to fall though and be processed (which gives forged reset
212480ab7c0eSGarrett Wollman 	 * segments with a random sequence number a 50 percent chance of
212580ab7c0eSGarrett Wollman 	 * killing a connection).
212680ab7c0eSGarrett Wollman 	 * Then check timestamp, if present.
2127a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
2128df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
2129df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
2130df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
213180ab7c0eSGarrett Wollman 	 */
2132fb59c426SYoshinobu Inoue 	if (thflags & TH_RST) {
21333220a212SGleb Smirnoff 		/*
21343220a212SGleb Smirnoff 		 * RFC5961 Section 3.2
21353220a212SGleb Smirnoff 		 *
21363220a212SGleb Smirnoff 		 * - RST drops connection only if SEG.SEQ == RCV.NXT.
21373220a212SGleb Smirnoff 		 * - If RST is in window, we send challenge ACK.
21383220a212SGleb Smirnoff 		 *
21393220a212SGleb Smirnoff 		 * Note: to take into account delayed ACKs, we should
21403220a212SGleb Smirnoff 		 *   test against last_ack_sent instead of rcv_nxt.
21413220a212SGleb Smirnoff 		 * Note 2: we handle special case of closed window, not
21423220a212SGleb Smirnoff 		 *   covered by the RFC.
21433220a212SGleb Smirnoff 		 */
21443220a212SGleb Smirnoff 		if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
21453220a212SGleb Smirnoff 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
21463220a212SGleb Smirnoff 		    (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) {
21473220a212SGleb Smirnoff 			KASSERT(tp->t_state != TCPS_SYN_SENT,
21483220a212SGleb Smirnoff 			    ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p",
21493220a212SGleb Smirnoff 			    __func__, th, tp));
21503220a212SGleb Smirnoff 
21513220a212SGleb Smirnoff 			if (V_tcp_insecure_rst ||
21523220a212SGleb Smirnoff 			    tp->last_ack_sent == th->th_seq) {
21533220a212SGleb Smirnoff 				TCPSTAT_INC(tcps_drops);
21543220a212SGleb Smirnoff 				/* Drop the connection. */
21553220a212SGleb Smirnoff 				switch (tp->t_state) {
215680ab7c0eSGarrett Wollman 				case TCPS_SYN_RECEIVED:
215780ab7c0eSGarrett Wollman 					so->so_error = ECONNREFUSED;
215880ab7c0eSGarrett Wollman 					goto close;
215980ab7c0eSGarrett Wollman 				case TCPS_ESTABLISHED:
216080ab7c0eSGarrett Wollman 				case TCPS_FIN_WAIT_1:
216180ab7c0eSGarrett Wollman 				case TCPS_FIN_WAIT_2:
216280ab7c0eSGarrett Wollman 				case TCPS_CLOSE_WAIT:
21636779a1a1SMichael Tuexen 				case TCPS_CLOSING:
21646779a1a1SMichael Tuexen 				case TCPS_LAST_ACK:
216580ab7c0eSGarrett Wollman 					so->so_error = ECONNRESET;
216680ab7c0eSGarrett Wollman 				close:
21673220a212SGleb Smirnoff 					/* FALLTHROUGH */
21683220a212SGleb Smirnoff 				default:
2169d1b07f36SRandall Stewart 					tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_RST);
217080ab7c0eSGarrett Wollman 					tp = tcp_close(tp);
21713220a212SGleb Smirnoff 				}
21723220a212SGleb Smirnoff 			} else {
21733220a212SGleb Smirnoff 				TCPSTAT_INC(tcps_badrst);
21743220a212SGleb Smirnoff 				/* Send challenge ACK. */
21753220a212SGleb Smirnoff 				tcp_respond(tp, mtod(m, void *), th, m,
21763220a212SGleb Smirnoff 				    tp->rcv_nxt, tp->snd_nxt, TH_ACK);
21773220a212SGleb Smirnoff 				tp->last_ack_sent = tp->rcv_nxt;
21783220a212SGleb Smirnoff 				m = NULL;
21793220a212SGleb Smirnoff 			}
21803220a212SGleb Smirnoff 		}
21813220a212SGleb Smirnoff 		goto drop;
21823220a212SGleb Smirnoff 	}
218380ab7c0eSGarrett Wollman 
21843220a212SGleb Smirnoff 	/*
21853220a212SGleb Smirnoff 	 * RFC5961 Section 4.2
21863220a212SGleb Smirnoff 	 * Send challenge ACK for any SYN in synchronized state.
21873220a212SGleb Smirnoff 	 */
2188281a0fd4SPatrick Kelsey 	if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT &&
2189281a0fd4SPatrick Kelsey 	    tp->t_state != TCPS_SYN_RECEIVED) {
21903220a212SGleb Smirnoff 		TCPSTAT_INC(tcps_badsyn);
21913220a212SGleb Smirnoff 		if (V_tcp_insecure_syn &&
21923220a212SGleb Smirnoff 		    SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
21933220a212SGleb Smirnoff 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
2194d1b07f36SRandall Stewart 			tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
21953220a212SGleb Smirnoff 			tp = tcp_drop(tp, ECONNRESET);
21963220a212SGleb Smirnoff 			rstreason = BANDLIM_UNLIMITED;
21973220a212SGleb Smirnoff 		} else {
219883c1ec92SRichard Scheffenegger 			tcp_ecn_input_syn_sent(tp, thflags, iptos);
21993220a212SGleb Smirnoff 			/* Send challenge ACK. */
22003220a212SGleb Smirnoff 			tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt,
22013220a212SGleb Smirnoff 			    tp->snd_nxt, TH_ACK);
22023220a212SGleb Smirnoff 			tp->last_ack_sent = tp->rcv_nxt;
22033220a212SGleb Smirnoff 			m = NULL;
220480ab7c0eSGarrett Wollman 		}
220580ab7c0eSGarrett Wollman 		goto drop;
220680ab7c0eSGarrett Wollman 	}
220780ab7c0eSGarrett Wollman 
220880ab7c0eSGarrett Wollman 	/*
2209df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
2210df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
2211df8bae1dSRodney W. Grimes 	 */
2212be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
221380ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
2214df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
2215d8951c8aSBjoern A. Zeeb 		if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) {
2216df8bae1dSRodney W. Grimes 			/*
2217df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
2218df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
2219df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
2220df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
2221df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
2222df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
2223df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
2224df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
2225df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
2226df8bae1dSRodney W. Grimes 			 */
2227df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
2228df8bae1dSRodney W. Grimes 		} else {
222978b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
223078b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, tlen);
223178b50714SRobert Watson 			TCPSTAT_INC(tcps_pawsdrop);
223207fd333dSMatthew Dillon 			if (tlen)
2233df8bae1dSRodney W. Grimes 				goto dropafterack;
22341ab4789dSMatthew Dillon 			goto drop;
22351ab4789dSMatthew Dillon 		}
2236df8bae1dSRodney W. Grimes 	}
2237df8bae1dSRodney W. Grimes 
2238a0292f23SGarrett Wollman 	/*
223980ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
224080ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
224180ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
224280ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
224380ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
224480ab7c0eSGarrett Wollman 	 */
2245a57815efSBosko Milekic 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
2246a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2247d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
2248a57815efSBosko Milekic 		goto dropwithreset;
2249a57815efSBosko Milekic 	}
225080ab7c0eSGarrett Wollman 
2251fb59c426SYoshinobu Inoue 	todrop = tp->rcv_nxt - th->th_seq;
2252df8bae1dSRodney W. Grimes 	if (todrop > 0) {
2253831ad37eSXin LI 		if (thflags & TH_SYN) {
2254fb59c426SYoshinobu Inoue 			thflags &= ~TH_SYN;
2255fb59c426SYoshinobu Inoue 			th->th_seq++;
2256fb59c426SYoshinobu Inoue 			if (th->th_urp > 1)
2257fb59c426SYoshinobu Inoue 				th->th_urp--;
2258df8bae1dSRodney W. Grimes 			else
2259fb59c426SYoshinobu Inoue 				thflags &= ~TH_URG;
2260df8bae1dSRodney W. Grimes 			todrop--;
2261df8bae1dSRodney W. Grimes 		}
2262df8bae1dSRodney W. Grimes 		/*
2263dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
2264df8bae1dSRodney W. Grimes 		 */
2265fb59c426SYoshinobu Inoue 		if (todrop > tlen
2266fb59c426SYoshinobu Inoue 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
2267dac20301SGarrett Wollman 			/*
2268dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
2269dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
2270dac20301SGarrett Wollman 			 * of sequence; drop it.
2271dac20301SGarrett Wollman 			 */
2272fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
2273dac20301SGarrett Wollman 
2274df8bae1dSRodney W. Grimes 			/*
2275dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
2276dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
2277df8bae1dSRodney W. Grimes 			 */
2278dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
2279fb59c426SYoshinobu Inoue 			todrop = tlen;
228078b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
228178b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, todrop);
2282df8bae1dSRodney W. Grimes 		} else {
228378b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpartduppack);
228478b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop);
2285df8bae1dSRodney W. Grimes 		}
22865acfd95cSMichael Tuexen 		/*
22875acfd95cSMichael Tuexen 		 * DSACK - add SACK block for dropped range
22885acfd95cSMichael Tuexen 		 */
22898f63a52bSMichael Tuexen 		if ((todrop > 0) && (tp->t_flags & TF_SACK_PERMIT)) {
2290ecc5b1d1SMichael Tuexen 			tcp_update_sack_list(tp, th->th_seq,
2291ecc5b1d1SMichael Tuexen 			    th->th_seq + todrop);
22925acfd95cSMichael Tuexen 			/*
22935acfd95cSMichael Tuexen 			 * ACK now, as the next in-sequence segment
22945acfd95cSMichael Tuexen 			 * will clear the DSACK block again
22955acfd95cSMichael Tuexen 			 */
22965acfd95cSMichael Tuexen 			tp->t_flags |= TF_ACKNOW;
22975acfd95cSMichael Tuexen 		}
2298fb59c426SYoshinobu Inoue 		drop_hdrlen += todrop;	/* drop from the top afterwards */
2299fb59c426SYoshinobu Inoue 		th->th_seq += todrop;
2300fb59c426SYoshinobu Inoue 		tlen -= todrop;
2301fb59c426SYoshinobu Inoue 		if (th->th_urp > todrop)
2302fb59c426SYoshinobu Inoue 			th->th_urp -= todrop;
2303df8bae1dSRodney W. Grimes 		else {
2304fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;
2305fb59c426SYoshinobu Inoue 			th->th_urp = 0;
2306df8bae1dSRodney W. Grimes 		}
2307df8bae1dSRodney W. Grimes 	}
2308df8bae1dSRodney W. Grimes 
2309df8bae1dSRodney W. Grimes 	/*
2310df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
2311df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
2312df8bae1dSRodney W. Grimes 	 */
231374703901SGleb Smirnoff 	if ((tp->t_flags & TF_CLOSED) && tlen) {
231407dacf03SAndre Oppermann 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
231507dacf03SAndre Oppermann 			log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data "
231607dacf03SAndre Oppermann 			    "after socket was closed, "
231707dacf03SAndre Oppermann 			    "sending RST and removing tcpcb\n",
2318e31d8aa3SMike Silbersack 			    s, __func__, tcpstates[tp->t_state], tlen);
2319773673c1SAndre Oppermann 			free(s, M_TCPLOG);
2320773673c1SAndre Oppermann 		}
2321d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
2322d1b07f36SRandall Stewart 		/* tcp_close will kill the inp pre-log the Reset */
2323d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
2324df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
232578b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvafterclose);
2326a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
2327df8bae1dSRodney W. Grimes 		goto dropwithreset;
2328df8bae1dSRodney W. Grimes 	}
2329df8bae1dSRodney W. Grimes 
2330df8bae1dSRodney W. Grimes 	/*
2331df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
2332df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
2333df8bae1dSRodney W. Grimes 	 */
2334fb59c426SYoshinobu Inoue 	todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
2335df8bae1dSRodney W. Grimes 	if (todrop > 0) {
233678b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvpackafterwin);
2337fb59c426SYoshinobu Inoue 		if (todrop >= tlen) {
233878b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen);
2339df8bae1dSRodney W. Grimes 			/*
2340df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
2341df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
2342df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
2343df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
2344df8bae1dSRodney W. Grimes 			 * and ack.
2345df8bae1dSRodney W. Grimes 			 */
2346fb59c426SYoshinobu Inoue 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
2347df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
234878b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvwinprobe);
2349df8bae1dSRodney W. Grimes 			} else
2350df8bae1dSRodney W. Grimes 				goto dropafterack;
2351df8bae1dSRodney W. Grimes 		} else
235278b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2353df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
2354fb59c426SYoshinobu Inoue 		tlen -= todrop;
2355fb59c426SYoshinobu Inoue 		thflags &= ~(TH_PUSH|TH_FIN);
2356df8bae1dSRodney W. Grimes 	}
2357df8bae1dSRodney W. Grimes 
2358df8bae1dSRodney W. Grimes 	/*
2359df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
2360df8bae1dSRodney W. Grimes 	 * record its timestamp.
2361cf09195bSPaul Saab 	 * NOTE:
2362cf09195bSPaul Saab 	 * 1) That the test incorporates suggestions from the latest
2363a0292f23SGarrett Wollman 	 *    proposal of the tcplw@cray.com list (Braden 1993/04/26).
2364cf09195bSPaul Saab 	 * 2) That updating only on newer timestamps interferes with
2365cf09195bSPaul Saab 	 *    our earlier PAWS tests, so this check should be solely
2366cf09195bSPaul Saab 	 *    predicated on the sequence space of this segment.
2367cf09195bSPaul Saab 	 * 3) That we modify the segment boundary check to be
2368cf09195bSPaul Saab 	 *        Last.ACK.Sent <= SEG.SEQ + SEG.Len
2369cf09195bSPaul Saab 	 *    instead of RFC1323's
2370cf09195bSPaul Saab 	 *        Last.ACK.Sent < SEG.SEQ + SEG.Len,
2371cf09195bSPaul Saab 	 *    This modified check allows us to overcome RFC1323's
2372cf09195bSPaul Saab 	 *    limitations as described in Stevens TCP/IP Illustrated
2373cf09195bSPaul Saab 	 *    Vol. 2 p.869. In such cases, we can still calculate the
2374cf09195bSPaul Saab 	 *    RTT correctly when RCV.NXT == Last.ACK.Sent.
2375df8bae1dSRodney W. Grimes 	 */
2376be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 &&
2377cf09195bSPaul Saab 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
2378cf09195bSPaul Saab 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
2379cf09195bSPaul Saab 		((thflags & (TH_SYN|TH_FIN)) != 0))) {
2380d8951c8aSBjoern A. Zeeb 		tp->ts_recent_age = tcp_ts_getticks();
2381a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
2382df8bae1dSRodney W. Grimes 	}
2383df8bae1dSRodney W. Grimes 
2384df8bae1dSRodney W. Grimes 	/*
2385a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
2386a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
2387a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
2388a0292f23SGarrett Wollman 	 */
2389fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) == 0) {
2390a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
2391281a0fd4SPatrick Kelsey 		    (tp->t_flags & TF_NEEDSYN)) {
2392281a0fd4SPatrick Kelsey 			if (tp->t_state == TCPS_SYN_RECEIVED &&
239368bd7ed1SJonathan T. Looney 			    IS_FASTOPEN(tp->t_flags)) {
2394281a0fd4SPatrick Kelsey 				tp->snd_wnd = tiwin;
2395281a0fd4SPatrick Kelsey 				cc_conn_init(tp);
2396281a0fd4SPatrick Kelsey 			}
2397a0292f23SGarrett Wollman 			goto step6;
2398281a0fd4SPatrick Kelsey 		} else if (tp->t_flags & TF_ACKNOW)
23995e1aa279SDavid Malone 			goto dropafterack;
2400a0292f23SGarrett Wollman 		else
2401a0292f23SGarrett Wollman 			goto drop;
2402a0292f23SGarrett Wollman 	}
2403df8bae1dSRodney W. Grimes 
2404df8bae1dSRodney W. Grimes 	/*
2405df8bae1dSRodney W. Grimes 	 * Ack processing.
2406df8bae1dSRodney W. Grimes 	 */
2407df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2408df8bae1dSRodney W. Grimes 	/*
2409764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
2410764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
2411764d8cefSBill Fenner 	 * The ACK was checked above.
2412df8bae1dSRodney W. Grimes 	 */
2413df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2414a0292f23SGarrett Wollman 
241578b50714SRobert Watson 		TCPSTAT_INC(tcps_connects);
2416493105c2SGleb Smirnoff 		if (tp->t_flags & TF_SONOTCONN) {
2417493105c2SGleb Smirnoff 			/*
2418493105c2SGleb Smirnoff 			 * Usually SYN_RECEIVED had been created from a LISTEN,
2419493105c2SGleb Smirnoff 			 * and solisten_enqueue() has already marked the socket
2420493105c2SGleb Smirnoff 			 * layer as connected.  If it didn't, which can happen
2421493105c2SGleb Smirnoff 			 * only with an accept_filter(9), then the tp is marked
2422493105c2SGleb Smirnoff 			 * with TF_SONOTCONN.  The other reason for this mark
2423493105c2SGleb Smirnoff 			 * to be set is a simultaneous open, a SYN_RECEIVED
2424493105c2SGleb Smirnoff 			 * that had been created from SYN_SENT.
2425493105c2SGleb Smirnoff 			 */
2426493105c2SGleb Smirnoff 			tp->t_flags &= ~TF_SONOTCONN;
2427df8bae1dSRodney W. Grimes 			soisconnected(so);
2428e80062a2SGleb Smirnoff 		}
2429df8bae1dSRodney W. Grimes 		/* Do window scaling? */
2430df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2431df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
2432df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
2433df8bae1dSRodney W. Grimes 		}
2434116ef4d6SMichael Tuexen 		tp->snd_wnd = tiwin;
2435a0292f23SGarrett Wollman 		/*
2436a0292f23SGarrett Wollman 		 * Make transitions:
2437a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
2438a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
2439a0292f23SGarrett Wollman 		 */
24409b8b58e0SJonathan Lemon 		tp->t_starttime = ticks;
244118a75309SPatrick Kelsey 		if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) {
2442281a0fd4SPatrick Kelsey 			tcp_fastopen_decrement_counter(tp->t_tfo_pending);
2443281a0fd4SPatrick Kelsey 			tp->t_tfo_pending = NULL;
2444281a0fd4SPatrick Kelsey 		}
24458db239dcSMichael Tuexen 		if (tp->t_flags & TF_NEEDFIN) {
244608af8aacSRandall Stewart 			tp->t_acktime = ticks;
24478db239dcSMichael Tuexen 			tcp_state_change(tp, TCPS_FIN_WAIT_1);
24488db239dcSMichael Tuexen 			tp->t_flags &= ~TF_NEEDFIN;
24498db239dcSMichael Tuexen 		} else {
24508db239dcSMichael Tuexen 			tcp_state_change(tp, TCPS_ESTABLISHED);
24518db239dcSMichael Tuexen 			TCP_PROBE5(accept__established, NULL, tp,
24528db239dcSMichael Tuexen 			    m, tp, th);
2453281a0fd4SPatrick Kelsey 			/*
2454281a0fd4SPatrick Kelsey 			 * TFO connections call cc_conn_init() during SYN
2455281a0fd4SPatrick Kelsey 			 * processing.  Calling it again here for such
2456281a0fd4SPatrick Kelsey 			 * connections is not harmless as it would undo the
2457281a0fd4SPatrick Kelsey 			 * snd_cwnd reduction that occurs when a TFO SYN|ACK
2458281a0fd4SPatrick Kelsey 			 * is retransmitted.
2459281a0fd4SPatrick Kelsey 			 */
246068bd7ed1SJonathan T. Looney 			if (!IS_FASTOPEN(tp->t_flags))
2461dbc42409SLawrence Stewart 				cc_conn_init(tp);
24629077f387SGleb Smirnoff 			tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp));
24637ff19458SPaul Traina 		}
2464a0292f23SGarrett Wollman 		/*
2465b2ade6b1SRichard Scheffenegger 		 * Account for the ACK of our SYN prior to
2466b2ade6b1SRichard Scheffenegger 		 * regular ACK processing below, except for
2467b2ade6b1SRichard Scheffenegger 		 * simultaneous SYN, which is handled later.
2468b2ade6b1SRichard Scheffenegger 		 */
2469b2ade6b1SRichard Scheffenegger 		if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN))
2470b2ade6b1SRichard Scheffenegger 			incforsyn = 1;
2471b2ade6b1SRichard Scheffenegger 		/*
2472a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
2473a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
2474a0292f23SGarrett Wollman 		 */
2475032bf749SRichard Scheffenegger 		if (tlen == 0 && (thflags & TH_FIN) == 0) {
2476c28440dbSRandall Stewart 			(void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0,
2477a0292f23SGarrett Wollman 			    (struct mbuf *)0);
2478c348e880SGleb Smirnoff 			tcp_handle_wakeup(tp);
2479032bf749SRichard Scheffenegger 		}
248060ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq - 1;
248193b0017fSPhilippe Charnier 		/* FALLTHROUGH */
2482df8bae1dSRodney W. Grimes 
2483df8bae1dSRodney W. Grimes 	/*
2484df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
2485df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
2486fb59c426SYoshinobu Inoue 	 *	tp->snd_una < th->th_ack <= tp->snd_max
2487fb59c426SYoshinobu Inoue 	 * then advance tp->snd_una to th->th_ack and drop
2488df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
2489df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
2490df8bae1dSRodney W. Grimes 	 */
2491df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
2492df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
2493df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
2494df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
2495df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
2496df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
24975a53ca16SPaul Saab 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
249878b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvacktoomuch);
24995a53ca16SPaul Saab 			goto dropafterack;
25005a53ca16SPaul Saab 		}
2501c21b7b55SRichard Scheffenegger 		if (tcp_is_sack_recovery(tp, &to)) {
2502*893ed42eSRichard Scheffenegger 			sack_changed = tcp_sack_doack(tp, &to, th->th_ack);
2503*893ed42eSRichard Scheffenegger 			if ((sack_changed != SACK_NOCHANGE) &&
25040471a8c7SRichard Scheffenegger 			    (tp->t_flags & TF_LRD)) {
25050471a8c7SRichard Scheffenegger 				tcp_sack_lost_retransmission(tp, th);
25060471a8c7SRichard Scheffenegger 			}
25070471a8c7SRichard Scheffenegger 		} else
250812eeb81fSHiren Panchasara 			/*
250912eeb81fSHiren Panchasara 			 * Reset the value so that previous (valid) value
251012eeb81fSHiren Panchasara 			 * from the last ack with SACK doesn't get used.
251112eeb81fSHiren Panchasara 			 */
251212eeb81fSHiren Panchasara 			tp->sackhint.sacked_bytes = 0;
251339bc9de5SLawrence Stewart 
2514bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
251539bc9de5SLawrence Stewart 		/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
251639bc9de5SLawrence Stewart 		hhook_run_tcp_est_in(tp, th, &to);
2517bd79708dSJonathan T. Looney #endif
251839bc9de5SLawrence Stewart 
2519fb59c426SYoshinobu Inoue 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
25200c39d38dSGleb Smirnoff 			maxseg = tcp_maxseg(tp);
2521021eaf79SHiren Panchasara 			if (tlen == 0 &&
2522021eaf79SHiren Panchasara 			    (tiwin == tp->snd_wnd ||
2523021eaf79SHiren Panchasara 			    (tp->t_flags & TF_SACK_PERMIT))) {
252462b90589SPeter Wemm 				/*
252562b90589SPeter Wemm 				 * If this is the first time we've seen a
252662b90589SPeter Wemm 				 * FIN from the remote, this is not a
252762b90589SPeter Wemm 				 * duplicate and it needs to be processed
252862b90589SPeter Wemm 				 * normally.  This happens during a
252962b90589SPeter Wemm 				 * simultaneous close.
253062b90589SPeter Wemm 				 */
253162b90589SPeter Wemm 				if ((thflags & TH_FIN) &&
253262b90589SPeter Wemm 				    (TCPS_HAVERCVDFIN(tp->t_state) == 0)) {
253362b90589SPeter Wemm 					tp->t_dupacks = 0;
253462b90589SPeter Wemm 					break;
253562b90589SPeter Wemm 				}
253678b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvdupack);
2537df8bae1dSRodney W. Grimes 				/*
2538df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
2539df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
2540df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
25415f30ec9bSEitan Adler 				 * change and FIN isn't set),
25425f30ec9bSEitan Adler 				 * the ack is the biggest we've
2543df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
2544a4641f4eSPedro F. Giffuni 				 * threshold of them, assume a packet
2545df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
2546df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
2547df8bae1dSRodney W. Grimes 				 * window so we send only this one
2548df8bae1dSRodney W. Grimes 				 * packet.
2549df8bae1dSRodney W. Grimes 				 *
2550df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
2551df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
2552df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
2553df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
2554df8bae1dSRodney W. Grimes 				 * the new ssthresh).
2555df8bae1dSRodney W. Grimes 				 *
2556df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
2557df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
2558df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
2559df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
2560df8bae1dSRodney W. Grimes 				 * network.
2561f2512ba1SRui Paulo 				 *
2562f2512ba1SRui Paulo 				 * When using TCP ECN, notify the peer that
2563f2512ba1SRui Paulo 				 * we reduced the cwnd.
2564df8bae1dSRodney W. Grimes 				 */
2565021eaf79SHiren Panchasara 				/*
2566021eaf79SHiren Panchasara 				 * Following 2 kinds of acks should not affect
2567021eaf79SHiren Panchasara 				 * dupack counting:
2568021eaf79SHiren Panchasara 				 * 1) Old acks
2569021eaf79SHiren Panchasara 				 * 2) Acks with SACK but without any new SACK
2570021eaf79SHiren Panchasara 				 * information in them. These could result from
2571021eaf79SHiren Panchasara 				 * any anomaly in the network like a switch
2572021eaf79SHiren Panchasara 				 * duplicating packets or a possible DoS attack.
2573021eaf79SHiren Panchasara 				 */
2574021eaf79SHiren Panchasara 				if (th->th_ack != tp->snd_una ||
2575c21b7b55SRichard Scheffenegger 				    (tcp_is_sack_recovery(tp, &to) &&
257649a6fbe3SRichard Scheffenegger 				    (sack_changed == SACK_NOCHANGE)))
2577021eaf79SHiren Panchasara 					break;
2578021eaf79SHiren Panchasara 				else if (!tcp_timer_active(tp, TT_REXMT))
2579df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
2580cb942153SJeffrey Hsu 				else if (++tp->t_dupacks > tcprexmtthresh ||
2581dbc42409SLawrence Stewart 				     IN_FASTRECOVERY(tp->t_flags)) {
25824b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
25834b7b743cSLawrence Stewart 					    CC_DUPACK);
25840e1d7c25SRichard Scheffenegger 					if (V_tcp_do_prr &&
258549a6fbe3SRichard Scheffenegger 					    IN_FASTRECOVERY(tp->t_flags) &&
258649a6fbe3SRichard Scheffenegger 					    (tp->t_flags & TF_SACK_PERMIT)) {
258749a6fbe3SRichard Scheffenegger 						tcp_do_prr_ack(tp, th, &to, sack_changed);
2588c21b7b55SRichard Scheffenegger 					} else if (tcp_is_sack_recovery(tp, &to) &&
2589dbc42409SLawrence Stewart 					    IN_FASTRECOVERY(tp->t_flags)) {
2590808f11b7SPaul Saab 						int awnd;
259125e6f9edSPaul Saab 
259225e6f9edSPaul Saab 						/*
259325e6f9edSPaul Saab 						 * Compute the amount of data in flight first.
25944c3972f0SHiren Panchasara 						 * We can inject new data into the pipe iff
259525e6f9edSPaul Saab 						 * we have less than 1/2 the original window's
259625e6f9edSPaul Saab 						 * worth of data in flight.
259725e6f9edSPaul Saab 						 */
2598d1de2b05SRichard Scheffenegger 						if (V_tcp_do_newsack)
259912eeb81fSHiren Panchasara 							awnd = tcp_compute_pipe(tp);
260012eeb81fSHiren Panchasara 						else
2601808f11b7SPaul Saab 							awnd = (tp->snd_nxt - tp->snd_fack) +
26020077b016SPaul Saab 								tp->sackhint.sack_bytes_rexmit;
260312eeb81fSHiren Panchasara 
2604808f11b7SPaul Saab 						if (awnd < tp->snd_ssthresh) {
26050c39d38dSGleb Smirnoff 							tp->snd_cwnd += maxseg;
260625e6f9edSPaul Saab 							if (tp->snd_cwnd > tp->snd_ssthresh)
260725e6f9edSPaul Saab 								tp->snd_cwnd = tp->snd_ssthresh;
260825e6f9edSPaul Saab 						}
260925e6f9edSPaul Saab 					} else
26100c39d38dSGleb Smirnoff 						tp->snd_cwnd += maxseg;
261140fa3e40SGleb Smirnoff 					(void) tcp_output(tp);
261246f58482SJonathan Lemon 					goto drop;
26133c40e1d5SRichard Scheffenegger 				} else if (tp->t_dupacks == tcprexmtthresh ||
26143c40e1d5SRichard Scheffenegger 					    (tp->t_flags & TF_SACK_PERMIT &&
2615d1de2b05SRichard Scheffenegger 					     V_tcp_do_newsack &&
26163c40e1d5SRichard Scheffenegger 					     tp->sackhint.sacked_bytes >
26173c40e1d5SRichard Scheffenegger 					     (tcprexmtthresh - 1) * maxseg)) {
26183c40e1d5SRichard Scheffenegger enter_recovery:
26193c40e1d5SRichard Scheffenegger 					/*
26203c40e1d5SRichard Scheffenegger 					 * Above is the RFC6675 trigger condition of
26213c40e1d5SRichard Scheffenegger 					 * more than (dupthresh-1)*maxseg sacked data.
26223c40e1d5SRichard Scheffenegger 					 * If the count of holes in the
26233c40e1d5SRichard Scheffenegger 					 * scoreboard is >= dupthresh, we could
26243c40e1d5SRichard Scheffenegger 					 * also enter loss recovery, but don't
26253c40e1d5SRichard Scheffenegger 					 * have that value readily available.
26263c40e1d5SRichard Scheffenegger 					 */
26273c40e1d5SRichard Scheffenegger 					tp->t_dupacks = tcprexmtthresh;
2628cb942153SJeffrey Hsu 					tcp_seq onxt = tp->snd_nxt;
2629a0445c2eSJayanth Vijayaraghavan 
2630a0445c2eSJayanth Vijayaraghavan 					/*
26310e1d7c25SRichard Scheffenegger 					 * If we're doing sack, or prr, check
26320e1d7c25SRichard Scheffenegger 					 * to see if we're already in sack
2633a0445c2eSJayanth Vijayaraghavan 					 * recovery. If we're not doing sack,
2634a0445c2eSJayanth Vijayaraghavan 					 * check to see if we're in newreno
2635a0445c2eSJayanth Vijayaraghavan 					 * recovery.
2636a0445c2eSJayanth Vijayaraghavan 					 */
26370e1d7c25SRichard Scheffenegger 					if (V_tcp_do_prr ||
26380e1d7c25SRichard Scheffenegger 					    (tp->t_flags & TF_SACK_PERMIT)) {
2639dbc42409SLawrence Stewart 						if (IN_FASTRECOVERY(tp->t_flags)) {
2640a0445c2eSJayanth Vijayaraghavan 							tp->t_dupacks = 0;
2641a0445c2eSJayanth Vijayaraghavan 							break;
2642a0445c2eSJayanth Vijayaraghavan 						}
2643dbc42409SLawrence Stewart 					} else {
2644a0445c2eSJayanth Vijayaraghavan 						if (SEQ_LEQ(th->th_ack,
26459d11646dSJeffrey Hsu 						    tp->snd_recover)) {
2646cb942153SJeffrey Hsu 							tp->t_dupacks = 0;
2647cb942153SJeffrey Hsu 							break;
264846f58482SJonathan Lemon 						}
2649a0445c2eSJayanth Vijayaraghavan 					}
2650dbc42409SLawrence Stewart 					/* Congestion signal before ack. */
2651dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_NDUPACK);
26524b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
26534b7b743cSLawrence Stewart 					    CC_DUPACK);
2654b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
26559b8b58e0SJonathan Lemon 					tp->t_rtttime = 0;
26560e1d7c25SRichard Scheffenegger 					if (V_tcp_do_prr) {
26570e1d7c25SRichard Scheffenegger 						/*
26580e1d7c25SRichard Scheffenegger 						 * snd_ssthresh is already updated by
26590e1d7c25SRichard Scheffenegger 						 * cc_cong_signal.
26600e1d7c25SRichard Scheffenegger 						 */
2661c21b7b55SRichard Scheffenegger 						if (tcp_is_sack_recovery(tp, &to)) {
266249a6fbe3SRichard Scheffenegger 							/*
266349a6fbe3SRichard Scheffenegger 							 * Exclude Limited Transmit
266449a6fbe3SRichard Scheffenegger 							 * segments here
266549a6fbe3SRichard Scheffenegger 							 */
2666e9071000SRichard Scheffenegger 							tp->sackhint.prr_delivered =
266749a6fbe3SRichard Scheffenegger 							    maxseg;
266874d7fc87SRichard Scheffenegger 						} else {
266974d7fc87SRichard Scheffenegger 							tp->sackhint.prr_delivered =
267074d7fc87SRichard Scheffenegger 							    imin(tp->snd_max - tp->snd_una,
267174d7fc87SRichard Scheffenegger 							    imin(INT_MAX / 65536,
267274d7fc87SRichard Scheffenegger 								tp->t_dupacks) * maxseg);
267374d7fc87SRichard Scheffenegger 						}
2674bc7ee8e5SRichard Scheffenegger 						tp->sackhint.recover_fs = max(1,
2675bc7ee8e5SRichard Scheffenegger 						    tp->snd_nxt - tp->snd_una);
26760e1d7c25SRichard Scheffenegger 					}
2677c21b7b55SRichard Scheffenegger 					if (tcp_is_sack_recovery(tp, &to)) {
267878b50714SRobert Watson 						TCPSTAT_INC(
267978b50714SRobert Watson 						    tcps_sack_recovery_episode);
2680a3574665SMichael Tuexen 						tp->snd_recover = tp->snd_nxt;
26810c39d38dSGleb Smirnoff 						tp->snd_cwnd = maxseg;
268240fa3e40SGleb Smirnoff 						(void) tcp_output(tp);
26833c40e1d5SRichard Scheffenegger 						if (SEQ_GT(th->th_ack, tp->snd_una))
26843c40e1d5SRichard Scheffenegger 							goto resume_partialack;
26856d90faf3SPaul Saab 						goto drop;
26866d90faf3SPaul Saab 					}
2687fb59c426SYoshinobu Inoue 					tp->snd_nxt = th->th_ack;
26880c39d38dSGleb Smirnoff 					tp->snd_cwnd = maxseg;
268940fa3e40SGleb Smirnoff 					(void) tcp_output(tp);
269048d2549cSJeffrey Hsu 					KASSERT(tp->snd_limited <= 2,
2691302ce8d6SAndre Oppermann 					    ("%s: tp->snd_limited too big",
2692302ce8d6SAndre Oppermann 					    __func__));
2693df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
26940c39d38dSGleb Smirnoff 					     maxseg *
269548d2549cSJeffrey Hsu 					     (tp->t_dupacks - tp->snd_limited);
2696df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
2697df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
2698df8bae1dSRodney W. Grimes 					goto drop;
2699603724d3SBjoern A. Zeeb 				} else if (V_tcp_do_rfc3042) {
270062d4443fSHiren Panchasara 					/*
270162d4443fSHiren Panchasara 					 * Process first and second duplicate
270262d4443fSHiren Panchasara 					 * ACKs. Each indicates a segment
270362d4443fSHiren Panchasara 					 * leaving the network, creating room
270462d4443fSHiren Panchasara 					 * for more. Make sure we can send a
270562d4443fSHiren Panchasara 					 * packet on reception of each duplicate
270662d4443fSHiren Panchasara 					 * ACK by increasing snd_cwnd by one
270762d4443fSHiren Panchasara 					 * segment. Restore the original
270862d4443fSHiren Panchasara 					 * snd_cwnd after packet transmission.
270962d4443fSHiren Panchasara 					 */
27104b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
27114b7b743cSLawrence Stewart 					    CC_DUPACK);
27123ac12506SJonathan T. Looney 					uint32_t oldcwnd = tp->snd_cwnd;
271348d2549cSJeffrey Hsu 					tcp_seq oldsndmax = tp->snd_max;
271448d2549cSJeffrey Hsu 					u_int sent;
27155628dd08SAndre Oppermann 					int avail;
271689c02376SJeffrey Hsu 
2717582a954bSJeffrey Hsu 					KASSERT(tp->t_dupacks == 1 ||
2718582a954bSJeffrey Hsu 					    tp->t_dupacks == 2,
2719302ce8d6SAndre Oppermann 					    ("%s: dupacks not 1 or 2",
2720302ce8d6SAndre Oppermann 					    __func__));
272161a36e3dSJeffrey Hsu 					if (tp->t_dupacks == 1)
272248d2549cSJeffrey Hsu 						tp->snd_limited = 0;
272361a36e3dSJeffrey Hsu 					tp->snd_cwnd =
272461a36e3dSJeffrey Hsu 					    (tp->snd_nxt - tp->snd_una) +
272561a36e3dSJeffrey Hsu 					    (tp->t_dupacks - tp->snd_limited) *
27260c39d38dSGleb Smirnoff 					    maxseg;
27275628dd08SAndre Oppermann 					/*
27285628dd08SAndre Oppermann 					 * Only call tcp_output when there
27295ae83e0dSMichael Tuexen 					 * is new data available to be sent
27305ae83e0dSMichael Tuexen 					 * or we need to send an ACK.
27315628dd08SAndre Oppermann 					 */
27325628dd08SAndre Oppermann 					SOCKBUF_LOCK(&so->so_snd);
2733cfa6009eSGleb Smirnoff 					avail = sbavail(&so->so_snd) -
27345628dd08SAndre Oppermann 					    (tp->snd_nxt - tp->snd_una);
27355628dd08SAndre Oppermann 					SOCKBUF_UNLOCK(&so->so_snd);
27365ae83e0dSMichael Tuexen 					if (avail > 0 || tp->t_flags & TF_ACKNOW)
273740fa3e40SGleb Smirnoff 						(void) tcp_output(tp);
273848d2549cSJeffrey Hsu 					sent = tp->snd_max - oldsndmax;
27390c39d38dSGleb Smirnoff 					if (sent > maxseg) {
274089c02376SJeffrey Hsu 						KASSERT((tp->t_dupacks == 2 &&
274189c02376SJeffrey Hsu 						    tp->snd_limited == 0) ||
27420c39d38dSGleb Smirnoff 						   (sent == maxseg + 1 &&
274389c02376SJeffrey Hsu 						    tp->t_flags & TF_SENTFIN),
2744302ce8d6SAndre Oppermann 						    ("%s: sent too much",
2745302ce8d6SAndre Oppermann 						    __func__));
274648d2549cSJeffrey Hsu 						tp->snd_limited = 2;
274748d2549cSJeffrey Hsu 					} else if (sent > 0)
274848d2549cSJeffrey Hsu 						++tp->snd_limited;
2749582a954bSJeffrey Hsu 					tp->snd_cwnd = oldcwnd;
2750582a954bSJeffrey Hsu 					goto drop;
2751df8bae1dSRodney W. Grimes 				}
2752021eaf79SHiren Panchasara 			}
2753df8bae1dSRodney W. Grimes 			break;
2754021eaf79SHiren Panchasara 		} else {
2755021eaf79SHiren Panchasara 			/*
2756021eaf79SHiren Panchasara 			 * This ack is advancing the left edge, reset the
2757021eaf79SHiren Panchasara 			 * counter.
2758021eaf79SHiren Panchasara 			 */
2759021eaf79SHiren Panchasara 			tp->t_dupacks = 0;
2760021eaf79SHiren Panchasara 			/*
2761021eaf79SHiren Panchasara 			 * If this ack also has new SACK info, increment the
2762f359d6ebSRichard Scheffenegger 			 * counter as per rfc6675. The variable
2763f359d6ebSRichard Scheffenegger 			 * sack_changed tracks all changes to the SACK
2764f359d6ebSRichard Scheffenegger 			 * scoreboard, including when partial ACKs without
2765f359d6ebSRichard Scheffenegger 			 * SACK options are received, and clear the scoreboard
2766f359d6ebSRichard Scheffenegger 			 * from the left side. Such partial ACKs should not be
2767f359d6ebSRichard Scheffenegger 			 * counted as dupacks here.
2768021eaf79SHiren Panchasara 			 */
2769c21b7b55SRichard Scheffenegger 			if (tcp_is_sack_recovery(tp, &to) &&
277049a6fbe3SRichard Scheffenegger 			    (sack_changed != SACK_NOCHANGE)) {
2771021eaf79SHiren Panchasara 				tp->t_dupacks++;
27723c40e1d5SRichard Scheffenegger 				/* limit overhead by setting maxseg last */
27733c40e1d5SRichard Scheffenegger 				if (!IN_FASTRECOVERY(tp->t_flags) &&
27743c40e1d5SRichard Scheffenegger 				    (tp->sackhint.sacked_bytes >
27753c40e1d5SRichard Scheffenegger 				    ((tcprexmtthresh - 1) *
27763c40e1d5SRichard Scheffenegger 				    (maxseg = tcp_maxseg(tp))))) {
27773c40e1d5SRichard Scheffenegger 					goto enter_recovery;
27783c40e1d5SRichard Scheffenegger 				}
27793c40e1d5SRichard Scheffenegger 			}
2780df8bae1dSRodney W. Grimes 		}
2781cb942153SJeffrey Hsu 
27823c40e1d5SRichard Scheffenegger resume_partialack:
2783302ce8d6SAndre Oppermann 		KASSERT(SEQ_GT(th->th_ack, tp->snd_una),
2784302ce8d6SAndre Oppermann 		    ("%s: th_ack <= snd_una", __func__));
2785cb942153SJeffrey Hsu 
2786df8bae1dSRodney W. Grimes 		/*
2787df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
2788df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
2789df8bae1dSRodney W. Grimes 		 */
2790dbc42409SLawrence Stewart 		if (IN_FASTRECOVERY(tp->t_flags)) {
2791cb942153SJeffrey Hsu 			if (SEQ_LT(th->th_ack, tp->snd_recover)) {
27923529149eSAndre Oppermann 				if (tp->t_flags & TF_SACK_PERMIT)
2793eb3a59a8SRichard Scheffenegger 					if (V_tcp_do_prr && to.to_flags & TOF_SACK) {
2794eb3a59a8SRichard Scheffenegger 						tcp_timer_activate(tp, TT_REXMT, 0);
2795eb3a59a8SRichard Scheffenegger 						tp->t_rtttime = 0;
279649a6fbe3SRichard Scheffenegger 						tcp_do_prr_ack(tp, th, &to, sack_changed);
2797eb3a59a8SRichard Scheffenegger 						tp->t_flags |= TF_ACKNOW;
2798eb3a59a8SRichard Scheffenegger 						(void) tcp_output(tp);
2799eb3a59a8SRichard Scheffenegger 					} else
28006d90faf3SPaul Saab 						tcp_sack_partialack(tp, th);
28016d90faf3SPaul Saab 				else
2802c068736aSJeffrey Hsu 					tcp_newreno_partial_ack(tp, th);
2803dbc42409SLawrence Stewart 			} else
2804dbc42409SLawrence Stewart 				cc_post_recovery(tp, th);
2805b9f803b7SRichard Scheffenegger 		} else if (IN_CONGRECOVERY(tp->t_flags)) {
2806b9f803b7SRichard Scheffenegger 			if (SEQ_LT(th->th_ack, tp->snd_recover)) {
2807b9f803b7SRichard Scheffenegger 				if (V_tcp_do_prr) {
2808b9f803b7SRichard Scheffenegger 					tp->sackhint.delivered_data = BYTES_THIS_ACK(tp, th);
2809b9f803b7SRichard Scheffenegger 					tp->snd_fack = th->th_ack;
281049a6fbe3SRichard Scheffenegger 					/*
281149a6fbe3SRichard Scheffenegger 					 * During ECN cwnd reduction
281249a6fbe3SRichard Scheffenegger 					 * always use PRR-SSRB
281349a6fbe3SRichard Scheffenegger 					 */
281449a6fbe3SRichard Scheffenegger 					tcp_do_prr_ack(tp, th, &to, SACK_CHANGE);
2815b9f803b7SRichard Scheffenegger 					(void) tcp_output(tp);
2816b9f803b7SRichard Scheffenegger 				}
2817b9f803b7SRichard Scheffenegger 			} else
2818b9f803b7SRichard Scheffenegger 				cc_post_recovery(tp, th);
281946f58482SJonathan Lemon 		}
2820a0292f23SGarrett Wollman 		/*
2821a0292f23SGarrett Wollman 		 * If we reach this point, ACK is not a duplicate,
2822a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
2823a0292f23SGarrett Wollman 		 */
2824a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
2825a0292f23SGarrett Wollman 			/*
2826a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
2827a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
282807e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
282907e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
283007e43e10SAndras Olah 			 * we can do window scaling.
2831a0292f23SGarrett Wollman 			 */
2832a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
2833a0292f23SGarrett Wollman 			tp->snd_una++;
283407e43e10SAndras Olah 			/* Do window scaling? */
283507e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
283607e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
283707e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2838464fcfbcSAndre Oppermann 				/* Send window already scaled. */
283907e43e10SAndras Olah 			}
2840a0292f23SGarrett Wollman 		}
2841a0292f23SGarrett Wollman 
2842a0292f23SGarrett Wollman process_ACK:
28439eb0e832SGleb Smirnoff 		INP_WLOCK_ASSERT(inp);
28447cfc6904SRobert Watson 
2845b2ade6b1SRichard Scheffenegger 		/*
2846b2ade6b1SRichard Scheffenegger 		 * Adjust for the SYN bit in sequence space,
2847b2ade6b1SRichard Scheffenegger 		 * but don't account for it in cwnd calculations.
2848b2ade6b1SRichard Scheffenegger 		 * This is for the SYN_RECEIVED, non-simultaneous
2849b2ade6b1SRichard Scheffenegger 		 * SYN case. SYN_SENT and simultaneous SYN are
2850b2ade6b1SRichard Scheffenegger 		 * treated elsewhere.
2851b2ade6b1SRichard Scheffenegger 		 */
2852b2ade6b1SRichard Scheffenegger 		if (incforsyn)
2853b2ade6b1SRichard Scheffenegger 			tp->snd_una++;
2854dbc42409SLawrence Stewart 		acked = BYTES_THIS_ACK(tp, th);
2855b8c2cd15SJonathan T. Looney 		KASSERT(acked >= 0, ("%s: acked unexepectedly negative "
2856b8c2cd15SJonathan T. Looney 		    "(tp->snd_una=%u, th->th_ack=%u, tp=%p, m=%p)", __func__,
2857b8c2cd15SJonathan T. Looney 		    tp->snd_una, th->th_ack, tp, m));
28584b7b743cSLawrence Stewart 		TCPSTAT_ADD(tcps_rcvackpack, nsegs);
285978b50714SRobert Watson 		TCPSTAT_ADD(tcps_rcvackbyte, acked);
2860df8bae1dSRodney W. Grimes 
2861df8bae1dSRodney W. Grimes 		/*
28629b8b58e0SJonathan Lemon 		 * If we just performed our first retransmit, and the ACK
28639b8b58e0SJonathan Lemon 		 * arrives within our recovery window, then it was a mistake
28649b8b58e0SJonathan Lemon 		 * to do the retransmit in the first place.  Recover our
28659b8b58e0SJonathan Lemon 		 * original cwnd and ssthresh, and proceed to transmit where
28669b8b58e0SJonathan Lemon 		 * we left off.
28679b8b58e0SJonathan Lemon 		 */
286810d20c84SMatt Macy 		if (tp->t_rxtshift == 1 &&
286910d20c84SMatt Macy 		    tp->t_flags & TF_PREVVALID &&
28704531b345SRichard Scheffenegger 		    tp->t_badrxtwin != 0 &&
28714531b345SRichard Scheffenegger 		    to.to_flags & TOF_TS &&
28724531b345SRichard Scheffenegger 		    to.to_tsecr != 0 &&
28734531b345SRichard Scheffenegger 		    TSTMP_LT(to.to_tsecr, tp->t_badrxtwin))
2874dbc42409SLawrence Stewart 			cc_cong_signal(tp, th, CC_RTO_ERR);
28759b8b58e0SJonathan Lemon 
28769b8b58e0SJonathan Lemon 		/*
2877df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
2878df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
2879df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
2880df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
2881df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
2882df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
2883df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
2884fa55172bSMatthew Dillon 		 *
2885fa55172bSMatthew Dillon 		 * Some boxes send broken timestamp replies
2886fa55172bSMatthew Dillon 		 * during the SYN+ACK phase, ignore
2887fa55172bSMatthew Dillon 		 * timestamps of 0 or we could calculate a
2888fa55172bSMatthew Dillon 		 * huge RTT and blow up the retransmit timer.
2889df8bae1dSRodney W. Grimes 		 */
2890d8951c8aSBjoern A. Zeeb 		if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) {
28913ac12506SJonathan T. Looney 			uint32_t t;
2892d8951c8aSBjoern A. Zeeb 
2893d8951c8aSBjoern A. Zeeb 			t = tcp_ts_getticks() - to.to_tsecr;
2894d8951c8aSBjoern A. Zeeb 			if (!tp->t_rttlow || tp->t_rttlow > t)
2895d8951c8aSBjoern A. Zeeb 				tp->t_rttlow = t;
2896d8951c8aSBjoern A. Zeeb 			tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1);
2897fa55172bSMatthew Dillon 		} else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
2898eaf80179SAndre Oppermann 			if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime)
2899eaf80179SAndre Oppermann 				tp->t_rttlow = ticks - tp->t_rtttime;
29009b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
2901fa55172bSMatthew Dillon 		}
2902df8bae1dSRodney W. Grimes 
290308af8aacSRandall Stewart 		SOCKBUF_LOCK(&so->so_snd);
290408af8aacSRandall Stewart 		/*
290508af8aacSRandall Stewart 		 * Clear t_acktime if remote side has ACKd all data in the
290608af8aacSRandall Stewart 		 * socket buffer and FIN (if applicable).
290708af8aacSRandall Stewart 		 * Otherwise, update t_acktime if we received a sufficiently
290808af8aacSRandall Stewart 		 * large ACK.
290908af8aacSRandall Stewart 		 */
291008af8aacSRandall Stewart 		if ((tp->t_state <= TCPS_CLOSE_WAIT &&
291108af8aacSRandall Stewart 		    acked == sbavail(&so->so_snd)) ||
291208af8aacSRandall Stewart 		    acked > sbavail(&so->so_snd))
291308af8aacSRandall Stewart 			tp->t_acktime = 0;
291408af8aacSRandall Stewart 		else if (acked > 1)
291508af8aacSRandall Stewart 			tp->t_acktime = ticks;
291608af8aacSRandall Stewart 
2917df8bae1dSRodney W. Grimes 		/*
2918df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
2919df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
2920df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
2921df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
2922df8bae1dSRodney W. Grimes 		 */
2923fb59c426SYoshinobu Inoue 		if (th->th_ack == tp->snd_max) {
2924b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
2925df8bae1dSRodney W. Grimes 			needoutput = 1;
2926b8152ba7SAndre Oppermann 		} else if (!tcp_timer_active(tp, TT_PERSIST))
292708af8aacSRandall Stewart 			tcp_timer_activate(tp, TT_REXMT, TP_RXTCUR(tp));
2928a0292f23SGarrett Wollman 
2929a0292f23SGarrett Wollman 		/*
2930a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
2931a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
2932a0292f23SGarrett Wollman 		 */
293308af8aacSRandall Stewart 		if (acked == 0) {
293408af8aacSRandall Stewart 			SOCKBUF_UNLOCK(&so->so_snd);
2935a0292f23SGarrett Wollman 			goto step6;
293608af8aacSRandall Stewart 		}
2937a0292f23SGarrett Wollman 
2938df8bae1dSRodney W. Grimes 		/*
2939dbc42409SLawrence Stewart 		 * Let the congestion control algorithm update congestion
2940dbc42409SLawrence Stewart 		 * control related information. This typically means increasing
2941dbc42409SLawrence Stewart 		 * the congestion window.
2942df8bae1dSRodney W. Grimes 		 */
29434b7b743cSLawrence Stewart 		cc_ack_received(tp, th, nsegs, CC_ACK);
2944dbc42409SLawrence Stewart 
2945cfa6009eSGleb Smirnoff 		if (acked > sbavail(&so->so_snd)) {
2946b8c2cd15SJonathan T. Looney 			if (tp->snd_wnd >= sbavail(&so->so_snd))
2947cfa6009eSGleb Smirnoff 				tp->snd_wnd -= sbavail(&so->so_snd);
2948b8c2cd15SJonathan T. Looney 			else
2949b8c2cd15SJonathan T. Looney 				tp->snd_wnd = 0;
2950c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd,
2951cfa6009eSGleb Smirnoff 			    (int)sbavail(&so->so_snd));
2952df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
2953df8bae1dSRodney W. Grimes 		} else {
2954c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd, acked);
29553ac12506SJonathan T. Looney 			if (tp->snd_wnd >= (uint32_t) acked)
295660ee3bb2SAndre Oppermann 				tp->snd_wnd -= acked;
2957b8c2cd15SJonathan T. Looney 			else
2958b8c2cd15SJonathan T. Looney 				tp->snd_wnd = 0;
2959df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
2960df8bae1dSRodney W. Grimes 		}
2961032bf749SRichard Scheffenegger 		/* NB: sowwakeup_locked() does an implicit unlock. */
2962032bf749SRichard Scheffenegger 		sowwakeup_locked(so);
2963c11a15bfSGleb Smirnoff 		m_freem(mfree);
2964e8949f74SAndre Oppermann 		/* Detect una wraparound. */
2965dbc42409SLawrence Stewart 		if (!IN_RECOVERY(tp->t_flags) &&
29669d11646dSJeffrey Hsu 		    SEQ_GT(tp->snd_una, tp->snd_recover) &&
29679d11646dSJeffrey Hsu 		    SEQ_LEQ(th->th_ack, tp->snd_recover))
29689d11646dSJeffrey Hsu 			tp->snd_recover = th->th_ack - 1;
2969dbc42409SLawrence Stewart 		/* XXXLAS: Can this be moved up into cc_post_recovery? */
2970dbc42409SLawrence Stewart 		if (IN_RECOVERY(tp->t_flags) &&
297124cb0f22SLawrence Stewart 		    SEQ_GEQ(th->th_ack, tp->snd_recover)) {
2972dbc42409SLawrence Stewart 			EXIT_RECOVERY(tp->t_flags);
297324cb0f22SLawrence Stewart 		}
2974fb59c426SYoshinobu Inoue 		tp->snd_una = th->th_ack;
29753529149eSAndre Oppermann 		if (tp->t_flags & TF_SACK_PERMIT) {
29766d90faf3SPaul Saab 			if (SEQ_GT(tp->snd_una, tp->snd_recover))
29776d90faf3SPaul Saab 				tp->snd_recover = tp->snd_una;
29786d90faf3SPaul Saab 		}
2979df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2980df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
2981df8bae1dSRodney W. Grimes 
2982df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2983df8bae1dSRodney W. Grimes 		/*
2984df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
2985df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
2986df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
2987df8bae1dSRodney W. Grimes 		 */
2988df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2989df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2990df8bae1dSRodney W. Grimes 				/*
2991df8bae1dSRodney W. Grimes 				 * If we can't receive any more
2992df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
2993df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
2994df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
2995df8bae1dSRodney W. Grimes 				 * we'll hang forever.
2996340c35deSJonathan Lemon 				 */
2997c0b99ffaSRobert Watson 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
299803e49181SSeigo Tanimura 					soisdisconnected(so);
29999077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
30009077f387SGleb Smirnoff 					    (tcp_fast_finwait2_recycle ?
30019077f387SGleb Smirnoff 					    tcp_finwait2_timeout :
30029077f387SGleb Smirnoff 					    TP_MAXIDLE(tp)));
30034cc20ab1SSeigo Tanimura 				}
300457f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_2);
3005df8bae1dSRodney W. Grimes 			}
3006df8bae1dSRodney W. Grimes 			break;
3007df8bae1dSRodney W. Grimes 
3008df8bae1dSRodney W. Grimes 		/*
3009df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
3010df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
3011df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
3012df8bae1dSRodney W. Grimes 		 * the segment.
3013df8bae1dSRodney W. Grimes 		 */
3014df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
3015df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
301611a20fb8SJeffrey Hsu 				tcp_twstart(tp);
3017340c35deSJonathan Lemon 				m_freem(m);
3018679d9708SAndre Oppermann 				return;
3019df8bae1dSRodney W. Grimes 			}
3020df8bae1dSRodney W. Grimes 			break;
3021df8bae1dSRodney W. Grimes 
3022df8bae1dSRodney W. Grimes 		/*
3023df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
3024df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
3025df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
3026df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
3027df8bae1dSRodney W. Grimes 		 */
3028df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
3029df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
3030df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
3031df8bae1dSRodney W. Grimes 				goto drop;
3032df8bae1dSRodney W. Grimes 			}
3033df8bae1dSRodney W. Grimes 			break;
3034df8bae1dSRodney W. Grimes 		}
3035df8bae1dSRodney W. Grimes 	}
3036df8bae1dSRodney W. Grimes 
3037df8bae1dSRodney W. Grimes step6:
30389eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
30397cfc6904SRobert Watson 
3040df8bae1dSRodney W. Grimes 	/*
304160ee3bb2SAndre Oppermann 	 * Update window information.
304260ee3bb2SAndre Oppermann 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
3043df8bae1dSRodney W. Grimes 	 */
304460ee3bb2SAndre Oppermann 	if ((thflags & TH_ACK) &&
304560ee3bb2SAndre Oppermann 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
304660ee3bb2SAndre Oppermann 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
304760ee3bb2SAndre Oppermann 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
304860ee3bb2SAndre Oppermann 		/* keep track of pure window updates */
304960ee3bb2SAndre Oppermann 		if (tlen == 0 &&
305060ee3bb2SAndre Oppermann 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
305178b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvwinupd);
3052df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
305360ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq;
305460ee3bb2SAndre Oppermann 		tp->snd_wl2 = th->th_ack;
3055df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
3056df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
305760ee3bb2SAndre Oppermann 		needoutput = 1;
3058df8bae1dSRodney W. Grimes 	}
3059df8bae1dSRodney W. Grimes 
3060df8bae1dSRodney W. Grimes 	/*
3061df8bae1dSRodney W. Grimes 	 * Process segments with URG.
3062df8bae1dSRodney W. Grimes 	 */
3063fb59c426SYoshinobu Inoue 	if ((thflags & TH_URG) && th->th_urp &&
3064df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
3065df8bae1dSRodney W. Grimes 		/*
3066df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
3067df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
3068df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
3069df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
3070df8bae1dSRodney W. Grimes 		 */
307118ad5842SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
3072cfa6009eSGleb Smirnoff 		if (th->th_urp + sbavail(&so->so_rcv) > sb_max) {
3073fb59c426SYoshinobu Inoue 			th->th_urp = 0;			/* XXX */
3074fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;		/* XXX */
307518ad5842SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);	/* XXX */
3076df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
3077df8bae1dSRodney W. Grimes 		}
3078df8bae1dSRodney W. Grimes 		/*
3079df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
3080df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
3081df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
3082df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
3083df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
3084df8bae1dSRodney W. Grimes 		 *
3085df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
3086df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
3087df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
3088df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
3089df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
3090df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
3091df8bae1dSRodney W. Grimes 		 */
3092fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
3093fb59c426SYoshinobu Inoue 			tp->rcv_up = th->th_seq + th->th_urp;
3094cfa6009eSGleb Smirnoff 			so->so_oobmark = sbavail(&so->so_rcv) +
3095df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
3096927c5ceaSRobert Watson 			if (so->so_oobmark == 0)
3097c0b99ffaSRobert Watson 				so->so_rcv.sb_state |= SBS_RCVATMARK;
3098df8bae1dSRodney W. Grimes 			sohasoutofband(so);
3099df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
3100df8bae1dSRodney W. Grimes 		}
310118ad5842SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
3102df8bae1dSRodney W. Grimes 		/*
3103df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
3104df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
3105df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
3106df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
3107df8bae1dSRodney W. Grimes 		 */
31083ac12506SJonathan T. Looney 		if (th->th_urp <= (uint32_t)tlen &&
310930613f56SJeffrey Hsu 		    !(so->so_options & SO_OOBINLINE)) {
311030613f56SJeffrey Hsu 			/* hdr drop is delayed */
311130613f56SJeffrey Hsu 			tcp_pulloutofband(so, th, m, drop_hdrlen);
311230613f56SJeffrey Hsu 		}
3113c068736aSJeffrey Hsu 	} else {
3114df8bae1dSRodney W. Grimes 		/*
3115df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
3116df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
3117df8bae1dSRodney W. Grimes 		 * with the receive window.
3118df8bae1dSRodney W. Grimes 		 */
3119df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
3120df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
3121c068736aSJeffrey Hsu 	}
3122df8bae1dSRodney W. Grimes dodata:							/* XXX */
31239eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
31247cfc6904SRobert Watson 
3125df8bae1dSRodney W. Grimes 	/*
3126df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
3127df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
3128df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
3129df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
3130df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
3131df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
3132df8bae1dSRodney W. Grimes 	 */
3133281a0fd4SPatrick Kelsey 	tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
313468bd7ed1SJonathan T. Looney 		   IS_FASTOPEN(tp->t_flags));
3135f1ea4e41SRandall Stewart 	if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) &&
3136df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
313769e03620SPaul Saab 		tcp_seq save_start = th->th_seq;
31385acfd95cSMichael Tuexen 		tcp_seq save_rnxt  = tp->rcv_nxt;
31395acfd95cSMichael Tuexen 		int     save_tlen  = tlen;
3140fb59c426SYoshinobu Inoue 		m_adj(m, drop_hdrlen);	/* delayed header drop */
3141e4b64281SJesper Skriver 		/*
3142c068736aSJeffrey Hsu 		 * Insert segment which includes th into TCP reassembly queue
3143c068736aSJeffrey Hsu 		 * with control block tp.  Set thflags to whether reassembly now
3144c068736aSJeffrey Hsu 		 * includes a segment with FIN.  This handles the common case
3145c068736aSJeffrey Hsu 		 * inline (segment is the next to be received on an established
3146c068736aSJeffrey Hsu 		 * connection, and the queue is empty), avoiding linkage into
3147c068736aSJeffrey Hsu 		 * and removal from the queue and repetition of various
3148c068736aSJeffrey Hsu 		 * conversions.
3149c068736aSJeffrey Hsu 		 * Set DELACK for segments received in order, but ack
3150c068736aSJeffrey Hsu 		 * immediately when segments are out of order (so
3151c068736aSJeffrey Hsu 		 * fast retransmit can work).
3152e4b64281SJesper Skriver 		 */
31534741bfcbSPatrick Kelsey 		if (th->th_seq == tp->rcv_nxt &&
3154c28440dbSRandall Stewart 		    SEGQ_EMPTY(tp) &&
3155281a0fd4SPatrick Kelsey 		    (TCPS_HAVEESTABLISHED(tp->t_state) ||
3156281a0fd4SPatrick Kelsey 		     tfo_syn)) {
3157281a0fd4SPatrick Kelsey 			if (DELAY_ACK(tp, tlen) || tfo_syn)
31583bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
3159e4b64281SJesper Skriver 			else
3160e4b64281SJesper Skriver 				tp->t_flags |= TF_ACKNOW;
3161e4b64281SJesper Skriver 			tp->rcv_nxt += tlen;
3162e854dd38SRandall Stewart 			if (tlen &&
3163e854dd38SRandall Stewart 			    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
3164e854dd38SRandall Stewart 			    (tp->t_fbyte_in == 0)) {
3165e854dd38SRandall Stewart 				tp->t_fbyte_in = ticks;
3166e854dd38SRandall Stewart 				if (tp->t_fbyte_in == 0)
3167e854dd38SRandall Stewart 					tp->t_fbyte_in = 1;
3168e854dd38SRandall Stewart 				if (tp->t_fbyte_out && tp->t_fbyte_in)
3169e854dd38SRandall Stewart 					tp->t_flags2 |= TF2_FBYTES_COMPLETE;
3170e854dd38SRandall Stewart 			}
31711ebf4607SRichard Scheffenegger 			thflags = tcp_get_flags(th) & TH_FIN;
317278b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpack);
317378b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
31741e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
3175c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
3176c1c36a2cSMike Silbersack 				m_freem(m);
3177c1c36a2cSMike Silbersack 			else
3178651e4e6aSGleb Smirnoff 				sbappendstream_locked(&so->so_rcv, m, 0);
31794d0770f1SRichard Scheffenegger 			tp->t_flags |= TF_WAKESOR;
3180e4b64281SJesper Skriver 		} else {
3181e8949f74SAndre Oppermann 			/*
3182e8949f74SAndre Oppermann 			 * XXX: Due to the header drop above "th" is
3183e8949f74SAndre Oppermann 			 * theoretically invalid by now.  Fortunately
3184e8949f74SAndre Oppermann 			 * m_adj() doesn't actually frees any mbufs
3185e8949f74SAndre Oppermann 			 * when trimming from the head.
3186e8949f74SAndre Oppermann 			 */
31875acfd95cSMichael Tuexen 			tcp_seq temp = save_start;
31884747500dSRandall Stewart 
31895acfd95cSMichael Tuexen 			thflags = tcp_reass(tp, th, &temp, &tlen, m);
3190e4b64281SJesper Skriver 			tp->t_flags |= TF_ACKNOW;
3191e4b64281SJesper Skriver 		}
319240f41eceSMichael Tuexen 		if ((tp->t_flags & TF_SACK_PERMIT) &&
319340f41eceSMichael Tuexen 		    (save_tlen > 0) &&
319440f41eceSMichael Tuexen 		    TCPS_HAVEESTABLISHED(tp->t_state)) {
31956d261981SMichael Tuexen 			if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) {
3196b5a154d8SMichael Tuexen 				/*
3197b5a154d8SMichael Tuexen 				 * DSACK actually handled in the fastpath
3198b5a154d8SMichael Tuexen 				 * above.
3199b5a154d8SMichael Tuexen 				 */
3200ecc5b1d1SMichael Tuexen 				tcp_update_sack_list(tp, save_start,
3201ecc5b1d1SMichael Tuexen 				    save_start + save_tlen);
3202ecc5b1d1SMichael Tuexen 			} else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) {
3203ecc5b1d1SMichael Tuexen 				if ((tp->rcv_numsacks >= 1) &&
3204ecc5b1d1SMichael Tuexen 				    (tp->sackblks[0].end == save_start)) {
32056d261981SMichael Tuexen 					/*
32066d261981SMichael Tuexen 					 * Partial overlap, recorded at todrop
32076d261981SMichael Tuexen 					 * above.
32086d261981SMichael Tuexen 					 */
32096d261981SMichael Tuexen 					tcp_update_sack_list(tp,
32106d261981SMichael Tuexen 					    tp->sackblks[0].start,
3211ecc5b1d1SMichael Tuexen 					    tp->sackblks[0].end);
3212ecc5b1d1SMichael Tuexen 				} else {
3213ecc5b1d1SMichael Tuexen 					tcp_update_dsack_list(tp, save_start,
3214ecc5b1d1SMichael Tuexen 					    save_start + save_tlen);
3215ecc5b1d1SMichael Tuexen 				}
32166d261981SMichael Tuexen 			} else if (tlen >= save_tlen) {
3217b5a154d8SMichael Tuexen 				/* Update of sackblks. */
3218ecc5b1d1SMichael Tuexen 				tcp_update_dsack_list(tp, save_start,
3219ecc5b1d1SMichael Tuexen 				    save_start + save_tlen);
3220ecc5b1d1SMichael Tuexen 			} else if (tlen > 0) {
3221ecc5b1d1SMichael Tuexen 				tcp_update_dsack_list(tp, save_start,
3222ecc5b1d1SMichael Tuexen 				    save_start + tlen);
32235acfd95cSMichael Tuexen 			}
32245acfd95cSMichael Tuexen 		}
3225c348e880SGleb Smirnoff 		tcp_handle_wakeup(tp);
3226302ce8d6SAndre Oppermann #if 0
3227df8bae1dSRodney W. Grimes 		/*
3228df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
3229df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
3230df8bae1dSRodney W. Grimes 		 * buffer size.
3231302ce8d6SAndre Oppermann 		 * XXX: Unused.
3232df8bae1dSRodney W. Grimes 		 */
3233f701e30dSJohn Baldwin 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt))
3234df8bae1dSRodney W. Grimes 			len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
3235f701e30dSJohn Baldwin 		else
3236f701e30dSJohn Baldwin 			len = so->so_rcv.sb_hiwat;
3237302ce8d6SAndre Oppermann #endif
3238df8bae1dSRodney W. Grimes 	} else {
3239df8bae1dSRodney W. Grimes 		m_freem(m);
3240fb59c426SYoshinobu Inoue 		thflags &= ~TH_FIN;
3241df8bae1dSRodney W. Grimes 	}
3242df8bae1dSRodney W. Grimes 
3243df8bae1dSRodney W. Grimes 	/*
3244df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
3245df8bae1dSRodney W. Grimes 	 * that the connection is closing.
3246df8bae1dSRodney W. Grimes 	 */
3247fb59c426SYoshinobu Inoue 	if (thflags & TH_FIN) {
3248df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
32494d0770f1SRichard Scheffenegger 			/* The socket upcall is handled by socantrcvmore. */
3250032bf749SRichard Scheffenegger 			socantrcvmore(so);
3251a0292f23SGarrett Wollman 			/*
3252a0292f23SGarrett Wollman 			 * If connection is half-synchronized
3253d3eede9dSAndras Olah 			 * (ie NEEDSYN flag on) then delay ACK,
3254a0292f23SGarrett Wollman 			 * so it may be piggybacked when SYN is sent.
3255a0292f23SGarrett Wollman 			 * Otherwise, since we received a FIN then no
3256a0292f23SGarrett Wollman 			 * more input can be expected, send ACK now.
3257a0292f23SGarrett Wollman 			 */
32583bfd6421SJonathan Lemon 			if (tp->t_flags & TF_NEEDSYN)
32593bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
3260a0292f23SGarrett Wollman 			else
3261df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
3262df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
3263df8bae1dSRodney W. Grimes 		}
3264df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
3265df8bae1dSRodney W. Grimes 		/*
3266df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
3267df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
3268df8bae1dSRodney W. Grimes 		 */
3269df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
32709b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
32719b8b58e0SJonathan Lemon 			/* FALLTHROUGH */
3272df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
327357f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSE_WAIT);
3274df8bae1dSRodney W. Grimes 			break;
3275df8bae1dSRodney W. Grimes 
3276df8bae1dSRodney W. Grimes 		/*
3277df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
3278df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
3279df8bae1dSRodney W. Grimes 		 */
3280df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
328157f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSING);
3282df8bae1dSRodney W. Grimes 			break;
3283df8bae1dSRodney W. Grimes 
3284df8bae1dSRodney W. Grimes 		/*
3285df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
3286df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
3287df8bae1dSRodney W. Grimes 		 * standard timers.
3288df8bae1dSRodney W. Grimes 		 */
3289df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
3290340c35deSJonathan Lemon 			tcp_twstart(tp);
3291679d9708SAndre Oppermann 			return;
3292df8bae1dSRodney W. Grimes 		}
3293df8bae1dSRodney W. Grimes 	}
32942b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
3295df8bae1dSRodney W. Grimes 
3296df8bae1dSRodney W. Grimes 	/*
3297df8bae1dSRodney W. Grimes 	 * Return any desired output.
3298df8bae1dSRodney W. Grimes 	 */
3299df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
330040fa3e40SGleb Smirnoff 		(void) tcp_output(tp);
33017792ea27SJeffrey Hsu 
3302a14c749fSJonathan Lemon check_delack:
33039eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
3304252ca428SRobert Watson 
3305a14c749fSJonathan Lemon 	if (tp->t_flags & TF_DELACK) {
33063bfd6421SJonathan Lemon 		tp->t_flags &= ~TF_DELACK;
3307b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
33083bfd6421SJonathan Lemon 	}
33099eb0e832SGleb Smirnoff 	INP_WUNLOCK(inp);
3310679d9708SAndre Oppermann 	return;
3311df8bae1dSRodney W. Grimes 
3312df8bae1dSRodney W. Grimes dropafterack:
3313df8bae1dSRodney W. Grimes 	/*
3314df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
3315df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
331680ab7c0eSGarrett Wollman 	 *
331780ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
331880ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
331980ab7c0eSGarrett Wollman 	 * RST have been dropped.
332080ab7c0eSGarrett Wollman 	 *
332180ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
332280ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
332380ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
332480ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
332580ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
332680ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
3327df8bae1dSRodney W. Grimes 	 */
3328fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
3329fb59c426SYoshinobu Inoue 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
3330a57815efSBosko Milekic 	     SEQ_GT(th->th_ack, tp->snd_max)) ) {
3331a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
3332d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
3333a57815efSBosko Milekic 		goto dropwithreset;
3334a57815efSBosko Milekic 	}
33352b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
3336df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
333740fa3e40SGleb Smirnoff 	(void) tcp_output(tp);
33389eb0e832SGleb Smirnoff 	INP_WUNLOCK(inp);
3339d6915262SRobert Watson 	m_freem(m);
3340679d9708SAndre Oppermann 	return;
3341df8bae1dSRodney W. Grimes 
3342df8bae1dSRodney W. Grimes dropwithreset:
3343a0ca0871SRobert Watson 	if (tp != NULL) {
3344302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
33459eb0e832SGleb Smirnoff 		INP_WUNLOCK(inp);
3346dd8ac7f9SRobert Watson 	} else
3347a0ca0871SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
3348679d9708SAndre Oppermann 	return;
3349df8bae1dSRodney W. Grimes 
3350df8bae1dSRodney W. Grimes drop:
3351df8bae1dSRodney W. Grimes 	/*
3352df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
3353df8bae1dSRodney W. Grimes 	 */
33542b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
33554d0770f1SRichard Scheffenegger 	if (tp != NULL) {
33569eb0e832SGleb Smirnoff 		INP_WUNLOCK(inp);
33574d0770f1SRichard Scheffenegger 	}
3358d6915262SRobert Watson 	m_freem(m);
3359302ce8d6SAndre Oppermann }
3360302ce8d6SAndre Oppermann 
3361302ce8d6SAndre Oppermann /*
33620ca3f933SAndre Oppermann  * Issue RST and make ACK acceptable to originator of segment.
33630ca3f933SAndre Oppermann  * The mbuf must still include the original packet header.
33640ca3f933SAndre Oppermann  * tp may be NULL.
3365302ce8d6SAndre Oppermann  */
336655bceb1eSRandall Stewart void
3367302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp,
3368302ce8d6SAndre Oppermann     int tlen, int rstreason)
3369302ce8d6SAndre Oppermann {
3370b287c6c7SBjoern A. Zeeb #ifdef INET
3371302ce8d6SAndre Oppermann 	struct ip *ip;
3372b287c6c7SBjoern A. Zeeb #endif
3373302ce8d6SAndre Oppermann #ifdef INET6
3374302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6;
3375302ce8d6SAndre Oppermann #endif
33767a3244ccSRobert Watson 
33777a3244ccSRobert Watson 	if (tp != NULL) {
33789eb0e832SGleb Smirnoff 		INP_LOCK_ASSERT(tptoinpcb(tp));
33797a3244ccSRobert Watson 	}
33807a3244ccSRobert Watson 
33810ca3f933SAndre Oppermann 	/* Don't bother if destination was broadcast/multicast. */
33821ebf4607SRichard Scheffenegger 	if ((tcp_get_flags(th) & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
3383302ce8d6SAndre Oppermann 		goto drop;
3384302ce8d6SAndre Oppermann #ifdef INET6
3385302ce8d6SAndre Oppermann 	if (mtod(m, struct ip *)->ip_v == 6) {
3386302ce8d6SAndre Oppermann 		ip6 = mtod(m, struct ip6_hdr *);
3387302ce8d6SAndre Oppermann 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
3388302ce8d6SAndre Oppermann 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
3389302ce8d6SAndre Oppermann 			goto drop;
3390302ce8d6SAndre Oppermann 		/* IPv6 anycast check is done at tcp6_input() */
3391b287c6c7SBjoern A. Zeeb 	}
3392302ce8d6SAndre Oppermann #endif
3393b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3394b287c6c7SBjoern A. Zeeb 	else
3395b287c6c7SBjoern A. Zeeb #endif
3396b287c6c7SBjoern A. Zeeb #ifdef INET
3397302ce8d6SAndre Oppermann 	{
3398302ce8d6SAndre Oppermann 		ip = mtod(m, struct ip *);
3399302ce8d6SAndre Oppermann 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
3400302ce8d6SAndre Oppermann 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
3401302ce8d6SAndre Oppermann 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
3402302ce8d6SAndre Oppermann 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
3403302ce8d6SAndre Oppermann 			goto drop;
3404302ce8d6SAndre Oppermann 	}
3405b287c6c7SBjoern A. Zeeb #endif
3406302ce8d6SAndre Oppermann 
3407302ce8d6SAndre Oppermann 	/* Perform bandwidth limiting. */
3408302ce8d6SAndre Oppermann 	if (badport_bandlim(rstreason) < 0)
3409302ce8d6SAndre Oppermann 		goto drop;
3410302ce8d6SAndre Oppermann 
3411302ce8d6SAndre Oppermann 	/* tcp_respond consumes the mbuf chain. */
34121ebf4607SRichard Scheffenegger 	if (tcp_get_flags(th) & TH_ACK) {
3413302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0,
3414302ce8d6SAndre Oppermann 		    th->th_ack, TH_RST);
3415302ce8d6SAndre Oppermann 	} else {
34161ebf4607SRichard Scheffenegger 		if (tcp_get_flags(th) & TH_SYN)
3417302ce8d6SAndre Oppermann 			tlen++;
34181ebf4607SRichard Scheffenegger 		if (tcp_get_flags(th) & TH_FIN)
34194ddd5aadSMichael Tuexen 			tlen++;
3420302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
3421302ce8d6SAndre Oppermann 		    (tcp_seq)0, TH_RST|TH_ACK);
3422302ce8d6SAndre Oppermann 	}
3423302ce8d6SAndre Oppermann 	return;
3424302ce8d6SAndre Oppermann drop:
3425302ce8d6SAndre Oppermann 	m_freem(m);
3426df8bae1dSRodney W. Grimes }
3427df8bae1dSRodney W. Grimes 
3428be2ac88cSJonathan Lemon /*
3429be2ac88cSJonathan Lemon  * Parse TCP options and place in tcpopt.
3430be2ac88cSJonathan Lemon  */
343155bceb1eSRandall Stewart void
3432ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags)
3433df8bae1dSRodney W. Grimes {
3434df8bae1dSRodney W. Grimes 	int opt, optlen;
3435df8bae1dSRodney W. Grimes 
3436be2ac88cSJonathan Lemon 	to->to_flags = 0;
3437df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
3438df8bae1dSRodney W. Grimes 		opt = cp[0];
3439df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
3440df8bae1dSRodney W. Grimes 			break;
3441df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
3442df8bae1dSRodney W. Grimes 			optlen = 1;
3443df8bae1dSRodney W. Grimes 		else {
3444b474779fSJun-ichiro itojun Hagino 			if (cnt < 2)
3445b474779fSJun-ichiro itojun Hagino 				break;
3446df8bae1dSRodney W. Grimes 			optlen = cp[1];
3447b474779fSJun-ichiro itojun Hagino 			if (optlen < 2 || optlen > cnt)
3448df8bae1dSRodney W. Grimes 				break;
3449df8bae1dSRodney W. Grimes 		}
3450df8bae1dSRodney W. Grimes 		switch (opt) {
3451df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
3452df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
3453df8bae1dSRodney W. Grimes 				continue;
3454f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3455df8bae1dSRodney W. Grimes 				continue;
3456be2ac88cSJonathan Lemon 			to->to_flags |= TOF_MSS;
3457be2ac88cSJonathan Lemon 			bcopy((char *)cp + 2,
3458be2ac88cSJonathan Lemon 			    (char *)&to->to_mss, sizeof(to->to_mss));
3459fd8e4ebcSMike Barcroft 			to->to_mss = ntohs(to->to_mss);
3460df8bae1dSRodney W. Grimes 			break;
3461df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
3462df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
3463df8bae1dSRodney W. Grimes 				continue;
3464f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3465df8bae1dSRodney W. Grimes 				continue;
3466be2ac88cSJonathan Lemon 			to->to_flags |= TOF_SCALE;
346702a1a643SAndre Oppermann 			to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT);
3468df8bae1dSRodney W. Grimes 			break;
3469df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
3470df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
3471df8bae1dSRodney W. Grimes 				continue;
3472be2ac88cSJonathan Lemon 			to->to_flags |= TOF_TS;
3473a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
3474a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
3475fd8e4ebcSMike Barcroft 			to->to_tsval = ntohl(to->to_tsval);
3476a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
3477a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
3478fd8e4ebcSMike Barcroft 			to->to_tsecr = ntohl(to->to_tsecr);
3479df8bae1dSRodney W. Grimes 			break;
34801cfd4b53SBruce M Simpson 		case TCPOPT_SIGNATURE:
3481fcf59617SAndrey V. Elsukov 			/*
3482fcf59617SAndrey V. Elsukov 			 * In order to reply to a host which has set the
3483fcf59617SAndrey V. Elsukov 			 * TCP_SIGNATURE option in its initial SYN, we have
3484fcf59617SAndrey V. Elsukov 			 * to record the fact that the option was observed
3485fcf59617SAndrey V. Elsukov 			 * here for the syncache code to perform the correct
3486fcf59617SAndrey V. Elsukov 			 * response.
3487fcf59617SAndrey V. Elsukov 			 */
34881cfd4b53SBruce M Simpson 			if (optlen != TCPOLEN_SIGNATURE)
34891cfd4b53SBruce M Simpson 				continue;
3490df47e437SAndre Oppermann 			to->to_flags |= TOF_SIGNATURE;
3491df47e437SAndre Oppermann 			to->to_signature = cp + 2;
34921cfd4b53SBruce M Simpson 			break;
34936d90faf3SPaul Saab 		case TCPOPT_SACK_PERMITTED:
3494f72167f4SAndre Oppermann 			if (optlen != TCPOLEN_SACK_PERMITTED)
34956d90faf3SPaul Saab 				continue;
3496f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3497f72167f4SAndre Oppermann 				continue;
3498603724d3SBjoern A. Zeeb 			if (!V_tcp_do_sack)
3499f72167f4SAndre Oppermann 				continue;
3500fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACKPERM;
35016d90faf3SPaul Saab 			break;
35026d90faf3SPaul Saab 		case TCPOPT_SACK:
35035a53ca16SPaul Saab 			if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
35046d90faf3SPaul Saab 				continue;
3505b728e902SAndre Oppermann 			if (flags & TO_SYN)
3506b728e902SAndre Oppermann 				continue;
3507fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACK;
35085a53ca16SPaul Saab 			to->to_nsacks = (optlen - 2) / TCPOLEN_SACK;
35095a53ca16SPaul Saab 			to->to_sacks = cp + 2;
351078b50714SRobert Watson 			TCPSTAT_INC(tcps_sack_rcv_blocks);
35116d90faf3SPaul Saab 			break;
3512281a0fd4SPatrick Kelsey 		case TCPOPT_FAST_OPEN:
3513c560df6fSPatrick Kelsey 			/*
3514c560df6fSPatrick Kelsey 			 * Cookie length validation is performed by the
3515c560df6fSPatrick Kelsey 			 * server side cookie checking code or the client
3516c560df6fSPatrick Kelsey 			 * side cookie cache update code.
3517c560df6fSPatrick Kelsey 			 */
3518281a0fd4SPatrick Kelsey 			if (!(flags & TO_SYN))
3519281a0fd4SPatrick Kelsey 				continue;
3520c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
3521c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
3522281a0fd4SPatrick Kelsey 				continue;
3523281a0fd4SPatrick Kelsey 			to->to_flags |= TOF_FASTOPEN;
3524281a0fd4SPatrick Kelsey 			to->to_tfo_len = optlen - 2;
3525281a0fd4SPatrick Kelsey 			to->to_tfo_cookie = to->to_tfo_len ? cp + 2 : NULL;
3526281a0fd4SPatrick Kelsey 			break;
3527be2ac88cSJonathan Lemon 		default:
3528be2ac88cSJonathan Lemon 			continue;
3529df8bae1dSRodney W. Grimes 		}
3530df8bae1dSRodney W. Grimes 	}
3531df8bae1dSRodney W. Grimes }
3532df8bae1dSRodney W. Grimes 
3533df8bae1dSRodney W. Grimes /*
3534df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
3535df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
3536df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
3537df8bae1dSRodney W. Grimes  * sequencing purposes.
3538df8bae1dSRodney W. Grimes  */
353955bceb1eSRandall Stewart void
3540ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m,
3541ad3f9ab3SAndre Oppermann     int off)
3542df8bae1dSRodney W. Grimes {
3543fb59c426SYoshinobu Inoue 	int cnt = off + th->th_urp - 1;
3544df8bae1dSRodney W. Grimes 
3545df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
3546df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
3547df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
3548df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
3549df8bae1dSRodney W. Grimes 
35509eb0e832SGleb Smirnoff 			INP_WLOCK_ASSERT(tptoinpcb(tp));
35517a3244ccSRobert Watson 
3552df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
3553df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
3554df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
3555df8bae1dSRodney W. Grimes 			m->m_len--;
355669a34685SYoshinobu Inoue 			if (m->m_flags & M_PKTHDR)
355769a34685SYoshinobu Inoue 				m->m_pkthdr.len--;
3558df8bae1dSRodney W. Grimes 			return;
3559df8bae1dSRodney W. Grimes 		}
3560df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
3561df8bae1dSRodney W. Grimes 		m = m->m_next;
35624dfdffe9SAndre Oppermann 		if (m == NULL)
3563df8bae1dSRodney W. Grimes 			break;
3564df8bae1dSRodney W. Grimes 	}
3565df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
3566df8bae1dSRodney W. Grimes }
3567df8bae1dSRodney W. Grimes 
3568df8bae1dSRodney W. Grimes /*
3569df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
3570df8bae1dSRodney W. Grimes  * and update averages and current timeout.
3571df8bae1dSRodney W. Grimes  */
357255bceb1eSRandall Stewart void
3573ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt)
3574df8bae1dSRodney W. Grimes {
3575ad3f9ab3SAndre Oppermann 	int delta;
3576233e8c18SGarrett Wollman 
35779eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
35782be3bf22SRobert Watson 
357978b50714SRobert Watson 	TCPSTAT_INC(tcps_rttupdated);
358018b83b62SRichard Scheffenegger 	if (tp->t_rttupdated < UCHAR_MAX)
3581233e8c18SGarrett Wollman 		tp->t_rttupdated++;
3582adc56f5aSEdward Tomasz Napierala #ifdef STATS
3583adc56f5aSEdward Tomasz Napierala 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT,
3584adc56f5aSEdward Tomasz Napierala 	    imax(0, rtt * 1000 / hz));
3585adc56f5aSEdward Tomasz Napierala #endif
35865ede40dcSRyan Stone 	if ((tp->t_srtt != 0) && (tp->t_rxtshift <= TCP_RTT_INVALIDATE)) {
3587233e8c18SGarrett Wollman 		/*
3588233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
3589233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
3590233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
3591233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
3592233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
3593233e8c18SGarrett Wollman 		 */
3594233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
3595233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
3596233e8c18SGarrett Wollman 
3597233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
3598233e8c18SGarrett Wollman 			tp->t_srtt = 1;
3599233e8c18SGarrett Wollman 
3600233e8c18SGarrett Wollman 		/*
3601233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
3602233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
3603233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
3604233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
3605233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
3606233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
3607233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
3608233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
3609233e8c18SGarrett Wollman 		 */
3610233e8c18SGarrett Wollman 		if (delta < 0)
3611233e8c18SGarrett Wollman 			delta = -delta;
3612233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
3613233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
3614233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
3615233e8c18SGarrett Wollman 	} else {
3616233e8c18SGarrett Wollman 		/*
3617233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
3618233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
3619233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
3620233e8c18SGarrett Wollman 		 */
3621233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
3622233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
3623233e8c18SGarrett Wollman 	}
36249b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
3625df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
3626df8bae1dSRodney W. Grimes 
3627df8bae1dSRodney W. Grimes 	/*
3628df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
3629df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
3630df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
3631df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
3632df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
3633df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
3634df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
3635df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
3636df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
3637df8bae1dSRodney W. Grimes 	 */
3638233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
36399e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
3640df8bae1dSRodney W. Grimes 
3641df8bae1dSRodney W. Grimes 	/*
3642df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
3643df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
3644df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
3645df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
3646df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
3647df8bae1dSRodney W. Grimes 	 */
3648df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
3649df8bae1dSRodney W. Grimes }
3650df8bae1dSRodney W. Grimes 
3651df8bae1dSRodney W. Grimes /*
3652df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
3653df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
36544faaea55SAndre Oppermann  * If none, use an mss that can be handled on the outgoing interface
36554faaea55SAndre Oppermann  * without forcing IP to fragment.  If no route is found, route has no mtu,
3656df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
3657df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
3658df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
3659df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
3660df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
3661df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
3662df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
3663a0292f23SGarrett Wollman  *
36640c39d38dSGleb Smirnoff  * NOTE that resulting t_maxseg doesn't include space for TCP options or
36650c39d38dSGleb Smirnoff  * IP options, e.g. IPSEC data, since length of this data may vary, and
36660c39d38dSGleb Smirnoff  * thus it is calculated for every segment separately in tcp_output().
3667a0292f23SGarrett Wollman  *
366897d8d152SAndre Oppermann  * NOTE that this routine is only called when we process an incoming
3669ef341ee1SGleb Smirnoff  * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS
3670ef341ee1SGleb Smirnoff  * settings are handled in tcp_mssopt().
3671df8bae1dSRodney W. Grimes  */
3672a0292f23SGarrett Wollman void
3673ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer,
36743c914c54SAndre Oppermann     struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap)
3675df8bae1dSRodney W. Grimes {
3676b287c6c7SBjoern A. Zeeb 	int mss = 0;
36773ac12506SJonathan T. Looney 	uint32_t maxmtu = 0;
36789eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
367997d8d152SAndre Oppermann 	struct hc_metrics_lite metrics;
3680fb59c426SYoshinobu Inoue #ifdef INET6
3681c068736aSJeffrey Hsu 	int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
3682c068736aSJeffrey Hsu 	size_t min_protoh = isipv6 ?
3683c068736aSJeffrey Hsu 			    sizeof (struct ip6_hdr) + sizeof (struct tcphdr) :
3684c068736aSJeffrey Hsu 			    sizeof (struct tcpiphdr);
3685c068736aSJeffrey Hsu #else
36869e644c23SMichael Tuexen 	 size_t min_protoh = sizeof(struct tcpiphdr);
3687fb59c426SYoshinobu Inoue #endif
3688df8bae1dSRodney W. Grimes 
36899eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
36907a3244ccSRobert Watson 
36919e644c23SMichael Tuexen 	if (tp->t_port)
36929e644c23SMichael Tuexen 		min_protoh += V_tcp_udp_tunneling_overhead;
3693ef341ee1SGleb Smirnoff 	if (mtuoffer != -1) {
3694ef341ee1SGleb Smirnoff 		KASSERT(offer == -1, ("%s: conflict", __func__));
3695ef341ee1SGleb Smirnoff 		offer = mtuoffer - min_protoh;
3696ef341ee1SGleb Smirnoff 	}
3697ef341ee1SGleb Smirnoff 
3698e8949f74SAndre Oppermann 	/* Initialize. */
369997d8d152SAndre Oppermann #ifdef INET6
370097d8d152SAndre Oppermann 	if (isipv6) {
37013c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu6(&inp->inp_inc, cap);
37020c39d38dSGleb Smirnoff 		tp->t_maxseg = V_tcp_v6mssdflt;
3703b287c6c7SBjoern A. Zeeb 	}
370497d8d152SAndre Oppermann #endif
3705b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3706b287c6c7SBjoern A. Zeeb 	else
3707b287c6c7SBjoern A. Zeeb #endif
3708b287c6c7SBjoern A. Zeeb #ifdef INET
370997d8d152SAndre Oppermann 	{
37103c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu(&inp->inp_inc, cap);
37110c39d38dSGleb Smirnoff 		tp->t_maxseg = V_tcp_mssdflt;
3712df8bae1dSRodney W. Grimes 	}
3713b287c6c7SBjoern A. Zeeb #endif
3714df8bae1dSRodney W. Grimes 
371597d8d152SAndre Oppermann 	/*
3716e8949f74SAndre Oppermann 	 * No route to sender, stay with default mss and return.
371797d8d152SAndre Oppermann 	 */
37186f01cac6SBjoern A. Zeeb 	if (maxmtu == 0) {
37196f01cac6SBjoern A. Zeeb 		/*
37208e5c87f4SBjoern A. Zeeb 		 * In case we return early we need to initialize metrics
37216f01cac6SBjoern A. Zeeb 		 * to a defined state as tcp_hc_get() would do for us
37226f01cac6SBjoern A. Zeeb 		 * if there was no cache hit.
37236f01cac6SBjoern A. Zeeb 		 */
37246f01cac6SBjoern A. Zeeb 		if (metricptr != NULL)
37256f01cac6SBjoern A. Zeeb 			bzero(metricptr, sizeof(struct hc_metrics_lite));
372697d8d152SAndre Oppermann 		return;
37276f01cac6SBjoern A. Zeeb 	}
372897d8d152SAndre Oppermann 
3729e8949f74SAndre Oppermann 	/* What have we got? */
373097d8d152SAndre Oppermann 	switch (offer) {
373197d8d152SAndre Oppermann 		case 0:
373297d8d152SAndre Oppermann 			/*
373397d8d152SAndre Oppermann 			 * Offer == 0 means that there was no MSS on the SYN
3734c3b02504SBjoern A. Zeeb 			 * segment, in this case we use tcp_mssdflt as
37350c39d38dSGleb Smirnoff 			 * already assigned to t_maxseg above.
373697d8d152SAndre Oppermann 			 */
37370c39d38dSGleb Smirnoff 			offer = tp->t_maxseg;
373897d8d152SAndre Oppermann 			break;
373997d8d152SAndre Oppermann 
374097d8d152SAndre Oppermann 		case -1:
3741a0292f23SGarrett Wollman 			/*
3742c94c54e4SAndre Oppermann 			 * Offer == -1 means that we didn't receive SYN yet.
3743a0292f23SGarrett Wollman 			 */
374497d8d152SAndre Oppermann 			/* FALLTHROUGH */
374597d8d152SAndre Oppermann 
374697d8d152SAndre Oppermann 		default:
3747a0292f23SGarrett Wollman 			/*
374853369ac9SAndre Oppermann 			 * Prevent DoS attack with too small MSS. Round up
374953369ac9SAndre Oppermann 			 * to at least minmss.
375053369ac9SAndre Oppermann 			 */
3751603724d3SBjoern A. Zeeb 			offer = max(offer, V_tcp_minmss);
375297d8d152SAndre Oppermann 	}
3753a0292f23SGarrett Wollman 
3754df8bae1dSRodney W. Grimes 	/*
3755e8949f74SAndre Oppermann 	 * rmx information is now retrieved from tcp_hostcache.
3756df8bae1dSRodney W. Grimes 	 */
375797d8d152SAndre Oppermann 	tcp_hc_get(&inp->inp_inc, &metrics);
37583cee92e0SBjoern A. Zeeb 	if (metricptr != NULL)
37593cee92e0SBjoern A. Zeeb 		bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite));
376097d8d152SAndre Oppermann 
3761df8bae1dSRodney W. Grimes 	/*
3762cc412412SHiren Panchasara 	 * If there's a discovered mtu in tcp hostcache, use it.
3763cc412412SHiren Panchasara 	 * Else, use the link mtu.
3764df8bae1dSRodney W. Grimes 	 */
376597d8d152SAndre Oppermann 	if (metrics.rmx_mtu)
37662d166c02SAndre Oppermann 		mss = min(metrics.rmx_mtu, maxmtu) - min_protoh;
3767c068736aSJeffrey Hsu 	else {
376831b3783cSHajimu UMEMOTO #ifdef INET6
3769fb59c426SYoshinobu Inoue 		if (isipv6) {
377097d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3771603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
377297d8d152SAndre Oppermann 			    !in6_localaddr(&inp->in6p_faddr))
3773603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_v6mssdflt);
3774b287c6c7SBjoern A. Zeeb 		}
3775b3399803SHajimu UMEMOTO #endif
3776b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3777b287c6c7SBjoern A. Zeeb 		else
3778b287c6c7SBjoern A. Zeeb #endif
3779b287c6c7SBjoern A. Zeeb #ifdef INET
378097d8d152SAndre Oppermann 		{
378197d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3782603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
378397d8d152SAndre Oppermann 			    !in_localaddr(inp->inp_faddr))
3784603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_mssdflt);
3785a0292f23SGarrett Wollman 		}
3786b287c6c7SBjoern A. Zeeb #endif
37873cee92e0SBjoern A. Zeeb 		/*
37883cee92e0SBjoern A. Zeeb 		 * XXX - The above conditional (mss = maxmtu - min_protoh)
37893cee92e0SBjoern A. Zeeb 		 * probably violates the TCP spec.
37903cee92e0SBjoern A. Zeeb 		 * The problem is that, since we don't know the
37913cee92e0SBjoern A. Zeeb 		 * other end's MSS, we are supposed to use a conservative
37923cee92e0SBjoern A. Zeeb 		 * default.  But, if we do that, then MTU discovery will
37933cee92e0SBjoern A. Zeeb 		 * never actually take place, because the conservative
37943cee92e0SBjoern A. Zeeb 		 * default is much less than the MTUs typically seen
37953cee92e0SBjoern A. Zeeb 		 * on the Internet today.  For the moment, we'll sweep
37963cee92e0SBjoern A. Zeeb 		 * this under the carpet.
37973cee92e0SBjoern A. Zeeb 		 *
37983cee92e0SBjoern A. Zeeb 		 * The conservative default might not actually be a problem
37993cee92e0SBjoern A. Zeeb 		 * if the only case this occurs is when sending an initial
38003cee92e0SBjoern A. Zeeb 		 * SYN with options and data to a host we've never talked
38013cee92e0SBjoern A. Zeeb 		 * to before.  Then, they will reply with an MSS value which
38023cee92e0SBjoern A. Zeeb 		 * will get recorded and the new parameters should get
38033cee92e0SBjoern A. Zeeb 		 * recomputed.  For Further Study.
38043cee92e0SBjoern A. Zeeb 		 */
380597d8d152SAndre Oppermann 	}
3806a0292f23SGarrett Wollman 	mss = min(mss, offer);
380797d8d152SAndre Oppermann 
3808a0292f23SGarrett Wollman 	/*
38090c39d38dSGleb Smirnoff 	 * Sanity check: make sure that maxseg will be large
38103cee92e0SBjoern A. Zeeb 	 * enough to allow some data on segments even if the
38113cee92e0SBjoern A. Zeeb 	 * all the option space is used (40bytes).  Otherwise
38123cee92e0SBjoern A. Zeeb 	 * funny things may happen in tcp_output.
38130c39d38dSGleb Smirnoff 	 *
38140c39d38dSGleb Smirnoff 	 * XXXGL: shouldn't we reserve space for IP/IPv6 options?
38153cee92e0SBjoern A. Zeeb 	 */
38163cee92e0SBjoern A. Zeeb 	mss = max(mss, 64);
38173cee92e0SBjoern A. Zeeb 
381897d8d152SAndre Oppermann 	tp->t_maxseg = mss;
38193cee92e0SBjoern A. Zeeb }
38203cee92e0SBjoern A. Zeeb 
38213cee92e0SBjoern A. Zeeb void
38223cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer)
38233cee92e0SBjoern A. Zeeb {
3824dbc42409SLawrence Stewart 	int mss;
38253ac12506SJonathan T. Looney 	uint32_t bufsize;
38269eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
38273cee92e0SBjoern A. Zeeb 	struct socket *so;
38283cee92e0SBjoern A. Zeeb 	struct hc_metrics_lite metrics;
38293c914c54SAndre Oppermann 	struct tcp_ifcap cap;
3830dbc42409SLawrence Stewart 
38313cee92e0SBjoern A. Zeeb 	KASSERT(tp != NULL, ("%s: tp == NULL", __func__));
38323cee92e0SBjoern A. Zeeb 
38333c914c54SAndre Oppermann 	bzero(&cap, sizeof(cap));
38343c914c54SAndre Oppermann 	tcp_mss_update(tp, offer, -1, &metrics, &cap);
38353cee92e0SBjoern A. Zeeb 
38363cee92e0SBjoern A. Zeeb 	mss = tp->t_maxseg;
383797d8d152SAndre Oppermann 
3838df8bae1dSRodney W. Grimes 	/*
383997d8d152SAndre Oppermann 	 * If there's a pipesize, change the socket buffer to that size,
384097d8d152SAndre Oppermann 	 * don't change if sb_hiwat is different than default (then it
384197d8d152SAndre Oppermann 	 * has been changed on purpose with setsockopt).
384297d8d152SAndre Oppermann 	 * Make the socket buffers an integral number of mss units;
384397d8d152SAndre Oppermann 	 * if the mss is larger than the socket buffer, decrease the mss.
3844df8bae1dSRodney W. Grimes 	 */
3845c3b02504SBjoern A. Zeeb 	so = inp->inp_socket;
38463f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
3847e233e2acSAndre Oppermann 	if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe)
384897d8d152SAndre Oppermann 		bufsize = metrics.rmx_sendpipe;
384997d8d152SAndre Oppermann 	else
3850df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
3851df8bae1dSRodney W. Grimes 	if (bufsize < mss)
3852df8bae1dSRodney W. Grimes 		mss = bufsize;
3853df8bae1dSRodney W. Grimes 	else {
3854df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3855df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3856df8bae1dSRodney W. Grimes 			bufsize = sb_max;
385788c39af3SRuslan Ermilov 		if (bufsize > so->so_snd.sb_hiwat)
385843283184SGleb Smirnoff 			(void)sbreserve_locked(so, SO_SND, bufsize, NULL);
3859df8bae1dSRodney W. Grimes 	}
38603f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
3861784ce8faSHiren Panchasara 	/*
3862784ce8faSHiren Panchasara 	 * Sanity check: make sure that maxseg will be large
3863784ce8faSHiren Panchasara 	 * enough to allow some data on segments even if the
3864784ce8faSHiren Panchasara 	 * all the option space is used (40bytes).  Otherwise
3865784ce8faSHiren Panchasara 	 * funny things may happen in tcp_output.
3866784ce8faSHiren Panchasara 	 *
3867784ce8faSHiren Panchasara 	 * XXXGL: shouldn't we reserve space for IP/IPv6 options?
3868784ce8faSHiren Panchasara 	 */
3869784ce8faSHiren Panchasara 	tp->t_maxseg = max(mss, 64);
3870df8bae1dSRodney W. Grimes 
38713f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
3872e233e2acSAndre Oppermann 	if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe)
387397d8d152SAndre Oppermann 		bufsize = metrics.rmx_recvpipe;
387497d8d152SAndre Oppermann 	else
3875df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
3876df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
3877df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3878df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3879df8bae1dSRodney W. Grimes 			bufsize = sb_max;
388088c39af3SRuslan Ermilov 		if (bufsize > so->so_rcv.sb_hiwat)
388143283184SGleb Smirnoff 			(void)sbreserve_locked(so, SO_RCV, bufsize, NULL);
3882df8bae1dSRodney W. Grimes 	}
38833f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
388491d6cfa6SBjoern A. Zeeb 
388591d6cfa6SBjoern A. Zeeb 	/* Check the interface for TSO capabilities. */
38863c914c54SAndre Oppermann 	if (cap.ifcap & CSUM_TSO) {
388791d6cfa6SBjoern A. Zeeb 		tp->t_flags |= TF_TSO;
38883c914c54SAndre Oppermann 		tp->t_tsomax = cap.tsomax;
38899fd573c3SHans Petter Selasky 		tp->t_tsomaxsegcount = cap.tsomaxsegcount;
38909fd573c3SHans Petter Selasky 		tp->t_tsomaxsegsize = cap.tsomaxsegsize;
38913c914c54SAndre Oppermann 	}
3892a0292f23SGarrett Wollman }
3893a0292f23SGarrett Wollman 
3894a0292f23SGarrett Wollman /*
3895a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
3896a0292f23SGarrett Wollman  */
3897a0292f23SGarrett Wollman int
3898ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc)
3899a0292f23SGarrett Wollman {
390097d8d152SAndre Oppermann 	int mss = 0;
39013ac12506SJonathan T. Looney 	uint32_t thcmtu = 0;
39023ac12506SJonathan T. Looney 	uint32_t maxmtu = 0;
390397d8d152SAndre Oppermann 	size_t min_protoh;
3904a0292f23SGarrett Wollman 
390597d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer"));
3906a0292f23SGarrett Wollman 
390731b3783cSHajimu UMEMOTO #ifdef INET6
3908dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
3909603724d3SBjoern A. Zeeb 		mss = V_tcp_v6mssdflt;
3910233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu6(inc, NULL);
391197d8d152SAndre Oppermann 		min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
3912b287c6c7SBjoern A. Zeeb 	}
391331b3783cSHajimu UMEMOTO #endif
3914b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3915b287c6c7SBjoern A. Zeeb 	else
3916b287c6c7SBjoern A. Zeeb #endif
3917b287c6c7SBjoern A. Zeeb #ifdef INET
391897d8d152SAndre Oppermann 	{
3919603724d3SBjoern A. Zeeb 		mss = V_tcp_mssdflt;
3920233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu(inc, NULL);
392197d8d152SAndre Oppermann 		min_protoh = sizeof(struct tcpiphdr);
392297d8d152SAndre Oppermann 	}
3923b287c6c7SBjoern A. Zeeb #endif
39243a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET)
39253a9391deSBjoern A. Zeeb 	thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
39263a9391deSBjoern A. Zeeb #endif
39273a9391deSBjoern A. Zeeb 
392897d8d152SAndre Oppermann 	if (maxmtu && thcmtu)
392997d8d152SAndre Oppermann 		mss = min(maxmtu, thcmtu) - min_protoh;
393097d8d152SAndre Oppermann 	else if (maxmtu || thcmtu)
393197d8d152SAndre Oppermann 		mss = max(maxmtu, thcmtu) - min_protoh;
393297d8d152SAndre Oppermann 
393397d8d152SAndre Oppermann 	return (mss);
3934df8bae1dSRodney W. Grimes }
393546f58482SJonathan Lemon 
39360e1d7c25SRichard Scheffenegger void
393749a6fbe3SRichard Scheffenegger tcp_do_prr_ack(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to, sackstatus_t sack_changed)
39380e1d7c25SRichard Scheffenegger {
393948396dc7SRichard Scheffenegger 	int snd_cnt = 0, limit = 0, del_data = 0, pipe = 0;
39400e1d7c25SRichard Scheffenegger 	int maxseg = tcp_maxseg(tp);
39410e1d7c25SRichard Scheffenegger 
39429eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
39430e1d7c25SRichard Scheffenegger 
39440e1d7c25SRichard Scheffenegger 	/*
39450e1d7c25SRichard Scheffenegger 	 * Compute the amount of data that this ACK is indicating
39460e1d7c25SRichard Scheffenegger 	 * (del_data) and an estimate of how many bytes are in the
39470e1d7c25SRichard Scheffenegger 	 * network.
39480e1d7c25SRichard Scheffenegger 	 */
3949c21b7b55SRichard Scheffenegger 	if (tcp_is_sack_recovery(tp, to) ||
395074d7fc87SRichard Scheffenegger 	    (IN_CONGRECOVERY(tp->t_flags) &&
395174d7fc87SRichard Scheffenegger 	     !IN_FASTRECOVERY(tp->t_flags))) {
395231d7a27cSRichard Scheffenegger 		del_data = tp->sackhint.delivered_data;
3953d1de2b05SRichard Scheffenegger 		if (V_tcp_do_newsack)
3954a8e431e1SRichard Scheffenegger 			pipe = tcp_compute_pipe(tp);
3955a8e431e1SRichard Scheffenegger 		else
395674d7fc87SRichard Scheffenegger 			pipe = (tp->snd_nxt - tp->snd_fack) +
395774d7fc87SRichard Scheffenegger 				tp->sackhint.sack_bytes_rexmit;
395874d7fc87SRichard Scheffenegger 	} else {
395974d7fc87SRichard Scheffenegger 		if (tp->sackhint.prr_delivered < (tcprexmtthresh * maxseg +
396074d7fc87SRichard Scheffenegger 					     tp->snd_recover - tp->snd_una))
396174d7fc87SRichard Scheffenegger 			del_data = maxseg;
396274d7fc87SRichard Scheffenegger 		pipe = imax(0, tp->snd_max - tp->snd_una -
396374d7fc87SRichard Scheffenegger 			    imin(INT_MAX / 65536, tp->t_dupacks) * maxseg);
396474d7fc87SRichard Scheffenegger 	}
39650e1d7c25SRichard Scheffenegger 	tp->sackhint.prr_delivered += del_data;
39660e1d7c25SRichard Scheffenegger 	/*
39670e1d7c25SRichard Scheffenegger 	 * Proportional Rate Reduction
39680e1d7c25SRichard Scheffenegger 	 */
3969e9071000SRichard Scheffenegger 	if (pipe >= tp->snd_ssthresh) {
39706a376af0SRichard Scheffenegger 		if (tp->sackhint.recover_fs == 0)
39716a376af0SRichard Scheffenegger 			tp->sackhint.recover_fs =
397248396dc7SRichard Scheffenegger 			    imax(1, tp->snd_nxt - tp->snd_una);
397348396dc7SRichard Scheffenegger 		snd_cnt = howmany((long)tp->sackhint.prr_delivered *
397448396dc7SRichard Scheffenegger 			    tp->snd_ssthresh, tp->sackhint.recover_fs) -
39759276ad23SRichard Scheffenegger 			    tp->sackhint.prr_out + maxseg - 1;
39766a376af0SRichard Scheffenegger 	} else {
397749a6fbe3SRichard Scheffenegger 		/*
397849a6fbe3SRichard Scheffenegger 		 * PRR 6937bis heuristic:
397949a6fbe3SRichard Scheffenegger 		 * - A partial ack without SACK block beneath snd_recover
398049a6fbe3SRichard Scheffenegger 		 * indicates further loss.
398149a6fbe3SRichard Scheffenegger 		 * - An SACK scoreboard update adding a new hole indicates
398249a6fbe3SRichard Scheffenegger 		 * further loss, so be conservative and send at most one
398349a6fbe3SRichard Scheffenegger 		 * segment.
398449a6fbe3SRichard Scheffenegger 		 * - Prevent ACK splitting attacks, by being conservative
398549a6fbe3SRichard Scheffenegger 		 * when no new data is acked.
398649a6fbe3SRichard Scheffenegger 		 */
398749a6fbe3SRichard Scheffenegger 		if ((sack_changed == SACK_NEWLOSS) || (del_data == 0))
398884761f3dSRichard Scheffenegger 			limit = tp->sackhint.prr_delivered -
3989e5313869SRichard Scheffenegger 				tp->sackhint.prr_out;
39900e1d7c25SRichard Scheffenegger 		else
399148396dc7SRichard Scheffenegger 			limit = imax(tp->sackhint.prr_delivered -
3992e5313869SRichard Scheffenegger 				    tp->sackhint.prr_out, del_data) +
3993e5313869SRichard Scheffenegger 				    maxseg;
399448396dc7SRichard Scheffenegger 		snd_cnt = imin((tp->snd_ssthresh - pipe), limit);
39950e1d7c25SRichard Scheffenegger 	}
399648396dc7SRichard Scheffenegger 	snd_cnt = imax(snd_cnt, 0) / maxseg;
39970e1d7c25SRichard Scheffenegger 	/*
39980e1d7c25SRichard Scheffenegger 	 * Send snd_cnt new data into the network in response to this ack.
39990e1d7c25SRichard Scheffenegger 	 * If there is going to be a SACK retransmission, adjust snd_cwnd
40000e1d7c25SRichard Scheffenegger 	 * accordingly.
40010e1d7c25SRichard Scheffenegger 	 */
4002b9f803b7SRichard Scheffenegger 	if (IN_FASTRECOVERY(tp->t_flags)) {
4003c21b7b55SRichard Scheffenegger 		if (tcp_is_sack_recovery(tp, to)) {
400474d7fc87SRichard Scheffenegger 			tp->snd_cwnd = tp->snd_nxt - tp->snd_recover +
400574d7fc87SRichard Scheffenegger 					    tp->sackhint.sack_bytes_rexmit +
400674d7fc87SRichard Scheffenegger 					    (snd_cnt * maxseg);
400774d7fc87SRichard Scheffenegger 		} else {
400874d7fc87SRichard Scheffenegger 			tp->snd_cwnd = (tp->snd_max - tp->snd_una) +
400974d7fc87SRichard Scheffenegger 					    (snd_cnt * maxseg);
401074d7fc87SRichard Scheffenegger 		}
4011b9f803b7SRichard Scheffenegger 	} else if (IN_CONGRECOVERY(tp->t_flags))
401274d7fc87SRichard Scheffenegger 		tp->snd_cwnd = pipe - del_data + (snd_cnt * maxseg);
401374d7fc87SRichard Scheffenegger 	tp->snd_cwnd = imax(maxseg, tp->snd_cwnd);
40140e1d7c25SRichard Scheffenegger }
40150e1d7c25SRichard Scheffenegger 
401646f58482SJonathan Lemon /*
4017c068736aSJeffrey Hsu  * On a partial ack arrives, force the retransmission of the
4018c068736aSJeffrey Hsu  * next unacknowledged segment.  Do not clear tp->t_dupacks.
4019c068736aSJeffrey Hsu  * By setting snd_nxt to ti_ack, this forces retransmission timer to
4020c068736aSJeffrey Hsu  * be started again.
402146f58482SJonathan Lemon  */
402255bceb1eSRandall Stewart void
4023ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th)
402446f58482SJonathan Lemon {
402546f58482SJonathan Lemon 	tcp_seq onxt = tp->snd_nxt;
40263ac12506SJonathan T. Looney 	uint32_t ocwnd = tp->snd_cwnd;
40270c39d38dSGleb Smirnoff 	u_int maxseg = tcp_maxseg(tp);
402846f58482SJonathan Lemon 
40299eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
40307a3244ccSRobert Watson 
4031b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_REXMT, 0);
403246f58482SJonathan Lemon 	tp->t_rtttime = 0;
403346f58482SJonathan Lemon 	tp->snd_nxt = th->th_ack;
40346b2a5f92SJayanth Vijayaraghavan 	/*
4035c068736aSJeffrey Hsu 	 * Set snd_cwnd to one segment beyond acknowledged offset.
4036c068736aSJeffrey Hsu 	 * (tp->snd_una has not yet been updated when this function is called.)
40376b2a5f92SJayanth Vijayaraghavan 	 */
40380c39d38dSGleb Smirnoff 	tp->snd_cwnd = maxseg + BYTES_THIS_ACK(tp, th);
4039a84db8f4SMatthew Dillon 	tp->t_flags |= TF_ACKNOW;
404040fa3e40SGleb Smirnoff 	(void) tcp_output(tp);
404146f58482SJonathan Lemon 	tp->snd_cwnd = ocwnd;
404246f58482SJonathan Lemon 	if (SEQ_GT(onxt, tp->snd_nxt))
404346f58482SJonathan Lemon 		tp->snd_nxt = onxt;
404446f58482SJonathan Lemon 	/*
404546f58482SJonathan Lemon 	 * Partial window deflation.  Relies on fact that tp->snd_una
404646f58482SJonathan Lemon 	 * not updated yet.
404746f58482SJonathan Lemon 	 */
4048dbc42409SLawrence Stewart 	if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th))
4049dbc42409SLawrence Stewart 		tp->snd_cwnd -= BYTES_THIS_ACK(tp, th);
4050d7587117SPaul Saab 	else
4051d7587117SPaul Saab 		tp->snd_cwnd = 0;
40520c39d38dSGleb Smirnoff 	tp->snd_cwnd += maxseg;
405346f58482SJonathan Lemon }
405412eeb81fSHiren Panchasara 
405512eeb81fSHiren Panchasara int
405612eeb81fSHiren Panchasara tcp_compute_pipe(struct tcpcb *tp)
405712eeb81fSHiren Panchasara {
4058e5049a17SRandall Stewart 	if (tp->t_fb->tfb_compute_pipe == NULL) {
405912eeb81fSHiren Panchasara 		return (tp->snd_max - tp->snd_una +
406012eeb81fSHiren Panchasara 			tp->sackhint.sack_bytes_rexmit -
4061e2c6a6d2SRichard Scheffenegger 			tp->sackhint.sacked_bytes -
4062e2c6a6d2SRichard Scheffenegger 			tp->sackhint.lost_bytes);
4063e5049a17SRandall Stewart 	} else {
4064e5049a17SRandall Stewart 		return((*tp->t_fb->tfb_compute_pipe)(tp));
4065e5049a17SRandall Stewart 	}
406612eeb81fSHiren Panchasara }
40677dc90a1dSMichael Tuexen 
40687dc90a1dSMichael Tuexen uint32_t
40697dc90a1dSMichael Tuexen tcp_compute_initwnd(uint32_t maxseg)
40707dc90a1dSMichael Tuexen {
40717dc90a1dSMichael Tuexen 	/*
40727dc90a1dSMichael Tuexen 	 * Calculate the Initial Window, also used as Restart Window
40737dc90a1dSMichael Tuexen 	 *
40747dc90a1dSMichael Tuexen 	 * RFC5681 Section 3.1 specifies the default conservative values.
40757dc90a1dSMichael Tuexen 	 * RFC3390 specifies slightly more aggressive values.
40767dc90a1dSMichael Tuexen 	 * RFC6928 increases it to ten segments.
40777dc90a1dSMichael Tuexen 	 * Support for user specified value for initial flight size.
40787dc90a1dSMichael Tuexen 	 */
40797dc90a1dSMichael Tuexen 	if (V_tcp_initcwnd_segments)
40807dc90a1dSMichael Tuexen 		return min(V_tcp_initcwnd_segments * maxseg,
40817dc90a1dSMichael Tuexen 		    max(2 * maxseg, V_tcp_initcwnd_segments * 1460));
40827dc90a1dSMichael Tuexen 	else if (V_tcp_do_rfc3390)
40837dc90a1dSMichael Tuexen 		return min(4 * maxseg, max(2 * maxseg, 4380));
40847dc90a1dSMichael Tuexen 	else {
40857dc90a1dSMichael Tuexen 		/* Per RFC5681 Section 3.1 */
40867dc90a1dSMichael Tuexen 		if (maxseg > 2190)
40877dc90a1dSMichael Tuexen 			return (2 * maxseg);
40887dc90a1dSMichael Tuexen 		else if (maxseg > 1095)
40897dc90a1dSMichael Tuexen 			return (3 * maxseg);
40907dc90a1dSMichael Tuexen 		else
40917dc90a1dSMichael Tuexen 			return (4 * maxseg);
40927dc90a1dSMichael Tuexen 	}
40937dc90a1dSMichael Tuexen }
4094