xref: /freebsd/sys/netinet/tcp_input.c (revision a8fe77d877af46bb07e639b2d790aa10fe8c6835)
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>
6198163b98SPoul-Henning Kamp #include <sys/kernel.h>
62bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
6339bc9de5SLawrence Stewart #include <sys/hhook.h>
64bd79708dSJonathan T. Looney #endif
65df8bae1dSRodney W. Grimes #include <sys/malloc.h>
66df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
67a29f300eSGarrett Wollman #include <sys/proc.h>		/* for proc0 declaration */
68df8bae1dSRodney W. Grimes #include <sys/protosw.h>
6957f60867SMark Johnston #include <sys/sdt.h>
70960ed29cSSeigo Tanimura #include <sys/signalvar.h>
71df8bae1dSRodney W. Grimes #include <sys/socket.h>
72df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
73960ed29cSSeigo Tanimura #include <sys/sysctl.h>
74816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
75960ed29cSSeigo Tanimura #include <sys/systm.h>
76df8bae1dSRodney W. Grimes 
77e79adb8eSGarrett Wollman #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
78e79adb8eSGarrett Wollman 
7912e2e970SAndre Oppermann #include <vm/uma.h>
8012e2e970SAndre Oppermann 
81df8bae1dSRodney W. Grimes #include <net/if.h>
8276039bc8SGleb Smirnoff #include <net/if_var.h>
83df8bae1dSRodney W. Grimes #include <net/route.h>
84530c0060SRobert Watson #include <net/vnet.h>
85df8bae1dSRodney W. Grimes 
86773673c1SAndre Oppermann #define TCPSTATES		/* for logging */
87773673c1SAndre Oppermann 
88df8bae1dSRodney W. Grimes #include <netinet/in.h>
8957f60867SMark Johnston #include <netinet/in_kdtrace.h>
90960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
91df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
92df8bae1dSRodney W. Grimes #include <netinet/ip.h>
938e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h>	/* required for icmp_var.h */
94686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM */
95df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
96ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
97686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
98686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
99686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
100b66d74c1SGleb Smirnoff #include <netinet6/in6_var.h>
101960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h>
102960ed29cSSeigo Tanimura #include <netinet6/nd6.h>
1032de3e790SGleb Smirnoff #include <netinet/tcp.h>
104df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
1052529f56eSJonathan T. Looney #include <netinet/tcp_log_buf.h>
106df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
107df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
108df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
109fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h>
110df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
1114644fda3SGleb Smirnoff #include <netinet/cc/cc.h>
112c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h>
11386a996e6SHiren Panchasara #ifdef TCPPCAP
11486a996e6SHiren Panchasara #include <netinet/tcp_pcap.h>
11586a996e6SHiren Panchasara #endif
116c325962bSMike Silbersack #include <netinet/tcp_syncache.h>
117610ee2f9SDavid Greenman #ifdef TCPDEBUG
118df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
119fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */
12009fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
12109fe6320SNavdeep Parhar #include <netinet/tcp_offload.h>
12209fe6320SNavdeep Parhar #endif
123fb59c426SYoshinobu Inoue 
124fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
125b9234fafSSam Leffler 
126db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
127db4f9cc7SJonathan Lemon 
128aed55708SRobert Watson #include <security/mac/mac_framework.h>
129aed55708SRobert Watson 
130dbc42409SLawrence Stewart const int tcprexmtthresh = 3;
1310312fbe9SPoul-Henning Kamp 
132773673c1SAndre Oppermann int tcp_log_in_vain = 0;
133816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW,
134eddfbb76SRobert Watson     &tcp_log_in_vain, 0,
135eddfbb76SRobert Watson     "Log all incoming TCP segments to closed ports");
136816a3d83SPoul-Henning Kamp 
13782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, blackhole) = 0;
13882cea7e6SBjoern A. Zeeb #define	V_blackhole		VNET(blackhole)
1396df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW,
140eddfbb76SRobert Watson     &VNET_NAME(blackhole), 0,
141eddfbb76SRobert Watson     "Do not send RST on segments to closed ports");
14216f7f31fSGeoff Rehmet 
14382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_delack_enabled) = 1;
1446df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_VNET | CTLFLAG_RW,
145eddfbb76SRobert Watson     &VNET_NAME(tcp_delack_enabled), 0,
1463d177f46SBill Fumerola     "Delay ACK to try and piggyback it onto a data packet");
147f498eeeeSDavid Greenman 
14882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, drop_synfin) = 0;
1496df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_VNET | CTLFLAG_RW,
150eddfbb76SRobert Watson     &VNET_NAME(drop_synfin), 0,
151eddfbb76SRobert Watson     "Drop TCP packets with SYN+FIN set");
152f8613305SDag-Erling Smørgrav 
15312eeb81fSHiren Panchasara VNET_DEFINE(int, tcp_do_rfc6675_pipe) = 0;
154054d38e3SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc6675_pipe, CTLFLAG_VNET | CTLFLAG_RW,
15512eeb81fSHiren Panchasara     &VNET_NAME(tcp_do_rfc6675_pipe), 0,
15612eeb81fSHiren Panchasara     "Use calculated pipe/in-flight bytes per RFC 6675");
15712eeb81fSHiren Panchasara 
15882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3042) = 1;
1596df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_VNET | CTLFLAG_RW,
160eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3042), 0,
161eddfbb76SRobert Watson     "Enable RFC 3042 (Limited Transmit)");
162582a954bSJeffrey Hsu 
16382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3390) = 1;
1646df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_VNET | CTLFLAG_RW,
165eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3390), 0,
166da3a8a1aSJeffrey Hsu     "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)");
167da3a8a1aSJeffrey Hsu 
168356c7958SHiren Panchasara VNET_DEFINE(int, tcp_initcwnd_segments) = 10;
169356c7958SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, initcwnd_segments,
170356c7958SHiren Panchasara     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_initcwnd_segments), 0,
171356c7958SHiren Panchasara     "Slow-start flight size (initial congestion window) in number of segments");
17209440655SAndre Oppermann 
17382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3465) = 1;
1746df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_VNET | CTLFLAG_RW,
175eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3465), 0,
17624cb0f22SLawrence Stewart     "Enable RFC 3465 (Appropriate Byte Counting)");
177eddfbb76SRobert Watson 
17882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_abc_l_var) = 2;
1796df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_VNET | CTLFLAG_RW,
180eddfbb76SRobert Watson     &VNET_NAME(tcp_abc_l_var), 2,
18124cb0f22SLawrence Stewart     "Cap the max cwnd increment during slow-start to this number of segments");
18224cb0f22SLawrence Stewart 
1836472ac3dSEd Schouten static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, CTLFLAG_RW, 0, "TCP ECN");
184f2512ba1SRui Paulo 
185883054b4SDon Lewis VNET_DEFINE(int, tcp_do_ecn) = 2;
1866df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW,
187eddfbb76SRobert Watson     &VNET_NAME(tcp_do_ecn), 0,
188eddfbb76SRobert Watson     "TCP ECN support");
189eddfbb76SRobert Watson 
19082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_ecn_maxretries) = 1;
1916df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, maxretries, CTLFLAG_VNET | CTLFLAG_RW,
192eddfbb76SRobert Watson     &VNET_NAME(tcp_ecn_maxretries), 0,
193eddfbb76SRobert Watson     "Max retries before giving up on ECN");
194eddfbb76SRobert Watson 
1953220a212SGleb Smirnoff VNET_DEFINE(int, tcp_insecure_syn) = 0;
1966df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_syn, CTLFLAG_VNET | CTLFLAG_RW,
1973220a212SGleb Smirnoff     &VNET_NAME(tcp_insecure_syn), 0,
1983220a212SGleb Smirnoff     "Follow RFC793 instead of RFC5961 criteria for accepting SYN packets");
1993220a212SGleb Smirnoff 
20082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_insecure_rst) = 0;
2016df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_VNET | CTLFLAG_RW,
202eddfbb76SRobert Watson     &VNET_NAME(tcp_insecure_rst), 0,
2033220a212SGleb Smirnoff     "Follow RFC793 instead of RFC5961 criteria for accepting RST packets");
204a69968eeSMike Silbersack 
205fba0cea1SBjoern A. Zeeb VNET_DEFINE(int, tcp_recvspace) = 1024*64;
206873789cbSAndre Oppermann #define	V_tcp_recvspace	VNET(tcp_recvspace)
2076df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_VNET | CTLFLAG_RW,
208873789cbSAndre Oppermann     &VNET_NAME(tcp_recvspace), 0, "Initial receive socket buffer size");
209873789cbSAndre Oppermann 
21082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autorcvbuf) = 1;
2116df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_VNET | CTLFLAG_RW,
212eddfbb76SRobert Watson     &VNET_NAME(tcp_do_autorcvbuf), 0,
2138b615593SMarko Zec     "Enable automatic receive buffer sizing");
2146741ecf5SAndre Oppermann 
215b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024;
2166df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_VNET | CTLFLAG_RW,
217eddfbb76SRobert Watson     &VNET_NAME(tcp_autorcvbuf_max), 0,
2188b615593SMarko Zec     "Max size of automatic receive buffer");
2196741ecf5SAndre Oppermann 
220eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, tcb);
22144e33a07SMarko Zec #define	tcb6	tcb  /* for KAME src sync over BSD*'s */
22282cea7e6SBjoern A. Zeeb VNET_DEFINE(struct inpcbinfo, tcbinfo);
223df8bae1dSRodney W. Grimes 
224315e3e38SRobert Watson /*
225bf840a17SGleb Smirnoff  * TCP statistics are stored in an array of counter(9)s, which size matches
226bf840a17SGleb Smirnoff  * size of struct tcpstat.  TCP running connection count is a regular array.
2275923c293SGleb Smirnoff  */
2285da0521fSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct tcpstat, tcpstat);
2295da0521fSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_tcp, TCPCTL_STATS, stats, struct tcpstat,
2305da0521fSAndrey V. Elsukov     tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
231bf840a17SGleb Smirnoff VNET_DEFINE(counter_u64_t, tcps_states[TCP_NSTATES]);
232bf840a17SGleb Smirnoff SYSCTL_COUNTER_U64_ARRAY(_net_inet_tcp, TCPCTL_STATES, states, CTLFLAG_RD |
233d4d32b9fSHans Petter Selasky     CTLFLAG_VNET, &VNET_NAME(tcps_states)[0], TCP_NSTATES,
234bf840a17SGleb Smirnoff     "TCP connection counts by TCP state");
235bf840a17SGleb Smirnoff 
236bf840a17SGleb Smirnoff static void
237bf840a17SGleb Smirnoff tcp_vnet_init(const void *unused)
238bf840a17SGleb Smirnoff {
239bf840a17SGleb Smirnoff 
240f59d975eSGleb Smirnoff 	COUNTER_ARRAY_ALLOC(V_tcps_states, TCP_NSTATES, M_WAITOK);
241bf840a17SGleb Smirnoff 	VNET_PCPUSTAT_ALLOC(tcpstat, M_WAITOK);
242bf840a17SGleb Smirnoff }
243bf840a17SGleb Smirnoff VNET_SYSINIT(tcp_vnet_init, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
244bf840a17SGleb Smirnoff     tcp_vnet_init, NULL);
2455923c293SGleb Smirnoff 
2465923c293SGleb Smirnoff #ifdef VIMAGE
247bf840a17SGleb Smirnoff static void
248bf840a17SGleb Smirnoff tcp_vnet_uninit(const void *unused)
249bf840a17SGleb Smirnoff {
250bf840a17SGleb Smirnoff 
251f59d975eSGleb Smirnoff 	COUNTER_ARRAY_FREE(V_tcps_states, TCP_NSTATES);
252bf840a17SGleb Smirnoff 	VNET_PCPUSTAT_FREE(tcpstat);
253bf840a17SGleb Smirnoff }
254bf840a17SGleb Smirnoff VNET_SYSUNINIT(tcp_vnet_uninit, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY,
255bf840a17SGleb Smirnoff     tcp_vnet_uninit, NULL);
2565923c293SGleb Smirnoff #endif /* VIMAGE */
257bf840a17SGleb Smirnoff 
2585923c293SGleb Smirnoff /*
259315e3e38SRobert Watson  * Kernel module interface for updating tcpstat.  The argument is an index
2605923c293SGleb Smirnoff  * into tcpstat treated as an array.
261315e3e38SRobert Watson  */
262315e3e38SRobert Watson void
263315e3e38SRobert Watson kmod_tcpstat_inc(int statnum)
264315e3e38SRobert Watson {
265315e3e38SRobert Watson 
2665da0521fSAndrey V. Elsukov 	counter_u64_add(VNET(tcpstat)[statnum], 1);
267315e3e38SRobert Watson }
268315e3e38SRobert Watson 
269bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
270dbc42409SLawrence Stewart /*
27139bc9de5SLawrence Stewart  * Wrapper for the TCP established input helper hook.
27239bc9de5SLawrence Stewart  */
27355bceb1eSRandall Stewart void
27439bc9de5SLawrence Stewart hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to)
27539bc9de5SLawrence Stewart {
27639bc9de5SLawrence Stewart 	struct tcp_hhook_data hhook_data;
27739bc9de5SLawrence Stewart 
27839bc9de5SLawrence Stewart 	if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) {
27939bc9de5SLawrence Stewart 		hhook_data.tp = tp;
28039bc9de5SLawrence Stewart 		hhook_data.th = th;
28139bc9de5SLawrence Stewart 		hhook_data.to = to;
28239bc9de5SLawrence Stewart 
28339bc9de5SLawrence Stewart 		hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data,
28439bc9de5SLawrence Stewart 		    tp->osd);
28539bc9de5SLawrence Stewart 	}
28639bc9de5SLawrence Stewart }
287bd79708dSJonathan T. Looney #endif
28839bc9de5SLawrence Stewart 
28939bc9de5SLawrence Stewart /*
290dbc42409SLawrence Stewart  * CC wrapper hook functions
291dbc42409SLawrence Stewart  */
29255bceb1eSRandall Stewart void
2934b7b743cSLawrence Stewart cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t nsegs,
2944b7b743cSLawrence Stewart     uint16_t type)
295f2512ba1SRui Paulo {
296dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
297f2512ba1SRui Paulo 
2984b7b743cSLawrence Stewart 	tp->ccv->nsegs = nsegs;
299dbc42409SLawrence Stewart 	tp->ccv->bytes_this_ack = BYTES_THIS_ACK(tp, th);
3005b648e79SLawrence Stewart 	if (tp->snd_cwnd <= tp->snd_wnd)
301dbc42409SLawrence Stewart 		tp->ccv->flags |= CCF_CWND_LIMITED;
302dbc42409SLawrence Stewart 	else
303dbc42409SLawrence Stewart 		tp->ccv->flags &= ~CCF_CWND_LIMITED;
304dbc42409SLawrence Stewart 
305dbc42409SLawrence Stewart 	if (type == CC_ACK) {
306dbc42409SLawrence Stewart 		if (tp->snd_cwnd > tp->snd_ssthresh) {
307dbc42409SLawrence Stewart 			tp->t_bytes_acked += min(tp->ccv->bytes_this_ack,
3084b7b743cSLawrence Stewart 			     nsegs * V_tcp_abc_l_var * tcp_maxseg(tp));
309dbc42409SLawrence Stewart 			if (tp->t_bytes_acked >= tp->snd_cwnd) {
310dbc42409SLawrence Stewart 				tp->t_bytes_acked -= tp->snd_cwnd;
311dbc42409SLawrence Stewart 				tp->ccv->flags |= CCF_ABC_SENTAWND;
312dbc42409SLawrence Stewart 			}
313dbc42409SLawrence Stewart 		} else {
314dbc42409SLawrence Stewart 				tp->ccv->flags &= ~CCF_ABC_SENTAWND;
315dbc42409SLawrence Stewart 				tp->t_bytes_acked = 0;
316dbc42409SLawrence Stewart 		}
317dbc42409SLawrence Stewart 	}
318dbc42409SLawrence Stewart 
319dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->ack_received != NULL) {
320dbc42409SLawrence Stewart 		/* XXXLAS: Find a way to live without this */
321dbc42409SLawrence Stewart 		tp->ccv->curack = th->th_ack;
322dbc42409SLawrence Stewart 		CC_ALGO(tp)->ack_received(tp->ccv, type);
323dbc42409SLawrence Stewart 	}
324dbc42409SLawrence Stewart }
325dbc42409SLawrence Stewart 
32655bceb1eSRandall Stewart void
327dbc42409SLawrence Stewart cc_conn_init(struct tcpcb *tp)
328dbc42409SLawrence Stewart {
329dbc42409SLawrence Stewart 	struct hc_metrics_lite metrics;
330dbc42409SLawrence Stewart 	struct inpcb *inp = tp->t_inpcb;
3310c39d38dSGleb Smirnoff 	u_int maxseg;
332dbc42409SLawrence Stewart 	int rtt;
333dbc42409SLawrence Stewart 
334dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
335dbc42409SLawrence Stewart 
336dbc42409SLawrence Stewart 	tcp_hc_get(&inp->inp_inc, &metrics);
3370c39d38dSGleb Smirnoff 	maxseg = tcp_maxseg(tp);
338dbc42409SLawrence Stewart 
339dbc42409SLawrence Stewart 	if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) {
340dbc42409SLawrence Stewart 		tp->t_srtt = rtt;
341dbc42409SLawrence Stewart 		tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE;
342dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_usedrtt);
343dbc42409SLawrence Stewart 		if (metrics.rmx_rttvar) {
344dbc42409SLawrence Stewart 			tp->t_rttvar = metrics.rmx_rttvar;
345dbc42409SLawrence Stewart 			TCPSTAT_INC(tcps_usedrttvar);
346dbc42409SLawrence Stewart 		} else {
347dbc42409SLawrence Stewart 			/* default variation is +- 1 rtt */
348dbc42409SLawrence Stewart 			tp->t_rttvar =
349dbc42409SLawrence Stewart 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
350dbc42409SLawrence Stewart 		}
351dbc42409SLawrence Stewart 		TCPT_RANGESET(tp->t_rxtcur,
352dbc42409SLawrence Stewart 		    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
353dbc42409SLawrence Stewart 		    tp->t_rttmin, TCPTV_REXMTMAX);
354dbc42409SLawrence Stewart 	}
355dbc42409SLawrence Stewart 	if (metrics.rmx_ssthresh) {
356dbc42409SLawrence Stewart 		/*
357dbc42409SLawrence Stewart 		 * There's some sort of gateway or interface
358dbc42409SLawrence Stewart 		 * buffer limit on the path.  Use this to set
359a4641f4eSPedro F. Giffuni 		 * the slow start threshold, but set the
360dbc42409SLawrence Stewart 		 * threshold to no less than 2*mss.
361dbc42409SLawrence Stewart 		 */
3620c39d38dSGleb Smirnoff 		tp->snd_ssthresh = max(2 * maxseg, metrics.rmx_ssthresh);
363dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_usedssthresh);
364dbc42409SLawrence Stewart 	}
365dbc42409SLawrence Stewart 
366dbc42409SLawrence Stewart 	/*
3679ec4a4ccSAndre Oppermann 	 * Set the initial slow-start flight size.
368dbc42409SLawrence Stewart 	 *
369cf8f04f4SAndre Oppermann 	 * If a SYN or SYN/ACK was lost and retransmitted, we have to
370cf8f04f4SAndre Oppermann 	 * reduce the initial CWND to one segment as congestion is likely
371cf8f04f4SAndre Oppermann 	 * requiring us to be cautious.
372dbc42409SLawrence Stewart 	 */
373cf8f04f4SAndre Oppermann 	if (tp->snd_cwnd == 1)
3740c39d38dSGleb Smirnoff 		tp->snd_cwnd = maxseg;		/* SYN(-ACK) lost */
375dbc42409SLawrence Stewart 	else
3767dc90a1dSMichael Tuexen 		tp->snd_cwnd = tcp_compute_initwnd(maxseg);
377dbc42409SLawrence Stewart 
378dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->conn_init != NULL)
379dbc42409SLawrence Stewart 		CC_ALGO(tp)->conn_init(tp->ccv);
380dbc42409SLawrence Stewart }
381dbc42409SLawrence Stewart 
382dbc42409SLawrence Stewart void inline
383dbc42409SLawrence Stewart cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type)
384dbc42409SLawrence Stewart {
3850c39d38dSGleb Smirnoff 	u_int maxseg;
3860c39d38dSGleb Smirnoff 
387dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
388dbc42409SLawrence Stewart 
389dbc42409SLawrence Stewart 	switch(type) {
390dbc42409SLawrence Stewart 	case CC_NDUPACK:
391dbc42409SLawrence Stewart 		if (!IN_FASTRECOVERY(tp->t_flags)) {
392f2512ba1SRui Paulo 			tp->snd_recover = tp->snd_max;
393f2512ba1SRui Paulo 			if (tp->t_flags & TF_ECN_PERMIT)
394f2512ba1SRui Paulo 				tp->t_flags |= TF_ECN_SND_CWR;
395f2512ba1SRui Paulo 		}
396dbc42409SLawrence Stewart 		break;
397dbc42409SLawrence Stewart 	case CC_ECN:
398dbc42409SLawrence Stewart 		if (!IN_CONGRECOVERY(tp->t_flags)) {
399dbc42409SLawrence Stewart 			TCPSTAT_INC(tcps_ecn_rcwnd);
400dbc42409SLawrence Stewart 			tp->snd_recover = tp->snd_max;
401dbc42409SLawrence Stewart 			if (tp->t_flags & TF_ECN_PERMIT)
402dbc42409SLawrence Stewart 				tp->t_flags |= TF_ECN_SND_CWR;
403dbc42409SLawrence Stewart 		}
404dbc42409SLawrence Stewart 		break;
405dbc42409SLawrence Stewart 	case CC_RTO:
4060c39d38dSGleb Smirnoff 		maxseg = tcp_maxseg(tp);
407dbc42409SLawrence Stewart 		tp->t_dupacks = 0;
408dbc42409SLawrence Stewart 		tp->t_bytes_acked = 0;
409dbc42409SLawrence Stewart 		EXIT_RECOVERY(tp->t_flags);
41043053c12SSean Bruno 		tp->snd_ssthresh = max(2, min(tp->snd_wnd, tp->snd_cwnd) / 2 /
41143053c12SSean Bruno 		    maxseg) * maxseg;
4120c39d38dSGleb Smirnoff 		tp->snd_cwnd = maxseg;
413dbc42409SLawrence Stewart 		break;
414dbc42409SLawrence Stewart 	case CC_RTO_ERR:
415dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_sndrexmitbad);
416dbc42409SLawrence Stewart 		/* RTO was unnecessary, so reset everything. */
417dbc42409SLawrence Stewart 		tp->snd_cwnd = tp->snd_cwnd_prev;
418dbc42409SLawrence Stewart 		tp->snd_ssthresh = tp->snd_ssthresh_prev;
419dbc42409SLawrence Stewart 		tp->snd_recover = tp->snd_recover_prev;
420dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASFRECOVERY)
421dbc42409SLawrence Stewart 			ENTER_FASTRECOVERY(tp->t_flags);
422dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASCRECOVERY)
423dbc42409SLawrence Stewart 			ENTER_CONGRECOVERY(tp->t_flags);
424dbc42409SLawrence Stewart 		tp->snd_nxt = tp->snd_max;
425672dc4aeSJohn Baldwin 		tp->t_flags &= ~TF_PREVVALID;
426dbc42409SLawrence Stewart 		tp->t_badrxtwin = 0;
427dbc42409SLawrence Stewart 		break;
428dbc42409SLawrence Stewart 	}
429dbc42409SLawrence Stewart 
430dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->cong_signal != NULL) {
431dbc42409SLawrence Stewart 		if (th != NULL)
432dbc42409SLawrence Stewart 			tp->ccv->curack = th->th_ack;
433dbc42409SLawrence Stewart 		CC_ALGO(tp)->cong_signal(tp->ccv, type);
434dbc42409SLawrence Stewart 	}
435dbc42409SLawrence Stewart }
436dbc42409SLawrence Stewart 
43755bceb1eSRandall Stewart void inline
438dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th)
439dbc42409SLawrence Stewart {
440dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
441dbc42409SLawrence Stewart 
442dbc42409SLawrence Stewart 	/* XXXLAS: KASSERT that we're in recovery? */
443dbc42409SLawrence Stewart 
444dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->post_recovery != NULL) {
445dbc42409SLawrence Stewart 		tp->ccv->curack = th->th_ack;
446dbc42409SLawrence Stewart 		CC_ALGO(tp)->post_recovery(tp->ccv);
447dbc42409SLawrence Stewart 	}
448dbc42409SLawrence Stewart 	/* XXXLAS: EXIT_RECOVERY ? */
449dbc42409SLawrence Stewart 	tp->t_bytes_acked = 0;
450dbc42409SLawrence Stewart }
4510312fbe9SPoul-Henning Kamp 
452df8bae1dSRodney W. Grimes /*
453262c1c1aSMatthew Dillon  * Indicate whether this ack should be delayed.  We can delay the ack if
45485536381SHiren Panchasara  * following conditions are met:
45585536381SHiren Panchasara  *	- There is no delayed ack timer in progress.
45685536381SHiren Panchasara  *	- Our last ack wasn't a 0-sized window. We never want to delay
45785536381SHiren Panchasara  *	  the ack that opens up a 0-sized window.
45885536381SHiren Panchasara  *	- LRO wasn't used for this segment. We make sure by checking that the
45985536381SHiren Panchasara  *	  segment size is not larger than the MSS.
460d8c85a26SJonathan Lemon  */
461c1e5a6e5SAndre Oppermann #define DELAY_ACK(tp, tlen)						\
462b8152ba7SAndre Oppermann 	((!tcp_timer_active(tp, TT_DELACK) &&				\
463a14c749fSJonathan Lemon 	    (tp->t_flags & TF_RXWIN0SENT) == 0) &&			\
4640c39d38dSGleb Smirnoff 	    (tlen <= tp->t_maxseg) &&					\
465603724d3SBjoern A. Zeeb 	    (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN)))
466d8c85a26SJonathan Lemon 
46764807b30SHiren Panchasara static void inline
46864807b30SHiren Panchasara cc_ecnpkt_handler(struct tcpcb *tp, struct tcphdr *th, uint8_t iptos)
46964807b30SHiren Panchasara {
47064807b30SHiren Panchasara 	INP_WLOCK_ASSERT(tp->t_inpcb);
47164807b30SHiren Panchasara 
47264807b30SHiren Panchasara 	if (CC_ALGO(tp)->ecnpkt_handler != NULL) {
47364807b30SHiren Panchasara 		switch (iptos & IPTOS_ECN_MASK) {
47464807b30SHiren Panchasara 		case IPTOS_ECN_CE:
47564807b30SHiren Panchasara 		    tp->ccv->flags |= CCF_IPHDR_CE;
47664807b30SHiren Panchasara 		    break;
47764807b30SHiren Panchasara 		case IPTOS_ECN_ECT0:
47864807b30SHiren Panchasara 		    tp->ccv->flags &= ~CCF_IPHDR_CE;
47964807b30SHiren Panchasara 		    break;
48064807b30SHiren Panchasara 		case IPTOS_ECN_ECT1:
48164807b30SHiren Panchasara 		    tp->ccv->flags &= ~CCF_IPHDR_CE;
48264807b30SHiren Panchasara 		    break;
48364807b30SHiren Panchasara 		}
48464807b30SHiren Panchasara 
48564807b30SHiren Panchasara 		if (th->th_flags & TH_CWR)
48664807b30SHiren Panchasara 			tp->ccv->flags |= CCF_TCPHDR_CWR;
48764807b30SHiren Panchasara 		else
48864807b30SHiren Panchasara 			tp->ccv->flags &= ~CCF_TCPHDR_CWR;
48964807b30SHiren Panchasara 
49064807b30SHiren Panchasara 		if (tp->t_flags & TF_DELACK)
49164807b30SHiren Panchasara 			tp->ccv->flags |= CCF_DELACK;
49264807b30SHiren Panchasara 		else
49364807b30SHiren Panchasara 			tp->ccv->flags &= ~CCF_DELACK;
49464807b30SHiren Panchasara 
49564807b30SHiren Panchasara 		CC_ALGO(tp)->ecnpkt_handler(tp->ccv);
49664807b30SHiren Panchasara 
49764807b30SHiren Panchasara 		if (tp->ccv->flags & CCF_ACKNOW)
49864807b30SHiren Panchasara 			tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
49964807b30SHiren Panchasara 	}
50064807b30SHiren Panchasara }
50164807b30SHiren Panchasara 
502df8bae1dSRodney W. Grimes /*
5038d573cc1SAndre Oppermann  * TCP input handling is split into multiple parts:
5048d573cc1SAndre Oppermann  *   tcp6_input is a thin wrapper around tcp_input for the extended
5058d573cc1SAndre Oppermann  *	ip6_protox[] call format in ip6_input
5068d573cc1SAndre Oppermann  *   tcp_input handles primary segment validation, inpcb lookup and
5078d573cc1SAndre Oppermann  *	SYN processing on listen sockets
5088d573cc1SAndre Oppermann  *   tcp_do_segment processes the ACK and text of the segment for
5098d573cc1SAndre Oppermann  *	establishing, established and closing connections
510df8bae1dSRodney W. Grimes  */
511fb59c426SYoshinobu Inoue #ifdef INET6
512fb59c426SYoshinobu Inoue int
513ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto)
514fb59c426SYoshinobu Inoue {
515503f4e47SBjoern A. Zeeb 	struct mbuf *m;
51633841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6;
5173e88eb90SAndrey V. Elsukov 	struct ip6_hdr *ip6;
518fb59c426SYoshinobu Inoue 
519503f4e47SBjoern A. Zeeb 	m = *mp;
520fb59c426SYoshinobu Inoue 	IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE);
521fb59c426SYoshinobu Inoue 
522fb59c426SYoshinobu Inoue 	/*
523fb59c426SYoshinobu Inoue 	 * draft-itojun-ipv6-tcp-to-anycast
524fb59c426SYoshinobu Inoue 	 * better place to put this in?
525fb59c426SYoshinobu Inoue 	 */
5263e88eb90SAndrey V. Elsukov 	ip6 = mtod(m, struct ip6_hdr *);
5273e88eb90SAndrey V. Elsukov 	ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
52833841545SHajimu UMEMOTO 	if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
529fb59c426SYoshinobu Inoue 
5308c0fec80SRobert Watson 		ifa_free(&ia6->ia_ifa);
531fb59c426SYoshinobu Inoue 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
532fb59c426SYoshinobu Inoue 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
533*a8fe77d8SBjoern A. Zeeb 		*mp = NULL;
5348f5a8818SKevin Lo 		return (IPPROTO_DONE);
535fb59c426SYoshinobu Inoue 	}
53677d396fdSMaksim Yevmenkin 	if (ia6)
53777d396fdSMaksim Yevmenkin 		ifa_free(&ia6->ia_ifa);
538fb59c426SYoshinobu Inoue 
539*a8fe77d8SBjoern A. Zeeb 	*mp = m;
5408f5a8818SKevin Lo 	return (tcp_input(mp, offp, proto));
541fb59c426SYoshinobu Inoue }
542b287c6c7SBjoern A. Zeeb #endif /* INET6 */
543fb59c426SYoshinobu Inoue 
5448f5a8818SKevin Lo int
5458f5a8818SKevin Lo tcp_input(struct mbuf **mp, int *offp, int proto)
546df8bae1dSRodney W. Grimes {
5478f5a8818SKevin Lo 	struct mbuf *m = *mp;
548b287c6c7SBjoern A. Zeeb 	struct tcphdr *th = NULL;
549ad3f9ab3SAndre Oppermann 	struct ip *ip = NULL;
550ad3f9ab3SAndre Oppermann 	struct inpcb *inp = NULL;
551302ce8d6SAndre Oppermann 	struct tcpcb *tp = NULL;
552302ce8d6SAndre Oppermann 	struct socket *so = NULL;
553e79adb8eSGarrett Wollman 	u_char *optp = NULL;
5548f5a8818SKevin Lo 	int off0;
55526f9a767SRodney W. Grimes 	int optlen = 0;
556b287c6c7SBjoern A. Zeeb #ifdef INET
557b287c6c7SBjoern A. Zeeb 	int len;
558c4556b2fSAndrey V. Elsukov 	uint8_t ipttl;
559b287c6c7SBjoern A. Zeeb #endif
560b287c6c7SBjoern A. Zeeb 	int tlen = 0, off;
561fb59c426SYoshinobu Inoue 	int drop_hdrlen;
562ad3f9ab3SAndre Oppermann 	int thflags;
563302ce8d6SAndre Oppermann 	int rstreason = 0;	/* For badport_bandlim accounting purposes */
5641d7ee746SKurt Lidl 	uint8_t iptos;
565c1de64a4SAndrey V. Elsukov 	struct m_tag *fwd_tag = NULL;
566c068736aSJeffrey Hsu #ifdef INET6
567302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6 = NULL;
568c068736aSJeffrey Hsu 	int isipv6;
569c068736aSJeffrey Hsu #else
570a160e630SAndre Oppermann 	const void *ip6 = NULL;
571b287c6c7SBjoern A. Zeeb #endif /* INET6 */
572302ce8d6SAndre Oppermann 	struct tcpopt to;		/* options in this segment */
573a160e630SAndre Oppermann 	char *s = NULL;			/* address and port logging */
574610ee2f9SDavid Greenman #ifdef TCPDEBUG
575c068736aSJeffrey Hsu 	/*
576c068736aSJeffrey Hsu 	 * The size of tcp_saveipgen must be the size of the max ip header,
577c068736aSJeffrey Hsu 	 * now IPv6.
578c068736aSJeffrey Hsu 	 */
57914739780SMaxim Konovalov 	u_char tcp_saveipgen[IP6_HDR_LEN];
580410bb1bfSLuigi Rizzo 	struct tcphdr tcp_savetcp;
581610ee2f9SDavid Greenman 	short ostate = 0;
582610ee2f9SDavid Greenman #endif
583c068736aSJeffrey Hsu 
584d40c0d47SGleb Smirnoff 	NET_EPOCH_ASSERT();
585d40c0d47SGleb Smirnoff 
586fb59c426SYoshinobu Inoue #ifdef INET6
587fb59c426SYoshinobu Inoue 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
588fb59c426SYoshinobu Inoue #endif
589a0292f23SGarrett Wollman 
5908f5a8818SKevin Lo 	off0 = *offp;
5918f5a8818SKevin Lo 	m = *mp;
5928f5a8818SKevin Lo 	*mp = NULL;
593302ce8d6SAndre Oppermann 	to.to_flags = 0;
59478b50714SRobert Watson 	TCPSTAT_INC(tcps_rcvtotal);
595fb59c426SYoshinobu Inoue 
59604d3a452SHajimu UMEMOTO #ifdef INET6
597b287c6c7SBjoern A. Zeeb 	if (isipv6) {
5988d573cc1SAndre Oppermann 		/* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */
59945747ba5SBjoern A. Zeeb 
60045747ba5SBjoern A. Zeeb 		if (m->m_len < (sizeof(*ip6) + sizeof(*th))) {
60145747ba5SBjoern A. Zeeb 			m = m_pullup(m, sizeof(*ip6) + sizeof(*th));
60245747ba5SBjoern A. Zeeb 			if (m == NULL) {
60345747ba5SBjoern A. Zeeb 				TCPSTAT_INC(tcps_rcvshort);
6048f5a8818SKevin Lo 				return (IPPROTO_DONE);
60545747ba5SBjoern A. Zeeb 			}
60645747ba5SBjoern A. Zeeb 		}
60745747ba5SBjoern A. Zeeb 
608fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
60945747ba5SBjoern A. Zeeb 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
610fb59c426SYoshinobu Inoue 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
611356ab07eSBjoern A. Zeeb 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) {
61245747ba5SBjoern A. Zeeb 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
61345747ba5SBjoern A. Zeeb 				th->th_sum = m->m_pkthdr.csum_data;
61445747ba5SBjoern A. Zeeb 			else
61545747ba5SBjoern A. Zeeb 				th->th_sum = in6_cksum_pseudo(ip6, tlen,
61645747ba5SBjoern A. Zeeb 				    IPPROTO_TCP, m->m_pkthdr.csum_data);
61745747ba5SBjoern A. Zeeb 			th->th_sum ^= 0xffff;
61845747ba5SBjoern A. Zeeb 		} else
61945747ba5SBjoern A. Zeeb 			th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen);
62045747ba5SBjoern A. Zeeb 		if (th->th_sum) {
62178b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
622fb59c426SYoshinobu Inoue 			goto drop;
623fb59c426SYoshinobu Inoue 		}
62433841545SHajimu UMEMOTO 
62533841545SHajimu UMEMOTO 		/*
62633841545SHajimu UMEMOTO 		 * Be proactive about unspecified IPv6 address in source.
62733841545SHajimu UMEMOTO 		 * As we use all-zero to indicate unbounded/unconnected pcb,
62833841545SHajimu UMEMOTO 		 * unspecified IPv6 address can be used to confuse us.
62933841545SHajimu UMEMOTO 		 *
63033841545SHajimu UMEMOTO 		 * Note that packets with unspecified IPv6 destination is
63133841545SHajimu UMEMOTO 		 * already dropped in ip6_input.
63233841545SHajimu UMEMOTO 		 */
63333841545SHajimu UMEMOTO 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
63433841545SHajimu UMEMOTO 			/* XXX stat */
63533841545SHajimu UMEMOTO 			goto drop;
63633841545SHajimu UMEMOTO 		}
6371d7ee746SKurt Lidl 		iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
638b287c6c7SBjoern A. Zeeb 	}
63904d3a452SHajimu UMEMOTO #endif
640b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
641b287c6c7SBjoern A. Zeeb 	else
642b287c6c7SBjoern A. Zeeb #endif
643b287c6c7SBjoern A. Zeeb #ifdef INET
644b287c6c7SBjoern A. Zeeb 	{
645df8bae1dSRodney W. Grimes 		/*
646df8bae1dSRodney W. Grimes 		 * Get IP and TCP header together in first mbuf.
647df8bae1dSRodney W. Grimes 		 * Note: IP leaves IP header in first mbuf.
648df8bae1dSRodney W. Grimes 		 */
649fb59c426SYoshinobu Inoue 		if (off0 > sizeof (struct ip)) {
650105bd211SGleb Smirnoff 			ip_stripoptions(m);
651fb59c426SYoshinobu Inoue 			off0 = sizeof(struct ip);
652fb59c426SYoshinobu Inoue 		}
653df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof (struct tcpiphdr)) {
6544dfdffe9SAndre Oppermann 			if ((m = m_pullup(m, sizeof (struct tcpiphdr)))
6554dfdffe9SAndre Oppermann 			    == NULL) {
65678b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvshort);
6578f5a8818SKevin Lo 				return (IPPROTO_DONE);
658df8bae1dSRodney W. Grimes 			}
659df8bae1dSRodney W. Grimes 		}
660fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
661db4f9cc7SJonathan Lemon 		th = (struct tcphdr *)((caddr_t)ip + off0);
6628ad458a4SGleb Smirnoff 		tlen = ntohs(ip->ip_len) - off0;
663df8bae1dSRodney W. Grimes 
6641d7ee746SKurt Lidl 		iptos = ip->ip_tos;
665db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
666db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
667db4f9cc7SJonathan Lemon 				th->th_sum = m->m_pkthdr.csum_data;
668db4f9cc7SJonathan Lemon 			else
669db4f9cc7SJonathan Lemon 				th->th_sum = in_pseudo(ip->ip_src.s_addr,
670c068736aSJeffrey Hsu 				    ip->ip_dst.s_addr,
6718f134647SGleb Smirnoff 				    htonl(m->m_pkthdr.csum_data + tlen +
672c068736aSJeffrey Hsu 				    IPPROTO_TCP));
673db4f9cc7SJonathan Lemon 			th->th_sum ^= 0xffff;
674db4f9cc7SJonathan Lemon 		} else {
6758f134647SGleb Smirnoff 			struct ipovly *ipov = (struct ipovly *)ip;
6768f134647SGleb Smirnoff 
677df8bae1dSRodney W. Grimes 			/*
678df8bae1dSRodney W. Grimes 			 * Checksum extended TCP header and data.
679df8bae1dSRodney W. Grimes 			 */
6808f134647SGleb Smirnoff 			len = off0 + tlen;
681c4556b2fSAndrey V. Elsukov 			ipttl = ip->ip_ttl;
682fb59c426SYoshinobu Inoue 			bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
6838f134647SGleb Smirnoff 			ipov->ih_len = htons(tlen);
684fb59c426SYoshinobu Inoue 			th->th_sum = in_cksum(m, len);
68557f60867SMark Johnston 			/* Reset length for SDT probes. */
6861d7ee746SKurt Lidl 			ip->ip_len = htons(len);
6871d7ee746SKurt Lidl 			/* Reset TOS bits */
6881d7ee746SKurt Lidl 			ip->ip_tos = iptos;
6891d7ee746SKurt Lidl 			/* Re-initialization for later version check */
690c4556b2fSAndrey V. Elsukov 			ip->ip_ttl = ipttl;
6911d7ee746SKurt Lidl 			ip->ip_v = IPVERSION;
692b2fe54bcSAndrey V. Elsukov 			ip->ip_hl = off0 >> 2;
693db4f9cc7SJonathan Lemon 		}
69457f60867SMark Johnston 
695fb59c426SYoshinobu Inoue 		if (th->th_sum) {
69678b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
697df8bae1dSRodney W. Grimes 			goto drop;
698df8bae1dSRodney W. Grimes 		}
699fb59c426SYoshinobu Inoue 	}
700b287c6c7SBjoern A. Zeeb #endif /* INET */
701df8bae1dSRodney W. Grimes 
702df8bae1dSRodney W. Grimes 	/*
703df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
704df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
705df8bae1dSRodney W. Grimes 	 */
706fb59c426SYoshinobu Inoue 	off = th->th_off << 2;
707df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
70878b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvbadoff);
709df8bae1dSRodney W. Grimes 		goto drop;
710df8bae1dSRodney W. Grimes 	}
711fb59c426SYoshinobu Inoue 	tlen -= off;	/* tlen is used instead of ti->ti_len */
712df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
71304d3a452SHajimu UMEMOTO #ifdef INET6
714b287c6c7SBjoern A. Zeeb 		if (isipv6) {
7158f5a8818SKevin Lo 			IP6_EXTHDR_CHECK(m, off0, off, IPPROTO_DONE);
716fb59c426SYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
717fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
718b287c6c7SBjoern A. Zeeb 		}
71904d3a452SHajimu UMEMOTO #endif
720b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
721b287c6c7SBjoern A. Zeeb 		else
722b287c6c7SBjoern A. Zeeb #endif
723b287c6c7SBjoern A. Zeeb #ifdef INET
724b287c6c7SBjoern A. Zeeb 		{
725df8bae1dSRodney W. Grimes 			if (m->m_len < sizeof(struct ip) + off) {
726c068736aSJeffrey Hsu 				if ((m = m_pullup(m, sizeof (struct ip) + off))
7274dfdffe9SAndre Oppermann 				    == NULL) {
72878b50714SRobert Watson 					TCPSTAT_INC(tcps_rcvshort);
7298f5a8818SKevin Lo 					return (IPPROTO_DONE);
730df8bae1dSRodney W. Grimes 				}
731fb59c426SYoshinobu Inoue 				ip = mtod(m, struct ip *);
732fb59c426SYoshinobu Inoue 				th = (struct tcphdr *)((caddr_t)ip + off0);
733fb59c426SYoshinobu Inoue 			}
734df8bae1dSRodney W. Grimes 		}
735b287c6c7SBjoern A. Zeeb #endif
736df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
737fb59c426SYoshinobu Inoue 		optp = (u_char *)(th + 1);
738df8bae1dSRodney W. Grimes 	}
739fb59c426SYoshinobu Inoue 	thflags = th->th_flags;
740df8bae1dSRodney W. Grimes 
741e46cd3d4SDag-Erling Smørgrav 	/*
742df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
743df8bae1dSRodney W. Grimes 	 */
7442903309aSAttilio Rao 	tcp_fields_to_host(th);
745df8bae1dSRodney W. Grimes 
746df8bae1dSRodney W. Grimes 	/*
747794235b7SAndre Oppermann 	 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options.
748755c1f07SAndras Olah 	 */
749fb59c426SYoshinobu Inoue 	drop_hdrlen = off0 + off;
750755c1f07SAndras Olah 
751755c1f07SAndras Olah 	/*
7528d573cc1SAndre Oppermann 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
7538d573cc1SAndre Oppermann 	 */
754b8056faeSGleb Smirnoff         if (
755b8056faeSGleb Smirnoff #ifdef INET6
756b8056faeSGleb Smirnoff 	    (isipv6 && (m->m_flags & M_IP6_NEXTHOP))
757b8056faeSGleb Smirnoff #ifdef INET
758b8056faeSGleb Smirnoff 	    || (!isipv6 && (m->m_flags & M_IP_NEXTHOP))
759b8056faeSGleb Smirnoff #endif
760b8056faeSGleb Smirnoff #endif
761b8056faeSGleb Smirnoff #if defined(INET) && !defined(INET6)
762b8056faeSGleb Smirnoff 	    (m->m_flags & M_IP_NEXTHOP)
763b8056faeSGleb Smirnoff #endif
764b8056faeSGleb Smirnoff 	    )
7659b932e9eSAndre Oppermann 		fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
7669b932e9eSAndre Oppermann 
767ce7ad664SEd Maste findpcb:
7688a006adbSBjoern A. Zeeb #ifdef INET6
7698a006adbSBjoern A. Zeeb 	if (isipv6 && fwd_tag != NULL) {
7708a006adbSBjoern A. Zeeb 		struct sockaddr_in6 *next_hop6;
7718a006adbSBjoern A. Zeeb 
7728a006adbSBjoern A. Zeeb 		next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
7738a006adbSBjoern A. Zeeb 		/*
7748a006adbSBjoern A. Zeeb 		 * Transparently forwarded. Pretend to be the destination.
7758a006adbSBjoern A. Zeeb 		 * Already got one like this?
7768a006adbSBjoern A. Zeeb 		 */
7778a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo,
7788a006adbSBjoern A. Zeeb 		    &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport,
7798a006adbSBjoern A. Zeeb 		    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif, m);
7808a006adbSBjoern A. Zeeb 		if (!inp) {
7818a006adbSBjoern A. Zeeb 			/*
7828a006adbSBjoern A. Zeeb 			 * It's new.  Try to find the ambushing socket.
7838a006adbSBjoern A. Zeeb 			 * Because we've rewritten the destination address,
7848a006adbSBjoern A. Zeeb 			 * any hardware-generated hash is ignored.
7858a006adbSBjoern A. Zeeb 			 */
7868a006adbSBjoern A. Zeeb 			inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src,
7878a006adbSBjoern A. Zeeb 			    th->th_sport, &next_hop6->sin6_addr,
7888a006adbSBjoern A. Zeeb 			    next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) :
7898a006adbSBjoern A. Zeeb 			    th->th_dport, INPLOOKUP_WILDCARD |
7908a006adbSBjoern A. Zeeb 			    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif);
7918a006adbSBjoern A. Zeeb 		}
792c1de64a4SAndrey V. Elsukov 	} else if (isipv6) {
7938a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src,
7948a006adbSBjoern A. Zeeb 		    th->th_sport, &ip6->ip6_dst, th->th_dport,
7958a006adbSBjoern A. Zeeb 		    INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB,
7968a006adbSBjoern A. Zeeb 		    m->m_pkthdr.rcvif, m);
7978a006adbSBjoern A. Zeeb 	}
7988a006adbSBjoern A. Zeeb #endif /* INET6 */
7998a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET)
8008a006adbSBjoern A. Zeeb 	else
8018a006adbSBjoern A. Zeeb #endif
8028a006adbSBjoern A. Zeeb #ifdef INET
8038a006adbSBjoern A. Zeeb 	if (fwd_tag != NULL) {
8049b932e9eSAndre Oppermann 		struct sockaddr_in *next_hop;
8059b932e9eSAndre Oppermann 
8069b932e9eSAndre Oppermann 		next_hop = (struct sockaddr_in *)(fwd_tag+1);
807f9e354dfSJulian Elischer 		/*
8082b25acc1SLuigi Rizzo 		 * Transparently forwarded. Pretend to be the destination.
809f9e354dfSJulian Elischer 		 * already got one like this?
810f9e354dfSJulian Elischer 		 */
811d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport,
812fa046d87SRobert Watson 		    ip->ip_dst, th->th_dport, INPLOOKUP_WLOCKPCB,
813d3c1f003SRobert Watson 		    m->m_pkthdr.rcvif, m);
814fa046d87SRobert Watson 		if (!inp) {
815fa046d87SRobert Watson 			/*
816fa046d87SRobert Watson 			 * It's new.  Try to find the ambushing socket.
817d3c1f003SRobert Watson 			 * Because we've rewritten the destination address,
818d3c1f003SRobert Watson 			 * any hardware-generated hash is ignored.
819fa046d87SRobert Watson 			 */
820fa046d87SRobert Watson 			inp = in_pcblookup(&V_tcbinfo, ip->ip_src,
821fa046d87SRobert Watson 			    th->th_sport, next_hop->sin_addr,
822fa046d87SRobert Watson 			    next_hop->sin_port ? ntohs(next_hop->sin_port) :
823fa046d87SRobert Watson 			    th->th_dport, INPLOOKUP_WILDCARD |
824fa046d87SRobert Watson 			    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif);
825f9e354dfSJulian Elischer 		}
826b10fbdeaSAndre Oppermann 	} else
827d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src,
828fa046d87SRobert Watson 		    th->th_sport, ip->ip_dst, th->th_dport,
829fa046d87SRobert Watson 		    INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB,
830d3c1f003SRobert Watson 		    m->m_pkthdr.rcvif, m);
8318a006adbSBjoern A. Zeeb #endif /* INET */
832f9e354dfSJulian Elischer 
833df8bae1dSRodney W. Grimes 	/*
834574b6964SAndre Oppermann 	 * If the INPCB does not exist then all data in the incoming
835574b6964SAndre Oppermann 	 * segment is discarded and an appropriate RST is sent back.
8368b07e49aSJulian Elischer 	 * XXX MRT Send RST using which routing table?
837df8bae1dSRodney W. Grimes 	 */
838816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
839574b6964SAndre Oppermann 		/*
840574b6964SAndre Oppermann 		 * Log communication attempts to ports that are not
841574b6964SAndre Oppermann 		 * in use.
842574b6964SAndre Oppermann 		 */
843574b6964SAndre Oppermann 		if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) ||
844574b6964SAndre Oppermann 		    tcp_log_in_vain == 2) {
845b7d747ecSAndre Oppermann 			if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6)))
846df541e5fSAndre Oppermann 				log(LOG_INFO, "%s; %s: Connection attempt "
847df541e5fSAndre Oppermann 				    "to closed port\n", s, __func__);
8482e4e1b4cSGeoff Rehmet 		}
849574b6964SAndre Oppermann 		/*
850574b6964SAndre Oppermann 		 * When blackholing do not respond with a RST but
851574b6964SAndre Oppermann 		 * completely ignore the segment and drop it.
852574b6964SAndre Oppermann 		 */
853603724d3SBjoern A. Zeeb 		if ((V_blackhole == 1 && (thflags & TH_SYN)) ||
854603724d3SBjoern A. Zeeb 		    V_blackhole == 2)
8551929eae1SAndre Oppermann 			goto dropunlock;
856574b6964SAndre Oppermann 
857a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
858a57815efSBosko Milekic 		goto dropwithreset;
859816a3d83SPoul-Henning Kamp 	}
860fa046d87SRobert Watson 	INP_WLOCK_ASSERT(inp);
861f5cf1e5fSJulien Charbon 	/*
862f5cf1e5fSJulien Charbon 	 * While waiting for inp lock during the lookup, another thread
863f5cf1e5fSJulien Charbon 	 * can have dropped the inpcb, in which case we need to loop back
864f5cf1e5fSJulien Charbon 	 * and try to find a new inpcb to deliver to.
865f5cf1e5fSJulien Charbon 	 */
866f5cf1e5fSJulien Charbon 	if (inp->inp_flags & INP_DROPPED) {
867f5cf1e5fSJulien Charbon 		INP_WUNLOCK(inp);
868f5cf1e5fSJulien Charbon 		inp = NULL;
869f5cf1e5fSJulien Charbon 		goto findpcb;
870f5cf1e5fSJulien Charbon 	}
871c2529042SHans Petter Selasky 	if ((inp->inp_flowtype == M_HASHTYPE_NONE) &&
872c2529042SHans Petter Selasky 	    (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) &&
873c2529042SHans Petter Selasky 	    ((inp->inp_socket == NULL) ||
874c2529042SHans Petter Selasky 	    (inp->inp_socket->so_options & SO_ACCEPTCONN) == 0)) {
87580cb9f21SKip Macy 		inp->inp_flowid = m->m_pkthdr.flowid;
8762f719932SAdrian Chadd 		inp->inp_flowtype = M_HASHTYPE_GET(m);
87780cb9f21SKip Macy 	}
878fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
879a2589465SKen Smith #ifdef INET6
880fcf59617SAndrey V. Elsukov 	if (isipv6 && IPSEC_ENABLED(ipv6) &&
881fcf59617SAndrey V. Elsukov 	    IPSEC_CHECK_POLICY(ipv6, m, inp) != 0) {
882a2589465SKen Smith 		goto dropunlock;
883a2589465SKen Smith 	}
884fcf59617SAndrey V. Elsukov #ifdef INET
885fcf59617SAndrey V. Elsukov 	else
886fcf59617SAndrey V. Elsukov #endif
887fcf59617SAndrey V. Elsukov #endif /* INET6 */
888fcf59617SAndrey V. Elsukov #ifdef INET
889fcf59617SAndrey V. Elsukov 	if (IPSEC_ENABLED(ipv4) &&
890fcf59617SAndrey V. Elsukov 	    IPSEC_CHECK_POLICY(ipv4, m, inp) != 0) {
891fcf59617SAndrey V. Elsukov 		goto dropunlock;
892fcf59617SAndrey V. Elsukov 	}
893fcf59617SAndrey V. Elsukov #endif /* INET */
894a2589465SKen Smith #endif /* IPSEC */
895a2589465SKen Smith 
8968d573cc1SAndre Oppermann 	/*
8978d573cc1SAndre Oppermann 	 * Check the minimum TTL for socket.
8988d573cc1SAndre Oppermann 	 */
89934f83c52SGeorge V. Neville-Neil 	if (inp->inp_ip_minttl != 0) {
90034f83c52SGeorge V. Neville-Neil #ifdef INET6
9012d8868dbSJonathan T. Looney 		if (isipv6) {
9022d8868dbSJonathan T. Looney 			if (inp->inp_ip_minttl > ip6->ip6_hlim)
903302ce8d6SAndre Oppermann 				goto dropunlock;
9042d8868dbSJonathan T. Looney 		} else
90534f83c52SGeorge V. Neville-Neil #endif
90634f83c52SGeorge V. Neville-Neil 		if (inp->inp_ip_minttl > ip->ip_ttl)
907302ce8d6SAndre Oppermann 			goto dropunlock;
90834f83c52SGeorge V. Neville-Neil 	}
909936cd18dSAndre Oppermann 
910340c35deSJonathan Lemon 	/*
911252ca428SRobert Watson 	 * A previous connection in TIMEWAIT state is supposed to catch stray
912252ca428SRobert Watson 	 * or duplicate segments arriving late.  If this segment was a
9130989f56cSRobert Watson 	 * legitimate new connection attempt, the old INPCB gets removed and
914252ca428SRobert Watson 	 * we can try again to find a listening socket.
915252ca428SRobert Watson 	 *
916fa046d87SRobert Watson 	 * At this point, due to earlier optimism, we may hold only an inpcb
917fa046d87SRobert Watson 	 * lock, and not the inpcbinfo write lock.  If so, we need to try to
918fa046d87SRobert Watson 	 * acquire it, or if that fails, acquire a reference on the inpcb,
919fa046d87SRobert Watson 	 * drop all locks, acquire a global write lock, and then re-acquire
920fa046d87SRobert Watson 	 * the inpcb lock.  We may at that point discover that another thread
921fa046d87SRobert Watson 	 * has tried to free the inpcb, in which case we need to loop back
922fa046d87SRobert Watson 	 * and try to find a new inpcb to deliver to.
923fa046d87SRobert Watson 	 *
924fa046d87SRobert Watson 	 * XXXRW: It may be time to rethink timewait locking.
925340c35deSJonathan Lemon 	 */
926ad71fe3cSRobert Watson 	if (inp->inp_flags & INP_TIMEWAIT) {
927340c35deSJonathan Lemon 		if (thflags & TH_SYN)
928f72167f4SAndre Oppermann 			tcp_dooptions(&to, optp, optlen, TO_SYN);
9298d573cc1SAndre Oppermann 		/*
9308d573cc1SAndre Oppermann 		 * NB: tcp_twcheck unlocks the INP and frees the mbuf.
9318d573cc1SAndre Oppermann 		 */
9322104448fSAndre Oppermann 		if (tcp_twcheck(inp, &to, th, m, tlen))
933340c35deSJonathan Lemon 			goto findpcb;
9348f5a8818SKevin Lo 		return (IPPROTO_DONE);
935340c35deSJonathan Lemon 	}
936794235b7SAndre Oppermann 	/*
937794235b7SAndre Oppermann 	 * The TCPCB may no longer exist if the connection is winding
938794235b7SAndre Oppermann 	 * down or it is in the CLOSED state.  Either way we drop the
939794235b7SAndre Oppermann 	 * segment and send an appropriate response.
940794235b7SAndre Oppermann 	 */
941df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
942a160e630SAndre Oppermann 	if (tp == NULL || tp->t_state == TCPS_CLOSED) {
943a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
944a57815efSBosko Milekic 		goto dropwithreset;
94509f81a46SBosko Milekic 	}
946df8bae1dSRodney W. Grimes 
94709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
94809fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
94909fe6320SNavdeep Parhar 		tcp_offload_input(tp, m);
95009fe6320SNavdeep Parhar 		m = NULL;	/* consumed by the TOE driver */
95109fe6320SNavdeep Parhar 		goto dropunlock;
95209fe6320SNavdeep Parhar 	}
95309fe6320SNavdeep Parhar #endif
95409fe6320SNavdeep Parhar 
955c488362eSRobert Watson #ifdef MAC
9568501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
95730d239bcSRobert Watson 	if (mac_inpcb_check_deliver(inp, m))
958302ce8d6SAndre Oppermann 		goto dropunlock;
959c488362eSRobert Watson #endif
960a557af22SRobert Watson 	so = inp->inp_socket;
961302ce8d6SAndre Oppermann 	KASSERT(so != NULL, ("%s: so == NULL", __func__));
962610ee2f9SDavid Greenman #ifdef TCPDEBUG
963df8bae1dSRodney W. Grimes 	if (so->so_options & SO_DEBUG) {
964df8bae1dSRodney W. Grimes 		ostate = tp->t_state;
96510fe523eSMaxim Konovalov #ifdef INET6
9666f697424SBjoern A. Zeeb 		if (isipv6) {
967540e8b7eSJeffrey Hsu 			bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6));
968d30d90dcSMaxim Konovalov 		} else
9696f697424SBjoern A. Zeeb #endif
970540e8b7eSJeffrey Hsu 			bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
971fb59c426SYoshinobu Inoue 		tcp_savetcp = *th;
972df8bae1dSRodney W. Grimes 	}
973b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */
974db33b3e6SAndre Oppermann 	/*
975db33b3e6SAndre Oppermann 	 * When the socket is accepting connections (the INPCB is in LISTEN
976db33b3e6SAndre Oppermann 	 * state) we look into the SYN cache if this is a new connection
977a7c7f2a7SJohn Baldwin 	 * attempt or the completion of a previous one.
978db33b3e6SAndre Oppermann 	 */
97945274760SJonathan T. Looney 	KASSERT(tp->t_state == TCPS_LISTEN || !(so->so_options & SO_ACCEPTCONN),
98045274760SJonathan T. Looney 	    ("%s: so accepting but tp %p not listening", __func__, tp));
98145274760SJonathan T. Looney 	if (tp->t_state == TCPS_LISTEN && (so->so_options & SO_ACCEPTCONN)) {
982540e8b7eSJeffrey Hsu 		struct in_conninfo inc;
983540e8b7eSJeffrey Hsu 
9848bfb1918SAndre Oppermann 		bzero(&inc, sizeof(inc));
985302ce8d6SAndre Oppermann #ifdef INET6
986be2ac88cSJonathan Lemon 		if (isipv6) {
987dcdb4371SBjoern A. Zeeb 			inc.inc_flags |= INC_ISIPV6;
9885dff1c38SMichael Tuexen 			if (inp->inp_inc.inc_flags & INC_IPV6MINMTU)
9895dff1c38SMichael Tuexen 				inc.inc_flags |= INC_IPV6MINMTU;
990be2ac88cSJonathan Lemon 			inc.inc6_faddr = ip6->ip6_src;
991be2ac88cSJonathan Lemon 			inc.inc6_laddr = ip6->ip6_dst;
992302ce8d6SAndre Oppermann 		} else
993302ce8d6SAndre Oppermann #endif
994302ce8d6SAndre Oppermann 		{
995be2ac88cSJonathan Lemon 			inc.inc_faddr = ip->ip_src;
996be2ac88cSJonathan Lemon 			inc.inc_laddr = ip->ip_dst;
997be2ac88cSJonathan Lemon 		}
998be2ac88cSJonathan Lemon 		inc.inc_fport = th->th_sport;
999be2ac88cSJonathan Lemon 		inc.inc_lport = th->th_dport;
10007973fba3SJulian Elischer 		inc.inc_fibnum = so->so_fibnum;
1001be2ac88cSJonathan Lemon 
1002fb59c426SYoshinobu Inoue 		/*
10038d573cc1SAndre Oppermann 		 * Check for an existing connection attempt in syncache if
10048d573cc1SAndre Oppermann 		 * the flag is only ACK.  A successful lookup creates a new
10058d573cc1SAndre Oppermann 		 * socket appended to the listen queue in SYN_RECEIVED state.
1006fb59c426SYoshinobu Inoue 		 */
1007be2ac88cSJonathan Lemon 		if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
1008a7c7f2a7SJohn Baldwin 
1009bf6d304aSAndre Oppermann 			/*
1010bf6d304aSAndre Oppermann 			 * Parse the TCP options here because
1011bf6d304aSAndre Oppermann 			 * syncookies need access to the reflected
1012bf6d304aSAndre Oppermann 			 * timestamp.
1013bf6d304aSAndre Oppermann 			 */
1014bf6d304aSAndre Oppermann 			tcp_dooptions(&to, optp, optlen, 0);
10159fa198beSAndre Oppermann 			/*
10169fa198beSAndre Oppermann 			 * NB: syncache_expand() doesn't unlock
10179fa198beSAndre Oppermann 			 * inp and tcpinfo locks.
10189fa198beSAndre Oppermann 			 */
1019fcf59617SAndrey V. Elsukov 			rstreason = syncache_expand(&inc, &to, th, &so, m);
1020fcf59617SAndrey V. Elsukov 			if (rstreason < 0) {
1021fcf59617SAndrey V. Elsukov 				/*
1022fcf59617SAndrey V. Elsukov 				 * A failing TCP MD5 signature comparison
1023fcf59617SAndrey V. Elsukov 				 * must result in the segment being dropped
1024fcf59617SAndrey V. Elsukov 				 * and must not produce any response back
1025fcf59617SAndrey V. Elsukov 				 * to the sender.
1026fcf59617SAndrey V. Elsukov 				 */
1027fcf59617SAndrey V. Elsukov 				goto dropunlock;
1028fcf59617SAndrey V. Elsukov 			} else if (rstreason == 0) {
10294195b4afSPaul Traina 				/*
1030e207f800SAndre Oppermann 				 * No syncache entry or ACK was not
1031be2ac88cSJonathan Lemon 				 * for our SYN/ACK.  Send a RST.
10328d573cc1SAndre Oppermann 				 * NB: syncache did its own logging
10338d573cc1SAndre Oppermann 				 * of the failure cause.
10344195b4afSPaul Traina 				 */
1035be2ac88cSJonathan Lemon 				rstreason = BANDLIM_RST_OPENPORT;
1036be2ac88cSJonathan Lemon 				goto dropwithreset;
1037be2ac88cSJonathan Lemon 			}
103809c305ebSPatrick Kelsey tfo_socket_result:
1039f76fcf6dSJeffrey Hsu 			if (so == NULL) {
1040be2ac88cSJonathan Lemon 				/*
1041e207f800SAndre Oppermann 				 * We completed the 3-way handshake
1042e207f800SAndre Oppermann 				 * but could not allocate a socket
1043e207f800SAndre Oppermann 				 * either due to memory shortage,
1044e207f800SAndre Oppermann 				 * listen queue length limits or
1045a160e630SAndre Oppermann 				 * global socket limits.  Send RST
1046a160e630SAndre Oppermann 				 * or wait and have the remote end
1047a160e630SAndre Oppermann 				 * retransmit the ACK for another
1048a160e630SAndre Oppermann 				 * try.
1049be2ac88cSJonathan Lemon 				 */
1050a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1051a160e630SAndre Oppermann 					log(LOG_DEBUG, "%s; %s: Listen socket: "
10528d573cc1SAndre Oppermann 					    "Socket allocation failed due to "
10538d573cc1SAndre Oppermann 					    "limits or memory shortage, %s\n",
1054ac957cd2SJulian Elischer 					    s, __func__,
1055ac957cd2SJulian Elischer 					    V_tcp_sc_rst_sock_fail ?
1056ac957cd2SJulian Elischer 					    "sending RST" : "try again");
1057603724d3SBjoern A. Zeeb 				if (V_tcp_sc_rst_sock_fail) {
1058e207f800SAndre Oppermann 					rstreason = BANDLIM_UNLIMITED;
1059e207f800SAndre Oppermann 					goto dropwithreset;
1060a160e630SAndre Oppermann 				} else
1061a160e630SAndre Oppermann 					goto dropunlock;
1062f76fcf6dSJeffrey Hsu 			}
1063be2ac88cSJonathan Lemon 			/*
1064be2ac88cSJonathan Lemon 			 * Socket is created in state SYN_RECEIVED.
10658d573cc1SAndre Oppermann 			 * Unlock the listen socket, lock the newly
10668d573cc1SAndre Oppermann 			 * created socket and update the tp variable.
1067be2ac88cSJonathan Lemon 			 */
10688501a69cSRobert Watson 			INP_WUNLOCK(inp);	/* listen socket */
1069be2ac88cSJonathan Lemon 			inp = sotoinpcb(so);
1070ff9b006dSJulien Charbon 			/*
1071ff9b006dSJulien Charbon 			 * New connection inpcb is already locked by
1072ff9b006dSJulien Charbon 			 * syncache_expand().
1073ff9b006dSJulien Charbon 			 */
1074ff9b006dSJulien Charbon 			INP_WLOCK_ASSERT(inp);
1075be2ac88cSJonathan Lemon 			tp = intotcpcb(inp);
10768d573cc1SAndre Oppermann 			KASSERT(tp->t_state == TCPS_SYN_RECEIVED,
10778d573cc1SAndre Oppermann 			    ("%s: ", __func__));
1078be2ac88cSJonathan Lemon 			/*
1079302ce8d6SAndre Oppermann 			 * Process the segment and the data it
1080302ce8d6SAndre Oppermann 			 * contains.  tcp_do_segment() consumes
1081302ce8d6SAndre Oppermann 			 * the mbuf chain and unlocks the inpcb.
1082302ce8d6SAndre Oppermann 			 */
10836138da62SMichael Tuexen 			TCP_PROBE5(receive, NULL, tp, m, tp, th);
108455bceb1eSRandall Stewart 			tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen,
10856573d758SMatt Macy 			    iptos);
10868f5a8818SKevin Lo 			return (IPPROTO_DONE);
1087be2ac88cSJonathan Lemon 		}
1088a160e630SAndre Oppermann 		/*
10898d573cc1SAndre Oppermann 		 * Segment flag validation for new connection attempts:
10908d573cc1SAndre Oppermann 		 *
1091a160e630SAndre Oppermann 		 * Our (SYN|ACK) response was rejected.
1092a160e630SAndre Oppermann 		 * Check with syncache and remove entry to prevent
1093a160e630SAndre Oppermann 		 * retransmits.
109419bc77c5SAndre Oppermann 		 *
109519bc77c5SAndre Oppermann 		 * NB: syncache_chkrst does its own logging of failure
109619bc77c5SAndre Oppermann 		 * causes.
1097a160e630SAndre Oppermann 		 */
1098a160e630SAndre Oppermann 		if (thflags & TH_RST) {
109993899d10SMichael Tuexen 			syncache_chkrst(&inc, th, m);
1100a160e630SAndre Oppermann 			goto dropunlock;
1101a160e630SAndre Oppermann 		}
1102a160e630SAndre Oppermann 		/*
1103a160e630SAndre Oppermann 		 * We can't do anything without SYN.
1104a160e630SAndre Oppermann 		 */
1105a160e630SAndre Oppermann 		if ((thflags & TH_SYN) == 0) {
1106a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1107a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1108773673c1SAndre Oppermann 				    "SYN is missing, segment ignored\n",
11098d573cc1SAndre Oppermann 				    s, __func__);
111078b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1111a160e630SAndre Oppermann 			goto dropunlock;
1112a160e630SAndre Oppermann 		}
1113a160e630SAndre Oppermann 		/*
1114a160e630SAndre Oppermann 		 * (SYN|ACK) is bogus on a listen socket.
1115a160e630SAndre Oppermann 		 */
1116fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1117a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1118a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
11198d573cc1SAndre Oppermann 				    "SYN|ACK invalid, segment rejected\n",
11208d573cc1SAndre Oppermann 				    s, __func__);
1121a160e630SAndre Oppermann 			syncache_badack(&inc);	/* XXX: Not needed! */
112278b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1123a57815efSBosko Milekic 			rstreason = BANDLIM_RST_OPENPORT;
1124a57815efSBosko Milekic 			goto dropwithreset;
11254195b4afSPaul Traina 		}
1126a160e630SAndre Oppermann 		/*
1127a160e630SAndre Oppermann 		 * If the drop_synfin option is enabled, drop all
1128a160e630SAndre Oppermann 		 * segments with both the SYN and FIN bits set.
1129a160e630SAndre Oppermann 		 * This prevents e.g. nmap from identifying the
1130a160e630SAndre Oppermann 		 * TCP/IP stack.
1131a160e630SAndre Oppermann 		 * XXX: Poor reasoning.  nmap has other methods
1132a160e630SAndre Oppermann 		 * and is constantly refining its stack detection
1133a160e630SAndre Oppermann 		 * strategies.
11348d573cc1SAndre Oppermann 		 * XXX: This is a violation of the TCP specification
1135a160e630SAndre Oppermann 		 * and was used by RFC1644.
1136a160e630SAndre Oppermann 		 */
1137603724d3SBjoern A. Zeeb 		if ((thflags & TH_FIN) && V_drop_synfin) {
1138a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1139a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1140773673c1SAndre Oppermann 				    "SYN|FIN segment ignored (based on "
11418d573cc1SAndre Oppermann 				    "sysctl setting)\n", s, __func__);
114278b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1143302ce8d6SAndre Oppermann 			goto dropunlock;
1144ebb0cbeaSPaul Traina 		}
1145be2ac88cSJonathan Lemon 		/*
1146be2ac88cSJonathan Lemon 		 * Segment's flags are (SYN) or (SYN|FIN).
1147a160e630SAndre Oppermann 		 *
1148a160e630SAndre Oppermann 		 * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored
1149a160e630SAndre Oppermann 		 * as they do not affect the state of the TCP FSM.
1150a160e630SAndre Oppermann 		 * The data pointed to by TH_URG and th_urp is ignored.
1151be2ac88cSJonathan Lemon 		 */
1152a160e630SAndre Oppermann 		KASSERT((thflags & (TH_RST|TH_ACK)) == 0,
1153a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_RST or TH_ACK set", __func__));
1154a160e630SAndre Oppermann 		KASSERT(thflags & (TH_SYN),
1155a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_SYN not set", __func__));
115633841545SHajimu UMEMOTO #ifdef INET6
115733841545SHajimu UMEMOTO 		/*
115833841545SHajimu UMEMOTO 		 * If deprecated address is forbidden,
115933841545SHajimu UMEMOTO 		 * we do not accept SYN to deprecated interface
116033841545SHajimu UMEMOTO 		 * address to prevent any new inbound connection from
116133841545SHajimu UMEMOTO 		 * getting established.
116233841545SHajimu UMEMOTO 		 * When we do not accept SYN, we send a TCP RST,
116333841545SHajimu UMEMOTO 		 * with deprecated source address (instead of dropping
116433841545SHajimu UMEMOTO 		 * it).  We compromise it as it is much better for peer
116533841545SHajimu UMEMOTO 		 * to send a RST, and RST will be the final packet
116633841545SHajimu UMEMOTO 		 * for the exchange.
116733841545SHajimu UMEMOTO 		 *
116833841545SHajimu UMEMOTO 		 * If we do not forbid deprecated addresses, we accept
116933841545SHajimu UMEMOTO 		 * the SYN packet.  RFC2462 does not suggest dropping
117033841545SHajimu UMEMOTO 		 * SYN in this case.
117133841545SHajimu UMEMOTO 		 * If we decipher RFC2462 5.5.4, it says like this:
117233841545SHajimu UMEMOTO 		 * 1. use of deprecated addr with existing
117333841545SHajimu UMEMOTO 		 *    communication is okay - "SHOULD continue to be
117433841545SHajimu UMEMOTO 		 *    used"
117533841545SHajimu UMEMOTO 		 * 2. use of it with new communication:
117633841545SHajimu UMEMOTO 		 *   (2a) "SHOULD NOT be used if alternate address
117733841545SHajimu UMEMOTO 		 *        with sufficient scope is available"
117833841545SHajimu UMEMOTO 		 *   (2b) nothing mentioned otherwise.
117933841545SHajimu UMEMOTO 		 * Here we fall into (2b) case as we have no choice in
118033841545SHajimu UMEMOTO 		 * our source address selection - we must obey the peer.
118133841545SHajimu UMEMOTO 		 *
118233841545SHajimu UMEMOTO 		 * The wording in RFC2462 is confusing, and there are
118333841545SHajimu UMEMOTO 		 * multiple description text for deprecated address
118433841545SHajimu UMEMOTO 		 * handling - worse, they are not exactly the same.
118533841545SHajimu UMEMOTO 		 * I believe 5.5.4 is the best one, so we follow 5.5.4.
118633841545SHajimu UMEMOTO 		 */
1187603724d3SBjoern A. Zeeb 		if (isipv6 && !V_ip6_use_deprecated) {
118833841545SHajimu UMEMOTO 			struct in6_ifaddr *ia6;
118933841545SHajimu UMEMOTO 
11903e88eb90SAndrey V. Elsukov 			ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
11918c0fec80SRobert Watson 			if (ia6 != NULL &&
119233841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
11938c0fec80SRobert Watson 				ifa_free(&ia6->ia_ifa);
1194a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1195a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
11968d573cc1SAndre Oppermann 					"Connection attempt to deprecated "
11978d573cc1SAndre Oppermann 					"IPv6 address rejected\n",
1198a160e630SAndre Oppermann 					s, __func__);
119933841545SHajimu UMEMOTO 				rstreason = BANDLIM_RST_OPENPORT;
120033841545SHajimu UMEMOTO 				goto dropwithreset;
120133841545SHajimu UMEMOTO 			}
120277d396fdSMaksim Yevmenkin 			if (ia6)
12038c0fec80SRobert Watson 				ifa_free(&ia6->ia_ifa);
120433841545SHajimu UMEMOTO 		}
1205b287c6c7SBjoern A. Zeeb #endif /* INET6 */
120665f28919SJesper Skriver 		/*
1207db33b3e6SAndre Oppermann 		 * Basic sanity checks on incoming SYN requests:
12088d573cc1SAndre Oppermann 		 *   Don't respond if the destination is a link layer
1209db33b3e6SAndre Oppermann 		 *	broadcast according to RFC1122 4.2.3.10, p. 104.
12108d573cc1SAndre Oppermann 		 *   If it is from this socket it must be forged.
12118d573cc1SAndre Oppermann 		 *   Don't respond if the source or destination is a
12128d573cc1SAndre Oppermann 		 *	global or subnet broad- or multicast address.
121393ec91baSCrist J. Clark 		 *   Note that it is quite possible to receive unicast
121493ec91baSCrist J. Clark 		 *	link-layer packets with a broadcast IP address. Use
121593ec91baSCrist J. Clark 		 *	in_broadcast() to find them.
1216be2ac88cSJonathan Lemon 		 */
12178d573cc1SAndre Oppermann 		if (m->m_flags & (M_BCAST|M_MCAST)) {
12188d573cc1SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
12198d573cc1SAndre Oppermann 			    log(LOG_DEBUG, "%s; %s: Listen socket: "
12208d573cc1SAndre Oppermann 				"Connection attempt from broad- or multicast "
1221773673c1SAndre Oppermann 				"link layer address ignored\n", s, __func__);
1222302ce8d6SAndre Oppermann 			goto dropunlock;
12238d573cc1SAndre Oppermann 		}
1224db33b3e6SAndre Oppermann #ifdef INET6
1225b287c6c7SBjoern A. Zeeb 		if (isipv6) {
1226db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1227a160e630SAndre Oppermann 			    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) {
1228a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1229a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12308d573cc1SAndre Oppermann 					"Connection attempt to/from self "
1231773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1232302ce8d6SAndre Oppermann 				goto dropunlock;
1233a160e630SAndre Oppermann 			}
1234be2ac88cSJonathan Lemon 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1235a160e630SAndre Oppermann 			    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
1236a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1237a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12388d573cc1SAndre Oppermann 					"Connection attempt from/to multicast "
1239773673c1SAndre Oppermann 					"address ignored\n", s, __func__);
1240302ce8d6SAndre Oppermann 				goto dropunlock;
1241a160e630SAndre Oppermann 			}
1242b287c6c7SBjoern A. Zeeb 		}
1243db33b3e6SAndre Oppermann #endif
1244b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
1245b287c6c7SBjoern A. Zeeb 		else
1246b287c6c7SBjoern A. Zeeb #endif
1247b287c6c7SBjoern A. Zeeb #ifdef INET
1248b287c6c7SBjoern A. Zeeb 		{
1249db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1250a160e630SAndre Oppermann 			    ip->ip_dst.s_addr == ip->ip_src.s_addr) {
1251a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1252a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12538d573cc1SAndre Oppermann 					"Connection attempt from/to self "
1254773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1255302ce8d6SAndre Oppermann 				goto dropunlock;
1256a160e630SAndre Oppermann 			}
1257be2ac88cSJonathan Lemon 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
1258be2ac88cSJonathan Lemon 			    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
12592ca2159fSCrist J. Clark 			    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
1260a160e630SAndre Oppermann 			    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
1261a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1262a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12638d573cc1SAndre Oppermann 					"Connection attempt from/to broad- "
1264773673c1SAndre Oppermann 					"or multicast address ignored\n",
1265a160e630SAndre Oppermann 					s, __func__);
1266302ce8d6SAndre Oppermann 				goto dropunlock;
1267c068736aSJeffrey Hsu 			}
1268a160e630SAndre Oppermann 		}
1269b287c6c7SBjoern A. Zeeb #endif
1270be2ac88cSJonathan Lemon 		/*
1271db33b3e6SAndre Oppermann 		 * SYN appears to be valid.  Create compressed TCP state
1272db33b3e6SAndre Oppermann 		 * for syncache.
1273be2ac88cSJonathan Lemon 		 */
12743c653157SHartmut Brandt #ifdef TCPDEBUG
12753c653157SHartmut Brandt 		if (so->so_options & SO_DEBUG)
12763c653157SHartmut Brandt 			tcp_trace(TA_INPUT, ostate, tp,
12773c653157SHartmut Brandt 			    (void *)tcp_saveipgen, &tcp_savetcp, 0);
12783c653157SHartmut Brandt #endif
12792b9c9984SGeorge V. Neville-Neil 		TCP_PROBE3(debug__input, tp, th, m);
1280f72167f4SAndre Oppermann 		tcp_dooptions(&to, optp, optlen, TO_SYN);
1281281a0fd4SPatrick Kelsey 		if (syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL))
128209c305ebSPatrick Kelsey 			goto tfo_socket_result;
128318a75309SPatrick Kelsey 
1284be2ac88cSJonathan Lemon 		/*
12854d6e7130SAndre Oppermann 		 * Entry added to syncache and mbuf consumed.
1286a7c7f2a7SJohn Baldwin 		 * Only the listen socket is unlocked by syncache_add().
1287be2ac88cSJonathan Lemon 		 */
1288384a5c3cSAndrey V. Elsukov 		INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo);
12898f5a8818SKevin Lo 		return (IPPROTO_DONE);
1290982c1675SAndre Oppermann 	} else if (tp->t_state == TCPS_LISTEN) {
1291982c1675SAndre Oppermann 		/*
1292982c1675SAndre Oppermann 		 * When a listen socket is torn down the SO_ACCEPTCONN
1293982c1675SAndre Oppermann 		 * flag is removed first while connections are drained
1294982c1675SAndre Oppermann 		 * from the accept queue in a unlock/lock cycle of the
1295982c1675SAndre Oppermann 		 * ACCEPT_LOCK, opening a race condition allowing a SYN
1296982c1675SAndre Oppermann 		 * attempt go through unhandled.
1297982c1675SAndre Oppermann 		 */
1298982c1675SAndre Oppermann 		goto dropunlock;
1299f76fcf6dSJeffrey Hsu 	}
1300fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1301fcf59617SAndrey V. Elsukov 	if (tp->t_flags & TF_SIGNATURE) {
1302fcf59617SAndrey V. Elsukov 		tcp_dooptions(&to, optp, optlen, thflags);
1303fcf59617SAndrey V. Elsukov 		if ((to.to_flags & TOF_SIGNATURE) == 0) {
1304fcf59617SAndrey V. Elsukov 			TCPSTAT_INC(tcps_sig_err_nosigopt);
13052903309aSAttilio Rao 			goto dropunlock;
13062903309aSAttilio Rao 		}
1307fcf59617SAndrey V. Elsukov 		if (!TCPMD5_ENABLED() ||
1308fcf59617SAndrey V. Elsukov 		    TCPMD5_INPUT(m, th, to.to_signature) != 0)
1309fcf59617SAndrey V. Elsukov 			goto dropunlock;
13102903309aSAttilio Rao 	}
13112903309aSAttilio Rao #endif
13122b9c9984SGeorge V. Neville-Neil 	TCP_PROBE5(receive, NULL, tp, m, tp, th);
131357f60867SMark Johnston 
1314302ce8d6SAndre Oppermann 	/*
13158d573cc1SAndre Oppermann 	 * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later
13168d573cc1SAndre Oppermann 	 * state.  tcp_do_segment() always consumes the mbuf chain, unlocks
13178d573cc1SAndre Oppermann 	 * the inpcb, and unlocks pcbinfo.
1318302ce8d6SAndre Oppermann 	 */
13196573d758SMatt Macy 	tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos);
13208f5a8818SKevin Lo 	return (IPPROTO_DONE);
1321be2ac88cSJonathan Lemon 
1322302ce8d6SAndre Oppermann dropwithreset:
13232b9c9984SGeorge V. Neville-Neil 	TCP_PROBE5(receive, NULL, tp, m, tp, th);
132457f60867SMark Johnston 
1325014ea782SRobert Watson 	if (inp != NULL) {
1326302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
1327014ea782SRobert Watson 		INP_WUNLOCK(inp);
1328dd8ac7f9SRobert Watson 	} else
1329014ea782SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
1330302ce8d6SAndre Oppermann 	m = NULL;	/* mbuf chain got consumed. */
1331014ea782SRobert Watson 	goto drop;
1332014ea782SRobert Watson 
1333302ce8d6SAndre Oppermann dropunlock:
133457f60867SMark Johnston 	if (m != NULL)
13352b9c9984SGeorge V. Neville-Neil 		TCP_PROBE5(receive, NULL, tp, m, tp, th);
133657f60867SMark Johnston 
13379fa198beSAndre Oppermann 	if (inp != NULL)
13388501a69cSRobert Watson 		INP_WUNLOCK(inp);
1339014ea782SRobert Watson 
1340302ce8d6SAndre Oppermann drop:
1341384a5c3cSAndrey V. Elsukov 	INP_INFO_WUNLOCK_ASSERT(&V_tcbinfo);
1342a160e630SAndre Oppermann 	if (s != NULL)
1343a160e630SAndre Oppermann 		free(s, M_TCPLOG);
1344302ce8d6SAndre Oppermann 	if (m != NULL)
1345302ce8d6SAndre Oppermann 		m_freem(m);
13468f5a8818SKevin Lo 	return (IPPROTO_DONE);
1347302ce8d6SAndre Oppermann }
1348302ce8d6SAndre Oppermann 
1349e44c1887SSteven Hartland /*
1350e44c1887SSteven Hartland  * Automatic sizing of receive socket buffer.  Often the send
1351e44c1887SSteven Hartland  * buffer size is not optimally adjusted to the actual network
1352e44c1887SSteven Hartland  * conditions at hand (delay bandwidth product).  Setting the
1353e44c1887SSteven Hartland  * buffer size too small limits throughput on links with high
1354e44c1887SSteven Hartland  * bandwidth and high delay (eg. trans-continental/oceanic links).
1355e44c1887SSteven Hartland  *
1356e44c1887SSteven Hartland  * On the receive side the socket buffer memory is only rarely
1357e44c1887SSteven Hartland  * used to any significant extent.  This allows us to be much
1358e44c1887SSteven Hartland  * more aggressive in scaling the receive socket buffer.  For
1359e44c1887SSteven Hartland  * the case that the buffer space is actually used to a large
1360e44c1887SSteven Hartland  * extent and we run out of kernel memory we can simply drop
1361e44c1887SSteven Hartland  * the new segments; TCP on the sender will just retransmit it
1362e44c1887SSteven Hartland  * later.  Setting the buffer size too big may only consume too
1363e44c1887SSteven Hartland  * much kernel memory if the application doesn't read() from
1364e44c1887SSteven Hartland  * the socket or packet loss or reordering makes use of the
1365e44c1887SSteven Hartland  * reassembly queue.
1366e44c1887SSteven Hartland  *
1367e44c1887SSteven Hartland  * The criteria to step up the receive buffer one notch are:
1368e44c1887SSteven Hartland  *  1. Application has not set receive buffer size with
1369e44c1887SSteven Hartland  *     SO_RCVBUF. Setting SO_RCVBUF clears SB_AUTOSIZE.
1370560c0586SMichael Tuexen  *  2. the number of bytes received during 1/2 of an sRTT
1371560c0586SMichael Tuexen  *     is at least 3/8 of the current socket buffer size.
1372560c0586SMichael Tuexen  *  3. receive buffer size has not hit maximal automatic size;
1373e44c1887SSteven Hartland  *
1374560c0586SMichael Tuexen  * If all of the criteria are met we increaset the socket buffer
1375560c0586SMichael Tuexen  * by a 1/2 (bounded by the max). This allows us to keep ahead
1376560c0586SMichael Tuexen  * of slow-start but also makes it so our peer never gets limited
1377560c0586SMichael Tuexen  * by our rwnd which we then open up causing a burst.
1378560c0586SMichael Tuexen  *
1379560c0586SMichael Tuexen  * This algorithm does two steps per RTT at most and only if
1380e44c1887SSteven Hartland  * we receive a bulk stream w/o packet losses or reorderings.
1381e44c1887SSteven Hartland  * Shrinking the buffer during idle times is not necessary as
1382e44c1887SSteven Hartland  * it doesn't consume any memory when idle.
1383e44c1887SSteven Hartland  *
1384e44c1887SSteven Hartland  * TODO: Only step up if the application is actually serving
1385e44c1887SSteven Hartland  * the buffer to better manage the socket buffer resources.
1386e44c1887SSteven Hartland  */
1387e44c1887SSteven Hartland int
1388e44c1887SSteven Hartland tcp_autorcvbuf(struct mbuf *m, struct tcphdr *th, struct socket *so,
1389e44c1887SSteven Hartland     struct tcpcb *tp, int tlen)
1390e44c1887SSteven Hartland {
1391e44c1887SSteven Hartland 	int newsize = 0;
1392e44c1887SSteven Hartland 
1393e44c1887SSteven Hartland 	if (V_tcp_do_autorcvbuf && (so->so_rcv.sb_flags & SB_AUTOSIZE) &&
1394e44c1887SSteven Hartland 	    tp->t_srtt != 0 && tp->rfbuf_ts != 0 &&
1395e44c1887SSteven Hartland 	    TCP_TS_TO_TICKS(tcp_ts_getticks() - tp->rfbuf_ts) >
1396560c0586SMichael Tuexen 	    ((tp->t_srtt >> TCP_RTT_SHIFT)/2)) {
1397560c0586SMichael Tuexen 		if (tp->rfbuf_cnt > ((so->so_rcv.sb_hiwat / 2)/ 4 * 3) &&
1398e44c1887SSteven Hartland 		    so->so_rcv.sb_hiwat < V_tcp_autorcvbuf_max) {
1399560c0586SMichael Tuexen 			newsize = min((so->so_rcv.sb_hiwat + (so->so_rcv.sb_hiwat/2)), V_tcp_autorcvbuf_max);
1400e44c1887SSteven Hartland 		}
1401e44c1887SSteven Hartland 		TCP_PROBE6(receive__autoresize, NULL, tp, m, tp, th, newsize);
1402e44c1887SSteven Hartland 
1403e44c1887SSteven Hartland 		/* Start over with next RTT. */
1404e44c1887SSteven Hartland 		tp->rfbuf_ts = 0;
1405e44c1887SSteven Hartland 		tp->rfbuf_cnt = 0;
1406e44c1887SSteven Hartland 	} else {
1407e44c1887SSteven Hartland 		tp->rfbuf_cnt += tlen;	/* add up */
1408e44c1887SSteven Hartland 	}
1409e44c1887SSteven Hartland 	return (newsize);
1410e44c1887SSteven Hartland }
1411e44c1887SSteven Hartland 
141255bceb1eSRandall Stewart void
1413302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so,
14146573d758SMatt Macy     struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos)
1415302ce8d6SAndre Oppermann {
1416021eaf79SHiren Panchasara 	int thflags, acked, ourfinisacked, needoutput = 0, sack_changed;
1417302ce8d6SAndre Oppermann 	int rstreason, todrop, win;
14183ac12506SJonathan T. Looney 	uint32_t tiwin;
14194b7b743cSLawrence Stewart 	uint16_t nsegs;
142007dacf03SAndre Oppermann 	char *s;
142107dacf03SAndre Oppermann 	struct in_conninfo *inc;
1422c11a15bfSGleb Smirnoff 	struct mbuf *mfree;
1423302ce8d6SAndre Oppermann 	struct tcpopt to;
1424281a0fd4SPatrick Kelsey 	int tfo_syn;
1425302ce8d6SAndre Oppermann 
142614739780SMaxim Konovalov #ifdef TCPDEBUG
142714739780SMaxim Konovalov 	/*
142814739780SMaxim Konovalov 	 * The size of tcp_saveipgen must be the size of the max ip header,
142914739780SMaxim Konovalov 	 * now IPv6.
143014739780SMaxim Konovalov 	 */
143114739780SMaxim Konovalov 	u_char tcp_saveipgen[IP6_HDR_LEN];
143214739780SMaxim Konovalov 	struct tcphdr tcp_savetcp;
143314739780SMaxim Konovalov 	short ostate = 0;
143414739780SMaxim Konovalov #endif
1435302ce8d6SAndre Oppermann 	thflags = th->th_flags;
143607dacf03SAndre Oppermann 	inc = &tp->t_inpcb->inp_inc;
1437d64a46eaSLawrence Stewart 	tp->sackhint.last_sack_ack = 0;
1438021eaf79SHiren Panchasara 	sack_changed = 0;
14394b7b743cSLawrence Stewart 	nsegs = max(1, m->m_pkthdr.lro_nsegs);
1440d40c0d47SGleb Smirnoff 
1441d40c0d47SGleb Smirnoff 	NET_EPOCH_ASSERT();
14428501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1443679d9708SAndre Oppermann 	KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
1444679d9708SAndre Oppermann 	    __func__));
1445679d9708SAndre Oppermann 	KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
1446679d9708SAndre Oppermann 	    __func__));
1447df8bae1dSRodney W. Grimes 
144886a996e6SHiren Panchasara #ifdef TCPPCAP
144986a996e6SHiren Panchasara 	/* Save segment, if requested. */
145086a996e6SHiren Panchasara 	tcp_pcap_add(th, m, &(tp->t_inpkts));
145186a996e6SHiren Panchasara #endif
14522529f56eSJonathan T. Looney 	TCP_LOG_EVENT(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_IN, 0,
14532529f56eSJonathan T. Looney 	    tlen, NULL, true);
145486a996e6SHiren Panchasara 
1455013f4df6SMichael Tuexen 	if ((thflags & TH_SYN) && (thflags & TH_FIN) && V_drop_synfin) {
1456013f4df6SMichael Tuexen 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
1457013f4df6SMichael Tuexen 			log(LOG_DEBUG, "%s; %s: "
1458013f4df6SMichael Tuexen 			    "SYN|FIN segment ignored (based on "
1459013f4df6SMichael Tuexen 			    "sysctl setting)\n", s, __func__);
1460013f4df6SMichael Tuexen 			free(s, M_TCPLOG);
1461013f4df6SMichael Tuexen 		}
1462013f4df6SMichael Tuexen 		goto drop;
1463013f4df6SMichael Tuexen 	}
1464013f4df6SMichael Tuexen 
1465df8bae1dSRodney W. Grimes 	/*
1466ebfd7534SMichael Tuexen 	 * If a segment with the ACK-bit set arrives in the SYN-SENT state
1467ebfd7534SMichael Tuexen 	 * check SEQ.ACK first.
1468ebfd7534SMichael Tuexen 	 */
1469ebfd7534SMichael Tuexen 	if ((tp->t_state == TCPS_SYN_SENT) && (thflags & TH_ACK) &&
1470ebfd7534SMichael Tuexen 	    (SEQ_LEQ(th->th_ack, tp->iss) || SEQ_GT(th->th_ack, tp->snd_max))) {
1471ebfd7534SMichael Tuexen 		rstreason = BANDLIM_UNLIMITED;
1472ebfd7534SMichael Tuexen 		goto dropwithreset;
1473ebfd7534SMichael Tuexen 	}
1474ebfd7534SMichael Tuexen 
1475ebfd7534SMichael Tuexen 	/*
1476df8bae1dSRodney W. Grimes 	 * Segment received on connection.
1477df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
1478e8949f74SAndre Oppermann 	 * XXX: This should be done after segment
1479e8949f74SAndre Oppermann 	 * validation to ignore broken/spoofed segs.
1480df8bae1dSRodney W. Grimes 	 */
14819b8b58e0SJonathan Lemon 	tp->t_rcvtime = ticks;
1482df8bae1dSRodney W. Grimes 
1483df8bae1dSRodney W. Grimes 	/*
1484c52102ddSHiren Panchasara 	 * Scale up the window into a 32-bit value.
1485e8949f74SAndre Oppermann 	 * For the SYN_SENT state the scale is zero.
1486464fcfbcSAndre Oppermann 	 */
1487464fcfbcSAndre Oppermann 	tiwin = th->th_win << tp->snd_scale;
1488464fcfbcSAndre Oppermann 
1489464fcfbcSAndre Oppermann 	/*
1490f2512ba1SRui Paulo 	 * TCP ECN processing.
1491f2512ba1SRui Paulo 	 */
1492f2512ba1SRui Paulo 	if (tp->t_flags & TF_ECN_PERMIT) {
14939c251892SRui Paulo 		if (thflags & TH_CWR)
14949c251892SRui Paulo 			tp->t_flags &= ~TF_ECN_SND_ECE;
1495f2512ba1SRui Paulo 		switch (iptos & IPTOS_ECN_MASK) {
1496f2512ba1SRui Paulo 		case IPTOS_ECN_CE:
1497f2512ba1SRui Paulo 			tp->t_flags |= TF_ECN_SND_ECE;
149878b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ce);
1499f2512ba1SRui Paulo 			break;
1500f2512ba1SRui Paulo 		case IPTOS_ECN_ECT0:
150178b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ect0);
1502f2512ba1SRui Paulo 			break;
1503f2512ba1SRui Paulo 		case IPTOS_ECN_ECT1:
150478b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ect1);
1505f2512ba1SRui Paulo 			break;
1506f2512ba1SRui Paulo 		}
150764807b30SHiren Panchasara 
150864807b30SHiren Panchasara 		/* Process a packet differently from RFC3168. */
150964807b30SHiren Panchasara 		cc_ecnpkt_handler(tp, th, iptos);
151064807b30SHiren Panchasara 
1511dbc42409SLawrence Stewart 		/* Congestion experienced. */
1512dbc42409SLawrence Stewart 		if (thflags & TH_ECE) {
1513dbc42409SLawrence Stewart 			cc_cong_signal(tp, th, CC_ECN);
1514f2512ba1SRui Paulo 		}
1515f2512ba1SRui Paulo 	}
1516f2512ba1SRui Paulo 
1517f2512ba1SRui Paulo 	/*
1518f72167f4SAndre Oppermann 	 * Parse options on any incoming segment.
1519f72167f4SAndre Oppermann 	 */
1520302ce8d6SAndre Oppermann 	tcp_dooptions(&to, (u_char *)(th + 1),
1521302ce8d6SAndre Oppermann 	    (th->th_off << 2) - sizeof(struct tcphdr),
1522302ce8d6SAndre Oppermann 	    (thflags & TH_SYN) ? TO_SYN : 0);
1523f72167f4SAndre Oppermann 
1524fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE)
1525fcf59617SAndrey V. Elsukov 	if ((tp->t_flags & TF_SIGNATURE) != 0 &&
1526fcf59617SAndrey V. Elsukov 	    (to.to_flags & TOF_SIGNATURE) == 0) {
1527fcf59617SAndrey V. Elsukov 		TCPSTAT_INC(tcps_sig_err_sigopt);
1528fcf59617SAndrey V. Elsukov 		/* XXX: should drop? */
1529fcf59617SAndrey V. Elsukov 	}
1530fcf59617SAndrey V. Elsukov #endif
1531f72167f4SAndre Oppermann 	/*
1532f72167f4SAndre Oppermann 	 * If echoed timestamp is later than the current time,
1533bf6d304aSAndre Oppermann 	 * fall back to non RFC1323 RTT calculation.  Normalize
1534bf6d304aSAndre Oppermann 	 * timestamp if syncookies were used when this connection
1535bf6d304aSAndre Oppermann 	 * was established.
1536f72167f4SAndre Oppermann 	 */
1537bf6d304aSAndre Oppermann 	if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
1538e16fa5caSJohn-Mark Gurney 		to.to_tsecr -= tp->ts_offset;
1539d8951c8aSBjoern A. Zeeb 		if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks()))
1540f72167f4SAndre Oppermann 			to.to_tsecr = 0;
154110d20c84SMatt Macy 		else if (tp->t_flags & TF_PREVVALID &&
154210d20c84SMatt Macy 			 tp->t_badrxtwin != 0 && SEQ_LT(to.to_tsecr, tp->t_badrxtwin))
154310d20c84SMatt Macy 			cc_cong_signal(tp, th, CC_RTO_ERR);
1544bf6d304aSAndre Oppermann 	}
154507dacf03SAndre Oppermann 	/*
154697d8d152SAndre Oppermann 	 * Process options only when we get SYN/ACK back. The SYN case
154797d8d152SAndre Oppermann 	 * for incoming connections is handled in tcp_syncache.
15485396d0f8SAndre Oppermann 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN>
15495396d0f8SAndre Oppermann 	 * or <SYN,ACK>) segment itself is never scaled.
155097d8d152SAndre Oppermann 	 * XXX this is traditional behavior, may need to be cleaned up.
1551df8bae1dSRodney W. Grimes 	 */
15520a389eabSSimon L. B. Nielsen 	if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
1553464fcfbcSAndre Oppermann 		if ((to.to_flags & TOF_SCALE) &&
1554464fcfbcSAndre Oppermann 		    (tp->t_flags & TF_REQ_SCALE)) {
1555be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_SCALE;
155602a1a643SAndre Oppermann 			tp->snd_scale = to.to_wscale;
1557be2ac88cSJonathan Lemon 		}
15585396d0f8SAndre Oppermann 		/*
15595396d0f8SAndre Oppermann 		 * Initial send window.  It will be updated with
15605396d0f8SAndre Oppermann 		 * the next incoming segment to the scaled value.
15615396d0f8SAndre Oppermann 		 */
15625396d0f8SAndre Oppermann 		tp->snd_wnd = th->th_win;
1563be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_TS) {
1564be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_TSTMP;
1565be2ac88cSJonathan Lemon 			tp->ts_recent = to.to_tsval;
1566d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
1567be2ac88cSJonathan Lemon 		}
1568be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_MSS)
1569be2ac88cSJonathan Lemon 			tcp_mss(tp, to.to_mss);
15703529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
15713529149eSAndre Oppermann 		    (to.to_flags & TOF_SACKPERM) == 0)
15723529149eSAndre Oppermann 			tp->t_flags &= ~TF_SACK_PERMIT;
1573c560df6fSPatrick Kelsey 		if (IS_FASTOPEN(tp->t_flags)) {
1574a026a53aSMichael Tuexen 			if (to.to_flags & TOF_FASTOPEN) {
1575a026a53aSMichael Tuexen 				uint16_t mss;
1576a026a53aSMichael Tuexen 
1577a026a53aSMichael Tuexen 				if (to.to_flags & TOF_MSS)
1578a026a53aSMichael Tuexen 					mss = to.to_mss;
1579c560df6fSPatrick Kelsey 				else
1580a026a53aSMichael Tuexen 					if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0)
1581a026a53aSMichael Tuexen 						mss = TCP6_MSS;
1582a026a53aSMichael Tuexen 					else
1583a026a53aSMichael Tuexen 						mss = TCP_MSS;
1584a026a53aSMichael Tuexen 				tcp_fastopen_update_cache(tp, mss,
1585a026a53aSMichael Tuexen 				    to.to_tfo_len, to.to_tfo_cookie);
1586a026a53aSMichael Tuexen 			} else
1587c560df6fSPatrick Kelsey 				tcp_fastopen_disable_path(tp);
1588c560df6fSPatrick Kelsey 		}
15896d90faf3SPaul Saab 	}
15906d90faf3SPaul Saab 
1591df8bae1dSRodney W. Grimes 	/*
15924da9052aSMichael Tuexen 	 * If timestamps were negotiated during SYN/ACK they should
15934da9052aSMichael Tuexen 	 * appear on every segment during this session and vice versa.
15944da9052aSMichael Tuexen 	 */
15954da9052aSMichael Tuexen 	if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) {
15964da9052aSMichael Tuexen 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
15974da9052aSMichael Tuexen 			log(LOG_DEBUG, "%s; %s: Timestamp missing, "
15984da9052aSMichael Tuexen 			    "no action\n", s, __func__);
15994da9052aSMichael Tuexen 			free(s, M_TCPLOG);
16004da9052aSMichael Tuexen 		}
16014da9052aSMichael Tuexen 	}
16024da9052aSMichael Tuexen 	if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) {
16034da9052aSMichael Tuexen 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
16044da9052aSMichael Tuexen 			log(LOG_DEBUG, "%s; %s: Timestamp not expected, "
16054da9052aSMichael Tuexen 			    "no action\n", s, __func__);
16064da9052aSMichael Tuexen 			free(s, M_TCPLOG);
16074da9052aSMichael Tuexen 		}
16084da9052aSMichael Tuexen 	}
16094da9052aSMichael Tuexen 
16104da9052aSMichael Tuexen 	/*
1611df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
1612df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
1613df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
1614df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
1615df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
1616df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
1617df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
1618df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
1619df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
1620df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
1621df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
1622df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
1623a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
1624c5ad39b9SAndre Oppermann 	 * Since we check for TCPS_ESTABLISHED first, it can only
1625a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
1626df8bae1dSRodney W. Grimes 	 */
1627df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
1628c5ad39b9SAndre Oppermann 	    th->th_seq == tp->rcv_nxt &&
1629fb59c426SYoshinobu Inoue 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1630c5ad39b9SAndre Oppermann 	    tp->snd_nxt == tp->snd_max &&
1631c5ad39b9SAndre Oppermann 	    tiwin && tiwin == tp->snd_wnd &&
1632a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
1633c28440dbSRandall Stewart 	    SEGQ_EMPTY(tp) &&
16344741bfcbSPatrick Kelsey 	    ((to.to_flags & TOF_TS) == 0 ||
1635c5ad39b9SAndre Oppermann 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) {
1636df8bae1dSRodney W. Grimes 
1637df8bae1dSRodney W. Grimes 		/*
1638df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
1639df8bae1dSRodney W. Grimes 		 * record the timestamp.
1640a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
1641a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1642df8bae1dSRodney W. Grimes 		 */
1643be2ac88cSJonathan Lemon 		if ((to.to_flags & TOF_TS) != 0 &&
1644fb59c426SYoshinobu Inoue 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1645d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
1646a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
1647df8bae1dSRodney W. Grimes 		}
1648df8bae1dSRodney W. Grimes 
1649fb59c426SYoshinobu Inoue 		if (tlen == 0) {
1650fb59c426SYoshinobu Inoue 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
1651fb59c426SYoshinobu Inoue 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
1652dbc42409SLawrence Stewart 			    !IN_RECOVERY(tp->t_flags) &&
1653fc30a251SAndre Oppermann 			    (to.to_flags & TOF_SACK) == 0 &&
1654dbc42409SLawrence Stewart 			    TAILQ_EMPTY(&tp->snd_holes)) {
1655df8bae1dSRodney W. Grimes 				/*
1656e8949f74SAndre Oppermann 				 * This is a pure ack for outstanding data.
1657df8bae1dSRodney W. Grimes 				 */
165878b50714SRobert Watson 				TCPSTAT_INC(tcps_predack);
1659252ca428SRobert Watson 
16609b8b58e0SJonathan Lemon 				/*
166110d20c84SMatt Macy 				 * "bad retransmit" recovery without timestamps.
16629b8b58e0SJonathan Lemon 				 */
166310d20c84SMatt Macy 				if ((to.to_flags & TOF_TS) == 0 &&
166410d20c84SMatt Macy 				    tp->t_rxtshift == 1 &&
1665672dc4aeSJohn Baldwin 				    tp->t_flags & TF_PREVVALID &&
16666dfb8b31SJohn Baldwin 				    (int)(ticks - tp->t_badrxtwin) < 0) {
1667dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_RTO_ERR);
16689b8b58e0SJonathan Lemon 				}
1669fa55172bSMatthew Dillon 
1670fa55172bSMatthew Dillon 				/*
1671fa55172bSMatthew Dillon 				 * Recalculate the transmit timer / rtt.
1672fa55172bSMatthew Dillon 				 *
1673fa55172bSMatthew Dillon 				 * Some boxes send broken timestamp replies
1674fa55172bSMatthew Dillon 				 * during the SYN+ACK phase, ignore
1675fa55172bSMatthew Dillon 				 * timestamps of 0 or we could calculate a
1676fa55172bSMatthew Dillon 				 * huge RTT and blow up the retransmit timer.
1677fa55172bSMatthew Dillon 				 */
1678fa55172bSMatthew Dillon 				if ((to.to_flags & TOF_TS) != 0 &&
1679fa55172bSMatthew Dillon 				    to.to_tsecr) {
16803ac12506SJonathan T. Looney 					uint32_t t;
1681d8951c8aSBjoern A. Zeeb 
1682d8951c8aSBjoern A. Zeeb 					t = tcp_ts_getticks() - to.to_tsecr;
1683d8951c8aSBjoern A. Zeeb 					if (!tp->t_rttlow || tp->t_rttlow > t)
1684d8951c8aSBjoern A. Zeeb 						tp->t_rttlow = t;
1685a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
1686d8951c8aSBjoern A. Zeeb 					    TCP_TS_TO_TICKS(t) + 1);
1687fa55172bSMatthew Dillon 				} else if (tp->t_rtttime &&
1688fa55172bSMatthew Dillon 				    SEQ_GT(th->th_ack, tp->t_rtseq)) {
1689eaf80179SAndre Oppermann 					if (!tp->t_rttlow ||
1690eaf80179SAndre Oppermann 					    tp->t_rttlow > ticks - tp->t_rtttime)
1691eaf80179SAndre Oppermann 						tp->t_rttlow = ticks - tp->t_rtttime;
1692c068736aSJeffrey Hsu 					tcp_xmit_timer(tp,
1693c068736aSJeffrey Hsu 							ticks - tp->t_rtttime);
1694fa55172bSMatthew Dillon 				}
1695dbc42409SLawrence Stewart 				acked = BYTES_THIS_ACK(tp, th);
169639bc9de5SLawrence Stewart 
1697bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
169839bc9de5SLawrence Stewart 				/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
169939bc9de5SLawrence Stewart 				hhook_run_tcp_est_in(tp, th, &to);
1700bd79708dSJonathan T. Looney #endif
170139bc9de5SLawrence Stewart 
17024b7b743cSLawrence Stewart 				TCPSTAT_ADD(tcps_rcvackpack, nsegs);
170378b50714SRobert Watson 				TCPSTAT_ADD(tcps_rcvackbyte, acked);
1704df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
17059d11646dSJeffrey Hsu 				if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
17069d11646dSJeffrey Hsu 				    SEQ_LEQ(th->th_ack, tp->snd_recover))
17079d11646dSJeffrey Hsu 					tp->snd_recover = th->th_ack - 1;
1708dbc42409SLawrence Stewart 
1709dbc42409SLawrence Stewart 				/*
1710dbc42409SLawrence Stewart 				 * Let the congestion control algorithm update
1711dbc42409SLawrence Stewart 				 * congestion control related information. This
1712dbc42409SLawrence Stewart 				 * typically means increasing the congestion
1713dbc42409SLawrence Stewart 				 * window.
1714dbc42409SLawrence Stewart 				 */
17154b7b743cSLawrence Stewart 				cc_ack_received(tp, th, nsegs, CC_ACK);
1716dbc42409SLawrence Stewart 
17179d11646dSJeffrey Hsu 				tp->snd_una = th->th_ack;
17181645d090SJeff Roberson 				/*
1719e8949f74SAndre Oppermann 				 * Pull snd_wl2 up to prevent seq wrap relative
17201645d090SJeff Roberson 				 * to th_ack.
17211645d090SJeff Roberson 				 */
1722cb942153SJeffrey Hsu 				tp->snd_wl2 = th->th_ack;
1723b5addd85SJeffrey Hsu 				tp->t_dupacks = 0;
1724df8bae1dSRodney W. Grimes 				m_freem(m);
1725df8bae1dSRodney W. Grimes 
1726df8bae1dSRodney W. Grimes 				/*
1727df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
1728df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
1729df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
1730df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
1731df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
1732df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
1733df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
1734e8949f74SAndre Oppermann 				 */
17353c653157SHartmut Brandt #ifdef TCPDEBUG
17363c653157SHartmut Brandt 				if (so->so_options & SO_DEBUG)
17373c653157SHartmut Brandt 					tcp_trace(TA_INPUT, ostate, tp,
17383c653157SHartmut Brandt 					    (void *)tcp_saveipgen,
17393c653157SHartmut Brandt 					    &tcp_savetcp, 0);
17403c653157SHartmut Brandt #endif
17412b9c9984SGeorge V. Neville-Neil 				TCP_PROBE3(debug__input, tp, th, m);
1742df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
1743b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
1744b8152ba7SAndre Oppermann 				else if (!tcp_timer_active(tp, TT_PERSIST))
1745b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT,
1746b8152ba7SAndre Oppermann 						      tp->t_rxtcur);
1747df8bae1dSRodney W. Grimes 				sowwakeup(so);
1748cfa6009eSGleb Smirnoff 				if (sbavail(&so->so_snd))
174955bceb1eSRandall Stewart 					(void) tp->t_fb->tfb_tcp_output(tp);
1750a14c749fSJonathan Lemon 				goto check_delack;
1751df8bae1dSRodney W. Grimes 			}
1752fb59c426SYoshinobu Inoue 		} else if (th->th_ack == tp->snd_una &&
1753fb59c426SYoshinobu Inoue 		    tlen <= sbspace(&so->so_rcv)) {
17546741ecf5SAndre Oppermann 			int newsize = 0;	/* automatic sockbuf scaling */
17556741ecf5SAndre Oppermann 
1756df8bae1dSRodney W. Grimes 			/*
1757252ca428SRobert Watson 			 * This is a pure, in-sequence data packet with
1758252ca428SRobert Watson 			 * nothing on the reassembly queue and we have enough
1759252ca428SRobert Watson 			 * buffer space to take it.
1760df8bae1dSRodney W. Grimes 			 */
17616d90faf3SPaul Saab 			/* Clean receiver SACK report if present */
17623529149eSAndre Oppermann 			if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks)
17636d90faf3SPaul Saab 				tcp_clean_sackreport(tp);
176478b50714SRobert Watson 			TCPSTAT_INC(tcps_preddat);
1765fb59c426SYoshinobu Inoue 			tp->rcv_nxt += tlen;
17661645d090SJeff Roberson 			/*
17671645d090SJeff Roberson 			 * Pull snd_wl1 up to prevent seq wrap relative to
17681645d090SJeff Roberson 			 * th_seq.
17691645d090SJeff Roberson 			 */
177060ee3bb2SAndre Oppermann 			tp->snd_wl1 = th->th_seq;
17711645d090SJeff Roberson 			/*
17721645d090SJeff Roberson 			 * Pull rcv_up up to prevent seq wrap relative to
17731645d090SJeff Roberson 			 * rcv_nxt.
17741645d090SJeff Roberson 			 */
17751645d090SJeff Roberson 			tp->rcv_up = tp->rcv_nxt;
17764b7b743cSLawrence Stewart 			TCPSTAT_ADD(tcps_rcvpack, nsegs);
177778b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
17783c653157SHartmut Brandt #ifdef TCPDEBUG
17793c653157SHartmut Brandt 			if (so->so_options & SO_DEBUG)
17803c653157SHartmut Brandt 				tcp_trace(TA_INPUT, ostate, tp,
17813c653157SHartmut Brandt 				    (void *)tcp_saveipgen, &tcp_savetcp, 0);
17823c653157SHartmut Brandt #endif
17832b9c9984SGeorge V. Neville-Neil 			TCP_PROBE3(debug__input, tp, th, m);
17845d06879aSGeorge V. Neville-Neil 
1785e44c1887SSteven Hartland 			newsize = tcp_autorcvbuf(m, th, so, tp, tlen);
17866741ecf5SAndre Oppermann 
17876741ecf5SAndre Oppermann 			/* Add data to socket buffer. */
17881e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
1789c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1790c1c36a2cSMike Silbersack 				m_freem(m);
1791c1c36a2cSMike Silbersack 			} else {
17926741ecf5SAndre Oppermann 				/*
17936741ecf5SAndre Oppermann 				 * Set new socket buffer size.
17946741ecf5SAndre Oppermann 				 * Give up when limit is reached.
17956741ecf5SAndre Oppermann 				 */
17966741ecf5SAndre Oppermann 				if (newsize)
17976741ecf5SAndre Oppermann 					if (!sbreserve_locked(&so->so_rcv,
17986c8286e4SRobert Watson 					    newsize, so, NULL))
17996741ecf5SAndre Oppermann 						so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
1800fb59c426SYoshinobu Inoue 				m_adj(m, drop_hdrlen);	/* delayed header drop */
1801651e4e6aSGleb Smirnoff 				sbappendstream_locked(&so->so_rcv, m, 0);
1802c1c36a2cSMike Silbersack 			}
1803e8949f74SAndre Oppermann 			/* NB: sorwakeup_locked() does an implicit unlock. */
18041e4d7da7SRobert Watson 			sorwakeup_locked(so);
1805c1e5a6e5SAndre Oppermann 			if (DELAY_ACK(tp, tlen)) {
18063bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
1807f498eeeeSDavid Greenman 			} else {
1808e612a582SDavid Greenman 				tp->t_flags |= TF_ACKNOW;
180955bceb1eSRandall Stewart 				tp->t_fb->tfb_tcp_output(tp);
1810e612a582SDavid Greenman 			}
1811a14c749fSJonathan Lemon 			goto check_delack;
1812df8bae1dSRodney W. Grimes 		}
1813df8bae1dSRodney W. Grimes 	}
1814df8bae1dSRodney W. Grimes 
1815df8bae1dSRodney W. Grimes 	/*
1816df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
1817df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
1818df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
1819df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
1820df8bae1dSRodney W. Grimes 	 */
1821df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
1822df8bae1dSRodney W. Grimes 	if (win < 0)
1823df8bae1dSRodney W. Grimes 		win = 0;
182466e39adcSJohn Polstra 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1825df8bae1dSRodney W. Grimes 
1826df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1827df8bae1dSRodney W. Grimes 
1828df8bae1dSRodney W. Grimes 	/*
1829764d8cefSBill Fenner 	 * If the state is SYN_RECEIVED:
1830764d8cefSBill Fenner 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
1831764d8cefSBill Fenner 	 */
1832764d8cefSBill Fenner 	case TCPS_SYN_RECEIVED:
1833fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1834fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1835a57815efSBosko Milekic 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1836a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
1837a57815efSBosko Milekic 				goto dropwithreset;
1838a57815efSBosko Milekic 		}
183968bd7ed1SJonathan T. Looney 		if (IS_FASTOPEN(tp->t_flags)) {
1840281a0fd4SPatrick Kelsey 			/*
1841281a0fd4SPatrick Kelsey 			 * When a TFO connection is in SYN_RECEIVED, the
1842281a0fd4SPatrick Kelsey 			 * only valid packets are the initial SYN, a
1843281a0fd4SPatrick Kelsey 			 * retransmit/copy of the initial SYN (possibly with
1844281a0fd4SPatrick Kelsey 			 * a subset of the original data), a valid ACK, a
1845281a0fd4SPatrick Kelsey 			 * FIN, or a RST.
1846281a0fd4SPatrick Kelsey 			 */
1847281a0fd4SPatrick Kelsey 			if ((thflags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) {
1848281a0fd4SPatrick Kelsey 				rstreason = BANDLIM_RST_OPENPORT;
1849281a0fd4SPatrick Kelsey 				goto dropwithreset;
1850281a0fd4SPatrick Kelsey 			} else if (thflags & TH_SYN) {
1851281a0fd4SPatrick Kelsey 				/* non-initial SYN is ignored */
1852281a0fd4SPatrick Kelsey 				if ((tcp_timer_active(tp, TT_DELACK) ||
1853281a0fd4SPatrick Kelsey 				     tcp_timer_active(tp, TT_REXMT)))
1854281a0fd4SPatrick Kelsey 					goto drop;
1855281a0fd4SPatrick Kelsey 			} else if (!(thflags & (TH_ACK|TH_FIN|TH_RST))) {
1856281a0fd4SPatrick Kelsey 				goto drop;
1857281a0fd4SPatrick Kelsey 			}
1858281a0fd4SPatrick Kelsey 		}
1859764d8cefSBill Fenner 		break;
1860764d8cefSBill Fenner 
1861764d8cefSBill Fenner 	/*
1862df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
186398732609SMichael Tuexen 	 *	if seg contains a RST with valid ACK (SEQ.ACK has already
186498732609SMichael Tuexen 	 *	    been verified), then drop the connection.
186598732609SMichael Tuexen 	 *	if seg contains a RST without an ACK, drop the seg.
186698732609SMichael Tuexen 	 *	if seg does not contain SYN, then drop the seg.
1867df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
1868df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
1869df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
1870f2512ba1SRui Paulo 	 *	if seg contains an ECE and ECN support is enabled, the stream
1871f2512ba1SRui Paulo 	 *	    is ECN capable.
1872df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1873df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
1874df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
1875df8bae1dSRodney W. Grimes 	 */
1876df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
187757f60867SMark Johnston 		if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) {
1878d9fae5abSAndriy Gapon 			TCP_PROBE5(connect__refused, NULL, tp,
18792b9c9984SGeorge V. Neville-Neil 			    m, tp, th);
1880df8bae1dSRodney W. Grimes 			tp = tcp_drop(tp, ECONNREFUSED);
188157f60867SMark Johnston 		}
18820ca3f933SAndre Oppermann 		if (thflags & TH_RST)
1883df8bae1dSRodney W. Grimes 			goto drop;
18840ca3f933SAndre Oppermann 		if (!(thflags & TH_SYN))
1885df8bae1dSRodney W. Grimes 			goto drop;
1886464fcfbcSAndre Oppermann 
1887fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
1888df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
1889fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1890c560df6fSPatrick Kelsey 			int tfo_partial_ack = 0;
1891c560df6fSPatrick Kelsey 
189278b50714SRobert Watson 			TCPSTAT_INC(tcps_connects);
1893845799c1SAndras Olah 			soisconnected(so);
1894c488362eSRobert Watson #ifdef MAC
189530d239bcSRobert Watson 			mac_socketpeer_set_from_mbuf(m, so);
1896c488362eSRobert Watson #endif
1897845799c1SAndras Olah 			/* Do window scaling on this connection? */
1898845799c1SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1899845799c1SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
1900845799c1SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
1901845799c1SAndras Olah 			}
19023ac12506SJonathan T. Looney 			tp->rcv_adv += min(tp->rcv_wnd,
1903766282cbSJohn Baldwin 			    TCP_MAXWIN << tp->rcv_scale);
1904a0292f23SGarrett Wollman 			tp->snd_una++;		/* SYN is acked */
1905a0292f23SGarrett Wollman 			/*
1906c560df6fSPatrick Kelsey 			 * If not all the data that was sent in the TFO SYN
1907c560df6fSPatrick Kelsey 			 * has been acked, resend the remainder right away.
1908c560df6fSPatrick Kelsey 			 */
1909c560df6fSPatrick Kelsey 			if (IS_FASTOPEN(tp->t_flags) &&
1910c560df6fSPatrick Kelsey 			    (tp->snd_una != tp->snd_max)) {
1911c560df6fSPatrick Kelsey 				tp->snd_nxt = th->th_ack;
1912c560df6fSPatrick Kelsey 				tfo_partial_ack = 1;
1913c560df6fSPatrick Kelsey 			}
1914c560df6fSPatrick Kelsey 			/*
1915a0292f23SGarrett Wollman 			 * If there's data, delay ACK; if there's also a FIN
1916a0292f23SGarrett Wollman 			 * ACKNOW will be turned on later.
1917a0292f23SGarrett Wollman 			 */
1918c560df6fSPatrick Kelsey 			if (DELAY_ACK(tp, tlen) && tlen != 0 && !tfo_partial_ack)
1919b8152ba7SAndre Oppermann 				tcp_timer_activate(tp, TT_DELACK,
1920b8152ba7SAndre Oppermann 				    tcp_delacktime);
1921a0292f23SGarrett Wollman 			else
1922a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
1923f2512ba1SRui Paulo 
1924bf7fcdb1SMichael Tuexen 			if (((thflags & (TH_CWR | TH_ECE)) == TH_ECE) &&
1925bf7fcdb1SMichael Tuexen 			    V_tcp_do_ecn) {
1926f2512ba1SRui Paulo 				tp->t_flags |= TF_ECN_PERMIT;
192778b50714SRobert Watson 				TCPSTAT_INC(tcps_ecn_shs);
1928f2512ba1SRui Paulo 			}
1929f2512ba1SRui Paulo 
1930a0292f23SGarrett Wollman 			/*
1931a0292f23SGarrett Wollman 			 * Received <SYN,ACK> in SYN_SENT[*] state.
1932a0292f23SGarrett Wollman 			 * Transitions:
1933a0292f23SGarrett Wollman 			 *	SYN_SENT  --> ESTABLISHED
1934a0292f23SGarrett Wollman 			 *	SYN_SENT* --> FIN_WAIT_1
1935a0292f23SGarrett Wollman 			 */
19369b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
1937a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDFIN) {
193857f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_1);
1939a0292f23SGarrett Wollman 				tp->t_flags &= ~TF_NEEDFIN;
1940fb59c426SYoshinobu Inoue 				thflags &= ~TH_SYN;
19417ff19458SPaul Traina 			} else {
194257f60867SMark Johnston 				tcp_state_change(tp, TCPS_ESTABLISHED);
1943d9fae5abSAndriy Gapon 				TCP_PROBE5(connect__established, NULL, tp,
19442b9c9984SGeorge V. Neville-Neil 				    m, tp, th);
1945dbc42409SLawrence Stewart 				cc_conn_init(tp);
19469077f387SGleb Smirnoff 				tcp_timer_activate(tp, TT_KEEP,
19479077f387SGleb Smirnoff 				    TP_KEEPIDLE(tp));
19487ff19458SPaul Traina 			}
1949a0292f23SGarrett Wollman 		} else {
1950a0292f23SGarrett Wollman 			/*
1951c068736aSJeffrey Hsu 			 * Received initial SYN in SYN-SENT[*] state =>
1952153edc50SHiren Panchasara 			 * simultaneous open.
1953c068736aSJeffrey Hsu 			 * If it succeeds, connection is * half-synchronized.
1954c068736aSJeffrey Hsu 			 * Otherwise, do 3-way handshake:
1955a0292f23SGarrett Wollman 			 *        SYN-SENT -> SYN-RECEIVED
1956a0292f23SGarrett Wollman 			 *        SYN-SENT* -> SYN-RECEIVED*
1957a0292f23SGarrett Wollman 			 */
19584b8e98d6SQing Li 			tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
1959b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
196057f60867SMark Johnston 			tcp_state_change(tp, TCPS_SYN_RECEIVED);
1961a0292f23SGarrett Wollman 		}
1962df8bae1dSRodney W. Grimes 
19638501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
19647cfc6904SRobert Watson 
1965df8bae1dSRodney W. Grimes 		/*
1966fb59c426SYoshinobu Inoue 		 * Advance th->th_seq to correspond to first data byte.
1967df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
1968df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
1969df8bae1dSRodney W. Grimes 		 */
1970fb59c426SYoshinobu Inoue 		th->th_seq++;
1971fb59c426SYoshinobu Inoue 		if (tlen > tp->rcv_wnd) {
1972fb59c426SYoshinobu Inoue 			todrop = tlen - tp->rcv_wnd;
1973df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
1974fb59c426SYoshinobu Inoue 			tlen = tp->rcv_wnd;
1975fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
197678b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpackafterwin);
197778b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
1978df8bae1dSRodney W. Grimes 		}
1979fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
1980fb59c426SYoshinobu Inoue 		tp->rcv_up = th->th_seq;
1981a0292f23SGarrett Wollman 		/*
1982a0292f23SGarrett Wollman 		 * Client side of transaction: already sent SYN and data.
1983a0292f23SGarrett Wollman 		 * If the remote host used T/TCP to validate the SYN,
1984a0292f23SGarrett Wollman 		 * our data will be ACK'd; if so, enter normal data segment
1985a0292f23SGarrett Wollman 		 * processing in the middle of step 5, ack processing.
1986a0292f23SGarrett Wollman 		 * Otherwise, goto step 6.
1987a0292f23SGarrett Wollman 		 */
1988fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK)
1989a0292f23SGarrett Wollman 			goto process_ACK;
1990c068736aSJeffrey Hsu 
1991df8bae1dSRodney W. Grimes 		goto step6;
1992c068736aSJeffrey Hsu 
1993a0292f23SGarrett Wollman 	/*
1994a0292f23SGarrett Wollman 	 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
1995c94c54e4SAndre Oppermann 	 *      do normal processing.
1996a0292f23SGarrett Wollman 	 *
1997c94c54e4SAndre Oppermann 	 * NB: Leftover from RFC1644 T/TCP.  Cases to be reused later.
1998a0292f23SGarrett Wollman 	 */
1999a0292f23SGarrett Wollman 	case TCPS_LAST_ACK:
2000a0292f23SGarrett Wollman 	case TCPS_CLOSING:
2001a0292f23SGarrett Wollman 		break;  /* continue normal processing */
2002df8bae1dSRodney W. Grimes 	}
2003df8bae1dSRodney W. Grimes 
2004df8bae1dSRodney W. Grimes 	/*
2005df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
200680ab7c0eSGarrett Wollman 	 * First check the RST flag and sequence number since reset segments
200780ab7c0eSGarrett Wollman 	 * are exempt from the timestamp and connection count tests.  This
200880ab7c0eSGarrett Wollman 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
200980ab7c0eSGarrett Wollman 	 * below which allowed reset segments in half the sequence space
201080ab7c0eSGarrett Wollman 	 * to fall though and be processed (which gives forged reset
201180ab7c0eSGarrett Wollman 	 * segments with a random sequence number a 50 percent chance of
201280ab7c0eSGarrett Wollman 	 * killing a connection).
201380ab7c0eSGarrett Wollman 	 * Then check timestamp, if present.
2014a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
2015df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
2016df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
2017df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
201880ab7c0eSGarrett Wollman 	 */
2019fb59c426SYoshinobu Inoue 	if (thflags & TH_RST) {
20203220a212SGleb Smirnoff 		/*
20213220a212SGleb Smirnoff 		 * RFC5961 Section 3.2
20223220a212SGleb Smirnoff 		 *
20233220a212SGleb Smirnoff 		 * - RST drops connection only if SEG.SEQ == RCV.NXT.
20243220a212SGleb Smirnoff 		 * - If RST is in window, we send challenge ACK.
20253220a212SGleb Smirnoff 		 *
20263220a212SGleb Smirnoff 		 * Note: to take into account delayed ACKs, we should
20273220a212SGleb Smirnoff 		 *   test against last_ack_sent instead of rcv_nxt.
20283220a212SGleb Smirnoff 		 * Note 2: we handle special case of closed window, not
20293220a212SGleb Smirnoff 		 *   covered by the RFC.
20303220a212SGleb Smirnoff 		 */
20313220a212SGleb Smirnoff 		if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
20323220a212SGleb Smirnoff 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
20333220a212SGleb Smirnoff 		    (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) {
203480ab7c0eSGarrett Wollman 
20353220a212SGleb Smirnoff 			KASSERT(tp->t_state != TCPS_SYN_SENT,
20363220a212SGleb Smirnoff 			    ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p",
20373220a212SGleb Smirnoff 			    __func__, th, tp));
20383220a212SGleb Smirnoff 
20393220a212SGleb Smirnoff 			if (V_tcp_insecure_rst ||
20403220a212SGleb Smirnoff 			    tp->last_ack_sent == th->th_seq) {
20413220a212SGleb Smirnoff 				TCPSTAT_INC(tcps_drops);
20423220a212SGleb Smirnoff 				/* Drop the connection. */
20433220a212SGleb Smirnoff 				switch (tp->t_state) {
204480ab7c0eSGarrett Wollman 				case TCPS_SYN_RECEIVED:
204580ab7c0eSGarrett Wollman 					so->so_error = ECONNREFUSED;
204680ab7c0eSGarrett Wollman 					goto close;
204780ab7c0eSGarrett Wollman 				case TCPS_ESTABLISHED:
204880ab7c0eSGarrett Wollman 				case TCPS_FIN_WAIT_1:
204980ab7c0eSGarrett Wollman 				case TCPS_FIN_WAIT_2:
205080ab7c0eSGarrett Wollman 				case TCPS_CLOSE_WAIT:
20516779a1a1SMichael Tuexen 				case TCPS_CLOSING:
20526779a1a1SMichael Tuexen 				case TCPS_LAST_ACK:
205380ab7c0eSGarrett Wollman 					so->so_error = ECONNRESET;
205480ab7c0eSGarrett Wollman 				close:
20553220a212SGleb Smirnoff 					/* FALLTHROUGH */
20563220a212SGleb Smirnoff 				default:
205780ab7c0eSGarrett Wollman 					tp = tcp_close(tp);
20583220a212SGleb Smirnoff 				}
20593220a212SGleb Smirnoff 			} else {
20603220a212SGleb Smirnoff 				TCPSTAT_INC(tcps_badrst);
20613220a212SGleb Smirnoff 				/* Send challenge ACK. */
20623220a212SGleb Smirnoff 				tcp_respond(tp, mtod(m, void *), th, m,
20633220a212SGleb Smirnoff 				    tp->rcv_nxt, tp->snd_nxt, TH_ACK);
20643220a212SGleb Smirnoff 				tp->last_ack_sent = tp->rcv_nxt;
20653220a212SGleb Smirnoff 				m = NULL;
20663220a212SGleb Smirnoff 			}
20673220a212SGleb Smirnoff 		}
20683220a212SGleb Smirnoff 		goto drop;
20693220a212SGleb Smirnoff 	}
207080ab7c0eSGarrett Wollman 
20713220a212SGleb Smirnoff 	/*
20723220a212SGleb Smirnoff 	 * RFC5961 Section 4.2
20733220a212SGleb Smirnoff 	 * Send challenge ACK for any SYN in synchronized state.
20743220a212SGleb Smirnoff 	 */
2075281a0fd4SPatrick Kelsey 	if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT &&
2076281a0fd4SPatrick Kelsey 	    tp->t_state != TCPS_SYN_RECEIVED) {
20773220a212SGleb Smirnoff 		TCPSTAT_INC(tcps_badsyn);
20783220a212SGleb Smirnoff 		if (V_tcp_insecure_syn &&
20793220a212SGleb Smirnoff 		    SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
20803220a212SGleb Smirnoff 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
20813220a212SGleb Smirnoff 			tp = tcp_drop(tp, ECONNRESET);
20823220a212SGleb Smirnoff 			rstreason = BANDLIM_UNLIMITED;
20833220a212SGleb Smirnoff 		} else {
20843220a212SGleb Smirnoff 			/* Send challenge ACK. */
20853220a212SGleb Smirnoff 			tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt,
20863220a212SGleb Smirnoff 			    tp->snd_nxt, TH_ACK);
20873220a212SGleb Smirnoff 			tp->last_ack_sent = tp->rcv_nxt;
20883220a212SGleb Smirnoff 			m = NULL;
208980ab7c0eSGarrett Wollman 		}
209080ab7c0eSGarrett Wollman 		goto drop;
209180ab7c0eSGarrett Wollman 	}
209280ab7c0eSGarrett Wollman 
209380ab7c0eSGarrett Wollman 	/*
2094df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
2095df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
2096df8bae1dSRodney W. Grimes 	 */
2097be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
209880ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
2099df8bae1dSRodney W. Grimes 
2100df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
2101d8951c8aSBjoern A. Zeeb 		if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) {
2102df8bae1dSRodney W. Grimes 			/*
2103df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
2104df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
2105df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
2106df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
2107df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
2108df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
2109df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
2110df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
2111df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
2112df8bae1dSRodney W. Grimes 			 */
2113df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
2114df8bae1dSRodney W. Grimes 		} else {
211578b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
211678b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, tlen);
211778b50714SRobert Watson 			TCPSTAT_INC(tcps_pawsdrop);
211807fd333dSMatthew Dillon 			if (tlen)
2119df8bae1dSRodney W. Grimes 				goto dropafterack;
21201ab4789dSMatthew Dillon 			goto drop;
21211ab4789dSMatthew Dillon 		}
2122df8bae1dSRodney W. Grimes 	}
2123df8bae1dSRodney W. Grimes 
2124a0292f23SGarrett Wollman 	/*
212580ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
212680ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
212780ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
212880ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
212980ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
213080ab7c0eSGarrett Wollman 	 */
2131a57815efSBosko Milekic 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
2132a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2133a57815efSBosko Milekic 		goto dropwithreset;
2134a57815efSBosko Milekic 	}
213580ab7c0eSGarrett Wollman 
2136fb59c426SYoshinobu Inoue 	todrop = tp->rcv_nxt - th->th_seq;
2137df8bae1dSRodney W. Grimes 	if (todrop > 0) {
2138831ad37eSXin LI 		if (thflags & TH_SYN) {
2139fb59c426SYoshinobu Inoue 			thflags &= ~TH_SYN;
2140fb59c426SYoshinobu Inoue 			th->th_seq++;
2141fb59c426SYoshinobu Inoue 			if (th->th_urp > 1)
2142fb59c426SYoshinobu Inoue 				th->th_urp--;
2143df8bae1dSRodney W. Grimes 			else
2144fb59c426SYoshinobu Inoue 				thflags &= ~TH_URG;
2145df8bae1dSRodney W. Grimes 			todrop--;
2146df8bae1dSRodney W. Grimes 		}
2147df8bae1dSRodney W. Grimes 		/*
2148dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
2149df8bae1dSRodney W. Grimes 		 */
2150fb59c426SYoshinobu Inoue 		if (todrop > tlen
2151fb59c426SYoshinobu Inoue 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
2152dac20301SGarrett Wollman 			/*
2153dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
2154dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
2155dac20301SGarrett Wollman 			 * of sequence; drop it.
2156dac20301SGarrett Wollman 			 */
2157fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
2158dac20301SGarrett Wollman 
2159df8bae1dSRodney W. Grimes 			/*
2160dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
2161dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
2162df8bae1dSRodney W. Grimes 			 */
2163dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
2164fb59c426SYoshinobu Inoue 			todrop = tlen;
216578b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
216678b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, todrop);
2167df8bae1dSRodney W. Grimes 		} else {
216878b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpartduppack);
216978b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop);
2170df8bae1dSRodney W. Grimes 		}
21715acfd95cSMichael Tuexen 		/*
21725acfd95cSMichael Tuexen 		 * DSACK - add SACK block for dropped range
21735acfd95cSMichael Tuexen 		 */
21745acfd95cSMichael Tuexen 		if (tp->t_flags & TF_SACK_PERMIT) {
2175ecc5b1d1SMichael Tuexen 			tcp_update_sack_list(tp, th->th_seq,
2176ecc5b1d1SMichael Tuexen 			    th->th_seq + todrop);
21775acfd95cSMichael Tuexen 			/*
21785acfd95cSMichael Tuexen 			 * ACK now, as the next in-sequence segment
21795acfd95cSMichael Tuexen 			 * will clear the DSACK block again
21805acfd95cSMichael Tuexen 			 */
21815acfd95cSMichael Tuexen 			tp->t_flags |= TF_ACKNOW;
21825acfd95cSMichael Tuexen 		}
2183fb59c426SYoshinobu Inoue 		drop_hdrlen += todrop;	/* drop from the top afterwards */
2184fb59c426SYoshinobu Inoue 		th->th_seq += todrop;
2185fb59c426SYoshinobu Inoue 		tlen -= todrop;
2186fb59c426SYoshinobu Inoue 		if (th->th_urp > todrop)
2187fb59c426SYoshinobu Inoue 			th->th_urp -= todrop;
2188df8bae1dSRodney W. Grimes 		else {
2189fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;
2190fb59c426SYoshinobu Inoue 			th->th_urp = 0;
2191df8bae1dSRodney W. Grimes 		}
2192df8bae1dSRodney W. Grimes 	}
2193df8bae1dSRodney W. Grimes 
2194df8bae1dSRodney W. Grimes 	/*
2195df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
2196df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
2197df8bae1dSRodney W. Grimes 	 */
2198df8bae1dSRodney W. Grimes 	if ((so->so_state & SS_NOFDREF) &&
2199fb59c426SYoshinobu Inoue 	    tp->t_state > TCPS_CLOSE_WAIT && tlen) {
220007dacf03SAndre Oppermann 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
220107dacf03SAndre Oppermann 			log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data "
220207dacf03SAndre Oppermann 			    "after socket was closed, "
220307dacf03SAndre Oppermann 			    "sending RST and removing tcpcb\n",
2204e31d8aa3SMike Silbersack 			    s, __func__, tcpstates[tp->t_state], tlen);
2205773673c1SAndre Oppermann 			free(s, M_TCPLOG);
2206773673c1SAndre Oppermann 		}
2207df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
220878b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvafterclose);
2209a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
2210df8bae1dSRodney W. Grimes 		goto dropwithreset;
2211df8bae1dSRodney W. Grimes 	}
2212df8bae1dSRodney W. Grimes 
2213df8bae1dSRodney W. Grimes 	/*
2214df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
2215df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
2216df8bae1dSRodney W. Grimes 	 */
2217fb59c426SYoshinobu Inoue 	todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
2218df8bae1dSRodney W. Grimes 	if (todrop > 0) {
221978b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvpackafterwin);
2220fb59c426SYoshinobu Inoue 		if (todrop >= tlen) {
222178b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen);
2222df8bae1dSRodney W. Grimes 			/*
2223df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
2224df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
2225df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
2226df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
2227df8bae1dSRodney W. Grimes 			 * and ack.
2228df8bae1dSRodney W. Grimes 			 */
2229fb59c426SYoshinobu Inoue 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
2230df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
223178b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvwinprobe);
2232df8bae1dSRodney W. Grimes 			} else
2233df8bae1dSRodney W. Grimes 				goto dropafterack;
2234df8bae1dSRodney W. Grimes 		} else
223578b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2236df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
2237fb59c426SYoshinobu Inoue 		tlen -= todrop;
2238fb59c426SYoshinobu Inoue 		thflags &= ~(TH_PUSH|TH_FIN);
2239df8bae1dSRodney W. Grimes 	}
2240df8bae1dSRodney W. Grimes 
2241df8bae1dSRodney W. Grimes 	/*
2242df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
2243df8bae1dSRodney W. Grimes 	 * record its timestamp.
2244cf09195bSPaul Saab 	 * NOTE:
2245cf09195bSPaul Saab 	 * 1) That the test incorporates suggestions from the latest
2246a0292f23SGarrett Wollman 	 *    proposal of the tcplw@cray.com list (Braden 1993/04/26).
2247cf09195bSPaul Saab 	 * 2) That updating only on newer timestamps interferes with
2248cf09195bSPaul Saab 	 *    our earlier PAWS tests, so this check should be solely
2249cf09195bSPaul Saab 	 *    predicated on the sequence space of this segment.
2250cf09195bSPaul Saab 	 * 3) That we modify the segment boundary check to be
2251cf09195bSPaul Saab 	 *        Last.ACK.Sent <= SEG.SEQ + SEG.Len
2252cf09195bSPaul Saab 	 *    instead of RFC1323's
2253cf09195bSPaul Saab 	 *        Last.ACK.Sent < SEG.SEQ + SEG.Len,
2254cf09195bSPaul Saab 	 *    This modified check allows us to overcome RFC1323's
2255cf09195bSPaul Saab 	 *    limitations as described in Stevens TCP/IP Illustrated
2256cf09195bSPaul Saab 	 *    Vol. 2 p.869. In such cases, we can still calculate the
2257cf09195bSPaul Saab 	 *    RTT correctly when RCV.NXT == Last.ACK.Sent.
2258df8bae1dSRodney W. Grimes 	 */
2259be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 &&
2260cf09195bSPaul Saab 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
2261cf09195bSPaul Saab 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
2262cf09195bSPaul Saab 		((thflags & (TH_SYN|TH_FIN)) != 0))) {
2263d8951c8aSBjoern A. Zeeb 		tp->ts_recent_age = tcp_ts_getticks();
2264a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
2265df8bae1dSRodney W. Grimes 	}
2266df8bae1dSRodney W. Grimes 
2267df8bae1dSRodney W. Grimes 	/*
2268a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
2269a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
2270a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
2271a0292f23SGarrett Wollman 	 */
2272fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) == 0) {
2273a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
2274281a0fd4SPatrick Kelsey 		    (tp->t_flags & TF_NEEDSYN)) {
2275281a0fd4SPatrick Kelsey 			if (tp->t_state == TCPS_SYN_RECEIVED &&
227668bd7ed1SJonathan T. Looney 			    IS_FASTOPEN(tp->t_flags)) {
2277281a0fd4SPatrick Kelsey 				tp->snd_wnd = tiwin;
2278281a0fd4SPatrick Kelsey 				cc_conn_init(tp);
2279281a0fd4SPatrick Kelsey 			}
2280a0292f23SGarrett Wollman 			goto step6;
2281281a0fd4SPatrick Kelsey 		} else if (tp->t_flags & TF_ACKNOW)
22825e1aa279SDavid Malone 			goto dropafterack;
2283a0292f23SGarrett Wollman 		else
2284a0292f23SGarrett Wollman 			goto drop;
2285a0292f23SGarrett Wollman 	}
2286df8bae1dSRodney W. Grimes 
2287df8bae1dSRodney W. Grimes 	/*
2288df8bae1dSRodney W. Grimes 	 * Ack processing.
2289df8bae1dSRodney W. Grimes 	 */
2290df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2291df8bae1dSRodney W. Grimes 
2292df8bae1dSRodney W. Grimes 	/*
2293764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
2294764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
2295764d8cefSBill Fenner 	 * The ACK was checked above.
2296df8bae1dSRodney W. Grimes 	 */
2297df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2298a0292f23SGarrett Wollman 
229978b50714SRobert Watson 		TCPSTAT_INC(tcps_connects);
2300df8bae1dSRodney W. Grimes 		soisconnected(so);
2301df8bae1dSRodney W. Grimes 		/* Do window scaling? */
2302df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2303df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
2304df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
2305df8bae1dSRodney W. Grimes 		}
2306116ef4d6SMichael Tuexen 		tp->snd_wnd = tiwin;
2307a0292f23SGarrett Wollman 		/*
2308a0292f23SGarrett Wollman 		 * Make transitions:
2309a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
2310a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
2311a0292f23SGarrett Wollman 		 */
23129b8b58e0SJonathan Lemon 		tp->t_starttime = ticks;
231318a75309SPatrick Kelsey 		if (IS_FASTOPEN(tp->t_flags) && tp->t_tfo_pending) {
2314281a0fd4SPatrick Kelsey 			tcp_fastopen_decrement_counter(tp->t_tfo_pending);
2315281a0fd4SPatrick Kelsey 			tp->t_tfo_pending = NULL;
2316281a0fd4SPatrick Kelsey 
2317281a0fd4SPatrick Kelsey 			/*
2318281a0fd4SPatrick Kelsey 			 * Account for the ACK of our SYN prior to
2319281a0fd4SPatrick Kelsey 			 * regular ACK processing below.
2320281a0fd4SPatrick Kelsey 			 */
2321281a0fd4SPatrick Kelsey 			tp->snd_una++;
2322281a0fd4SPatrick Kelsey 		}
23238db239dcSMichael Tuexen 		if (tp->t_flags & TF_NEEDFIN) {
23248db239dcSMichael Tuexen 			tcp_state_change(tp, TCPS_FIN_WAIT_1);
23258db239dcSMichael Tuexen 			tp->t_flags &= ~TF_NEEDFIN;
23268db239dcSMichael Tuexen 		} else {
23278db239dcSMichael Tuexen 			tcp_state_change(tp, TCPS_ESTABLISHED);
23288db239dcSMichael Tuexen 			TCP_PROBE5(accept__established, NULL, tp,
23298db239dcSMichael Tuexen 			    m, tp, th);
2330281a0fd4SPatrick Kelsey 			/*
2331281a0fd4SPatrick Kelsey 			 * TFO connections call cc_conn_init() during SYN
2332281a0fd4SPatrick Kelsey 			 * processing.  Calling it again here for such
2333281a0fd4SPatrick Kelsey 			 * connections is not harmless as it would undo the
2334281a0fd4SPatrick Kelsey 			 * snd_cwnd reduction that occurs when a TFO SYN|ACK
2335281a0fd4SPatrick Kelsey 			 * is retransmitted.
2336281a0fd4SPatrick Kelsey 			 */
233768bd7ed1SJonathan T. Looney 			if (!IS_FASTOPEN(tp->t_flags))
2338dbc42409SLawrence Stewart 				cc_conn_init(tp);
23399077f387SGleb Smirnoff 			tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp));
23407ff19458SPaul Traina 		}
2341a0292f23SGarrett Wollman 		/*
2342a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
2343a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
2344a0292f23SGarrett Wollman 		 */
2345fb59c426SYoshinobu Inoue 		if (tlen == 0 && (thflags & TH_FIN) == 0)
2346c28440dbSRandall Stewart 			(void) tcp_reass(tp, (struct tcphdr *)0, NULL, 0,
2347a0292f23SGarrett Wollman 			    (struct mbuf *)0);
234860ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq - 1;
234993b0017fSPhilippe Charnier 		/* FALLTHROUGH */
2350df8bae1dSRodney W. Grimes 
2351df8bae1dSRodney W. Grimes 	/*
2352df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
2353df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
2354fb59c426SYoshinobu Inoue 	 *	tp->snd_una < th->th_ack <= tp->snd_max
2355fb59c426SYoshinobu Inoue 	 * then advance tp->snd_una to th->th_ack and drop
2356df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
2357df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
2358df8bae1dSRodney W. Grimes 	 */
2359df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
2360df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
2361df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
2362df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
2363df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
2364df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
23655a53ca16SPaul Saab 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
236678b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvacktoomuch);
23675a53ca16SPaul Saab 			goto dropafterack;
23685a53ca16SPaul Saab 		}
23693529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
2370fc30a251SAndre Oppermann 		    ((to.to_flags & TOF_SACK) ||
2371fc30a251SAndre Oppermann 		     !TAILQ_EMPTY(&tp->snd_holes)))
2372021eaf79SHiren Panchasara 			sack_changed = tcp_sack_doack(tp, &to, th->th_ack);
237312eeb81fSHiren Panchasara 		else
237412eeb81fSHiren Panchasara 			/*
237512eeb81fSHiren Panchasara 			 * Reset the value so that previous (valid) value
237612eeb81fSHiren Panchasara 			 * from the last ack with SACK doesn't get used.
237712eeb81fSHiren Panchasara 			 */
237812eeb81fSHiren Panchasara 			tp->sackhint.sacked_bytes = 0;
237939bc9de5SLawrence Stewart 
2380bd79708dSJonathan T. Looney #ifdef TCP_HHOOK
238139bc9de5SLawrence Stewart 		/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
238239bc9de5SLawrence Stewart 		hhook_run_tcp_est_in(tp, th, &to);
2383bd79708dSJonathan T. Looney #endif
238439bc9de5SLawrence Stewart 
2385fb59c426SYoshinobu Inoue 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
23860c39d38dSGleb Smirnoff 			u_int maxseg;
23870c39d38dSGleb Smirnoff 
23880c39d38dSGleb Smirnoff 			maxseg = tcp_maxseg(tp);
2389021eaf79SHiren Panchasara 			if (tlen == 0 &&
2390021eaf79SHiren Panchasara 			    (tiwin == tp->snd_wnd ||
2391021eaf79SHiren Panchasara 			    (tp->t_flags & TF_SACK_PERMIT))) {
239262b90589SPeter Wemm 				/*
239362b90589SPeter Wemm 				 * If this is the first time we've seen a
239462b90589SPeter Wemm 				 * FIN from the remote, this is not a
239562b90589SPeter Wemm 				 * duplicate and it needs to be processed
239662b90589SPeter Wemm 				 * normally.  This happens during a
239762b90589SPeter Wemm 				 * simultaneous close.
239862b90589SPeter Wemm 				 */
239962b90589SPeter Wemm 				if ((thflags & TH_FIN) &&
240062b90589SPeter Wemm 				    (TCPS_HAVERCVDFIN(tp->t_state) == 0)) {
240162b90589SPeter Wemm 					tp->t_dupacks = 0;
240262b90589SPeter Wemm 					break;
240362b90589SPeter Wemm 				}
240478b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvdupack);
2405df8bae1dSRodney W. Grimes 				/*
2406df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
2407df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
2408df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
24095f30ec9bSEitan Adler 				 * change and FIN isn't set),
24105f30ec9bSEitan Adler 				 * the ack is the biggest we've
2411df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
2412a4641f4eSPedro F. Giffuni 				 * threshold of them, assume a packet
2413df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
2414df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
2415df8bae1dSRodney W. Grimes 				 * window so we send only this one
2416df8bae1dSRodney W. Grimes 				 * packet.
2417df8bae1dSRodney W. Grimes 				 *
2418df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
2419df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
2420df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
2421df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
2422df8bae1dSRodney W. Grimes 				 * the new ssthresh).
2423df8bae1dSRodney W. Grimes 				 *
2424df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
2425df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
2426df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
2427df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
2428df8bae1dSRodney W. Grimes 				 * network.
2429f2512ba1SRui Paulo 				 *
2430f2512ba1SRui Paulo 				 * When using TCP ECN, notify the peer that
2431f2512ba1SRui Paulo 				 * we reduced the cwnd.
2432df8bae1dSRodney W. Grimes 				 */
2433021eaf79SHiren Panchasara 				/*
2434021eaf79SHiren Panchasara 				 * Following 2 kinds of acks should not affect
2435021eaf79SHiren Panchasara 				 * dupack counting:
2436021eaf79SHiren Panchasara 				 * 1) Old acks
2437021eaf79SHiren Panchasara 				 * 2) Acks with SACK but without any new SACK
2438021eaf79SHiren Panchasara 				 * information in them. These could result from
2439021eaf79SHiren Panchasara 				 * any anomaly in the network like a switch
2440021eaf79SHiren Panchasara 				 * duplicating packets or a possible DoS attack.
2441021eaf79SHiren Panchasara 				 */
2442021eaf79SHiren Panchasara 				if (th->th_ack != tp->snd_una ||
2443021eaf79SHiren Panchasara 				    ((tp->t_flags & TF_SACK_PERMIT) &&
2444021eaf79SHiren Panchasara 				    !sack_changed))
2445021eaf79SHiren Panchasara 					break;
2446021eaf79SHiren Panchasara 				else if (!tcp_timer_active(tp, TT_REXMT))
2447df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
2448cb942153SJeffrey Hsu 				else if (++tp->t_dupacks > tcprexmtthresh ||
2449dbc42409SLawrence Stewart 				     IN_FASTRECOVERY(tp->t_flags)) {
24504b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
24514b7b743cSLawrence Stewart 					    CC_DUPACK);
24523529149eSAndre Oppermann 					if ((tp->t_flags & TF_SACK_PERMIT) &&
2453dbc42409SLawrence Stewart 					    IN_FASTRECOVERY(tp->t_flags)) {
2454808f11b7SPaul Saab 						int awnd;
245525e6f9edSPaul Saab 
245625e6f9edSPaul Saab 						/*
245725e6f9edSPaul Saab 						 * Compute the amount of data in flight first.
24584c3972f0SHiren Panchasara 						 * We can inject new data into the pipe iff
245925e6f9edSPaul Saab 						 * we have less than 1/2 the original window's
246025e6f9edSPaul Saab 						 * worth of data in flight.
246125e6f9edSPaul Saab 						 */
246212eeb81fSHiren Panchasara 						if (V_tcp_do_rfc6675_pipe)
246312eeb81fSHiren Panchasara 							awnd = tcp_compute_pipe(tp);
246412eeb81fSHiren Panchasara 						else
2465808f11b7SPaul Saab 							awnd = (tp->snd_nxt - tp->snd_fack) +
24660077b016SPaul Saab 								tp->sackhint.sack_bytes_rexmit;
246712eeb81fSHiren Panchasara 
2468808f11b7SPaul Saab 						if (awnd < tp->snd_ssthresh) {
24690c39d38dSGleb Smirnoff 							tp->snd_cwnd += maxseg;
247025e6f9edSPaul Saab 							if (tp->snd_cwnd > tp->snd_ssthresh)
247125e6f9edSPaul Saab 								tp->snd_cwnd = tp->snd_ssthresh;
247225e6f9edSPaul Saab 						}
247325e6f9edSPaul Saab 					} else
24740c39d38dSGleb Smirnoff 						tp->snd_cwnd += maxseg;
247555bceb1eSRandall Stewart 					(void) tp->t_fb->tfb_tcp_output(tp);
247646f58482SJonathan Lemon 					goto drop;
2477cb942153SJeffrey Hsu 				} else if (tp->t_dupacks == tcprexmtthresh) {
2478cb942153SJeffrey Hsu 					tcp_seq onxt = tp->snd_nxt;
2479a0445c2eSJayanth Vijayaraghavan 
2480a0445c2eSJayanth Vijayaraghavan 					/*
2481a0445c2eSJayanth Vijayaraghavan 					 * If we're doing sack, check to
2482a0445c2eSJayanth Vijayaraghavan 					 * see if we're already in sack
2483a0445c2eSJayanth Vijayaraghavan 					 * recovery. If we're not doing sack,
2484a0445c2eSJayanth Vijayaraghavan 					 * check to see if we're in newreno
2485a0445c2eSJayanth Vijayaraghavan 					 * recovery.
2486a0445c2eSJayanth Vijayaraghavan 					 */
24873529149eSAndre Oppermann 					if (tp->t_flags & TF_SACK_PERMIT) {
2488dbc42409SLawrence Stewart 						if (IN_FASTRECOVERY(tp->t_flags)) {
2489a0445c2eSJayanth Vijayaraghavan 							tp->t_dupacks = 0;
2490a0445c2eSJayanth Vijayaraghavan 							break;
2491a0445c2eSJayanth Vijayaraghavan 						}
2492dbc42409SLawrence Stewart 					} else {
2493a0445c2eSJayanth Vijayaraghavan 						if (SEQ_LEQ(th->th_ack,
24949d11646dSJeffrey Hsu 						    tp->snd_recover)) {
2495cb942153SJeffrey Hsu 							tp->t_dupacks = 0;
2496cb942153SJeffrey Hsu 							break;
249746f58482SJonathan Lemon 						}
2498a0445c2eSJayanth Vijayaraghavan 					}
2499dbc42409SLawrence Stewart 					/* Congestion signal before ack. */
2500dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_NDUPACK);
25014b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
25024b7b743cSLawrence Stewart 					    CC_DUPACK);
2503b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
25049b8b58e0SJonathan Lemon 					tp->t_rtttime = 0;
25053529149eSAndre Oppermann 					if (tp->t_flags & TF_SACK_PERMIT) {
250678b50714SRobert Watson 						TCPSTAT_INC(
250778b50714SRobert Watson 						    tcps_sack_recovery_episode);
2508a55db2b6SPaul Saab 						tp->sack_newdata = tp->snd_nxt;
25090c39d38dSGleb Smirnoff 						tp->snd_cwnd = maxseg;
251055bceb1eSRandall Stewart 						(void) tp->t_fb->tfb_tcp_output(tp);
25116d90faf3SPaul Saab 						goto drop;
25126d90faf3SPaul Saab 					}
2513fb59c426SYoshinobu Inoue 					tp->snd_nxt = th->th_ack;
25140c39d38dSGleb Smirnoff 					tp->snd_cwnd = maxseg;
251555bceb1eSRandall Stewart 					(void) tp->t_fb->tfb_tcp_output(tp);
251648d2549cSJeffrey Hsu 					KASSERT(tp->snd_limited <= 2,
2517302ce8d6SAndre Oppermann 					    ("%s: tp->snd_limited too big",
2518302ce8d6SAndre Oppermann 					    __func__));
2519df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
25200c39d38dSGleb Smirnoff 					     maxseg *
252148d2549cSJeffrey Hsu 					     (tp->t_dupacks - tp->snd_limited);
2522df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
2523df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
2524df8bae1dSRodney W. Grimes 					goto drop;
2525603724d3SBjoern A. Zeeb 				} else if (V_tcp_do_rfc3042) {
252662d4443fSHiren Panchasara 					/*
252762d4443fSHiren Panchasara 					 * Process first and second duplicate
252862d4443fSHiren Panchasara 					 * ACKs. Each indicates a segment
252962d4443fSHiren Panchasara 					 * leaving the network, creating room
253062d4443fSHiren Panchasara 					 * for more. Make sure we can send a
253162d4443fSHiren Panchasara 					 * packet on reception of each duplicate
253262d4443fSHiren Panchasara 					 * ACK by increasing snd_cwnd by one
253362d4443fSHiren Panchasara 					 * segment. Restore the original
253462d4443fSHiren Panchasara 					 * snd_cwnd after packet transmission.
253562d4443fSHiren Panchasara 					 */
25364b7b743cSLawrence Stewart 					cc_ack_received(tp, th, nsegs,
25374b7b743cSLawrence Stewart 					    CC_DUPACK);
25383ac12506SJonathan T. Looney 					uint32_t oldcwnd = tp->snd_cwnd;
253948d2549cSJeffrey Hsu 					tcp_seq oldsndmax = tp->snd_max;
254048d2549cSJeffrey Hsu 					u_int sent;
25415628dd08SAndre Oppermann 					int avail;
254289c02376SJeffrey Hsu 
2543582a954bSJeffrey Hsu 					KASSERT(tp->t_dupacks == 1 ||
2544582a954bSJeffrey Hsu 					    tp->t_dupacks == 2,
2545302ce8d6SAndre Oppermann 					    ("%s: dupacks not 1 or 2",
2546302ce8d6SAndre Oppermann 					    __func__));
254761a36e3dSJeffrey Hsu 					if (tp->t_dupacks == 1)
254848d2549cSJeffrey Hsu 						tp->snd_limited = 0;
254961a36e3dSJeffrey Hsu 					tp->snd_cwnd =
255061a36e3dSJeffrey Hsu 					    (tp->snd_nxt - tp->snd_una) +
255161a36e3dSJeffrey Hsu 					    (tp->t_dupacks - tp->snd_limited) *
25520c39d38dSGleb Smirnoff 					    maxseg;
25535628dd08SAndre Oppermann 					/*
25545628dd08SAndre Oppermann 					 * Only call tcp_output when there
25555628dd08SAndre Oppermann 					 * is new data available to be sent.
25565628dd08SAndre Oppermann 					 * Otherwise we would send pure ACKs.
25575628dd08SAndre Oppermann 					 */
25585628dd08SAndre Oppermann 					SOCKBUF_LOCK(&so->so_snd);
2559cfa6009eSGleb Smirnoff 					avail = sbavail(&so->so_snd) -
25605628dd08SAndre Oppermann 					    (tp->snd_nxt - tp->snd_una);
25615628dd08SAndre Oppermann 					SOCKBUF_UNLOCK(&so->so_snd);
25625628dd08SAndre Oppermann 					if (avail > 0)
256355bceb1eSRandall Stewart 						(void) tp->t_fb->tfb_tcp_output(tp);
256448d2549cSJeffrey Hsu 					sent = tp->snd_max - oldsndmax;
25650c39d38dSGleb Smirnoff 					if (sent > maxseg) {
256689c02376SJeffrey Hsu 						KASSERT((tp->t_dupacks == 2 &&
256789c02376SJeffrey Hsu 						    tp->snd_limited == 0) ||
25680c39d38dSGleb Smirnoff 						   (sent == maxseg + 1 &&
256989c02376SJeffrey Hsu 						    tp->t_flags & TF_SENTFIN),
2570302ce8d6SAndre Oppermann 						    ("%s: sent too much",
2571302ce8d6SAndre Oppermann 						    __func__));
257248d2549cSJeffrey Hsu 						tp->snd_limited = 2;
257348d2549cSJeffrey Hsu 					} else if (sent > 0)
257448d2549cSJeffrey Hsu 						++tp->snd_limited;
2575582a954bSJeffrey Hsu 					tp->snd_cwnd = oldcwnd;
2576582a954bSJeffrey Hsu 					goto drop;
2577df8bae1dSRodney W. Grimes 				}
2578021eaf79SHiren Panchasara 			}
2579df8bae1dSRodney W. Grimes 			break;
2580021eaf79SHiren Panchasara 		} else {
2581021eaf79SHiren Panchasara 			/*
2582021eaf79SHiren Panchasara 			 * This ack is advancing the left edge, reset the
2583021eaf79SHiren Panchasara 			 * counter.
2584021eaf79SHiren Panchasara 			 */
2585021eaf79SHiren Panchasara 			tp->t_dupacks = 0;
2586021eaf79SHiren Panchasara 			/*
2587021eaf79SHiren Panchasara 			 * If this ack also has new SACK info, increment the
2588021eaf79SHiren Panchasara 			 * counter as per rfc6675.
2589021eaf79SHiren Panchasara 			 */
2590021eaf79SHiren Panchasara 			if ((tp->t_flags & TF_SACK_PERMIT) && sack_changed)
2591021eaf79SHiren Panchasara 				tp->t_dupacks++;
2592df8bae1dSRodney W. Grimes 		}
2593cb942153SJeffrey Hsu 
2594302ce8d6SAndre Oppermann 		KASSERT(SEQ_GT(th->th_ack, tp->snd_una),
2595302ce8d6SAndre Oppermann 		    ("%s: th_ack <= snd_una", __func__));
2596cb942153SJeffrey Hsu 
2597df8bae1dSRodney W. Grimes 		/*
2598df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
2599df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
2600df8bae1dSRodney W. Grimes 		 */
2601dbc42409SLawrence Stewart 		if (IN_FASTRECOVERY(tp->t_flags)) {
2602cb942153SJeffrey Hsu 			if (SEQ_LT(th->th_ack, tp->snd_recover)) {
26033529149eSAndre Oppermann 				if (tp->t_flags & TF_SACK_PERMIT)
26046d90faf3SPaul Saab 					tcp_sack_partialack(tp, th);
26056d90faf3SPaul Saab 				else
2606c068736aSJeffrey Hsu 					tcp_newreno_partial_ack(tp, th);
2607dbc42409SLawrence Stewart 			} else
2608dbc42409SLawrence Stewart 				cc_post_recovery(tp, th);
260946f58482SJonathan Lemon 		}
2610a0292f23SGarrett Wollman 		/*
2611a0292f23SGarrett Wollman 		 * If we reach this point, ACK is not a duplicate,
2612a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
2613a0292f23SGarrett Wollman 		 */
2614a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
2615a0292f23SGarrett Wollman 			/*
2616a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
2617a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
261807e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
261907e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
262007e43e10SAndras Olah 			 * we can do window scaling.
2621a0292f23SGarrett Wollman 			 */
2622a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
2623a0292f23SGarrett Wollman 			tp->snd_una++;
262407e43e10SAndras Olah 			/* Do window scaling? */
262507e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
262607e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
262707e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2628464fcfbcSAndre Oppermann 				/* Send window already scaled. */
262907e43e10SAndras Olah 			}
2630a0292f23SGarrett Wollman 		}
2631a0292f23SGarrett Wollman 
2632a0292f23SGarrett Wollman process_ACK:
26338501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
26347cfc6904SRobert Watson 
2635dbc42409SLawrence Stewart 		acked = BYTES_THIS_ACK(tp, th);
2636b8c2cd15SJonathan T. Looney 		KASSERT(acked >= 0, ("%s: acked unexepectedly negative "
2637b8c2cd15SJonathan T. Looney 		    "(tp->snd_una=%u, th->th_ack=%u, tp=%p, m=%p)", __func__,
2638b8c2cd15SJonathan T. Looney 		    tp->snd_una, th->th_ack, tp, m));
26394b7b743cSLawrence Stewart 		TCPSTAT_ADD(tcps_rcvackpack, nsegs);
264078b50714SRobert Watson 		TCPSTAT_ADD(tcps_rcvackbyte, acked);
2641df8bae1dSRodney W. Grimes 
2642df8bae1dSRodney W. Grimes 		/*
26439b8b58e0SJonathan Lemon 		 * If we just performed our first retransmit, and the ACK
26449b8b58e0SJonathan Lemon 		 * arrives within our recovery window, then it was a mistake
26459b8b58e0SJonathan Lemon 		 * to do the retransmit in the first place.  Recover our
26469b8b58e0SJonathan Lemon 		 * original cwnd and ssthresh, and proceed to transmit where
26479b8b58e0SJonathan Lemon 		 * we left off.
26489b8b58e0SJonathan Lemon 		 */
264910d20c84SMatt Macy 		if (tp->t_rxtshift == 1 &&
265010d20c84SMatt Macy 		    tp->t_flags & TF_PREVVALID &&
265110d20c84SMatt Macy 		    tp->t_badrxtwin &&
265210d20c84SMatt Macy 		    SEQ_LT(to.to_tsecr, tp->t_badrxtwin))
2653dbc42409SLawrence Stewart 			cc_cong_signal(tp, th, CC_RTO_ERR);
26549b8b58e0SJonathan Lemon 
26559b8b58e0SJonathan Lemon 		/*
2656df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
2657df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
2658df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
2659df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
2660df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
2661df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
2662df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
2663fa55172bSMatthew Dillon 		 *
2664fa55172bSMatthew Dillon 		 * Some boxes send broken timestamp replies
2665fa55172bSMatthew Dillon 		 * during the SYN+ACK phase, ignore
2666fa55172bSMatthew Dillon 		 * timestamps of 0 or we could calculate a
2667fa55172bSMatthew Dillon 		 * huge RTT and blow up the retransmit timer.
2668df8bae1dSRodney W. Grimes 		 */
2669d8951c8aSBjoern A. Zeeb 		if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) {
26703ac12506SJonathan T. Looney 			uint32_t t;
2671d8951c8aSBjoern A. Zeeb 
2672d8951c8aSBjoern A. Zeeb 			t = tcp_ts_getticks() - to.to_tsecr;
2673d8951c8aSBjoern A. Zeeb 			if (!tp->t_rttlow || tp->t_rttlow > t)
2674d8951c8aSBjoern A. Zeeb 				tp->t_rttlow = t;
2675d8951c8aSBjoern A. Zeeb 			tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1);
2676fa55172bSMatthew Dillon 		} else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
2677eaf80179SAndre Oppermann 			if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime)
2678eaf80179SAndre Oppermann 				tp->t_rttlow = ticks - tp->t_rtttime;
26799b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
2680fa55172bSMatthew Dillon 		}
2681df8bae1dSRodney W. Grimes 
2682df8bae1dSRodney W. Grimes 		/*
2683df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
2684df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
2685df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
2686df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
2687df8bae1dSRodney W. Grimes 		 */
2688fb59c426SYoshinobu Inoue 		if (th->th_ack == tp->snd_max) {
2689b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
2690df8bae1dSRodney W. Grimes 			needoutput = 1;
2691b8152ba7SAndre Oppermann 		} else if (!tcp_timer_active(tp, TT_PERSIST))
2692b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
2693a0292f23SGarrett Wollman 
2694a0292f23SGarrett Wollman 		/*
2695a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
2696a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
2697a0292f23SGarrett Wollman 		 */
2698a0292f23SGarrett Wollman 		if (acked == 0)
2699a0292f23SGarrett Wollman 			goto step6;
2700a0292f23SGarrett Wollman 
2701df8bae1dSRodney W. Grimes 		/*
2702dbc42409SLawrence Stewart 		 * Let the congestion control algorithm update congestion
2703dbc42409SLawrence Stewart 		 * control related information. This typically means increasing
2704dbc42409SLawrence Stewart 		 * the congestion window.
2705df8bae1dSRodney W. Grimes 		 */
27064b7b743cSLawrence Stewart 		cc_ack_received(tp, th, nsegs, CC_ACK);
2707dbc42409SLawrence Stewart 
27085905999bSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
2709cfa6009eSGleb Smirnoff 		if (acked > sbavail(&so->so_snd)) {
2710b8c2cd15SJonathan T. Looney 			if (tp->snd_wnd >= sbavail(&so->so_snd))
2711cfa6009eSGleb Smirnoff 				tp->snd_wnd -= sbavail(&so->so_snd);
2712b8c2cd15SJonathan T. Looney 			else
2713b8c2cd15SJonathan T. Looney 				tp->snd_wnd = 0;
2714c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd,
2715cfa6009eSGleb Smirnoff 			    (int)sbavail(&so->so_snd));
2716df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
2717df8bae1dSRodney W. Grimes 		} else {
2718c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd, acked);
27193ac12506SJonathan T. Looney 			if (tp->snd_wnd >= (uint32_t) acked)
272060ee3bb2SAndre Oppermann 				tp->snd_wnd -= acked;
2721b8c2cd15SJonathan T. Looney 			else
2722b8c2cd15SJonathan T. Looney 				tp->snd_wnd = 0;
2723df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
2724df8bae1dSRodney W. Grimes 		}
2725564aab1fSAndre Oppermann 		/* NB: sowwakeup_locked() does an implicit unlock. */
27261e4d7da7SRobert Watson 		sowwakeup_locked(so);
2727c11a15bfSGleb Smirnoff 		m_freem(mfree);
2728e8949f74SAndre Oppermann 		/* Detect una wraparound. */
2729dbc42409SLawrence Stewart 		if (!IN_RECOVERY(tp->t_flags) &&
27309d11646dSJeffrey Hsu 		    SEQ_GT(tp->snd_una, tp->snd_recover) &&
27319d11646dSJeffrey Hsu 		    SEQ_LEQ(th->th_ack, tp->snd_recover))
27329d11646dSJeffrey Hsu 			tp->snd_recover = th->th_ack - 1;
2733dbc42409SLawrence Stewart 		/* XXXLAS: Can this be moved up into cc_post_recovery? */
2734dbc42409SLawrence Stewart 		if (IN_RECOVERY(tp->t_flags) &&
273524cb0f22SLawrence Stewart 		    SEQ_GEQ(th->th_ack, tp->snd_recover)) {
2736dbc42409SLawrence Stewart 			EXIT_RECOVERY(tp->t_flags);
273724cb0f22SLawrence Stewart 		}
2738fb59c426SYoshinobu Inoue 		tp->snd_una = th->th_ack;
27393529149eSAndre Oppermann 		if (tp->t_flags & TF_SACK_PERMIT) {
27406d90faf3SPaul Saab 			if (SEQ_GT(tp->snd_una, tp->snd_recover))
27416d90faf3SPaul Saab 				tp->snd_recover = tp->snd_una;
27426d90faf3SPaul Saab 		}
2743df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2744df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
2745df8bae1dSRodney W. Grimes 
2746df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2747df8bae1dSRodney W. Grimes 
2748df8bae1dSRodney W. Grimes 		/*
2749df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
2750df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
2751df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
2752df8bae1dSRodney W. Grimes 		 */
2753df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2754df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2755df8bae1dSRodney W. Grimes 				/*
2756df8bae1dSRodney W. Grimes 				 * If we can't receive any more
2757df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
2758df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
2759df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
2760df8bae1dSRodney W. Grimes 				 * we'll hang forever.
2761e8949f74SAndre Oppermann 				 *
2762e8949f74SAndre Oppermann 				 * XXXjl:
2763340c35deSJonathan Lemon 				 * we should release the tp also, and use a
2764340c35deSJonathan Lemon 				 * compressed state.
2765340c35deSJonathan Lemon 				 */
2766c0b99ffaSRobert Watson 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
276703e49181SSeigo Tanimura 					soisdisconnected(so);
27689077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
27699077f387SGleb Smirnoff 					    (tcp_fast_finwait2_recycle ?
27709077f387SGleb Smirnoff 					    tcp_finwait2_timeout :
27719077f387SGleb Smirnoff 					    TP_MAXIDLE(tp)));
27724cc20ab1SSeigo Tanimura 				}
277357f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_2);
2774df8bae1dSRodney W. Grimes 			}
2775df8bae1dSRodney W. Grimes 			break;
2776df8bae1dSRodney W. Grimes 
2777df8bae1dSRodney W. Grimes 		/*
2778df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
2779df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
2780df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
2781df8bae1dSRodney W. Grimes 		 * the segment.
2782df8bae1dSRodney W. Grimes 		 */
2783df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
2784df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
278511a20fb8SJeffrey Hsu 				tcp_twstart(tp);
2786340c35deSJonathan Lemon 				m_freem(m);
2787679d9708SAndre Oppermann 				return;
2788df8bae1dSRodney W. Grimes 			}
2789df8bae1dSRodney W. Grimes 			break;
2790df8bae1dSRodney W. Grimes 
2791df8bae1dSRodney W. Grimes 		/*
2792df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
2793df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
2794df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
2795df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
2796df8bae1dSRodney W. Grimes 		 */
2797df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
2798df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2799df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
2800df8bae1dSRodney W. Grimes 				goto drop;
2801df8bae1dSRodney W. Grimes 			}
2802df8bae1dSRodney W. Grimes 			break;
2803df8bae1dSRodney W. Grimes 		}
2804df8bae1dSRodney W. Grimes 	}
2805df8bae1dSRodney W. Grimes 
2806df8bae1dSRodney W. Grimes step6:
28078501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
28087cfc6904SRobert Watson 
2809df8bae1dSRodney W. Grimes 	/*
281060ee3bb2SAndre Oppermann 	 * Update window information.
281160ee3bb2SAndre Oppermann 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2812df8bae1dSRodney W. Grimes 	 */
281360ee3bb2SAndre Oppermann 	if ((thflags & TH_ACK) &&
281460ee3bb2SAndre Oppermann 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
281560ee3bb2SAndre Oppermann 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
281660ee3bb2SAndre Oppermann 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
281760ee3bb2SAndre Oppermann 		/* keep track of pure window updates */
281860ee3bb2SAndre Oppermann 		if (tlen == 0 &&
281960ee3bb2SAndre Oppermann 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
282078b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvwinupd);
2821df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
282260ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq;
282360ee3bb2SAndre Oppermann 		tp->snd_wl2 = th->th_ack;
2824df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
2825df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
282660ee3bb2SAndre Oppermann 		needoutput = 1;
2827df8bae1dSRodney W. Grimes 	}
2828df8bae1dSRodney W. Grimes 
2829df8bae1dSRodney W. Grimes 	/*
2830df8bae1dSRodney W. Grimes 	 * Process segments with URG.
2831df8bae1dSRodney W. Grimes 	 */
2832fb59c426SYoshinobu Inoue 	if ((thflags & TH_URG) && th->th_urp &&
2833df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2834df8bae1dSRodney W. Grimes 		/*
2835df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
2836df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
2837df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
2838df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
2839df8bae1dSRodney W. Grimes 		 */
284018ad5842SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
2841cfa6009eSGleb Smirnoff 		if (th->th_urp + sbavail(&so->so_rcv) > sb_max) {
2842fb59c426SYoshinobu Inoue 			th->th_urp = 0;			/* XXX */
2843fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;		/* XXX */
284418ad5842SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);	/* XXX */
2845df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
2846df8bae1dSRodney W. Grimes 		}
2847df8bae1dSRodney W. Grimes 		/*
2848df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
2849df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
2850df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2851df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
2852df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
2853df8bae1dSRodney W. Grimes 		 *
2854df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
2855df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
2856df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
2857df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
2858df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
2859df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
2860df8bae1dSRodney W. Grimes 		 */
2861fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
2862fb59c426SYoshinobu Inoue 			tp->rcv_up = th->th_seq + th->th_urp;
2863cfa6009eSGleb Smirnoff 			so->so_oobmark = sbavail(&so->so_rcv) +
2864df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
2865927c5ceaSRobert Watson 			if (so->so_oobmark == 0)
2866c0b99ffaSRobert Watson 				so->so_rcv.sb_state |= SBS_RCVATMARK;
2867df8bae1dSRodney W. Grimes 			sohasoutofband(so);
2868df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2869df8bae1dSRodney W. Grimes 		}
287018ad5842SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
2871df8bae1dSRodney W. Grimes 		/*
2872df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
2873df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
2874df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
2875df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
2876df8bae1dSRodney W. Grimes 		 */
28773ac12506SJonathan T. Looney 		if (th->th_urp <= (uint32_t)tlen &&
287830613f56SJeffrey Hsu 		    !(so->so_options & SO_OOBINLINE)) {
287930613f56SJeffrey Hsu 			/* hdr drop is delayed */
288030613f56SJeffrey Hsu 			tcp_pulloutofband(so, th, m, drop_hdrlen);
288130613f56SJeffrey Hsu 		}
2882c068736aSJeffrey Hsu 	} else {
2883df8bae1dSRodney W. Grimes 		/*
2884df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
2885df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
2886df8bae1dSRodney W. Grimes 		 * with the receive window.
2887df8bae1dSRodney W. Grimes 		 */
2888df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
2889df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
2890c068736aSJeffrey Hsu 	}
2891df8bae1dSRodney W. Grimes dodata:							/* XXX */
28928501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
28937cfc6904SRobert Watson 
2894df8bae1dSRodney W. Grimes 	/*
2895df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
2896df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
2897df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
2898df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
2899df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
2900df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
2901df8bae1dSRodney W. Grimes 	 */
2902281a0fd4SPatrick Kelsey 	tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
290368bd7ed1SJonathan T. Looney 		   IS_FASTOPEN(tp->t_flags));
2904281a0fd4SPatrick Kelsey 	if ((tlen || (thflags & TH_FIN) || tfo_syn) &&
2905df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
290669e03620SPaul Saab 		tcp_seq save_start = th->th_seq;
29075acfd95cSMichael Tuexen 		tcp_seq save_rnxt  = tp->rcv_nxt;
29085acfd95cSMichael Tuexen 		int     save_tlen  = tlen;
2909fb59c426SYoshinobu Inoue 		m_adj(m, drop_hdrlen);	/* delayed header drop */
2910e4b64281SJesper Skriver 		/*
2911c068736aSJeffrey Hsu 		 * Insert segment which includes th into TCP reassembly queue
2912c068736aSJeffrey Hsu 		 * with control block tp.  Set thflags to whether reassembly now
2913c068736aSJeffrey Hsu 		 * includes a segment with FIN.  This handles the common case
2914c068736aSJeffrey Hsu 		 * inline (segment is the next to be received on an established
2915c068736aSJeffrey Hsu 		 * connection, and the queue is empty), avoiding linkage into
2916c068736aSJeffrey Hsu 		 * and removal from the queue and repetition of various
2917c068736aSJeffrey Hsu 		 * conversions.
2918c068736aSJeffrey Hsu 		 * Set DELACK for segments received in order, but ack
2919c068736aSJeffrey Hsu 		 * immediately when segments are out of order (so
2920c068736aSJeffrey Hsu 		 * fast retransmit can work).
2921e4b64281SJesper Skriver 		 */
29224741bfcbSPatrick Kelsey 		if (th->th_seq == tp->rcv_nxt &&
2923c28440dbSRandall Stewart 		    SEGQ_EMPTY(tp) &&
2924281a0fd4SPatrick Kelsey 		    (TCPS_HAVEESTABLISHED(tp->t_state) ||
2925281a0fd4SPatrick Kelsey 		     tfo_syn)) {
2926281a0fd4SPatrick Kelsey 			if (DELAY_ACK(tp, tlen) || tfo_syn)
29273bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
2928e4b64281SJesper Skriver 			else
2929e4b64281SJesper Skriver 				tp->t_flags |= TF_ACKNOW;
2930e4b64281SJesper Skriver 			tp->rcv_nxt += tlen;
2931e4b64281SJesper Skriver 			thflags = th->th_flags & TH_FIN;
293278b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpack);
293378b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
29341e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
2935c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
2936c1c36a2cSMike Silbersack 				m_freem(m);
2937c1c36a2cSMike Silbersack 			else
2938651e4e6aSGleb Smirnoff 				sbappendstream_locked(&so->so_rcv, m, 0);
2939e8949f74SAndre Oppermann 			/* NB: sorwakeup_locked() does an implicit unlock. */
29401e4d7da7SRobert Watson 			sorwakeup_locked(so);
2941e4b64281SJesper Skriver 		} else {
2942e8949f74SAndre Oppermann 			/*
2943e8949f74SAndre Oppermann 			 * XXX: Due to the header drop above "th" is
2944e8949f74SAndre Oppermann 			 * theoretically invalid by now.  Fortunately
2945e8949f74SAndre Oppermann 			 * m_adj() doesn't actually frees any mbufs
2946e8949f74SAndre Oppermann 			 * when trimming from the head.
2947e8949f74SAndre Oppermann 			 */
29485acfd95cSMichael Tuexen 			tcp_seq temp = save_start;
29495acfd95cSMichael Tuexen 			thflags = tcp_reass(tp, th, &temp, &tlen, m);
2950e4b64281SJesper Skriver 			tp->t_flags |= TF_ACKNOW;
2951e4b64281SJesper Skriver 		}
29526d261981SMichael Tuexen 		if ((tp->t_flags & TF_SACK_PERMIT) && (save_tlen > 0)) {
29536d261981SMichael Tuexen 			if ((tlen == 0) && (SEQ_LT(save_start, save_rnxt))) {
2954b5a154d8SMichael Tuexen 				/*
2955b5a154d8SMichael Tuexen 				 * DSACK actually handled in the fastpath
2956b5a154d8SMichael Tuexen 				 * above.
2957b5a154d8SMichael Tuexen 				 */
2958ecc5b1d1SMichael Tuexen 				tcp_update_sack_list(tp, save_start,
2959ecc5b1d1SMichael Tuexen 				    save_start + save_tlen);
2960ecc5b1d1SMichael Tuexen 			} else if ((tlen > 0) && SEQ_GT(tp->rcv_nxt, save_rnxt)) {
2961ecc5b1d1SMichael Tuexen 				if ((tp->rcv_numsacks >= 1) &&
2962ecc5b1d1SMichael Tuexen 				    (tp->sackblks[0].end == save_start)) {
29636d261981SMichael Tuexen 					/*
29646d261981SMichael Tuexen 					 * Partial overlap, recorded at todrop
29656d261981SMichael Tuexen 					 * above.
29666d261981SMichael Tuexen 					 */
29676d261981SMichael Tuexen 					tcp_update_sack_list(tp,
29686d261981SMichael Tuexen 					    tp->sackblks[0].start,
2969ecc5b1d1SMichael Tuexen 					    tp->sackblks[0].end);
2970ecc5b1d1SMichael Tuexen 				} else {
2971ecc5b1d1SMichael Tuexen 					tcp_update_dsack_list(tp, save_start,
2972ecc5b1d1SMichael Tuexen 					    save_start + save_tlen);
2973ecc5b1d1SMichael Tuexen 				}
29746d261981SMichael Tuexen 			} else if (tlen >= save_tlen) {
2975b5a154d8SMichael Tuexen 				/* Update of sackblks. */
2976ecc5b1d1SMichael Tuexen 				tcp_update_dsack_list(tp, save_start,
2977ecc5b1d1SMichael Tuexen 				    save_start + save_tlen);
2978ecc5b1d1SMichael Tuexen 			} else if (tlen > 0) {
2979ecc5b1d1SMichael Tuexen 				tcp_update_dsack_list(tp, save_start,
2980ecc5b1d1SMichael Tuexen 				    save_start + tlen);
29815acfd95cSMichael Tuexen 			}
29825acfd95cSMichael Tuexen 		}
2983302ce8d6SAndre Oppermann #if 0
2984df8bae1dSRodney W. Grimes 		/*
2985df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
2986df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
2987df8bae1dSRodney W. Grimes 		 * buffer size.
2988302ce8d6SAndre Oppermann 		 * XXX: Unused.
2989df8bae1dSRodney W. Grimes 		 */
2990f701e30dSJohn Baldwin 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt))
2991df8bae1dSRodney W. Grimes 			len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
2992f701e30dSJohn Baldwin 		else
2993f701e30dSJohn Baldwin 			len = so->so_rcv.sb_hiwat;
2994302ce8d6SAndre Oppermann #endif
2995df8bae1dSRodney W. Grimes 	} else {
2996df8bae1dSRodney W. Grimes 		m_freem(m);
2997fb59c426SYoshinobu Inoue 		thflags &= ~TH_FIN;
2998df8bae1dSRodney W. Grimes 	}
2999df8bae1dSRodney W. Grimes 
3000df8bae1dSRodney W. Grimes 	/*
3001df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
3002df8bae1dSRodney W. Grimes 	 * that the connection is closing.
3003df8bae1dSRodney W. Grimes 	 */
3004fb59c426SYoshinobu Inoue 	if (thflags & TH_FIN) {
3005df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
3006df8bae1dSRodney W. Grimes 			socantrcvmore(so);
3007a0292f23SGarrett Wollman 			/*
3008a0292f23SGarrett Wollman 			 * If connection is half-synchronized
3009d3eede9dSAndras Olah 			 * (ie NEEDSYN flag on) then delay ACK,
3010a0292f23SGarrett Wollman 			 * so it may be piggybacked when SYN is sent.
3011a0292f23SGarrett Wollman 			 * Otherwise, since we received a FIN then no
3012a0292f23SGarrett Wollman 			 * more input can be expected, send ACK now.
3013a0292f23SGarrett Wollman 			 */
30143bfd6421SJonathan Lemon 			if (tp->t_flags & TF_NEEDSYN)
30153bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
3016a0292f23SGarrett Wollman 			else
3017df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
3018df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
3019df8bae1dSRodney W. Grimes 		}
3020df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
3021df8bae1dSRodney W. Grimes 
3022df8bae1dSRodney W. Grimes 		/*
3023df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
3024df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
3025df8bae1dSRodney W. Grimes 		 */
3026df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
30279b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
30289b8b58e0SJonathan Lemon 			/* FALLTHROUGH */
3029df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
303057f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSE_WAIT);
3031df8bae1dSRodney W. Grimes 			break;
3032df8bae1dSRodney W. Grimes 
3033df8bae1dSRodney W. Grimes 		/*
3034df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
3035df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
3036df8bae1dSRodney W. Grimes 		 */
3037df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
303857f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSING);
3039df8bae1dSRodney W. Grimes 			break;
3040df8bae1dSRodney W. Grimes 
3041df8bae1dSRodney W. Grimes 		/*
3042df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
3043df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
3044df8bae1dSRodney W. Grimes 		 * standard timers.
3045df8bae1dSRodney W. Grimes 		 */
3046df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
3047340c35deSJonathan Lemon 			tcp_twstart(tp);
3048679d9708SAndre Oppermann 			return;
3049df8bae1dSRodney W. Grimes 		}
3050df8bae1dSRodney W. Grimes 	}
3051610ee2f9SDavid Greenman #ifdef TCPDEBUG
30524cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
3053fb59c426SYoshinobu Inoue 		tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
3054fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3055610ee2f9SDavid Greenman #endif
30562b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
3057df8bae1dSRodney W. Grimes 
3058df8bae1dSRodney W. Grimes 	/*
3059df8bae1dSRodney W. Grimes 	 * Return any desired output.
3060df8bae1dSRodney W. Grimes 	 */
3061df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
306255bceb1eSRandall Stewart 		(void) tp->t_fb->tfb_tcp_output(tp);
30637792ea27SJeffrey Hsu 
3064a14c749fSJonathan Lemon check_delack:
30658501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
3066252ca428SRobert Watson 
3067a14c749fSJonathan Lemon 	if (tp->t_flags & TF_DELACK) {
30683bfd6421SJonathan Lemon 		tp->t_flags &= ~TF_DELACK;
3069b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
30703bfd6421SJonathan Lemon 	}
30718501a69cSRobert Watson 	INP_WUNLOCK(tp->t_inpcb);
3072679d9708SAndre Oppermann 	return;
3073df8bae1dSRodney W. Grimes 
3074df8bae1dSRodney W. Grimes dropafterack:
3075df8bae1dSRodney W. Grimes 	/*
3076df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
3077df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
307880ab7c0eSGarrett Wollman 	 *
307980ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
308080ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
308180ab7c0eSGarrett Wollman 	 * RST have been dropped.
308280ab7c0eSGarrett Wollman 	 *
308380ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
308480ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
308580ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
308680ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
308780ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
308880ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
3089df8bae1dSRodney W. Grimes 	 */
3090fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
3091fb59c426SYoshinobu Inoue 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
3092a57815efSBosko Milekic 	     SEQ_GT(th->th_ack, tp->snd_max)) ) {
3093a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
3094a57815efSBosko Milekic 		goto dropwithreset;
3095a57815efSBosko Milekic 	}
3096a0292f23SGarrett Wollman #ifdef TCPDEBUG
30974cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
3098fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
3099fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3100a0292f23SGarrett Wollman #endif
31012b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
3102df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
310355bceb1eSRandall Stewart 	(void) tp->t_fb->tfb_tcp_output(tp);
31048501a69cSRobert Watson 	INP_WUNLOCK(tp->t_inpcb);
3105d6915262SRobert Watson 	m_freem(m);
3106679d9708SAndre Oppermann 	return;
3107df8bae1dSRodney W. Grimes 
3108df8bae1dSRodney W. Grimes dropwithreset:
3109a0ca0871SRobert Watson 	if (tp != NULL) {
3110302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
31118501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
3112dd8ac7f9SRobert Watson 	} else
3113a0ca0871SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
3114679d9708SAndre Oppermann 	return;
3115df8bae1dSRodney W. Grimes 
3116df8bae1dSRodney W. Grimes drop:
3117df8bae1dSRodney W. Grimes 	/*
3118df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
3119df8bae1dSRodney W. Grimes 	 */
3120610ee2f9SDavid Greenman #ifdef TCPDEBUG
31211c53f806SRobert Watson 	if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
3122fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
3123fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3124a0292f23SGarrett Wollman #endif
31252b9c9984SGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, m);
31261c53f806SRobert Watson 	if (tp != NULL)
31278501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
3128d6915262SRobert Watson 	m_freem(m);
3129302ce8d6SAndre Oppermann }
3130302ce8d6SAndre Oppermann 
3131302ce8d6SAndre Oppermann /*
31320ca3f933SAndre Oppermann  * Issue RST and make ACK acceptable to originator of segment.
31330ca3f933SAndre Oppermann  * The mbuf must still include the original packet header.
31340ca3f933SAndre Oppermann  * tp may be NULL.
3135302ce8d6SAndre Oppermann  */
313655bceb1eSRandall Stewart void
3137302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp,
3138302ce8d6SAndre Oppermann     int tlen, int rstreason)
3139302ce8d6SAndre Oppermann {
3140b287c6c7SBjoern A. Zeeb #ifdef INET
3141302ce8d6SAndre Oppermann 	struct ip *ip;
3142b287c6c7SBjoern A. Zeeb #endif
3143302ce8d6SAndre Oppermann #ifdef INET6
3144302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6;
3145302ce8d6SAndre Oppermann #endif
31467a3244ccSRobert Watson 
31477a3244ccSRobert Watson 	if (tp != NULL) {
31488501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
31497a3244ccSRobert Watson 	}
31507a3244ccSRobert Watson 
31510ca3f933SAndre Oppermann 	/* Don't bother if destination was broadcast/multicast. */
3152302ce8d6SAndre Oppermann 	if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
3153302ce8d6SAndre Oppermann 		goto drop;
3154302ce8d6SAndre Oppermann #ifdef INET6
3155302ce8d6SAndre Oppermann 	if (mtod(m, struct ip *)->ip_v == 6) {
3156302ce8d6SAndre Oppermann 		ip6 = mtod(m, struct ip6_hdr *);
3157302ce8d6SAndre Oppermann 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
3158302ce8d6SAndre Oppermann 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
3159302ce8d6SAndre Oppermann 			goto drop;
3160302ce8d6SAndre Oppermann 		/* IPv6 anycast check is done at tcp6_input() */
3161b287c6c7SBjoern A. Zeeb 	}
3162302ce8d6SAndre Oppermann #endif
3163b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3164b287c6c7SBjoern A. Zeeb 	else
3165b287c6c7SBjoern A. Zeeb #endif
3166b287c6c7SBjoern A. Zeeb #ifdef INET
3167302ce8d6SAndre Oppermann 	{
3168302ce8d6SAndre Oppermann 		ip = mtod(m, struct ip *);
3169302ce8d6SAndre Oppermann 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
3170302ce8d6SAndre Oppermann 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
3171302ce8d6SAndre Oppermann 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
3172302ce8d6SAndre Oppermann 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
3173302ce8d6SAndre Oppermann 			goto drop;
3174302ce8d6SAndre Oppermann 	}
3175b287c6c7SBjoern A. Zeeb #endif
3176302ce8d6SAndre Oppermann 
3177302ce8d6SAndre Oppermann 	/* Perform bandwidth limiting. */
3178302ce8d6SAndre Oppermann 	if (badport_bandlim(rstreason) < 0)
3179302ce8d6SAndre Oppermann 		goto drop;
3180302ce8d6SAndre Oppermann 
3181302ce8d6SAndre Oppermann 	/* tcp_respond consumes the mbuf chain. */
3182302ce8d6SAndre Oppermann 	if (th->th_flags & TH_ACK) {
3183302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0,
3184302ce8d6SAndre Oppermann 		    th->th_ack, TH_RST);
3185302ce8d6SAndre Oppermann 	} else {
3186302ce8d6SAndre Oppermann 		if (th->th_flags & TH_SYN)
3187302ce8d6SAndre Oppermann 			tlen++;
31884ddd5aadSMichael Tuexen 		if (th->th_flags & TH_FIN)
31894ddd5aadSMichael Tuexen 			tlen++;
3190302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
3191302ce8d6SAndre Oppermann 		    (tcp_seq)0, TH_RST|TH_ACK);
3192302ce8d6SAndre Oppermann 	}
3193302ce8d6SAndre Oppermann 	return;
3194302ce8d6SAndre Oppermann drop:
3195302ce8d6SAndre Oppermann 	m_freem(m);
3196df8bae1dSRodney W. Grimes }
3197df8bae1dSRodney W. Grimes 
3198be2ac88cSJonathan Lemon /*
3199be2ac88cSJonathan Lemon  * Parse TCP options and place in tcpopt.
3200be2ac88cSJonathan Lemon  */
320155bceb1eSRandall Stewart void
3202ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags)
3203df8bae1dSRodney W. Grimes {
3204df8bae1dSRodney W. Grimes 	int opt, optlen;
3205df8bae1dSRodney W. Grimes 
3206be2ac88cSJonathan Lemon 	to->to_flags = 0;
3207df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
3208df8bae1dSRodney W. Grimes 		opt = cp[0];
3209df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
3210df8bae1dSRodney W. Grimes 			break;
3211df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
3212df8bae1dSRodney W. Grimes 			optlen = 1;
3213df8bae1dSRodney W. Grimes 		else {
3214b474779fSJun-ichiro itojun Hagino 			if (cnt < 2)
3215b474779fSJun-ichiro itojun Hagino 				break;
3216df8bae1dSRodney W. Grimes 			optlen = cp[1];
3217b474779fSJun-ichiro itojun Hagino 			if (optlen < 2 || optlen > cnt)
3218df8bae1dSRodney W. Grimes 				break;
3219df8bae1dSRodney W. Grimes 		}
3220df8bae1dSRodney W. Grimes 		switch (opt) {
3221df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
3222df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
3223df8bae1dSRodney W. Grimes 				continue;
3224f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3225df8bae1dSRodney W. Grimes 				continue;
3226be2ac88cSJonathan Lemon 			to->to_flags |= TOF_MSS;
3227be2ac88cSJonathan Lemon 			bcopy((char *)cp + 2,
3228be2ac88cSJonathan Lemon 			    (char *)&to->to_mss, sizeof(to->to_mss));
3229fd8e4ebcSMike Barcroft 			to->to_mss = ntohs(to->to_mss);
3230df8bae1dSRodney W. Grimes 			break;
3231df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
3232df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
3233df8bae1dSRodney W. Grimes 				continue;
3234f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3235df8bae1dSRodney W. Grimes 				continue;
3236be2ac88cSJonathan Lemon 			to->to_flags |= TOF_SCALE;
323702a1a643SAndre Oppermann 			to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT);
3238df8bae1dSRodney W. Grimes 			break;
3239df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
3240df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
3241df8bae1dSRodney W. Grimes 				continue;
3242be2ac88cSJonathan Lemon 			to->to_flags |= TOF_TS;
3243a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
3244a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
3245fd8e4ebcSMike Barcroft 			to->to_tsval = ntohl(to->to_tsval);
3246a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
3247a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
3248fd8e4ebcSMike Barcroft 			to->to_tsecr = ntohl(to->to_tsecr);
3249df8bae1dSRodney W. Grimes 			break;
32501cfd4b53SBruce M Simpson 		case TCPOPT_SIGNATURE:
3251fcf59617SAndrey V. Elsukov 			/*
3252fcf59617SAndrey V. Elsukov 			 * In order to reply to a host which has set the
3253fcf59617SAndrey V. Elsukov 			 * TCP_SIGNATURE option in its initial SYN, we have
3254fcf59617SAndrey V. Elsukov 			 * to record the fact that the option was observed
3255fcf59617SAndrey V. Elsukov 			 * here for the syncache code to perform the correct
3256fcf59617SAndrey V. Elsukov 			 * response.
3257fcf59617SAndrey V. Elsukov 			 */
32581cfd4b53SBruce M Simpson 			if (optlen != TCPOLEN_SIGNATURE)
32591cfd4b53SBruce M Simpson 				continue;
3260df47e437SAndre Oppermann 			to->to_flags |= TOF_SIGNATURE;
3261df47e437SAndre Oppermann 			to->to_signature = cp + 2;
32621cfd4b53SBruce M Simpson 			break;
32636d90faf3SPaul Saab 		case TCPOPT_SACK_PERMITTED:
3264f72167f4SAndre Oppermann 			if (optlen != TCPOLEN_SACK_PERMITTED)
32656d90faf3SPaul Saab 				continue;
3266f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3267f72167f4SAndre Oppermann 				continue;
3268603724d3SBjoern A. Zeeb 			if (!V_tcp_do_sack)
3269f72167f4SAndre Oppermann 				continue;
3270fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACKPERM;
32716d90faf3SPaul Saab 			break;
32726d90faf3SPaul Saab 		case TCPOPT_SACK:
32735a53ca16SPaul Saab 			if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
32746d90faf3SPaul Saab 				continue;
3275b728e902SAndre Oppermann 			if (flags & TO_SYN)
3276b728e902SAndre Oppermann 				continue;
3277fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACK;
32785a53ca16SPaul Saab 			to->to_nsacks = (optlen - 2) / TCPOLEN_SACK;
32795a53ca16SPaul Saab 			to->to_sacks = cp + 2;
328078b50714SRobert Watson 			TCPSTAT_INC(tcps_sack_rcv_blocks);
32816d90faf3SPaul Saab 			break;
3282281a0fd4SPatrick Kelsey 		case TCPOPT_FAST_OPEN:
3283c560df6fSPatrick Kelsey 			/*
3284c560df6fSPatrick Kelsey 			 * Cookie length validation is performed by the
3285c560df6fSPatrick Kelsey 			 * server side cookie checking code or the client
3286c560df6fSPatrick Kelsey 			 * side cookie cache update code.
3287c560df6fSPatrick Kelsey 			 */
3288281a0fd4SPatrick Kelsey 			if (!(flags & TO_SYN))
3289281a0fd4SPatrick Kelsey 				continue;
3290c560df6fSPatrick Kelsey 			if (!V_tcp_fastopen_client_enable &&
3291c560df6fSPatrick Kelsey 			    !V_tcp_fastopen_server_enable)
3292281a0fd4SPatrick Kelsey 				continue;
3293281a0fd4SPatrick Kelsey 			to->to_flags |= TOF_FASTOPEN;
3294281a0fd4SPatrick Kelsey 			to->to_tfo_len = optlen - 2;
3295281a0fd4SPatrick Kelsey 			to->to_tfo_cookie = to->to_tfo_len ? cp + 2 : NULL;
3296281a0fd4SPatrick Kelsey 			break;
3297be2ac88cSJonathan Lemon 		default:
3298be2ac88cSJonathan Lemon 			continue;
3299df8bae1dSRodney W. Grimes 		}
3300df8bae1dSRodney W. Grimes 	}
3301df8bae1dSRodney W. Grimes }
3302df8bae1dSRodney W. Grimes 
3303df8bae1dSRodney W. Grimes /*
3304df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
3305df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
3306df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
3307df8bae1dSRodney W. Grimes  * sequencing purposes.
3308df8bae1dSRodney W. Grimes  */
330955bceb1eSRandall Stewart void
3310ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m,
3311ad3f9ab3SAndre Oppermann     int off)
3312df8bae1dSRodney W. Grimes {
3313fb59c426SYoshinobu Inoue 	int cnt = off + th->th_urp - 1;
3314df8bae1dSRodney W. Grimes 
3315df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
3316df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
3317df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
3318df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
3319df8bae1dSRodney W. Grimes 
33208501a69cSRobert Watson 			INP_WLOCK_ASSERT(tp->t_inpcb);
33217a3244ccSRobert Watson 
3322df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
3323df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
3324df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
3325df8bae1dSRodney W. Grimes 			m->m_len--;
332669a34685SYoshinobu Inoue 			if (m->m_flags & M_PKTHDR)
332769a34685SYoshinobu Inoue 				m->m_pkthdr.len--;
3328df8bae1dSRodney W. Grimes 			return;
3329df8bae1dSRodney W. Grimes 		}
3330df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
3331df8bae1dSRodney W. Grimes 		m = m->m_next;
33324dfdffe9SAndre Oppermann 		if (m == NULL)
3333df8bae1dSRodney W. Grimes 			break;
3334df8bae1dSRodney W. Grimes 	}
3335df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
3336df8bae1dSRodney W. Grimes }
3337df8bae1dSRodney W. Grimes 
3338df8bae1dSRodney W. Grimes /*
3339df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
3340df8bae1dSRodney W. Grimes  * and update averages and current timeout.
3341df8bae1dSRodney W. Grimes  */
334255bceb1eSRandall Stewart void
3343ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt)
3344df8bae1dSRodney W. Grimes {
3345ad3f9ab3SAndre Oppermann 	int delta;
3346233e8c18SGarrett Wollman 
33478501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
33482be3bf22SRobert Watson 
334978b50714SRobert Watson 	TCPSTAT_INC(tcps_rttupdated);
3350233e8c18SGarrett Wollman 	tp->t_rttupdated++;
33515ede40dcSRyan Stone 	if ((tp->t_srtt != 0) && (tp->t_rxtshift <= TCP_RTT_INVALIDATE)) {
3352233e8c18SGarrett Wollman 		/*
3353233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
3354233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
3355233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
3356233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
3357233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
3358233e8c18SGarrett Wollman 		 */
3359233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
3360233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
3361233e8c18SGarrett Wollman 
3362233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
3363233e8c18SGarrett Wollman 			tp->t_srtt = 1;
3364233e8c18SGarrett Wollman 
3365233e8c18SGarrett Wollman 		/*
3366233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
3367233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
3368233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
3369233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
3370233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
3371233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
3372233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
3373233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
3374233e8c18SGarrett Wollman 		 */
3375233e8c18SGarrett Wollman 		if (delta < 0)
3376233e8c18SGarrett Wollman 			delta = -delta;
3377233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
3378233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
3379233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
33801fcc99b5SMatthew Dillon 		if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
33811fcc99b5SMatthew Dillon 		    tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3382233e8c18SGarrett Wollman 	} else {
3383233e8c18SGarrett Wollman 		/*
3384233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
3385233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
3386233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
3387233e8c18SGarrett Wollman 		 */
3388233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
3389233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
33901fcc99b5SMatthew Dillon 		tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3391233e8c18SGarrett Wollman 	}
33929b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
3393df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
3394df8bae1dSRodney W. Grimes 
3395df8bae1dSRodney W. Grimes 	/*
3396df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
3397df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
3398df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
3399df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
3400df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
3401df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
3402df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
3403df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
3404df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
3405df8bae1dSRodney W. Grimes 	 */
3406233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
34079e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
3408df8bae1dSRodney W. Grimes 
3409df8bae1dSRodney W. Grimes 	/*
3410df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
3411df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
3412df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
3413df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
3414df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
3415df8bae1dSRodney W. Grimes 	 */
3416df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
3417df8bae1dSRodney W. Grimes }
3418df8bae1dSRodney W. Grimes 
3419df8bae1dSRodney W. Grimes /*
3420df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
3421df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
34224faaea55SAndre Oppermann  * If none, use an mss that can be handled on the outgoing interface
34234faaea55SAndre Oppermann  * without forcing IP to fragment.  If no route is found, route has no mtu,
3424df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
3425df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
3426df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
3427df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
3428df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
3429df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
3430df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
3431a0292f23SGarrett Wollman  *
34320c39d38dSGleb Smirnoff  * NOTE that resulting t_maxseg doesn't include space for TCP options or
34330c39d38dSGleb Smirnoff  * IP options, e.g. IPSEC data, since length of this data may vary, and
34340c39d38dSGleb Smirnoff  * thus it is calculated for every segment separately in tcp_output().
3435a0292f23SGarrett Wollman  *
343697d8d152SAndre Oppermann  * NOTE that this routine is only called when we process an incoming
3437ef341ee1SGleb Smirnoff  * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS
3438ef341ee1SGleb Smirnoff  * settings are handled in tcp_mssopt().
3439df8bae1dSRodney W. Grimes  */
3440a0292f23SGarrett Wollman void
3441ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer,
34423c914c54SAndre Oppermann     struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap)
3443df8bae1dSRodney W. Grimes {
3444b287c6c7SBjoern A. Zeeb 	int mss = 0;
34453ac12506SJonathan T. Looney 	uint32_t maxmtu = 0;
3446c068736aSJeffrey Hsu 	struct inpcb *inp = tp->t_inpcb;
344797d8d152SAndre Oppermann 	struct hc_metrics_lite metrics;
3448fb59c426SYoshinobu Inoue #ifdef INET6
3449c068736aSJeffrey Hsu 	int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
3450c068736aSJeffrey Hsu 	size_t min_protoh = isipv6 ?
3451c068736aSJeffrey Hsu 			    sizeof (struct ip6_hdr) + sizeof (struct tcphdr) :
3452c068736aSJeffrey Hsu 			    sizeof (struct tcpiphdr);
3453c068736aSJeffrey Hsu #else
3454c068736aSJeffrey Hsu 	const size_t min_protoh = sizeof(struct tcpiphdr);
3455fb59c426SYoshinobu Inoue #endif
3456df8bae1dSRodney W. Grimes 
34578501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
34587a3244ccSRobert Watson 
3459ef341ee1SGleb Smirnoff 	if (mtuoffer != -1) {
3460ef341ee1SGleb Smirnoff 		KASSERT(offer == -1, ("%s: conflict", __func__));
3461ef341ee1SGleb Smirnoff 		offer = mtuoffer - min_protoh;
3462ef341ee1SGleb Smirnoff 	}
3463ef341ee1SGleb Smirnoff 
3464e8949f74SAndre Oppermann 	/* Initialize. */
346597d8d152SAndre Oppermann #ifdef INET6
346697d8d152SAndre Oppermann 	if (isipv6) {
34673c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu6(&inp->inp_inc, cap);
34680c39d38dSGleb Smirnoff 		tp->t_maxseg = V_tcp_v6mssdflt;
3469b287c6c7SBjoern A. Zeeb 	}
347097d8d152SAndre Oppermann #endif
3471b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3472b287c6c7SBjoern A. Zeeb 	else
3473b287c6c7SBjoern A. Zeeb #endif
3474b287c6c7SBjoern A. Zeeb #ifdef INET
347597d8d152SAndre Oppermann 	{
34763c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu(&inp->inp_inc, cap);
34770c39d38dSGleb Smirnoff 		tp->t_maxseg = V_tcp_mssdflt;
3478df8bae1dSRodney W. Grimes 	}
3479b287c6c7SBjoern A. Zeeb #endif
3480df8bae1dSRodney W. Grimes 
348197d8d152SAndre Oppermann 	/*
3482e8949f74SAndre Oppermann 	 * No route to sender, stay with default mss and return.
348397d8d152SAndre Oppermann 	 */
34846f01cac6SBjoern A. Zeeb 	if (maxmtu == 0) {
34856f01cac6SBjoern A. Zeeb 		/*
34868e5c87f4SBjoern A. Zeeb 		 * In case we return early we need to initialize metrics
34876f01cac6SBjoern A. Zeeb 		 * to a defined state as tcp_hc_get() would do for us
34886f01cac6SBjoern A. Zeeb 		 * if there was no cache hit.
34896f01cac6SBjoern A. Zeeb 		 */
34906f01cac6SBjoern A. Zeeb 		if (metricptr != NULL)
34916f01cac6SBjoern A. Zeeb 			bzero(metricptr, sizeof(struct hc_metrics_lite));
349297d8d152SAndre Oppermann 		return;
34936f01cac6SBjoern A. Zeeb 	}
349497d8d152SAndre Oppermann 
3495e8949f74SAndre Oppermann 	/* What have we got? */
349697d8d152SAndre Oppermann 	switch (offer) {
349797d8d152SAndre Oppermann 		case 0:
349897d8d152SAndre Oppermann 			/*
349997d8d152SAndre Oppermann 			 * Offer == 0 means that there was no MSS on the SYN
3500c3b02504SBjoern A. Zeeb 			 * segment, in this case we use tcp_mssdflt as
35010c39d38dSGleb Smirnoff 			 * already assigned to t_maxseg above.
350297d8d152SAndre Oppermann 			 */
35030c39d38dSGleb Smirnoff 			offer = tp->t_maxseg;
350497d8d152SAndre Oppermann 			break;
350597d8d152SAndre Oppermann 
350697d8d152SAndre Oppermann 		case -1:
3507a0292f23SGarrett Wollman 			/*
3508c94c54e4SAndre Oppermann 			 * Offer == -1 means that we didn't receive SYN yet.
3509a0292f23SGarrett Wollman 			 */
351097d8d152SAndre Oppermann 			/* FALLTHROUGH */
351197d8d152SAndre Oppermann 
351297d8d152SAndre Oppermann 		default:
3513a0292f23SGarrett Wollman 			/*
351453369ac9SAndre Oppermann 			 * Prevent DoS attack with too small MSS. Round up
351553369ac9SAndre Oppermann 			 * to at least minmss.
351653369ac9SAndre Oppermann 			 */
3517603724d3SBjoern A. Zeeb 			offer = max(offer, V_tcp_minmss);
351897d8d152SAndre Oppermann 	}
3519a0292f23SGarrett Wollman 
3520df8bae1dSRodney W. Grimes 	/*
3521e8949f74SAndre Oppermann 	 * rmx information is now retrieved from tcp_hostcache.
3522df8bae1dSRodney W. Grimes 	 */
352397d8d152SAndre Oppermann 	tcp_hc_get(&inp->inp_inc, &metrics);
35243cee92e0SBjoern A. Zeeb 	if (metricptr != NULL)
35253cee92e0SBjoern A. Zeeb 		bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite));
352697d8d152SAndre Oppermann 
3527df8bae1dSRodney W. Grimes 	/*
3528cc412412SHiren Panchasara 	 * If there's a discovered mtu in tcp hostcache, use it.
3529cc412412SHiren Panchasara 	 * Else, use the link mtu.
3530df8bae1dSRodney W. Grimes 	 */
353197d8d152SAndre Oppermann 	if (metrics.rmx_mtu)
35322d166c02SAndre Oppermann 		mss = min(metrics.rmx_mtu, maxmtu) - min_protoh;
3533c068736aSJeffrey Hsu 	else {
353431b3783cSHajimu UMEMOTO #ifdef INET6
3535fb59c426SYoshinobu Inoue 		if (isipv6) {
353697d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3537603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
353897d8d152SAndre Oppermann 			    !in6_localaddr(&inp->in6p_faddr))
3539603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_v6mssdflt);
3540b287c6c7SBjoern A. Zeeb 		}
3541b3399803SHajimu UMEMOTO #endif
3542b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3543b287c6c7SBjoern A. Zeeb 		else
3544b287c6c7SBjoern A. Zeeb #endif
3545b287c6c7SBjoern A. Zeeb #ifdef INET
354697d8d152SAndre Oppermann 		{
354797d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3548603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
354997d8d152SAndre Oppermann 			    !in_localaddr(inp->inp_faddr))
3550603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_mssdflt);
3551a0292f23SGarrett Wollman 		}
3552b287c6c7SBjoern A. Zeeb #endif
35533cee92e0SBjoern A. Zeeb 		/*
35543cee92e0SBjoern A. Zeeb 		 * XXX - The above conditional (mss = maxmtu - min_protoh)
35553cee92e0SBjoern A. Zeeb 		 * probably violates the TCP spec.
35563cee92e0SBjoern A. Zeeb 		 * The problem is that, since we don't know the
35573cee92e0SBjoern A. Zeeb 		 * other end's MSS, we are supposed to use a conservative
35583cee92e0SBjoern A. Zeeb 		 * default.  But, if we do that, then MTU discovery will
35593cee92e0SBjoern A. Zeeb 		 * never actually take place, because the conservative
35603cee92e0SBjoern A. Zeeb 		 * default is much less than the MTUs typically seen
35613cee92e0SBjoern A. Zeeb 		 * on the Internet today.  For the moment, we'll sweep
35623cee92e0SBjoern A. Zeeb 		 * this under the carpet.
35633cee92e0SBjoern A. Zeeb 		 *
35643cee92e0SBjoern A. Zeeb 		 * The conservative default might not actually be a problem
35653cee92e0SBjoern A. Zeeb 		 * if the only case this occurs is when sending an initial
35663cee92e0SBjoern A. Zeeb 		 * SYN with options and data to a host we've never talked
35673cee92e0SBjoern A. Zeeb 		 * to before.  Then, they will reply with an MSS value which
35683cee92e0SBjoern A. Zeeb 		 * will get recorded and the new parameters should get
35693cee92e0SBjoern A. Zeeb 		 * recomputed.  For Further Study.
35703cee92e0SBjoern A. Zeeb 		 */
357197d8d152SAndre Oppermann 	}
3572a0292f23SGarrett Wollman 	mss = min(mss, offer);
357397d8d152SAndre Oppermann 
3574a0292f23SGarrett Wollman 	/*
35750c39d38dSGleb Smirnoff 	 * Sanity check: make sure that maxseg will be large
35763cee92e0SBjoern A. Zeeb 	 * enough to allow some data on segments even if the
35773cee92e0SBjoern A. Zeeb 	 * all the option space is used (40bytes).  Otherwise
35783cee92e0SBjoern A. Zeeb 	 * funny things may happen in tcp_output.
35790c39d38dSGleb Smirnoff 	 *
35800c39d38dSGleb Smirnoff 	 * XXXGL: shouldn't we reserve space for IP/IPv6 options?
35813cee92e0SBjoern A. Zeeb 	 */
35823cee92e0SBjoern A. Zeeb 	mss = max(mss, 64);
35833cee92e0SBjoern A. Zeeb 
358497d8d152SAndre Oppermann 	tp->t_maxseg = mss;
35853cee92e0SBjoern A. Zeeb }
35863cee92e0SBjoern A. Zeeb 
35873cee92e0SBjoern A. Zeeb void
35883cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer)
35893cee92e0SBjoern A. Zeeb {
3590dbc42409SLawrence Stewart 	int mss;
35913ac12506SJonathan T. Looney 	uint32_t bufsize;
35923cee92e0SBjoern A. Zeeb 	struct inpcb *inp;
35933cee92e0SBjoern A. Zeeb 	struct socket *so;
35943cee92e0SBjoern A. Zeeb 	struct hc_metrics_lite metrics;
35953c914c54SAndre Oppermann 	struct tcp_ifcap cap;
3596dbc42409SLawrence Stewart 
35973cee92e0SBjoern A. Zeeb 	KASSERT(tp != NULL, ("%s: tp == NULL", __func__));
35983cee92e0SBjoern A. Zeeb 
35993c914c54SAndre Oppermann 	bzero(&cap, sizeof(cap));
36003c914c54SAndre Oppermann 	tcp_mss_update(tp, offer, -1, &metrics, &cap);
36013cee92e0SBjoern A. Zeeb 
36023cee92e0SBjoern A. Zeeb 	mss = tp->t_maxseg;
36033cee92e0SBjoern A. Zeeb 	inp = tp->t_inpcb;
360497d8d152SAndre Oppermann 
3605df8bae1dSRodney W. Grimes 	/*
360697d8d152SAndre Oppermann 	 * If there's a pipesize, change the socket buffer to that size,
360797d8d152SAndre Oppermann 	 * don't change if sb_hiwat is different than default (then it
360897d8d152SAndre Oppermann 	 * has been changed on purpose with setsockopt).
360997d8d152SAndre Oppermann 	 * Make the socket buffers an integral number of mss units;
361097d8d152SAndre Oppermann 	 * if the mss is larger than the socket buffer, decrease the mss.
3611df8bae1dSRodney W. Grimes 	 */
3612c3b02504SBjoern A. Zeeb 	so = inp->inp_socket;
36133f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
3614e233e2acSAndre Oppermann 	if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe)
361597d8d152SAndre Oppermann 		bufsize = metrics.rmx_sendpipe;
361697d8d152SAndre Oppermann 	else
3617df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
3618df8bae1dSRodney W. Grimes 	if (bufsize < mss)
3619df8bae1dSRodney W. Grimes 		mss = bufsize;
3620df8bae1dSRodney W. Grimes 	else {
3621df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3622df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3623df8bae1dSRodney W. Grimes 			bufsize = sb_max;
362488c39af3SRuslan Ermilov 		if (bufsize > so->so_snd.sb_hiwat)
36253f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_snd, bufsize, so, NULL);
3626df8bae1dSRodney W. Grimes 	}
36273f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
3628784ce8faSHiren Panchasara 	/*
3629784ce8faSHiren Panchasara 	 * Sanity check: make sure that maxseg will be large
3630784ce8faSHiren Panchasara 	 * enough to allow some data on segments even if the
3631784ce8faSHiren Panchasara 	 * all the option space is used (40bytes).  Otherwise
3632784ce8faSHiren Panchasara 	 * funny things may happen in tcp_output.
3633784ce8faSHiren Panchasara 	 *
3634784ce8faSHiren Panchasara 	 * XXXGL: shouldn't we reserve space for IP/IPv6 options?
3635784ce8faSHiren Panchasara 	 */
3636784ce8faSHiren Panchasara 	tp->t_maxseg = max(mss, 64);
3637df8bae1dSRodney W. Grimes 
36383f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
3639e233e2acSAndre Oppermann 	if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe)
364097d8d152SAndre Oppermann 		bufsize = metrics.rmx_recvpipe;
364197d8d152SAndre Oppermann 	else
3642df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
3643df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
3644df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3645df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3646df8bae1dSRodney W. Grimes 			bufsize = sb_max;
364788c39af3SRuslan Ermilov 		if (bufsize > so->so_rcv.sb_hiwat)
36483f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL);
3649df8bae1dSRodney W. Grimes 	}
36503f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
365191d6cfa6SBjoern A. Zeeb 
365291d6cfa6SBjoern A. Zeeb 	/* Check the interface for TSO capabilities. */
36533c914c54SAndre Oppermann 	if (cap.ifcap & CSUM_TSO) {
365491d6cfa6SBjoern A. Zeeb 		tp->t_flags |= TF_TSO;
36553c914c54SAndre Oppermann 		tp->t_tsomax = cap.tsomax;
36569fd573c3SHans Petter Selasky 		tp->t_tsomaxsegcount = cap.tsomaxsegcount;
36579fd573c3SHans Petter Selasky 		tp->t_tsomaxsegsize = cap.tsomaxsegsize;
36583c914c54SAndre Oppermann 	}
3659a0292f23SGarrett Wollman }
3660a0292f23SGarrett Wollman 
3661a0292f23SGarrett Wollman /*
3662a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
3663a0292f23SGarrett Wollman  */
3664a0292f23SGarrett Wollman int
3665ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc)
3666a0292f23SGarrett Wollman {
366797d8d152SAndre Oppermann 	int mss = 0;
36683ac12506SJonathan T. Looney 	uint32_t thcmtu = 0;
36693ac12506SJonathan T. Looney 	uint32_t maxmtu = 0;
367097d8d152SAndre Oppermann 	size_t min_protoh;
3671a0292f23SGarrett Wollman 
367297d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer"));
3673a0292f23SGarrett Wollman 
367431b3783cSHajimu UMEMOTO #ifdef INET6
3675dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
3676603724d3SBjoern A. Zeeb 		mss = V_tcp_v6mssdflt;
3677233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu6(inc, NULL);
367897d8d152SAndre Oppermann 		min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
3679b287c6c7SBjoern A. Zeeb 	}
368031b3783cSHajimu UMEMOTO #endif
3681b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3682b287c6c7SBjoern A. Zeeb 	else
3683b287c6c7SBjoern A. Zeeb #endif
3684b287c6c7SBjoern A. Zeeb #ifdef INET
368597d8d152SAndre Oppermann 	{
3686603724d3SBjoern A. Zeeb 		mss = V_tcp_mssdflt;
3687233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu(inc, NULL);
368897d8d152SAndre Oppermann 		min_protoh = sizeof(struct tcpiphdr);
368997d8d152SAndre Oppermann 	}
3690b287c6c7SBjoern A. Zeeb #endif
36913a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET)
36923a9391deSBjoern A. Zeeb 	thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
36933a9391deSBjoern A. Zeeb #endif
36943a9391deSBjoern A. Zeeb 
369597d8d152SAndre Oppermann 	if (maxmtu && thcmtu)
369697d8d152SAndre Oppermann 		mss = min(maxmtu, thcmtu) - min_protoh;
369797d8d152SAndre Oppermann 	else if (maxmtu || thcmtu)
369897d8d152SAndre Oppermann 		mss = max(maxmtu, thcmtu) - min_protoh;
369997d8d152SAndre Oppermann 
370097d8d152SAndre Oppermann 	return (mss);
3701df8bae1dSRodney W. Grimes }
370246f58482SJonathan Lemon 
370346f58482SJonathan Lemon 
370446f58482SJonathan Lemon /*
3705c068736aSJeffrey Hsu  * On a partial ack arrives, force the retransmission of the
3706c068736aSJeffrey Hsu  * next unacknowledged segment.  Do not clear tp->t_dupacks.
3707c068736aSJeffrey Hsu  * By setting snd_nxt to ti_ack, this forces retransmission timer to
3708c068736aSJeffrey Hsu  * be started again.
370946f58482SJonathan Lemon  */
371055bceb1eSRandall Stewart void
3711ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th)
371246f58482SJonathan Lemon {
371346f58482SJonathan Lemon 	tcp_seq onxt = tp->snd_nxt;
37143ac12506SJonathan T. Looney 	uint32_t ocwnd = tp->snd_cwnd;
37150c39d38dSGleb Smirnoff 	u_int maxseg = tcp_maxseg(tp);
371646f58482SJonathan Lemon 
37178501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
37187a3244ccSRobert Watson 
3719b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_REXMT, 0);
372046f58482SJonathan Lemon 	tp->t_rtttime = 0;
372146f58482SJonathan Lemon 	tp->snd_nxt = th->th_ack;
37226b2a5f92SJayanth Vijayaraghavan 	/*
3723c068736aSJeffrey Hsu 	 * Set snd_cwnd to one segment beyond acknowledged offset.
3724c068736aSJeffrey Hsu 	 * (tp->snd_una has not yet been updated when this function is called.)
37256b2a5f92SJayanth Vijayaraghavan 	 */
37260c39d38dSGleb Smirnoff 	tp->snd_cwnd = maxseg + BYTES_THIS_ACK(tp, th);
3727a84db8f4SMatthew Dillon 	tp->t_flags |= TF_ACKNOW;
372855bceb1eSRandall Stewart 	(void) tp->t_fb->tfb_tcp_output(tp);
372946f58482SJonathan Lemon 	tp->snd_cwnd = ocwnd;
373046f58482SJonathan Lemon 	if (SEQ_GT(onxt, tp->snd_nxt))
373146f58482SJonathan Lemon 		tp->snd_nxt = onxt;
373246f58482SJonathan Lemon 	/*
373346f58482SJonathan Lemon 	 * Partial window deflation.  Relies on fact that tp->snd_una
373446f58482SJonathan Lemon 	 * not updated yet.
373546f58482SJonathan Lemon 	 */
3736dbc42409SLawrence Stewart 	if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th))
3737dbc42409SLawrence Stewart 		tp->snd_cwnd -= BYTES_THIS_ACK(tp, th);
3738d7587117SPaul Saab 	else
3739d7587117SPaul Saab 		tp->snd_cwnd = 0;
37400c39d38dSGleb Smirnoff 	tp->snd_cwnd += maxseg;
374146f58482SJonathan Lemon }
374212eeb81fSHiren Panchasara 
374312eeb81fSHiren Panchasara int
374412eeb81fSHiren Panchasara tcp_compute_pipe(struct tcpcb *tp)
374512eeb81fSHiren Panchasara {
374612eeb81fSHiren Panchasara 	return (tp->snd_max - tp->snd_una +
374712eeb81fSHiren Panchasara 		tp->sackhint.sack_bytes_rexmit -
374812eeb81fSHiren Panchasara 		tp->sackhint.sacked_bytes);
374912eeb81fSHiren Panchasara }
37507dc90a1dSMichael Tuexen 
37517dc90a1dSMichael Tuexen uint32_t
37527dc90a1dSMichael Tuexen tcp_compute_initwnd(uint32_t maxseg)
37537dc90a1dSMichael Tuexen {
37547dc90a1dSMichael Tuexen 	/*
37557dc90a1dSMichael Tuexen 	 * Calculate the Initial Window, also used as Restart Window
37567dc90a1dSMichael Tuexen 	 *
37577dc90a1dSMichael Tuexen 	 * RFC5681 Section 3.1 specifies the default conservative values.
37587dc90a1dSMichael Tuexen 	 * RFC3390 specifies slightly more aggressive values.
37597dc90a1dSMichael Tuexen 	 * RFC6928 increases it to ten segments.
37607dc90a1dSMichael Tuexen 	 * Support for user specified value for initial flight size.
37617dc90a1dSMichael Tuexen 	 */
37627dc90a1dSMichael Tuexen 	if (V_tcp_initcwnd_segments)
37637dc90a1dSMichael Tuexen 		return min(V_tcp_initcwnd_segments * maxseg,
37647dc90a1dSMichael Tuexen 		    max(2 * maxseg, V_tcp_initcwnd_segments * 1460));
37657dc90a1dSMichael Tuexen 	else if (V_tcp_do_rfc3390)
37667dc90a1dSMichael Tuexen 		return min(4 * maxseg, max(2 * maxseg, 4380));
37677dc90a1dSMichael Tuexen 	else {
37687dc90a1dSMichael Tuexen 		/* Per RFC5681 Section 3.1 */
37697dc90a1dSMichael Tuexen 		if (maxseg > 2190)
37707dc90a1dSMichael Tuexen 			return (2 * maxseg);
37717dc90a1dSMichael Tuexen 		else if (maxseg > 1095)
37727dc90a1dSMichael Tuexen 			return (3 * maxseg);
37737dc90a1dSMichael Tuexen 		else
37747dc90a1dSMichael Tuexen 			return (4 * maxseg);
37757dc90a1dSMichael Tuexen 	}
37767dc90a1dSMichael Tuexen }
3777