xref: /freebsd/sys/netinet/tcp_input.c (revision 2a0d26d793b2ff63d36305aa98047a4bc6a6cd8c)
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>
115c325962bSMike Silbersack #include <netinet/tcp_syncache.h>
11609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
11709fe6320SNavdeep Parhar #include <netinet/tcp_offload.h>
11809fe6320SNavdeep Parhar #endif
119f7220c48SRichard Scheffenegger #include <netinet/tcp_ecn.h>
1209e644c23SMichael Tuexen #include <netinet/udp.h>
121fb59c426SYoshinobu Inoue 
122fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
123b9234fafSSam Leffler 
124db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
125db4f9cc7SJonathan Lemon 
126aed55708SRobert Watson #include <security/mac/mac_framework.h>
127aed55708SRobert Watson 
128dbc42409SLawrence Stewart const int tcprexmtthresh = 3;
1290312fbe9SPoul-Henning Kamp 
130334fc582SBjoern A. Zeeb VNET_DEFINE(int, tcp_log_in_vain) = 0;
131334fc582SBjoern A. Zeeb SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_VNET | CTLFLAG_RW,
132334fc582SBjoern A. Zeeb     &VNET_NAME(tcp_log_in_vain), 0,
133eddfbb76SRobert Watson     "Log all incoming TCP segments to closed ports");
134816a3d83SPoul-Henning Kamp 
1355dc99e9bSMark Johnston VNET_DEFINE(int, tcp_bind_all_fibs) = 1;
1365dc99e9bSMark Johnston SYSCTL_INT(_net_inet_tcp, OID_AUTO, bind_all_fibs, CTLFLAG_VNET | CTLFLAG_RDTUN,
1375dc99e9bSMark Johnston     &VNET_NAME(tcp_bind_all_fibs), 0,
1385dc99e9bSMark Johnston     "Bound sockets receive traffic from all FIBs");
1395dc99e9bSMark Johnston 
14082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, blackhole) = 0;
14182cea7e6SBjoern A. Zeeb #define	V_blackhole		VNET(blackhole)
1426df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW,
143eddfbb76SRobert Watson     &VNET_NAME(blackhole), 0,
144eddfbb76SRobert Watson     "Do not send RST on segments to closed ports");
14516f7f31fSGeoff Rehmet 
1463ea9a7cfSGleb Smirnoff VNET_DEFINE(bool, blackhole_local) = false;
1473ea9a7cfSGleb Smirnoff #define	V_blackhole_local	VNET(blackhole_local)
1483ea9a7cfSGleb Smirnoff SYSCTL_BOOL(_net_inet_tcp, OID_AUTO, blackhole_local, CTLFLAG_VNET |
1493ea9a7cfSGleb Smirnoff     CTLFLAG_RW, &VNET_NAME(blackhole_local), false,
1503ea9a7cfSGleb Smirnoff     "Enforce net.inet.tcp.blackhole for locally originated packets");
1513ea9a7cfSGleb Smirnoff 
15282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_delack_enabled) = 1;
1536df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_VNET | CTLFLAG_RW,
154eddfbb76SRobert Watson     &VNET_NAME(tcp_delack_enabled), 0,
1553d177f46SBill Fumerola     "Delay ACK to try and piggyback it onto a data packet");
156f498eeeeSDavid Greenman 
15782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, drop_synfin) = 0;
1586df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_VNET | CTLFLAG_RW,
159eddfbb76SRobert Watson     &VNET_NAME(drop_synfin), 0,
160eddfbb76SRobert Watson     "Drop TCP packets with SYN+FIN set");
161f8613305SDag-Erling Smørgrav 
1620e1d7c25SRichard Scheffenegger VNET_DEFINE(int, tcp_do_prr) = 1;
1630e1d7c25SRichard Scheffenegger SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_prr, CTLFLAG_VNET | CTLFLAG_RW,
1640e1d7c25SRichard Scheffenegger     &VNET_NAME(tcp_do_prr), 1,
1650e1d7c25SRichard Scheffenegger     "Enable Proportional Rate Reduction per RFC 6937");
1660e1d7c25SRichard Scheffenegger 
167b72e56e7SMichael Tuexen VNET_DEFINE(int, tcp_do_newcwv) = 0;
168b72e56e7SMichael Tuexen SYSCTL_INT(_net_inet_tcp, OID_AUTO, newcwv, CTLFLAG_VNET | CTLFLAG_RW,
169b72e56e7SMichael Tuexen     &VNET_NAME(tcp_do_newcwv), 0,
170b72e56e7SMichael Tuexen     "Enable New Congestion Window Validation per RFC7661");
171b72e56e7SMichael Tuexen 
17282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3042) = 1;
1736df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_VNET | CTLFLAG_RW,
174eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3042), 0,
175eddfbb76SRobert Watson     "Enable RFC 3042 (Limited Transmit)");
176582a954bSJeffrey Hsu 
17782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3390) = 1;
1786df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_VNET | CTLFLAG_RW,
179eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3390), 0,
180da3a8a1aSJeffrey Hsu     "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)");
181da3a8a1aSJeffrey Hsu 
182356c7958SHiren Panchasara VNET_DEFINE(int, tcp_initcwnd_segments) = 10;
183356c7958SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, initcwnd_segments,
184356c7958SHiren Panchasara     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_initcwnd_segments), 0,
185356c7958SHiren Panchasara     "Slow-start flight size (initial congestion window) in number of segments");
18609440655SAndre Oppermann 
18782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3465) = 1;
1886df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_VNET | CTLFLAG_RW,
189eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3465), 0,
19024cb0f22SLawrence Stewart     "Enable RFC 3465 (Appropriate Byte Counting)");
191eddfbb76SRobert Watson 
19282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_abc_l_var) = 2;
1936df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_VNET | CTLFLAG_RW,
194eddfbb76SRobert Watson     &VNET_NAME(tcp_abc_l_var), 2,
19524cb0f22SLawrence Stewart     "Cap the max cwnd increment during slow-start to this number of segments");
19624cb0f22SLawrence Stewart 
1973220a212SGleb Smirnoff VNET_DEFINE(int, tcp_insecure_syn) = 0;
1986df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_syn, CTLFLAG_VNET | CTLFLAG_RW,
1993220a212SGleb Smirnoff     &VNET_NAME(tcp_insecure_syn), 0,
2003220a212SGleb Smirnoff     "Follow RFC793 instead of RFC5961 criteria for accepting SYN packets");
2013220a212SGleb Smirnoff 
20282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_insecure_rst) = 0;
2036df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_VNET | CTLFLAG_RW,
204eddfbb76SRobert Watson     &VNET_NAME(tcp_insecure_rst), 0,
2053220a212SGleb Smirnoff     "Follow RFC793 instead of RFC5961 criteria for accepting RST packets");
206a69968eeSMike Silbersack 
207646c28eaSMichael Tuexen VNET_DEFINE(int, tcp_insecure_ack) = 0;
208646c28eaSMichael Tuexen SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_ack, CTLFLAG_VNET | CTLFLAG_RW,
209646c28eaSMichael Tuexen     &VNET_NAME(tcp_insecure_ack), 0,
210646c28eaSMichael Tuexen     "Follow RFC793 criteria for validating SEG.ACK");
211646c28eaSMichael Tuexen 
212fba0cea1SBjoern A. Zeeb VNET_DEFINE(int, tcp_recvspace) = 1024*64;
213873789cbSAndre Oppermann #define	V_tcp_recvspace	VNET(tcp_recvspace)
2146df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_VNET | CTLFLAG_RW,
215873789cbSAndre Oppermann     &VNET_NAME(tcp_recvspace), 0, "Initial receive socket buffer size");
216873789cbSAndre Oppermann 
21782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autorcvbuf) = 1;
2186df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_VNET | CTLFLAG_RW,
219eddfbb76SRobert Watson     &VNET_NAME(tcp_do_autorcvbuf), 0,
2208b615593SMarko Zec     "Enable automatic receive buffer sizing");
2216741ecf5SAndre Oppermann 
222b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024;
2236df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_VNET | CTLFLAG_RW,
224eddfbb76SRobert Watson     &VNET_NAME(tcp_autorcvbuf_max), 0,
2258b615593SMarko Zec     "Max size of automatic receive buffer");
2266741ecf5SAndre Oppermann 
22782cea7e6SBjoern A. Zeeb VNET_DEFINE(struct inpcbinfo, tcbinfo);
228df8bae1dSRodney W. Grimes 
229315e3e38SRobert Watson /*
230bf840a17SGleb Smirnoff  * TCP statistics are stored in an array of counter(9)s, which size matches
231bf840a17SGleb Smirnoff  * size of struct tcpstat.  TCP running connection count is a regular array.
2325923c293SGleb Smirnoff  */
2335da0521fSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct tcpstat, tcpstat);
2345da0521fSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_tcp, TCPCTL_STATS, stats, struct tcpstat,
2355da0521fSAndrey V. Elsukov     tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
236bf840a17SGleb Smirnoff VNET_DEFINE(counter_u64_t, tcps_states[TCP_NSTATES]);
237bf840a17SGleb Smirnoff SYSCTL_COUNTER_U64_ARRAY(_net_inet_tcp, TCPCTL_STATES, states, CTLFLAG_RD |
238d4d32b9fSHans Petter Selasky     CTLFLAG_VNET, &VNET_NAME(tcps_states)[0], TCP_NSTATES,
239bf840a17SGleb Smirnoff     "TCP connection counts by TCP state");
240bf840a17SGleb Smirnoff 
2415923c293SGleb Smirnoff /*
2427ca6e296SMichael Tuexen  * Kernel module interface for updating tcpstat.  The first argument is an index
2435923c293SGleb Smirnoff  * into tcpstat treated as an array.
244315e3e38SRobert Watson  */
245315e3e38SRobert Watson void
kmod_tcpstat_add(int statnum,int val)2467ca6e296SMichael Tuexen kmod_tcpstat_add(int statnum, int val)
247315e3e38SRobert Watson {
248315e3e38SRobert Watson 
2497ca6e296SMichael Tuexen 	counter_u64_add(VNET(tcpstat)[statnum], val);
250315e3e38SRobert Watson }
251315e3e38SRobert Watson 
252c21b7b55SRichard Scheffenegger /*
253c21b7b55SRichard Scheffenegger  * Make sure that we only start a SACK loss recovery when
254c21b7b55SRichard Scheffenegger  * receiving a duplicate ACK with a SACK block, and also
255c21b7b55SRichard Scheffenegger  * complete SACK loss recovery in case the other end
256c21b7b55SRichard Scheffenegger  * reneges.
257c21b7b55SRichard Scheffenegger  */
258c21b7b55SRichard Scheffenegger static bool inline
tcp_is_sack_recovery(struct tcpcb * tp,struct tcpopt * to)259c21b7b55SRichard Scheffenegger tcp_is_sack_recovery(struct tcpcb *tp, struct tcpopt *to)
260c21b7b55SRichard Scheffenegger {
261c21b7b55SRichard Scheffenegger 	return ((tp->t_flags & TF_SACK_PERMIT) &&
262c21b7b55SRichard Scheffenegger 		((to->to_flags & TOF_SACK) ||
263c21b7b55SRichard Scheffenegger 		(!TAILQ_EMPTY(&tp->snd_holes))));
264c21b7b55SRichard Scheffenegger }
265c21b7b55SRichard Scheffenegger 
266bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
267dbc42409SLawrence Stewart /*
26839bc9de5SLawrence Stewart  * Wrapper for the TCP established input helper hook.
26939bc9de5SLawrence Stewart  */
27055bceb1eSRandall Stewart void
hhook_run_tcp_est_in(struct tcpcb * tp,struct tcphdr * th,struct tcpopt * to)27139bc9de5SLawrence Stewart hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to)
27239bc9de5SLawrence Stewart {
27339bc9de5SLawrence Stewart 	struct tcp_hhook_data hhook_data;
27439bc9de5SLawrence Stewart 
27539bc9de5SLawrence Stewart 	if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) {
27639bc9de5SLawrence Stewart 		hhook_data.tp = tp;
27739bc9de5SLawrence Stewart 		hhook_data.th = th;
27839bc9de5SLawrence Stewart 		hhook_data.to = to;
27939bc9de5SLawrence Stewart 
28039bc9de5SLawrence Stewart 		hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data,
281e68b3792SGleb Smirnoff 		    &tp->t_osd);
28239bc9de5SLawrence Stewart 	}
28339bc9de5SLawrence Stewart }
284bd79708dSJonathan T. Looney #endif
28539bc9de5SLawrence Stewart 
28639bc9de5SLawrence Stewart /*
287dbc42409SLawrence Stewart  * CC wrapper hook functions
288dbc42409SLawrence Stewart  */
28955bceb1eSRandall Stewart void
cc_ack_received(struct tcpcb * tp,struct tcphdr * th,uint16_t nsegs,uint16_t type)2904b7b743cSLawrence Stewart cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t nsegs,
2914b7b743cSLawrence Stewart     uint16_t type)
292f2512ba1SRui Paulo {
293adc56f5aSEdward Tomasz Napierala #ifdef STATS
294adc56f5aSEdward Tomasz Napierala 	int32_t gput;
295adc56f5aSEdward Tomasz Napierala #endif
296adc56f5aSEdward Tomasz Napierala 
2979eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
298f2512ba1SRui Paulo 
299e68b3792SGleb Smirnoff 	tp->t_ccv.nsegs = nsegs;
300e68b3792SGleb Smirnoff 	tp->t_ccv.bytes_this_ack = BYTES_THIS_ACK(tp, th);
301b72e56e7SMichael Tuexen 	if ((!V_tcp_do_newcwv && (tp->snd_cwnd <= tp->snd_wnd)) ||
302b72e56e7SMichael Tuexen 	    (V_tcp_do_newcwv && (tp->snd_cwnd <= tp->snd_wnd) &&
303b72e56e7SMichael Tuexen 	     (tp->snd_cwnd < (tcp_compute_pipe(tp) * 2))))
304e68b3792SGleb Smirnoff 		tp->t_ccv.flags |= CCF_CWND_LIMITED;
305dbc42409SLawrence Stewart 	else
306e68b3792SGleb Smirnoff 		tp->t_ccv.flags &= ~CCF_CWND_LIMITED;
307dbc42409SLawrence Stewart 
308dbc42409SLawrence Stewart 	if (type == CC_ACK) {
309adc56f5aSEdward Tomasz Napierala #ifdef STATS
310adc56f5aSEdward Tomasz Napierala 		stats_voi_update_abs_s32(tp->t_stats, VOI_TCP_CALCFRWINDIFF,
311adc56f5aSEdward Tomasz Napierala 		    ((int32_t)tp->snd_cwnd) - tp->snd_wnd);
312adc56f5aSEdward Tomasz Napierala 		if (!IN_RECOVERY(tp->t_flags))
313adc56f5aSEdward Tomasz Napierala 			stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_ACKLEN,
314e68b3792SGleb Smirnoff 			   tp->t_ccv.bytes_this_ack / (tcp_maxseg(tp) * nsegs));
315adc56f5aSEdward Tomasz Napierala 		if ((tp->t_flags & TF_GPUTINPROG) &&
316adc56f5aSEdward Tomasz Napierala 		    SEQ_GEQ(th->th_ack, tp->gput_ack)) {
317adc56f5aSEdward Tomasz Napierala 			/*
318adc56f5aSEdward Tomasz Napierala 			 * Compute goodput in bits per millisecond.
319adc56f5aSEdward Tomasz Napierala 			 */
320f5766992SHans Petter Selasky 			gput = (((int64_t)SEQ_SUB(th->th_ack, tp->gput_seq)) << 3) /
321adc56f5aSEdward Tomasz Napierala 			    max(1, tcp_ts_getticks() - tp->gput_ts);
322adc56f5aSEdward Tomasz Napierala 			stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT,
323adc56f5aSEdward Tomasz Napierala 			    gput);
324adc56f5aSEdward Tomasz Napierala 			/*
325adc56f5aSEdward Tomasz Napierala 			 * XXXLAS: This is a temporary hack, and should be
326adc56f5aSEdward Tomasz Napierala 			 * chained off VOI_TCP_GPUT when stats(9) grows an API
327adc56f5aSEdward Tomasz Napierala 			 * to deal with chained VOIs.
328adc56f5aSEdward Tomasz Napierala 			 */
329adc56f5aSEdward Tomasz Napierala 			if (tp->t_stats_gput_prev > 0)
330adc56f5aSEdward Tomasz Napierala 				stats_voi_update_abs_s32(tp->t_stats,
331adc56f5aSEdward Tomasz Napierala 				    VOI_TCP_GPUT_ND,
332adc56f5aSEdward Tomasz Napierala 				    ((gput - tp->t_stats_gput_prev) * 100) /
333adc56f5aSEdward Tomasz Napierala 				    tp->t_stats_gput_prev);
334adc56f5aSEdward Tomasz Napierala 			tp->t_flags &= ~TF_GPUTINPROG;
335adc56f5aSEdward Tomasz Napierala 			tp->t_stats_gput_prev = gput;
336adc56f5aSEdward Tomasz Napierala 		}
337adc56f5aSEdward Tomasz Napierala #endif /* STATS */
338dbc42409SLawrence Stewart 		if (tp->snd_cwnd > tp->snd_ssthresh) {
339e68b3792SGleb Smirnoff 			tp->t_bytes_acked += tp->t_ccv.bytes_this_ack;
340dbc42409SLawrence Stewart 			if (tp->t_bytes_acked >= tp->snd_cwnd) {
341dbc42409SLawrence Stewart 				tp->t_bytes_acked -= tp->snd_cwnd;
342e68b3792SGleb Smirnoff 				tp->t_ccv.flags |= CCF_ABC_SENTAWND;
343dbc42409SLawrence Stewart 			}
344dbc42409SLawrence Stewart 		} else {
345e68b3792SGleb Smirnoff 				tp->t_ccv.flags &= ~CCF_ABC_SENTAWND;
346dbc42409SLawrence Stewart 				tp->t_bytes_acked = 0;
347dbc42409SLawrence Stewart 		}
348dbc42409SLawrence Stewart 	}
349dbc42409SLawrence Stewart 
350dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->ack_received != NULL) {
351dbc42409SLawrence Stewart 		/* XXXLAS: Find a way to live without this */
352e68b3792SGleb Smirnoff 		tp->t_ccv.curack = th->th_ack;
353e68b3792SGleb Smirnoff 		CC_ALGO(tp)->ack_received(&tp->t_ccv, type);
354dbc42409SLawrence Stewart 	}
355adc56f5aSEdward Tomasz Napierala #ifdef STATS
356adc56f5aSEdward Tomasz Napierala 	stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_LCWIN, tp->snd_cwnd);
357adc56f5aSEdward Tomasz Napierala #endif
358dbc42409SLawrence Stewart }
359dbc42409SLawrence Stewart 
36055bceb1eSRandall Stewart void
cc_conn_init(struct tcpcb * tp)361dbc42409SLawrence Stewart cc_conn_init(struct tcpcb *tp)
362dbc42409SLawrence Stewart {
363dbc42409SLawrence Stewart 	struct hc_metrics_lite metrics;
3649eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
3650c39d38dSGleb Smirnoff 	u_int maxseg;
366dbc42409SLawrence Stewart 	int rtt;
367dbc42409SLawrence Stewart 
3689eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
369dbc42409SLawrence Stewart 
370dbc42409SLawrence Stewart 	tcp_hc_get(&inp->inp_inc, &metrics);
3710c39d38dSGleb Smirnoff 	maxseg = tcp_maxseg(tp);
372dbc42409SLawrence Stewart 
37309000cc1SGleb Smirnoff 	if (tp->t_srtt == 0 && (rtt = metrics.hc_rtt)) {
374dbc42409SLawrence Stewart 		tp->t_srtt = rtt;
375dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_usedrtt);
37609000cc1SGleb Smirnoff 		if (metrics.hc_rttvar) {
37709000cc1SGleb Smirnoff 			tp->t_rttvar = metrics.hc_rttvar;
378dbc42409SLawrence Stewart 			TCPSTAT_INC(tcps_usedrttvar);
379dbc42409SLawrence Stewart 		} else {
380dbc42409SLawrence Stewart 			/* default variation is +- 1 rtt */
381dbc42409SLawrence Stewart 			tp->t_rttvar =
382dbc42409SLawrence Stewart 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
383dbc42409SLawrence Stewart 		}
384dbc42409SLawrence Stewart 		TCPT_RANGESET(tp->t_rxtcur,
385dbc42409SLawrence Stewart 		    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
386dbc42409SLawrence Stewart 		    tp->t_rttmin, TCPTV_REXMTMAX);
387dbc42409SLawrence Stewart 	}
38809000cc1SGleb Smirnoff 	if (metrics.hc_ssthresh) {
389dbc42409SLawrence Stewart 		/*
390dbc42409SLawrence Stewart 		 * There's some sort of gateway or interface
391dbc42409SLawrence Stewart 		 * buffer limit on the path.  Use this to set
392a4641f4eSPedro F. Giffuni 		 * the slow start threshold, but set the
393dbc42409SLawrence Stewart 		 * threshold to no less than 2*mss.
394dbc42409SLawrence Stewart 		 */
39509000cc1SGleb Smirnoff 		tp->snd_ssthresh = max(2 * maxseg, metrics.hc_ssthresh);
396dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_usedssthresh);
397dbc42409SLawrence Stewart 	}
398dbc42409SLawrence Stewart 
399dbc42409SLawrence Stewart 	/*
4009ec4a4ccSAndre Oppermann 	 * Set the initial slow-start flight size.
401dbc42409SLawrence Stewart 	 *
402cf8f04f4SAndre Oppermann 	 * If a SYN or SYN/ACK was lost and retransmitted, we have to
403cf8f04f4SAndre Oppermann 	 * reduce the initial CWND to one segment as congestion is likely
404cf8f04f4SAndre Oppermann 	 * requiring us to be cautious.
405dbc42409SLawrence Stewart 	 */
406cf8f04f4SAndre Oppermann 	if (tp->snd_cwnd == 1)
4070c39d38dSGleb Smirnoff 		tp->snd_cwnd = maxseg;		/* SYN(-ACK) lost */
408dbc42409SLawrence Stewart 	else
4097dc90a1dSMichael Tuexen 		tp->snd_cwnd = tcp_compute_initwnd(maxseg);
410dbc42409SLawrence Stewart 
411dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->conn_init != NULL)
412e68b3792SGleb Smirnoff 		CC_ALGO(tp)->conn_init(&tp->t_ccv);
413dbc42409SLawrence Stewart }
414dbc42409SLawrence Stewart 
415dbc42409SLawrence Stewart void inline
cc_cong_signal(struct tcpcb * tp,struct tcphdr * th,uint32_t type)416dbc42409SLawrence Stewart cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type)
417dbc42409SLawrence Stewart {
4189eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
419dbc42409SLawrence Stewart 
420adc56f5aSEdward Tomasz Napierala #ifdef STATS
421adc56f5aSEdward Tomasz Napierala 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_CSIG, type);
422adc56f5aSEdward Tomasz Napierala #endif
423adc56f5aSEdward Tomasz Napierala 
424dbc42409SLawrence Stewart 	switch(type) {
425dbc42409SLawrence Stewart 	case CC_NDUPACK:
426dbc42409SLawrence Stewart 		if (!IN_FASTRECOVERY(tp->t_flags)) {
427f2512ba1SRui Paulo 			tp->snd_recover = tp->snd_max;
4283cf38784SMichael Tuexen 			if (tp->t_flags2 & TF2_ECN_PERMIT)
4293cf38784SMichael Tuexen 				tp->t_flags2 |= TF2_ECN_SND_CWR;
430f2512ba1SRui Paulo 		}
431dbc42409SLawrence Stewart 		break;
432dbc42409SLawrence Stewart 	case CC_ECN:
433af2fb894SRichard Scheffenegger 		if (!IN_CONGRECOVERY(tp->t_flags) ||
434af2fb894SRichard Scheffenegger 		    /*
435af2fb894SRichard Scheffenegger 		     * Allow ECN reaction on ACK to CWR, if
436af2fb894SRichard Scheffenegger 		     * that data segment was also CE marked.
437af2fb894SRichard Scheffenegger 		     */
438af2fb894SRichard Scheffenegger 		    SEQ_GEQ(th->th_ack, tp->snd_recover)) {
439af2fb894SRichard Scheffenegger 			EXIT_CONGRECOVERY(tp->t_flags);
440dbc42409SLawrence Stewart 			TCPSTAT_INC(tcps_ecn_rcwnd);
441af2fb894SRichard Scheffenegger 			tp->snd_recover = tp->snd_max + 1;
4423cf38784SMichael Tuexen 			if (tp->t_flags2 & TF2_ECN_PERMIT)
4433cf38784SMichael Tuexen 				tp->t_flags2 |= TF2_ECN_SND_CWR;
444dbc42409SLawrence Stewart 		}
445dbc42409SLawrence Stewart 		break;
446dbc42409SLawrence Stewart 	case CC_RTO:
447dbc42409SLawrence Stewart 		tp->t_dupacks = 0;
448dbc42409SLawrence Stewart 		tp->t_bytes_acked = 0;
449dbc42409SLawrence Stewart 		EXIT_RECOVERY(tp->t_flags);
4509cc711c9SMichael Tuexen 		if (tp->t_flags2 & TF2_ECN_PERMIT)
4519cc711c9SMichael Tuexen 			tp->t_flags2 |= TF2_ECN_SND_CWR;
452dbc42409SLawrence Stewart 		break;
453dbc42409SLawrence Stewart 	case CC_RTO_ERR:
454dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_sndrexmitbad);
455dbc42409SLawrence Stewart 		/* RTO was unnecessary, so reset everything. */
456dbc42409SLawrence Stewart 		tp->snd_cwnd = tp->snd_cwnd_prev;
457dbc42409SLawrence Stewart 		tp->snd_ssthresh = tp->snd_ssthresh_prev;
458dbc42409SLawrence Stewart 		tp->snd_recover = tp->snd_recover_prev;
459dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASFRECOVERY)
460dbc42409SLawrence Stewart 			ENTER_FASTRECOVERY(tp->t_flags);
461dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASCRECOVERY)
462dbc42409SLawrence Stewart 			ENTER_CONGRECOVERY(tp->t_flags);
463dbc42409SLawrence Stewart 		tp->snd_nxt = tp->snd_max;
464672dc4aeSJohn Baldwin 		tp->t_flags &= ~TF_PREVVALID;
4656f6c0781SRichard Scheffenegger 		tp->t_rxtshift = 0;
466dbc42409SLawrence Stewart 		tp->t_badrxtwin = 0;
467dbc42409SLawrence Stewart 		break;
468dbc42409SLawrence Stewart 	}
469fcea1cc9SRichard Scheffenegger 	if (SEQ_LT(tp->snd_fack, tp->snd_una) ||
470fcea1cc9SRichard Scheffenegger 	    SEQ_GT(tp->snd_fack, tp->snd_max)) {
471fcea1cc9SRichard Scheffenegger 		tp->snd_fack = tp->snd_una;
472fcea1cc9SRichard Scheffenegger 	}
473dbc42409SLawrence Stewart 
474dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->cong_signal != NULL) {
475dbc42409SLawrence Stewart 		if (th != NULL)
476e68b3792SGleb Smirnoff 			tp->t_ccv.curack = th->th_ack;
477e68b3792SGleb Smirnoff 		CC_ALGO(tp)->cong_signal(&tp->t_ccv, type);
478dbc42409SLawrence Stewart 	}
479dbc42409SLawrence Stewart }
480dbc42409SLawrence Stewart 
48155bceb1eSRandall Stewart void inline
cc_post_recovery(struct tcpcb * tp,struct tcphdr * th)482dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th)
483dbc42409SLawrence Stewart {
4849eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
485dbc42409SLawrence Stewart 
486dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->post_recovery != NULL) {
48740fdc6d2SRichard Scheffenegger 		if (SEQ_LT(tp->snd_fack, th->th_ack) ||
48840fdc6d2SRichard Scheffenegger 		    SEQ_GT(tp->snd_fack, tp->snd_max)) {
48940fdc6d2SRichard Scheffenegger 			tp->snd_fack = th->th_ack;
49040fdc6d2SRichard Scheffenegger 		}
491e68b3792SGleb Smirnoff 		tp->t_ccv.curack = th->th_ack;
492e68b3792SGleb Smirnoff 		CC_ALGO(tp)->post_recovery(&tp->t_ccv);
493dbc42409SLawrence Stewart 	}
4940b3f9e43SRichard Scheffenegger 	EXIT_RECOVERY(tp->t_flags);
4950b3f9e43SRichard Scheffenegger 
496dbc42409SLawrence Stewart 	tp->t_bytes_acked = 0;
49748be5b97SRichard Scheffenegger 	tp->sackhint.delivered_data = 0;
498429f14f8SRichard Scheffenegger 	tp->sackhint.prr_delivered = 0;
499e5313869SRichard Scheffenegger 	tp->sackhint.prr_out = 0;
500dbc42409SLawrence Stewart }
5010312fbe9SPoul-Henning Kamp 
502df8bae1dSRodney W. Grimes /*
503262c1c1aSMatthew Dillon  * Indicate whether this ack should be delayed.  We can delay the ack if
50485536381SHiren Panchasara  * following conditions are met:
50585536381SHiren Panchasara  *	- There is no delayed ack timer in progress.
50685536381SHiren Panchasara  *	- Our last ack wasn't a 0-sized window. We never want to delay
50785536381SHiren Panchasara  *	  the ack that opens up a 0-sized window.
50885536381SHiren Panchasara  *	- LRO wasn't used for this segment. We make sure by checking that the
50985536381SHiren Panchasara  *	  segment size is not larger than the MSS.
510d8c85a26SJonathan Lemon  */
511c1e5a6e5SAndre Oppermann #define DELAY_ACK(tp, tlen)						\
512b8152ba7SAndre Oppermann 	((!tcp_timer_active(tp, TT_DELACK) &&				\
513a14c749fSJonathan Lemon 	    (tp->t_flags & TF_RXWIN0SENT) == 0) &&			\
5140c39d38dSGleb Smirnoff 	    (tlen <= tp->t_maxseg) &&					\
515603724d3SBjoern A. Zeeb 	    (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN)))
516d8c85a26SJonathan Lemon 
5174ad24737SRandall Stewart void inline
cc_ecnpkt_handler_flags(struct tcpcb * tp,uint16_t flags,uint8_t iptos)5185d8fd932SRandall Stewart cc_ecnpkt_handler_flags(struct tcpcb *tp, uint16_t flags, uint8_t iptos)
51964807b30SHiren Panchasara {
5209eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
52164807b30SHiren Panchasara 
52264807b30SHiren Panchasara 	if (CC_ALGO(tp)->ecnpkt_handler != NULL) {
52364807b30SHiren Panchasara 		switch (iptos & IPTOS_ECN_MASK) {
52464807b30SHiren Panchasara 		case IPTOS_ECN_CE:
525e68b3792SGleb Smirnoff 			tp->t_ccv.flags |= CCF_IPHDR_CE;
52664807b30SHiren Panchasara 			break;
52764807b30SHiren Panchasara 		case IPTOS_ECN_ECT0:
52883a2839fSMichael Tuexen 			/* FALLTHROUGH */
52964807b30SHiren Panchasara 		case IPTOS_ECN_ECT1:
53083a2839fSMichael Tuexen 			/* FALLTHROUGH */
53183a2839fSMichael Tuexen 		case IPTOS_ECN_NOTECT:
532e68b3792SGleb Smirnoff 			tp->t_ccv.flags &= ~CCF_IPHDR_CE;
53364807b30SHiren Panchasara 			break;
53464807b30SHiren Panchasara 		}
53564807b30SHiren Panchasara 
5365d8fd932SRandall Stewart 		if (flags & TH_CWR)
537e68b3792SGleb Smirnoff 			tp->t_ccv.flags |= CCF_TCPHDR_CWR;
53864807b30SHiren Panchasara 		else
539e68b3792SGleb Smirnoff 			tp->t_ccv.flags &= ~CCF_TCPHDR_CWR;
54064807b30SHiren Panchasara 
541e68b3792SGleb Smirnoff 		CC_ALGO(tp)->ecnpkt_handler(&tp->t_ccv);
54264807b30SHiren Panchasara 
543e68b3792SGleb Smirnoff 		if (tp->t_ccv.flags & CCF_ACKNOW) {
54464807b30SHiren Panchasara 			tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
545e11c9783SMichael Tuexen 			tp->t_flags |= TF_ACKNOW;
546e11c9783SMichael Tuexen 		}
54764807b30SHiren Panchasara 	}
54864807b30SHiren Panchasara }
54964807b30SHiren Panchasara 
5505d8fd932SRandall Stewart void inline
cc_ecnpkt_handler(struct tcpcb * tp,struct tcphdr * th,uint8_t iptos)5515d8fd932SRandall Stewart cc_ecnpkt_handler(struct tcpcb *tp, struct tcphdr *th, uint8_t iptos)
5525d8fd932SRandall Stewart {
5531ebf4607SRichard Scheffenegger 	cc_ecnpkt_handler_flags(tp, tcp_get_flags(th), iptos);
5545d8fd932SRandall Stewart }
5555d8fd932SRandall Stewart 
556df8bae1dSRodney W. Grimes /*
5578d573cc1SAndre Oppermann  * TCP input handling is split into multiple parts:
5588d573cc1SAndre Oppermann  *   tcp6_input is a thin wrapper around tcp_input for the extended
5598d573cc1SAndre Oppermann  *	ip6_protox[] call format in ip6_input
5608d573cc1SAndre Oppermann  *   tcp_input handles primary segment validation, inpcb lookup and
5618d573cc1SAndre Oppermann  *	SYN processing on listen sockets
5628d573cc1SAndre Oppermann  *   tcp_do_segment processes the ACK and text of the segment for
5638d573cc1SAndre Oppermann  *	establishing, established and closing connections
564df8bae1dSRodney W. Grimes  */
565fb59c426SYoshinobu Inoue #ifdef INET6
566fb59c426SYoshinobu Inoue int
tcp6_input_with_port(struct mbuf ** mp,int * offp,int proto,uint16_t port)5679e644c23SMichael Tuexen tcp6_input_with_port(struct mbuf **mp, int *offp, int proto, uint16_t port)
568fb59c426SYoshinobu Inoue {
569503f4e47SBjoern A. Zeeb 	struct mbuf *m;
57033841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6;
5713e88eb90SAndrey V. Elsukov 	struct ip6_hdr *ip6;
572fb59c426SYoshinobu Inoue 
573503f4e47SBjoern A. Zeeb 	m = *mp;
574a4adf6ccSBjoern A. Zeeb 	if (m->m_len < *offp + sizeof(struct tcphdr)) {
5754e619b17SBjoern A. Zeeb 		m = m_pullup(m, *offp + sizeof(struct tcphdr));
5764e619b17SBjoern A. Zeeb 		if (m == NULL) {
5774e619b17SBjoern A. Zeeb 			*mp = m;
5784e619b17SBjoern A. Zeeb 			TCPSTAT_INC(tcps_rcvshort);
5794e619b17SBjoern A. Zeeb 			return (IPPROTO_DONE);
5804e619b17SBjoern A. Zeeb 		}
581a4adf6ccSBjoern A. Zeeb 	}
582fb59c426SYoshinobu Inoue 
583fb59c426SYoshinobu Inoue 	/*
584fb59c426SYoshinobu Inoue 	 * draft-itojun-ipv6-tcp-to-anycast
585fb59c426SYoshinobu Inoue 	 * better place to put this in?
586fb59c426SYoshinobu Inoue 	 */
5873e88eb90SAndrey V. Elsukov 	ip6 = mtod(m, struct ip6_hdr *);
5888268d82cSAlexander V. Chernikov 	ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false);
58933841545SHajimu UMEMOTO 	if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
590fb59c426SYoshinobu Inoue 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
591fb59c426SYoshinobu Inoue 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
592a8fe77d8SBjoern A. Zeeb 		*mp = NULL;
5938f5a8818SKevin Lo 		return (IPPROTO_DONE);
594fb59c426SYoshinobu Inoue 	}
595fb59c426SYoshinobu Inoue 
596a8fe77d8SBjoern A. Zeeb 	*mp = m;
5979e644c23SMichael Tuexen 	return (tcp_input_with_port(mp, offp, proto, port));
5989e644c23SMichael Tuexen }
5999e644c23SMichael Tuexen 
6009e644c23SMichael Tuexen int
tcp6_input(struct mbuf ** mp,int * offp,int proto)6019e644c23SMichael Tuexen tcp6_input(struct mbuf **mp, int *offp, int proto)
6029e644c23SMichael Tuexen {
6039e644c23SMichael Tuexen 
6049e644c23SMichael Tuexen 	return(tcp6_input_with_port(mp, offp, proto, 0));
605fb59c426SYoshinobu Inoue }
606b287c6c7SBjoern A. Zeeb #endif /* INET6 */
607fb59c426SYoshinobu Inoue 
6088f5a8818SKevin Lo int
tcp_input_with_port(struct mbuf ** mp,int * offp,int proto,uint16_t port)6099e644c23SMichael Tuexen tcp_input_with_port(struct mbuf **mp, int *offp, int proto, uint16_t port)
610df8bae1dSRodney W. Grimes {
6118f5a8818SKevin Lo 	struct mbuf *m = *mp;
612b287c6c7SBjoern A. Zeeb 	struct tcphdr *th = NULL;
613ad3f9ab3SAndre Oppermann 	struct ip *ip = NULL;
614ad3f9ab3SAndre Oppermann 	struct inpcb *inp = NULL;
615302ce8d6SAndre Oppermann 	struct tcpcb *tp = NULL;
616302ce8d6SAndre Oppermann 	struct socket *so = NULL;
617e79adb8eSGarrett Wollman 	u_char *optp = NULL;
6188f5a8818SKevin Lo 	int off0;
61926f9a767SRodney W. Grimes 	int optlen = 0;
620b287c6c7SBjoern A. Zeeb #ifdef INET
621b287c6c7SBjoern A. Zeeb 	int len;
622c4556b2fSAndrey V. Elsukov 	uint8_t ipttl;
623b287c6c7SBjoern A. Zeeb #endif
624b287c6c7SBjoern A. Zeeb 	int tlen = 0, off;
625fb59c426SYoshinobu Inoue 	int drop_hdrlen;
626ad3f9ab3SAndre Oppermann 	int thflags;
627302ce8d6SAndre Oppermann 	int rstreason = 0;	/* For badport_bandlim accounting purposes */
62808d9c920SGleb Smirnoff 	int lookupflag;
6291d7ee746SKurt Lidl 	uint8_t iptos;
630c1de64a4SAndrey V. Elsukov 	struct m_tag *fwd_tag = NULL;
631c068736aSJeffrey Hsu #ifdef INET6
632302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6 = NULL;
633c068736aSJeffrey Hsu 	int isipv6;
634c068736aSJeffrey Hsu #else
635a160e630SAndre Oppermann 	const void *ip6 = NULL;
636b287c6c7SBjoern A. Zeeb #endif /* INET6 */
637302ce8d6SAndre Oppermann 	struct tcpopt to;		/* options in this segment */
638a160e630SAndre Oppermann 	char *s = NULL;			/* address and port logging */
639c068736aSJeffrey Hsu 
640d40c0d47SGleb Smirnoff 	NET_EPOCH_ASSERT();
641d40c0d47SGleb Smirnoff 
642fb59c426SYoshinobu Inoue #ifdef INET6
643fb59c426SYoshinobu Inoue 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
644fb59c426SYoshinobu Inoue #endif
645a0292f23SGarrett Wollman 
6468f5a8818SKevin Lo 	off0 = *offp;
6478f5a8818SKevin Lo 	m = *mp;
6488f5a8818SKevin Lo 	*mp = NULL;
649302ce8d6SAndre Oppermann 	to.to_flags = 0;
65078b50714SRobert Watson 	TCPSTAT_INC(tcps_rcvtotal);
651fb59c426SYoshinobu Inoue 
652513f2e2eSGleb Smirnoff 	m->m_pkthdr.tcp_tun_port = port;
65304d3a452SHajimu UMEMOTO #ifdef INET6
654b287c6c7SBjoern A. Zeeb 	if (isipv6) {
655fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
65645747ba5SBjoern A. Zeeb 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
657fb59c426SYoshinobu Inoue 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
6589e644c23SMichael Tuexen 		if (port)
6599e644c23SMichael Tuexen 			goto skip6_csum;
660356ab07eSBjoern A. Zeeb 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) {
66145747ba5SBjoern A. Zeeb 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
66245747ba5SBjoern A. Zeeb 				th->th_sum = m->m_pkthdr.csum_data;
66345747ba5SBjoern A. Zeeb 			else
66445747ba5SBjoern A. Zeeb 				th->th_sum = in6_cksum_pseudo(ip6, tlen,
66545747ba5SBjoern A. Zeeb 				    IPPROTO_TCP, m->m_pkthdr.csum_data);
66645747ba5SBjoern A. Zeeb 			th->th_sum ^= 0xffff;
66745747ba5SBjoern A. Zeeb 		} else
66845747ba5SBjoern A. Zeeb 			th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen);
66945747ba5SBjoern A. Zeeb 		if (th->th_sum) {
67078b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
671fb59c426SYoshinobu Inoue 			goto drop;
672fb59c426SYoshinobu Inoue 		}
6739e644c23SMichael Tuexen 	skip6_csum:
67433841545SHajimu UMEMOTO 		/*
67533841545SHajimu UMEMOTO 		 * Be proactive about unspecified IPv6 address in source.
67633841545SHajimu UMEMOTO 		 * As we use all-zero to indicate unbounded/unconnected pcb,
67733841545SHajimu UMEMOTO 		 * unspecified IPv6 address can be used to confuse us.
67833841545SHajimu UMEMOTO 		 *
67933841545SHajimu UMEMOTO 		 * Note that packets with unspecified IPv6 destination is
68033841545SHajimu UMEMOTO 		 * already dropped in ip6_input.
68133841545SHajimu UMEMOTO 		 */
682713264f6SMark Johnston 		KASSERT(!IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst),
683713264f6SMark Johnston 		    ("%s: unspecified destination v6 address", __func__));
68433841545SHajimu UMEMOTO 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
68578e6c3aaSGleb Smirnoff 			IP6STAT_INC(ip6s_badscope); /* XXX */
68633841545SHajimu UMEMOTO 			goto drop;
68733841545SHajimu UMEMOTO 		}
688bb4a7d94SKristof Provost 		iptos = IPV6_TRAFFIC_CLASS(ip6);
689b287c6c7SBjoern A. Zeeb 	}
69004d3a452SHajimu UMEMOTO #endif
691b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
692b287c6c7SBjoern A. Zeeb 	else
693b287c6c7SBjoern A. Zeeb #endif
694b287c6c7SBjoern A. Zeeb #ifdef INET
695b287c6c7SBjoern A. Zeeb 	{
696df8bae1dSRodney W. Grimes 		/*
697df8bae1dSRodney W. Grimes 		 * Get IP and TCP header together in first mbuf.
698df8bae1dSRodney W. Grimes 		 * Note: IP leaves IP header in first mbuf.
699df8bae1dSRodney W. Grimes 		 */
700fb59c426SYoshinobu Inoue 		if (off0 > sizeof (struct ip)) {
701105bd211SGleb Smirnoff 			ip_stripoptions(m);
702fb59c426SYoshinobu Inoue 			off0 = sizeof(struct ip);
703fb59c426SYoshinobu Inoue 		}
704df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof (struct tcpiphdr)) {
7054dfdffe9SAndre Oppermann 			if ((m = m_pullup(m, sizeof (struct tcpiphdr)))
7064dfdffe9SAndre Oppermann 			    == NULL) {
70778b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvshort);
7088f5a8818SKevin Lo 				return (IPPROTO_DONE);
709df8bae1dSRodney W. Grimes 			}
710df8bae1dSRodney W. Grimes 		}
711fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
712db4f9cc7SJonathan Lemon 		th = (struct tcphdr *)((caddr_t)ip + off0);
7138ad458a4SGleb Smirnoff 		tlen = ntohs(ip->ip_len) - off0;
714df8bae1dSRodney W. Grimes 
7151d7ee746SKurt Lidl 		iptos = ip->ip_tos;
7169e644c23SMichael Tuexen 		if (port)
7179e644c23SMichael Tuexen 			goto skip_csum;
718db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
719db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
720db4f9cc7SJonathan Lemon 				th->th_sum = m->m_pkthdr.csum_data;
721db4f9cc7SJonathan Lemon 			else
722db4f9cc7SJonathan Lemon 				th->th_sum = in_pseudo(ip->ip_src.s_addr,
723c068736aSJeffrey Hsu 				    ip->ip_dst.s_addr,
7248f134647SGleb Smirnoff 				    htonl(m->m_pkthdr.csum_data + tlen +
725c068736aSJeffrey Hsu 				    IPPROTO_TCP));
726db4f9cc7SJonathan Lemon 			th->th_sum ^= 0xffff;
727db4f9cc7SJonathan Lemon 		} else {
7288f134647SGleb Smirnoff 			struct ipovly *ipov = (struct ipovly *)ip;
7298f134647SGleb Smirnoff 
730df8bae1dSRodney W. Grimes 			/*
731df8bae1dSRodney W. Grimes 			 * Checksum extended TCP header and data.
732df8bae1dSRodney W. Grimes 			 */
7338f134647SGleb Smirnoff 			len = off0 + tlen;
734c4556b2fSAndrey V. Elsukov 			ipttl = ip->ip_ttl;
735fb59c426SYoshinobu Inoue 			bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
7368f134647SGleb Smirnoff 			ipov->ih_len = htons(tlen);
737fb59c426SYoshinobu Inoue 			th->th_sum = in_cksum(m, len);
73857f60867SMark Johnston 			/* Reset length for SDT probes. */
7391d7ee746SKurt Lidl 			ip->ip_len = htons(len);
7401d7ee746SKurt Lidl 			/* Reset TOS bits */
7411d7ee746SKurt Lidl 			ip->ip_tos = iptos;
7421d7ee746SKurt Lidl 			/* Re-initialization for later version check */
743c4556b2fSAndrey V. Elsukov 			ip->ip_ttl = ipttl;
7441d7ee746SKurt Lidl 			ip->ip_v = IPVERSION;
745b2fe54bcSAndrey V. Elsukov 			ip->ip_hl = off0 >> 2;
746db4f9cc7SJonathan Lemon 		}
7479e644c23SMichael Tuexen 	skip_csum:
7489e644c23SMichael Tuexen 		if (th->th_sum && (port == 0)) {
74978b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
750df8bae1dSRodney W. Grimes 			goto drop;
751df8bae1dSRodney W. Grimes 		}
752713264f6SMark Johnston 		KASSERT(ip->ip_dst.s_addr != INADDR_ANY,
753713264f6SMark Johnston 		    ("%s: unspecified destination v4 address", __func__));
754713264f6SMark Johnston 		if (__predict_false(ip->ip_src.s_addr == INADDR_ANY)) {
75578e6c3aaSGleb Smirnoff 			IPSTAT_INC(ips_badaddr);
756713264f6SMark Johnston 			goto drop;
757713264f6SMark Johnston 		}
758fb59c426SYoshinobu Inoue 	}
759b287c6c7SBjoern A. Zeeb #endif /* INET */
760df8bae1dSRodney W. Grimes 
761df8bae1dSRodney W. Grimes 	/*
762df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
763df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
764df8bae1dSRodney W. Grimes 	 */
765fb59c426SYoshinobu Inoue 	off = th->th_off << 2;
766df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
76778b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvbadoff);
768df8bae1dSRodney W. Grimes 		goto drop;
769df8bae1dSRodney W. Grimes 	}
770fb59c426SYoshinobu Inoue 	tlen -= off;	/* tlen is used instead of ti->ti_len */
771df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
77204d3a452SHajimu UMEMOTO #ifdef INET6
773b287c6c7SBjoern A. Zeeb 		if (isipv6) {
774a4adf6ccSBjoern A. Zeeb 			if (m->m_len < off0 + off) {
7754e619b17SBjoern A. Zeeb 				m = m_pullup(m, off0 + off);
7764e619b17SBjoern A. Zeeb 				if (m == NULL) {
7774e619b17SBjoern A. Zeeb 					TCPSTAT_INC(tcps_rcvshort);
7784e619b17SBjoern A. Zeeb 					return (IPPROTO_DONE);
7794e619b17SBjoern A. Zeeb 				}
780a4adf6ccSBjoern A. Zeeb 			}
781fb59c426SYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
782fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
783b287c6c7SBjoern A. Zeeb 		}
78404d3a452SHajimu UMEMOTO #endif
785b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
786b287c6c7SBjoern A. Zeeb 		else
787b287c6c7SBjoern A. Zeeb #endif
788b287c6c7SBjoern A. Zeeb #ifdef INET
789b287c6c7SBjoern A. Zeeb 		{
790df8bae1dSRodney W. Grimes 			if (m->m_len < sizeof(struct ip) + off) {
791c068736aSJeffrey Hsu 				if ((m = m_pullup(m, sizeof (struct ip) + off))
7924dfdffe9SAndre Oppermann 				    == NULL) {
79378b50714SRobert Watson 					TCPSTAT_INC(tcps_rcvshort);
7948f5a8818SKevin Lo 					return (IPPROTO_DONE);
795df8bae1dSRodney W. Grimes 				}
796fb59c426SYoshinobu Inoue 				ip = mtod(m, struct ip *);
797fb59c426SYoshinobu Inoue 				th = (struct tcphdr *)((caddr_t)ip + off0);
798fb59c426SYoshinobu Inoue 			}
799df8bae1dSRodney W. Grimes 		}
800b287c6c7SBjoern A. Zeeb #endif
801df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
802fb59c426SYoshinobu Inoue 		optp = (u_char *)(th + 1);
803df8bae1dSRodney W. Grimes 	}
8041ebf4607SRichard Scheffenegger 	thflags = tcp_get_flags(th);
805df8bae1dSRodney W. Grimes 
806e46cd3d4SDag-Erling Smørgrav 	/*
807df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
808df8bae1dSRodney W. Grimes 	 */
8092903309aSAttilio Rao 	tcp_fields_to_host(th);
810df8bae1dSRodney W. Grimes 
811df8bae1dSRodney W. Grimes 	/*
812794235b7SAndre Oppermann 	 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options.
813755c1f07SAndras Olah 	 */
814fb59c426SYoshinobu Inoue 	drop_hdrlen = off0 + off;
815755c1f07SAndras Olah 
816755c1f07SAndras Olah 	/*
8178d573cc1SAndre Oppermann 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
8188d573cc1SAndre Oppermann 	 */
819b8056faeSGleb Smirnoff         if (
820b8056faeSGleb Smirnoff #ifdef INET6
821b8056faeSGleb Smirnoff 	    (isipv6 && (m->m_flags & M_IP6_NEXTHOP))
822b8056faeSGleb Smirnoff #ifdef INET
823b8056faeSGleb Smirnoff 	    || (!isipv6 && (m->m_flags & M_IP_NEXTHOP))
824b8056faeSGleb Smirnoff #endif
825b8056faeSGleb Smirnoff #endif
826b8056faeSGleb Smirnoff #if defined(INET) && !defined(INET6)
827b8056faeSGleb Smirnoff 	    (m->m_flags & M_IP_NEXTHOP)
828b8056faeSGleb Smirnoff #endif
829b8056faeSGleb Smirnoff 	    )
8309b932e9eSAndre Oppermann 		fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
8319b932e9eSAndre Oppermann 
83208d9c920SGleb Smirnoff 	/*
8331db08fbeSGleb Smirnoff 	 * For initial SYN packets we don't need write lock on matching
8341db08fbeSGleb Smirnoff 	 * PCB, be it a listening one or a synchronized one.  The packet
8351db08fbeSGleb Smirnoff 	 * shall not modify its state.
83608d9c920SGleb Smirnoff 	 */
837d88eb465SGleb Smirnoff 	lookupflag = INPLOOKUP_WILDCARD |
838d88eb465SGleb Smirnoff 	    ((thflags & (TH_ACK|TH_SYN)) == TH_SYN ?
8395dc99e9bSMark Johnston 	    INPLOOKUP_RLOCKPCB : INPLOOKUP_WLOCKPCB) |
8405dc99e9bSMark Johnston 	    (V_tcp_bind_all_fibs ? 0 : INPLOOKUP_FIB);
841ce7ad664SEd Maste findpcb:
842df9de82fSMichael Tuexen 	tp = NULL;
8438a006adbSBjoern A. Zeeb #ifdef INET6
8448a006adbSBjoern A. Zeeb 	if (isipv6 && fwd_tag != NULL) {
8458a006adbSBjoern A. Zeeb 		struct sockaddr_in6 *next_hop6;
8468a006adbSBjoern A. Zeeb 
8478a006adbSBjoern A. Zeeb 		next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
8488a006adbSBjoern A. Zeeb 		/*
8498a006adbSBjoern A. Zeeb 		 * Transparently forwarded. Pretend to be the destination.
8508a006adbSBjoern A. Zeeb 		 * Already got one like this?
8518a006adbSBjoern A. Zeeb 		 */
8528a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo,
8538a006adbSBjoern A. Zeeb 		    &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport,
854aab8c844SGleb Smirnoff 		    lookupflag & ~INPLOOKUP_WILDCARD, m->m_pkthdr.rcvif, m);
8558a006adbSBjoern A. Zeeb 		if (!inp) {
8568a006adbSBjoern A. Zeeb 			/*
8578a006adbSBjoern A. Zeeb 			 * It's new.  Try to find the ambushing socket.
8588a006adbSBjoern A. Zeeb 			 * Because we've rewritten the destination address,
8598a006adbSBjoern A. Zeeb 			 * any hardware-generated hash is ignored.
8608a006adbSBjoern A. Zeeb 			 */
8618a006adbSBjoern A. Zeeb 			inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src,
8628a006adbSBjoern A. Zeeb 			    th->th_sport, &next_hop6->sin6_addr,
8638a006adbSBjoern A. Zeeb 			    next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) :
864d88eb465SGleb Smirnoff 			    th->th_dport, lookupflag, m->m_pkthdr.rcvif);
8658a006adbSBjoern A. Zeeb 		}
866c1de64a4SAndrey V. Elsukov 	} else if (isipv6) {
8678a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src,
868d88eb465SGleb Smirnoff 		    th->th_sport, &ip6->ip6_dst, th->th_dport, lookupflag,
869d88eb465SGleb Smirnoff 		    m->m_pkthdr.rcvif, m);
8708a006adbSBjoern A. Zeeb 	}
8718a006adbSBjoern A. Zeeb #endif /* INET6 */
8728a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET)
8738a006adbSBjoern A. Zeeb 	else
8748a006adbSBjoern A. Zeeb #endif
8758a006adbSBjoern A. Zeeb #ifdef INET
8768a006adbSBjoern A. Zeeb 	if (fwd_tag != NULL) {
8779b932e9eSAndre Oppermann 		struct sockaddr_in *next_hop;
8789b932e9eSAndre Oppermann 
8799b932e9eSAndre Oppermann 		next_hop = (struct sockaddr_in *)(fwd_tag+1);
880f9e354dfSJulian Elischer 		/*
8812b25acc1SLuigi Rizzo 		 * Transparently forwarded. Pretend to be the destination.
882f9e354dfSJulian Elischer 		 * already got one like this?
883f9e354dfSJulian Elischer 		 */
884d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport,
885aab8c844SGleb Smirnoff 		    ip->ip_dst, th->th_dport, lookupflag & ~INPLOOKUP_WILDCARD,
886aab8c844SGleb Smirnoff 		    m->m_pkthdr.rcvif, m);
887fa046d87SRobert Watson 		if (!inp) {
888fa046d87SRobert Watson 			/*
889fa046d87SRobert Watson 			 * It's new.  Try to find the ambushing socket.
890d3c1f003SRobert Watson 			 * Because we've rewritten the destination address,
891d3c1f003SRobert Watson 			 * any hardware-generated hash is ignored.
892fa046d87SRobert Watson 			 */
893fa046d87SRobert Watson 			inp = in_pcblookup(&V_tcbinfo, ip->ip_src,
894fa046d87SRobert Watson 			    th->th_sport, next_hop->sin_addr,
895fa046d87SRobert Watson 			    next_hop->sin_port ? ntohs(next_hop->sin_port) :
896d88eb465SGleb Smirnoff 			    th->th_dport, lookupflag, m->m_pkthdr.rcvif);
897f9e354dfSJulian Elischer 		}
898b10fbdeaSAndre Oppermann 	} else
899d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src,
900d88eb465SGleb Smirnoff 		    th->th_sport, ip->ip_dst, th->th_dport, lookupflag,
901d88eb465SGleb Smirnoff 		    m->m_pkthdr.rcvif, m);
9028a006adbSBjoern A. Zeeb #endif /* INET */
903f9e354dfSJulian Elischer 
904df8bae1dSRodney W. Grimes 	/*
905574b6964SAndre Oppermann 	 * If the INPCB does not exist then all data in the incoming
906574b6964SAndre Oppermann 	 * segment is discarded and an appropriate RST is sent back.
9078b07e49aSJulian Elischer 	 * XXX MRT Send RST using which routing table?
908df8bae1dSRodney W. Grimes 	 */
909816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
910d88eb465SGleb Smirnoff 		if (rstreason != 0) {
911d88eb465SGleb Smirnoff 			/* We came here after second (safety) lookup. */
912d88eb465SGleb Smirnoff 			MPASS((lookupflag & INPLOOKUP_WILDCARD) == 0);
913d88eb465SGleb Smirnoff 			goto dropwithreset;
914d88eb465SGleb Smirnoff 		}
915574b6964SAndre Oppermann 		/*
916574b6964SAndre Oppermann 		 * Log communication attempts to ports that are not
917574b6964SAndre Oppermann 		 * in use.
918574b6964SAndre Oppermann 		 */
919334fc582SBjoern A. Zeeb 		if ((V_tcp_log_in_vain == 1 && (thflags & TH_SYN)) ||
920334fc582SBjoern A. Zeeb 		    V_tcp_log_in_vain == 2) {
921b7d747ecSAndre Oppermann 			if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6)))
922df541e5fSAndre Oppermann 				log(LOG_INFO, "%s; %s: Connection attempt "
923df541e5fSAndre Oppermann 				    "to closed port\n", s, __func__);
9242e4e1b4cSGeoff Rehmet 		}
925a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
926a57815efSBosko Milekic 		goto dropwithreset;
927816a3d83SPoul-Henning Kamp 	}
92808d9c920SGleb Smirnoff 	INP_LOCK_ASSERT(inp);
929f567d55fSGleb Smirnoff 
930c2529042SHans Petter Selasky 	if ((inp->inp_flowtype == M_HASHTYPE_NONE) &&
931f71cb9f7SGleb Smirnoff 	    !SOLISTENING(inp->inp_socket)) {
932e5738ee0SCheng Cui 		if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
93380cb9f21SKip Macy 			inp->inp_flowid = m->m_pkthdr.flowid;
9342f719932SAdrian Chadd 			inp->inp_flowtype = M_HASHTYPE_GET(m);
935e5738ee0SCheng Cui #ifdef	RSS
936e5738ee0SCheng Cui 		} else {
937e5738ee0SCheng Cui 			  /* assign flowid by software RSS hash */
938e5738ee0SCheng Cui #ifdef INET6
939e5738ee0SCheng Cui 			  if (isipv6) {
940e5738ee0SCheng Cui 				rss_proto_software_hash_v6(&inp->in6p_faddr,
941e5738ee0SCheng Cui 							   &inp->in6p_laddr,
942e5738ee0SCheng Cui 							   inp->inp_fport,
943e5738ee0SCheng Cui 							   inp->inp_lport,
944e5738ee0SCheng Cui 							   IPPROTO_TCP,
945e5738ee0SCheng Cui 							   &inp->inp_flowid,
946e5738ee0SCheng Cui 							   &inp->inp_flowtype);
947e5738ee0SCheng Cui 			  } else
948e5738ee0SCheng Cui #endif	/* INET6 */
949e5738ee0SCheng Cui 			  {
950e5738ee0SCheng Cui 				rss_proto_software_hash_v4(inp->inp_faddr,
951e5738ee0SCheng Cui 							   inp->inp_laddr,
952e5738ee0SCheng Cui 							   inp->inp_fport,
953e5738ee0SCheng Cui 							   inp->inp_lport,
954e5738ee0SCheng Cui 							   IPPROTO_TCP,
955e5738ee0SCheng Cui 							   &inp->inp_flowid,
956e5738ee0SCheng Cui 							   &inp->inp_flowtype);
957e5738ee0SCheng Cui 			  }
958e5738ee0SCheng Cui #endif	/* RSS */
959e5738ee0SCheng Cui 		}
96080cb9f21SKip Macy 	}
961fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
962a2589465SKen Smith #ifdef INET6
963fcf59617SAndrey V. Elsukov 	if (isipv6 && IPSEC_ENABLED(ipv6) &&
964fcf59617SAndrey V. Elsukov 	    IPSEC_CHECK_POLICY(ipv6, m, inp) != 0) {
965a2589465SKen Smith 		goto dropunlock;
966a2589465SKen Smith 	}
967fcf59617SAndrey V. Elsukov #ifdef INET
968fcf59617SAndrey V. Elsukov 	else
969fcf59617SAndrey V. Elsukov #endif
970fcf59617SAndrey V. Elsukov #endif /* INET6 */
971fcf59617SAndrey V. Elsukov #ifdef INET
972fcf59617SAndrey V. Elsukov 	if (IPSEC_ENABLED(ipv4) &&
973fcf59617SAndrey V. Elsukov 	    IPSEC_CHECK_POLICY(ipv4, m, inp) != 0) {
974fcf59617SAndrey V. Elsukov 		goto dropunlock;
975fcf59617SAndrey V. Elsukov 	}
976fcf59617SAndrey V. Elsukov #endif /* INET */
977a2589465SKen Smith #endif /* IPSEC */
978a2589465SKen Smith 
9798d573cc1SAndre Oppermann 	/*
9808d573cc1SAndre Oppermann 	 * Check the minimum TTL for socket.
9818d573cc1SAndre Oppermann 	 */
98234f83c52SGeorge V. Neville-Neil 	if (inp->inp_ip_minttl != 0) {
98334f83c52SGeorge V. Neville-Neil #ifdef INET6
9842d8868dbSJonathan T. Looney 		if (isipv6) {
9852d8868dbSJonathan T. Looney 			if (inp->inp_ip_minttl > ip6->ip6_hlim)
986302ce8d6SAndre Oppermann 				goto dropunlock;
9872d8868dbSJonathan T. Looney 		} else
98834f83c52SGeorge V. Neville-Neil #endif
98934f83c52SGeorge V. Neville-Neil 		if (inp->inp_ip_minttl > ip->ip_ttl)
990302ce8d6SAndre Oppermann 			goto dropunlock;
99134f83c52SGeorge V. Neville-Neil 	}
992936cd18dSAndre Oppermann 
9930d744519SGleb Smirnoff 	tp = intotcpcb(inp);
9940d744519SGleb Smirnoff 	switch (tp->t_state) {
9950d744519SGleb Smirnoff 	case TCPS_TIME_WAIT:
996340c35deSJonathan Lemon 		/*
9970d744519SGleb Smirnoff 		 * A previous connection in TIMEWAIT state is supposed to catch
9980d744519SGleb Smirnoff 		 * stray or duplicate segments arriving late.  If this segment
9990d744519SGleb Smirnoff 		 * was a legitimate new connection attempt, the old INPCB gets
10000d744519SGleb Smirnoff 		 * removed and we can try again to find a listening socket.
1001340c35deSJonathan Lemon 		 */
1002283c76c7SMichael Tuexen 		tcp_dooptions(&to, optp, optlen,
1003283c76c7SMichael Tuexen 		    (thflags & TH_SYN) ? TO_SYN : 0);
10048d573cc1SAndre Oppermann 		/*
10050d744519SGleb Smirnoff 		 * tcp_twcheck unlocks the inp always, and frees the m if fails.
10068d573cc1SAndre Oppermann 		 */
10072104448fSAndre Oppermann 		if (tcp_twcheck(inp, &to, th, m, tlen))
1008340c35deSJonathan Lemon 			goto findpcb;
10098f5a8818SKevin Lo 		return (IPPROTO_DONE);
10100d744519SGleb Smirnoff 	case TCPS_CLOSED:
1011794235b7SAndre Oppermann 		/*
1012794235b7SAndre Oppermann 		 * The TCPCB may no longer exist if the connection is winding
1013794235b7SAndre Oppermann 		 * down or it is in the CLOSED state.  Either way we drop the
1014794235b7SAndre Oppermann 		 * segment and send an appropriate response.
1015794235b7SAndre Oppermann 		 */
1016a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
1017a57815efSBosko Milekic 		goto dropwithreset;
101809f81a46SBosko Milekic 	}
1019df8bae1dSRodney W. Grimes 
10209e644c23SMichael Tuexen 	if ((tp->t_port != port) && (tp->t_state > TCPS_LISTEN)) {
10219e644c23SMichael Tuexen 		rstreason = BANDLIM_RST_CLOSEDPORT;
10229e644c23SMichael Tuexen 		goto dropwithreset;
10239e644c23SMichael Tuexen 	}
10249e644c23SMichael Tuexen 
102509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
102609fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
102709fe6320SNavdeep Parhar 		tcp_offload_input(tp, m);
102809fe6320SNavdeep Parhar 		m = NULL;	/* consumed by the TOE driver */
102909fe6320SNavdeep Parhar 		goto dropunlock;
103009fe6320SNavdeep Parhar 	}
103109fe6320SNavdeep Parhar #endif
103209fe6320SNavdeep Parhar 
1033c488362eSRobert Watson #ifdef MAC
103430d239bcSRobert Watson 	if (mac_inpcb_check_deliver(inp, m))
1035302ce8d6SAndre Oppermann 		goto dropunlock;
1036c488362eSRobert Watson #endif
1037a557af22SRobert Watson 	so = inp->inp_socket;
1038302ce8d6SAndre Oppermann 	KASSERT(so != NULL, ("%s: so == NULL", __func__));
1039db33b3e6SAndre Oppermann 	/*
1040db33b3e6SAndre Oppermann 	 * When the socket is accepting connections (the INPCB is in LISTEN
1041db33b3e6SAndre Oppermann 	 * state) we look into the SYN cache if this is a new connection
1042a7c7f2a7SJohn Baldwin 	 * attempt or the completion of a previous one.
1043db33b3e6SAndre Oppermann 	 */
1044f4bb1869SMark Johnston 	KASSERT(tp->t_state == TCPS_LISTEN || !SOLISTENING(so),
104545274760SJonathan T. Looney 	    ("%s: so accepting but tp %p not listening", __func__, tp));
1046f4bb1869SMark Johnston 	if (tp->t_state == TCPS_LISTEN && SOLISTENING(so)) {
1047540e8b7eSJeffrey Hsu 		struct in_conninfo inc;
1048540e8b7eSJeffrey Hsu 
10498bfb1918SAndre Oppermann 		bzero(&inc, sizeof(inc));
1050302ce8d6SAndre Oppermann #ifdef INET6
1051be2ac88cSJonathan Lemon 		if (isipv6) {
1052dcdb4371SBjoern A. Zeeb 			inc.inc_flags |= INC_ISIPV6;
10535dff1c38SMichael Tuexen 			if (inp->inp_inc.inc_flags & INC_IPV6MINMTU)
10545dff1c38SMichael Tuexen 				inc.inc_flags |= INC_IPV6MINMTU;
1055be2ac88cSJonathan Lemon 			inc.inc6_faddr = ip6->ip6_src;
1056be2ac88cSJonathan Lemon 			inc.inc6_laddr = ip6->ip6_dst;
1057302ce8d6SAndre Oppermann 		} else
1058302ce8d6SAndre Oppermann #endif
1059302ce8d6SAndre Oppermann 		{
1060be2ac88cSJonathan Lemon 			inc.inc_faddr = ip->ip_src;
1061be2ac88cSJonathan Lemon 			inc.inc_laddr = ip->ip_dst;
1062be2ac88cSJonathan Lemon 		}
1063be2ac88cSJonathan Lemon 		inc.inc_fport = th->th_sport;
1064be2ac88cSJonathan Lemon 		inc.inc_lport = th->th_dport;
10657973fba3SJulian Elischer 		inc.inc_fibnum = so->so_fibnum;
1066be2ac88cSJonathan Lemon 
1067fb59c426SYoshinobu Inoue 		/*
10688d573cc1SAndre Oppermann 		 * Check for an existing connection attempt in syncache if
10698d573cc1SAndre Oppermann 		 * the flag is only ACK.  A successful lookup creates a new
10708d573cc1SAndre Oppermann 		 * socket appended to the listen queue in SYN_RECEIVED state.
1071fb59c426SYoshinobu Inoue 		 */
1072be2ac88cSJonathan Lemon 		if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
1073bf6d304aSAndre Oppermann 			/*
1074bf6d304aSAndre Oppermann 			 * Parse the TCP options here because
1075bf6d304aSAndre Oppermann 			 * syncookies need access to the reflected
1076bf6d304aSAndre Oppermann 			 * timestamp.
1077bf6d304aSAndre Oppermann 			 */
1078bf6d304aSAndre Oppermann 			tcp_dooptions(&to, optp, optlen, 0);
10799fa198beSAndre Oppermann 			/*
1080e7231d07SGleb Smirnoff 			 * NB: syncache_expand() doesn't unlock inp.
10819fa198beSAndre Oppermann 			 */
10829e644c23SMichael Tuexen 			rstreason = syncache_expand(&inc, &to, th, &so, m, port);
1083fcf59617SAndrey V. Elsukov 			if (rstreason < 0) {
1084fcf59617SAndrey V. Elsukov 				/*
1085fcf59617SAndrey V. Elsukov 				 * A failing TCP MD5 signature comparison
1086fcf59617SAndrey V. Elsukov 				 * must result in the segment being dropped
1087fcf59617SAndrey V. Elsukov 				 * and must not produce any response back
1088fcf59617SAndrey V. Elsukov 				 * to the sender.
1089fcf59617SAndrey V. Elsukov 				 */
1090fcf59617SAndrey V. Elsukov 				goto dropunlock;
1091fcf59617SAndrey V. Elsukov 			} else if (rstreason == 0) {
10924195b4afSPaul Traina 				/*
1093d88eb465SGleb Smirnoff 				 * No syncache entry, or ACK was not for our
1094d88eb465SGleb Smirnoff 				 * SYN/ACK.  Do our protection against double
1095d88eb465SGleb Smirnoff 				 * ACK.  If peer sent us 2 ACKs, then for the
1096d88eb465SGleb Smirnoff 				 * first one syncache_expand() successfully
1097d88eb465SGleb Smirnoff 				 * converted syncache entry into a socket,
1098d88eb465SGleb Smirnoff 				 * while we were waiting on the inpcb lock.  We
1099d88eb465SGleb Smirnoff 				 * don't want to sent RST for the second ACK,
1100d88eb465SGleb Smirnoff 				 * so we perform second lookup without wildcard
1101d88eb465SGleb Smirnoff 				 * match, hoping to find the new socket.  If
1102d88eb465SGleb Smirnoff 				 * the ACK is stray indeed, rstreason would
1103d88eb465SGleb Smirnoff 				 * hint the above code that the lookup was a
1104d88eb465SGleb Smirnoff 				 * second attempt.
1105d88eb465SGleb Smirnoff 				 *
11068d573cc1SAndre Oppermann 				 * NB: syncache did its own logging
11078d573cc1SAndre Oppermann 				 * of the failure cause.
11084195b4afSPaul Traina 				 */
1109d88eb465SGleb Smirnoff 				INP_WUNLOCK(inp);
1110be2ac88cSJonathan Lemon 				rstreason = BANDLIM_RST_OPENPORT;
1111d88eb465SGleb Smirnoff 				lookupflag &= ~INPLOOKUP_WILDCARD;
1112d88eb465SGleb Smirnoff 				goto findpcb;
1113be2ac88cSJonathan Lemon 			}
111409c305ebSPatrick Kelsey tfo_socket_result:
1115f76fcf6dSJeffrey Hsu 			if (so == NULL) {
1116be2ac88cSJonathan Lemon 				/*
1117e207f800SAndre Oppermann 				 * We completed the 3-way handshake
1118e207f800SAndre Oppermann 				 * but could not allocate a socket
1119e207f800SAndre Oppermann 				 * either due to memory shortage,
1120e207f800SAndre Oppermann 				 * listen queue length limits or
1121a160e630SAndre Oppermann 				 * global socket limits.  Send RST
1122a160e630SAndre Oppermann 				 * or wait and have the remote end
1123a160e630SAndre Oppermann 				 * retransmit the ACK for another
1124a160e630SAndre Oppermann 				 * try.
1125be2ac88cSJonathan Lemon 				 */
1126a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1127a160e630SAndre Oppermann 					log(LOG_DEBUG, "%s; %s: Listen socket: "
11288d573cc1SAndre Oppermann 					    "Socket allocation failed due to "
11298d573cc1SAndre Oppermann 					    "limits or memory shortage, %s\n",
1130ac957cd2SJulian Elischer 					    s, __func__,
1131ac957cd2SJulian Elischer 					    V_tcp_sc_rst_sock_fail ?
1132ac957cd2SJulian Elischer 					    "sending RST" : "try again");
1133603724d3SBjoern A. Zeeb 				if (V_tcp_sc_rst_sock_fail) {
1134e207f800SAndre Oppermann 					rstreason = BANDLIM_UNLIMITED;
1135e207f800SAndre Oppermann 					goto dropwithreset;
1136a160e630SAndre Oppermann 				} else
1137a160e630SAndre Oppermann 					goto dropunlock;
1138f76fcf6dSJeffrey Hsu 			}
1139be2ac88cSJonathan Lemon 			/*
1140be2ac88cSJonathan Lemon 			 * Socket is created in state SYN_RECEIVED.
11418d573cc1SAndre Oppermann 			 * Unlock the listen socket, lock the newly
11428d573cc1SAndre Oppermann 			 * created socket and update the tp variable.
114308d9c920SGleb Smirnoff 			 * If we came here via jump to tfo_socket_result,
114408d9c920SGleb Smirnoff 			 * then listening socket is read-locked.
1145be2ac88cSJonathan Lemon 			 */
114608d9c920SGleb Smirnoff 			INP_UNLOCK(inp);	/* listen socket */
1147be2ac88cSJonathan Lemon 			inp = sotoinpcb(so);
1148ff9b006dSJulien Charbon 			/*
1149ff9b006dSJulien Charbon 			 * New connection inpcb is already locked by
1150ff9b006dSJulien Charbon 			 * syncache_expand().
1151ff9b006dSJulien Charbon 			 */
1152ff9b006dSJulien Charbon 			INP_WLOCK_ASSERT(inp);
1153be2ac88cSJonathan Lemon 			tp = intotcpcb(inp);
11548d573cc1SAndre Oppermann 			KASSERT(tp->t_state == TCPS_SYN_RECEIVED,
11558d573cc1SAndre Oppermann 			    ("%s: ", __func__));
1156be2ac88cSJonathan Lemon 			/*
1157302ce8d6SAndre Oppermann 			 * Process the segment and the data it
1158302ce8d6SAndre Oppermann 			 * contains.  tcp_do_segment() consumes
1159302ce8d6SAndre Oppermann 			 * the mbuf chain and unlocks the inpcb.
1160302ce8d6SAndre Oppermann 			 */
11616138da62SMichael Tuexen 			TCP_PROBE5(receive, NULL, tp, m, tp, th);
116235bc0bccSGleb Smirnoff 			tp->t_fb->tfb_tcp_do_segment(tp, m, th, drop_hdrlen,
116335bc0bccSGleb Smirnoff 			    tlen, iptos);
11648f5a8818SKevin Lo 			return (IPPROTO_DONE);
1165be2ac88cSJonathan Lemon 		}
1166a160e630SAndre Oppermann 		/*
11678d573cc1SAndre Oppermann 		 * Segment flag validation for new connection attempts:
11688d573cc1SAndre Oppermann 		 *
1169a160e630SAndre Oppermann 		 * Our (SYN|ACK) response was rejected.
1170a160e630SAndre Oppermann 		 * Check with syncache and remove entry to prevent
1171a160e630SAndre Oppermann 		 * retransmits.
117219bc77c5SAndre Oppermann 		 *
117319bc77c5SAndre Oppermann 		 * NB: syncache_chkrst does its own logging of failure
117419bc77c5SAndre Oppermann 		 * causes.
1175a160e630SAndre Oppermann 		 */
1176a160e630SAndre Oppermann 		if (thflags & TH_RST) {
11779e644c23SMichael Tuexen 			syncache_chkrst(&inc, th, m, port);
1178a160e630SAndre Oppermann 			goto dropunlock;
1179a160e630SAndre Oppermann 		}
1180a160e630SAndre Oppermann 		/*
1181a160e630SAndre Oppermann 		 * We can't do anything without SYN.
1182a160e630SAndre Oppermann 		 */
1183a160e630SAndre Oppermann 		if ((thflags & TH_SYN) == 0) {
1184a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1185a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1186773673c1SAndre Oppermann 				    "SYN is missing, segment ignored\n",
11878d573cc1SAndre Oppermann 				    s, __func__);
118878b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1189a160e630SAndre Oppermann 			goto dropunlock;
1190a160e630SAndre Oppermann 		}
1191a160e630SAndre Oppermann 		/*
1192a160e630SAndre Oppermann 		 * (SYN|ACK) is bogus on a listen socket.
1193a160e630SAndre Oppermann 		 */
1194fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1195a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1196a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
11978d573cc1SAndre Oppermann 				    "SYN|ACK invalid, segment rejected\n",
11988d573cc1SAndre Oppermann 				    s, __func__);
11999e644c23SMichael Tuexen 			syncache_badack(&inc, port);	/* XXX: Not needed! */
120078b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1201a57815efSBosko Milekic 			rstreason = BANDLIM_RST_OPENPORT;
1202a57815efSBosko Milekic 			goto dropwithreset;
12034195b4afSPaul Traina 		}
1204a160e630SAndre Oppermann 		/*
1205a160e630SAndre Oppermann 		 * If the drop_synfin option is enabled, drop all
1206a160e630SAndre Oppermann 		 * segments with both the SYN and FIN bits set.
1207a160e630SAndre Oppermann 		 * This prevents e.g. nmap from identifying the
1208a160e630SAndre Oppermann 		 * TCP/IP stack.
1209a160e630SAndre Oppermann 		 * XXX: Poor reasoning.  nmap has other methods
1210a160e630SAndre Oppermann 		 * and is constantly refining its stack detection
1211a160e630SAndre Oppermann 		 * strategies.
12128d573cc1SAndre Oppermann 		 * XXX: This is a violation of the TCP specification
1213a160e630SAndre Oppermann 		 * and was used by RFC1644.
1214a160e630SAndre Oppermann 		 */
1215603724d3SBjoern A. Zeeb 		if ((thflags & TH_FIN) && V_drop_synfin) {
1216a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1217a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1218773673c1SAndre Oppermann 				    "SYN|FIN segment ignored (based on "
12198d573cc1SAndre Oppermann 				    "sysctl setting)\n", s, __func__);
122078b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1221302ce8d6SAndre Oppermann 			goto dropunlock;
1222ebb0cbeaSPaul Traina 		}
1223be2ac88cSJonathan Lemon 		/*
1224be2ac88cSJonathan Lemon 		 * Segment's flags are (SYN) or (SYN|FIN).
1225a160e630SAndre Oppermann 		 *
1226a160e630SAndre Oppermann 		 * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored
1227a160e630SAndre Oppermann 		 * as they do not affect the state of the TCP FSM.
1228a160e630SAndre Oppermann 		 * The data pointed to by TH_URG and th_urp is ignored.
1229be2ac88cSJonathan Lemon 		 */
1230a160e630SAndre Oppermann 		KASSERT((thflags & (TH_RST|TH_ACK)) == 0,
1231a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_RST or TH_ACK set", __func__));
1232a160e630SAndre Oppermann 		KASSERT(thflags & (TH_SYN),
1233a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_SYN not set", __func__));
123408d9c920SGleb Smirnoff 		INP_RLOCK_ASSERT(inp);
123533841545SHajimu UMEMOTO #ifdef INET6
123633841545SHajimu UMEMOTO 		/*
123733841545SHajimu UMEMOTO 		 * If deprecated address is forbidden,
123833841545SHajimu UMEMOTO 		 * we do not accept SYN to deprecated interface
123933841545SHajimu UMEMOTO 		 * address to prevent any new inbound connection from
124033841545SHajimu UMEMOTO 		 * getting established.
124133841545SHajimu UMEMOTO 		 * When we do not accept SYN, we send a TCP RST,
124233841545SHajimu UMEMOTO 		 * with deprecated source address (instead of dropping
124333841545SHajimu UMEMOTO 		 * it).  We compromise it as it is much better for peer
124433841545SHajimu UMEMOTO 		 * to send a RST, and RST will be the final packet
124533841545SHajimu UMEMOTO 		 * for the exchange.
124633841545SHajimu UMEMOTO 		 *
124733841545SHajimu UMEMOTO 		 * If we do not forbid deprecated addresses, we accept
124833841545SHajimu UMEMOTO 		 * the SYN packet.  RFC2462 does not suggest dropping
124933841545SHajimu UMEMOTO 		 * SYN in this case.
125033841545SHajimu UMEMOTO 		 * If we decipher RFC2462 5.5.4, it says like this:
125133841545SHajimu UMEMOTO 		 * 1. use of deprecated addr with existing
125233841545SHajimu UMEMOTO 		 *    communication is okay - "SHOULD continue to be
125333841545SHajimu UMEMOTO 		 *    used"
125433841545SHajimu UMEMOTO 		 * 2. use of it with new communication:
125533841545SHajimu UMEMOTO 		 *   (2a) "SHOULD NOT be used if alternate address
125633841545SHajimu UMEMOTO 		 *        with sufficient scope is available"
125733841545SHajimu UMEMOTO 		 *   (2b) nothing mentioned otherwise.
125833841545SHajimu UMEMOTO 		 * Here we fall into (2b) case as we have no choice in
125933841545SHajimu UMEMOTO 		 * our source address selection - we must obey the peer.
126033841545SHajimu UMEMOTO 		 *
126133841545SHajimu UMEMOTO 		 * The wording in RFC2462 is confusing, and there are
126233841545SHajimu UMEMOTO 		 * multiple description text for deprecated address
126333841545SHajimu UMEMOTO 		 * handling - worse, they are not exactly the same.
126433841545SHajimu UMEMOTO 		 * I believe 5.5.4 is the best one, so we follow 5.5.4.
126533841545SHajimu UMEMOTO 		 */
1266603724d3SBjoern A. Zeeb 		if (isipv6 && !V_ip6_use_deprecated) {
126733841545SHajimu UMEMOTO 			struct in6_ifaddr *ia6;
126833841545SHajimu UMEMOTO 
12698268d82cSAlexander V. Chernikov 			ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false);
12708c0fec80SRobert Watson 			if (ia6 != NULL &&
127133841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
1272a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1273a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12748d573cc1SAndre Oppermann 					"Connection attempt to deprecated "
12758d573cc1SAndre Oppermann 					"IPv6 address rejected\n",
1276a160e630SAndre Oppermann 					s, __func__);
127733841545SHajimu UMEMOTO 				rstreason = BANDLIM_RST_OPENPORT;
127833841545SHajimu UMEMOTO 				goto dropwithreset;
127933841545SHajimu UMEMOTO 			}
128033841545SHajimu UMEMOTO 		}
1281b287c6c7SBjoern A. Zeeb #endif /* INET6 */
128265f28919SJesper Skriver 		/*
1283db33b3e6SAndre Oppermann 		 * Basic sanity checks on incoming SYN requests:
12848d573cc1SAndre Oppermann 		 *   Don't respond if the destination is a link layer
1285db33b3e6SAndre Oppermann 		 *	broadcast according to RFC1122 4.2.3.10, p. 104.
12868d573cc1SAndre Oppermann 		 *   If it is from this socket it must be forged.
12878d573cc1SAndre Oppermann 		 *   Don't respond if the source or destination is a
12888d573cc1SAndre Oppermann 		 *	global or subnet broad- or multicast address.
128993ec91baSCrist J. Clark 		 *   Note that it is quite possible to receive unicast
129093ec91baSCrist J. Clark 		 *	link-layer packets with a broadcast IP address. Use
1291197fc4caSGleb Smirnoff 		 *	in_ifnet_broadcast() to find them.
1292be2ac88cSJonathan Lemon 		 */
12938d573cc1SAndre Oppermann 		if (m->m_flags & (M_BCAST|M_MCAST)) {
12948d573cc1SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
12958d573cc1SAndre Oppermann 			    log(LOG_DEBUG, "%s; %s: Listen socket: "
12968d573cc1SAndre Oppermann 				"Connection attempt from broad- or multicast "
1297773673c1SAndre Oppermann 				"link layer address ignored\n", s, __func__);
1298302ce8d6SAndre Oppermann 			goto dropunlock;
12998d573cc1SAndre Oppermann 		}
1300db33b3e6SAndre Oppermann #ifdef INET6
1301b287c6c7SBjoern A. Zeeb 		if (isipv6) {
1302db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1303a160e630SAndre Oppermann 			    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) {
1304a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1305a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13068d573cc1SAndre Oppermann 					"Connection attempt to/from self "
1307773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1308302ce8d6SAndre Oppermann 				goto dropunlock;
1309a160e630SAndre Oppermann 			}
1310be2ac88cSJonathan Lemon 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1311a160e630SAndre Oppermann 			    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
1312a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1313a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13148d573cc1SAndre Oppermann 					"Connection attempt from/to multicast "
1315773673c1SAndre Oppermann 					"address ignored\n", s, __func__);
1316302ce8d6SAndre Oppermann 				goto dropunlock;
1317a160e630SAndre Oppermann 			}
1318b287c6c7SBjoern A. Zeeb 		}
1319db33b3e6SAndre Oppermann #endif
1320b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
1321b287c6c7SBjoern A. Zeeb 		else
1322b287c6c7SBjoern A. Zeeb #endif
1323b287c6c7SBjoern A. Zeeb #ifdef INET
1324b287c6c7SBjoern A. Zeeb 		{
1325db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1326a160e630SAndre Oppermann 			    ip->ip_dst.s_addr == ip->ip_src.s_addr) {
1327a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1328a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13298d573cc1SAndre Oppermann 					"Connection attempt from/to self "
1330773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1331302ce8d6SAndre Oppermann 				goto dropunlock;
1332a160e630SAndre Oppermann 			}
1333be2ac88cSJonathan Lemon 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
1334be2ac88cSJonathan Lemon 			    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
13352ca2159fSCrist J. Clark 			    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
1336197fc4caSGleb Smirnoff 			    in_ifnet_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
1337a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1338a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13398d573cc1SAndre Oppermann 					"Connection attempt from/to broad- "
1340773673c1SAndre Oppermann 					"or multicast address ignored\n",
1341a160e630SAndre Oppermann 					s, __func__);
1342302ce8d6SAndre Oppermann 				goto dropunlock;
1343c068736aSJeffrey Hsu 			}
1344a160e630SAndre Oppermann 		}
1345b287c6c7SBjoern A. Zeeb #endif
1346be2ac88cSJonathan Lemon 		/*
1347db33b3e6SAndre Oppermann 		 * SYN appears to be valid.  Create compressed TCP state
1348db33b3e6SAndre Oppermann 		 * for syncache.
1349be2ac88cSJonathan Lemon 		 */
13502b9c9984SGeorge V. Neville-Neil 		TCP_PROBE3(debug__input, tp, th, m);
1351f72167f4SAndre Oppermann 		tcp_dooptions(&to, optp, optlen, TO_SYN);
13528d5719aaSGleb Smirnoff 		if ((so = syncache_add(&inc, &to, th, inp, so, m, NULL, NULL,
13539e644c23SMichael Tuexen 		    iptos, port)) != NULL)
135409c305ebSPatrick Kelsey 			goto tfo_socket_result;
135518a75309SPatrick Kelsey 
1356be2ac88cSJonathan Lemon 		/*
13574d6e7130SAndre Oppermann 		 * Entry added to syncache and mbuf consumed.
1358a7c7f2a7SJohn Baldwin 		 * Only the listen socket is unlocked by syncache_add().
1359be2ac88cSJonathan Lemon 		 */
13608f5a8818SKevin Lo 		return (IPPROTO_DONE);
1361f76fcf6dSJeffrey Hsu 	}
1362fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1363fcf59617SAndrey V. Elsukov 	if (tp->t_flags & TF_SIGNATURE) {
1364fcf59617SAndrey V. Elsukov 		tcp_dooptions(&to, optp, optlen, thflags);
1365fcf59617SAndrey V. Elsukov 		if ((to.to_flags & TOF_SIGNATURE) == 0) {
1366fcf59617SAndrey V. Elsukov 			TCPSTAT_INC(tcps_sig_err_nosigopt);
13672903309aSAttilio Rao 			goto dropunlock;
13682903309aSAttilio Rao 		}
1369fcf59617SAndrey V. Elsukov 		if (!TCPMD5_ENABLED() ||
1370fcf59617SAndrey V. Elsukov 		    TCPMD5_INPUT(m, th, to.to_signature) != 0)
1371fcf59617SAndrey V. Elsukov 			goto dropunlock;
13722903309aSAttilio Rao 	}
13732903309aSAttilio Rao #endif
13742b9c9984SGeorge V. Neville-Neil 	TCP_PROBE5(receive, NULL, tp, m, tp, th);
137557f60867SMark Johnston 
1376302ce8d6SAndre Oppermann 	/*
13778d573cc1SAndre Oppermann 	 * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later
13788d573cc1SAndre Oppermann 	 * state.  tcp_do_segment() always consumes the mbuf chain, unlocks
13798d573cc1SAndre Oppermann 	 * the inpcb, and unlocks pcbinfo.
13801db08fbeSGleb Smirnoff 	 *
13811db08fbeSGleb Smirnoff 	 * XXXGL: in case of a pure SYN arriving on existing connection
13821db08fbeSGleb Smirnoff 	 * TCP stacks won't need to modify the PCB, they would either drop
13831db08fbeSGleb Smirnoff 	 * the segment silently, or send a challenge ACK.  However, we try
13841db08fbeSGleb Smirnoff 	 * to upgrade the lock, because calling convention for stacks is
13851db08fbeSGleb Smirnoff 	 * write-lock on PCB.  If upgrade fails, drop the SYN.
1386302ce8d6SAndre Oppermann 	 */
1387d88eb465SGleb Smirnoff 	if ((lookupflag & INPLOOKUP_RLOCKPCB) && INP_TRY_UPGRADE(inp) == 0)
13881db08fbeSGleb Smirnoff 		goto dropunlock;
13891db08fbeSGleb Smirnoff 
139035bc0bccSGleb Smirnoff 	tp->t_fb->tfb_tcp_do_segment(tp, m, th, drop_hdrlen, tlen, iptos);
13918f5a8818SKevin Lo 	return (IPPROTO_DONE);
1392be2ac88cSJonathan Lemon 
1393302ce8d6SAndre Oppermann dropwithreset:
139402d15215SMichael Tuexen 	/*
139502d15215SMichael Tuexen 	 * When blackholing do not respond with a RST but
139602d15215SMichael Tuexen 	 * completely ignore the segment and drop it.
139702d15215SMichael Tuexen 	 */
139802d15215SMichael Tuexen 	if (((rstreason == BANDLIM_RST_OPENPORT && V_blackhole == 3) ||
139902d15215SMichael Tuexen 	    (rstreason == BANDLIM_RST_CLOSEDPORT &&
140002d15215SMichael Tuexen 	    ((V_blackhole == 1 && (thflags & TH_SYN)) || V_blackhole > 1))) &&
140102d15215SMichael Tuexen 	    (V_blackhole_local || (
140202d15215SMichael Tuexen #ifdef INET6
140302d15215SMichael Tuexen 	    isipv6 ? !in6_localaddr(&ip6->ip6_src) :
140402d15215SMichael Tuexen #endif
140502d15215SMichael Tuexen #ifdef INET
140602d15215SMichael Tuexen 	    !in_localip(ip->ip_src)
140702d15215SMichael Tuexen #else
140802d15215SMichael Tuexen 	    true
140902d15215SMichael Tuexen #endif
141002d15215SMichael Tuexen 	    )))
141102d15215SMichael Tuexen 		goto dropunlock;
14122b9c9984SGeorge V. Neville-Neil 	TCP_PROBE5(receive, NULL, tp, m, tp, th);
1413302ce8d6SAndre Oppermann 	tcp_dropwithreset(m, th, tp, tlen, rstreason);
1414302ce8d6SAndre Oppermann 	m = NULL;	/* mbuf chain got consumed. */
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  *
1456*2a0d26d7SPeter Lei  * If all of the criteria are met, we increase 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
tcp_autorcvbuf(struct mbuf * m,struct tcphdr * th,struct socket * so,struct tcpcb * tp,int tlen)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
tcp_input(struct mbuf ** mp,int * offp,int proto)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
tcp_handle_wakeup(struct tcpcb * tp)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;
1510dded4e9eSRichard Scheffenegger 		SOCK_RECVBUF_LOCK_ASSERT(so);
1511032bf749SRichard Scheffenegger 		sorwakeup_locked(so);
15124d0770f1SRichard Scheffenegger 	}
15134d0770f1SRichard Scheffenegger }
15144d0770f1SRichard Scheffenegger 
15154d0770f1SRichard Scheffenegger void
tcp_do_segment(struct tcpcb * tp,struct mbuf * m,struct tcphdr * th,int drop_hdrlen,int tlen,uint8_t iptos)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 
15462529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0,
15472529f56eSJonathan T. Looney 	    tlen, NULL, true);
154886a996e6SHiren Panchasara 
1549013f4df6SMichael Tuexen 	if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
1550013f4df6SMichael Tuexen 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
1551013f4df6SMichael Tuexen 			log(LOG_DEBUG, "%s; %s: "
1552013f4df6SMichael Tuexen 			    "SYN|FIN segment ignored (based on "
1553013f4df6SMichael Tuexen 			    "sysctl setting)\n", s, __func__);
1554013f4df6SMichael Tuexen 			free(s, M_TCPLOG);
1555013f4df6SMichael Tuexen 		}
1556013f4df6SMichael Tuexen 		goto drop;
1557013f4df6SMichael Tuexen 	}
1558013f4df6SMichael Tuexen 
1559df8bae1dSRodney W. Grimes 	/*
1560ebfd7534SMichael Tuexen 	 * If a segment with the ACK-bit set arrives in the SYN-SENT state
1561ebfd7534SMichael Tuexen 	 * check SEQ.ACK first.
1562ebfd7534SMichael Tuexen 	 */
1563ebfd7534SMichael Tuexen 	if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) &&
1564ebfd7534SMichael Tuexen 	    (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) {
1565ebfd7534SMichael Tuexen 		rstreason = BANDLIM_UNLIMITED;
1566d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
1567ebfd7534SMichael Tuexen 		goto dropwithreset;
1568ebfd7534SMichael Tuexen 	}
1569ebfd7534SMichael Tuexen 
1570ebfd7534SMichael Tuexen 	/*
1571df8bae1dSRodney W. Grimes 	 * Segment received on connection.
1572df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
1573e8949f74SAndre Oppermann 	 * XXX: This should be done after segment
1574e8949f74SAndre Oppermann 	 * validation to ignore broken/spoofed segs.
1575df8bae1dSRodney W. Grimes 	 */
1576cd84e78fSRandall Stewart 	if  (tp->t_idle_reduce &&
1577cd84e78fSRandall Stewart 	     (tp->snd_max == tp->snd_una) &&
1578cd84e78fSRandall Stewart 	     ((ticks - tp->t_rcvtime) >= tp->t_rxtcur))
1579cd84e78fSRandall Stewart 		cc_after_idle(tp);
15809b8b58e0SJonathan Lemon 	tp->t_rcvtime = ticks;
1581df8bae1dSRodney W. Grimes 
1582d1b07f36SRandall Stewart 	if (thflags & TH_FIN)
1583d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_FIN);
1584df8bae1dSRodney W. Grimes 	/*
1585c52102ddSHiren Panchasara 	 * Scale up the window into a 32-bit value.
1586e8949f74SAndre Oppermann 	 * For the SYN_SENT state the scale is zero.
1587464fcfbcSAndre Oppermann 	 */
1588464fcfbcSAndre Oppermann 	tiwin = th->th_win << tp->snd_scale;
1589adc56f5aSEdward Tomasz Napierala #ifdef STATS
1590adc56f5aSEdward Tomasz Napierala 	stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin);
1591adc56f5aSEdward Tomasz Napierala #endif
1592464fcfbcSAndre Oppermann 
1593464fcfbcSAndre Oppermann 	/*
1594f2512ba1SRui Paulo 	 * TCP ECN processing.
1595f2512ba1SRui Paulo 	 */
1596b1258b76SRichard Scheffenegger 	if (tcp_ecn_input_segment(tp, thflags, tlen,
1597b1258b76SRichard Scheffenegger 	    tcp_packets_this_ack(tp, th->th_ack),
1598b1258b76SRichard Scheffenegger 	    iptos))
1599dbc42409SLawrence Stewart 		cc_cong_signal(tp, th, CC_ECN);
1600f2512ba1SRui Paulo 
1601f2512ba1SRui Paulo 	/*
1602f72167f4SAndre Oppermann 	 * Parse options on any incoming segment.
1603f72167f4SAndre Oppermann 	 */
1604302ce8d6SAndre Oppermann 	tcp_dooptions(&to, (u_char *)(th + 1),
1605302ce8d6SAndre Oppermann 	    (th->th_off << 2) - sizeof(struct tcphdr),
1606302ce8d6SAndre Oppermann 	    (thflags & TH_SYN) ? TO_SYN : 0);
1607fce03f85SRandall Stewart 	if (tp->t_flags2 & TF2_PROC_SACK_PROHIBIT) {
1608fce03f85SRandall Stewart 		/*
1609fce03f85SRandall Stewart 		 * We don't look at sack's from the
1610fce03f85SRandall Stewart 		 * peer because the MSS is too small which
1611fce03f85SRandall Stewart 		 * can subject us to an attack.
1612fce03f85SRandall Stewart 		 */
1613fce03f85SRandall Stewart 		to.to_flags &= ~TOF_SACK;
1614fce03f85SRandall Stewart 	}
1615fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1616fcf59617SAndrey V. Elsukov 	if ((tp->t_flags & TF_SIGNATURE) != 0 &&
1617fcf59617SAndrey V. Elsukov 	    (to.to_flags & TOF_SIGNATURE) == 0) {
1618fcf59617SAndrey V. Elsukov 		TCPSTAT_INC(tcps_sig_err_sigopt);
1619fcf59617SAndrey V. Elsukov 		/* XXX: should drop? */
1620fcf59617SAndrey V. Elsukov 	}
1621fcf59617SAndrey V. Elsukov #endif
1622f72167f4SAndre Oppermann 	/*
1623f72167f4SAndre Oppermann 	 * If echoed timestamp is later than the current time,
1624bf6d304aSAndre Oppermann 	 * fall back to non RFC1323 RTT calculation.  Normalize
1625bf6d304aSAndre Oppermann 	 * timestamp if syncookies were used when this connection
1626bf6d304aSAndre Oppermann 	 * was established.
1627f72167f4SAndre Oppermann 	 */
1628bf6d304aSAndre Oppermann 	if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
1629e16fa5caSJohn-Mark Gurney 		to.to_tsecr -= tp->ts_offset;
16302d05a1c8SRichard Scheffenegger 		if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks())) {
1631f72167f4SAndre Oppermann 			to.to_tsecr = 0;
1632bf6d304aSAndre Oppermann 		}
16332d05a1c8SRichard Scheffenegger 	}
163407dacf03SAndre Oppermann 	/*
163597d8d152SAndre Oppermann 	 * Process options only when we get SYN/ACK back. The SYN case
163697d8d152SAndre Oppermann 	 * for incoming connections is handled in tcp_syncache.
16375396d0f8SAndre Oppermann 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN>
16385396d0f8SAndre Oppermann 	 * or <SYN,ACK>) segment itself is never scaled.
163997d8d152SAndre Oppermann 	 * XXX this is traditional behavior, may need to be cleaned up.
1640df8bae1dSRodney W. Grimes 	 */
16410a389eabSSimon L. B. Nielsen 	if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
16426e16d877SRichard Scheffenegger 		/* Handle parallel SYN for ECN */
1643f7220c48SRichard Scheffenegger 		tcp_ecn_input_parallel_syn(tp, thflags, iptos);
1644464fcfbcSAndre Oppermann 		if ((to.to_flags & TOF_SCALE) &&
16452593f858SRichard Scheffenegger 		    (tp->t_flags & TF_REQ_SCALE) &&
16462593f858SRichard Scheffenegger 		    !(tp->t_flags & TF_NOOPT)) {
1647be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_SCALE;
164802a1a643SAndre Oppermann 			tp->snd_scale = to.to_wscale;
16492d05a1c8SRichard Scheffenegger 		} else {
16508e051165SRichard Scheffenegger 			tp->t_flags &= ~TF_REQ_SCALE;
16512d05a1c8SRichard Scheffenegger 		}
16525396d0f8SAndre Oppermann 		/*
16535396d0f8SAndre Oppermann 		 * Initial send window.  It will be updated with
16545396d0f8SAndre Oppermann 		 * the next incoming segment to the scaled value.
16555396d0f8SAndre Oppermann 		 */
16565396d0f8SAndre Oppermann 		tp->snd_wnd = th->th_win;
16578e051165SRichard Scheffenegger 		if ((to.to_flags & TOF_TS) &&
16582593f858SRichard Scheffenegger 		    (tp->t_flags & TF_REQ_TSTMP) &&
16592593f858SRichard Scheffenegger 		    !(tp->t_flags & TF_NOOPT)) {
1660be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_TSTMP;
1661be2ac88cSJonathan Lemon 			tp->ts_recent = to.to_tsval;
1662d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
16632d05a1c8SRichard Scheffenegger 		} else {
16648e051165SRichard Scheffenegger 			tp->t_flags &= ~TF_REQ_TSTMP;
16652d05a1c8SRichard Scheffenegger 		}
16662d05a1c8SRichard Scheffenegger 		if (to.to_flags & TOF_MSS) {
1667be2ac88cSJonathan Lemon 			tcp_mss(tp, to.to_mss);
16682d05a1c8SRichard Scheffenegger 		}
16693529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
16702593f858SRichard Scheffenegger 		    (!(to.to_flags & TOF_SACKPERM) ||
16712d05a1c8SRichard Scheffenegger 		    (tp->t_flags & TF_NOOPT))) {
16723529149eSAndre Oppermann 			tp->t_flags &= ~TF_SACK_PERMIT;
16732d05a1c8SRichard Scheffenegger 		}
1674dd7b86e2SGleb Smirnoff 		if (tp->t_flags & TF_FASTOPEN) {
16752593f858SRichard Scheffenegger 			if ((to.to_flags & TOF_FASTOPEN) &&
16762593f858SRichard Scheffenegger 			    !(tp->t_flags & TF_NOOPT)) {
1677a026a53aSMichael Tuexen 				uint16_t mss;
1678a026a53aSMichael Tuexen 
16792d05a1c8SRichard Scheffenegger 				if (to.to_flags & TOF_MSS) {
1680a026a53aSMichael Tuexen 					mss = to.to_mss;
16812d05a1c8SRichard Scheffenegger 				} else {
16822d05a1c8SRichard Scheffenegger 					if ((inp->inp_vflag & INP_IPV6) != 0) {
1683a026a53aSMichael Tuexen 						mss = TCP6_MSS;
16842d05a1c8SRichard Scheffenegger 					} else {
1685a026a53aSMichael Tuexen 						mss = TCP_MSS;
16862d05a1c8SRichard Scheffenegger 					}
16872d05a1c8SRichard Scheffenegger 				}
1688a026a53aSMichael Tuexen 				tcp_fastopen_update_cache(tp, mss,
1689a026a53aSMichael Tuexen 				    to.to_tfo_len, to.to_tfo_cookie);
16902d05a1c8SRichard Scheffenegger 			} else {
1691c560df6fSPatrick Kelsey 				tcp_fastopen_disable_path(tp);
1692c560df6fSPatrick Kelsey 			}
16936d90faf3SPaul Saab 		}
16942d05a1c8SRichard Scheffenegger 	}
16956d90faf3SPaul Saab 
1696df8bae1dSRodney W. Grimes 	/*
1697283c76c7SMichael Tuexen 	 * If timestamps were negotiated during SYN/ACK and a
1698283c76c7SMichael Tuexen 	 * segment without a timestamp is received, silently drop
1699d2b3ceddSMichael Tuexen 	 * the segment, unless it is a RST segment or missing timestamps are
1700d2b3ceddSMichael Tuexen 	 * tolerated.
1701283c76c7SMichael Tuexen 	 * See section 3.2 of RFC 7323.
17024da9052aSMichael Tuexen 	 */
17034da9052aSMichael Tuexen 	if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) {
1704d2b3ceddSMichael Tuexen 		if (((thflags & TH_RST) != 0) || V_tcp_tolerate_missing_ts) {
1705cc3c3485SMichael Tuexen 			if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
1706cc3c3485SMichael Tuexen 				log(LOG_DEBUG, "%s; %s: Timestamp missing, "
1707cc3c3485SMichael Tuexen 				    "segment processed normally\n",
1708cc3c3485SMichael Tuexen 				    s, __func__);
1709cc3c3485SMichael Tuexen 				free(s, M_TCPLOG);
1710cc3c3485SMichael Tuexen 			}
1711cc3c3485SMichael Tuexen 		} else {
17124da9052aSMichael Tuexen 			if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
17134da9052aSMichael Tuexen 				log(LOG_DEBUG, "%s; %s: Timestamp missing, "
1714283c76c7SMichael Tuexen 				    "segment silently dropped\n", s, __func__);
17154da9052aSMichael Tuexen 				free(s, M_TCPLOG);
17164da9052aSMichael Tuexen 			}
1717283c76c7SMichael Tuexen 			goto drop;
17184da9052aSMichael Tuexen 		}
1719cc3c3485SMichael Tuexen 	}
1720283c76c7SMichael Tuexen 	/*
1721283c76c7SMichael Tuexen 	 * If timestamps were not negotiated during SYN/ACK and a
172275fcd27aSMichael Tuexen 	 * segment with a timestamp is received, ignore the
1723283c76c7SMichael Tuexen 	 * timestamp and process the packet normally.
1724283c76c7SMichael Tuexen 	 * See section 3.2 of RFC 7323.
1725283c76c7SMichael Tuexen 	 */
17264da9052aSMichael Tuexen 	if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) {
17274da9052aSMichael Tuexen 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
17284da9052aSMichael Tuexen 			log(LOG_DEBUG, "%s; %s: Timestamp not expected, "
1729283c76c7SMichael Tuexen 			    "segment processed normally\n", s, __func__);
17304da9052aSMichael Tuexen 			free(s, M_TCPLOG);
17314da9052aSMichael Tuexen 		}
17324da9052aSMichael Tuexen 	}
17334da9052aSMichael Tuexen 
17344da9052aSMichael Tuexen 	/*
1735df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
1736df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
1737df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
1738df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
1739df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
1740df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
1741df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
1742df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
1743df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
1744df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
1745df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
1746df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
1747a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
1748c5ad39b9SAndre Oppermann 	 * Since we check for TCPS_ESTABLISHED first, it can only
1749a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
1750df8bae1dSRodney W. Grimes 	 */
1751df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
1752c5ad39b9SAndre Oppermann 	    th->th_seq == tp->rcv_nxt &&
1753fb59c426SYoshinobu Inoue 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1754c5ad39b9SAndre Oppermann 	    tp->snd_nxt == tp->snd_max &&
1755c5ad39b9SAndre Oppermann 	    tiwin && tiwin == tp->snd_wnd &&
1756a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
1757c28440dbSRandall Stewart 	    SEGQ_EMPTY(tp) &&
17584741bfcbSPatrick Kelsey 	    ((to.to_flags & TOF_TS) == 0 ||
1759c5ad39b9SAndre Oppermann 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) {
1760df8bae1dSRodney W. Grimes 		/*
1761df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
1762df8bae1dSRodney W. Grimes 		 * record the timestamp.
1763a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
1764a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1765df8bae1dSRodney W. Grimes 		 */
1766be2ac88cSJonathan Lemon 		if ((to.to_flags & TOF_TS) != 0 &&
1767fb59c426SYoshinobu Inoue 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1768d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
1769a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
1770df8bae1dSRodney W. Grimes 		}
1771df8bae1dSRodney W. Grimes 
1772fb59c426SYoshinobu Inoue 		if (tlen == 0) {
1773fb59c426SYoshinobu Inoue 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
1774fb59c426SYoshinobu Inoue 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
1775dbc42409SLawrence Stewart 			    !IN_RECOVERY(tp->t_flags) &&
1776fc30a251SAndre Oppermann 			    (to.to_flags & TOF_SACK) == 0 &&
1777dbc42409SLawrence Stewart 			    TAILQ_EMPTY(&tp->snd_holes)) {
1778df8bae1dSRodney W. Grimes 				/*
1779e8949f74SAndre Oppermann 				 * This is a pure ack for outstanding data.
1780df8bae1dSRodney W. Grimes 				 */
178178b50714SRobert Watson 				TCPSTAT_INC(tcps_predack);
1782252ca428SRobert Watson 
17839b8b58e0SJonathan Lemon 				/*
1784fbcf3b74SMichael Tuexen 				 * "bad retransmit" recovery.
17859b8b58e0SJonathan Lemon 				 */
1786fbcf3b74SMichael Tuexen 				if (tp->t_rxtshift == 1 &&
1787672dc4aeSJohn Baldwin 				    tp->t_flags & TF_PREVVALID &&
17884531b345SRichard Scheffenegger 				    tp->t_badrxtwin != 0 &&
1789fbcf3b74SMichael Tuexen 				    (((to.to_flags & TOF_TS) != 0 &&
1790fbcf3b74SMichael Tuexen 				      to.to_tsecr != 0 &&
1791fbcf3b74SMichael Tuexen 				      TSTMP_LT(to.to_tsecr, tp->t_badrxtwin)) ||
1792fbcf3b74SMichael Tuexen 				     ((to.to_flags & TOF_TS) == 0 &&
1793fbcf3b74SMichael Tuexen 				      TSTMP_LT(ticks, tp->t_badrxtwin))))
1794dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_RTO_ERR);
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 				 */
18832d05a1c8SRichard Scheffenegger 				if ((tp->t_flags & TF_ACKNOW) ||
18842d05a1c8SRichard Scheffenegger 				    (sbavail(&so->so_snd) >=
18852d05a1c8SRichard Scheffenegger 				     SEQ_SUB(tp->snd_max, tp->snd_una))) {
188640fa3e40SGleb Smirnoff 					(void) tcp_output(tp);
18872d05a1c8SRichard 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. */
1930dded4e9eSRichard Scheffenegger 			SOCK_RECVBUF_LOCK(so);
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;
19513e1c8a35SMichael Tuexen 				(void) 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 		}
1985dd7b86e2SGleb Smirnoff 		if (tp->t_flags & TF_FASTOPEN) {
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 			 */
2059dd7b86e2SGleb Smirnoff 			if ((tp->t_flags & TF_FASTOPEN) &&
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);
219640299c55SMichael Tuexen 				tcp_send_challenge_ack(tp, th, m);
21973220a212SGleb Smirnoff 				m = NULL;
21983220a212SGleb Smirnoff 			}
21993220a212SGleb Smirnoff 		}
22003220a212SGleb Smirnoff 		goto drop;
22013220a212SGleb Smirnoff 	}
220280ab7c0eSGarrett Wollman 
22033220a212SGleb Smirnoff 	/*
22043220a212SGleb Smirnoff 	 * RFC5961 Section 4.2
22053220a212SGleb Smirnoff 	 * Send challenge ACK for any SYN in synchronized state.
22063220a212SGleb Smirnoff 	 */
2207281a0fd4SPatrick Kelsey 	if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT &&
2208281a0fd4SPatrick Kelsey 	    tp->t_state != TCPS_SYN_RECEIVED) {
22093220a212SGleb Smirnoff 		TCPSTAT_INC(tcps_badsyn);
22103220a212SGleb Smirnoff 		if (V_tcp_insecure_syn &&
22113220a212SGleb Smirnoff 		    SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
22123220a212SGleb Smirnoff 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
2213d1b07f36SRandall Stewart 			tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
22143220a212SGleb Smirnoff 			tp = tcp_drop(tp, ECONNRESET);
22153220a212SGleb Smirnoff 			rstreason = BANDLIM_UNLIMITED;
22163220a212SGleb Smirnoff 		} else {
221783c1ec92SRichard Scheffenegger 			tcp_ecn_input_syn_sent(tp, thflags, iptos);
221840299c55SMichael Tuexen 			tcp_send_challenge_ack(tp, th, m);
22193220a212SGleb Smirnoff 			m = NULL;
222080ab7c0eSGarrett Wollman 		}
222180ab7c0eSGarrett Wollman 		goto drop;
222280ab7c0eSGarrett Wollman 	}
222380ab7c0eSGarrett Wollman 
222480ab7c0eSGarrett Wollman 	/*
2225df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
2226df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
2227df8bae1dSRodney W. Grimes 	 */
2228be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
222980ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
2230df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
2231d8951c8aSBjoern A. Zeeb 		if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) {
2232df8bae1dSRodney W. Grimes 			/*
2233df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
2234df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
2235df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
2236df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
2237df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
2238df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
2239df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
2240df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
2241df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
2242df8bae1dSRodney W. Grimes 			 */
2243df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
2244df8bae1dSRodney W. Grimes 		} else {
224578b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
224678b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, tlen);
224778b50714SRobert Watson 			TCPSTAT_INC(tcps_pawsdrop);
224807fd333dSMatthew Dillon 			if (tlen)
2249df8bae1dSRodney W. Grimes 				goto dropafterack;
22501ab4789dSMatthew Dillon 			goto drop;
22511ab4789dSMatthew Dillon 		}
2252df8bae1dSRodney W. Grimes 	}
2253df8bae1dSRodney W. Grimes 
2254a0292f23SGarrett Wollman 	/*
225580ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
225680ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
225780ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
225880ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
225980ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
226080ab7c0eSGarrett Wollman 	 */
2261a57815efSBosko Milekic 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
2262a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2263d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
2264a57815efSBosko Milekic 		goto dropwithreset;
2265a57815efSBosko Milekic 	}
226680ab7c0eSGarrett Wollman 
2267fb59c426SYoshinobu Inoue 	todrop = tp->rcv_nxt - th->th_seq;
2268df8bae1dSRodney W. Grimes 	if (todrop > 0) {
2269831ad37eSXin LI 		if (thflags & TH_SYN) {
2270fb59c426SYoshinobu Inoue 			thflags &= ~TH_SYN;
2271fb59c426SYoshinobu Inoue 			th->th_seq++;
2272fb59c426SYoshinobu Inoue 			if (th->th_urp > 1)
2273fb59c426SYoshinobu Inoue 				th->th_urp--;
2274df8bae1dSRodney W. Grimes 			else
2275fb59c426SYoshinobu Inoue 				thflags &= ~TH_URG;
2276df8bae1dSRodney W. Grimes 			todrop--;
2277df8bae1dSRodney W. Grimes 		}
2278df8bae1dSRodney W. Grimes 		/*
2279dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
2280df8bae1dSRodney W. Grimes 		 */
2281fb59c426SYoshinobu Inoue 		if (todrop > tlen
2282fb59c426SYoshinobu Inoue 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
2283dac20301SGarrett Wollman 			/*
2284dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
2285dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
2286dac20301SGarrett Wollman 			 * of sequence; drop it.
2287dac20301SGarrett Wollman 			 */
2288fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
2289dac20301SGarrett Wollman 
2290df8bae1dSRodney W. Grimes 			/*
2291dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
2292dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
2293df8bae1dSRodney W. Grimes 			 */
2294dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
2295fb59c426SYoshinobu Inoue 			todrop = tlen;
229678b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
229778b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, todrop);
2298df8bae1dSRodney W. Grimes 		} else {
229978b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpartduppack);
230078b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop);
2301df8bae1dSRodney W. Grimes 		}
23025acfd95cSMichael Tuexen 		/*
23035acfd95cSMichael Tuexen 		 * DSACK - add SACK block for dropped range
23045acfd95cSMichael Tuexen 		 */
23058f63a52bSMichael Tuexen 		if ((todrop > 0) && (tp->t_flags & TF_SACK_PERMIT)) {
2306ecc5b1d1SMichael Tuexen 			tcp_update_sack_list(tp, th->th_seq,
2307ecc5b1d1SMichael Tuexen 			    th->th_seq + todrop);
23085acfd95cSMichael Tuexen 			/*
23095acfd95cSMichael Tuexen 			 * ACK now, as the next in-sequence segment
23105acfd95cSMichael Tuexen 			 * will clear the DSACK block again
23115acfd95cSMichael Tuexen 			 */
23125acfd95cSMichael Tuexen 			tp->t_flags |= TF_ACKNOW;
23135acfd95cSMichael Tuexen 		}
2314fb59c426SYoshinobu Inoue 		drop_hdrlen += todrop;	/* drop from the top afterwards */
2315fb59c426SYoshinobu Inoue 		th->th_seq += todrop;
2316fb59c426SYoshinobu Inoue 		tlen -= todrop;
2317fb59c426SYoshinobu Inoue 		if (th->th_urp > todrop)
2318fb59c426SYoshinobu Inoue 			th->th_urp -= todrop;
2319df8bae1dSRodney W. Grimes 		else {
2320fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;
2321fb59c426SYoshinobu Inoue 			th->th_urp = 0;
2322df8bae1dSRodney W. Grimes 		}
2323df8bae1dSRodney W. Grimes 	}
2324df8bae1dSRodney W. Grimes 
2325df8bae1dSRodney W. Grimes 	/*
2326df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
2327c9cd686bSMichael Tuexen 	 * user processes are gone, then RST the other end if
2328c9cd686bSMichael Tuexen 	 * no FIN has been processed.
2329df8bae1dSRodney W. Grimes 	 */
2330c9cd686bSMichael Tuexen 	if ((tp->t_flags & TF_CLOSED) && tlen > 0 &&
2331c9cd686bSMichael Tuexen 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
233207dacf03SAndre Oppermann 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
233307dacf03SAndre Oppermann 			log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data "
233407dacf03SAndre Oppermann 			    "after socket was closed, "
233507dacf03SAndre Oppermann 			    "sending RST and removing tcpcb\n",
2336e31d8aa3SMike Silbersack 			    s, __func__, tcpstates[tp->t_state], tlen);
2337773673c1SAndre Oppermann 			free(s, M_TCPLOG);
2338773673c1SAndre Oppermann 		}
2339d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
2340d1b07f36SRandall Stewart 		/* tcp_close will kill the inp pre-log the Reset */
2341d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
2342df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
234378b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvafterclose);
2344a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
2345df8bae1dSRodney W. Grimes 		goto dropwithreset;
2346df8bae1dSRodney W. Grimes 	}
2347df8bae1dSRodney W. Grimes 
2348df8bae1dSRodney W. Grimes 	/*
2349df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
2350df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
2351df8bae1dSRodney W. Grimes 	 */
2352fb59c426SYoshinobu Inoue 	todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
2353df8bae1dSRodney W. Grimes 	if (todrop > 0) {
235478b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvpackafterwin);
2355fb59c426SYoshinobu Inoue 		if (todrop >= tlen) {
235678b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen);
2357df8bae1dSRodney W. Grimes 			/*
2358df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
2359df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
2360df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
2361df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
2362df8bae1dSRodney W. Grimes 			 * and ack.
2363df8bae1dSRodney W. Grimes 			 */
2364fb59c426SYoshinobu Inoue 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
2365df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
236678b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvwinprobe);
2367df8bae1dSRodney W. Grimes 			} else
2368df8bae1dSRodney W. Grimes 				goto dropafterack;
2369df8bae1dSRodney W. Grimes 		} else
237078b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2371df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
2372fb59c426SYoshinobu Inoue 		tlen -= todrop;
2373fb59c426SYoshinobu Inoue 		thflags &= ~(TH_PUSH|TH_FIN);
2374df8bae1dSRodney W. Grimes 	}
2375df8bae1dSRodney W. Grimes 
2376df8bae1dSRodney W. Grimes 	/*
2377df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
2378df8bae1dSRodney W. Grimes 	 * record its timestamp.
2379cf09195bSPaul Saab 	 * NOTE:
2380cf09195bSPaul Saab 	 * 1) That the test incorporates suggestions from the latest
2381a0292f23SGarrett Wollman 	 *    proposal of the tcplw@cray.com list (Braden 1993/04/26).
2382cf09195bSPaul Saab 	 * 2) That updating only on newer timestamps interferes with
2383cf09195bSPaul Saab 	 *    our earlier PAWS tests, so this check should be solely
2384cf09195bSPaul Saab 	 *    predicated on the sequence space of this segment.
2385cf09195bSPaul Saab 	 * 3) That we modify the segment boundary check to be
2386cf09195bSPaul Saab 	 *        Last.ACK.Sent <= SEG.SEQ + SEG.Len
2387cf09195bSPaul Saab 	 *    instead of RFC1323's
2388cf09195bSPaul Saab 	 *        Last.ACK.Sent < SEG.SEQ + SEG.Len,
2389cf09195bSPaul Saab 	 *    This modified check allows us to overcome RFC1323's
2390cf09195bSPaul Saab 	 *    limitations as described in Stevens TCP/IP Illustrated
2391cf09195bSPaul Saab 	 *    Vol. 2 p.869. In such cases, we can still calculate the
2392cf09195bSPaul Saab 	 *    RTT correctly when RCV.NXT == Last.ACK.Sent.
2393df8bae1dSRodney W. Grimes 	 */
2394be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 &&
2395cf09195bSPaul Saab 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
2396cf09195bSPaul Saab 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
2397cf09195bSPaul Saab 		((thflags & (TH_SYN|TH_FIN)) != 0))) {
2398d8951c8aSBjoern A. Zeeb 		tp->ts_recent_age = tcp_ts_getticks();
2399a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
2400df8bae1dSRodney W. Grimes 	}
2401df8bae1dSRodney W. Grimes 
2402df8bae1dSRodney W. Grimes 	/*
2403a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
2404a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
2405a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
2406a0292f23SGarrett Wollman 	 */
2407fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) == 0) {
2408a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
2409281a0fd4SPatrick Kelsey 		    (tp->t_flags & TF_NEEDSYN)) {
2410281a0fd4SPatrick Kelsey 			if (tp->t_state == TCPS_SYN_RECEIVED &&
2411dd7b86e2SGleb Smirnoff 			    (tp->t_flags & TF_FASTOPEN)) {
2412281a0fd4SPatrick Kelsey 				tp->snd_wnd = tiwin;
2413281a0fd4SPatrick Kelsey 				cc_conn_init(tp);
2414281a0fd4SPatrick Kelsey 			}
2415a0292f23SGarrett Wollman 			goto step6;
2416281a0fd4SPatrick Kelsey 		} else if (tp->t_flags & TF_ACKNOW)
24175e1aa279SDavid Malone 			goto dropafterack;
2418a0292f23SGarrett Wollman 		else
2419a0292f23SGarrett Wollman 			goto drop;
2420a0292f23SGarrett Wollman 	}
2421df8bae1dSRodney W. Grimes 
2422df8bae1dSRodney W. Grimes 	/*
2423df8bae1dSRodney W. Grimes 	 * Ack processing.
2424df8bae1dSRodney W. Grimes 	 */
242537b3e6a6SMichael Tuexen 	if (SEQ_GEQ(tp->snd_una, tp->iss + (TCP_MAXWIN << tp->snd_scale))) {
2426646c28eaSMichael Tuexen 		/* Checking SEG.ACK against ISS is definitely redundant. */
2427646c28eaSMichael Tuexen 		tp->t_flags2 |= TF2_NO_ISS_CHECK;
2428646c28eaSMichael Tuexen 	}
2429646c28eaSMichael Tuexen 	if (!V_tcp_insecure_ack) {
2430646c28eaSMichael Tuexen 		tcp_seq seq_min;
2431646c28eaSMichael Tuexen 		bool ghost_ack_check;
2432646c28eaSMichael Tuexen 
2433646c28eaSMichael Tuexen 		if (tp->t_flags2 & TF2_NO_ISS_CHECK) {
2434646c28eaSMichael Tuexen 			/* Check for too old ACKs (RFC 5961, Section 5.2). */
2435646c28eaSMichael Tuexen 			seq_min = tp->snd_una - tp->max_sndwnd;
2436646c28eaSMichael Tuexen 			ghost_ack_check = false;
2437646c28eaSMichael Tuexen 		} else {
2438646c28eaSMichael Tuexen 			if (SEQ_GT(tp->iss + 1, tp->snd_una - tp->max_sndwnd)) {
2439646c28eaSMichael Tuexen 				/* Checking for ghost ACKs is stricter. */
2440646c28eaSMichael Tuexen 				seq_min = tp->iss + 1;
2441646c28eaSMichael Tuexen 				ghost_ack_check = true;
2442646c28eaSMichael Tuexen 			} else {
2443646c28eaSMichael Tuexen 				/*
2444646c28eaSMichael Tuexen 				 * Checking for too old ACKs (RFC 5961,
2445646c28eaSMichael Tuexen 				 * Section 5.2) is stricter.
2446646c28eaSMichael Tuexen 				 */
2447646c28eaSMichael Tuexen 				seq_min = tp->snd_una - tp->max_sndwnd;
2448646c28eaSMichael Tuexen 				ghost_ack_check = false;
2449646c28eaSMichael Tuexen 			}
2450646c28eaSMichael Tuexen 		}
2451646c28eaSMichael Tuexen 		if (SEQ_LT(th->th_ack, seq_min)) {
2452646c28eaSMichael Tuexen 			if (ghost_ack_check)
2453646c28eaSMichael Tuexen 				TCPSTAT_INC(tcps_rcvghostack);
2454646c28eaSMichael Tuexen 			else
2455646c28eaSMichael Tuexen 				TCPSTAT_INC(tcps_rcvacktooold);
245640299c55SMichael Tuexen 			tcp_send_challenge_ack(tp, th, m);
2457646c28eaSMichael Tuexen 			m = NULL;
2458646c28eaSMichael Tuexen 			goto drop;
2459646c28eaSMichael Tuexen 		}
2460646c28eaSMichael Tuexen 	}
2461df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2462df8bae1dSRodney W. Grimes 	/*
2463764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
2464764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
2465764d8cefSBill Fenner 	 * The ACK was checked above.
2466df8bae1dSRodney W. Grimes 	 */
2467df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2468a0292f23SGarrett Wollman 
246978b50714SRobert Watson 		TCPSTAT_INC(tcps_connects);
2470493105c2SGleb Smirnoff 		if (tp->t_flags & TF_SONOTCONN) {
2471493105c2SGleb Smirnoff 			/*
2472493105c2SGleb Smirnoff 			 * Usually SYN_RECEIVED had been created from a LISTEN,
2473493105c2SGleb Smirnoff 			 * and solisten_enqueue() has already marked the socket
2474493105c2SGleb Smirnoff 			 * layer as connected.  If it didn't, which can happen
2475493105c2SGleb Smirnoff 			 * only with an accept_filter(9), then the tp is marked
2476493105c2SGleb Smirnoff 			 * with TF_SONOTCONN.  The other reason for this mark
2477493105c2SGleb Smirnoff 			 * to be set is a simultaneous open, a SYN_RECEIVED
2478493105c2SGleb Smirnoff 			 * that had been created from SYN_SENT.
2479493105c2SGleb Smirnoff 			 */
2480493105c2SGleb Smirnoff 			tp->t_flags &= ~TF_SONOTCONN;
2481df8bae1dSRodney W. Grimes 			soisconnected(so);
2482e80062a2SGleb Smirnoff 		}
2483df8bae1dSRodney W. Grimes 		/* Do window scaling? */
2484df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2485df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
2486df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
2487df8bae1dSRodney W. Grimes 		}
2488116ef4d6SMichael Tuexen 		tp->snd_wnd = tiwin;
2489a0292f23SGarrett Wollman 		/*
2490a0292f23SGarrett Wollman 		 * Make transitions:
2491a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
2492a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
2493a0292f23SGarrett Wollman 		 */
24949b8b58e0SJonathan Lemon 		tp->t_starttime = ticks;
2495dd7b86e2SGleb Smirnoff 		if ((tp->t_flags & TF_FASTOPEN) && tp->t_tfo_pending) {
2496281a0fd4SPatrick Kelsey 			tcp_fastopen_decrement_counter(tp->t_tfo_pending);
2497281a0fd4SPatrick Kelsey 			tp->t_tfo_pending = NULL;
2498281a0fd4SPatrick Kelsey 		}
24998db239dcSMichael Tuexen 		if (tp->t_flags & TF_NEEDFIN) {
250008af8aacSRandall Stewart 			tp->t_acktime = ticks;
25018db239dcSMichael Tuexen 			tcp_state_change(tp, TCPS_FIN_WAIT_1);
25028db239dcSMichael Tuexen 			tp->t_flags &= ~TF_NEEDFIN;
25038db239dcSMichael Tuexen 		} else {
25048db239dcSMichael Tuexen 			tcp_state_change(tp, TCPS_ESTABLISHED);
25058db239dcSMichael Tuexen 			TCP_PROBE5(accept__established, NULL, tp,
25068db239dcSMichael Tuexen 			    m, tp, th);
2507281a0fd4SPatrick Kelsey 			/*
2508281a0fd4SPatrick Kelsey 			 * TFO connections call cc_conn_init() during SYN
2509281a0fd4SPatrick Kelsey 			 * processing.  Calling it again here for such
2510281a0fd4SPatrick Kelsey 			 * connections is not harmless as it would undo the
2511281a0fd4SPatrick Kelsey 			 * snd_cwnd reduction that occurs when a TFO SYN|ACK
2512281a0fd4SPatrick Kelsey 			 * is retransmitted.
2513281a0fd4SPatrick Kelsey 			 */
2514dd7b86e2SGleb Smirnoff 			if (!(tp->t_flags & TF_FASTOPEN))
2515dbc42409SLawrence Stewart 				cc_conn_init(tp);
25169077f387SGleb Smirnoff 			tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp));
25177ff19458SPaul Traina 		}
2518a0292f23SGarrett Wollman 		/*
2519b2ade6b1SRichard Scheffenegger 		 * Account for the ACK of our SYN prior to
2520b2ade6b1SRichard Scheffenegger 		 * regular ACK processing below, except for
2521b2ade6b1SRichard Scheffenegger 		 * simultaneous SYN, which is handled later.
2522b2ade6b1SRichard Scheffenegger 		 */
2523b2ade6b1SRichard Scheffenegger 		if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN))
2524b2ade6b1SRichard Scheffenegger 			incforsyn = 1;
2525b2ade6b1SRichard Scheffenegger 		/*
2526a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
2527a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
2528a0292f23SGarrett Wollman 		 */
2529032bf749SRichard Scheffenegger 		if (tlen == 0 && (thflags & TH_FIN) == 0) {
2530c28440dbSRandall Stewart 			(void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0,
2531a0292f23SGarrett Wollman 			    (struct mbuf *)0);
2532c348e880SGleb Smirnoff 			tcp_handle_wakeup(tp);
2533032bf749SRichard Scheffenegger 		}
253460ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq - 1;
253593b0017fSPhilippe Charnier 		/* FALLTHROUGH */
2536df8bae1dSRodney W. Grimes 
2537df8bae1dSRodney W. Grimes 	/*
2538df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
2539df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
2540fb59c426SYoshinobu Inoue 	 *	tp->snd_una < th->th_ack <= tp->snd_max
2541fb59c426SYoshinobu Inoue 	 * then advance tp->snd_una to th->th_ack and drop
2542df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
2543df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
2544df8bae1dSRodney W. Grimes 	 */
2545df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
2546df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
2547df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
2548df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
2549df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
2550df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
25515a53ca16SPaul Saab 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
255278b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvacktoomuch);
25535a53ca16SPaul Saab 			goto dropafterack;
25545a53ca16SPaul Saab 		}
2555c21b7b55SRichard Scheffenegger 		if (tcp_is_sack_recovery(tp, &to)) {
2556893ed42eSRichard Scheffenegger 			sack_changed = tcp_sack_doack(tp, &to, th->th_ack);
2557893ed42eSRichard Scheffenegger 			if ((sack_changed != SACK_NOCHANGE) &&
25580471a8c7SRichard Scheffenegger 			    (tp->t_flags & TF_LRD)) {
25590471a8c7SRichard Scheffenegger 				tcp_sack_lost_retransmission(tp, th);
25600471a8c7SRichard Scheffenegger 			}
25610471a8c7SRichard Scheffenegger 		} else
256212eeb81fSHiren Panchasara 			/*
256312eeb81fSHiren Panchasara 			 * Reset the value so that previous (valid) value
256412eeb81fSHiren Panchasara 			 * from the last ack with SACK doesn't get used.
256512eeb81fSHiren Panchasara 			 */
256612eeb81fSHiren Panchasara 			tp->sackhint.sacked_bytes = 0;
256739bc9de5SLawrence Stewart 
2568bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
256939bc9de5SLawrence Stewart 		/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
257039bc9de5SLawrence Stewart 		hhook_run_tcp_est_in(tp, th, &to);
2571bd79708dSJonathan T. Looney #endif
257239bc9de5SLawrence Stewart 
2573fb59c426SYoshinobu Inoue 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
25740c39d38dSGleb Smirnoff 			maxseg = tcp_maxseg(tp);
2575021eaf79SHiren Panchasara 			if (tlen == 0 &&
2576021eaf79SHiren Panchasara 			    (tiwin == tp->snd_wnd ||
2577021eaf79SHiren Panchasara 			    (tp->t_flags & TF_SACK_PERMIT))) {
257862b90589SPeter Wemm 				/*
257962b90589SPeter Wemm 				 * If this is the first time we've seen a
258062b90589SPeter Wemm 				 * FIN from the remote, this is not a
258162b90589SPeter Wemm 				 * duplicate and it needs to be processed
258262b90589SPeter Wemm 				 * normally.  This happens during a
258362b90589SPeter Wemm 				 * simultaneous close.
258462b90589SPeter Wemm 				 */
258562b90589SPeter Wemm 				if ((thflags & TH_FIN) &&
258662b90589SPeter Wemm 				    (TCPS_HAVERCVDFIN(tp->t_state) == 0)) {
258762b90589SPeter Wemm 					tp->t_dupacks = 0;
258862b90589SPeter Wemm 					break;
258962b90589SPeter Wemm 				}
259078b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvdupack);
2591df8bae1dSRodney W. Grimes 				/*
2592df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
2593df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
2594df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
25955f30ec9bSEitan Adler 				 * change and FIN isn't set),
25965f30ec9bSEitan Adler 				 * the ack is the biggest we've
2597df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
2598a4641f4eSPedro F. Giffuni 				 * threshold of them, assume a packet
2599df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
2600df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
2601df8bae1dSRodney W. Grimes 				 * window so we send only this one
2602df8bae1dSRodney W. Grimes 				 * packet.
2603df8bae1dSRodney W. Grimes 				 *
2604df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
2605df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
2606df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
2607df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
2608df8bae1dSRodney W. Grimes 				 * the new ssthresh).
2609df8bae1dSRodney W. Grimes 				 *
2610df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
2611df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
2612df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
2613df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
2614df8bae1dSRodney W. Grimes 				 * network.
2615f2512ba1SRui Paulo 				 *
2616f2512ba1SRui Paulo 				 * When using TCP ECN, notify the peer that
2617f2512ba1SRui Paulo 				 * we reduced the cwnd.
2618df8bae1dSRodney W. Grimes 				 */
2619021eaf79SHiren Panchasara 				/*
2620021eaf79SHiren Panchasara 				 * Following 2 kinds of acks should not affect
2621021eaf79SHiren Panchasara 				 * dupack counting:
2622021eaf79SHiren Panchasara 				 * 1) Old acks
2623021eaf79SHiren Panchasara 				 * 2) Acks with SACK but without any new SACK
2624021eaf79SHiren Panchasara 				 * information in them. These could result from
2625021eaf79SHiren Panchasara 				 * any anomaly in the network like a switch
2626021eaf79SHiren Panchasara 				 * duplicating packets or a possible DoS attack.
2627021eaf79SHiren Panchasara 				 */
2628021eaf79SHiren Panchasara 				if (th->th_ack != tp->snd_una ||
2629c21b7b55SRichard Scheffenegger 				    (tcp_is_sack_recovery(tp, &to) &&
26302d05a1c8SRichard Scheffenegger 				    (sack_changed == SACK_NOCHANGE))) {
2631021eaf79SHiren Panchasara 					break;
26322d05a1c8SRichard Scheffenegger 				} else if (!tcp_timer_active(tp, TT_REXMT)) {
2633df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
26342d05a1c8SRichard Scheffenegger 				} else if (++tp->t_dupacks > tcprexmtthresh ||
2635dbc42409SLawrence Stewart 					    IN_FASTRECOVERY(tp->t_flags)) {
26364b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
26374b7b743cSLawrence Stewart 					    CC_DUPACK);
26380e1d7c25SRichard Scheffenegger 					if (V_tcp_do_prr &&
263949a6fbe3SRichard Scheffenegger 					    IN_FASTRECOVERY(tp->t_flags) &&
264049a6fbe3SRichard Scheffenegger 					    (tp->t_flags & TF_SACK_PERMIT)) {
2641c7c325d0SRichard Scheffenegger 						tcp_do_prr_ack(tp, th, &to,
2642c7c325d0SRichard Scheffenegger 						    sack_changed, &maxseg);
2643c21b7b55SRichard Scheffenegger 					} else if (tcp_is_sack_recovery(tp, &to) &&
26447dc78150SRichard Scheffenegger 						    IN_FASTRECOVERY(tp->t_flags) &&
26457dc78150SRichard Scheffenegger 						    (tp->snd_nxt == tp->snd_max)) {
2646808f11b7SPaul Saab 						int awnd;
264725e6f9edSPaul Saab 
264825e6f9edSPaul Saab 						/*
264925e6f9edSPaul Saab 						 * Compute the amount of data in flight first.
26504c3972f0SHiren Panchasara 						 * We can inject new data into the pipe iff
26517dc78150SRichard Scheffenegger 						 * we have less than ssthresh
265225e6f9edSPaul Saab 						 * worth of data in flight.
265325e6f9edSPaul Saab 						 */
265412eeb81fSHiren Panchasara 						awnd = tcp_compute_pipe(tp);
2655808f11b7SPaul Saab 						if (awnd < tp->snd_ssthresh) {
26567dc78150SRichard Scheffenegger 							tp->snd_cwnd += imax(maxseg,
26577dc78150SRichard Scheffenegger 							    imin(2 * maxseg,
26587dc78150SRichard Scheffenegger 							    tp->sackhint.delivered_data));
265925e6f9edSPaul Saab 							if (tp->snd_cwnd > tp->snd_ssthresh)
266025e6f9edSPaul Saab 								tp->snd_cwnd = tp->snd_ssthresh;
266125e6f9edSPaul Saab 						}
26627dc78150SRichard Scheffenegger 					} else if (tcp_is_sack_recovery(tp, &to) &&
26637dc78150SRichard Scheffenegger 						    IN_FASTRECOVERY(tp->t_flags) &&
26647dc78150SRichard Scheffenegger 						    SEQ_LT(tp->snd_nxt, tp->snd_max)) {
26657dc78150SRichard Scheffenegger 						tp->snd_cwnd += imax(maxseg,
26667dc78150SRichard Scheffenegger 						    imin(2 * maxseg,
26677dc78150SRichard Scheffenegger 						    tp->sackhint.delivered_data));
26682d05a1c8SRichard Scheffenegger 					} else {
26690c39d38dSGleb Smirnoff 						tp->snd_cwnd += maxseg;
26702d05a1c8SRichard Scheffenegger 					}
267140fa3e40SGleb Smirnoff 					(void) tcp_output(tp);
267246f58482SJonathan Lemon 					goto drop;
26733c40e1d5SRichard Scheffenegger 				} else if (tp->t_dupacks == tcprexmtthresh ||
26743c40e1d5SRichard Scheffenegger 					    (tp->t_flags & TF_SACK_PERMIT &&
2675d1de2b05SRichard Scheffenegger 					     V_tcp_do_newsack &&
26763c40e1d5SRichard Scheffenegger 					     tp->sackhint.sacked_bytes >
26773c40e1d5SRichard Scheffenegger 					     (tcprexmtthresh - 1) * maxseg)) {
26783c40e1d5SRichard Scheffenegger enter_recovery:
26793c40e1d5SRichard Scheffenegger 					/*
26803c40e1d5SRichard Scheffenegger 					 * Above is the RFC6675 trigger condition of
26813c40e1d5SRichard Scheffenegger 					 * more than (dupthresh-1)*maxseg sacked data.
26823c40e1d5SRichard Scheffenegger 					 * If the count of holes in the
26833c40e1d5SRichard Scheffenegger 					 * scoreboard is >= dupthresh, we could
26843c40e1d5SRichard Scheffenegger 					 * also enter loss recovery, but don't
26853c40e1d5SRichard Scheffenegger 					 * have that value readily available.
26863c40e1d5SRichard Scheffenegger 					 */
26873c40e1d5SRichard Scheffenegger 					tp->t_dupacks = tcprexmtthresh;
2688cb942153SJeffrey Hsu 					tcp_seq onxt = tp->snd_nxt;
2689a0445c2eSJayanth Vijayaraghavan 
2690a0445c2eSJayanth Vijayaraghavan 					/*
26917dc78150SRichard Scheffenegger 					 * If we're doing sack, check to
26927dc78150SRichard Scheffenegger 					 * see if we're already in sack
2693a0445c2eSJayanth Vijayaraghavan 					 * recovery. If we're not doing sack,
2694a0445c2eSJayanth Vijayaraghavan 					 * check to see if we're in newreno
2695a0445c2eSJayanth Vijayaraghavan 					 * recovery.
2696a0445c2eSJayanth Vijayaraghavan 					 */
26977dc78150SRichard Scheffenegger 					if (tcp_is_sack_recovery(tp, &to)) {
2698dbc42409SLawrence Stewart 						if (IN_FASTRECOVERY(tp->t_flags)) {
2699a0445c2eSJayanth Vijayaraghavan 							tp->t_dupacks = 0;
2700a0445c2eSJayanth Vijayaraghavan 							break;
2701a0445c2eSJayanth Vijayaraghavan 						}
2702dbc42409SLawrence Stewart 					} else {
2703a0445c2eSJayanth Vijayaraghavan 						if (SEQ_LEQ(th->th_ack,
27049d11646dSJeffrey Hsu 						    tp->snd_recover)) {
2705cb942153SJeffrey Hsu 							tp->t_dupacks = 0;
2706cb942153SJeffrey Hsu 							break;
270746f58482SJonathan Lemon 						}
2708a0445c2eSJayanth Vijayaraghavan 					}
2709dbc42409SLawrence Stewart 					/* Congestion signal before ack. */
2710dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_NDUPACK);
27114b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
27124b7b743cSLawrence Stewart 					    CC_DUPACK);
2713b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
27149b8b58e0SJonathan Lemon 					tp->t_rtttime = 0;
27150e1d7c25SRichard Scheffenegger 					if (V_tcp_do_prr) {
27160e1d7c25SRichard Scheffenegger 						/*
27177dc78150SRichard Scheffenegger 						 * snd_ssthresh and snd_recover are
27187dc78150SRichard Scheffenegger 						 * already updated by cc_cong_signal.
27190e1d7c25SRichard Scheffenegger 						 */
2720c21b7b55SRichard Scheffenegger 						if (tcp_is_sack_recovery(tp, &to)) {
272149a6fbe3SRichard Scheffenegger 							/*
27227dc78150SRichard Scheffenegger 							 * Include Limited Transmit
272349a6fbe3SRichard Scheffenegger 							 * segments here
272449a6fbe3SRichard Scheffenegger 							 */
2725e9071000SRichard Scheffenegger 							tp->sackhint.prr_delivered =
27267dc78150SRichard Scheffenegger 							    imin(tp->snd_max - th->th_ack,
27277dc78150SRichard Scheffenegger 							    (tp->snd_limited + 1) * maxseg);
272874d7fc87SRichard Scheffenegger 						} else {
272974d7fc87SRichard Scheffenegger 							tp->sackhint.prr_delivered =
27307dc78150SRichard Scheffenegger 							    maxseg;
273174d7fc87SRichard Scheffenegger 						}
2732bc7ee8e5SRichard Scheffenegger 						tp->sackhint.recover_fs = max(1,
2733bc7ee8e5SRichard Scheffenegger 						    tp->snd_nxt - tp->snd_una);
27340e1d7c25SRichard Scheffenegger 					}
27357dc78150SRichard Scheffenegger 					tp->snd_limited = 0;
2736c21b7b55SRichard Scheffenegger 					if (tcp_is_sack_recovery(tp, &to)) {
27372d05a1c8SRichard Scheffenegger 						TCPSTAT_INC(tcps_sack_recovery_episode);
27387dc78150SRichard Scheffenegger 						/*
27397dc78150SRichard Scheffenegger 						 * When entering LR after RTO due to
27407dc78150SRichard Scheffenegger 						 * Duplicate ACKs, retransmit existing
27417dc78150SRichard Scheffenegger 						 * holes from the scoreboard.
27427dc78150SRichard Scheffenegger 						 */
27437dc78150SRichard Scheffenegger 						tcp_resend_sackholes(tp);
27447dc78150SRichard Scheffenegger 						/* Avoid inflating cwnd in tcp_output */
27457dc78150SRichard Scheffenegger 						tp->snd_nxt = tp->snd_max;
27467dc78150SRichard Scheffenegger 						tp->snd_cwnd = tcp_compute_pipe(tp) +
27477dc78150SRichard Scheffenegger 						    maxseg;
274840fa3e40SGleb Smirnoff 						(void) tcp_output(tp);
27497dc78150SRichard Scheffenegger 						/* Set cwnd to the expected flightsize */
27507dc78150SRichard Scheffenegger 						tp->snd_cwnd = tp->snd_ssthresh;
27512d05a1c8SRichard Scheffenegger 						if (SEQ_GT(th->th_ack, tp->snd_una)) {
27523c40e1d5SRichard Scheffenegger 							goto resume_partialack;
27532d05a1c8SRichard Scheffenegger 						}
27546d90faf3SPaul Saab 						goto drop;
27556d90faf3SPaul Saab 					}
2756fb59c426SYoshinobu Inoue 					tp->snd_nxt = th->th_ack;
27570c39d38dSGleb Smirnoff 					tp->snd_cwnd = maxseg;
275840fa3e40SGleb Smirnoff 					(void) tcp_output(tp);
275948d2549cSJeffrey Hsu 					KASSERT(tp->snd_limited <= 2,
2760302ce8d6SAndre Oppermann 					    ("%s: tp->snd_limited too big",
2761302ce8d6SAndre Oppermann 					    __func__));
2762df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
27630c39d38dSGleb Smirnoff 					     maxseg *
276448d2549cSJeffrey Hsu 					     (tp->t_dupacks - tp->snd_limited);
2765df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
2766df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
2767df8bae1dSRodney W. Grimes 					goto drop;
2768603724d3SBjoern A. Zeeb 				} else if (V_tcp_do_rfc3042) {
276962d4443fSHiren Panchasara 					/*
277062d4443fSHiren Panchasara 					 * Process first and second duplicate
277162d4443fSHiren Panchasara 					 * ACKs. Each indicates a segment
277262d4443fSHiren Panchasara 					 * leaving the network, creating room
277362d4443fSHiren Panchasara 					 * for more. Make sure we can send a
277462d4443fSHiren Panchasara 					 * packet on reception of each duplicate
277562d4443fSHiren Panchasara 					 * ACK by increasing snd_cwnd by one
277662d4443fSHiren Panchasara 					 * segment. Restore the original
277762d4443fSHiren Panchasara 					 * snd_cwnd after packet transmission.
277862d4443fSHiren Panchasara 					 */
27792d05a1c8SRichard Scheffenegger 					cc_ack_received(tp, th, nsegs, CC_DUPACK);
27803ac12506SJonathan T. Looney 					uint32_t oldcwnd = tp->snd_cwnd;
278148d2549cSJeffrey Hsu 					tcp_seq oldsndmax = tp->snd_max;
278248d2549cSJeffrey Hsu 					u_int sent;
27835628dd08SAndre Oppermann 					int avail;
278489c02376SJeffrey Hsu 
2785582a954bSJeffrey Hsu 					KASSERT(tp->t_dupacks == 1 ||
2786582a954bSJeffrey Hsu 					    tp->t_dupacks == 2,
2787302ce8d6SAndre Oppermann 					    ("%s: dupacks not 1 or 2",
2788302ce8d6SAndre Oppermann 					    __func__));
278961a36e3dSJeffrey Hsu 					if (tp->t_dupacks == 1)
279048d2549cSJeffrey Hsu 						tp->snd_limited = 0;
2791440f4ba1SRichard Scheffenegger 					if ((tp->snd_nxt == tp->snd_max) &&
2792440f4ba1SRichard Scheffenegger 					    (tp->t_rxtshift == 0))
279361a36e3dSJeffrey Hsu 						tp->snd_cwnd =
2794440f4ba1SRichard Scheffenegger 						    SEQ_SUB(tp->snd_nxt,
27957dc78150SRichard Scheffenegger 							    tp->snd_una) -
27967dc78150SRichard Scheffenegger 							tcp_sack_adjust(tp);
2797440f4ba1SRichard Scheffenegger 					tp->snd_cwnd +=
279861a36e3dSJeffrey Hsu 					    (tp->t_dupacks - tp->snd_limited) *
27998f5a2e21SRichard Scheffenegger 					    maxseg - tcp_sack_adjust(tp);
28005628dd08SAndre Oppermann 					/*
28015628dd08SAndre Oppermann 					 * Only call tcp_output when there
28025ae83e0dSMichael Tuexen 					 * is new data available to be sent
28035ae83e0dSMichael Tuexen 					 * or we need to send an ACK.
28045628dd08SAndre Oppermann 					 */
2805dded4e9eSRichard Scheffenegger 					SOCK_SENDBUF_LOCK(so);
28062d05a1c8SRichard Scheffenegger 					avail = sbavail(&so->so_snd);
2807dded4e9eSRichard Scheffenegger 					SOCK_SENDBUF_UNLOCK(so);
28082d05a1c8SRichard Scheffenegger 					if (tp->t_flags & TF_ACKNOW ||
28092d05a1c8SRichard Scheffenegger 					    (avail >=
28102d05a1c8SRichard Scheffenegger 					     SEQ_SUB(tp->snd_nxt, tp->snd_una))) {
281140fa3e40SGleb Smirnoff 						(void) tcp_output(tp);
28122d05a1c8SRichard Scheffenegger 					}
28132d05a1c8SRichard Scheffenegger 					sent = SEQ_SUB(tp->snd_max, oldsndmax);
28140c39d38dSGleb Smirnoff 					if (sent > maxseg) {
281589c02376SJeffrey Hsu 						KASSERT((tp->t_dupacks == 2 &&
281689c02376SJeffrey Hsu 						    tp->snd_limited == 0) ||
28170c39d38dSGleb Smirnoff 						   (sent == maxseg + 1 &&
281889c02376SJeffrey Hsu 						    tp->t_flags & TF_SENTFIN),
2819302ce8d6SAndre Oppermann 						    ("%s: sent too much",
2820302ce8d6SAndre Oppermann 						    __func__));
282148d2549cSJeffrey Hsu 						tp->snd_limited = 2;
28222d05a1c8SRichard Scheffenegger 					} else if (sent > 0) {
282348d2549cSJeffrey Hsu 						++tp->snd_limited;
28242d05a1c8SRichard Scheffenegger 					}
2825582a954bSJeffrey Hsu 					tp->snd_cwnd = oldcwnd;
2826582a954bSJeffrey Hsu 					goto drop;
2827df8bae1dSRodney W. Grimes 				}
2828021eaf79SHiren Panchasara 			}
2829df8bae1dSRodney W. Grimes 			break;
2830021eaf79SHiren Panchasara 		} else {
2831021eaf79SHiren Panchasara 			/*
2832021eaf79SHiren Panchasara 			 * This ack is advancing the left edge, reset the
2833021eaf79SHiren Panchasara 			 * counter.
2834021eaf79SHiren Panchasara 			 */
2835021eaf79SHiren Panchasara 			tp->t_dupacks = 0;
2836021eaf79SHiren Panchasara 			/*
2837021eaf79SHiren Panchasara 			 * If this ack also has new SACK info, increment the
2838f359d6ebSRichard Scheffenegger 			 * counter as per rfc6675. The variable
2839f359d6ebSRichard Scheffenegger 			 * sack_changed tracks all changes to the SACK
2840f359d6ebSRichard Scheffenegger 			 * scoreboard, including when partial ACKs without
2841f359d6ebSRichard Scheffenegger 			 * SACK options are received, and clear the scoreboard
2842f359d6ebSRichard Scheffenegger 			 * from the left side. Such partial ACKs should not be
2843f359d6ebSRichard Scheffenegger 			 * counted as dupacks here.
2844021eaf79SHiren Panchasara 			 */
2845c21b7b55SRichard Scheffenegger 			if (tcp_is_sack_recovery(tp, &to) &&
2846440f4ba1SRichard Scheffenegger 			    (((tp->t_rxtshift == 0) && (sack_changed != SACK_NOCHANGE)) ||
2847440f4ba1SRichard Scheffenegger 			     ((tp->t_rxtshift > 0) && (sack_changed == SACK_NEWLOSS))) &&
2848440f4ba1SRichard Scheffenegger 			    (tp->snd_nxt == tp->snd_max)) {
2849021eaf79SHiren Panchasara 				tp->t_dupacks++;
28503c40e1d5SRichard Scheffenegger 				/* limit overhead by setting maxseg last */
28513c40e1d5SRichard Scheffenegger 				if (!IN_FASTRECOVERY(tp->t_flags) &&
28523c40e1d5SRichard Scheffenegger 				    (tp->sackhint.sacked_bytes >
28533c40e1d5SRichard Scheffenegger 				    ((tcprexmtthresh - 1) *
28543c40e1d5SRichard Scheffenegger 				    (maxseg = tcp_maxseg(tp))))) {
28553c40e1d5SRichard Scheffenegger 					goto enter_recovery;
28563c40e1d5SRichard Scheffenegger 				}
28573c40e1d5SRichard Scheffenegger 			}
2858df8bae1dSRodney W. Grimes 		}
2859cb942153SJeffrey Hsu 
28603c40e1d5SRichard Scheffenegger resume_partialack:
2861302ce8d6SAndre Oppermann 		KASSERT(SEQ_GT(th->th_ack, tp->snd_una),
2862302ce8d6SAndre Oppermann 		    ("%s: th_ack <= snd_una", __func__));
2863cb942153SJeffrey Hsu 
2864df8bae1dSRodney W. Grimes 		/*
2865df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
2866df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
2867df8bae1dSRodney W. Grimes 		 */
2868cb942153SJeffrey Hsu 		if (SEQ_LT(th->th_ack, tp->snd_recover)) {
28690b3f9e43SRichard Scheffenegger 			if (IN_FASTRECOVERY(tp->t_flags)) {
2870c7c325d0SRichard Scheffenegger 				if (tp->t_flags & TF_SACK_PERMIT) {
28710b3f9e43SRichard Scheffenegger 					if (V_tcp_do_prr &&
28720b3f9e43SRichard Scheffenegger 					    (to.to_flags & TOF_SACK)) {
28730b3f9e43SRichard Scheffenegger 						tcp_timer_activate(tp,
28740b3f9e43SRichard Scheffenegger 						    TT_REXMT, 0);
2875eb3a59a8SRichard Scheffenegger 						tp->t_rtttime = 0;
2876c7c325d0SRichard Scheffenegger 						tcp_do_prr_ack(tp, th, &to,
2877c7c325d0SRichard Scheffenegger 						    sack_changed, &maxseg);
2878eb3a59a8SRichard Scheffenegger 						tp->t_flags |= TF_ACKNOW;
2879eb3a59a8SRichard Scheffenegger 						(void) tcp_output(tp);
2880c7c325d0SRichard Scheffenegger 					} else {
2881c7c325d0SRichard Scheffenegger 						tcp_sack_partialack(tp, th,
2882c7c325d0SRichard Scheffenegger 						    &maxseg);
2883c7c325d0SRichard Scheffenegger 					}
2884c7c325d0SRichard Scheffenegger 				} else {
2885c068736aSJeffrey Hsu 					tcp_newreno_partial_ack(tp, th);
2886c7c325d0SRichard Scheffenegger 				}
28870b3f9e43SRichard Scheffenegger 			} else if (IN_CONGRECOVERY(tp->t_flags) &&
28880b3f9e43SRichard Scheffenegger 				    (V_tcp_do_prr)) {
28890b3f9e43SRichard Scheffenegger 				tp->sackhint.delivered_data =
28900b3f9e43SRichard Scheffenegger 				    BYTES_THIS_ACK(tp, th);
2891b9f803b7SRichard Scheffenegger 				tp->snd_fack = th->th_ack;
289249a6fbe3SRichard Scheffenegger 				/*
289349a6fbe3SRichard Scheffenegger 				 * During ECN cwnd reduction
289449a6fbe3SRichard Scheffenegger 				 * always use PRR-SSRB
289549a6fbe3SRichard Scheffenegger 				 */
2896c7c325d0SRichard Scheffenegger 				tcp_do_prr_ack(tp, th, &to, SACK_CHANGE,
2897c7c325d0SRichard Scheffenegger 				    &maxseg);
2898b9f803b7SRichard Scheffenegger 				(void) tcp_output(tp);
2899b9f803b7SRichard Scheffenegger 			}
2900c7c325d0SRichard Scheffenegger 		}
2901a0292f23SGarrett Wollman 		/*
2902a0292f23SGarrett Wollman 		 * If we reach this point, ACK is not a duplicate,
2903a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
2904a0292f23SGarrett Wollman 		 */
2905a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
2906a0292f23SGarrett Wollman 			/*
2907a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
2908a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
290907e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
291007e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
291107e43e10SAndras Olah 			 * we can do window scaling.
2912a0292f23SGarrett Wollman 			 */
2913a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
2914a0292f23SGarrett Wollman 			tp->snd_una++;
291507e43e10SAndras Olah 			/* Do window scaling? */
291607e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
291707e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
291807e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2919464fcfbcSAndre Oppermann 				/* Send window already scaled. */
292007e43e10SAndras Olah 			}
2921a0292f23SGarrett Wollman 		}
2922a0292f23SGarrett Wollman 
2923a0292f23SGarrett Wollman process_ACK:
29249eb0e832SGleb Smirnoff 		INP_WLOCK_ASSERT(inp);
29257cfc6904SRobert Watson 
2926b2ade6b1SRichard Scheffenegger 		/*
2927b2ade6b1SRichard Scheffenegger 		 * Adjust for the SYN bit in sequence space,
2928b2ade6b1SRichard Scheffenegger 		 * but don't account for it in cwnd calculations.
2929b2ade6b1SRichard Scheffenegger 		 * This is for the SYN_RECEIVED, non-simultaneous
2930b2ade6b1SRichard Scheffenegger 		 * SYN case. SYN_SENT and simultaneous SYN are
2931b2ade6b1SRichard Scheffenegger 		 * treated elsewhere.
2932b2ade6b1SRichard Scheffenegger 		 */
2933b2ade6b1SRichard Scheffenegger 		if (incforsyn)
2934b2ade6b1SRichard Scheffenegger 			tp->snd_una++;
2935dbc42409SLawrence Stewart 		acked = BYTES_THIS_ACK(tp, th);
2936b8c2cd15SJonathan T. Looney 		KASSERT(acked >= 0, ("%s: acked unexepectedly negative "
2937b8c2cd15SJonathan T. Looney 		    "(tp->snd_una=%u, th->th_ack=%u, tp=%p, m=%p)", __func__,
2938b8c2cd15SJonathan T. Looney 		    tp->snd_una, th->th_ack, tp, m));
29394b7b743cSLawrence Stewart 		TCPSTAT_ADD(tcps_rcvackpack, nsegs);
294078b50714SRobert Watson 		TCPSTAT_ADD(tcps_rcvackbyte, acked);
2941df8bae1dSRodney W. Grimes 
2942df8bae1dSRodney W. Grimes 		/*
29439b8b58e0SJonathan Lemon 		 * If we just performed our first retransmit, and the ACK
29449b8b58e0SJonathan Lemon 		 * arrives within our recovery window, then it was a mistake
29459b8b58e0SJonathan Lemon 		 * to do the retransmit in the first place.  Recover our
29469b8b58e0SJonathan Lemon 		 * original cwnd and ssthresh, and proceed to transmit where
29479b8b58e0SJonathan Lemon 		 * we left off.
29489b8b58e0SJonathan Lemon 		 */
294910d20c84SMatt Macy 		if (tp->t_rxtshift == 1 &&
295010d20c84SMatt Macy 		    tp->t_flags & TF_PREVVALID &&
29514531b345SRichard Scheffenegger 		    tp->t_badrxtwin != 0 &&
29524531b345SRichard Scheffenegger 		    to.to_flags & TOF_TS &&
29534531b345SRichard Scheffenegger 		    to.to_tsecr != 0 &&
29544531b345SRichard Scheffenegger 		    TSTMP_LT(to.to_tsecr, tp->t_badrxtwin))
2955dbc42409SLawrence Stewart 			cc_cong_signal(tp, th, CC_RTO_ERR);
29569b8b58e0SJonathan Lemon 
29579b8b58e0SJonathan Lemon 		/*
2958df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
2959df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
2960df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
2961df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
2962df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
2963df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
2964df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
2965fa55172bSMatthew Dillon 		 *
2966fa55172bSMatthew Dillon 		 * Some boxes send broken timestamp replies
2967fa55172bSMatthew Dillon 		 * during the SYN+ACK phase, ignore
2968fa55172bSMatthew Dillon 		 * timestamps of 0 or we could calculate a
2969fa55172bSMatthew Dillon 		 * huge RTT and blow up the retransmit timer.
2970df8bae1dSRodney W. Grimes 		 */
2971d8951c8aSBjoern A. Zeeb 		if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) {
29723ac12506SJonathan T. Looney 			uint32_t t;
2973d8951c8aSBjoern A. Zeeb 
2974d8951c8aSBjoern A. Zeeb 			t = tcp_ts_getticks() - to.to_tsecr;
2975d8951c8aSBjoern A. Zeeb 			if (!tp->t_rttlow || tp->t_rttlow > t)
2976d8951c8aSBjoern A. Zeeb 				tp->t_rttlow = t;
2977d8951c8aSBjoern A. Zeeb 			tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1);
2978fa55172bSMatthew Dillon 		} else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
2979eaf80179SAndre Oppermann 			if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime)
2980eaf80179SAndre Oppermann 				tp->t_rttlow = ticks - tp->t_rtttime;
29819b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
2982fa55172bSMatthew Dillon 		}
2983df8bae1dSRodney W. Grimes 
2984dded4e9eSRichard Scheffenegger 		SOCK_SENDBUF_LOCK(so);
298508af8aacSRandall Stewart 		/*
298608af8aacSRandall Stewart 		 * Clear t_acktime if remote side has ACKd all data in the
298708af8aacSRandall Stewart 		 * socket buffer and FIN (if applicable).
298808af8aacSRandall Stewart 		 * Otherwise, update t_acktime if we received a sufficiently
298908af8aacSRandall Stewart 		 * large ACK.
299008af8aacSRandall Stewart 		 */
299108af8aacSRandall Stewart 		if ((tp->t_state <= TCPS_CLOSE_WAIT &&
299208af8aacSRandall Stewart 		    acked == sbavail(&so->so_snd)) ||
299308af8aacSRandall Stewart 		    acked > sbavail(&so->so_snd))
299408af8aacSRandall Stewart 			tp->t_acktime = 0;
299508af8aacSRandall Stewart 		else if (acked > 1)
299608af8aacSRandall Stewart 			tp->t_acktime = ticks;
299708af8aacSRandall Stewart 
2998df8bae1dSRodney W. Grimes 		/*
2999df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
3000df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
3001df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
3002df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
3003df8bae1dSRodney W. Grimes 		 */
3004fb59c426SYoshinobu Inoue 		if (th->th_ack == tp->snd_max) {
3005b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
3006df8bae1dSRodney W. Grimes 			needoutput = 1;
3007b8152ba7SAndre Oppermann 		} else if (!tcp_timer_active(tp, TT_PERSIST))
300808af8aacSRandall Stewart 			tcp_timer_activate(tp, TT_REXMT, TP_RXTCUR(tp));
3009a0292f23SGarrett Wollman 
3010a0292f23SGarrett Wollman 		/*
3011a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
3012a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
3013a0292f23SGarrett Wollman 		 */
301408af8aacSRandall Stewart 		if (acked == 0) {
3015dded4e9eSRichard Scheffenegger 			SOCK_SENDBUF_UNLOCK(so);
3016a0292f23SGarrett Wollman 			goto step6;
301708af8aacSRandall Stewart 		}
3018a0292f23SGarrett Wollman 
3019df8bae1dSRodney W. Grimes 		/*
3020dbc42409SLawrence Stewart 		 * Let the congestion control algorithm update congestion
3021dbc42409SLawrence Stewart 		 * control related information. This typically means increasing
3022dbc42409SLawrence Stewart 		 * the congestion window.
3023df8bae1dSRodney W. Grimes 		 */
30244b7b743cSLawrence Stewart 		cc_ack_received(tp, th, nsegs, CC_ACK);
3025dbc42409SLawrence Stewart 
3026cfa6009eSGleb Smirnoff 		if (acked > sbavail(&so->so_snd)) {
3027b8c2cd15SJonathan T. Looney 			if (tp->snd_wnd >= sbavail(&so->so_snd))
3028cfa6009eSGleb Smirnoff 				tp->snd_wnd -= sbavail(&so->so_snd);
3029b8c2cd15SJonathan T. Looney 			else
3030b8c2cd15SJonathan T. Looney 				tp->snd_wnd = 0;
3031c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd,
3032cfa6009eSGleb Smirnoff 			    (int)sbavail(&so->so_snd));
3033df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
3034df8bae1dSRodney W. Grimes 		} else {
3035c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd, acked);
30363ac12506SJonathan T. Looney 			if (tp->snd_wnd >= (uint32_t) acked)
303760ee3bb2SAndre Oppermann 				tp->snd_wnd -= acked;
3038b8c2cd15SJonathan T. Looney 			else
3039b8c2cd15SJonathan T. Looney 				tp->snd_wnd = 0;
3040df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
3041df8bae1dSRodney W. Grimes 		}
3042032bf749SRichard Scheffenegger 		/* NB: sowwakeup_locked() does an implicit unlock. */
3043032bf749SRichard Scheffenegger 		sowwakeup_locked(so);
3044c11a15bfSGleb Smirnoff 		m_freem(mfree);
3045e8949f74SAndre Oppermann 		/* Detect una wraparound. */
3046dbc42409SLawrence Stewart 		if (!IN_RECOVERY(tp->t_flags) &&
30479d11646dSJeffrey Hsu 		    SEQ_GT(tp->snd_una, tp->snd_recover) &&
30489d11646dSJeffrey Hsu 		    SEQ_LEQ(th->th_ack, tp->snd_recover))
30499d11646dSJeffrey Hsu 			tp->snd_recover = th->th_ack - 1;
30500b3f9e43SRichard Scheffenegger 		tp->snd_una = th->th_ack;
3051dbc42409SLawrence Stewart 		if (IN_RECOVERY(tp->t_flags) &&
305224cb0f22SLawrence Stewart 		    SEQ_GEQ(th->th_ack, tp->snd_recover)) {
30530b3f9e43SRichard Scheffenegger 			cc_post_recovery(tp, th);
305424cb0f22SLawrence Stewart 		}
30557dc78150SRichard Scheffenegger 		if (SEQ_GT(tp->snd_una, tp->snd_recover)) {
30566d90faf3SPaul Saab 			tp->snd_recover = tp->snd_una;
30576d90faf3SPaul Saab 		}
3058df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
3059df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
3060df8bae1dSRodney W. Grimes 
3061df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
3062df8bae1dSRodney W. Grimes 		/*
3063df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
3064df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
3065df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
3066df8bae1dSRodney W. Grimes 		 */
3067df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
3068df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
3069df8bae1dSRodney W. Grimes 				/*
3070df8bae1dSRodney W. Grimes 				 * If we can't receive any more
3071df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
3072df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
3073df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
3074df8bae1dSRodney W. Grimes 				 * we'll hang forever.
3075340c35deSJonathan Lemon 				 */
3076c0b99ffaSRobert Watson 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
30773eeb22cbSRichard Scheffenegger 					tcp_free_sackholes(tp);
307803e49181SSeigo Tanimura 					soisdisconnected(so);
30799077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
30809077f387SGleb Smirnoff 					    (tcp_fast_finwait2_recycle ?
30819077f387SGleb Smirnoff 					    tcp_finwait2_timeout :
30829077f387SGleb Smirnoff 					    TP_MAXIDLE(tp)));
30834cc20ab1SSeigo Tanimura 				}
308457f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_2);
3085df8bae1dSRodney W. Grimes 			}
3086df8bae1dSRodney W. Grimes 			break;
3087df8bae1dSRodney W. Grimes 
3088df8bae1dSRodney W. Grimes 		/*
3089df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
3090df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
3091df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
3092df8bae1dSRodney W. Grimes 		 * the segment.
3093df8bae1dSRodney W. Grimes 		 */
3094df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
3095df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
309611a20fb8SJeffrey Hsu 				tcp_twstart(tp);
3097340c35deSJonathan Lemon 				m_freem(m);
3098679d9708SAndre Oppermann 				return;
3099df8bae1dSRodney W. Grimes 			}
3100df8bae1dSRodney W. Grimes 			break;
3101df8bae1dSRodney W. Grimes 
3102df8bae1dSRodney W. Grimes 		/*
3103df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
3104df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
3105df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
3106df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
3107df8bae1dSRodney W. Grimes 		 */
3108df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
3109df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
3110df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
3111df8bae1dSRodney W. Grimes 				goto drop;
3112df8bae1dSRodney W. Grimes 			}
3113df8bae1dSRodney W. Grimes 			break;
3114df8bae1dSRodney W. Grimes 		}
3115df8bae1dSRodney W. Grimes 	}
3116df8bae1dSRodney W. Grimes 
3117df8bae1dSRodney W. Grimes step6:
31189eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
31197cfc6904SRobert Watson 
3120df8bae1dSRodney W. Grimes 	/*
312160ee3bb2SAndre Oppermann 	 * Update window information.
312260ee3bb2SAndre Oppermann 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
3123df8bae1dSRodney W. Grimes 	 */
312460ee3bb2SAndre Oppermann 	if ((thflags & TH_ACK) &&
312560ee3bb2SAndre Oppermann 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
312660ee3bb2SAndre Oppermann 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
312760ee3bb2SAndre Oppermann 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
312860ee3bb2SAndre Oppermann 		/* keep track of pure window updates */
312960ee3bb2SAndre Oppermann 		if (tlen == 0 &&
313060ee3bb2SAndre Oppermann 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
313178b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvwinupd);
3132df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
313360ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq;
313460ee3bb2SAndre Oppermann 		tp->snd_wl2 = th->th_ack;
3135df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
3136df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
313760ee3bb2SAndre Oppermann 		needoutput = 1;
3138df8bae1dSRodney W. Grimes 	}
3139df8bae1dSRodney W. Grimes 
3140df8bae1dSRodney W. Grimes 	/*
3141df8bae1dSRodney W. Grimes 	 * Process segments with URG.
3142df8bae1dSRodney W. Grimes 	 */
3143fb59c426SYoshinobu Inoue 	if ((thflags & TH_URG) && th->th_urp &&
3144df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
3145df8bae1dSRodney W. Grimes 		/*
3146df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
3147df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
3148df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
3149df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
3150df8bae1dSRodney W. Grimes 		 */
3151dded4e9eSRichard Scheffenegger 		SOCK_RECVBUF_LOCK(so);
3152cfa6009eSGleb Smirnoff 		if (th->th_urp + sbavail(&so->so_rcv) > sb_max) {
3153fb59c426SYoshinobu Inoue 			th->th_urp = 0;			/* XXX */
3154fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;		/* XXX */
3155dded4e9eSRichard Scheffenegger 			SOCK_RECVBUF_UNLOCK(so);	/* XXX */
3156df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
3157df8bae1dSRodney W. Grimes 		}
3158df8bae1dSRodney W. Grimes 		/*
3159df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
3160df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
3161df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
3162df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
3163df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
3164df8bae1dSRodney W. Grimes 		 *
3165df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
3166df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
3167df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
3168df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
3169df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
3170df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
3171df8bae1dSRodney W. Grimes 		 */
3172fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
3173fb59c426SYoshinobu Inoue 			tp->rcv_up = th->th_seq + th->th_urp;
3174cfa6009eSGleb Smirnoff 			so->so_oobmark = sbavail(&so->so_rcv) +
3175df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
3176927c5ceaSRobert Watson 			if (so->so_oobmark == 0)
3177c0b99ffaSRobert Watson 				so->so_rcv.sb_state |= SBS_RCVATMARK;
3178df8bae1dSRodney W. Grimes 			sohasoutofband(so);
3179df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
3180df8bae1dSRodney W. Grimes 		}
3181dded4e9eSRichard Scheffenegger 		SOCK_RECVBUF_UNLOCK(so);
3182df8bae1dSRodney W. Grimes 		/*
3183df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
3184df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
3185df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
3186df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
3187df8bae1dSRodney W. Grimes 		 */
31883ac12506SJonathan T. Looney 		if (th->th_urp <= (uint32_t)tlen &&
318930613f56SJeffrey Hsu 		    !(so->so_options & SO_OOBINLINE)) {
319030613f56SJeffrey Hsu 			/* hdr drop is delayed */
319130613f56SJeffrey Hsu 			tcp_pulloutofband(so, th, m, drop_hdrlen);
319230613f56SJeffrey Hsu 		}
3193c068736aSJeffrey Hsu 	} else {
3194df8bae1dSRodney W. Grimes 		/*
3195df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
3196df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
3197df8bae1dSRodney W. Grimes 		 * with the receive window.
3198df8bae1dSRodney W. Grimes 		 */
3199df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
3200df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
3201c068736aSJeffrey Hsu 	}
3202df8bae1dSRodney W. Grimes dodata:							/* XXX */
32039eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
32047cfc6904SRobert Watson 
3205df8bae1dSRodney W. Grimes 	/*
3206df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
3207df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
3208df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
3209df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
3210df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
3211df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
3212df8bae1dSRodney W. Grimes 	 */
3213281a0fd4SPatrick Kelsey 	tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
3214dd7b86e2SGleb Smirnoff 	    (tp->t_flags & TF_FASTOPEN));
3215f1ea4e41SRandall Stewart 	if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) &&
3216df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
321769e03620SPaul Saab 		tcp_seq save_start = th->th_seq;
32185acfd95cSMichael Tuexen 		tcp_seq save_rnxt  = tp->rcv_nxt;
32195acfd95cSMichael Tuexen 		int     save_tlen  = tlen;
3220fb59c426SYoshinobu Inoue 		m_adj(m, drop_hdrlen);	/* delayed header drop */
3221e4b64281SJesper Skriver 		/*
3222c068736aSJeffrey Hsu 		 * Insert segment which includes th into TCP reassembly queue
3223c068736aSJeffrey Hsu 		 * with control block tp.  Set thflags to whether reassembly now
3224c068736aSJeffrey Hsu 		 * includes a segment with FIN.  This handles the common case
3225c068736aSJeffrey Hsu 		 * inline (segment is the next to be received on an established
3226c068736aSJeffrey Hsu 		 * connection, and the queue is empty), avoiding linkage into
3227c068736aSJeffrey Hsu 		 * and removal from the queue and repetition of various
3228c068736aSJeffrey Hsu 		 * conversions.
3229c068736aSJeffrey Hsu 		 * Set DELACK for segments received in order, but ack
3230c068736aSJeffrey Hsu 		 * immediately when segments are out of order (so
3231c068736aSJeffrey Hsu 		 * fast retransmit can work).
3232e4b64281SJesper Skriver 		 */
32334741bfcbSPatrick Kelsey 		if (th->th_seq == tp->rcv_nxt &&
3234c28440dbSRandall Stewart 		    SEGQ_EMPTY(tp) &&
3235281a0fd4SPatrick Kelsey 		    (TCPS_HAVEESTABLISHED(tp->t_state) ||
3236281a0fd4SPatrick Kelsey 		     tfo_syn)) {
3237281a0fd4SPatrick Kelsey 			if (DELAY_ACK(tp, tlen) || tfo_syn)
32383bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
3239e4b64281SJesper Skriver 			else
3240e4b64281SJesper Skriver 				tp->t_flags |= TF_ACKNOW;
3241e4b64281SJesper Skriver 			tp->rcv_nxt += tlen;
3242e854dd38SRandall Stewart 			if (tlen &&
3243e854dd38SRandall Stewart 			    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
3244e854dd38SRandall Stewart 			    (tp->t_fbyte_in == 0)) {
3245e854dd38SRandall Stewart 				tp->t_fbyte_in = ticks;
3246e854dd38SRandall Stewart 				if (tp->t_fbyte_in == 0)
3247e854dd38SRandall Stewart 					tp->t_fbyte_in = 1;
3248e854dd38SRandall Stewart 				if (tp->t_fbyte_out && tp->t_fbyte_in)
3249e854dd38SRandall Stewart 					tp->t_flags2 |= TF2_FBYTES_COMPLETE;
3250e854dd38SRandall Stewart 			}
32511ebf4607SRichard Scheffenegger 			thflags = tcp_get_flags(th) & TH_FIN;
325278b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpack);
325378b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
3254dded4e9eSRichard Scheffenegger 			SOCK_RECVBUF_LOCK(so);
3255c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
3256c1c36a2cSMike Silbersack 				m_freem(m);
3257c1c36a2cSMike Silbersack 			else
3258651e4e6aSGleb Smirnoff 				sbappendstream_locked(&so->so_rcv, m, 0);
32594d0770f1SRichard Scheffenegger 			tp->t_flags |= TF_WAKESOR;
3260e4b64281SJesper Skriver 		} else {
3261e8949f74SAndre Oppermann 			/*
3262e8949f74SAndre Oppermann 			 * XXX: Due to the header drop above "th" is
3263e8949f74SAndre Oppermann 			 * theoretically invalid by now.  Fortunately
3264e8949f74SAndre Oppermann 			 * m_adj() doesn't actually frees any mbufs
3265e8949f74SAndre Oppermann 			 * when trimming from the head.
3266e8949f74SAndre Oppermann 			 */
32675acfd95cSMichael Tuexen 			tcp_seq temp = save_start;
32684747500dSRandall Stewart 
32695acfd95cSMichael Tuexen 			thflags = tcp_reass(tp, th, &temp, &tlen, m);
3270e4b64281SJesper Skriver 			tp->t_flags |= TF_ACKNOW;
3271e4b64281SJesper Skriver 		}
327240f41eceSMichael Tuexen 		if ((tp->t_flags & TF_SACK_PERMIT) &&
327340f41eceSMichael Tuexen 		    (save_tlen > 0) &&
327440f41eceSMichael Tuexen 		    TCPS_HAVEESTABLISHED(tp->t_state)) {
32756d261981SMichael Tuexen 			if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) {
3276b5a154d8SMichael Tuexen 				/*
3277b5a154d8SMichael Tuexen 				 * DSACK actually handled in the fastpath
3278b5a154d8SMichael Tuexen 				 * above.
3279b5a154d8SMichael Tuexen 				 */
3280ecc5b1d1SMichael Tuexen 				tcp_update_sack_list(tp, save_start,
3281ecc5b1d1SMichael Tuexen 				    save_start + save_tlen);
3282ecc5b1d1SMichael Tuexen 			} else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) {
3283ecc5b1d1SMichael Tuexen 				if ((tp->rcv_numsacks >= 1) &&
3284ecc5b1d1SMichael Tuexen 				    (tp->sackblks[0].end == save_start)) {
32856d261981SMichael Tuexen 					/*
32866d261981SMichael Tuexen 					 * Partial overlap, recorded at todrop
32876d261981SMichael Tuexen 					 * above.
32886d261981SMichael Tuexen 					 */
32896d261981SMichael Tuexen 					tcp_update_sack_list(tp,
32906d261981SMichael Tuexen 					    tp->sackblks[0].start,
3291ecc5b1d1SMichael Tuexen 					    tp->sackblks[0].end);
3292ecc5b1d1SMichael Tuexen 				} else {
3293ecc5b1d1SMichael Tuexen 					tcp_update_dsack_list(tp, save_start,
3294ecc5b1d1SMichael Tuexen 					    save_start + save_tlen);
3295ecc5b1d1SMichael Tuexen 				}
32966d261981SMichael Tuexen 			} else if (tlen >= save_tlen) {
3297b5a154d8SMichael Tuexen 				/* Update of sackblks. */
3298ecc5b1d1SMichael Tuexen 				tcp_update_dsack_list(tp, save_start,
3299ecc5b1d1SMichael Tuexen 				    save_start + save_tlen);
3300ecc5b1d1SMichael Tuexen 			} else if (tlen > 0) {
3301ecc5b1d1SMichael Tuexen 				tcp_update_dsack_list(tp, save_start,
3302ecc5b1d1SMichael Tuexen 				    save_start + tlen);
33035acfd95cSMichael Tuexen 			}
33045acfd95cSMichael Tuexen 		}
3305c348e880SGleb Smirnoff 		tcp_handle_wakeup(tp);
3306302ce8d6SAndre Oppermann #if 0
3307df8bae1dSRodney W. Grimes 		/*
3308df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
3309df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
3310df8bae1dSRodney W. Grimes 		 * buffer size.
3311302ce8d6SAndre Oppermann 		 * XXX: Unused.
3312df8bae1dSRodney W. Grimes 		 */
3313f701e30dSJohn Baldwin 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt))
3314df8bae1dSRodney W. Grimes 			len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
3315f701e30dSJohn Baldwin 		else
3316f701e30dSJohn Baldwin 			len = so->so_rcv.sb_hiwat;
3317302ce8d6SAndre Oppermann #endif
3318df8bae1dSRodney W. Grimes 	} else {
3319e8c149abSMichael Tuexen 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
3320e8c149abSMichael Tuexen 			if (tlen > 0) {
3321e8c149abSMichael Tuexen 				if ((thflags & TH_FIN) != 0) {
3322e8c149abSMichael Tuexen 					log(LOG_DEBUG, "%s; %s: %s: "
3323e8c149abSMichael Tuexen 					    "Received %d bytes of data and FIN "
3324e8c149abSMichael Tuexen 					    "after having received a FIN, "
3325e8c149abSMichael Tuexen 					    "just dropping both\n",
3326e8c149abSMichael Tuexen 					    s, __func__,
3327e8c149abSMichael Tuexen 					    tcpstates[tp->t_state], tlen);
3328e8c149abSMichael Tuexen 				} else {
3329e8c149abSMichael Tuexen 					log(LOG_DEBUG, "%s; %s: %s: "
3330e8c149abSMichael Tuexen 					    "Received %d bytes of data "
3331e8c149abSMichael Tuexen 					    "after having received a FIN, "
3332e8c149abSMichael Tuexen 					    "just dropping it\n",
3333e8c149abSMichael Tuexen 					    s, __func__,
3334e8c149abSMichael Tuexen 					    tcpstates[tp->t_state], tlen);
3335e8c149abSMichael Tuexen 				}
3336e8c149abSMichael Tuexen 			} else {
3337e8c149abSMichael Tuexen 				if ((thflags & TH_FIN) != 0) {
3338e8c149abSMichael Tuexen 					log(LOG_DEBUG, "%s; %s: %s: "
3339e8c149abSMichael Tuexen 					    "Received FIN "
3340e8c149abSMichael Tuexen 					    "after having received a FIN, "
3341e8c149abSMichael Tuexen 					    "just dropping it\n",
3342e8c149abSMichael Tuexen 					    s, __func__,
3343e8c149abSMichael Tuexen 					    tcpstates[tp->t_state]);
3344e8c149abSMichael Tuexen 				}
3345e8c149abSMichael Tuexen 			}
3346e8c149abSMichael Tuexen 			free(s, M_TCPLOG);
3347e8c149abSMichael Tuexen 		}
3348df8bae1dSRodney W. Grimes 		m_freem(m);
3349fb59c426SYoshinobu Inoue 		thflags &= ~TH_FIN;
3350df8bae1dSRodney W. Grimes 	}
3351df8bae1dSRodney W. Grimes 
3352df8bae1dSRodney W. Grimes 	/*
3353df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
3354df8bae1dSRodney W. Grimes 	 * that the connection is closing.
3355df8bae1dSRodney W. Grimes 	 */
3356fb59c426SYoshinobu Inoue 	if (thflags & TH_FIN) {
3357df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
33584d0770f1SRichard Scheffenegger 			/* The socket upcall is handled by socantrcvmore. */
3359032bf749SRichard Scheffenegger 			socantrcvmore(so);
3360a0292f23SGarrett Wollman 			/*
3361a0292f23SGarrett Wollman 			 * If connection is half-synchronized
3362d3eede9dSAndras Olah 			 * (ie NEEDSYN flag on) then delay ACK,
3363a0292f23SGarrett Wollman 			 * so it may be piggybacked when SYN is sent.
3364a0292f23SGarrett Wollman 			 * Otherwise, since we received a FIN then no
3365a0292f23SGarrett Wollman 			 * more input can be expected, send ACK now.
3366a0292f23SGarrett Wollman 			 */
33673bfd6421SJonathan Lemon 			if (tp->t_flags & TF_NEEDSYN)
33683bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
3369a0292f23SGarrett Wollman 			else
3370df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
3371df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
3372df8bae1dSRodney W. Grimes 		}
3373df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
3374df8bae1dSRodney W. Grimes 		/*
3375df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
3376df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
3377df8bae1dSRodney W. Grimes 		 */
3378df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
33799b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
33809b8b58e0SJonathan Lemon 			/* FALLTHROUGH */
3381df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
338257f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSE_WAIT);
3383df8bae1dSRodney W. Grimes 			break;
3384df8bae1dSRodney W. Grimes 
3385df8bae1dSRodney W. Grimes 		/*
3386df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
3387df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
3388df8bae1dSRodney W. Grimes 		 */
3389df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
339057f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSING);
3391df8bae1dSRodney W. Grimes 			break;
3392df8bae1dSRodney W. Grimes 
3393df8bae1dSRodney W. Grimes 		/*
3394df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
3395df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
3396df8bae1dSRodney W. Grimes 		 * standard timers.
3397df8bae1dSRodney W. Grimes 		 */
3398df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
3399340c35deSJonathan Lemon 			tcp_twstart(tp);
3400679d9708SAndre Oppermann 			return;
3401df8bae1dSRodney W. Grimes 		}
3402df8bae1dSRodney W. Grimes 	}
34032b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
3404df8bae1dSRodney W. Grimes 
3405df8bae1dSRodney W. Grimes 	/*
3406df8bae1dSRodney W. Grimes 	 * Return any desired output.
3407df8bae1dSRodney W. Grimes 	 */
34082d05a1c8SRichard Scheffenegger 	if (needoutput || (tp->t_flags & TF_ACKNOW)) {
340940fa3e40SGleb Smirnoff 		(void) tcp_output(tp);
34102d05a1c8SRichard Scheffenegger 	}
3411a14c749fSJonathan Lemon check_delack:
34129eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
3413252ca428SRobert Watson 
3414a14c749fSJonathan Lemon 	if (tp->t_flags & TF_DELACK) {
34153bfd6421SJonathan Lemon 		tp->t_flags &= ~TF_DELACK;
3416b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
34173bfd6421SJonathan Lemon 	}
34189eb0e832SGleb Smirnoff 	INP_WUNLOCK(inp);
3419679d9708SAndre Oppermann 	return;
3420df8bae1dSRodney W. Grimes 
3421df8bae1dSRodney W. Grimes dropafterack:
3422df8bae1dSRodney W. Grimes 	/*
3423df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
3424df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
342580ab7c0eSGarrett Wollman 	 *
342680ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
342780ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
342880ab7c0eSGarrett Wollman 	 * RST have been dropped.
342980ab7c0eSGarrett Wollman 	 *
343080ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
343180ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
343280ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
343380ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
343480ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
343580ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
3436df8bae1dSRodney W. Grimes 	 */
3437fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
3438fb59c426SYoshinobu Inoue 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
3439a57815efSBosko Milekic 	     SEQ_GT(th->th_ack, tp->snd_max)) ) {
3440a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
3441d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
3442a57815efSBosko Milekic 		goto dropwithreset;
3443a57815efSBosko Milekic 	}
34442b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
3445df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
344640fa3e40SGleb Smirnoff 	(void) tcp_output(tp);
34479eb0e832SGleb Smirnoff 	INP_WUNLOCK(inp);
3448d6915262SRobert Watson 	m_freem(m);
3449679d9708SAndre Oppermann 	return;
3450df8bae1dSRodney W. Grimes 
3451df8bae1dSRodney W. Grimes dropwithreset:
3452a0ca0871SRobert Watson 	if (tp != NULL) {
3453302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
34549eb0e832SGleb Smirnoff 		INP_WUNLOCK(inp);
3455dd8ac7f9SRobert Watson 	} else
3456a0ca0871SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
3457679d9708SAndre Oppermann 	return;
3458df8bae1dSRodney W. Grimes 
3459df8bae1dSRodney W. Grimes drop:
3460df8bae1dSRodney W. Grimes 	/*
3461df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
3462df8bae1dSRodney W. Grimes 	 */
34632b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
34644d0770f1SRichard Scheffenegger 	if (tp != NULL) {
34659eb0e832SGleb Smirnoff 		INP_WUNLOCK(inp);
34664d0770f1SRichard Scheffenegger 	}
3467d6915262SRobert Watson 	m_freem(m);
3468302ce8d6SAndre Oppermann }
3469302ce8d6SAndre Oppermann 
3470302ce8d6SAndre Oppermann /*
34710ca3f933SAndre Oppermann  * Issue RST and make ACK acceptable to originator of segment.
34720ca3f933SAndre Oppermann  * The mbuf must still include the original packet header.
34730ca3f933SAndre Oppermann  * tp may be NULL.
3474302ce8d6SAndre Oppermann  */
347555bceb1eSRandall Stewart void
tcp_dropwithreset(struct mbuf * m,struct tcphdr * th,struct tcpcb * tp,int tlen,int rstreason)3476302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp,
3477302ce8d6SAndre Oppermann     int tlen, int rstreason)
3478302ce8d6SAndre Oppermann {
3479b287c6c7SBjoern A. Zeeb #ifdef INET
3480302ce8d6SAndre Oppermann 	struct ip *ip;
3481b287c6c7SBjoern A. Zeeb #endif
3482302ce8d6SAndre Oppermann #ifdef INET6
3483302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6;
3484302ce8d6SAndre Oppermann #endif
34857a3244ccSRobert Watson 
34867a3244ccSRobert Watson 	if (tp != NULL) {
34879eb0e832SGleb Smirnoff 		INP_LOCK_ASSERT(tptoinpcb(tp));
34887a3244ccSRobert Watson 	}
34897a3244ccSRobert Watson 
34900ca3f933SAndre Oppermann 	/* Don't bother if destination was broadcast/multicast. */
34911ebf4607SRichard Scheffenegger 	if ((tcp_get_flags(th) & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
3492302ce8d6SAndre Oppermann 		goto drop;
3493302ce8d6SAndre Oppermann #ifdef INET6
3494302ce8d6SAndre Oppermann 	if (mtod(m, struct ip *)->ip_v == 6) {
3495302ce8d6SAndre Oppermann 		ip6 = mtod(m, struct ip6_hdr *);
3496302ce8d6SAndre Oppermann 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
3497302ce8d6SAndre Oppermann 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
3498302ce8d6SAndre Oppermann 			goto drop;
3499302ce8d6SAndre Oppermann 		/* IPv6 anycast check is done at tcp6_input() */
3500b287c6c7SBjoern A. Zeeb 	}
3501302ce8d6SAndre Oppermann #endif
3502b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3503b287c6c7SBjoern A. Zeeb 	else
3504b287c6c7SBjoern A. Zeeb #endif
3505b287c6c7SBjoern A. Zeeb #ifdef INET
3506302ce8d6SAndre Oppermann 	{
3507302ce8d6SAndre Oppermann 		ip = mtod(m, struct ip *);
3508302ce8d6SAndre Oppermann 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
3509302ce8d6SAndre Oppermann 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
3510302ce8d6SAndre Oppermann 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
3511197fc4caSGleb Smirnoff 		    in_ifnet_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
3512302ce8d6SAndre Oppermann 			goto drop;
3513302ce8d6SAndre Oppermann 	}
3514b287c6c7SBjoern A. Zeeb #endif
3515302ce8d6SAndre Oppermann 
3516302ce8d6SAndre Oppermann 	/* Perform bandwidth limiting. */
3517302ce8d6SAndre Oppermann 	if (badport_bandlim(rstreason) < 0)
3518302ce8d6SAndre Oppermann 		goto drop;
3519302ce8d6SAndre Oppermann 
3520302ce8d6SAndre Oppermann 	/* tcp_respond consumes the mbuf chain. */
35211ebf4607SRichard Scheffenegger 	if (tcp_get_flags(th) & TH_ACK) {
3522302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0,
3523302ce8d6SAndre Oppermann 		    th->th_ack, TH_RST);
3524302ce8d6SAndre Oppermann 	} else {
35251ebf4607SRichard Scheffenegger 		if (tcp_get_flags(th) & TH_SYN)
3526302ce8d6SAndre Oppermann 			tlen++;
35271ebf4607SRichard Scheffenegger 		if (tcp_get_flags(th) & TH_FIN)
35284ddd5aadSMichael Tuexen 			tlen++;
3529302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
3530302ce8d6SAndre Oppermann 		    (tcp_seq)0, TH_RST|TH_ACK);
3531302ce8d6SAndre Oppermann 	}
3532302ce8d6SAndre Oppermann 	return;
3533302ce8d6SAndre Oppermann drop:
3534302ce8d6SAndre Oppermann 	m_freem(m);
3535df8bae1dSRodney W. Grimes }
3536df8bae1dSRodney W. Grimes 
3537be2ac88cSJonathan Lemon /*
3538be2ac88cSJonathan Lemon  * Parse TCP options and place in tcpopt.
3539be2ac88cSJonathan Lemon  */
354055bceb1eSRandall Stewart void
tcp_dooptions(struct tcpopt * to,u_char * cp,int cnt,int flags)3541ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags)
3542df8bae1dSRodney W. Grimes {
3543df8bae1dSRodney W. Grimes 	int opt, optlen;
3544df8bae1dSRodney W. Grimes 
3545be2ac88cSJonathan Lemon 	to->to_flags = 0;
3546df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
3547df8bae1dSRodney W. Grimes 		opt = cp[0];
3548df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
3549df8bae1dSRodney W. Grimes 			break;
3550df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
3551df8bae1dSRodney W. Grimes 			optlen = 1;
3552df8bae1dSRodney W. Grimes 		else {
3553b474779fSJun-ichiro itojun Hagino 			if (cnt < 2)
3554b474779fSJun-ichiro itojun Hagino 				break;
3555df8bae1dSRodney W. Grimes 			optlen = cp[1];
3556b474779fSJun-ichiro itojun Hagino 			if (optlen < 2 || optlen > cnt)
3557df8bae1dSRodney W. Grimes 				break;
3558df8bae1dSRodney W. Grimes 		}
3559df8bae1dSRodney W. Grimes 		switch (opt) {
3560df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
3561df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
3562df8bae1dSRodney W. Grimes 				continue;
3563f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3564df8bae1dSRodney W. Grimes 				continue;
3565be2ac88cSJonathan Lemon 			to->to_flags |= TOF_MSS;
3566be2ac88cSJonathan Lemon 			bcopy((char *)cp + 2,
3567be2ac88cSJonathan Lemon 			    (char *)&to->to_mss, sizeof(to->to_mss));
3568fd8e4ebcSMike Barcroft 			to->to_mss = ntohs(to->to_mss);
3569df8bae1dSRodney W. Grimes 			break;
3570df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
3571df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
3572df8bae1dSRodney W. Grimes 				continue;
3573f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3574df8bae1dSRodney W. Grimes 				continue;
3575be2ac88cSJonathan Lemon 			to->to_flags |= TOF_SCALE;
357602a1a643SAndre Oppermann 			to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT);
3577df8bae1dSRodney W. Grimes 			break;
3578df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
3579df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
3580df8bae1dSRodney W. Grimes 				continue;
3581be2ac88cSJonathan Lemon 			to->to_flags |= TOF_TS;
3582a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
3583a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
3584fd8e4ebcSMike Barcroft 			to->to_tsval = ntohl(to->to_tsval);
3585a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
3586a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
3587fd8e4ebcSMike Barcroft 			to->to_tsecr = ntohl(to->to_tsecr);
3588df8bae1dSRodney W. Grimes 			break;
35891cfd4b53SBruce M Simpson 		case TCPOPT_SIGNATURE:
3590fcf59617SAndrey V. Elsukov 			/*
3591fcf59617SAndrey V. Elsukov 			 * In order to reply to a host which has set the
3592fcf59617SAndrey V. Elsukov 			 * TCP_SIGNATURE option in its initial SYN, we have
3593fcf59617SAndrey V. Elsukov 			 * to record the fact that the option was observed
3594fcf59617SAndrey V. Elsukov 			 * here for the syncache code to perform the correct
3595fcf59617SAndrey V. Elsukov 			 * response.
3596fcf59617SAndrey V. Elsukov 			 */
35971cfd4b53SBruce M Simpson 			if (optlen != TCPOLEN_SIGNATURE)
35981cfd4b53SBruce M Simpson 				continue;
3599df47e437SAndre Oppermann 			to->to_flags |= TOF_SIGNATURE;
3600df47e437SAndre Oppermann 			to->to_signature = cp + 2;
36011cfd4b53SBruce M Simpson 			break;
36026d90faf3SPaul Saab 		case TCPOPT_SACK_PERMITTED:
3603f72167f4SAndre Oppermann 			if (optlen != TCPOLEN_SACK_PERMITTED)
36046d90faf3SPaul Saab 				continue;
3605f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3606f72167f4SAndre Oppermann 				continue;
3607603724d3SBjoern A. Zeeb 			if (!V_tcp_do_sack)
3608f72167f4SAndre Oppermann 				continue;
3609fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACKPERM;
36106d90faf3SPaul Saab 			break;
36116d90faf3SPaul Saab 		case TCPOPT_SACK:
36125a53ca16SPaul Saab 			if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
36136d90faf3SPaul Saab 				continue;
3614b728e902SAndre Oppermann 			if (flags & TO_SYN)
3615b728e902SAndre Oppermann 				continue;
3616fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACK;
36175a53ca16SPaul Saab 			to->to_nsacks = (optlen - 2) / TCPOLEN_SACK;
36185a53ca16SPaul Saab 			to->to_sacks = cp + 2;
361978b50714SRobert Watson 			TCPSTAT_INC(tcps_sack_rcv_blocks);
36206d90faf3SPaul Saab 			break;
3621281a0fd4SPatrick Kelsey 		case TCPOPT_FAST_OPEN:
3622c560df6fSPatrick Kelsey 			/*
3623c560df6fSPatrick Kelsey 			 * Cookie length validation is performed by the
3624c560df6fSPatrick Kelsey 			 * server side cookie checking code or the client
3625c560df6fSPatrick Kelsey 			 * side cookie cache update code.
3626c560df6fSPatrick Kelsey 			 */
3627281a0fd4SPatrick Kelsey 			if (!(flags & TO_SYN))
3628281a0fd4SPatrick Kelsey 				continue;
3629c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
3630c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
3631281a0fd4SPatrick Kelsey 				continue;
3632281a0fd4SPatrick Kelsey 			to->to_flags |= TOF_FASTOPEN;
3633281a0fd4SPatrick Kelsey 			to->to_tfo_len = optlen - 2;
3634281a0fd4SPatrick Kelsey 			to->to_tfo_cookie = to->to_tfo_len ? cp + 2 : NULL;
3635281a0fd4SPatrick Kelsey 			break;
3636be2ac88cSJonathan Lemon 		default:
3637be2ac88cSJonathan Lemon 			continue;
3638df8bae1dSRodney W. Grimes 		}
3639df8bae1dSRodney W. Grimes 	}
3640df8bae1dSRodney W. Grimes }
3641df8bae1dSRodney W. Grimes 
3642df8bae1dSRodney W. Grimes /*
3643df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
3644df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
3645df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
3646df8bae1dSRodney W. Grimes  * sequencing purposes.
3647df8bae1dSRodney W. Grimes  */
364855bceb1eSRandall Stewart void
tcp_pulloutofband(struct socket * so,struct tcphdr * th,struct mbuf * m,int off)3649ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m,
3650ad3f9ab3SAndre Oppermann     int off)
3651df8bae1dSRodney W. Grimes {
3652fb59c426SYoshinobu Inoue 	int cnt = off + th->th_urp - 1;
3653df8bae1dSRodney W. Grimes 
3654df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
3655df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
3656df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
3657df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
3658df8bae1dSRodney W. Grimes 
36599eb0e832SGleb Smirnoff 			INP_WLOCK_ASSERT(tptoinpcb(tp));
36607a3244ccSRobert Watson 
3661df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
3662df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
3663df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
3664df8bae1dSRodney W. Grimes 			m->m_len--;
366569a34685SYoshinobu Inoue 			if (m->m_flags & M_PKTHDR)
366669a34685SYoshinobu Inoue 				m->m_pkthdr.len--;
3667df8bae1dSRodney W. Grimes 			return;
3668df8bae1dSRodney W. Grimes 		}
3669df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
3670df8bae1dSRodney W. Grimes 		m = m->m_next;
36714dfdffe9SAndre Oppermann 		if (m == NULL)
3672df8bae1dSRodney W. Grimes 			break;
3673df8bae1dSRodney W. Grimes 	}
3674df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
3675df8bae1dSRodney W. Grimes }
3676df8bae1dSRodney W. Grimes 
3677df8bae1dSRodney W. Grimes /*
3678df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
3679df8bae1dSRodney W. Grimes  * and update averages and current timeout.
3680df8bae1dSRodney W. Grimes  */
368155bceb1eSRandall Stewart void
tcp_xmit_timer(struct tcpcb * tp,int rtt)3682ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt)
3683df8bae1dSRodney W. Grimes {
3684ad3f9ab3SAndre Oppermann 	int delta;
3685233e8c18SGarrett Wollman 
36869eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
36872be3bf22SRobert Watson 
368878b50714SRobert Watson 	TCPSTAT_INC(tcps_rttupdated);
368918b83b62SRichard Scheffenegger 	if (tp->t_rttupdated < UCHAR_MAX)
3690233e8c18SGarrett Wollman 		tp->t_rttupdated++;
3691adc56f5aSEdward Tomasz Napierala #ifdef STATS
3692adc56f5aSEdward Tomasz Napierala 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT,
3693adc56f5aSEdward Tomasz Napierala 	    imax(0, rtt * 1000 / hz));
3694adc56f5aSEdward Tomasz Napierala #endif
36955ede40dcSRyan Stone 	if ((tp->t_srtt != 0) && (tp->t_rxtshift <= TCP_RTT_INVALIDATE)) {
3696233e8c18SGarrett Wollman 		/*
3697233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
3698233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
3699233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
3700233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
3701233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
3702233e8c18SGarrett Wollman 		 */
3703233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
3704233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
3705233e8c18SGarrett Wollman 
3706233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
3707233e8c18SGarrett Wollman 			tp->t_srtt = 1;
3708233e8c18SGarrett Wollman 
3709233e8c18SGarrett Wollman 		/*
3710233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
3711233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
3712233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
3713233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
3714233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
3715233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
3716233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
3717233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
3718233e8c18SGarrett Wollman 		 */
3719233e8c18SGarrett Wollman 		if (delta < 0)
3720233e8c18SGarrett Wollman 			delta = -delta;
3721233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
3722233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
3723233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
3724233e8c18SGarrett Wollman 	} else {
3725233e8c18SGarrett Wollman 		/*
3726233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
3727233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
3728233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
3729233e8c18SGarrett Wollman 		 */
3730233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
3731233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
3732233e8c18SGarrett Wollman 	}
37339b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
3734df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
3735df8bae1dSRodney W. Grimes 
3736df8bae1dSRodney W. Grimes 	/*
3737df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
3738df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
3739df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
3740df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
3741df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
3742df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
3743df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
3744df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
3745df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
3746df8bae1dSRodney W. Grimes 	 */
3747233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
37489e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
3749df8bae1dSRodney W. Grimes 
3750df8bae1dSRodney W. Grimes 	/*
3751df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
3752df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
3753df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
3754df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
3755df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
3756df8bae1dSRodney W. Grimes 	 */
3757df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
3758df8bae1dSRodney W. Grimes }
3759df8bae1dSRodney W. Grimes 
3760df8bae1dSRodney W. Grimes /*
3761df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
3762df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
37634faaea55SAndre Oppermann  * If none, use an mss that can be handled on the outgoing interface
37644faaea55SAndre Oppermann  * without forcing IP to fragment.  If no route is found, route has no mtu,
3765df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
3766df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
3767df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
3768df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
3769df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
3770df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
3771df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
3772a0292f23SGarrett Wollman  *
37730c39d38dSGleb Smirnoff  * NOTE that resulting t_maxseg doesn't include space for TCP options or
37740c39d38dSGleb Smirnoff  * IP options, e.g. IPSEC data, since length of this data may vary, and
37750c39d38dSGleb Smirnoff  * thus it is calculated for every segment separately in tcp_output().
3776a0292f23SGarrett Wollman  *
377797d8d152SAndre Oppermann  * NOTE that this routine is only called when we process an incoming
3778ef341ee1SGleb Smirnoff  * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS
3779ef341ee1SGleb Smirnoff  * settings are handled in tcp_mssopt().
3780df8bae1dSRodney W. Grimes  */
3781a0292f23SGarrett Wollman void
tcp_mss_update(struct tcpcb * tp,int offer,int mtuoffer,struct hc_metrics_lite * metricptr,struct tcp_ifcap * cap)3782ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer,
37833c914c54SAndre Oppermann     struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap)
3784df8bae1dSRodney W. Grimes {
3785b287c6c7SBjoern A. Zeeb 	int mss = 0;
37863ac12506SJonathan T. Looney 	uint32_t maxmtu = 0;
37879eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
378897d8d152SAndre Oppermann 	struct hc_metrics_lite metrics;
3789fb59c426SYoshinobu Inoue #ifdef INET6
3790c068736aSJeffrey Hsu 	int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
3791c068736aSJeffrey Hsu 	size_t min_protoh = isipv6 ?
3792c068736aSJeffrey Hsu 			    sizeof (struct ip6_hdr) + sizeof (struct tcphdr) :
3793c068736aSJeffrey Hsu 			    sizeof (struct tcpiphdr);
3794c068736aSJeffrey Hsu #else
37959e644c23SMichael Tuexen 	 size_t min_protoh = sizeof(struct tcpiphdr);
3796fb59c426SYoshinobu Inoue #endif
3797df8bae1dSRodney W. Grimes 
37989eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
37997a3244ccSRobert Watson 
38009e644c23SMichael Tuexen 	if (tp->t_port)
38019e644c23SMichael Tuexen 		min_protoh += V_tcp_udp_tunneling_overhead;
3802ef341ee1SGleb Smirnoff 	if (mtuoffer != -1) {
3803ef341ee1SGleb Smirnoff 		KASSERT(offer == -1, ("%s: conflict", __func__));
3804ef341ee1SGleb Smirnoff 		offer = mtuoffer - min_protoh;
3805ef341ee1SGleb Smirnoff 	}
3806ef341ee1SGleb Smirnoff 
3807e8949f74SAndre Oppermann 	/* Initialize. */
380897d8d152SAndre Oppermann #ifdef INET6
380997d8d152SAndre Oppermann 	if (isipv6) {
38103c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu6(&inp->inp_inc, cap);
38110c39d38dSGleb Smirnoff 		tp->t_maxseg = V_tcp_v6mssdflt;
3812b287c6c7SBjoern A. Zeeb 	}
381397d8d152SAndre Oppermann #endif
3814b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3815b287c6c7SBjoern A. Zeeb 	else
3816b287c6c7SBjoern A. Zeeb #endif
3817b287c6c7SBjoern A. Zeeb #ifdef INET
381897d8d152SAndre Oppermann 	{
38193c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu(&inp->inp_inc, cap);
38200c39d38dSGleb Smirnoff 		tp->t_maxseg = V_tcp_mssdflt;
3821df8bae1dSRodney W. Grimes 	}
3822b287c6c7SBjoern A. Zeeb #endif
3823df8bae1dSRodney W. Grimes 
382497d8d152SAndre Oppermann 	/*
3825e8949f74SAndre Oppermann 	 * No route to sender, stay with default mss and return.
382697d8d152SAndre Oppermann 	 */
38276f01cac6SBjoern A. Zeeb 	if (maxmtu == 0) {
38286f01cac6SBjoern A. Zeeb 		/*
38298e5c87f4SBjoern A. Zeeb 		 * In case we return early we need to initialize metrics
38306f01cac6SBjoern A. Zeeb 		 * to a defined state as tcp_hc_get() would do for us
38316f01cac6SBjoern A. Zeeb 		 * if there was no cache hit.
38326f01cac6SBjoern A. Zeeb 		 */
38336f01cac6SBjoern A. Zeeb 		if (metricptr != NULL)
38346f01cac6SBjoern A. Zeeb 			bzero(metricptr, sizeof(struct hc_metrics_lite));
383597d8d152SAndre Oppermann 		return;
38366f01cac6SBjoern A. Zeeb 	}
383797d8d152SAndre Oppermann 
3838e8949f74SAndre Oppermann 	/* What have we got? */
383997d8d152SAndre Oppermann 	switch (offer) {
384097d8d152SAndre Oppermann 		case 0:
384197d8d152SAndre Oppermann 			/*
384297d8d152SAndre Oppermann 			 * Offer == 0 means that there was no MSS on the SYN
3843c3b02504SBjoern A. Zeeb 			 * segment, in this case we use tcp_mssdflt as
38440c39d38dSGleb Smirnoff 			 * already assigned to t_maxseg above.
384597d8d152SAndre Oppermann 			 */
38460c39d38dSGleb Smirnoff 			offer = tp->t_maxseg;
384797d8d152SAndre Oppermann 			break;
384897d8d152SAndre Oppermann 
384997d8d152SAndre Oppermann 		case -1:
3850a0292f23SGarrett Wollman 			/*
3851c94c54e4SAndre Oppermann 			 * Offer == -1 means that we didn't receive SYN yet.
3852a0292f23SGarrett Wollman 			 */
385397d8d152SAndre Oppermann 			/* FALLTHROUGH */
385497d8d152SAndre Oppermann 
385597d8d152SAndre Oppermann 		default:
3856a0292f23SGarrett Wollman 			/*
385753369ac9SAndre Oppermann 			 * Prevent DoS attack with too small MSS. Round up
385853369ac9SAndre Oppermann 			 * to at least minmss.
385953369ac9SAndre Oppermann 			 */
3860603724d3SBjoern A. Zeeb 			offer = max(offer, V_tcp_minmss);
386197d8d152SAndre Oppermann 	}
3862a0292f23SGarrett Wollman 
386337898108SGleb Smirnoff 	if (metricptr == NULL)
386437898108SGleb Smirnoff 		metricptr = &metrics;
386537898108SGleb Smirnoff 	tcp_hc_get(&inp->inp_inc, metricptr);
386697d8d152SAndre Oppermann 
3867df8bae1dSRodney W. Grimes 	/*
3868cc412412SHiren Panchasara 	 * If there's a discovered mtu in tcp hostcache, use it.
3869cc412412SHiren Panchasara 	 * Else, use the link mtu.
3870df8bae1dSRodney W. Grimes 	 */
387137898108SGleb Smirnoff 	if (metricptr->hc_mtu)
387237898108SGleb Smirnoff 		mss = min(metricptr->hc_mtu, maxmtu) - min_protoh;
3873c068736aSJeffrey Hsu 	else {
387431b3783cSHajimu UMEMOTO #ifdef INET6
3875fb59c426SYoshinobu Inoue 		if (isipv6) {
387697d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3877603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
387897d8d152SAndre Oppermann 			    !in6_localaddr(&inp->in6p_faddr))
3879603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_v6mssdflt);
3880b287c6c7SBjoern A. Zeeb 		}
3881b3399803SHajimu UMEMOTO #endif
3882b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3883b287c6c7SBjoern A. Zeeb 		else
3884b287c6c7SBjoern A. Zeeb #endif
3885b287c6c7SBjoern A. Zeeb #ifdef INET
388697d8d152SAndre Oppermann 		{
388797d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3888603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
388997d8d152SAndre Oppermann 			    !in_localaddr(inp->inp_faddr))
3890603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_mssdflt);
3891a0292f23SGarrett Wollman 		}
3892b287c6c7SBjoern A. Zeeb #endif
38933cee92e0SBjoern A. Zeeb 		/*
38943cee92e0SBjoern A. Zeeb 		 * XXX - The above conditional (mss = maxmtu - min_protoh)
38953cee92e0SBjoern A. Zeeb 		 * probably violates the TCP spec.
38963cee92e0SBjoern A. Zeeb 		 * The problem is that, since we don't know the
38973cee92e0SBjoern A. Zeeb 		 * other end's MSS, we are supposed to use a conservative
38983cee92e0SBjoern A. Zeeb 		 * default.  But, if we do that, then MTU discovery will
38993cee92e0SBjoern A. Zeeb 		 * never actually take place, because the conservative
39003cee92e0SBjoern A. Zeeb 		 * default is much less than the MTUs typically seen
39013cee92e0SBjoern A. Zeeb 		 * on the Internet today.  For the moment, we'll sweep
39023cee92e0SBjoern A. Zeeb 		 * this under the carpet.
39033cee92e0SBjoern A. Zeeb 		 *
39043cee92e0SBjoern A. Zeeb 		 * The conservative default might not actually be a problem
39053cee92e0SBjoern A. Zeeb 		 * if the only case this occurs is when sending an initial
39063cee92e0SBjoern A. Zeeb 		 * SYN with options and data to a host we've never talked
39073cee92e0SBjoern A. Zeeb 		 * to before.  Then, they will reply with an MSS value which
39083cee92e0SBjoern A. Zeeb 		 * will get recorded and the new parameters should get
39093cee92e0SBjoern A. Zeeb 		 * recomputed.  For Further Study.
39103cee92e0SBjoern A. Zeeb 		 */
391197d8d152SAndre Oppermann 	}
3912a0292f23SGarrett Wollman 	mss = min(mss, offer);
391397d8d152SAndre Oppermann 
3914a0292f23SGarrett Wollman 	/*
39150c39d38dSGleb Smirnoff 	 * Sanity check: make sure that maxseg will be large
39163cee92e0SBjoern A. Zeeb 	 * enough to allow some data on segments even if the
39173cee92e0SBjoern A. Zeeb 	 * all the option space is used (40bytes).  Otherwise
39183cee92e0SBjoern A. Zeeb 	 * funny things may happen in tcp_output.
39190c39d38dSGleb Smirnoff 	 *
39200c39d38dSGleb Smirnoff 	 * XXXGL: shouldn't we reserve space for IP/IPv6 options?
39213cee92e0SBjoern A. Zeeb 	 */
39223cee92e0SBjoern A. Zeeb 	mss = max(mss, 64);
39233cee92e0SBjoern A. Zeeb 
392497d8d152SAndre Oppermann 	tp->t_maxseg = mss;
3925fce03f85SRandall Stewart 	if (tp->t_maxseg < V_tcp_mssdflt) {
3926fce03f85SRandall Stewart 		/*
3927fce03f85SRandall Stewart 		 * The MSS is so small we should not process incoming
3928fce03f85SRandall Stewart 		 * SACK's since we are subject to attack in such a
3929fce03f85SRandall Stewart 		 * case.
3930fce03f85SRandall Stewart 		 */
3931fce03f85SRandall Stewart 		tp->t_flags2 |= TF2_PROC_SACK_PROHIBIT;
3932fce03f85SRandall Stewart 	} else {
3933fce03f85SRandall Stewart 		tp->t_flags2 &= ~TF2_PROC_SACK_PROHIBIT;
3934fce03f85SRandall Stewart 	}
3935fce03f85SRandall Stewart 
39363cee92e0SBjoern A. Zeeb }
39373cee92e0SBjoern A. Zeeb 
39383cee92e0SBjoern A. Zeeb void
tcp_mss(struct tcpcb * tp,int offer)39393cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer)
39403cee92e0SBjoern A. Zeeb {
3941dbc42409SLawrence Stewart 	int mss;
39423ac12506SJonathan T. Looney 	uint32_t bufsize;
39439eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
39443cee92e0SBjoern A. Zeeb 	struct socket *so;
39453cee92e0SBjoern A. Zeeb 	struct hc_metrics_lite metrics;
39463c914c54SAndre Oppermann 	struct tcp_ifcap cap;
3947dbc42409SLawrence Stewart 
39483cee92e0SBjoern A. Zeeb 	KASSERT(tp != NULL, ("%s: tp == NULL", __func__));
39493cee92e0SBjoern A. Zeeb 
39503c914c54SAndre Oppermann 	bzero(&cap, sizeof(cap));
39513c914c54SAndre Oppermann 	tcp_mss_update(tp, offer, -1, &metrics, &cap);
39523cee92e0SBjoern A. Zeeb 
39533cee92e0SBjoern A. Zeeb 	mss = tp->t_maxseg;
395497d8d152SAndre Oppermann 
3955df8bae1dSRodney W. Grimes 	/*
395697d8d152SAndre Oppermann 	 * If there's a pipesize, change the socket buffer to that size,
395797d8d152SAndre Oppermann 	 * don't change if sb_hiwat is different than default (then it
395897d8d152SAndre Oppermann 	 * has been changed on purpose with setsockopt).
395997d8d152SAndre Oppermann 	 * Make the socket buffers an integral number of mss units;
396097d8d152SAndre Oppermann 	 * if the mss is larger than the socket buffer, decrease the mss.
3961df8bae1dSRodney W. Grimes 	 */
3962c3b02504SBjoern A. Zeeb 	so = inp->inp_socket;
3963dded4e9eSRichard Scheffenegger 	SOCK_SENDBUF_LOCK(so);
396409000cc1SGleb Smirnoff 	if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.hc_sendpipe)
396509000cc1SGleb Smirnoff 		bufsize = metrics.hc_sendpipe;
396697d8d152SAndre Oppermann 	else
3967df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
3968df8bae1dSRodney W. Grimes 	if (bufsize < mss)
3969df8bae1dSRodney W. Grimes 		mss = bufsize;
3970df8bae1dSRodney W. Grimes 	else {
3971df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3972df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3973df8bae1dSRodney W. Grimes 			bufsize = sb_max;
397488c39af3SRuslan Ermilov 		if (bufsize > so->so_snd.sb_hiwat)
397543283184SGleb Smirnoff 			(void)sbreserve_locked(so, SO_SND, bufsize, NULL);
3976df8bae1dSRodney W. Grimes 	}
3977dded4e9eSRichard Scheffenegger 	SOCK_SENDBUF_UNLOCK(so);
3978784ce8faSHiren Panchasara 	/*
3979784ce8faSHiren Panchasara 	 * Sanity check: make sure that maxseg will be large
3980784ce8faSHiren Panchasara 	 * enough to allow some data on segments even if the
3981784ce8faSHiren Panchasara 	 * all the option space is used (40bytes).  Otherwise
3982784ce8faSHiren Panchasara 	 * funny things may happen in tcp_output.
3983784ce8faSHiren Panchasara 	 *
3984784ce8faSHiren Panchasara 	 * XXXGL: shouldn't we reserve space for IP/IPv6 options?
3985784ce8faSHiren Panchasara 	 */
3986784ce8faSHiren Panchasara 	tp->t_maxseg = max(mss, 64);
3987fce03f85SRandall Stewart 	if (tp->t_maxseg < V_tcp_mssdflt) {
3988fce03f85SRandall Stewart 		/*
3989fce03f85SRandall Stewart 		 * The MSS is so small we should not process incoming
3990fce03f85SRandall Stewart 		 * SACK's since we are subject to attack in such a
3991fce03f85SRandall Stewart 		 * case.
3992fce03f85SRandall Stewart 		 */
3993fce03f85SRandall Stewart 		tp->t_flags2 |= TF2_PROC_SACK_PROHIBIT;
3994fce03f85SRandall Stewart 	} else {
3995fce03f85SRandall Stewart 		tp->t_flags2 &= ~TF2_PROC_SACK_PROHIBIT;
3996fce03f85SRandall Stewart 	}
3997df8bae1dSRodney W. Grimes 
3998dded4e9eSRichard Scheffenegger 	SOCK_RECVBUF_LOCK(so);
399909000cc1SGleb Smirnoff 	if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.hc_recvpipe)
400009000cc1SGleb Smirnoff 		bufsize = metrics.hc_recvpipe;
400197d8d152SAndre Oppermann 	else
4002df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
4003df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
4004df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
4005df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
4006df8bae1dSRodney W. Grimes 			bufsize = sb_max;
400788c39af3SRuslan Ermilov 		if (bufsize > so->so_rcv.sb_hiwat)
400843283184SGleb Smirnoff 			(void)sbreserve_locked(so, SO_RCV, bufsize, NULL);
4009df8bae1dSRodney W. Grimes 	}
4010dded4e9eSRichard Scheffenegger 	SOCK_RECVBUF_UNLOCK(so);
401191d6cfa6SBjoern A. Zeeb 
401291d6cfa6SBjoern A. Zeeb 	/* Check the interface for TSO capabilities. */
40133c914c54SAndre Oppermann 	if (cap.ifcap & CSUM_TSO) {
401491d6cfa6SBjoern A. Zeeb 		tp->t_flags |= TF_TSO;
40153c914c54SAndre Oppermann 		tp->t_tsomax = cap.tsomax;
40169fd573c3SHans Petter Selasky 		tp->t_tsomaxsegcount = cap.tsomaxsegcount;
40179fd573c3SHans Petter Selasky 		tp->t_tsomaxsegsize = cap.tsomaxsegsize;
4018b6919741SKonstantin Belousov 		if (cap.ipsec_tso)
4019b6919741SKonstantin Belousov 			tp->t_flags2 |= TF2_IPSEC_TSO;
40203c914c54SAndre Oppermann 	}
4021a0292f23SGarrett Wollman }
4022a0292f23SGarrett Wollman 
4023a0292f23SGarrett Wollman /*
4024a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
4025a0292f23SGarrett Wollman  */
4026a0292f23SGarrett Wollman int
tcp_mssopt(struct in_conninfo * inc)4027ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc)
4028a0292f23SGarrett Wollman {
402997d8d152SAndre Oppermann 	int mss = 0;
40303ac12506SJonathan T. Looney 	uint32_t thcmtu = 0;
40313ac12506SJonathan T. Looney 	uint32_t maxmtu = 0;
403297d8d152SAndre Oppermann 	size_t min_protoh;
4033a0292f23SGarrett Wollman 
403497d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer"));
4035a0292f23SGarrett Wollman 
403631b3783cSHajimu UMEMOTO #ifdef INET6
4037dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
4038603724d3SBjoern A. Zeeb 		mss = V_tcp_v6mssdflt;
4039233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu6(inc, NULL);
404097d8d152SAndre Oppermann 		min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
4041b287c6c7SBjoern A. Zeeb 	}
404231b3783cSHajimu UMEMOTO #endif
4043b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
4044b287c6c7SBjoern A. Zeeb 	else
4045b287c6c7SBjoern A. Zeeb #endif
4046b287c6c7SBjoern A. Zeeb #ifdef INET
404797d8d152SAndre Oppermann 	{
4048603724d3SBjoern A. Zeeb 		mss = V_tcp_mssdflt;
4049233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu(inc, NULL);
405097d8d152SAndre Oppermann 		min_protoh = sizeof(struct tcpiphdr);
405197d8d152SAndre Oppermann 	}
4052b287c6c7SBjoern A. Zeeb #endif
40533a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET)
40543a9391deSBjoern A. Zeeb 	thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
40553a9391deSBjoern A. Zeeb #endif
40563a9391deSBjoern A. Zeeb 
405797d8d152SAndre Oppermann 	if (maxmtu && thcmtu)
405897d8d152SAndre Oppermann 		mss = min(maxmtu, thcmtu) - min_protoh;
405997d8d152SAndre Oppermann 	else if (maxmtu || thcmtu)
406097d8d152SAndre Oppermann 		mss = max(maxmtu, thcmtu) - min_protoh;
406197d8d152SAndre Oppermann 
406297d8d152SAndre Oppermann 	return (mss);
4063df8bae1dSRodney W. Grimes }
406446f58482SJonathan Lemon 
40650e1d7c25SRichard Scheffenegger void
tcp_do_prr_ack(struct tcpcb * tp,struct tcphdr * th,struct tcpopt * to,sackstatus_t sack_changed,u_int * maxsegp)4066c7c325d0SRichard Scheffenegger tcp_do_prr_ack(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to,
4067c7c325d0SRichard Scheffenegger     sackstatus_t sack_changed, u_int *maxsegp)
40680e1d7c25SRichard Scheffenegger {
406948396dc7SRichard Scheffenegger 	int snd_cnt = 0, limit = 0, del_data = 0, pipe = 0;
4070c7c325d0SRichard Scheffenegger 	u_int maxseg;
40710e1d7c25SRichard Scheffenegger 
40729eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
40730e1d7c25SRichard Scheffenegger 
4074c7c325d0SRichard Scheffenegger 	if (*maxsegp == 0) {
4075c7c325d0SRichard Scheffenegger 		*maxsegp = tcp_maxseg(tp);
4076c7c325d0SRichard Scheffenegger 	}
4077c7c325d0SRichard Scheffenegger 	maxseg = *maxsegp;
40780e1d7c25SRichard Scheffenegger 	/*
40790e1d7c25SRichard Scheffenegger 	 * Compute the amount of data that this ACK is indicating
40800e1d7c25SRichard Scheffenegger 	 * (del_data) and an estimate of how many bytes are in the
40810e1d7c25SRichard Scheffenegger 	 * network.
40820e1d7c25SRichard Scheffenegger 	 */
4083c21b7b55SRichard Scheffenegger 	if (tcp_is_sack_recovery(tp, to) ||
408474d7fc87SRichard Scheffenegger 	    (IN_CONGRECOVERY(tp->t_flags) &&
408574d7fc87SRichard Scheffenegger 	     !IN_FASTRECOVERY(tp->t_flags))) {
408631d7a27cSRichard Scheffenegger 		del_data = tp->sackhint.delivered_data;
4087a8e431e1SRichard Scheffenegger 		pipe = tcp_compute_pipe(tp);
408874d7fc87SRichard Scheffenegger 	} else {
408974d7fc87SRichard Scheffenegger 		if (tp->sackhint.prr_delivered < (tcprexmtthresh * maxseg +
40902d05a1c8SRichard Scheffenegger 					     tp->snd_recover - tp->snd_una)) {
409174d7fc87SRichard Scheffenegger 			del_data = maxseg;
40922d05a1c8SRichard Scheffenegger 		}
409374d7fc87SRichard Scheffenegger 		pipe = imax(0, tp->snd_max - tp->snd_una -
409474d7fc87SRichard Scheffenegger 			    imin(INT_MAX / 65536, tp->t_dupacks) * maxseg);
409574d7fc87SRichard Scheffenegger 	}
40960e1d7c25SRichard Scheffenegger 	tp->sackhint.prr_delivered += del_data;
40970e1d7c25SRichard Scheffenegger 	/*
40980e1d7c25SRichard Scheffenegger 	 * Proportional Rate Reduction
40990e1d7c25SRichard Scheffenegger 	 */
4100e9071000SRichard Scheffenegger 	if (pipe >= tp->snd_ssthresh) {
41016a376af0SRichard Scheffenegger 		if (tp->sackhint.recover_fs == 0)
41026a376af0SRichard Scheffenegger 			tp->sackhint.recover_fs =
410348396dc7SRichard Scheffenegger 			    imax(1, tp->snd_nxt - tp->snd_una);
410448396dc7SRichard Scheffenegger 		snd_cnt = howmany((long)tp->sackhint.prr_delivered *
410548396dc7SRichard Scheffenegger 			    tp->snd_ssthresh, tp->sackhint.recover_fs) -
41069276ad23SRichard Scheffenegger 			    tp->sackhint.prr_out + maxseg - 1;
41076a376af0SRichard Scheffenegger 	} else {
410849a6fbe3SRichard Scheffenegger 		/*
410949a6fbe3SRichard Scheffenegger 		 * PRR 6937bis heuristic:
411049a6fbe3SRichard Scheffenegger 		 * - A partial ack without SACK block beneath snd_recover
411149a6fbe3SRichard Scheffenegger 		 * indicates further loss.
411249a6fbe3SRichard Scheffenegger 		 * - An SACK scoreboard update adding a new hole indicates
411349a6fbe3SRichard Scheffenegger 		 * further loss, so be conservative and send at most one
411449a6fbe3SRichard Scheffenegger 		 * segment.
411549a6fbe3SRichard Scheffenegger 		 * - Prevent ACK splitting attacks, by being conservative
411649a6fbe3SRichard Scheffenegger 		 * when no new data is acked.
411749a6fbe3SRichard Scheffenegger 		 */
41182d05a1c8SRichard Scheffenegger 		if ((sack_changed == SACK_NEWLOSS) || (del_data == 0)) {
411984761f3dSRichard Scheffenegger 			limit = tp->sackhint.prr_delivered -
4120e5313869SRichard Scheffenegger 				tp->sackhint.prr_out;
41212d05a1c8SRichard Scheffenegger 		} else {
412248396dc7SRichard Scheffenegger 			limit = imax(tp->sackhint.prr_delivered -
4123e5313869SRichard Scheffenegger 				    tp->sackhint.prr_out, del_data) +
4124e5313869SRichard Scheffenegger 				    maxseg;
41252d05a1c8SRichard Scheffenegger 		}
412648396dc7SRichard Scheffenegger 		snd_cnt = imin((tp->snd_ssthresh - pipe), limit);
41270e1d7c25SRichard Scheffenegger 	}
412848396dc7SRichard Scheffenegger 	snd_cnt = imax(snd_cnt, 0) / maxseg;
41290e1d7c25SRichard Scheffenegger 	/*
41300e1d7c25SRichard Scheffenegger 	 * Send snd_cnt new data into the network in response to this ack.
41310e1d7c25SRichard Scheffenegger 	 * If there is going to be a SACK retransmission, adjust snd_cwnd
41320e1d7c25SRichard Scheffenegger 	 * accordingly.
41330e1d7c25SRichard Scheffenegger 	 */
4134b9f803b7SRichard Scheffenegger 	if (IN_FASTRECOVERY(tp->t_flags)) {
4135c21b7b55SRichard Scheffenegger 		if (tcp_is_sack_recovery(tp, to)) {
41367dc78150SRichard Scheffenegger 			tp->snd_cwnd = pipe - del_data + (snd_cnt * maxseg);
413774d7fc87SRichard Scheffenegger 		} else {
413874d7fc87SRichard Scheffenegger 			tp->snd_cwnd = (tp->snd_max - tp->snd_una) +
413974d7fc87SRichard Scheffenegger 					    (snd_cnt * maxseg);
414074d7fc87SRichard Scheffenegger 		}
41412d05a1c8SRichard Scheffenegger 	} else if (IN_CONGRECOVERY(tp->t_flags)) {
414274d7fc87SRichard Scheffenegger 		tp->snd_cwnd = pipe - del_data + (snd_cnt * maxseg);
41432d05a1c8SRichard Scheffenegger 	}
414474d7fc87SRichard Scheffenegger 	tp->snd_cwnd = imax(maxseg, tp->snd_cwnd);
41450e1d7c25SRichard Scheffenegger }
41460e1d7c25SRichard Scheffenegger 
414746f58482SJonathan Lemon /*
4148c068736aSJeffrey Hsu  * On a partial ack arrives, force the retransmission of the
4149c068736aSJeffrey Hsu  * next unacknowledged segment.  Do not clear tp->t_dupacks.
4150c068736aSJeffrey Hsu  * By setting snd_nxt to ti_ack, this forces retransmission timer to
4151c068736aSJeffrey Hsu  * be started again.
415246f58482SJonathan Lemon  */
415355bceb1eSRandall Stewart void
tcp_newreno_partial_ack(struct tcpcb * tp,struct tcphdr * th)4154ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th)
415546f58482SJonathan Lemon {
415646f58482SJonathan Lemon 	tcp_seq onxt = tp->snd_nxt;
41573ac12506SJonathan T. Looney 	uint32_t ocwnd = tp->snd_cwnd;
41580c39d38dSGleb Smirnoff 	u_int maxseg = tcp_maxseg(tp);
415946f58482SJonathan Lemon 
41609eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
41617a3244ccSRobert Watson 
4162b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_REXMT, 0);
416346f58482SJonathan Lemon 	tp->t_rtttime = 0;
41647dc78150SRichard Scheffenegger 	if (IN_FASTRECOVERY(tp->t_flags)) {
416546f58482SJonathan Lemon 		tp->snd_nxt = th->th_ack;
41666b2a5f92SJayanth Vijayaraghavan 		/*
4167c068736aSJeffrey Hsu 		 * Set snd_cwnd to one segment beyond acknowledged offset.
4168c068736aSJeffrey Hsu 		 * (tp->snd_una has not yet been updated when this function is called.)
41696b2a5f92SJayanth Vijayaraghavan 		 */
41700c39d38dSGleb Smirnoff 		tp->snd_cwnd = maxseg + BYTES_THIS_ACK(tp, th);
4171a84db8f4SMatthew Dillon 		tp->t_flags |= TF_ACKNOW;
417240fa3e40SGleb Smirnoff 		(void) tcp_output(tp);
417346f58482SJonathan Lemon 		tp->snd_cwnd = ocwnd;
417446f58482SJonathan Lemon 		if (SEQ_GT(onxt, tp->snd_nxt))
417546f58482SJonathan Lemon 			tp->snd_nxt = onxt;
41767dc78150SRichard Scheffenegger 	}
417746f58482SJonathan Lemon 	/*
417846f58482SJonathan Lemon 	 * Partial window deflation.  Relies on fact that tp->snd_una
417946f58482SJonathan Lemon 	 * not updated yet.
418046f58482SJonathan Lemon 	 */
4181dbc42409SLawrence Stewart 	if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th))
4182dbc42409SLawrence Stewart 		tp->snd_cwnd -= BYTES_THIS_ACK(tp, th);
4183d7587117SPaul Saab 	else
4184d7587117SPaul Saab 		tp->snd_cwnd = 0;
41850c39d38dSGleb Smirnoff 	tp->snd_cwnd += maxseg;
418646f58482SJonathan Lemon }
418712eeb81fSHiren Panchasara 
418812eeb81fSHiren Panchasara int
tcp_compute_pipe(struct tcpcb * tp)418912eeb81fSHiren Panchasara tcp_compute_pipe(struct tcpcb *tp)
419012eeb81fSHiren Panchasara {
419167787d20SCheng Cui 	int pipe;
419267787d20SCheng Cui 
419367787d20SCheng Cui 	if (tp->t_fb->tfb_compute_pipe != NULL) {
419467787d20SCheng Cui 		pipe = (*tp->t_fb->tfb_compute_pipe)(tp);
419567787d20SCheng Cui 	} else if (V_tcp_do_newsack) {
419667787d20SCheng Cui 		pipe = tp->snd_max - tp->snd_una +
419712eeb81fSHiren Panchasara 			tp->sackhint.sack_bytes_rexmit -
4198e2c6a6d2SRichard Scheffenegger 			tp->sackhint.sacked_bytes -
419967787d20SCheng Cui 			tp->sackhint.lost_bytes;
4200e5049a17SRandall Stewart 	} else {
420167787d20SCheng Cui 		pipe = tp->snd_nxt - tp->snd_fack + tp->sackhint.sack_bytes_rexmit;
4202e5049a17SRandall Stewart 	}
420367787d20SCheng Cui 	return (imax(pipe, 0));
420412eeb81fSHiren Panchasara }
42057dc90a1dSMichael Tuexen 
42067dc90a1dSMichael Tuexen uint32_t
tcp_compute_initwnd(uint32_t maxseg)42077dc90a1dSMichael Tuexen tcp_compute_initwnd(uint32_t maxseg)
42087dc90a1dSMichael Tuexen {
42097dc90a1dSMichael Tuexen 	/*
42107dc90a1dSMichael Tuexen 	 * Calculate the Initial Window, also used as Restart Window
42117dc90a1dSMichael Tuexen 	 *
42127dc90a1dSMichael Tuexen 	 * RFC5681 Section 3.1 specifies the default conservative values.
42137dc90a1dSMichael Tuexen 	 * RFC3390 specifies slightly more aggressive values.
42147dc90a1dSMichael Tuexen 	 * RFC6928 increases it to ten segments.
42157dc90a1dSMichael Tuexen 	 * Support for user specified value for initial flight size.
42167dc90a1dSMichael Tuexen 	 */
42177dc90a1dSMichael Tuexen 	if (V_tcp_initcwnd_segments)
42187dc90a1dSMichael Tuexen 		return min(V_tcp_initcwnd_segments * maxseg,
42197dc90a1dSMichael Tuexen 		    max(2 * maxseg, V_tcp_initcwnd_segments * 1460));
42207dc90a1dSMichael Tuexen 	else if (V_tcp_do_rfc3390)
42217dc90a1dSMichael Tuexen 		return min(4 * maxseg, max(2 * maxseg, 4380));
42227dc90a1dSMichael Tuexen 	else {
42237dc90a1dSMichael Tuexen 		/* Per RFC5681 Section 3.1 */
42247dc90a1dSMichael Tuexen 		if (maxseg > 2190)
42257dc90a1dSMichael Tuexen 			return (2 * maxseg);
42267dc90a1dSMichael Tuexen 		else if (maxseg > 1095)
42277dc90a1dSMichael Tuexen 			return (3 * maxseg);
42287dc90a1dSMichael Tuexen 		else
42297dc90a1dSMichael Tuexen 			return (4 * maxseg);
42307dc90a1dSMichael Tuexen 	}
42317dc90a1dSMichael Tuexen }
4232