xref: /freebsd/sys/netinet/tcp_input.c (revision 84761f3df508aed50783b60f028af9d98a684b41)
1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4e79adb8eSGarrett Wollman  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6dbc42409SLawrence Stewart  * Copyright (c) 2007-2008,2010
7dbc42409SLawrence Stewart  *	Swinburne University of Technology, Melbourne, Australia.
8dbc42409SLawrence Stewart  * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org>
9dbc42409SLawrence Stewart  * Copyright (c) 2010 The FreeBSD Foundation
10fa046d87SRobert Watson  * Copyright (c) 2010-2011 Juniper Networks, Inc.
11dbc42409SLawrence Stewart  * All rights reserved.
12dbc42409SLawrence Stewart  *
13dbc42409SLawrence Stewart  * Portions of this software were developed at the Centre for Advanced Internet
14891b8ed4SLawrence Stewart  * Architectures, Swinburne University of Technology, by Lawrence Stewart,
15891b8ed4SLawrence Stewart  * James Healy and David Hayes, made possible in part by a grant from the Cisco
16891b8ed4SLawrence Stewart  * University Research Program Fund at Community Foundation Silicon Valley.
17dbc42409SLawrence Stewart  *
18dbc42409SLawrence Stewart  * Portions of this software were developed at the Centre for Advanced
19dbc42409SLawrence Stewart  * Internet Architectures, Swinburne University of Technology, Melbourne,
20dbc42409SLawrence Stewart  * Australia by David Hayes under sponsorship from the FreeBSD Foundation.
21df8bae1dSRodney W. Grimes  *
22fa046d87SRobert Watson  * Portions of this software were developed by Robert N. M. Watson under
23fa046d87SRobert Watson  * contract to Juniper Networks, Inc.
24fa046d87SRobert Watson  *
25df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
26df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
27df8bae1dSRodney W. Grimes  * are met:
28df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
29df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
30df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
31df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
32df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
33fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
34df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
35df8bae1dSRodney W. Grimes  *    without specific prior written permission.
36df8bae1dSRodney W. Grimes  *
37df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
38df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
41df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
48df8bae1dSRodney W. Grimes  *
49e79adb8eSGarrett Wollman  *	@(#)tcp_input.c	8.12 (Berkeley) 5/24/95
50df8bae1dSRodney W. Grimes  */
51df8bae1dSRodney W. Grimes 
524b421e2dSMike Silbersack #include <sys/cdefs.h>
534b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
544b421e2dSMike Silbersack 
551cfd4b53SBruce M Simpson #include "opt_inet.h"
56fb59c426SYoshinobu Inoue #include "opt_inet6.h"
573a2a9f79SYoshinobu Inoue #include "opt_ipsec.h"
580cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
590cc12cc5SJoerg Wunsch 
60df8bae1dSRodney W. Grimes #include <sys/param.h>
61adc56f5aSEdward Tomasz Napierala #include <sys/arb.h>
6298163b98SPoul-Henning Kamp #include <sys/kernel.h>
63bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
6439bc9de5SLawrence Stewart #include <sys/hhook.h>
65bd79708dSJonathan T. Looney #endif
66df8bae1dSRodney W. Grimes #include <sys/malloc.h>
67df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
68a29f300eSGarrett Wollman #include <sys/proc.h>		/* for proc0 declaration */
69df8bae1dSRodney W. Grimes #include <sys/protosw.h>
70adc56f5aSEdward Tomasz Napierala #include <sys/qmath.h>
7157f60867SMark Johnston #include <sys/sdt.h>
72960ed29cSSeigo Tanimura #include <sys/signalvar.h>
73df8bae1dSRodney W. Grimes #include <sys/socket.h>
74df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
75960ed29cSSeigo Tanimura #include <sys/sysctl.h>
76816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
77960ed29cSSeigo Tanimura #include <sys/systm.h>
78adc56f5aSEdward Tomasz Napierala #include <sys/stats.h>
79df8bae1dSRodney W. Grimes 
80e79adb8eSGarrett Wollman #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
81e79adb8eSGarrett Wollman 
8212e2e970SAndre Oppermann #include <vm/uma.h>
8312e2e970SAndre Oppermann 
84df8bae1dSRodney W. Grimes #include <net/if.h>
8576039bc8SGleb Smirnoff #include <net/if_var.h>
86df8bae1dSRodney W. Grimes #include <net/route.h>
87530c0060SRobert Watson #include <net/vnet.h>
88df8bae1dSRodney W. Grimes 
89773673c1SAndre Oppermann #define TCPSTATES		/* for logging */
90773673c1SAndre Oppermann 
91df8bae1dSRodney W. Grimes #include <netinet/in.h>
9257f60867SMark Johnston #include <netinet/in_kdtrace.h>
93960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
94df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
95df8bae1dSRodney W. Grimes #include <netinet/ip.h>
968e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h>	/* required for icmp_var.h */
97686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM */
98df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
99ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
100686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
101686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
102686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
103b66d74c1SGleb Smirnoff #include <netinet6/in6_var.h>
104960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h>
105960ed29cSSeigo Tanimura #include <netinet6/nd6.h>
1062de3e790SGleb Smirnoff #include <netinet/tcp.h>
107df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
1082529f56eSJonathan T. Looney #include <netinet/tcp_log_buf.h>
109df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
110df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
111df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
112fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h>
113df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
1144644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
115c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h>
11686a996e6SHiren Panchasara #ifdef TCPPCAP
11786a996e6SHiren Panchasara #include <netinet/tcp_pcap.h>
11886a996e6SHiren Panchasara #endif
119c325962bSMike Silbersack #include <netinet/tcp_syncache.h>
120610ee2f9SDavid Greenman #ifdef TCPDEBUG
121df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
122fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */
12309fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
12409fe6320SNavdeep Parhar #include <netinet/tcp_offload.h>
12509fe6320SNavdeep Parhar #endif
126fb59c426SYoshinobu Inoue 
127fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
128b9234fafSSam Leffler 
129db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
130db4f9cc7SJonathan Lemon 
131aed55708SRobert Watson #include <security/mac/mac_framework.h>
132aed55708SRobert Watson 
133dbc42409SLawrence Stewart const int tcprexmtthresh = 3;
1340312fbe9SPoul-Henning Kamp 
135334fc582SBjoern A. Zeeb VNET_DEFINE(int, tcp_log_in_vain) = 0;
136334fc582SBjoern A. Zeeb SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_VNET | CTLFLAG_RW,
137334fc582SBjoern A. Zeeb     &VNET_NAME(tcp_log_in_vain), 0,
138eddfbb76SRobert Watson     "Log all incoming TCP segments to closed ports");
139816a3d83SPoul-Henning Kamp 
14082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, blackhole) = 0;
14182cea7e6SBjoern A. Zeeb #define	V_blackhole		VNET(blackhole)
1426df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW,
143eddfbb76SRobert Watson     &VNET_NAME(blackhole), 0,
144eddfbb76SRobert Watson     "Do not send RST on segments to closed ports");
14516f7f31fSGeoff Rehmet 
14682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_delack_enabled) = 1;
1476df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_VNET | CTLFLAG_RW,
148eddfbb76SRobert Watson     &VNET_NAME(tcp_delack_enabled), 0,
1493d177f46SBill Fumerola     "Delay ACK to try and piggyback it onto a data packet");
150f498eeeeSDavid Greenman 
15182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, drop_synfin) = 0;
1526df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_VNET | CTLFLAG_RW,
153eddfbb76SRobert Watson     &VNET_NAME(drop_synfin), 0,
154eddfbb76SRobert Watson     "Drop TCP packets with SYN+FIN set");
155f8613305SDag-Erling Smørgrav 
1560e1d7c25SRichard Scheffenegger VNET_DEFINE(int, tcp_do_prr_conservative) = 0;
1570e1d7c25SRichard Scheffenegger SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_prr_conservative, CTLFLAG_VNET | CTLFLAG_RW,
1580e1d7c25SRichard Scheffenegger     &VNET_NAME(tcp_do_prr_conservative), 0,
1590e1d7c25SRichard Scheffenegger     "Do conservative Proportional Rate Reduction");
1600e1d7c25SRichard Scheffenegger 
1610e1d7c25SRichard Scheffenegger VNET_DEFINE(int, tcp_do_prr) = 1;
1620e1d7c25SRichard Scheffenegger SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_prr, CTLFLAG_VNET | CTLFLAG_RW,
1630e1d7c25SRichard Scheffenegger     &VNET_NAME(tcp_do_prr), 1,
1640e1d7c25SRichard Scheffenegger     "Enable Proportional Rate Reduction per RFC 6937");
1650e1d7c25SRichard Scheffenegger 
166b72e56e7SMichael Tuexen VNET_DEFINE(int, tcp_do_newcwv) = 0;
167b72e56e7SMichael Tuexen SYSCTL_INT(_net_inet_tcp, OID_AUTO, newcwv, CTLFLAG_VNET | CTLFLAG_RW,
168b72e56e7SMichael Tuexen     &VNET_NAME(tcp_do_newcwv), 0,
169b72e56e7SMichael Tuexen     "Enable New Congestion Window Validation per RFC7661");
170b72e56e7SMichael Tuexen 
17112eeb81fSHiren Panchasara VNET_DEFINE(int, tcp_do_rfc6675_pipe) = 0;
172054d38e3SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc6675_pipe, CTLFLAG_VNET | CTLFLAG_RW,
17312eeb81fSHiren Panchasara     &VNET_NAME(tcp_do_rfc6675_pipe), 0,
17412eeb81fSHiren Panchasara     "Use calculated pipe/in-flight bytes per RFC 6675");
17512eeb81fSHiren Panchasara 
17682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3042) = 1;
1776df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_VNET | CTLFLAG_RW,
178eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3042), 0,
179eddfbb76SRobert Watson     "Enable RFC 3042 (Limited Transmit)");
180582a954bSJeffrey Hsu 
18182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3390) = 1;
1826df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_VNET | CTLFLAG_RW,
183eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3390), 0,
184da3a8a1aSJeffrey Hsu     "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)");
185da3a8a1aSJeffrey Hsu 
186356c7958SHiren Panchasara VNET_DEFINE(int, tcp_initcwnd_segments) = 10;
187356c7958SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, initcwnd_segments,
188356c7958SHiren Panchasara     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_initcwnd_segments), 0,
189356c7958SHiren Panchasara     "Slow-start flight size (initial congestion window) in number of segments");
19009440655SAndre Oppermann 
19182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3465) = 1;
1926df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_VNET | CTLFLAG_RW,
193eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3465), 0,
19424cb0f22SLawrence Stewart     "Enable RFC 3465 (Appropriate Byte Counting)");
195eddfbb76SRobert Watson 
19682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_abc_l_var) = 2;
1976df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_VNET | CTLFLAG_RW,
198eddfbb76SRobert Watson     &VNET_NAME(tcp_abc_l_var), 2,
19924cb0f22SLawrence Stewart     "Cap the max cwnd increment during slow-start to this number of segments");
20024cb0f22SLawrence Stewart 
2017029da5cSPawel Biernacki static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn,
2027029da5cSPawel Biernacki     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
2037029da5cSPawel Biernacki     "TCP ECN");
204f2512ba1SRui Paulo 
205883054b4SDon Lewis VNET_DEFINE(int, tcp_do_ecn) = 2;
2066df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW,
207eddfbb76SRobert Watson     &VNET_NAME(tcp_do_ecn), 0,
208eddfbb76SRobert Watson     "TCP ECN support");
209eddfbb76SRobert Watson 
21082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_ecn_maxretries) = 1;
2116df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, maxretries, CTLFLAG_VNET | CTLFLAG_RW,
212eddfbb76SRobert Watson     &VNET_NAME(tcp_ecn_maxretries), 0,
213eddfbb76SRobert Watson     "Max retries before giving up on ECN");
214eddfbb76SRobert Watson 
2153220a212SGleb Smirnoff VNET_DEFINE(int, tcp_insecure_syn) = 0;
2166df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_syn, CTLFLAG_VNET | CTLFLAG_RW,
2173220a212SGleb Smirnoff     &VNET_NAME(tcp_insecure_syn), 0,
2183220a212SGleb Smirnoff     "Follow RFC793 instead of RFC5961 criteria for accepting SYN packets");
2193220a212SGleb Smirnoff 
22082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_insecure_rst) = 0;
2216df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_VNET | CTLFLAG_RW,
222eddfbb76SRobert Watson     &VNET_NAME(tcp_insecure_rst), 0,
2233220a212SGleb Smirnoff     "Follow RFC793 instead of RFC5961 criteria for accepting RST packets");
224a69968eeSMike Silbersack 
225fba0cea1SBjoern A. Zeeb VNET_DEFINE(int, tcp_recvspace) = 1024*64;
226873789cbSAndre Oppermann #define	V_tcp_recvspace	VNET(tcp_recvspace)
2276df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_VNET | CTLFLAG_RW,
228873789cbSAndre Oppermann     &VNET_NAME(tcp_recvspace), 0, "Initial receive socket buffer size");
229873789cbSAndre Oppermann 
23082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autorcvbuf) = 1;
2316df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_VNET | CTLFLAG_RW,
232eddfbb76SRobert Watson     &VNET_NAME(tcp_do_autorcvbuf), 0,
2338b615593SMarko Zec     "Enable automatic receive buffer sizing");
2346741ecf5SAndre Oppermann 
235b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024;
2366df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_VNET | CTLFLAG_RW,
237eddfbb76SRobert Watson     &VNET_NAME(tcp_autorcvbuf_max), 0,
2388b615593SMarko Zec     "Max size of automatic receive buffer");
2396741ecf5SAndre Oppermann 
240eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, tcb);
24144e33a07SMarko Zec #define	tcb6	tcb  /* for KAME src sync over BSD*'s */
24282cea7e6SBjoern A. Zeeb VNET_DEFINE(struct inpcbinfo, tcbinfo);
243df8bae1dSRodney W. Grimes 
244315e3e38SRobert Watson /*
245bf840a17SGleb Smirnoff  * TCP statistics are stored in an array of counter(9)s, which size matches
246bf840a17SGleb Smirnoff  * size of struct tcpstat.  TCP running connection count is a regular array.
2475923c293SGleb Smirnoff  */
2485da0521fSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct tcpstat, tcpstat);
2495da0521fSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_tcp, TCPCTL_STATS, stats, struct tcpstat,
2505da0521fSAndrey V. Elsukov     tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
251bf840a17SGleb Smirnoff VNET_DEFINE(counter_u64_t, tcps_states[TCP_NSTATES]);
252bf840a17SGleb Smirnoff SYSCTL_COUNTER_U64_ARRAY(_net_inet_tcp, TCPCTL_STATES, states, CTLFLAG_RD |
253d4d32b9fSHans Petter Selasky     CTLFLAG_VNET, &VNET_NAME(tcps_states)[0], TCP_NSTATES,
254bf840a17SGleb Smirnoff     "TCP connection counts by TCP state");
255bf840a17SGleb Smirnoff 
256bf840a17SGleb Smirnoff static void
257bf840a17SGleb Smirnoff tcp_vnet_init(const void *unused)
258bf840a17SGleb Smirnoff {
259bf840a17SGleb Smirnoff 
260f59d975eSGleb Smirnoff 	COUNTER_ARRAY_ALLOC(V_tcps_states, TCP_NSTATES, M_WAITOK);
261bf840a17SGleb Smirnoff 	VNET_PCPUSTAT_ALLOC(tcpstat, M_WAITOK);
262bf840a17SGleb Smirnoff }
263bf840a17SGleb Smirnoff VNET_SYSINIT(tcp_vnet_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
264bf840a17SGleb Smirnoff     tcp_vnet_init, NULL);
2655923c293SGleb Smirnoff 
2665923c293SGleb Smirnoff #ifdef VIMAGE
267bf840a17SGleb Smirnoff static void
268bf840a17SGleb Smirnoff tcp_vnet_uninit(const void *unused)
269bf840a17SGleb Smirnoff {
270bf840a17SGleb Smirnoff 
271f59d975eSGleb Smirnoff 	COUNTER_ARRAY_FREE(V_tcps_states, TCP_NSTATES);
272bf840a17SGleb Smirnoff 	VNET_PCPUSTAT_FREE(tcpstat);
273bf840a17SGleb Smirnoff }
274bf840a17SGleb Smirnoff VNET_SYSUNINIT(tcp_vnet_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
275bf840a17SGleb Smirnoff     tcp_vnet_uninit, NULL);
2765923c293SGleb Smirnoff #endif /* VIMAGE */
277bf840a17SGleb Smirnoff 
2785923c293SGleb Smirnoff /*
2797ca6e296SMichael Tuexen  * Kernel module interface for updating tcpstat.  The first argument is an index
2805923c293SGleb Smirnoff  * into tcpstat treated as an array.
281315e3e38SRobert Watson  */
282315e3e38SRobert Watson void
2837ca6e296SMichael Tuexen kmod_tcpstat_add(int statnum, int val)
284315e3e38SRobert Watson {
285315e3e38SRobert Watson 
2867ca6e296SMichael Tuexen 	counter_u64_add(VNET(tcpstat)[statnum], val);
287315e3e38SRobert Watson }
288315e3e38SRobert Watson 
289bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
290dbc42409SLawrence Stewart /*
29139bc9de5SLawrence Stewart  * Wrapper for the TCP established input helper hook.
29239bc9de5SLawrence Stewart  */
29355bceb1eSRandall Stewart void
29439bc9de5SLawrence Stewart hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to)
29539bc9de5SLawrence Stewart {
29639bc9de5SLawrence Stewart 	struct tcp_hhook_data hhook_data;
29739bc9de5SLawrence Stewart 
29839bc9de5SLawrence Stewart 	if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) {
29939bc9de5SLawrence Stewart 		hhook_data.tp = tp;
30039bc9de5SLawrence Stewart 		hhook_data.th = th;
30139bc9de5SLawrence Stewart 		hhook_data.to = to;
30239bc9de5SLawrence Stewart 
30339bc9de5SLawrence Stewart 		hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data,
30439bc9de5SLawrence Stewart 		    tp->osd);
30539bc9de5SLawrence Stewart 	}
30639bc9de5SLawrence Stewart }
307bd79708dSJonathan T. Looney #endif
30839bc9de5SLawrence Stewart 
30939bc9de5SLawrence Stewart /*
310dbc42409SLawrence Stewart  * CC wrapper hook functions
311dbc42409SLawrence Stewart  */
31255bceb1eSRandall Stewart void
3134b7b743cSLawrence Stewart cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t nsegs,
3144b7b743cSLawrence Stewart     uint16_t type)
315f2512ba1SRui Paulo {
316adc56f5aSEdward Tomasz Napierala #ifdef STATS
317adc56f5aSEdward Tomasz Napierala 	int32_t gput;
318adc56f5aSEdward Tomasz Napierala #endif
319adc56f5aSEdward Tomasz Napierala 
320dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
321f2512ba1SRui Paulo 
3224b7b743cSLawrence Stewart 	tp->ccv->nsegs = nsegs;
323dbc42409SLawrence Stewart 	tp->ccv->bytes_this_ack = BYTES_THIS_ACK(tp, th);
324b72e56e7SMichael Tuexen 	if ((!V_tcp_do_newcwv && (tp->snd_cwnd <= tp->snd_wnd)) ||
325b72e56e7SMichael Tuexen 	    (V_tcp_do_newcwv && (tp->snd_cwnd <= tp->snd_wnd) &&
326b72e56e7SMichael Tuexen 	     (tp->snd_cwnd < (tcp_compute_pipe(tp) * 2))))
327dbc42409SLawrence Stewart 		tp->ccv->flags |= CCF_CWND_LIMITED;
328dbc42409SLawrence Stewart 	else
329dbc42409SLawrence Stewart 		tp->ccv->flags &= ~CCF_CWND_LIMITED;
330dbc42409SLawrence Stewart 
331dbc42409SLawrence Stewart 	if (type == CC_ACK) {
332adc56f5aSEdward Tomasz Napierala #ifdef STATS
333adc56f5aSEdward Tomasz Napierala 		stats_voi_update_abs_s32(tp->t_stats, VOI_TCP_CALCFRWINDIFF,
334adc56f5aSEdward Tomasz Napierala 		    ((int32_t)tp->snd_cwnd) - tp->snd_wnd);
335adc56f5aSEdward Tomasz Napierala 		if (!IN_RECOVERY(tp->t_flags))
336adc56f5aSEdward Tomasz Napierala 			stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_ACKLEN,
337adc56f5aSEdward Tomasz Napierala 			   tp->ccv->bytes_this_ack / (tcp_maxseg(tp) * nsegs));
338adc56f5aSEdward Tomasz Napierala 		if ((tp->t_flags & TF_GPUTINPROG) &&
339adc56f5aSEdward Tomasz Napierala 		    SEQ_GEQ(th->th_ack, tp->gput_ack)) {
340adc56f5aSEdward Tomasz Napierala 			/*
341adc56f5aSEdward Tomasz Napierala 			 * Compute goodput in bits per millisecond.
342adc56f5aSEdward Tomasz Napierala 			 */
343adc56f5aSEdward Tomasz Napierala 			gput = (((int64_t)(th->th_ack - tp->gput_seq)) << 3) /
344adc56f5aSEdward Tomasz Napierala 			    max(1, tcp_ts_getticks() - tp->gput_ts);
345adc56f5aSEdward Tomasz Napierala 			stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_GPUT,
346adc56f5aSEdward Tomasz Napierala 			    gput);
347adc56f5aSEdward Tomasz Napierala 			/*
348adc56f5aSEdward Tomasz Napierala 			 * XXXLAS: This is a temporary hack, and should be
349adc56f5aSEdward Tomasz Napierala 			 * chained off VOI_TCP_GPUT when stats(9) grows an API
350adc56f5aSEdward Tomasz Napierala 			 * to deal with chained VOIs.
351adc56f5aSEdward Tomasz Napierala 			 */
352adc56f5aSEdward Tomasz Napierala 			if (tp->t_stats_gput_prev > 0)
353adc56f5aSEdward Tomasz Napierala 				stats_voi_update_abs_s32(tp->t_stats,
354adc56f5aSEdward Tomasz Napierala 				    VOI_TCP_GPUT_ND,
355adc56f5aSEdward Tomasz Napierala 				    ((gput - tp->t_stats_gput_prev) * 100) /
356adc56f5aSEdward Tomasz Napierala 				    tp->t_stats_gput_prev);
357adc56f5aSEdward Tomasz Napierala 			tp->t_flags &= ~TF_GPUTINPROG;
358adc56f5aSEdward Tomasz Napierala 			tp->t_stats_gput_prev = gput;
359adc56f5aSEdward Tomasz Napierala 		}
360adc56f5aSEdward Tomasz Napierala #endif /* STATS */
361dbc42409SLawrence Stewart 		if (tp->snd_cwnd > tp->snd_ssthresh) {
3621951fa79SMichael Tuexen 			tp->t_bytes_acked += tp->ccv->bytes_this_ack;
363dbc42409SLawrence Stewart 			if (tp->t_bytes_acked >= tp->snd_cwnd) {
364dbc42409SLawrence Stewart 				tp->t_bytes_acked -= tp->snd_cwnd;
365dbc42409SLawrence Stewart 				tp->ccv->flags |= CCF_ABC_SENTAWND;
366dbc42409SLawrence Stewart 			}
367dbc42409SLawrence Stewart 		} else {
368dbc42409SLawrence Stewart 				tp->ccv->flags &= ~CCF_ABC_SENTAWND;
369dbc42409SLawrence Stewart 				tp->t_bytes_acked = 0;
370dbc42409SLawrence Stewart 		}
371dbc42409SLawrence Stewart 	}
372dbc42409SLawrence Stewart 
373dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->ack_received != NULL) {
374dbc42409SLawrence Stewart 		/* XXXLAS: Find a way to live without this */
375dbc42409SLawrence Stewart 		tp->ccv->curack = th->th_ack;
376dbc42409SLawrence Stewart 		CC_ALGO(tp)->ack_received(tp->ccv, type);
377dbc42409SLawrence Stewart 	}
378adc56f5aSEdward Tomasz Napierala #ifdef STATS
379adc56f5aSEdward Tomasz Napierala 	stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_LCWIN, tp->snd_cwnd);
380adc56f5aSEdward Tomasz Napierala #endif
381dbc42409SLawrence Stewart }
382dbc42409SLawrence Stewart 
38355bceb1eSRandall Stewart void
384dbc42409SLawrence Stewart cc_conn_init(struct tcpcb *tp)
385dbc42409SLawrence Stewart {
386dbc42409SLawrence Stewart 	struct hc_metrics_lite metrics;
387dbc42409SLawrence Stewart 	struct inpcb *inp = tp->t_inpcb;
3880c39d38dSGleb Smirnoff 	u_int maxseg;
389dbc42409SLawrence Stewart 	int rtt;
390dbc42409SLawrence Stewart 
391dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
392dbc42409SLawrence Stewart 
393dbc42409SLawrence Stewart 	tcp_hc_get(&inp->inp_inc, &metrics);
3940c39d38dSGleb Smirnoff 	maxseg = tcp_maxseg(tp);
395dbc42409SLawrence Stewart 
396dbc42409SLawrence Stewart 	if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) {
397dbc42409SLawrence Stewart 		tp->t_srtt = rtt;
398dbc42409SLawrence Stewart 		tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE;
399dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_usedrtt);
400dbc42409SLawrence Stewart 		if (metrics.rmx_rttvar) {
401dbc42409SLawrence Stewart 			tp->t_rttvar = metrics.rmx_rttvar;
402dbc42409SLawrence Stewart 			TCPSTAT_INC(tcps_usedrttvar);
403dbc42409SLawrence Stewart 		} else {
404dbc42409SLawrence Stewart 			/* default variation is +- 1 rtt */
405dbc42409SLawrence Stewart 			tp->t_rttvar =
406dbc42409SLawrence Stewart 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
407dbc42409SLawrence Stewart 		}
408dbc42409SLawrence Stewart 		TCPT_RANGESET(tp->t_rxtcur,
409dbc42409SLawrence Stewart 		    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
410dbc42409SLawrence Stewart 		    tp->t_rttmin, TCPTV_REXMTMAX);
411dbc42409SLawrence Stewart 	}
412dbc42409SLawrence Stewart 	if (metrics.rmx_ssthresh) {
413dbc42409SLawrence Stewart 		/*
414dbc42409SLawrence Stewart 		 * There's some sort of gateway or interface
415dbc42409SLawrence Stewart 		 * buffer limit on the path.  Use this to set
416a4641f4eSPedro F. Giffuni 		 * the slow start threshold, but set the
417dbc42409SLawrence Stewart 		 * threshold to no less than 2*mss.
418dbc42409SLawrence Stewart 		 */
4190c39d38dSGleb Smirnoff 		tp->snd_ssthresh = max(2 * maxseg, metrics.rmx_ssthresh);
420dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_usedssthresh);
421dbc42409SLawrence Stewart 	}
422dbc42409SLawrence Stewart 
423dbc42409SLawrence Stewart 	/*
4249ec4a4ccSAndre Oppermann 	 * Set the initial slow-start flight size.
425dbc42409SLawrence Stewart 	 *
426cf8f04f4SAndre Oppermann 	 * If a SYN or SYN/ACK was lost and retransmitted, we have to
427cf8f04f4SAndre Oppermann 	 * reduce the initial CWND to one segment as congestion is likely
428cf8f04f4SAndre Oppermann 	 * requiring us to be cautious.
429dbc42409SLawrence Stewart 	 */
430cf8f04f4SAndre Oppermann 	if (tp->snd_cwnd == 1)
4310c39d38dSGleb Smirnoff 		tp->snd_cwnd = maxseg;		/* SYN(-ACK) lost */
432dbc42409SLawrence Stewart 	else
4337dc90a1dSMichael Tuexen 		tp->snd_cwnd = tcp_compute_initwnd(maxseg);
434dbc42409SLawrence Stewart 
435dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->conn_init != NULL)
436dbc42409SLawrence Stewart 		CC_ALGO(tp)->conn_init(tp->ccv);
437dbc42409SLawrence Stewart }
438dbc42409SLawrence Stewart 
439dbc42409SLawrence Stewart void inline
440dbc42409SLawrence Stewart cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type)
441dbc42409SLawrence Stewart {
442dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
443dbc42409SLawrence Stewart 
444adc56f5aSEdward Tomasz Napierala #ifdef STATS
445adc56f5aSEdward Tomasz Napierala 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_CSIG, type);
446adc56f5aSEdward Tomasz Napierala #endif
447adc56f5aSEdward Tomasz Napierala 
448dbc42409SLawrence Stewart 	switch(type) {
449dbc42409SLawrence Stewart 	case CC_NDUPACK:
450dbc42409SLawrence Stewart 		if (!IN_FASTRECOVERY(tp->t_flags)) {
451f2512ba1SRui Paulo 			tp->snd_recover = tp->snd_max;
4523cf38784SMichael Tuexen 			if (tp->t_flags2 & TF2_ECN_PERMIT)
4533cf38784SMichael Tuexen 				tp->t_flags2 |= TF2_ECN_SND_CWR;
454f2512ba1SRui Paulo 		}
455dbc42409SLawrence Stewart 		break;
456dbc42409SLawrence Stewart 	case CC_ECN:
457af2fb894SRichard Scheffenegger 		if (!IN_CONGRECOVERY(tp->t_flags) ||
458af2fb894SRichard Scheffenegger 		    /*
459af2fb894SRichard Scheffenegger 		     * Allow ECN reaction on ACK to CWR, if
460af2fb894SRichard Scheffenegger 		     * that data segment was also CE marked.
461af2fb894SRichard Scheffenegger 		     */
462af2fb894SRichard Scheffenegger 		    SEQ_GEQ(th->th_ack, tp->snd_recover)) {
463af2fb894SRichard Scheffenegger 			EXIT_CONGRECOVERY(tp->t_flags);
464dbc42409SLawrence Stewart 			TCPSTAT_INC(tcps_ecn_rcwnd);
465af2fb894SRichard Scheffenegger 			tp->snd_recover = tp->snd_max + 1;
4663cf38784SMichael Tuexen 			if (tp->t_flags2 & TF2_ECN_PERMIT)
4673cf38784SMichael Tuexen 				tp->t_flags2 |= TF2_ECN_SND_CWR;
468dbc42409SLawrence Stewart 		}
469dbc42409SLawrence Stewart 		break;
470dbc42409SLawrence Stewart 	case CC_RTO:
471dbc42409SLawrence Stewart 		tp->t_dupacks = 0;
472dbc42409SLawrence Stewart 		tp->t_bytes_acked = 0;
473dbc42409SLawrence Stewart 		EXIT_RECOVERY(tp->t_flags);
4749cc711c9SMichael Tuexen 		if (tp->t_flags2 & TF2_ECN_PERMIT)
4759cc711c9SMichael Tuexen 			tp->t_flags2 |= TF2_ECN_SND_CWR;
476dbc42409SLawrence Stewart 		break;
477dbc42409SLawrence Stewart 	case CC_RTO_ERR:
478dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_sndrexmitbad);
479dbc42409SLawrence Stewart 		/* RTO was unnecessary, so reset everything. */
480dbc42409SLawrence Stewart 		tp->snd_cwnd = tp->snd_cwnd_prev;
481dbc42409SLawrence Stewart 		tp->snd_ssthresh = tp->snd_ssthresh_prev;
482dbc42409SLawrence Stewart 		tp->snd_recover = tp->snd_recover_prev;
483dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASFRECOVERY)
484dbc42409SLawrence Stewart 			ENTER_FASTRECOVERY(tp->t_flags);
485dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASCRECOVERY)
486dbc42409SLawrence Stewart 			ENTER_CONGRECOVERY(tp->t_flags);
487dbc42409SLawrence Stewart 		tp->snd_nxt = tp->snd_max;
488672dc4aeSJohn Baldwin 		tp->t_flags &= ~TF_PREVVALID;
489dbc42409SLawrence Stewart 		tp->t_badrxtwin = 0;
490dbc42409SLawrence Stewart 		break;
491dbc42409SLawrence Stewart 	}
492dbc42409SLawrence Stewart 
493dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->cong_signal != NULL) {
494dbc42409SLawrence Stewart 		if (th != NULL)
495dbc42409SLawrence Stewart 			tp->ccv->curack = th->th_ack;
496dbc42409SLawrence Stewart 		CC_ALGO(tp)->cong_signal(tp->ccv, type);
497dbc42409SLawrence Stewart 	}
498dbc42409SLawrence Stewart }
499dbc42409SLawrence Stewart 
50055bceb1eSRandall Stewart void inline
501dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th)
502dbc42409SLawrence Stewart {
503dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
504dbc42409SLawrence Stewart 
505dbc42409SLawrence Stewart 	/* XXXLAS: KASSERT that we're in recovery? */
506dbc42409SLawrence Stewart 
507dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->post_recovery != NULL) {
508dbc42409SLawrence Stewart 		tp->ccv->curack = th->th_ack;
509dbc42409SLawrence Stewart 		CC_ALGO(tp)->post_recovery(tp->ccv);
510dbc42409SLawrence Stewart 	}
511dbc42409SLawrence Stewart 	/* XXXLAS: EXIT_RECOVERY ? */
512dbc42409SLawrence Stewart 	tp->t_bytes_acked = 0;
513bc7ee8e5SRichard Scheffenegger 	tp->sackhint.recover_fs = 0;
514dbc42409SLawrence Stewart }
5150312fbe9SPoul-Henning Kamp 
516df8bae1dSRodney W. Grimes /*
517262c1c1aSMatthew Dillon  * Indicate whether this ack should be delayed.  We can delay the ack if
51885536381SHiren Panchasara  * following conditions are met:
51985536381SHiren Panchasara  *	- There is no delayed ack timer in progress.
52085536381SHiren Panchasara  *	- Our last ack wasn't a 0-sized window. We never want to delay
52185536381SHiren Panchasara  *	  the ack that opens up a 0-sized window.
52285536381SHiren Panchasara  *	- LRO wasn't used for this segment. We make sure by checking that the
52385536381SHiren Panchasara  *	  segment size is not larger than the MSS.
524d8c85a26SJonathan Lemon  */
525c1e5a6e5SAndre Oppermann #define DELAY_ACK(tp, tlen)						\
526b8152ba7SAndre Oppermann 	((!tcp_timer_active(tp, TT_DELACK) &&				\
527a14c749fSJonathan Lemon 	    (tp->t_flags & TF_RXWIN0SENT) == 0) &&			\
5280c39d38dSGleb Smirnoff 	    (tlen <= tp->t_maxseg) &&					\
529603724d3SBjoern A. Zeeb 	    (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN)))
530d8c85a26SJonathan Lemon 
5314ad24737SRandall Stewart void inline
53264807b30SHiren Panchasara cc_ecnpkt_handler(struct tcpcb *tp, struct tcphdr *th, uint8_t iptos)
53364807b30SHiren Panchasara {
53464807b30SHiren Panchasara 	INP_WLOCK_ASSERT(tp->t_inpcb);
53564807b30SHiren Panchasara 
53664807b30SHiren Panchasara 	if (CC_ALGO(tp)->ecnpkt_handler != NULL) {
53764807b30SHiren Panchasara 		switch (iptos & IPTOS_ECN_MASK) {
53864807b30SHiren Panchasara 		case IPTOS_ECN_CE:
53964807b30SHiren Panchasara 			tp->ccv->flags |= CCF_IPHDR_CE;
54064807b30SHiren Panchasara 			break;
54164807b30SHiren Panchasara 		case IPTOS_ECN_ECT0:
54283a2839fSMichael Tuexen 			/* FALLTHROUGH */
54364807b30SHiren Panchasara 		case IPTOS_ECN_ECT1:
54483a2839fSMichael Tuexen 			/* FALLTHROUGH */
54583a2839fSMichael Tuexen 		case IPTOS_ECN_NOTECT:
54664807b30SHiren Panchasara 			tp->ccv->flags &= ~CCF_IPHDR_CE;
54764807b30SHiren Panchasara 			break;
54864807b30SHiren Panchasara 		}
54964807b30SHiren Panchasara 
55064807b30SHiren Panchasara 		if (th->th_flags & TH_CWR)
55164807b30SHiren Panchasara 			tp->ccv->flags |= CCF_TCPHDR_CWR;
55264807b30SHiren Panchasara 		else
55364807b30SHiren Panchasara 			tp->ccv->flags &= ~CCF_TCPHDR_CWR;
55464807b30SHiren Panchasara 
55564807b30SHiren Panchasara 		CC_ALGO(tp)->ecnpkt_handler(tp->ccv);
55664807b30SHiren Panchasara 
557e11c9783SMichael Tuexen 		if (tp->ccv->flags & CCF_ACKNOW) {
55864807b30SHiren Panchasara 			tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
559e11c9783SMichael Tuexen 			tp->t_flags |= TF_ACKNOW;
560e11c9783SMichael Tuexen 		}
56164807b30SHiren Panchasara 	}
56264807b30SHiren Panchasara }
56364807b30SHiren Panchasara 
564df8bae1dSRodney W. Grimes /*
5658d573cc1SAndre Oppermann  * TCP input handling is split into multiple parts:
5668d573cc1SAndre Oppermann  *   tcp6_input is a thin wrapper around tcp_input for the extended
5678d573cc1SAndre Oppermann  *	ip6_protox[] call format in ip6_input
5688d573cc1SAndre Oppermann  *   tcp_input handles primary segment validation, inpcb lookup and
5698d573cc1SAndre Oppermann  *	SYN processing on listen sockets
5708d573cc1SAndre Oppermann  *   tcp_do_segment processes the ACK and text of the segment for
5718d573cc1SAndre Oppermann  *	establishing, established and closing connections
572df8bae1dSRodney W. Grimes  */
573fb59c426SYoshinobu Inoue #ifdef INET6
574fb59c426SYoshinobu Inoue int
575ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto)
576fb59c426SYoshinobu Inoue {
577503f4e47SBjoern A. Zeeb 	struct mbuf *m;
57833841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6;
5793e88eb90SAndrey V. Elsukov 	struct ip6_hdr *ip6;
580fb59c426SYoshinobu Inoue 
581503f4e47SBjoern A. Zeeb 	m = *mp;
582a4adf6ccSBjoern A. Zeeb 	if (m->m_len < *offp + sizeof(struct tcphdr)) {
5834e619b17SBjoern A. Zeeb 		m = m_pullup(m, *offp + sizeof(struct tcphdr));
5844e619b17SBjoern A. Zeeb 		if (m == NULL) {
5854e619b17SBjoern A. Zeeb 			*mp = m;
5864e619b17SBjoern A. Zeeb 			TCPSTAT_INC(tcps_rcvshort);
5874e619b17SBjoern A. Zeeb 			return (IPPROTO_DONE);
5884e619b17SBjoern A. Zeeb 		}
589a4adf6ccSBjoern A. Zeeb 	}
590fb59c426SYoshinobu Inoue 
591fb59c426SYoshinobu Inoue 	/*
592fb59c426SYoshinobu Inoue 	 * draft-itojun-ipv6-tcp-to-anycast
593fb59c426SYoshinobu Inoue 	 * better place to put this in?
594fb59c426SYoshinobu Inoue 	 */
5953e88eb90SAndrey V. Elsukov 	ip6 = mtod(m, struct ip6_hdr *);
5963e88eb90SAndrey V. Elsukov 	ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
59733841545SHajimu UMEMOTO 	if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
5988c0fec80SRobert Watson 		ifa_free(&ia6->ia_ifa);
599fb59c426SYoshinobu Inoue 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
600fb59c426SYoshinobu Inoue 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
601a8fe77d8SBjoern A. Zeeb 		*mp = NULL;
6028f5a8818SKevin Lo 		return (IPPROTO_DONE);
603fb59c426SYoshinobu Inoue 	}
60477d396fdSMaksim Yevmenkin 	if (ia6)
60577d396fdSMaksim Yevmenkin 		ifa_free(&ia6->ia_ifa);
606fb59c426SYoshinobu Inoue 
607a8fe77d8SBjoern A. Zeeb 	*mp = m;
6088f5a8818SKevin Lo 	return (tcp_input(mp, offp, proto));
609fb59c426SYoshinobu Inoue }
610b287c6c7SBjoern A. Zeeb #endif /* INET6 */
611fb59c426SYoshinobu Inoue 
6128f5a8818SKevin Lo int
6138f5a8818SKevin Lo tcp_input(struct mbuf **mp, int *offp, int proto)
614df8bae1dSRodney W. Grimes {
6158f5a8818SKevin Lo 	struct mbuf *m = *mp;
616b287c6c7SBjoern A. Zeeb 	struct tcphdr *th = NULL;
617ad3f9ab3SAndre Oppermann 	struct ip *ip = NULL;
618ad3f9ab3SAndre Oppermann 	struct inpcb *inp = NULL;
619302ce8d6SAndre Oppermann 	struct tcpcb *tp = NULL;
620302ce8d6SAndre Oppermann 	struct socket *so = NULL;
621e79adb8eSGarrett Wollman 	u_char *optp = NULL;
6228f5a8818SKevin Lo 	int off0;
62326f9a767SRodney W. Grimes 	int optlen = 0;
624b287c6c7SBjoern A. Zeeb #ifdef INET
625b287c6c7SBjoern A. Zeeb 	int len;
626c4556b2fSAndrey V. Elsukov 	uint8_t ipttl;
627b287c6c7SBjoern A. Zeeb #endif
628b287c6c7SBjoern A. Zeeb 	int tlen = 0, off;
629fb59c426SYoshinobu Inoue 	int drop_hdrlen;
630ad3f9ab3SAndre Oppermann 	int thflags;
631302ce8d6SAndre Oppermann 	int rstreason = 0;	/* For badport_bandlim accounting purposes */
6321d7ee746SKurt Lidl 	uint8_t iptos;
633c1de64a4SAndrey V. Elsukov 	struct m_tag *fwd_tag = NULL;
634c068736aSJeffrey Hsu #ifdef INET6
635302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6 = NULL;
636c068736aSJeffrey Hsu 	int isipv6;
637c068736aSJeffrey Hsu #else
638a160e630SAndre Oppermann 	const void *ip6 = NULL;
639b287c6c7SBjoern A. Zeeb #endif /* INET6 */
640302ce8d6SAndre Oppermann 	struct tcpopt to;		/* options in this segment */
641a160e630SAndre Oppermann 	char *s = NULL;			/* address and port logging */
642610ee2f9SDavid Greenman #ifdef TCPDEBUG
643c068736aSJeffrey Hsu 	/*
644c068736aSJeffrey Hsu 	 * The size of tcp_saveipgen must be the size of the max ip header,
645c068736aSJeffrey Hsu 	 * now IPv6.
646c068736aSJeffrey Hsu 	 */
64714739780SMaxim Konovalov 	u_char tcp_saveipgen[IP6_HDR_LEN];
648410bb1bfSLuigi Rizzo 	struct tcphdr tcp_savetcp;
649610ee2f9SDavid Greenman 	short ostate = 0;
650610ee2f9SDavid Greenman #endif
651c068736aSJeffrey Hsu 
652d40c0d47SGleb Smirnoff 	NET_EPOCH_ASSERT();
653d40c0d47SGleb Smirnoff 
654fb59c426SYoshinobu Inoue #ifdef INET6
655fb59c426SYoshinobu Inoue 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
656fb59c426SYoshinobu Inoue #endif
657a0292f23SGarrett Wollman 
6588f5a8818SKevin Lo 	off0 = *offp;
6598f5a8818SKevin Lo 	m = *mp;
6608f5a8818SKevin Lo 	*mp = NULL;
661302ce8d6SAndre Oppermann 	to.to_flags = 0;
66278b50714SRobert Watson 	TCPSTAT_INC(tcps_rcvtotal);
663fb59c426SYoshinobu Inoue 
66404d3a452SHajimu UMEMOTO #ifdef INET6
665b287c6c7SBjoern A. Zeeb 	if (isipv6) {
666fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
66745747ba5SBjoern A. Zeeb 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
668fb59c426SYoshinobu Inoue 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
669356ab07eSBjoern A. Zeeb 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) {
67045747ba5SBjoern A. Zeeb 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
67145747ba5SBjoern A. Zeeb 				th->th_sum = m->m_pkthdr.csum_data;
67245747ba5SBjoern A. Zeeb 			else
67345747ba5SBjoern A. Zeeb 				th->th_sum = in6_cksum_pseudo(ip6, tlen,
67445747ba5SBjoern A. Zeeb 				    IPPROTO_TCP, m->m_pkthdr.csum_data);
67545747ba5SBjoern A. Zeeb 			th->th_sum ^= 0xffff;
67645747ba5SBjoern A. Zeeb 		} else
67745747ba5SBjoern A. Zeeb 			th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen);
67845747ba5SBjoern A. Zeeb 		if (th->th_sum) {
67978b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
680fb59c426SYoshinobu Inoue 			goto drop;
681fb59c426SYoshinobu Inoue 		}
68233841545SHajimu UMEMOTO 
68333841545SHajimu UMEMOTO 		/*
68433841545SHajimu UMEMOTO 		 * Be proactive about unspecified IPv6 address in source.
68533841545SHajimu UMEMOTO 		 * As we use all-zero to indicate unbounded/unconnected pcb,
68633841545SHajimu UMEMOTO 		 * unspecified IPv6 address can be used to confuse us.
68733841545SHajimu UMEMOTO 		 *
68833841545SHajimu UMEMOTO 		 * Note that packets with unspecified IPv6 destination is
68933841545SHajimu UMEMOTO 		 * already dropped in ip6_input.
69033841545SHajimu UMEMOTO 		 */
69133841545SHajimu UMEMOTO 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
69233841545SHajimu UMEMOTO 			/* XXX stat */
69333841545SHajimu UMEMOTO 			goto drop;
69433841545SHajimu UMEMOTO 		}
6951d7ee746SKurt Lidl 		iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
696b287c6c7SBjoern A. Zeeb 	}
69704d3a452SHajimu UMEMOTO #endif
698b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
699b287c6c7SBjoern A. Zeeb 	else
700b287c6c7SBjoern A. Zeeb #endif
701b287c6c7SBjoern A. Zeeb #ifdef INET
702b287c6c7SBjoern A. Zeeb 	{
703df8bae1dSRodney W. Grimes 		/*
704df8bae1dSRodney W. Grimes 		 * Get IP and TCP header together in first mbuf.
705df8bae1dSRodney W. Grimes 		 * Note: IP leaves IP header in first mbuf.
706df8bae1dSRodney W. Grimes 		 */
707fb59c426SYoshinobu Inoue 		if (off0 > sizeof (struct ip)) {
708105bd211SGleb Smirnoff 			ip_stripoptions(m);
709fb59c426SYoshinobu Inoue 			off0 = sizeof(struct ip);
710fb59c426SYoshinobu Inoue 		}
711df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof (struct tcpiphdr)) {
7124dfdffe9SAndre Oppermann 			if ((m = m_pullup(m, sizeof (struct tcpiphdr)))
7134dfdffe9SAndre Oppermann 			    == NULL) {
71478b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvshort);
7158f5a8818SKevin Lo 				return (IPPROTO_DONE);
716df8bae1dSRodney W. Grimes 			}
717df8bae1dSRodney W. Grimes 		}
718fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
719db4f9cc7SJonathan Lemon 		th = (struct tcphdr *)((caddr_t)ip + off0);
7208ad458a4SGleb Smirnoff 		tlen = ntohs(ip->ip_len) - off0;
721df8bae1dSRodney W. Grimes 
7221d7ee746SKurt Lidl 		iptos = ip->ip_tos;
723db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
724db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
725db4f9cc7SJonathan Lemon 				th->th_sum = m->m_pkthdr.csum_data;
726db4f9cc7SJonathan Lemon 			else
727db4f9cc7SJonathan Lemon 				th->th_sum = in_pseudo(ip->ip_src.s_addr,
728c068736aSJeffrey Hsu 				    ip->ip_dst.s_addr,
7298f134647SGleb Smirnoff 				    htonl(m->m_pkthdr.csum_data + tlen +
730c068736aSJeffrey Hsu 				    IPPROTO_TCP));
731db4f9cc7SJonathan Lemon 			th->th_sum ^= 0xffff;
732db4f9cc7SJonathan Lemon 		} else {
7338f134647SGleb Smirnoff 			struct ipovly *ipov = (struct ipovly *)ip;
7348f134647SGleb Smirnoff 
735df8bae1dSRodney W. Grimes 			/*
736df8bae1dSRodney W. Grimes 			 * Checksum extended TCP header and data.
737df8bae1dSRodney W. Grimes 			 */
7388f134647SGleb Smirnoff 			len = off0 + tlen;
739c4556b2fSAndrey V. Elsukov 			ipttl = ip->ip_ttl;
740fb59c426SYoshinobu Inoue 			bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
7418f134647SGleb Smirnoff 			ipov->ih_len = htons(tlen);
742fb59c426SYoshinobu Inoue 			th->th_sum = in_cksum(m, len);
74357f60867SMark Johnston 			/* Reset length for SDT probes. */
7441d7ee746SKurt Lidl 			ip->ip_len = htons(len);
7451d7ee746SKurt Lidl 			/* Reset TOS bits */
7461d7ee746SKurt Lidl 			ip->ip_tos = iptos;
7471d7ee746SKurt Lidl 			/* Re-initialization for later version check */
748c4556b2fSAndrey V. Elsukov 			ip->ip_ttl = ipttl;
7491d7ee746SKurt Lidl 			ip->ip_v = IPVERSION;
750b2fe54bcSAndrey V. Elsukov 			ip->ip_hl = off0 >> 2;
751db4f9cc7SJonathan Lemon 		}
75257f60867SMark Johnston 
753fb59c426SYoshinobu Inoue 		if (th->th_sum) {
75478b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
755df8bae1dSRodney W. Grimes 			goto drop;
756df8bae1dSRodney W. Grimes 		}
757fb59c426SYoshinobu Inoue 	}
758b287c6c7SBjoern A. Zeeb #endif /* INET */
759df8bae1dSRodney W. Grimes 
760df8bae1dSRodney W. Grimes 	/*
761df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
762df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
763df8bae1dSRodney W. Grimes 	 */
764fb59c426SYoshinobu Inoue 	off = th->th_off << 2;
765df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
76678b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvbadoff);
767df8bae1dSRodney W. Grimes 		goto drop;
768df8bae1dSRodney W. Grimes 	}
769fb59c426SYoshinobu Inoue 	tlen -= off;	/* tlen is used instead of ti->ti_len */
770df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
77104d3a452SHajimu UMEMOTO #ifdef INET6
772b287c6c7SBjoern A. Zeeb 		if (isipv6) {
773a4adf6ccSBjoern A. Zeeb 			if (m->m_len < off0 + off) {
7744e619b17SBjoern A. Zeeb 				m = m_pullup(m, off0 + off);
7754e619b17SBjoern A. Zeeb 				if (m == NULL) {
7764e619b17SBjoern A. Zeeb 					TCPSTAT_INC(tcps_rcvshort);
7774e619b17SBjoern A. Zeeb 					return (IPPROTO_DONE);
7784e619b17SBjoern A. Zeeb 				}
779a4adf6ccSBjoern A. Zeeb 			}
780fb59c426SYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
781fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
782b287c6c7SBjoern A. Zeeb 		}
78304d3a452SHajimu UMEMOTO #endif
784b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
785b287c6c7SBjoern A. Zeeb 		else
786b287c6c7SBjoern A. Zeeb #endif
787b287c6c7SBjoern A. Zeeb #ifdef INET
788b287c6c7SBjoern A. Zeeb 		{
789df8bae1dSRodney W. Grimes 			if (m->m_len < sizeof(struct ip) + off) {
790c068736aSJeffrey Hsu 				if ((m = m_pullup(m, sizeof (struct ip) + off))
7914dfdffe9SAndre Oppermann 				    == NULL) {
79278b50714SRobert Watson 					TCPSTAT_INC(tcps_rcvshort);
7938f5a8818SKevin Lo 					return (IPPROTO_DONE);
794df8bae1dSRodney W. Grimes 				}
795fb59c426SYoshinobu Inoue 				ip = mtod(m, struct ip *);
796fb59c426SYoshinobu Inoue 				th = (struct tcphdr *)((caddr_t)ip + off0);
797fb59c426SYoshinobu Inoue 			}
798df8bae1dSRodney W. Grimes 		}
799b287c6c7SBjoern A. Zeeb #endif
800df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
801fb59c426SYoshinobu Inoue 		optp = (u_char *)(th + 1);
802df8bae1dSRodney W. Grimes 	}
803fb59c426SYoshinobu Inoue 	thflags = th->th_flags;
804df8bae1dSRodney W. Grimes 
805e46cd3d4SDag-Erling Smørgrav 	/*
806df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
807df8bae1dSRodney W. Grimes 	 */
8082903309aSAttilio Rao 	tcp_fields_to_host(th);
809df8bae1dSRodney W. Grimes 
810df8bae1dSRodney W. Grimes 	/*
811794235b7SAndre Oppermann 	 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options.
812755c1f07SAndras Olah 	 */
813fb59c426SYoshinobu Inoue 	drop_hdrlen = off0 + off;
814755c1f07SAndras Olah 
815755c1f07SAndras Olah 	/*
8168d573cc1SAndre Oppermann 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
8178d573cc1SAndre Oppermann 	 */
818b8056faeSGleb Smirnoff         if (
819b8056faeSGleb Smirnoff #ifdef INET6
820b8056faeSGleb Smirnoff 	    (isipv6 && (m->m_flags & M_IP6_NEXTHOP))
821b8056faeSGleb Smirnoff #ifdef INET
822b8056faeSGleb Smirnoff 	    || (!isipv6 && (m->m_flags & M_IP_NEXTHOP))
823b8056faeSGleb Smirnoff #endif
824b8056faeSGleb Smirnoff #endif
825b8056faeSGleb Smirnoff #if defined(INET) && !defined(INET6)
826b8056faeSGleb Smirnoff 	    (m->m_flags & M_IP_NEXTHOP)
827b8056faeSGleb Smirnoff #endif
828b8056faeSGleb Smirnoff 	    )
8299b932e9eSAndre Oppermann 		fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
8309b932e9eSAndre Oppermann 
831ce7ad664SEd Maste findpcb:
8328a006adbSBjoern A. Zeeb #ifdef INET6
8338a006adbSBjoern A. Zeeb 	if (isipv6 && fwd_tag != NULL) {
8348a006adbSBjoern A. Zeeb 		struct sockaddr_in6 *next_hop6;
8358a006adbSBjoern A. Zeeb 
8368a006adbSBjoern A. Zeeb 		next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
8378a006adbSBjoern A. Zeeb 		/*
8388a006adbSBjoern A. Zeeb 		 * Transparently forwarded. Pretend to be the destination.
8398a006adbSBjoern A. Zeeb 		 * Already got one like this?
8408a006adbSBjoern A. Zeeb 		 */
8418a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo,
8428a006adbSBjoern A. Zeeb 		    &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport,
8438a006adbSBjoern A. Zeeb 		    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif, m);
8448a006adbSBjoern A. Zeeb 		if (!inp) {
8458a006adbSBjoern A. Zeeb 			/*
8468a006adbSBjoern A. Zeeb 			 * It's new.  Try to find the ambushing socket.
8478a006adbSBjoern A. Zeeb 			 * Because we've rewritten the destination address,
8488a006adbSBjoern A. Zeeb 			 * any hardware-generated hash is ignored.
8498a006adbSBjoern A. Zeeb 			 */
8508a006adbSBjoern A. Zeeb 			inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src,
8518a006adbSBjoern A. Zeeb 			    th->th_sport, &next_hop6->sin6_addr,
8528a006adbSBjoern A. Zeeb 			    next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) :
8538a006adbSBjoern A. Zeeb 			    th->th_dport, INPLOOKUP_WILDCARD |
8548a006adbSBjoern A. Zeeb 			    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif);
8558a006adbSBjoern A. Zeeb 		}
856c1de64a4SAndrey V. Elsukov 	} else if (isipv6) {
8578a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src,
8588a006adbSBjoern A. Zeeb 		    th->th_sport, &ip6->ip6_dst, th->th_dport,
8598a006adbSBjoern A. Zeeb 		    INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB,
8608a006adbSBjoern A. Zeeb 		    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,
876fa046d87SRobert Watson 		    ip->ip_dst, th->th_dport, INPLOOKUP_WLOCKPCB,
877d3c1f003SRobert Watson 		    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) :
887fa046d87SRobert Watson 			    th->th_dport, INPLOOKUP_WILDCARD |
888fa046d87SRobert Watson 			    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif);
889f9e354dfSJulian Elischer 		}
890b10fbdeaSAndre Oppermann 	} else
891d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src,
892fa046d87SRobert Watson 		    th->th_sport, ip->ip_dst, th->th_dport,
893fa046d87SRobert Watson 		    INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB,
894d3c1f003SRobert Watson 		    m->m_pkthdr.rcvif, m);
8958a006adbSBjoern A. Zeeb #endif /* INET */
896f9e354dfSJulian Elischer 
897df8bae1dSRodney W. Grimes 	/*
898574b6964SAndre Oppermann 	 * If the INPCB does not exist then all data in the incoming
899574b6964SAndre Oppermann 	 * segment is discarded and an appropriate RST is sent back.
9008b07e49aSJulian Elischer 	 * XXX MRT Send RST using which routing table?
901df8bae1dSRodney W. Grimes 	 */
902816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
903574b6964SAndre Oppermann 		/*
904574b6964SAndre Oppermann 		 * Log communication attempts to ports that are not
905574b6964SAndre Oppermann 		 * in use.
906574b6964SAndre Oppermann 		 */
907334fc582SBjoern A. Zeeb 		if ((V_tcp_log_in_vain == 1 && (thflags & TH_SYN)) ||
908334fc582SBjoern A. Zeeb 		    V_tcp_log_in_vain == 2) {
909b7d747ecSAndre Oppermann 			if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6)))
910df541e5fSAndre Oppermann 				log(LOG_INFO, "%s; %s: Connection attempt "
911df541e5fSAndre Oppermann 				    "to closed port\n", s, __func__);
9122e4e1b4cSGeoff Rehmet 		}
913574b6964SAndre Oppermann 		/*
914574b6964SAndre Oppermann 		 * When blackholing do not respond with a RST but
915574b6964SAndre Oppermann 		 * completely ignore the segment and drop it.
916574b6964SAndre Oppermann 		 */
917603724d3SBjoern A. Zeeb 		if ((V_blackhole == 1 && (thflags & TH_SYN)) ||
918603724d3SBjoern A. Zeeb 		    V_blackhole == 2)
9191929eae1SAndre Oppermann 			goto dropunlock;
920574b6964SAndre Oppermann 
921a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
922a57815efSBosko Milekic 		goto dropwithreset;
923816a3d83SPoul-Henning Kamp 	}
924fa046d87SRobert Watson 	INP_WLOCK_ASSERT(inp);
925f5cf1e5fSJulien Charbon 	/*
926f5cf1e5fSJulien Charbon 	 * While waiting for inp lock during the lookup, another thread
927f5cf1e5fSJulien Charbon 	 * can have dropped the inpcb, in which case we need to loop back
928f5cf1e5fSJulien Charbon 	 * and try to find a new inpcb to deliver to.
929f5cf1e5fSJulien Charbon 	 */
930f5cf1e5fSJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
931f5cf1e5fSJulien Charbon 		INP_WUNLOCK(inp);
932f5cf1e5fSJulien Charbon 		inp = NULL;
933f5cf1e5fSJulien Charbon 		goto findpcb;
934f5cf1e5fSJulien Charbon 	}
935c2529042SHans Petter Selasky 	if ((inp->inp_flowtype == M_HASHTYPE_NONE) &&
936c2529042SHans Petter Selasky 	    (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) &&
937c2529042SHans Petter Selasky 	    ((inp->inp_socket == NULL) ||
938c2529042SHans Petter Selasky 	    (inp->inp_socket->so_options & SO_ACCEPTCONN) == 0)) {
93980cb9f21SKip Macy 		inp->inp_flowid = m->m_pkthdr.flowid;
9402f719932SAdrian Chadd 		inp->inp_flowtype = M_HASHTYPE_GET(m);
94180cb9f21SKip Macy 	}
942fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
943a2589465SKen Smith #ifdef INET6
944fcf59617SAndrey V. Elsukov 	if (isipv6 && IPSEC_ENABLED(ipv6) &&
945fcf59617SAndrey V. Elsukov 	    IPSEC_CHECK_POLICY(ipv6, m, inp) != 0) {
946a2589465SKen Smith 		goto dropunlock;
947a2589465SKen Smith 	}
948fcf59617SAndrey V. Elsukov #ifdef INET
949fcf59617SAndrey V. Elsukov 	else
950fcf59617SAndrey V. Elsukov #endif
951fcf59617SAndrey V. Elsukov #endif /* INET6 */
952fcf59617SAndrey V. Elsukov #ifdef INET
953fcf59617SAndrey V. Elsukov 	if (IPSEC_ENABLED(ipv4) &&
954fcf59617SAndrey V. Elsukov 	    IPSEC_CHECK_POLICY(ipv4, m, inp) != 0) {
955fcf59617SAndrey V. Elsukov 		goto dropunlock;
956fcf59617SAndrey V. Elsukov 	}
957fcf59617SAndrey V. Elsukov #endif /* INET */
958a2589465SKen Smith #endif /* IPSEC */
959a2589465SKen Smith 
9608d573cc1SAndre Oppermann 	/*
9618d573cc1SAndre Oppermann 	 * Check the minimum TTL for socket.
9628d573cc1SAndre Oppermann 	 */
96334f83c52SGeorge V. Neville-Neil 	if (inp->inp_ip_minttl != 0) {
96434f83c52SGeorge V. Neville-Neil #ifdef INET6
9652d8868dbSJonathan T. Looney 		if (isipv6) {
9662d8868dbSJonathan T. Looney 			if (inp->inp_ip_minttl > ip6->ip6_hlim)
967302ce8d6SAndre Oppermann 				goto dropunlock;
9682d8868dbSJonathan T. Looney 		} else
96934f83c52SGeorge V. Neville-Neil #endif
97034f83c52SGeorge V. Neville-Neil 		if (inp->inp_ip_minttl > ip->ip_ttl)
971302ce8d6SAndre Oppermann 			goto dropunlock;
97234f83c52SGeorge V. Neville-Neil 	}
973936cd18dSAndre Oppermann 
974340c35deSJonathan Lemon 	/*
975252ca428SRobert Watson 	 * A previous connection in TIMEWAIT state is supposed to catch stray
976252ca428SRobert Watson 	 * or duplicate segments arriving late.  If this segment was a
9770989f56cSRobert Watson 	 * legitimate new connection attempt, the old INPCB gets removed and
978252ca428SRobert Watson 	 * we can try again to find a listening socket.
979252ca428SRobert Watson 	 *
980fa046d87SRobert Watson 	 * At this point, due to earlier optimism, we may hold only an inpcb
981fa046d87SRobert Watson 	 * lock, and not the inpcbinfo write lock.  If so, we need to try to
982fa046d87SRobert Watson 	 * acquire it, or if that fails, acquire a reference on the inpcb,
983fa046d87SRobert Watson 	 * drop all locks, acquire a global write lock, and then re-acquire
984fa046d87SRobert Watson 	 * the inpcb lock.  We may at that point discover that another thread
985fa046d87SRobert Watson 	 * has tried to free the inpcb, in which case we need to loop back
986fa046d87SRobert Watson 	 * and try to find a new inpcb to deliver to.
987fa046d87SRobert Watson 	 *
988fa046d87SRobert Watson 	 * XXXRW: It may be time to rethink timewait locking.
989340c35deSJonathan Lemon 	 */
990ad71fe3cSRobert Watson 	if (inp->inp_flags & INP_TIMEWAIT) {
991283c76c7SMichael Tuexen 		tcp_dooptions(&to, optp, optlen,
992283c76c7SMichael Tuexen 		    (thflags & TH_SYN) ? TO_SYN : 0);
9938d573cc1SAndre Oppermann 		/*
9948d573cc1SAndre Oppermann 		 * NB: tcp_twcheck unlocks the INP and frees the mbuf.
9958d573cc1SAndre Oppermann 		 */
9962104448fSAndre Oppermann 		if (tcp_twcheck(inp, &to, th, m, tlen))
997340c35deSJonathan Lemon 			goto findpcb;
9988f5a8818SKevin Lo 		return (IPPROTO_DONE);
999340c35deSJonathan Lemon 	}
1000794235b7SAndre Oppermann 	/*
1001794235b7SAndre Oppermann 	 * The TCPCB may no longer exist if the connection is winding
1002794235b7SAndre Oppermann 	 * down or it is in the CLOSED state.  Either way we drop the
1003794235b7SAndre Oppermann 	 * segment and send an appropriate response.
1004794235b7SAndre Oppermann 	 */
1005df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
1006a160e630SAndre Oppermann 	if (tp == NULL || tp->t_state == TCPS_CLOSED) {
1007a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
1008a57815efSBosko Milekic 		goto dropwithreset;
100909f81a46SBosko Milekic 	}
1010df8bae1dSRodney W. Grimes 
101109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
101209fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
101309fe6320SNavdeep Parhar 		tcp_offload_input(tp, m);
101409fe6320SNavdeep Parhar 		m = NULL;	/* consumed by the TOE driver */
101509fe6320SNavdeep Parhar 		goto dropunlock;
101609fe6320SNavdeep Parhar 	}
101709fe6320SNavdeep Parhar #endif
101809fe6320SNavdeep Parhar 
1019c488362eSRobert Watson #ifdef MAC
10208501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
102130d239bcSRobert Watson 	if (mac_inpcb_check_deliver(inp, m))
1022302ce8d6SAndre Oppermann 		goto dropunlock;
1023c488362eSRobert Watson #endif
1024a557af22SRobert Watson 	so = inp->inp_socket;
1025302ce8d6SAndre Oppermann 	KASSERT(so != NULL, ("%s: so == NULL", __func__));
1026610ee2f9SDavid Greenman #ifdef TCPDEBUG
1027df8bae1dSRodney W. Grimes 	if (so->so_options & SO_DEBUG) {
1028df8bae1dSRodney W. Grimes 		ostate = tp->t_state;
102910fe523eSMaxim Konovalov #ifdef INET6
10306f697424SBjoern A. Zeeb 		if (isipv6) {
1031540e8b7eSJeffrey Hsu 			bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6));
1032d30d90dcSMaxim Konovalov 		} else
10336f697424SBjoern A. Zeeb #endif
1034540e8b7eSJeffrey Hsu 			bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
1035fb59c426SYoshinobu Inoue 		tcp_savetcp = *th;
1036df8bae1dSRodney W. Grimes 	}
1037b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */
1038db33b3e6SAndre Oppermann 	/*
1039db33b3e6SAndre Oppermann 	 * When the socket is accepting connections (the INPCB is in LISTEN
1040db33b3e6SAndre Oppermann 	 * state) we look into the SYN cache if this is a new connection
1041a7c7f2a7SJohn Baldwin 	 * attempt or the completion of a previous one.
1042db33b3e6SAndre Oppermann 	 */
104345274760SJonathan T. Looney 	KASSERT(tp->t_state == TCPS_LISTEN || !(so->so_options & SO_ACCEPTCONN),
104445274760SJonathan T. Looney 	    ("%s: so accepting but tp %p not listening", __func__, tp));
104545274760SJonathan T. Looney 	if (tp->t_state == TCPS_LISTEN && (so->so_options & SO_ACCEPTCONN)) {
1046540e8b7eSJeffrey Hsu 		struct in_conninfo inc;
1047540e8b7eSJeffrey Hsu 
10488bfb1918SAndre Oppermann 		bzero(&inc, sizeof(inc));
1049302ce8d6SAndre Oppermann #ifdef INET6
1050be2ac88cSJonathan Lemon 		if (isipv6) {
1051dcdb4371SBjoern A. Zeeb 			inc.inc_flags |= INC_ISIPV6;
10525dff1c38SMichael Tuexen 			if (inp->inp_inc.inc_flags & INC_IPV6MINMTU)
10535dff1c38SMichael Tuexen 				inc.inc_flags |= INC_IPV6MINMTU;
1054be2ac88cSJonathan Lemon 			inc.inc6_faddr = ip6->ip6_src;
1055be2ac88cSJonathan Lemon 			inc.inc6_laddr = ip6->ip6_dst;
1056302ce8d6SAndre Oppermann 		} else
1057302ce8d6SAndre Oppermann #endif
1058302ce8d6SAndre Oppermann 		{
1059be2ac88cSJonathan Lemon 			inc.inc_faddr = ip->ip_src;
1060be2ac88cSJonathan Lemon 			inc.inc_laddr = ip->ip_dst;
1061be2ac88cSJonathan Lemon 		}
1062be2ac88cSJonathan Lemon 		inc.inc_fport = th->th_sport;
1063be2ac88cSJonathan Lemon 		inc.inc_lport = th->th_dport;
10647973fba3SJulian Elischer 		inc.inc_fibnum = so->so_fibnum;
1065be2ac88cSJonathan Lemon 
1066fb59c426SYoshinobu Inoue 		/*
10678d573cc1SAndre Oppermann 		 * Check for an existing connection attempt in syncache if
10688d573cc1SAndre Oppermann 		 * the flag is only ACK.  A successful lookup creates a new
10698d573cc1SAndre Oppermann 		 * socket appended to the listen queue in SYN_RECEIVED state.
1070fb59c426SYoshinobu Inoue 		 */
1071be2ac88cSJonathan Lemon 		if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
1072bf6d304aSAndre Oppermann 			/*
1073bf6d304aSAndre Oppermann 			 * Parse the TCP options here because
1074bf6d304aSAndre Oppermann 			 * syncookies need access to the reflected
1075bf6d304aSAndre Oppermann 			 * timestamp.
1076bf6d304aSAndre Oppermann 			 */
1077bf6d304aSAndre Oppermann 			tcp_dooptions(&to, optp, optlen, 0);
10789fa198beSAndre Oppermann 			/*
10799fa198beSAndre Oppermann 			 * NB: syncache_expand() doesn't unlock
10809fa198beSAndre Oppermann 			 * inp and tcpinfo locks.
10819fa198beSAndre Oppermann 			 */
1082fcf59617SAndrey V. Elsukov 			rstreason = syncache_expand(&inc, &to, th, &so, m);
1083fcf59617SAndrey V. Elsukov 			if (rstreason < 0) {
1084fcf59617SAndrey V. Elsukov 				/*
1085fcf59617SAndrey V. Elsukov 				 * A failing TCP MD5 signature comparison
1086fcf59617SAndrey V. Elsukov 				 * must result in the segment being dropped
1087fcf59617SAndrey V. Elsukov 				 * and must not produce any response back
1088fcf59617SAndrey V. Elsukov 				 * to the sender.
1089fcf59617SAndrey V. Elsukov 				 */
1090fcf59617SAndrey V. Elsukov 				goto dropunlock;
1091fcf59617SAndrey V. Elsukov 			} else if (rstreason == 0) {
10924195b4afSPaul Traina 				/*
1093e207f800SAndre Oppermann 				 * No syncache entry or ACK was not
1094be2ac88cSJonathan Lemon 				 * for our SYN/ACK.  Send a RST.
10958d573cc1SAndre Oppermann 				 * NB: syncache did its own logging
10968d573cc1SAndre Oppermann 				 * of the failure cause.
10974195b4afSPaul Traina 				 */
1098be2ac88cSJonathan Lemon 				rstreason = BANDLIM_RST_OPENPORT;
1099be2ac88cSJonathan Lemon 				goto dropwithreset;
1100be2ac88cSJonathan Lemon 			}
110109c305ebSPatrick Kelsey tfo_socket_result:
1102f76fcf6dSJeffrey Hsu 			if (so == NULL) {
1103be2ac88cSJonathan Lemon 				/*
1104e207f800SAndre Oppermann 				 * We completed the 3-way handshake
1105e207f800SAndre Oppermann 				 * but could not allocate a socket
1106e207f800SAndre Oppermann 				 * either due to memory shortage,
1107e207f800SAndre Oppermann 				 * listen queue length limits or
1108a160e630SAndre Oppermann 				 * global socket limits.  Send RST
1109a160e630SAndre Oppermann 				 * or wait and have the remote end
1110a160e630SAndre Oppermann 				 * retransmit the ACK for another
1111a160e630SAndre Oppermann 				 * try.
1112be2ac88cSJonathan Lemon 				 */
1113a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1114a160e630SAndre Oppermann 					log(LOG_DEBUG, "%s; %s: Listen socket: "
11158d573cc1SAndre Oppermann 					    "Socket allocation failed due to "
11168d573cc1SAndre Oppermann 					    "limits or memory shortage, %s\n",
1117ac957cd2SJulian Elischer 					    s, __func__,
1118ac957cd2SJulian Elischer 					    V_tcp_sc_rst_sock_fail ?
1119ac957cd2SJulian Elischer 					    "sending RST" : "try again");
1120603724d3SBjoern A. Zeeb 				if (V_tcp_sc_rst_sock_fail) {
1121e207f800SAndre Oppermann 					rstreason = BANDLIM_UNLIMITED;
1122e207f800SAndre Oppermann 					goto dropwithreset;
1123a160e630SAndre Oppermann 				} else
1124a160e630SAndre Oppermann 					goto dropunlock;
1125f76fcf6dSJeffrey Hsu 			}
1126be2ac88cSJonathan Lemon 			/*
1127be2ac88cSJonathan Lemon 			 * Socket is created in state SYN_RECEIVED.
11288d573cc1SAndre Oppermann 			 * Unlock the listen socket, lock the newly
11298d573cc1SAndre Oppermann 			 * created socket and update the tp variable.
1130be2ac88cSJonathan Lemon 			 */
11318501a69cSRobert Watson 			INP_WUNLOCK(inp);	/* listen socket */
1132be2ac88cSJonathan Lemon 			inp = sotoinpcb(so);
1133ff9b006dSJulien Charbon 			/*
1134ff9b006dSJulien Charbon 			 * New connection inpcb is already locked by
1135ff9b006dSJulien Charbon 			 * syncache_expand().
1136ff9b006dSJulien Charbon 			 */
1137ff9b006dSJulien Charbon 			INP_WLOCK_ASSERT(inp);
1138be2ac88cSJonathan Lemon 			tp = intotcpcb(inp);
11398d573cc1SAndre Oppermann 			KASSERT(tp->t_state == TCPS_SYN_RECEIVED,
11408d573cc1SAndre Oppermann 			    ("%s: ", __func__));
1141be2ac88cSJonathan Lemon 			/*
1142302ce8d6SAndre Oppermann 			 * Process the segment and the data it
1143302ce8d6SAndre Oppermann 			 * contains.  tcp_do_segment() consumes
1144302ce8d6SAndre Oppermann 			 * the mbuf chain and unlocks the inpcb.
1145302ce8d6SAndre Oppermann 			 */
11466138da62SMichael Tuexen 			TCP_PROBE5(receive, NULL, tp, m, tp, th);
114755bceb1eSRandall Stewart 			tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen,
11486573d758SMatt Macy 			    iptos);
11498f5a8818SKevin Lo 			return (IPPROTO_DONE);
1150be2ac88cSJonathan Lemon 		}
1151a160e630SAndre Oppermann 		/*
11528d573cc1SAndre Oppermann 		 * Segment flag validation for new connection attempts:
11538d573cc1SAndre Oppermann 		 *
1154a160e630SAndre Oppermann 		 * Our (SYN|ACK) response was rejected.
1155a160e630SAndre Oppermann 		 * Check with syncache and remove entry to prevent
1156a160e630SAndre Oppermann 		 * retransmits.
115719bc77c5SAndre Oppermann 		 *
115819bc77c5SAndre Oppermann 		 * NB: syncache_chkrst does its own logging of failure
115919bc77c5SAndre Oppermann 		 * causes.
1160a160e630SAndre Oppermann 		 */
1161a160e630SAndre Oppermann 		if (thflags & TH_RST) {
116293899d10SMichael Tuexen 			syncache_chkrst(&inc, th, m);
1163a160e630SAndre Oppermann 			goto dropunlock;
1164a160e630SAndre Oppermann 		}
1165a160e630SAndre Oppermann 		/*
1166a160e630SAndre Oppermann 		 * We can't do anything without SYN.
1167a160e630SAndre Oppermann 		 */
1168a160e630SAndre Oppermann 		if ((thflags & TH_SYN) == 0) {
1169a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1170a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1171773673c1SAndre Oppermann 				    "SYN is missing, segment ignored\n",
11728d573cc1SAndre Oppermann 				    s, __func__);
117378b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1174a160e630SAndre Oppermann 			goto dropunlock;
1175a160e630SAndre Oppermann 		}
1176a160e630SAndre Oppermann 		/*
1177a160e630SAndre Oppermann 		 * (SYN|ACK) is bogus on a listen socket.
1178a160e630SAndre Oppermann 		 */
1179fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1180a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1181a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
11828d573cc1SAndre Oppermann 				    "SYN|ACK invalid, segment rejected\n",
11838d573cc1SAndre Oppermann 				    s, __func__);
1184a160e630SAndre Oppermann 			syncache_badack(&inc);	/* XXX: Not needed! */
118578b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1186a57815efSBosko Milekic 			rstreason = BANDLIM_RST_OPENPORT;
1187a57815efSBosko Milekic 			goto dropwithreset;
11884195b4afSPaul Traina 		}
1189a160e630SAndre Oppermann 		/*
1190a160e630SAndre Oppermann 		 * If the drop_synfin option is enabled, drop all
1191a160e630SAndre Oppermann 		 * segments with both the SYN and FIN bits set.
1192a160e630SAndre Oppermann 		 * This prevents e.g. nmap from identifying the
1193a160e630SAndre Oppermann 		 * TCP/IP stack.
1194a160e630SAndre Oppermann 		 * XXX: Poor reasoning.  nmap has other methods
1195a160e630SAndre Oppermann 		 * and is constantly refining its stack detection
1196a160e630SAndre Oppermann 		 * strategies.
11978d573cc1SAndre Oppermann 		 * XXX: This is a violation of the TCP specification
1198a160e630SAndre Oppermann 		 * and was used by RFC1644.
1199a160e630SAndre Oppermann 		 */
1200603724d3SBjoern A. Zeeb 		if ((thflags & TH_FIN) && V_drop_synfin) {
1201a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1202a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1203773673c1SAndre Oppermann 				    "SYN|FIN segment ignored (based on "
12048d573cc1SAndre Oppermann 				    "sysctl setting)\n", s, __func__);
120578b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1206302ce8d6SAndre Oppermann 			goto dropunlock;
1207ebb0cbeaSPaul Traina 		}
1208be2ac88cSJonathan Lemon 		/*
1209be2ac88cSJonathan Lemon 		 * Segment's flags are (SYN) or (SYN|FIN).
1210a160e630SAndre Oppermann 		 *
1211a160e630SAndre Oppermann 		 * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored
1212a160e630SAndre Oppermann 		 * as they do not affect the state of the TCP FSM.
1213a160e630SAndre Oppermann 		 * The data pointed to by TH_URG and th_urp is ignored.
1214be2ac88cSJonathan Lemon 		 */
1215a160e630SAndre Oppermann 		KASSERT((thflags & (TH_RST|TH_ACK)) == 0,
1216a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_RST or TH_ACK set", __func__));
1217a160e630SAndre Oppermann 		KASSERT(thflags & (TH_SYN),
1218a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_SYN not set", __func__));
121933841545SHajimu UMEMOTO #ifdef INET6
122033841545SHajimu UMEMOTO 		/*
122133841545SHajimu UMEMOTO 		 * If deprecated address is forbidden,
122233841545SHajimu UMEMOTO 		 * we do not accept SYN to deprecated interface
122333841545SHajimu UMEMOTO 		 * address to prevent any new inbound connection from
122433841545SHajimu UMEMOTO 		 * getting established.
122533841545SHajimu UMEMOTO 		 * When we do not accept SYN, we send a TCP RST,
122633841545SHajimu UMEMOTO 		 * with deprecated source address (instead of dropping
122733841545SHajimu UMEMOTO 		 * it).  We compromise it as it is much better for peer
122833841545SHajimu UMEMOTO 		 * to send a RST, and RST will be the final packet
122933841545SHajimu UMEMOTO 		 * for the exchange.
123033841545SHajimu UMEMOTO 		 *
123133841545SHajimu UMEMOTO 		 * If we do not forbid deprecated addresses, we accept
123233841545SHajimu UMEMOTO 		 * the SYN packet.  RFC2462 does not suggest dropping
123333841545SHajimu UMEMOTO 		 * SYN in this case.
123433841545SHajimu UMEMOTO 		 * If we decipher RFC2462 5.5.4, it says like this:
123533841545SHajimu UMEMOTO 		 * 1. use of deprecated addr with existing
123633841545SHajimu UMEMOTO 		 *    communication is okay - "SHOULD continue to be
123733841545SHajimu UMEMOTO 		 *    used"
123833841545SHajimu UMEMOTO 		 * 2. use of it with new communication:
123933841545SHajimu UMEMOTO 		 *   (2a) "SHOULD NOT be used if alternate address
124033841545SHajimu UMEMOTO 		 *        with sufficient scope is available"
124133841545SHajimu UMEMOTO 		 *   (2b) nothing mentioned otherwise.
124233841545SHajimu UMEMOTO 		 * Here we fall into (2b) case as we have no choice in
124333841545SHajimu UMEMOTO 		 * our source address selection - we must obey the peer.
124433841545SHajimu UMEMOTO 		 *
124533841545SHajimu UMEMOTO 		 * The wording in RFC2462 is confusing, and there are
124633841545SHajimu UMEMOTO 		 * multiple description text for deprecated address
124733841545SHajimu UMEMOTO 		 * handling - worse, they are not exactly the same.
124833841545SHajimu UMEMOTO 		 * I believe 5.5.4 is the best one, so we follow 5.5.4.
124933841545SHajimu UMEMOTO 		 */
1250603724d3SBjoern A. Zeeb 		if (isipv6 && !V_ip6_use_deprecated) {
125133841545SHajimu UMEMOTO 			struct in6_ifaddr *ia6;
125233841545SHajimu UMEMOTO 
12533e88eb90SAndrey V. Elsukov 			ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
12548c0fec80SRobert Watson 			if (ia6 != NULL &&
125533841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
12568c0fec80SRobert Watson 				ifa_free(&ia6->ia_ifa);
1257a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1258a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12598d573cc1SAndre Oppermann 					"Connection attempt to deprecated "
12608d573cc1SAndre Oppermann 					"IPv6 address rejected\n",
1261a160e630SAndre Oppermann 					s, __func__);
126233841545SHajimu UMEMOTO 				rstreason = BANDLIM_RST_OPENPORT;
126333841545SHajimu UMEMOTO 				goto dropwithreset;
126433841545SHajimu UMEMOTO 			}
126577d396fdSMaksim Yevmenkin 			if (ia6)
12668c0fec80SRobert Watson 				ifa_free(&ia6->ia_ifa);
126733841545SHajimu UMEMOTO 		}
1268b287c6c7SBjoern A. Zeeb #endif /* INET6 */
126965f28919SJesper Skriver 		/*
1270db33b3e6SAndre Oppermann 		 * Basic sanity checks on incoming SYN requests:
12718d573cc1SAndre Oppermann 		 *   Don't respond if the destination is a link layer
1272db33b3e6SAndre Oppermann 		 *	broadcast according to RFC1122 4.2.3.10, p. 104.
12738d573cc1SAndre Oppermann 		 *   If it is from this socket it must be forged.
12748d573cc1SAndre Oppermann 		 *   Don't respond if the source or destination is a
12758d573cc1SAndre Oppermann 		 *	global or subnet broad- or multicast address.
127693ec91baSCrist J. Clark 		 *   Note that it is quite possible to receive unicast
127793ec91baSCrist J. Clark 		 *	link-layer packets with a broadcast IP address. Use
127893ec91baSCrist J. Clark 		 *	in_broadcast() to find them.
1279be2ac88cSJonathan Lemon 		 */
12808d573cc1SAndre Oppermann 		if (m->m_flags & (M_BCAST|M_MCAST)) {
12818d573cc1SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
12828d573cc1SAndre Oppermann 			    log(LOG_DEBUG, "%s; %s: Listen socket: "
12838d573cc1SAndre Oppermann 				"Connection attempt from broad- or multicast "
1284773673c1SAndre Oppermann 				"link layer address ignored\n", s, __func__);
1285302ce8d6SAndre Oppermann 			goto dropunlock;
12868d573cc1SAndre Oppermann 		}
1287db33b3e6SAndre Oppermann #ifdef INET6
1288b287c6c7SBjoern A. Zeeb 		if (isipv6) {
1289db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1290a160e630SAndre Oppermann 			    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) {
1291a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1292a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12938d573cc1SAndre Oppermann 					"Connection attempt to/from self "
1294773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1295302ce8d6SAndre Oppermann 				goto dropunlock;
1296a160e630SAndre Oppermann 			}
1297be2ac88cSJonathan Lemon 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1298a160e630SAndre Oppermann 			    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
1299a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1300a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13018d573cc1SAndre Oppermann 					"Connection attempt from/to multicast "
1302773673c1SAndre Oppermann 					"address ignored\n", s, __func__);
1303302ce8d6SAndre Oppermann 				goto dropunlock;
1304a160e630SAndre Oppermann 			}
1305b287c6c7SBjoern A. Zeeb 		}
1306db33b3e6SAndre Oppermann #endif
1307b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
1308b287c6c7SBjoern A. Zeeb 		else
1309b287c6c7SBjoern A. Zeeb #endif
1310b287c6c7SBjoern A. Zeeb #ifdef INET
1311b287c6c7SBjoern A. Zeeb 		{
1312db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1313a160e630SAndre Oppermann 			    ip->ip_dst.s_addr == ip->ip_src.s_addr) {
1314a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1315a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13168d573cc1SAndre Oppermann 					"Connection attempt from/to self "
1317773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1318302ce8d6SAndre Oppermann 				goto dropunlock;
1319a160e630SAndre Oppermann 			}
1320be2ac88cSJonathan Lemon 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
1321be2ac88cSJonathan Lemon 			    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
13222ca2159fSCrist J. Clark 			    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
1323a160e630SAndre Oppermann 			    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
1324a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1325a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13268d573cc1SAndre Oppermann 					"Connection attempt from/to broad- "
1327773673c1SAndre Oppermann 					"or multicast address ignored\n",
1328a160e630SAndre Oppermann 					s, __func__);
1329302ce8d6SAndre Oppermann 				goto dropunlock;
1330c068736aSJeffrey Hsu 			}
1331a160e630SAndre Oppermann 		}
1332b287c6c7SBjoern A. Zeeb #endif
1333be2ac88cSJonathan Lemon 		/*
1334db33b3e6SAndre Oppermann 		 * SYN appears to be valid.  Create compressed TCP state
1335db33b3e6SAndre Oppermann 		 * for syncache.
1336be2ac88cSJonathan Lemon 		 */
13373c653157SHartmut Brandt #ifdef TCPDEBUG
13383c653157SHartmut Brandt 		if (so->so_options & SO_DEBUG)
13393c653157SHartmut Brandt 			tcp_trace(TA_INPUT, ostate, tp,
13403c653157SHartmut Brandt 			    (void *)tcp_saveipgen, &tcp_savetcp, 0);
13413c653157SHartmut Brandt #endif
13422b9c9984SGeorge V. Neville-Neil 		TCP_PROBE3(debug__input, tp, th, m);
1343f72167f4SAndre Oppermann 		tcp_dooptions(&to, optp, optlen, TO_SYN);
1344fa49a964SMichael Tuexen 		if (syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL, iptos))
134509c305ebSPatrick Kelsey 			goto tfo_socket_result;
134618a75309SPatrick Kelsey 
1347be2ac88cSJonathan Lemon 		/*
13484d6e7130SAndre Oppermann 		 * Entry added to syncache and mbuf consumed.
1349a7c7f2a7SJohn Baldwin 		 * Only the listen socket is unlocked by syncache_add().
1350be2ac88cSJonathan Lemon 		 */
1351384a5c3cSAndrey V. Elsukov 		INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo);
13528f5a8818SKevin Lo 		return (IPPROTO_DONE);
1353982c1675SAndre Oppermann 	} else if (tp->t_state == TCPS_LISTEN) {
1354982c1675SAndre Oppermann 		/*
1355982c1675SAndre Oppermann 		 * When a listen socket is torn down the SO_ACCEPTCONN
1356982c1675SAndre Oppermann 		 * flag is removed first while connections are drained
1357982c1675SAndre Oppermann 		 * from the accept queue in a unlock/lock cycle of the
1358982c1675SAndre Oppermann 		 * ACCEPT_LOCK, opening a race condition allowing a SYN
1359982c1675SAndre Oppermann 		 * attempt go through unhandled.
1360982c1675SAndre Oppermann 		 */
1361982c1675SAndre Oppermann 		goto dropunlock;
1362f76fcf6dSJeffrey Hsu 	}
1363fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1364fcf59617SAndrey V. Elsukov 	if (tp->t_flags & TF_SIGNATURE) {
1365fcf59617SAndrey V. Elsukov 		tcp_dooptions(&to, optp, optlen, thflags);
1366fcf59617SAndrey V. Elsukov 		if ((to.to_flags & TOF_SIGNATURE) == 0) {
1367fcf59617SAndrey V. Elsukov 			TCPSTAT_INC(tcps_sig_err_nosigopt);
13682903309aSAttilio Rao 			goto dropunlock;
13692903309aSAttilio Rao 		}
1370fcf59617SAndrey V. Elsukov 		if (!TCPMD5_ENABLED() ||
1371fcf59617SAndrey V. Elsukov 		    TCPMD5_INPUT(m, th, to.to_signature) != 0)
1372fcf59617SAndrey V. Elsukov 			goto dropunlock;
13732903309aSAttilio Rao 	}
13742903309aSAttilio Rao #endif
13752b9c9984SGeorge V. Neville-Neil 	TCP_PROBE5(receive, NULL, tp, m, tp, th);
137657f60867SMark Johnston 
1377302ce8d6SAndre Oppermann 	/*
13788d573cc1SAndre Oppermann 	 * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later
13798d573cc1SAndre Oppermann 	 * state.  tcp_do_segment() always consumes the mbuf chain, unlocks
13808d573cc1SAndre Oppermann 	 * the inpcb, and unlocks pcbinfo.
1381302ce8d6SAndre Oppermann 	 */
13826573d758SMatt Macy 	tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, 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);
1390014ea782SRobert Watson 		INP_WUNLOCK(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)
14018501a69cSRobert Watson 		INP_WUNLOCK(inp);
1402014ea782SRobert Watson 
1403302ce8d6SAndre Oppermann drop:
1404384a5c3cSAndrey V. Elsukov 	INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo);
1405a160e630SAndre Oppermann 	if (s != NULL)
1406a160e630SAndre Oppermann 		free(s, M_TCPLOG);
1407302ce8d6SAndre Oppermann 	if (m != NULL)
1408302ce8d6SAndre Oppermann 		m_freem(m);
14098f5a8818SKevin Lo 	return (IPPROTO_DONE);
1410302ce8d6SAndre Oppermann }
1411302ce8d6SAndre Oppermann 
1412e44c1887SSteven Hartland /*
1413e44c1887SSteven Hartland  * Automatic sizing of receive socket buffer.  Often the send
1414e44c1887SSteven Hartland  * buffer size is not optimally adjusted to the actual network
1415e44c1887SSteven Hartland  * conditions at hand (delay bandwidth product).  Setting the
1416e44c1887SSteven Hartland  * buffer size too small limits throughput on links with high
1417e44c1887SSteven Hartland  * bandwidth and high delay (eg. trans-continental/oceanic links).
1418e44c1887SSteven Hartland  *
1419e44c1887SSteven Hartland  * On the receive side the socket buffer memory is only rarely
1420e44c1887SSteven Hartland  * used to any significant extent.  This allows us to be much
1421e44c1887SSteven Hartland  * more aggressive in scaling the receive socket buffer.  For
1422e44c1887SSteven Hartland  * the case that the buffer space is actually used to a large
1423e44c1887SSteven Hartland  * extent and we run out of kernel memory we can simply drop
1424e44c1887SSteven Hartland  * the new segments; TCP on the sender will just retransmit it
1425e44c1887SSteven Hartland  * later.  Setting the buffer size too big may only consume too
1426e44c1887SSteven Hartland  * much kernel memory if the application doesn't read() from
1427e44c1887SSteven Hartland  * the socket or packet loss or reordering makes use of the
1428e44c1887SSteven Hartland  * reassembly queue.
1429e44c1887SSteven Hartland  *
1430e44c1887SSteven Hartland  * The criteria to step up the receive buffer one notch are:
1431e44c1887SSteven Hartland  *  1. Application has not set receive buffer size with
1432e44c1887SSteven Hartland  *     SO_RCVBUF. Setting SO_RCVBUF clears SB_AUTOSIZE.
1433560c0586SMichael Tuexen  *  2. the number of bytes received during 1/2 of an sRTT
1434560c0586SMichael Tuexen  *     is at least 3/8 of the current socket buffer size.
1435560c0586SMichael Tuexen  *  3. receive buffer size has not hit maximal automatic size;
1436e44c1887SSteven Hartland  *
1437560c0586SMichael Tuexen  * If all of the criteria are met we increaset the socket buffer
1438560c0586SMichael Tuexen  * by a 1/2 (bounded by the max). This allows us to keep ahead
1439560c0586SMichael Tuexen  * of slow-start but also makes it so our peer never gets limited
1440560c0586SMichael Tuexen  * by our rwnd which we then open up causing a burst.
1441560c0586SMichael Tuexen  *
1442560c0586SMichael Tuexen  * This algorithm does two steps per RTT at most and only if
1443e44c1887SSteven Hartland  * we receive a bulk stream w/o packet losses or reorderings.
1444e44c1887SSteven Hartland  * Shrinking the buffer during idle times is not necessary as
1445e44c1887SSteven Hartland  * it doesn't consume any memory when idle.
1446e44c1887SSteven Hartland  *
1447e44c1887SSteven Hartland  * TODO: Only step up if the application is actually serving
1448e44c1887SSteven Hartland  * the buffer to better manage the socket buffer resources.
1449e44c1887SSteven Hartland  */
1450e44c1887SSteven Hartland int
1451e44c1887SSteven Hartland tcp_autorcvbuf(struct mbuf *m, struct tcphdr *th, struct socket *so,
1452e44c1887SSteven Hartland     struct tcpcb *tp, int tlen)
1453e44c1887SSteven Hartland {
1454e44c1887SSteven Hartland 	int newsize = 0;
1455e44c1887SSteven Hartland 
1456e44c1887SSteven Hartland 	if (V_tcp_do_autorcvbuf && (so->so_rcv.sb_flags & SB_AUTOSIZE) &&
1457e44c1887SSteven Hartland 	    tp->t_srtt != 0 && tp->rfbuf_ts != 0 &&
1458e44c1887SSteven Hartland 	    TCP_TS_TO_TICKS(tcp_ts_getticks() - tp->rfbuf_ts) >
1459560c0586SMichael Tuexen 	    ((tp->t_srtt >> TCP_RTT_SHIFT)/2)) {
1460560c0586SMichael Tuexen 		if (tp->rfbuf_cnt > ((so->so_rcv.sb_hiwat / 2)/ 4 * 3) &&
1461e44c1887SSteven Hartland 		    so->so_rcv.sb_hiwat < V_tcp_autorcvbuf_max) {
1462560c0586SMichael Tuexen 			newsize = min((so->so_rcv.sb_hiwat + (so->so_rcv.sb_hiwat/2)), V_tcp_autorcvbuf_max);
1463e44c1887SSteven Hartland 		}
1464e44c1887SSteven Hartland 		TCP_PROBE6(receive__autoresize, NULL, tp, m, tp, th, newsize);
1465e44c1887SSteven Hartland 
1466e44c1887SSteven Hartland 		/* Start over with next RTT. */
1467e44c1887SSteven Hartland 		tp->rfbuf_ts = 0;
1468e44c1887SSteven Hartland 		tp->rfbuf_cnt = 0;
1469e44c1887SSteven Hartland 	} else {
1470e44c1887SSteven Hartland 		tp->rfbuf_cnt += tlen;	/* add up */
1471e44c1887SSteven Hartland 	}
1472e44c1887SSteven Hartland 	return (newsize);
1473e44c1887SSteven Hartland }
1474e44c1887SSteven Hartland 
147555bceb1eSRandall Stewart void
14764d0770f1SRichard Scheffenegger tcp_handle_wakeup(struct tcpcb *tp, struct socket *so)
14774d0770f1SRichard Scheffenegger {
14784d0770f1SRichard Scheffenegger 	/*
14794d0770f1SRichard Scheffenegger 	 * Since tp might be gone if the session entered
14804d0770f1SRichard Scheffenegger 	 * the TIME_WAIT state before coming here, we need
14814d0770f1SRichard Scheffenegger 	 * to check if the socket is still connected.
14824d0770f1SRichard Scheffenegger 	 */
14834d0770f1SRichard Scheffenegger 	if ((so->so_state & SS_ISCONNECTED) == 0)
14844d0770f1SRichard Scheffenegger 		return;
14854d0770f1SRichard Scheffenegger 	INP_LOCK_ASSERT(tp->t_inpcb);
14864d0770f1SRichard Scheffenegger 	if (tp->t_flags & TF_WAKESOR) {
14874d0770f1SRichard Scheffenegger 		tp->t_flags &= ~TF_WAKESOR;
14884d0770f1SRichard Scheffenegger 		SOCKBUF_UNLOCK_ASSERT(&so->so_rcv);
14894d0770f1SRichard Scheffenegger 		sorwakeup(so);
14904d0770f1SRichard Scheffenegger 	}
14914d0770f1SRichard Scheffenegger 	if (tp->t_flags & TF_WAKESOW) {
14924d0770f1SRichard Scheffenegger 		tp->t_flags &= ~TF_WAKESOW;
14934d0770f1SRichard Scheffenegger 		SOCKBUF_UNLOCK_ASSERT(&so->so_snd);
14944d0770f1SRichard Scheffenegger 		sowwakeup(so);
14954d0770f1SRichard Scheffenegger 	}
14964d0770f1SRichard Scheffenegger }
14974d0770f1SRichard Scheffenegger 
14984d0770f1SRichard Scheffenegger void
1499302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so,
15006573d758SMatt Macy     struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos)
1501302ce8d6SAndre Oppermann {
1502021eaf79SHiren Panchasara 	int thflags, acked, ourfinisacked, needoutput = 0, sack_changed;
1503b2ade6b1SRichard Scheffenegger 	int rstreason, todrop, win, incforsyn = 0;
15043ac12506SJonathan T. Looney 	uint32_t tiwin;
15054b7b743cSLawrence Stewart 	uint16_t nsegs;
150607dacf03SAndre Oppermann 	char *s;
150707dacf03SAndre Oppermann 	struct in_conninfo *inc;
1508c11a15bfSGleb Smirnoff 	struct mbuf *mfree;
1509302ce8d6SAndre Oppermann 	struct tcpopt to;
1510281a0fd4SPatrick Kelsey 	int tfo_syn;
1511302ce8d6SAndre Oppermann 
151214739780SMaxim Konovalov #ifdef TCPDEBUG
151314739780SMaxim Konovalov 	/*
151414739780SMaxim Konovalov 	 * The size of tcp_saveipgen must be the size of the max ip header,
151514739780SMaxim Konovalov 	 * now IPv6.
151614739780SMaxim Konovalov 	 */
151714739780SMaxim Konovalov 	u_char tcp_saveipgen[IP6_HDR_LEN];
151814739780SMaxim Konovalov 	struct tcphdr tcp_savetcp;
151914739780SMaxim Konovalov 	short ostate = 0;
152014739780SMaxim Konovalov #endif
1521302ce8d6SAndre Oppermann 	thflags = th->th_flags;
152207dacf03SAndre Oppermann 	inc = &tp->t_inpcb->inp_inc;
1523d64a46eaSLawrence Stewart 	tp->sackhint.last_sack_ack = 0;
1524021eaf79SHiren Panchasara 	sack_changed = 0;
15254b7b743cSLawrence Stewart 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
1526d40c0d47SGleb Smirnoff 
1527d40c0d47SGleb Smirnoff 	NET_EPOCH_ASSERT();
15288501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1529679d9708SAndre Oppermann 	KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
1530679d9708SAndre Oppermann 	    __func__));
1531679d9708SAndre Oppermann 	KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
1532679d9708SAndre Oppermann 	    __func__));
1533df8bae1dSRodney W. Grimes 
153486a996e6SHiren Panchasara #ifdef TCPPCAP
153586a996e6SHiren Panchasara 	/* Save segment, if requested. */
153686a996e6SHiren Panchasara 	tcp_pcap_add(th, m, &(tp->t_inpkts));
153786a996e6SHiren Panchasara #endif
15382529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0,
15392529f56eSJonathan T. Looney 	    tlen, NULL, true);
154086a996e6SHiren Panchasara 
1541013f4df6SMichael Tuexen 	if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
1542013f4df6SMichael Tuexen 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
1543013f4df6SMichael Tuexen 			log(LOG_DEBUG, "%s; %s: "
1544013f4df6SMichael Tuexen 			    "SYN|FIN segment ignored (based on "
1545013f4df6SMichael Tuexen 			    "sysctl setting)\n", s, __func__);
1546013f4df6SMichael Tuexen 			free(s, M_TCPLOG);
1547013f4df6SMichael Tuexen 		}
1548013f4df6SMichael Tuexen 		goto drop;
1549013f4df6SMichael Tuexen 	}
1550013f4df6SMichael Tuexen 
1551df8bae1dSRodney W. Grimes 	/*
1552ebfd7534SMichael Tuexen 	 * If a segment with the ACK-bit set arrives in the SYN-SENT state
1553ebfd7534SMichael Tuexen 	 * check SEQ.ACK first.
1554ebfd7534SMichael Tuexen 	 */
1555ebfd7534SMichael Tuexen 	if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) &&
1556ebfd7534SMichael Tuexen 	    (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) {
1557ebfd7534SMichael Tuexen 		rstreason = BANDLIM_UNLIMITED;
1558ebfd7534SMichael Tuexen 		goto dropwithreset;
1559ebfd7534SMichael Tuexen 	}
1560ebfd7534SMichael Tuexen 
1561ebfd7534SMichael Tuexen 	/*
1562df8bae1dSRodney W. Grimes 	 * Segment received on connection.
1563df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
1564e8949f74SAndre Oppermann 	 * XXX: This should be done after segment
1565e8949f74SAndre Oppermann 	 * validation to ignore broken/spoofed segs.
1566df8bae1dSRodney W. Grimes 	 */
15679b8b58e0SJonathan Lemon 	tp->t_rcvtime = ticks;
1568df8bae1dSRodney W. Grimes 
1569df8bae1dSRodney W. Grimes 	/*
1570c52102ddSHiren Panchasara 	 * Scale up the window into a 32-bit value.
1571e8949f74SAndre Oppermann 	 * For the SYN_SENT state the scale is zero.
1572464fcfbcSAndre Oppermann 	 */
1573464fcfbcSAndre Oppermann 	tiwin = th->th_win << tp->snd_scale;
1574adc56f5aSEdward Tomasz Napierala #ifdef STATS
1575adc56f5aSEdward Tomasz Napierala 	stats_voi_update_abs_ulong(tp->t_stats, VOI_TCP_FRWIN, tiwin);
1576adc56f5aSEdward Tomasz Napierala #endif
1577464fcfbcSAndre Oppermann 
1578464fcfbcSAndre Oppermann 	/*
1579f2512ba1SRui Paulo 	 * TCP ECN processing.
1580f2512ba1SRui Paulo 	 */
15813cf38784SMichael Tuexen 	if (tp->t_flags2 & TF2_ECN_PERMIT) {
1582ee97681eSMichael Tuexen 		if (thflags & TH_CWR) {
15833cf38784SMichael Tuexen 			tp->t_flags2 &= ~TF2_ECN_SND_ECE;
1584ee97681eSMichael Tuexen 			tp->t_flags |= TF_ACKNOW;
1585ee97681eSMichael Tuexen 		}
1586f2512ba1SRui Paulo 		switch (iptos & IPTOS_ECN_MASK) {
1587f2512ba1SRui Paulo 		case IPTOS_ECN_CE:
15883cf38784SMichael Tuexen 			tp->t_flags2 |= TF2_ECN_SND_ECE;
158978b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ce);
1590f2512ba1SRui Paulo 			break;
1591f2512ba1SRui Paulo 		case IPTOS_ECN_ECT0:
159278b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ect0);
1593f2512ba1SRui Paulo 			break;
1594f2512ba1SRui Paulo 		case IPTOS_ECN_ECT1:
159578b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ect1);
1596f2512ba1SRui Paulo 			break;
1597f2512ba1SRui Paulo 		}
159864807b30SHiren Panchasara 
159964807b30SHiren Panchasara 		/* Process a packet differently from RFC3168. */
160064807b30SHiren Panchasara 		cc_ecnpkt_handler(tp, th, iptos);
160164807b30SHiren Panchasara 
1602dbc42409SLawrence Stewart 		/* Congestion experienced. */
1603dbc42409SLawrence Stewart 		if (thflags & TH_ECE) {
1604dbc42409SLawrence Stewart 			cc_cong_signal(tp, th, CC_ECN);
1605f2512ba1SRui Paulo 		}
1606f2512ba1SRui Paulo 	}
1607f2512ba1SRui Paulo 
1608f2512ba1SRui Paulo 	/*
1609f72167f4SAndre Oppermann 	 * Parse options on any incoming segment.
1610f72167f4SAndre Oppermann 	 */
1611302ce8d6SAndre Oppermann 	tcp_dooptions(&to, (u_char *)(th + 1),
1612302ce8d6SAndre Oppermann 	    (th->th_off << 2) - sizeof(struct tcphdr),
1613302ce8d6SAndre Oppermann 	    (thflags & TH_SYN) ? TO_SYN : 0);
1614f72167f4SAndre Oppermann 
1615fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1616fcf59617SAndrey V. Elsukov 	if ((tp->t_flags & TF_SIGNATURE) != 0 &&
1617fcf59617SAndrey V. Elsukov 	    (to.to_flags & TOF_SIGNATURE) == 0) {
1618fcf59617SAndrey V. Elsukov 		TCPSTAT_INC(tcps_sig_err_sigopt);
1619fcf59617SAndrey V. Elsukov 		/* XXX: should drop? */
1620fcf59617SAndrey V. Elsukov 	}
1621fcf59617SAndrey V. Elsukov #endif
1622f72167f4SAndre Oppermann 	/*
1623f72167f4SAndre Oppermann 	 * If echoed timestamp is later than the current time,
1624bf6d304aSAndre Oppermann 	 * fall back to non RFC1323 RTT calculation.  Normalize
1625bf6d304aSAndre Oppermann 	 * timestamp if syncookies were used when this connection
1626bf6d304aSAndre Oppermann 	 * was established.
1627f72167f4SAndre Oppermann 	 */
1628bf6d304aSAndre Oppermann 	if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
1629e16fa5caSJohn-Mark Gurney 		to.to_tsecr -= tp->ts_offset;
1630d8951c8aSBjoern A. Zeeb 		if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks()))
1631f72167f4SAndre Oppermann 			to.to_tsecr = 0;
163210d20c84SMatt Macy 		else if (tp->t_flags & TF_PREVVALID &&
163310d20c84SMatt Macy 			 tp->t_badrxtwin != 0 && SEQ_LT(to.to_tsecr, tp->t_badrxtwin))
163410d20c84SMatt Macy 			cc_cong_signal(tp, th, CC_RTO_ERR);
1635bf6d304aSAndre Oppermann 	}
163607dacf03SAndre Oppermann 	/*
163797d8d152SAndre Oppermann 	 * Process options only when we get SYN/ACK back. The SYN case
163897d8d152SAndre Oppermann 	 * for incoming connections is handled in tcp_syncache.
16395396d0f8SAndre Oppermann 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN>
16405396d0f8SAndre Oppermann 	 * or <SYN,ACK>) segment itself is never scaled.
164197d8d152SAndre Oppermann 	 * XXX this is traditional behavior, may need to be cleaned up.
1642df8bae1dSRodney W. Grimes 	 */
16430a389eabSSimon L. B. Nielsen 	if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
16446e16d877SRichard Scheffenegger 		/* Handle parallel SYN for ECN */
16456e16d877SRichard Scheffenegger 		if (!(thflags & TH_ACK) &&
16466e16d877SRichard Scheffenegger 		    ((thflags & (TH_CWR | TH_ECE)) == (TH_CWR | TH_ECE)) &&
16476e16d877SRichard Scheffenegger 		    ((V_tcp_do_ecn == 1) || (V_tcp_do_ecn == 2))) {
16486e16d877SRichard Scheffenegger 			tp->t_flags2 |= TF2_ECN_PERMIT;
16496e16d877SRichard Scheffenegger 			tp->t_flags2 |= TF2_ECN_SND_ECE;
16506e16d877SRichard Scheffenegger 			TCPSTAT_INC(tcps_ecn_shs);
16516e16d877SRichard Scheffenegger 		}
1652464fcfbcSAndre Oppermann 		if ((to.to_flags & TOF_SCALE) &&
1653464fcfbcSAndre Oppermann 		    (tp->t_flags & TF_REQ_SCALE)) {
1654be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_SCALE;
165502a1a643SAndre Oppermann 			tp->snd_scale = to.to_wscale;
16568e051165SRichard Scheffenegger 		} else
16578e051165SRichard Scheffenegger 			tp->t_flags &= ~TF_REQ_SCALE;
16585396d0f8SAndre Oppermann 		/*
16595396d0f8SAndre Oppermann 		 * Initial send window.  It will be updated with
16605396d0f8SAndre Oppermann 		 * the next incoming segment to the scaled value.
16615396d0f8SAndre Oppermann 		 */
16625396d0f8SAndre Oppermann 		tp->snd_wnd = th->th_win;
16638e051165SRichard Scheffenegger 		if ((to.to_flags & TOF_TS) &&
16648e051165SRichard Scheffenegger 		    (tp->t_flags & TF_REQ_TSTMP)) {
1665be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_TSTMP;
1666be2ac88cSJonathan Lemon 			tp->ts_recent = to.to_tsval;
1667d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
16688e051165SRichard Scheffenegger 		} else
16698e051165SRichard Scheffenegger 			tp->t_flags &= ~TF_REQ_TSTMP;
1670be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_MSS)
1671be2ac88cSJonathan Lemon 			tcp_mss(tp, to.to_mss);
16723529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
16733529149eSAndre Oppermann 		    (to.to_flags & TOF_SACKPERM) == 0)
16743529149eSAndre Oppermann 			tp->t_flags &= ~TF_SACK_PERMIT;
1675c560df6fSPatrick Kelsey 		if (IS_FASTOPEN(tp->t_flags)) {
1676a026a53aSMichael Tuexen 			if (to.to_flags & TOF_FASTOPEN) {
1677a026a53aSMichael Tuexen 				uint16_t mss;
1678a026a53aSMichael Tuexen 
1679a026a53aSMichael Tuexen 				if (to.to_flags & TOF_MSS)
1680a026a53aSMichael Tuexen 					mss = to.to_mss;
1681c560df6fSPatrick Kelsey 				else
1682a026a53aSMichael Tuexen 					if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
1683a026a53aSMichael Tuexen 						mss = TCP6_MSS;
1684a026a53aSMichael Tuexen 					else
1685a026a53aSMichael Tuexen 						mss = TCP_MSS;
1686a026a53aSMichael Tuexen 				tcp_fastopen_update_cache(tp, mss,
1687a026a53aSMichael Tuexen 				    to.to_tfo_len, to.to_tfo_cookie);
1688a026a53aSMichael Tuexen 			} else
1689c560df6fSPatrick Kelsey 				tcp_fastopen_disable_path(tp);
1690c560df6fSPatrick Kelsey 		}
16916d90faf3SPaul Saab 	}
16926d90faf3SPaul Saab 
1693df8bae1dSRodney W. Grimes 	/*
1694283c76c7SMichael Tuexen 	 * If timestamps were negotiated during SYN/ACK and a
1695283c76c7SMichael Tuexen 	 * segment without a timestamp is received, silently drop
1696d2b3ceddSMichael Tuexen 	 * the segment, unless it is a RST segment or missing timestamps are
1697d2b3ceddSMichael Tuexen 	 * tolerated.
1698283c76c7SMichael Tuexen 	 * See section 3.2 of RFC 7323.
16994da9052aSMichael Tuexen 	 */
17004da9052aSMichael Tuexen 	if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) {
1701d2b3ceddSMichael Tuexen 		if (((thflags & TH_RST) != 0) || V_tcp_tolerate_missing_ts) {
1702cc3c3485SMichael Tuexen 			if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
1703cc3c3485SMichael Tuexen 				log(LOG_DEBUG, "%s; %s: Timestamp missing, "
1704cc3c3485SMichael Tuexen 				    "segment processed normally\n",
1705cc3c3485SMichael Tuexen 				    s, __func__);
1706cc3c3485SMichael Tuexen 				free(s, M_TCPLOG);
1707cc3c3485SMichael Tuexen 			}
1708cc3c3485SMichael Tuexen 		} else {
17094da9052aSMichael Tuexen 			if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
17104da9052aSMichael Tuexen 				log(LOG_DEBUG, "%s; %s: Timestamp missing, "
1711283c76c7SMichael Tuexen 				    "segment silently dropped\n", s, __func__);
17124da9052aSMichael Tuexen 				free(s, M_TCPLOG);
17134da9052aSMichael Tuexen 			}
1714283c76c7SMichael Tuexen 			goto drop;
17154da9052aSMichael Tuexen 		}
1716cc3c3485SMichael Tuexen 	}
1717283c76c7SMichael Tuexen 	/*
1718283c76c7SMichael Tuexen 	 * If timestamps were not negotiated during SYN/ACK and a
171975fcd27aSMichael Tuexen 	 * segment with a timestamp is received, ignore the
1720283c76c7SMichael Tuexen 	 * timestamp and process the packet normally.
1721283c76c7SMichael Tuexen 	 * See section 3.2 of RFC 7323.
1722283c76c7SMichael Tuexen 	 */
17234da9052aSMichael Tuexen 	if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) {
17244da9052aSMichael Tuexen 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
17254da9052aSMichael Tuexen 			log(LOG_DEBUG, "%s; %s: Timestamp not expected, "
1726283c76c7SMichael Tuexen 			    "segment processed normally\n", s, __func__);
17274da9052aSMichael Tuexen 			free(s, M_TCPLOG);
17284da9052aSMichael Tuexen 		}
17294da9052aSMichael Tuexen 	}
17304da9052aSMichael Tuexen 
17314da9052aSMichael Tuexen 	/*
1732df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
1733df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
1734df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
1735df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
1736df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
1737df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
1738df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
1739df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
1740df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
1741df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
1742df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
1743df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
1744a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
1745c5ad39b9SAndre Oppermann 	 * Since we check for TCPS_ESTABLISHED first, it can only
1746a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
1747df8bae1dSRodney W. Grimes 	 */
1748df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
1749c5ad39b9SAndre Oppermann 	    th->th_seq == tp->rcv_nxt &&
1750fb59c426SYoshinobu Inoue 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1751c5ad39b9SAndre Oppermann 	    tp->snd_nxt == tp->snd_max &&
1752c5ad39b9SAndre Oppermann 	    tiwin && tiwin == tp->snd_wnd &&
1753a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
1754c28440dbSRandall Stewart 	    SEGQ_EMPTY(tp) &&
17554741bfcbSPatrick Kelsey 	    ((to.to_flags & TOF_TS) == 0 ||
1756c5ad39b9SAndre Oppermann 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) {
1757df8bae1dSRodney W. Grimes 		/*
1758df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
1759df8bae1dSRodney W. Grimes 		 * record the timestamp.
1760a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
1761a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1762df8bae1dSRodney W. Grimes 		 */
1763be2ac88cSJonathan Lemon 		if ((to.to_flags & TOF_TS) != 0 &&
1764fb59c426SYoshinobu Inoue 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1765d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
1766a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
1767df8bae1dSRodney W. Grimes 		}
1768df8bae1dSRodney W. Grimes 
1769fb59c426SYoshinobu Inoue 		if (tlen == 0) {
1770fb59c426SYoshinobu Inoue 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
1771fb59c426SYoshinobu Inoue 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
1772dbc42409SLawrence Stewart 			    !IN_RECOVERY(tp->t_flags) &&
1773fc30a251SAndre Oppermann 			    (to.to_flags & TOF_SACK) == 0 &&
1774dbc42409SLawrence Stewart 			    TAILQ_EMPTY(&tp->snd_holes)) {
1775df8bae1dSRodney W. Grimes 				/*
1776e8949f74SAndre Oppermann 				 * This is a pure ack for outstanding data.
1777df8bae1dSRodney W. Grimes 				 */
177878b50714SRobert Watson 				TCPSTAT_INC(tcps_predack);
1779252ca428SRobert Watson 
17809b8b58e0SJonathan Lemon 				/*
178110d20c84SMatt Macy 				 * "bad retransmit" recovery without timestamps.
17829b8b58e0SJonathan Lemon 				 */
178310d20c84SMatt Macy 				if ((to.to_flags & TOF_TS) == 0 &&
178410d20c84SMatt Macy 				    tp->t_rxtshift == 1 &&
1785672dc4aeSJohn Baldwin 				    tp->t_flags & TF_PREVVALID &&
17866dfb8b31SJohn Baldwin 				    (int)(ticks - tp->t_badrxtwin) < 0) {
1787dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_RTO_ERR);
17889b8b58e0SJonathan Lemon 				}
1789fa55172bSMatthew Dillon 
1790fa55172bSMatthew Dillon 				/*
1791fa55172bSMatthew Dillon 				 * Recalculate the transmit timer / rtt.
1792fa55172bSMatthew Dillon 				 *
1793fa55172bSMatthew Dillon 				 * Some boxes send broken timestamp replies
1794fa55172bSMatthew Dillon 				 * during the SYN+ACK phase, ignore
1795fa55172bSMatthew Dillon 				 * timestamps of 0 or we could calculate a
1796fa55172bSMatthew Dillon 				 * huge RTT and blow up the retransmit timer.
1797fa55172bSMatthew Dillon 				 */
1798fa55172bSMatthew Dillon 				if ((to.to_flags & TOF_TS) != 0 &&
1799fa55172bSMatthew Dillon 				    to.to_tsecr) {
18003ac12506SJonathan T. Looney 					uint32_t t;
1801d8951c8aSBjoern A. Zeeb 
1802d8951c8aSBjoern A. Zeeb 					t = tcp_ts_getticks() - to.to_tsecr;
1803d8951c8aSBjoern A. Zeeb 					if (!tp->t_rttlow || tp->t_rttlow > t)
1804d8951c8aSBjoern A. Zeeb 						tp->t_rttlow = t;
1805a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
1806d8951c8aSBjoern A. Zeeb 					    TCP_TS_TO_TICKS(t) + 1);
1807fa55172bSMatthew Dillon 				} else if (tp->t_rtttime &&
1808fa55172bSMatthew Dillon 				    SEQ_GT(th->th_ack, tp->t_rtseq)) {
1809eaf80179SAndre Oppermann 					if (!tp->t_rttlow ||
1810eaf80179SAndre Oppermann 					    tp->t_rttlow > ticks - tp->t_rtttime)
1811eaf80179SAndre Oppermann 						tp->t_rttlow = ticks - tp->t_rtttime;
1812c068736aSJeffrey Hsu 					tcp_xmit_timer(tp,
1813c068736aSJeffrey Hsu 							ticks - tp->t_rtttime);
1814fa55172bSMatthew Dillon 				}
1815dbc42409SLawrence Stewart 				acked = BYTES_THIS_ACK(tp, th);
181639bc9de5SLawrence Stewart 
1817bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
181839bc9de5SLawrence Stewart 				/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
181939bc9de5SLawrence Stewart 				hhook_run_tcp_est_in(tp, th, &to);
1820bd79708dSJonathan T. Looney #endif
182139bc9de5SLawrence Stewart 
18224b7b743cSLawrence Stewart 				TCPSTAT_ADD(tcps_rcvackpack, nsegs);
182378b50714SRobert Watson 				TCPSTAT_ADD(tcps_rcvackbyte, acked);
1824df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
18259d11646dSJeffrey Hsu 				if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
18269d11646dSJeffrey Hsu 				    SEQ_LEQ(th->th_ack, tp->snd_recover))
18279d11646dSJeffrey Hsu 					tp->snd_recover = th->th_ack - 1;
1828dbc42409SLawrence Stewart 
1829dbc42409SLawrence Stewart 				/*
1830dbc42409SLawrence Stewart 				 * Let the congestion control algorithm update
1831dbc42409SLawrence Stewart 				 * congestion control related information. This
1832dbc42409SLawrence Stewart 				 * typically means increasing the congestion
1833dbc42409SLawrence Stewart 				 * window.
1834dbc42409SLawrence Stewart 				 */
18354b7b743cSLawrence Stewart 				cc_ack_received(tp, th, nsegs, CC_ACK);
1836dbc42409SLawrence Stewart 
18379d11646dSJeffrey Hsu 				tp->snd_una = th->th_ack;
18381645d090SJeff Roberson 				/*
1839e8949f74SAndre Oppermann 				 * Pull snd_wl2 up to prevent seq wrap relative
18401645d090SJeff Roberson 				 * to th_ack.
18411645d090SJeff Roberson 				 */
1842cb942153SJeffrey Hsu 				tp->snd_wl2 = th->th_ack;
1843b5addd85SJeffrey Hsu 				tp->t_dupacks = 0;
1844df8bae1dSRodney W. Grimes 				m_freem(m);
1845df8bae1dSRodney W. Grimes 
1846df8bae1dSRodney W. Grimes 				/*
1847df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
1848df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
1849df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
1850df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
1851df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
1852df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
1853df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
1854e8949f74SAndre Oppermann 				 */
18553c653157SHartmut Brandt #ifdef TCPDEBUG
18563c653157SHartmut Brandt 				if (so->so_options & SO_DEBUG)
18573c653157SHartmut Brandt 					tcp_trace(TA_INPUT, ostate, tp,
18583c653157SHartmut Brandt 					    (void *)tcp_saveipgen,
18593c653157SHartmut Brandt 					    &tcp_savetcp, 0);
18603c653157SHartmut Brandt #endif
18612b9c9984SGeorge V. Neville-Neil 				TCP_PROBE3(debug__input, tp, th, m);
1862df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
1863b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
1864b8152ba7SAndre Oppermann 				else if (!tcp_timer_active(tp, TT_PERSIST))
1865b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT,
1866b8152ba7SAndre Oppermann 						      tp->t_rxtcur);
18674d0770f1SRichard Scheffenegger 				tp->t_flags |= TF_WAKESOW;
1868cfa6009eSGleb Smirnoff 				if (sbavail(&so->so_snd))
186955bceb1eSRandall Stewart 					(void) tp->t_fb->tfb_tcp_output(tp);
1870a14c749fSJonathan Lemon 				goto check_delack;
1871df8bae1dSRodney W. Grimes 			}
1872fb59c426SYoshinobu Inoue 		} else if (th->th_ack == tp->snd_una &&
1873fb59c426SYoshinobu Inoue 		    tlen <= sbspace(&so->so_rcv)) {
18746741ecf5SAndre Oppermann 			int newsize = 0;	/* automatic sockbuf scaling */
18756741ecf5SAndre Oppermann 
1876df8bae1dSRodney W. Grimes 			/*
1877252ca428SRobert Watson 			 * This is a pure, in-sequence data packet with
1878252ca428SRobert Watson 			 * nothing on the reassembly queue and we have enough
1879252ca428SRobert Watson 			 * buffer space to take it.
1880df8bae1dSRodney W. Grimes 			 */
18816d90faf3SPaul Saab 			/* Clean receiver SACK report if present */
18823529149eSAndre Oppermann 			if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks)
18836d90faf3SPaul Saab 				tcp_clean_sackreport(tp);
188478b50714SRobert Watson 			TCPSTAT_INC(tcps_preddat);
1885fb59c426SYoshinobu Inoue 			tp->rcv_nxt += tlen;
1886e854dd38SRandall Stewart 			if (tlen &&
1887e854dd38SRandall Stewart 			    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
1888e854dd38SRandall Stewart 			    (tp->t_fbyte_in == 0)) {
1889e854dd38SRandall Stewart 				tp->t_fbyte_in = ticks;
1890e854dd38SRandall Stewart 				if (tp->t_fbyte_in == 0)
1891e854dd38SRandall Stewart 					tp->t_fbyte_in = 1;
1892e854dd38SRandall Stewart 				if (tp->t_fbyte_out && tp->t_fbyte_in)
1893e854dd38SRandall Stewart 					tp->t_flags2 |= TF2_FBYTES_COMPLETE;
1894e854dd38SRandall Stewart 			}
18951645d090SJeff Roberson 			/*
18961645d090SJeff Roberson 			 * Pull snd_wl1 up to prevent seq wrap relative to
18971645d090SJeff Roberson 			 * th_seq.
18981645d090SJeff Roberson 			 */
189960ee3bb2SAndre Oppermann 			tp->snd_wl1 = th->th_seq;
19001645d090SJeff Roberson 			/*
19011645d090SJeff Roberson 			 * Pull rcv_up up to prevent seq wrap relative to
19021645d090SJeff Roberson 			 * rcv_nxt.
19031645d090SJeff Roberson 			 */
19041645d090SJeff Roberson 			tp->rcv_up = tp->rcv_nxt;
19054b7b743cSLawrence Stewart 			TCPSTAT_ADD(tcps_rcvpack, nsegs);
190678b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
19073c653157SHartmut Brandt #ifdef TCPDEBUG
19083c653157SHartmut Brandt 			if (so->so_options & SO_DEBUG)
19093c653157SHartmut Brandt 				tcp_trace(TA_INPUT, ostate, tp,
19103c653157SHartmut Brandt 				    (void *)tcp_saveipgen, &tcp_savetcp, 0);
19113c653157SHartmut Brandt #endif
19122b9c9984SGeorge V. Neville-Neil 			TCP_PROBE3(debug__input, tp, th, m);
19135d06879aSGeorge V. Neville-Neil 
1914e44c1887SSteven Hartland 			newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
19156741ecf5SAndre Oppermann 
19166741ecf5SAndre Oppermann 			/* Add data to socket buffer. */
19171e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
1918c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1919c1c36a2cSMike Silbersack 				m_freem(m);
1920c1c36a2cSMike Silbersack 			} else {
19216741ecf5SAndre Oppermann 				/*
19226741ecf5SAndre Oppermann 				 * Set new socket buffer size.
19236741ecf5SAndre Oppermann 				 * Give up when limit is reached.
19246741ecf5SAndre Oppermann 				 */
19256741ecf5SAndre Oppermann 				if (newsize)
19266741ecf5SAndre Oppermann 					if (!sbreserve_locked(&so->so_rcv,
19276c8286e4SRobert Watson 					    newsize, so, NULL))
19286741ecf5SAndre Oppermann 						so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
1929fb59c426SYoshinobu Inoue 				m_adj(m, drop_hdrlen);	/* delayed header drop */
1930651e4e6aSGleb Smirnoff 				sbappendstream_locked(&so->so_rcv, m, 0);
1931c1c36a2cSMike Silbersack 			}
19324d0770f1SRichard Scheffenegger 			SOCKBUF_UNLOCK(&so->so_rcv);
19334d0770f1SRichard Scheffenegger 			tp->t_flags |= TF_WAKESOR;
1934c1e5a6e5SAndre Oppermann 			if (DELAY_ACK(tp, tlen)) {
19353bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
1936f498eeeeSDavid Greenman 			} else {
1937e612a582SDavid Greenman 				tp->t_flags |= TF_ACKNOW;
193855bceb1eSRandall Stewart 				tp->t_fb->tfb_tcp_output(tp);
1939e612a582SDavid Greenman 			}
1940a14c749fSJonathan Lemon 			goto check_delack;
1941df8bae1dSRodney W. Grimes 		}
1942df8bae1dSRodney W. Grimes 	}
1943df8bae1dSRodney W. Grimes 
1944df8bae1dSRodney W. Grimes 	/*
1945df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
1946df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
1947df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
1948df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
1949df8bae1dSRodney W. Grimes 	 */
1950df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
1951df8bae1dSRodney W. Grimes 	if (win < 0)
1952df8bae1dSRodney W. Grimes 		win = 0;
195366e39adcSJohn Polstra 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1954df8bae1dSRodney W. Grimes 
1955df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1956df8bae1dSRodney W. Grimes 	/*
1957764d8cefSBill Fenner 	 * If the state is SYN_RECEIVED:
1958764d8cefSBill Fenner 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
1959764d8cefSBill Fenner 	 */
1960764d8cefSBill Fenner 	case TCPS_SYN_RECEIVED:
1961fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1962fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1963a57815efSBosko Milekic 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1964a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
1965a57815efSBosko Milekic 				goto dropwithreset;
1966a57815efSBosko Milekic 		}
196768bd7ed1SJonathan T. Looney 		if (IS_FASTOPEN(tp->t_flags)) {
1968281a0fd4SPatrick Kelsey 			/*
1969281a0fd4SPatrick Kelsey 			 * When a TFO connection is in SYN_RECEIVED, the
1970281a0fd4SPatrick Kelsey 			 * only valid packets are the initial SYN, a
1971281a0fd4SPatrick Kelsey 			 * retransmit/copy of the initial SYN (possibly with
1972281a0fd4SPatrick Kelsey 			 * a subset of the original data), a valid ACK, a
1973281a0fd4SPatrick Kelsey 			 * FIN, or a RST.
1974281a0fd4SPatrick Kelsey 			 */
1975281a0fd4SPatrick Kelsey 			if ((thflags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) {
1976281a0fd4SPatrick Kelsey 				rstreason = BANDLIM_RST_OPENPORT;
1977281a0fd4SPatrick Kelsey 				goto dropwithreset;
1978281a0fd4SPatrick Kelsey 			} else if (thflags & TH_SYN) {
1979281a0fd4SPatrick Kelsey 				/* non-initial SYN is ignored */
1980281a0fd4SPatrick Kelsey 				if ((tcp_timer_active(tp, TT_DELACK) ||
1981281a0fd4SPatrick Kelsey 				     tcp_timer_active(tp, TT_REXMT)))
1982281a0fd4SPatrick Kelsey 					goto drop;
1983281a0fd4SPatrick Kelsey 			} else if (!(thflags & (TH_ACK|TH_FIN|TH_RST))) {
1984281a0fd4SPatrick Kelsey 				goto drop;
1985281a0fd4SPatrick Kelsey 			}
1986281a0fd4SPatrick Kelsey 		}
1987764d8cefSBill Fenner 		break;
1988764d8cefSBill Fenner 
1989764d8cefSBill Fenner 	/*
1990df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
199198732609SMichael Tuexen 	 *	if seg contains a RST with valid ACK (SEQ.ACK has already
199298732609SMichael Tuexen 	 *	    been verified), then drop the connection.
199398732609SMichael Tuexen 	 *	if seg contains a RST without an ACK, drop the seg.
199498732609SMichael Tuexen 	 *	if seg does not contain SYN, then drop the seg.
1995df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
1996df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
1997df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
1998f2512ba1SRui Paulo 	 *	if seg contains an ECE and ECN support is enabled, the stream
1999f2512ba1SRui Paulo 	 *	    is ECN capable.
2000df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
2001df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
2002df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
2003df8bae1dSRodney W. Grimes 	 */
2004df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
200557f60867SMark Johnston 		if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) {
2006d9fae5abSAndriy Gapon 			TCP_PROBE5(connect__refused, NULL, tp,
20072b9c9984SGeorge V. Neville-Neil 			    m, tp, th);
2008df8bae1dSRodney W. Grimes 			tp = tcp_drop(tp, ECONNREFUSED);
200957f60867SMark Johnston 		}
20100ca3f933SAndre Oppermann 		if (thflags & TH_RST)
2011df8bae1dSRodney W. Grimes 			goto drop;
20120ca3f933SAndre Oppermann 		if (!(thflags & TH_SYN))
2013df8bae1dSRodney W. Grimes 			goto drop;
2014464fcfbcSAndre Oppermann 
2015fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
2016df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
2017fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
2018c560df6fSPatrick Kelsey 			int tfo_partial_ack = 0;
2019c560df6fSPatrick Kelsey 
202078b50714SRobert Watson 			TCPSTAT_INC(tcps_connects);
2021845799c1SAndras Olah 			soisconnected(so);
2022c488362eSRobert Watson #ifdef MAC
202330d239bcSRobert Watson 			mac_socketpeer_set_from_mbuf(m, so);
2024c488362eSRobert Watson #endif
2025845799c1SAndras Olah 			/* Do window scaling on this connection? */
2026845799c1SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2027845799c1SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
2028845799c1SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2029845799c1SAndras Olah 			}
20303ac12506SJonathan T. Looney 			tp->rcv_adv += min(tp->rcv_wnd,
2031766282cbSJohn Baldwin 			    TCP_MAXWIN << tp->rcv_scale);
2032a0292f23SGarrett Wollman 			tp->snd_una++;		/* SYN is acked */
2033a0292f23SGarrett Wollman 			/*
2034c560df6fSPatrick Kelsey 			 * If not all the data that was sent in the TFO SYN
2035c560df6fSPatrick Kelsey 			 * has been acked, resend the remainder right away.
2036c560df6fSPatrick Kelsey 			 */
2037c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags) &&
2038c560df6fSPatrick Kelsey 			    (tp->snd_una != tp->snd_max)) {
2039c560df6fSPatrick Kelsey 				tp->snd_nxt = th->th_ack;
2040c560df6fSPatrick Kelsey 				tfo_partial_ack = 1;
2041c560df6fSPatrick Kelsey 			}
2042c560df6fSPatrick Kelsey 			/*
2043a0292f23SGarrett Wollman 			 * If there's data, delay ACK; if there's also a FIN
2044a0292f23SGarrett Wollman 			 * ACKNOW will be turned on later.
2045a0292f23SGarrett Wollman 			 */
2046c560df6fSPatrick Kelsey 			if (DELAY_ACK(tp, tlen) && tlen != 0 && !tfo_partial_ack)
2047b8152ba7SAndre Oppermann 				tcp_timer_activate(tp, TT_DELACK,
2048b8152ba7SAndre Oppermann 				    tcp_delacktime);
2049a0292f23SGarrett Wollman 			else
2050a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
2051f2512ba1SRui Paulo 
2052bf7fcdb1SMichael Tuexen 			if (((thflags & (TH_CWR | TH_ECE)) == TH_ECE) &&
2053a2d59694SMichael Tuexen 			    (V_tcp_do_ecn == 1)) {
20543cf38784SMichael Tuexen 				tp->t_flags2 |= TF2_ECN_PERMIT;
205578b50714SRobert Watson 				TCPSTAT_INC(tcps_ecn_shs);
2056f2512ba1SRui Paulo 			}
2057f2512ba1SRui Paulo 
2058a0292f23SGarrett Wollman 			/*
2059a0292f23SGarrett Wollman 			 * Received <SYN,ACK> in SYN_SENT[*] state.
2060a0292f23SGarrett Wollman 			 * Transitions:
2061a0292f23SGarrett Wollman 			 *	SYN_SENT  --> ESTABLISHED
2062a0292f23SGarrett Wollman 			 *	SYN_SENT* --> FIN_WAIT_1
2063a0292f23SGarrett Wollman 			 */
20649b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
2065a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDFIN) {
206657f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_1);
2067a0292f23SGarrett Wollman 				tp->t_flags &= ~TF_NEEDFIN;
2068fb59c426SYoshinobu Inoue 				thflags &= ~TH_SYN;
20697ff19458SPaul Traina 			} else {
207057f60867SMark Johnston 				tcp_state_change(tp, TCPS_ESTABLISHED);
2071d9fae5abSAndriy Gapon 				TCP_PROBE5(connect__established, NULL, tp,
20722b9c9984SGeorge V. Neville-Neil 				    m, tp, th);
2073dbc42409SLawrence Stewart 				cc_conn_init(tp);
20749077f387SGleb Smirnoff 				tcp_timer_activate(tp, TT_KEEP,
20759077f387SGleb Smirnoff 				    TP_KEEPIDLE(tp));
20767ff19458SPaul Traina 			}
2077a0292f23SGarrett Wollman 		} else {
2078a0292f23SGarrett Wollman 			/*
2079c068736aSJeffrey Hsu 			 * Received initial SYN in SYN-SENT[*] state =>
2080153edc50SHiren Panchasara 			 * simultaneous open.
2081c068736aSJeffrey Hsu 			 * If it succeeds, connection is * half-synchronized.
2082c068736aSJeffrey Hsu 			 * Otherwise, do 3-way handshake:
2083a0292f23SGarrett Wollman 			 *        SYN-SENT -> SYN-RECEIVED
2084a0292f23SGarrett Wollman 			 *        SYN-SENT* -> SYN-RECEIVED*
2085a0292f23SGarrett Wollman 			 */
20864b8e98d6SQing Li 			tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
2087b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
208857f60867SMark Johnston 			tcp_state_change(tp, TCPS_SYN_RECEIVED);
2089a0292f23SGarrett Wollman 		}
2090df8bae1dSRodney W. Grimes 
20918501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
20927cfc6904SRobert Watson 
2093df8bae1dSRodney W. Grimes 		/*
2094fb59c426SYoshinobu Inoue 		 * Advance th->th_seq to correspond to first data byte.
2095df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
2096df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
2097df8bae1dSRodney W. Grimes 		 */
2098fb59c426SYoshinobu Inoue 		th->th_seq++;
2099fb59c426SYoshinobu Inoue 		if (tlen > tp->rcv_wnd) {
2100fb59c426SYoshinobu Inoue 			todrop = tlen - tp->rcv_wnd;
2101df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
2102fb59c426SYoshinobu Inoue 			tlen = tp->rcv_wnd;
2103fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
210478b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpackafterwin);
210578b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2106df8bae1dSRodney W. Grimes 		}
2107fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
2108fb59c426SYoshinobu Inoue 		tp->rcv_up = th->th_seq;
2109a0292f23SGarrett Wollman 		/*
2110a0292f23SGarrett Wollman 		 * Client side of transaction: already sent SYN and data.
2111a0292f23SGarrett Wollman 		 * If the remote host used T/TCP to validate the SYN,
2112a0292f23SGarrett Wollman 		 * our data will be ACK'd; if so, enter normal data segment
2113a0292f23SGarrett Wollman 		 * processing in the middle of step 5, ack processing.
2114a0292f23SGarrett Wollman 		 * Otherwise, goto step 6.
2115a0292f23SGarrett Wollman 		 */
2116fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK)
2117a0292f23SGarrett Wollman 			goto process_ACK;
2118c068736aSJeffrey Hsu 
2119df8bae1dSRodney W. Grimes 		goto step6;
2120c068736aSJeffrey Hsu 
2121a0292f23SGarrett Wollman 	/*
2122a0292f23SGarrett Wollman 	 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
2123c94c54e4SAndre Oppermann 	 *      do normal processing.
2124a0292f23SGarrett Wollman 	 *
2125c94c54e4SAndre Oppermann 	 * NB: Leftover from RFC1644 T/TCP.  Cases to be reused later.
2126a0292f23SGarrett Wollman 	 */
2127a0292f23SGarrett Wollman 	case TCPS_LAST_ACK:
2128a0292f23SGarrett Wollman 	case TCPS_CLOSING:
2129a0292f23SGarrett Wollman 		break;  /* continue normal processing */
2130df8bae1dSRodney W. Grimes 	}
2131df8bae1dSRodney W. Grimes 
2132df8bae1dSRodney W. Grimes 	/*
2133df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
213480ab7c0eSGarrett Wollman 	 * First check the RST flag and sequence number since reset segments
213580ab7c0eSGarrett Wollman 	 * are exempt from the timestamp and connection count tests.  This
213680ab7c0eSGarrett Wollman 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
213780ab7c0eSGarrett Wollman 	 * below which allowed reset segments in half the sequence space
213880ab7c0eSGarrett Wollman 	 * to fall though and be processed (which gives forged reset
213980ab7c0eSGarrett Wollman 	 * segments with a random sequence number a 50 percent chance of
214080ab7c0eSGarrett Wollman 	 * killing a connection).
214180ab7c0eSGarrett Wollman 	 * Then check timestamp, if present.
2142a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
2143df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
2144df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
2145df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
214680ab7c0eSGarrett Wollman 	 */
2147fb59c426SYoshinobu Inoue 	if (thflags & TH_RST) {
21483220a212SGleb Smirnoff 		/*
21493220a212SGleb Smirnoff 		 * RFC5961 Section 3.2
21503220a212SGleb Smirnoff 		 *
21513220a212SGleb Smirnoff 		 * - RST drops connection only if SEG.SEQ == RCV.NXT.
21523220a212SGleb Smirnoff 		 * - If RST is in window, we send challenge ACK.
21533220a212SGleb Smirnoff 		 *
21543220a212SGleb Smirnoff 		 * Note: to take into account delayed ACKs, we should
21553220a212SGleb Smirnoff 		 *   test against last_ack_sent instead of rcv_nxt.
21563220a212SGleb Smirnoff 		 * Note 2: we handle special case of closed window, not
21573220a212SGleb Smirnoff 		 *   covered by the RFC.
21583220a212SGleb Smirnoff 		 */
21593220a212SGleb Smirnoff 		if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
21603220a212SGleb Smirnoff 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
21613220a212SGleb Smirnoff 		    (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) {
21623220a212SGleb Smirnoff 			KASSERT(tp->t_state != TCPS_SYN_SENT,
21633220a212SGleb Smirnoff 			    ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p",
21643220a212SGleb Smirnoff 			    __func__, th, tp));
21653220a212SGleb Smirnoff 
21663220a212SGleb Smirnoff 			if (V_tcp_insecure_rst ||
21673220a212SGleb Smirnoff 			    tp->last_ack_sent == th->th_seq) {
21683220a212SGleb Smirnoff 				TCPSTAT_INC(tcps_drops);
21693220a212SGleb Smirnoff 				/* Drop the connection. */
21703220a212SGleb Smirnoff 				switch (tp->t_state) {
217180ab7c0eSGarrett Wollman 				case TCPS_SYN_RECEIVED:
217280ab7c0eSGarrett Wollman 					so->so_error = ECONNREFUSED;
217380ab7c0eSGarrett Wollman 					goto close;
217480ab7c0eSGarrett Wollman 				case TCPS_ESTABLISHED:
217580ab7c0eSGarrett Wollman 				case TCPS_FIN_WAIT_1:
217680ab7c0eSGarrett Wollman 				case TCPS_FIN_WAIT_2:
217780ab7c0eSGarrett Wollman 				case TCPS_CLOSE_WAIT:
21786779a1a1SMichael Tuexen 				case TCPS_CLOSING:
21796779a1a1SMichael Tuexen 				case TCPS_LAST_ACK:
218080ab7c0eSGarrett Wollman 					so->so_error = ECONNRESET;
218180ab7c0eSGarrett Wollman 				close:
21823220a212SGleb Smirnoff 					/* FALLTHROUGH */
21833220a212SGleb Smirnoff 				default:
218480ab7c0eSGarrett Wollman 					tp = tcp_close(tp);
21853220a212SGleb Smirnoff 				}
21863220a212SGleb Smirnoff 			} else {
21873220a212SGleb Smirnoff 				TCPSTAT_INC(tcps_badrst);
21883220a212SGleb Smirnoff 				/* Send challenge ACK. */
21893220a212SGleb Smirnoff 				tcp_respond(tp, mtod(m, void *), th, m,
21903220a212SGleb Smirnoff 				    tp->rcv_nxt, tp->snd_nxt, TH_ACK);
21913220a212SGleb Smirnoff 				tp->last_ack_sent = tp->rcv_nxt;
21923220a212SGleb Smirnoff 				m = NULL;
21933220a212SGleb Smirnoff 			}
21943220a212SGleb Smirnoff 		}
21953220a212SGleb Smirnoff 		goto drop;
21963220a212SGleb Smirnoff 	}
219780ab7c0eSGarrett Wollman 
21983220a212SGleb Smirnoff 	/*
21993220a212SGleb Smirnoff 	 * RFC5961 Section 4.2
22003220a212SGleb Smirnoff 	 * Send challenge ACK for any SYN in synchronized state.
22013220a212SGleb Smirnoff 	 */
2202281a0fd4SPatrick Kelsey 	if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT &&
2203281a0fd4SPatrick Kelsey 	    tp->t_state != TCPS_SYN_RECEIVED) {
22043220a212SGleb Smirnoff 		TCPSTAT_INC(tcps_badsyn);
22053220a212SGleb Smirnoff 		if (V_tcp_insecure_syn &&
22063220a212SGleb Smirnoff 		    SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
22073220a212SGleb Smirnoff 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
22083220a212SGleb Smirnoff 			tp = tcp_drop(tp, ECONNRESET);
22093220a212SGleb Smirnoff 			rstreason = BANDLIM_UNLIMITED;
22103220a212SGleb Smirnoff 		} else {
22113220a212SGleb Smirnoff 			/* Send challenge ACK. */
22123220a212SGleb Smirnoff 			tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt,
22133220a212SGleb Smirnoff 			    tp->snd_nxt, TH_ACK);
22143220a212SGleb Smirnoff 			tp->last_ack_sent = tp->rcv_nxt;
22153220a212SGleb Smirnoff 			m = NULL;
221680ab7c0eSGarrett Wollman 		}
221780ab7c0eSGarrett Wollman 		goto drop;
221880ab7c0eSGarrett Wollman 	}
221980ab7c0eSGarrett Wollman 
222080ab7c0eSGarrett Wollman 	/*
2221df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
2222df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
2223df8bae1dSRodney W. Grimes 	 */
2224be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
222580ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
2226df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
2227d8951c8aSBjoern A. Zeeb 		if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) {
2228df8bae1dSRodney W. Grimes 			/*
2229df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
2230df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
2231df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
2232df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
2233df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
2234df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
2235df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
2236df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
2237df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
2238df8bae1dSRodney W. Grimes 			 */
2239df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
2240df8bae1dSRodney W. Grimes 		} else {
224178b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
224278b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, tlen);
224378b50714SRobert Watson 			TCPSTAT_INC(tcps_pawsdrop);
224407fd333dSMatthew Dillon 			if (tlen)
2245df8bae1dSRodney W. Grimes 				goto dropafterack;
22461ab4789dSMatthew Dillon 			goto drop;
22471ab4789dSMatthew Dillon 		}
2248df8bae1dSRodney W. Grimes 	}
2249df8bae1dSRodney W. Grimes 
2250a0292f23SGarrett Wollman 	/*
225180ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
225280ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
225380ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
225480ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
225580ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
225680ab7c0eSGarrett Wollman 	 */
2257a57815efSBosko Milekic 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
2258a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2259a57815efSBosko Milekic 		goto dropwithreset;
2260a57815efSBosko Milekic 	}
226180ab7c0eSGarrett Wollman 
2262fb59c426SYoshinobu Inoue 	todrop = tp->rcv_nxt - th->th_seq;
2263df8bae1dSRodney W. Grimes 	if (todrop > 0) {
2264831ad37eSXin LI 		if (thflags & TH_SYN) {
2265fb59c426SYoshinobu Inoue 			thflags &= ~TH_SYN;
2266fb59c426SYoshinobu Inoue 			th->th_seq++;
2267fb59c426SYoshinobu Inoue 			if (th->th_urp > 1)
2268fb59c426SYoshinobu Inoue 				th->th_urp--;
2269df8bae1dSRodney W. Grimes 			else
2270fb59c426SYoshinobu Inoue 				thflags &= ~TH_URG;
2271df8bae1dSRodney W. Grimes 			todrop--;
2272df8bae1dSRodney W. Grimes 		}
2273df8bae1dSRodney W. Grimes 		/*
2274dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
2275df8bae1dSRodney W. Grimes 		 */
2276fb59c426SYoshinobu Inoue 		if (todrop > tlen
2277fb59c426SYoshinobu Inoue 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
2278dac20301SGarrett Wollman 			/*
2279dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
2280dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
2281dac20301SGarrett Wollman 			 * of sequence; drop it.
2282dac20301SGarrett Wollman 			 */
2283fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
2284dac20301SGarrett Wollman 
2285df8bae1dSRodney W. Grimes 			/*
2286dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
2287dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
2288df8bae1dSRodney W. Grimes 			 */
2289dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
2290fb59c426SYoshinobu Inoue 			todrop = tlen;
229178b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
229278b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, todrop);
2293df8bae1dSRodney W. Grimes 		} else {
229478b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpartduppack);
229578b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop);
2296df8bae1dSRodney W. Grimes 		}
22975acfd95cSMichael Tuexen 		/*
22985acfd95cSMichael Tuexen 		 * DSACK - add SACK block for dropped range
22995acfd95cSMichael Tuexen 		 */
23008f63a52bSMichael Tuexen 		if ((todrop > 0) && (tp->t_flags & TF_SACK_PERMIT)) {
2301ecc5b1d1SMichael Tuexen 			tcp_update_sack_list(tp, th->th_seq,
2302ecc5b1d1SMichael Tuexen 			    th->th_seq + todrop);
23035acfd95cSMichael Tuexen 			/*
23045acfd95cSMichael Tuexen 			 * ACK now, as the next in-sequence segment
23055acfd95cSMichael Tuexen 			 * will clear the DSACK block again
23065acfd95cSMichael Tuexen 			 */
23075acfd95cSMichael Tuexen 			tp->t_flags |= TF_ACKNOW;
23085acfd95cSMichael Tuexen 		}
2309fb59c426SYoshinobu Inoue 		drop_hdrlen += todrop;	/* drop from the top afterwards */
2310fb59c426SYoshinobu Inoue 		th->th_seq += todrop;
2311fb59c426SYoshinobu Inoue 		tlen -= todrop;
2312fb59c426SYoshinobu Inoue 		if (th->th_urp > todrop)
2313fb59c426SYoshinobu Inoue 			th->th_urp -= todrop;
2314df8bae1dSRodney W. Grimes 		else {
2315fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;
2316fb59c426SYoshinobu Inoue 			th->th_urp = 0;
2317df8bae1dSRodney W. Grimes 		}
2318df8bae1dSRodney W. Grimes 	}
2319df8bae1dSRodney W. Grimes 
2320df8bae1dSRodney W. Grimes 	/*
2321df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
2322df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
2323df8bae1dSRodney W. Grimes 	 */
2324df8bae1dSRodney W. Grimes 	if ((so->so_state & SS_NOFDREF) &&
2325fb59c426SYoshinobu Inoue 	    tp->t_state > TCPS_CLOSE_WAIT && tlen) {
232607dacf03SAndre Oppermann 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
232707dacf03SAndre Oppermann 			log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data "
232807dacf03SAndre Oppermann 			    "after socket was closed, "
232907dacf03SAndre Oppermann 			    "sending RST and removing tcpcb\n",
2330e31d8aa3SMike Silbersack 			    s, __func__, tcpstates[tp->t_state], tlen);
2331773673c1SAndre Oppermann 			free(s, M_TCPLOG);
2332773673c1SAndre Oppermann 		}
2333df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
233478b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvafterclose);
2335a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
2336df8bae1dSRodney W. Grimes 		goto dropwithreset;
2337df8bae1dSRodney W. Grimes 	}
2338df8bae1dSRodney W. Grimes 
2339df8bae1dSRodney W. Grimes 	/*
2340df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
2341df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
2342df8bae1dSRodney W. Grimes 	 */
2343fb59c426SYoshinobu Inoue 	todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
2344df8bae1dSRodney W. Grimes 	if (todrop > 0) {
234578b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvpackafterwin);
2346fb59c426SYoshinobu Inoue 		if (todrop >= tlen) {
234778b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen);
2348df8bae1dSRodney W. Grimes 			/*
2349df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
2350df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
2351df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
2352df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
2353df8bae1dSRodney W. Grimes 			 * and ack.
2354df8bae1dSRodney W. Grimes 			 */
2355fb59c426SYoshinobu Inoue 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
2356df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
235778b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvwinprobe);
2358df8bae1dSRodney W. Grimes 			} else
2359df8bae1dSRodney W. Grimes 				goto dropafterack;
2360df8bae1dSRodney W. Grimes 		} else
236178b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2362df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
2363fb59c426SYoshinobu Inoue 		tlen -= todrop;
2364fb59c426SYoshinobu Inoue 		thflags &= ~(TH_PUSH|TH_FIN);
2365df8bae1dSRodney W. Grimes 	}
2366df8bae1dSRodney W. Grimes 
2367df8bae1dSRodney W. Grimes 	/*
2368df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
2369df8bae1dSRodney W. Grimes 	 * record its timestamp.
2370cf09195bSPaul Saab 	 * NOTE:
2371cf09195bSPaul Saab 	 * 1) That the test incorporates suggestions from the latest
2372a0292f23SGarrett Wollman 	 *    proposal of the tcplw@cray.com list (Braden 1993/04/26).
2373cf09195bSPaul Saab 	 * 2) That updating only on newer timestamps interferes with
2374cf09195bSPaul Saab 	 *    our earlier PAWS tests, so this check should be solely
2375cf09195bSPaul Saab 	 *    predicated on the sequence space of this segment.
2376cf09195bSPaul Saab 	 * 3) That we modify the segment boundary check to be
2377cf09195bSPaul Saab 	 *        Last.ACK.Sent <= SEG.SEQ + SEG.Len
2378cf09195bSPaul Saab 	 *    instead of RFC1323's
2379cf09195bSPaul Saab 	 *        Last.ACK.Sent < SEG.SEQ + SEG.Len,
2380cf09195bSPaul Saab 	 *    This modified check allows us to overcome RFC1323's
2381cf09195bSPaul Saab 	 *    limitations as described in Stevens TCP/IP Illustrated
2382cf09195bSPaul Saab 	 *    Vol. 2 p.869. In such cases, we can still calculate the
2383cf09195bSPaul Saab 	 *    RTT correctly when RCV.NXT == Last.ACK.Sent.
2384df8bae1dSRodney W. Grimes 	 */
2385be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 &&
2386cf09195bSPaul Saab 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
2387cf09195bSPaul Saab 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
2388cf09195bSPaul Saab 		((thflags & (TH_SYN|TH_FIN)) != 0))) {
2389d8951c8aSBjoern A. Zeeb 		tp->ts_recent_age = tcp_ts_getticks();
2390a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
2391df8bae1dSRodney W. Grimes 	}
2392df8bae1dSRodney W. Grimes 
2393df8bae1dSRodney W. Grimes 	/*
2394a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
2395a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
2396a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
2397a0292f23SGarrett Wollman 	 */
2398fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) == 0) {
2399a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
2400281a0fd4SPatrick Kelsey 		    (tp->t_flags & TF_NEEDSYN)) {
2401281a0fd4SPatrick Kelsey 			if (tp->t_state == TCPS_SYN_RECEIVED &&
240268bd7ed1SJonathan T. Looney 			    IS_FASTOPEN(tp->t_flags)) {
2403281a0fd4SPatrick Kelsey 				tp->snd_wnd = tiwin;
2404281a0fd4SPatrick Kelsey 				cc_conn_init(tp);
2405281a0fd4SPatrick Kelsey 			}
2406a0292f23SGarrett Wollman 			goto step6;
2407281a0fd4SPatrick Kelsey 		} else if (tp->t_flags & TF_ACKNOW)
24085e1aa279SDavid Malone 			goto dropafterack;
2409a0292f23SGarrett Wollman 		else
2410a0292f23SGarrett Wollman 			goto drop;
2411a0292f23SGarrett Wollman 	}
2412df8bae1dSRodney W. Grimes 
2413df8bae1dSRodney W. Grimes 	/*
2414df8bae1dSRodney W. Grimes 	 * Ack processing.
2415df8bae1dSRodney W. Grimes 	 */
2416df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2417df8bae1dSRodney W. Grimes 	/*
2418764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
2419764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
2420764d8cefSBill Fenner 	 * The ACK was checked above.
2421df8bae1dSRodney W. Grimes 	 */
2422df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2423a0292f23SGarrett Wollman 
242478b50714SRobert Watson 		TCPSTAT_INC(tcps_connects);
2425df8bae1dSRodney W. Grimes 		soisconnected(so);
2426df8bae1dSRodney W. Grimes 		/* Do window scaling? */
2427df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2428df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
2429df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
2430df8bae1dSRodney W. Grimes 		}
2431116ef4d6SMichael Tuexen 		tp->snd_wnd = tiwin;
2432a0292f23SGarrett Wollman 		/*
2433a0292f23SGarrett Wollman 		 * Make transitions:
2434a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
2435a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
2436a0292f23SGarrett Wollman 		 */
24379b8b58e0SJonathan Lemon 		tp->t_starttime = ticks;
243818a75309SPatrick Kelsey 		if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) {
2439281a0fd4SPatrick Kelsey 			tcp_fastopen_decrement_counter(tp->t_tfo_pending);
2440281a0fd4SPatrick Kelsey 			tp->t_tfo_pending = NULL;
2441281a0fd4SPatrick Kelsey 		}
24428db239dcSMichael Tuexen 		if (tp->t_flags & TF_NEEDFIN) {
24438db239dcSMichael Tuexen 			tcp_state_change(tp, TCPS_FIN_WAIT_1);
24448db239dcSMichael Tuexen 			tp->t_flags &= ~TF_NEEDFIN;
24458db239dcSMichael Tuexen 		} else {
24468db239dcSMichael Tuexen 			tcp_state_change(tp, TCPS_ESTABLISHED);
24478db239dcSMichael Tuexen 			TCP_PROBE5(accept__established, NULL, tp,
24488db239dcSMichael Tuexen 			    m, tp, th);
2449281a0fd4SPatrick Kelsey 			/*
2450281a0fd4SPatrick Kelsey 			 * TFO connections call cc_conn_init() during SYN
2451281a0fd4SPatrick Kelsey 			 * processing.  Calling it again here for such
2452281a0fd4SPatrick Kelsey 			 * connections is not harmless as it would undo the
2453281a0fd4SPatrick Kelsey 			 * snd_cwnd reduction that occurs when a TFO SYN|ACK
2454281a0fd4SPatrick Kelsey 			 * is retransmitted.
2455281a0fd4SPatrick Kelsey 			 */
245668bd7ed1SJonathan T. Looney 			if (!IS_FASTOPEN(tp->t_flags))
2457dbc42409SLawrence Stewart 				cc_conn_init(tp);
24589077f387SGleb Smirnoff 			tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp));
24597ff19458SPaul Traina 		}
2460a0292f23SGarrett Wollman 		/*
2461b2ade6b1SRichard Scheffenegger 		 * Account for the ACK of our SYN prior to
2462b2ade6b1SRichard Scheffenegger 		 * regular ACK processing below, except for
2463b2ade6b1SRichard Scheffenegger 		 * simultaneous SYN, which is handled later.
2464b2ade6b1SRichard Scheffenegger 		 */
2465b2ade6b1SRichard Scheffenegger 		if (SEQ_GT(th->th_ack, tp->snd_una) && !(tp->t_flags & TF_NEEDSYN))
2466b2ade6b1SRichard Scheffenegger 			incforsyn = 1;
2467b2ade6b1SRichard Scheffenegger 		/*
2468a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
2469a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
2470a0292f23SGarrett Wollman 		 */
2471fb59c426SYoshinobu Inoue 		if (tlen == 0 && (thflags & TH_FIN) == 0)
2472c28440dbSRandall Stewart 			(void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0,
2473a0292f23SGarrett Wollman 			    (struct mbuf *)0);
247460ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq - 1;
247593b0017fSPhilippe Charnier 		/* FALLTHROUGH */
2476df8bae1dSRodney W. Grimes 
2477df8bae1dSRodney W. Grimes 	/*
2478df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
2479df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
2480fb59c426SYoshinobu Inoue 	 *	tp->snd_una < th->th_ack <= tp->snd_max
2481fb59c426SYoshinobu Inoue 	 * then advance tp->snd_una to th->th_ack and drop
2482df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
2483df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
2484df8bae1dSRodney W. Grimes 	 */
2485df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
2486df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
2487df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
2488df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
2489df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
2490df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
24915a53ca16SPaul Saab 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
249278b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvacktoomuch);
24935a53ca16SPaul Saab 			goto dropafterack;
24945a53ca16SPaul Saab 		}
24953529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
2496fc30a251SAndre Oppermann 		    ((to.to_flags & TOF_SACK) ||
2497fc30a251SAndre Oppermann 		     !TAILQ_EMPTY(&tp->snd_holes)))
2498021eaf79SHiren Panchasara 			sack_changed = tcp_sack_doack(tp, &to, th->th_ack);
249912eeb81fSHiren Panchasara 		else
250012eeb81fSHiren Panchasara 			/*
250112eeb81fSHiren Panchasara 			 * Reset the value so that previous (valid) value
250212eeb81fSHiren Panchasara 			 * from the last ack with SACK doesn't get used.
250312eeb81fSHiren Panchasara 			 */
250412eeb81fSHiren Panchasara 			tp->sackhint.sacked_bytes = 0;
250539bc9de5SLawrence Stewart 
2506bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
250739bc9de5SLawrence Stewart 		/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
250839bc9de5SLawrence Stewart 		hhook_run_tcp_est_in(tp, th, &to);
2509bd79708dSJonathan T. Looney #endif
251039bc9de5SLawrence Stewart 
2511fb59c426SYoshinobu Inoue 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
25120c39d38dSGleb Smirnoff 			u_int maxseg;
25130c39d38dSGleb Smirnoff 
25140c39d38dSGleb Smirnoff 			maxseg = tcp_maxseg(tp);
2515021eaf79SHiren Panchasara 			if (tlen == 0 &&
2516021eaf79SHiren Panchasara 			    (tiwin == tp->snd_wnd ||
2517021eaf79SHiren Panchasara 			    (tp->t_flags & TF_SACK_PERMIT))) {
251862b90589SPeter Wemm 				/*
251962b90589SPeter Wemm 				 * If this is the first time we've seen a
252062b90589SPeter Wemm 				 * FIN from the remote, this is not a
252162b90589SPeter Wemm 				 * duplicate and it needs to be processed
252262b90589SPeter Wemm 				 * normally.  This happens during a
252362b90589SPeter Wemm 				 * simultaneous close.
252462b90589SPeter Wemm 				 */
252562b90589SPeter Wemm 				if ((thflags & TH_FIN) &&
252662b90589SPeter Wemm 				    (TCPS_HAVERCVDFIN(tp->t_state) == 0)) {
252762b90589SPeter Wemm 					tp->t_dupacks = 0;
252862b90589SPeter Wemm 					break;
252962b90589SPeter Wemm 				}
253078b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvdupack);
2531df8bae1dSRodney W. Grimes 				/*
2532df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
2533df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
2534df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
25355f30ec9bSEitan Adler 				 * change and FIN isn't set),
25365f30ec9bSEitan Adler 				 * the ack is the biggest we've
2537df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
2538a4641f4eSPedro F. Giffuni 				 * threshold of them, assume a packet
2539df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
2540df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
2541df8bae1dSRodney W. Grimes 				 * window so we send only this one
2542df8bae1dSRodney W. Grimes 				 * packet.
2543df8bae1dSRodney W. Grimes 				 *
2544df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
2545df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
2546df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
2547df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
2548df8bae1dSRodney W. Grimes 				 * the new ssthresh).
2549df8bae1dSRodney W. Grimes 				 *
2550df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
2551df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
2552df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
2553df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
2554df8bae1dSRodney W. Grimes 				 * network.
2555f2512ba1SRui Paulo 				 *
2556f2512ba1SRui Paulo 				 * When using TCP ECN, notify the peer that
2557f2512ba1SRui Paulo 				 * we reduced the cwnd.
2558df8bae1dSRodney W. Grimes 				 */
2559021eaf79SHiren Panchasara 				/*
2560021eaf79SHiren Panchasara 				 * Following 2 kinds of acks should not affect
2561021eaf79SHiren Panchasara 				 * dupack counting:
2562021eaf79SHiren Panchasara 				 * 1) Old acks
2563021eaf79SHiren Panchasara 				 * 2) Acks with SACK but without any new SACK
2564021eaf79SHiren Panchasara 				 * information in them. These could result from
2565021eaf79SHiren Panchasara 				 * any anomaly in the network like a switch
2566021eaf79SHiren Panchasara 				 * duplicating packets or a possible DoS attack.
2567021eaf79SHiren Panchasara 				 */
2568021eaf79SHiren Panchasara 				if (th->th_ack != tp->snd_una ||
2569021eaf79SHiren Panchasara 				    ((tp->t_flags & TF_SACK_PERMIT) &&
2570021eaf79SHiren Panchasara 				    !sack_changed))
2571021eaf79SHiren Panchasara 					break;
2572021eaf79SHiren Panchasara 				else if (!tcp_timer_active(tp, TT_REXMT))
2573df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
2574cb942153SJeffrey Hsu 				else if (++tp->t_dupacks > tcprexmtthresh ||
2575dbc42409SLawrence Stewart 				     IN_FASTRECOVERY(tp->t_flags)) {
25764b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
25774b7b743cSLawrence Stewart 					    CC_DUPACK);
25780e1d7c25SRichard Scheffenegger 					if (V_tcp_do_prr &&
25790e1d7c25SRichard Scheffenegger 					    IN_FASTRECOVERY(tp->t_flags) &&
25800e1d7c25SRichard Scheffenegger 					    (tp->t_flags & TF_SACK_PERMIT)) {
25810e1d7c25SRichard Scheffenegger 						long snd_cnt = 0, limit = 0;
25820e1d7c25SRichard Scheffenegger 						long del_data = 0, pipe = 0;
25830e1d7c25SRichard Scheffenegger 						/*
25840e1d7c25SRichard Scheffenegger 						 * In a duplicate ACK del_data is only the
25850e1d7c25SRichard Scheffenegger 						 * diff_in_sack. If no SACK is used del_data
25860e1d7c25SRichard Scheffenegger 						 * will be 0. Pipe is the amount of data we
25870e1d7c25SRichard Scheffenegger 						 * estimate to be in the network.
25880e1d7c25SRichard Scheffenegger 						 */
25890e1d7c25SRichard Scheffenegger 						del_data = tp->sackhint.delivered_data;
25900e1d7c25SRichard Scheffenegger 						pipe = (tp->snd_nxt - tp->snd_fack) +
25910e1d7c25SRichard Scheffenegger 							tp->sackhint.sack_bytes_rexmit;
25920e1d7c25SRichard Scheffenegger 						tp->sackhint.prr_delivered += del_data;
25930e1d7c25SRichard Scheffenegger 						if (pipe > tp->snd_ssthresh) {
2594bc7ee8e5SRichard Scheffenegger 							if (tp->sackhint.recover_fs == 0)
2595bc7ee8e5SRichard Scheffenegger 								tp->sackhint.recover_fs =
2596bc7ee8e5SRichard Scheffenegger 								    max(1, tp->snd_nxt - tp->snd_una);
25970e1d7c25SRichard Scheffenegger 							snd_cnt = (tp->sackhint.prr_delivered *
25980e1d7c25SRichard Scheffenegger 							    tp->snd_ssthresh /
25990e1d7c25SRichard Scheffenegger 							    tp->sackhint.recover_fs) +
26000e1d7c25SRichard Scheffenegger 							    1 - tp->sackhint.sack_bytes_rexmit;
26010e1d7c25SRichard Scheffenegger 						} else {
26020e1d7c25SRichard Scheffenegger 							if (V_tcp_do_prr_conservative)
26030e1d7c25SRichard Scheffenegger 								limit = tp->sackhint.prr_delivered -
26040e1d7c25SRichard Scheffenegger 									tp->sackhint.sack_bytes_rexmit;
26050e1d7c25SRichard Scheffenegger 							else
26060e1d7c25SRichard Scheffenegger 								if ((tp->sackhint.prr_delivered -
26070e1d7c25SRichard Scheffenegger 								    tp->sackhint.sack_bytes_rexmit) >
26080e1d7c25SRichard Scheffenegger 								    del_data)
26090e1d7c25SRichard Scheffenegger 									limit = tp->sackhint.prr_delivered -
26100e1d7c25SRichard Scheffenegger 									    tp->sackhint.sack_bytes_rexmit +
26110e1d7c25SRichard Scheffenegger 									    maxseg;
26120e1d7c25SRichard Scheffenegger 								else
26130e1d7c25SRichard Scheffenegger 									limit = del_data + maxseg;
26140e1d7c25SRichard Scheffenegger 							if ((tp->snd_ssthresh - pipe) < limit)
26150e1d7c25SRichard Scheffenegger 								snd_cnt = tp->snd_ssthresh - pipe;
26160e1d7c25SRichard Scheffenegger 							else
26170e1d7c25SRichard Scheffenegger 								snd_cnt = limit;
26180e1d7c25SRichard Scheffenegger 						}
26190e1d7c25SRichard Scheffenegger 						snd_cnt = max((snd_cnt / maxseg), 0);
26200e1d7c25SRichard Scheffenegger 						/*
26210e1d7c25SRichard Scheffenegger 						 * Send snd_cnt new data into the network in
26220e1d7c25SRichard Scheffenegger 						 * response to this ACK. If there is a going
26230e1d7c25SRichard Scheffenegger 						 * to be a SACK retransmission, adjust snd_cwnd
26240e1d7c25SRichard Scheffenegger 						 * accordingly.
26250e1d7c25SRichard Scheffenegger 						 */
26260e1d7c25SRichard Scheffenegger 						tp->snd_cwnd = tp->snd_nxt - tp->snd_recover +
26270e1d7c25SRichard Scheffenegger 						    tp->sackhint.sack_bytes_rexmit +
26280e1d7c25SRichard Scheffenegger 						    (snd_cnt * maxseg);
26290e1d7c25SRichard Scheffenegger 					} else if ((tp->t_flags & TF_SACK_PERMIT) &&
2630dbc42409SLawrence Stewart 					    IN_FASTRECOVERY(tp->t_flags)) {
2631808f11b7SPaul Saab 						int awnd;
263225e6f9edSPaul Saab 
263325e6f9edSPaul Saab 						/*
263425e6f9edSPaul Saab 						 * Compute the amount of data in flight first.
26354c3972f0SHiren Panchasara 						 * We can inject new data into the pipe iff
263625e6f9edSPaul Saab 						 * we have less than 1/2 the original window's
263725e6f9edSPaul Saab 						 * worth of data in flight.
263825e6f9edSPaul Saab 						 */
263912eeb81fSHiren Panchasara 						if (V_tcp_do_rfc6675_pipe)
264012eeb81fSHiren Panchasara 							awnd = tcp_compute_pipe(tp);
264112eeb81fSHiren Panchasara 						else
2642808f11b7SPaul Saab 							awnd = (tp->snd_nxt - tp->snd_fack) +
26430077b016SPaul Saab 								tp->sackhint.sack_bytes_rexmit;
264412eeb81fSHiren Panchasara 
2645808f11b7SPaul Saab 						if (awnd < tp->snd_ssthresh) {
26460c39d38dSGleb Smirnoff 							tp->snd_cwnd += maxseg;
264725e6f9edSPaul Saab 							if (tp->snd_cwnd > tp->snd_ssthresh)
264825e6f9edSPaul Saab 								tp->snd_cwnd = tp->snd_ssthresh;
264925e6f9edSPaul Saab 						}
265025e6f9edSPaul Saab 					} else
26510c39d38dSGleb Smirnoff 						tp->snd_cwnd += maxseg;
265255bceb1eSRandall Stewart 					(void) tp->t_fb->tfb_tcp_output(tp);
265346f58482SJonathan Lemon 					goto drop;
2654cb942153SJeffrey Hsu 				} else if (tp->t_dupacks == tcprexmtthresh) {
2655cb942153SJeffrey Hsu 					tcp_seq onxt = tp->snd_nxt;
2656a0445c2eSJayanth Vijayaraghavan 
2657a0445c2eSJayanth Vijayaraghavan 					/*
26580e1d7c25SRichard Scheffenegger 					 * If we're doing sack, or prr, check
26590e1d7c25SRichard Scheffenegger 					 * to see if we're already in sack
2660a0445c2eSJayanth Vijayaraghavan 					 * recovery. If we're not doing sack,
2661a0445c2eSJayanth Vijayaraghavan 					 * check to see if we're in newreno
2662a0445c2eSJayanth Vijayaraghavan 					 * recovery.
2663a0445c2eSJayanth Vijayaraghavan 					 */
26640e1d7c25SRichard Scheffenegger 					if (V_tcp_do_prr ||
26650e1d7c25SRichard Scheffenegger 					    (tp->t_flags & TF_SACK_PERMIT)) {
2666dbc42409SLawrence Stewart 						if (IN_FASTRECOVERY(tp->t_flags)) {
2667a0445c2eSJayanth Vijayaraghavan 							tp->t_dupacks = 0;
2668a0445c2eSJayanth Vijayaraghavan 							break;
2669a0445c2eSJayanth Vijayaraghavan 						}
2670dbc42409SLawrence Stewart 					} else {
2671a0445c2eSJayanth Vijayaraghavan 						if (SEQ_LEQ(th->th_ack,
26729d11646dSJeffrey Hsu 						    tp->snd_recover)) {
2673cb942153SJeffrey Hsu 							tp->t_dupacks = 0;
2674cb942153SJeffrey Hsu 							break;
267546f58482SJonathan Lemon 						}
2676a0445c2eSJayanth Vijayaraghavan 					}
2677dbc42409SLawrence Stewart 					/* Congestion signal before ack. */
2678dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_NDUPACK);
26794b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
26804b7b743cSLawrence Stewart 					    CC_DUPACK);
2681b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
26829b8b58e0SJonathan Lemon 					tp->t_rtttime = 0;
26830e1d7c25SRichard Scheffenegger 					if (V_tcp_do_prr) {
26840e1d7c25SRichard Scheffenegger 						/*
26850e1d7c25SRichard Scheffenegger 						 * snd_ssthresh is already updated by
26860e1d7c25SRichard Scheffenegger 						 * cc_cong_signal.
26870e1d7c25SRichard Scheffenegger 						 */
26880e1d7c25SRichard Scheffenegger 						tp->sackhint.prr_delivered = 0;
26890e1d7c25SRichard Scheffenegger 						tp->sackhint.sack_bytes_rexmit = 0;
2690bc7ee8e5SRichard Scheffenegger 						tp->sackhint.recover_fs = max(1,
2691bc7ee8e5SRichard Scheffenegger 						    tp->snd_nxt - tp->snd_una);
26920e1d7c25SRichard Scheffenegger 					}
26933529149eSAndre Oppermann 					if (tp->t_flags & TF_SACK_PERMIT) {
269478b50714SRobert Watson 						TCPSTAT_INC(
269578b50714SRobert Watson 						    tcps_sack_recovery_episode);
2696a3574665SMichael Tuexen 						tp->snd_recover = tp->snd_nxt;
26970c39d38dSGleb Smirnoff 						tp->snd_cwnd = maxseg;
269855bceb1eSRandall Stewart 						(void) tp->t_fb->tfb_tcp_output(tp);
26996d90faf3SPaul Saab 						goto drop;
27006d90faf3SPaul Saab 					}
2701fb59c426SYoshinobu Inoue 					tp->snd_nxt = th->th_ack;
27020c39d38dSGleb Smirnoff 					tp->snd_cwnd = maxseg;
270355bceb1eSRandall Stewart 					(void) tp->t_fb->tfb_tcp_output(tp);
270448d2549cSJeffrey Hsu 					KASSERT(tp->snd_limited <= 2,
2705302ce8d6SAndre Oppermann 					    ("%s: tp->snd_limited too big",
2706302ce8d6SAndre Oppermann 					    __func__));
2707df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
27080c39d38dSGleb Smirnoff 					     maxseg *
270948d2549cSJeffrey Hsu 					     (tp->t_dupacks - tp->snd_limited);
2710df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
2711df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
2712df8bae1dSRodney W. Grimes 					goto drop;
2713603724d3SBjoern A. Zeeb 				} else if (V_tcp_do_rfc3042) {
271462d4443fSHiren Panchasara 					/*
271562d4443fSHiren Panchasara 					 * Process first and second duplicate
271662d4443fSHiren Panchasara 					 * ACKs. Each indicates a segment
271762d4443fSHiren Panchasara 					 * leaving the network, creating room
271862d4443fSHiren Panchasara 					 * for more. Make sure we can send a
271962d4443fSHiren Panchasara 					 * packet on reception of each duplicate
272062d4443fSHiren Panchasara 					 * ACK by increasing snd_cwnd by one
272162d4443fSHiren Panchasara 					 * segment. Restore the original
272262d4443fSHiren Panchasara 					 * snd_cwnd after packet transmission.
272362d4443fSHiren Panchasara 					 */
27244b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
27254b7b743cSLawrence Stewart 					    CC_DUPACK);
27263ac12506SJonathan T. Looney 					uint32_t oldcwnd = tp->snd_cwnd;
272748d2549cSJeffrey Hsu 					tcp_seq oldsndmax = tp->snd_max;
272848d2549cSJeffrey Hsu 					u_int sent;
27295628dd08SAndre Oppermann 					int avail;
273089c02376SJeffrey Hsu 
2731582a954bSJeffrey Hsu 					KASSERT(tp->t_dupacks == 1 ||
2732582a954bSJeffrey Hsu 					    tp->t_dupacks == 2,
2733302ce8d6SAndre Oppermann 					    ("%s: dupacks not 1 or 2",
2734302ce8d6SAndre Oppermann 					    __func__));
273561a36e3dSJeffrey Hsu 					if (tp->t_dupacks == 1)
273648d2549cSJeffrey Hsu 						tp->snd_limited = 0;
273761a36e3dSJeffrey Hsu 					tp->snd_cwnd =
273861a36e3dSJeffrey Hsu 					    (tp->snd_nxt - tp->snd_una) +
273961a36e3dSJeffrey Hsu 					    (tp->t_dupacks - tp->snd_limited) *
27400c39d38dSGleb Smirnoff 					    maxseg;
27415628dd08SAndre Oppermann 					/*
27425628dd08SAndre Oppermann 					 * Only call tcp_output when there
27435628dd08SAndre Oppermann 					 * is new data available to be sent.
27445628dd08SAndre Oppermann 					 * Otherwise we would send pure ACKs.
27455628dd08SAndre Oppermann 					 */
27465628dd08SAndre Oppermann 					SOCKBUF_LOCK(&so->so_snd);
2747cfa6009eSGleb Smirnoff 					avail = sbavail(&so->so_snd) -
27485628dd08SAndre Oppermann 					    (tp->snd_nxt - tp->snd_una);
27495628dd08SAndre Oppermann 					SOCKBUF_UNLOCK(&so->so_snd);
27505628dd08SAndre Oppermann 					if (avail > 0)
275155bceb1eSRandall Stewart 						(void) tp->t_fb->tfb_tcp_output(tp);
275248d2549cSJeffrey Hsu 					sent = tp->snd_max - oldsndmax;
27530c39d38dSGleb Smirnoff 					if (sent > maxseg) {
275489c02376SJeffrey Hsu 						KASSERT((tp->t_dupacks == 2 &&
275589c02376SJeffrey Hsu 						    tp->snd_limited == 0) ||
27560c39d38dSGleb Smirnoff 						   (sent == maxseg + 1 &&
275789c02376SJeffrey Hsu 						    tp->t_flags & TF_SENTFIN),
2758302ce8d6SAndre Oppermann 						    ("%s: sent too much",
2759302ce8d6SAndre Oppermann 						    __func__));
276048d2549cSJeffrey Hsu 						tp->snd_limited = 2;
276148d2549cSJeffrey Hsu 					} else if (sent > 0)
276248d2549cSJeffrey Hsu 						++tp->snd_limited;
2763582a954bSJeffrey Hsu 					tp->snd_cwnd = oldcwnd;
2764582a954bSJeffrey Hsu 					goto drop;
2765df8bae1dSRodney W. Grimes 				}
2766021eaf79SHiren Panchasara 			}
2767df8bae1dSRodney W. Grimes 			break;
2768021eaf79SHiren Panchasara 		} else {
2769021eaf79SHiren Panchasara 			/*
2770021eaf79SHiren Panchasara 			 * This ack is advancing the left edge, reset the
2771021eaf79SHiren Panchasara 			 * counter.
2772021eaf79SHiren Panchasara 			 */
2773021eaf79SHiren Panchasara 			tp->t_dupacks = 0;
2774021eaf79SHiren Panchasara 			/*
2775021eaf79SHiren Panchasara 			 * If this ack also has new SACK info, increment the
2776f359d6ebSRichard Scheffenegger 			 * counter as per rfc6675. The variable
2777f359d6ebSRichard Scheffenegger 			 * sack_changed tracks all changes to the SACK
2778f359d6ebSRichard Scheffenegger 			 * scoreboard, including when partial ACKs without
2779f359d6ebSRichard Scheffenegger 			 * SACK options are received, and clear the scoreboard
2780f359d6ebSRichard Scheffenegger 			 * from the left side. Such partial ACKs should not be
2781f359d6ebSRichard Scheffenegger 			 * counted as dupacks here.
2782021eaf79SHiren Panchasara 			 */
2783f359d6ebSRichard Scheffenegger 			if ((tp->t_flags & TF_SACK_PERMIT) &&
2784f359d6ebSRichard Scheffenegger 			    (to.to_flags & TOF_SACK) &&
2785f359d6ebSRichard Scheffenegger 			    sack_changed)
2786021eaf79SHiren Panchasara 				tp->t_dupacks++;
2787df8bae1dSRodney W. Grimes 		}
2788cb942153SJeffrey Hsu 
2789302ce8d6SAndre Oppermann 		KASSERT(SEQ_GT(th->th_ack, tp->snd_una),
2790302ce8d6SAndre Oppermann 		    ("%s: th_ack <= snd_una", __func__));
2791cb942153SJeffrey Hsu 
2792df8bae1dSRodney W. Grimes 		/*
2793df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
2794df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
2795df8bae1dSRodney W. Grimes 		 */
2796dbc42409SLawrence Stewart 		if (IN_FASTRECOVERY(tp->t_flags)) {
2797cb942153SJeffrey Hsu 			if (SEQ_LT(th->th_ack, tp->snd_recover)) {
27983529149eSAndre Oppermann 				if (tp->t_flags & TF_SACK_PERMIT)
27990e1d7c25SRichard Scheffenegger 					if (V_tcp_do_prr)
28000e1d7c25SRichard Scheffenegger 						tcp_prr_partialack(tp, th);
28010e1d7c25SRichard Scheffenegger 					else
28026d90faf3SPaul Saab 						tcp_sack_partialack(tp, th);
28036d90faf3SPaul Saab 				else
2804c068736aSJeffrey Hsu 					tcp_newreno_partial_ack(tp, th);
2805dbc42409SLawrence Stewart 			} else
2806dbc42409SLawrence Stewart 				cc_post_recovery(tp, th);
280746f58482SJonathan Lemon 		}
2808a0292f23SGarrett Wollman 		/*
2809a0292f23SGarrett Wollman 		 * If we reach this point, ACK is not a duplicate,
2810a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
2811a0292f23SGarrett Wollman 		 */
2812a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
2813a0292f23SGarrett Wollman 			/*
2814a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
2815a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
281607e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
281707e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
281807e43e10SAndras Olah 			 * we can do window scaling.
2819a0292f23SGarrett Wollman 			 */
2820a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
2821a0292f23SGarrett Wollman 			tp->snd_una++;
282207e43e10SAndras Olah 			/* Do window scaling? */
282307e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
282407e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
282507e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2826464fcfbcSAndre Oppermann 				/* Send window already scaled. */
282707e43e10SAndras Olah 			}
2828a0292f23SGarrett Wollman 		}
2829a0292f23SGarrett Wollman 
2830a0292f23SGarrett Wollman process_ACK:
28318501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
28327cfc6904SRobert Watson 
2833b2ade6b1SRichard Scheffenegger 		/*
2834b2ade6b1SRichard Scheffenegger 		 * Adjust for the SYN bit in sequence space,
2835b2ade6b1SRichard Scheffenegger 		 * but don't account for it in cwnd calculations.
2836b2ade6b1SRichard Scheffenegger 		 * This is for the SYN_RECEIVED, non-simultaneous
2837b2ade6b1SRichard Scheffenegger 		 * SYN case. SYN_SENT and simultaneous SYN are
2838b2ade6b1SRichard Scheffenegger 		 * treated elsewhere.
2839b2ade6b1SRichard Scheffenegger 		 */
2840b2ade6b1SRichard Scheffenegger 		if (incforsyn)
2841b2ade6b1SRichard Scheffenegger 			tp->snd_una++;
2842dbc42409SLawrence Stewart 		acked = BYTES_THIS_ACK(tp, th);
2843b8c2cd15SJonathan T. Looney 		KASSERT(acked >= 0, ("%s: acked unexepectedly negative "
2844b8c2cd15SJonathan T. Looney 		    "(tp->snd_una=%u, th->th_ack=%u, tp=%p, m=%p)", __func__,
2845b8c2cd15SJonathan T. Looney 		    tp->snd_una, th->th_ack, tp, m));
28464b7b743cSLawrence Stewart 		TCPSTAT_ADD(tcps_rcvackpack, nsegs);
284778b50714SRobert Watson 		TCPSTAT_ADD(tcps_rcvackbyte, acked);
2848df8bae1dSRodney W. Grimes 
2849df8bae1dSRodney W. Grimes 		/*
28509b8b58e0SJonathan Lemon 		 * If we just performed our first retransmit, and the ACK
28519b8b58e0SJonathan Lemon 		 * arrives within our recovery window, then it was a mistake
28529b8b58e0SJonathan Lemon 		 * to do the retransmit in the first place.  Recover our
28539b8b58e0SJonathan Lemon 		 * original cwnd and ssthresh, and proceed to transmit where
28549b8b58e0SJonathan Lemon 		 * we left off.
28559b8b58e0SJonathan Lemon 		 */
285610d20c84SMatt Macy 		if (tp->t_rxtshift == 1 &&
285710d20c84SMatt Macy 		    tp->t_flags & TF_PREVVALID &&
285810d20c84SMatt Macy 		    tp->t_badrxtwin &&
285910d20c84SMatt Macy 		    SEQ_LT(to.to_tsecr, tp->t_badrxtwin))
2860dbc42409SLawrence Stewart 			cc_cong_signal(tp, th, CC_RTO_ERR);
28619b8b58e0SJonathan Lemon 
28629b8b58e0SJonathan Lemon 		/*
2863df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
2864df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
2865df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
2866df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
2867df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
2868df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
2869df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
2870fa55172bSMatthew Dillon 		 *
2871fa55172bSMatthew Dillon 		 * Some boxes send broken timestamp replies
2872fa55172bSMatthew Dillon 		 * during the SYN+ACK phase, ignore
2873fa55172bSMatthew Dillon 		 * timestamps of 0 or we could calculate a
2874fa55172bSMatthew Dillon 		 * huge RTT and blow up the retransmit timer.
2875df8bae1dSRodney W. Grimes 		 */
2876d8951c8aSBjoern A. Zeeb 		if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) {
28773ac12506SJonathan T. Looney 			uint32_t t;
2878d8951c8aSBjoern A. Zeeb 
2879d8951c8aSBjoern A. Zeeb 			t = tcp_ts_getticks() - to.to_tsecr;
2880d8951c8aSBjoern A. Zeeb 			if (!tp->t_rttlow || tp->t_rttlow > t)
2881d8951c8aSBjoern A. Zeeb 				tp->t_rttlow = t;
2882d8951c8aSBjoern A. Zeeb 			tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1);
2883fa55172bSMatthew Dillon 		} else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
2884eaf80179SAndre Oppermann 			if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime)
2885eaf80179SAndre Oppermann 				tp->t_rttlow = ticks - tp->t_rtttime;
28869b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
2887fa55172bSMatthew Dillon 		}
2888df8bae1dSRodney W. Grimes 
2889df8bae1dSRodney W. Grimes 		/*
2890df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
2891df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
2892df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
2893df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
2894df8bae1dSRodney W. Grimes 		 */
2895fb59c426SYoshinobu Inoue 		if (th->th_ack == tp->snd_max) {
2896b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
2897df8bae1dSRodney W. Grimes 			needoutput = 1;
2898b8152ba7SAndre Oppermann 		} else if (!tcp_timer_active(tp, TT_PERSIST))
2899b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
2900a0292f23SGarrett Wollman 
2901a0292f23SGarrett Wollman 		/*
2902a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
2903a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
2904a0292f23SGarrett Wollman 		 */
2905a0292f23SGarrett Wollman 		if (acked == 0)
2906a0292f23SGarrett Wollman 			goto step6;
2907a0292f23SGarrett Wollman 
2908df8bae1dSRodney W. Grimes 		/*
2909dbc42409SLawrence Stewart 		 * Let the congestion control algorithm update congestion
2910dbc42409SLawrence Stewart 		 * control related information. This typically means increasing
2911dbc42409SLawrence Stewart 		 * the congestion window.
2912df8bae1dSRodney W. Grimes 		 */
29134b7b743cSLawrence Stewart 		cc_ack_received(tp, th, nsegs, CC_ACK);
2914dbc42409SLawrence Stewart 
29155905999bSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
2916cfa6009eSGleb Smirnoff 		if (acked > sbavail(&so->so_snd)) {
2917b8c2cd15SJonathan T. Looney 			if (tp->snd_wnd >= sbavail(&so->so_snd))
2918cfa6009eSGleb Smirnoff 				tp->snd_wnd -= sbavail(&so->so_snd);
2919b8c2cd15SJonathan T. Looney 			else
2920b8c2cd15SJonathan T. Looney 				tp->snd_wnd = 0;
2921c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd,
2922cfa6009eSGleb Smirnoff 			    (int)sbavail(&so->so_snd));
2923df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
2924df8bae1dSRodney W. Grimes 		} else {
2925c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd, acked);
29263ac12506SJonathan T. Looney 			if (tp->snd_wnd >= (uint32_t) acked)
292760ee3bb2SAndre Oppermann 				tp->snd_wnd -= acked;
2928b8c2cd15SJonathan T. Looney 			else
2929b8c2cd15SJonathan T. Looney 				tp->snd_wnd = 0;
2930df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
2931df8bae1dSRodney W. Grimes 		}
29324d0770f1SRichard Scheffenegger 		SOCKBUF_UNLOCK(&so->so_snd);
29334d0770f1SRichard Scheffenegger 		tp->t_flags |= TF_WAKESOW;
2934c11a15bfSGleb Smirnoff 		m_freem(mfree);
2935e8949f74SAndre Oppermann 		/* Detect una wraparound. */
2936dbc42409SLawrence Stewart 		if (!IN_RECOVERY(tp->t_flags) &&
29379d11646dSJeffrey Hsu 		    SEQ_GT(tp->snd_una, tp->snd_recover) &&
29389d11646dSJeffrey Hsu 		    SEQ_LEQ(th->th_ack, tp->snd_recover))
29399d11646dSJeffrey Hsu 			tp->snd_recover = th->th_ack - 1;
2940dbc42409SLawrence Stewart 		/* XXXLAS: Can this be moved up into cc_post_recovery? */
2941dbc42409SLawrence Stewart 		if (IN_RECOVERY(tp->t_flags) &&
294224cb0f22SLawrence Stewart 		    SEQ_GEQ(th->th_ack, tp->snd_recover)) {
2943dbc42409SLawrence Stewart 			EXIT_RECOVERY(tp->t_flags);
294424cb0f22SLawrence Stewart 		}
2945fb59c426SYoshinobu Inoue 		tp->snd_una = th->th_ack;
29463529149eSAndre Oppermann 		if (tp->t_flags & TF_SACK_PERMIT) {
29476d90faf3SPaul Saab 			if (SEQ_GT(tp->snd_una, tp->snd_recover))
29486d90faf3SPaul Saab 				tp->snd_recover = tp->snd_una;
29496d90faf3SPaul Saab 		}
2950df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2951df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
2952df8bae1dSRodney W. Grimes 
2953df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2954df8bae1dSRodney W. Grimes 		/*
2955df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
2956df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
2957df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
2958df8bae1dSRodney W. Grimes 		 */
2959df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2960df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2961df8bae1dSRodney W. Grimes 				/*
2962df8bae1dSRodney W. Grimes 				 * If we can't receive any more
2963df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
2964df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
2965df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
2966df8bae1dSRodney W. Grimes 				 * we'll hang forever.
2967e8949f74SAndre Oppermann 				 *
2968e8949f74SAndre Oppermann 				 * XXXjl:
2969340c35deSJonathan Lemon 				 * we should release the tp also, and use a
2970340c35deSJonathan Lemon 				 * compressed state.
2971340c35deSJonathan Lemon 				 */
2972c0b99ffaSRobert Watson 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
297303e49181SSeigo Tanimura 					soisdisconnected(so);
29749077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
29759077f387SGleb Smirnoff 					    (tcp_fast_finwait2_recycle ?
29769077f387SGleb Smirnoff 					    tcp_finwait2_timeout :
29779077f387SGleb Smirnoff 					    TP_MAXIDLE(tp)));
29784cc20ab1SSeigo Tanimura 				}
297957f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_2);
2980df8bae1dSRodney W. Grimes 			}
2981df8bae1dSRodney W. Grimes 			break;
2982df8bae1dSRodney W. Grimes 
2983df8bae1dSRodney W. Grimes 		/*
2984df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
2985df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
2986df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
2987df8bae1dSRodney W. Grimes 		 * the segment.
2988df8bae1dSRodney W. Grimes 		 */
2989df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
2990df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
299111a20fb8SJeffrey Hsu 				tcp_twstart(tp);
2992340c35deSJonathan Lemon 				m_freem(m);
2993679d9708SAndre Oppermann 				return;
2994df8bae1dSRodney W. Grimes 			}
2995df8bae1dSRodney W. Grimes 			break;
2996df8bae1dSRodney W. Grimes 
2997df8bae1dSRodney W. Grimes 		/*
2998df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
2999df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
3000df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
3001df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
3002df8bae1dSRodney W. Grimes 		 */
3003df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
3004df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
3005df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
3006df8bae1dSRodney W. Grimes 				goto drop;
3007df8bae1dSRodney W. Grimes 			}
3008df8bae1dSRodney W. Grimes 			break;
3009df8bae1dSRodney W. Grimes 		}
3010df8bae1dSRodney W. Grimes 	}
3011df8bae1dSRodney W. Grimes 
3012df8bae1dSRodney W. Grimes step6:
30138501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
30147cfc6904SRobert Watson 
3015df8bae1dSRodney W. Grimes 	/*
301660ee3bb2SAndre Oppermann 	 * Update window information.
301760ee3bb2SAndre Oppermann 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
3018df8bae1dSRodney W. Grimes 	 */
301960ee3bb2SAndre Oppermann 	if ((thflags & TH_ACK) &&
302060ee3bb2SAndre Oppermann 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
302160ee3bb2SAndre Oppermann 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
302260ee3bb2SAndre Oppermann 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
302360ee3bb2SAndre Oppermann 		/* keep track of pure window updates */
302460ee3bb2SAndre Oppermann 		if (tlen == 0 &&
302560ee3bb2SAndre Oppermann 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
302678b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvwinupd);
3027df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
302860ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq;
302960ee3bb2SAndre Oppermann 		tp->snd_wl2 = th->th_ack;
3030df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
3031df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
303260ee3bb2SAndre Oppermann 		needoutput = 1;
3033df8bae1dSRodney W. Grimes 	}
3034df8bae1dSRodney W. Grimes 
3035df8bae1dSRodney W. Grimes 	/*
3036df8bae1dSRodney W. Grimes 	 * Process segments with URG.
3037df8bae1dSRodney W. Grimes 	 */
3038fb59c426SYoshinobu Inoue 	if ((thflags & TH_URG) && th->th_urp &&
3039df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
3040df8bae1dSRodney W. Grimes 		/*
3041df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
3042df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
3043df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
3044df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
3045df8bae1dSRodney W. Grimes 		 */
304618ad5842SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
3047cfa6009eSGleb Smirnoff 		if (th->th_urp + sbavail(&so->so_rcv) > sb_max) {
3048fb59c426SYoshinobu Inoue 			th->th_urp = 0;			/* XXX */
3049fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;		/* XXX */
305018ad5842SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);	/* XXX */
3051df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
3052df8bae1dSRodney W. Grimes 		}
3053df8bae1dSRodney W. Grimes 		/*
3054df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
3055df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
3056df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
3057df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
3058df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
3059df8bae1dSRodney W. Grimes 		 *
3060df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
3061df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
3062df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
3063df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
3064df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
3065df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
3066df8bae1dSRodney W. Grimes 		 */
3067fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
3068fb59c426SYoshinobu Inoue 			tp->rcv_up = th->th_seq + th->th_urp;
3069cfa6009eSGleb Smirnoff 			so->so_oobmark = sbavail(&so->so_rcv) +
3070df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
3071927c5ceaSRobert Watson 			if (so->so_oobmark == 0)
3072c0b99ffaSRobert Watson 				so->so_rcv.sb_state |= SBS_RCVATMARK;
3073df8bae1dSRodney W. Grimes 			sohasoutofband(so);
3074df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
3075df8bae1dSRodney W. Grimes 		}
307618ad5842SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
3077df8bae1dSRodney W. Grimes 		/*
3078df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
3079df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
3080df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
3081df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
3082df8bae1dSRodney W. Grimes 		 */
30833ac12506SJonathan T. Looney 		if (th->th_urp <= (uint32_t)tlen &&
308430613f56SJeffrey Hsu 		    !(so->so_options & SO_OOBINLINE)) {
308530613f56SJeffrey Hsu 			/* hdr drop is delayed */
308630613f56SJeffrey Hsu 			tcp_pulloutofband(so, th, m, drop_hdrlen);
308730613f56SJeffrey Hsu 		}
3088c068736aSJeffrey Hsu 	} else {
3089df8bae1dSRodney W. Grimes 		/*
3090df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
3091df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
3092df8bae1dSRodney W. Grimes 		 * with the receive window.
3093df8bae1dSRodney W. Grimes 		 */
3094df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
3095df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
3096c068736aSJeffrey Hsu 	}
3097df8bae1dSRodney W. Grimes dodata:							/* XXX */
30988501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
30997cfc6904SRobert Watson 
3100df8bae1dSRodney W. Grimes 	/*
3101df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
3102df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
3103df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
3104df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
3105df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
3106df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
3107df8bae1dSRodney W. Grimes 	 */
3108281a0fd4SPatrick Kelsey 	tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
310968bd7ed1SJonathan T. Looney 		   IS_FASTOPEN(tp->t_flags));
3110f1ea4e41SRandall Stewart 	if ((tlen || (thflags & TH_FIN) || (tfo_syn && tlen > 0)) &&
3111df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
311269e03620SPaul Saab 		tcp_seq save_start = th->th_seq;
31135acfd95cSMichael Tuexen 		tcp_seq save_rnxt  = tp->rcv_nxt;
31145acfd95cSMichael Tuexen 		int     save_tlen  = tlen;
3115fb59c426SYoshinobu Inoue 		m_adj(m, drop_hdrlen);	/* delayed header drop */
3116e4b64281SJesper Skriver 		/*
3117c068736aSJeffrey Hsu 		 * Insert segment which includes th into TCP reassembly queue
3118c068736aSJeffrey Hsu 		 * with control block tp.  Set thflags to whether reassembly now
3119c068736aSJeffrey Hsu 		 * includes a segment with FIN.  This handles the common case
3120c068736aSJeffrey Hsu 		 * inline (segment is the next to be received on an established
3121c068736aSJeffrey Hsu 		 * connection, and the queue is empty), avoiding linkage into
3122c068736aSJeffrey Hsu 		 * and removal from the queue and repetition of various
3123c068736aSJeffrey Hsu 		 * conversions.
3124c068736aSJeffrey Hsu 		 * Set DELACK for segments received in order, but ack
3125c068736aSJeffrey Hsu 		 * immediately when segments are out of order (so
3126c068736aSJeffrey Hsu 		 * fast retransmit can work).
3127e4b64281SJesper Skriver 		 */
31284741bfcbSPatrick Kelsey 		if (th->th_seq == tp->rcv_nxt &&
3129c28440dbSRandall Stewart 		    SEGQ_EMPTY(tp) &&
3130281a0fd4SPatrick Kelsey 		    (TCPS_HAVEESTABLISHED(tp->t_state) ||
3131281a0fd4SPatrick Kelsey 		     tfo_syn)) {
3132281a0fd4SPatrick Kelsey 			if (DELAY_ACK(tp, tlen) || tfo_syn)
31333bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
3134e4b64281SJesper Skriver 			else
3135e4b64281SJesper Skriver 				tp->t_flags |= TF_ACKNOW;
3136e4b64281SJesper Skriver 			tp->rcv_nxt += tlen;
3137e854dd38SRandall Stewart 			if (tlen &&
3138e854dd38SRandall Stewart 			    ((tp->t_flags2 & TF2_FBYTES_COMPLETE) == 0) &&
3139e854dd38SRandall Stewart 			    (tp->t_fbyte_in == 0)) {
3140e854dd38SRandall Stewart 				tp->t_fbyte_in = ticks;
3141e854dd38SRandall Stewart 				if (tp->t_fbyte_in == 0)
3142e854dd38SRandall Stewart 					tp->t_fbyte_in = 1;
3143e854dd38SRandall Stewart 				if (tp->t_fbyte_out && tp->t_fbyte_in)
3144e854dd38SRandall Stewart 					tp->t_flags2 |= TF2_FBYTES_COMPLETE;
3145e854dd38SRandall Stewart 			}
3146e4b64281SJesper Skriver 			thflags = th->th_flags & TH_FIN;
314778b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpack);
314878b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
31491e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
3150c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
3151c1c36a2cSMike Silbersack 				m_freem(m);
3152c1c36a2cSMike Silbersack 			else
3153651e4e6aSGleb Smirnoff 				sbappendstream_locked(&so->so_rcv, m, 0);
31544d0770f1SRichard Scheffenegger 			SOCKBUF_UNLOCK(&so->so_rcv);
31554d0770f1SRichard Scheffenegger 			tp->t_flags |= TF_WAKESOR;
3156e4b64281SJesper Skriver 		} else {
3157e8949f74SAndre Oppermann 			/*
3158e8949f74SAndre Oppermann 			 * XXX: Due to the header drop above "th" is
3159e8949f74SAndre Oppermann 			 * theoretically invalid by now.  Fortunately
3160e8949f74SAndre Oppermann 			 * m_adj() doesn't actually frees any mbufs
3161e8949f74SAndre Oppermann 			 * when trimming from the head.
3162e8949f74SAndre Oppermann 			 */
31635acfd95cSMichael Tuexen 			tcp_seq temp = save_start;
31645acfd95cSMichael Tuexen 			thflags = tcp_reass(tp, th, &temp, &tlen, m);
3165e4b64281SJesper Skriver 			tp->t_flags |= TF_ACKNOW;
3166e4b64281SJesper Skriver 		}
31676d261981SMichael Tuexen 		if ((tp->t_flags & TF_SACK_PERMIT) && (save_tlen > 0)) {
31686d261981SMichael Tuexen 			if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) {
3169b5a154d8SMichael Tuexen 				/*
3170b5a154d8SMichael Tuexen 				 * DSACK actually handled in the fastpath
3171b5a154d8SMichael Tuexen 				 * above.
3172b5a154d8SMichael Tuexen 				 */
3173ecc5b1d1SMichael Tuexen 				tcp_update_sack_list(tp, save_start,
3174ecc5b1d1SMichael Tuexen 				    save_start + save_tlen);
3175ecc5b1d1SMichael Tuexen 			} else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) {
3176ecc5b1d1SMichael Tuexen 				if ((tp->rcv_numsacks >= 1) &&
3177ecc5b1d1SMichael Tuexen 				    (tp->sackblks[0].end == save_start)) {
31786d261981SMichael Tuexen 					/*
31796d261981SMichael Tuexen 					 * Partial overlap, recorded at todrop
31806d261981SMichael Tuexen 					 * above.
31816d261981SMichael Tuexen 					 */
31826d261981SMichael Tuexen 					tcp_update_sack_list(tp,
31836d261981SMichael Tuexen 					    tp->sackblks[0].start,
3184ecc5b1d1SMichael Tuexen 					    tp->sackblks[0].end);
3185ecc5b1d1SMichael Tuexen 				} else {
3186ecc5b1d1SMichael Tuexen 					tcp_update_dsack_list(tp, save_start,
3187ecc5b1d1SMichael Tuexen 					    save_start + save_tlen);
3188ecc5b1d1SMichael Tuexen 				}
31896d261981SMichael Tuexen 			} else if (tlen >= save_tlen) {
3190b5a154d8SMichael Tuexen 				/* Update of sackblks. */
3191ecc5b1d1SMichael Tuexen 				tcp_update_dsack_list(tp, save_start,
3192ecc5b1d1SMichael Tuexen 				    save_start + save_tlen);
3193ecc5b1d1SMichael Tuexen 			} else if (tlen > 0) {
3194ecc5b1d1SMichael Tuexen 				tcp_update_dsack_list(tp, save_start,
3195ecc5b1d1SMichael Tuexen 				    save_start + tlen);
31965acfd95cSMichael Tuexen 			}
31975acfd95cSMichael Tuexen 		}
3198302ce8d6SAndre Oppermann #if 0
3199df8bae1dSRodney W. Grimes 		/*
3200df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
3201df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
3202df8bae1dSRodney W. Grimes 		 * buffer size.
3203302ce8d6SAndre Oppermann 		 * XXX: Unused.
3204df8bae1dSRodney W. Grimes 		 */
3205f701e30dSJohn Baldwin 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt))
3206df8bae1dSRodney W. Grimes 			len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
3207f701e30dSJohn Baldwin 		else
3208f701e30dSJohn Baldwin 			len = so->so_rcv.sb_hiwat;
3209302ce8d6SAndre Oppermann #endif
3210df8bae1dSRodney W. Grimes 	} else {
3211df8bae1dSRodney W. Grimes 		m_freem(m);
3212fb59c426SYoshinobu Inoue 		thflags &= ~TH_FIN;
3213df8bae1dSRodney W. Grimes 	}
3214df8bae1dSRodney W. Grimes 
3215df8bae1dSRodney W. Grimes 	/*
3216df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
3217df8bae1dSRodney W. Grimes 	 * that the connection is closing.
3218df8bae1dSRodney W. Grimes 	 */
3219fb59c426SYoshinobu Inoue 	if (thflags & TH_FIN) {
3220df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
3221df8bae1dSRodney W. Grimes 			socantrcvmore(so);
32224d0770f1SRichard Scheffenegger 			/* The socket upcall is handled by socantrcvmore. */
32234d0770f1SRichard Scheffenegger 			tp->t_flags &= ~TF_WAKESOR;
3224a0292f23SGarrett Wollman 			/*
3225a0292f23SGarrett Wollman 			 * If connection is half-synchronized
3226d3eede9dSAndras Olah 			 * (ie NEEDSYN flag on) then delay ACK,
3227a0292f23SGarrett Wollman 			 * so it may be piggybacked when SYN is sent.
3228a0292f23SGarrett Wollman 			 * Otherwise, since we received a FIN then no
3229a0292f23SGarrett Wollman 			 * more input can be expected, send ACK now.
3230a0292f23SGarrett Wollman 			 */
32313bfd6421SJonathan Lemon 			if (tp->t_flags & TF_NEEDSYN)
32323bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
3233a0292f23SGarrett Wollman 			else
3234df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
3235df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
3236df8bae1dSRodney W. Grimes 		}
3237df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
3238df8bae1dSRodney W. Grimes 		/*
3239df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
3240df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
3241df8bae1dSRodney W. Grimes 		 */
3242df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
32439b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
32449b8b58e0SJonathan Lemon 			/* FALLTHROUGH */
3245df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
324657f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSE_WAIT);
3247df8bae1dSRodney W. Grimes 			break;
3248df8bae1dSRodney W. Grimes 
3249df8bae1dSRodney W. Grimes 		/*
3250df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
3251df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
3252df8bae1dSRodney W. Grimes 		 */
3253df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
325457f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSING);
3255df8bae1dSRodney W. Grimes 			break;
3256df8bae1dSRodney W. Grimes 
3257df8bae1dSRodney W. Grimes 		/*
3258df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
3259df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
3260df8bae1dSRodney W. Grimes 		 * standard timers.
3261df8bae1dSRodney W. Grimes 		 */
3262df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
3263340c35deSJonathan Lemon 			tcp_twstart(tp);
3264679d9708SAndre Oppermann 			return;
3265df8bae1dSRodney W. Grimes 		}
3266df8bae1dSRodney W. Grimes 	}
3267610ee2f9SDavid Greenman #ifdef TCPDEBUG
32684cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
3269fb59c426SYoshinobu Inoue 		tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
3270fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3271610ee2f9SDavid Greenman #endif
32722b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
3273df8bae1dSRodney W. Grimes 
3274df8bae1dSRodney W. Grimes 	/*
3275df8bae1dSRodney W. Grimes 	 * Return any desired output.
3276df8bae1dSRodney W. Grimes 	 */
3277df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
327855bceb1eSRandall Stewart 		(void) tp->t_fb->tfb_tcp_output(tp);
32797792ea27SJeffrey Hsu 
3280a14c749fSJonathan Lemon check_delack:
32818501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
3282252ca428SRobert Watson 
3283a14c749fSJonathan Lemon 	if (tp->t_flags & TF_DELACK) {
32843bfd6421SJonathan Lemon 		tp->t_flags &= ~TF_DELACK;
3285b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
32863bfd6421SJonathan Lemon 	}
32874d0770f1SRichard Scheffenegger 	tcp_handle_wakeup(tp, so);
32888501a69cSRobert Watson 	INP_WUNLOCK(tp->t_inpcb);
3289679d9708SAndre Oppermann 	return;
3290df8bae1dSRodney W. Grimes 
3291df8bae1dSRodney W. Grimes dropafterack:
3292df8bae1dSRodney W. Grimes 	/*
3293df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
3294df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
329580ab7c0eSGarrett Wollman 	 *
329680ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
329780ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
329880ab7c0eSGarrett Wollman 	 * RST have been dropped.
329980ab7c0eSGarrett Wollman 	 *
330080ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
330180ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
330280ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
330380ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
330480ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
330580ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
3306df8bae1dSRodney W. Grimes 	 */
3307fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
3308fb59c426SYoshinobu Inoue 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
3309a57815efSBosko Milekic 	     SEQ_GT(th->th_ack, tp->snd_max)) ) {
3310a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
3311a57815efSBosko Milekic 		goto dropwithreset;
3312a57815efSBosko Milekic 	}
3313a0292f23SGarrett Wollman #ifdef TCPDEBUG
33144cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
3315fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
3316fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3317a0292f23SGarrett Wollman #endif
33182b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
3319df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
332055bceb1eSRandall Stewart 	(void) tp->t_fb->tfb_tcp_output(tp);
33214d0770f1SRichard Scheffenegger 	tcp_handle_wakeup(tp, so);
33228501a69cSRobert Watson 	INP_WUNLOCK(tp->t_inpcb);
3323d6915262SRobert Watson 	m_freem(m);
3324679d9708SAndre Oppermann 	return;
3325df8bae1dSRodney W. Grimes 
3326df8bae1dSRodney W. Grimes dropwithreset:
3327a0ca0871SRobert Watson 	if (tp != NULL) {
3328302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
33294d0770f1SRichard Scheffenegger 		tcp_handle_wakeup(tp, so);
33308501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
3331dd8ac7f9SRobert Watson 	} else
3332a0ca0871SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
3333679d9708SAndre Oppermann 	return;
3334df8bae1dSRodney W. Grimes 
3335df8bae1dSRodney W. Grimes drop:
3336df8bae1dSRodney W. Grimes 	/*
3337df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
3338df8bae1dSRodney W. Grimes 	 */
3339610ee2f9SDavid Greenman #ifdef TCPDEBUG
33401c53f806SRobert Watson 	if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
3341fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
3342fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3343a0292f23SGarrett Wollman #endif
33442b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
33454d0770f1SRichard Scheffenegger 	if (tp != NULL) {
33464d0770f1SRichard Scheffenegger 		tcp_handle_wakeup(tp, so);
33478501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
33484d0770f1SRichard Scheffenegger 	}
3349d6915262SRobert Watson 	m_freem(m);
3350302ce8d6SAndre Oppermann }
3351302ce8d6SAndre Oppermann 
3352302ce8d6SAndre Oppermann /*
33530ca3f933SAndre Oppermann  * Issue RST and make ACK acceptable to originator of segment.
33540ca3f933SAndre Oppermann  * The mbuf must still include the original packet header.
33550ca3f933SAndre Oppermann  * tp may be NULL.
3356302ce8d6SAndre Oppermann  */
335755bceb1eSRandall Stewart void
3358302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp,
3359302ce8d6SAndre Oppermann     int tlen, int rstreason)
3360302ce8d6SAndre Oppermann {
3361b287c6c7SBjoern A. Zeeb #ifdef INET
3362302ce8d6SAndre Oppermann 	struct ip *ip;
3363b287c6c7SBjoern A. Zeeb #endif
3364302ce8d6SAndre Oppermann #ifdef INET6
3365302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6;
3366302ce8d6SAndre Oppermann #endif
33677a3244ccSRobert Watson 
33687a3244ccSRobert Watson 	if (tp != NULL) {
33698501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
33707a3244ccSRobert Watson 	}
33717a3244ccSRobert Watson 
33720ca3f933SAndre Oppermann 	/* Don't bother if destination was broadcast/multicast. */
3373302ce8d6SAndre Oppermann 	if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
3374302ce8d6SAndre Oppermann 		goto drop;
3375302ce8d6SAndre Oppermann #ifdef INET6
3376302ce8d6SAndre Oppermann 	if (mtod(m, struct ip *)->ip_v == 6) {
3377302ce8d6SAndre Oppermann 		ip6 = mtod(m, struct ip6_hdr *);
3378302ce8d6SAndre Oppermann 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
3379302ce8d6SAndre Oppermann 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
3380302ce8d6SAndre Oppermann 			goto drop;
3381302ce8d6SAndre Oppermann 		/* IPv6 anycast check is done at tcp6_input() */
3382b287c6c7SBjoern A. Zeeb 	}
3383302ce8d6SAndre Oppermann #endif
3384b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3385b287c6c7SBjoern A. Zeeb 	else
3386b287c6c7SBjoern A. Zeeb #endif
3387b287c6c7SBjoern A. Zeeb #ifdef INET
3388302ce8d6SAndre Oppermann 	{
3389302ce8d6SAndre Oppermann 		ip = mtod(m, struct ip *);
3390302ce8d6SAndre Oppermann 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
3391302ce8d6SAndre Oppermann 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
3392302ce8d6SAndre Oppermann 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
3393302ce8d6SAndre Oppermann 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
3394302ce8d6SAndre Oppermann 			goto drop;
3395302ce8d6SAndre Oppermann 	}
3396b287c6c7SBjoern A. Zeeb #endif
3397302ce8d6SAndre Oppermann 
3398302ce8d6SAndre Oppermann 	/* Perform bandwidth limiting. */
3399302ce8d6SAndre Oppermann 	if (badport_bandlim(rstreason) < 0)
3400302ce8d6SAndre Oppermann 		goto drop;
3401302ce8d6SAndre Oppermann 
3402302ce8d6SAndre Oppermann 	/* tcp_respond consumes the mbuf chain. */
3403302ce8d6SAndre Oppermann 	if (th->th_flags & TH_ACK) {
3404302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0,
3405302ce8d6SAndre Oppermann 		    th->th_ack, TH_RST);
3406302ce8d6SAndre Oppermann 	} else {
3407302ce8d6SAndre Oppermann 		if (th->th_flags & TH_SYN)
3408302ce8d6SAndre Oppermann 			tlen++;
34094ddd5aadSMichael Tuexen 		if (th->th_flags & TH_FIN)
34104ddd5aadSMichael Tuexen 			tlen++;
3411302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
3412302ce8d6SAndre Oppermann 		    (tcp_seq)0, TH_RST|TH_ACK);
3413302ce8d6SAndre Oppermann 	}
3414302ce8d6SAndre Oppermann 	return;
3415302ce8d6SAndre Oppermann drop:
3416302ce8d6SAndre Oppermann 	m_freem(m);
3417df8bae1dSRodney W. Grimes }
3418df8bae1dSRodney W. Grimes 
3419be2ac88cSJonathan Lemon /*
3420be2ac88cSJonathan Lemon  * Parse TCP options and place in tcpopt.
3421be2ac88cSJonathan Lemon  */
342255bceb1eSRandall Stewart void
3423ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags)
3424df8bae1dSRodney W. Grimes {
3425df8bae1dSRodney W. Grimes 	int opt, optlen;
3426df8bae1dSRodney W. Grimes 
3427be2ac88cSJonathan Lemon 	to->to_flags = 0;
3428df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
3429df8bae1dSRodney W. Grimes 		opt = cp[0];
3430df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
3431df8bae1dSRodney W. Grimes 			break;
3432df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
3433df8bae1dSRodney W. Grimes 			optlen = 1;
3434df8bae1dSRodney W. Grimes 		else {
3435b474779fSJun-ichiro itojun Hagino 			if (cnt < 2)
3436b474779fSJun-ichiro itojun Hagino 				break;
3437df8bae1dSRodney W. Grimes 			optlen = cp[1];
3438b474779fSJun-ichiro itojun Hagino 			if (optlen < 2 || optlen > cnt)
3439df8bae1dSRodney W. Grimes 				break;
3440df8bae1dSRodney W. Grimes 		}
3441df8bae1dSRodney W. Grimes 		switch (opt) {
3442df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
3443df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
3444df8bae1dSRodney W. Grimes 				continue;
3445f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3446df8bae1dSRodney W. Grimes 				continue;
3447be2ac88cSJonathan Lemon 			to->to_flags |= TOF_MSS;
3448be2ac88cSJonathan Lemon 			bcopy((char *)cp + 2,
3449be2ac88cSJonathan Lemon 			    (char *)&to->to_mss, sizeof(to->to_mss));
3450fd8e4ebcSMike Barcroft 			to->to_mss = ntohs(to->to_mss);
3451df8bae1dSRodney W. Grimes 			break;
3452df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
3453df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
3454df8bae1dSRodney W. Grimes 				continue;
3455f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3456df8bae1dSRodney W. Grimes 				continue;
3457be2ac88cSJonathan Lemon 			to->to_flags |= TOF_SCALE;
345802a1a643SAndre Oppermann 			to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT);
3459df8bae1dSRodney W. Grimes 			break;
3460df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
3461df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
3462df8bae1dSRodney W. Grimes 				continue;
3463be2ac88cSJonathan Lemon 			to->to_flags |= TOF_TS;
3464a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
3465a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
3466fd8e4ebcSMike Barcroft 			to->to_tsval = ntohl(to->to_tsval);
3467a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
3468a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
3469fd8e4ebcSMike Barcroft 			to->to_tsecr = ntohl(to->to_tsecr);
3470df8bae1dSRodney W. Grimes 			break;
34711cfd4b53SBruce M Simpson 		case TCPOPT_SIGNATURE:
3472fcf59617SAndrey V. Elsukov 			/*
3473fcf59617SAndrey V. Elsukov 			 * In order to reply to a host which has set the
3474fcf59617SAndrey V. Elsukov 			 * TCP_SIGNATURE option in its initial SYN, we have
3475fcf59617SAndrey V. Elsukov 			 * to record the fact that the option was observed
3476fcf59617SAndrey V. Elsukov 			 * here for the syncache code to perform the correct
3477fcf59617SAndrey V. Elsukov 			 * response.
3478fcf59617SAndrey V. Elsukov 			 */
34791cfd4b53SBruce M Simpson 			if (optlen != TCPOLEN_SIGNATURE)
34801cfd4b53SBruce M Simpson 				continue;
3481df47e437SAndre Oppermann 			to->to_flags |= TOF_SIGNATURE;
3482df47e437SAndre Oppermann 			to->to_signature = cp + 2;
34831cfd4b53SBruce M Simpson 			break;
34846d90faf3SPaul Saab 		case TCPOPT_SACK_PERMITTED:
3485f72167f4SAndre Oppermann 			if (optlen != TCPOLEN_SACK_PERMITTED)
34866d90faf3SPaul Saab 				continue;
3487f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3488f72167f4SAndre Oppermann 				continue;
3489603724d3SBjoern A. Zeeb 			if (!V_tcp_do_sack)
3490f72167f4SAndre Oppermann 				continue;
3491fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACKPERM;
34926d90faf3SPaul Saab 			break;
34936d90faf3SPaul Saab 		case TCPOPT_SACK:
34945a53ca16SPaul Saab 			if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
34956d90faf3SPaul Saab 				continue;
3496b728e902SAndre Oppermann 			if (flags & TO_SYN)
3497b728e902SAndre Oppermann 				continue;
3498fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACK;
34995a53ca16SPaul Saab 			to->to_nsacks = (optlen - 2) / TCPOLEN_SACK;
35005a53ca16SPaul Saab 			to->to_sacks = cp + 2;
350178b50714SRobert Watson 			TCPSTAT_INC(tcps_sack_rcv_blocks);
35026d90faf3SPaul Saab 			break;
3503281a0fd4SPatrick Kelsey 		case TCPOPT_FAST_OPEN:
3504c560df6fSPatrick Kelsey 			/*
3505c560df6fSPatrick Kelsey 			 * Cookie length validation is performed by the
3506c560df6fSPatrick Kelsey 			 * server side cookie checking code or the client
3507c560df6fSPatrick Kelsey 			 * side cookie cache update code.
3508c560df6fSPatrick Kelsey 			 */
3509281a0fd4SPatrick Kelsey 			if (!(flags & TO_SYN))
3510281a0fd4SPatrick Kelsey 				continue;
3511c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
3512c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
3513281a0fd4SPatrick Kelsey 				continue;
3514281a0fd4SPatrick Kelsey 			to->to_flags |= TOF_FASTOPEN;
3515281a0fd4SPatrick Kelsey 			to->to_tfo_len = optlen - 2;
3516281a0fd4SPatrick Kelsey 			to->to_tfo_cookie = to->to_tfo_len ? cp + 2 : NULL;
3517281a0fd4SPatrick Kelsey 			break;
3518be2ac88cSJonathan Lemon 		default:
3519be2ac88cSJonathan Lemon 			continue;
3520df8bae1dSRodney W. Grimes 		}
3521df8bae1dSRodney W. Grimes 	}
3522df8bae1dSRodney W. Grimes }
3523df8bae1dSRodney W. Grimes 
3524df8bae1dSRodney W. Grimes /*
3525df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
3526df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
3527df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
3528df8bae1dSRodney W. Grimes  * sequencing purposes.
3529df8bae1dSRodney W. Grimes  */
353055bceb1eSRandall Stewart void
3531ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m,
3532ad3f9ab3SAndre Oppermann     int off)
3533df8bae1dSRodney W. Grimes {
3534fb59c426SYoshinobu Inoue 	int cnt = off + th->th_urp - 1;
3535df8bae1dSRodney W. Grimes 
3536df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
3537df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
3538df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
3539df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
3540df8bae1dSRodney W. Grimes 
35418501a69cSRobert Watson 			INP_WLOCK_ASSERT(tp->t_inpcb);
35427a3244ccSRobert Watson 
3543df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
3544df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
3545df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
3546df8bae1dSRodney W. Grimes 			m->m_len--;
354769a34685SYoshinobu Inoue 			if (m->m_flags & M_PKTHDR)
354869a34685SYoshinobu Inoue 				m->m_pkthdr.len--;
3549df8bae1dSRodney W. Grimes 			return;
3550df8bae1dSRodney W. Grimes 		}
3551df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
3552df8bae1dSRodney W. Grimes 		m = m->m_next;
35534dfdffe9SAndre Oppermann 		if (m == NULL)
3554df8bae1dSRodney W. Grimes 			break;
3555df8bae1dSRodney W. Grimes 	}
3556df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
3557df8bae1dSRodney W. Grimes }
3558df8bae1dSRodney W. Grimes 
3559df8bae1dSRodney W. Grimes /*
3560df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
3561df8bae1dSRodney W. Grimes  * and update averages and current timeout.
3562df8bae1dSRodney W. Grimes  */
356355bceb1eSRandall Stewart void
3564ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt)
3565df8bae1dSRodney W. Grimes {
3566ad3f9ab3SAndre Oppermann 	int delta;
3567233e8c18SGarrett Wollman 
35688501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
35692be3bf22SRobert Watson 
357078b50714SRobert Watson 	TCPSTAT_INC(tcps_rttupdated);
3571233e8c18SGarrett Wollman 	tp->t_rttupdated++;
3572adc56f5aSEdward Tomasz Napierala #ifdef STATS
3573adc56f5aSEdward Tomasz Napierala 	stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RTT,
3574adc56f5aSEdward Tomasz Napierala 	    imax(0, rtt * 1000 / hz));
3575adc56f5aSEdward Tomasz Napierala #endif
35765ede40dcSRyan Stone 	if ((tp->t_srtt != 0) && (tp->t_rxtshift <= TCP_RTT_INVALIDATE)) {
3577233e8c18SGarrett Wollman 		/*
3578233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
3579233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
3580233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
3581233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
3582233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
3583233e8c18SGarrett Wollman 		 */
3584233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
3585233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
3586233e8c18SGarrett Wollman 
3587233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
3588233e8c18SGarrett Wollman 			tp->t_srtt = 1;
3589233e8c18SGarrett Wollman 
3590233e8c18SGarrett Wollman 		/*
3591233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
3592233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
3593233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
3594233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
3595233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
3596233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
3597233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
3598233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
3599233e8c18SGarrett Wollman 		 */
3600233e8c18SGarrett Wollman 		if (delta < 0)
3601233e8c18SGarrett Wollman 			delta = -delta;
3602233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
3603233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
3604233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
36051fcc99b5SMatthew Dillon 		if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
36061fcc99b5SMatthew Dillon 		    tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3607233e8c18SGarrett Wollman 	} else {
3608233e8c18SGarrett Wollman 		/*
3609233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
3610233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
3611233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
3612233e8c18SGarrett Wollman 		 */
3613233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
3614233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
36151fcc99b5SMatthew Dillon 		tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3616233e8c18SGarrett Wollman 	}
36179b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
3618df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
3619df8bae1dSRodney W. Grimes 
3620df8bae1dSRodney W. Grimes 	/*
3621df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
3622df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
3623df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
3624df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
3625df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
3626df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
3627df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
3628df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
3629df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
3630df8bae1dSRodney W. Grimes 	 */
3631233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
36329e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
3633df8bae1dSRodney W. Grimes 
3634df8bae1dSRodney W. Grimes 	/*
3635df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
3636df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
3637df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
3638df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
3639df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
3640df8bae1dSRodney W. Grimes 	 */
3641df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
3642df8bae1dSRodney W. Grimes }
3643df8bae1dSRodney W. Grimes 
3644df8bae1dSRodney W. Grimes /*
3645df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
3646df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
36474faaea55SAndre Oppermann  * If none, use an mss that can be handled on the outgoing interface
36484faaea55SAndre Oppermann  * without forcing IP to fragment.  If no route is found, route has no mtu,
3649df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
3650df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
3651df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
3652df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
3653df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
3654df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
3655df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
3656a0292f23SGarrett Wollman  *
36570c39d38dSGleb Smirnoff  * NOTE that resulting t_maxseg doesn't include space for TCP options or
36580c39d38dSGleb Smirnoff  * IP options, e.g. IPSEC data, since length of this data may vary, and
36590c39d38dSGleb Smirnoff  * thus it is calculated for every segment separately in tcp_output().
3660a0292f23SGarrett Wollman  *
366197d8d152SAndre Oppermann  * NOTE that this routine is only called when we process an incoming
3662ef341ee1SGleb Smirnoff  * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS
3663ef341ee1SGleb Smirnoff  * settings are handled in tcp_mssopt().
3664df8bae1dSRodney W. Grimes  */
3665a0292f23SGarrett Wollman void
3666ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer,
36673c914c54SAndre Oppermann     struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap)
3668df8bae1dSRodney W. Grimes {
3669b287c6c7SBjoern A. Zeeb 	int mss = 0;
36703ac12506SJonathan T. Looney 	uint32_t maxmtu = 0;
3671c068736aSJeffrey Hsu 	struct inpcb *inp = tp->t_inpcb;
367297d8d152SAndre Oppermann 	struct hc_metrics_lite metrics;
3673fb59c426SYoshinobu Inoue #ifdef INET6
3674c068736aSJeffrey Hsu 	int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
3675c068736aSJeffrey Hsu 	size_t min_protoh = isipv6 ?
3676c068736aSJeffrey Hsu 			    sizeof (struct ip6_hdr) + sizeof (struct tcphdr) :
3677c068736aSJeffrey Hsu 			    sizeof (struct tcpiphdr);
3678c068736aSJeffrey Hsu #else
3679c068736aSJeffrey Hsu 	const size_t min_protoh = sizeof(struct tcpiphdr);
3680fb59c426SYoshinobu Inoue #endif
3681df8bae1dSRodney W. Grimes 
36828501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
36837a3244ccSRobert Watson 
3684ef341ee1SGleb Smirnoff 	if (mtuoffer != -1) {
3685ef341ee1SGleb Smirnoff 		KASSERT(offer == -1, ("%s: conflict", __func__));
3686ef341ee1SGleb Smirnoff 		offer = mtuoffer - min_protoh;
3687ef341ee1SGleb Smirnoff 	}
3688ef341ee1SGleb Smirnoff 
3689e8949f74SAndre Oppermann 	/* Initialize. */
369097d8d152SAndre Oppermann #ifdef INET6
369197d8d152SAndre Oppermann 	if (isipv6) {
36923c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu6(&inp->inp_inc, cap);
36930c39d38dSGleb Smirnoff 		tp->t_maxseg = V_tcp_v6mssdflt;
3694b287c6c7SBjoern A. Zeeb 	}
369597d8d152SAndre Oppermann #endif
3696b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3697b287c6c7SBjoern A. Zeeb 	else
3698b287c6c7SBjoern A. Zeeb #endif
3699b287c6c7SBjoern A. Zeeb #ifdef INET
370097d8d152SAndre Oppermann 	{
37013c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu(&inp->inp_inc, cap);
37020c39d38dSGleb Smirnoff 		tp->t_maxseg = V_tcp_mssdflt;
3703df8bae1dSRodney W. Grimes 	}
3704b287c6c7SBjoern A. Zeeb #endif
3705df8bae1dSRodney W. Grimes 
370697d8d152SAndre Oppermann 	/*
3707e8949f74SAndre Oppermann 	 * No route to sender, stay with default mss and return.
370897d8d152SAndre Oppermann 	 */
37096f01cac6SBjoern A. Zeeb 	if (maxmtu == 0) {
37106f01cac6SBjoern A. Zeeb 		/*
37118e5c87f4SBjoern A. Zeeb 		 * In case we return early we need to initialize metrics
37126f01cac6SBjoern A. Zeeb 		 * to a defined state as tcp_hc_get() would do for us
37136f01cac6SBjoern A. Zeeb 		 * if there was no cache hit.
37146f01cac6SBjoern A. Zeeb 		 */
37156f01cac6SBjoern A. Zeeb 		if (metricptr != NULL)
37166f01cac6SBjoern A. Zeeb 			bzero(metricptr, sizeof(struct hc_metrics_lite));
371797d8d152SAndre Oppermann 		return;
37186f01cac6SBjoern A. Zeeb 	}
371997d8d152SAndre Oppermann 
3720e8949f74SAndre Oppermann 	/* What have we got? */
372197d8d152SAndre Oppermann 	switch (offer) {
372297d8d152SAndre Oppermann 		case 0:
372397d8d152SAndre Oppermann 			/*
372497d8d152SAndre Oppermann 			 * Offer == 0 means that there was no MSS on the SYN
3725c3b02504SBjoern A. Zeeb 			 * segment, in this case we use tcp_mssdflt as
37260c39d38dSGleb Smirnoff 			 * already assigned to t_maxseg above.
372797d8d152SAndre Oppermann 			 */
37280c39d38dSGleb Smirnoff 			offer = tp->t_maxseg;
372997d8d152SAndre Oppermann 			break;
373097d8d152SAndre Oppermann 
373197d8d152SAndre Oppermann 		case -1:
3732a0292f23SGarrett Wollman 			/*
3733c94c54e4SAndre Oppermann 			 * Offer == -1 means that we didn't receive SYN yet.
3734a0292f23SGarrett Wollman 			 */
373597d8d152SAndre Oppermann 			/* FALLTHROUGH */
373697d8d152SAndre Oppermann 
373797d8d152SAndre Oppermann 		default:
3738a0292f23SGarrett Wollman 			/*
373953369ac9SAndre Oppermann 			 * Prevent DoS attack with too small MSS. Round up
374053369ac9SAndre Oppermann 			 * to at least minmss.
374153369ac9SAndre Oppermann 			 */
3742603724d3SBjoern A. Zeeb 			offer = max(offer, V_tcp_minmss);
374397d8d152SAndre Oppermann 	}
3744a0292f23SGarrett Wollman 
3745df8bae1dSRodney W. Grimes 	/*
3746e8949f74SAndre Oppermann 	 * rmx information is now retrieved from tcp_hostcache.
3747df8bae1dSRodney W. Grimes 	 */
374897d8d152SAndre Oppermann 	tcp_hc_get(&inp->inp_inc, &metrics);
37493cee92e0SBjoern A. Zeeb 	if (metricptr != NULL)
37503cee92e0SBjoern A. Zeeb 		bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite));
375197d8d152SAndre Oppermann 
3752df8bae1dSRodney W. Grimes 	/*
3753cc412412SHiren Panchasara 	 * If there's a discovered mtu in tcp hostcache, use it.
3754cc412412SHiren Panchasara 	 * Else, use the link mtu.
3755df8bae1dSRodney W. Grimes 	 */
375697d8d152SAndre Oppermann 	if (metrics.rmx_mtu)
37572d166c02SAndre Oppermann 		mss = min(metrics.rmx_mtu, maxmtu) - min_protoh;
3758c068736aSJeffrey Hsu 	else {
375931b3783cSHajimu UMEMOTO #ifdef INET6
3760fb59c426SYoshinobu Inoue 		if (isipv6) {
376197d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3762603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
376397d8d152SAndre Oppermann 			    !in6_localaddr(&inp->in6p_faddr))
3764603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_v6mssdflt);
3765b287c6c7SBjoern A. Zeeb 		}
3766b3399803SHajimu UMEMOTO #endif
3767b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3768b287c6c7SBjoern A. Zeeb 		else
3769b287c6c7SBjoern A. Zeeb #endif
3770b287c6c7SBjoern A. Zeeb #ifdef INET
377197d8d152SAndre Oppermann 		{
377297d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3773603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
377497d8d152SAndre Oppermann 			    !in_localaddr(inp->inp_faddr))
3775603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_mssdflt);
3776a0292f23SGarrett Wollman 		}
3777b287c6c7SBjoern A. Zeeb #endif
37783cee92e0SBjoern A. Zeeb 		/*
37793cee92e0SBjoern A. Zeeb 		 * XXX - The above conditional (mss = maxmtu - min_protoh)
37803cee92e0SBjoern A. Zeeb 		 * probably violates the TCP spec.
37813cee92e0SBjoern A. Zeeb 		 * The problem is that, since we don't know the
37823cee92e0SBjoern A. Zeeb 		 * other end's MSS, we are supposed to use a conservative
37833cee92e0SBjoern A. Zeeb 		 * default.  But, if we do that, then MTU discovery will
37843cee92e0SBjoern A. Zeeb 		 * never actually take place, because the conservative
37853cee92e0SBjoern A. Zeeb 		 * default is much less than the MTUs typically seen
37863cee92e0SBjoern A. Zeeb 		 * on the Internet today.  For the moment, we'll sweep
37873cee92e0SBjoern A. Zeeb 		 * this under the carpet.
37883cee92e0SBjoern A. Zeeb 		 *
37893cee92e0SBjoern A. Zeeb 		 * The conservative default might not actually be a problem
37903cee92e0SBjoern A. Zeeb 		 * if the only case this occurs is when sending an initial
37913cee92e0SBjoern A. Zeeb 		 * SYN with options and data to a host we've never talked
37923cee92e0SBjoern A. Zeeb 		 * to before.  Then, they will reply with an MSS value which
37933cee92e0SBjoern A. Zeeb 		 * will get recorded and the new parameters should get
37943cee92e0SBjoern A. Zeeb 		 * recomputed.  For Further Study.
37953cee92e0SBjoern A. Zeeb 		 */
379697d8d152SAndre Oppermann 	}
3797a0292f23SGarrett Wollman 	mss = min(mss, offer);
379897d8d152SAndre Oppermann 
3799a0292f23SGarrett Wollman 	/*
38000c39d38dSGleb Smirnoff 	 * Sanity check: make sure that maxseg will be large
38013cee92e0SBjoern A. Zeeb 	 * enough to allow some data on segments even if the
38023cee92e0SBjoern A. Zeeb 	 * all the option space is used (40bytes).  Otherwise
38033cee92e0SBjoern A. Zeeb 	 * funny things may happen in tcp_output.
38040c39d38dSGleb Smirnoff 	 *
38050c39d38dSGleb Smirnoff 	 * XXXGL: shouldn't we reserve space for IP/IPv6 options?
38063cee92e0SBjoern A. Zeeb 	 */
38073cee92e0SBjoern A. Zeeb 	mss = max(mss, 64);
38083cee92e0SBjoern A. Zeeb 
380997d8d152SAndre Oppermann 	tp->t_maxseg = mss;
38103cee92e0SBjoern A. Zeeb }
38113cee92e0SBjoern A. Zeeb 
38123cee92e0SBjoern A. Zeeb void
38133cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer)
38143cee92e0SBjoern A. Zeeb {
3815dbc42409SLawrence Stewart 	int mss;
38163ac12506SJonathan T. Looney 	uint32_t bufsize;
38173cee92e0SBjoern A. Zeeb 	struct inpcb *inp;
38183cee92e0SBjoern A. Zeeb 	struct socket *so;
38193cee92e0SBjoern A. Zeeb 	struct hc_metrics_lite metrics;
38203c914c54SAndre Oppermann 	struct tcp_ifcap cap;
3821dbc42409SLawrence Stewart 
38223cee92e0SBjoern A. Zeeb 	KASSERT(tp != NULL, ("%s: tp == NULL", __func__));
38233cee92e0SBjoern A. Zeeb 
38243c914c54SAndre Oppermann 	bzero(&cap, sizeof(cap));
38253c914c54SAndre Oppermann 	tcp_mss_update(tp, offer, -1, &metrics, &cap);
38263cee92e0SBjoern A. Zeeb 
38273cee92e0SBjoern A. Zeeb 	mss = tp->t_maxseg;
38283cee92e0SBjoern A. Zeeb 	inp = tp->t_inpcb;
382997d8d152SAndre Oppermann 
3830df8bae1dSRodney W. Grimes 	/*
383197d8d152SAndre Oppermann 	 * If there's a pipesize, change the socket buffer to that size,
383297d8d152SAndre Oppermann 	 * don't change if sb_hiwat is different than default (then it
383397d8d152SAndre Oppermann 	 * has been changed on purpose with setsockopt).
383497d8d152SAndre Oppermann 	 * Make the socket buffers an integral number of mss units;
383597d8d152SAndre Oppermann 	 * if the mss is larger than the socket buffer, decrease the mss.
3836df8bae1dSRodney W. Grimes 	 */
3837c3b02504SBjoern A. Zeeb 	so = inp->inp_socket;
38383f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
3839e233e2acSAndre Oppermann 	if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe)
384097d8d152SAndre Oppermann 		bufsize = metrics.rmx_sendpipe;
384197d8d152SAndre Oppermann 	else
3842df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
3843df8bae1dSRodney W. Grimes 	if (bufsize < mss)
3844df8bae1dSRodney W. Grimes 		mss = bufsize;
3845df8bae1dSRodney W. Grimes 	else {
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_snd.sb_hiwat)
38503f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_snd, bufsize, so, NULL);
3851df8bae1dSRodney W. Grimes 	}
38523f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
3853784ce8faSHiren Panchasara 	/*
3854784ce8faSHiren Panchasara 	 * Sanity check: make sure that maxseg will be large
3855784ce8faSHiren Panchasara 	 * enough to allow some data on segments even if the
3856784ce8faSHiren Panchasara 	 * all the option space is used (40bytes).  Otherwise
3857784ce8faSHiren Panchasara 	 * funny things may happen in tcp_output.
3858784ce8faSHiren Panchasara 	 *
3859784ce8faSHiren Panchasara 	 * XXXGL: shouldn't we reserve space for IP/IPv6 options?
3860784ce8faSHiren Panchasara 	 */
3861784ce8faSHiren Panchasara 	tp->t_maxseg = max(mss, 64);
3862df8bae1dSRodney W. Grimes 
38633f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
3864e233e2acSAndre Oppermann 	if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe)
386597d8d152SAndre Oppermann 		bufsize = metrics.rmx_recvpipe;
386697d8d152SAndre Oppermann 	else
3867df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
3868df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
3869df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3870df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3871df8bae1dSRodney W. Grimes 			bufsize = sb_max;
387288c39af3SRuslan Ermilov 		if (bufsize > so->so_rcv.sb_hiwat)
38733f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL);
3874df8bae1dSRodney W. Grimes 	}
38753f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
387691d6cfa6SBjoern A. Zeeb 
387791d6cfa6SBjoern A. Zeeb 	/* Check the interface for TSO capabilities. */
38783c914c54SAndre Oppermann 	if (cap.ifcap & CSUM_TSO) {
387991d6cfa6SBjoern A. Zeeb 		tp->t_flags |= TF_TSO;
38803c914c54SAndre Oppermann 		tp->t_tsomax = cap.tsomax;
38819fd573c3SHans Petter Selasky 		tp->t_tsomaxsegcount = cap.tsomaxsegcount;
38829fd573c3SHans Petter Selasky 		tp->t_tsomaxsegsize = cap.tsomaxsegsize;
38833c914c54SAndre Oppermann 	}
3884a0292f23SGarrett Wollman }
3885a0292f23SGarrett Wollman 
3886a0292f23SGarrett Wollman /*
3887a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
3888a0292f23SGarrett Wollman  */
3889a0292f23SGarrett Wollman int
3890ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc)
3891a0292f23SGarrett Wollman {
389297d8d152SAndre Oppermann 	int mss = 0;
38933ac12506SJonathan T. Looney 	uint32_t thcmtu = 0;
38943ac12506SJonathan T. Looney 	uint32_t maxmtu = 0;
389597d8d152SAndre Oppermann 	size_t min_protoh;
3896a0292f23SGarrett Wollman 
389797d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer"));
3898a0292f23SGarrett Wollman 
389931b3783cSHajimu UMEMOTO #ifdef INET6
3900dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
3901603724d3SBjoern A. Zeeb 		mss = V_tcp_v6mssdflt;
3902233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu6(inc, NULL);
390397d8d152SAndre Oppermann 		min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
3904b287c6c7SBjoern A. Zeeb 	}
390531b3783cSHajimu UMEMOTO #endif
3906b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3907b287c6c7SBjoern A. Zeeb 	else
3908b287c6c7SBjoern A. Zeeb #endif
3909b287c6c7SBjoern A. Zeeb #ifdef INET
391097d8d152SAndre Oppermann 	{
3911603724d3SBjoern A. Zeeb 		mss = V_tcp_mssdflt;
3912233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu(inc, NULL);
391397d8d152SAndre Oppermann 		min_protoh = sizeof(struct tcpiphdr);
391497d8d152SAndre Oppermann 	}
3915b287c6c7SBjoern A. Zeeb #endif
39163a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET)
39173a9391deSBjoern A. Zeeb 	thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
39183a9391deSBjoern A. Zeeb #endif
39193a9391deSBjoern A. Zeeb 
392097d8d152SAndre Oppermann 	if (maxmtu && thcmtu)
392197d8d152SAndre Oppermann 		mss = min(maxmtu, thcmtu) - min_protoh;
392297d8d152SAndre Oppermann 	else if (maxmtu || thcmtu)
392397d8d152SAndre Oppermann 		mss = max(maxmtu, thcmtu) - min_protoh;
392497d8d152SAndre Oppermann 
392597d8d152SAndre Oppermann 	return (mss);
3926df8bae1dSRodney W. Grimes }
392746f58482SJonathan Lemon 
39280e1d7c25SRichard Scheffenegger void
39290e1d7c25SRichard Scheffenegger tcp_prr_partialack(struct tcpcb *tp, struct tcphdr *th)
39300e1d7c25SRichard Scheffenegger {
39310e1d7c25SRichard Scheffenegger 	long snd_cnt = 0, limit = 0, del_data = 0, pipe = 0;
39320e1d7c25SRichard Scheffenegger 	int maxseg = tcp_maxseg(tp);
39330e1d7c25SRichard Scheffenegger 
39340e1d7c25SRichard Scheffenegger 	INP_WLOCK_ASSERT(tp->t_inpcb);
39350e1d7c25SRichard Scheffenegger 
39360e1d7c25SRichard Scheffenegger 	tcp_timer_activate(tp, TT_REXMT, 0);
39370e1d7c25SRichard Scheffenegger 	tp->t_rtttime = 0;
39380e1d7c25SRichard Scheffenegger 	/*
39390e1d7c25SRichard Scheffenegger 	 * Compute the amount of data that this ACK is indicating
39400e1d7c25SRichard Scheffenegger 	 * (del_data) and an estimate of how many bytes are in the
39410e1d7c25SRichard Scheffenegger 	 * network.
39420e1d7c25SRichard Scheffenegger 	 */
39430e1d7c25SRichard Scheffenegger 	if (SEQ_GEQ(th->th_ack, tp->snd_una))
39440e1d7c25SRichard Scheffenegger 		del_data = BYTES_THIS_ACK(tp, th);
39450e1d7c25SRichard Scheffenegger 	del_data += tp->sackhint.delivered_data;
39460e1d7c25SRichard Scheffenegger 	pipe = (tp->snd_nxt - tp->snd_fack) + tp->sackhint.sack_bytes_rexmit;
39470e1d7c25SRichard Scheffenegger 	tp->sackhint.prr_delivered += del_data;
39480e1d7c25SRichard Scheffenegger 	/*
39490e1d7c25SRichard Scheffenegger 	 * Proportional Rate Reduction
39500e1d7c25SRichard Scheffenegger 	 */
39510e1d7c25SRichard Scheffenegger 	if (pipe > tp->snd_ssthresh)
3952*84761f3dSRichard Scheffenegger 		snd_cnt = (tp->sackhint.prr_delivered * tp->snd_ssthresh /
3953*84761f3dSRichard Scheffenegger 		    tp->sackhint.recover_fs) - tp->sackhint.sack_bytes_rexmit;
39540e1d7c25SRichard Scheffenegger 	else {
39550e1d7c25SRichard Scheffenegger 		if (V_tcp_do_prr_conservative)
3956*84761f3dSRichard Scheffenegger 			limit = tp->sackhint.prr_delivered -
3957*84761f3dSRichard Scheffenegger 			    tp->sackhint.sack_bytes_rexmit;
39580e1d7c25SRichard Scheffenegger 		else
3959*84761f3dSRichard Scheffenegger 			if ((tp->sackhint.prr_delivered -
3960*84761f3dSRichard Scheffenegger 			    tp->sackhint.sack_bytes_rexmit) > del_data)
3961*84761f3dSRichard Scheffenegger 				limit = tp->sackhint.prr_delivered -
3962*84761f3dSRichard Scheffenegger 				    tp->sackhint.sack_bytes_rexmit + maxseg;
39630e1d7c25SRichard Scheffenegger 			else
39640e1d7c25SRichard Scheffenegger 				limit = del_data + maxseg;
39650e1d7c25SRichard Scheffenegger 		snd_cnt = min((tp->snd_ssthresh - pipe), limit);
39660e1d7c25SRichard Scheffenegger 	}
39670e1d7c25SRichard Scheffenegger 	snd_cnt = max((snd_cnt / maxseg), 0);
39680e1d7c25SRichard Scheffenegger 	/*
39690e1d7c25SRichard Scheffenegger 	 * Send snd_cnt new data into the network in response to this ack.
39700e1d7c25SRichard Scheffenegger 	 * If there is going to be a SACK retransmission, adjust snd_cwnd
39710e1d7c25SRichard Scheffenegger 	 * accordingly.
39720e1d7c25SRichard Scheffenegger 	 */
39730e1d7c25SRichard Scheffenegger 	tp->snd_cwnd = tp->snd_nxt - tp->snd_recover +
39740e1d7c25SRichard Scheffenegger 		tp->sackhint.sack_bytes_rexmit + (snd_cnt * maxseg);
39750e1d7c25SRichard Scheffenegger 	tp->t_flags |= TF_ACKNOW;
39760e1d7c25SRichard Scheffenegger 	(void) tcp_output(tp);
39770e1d7c25SRichard Scheffenegger }
39780e1d7c25SRichard Scheffenegger 
397946f58482SJonathan Lemon /*
3980c068736aSJeffrey Hsu  * On a partial ack arrives, force the retransmission of the
3981c068736aSJeffrey Hsu  * next unacknowledged segment.  Do not clear tp->t_dupacks.
3982c068736aSJeffrey Hsu  * By setting snd_nxt to ti_ack, this forces retransmission timer to
3983c068736aSJeffrey Hsu  * be started again.
398446f58482SJonathan Lemon  */
398555bceb1eSRandall Stewart void
3986ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th)
398746f58482SJonathan Lemon {
398846f58482SJonathan Lemon 	tcp_seq onxt = tp->snd_nxt;
39893ac12506SJonathan T. Looney 	uint32_t ocwnd = tp->snd_cwnd;
39900c39d38dSGleb Smirnoff 	u_int maxseg = tcp_maxseg(tp);
399146f58482SJonathan Lemon 
39928501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
39937a3244ccSRobert Watson 
3994b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_REXMT, 0);
399546f58482SJonathan Lemon 	tp->t_rtttime = 0;
399646f58482SJonathan Lemon 	tp->snd_nxt = th->th_ack;
39976b2a5f92SJayanth Vijayaraghavan 	/*
3998c068736aSJeffrey Hsu 	 * Set snd_cwnd to one segment beyond acknowledged offset.
3999c068736aSJeffrey Hsu 	 * (tp->snd_una has not yet been updated when this function is called.)
40006b2a5f92SJayanth Vijayaraghavan 	 */
40010c39d38dSGleb Smirnoff 	tp->snd_cwnd = maxseg + BYTES_THIS_ACK(tp, th);
4002a84db8f4SMatthew Dillon 	tp->t_flags |= TF_ACKNOW;
400355bceb1eSRandall Stewart 	(void) tp->t_fb->tfb_tcp_output(tp);
400446f58482SJonathan Lemon 	tp->snd_cwnd = ocwnd;
400546f58482SJonathan Lemon 	if (SEQ_GT(onxt, tp->snd_nxt))
400646f58482SJonathan Lemon 		tp->snd_nxt = onxt;
400746f58482SJonathan Lemon 	/*
400846f58482SJonathan Lemon 	 * Partial window deflation.  Relies on fact that tp->snd_una
400946f58482SJonathan Lemon 	 * not updated yet.
401046f58482SJonathan Lemon 	 */
4011dbc42409SLawrence Stewart 	if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th))
4012dbc42409SLawrence Stewart 		tp->snd_cwnd -= BYTES_THIS_ACK(tp, th);
4013d7587117SPaul Saab 	else
4014d7587117SPaul Saab 		tp->snd_cwnd = 0;
40150c39d38dSGleb Smirnoff 	tp->snd_cwnd += maxseg;
401646f58482SJonathan Lemon }
401712eeb81fSHiren Panchasara 
401812eeb81fSHiren Panchasara int
401912eeb81fSHiren Panchasara tcp_compute_pipe(struct tcpcb *tp)
402012eeb81fSHiren Panchasara {
402112eeb81fSHiren Panchasara 	return (tp->snd_max - tp->snd_una +
402212eeb81fSHiren Panchasara 		tp->sackhint.sack_bytes_rexmit -
402312eeb81fSHiren Panchasara 		tp->sackhint.sacked_bytes);
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