xref: /freebsd/sys/netinet/tcp_input.c (revision 2d05a1c81b2cbb5468a242d0add44f850aa31811)
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;
44230409ecdSRichard Scheffenegger 		if ((tp->t_rxtshift > 1) ||
44330409ecdSRichard Scheffenegger 		    !((tp->t_flags & TF_SACK_PERMIT) &&
44430409ecdSRichard Scheffenegger 		      (!TAILQ_EMPTY(&tp->snd_holes))))
445dbc42409SLawrence Stewart 			EXIT_RECOVERY(tp->t_flags);
4469cc711c9SMichael Tuexen 		if (tp->t_flags2 & TF2_ECN_PERMIT)
4479cc711c9SMichael Tuexen 			tp->t_flags2 |= TF2_ECN_SND_CWR;
448dbc42409SLawrence Stewart 		break;
449dbc42409SLawrence Stewart 	case CC_RTO_ERR:
450dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_sndrexmitbad);
451dbc42409SLawrence Stewart 		/* RTO was unnecessary, so reset everything. */
452dbc42409SLawrence Stewart 		tp->snd_cwnd = tp->snd_cwnd_prev;
453dbc42409SLawrence Stewart 		tp->snd_ssthresh = tp->snd_ssthresh_prev;
454dbc42409SLawrence Stewart 		tp->snd_recover = tp->snd_recover_prev;
455dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASFRECOVERY)
456dbc42409SLawrence Stewart 			ENTER_FASTRECOVERY(tp->t_flags);
457dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASCRECOVERY)
458dbc42409SLawrence Stewart 			ENTER_CONGRECOVERY(tp->t_flags);
459dbc42409SLawrence Stewart 		tp->snd_nxt = tp->snd_max;
460672dc4aeSJohn Baldwin 		tp->t_flags &= ~TF_PREVVALID;
461dbc42409SLawrence Stewart 		tp->t_badrxtwin = 0;
462dbc42409SLawrence Stewart 		break;
463dbc42409SLawrence Stewart 	}
464dbc42409SLawrence Stewart 
465dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->cong_signal != NULL) {
466dbc42409SLawrence Stewart 		if (th != NULL)
467e68b3792SGleb Smirnoff 			tp->t_ccv.curack = th->th_ack;
468e68b3792SGleb Smirnoff 		CC_ALGO(tp)->cong_signal(&tp->t_ccv, type);
469dbc42409SLawrence Stewart 	}
470dbc42409SLawrence Stewart }
471dbc42409SLawrence Stewart 
47255bceb1eSRandall Stewart void inline
473dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th)
474dbc42409SLawrence Stewart {
4759eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
476dbc42409SLawrence Stewart 
477dbc42409SLawrence Stewart 	/* XXXLAS: KASSERT that we're in recovery? */
478dbc42409SLawrence Stewart 
479dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->post_recovery != NULL) {
480e68b3792SGleb Smirnoff 		tp->t_ccv.curack = th->th_ack;
481e68b3792SGleb Smirnoff 		CC_ALGO(tp)->post_recovery(&tp->t_ccv);
482dbc42409SLawrence Stewart 	}
483dbc42409SLawrence Stewart 	/* XXXLAS: EXIT_RECOVERY ? */
484dbc42409SLawrence Stewart 	tp->t_bytes_acked = 0;
48548be5b97SRichard Scheffenegger 	tp->sackhint.delivered_data = 0;
486429f14f8SRichard Scheffenegger 	tp->sackhint.prr_delivered = 0;
487e5313869SRichard Scheffenegger 	tp->sackhint.prr_out = 0;
488dbc42409SLawrence Stewart }
4890312fbe9SPoul-Henning Kamp 
490df8bae1dSRodney W. Grimes /*
491262c1c1aSMatthew Dillon  * Indicate whether this ack should be delayed.  We can delay the ack if
49285536381SHiren Panchasara  * following conditions are met:
49385536381SHiren Panchasara  *	- There is no delayed ack timer in progress.
49485536381SHiren Panchasara  *	- Our last ack wasn't a 0-sized window. We never want to delay
49585536381SHiren Panchasara  *	  the ack that opens up a 0-sized window.
49685536381SHiren Panchasara  *	- LRO wasn't used for this segment. We make sure by checking that the
49785536381SHiren Panchasara  *	  segment size is not larger than the MSS.
498d8c85a26SJonathan Lemon  */
499c1e5a6e5SAndre Oppermann #define DELAY_ACK(tp, tlen)						\
500b8152ba7SAndre Oppermann 	((!tcp_timer_active(tp, TT_DELACK) &&				\
501a14c749fSJonathan Lemon 	    (tp->t_flags & TF_RXWIN0SENT) == 0) &&			\
5020c39d38dSGleb Smirnoff 	    (tlen <= tp->t_maxseg) &&					\
503603724d3SBjoern A. Zeeb 	    (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN)))
504d8c85a26SJonathan Lemon 
5054ad24737SRandall Stewart void inline
5065d8fd932SRandall Stewart cc_ecnpkt_handler_flags(struct tcpcb *tp, uint16_t flags, uint8_t iptos)
50764807b30SHiren Panchasara {
5089eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
50964807b30SHiren Panchasara 
51064807b30SHiren Panchasara 	if (CC_ALGO(tp)->ecnpkt_handler != NULL) {
51164807b30SHiren Panchasara 		switch (iptos & IPTOS_ECN_MASK) {
51264807b30SHiren Panchasara 		case IPTOS_ECN_CE:
513e68b3792SGleb Smirnoff 			tp->t_ccv.flags |= CCF_IPHDR_CE;
51464807b30SHiren Panchasara 			break;
51564807b30SHiren Panchasara 		case IPTOS_ECN_ECT0:
51683a2839fSMichael Tuexen 			/* FALLTHROUGH */
51764807b30SHiren Panchasara 		case IPTOS_ECN_ECT1:
51883a2839fSMichael Tuexen 			/* FALLTHROUGH */
51983a2839fSMichael Tuexen 		case IPTOS_ECN_NOTECT:
520e68b3792SGleb Smirnoff 			tp->t_ccv.flags &= ~CCF_IPHDR_CE;
52164807b30SHiren Panchasara 			break;
52264807b30SHiren Panchasara 		}
52364807b30SHiren Panchasara 
5245d8fd932SRandall Stewart 		if (flags & TH_CWR)
525e68b3792SGleb Smirnoff 			tp->t_ccv.flags |= CCF_TCPHDR_CWR;
52664807b30SHiren Panchasara 		else
527e68b3792SGleb Smirnoff 			tp->t_ccv.flags &= ~CCF_TCPHDR_CWR;
52864807b30SHiren Panchasara 
529e68b3792SGleb Smirnoff 		CC_ALGO(tp)->ecnpkt_handler(&tp->t_ccv);
53064807b30SHiren Panchasara 
531e68b3792SGleb Smirnoff 		if (tp->t_ccv.flags & CCF_ACKNOW) {
53264807b30SHiren Panchasara 			tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
533e11c9783SMichael Tuexen 			tp->t_flags |= TF_ACKNOW;
534e11c9783SMichael Tuexen 		}
53564807b30SHiren Panchasara 	}
53664807b30SHiren Panchasara }
53764807b30SHiren Panchasara 
5385d8fd932SRandall Stewart void inline
5395d8fd932SRandall Stewart cc_ecnpkt_handler(struct tcpcb *tp, struct tcphdr *th, uint8_t iptos)
5405d8fd932SRandall Stewart {
5411ebf4607SRichard Scheffenegger 	cc_ecnpkt_handler_flags(tp, tcp_get_flags(th), iptos);
5425d8fd932SRandall Stewart }
5435d8fd932SRandall Stewart 
544df8bae1dSRodney W. Grimes /*
5458d573cc1SAndre Oppermann  * TCP input handling is split into multiple parts:
5468d573cc1SAndre Oppermann  *   tcp6_input is a thin wrapper around tcp_input for the extended
5478d573cc1SAndre Oppermann  *	ip6_protox[] call format in ip6_input
5488d573cc1SAndre Oppermann  *   tcp_input handles primary segment validation, inpcb lookup and
5498d573cc1SAndre Oppermann  *	SYN processing on listen sockets
5508d573cc1SAndre Oppermann  *   tcp_do_segment processes the ACK and text of the segment for
5518d573cc1SAndre Oppermann  *	establishing, established and closing connections
552df8bae1dSRodney W. Grimes  */
553fb59c426SYoshinobu Inoue #ifdef INET6
554fb59c426SYoshinobu Inoue int
5559e644c23SMichael Tuexen tcp6_input_with_port(struct mbuf **mp, int *offp, int proto, uint16_t port)
556fb59c426SYoshinobu Inoue {
557503f4e47SBjoern A. Zeeb 	struct mbuf *m;
55833841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6;
5593e88eb90SAndrey V. Elsukov 	struct ip6_hdr *ip6;
560fb59c426SYoshinobu Inoue 
561503f4e47SBjoern A. Zeeb 	m = *mp;
562a4adf6ccSBjoern A. Zeeb 	if (m->m_len < *offp + sizeof(struct tcphdr)) {
5634e619b17SBjoern A. Zeeb 		m = m_pullup(m, *offp + sizeof(struct tcphdr));
5644e619b17SBjoern A. Zeeb 		if (m == NULL) {
5654e619b17SBjoern A. Zeeb 			*mp = m;
5664e619b17SBjoern A. Zeeb 			TCPSTAT_INC(tcps_rcvshort);
5674e619b17SBjoern A. Zeeb 			return (IPPROTO_DONE);
5684e619b17SBjoern A. Zeeb 		}
569a4adf6ccSBjoern A. Zeeb 	}
570fb59c426SYoshinobu Inoue 
571fb59c426SYoshinobu Inoue 	/*
572fb59c426SYoshinobu Inoue 	 * draft-itojun-ipv6-tcp-to-anycast
573fb59c426SYoshinobu Inoue 	 * better place to put this in?
574fb59c426SYoshinobu Inoue 	 */
5753e88eb90SAndrey V. Elsukov 	ip6 = mtod(m, struct ip6_hdr *);
5768268d82cSAlexander V. Chernikov 	ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false);
57733841545SHajimu UMEMOTO 	if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
578fb59c426SYoshinobu Inoue 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
579fb59c426SYoshinobu Inoue 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
580a8fe77d8SBjoern A. Zeeb 		*mp = NULL;
5818f5a8818SKevin Lo 		return (IPPROTO_DONE);
582fb59c426SYoshinobu Inoue 	}
583fb59c426SYoshinobu Inoue 
584a8fe77d8SBjoern A. Zeeb 	*mp = m;
5859e644c23SMichael Tuexen 	return (tcp_input_with_port(mp, offp, proto, port));
5869e644c23SMichael Tuexen }
5879e644c23SMichael Tuexen 
5889e644c23SMichael Tuexen int
5899e644c23SMichael Tuexen tcp6_input(struct mbuf **mp, int *offp, int proto)
5909e644c23SMichael Tuexen {
5919e644c23SMichael Tuexen 
5929e644c23SMichael Tuexen 	return(tcp6_input_with_port(mp, offp, proto, 0));
593fb59c426SYoshinobu Inoue }
594b287c6c7SBjoern A. Zeeb #endif /* INET6 */
595fb59c426SYoshinobu Inoue 
5968f5a8818SKevin Lo int
5979e644c23SMichael Tuexen tcp_input_with_port(struct mbuf **mp, int *offp, int proto, uint16_t port)
598df8bae1dSRodney W. Grimes {
5998f5a8818SKevin Lo 	struct mbuf *m = *mp;
600b287c6c7SBjoern A. Zeeb 	struct tcphdr *th = NULL;
601ad3f9ab3SAndre Oppermann 	struct ip *ip = NULL;
602ad3f9ab3SAndre Oppermann 	struct inpcb *inp = NULL;
603302ce8d6SAndre Oppermann 	struct tcpcb *tp = NULL;
604302ce8d6SAndre Oppermann 	struct socket *so = NULL;
605e79adb8eSGarrett Wollman 	u_char *optp = NULL;
6068f5a8818SKevin Lo 	int off0;
60726f9a767SRodney W. Grimes 	int optlen = 0;
608b287c6c7SBjoern A. Zeeb #ifdef INET
609b287c6c7SBjoern A. Zeeb 	int len;
610c4556b2fSAndrey V. Elsukov 	uint8_t ipttl;
611b287c6c7SBjoern A. Zeeb #endif
612b287c6c7SBjoern A. Zeeb 	int tlen = 0, off;
613fb59c426SYoshinobu Inoue 	int drop_hdrlen;
614ad3f9ab3SAndre Oppermann 	int thflags;
615302ce8d6SAndre Oppermann 	int rstreason = 0;	/* For badport_bandlim accounting purposes */
61608d9c920SGleb Smirnoff 	int lookupflag;
6171d7ee746SKurt Lidl 	uint8_t iptos;
618c1de64a4SAndrey V. Elsukov 	struct m_tag *fwd_tag = NULL;
619c068736aSJeffrey Hsu #ifdef INET6
620302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6 = NULL;
621c068736aSJeffrey Hsu 	int isipv6;
622c068736aSJeffrey Hsu #else
623a160e630SAndre Oppermann 	const void *ip6 = NULL;
624b287c6c7SBjoern A. Zeeb #endif /* INET6 */
625302ce8d6SAndre Oppermann 	struct tcpopt to;		/* options in this segment */
626a160e630SAndre Oppermann 	char *s = NULL;			/* address and port logging */
627c068736aSJeffrey Hsu 
628d40c0d47SGleb Smirnoff 	NET_EPOCH_ASSERT();
629d40c0d47SGleb Smirnoff 
630fb59c426SYoshinobu Inoue #ifdef INET6
631fb59c426SYoshinobu Inoue 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
632fb59c426SYoshinobu Inoue #endif
633a0292f23SGarrett Wollman 
6348f5a8818SKevin Lo 	off0 = *offp;
6358f5a8818SKevin Lo 	m = *mp;
6368f5a8818SKevin Lo 	*mp = NULL;
637302ce8d6SAndre Oppermann 	to.to_flags = 0;
63878b50714SRobert Watson 	TCPSTAT_INC(tcps_rcvtotal);
639fb59c426SYoshinobu Inoue 
640513f2e2eSGleb Smirnoff 	m->m_pkthdr.tcp_tun_port = port;
64104d3a452SHajimu UMEMOTO #ifdef INET6
642b287c6c7SBjoern A. Zeeb 	if (isipv6) {
643fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
64445747ba5SBjoern A. Zeeb 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
645fb59c426SYoshinobu Inoue 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
6469e644c23SMichael Tuexen 		if (port)
6479e644c23SMichael Tuexen 			goto skip6_csum;
648356ab07eSBjoern A. Zeeb 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) {
64945747ba5SBjoern A. Zeeb 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
65045747ba5SBjoern A. Zeeb 				th->th_sum = m->m_pkthdr.csum_data;
65145747ba5SBjoern A. Zeeb 			else
65245747ba5SBjoern A. Zeeb 				th->th_sum = in6_cksum_pseudo(ip6, tlen,
65345747ba5SBjoern A. Zeeb 				    IPPROTO_TCP, m->m_pkthdr.csum_data);
65445747ba5SBjoern A. Zeeb 			th->th_sum ^= 0xffff;
65545747ba5SBjoern A. Zeeb 		} else
65645747ba5SBjoern A. Zeeb 			th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen);
65745747ba5SBjoern A. Zeeb 		if (th->th_sum) {
65878b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
659fb59c426SYoshinobu Inoue 			goto drop;
660fb59c426SYoshinobu Inoue 		}
6619e644c23SMichael Tuexen 	skip6_csum:
66233841545SHajimu UMEMOTO 		/*
66333841545SHajimu UMEMOTO 		 * Be proactive about unspecified IPv6 address in source.
66433841545SHajimu UMEMOTO 		 * As we use all-zero to indicate unbounded/unconnected pcb,
66533841545SHajimu UMEMOTO 		 * unspecified IPv6 address can be used to confuse us.
66633841545SHajimu UMEMOTO 		 *
66733841545SHajimu UMEMOTO 		 * Note that packets with unspecified IPv6 destination is
66833841545SHajimu UMEMOTO 		 * already dropped in ip6_input.
66933841545SHajimu UMEMOTO 		 */
670713264f6SMark Johnston 		KASSERT(!IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst),
671713264f6SMark Johnston 		    ("%s: unspecified destination v6 address", __func__));
67233841545SHajimu UMEMOTO 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
67378e6c3aaSGleb Smirnoff 			IP6STAT_INC(ip6s_badscope); /* XXX */
67433841545SHajimu UMEMOTO 			goto drop;
67533841545SHajimu UMEMOTO 		}
676bb4a7d94SKristof Provost 		iptos = IPV6_TRAFFIC_CLASS(ip6);
677b287c6c7SBjoern A. Zeeb 	}
67804d3a452SHajimu UMEMOTO #endif
679b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
680b287c6c7SBjoern A. Zeeb 	else
681b287c6c7SBjoern A. Zeeb #endif
682b287c6c7SBjoern A. Zeeb #ifdef INET
683b287c6c7SBjoern A. Zeeb 	{
684df8bae1dSRodney W. Grimes 		/*
685df8bae1dSRodney W. Grimes 		 * Get IP and TCP header together in first mbuf.
686df8bae1dSRodney W. Grimes 		 * Note: IP leaves IP header in first mbuf.
687df8bae1dSRodney W. Grimes 		 */
688fb59c426SYoshinobu Inoue 		if (off0 > sizeof (struct ip)) {
689105bd211SGleb Smirnoff 			ip_stripoptions(m);
690fb59c426SYoshinobu Inoue 			off0 = sizeof(struct ip);
691fb59c426SYoshinobu Inoue 		}
692df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof (struct tcpiphdr)) {
6934dfdffe9SAndre Oppermann 			if ((m = m_pullup(m, sizeof (struct tcpiphdr)))
6944dfdffe9SAndre Oppermann 			    == NULL) {
69578b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvshort);
6968f5a8818SKevin Lo 				return (IPPROTO_DONE);
697df8bae1dSRodney W. Grimes 			}
698df8bae1dSRodney W. Grimes 		}
699fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
700db4f9cc7SJonathan Lemon 		th = (struct tcphdr *)((caddr_t)ip + off0);
7018ad458a4SGleb Smirnoff 		tlen = ntohs(ip->ip_len) - off0;
702df8bae1dSRodney W. Grimes 
7031d7ee746SKurt Lidl 		iptos = ip->ip_tos;
7049e644c23SMichael Tuexen 		if (port)
7059e644c23SMichael Tuexen 			goto skip_csum;
706db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
707db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
708db4f9cc7SJonathan Lemon 				th->th_sum = m->m_pkthdr.csum_data;
709db4f9cc7SJonathan Lemon 			else
710db4f9cc7SJonathan Lemon 				th->th_sum = in_pseudo(ip->ip_src.s_addr,
711c068736aSJeffrey Hsu 				    ip->ip_dst.s_addr,
7128f134647SGleb Smirnoff 				    htonl(m->m_pkthdr.csum_data + tlen +
713c068736aSJeffrey Hsu 				    IPPROTO_TCP));
714db4f9cc7SJonathan Lemon 			th->th_sum ^= 0xffff;
715db4f9cc7SJonathan Lemon 		} else {
7168f134647SGleb Smirnoff 			struct ipovly *ipov = (struct ipovly *)ip;
7178f134647SGleb Smirnoff 
718df8bae1dSRodney W. Grimes 			/*
719df8bae1dSRodney W. Grimes 			 * Checksum extended TCP header and data.
720df8bae1dSRodney W. Grimes 			 */
7218f134647SGleb Smirnoff 			len = off0 + tlen;
722c4556b2fSAndrey V. Elsukov 			ipttl = ip->ip_ttl;
723fb59c426SYoshinobu Inoue 			bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
7248f134647SGleb Smirnoff 			ipov->ih_len = htons(tlen);
725fb59c426SYoshinobu Inoue 			th->th_sum = in_cksum(m, len);
72657f60867SMark Johnston 			/* Reset length for SDT probes. */
7271d7ee746SKurt Lidl 			ip->ip_len = htons(len);
7281d7ee746SKurt Lidl 			/* Reset TOS bits */
7291d7ee746SKurt Lidl 			ip->ip_tos = iptos;
7301d7ee746SKurt Lidl 			/* Re-initialization for later version check */
731c4556b2fSAndrey V. Elsukov 			ip->ip_ttl = ipttl;
7321d7ee746SKurt Lidl 			ip->ip_v = IPVERSION;
733b2fe54bcSAndrey V. Elsukov 			ip->ip_hl = off0 >> 2;
734db4f9cc7SJonathan Lemon 		}
7359e644c23SMichael Tuexen 	skip_csum:
7369e644c23SMichael Tuexen 		if (th->th_sum && (port == 0)) {
73778b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
738df8bae1dSRodney W. Grimes 			goto drop;
739df8bae1dSRodney W. Grimes 		}
740713264f6SMark Johnston 		KASSERT(ip->ip_dst.s_addr != INADDR_ANY,
741713264f6SMark Johnston 		    ("%s: unspecified destination v4 address", __func__));
742713264f6SMark Johnston 		if (__predict_false(ip->ip_src.s_addr == INADDR_ANY)) {
74378e6c3aaSGleb Smirnoff 			IPSTAT_INC(ips_badaddr);
744713264f6SMark Johnston 			goto drop;
745713264f6SMark Johnston 		}
746fb59c426SYoshinobu Inoue 	}
747b287c6c7SBjoern A. Zeeb #endif /* INET */
748df8bae1dSRodney W. Grimes 
749df8bae1dSRodney W. Grimes 	/*
750df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
751df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
752df8bae1dSRodney W. Grimes 	 */
753fb59c426SYoshinobu Inoue 	off = th->th_off << 2;
754df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
75578b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvbadoff);
756df8bae1dSRodney W. Grimes 		goto drop;
757df8bae1dSRodney W. Grimes 	}
758fb59c426SYoshinobu Inoue 	tlen -= off;	/* tlen is used instead of ti->ti_len */
759df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
76004d3a452SHajimu UMEMOTO #ifdef INET6
761b287c6c7SBjoern A. Zeeb 		if (isipv6) {
762a4adf6ccSBjoern A. Zeeb 			if (m->m_len < off0 + off) {
7634e619b17SBjoern A. Zeeb 				m = m_pullup(m, off0 + off);
7644e619b17SBjoern A. Zeeb 				if (m == NULL) {
7654e619b17SBjoern A. Zeeb 					TCPSTAT_INC(tcps_rcvshort);
7664e619b17SBjoern A. Zeeb 					return (IPPROTO_DONE);
7674e619b17SBjoern A. Zeeb 				}
768a4adf6ccSBjoern A. Zeeb 			}
769fb59c426SYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
770fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
771b287c6c7SBjoern A. Zeeb 		}
77204d3a452SHajimu UMEMOTO #endif
773b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
774b287c6c7SBjoern A. Zeeb 		else
775b287c6c7SBjoern A. Zeeb #endif
776b287c6c7SBjoern A. Zeeb #ifdef INET
777b287c6c7SBjoern A. Zeeb 		{
778df8bae1dSRodney W. Grimes 			if (m->m_len < sizeof(struct ip) + off) {
779c068736aSJeffrey Hsu 				if ((m = m_pullup(m, sizeof (struct ip) + off))
7804dfdffe9SAndre Oppermann 				    == NULL) {
78178b50714SRobert Watson 					TCPSTAT_INC(tcps_rcvshort);
7828f5a8818SKevin Lo 					return (IPPROTO_DONE);
783df8bae1dSRodney W. Grimes 				}
784fb59c426SYoshinobu Inoue 				ip = mtod(m, struct ip *);
785fb59c426SYoshinobu Inoue 				th = (struct tcphdr *)((caddr_t)ip + off0);
786fb59c426SYoshinobu Inoue 			}
787df8bae1dSRodney W. Grimes 		}
788b287c6c7SBjoern A. Zeeb #endif
789df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
790fb59c426SYoshinobu Inoue 		optp = (u_char *)(th + 1);
791df8bae1dSRodney W. Grimes 	}
7921ebf4607SRichard Scheffenegger 	thflags = tcp_get_flags(th);
793df8bae1dSRodney W. Grimes 
794e46cd3d4SDag-Erling Smørgrav 	/*
795df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
796df8bae1dSRodney W. Grimes 	 */
7972903309aSAttilio Rao 	tcp_fields_to_host(th);
798df8bae1dSRodney W. Grimes 
799df8bae1dSRodney W. Grimes 	/*
800794235b7SAndre Oppermann 	 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options.
801755c1f07SAndras Olah 	 */
802fb59c426SYoshinobu Inoue 	drop_hdrlen = off0 + off;
803755c1f07SAndras Olah 
804755c1f07SAndras Olah 	/*
8058d573cc1SAndre Oppermann 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
8068d573cc1SAndre Oppermann 	 */
807b8056faeSGleb Smirnoff         if (
808b8056faeSGleb Smirnoff #ifdef INET6
809b8056faeSGleb Smirnoff 	    (isipv6 && (m->m_flags & M_IP6_NEXTHOP))
810b8056faeSGleb Smirnoff #ifdef INET
811b8056faeSGleb Smirnoff 	    || (!isipv6 && (m->m_flags & M_IP_NEXTHOP))
812b8056faeSGleb Smirnoff #endif
813b8056faeSGleb Smirnoff #endif
814b8056faeSGleb Smirnoff #if defined(INET) && !defined(INET6)
815b8056faeSGleb Smirnoff 	    (m->m_flags & M_IP_NEXTHOP)
816b8056faeSGleb Smirnoff #endif
817b8056faeSGleb Smirnoff 	    )
8189b932e9eSAndre Oppermann 		fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
8199b932e9eSAndre Oppermann 
82008d9c920SGleb Smirnoff 	/*
8211db08fbeSGleb Smirnoff 	 * For initial SYN packets we don't need write lock on matching
8221db08fbeSGleb Smirnoff 	 * PCB, be it a listening one or a synchronized one.  The packet
8231db08fbeSGleb Smirnoff 	 * shall not modify its state.
82408d9c920SGleb Smirnoff 	 */
825d88eb465SGleb Smirnoff 	lookupflag = INPLOOKUP_WILDCARD |
826d88eb465SGleb Smirnoff 	    ((thflags & (TH_ACK|TH_SYN)) == TH_SYN ?
827d88eb465SGleb Smirnoff 	    INPLOOKUP_RLOCKPCB : INPLOOKUP_WLOCKPCB);
828ce7ad664SEd Maste findpcb:
8298a006adbSBjoern A. Zeeb #ifdef INET6
8308a006adbSBjoern A. Zeeb 	if (isipv6 && fwd_tag != NULL) {
8318a006adbSBjoern A. Zeeb 		struct sockaddr_in6 *next_hop6;
8328a006adbSBjoern A. Zeeb 
8338a006adbSBjoern A. Zeeb 		next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
8348a006adbSBjoern A. Zeeb 		/*
8358a006adbSBjoern A. Zeeb 		 * Transparently forwarded. Pretend to be the destination.
8368a006adbSBjoern A. Zeeb 		 * Already got one like this?
8378a006adbSBjoern A. Zeeb 		 */
8388a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo,
8398a006adbSBjoern A. Zeeb 		    &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport,
840aab8c844SGleb Smirnoff 		    lookupflag & ~INPLOOKUP_WILDCARD, m->m_pkthdr.rcvif, m);
8418a006adbSBjoern A. Zeeb 		if (!inp) {
8428a006adbSBjoern A. Zeeb 			/*
8438a006adbSBjoern A. Zeeb 			 * It's new.  Try to find the ambushing socket.
8448a006adbSBjoern A. Zeeb 			 * Because we've rewritten the destination address,
8458a006adbSBjoern A. Zeeb 			 * any hardware-generated hash is ignored.
8468a006adbSBjoern A. Zeeb 			 */
8478a006adbSBjoern A. Zeeb 			inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src,
8488a006adbSBjoern A. Zeeb 			    th->th_sport, &next_hop6->sin6_addr,
8498a006adbSBjoern A. Zeeb 			    next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) :
850d88eb465SGleb Smirnoff 			    th->th_dport, lookupflag, m->m_pkthdr.rcvif);
8518a006adbSBjoern A. Zeeb 		}
852c1de64a4SAndrey V. Elsukov 	} else if (isipv6) {
8538a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src,
854d88eb465SGleb Smirnoff 		    th->th_sport, &ip6->ip6_dst, th->th_dport, lookupflag,
855d88eb465SGleb Smirnoff 		    m->m_pkthdr.rcvif, m);
8568a006adbSBjoern A. Zeeb 	}
8578a006adbSBjoern A. Zeeb #endif /* INET6 */
8588a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET)
8598a006adbSBjoern A. Zeeb 	else
8608a006adbSBjoern A. Zeeb #endif
8618a006adbSBjoern A. Zeeb #ifdef INET
8628a006adbSBjoern A. Zeeb 	if (fwd_tag != NULL) {
8639b932e9eSAndre Oppermann 		struct sockaddr_in *next_hop;
8649b932e9eSAndre Oppermann 
8659b932e9eSAndre Oppermann 		next_hop = (struct sockaddr_in *)(fwd_tag+1);
866f9e354dfSJulian Elischer 		/*
8672b25acc1SLuigi Rizzo 		 * Transparently forwarded. Pretend to be the destination.
868f9e354dfSJulian Elischer 		 * already got one like this?
869f9e354dfSJulian Elischer 		 */
870d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport,
871aab8c844SGleb Smirnoff 		    ip->ip_dst, th->th_dport, lookupflag & ~INPLOOKUP_WILDCARD,
872aab8c844SGleb Smirnoff 		    m->m_pkthdr.rcvif, m);
873fa046d87SRobert Watson 		if (!inp) {
874fa046d87SRobert Watson 			/*
875fa046d87SRobert Watson 			 * It's new.  Try to find the ambushing socket.
876d3c1f003SRobert Watson 			 * Because we've rewritten the destination address,
877d3c1f003SRobert Watson 			 * any hardware-generated hash is ignored.
878fa046d87SRobert Watson 			 */
879fa046d87SRobert Watson 			inp = in_pcblookup(&V_tcbinfo, ip->ip_src,
880fa046d87SRobert Watson 			    th->th_sport, next_hop->sin_addr,
881fa046d87SRobert Watson 			    next_hop->sin_port ? ntohs(next_hop->sin_port) :
882d88eb465SGleb Smirnoff 			    th->th_dport, lookupflag, m->m_pkthdr.rcvif);
883f9e354dfSJulian Elischer 		}
884b10fbdeaSAndre Oppermann 	} else
885d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src,
886d88eb465SGleb Smirnoff 		    th->th_sport, ip->ip_dst, th->th_dport, lookupflag,
887d88eb465SGleb Smirnoff 		    m->m_pkthdr.rcvif, m);
8888a006adbSBjoern A. Zeeb #endif /* INET */
889f9e354dfSJulian Elischer 
890df8bae1dSRodney W. Grimes 	/*
891574b6964SAndre Oppermann 	 * If the INPCB does not exist then all data in the incoming
892574b6964SAndre Oppermann 	 * segment is discarded and an appropriate RST is sent back.
8938b07e49aSJulian Elischer 	 * XXX MRT Send RST using which routing table?
894df8bae1dSRodney W. Grimes 	 */
895816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
896d88eb465SGleb Smirnoff 		if (rstreason != 0) {
897d88eb465SGleb Smirnoff 			/* We came here after second (safety) lookup. */
898d88eb465SGleb Smirnoff 			MPASS((lookupflag & INPLOOKUP_WILDCARD) == 0);
899d88eb465SGleb Smirnoff 			goto dropwithreset;
900d88eb465SGleb Smirnoff 		}
901574b6964SAndre Oppermann 		/*
902574b6964SAndre Oppermann 		 * Log communication attempts to ports that are not
903574b6964SAndre Oppermann 		 * in use.
904574b6964SAndre Oppermann 		 */
905334fc582SBjoern A. Zeeb 		if ((V_tcp_log_in_vain == 1 && (thflags & TH_SYN)) ||
906334fc582SBjoern A. Zeeb 		    V_tcp_log_in_vain == 2) {
907b7d747ecSAndre Oppermann 			if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6)))
908df541e5fSAndre Oppermann 				log(LOG_INFO, "%s; %s: Connection attempt "
909df541e5fSAndre Oppermann 				    "to closed port\n", s, __func__);
9102e4e1b4cSGeoff Rehmet 		}
911574b6964SAndre Oppermann 		/*
912574b6964SAndre Oppermann 		 * When blackholing do not respond with a RST but
913574b6964SAndre Oppermann 		 * completely ignore the segment and drop it.
914574b6964SAndre Oppermann 		 */
9153ea9a7cfSGleb Smirnoff 		if (((V_blackhole == 1 && (thflags & TH_SYN)) ||
916ad3ad064SGleb Smirnoff 		    V_blackhole == 2) && (V_blackhole_local || (
9173ea9a7cfSGleb Smirnoff #ifdef INET6
9183ea9a7cfSGleb Smirnoff 		    isipv6 ? !in6_localaddr(&ip6->ip6_src) :
9193ea9a7cfSGleb Smirnoff #endif
9203ea9a7cfSGleb Smirnoff #ifdef INET
9213ea9a7cfSGleb Smirnoff 		    !in_localip(ip->ip_src)
9223ea9a7cfSGleb Smirnoff #else
9233ea9a7cfSGleb Smirnoff 		    true
9243ea9a7cfSGleb Smirnoff #endif
925ad3ad064SGleb Smirnoff 		    )))
9261929eae1SAndre Oppermann 			goto dropunlock;
927574b6964SAndre Oppermann 
928a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
929a57815efSBosko Milekic 		goto dropwithreset;
930816a3d83SPoul-Henning Kamp 	}
93108d9c920SGleb Smirnoff 	INP_LOCK_ASSERT(inp);
932f567d55fSGleb Smirnoff 
933c2529042SHans Petter Selasky 	if ((inp->inp_flowtype == M_HASHTYPE_NONE) &&
934f71cb9f7SGleb Smirnoff 	    !SOLISTENING(inp->inp_socket)) {
935e5738ee0SCheng Cui 		if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
93680cb9f21SKip Macy 			inp->inp_flowid = m->m_pkthdr.flowid;
9372f719932SAdrian Chadd 			inp->inp_flowtype = M_HASHTYPE_GET(m);
938e5738ee0SCheng Cui #ifdef	RSS
939e5738ee0SCheng Cui 		} else {
940e5738ee0SCheng Cui 			  /* assign flowid by software RSS hash */
941e5738ee0SCheng Cui #ifdef INET6
942e5738ee0SCheng Cui 			  if (isipv6) {
943e5738ee0SCheng Cui 				rss_proto_software_hash_v6(&inp->in6p_faddr,
944e5738ee0SCheng Cui 							   &inp->in6p_laddr,
945e5738ee0SCheng Cui 							   inp->inp_fport,
946e5738ee0SCheng Cui 							   inp->inp_lport,
947e5738ee0SCheng Cui 							   IPPROTO_TCP,
948e5738ee0SCheng Cui 							   &inp->inp_flowid,
949e5738ee0SCheng Cui 							   &inp->inp_flowtype);
950e5738ee0SCheng Cui 			  } else
951e5738ee0SCheng Cui #endif	/* INET6 */
952e5738ee0SCheng Cui 			  {
953e5738ee0SCheng Cui 				rss_proto_software_hash_v4(inp->inp_faddr,
954e5738ee0SCheng Cui 							   inp->inp_laddr,
955e5738ee0SCheng Cui 							   inp->inp_fport,
956e5738ee0SCheng Cui 							   inp->inp_lport,
957e5738ee0SCheng Cui 							   IPPROTO_TCP,
958e5738ee0SCheng Cui 							   &inp->inp_flowid,
959e5738ee0SCheng Cui 							   &inp->inp_flowtype);
960e5738ee0SCheng Cui 			  }
961e5738ee0SCheng Cui #endif	/* RSS */
962e5738ee0SCheng Cui 		}
96380cb9f21SKip Macy 	}
964fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
965a2589465SKen Smith #ifdef INET6
966fcf59617SAndrey V. Elsukov 	if (isipv6 && IPSEC_ENABLED(ipv6) &&
967fcf59617SAndrey V. Elsukov 	    IPSEC_CHECK_POLICY(ipv6, m, inp) != 0) {
968a2589465SKen Smith 		goto dropunlock;
969a2589465SKen Smith 	}
970fcf59617SAndrey V. Elsukov #ifdef INET
971fcf59617SAndrey V. Elsukov 	else
972fcf59617SAndrey V. Elsukov #endif
973fcf59617SAndrey V. Elsukov #endif /* INET6 */
974fcf59617SAndrey V. Elsukov #ifdef INET
975fcf59617SAndrey V. Elsukov 	if (IPSEC_ENABLED(ipv4) &&
976fcf59617SAndrey V. Elsukov 	    IPSEC_CHECK_POLICY(ipv4, m, inp) != 0) {
977fcf59617SAndrey V. Elsukov 		goto dropunlock;
978fcf59617SAndrey V. Elsukov 	}
979fcf59617SAndrey V. Elsukov #endif /* INET */
980a2589465SKen Smith #endif /* IPSEC */
981a2589465SKen Smith 
9828d573cc1SAndre Oppermann 	/*
9838d573cc1SAndre Oppermann 	 * Check the minimum TTL for socket.
9848d573cc1SAndre Oppermann 	 */
98534f83c52SGeorge V. Neville-Neil 	if (inp->inp_ip_minttl != 0) {
98634f83c52SGeorge V. Neville-Neil #ifdef INET6
9872d8868dbSJonathan T. Looney 		if (isipv6) {
9882d8868dbSJonathan T. Looney 			if (inp->inp_ip_minttl > ip6->ip6_hlim)
989302ce8d6SAndre Oppermann 				goto dropunlock;
9902d8868dbSJonathan T. Looney 		} else
99134f83c52SGeorge V. Neville-Neil #endif
99234f83c52SGeorge V. Neville-Neil 		if (inp->inp_ip_minttl > ip->ip_ttl)
993302ce8d6SAndre Oppermann 			goto dropunlock;
99434f83c52SGeorge V. Neville-Neil 	}
995936cd18dSAndre Oppermann 
9960d744519SGleb Smirnoff 	tp = intotcpcb(inp);
9970d744519SGleb Smirnoff 	switch (tp->t_state) {
9980d744519SGleb Smirnoff 	case TCPS_TIME_WAIT:
999340c35deSJonathan Lemon 		/*
10000d744519SGleb Smirnoff 		 * A previous connection in TIMEWAIT state is supposed to catch
10010d744519SGleb Smirnoff 		 * stray or duplicate segments arriving late.  If this segment
10020d744519SGleb Smirnoff 		 * was a legitimate new connection attempt, the old INPCB gets
10030d744519SGleb Smirnoff 		 * removed and we can try again to find a listening socket.
1004340c35deSJonathan Lemon 		 */
1005283c76c7SMichael Tuexen 		tcp_dooptions(&to, optp, optlen,
1006283c76c7SMichael Tuexen 		    (thflags & TH_SYN) ? TO_SYN : 0);
10078d573cc1SAndre Oppermann 		/*
10080d744519SGleb Smirnoff 		 * tcp_twcheck unlocks the inp always, and frees the m if fails.
10098d573cc1SAndre Oppermann 		 */
10102104448fSAndre Oppermann 		if (tcp_twcheck(inp, &to, th, m, tlen))
1011340c35deSJonathan Lemon 			goto findpcb;
10128f5a8818SKevin Lo 		return (IPPROTO_DONE);
10130d744519SGleb Smirnoff 	case TCPS_CLOSED:
1014794235b7SAndre Oppermann 		/*
1015794235b7SAndre Oppermann 		 * The TCPCB may no longer exist if the connection is winding
1016794235b7SAndre Oppermann 		 * down or it is in the CLOSED state.  Either way we drop the
1017794235b7SAndre Oppermann 		 * segment and send an appropriate response.
1018794235b7SAndre Oppermann 		 */
1019a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
1020a57815efSBosko Milekic 		goto dropwithreset;
102109f81a46SBosko Milekic 	}
1022df8bae1dSRodney W. Grimes 
10239e644c23SMichael Tuexen 	if ((tp->t_port != port) && (tp->t_state > TCPS_LISTEN)) {
10249e644c23SMichael Tuexen 		rstreason = BANDLIM_RST_CLOSEDPORT;
10259e644c23SMichael Tuexen 		goto dropwithreset;
10269e644c23SMichael Tuexen 	}
10279e644c23SMichael Tuexen 
102809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
102909fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
103009fe6320SNavdeep Parhar 		tcp_offload_input(tp, m);
103109fe6320SNavdeep Parhar 		m = NULL;	/* consumed by the TOE driver */
103209fe6320SNavdeep Parhar 		goto dropunlock;
103309fe6320SNavdeep Parhar 	}
103409fe6320SNavdeep Parhar #endif
103509fe6320SNavdeep Parhar 
1036c488362eSRobert Watson #ifdef MAC
103730d239bcSRobert Watson 	if (mac_inpcb_check_deliver(inp, m))
1038302ce8d6SAndre Oppermann 		goto dropunlock;
1039c488362eSRobert Watson #endif
1040a557af22SRobert Watson 	so = inp->inp_socket;
1041302ce8d6SAndre Oppermann 	KASSERT(so != NULL, ("%s: so == NULL", __func__));
1042db33b3e6SAndre Oppermann 	/*
1043db33b3e6SAndre Oppermann 	 * When the socket is accepting connections (the INPCB is in LISTEN
1044db33b3e6SAndre Oppermann 	 * state) we look into the SYN cache if this is a new connection
1045a7c7f2a7SJohn Baldwin 	 * attempt or the completion of a previous one.
1046db33b3e6SAndre Oppermann 	 */
1047f4bb1869SMark Johnston 	KASSERT(tp->t_state == TCPS_LISTEN || !SOLISTENING(so),
104845274760SJonathan T. Looney 	    ("%s: so accepting but tp %p not listening", __func__, tp));
1049f4bb1869SMark Johnston 	if (tp->t_state == TCPS_LISTEN && SOLISTENING(so)) {
1050540e8b7eSJeffrey Hsu 		struct in_conninfo inc;
1051540e8b7eSJeffrey Hsu 
10528bfb1918SAndre Oppermann 		bzero(&inc, sizeof(inc));
1053302ce8d6SAndre Oppermann #ifdef INET6
1054be2ac88cSJonathan Lemon 		if (isipv6) {
1055dcdb4371SBjoern A. Zeeb 			inc.inc_flags |= INC_ISIPV6;
10565dff1c38SMichael Tuexen 			if (inp->inp_inc.inc_flags & INC_IPV6MINMTU)
10575dff1c38SMichael Tuexen 				inc.inc_flags |= INC_IPV6MINMTU;
1058be2ac88cSJonathan Lemon 			inc.inc6_faddr = ip6->ip6_src;
1059be2ac88cSJonathan Lemon 			inc.inc6_laddr = ip6->ip6_dst;
1060302ce8d6SAndre Oppermann 		} else
1061302ce8d6SAndre Oppermann #endif
1062302ce8d6SAndre Oppermann 		{
1063be2ac88cSJonathan Lemon 			inc.inc_faddr = ip->ip_src;
1064be2ac88cSJonathan Lemon 			inc.inc_laddr = ip->ip_dst;
1065be2ac88cSJonathan Lemon 		}
1066be2ac88cSJonathan Lemon 		inc.inc_fport = th->th_sport;
1067be2ac88cSJonathan Lemon 		inc.inc_lport = th->th_dport;
10687973fba3SJulian Elischer 		inc.inc_fibnum = so->so_fibnum;
1069be2ac88cSJonathan Lemon 
1070fb59c426SYoshinobu Inoue 		/*
10718d573cc1SAndre Oppermann 		 * Check for an existing connection attempt in syncache if
10728d573cc1SAndre Oppermann 		 * the flag is only ACK.  A successful lookup creates a new
10738d573cc1SAndre Oppermann 		 * socket appended to the listen queue in SYN_RECEIVED state.
1074fb59c426SYoshinobu Inoue 		 */
1075be2ac88cSJonathan Lemon 		if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
1076bf6d304aSAndre Oppermann 			/*
1077bf6d304aSAndre Oppermann 			 * Parse the TCP options here because
1078bf6d304aSAndre Oppermann 			 * syncookies need access to the reflected
1079bf6d304aSAndre Oppermann 			 * timestamp.
1080bf6d304aSAndre Oppermann 			 */
1081bf6d304aSAndre Oppermann 			tcp_dooptions(&to, optp, optlen, 0);
10829fa198beSAndre Oppermann 			/*
1083e7231d07SGleb Smirnoff 			 * NB: syncache_expand() doesn't unlock inp.
10849fa198beSAndre Oppermann 			 */
10859e644c23SMichael Tuexen 			rstreason = syncache_expand(&inc, &to, th, &so, m, port);
1086fcf59617SAndrey V. Elsukov 			if (rstreason < 0) {
1087fcf59617SAndrey V. Elsukov 				/*
1088fcf59617SAndrey V. Elsukov 				 * A failing TCP MD5 signature comparison
1089fcf59617SAndrey V. Elsukov 				 * must result in the segment being dropped
1090fcf59617SAndrey V. Elsukov 				 * and must not produce any response back
1091fcf59617SAndrey V. Elsukov 				 * to the sender.
1092fcf59617SAndrey V. Elsukov 				 */
1093fcf59617SAndrey V. Elsukov 				goto dropunlock;
1094fcf59617SAndrey V. Elsukov 			} else if (rstreason == 0) {
10954195b4afSPaul Traina 				/*
1096d88eb465SGleb Smirnoff 				 * No syncache entry, or ACK was not for our
1097d88eb465SGleb Smirnoff 				 * SYN/ACK.  Do our protection against double
1098d88eb465SGleb Smirnoff 				 * ACK.  If peer sent us 2 ACKs, then for the
1099d88eb465SGleb Smirnoff 				 * first one syncache_expand() successfully
1100d88eb465SGleb Smirnoff 				 * converted syncache entry into a socket,
1101d88eb465SGleb Smirnoff 				 * while we were waiting on the inpcb lock.  We
1102d88eb465SGleb Smirnoff 				 * don't want to sent RST for the second ACK,
1103d88eb465SGleb Smirnoff 				 * so we perform second lookup without wildcard
1104d88eb465SGleb Smirnoff 				 * match, hoping to find the new socket.  If
1105d88eb465SGleb Smirnoff 				 * the ACK is stray indeed, rstreason would
1106d88eb465SGleb Smirnoff 				 * hint the above code that the lookup was a
1107d88eb465SGleb Smirnoff 				 * second attempt.
1108d88eb465SGleb Smirnoff 				 *
11098d573cc1SAndre Oppermann 				 * NB: syncache did its own logging
11108d573cc1SAndre Oppermann 				 * of the failure cause.
11114195b4afSPaul Traina 				 */
1112d88eb465SGleb Smirnoff 				INP_WUNLOCK(inp);
1113be2ac88cSJonathan Lemon 				rstreason = BANDLIM_RST_OPENPORT;
1114d88eb465SGleb Smirnoff 				lookupflag &= ~INPLOOKUP_WILDCARD;
1115d88eb465SGleb Smirnoff 				goto findpcb;
1116be2ac88cSJonathan Lemon 			}
111709c305ebSPatrick Kelsey tfo_socket_result:
1118f76fcf6dSJeffrey Hsu 			if (so == NULL) {
1119be2ac88cSJonathan Lemon 				/*
1120e207f800SAndre Oppermann 				 * We completed the 3-way handshake
1121e207f800SAndre Oppermann 				 * but could not allocate a socket
1122e207f800SAndre Oppermann 				 * either due to memory shortage,
1123e207f800SAndre Oppermann 				 * listen queue length limits or
1124a160e630SAndre Oppermann 				 * global socket limits.  Send RST
1125a160e630SAndre Oppermann 				 * or wait and have the remote end
1126a160e630SAndre Oppermann 				 * retransmit the ACK for another
1127a160e630SAndre Oppermann 				 * try.
1128be2ac88cSJonathan Lemon 				 */
1129a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1130a160e630SAndre Oppermann 					log(LOG_DEBUG, "%s; %s: Listen socket: "
11318d573cc1SAndre Oppermann 					    "Socket allocation failed due to "
11328d573cc1SAndre Oppermann 					    "limits or memory shortage, %s\n",
1133ac957cd2SJulian Elischer 					    s, __func__,
1134ac957cd2SJulian Elischer 					    V_tcp_sc_rst_sock_fail ?
1135ac957cd2SJulian Elischer 					    "sending RST" : "try again");
1136603724d3SBjoern A. Zeeb 				if (V_tcp_sc_rst_sock_fail) {
1137e207f800SAndre Oppermann 					rstreason = BANDLIM_UNLIMITED;
1138e207f800SAndre Oppermann 					goto dropwithreset;
1139a160e630SAndre Oppermann 				} else
1140a160e630SAndre Oppermann 					goto dropunlock;
1141f76fcf6dSJeffrey Hsu 			}
1142be2ac88cSJonathan Lemon 			/*
1143be2ac88cSJonathan Lemon 			 * Socket is created in state SYN_RECEIVED.
11448d573cc1SAndre Oppermann 			 * Unlock the listen socket, lock the newly
11458d573cc1SAndre Oppermann 			 * created socket and update the tp variable.
114608d9c920SGleb Smirnoff 			 * If we came here via jump to tfo_socket_result,
114708d9c920SGleb Smirnoff 			 * then listening socket is read-locked.
1148be2ac88cSJonathan Lemon 			 */
114908d9c920SGleb Smirnoff 			INP_UNLOCK(inp);	/* listen socket */
1150be2ac88cSJonathan Lemon 			inp = sotoinpcb(so);
1151ff9b006dSJulien Charbon 			/*
1152ff9b006dSJulien Charbon 			 * New connection inpcb is already locked by
1153ff9b006dSJulien Charbon 			 * syncache_expand().
1154ff9b006dSJulien Charbon 			 */
1155ff9b006dSJulien Charbon 			INP_WLOCK_ASSERT(inp);
1156be2ac88cSJonathan Lemon 			tp = intotcpcb(inp);
11578d573cc1SAndre Oppermann 			KASSERT(tp->t_state == TCPS_SYN_RECEIVED,
11588d573cc1SAndre Oppermann 			    ("%s: ", __func__));
1159be2ac88cSJonathan Lemon 			/*
1160302ce8d6SAndre Oppermann 			 * Process the segment and the data it
1161302ce8d6SAndre Oppermann 			 * contains.  tcp_do_segment() consumes
1162302ce8d6SAndre Oppermann 			 * the mbuf chain and unlocks the inpcb.
1163302ce8d6SAndre Oppermann 			 */
11646138da62SMichael Tuexen 			TCP_PROBE5(receive, NULL, tp, m, tp, th);
116535bc0bccSGleb Smirnoff 			tp->t_fb->tfb_tcp_do_segment(tp, m, th, drop_hdrlen,
116635bc0bccSGleb Smirnoff 			    tlen, iptos);
11678f5a8818SKevin Lo 			return (IPPROTO_DONE);
1168be2ac88cSJonathan Lemon 		}
1169a160e630SAndre Oppermann 		/*
11708d573cc1SAndre Oppermann 		 * Segment flag validation for new connection attempts:
11718d573cc1SAndre Oppermann 		 *
1172a160e630SAndre Oppermann 		 * Our (SYN|ACK) response was rejected.
1173a160e630SAndre Oppermann 		 * Check with syncache and remove entry to prevent
1174a160e630SAndre Oppermann 		 * retransmits.
117519bc77c5SAndre Oppermann 		 *
117619bc77c5SAndre Oppermann 		 * NB: syncache_chkrst does its own logging of failure
117719bc77c5SAndre Oppermann 		 * causes.
1178a160e630SAndre Oppermann 		 */
1179a160e630SAndre Oppermann 		if (thflags & TH_RST) {
11809e644c23SMichael Tuexen 			syncache_chkrst(&inc, th, m, port);
1181a160e630SAndre Oppermann 			goto dropunlock;
1182a160e630SAndre Oppermann 		}
1183a160e630SAndre Oppermann 		/*
1184a160e630SAndre Oppermann 		 * We can't do anything without SYN.
1185a160e630SAndre Oppermann 		 */
1186a160e630SAndre Oppermann 		if ((thflags & TH_SYN) == 0) {
1187a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1188a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1189773673c1SAndre Oppermann 				    "SYN is missing, segment ignored\n",
11908d573cc1SAndre Oppermann 				    s, __func__);
119178b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1192a160e630SAndre Oppermann 			goto dropunlock;
1193a160e630SAndre Oppermann 		}
1194a160e630SAndre Oppermann 		/*
1195a160e630SAndre Oppermann 		 * (SYN|ACK) is bogus on a listen socket.
1196a160e630SAndre Oppermann 		 */
1197fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1198a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1199a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
12008d573cc1SAndre Oppermann 				    "SYN|ACK invalid, segment rejected\n",
12018d573cc1SAndre Oppermann 				    s, __func__);
12029e644c23SMichael Tuexen 			syncache_badack(&inc, port);	/* XXX: Not needed! */
120378b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1204a57815efSBosko Milekic 			rstreason = BANDLIM_RST_OPENPORT;
1205a57815efSBosko Milekic 			goto dropwithreset;
12064195b4afSPaul Traina 		}
1207a160e630SAndre Oppermann 		/*
1208a160e630SAndre Oppermann 		 * If the drop_synfin option is enabled, drop all
1209a160e630SAndre Oppermann 		 * segments with both the SYN and FIN bits set.
1210a160e630SAndre Oppermann 		 * This prevents e.g. nmap from identifying the
1211a160e630SAndre Oppermann 		 * TCP/IP stack.
1212a160e630SAndre Oppermann 		 * XXX: Poor reasoning.  nmap has other methods
1213a160e630SAndre Oppermann 		 * and is constantly refining its stack detection
1214a160e630SAndre Oppermann 		 * strategies.
12158d573cc1SAndre Oppermann 		 * XXX: This is a violation of the TCP specification
1216a160e630SAndre Oppermann 		 * and was used by RFC1644.
1217a160e630SAndre Oppermann 		 */
1218603724d3SBjoern A. Zeeb 		if ((thflags & TH_FIN) && V_drop_synfin) {
1219a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1220a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1221773673c1SAndre Oppermann 				    "SYN|FIN segment ignored (based on "
12228d573cc1SAndre Oppermann 				    "sysctl setting)\n", s, __func__);
122378b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1224302ce8d6SAndre Oppermann 			goto dropunlock;
1225ebb0cbeaSPaul Traina 		}
1226be2ac88cSJonathan Lemon 		/*
1227be2ac88cSJonathan Lemon 		 * Segment's flags are (SYN) or (SYN|FIN).
1228a160e630SAndre Oppermann 		 *
1229a160e630SAndre Oppermann 		 * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored
1230a160e630SAndre Oppermann 		 * as they do not affect the state of the TCP FSM.
1231a160e630SAndre Oppermann 		 * The data pointed to by TH_URG and th_urp is ignored.
1232be2ac88cSJonathan Lemon 		 */
1233a160e630SAndre Oppermann 		KASSERT((thflags & (TH_RST|TH_ACK)) == 0,
1234a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_RST or TH_ACK set", __func__));
1235a160e630SAndre Oppermann 		KASSERT(thflags & (TH_SYN),
1236a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_SYN not set", __func__));
123708d9c920SGleb Smirnoff 		INP_RLOCK_ASSERT(inp);
123833841545SHajimu UMEMOTO #ifdef INET6
123933841545SHajimu UMEMOTO 		/*
124033841545SHajimu UMEMOTO 		 * If deprecated address is forbidden,
124133841545SHajimu UMEMOTO 		 * we do not accept SYN to deprecated interface
124233841545SHajimu UMEMOTO 		 * address to prevent any new inbound connection from
124333841545SHajimu UMEMOTO 		 * getting established.
124433841545SHajimu UMEMOTO 		 * When we do not accept SYN, we send a TCP RST,
124533841545SHajimu UMEMOTO 		 * with deprecated source address (instead of dropping
124633841545SHajimu UMEMOTO 		 * it).  We compromise it as it is much better for peer
124733841545SHajimu UMEMOTO 		 * to send a RST, and RST will be the final packet
124833841545SHajimu UMEMOTO 		 * for the exchange.
124933841545SHajimu UMEMOTO 		 *
125033841545SHajimu UMEMOTO 		 * If we do not forbid deprecated addresses, we accept
125133841545SHajimu UMEMOTO 		 * the SYN packet.  RFC2462 does not suggest dropping
125233841545SHajimu UMEMOTO 		 * SYN in this case.
125333841545SHajimu UMEMOTO 		 * If we decipher RFC2462 5.5.4, it says like this:
125433841545SHajimu UMEMOTO 		 * 1. use of deprecated addr with existing
125533841545SHajimu UMEMOTO 		 *    communication is okay - "SHOULD continue to be
125633841545SHajimu UMEMOTO 		 *    used"
125733841545SHajimu UMEMOTO 		 * 2. use of it with new communication:
125833841545SHajimu UMEMOTO 		 *   (2a) "SHOULD NOT be used if alternate address
125933841545SHajimu UMEMOTO 		 *        with sufficient scope is available"
126033841545SHajimu UMEMOTO 		 *   (2b) nothing mentioned otherwise.
126133841545SHajimu UMEMOTO 		 * Here we fall into (2b) case as we have no choice in
126233841545SHajimu UMEMOTO 		 * our source address selection - we must obey the peer.
126333841545SHajimu UMEMOTO 		 *
126433841545SHajimu UMEMOTO 		 * The wording in RFC2462 is confusing, and there are
126533841545SHajimu UMEMOTO 		 * multiple description text for deprecated address
126633841545SHajimu UMEMOTO 		 * handling - worse, they are not exactly the same.
126733841545SHajimu UMEMOTO 		 * I believe 5.5.4 is the best one, so we follow 5.5.4.
126833841545SHajimu UMEMOTO 		 */
1269603724d3SBjoern A. Zeeb 		if (isipv6 && !V_ip6_use_deprecated) {
127033841545SHajimu UMEMOTO 			struct in6_ifaddr *ia6;
127133841545SHajimu UMEMOTO 
12728268d82cSAlexander V. Chernikov 			ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false);
12738c0fec80SRobert Watson 			if (ia6 != NULL &&
127433841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
1275a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1276a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12778d573cc1SAndre Oppermann 					"Connection attempt to deprecated "
12788d573cc1SAndre Oppermann 					"IPv6 address rejected\n",
1279a160e630SAndre Oppermann 					s, __func__);
128033841545SHajimu UMEMOTO 				rstreason = BANDLIM_RST_OPENPORT;
128133841545SHajimu UMEMOTO 				goto dropwithreset;
128233841545SHajimu UMEMOTO 			}
128333841545SHajimu UMEMOTO 		}
1284b287c6c7SBjoern A. Zeeb #endif /* INET6 */
128565f28919SJesper Skriver 		/*
1286db33b3e6SAndre Oppermann 		 * Basic sanity checks on incoming SYN requests:
12878d573cc1SAndre Oppermann 		 *   Don't respond if the destination is a link layer
1288db33b3e6SAndre Oppermann 		 *	broadcast according to RFC1122 4.2.3.10, p. 104.
12898d573cc1SAndre Oppermann 		 *   If it is from this socket it must be forged.
12908d573cc1SAndre Oppermann 		 *   Don't respond if the source or destination is a
12918d573cc1SAndre Oppermann 		 *	global or subnet broad- or multicast address.
129293ec91baSCrist J. Clark 		 *   Note that it is quite possible to receive unicast
129393ec91baSCrist J. Clark 		 *	link-layer packets with a broadcast IP address. Use
129493ec91baSCrist J. Clark 		 *	in_broadcast() to find them.
1295be2ac88cSJonathan Lemon 		 */
12968d573cc1SAndre Oppermann 		if (m->m_flags & (M_BCAST|M_MCAST)) {
12978d573cc1SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
12988d573cc1SAndre Oppermann 			    log(LOG_DEBUG, "%s; %s: Listen socket: "
12998d573cc1SAndre Oppermann 				"Connection attempt from broad- or multicast "
1300773673c1SAndre Oppermann 				"link layer address ignored\n", s, __func__);
1301302ce8d6SAndre Oppermann 			goto dropunlock;
13028d573cc1SAndre Oppermann 		}
1303db33b3e6SAndre Oppermann #ifdef INET6
1304b287c6c7SBjoern A. Zeeb 		if (isipv6) {
1305db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1306a160e630SAndre Oppermann 			    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) {
1307a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1308a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13098d573cc1SAndre Oppermann 					"Connection attempt to/from self "
1310773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1311302ce8d6SAndre Oppermann 				goto dropunlock;
1312a160e630SAndre Oppermann 			}
1313be2ac88cSJonathan Lemon 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1314a160e630SAndre Oppermann 			    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
1315a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1316a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13178d573cc1SAndre Oppermann 					"Connection attempt from/to multicast "
1318773673c1SAndre Oppermann 					"address ignored\n", s, __func__);
1319302ce8d6SAndre Oppermann 				goto dropunlock;
1320a160e630SAndre Oppermann 			}
1321b287c6c7SBjoern A. Zeeb 		}
1322db33b3e6SAndre Oppermann #endif
1323b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
1324b287c6c7SBjoern A. Zeeb 		else
1325b287c6c7SBjoern A. Zeeb #endif
1326b287c6c7SBjoern A. Zeeb #ifdef INET
1327b287c6c7SBjoern A. Zeeb 		{
1328db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1329a160e630SAndre Oppermann 			    ip->ip_dst.s_addr == ip->ip_src.s_addr) {
1330a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1331a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13328d573cc1SAndre Oppermann 					"Connection attempt from/to self "
1333773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1334302ce8d6SAndre Oppermann 				goto dropunlock;
1335a160e630SAndre Oppermann 			}
1336be2ac88cSJonathan Lemon 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
1337be2ac88cSJonathan Lemon 			    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
13382ca2159fSCrist J. Clark 			    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
1339a160e630SAndre Oppermann 			    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
1340a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1341a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13428d573cc1SAndre Oppermann 					"Connection attempt from/to broad- "
1343773673c1SAndre Oppermann 					"or multicast address ignored\n",
1344a160e630SAndre Oppermann 					s, __func__);
1345302ce8d6SAndre Oppermann 				goto dropunlock;
1346c068736aSJeffrey Hsu 			}
1347a160e630SAndre Oppermann 		}
1348b287c6c7SBjoern A. Zeeb #endif
1349be2ac88cSJonathan Lemon 		/*
1350db33b3e6SAndre Oppermann 		 * SYN appears to be valid.  Create compressed TCP state
1351db33b3e6SAndre Oppermann 		 * for syncache.
1352be2ac88cSJonathan Lemon 		 */
13532b9c9984SGeorge V. Neville-Neil 		TCP_PROBE3(debug__input, tp, th, m);
1354f72167f4SAndre Oppermann 		tcp_dooptions(&to, optp, optlen, TO_SYN);
13558d5719aaSGleb Smirnoff 		if ((so = syncache_add(&inc, &to, th, inp, so, m, NULL, NULL,
13569e644c23SMichael Tuexen 		    iptos, port)) != NULL)
135709c305ebSPatrick Kelsey 			goto tfo_socket_result;
135818a75309SPatrick Kelsey 
1359be2ac88cSJonathan Lemon 		/*
13604d6e7130SAndre Oppermann 		 * Entry added to syncache and mbuf consumed.
1361a7c7f2a7SJohn Baldwin 		 * Only the listen socket is unlocked by syncache_add().
1362be2ac88cSJonathan Lemon 		 */
13638f5a8818SKevin Lo 		return (IPPROTO_DONE);
1364982c1675SAndre Oppermann 	} else if (tp->t_state == TCPS_LISTEN) {
1365982c1675SAndre Oppermann 		/*
1366982c1675SAndre Oppermann 		 * When a listen socket is torn down the SO_ACCEPTCONN
1367982c1675SAndre Oppermann 		 * flag is removed first while connections are drained
1368982c1675SAndre Oppermann 		 * from the accept queue in a unlock/lock cycle of the
1369982c1675SAndre Oppermann 		 * ACCEPT_LOCK, opening a race condition allowing a SYN
1370982c1675SAndre Oppermann 		 * attempt go through unhandled.
1371982c1675SAndre Oppermann 		 */
1372982c1675SAndre Oppermann 		goto dropunlock;
1373f76fcf6dSJeffrey Hsu 	}
1374fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1375fcf59617SAndrey V. Elsukov 	if (tp->t_flags & TF_SIGNATURE) {
1376fcf59617SAndrey V. Elsukov 		tcp_dooptions(&to, optp, optlen, thflags);
1377fcf59617SAndrey V. Elsukov 		if ((to.to_flags & TOF_SIGNATURE) == 0) {
1378fcf59617SAndrey V. Elsukov 			TCPSTAT_INC(tcps_sig_err_nosigopt);
13792903309aSAttilio Rao 			goto dropunlock;
13802903309aSAttilio Rao 		}
1381fcf59617SAndrey V. Elsukov 		if (!TCPMD5_ENABLED() ||
1382fcf59617SAndrey V. Elsukov 		    TCPMD5_INPUT(m, th, to.to_signature) != 0)
1383fcf59617SAndrey V. Elsukov 			goto dropunlock;
13842903309aSAttilio Rao 	}
13852903309aSAttilio Rao #endif
13862b9c9984SGeorge V. Neville-Neil 	TCP_PROBE5(receive, NULL, tp, m, tp, th);
138757f60867SMark Johnston 
1388302ce8d6SAndre Oppermann 	/*
13898d573cc1SAndre Oppermann 	 * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later
13908d573cc1SAndre Oppermann 	 * state.  tcp_do_segment() always consumes the mbuf chain, unlocks
13918d573cc1SAndre Oppermann 	 * the inpcb, and unlocks pcbinfo.
13921db08fbeSGleb Smirnoff 	 *
13931db08fbeSGleb Smirnoff 	 * XXXGL: in case of a pure SYN arriving on existing connection
13941db08fbeSGleb Smirnoff 	 * TCP stacks won't need to modify the PCB, they would either drop
13951db08fbeSGleb Smirnoff 	 * the segment silently, or send a challenge ACK.  However, we try
13961db08fbeSGleb Smirnoff 	 * to upgrade the lock, because calling convention for stacks is
13971db08fbeSGleb Smirnoff 	 * write-lock on PCB.  If upgrade fails, drop the SYN.
1398302ce8d6SAndre Oppermann 	 */
1399d88eb465SGleb Smirnoff 	if ((lookupflag & INPLOOKUP_RLOCKPCB) && INP_TRY_UPGRADE(inp) == 0)
14001db08fbeSGleb Smirnoff 		goto dropunlock;
14011db08fbeSGleb Smirnoff 
140235bc0bccSGleb Smirnoff 	tp->t_fb->tfb_tcp_do_segment(tp, m, th, drop_hdrlen, tlen, iptos);
14038f5a8818SKevin Lo 	return (IPPROTO_DONE);
1404be2ac88cSJonathan Lemon 
1405302ce8d6SAndre Oppermann dropwithreset:
14062b9c9984SGeorge V. Neville-Neil 	TCP_PROBE5(receive, NULL, tp, m, tp, th);
140757f60867SMark Johnston 
1408014ea782SRobert Watson 	if (inp != NULL) {
1409302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
141008d9c920SGleb Smirnoff 		INP_UNLOCK(inp);
1411dd8ac7f9SRobert Watson 	} else
1412014ea782SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
1413302ce8d6SAndre Oppermann 	m = NULL;	/* mbuf chain got consumed. */
1414014ea782SRobert Watson 	goto drop;
1415014ea782SRobert Watson 
1416302ce8d6SAndre Oppermann dropunlock:
141757f60867SMark Johnston 	if (m != NULL)
14182b9c9984SGeorge V. Neville-Neil 		TCP_PROBE5(receive, NULL, tp, m, tp, th);
141957f60867SMark Johnston 
14209fa198beSAndre Oppermann 	if (inp != NULL)
142108d9c920SGleb Smirnoff 		INP_UNLOCK(inp);
1422014ea782SRobert Watson 
1423302ce8d6SAndre Oppermann drop:
1424a160e630SAndre Oppermann 	if (s != NULL)
1425a160e630SAndre Oppermann 		free(s, M_TCPLOG);
1426302ce8d6SAndre Oppermann 	if (m != NULL)
1427302ce8d6SAndre Oppermann 		m_freem(m);
14288f5a8818SKevin Lo 	return (IPPROTO_DONE);
1429302ce8d6SAndre Oppermann }
1430302ce8d6SAndre Oppermann 
1431e44c1887SSteven Hartland /*
1432e44c1887SSteven Hartland  * Automatic sizing of receive socket buffer.  Often the send
1433e44c1887SSteven Hartland  * buffer size is not optimally adjusted to the actual network
1434e44c1887SSteven Hartland  * conditions at hand (delay bandwidth product).  Setting the
1435e44c1887SSteven Hartland  * buffer size too small limits throughput on links with high
1436e44c1887SSteven Hartland  * bandwidth and high delay (eg. trans-continental/oceanic links).
1437e44c1887SSteven Hartland  *
1438e44c1887SSteven Hartland  * On the receive side the socket buffer memory is only rarely
1439e44c1887SSteven Hartland  * used to any significant extent.  This allows us to be much
1440e44c1887SSteven Hartland  * more aggressive in scaling the receive socket buffer.  For
1441e44c1887SSteven Hartland  * the case that the buffer space is actually used to a large
1442e44c1887SSteven Hartland  * extent and we run out of kernel memory we can simply drop
1443e44c1887SSteven Hartland  * the new segments; TCP on the sender will just retransmit it
1444e44c1887SSteven Hartland  * later.  Setting the buffer size too big may only consume too
1445e44c1887SSteven Hartland  * much kernel memory if the application doesn't read() from
1446e44c1887SSteven Hartland  * the socket or packet loss or reordering makes use of the
1447e44c1887SSteven Hartland  * reassembly queue.
1448e44c1887SSteven Hartland  *
1449e44c1887SSteven Hartland  * The criteria to step up the receive buffer one notch are:
1450e44c1887SSteven Hartland  *  1. Application has not set receive buffer size with
1451e44c1887SSteven Hartland  *     SO_RCVBUF. Setting SO_RCVBUF clears SB_AUTOSIZE.
1452560c0586SMichael Tuexen  *  2. the number of bytes received during 1/2 of an sRTT
1453560c0586SMichael Tuexen  *     is at least 3/8 of the current socket buffer size.
1454560c0586SMichael Tuexen  *  3. receive buffer size has not hit maximal automatic size;
1455e44c1887SSteven Hartland  *
1456560c0586SMichael Tuexen  * If all of the criteria are met we increaset the socket buffer
1457560c0586SMichael Tuexen  * by a 1/2 (bounded by the max). This allows us to keep ahead
1458560c0586SMichael Tuexen  * of slow-start but also makes it so our peer never gets limited
1459560c0586SMichael Tuexen  * by our rwnd which we then open up causing a burst.
1460560c0586SMichael Tuexen  *
1461560c0586SMichael Tuexen  * This algorithm does two steps per RTT at most and only if
1462e44c1887SSteven Hartland  * we receive a bulk stream w/o packet losses or reorderings.
1463e44c1887SSteven Hartland  * Shrinking the buffer during idle times is not necessary as
1464e44c1887SSteven Hartland  * it doesn't consume any memory when idle.
1465e44c1887SSteven Hartland  *
1466e44c1887SSteven Hartland  * TODO: Only step up if the application is actually serving
1467e44c1887SSteven Hartland  * the buffer to better manage the socket buffer resources.
1468e44c1887SSteven Hartland  */
1469e44c1887SSteven Hartland int
1470e44c1887SSteven Hartland tcp_autorcvbuf(struct mbuf *m, struct tcphdr *th, struct socket *so,
1471e44c1887SSteven Hartland     struct tcpcb *tp, int tlen)
1472e44c1887SSteven Hartland {
1473e44c1887SSteven Hartland 	int newsize = 0;
1474e44c1887SSteven Hartland 
1475e44c1887SSteven Hartland 	if (V_tcp_do_autorcvbuf && (so->so_rcv.sb_flags & SB_AUTOSIZE) &&
1476e44c1887SSteven Hartland 	    tp->t_srtt != 0 && tp->rfbuf_ts != 0 &&
1477e44c1887SSteven Hartland 	    TCP_TS_TO_TICKS(tcp_ts_getticks() - tp->rfbuf_ts) >
1478560c0586SMichael Tuexen 	    ((tp->t_srtt >> TCP_RTT_SHIFT)/2)) {
1479560c0586SMichael Tuexen 		if (tp->rfbuf_cnt > ((so->so_rcv.sb_hiwat / 2)/ 4 * 3) &&
1480e44c1887SSteven Hartland 		    so->so_rcv.sb_hiwat < V_tcp_autorcvbuf_max) {
1481560c0586SMichael Tuexen 			newsize = min((so->so_rcv.sb_hiwat + (so->so_rcv.sb_hiwat/2)), V_tcp_autorcvbuf_max);
1482e44c1887SSteven Hartland 		}
1483e44c1887SSteven Hartland 		TCP_PROBE6(receive__autoresize, NULL, tp, m, tp, th, newsize);
1484e44c1887SSteven Hartland 
1485e44c1887SSteven Hartland 		/* Start over with next RTT. */
1486e44c1887SSteven Hartland 		tp->rfbuf_ts = 0;
1487e44c1887SSteven Hartland 		tp->rfbuf_cnt = 0;
1488e44c1887SSteven Hartland 	} else {
1489e44c1887SSteven Hartland 		tp->rfbuf_cnt += tlen;	/* add up */
1490e44c1887SSteven Hartland 	}
1491e44c1887SSteven Hartland 	return (newsize);
1492e44c1887SSteven Hartland }
1493e44c1887SSteven Hartland 
14949e644c23SMichael Tuexen int
14959e644c23SMichael Tuexen tcp_input(struct mbuf **mp, int *offp, int proto)
14969e644c23SMichael Tuexen {
14979e644c23SMichael Tuexen 	return(tcp_input_with_port(mp, offp, proto, 0));
14989e644c23SMichael Tuexen }
14999e644c23SMichael Tuexen 
1500c348e880SGleb Smirnoff static void
1501c348e880SGleb Smirnoff tcp_handle_wakeup(struct tcpcb *tp)
15024d0770f1SRichard Scheffenegger {
1503c348e880SGleb Smirnoff 
15049eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
1505c348e880SGleb Smirnoff 
15064d0770f1SRichard Scheffenegger 	if (tp->t_flags & TF_WAKESOR) {
15079eb0e832SGleb Smirnoff 		struct socket *so = tptosocket(tp);
1508c348e880SGleb Smirnoff 
15094d0770f1SRichard Scheffenegger 		tp->t_flags &= ~TF_WAKESOR;
1510032bf749SRichard Scheffenegger 		SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1511032bf749SRichard Scheffenegger 		sorwakeup_locked(so);
15124d0770f1SRichard Scheffenegger 	}
15134d0770f1SRichard Scheffenegger }
15144d0770f1SRichard Scheffenegger 
15154d0770f1SRichard Scheffenegger void
151635bc0bccSGleb Smirnoff tcp_do_segment(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th,
151735bc0bccSGleb Smirnoff     int drop_hdrlen, int tlen, uint8_t iptos)
1518302ce8d6SAndre Oppermann {
1519f7220c48SRichard Scheffenegger 	uint16_t thflags;
152049a6fbe3SRichard Scheffenegger 	int acked, ourfinisacked, needoutput = 0;
152149a6fbe3SRichard Scheffenegger 	sackstatus_t sack_changed;
1522b2ade6b1SRichard Scheffenegger 	int rstreason, todrop, win, incforsyn = 0;
15233ac12506SJonathan T. Looney 	uint32_t tiwin;
15244b7b743cSLawrence Stewart 	uint16_t nsegs;
152507dacf03SAndre Oppermann 	char *s;
15269eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
152735bc0bccSGleb Smirnoff 	struct socket *so = tptosocket(tp);
15289eb0e832SGleb Smirnoff 	struct in_conninfo *inc = &inp->inp_inc;
1529c11a15bfSGleb Smirnoff 	struct mbuf *mfree;
1530302ce8d6SAndre Oppermann 	struct tcpopt to;
1531281a0fd4SPatrick Kelsey 	int tfo_syn;
1532c7c325d0SRichard Scheffenegger 	u_int maxseg = 0;
1533302ce8d6SAndre Oppermann 
15341ebf4607SRichard Scheffenegger 	thflags = tcp_get_flags(th);
1535d64a46eaSLawrence Stewart 	tp->sackhint.last_sack_ack = 0;
153649a6fbe3SRichard Scheffenegger 	sack_changed = SACK_NOCHANGE;
15374b7b743cSLawrence Stewart 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
1538d40c0d47SGleb Smirnoff 
1539d40c0d47SGleb Smirnoff 	NET_EPOCH_ASSERT();
15409eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
1541679d9708SAndre Oppermann 	KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
1542679d9708SAndre Oppermann 	    __func__));
1543679d9708SAndre Oppermann 	KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
1544679d9708SAndre Oppermann 	    __func__));
1545df8bae1dSRodney W. Grimes 
154686a996e6SHiren Panchasara #ifdef TCPPCAP
154786a996e6SHiren Panchasara 	/* Save segment, if requested. */
154886a996e6SHiren Panchasara 	tcp_pcap_add(th, m, &(tp->t_inpkts));
154986a996e6SHiren Panchasara #endif
15502529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0,
15512529f56eSJonathan T. Looney 	    tlen, NULL, true);
155286a996e6SHiren Panchasara 
1553013f4df6SMichael Tuexen 	if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
1554013f4df6SMichael Tuexen 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
1555013f4df6SMichael Tuexen 			log(LOG_DEBUG, "%s; %s: "
1556013f4df6SMichael Tuexen 			    "SYN|FIN segment ignored (based on "
1557013f4df6SMichael Tuexen 			    "sysctl setting)\n", s, __func__);
1558013f4df6SMichael Tuexen 			free(s, M_TCPLOG);
1559013f4df6SMichael Tuexen 		}
1560013f4df6SMichael Tuexen 		goto drop;
1561013f4df6SMichael Tuexen 	}
1562013f4df6SMichael Tuexen 
1563df8bae1dSRodney W. Grimes 	/*
1564ebfd7534SMichael Tuexen 	 * If a segment with the ACK-bit set arrives in the SYN-SENT state
1565ebfd7534SMichael Tuexen 	 * check SEQ.ACK first.
1566ebfd7534SMichael Tuexen 	 */
1567ebfd7534SMichael Tuexen 	if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) &&
1568ebfd7534SMichael Tuexen 	    (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) {
1569ebfd7534SMichael Tuexen 		rstreason = BANDLIM_UNLIMITED;
1570d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
1571ebfd7534SMichael Tuexen 		goto dropwithreset;
1572ebfd7534SMichael Tuexen 	}
1573ebfd7534SMichael Tuexen 
1574ebfd7534SMichael Tuexen 	/*
1575df8bae1dSRodney W. Grimes 	 * Segment received on connection.
1576df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
1577e8949f74SAndre Oppermann 	 * XXX: This should be done after segment
1578e8949f74SAndre Oppermann 	 * validation to ignore broken/spoofed segs.
1579df8bae1dSRodney W. Grimes 	 */
1580cd84e78fSRandall Stewart 	if  (tp->t_idle_reduce &&
1581cd84e78fSRandall Stewart 	     (tp->snd_max == tp->snd_una) &&
1582cd84e78fSRandall Stewart 	     ((ticks - tp->t_rcvtime) >= tp->t_rxtcur))
1583cd84e78fSRandall Stewart 		cc_after_idle(tp);
15849b8b58e0SJonathan Lemon 	tp->t_rcvtime = ticks;
1585df8bae1dSRodney W. Grimes 
1586d1b07f36SRandall Stewart 	if (thflags & TH_FIN)
1587d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_FIN);
1588df8bae1dSRodney W. Grimes 	/*
1589c52102ddSHiren Panchasara 	 * Scale up the window into a 32-bit value.
1590e8949f74SAndre Oppermann 	 * For the SYN_SENT state the scale is zero.
1591464fcfbcSAndre Oppermann 	 */
1592464fcfbcSAndre Oppermann 	tiwin = th->th_win << tp->snd_scale;
1593adc56f5aSEdward Tomasz Napierala #ifdef STATS
1594adc56f5aSEdward Tomasz Napierala 	stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin);
1595adc56f5aSEdward Tomasz Napierala #endif
1596464fcfbcSAndre Oppermann 
1597464fcfbcSAndre Oppermann 	/*
1598f2512ba1SRui Paulo 	 * TCP ECN processing.
1599f2512ba1SRui Paulo 	 */
1600b1258b76SRichard Scheffenegger 	if (tcp_ecn_input_segment(tp, thflags, tlen,
1601b1258b76SRichard Scheffenegger 	    tcp_packets_this_ack(tp, th->th_ack),
1602b1258b76SRichard Scheffenegger 	    iptos))
1603dbc42409SLawrence Stewart 		cc_cong_signal(tp, th, CC_ECN);
1604f2512ba1SRui Paulo 
1605f2512ba1SRui Paulo 	/*
1606f72167f4SAndre Oppermann 	 * Parse options on any incoming segment.
1607f72167f4SAndre Oppermann 	 */
1608302ce8d6SAndre Oppermann 	tcp_dooptions(&to, (u_char *)(th + 1),
1609302ce8d6SAndre Oppermann 	    (th->th_off << 2) - sizeof(struct tcphdr),
1610302ce8d6SAndre Oppermann 	    (thflags & TH_SYN) ? TO_SYN : 0);
1611f72167f4SAndre Oppermann 
1612fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1613fcf59617SAndrey V. Elsukov 	if ((tp->t_flags & TF_SIGNATURE) != 0 &&
1614fcf59617SAndrey V. Elsukov 	    (to.to_flags & TOF_SIGNATURE) == 0) {
1615fcf59617SAndrey V. Elsukov 		TCPSTAT_INC(tcps_sig_err_sigopt);
1616fcf59617SAndrey V. Elsukov 		/* XXX: should drop? */
1617fcf59617SAndrey V. Elsukov 	}
1618fcf59617SAndrey V. Elsukov #endif
1619f72167f4SAndre Oppermann 	/*
1620f72167f4SAndre Oppermann 	 * If echoed timestamp is later than the current time,
1621bf6d304aSAndre Oppermann 	 * fall back to non RFC1323 RTT calculation.  Normalize
1622bf6d304aSAndre Oppermann 	 * timestamp if syncookies were used when this connection
1623bf6d304aSAndre Oppermann 	 * was established.
1624f72167f4SAndre Oppermann 	 */
1625bf6d304aSAndre Oppermann 	if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
1626e16fa5caSJohn-Mark Gurney 		to.to_tsecr -= tp->ts_offset;
1627*2d05a1c8SRichard Scheffenegger 		if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks())) {
1628f72167f4SAndre Oppermann 			to.to_tsecr = 0;
1629*2d05a1c8SRichard Scheffenegger 		} else if (tp->t_rxtshift == 1 &&
16304531b345SRichard Scheffenegger 			 tp->t_flags & TF_PREVVALID &&
16314531b345SRichard Scheffenegger 			 tp->t_badrxtwin != 0 &&
1632*2d05a1c8SRichard Scheffenegger 			 TSTMP_LT(to.to_tsecr, tp->t_badrxtwin)) {
163310d20c84SMatt Macy 			cc_cong_signal(tp, th, CC_RTO_ERR);
1634bf6d304aSAndre Oppermann 		}
1635*2d05a1c8SRichard Scheffenegger 	}
163607dacf03SAndre Oppermann 	/*
163797d8d152SAndre Oppermann 	 * Process options only when we get SYN/ACK back. The SYN case
163897d8d152SAndre Oppermann 	 * for incoming connections is handled in tcp_syncache.
16395396d0f8SAndre Oppermann 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN>
16405396d0f8SAndre Oppermann 	 * or <SYN,ACK>) segment itself is never scaled.
164197d8d152SAndre Oppermann 	 * XXX this is traditional behavior, may need to be cleaned up.
1642df8bae1dSRodney W. Grimes 	 */
16430a389eabSSimon L. B. Nielsen 	if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
16446e16d877SRichard Scheffenegger 		/* Handle parallel SYN for ECN */
1645f7220c48SRichard Scheffenegger 		tcp_ecn_input_parallel_syn(tp, thflags, iptos);
1646464fcfbcSAndre Oppermann 		if ((to.to_flags & TOF_SCALE) &&
16472593f858SRichard Scheffenegger 		    (tp->t_flags & TF_REQ_SCALE) &&
16482593f858SRichard Scheffenegger 		    !(tp->t_flags & TF_NOOPT)) {
1649be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_SCALE;
165002a1a643SAndre Oppermann 			tp->snd_scale = to.to_wscale;
1651*2d05a1c8SRichard Scheffenegger 		} else {
16528e051165SRichard Scheffenegger 			tp->t_flags &= ~TF_REQ_SCALE;
1653*2d05a1c8SRichard Scheffenegger 		}
16545396d0f8SAndre Oppermann 		/*
16555396d0f8SAndre Oppermann 		 * Initial send window.  It will be updated with
16565396d0f8SAndre Oppermann 		 * the next incoming segment to the scaled value.
16575396d0f8SAndre Oppermann 		 */
16585396d0f8SAndre Oppermann 		tp->snd_wnd = th->th_win;
16598e051165SRichard Scheffenegger 		if ((to.to_flags & TOF_TS) &&
16602593f858SRichard Scheffenegger 		    (tp->t_flags & TF_REQ_TSTMP) &&
16612593f858SRichard Scheffenegger 		    !(tp->t_flags & TF_NOOPT)) {
1662be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_TSTMP;
1663be2ac88cSJonathan Lemon 			tp->ts_recent = to.to_tsval;
1664d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
1665*2d05a1c8SRichard Scheffenegger 		} else {
16668e051165SRichard Scheffenegger 			tp->t_flags &= ~TF_REQ_TSTMP;
1667*2d05a1c8SRichard Scheffenegger 		}
1668*2d05a1c8SRichard Scheffenegger 		if (to.to_flags & TOF_MSS) {
1669be2ac88cSJonathan Lemon 			tcp_mss(tp, to.to_mss);
1670*2d05a1c8SRichard Scheffenegger 		}
16713529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
16722593f858SRichard Scheffenegger 		    (!(to.to_flags & TOF_SACKPERM) ||
1673*2d05a1c8SRichard Scheffenegger 		    (tp->t_flags & TF_NOOPT))) {
16743529149eSAndre Oppermann 			tp->t_flags &= ~TF_SACK_PERMIT;
1675*2d05a1c8SRichard Scheffenegger 		}
1676c560df6fSPatrick Kelsey 		if (IS_FASTOPEN(tp->t_flags)) {
16772593f858SRichard Scheffenegger 			if ((to.to_flags & TOF_FASTOPEN) &&
16782593f858SRichard Scheffenegger 			    !(tp->t_flags & TF_NOOPT)) {
1679a026a53aSMichael Tuexen 				uint16_t mss;
1680a026a53aSMichael Tuexen 
1681*2d05a1c8SRichard Scheffenegger 				if (to.to_flags & TOF_MSS) {
1682a026a53aSMichael Tuexen 					mss = to.to_mss;
1683*2d05a1c8SRichard Scheffenegger 				} else {
1684*2d05a1c8SRichard Scheffenegger 					if ((inp->inp_vflag & INP_IPV6) != 0) {
1685a026a53aSMichael Tuexen 						mss = TCP6_MSS;
1686*2d05a1c8SRichard Scheffenegger 					} else {
1687a026a53aSMichael Tuexen 						mss = TCP_MSS;
1688*2d05a1c8SRichard Scheffenegger 					}
1689*2d05a1c8SRichard Scheffenegger 				}
1690a026a53aSMichael Tuexen 				tcp_fastopen_update_cache(tp, mss,
1691a026a53aSMichael Tuexen 				    to.to_tfo_len, to.to_tfo_cookie);
1692*2d05a1c8SRichard Scheffenegger 			} else {
1693c560df6fSPatrick Kelsey 				tcp_fastopen_disable_path(tp);
1694c560df6fSPatrick Kelsey 			}
16956d90faf3SPaul Saab 		}
1696*2d05a1c8SRichard Scheffenegger 	}
16976d90faf3SPaul Saab 
1698df8bae1dSRodney W. Grimes 	/*
1699283c76c7SMichael Tuexen 	 * If timestamps were negotiated during SYN/ACK and a
1700283c76c7SMichael Tuexen 	 * segment without a timestamp is received, silently drop
1701d2b3ceddSMichael Tuexen 	 * the segment, unless it is a RST segment or missing timestamps are
1702d2b3ceddSMichael Tuexen 	 * tolerated.
1703283c76c7SMichael Tuexen 	 * See section 3.2 of RFC 7323.
17044da9052aSMichael Tuexen 	 */
17054da9052aSMichael Tuexen 	if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) {
1706d2b3ceddSMichael Tuexen 		if (((thflags & TH_RST) != 0) || V_tcp_tolerate_missing_ts) {
1707cc3c3485SMichael Tuexen 			if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
1708cc3c3485SMichael Tuexen 				log(LOG_DEBUG, "%s; %s: Timestamp missing, "
1709cc3c3485SMichael Tuexen 				    "segment processed normally\n",
1710cc3c3485SMichael Tuexen 				    s, __func__);
1711cc3c3485SMichael Tuexen 				free(s, M_TCPLOG);
1712cc3c3485SMichael Tuexen 			}
1713cc3c3485SMichael Tuexen 		} else {
17144da9052aSMichael Tuexen 			if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
17154da9052aSMichael Tuexen 				log(LOG_DEBUG, "%s; %s: Timestamp missing, "
1716283c76c7SMichael Tuexen 				    "segment silently dropped\n", s, __func__);
17174da9052aSMichael Tuexen 				free(s, M_TCPLOG);
17184da9052aSMichael Tuexen 			}
1719283c76c7SMichael Tuexen 			goto drop;
17204da9052aSMichael Tuexen 		}
1721cc3c3485SMichael Tuexen 	}
1722283c76c7SMichael Tuexen 	/*
1723283c76c7SMichael Tuexen 	 * If timestamps were not negotiated during SYN/ACK and a
172475fcd27aSMichael Tuexen 	 * segment with a timestamp is received, ignore the
1725283c76c7SMichael Tuexen 	 * timestamp and process the packet normally.
1726283c76c7SMichael Tuexen 	 * See section 3.2 of RFC 7323.
1727283c76c7SMichael Tuexen 	 */
17284da9052aSMichael Tuexen 	if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) {
17294da9052aSMichael Tuexen 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
17304da9052aSMichael Tuexen 			log(LOG_DEBUG, "%s; %s: Timestamp not expected, "
1731283c76c7SMichael Tuexen 			    "segment processed normally\n", s, __func__);
17324da9052aSMichael Tuexen 			free(s, M_TCPLOG);
17334da9052aSMichael Tuexen 		}
17344da9052aSMichael Tuexen 	}
17354da9052aSMichael Tuexen 
17364da9052aSMichael Tuexen 	/*
1737df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
1738df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
1739df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
1740df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
1741df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
1742df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
1743df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
1744df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
1745df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
1746df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
1747df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
1748df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
1749a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
1750c5ad39b9SAndre Oppermann 	 * Since we check for TCPS_ESTABLISHED first, it can only
1751a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
1752df8bae1dSRodney W. Grimes 	 */
1753df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
1754c5ad39b9SAndre Oppermann 	    th->th_seq == tp->rcv_nxt &&
1755fb59c426SYoshinobu Inoue 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1756c5ad39b9SAndre Oppermann 	    tp->snd_nxt == tp->snd_max &&
1757c5ad39b9SAndre Oppermann 	    tiwin && tiwin == tp->snd_wnd &&
1758a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
1759c28440dbSRandall Stewart 	    SEGQ_EMPTY(tp) &&
17604741bfcbSPatrick Kelsey 	    ((to.to_flags & TOF_TS) == 0 ||
1761c5ad39b9SAndre Oppermann 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) {
1762df8bae1dSRodney W. Grimes 		/*
1763df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
1764df8bae1dSRodney W. Grimes 		 * record the timestamp.
1765a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
1766a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1767df8bae1dSRodney W. Grimes 		 */
1768be2ac88cSJonathan Lemon 		if ((to.to_flags & TOF_TS) != 0 &&
1769fb59c426SYoshinobu Inoue 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1770d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
1771a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
1772df8bae1dSRodney W. Grimes 		}
1773df8bae1dSRodney W. Grimes 
1774fb59c426SYoshinobu Inoue 		if (tlen == 0) {
1775fb59c426SYoshinobu Inoue 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
1776fb59c426SYoshinobu Inoue 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
1777dbc42409SLawrence Stewart 			    !IN_RECOVERY(tp->t_flags) &&
1778fc30a251SAndre Oppermann 			    (to.to_flags & TOF_SACK) == 0 &&
1779dbc42409SLawrence Stewart 			    TAILQ_EMPTY(&tp->snd_holes)) {
1780df8bae1dSRodney W. Grimes 				/*
1781e8949f74SAndre Oppermann 				 * This is a pure ack for outstanding data.
1782df8bae1dSRodney W. Grimes 				 */
178378b50714SRobert Watson 				TCPSTAT_INC(tcps_predack);
1784252ca428SRobert Watson 
17859b8b58e0SJonathan Lemon 				/*
178610d20c84SMatt Macy 				 * "bad retransmit" recovery without timestamps.
17879b8b58e0SJonathan Lemon 				 */
178810d20c84SMatt Macy 				if ((to.to_flags & TOF_TS) == 0 &&
178910d20c84SMatt Macy 				    tp->t_rxtshift == 1 &&
1790672dc4aeSJohn Baldwin 				    tp->t_flags & TF_PREVVALID &&
17914531b345SRichard Scheffenegger 				    tp->t_badrxtwin != 0 &&
17924531b345SRichard Scheffenegger 				    TSTMP_LT(ticks, tp->t_badrxtwin)) {
1793dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_RTO_ERR);
17949b8b58e0SJonathan Lemon 				}
1795fa55172bSMatthew Dillon 
1796fa55172bSMatthew Dillon 				/*
1797fa55172bSMatthew Dillon 				 * Recalculate the transmit timer / rtt.
1798fa55172bSMatthew Dillon 				 *
1799fa55172bSMatthew Dillon 				 * Some boxes send broken timestamp replies
1800fa55172bSMatthew Dillon 				 * during the SYN+ACK phase, ignore
1801fa55172bSMatthew Dillon 				 * timestamps of 0 or we could calculate a
1802fa55172bSMatthew Dillon 				 * huge RTT and blow up the retransmit timer.
1803fa55172bSMatthew Dillon 				 */
1804fa55172bSMatthew Dillon 				if ((to.to_flags & TOF_TS) != 0 &&
1805fa55172bSMatthew Dillon 				    to.to_tsecr) {
18063ac12506SJonathan T. Looney 					uint32_t t;
1807d8951c8aSBjoern A. Zeeb 
1808d8951c8aSBjoern A. Zeeb 					t = tcp_ts_getticks() - to.to_tsecr;
1809d8951c8aSBjoern A. Zeeb 					if (!tp->t_rttlow || tp->t_rttlow > t)
1810d8951c8aSBjoern A. Zeeb 						tp->t_rttlow = t;
1811a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
1812d8951c8aSBjoern A. Zeeb 					    TCP_TS_TO_TICKS(t) + 1);
1813fa55172bSMatthew Dillon 				} else if (tp->t_rtttime &&
1814fa55172bSMatthew Dillon 				    SEQ_GT(th->th_ack, tp->t_rtseq)) {
1815eaf80179SAndre Oppermann 					if (!tp->t_rttlow ||
1816eaf80179SAndre Oppermann 					    tp->t_rttlow > ticks - tp->t_rtttime)
1817eaf80179SAndre Oppermann 						tp->t_rttlow = ticks - tp->t_rtttime;
1818c068736aSJeffrey Hsu 					tcp_xmit_timer(tp,
1819c068736aSJeffrey Hsu 							ticks - tp->t_rtttime);
1820fa55172bSMatthew Dillon 				}
1821dbc42409SLawrence Stewart 				acked = BYTES_THIS_ACK(tp, th);
182239bc9de5SLawrence Stewart 
1823bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
182439bc9de5SLawrence Stewart 				/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
182539bc9de5SLawrence Stewart 				hhook_run_tcp_est_in(tp, th, &to);
1826bd79708dSJonathan T. Looney #endif
182739bc9de5SLawrence Stewart 
18284b7b743cSLawrence Stewart 				TCPSTAT_ADD(tcps_rcvackpack, nsegs);
182978b50714SRobert Watson 				TCPSTAT_ADD(tcps_rcvackbyte, acked);
1830df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
18319d11646dSJeffrey Hsu 				if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
18329d11646dSJeffrey Hsu 				    SEQ_LEQ(th->th_ack, tp->snd_recover))
18339d11646dSJeffrey Hsu 					tp->snd_recover = th->th_ack - 1;
1834dbc42409SLawrence Stewart 
1835dbc42409SLawrence Stewart 				/*
1836dbc42409SLawrence Stewart 				 * Let the congestion control algorithm update
1837dbc42409SLawrence Stewart 				 * congestion control related information. This
1838dbc42409SLawrence Stewart 				 * typically means increasing the congestion
1839dbc42409SLawrence Stewart 				 * window.
1840dbc42409SLawrence Stewart 				 */
18414b7b743cSLawrence Stewart 				cc_ack_received(tp, th, nsegs, CC_ACK);
1842dbc42409SLawrence Stewart 
18439d11646dSJeffrey Hsu 				tp->snd_una = th->th_ack;
18441645d090SJeff Roberson 				/*
1845e8949f74SAndre Oppermann 				 * Pull snd_wl2 up to prevent seq wrap relative
18461645d090SJeff Roberson 				 * to th_ack.
18471645d090SJeff Roberson 				 */
1848cb942153SJeffrey Hsu 				tp->snd_wl2 = th->th_ack;
1849b5addd85SJeffrey Hsu 				tp->t_dupacks = 0;
1850df8bae1dSRodney W. Grimes 				m_freem(m);
1851df8bae1dSRodney W. Grimes 
1852df8bae1dSRodney W. Grimes 				/*
1853df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
1854df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
1855df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
1856df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
1857df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
1858df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
1859df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
1860e8949f74SAndre Oppermann 				 */
18612b9c9984SGeorge V. Neville-Neil 				TCP_PROBE3(debug__input, tp, th, m);
186208af8aacSRandall Stewart 				/*
186308af8aacSRandall Stewart 				 * Clear t_acktime if remote side has ACKd
186408af8aacSRandall Stewart 				 * all data in the socket buffer.
186508af8aacSRandall Stewart 				 * Otherwise, update t_acktime if we received
186608af8aacSRandall Stewart 				 * a sufficiently large ACK.
186708af8aacSRandall Stewart 				 */
186808af8aacSRandall Stewart 				if (sbavail(&so->so_snd) == 0)
186908af8aacSRandall Stewart 					tp->t_acktime = 0;
187008af8aacSRandall Stewart 				else if (acked > 1)
187108af8aacSRandall Stewart 					tp->t_acktime = ticks;
1872df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
1873b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
1874b8152ba7SAndre Oppermann 				else if (!tcp_timer_active(tp, TT_PERSIST))
1875b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT,
187608af8aacSRandall Stewart 					    TP_RXTCUR(tp));
1877032bf749SRichard Scheffenegger 				sowwakeup(so);
1878f4574e2dSRichard Scheffenegger 				/*
1879f4574e2dSRichard Scheffenegger 				 * Only call tcp_output when there
1880f4574e2dSRichard Scheffenegger 				 * is new data available to be sent
1881f4574e2dSRichard Scheffenegger 				 * or we need to send an ACK.
1882f4574e2dSRichard Scheffenegger 				 */
1883*2d05a1c8SRichard Scheffenegger 				if ((tp->t_flags & TF_ACKNOW) ||
1884*2d05a1c8SRichard Scheffenegger 				    (sbavail(&so->so_snd) >=
1885*2d05a1c8SRichard Scheffenegger 				     SEQ_SUB(tp->snd_max, tp->snd_una))) {
188640fa3e40SGleb Smirnoff 					(void) tcp_output(tp);
1887*2d05a1c8SRichard Scheffenegger 				}
1888a14c749fSJonathan Lemon 				goto check_delack;
1889df8bae1dSRodney W. Grimes 			}
1890fb59c426SYoshinobu Inoue 		} else if (th->th_ack == tp->snd_una &&
1891fb59c426SYoshinobu Inoue 		    tlen <= sbspace(&so->so_rcv)) {
18926741ecf5SAndre Oppermann 			int newsize = 0;	/* automatic sockbuf scaling */
18936741ecf5SAndre Oppermann 
1894df8bae1dSRodney W. Grimes 			/*
1895252ca428SRobert Watson 			 * This is a pure, in-sequence data packet with
1896252ca428SRobert Watson 			 * nothing on the reassembly queue and we have enough
1897252ca428SRobert Watson 			 * buffer space to take it.
1898df8bae1dSRodney W. Grimes 			 */
18996d90faf3SPaul Saab 			/* Clean receiver SACK report if present */
19003529149eSAndre Oppermann 			if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks)
19016d90faf3SPaul Saab 				tcp_clean_sackreport(tp);
190278b50714SRobert Watson 			TCPSTAT_INC(tcps_preddat);
1903fb59c426SYoshinobu Inoue 			tp->rcv_nxt += tlen;
1904e854dd38SRandall Stewart 			if (tlen &&
1905e854dd38SRandall Stewart 			    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1906e854dd38SRandall Stewart 			    (tp->t_fbyte_in == 0)) {
1907e854dd38SRandall Stewart 				tp->t_fbyte_in = ticks;
1908e854dd38SRandall Stewart 				if (tp->t_fbyte_in == 0)
1909e854dd38SRandall Stewart 					tp->t_fbyte_in = 1;
1910e854dd38SRandall Stewart 				if (tp->t_fbyte_out && tp->t_fbyte_in)
1911e854dd38SRandall Stewart 					tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1912e854dd38SRandall Stewart 			}
19131645d090SJeff Roberson 			/*
19141645d090SJeff Roberson 			 * Pull snd_wl1 up to prevent seq wrap relative to
19151645d090SJeff Roberson 			 * th_seq.
19161645d090SJeff Roberson 			 */
191760ee3bb2SAndre Oppermann 			tp->snd_wl1 = th->th_seq;
19181645d090SJeff Roberson 			/*
19191645d090SJeff Roberson 			 * Pull rcv_up up to prevent seq wrap relative to
19201645d090SJeff Roberson 			 * rcv_nxt.
19211645d090SJeff Roberson 			 */
19221645d090SJeff Roberson 			tp->rcv_up = tp->rcv_nxt;
19234b7b743cSLawrence Stewart 			TCPSTAT_ADD(tcps_rcvpack, nsegs);
192478b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
19252b9c9984SGeorge V. Neville-Neil 			TCP_PROBE3(debug__input, tp, th, m);
19265d06879aSGeorge V. Neville-Neil 
1927e44c1887SSteven Hartland 			newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
19286741ecf5SAndre Oppermann 
19296741ecf5SAndre Oppermann 			/* Add data to socket buffer. */
19301e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
1931c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1932c1c36a2cSMike Silbersack 				m_freem(m);
1933c1c36a2cSMike Silbersack 			} else {
19346741ecf5SAndre Oppermann 				/*
19356741ecf5SAndre Oppermann 				 * Set new socket buffer size.
19366741ecf5SAndre Oppermann 				 * Give up when limit is reached.
19376741ecf5SAndre Oppermann 				 */
19386741ecf5SAndre Oppermann 				if (newsize)
193943283184SGleb Smirnoff 					if (!sbreserve_locked(so, SO_RCV,
194043283184SGleb Smirnoff 					    newsize, NULL))
19416741ecf5SAndre Oppermann 						so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
1942fb59c426SYoshinobu Inoue 				m_adj(m, drop_hdrlen);	/* delayed header drop */
1943651e4e6aSGleb Smirnoff 				sbappendstream_locked(&so->so_rcv, m, 0);
1944c1c36a2cSMike Silbersack 			}
1945032bf749SRichard Scheffenegger 			/* NB: sorwakeup_locked() does an implicit unlock. */
1946032bf749SRichard Scheffenegger 			sorwakeup_locked(so);
1947c1e5a6e5SAndre Oppermann 			if (DELAY_ACK(tp, tlen)) {
19483bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
1949f498eeeeSDavid Greenman 			} else {
1950e612a582SDavid Greenman 				tp->t_flags |= TF_ACKNOW;
195140fa3e40SGleb Smirnoff 				tcp_output(tp);
1952e612a582SDavid Greenman 			}
1953a14c749fSJonathan Lemon 			goto check_delack;
1954df8bae1dSRodney W. Grimes 		}
1955df8bae1dSRodney W. Grimes 	}
1956df8bae1dSRodney W. Grimes 
1957df8bae1dSRodney W. Grimes 	/*
1958df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
1959df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
1960df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
1961df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
1962df8bae1dSRodney W. Grimes 	 */
1963df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
1964df8bae1dSRodney W. Grimes 	if (win < 0)
1965df8bae1dSRodney W. Grimes 		win = 0;
196666e39adcSJohn Polstra 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1967df8bae1dSRodney W. Grimes 
1968df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1969df8bae1dSRodney W. Grimes 	/*
1970764d8cefSBill Fenner 	 * If the state is SYN_RECEIVED:
1971764d8cefSBill Fenner 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
1972764d8cefSBill Fenner 	 */
1973764d8cefSBill Fenner 	case TCPS_SYN_RECEIVED:
1974b352ef58SRichard Scheffenegger 		if (thflags & TH_RST) {
1975b352ef58SRichard Scheffenegger 			/* Handle RST segments later. */
1976b352ef58SRichard Scheffenegger 			break;
1977b352ef58SRichard Scheffenegger 		}
1978fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1979fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1980a57815efSBosko Milekic 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1981a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
1982d1b07f36SRandall Stewart 				tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
1983a57815efSBosko Milekic 				goto dropwithreset;
1984a57815efSBosko Milekic 		}
198568bd7ed1SJonathan T. Looney 		if (IS_FASTOPEN(tp->t_flags)) {
1986281a0fd4SPatrick Kelsey 			/*
1987281a0fd4SPatrick Kelsey 			 * When a TFO connection is in SYN_RECEIVED, the
1988281a0fd4SPatrick Kelsey 			 * only valid packets are the initial SYN, a
1989281a0fd4SPatrick Kelsey 			 * retransmit/copy of the initial SYN (possibly with
1990281a0fd4SPatrick Kelsey 			 * a subset of the original data), a valid ACK, a
1991281a0fd4SPatrick Kelsey 			 * FIN, or a RST.
1992281a0fd4SPatrick Kelsey 			 */
1993281a0fd4SPatrick Kelsey 			if ((thflags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) {
1994281a0fd4SPatrick Kelsey 				rstreason = BANDLIM_RST_OPENPORT;
1995d1b07f36SRandall Stewart 				tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
1996281a0fd4SPatrick Kelsey 				goto dropwithreset;
1997281a0fd4SPatrick Kelsey 			} else if (thflags & TH_SYN) {
1998281a0fd4SPatrick Kelsey 				/* non-initial SYN is ignored */
1999281a0fd4SPatrick Kelsey 				if ((tcp_timer_active(tp, TT_DELACK) ||
2000281a0fd4SPatrick Kelsey 				     tcp_timer_active(tp, TT_REXMT)))
2001281a0fd4SPatrick Kelsey 					goto drop;
2002281a0fd4SPatrick Kelsey 			} else if (!(thflags & (TH_ACK|TH_FIN|TH_RST))) {
2003281a0fd4SPatrick Kelsey 				goto drop;
2004281a0fd4SPatrick Kelsey 			}
2005281a0fd4SPatrick Kelsey 		}
2006764d8cefSBill Fenner 		break;
2007764d8cefSBill Fenner 
2008764d8cefSBill Fenner 	/*
2009df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
201098732609SMichael Tuexen 	 *	if seg contains a RST with valid ACK (SEQ.ACK has already
201198732609SMichael Tuexen 	 *	    been verified), then drop the connection.
201298732609SMichael Tuexen 	 *	if seg contains a RST without an ACK, drop the seg.
201398732609SMichael Tuexen 	 *	if seg does not contain SYN, then drop the seg.
2014df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
2015df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
2016df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
2017f2512ba1SRui Paulo 	 *	if seg contains an ECE and ECN support is enabled, the stream
2018f2512ba1SRui Paulo 	 *	    is ECN capable.
2019df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
2020df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
2021df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
2022df8bae1dSRodney W. Grimes 	 */
2023df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
202457f60867SMark Johnston 		if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) {
2025d9fae5abSAndriy Gapon 			TCP_PROBE5(connect__refused, NULL, tp,
20262b9c9984SGeorge V. Neville-Neil 			    m, tp, th);
2027d1b07f36SRandall Stewart 			tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
2028df8bae1dSRodney W. Grimes 			tp = tcp_drop(tp, ECONNREFUSED);
202957f60867SMark Johnston 		}
20300ca3f933SAndre Oppermann 		if (thflags & TH_RST)
2031df8bae1dSRodney W. Grimes 			goto drop;
20320ca3f933SAndre Oppermann 		if (!(thflags & TH_SYN))
2033df8bae1dSRodney W. Grimes 			goto drop;
2034464fcfbcSAndre Oppermann 
2035fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
2036df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
2037fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
2038c560df6fSPatrick Kelsey 			int tfo_partial_ack = 0;
2039c560df6fSPatrick Kelsey 
204078b50714SRobert Watson 			TCPSTAT_INC(tcps_connects);
2041845799c1SAndras Olah 			soisconnected(so);
2042c488362eSRobert Watson #ifdef MAC
204330d239bcSRobert Watson 			mac_socketpeer_set_from_mbuf(m, so);
2044c488362eSRobert Watson #endif
2045845799c1SAndras Olah 			/* Do window scaling on this connection? */
2046845799c1SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2047845799c1SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
2048845799c1SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2049845799c1SAndras Olah 			}
20503ac12506SJonathan T. Looney 			tp->rcv_adv += min(tp->rcv_wnd,
2051766282cbSJohn Baldwin 			    TCP_MAXWIN << tp->rcv_scale);
2052a0292f23SGarrett Wollman 			tp->snd_una++;		/* SYN is acked */
2053f4574e2dSRichard Scheffenegger 			if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2054f4574e2dSRichard Scheffenegger 				tp->snd_nxt = tp->snd_una;
2055a0292f23SGarrett Wollman 			/*
2056c560df6fSPatrick Kelsey 			 * If not all the data that was sent in the TFO SYN
2057c560df6fSPatrick Kelsey 			 * has been acked, resend the remainder right away.
2058c560df6fSPatrick Kelsey 			 */
2059c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags) &&
2060c560df6fSPatrick Kelsey 			    (tp->snd_una != tp->snd_max)) {
2061c560df6fSPatrick Kelsey 				tp->snd_nxt = th->th_ack;
2062c560df6fSPatrick Kelsey 				tfo_partial_ack = 1;
2063c560df6fSPatrick Kelsey 			}
2064c560df6fSPatrick Kelsey 			/*
2065a0292f23SGarrett Wollman 			 * If there's data, delay ACK; if there's also a FIN
2066a0292f23SGarrett Wollman 			 * ACKNOW will be turned on later.
2067a0292f23SGarrett Wollman 			 */
2068c560df6fSPatrick Kelsey 			if (DELAY_ACK(tp, tlen) && tlen != 0 && !tfo_partial_ack)
2069b8152ba7SAndre Oppermann 				tcp_timer_activate(tp, TT_DELACK,
2070b8152ba7SAndre Oppermann 				    tcp_delacktime);
2071a0292f23SGarrett Wollman 			else
2072a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
2073f2512ba1SRui Paulo 
2074f7220c48SRichard Scheffenegger 			tcp_ecn_input_syn_sent(tp, thflags, iptos);
2075f2512ba1SRui Paulo 
2076a0292f23SGarrett Wollman 			/*
2077a0292f23SGarrett Wollman 			 * Received <SYN,ACK> in SYN_SENT[*] state.
2078a0292f23SGarrett Wollman 			 * Transitions:
2079a0292f23SGarrett Wollman 			 *	SYN_SENT  --> ESTABLISHED
2080a0292f23SGarrett Wollman 			 *	SYN_SENT* --> FIN_WAIT_1
2081a0292f23SGarrett Wollman 			 */
20829b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
2083a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDFIN) {
208408af8aacSRandall Stewart 				tp->t_acktime = ticks;
208557f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_1);
2086a0292f23SGarrett Wollman 				tp->t_flags &= ~TF_NEEDFIN;
2087fb59c426SYoshinobu Inoue 				thflags &= ~TH_SYN;
20887ff19458SPaul Traina 			} else {
208957f60867SMark Johnston 				tcp_state_change(tp, TCPS_ESTABLISHED);
2090d9fae5abSAndriy Gapon 				TCP_PROBE5(connect__established, NULL, tp,
20912b9c9984SGeorge V. Neville-Neil 				    m, tp, th);
2092dbc42409SLawrence Stewart 				cc_conn_init(tp);
20939077f387SGleb Smirnoff 				tcp_timer_activate(tp, TT_KEEP,
20949077f387SGleb Smirnoff 				    TP_KEEPIDLE(tp));
20957ff19458SPaul Traina 			}
2096a0292f23SGarrett Wollman 		} else {
2097a0292f23SGarrett Wollman 			/*
2098c068736aSJeffrey Hsu 			 * Received initial SYN in SYN-SENT[*] state =>
2099153edc50SHiren Panchasara 			 * simultaneous open.
2100c068736aSJeffrey Hsu 			 * If it succeeds, connection is * half-synchronized.
2101c068736aSJeffrey Hsu 			 * Otherwise, do 3-way handshake:
2102a0292f23SGarrett Wollman 			 *        SYN-SENT -> SYN-RECEIVED
2103a0292f23SGarrett Wollman 			 *        SYN-SENT* -> SYN-RECEIVED*
2104a0292f23SGarrett Wollman 			 */
2105493105c2SGleb Smirnoff 			tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN | TF_SONOTCONN);
2106b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
210757f60867SMark Johnston 			tcp_state_change(tp, TCPS_SYN_RECEIVED);
2108a0292f23SGarrett Wollman 		}
2109df8bae1dSRodney W. Grimes 
2110df8bae1dSRodney W. Grimes 		/*
2111fb59c426SYoshinobu Inoue 		 * Advance th->th_seq to correspond to first data byte.
2112df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
2113df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
2114df8bae1dSRodney W. Grimes 		 */
2115fb59c426SYoshinobu Inoue 		th->th_seq++;
2116fb59c426SYoshinobu Inoue 		if (tlen > tp->rcv_wnd) {
2117fb59c426SYoshinobu Inoue 			todrop = tlen - tp->rcv_wnd;
2118df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
2119fb59c426SYoshinobu Inoue 			tlen = tp->rcv_wnd;
2120fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
212178b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpackafterwin);
212278b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2123df8bae1dSRodney W. Grimes 		}
2124fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
2125fb59c426SYoshinobu Inoue 		tp->rcv_up = th->th_seq;
2126a0292f23SGarrett Wollman 		/*
2127a0292f23SGarrett Wollman 		 * Client side of transaction: already sent SYN and data.
2128a0292f23SGarrett Wollman 		 * If the remote host used T/TCP to validate the SYN,
2129a0292f23SGarrett Wollman 		 * our data will be ACK'd; if so, enter normal data segment
2130a0292f23SGarrett Wollman 		 * processing in the middle of step 5, ack processing.
2131a0292f23SGarrett Wollman 		 * Otherwise, goto step 6.
2132a0292f23SGarrett Wollman 		 */
2133fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK)
2134a0292f23SGarrett Wollman 			goto process_ACK;
2135c068736aSJeffrey Hsu 
2136df8bae1dSRodney W. Grimes 		goto step6;
2137df8bae1dSRodney W. Grimes 	}
2138df8bae1dSRodney W. Grimes 
2139df8bae1dSRodney W. Grimes 	/*
2140df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
214180ab7c0eSGarrett Wollman 	 * First check the RST flag and sequence number since reset segments
214280ab7c0eSGarrett Wollman 	 * are exempt from the timestamp and connection count tests.  This
214380ab7c0eSGarrett Wollman 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
214480ab7c0eSGarrett Wollman 	 * below which allowed reset segments in half the sequence space
214580ab7c0eSGarrett Wollman 	 * to fall though and be processed (which gives forged reset
214680ab7c0eSGarrett Wollman 	 * segments with a random sequence number a 50 percent chance of
214780ab7c0eSGarrett Wollman 	 * killing a connection).
214880ab7c0eSGarrett Wollman 	 * Then check timestamp, if present.
2149a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
2150df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
2151df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
2152df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
215380ab7c0eSGarrett Wollman 	 */
2154fb59c426SYoshinobu Inoue 	if (thflags & TH_RST) {
21553220a212SGleb Smirnoff 		/*
21563220a212SGleb Smirnoff 		 * RFC5961 Section 3.2
21573220a212SGleb Smirnoff 		 *
21583220a212SGleb Smirnoff 		 * - RST drops connection only if SEG.SEQ == RCV.NXT.
21593220a212SGleb Smirnoff 		 * - If RST is in window, we send challenge ACK.
21603220a212SGleb Smirnoff 		 *
21613220a212SGleb Smirnoff 		 * Note: to take into account delayed ACKs, we should
21623220a212SGleb Smirnoff 		 *   test against last_ack_sent instead of rcv_nxt.
21633220a212SGleb Smirnoff 		 * Note 2: we handle special case of closed window, not
21643220a212SGleb Smirnoff 		 *   covered by the RFC.
21653220a212SGleb Smirnoff 		 */
21663220a212SGleb Smirnoff 		if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
21673220a212SGleb Smirnoff 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
21683220a212SGleb Smirnoff 		    (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) {
21693220a212SGleb Smirnoff 			KASSERT(tp->t_state != TCPS_SYN_SENT,
21703220a212SGleb Smirnoff 			    ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p",
21713220a212SGleb Smirnoff 			    __func__, th, tp));
21723220a212SGleb Smirnoff 
21733220a212SGleb Smirnoff 			if (V_tcp_insecure_rst ||
21743220a212SGleb Smirnoff 			    tp->last_ack_sent == th->th_seq) {
21753220a212SGleb Smirnoff 				TCPSTAT_INC(tcps_drops);
21763220a212SGleb Smirnoff 				/* Drop the connection. */
21773220a212SGleb Smirnoff 				switch (tp->t_state) {
217880ab7c0eSGarrett Wollman 				case TCPS_SYN_RECEIVED:
217980ab7c0eSGarrett Wollman 					so->so_error = ECONNREFUSED;
218080ab7c0eSGarrett Wollman 					goto close;
218180ab7c0eSGarrett Wollman 				case TCPS_ESTABLISHED:
218280ab7c0eSGarrett Wollman 				case TCPS_FIN_WAIT_1:
218380ab7c0eSGarrett Wollman 				case TCPS_FIN_WAIT_2:
218480ab7c0eSGarrett Wollman 				case TCPS_CLOSE_WAIT:
21856779a1a1SMichael Tuexen 				case TCPS_CLOSING:
21866779a1a1SMichael Tuexen 				case TCPS_LAST_ACK:
218780ab7c0eSGarrett Wollman 					so->so_error = ECONNRESET;
218880ab7c0eSGarrett Wollman 				close:
21893220a212SGleb Smirnoff 					/* FALLTHROUGH */
21903220a212SGleb Smirnoff 				default:
2191d1b07f36SRandall Stewart 					tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_RST);
219280ab7c0eSGarrett Wollman 					tp = tcp_close(tp);
21933220a212SGleb Smirnoff 				}
21943220a212SGleb Smirnoff 			} else {
21953220a212SGleb Smirnoff 				TCPSTAT_INC(tcps_badrst);
21963220a212SGleb Smirnoff 				/* Send challenge ACK. */
21973220a212SGleb Smirnoff 				tcp_respond(tp, mtod(m, void *), th, m,
21983220a212SGleb Smirnoff 				    tp->rcv_nxt, tp->snd_nxt, TH_ACK);
21993220a212SGleb Smirnoff 				tp->last_ack_sent = tp->rcv_nxt;
22003220a212SGleb Smirnoff 				m = NULL;
22013220a212SGleb Smirnoff 			}
22023220a212SGleb Smirnoff 		}
22033220a212SGleb Smirnoff 		goto drop;
22043220a212SGleb Smirnoff 	}
220580ab7c0eSGarrett Wollman 
22063220a212SGleb Smirnoff 	/*
22073220a212SGleb Smirnoff 	 * RFC5961 Section 4.2
22083220a212SGleb Smirnoff 	 * Send challenge ACK for any SYN in synchronized state.
22093220a212SGleb Smirnoff 	 */
2210281a0fd4SPatrick Kelsey 	if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT &&
2211281a0fd4SPatrick Kelsey 	    tp->t_state != TCPS_SYN_RECEIVED) {
22123220a212SGleb Smirnoff 		TCPSTAT_INC(tcps_badsyn);
22133220a212SGleb Smirnoff 		if (V_tcp_insecure_syn &&
22143220a212SGleb Smirnoff 		    SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
22153220a212SGleb Smirnoff 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
2216d1b07f36SRandall Stewart 			tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
22173220a212SGleb Smirnoff 			tp = tcp_drop(tp, ECONNRESET);
22183220a212SGleb Smirnoff 			rstreason = BANDLIM_UNLIMITED;
22193220a212SGleb Smirnoff 		} else {
222083c1ec92SRichard Scheffenegger 			tcp_ecn_input_syn_sent(tp, thflags, iptos);
22213220a212SGleb Smirnoff 			/* Send challenge ACK. */
22223220a212SGleb Smirnoff 			tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt,
22233220a212SGleb Smirnoff 			    tp->snd_nxt, TH_ACK);
22243220a212SGleb Smirnoff 			tp->last_ack_sent = tp->rcv_nxt;
22253220a212SGleb Smirnoff 			m = NULL;
222680ab7c0eSGarrett Wollman 		}
222780ab7c0eSGarrett Wollman 		goto drop;
222880ab7c0eSGarrett Wollman 	}
222980ab7c0eSGarrett Wollman 
223080ab7c0eSGarrett Wollman 	/*
2231df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
2232df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
2233df8bae1dSRodney W. Grimes 	 */
2234be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
223580ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
2236df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
2237d8951c8aSBjoern A. Zeeb 		if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) {
2238df8bae1dSRodney W. Grimes 			/*
2239df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
2240df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
2241df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
2242df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
2243df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
2244df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
2245df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
2246df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
2247df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
2248df8bae1dSRodney W. Grimes 			 */
2249df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
2250df8bae1dSRodney W. Grimes 		} else {
225178b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
225278b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, tlen);
225378b50714SRobert Watson 			TCPSTAT_INC(tcps_pawsdrop);
225407fd333dSMatthew Dillon 			if (tlen)
2255df8bae1dSRodney W. Grimes 				goto dropafterack;
22561ab4789dSMatthew Dillon 			goto drop;
22571ab4789dSMatthew Dillon 		}
2258df8bae1dSRodney W. Grimes 	}
2259df8bae1dSRodney W. Grimes 
2260a0292f23SGarrett Wollman 	/*
226180ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
226280ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
226380ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
226480ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
226580ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
226680ab7c0eSGarrett Wollman 	 */
2267a57815efSBosko Milekic 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
2268a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2269d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
2270a57815efSBosko Milekic 		goto dropwithreset;
2271a57815efSBosko Milekic 	}
227280ab7c0eSGarrett Wollman 
2273fb59c426SYoshinobu Inoue 	todrop = tp->rcv_nxt - th->th_seq;
2274df8bae1dSRodney W. Grimes 	if (todrop > 0) {
2275831ad37eSXin LI 		if (thflags & TH_SYN) {
2276fb59c426SYoshinobu Inoue 			thflags &= ~TH_SYN;
2277fb59c426SYoshinobu Inoue 			th->th_seq++;
2278fb59c426SYoshinobu Inoue 			if (th->th_urp > 1)
2279fb59c426SYoshinobu Inoue 				th->th_urp--;
2280df8bae1dSRodney W. Grimes 			else
2281fb59c426SYoshinobu Inoue 				thflags &= ~TH_URG;
2282df8bae1dSRodney W. Grimes 			todrop--;
2283df8bae1dSRodney W. Grimes 		}
2284df8bae1dSRodney W. Grimes 		/*
2285dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
2286df8bae1dSRodney W. Grimes 		 */
2287fb59c426SYoshinobu Inoue 		if (todrop > tlen
2288fb59c426SYoshinobu Inoue 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
2289dac20301SGarrett Wollman 			/*
2290dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
2291dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
2292dac20301SGarrett Wollman 			 * of sequence; drop it.
2293dac20301SGarrett Wollman 			 */
2294fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
2295dac20301SGarrett Wollman 
2296df8bae1dSRodney W. Grimes 			/*
2297dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
2298dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
2299df8bae1dSRodney W. Grimes 			 */
2300dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
2301fb59c426SYoshinobu Inoue 			todrop = tlen;
230278b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
230378b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, todrop);
2304df8bae1dSRodney W. Grimes 		} else {
230578b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpartduppack);
230678b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop);
2307df8bae1dSRodney W. Grimes 		}
23085acfd95cSMichael Tuexen 		/*
23095acfd95cSMichael Tuexen 		 * DSACK - add SACK block for dropped range
23105acfd95cSMichael Tuexen 		 */
23118f63a52bSMichael Tuexen 		if ((todrop > 0) && (tp->t_flags & TF_SACK_PERMIT)) {
2312ecc5b1d1SMichael Tuexen 			tcp_update_sack_list(tp, th->th_seq,
2313ecc5b1d1SMichael Tuexen 			    th->th_seq + todrop);
23145acfd95cSMichael Tuexen 			/*
23155acfd95cSMichael Tuexen 			 * ACK now, as the next in-sequence segment
23165acfd95cSMichael Tuexen 			 * will clear the DSACK block again
23175acfd95cSMichael Tuexen 			 */
23185acfd95cSMichael Tuexen 			tp->t_flags |= TF_ACKNOW;
23195acfd95cSMichael Tuexen 		}
2320fb59c426SYoshinobu Inoue 		drop_hdrlen += todrop;	/* drop from the top afterwards */
2321fb59c426SYoshinobu Inoue 		th->th_seq += todrop;
2322fb59c426SYoshinobu Inoue 		tlen -= todrop;
2323fb59c426SYoshinobu Inoue 		if (th->th_urp > todrop)
2324fb59c426SYoshinobu Inoue 			th->th_urp -= todrop;
2325df8bae1dSRodney W. Grimes 		else {
2326fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;
2327fb59c426SYoshinobu Inoue 			th->th_urp = 0;
2328df8bae1dSRodney W. Grimes 		}
2329df8bae1dSRodney W. Grimes 	}
2330df8bae1dSRodney W. Grimes 
2331df8bae1dSRodney W. Grimes 	/*
2332df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
2333df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
2334df8bae1dSRodney W. Grimes 	 */
233574703901SGleb Smirnoff 	if ((tp->t_flags & TF_CLOSED) && tlen) {
233607dacf03SAndre Oppermann 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
233707dacf03SAndre Oppermann 			log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data "
233807dacf03SAndre Oppermann 			    "after socket was closed, "
233907dacf03SAndre Oppermann 			    "sending RST and removing tcpcb\n",
2340e31d8aa3SMike Silbersack 			    s, __func__, tcpstates[tp->t_state], tlen);
2341773673c1SAndre Oppermann 			free(s, M_TCPLOG);
2342773673c1SAndre Oppermann 		}
2343d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
2344d1b07f36SRandall Stewart 		/* tcp_close will kill the inp pre-log the Reset */
2345d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
2346df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
234778b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvafterclose);
2348a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
2349df8bae1dSRodney W. Grimes 		goto dropwithreset;
2350df8bae1dSRodney W. Grimes 	}
2351df8bae1dSRodney W. Grimes 
2352df8bae1dSRodney W. Grimes 	/*
2353df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
2354df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
2355df8bae1dSRodney W. Grimes 	 */
2356fb59c426SYoshinobu Inoue 	todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
2357df8bae1dSRodney W. Grimes 	if (todrop > 0) {
235878b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvpackafterwin);
2359fb59c426SYoshinobu Inoue 		if (todrop >= tlen) {
236078b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen);
2361df8bae1dSRodney W. Grimes 			/*
2362df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
2363df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
2364df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
2365df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
2366df8bae1dSRodney W. Grimes 			 * and ack.
2367df8bae1dSRodney W. Grimes 			 */
2368fb59c426SYoshinobu Inoue 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
2369df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
237078b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvwinprobe);
2371df8bae1dSRodney W. Grimes 			} else
2372df8bae1dSRodney W. Grimes 				goto dropafterack;
2373df8bae1dSRodney W. Grimes 		} else
237478b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2375df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
2376fb59c426SYoshinobu Inoue 		tlen -= todrop;
2377fb59c426SYoshinobu Inoue 		thflags &= ~(TH_PUSH|TH_FIN);
2378df8bae1dSRodney W. Grimes 	}
2379df8bae1dSRodney W. Grimes 
2380df8bae1dSRodney W. Grimes 	/*
2381df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
2382df8bae1dSRodney W. Grimes 	 * record its timestamp.
2383cf09195bSPaul Saab 	 * NOTE:
2384cf09195bSPaul Saab 	 * 1) That the test incorporates suggestions from the latest
2385a0292f23SGarrett Wollman 	 *    proposal of the tcplw@cray.com list (Braden 1993/04/26).
2386cf09195bSPaul Saab 	 * 2) That updating only on newer timestamps interferes with
2387cf09195bSPaul Saab 	 *    our earlier PAWS tests, so this check should be solely
2388cf09195bSPaul Saab 	 *    predicated on the sequence space of this segment.
2389cf09195bSPaul Saab 	 * 3) That we modify the segment boundary check to be
2390cf09195bSPaul Saab 	 *        Last.ACK.Sent <= SEG.SEQ + SEG.Len
2391cf09195bSPaul Saab 	 *    instead of RFC1323's
2392cf09195bSPaul Saab 	 *        Last.ACK.Sent < SEG.SEQ + SEG.Len,
2393cf09195bSPaul Saab 	 *    This modified check allows us to overcome RFC1323's
2394cf09195bSPaul Saab 	 *    limitations as described in Stevens TCP/IP Illustrated
2395cf09195bSPaul Saab 	 *    Vol. 2 p.869. In such cases, we can still calculate the
2396cf09195bSPaul Saab 	 *    RTT correctly when RCV.NXT == Last.ACK.Sent.
2397df8bae1dSRodney W. Grimes 	 */
2398be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 &&
2399cf09195bSPaul Saab 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
2400cf09195bSPaul Saab 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
2401cf09195bSPaul Saab 		((thflags & (TH_SYN|TH_FIN)) != 0))) {
2402d8951c8aSBjoern A. Zeeb 		tp->ts_recent_age = tcp_ts_getticks();
2403a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
2404df8bae1dSRodney W. Grimes 	}
2405df8bae1dSRodney W. Grimes 
2406df8bae1dSRodney W. Grimes 	/*
2407a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
2408a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
2409a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
2410a0292f23SGarrett Wollman 	 */
2411fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) == 0) {
2412a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
2413281a0fd4SPatrick Kelsey 		    (tp->t_flags & TF_NEEDSYN)) {
2414281a0fd4SPatrick Kelsey 			if (tp->t_state == TCPS_SYN_RECEIVED &&
241568bd7ed1SJonathan T. Looney 			    IS_FASTOPEN(tp->t_flags)) {
2416281a0fd4SPatrick Kelsey 				tp->snd_wnd = tiwin;
2417281a0fd4SPatrick Kelsey 				cc_conn_init(tp);
2418281a0fd4SPatrick Kelsey 			}
2419a0292f23SGarrett Wollman 			goto step6;
2420281a0fd4SPatrick Kelsey 		} else if (tp->t_flags & TF_ACKNOW)
24215e1aa279SDavid Malone 			goto dropafterack;
2422a0292f23SGarrett Wollman 		else
2423a0292f23SGarrett Wollman 			goto drop;
2424a0292f23SGarrett Wollman 	}
2425df8bae1dSRodney W. Grimes 
2426df8bae1dSRodney W. Grimes 	/*
2427df8bae1dSRodney W. Grimes 	 * Ack processing.
2428df8bae1dSRodney W. Grimes 	 */
2429df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2430df8bae1dSRodney W. Grimes 	/*
2431764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
2432764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
2433764d8cefSBill Fenner 	 * The ACK was checked above.
2434df8bae1dSRodney W. Grimes 	 */
2435df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2436a0292f23SGarrett Wollman 
243778b50714SRobert Watson 		TCPSTAT_INC(tcps_connects);
2438493105c2SGleb Smirnoff 		if (tp->t_flags & TF_SONOTCONN) {
2439493105c2SGleb Smirnoff 			/*
2440493105c2SGleb Smirnoff 			 * Usually SYN_RECEIVED had been created from a LISTEN,
2441493105c2SGleb Smirnoff 			 * and solisten_enqueue() has already marked the socket
2442493105c2SGleb Smirnoff 			 * layer as connected.  If it didn't, which can happen
2443493105c2SGleb Smirnoff 			 * only with an accept_filter(9), then the tp is marked
2444493105c2SGleb Smirnoff 			 * with TF_SONOTCONN.  The other reason for this mark
2445493105c2SGleb Smirnoff 			 * to be set is a simultaneous open, a SYN_RECEIVED
2446493105c2SGleb Smirnoff 			 * that had been created from SYN_SENT.
2447493105c2SGleb Smirnoff 			 */
2448493105c2SGleb Smirnoff 			tp->t_flags &= ~TF_SONOTCONN;
2449df8bae1dSRodney W. Grimes 			soisconnected(so);
2450e80062a2SGleb Smirnoff 		}
2451df8bae1dSRodney W. Grimes 		/* Do window scaling? */
2452df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2453df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
2454df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
2455df8bae1dSRodney W. Grimes 		}
2456116ef4d6SMichael Tuexen 		tp->snd_wnd = tiwin;
2457a0292f23SGarrett Wollman 		/*
2458a0292f23SGarrett Wollman 		 * Make transitions:
2459a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
2460a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
2461a0292f23SGarrett Wollman 		 */
24629b8b58e0SJonathan Lemon 		tp->t_starttime = ticks;
246318a75309SPatrick Kelsey 		if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) {
2464281a0fd4SPatrick Kelsey 			tcp_fastopen_decrement_counter(tp->t_tfo_pending);
2465281a0fd4SPatrick Kelsey 			tp->t_tfo_pending = NULL;
2466281a0fd4SPatrick Kelsey 		}
24678db239dcSMichael Tuexen 		if (tp->t_flags & TF_NEEDFIN) {
246808af8aacSRandall Stewart 			tp->t_acktime = ticks;
24698db239dcSMichael Tuexen 			tcp_state_change(tp, TCPS_FIN_WAIT_1);
24708db239dcSMichael Tuexen 			tp->t_flags &= ~TF_NEEDFIN;
24718db239dcSMichael Tuexen 		} else {
24728db239dcSMichael Tuexen 			tcp_state_change(tp, TCPS_ESTABLISHED);
24738db239dcSMichael Tuexen 			TCP_PROBE5(accept__established, NULL, tp,
24748db239dcSMichael Tuexen 			    m, tp, th);
2475281a0fd4SPatrick Kelsey 			/*
2476281a0fd4SPatrick Kelsey 			 * TFO connections call cc_conn_init() during SYN
2477281a0fd4SPatrick Kelsey 			 * processing.  Calling it again here for such
2478281a0fd4SPatrick Kelsey 			 * connections is not harmless as it would undo the
2479281a0fd4SPatrick Kelsey 			 * snd_cwnd reduction that occurs when a TFO SYN|ACK
2480281a0fd4SPatrick Kelsey 			 * is retransmitted.
2481281a0fd4SPatrick Kelsey 			 */
248268bd7ed1SJonathan T. Looney 			if (!IS_FASTOPEN(tp->t_flags))
2483dbc42409SLawrence Stewart 				cc_conn_init(tp);
24849077f387SGleb Smirnoff 			tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp));
24857ff19458SPaul Traina 		}
2486a0292f23SGarrett Wollman 		/*
2487b2ade6b1SRichard Scheffenegger 		 * Account for the ACK of our SYN prior to
2488b2ade6b1SRichard Scheffenegger 		 * regular ACK processing below, except for
2489b2ade6b1SRichard Scheffenegger 		 * simultaneous SYN, which is handled later.
2490b2ade6b1SRichard Scheffenegger 		 */
2491b2ade6b1SRichard Scheffenegger 		if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN))
2492b2ade6b1SRichard Scheffenegger 			incforsyn = 1;
2493b2ade6b1SRichard Scheffenegger 		/*
2494a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
2495a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
2496a0292f23SGarrett Wollman 		 */
2497032bf749SRichard Scheffenegger 		if (tlen == 0 && (thflags & TH_FIN) == 0) {
2498c28440dbSRandall Stewart 			(void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0,
2499a0292f23SGarrett Wollman 			    (struct mbuf *)0);
2500c348e880SGleb Smirnoff 			tcp_handle_wakeup(tp);
2501032bf749SRichard Scheffenegger 		}
250260ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq - 1;
250393b0017fSPhilippe Charnier 		/* FALLTHROUGH */
2504df8bae1dSRodney W. Grimes 
2505df8bae1dSRodney W. Grimes 	/*
2506df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
2507df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
2508fb59c426SYoshinobu Inoue 	 *	tp->snd_una < th->th_ack <= tp->snd_max
2509fb59c426SYoshinobu Inoue 	 * then advance tp->snd_una to th->th_ack and drop
2510df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
2511df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
2512df8bae1dSRodney W. Grimes 	 */
2513df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
2514df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
2515df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
2516df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
2517df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
2518df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
25195a53ca16SPaul Saab 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
252078b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvacktoomuch);
25215a53ca16SPaul Saab 			goto dropafterack;
25225a53ca16SPaul Saab 		}
2523c21b7b55SRichard Scheffenegger 		if (tcp_is_sack_recovery(tp, &to)) {
2524893ed42eSRichard Scheffenegger 			sack_changed = tcp_sack_doack(tp, &to, th->th_ack);
2525893ed42eSRichard Scheffenegger 			if ((sack_changed != SACK_NOCHANGE) &&
25260471a8c7SRichard Scheffenegger 			    (tp->t_flags & TF_LRD)) {
25270471a8c7SRichard Scheffenegger 				tcp_sack_lost_retransmission(tp, th);
25280471a8c7SRichard Scheffenegger 			}
25290471a8c7SRichard Scheffenegger 		} else
253012eeb81fSHiren Panchasara 			/*
253112eeb81fSHiren Panchasara 			 * Reset the value so that previous (valid) value
253212eeb81fSHiren Panchasara 			 * from the last ack with SACK doesn't get used.
253312eeb81fSHiren Panchasara 			 */
253412eeb81fSHiren Panchasara 			tp->sackhint.sacked_bytes = 0;
253539bc9de5SLawrence Stewart 
2536bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
253739bc9de5SLawrence Stewart 		/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
253839bc9de5SLawrence Stewart 		hhook_run_tcp_est_in(tp, th, &to);
2539bd79708dSJonathan T. Looney #endif
254039bc9de5SLawrence Stewart 
2541fb59c426SYoshinobu Inoue 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
25420c39d38dSGleb Smirnoff 			maxseg = tcp_maxseg(tp);
2543021eaf79SHiren Panchasara 			if (tlen == 0 &&
2544021eaf79SHiren Panchasara 			    (tiwin == tp->snd_wnd ||
2545021eaf79SHiren Panchasara 			    (tp->t_flags & TF_SACK_PERMIT))) {
254662b90589SPeter Wemm 				/*
254762b90589SPeter Wemm 				 * If this is the first time we've seen a
254862b90589SPeter Wemm 				 * FIN from the remote, this is not a
254962b90589SPeter Wemm 				 * duplicate and it needs to be processed
255062b90589SPeter Wemm 				 * normally.  This happens during a
255162b90589SPeter Wemm 				 * simultaneous close.
255262b90589SPeter Wemm 				 */
255362b90589SPeter Wemm 				if ((thflags & TH_FIN) &&
255462b90589SPeter Wemm 				    (TCPS_HAVERCVDFIN(tp->t_state) == 0)) {
255562b90589SPeter Wemm 					tp->t_dupacks = 0;
255662b90589SPeter Wemm 					break;
255762b90589SPeter Wemm 				}
255878b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvdupack);
2559df8bae1dSRodney W. Grimes 				/*
2560df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
2561df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
2562df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
25635f30ec9bSEitan Adler 				 * change and FIN isn't set),
25645f30ec9bSEitan Adler 				 * the ack is the biggest we've
2565df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
2566a4641f4eSPedro F. Giffuni 				 * threshold of them, assume a packet
2567df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
2568df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
2569df8bae1dSRodney W. Grimes 				 * window so we send only this one
2570df8bae1dSRodney W. Grimes 				 * packet.
2571df8bae1dSRodney W. Grimes 				 *
2572df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
2573df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
2574df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
2575df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
2576df8bae1dSRodney W. Grimes 				 * the new ssthresh).
2577df8bae1dSRodney W. Grimes 				 *
2578df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
2579df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
2580df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
2581df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
2582df8bae1dSRodney W. Grimes 				 * network.
2583f2512ba1SRui Paulo 				 *
2584f2512ba1SRui Paulo 				 * When using TCP ECN, notify the peer that
2585f2512ba1SRui Paulo 				 * we reduced the cwnd.
2586df8bae1dSRodney W. Grimes 				 */
2587021eaf79SHiren Panchasara 				/*
2588021eaf79SHiren Panchasara 				 * Following 2 kinds of acks should not affect
2589021eaf79SHiren Panchasara 				 * dupack counting:
2590021eaf79SHiren Panchasara 				 * 1) Old acks
2591021eaf79SHiren Panchasara 				 * 2) Acks with SACK but without any new SACK
2592021eaf79SHiren Panchasara 				 * information in them. These could result from
2593021eaf79SHiren Panchasara 				 * any anomaly in the network like a switch
2594021eaf79SHiren Panchasara 				 * duplicating packets or a possible DoS attack.
2595021eaf79SHiren Panchasara 				 */
2596021eaf79SHiren Panchasara 				if (th->th_ack != tp->snd_una ||
2597c21b7b55SRichard Scheffenegger 				    (tcp_is_sack_recovery(tp, &to) &&
2598*2d05a1c8SRichard Scheffenegger 				    (sack_changed == SACK_NOCHANGE))) {
2599021eaf79SHiren Panchasara 					break;
2600*2d05a1c8SRichard Scheffenegger 				} else if (!tcp_timer_active(tp, TT_REXMT)) {
2601df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
2602*2d05a1c8SRichard Scheffenegger 				} else if (++tp->t_dupacks > tcprexmtthresh ||
2603dbc42409SLawrence Stewart 					    IN_FASTRECOVERY(tp->t_flags)) {
26044b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
26054b7b743cSLawrence Stewart 					    CC_DUPACK);
26060e1d7c25SRichard Scheffenegger 					if (V_tcp_do_prr &&
260749a6fbe3SRichard Scheffenegger 					    IN_FASTRECOVERY(tp->t_flags) &&
260849a6fbe3SRichard Scheffenegger 					    (tp->t_flags & TF_SACK_PERMIT)) {
2609c7c325d0SRichard Scheffenegger 						tcp_do_prr_ack(tp, th, &to,
2610c7c325d0SRichard Scheffenegger 						    sack_changed, &maxseg);
2611c21b7b55SRichard Scheffenegger 					} else if (tcp_is_sack_recovery(tp, &to) &&
2612dbc42409SLawrence Stewart 						    IN_FASTRECOVERY(tp->t_flags)) {
2613808f11b7SPaul Saab 						int awnd;
261425e6f9edSPaul Saab 
261525e6f9edSPaul Saab 						/*
261625e6f9edSPaul Saab 						 * Compute the amount of data in flight first.
26174c3972f0SHiren Panchasara 						 * We can inject new data into the pipe iff
261825e6f9edSPaul Saab 						 * we have less than 1/2 the original window's
261925e6f9edSPaul Saab 						 * worth of data in flight.
262025e6f9edSPaul Saab 						 */
2621*2d05a1c8SRichard Scheffenegger 						if (V_tcp_do_newsack) {
262212eeb81fSHiren Panchasara 							awnd = tcp_compute_pipe(tp);
2623*2d05a1c8SRichard Scheffenegger 						} else {
2624808f11b7SPaul Saab 							awnd = (tp->snd_nxt - tp->snd_fack) +
26250077b016SPaul Saab 								tp->sackhint.sack_bytes_rexmit;
2626*2d05a1c8SRichard Scheffenegger 						}
2627808f11b7SPaul Saab 						if (awnd < tp->snd_ssthresh) {
26280c39d38dSGleb Smirnoff 							tp->snd_cwnd += maxseg;
262925e6f9edSPaul Saab 							if (tp->snd_cwnd > tp->snd_ssthresh)
263025e6f9edSPaul Saab 								tp->snd_cwnd = tp->snd_ssthresh;
263125e6f9edSPaul Saab 						}
2632*2d05a1c8SRichard Scheffenegger 					} else {
26330c39d38dSGleb Smirnoff 						tp->snd_cwnd += maxseg;
2634*2d05a1c8SRichard Scheffenegger 					}
263540fa3e40SGleb Smirnoff 					(void) tcp_output(tp);
263646f58482SJonathan Lemon 					goto drop;
26373c40e1d5SRichard Scheffenegger 				} else if (tp->t_dupacks == tcprexmtthresh ||
26383c40e1d5SRichard Scheffenegger 					    (tp->t_flags & TF_SACK_PERMIT &&
2639d1de2b05SRichard Scheffenegger 					     V_tcp_do_newsack &&
26403c40e1d5SRichard Scheffenegger 					     tp->sackhint.sacked_bytes >
26413c40e1d5SRichard Scheffenegger 					     (tcprexmtthresh - 1) * maxseg)) {
26423c40e1d5SRichard Scheffenegger enter_recovery:
26433c40e1d5SRichard Scheffenegger 					/*
26443c40e1d5SRichard Scheffenegger 					 * Above is the RFC6675 trigger condition of
26453c40e1d5SRichard Scheffenegger 					 * more than (dupthresh-1)*maxseg sacked data.
26463c40e1d5SRichard Scheffenegger 					 * If the count of holes in the
26473c40e1d5SRichard Scheffenegger 					 * scoreboard is >= dupthresh, we could
26483c40e1d5SRichard Scheffenegger 					 * also enter loss recovery, but don't
26493c40e1d5SRichard Scheffenegger 					 * have that value readily available.
26503c40e1d5SRichard Scheffenegger 					 */
26513c40e1d5SRichard Scheffenegger 					tp->t_dupacks = tcprexmtthresh;
2652cb942153SJeffrey Hsu 					tcp_seq onxt = tp->snd_nxt;
2653a0445c2eSJayanth Vijayaraghavan 
2654a0445c2eSJayanth Vijayaraghavan 					/*
26550e1d7c25SRichard Scheffenegger 					 * If we're doing sack, or prr, check
26560e1d7c25SRichard Scheffenegger 					 * to see if we're already in sack
2657a0445c2eSJayanth Vijayaraghavan 					 * recovery. If we're not doing sack,
2658a0445c2eSJayanth Vijayaraghavan 					 * check to see if we're in newreno
2659a0445c2eSJayanth Vijayaraghavan 					 * recovery.
2660a0445c2eSJayanth Vijayaraghavan 					 */
26610e1d7c25SRichard Scheffenegger 					if (V_tcp_do_prr ||
26620e1d7c25SRichard Scheffenegger 					    (tp->t_flags & TF_SACK_PERMIT)) {
2663dbc42409SLawrence Stewart 						if (IN_FASTRECOVERY(tp->t_flags)) {
2664a0445c2eSJayanth Vijayaraghavan 							tp->t_dupacks = 0;
2665a0445c2eSJayanth Vijayaraghavan 							break;
2666a0445c2eSJayanth Vijayaraghavan 						}
2667dbc42409SLawrence Stewart 					} else {
2668a0445c2eSJayanth Vijayaraghavan 						if (SEQ_LEQ(th->th_ack,
26699d11646dSJeffrey Hsu 						    tp->snd_recover)) {
2670cb942153SJeffrey Hsu 							tp->t_dupacks = 0;
2671cb942153SJeffrey Hsu 							break;
267246f58482SJonathan Lemon 						}
2673a0445c2eSJayanth Vijayaraghavan 					}
2674dbc42409SLawrence Stewart 					/* Congestion signal before ack. */
2675dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_NDUPACK);
26764b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
26774b7b743cSLawrence Stewart 					    CC_DUPACK);
2678b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
26799b8b58e0SJonathan Lemon 					tp->t_rtttime = 0;
26800e1d7c25SRichard Scheffenegger 					if (V_tcp_do_prr) {
26810e1d7c25SRichard Scheffenegger 						/*
26820e1d7c25SRichard Scheffenegger 						 * snd_ssthresh is already updated by
26830e1d7c25SRichard Scheffenegger 						 * cc_cong_signal.
26840e1d7c25SRichard Scheffenegger 						 */
2685c21b7b55SRichard Scheffenegger 						if (tcp_is_sack_recovery(tp, &to)) {
268649a6fbe3SRichard Scheffenegger 							/*
268749a6fbe3SRichard Scheffenegger 							 * Exclude Limited Transmit
268849a6fbe3SRichard Scheffenegger 							 * segments here
268949a6fbe3SRichard Scheffenegger 							 */
2690e9071000SRichard Scheffenegger 							tp->sackhint.prr_delivered =
269149a6fbe3SRichard Scheffenegger 							    maxseg;
269274d7fc87SRichard Scheffenegger 						} else {
269374d7fc87SRichard Scheffenegger 							tp->sackhint.prr_delivered =
269474d7fc87SRichard Scheffenegger 							    imin(tp->snd_max - tp->snd_una,
269574d7fc87SRichard Scheffenegger 							    imin(INT_MAX / 65536,
269674d7fc87SRichard Scheffenegger 								tp->t_dupacks) * maxseg);
269774d7fc87SRichard Scheffenegger 						}
2698bc7ee8e5SRichard Scheffenegger 						tp->sackhint.recover_fs = max(1,
2699bc7ee8e5SRichard Scheffenegger 						    tp->snd_nxt - tp->snd_una);
27000e1d7c25SRichard Scheffenegger 					}
2701c21b7b55SRichard Scheffenegger 					if (tcp_is_sack_recovery(tp, &to)) {
2702*2d05a1c8SRichard Scheffenegger 						TCPSTAT_INC(tcps_sack_recovery_episode);
2703a3574665SMichael Tuexen 						tp->snd_recover = tp->snd_nxt;
27040c39d38dSGleb Smirnoff 						tp->snd_cwnd = maxseg;
270540fa3e40SGleb Smirnoff 						(void) tcp_output(tp);
2706*2d05a1c8SRichard Scheffenegger 						if (SEQ_GT(th->th_ack, tp->snd_una)) {
27073c40e1d5SRichard Scheffenegger 							goto resume_partialack;
2708*2d05a1c8SRichard Scheffenegger 						}
27096d90faf3SPaul Saab 						goto drop;
27106d90faf3SPaul Saab 					}
2711fb59c426SYoshinobu Inoue 					tp->snd_nxt = th->th_ack;
27120c39d38dSGleb Smirnoff 					tp->snd_cwnd = maxseg;
271340fa3e40SGleb Smirnoff 					(void) tcp_output(tp);
271448d2549cSJeffrey Hsu 					KASSERT(tp->snd_limited <= 2,
2715302ce8d6SAndre Oppermann 					    ("%s: tp->snd_limited too big",
2716302ce8d6SAndre Oppermann 					    __func__));
2717df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
27180c39d38dSGleb Smirnoff 					     maxseg *
271948d2549cSJeffrey Hsu 					     (tp->t_dupacks - tp->snd_limited);
2720df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
2721df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
2722df8bae1dSRodney W. Grimes 					goto drop;
2723603724d3SBjoern A. Zeeb 				} else if (V_tcp_do_rfc3042) {
272462d4443fSHiren Panchasara 					/*
272562d4443fSHiren Panchasara 					 * Process first and second duplicate
272662d4443fSHiren Panchasara 					 * ACKs. Each indicates a segment
272762d4443fSHiren Panchasara 					 * leaving the network, creating room
272862d4443fSHiren Panchasara 					 * for more. Make sure we can send a
272962d4443fSHiren Panchasara 					 * packet on reception of each duplicate
273062d4443fSHiren Panchasara 					 * ACK by increasing snd_cwnd by one
273162d4443fSHiren Panchasara 					 * segment. Restore the original
273262d4443fSHiren Panchasara 					 * snd_cwnd after packet transmission.
273362d4443fSHiren Panchasara 					 */
2734*2d05a1c8SRichard Scheffenegger 					cc_ack_received(tp, th, nsegs, CC_DUPACK);
27353ac12506SJonathan T. Looney 					uint32_t oldcwnd = tp->snd_cwnd;
273648d2549cSJeffrey Hsu 					tcp_seq oldsndmax = tp->snd_max;
273748d2549cSJeffrey Hsu 					u_int sent;
27385628dd08SAndre Oppermann 					int avail;
273989c02376SJeffrey Hsu 
2740582a954bSJeffrey Hsu 					KASSERT(tp->t_dupacks == 1 ||
2741582a954bSJeffrey Hsu 					    tp->t_dupacks == 2,
2742302ce8d6SAndre Oppermann 					    ("%s: dupacks not 1 or 2",
2743302ce8d6SAndre Oppermann 					    __func__));
274461a36e3dSJeffrey Hsu 					if (tp->t_dupacks == 1)
274548d2549cSJeffrey Hsu 						tp->snd_limited = 0;
274661a36e3dSJeffrey Hsu 					tp->snd_cwnd =
274761a36e3dSJeffrey Hsu 					    (tp->snd_nxt - tp->snd_una) +
274861a36e3dSJeffrey Hsu 					    (tp->t_dupacks - tp->snd_limited) *
27490c39d38dSGleb Smirnoff 					    maxseg;
27505628dd08SAndre Oppermann 					/*
27515628dd08SAndre Oppermann 					 * Only call tcp_output when there
27525ae83e0dSMichael Tuexen 					 * is new data available to be sent
27535ae83e0dSMichael Tuexen 					 * or we need to send an ACK.
27545628dd08SAndre Oppermann 					 */
27555628dd08SAndre Oppermann 					SOCKBUF_LOCK(&so->so_snd);
2756*2d05a1c8SRichard Scheffenegger 					avail = sbavail(&so->so_snd);
27575628dd08SAndre Oppermann 					SOCKBUF_UNLOCK(&so->so_snd);
2758*2d05a1c8SRichard Scheffenegger 					if (tp->t_flags & TF_ACKNOW ||
2759*2d05a1c8SRichard Scheffenegger 					    (avail >=
2760*2d05a1c8SRichard Scheffenegger 					     SEQ_SUB(tp->snd_nxt, tp->snd_una))) {
276140fa3e40SGleb Smirnoff 						(void) tcp_output(tp);
2762*2d05a1c8SRichard Scheffenegger 					}
2763*2d05a1c8SRichard Scheffenegger 					sent = SEQ_SUB(tp->snd_max, oldsndmax);
27640c39d38dSGleb Smirnoff 					if (sent > maxseg) {
276589c02376SJeffrey Hsu 						KASSERT((tp->t_dupacks == 2 &&
276689c02376SJeffrey Hsu 						    tp->snd_limited == 0) ||
27670c39d38dSGleb Smirnoff 						   (sent == maxseg + 1 &&
276889c02376SJeffrey Hsu 						    tp->t_flags & TF_SENTFIN),
2769302ce8d6SAndre Oppermann 						    ("%s: sent too much",
2770302ce8d6SAndre Oppermann 						    __func__));
277148d2549cSJeffrey Hsu 						tp->snd_limited = 2;
2772*2d05a1c8SRichard Scheffenegger 					} else if (sent > 0) {
277348d2549cSJeffrey Hsu 						++tp->snd_limited;
2774*2d05a1c8SRichard Scheffenegger 					}
2775582a954bSJeffrey Hsu 					tp->snd_cwnd = oldcwnd;
2776582a954bSJeffrey Hsu 					goto drop;
2777df8bae1dSRodney W. Grimes 				}
2778021eaf79SHiren Panchasara 			}
2779df8bae1dSRodney W. Grimes 			break;
2780021eaf79SHiren Panchasara 		} else {
2781021eaf79SHiren Panchasara 			/*
2782021eaf79SHiren Panchasara 			 * This ack is advancing the left edge, reset the
2783021eaf79SHiren Panchasara 			 * counter.
2784021eaf79SHiren Panchasara 			 */
2785021eaf79SHiren Panchasara 			tp->t_dupacks = 0;
2786021eaf79SHiren Panchasara 			/*
2787021eaf79SHiren Panchasara 			 * If this ack also has new SACK info, increment the
2788f359d6ebSRichard Scheffenegger 			 * counter as per rfc6675. The variable
2789f359d6ebSRichard Scheffenegger 			 * sack_changed tracks all changes to the SACK
2790f359d6ebSRichard Scheffenegger 			 * scoreboard, including when partial ACKs without
2791f359d6ebSRichard Scheffenegger 			 * SACK options are received, and clear the scoreboard
2792f359d6ebSRichard Scheffenegger 			 * from the left side. Such partial ACKs should not be
2793f359d6ebSRichard Scheffenegger 			 * counted as dupacks here.
2794021eaf79SHiren Panchasara 			 */
2795c21b7b55SRichard Scheffenegger 			if (tcp_is_sack_recovery(tp, &to) &&
279649a6fbe3SRichard Scheffenegger 			    (sack_changed != SACK_NOCHANGE)) {
2797021eaf79SHiren Panchasara 				tp->t_dupacks++;
27983c40e1d5SRichard Scheffenegger 				/* limit overhead by setting maxseg last */
27993c40e1d5SRichard Scheffenegger 				if (!IN_FASTRECOVERY(tp->t_flags) &&
28003c40e1d5SRichard Scheffenegger 				    (tp->sackhint.sacked_bytes >
28013c40e1d5SRichard Scheffenegger 				    ((tcprexmtthresh - 1) *
28023c40e1d5SRichard Scheffenegger 				    (maxseg = tcp_maxseg(tp))))) {
28033c40e1d5SRichard Scheffenegger 					goto enter_recovery;
28043c40e1d5SRichard Scheffenegger 				}
28053c40e1d5SRichard Scheffenegger 			}
2806df8bae1dSRodney W. Grimes 		}
2807cb942153SJeffrey Hsu 
28083c40e1d5SRichard Scheffenegger resume_partialack:
2809302ce8d6SAndre Oppermann 		KASSERT(SEQ_GT(th->th_ack, tp->snd_una),
2810302ce8d6SAndre Oppermann 		    ("%s: th_ack <= snd_una", __func__));
2811cb942153SJeffrey Hsu 
2812df8bae1dSRodney W. Grimes 		/*
2813df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
2814df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
2815df8bae1dSRodney W. Grimes 		 */
2816dbc42409SLawrence Stewart 		if (IN_FASTRECOVERY(tp->t_flags)) {
2817cb942153SJeffrey Hsu 			if (SEQ_LT(th->th_ack, tp->snd_recover)) {
2818c7c325d0SRichard Scheffenegger 				if (tp->t_flags & TF_SACK_PERMIT) {
2819eb3a59a8SRichard Scheffenegger 					if (V_tcp_do_prr && to.to_flags & TOF_SACK) {
2820eb3a59a8SRichard Scheffenegger 						tcp_timer_activate(tp, TT_REXMT, 0);
2821eb3a59a8SRichard Scheffenegger 						tp->t_rtttime = 0;
2822c7c325d0SRichard Scheffenegger 						tcp_do_prr_ack(tp, th, &to,
2823c7c325d0SRichard Scheffenegger 						    sack_changed, &maxseg);
2824eb3a59a8SRichard Scheffenegger 						tp->t_flags |= TF_ACKNOW;
2825eb3a59a8SRichard Scheffenegger 						(void) tcp_output(tp);
2826c7c325d0SRichard Scheffenegger 					} else {
2827c7c325d0SRichard Scheffenegger 						tcp_sack_partialack(tp, th,
2828c7c325d0SRichard Scheffenegger 						    &maxseg);
2829c7c325d0SRichard Scheffenegger 					}
2830c7c325d0SRichard Scheffenegger 				} else {
2831c068736aSJeffrey Hsu 					tcp_newreno_partial_ack(tp, th);
2832c7c325d0SRichard Scheffenegger 				}
2833c7c325d0SRichard Scheffenegger 			} else {
2834dbc42409SLawrence Stewart 				cc_post_recovery(tp, th);
2835c7c325d0SRichard Scheffenegger 			}
2836b9f803b7SRichard Scheffenegger 		} else if (IN_CONGRECOVERY(tp->t_flags)) {
2837b9f803b7SRichard Scheffenegger 			if (SEQ_LT(th->th_ack, tp->snd_recover)) {
2838b9f803b7SRichard Scheffenegger 				if (V_tcp_do_prr) {
2839b9f803b7SRichard Scheffenegger 					tp->sackhint.delivered_data = BYTES_THIS_ACK(tp, th);
2840b9f803b7SRichard Scheffenegger 					tp->snd_fack = th->th_ack;
284149a6fbe3SRichard Scheffenegger 					/*
284249a6fbe3SRichard Scheffenegger 					 * During ECN cwnd reduction
284349a6fbe3SRichard Scheffenegger 					 * always use PRR-SSRB
284449a6fbe3SRichard Scheffenegger 					 */
2845c7c325d0SRichard Scheffenegger 					tcp_do_prr_ack(tp, th, &to, SACK_CHANGE,
2846c7c325d0SRichard Scheffenegger 					    &maxseg);
2847b9f803b7SRichard Scheffenegger 					(void) tcp_output(tp);
2848b9f803b7SRichard Scheffenegger 				}
2849c7c325d0SRichard Scheffenegger 			} else {
2850b9f803b7SRichard Scheffenegger 				cc_post_recovery(tp, th);
285146f58482SJonathan Lemon 			}
2852c7c325d0SRichard Scheffenegger 		}
2853a0292f23SGarrett Wollman 		/*
2854a0292f23SGarrett Wollman 		 * If we reach this point, ACK is not a duplicate,
2855a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
2856a0292f23SGarrett Wollman 		 */
2857a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
2858a0292f23SGarrett Wollman 			/*
2859a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
2860a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
286107e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
286207e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
286307e43e10SAndras Olah 			 * we can do window scaling.
2864a0292f23SGarrett Wollman 			 */
2865a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
2866a0292f23SGarrett Wollman 			tp->snd_una++;
286707e43e10SAndras Olah 			/* Do window scaling? */
286807e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
286907e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
287007e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2871464fcfbcSAndre Oppermann 				/* Send window already scaled. */
287207e43e10SAndras Olah 			}
2873a0292f23SGarrett Wollman 		}
2874a0292f23SGarrett Wollman 
2875a0292f23SGarrett Wollman process_ACK:
28769eb0e832SGleb Smirnoff 		INP_WLOCK_ASSERT(inp);
28777cfc6904SRobert Watson 
2878b2ade6b1SRichard Scheffenegger 		/*
2879b2ade6b1SRichard Scheffenegger 		 * Adjust for the SYN bit in sequence space,
2880b2ade6b1SRichard Scheffenegger 		 * but don't account for it in cwnd calculations.
2881b2ade6b1SRichard Scheffenegger 		 * This is for the SYN_RECEIVED, non-simultaneous
2882b2ade6b1SRichard Scheffenegger 		 * SYN case. SYN_SENT and simultaneous SYN are
2883b2ade6b1SRichard Scheffenegger 		 * treated elsewhere.
2884b2ade6b1SRichard Scheffenegger 		 */
2885b2ade6b1SRichard Scheffenegger 		if (incforsyn)
2886b2ade6b1SRichard Scheffenegger 			tp->snd_una++;
2887dbc42409SLawrence Stewart 		acked = BYTES_THIS_ACK(tp, th);
2888b8c2cd15SJonathan T. Looney 		KASSERT(acked >= 0, ("%s: acked unexepectedly negative "
2889b8c2cd15SJonathan T. Looney 		    "(tp->snd_una=%u, th->th_ack=%u, tp=%p, m=%p)", __func__,
2890b8c2cd15SJonathan T. Looney 		    tp->snd_una, th->th_ack, tp, m));
28914b7b743cSLawrence Stewart 		TCPSTAT_ADD(tcps_rcvackpack, nsegs);
289278b50714SRobert Watson 		TCPSTAT_ADD(tcps_rcvackbyte, acked);
2893df8bae1dSRodney W. Grimes 
2894df8bae1dSRodney W. Grimes 		/*
28959b8b58e0SJonathan Lemon 		 * If we just performed our first retransmit, and the ACK
28969b8b58e0SJonathan Lemon 		 * arrives within our recovery window, then it was a mistake
28979b8b58e0SJonathan Lemon 		 * to do the retransmit in the first place.  Recover our
28989b8b58e0SJonathan Lemon 		 * original cwnd and ssthresh, and proceed to transmit where
28999b8b58e0SJonathan Lemon 		 * we left off.
29009b8b58e0SJonathan Lemon 		 */
290110d20c84SMatt Macy 		if (tp->t_rxtshift == 1 &&
290210d20c84SMatt Macy 		    tp->t_flags & TF_PREVVALID &&
29034531b345SRichard Scheffenegger 		    tp->t_badrxtwin != 0 &&
29044531b345SRichard Scheffenegger 		    to.to_flags & TOF_TS &&
29054531b345SRichard Scheffenegger 		    to.to_tsecr != 0 &&
29064531b345SRichard Scheffenegger 		    TSTMP_LT(to.to_tsecr, tp->t_badrxtwin))
2907dbc42409SLawrence Stewart 			cc_cong_signal(tp, th, CC_RTO_ERR);
29089b8b58e0SJonathan Lemon 
29099b8b58e0SJonathan Lemon 		/*
2910df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
2911df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
2912df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
2913df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
2914df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
2915df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
2916df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
2917fa55172bSMatthew Dillon 		 *
2918fa55172bSMatthew Dillon 		 * Some boxes send broken timestamp replies
2919fa55172bSMatthew Dillon 		 * during the SYN+ACK phase, ignore
2920fa55172bSMatthew Dillon 		 * timestamps of 0 or we could calculate a
2921fa55172bSMatthew Dillon 		 * huge RTT and blow up the retransmit timer.
2922df8bae1dSRodney W. Grimes 		 */
2923d8951c8aSBjoern A. Zeeb 		if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) {
29243ac12506SJonathan T. Looney 			uint32_t t;
2925d8951c8aSBjoern A. Zeeb 
2926d8951c8aSBjoern A. Zeeb 			t = tcp_ts_getticks() - to.to_tsecr;
2927d8951c8aSBjoern A. Zeeb 			if (!tp->t_rttlow || tp->t_rttlow > t)
2928d8951c8aSBjoern A. Zeeb 				tp->t_rttlow = t;
2929d8951c8aSBjoern A. Zeeb 			tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1);
2930fa55172bSMatthew Dillon 		} else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
2931eaf80179SAndre Oppermann 			if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime)
2932eaf80179SAndre Oppermann 				tp->t_rttlow = ticks - tp->t_rtttime;
29339b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
2934fa55172bSMatthew Dillon 		}
2935df8bae1dSRodney W. Grimes 
293608af8aacSRandall Stewart 		SOCKBUF_LOCK(&so->so_snd);
293708af8aacSRandall Stewart 		/*
293808af8aacSRandall Stewart 		 * Clear t_acktime if remote side has ACKd all data in the
293908af8aacSRandall Stewart 		 * socket buffer and FIN (if applicable).
294008af8aacSRandall Stewart 		 * Otherwise, update t_acktime if we received a sufficiently
294108af8aacSRandall Stewart 		 * large ACK.
294208af8aacSRandall Stewart 		 */
294308af8aacSRandall Stewart 		if ((tp->t_state <= TCPS_CLOSE_WAIT &&
294408af8aacSRandall Stewart 		    acked == sbavail(&so->so_snd)) ||
294508af8aacSRandall Stewart 		    acked > sbavail(&so->so_snd))
294608af8aacSRandall Stewart 			tp->t_acktime = 0;
294708af8aacSRandall Stewart 		else if (acked > 1)
294808af8aacSRandall Stewart 			tp->t_acktime = ticks;
294908af8aacSRandall Stewart 
2950df8bae1dSRodney W. Grimes 		/*
2951df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
2952df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
2953df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
2954df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
2955df8bae1dSRodney W. Grimes 		 */
2956fb59c426SYoshinobu Inoue 		if (th->th_ack == tp->snd_max) {
2957b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
2958df8bae1dSRodney W. Grimes 			needoutput = 1;
2959b8152ba7SAndre Oppermann 		} else if (!tcp_timer_active(tp, TT_PERSIST))
296008af8aacSRandall Stewart 			tcp_timer_activate(tp, TT_REXMT, TP_RXTCUR(tp));
2961a0292f23SGarrett Wollman 
2962a0292f23SGarrett Wollman 		/*
2963a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
2964a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
2965a0292f23SGarrett Wollman 		 */
296608af8aacSRandall Stewart 		if (acked == 0) {
296708af8aacSRandall Stewart 			SOCKBUF_UNLOCK(&so->so_snd);
2968a0292f23SGarrett Wollman 			goto step6;
296908af8aacSRandall Stewart 		}
2970a0292f23SGarrett Wollman 
2971df8bae1dSRodney W. Grimes 		/*
2972dbc42409SLawrence Stewart 		 * Let the congestion control algorithm update congestion
2973dbc42409SLawrence Stewart 		 * control related information. This typically means increasing
2974dbc42409SLawrence Stewart 		 * the congestion window.
2975df8bae1dSRodney W. Grimes 		 */
29764b7b743cSLawrence Stewart 		cc_ack_received(tp, th, nsegs, CC_ACK);
2977dbc42409SLawrence Stewart 
2978cfa6009eSGleb Smirnoff 		if (acked > sbavail(&so->so_snd)) {
2979b8c2cd15SJonathan T. Looney 			if (tp->snd_wnd >= sbavail(&so->so_snd))
2980cfa6009eSGleb Smirnoff 				tp->snd_wnd -= sbavail(&so->so_snd);
2981b8c2cd15SJonathan T. Looney 			else
2982b8c2cd15SJonathan T. Looney 				tp->snd_wnd = 0;
2983c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd,
2984cfa6009eSGleb Smirnoff 			    (int)sbavail(&so->so_snd));
2985df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
2986df8bae1dSRodney W. Grimes 		} else {
2987c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd, acked);
29883ac12506SJonathan T. Looney 			if (tp->snd_wnd >= (uint32_t) acked)
298960ee3bb2SAndre Oppermann 				tp->snd_wnd -= acked;
2990b8c2cd15SJonathan T. Looney 			else
2991b8c2cd15SJonathan T. Looney 				tp->snd_wnd = 0;
2992df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
2993df8bae1dSRodney W. Grimes 		}
2994032bf749SRichard Scheffenegger 		/* NB: sowwakeup_locked() does an implicit unlock. */
2995032bf749SRichard Scheffenegger 		sowwakeup_locked(so);
2996c11a15bfSGleb Smirnoff 		m_freem(mfree);
2997e8949f74SAndre Oppermann 		/* Detect una wraparound. */
2998dbc42409SLawrence Stewart 		if (!IN_RECOVERY(tp->t_flags) &&
29999d11646dSJeffrey Hsu 		    SEQ_GT(tp->snd_una, tp->snd_recover) &&
30009d11646dSJeffrey Hsu 		    SEQ_LEQ(th->th_ack, tp->snd_recover))
30019d11646dSJeffrey Hsu 			tp->snd_recover = th->th_ack - 1;
3002dbc42409SLawrence Stewart 		/* XXXLAS: Can this be moved up into cc_post_recovery? */
3003dbc42409SLawrence Stewart 		if (IN_RECOVERY(tp->t_flags) &&
300424cb0f22SLawrence Stewart 		    SEQ_GEQ(th->th_ack, tp->snd_recover)) {
3005dbc42409SLawrence Stewart 			EXIT_RECOVERY(tp->t_flags);
300624cb0f22SLawrence Stewart 		}
3007fb59c426SYoshinobu Inoue 		tp->snd_una = th->th_ack;
30083529149eSAndre Oppermann 		if (tp->t_flags & TF_SACK_PERMIT) {
30096d90faf3SPaul Saab 			if (SEQ_GT(tp->snd_una, tp->snd_recover))
30106d90faf3SPaul Saab 				tp->snd_recover = tp->snd_una;
30116d90faf3SPaul Saab 		}
3012df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
3013df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
3014df8bae1dSRodney W. Grimes 
3015df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
3016df8bae1dSRodney W. Grimes 		/*
3017df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
3018df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
3019df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
3020df8bae1dSRodney W. Grimes 		 */
3021df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
3022df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
3023df8bae1dSRodney W. Grimes 				/*
3024df8bae1dSRodney W. Grimes 				 * If we can't receive any more
3025df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
3026df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
3027df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
3028df8bae1dSRodney W. Grimes 				 * we'll hang forever.
3029340c35deSJonathan Lemon 				 */
3030c0b99ffaSRobert Watson 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
303103e49181SSeigo Tanimura 					soisdisconnected(so);
30329077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
30339077f387SGleb Smirnoff 					    (tcp_fast_finwait2_recycle ?
30349077f387SGleb Smirnoff 					    tcp_finwait2_timeout :
30359077f387SGleb Smirnoff 					    TP_MAXIDLE(tp)));
30364cc20ab1SSeigo Tanimura 				}
303757f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_2);
3038df8bae1dSRodney W. Grimes 			}
3039df8bae1dSRodney W. Grimes 			break;
3040df8bae1dSRodney W. Grimes 
3041df8bae1dSRodney W. Grimes 		/*
3042df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
3043df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
3044df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
3045df8bae1dSRodney W. Grimes 		 * the segment.
3046df8bae1dSRodney W. Grimes 		 */
3047df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
3048df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
304911a20fb8SJeffrey Hsu 				tcp_twstart(tp);
3050340c35deSJonathan Lemon 				m_freem(m);
3051679d9708SAndre Oppermann 				return;
3052df8bae1dSRodney W. Grimes 			}
3053df8bae1dSRodney W. Grimes 			break;
3054df8bae1dSRodney W. Grimes 
3055df8bae1dSRodney W. Grimes 		/*
3056df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
3057df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
3058df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
3059df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
3060df8bae1dSRodney W. Grimes 		 */
3061df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
3062df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
3063df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
3064df8bae1dSRodney W. Grimes 				goto drop;
3065df8bae1dSRodney W. Grimes 			}
3066df8bae1dSRodney W. Grimes 			break;
3067df8bae1dSRodney W. Grimes 		}
3068df8bae1dSRodney W. Grimes 	}
3069df8bae1dSRodney W. Grimes 
3070df8bae1dSRodney W. Grimes step6:
30719eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
30727cfc6904SRobert Watson 
3073df8bae1dSRodney W. Grimes 	/*
307460ee3bb2SAndre Oppermann 	 * Update window information.
307560ee3bb2SAndre Oppermann 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
3076df8bae1dSRodney W. Grimes 	 */
307760ee3bb2SAndre Oppermann 	if ((thflags & TH_ACK) &&
307860ee3bb2SAndre Oppermann 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
307960ee3bb2SAndre Oppermann 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
308060ee3bb2SAndre Oppermann 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
308160ee3bb2SAndre Oppermann 		/* keep track of pure window updates */
308260ee3bb2SAndre Oppermann 		if (tlen == 0 &&
308360ee3bb2SAndre Oppermann 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
308478b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvwinupd);
3085df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
308660ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq;
308760ee3bb2SAndre Oppermann 		tp->snd_wl2 = th->th_ack;
3088df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
3089df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
309060ee3bb2SAndre Oppermann 		needoutput = 1;
3091df8bae1dSRodney W. Grimes 	}
3092df8bae1dSRodney W. Grimes 
3093df8bae1dSRodney W. Grimes 	/*
3094df8bae1dSRodney W. Grimes 	 * Process segments with URG.
3095df8bae1dSRodney W. Grimes 	 */
3096fb59c426SYoshinobu Inoue 	if ((thflags & TH_URG) && th->th_urp &&
3097df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
3098df8bae1dSRodney W. Grimes 		/*
3099df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
3100df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
3101df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
3102df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
3103df8bae1dSRodney W. Grimes 		 */
310418ad5842SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
3105cfa6009eSGleb Smirnoff 		if (th->th_urp + sbavail(&so->so_rcv) > sb_max) {
3106fb59c426SYoshinobu Inoue 			th->th_urp = 0;			/* XXX */
3107fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;		/* XXX */
310818ad5842SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);	/* XXX */
3109df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
3110df8bae1dSRodney W. Grimes 		}
3111df8bae1dSRodney W. Grimes 		/*
3112df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
3113df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
3114df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
3115df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
3116df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
3117df8bae1dSRodney W. Grimes 		 *
3118df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
3119df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
3120df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
3121df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
3122df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
3123df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
3124df8bae1dSRodney W. Grimes 		 */
3125fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
3126fb59c426SYoshinobu Inoue 			tp->rcv_up = th->th_seq + th->th_urp;
3127cfa6009eSGleb Smirnoff 			so->so_oobmark = sbavail(&so->so_rcv) +
3128df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
3129927c5ceaSRobert Watson 			if (so->so_oobmark == 0)
3130c0b99ffaSRobert Watson 				so->so_rcv.sb_state |= SBS_RCVATMARK;
3131df8bae1dSRodney W. Grimes 			sohasoutofband(so);
3132df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
3133df8bae1dSRodney W. Grimes 		}
313418ad5842SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
3135df8bae1dSRodney W. Grimes 		/*
3136df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
3137df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
3138df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
3139df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
3140df8bae1dSRodney W. Grimes 		 */
31413ac12506SJonathan T. Looney 		if (th->th_urp <= (uint32_t)tlen &&
314230613f56SJeffrey Hsu 		    !(so->so_options & SO_OOBINLINE)) {
314330613f56SJeffrey Hsu 			/* hdr drop is delayed */
314430613f56SJeffrey Hsu 			tcp_pulloutofband(so, th, m, drop_hdrlen);
314530613f56SJeffrey Hsu 		}
3146c068736aSJeffrey Hsu 	} else {
3147df8bae1dSRodney W. Grimes 		/*
3148df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
3149df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
3150df8bae1dSRodney W. Grimes 		 * with the receive window.
3151df8bae1dSRodney W. Grimes 		 */
3152df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
3153df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
3154c068736aSJeffrey Hsu 	}
3155df8bae1dSRodney W. Grimes dodata:							/* XXX */
31569eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
31577cfc6904SRobert Watson 
3158df8bae1dSRodney W. Grimes 	/*
3159df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
3160df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
3161df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
3162df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
3163df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
3164df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
3165df8bae1dSRodney W. Grimes 	 */
3166281a0fd4SPatrick Kelsey 	tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
316768bd7ed1SJonathan T. Looney 		   IS_FASTOPEN(tp->t_flags));
3168f1ea4e41SRandall Stewart 	if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) &&
3169df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
317069e03620SPaul Saab 		tcp_seq save_start = th->th_seq;
31715acfd95cSMichael Tuexen 		tcp_seq save_rnxt  = tp->rcv_nxt;
31725acfd95cSMichael Tuexen 		int     save_tlen  = tlen;
3173fb59c426SYoshinobu Inoue 		m_adj(m, drop_hdrlen);	/* delayed header drop */
3174e4b64281SJesper Skriver 		/*
3175c068736aSJeffrey Hsu 		 * Insert segment which includes th into TCP reassembly queue
3176c068736aSJeffrey Hsu 		 * with control block tp.  Set thflags to whether reassembly now
3177c068736aSJeffrey Hsu 		 * includes a segment with FIN.  This handles the common case
3178c068736aSJeffrey Hsu 		 * inline (segment is the next to be received on an established
3179c068736aSJeffrey Hsu 		 * connection, and the queue is empty), avoiding linkage into
3180c068736aSJeffrey Hsu 		 * and removal from the queue and repetition of various
3181c068736aSJeffrey Hsu 		 * conversions.
3182c068736aSJeffrey Hsu 		 * Set DELACK for segments received in order, but ack
3183c068736aSJeffrey Hsu 		 * immediately when segments are out of order (so
3184c068736aSJeffrey Hsu 		 * fast retransmit can work).
3185e4b64281SJesper Skriver 		 */
31864741bfcbSPatrick Kelsey 		if (th->th_seq == tp->rcv_nxt &&
3187c28440dbSRandall Stewart 		    SEGQ_EMPTY(tp) &&
3188281a0fd4SPatrick Kelsey 		    (TCPS_HAVEESTABLISHED(tp->t_state) ||
3189281a0fd4SPatrick Kelsey 		     tfo_syn)) {
3190281a0fd4SPatrick Kelsey 			if (DELAY_ACK(tp, tlen) || tfo_syn)
31913bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
3192e4b64281SJesper Skriver 			else
3193e4b64281SJesper Skriver 				tp->t_flags |= TF_ACKNOW;
3194e4b64281SJesper Skriver 			tp->rcv_nxt += tlen;
3195e854dd38SRandall Stewart 			if (tlen &&
3196e854dd38SRandall Stewart 			    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
3197e854dd38SRandall Stewart 			    (tp->t_fbyte_in == 0)) {
3198e854dd38SRandall Stewart 				tp->t_fbyte_in = ticks;
3199e854dd38SRandall Stewart 				if (tp->t_fbyte_in == 0)
3200e854dd38SRandall Stewart 					tp->t_fbyte_in = 1;
3201e854dd38SRandall Stewart 				if (tp->t_fbyte_out && tp->t_fbyte_in)
3202e854dd38SRandall Stewart 					tp->t_flags2 |= TF2_FBYTES_COMPLETE;
3203e854dd38SRandall Stewart 			}
32041ebf4607SRichard Scheffenegger 			thflags = tcp_get_flags(th) & TH_FIN;
320578b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpack);
320678b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
32071e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
3208c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
3209c1c36a2cSMike Silbersack 				m_freem(m);
3210c1c36a2cSMike Silbersack 			else
3211651e4e6aSGleb Smirnoff 				sbappendstream_locked(&so->so_rcv, m, 0);
32124d0770f1SRichard Scheffenegger 			tp->t_flags |= TF_WAKESOR;
3213e4b64281SJesper Skriver 		} else {
3214e8949f74SAndre Oppermann 			/*
3215e8949f74SAndre Oppermann 			 * XXX: Due to the header drop above "th" is
3216e8949f74SAndre Oppermann 			 * theoretically invalid by now.  Fortunately
3217e8949f74SAndre Oppermann 			 * m_adj() doesn't actually frees any mbufs
3218e8949f74SAndre Oppermann 			 * when trimming from the head.
3219e8949f74SAndre Oppermann 			 */
32205acfd95cSMichael Tuexen 			tcp_seq temp = save_start;
32214747500dSRandall Stewart 
32225acfd95cSMichael Tuexen 			thflags = tcp_reass(tp, th, &temp, &tlen, m);
3223e4b64281SJesper Skriver 			tp->t_flags |= TF_ACKNOW;
3224e4b64281SJesper Skriver 		}
322540f41eceSMichael Tuexen 		if ((tp->t_flags & TF_SACK_PERMIT) &&
322640f41eceSMichael Tuexen 		    (save_tlen > 0) &&
322740f41eceSMichael Tuexen 		    TCPS_HAVEESTABLISHED(tp->t_state)) {
32286d261981SMichael Tuexen 			if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) {
3229b5a154d8SMichael Tuexen 				/*
3230b5a154d8SMichael Tuexen 				 * DSACK actually handled in the fastpath
3231b5a154d8SMichael Tuexen 				 * above.
3232b5a154d8SMichael Tuexen 				 */
3233ecc5b1d1SMichael Tuexen 				tcp_update_sack_list(tp, save_start,
3234ecc5b1d1SMichael Tuexen 				    save_start + save_tlen);
3235ecc5b1d1SMichael Tuexen 			} else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) {
3236ecc5b1d1SMichael Tuexen 				if ((tp->rcv_numsacks >= 1) &&
3237ecc5b1d1SMichael Tuexen 				    (tp->sackblks[0].end == save_start)) {
32386d261981SMichael Tuexen 					/*
32396d261981SMichael Tuexen 					 * Partial overlap, recorded at todrop
32406d261981SMichael Tuexen 					 * above.
32416d261981SMichael Tuexen 					 */
32426d261981SMichael Tuexen 					tcp_update_sack_list(tp,
32436d261981SMichael Tuexen 					    tp->sackblks[0].start,
3244ecc5b1d1SMichael Tuexen 					    tp->sackblks[0].end);
3245ecc5b1d1SMichael Tuexen 				} else {
3246ecc5b1d1SMichael Tuexen 					tcp_update_dsack_list(tp, save_start,
3247ecc5b1d1SMichael Tuexen 					    save_start + save_tlen);
3248ecc5b1d1SMichael Tuexen 				}
32496d261981SMichael Tuexen 			} else if (tlen >= save_tlen) {
3250b5a154d8SMichael Tuexen 				/* Update of sackblks. */
3251ecc5b1d1SMichael Tuexen 				tcp_update_dsack_list(tp, save_start,
3252ecc5b1d1SMichael Tuexen 				    save_start + save_tlen);
3253ecc5b1d1SMichael Tuexen 			} else if (tlen > 0) {
3254ecc5b1d1SMichael Tuexen 				tcp_update_dsack_list(tp, save_start,
3255ecc5b1d1SMichael Tuexen 				    save_start + tlen);
32565acfd95cSMichael Tuexen 			}
32575acfd95cSMichael Tuexen 		}
3258c348e880SGleb Smirnoff 		tcp_handle_wakeup(tp);
3259302ce8d6SAndre Oppermann #if 0
3260df8bae1dSRodney W. Grimes 		/*
3261df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
3262df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
3263df8bae1dSRodney W. Grimes 		 * buffer size.
3264302ce8d6SAndre Oppermann 		 * XXX: Unused.
3265df8bae1dSRodney W. Grimes 		 */
3266f701e30dSJohn Baldwin 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt))
3267df8bae1dSRodney W. Grimes 			len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
3268f701e30dSJohn Baldwin 		else
3269f701e30dSJohn Baldwin 			len = so->so_rcv.sb_hiwat;
3270302ce8d6SAndre Oppermann #endif
3271df8bae1dSRodney W. Grimes 	} else {
3272df8bae1dSRodney W. Grimes 		m_freem(m);
3273fb59c426SYoshinobu Inoue 		thflags &= ~TH_FIN;
3274df8bae1dSRodney W. Grimes 	}
3275df8bae1dSRodney W. Grimes 
3276df8bae1dSRodney W. Grimes 	/*
3277df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
3278df8bae1dSRodney W. Grimes 	 * that the connection is closing.
3279df8bae1dSRodney W. Grimes 	 */
3280fb59c426SYoshinobu Inoue 	if (thflags & TH_FIN) {
3281df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
32824d0770f1SRichard Scheffenegger 			/* The socket upcall is handled by socantrcvmore. */
3283032bf749SRichard Scheffenegger 			socantrcvmore(so);
3284a0292f23SGarrett Wollman 			/*
3285a0292f23SGarrett Wollman 			 * If connection is half-synchronized
3286d3eede9dSAndras Olah 			 * (ie NEEDSYN flag on) then delay ACK,
3287a0292f23SGarrett Wollman 			 * so it may be piggybacked when SYN is sent.
3288a0292f23SGarrett Wollman 			 * Otherwise, since we received a FIN then no
3289a0292f23SGarrett Wollman 			 * more input can be expected, send ACK now.
3290a0292f23SGarrett Wollman 			 */
32913bfd6421SJonathan Lemon 			if (tp->t_flags & TF_NEEDSYN)
32923bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
3293a0292f23SGarrett Wollman 			else
3294df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
3295df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
3296df8bae1dSRodney W. Grimes 		}
3297df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
3298df8bae1dSRodney W. Grimes 		/*
3299df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
3300df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
3301df8bae1dSRodney W. Grimes 		 */
3302df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
33039b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
33049b8b58e0SJonathan Lemon 			/* FALLTHROUGH */
3305df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
330657f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSE_WAIT);
3307df8bae1dSRodney W. Grimes 			break;
3308df8bae1dSRodney W. Grimes 
3309df8bae1dSRodney W. Grimes 		/*
3310df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
3311df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
3312df8bae1dSRodney W. Grimes 		 */
3313df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
331457f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSING);
3315df8bae1dSRodney W. Grimes 			break;
3316df8bae1dSRodney W. Grimes 
3317df8bae1dSRodney W. Grimes 		/*
3318df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
3319df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
3320df8bae1dSRodney W. Grimes 		 * standard timers.
3321df8bae1dSRodney W. Grimes 		 */
3322df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
3323340c35deSJonathan Lemon 			tcp_twstart(tp);
3324679d9708SAndre Oppermann 			return;
3325df8bae1dSRodney W. Grimes 		}
3326df8bae1dSRodney W. Grimes 	}
33272b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
3328df8bae1dSRodney W. Grimes 
3329df8bae1dSRodney W. Grimes 	/*
3330df8bae1dSRodney W. Grimes 	 * Return any desired output.
3331df8bae1dSRodney W. Grimes 	 */
3332*2d05a1c8SRichard Scheffenegger 	if (needoutput || (tp->t_flags & TF_ACKNOW)) {
333340fa3e40SGleb Smirnoff 		(void) tcp_output(tp);
3334*2d05a1c8SRichard Scheffenegger 	}
3335a14c749fSJonathan Lemon check_delack:
33369eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
3337252ca428SRobert Watson 
3338a14c749fSJonathan Lemon 	if (tp->t_flags & TF_DELACK) {
33393bfd6421SJonathan Lemon 		tp->t_flags &= ~TF_DELACK;
3340b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
33413bfd6421SJonathan Lemon 	}
33429eb0e832SGleb Smirnoff 	INP_WUNLOCK(inp);
3343679d9708SAndre Oppermann 	return;
3344df8bae1dSRodney W. Grimes 
3345df8bae1dSRodney W. Grimes dropafterack:
3346df8bae1dSRodney W. Grimes 	/*
3347df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
3348df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
334980ab7c0eSGarrett Wollman 	 *
335080ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
335180ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
335280ab7c0eSGarrett Wollman 	 * RST have been dropped.
335380ab7c0eSGarrett Wollman 	 *
335480ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
335580ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
335680ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
335780ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
335880ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
335980ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
3360df8bae1dSRodney W. Grimes 	 */
3361fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
3362fb59c426SYoshinobu Inoue 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
3363a57815efSBosko Milekic 	     SEQ_GT(th->th_ack, tp->snd_max)) ) {
3364a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
3365d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
3366a57815efSBosko Milekic 		goto dropwithreset;
3367a57815efSBosko Milekic 	}
33682b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
3369df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
337040fa3e40SGleb Smirnoff 	(void) tcp_output(tp);
33719eb0e832SGleb Smirnoff 	INP_WUNLOCK(inp);
3372d6915262SRobert Watson 	m_freem(m);
3373679d9708SAndre Oppermann 	return;
3374df8bae1dSRodney W. Grimes 
3375df8bae1dSRodney W. Grimes dropwithreset:
3376a0ca0871SRobert Watson 	if (tp != NULL) {
3377302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
33789eb0e832SGleb Smirnoff 		INP_WUNLOCK(inp);
3379dd8ac7f9SRobert Watson 	} else
3380a0ca0871SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
3381679d9708SAndre Oppermann 	return;
3382df8bae1dSRodney W. Grimes 
3383df8bae1dSRodney W. Grimes drop:
3384df8bae1dSRodney W. Grimes 	/*
3385df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
3386df8bae1dSRodney W. Grimes 	 */
33872b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
33884d0770f1SRichard Scheffenegger 	if (tp != NULL) {
33899eb0e832SGleb Smirnoff 		INP_WUNLOCK(inp);
33904d0770f1SRichard Scheffenegger 	}
3391d6915262SRobert Watson 	m_freem(m);
3392302ce8d6SAndre Oppermann }
3393302ce8d6SAndre Oppermann 
3394302ce8d6SAndre Oppermann /*
33950ca3f933SAndre Oppermann  * Issue RST and make ACK acceptable to originator of segment.
33960ca3f933SAndre Oppermann  * The mbuf must still include the original packet header.
33970ca3f933SAndre Oppermann  * tp may be NULL.
3398302ce8d6SAndre Oppermann  */
339955bceb1eSRandall Stewart void
3400302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp,
3401302ce8d6SAndre Oppermann     int tlen, int rstreason)
3402302ce8d6SAndre Oppermann {
3403b287c6c7SBjoern A. Zeeb #ifdef INET
3404302ce8d6SAndre Oppermann 	struct ip *ip;
3405b287c6c7SBjoern A. Zeeb #endif
3406302ce8d6SAndre Oppermann #ifdef INET6
3407302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6;
3408302ce8d6SAndre Oppermann #endif
34097a3244ccSRobert Watson 
34107a3244ccSRobert Watson 	if (tp != NULL) {
34119eb0e832SGleb Smirnoff 		INP_LOCK_ASSERT(tptoinpcb(tp));
34127a3244ccSRobert Watson 	}
34137a3244ccSRobert Watson 
34140ca3f933SAndre Oppermann 	/* Don't bother if destination was broadcast/multicast. */
34151ebf4607SRichard Scheffenegger 	if ((tcp_get_flags(th) & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
3416302ce8d6SAndre Oppermann 		goto drop;
3417302ce8d6SAndre Oppermann #ifdef INET6
3418302ce8d6SAndre Oppermann 	if (mtod(m, struct ip *)->ip_v == 6) {
3419302ce8d6SAndre Oppermann 		ip6 = mtod(m, struct ip6_hdr *);
3420302ce8d6SAndre Oppermann 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
3421302ce8d6SAndre Oppermann 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
3422302ce8d6SAndre Oppermann 			goto drop;
3423302ce8d6SAndre Oppermann 		/* IPv6 anycast check is done at tcp6_input() */
3424b287c6c7SBjoern A. Zeeb 	}
3425302ce8d6SAndre Oppermann #endif
3426b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3427b287c6c7SBjoern A. Zeeb 	else
3428b287c6c7SBjoern A. Zeeb #endif
3429b287c6c7SBjoern A. Zeeb #ifdef INET
3430302ce8d6SAndre Oppermann 	{
3431302ce8d6SAndre Oppermann 		ip = mtod(m, struct ip *);
3432302ce8d6SAndre Oppermann 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
3433302ce8d6SAndre Oppermann 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
3434302ce8d6SAndre Oppermann 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
3435302ce8d6SAndre Oppermann 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
3436302ce8d6SAndre Oppermann 			goto drop;
3437302ce8d6SAndre Oppermann 	}
3438b287c6c7SBjoern A. Zeeb #endif
3439302ce8d6SAndre Oppermann 
3440302ce8d6SAndre Oppermann 	/* Perform bandwidth limiting. */
3441302ce8d6SAndre Oppermann 	if (badport_bandlim(rstreason) < 0)
3442302ce8d6SAndre Oppermann 		goto drop;
3443302ce8d6SAndre Oppermann 
3444302ce8d6SAndre Oppermann 	/* tcp_respond consumes the mbuf chain. */
34451ebf4607SRichard Scheffenegger 	if (tcp_get_flags(th) & TH_ACK) {
3446302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0,
3447302ce8d6SAndre Oppermann 		    th->th_ack, TH_RST);
3448302ce8d6SAndre Oppermann 	} else {
34491ebf4607SRichard Scheffenegger 		if (tcp_get_flags(th) & TH_SYN)
3450302ce8d6SAndre Oppermann 			tlen++;
34511ebf4607SRichard Scheffenegger 		if (tcp_get_flags(th) & TH_FIN)
34524ddd5aadSMichael Tuexen 			tlen++;
3453302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
3454302ce8d6SAndre Oppermann 		    (tcp_seq)0, TH_RST|TH_ACK);
3455302ce8d6SAndre Oppermann 	}
3456302ce8d6SAndre Oppermann 	return;
3457302ce8d6SAndre Oppermann drop:
3458302ce8d6SAndre Oppermann 	m_freem(m);
3459df8bae1dSRodney W. Grimes }
3460df8bae1dSRodney W. Grimes 
3461be2ac88cSJonathan Lemon /*
3462be2ac88cSJonathan Lemon  * Parse TCP options and place in tcpopt.
3463be2ac88cSJonathan Lemon  */
346455bceb1eSRandall Stewart void
3465ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags)
3466df8bae1dSRodney W. Grimes {
3467df8bae1dSRodney W. Grimes 	int opt, optlen;
3468df8bae1dSRodney W. Grimes 
3469be2ac88cSJonathan Lemon 	to->to_flags = 0;
3470df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
3471df8bae1dSRodney W. Grimes 		opt = cp[0];
3472df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
3473df8bae1dSRodney W. Grimes 			break;
3474df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
3475df8bae1dSRodney W. Grimes 			optlen = 1;
3476df8bae1dSRodney W. Grimes 		else {
3477b474779fSJun-ichiro itojun Hagino 			if (cnt < 2)
3478b474779fSJun-ichiro itojun Hagino 				break;
3479df8bae1dSRodney W. Grimes 			optlen = cp[1];
3480b474779fSJun-ichiro itojun Hagino 			if (optlen < 2 || optlen > cnt)
3481df8bae1dSRodney W. Grimes 				break;
3482df8bae1dSRodney W. Grimes 		}
3483df8bae1dSRodney W. Grimes 		switch (opt) {
3484df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
3485df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
3486df8bae1dSRodney W. Grimes 				continue;
3487f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3488df8bae1dSRodney W. Grimes 				continue;
3489be2ac88cSJonathan Lemon 			to->to_flags |= TOF_MSS;
3490be2ac88cSJonathan Lemon 			bcopy((char *)cp + 2,
3491be2ac88cSJonathan Lemon 			    (char *)&to->to_mss, sizeof(to->to_mss));
3492fd8e4ebcSMike Barcroft 			to->to_mss = ntohs(to->to_mss);
3493df8bae1dSRodney W. Grimes 			break;
3494df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
3495df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
3496df8bae1dSRodney W. Grimes 				continue;
3497f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3498df8bae1dSRodney W. Grimes 				continue;
3499be2ac88cSJonathan Lemon 			to->to_flags |= TOF_SCALE;
350002a1a643SAndre Oppermann 			to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT);
3501df8bae1dSRodney W. Grimes 			break;
3502df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
3503df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
3504df8bae1dSRodney W. Grimes 				continue;
3505be2ac88cSJonathan Lemon 			to->to_flags |= TOF_TS;
3506a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
3507a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
3508fd8e4ebcSMike Barcroft 			to->to_tsval = ntohl(to->to_tsval);
3509a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
3510a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
3511fd8e4ebcSMike Barcroft 			to->to_tsecr = ntohl(to->to_tsecr);
3512df8bae1dSRodney W. Grimes 			break;
35131cfd4b53SBruce M Simpson 		case TCPOPT_SIGNATURE:
3514fcf59617SAndrey V. Elsukov 			/*
3515fcf59617SAndrey V. Elsukov 			 * In order to reply to a host which has set the
3516fcf59617SAndrey V. Elsukov 			 * TCP_SIGNATURE option in its initial SYN, we have
3517fcf59617SAndrey V. Elsukov 			 * to record the fact that the option was observed
3518fcf59617SAndrey V. Elsukov 			 * here for the syncache code to perform the correct
3519fcf59617SAndrey V. Elsukov 			 * response.
3520fcf59617SAndrey V. Elsukov 			 */
35211cfd4b53SBruce M Simpson 			if (optlen != TCPOLEN_SIGNATURE)
35221cfd4b53SBruce M Simpson 				continue;
3523df47e437SAndre Oppermann 			to->to_flags |= TOF_SIGNATURE;
3524df47e437SAndre Oppermann 			to->to_signature = cp + 2;
35251cfd4b53SBruce M Simpson 			break;
35266d90faf3SPaul Saab 		case TCPOPT_SACK_PERMITTED:
3527f72167f4SAndre Oppermann 			if (optlen != TCPOLEN_SACK_PERMITTED)
35286d90faf3SPaul Saab 				continue;
3529f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3530f72167f4SAndre Oppermann 				continue;
3531603724d3SBjoern A. Zeeb 			if (!V_tcp_do_sack)
3532f72167f4SAndre Oppermann 				continue;
3533fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACKPERM;
35346d90faf3SPaul Saab 			break;
35356d90faf3SPaul Saab 		case TCPOPT_SACK:
35365a53ca16SPaul Saab 			if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
35376d90faf3SPaul Saab 				continue;
3538b728e902SAndre Oppermann 			if (flags & TO_SYN)
3539b728e902SAndre Oppermann 				continue;
3540fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACK;
35415a53ca16SPaul Saab 			to->to_nsacks = (optlen - 2) / TCPOLEN_SACK;
35425a53ca16SPaul Saab 			to->to_sacks = cp + 2;
354378b50714SRobert Watson 			TCPSTAT_INC(tcps_sack_rcv_blocks);
35446d90faf3SPaul Saab 			break;
3545281a0fd4SPatrick Kelsey 		case TCPOPT_FAST_OPEN:
3546c560df6fSPatrick Kelsey 			/*
3547c560df6fSPatrick Kelsey 			 * Cookie length validation is performed by the
3548c560df6fSPatrick Kelsey 			 * server side cookie checking code or the client
3549c560df6fSPatrick Kelsey 			 * side cookie cache update code.
3550c560df6fSPatrick Kelsey 			 */
3551281a0fd4SPatrick Kelsey 			if (!(flags & TO_SYN))
3552281a0fd4SPatrick Kelsey 				continue;
3553c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
3554c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
3555281a0fd4SPatrick Kelsey 				continue;
3556281a0fd4SPatrick Kelsey 			to->to_flags |= TOF_FASTOPEN;
3557281a0fd4SPatrick Kelsey 			to->to_tfo_len = optlen - 2;
3558281a0fd4SPatrick Kelsey 			to->to_tfo_cookie = to->to_tfo_len ? cp + 2 : NULL;
3559281a0fd4SPatrick Kelsey 			break;
3560be2ac88cSJonathan Lemon 		default:
3561be2ac88cSJonathan Lemon 			continue;
3562df8bae1dSRodney W. Grimes 		}
3563df8bae1dSRodney W. Grimes 	}
3564df8bae1dSRodney W. Grimes }
3565df8bae1dSRodney W. Grimes 
3566df8bae1dSRodney W. Grimes /*
3567df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
3568df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
3569df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
3570df8bae1dSRodney W. Grimes  * sequencing purposes.
3571df8bae1dSRodney W. Grimes  */
357255bceb1eSRandall Stewart void
3573ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m,
3574ad3f9ab3SAndre Oppermann     int off)
3575df8bae1dSRodney W. Grimes {
3576fb59c426SYoshinobu Inoue 	int cnt = off + th->th_urp - 1;
3577df8bae1dSRodney W. Grimes 
3578df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
3579df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
3580df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
3581df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
3582df8bae1dSRodney W. Grimes 
35839eb0e832SGleb Smirnoff 			INP_WLOCK_ASSERT(tptoinpcb(tp));
35847a3244ccSRobert Watson 
3585df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
3586df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
3587df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
3588df8bae1dSRodney W. Grimes 			m->m_len--;
358969a34685SYoshinobu Inoue 			if (m->m_flags & M_PKTHDR)
359069a34685SYoshinobu Inoue 				m->m_pkthdr.len--;
3591df8bae1dSRodney W. Grimes 			return;
3592df8bae1dSRodney W. Grimes 		}
3593df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
3594df8bae1dSRodney W. Grimes 		m = m->m_next;
35954dfdffe9SAndre Oppermann 		if (m == NULL)
3596df8bae1dSRodney W. Grimes 			break;
3597df8bae1dSRodney W. Grimes 	}
3598df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
3599df8bae1dSRodney W. Grimes }
3600df8bae1dSRodney W. Grimes 
3601df8bae1dSRodney W. Grimes /*
3602df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
3603df8bae1dSRodney W. Grimes  * and update averages and current timeout.
3604df8bae1dSRodney W. Grimes  */
360555bceb1eSRandall Stewart void
3606ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt)
3607df8bae1dSRodney W. Grimes {
3608ad3f9ab3SAndre Oppermann 	int delta;
3609233e8c18SGarrett Wollman 
36109eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
36112be3bf22SRobert Watson 
361278b50714SRobert Watson 	TCPSTAT_INC(tcps_rttupdated);
361318b83b62SRichard Scheffenegger 	if (tp->t_rttupdated < UCHAR_MAX)
3614233e8c18SGarrett Wollman 		tp->t_rttupdated++;
3615adc56f5aSEdward Tomasz Napierala #ifdef STATS
3616adc56f5aSEdward Tomasz Napierala 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT,
3617adc56f5aSEdward Tomasz Napierala 	    imax(0, rtt * 1000 / hz));
3618adc56f5aSEdward Tomasz Napierala #endif
36195ede40dcSRyan Stone 	if ((tp->t_srtt != 0) && (tp->t_rxtshift <= TCP_RTT_INVALIDATE)) {
3620233e8c18SGarrett Wollman 		/*
3621233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
3622233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
3623233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
3624233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
3625233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
3626233e8c18SGarrett Wollman 		 */
3627233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
3628233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
3629233e8c18SGarrett Wollman 
3630233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
3631233e8c18SGarrett Wollman 			tp->t_srtt = 1;
3632233e8c18SGarrett Wollman 
3633233e8c18SGarrett Wollman 		/*
3634233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
3635233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
3636233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
3637233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
3638233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
3639233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
3640233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
3641233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
3642233e8c18SGarrett Wollman 		 */
3643233e8c18SGarrett Wollman 		if (delta < 0)
3644233e8c18SGarrett Wollman 			delta = -delta;
3645233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
3646233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
3647233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
3648233e8c18SGarrett Wollman 	} else {
3649233e8c18SGarrett Wollman 		/*
3650233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
3651233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
3652233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
3653233e8c18SGarrett Wollman 		 */
3654233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
3655233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
3656233e8c18SGarrett Wollman 	}
36579b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
3658df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
3659df8bae1dSRodney W. Grimes 
3660df8bae1dSRodney W. Grimes 	/*
3661df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
3662df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
3663df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
3664df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
3665df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
3666df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
3667df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
3668df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
3669df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
3670df8bae1dSRodney W. Grimes 	 */
3671233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
36729e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
3673df8bae1dSRodney W. Grimes 
3674df8bae1dSRodney W. Grimes 	/*
3675df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
3676df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
3677df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
3678df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
3679df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
3680df8bae1dSRodney W. Grimes 	 */
3681df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
3682df8bae1dSRodney W. Grimes }
3683df8bae1dSRodney W. Grimes 
3684df8bae1dSRodney W. Grimes /*
3685df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
3686df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
36874faaea55SAndre Oppermann  * If none, use an mss that can be handled on the outgoing interface
36884faaea55SAndre Oppermann  * without forcing IP to fragment.  If no route is found, route has no mtu,
3689df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
3690df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
3691df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
3692df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
3693df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
3694df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
3695df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
3696a0292f23SGarrett Wollman  *
36970c39d38dSGleb Smirnoff  * NOTE that resulting t_maxseg doesn't include space for TCP options or
36980c39d38dSGleb Smirnoff  * IP options, e.g. IPSEC data, since length of this data may vary, and
36990c39d38dSGleb Smirnoff  * thus it is calculated for every segment separately in tcp_output().
3700a0292f23SGarrett Wollman  *
370197d8d152SAndre Oppermann  * NOTE that this routine is only called when we process an incoming
3702ef341ee1SGleb Smirnoff  * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS
3703ef341ee1SGleb Smirnoff  * settings are handled in tcp_mssopt().
3704df8bae1dSRodney W. Grimes  */
3705a0292f23SGarrett Wollman void
3706ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer,
37073c914c54SAndre Oppermann     struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap)
3708df8bae1dSRodney W. Grimes {
3709b287c6c7SBjoern A. Zeeb 	int mss = 0;
37103ac12506SJonathan T. Looney 	uint32_t maxmtu = 0;
37119eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
371297d8d152SAndre Oppermann 	struct hc_metrics_lite metrics;
3713fb59c426SYoshinobu Inoue #ifdef INET6
3714c068736aSJeffrey Hsu 	int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
3715c068736aSJeffrey Hsu 	size_t min_protoh = isipv6 ?
3716c068736aSJeffrey Hsu 			    sizeof (struct ip6_hdr) + sizeof (struct tcphdr) :
3717c068736aSJeffrey Hsu 			    sizeof (struct tcpiphdr);
3718c068736aSJeffrey Hsu #else
37199e644c23SMichael Tuexen 	 size_t min_protoh = sizeof(struct tcpiphdr);
3720fb59c426SYoshinobu Inoue #endif
3721df8bae1dSRodney W. Grimes 
37229eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
37237a3244ccSRobert Watson 
37249e644c23SMichael Tuexen 	if (tp->t_port)
37259e644c23SMichael Tuexen 		min_protoh += V_tcp_udp_tunneling_overhead;
3726ef341ee1SGleb Smirnoff 	if (mtuoffer != -1) {
3727ef341ee1SGleb Smirnoff 		KASSERT(offer == -1, ("%s: conflict", __func__));
3728ef341ee1SGleb Smirnoff 		offer = mtuoffer - min_protoh;
3729ef341ee1SGleb Smirnoff 	}
3730ef341ee1SGleb Smirnoff 
3731e8949f74SAndre Oppermann 	/* Initialize. */
373297d8d152SAndre Oppermann #ifdef INET6
373397d8d152SAndre Oppermann 	if (isipv6) {
37343c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu6(&inp->inp_inc, cap);
37350c39d38dSGleb Smirnoff 		tp->t_maxseg = V_tcp_v6mssdflt;
3736b287c6c7SBjoern A. Zeeb 	}
373797d8d152SAndre Oppermann #endif
3738b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3739b287c6c7SBjoern A. Zeeb 	else
3740b287c6c7SBjoern A. Zeeb #endif
3741b287c6c7SBjoern A. Zeeb #ifdef INET
374297d8d152SAndre Oppermann 	{
37433c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu(&inp->inp_inc, cap);
37440c39d38dSGleb Smirnoff 		tp->t_maxseg = V_tcp_mssdflt;
3745df8bae1dSRodney W. Grimes 	}
3746b287c6c7SBjoern A. Zeeb #endif
3747df8bae1dSRodney W. Grimes 
374897d8d152SAndre Oppermann 	/*
3749e8949f74SAndre Oppermann 	 * No route to sender, stay with default mss and return.
375097d8d152SAndre Oppermann 	 */
37516f01cac6SBjoern A. Zeeb 	if (maxmtu == 0) {
37526f01cac6SBjoern A. Zeeb 		/*
37538e5c87f4SBjoern A. Zeeb 		 * In case we return early we need to initialize metrics
37546f01cac6SBjoern A. Zeeb 		 * to a defined state as tcp_hc_get() would do for us
37556f01cac6SBjoern A. Zeeb 		 * if there was no cache hit.
37566f01cac6SBjoern A. Zeeb 		 */
37576f01cac6SBjoern A. Zeeb 		if (metricptr != NULL)
37586f01cac6SBjoern A. Zeeb 			bzero(metricptr, sizeof(struct hc_metrics_lite));
375997d8d152SAndre Oppermann 		return;
37606f01cac6SBjoern A. Zeeb 	}
376197d8d152SAndre Oppermann 
3762e8949f74SAndre Oppermann 	/* What have we got? */
376397d8d152SAndre Oppermann 	switch (offer) {
376497d8d152SAndre Oppermann 		case 0:
376597d8d152SAndre Oppermann 			/*
376697d8d152SAndre Oppermann 			 * Offer == 0 means that there was no MSS on the SYN
3767c3b02504SBjoern A. Zeeb 			 * segment, in this case we use tcp_mssdflt as
37680c39d38dSGleb Smirnoff 			 * already assigned to t_maxseg above.
376997d8d152SAndre Oppermann 			 */
37700c39d38dSGleb Smirnoff 			offer = tp->t_maxseg;
377197d8d152SAndre Oppermann 			break;
377297d8d152SAndre Oppermann 
377397d8d152SAndre Oppermann 		case -1:
3774a0292f23SGarrett Wollman 			/*
3775c94c54e4SAndre Oppermann 			 * Offer == -1 means that we didn't receive SYN yet.
3776a0292f23SGarrett Wollman 			 */
377797d8d152SAndre Oppermann 			/* FALLTHROUGH */
377897d8d152SAndre Oppermann 
377997d8d152SAndre Oppermann 		default:
3780a0292f23SGarrett Wollman 			/*
378153369ac9SAndre Oppermann 			 * Prevent DoS attack with too small MSS. Round up
378253369ac9SAndre Oppermann 			 * to at least minmss.
378353369ac9SAndre Oppermann 			 */
3784603724d3SBjoern A. Zeeb 			offer = max(offer, V_tcp_minmss);
378597d8d152SAndre Oppermann 	}
3786a0292f23SGarrett Wollman 
3787df8bae1dSRodney W. Grimes 	/*
3788e8949f74SAndre Oppermann 	 * rmx information is now retrieved from tcp_hostcache.
3789df8bae1dSRodney W. Grimes 	 */
379097d8d152SAndre Oppermann 	tcp_hc_get(&inp->inp_inc, &metrics);
37913cee92e0SBjoern A. Zeeb 	if (metricptr != NULL)
37923cee92e0SBjoern A. Zeeb 		bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite));
379397d8d152SAndre Oppermann 
3794df8bae1dSRodney W. Grimes 	/*
3795cc412412SHiren Panchasara 	 * If there's a discovered mtu in tcp hostcache, use it.
3796cc412412SHiren Panchasara 	 * Else, use the link mtu.
3797df8bae1dSRodney W. Grimes 	 */
379897d8d152SAndre Oppermann 	if (metrics.rmx_mtu)
37992d166c02SAndre Oppermann 		mss = min(metrics.rmx_mtu, maxmtu) - min_protoh;
3800c068736aSJeffrey Hsu 	else {
380131b3783cSHajimu UMEMOTO #ifdef INET6
3802fb59c426SYoshinobu Inoue 		if (isipv6) {
380397d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3804603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
380597d8d152SAndre Oppermann 			    !in6_localaddr(&inp->in6p_faddr))
3806603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_v6mssdflt);
3807b287c6c7SBjoern A. Zeeb 		}
3808b3399803SHajimu UMEMOTO #endif
3809b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3810b287c6c7SBjoern A. Zeeb 		else
3811b287c6c7SBjoern A. Zeeb #endif
3812b287c6c7SBjoern A. Zeeb #ifdef INET
381397d8d152SAndre Oppermann 		{
381497d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3815603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
381697d8d152SAndre Oppermann 			    !in_localaddr(inp->inp_faddr))
3817603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_mssdflt);
3818a0292f23SGarrett Wollman 		}
3819b287c6c7SBjoern A. Zeeb #endif
38203cee92e0SBjoern A. Zeeb 		/*
38213cee92e0SBjoern A. Zeeb 		 * XXX - The above conditional (mss = maxmtu - min_protoh)
38223cee92e0SBjoern A. Zeeb 		 * probably violates the TCP spec.
38233cee92e0SBjoern A. Zeeb 		 * The problem is that, since we don't know the
38243cee92e0SBjoern A. Zeeb 		 * other end's MSS, we are supposed to use a conservative
38253cee92e0SBjoern A. Zeeb 		 * default.  But, if we do that, then MTU discovery will
38263cee92e0SBjoern A. Zeeb 		 * never actually take place, because the conservative
38273cee92e0SBjoern A. Zeeb 		 * default is much less than the MTUs typically seen
38283cee92e0SBjoern A. Zeeb 		 * on the Internet today.  For the moment, we'll sweep
38293cee92e0SBjoern A. Zeeb 		 * this under the carpet.
38303cee92e0SBjoern A. Zeeb 		 *
38313cee92e0SBjoern A. Zeeb 		 * The conservative default might not actually be a problem
38323cee92e0SBjoern A. Zeeb 		 * if the only case this occurs is when sending an initial
38333cee92e0SBjoern A. Zeeb 		 * SYN with options and data to a host we've never talked
38343cee92e0SBjoern A. Zeeb 		 * to before.  Then, they will reply with an MSS value which
38353cee92e0SBjoern A. Zeeb 		 * will get recorded and the new parameters should get
38363cee92e0SBjoern A. Zeeb 		 * recomputed.  For Further Study.
38373cee92e0SBjoern A. Zeeb 		 */
383897d8d152SAndre Oppermann 	}
3839a0292f23SGarrett Wollman 	mss = min(mss, offer);
384097d8d152SAndre Oppermann 
3841a0292f23SGarrett Wollman 	/*
38420c39d38dSGleb Smirnoff 	 * Sanity check: make sure that maxseg will be large
38433cee92e0SBjoern A. Zeeb 	 * enough to allow some data on segments even if the
38443cee92e0SBjoern A. Zeeb 	 * all the option space is used (40bytes).  Otherwise
38453cee92e0SBjoern A. Zeeb 	 * funny things may happen in tcp_output.
38460c39d38dSGleb Smirnoff 	 *
38470c39d38dSGleb Smirnoff 	 * XXXGL: shouldn't we reserve space for IP/IPv6 options?
38483cee92e0SBjoern A. Zeeb 	 */
38493cee92e0SBjoern A. Zeeb 	mss = max(mss, 64);
38503cee92e0SBjoern A. Zeeb 
385197d8d152SAndre Oppermann 	tp->t_maxseg = mss;
38523cee92e0SBjoern A. Zeeb }
38533cee92e0SBjoern A. Zeeb 
38543cee92e0SBjoern A. Zeeb void
38553cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer)
38563cee92e0SBjoern A. Zeeb {
3857dbc42409SLawrence Stewart 	int mss;
38583ac12506SJonathan T. Looney 	uint32_t bufsize;
38599eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
38603cee92e0SBjoern A. Zeeb 	struct socket *so;
38613cee92e0SBjoern A. Zeeb 	struct hc_metrics_lite metrics;
38623c914c54SAndre Oppermann 	struct tcp_ifcap cap;
3863dbc42409SLawrence Stewart 
38643cee92e0SBjoern A. Zeeb 	KASSERT(tp != NULL, ("%s: tp == NULL", __func__));
38653cee92e0SBjoern A. Zeeb 
38663c914c54SAndre Oppermann 	bzero(&cap, sizeof(cap));
38673c914c54SAndre Oppermann 	tcp_mss_update(tp, offer, -1, &metrics, &cap);
38683cee92e0SBjoern A. Zeeb 
38693cee92e0SBjoern A. Zeeb 	mss = tp->t_maxseg;
387097d8d152SAndre Oppermann 
3871df8bae1dSRodney W. Grimes 	/*
387297d8d152SAndre Oppermann 	 * If there's a pipesize, change the socket buffer to that size,
387397d8d152SAndre Oppermann 	 * don't change if sb_hiwat is different than default (then it
387497d8d152SAndre Oppermann 	 * has been changed on purpose with setsockopt).
387597d8d152SAndre Oppermann 	 * Make the socket buffers an integral number of mss units;
387697d8d152SAndre Oppermann 	 * if the mss is larger than the socket buffer, decrease the mss.
3877df8bae1dSRodney W. Grimes 	 */
3878c3b02504SBjoern A. Zeeb 	so = inp->inp_socket;
38793f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
3880e233e2acSAndre Oppermann 	if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe)
388197d8d152SAndre Oppermann 		bufsize = metrics.rmx_sendpipe;
388297d8d152SAndre Oppermann 	else
3883df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
3884df8bae1dSRodney W. Grimes 	if (bufsize < mss)
3885df8bae1dSRodney W. Grimes 		mss = bufsize;
3886df8bae1dSRodney W. Grimes 	else {
3887df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3888df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3889df8bae1dSRodney W. Grimes 			bufsize = sb_max;
389088c39af3SRuslan Ermilov 		if (bufsize > so->so_snd.sb_hiwat)
389143283184SGleb Smirnoff 			(void)sbreserve_locked(so, SO_SND, bufsize, NULL);
3892df8bae1dSRodney W. Grimes 	}
38933f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
3894784ce8faSHiren Panchasara 	/*
3895784ce8faSHiren Panchasara 	 * Sanity check: make sure that maxseg will be large
3896784ce8faSHiren Panchasara 	 * enough to allow some data on segments even if the
3897784ce8faSHiren Panchasara 	 * all the option space is used (40bytes).  Otherwise
3898784ce8faSHiren Panchasara 	 * funny things may happen in tcp_output.
3899784ce8faSHiren Panchasara 	 *
3900784ce8faSHiren Panchasara 	 * XXXGL: shouldn't we reserve space for IP/IPv6 options?
3901784ce8faSHiren Panchasara 	 */
3902784ce8faSHiren Panchasara 	tp->t_maxseg = max(mss, 64);
3903df8bae1dSRodney W. Grimes 
39043f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
3905e233e2acSAndre Oppermann 	if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe)
390697d8d152SAndre Oppermann 		bufsize = metrics.rmx_recvpipe;
390797d8d152SAndre Oppermann 	else
3908df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
3909df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
3910df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3911df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3912df8bae1dSRodney W. Grimes 			bufsize = sb_max;
391388c39af3SRuslan Ermilov 		if (bufsize > so->so_rcv.sb_hiwat)
391443283184SGleb Smirnoff 			(void)sbreserve_locked(so, SO_RCV, bufsize, NULL);
3915df8bae1dSRodney W. Grimes 	}
39163f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
391791d6cfa6SBjoern A. Zeeb 
391891d6cfa6SBjoern A. Zeeb 	/* Check the interface for TSO capabilities. */
39193c914c54SAndre Oppermann 	if (cap.ifcap & CSUM_TSO) {
392091d6cfa6SBjoern A. Zeeb 		tp->t_flags |= TF_TSO;
39213c914c54SAndre Oppermann 		tp->t_tsomax = cap.tsomax;
39229fd573c3SHans Petter Selasky 		tp->t_tsomaxsegcount = cap.tsomaxsegcount;
39239fd573c3SHans Petter Selasky 		tp->t_tsomaxsegsize = cap.tsomaxsegsize;
39243c914c54SAndre Oppermann 	}
3925a0292f23SGarrett Wollman }
3926a0292f23SGarrett Wollman 
3927a0292f23SGarrett Wollman /*
3928a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
3929a0292f23SGarrett Wollman  */
3930a0292f23SGarrett Wollman int
3931ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc)
3932a0292f23SGarrett Wollman {
393397d8d152SAndre Oppermann 	int mss = 0;
39343ac12506SJonathan T. Looney 	uint32_t thcmtu = 0;
39353ac12506SJonathan T. Looney 	uint32_t maxmtu = 0;
393697d8d152SAndre Oppermann 	size_t min_protoh;
3937a0292f23SGarrett Wollman 
393897d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer"));
3939a0292f23SGarrett Wollman 
394031b3783cSHajimu UMEMOTO #ifdef INET6
3941dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
3942603724d3SBjoern A. Zeeb 		mss = V_tcp_v6mssdflt;
3943233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu6(inc, NULL);
394497d8d152SAndre Oppermann 		min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
3945b287c6c7SBjoern A. Zeeb 	}
394631b3783cSHajimu UMEMOTO #endif
3947b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3948b287c6c7SBjoern A. Zeeb 	else
3949b287c6c7SBjoern A. Zeeb #endif
3950b287c6c7SBjoern A. Zeeb #ifdef INET
395197d8d152SAndre Oppermann 	{
3952603724d3SBjoern A. Zeeb 		mss = V_tcp_mssdflt;
3953233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu(inc, NULL);
395497d8d152SAndre Oppermann 		min_protoh = sizeof(struct tcpiphdr);
395597d8d152SAndre Oppermann 	}
3956b287c6c7SBjoern A. Zeeb #endif
39573a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET)
39583a9391deSBjoern A. Zeeb 	thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
39593a9391deSBjoern A. Zeeb #endif
39603a9391deSBjoern A. Zeeb 
396197d8d152SAndre Oppermann 	if (maxmtu && thcmtu)
396297d8d152SAndre Oppermann 		mss = min(maxmtu, thcmtu) - min_protoh;
396397d8d152SAndre Oppermann 	else if (maxmtu || thcmtu)
396497d8d152SAndre Oppermann 		mss = max(maxmtu, thcmtu) - min_protoh;
396597d8d152SAndre Oppermann 
396697d8d152SAndre Oppermann 	return (mss);
3967df8bae1dSRodney W. Grimes }
396846f58482SJonathan Lemon 
39690e1d7c25SRichard Scheffenegger void
3970c7c325d0SRichard Scheffenegger tcp_do_prr_ack(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to,
3971c7c325d0SRichard Scheffenegger     sackstatus_t sack_changed, u_int *maxsegp)
39720e1d7c25SRichard Scheffenegger {
397348396dc7SRichard Scheffenegger 	int snd_cnt = 0, limit = 0, del_data = 0, pipe = 0;
3974c7c325d0SRichard Scheffenegger 	u_int maxseg;
39750e1d7c25SRichard Scheffenegger 
39769eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
39770e1d7c25SRichard Scheffenegger 
3978c7c325d0SRichard Scheffenegger 	if (*maxsegp == 0) {
3979c7c325d0SRichard Scheffenegger 		*maxsegp = tcp_maxseg(tp);
3980c7c325d0SRichard Scheffenegger 	}
3981c7c325d0SRichard Scheffenegger 	maxseg = *maxsegp;
39820e1d7c25SRichard Scheffenegger 	/*
39830e1d7c25SRichard Scheffenegger 	 * Compute the amount of data that this ACK is indicating
39840e1d7c25SRichard Scheffenegger 	 * (del_data) and an estimate of how many bytes are in the
39850e1d7c25SRichard Scheffenegger 	 * network.
39860e1d7c25SRichard Scheffenegger 	 */
3987c21b7b55SRichard Scheffenegger 	if (tcp_is_sack_recovery(tp, to) ||
398874d7fc87SRichard Scheffenegger 	    (IN_CONGRECOVERY(tp->t_flags) &&
398974d7fc87SRichard Scheffenegger 	     !IN_FASTRECOVERY(tp->t_flags))) {
399031d7a27cSRichard Scheffenegger 		del_data = tp->sackhint.delivered_data;
3991d1de2b05SRichard Scheffenegger 		if (V_tcp_do_newsack)
3992a8e431e1SRichard Scheffenegger 			pipe = tcp_compute_pipe(tp);
3993a8e431e1SRichard Scheffenegger 		else
399474d7fc87SRichard Scheffenegger 			pipe = (tp->snd_nxt - tp->snd_fack) +
399574d7fc87SRichard Scheffenegger 				tp->sackhint.sack_bytes_rexmit;
399674d7fc87SRichard Scheffenegger 	} else {
399774d7fc87SRichard Scheffenegger 		if (tp->sackhint.prr_delivered < (tcprexmtthresh * maxseg +
3998*2d05a1c8SRichard Scheffenegger 					     tp->snd_recover - tp->snd_una)) {
399974d7fc87SRichard Scheffenegger 			del_data = maxseg;
4000*2d05a1c8SRichard Scheffenegger 		}
400174d7fc87SRichard Scheffenegger 		pipe = imax(0, tp->snd_max - tp->snd_una -
400274d7fc87SRichard Scheffenegger 			    imin(INT_MAX / 65536, tp->t_dupacks) * maxseg);
400374d7fc87SRichard Scheffenegger 	}
40040e1d7c25SRichard Scheffenegger 	tp->sackhint.prr_delivered += del_data;
40050e1d7c25SRichard Scheffenegger 	/*
40060e1d7c25SRichard Scheffenegger 	 * Proportional Rate Reduction
40070e1d7c25SRichard Scheffenegger 	 */
4008e9071000SRichard Scheffenegger 	if (pipe >= tp->snd_ssthresh) {
40096a376af0SRichard Scheffenegger 		if (tp->sackhint.recover_fs == 0)
40106a376af0SRichard Scheffenegger 			tp->sackhint.recover_fs =
401148396dc7SRichard Scheffenegger 			    imax(1, tp->snd_nxt - tp->snd_una);
401248396dc7SRichard Scheffenegger 		snd_cnt = howmany((long)tp->sackhint.prr_delivered *
401348396dc7SRichard Scheffenegger 			    tp->snd_ssthresh, tp->sackhint.recover_fs) -
40149276ad23SRichard Scheffenegger 			    tp->sackhint.prr_out + maxseg - 1;
40156a376af0SRichard Scheffenegger 	} else {
401649a6fbe3SRichard Scheffenegger 		/*
401749a6fbe3SRichard Scheffenegger 		 * PRR 6937bis heuristic:
401849a6fbe3SRichard Scheffenegger 		 * - A partial ack without SACK block beneath snd_recover
401949a6fbe3SRichard Scheffenegger 		 * indicates further loss.
402049a6fbe3SRichard Scheffenegger 		 * - An SACK scoreboard update adding a new hole indicates
402149a6fbe3SRichard Scheffenegger 		 * further loss, so be conservative and send at most one
402249a6fbe3SRichard Scheffenegger 		 * segment.
402349a6fbe3SRichard Scheffenegger 		 * - Prevent ACK splitting attacks, by being conservative
402449a6fbe3SRichard Scheffenegger 		 * when no new data is acked.
402549a6fbe3SRichard Scheffenegger 		 */
4026*2d05a1c8SRichard Scheffenegger 		if ((sack_changed == SACK_NEWLOSS) || (del_data == 0)) {
402784761f3dSRichard Scheffenegger 			limit = tp->sackhint.prr_delivered -
4028e5313869SRichard Scheffenegger 				tp->sackhint.prr_out;
4029*2d05a1c8SRichard Scheffenegger 		} else {
403048396dc7SRichard Scheffenegger 			limit = imax(tp->sackhint.prr_delivered -
4031e5313869SRichard Scheffenegger 				    tp->sackhint.prr_out, del_data) +
4032e5313869SRichard Scheffenegger 				    maxseg;
4033*2d05a1c8SRichard Scheffenegger 		}
403448396dc7SRichard Scheffenegger 		snd_cnt = imin((tp->snd_ssthresh - pipe), limit);
40350e1d7c25SRichard Scheffenegger 	}
403648396dc7SRichard Scheffenegger 	snd_cnt = imax(snd_cnt, 0) / maxseg;
40370e1d7c25SRichard Scheffenegger 	/*
40380e1d7c25SRichard Scheffenegger 	 * Send snd_cnt new data into the network in response to this ack.
40390e1d7c25SRichard Scheffenegger 	 * If there is going to be a SACK retransmission, adjust snd_cwnd
40400e1d7c25SRichard Scheffenegger 	 * accordingly.
40410e1d7c25SRichard Scheffenegger 	 */
4042b9f803b7SRichard Scheffenegger 	if (IN_FASTRECOVERY(tp->t_flags)) {
4043c21b7b55SRichard Scheffenegger 		if (tcp_is_sack_recovery(tp, to)) {
404474d7fc87SRichard Scheffenegger 			tp->snd_cwnd = tp->snd_nxt - tp->snd_recover +
404574d7fc87SRichard Scheffenegger 					    tp->sackhint.sack_bytes_rexmit +
404674d7fc87SRichard Scheffenegger 					    (snd_cnt * maxseg);
404774d7fc87SRichard Scheffenegger 		} else {
404874d7fc87SRichard Scheffenegger 			tp->snd_cwnd = (tp->snd_max - tp->snd_una) +
404974d7fc87SRichard Scheffenegger 					    (snd_cnt * maxseg);
405074d7fc87SRichard Scheffenegger 		}
4051*2d05a1c8SRichard Scheffenegger 	} else if (IN_CONGRECOVERY(tp->t_flags)) {
405274d7fc87SRichard Scheffenegger 		tp->snd_cwnd = pipe - del_data + (snd_cnt * maxseg);
4053*2d05a1c8SRichard Scheffenegger 	}
405474d7fc87SRichard Scheffenegger 	tp->snd_cwnd = imax(maxseg, tp->snd_cwnd);
40550e1d7c25SRichard Scheffenegger }
40560e1d7c25SRichard Scheffenegger 
405746f58482SJonathan Lemon /*
4058c068736aSJeffrey Hsu  * On a partial ack arrives, force the retransmission of the
4059c068736aSJeffrey Hsu  * next unacknowledged segment.  Do not clear tp->t_dupacks.
4060c068736aSJeffrey Hsu  * By setting snd_nxt to ti_ack, this forces retransmission timer to
4061c068736aSJeffrey Hsu  * be started again.
406246f58482SJonathan Lemon  */
406355bceb1eSRandall Stewart void
4064ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th)
406546f58482SJonathan Lemon {
406646f58482SJonathan Lemon 	tcp_seq onxt = tp->snd_nxt;
40673ac12506SJonathan T. Looney 	uint32_t ocwnd = tp->snd_cwnd;
40680c39d38dSGleb Smirnoff 	u_int maxseg = tcp_maxseg(tp);
406946f58482SJonathan Lemon 
40709eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
40717a3244ccSRobert Watson 
4072b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_REXMT, 0);
407346f58482SJonathan Lemon 	tp->t_rtttime = 0;
407446f58482SJonathan Lemon 	tp->snd_nxt = th->th_ack;
40756b2a5f92SJayanth Vijayaraghavan 	/*
4076c068736aSJeffrey Hsu 	 * Set snd_cwnd to one segment beyond acknowledged offset.
4077c068736aSJeffrey Hsu 	 * (tp->snd_una has not yet been updated when this function is called.)
40786b2a5f92SJayanth Vijayaraghavan 	 */
40790c39d38dSGleb Smirnoff 	tp->snd_cwnd = maxseg + BYTES_THIS_ACK(tp, th);
4080a84db8f4SMatthew Dillon 	tp->t_flags |= TF_ACKNOW;
408140fa3e40SGleb Smirnoff 	(void) tcp_output(tp);
408246f58482SJonathan Lemon 	tp->snd_cwnd = ocwnd;
408346f58482SJonathan Lemon 	if (SEQ_GT(onxt, tp->snd_nxt))
408446f58482SJonathan Lemon 		tp->snd_nxt = onxt;
408546f58482SJonathan Lemon 	/*
408646f58482SJonathan Lemon 	 * Partial window deflation.  Relies on fact that tp->snd_una
408746f58482SJonathan Lemon 	 * not updated yet.
408846f58482SJonathan Lemon 	 */
4089dbc42409SLawrence Stewart 	if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th))
4090dbc42409SLawrence Stewart 		tp->snd_cwnd -= BYTES_THIS_ACK(tp, th);
4091d7587117SPaul Saab 	else
4092d7587117SPaul Saab 		tp->snd_cwnd = 0;
40930c39d38dSGleb Smirnoff 	tp->snd_cwnd += maxseg;
409446f58482SJonathan Lemon }
409512eeb81fSHiren Panchasara 
409612eeb81fSHiren Panchasara int
409712eeb81fSHiren Panchasara tcp_compute_pipe(struct tcpcb *tp)
409812eeb81fSHiren Panchasara {
4099e5049a17SRandall Stewart 	if (tp->t_fb->tfb_compute_pipe == NULL) {
410012eeb81fSHiren Panchasara 		return (tp->snd_max - tp->snd_una +
410112eeb81fSHiren Panchasara 			tp->sackhint.sack_bytes_rexmit -
4102e2c6a6d2SRichard Scheffenegger 			tp->sackhint.sacked_bytes -
4103e2c6a6d2SRichard Scheffenegger 			tp->sackhint.lost_bytes);
4104e5049a17SRandall Stewart 	} else {
4105e5049a17SRandall Stewart 		return((*tp->t_fb->tfb_compute_pipe)(tp));
4106e5049a17SRandall Stewart 	}
410712eeb81fSHiren Panchasara }
41087dc90a1dSMichael Tuexen 
41097dc90a1dSMichael Tuexen uint32_t
41107dc90a1dSMichael Tuexen tcp_compute_initwnd(uint32_t maxseg)
41117dc90a1dSMichael Tuexen {
41127dc90a1dSMichael Tuexen 	/*
41137dc90a1dSMichael Tuexen 	 * Calculate the Initial Window, also used as Restart Window
41147dc90a1dSMichael Tuexen 	 *
41157dc90a1dSMichael Tuexen 	 * RFC5681 Section 3.1 specifies the default conservative values.
41167dc90a1dSMichael Tuexen 	 * RFC3390 specifies slightly more aggressive values.
41177dc90a1dSMichael Tuexen 	 * RFC6928 increases it to ten segments.
41187dc90a1dSMichael Tuexen 	 * Support for user specified value for initial flight size.
41197dc90a1dSMichael Tuexen 	 */
41207dc90a1dSMichael Tuexen 	if (V_tcp_initcwnd_segments)
41217dc90a1dSMichael Tuexen 		return min(V_tcp_initcwnd_segments * maxseg,
41227dc90a1dSMichael Tuexen 		    max(2 * maxseg, V_tcp_initcwnd_segments * 1460));
41237dc90a1dSMichael Tuexen 	else if (V_tcp_do_rfc3390)
41247dc90a1dSMichael Tuexen 		return min(4 * maxseg, max(2 * maxseg, 4380));
41257dc90a1dSMichael Tuexen 	else {
41267dc90a1dSMichael Tuexen 		/* Per RFC5681 Section 3.1 */
41277dc90a1dSMichael Tuexen 		if (maxseg > 2190)
41287dc90a1dSMichael Tuexen 			return (2 * maxseg);
41297dc90a1dSMichael Tuexen 		else if (maxseg > 1095)
41307dc90a1dSMichael Tuexen 			return (3 * maxseg);
41317dc90a1dSMichael Tuexen 		else
41327dc90a1dSMichael Tuexen 			return (4 * maxseg);
41337dc90a1dSMichael Tuexen 	}
41347dc90a1dSMichael Tuexen }
4135