xref: /freebsd/sys/netinet/tcp_input.c (revision 35bc0bcc5192c68412de9fbf76201d602dd219ca)
1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4e79adb8eSGarrett Wollman  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6dbc42409SLawrence Stewart  * Copyright (c) 2007-2008,2010
7dbc42409SLawrence Stewart  *	Swinburne University of Technology, Melbourne, Australia.
8dbc42409SLawrence Stewart  * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org>
9dbc42409SLawrence Stewart  * Copyright (c) 2010 The FreeBSD Foundation
10fa046d87SRobert Watson  * Copyright (c) 2010-2011 Juniper Networks, Inc.
11dbc42409SLawrence Stewart  * All rights reserved.
12dbc42409SLawrence Stewart  *
13dbc42409SLawrence Stewart  * Portions of this software were developed at the Centre for Advanced Internet
14891b8ed4SLawrence Stewart  * Architectures, Swinburne University of Technology, by Lawrence Stewart,
15891b8ed4SLawrence Stewart  * James Healy and David Hayes, made possible in part by a grant from the Cisco
16891b8ed4SLawrence Stewart  * University Research Program Fund at Community Foundation Silicon Valley.
17dbc42409SLawrence Stewart  *
18dbc42409SLawrence Stewart  * Portions of this software were developed at the Centre for Advanced
19dbc42409SLawrence Stewart  * Internet Architectures, Swinburne University of Technology, Melbourne,
20dbc42409SLawrence Stewart  * Australia by David Hayes under sponsorship from the FreeBSD Foundation.
21df8bae1dSRodney W. Grimes  *
22fa046d87SRobert Watson  * Portions of this software were developed by Robert N. M. Watson under
23fa046d87SRobert Watson  * contract to Juniper Networks, Inc.
24fa046d87SRobert Watson  *
25df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
26df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
27df8bae1dSRodney W. Grimes  * are met:
28df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
29df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
30df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
31df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
32df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
33fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
34df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
35df8bae1dSRodney W. Grimes  *    without specific prior written permission.
36df8bae1dSRodney W. Grimes  *
37df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
38df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
41df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
48df8bae1dSRodney W. Grimes  *
49e79adb8eSGarrett Wollman  *	@(#)tcp_input.c	8.12 (Berkeley) 5/24/95
50df8bae1dSRodney W. Grimes  */
51df8bae1dSRodney W. Grimes 
524b421e2dSMike Silbersack #include <sys/cdefs.h>
534b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
544b421e2dSMike Silbersack 
551cfd4b53SBruce M Simpson #include "opt_inet.h"
56fb59c426SYoshinobu Inoue #include "opt_inet6.h"
573a2a9f79SYoshinobu Inoue #include "opt_ipsec.h"
580cc12cc5SJoerg Wunsch 
59df8bae1dSRodney W. Grimes #include <sys/param.h>
60adc56f5aSEdward Tomasz Napierala #include <sys/arb.h>
6198163b98SPoul-Henning Kamp #include <sys/kernel.h>
62bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
6339bc9de5SLawrence Stewart #include <sys/hhook.h>
64bd79708dSJonathan T. Looney #endif
65df8bae1dSRodney W. Grimes #include <sys/malloc.h>
66df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
67a29f300eSGarrett Wollman #include <sys/proc.h>		/* for proc0 declaration */
68df8bae1dSRodney W. Grimes #include <sys/protosw.h>
69adc56f5aSEdward Tomasz Napierala #include <sys/qmath.h>
7057f60867SMark Johnston #include <sys/sdt.h>
71960ed29cSSeigo Tanimura #include <sys/signalvar.h>
72df8bae1dSRodney W. Grimes #include <sys/socket.h>
73df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
74960ed29cSSeigo Tanimura #include <sys/sysctl.h>
75816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
76960ed29cSSeigo Tanimura #include <sys/systm.h>
77adc56f5aSEdward Tomasz Napierala #include <sys/stats.h>
78df8bae1dSRodney W. Grimes 
79e79adb8eSGarrett Wollman #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
80e79adb8eSGarrett Wollman 
8112e2e970SAndre Oppermann #include <vm/uma.h>
8212e2e970SAndre Oppermann 
83df8bae1dSRodney W. Grimes #include <net/if.h>
8476039bc8SGleb Smirnoff #include <net/if_var.h>
85df8bae1dSRodney W. Grimes #include <net/route.h>
86530c0060SRobert Watson #include <net/vnet.h>
87df8bae1dSRodney W. Grimes 
88773673c1SAndre Oppermann #define TCPSTATES		/* for logging */
89773673c1SAndre Oppermann 
90df8bae1dSRodney W. Grimes #include <netinet/in.h>
9157f60867SMark Johnston #include <netinet/in_kdtrace.h>
92960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
93df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
94df8bae1dSRodney W. Grimes #include <netinet/ip.h>
958e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h>	/* required for icmp_var.h */
96686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM */
97df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
98ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
99686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
100686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
101686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
102b66d74c1SGleb Smirnoff #include <netinet6/in6_var.h>
103960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h>
104960ed29cSSeigo Tanimura #include <netinet6/nd6.h>
1052de3e790SGleb Smirnoff #include <netinet/tcp.h>
106df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
107df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
108df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
109df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
11069c7c811SRandall Stewart #include <netinet/tcp_log_buf.h>
111fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h>
112df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
1134644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
114c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h>
11586a996e6SHiren Panchasara #ifdef TCPPCAP
11686a996e6SHiren Panchasara #include <netinet/tcp_pcap.h>
11786a996e6SHiren Panchasara #endif
118c325962bSMike Silbersack #include <netinet/tcp_syncache.h>
11909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
12009fe6320SNavdeep Parhar #include <netinet/tcp_offload.h>
12109fe6320SNavdeep Parhar #endif
122f7220c48SRichard Scheffenegger #include <netinet/tcp_ecn.h>
1239e644c23SMichael Tuexen #include <netinet/udp.h>
124fb59c426SYoshinobu Inoue 
125fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
126b9234fafSSam Leffler 
127db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
128db4f9cc7SJonathan Lemon 
129aed55708SRobert Watson #include <security/mac/mac_framework.h>
130aed55708SRobert Watson 
131dbc42409SLawrence Stewart const int tcprexmtthresh = 3;
1320312fbe9SPoul-Henning Kamp 
133334fc582SBjoern A. Zeeb VNET_DEFINE(int, tcp_log_in_vain) = 0;
134334fc582SBjoern A. Zeeb SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_VNET | CTLFLAG_RW,
135334fc582SBjoern A. Zeeb     &VNET_NAME(tcp_log_in_vain), 0,
136eddfbb76SRobert Watson     "Log all incoming TCP segments to closed ports");
137816a3d83SPoul-Henning Kamp 
13882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, blackhole) = 0;
13982cea7e6SBjoern A. Zeeb #define	V_blackhole		VNET(blackhole)
1406df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW,
141eddfbb76SRobert Watson     &VNET_NAME(blackhole), 0,
142eddfbb76SRobert Watson     "Do not send RST on segments to closed ports");
14316f7f31fSGeoff Rehmet 
1443ea9a7cfSGleb Smirnoff VNET_DEFINE(bool, blackhole_local) = false;
1453ea9a7cfSGleb Smirnoff #define	V_blackhole_local	VNET(blackhole_local)
1463ea9a7cfSGleb Smirnoff SYSCTL_BOOL(_net_inet_tcp, OID_AUTO, blackhole_local, CTLFLAG_VNET |
1473ea9a7cfSGleb Smirnoff     CTLFLAG_RW, &VNET_NAME(blackhole_local), false,
1483ea9a7cfSGleb Smirnoff     "Enforce net.inet.tcp.blackhole for locally originated packets");
1493ea9a7cfSGleb Smirnoff 
15082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_delack_enabled) = 1;
1516df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_VNET | CTLFLAG_RW,
152eddfbb76SRobert Watson     &VNET_NAME(tcp_delack_enabled), 0,
1533d177f46SBill Fumerola     "Delay ACK to try and piggyback it onto a data packet");
154f498eeeeSDavid Greenman 
15582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, drop_synfin) = 0;
1566df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_VNET | CTLFLAG_RW,
157eddfbb76SRobert Watson     &VNET_NAME(drop_synfin), 0,
158eddfbb76SRobert Watson     "Drop TCP packets with SYN+FIN set");
159f8613305SDag-Erling Smørgrav 
1600e1d7c25SRichard Scheffenegger VNET_DEFINE(int, tcp_do_prr_conservative) = 0;
1610e1d7c25SRichard Scheffenegger SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_prr_conservative, CTLFLAG_VNET | CTLFLAG_RW,
1620e1d7c25SRichard Scheffenegger     &VNET_NAME(tcp_do_prr_conservative), 0,
1630e1d7c25SRichard Scheffenegger     "Do conservative Proportional Rate Reduction");
1640e1d7c25SRichard Scheffenegger 
1650e1d7c25SRichard Scheffenegger VNET_DEFINE(int, tcp_do_prr) = 1;
1660e1d7c25SRichard Scheffenegger SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_prr, CTLFLAG_VNET | CTLFLAG_RW,
1670e1d7c25SRichard Scheffenegger     &VNET_NAME(tcp_do_prr), 1,
1680e1d7c25SRichard Scheffenegger     "Enable Proportional Rate Reduction per RFC 6937");
1690e1d7c25SRichard Scheffenegger 
1700471a8c7SRichard Scheffenegger VNET_DEFINE(int, tcp_do_lrd) = 0;
1710471a8c7SRichard Scheffenegger SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_lrd, CTLFLAG_VNET | CTLFLAG_RW,
1720471a8c7SRichard Scheffenegger     &VNET_NAME(tcp_do_lrd), 1,
1730471a8c7SRichard Scheffenegger     "Perform Lost Retransmission Detection");
1740471a8c7SRichard Scheffenegger 
175b72e56e7SMichael Tuexen VNET_DEFINE(int, tcp_do_newcwv) = 0;
176b72e56e7SMichael Tuexen SYSCTL_INT(_net_inet_tcp, OID_AUTO, newcwv, CTLFLAG_VNET | CTLFLAG_RW,
177b72e56e7SMichael Tuexen     &VNET_NAME(tcp_do_newcwv), 0,
178b72e56e7SMichael Tuexen     "Enable New Congestion Window Validation per RFC7661");
179b72e56e7SMichael Tuexen 
18082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3042) = 1;
1816df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_VNET | CTLFLAG_RW,
182eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3042), 0,
183eddfbb76SRobert Watson     "Enable RFC 3042 (Limited Transmit)");
184582a954bSJeffrey Hsu 
18582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3390) = 1;
1866df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_VNET | CTLFLAG_RW,
187eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3390), 0,
188da3a8a1aSJeffrey Hsu     "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)");
189da3a8a1aSJeffrey Hsu 
190356c7958SHiren Panchasara VNET_DEFINE(int, tcp_initcwnd_segments) = 10;
191356c7958SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, initcwnd_segments,
192356c7958SHiren Panchasara     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_initcwnd_segments), 0,
193356c7958SHiren Panchasara     "Slow-start flight size (initial congestion window) in number of segments");
19409440655SAndre Oppermann 
19582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3465) = 1;
1966df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_VNET | CTLFLAG_RW,
197eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3465), 0,
19824cb0f22SLawrence Stewart     "Enable RFC 3465 (Appropriate Byte Counting)");
199eddfbb76SRobert Watson 
20082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_abc_l_var) = 2;
2016df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_VNET | CTLFLAG_RW,
202eddfbb76SRobert Watson     &VNET_NAME(tcp_abc_l_var), 2,
20324cb0f22SLawrence Stewart     "Cap the max cwnd increment during slow-start to this number of segments");
20424cb0f22SLawrence Stewart 
2053220a212SGleb Smirnoff VNET_DEFINE(int, tcp_insecure_syn) = 0;
2066df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_syn, CTLFLAG_VNET | CTLFLAG_RW,
2073220a212SGleb Smirnoff     &VNET_NAME(tcp_insecure_syn), 0,
2083220a212SGleb Smirnoff     "Follow RFC793 instead of RFC5961 criteria for accepting SYN packets");
2093220a212SGleb Smirnoff 
21082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_insecure_rst) = 0;
2116df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_VNET | CTLFLAG_RW,
212eddfbb76SRobert Watson     &VNET_NAME(tcp_insecure_rst), 0,
2133220a212SGleb Smirnoff     "Follow RFC793 instead of RFC5961 criteria for accepting RST packets");
214a69968eeSMike Silbersack 
215fba0cea1SBjoern A. Zeeb VNET_DEFINE(int, tcp_recvspace) = 1024*64;
216873789cbSAndre Oppermann #define	V_tcp_recvspace	VNET(tcp_recvspace)
2176df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_VNET | CTLFLAG_RW,
218873789cbSAndre Oppermann     &VNET_NAME(tcp_recvspace), 0, "Initial receive socket buffer size");
219873789cbSAndre Oppermann 
22082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autorcvbuf) = 1;
2216df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_VNET | CTLFLAG_RW,
222eddfbb76SRobert Watson     &VNET_NAME(tcp_do_autorcvbuf), 0,
2238b615593SMarko Zec     "Enable automatic receive buffer sizing");
2246741ecf5SAndre Oppermann 
225b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024;
2266df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_VNET | CTLFLAG_RW,
227eddfbb76SRobert Watson     &VNET_NAME(tcp_autorcvbuf_max), 0,
2288b615593SMarko Zec     "Max size of automatic receive buffer");
2296741ecf5SAndre Oppermann 
23082cea7e6SBjoern A. Zeeb VNET_DEFINE(struct inpcbinfo, tcbinfo);
231df8bae1dSRodney W. Grimes 
232315e3e38SRobert Watson /*
233bf840a17SGleb Smirnoff  * TCP statistics are stored in an array of counter(9)s, which size matches
234bf840a17SGleb Smirnoff  * size of struct tcpstat.  TCP running connection count is a regular array.
2355923c293SGleb Smirnoff  */
2365da0521fSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct tcpstat, tcpstat);
2375da0521fSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_tcp, TCPCTL_STATS, stats, struct tcpstat,
2385da0521fSAndrey V. Elsukov     tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
239bf840a17SGleb Smirnoff VNET_DEFINE(counter_u64_t, tcps_states[TCP_NSTATES]);
240bf840a17SGleb Smirnoff SYSCTL_COUNTER_U64_ARRAY(_net_inet_tcp, TCPCTL_STATES, states, CTLFLAG_RD |
241d4d32b9fSHans Petter Selasky     CTLFLAG_VNET, &VNET_NAME(tcps_states)[0], TCP_NSTATES,
242bf840a17SGleb Smirnoff     "TCP connection counts by TCP state");
243bf840a17SGleb Smirnoff 
2445923c293SGleb Smirnoff /*
2457ca6e296SMichael Tuexen  * Kernel module interface for updating tcpstat.  The first argument is an index
2465923c293SGleb Smirnoff  * into tcpstat treated as an array.
247315e3e38SRobert Watson  */
248315e3e38SRobert Watson void
2497ca6e296SMichael Tuexen kmod_tcpstat_add(int statnum, int val)
250315e3e38SRobert Watson {
251315e3e38SRobert Watson 
2527ca6e296SMichael Tuexen 	counter_u64_add(VNET(tcpstat)[statnum], val);
253315e3e38SRobert Watson }
254315e3e38SRobert Watson 
255c21b7b55SRichard Scheffenegger /*
256c21b7b55SRichard Scheffenegger  * Make sure that we only start a SACK loss recovery when
257c21b7b55SRichard Scheffenegger  * receiving a duplicate ACK with a SACK block, and also
258c21b7b55SRichard Scheffenegger  * complete SACK loss recovery in case the other end
259c21b7b55SRichard Scheffenegger  * reneges.
260c21b7b55SRichard Scheffenegger  */
261c21b7b55SRichard Scheffenegger static bool inline
262c21b7b55SRichard Scheffenegger tcp_is_sack_recovery(struct tcpcb *tp, struct tcpopt *to)
263c21b7b55SRichard Scheffenegger {
264c21b7b55SRichard Scheffenegger 	return ((tp->t_flags & TF_SACK_PERMIT) &&
265c21b7b55SRichard Scheffenegger 		((to->to_flags & TOF_SACK) ||
266c21b7b55SRichard Scheffenegger 		(!TAILQ_EMPTY(&tp->snd_holes))));
267c21b7b55SRichard Scheffenegger }
268c21b7b55SRichard Scheffenegger 
269bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
270dbc42409SLawrence Stewart /*
27139bc9de5SLawrence Stewart  * Wrapper for the TCP established input helper hook.
27239bc9de5SLawrence Stewart  */
27355bceb1eSRandall Stewart void
27439bc9de5SLawrence Stewart hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to)
27539bc9de5SLawrence Stewart {
27639bc9de5SLawrence Stewart 	struct tcp_hhook_data hhook_data;
27739bc9de5SLawrence Stewart 
27839bc9de5SLawrence Stewart 	if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) {
27939bc9de5SLawrence Stewart 		hhook_data.tp = tp;
28039bc9de5SLawrence Stewart 		hhook_data.th = th;
28139bc9de5SLawrence Stewart 		hhook_data.to = to;
28239bc9de5SLawrence Stewart 
28339bc9de5SLawrence Stewart 		hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data,
284e68b3792SGleb Smirnoff 		    &tp->t_osd);
28539bc9de5SLawrence Stewart 	}
28639bc9de5SLawrence Stewart }
287bd79708dSJonathan T. Looney #endif
28839bc9de5SLawrence Stewart 
28939bc9de5SLawrence Stewart /*
290dbc42409SLawrence Stewart  * CC wrapper hook functions
291dbc42409SLawrence Stewart  */
29255bceb1eSRandall Stewart void
2934b7b743cSLawrence Stewart cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t nsegs,
2944b7b743cSLawrence Stewart     uint16_t type)
295f2512ba1SRui Paulo {
296adc56f5aSEdward Tomasz Napierala #ifdef STATS
297adc56f5aSEdward Tomasz Napierala 	int32_t gput;
298adc56f5aSEdward Tomasz Napierala #endif
299adc56f5aSEdward Tomasz Napierala 
3009eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
301f2512ba1SRui Paulo 
302e68b3792SGleb Smirnoff 	tp->t_ccv.nsegs = nsegs;
303e68b3792SGleb Smirnoff 	tp->t_ccv.bytes_this_ack = BYTES_THIS_ACK(tp, th);
304b72e56e7SMichael Tuexen 	if ((!V_tcp_do_newcwv && (tp->snd_cwnd <= tp->snd_wnd)) ||
305b72e56e7SMichael Tuexen 	    (V_tcp_do_newcwv && (tp->snd_cwnd <= tp->snd_wnd) &&
306b72e56e7SMichael Tuexen 	     (tp->snd_cwnd < (tcp_compute_pipe(tp) * 2))))
307e68b3792SGleb Smirnoff 		tp->t_ccv.flags |= CCF_CWND_LIMITED;
308dbc42409SLawrence Stewart 	else
309e68b3792SGleb Smirnoff 		tp->t_ccv.flags &= ~CCF_CWND_LIMITED;
310dbc42409SLawrence Stewart 
311dbc42409SLawrence Stewart 	if (type == CC_ACK) {
312adc56f5aSEdward Tomasz Napierala #ifdef STATS
313adc56f5aSEdward Tomasz Napierala 		stats_voi_update_abs_s32(tp->t_stats, VOI_TCP_CALCFRWINDIFF,
314adc56f5aSEdward Tomasz Napierala 		    ((int32_t)tp->snd_cwnd) - tp->snd_wnd);
315adc56f5aSEdward Tomasz Napierala 		if (!IN_RECOVERY(tp->t_flags))
316adc56f5aSEdward Tomasz Napierala 			stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_ACKLEN,
317e68b3792SGleb Smirnoff 			   tp->t_ccv.bytes_this_ack / (tcp_maxseg(tp) * nsegs));
318adc56f5aSEdward Tomasz Napierala 		if ((tp->t_flags & TF_GPUTINPROG) &&
319adc56f5aSEdward Tomasz Napierala 		    SEQ_GEQ(th->th_ack, tp->gput_ack)) {
320adc56f5aSEdward Tomasz Napierala 			/*
321adc56f5aSEdward Tomasz Napierala 			 * Compute goodput in bits per millisecond.
322adc56f5aSEdward Tomasz Napierala 			 */
323f5766992SHans Petter Selasky 			gput = (((int64_t)SEQ_SUB(th->th_ack, tp->gput_seq)) << 3) /
324adc56f5aSEdward Tomasz Napierala 			    max(1, tcp_ts_getticks() - tp->gput_ts);
325adc56f5aSEdward Tomasz Napierala 			stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT,
326adc56f5aSEdward Tomasz Napierala 			    gput);
327adc56f5aSEdward Tomasz Napierala 			/*
328adc56f5aSEdward Tomasz Napierala 			 * XXXLAS: This is a temporary hack, and should be
329adc56f5aSEdward Tomasz Napierala 			 * chained off VOI_TCP_GPUT when stats(9) grows an API
330adc56f5aSEdward Tomasz Napierala 			 * to deal with chained VOIs.
331adc56f5aSEdward Tomasz Napierala 			 */
332adc56f5aSEdward Tomasz Napierala 			if (tp->t_stats_gput_prev > 0)
333adc56f5aSEdward Tomasz Napierala 				stats_voi_update_abs_s32(tp->t_stats,
334adc56f5aSEdward Tomasz Napierala 				    VOI_TCP_GPUT_ND,
335adc56f5aSEdward Tomasz Napierala 				    ((gput - tp->t_stats_gput_prev) * 100) /
336adc56f5aSEdward Tomasz Napierala 				    tp->t_stats_gput_prev);
337adc56f5aSEdward Tomasz Napierala 			tp->t_flags &= ~TF_GPUTINPROG;
338adc56f5aSEdward Tomasz Napierala 			tp->t_stats_gput_prev = gput;
339adc56f5aSEdward Tomasz Napierala 		}
340adc56f5aSEdward Tomasz Napierala #endif /* STATS */
341dbc42409SLawrence Stewart 		if (tp->snd_cwnd > tp->snd_ssthresh) {
342e68b3792SGleb Smirnoff 			tp->t_bytes_acked += tp->t_ccv.bytes_this_ack;
343dbc42409SLawrence Stewart 			if (tp->t_bytes_acked >= tp->snd_cwnd) {
344dbc42409SLawrence Stewart 				tp->t_bytes_acked -= tp->snd_cwnd;
345e68b3792SGleb Smirnoff 				tp->t_ccv.flags |= CCF_ABC_SENTAWND;
346dbc42409SLawrence Stewart 			}
347dbc42409SLawrence Stewart 		} else {
348e68b3792SGleb Smirnoff 				tp->t_ccv.flags &= ~CCF_ABC_SENTAWND;
349dbc42409SLawrence Stewart 				tp->t_bytes_acked = 0;
350dbc42409SLawrence Stewart 		}
351dbc42409SLawrence Stewart 	}
352dbc42409SLawrence Stewart 
353dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->ack_received != NULL) {
354dbc42409SLawrence Stewart 		/* XXXLAS: Find a way to live without this */
355e68b3792SGleb Smirnoff 		tp->t_ccv.curack = th->th_ack;
356e68b3792SGleb Smirnoff 		CC_ALGO(tp)->ack_received(&tp->t_ccv, type);
357dbc42409SLawrence Stewart 	}
358adc56f5aSEdward Tomasz Napierala #ifdef STATS
359adc56f5aSEdward Tomasz Napierala 	stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_LCWIN, tp->snd_cwnd);
360adc56f5aSEdward Tomasz Napierala #endif
361dbc42409SLawrence Stewart }
362dbc42409SLawrence Stewart 
36355bceb1eSRandall Stewart void
364dbc42409SLawrence Stewart cc_conn_init(struct tcpcb *tp)
365dbc42409SLawrence Stewart {
366dbc42409SLawrence Stewart 	struct hc_metrics_lite metrics;
3679eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
3680c39d38dSGleb Smirnoff 	u_int maxseg;
369dbc42409SLawrence Stewart 	int rtt;
370dbc42409SLawrence Stewart 
3719eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
372dbc42409SLawrence Stewart 
373dbc42409SLawrence Stewart 	tcp_hc_get(&inp->inp_inc, &metrics);
3740c39d38dSGleb Smirnoff 	maxseg = tcp_maxseg(tp);
375dbc42409SLawrence Stewart 
376dbc42409SLawrence Stewart 	if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) {
377dbc42409SLawrence Stewart 		tp->t_srtt = rtt;
378dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_usedrtt);
379dbc42409SLawrence Stewart 		if (metrics.rmx_rttvar) {
380dbc42409SLawrence Stewart 			tp->t_rttvar = metrics.rmx_rttvar;
381dbc42409SLawrence Stewart 			TCPSTAT_INC(tcps_usedrttvar);
382dbc42409SLawrence Stewart 		} else {
383dbc42409SLawrence Stewart 			/* default variation is +- 1 rtt */
384dbc42409SLawrence Stewart 			tp->t_rttvar =
385dbc42409SLawrence Stewart 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
386dbc42409SLawrence Stewart 		}
387dbc42409SLawrence Stewart 		TCPT_RANGESET(tp->t_rxtcur,
388dbc42409SLawrence Stewart 		    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
389dbc42409SLawrence Stewart 		    tp->t_rttmin, TCPTV_REXMTMAX);
390dbc42409SLawrence Stewart 	}
391dbc42409SLawrence Stewart 	if (metrics.rmx_ssthresh) {
392dbc42409SLawrence Stewart 		/*
393dbc42409SLawrence Stewart 		 * There's some sort of gateway or interface
394dbc42409SLawrence Stewart 		 * buffer limit on the path.  Use this to set
395a4641f4eSPedro F. Giffuni 		 * the slow start threshold, but set the
396dbc42409SLawrence Stewart 		 * threshold to no less than 2*mss.
397dbc42409SLawrence Stewart 		 */
3980c39d38dSGleb Smirnoff 		tp->snd_ssthresh = max(2 * maxseg, metrics.rmx_ssthresh);
399dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_usedssthresh);
400dbc42409SLawrence Stewart 	}
401dbc42409SLawrence Stewart 
402dbc42409SLawrence Stewart 	/*
4039ec4a4ccSAndre Oppermann 	 * Set the initial slow-start flight size.
404dbc42409SLawrence Stewart 	 *
405cf8f04f4SAndre Oppermann 	 * If a SYN or SYN/ACK was lost and retransmitted, we have to
406cf8f04f4SAndre Oppermann 	 * reduce the initial CWND to one segment as congestion is likely
407cf8f04f4SAndre Oppermann 	 * requiring us to be cautious.
408dbc42409SLawrence Stewart 	 */
409cf8f04f4SAndre Oppermann 	if (tp->snd_cwnd == 1)
4100c39d38dSGleb Smirnoff 		tp->snd_cwnd = maxseg;		/* SYN(-ACK) lost */
411dbc42409SLawrence Stewart 	else
4127dc90a1dSMichael Tuexen 		tp->snd_cwnd = tcp_compute_initwnd(maxseg);
413dbc42409SLawrence Stewart 
414dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->conn_init != NULL)
415e68b3792SGleb Smirnoff 		CC_ALGO(tp)->conn_init(&tp->t_ccv);
416dbc42409SLawrence Stewart }
417dbc42409SLawrence Stewart 
418dbc42409SLawrence Stewart void inline
419dbc42409SLawrence Stewart cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type)
420dbc42409SLawrence Stewart {
4219eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
422dbc42409SLawrence Stewart 
423adc56f5aSEdward Tomasz Napierala #ifdef STATS
424adc56f5aSEdward Tomasz Napierala 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_CSIG, type);
425adc56f5aSEdward Tomasz Napierala #endif
426adc56f5aSEdward Tomasz Napierala 
427dbc42409SLawrence Stewart 	switch(type) {
428dbc42409SLawrence Stewart 	case CC_NDUPACK:
429dbc42409SLawrence Stewart 		if (!IN_FASTRECOVERY(tp->t_flags)) {
430f2512ba1SRui Paulo 			tp->snd_recover = tp->snd_max;
4313cf38784SMichael Tuexen 			if (tp->t_flags2 & TF2_ECN_PERMIT)
4323cf38784SMichael Tuexen 				tp->t_flags2 |= TF2_ECN_SND_CWR;
433f2512ba1SRui Paulo 		}
434dbc42409SLawrence Stewart 		break;
435dbc42409SLawrence Stewart 	case CC_ECN:
436af2fb894SRichard Scheffenegger 		if (!IN_CONGRECOVERY(tp->t_flags) ||
437af2fb894SRichard Scheffenegger 		    /*
438af2fb894SRichard Scheffenegger 		     * Allow ECN reaction on ACK to CWR, if
439af2fb894SRichard Scheffenegger 		     * that data segment was also CE marked.
440af2fb894SRichard Scheffenegger 		     */
441af2fb894SRichard Scheffenegger 		    SEQ_GEQ(th->th_ack, tp->snd_recover)) {
442af2fb894SRichard Scheffenegger 			EXIT_CONGRECOVERY(tp->t_flags);
443dbc42409SLawrence Stewart 			TCPSTAT_INC(tcps_ecn_rcwnd);
444af2fb894SRichard Scheffenegger 			tp->snd_recover = tp->snd_max + 1;
4453cf38784SMichael Tuexen 			if (tp->t_flags2 & TF2_ECN_PERMIT)
4463cf38784SMichael Tuexen 				tp->t_flags2 |= TF2_ECN_SND_CWR;
447dbc42409SLawrence Stewart 		}
448dbc42409SLawrence Stewart 		break;
449dbc42409SLawrence Stewart 	case CC_RTO:
450dbc42409SLawrence Stewart 		tp->t_dupacks = 0;
451dbc42409SLawrence Stewart 		tp->t_bytes_acked = 0;
452dbc42409SLawrence Stewart 		EXIT_RECOVERY(tp->t_flags);
4539cc711c9SMichael Tuexen 		if (tp->t_flags2 & TF2_ECN_PERMIT)
4549cc711c9SMichael Tuexen 			tp->t_flags2 |= TF2_ECN_SND_CWR;
455dbc42409SLawrence Stewart 		break;
456dbc42409SLawrence Stewart 	case CC_RTO_ERR:
457dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_sndrexmitbad);
458dbc42409SLawrence Stewart 		/* RTO was unnecessary, so reset everything. */
459dbc42409SLawrence Stewart 		tp->snd_cwnd = tp->snd_cwnd_prev;
460dbc42409SLawrence Stewart 		tp->snd_ssthresh = tp->snd_ssthresh_prev;
461dbc42409SLawrence Stewart 		tp->snd_recover = tp->snd_recover_prev;
462dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASFRECOVERY)
463dbc42409SLawrence Stewart 			ENTER_FASTRECOVERY(tp->t_flags);
464dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASCRECOVERY)
465dbc42409SLawrence Stewart 			ENTER_CONGRECOVERY(tp->t_flags);
466dbc42409SLawrence Stewart 		tp->snd_nxt = tp->snd_max;
467672dc4aeSJohn Baldwin 		tp->t_flags &= ~TF_PREVVALID;
468dbc42409SLawrence Stewart 		tp->t_badrxtwin = 0;
469dbc42409SLawrence Stewart 		break;
470dbc42409SLawrence Stewart 	}
471dbc42409SLawrence Stewart 
472dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->cong_signal != NULL) {
473dbc42409SLawrence Stewart 		if (th != NULL)
474e68b3792SGleb Smirnoff 			tp->t_ccv.curack = th->th_ack;
475e68b3792SGleb Smirnoff 		CC_ALGO(tp)->cong_signal(&tp->t_ccv, type);
476dbc42409SLawrence Stewart 	}
477dbc42409SLawrence Stewart }
478dbc42409SLawrence Stewart 
47955bceb1eSRandall Stewart void inline
480dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th)
481dbc42409SLawrence Stewart {
4829eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
483dbc42409SLawrence Stewart 
484dbc42409SLawrence Stewart 	/* XXXLAS: KASSERT that we're in recovery? */
485dbc42409SLawrence Stewart 
486dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->post_recovery != NULL) {
487e68b3792SGleb Smirnoff 		tp->t_ccv.curack = th->th_ack;
488e68b3792SGleb Smirnoff 		CC_ALGO(tp)->post_recovery(&tp->t_ccv);
489dbc42409SLawrence Stewart 	}
490dbc42409SLawrence Stewart 	/* XXXLAS: EXIT_RECOVERY ? */
491dbc42409SLawrence Stewart 	tp->t_bytes_acked = 0;
49248be5b97SRichard Scheffenegger 	tp->sackhint.delivered_data = 0;
493e5313869SRichard Scheffenegger 	tp->sackhint.prr_out = 0;
494dbc42409SLawrence Stewart }
4950312fbe9SPoul-Henning Kamp 
496df8bae1dSRodney W. Grimes /*
497262c1c1aSMatthew Dillon  * Indicate whether this ack should be delayed.  We can delay the ack if
49885536381SHiren Panchasara  * following conditions are met:
49985536381SHiren Panchasara  *	- There is no delayed ack timer in progress.
50085536381SHiren Panchasara  *	- Our last ack wasn't a 0-sized window. We never want to delay
50185536381SHiren Panchasara  *	  the ack that opens up a 0-sized window.
50285536381SHiren Panchasara  *	- LRO wasn't used for this segment. We make sure by checking that the
50385536381SHiren Panchasara  *	  segment size is not larger than the MSS.
504d8c85a26SJonathan Lemon  */
505c1e5a6e5SAndre Oppermann #define DELAY_ACK(tp, tlen)						\
506b8152ba7SAndre Oppermann 	((!tcp_timer_active(tp, TT_DELACK) &&				\
507a14c749fSJonathan Lemon 	    (tp->t_flags & TF_RXWIN0SENT) == 0) &&			\
5080c39d38dSGleb Smirnoff 	    (tlen <= tp->t_maxseg) &&					\
509603724d3SBjoern A. Zeeb 	    (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN)))
510d8c85a26SJonathan Lemon 
5114ad24737SRandall Stewart void inline
5125d8fd932SRandall Stewart cc_ecnpkt_handler_flags(struct tcpcb *tp, uint16_t flags, uint8_t iptos)
51364807b30SHiren Panchasara {
5149eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
51564807b30SHiren Panchasara 
51664807b30SHiren Panchasara 	if (CC_ALGO(tp)->ecnpkt_handler != NULL) {
51764807b30SHiren Panchasara 		switch (iptos & IPTOS_ECN_MASK) {
51864807b30SHiren Panchasara 		case IPTOS_ECN_CE:
519e68b3792SGleb Smirnoff 			tp->t_ccv.flags |= CCF_IPHDR_CE;
52064807b30SHiren Panchasara 			break;
52164807b30SHiren Panchasara 		case IPTOS_ECN_ECT0:
52283a2839fSMichael Tuexen 			/* FALLTHROUGH */
52364807b30SHiren Panchasara 		case IPTOS_ECN_ECT1:
52483a2839fSMichael Tuexen 			/* FALLTHROUGH */
52583a2839fSMichael Tuexen 		case IPTOS_ECN_NOTECT:
526e68b3792SGleb Smirnoff 			tp->t_ccv.flags &= ~CCF_IPHDR_CE;
52764807b30SHiren Panchasara 			break;
52864807b30SHiren Panchasara 		}
52964807b30SHiren Panchasara 
5305d8fd932SRandall Stewart 		if (flags & TH_CWR)
531e68b3792SGleb Smirnoff 			tp->t_ccv.flags |= CCF_TCPHDR_CWR;
53264807b30SHiren Panchasara 		else
533e68b3792SGleb Smirnoff 			tp->t_ccv.flags &= ~CCF_TCPHDR_CWR;
53464807b30SHiren Panchasara 
535e68b3792SGleb Smirnoff 		CC_ALGO(tp)->ecnpkt_handler(&tp->t_ccv);
53664807b30SHiren Panchasara 
537e68b3792SGleb Smirnoff 		if (tp->t_ccv.flags & CCF_ACKNOW) {
53864807b30SHiren Panchasara 			tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
539e11c9783SMichael Tuexen 			tp->t_flags |= TF_ACKNOW;
540e11c9783SMichael Tuexen 		}
54164807b30SHiren Panchasara 	}
54264807b30SHiren Panchasara }
54364807b30SHiren Panchasara 
5445d8fd932SRandall Stewart void inline
5455d8fd932SRandall Stewart cc_ecnpkt_handler(struct tcpcb *tp, struct tcphdr *th, uint8_t iptos)
5465d8fd932SRandall Stewart {
5471ebf4607SRichard Scheffenegger 	cc_ecnpkt_handler_flags(tp, tcp_get_flags(th), iptos);
5485d8fd932SRandall Stewart }
5495d8fd932SRandall Stewart 
550df8bae1dSRodney W. Grimes /*
5518d573cc1SAndre Oppermann  * TCP input handling is split into multiple parts:
5528d573cc1SAndre Oppermann  *   tcp6_input is a thin wrapper around tcp_input for the extended
5538d573cc1SAndre Oppermann  *	ip6_protox[] call format in ip6_input
5548d573cc1SAndre Oppermann  *   tcp_input handles primary segment validation, inpcb lookup and
5558d573cc1SAndre Oppermann  *	SYN processing on listen sockets
5568d573cc1SAndre Oppermann  *   tcp_do_segment processes the ACK and text of the segment for
5578d573cc1SAndre Oppermann  *	establishing, established and closing connections
558df8bae1dSRodney W. Grimes  */
559fb59c426SYoshinobu Inoue #ifdef INET6
560fb59c426SYoshinobu Inoue int
5619e644c23SMichael Tuexen tcp6_input_with_port(struct mbuf **mp, int *offp, int proto, uint16_t port)
562fb59c426SYoshinobu Inoue {
563503f4e47SBjoern A. Zeeb 	struct mbuf *m;
56433841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6;
5653e88eb90SAndrey V. Elsukov 	struct ip6_hdr *ip6;
566fb59c426SYoshinobu Inoue 
567503f4e47SBjoern A. Zeeb 	m = *mp;
568a4adf6ccSBjoern A. Zeeb 	if (m->m_len < *offp + sizeof(struct tcphdr)) {
5694e619b17SBjoern A. Zeeb 		m = m_pullup(m, *offp + sizeof(struct tcphdr));
5704e619b17SBjoern A. Zeeb 		if (m == NULL) {
5714e619b17SBjoern A. Zeeb 			*mp = m;
5724e619b17SBjoern A. Zeeb 			TCPSTAT_INC(tcps_rcvshort);
5734e619b17SBjoern A. Zeeb 			return (IPPROTO_DONE);
5744e619b17SBjoern A. Zeeb 		}
575a4adf6ccSBjoern A. Zeeb 	}
576fb59c426SYoshinobu Inoue 
577fb59c426SYoshinobu Inoue 	/*
578fb59c426SYoshinobu Inoue 	 * draft-itojun-ipv6-tcp-to-anycast
579fb59c426SYoshinobu Inoue 	 * better place to put this in?
580fb59c426SYoshinobu Inoue 	 */
5813e88eb90SAndrey V. Elsukov 	ip6 = mtod(m, struct ip6_hdr *);
5828268d82cSAlexander V. Chernikov 	ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false);
58333841545SHajimu UMEMOTO 	if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
584fb59c426SYoshinobu Inoue 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
585fb59c426SYoshinobu Inoue 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
586a8fe77d8SBjoern A. Zeeb 		*mp = NULL;
5878f5a8818SKevin Lo 		return (IPPROTO_DONE);
588fb59c426SYoshinobu Inoue 	}
589fb59c426SYoshinobu Inoue 
590a8fe77d8SBjoern A. Zeeb 	*mp = m;
5919e644c23SMichael Tuexen 	return (tcp_input_with_port(mp, offp, proto, port));
5929e644c23SMichael Tuexen }
5939e644c23SMichael Tuexen 
5949e644c23SMichael Tuexen int
5959e644c23SMichael Tuexen tcp6_input(struct mbuf **mp, int *offp, int proto)
5969e644c23SMichael Tuexen {
5979e644c23SMichael Tuexen 
5989e644c23SMichael Tuexen 	return(tcp6_input_with_port(mp, offp, proto, 0));
599fb59c426SYoshinobu Inoue }
600b287c6c7SBjoern A. Zeeb #endif /* INET6 */
601fb59c426SYoshinobu Inoue 
6028f5a8818SKevin Lo int
6039e644c23SMichael Tuexen tcp_input_with_port(struct mbuf **mp, int *offp, int proto, uint16_t port)
604df8bae1dSRodney W. Grimes {
6058f5a8818SKevin Lo 	struct mbuf *m = *mp;
606b287c6c7SBjoern A. Zeeb 	struct tcphdr *th = NULL;
607ad3f9ab3SAndre Oppermann 	struct ip *ip = NULL;
608ad3f9ab3SAndre Oppermann 	struct inpcb *inp = NULL;
609302ce8d6SAndre Oppermann 	struct tcpcb *tp = NULL;
610302ce8d6SAndre Oppermann 	struct socket *so = NULL;
611e79adb8eSGarrett Wollman 	u_char *optp = NULL;
6128f5a8818SKevin Lo 	int off0;
61326f9a767SRodney W. Grimes 	int optlen = 0;
614b287c6c7SBjoern A. Zeeb #ifdef INET
615b287c6c7SBjoern A. Zeeb 	int len;
616c4556b2fSAndrey V. Elsukov 	uint8_t ipttl;
617b287c6c7SBjoern A. Zeeb #endif
618b287c6c7SBjoern A. Zeeb 	int tlen = 0, off;
619fb59c426SYoshinobu Inoue 	int drop_hdrlen;
620ad3f9ab3SAndre Oppermann 	int thflags;
621302ce8d6SAndre Oppermann 	int rstreason = 0;	/* For badport_bandlim accounting purposes */
62208d9c920SGleb Smirnoff 	int lookupflag;
6231d7ee746SKurt Lidl 	uint8_t iptos;
624c1de64a4SAndrey V. Elsukov 	struct m_tag *fwd_tag = NULL;
625c068736aSJeffrey Hsu #ifdef INET6
626302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6 = NULL;
627c068736aSJeffrey Hsu 	int isipv6;
628c068736aSJeffrey Hsu #else
629a160e630SAndre Oppermann 	const void *ip6 = NULL;
630b287c6c7SBjoern A. Zeeb #endif /* INET6 */
631302ce8d6SAndre Oppermann 	struct tcpopt to;		/* options in this segment */
632a160e630SAndre Oppermann 	char *s = NULL;			/* address and port logging */
633c068736aSJeffrey Hsu 
634d40c0d47SGleb Smirnoff 	NET_EPOCH_ASSERT();
635d40c0d47SGleb Smirnoff 
636fb59c426SYoshinobu Inoue #ifdef INET6
637fb59c426SYoshinobu Inoue 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
638fb59c426SYoshinobu Inoue #endif
639a0292f23SGarrett Wollman 
6408f5a8818SKevin Lo 	off0 = *offp;
6418f5a8818SKevin Lo 	m = *mp;
6428f5a8818SKevin Lo 	*mp = NULL;
643302ce8d6SAndre Oppermann 	to.to_flags = 0;
64478b50714SRobert Watson 	TCPSTAT_INC(tcps_rcvtotal);
645fb59c426SYoshinobu Inoue 
64604d3a452SHajimu UMEMOTO #ifdef INET6
647b287c6c7SBjoern A. Zeeb 	if (isipv6) {
648fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
64945747ba5SBjoern A. Zeeb 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
650fb59c426SYoshinobu Inoue 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
6519e644c23SMichael Tuexen 		if (port)
6529e644c23SMichael Tuexen 			goto skip6_csum;
653356ab07eSBjoern A. Zeeb 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) {
65445747ba5SBjoern A. Zeeb 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
65545747ba5SBjoern A. Zeeb 				th->th_sum = m->m_pkthdr.csum_data;
65645747ba5SBjoern A. Zeeb 			else
65745747ba5SBjoern A. Zeeb 				th->th_sum = in6_cksum_pseudo(ip6, tlen,
65845747ba5SBjoern A. Zeeb 				    IPPROTO_TCP, m->m_pkthdr.csum_data);
65945747ba5SBjoern A. Zeeb 			th->th_sum ^= 0xffff;
66045747ba5SBjoern A. Zeeb 		} else
66145747ba5SBjoern A. Zeeb 			th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen);
66245747ba5SBjoern A. Zeeb 		if (th->th_sum) {
66378b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
664fb59c426SYoshinobu Inoue 			goto drop;
665fb59c426SYoshinobu Inoue 		}
6669e644c23SMichael Tuexen 	skip6_csum:
66733841545SHajimu UMEMOTO 		/*
66833841545SHajimu UMEMOTO 		 * Be proactive about unspecified IPv6 address in source.
66933841545SHajimu UMEMOTO 		 * As we use all-zero to indicate unbounded/unconnected pcb,
67033841545SHajimu UMEMOTO 		 * unspecified IPv6 address can be used to confuse us.
67133841545SHajimu UMEMOTO 		 *
67233841545SHajimu UMEMOTO 		 * Note that packets with unspecified IPv6 destination is
67333841545SHajimu UMEMOTO 		 * already dropped in ip6_input.
67433841545SHajimu UMEMOTO 		 */
675713264f6SMark Johnston 		KASSERT(!IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst),
676713264f6SMark Johnston 		    ("%s: unspecified destination v6 address", __func__));
67733841545SHajimu UMEMOTO 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
67878e6c3aaSGleb Smirnoff 			IP6STAT_INC(ip6s_badscope); /* XXX */
67933841545SHajimu UMEMOTO 			goto drop;
68033841545SHajimu UMEMOTO 		}
681bb4a7d94SKristof Provost 		iptos = IPV6_TRAFFIC_CLASS(ip6);
682b287c6c7SBjoern A. Zeeb 	}
68304d3a452SHajimu UMEMOTO #endif
684b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
685b287c6c7SBjoern A. Zeeb 	else
686b287c6c7SBjoern A. Zeeb #endif
687b287c6c7SBjoern A. Zeeb #ifdef INET
688b287c6c7SBjoern A. Zeeb 	{
689df8bae1dSRodney W. Grimes 		/*
690df8bae1dSRodney W. Grimes 		 * Get IP and TCP header together in first mbuf.
691df8bae1dSRodney W. Grimes 		 * Note: IP leaves IP header in first mbuf.
692df8bae1dSRodney W. Grimes 		 */
693fb59c426SYoshinobu Inoue 		if (off0 > sizeof (struct ip)) {
694105bd211SGleb Smirnoff 			ip_stripoptions(m);
695fb59c426SYoshinobu Inoue 			off0 = sizeof(struct ip);
696fb59c426SYoshinobu Inoue 		}
697df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof (struct tcpiphdr)) {
6984dfdffe9SAndre Oppermann 			if ((m = m_pullup(m, sizeof (struct tcpiphdr)))
6994dfdffe9SAndre Oppermann 			    == NULL) {
70078b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvshort);
7018f5a8818SKevin Lo 				return (IPPROTO_DONE);
702df8bae1dSRodney W. Grimes 			}
703df8bae1dSRodney W. Grimes 		}
704fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
705db4f9cc7SJonathan Lemon 		th = (struct tcphdr *)((caddr_t)ip + off0);
7068ad458a4SGleb Smirnoff 		tlen = ntohs(ip->ip_len) - off0;
707df8bae1dSRodney W. Grimes 
7081d7ee746SKurt Lidl 		iptos = ip->ip_tos;
7099e644c23SMichael Tuexen 		if (port)
7109e644c23SMichael Tuexen 			goto skip_csum;
711db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
712db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
713db4f9cc7SJonathan Lemon 				th->th_sum = m->m_pkthdr.csum_data;
714db4f9cc7SJonathan Lemon 			else
715db4f9cc7SJonathan Lemon 				th->th_sum = in_pseudo(ip->ip_src.s_addr,
716c068736aSJeffrey Hsu 				    ip->ip_dst.s_addr,
7178f134647SGleb Smirnoff 				    htonl(m->m_pkthdr.csum_data + tlen +
718c068736aSJeffrey Hsu 				    IPPROTO_TCP));
719db4f9cc7SJonathan Lemon 			th->th_sum ^= 0xffff;
720db4f9cc7SJonathan Lemon 		} else {
7218f134647SGleb Smirnoff 			struct ipovly *ipov = (struct ipovly *)ip;
7228f134647SGleb Smirnoff 
723df8bae1dSRodney W. Grimes 			/*
724df8bae1dSRodney W. Grimes 			 * Checksum extended TCP header and data.
725df8bae1dSRodney W. Grimes 			 */
7268f134647SGleb Smirnoff 			len = off0 + tlen;
727c4556b2fSAndrey V. Elsukov 			ipttl = ip->ip_ttl;
728fb59c426SYoshinobu Inoue 			bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
7298f134647SGleb Smirnoff 			ipov->ih_len = htons(tlen);
730fb59c426SYoshinobu Inoue 			th->th_sum = in_cksum(m, len);
73157f60867SMark Johnston 			/* Reset length for SDT probes. */
7321d7ee746SKurt Lidl 			ip->ip_len = htons(len);
7331d7ee746SKurt Lidl 			/* Reset TOS bits */
7341d7ee746SKurt Lidl 			ip->ip_tos = iptos;
7351d7ee746SKurt Lidl 			/* Re-initialization for later version check */
736c4556b2fSAndrey V. Elsukov 			ip->ip_ttl = ipttl;
7371d7ee746SKurt Lidl 			ip->ip_v = IPVERSION;
738b2fe54bcSAndrey V. Elsukov 			ip->ip_hl = off0 >> 2;
739db4f9cc7SJonathan Lemon 		}
7409e644c23SMichael Tuexen 	skip_csum:
7419e644c23SMichael Tuexen 		if (th->th_sum && (port == 0)) {
74278b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
743df8bae1dSRodney W. Grimes 			goto drop;
744df8bae1dSRodney W. Grimes 		}
745713264f6SMark Johnston 		KASSERT(ip->ip_dst.s_addr != INADDR_ANY,
746713264f6SMark Johnston 		    ("%s: unspecified destination v4 address", __func__));
747713264f6SMark Johnston 		if (__predict_false(ip->ip_src.s_addr == INADDR_ANY)) {
74878e6c3aaSGleb Smirnoff 			IPSTAT_INC(ips_badaddr);
749713264f6SMark Johnston 			goto drop;
750713264f6SMark Johnston 		}
751fb59c426SYoshinobu Inoue 	}
752b287c6c7SBjoern A. Zeeb #endif /* INET */
753df8bae1dSRodney W. Grimes 
754df8bae1dSRodney W. Grimes 	/*
755df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
756df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
757df8bae1dSRodney W. Grimes 	 */
758fb59c426SYoshinobu Inoue 	off = th->th_off << 2;
759df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
76078b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvbadoff);
761df8bae1dSRodney W. Grimes 		goto drop;
762df8bae1dSRodney W. Grimes 	}
763fb59c426SYoshinobu Inoue 	tlen -= off;	/* tlen is used instead of ti->ti_len */
764df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
76504d3a452SHajimu UMEMOTO #ifdef INET6
766b287c6c7SBjoern A. Zeeb 		if (isipv6) {
767a4adf6ccSBjoern A. Zeeb 			if (m->m_len < off0 + off) {
7684e619b17SBjoern A. Zeeb 				m = m_pullup(m, off0 + off);
7694e619b17SBjoern A. Zeeb 				if (m == NULL) {
7704e619b17SBjoern A. Zeeb 					TCPSTAT_INC(tcps_rcvshort);
7714e619b17SBjoern A. Zeeb 					return (IPPROTO_DONE);
7724e619b17SBjoern A. Zeeb 				}
773a4adf6ccSBjoern A. Zeeb 			}
774fb59c426SYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
775fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
776b287c6c7SBjoern A. Zeeb 		}
77704d3a452SHajimu UMEMOTO #endif
778b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
779b287c6c7SBjoern A. Zeeb 		else
780b287c6c7SBjoern A. Zeeb #endif
781b287c6c7SBjoern A. Zeeb #ifdef INET
782b287c6c7SBjoern A. Zeeb 		{
783df8bae1dSRodney W. Grimes 			if (m->m_len < sizeof(struct ip) + off) {
784c068736aSJeffrey Hsu 				if ((m = m_pullup(m, sizeof (struct ip) + off))
7854dfdffe9SAndre Oppermann 				    == NULL) {
78678b50714SRobert Watson 					TCPSTAT_INC(tcps_rcvshort);
7878f5a8818SKevin Lo 					return (IPPROTO_DONE);
788df8bae1dSRodney W. Grimes 				}
789fb59c426SYoshinobu Inoue 				ip = mtod(m, struct ip *);
790fb59c426SYoshinobu Inoue 				th = (struct tcphdr *)((caddr_t)ip + off0);
791fb59c426SYoshinobu Inoue 			}
792df8bae1dSRodney W. Grimes 		}
793b287c6c7SBjoern A. Zeeb #endif
794df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
795fb59c426SYoshinobu Inoue 		optp = (u_char *)(th + 1);
796df8bae1dSRodney W. Grimes 	}
7971ebf4607SRichard Scheffenegger 	thflags = tcp_get_flags(th);
798df8bae1dSRodney W. Grimes 
799e46cd3d4SDag-Erling Smørgrav 	/*
800df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
801df8bae1dSRodney W. Grimes 	 */
8022903309aSAttilio Rao 	tcp_fields_to_host(th);
803df8bae1dSRodney W. Grimes 
804df8bae1dSRodney W. Grimes 	/*
805794235b7SAndre Oppermann 	 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options.
806755c1f07SAndras Olah 	 */
807fb59c426SYoshinobu Inoue 	drop_hdrlen = off0 + off;
808755c1f07SAndras Olah 
809755c1f07SAndras Olah 	/*
8108d573cc1SAndre Oppermann 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
8118d573cc1SAndre Oppermann 	 */
812b8056faeSGleb Smirnoff         if (
813b8056faeSGleb Smirnoff #ifdef INET6
814b8056faeSGleb Smirnoff 	    (isipv6 && (m->m_flags & M_IP6_NEXTHOP))
815b8056faeSGleb Smirnoff #ifdef INET
816b8056faeSGleb Smirnoff 	    || (!isipv6 && (m->m_flags & M_IP_NEXTHOP))
817b8056faeSGleb Smirnoff #endif
818b8056faeSGleb Smirnoff #endif
819b8056faeSGleb Smirnoff #if defined(INET) && !defined(INET6)
820b8056faeSGleb Smirnoff 	    (m->m_flags & M_IP_NEXTHOP)
821b8056faeSGleb Smirnoff #endif
822b8056faeSGleb Smirnoff 	    )
8239b932e9eSAndre Oppermann 		fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
8249b932e9eSAndre Oppermann 
82508d9c920SGleb Smirnoff 	/*
8261db08fbeSGleb Smirnoff 	 * For initial SYN packets we don't need write lock on matching
8271db08fbeSGleb Smirnoff 	 * PCB, be it a listening one or a synchronized one.  The packet
8281db08fbeSGleb Smirnoff 	 * shall not modify its state.
82908d9c920SGleb Smirnoff 	 */
830d88eb465SGleb Smirnoff 	lookupflag = INPLOOKUP_WILDCARD |
831d88eb465SGleb Smirnoff 	    ((thflags & (TH_ACK|TH_SYN)) == TH_SYN ?
832d88eb465SGleb Smirnoff 	    INPLOOKUP_RLOCKPCB : INPLOOKUP_WLOCKPCB);
833ce7ad664SEd Maste findpcb:
8348a006adbSBjoern A. Zeeb #ifdef INET6
8358a006adbSBjoern A. Zeeb 	if (isipv6 && fwd_tag != NULL) {
8368a006adbSBjoern A. Zeeb 		struct sockaddr_in6 *next_hop6;
8378a006adbSBjoern A. Zeeb 
8388a006adbSBjoern A. Zeeb 		next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
8398a006adbSBjoern A. Zeeb 		/*
8408a006adbSBjoern A. Zeeb 		 * Transparently forwarded. Pretend to be the destination.
8418a006adbSBjoern A. Zeeb 		 * Already got one like this?
8428a006adbSBjoern A. Zeeb 		 */
8438a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo,
8448a006adbSBjoern A. Zeeb 		    &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport,
845aab8c844SGleb Smirnoff 		    lookupflag & ~INPLOOKUP_WILDCARD, m->m_pkthdr.rcvif, m);
8468a006adbSBjoern A. Zeeb 		if (!inp) {
8478a006adbSBjoern A. Zeeb 			/*
8488a006adbSBjoern A. Zeeb 			 * It's new.  Try to find the ambushing socket.
8498a006adbSBjoern A. Zeeb 			 * Because we've rewritten the destination address,
8508a006adbSBjoern A. Zeeb 			 * any hardware-generated hash is ignored.
8518a006adbSBjoern A. Zeeb 			 */
8528a006adbSBjoern A. Zeeb 			inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src,
8538a006adbSBjoern A. Zeeb 			    th->th_sport, &next_hop6->sin6_addr,
8548a006adbSBjoern A. Zeeb 			    next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) :
855d88eb465SGleb Smirnoff 			    th->th_dport, lookupflag, m->m_pkthdr.rcvif);
8568a006adbSBjoern A. Zeeb 		}
857c1de64a4SAndrey V. Elsukov 	} else if (isipv6) {
8588a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src,
859d88eb465SGleb Smirnoff 		    th->th_sport, &ip6->ip6_dst, th->th_dport, lookupflag,
860d88eb465SGleb Smirnoff 		    m->m_pkthdr.rcvif, m);
8618a006adbSBjoern A. Zeeb 	}
8628a006adbSBjoern A. Zeeb #endif /* INET6 */
8638a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET)
8648a006adbSBjoern A. Zeeb 	else
8658a006adbSBjoern A. Zeeb #endif
8668a006adbSBjoern A. Zeeb #ifdef INET
8678a006adbSBjoern A. Zeeb 	if (fwd_tag != NULL) {
8689b932e9eSAndre Oppermann 		struct sockaddr_in *next_hop;
8699b932e9eSAndre Oppermann 
8709b932e9eSAndre Oppermann 		next_hop = (struct sockaddr_in *)(fwd_tag+1);
871f9e354dfSJulian Elischer 		/*
8722b25acc1SLuigi Rizzo 		 * Transparently forwarded. Pretend to be the destination.
873f9e354dfSJulian Elischer 		 * already got one like this?
874f9e354dfSJulian Elischer 		 */
875d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport,
876aab8c844SGleb Smirnoff 		    ip->ip_dst, th->th_dport, lookupflag & ~INPLOOKUP_WILDCARD,
877aab8c844SGleb Smirnoff 		    m->m_pkthdr.rcvif, m);
878fa046d87SRobert Watson 		if (!inp) {
879fa046d87SRobert Watson 			/*
880fa046d87SRobert Watson 			 * It's new.  Try to find the ambushing socket.
881d3c1f003SRobert Watson 			 * Because we've rewritten the destination address,
882d3c1f003SRobert Watson 			 * any hardware-generated hash is ignored.
883fa046d87SRobert Watson 			 */
884fa046d87SRobert Watson 			inp = in_pcblookup(&V_tcbinfo, ip->ip_src,
885fa046d87SRobert Watson 			    th->th_sport, next_hop->sin_addr,
886fa046d87SRobert Watson 			    next_hop->sin_port ? ntohs(next_hop->sin_port) :
887d88eb465SGleb Smirnoff 			    th->th_dport, lookupflag, m->m_pkthdr.rcvif);
888f9e354dfSJulian Elischer 		}
889b10fbdeaSAndre Oppermann 	} else
890d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src,
891d88eb465SGleb Smirnoff 		    th->th_sport, ip->ip_dst, th->th_dport, lookupflag,
892d88eb465SGleb Smirnoff 		    m->m_pkthdr.rcvif, m);
8938a006adbSBjoern A. Zeeb #endif /* INET */
894f9e354dfSJulian Elischer 
895df8bae1dSRodney W. Grimes 	/*
896574b6964SAndre Oppermann 	 * If the INPCB does not exist then all data in the incoming
897574b6964SAndre Oppermann 	 * segment is discarded and an appropriate RST is sent back.
8988b07e49aSJulian Elischer 	 * XXX MRT Send RST using which routing table?
899df8bae1dSRodney W. Grimes 	 */
900816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
901d88eb465SGleb Smirnoff 		if (rstreason != 0) {
902d88eb465SGleb Smirnoff 			/* We came here after second (safety) lookup. */
903d88eb465SGleb Smirnoff 			MPASS((lookupflag & INPLOOKUP_WILDCARD) == 0);
904d88eb465SGleb Smirnoff 			goto dropwithreset;
905d88eb465SGleb Smirnoff 		}
906574b6964SAndre Oppermann 		/*
907574b6964SAndre Oppermann 		 * Log communication attempts to ports that are not
908574b6964SAndre Oppermann 		 * in use.
909574b6964SAndre Oppermann 		 */
910334fc582SBjoern A. Zeeb 		if ((V_tcp_log_in_vain == 1 && (thflags & TH_SYN)) ||
911334fc582SBjoern A. Zeeb 		    V_tcp_log_in_vain == 2) {
912b7d747ecSAndre Oppermann 			if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6)))
913df541e5fSAndre Oppermann 				log(LOG_INFO, "%s; %s: Connection attempt "
914df541e5fSAndre Oppermann 				    "to closed port\n", s, __func__);
9152e4e1b4cSGeoff Rehmet 		}
916574b6964SAndre Oppermann 		/*
917574b6964SAndre Oppermann 		 * When blackholing do not respond with a RST but
918574b6964SAndre Oppermann 		 * completely ignore the segment and drop it.
919574b6964SAndre Oppermann 		 */
9203ea9a7cfSGleb Smirnoff 		if (((V_blackhole == 1 && (thflags & TH_SYN)) ||
921ad3ad064SGleb Smirnoff 		    V_blackhole == 2) && (V_blackhole_local || (
9223ea9a7cfSGleb Smirnoff #ifdef INET6
9233ea9a7cfSGleb Smirnoff 		    isipv6 ? !in6_localaddr(&ip6->ip6_src) :
9243ea9a7cfSGleb Smirnoff #endif
9253ea9a7cfSGleb Smirnoff #ifdef INET
9263ea9a7cfSGleb Smirnoff 		    !in_localip(ip->ip_src)
9273ea9a7cfSGleb Smirnoff #else
9283ea9a7cfSGleb Smirnoff 		    true
9293ea9a7cfSGleb Smirnoff #endif
930ad3ad064SGleb Smirnoff 		    )))
9311929eae1SAndre Oppermann 			goto dropunlock;
932574b6964SAndre Oppermann 
933a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
934a57815efSBosko Milekic 		goto dropwithreset;
935816a3d83SPoul-Henning Kamp 	}
93608d9c920SGleb Smirnoff 	INP_LOCK_ASSERT(inp);
937f567d55fSGleb Smirnoff 
938c2529042SHans Petter Selasky 	if ((inp->inp_flowtype == M_HASHTYPE_NONE) &&
939c2529042SHans Petter Selasky 	    (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) &&
940f71cb9f7SGleb Smirnoff 	    !SOLISTENING(inp->inp_socket)) {
94180cb9f21SKip Macy 		inp->inp_flowid = m->m_pkthdr.flowid;
9422f719932SAdrian Chadd 		inp->inp_flowtype = M_HASHTYPE_GET(m);
94380cb9f21SKip Macy 	}
944fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
945a2589465SKen Smith #ifdef INET6
946fcf59617SAndrey V. Elsukov 	if (isipv6 && IPSEC_ENABLED(ipv6) &&
947fcf59617SAndrey V. Elsukov 	    IPSEC_CHECK_POLICY(ipv6, m, inp) != 0) {
948a2589465SKen Smith 		goto dropunlock;
949a2589465SKen Smith 	}
950fcf59617SAndrey V. Elsukov #ifdef INET
951fcf59617SAndrey V. Elsukov 	else
952fcf59617SAndrey V. Elsukov #endif
953fcf59617SAndrey V. Elsukov #endif /* INET6 */
954fcf59617SAndrey V. Elsukov #ifdef INET
955fcf59617SAndrey V. Elsukov 	if (IPSEC_ENABLED(ipv4) &&
956fcf59617SAndrey V. Elsukov 	    IPSEC_CHECK_POLICY(ipv4, m, inp) != 0) {
957fcf59617SAndrey V. Elsukov 		goto dropunlock;
958fcf59617SAndrey V. Elsukov 	}
959fcf59617SAndrey V. Elsukov #endif /* INET */
960a2589465SKen Smith #endif /* IPSEC */
961a2589465SKen Smith 
9628d573cc1SAndre Oppermann 	/*
9638d573cc1SAndre Oppermann 	 * Check the minimum TTL for socket.
9648d573cc1SAndre Oppermann 	 */
96534f83c52SGeorge V. Neville-Neil 	if (inp->inp_ip_minttl != 0) {
96634f83c52SGeorge V. Neville-Neil #ifdef INET6
9672d8868dbSJonathan T. Looney 		if (isipv6) {
9682d8868dbSJonathan T. Looney 			if (inp->inp_ip_minttl > ip6->ip6_hlim)
969302ce8d6SAndre Oppermann 				goto dropunlock;
9702d8868dbSJonathan T. Looney 		} else
97134f83c52SGeorge V. Neville-Neil #endif
97234f83c52SGeorge V. Neville-Neil 		if (inp->inp_ip_minttl > ip->ip_ttl)
973302ce8d6SAndre Oppermann 			goto dropunlock;
97434f83c52SGeorge V. Neville-Neil 	}
975936cd18dSAndre Oppermann 
9760d744519SGleb Smirnoff 	tp = intotcpcb(inp);
9770d744519SGleb Smirnoff 	switch (tp->t_state) {
9780d744519SGleb Smirnoff 	case TCPS_TIME_WAIT:
979340c35deSJonathan Lemon 		/*
9800d744519SGleb Smirnoff 		 * A previous connection in TIMEWAIT state is supposed to catch
9810d744519SGleb Smirnoff 		 * stray or duplicate segments arriving late.  If this segment
9820d744519SGleb Smirnoff 		 * was a legitimate new connection attempt, the old INPCB gets
9830d744519SGleb Smirnoff 		 * removed and we can try again to find a listening socket.
984340c35deSJonathan Lemon 		 */
985283c76c7SMichael Tuexen 		tcp_dooptions(&to, optp, optlen,
986283c76c7SMichael Tuexen 		    (thflags & TH_SYN) ? TO_SYN : 0);
9878d573cc1SAndre Oppermann 		/*
9880d744519SGleb Smirnoff 		 * tcp_twcheck unlocks the inp always, and frees the m if fails.
9898d573cc1SAndre Oppermann 		 */
9902104448fSAndre Oppermann 		if (tcp_twcheck(inp, &to, th, m, tlen))
991340c35deSJonathan Lemon 			goto findpcb;
9928f5a8818SKevin Lo 		return (IPPROTO_DONE);
9930d744519SGleb Smirnoff 	case TCPS_CLOSED:
994794235b7SAndre Oppermann 		/*
995794235b7SAndre Oppermann 		 * The TCPCB may no longer exist if the connection is winding
996794235b7SAndre Oppermann 		 * down or it is in the CLOSED state.  Either way we drop the
997794235b7SAndre Oppermann 		 * segment and send an appropriate response.
998794235b7SAndre Oppermann 		 */
999a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
1000a57815efSBosko Milekic 		goto dropwithreset;
100109f81a46SBosko Milekic 	}
1002df8bae1dSRodney W. Grimes 
10039e644c23SMichael Tuexen 	if ((tp->t_port != port) && (tp->t_state > TCPS_LISTEN)) {
10049e644c23SMichael Tuexen 		rstreason = BANDLIM_RST_CLOSEDPORT;
10059e644c23SMichael Tuexen 		goto dropwithreset;
10069e644c23SMichael Tuexen 	}
10079e644c23SMichael Tuexen 
100809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
100909fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
101009fe6320SNavdeep Parhar 		tcp_offload_input(tp, m);
101109fe6320SNavdeep Parhar 		m = NULL;	/* consumed by the TOE driver */
101209fe6320SNavdeep Parhar 		goto dropunlock;
101309fe6320SNavdeep Parhar 	}
101409fe6320SNavdeep Parhar #endif
101509fe6320SNavdeep Parhar 
1016c488362eSRobert Watson #ifdef MAC
101730d239bcSRobert Watson 	if (mac_inpcb_check_deliver(inp, m))
1018302ce8d6SAndre Oppermann 		goto dropunlock;
1019c488362eSRobert Watson #endif
1020a557af22SRobert Watson 	so = inp->inp_socket;
1021302ce8d6SAndre Oppermann 	KASSERT(so != NULL, ("%s: so == NULL", __func__));
1022db33b3e6SAndre Oppermann 	/*
1023db33b3e6SAndre Oppermann 	 * When the socket is accepting connections (the INPCB is in LISTEN
1024db33b3e6SAndre Oppermann 	 * state) we look into the SYN cache if this is a new connection
1025a7c7f2a7SJohn Baldwin 	 * attempt or the completion of a previous one.
1026db33b3e6SAndre Oppermann 	 */
1027f4bb1869SMark Johnston 	KASSERT(tp->t_state == TCPS_LISTEN || !SOLISTENING(so),
102845274760SJonathan T. Looney 	    ("%s: so accepting but tp %p not listening", __func__, tp));
1029f4bb1869SMark Johnston 	if (tp->t_state == TCPS_LISTEN && SOLISTENING(so)) {
1030540e8b7eSJeffrey Hsu 		struct in_conninfo inc;
1031540e8b7eSJeffrey Hsu 
10328bfb1918SAndre Oppermann 		bzero(&inc, sizeof(inc));
1033302ce8d6SAndre Oppermann #ifdef INET6
1034be2ac88cSJonathan Lemon 		if (isipv6) {
1035dcdb4371SBjoern A. Zeeb 			inc.inc_flags |= INC_ISIPV6;
10365dff1c38SMichael Tuexen 			if (inp->inp_inc.inc_flags & INC_IPV6MINMTU)
10375dff1c38SMichael Tuexen 				inc.inc_flags |= INC_IPV6MINMTU;
1038be2ac88cSJonathan Lemon 			inc.inc6_faddr = ip6->ip6_src;
1039be2ac88cSJonathan Lemon 			inc.inc6_laddr = ip6->ip6_dst;
1040302ce8d6SAndre Oppermann 		} else
1041302ce8d6SAndre Oppermann #endif
1042302ce8d6SAndre Oppermann 		{
1043be2ac88cSJonathan Lemon 			inc.inc_faddr = ip->ip_src;
1044be2ac88cSJonathan Lemon 			inc.inc_laddr = ip->ip_dst;
1045be2ac88cSJonathan Lemon 		}
1046be2ac88cSJonathan Lemon 		inc.inc_fport = th->th_sport;
1047be2ac88cSJonathan Lemon 		inc.inc_lport = th->th_dport;
10487973fba3SJulian Elischer 		inc.inc_fibnum = so->so_fibnum;
1049be2ac88cSJonathan Lemon 
1050fb59c426SYoshinobu Inoue 		/*
10518d573cc1SAndre Oppermann 		 * Check for an existing connection attempt in syncache if
10528d573cc1SAndre Oppermann 		 * the flag is only ACK.  A successful lookup creates a new
10538d573cc1SAndre Oppermann 		 * socket appended to the listen queue in SYN_RECEIVED state.
1054fb59c426SYoshinobu Inoue 		 */
1055be2ac88cSJonathan Lemon 		if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
1056bf6d304aSAndre Oppermann 			/*
1057bf6d304aSAndre Oppermann 			 * Parse the TCP options here because
1058bf6d304aSAndre Oppermann 			 * syncookies need access to the reflected
1059bf6d304aSAndre Oppermann 			 * timestamp.
1060bf6d304aSAndre Oppermann 			 */
1061bf6d304aSAndre Oppermann 			tcp_dooptions(&to, optp, optlen, 0);
10629fa198beSAndre Oppermann 			/*
1063e7231d07SGleb Smirnoff 			 * NB: syncache_expand() doesn't unlock inp.
10649fa198beSAndre Oppermann 			 */
10659e644c23SMichael Tuexen 			rstreason = syncache_expand(&inc, &to, th, &so, m, port);
1066fcf59617SAndrey V. Elsukov 			if (rstreason < 0) {
1067fcf59617SAndrey V. Elsukov 				/*
1068fcf59617SAndrey V. Elsukov 				 * A failing TCP MD5 signature comparison
1069fcf59617SAndrey V. Elsukov 				 * must result in the segment being dropped
1070fcf59617SAndrey V. Elsukov 				 * and must not produce any response back
1071fcf59617SAndrey V. Elsukov 				 * to the sender.
1072fcf59617SAndrey V. Elsukov 				 */
1073fcf59617SAndrey V. Elsukov 				goto dropunlock;
1074fcf59617SAndrey V. Elsukov 			} else if (rstreason == 0) {
10754195b4afSPaul Traina 				/*
1076d88eb465SGleb Smirnoff 				 * No syncache entry, or ACK was not for our
1077d88eb465SGleb Smirnoff 				 * SYN/ACK.  Do our protection against double
1078d88eb465SGleb Smirnoff 				 * ACK.  If peer sent us 2 ACKs, then for the
1079d88eb465SGleb Smirnoff 				 * first one syncache_expand() successfully
1080d88eb465SGleb Smirnoff 				 * converted syncache entry into a socket,
1081d88eb465SGleb Smirnoff 				 * while we were waiting on the inpcb lock.  We
1082d88eb465SGleb Smirnoff 				 * don't want to sent RST for the second ACK,
1083d88eb465SGleb Smirnoff 				 * so we perform second lookup without wildcard
1084d88eb465SGleb Smirnoff 				 * match, hoping to find the new socket.  If
1085d88eb465SGleb Smirnoff 				 * the ACK is stray indeed, rstreason would
1086d88eb465SGleb Smirnoff 				 * hint the above code that the lookup was a
1087d88eb465SGleb Smirnoff 				 * second attempt.
1088d88eb465SGleb Smirnoff 				 *
10898d573cc1SAndre Oppermann 				 * NB: syncache did its own logging
10908d573cc1SAndre Oppermann 				 * of the failure cause.
10914195b4afSPaul Traina 				 */
1092d88eb465SGleb Smirnoff 				INP_WUNLOCK(inp);
1093be2ac88cSJonathan Lemon 				rstreason = BANDLIM_RST_OPENPORT;
1094d88eb465SGleb Smirnoff 				lookupflag &= ~INPLOOKUP_WILDCARD;
1095d88eb465SGleb Smirnoff 				goto findpcb;
1096be2ac88cSJonathan Lemon 			}
109709c305ebSPatrick Kelsey tfo_socket_result:
1098f76fcf6dSJeffrey Hsu 			if (so == NULL) {
1099be2ac88cSJonathan Lemon 				/*
1100e207f800SAndre Oppermann 				 * We completed the 3-way handshake
1101e207f800SAndre Oppermann 				 * but could not allocate a socket
1102e207f800SAndre Oppermann 				 * either due to memory shortage,
1103e207f800SAndre Oppermann 				 * listen queue length limits or
1104a160e630SAndre Oppermann 				 * global socket limits.  Send RST
1105a160e630SAndre Oppermann 				 * or wait and have the remote end
1106a160e630SAndre Oppermann 				 * retransmit the ACK for another
1107a160e630SAndre Oppermann 				 * try.
1108be2ac88cSJonathan Lemon 				 */
1109a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1110a160e630SAndre Oppermann 					log(LOG_DEBUG, "%s; %s: Listen socket: "
11118d573cc1SAndre Oppermann 					    "Socket allocation failed due to "
11128d573cc1SAndre Oppermann 					    "limits or memory shortage, %s\n",
1113ac957cd2SJulian Elischer 					    s, __func__,
1114ac957cd2SJulian Elischer 					    V_tcp_sc_rst_sock_fail ?
1115ac957cd2SJulian Elischer 					    "sending RST" : "try again");
1116603724d3SBjoern A. Zeeb 				if (V_tcp_sc_rst_sock_fail) {
1117e207f800SAndre Oppermann 					rstreason = BANDLIM_UNLIMITED;
1118e207f800SAndre Oppermann 					goto dropwithreset;
1119a160e630SAndre Oppermann 				} else
1120a160e630SAndre Oppermann 					goto dropunlock;
1121f76fcf6dSJeffrey Hsu 			}
1122be2ac88cSJonathan Lemon 			/*
1123be2ac88cSJonathan Lemon 			 * Socket is created in state SYN_RECEIVED.
11248d573cc1SAndre Oppermann 			 * Unlock the listen socket, lock the newly
11258d573cc1SAndre Oppermann 			 * created socket and update the tp variable.
112608d9c920SGleb Smirnoff 			 * If we came here via jump to tfo_socket_result,
112708d9c920SGleb Smirnoff 			 * then listening socket is read-locked.
1128be2ac88cSJonathan Lemon 			 */
112908d9c920SGleb Smirnoff 			INP_UNLOCK(inp);	/* listen socket */
1130be2ac88cSJonathan Lemon 			inp = sotoinpcb(so);
1131ff9b006dSJulien Charbon 			/*
1132ff9b006dSJulien Charbon 			 * New connection inpcb is already locked by
1133ff9b006dSJulien Charbon 			 * syncache_expand().
1134ff9b006dSJulien Charbon 			 */
1135ff9b006dSJulien Charbon 			INP_WLOCK_ASSERT(inp);
1136be2ac88cSJonathan Lemon 			tp = intotcpcb(inp);
11378d573cc1SAndre Oppermann 			KASSERT(tp->t_state == TCPS_SYN_RECEIVED,
11388d573cc1SAndre Oppermann 			    ("%s: ", __func__));
1139be2ac88cSJonathan Lemon 			/*
1140302ce8d6SAndre Oppermann 			 * Process the segment and the data it
1141302ce8d6SAndre Oppermann 			 * contains.  tcp_do_segment() consumes
1142302ce8d6SAndre Oppermann 			 * the mbuf chain and unlocks the inpcb.
1143302ce8d6SAndre Oppermann 			 */
11446138da62SMichael Tuexen 			TCP_PROBE5(receive, NULL, tp, m, tp, th);
1145*35bc0bccSGleb Smirnoff 			tp->t_fb->tfb_tcp_do_segment(tp, m, th, drop_hdrlen,
1146*35bc0bccSGleb Smirnoff 			    tlen, iptos);
11478f5a8818SKevin Lo 			return (IPPROTO_DONE);
1148be2ac88cSJonathan Lemon 		}
1149a160e630SAndre Oppermann 		/*
11508d573cc1SAndre Oppermann 		 * Segment flag validation for new connection attempts:
11518d573cc1SAndre Oppermann 		 *
1152a160e630SAndre Oppermann 		 * Our (SYN|ACK) response was rejected.
1153a160e630SAndre Oppermann 		 * Check with syncache and remove entry to prevent
1154a160e630SAndre Oppermann 		 * retransmits.
115519bc77c5SAndre Oppermann 		 *
115619bc77c5SAndre Oppermann 		 * NB: syncache_chkrst does its own logging of failure
115719bc77c5SAndre Oppermann 		 * causes.
1158a160e630SAndre Oppermann 		 */
1159a160e630SAndre Oppermann 		if (thflags & TH_RST) {
11609e644c23SMichael Tuexen 			syncache_chkrst(&inc, th, m, port);
1161a160e630SAndre Oppermann 			goto dropunlock;
1162a160e630SAndre Oppermann 		}
1163a160e630SAndre Oppermann 		/*
1164a160e630SAndre Oppermann 		 * We can't do anything without SYN.
1165a160e630SAndre Oppermann 		 */
1166a160e630SAndre Oppermann 		if ((thflags & TH_SYN) == 0) {
1167a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1168a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1169773673c1SAndre Oppermann 				    "SYN is missing, segment ignored\n",
11708d573cc1SAndre Oppermann 				    s, __func__);
117178b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1172a160e630SAndre Oppermann 			goto dropunlock;
1173a160e630SAndre Oppermann 		}
1174a160e630SAndre Oppermann 		/*
1175a160e630SAndre Oppermann 		 * (SYN|ACK) is bogus on a listen socket.
1176a160e630SAndre Oppermann 		 */
1177fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1178a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1179a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
11808d573cc1SAndre Oppermann 				    "SYN|ACK invalid, segment rejected\n",
11818d573cc1SAndre Oppermann 				    s, __func__);
11829e644c23SMichael Tuexen 			syncache_badack(&inc, port);	/* XXX: Not needed! */
118378b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1184a57815efSBosko Milekic 			rstreason = BANDLIM_RST_OPENPORT;
1185a57815efSBosko Milekic 			goto dropwithreset;
11864195b4afSPaul Traina 		}
1187a160e630SAndre Oppermann 		/*
1188a160e630SAndre Oppermann 		 * If the drop_synfin option is enabled, drop all
1189a160e630SAndre Oppermann 		 * segments with both the SYN and FIN bits set.
1190a160e630SAndre Oppermann 		 * This prevents e.g. nmap from identifying the
1191a160e630SAndre Oppermann 		 * TCP/IP stack.
1192a160e630SAndre Oppermann 		 * XXX: Poor reasoning.  nmap has other methods
1193a160e630SAndre Oppermann 		 * and is constantly refining its stack detection
1194a160e630SAndre Oppermann 		 * strategies.
11958d573cc1SAndre Oppermann 		 * XXX: This is a violation of the TCP specification
1196a160e630SAndre Oppermann 		 * and was used by RFC1644.
1197a160e630SAndre Oppermann 		 */
1198603724d3SBjoern A. Zeeb 		if ((thflags & TH_FIN) && V_drop_synfin) {
1199a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1200a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1201773673c1SAndre Oppermann 				    "SYN|FIN segment ignored (based on "
12028d573cc1SAndre Oppermann 				    "sysctl setting)\n", s, __func__);
120378b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1204302ce8d6SAndre Oppermann 			goto dropunlock;
1205ebb0cbeaSPaul Traina 		}
1206be2ac88cSJonathan Lemon 		/*
1207be2ac88cSJonathan Lemon 		 * Segment's flags are (SYN) or (SYN|FIN).
1208a160e630SAndre Oppermann 		 *
1209a160e630SAndre Oppermann 		 * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored
1210a160e630SAndre Oppermann 		 * as they do not affect the state of the TCP FSM.
1211a160e630SAndre Oppermann 		 * The data pointed to by TH_URG and th_urp is ignored.
1212be2ac88cSJonathan Lemon 		 */
1213a160e630SAndre Oppermann 		KASSERT((thflags & (TH_RST|TH_ACK)) == 0,
1214a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_RST or TH_ACK set", __func__));
1215a160e630SAndre Oppermann 		KASSERT(thflags & (TH_SYN),
1216a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_SYN not set", __func__));
121708d9c920SGleb Smirnoff 		INP_RLOCK_ASSERT(inp);
121833841545SHajimu UMEMOTO #ifdef INET6
121933841545SHajimu UMEMOTO 		/*
122033841545SHajimu UMEMOTO 		 * If deprecated address is forbidden,
122133841545SHajimu UMEMOTO 		 * we do not accept SYN to deprecated interface
122233841545SHajimu UMEMOTO 		 * address to prevent any new inbound connection from
122333841545SHajimu UMEMOTO 		 * getting established.
122433841545SHajimu UMEMOTO 		 * When we do not accept SYN, we send a TCP RST,
122533841545SHajimu UMEMOTO 		 * with deprecated source address (instead of dropping
122633841545SHajimu UMEMOTO 		 * it).  We compromise it as it is much better for peer
122733841545SHajimu UMEMOTO 		 * to send a RST, and RST will be the final packet
122833841545SHajimu UMEMOTO 		 * for the exchange.
122933841545SHajimu UMEMOTO 		 *
123033841545SHajimu UMEMOTO 		 * If we do not forbid deprecated addresses, we accept
123133841545SHajimu UMEMOTO 		 * the SYN packet.  RFC2462 does not suggest dropping
123233841545SHajimu UMEMOTO 		 * SYN in this case.
123333841545SHajimu UMEMOTO 		 * If we decipher RFC2462 5.5.4, it says like this:
123433841545SHajimu UMEMOTO 		 * 1. use of deprecated addr with existing
123533841545SHajimu UMEMOTO 		 *    communication is okay - "SHOULD continue to be
123633841545SHajimu UMEMOTO 		 *    used"
123733841545SHajimu UMEMOTO 		 * 2. use of it with new communication:
123833841545SHajimu UMEMOTO 		 *   (2a) "SHOULD NOT be used if alternate address
123933841545SHajimu UMEMOTO 		 *        with sufficient scope is available"
124033841545SHajimu UMEMOTO 		 *   (2b) nothing mentioned otherwise.
124133841545SHajimu UMEMOTO 		 * Here we fall into (2b) case as we have no choice in
124233841545SHajimu UMEMOTO 		 * our source address selection - we must obey the peer.
124333841545SHajimu UMEMOTO 		 *
124433841545SHajimu UMEMOTO 		 * The wording in RFC2462 is confusing, and there are
124533841545SHajimu UMEMOTO 		 * multiple description text for deprecated address
124633841545SHajimu UMEMOTO 		 * handling - worse, they are not exactly the same.
124733841545SHajimu UMEMOTO 		 * I believe 5.5.4 is the best one, so we follow 5.5.4.
124833841545SHajimu UMEMOTO 		 */
1249603724d3SBjoern A. Zeeb 		if (isipv6 && !V_ip6_use_deprecated) {
125033841545SHajimu UMEMOTO 			struct in6_ifaddr *ia6;
125133841545SHajimu UMEMOTO 
12528268d82cSAlexander V. Chernikov 			ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false);
12538c0fec80SRobert Watson 			if (ia6 != NULL &&
125433841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
1255a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1256a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12578d573cc1SAndre Oppermann 					"Connection attempt to deprecated "
12588d573cc1SAndre Oppermann 					"IPv6 address rejected\n",
1259a160e630SAndre Oppermann 					s, __func__);
126033841545SHajimu UMEMOTO 				rstreason = BANDLIM_RST_OPENPORT;
126133841545SHajimu UMEMOTO 				goto dropwithreset;
126233841545SHajimu UMEMOTO 			}
126333841545SHajimu UMEMOTO 		}
1264b287c6c7SBjoern A. Zeeb #endif /* INET6 */
126565f28919SJesper Skriver 		/*
1266db33b3e6SAndre Oppermann 		 * Basic sanity checks on incoming SYN requests:
12678d573cc1SAndre Oppermann 		 *   Don't respond if the destination is a link layer
1268db33b3e6SAndre Oppermann 		 *	broadcast according to RFC1122 4.2.3.10, p. 104.
12698d573cc1SAndre Oppermann 		 *   If it is from this socket it must be forged.
12708d573cc1SAndre Oppermann 		 *   Don't respond if the source or destination is a
12718d573cc1SAndre Oppermann 		 *	global or subnet broad- or multicast address.
127293ec91baSCrist J. Clark 		 *   Note that it is quite possible to receive unicast
127393ec91baSCrist J. Clark 		 *	link-layer packets with a broadcast IP address. Use
127493ec91baSCrist J. Clark 		 *	in_broadcast() to find them.
1275be2ac88cSJonathan Lemon 		 */
12768d573cc1SAndre Oppermann 		if (m->m_flags & (M_BCAST|M_MCAST)) {
12778d573cc1SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
12788d573cc1SAndre Oppermann 			    log(LOG_DEBUG, "%s; %s: Listen socket: "
12798d573cc1SAndre Oppermann 				"Connection attempt from broad- or multicast "
1280773673c1SAndre Oppermann 				"link layer address ignored\n", s, __func__);
1281302ce8d6SAndre Oppermann 			goto dropunlock;
12828d573cc1SAndre Oppermann 		}
1283db33b3e6SAndre Oppermann #ifdef INET6
1284b287c6c7SBjoern A. Zeeb 		if (isipv6) {
1285db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1286a160e630SAndre Oppermann 			    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) {
1287a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1288a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12898d573cc1SAndre Oppermann 					"Connection attempt to/from self "
1290773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1291302ce8d6SAndre Oppermann 				goto dropunlock;
1292a160e630SAndre Oppermann 			}
1293be2ac88cSJonathan Lemon 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1294a160e630SAndre Oppermann 			    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
1295a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1296a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12978d573cc1SAndre Oppermann 					"Connection attempt from/to multicast "
1298773673c1SAndre Oppermann 					"address ignored\n", s, __func__);
1299302ce8d6SAndre Oppermann 				goto dropunlock;
1300a160e630SAndre Oppermann 			}
1301b287c6c7SBjoern A. Zeeb 		}
1302db33b3e6SAndre Oppermann #endif
1303b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
1304b287c6c7SBjoern A. Zeeb 		else
1305b287c6c7SBjoern A. Zeeb #endif
1306b287c6c7SBjoern A. Zeeb #ifdef INET
1307b287c6c7SBjoern A. Zeeb 		{
1308db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1309a160e630SAndre Oppermann 			    ip->ip_dst.s_addr == ip->ip_src.s_addr) {
1310a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1311a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13128d573cc1SAndre Oppermann 					"Connection attempt from/to self "
1313773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1314302ce8d6SAndre Oppermann 				goto dropunlock;
1315a160e630SAndre Oppermann 			}
1316be2ac88cSJonathan Lemon 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
1317be2ac88cSJonathan Lemon 			    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
13182ca2159fSCrist J. Clark 			    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
1319a160e630SAndre Oppermann 			    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
1320a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1321a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13228d573cc1SAndre Oppermann 					"Connection attempt from/to broad- "
1323773673c1SAndre Oppermann 					"or multicast address ignored\n",
1324a160e630SAndre Oppermann 					s, __func__);
1325302ce8d6SAndre Oppermann 				goto dropunlock;
1326c068736aSJeffrey Hsu 			}
1327a160e630SAndre Oppermann 		}
1328b287c6c7SBjoern A. Zeeb #endif
1329be2ac88cSJonathan Lemon 		/*
1330db33b3e6SAndre Oppermann 		 * SYN appears to be valid.  Create compressed TCP state
1331db33b3e6SAndre Oppermann 		 * for syncache.
1332be2ac88cSJonathan Lemon 		 */
13332b9c9984SGeorge V. Neville-Neil 		TCP_PROBE3(debug__input, tp, th, m);
1334f72167f4SAndre Oppermann 		tcp_dooptions(&to, optp, optlen, TO_SYN);
13358d5719aaSGleb Smirnoff 		if ((so = syncache_add(&inc, &to, th, inp, so, m, NULL, NULL,
13369e644c23SMichael Tuexen 		    iptos, port)) != NULL)
133709c305ebSPatrick Kelsey 			goto tfo_socket_result;
133818a75309SPatrick Kelsey 
1339be2ac88cSJonathan Lemon 		/*
13404d6e7130SAndre Oppermann 		 * Entry added to syncache and mbuf consumed.
1341a7c7f2a7SJohn Baldwin 		 * Only the listen socket is unlocked by syncache_add().
1342be2ac88cSJonathan Lemon 		 */
13438f5a8818SKevin Lo 		return (IPPROTO_DONE);
1344982c1675SAndre Oppermann 	} else if (tp->t_state == TCPS_LISTEN) {
1345982c1675SAndre Oppermann 		/*
1346982c1675SAndre Oppermann 		 * When a listen socket is torn down the SO_ACCEPTCONN
1347982c1675SAndre Oppermann 		 * flag is removed first while connections are drained
1348982c1675SAndre Oppermann 		 * from the accept queue in a unlock/lock cycle of the
1349982c1675SAndre Oppermann 		 * ACCEPT_LOCK, opening a race condition allowing a SYN
1350982c1675SAndre Oppermann 		 * attempt go through unhandled.
1351982c1675SAndre Oppermann 		 */
1352982c1675SAndre Oppermann 		goto dropunlock;
1353f76fcf6dSJeffrey Hsu 	}
1354fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1355fcf59617SAndrey V. Elsukov 	if (tp->t_flags & TF_SIGNATURE) {
1356fcf59617SAndrey V. Elsukov 		tcp_dooptions(&to, optp, optlen, thflags);
1357fcf59617SAndrey V. Elsukov 		if ((to.to_flags & TOF_SIGNATURE) == 0) {
1358fcf59617SAndrey V. Elsukov 			TCPSTAT_INC(tcps_sig_err_nosigopt);
13592903309aSAttilio Rao 			goto dropunlock;
13602903309aSAttilio Rao 		}
1361fcf59617SAndrey V. Elsukov 		if (!TCPMD5_ENABLED() ||
1362fcf59617SAndrey V. Elsukov 		    TCPMD5_INPUT(m, th, to.to_signature) != 0)
1363fcf59617SAndrey V. Elsukov 			goto dropunlock;
13642903309aSAttilio Rao 	}
13652903309aSAttilio Rao #endif
13662b9c9984SGeorge V. Neville-Neil 	TCP_PROBE5(receive, NULL, tp, m, tp, th);
136757f60867SMark Johnston 
1368302ce8d6SAndre Oppermann 	/*
13698d573cc1SAndre Oppermann 	 * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later
13708d573cc1SAndre Oppermann 	 * state.  tcp_do_segment() always consumes the mbuf chain, unlocks
13718d573cc1SAndre Oppermann 	 * the inpcb, and unlocks pcbinfo.
13721db08fbeSGleb Smirnoff 	 *
13731db08fbeSGleb Smirnoff 	 * XXXGL: in case of a pure SYN arriving on existing connection
13741db08fbeSGleb Smirnoff 	 * TCP stacks won't need to modify the PCB, they would either drop
13751db08fbeSGleb Smirnoff 	 * the segment silently, or send a challenge ACK.  However, we try
13761db08fbeSGleb Smirnoff 	 * to upgrade the lock, because calling convention for stacks is
13771db08fbeSGleb Smirnoff 	 * write-lock on PCB.  If upgrade fails, drop the SYN.
1378302ce8d6SAndre Oppermann 	 */
1379d88eb465SGleb Smirnoff 	if ((lookupflag & INPLOOKUP_RLOCKPCB) && INP_TRY_UPGRADE(inp) == 0)
13801db08fbeSGleb Smirnoff 		goto dropunlock;
13811db08fbeSGleb Smirnoff 
1382*35bc0bccSGleb Smirnoff 	tp->t_fb->tfb_tcp_do_segment(tp, m, th, drop_hdrlen, tlen, iptos);
13838f5a8818SKevin Lo 	return (IPPROTO_DONE);
1384be2ac88cSJonathan Lemon 
1385302ce8d6SAndre Oppermann dropwithreset:
13862b9c9984SGeorge V. Neville-Neil 	TCP_PROBE5(receive, NULL, tp, m, tp, th);
138757f60867SMark Johnston 
1388014ea782SRobert Watson 	if (inp != NULL) {
1389302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
139008d9c920SGleb Smirnoff 		INP_UNLOCK(inp);
1391dd8ac7f9SRobert Watson 	} else
1392014ea782SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
1393302ce8d6SAndre Oppermann 	m = NULL;	/* mbuf chain got consumed. */
1394014ea782SRobert Watson 	goto drop;
1395014ea782SRobert Watson 
1396302ce8d6SAndre Oppermann dropunlock:
139757f60867SMark Johnston 	if (m != NULL)
13982b9c9984SGeorge V. Neville-Neil 		TCP_PROBE5(receive, NULL, tp, m, tp, th);
139957f60867SMark Johnston 
14009fa198beSAndre Oppermann 	if (inp != NULL)
140108d9c920SGleb Smirnoff 		INP_UNLOCK(inp);
1402014ea782SRobert Watson 
1403302ce8d6SAndre Oppermann drop:
1404a160e630SAndre Oppermann 	if (s != NULL)
1405a160e630SAndre Oppermann 		free(s, M_TCPLOG);
1406302ce8d6SAndre Oppermann 	if (m != NULL)
1407302ce8d6SAndre Oppermann 		m_freem(m);
14088f5a8818SKevin Lo 	return (IPPROTO_DONE);
1409302ce8d6SAndre Oppermann }
1410302ce8d6SAndre Oppermann 
1411e44c1887SSteven Hartland /*
1412e44c1887SSteven Hartland  * Automatic sizing of receive socket buffer.  Often the send
1413e44c1887SSteven Hartland  * buffer size is not optimally adjusted to the actual network
1414e44c1887SSteven Hartland  * conditions at hand (delay bandwidth product).  Setting the
1415e44c1887SSteven Hartland  * buffer size too small limits throughput on links with high
1416e44c1887SSteven Hartland  * bandwidth and high delay (eg. trans-continental/oceanic links).
1417e44c1887SSteven Hartland  *
1418e44c1887SSteven Hartland  * On the receive side the socket buffer memory is only rarely
1419e44c1887SSteven Hartland  * used to any significant extent.  This allows us to be much
1420e44c1887SSteven Hartland  * more aggressive in scaling the receive socket buffer.  For
1421e44c1887SSteven Hartland  * the case that the buffer space is actually used to a large
1422e44c1887SSteven Hartland  * extent and we run out of kernel memory we can simply drop
1423e44c1887SSteven Hartland  * the new segments; TCP on the sender will just retransmit it
1424e44c1887SSteven Hartland  * later.  Setting the buffer size too big may only consume too
1425e44c1887SSteven Hartland  * much kernel memory if the application doesn't read() from
1426e44c1887SSteven Hartland  * the socket or packet loss or reordering makes use of the
1427e44c1887SSteven Hartland  * reassembly queue.
1428e44c1887SSteven Hartland  *
1429e44c1887SSteven Hartland  * The criteria to step up the receive buffer one notch are:
1430e44c1887SSteven Hartland  *  1. Application has not set receive buffer size with
1431e44c1887SSteven Hartland  *     SO_RCVBUF. Setting SO_RCVBUF clears SB_AUTOSIZE.
1432560c0586SMichael Tuexen  *  2. the number of bytes received during 1/2 of an sRTT
1433560c0586SMichael Tuexen  *     is at least 3/8 of the current socket buffer size.
1434560c0586SMichael Tuexen  *  3. receive buffer size has not hit maximal automatic size;
1435e44c1887SSteven Hartland  *
1436560c0586SMichael Tuexen  * If all of the criteria are met we increaset the socket buffer
1437560c0586SMichael Tuexen  * by a 1/2 (bounded by the max). This allows us to keep ahead
1438560c0586SMichael Tuexen  * of slow-start but also makes it so our peer never gets limited
1439560c0586SMichael Tuexen  * by our rwnd which we then open up causing a burst.
1440560c0586SMichael Tuexen  *
1441560c0586SMichael Tuexen  * This algorithm does two steps per RTT at most and only if
1442e44c1887SSteven Hartland  * we receive a bulk stream w/o packet losses or reorderings.
1443e44c1887SSteven Hartland  * Shrinking the buffer during idle times is not necessary as
1444e44c1887SSteven Hartland  * it doesn't consume any memory when idle.
1445e44c1887SSteven Hartland  *
1446e44c1887SSteven Hartland  * TODO: Only step up if the application is actually serving
1447e44c1887SSteven Hartland  * the buffer to better manage the socket buffer resources.
1448e44c1887SSteven Hartland  */
1449e44c1887SSteven Hartland int
1450e44c1887SSteven Hartland tcp_autorcvbuf(struct mbuf *m, struct tcphdr *th, struct socket *so,
1451e44c1887SSteven Hartland     struct tcpcb *tp, int tlen)
1452e44c1887SSteven Hartland {
1453e44c1887SSteven Hartland 	int newsize = 0;
1454e44c1887SSteven Hartland 
1455e44c1887SSteven Hartland 	if (V_tcp_do_autorcvbuf && (so->so_rcv.sb_flags & SB_AUTOSIZE) &&
1456e44c1887SSteven Hartland 	    tp->t_srtt != 0 && tp->rfbuf_ts != 0 &&
1457e44c1887SSteven Hartland 	    TCP_TS_TO_TICKS(tcp_ts_getticks() - tp->rfbuf_ts) >
1458560c0586SMichael Tuexen 	    ((tp->t_srtt >> TCP_RTT_SHIFT)/2)) {
1459560c0586SMichael Tuexen 		if (tp->rfbuf_cnt > ((so->so_rcv.sb_hiwat / 2)/ 4 * 3) &&
1460e44c1887SSteven Hartland 		    so->so_rcv.sb_hiwat < V_tcp_autorcvbuf_max) {
1461560c0586SMichael Tuexen 			newsize = min((so->so_rcv.sb_hiwat + (so->so_rcv.sb_hiwat/2)), V_tcp_autorcvbuf_max);
1462e44c1887SSteven Hartland 		}
1463e44c1887SSteven Hartland 		TCP_PROBE6(receive__autoresize, NULL, tp, m, tp, th, newsize);
1464e44c1887SSteven Hartland 
1465e44c1887SSteven Hartland 		/* Start over with next RTT. */
1466e44c1887SSteven Hartland 		tp->rfbuf_ts = 0;
1467e44c1887SSteven Hartland 		tp->rfbuf_cnt = 0;
1468e44c1887SSteven Hartland 	} else {
1469e44c1887SSteven Hartland 		tp->rfbuf_cnt += tlen;	/* add up */
1470e44c1887SSteven Hartland 	}
1471e44c1887SSteven Hartland 	return (newsize);
1472e44c1887SSteven Hartland }
1473e44c1887SSteven Hartland 
14749e644c23SMichael Tuexen int
14759e644c23SMichael Tuexen tcp_input(struct mbuf **mp, int *offp, int proto)
14769e644c23SMichael Tuexen {
14779e644c23SMichael Tuexen 	return(tcp_input_with_port(mp, offp, proto, 0));
14789e644c23SMichael Tuexen }
14799e644c23SMichael Tuexen 
1480c348e880SGleb Smirnoff static void
1481c348e880SGleb Smirnoff tcp_handle_wakeup(struct tcpcb *tp)
14824d0770f1SRichard Scheffenegger {
1483c348e880SGleb Smirnoff 
14849eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
1485c348e880SGleb Smirnoff 
14864d0770f1SRichard Scheffenegger 	if (tp->t_flags & TF_WAKESOR) {
14879eb0e832SGleb Smirnoff 		struct socket *so = tptosocket(tp);
1488c348e880SGleb Smirnoff 
14894d0770f1SRichard Scheffenegger 		tp->t_flags &= ~TF_WAKESOR;
1490032bf749SRichard Scheffenegger 		SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1491032bf749SRichard Scheffenegger 		sorwakeup_locked(so);
14924d0770f1SRichard Scheffenegger 	}
14934d0770f1SRichard Scheffenegger }
14944d0770f1SRichard Scheffenegger 
14954d0770f1SRichard Scheffenegger void
1496*35bc0bccSGleb Smirnoff tcp_do_segment(struct tcpcb *tp, struct mbuf *m, struct tcphdr *th,
1497*35bc0bccSGleb Smirnoff     int drop_hdrlen, int tlen, uint8_t iptos)
1498302ce8d6SAndre Oppermann {
1499f7220c48SRichard Scheffenegger 	uint16_t thflags;
1500f7220c48SRichard Scheffenegger 	int acked, ourfinisacked, needoutput = 0, sack_changed;
1501b2ade6b1SRichard Scheffenegger 	int rstreason, todrop, win, incforsyn = 0;
15023ac12506SJonathan T. Looney 	uint32_t tiwin;
15034b7b743cSLawrence Stewart 	uint16_t nsegs;
150407dacf03SAndre Oppermann 	char *s;
15059eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
1506*35bc0bccSGleb Smirnoff 	struct socket *so = tptosocket(tp);
15079eb0e832SGleb Smirnoff 	struct in_conninfo *inc = &inp->inp_inc;
1508c11a15bfSGleb Smirnoff 	struct mbuf *mfree;
1509302ce8d6SAndre Oppermann 	struct tcpopt to;
1510281a0fd4SPatrick Kelsey 	int tfo_syn;
15113c40e1d5SRichard Scheffenegger 	u_int maxseg;
1512302ce8d6SAndre Oppermann 
15131ebf4607SRichard Scheffenegger 	thflags = tcp_get_flags(th);
1514d64a46eaSLawrence Stewart 	tp->sackhint.last_sack_ack = 0;
1515021eaf79SHiren Panchasara 	sack_changed = 0;
15164b7b743cSLawrence Stewart 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
1517d40c0d47SGleb Smirnoff 
1518d40c0d47SGleb Smirnoff 	NET_EPOCH_ASSERT();
15199eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
1520679d9708SAndre Oppermann 	KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
1521679d9708SAndre Oppermann 	    __func__));
1522679d9708SAndre Oppermann 	KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
1523679d9708SAndre Oppermann 	    __func__));
1524df8bae1dSRodney W. Grimes 
152586a996e6SHiren Panchasara #ifdef TCPPCAP
152686a996e6SHiren Panchasara 	/* Save segment, if requested. */
152786a996e6SHiren Panchasara 	tcp_pcap_add(th, m, &(tp->t_inpkts));
152886a996e6SHiren Panchasara #endif
15292529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0,
15302529f56eSJonathan T. Looney 	    tlen, NULL, true);
153186a996e6SHiren Panchasara 
1532013f4df6SMichael Tuexen 	if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
1533013f4df6SMichael Tuexen 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
1534013f4df6SMichael Tuexen 			log(LOG_DEBUG, "%s; %s: "
1535013f4df6SMichael Tuexen 			    "SYN|FIN segment ignored (based on "
1536013f4df6SMichael Tuexen 			    "sysctl setting)\n", s, __func__);
1537013f4df6SMichael Tuexen 			free(s, M_TCPLOG);
1538013f4df6SMichael Tuexen 		}
1539013f4df6SMichael Tuexen 		goto drop;
1540013f4df6SMichael Tuexen 	}
1541013f4df6SMichael Tuexen 
1542df8bae1dSRodney W. Grimes 	/*
1543ebfd7534SMichael Tuexen 	 * If a segment with the ACK-bit set arrives in the SYN-SENT state
1544ebfd7534SMichael Tuexen 	 * check SEQ.ACK first.
1545ebfd7534SMichael Tuexen 	 */
1546ebfd7534SMichael Tuexen 	if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) &&
1547ebfd7534SMichael Tuexen 	    (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) {
1548ebfd7534SMichael Tuexen 		rstreason = BANDLIM_UNLIMITED;
1549d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
1550ebfd7534SMichael Tuexen 		goto dropwithreset;
1551ebfd7534SMichael Tuexen 	}
1552ebfd7534SMichael Tuexen 
1553ebfd7534SMichael Tuexen 	/*
1554df8bae1dSRodney W. Grimes 	 * Segment received on connection.
1555df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
1556e8949f74SAndre Oppermann 	 * XXX: This should be done after segment
1557e8949f74SAndre Oppermann 	 * validation to ignore broken/spoofed segs.
1558df8bae1dSRodney W. Grimes 	 */
1559cd84e78fSRandall Stewart 	if  (tp->t_idle_reduce &&
1560cd84e78fSRandall Stewart 	     (tp->snd_max == tp->snd_una) &&
1561cd84e78fSRandall Stewart 	     ((ticks - tp->t_rcvtime) >= tp->t_rxtcur))
1562cd84e78fSRandall Stewart 		cc_after_idle(tp);
15639b8b58e0SJonathan Lemon 	tp->t_rcvtime = ticks;
1564df8bae1dSRodney W. Grimes 
1565d1b07f36SRandall Stewart 	if (thflags & TH_FIN)
1566d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_FIN);
1567df8bae1dSRodney W. Grimes 	/*
1568c52102ddSHiren Panchasara 	 * Scale up the window into a 32-bit value.
1569e8949f74SAndre Oppermann 	 * For the SYN_SENT state the scale is zero.
1570464fcfbcSAndre Oppermann 	 */
1571464fcfbcSAndre Oppermann 	tiwin = th->th_win << tp->snd_scale;
1572adc56f5aSEdward Tomasz Napierala #ifdef STATS
1573adc56f5aSEdward Tomasz Napierala 	stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin);
1574adc56f5aSEdward Tomasz Napierala #endif
1575464fcfbcSAndre Oppermann 
1576464fcfbcSAndre Oppermann 	/*
1577f2512ba1SRui Paulo 	 * TCP ECN processing.
1578f2512ba1SRui Paulo 	 */
1579b1258b76SRichard Scheffenegger 	if (tcp_ecn_input_segment(tp, thflags, tlen,
1580b1258b76SRichard Scheffenegger 	    tcp_packets_this_ack(tp, th->th_ack),
1581b1258b76SRichard Scheffenegger 	    iptos))
1582dbc42409SLawrence Stewart 		cc_cong_signal(tp, th, CC_ECN);
1583f2512ba1SRui Paulo 
1584f2512ba1SRui Paulo 	/*
1585f72167f4SAndre Oppermann 	 * Parse options on any incoming segment.
1586f72167f4SAndre Oppermann 	 */
1587302ce8d6SAndre Oppermann 	tcp_dooptions(&to, (u_char *)(th + 1),
1588302ce8d6SAndre Oppermann 	    (th->th_off << 2) - sizeof(struct tcphdr),
1589302ce8d6SAndre Oppermann 	    (thflags & TH_SYN) ? TO_SYN : 0);
1590f72167f4SAndre Oppermann 
1591fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1592fcf59617SAndrey V. Elsukov 	if ((tp->t_flags & TF_SIGNATURE) != 0 &&
1593fcf59617SAndrey V. Elsukov 	    (to.to_flags & TOF_SIGNATURE) == 0) {
1594fcf59617SAndrey V. Elsukov 		TCPSTAT_INC(tcps_sig_err_sigopt);
1595fcf59617SAndrey V. Elsukov 		/* XXX: should drop? */
1596fcf59617SAndrey V. Elsukov 	}
1597fcf59617SAndrey V. Elsukov #endif
1598f72167f4SAndre Oppermann 	/*
1599f72167f4SAndre Oppermann 	 * If echoed timestamp is later than the current time,
1600bf6d304aSAndre Oppermann 	 * fall back to non RFC1323 RTT calculation.  Normalize
1601bf6d304aSAndre Oppermann 	 * timestamp if syncookies were used when this connection
1602bf6d304aSAndre Oppermann 	 * was established.
1603f72167f4SAndre Oppermann 	 */
1604bf6d304aSAndre Oppermann 	if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
1605e16fa5caSJohn-Mark Gurney 		to.to_tsecr -= tp->ts_offset;
1606d8951c8aSBjoern A. Zeeb 		if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks()))
1607f72167f4SAndre Oppermann 			to.to_tsecr = 0;
16084531b345SRichard Scheffenegger 		else if (tp->t_rxtshift == 1 &&
16094531b345SRichard Scheffenegger 			 tp->t_flags & TF_PREVVALID &&
16104531b345SRichard Scheffenegger 			 tp->t_badrxtwin != 0 &&
16114531b345SRichard Scheffenegger 			 TSTMP_LT(to.to_tsecr, tp->t_badrxtwin))
161210d20c84SMatt Macy 			cc_cong_signal(tp, th, CC_RTO_ERR);
1613bf6d304aSAndre Oppermann 	}
161407dacf03SAndre Oppermann 	/*
161597d8d152SAndre Oppermann 	 * Process options only when we get SYN/ACK back. The SYN case
161697d8d152SAndre Oppermann 	 * for incoming connections is handled in tcp_syncache.
16175396d0f8SAndre Oppermann 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN>
16185396d0f8SAndre Oppermann 	 * or <SYN,ACK>) segment itself is never scaled.
161997d8d152SAndre Oppermann 	 * XXX this is traditional behavior, may need to be cleaned up.
1620df8bae1dSRodney W. Grimes 	 */
16210a389eabSSimon L. B. Nielsen 	if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
16226e16d877SRichard Scheffenegger 		/* Handle parallel SYN for ECN */
1623f7220c48SRichard Scheffenegger 		tcp_ecn_input_parallel_syn(tp, thflags, iptos);
1624464fcfbcSAndre Oppermann 		if ((to.to_flags & TOF_SCALE) &&
16252593f858SRichard Scheffenegger 		    (tp->t_flags & TF_REQ_SCALE) &&
16262593f858SRichard Scheffenegger 		    !(tp->t_flags & TF_NOOPT)) {
1627be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_SCALE;
162802a1a643SAndre Oppermann 			tp->snd_scale = to.to_wscale;
16298e051165SRichard Scheffenegger 		} else
16308e051165SRichard Scheffenegger 			tp->t_flags &= ~TF_REQ_SCALE;
16315396d0f8SAndre Oppermann 		/*
16325396d0f8SAndre Oppermann 		 * Initial send window.  It will be updated with
16335396d0f8SAndre Oppermann 		 * the next incoming segment to the scaled value.
16345396d0f8SAndre Oppermann 		 */
16355396d0f8SAndre Oppermann 		tp->snd_wnd = th->th_win;
16368e051165SRichard Scheffenegger 		if ((to.to_flags & TOF_TS) &&
16372593f858SRichard Scheffenegger 		    (tp->t_flags & TF_REQ_TSTMP) &&
16382593f858SRichard Scheffenegger 		    !(tp->t_flags & TF_NOOPT)) {
1639be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_TSTMP;
1640be2ac88cSJonathan Lemon 			tp->ts_recent = to.to_tsval;
1641d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
16428e051165SRichard Scheffenegger 		} else
16438e051165SRichard Scheffenegger 			tp->t_flags &= ~TF_REQ_TSTMP;
1644be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_MSS)
1645be2ac88cSJonathan Lemon 			tcp_mss(tp, to.to_mss);
16463529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
16472593f858SRichard Scheffenegger 		    (!(to.to_flags & TOF_SACKPERM) ||
16482593f858SRichard Scheffenegger 		    (tp->t_flags & TF_NOOPT)))
16493529149eSAndre Oppermann 			tp->t_flags &= ~TF_SACK_PERMIT;
1650c560df6fSPatrick Kelsey 		if (IS_FASTOPEN(tp->t_flags)) {
16512593f858SRichard Scheffenegger 			if ((to.to_flags & TOF_FASTOPEN) &&
16522593f858SRichard Scheffenegger 			    !(tp->t_flags & TF_NOOPT)) {
1653a026a53aSMichael Tuexen 				uint16_t mss;
1654a026a53aSMichael Tuexen 
1655a026a53aSMichael Tuexen 				if (to.to_flags & TOF_MSS)
1656a026a53aSMichael Tuexen 					mss = to.to_mss;
1657c560df6fSPatrick Kelsey 				else
16589eb0e832SGleb Smirnoff 					if ((inp->inp_vflag & INP_IPV6) != 0)
1659a026a53aSMichael Tuexen 						mss = TCP6_MSS;
1660a026a53aSMichael Tuexen 					else
1661a026a53aSMichael Tuexen 						mss = TCP_MSS;
1662a026a53aSMichael Tuexen 				tcp_fastopen_update_cache(tp, mss,
1663a026a53aSMichael Tuexen 				    to.to_tfo_len, to.to_tfo_cookie);
1664a026a53aSMichael Tuexen 			} else
1665c560df6fSPatrick Kelsey 				tcp_fastopen_disable_path(tp);
1666c560df6fSPatrick Kelsey 		}
16676d90faf3SPaul Saab 	}
16686d90faf3SPaul Saab 
1669df8bae1dSRodney W. Grimes 	/*
1670283c76c7SMichael Tuexen 	 * If timestamps were negotiated during SYN/ACK and a
1671283c76c7SMichael Tuexen 	 * segment without a timestamp is received, silently drop
1672d2b3ceddSMichael Tuexen 	 * the segment, unless it is a RST segment or missing timestamps are
1673d2b3ceddSMichael Tuexen 	 * tolerated.
1674283c76c7SMichael Tuexen 	 * See section 3.2 of RFC 7323.
16754da9052aSMichael Tuexen 	 */
16764da9052aSMichael Tuexen 	if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) {
1677d2b3ceddSMichael Tuexen 		if (((thflags & TH_RST) != 0) || V_tcp_tolerate_missing_ts) {
1678cc3c3485SMichael Tuexen 			if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
1679cc3c3485SMichael Tuexen 				log(LOG_DEBUG, "%s; %s: Timestamp missing, "
1680cc3c3485SMichael Tuexen 				    "segment processed normally\n",
1681cc3c3485SMichael Tuexen 				    s, __func__);
1682cc3c3485SMichael Tuexen 				free(s, M_TCPLOG);
1683cc3c3485SMichael Tuexen 			}
1684cc3c3485SMichael Tuexen 		} else {
16854da9052aSMichael Tuexen 			if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
16864da9052aSMichael Tuexen 				log(LOG_DEBUG, "%s; %s: Timestamp missing, "
1687283c76c7SMichael Tuexen 				    "segment silently dropped\n", s, __func__);
16884da9052aSMichael Tuexen 				free(s, M_TCPLOG);
16894da9052aSMichael Tuexen 			}
1690283c76c7SMichael Tuexen 			goto drop;
16914da9052aSMichael Tuexen 		}
1692cc3c3485SMichael Tuexen 	}
1693283c76c7SMichael Tuexen 	/*
1694283c76c7SMichael Tuexen 	 * If timestamps were not negotiated during SYN/ACK and a
169575fcd27aSMichael Tuexen 	 * segment with a timestamp is received, ignore the
1696283c76c7SMichael Tuexen 	 * timestamp and process the packet normally.
1697283c76c7SMichael Tuexen 	 * See section 3.2 of RFC 7323.
1698283c76c7SMichael Tuexen 	 */
16994da9052aSMichael Tuexen 	if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) {
17004da9052aSMichael Tuexen 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
17014da9052aSMichael Tuexen 			log(LOG_DEBUG, "%s; %s: Timestamp not expected, "
1702283c76c7SMichael Tuexen 			    "segment processed normally\n", s, __func__);
17034da9052aSMichael Tuexen 			free(s, M_TCPLOG);
17044da9052aSMichael Tuexen 		}
17054da9052aSMichael Tuexen 	}
17064da9052aSMichael Tuexen 
17074da9052aSMichael Tuexen 	/*
1708df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
1709df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
1710df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
1711df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
1712df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
1713df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
1714df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
1715df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
1716df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
1717df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
1718df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
1719df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
1720a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
1721c5ad39b9SAndre Oppermann 	 * Since we check for TCPS_ESTABLISHED first, it can only
1722a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
1723df8bae1dSRodney W. Grimes 	 */
1724df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
1725c5ad39b9SAndre Oppermann 	    th->th_seq == tp->rcv_nxt &&
1726fb59c426SYoshinobu Inoue 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1727c5ad39b9SAndre Oppermann 	    tp->snd_nxt == tp->snd_max &&
1728c5ad39b9SAndre Oppermann 	    tiwin && tiwin == tp->snd_wnd &&
1729a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
1730c28440dbSRandall Stewart 	    SEGQ_EMPTY(tp) &&
17314741bfcbSPatrick Kelsey 	    ((to.to_flags & TOF_TS) == 0 ||
1732c5ad39b9SAndre Oppermann 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) {
1733df8bae1dSRodney W. Grimes 		/*
1734df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
1735df8bae1dSRodney W. Grimes 		 * record the timestamp.
1736a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
1737a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1738df8bae1dSRodney W. Grimes 		 */
1739be2ac88cSJonathan Lemon 		if ((to.to_flags & TOF_TS) != 0 &&
1740fb59c426SYoshinobu Inoue 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1741d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
1742a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
1743df8bae1dSRodney W. Grimes 		}
1744df8bae1dSRodney W. Grimes 
1745fb59c426SYoshinobu Inoue 		if (tlen == 0) {
1746fb59c426SYoshinobu Inoue 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
1747fb59c426SYoshinobu Inoue 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
1748dbc42409SLawrence Stewart 			    !IN_RECOVERY(tp->t_flags) &&
1749fc30a251SAndre Oppermann 			    (to.to_flags & TOF_SACK) == 0 &&
1750dbc42409SLawrence Stewart 			    TAILQ_EMPTY(&tp->snd_holes)) {
1751df8bae1dSRodney W. Grimes 				/*
1752e8949f74SAndre Oppermann 				 * This is a pure ack for outstanding data.
1753df8bae1dSRodney W. Grimes 				 */
175478b50714SRobert Watson 				TCPSTAT_INC(tcps_predack);
1755252ca428SRobert Watson 
17569b8b58e0SJonathan Lemon 				/*
175710d20c84SMatt Macy 				 * "bad retransmit" recovery without timestamps.
17589b8b58e0SJonathan Lemon 				 */
175910d20c84SMatt Macy 				if ((to.to_flags & TOF_TS) == 0 &&
176010d20c84SMatt Macy 				    tp->t_rxtshift == 1 &&
1761672dc4aeSJohn Baldwin 				    tp->t_flags & TF_PREVVALID &&
17624531b345SRichard Scheffenegger 				    tp->t_badrxtwin != 0 &&
17634531b345SRichard Scheffenegger 				    TSTMP_LT(ticks, tp->t_badrxtwin)) {
1764dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_RTO_ERR);
17659b8b58e0SJonathan Lemon 				}
1766fa55172bSMatthew Dillon 
1767fa55172bSMatthew Dillon 				/*
1768fa55172bSMatthew Dillon 				 * Recalculate the transmit timer / rtt.
1769fa55172bSMatthew Dillon 				 *
1770fa55172bSMatthew Dillon 				 * Some boxes send broken timestamp replies
1771fa55172bSMatthew Dillon 				 * during the SYN+ACK phase, ignore
1772fa55172bSMatthew Dillon 				 * timestamps of 0 or we could calculate a
1773fa55172bSMatthew Dillon 				 * huge RTT and blow up the retransmit timer.
1774fa55172bSMatthew Dillon 				 */
1775fa55172bSMatthew Dillon 				if ((to.to_flags & TOF_TS) != 0 &&
1776fa55172bSMatthew Dillon 				    to.to_tsecr) {
17773ac12506SJonathan T. Looney 					uint32_t t;
1778d8951c8aSBjoern A. Zeeb 
1779d8951c8aSBjoern A. Zeeb 					t = tcp_ts_getticks() - to.to_tsecr;
1780d8951c8aSBjoern A. Zeeb 					if (!tp->t_rttlow || tp->t_rttlow > t)
1781d8951c8aSBjoern A. Zeeb 						tp->t_rttlow = t;
1782a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
1783d8951c8aSBjoern A. Zeeb 					    TCP_TS_TO_TICKS(t) + 1);
1784fa55172bSMatthew Dillon 				} else if (tp->t_rtttime &&
1785fa55172bSMatthew Dillon 				    SEQ_GT(th->th_ack, tp->t_rtseq)) {
1786eaf80179SAndre Oppermann 					if (!tp->t_rttlow ||
1787eaf80179SAndre Oppermann 					    tp->t_rttlow > ticks - tp->t_rtttime)
1788eaf80179SAndre Oppermann 						tp->t_rttlow = ticks - tp->t_rtttime;
1789c068736aSJeffrey Hsu 					tcp_xmit_timer(tp,
1790c068736aSJeffrey Hsu 							ticks - tp->t_rtttime);
1791fa55172bSMatthew Dillon 				}
1792dbc42409SLawrence Stewart 				acked = BYTES_THIS_ACK(tp, th);
179339bc9de5SLawrence Stewart 
1794bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
179539bc9de5SLawrence Stewart 				/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
179639bc9de5SLawrence Stewart 				hhook_run_tcp_est_in(tp, th, &to);
1797bd79708dSJonathan T. Looney #endif
179839bc9de5SLawrence Stewart 
17994b7b743cSLawrence Stewart 				TCPSTAT_ADD(tcps_rcvackpack, nsegs);
180078b50714SRobert Watson 				TCPSTAT_ADD(tcps_rcvackbyte, acked);
1801df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
18029d11646dSJeffrey Hsu 				if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
18039d11646dSJeffrey Hsu 				    SEQ_LEQ(th->th_ack, tp->snd_recover))
18049d11646dSJeffrey Hsu 					tp->snd_recover = th->th_ack - 1;
1805dbc42409SLawrence Stewart 
1806dbc42409SLawrence Stewart 				/*
1807dbc42409SLawrence Stewart 				 * Let the congestion control algorithm update
1808dbc42409SLawrence Stewart 				 * congestion control related information. This
1809dbc42409SLawrence Stewart 				 * typically means increasing the congestion
1810dbc42409SLawrence Stewart 				 * window.
1811dbc42409SLawrence Stewart 				 */
18124b7b743cSLawrence Stewart 				cc_ack_received(tp, th, nsegs, CC_ACK);
1813dbc42409SLawrence Stewart 
18149d11646dSJeffrey Hsu 				tp->snd_una = th->th_ack;
18151645d090SJeff Roberson 				/*
1816e8949f74SAndre Oppermann 				 * Pull snd_wl2 up to prevent seq wrap relative
18171645d090SJeff Roberson 				 * to th_ack.
18181645d090SJeff Roberson 				 */
1819cb942153SJeffrey Hsu 				tp->snd_wl2 = th->th_ack;
1820b5addd85SJeffrey Hsu 				tp->t_dupacks = 0;
1821df8bae1dSRodney W. Grimes 				m_freem(m);
1822df8bae1dSRodney W. Grimes 
1823df8bae1dSRodney W. Grimes 				/*
1824df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
1825df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
1826df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
1827df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
1828df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
1829df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
1830df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
1831e8949f74SAndre Oppermann 				 */
18322b9c9984SGeorge V. Neville-Neil 				TCP_PROBE3(debug__input, tp, th, m);
183308af8aacSRandall Stewart 				/*
183408af8aacSRandall Stewart 				 * Clear t_acktime if remote side has ACKd
183508af8aacSRandall Stewart 				 * all data in the socket buffer.
183608af8aacSRandall Stewart 				 * Otherwise, update t_acktime if we received
183708af8aacSRandall Stewart 				 * a sufficiently large ACK.
183808af8aacSRandall Stewart 				 */
183908af8aacSRandall Stewart 				if (sbavail(&so->so_snd) == 0)
184008af8aacSRandall Stewart 					tp->t_acktime = 0;
184108af8aacSRandall Stewart 				else if (acked > 1)
184208af8aacSRandall Stewart 					tp->t_acktime = ticks;
1843df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
1844b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
1845b8152ba7SAndre Oppermann 				else if (!tcp_timer_active(tp, TT_PERSIST))
1846b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT,
184708af8aacSRandall Stewart 					    TP_RXTCUR(tp));
1848032bf749SRichard Scheffenegger 				sowwakeup(so);
1849cfa6009eSGleb Smirnoff 				if (sbavail(&so->so_snd))
185040fa3e40SGleb Smirnoff 					(void) tcp_output(tp);
1851a14c749fSJonathan Lemon 				goto check_delack;
1852df8bae1dSRodney W. Grimes 			}
1853fb59c426SYoshinobu Inoue 		} else if (th->th_ack == tp->snd_una &&
1854fb59c426SYoshinobu Inoue 		    tlen <= sbspace(&so->so_rcv)) {
18556741ecf5SAndre Oppermann 			int newsize = 0;	/* automatic sockbuf scaling */
18566741ecf5SAndre Oppermann 
1857df8bae1dSRodney W. Grimes 			/*
1858252ca428SRobert Watson 			 * This is a pure, in-sequence data packet with
1859252ca428SRobert Watson 			 * nothing on the reassembly queue and we have enough
1860252ca428SRobert Watson 			 * buffer space to take it.
1861df8bae1dSRodney W. Grimes 			 */
18626d90faf3SPaul Saab 			/* Clean receiver SACK report if present */
18633529149eSAndre Oppermann 			if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks)
18646d90faf3SPaul Saab 				tcp_clean_sackreport(tp);
186578b50714SRobert Watson 			TCPSTAT_INC(tcps_preddat);
1866fb59c426SYoshinobu Inoue 			tp->rcv_nxt += tlen;
1867e854dd38SRandall Stewart 			if (tlen &&
1868e854dd38SRandall Stewart 			    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1869e854dd38SRandall Stewart 			    (tp->t_fbyte_in == 0)) {
1870e854dd38SRandall Stewart 				tp->t_fbyte_in = ticks;
1871e854dd38SRandall Stewart 				if (tp->t_fbyte_in == 0)
1872e854dd38SRandall Stewart 					tp->t_fbyte_in = 1;
1873e854dd38SRandall Stewart 				if (tp->t_fbyte_out && tp->t_fbyte_in)
1874e854dd38SRandall Stewart 					tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1875e854dd38SRandall Stewart 			}
18761645d090SJeff Roberson 			/*
18771645d090SJeff Roberson 			 * Pull snd_wl1 up to prevent seq wrap relative to
18781645d090SJeff Roberson 			 * th_seq.
18791645d090SJeff Roberson 			 */
188060ee3bb2SAndre Oppermann 			tp->snd_wl1 = th->th_seq;
18811645d090SJeff Roberson 			/*
18821645d090SJeff Roberson 			 * Pull rcv_up up to prevent seq wrap relative to
18831645d090SJeff Roberson 			 * rcv_nxt.
18841645d090SJeff Roberson 			 */
18851645d090SJeff Roberson 			tp->rcv_up = tp->rcv_nxt;
18864b7b743cSLawrence Stewart 			TCPSTAT_ADD(tcps_rcvpack, nsegs);
188778b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
18882b9c9984SGeorge V. Neville-Neil 			TCP_PROBE3(debug__input, tp, th, m);
18895d06879aSGeorge V. Neville-Neil 
1890e44c1887SSteven Hartland 			newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
18916741ecf5SAndre Oppermann 
18926741ecf5SAndre Oppermann 			/* Add data to socket buffer. */
18931e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
1894c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1895c1c36a2cSMike Silbersack 				m_freem(m);
1896c1c36a2cSMike Silbersack 			} else {
18976741ecf5SAndre Oppermann 				/*
18986741ecf5SAndre Oppermann 				 * Set new socket buffer size.
18996741ecf5SAndre Oppermann 				 * Give up when limit is reached.
19006741ecf5SAndre Oppermann 				 */
19016741ecf5SAndre Oppermann 				if (newsize)
190243283184SGleb Smirnoff 					if (!sbreserve_locked(so, SO_RCV,
190343283184SGleb Smirnoff 					    newsize, NULL))
19046741ecf5SAndre Oppermann 						so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
1905fb59c426SYoshinobu Inoue 				m_adj(m, drop_hdrlen);	/* delayed header drop */
1906651e4e6aSGleb Smirnoff 				sbappendstream_locked(&so->so_rcv, m, 0);
1907c1c36a2cSMike Silbersack 			}
1908032bf749SRichard Scheffenegger 			/* NB: sorwakeup_locked() does an implicit unlock. */
1909032bf749SRichard Scheffenegger 			sorwakeup_locked(so);
1910c1e5a6e5SAndre Oppermann 			if (DELAY_ACK(tp, tlen)) {
19113bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
1912f498eeeeSDavid Greenman 			} else {
1913e612a582SDavid Greenman 				tp->t_flags |= TF_ACKNOW;
191440fa3e40SGleb Smirnoff 				tcp_output(tp);
1915e612a582SDavid Greenman 			}
1916a14c749fSJonathan Lemon 			goto check_delack;
1917df8bae1dSRodney W. Grimes 		}
1918df8bae1dSRodney W. Grimes 	}
1919df8bae1dSRodney W. Grimes 
1920df8bae1dSRodney W. Grimes 	/*
1921df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
1922df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
1923df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
1924df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
1925df8bae1dSRodney W. Grimes 	 */
1926df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
1927df8bae1dSRodney W. Grimes 	if (win < 0)
1928df8bae1dSRodney W. Grimes 		win = 0;
192966e39adcSJohn Polstra 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1930df8bae1dSRodney W. Grimes 
1931df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1932df8bae1dSRodney W. Grimes 	/*
1933764d8cefSBill Fenner 	 * If the state is SYN_RECEIVED:
1934764d8cefSBill Fenner 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
1935764d8cefSBill Fenner 	 */
1936764d8cefSBill Fenner 	case TCPS_SYN_RECEIVED:
1937fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1938fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1939a57815efSBosko Milekic 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1940a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
1941d1b07f36SRandall Stewart 				tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
1942a57815efSBosko Milekic 				goto dropwithreset;
1943a57815efSBosko Milekic 		}
194468bd7ed1SJonathan T. Looney 		if (IS_FASTOPEN(tp->t_flags)) {
1945281a0fd4SPatrick Kelsey 			/*
1946281a0fd4SPatrick Kelsey 			 * When a TFO connection is in SYN_RECEIVED, the
1947281a0fd4SPatrick Kelsey 			 * only valid packets are the initial SYN, a
1948281a0fd4SPatrick Kelsey 			 * retransmit/copy of the initial SYN (possibly with
1949281a0fd4SPatrick Kelsey 			 * a subset of the original data), a valid ACK, a
1950281a0fd4SPatrick Kelsey 			 * FIN, or a RST.
1951281a0fd4SPatrick Kelsey 			 */
1952281a0fd4SPatrick Kelsey 			if ((thflags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) {
1953281a0fd4SPatrick Kelsey 				rstreason = BANDLIM_RST_OPENPORT;
1954d1b07f36SRandall Stewart 				tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
1955281a0fd4SPatrick Kelsey 				goto dropwithreset;
1956281a0fd4SPatrick Kelsey 			} else if (thflags & TH_SYN) {
1957281a0fd4SPatrick Kelsey 				/* non-initial SYN is ignored */
1958281a0fd4SPatrick Kelsey 				if ((tcp_timer_active(tp, TT_DELACK) ||
1959281a0fd4SPatrick Kelsey 				     tcp_timer_active(tp, TT_REXMT)))
1960281a0fd4SPatrick Kelsey 					goto drop;
1961281a0fd4SPatrick Kelsey 			} else if (!(thflags & (TH_ACK|TH_FIN|TH_RST))) {
1962281a0fd4SPatrick Kelsey 				goto drop;
1963281a0fd4SPatrick Kelsey 			}
1964281a0fd4SPatrick Kelsey 		}
1965764d8cefSBill Fenner 		break;
1966764d8cefSBill Fenner 
1967764d8cefSBill Fenner 	/*
1968df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
196998732609SMichael Tuexen 	 *	if seg contains a RST with valid ACK (SEQ.ACK has already
197098732609SMichael Tuexen 	 *	    been verified), then drop the connection.
197198732609SMichael Tuexen 	 *	if seg contains a RST without an ACK, drop the seg.
197298732609SMichael Tuexen 	 *	if seg does not contain SYN, then drop the seg.
1973df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
1974df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
1975df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
1976f2512ba1SRui Paulo 	 *	if seg contains an ECE and ECN support is enabled, the stream
1977f2512ba1SRui Paulo 	 *	    is ECN capable.
1978df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1979df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
1980df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
1981df8bae1dSRodney W. Grimes 	 */
1982df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
198357f60867SMark Johnston 		if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) {
1984d9fae5abSAndriy Gapon 			TCP_PROBE5(connect__refused, NULL, tp,
19852b9c9984SGeorge V. Neville-Neil 			    m, tp, th);
1986d1b07f36SRandall Stewart 			tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
1987df8bae1dSRodney W. Grimes 			tp = tcp_drop(tp, ECONNREFUSED);
198857f60867SMark Johnston 		}
19890ca3f933SAndre Oppermann 		if (thflags & TH_RST)
1990df8bae1dSRodney W. Grimes 			goto drop;
19910ca3f933SAndre Oppermann 		if (!(thflags & TH_SYN))
1992df8bae1dSRodney W. Grimes 			goto drop;
1993464fcfbcSAndre Oppermann 
1994fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
1995df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
1996fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1997c560df6fSPatrick Kelsey 			int tfo_partial_ack = 0;
1998c560df6fSPatrick Kelsey 
199978b50714SRobert Watson 			TCPSTAT_INC(tcps_connects);
2000845799c1SAndras Olah 			soisconnected(so);
2001c488362eSRobert Watson #ifdef MAC
200230d239bcSRobert Watson 			mac_socketpeer_set_from_mbuf(m, so);
2003c488362eSRobert Watson #endif
2004845799c1SAndras Olah 			/* Do window scaling on this connection? */
2005845799c1SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2006845799c1SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
2007845799c1SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2008845799c1SAndras Olah 			}
20093ac12506SJonathan T. Looney 			tp->rcv_adv += min(tp->rcv_wnd,
2010766282cbSJohn Baldwin 			    TCP_MAXWIN << tp->rcv_scale);
2011a0292f23SGarrett Wollman 			tp->snd_una++;		/* SYN is acked */
2012a0292f23SGarrett Wollman 			/*
2013c560df6fSPatrick Kelsey 			 * If not all the data that was sent in the TFO SYN
2014c560df6fSPatrick Kelsey 			 * has been acked, resend the remainder right away.
2015c560df6fSPatrick Kelsey 			 */
2016c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags) &&
2017c560df6fSPatrick Kelsey 			    (tp->snd_una != tp->snd_max)) {
2018c560df6fSPatrick Kelsey 				tp->snd_nxt = th->th_ack;
2019c560df6fSPatrick Kelsey 				tfo_partial_ack = 1;
2020c560df6fSPatrick Kelsey 			}
2021c560df6fSPatrick Kelsey 			/*
2022a0292f23SGarrett Wollman 			 * If there's data, delay ACK; if there's also a FIN
2023a0292f23SGarrett Wollman 			 * ACKNOW will be turned on later.
2024a0292f23SGarrett Wollman 			 */
2025c560df6fSPatrick Kelsey 			if (DELAY_ACK(tp, tlen) && tlen != 0 && !tfo_partial_ack)
2026b8152ba7SAndre Oppermann 				tcp_timer_activate(tp, TT_DELACK,
2027b8152ba7SAndre Oppermann 				    tcp_delacktime);
2028a0292f23SGarrett Wollman 			else
2029a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
2030f2512ba1SRui Paulo 
2031f7220c48SRichard Scheffenegger 			tcp_ecn_input_syn_sent(tp, thflags, iptos);
2032f2512ba1SRui Paulo 
2033a0292f23SGarrett Wollman 			/*
2034a0292f23SGarrett Wollman 			 * Received <SYN,ACK> in SYN_SENT[*] state.
2035a0292f23SGarrett Wollman 			 * Transitions:
2036a0292f23SGarrett Wollman 			 *	SYN_SENT  --> ESTABLISHED
2037a0292f23SGarrett Wollman 			 *	SYN_SENT* --> FIN_WAIT_1
2038a0292f23SGarrett Wollman 			 */
20399b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
2040a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDFIN) {
204108af8aacSRandall Stewart 				tp->t_acktime = ticks;
204257f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_1);
2043a0292f23SGarrett Wollman 				tp->t_flags &= ~TF_NEEDFIN;
2044fb59c426SYoshinobu Inoue 				thflags &= ~TH_SYN;
20457ff19458SPaul Traina 			} else {
204657f60867SMark Johnston 				tcp_state_change(tp, TCPS_ESTABLISHED);
2047d9fae5abSAndriy Gapon 				TCP_PROBE5(connect__established, NULL, tp,
20482b9c9984SGeorge V. Neville-Neil 				    m, tp, th);
2049dbc42409SLawrence Stewart 				cc_conn_init(tp);
20509077f387SGleb Smirnoff 				tcp_timer_activate(tp, TT_KEEP,
20519077f387SGleb Smirnoff 				    TP_KEEPIDLE(tp));
20527ff19458SPaul Traina 			}
2053a0292f23SGarrett Wollman 		} else {
2054a0292f23SGarrett Wollman 			/*
2055c068736aSJeffrey Hsu 			 * Received initial SYN in SYN-SENT[*] state =>
2056153edc50SHiren Panchasara 			 * simultaneous open.
2057c068736aSJeffrey Hsu 			 * If it succeeds, connection is * half-synchronized.
2058c068736aSJeffrey Hsu 			 * Otherwise, do 3-way handshake:
2059a0292f23SGarrett Wollman 			 *        SYN-SENT -> SYN-RECEIVED
2060a0292f23SGarrett Wollman 			 *        SYN-SENT* -> SYN-RECEIVED*
2061a0292f23SGarrett Wollman 			 */
2062493105c2SGleb Smirnoff 			tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN | TF_SONOTCONN);
2063b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
206457f60867SMark Johnston 			tcp_state_change(tp, TCPS_SYN_RECEIVED);
2065a0292f23SGarrett Wollman 		}
2066df8bae1dSRodney W. Grimes 
2067df8bae1dSRodney W. Grimes 		/*
2068fb59c426SYoshinobu Inoue 		 * Advance th->th_seq to correspond to first data byte.
2069df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
2070df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
2071df8bae1dSRodney W. Grimes 		 */
2072fb59c426SYoshinobu Inoue 		th->th_seq++;
2073fb59c426SYoshinobu Inoue 		if (tlen > tp->rcv_wnd) {
2074fb59c426SYoshinobu Inoue 			todrop = tlen - tp->rcv_wnd;
2075df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
2076fb59c426SYoshinobu Inoue 			tlen = tp->rcv_wnd;
2077fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
207878b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpackafterwin);
207978b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2080df8bae1dSRodney W. Grimes 		}
2081fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
2082fb59c426SYoshinobu Inoue 		tp->rcv_up = th->th_seq;
2083a0292f23SGarrett Wollman 		/*
2084a0292f23SGarrett Wollman 		 * Client side of transaction: already sent SYN and data.
2085a0292f23SGarrett Wollman 		 * If the remote host used T/TCP to validate the SYN,
2086a0292f23SGarrett Wollman 		 * our data will be ACK'd; if so, enter normal data segment
2087a0292f23SGarrett Wollman 		 * processing in the middle of step 5, ack processing.
2088a0292f23SGarrett Wollman 		 * Otherwise, goto step 6.
2089a0292f23SGarrett Wollman 		 */
2090fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK)
2091a0292f23SGarrett Wollman 			goto process_ACK;
2092c068736aSJeffrey Hsu 
2093df8bae1dSRodney W. Grimes 		goto step6;
2094df8bae1dSRodney W. Grimes 	}
2095df8bae1dSRodney W. Grimes 
2096df8bae1dSRodney W. Grimes 	/*
2097df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
209880ab7c0eSGarrett Wollman 	 * First check the RST flag and sequence number since reset segments
209980ab7c0eSGarrett Wollman 	 * are exempt from the timestamp and connection count tests.  This
210080ab7c0eSGarrett Wollman 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
210180ab7c0eSGarrett Wollman 	 * below which allowed reset segments in half the sequence space
210280ab7c0eSGarrett Wollman 	 * to fall though and be processed (which gives forged reset
210380ab7c0eSGarrett Wollman 	 * segments with a random sequence number a 50 percent chance of
210480ab7c0eSGarrett Wollman 	 * killing a connection).
210580ab7c0eSGarrett Wollman 	 * Then check timestamp, if present.
2106a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
2107df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
2108df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
2109df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
211080ab7c0eSGarrett Wollman 	 */
2111fb59c426SYoshinobu Inoue 	if (thflags & TH_RST) {
21123220a212SGleb Smirnoff 		/*
21133220a212SGleb Smirnoff 		 * RFC5961 Section 3.2
21143220a212SGleb Smirnoff 		 *
21153220a212SGleb Smirnoff 		 * - RST drops connection only if SEG.SEQ == RCV.NXT.
21163220a212SGleb Smirnoff 		 * - If RST is in window, we send challenge ACK.
21173220a212SGleb Smirnoff 		 *
21183220a212SGleb Smirnoff 		 * Note: to take into account delayed ACKs, we should
21193220a212SGleb Smirnoff 		 *   test against last_ack_sent instead of rcv_nxt.
21203220a212SGleb Smirnoff 		 * Note 2: we handle special case of closed window, not
21213220a212SGleb Smirnoff 		 *   covered by the RFC.
21223220a212SGleb Smirnoff 		 */
21233220a212SGleb Smirnoff 		if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
21243220a212SGleb Smirnoff 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
21253220a212SGleb Smirnoff 		    (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) {
21263220a212SGleb Smirnoff 			KASSERT(tp->t_state != TCPS_SYN_SENT,
21273220a212SGleb Smirnoff 			    ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p",
21283220a212SGleb Smirnoff 			    __func__, th, tp));
21293220a212SGleb Smirnoff 
21303220a212SGleb Smirnoff 			if (V_tcp_insecure_rst ||
21313220a212SGleb Smirnoff 			    tp->last_ack_sent == th->th_seq) {
21323220a212SGleb Smirnoff 				TCPSTAT_INC(tcps_drops);
21333220a212SGleb Smirnoff 				/* Drop the connection. */
21343220a212SGleb Smirnoff 				switch (tp->t_state) {
213580ab7c0eSGarrett Wollman 				case TCPS_SYN_RECEIVED:
213680ab7c0eSGarrett Wollman 					so->so_error = ECONNREFUSED;
213780ab7c0eSGarrett Wollman 					goto close;
213880ab7c0eSGarrett Wollman 				case TCPS_ESTABLISHED:
213980ab7c0eSGarrett Wollman 				case TCPS_FIN_WAIT_1:
214080ab7c0eSGarrett Wollman 				case TCPS_FIN_WAIT_2:
214180ab7c0eSGarrett Wollman 				case TCPS_CLOSE_WAIT:
21426779a1a1SMichael Tuexen 				case TCPS_CLOSING:
21436779a1a1SMichael Tuexen 				case TCPS_LAST_ACK:
214480ab7c0eSGarrett Wollman 					so->so_error = ECONNRESET;
214580ab7c0eSGarrett Wollman 				close:
21463220a212SGleb Smirnoff 					/* FALLTHROUGH */
21473220a212SGleb Smirnoff 				default:
2148d1b07f36SRandall Stewart 					tcp_log_end_status(tp, TCP_EI_STATUS_CLIENT_RST);
214980ab7c0eSGarrett Wollman 					tp = tcp_close(tp);
21503220a212SGleb Smirnoff 				}
21513220a212SGleb Smirnoff 			} else {
21523220a212SGleb Smirnoff 				TCPSTAT_INC(tcps_badrst);
21533220a212SGleb Smirnoff 				/* Send challenge ACK. */
21543220a212SGleb Smirnoff 				tcp_respond(tp, mtod(m, void *), th, m,
21553220a212SGleb Smirnoff 				    tp->rcv_nxt, tp->snd_nxt, TH_ACK);
21563220a212SGleb Smirnoff 				tp->last_ack_sent = tp->rcv_nxt;
21573220a212SGleb Smirnoff 				m = NULL;
21583220a212SGleb Smirnoff 			}
21593220a212SGleb Smirnoff 		}
21603220a212SGleb Smirnoff 		goto drop;
21613220a212SGleb Smirnoff 	}
216280ab7c0eSGarrett Wollman 
21633220a212SGleb Smirnoff 	/*
21643220a212SGleb Smirnoff 	 * RFC5961 Section 4.2
21653220a212SGleb Smirnoff 	 * Send challenge ACK for any SYN in synchronized state.
21663220a212SGleb Smirnoff 	 */
2167281a0fd4SPatrick Kelsey 	if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT &&
2168281a0fd4SPatrick Kelsey 	    tp->t_state != TCPS_SYN_RECEIVED) {
21693220a212SGleb Smirnoff 		TCPSTAT_INC(tcps_badsyn);
21703220a212SGleb Smirnoff 		if (V_tcp_insecure_syn &&
21713220a212SGleb Smirnoff 		    SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
21723220a212SGleb Smirnoff 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
2173d1b07f36SRandall Stewart 			tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
21743220a212SGleb Smirnoff 			tp = tcp_drop(tp, ECONNRESET);
21753220a212SGleb Smirnoff 			rstreason = BANDLIM_UNLIMITED;
21763220a212SGleb Smirnoff 		} else {
217783c1ec92SRichard Scheffenegger 			tcp_ecn_input_syn_sent(tp, thflags, iptos);
21783220a212SGleb Smirnoff 			/* Send challenge ACK. */
21793220a212SGleb Smirnoff 			tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt,
21803220a212SGleb Smirnoff 			    tp->snd_nxt, TH_ACK);
21813220a212SGleb Smirnoff 			tp->last_ack_sent = tp->rcv_nxt;
21823220a212SGleb Smirnoff 			m = NULL;
218380ab7c0eSGarrett Wollman 		}
218480ab7c0eSGarrett Wollman 		goto drop;
218580ab7c0eSGarrett Wollman 	}
218680ab7c0eSGarrett Wollman 
218780ab7c0eSGarrett Wollman 	/*
2188df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
2189df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
2190df8bae1dSRodney W. Grimes 	 */
2191be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
219280ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
2193df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
2194d8951c8aSBjoern A. Zeeb 		if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) {
2195df8bae1dSRodney W. Grimes 			/*
2196df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
2197df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
2198df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
2199df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
2200df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
2201df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
2202df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
2203df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
2204df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
2205df8bae1dSRodney W. Grimes 			 */
2206df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
2207df8bae1dSRodney W. Grimes 		} else {
220878b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
220978b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, tlen);
221078b50714SRobert Watson 			TCPSTAT_INC(tcps_pawsdrop);
221107fd333dSMatthew Dillon 			if (tlen)
2212df8bae1dSRodney W. Grimes 				goto dropafterack;
22131ab4789dSMatthew Dillon 			goto drop;
22141ab4789dSMatthew Dillon 		}
2215df8bae1dSRodney W. Grimes 	}
2216df8bae1dSRodney W. Grimes 
2217a0292f23SGarrett Wollman 	/*
221880ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
221980ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
222080ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
222180ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
222280ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
222380ab7c0eSGarrett Wollman 	 */
2224a57815efSBosko Milekic 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
2225a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2226d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
2227a57815efSBosko Milekic 		goto dropwithreset;
2228a57815efSBosko Milekic 	}
222980ab7c0eSGarrett Wollman 
2230fb59c426SYoshinobu Inoue 	todrop = tp->rcv_nxt - th->th_seq;
2231df8bae1dSRodney W. Grimes 	if (todrop > 0) {
2232831ad37eSXin LI 		if (thflags & TH_SYN) {
2233fb59c426SYoshinobu Inoue 			thflags &= ~TH_SYN;
2234fb59c426SYoshinobu Inoue 			th->th_seq++;
2235fb59c426SYoshinobu Inoue 			if (th->th_urp > 1)
2236fb59c426SYoshinobu Inoue 				th->th_urp--;
2237df8bae1dSRodney W. Grimes 			else
2238fb59c426SYoshinobu Inoue 				thflags &= ~TH_URG;
2239df8bae1dSRodney W. Grimes 			todrop--;
2240df8bae1dSRodney W. Grimes 		}
2241df8bae1dSRodney W. Grimes 		/*
2242dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
2243df8bae1dSRodney W. Grimes 		 */
2244fb59c426SYoshinobu Inoue 		if (todrop > tlen
2245fb59c426SYoshinobu Inoue 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
2246dac20301SGarrett Wollman 			/*
2247dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
2248dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
2249dac20301SGarrett Wollman 			 * of sequence; drop it.
2250dac20301SGarrett Wollman 			 */
2251fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
2252dac20301SGarrett Wollman 
2253df8bae1dSRodney W. Grimes 			/*
2254dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
2255dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
2256df8bae1dSRodney W. Grimes 			 */
2257dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
2258fb59c426SYoshinobu Inoue 			todrop = tlen;
225978b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
226078b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, todrop);
2261df8bae1dSRodney W. Grimes 		} else {
226278b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpartduppack);
226378b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop);
2264df8bae1dSRodney W. Grimes 		}
22655acfd95cSMichael Tuexen 		/*
22665acfd95cSMichael Tuexen 		 * DSACK - add SACK block for dropped range
22675acfd95cSMichael Tuexen 		 */
22688f63a52bSMichael Tuexen 		if ((todrop > 0) && (tp->t_flags & TF_SACK_PERMIT)) {
2269ecc5b1d1SMichael Tuexen 			tcp_update_sack_list(tp, th->th_seq,
2270ecc5b1d1SMichael Tuexen 			    th->th_seq + todrop);
22715acfd95cSMichael Tuexen 			/*
22725acfd95cSMichael Tuexen 			 * ACK now, as the next in-sequence segment
22735acfd95cSMichael Tuexen 			 * will clear the DSACK block again
22745acfd95cSMichael Tuexen 			 */
22755acfd95cSMichael Tuexen 			tp->t_flags |= TF_ACKNOW;
22765acfd95cSMichael Tuexen 		}
2277fb59c426SYoshinobu Inoue 		drop_hdrlen += todrop;	/* drop from the top afterwards */
2278fb59c426SYoshinobu Inoue 		th->th_seq += todrop;
2279fb59c426SYoshinobu Inoue 		tlen -= todrop;
2280fb59c426SYoshinobu Inoue 		if (th->th_urp > todrop)
2281fb59c426SYoshinobu Inoue 			th->th_urp -= todrop;
2282df8bae1dSRodney W. Grimes 		else {
2283fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;
2284fb59c426SYoshinobu Inoue 			th->th_urp = 0;
2285df8bae1dSRodney W. Grimes 		}
2286df8bae1dSRodney W. Grimes 	}
2287df8bae1dSRodney W. Grimes 
2288df8bae1dSRodney W. Grimes 	/*
2289df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
2290df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
2291df8bae1dSRodney W. Grimes 	 */
229274703901SGleb Smirnoff 	if ((tp->t_flags & TF_CLOSED) && tlen) {
229307dacf03SAndre Oppermann 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
229407dacf03SAndre Oppermann 			log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data "
229507dacf03SAndre Oppermann 			    "after socket was closed, "
229607dacf03SAndre Oppermann 			    "sending RST and removing tcpcb\n",
2297e31d8aa3SMike Silbersack 			    s, __func__, tcpstates[tp->t_state], tlen);
2298773673c1SAndre Oppermann 			free(s, M_TCPLOG);
2299773673c1SAndre Oppermann 		}
2300d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_DATA_A_CLOSE);
2301d1b07f36SRandall Stewart 		/* tcp_close will kill the inp pre-log the Reset */
2302d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_SERVER_RST);
2303df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
230478b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvafterclose);
2305a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
2306df8bae1dSRodney W. Grimes 		goto dropwithreset;
2307df8bae1dSRodney W. Grimes 	}
2308df8bae1dSRodney W. Grimes 
2309df8bae1dSRodney W. Grimes 	/*
2310df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
2311df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
2312df8bae1dSRodney W. Grimes 	 */
2313fb59c426SYoshinobu Inoue 	todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
2314df8bae1dSRodney W. Grimes 	if (todrop > 0) {
231578b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvpackafterwin);
2316fb59c426SYoshinobu Inoue 		if (todrop >= tlen) {
231778b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen);
2318df8bae1dSRodney W. Grimes 			/*
2319df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
2320df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
2321df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
2322df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
2323df8bae1dSRodney W. Grimes 			 * and ack.
2324df8bae1dSRodney W. Grimes 			 */
2325fb59c426SYoshinobu Inoue 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
2326df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
232778b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvwinprobe);
2328df8bae1dSRodney W. Grimes 			} else
2329df8bae1dSRodney W. Grimes 				goto dropafterack;
2330df8bae1dSRodney W. Grimes 		} else
233178b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2332df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
2333fb59c426SYoshinobu Inoue 		tlen -= todrop;
2334fb59c426SYoshinobu Inoue 		thflags &= ~(TH_PUSH|TH_FIN);
2335df8bae1dSRodney W. Grimes 	}
2336df8bae1dSRodney W. Grimes 
2337df8bae1dSRodney W. Grimes 	/*
2338df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
2339df8bae1dSRodney W. Grimes 	 * record its timestamp.
2340cf09195bSPaul Saab 	 * NOTE:
2341cf09195bSPaul Saab 	 * 1) That the test incorporates suggestions from the latest
2342a0292f23SGarrett Wollman 	 *    proposal of the tcplw@cray.com list (Braden 1993/04/26).
2343cf09195bSPaul Saab 	 * 2) That updating only on newer timestamps interferes with
2344cf09195bSPaul Saab 	 *    our earlier PAWS tests, so this check should be solely
2345cf09195bSPaul Saab 	 *    predicated on the sequence space of this segment.
2346cf09195bSPaul Saab 	 * 3) That we modify the segment boundary check to be
2347cf09195bSPaul Saab 	 *        Last.ACK.Sent <= SEG.SEQ + SEG.Len
2348cf09195bSPaul Saab 	 *    instead of RFC1323's
2349cf09195bSPaul Saab 	 *        Last.ACK.Sent < SEG.SEQ + SEG.Len,
2350cf09195bSPaul Saab 	 *    This modified check allows us to overcome RFC1323's
2351cf09195bSPaul Saab 	 *    limitations as described in Stevens TCP/IP Illustrated
2352cf09195bSPaul Saab 	 *    Vol. 2 p.869. In such cases, we can still calculate the
2353cf09195bSPaul Saab 	 *    RTT correctly when RCV.NXT == Last.ACK.Sent.
2354df8bae1dSRodney W. Grimes 	 */
2355be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 &&
2356cf09195bSPaul Saab 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
2357cf09195bSPaul Saab 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
2358cf09195bSPaul Saab 		((thflags & (TH_SYN|TH_FIN)) != 0))) {
2359d8951c8aSBjoern A. Zeeb 		tp->ts_recent_age = tcp_ts_getticks();
2360a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
2361df8bae1dSRodney W. Grimes 	}
2362df8bae1dSRodney W. Grimes 
2363df8bae1dSRodney W. Grimes 	/*
2364a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
2365a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
2366a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
2367a0292f23SGarrett Wollman 	 */
2368fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) == 0) {
2369a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
2370281a0fd4SPatrick Kelsey 		    (tp->t_flags & TF_NEEDSYN)) {
2371281a0fd4SPatrick Kelsey 			if (tp->t_state == TCPS_SYN_RECEIVED &&
237268bd7ed1SJonathan T. Looney 			    IS_FASTOPEN(tp->t_flags)) {
2373281a0fd4SPatrick Kelsey 				tp->snd_wnd = tiwin;
2374281a0fd4SPatrick Kelsey 				cc_conn_init(tp);
2375281a0fd4SPatrick Kelsey 			}
2376a0292f23SGarrett Wollman 			goto step6;
2377281a0fd4SPatrick Kelsey 		} else if (tp->t_flags & TF_ACKNOW)
23785e1aa279SDavid Malone 			goto dropafterack;
2379a0292f23SGarrett Wollman 		else
2380a0292f23SGarrett Wollman 			goto drop;
2381a0292f23SGarrett Wollman 	}
2382df8bae1dSRodney W. Grimes 
2383df8bae1dSRodney W. Grimes 	/*
2384df8bae1dSRodney W. Grimes 	 * Ack processing.
2385df8bae1dSRodney W. Grimes 	 */
2386df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2387df8bae1dSRodney W. Grimes 	/*
2388764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
2389764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
2390764d8cefSBill Fenner 	 * The ACK was checked above.
2391df8bae1dSRodney W. Grimes 	 */
2392df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2393a0292f23SGarrett Wollman 
239478b50714SRobert Watson 		TCPSTAT_INC(tcps_connects);
2395493105c2SGleb Smirnoff 		if (tp->t_flags & TF_SONOTCONN) {
2396493105c2SGleb Smirnoff 			/*
2397493105c2SGleb Smirnoff 			 * Usually SYN_RECEIVED had been created from a LISTEN,
2398493105c2SGleb Smirnoff 			 * and solisten_enqueue() has already marked the socket
2399493105c2SGleb Smirnoff 			 * layer as connected.  If it didn't, which can happen
2400493105c2SGleb Smirnoff 			 * only with an accept_filter(9), then the tp is marked
2401493105c2SGleb Smirnoff 			 * with TF_SONOTCONN.  The other reason for this mark
2402493105c2SGleb Smirnoff 			 * to be set is a simultaneous open, a SYN_RECEIVED
2403493105c2SGleb Smirnoff 			 * that had been created from SYN_SENT.
2404493105c2SGleb Smirnoff 			 */
2405493105c2SGleb Smirnoff 			tp->t_flags &= ~TF_SONOTCONN;
2406df8bae1dSRodney W. Grimes 			soisconnected(so);
2407e80062a2SGleb Smirnoff 		}
2408df8bae1dSRodney W. Grimes 		/* Do window scaling? */
2409df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2410df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
2411df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
2412df8bae1dSRodney W. Grimes 		}
2413116ef4d6SMichael Tuexen 		tp->snd_wnd = tiwin;
2414a0292f23SGarrett Wollman 		/*
2415a0292f23SGarrett Wollman 		 * Make transitions:
2416a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
2417a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
2418a0292f23SGarrett Wollman 		 */
24199b8b58e0SJonathan Lemon 		tp->t_starttime = ticks;
242018a75309SPatrick Kelsey 		if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) {
2421281a0fd4SPatrick Kelsey 			tcp_fastopen_decrement_counter(tp->t_tfo_pending);
2422281a0fd4SPatrick Kelsey 			tp->t_tfo_pending = NULL;
2423281a0fd4SPatrick Kelsey 		}
24248db239dcSMichael Tuexen 		if (tp->t_flags & TF_NEEDFIN) {
242508af8aacSRandall Stewart 			tp->t_acktime = ticks;
24268db239dcSMichael Tuexen 			tcp_state_change(tp, TCPS_FIN_WAIT_1);
24278db239dcSMichael Tuexen 			tp->t_flags &= ~TF_NEEDFIN;
24288db239dcSMichael Tuexen 		} else {
24298db239dcSMichael Tuexen 			tcp_state_change(tp, TCPS_ESTABLISHED);
24308db239dcSMichael Tuexen 			TCP_PROBE5(accept__established, NULL, tp,
24318db239dcSMichael Tuexen 			    m, tp, th);
2432281a0fd4SPatrick Kelsey 			/*
2433281a0fd4SPatrick Kelsey 			 * TFO connections call cc_conn_init() during SYN
2434281a0fd4SPatrick Kelsey 			 * processing.  Calling it again here for such
2435281a0fd4SPatrick Kelsey 			 * connections is not harmless as it would undo the
2436281a0fd4SPatrick Kelsey 			 * snd_cwnd reduction that occurs when a TFO SYN|ACK
2437281a0fd4SPatrick Kelsey 			 * is retransmitted.
2438281a0fd4SPatrick Kelsey 			 */
243968bd7ed1SJonathan T. Looney 			if (!IS_FASTOPEN(tp->t_flags))
2440dbc42409SLawrence Stewart 				cc_conn_init(tp);
24419077f387SGleb Smirnoff 			tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp));
24427ff19458SPaul Traina 		}
2443a0292f23SGarrett Wollman 		/*
2444b2ade6b1SRichard Scheffenegger 		 * Account for the ACK of our SYN prior to
2445b2ade6b1SRichard Scheffenegger 		 * regular ACK processing below, except for
2446b2ade6b1SRichard Scheffenegger 		 * simultaneous SYN, which is handled later.
2447b2ade6b1SRichard Scheffenegger 		 */
2448b2ade6b1SRichard Scheffenegger 		if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN))
2449b2ade6b1SRichard Scheffenegger 			incforsyn = 1;
2450b2ade6b1SRichard Scheffenegger 		/*
2451a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
2452a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
2453a0292f23SGarrett Wollman 		 */
2454032bf749SRichard Scheffenegger 		if (tlen == 0 && (thflags & TH_FIN) == 0) {
2455c28440dbSRandall Stewart 			(void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0,
2456a0292f23SGarrett Wollman 			    (struct mbuf *)0);
2457c348e880SGleb Smirnoff 			tcp_handle_wakeup(tp);
2458032bf749SRichard Scheffenegger 		}
245960ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq - 1;
246093b0017fSPhilippe Charnier 		/* FALLTHROUGH */
2461df8bae1dSRodney W. Grimes 
2462df8bae1dSRodney W. Grimes 	/*
2463df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
2464df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
2465fb59c426SYoshinobu Inoue 	 *	tp->snd_una < th->th_ack <= tp->snd_max
2466fb59c426SYoshinobu Inoue 	 * then advance tp->snd_una to th->th_ack and drop
2467df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
2468df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
2469df8bae1dSRodney W. Grimes 	 */
2470df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
2471df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
2472df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
2473df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
2474df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
2475df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
24765a53ca16SPaul Saab 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
247778b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvacktoomuch);
24785a53ca16SPaul Saab 			goto dropafterack;
24795a53ca16SPaul Saab 		}
2480c21b7b55SRichard Scheffenegger 		if (tcp_is_sack_recovery(tp, &to)) {
24810471a8c7SRichard Scheffenegger 			if (((sack_changed = tcp_sack_doack(tp, &to, th->th_ack)) != 0) &&
24820471a8c7SRichard Scheffenegger 			    (tp->t_flags & TF_LRD)) {
24830471a8c7SRichard Scheffenegger 				tcp_sack_lost_retransmission(tp, th);
24840471a8c7SRichard Scheffenegger 			}
24850471a8c7SRichard Scheffenegger 		} else
248612eeb81fSHiren Panchasara 			/*
248712eeb81fSHiren Panchasara 			 * Reset the value so that previous (valid) value
248812eeb81fSHiren Panchasara 			 * from the last ack with SACK doesn't get used.
248912eeb81fSHiren Panchasara 			 */
249012eeb81fSHiren Panchasara 			tp->sackhint.sacked_bytes = 0;
249139bc9de5SLawrence Stewart 
2492bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
249339bc9de5SLawrence Stewart 		/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
249439bc9de5SLawrence Stewart 		hhook_run_tcp_est_in(tp, th, &to);
2495bd79708dSJonathan T. Looney #endif
249639bc9de5SLawrence Stewart 
2497fb59c426SYoshinobu Inoue 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
24980c39d38dSGleb Smirnoff 			maxseg = tcp_maxseg(tp);
2499021eaf79SHiren Panchasara 			if (tlen == 0 &&
2500021eaf79SHiren Panchasara 			    (tiwin == tp->snd_wnd ||
2501021eaf79SHiren Panchasara 			    (tp->t_flags & TF_SACK_PERMIT))) {
250262b90589SPeter Wemm 				/*
250362b90589SPeter Wemm 				 * If this is the first time we've seen a
250462b90589SPeter Wemm 				 * FIN from the remote, this is not a
250562b90589SPeter Wemm 				 * duplicate and it needs to be processed
250662b90589SPeter Wemm 				 * normally.  This happens during a
250762b90589SPeter Wemm 				 * simultaneous close.
250862b90589SPeter Wemm 				 */
250962b90589SPeter Wemm 				if ((thflags & TH_FIN) &&
251062b90589SPeter Wemm 				    (TCPS_HAVERCVDFIN(tp->t_state) == 0)) {
251162b90589SPeter Wemm 					tp->t_dupacks = 0;
251262b90589SPeter Wemm 					break;
251362b90589SPeter Wemm 				}
251478b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvdupack);
2515df8bae1dSRodney W. Grimes 				/*
2516df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
2517df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
2518df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
25195f30ec9bSEitan Adler 				 * change and FIN isn't set),
25205f30ec9bSEitan Adler 				 * the ack is the biggest we've
2521df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
2522a4641f4eSPedro F. Giffuni 				 * threshold of them, assume a packet
2523df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
2524df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
2525df8bae1dSRodney W. Grimes 				 * window so we send only this one
2526df8bae1dSRodney W. Grimes 				 * packet.
2527df8bae1dSRodney W. Grimes 				 *
2528df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
2529df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
2530df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
2531df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
2532df8bae1dSRodney W. Grimes 				 * the new ssthresh).
2533df8bae1dSRodney W. Grimes 				 *
2534df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
2535df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
2536df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
2537df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
2538df8bae1dSRodney W. Grimes 				 * network.
2539f2512ba1SRui Paulo 				 *
2540f2512ba1SRui Paulo 				 * When using TCP ECN, notify the peer that
2541f2512ba1SRui Paulo 				 * we reduced the cwnd.
2542df8bae1dSRodney W. Grimes 				 */
2543021eaf79SHiren Panchasara 				/*
2544021eaf79SHiren Panchasara 				 * Following 2 kinds of acks should not affect
2545021eaf79SHiren Panchasara 				 * dupack counting:
2546021eaf79SHiren Panchasara 				 * 1) Old acks
2547021eaf79SHiren Panchasara 				 * 2) Acks with SACK but without any new SACK
2548021eaf79SHiren Panchasara 				 * information in them. These could result from
2549021eaf79SHiren Panchasara 				 * any anomaly in the network like a switch
2550021eaf79SHiren Panchasara 				 * duplicating packets or a possible DoS attack.
2551021eaf79SHiren Panchasara 				 */
2552021eaf79SHiren Panchasara 				if (th->th_ack != tp->snd_una ||
2553c21b7b55SRichard Scheffenegger 				    (tcp_is_sack_recovery(tp, &to) &&
2554021eaf79SHiren Panchasara 				    !sack_changed))
2555021eaf79SHiren Panchasara 					break;
2556021eaf79SHiren Panchasara 				else if (!tcp_timer_active(tp, TT_REXMT))
2557df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
2558cb942153SJeffrey Hsu 				else if (++tp->t_dupacks > tcprexmtthresh ||
2559dbc42409SLawrence Stewart 				     IN_FASTRECOVERY(tp->t_flags)) {
25604b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
25614b7b743cSLawrence Stewart 					    CC_DUPACK);
25620e1d7c25SRichard Scheffenegger 					if (V_tcp_do_prr &&
256374d7fc87SRichard Scheffenegger 					    IN_FASTRECOVERY(tp->t_flags)) {
256490cca08eSRichard Scheffenegger 						tcp_do_prr_ack(tp, th, &to);
2565c21b7b55SRichard Scheffenegger 					} else if (tcp_is_sack_recovery(tp, &to) &&
2566dbc42409SLawrence Stewart 					    IN_FASTRECOVERY(tp->t_flags)) {
2567808f11b7SPaul Saab 						int awnd;
256825e6f9edSPaul Saab 
256925e6f9edSPaul Saab 						/*
257025e6f9edSPaul Saab 						 * Compute the amount of data in flight first.
25714c3972f0SHiren Panchasara 						 * We can inject new data into the pipe iff
257225e6f9edSPaul Saab 						 * we have less than 1/2 the original window's
257325e6f9edSPaul Saab 						 * worth of data in flight.
257425e6f9edSPaul Saab 						 */
2575d1de2b05SRichard Scheffenegger 						if (V_tcp_do_newsack)
257612eeb81fSHiren Panchasara 							awnd = tcp_compute_pipe(tp);
257712eeb81fSHiren Panchasara 						else
2578808f11b7SPaul Saab 							awnd = (tp->snd_nxt - tp->snd_fack) +
25790077b016SPaul Saab 								tp->sackhint.sack_bytes_rexmit;
258012eeb81fSHiren Panchasara 
2581808f11b7SPaul Saab 						if (awnd < tp->snd_ssthresh) {
25820c39d38dSGleb Smirnoff 							tp->snd_cwnd += maxseg;
258325e6f9edSPaul Saab 							if (tp->snd_cwnd > tp->snd_ssthresh)
258425e6f9edSPaul Saab 								tp->snd_cwnd = tp->snd_ssthresh;
258525e6f9edSPaul Saab 						}
258625e6f9edSPaul Saab 					} else
25870c39d38dSGleb Smirnoff 						tp->snd_cwnd += maxseg;
258840fa3e40SGleb Smirnoff 					(void) tcp_output(tp);
258946f58482SJonathan Lemon 					goto drop;
25903c40e1d5SRichard Scheffenegger 				} else if (tp->t_dupacks == tcprexmtthresh ||
25913c40e1d5SRichard Scheffenegger 					    (tp->t_flags & TF_SACK_PERMIT &&
2592d1de2b05SRichard Scheffenegger 					     V_tcp_do_newsack &&
25933c40e1d5SRichard Scheffenegger 					     tp->sackhint.sacked_bytes >
25943c40e1d5SRichard Scheffenegger 					     (tcprexmtthresh - 1) * maxseg)) {
25953c40e1d5SRichard Scheffenegger enter_recovery:
25963c40e1d5SRichard Scheffenegger 					/*
25973c40e1d5SRichard Scheffenegger 					 * Above is the RFC6675 trigger condition of
25983c40e1d5SRichard Scheffenegger 					 * more than (dupthresh-1)*maxseg sacked data.
25993c40e1d5SRichard Scheffenegger 					 * If the count of holes in the
26003c40e1d5SRichard Scheffenegger 					 * scoreboard is >= dupthresh, we could
26013c40e1d5SRichard Scheffenegger 					 * also enter loss recovery, but don't
26023c40e1d5SRichard Scheffenegger 					 * have that value readily available.
26033c40e1d5SRichard Scheffenegger 					 */
26043c40e1d5SRichard Scheffenegger 					tp->t_dupacks = tcprexmtthresh;
2605cb942153SJeffrey Hsu 					tcp_seq onxt = tp->snd_nxt;
2606a0445c2eSJayanth Vijayaraghavan 
2607a0445c2eSJayanth Vijayaraghavan 					/*
26080e1d7c25SRichard Scheffenegger 					 * If we're doing sack, or prr, check
26090e1d7c25SRichard Scheffenegger 					 * to see if we're already in sack
2610a0445c2eSJayanth Vijayaraghavan 					 * recovery. If we're not doing sack,
2611a0445c2eSJayanth Vijayaraghavan 					 * check to see if we're in newreno
2612a0445c2eSJayanth Vijayaraghavan 					 * recovery.
2613a0445c2eSJayanth Vijayaraghavan 					 */
26140e1d7c25SRichard Scheffenegger 					if (V_tcp_do_prr ||
26150e1d7c25SRichard Scheffenegger 					    (tp->t_flags & TF_SACK_PERMIT)) {
2616dbc42409SLawrence Stewart 						if (IN_FASTRECOVERY(tp->t_flags)) {
2617a0445c2eSJayanth Vijayaraghavan 							tp->t_dupacks = 0;
2618a0445c2eSJayanth Vijayaraghavan 							break;
2619a0445c2eSJayanth Vijayaraghavan 						}
2620dbc42409SLawrence Stewart 					} else {
2621a0445c2eSJayanth Vijayaraghavan 						if (SEQ_LEQ(th->th_ack,
26229d11646dSJeffrey Hsu 						    tp->snd_recover)) {
2623cb942153SJeffrey Hsu 							tp->t_dupacks = 0;
2624cb942153SJeffrey Hsu 							break;
262546f58482SJonathan Lemon 						}
2626a0445c2eSJayanth Vijayaraghavan 					}
2627dbc42409SLawrence Stewart 					/* Congestion signal before ack. */
2628dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_NDUPACK);
26294b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
26304b7b743cSLawrence Stewart 					    CC_DUPACK);
2631b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
26329b8b58e0SJonathan Lemon 					tp->t_rtttime = 0;
26330e1d7c25SRichard Scheffenegger 					if (V_tcp_do_prr) {
26340e1d7c25SRichard Scheffenegger 						/*
26350e1d7c25SRichard Scheffenegger 						 * snd_ssthresh is already updated by
26360e1d7c25SRichard Scheffenegger 						 * cc_cong_signal.
26370e1d7c25SRichard Scheffenegger 						 */
2638c21b7b55SRichard Scheffenegger 						if (tcp_is_sack_recovery(tp, &to)) {
2639e9071000SRichard Scheffenegger 							tp->sackhint.prr_delivered =
2640e9071000SRichard Scheffenegger 							    tp->sackhint.sacked_bytes;
264174d7fc87SRichard Scheffenegger 						} else {
264274d7fc87SRichard Scheffenegger 							tp->sackhint.prr_delivered =
264374d7fc87SRichard Scheffenegger 							    imin(tp->snd_max - tp->snd_una,
264474d7fc87SRichard Scheffenegger 							    imin(INT_MAX / 65536,
264574d7fc87SRichard Scheffenegger 								tp->t_dupacks) * maxseg);
264674d7fc87SRichard Scheffenegger 						}
2647bc7ee8e5SRichard Scheffenegger 						tp->sackhint.recover_fs = max(1,
2648bc7ee8e5SRichard Scheffenegger 						    tp->snd_nxt - tp->snd_una);
26490e1d7c25SRichard Scheffenegger 					}
2650c21b7b55SRichard Scheffenegger 					if (tcp_is_sack_recovery(tp, &to)) {
265178b50714SRobert Watson 						TCPSTAT_INC(
265278b50714SRobert Watson 						    tcps_sack_recovery_episode);
2653a3574665SMichael Tuexen 						tp->snd_recover = tp->snd_nxt;
26540c39d38dSGleb Smirnoff 						tp->snd_cwnd = maxseg;
265540fa3e40SGleb Smirnoff 						(void) tcp_output(tp);
26563c40e1d5SRichard Scheffenegger 						if (SEQ_GT(th->th_ack, tp->snd_una))
26573c40e1d5SRichard Scheffenegger 							goto resume_partialack;
26586d90faf3SPaul Saab 						goto drop;
26596d90faf3SPaul Saab 					}
2660fb59c426SYoshinobu Inoue 					tp->snd_nxt = th->th_ack;
26610c39d38dSGleb Smirnoff 					tp->snd_cwnd = maxseg;
266240fa3e40SGleb Smirnoff 					(void) tcp_output(tp);
266348d2549cSJeffrey Hsu 					KASSERT(tp->snd_limited <= 2,
2664302ce8d6SAndre Oppermann 					    ("%s: tp->snd_limited too big",
2665302ce8d6SAndre Oppermann 					    __func__));
2666df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
26670c39d38dSGleb Smirnoff 					     maxseg *
266848d2549cSJeffrey Hsu 					     (tp->t_dupacks - tp->snd_limited);
2669df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
2670df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
2671df8bae1dSRodney W. Grimes 					goto drop;
2672603724d3SBjoern A. Zeeb 				} else if (V_tcp_do_rfc3042) {
267362d4443fSHiren Panchasara 					/*
267462d4443fSHiren Panchasara 					 * Process first and second duplicate
267562d4443fSHiren Panchasara 					 * ACKs. Each indicates a segment
267662d4443fSHiren Panchasara 					 * leaving the network, creating room
267762d4443fSHiren Panchasara 					 * for more. Make sure we can send a
267862d4443fSHiren Panchasara 					 * packet on reception of each duplicate
267962d4443fSHiren Panchasara 					 * ACK by increasing snd_cwnd by one
268062d4443fSHiren Panchasara 					 * segment. Restore the original
268162d4443fSHiren Panchasara 					 * snd_cwnd after packet transmission.
268262d4443fSHiren Panchasara 					 */
26834b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
26844b7b743cSLawrence Stewart 					    CC_DUPACK);
26853ac12506SJonathan T. Looney 					uint32_t oldcwnd = tp->snd_cwnd;
268648d2549cSJeffrey Hsu 					tcp_seq oldsndmax = tp->snd_max;
268748d2549cSJeffrey Hsu 					u_int sent;
26885628dd08SAndre Oppermann 					int avail;
268989c02376SJeffrey Hsu 
2690582a954bSJeffrey Hsu 					KASSERT(tp->t_dupacks == 1 ||
2691582a954bSJeffrey Hsu 					    tp->t_dupacks == 2,
2692302ce8d6SAndre Oppermann 					    ("%s: dupacks not 1 or 2",
2693302ce8d6SAndre Oppermann 					    __func__));
269461a36e3dSJeffrey Hsu 					if (tp->t_dupacks == 1)
269548d2549cSJeffrey Hsu 						tp->snd_limited = 0;
269661a36e3dSJeffrey Hsu 					tp->snd_cwnd =
269761a36e3dSJeffrey Hsu 					    (tp->snd_nxt - tp->snd_una) +
269861a36e3dSJeffrey Hsu 					    (tp->t_dupacks - tp->snd_limited) *
26990c39d38dSGleb Smirnoff 					    maxseg;
27005628dd08SAndre Oppermann 					/*
27015628dd08SAndre Oppermann 					 * Only call tcp_output when there
27025ae83e0dSMichael Tuexen 					 * is new data available to be sent
27035ae83e0dSMichael Tuexen 					 * or we need to send an ACK.
27045628dd08SAndre Oppermann 					 */
27055628dd08SAndre Oppermann 					SOCKBUF_LOCK(&so->so_snd);
2706cfa6009eSGleb Smirnoff 					avail = sbavail(&so->so_snd) -
27075628dd08SAndre Oppermann 					    (tp->snd_nxt - tp->snd_una);
27085628dd08SAndre Oppermann 					SOCKBUF_UNLOCK(&so->so_snd);
27095ae83e0dSMichael Tuexen 					if (avail > 0 || tp->t_flags & TF_ACKNOW)
271040fa3e40SGleb Smirnoff 						(void) tcp_output(tp);
271148d2549cSJeffrey Hsu 					sent = tp->snd_max - oldsndmax;
27120c39d38dSGleb Smirnoff 					if (sent > maxseg) {
271389c02376SJeffrey Hsu 						KASSERT((tp->t_dupacks == 2 &&
271489c02376SJeffrey Hsu 						    tp->snd_limited == 0) ||
27150c39d38dSGleb Smirnoff 						   (sent == maxseg + 1 &&
271689c02376SJeffrey Hsu 						    tp->t_flags & TF_SENTFIN),
2717302ce8d6SAndre Oppermann 						    ("%s: sent too much",
2718302ce8d6SAndre Oppermann 						    __func__));
271948d2549cSJeffrey Hsu 						tp->snd_limited = 2;
272048d2549cSJeffrey Hsu 					} else if (sent > 0)
272148d2549cSJeffrey Hsu 						++tp->snd_limited;
2722582a954bSJeffrey Hsu 					tp->snd_cwnd = oldcwnd;
2723582a954bSJeffrey Hsu 					goto drop;
2724df8bae1dSRodney W. Grimes 				}
2725021eaf79SHiren Panchasara 			}
2726df8bae1dSRodney W. Grimes 			break;
2727021eaf79SHiren Panchasara 		} else {
2728021eaf79SHiren Panchasara 			/*
2729021eaf79SHiren Panchasara 			 * This ack is advancing the left edge, reset the
2730021eaf79SHiren Panchasara 			 * counter.
2731021eaf79SHiren Panchasara 			 */
2732021eaf79SHiren Panchasara 			tp->t_dupacks = 0;
2733021eaf79SHiren Panchasara 			/*
2734021eaf79SHiren Panchasara 			 * If this ack also has new SACK info, increment the
2735f359d6ebSRichard Scheffenegger 			 * counter as per rfc6675. The variable
2736f359d6ebSRichard Scheffenegger 			 * sack_changed tracks all changes to the SACK
2737f359d6ebSRichard Scheffenegger 			 * scoreboard, including when partial ACKs without
2738f359d6ebSRichard Scheffenegger 			 * SACK options are received, and clear the scoreboard
2739f359d6ebSRichard Scheffenegger 			 * from the left side. Such partial ACKs should not be
2740f359d6ebSRichard Scheffenegger 			 * counted as dupacks here.
2741021eaf79SHiren Panchasara 			 */
2742c21b7b55SRichard Scheffenegger 			if (tcp_is_sack_recovery(tp, &to) &&
27433c40e1d5SRichard Scheffenegger 			    sack_changed) {
2744021eaf79SHiren Panchasara 				tp->t_dupacks++;
27453c40e1d5SRichard Scheffenegger 				/* limit overhead by setting maxseg last */
27463c40e1d5SRichard Scheffenegger 				if (!IN_FASTRECOVERY(tp->t_flags) &&
27473c40e1d5SRichard Scheffenegger 				    (tp->sackhint.sacked_bytes >
27483c40e1d5SRichard Scheffenegger 				    ((tcprexmtthresh - 1) *
27493c40e1d5SRichard Scheffenegger 				    (maxseg = tcp_maxseg(tp))))) {
27503c40e1d5SRichard Scheffenegger 					goto enter_recovery;
27513c40e1d5SRichard Scheffenegger 				}
27523c40e1d5SRichard Scheffenegger 			}
2753df8bae1dSRodney W. Grimes 		}
2754cb942153SJeffrey Hsu 
27553c40e1d5SRichard Scheffenegger resume_partialack:
2756302ce8d6SAndre Oppermann 		KASSERT(SEQ_GT(th->th_ack, tp->snd_una),
2757302ce8d6SAndre Oppermann 		    ("%s: th_ack <= snd_una", __func__));
2758cb942153SJeffrey Hsu 
2759df8bae1dSRodney W. Grimes 		/*
2760df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
2761df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
2762df8bae1dSRodney W. Grimes 		 */
2763dbc42409SLawrence Stewart 		if (IN_FASTRECOVERY(tp->t_flags)) {
2764cb942153SJeffrey Hsu 			if (SEQ_LT(th->th_ack, tp->snd_recover)) {
27653529149eSAndre Oppermann 				if (tp->t_flags & TF_SACK_PERMIT)
2766eb3a59a8SRichard Scheffenegger 					if (V_tcp_do_prr && to.to_flags & TOF_SACK) {
2767eb3a59a8SRichard Scheffenegger 						tcp_timer_activate(tp, TT_REXMT, 0);
2768eb3a59a8SRichard Scheffenegger 						tp->t_rtttime = 0;
276990cca08eSRichard Scheffenegger 						tcp_do_prr_ack(tp, th, &to);
2770eb3a59a8SRichard Scheffenegger 						tp->t_flags |= TF_ACKNOW;
2771eb3a59a8SRichard Scheffenegger 						(void) tcp_output(tp);
2772eb3a59a8SRichard Scheffenegger 					} else
27736d90faf3SPaul Saab 						tcp_sack_partialack(tp, th);
27746d90faf3SPaul Saab 				else
2775c068736aSJeffrey Hsu 					tcp_newreno_partial_ack(tp, th);
2776dbc42409SLawrence Stewart 			} else
2777dbc42409SLawrence Stewart 				cc_post_recovery(tp, th);
2778b9f803b7SRichard Scheffenegger 		} else if (IN_CONGRECOVERY(tp->t_flags)) {
2779b9f803b7SRichard Scheffenegger 			if (SEQ_LT(th->th_ack, tp->snd_recover)) {
2780b9f803b7SRichard Scheffenegger 				if (V_tcp_do_prr) {
2781b9f803b7SRichard Scheffenegger 					tp->sackhint.delivered_data = BYTES_THIS_ACK(tp, th);
2782b9f803b7SRichard Scheffenegger 					tp->snd_fack = th->th_ack;
278390cca08eSRichard Scheffenegger 					tcp_do_prr_ack(tp, th, &to);
2784b9f803b7SRichard Scheffenegger 					(void) tcp_output(tp);
2785b9f803b7SRichard Scheffenegger 				}
2786b9f803b7SRichard Scheffenegger 			} else
2787b9f803b7SRichard Scheffenegger 				cc_post_recovery(tp, th);
278846f58482SJonathan Lemon 		}
2789a0292f23SGarrett Wollman 		/*
2790a0292f23SGarrett Wollman 		 * If we reach this point, ACK is not a duplicate,
2791a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
2792a0292f23SGarrett Wollman 		 */
2793a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
2794a0292f23SGarrett Wollman 			/*
2795a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
2796a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
279707e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
279807e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
279907e43e10SAndras Olah 			 * we can do window scaling.
2800a0292f23SGarrett Wollman 			 */
2801a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
2802a0292f23SGarrett Wollman 			tp->snd_una++;
280307e43e10SAndras Olah 			/* Do window scaling? */
280407e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
280507e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
280607e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2807464fcfbcSAndre Oppermann 				/* Send window already scaled. */
280807e43e10SAndras Olah 			}
2809a0292f23SGarrett Wollman 		}
2810a0292f23SGarrett Wollman 
2811a0292f23SGarrett Wollman process_ACK:
28129eb0e832SGleb Smirnoff 		INP_WLOCK_ASSERT(inp);
28137cfc6904SRobert Watson 
2814b2ade6b1SRichard Scheffenegger 		/*
2815b2ade6b1SRichard Scheffenegger 		 * Adjust for the SYN bit in sequence space,
2816b2ade6b1SRichard Scheffenegger 		 * but don't account for it in cwnd calculations.
2817b2ade6b1SRichard Scheffenegger 		 * This is for the SYN_RECEIVED, non-simultaneous
2818b2ade6b1SRichard Scheffenegger 		 * SYN case. SYN_SENT and simultaneous SYN are
2819b2ade6b1SRichard Scheffenegger 		 * treated elsewhere.
2820b2ade6b1SRichard Scheffenegger 		 */
2821b2ade6b1SRichard Scheffenegger 		if (incforsyn)
2822b2ade6b1SRichard Scheffenegger 			tp->snd_una++;
2823dbc42409SLawrence Stewart 		acked = BYTES_THIS_ACK(tp, th);
2824b8c2cd15SJonathan T. Looney 		KASSERT(acked >= 0, ("%s: acked unexepectedly negative "
2825b8c2cd15SJonathan T. Looney 		    "(tp->snd_una=%u, th->th_ack=%u, tp=%p, m=%p)", __func__,
2826b8c2cd15SJonathan T. Looney 		    tp->snd_una, th->th_ack, tp, m));
28274b7b743cSLawrence Stewart 		TCPSTAT_ADD(tcps_rcvackpack, nsegs);
282878b50714SRobert Watson 		TCPSTAT_ADD(tcps_rcvackbyte, acked);
2829df8bae1dSRodney W. Grimes 
2830df8bae1dSRodney W. Grimes 		/*
28319b8b58e0SJonathan Lemon 		 * If we just performed our first retransmit, and the ACK
28329b8b58e0SJonathan Lemon 		 * arrives within our recovery window, then it was a mistake
28339b8b58e0SJonathan Lemon 		 * to do the retransmit in the first place.  Recover our
28349b8b58e0SJonathan Lemon 		 * original cwnd and ssthresh, and proceed to transmit where
28359b8b58e0SJonathan Lemon 		 * we left off.
28369b8b58e0SJonathan Lemon 		 */
283710d20c84SMatt Macy 		if (tp->t_rxtshift == 1 &&
283810d20c84SMatt Macy 		    tp->t_flags & TF_PREVVALID &&
28394531b345SRichard Scheffenegger 		    tp->t_badrxtwin != 0 &&
28404531b345SRichard Scheffenegger 		    to.to_flags & TOF_TS &&
28414531b345SRichard Scheffenegger 		    to.to_tsecr != 0 &&
28424531b345SRichard Scheffenegger 		    TSTMP_LT(to.to_tsecr, tp->t_badrxtwin))
2843dbc42409SLawrence Stewart 			cc_cong_signal(tp, th, CC_RTO_ERR);
28449b8b58e0SJonathan Lemon 
28459b8b58e0SJonathan Lemon 		/*
2846df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
2847df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
2848df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
2849df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
2850df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
2851df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
2852df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
2853fa55172bSMatthew Dillon 		 *
2854fa55172bSMatthew Dillon 		 * Some boxes send broken timestamp replies
2855fa55172bSMatthew Dillon 		 * during the SYN+ACK phase, ignore
2856fa55172bSMatthew Dillon 		 * timestamps of 0 or we could calculate a
2857fa55172bSMatthew Dillon 		 * huge RTT and blow up the retransmit timer.
2858df8bae1dSRodney W. Grimes 		 */
2859d8951c8aSBjoern A. Zeeb 		if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) {
28603ac12506SJonathan T. Looney 			uint32_t t;
2861d8951c8aSBjoern A. Zeeb 
2862d8951c8aSBjoern A. Zeeb 			t = tcp_ts_getticks() - to.to_tsecr;
2863d8951c8aSBjoern A. Zeeb 			if (!tp->t_rttlow || tp->t_rttlow > t)
2864d8951c8aSBjoern A. Zeeb 				tp->t_rttlow = t;
2865d8951c8aSBjoern A. Zeeb 			tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1);
2866fa55172bSMatthew Dillon 		} else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
2867eaf80179SAndre Oppermann 			if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime)
2868eaf80179SAndre Oppermann 				tp->t_rttlow = ticks - tp->t_rtttime;
28699b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
2870fa55172bSMatthew Dillon 		}
2871df8bae1dSRodney W. Grimes 
287208af8aacSRandall Stewart 		SOCKBUF_LOCK(&so->so_snd);
287308af8aacSRandall Stewart 		/*
287408af8aacSRandall Stewart 		 * Clear t_acktime if remote side has ACKd all data in the
287508af8aacSRandall Stewart 		 * socket buffer and FIN (if applicable).
287608af8aacSRandall Stewart 		 * Otherwise, update t_acktime if we received a sufficiently
287708af8aacSRandall Stewart 		 * large ACK.
287808af8aacSRandall Stewart 		 */
287908af8aacSRandall Stewart 		if ((tp->t_state <= TCPS_CLOSE_WAIT &&
288008af8aacSRandall Stewart 		    acked == sbavail(&so->so_snd)) ||
288108af8aacSRandall Stewart 		    acked > sbavail(&so->so_snd))
288208af8aacSRandall Stewart 			tp->t_acktime = 0;
288308af8aacSRandall Stewart 		else if (acked > 1)
288408af8aacSRandall Stewart 			tp->t_acktime = ticks;
288508af8aacSRandall Stewart 
2886df8bae1dSRodney W. Grimes 		/*
2887df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
2888df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
2889df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
2890df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
2891df8bae1dSRodney W. Grimes 		 */
2892fb59c426SYoshinobu Inoue 		if (th->th_ack == tp->snd_max) {
2893b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
2894df8bae1dSRodney W. Grimes 			needoutput = 1;
2895b8152ba7SAndre Oppermann 		} else if (!tcp_timer_active(tp, TT_PERSIST))
289608af8aacSRandall Stewart 			tcp_timer_activate(tp, TT_REXMT, TP_RXTCUR(tp));
2897a0292f23SGarrett Wollman 
2898a0292f23SGarrett Wollman 		/*
2899a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
2900a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
2901a0292f23SGarrett Wollman 		 */
290208af8aacSRandall Stewart 		if (acked == 0) {
290308af8aacSRandall Stewart 			SOCKBUF_UNLOCK(&so->so_snd);
2904a0292f23SGarrett Wollman 			goto step6;
290508af8aacSRandall Stewart 		}
2906a0292f23SGarrett Wollman 
2907df8bae1dSRodney W. Grimes 		/*
2908dbc42409SLawrence Stewart 		 * Let the congestion control algorithm update congestion
2909dbc42409SLawrence Stewart 		 * control related information. This typically means increasing
2910dbc42409SLawrence Stewart 		 * the congestion window.
2911df8bae1dSRodney W. Grimes 		 */
29124b7b743cSLawrence Stewart 		cc_ack_received(tp, th, nsegs, CC_ACK);
2913dbc42409SLawrence Stewart 
2914cfa6009eSGleb Smirnoff 		if (acked > sbavail(&so->so_snd)) {
2915b8c2cd15SJonathan T. Looney 			if (tp->snd_wnd >= sbavail(&so->so_snd))
2916cfa6009eSGleb Smirnoff 				tp->snd_wnd -= sbavail(&so->so_snd);
2917b8c2cd15SJonathan T. Looney 			else
2918b8c2cd15SJonathan T. Looney 				tp->snd_wnd = 0;
2919c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd,
2920cfa6009eSGleb Smirnoff 			    (int)sbavail(&so->so_snd));
2921df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
2922df8bae1dSRodney W. Grimes 		} else {
2923c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd, acked);
29243ac12506SJonathan T. Looney 			if (tp->snd_wnd >= (uint32_t) acked)
292560ee3bb2SAndre Oppermann 				tp->snd_wnd -= acked;
2926b8c2cd15SJonathan T. Looney 			else
2927b8c2cd15SJonathan T. Looney 				tp->snd_wnd = 0;
2928df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
2929df8bae1dSRodney W. Grimes 		}
2930032bf749SRichard Scheffenegger 		/* NB: sowwakeup_locked() does an implicit unlock. */
2931032bf749SRichard Scheffenegger 		sowwakeup_locked(so);
2932c11a15bfSGleb Smirnoff 		m_freem(mfree);
2933e8949f74SAndre Oppermann 		/* Detect una wraparound. */
2934dbc42409SLawrence Stewart 		if (!IN_RECOVERY(tp->t_flags) &&
29359d11646dSJeffrey Hsu 		    SEQ_GT(tp->snd_una, tp->snd_recover) &&
29369d11646dSJeffrey Hsu 		    SEQ_LEQ(th->th_ack, tp->snd_recover))
29379d11646dSJeffrey Hsu 			tp->snd_recover = th->th_ack - 1;
2938dbc42409SLawrence Stewart 		/* XXXLAS: Can this be moved up into cc_post_recovery? */
2939dbc42409SLawrence Stewart 		if (IN_RECOVERY(tp->t_flags) &&
294024cb0f22SLawrence Stewart 		    SEQ_GEQ(th->th_ack, tp->snd_recover)) {
2941dbc42409SLawrence Stewart 			EXIT_RECOVERY(tp->t_flags);
294224cb0f22SLawrence Stewart 		}
2943fb59c426SYoshinobu Inoue 		tp->snd_una = th->th_ack;
29443529149eSAndre Oppermann 		if (tp->t_flags & TF_SACK_PERMIT) {
29456d90faf3SPaul Saab 			if (SEQ_GT(tp->snd_una, tp->snd_recover))
29466d90faf3SPaul Saab 				tp->snd_recover = tp->snd_una;
29476d90faf3SPaul Saab 		}
2948df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2949df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
2950df8bae1dSRodney W. Grimes 
2951df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2952df8bae1dSRodney W. Grimes 		/*
2953df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
2954df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
2955df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
2956df8bae1dSRodney W. Grimes 		 */
2957df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2958df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2959df8bae1dSRodney W. Grimes 				/*
2960df8bae1dSRodney W. Grimes 				 * If we can't receive any more
2961df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
2962df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
2963df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
2964df8bae1dSRodney W. Grimes 				 * we'll hang forever.
2965340c35deSJonathan Lemon 				 */
2966c0b99ffaSRobert Watson 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
296703e49181SSeigo Tanimura 					soisdisconnected(so);
29689077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
29699077f387SGleb Smirnoff 					    (tcp_fast_finwait2_recycle ?
29709077f387SGleb Smirnoff 					    tcp_finwait2_timeout :
29719077f387SGleb Smirnoff 					    TP_MAXIDLE(tp)));
29724cc20ab1SSeigo Tanimura 				}
297357f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_2);
2974df8bae1dSRodney W. Grimes 			}
2975df8bae1dSRodney W. Grimes 			break;
2976df8bae1dSRodney W. Grimes 
2977df8bae1dSRodney W. Grimes 		/*
2978df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
2979df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
2980df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
2981df8bae1dSRodney W. Grimes 		 * the segment.
2982df8bae1dSRodney W. Grimes 		 */
2983df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
2984df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
298511a20fb8SJeffrey Hsu 				tcp_twstart(tp);
2986340c35deSJonathan Lemon 				m_freem(m);
2987679d9708SAndre Oppermann 				return;
2988df8bae1dSRodney W. Grimes 			}
2989df8bae1dSRodney W. Grimes 			break;
2990df8bae1dSRodney W. Grimes 
2991df8bae1dSRodney W. Grimes 		/*
2992df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
2993df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
2994df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
2995df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
2996df8bae1dSRodney W. Grimes 		 */
2997df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
2998df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2999df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
3000df8bae1dSRodney W. Grimes 				goto drop;
3001df8bae1dSRodney W. Grimes 			}
3002df8bae1dSRodney W. Grimes 			break;
3003df8bae1dSRodney W. Grimes 		}
3004df8bae1dSRodney W. Grimes 	}
3005df8bae1dSRodney W. Grimes 
3006df8bae1dSRodney W. Grimes step6:
30079eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
30087cfc6904SRobert Watson 
3009df8bae1dSRodney W. Grimes 	/*
301060ee3bb2SAndre Oppermann 	 * Update window information.
301160ee3bb2SAndre Oppermann 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
3012df8bae1dSRodney W. Grimes 	 */
301360ee3bb2SAndre Oppermann 	if ((thflags & TH_ACK) &&
301460ee3bb2SAndre Oppermann 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
301560ee3bb2SAndre Oppermann 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
301660ee3bb2SAndre Oppermann 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
301760ee3bb2SAndre Oppermann 		/* keep track of pure window updates */
301860ee3bb2SAndre Oppermann 		if (tlen == 0 &&
301960ee3bb2SAndre Oppermann 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
302078b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvwinupd);
3021df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
302260ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq;
302360ee3bb2SAndre Oppermann 		tp->snd_wl2 = th->th_ack;
3024df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
3025df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
302660ee3bb2SAndre Oppermann 		needoutput = 1;
3027df8bae1dSRodney W. Grimes 	}
3028df8bae1dSRodney W. Grimes 
3029df8bae1dSRodney W. Grimes 	/*
3030df8bae1dSRodney W. Grimes 	 * Process segments with URG.
3031df8bae1dSRodney W. Grimes 	 */
3032fb59c426SYoshinobu Inoue 	if ((thflags & TH_URG) && th->th_urp &&
3033df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
3034df8bae1dSRodney W. Grimes 		/*
3035df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
3036df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
3037df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
3038df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
3039df8bae1dSRodney W. Grimes 		 */
304018ad5842SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
3041cfa6009eSGleb Smirnoff 		if (th->th_urp + sbavail(&so->so_rcv) > sb_max) {
3042fb59c426SYoshinobu Inoue 			th->th_urp = 0;			/* XXX */
3043fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;		/* XXX */
304418ad5842SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);	/* XXX */
3045df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
3046df8bae1dSRodney W. Grimes 		}
3047df8bae1dSRodney W. Grimes 		/*
3048df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
3049df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
3050df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
3051df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
3052df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
3053df8bae1dSRodney W. Grimes 		 *
3054df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
3055df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
3056df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
3057df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
3058df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
3059df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
3060df8bae1dSRodney W. Grimes 		 */
3061fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
3062fb59c426SYoshinobu Inoue 			tp->rcv_up = th->th_seq + th->th_urp;
3063cfa6009eSGleb Smirnoff 			so->so_oobmark = sbavail(&so->so_rcv) +
3064df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
3065927c5ceaSRobert Watson 			if (so->so_oobmark == 0)
3066c0b99ffaSRobert Watson 				so->so_rcv.sb_state |= SBS_RCVATMARK;
3067df8bae1dSRodney W. Grimes 			sohasoutofband(so);
3068df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
3069df8bae1dSRodney W. Grimes 		}
307018ad5842SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
3071df8bae1dSRodney W. Grimes 		/*
3072df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
3073df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
3074df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
3075df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
3076df8bae1dSRodney W. Grimes 		 */
30773ac12506SJonathan T. Looney 		if (th->th_urp <= (uint32_t)tlen &&
307830613f56SJeffrey Hsu 		    !(so->so_options & SO_OOBINLINE)) {
307930613f56SJeffrey Hsu 			/* hdr drop is delayed */
308030613f56SJeffrey Hsu 			tcp_pulloutofband(so, th, m, drop_hdrlen);
308130613f56SJeffrey Hsu 		}
3082c068736aSJeffrey Hsu 	} else {
3083df8bae1dSRodney W. Grimes 		/*
3084df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
3085df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
3086df8bae1dSRodney W. Grimes 		 * with the receive window.
3087df8bae1dSRodney W. Grimes 		 */
3088df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
3089df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
3090c068736aSJeffrey Hsu 	}
3091df8bae1dSRodney W. Grimes dodata:							/* XXX */
30929eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
30937cfc6904SRobert Watson 
3094df8bae1dSRodney W. Grimes 	/*
3095df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
3096df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
3097df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
3098df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
3099df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
3100df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
3101df8bae1dSRodney W. Grimes 	 */
3102281a0fd4SPatrick Kelsey 	tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
310368bd7ed1SJonathan T. Looney 		   IS_FASTOPEN(tp->t_flags));
3104f1ea4e41SRandall Stewart 	if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) &&
3105df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
310669e03620SPaul Saab 		tcp_seq save_start = th->th_seq;
31075acfd95cSMichael Tuexen 		tcp_seq save_rnxt  = tp->rcv_nxt;
31085acfd95cSMichael Tuexen 		int     save_tlen  = tlen;
3109fb59c426SYoshinobu Inoue 		m_adj(m, drop_hdrlen);	/* delayed header drop */
3110e4b64281SJesper Skriver 		/*
3111c068736aSJeffrey Hsu 		 * Insert segment which includes th into TCP reassembly queue
3112c068736aSJeffrey Hsu 		 * with control block tp.  Set thflags to whether reassembly now
3113c068736aSJeffrey Hsu 		 * includes a segment with FIN.  This handles the common case
3114c068736aSJeffrey Hsu 		 * inline (segment is the next to be received on an established
3115c068736aSJeffrey Hsu 		 * connection, and the queue is empty), avoiding linkage into
3116c068736aSJeffrey Hsu 		 * and removal from the queue and repetition of various
3117c068736aSJeffrey Hsu 		 * conversions.
3118c068736aSJeffrey Hsu 		 * Set DELACK for segments received in order, but ack
3119c068736aSJeffrey Hsu 		 * immediately when segments are out of order (so
3120c068736aSJeffrey Hsu 		 * fast retransmit can work).
3121e4b64281SJesper Skriver 		 */
31224741bfcbSPatrick Kelsey 		if (th->th_seq == tp->rcv_nxt &&
3123c28440dbSRandall Stewart 		    SEGQ_EMPTY(tp) &&
3124281a0fd4SPatrick Kelsey 		    (TCPS_HAVEESTABLISHED(tp->t_state) ||
3125281a0fd4SPatrick Kelsey 		     tfo_syn)) {
3126281a0fd4SPatrick Kelsey 			if (DELAY_ACK(tp, tlen) || tfo_syn)
31273bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
3128e4b64281SJesper Skriver 			else
3129e4b64281SJesper Skriver 				tp->t_flags |= TF_ACKNOW;
3130e4b64281SJesper Skriver 			tp->rcv_nxt += tlen;
3131e854dd38SRandall Stewart 			if (tlen &&
3132e854dd38SRandall Stewart 			    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
3133e854dd38SRandall Stewart 			    (tp->t_fbyte_in == 0)) {
3134e854dd38SRandall Stewart 				tp->t_fbyte_in = ticks;
3135e854dd38SRandall Stewart 				if (tp->t_fbyte_in == 0)
3136e854dd38SRandall Stewart 					tp->t_fbyte_in = 1;
3137e854dd38SRandall Stewart 				if (tp->t_fbyte_out && tp->t_fbyte_in)
3138e854dd38SRandall Stewart 					tp->t_flags2 |= TF2_FBYTES_COMPLETE;
3139e854dd38SRandall Stewart 			}
31401ebf4607SRichard Scheffenegger 			thflags = tcp_get_flags(th) & TH_FIN;
314178b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpack);
314278b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
31431e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
3144c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
3145c1c36a2cSMike Silbersack 				m_freem(m);
3146c1c36a2cSMike Silbersack 			else
3147651e4e6aSGleb Smirnoff 				sbappendstream_locked(&so->so_rcv, m, 0);
31484d0770f1SRichard Scheffenegger 			tp->t_flags |= TF_WAKESOR;
3149e4b64281SJesper Skriver 		} else {
3150e8949f74SAndre Oppermann 			/*
3151e8949f74SAndre Oppermann 			 * XXX: Due to the header drop above "th" is
3152e8949f74SAndre Oppermann 			 * theoretically invalid by now.  Fortunately
3153e8949f74SAndre Oppermann 			 * m_adj() doesn't actually frees any mbufs
3154e8949f74SAndre Oppermann 			 * when trimming from the head.
3155e8949f74SAndre Oppermann 			 */
31565acfd95cSMichael Tuexen 			tcp_seq temp = save_start;
31574747500dSRandall Stewart 
31585acfd95cSMichael Tuexen 			thflags = tcp_reass(tp, th, &temp, &tlen, m);
3159e4b64281SJesper Skriver 			tp->t_flags |= TF_ACKNOW;
3160e4b64281SJesper Skriver 		}
316140f41eceSMichael Tuexen 		if ((tp->t_flags & TF_SACK_PERMIT) &&
316240f41eceSMichael Tuexen 		    (save_tlen > 0) &&
316340f41eceSMichael Tuexen 		    TCPS_HAVEESTABLISHED(tp->t_state)) {
31646d261981SMichael Tuexen 			if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) {
3165b5a154d8SMichael Tuexen 				/*
3166b5a154d8SMichael Tuexen 				 * DSACK actually handled in the fastpath
3167b5a154d8SMichael Tuexen 				 * above.
3168b5a154d8SMichael Tuexen 				 */
3169ecc5b1d1SMichael Tuexen 				tcp_update_sack_list(tp, save_start,
3170ecc5b1d1SMichael Tuexen 				    save_start + save_tlen);
3171ecc5b1d1SMichael Tuexen 			} else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) {
3172ecc5b1d1SMichael Tuexen 				if ((tp->rcv_numsacks >= 1) &&
3173ecc5b1d1SMichael Tuexen 				    (tp->sackblks[0].end == save_start)) {
31746d261981SMichael Tuexen 					/*
31756d261981SMichael Tuexen 					 * Partial overlap, recorded at todrop
31766d261981SMichael Tuexen 					 * above.
31776d261981SMichael Tuexen 					 */
31786d261981SMichael Tuexen 					tcp_update_sack_list(tp,
31796d261981SMichael Tuexen 					    tp->sackblks[0].start,
3180ecc5b1d1SMichael Tuexen 					    tp->sackblks[0].end);
3181ecc5b1d1SMichael Tuexen 				} else {
3182ecc5b1d1SMichael Tuexen 					tcp_update_dsack_list(tp, save_start,
3183ecc5b1d1SMichael Tuexen 					    save_start + save_tlen);
3184ecc5b1d1SMichael Tuexen 				}
31856d261981SMichael Tuexen 			} else if (tlen >= save_tlen) {
3186b5a154d8SMichael Tuexen 				/* Update of sackblks. */
3187ecc5b1d1SMichael Tuexen 				tcp_update_dsack_list(tp, save_start,
3188ecc5b1d1SMichael Tuexen 				    save_start + save_tlen);
3189ecc5b1d1SMichael Tuexen 			} else if (tlen > 0) {
3190ecc5b1d1SMichael Tuexen 				tcp_update_dsack_list(tp, save_start,
3191ecc5b1d1SMichael Tuexen 				    save_start + tlen);
31925acfd95cSMichael Tuexen 			}
31935acfd95cSMichael Tuexen 		}
3194c348e880SGleb Smirnoff 		tcp_handle_wakeup(tp);
3195302ce8d6SAndre Oppermann #if 0
3196df8bae1dSRodney W. Grimes 		/*
3197df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
3198df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
3199df8bae1dSRodney W. Grimes 		 * buffer size.
3200302ce8d6SAndre Oppermann 		 * XXX: Unused.
3201df8bae1dSRodney W. Grimes 		 */
3202f701e30dSJohn Baldwin 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt))
3203df8bae1dSRodney W. Grimes 			len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
3204f701e30dSJohn Baldwin 		else
3205f701e30dSJohn Baldwin 			len = so->so_rcv.sb_hiwat;
3206302ce8d6SAndre Oppermann #endif
3207df8bae1dSRodney W. Grimes 	} else {
3208df8bae1dSRodney W. Grimes 		m_freem(m);
3209fb59c426SYoshinobu Inoue 		thflags &= ~TH_FIN;
3210df8bae1dSRodney W. Grimes 	}
3211df8bae1dSRodney W. Grimes 
3212df8bae1dSRodney W. Grimes 	/*
3213df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
3214df8bae1dSRodney W. Grimes 	 * that the connection is closing.
3215df8bae1dSRodney W. Grimes 	 */
3216fb59c426SYoshinobu Inoue 	if (thflags & TH_FIN) {
3217df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
32184d0770f1SRichard Scheffenegger 			/* The socket upcall is handled by socantrcvmore. */
3219032bf749SRichard Scheffenegger 			socantrcvmore(so);
3220a0292f23SGarrett Wollman 			/*
3221a0292f23SGarrett Wollman 			 * If connection is half-synchronized
3222d3eede9dSAndras Olah 			 * (ie NEEDSYN flag on) then delay ACK,
3223a0292f23SGarrett Wollman 			 * so it may be piggybacked when SYN is sent.
3224a0292f23SGarrett Wollman 			 * Otherwise, since we received a FIN then no
3225a0292f23SGarrett Wollman 			 * more input can be expected, send ACK now.
3226a0292f23SGarrett Wollman 			 */
32273bfd6421SJonathan Lemon 			if (tp->t_flags & TF_NEEDSYN)
32283bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
3229a0292f23SGarrett Wollman 			else
3230df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
3231df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
3232df8bae1dSRodney W. Grimes 		}
3233df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
3234df8bae1dSRodney W. Grimes 		/*
3235df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
3236df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
3237df8bae1dSRodney W. Grimes 		 */
3238df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
32399b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
32409b8b58e0SJonathan Lemon 			/* FALLTHROUGH */
3241df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
324257f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSE_WAIT);
3243df8bae1dSRodney W. Grimes 			break;
3244df8bae1dSRodney W. Grimes 
3245df8bae1dSRodney W. Grimes 		/*
3246df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
3247df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
3248df8bae1dSRodney W. Grimes 		 */
3249df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
325057f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSING);
3251df8bae1dSRodney W. Grimes 			break;
3252df8bae1dSRodney W. Grimes 
3253df8bae1dSRodney W. Grimes 		/*
3254df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
3255df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
3256df8bae1dSRodney W. Grimes 		 * standard timers.
3257df8bae1dSRodney W. Grimes 		 */
3258df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
3259340c35deSJonathan Lemon 			tcp_twstart(tp);
3260679d9708SAndre Oppermann 			return;
3261df8bae1dSRodney W. Grimes 		}
3262df8bae1dSRodney W. Grimes 	}
32632b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
3264df8bae1dSRodney W. Grimes 
3265df8bae1dSRodney W. Grimes 	/*
3266df8bae1dSRodney W. Grimes 	 * Return any desired output.
3267df8bae1dSRodney W. Grimes 	 */
3268df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
326940fa3e40SGleb Smirnoff 		(void) tcp_output(tp);
32707792ea27SJeffrey Hsu 
3271a14c749fSJonathan Lemon check_delack:
32729eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
3273252ca428SRobert Watson 
3274a14c749fSJonathan Lemon 	if (tp->t_flags & TF_DELACK) {
32753bfd6421SJonathan Lemon 		tp->t_flags &= ~TF_DELACK;
3276b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
32773bfd6421SJonathan Lemon 	}
32789eb0e832SGleb Smirnoff 	INP_WUNLOCK(inp);
3279679d9708SAndre Oppermann 	return;
3280df8bae1dSRodney W. Grimes 
3281df8bae1dSRodney W. Grimes dropafterack:
3282df8bae1dSRodney W. Grimes 	/*
3283df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
3284df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
328580ab7c0eSGarrett Wollman 	 *
328680ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
328780ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
328880ab7c0eSGarrett Wollman 	 * RST have been dropped.
328980ab7c0eSGarrett Wollman 	 *
329080ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
329180ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
329280ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
329380ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
329480ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
329580ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
3296df8bae1dSRodney W. Grimes 	 */
3297fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
3298fb59c426SYoshinobu Inoue 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
3299a57815efSBosko Milekic 	     SEQ_GT(th->th_ack, tp->snd_max)) ) {
3300a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
3301d1b07f36SRandall Stewart 		tcp_log_end_status(tp, TCP_EI_STATUS_RST_IN_FRONT);
3302a57815efSBosko Milekic 		goto dropwithreset;
3303a57815efSBosko Milekic 	}
33042b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
3305df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
330640fa3e40SGleb Smirnoff 	(void) tcp_output(tp);
33079eb0e832SGleb Smirnoff 	INP_WUNLOCK(inp);
3308d6915262SRobert Watson 	m_freem(m);
3309679d9708SAndre Oppermann 	return;
3310df8bae1dSRodney W. Grimes 
3311df8bae1dSRodney W. Grimes dropwithreset:
3312a0ca0871SRobert Watson 	if (tp != NULL) {
3313302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
33149eb0e832SGleb Smirnoff 		INP_WUNLOCK(inp);
3315dd8ac7f9SRobert Watson 	} else
3316a0ca0871SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
3317679d9708SAndre Oppermann 	return;
3318df8bae1dSRodney W. Grimes 
3319df8bae1dSRodney W. Grimes drop:
3320df8bae1dSRodney W. Grimes 	/*
3321df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
3322df8bae1dSRodney W. Grimes 	 */
33232b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
33244d0770f1SRichard Scheffenegger 	if (tp != NULL) {
33259eb0e832SGleb Smirnoff 		INP_WUNLOCK(inp);
33264d0770f1SRichard Scheffenegger 	}
3327d6915262SRobert Watson 	m_freem(m);
3328302ce8d6SAndre Oppermann }
3329302ce8d6SAndre Oppermann 
3330302ce8d6SAndre Oppermann /*
33310ca3f933SAndre Oppermann  * Issue RST and make ACK acceptable to originator of segment.
33320ca3f933SAndre Oppermann  * The mbuf must still include the original packet header.
33330ca3f933SAndre Oppermann  * tp may be NULL.
3334302ce8d6SAndre Oppermann  */
333555bceb1eSRandall Stewart void
3336302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp,
3337302ce8d6SAndre Oppermann     int tlen, int rstreason)
3338302ce8d6SAndre Oppermann {
3339b287c6c7SBjoern A. Zeeb #ifdef INET
3340302ce8d6SAndre Oppermann 	struct ip *ip;
3341b287c6c7SBjoern A. Zeeb #endif
3342302ce8d6SAndre Oppermann #ifdef INET6
3343302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6;
3344302ce8d6SAndre Oppermann #endif
33457a3244ccSRobert Watson 
33467a3244ccSRobert Watson 	if (tp != NULL) {
33479eb0e832SGleb Smirnoff 		INP_LOCK_ASSERT(tptoinpcb(tp));
33487a3244ccSRobert Watson 	}
33497a3244ccSRobert Watson 
33500ca3f933SAndre Oppermann 	/* Don't bother if destination was broadcast/multicast. */
33511ebf4607SRichard Scheffenegger 	if ((tcp_get_flags(th) & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
3352302ce8d6SAndre Oppermann 		goto drop;
3353302ce8d6SAndre Oppermann #ifdef INET6
3354302ce8d6SAndre Oppermann 	if (mtod(m, struct ip *)->ip_v == 6) {
3355302ce8d6SAndre Oppermann 		ip6 = mtod(m, struct ip6_hdr *);
3356302ce8d6SAndre Oppermann 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
3357302ce8d6SAndre Oppermann 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
3358302ce8d6SAndre Oppermann 			goto drop;
3359302ce8d6SAndre Oppermann 		/* IPv6 anycast check is done at tcp6_input() */
3360b287c6c7SBjoern A. Zeeb 	}
3361302ce8d6SAndre Oppermann #endif
3362b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3363b287c6c7SBjoern A. Zeeb 	else
3364b287c6c7SBjoern A. Zeeb #endif
3365b287c6c7SBjoern A. Zeeb #ifdef INET
3366302ce8d6SAndre Oppermann 	{
3367302ce8d6SAndre Oppermann 		ip = mtod(m, struct ip *);
3368302ce8d6SAndre Oppermann 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
3369302ce8d6SAndre Oppermann 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
3370302ce8d6SAndre Oppermann 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
3371302ce8d6SAndre Oppermann 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
3372302ce8d6SAndre Oppermann 			goto drop;
3373302ce8d6SAndre Oppermann 	}
3374b287c6c7SBjoern A. Zeeb #endif
3375302ce8d6SAndre Oppermann 
3376302ce8d6SAndre Oppermann 	/* Perform bandwidth limiting. */
3377302ce8d6SAndre Oppermann 	if (badport_bandlim(rstreason) < 0)
3378302ce8d6SAndre Oppermann 		goto drop;
3379302ce8d6SAndre Oppermann 
3380302ce8d6SAndre Oppermann 	/* tcp_respond consumes the mbuf chain. */
33811ebf4607SRichard Scheffenegger 	if (tcp_get_flags(th) & TH_ACK) {
3382302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0,
3383302ce8d6SAndre Oppermann 		    th->th_ack, TH_RST);
3384302ce8d6SAndre Oppermann 	} else {
33851ebf4607SRichard Scheffenegger 		if (tcp_get_flags(th) & TH_SYN)
3386302ce8d6SAndre Oppermann 			tlen++;
33871ebf4607SRichard Scheffenegger 		if (tcp_get_flags(th) & TH_FIN)
33884ddd5aadSMichael Tuexen 			tlen++;
3389302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
3390302ce8d6SAndre Oppermann 		    (tcp_seq)0, TH_RST|TH_ACK);
3391302ce8d6SAndre Oppermann 	}
3392302ce8d6SAndre Oppermann 	return;
3393302ce8d6SAndre Oppermann drop:
3394302ce8d6SAndre Oppermann 	m_freem(m);
3395df8bae1dSRodney W. Grimes }
3396df8bae1dSRodney W. Grimes 
3397be2ac88cSJonathan Lemon /*
3398be2ac88cSJonathan Lemon  * Parse TCP options and place in tcpopt.
3399be2ac88cSJonathan Lemon  */
340055bceb1eSRandall Stewart void
3401ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags)
3402df8bae1dSRodney W. Grimes {
3403df8bae1dSRodney W. Grimes 	int opt, optlen;
3404df8bae1dSRodney W. Grimes 
3405be2ac88cSJonathan Lemon 	to->to_flags = 0;
3406df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
3407df8bae1dSRodney W. Grimes 		opt = cp[0];
3408df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
3409df8bae1dSRodney W. Grimes 			break;
3410df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
3411df8bae1dSRodney W. Grimes 			optlen = 1;
3412df8bae1dSRodney W. Grimes 		else {
3413b474779fSJun-ichiro itojun Hagino 			if (cnt < 2)
3414b474779fSJun-ichiro itojun Hagino 				break;
3415df8bae1dSRodney W. Grimes 			optlen = cp[1];
3416b474779fSJun-ichiro itojun Hagino 			if (optlen < 2 || optlen > cnt)
3417df8bae1dSRodney W. Grimes 				break;
3418df8bae1dSRodney W. Grimes 		}
3419df8bae1dSRodney W. Grimes 		switch (opt) {
3420df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
3421df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
3422df8bae1dSRodney W. Grimes 				continue;
3423f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3424df8bae1dSRodney W. Grimes 				continue;
3425be2ac88cSJonathan Lemon 			to->to_flags |= TOF_MSS;
3426be2ac88cSJonathan Lemon 			bcopy((char *)cp + 2,
3427be2ac88cSJonathan Lemon 			    (char *)&to->to_mss, sizeof(to->to_mss));
3428fd8e4ebcSMike Barcroft 			to->to_mss = ntohs(to->to_mss);
3429df8bae1dSRodney W. Grimes 			break;
3430df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
3431df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
3432df8bae1dSRodney W. Grimes 				continue;
3433f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3434df8bae1dSRodney W. Grimes 				continue;
3435be2ac88cSJonathan Lemon 			to->to_flags |= TOF_SCALE;
343602a1a643SAndre Oppermann 			to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT);
3437df8bae1dSRodney W. Grimes 			break;
3438df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
3439df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
3440df8bae1dSRodney W. Grimes 				continue;
3441be2ac88cSJonathan Lemon 			to->to_flags |= TOF_TS;
3442a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
3443a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
3444fd8e4ebcSMike Barcroft 			to->to_tsval = ntohl(to->to_tsval);
3445a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
3446a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
3447fd8e4ebcSMike Barcroft 			to->to_tsecr = ntohl(to->to_tsecr);
3448df8bae1dSRodney W. Grimes 			break;
34491cfd4b53SBruce M Simpson 		case TCPOPT_SIGNATURE:
3450fcf59617SAndrey V. Elsukov 			/*
3451fcf59617SAndrey V. Elsukov 			 * In order to reply to a host which has set the
3452fcf59617SAndrey V. Elsukov 			 * TCP_SIGNATURE option in its initial SYN, we have
3453fcf59617SAndrey V. Elsukov 			 * to record the fact that the option was observed
3454fcf59617SAndrey V. Elsukov 			 * here for the syncache code to perform the correct
3455fcf59617SAndrey V. Elsukov 			 * response.
3456fcf59617SAndrey V. Elsukov 			 */
34571cfd4b53SBruce M Simpson 			if (optlen != TCPOLEN_SIGNATURE)
34581cfd4b53SBruce M Simpson 				continue;
3459df47e437SAndre Oppermann 			to->to_flags |= TOF_SIGNATURE;
3460df47e437SAndre Oppermann 			to->to_signature = cp + 2;
34611cfd4b53SBruce M Simpson 			break;
34626d90faf3SPaul Saab 		case TCPOPT_SACK_PERMITTED:
3463f72167f4SAndre Oppermann 			if (optlen != TCPOLEN_SACK_PERMITTED)
34646d90faf3SPaul Saab 				continue;
3465f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3466f72167f4SAndre Oppermann 				continue;
3467603724d3SBjoern A. Zeeb 			if (!V_tcp_do_sack)
3468f72167f4SAndre Oppermann 				continue;
3469fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACKPERM;
34706d90faf3SPaul Saab 			break;
34716d90faf3SPaul Saab 		case TCPOPT_SACK:
34725a53ca16SPaul Saab 			if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
34736d90faf3SPaul Saab 				continue;
3474b728e902SAndre Oppermann 			if (flags & TO_SYN)
3475b728e902SAndre Oppermann 				continue;
3476fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACK;
34775a53ca16SPaul Saab 			to->to_nsacks = (optlen - 2) / TCPOLEN_SACK;
34785a53ca16SPaul Saab 			to->to_sacks = cp + 2;
347978b50714SRobert Watson 			TCPSTAT_INC(tcps_sack_rcv_blocks);
34806d90faf3SPaul Saab 			break;
3481281a0fd4SPatrick Kelsey 		case TCPOPT_FAST_OPEN:
3482c560df6fSPatrick Kelsey 			/*
3483c560df6fSPatrick Kelsey 			 * Cookie length validation is performed by the
3484c560df6fSPatrick Kelsey 			 * server side cookie checking code or the client
3485c560df6fSPatrick Kelsey 			 * side cookie cache update code.
3486c560df6fSPatrick Kelsey 			 */
3487281a0fd4SPatrick Kelsey 			if (!(flags & TO_SYN))
3488281a0fd4SPatrick Kelsey 				continue;
3489c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
3490c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
3491281a0fd4SPatrick Kelsey 				continue;
3492281a0fd4SPatrick Kelsey 			to->to_flags |= TOF_FASTOPEN;
3493281a0fd4SPatrick Kelsey 			to->to_tfo_len = optlen - 2;
3494281a0fd4SPatrick Kelsey 			to->to_tfo_cookie = to->to_tfo_len ? cp + 2 : NULL;
3495281a0fd4SPatrick Kelsey 			break;
3496be2ac88cSJonathan Lemon 		default:
3497be2ac88cSJonathan Lemon 			continue;
3498df8bae1dSRodney W. Grimes 		}
3499df8bae1dSRodney W. Grimes 	}
3500df8bae1dSRodney W. Grimes }
3501df8bae1dSRodney W. Grimes 
3502df8bae1dSRodney W. Grimes /*
3503df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
3504df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
3505df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
3506df8bae1dSRodney W. Grimes  * sequencing purposes.
3507df8bae1dSRodney W. Grimes  */
350855bceb1eSRandall Stewart void
3509ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m,
3510ad3f9ab3SAndre Oppermann     int off)
3511df8bae1dSRodney W. Grimes {
3512fb59c426SYoshinobu Inoue 	int cnt = off + th->th_urp - 1;
3513df8bae1dSRodney W. Grimes 
3514df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
3515df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
3516df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
3517df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
3518df8bae1dSRodney W. Grimes 
35199eb0e832SGleb Smirnoff 			INP_WLOCK_ASSERT(tptoinpcb(tp));
35207a3244ccSRobert Watson 
3521df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
3522df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
3523df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
3524df8bae1dSRodney W. Grimes 			m->m_len--;
352569a34685SYoshinobu Inoue 			if (m->m_flags & M_PKTHDR)
352669a34685SYoshinobu Inoue 				m->m_pkthdr.len--;
3527df8bae1dSRodney W. Grimes 			return;
3528df8bae1dSRodney W. Grimes 		}
3529df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
3530df8bae1dSRodney W. Grimes 		m = m->m_next;
35314dfdffe9SAndre Oppermann 		if (m == NULL)
3532df8bae1dSRodney W. Grimes 			break;
3533df8bae1dSRodney W. Grimes 	}
3534df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
3535df8bae1dSRodney W. Grimes }
3536df8bae1dSRodney W. Grimes 
3537df8bae1dSRodney W. Grimes /*
3538df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
3539df8bae1dSRodney W. Grimes  * and update averages and current timeout.
3540df8bae1dSRodney W. Grimes  */
354155bceb1eSRandall Stewart void
3542ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt)
3543df8bae1dSRodney W. Grimes {
3544ad3f9ab3SAndre Oppermann 	int delta;
3545233e8c18SGarrett Wollman 
35469eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
35472be3bf22SRobert Watson 
354878b50714SRobert Watson 	TCPSTAT_INC(tcps_rttupdated);
354918b83b62SRichard Scheffenegger 	if (tp->t_rttupdated < UCHAR_MAX)
3550233e8c18SGarrett Wollman 		tp->t_rttupdated++;
3551adc56f5aSEdward Tomasz Napierala #ifdef STATS
3552adc56f5aSEdward Tomasz Napierala 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT,
3553adc56f5aSEdward Tomasz Napierala 	    imax(0, rtt * 1000 / hz));
3554adc56f5aSEdward Tomasz Napierala #endif
35555ede40dcSRyan Stone 	if ((tp->t_srtt != 0) && (tp->t_rxtshift <= TCP_RTT_INVALIDATE)) {
3556233e8c18SGarrett Wollman 		/*
3557233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
3558233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
3559233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
3560233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
3561233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
3562233e8c18SGarrett Wollman 		 */
3563233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
3564233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
3565233e8c18SGarrett Wollman 
3566233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
3567233e8c18SGarrett Wollman 			tp->t_srtt = 1;
3568233e8c18SGarrett Wollman 
3569233e8c18SGarrett Wollman 		/*
3570233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
3571233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
3572233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
3573233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
3574233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
3575233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
3576233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
3577233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
3578233e8c18SGarrett Wollman 		 */
3579233e8c18SGarrett Wollman 		if (delta < 0)
3580233e8c18SGarrett Wollman 			delta = -delta;
3581233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
3582233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
3583233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
3584233e8c18SGarrett Wollman 	} else {
3585233e8c18SGarrett Wollman 		/*
3586233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
3587233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
3588233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
3589233e8c18SGarrett Wollman 		 */
3590233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
3591233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
3592233e8c18SGarrett Wollman 	}
35939b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
3594df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
3595df8bae1dSRodney W. Grimes 
3596df8bae1dSRodney W. Grimes 	/*
3597df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
3598df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
3599df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
3600df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
3601df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
3602df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
3603df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
3604df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
3605df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
3606df8bae1dSRodney W. Grimes 	 */
3607233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
36089e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
3609df8bae1dSRodney W. Grimes 
3610df8bae1dSRodney W. Grimes 	/*
3611df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
3612df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
3613df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
3614df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
3615df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
3616df8bae1dSRodney W. Grimes 	 */
3617df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
3618df8bae1dSRodney W. Grimes }
3619df8bae1dSRodney W. Grimes 
3620df8bae1dSRodney W. Grimes /*
3621df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
3622df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
36234faaea55SAndre Oppermann  * If none, use an mss that can be handled on the outgoing interface
36244faaea55SAndre Oppermann  * without forcing IP to fragment.  If no route is found, route has no mtu,
3625df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
3626df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
3627df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
3628df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
3629df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
3630df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
3631df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
3632a0292f23SGarrett Wollman  *
36330c39d38dSGleb Smirnoff  * NOTE that resulting t_maxseg doesn't include space for TCP options or
36340c39d38dSGleb Smirnoff  * IP options, e.g. IPSEC data, since length of this data may vary, and
36350c39d38dSGleb Smirnoff  * thus it is calculated for every segment separately in tcp_output().
3636a0292f23SGarrett Wollman  *
363797d8d152SAndre Oppermann  * NOTE that this routine is only called when we process an incoming
3638ef341ee1SGleb Smirnoff  * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS
3639ef341ee1SGleb Smirnoff  * settings are handled in tcp_mssopt().
3640df8bae1dSRodney W. Grimes  */
3641a0292f23SGarrett Wollman void
3642ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer,
36433c914c54SAndre Oppermann     struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap)
3644df8bae1dSRodney W. Grimes {
3645b287c6c7SBjoern A. Zeeb 	int mss = 0;
36463ac12506SJonathan T. Looney 	uint32_t maxmtu = 0;
36479eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
364897d8d152SAndre Oppermann 	struct hc_metrics_lite metrics;
3649fb59c426SYoshinobu Inoue #ifdef INET6
3650c068736aSJeffrey Hsu 	int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
3651c068736aSJeffrey Hsu 	size_t min_protoh = isipv6 ?
3652c068736aSJeffrey Hsu 			    sizeof (struct ip6_hdr) + sizeof (struct tcphdr) :
3653c068736aSJeffrey Hsu 			    sizeof (struct tcpiphdr);
3654c068736aSJeffrey Hsu #else
36559e644c23SMichael Tuexen 	 size_t min_protoh = sizeof(struct tcpiphdr);
3656fb59c426SYoshinobu Inoue #endif
3657df8bae1dSRodney W. Grimes 
36589eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(inp);
36597a3244ccSRobert Watson 
36609e644c23SMichael Tuexen 	if (tp->t_port)
36619e644c23SMichael Tuexen 		min_protoh += V_tcp_udp_tunneling_overhead;
3662ef341ee1SGleb Smirnoff 	if (mtuoffer != -1) {
3663ef341ee1SGleb Smirnoff 		KASSERT(offer == -1, ("%s: conflict", __func__));
3664ef341ee1SGleb Smirnoff 		offer = mtuoffer - min_protoh;
3665ef341ee1SGleb Smirnoff 	}
3666ef341ee1SGleb Smirnoff 
3667e8949f74SAndre Oppermann 	/* Initialize. */
366897d8d152SAndre Oppermann #ifdef INET6
366997d8d152SAndre Oppermann 	if (isipv6) {
36703c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu6(&inp->inp_inc, cap);
36710c39d38dSGleb Smirnoff 		tp->t_maxseg = V_tcp_v6mssdflt;
3672b287c6c7SBjoern A. Zeeb 	}
367397d8d152SAndre Oppermann #endif
3674b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3675b287c6c7SBjoern A. Zeeb 	else
3676b287c6c7SBjoern A. Zeeb #endif
3677b287c6c7SBjoern A. Zeeb #ifdef INET
367897d8d152SAndre Oppermann 	{
36793c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu(&inp->inp_inc, cap);
36800c39d38dSGleb Smirnoff 		tp->t_maxseg = V_tcp_mssdflt;
3681df8bae1dSRodney W. Grimes 	}
3682b287c6c7SBjoern A. Zeeb #endif
3683df8bae1dSRodney W. Grimes 
368497d8d152SAndre Oppermann 	/*
3685e8949f74SAndre Oppermann 	 * No route to sender, stay with default mss and return.
368697d8d152SAndre Oppermann 	 */
36876f01cac6SBjoern A. Zeeb 	if (maxmtu == 0) {
36886f01cac6SBjoern A. Zeeb 		/*
36898e5c87f4SBjoern A. Zeeb 		 * In case we return early we need to initialize metrics
36906f01cac6SBjoern A. Zeeb 		 * to a defined state as tcp_hc_get() would do for us
36916f01cac6SBjoern A. Zeeb 		 * if there was no cache hit.
36926f01cac6SBjoern A. Zeeb 		 */
36936f01cac6SBjoern A. Zeeb 		if (metricptr != NULL)
36946f01cac6SBjoern A. Zeeb 			bzero(metricptr, sizeof(struct hc_metrics_lite));
369597d8d152SAndre Oppermann 		return;
36966f01cac6SBjoern A. Zeeb 	}
369797d8d152SAndre Oppermann 
3698e8949f74SAndre Oppermann 	/* What have we got? */
369997d8d152SAndre Oppermann 	switch (offer) {
370097d8d152SAndre Oppermann 		case 0:
370197d8d152SAndre Oppermann 			/*
370297d8d152SAndre Oppermann 			 * Offer == 0 means that there was no MSS on the SYN
3703c3b02504SBjoern A. Zeeb 			 * segment, in this case we use tcp_mssdflt as
37040c39d38dSGleb Smirnoff 			 * already assigned to t_maxseg above.
370597d8d152SAndre Oppermann 			 */
37060c39d38dSGleb Smirnoff 			offer = tp->t_maxseg;
370797d8d152SAndre Oppermann 			break;
370897d8d152SAndre Oppermann 
370997d8d152SAndre Oppermann 		case -1:
3710a0292f23SGarrett Wollman 			/*
3711c94c54e4SAndre Oppermann 			 * Offer == -1 means that we didn't receive SYN yet.
3712a0292f23SGarrett Wollman 			 */
371397d8d152SAndre Oppermann 			/* FALLTHROUGH */
371497d8d152SAndre Oppermann 
371597d8d152SAndre Oppermann 		default:
3716a0292f23SGarrett Wollman 			/*
371753369ac9SAndre Oppermann 			 * Prevent DoS attack with too small MSS. Round up
371853369ac9SAndre Oppermann 			 * to at least minmss.
371953369ac9SAndre Oppermann 			 */
3720603724d3SBjoern A. Zeeb 			offer = max(offer, V_tcp_minmss);
372197d8d152SAndre Oppermann 	}
3722a0292f23SGarrett Wollman 
3723df8bae1dSRodney W. Grimes 	/*
3724e8949f74SAndre Oppermann 	 * rmx information is now retrieved from tcp_hostcache.
3725df8bae1dSRodney W. Grimes 	 */
372697d8d152SAndre Oppermann 	tcp_hc_get(&inp->inp_inc, &metrics);
37273cee92e0SBjoern A. Zeeb 	if (metricptr != NULL)
37283cee92e0SBjoern A. Zeeb 		bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite));
372997d8d152SAndre Oppermann 
3730df8bae1dSRodney W. Grimes 	/*
3731cc412412SHiren Panchasara 	 * If there's a discovered mtu in tcp hostcache, use it.
3732cc412412SHiren Panchasara 	 * Else, use the link mtu.
3733df8bae1dSRodney W. Grimes 	 */
373497d8d152SAndre Oppermann 	if (metrics.rmx_mtu)
37352d166c02SAndre Oppermann 		mss = min(metrics.rmx_mtu, maxmtu) - min_protoh;
3736c068736aSJeffrey Hsu 	else {
373731b3783cSHajimu UMEMOTO #ifdef INET6
3738fb59c426SYoshinobu Inoue 		if (isipv6) {
373997d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3740603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
374197d8d152SAndre Oppermann 			    !in6_localaddr(&inp->in6p_faddr))
3742603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_v6mssdflt);
3743b287c6c7SBjoern A. Zeeb 		}
3744b3399803SHajimu UMEMOTO #endif
3745b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3746b287c6c7SBjoern A. Zeeb 		else
3747b287c6c7SBjoern A. Zeeb #endif
3748b287c6c7SBjoern A. Zeeb #ifdef INET
374997d8d152SAndre Oppermann 		{
375097d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3751603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
375297d8d152SAndre Oppermann 			    !in_localaddr(inp->inp_faddr))
3753603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_mssdflt);
3754a0292f23SGarrett Wollman 		}
3755b287c6c7SBjoern A. Zeeb #endif
37563cee92e0SBjoern A. Zeeb 		/*
37573cee92e0SBjoern A. Zeeb 		 * XXX - The above conditional (mss = maxmtu - min_protoh)
37583cee92e0SBjoern A. Zeeb 		 * probably violates the TCP spec.
37593cee92e0SBjoern A. Zeeb 		 * The problem is that, since we don't know the
37603cee92e0SBjoern A. Zeeb 		 * other end's MSS, we are supposed to use a conservative
37613cee92e0SBjoern A. Zeeb 		 * default.  But, if we do that, then MTU discovery will
37623cee92e0SBjoern A. Zeeb 		 * never actually take place, because the conservative
37633cee92e0SBjoern A. Zeeb 		 * default is much less than the MTUs typically seen
37643cee92e0SBjoern A. Zeeb 		 * on the Internet today.  For the moment, we'll sweep
37653cee92e0SBjoern A. Zeeb 		 * this under the carpet.
37663cee92e0SBjoern A. Zeeb 		 *
37673cee92e0SBjoern A. Zeeb 		 * The conservative default might not actually be a problem
37683cee92e0SBjoern A. Zeeb 		 * if the only case this occurs is when sending an initial
37693cee92e0SBjoern A. Zeeb 		 * SYN with options and data to a host we've never talked
37703cee92e0SBjoern A. Zeeb 		 * to before.  Then, they will reply with an MSS value which
37713cee92e0SBjoern A. Zeeb 		 * will get recorded and the new parameters should get
37723cee92e0SBjoern A. Zeeb 		 * recomputed.  For Further Study.
37733cee92e0SBjoern A. Zeeb 		 */
377497d8d152SAndre Oppermann 	}
3775a0292f23SGarrett Wollman 	mss = min(mss, offer);
377697d8d152SAndre Oppermann 
3777a0292f23SGarrett Wollman 	/*
37780c39d38dSGleb Smirnoff 	 * Sanity check: make sure that maxseg will be large
37793cee92e0SBjoern A. Zeeb 	 * enough to allow some data on segments even if the
37803cee92e0SBjoern A. Zeeb 	 * all the option space is used (40bytes).  Otherwise
37813cee92e0SBjoern A. Zeeb 	 * funny things may happen in tcp_output.
37820c39d38dSGleb Smirnoff 	 *
37830c39d38dSGleb Smirnoff 	 * XXXGL: shouldn't we reserve space for IP/IPv6 options?
37843cee92e0SBjoern A. Zeeb 	 */
37853cee92e0SBjoern A. Zeeb 	mss = max(mss, 64);
37863cee92e0SBjoern A. Zeeb 
378797d8d152SAndre Oppermann 	tp->t_maxseg = mss;
37883cee92e0SBjoern A. Zeeb }
37893cee92e0SBjoern A. Zeeb 
37903cee92e0SBjoern A. Zeeb void
37913cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer)
37923cee92e0SBjoern A. Zeeb {
3793dbc42409SLawrence Stewart 	int mss;
37943ac12506SJonathan T. Looney 	uint32_t bufsize;
37959eb0e832SGleb Smirnoff 	struct inpcb *inp = tptoinpcb(tp);
37963cee92e0SBjoern A. Zeeb 	struct socket *so;
37973cee92e0SBjoern A. Zeeb 	struct hc_metrics_lite metrics;
37983c914c54SAndre Oppermann 	struct tcp_ifcap cap;
3799dbc42409SLawrence Stewart 
38003cee92e0SBjoern A. Zeeb 	KASSERT(tp != NULL, ("%s: tp == NULL", __func__));
38013cee92e0SBjoern A. Zeeb 
38023c914c54SAndre Oppermann 	bzero(&cap, sizeof(cap));
38033c914c54SAndre Oppermann 	tcp_mss_update(tp, offer, -1, &metrics, &cap);
38043cee92e0SBjoern A. Zeeb 
38053cee92e0SBjoern A. Zeeb 	mss = tp->t_maxseg;
380697d8d152SAndre Oppermann 
3807df8bae1dSRodney W. Grimes 	/*
380897d8d152SAndre Oppermann 	 * If there's a pipesize, change the socket buffer to that size,
380997d8d152SAndre Oppermann 	 * don't change if sb_hiwat is different than default (then it
381097d8d152SAndre Oppermann 	 * has been changed on purpose with setsockopt).
381197d8d152SAndre Oppermann 	 * Make the socket buffers an integral number of mss units;
381297d8d152SAndre Oppermann 	 * if the mss is larger than the socket buffer, decrease the mss.
3813df8bae1dSRodney W. Grimes 	 */
3814c3b02504SBjoern A. Zeeb 	so = inp->inp_socket;
38153f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
3816e233e2acSAndre Oppermann 	if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe)
381797d8d152SAndre Oppermann 		bufsize = metrics.rmx_sendpipe;
381897d8d152SAndre Oppermann 	else
3819df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
3820df8bae1dSRodney W. Grimes 	if (bufsize < mss)
3821df8bae1dSRodney W. Grimes 		mss = bufsize;
3822df8bae1dSRodney W. Grimes 	else {
3823df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3824df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3825df8bae1dSRodney W. Grimes 			bufsize = sb_max;
382688c39af3SRuslan Ermilov 		if (bufsize > so->so_snd.sb_hiwat)
382743283184SGleb Smirnoff 			(void)sbreserve_locked(so, SO_SND, bufsize, NULL);
3828df8bae1dSRodney W. Grimes 	}
38293f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
3830784ce8faSHiren Panchasara 	/*
3831784ce8faSHiren Panchasara 	 * Sanity check: make sure that maxseg will be large
3832784ce8faSHiren Panchasara 	 * enough to allow some data on segments even if the
3833784ce8faSHiren Panchasara 	 * all the option space is used (40bytes).  Otherwise
3834784ce8faSHiren Panchasara 	 * funny things may happen in tcp_output.
3835784ce8faSHiren Panchasara 	 *
3836784ce8faSHiren Panchasara 	 * XXXGL: shouldn't we reserve space for IP/IPv6 options?
3837784ce8faSHiren Panchasara 	 */
3838784ce8faSHiren Panchasara 	tp->t_maxseg = max(mss, 64);
3839df8bae1dSRodney W. Grimes 
38403f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
3841e233e2acSAndre Oppermann 	if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe)
384297d8d152SAndre Oppermann 		bufsize = metrics.rmx_recvpipe;
384397d8d152SAndre Oppermann 	else
3844df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
3845df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
3846df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3847df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3848df8bae1dSRodney W. Grimes 			bufsize = sb_max;
384988c39af3SRuslan Ermilov 		if (bufsize > so->so_rcv.sb_hiwat)
385043283184SGleb Smirnoff 			(void)sbreserve_locked(so, SO_RCV, bufsize, NULL);
3851df8bae1dSRodney W. Grimes 	}
38523f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
385391d6cfa6SBjoern A. Zeeb 
385491d6cfa6SBjoern A. Zeeb 	/* Check the interface for TSO capabilities. */
38553c914c54SAndre Oppermann 	if (cap.ifcap & CSUM_TSO) {
385691d6cfa6SBjoern A. Zeeb 		tp->t_flags |= TF_TSO;
38573c914c54SAndre Oppermann 		tp->t_tsomax = cap.tsomax;
38589fd573c3SHans Petter Selasky 		tp->t_tsomaxsegcount = cap.tsomaxsegcount;
38599fd573c3SHans Petter Selasky 		tp->t_tsomaxsegsize = cap.tsomaxsegsize;
38603c914c54SAndre Oppermann 	}
3861a0292f23SGarrett Wollman }
3862a0292f23SGarrett Wollman 
3863a0292f23SGarrett Wollman /*
3864a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
3865a0292f23SGarrett Wollman  */
3866a0292f23SGarrett Wollman int
3867ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc)
3868a0292f23SGarrett Wollman {
386997d8d152SAndre Oppermann 	int mss = 0;
38703ac12506SJonathan T. Looney 	uint32_t thcmtu = 0;
38713ac12506SJonathan T. Looney 	uint32_t maxmtu = 0;
387297d8d152SAndre Oppermann 	size_t min_protoh;
3873a0292f23SGarrett Wollman 
387497d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer"));
3875a0292f23SGarrett Wollman 
387631b3783cSHajimu UMEMOTO #ifdef INET6
3877dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
3878603724d3SBjoern A. Zeeb 		mss = V_tcp_v6mssdflt;
3879233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu6(inc, NULL);
388097d8d152SAndre Oppermann 		min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
3881b287c6c7SBjoern A. Zeeb 	}
388231b3783cSHajimu UMEMOTO #endif
3883b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3884b287c6c7SBjoern A. Zeeb 	else
3885b287c6c7SBjoern A. Zeeb #endif
3886b287c6c7SBjoern A. Zeeb #ifdef INET
388797d8d152SAndre Oppermann 	{
3888603724d3SBjoern A. Zeeb 		mss = V_tcp_mssdflt;
3889233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu(inc, NULL);
389097d8d152SAndre Oppermann 		min_protoh = sizeof(struct tcpiphdr);
389197d8d152SAndre Oppermann 	}
3892b287c6c7SBjoern A. Zeeb #endif
38933a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET)
38943a9391deSBjoern A. Zeeb 	thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
38953a9391deSBjoern A. Zeeb #endif
38963a9391deSBjoern A. Zeeb 
389797d8d152SAndre Oppermann 	if (maxmtu && thcmtu)
389897d8d152SAndre Oppermann 		mss = min(maxmtu, thcmtu) - min_protoh;
389997d8d152SAndre Oppermann 	else if (maxmtu || thcmtu)
390097d8d152SAndre Oppermann 		mss = max(maxmtu, thcmtu) - min_protoh;
390197d8d152SAndre Oppermann 
390297d8d152SAndre Oppermann 	return (mss);
3903df8bae1dSRodney W. Grimes }
390446f58482SJonathan Lemon 
39050e1d7c25SRichard Scheffenegger void
390690cca08eSRichard Scheffenegger tcp_do_prr_ack(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to)
39070e1d7c25SRichard Scheffenegger {
390848396dc7SRichard Scheffenegger 	int snd_cnt = 0, limit = 0, del_data = 0, pipe = 0;
39090e1d7c25SRichard Scheffenegger 	int maxseg = tcp_maxseg(tp);
39100e1d7c25SRichard Scheffenegger 
39119eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
39120e1d7c25SRichard Scheffenegger 
39130e1d7c25SRichard Scheffenegger 	/*
39140e1d7c25SRichard Scheffenegger 	 * Compute the amount of data that this ACK is indicating
39150e1d7c25SRichard Scheffenegger 	 * (del_data) and an estimate of how many bytes are in the
39160e1d7c25SRichard Scheffenegger 	 * network.
39170e1d7c25SRichard Scheffenegger 	 */
3918c21b7b55SRichard Scheffenegger 	if (tcp_is_sack_recovery(tp, to) ||
391974d7fc87SRichard Scheffenegger 	    (IN_CONGRECOVERY(tp->t_flags) &&
392074d7fc87SRichard Scheffenegger 	     !IN_FASTRECOVERY(tp->t_flags))) {
392131d7a27cSRichard Scheffenegger 		del_data = tp->sackhint.delivered_data;
3922d1de2b05SRichard Scheffenegger 		if (V_tcp_do_newsack)
3923a8e431e1SRichard Scheffenegger 			pipe = tcp_compute_pipe(tp);
3924a8e431e1SRichard Scheffenegger 		else
392574d7fc87SRichard Scheffenegger 			pipe = (tp->snd_nxt - tp->snd_fack) +
392674d7fc87SRichard Scheffenegger 				tp->sackhint.sack_bytes_rexmit;
392774d7fc87SRichard Scheffenegger 	} else {
392874d7fc87SRichard Scheffenegger 		if (tp->sackhint.prr_delivered < (tcprexmtthresh * maxseg +
392974d7fc87SRichard Scheffenegger 					     tp->snd_recover - tp->snd_una))
393074d7fc87SRichard Scheffenegger 			del_data = maxseg;
393174d7fc87SRichard Scheffenegger 		pipe = imax(0, tp->snd_max - tp->snd_una -
393274d7fc87SRichard Scheffenegger 			    imin(INT_MAX / 65536, tp->t_dupacks) * maxseg);
393374d7fc87SRichard Scheffenegger 	}
39340e1d7c25SRichard Scheffenegger 	tp->sackhint.prr_delivered += del_data;
39350e1d7c25SRichard Scheffenegger 	/*
39360e1d7c25SRichard Scheffenegger 	 * Proportional Rate Reduction
39370e1d7c25SRichard Scheffenegger 	 */
3938e9071000SRichard Scheffenegger 	if (pipe >= tp->snd_ssthresh) {
39396a376af0SRichard Scheffenegger 		if (tp->sackhint.recover_fs == 0)
39406a376af0SRichard Scheffenegger 			tp->sackhint.recover_fs =
394148396dc7SRichard Scheffenegger 			    imax(1, tp->snd_nxt - tp->snd_una);
394248396dc7SRichard Scheffenegger 		snd_cnt = howmany((long)tp->sackhint.prr_delivered *
394348396dc7SRichard Scheffenegger 			    tp->snd_ssthresh, tp->sackhint.recover_fs) -
3944e5313869SRichard Scheffenegger 			    tp->sackhint.prr_out;
39456a376af0SRichard Scheffenegger 	} else {
394674d7fc87SRichard Scheffenegger 		if (V_tcp_do_prr_conservative || (del_data == 0))
394784761f3dSRichard Scheffenegger 			limit = tp->sackhint.prr_delivered -
3948e5313869SRichard Scheffenegger 				tp->sackhint.prr_out;
39490e1d7c25SRichard Scheffenegger 		else
395048396dc7SRichard Scheffenegger 			limit = imax(tp->sackhint.prr_delivered -
3951e5313869SRichard Scheffenegger 				    tp->sackhint.prr_out, del_data) +
3952e5313869SRichard Scheffenegger 				    maxseg;
395348396dc7SRichard Scheffenegger 		snd_cnt = imin((tp->snd_ssthresh - pipe), limit);
39540e1d7c25SRichard Scheffenegger 	}
395548396dc7SRichard Scheffenegger 	snd_cnt = imax(snd_cnt, 0) / maxseg;
39560e1d7c25SRichard Scheffenegger 	/*
39570e1d7c25SRichard Scheffenegger 	 * Send snd_cnt new data into the network in response to this ack.
39580e1d7c25SRichard Scheffenegger 	 * If there is going to be a SACK retransmission, adjust snd_cwnd
39590e1d7c25SRichard Scheffenegger 	 * accordingly.
39600e1d7c25SRichard Scheffenegger 	 */
3961b9f803b7SRichard Scheffenegger 	if (IN_FASTRECOVERY(tp->t_flags)) {
3962c21b7b55SRichard Scheffenegger 		if (tcp_is_sack_recovery(tp, to)) {
396374d7fc87SRichard Scheffenegger 			tp->snd_cwnd = tp->snd_nxt - tp->snd_recover +
396474d7fc87SRichard Scheffenegger 					    tp->sackhint.sack_bytes_rexmit +
396574d7fc87SRichard Scheffenegger 					    (snd_cnt * maxseg);
396674d7fc87SRichard Scheffenegger 		} else {
396774d7fc87SRichard Scheffenegger 			tp->snd_cwnd = (tp->snd_max - tp->snd_una) +
396874d7fc87SRichard Scheffenegger 					    (snd_cnt * maxseg);
396974d7fc87SRichard Scheffenegger 		}
3970b9f803b7SRichard Scheffenegger 	} else if (IN_CONGRECOVERY(tp->t_flags))
397174d7fc87SRichard Scheffenegger 		tp->snd_cwnd = pipe - del_data + (snd_cnt * maxseg);
397274d7fc87SRichard Scheffenegger 	tp->snd_cwnd = imax(maxseg, tp->snd_cwnd);
39730e1d7c25SRichard Scheffenegger }
39740e1d7c25SRichard Scheffenegger 
397546f58482SJonathan Lemon /*
3976c068736aSJeffrey Hsu  * On a partial ack arrives, force the retransmission of the
3977c068736aSJeffrey Hsu  * next unacknowledged segment.  Do not clear tp->t_dupacks.
3978c068736aSJeffrey Hsu  * By setting snd_nxt to ti_ack, this forces retransmission timer to
3979c068736aSJeffrey Hsu  * be started again.
398046f58482SJonathan Lemon  */
398155bceb1eSRandall Stewart void
3982ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th)
398346f58482SJonathan Lemon {
398446f58482SJonathan Lemon 	tcp_seq onxt = tp->snd_nxt;
39853ac12506SJonathan T. Looney 	uint32_t ocwnd = tp->snd_cwnd;
39860c39d38dSGleb Smirnoff 	u_int maxseg = tcp_maxseg(tp);
398746f58482SJonathan Lemon 
39889eb0e832SGleb Smirnoff 	INP_WLOCK_ASSERT(tptoinpcb(tp));
39897a3244ccSRobert Watson 
3990b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_REXMT, 0);
399146f58482SJonathan Lemon 	tp->t_rtttime = 0;
399246f58482SJonathan Lemon 	tp->snd_nxt = th->th_ack;
39936b2a5f92SJayanth Vijayaraghavan 	/*
3994c068736aSJeffrey Hsu 	 * Set snd_cwnd to one segment beyond acknowledged offset.
3995c068736aSJeffrey Hsu 	 * (tp->snd_una has not yet been updated when this function is called.)
39966b2a5f92SJayanth Vijayaraghavan 	 */
39970c39d38dSGleb Smirnoff 	tp->snd_cwnd = maxseg + BYTES_THIS_ACK(tp, th);
3998a84db8f4SMatthew Dillon 	tp->t_flags |= TF_ACKNOW;
399940fa3e40SGleb Smirnoff 	(void) tcp_output(tp);
400046f58482SJonathan Lemon 	tp->snd_cwnd = ocwnd;
400146f58482SJonathan Lemon 	if (SEQ_GT(onxt, tp->snd_nxt))
400246f58482SJonathan Lemon 		tp->snd_nxt = onxt;
400346f58482SJonathan Lemon 	/*
400446f58482SJonathan Lemon 	 * Partial window deflation.  Relies on fact that tp->snd_una
400546f58482SJonathan Lemon 	 * not updated yet.
400646f58482SJonathan Lemon 	 */
4007dbc42409SLawrence Stewart 	if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th))
4008dbc42409SLawrence Stewart 		tp->snd_cwnd -= BYTES_THIS_ACK(tp, th);
4009d7587117SPaul Saab 	else
4010d7587117SPaul Saab 		tp->snd_cwnd = 0;
40110c39d38dSGleb Smirnoff 	tp->snd_cwnd += maxseg;
401246f58482SJonathan Lemon }
401312eeb81fSHiren Panchasara 
401412eeb81fSHiren Panchasara int
401512eeb81fSHiren Panchasara tcp_compute_pipe(struct tcpcb *tp)
401612eeb81fSHiren Panchasara {
4017e5049a17SRandall Stewart 	if (tp->t_fb->tfb_compute_pipe == NULL) {
401812eeb81fSHiren Panchasara 		return (tp->snd_max - tp->snd_una +
401912eeb81fSHiren Panchasara 			tp->sackhint.sack_bytes_rexmit -
402012eeb81fSHiren Panchasara 			tp->sackhint.sacked_bytes);
4021e5049a17SRandall Stewart 	} else {
4022e5049a17SRandall Stewart 		return((*tp->t_fb->tfb_compute_pipe)(tp));
4023e5049a17SRandall Stewart 	}
402412eeb81fSHiren Panchasara }
40257dc90a1dSMichael Tuexen 
40267dc90a1dSMichael Tuexen uint32_t
40277dc90a1dSMichael Tuexen tcp_compute_initwnd(uint32_t maxseg)
40287dc90a1dSMichael Tuexen {
40297dc90a1dSMichael Tuexen 	/*
40307dc90a1dSMichael Tuexen 	 * Calculate the Initial Window, also used as Restart Window
40317dc90a1dSMichael Tuexen 	 *
40327dc90a1dSMichael Tuexen 	 * RFC5681 Section 3.1 specifies the default conservative values.
40337dc90a1dSMichael Tuexen 	 * RFC3390 specifies slightly more aggressive values.
40347dc90a1dSMichael Tuexen 	 * RFC6928 increases it to ten segments.
40357dc90a1dSMichael Tuexen 	 * Support for user specified value for initial flight size.
40367dc90a1dSMichael Tuexen 	 */
40377dc90a1dSMichael Tuexen 	if (V_tcp_initcwnd_segments)
40387dc90a1dSMichael Tuexen 		return min(V_tcp_initcwnd_segments * maxseg,
40397dc90a1dSMichael Tuexen 		    max(2 * maxseg, V_tcp_initcwnd_segments * 1460));
40407dc90a1dSMichael Tuexen 	else if (V_tcp_do_rfc3390)
40417dc90a1dSMichael Tuexen 		return min(4 * maxseg, max(2 * maxseg, 4380));
40427dc90a1dSMichael Tuexen 	else {
40437dc90a1dSMichael Tuexen 		/* Per RFC5681 Section 3.1 */
40447dc90a1dSMichael Tuexen 		if (maxseg > 2190)
40457dc90a1dSMichael Tuexen 			return (2 * maxseg);
40467dc90a1dSMichael Tuexen 		else if (maxseg > 1095)
40477dc90a1dSMichael Tuexen 			return (3 * maxseg);
40487dc90a1dSMichael Tuexen 		else
40497dc90a1dSMichael Tuexen 			return (4 * maxseg);
40507dc90a1dSMichael Tuexen 	}
40517dc90a1dSMichael Tuexen }
4052