xref: /freebsd/sys/netinet/tcp_input.c (revision 2de3e790f537301e0a1cf202d022afca122acdbc)
1c398230bSWarner Losh /*-
2e79adb8eSGarrett Wollman  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4dbc42409SLawrence Stewart  * Copyright (c) 2007-2008,2010
5dbc42409SLawrence Stewart  *	Swinburne University of Technology, Melbourne, Australia.
6dbc42409SLawrence Stewart  * Copyright (c) 2009-2010 Lawrence Stewart <lstewart@freebsd.org>
7dbc42409SLawrence Stewart  * Copyright (c) 2010 The FreeBSD Foundation
8fa046d87SRobert Watson  * Copyright (c) 2010-2011 Juniper Networks, Inc.
9dbc42409SLawrence Stewart  * All rights reserved.
10dbc42409SLawrence Stewart  *
11dbc42409SLawrence Stewart  * Portions of this software were developed at the Centre for Advanced Internet
12891b8ed4SLawrence Stewart  * Architectures, Swinburne University of Technology, by Lawrence Stewart,
13891b8ed4SLawrence Stewart  * James Healy and David Hayes, made possible in part by a grant from the Cisco
14891b8ed4SLawrence Stewart  * University Research Program Fund at Community Foundation Silicon Valley.
15dbc42409SLawrence Stewart  *
16dbc42409SLawrence Stewart  * Portions of this software were developed at the Centre for Advanced
17dbc42409SLawrence Stewart  * Internet Architectures, Swinburne University of Technology, Melbourne,
18dbc42409SLawrence Stewart  * Australia by David Hayes under sponsorship from the FreeBSD Foundation.
19df8bae1dSRodney W. Grimes  *
20fa046d87SRobert Watson  * Portions of this software were developed by Robert N. M. Watson under
21fa046d87SRobert Watson  * contract to Juniper Networks, Inc.
22fa046d87SRobert Watson  *
23df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
24df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
25df8bae1dSRodney W. Grimes  * are met:
26df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
27df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
28df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
29df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
30df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
31df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
32df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
33df8bae1dSRodney W. Grimes  *    without specific prior written permission.
34df8bae1dSRodney W. Grimes  *
35df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
46df8bae1dSRodney W. Grimes  *
47e79adb8eSGarrett Wollman  *	@(#)tcp_input.c	8.12 (Berkeley) 5/24/95
48df8bae1dSRodney W. Grimes  */
49df8bae1dSRodney W. Grimes 
504b421e2dSMike Silbersack #include <sys/cdefs.h>
514b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
524b421e2dSMike Silbersack 
53f9e354dfSJulian Elischer #include "opt_ipfw.h"		/* for ipfw_fwd	*/
541cfd4b53SBruce M Simpson #include "opt_inet.h"
55fb59c426SYoshinobu Inoue #include "opt_inet6.h"
563a2a9f79SYoshinobu Inoue #include "opt_ipsec.h"
570cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h"
580cc12cc5SJoerg Wunsch 
59df8bae1dSRodney W. Grimes #include <sys/param.h>
6098163b98SPoul-Henning Kamp #include <sys/kernel.h>
6139bc9de5SLawrence Stewart #include <sys/hhook.h>
62df8bae1dSRodney W. Grimes #include <sys/malloc.h>
63df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
64a29f300eSGarrett Wollman #include <sys/proc.h>		/* for proc0 declaration */
65df8bae1dSRodney W. Grimes #include <sys/protosw.h>
6657f60867SMark Johnston #include <sys/sdt.h>
67960ed29cSSeigo Tanimura #include <sys/signalvar.h>
68df8bae1dSRodney W. Grimes #include <sys/socket.h>
69df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
70960ed29cSSeigo Tanimura #include <sys/sysctl.h>
71816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
72960ed29cSSeigo Tanimura #include <sys/systm.h>
73df8bae1dSRodney W. Grimes 
74e79adb8eSGarrett Wollman #include <machine/cpu.h>	/* before tcp_seq.h, for tcp_random18() */
75e79adb8eSGarrett Wollman 
7612e2e970SAndre Oppermann #include <vm/uma.h>
7712e2e970SAndre Oppermann 
78df8bae1dSRodney W. Grimes #include <net/if.h>
7976039bc8SGleb Smirnoff #include <net/if_var.h>
80df8bae1dSRodney W. Grimes #include <net/route.h>
81530c0060SRobert Watson #include <net/vnet.h>
82df8bae1dSRodney W. Grimes 
83773673c1SAndre Oppermann #define TCPSTATES		/* for logging */
84773673c1SAndre Oppermann 
85df8bae1dSRodney W. Grimes #include <netinet/in.h>
8657f60867SMark Johnston #include <netinet/in_kdtrace.h>
87960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
88df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
89df8bae1dSRodney W. Grimes #include <netinet/ip.h>
908e8aab7aSAndre Oppermann #include <netinet/ip_icmp.h>	/* required for icmp_var.h */
91686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp_var.h>	/* for ICMP_BANDLIM */
92df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
93ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
94686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
95686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h>
96686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h>
97b66d74c1SGleb Smirnoff #include <netinet6/in6_var.h>
98960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h>
99960ed29cSSeigo Tanimura #include <netinet6/nd6.h>
100281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
101281a0fd4SPatrick Kelsey #include <netinet/tcp_fastopen.h>
102281a0fd4SPatrick Kelsey #endif
103*2de3e790SGleb Smirnoff #include <netinet/tcp.h>
104df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h>
105df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h>
106df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h>
107df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h>
108fb59c426SYoshinobu Inoue #include <netinet6/tcp6_var.h>
109df8bae1dSRodney W. Grimes #include <netinet/tcpip.h>
110*2de3e790SGleb Smirnoff #include <netinet/tcp_cc.h>
11186a996e6SHiren Panchasara #ifdef TCPPCAP
11286a996e6SHiren Panchasara #include <netinet/tcp_pcap.h>
11386a996e6SHiren Panchasara #endif
114c325962bSMike Silbersack #include <netinet/tcp_syncache.h>
115610ee2f9SDavid Greenman #ifdef TCPDEBUG
116df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h>
117fb59c426SYoshinobu Inoue #endif /* TCPDEBUG */
11809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
11909fe6320SNavdeep Parhar #include <netinet/tcp_offload.h>
12009fe6320SNavdeep Parhar #endif
121fb59c426SYoshinobu Inoue 
122b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
123b9234fafSSam Leffler #include <netipsec/ipsec.h>
124b9234fafSSam Leffler #include <netipsec/ipsec6.h>
125b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/
126b9234fafSSam Leffler 
127db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
128db4f9cc7SJonathan Lemon 
129aed55708SRobert Watson #include <security/mac/mac_framework.h>
130aed55708SRobert Watson 
131dbc42409SLawrence Stewart const int tcprexmtthresh = 3;
1320312fbe9SPoul-Henning Kamp 
133773673c1SAndre Oppermann int tcp_log_in_vain = 0;
134816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW,
135eddfbb76SRobert Watson     &tcp_log_in_vain, 0,
136eddfbb76SRobert Watson     "Log all incoming TCP segments to closed ports");
137816a3d83SPoul-Henning Kamp 
13882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, blackhole) = 0;
13982cea7e6SBjoern A. Zeeb #define	V_blackhole		VNET(blackhole)
1406df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW,
141eddfbb76SRobert Watson     &VNET_NAME(blackhole), 0,
142eddfbb76SRobert Watson     "Do not send RST on segments to closed ports");
14316f7f31fSGeoff Rehmet 
14482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_delack_enabled) = 1;
1456df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_VNET | CTLFLAG_RW,
146eddfbb76SRobert Watson     &VNET_NAME(tcp_delack_enabled), 0,
1473d177f46SBill Fumerola     "Delay ACK to try and piggyback it onto a data packet");
148f498eeeeSDavid Greenman 
14982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, drop_synfin) = 0;
15082cea7e6SBjoern A. Zeeb #define	V_drop_synfin		VNET(drop_synfin)
1516df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, drop_synfin, CTLFLAG_VNET | CTLFLAG_RW,
152eddfbb76SRobert Watson     &VNET_NAME(drop_synfin), 0,
153eddfbb76SRobert Watson     "Drop TCP packets with SYN+FIN set");
154f8613305SDag-Erling Smørgrav 
15512eeb81fSHiren Panchasara VNET_DEFINE(int, tcp_do_rfc6675_pipe) = 0;
156054d38e3SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc6675_pipe, CTLFLAG_VNET | CTLFLAG_RW,
15712eeb81fSHiren Panchasara     &VNET_NAME(tcp_do_rfc6675_pipe), 0,
15812eeb81fSHiren Panchasara     "Use calculated pipe/in-flight bytes per RFC 6675");
15912eeb81fSHiren Panchasara 
16082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3042) = 1;
16182cea7e6SBjoern A. Zeeb #define	V_tcp_do_rfc3042	VNET(tcp_do_rfc3042)
1626df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3042, CTLFLAG_VNET | CTLFLAG_RW,
163eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3042), 0,
164eddfbb76SRobert Watson     "Enable RFC 3042 (Limited Transmit)");
165582a954bSJeffrey Hsu 
16682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3390) = 1;
1676df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3390, CTLFLAG_VNET | CTLFLAG_RW,
168eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3390), 0,
169da3a8a1aSJeffrey Hsu     "Enable RFC 3390 (Increasing TCP's Initial Congestion Window)");
170da3a8a1aSJeffrey Hsu 
171356c7958SHiren Panchasara VNET_DEFINE(int, tcp_initcwnd_segments) = 10;
172356c7958SHiren Panchasara SYSCTL_INT(_net_inet_tcp, OID_AUTO, initcwnd_segments,
173356c7958SHiren Panchasara     CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(tcp_initcwnd_segments), 0,
174356c7958SHiren Panchasara     "Slow-start flight size (initial congestion window) in number of segments");
17509440655SAndre Oppermann 
17682cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_rfc3465) = 1;
1776df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, rfc3465, CTLFLAG_VNET | CTLFLAG_RW,
178eddfbb76SRobert Watson     &VNET_NAME(tcp_do_rfc3465), 0,
17924cb0f22SLawrence Stewart     "Enable RFC 3465 (Appropriate Byte Counting)");
180eddfbb76SRobert Watson 
18182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_abc_l_var) = 2;
1826df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, abc_l_var, CTLFLAG_VNET | CTLFLAG_RW,
183eddfbb76SRobert Watson     &VNET_NAME(tcp_abc_l_var), 2,
18424cb0f22SLawrence Stewart     "Cap the max cwnd increment during slow-start to this number of segments");
18524cb0f22SLawrence Stewart 
1866472ac3dSEd Schouten static SYSCTL_NODE(_net_inet_tcp, OID_AUTO, ecn, CTLFLAG_RW, 0, "TCP ECN");
187f2512ba1SRui Paulo 
18882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_ecn) = 0;
1896df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, enable, CTLFLAG_VNET | CTLFLAG_RW,
190eddfbb76SRobert Watson     &VNET_NAME(tcp_do_ecn), 0,
191eddfbb76SRobert Watson     "TCP ECN support");
192eddfbb76SRobert Watson 
19382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_ecn_maxretries) = 1;
1946df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp_ecn, OID_AUTO, maxretries, CTLFLAG_VNET | CTLFLAG_RW,
195eddfbb76SRobert Watson     &VNET_NAME(tcp_ecn_maxretries), 0,
196eddfbb76SRobert Watson     "Max retries before giving up on ECN");
197eddfbb76SRobert Watson 
1983220a212SGleb Smirnoff VNET_DEFINE(int, tcp_insecure_syn) = 0;
1993220a212SGleb Smirnoff #define	V_tcp_insecure_syn	VNET(tcp_insecure_syn)
2006df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_syn, CTLFLAG_VNET | CTLFLAG_RW,
2013220a212SGleb Smirnoff     &VNET_NAME(tcp_insecure_syn), 0,
2023220a212SGleb Smirnoff     "Follow RFC793 instead of RFC5961 criteria for accepting SYN packets");
2033220a212SGleb Smirnoff 
20482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_insecure_rst) = 0;
20582cea7e6SBjoern A. Zeeb #define	V_tcp_insecure_rst	VNET(tcp_insecure_rst)
2066df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, insecure_rst, CTLFLAG_VNET | CTLFLAG_RW,
207eddfbb76SRobert Watson     &VNET_NAME(tcp_insecure_rst), 0,
2083220a212SGleb Smirnoff     "Follow RFC793 instead of RFC5961 criteria for accepting RST packets");
209a69968eeSMike Silbersack 
210fba0cea1SBjoern A. Zeeb VNET_DEFINE(int, tcp_recvspace) = 1024*64;
211873789cbSAndre Oppermann #define	V_tcp_recvspace	VNET(tcp_recvspace)
2126df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_VNET | CTLFLAG_RW,
213873789cbSAndre Oppermann     &VNET_NAME(tcp_recvspace), 0, "Initial receive socket buffer size");
214873789cbSAndre Oppermann 
21582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autorcvbuf) = 1;
21682cea7e6SBjoern A. Zeeb #define	V_tcp_do_autorcvbuf	VNET(tcp_do_autorcvbuf)
2176df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_auto, CTLFLAG_VNET | CTLFLAG_RW,
218eddfbb76SRobert Watson     &VNET_NAME(tcp_do_autorcvbuf), 0,
2198b615593SMarko Zec     "Enable automatic receive buffer sizing");
2206741ecf5SAndre Oppermann 
22182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_inc) = 16*1024;
22282cea7e6SBjoern A. Zeeb #define	V_tcp_autorcvbuf_inc	VNET(tcp_autorcvbuf_inc)
2236df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_inc, CTLFLAG_VNET | CTLFLAG_RW,
224eddfbb76SRobert Watson     &VNET_NAME(tcp_autorcvbuf_inc), 0,
2256489fe65SAndre Oppermann     "Incrementor step size of automatic receive buffer");
2266741ecf5SAndre Oppermann 
227b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autorcvbuf_max) = 2*1024*1024;
22882cea7e6SBjoern A. Zeeb #define	V_tcp_autorcvbuf_max	VNET(tcp_autorcvbuf_max)
2296df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, recvbuf_max, CTLFLAG_VNET | CTLFLAG_RW,
230eddfbb76SRobert Watson     &VNET_NAME(tcp_autorcvbuf_max), 0,
2318b615593SMarko Zec     "Max size of automatic receive buffer");
2326741ecf5SAndre Oppermann 
233eddfbb76SRobert Watson VNET_DEFINE(struct inpcbhead, tcb);
23444e33a07SMarko Zec #define	tcb6	tcb  /* for KAME src sync over BSD*'s */
23582cea7e6SBjoern A. Zeeb VNET_DEFINE(struct inpcbinfo, tcbinfo);
236df8bae1dSRodney W. Grimes 
237315e3e38SRobert Watson /*
2385da0521fSAndrey V. Elsukov  * TCP statistics are stored in an "array" of counter(9)s.
2395923c293SGleb Smirnoff  */
2405da0521fSAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct tcpstat, tcpstat);
2415da0521fSAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(tcpstat);
2425da0521fSAndrey V. Elsukov SYSCTL_VNET_PCPUSTAT(_net_inet_tcp, TCPCTL_STATS, stats, struct tcpstat,
2435da0521fSAndrey V. Elsukov     tcpstat, "TCP statistics (struct tcpstat, netinet/tcp_var.h)");
2445923c293SGleb Smirnoff 
2455923c293SGleb Smirnoff #ifdef VIMAGE
2465da0521fSAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(tcpstat);
2475923c293SGleb Smirnoff #endif /* VIMAGE */
2485923c293SGleb Smirnoff /*
249315e3e38SRobert Watson  * Kernel module interface for updating tcpstat.  The argument is an index
2505923c293SGleb Smirnoff  * into tcpstat treated as an array.
251315e3e38SRobert Watson  */
252315e3e38SRobert Watson void
253315e3e38SRobert Watson kmod_tcpstat_inc(int statnum)
254315e3e38SRobert Watson {
255315e3e38SRobert Watson 
2565da0521fSAndrey V. Elsukov 	counter_u64_add(VNET(tcpstat)[statnum], 1);
257315e3e38SRobert Watson }
258315e3e38SRobert Watson 
259dbc42409SLawrence Stewart /*
26039bc9de5SLawrence Stewart  * Wrapper for the TCP established input helper hook.
26139bc9de5SLawrence Stewart  */
26255bceb1eSRandall Stewart void
26339bc9de5SLawrence Stewart hhook_run_tcp_est_in(struct tcpcb *tp, struct tcphdr *th, struct tcpopt *to)
26439bc9de5SLawrence Stewart {
26539bc9de5SLawrence Stewart 	struct tcp_hhook_data hhook_data;
26639bc9de5SLawrence Stewart 
26739bc9de5SLawrence Stewart 	if (V_tcp_hhh[HHOOK_TCP_EST_IN]->hhh_nhooks > 0) {
26839bc9de5SLawrence Stewart 		hhook_data.tp = tp;
26939bc9de5SLawrence Stewart 		hhook_data.th = th;
27039bc9de5SLawrence Stewart 		hhook_data.to = to;
27139bc9de5SLawrence Stewart 
27239bc9de5SLawrence Stewart 		hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_IN], &hhook_data,
27339bc9de5SLawrence Stewart 		    tp->osd);
27439bc9de5SLawrence Stewart 	}
27539bc9de5SLawrence Stewart }
27639bc9de5SLawrence Stewart 
27739bc9de5SLawrence Stewart /*
278dbc42409SLawrence Stewart  * CC wrapper hook functions
279dbc42409SLawrence Stewart  */
28055bceb1eSRandall Stewart void
281dbc42409SLawrence Stewart cc_ack_received(struct tcpcb *tp, struct tcphdr *th, uint16_t type)
282f2512ba1SRui Paulo {
283dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
284f2512ba1SRui Paulo 
285dbc42409SLawrence Stewart 	tp->ccv->bytes_this_ack = BYTES_THIS_ACK(tp, th);
2865b648e79SLawrence Stewart 	if (tp->snd_cwnd <= tp->snd_wnd)
287dbc42409SLawrence Stewart 		tp->ccv->flags |= CCF_CWND_LIMITED;
288dbc42409SLawrence Stewart 	else
289dbc42409SLawrence Stewart 		tp->ccv->flags &= ~CCF_CWND_LIMITED;
290dbc42409SLawrence Stewart 
291dbc42409SLawrence Stewart 	if (type == CC_ACK) {
292dbc42409SLawrence Stewart 		if (tp->snd_cwnd > tp->snd_ssthresh) {
293dbc42409SLawrence Stewart 			tp->t_bytes_acked += min(tp->ccv->bytes_this_ack,
2940c39d38dSGleb Smirnoff 			     V_tcp_abc_l_var * tcp_maxseg(tp));
295dbc42409SLawrence Stewart 			if (tp->t_bytes_acked >= tp->snd_cwnd) {
296dbc42409SLawrence Stewart 				tp->t_bytes_acked -= tp->snd_cwnd;
297dbc42409SLawrence Stewart 				tp->ccv->flags |= CCF_ABC_SENTAWND;
298dbc42409SLawrence Stewart 			}
299dbc42409SLawrence Stewart 		} else {
300dbc42409SLawrence Stewart 				tp->ccv->flags &= ~CCF_ABC_SENTAWND;
301dbc42409SLawrence Stewart 				tp->t_bytes_acked = 0;
302dbc42409SLawrence Stewart 		}
303dbc42409SLawrence Stewart 	}
304dbc42409SLawrence Stewart 
305dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->ack_received != NULL) {
306dbc42409SLawrence Stewart 		/* XXXLAS: Find a way to live without this */
307dbc42409SLawrence Stewart 		tp->ccv->curack = th->th_ack;
308dbc42409SLawrence Stewart 		CC_ALGO(tp)->ack_received(tp->ccv, type);
309dbc42409SLawrence Stewart 	}
310dbc42409SLawrence Stewart }
311dbc42409SLawrence Stewart 
31255bceb1eSRandall Stewart void
313dbc42409SLawrence Stewart cc_conn_init(struct tcpcb *tp)
314dbc42409SLawrence Stewart {
315dbc42409SLawrence Stewart 	struct hc_metrics_lite metrics;
316dbc42409SLawrence Stewart 	struct inpcb *inp = tp->t_inpcb;
3170c39d38dSGleb Smirnoff 	u_int maxseg;
318dbc42409SLawrence Stewart 	int rtt;
319dbc42409SLawrence Stewart 
320dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
321dbc42409SLawrence Stewart 
322dbc42409SLawrence Stewart 	tcp_hc_get(&inp->inp_inc, &metrics);
3230c39d38dSGleb Smirnoff 	maxseg = tcp_maxseg(tp);
324dbc42409SLawrence Stewart 
325dbc42409SLawrence Stewart 	if (tp->t_srtt == 0 && (rtt = metrics.rmx_rtt)) {
326dbc42409SLawrence Stewart 		tp->t_srtt = rtt;
327dbc42409SLawrence Stewart 		tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE;
328dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_usedrtt);
329dbc42409SLawrence Stewart 		if (metrics.rmx_rttvar) {
330dbc42409SLawrence Stewart 			tp->t_rttvar = metrics.rmx_rttvar;
331dbc42409SLawrence Stewart 			TCPSTAT_INC(tcps_usedrttvar);
332dbc42409SLawrence Stewart 		} else {
333dbc42409SLawrence Stewart 			/* default variation is +- 1 rtt */
334dbc42409SLawrence Stewart 			tp->t_rttvar =
335dbc42409SLawrence Stewart 			    tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
336dbc42409SLawrence Stewart 		}
337dbc42409SLawrence Stewart 		TCPT_RANGESET(tp->t_rxtcur,
338dbc42409SLawrence Stewart 		    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
339dbc42409SLawrence Stewart 		    tp->t_rttmin, TCPTV_REXMTMAX);
340dbc42409SLawrence Stewart 	}
341dbc42409SLawrence Stewart 	if (metrics.rmx_ssthresh) {
342dbc42409SLawrence Stewart 		/*
343dbc42409SLawrence Stewart 		 * There's some sort of gateway or interface
344dbc42409SLawrence Stewart 		 * buffer limit on the path.  Use this to set
345dbc42409SLawrence Stewart 		 * the slow start threshhold, but set the
346dbc42409SLawrence Stewart 		 * threshold to no less than 2*mss.
347dbc42409SLawrence Stewart 		 */
3480c39d38dSGleb Smirnoff 		tp->snd_ssthresh = max(2 * maxseg, metrics.rmx_ssthresh);
349dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_usedssthresh);
350dbc42409SLawrence Stewart 	}
351dbc42409SLawrence Stewart 
352dbc42409SLawrence Stewart 	/*
3539ec4a4ccSAndre Oppermann 	 * Set the initial slow-start flight size.
354dbc42409SLawrence Stewart 	 *
355cf8f04f4SAndre Oppermann 	 * RFC5681 Section 3.1 specifies the default conservative values.
356cf8f04f4SAndre Oppermann 	 * RFC3390 specifies slightly more aggressive values.
357b8b4cfcdSSergey Kandaurov 	 * RFC6928 increases it to ten segments.
358356c7958SHiren Panchasara 	 * Support for user specified value for initial flight size.
359cf8f04f4SAndre Oppermann 	 *
360cf8f04f4SAndre Oppermann 	 * If a SYN or SYN/ACK was lost and retransmitted, we have to
361cf8f04f4SAndre Oppermann 	 * reduce the initial CWND to one segment as congestion is likely
362cf8f04f4SAndre Oppermann 	 * requiring us to be cautious.
363dbc42409SLawrence Stewart 	 */
364cf8f04f4SAndre Oppermann 	if (tp->snd_cwnd == 1)
3650c39d38dSGleb Smirnoff 		tp->snd_cwnd = maxseg;		/* SYN(-ACK) lost */
366356c7958SHiren Panchasara 	else if (V_tcp_initcwnd_segments)
3670c39d38dSGleb Smirnoff 		tp->snd_cwnd = min(V_tcp_initcwnd_segments * maxseg,
3680c39d38dSGleb Smirnoff 		    max(2 * maxseg, V_tcp_initcwnd_segments * 1460));
369cf8f04f4SAndre Oppermann 	else if (V_tcp_do_rfc3390)
3700c39d38dSGleb Smirnoff 		tp->snd_cwnd = min(4 * maxseg, max(2 * maxseg, 4380));
37122efabd4SAndre Oppermann 	else {
37222efabd4SAndre Oppermann 		/* Per RFC5681 Section 3.1 */
3730c39d38dSGleb Smirnoff 		if (maxseg > 2190)
3740c39d38dSGleb Smirnoff 			tp->snd_cwnd = 2 * maxseg;
3750c39d38dSGleb Smirnoff 		else if (maxseg > 1095)
3760c39d38dSGleb Smirnoff 			tp->snd_cwnd = 3 * maxseg;
377dbc42409SLawrence Stewart 		else
3780c39d38dSGleb Smirnoff 			tp->snd_cwnd = 4 * maxseg;
37922efabd4SAndre Oppermann 	}
380dbc42409SLawrence Stewart 
381dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->conn_init != NULL)
382dbc42409SLawrence Stewart 		CC_ALGO(tp)->conn_init(tp->ccv);
383dbc42409SLawrence Stewart }
384dbc42409SLawrence Stewart 
385dbc42409SLawrence Stewart void inline
386dbc42409SLawrence Stewart cc_cong_signal(struct tcpcb *tp, struct tcphdr *th, uint32_t type)
387dbc42409SLawrence Stewart {
3880c39d38dSGleb Smirnoff 	u_int maxseg;
3890c39d38dSGleb Smirnoff 
390dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
391dbc42409SLawrence Stewart 
392dbc42409SLawrence Stewart 	switch(type) {
393dbc42409SLawrence Stewart 	case CC_NDUPACK:
394dbc42409SLawrence Stewart 		if (!IN_FASTRECOVERY(tp->t_flags)) {
395f2512ba1SRui Paulo 			tp->snd_recover = tp->snd_max;
396f2512ba1SRui Paulo 			if (tp->t_flags & TF_ECN_PERMIT)
397f2512ba1SRui Paulo 				tp->t_flags |= TF_ECN_SND_CWR;
398f2512ba1SRui Paulo 		}
399dbc42409SLawrence Stewart 		break;
400dbc42409SLawrence Stewart 	case CC_ECN:
401dbc42409SLawrence Stewart 		if (!IN_CONGRECOVERY(tp->t_flags)) {
402dbc42409SLawrence Stewart 			TCPSTAT_INC(tcps_ecn_rcwnd);
403dbc42409SLawrence Stewart 			tp->snd_recover = tp->snd_max;
404dbc42409SLawrence Stewart 			if (tp->t_flags & TF_ECN_PERMIT)
405dbc42409SLawrence Stewart 				tp->t_flags |= TF_ECN_SND_CWR;
406dbc42409SLawrence Stewart 		}
407dbc42409SLawrence Stewart 		break;
408dbc42409SLawrence Stewart 	case CC_RTO:
4090c39d38dSGleb Smirnoff 		maxseg = tcp_maxseg(tp);
410dbc42409SLawrence Stewart 		tp->t_dupacks = 0;
411dbc42409SLawrence Stewart 		tp->t_bytes_acked = 0;
412dbc42409SLawrence Stewart 		EXIT_RECOVERY(tp->t_flags);
4136157935fSLawrence Stewart 		tp->snd_ssthresh = max(2, min(tp->snd_wnd, tp->snd_cwnd) / 2 /
4140c39d38dSGleb Smirnoff 		    maxseg) * maxseg;
4150c39d38dSGleb Smirnoff 		tp->snd_cwnd = maxseg;
416dbc42409SLawrence Stewart 		break;
417dbc42409SLawrence Stewart 	case CC_RTO_ERR:
418dbc42409SLawrence Stewart 		TCPSTAT_INC(tcps_sndrexmitbad);
419dbc42409SLawrence Stewart 		/* RTO was unnecessary, so reset everything. */
420dbc42409SLawrence Stewart 		tp->snd_cwnd = tp->snd_cwnd_prev;
421dbc42409SLawrence Stewart 		tp->snd_ssthresh = tp->snd_ssthresh_prev;
422dbc42409SLawrence Stewart 		tp->snd_recover = tp->snd_recover_prev;
423dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASFRECOVERY)
424dbc42409SLawrence Stewart 			ENTER_FASTRECOVERY(tp->t_flags);
425dbc42409SLawrence Stewart 		if (tp->t_flags & TF_WASCRECOVERY)
426dbc42409SLawrence Stewart 			ENTER_CONGRECOVERY(tp->t_flags);
427dbc42409SLawrence Stewart 		tp->snd_nxt = tp->snd_max;
428672dc4aeSJohn Baldwin 		tp->t_flags &= ~TF_PREVVALID;
429dbc42409SLawrence Stewart 		tp->t_badrxtwin = 0;
430dbc42409SLawrence Stewart 		break;
431dbc42409SLawrence Stewart 	}
432dbc42409SLawrence Stewart 
433dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->cong_signal != NULL) {
434dbc42409SLawrence Stewart 		if (th != NULL)
435dbc42409SLawrence Stewart 			tp->ccv->curack = th->th_ack;
436dbc42409SLawrence Stewart 		CC_ALGO(tp)->cong_signal(tp->ccv, type);
437dbc42409SLawrence Stewart 	}
438dbc42409SLawrence Stewart }
439dbc42409SLawrence Stewart 
44055bceb1eSRandall Stewart void inline
441dbc42409SLawrence Stewart cc_post_recovery(struct tcpcb *tp, struct tcphdr *th)
442dbc42409SLawrence Stewart {
443dbc42409SLawrence Stewart 	INP_WLOCK_ASSERT(tp->t_inpcb);
444dbc42409SLawrence Stewart 
445dbc42409SLawrence Stewart 	/* XXXLAS: KASSERT that we're in recovery? */
446dbc42409SLawrence Stewart 
447dbc42409SLawrence Stewart 	if (CC_ALGO(tp)->post_recovery != NULL) {
448dbc42409SLawrence Stewart 		tp->ccv->curack = th->th_ack;
449dbc42409SLawrence Stewart 		CC_ALGO(tp)->post_recovery(tp->ccv);
450dbc42409SLawrence Stewart 	}
451dbc42409SLawrence Stewart 	/* XXXLAS: EXIT_RECOVERY ? */
452dbc42409SLawrence Stewart 	tp->t_bytes_acked = 0;
453dbc42409SLawrence Stewart }
4540312fbe9SPoul-Henning Kamp 
4552903309aSAttilio Rao #ifdef TCP_SIGNATURE
4562903309aSAttilio Rao static inline int
4572903309aSAttilio Rao tcp_signature_verify_input(struct mbuf *m, int off0, int tlen, int optlen,
4582903309aSAttilio Rao     struct tcpopt *to, struct tcphdr *th, u_int tcpbflag)
4592903309aSAttilio Rao {
4602903309aSAttilio Rao 	int ret;
4612903309aSAttilio Rao 
4622903309aSAttilio Rao 	tcp_fields_to_net(th);
4632903309aSAttilio Rao 	ret = tcp_signature_verify(m, off0, tlen, optlen, to, th, tcpbflag);
4642903309aSAttilio Rao 	tcp_fields_to_host(th);
4652903309aSAttilio Rao 	return (ret);
4662903309aSAttilio Rao }
4672903309aSAttilio Rao #endif
4682903309aSAttilio Rao 
469df8bae1dSRodney W. Grimes /*
470262c1c1aSMatthew Dillon  * Indicate whether this ack should be delayed.  We can delay the ack if
47185536381SHiren Panchasara  * following conditions are met:
47285536381SHiren Panchasara  *	- There is no delayed ack timer in progress.
47385536381SHiren Panchasara  *	- Our last ack wasn't a 0-sized window. We never want to delay
47485536381SHiren Panchasara  *	  the ack that opens up a 0-sized window.
47585536381SHiren Panchasara  *	- LRO wasn't used for this segment. We make sure by checking that the
47685536381SHiren Panchasara  *	  segment size is not larger than the MSS.
477d8c85a26SJonathan Lemon  */
478c1e5a6e5SAndre Oppermann #define DELAY_ACK(tp, tlen)						\
479b8152ba7SAndre Oppermann 	((!tcp_timer_active(tp, TT_DELACK) &&				\
480a14c749fSJonathan Lemon 	    (tp->t_flags & TF_RXWIN0SENT) == 0) &&			\
4810c39d38dSGleb Smirnoff 	    (tlen <= tp->t_maxseg) &&					\
482603724d3SBjoern A. Zeeb 	    (V_tcp_delack_enabled || (tp->t_flags & TF_NEEDSYN)))
483d8c85a26SJonathan Lemon 
48464807b30SHiren Panchasara static void inline
48564807b30SHiren Panchasara cc_ecnpkt_handler(struct tcpcb *tp, struct tcphdr *th, uint8_t iptos)
48664807b30SHiren Panchasara {
48764807b30SHiren Panchasara 	INP_WLOCK_ASSERT(tp->t_inpcb);
48864807b30SHiren Panchasara 
48964807b30SHiren Panchasara 	if (CC_ALGO(tp)->ecnpkt_handler != NULL) {
49064807b30SHiren Panchasara 		switch (iptos & IPTOS_ECN_MASK) {
49164807b30SHiren Panchasara 		case IPTOS_ECN_CE:
49264807b30SHiren Panchasara 		    tp->ccv->flags |= CCF_IPHDR_CE;
49364807b30SHiren Panchasara 		    break;
49464807b30SHiren Panchasara 		case IPTOS_ECN_ECT0:
49564807b30SHiren Panchasara 		    tp->ccv->flags &= ~CCF_IPHDR_CE;
49664807b30SHiren Panchasara 		    break;
49764807b30SHiren Panchasara 		case IPTOS_ECN_ECT1:
49864807b30SHiren Panchasara 		    tp->ccv->flags &= ~CCF_IPHDR_CE;
49964807b30SHiren Panchasara 		    break;
50064807b30SHiren Panchasara 		}
50164807b30SHiren Panchasara 
50264807b30SHiren Panchasara 		if (th->th_flags & TH_CWR)
50364807b30SHiren Panchasara 			tp->ccv->flags |= CCF_TCPHDR_CWR;
50464807b30SHiren Panchasara 		else
50564807b30SHiren Panchasara 			tp->ccv->flags &= ~CCF_TCPHDR_CWR;
50664807b30SHiren Panchasara 
50764807b30SHiren Panchasara 		if (tp->t_flags & TF_DELACK)
50864807b30SHiren Panchasara 			tp->ccv->flags |= CCF_DELACK;
50964807b30SHiren Panchasara 		else
51064807b30SHiren Panchasara 			tp->ccv->flags &= ~CCF_DELACK;
51164807b30SHiren Panchasara 
51264807b30SHiren Panchasara 		CC_ALGO(tp)->ecnpkt_handler(tp->ccv);
51364807b30SHiren Panchasara 
51464807b30SHiren Panchasara 		if (tp->ccv->flags & CCF_ACKNOW)
51564807b30SHiren Panchasara 			tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
51664807b30SHiren Panchasara 	}
51764807b30SHiren Panchasara }
51864807b30SHiren Panchasara 
519df8bae1dSRodney W. Grimes /*
5208d573cc1SAndre Oppermann  * TCP input handling is split into multiple parts:
5218d573cc1SAndre Oppermann  *   tcp6_input is a thin wrapper around tcp_input for the extended
5228d573cc1SAndre Oppermann  *	ip6_protox[] call format in ip6_input
5238d573cc1SAndre Oppermann  *   tcp_input handles primary segment validation, inpcb lookup and
5248d573cc1SAndre Oppermann  *	SYN processing on listen sockets
5258d573cc1SAndre Oppermann  *   tcp_do_segment processes the ACK and text of the segment for
5268d573cc1SAndre Oppermann  *	establishing, established and closing connections
527df8bae1dSRodney W. Grimes  */
528fb59c426SYoshinobu Inoue #ifdef INET6
529fb59c426SYoshinobu Inoue int
530ad3f9ab3SAndre Oppermann tcp6_input(struct mbuf **mp, int *offp, int proto)
531fb59c426SYoshinobu Inoue {
532ad3f9ab3SAndre Oppermann 	struct mbuf *m = *mp;
53333841545SHajimu UMEMOTO 	struct in6_ifaddr *ia6;
5343e88eb90SAndrey V. Elsukov 	struct ip6_hdr *ip6;
535fb59c426SYoshinobu Inoue 
536fb59c426SYoshinobu Inoue 	IP6_EXTHDR_CHECK(m, *offp, sizeof(struct tcphdr), IPPROTO_DONE);
537fb59c426SYoshinobu Inoue 
538fb59c426SYoshinobu Inoue 	/*
539fb59c426SYoshinobu Inoue 	 * draft-itojun-ipv6-tcp-to-anycast
540fb59c426SYoshinobu Inoue 	 * better place to put this in?
541fb59c426SYoshinobu Inoue 	 */
5423e88eb90SAndrey V. Elsukov 	ip6 = mtod(m, struct ip6_hdr *);
5433e88eb90SAndrey V. Elsukov 	ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
54433841545SHajimu UMEMOTO 	if (ia6 && (ia6->ia6_flags & IN6_IFF_ANYCAST)) {
545fb59c426SYoshinobu Inoue 		struct ip6_hdr *ip6;
546fb59c426SYoshinobu Inoue 
5478c0fec80SRobert Watson 		ifa_free(&ia6->ia_ifa);
548fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
549fb59c426SYoshinobu Inoue 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR,
550fb59c426SYoshinobu Inoue 			    (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
5518f5a8818SKevin Lo 		return (IPPROTO_DONE);
552fb59c426SYoshinobu Inoue 	}
55377d396fdSMaksim Yevmenkin 	if (ia6)
55477d396fdSMaksim Yevmenkin 		ifa_free(&ia6->ia_ifa);
555fb59c426SYoshinobu Inoue 
5568f5a8818SKevin Lo 	return (tcp_input(mp, offp, proto));
557fb59c426SYoshinobu Inoue }
558b287c6c7SBjoern A. Zeeb #endif /* INET6 */
559fb59c426SYoshinobu Inoue 
5608f5a8818SKevin Lo int
5618f5a8818SKevin Lo tcp_input(struct mbuf **mp, int *offp, int proto)
562df8bae1dSRodney W. Grimes {
5638f5a8818SKevin Lo 	struct mbuf *m = *mp;
564b287c6c7SBjoern A. Zeeb 	struct tcphdr *th = NULL;
565ad3f9ab3SAndre Oppermann 	struct ip *ip = NULL;
566ad3f9ab3SAndre Oppermann 	struct inpcb *inp = NULL;
567302ce8d6SAndre Oppermann 	struct tcpcb *tp = NULL;
568302ce8d6SAndre Oppermann 	struct socket *so = NULL;
569e79adb8eSGarrett Wollman 	u_char *optp = NULL;
5708f5a8818SKevin Lo 	int off0;
57126f9a767SRodney W. Grimes 	int optlen = 0;
572b287c6c7SBjoern A. Zeeb #ifdef INET
573b287c6c7SBjoern A. Zeeb 	int len;
574b287c6c7SBjoern A. Zeeb #endif
575b287c6c7SBjoern A. Zeeb 	int tlen = 0, off;
576fb59c426SYoshinobu Inoue 	int drop_hdrlen;
577ad3f9ab3SAndre Oppermann 	int thflags;
578302ce8d6SAndre Oppermann 	int rstreason = 0;	/* For badport_bandlim accounting purposes */
5792903309aSAttilio Rao #ifdef TCP_SIGNATURE
5802903309aSAttilio Rao 	uint8_t sig_checked = 0;
5812903309aSAttilio Rao #endif
582b287c6c7SBjoern A. Zeeb 	uint8_t iptos = 0;
583c1de64a4SAndrey V. Elsukov 	struct m_tag *fwd_tag = NULL;
584c068736aSJeffrey Hsu #ifdef INET6
585302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6 = NULL;
586c068736aSJeffrey Hsu 	int isipv6;
587c068736aSJeffrey Hsu #else
588a160e630SAndre Oppermann 	const void *ip6 = NULL;
589b287c6c7SBjoern A. Zeeb #endif /* INET6 */
590302ce8d6SAndre Oppermann 	struct tcpopt to;		/* options in this segment */
591a160e630SAndre Oppermann 	char *s = NULL;			/* address and port logging */
592252ca428SRobert Watson 	int ti_locked;
593610ee2f9SDavid Greenman #ifdef TCPDEBUG
594c068736aSJeffrey Hsu 	/*
595c068736aSJeffrey Hsu 	 * The size of tcp_saveipgen must be the size of the max ip header,
596c068736aSJeffrey Hsu 	 * now IPv6.
597c068736aSJeffrey Hsu 	 */
59814739780SMaxim Konovalov 	u_char tcp_saveipgen[IP6_HDR_LEN];
599410bb1bfSLuigi Rizzo 	struct tcphdr tcp_savetcp;
600610ee2f9SDavid Greenman 	short ostate = 0;
601610ee2f9SDavid Greenman #endif
602c068736aSJeffrey Hsu 
603fb59c426SYoshinobu Inoue #ifdef INET6
604fb59c426SYoshinobu Inoue 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
605fb59c426SYoshinobu Inoue #endif
606a0292f23SGarrett Wollman 
6078f5a8818SKevin Lo 	off0 = *offp;
6088f5a8818SKevin Lo 	m = *mp;
6098f5a8818SKevin Lo 	*mp = NULL;
610302ce8d6SAndre Oppermann 	to.to_flags = 0;
61178b50714SRobert Watson 	TCPSTAT_INC(tcps_rcvtotal);
612fb59c426SYoshinobu Inoue 
61304d3a452SHajimu UMEMOTO #ifdef INET6
614b287c6c7SBjoern A. Zeeb 	if (isipv6) {
6158d573cc1SAndre Oppermann 		/* IP6_EXTHDR_CHECK() is already done at tcp6_input(). */
61645747ba5SBjoern A. Zeeb 
61745747ba5SBjoern A. Zeeb 		if (m->m_len < (sizeof(*ip6) + sizeof(*th))) {
61845747ba5SBjoern A. Zeeb 			m = m_pullup(m, sizeof(*ip6) + sizeof(*th));
61945747ba5SBjoern A. Zeeb 			if (m == NULL) {
62045747ba5SBjoern A. Zeeb 				TCPSTAT_INC(tcps_rcvshort);
6218f5a8818SKevin Lo 				return (IPPROTO_DONE);
62245747ba5SBjoern A. Zeeb 			}
62345747ba5SBjoern A. Zeeb 		}
62445747ba5SBjoern A. Zeeb 
625fb59c426SYoshinobu Inoue 		ip6 = mtod(m, struct ip6_hdr *);
62645747ba5SBjoern A. Zeeb 		th = (struct tcphdr *)((caddr_t)ip6 + off0);
627fb59c426SYoshinobu Inoue 		tlen = sizeof(*ip6) + ntohs(ip6->ip6_plen) - off0;
628356ab07eSBjoern A. Zeeb 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) {
62945747ba5SBjoern A. Zeeb 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
63045747ba5SBjoern A. Zeeb 				th->th_sum = m->m_pkthdr.csum_data;
63145747ba5SBjoern A. Zeeb 			else
63245747ba5SBjoern A. Zeeb 				th->th_sum = in6_cksum_pseudo(ip6, tlen,
63345747ba5SBjoern A. Zeeb 				    IPPROTO_TCP, m->m_pkthdr.csum_data);
63445747ba5SBjoern A. Zeeb 			th->th_sum ^= 0xffff;
63545747ba5SBjoern A. Zeeb 		} else
63645747ba5SBjoern A. Zeeb 			th->th_sum = in6_cksum(m, IPPROTO_TCP, off0, tlen);
63745747ba5SBjoern A. Zeeb 		if (th->th_sum) {
63878b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
639fb59c426SYoshinobu Inoue 			goto drop;
640fb59c426SYoshinobu Inoue 		}
64133841545SHajimu UMEMOTO 
64233841545SHajimu UMEMOTO 		/*
64333841545SHajimu UMEMOTO 		 * Be proactive about unspecified IPv6 address in source.
64433841545SHajimu UMEMOTO 		 * As we use all-zero to indicate unbounded/unconnected pcb,
64533841545SHajimu UMEMOTO 		 * unspecified IPv6 address can be used to confuse us.
64633841545SHajimu UMEMOTO 		 *
64733841545SHajimu UMEMOTO 		 * Note that packets with unspecified IPv6 destination is
64833841545SHajimu UMEMOTO 		 * already dropped in ip6_input.
64933841545SHajimu UMEMOTO 		 */
65033841545SHajimu UMEMOTO 		if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
65133841545SHajimu UMEMOTO 			/* XXX stat */
65233841545SHajimu UMEMOTO 			goto drop;
65333841545SHajimu UMEMOTO 		}
654b287c6c7SBjoern A. Zeeb 	}
65504d3a452SHajimu UMEMOTO #endif
656b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
657b287c6c7SBjoern A. Zeeb 	else
658b287c6c7SBjoern A. Zeeb #endif
659b287c6c7SBjoern A. Zeeb #ifdef INET
660b287c6c7SBjoern A. Zeeb 	{
661df8bae1dSRodney W. Grimes 		/*
662df8bae1dSRodney W. Grimes 		 * Get IP and TCP header together in first mbuf.
663df8bae1dSRodney W. Grimes 		 * Note: IP leaves IP header in first mbuf.
664df8bae1dSRodney W. Grimes 		 */
665fb59c426SYoshinobu Inoue 		if (off0 > sizeof (struct ip)) {
666105bd211SGleb Smirnoff 			ip_stripoptions(m);
667fb59c426SYoshinobu Inoue 			off0 = sizeof(struct ip);
668fb59c426SYoshinobu Inoue 		}
669df8bae1dSRodney W. Grimes 		if (m->m_len < sizeof (struct tcpiphdr)) {
6704dfdffe9SAndre Oppermann 			if ((m = m_pullup(m, sizeof (struct tcpiphdr)))
6714dfdffe9SAndre Oppermann 			    == NULL) {
67278b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvshort);
6738f5a8818SKevin Lo 				return (IPPROTO_DONE);
674df8bae1dSRodney W. Grimes 			}
675df8bae1dSRodney W. Grimes 		}
676fb59c426SYoshinobu Inoue 		ip = mtod(m, struct ip *);
677db4f9cc7SJonathan Lemon 		th = (struct tcphdr *)((caddr_t)ip + off0);
6788ad458a4SGleb Smirnoff 		tlen = ntohs(ip->ip_len) - off0;
679df8bae1dSRodney W. Grimes 
680db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
681db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
682db4f9cc7SJonathan Lemon 				th->th_sum = m->m_pkthdr.csum_data;
683db4f9cc7SJonathan Lemon 			else
684db4f9cc7SJonathan Lemon 				th->th_sum = in_pseudo(ip->ip_src.s_addr,
685c068736aSJeffrey Hsu 				    ip->ip_dst.s_addr,
6868f134647SGleb Smirnoff 				    htonl(m->m_pkthdr.csum_data + tlen +
687c068736aSJeffrey Hsu 				    IPPROTO_TCP));
688db4f9cc7SJonathan Lemon 			th->th_sum ^= 0xffff;
689db4f9cc7SJonathan Lemon 		} else {
6908f134647SGleb Smirnoff 			struct ipovly *ipov = (struct ipovly *)ip;
6918f134647SGleb Smirnoff 
692df8bae1dSRodney W. Grimes 			/*
693df8bae1dSRodney W. Grimes 			 * Checksum extended TCP header and data.
694df8bae1dSRodney W. Grimes 			 */
6958f134647SGleb Smirnoff 			len = off0 + tlen;
696fb59c426SYoshinobu Inoue 			bzero(ipov->ih_x1, sizeof(ipov->ih_x1));
6978f134647SGleb Smirnoff 			ipov->ih_len = htons(tlen);
698fb59c426SYoshinobu Inoue 			th->th_sum = in_cksum(m, len);
69957f60867SMark Johnston 			/* Reset length for SDT probes. */
70057f60867SMark Johnston 			ip->ip_len = htons(tlen + off0);
701db4f9cc7SJonathan Lemon 		}
70257f60867SMark Johnston 
703fb59c426SYoshinobu Inoue 		if (th->th_sum) {
70478b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvbadsum);
705df8bae1dSRodney W. Grimes 			goto drop;
706df8bae1dSRodney W. Grimes 		}
707fb59c426SYoshinobu Inoue 		/* Re-initialization for later version check */
708fb59c426SYoshinobu Inoue 		ip->ip_v = IPVERSION;
709fb59c426SYoshinobu Inoue 	}
710b287c6c7SBjoern A. Zeeb #endif /* INET */
711df8bae1dSRodney W. Grimes 
712f2512ba1SRui Paulo #ifdef INET6
713f2512ba1SRui Paulo 	if (isipv6)
714f2512ba1SRui Paulo 		iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
715b287c6c7SBjoern A. Zeeb #endif
716b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
717f2512ba1SRui Paulo 	else
718f2512ba1SRui Paulo #endif
719b287c6c7SBjoern A. Zeeb #ifdef INET
720f2512ba1SRui Paulo 		iptos = ip->ip_tos;
721b287c6c7SBjoern A. Zeeb #endif
722f2512ba1SRui Paulo 
723df8bae1dSRodney W. Grimes 	/*
724df8bae1dSRodney W. Grimes 	 * Check that TCP offset makes sense,
725df8bae1dSRodney W. Grimes 	 * pull out TCP options and adjust length.		XXX
726df8bae1dSRodney W. Grimes 	 */
727fb59c426SYoshinobu Inoue 	off = th->th_off << 2;
728df8bae1dSRodney W. Grimes 	if (off < sizeof (struct tcphdr) || off > tlen) {
72978b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvbadoff);
730df8bae1dSRodney W. Grimes 		goto drop;
731df8bae1dSRodney W. Grimes 	}
732fb59c426SYoshinobu Inoue 	tlen -= off;	/* tlen is used instead of ti->ti_len */
733df8bae1dSRodney W. Grimes 	if (off > sizeof (struct tcphdr)) {
73404d3a452SHajimu UMEMOTO #ifdef INET6
735b287c6c7SBjoern A. Zeeb 		if (isipv6) {
7368f5a8818SKevin Lo 			IP6_EXTHDR_CHECK(m, off0, off, IPPROTO_DONE);
737fb59c426SYoshinobu Inoue 			ip6 = mtod(m, struct ip6_hdr *);
738fb59c426SYoshinobu Inoue 			th = (struct tcphdr *)((caddr_t)ip6 + off0);
739b287c6c7SBjoern A. Zeeb 		}
74004d3a452SHajimu UMEMOTO #endif
741b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
742b287c6c7SBjoern A. Zeeb 		else
743b287c6c7SBjoern A. Zeeb #endif
744b287c6c7SBjoern A. Zeeb #ifdef INET
745b287c6c7SBjoern A. Zeeb 		{
746df8bae1dSRodney W. Grimes 			if (m->m_len < sizeof(struct ip) + off) {
747c068736aSJeffrey Hsu 				if ((m = m_pullup(m, sizeof (struct ip) + off))
7484dfdffe9SAndre Oppermann 				    == NULL) {
74978b50714SRobert Watson 					TCPSTAT_INC(tcps_rcvshort);
7508f5a8818SKevin Lo 					return (IPPROTO_DONE);
751df8bae1dSRodney W. Grimes 				}
752fb59c426SYoshinobu Inoue 				ip = mtod(m, struct ip *);
753fb59c426SYoshinobu Inoue 				th = (struct tcphdr *)((caddr_t)ip + off0);
754fb59c426SYoshinobu Inoue 			}
755df8bae1dSRodney W. Grimes 		}
756b287c6c7SBjoern A. Zeeb #endif
757df8bae1dSRodney W. Grimes 		optlen = off - sizeof (struct tcphdr);
758fb59c426SYoshinobu Inoue 		optp = (u_char *)(th + 1);
759df8bae1dSRodney W. Grimes 	}
760fb59c426SYoshinobu Inoue 	thflags = th->th_flags;
761df8bae1dSRodney W. Grimes 
762e46cd3d4SDag-Erling Smørgrav 	/*
763df8bae1dSRodney W. Grimes 	 * Convert TCP protocol specific fields to host format.
764df8bae1dSRodney W. Grimes 	 */
7652903309aSAttilio Rao 	tcp_fields_to_host(th);
766df8bae1dSRodney W. Grimes 
767df8bae1dSRodney W. Grimes 	/*
768794235b7SAndre Oppermann 	 * Delay dropping TCP, IP headers, IPv6 ext headers, and TCP options.
769755c1f07SAndras Olah 	 */
770fb59c426SYoshinobu Inoue 	drop_hdrlen = off0 + off;
771755c1f07SAndras Olah 
772755c1f07SAndras Olah 	/*
773fa046d87SRobert Watson 	 * Locate pcb for segment; if we're likely to add or remove a
774a7c7f2a7SJohn Baldwin 	 * connection then first acquire pcbinfo lock.  There are three cases
775fa046d87SRobert Watson 	 * where we might discover later we need a write lock despite the
776a7c7f2a7SJohn Baldwin 	 * flags: ACKs moving a connection out of the syncache, ACKs for a
777a7c7f2a7SJohn Baldwin 	 * connection in TIMEWAIT and SYNs not targeting a listening socket.
778df8bae1dSRodney W. Grimes 	 */
779a7c7f2a7SJohn Baldwin 	if ((thflags & (TH_FIN | TH_RST)) != 0) {
780ff9b006dSJulien Charbon 		INP_INFO_RLOCK(&V_tcbinfo);
781ff9b006dSJulien Charbon 		ti_locked = TI_RLOCKED;
782fa046d87SRobert Watson 	} else
783fa046d87SRobert Watson 		ti_locked = TI_UNLOCKED;
784252ca428SRobert Watson 
7858d573cc1SAndre Oppermann 	/*
7868d573cc1SAndre Oppermann 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
7878d573cc1SAndre Oppermann 	 */
788b8056faeSGleb Smirnoff         if (
789b8056faeSGleb Smirnoff #ifdef INET6
790b8056faeSGleb Smirnoff 	    (isipv6 && (m->m_flags & M_IP6_NEXTHOP))
791b8056faeSGleb Smirnoff #ifdef INET
792b8056faeSGleb Smirnoff 	    || (!isipv6 && (m->m_flags & M_IP_NEXTHOP))
793b8056faeSGleb Smirnoff #endif
794b8056faeSGleb Smirnoff #endif
795b8056faeSGleb Smirnoff #if defined(INET) && !defined(INET6)
796b8056faeSGleb Smirnoff 	    (m->m_flags & M_IP_NEXTHOP)
797b8056faeSGleb Smirnoff #endif
798b8056faeSGleb Smirnoff 	    )
7999b932e9eSAndre Oppermann 		fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
8009b932e9eSAndre Oppermann 
801ce7ad664SEd Maste findpcb:
802ce7ad664SEd Maste #ifdef INVARIANTS
803ff9b006dSJulien Charbon 	if (ti_locked == TI_RLOCKED) {
804ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
805ce7ad664SEd Maste 	} else {
806ce7ad664SEd Maste 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
807ce7ad664SEd Maste 	}
808ce7ad664SEd Maste #endif
8098a006adbSBjoern A. Zeeb #ifdef INET6
8108a006adbSBjoern A. Zeeb 	if (isipv6 && fwd_tag != NULL) {
8118a006adbSBjoern A. Zeeb 		struct sockaddr_in6 *next_hop6;
8128a006adbSBjoern A. Zeeb 
8138a006adbSBjoern A. Zeeb 		next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
8148a006adbSBjoern A. Zeeb 		/*
8158a006adbSBjoern A. Zeeb 		 * Transparently forwarded. Pretend to be the destination.
8168a006adbSBjoern A. Zeeb 		 * Already got one like this?
8178a006adbSBjoern A. Zeeb 		 */
8188a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo,
8198a006adbSBjoern A. Zeeb 		    &ip6->ip6_src, th->th_sport, &ip6->ip6_dst, th->th_dport,
8208a006adbSBjoern A. Zeeb 		    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif, m);
8218a006adbSBjoern A. Zeeb 		if (!inp) {
8228a006adbSBjoern A. Zeeb 			/*
8238a006adbSBjoern A. Zeeb 			 * It's new.  Try to find the ambushing socket.
8248a006adbSBjoern A. Zeeb 			 * Because we've rewritten the destination address,
8258a006adbSBjoern A. Zeeb 			 * any hardware-generated hash is ignored.
8268a006adbSBjoern A. Zeeb 			 */
8278a006adbSBjoern A. Zeeb 			inp = in6_pcblookup(&V_tcbinfo, &ip6->ip6_src,
8288a006adbSBjoern A. Zeeb 			    th->th_sport, &next_hop6->sin6_addr,
8298a006adbSBjoern A. Zeeb 			    next_hop6->sin6_port ? ntohs(next_hop6->sin6_port) :
8308a006adbSBjoern A. Zeeb 			    th->th_dport, INPLOOKUP_WILDCARD |
8318a006adbSBjoern A. Zeeb 			    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif);
8328a006adbSBjoern A. Zeeb 		}
833c1de64a4SAndrey V. Elsukov 	} else if (isipv6) {
8348a006adbSBjoern A. Zeeb 		inp = in6_pcblookup_mbuf(&V_tcbinfo, &ip6->ip6_src,
8358a006adbSBjoern A. Zeeb 		    th->th_sport, &ip6->ip6_dst, th->th_dport,
8368a006adbSBjoern A. Zeeb 		    INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB,
8378a006adbSBjoern A. Zeeb 		    m->m_pkthdr.rcvif, m);
8388a006adbSBjoern A. Zeeb 	}
8398a006adbSBjoern A. Zeeb #endif /* INET6 */
8408a006adbSBjoern A. Zeeb #if defined(INET6) && defined(INET)
8418a006adbSBjoern A. Zeeb 	else
8428a006adbSBjoern A. Zeeb #endif
8438a006adbSBjoern A. Zeeb #ifdef INET
8448a006adbSBjoern A. Zeeb 	if (fwd_tag != NULL) {
8459b932e9eSAndre Oppermann 		struct sockaddr_in *next_hop;
8469b932e9eSAndre Oppermann 
8479b932e9eSAndre Oppermann 		next_hop = (struct sockaddr_in *)(fwd_tag+1);
848f9e354dfSJulian Elischer 		/*
8492b25acc1SLuigi Rizzo 		 * Transparently forwarded. Pretend to be the destination.
850f9e354dfSJulian Elischer 		 * already got one like this?
851f9e354dfSJulian Elischer 		 */
852d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src, th->th_sport,
853fa046d87SRobert Watson 		    ip->ip_dst, th->th_dport, INPLOOKUP_WLOCKPCB,
854d3c1f003SRobert Watson 		    m->m_pkthdr.rcvif, m);
855fa046d87SRobert Watson 		if (!inp) {
856fa046d87SRobert Watson 			/*
857fa046d87SRobert Watson 			 * It's new.  Try to find the ambushing socket.
858d3c1f003SRobert Watson 			 * Because we've rewritten the destination address,
859d3c1f003SRobert Watson 			 * any hardware-generated hash is ignored.
860fa046d87SRobert Watson 			 */
861fa046d87SRobert Watson 			inp = in_pcblookup(&V_tcbinfo, ip->ip_src,
862fa046d87SRobert Watson 			    th->th_sport, next_hop->sin_addr,
863fa046d87SRobert Watson 			    next_hop->sin_port ? ntohs(next_hop->sin_port) :
864fa046d87SRobert Watson 			    th->th_dport, INPLOOKUP_WILDCARD |
865fa046d87SRobert Watson 			    INPLOOKUP_WLOCKPCB, m->m_pkthdr.rcvif);
866f9e354dfSJulian Elischer 		}
867b10fbdeaSAndre Oppermann 	} else
868d3c1f003SRobert Watson 		inp = in_pcblookup_mbuf(&V_tcbinfo, ip->ip_src,
869fa046d87SRobert Watson 		    th->th_sport, ip->ip_dst, th->th_dport,
870fa046d87SRobert Watson 		    INPLOOKUP_WILDCARD | INPLOOKUP_WLOCKPCB,
871d3c1f003SRobert Watson 		    m->m_pkthdr.rcvif, m);
8728a006adbSBjoern A. Zeeb #endif /* INET */
873f9e354dfSJulian Elischer 
874df8bae1dSRodney W. Grimes 	/*
875574b6964SAndre Oppermann 	 * If the INPCB does not exist then all data in the incoming
876574b6964SAndre Oppermann 	 * segment is discarded and an appropriate RST is sent back.
8778b07e49aSJulian Elischer 	 * XXX MRT Send RST using which routing table?
878df8bae1dSRodney W. Grimes 	 */
879816a3d83SPoul-Henning Kamp 	if (inp == NULL) {
880574b6964SAndre Oppermann 		/*
881574b6964SAndre Oppermann 		 * Log communication attempts to ports that are not
882574b6964SAndre Oppermann 		 * in use.
883574b6964SAndre Oppermann 		 */
884574b6964SAndre Oppermann 		if ((tcp_log_in_vain == 1 && (thflags & TH_SYN)) ||
885574b6964SAndre Oppermann 		    tcp_log_in_vain == 2) {
886b7d747ecSAndre Oppermann 			if ((s = tcp_log_vain(NULL, th, (void *)ip, ip6)))
887df541e5fSAndre Oppermann 				log(LOG_INFO, "%s; %s: Connection attempt "
888df541e5fSAndre Oppermann 				    "to closed port\n", s, __func__);
8892e4e1b4cSGeoff Rehmet 		}
890574b6964SAndre Oppermann 		/*
891574b6964SAndre Oppermann 		 * When blackholing do not respond with a RST but
892574b6964SAndre Oppermann 		 * completely ignore the segment and drop it.
893574b6964SAndre Oppermann 		 */
894603724d3SBjoern A. Zeeb 		if ((V_blackhole == 1 && (thflags & TH_SYN)) ||
895603724d3SBjoern A. Zeeb 		    V_blackhole == 2)
8961929eae1SAndre Oppermann 			goto dropunlock;
897574b6964SAndre Oppermann 
898a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
899a57815efSBosko Milekic 		goto dropwithreset;
900816a3d83SPoul-Henning Kamp 	}
901fa046d87SRobert Watson 	INP_WLOCK_ASSERT(inp);
902c2529042SHans Petter Selasky 	if ((inp->inp_flowtype == M_HASHTYPE_NONE) &&
903c2529042SHans Petter Selasky 	    (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) &&
904c2529042SHans Petter Selasky 	    ((inp->inp_socket == NULL) ||
905c2529042SHans Petter Selasky 	    (inp->inp_socket->so_options & SO_ACCEPTCONN) == 0)) {
90680cb9f21SKip Macy 		inp->inp_flowid = m->m_pkthdr.flowid;
9072f719932SAdrian Chadd 		inp->inp_flowtype = M_HASHTYPE_GET(m);
90880cb9f21SKip Macy 	}
909a2589465SKen Smith #ifdef IPSEC
910a2589465SKen Smith #ifdef INET6
911a2589465SKen Smith 	if (isipv6 && ipsec6_in_reject(m, inp)) {
912a2589465SKen Smith 		goto dropunlock;
913a2589465SKen Smith 	} else
914a2589465SKen Smith #endif /* INET6 */
915a2589465SKen Smith 	if (ipsec4_in_reject(m, inp) != 0) {
916a2589465SKen Smith 		goto dropunlock;
917a2589465SKen Smith 	}
918a2589465SKen Smith #endif /* IPSEC */
919a2589465SKen Smith 
9208d573cc1SAndre Oppermann 	/*
9218d573cc1SAndre Oppermann 	 * Check the minimum TTL for socket.
9228d573cc1SAndre Oppermann 	 */
92334f83c52SGeorge V. Neville-Neil 	if (inp->inp_ip_minttl != 0) {
92434f83c52SGeorge V. Neville-Neil #ifdef INET6
9252d8868dbSJonathan T. Looney 		if (isipv6) {
9262d8868dbSJonathan T. Looney 			if (inp->inp_ip_minttl > ip6->ip6_hlim)
927302ce8d6SAndre Oppermann 				goto dropunlock;
9282d8868dbSJonathan T. Looney 		} else
92934f83c52SGeorge V. Neville-Neil #endif
93034f83c52SGeorge V. Neville-Neil 		if (inp->inp_ip_minttl > ip->ip_ttl)
931302ce8d6SAndre Oppermann 			goto dropunlock;
93234f83c52SGeorge V. Neville-Neil 	}
933936cd18dSAndre Oppermann 
934340c35deSJonathan Lemon 	/*
935252ca428SRobert Watson 	 * A previous connection in TIMEWAIT state is supposed to catch stray
936252ca428SRobert Watson 	 * or duplicate segments arriving late.  If this segment was a
9370989f56cSRobert Watson 	 * legitimate new connection attempt, the old INPCB gets removed and
938252ca428SRobert Watson 	 * we can try again to find a listening socket.
939252ca428SRobert Watson 	 *
940fa046d87SRobert Watson 	 * At this point, due to earlier optimism, we may hold only an inpcb
941fa046d87SRobert Watson 	 * lock, and not the inpcbinfo write lock.  If so, we need to try to
942fa046d87SRobert Watson 	 * acquire it, or if that fails, acquire a reference on the inpcb,
943fa046d87SRobert Watson 	 * drop all locks, acquire a global write lock, and then re-acquire
944fa046d87SRobert Watson 	 * the inpcb lock.  We may at that point discover that another thread
945fa046d87SRobert Watson 	 * has tried to free the inpcb, in which case we need to loop back
946fa046d87SRobert Watson 	 * and try to find a new inpcb to deliver to.
947fa046d87SRobert Watson 	 *
948fa046d87SRobert Watson 	 * XXXRW: It may be time to rethink timewait locking.
949340c35deSJonathan Lemon 	 */
950883e9bc4SRobert Watson relocked:
951ad71fe3cSRobert Watson 	if (inp->inp_flags & INP_TIMEWAIT) {
952fa046d87SRobert Watson 		if (ti_locked == TI_UNLOCKED) {
953ff9b006dSJulien Charbon 			if (INP_INFO_TRY_RLOCK(&V_tcbinfo) == 0) {
954252ca428SRobert Watson 				in_pcbref(inp);
955252ca428SRobert Watson 				INP_WUNLOCK(inp);
956ff9b006dSJulien Charbon 				INP_INFO_RLOCK(&V_tcbinfo);
957ff9b006dSJulien Charbon 				ti_locked = TI_RLOCKED;
958252ca428SRobert Watson 				INP_WLOCK(inp);
959fa046d87SRobert Watson 				if (in_pcbrele_wlocked(inp)) {
960252ca428SRobert Watson 					inp = NULL;
961252ca428SRobert Watson 					goto findpcb;
962252ca428SRobert Watson 				}
963f681a5fdSRobert Watson 			} else
964ff9b006dSJulien Charbon 				ti_locked = TI_RLOCKED;
965252ca428SRobert Watson 		}
966ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
967252ca428SRobert Watson 
968340c35deSJonathan Lemon 		if (thflags & TH_SYN)
969f72167f4SAndre Oppermann 			tcp_dooptions(&to, optp, optlen, TO_SYN);
9708d573cc1SAndre Oppermann 		/*
9718d573cc1SAndre Oppermann 		 * NB: tcp_twcheck unlocks the INP and frees the mbuf.
9728d573cc1SAndre Oppermann 		 */
9732104448fSAndre Oppermann 		if (tcp_twcheck(inp, &to, th, m, tlen))
974340c35deSJonathan Lemon 			goto findpcb;
975ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
9768f5a8818SKevin Lo 		return (IPPROTO_DONE);
977340c35deSJonathan Lemon 	}
978794235b7SAndre Oppermann 	/*
979794235b7SAndre Oppermann 	 * The TCPCB may no longer exist if the connection is winding
980794235b7SAndre Oppermann 	 * down or it is in the CLOSED state.  Either way we drop the
981794235b7SAndre Oppermann 	 * segment and send an appropriate response.
982794235b7SAndre Oppermann 	 */
983df8bae1dSRodney W. Grimes 	tp = intotcpcb(inp);
984a160e630SAndre Oppermann 	if (tp == NULL || tp->t_state == TCPS_CLOSED) {
985a57815efSBosko Milekic 		rstreason = BANDLIM_RST_CLOSEDPORT;
986a57815efSBosko Milekic 		goto dropwithreset;
98709f81a46SBosko Milekic 	}
988df8bae1dSRodney W. Grimes 
98909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD
99009fe6320SNavdeep Parhar 	if (tp->t_flags & TF_TOE) {
99109fe6320SNavdeep Parhar 		tcp_offload_input(tp, m);
99209fe6320SNavdeep Parhar 		m = NULL;	/* consumed by the TOE driver */
99309fe6320SNavdeep Parhar 		goto dropunlock;
99409fe6320SNavdeep Parhar 	}
99509fe6320SNavdeep Parhar #endif
99609fe6320SNavdeep Parhar 
997252ca428SRobert Watson 	/*
998252ca428SRobert Watson 	 * We've identified a valid inpcb, but it could be that we need an
999fa046d87SRobert Watson 	 * inpcbinfo write lock but don't hold it.  In this case, attempt to
1000fa046d87SRobert Watson 	 * acquire using the same strategy as the TIMEWAIT case above.  If we
1001fa046d87SRobert Watson 	 * relock, we have to jump back to 'relocked' as the connection might
1002fa046d87SRobert Watson 	 * now be in TIMEWAIT.
1003252ca428SRobert Watson 	 */
1004fa046d87SRobert Watson #ifdef INVARIANTS
1005a7c7f2a7SJohn Baldwin 	if ((thflags & (TH_FIN | TH_RST)) != 0)
1006ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1007fa046d87SRobert Watson #endif
1008a7c7f2a7SJohn Baldwin 	if (!((tp->t_state == TCPS_ESTABLISHED && (thflags & TH_SYN) == 0) ||
1009281a0fd4SPatrick Kelsey 	      (tp->t_state == TCPS_LISTEN && (thflags & TH_SYN) &&
1010281a0fd4SPatrick Kelsey 	       !(tp->t_flags & TF_FASTOPEN)))) {
1011fa046d87SRobert Watson 		if (ti_locked == TI_UNLOCKED) {
1012ff9b006dSJulien Charbon 			if (INP_INFO_TRY_RLOCK(&V_tcbinfo) == 0) {
1013252ca428SRobert Watson 				in_pcbref(inp);
1014252ca428SRobert Watson 				INP_WUNLOCK(inp);
1015ff9b006dSJulien Charbon 				INP_INFO_RLOCK(&V_tcbinfo);
1016ff9b006dSJulien Charbon 				ti_locked = TI_RLOCKED;
1017252ca428SRobert Watson 				INP_WLOCK(inp);
1018fa046d87SRobert Watson 				if (in_pcbrele_wlocked(inp)) {
1019252ca428SRobert Watson 					inp = NULL;
1020252ca428SRobert Watson 					goto findpcb;
1021252ca428SRobert Watson 				}
1022883e9bc4SRobert Watson 				goto relocked;
1023f681a5fdSRobert Watson 			} else
1024ff9b006dSJulien Charbon 				ti_locked = TI_RLOCKED;
1025252ca428SRobert Watson 		}
1026ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1027252ca428SRobert Watson 	}
1028252ca428SRobert Watson 
1029c488362eSRobert Watson #ifdef MAC
10308501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
103130d239bcSRobert Watson 	if (mac_inpcb_check_deliver(inp, m))
1032302ce8d6SAndre Oppermann 		goto dropunlock;
1033c488362eSRobert Watson #endif
1034a557af22SRobert Watson 	so = inp->inp_socket;
1035302ce8d6SAndre Oppermann 	KASSERT(so != NULL, ("%s: so == NULL", __func__));
1036610ee2f9SDavid Greenman #ifdef TCPDEBUG
1037df8bae1dSRodney W. Grimes 	if (so->so_options & SO_DEBUG) {
1038df8bae1dSRodney W. Grimes 		ostate = tp->t_state;
103910fe523eSMaxim Konovalov #ifdef INET6
10406f697424SBjoern A. Zeeb 		if (isipv6) {
1041540e8b7eSJeffrey Hsu 			bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6));
1042d30d90dcSMaxim Konovalov 		} else
10436f697424SBjoern A. Zeeb #endif
1044540e8b7eSJeffrey Hsu 			bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
1045fb59c426SYoshinobu Inoue 		tcp_savetcp = *th;
1046df8bae1dSRodney W. Grimes 	}
1047b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */
1048db33b3e6SAndre Oppermann 	/*
1049db33b3e6SAndre Oppermann 	 * When the socket is accepting connections (the INPCB is in LISTEN
1050db33b3e6SAndre Oppermann 	 * state) we look into the SYN cache if this is a new connection
1051a7c7f2a7SJohn Baldwin 	 * attempt or the completion of a previous one.
1052db33b3e6SAndre Oppermann 	 */
1053540e8b7eSJeffrey Hsu 	if (so->so_options & SO_ACCEPTCONN) {
1054540e8b7eSJeffrey Hsu 		struct in_conninfo inc;
1055540e8b7eSJeffrey Hsu 
10567824d002SAndre Oppermann 		KASSERT(tp->t_state == TCPS_LISTEN, ("%s: so accepting but "
10577824d002SAndre Oppermann 		    "tp not listening", __func__));
10588bfb1918SAndre Oppermann 		bzero(&inc, sizeof(inc));
1059302ce8d6SAndre Oppermann #ifdef INET6
1060be2ac88cSJonathan Lemon 		if (isipv6) {
1061dcdb4371SBjoern A. Zeeb 			inc.inc_flags |= INC_ISIPV6;
1062be2ac88cSJonathan Lemon 			inc.inc6_faddr = ip6->ip6_src;
1063be2ac88cSJonathan Lemon 			inc.inc6_laddr = ip6->ip6_dst;
1064302ce8d6SAndre Oppermann 		} else
1065302ce8d6SAndre Oppermann #endif
1066302ce8d6SAndre Oppermann 		{
1067be2ac88cSJonathan Lemon 			inc.inc_faddr = ip->ip_src;
1068be2ac88cSJonathan Lemon 			inc.inc_laddr = ip->ip_dst;
1069be2ac88cSJonathan Lemon 		}
1070be2ac88cSJonathan Lemon 		inc.inc_fport = th->th_sport;
1071be2ac88cSJonathan Lemon 		inc.inc_lport = th->th_dport;
10727973fba3SJulian Elischer 		inc.inc_fibnum = so->so_fibnum;
1073be2ac88cSJonathan Lemon 
1074fb59c426SYoshinobu Inoue 		/*
10758d573cc1SAndre Oppermann 		 * Check for an existing connection attempt in syncache if
10768d573cc1SAndre Oppermann 		 * the flag is only ACK.  A successful lookup creates a new
10778d573cc1SAndre Oppermann 		 * socket appended to the listen queue in SYN_RECEIVED state.
1078fb59c426SYoshinobu Inoue 		 */
1079be2ac88cSJonathan Lemon 		if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
1080a7c7f2a7SJohn Baldwin 
1081ff9b006dSJulien Charbon 			INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1082bf6d304aSAndre Oppermann 			/*
1083bf6d304aSAndre Oppermann 			 * Parse the TCP options here because
1084bf6d304aSAndre Oppermann 			 * syncookies need access to the reflected
1085bf6d304aSAndre Oppermann 			 * timestamp.
1086bf6d304aSAndre Oppermann 			 */
1087bf6d304aSAndre Oppermann 			tcp_dooptions(&to, optp, optlen, 0);
10889fa198beSAndre Oppermann 			/*
10899fa198beSAndre Oppermann 			 * NB: syncache_expand() doesn't unlock
10909fa198beSAndre Oppermann 			 * inp and tcpinfo locks.
10919fa198beSAndre Oppermann 			 */
1092bf6d304aSAndre Oppermann 			if (!syncache_expand(&inc, &to, th, &so, m)) {
10934195b4afSPaul Traina 				/*
1094e207f800SAndre Oppermann 				 * No syncache entry or ACK was not
1095be2ac88cSJonathan Lemon 				 * for our SYN/ACK.  Send a RST.
10968d573cc1SAndre Oppermann 				 * NB: syncache did its own logging
10978d573cc1SAndre Oppermann 				 * of the failure cause.
10984195b4afSPaul Traina 				 */
1099be2ac88cSJonathan Lemon 				rstreason = BANDLIM_RST_OPENPORT;
1100be2ac88cSJonathan Lemon 				goto dropwithreset;
1101be2ac88cSJonathan Lemon 			}
1102281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
1103281a0fd4SPatrick Kelsey new_tfo_socket:
1104281a0fd4SPatrick Kelsey #endif
1105f76fcf6dSJeffrey Hsu 			if (so == NULL) {
1106be2ac88cSJonathan Lemon 				/*
1107e207f800SAndre Oppermann 				 * We completed the 3-way handshake
1108e207f800SAndre Oppermann 				 * but could not allocate a socket
1109e207f800SAndre Oppermann 				 * either due to memory shortage,
1110e207f800SAndre Oppermann 				 * listen queue length limits or
1111a160e630SAndre Oppermann 				 * global socket limits.  Send RST
1112a160e630SAndre Oppermann 				 * or wait and have the remote end
1113a160e630SAndre Oppermann 				 * retransmit the ACK for another
1114a160e630SAndre Oppermann 				 * try.
1115be2ac88cSJonathan Lemon 				 */
1116a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1117a160e630SAndre Oppermann 					log(LOG_DEBUG, "%s; %s: Listen socket: "
11188d573cc1SAndre Oppermann 					    "Socket allocation failed due to "
11198d573cc1SAndre Oppermann 					    "limits or memory shortage, %s\n",
1120ac957cd2SJulian Elischer 					    s, __func__,
1121ac957cd2SJulian Elischer 					    V_tcp_sc_rst_sock_fail ?
1122ac957cd2SJulian Elischer 					    "sending RST" : "try again");
1123603724d3SBjoern A. Zeeb 				if (V_tcp_sc_rst_sock_fail) {
1124e207f800SAndre Oppermann 					rstreason = BANDLIM_UNLIMITED;
1125e207f800SAndre Oppermann 					goto dropwithreset;
1126a160e630SAndre Oppermann 				} else
1127a160e630SAndre Oppermann 					goto dropunlock;
1128f76fcf6dSJeffrey Hsu 			}
1129be2ac88cSJonathan Lemon 			/*
1130be2ac88cSJonathan Lemon 			 * Socket is created in state SYN_RECEIVED.
11318d573cc1SAndre Oppermann 			 * Unlock the listen socket, lock the newly
11328d573cc1SAndre Oppermann 			 * created socket and update the tp variable.
1133be2ac88cSJonathan Lemon 			 */
11348501a69cSRobert Watson 			INP_WUNLOCK(inp);	/* listen socket */
1135be2ac88cSJonathan Lemon 			inp = sotoinpcb(so);
1136ff9b006dSJulien Charbon 			/*
1137ff9b006dSJulien Charbon 			 * New connection inpcb is already locked by
1138ff9b006dSJulien Charbon 			 * syncache_expand().
1139ff9b006dSJulien Charbon 			 */
1140ff9b006dSJulien Charbon 			INP_WLOCK_ASSERT(inp);
1141be2ac88cSJonathan Lemon 			tp = intotcpcb(inp);
11428d573cc1SAndre Oppermann 			KASSERT(tp->t_state == TCPS_SYN_RECEIVED,
11438d573cc1SAndre Oppermann 			    ("%s: ", __func__));
11442903309aSAttilio Rao #ifdef TCP_SIGNATURE
11452903309aSAttilio Rao 			if (sig_checked == 0)  {
11462903309aSAttilio Rao 				tcp_dooptions(&to, optp, optlen,
11472903309aSAttilio Rao 				    (thflags & TH_SYN) ? TO_SYN : 0);
11482903309aSAttilio Rao 				if (!tcp_signature_verify_input(m, off0, tlen,
11492903309aSAttilio Rao 				    optlen, &to, th, tp->t_flags)) {
11502903309aSAttilio Rao 
11512903309aSAttilio Rao 					/*
11522903309aSAttilio Rao 					 * In SYN_SENT state if it receives an
11532903309aSAttilio Rao 					 * RST, it is allowed for further
11542903309aSAttilio Rao 					 * processing.
11552903309aSAttilio Rao 					 */
11562903309aSAttilio Rao 					if ((thflags & TH_RST) == 0 ||
11572903309aSAttilio Rao 					    (tp->t_state == TCPS_SYN_SENT) == 0)
11582903309aSAttilio Rao 						goto dropunlock;
11592903309aSAttilio Rao 				}
11602903309aSAttilio Rao 				sig_checked = 1;
11612903309aSAttilio Rao 			}
11622903309aSAttilio Rao #endif
11632903309aSAttilio Rao 
1164be2ac88cSJonathan Lemon 			/*
1165302ce8d6SAndre Oppermann 			 * Process the segment and the data it
1166302ce8d6SAndre Oppermann 			 * contains.  tcp_do_segment() consumes
1167302ce8d6SAndre Oppermann 			 * the mbuf chain and unlocks the inpcb.
1168302ce8d6SAndre Oppermann 			 */
116955bceb1eSRandall Stewart 			tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen,
1170252ca428SRobert Watson 			    iptos, ti_locked);
1171603724d3SBjoern A. Zeeb 			INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
11728f5a8818SKevin Lo 			return (IPPROTO_DONE);
1173be2ac88cSJonathan Lemon 		}
1174a160e630SAndre Oppermann 		/*
11758d573cc1SAndre Oppermann 		 * Segment flag validation for new connection attempts:
11768d573cc1SAndre Oppermann 		 *
1177a160e630SAndre Oppermann 		 * Our (SYN|ACK) response was rejected.
1178a160e630SAndre Oppermann 		 * Check with syncache and remove entry to prevent
1179a160e630SAndre Oppermann 		 * retransmits.
118019bc77c5SAndre Oppermann 		 *
118119bc77c5SAndre Oppermann 		 * NB: syncache_chkrst does its own logging of failure
118219bc77c5SAndre Oppermann 		 * causes.
1183a160e630SAndre Oppermann 		 */
1184a160e630SAndre Oppermann 		if (thflags & TH_RST) {
118519bc77c5SAndre Oppermann 			syncache_chkrst(&inc, th);
1186a160e630SAndre Oppermann 			goto dropunlock;
1187a160e630SAndre Oppermann 		}
1188a160e630SAndre Oppermann 		/*
1189a160e630SAndre Oppermann 		 * We can't do anything without SYN.
1190a160e630SAndre Oppermann 		 */
1191a160e630SAndre Oppermann 		if ((thflags & TH_SYN) == 0) {
1192a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1193a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1194773673c1SAndre Oppermann 				    "SYN is missing, segment ignored\n",
11958d573cc1SAndre Oppermann 				    s, __func__);
119678b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1197a160e630SAndre Oppermann 			goto dropunlock;
1198a160e630SAndre Oppermann 		}
1199a160e630SAndre Oppermann 		/*
1200a160e630SAndre Oppermann 		 * (SYN|ACK) is bogus on a listen socket.
1201a160e630SAndre Oppermann 		 */
1202fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
1203a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1204a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
12058d573cc1SAndre Oppermann 				    "SYN|ACK invalid, segment rejected\n",
12068d573cc1SAndre Oppermann 				    s, __func__);
1207a160e630SAndre Oppermann 			syncache_badack(&inc);	/* XXX: Not needed! */
120878b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1209a57815efSBosko Milekic 			rstreason = BANDLIM_RST_OPENPORT;
1210a57815efSBosko Milekic 			goto dropwithreset;
12114195b4afSPaul Traina 		}
1212a160e630SAndre Oppermann 		/*
1213a160e630SAndre Oppermann 		 * If the drop_synfin option is enabled, drop all
1214a160e630SAndre Oppermann 		 * segments with both the SYN and FIN bits set.
1215a160e630SAndre Oppermann 		 * This prevents e.g. nmap from identifying the
1216a160e630SAndre Oppermann 		 * TCP/IP stack.
1217a160e630SAndre Oppermann 		 * XXX: Poor reasoning.  nmap has other methods
1218a160e630SAndre Oppermann 		 * and is constantly refining its stack detection
1219a160e630SAndre Oppermann 		 * strategies.
12208d573cc1SAndre Oppermann 		 * XXX: This is a violation of the TCP specification
1221a160e630SAndre Oppermann 		 * and was used by RFC1644.
1222a160e630SAndre Oppermann 		 */
1223603724d3SBjoern A. Zeeb 		if ((thflags & TH_FIN) && V_drop_synfin) {
1224a160e630SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1225a160e630SAndre Oppermann 				log(LOG_DEBUG, "%s; %s: Listen socket: "
1226773673c1SAndre Oppermann 				    "SYN|FIN segment ignored (based on "
12278d573cc1SAndre Oppermann 				    "sysctl setting)\n", s, __func__);
122878b50714SRobert Watson 			TCPSTAT_INC(tcps_badsyn);
1229302ce8d6SAndre Oppermann 			goto dropunlock;
1230ebb0cbeaSPaul Traina 		}
1231be2ac88cSJonathan Lemon 		/*
1232be2ac88cSJonathan Lemon 		 * Segment's flags are (SYN) or (SYN|FIN).
1233a160e630SAndre Oppermann 		 *
1234a160e630SAndre Oppermann 		 * TH_PUSH, TH_URG, TH_ECE, TH_CWR are ignored
1235a160e630SAndre Oppermann 		 * as they do not affect the state of the TCP FSM.
1236a160e630SAndre Oppermann 		 * The data pointed to by TH_URG and th_urp is ignored.
1237be2ac88cSJonathan Lemon 		 */
1238a160e630SAndre Oppermann 		KASSERT((thflags & (TH_RST|TH_ACK)) == 0,
1239a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_RST or TH_ACK set", __func__));
1240a160e630SAndre Oppermann 		KASSERT(thflags & (TH_SYN),
1241a160e630SAndre Oppermann 		    ("%s: Listen socket: TH_SYN not set", __func__));
124233841545SHajimu UMEMOTO #ifdef INET6
124333841545SHajimu UMEMOTO 		/*
124433841545SHajimu UMEMOTO 		 * If deprecated address is forbidden,
124533841545SHajimu UMEMOTO 		 * we do not accept SYN to deprecated interface
124633841545SHajimu UMEMOTO 		 * address to prevent any new inbound connection from
124733841545SHajimu UMEMOTO 		 * getting established.
124833841545SHajimu UMEMOTO 		 * When we do not accept SYN, we send a TCP RST,
124933841545SHajimu UMEMOTO 		 * with deprecated source address (instead of dropping
125033841545SHajimu UMEMOTO 		 * it).  We compromise it as it is much better for peer
125133841545SHajimu UMEMOTO 		 * to send a RST, and RST will be the final packet
125233841545SHajimu UMEMOTO 		 * for the exchange.
125333841545SHajimu UMEMOTO 		 *
125433841545SHajimu UMEMOTO 		 * If we do not forbid deprecated addresses, we accept
125533841545SHajimu UMEMOTO 		 * the SYN packet.  RFC2462 does not suggest dropping
125633841545SHajimu UMEMOTO 		 * SYN in this case.
125733841545SHajimu UMEMOTO 		 * If we decipher RFC2462 5.5.4, it says like this:
125833841545SHajimu UMEMOTO 		 * 1. use of deprecated addr with existing
125933841545SHajimu UMEMOTO 		 *    communication is okay - "SHOULD continue to be
126033841545SHajimu UMEMOTO 		 *    used"
126133841545SHajimu UMEMOTO 		 * 2. use of it with new communication:
126233841545SHajimu UMEMOTO 		 *   (2a) "SHOULD NOT be used if alternate address
126333841545SHajimu UMEMOTO 		 *        with sufficient scope is available"
126433841545SHajimu UMEMOTO 		 *   (2b) nothing mentioned otherwise.
126533841545SHajimu UMEMOTO 		 * Here we fall into (2b) case as we have no choice in
126633841545SHajimu UMEMOTO 		 * our source address selection - we must obey the peer.
126733841545SHajimu UMEMOTO 		 *
126833841545SHajimu UMEMOTO 		 * The wording in RFC2462 is confusing, and there are
126933841545SHajimu UMEMOTO 		 * multiple description text for deprecated address
127033841545SHajimu UMEMOTO 		 * handling - worse, they are not exactly the same.
127133841545SHajimu UMEMOTO 		 * I believe 5.5.4 is the best one, so we follow 5.5.4.
127233841545SHajimu UMEMOTO 		 */
1273603724d3SBjoern A. Zeeb 		if (isipv6 && !V_ip6_use_deprecated) {
127433841545SHajimu UMEMOTO 			struct in6_ifaddr *ia6;
127533841545SHajimu UMEMOTO 
12763e88eb90SAndrey V. Elsukov 			ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
12778c0fec80SRobert Watson 			if (ia6 != NULL &&
127833841545SHajimu UMEMOTO 			    (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
12798c0fec80SRobert Watson 				ifa_free(&ia6->ia_ifa);
1280a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1281a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
12828d573cc1SAndre Oppermann 					"Connection attempt to deprecated "
12838d573cc1SAndre Oppermann 					"IPv6 address rejected\n",
1284a160e630SAndre Oppermann 					s, __func__);
128533841545SHajimu UMEMOTO 				rstreason = BANDLIM_RST_OPENPORT;
128633841545SHajimu UMEMOTO 				goto dropwithreset;
128733841545SHajimu UMEMOTO 			}
128877d396fdSMaksim Yevmenkin 			if (ia6)
12898c0fec80SRobert Watson 				ifa_free(&ia6->ia_ifa);
129033841545SHajimu UMEMOTO 		}
1291b287c6c7SBjoern A. Zeeb #endif /* INET6 */
129265f28919SJesper Skriver 		/*
1293db33b3e6SAndre Oppermann 		 * Basic sanity checks on incoming SYN requests:
12948d573cc1SAndre Oppermann 		 *   Don't respond if the destination is a link layer
1295db33b3e6SAndre Oppermann 		 *	broadcast according to RFC1122 4.2.3.10, p. 104.
12968d573cc1SAndre Oppermann 		 *   If it is from this socket it must be forged.
12978d573cc1SAndre Oppermann 		 *   Don't respond if the source or destination is a
12988d573cc1SAndre Oppermann 		 *	global or subnet broad- or multicast address.
129993ec91baSCrist J. Clark 		 *   Note that it is quite possible to receive unicast
130093ec91baSCrist J. Clark 		 *	link-layer packets with a broadcast IP address. Use
130193ec91baSCrist J. Clark 		 *	in_broadcast() to find them.
1302be2ac88cSJonathan Lemon 		 */
13038d573cc1SAndre Oppermann 		if (m->m_flags & (M_BCAST|M_MCAST)) {
13048d573cc1SAndre Oppermann 			if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
13058d573cc1SAndre Oppermann 			    log(LOG_DEBUG, "%s; %s: Listen socket: "
13068d573cc1SAndre Oppermann 				"Connection attempt from broad- or multicast "
1307773673c1SAndre Oppermann 				"link layer address ignored\n", s, __func__);
1308302ce8d6SAndre Oppermann 			goto dropunlock;
13098d573cc1SAndre Oppermann 		}
1310db33b3e6SAndre Oppermann #ifdef INET6
1311b287c6c7SBjoern A. Zeeb 		if (isipv6) {
1312db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1313a160e630SAndre Oppermann 			    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) {
1314a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1315a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13168d573cc1SAndre Oppermann 					"Connection attempt to/from self "
1317773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1318302ce8d6SAndre Oppermann 				goto dropunlock;
1319a160e630SAndre Oppermann 			}
1320be2ac88cSJonathan Lemon 			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1321a160e630SAndre Oppermann 			    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
1322a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1323a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13248d573cc1SAndre Oppermann 					"Connection attempt from/to multicast "
1325773673c1SAndre Oppermann 					"address ignored\n", s, __func__);
1326302ce8d6SAndre Oppermann 				goto dropunlock;
1327a160e630SAndre Oppermann 			}
1328b287c6c7SBjoern A. Zeeb 		}
1329db33b3e6SAndre Oppermann #endif
1330b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
1331b287c6c7SBjoern A. Zeeb 		else
1332b287c6c7SBjoern A. Zeeb #endif
1333b287c6c7SBjoern A. Zeeb #ifdef INET
1334b287c6c7SBjoern A. Zeeb 		{
1335db33b3e6SAndre Oppermann 			if (th->th_dport == th->th_sport &&
1336a160e630SAndre Oppermann 			    ip->ip_dst.s_addr == ip->ip_src.s_addr) {
1337a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1338a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13398d573cc1SAndre Oppermann 					"Connection attempt from/to self "
1340773673c1SAndre Oppermann 					"ignored\n", s, __func__);
1341302ce8d6SAndre Oppermann 				goto dropunlock;
1342a160e630SAndre Oppermann 			}
1343be2ac88cSJonathan Lemon 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
1344be2ac88cSJonathan Lemon 			    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
13452ca2159fSCrist J. Clark 			    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
1346a160e630SAndre Oppermann 			    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
1347a160e630SAndre Oppermann 				if ((s = tcp_log_addrs(&inc, th, NULL, NULL)))
1348a160e630SAndre Oppermann 				    log(LOG_DEBUG, "%s; %s: Listen socket: "
13498d573cc1SAndre Oppermann 					"Connection attempt from/to broad- "
1350773673c1SAndre Oppermann 					"or multicast address ignored\n",
1351a160e630SAndre Oppermann 					s, __func__);
1352302ce8d6SAndre Oppermann 				goto dropunlock;
1353c068736aSJeffrey Hsu 			}
1354a160e630SAndre Oppermann 		}
1355b287c6c7SBjoern A. Zeeb #endif
1356be2ac88cSJonathan Lemon 		/*
1357db33b3e6SAndre Oppermann 		 * SYN appears to be valid.  Create compressed TCP state
1358db33b3e6SAndre Oppermann 		 * for syncache.
1359be2ac88cSJonathan Lemon 		 */
13603c653157SHartmut Brandt #ifdef TCPDEBUG
13613c653157SHartmut Brandt 		if (so->so_options & SO_DEBUG)
13623c653157SHartmut Brandt 			tcp_trace(TA_INPUT, ostate, tp,
13633c653157SHartmut Brandt 			    (void *)tcp_saveipgen, &tcp_savetcp, 0);
13643c653157SHartmut Brandt #endif
13655d06879aSGeorge V. Neville-Neil 		TCP_PROBE3(debug__input, tp, th, mtod(m, const char *));
1366f72167f4SAndre Oppermann 		tcp_dooptions(&to, optp, optlen, TO_SYN);
1367281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
1368281a0fd4SPatrick Kelsey 		if (syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL))
1369281a0fd4SPatrick Kelsey 			goto new_tfo_socket;
1370281a0fd4SPatrick Kelsey #else
137109fe6320SNavdeep Parhar 		syncache_add(&inc, &to, th, inp, &so, m, NULL, NULL);
1372281a0fd4SPatrick Kelsey #endif
1373be2ac88cSJonathan Lemon 		/*
13744d6e7130SAndre Oppermann 		 * Entry added to syncache and mbuf consumed.
1375a7c7f2a7SJohn Baldwin 		 * Only the listen socket is unlocked by syncache_add().
1376be2ac88cSJonathan Lemon 		 */
1377ff9b006dSJulien Charbon 		if (ti_locked == TI_RLOCKED) {
1378ff9b006dSJulien Charbon 			INP_INFO_RUNLOCK(&V_tcbinfo);
1379a7c7f2a7SJohn Baldwin 			ti_locked = TI_UNLOCKED;
1380a7c7f2a7SJohn Baldwin 		}
1381603724d3SBjoern A. Zeeb 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
13828f5a8818SKevin Lo 		return (IPPROTO_DONE);
1383982c1675SAndre Oppermann 	} else if (tp->t_state == TCPS_LISTEN) {
1384982c1675SAndre Oppermann 		/*
1385982c1675SAndre Oppermann 		 * When a listen socket is torn down the SO_ACCEPTCONN
1386982c1675SAndre Oppermann 		 * flag is removed first while connections are drained
1387982c1675SAndre Oppermann 		 * from the accept queue in a unlock/lock cycle of the
1388982c1675SAndre Oppermann 		 * ACCEPT_LOCK, opening a race condition allowing a SYN
1389982c1675SAndre Oppermann 		 * attempt go through unhandled.
1390982c1675SAndre Oppermann 		 */
1391982c1675SAndre Oppermann 		goto dropunlock;
1392f76fcf6dSJeffrey Hsu 	}
1393db33b3e6SAndre Oppermann 
13942903309aSAttilio Rao #ifdef TCP_SIGNATURE
13952903309aSAttilio Rao 	if (sig_checked == 0)  {
13962903309aSAttilio Rao 		tcp_dooptions(&to, optp, optlen,
13972903309aSAttilio Rao 		    (thflags & TH_SYN) ? TO_SYN : 0);
13982903309aSAttilio Rao 		if (!tcp_signature_verify_input(m, off0, tlen, optlen, &to,
13992903309aSAttilio Rao 		    th, tp->t_flags)) {
14002903309aSAttilio Rao 
14012903309aSAttilio Rao 			/*
14022903309aSAttilio Rao 			 * In SYN_SENT state if it receives an RST, it is
14032903309aSAttilio Rao 			 * allowed for further processing.
14042903309aSAttilio Rao 			 */
14052903309aSAttilio Rao 			if ((thflags & TH_RST) == 0 ||
14062903309aSAttilio Rao 			    (tp->t_state == TCPS_SYN_SENT) == 0)
14072903309aSAttilio Rao 				goto dropunlock;
14082903309aSAttilio Rao 		}
14092903309aSAttilio Rao 		sig_checked = 1;
14102903309aSAttilio Rao 	}
14112903309aSAttilio Rao #endif
14122903309aSAttilio Rao 
1413fa22ce15SAdrian Chadd 	TCP_PROBE5(receive, NULL, tp, mtod(m, const char *), tp, th);
141457f60867SMark Johnston 
1415302ce8d6SAndre Oppermann 	/*
14168d573cc1SAndre Oppermann 	 * Segment belongs to a connection in SYN_SENT, ESTABLISHED or later
14178d573cc1SAndre Oppermann 	 * state.  tcp_do_segment() always consumes the mbuf chain, unlocks
14188d573cc1SAndre Oppermann 	 * the inpcb, and unlocks pcbinfo.
1419302ce8d6SAndre Oppermann 	 */
142055bceb1eSRandall Stewart 	tp->t_fb->tfb_tcp_do_segment(m, th, so, tp, drop_hdrlen, tlen, iptos, ti_locked);
1421603724d3SBjoern A. Zeeb 	INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
14228f5a8818SKevin Lo 	return (IPPROTO_DONE);
1423be2ac88cSJonathan Lemon 
1424302ce8d6SAndre Oppermann dropwithreset:
1425fa22ce15SAdrian Chadd 	TCP_PROBE5(receive, NULL, tp, mtod(m, const char *), tp, th);
142657f60867SMark Johnston 
1427ff9b006dSJulien Charbon 	if (ti_locked == TI_RLOCKED) {
1428ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
1429252ca428SRobert Watson 		ti_locked = TI_UNLOCKED;
1430fa046d87SRobert Watson 	}
1431fa046d87SRobert Watson #ifdef INVARIANTS
1432fa046d87SRobert Watson 	else {
1433fa046d87SRobert Watson 		KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropwithreset "
1434fa046d87SRobert Watson 		    "ti_locked: %d", __func__, ti_locked));
1435fa046d87SRobert Watson 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1436fa046d87SRobert Watson 	}
1437fa046d87SRobert Watson #endif
1438014ea782SRobert Watson 
1439014ea782SRobert Watson 	if (inp != NULL) {
1440302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
1441014ea782SRobert Watson 		INP_WUNLOCK(inp);
1442dd8ac7f9SRobert Watson 	} else
1443014ea782SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
1444302ce8d6SAndre Oppermann 	m = NULL;	/* mbuf chain got consumed. */
1445014ea782SRobert Watson 	goto drop;
1446014ea782SRobert Watson 
1447302ce8d6SAndre Oppermann dropunlock:
144857f60867SMark Johnston 	if (m != NULL)
1449fa22ce15SAdrian Chadd 		TCP_PROBE5(receive, NULL, tp, mtod(m, const char *), tp, th);
145057f60867SMark Johnston 
1451ff9b006dSJulien Charbon 	if (ti_locked == TI_RLOCKED) {
1452ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
1453252ca428SRobert Watson 		ti_locked = TI_UNLOCKED;
1454fa046d87SRobert Watson 	}
1455fa046d87SRobert Watson #ifdef INVARIANTS
1456fa046d87SRobert Watson 	else {
1457fa046d87SRobert Watson 		KASSERT(ti_locked == TI_UNLOCKED, ("%s: dropunlock "
1458fa046d87SRobert Watson 		    "ti_locked: %d", __func__, ti_locked));
1459fa046d87SRobert Watson 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1460fa046d87SRobert Watson 	}
1461fa046d87SRobert Watson #endif
1462252ca428SRobert Watson 
14639fa198beSAndre Oppermann 	if (inp != NULL)
14648501a69cSRobert Watson 		INP_WUNLOCK(inp);
1465014ea782SRobert Watson 
1466302ce8d6SAndre Oppermann drop:
1467603724d3SBjoern A. Zeeb 	INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1468a160e630SAndre Oppermann 	if (s != NULL)
1469a160e630SAndre Oppermann 		free(s, M_TCPLOG);
1470302ce8d6SAndre Oppermann 	if (m != NULL)
1471302ce8d6SAndre Oppermann 		m_freem(m);
14728f5a8818SKevin Lo 	return (IPPROTO_DONE);
1473302ce8d6SAndre Oppermann }
1474302ce8d6SAndre Oppermann 
147555bceb1eSRandall Stewart void
1476302ce8d6SAndre Oppermann tcp_do_segment(struct mbuf *m, struct tcphdr *th, struct socket *so,
1477252ca428SRobert Watson     struct tcpcb *tp, int drop_hdrlen, int tlen, uint8_t iptos,
1478252ca428SRobert Watson     int ti_locked)
1479302ce8d6SAndre Oppermann {
1480021eaf79SHiren Panchasara 	int thflags, acked, ourfinisacked, needoutput = 0, sack_changed;
1481302ce8d6SAndre Oppermann 	int rstreason, todrop, win;
1482302ce8d6SAndre Oppermann 	u_long tiwin;
148307dacf03SAndre Oppermann 	char *s;
148407dacf03SAndre Oppermann 	struct in_conninfo *inc;
1485c11a15bfSGleb Smirnoff 	struct mbuf *mfree;
1486302ce8d6SAndre Oppermann 	struct tcpopt to;
1487281a0fd4SPatrick Kelsey 	int tfo_syn;
1488302ce8d6SAndre Oppermann 
148914739780SMaxim Konovalov #ifdef TCPDEBUG
149014739780SMaxim Konovalov 	/*
149114739780SMaxim Konovalov 	 * The size of tcp_saveipgen must be the size of the max ip header,
149214739780SMaxim Konovalov 	 * now IPv6.
149314739780SMaxim Konovalov 	 */
149414739780SMaxim Konovalov 	u_char tcp_saveipgen[IP6_HDR_LEN];
149514739780SMaxim Konovalov 	struct tcphdr tcp_savetcp;
149614739780SMaxim Konovalov 	short ostate = 0;
149714739780SMaxim Konovalov #endif
1498302ce8d6SAndre Oppermann 	thflags = th->th_flags;
149907dacf03SAndre Oppermann 	inc = &tp->t_inpcb->inp_inc;
1500d64a46eaSLawrence Stewart 	tp->sackhint.last_sack_ack = 0;
1501021eaf79SHiren Panchasara 	sack_changed = 0;
1502302ce8d6SAndre Oppermann 
1503252ca428SRobert Watson 	/*
1504252ca428SRobert Watson 	 * If this is either a state-changing packet or current state isn't
1505252ca428SRobert Watson 	 * established, we require a write lock on tcbinfo.  Otherwise, we
15060989f56cSRobert Watson 	 * allow the tcbinfo to be in either alocked or unlocked, as the
15070989f56cSRobert Watson 	 * caller may have unnecessarily acquired a write lock due to a race.
1508252ca428SRobert Watson 	 */
1509252ca428SRobert Watson 	if ((thflags & (TH_SYN | TH_FIN | TH_RST)) != 0 ||
1510252ca428SRobert Watson 	    tp->t_state != TCPS_ESTABLISHED) {
1511ff9b006dSJulien Charbon 		KASSERT(ti_locked == TI_RLOCKED, ("%s ti_locked %d for "
1512252ca428SRobert Watson 		    "SYN/FIN/RST/!EST", __func__, ti_locked));
1513ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1514252ca428SRobert Watson 	} else {
1515252ca428SRobert Watson #ifdef INVARIANTS
1516ff9b006dSJulien Charbon 		if (ti_locked == TI_RLOCKED)
1517ff9b006dSJulien Charbon 			INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
1518fa046d87SRobert Watson 		else {
1519fa046d87SRobert Watson 			KASSERT(ti_locked == TI_UNLOCKED, ("%s: EST "
1520fa046d87SRobert Watson 			    "ti_locked: %d", __func__, ti_locked));
1521fa046d87SRobert Watson 			INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
1522fa046d87SRobert Watson 		}
1523252ca428SRobert Watson #endif
1524252ca428SRobert Watson 	}
15258501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
1526679d9708SAndre Oppermann 	KASSERT(tp->t_state > TCPS_LISTEN, ("%s: TCPS_LISTEN",
1527679d9708SAndre Oppermann 	    __func__));
1528679d9708SAndre Oppermann 	KASSERT(tp->t_state != TCPS_TIME_WAIT, ("%s: TCPS_TIME_WAIT",
1529679d9708SAndre Oppermann 	    __func__));
1530df8bae1dSRodney W. Grimes 
153186a996e6SHiren Panchasara #ifdef TCPPCAP
153286a996e6SHiren Panchasara 	/* Save segment, if requested. */
153386a996e6SHiren Panchasara 	tcp_pcap_add(th, m, &(tp->t_inpkts));
153486a996e6SHiren Panchasara #endif
153586a996e6SHiren Panchasara 
1536df8bae1dSRodney W. Grimes 	/*
1537df8bae1dSRodney W. Grimes 	 * Segment received on connection.
1538df8bae1dSRodney W. Grimes 	 * Reset idle time and keep-alive timer.
1539e8949f74SAndre Oppermann 	 * XXX: This should be done after segment
1540e8949f74SAndre Oppermann 	 * validation to ignore broken/spoofed segs.
1541df8bae1dSRodney W. Grimes 	 */
15429b8b58e0SJonathan Lemon 	tp->t_rcvtime = ticks;
15437ff19458SPaul Traina 	if (TCPS_HAVEESTABLISHED(tp->t_state))
15449077f387SGleb Smirnoff 		tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp));
1545df8bae1dSRodney W. Grimes 
1546df8bae1dSRodney W. Grimes 	/*
1547c52102ddSHiren Panchasara 	 * Scale up the window into a 32-bit value.
1548e8949f74SAndre Oppermann 	 * For the SYN_SENT state the scale is zero.
1549464fcfbcSAndre Oppermann 	 */
1550464fcfbcSAndre Oppermann 	tiwin = th->th_win << tp->snd_scale;
1551464fcfbcSAndre Oppermann 
1552464fcfbcSAndre Oppermann 	/*
1553f2512ba1SRui Paulo 	 * TCP ECN processing.
1554f2512ba1SRui Paulo 	 */
1555f2512ba1SRui Paulo 	if (tp->t_flags & TF_ECN_PERMIT) {
15569c251892SRui Paulo 		if (thflags & TH_CWR)
15579c251892SRui Paulo 			tp->t_flags &= ~TF_ECN_SND_ECE;
1558f2512ba1SRui Paulo 		switch (iptos & IPTOS_ECN_MASK) {
1559f2512ba1SRui Paulo 		case IPTOS_ECN_CE:
1560f2512ba1SRui Paulo 			tp->t_flags |= TF_ECN_SND_ECE;
156178b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ce);
1562f2512ba1SRui Paulo 			break;
1563f2512ba1SRui Paulo 		case IPTOS_ECN_ECT0:
156478b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ect0);
1565f2512ba1SRui Paulo 			break;
1566f2512ba1SRui Paulo 		case IPTOS_ECN_ECT1:
156778b50714SRobert Watson 			TCPSTAT_INC(tcps_ecn_ect1);
1568f2512ba1SRui Paulo 			break;
1569f2512ba1SRui Paulo 		}
157064807b30SHiren Panchasara 
157164807b30SHiren Panchasara 		/* Process a packet differently from RFC3168. */
157264807b30SHiren Panchasara 		cc_ecnpkt_handler(tp, th, iptos);
157364807b30SHiren Panchasara 
1574dbc42409SLawrence Stewart 		/* Congestion experienced. */
1575dbc42409SLawrence Stewart 		if (thflags & TH_ECE) {
1576dbc42409SLawrence Stewart 			cc_cong_signal(tp, th, CC_ECN);
1577f2512ba1SRui Paulo 		}
1578f2512ba1SRui Paulo 	}
1579f2512ba1SRui Paulo 
1580f2512ba1SRui Paulo 	/*
1581f72167f4SAndre Oppermann 	 * Parse options on any incoming segment.
1582f72167f4SAndre Oppermann 	 */
1583302ce8d6SAndre Oppermann 	tcp_dooptions(&to, (u_char *)(th + 1),
1584302ce8d6SAndre Oppermann 	    (th->th_off << 2) - sizeof(struct tcphdr),
1585302ce8d6SAndre Oppermann 	    (thflags & TH_SYN) ? TO_SYN : 0);
1586f72167f4SAndre Oppermann 
1587f72167f4SAndre Oppermann 	/*
1588f72167f4SAndre Oppermann 	 * If echoed timestamp is later than the current time,
1589bf6d304aSAndre Oppermann 	 * fall back to non RFC1323 RTT calculation.  Normalize
1590bf6d304aSAndre Oppermann 	 * timestamp if syncookies were used when this connection
1591bf6d304aSAndre Oppermann 	 * was established.
1592f72167f4SAndre Oppermann 	 */
1593bf6d304aSAndre Oppermann 	if ((to.to_flags & TOF_TS) && (to.to_tsecr != 0)) {
1594e16fa5caSJohn-Mark Gurney 		to.to_tsecr -= tp->ts_offset;
1595d8951c8aSBjoern A. Zeeb 		if (TSTMP_GT(to.to_tsecr, tcp_ts_getticks()))
1596f72167f4SAndre Oppermann 			to.to_tsecr = 0;
1597bf6d304aSAndre Oppermann 	}
159807dacf03SAndre Oppermann 	/*
159907dacf03SAndre Oppermann 	 * If timestamps were negotiated during SYN/ACK they should
160007dacf03SAndre Oppermann 	 * appear on every segment during this session and vice versa.
160107dacf03SAndre Oppermann 	 */
160207dacf03SAndre Oppermann 	if ((tp->t_flags & TF_RCVD_TSTMP) && !(to.to_flags & TOF_TS)) {
160307dacf03SAndre Oppermann 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
160407dacf03SAndre Oppermann 			log(LOG_DEBUG, "%s; %s: Timestamp missing, "
160507dacf03SAndre Oppermann 			    "no action\n", s, __func__);
160607dacf03SAndre Oppermann 			free(s, M_TCPLOG);
160707dacf03SAndre Oppermann 		}
160807dacf03SAndre Oppermann 	}
160907dacf03SAndre Oppermann 	if (!(tp->t_flags & TF_RCVD_TSTMP) && (to.to_flags & TOF_TS)) {
161007dacf03SAndre Oppermann 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
161107dacf03SAndre Oppermann 			log(LOG_DEBUG, "%s; %s: Timestamp not expected, "
161207dacf03SAndre Oppermann 			    "no action\n", s, __func__);
161307dacf03SAndre Oppermann 			free(s, M_TCPLOG);
161407dacf03SAndre Oppermann 		}
161507dacf03SAndre Oppermann 	}
1616f72167f4SAndre Oppermann 
1617f72167f4SAndre Oppermann 	/*
161897d8d152SAndre Oppermann 	 * Process options only when we get SYN/ACK back. The SYN case
161997d8d152SAndre Oppermann 	 * for incoming connections is handled in tcp_syncache.
16205396d0f8SAndre Oppermann 	 * According to RFC1323 the window field in a SYN (i.e., a <SYN>
16215396d0f8SAndre Oppermann 	 * or <SYN,ACK>) segment itself is never scaled.
162297d8d152SAndre Oppermann 	 * XXX this is traditional behavior, may need to be cleaned up.
1623df8bae1dSRodney W. Grimes 	 */
16240a389eabSSimon L. B. Nielsen 	if (tp->t_state == TCPS_SYN_SENT && (thflags & TH_SYN)) {
1625464fcfbcSAndre Oppermann 		if ((to.to_flags & TOF_SCALE) &&
1626464fcfbcSAndre Oppermann 		    (tp->t_flags & TF_REQ_SCALE)) {
1627be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_SCALE;
162802a1a643SAndre Oppermann 			tp->snd_scale = to.to_wscale;
1629be2ac88cSJonathan Lemon 		}
16305396d0f8SAndre Oppermann 		/*
16315396d0f8SAndre Oppermann 		 * Initial send window.  It will be updated with
16325396d0f8SAndre Oppermann 		 * the next incoming segment to the scaled value.
16335396d0f8SAndre Oppermann 		 */
16345396d0f8SAndre Oppermann 		tp->snd_wnd = th->th_win;
1635be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_TS) {
1636be2ac88cSJonathan Lemon 			tp->t_flags |= TF_RCVD_TSTMP;
1637be2ac88cSJonathan Lemon 			tp->ts_recent = to.to_tsval;
1638d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
1639be2ac88cSJonathan Lemon 		}
1640be2ac88cSJonathan Lemon 		if (to.to_flags & TOF_MSS)
1641be2ac88cSJonathan Lemon 			tcp_mss(tp, to.to_mss);
16423529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
16433529149eSAndre Oppermann 		    (to.to_flags & TOF_SACKPERM) == 0)
16443529149eSAndre Oppermann 			tp->t_flags &= ~TF_SACK_PERMIT;
16456d90faf3SPaul Saab 	}
16466d90faf3SPaul Saab 
1647df8bae1dSRodney W. Grimes 	/*
1648df8bae1dSRodney W. Grimes 	 * Header prediction: check for the two common cases
1649df8bae1dSRodney W. Grimes 	 * of a uni-directional data xfer.  If the packet has
1650df8bae1dSRodney W. Grimes 	 * no control flags, is in-sequence, the window didn't
1651df8bae1dSRodney W. Grimes 	 * change and we're not retransmitting, it's a
1652df8bae1dSRodney W. Grimes 	 * candidate.  If the length is zero and the ack moved
1653df8bae1dSRodney W. Grimes 	 * forward, we're the sender side of the xfer.  Just
1654df8bae1dSRodney W. Grimes 	 * free the data acked & wake any higher level process
1655df8bae1dSRodney W. Grimes 	 * that was blocked waiting for space.  If the length
1656df8bae1dSRodney W. Grimes 	 * is non-zero and the ack didn't move, we're the
1657df8bae1dSRodney W. Grimes 	 * receiver side.  If we're getting packets in-order
1658df8bae1dSRodney W. Grimes 	 * (the reassembly queue is empty), add the data to
1659df8bae1dSRodney W. Grimes 	 * the socket buffer and note that we need a delayed ack.
1660a0292f23SGarrett Wollman 	 * Make sure that the hidden state-flags are also off.
1661c5ad39b9SAndre Oppermann 	 * Since we check for TCPS_ESTABLISHED first, it can only
1662a0292f23SGarrett Wollman 	 * be TH_NEEDSYN.
1663df8bae1dSRodney W. Grimes 	 */
1664df8bae1dSRodney W. Grimes 	if (tp->t_state == TCPS_ESTABLISHED &&
1665c5ad39b9SAndre Oppermann 	    th->th_seq == tp->rcv_nxt &&
1666fb59c426SYoshinobu Inoue 	    (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1667c5ad39b9SAndre Oppermann 	    tp->snd_nxt == tp->snd_max &&
1668c5ad39b9SAndre Oppermann 	    tiwin && tiwin == tp->snd_wnd &&
1669a0292f23SGarrett Wollman 	    ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
16704741bfcbSPatrick Kelsey 	    LIST_EMPTY(&tp->t_segq) &&
16714741bfcbSPatrick Kelsey 	    ((to.to_flags & TOF_TS) == 0 ||
1672c5ad39b9SAndre Oppermann 	     TSTMP_GEQ(to.to_tsval, tp->ts_recent)) ) {
1673df8bae1dSRodney W. Grimes 
1674df8bae1dSRodney W. Grimes 		/*
1675df8bae1dSRodney W. Grimes 		 * If last ACK falls within this segment's sequence numbers,
1676df8bae1dSRodney W. Grimes 		 * record the timestamp.
1677a0292f23SGarrett Wollman 		 * NOTE that the test is modified according to the latest
1678a0292f23SGarrett Wollman 		 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1679df8bae1dSRodney W. Grimes 		 */
1680be2ac88cSJonathan Lemon 		if ((to.to_flags & TOF_TS) != 0 &&
1681fb59c426SYoshinobu Inoue 		    SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1682d8951c8aSBjoern A. Zeeb 			tp->ts_recent_age = tcp_ts_getticks();
1683a0292f23SGarrett Wollman 			tp->ts_recent = to.to_tsval;
1684df8bae1dSRodney W. Grimes 		}
1685df8bae1dSRodney W. Grimes 
1686fb59c426SYoshinobu Inoue 		if (tlen == 0) {
1687fb59c426SYoshinobu Inoue 			if (SEQ_GT(th->th_ack, tp->snd_una) &&
1688fb59c426SYoshinobu Inoue 			    SEQ_LEQ(th->th_ack, tp->snd_max) &&
1689dbc42409SLawrence Stewart 			    !IN_RECOVERY(tp->t_flags) &&
1690fc30a251SAndre Oppermann 			    (to.to_flags & TOF_SACK) == 0 &&
1691dbc42409SLawrence Stewart 			    TAILQ_EMPTY(&tp->snd_holes)) {
1692df8bae1dSRodney W. Grimes 				/*
1693e8949f74SAndre Oppermann 				 * This is a pure ack for outstanding data.
1694df8bae1dSRodney W. Grimes 				 */
1695ff9b006dSJulien Charbon 				if (ti_locked == TI_RLOCKED)
1696ff9b006dSJulien Charbon 					INP_INFO_RUNLOCK(&V_tcbinfo);
1697252ca428SRobert Watson 				ti_locked = TI_UNLOCKED;
1698252ca428SRobert Watson 
169978b50714SRobert Watson 				TCPSTAT_INC(tcps_predack);
1700252ca428SRobert Watson 
17019b8b58e0SJonathan Lemon 				/*
1702e8949f74SAndre Oppermann 				 * "bad retransmit" recovery.
17039b8b58e0SJonathan Lemon 				 */
17049b8b58e0SJonathan Lemon 				if (tp->t_rxtshift == 1 &&
1705672dc4aeSJohn Baldwin 				    tp->t_flags & TF_PREVVALID &&
17066dfb8b31SJohn Baldwin 				    (int)(ticks - tp->t_badrxtwin) < 0) {
1707dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_RTO_ERR);
17089b8b58e0SJonathan Lemon 				}
1709fa55172bSMatthew Dillon 
1710fa55172bSMatthew Dillon 				/*
1711fa55172bSMatthew Dillon 				 * Recalculate the transmit timer / rtt.
1712fa55172bSMatthew Dillon 				 *
1713fa55172bSMatthew Dillon 				 * Some boxes send broken timestamp replies
1714fa55172bSMatthew Dillon 				 * during the SYN+ACK phase, ignore
1715fa55172bSMatthew Dillon 				 * timestamps of 0 or we could calculate a
1716fa55172bSMatthew Dillon 				 * huge RTT and blow up the retransmit timer.
1717fa55172bSMatthew Dillon 				 */
1718fa55172bSMatthew Dillon 				if ((to.to_flags & TOF_TS) != 0 &&
1719fa55172bSMatthew Dillon 				    to.to_tsecr) {
1720d8951c8aSBjoern A. Zeeb 					u_int t;
1721d8951c8aSBjoern A. Zeeb 
1722d8951c8aSBjoern A. Zeeb 					t = tcp_ts_getticks() - to.to_tsecr;
1723d8951c8aSBjoern A. Zeeb 					if (!tp->t_rttlow || tp->t_rttlow > t)
1724d8951c8aSBjoern A. Zeeb 						tp->t_rttlow = t;
1725a0292f23SGarrett Wollman 					tcp_xmit_timer(tp,
1726d8951c8aSBjoern A. Zeeb 					    TCP_TS_TO_TICKS(t) + 1);
1727fa55172bSMatthew Dillon 				} else if (tp->t_rtttime &&
1728fa55172bSMatthew Dillon 				    SEQ_GT(th->th_ack, tp->t_rtseq)) {
1729eaf80179SAndre Oppermann 					if (!tp->t_rttlow ||
1730eaf80179SAndre Oppermann 					    tp->t_rttlow > ticks - tp->t_rtttime)
1731eaf80179SAndre Oppermann 						tp->t_rttlow = ticks - tp->t_rtttime;
1732c068736aSJeffrey Hsu 					tcp_xmit_timer(tp,
1733c068736aSJeffrey Hsu 							ticks - tp->t_rtttime);
1734fa55172bSMatthew Dillon 				}
1735dbc42409SLawrence Stewart 				acked = BYTES_THIS_ACK(tp, th);
173639bc9de5SLawrence Stewart 
173739bc9de5SLawrence Stewart 				/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
173839bc9de5SLawrence Stewart 				hhook_run_tcp_est_in(tp, th, &to);
173939bc9de5SLawrence Stewart 
174078b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvackpack);
174178b50714SRobert Watson 				TCPSTAT_ADD(tcps_rcvackbyte, acked);
1742df8bae1dSRodney W. Grimes 				sbdrop(&so->so_snd, acked);
17439d11646dSJeffrey Hsu 				if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
17449d11646dSJeffrey Hsu 				    SEQ_LEQ(th->th_ack, tp->snd_recover))
17459d11646dSJeffrey Hsu 					tp->snd_recover = th->th_ack - 1;
1746dbc42409SLawrence Stewart 
1747dbc42409SLawrence Stewart 				/*
1748dbc42409SLawrence Stewart 				 * Let the congestion control algorithm update
1749dbc42409SLawrence Stewart 				 * congestion control related information. This
1750dbc42409SLawrence Stewart 				 * typically means increasing the congestion
1751dbc42409SLawrence Stewart 				 * window.
1752dbc42409SLawrence Stewart 				 */
1753dbc42409SLawrence Stewart 				cc_ack_received(tp, th, CC_ACK);
1754dbc42409SLawrence Stewart 
17559d11646dSJeffrey Hsu 				tp->snd_una = th->th_ack;
17561645d090SJeff Roberson 				/*
1757e8949f74SAndre Oppermann 				 * Pull snd_wl2 up to prevent seq wrap relative
17581645d090SJeff Roberson 				 * to th_ack.
17591645d090SJeff Roberson 				 */
1760cb942153SJeffrey Hsu 				tp->snd_wl2 = th->th_ack;
1761b5addd85SJeffrey Hsu 				tp->t_dupacks = 0;
1762df8bae1dSRodney W. Grimes 				m_freem(m);
1763df8bae1dSRodney W. Grimes 
1764df8bae1dSRodney W. Grimes 				/*
1765df8bae1dSRodney W. Grimes 				 * If all outstanding data are acked, stop
1766df8bae1dSRodney W. Grimes 				 * retransmit timer, otherwise restart timer
1767df8bae1dSRodney W. Grimes 				 * using current (possibly backed-off) value.
1768df8bae1dSRodney W. Grimes 				 * If process is waiting for space,
1769df8bae1dSRodney W. Grimes 				 * wakeup/selwakeup/signal.  If data
1770df8bae1dSRodney W. Grimes 				 * are ready to send, let tcp_output
1771df8bae1dSRodney W. Grimes 				 * decide between more output or persist.
1772e8949f74SAndre Oppermann 				 */
17733c653157SHartmut Brandt #ifdef TCPDEBUG
17743c653157SHartmut Brandt 				if (so->so_options & SO_DEBUG)
17753c653157SHartmut Brandt 					tcp_trace(TA_INPUT, ostate, tp,
17763c653157SHartmut Brandt 					    (void *)tcp_saveipgen,
17773c653157SHartmut Brandt 					    &tcp_savetcp, 0);
17783c653157SHartmut Brandt #endif
17795d06879aSGeorge V. Neville-Neil 				TCP_PROBE3(debug__input, tp, th,
17805d06879aSGeorge V. Neville-Neil 					mtod(m, const char *));
1781df8bae1dSRodney W. Grimes 				if (tp->snd_una == tp->snd_max)
1782b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
1783b8152ba7SAndre Oppermann 				else if (!tcp_timer_active(tp, TT_PERSIST))
1784b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT,
1785b8152ba7SAndre Oppermann 						      tp->t_rxtcur);
1786df8bae1dSRodney W. Grimes 				sowwakeup(so);
1787cfa6009eSGleb Smirnoff 				if (sbavail(&so->so_snd))
178855bceb1eSRandall Stewart 					(void) tp->t_fb->tfb_tcp_output(tp);
1789a14c749fSJonathan Lemon 				goto check_delack;
1790df8bae1dSRodney W. Grimes 			}
1791fb59c426SYoshinobu Inoue 		} else if (th->th_ack == tp->snd_una &&
1792fb59c426SYoshinobu Inoue 		    tlen <= sbspace(&so->so_rcv)) {
17936741ecf5SAndre Oppermann 			int newsize = 0;	/* automatic sockbuf scaling */
17946741ecf5SAndre Oppermann 
1795df8bae1dSRodney W. Grimes 			/*
1796252ca428SRobert Watson 			 * This is a pure, in-sequence data packet with
1797252ca428SRobert Watson 			 * nothing on the reassembly queue and we have enough
1798252ca428SRobert Watson 			 * buffer space to take it.
1799df8bae1dSRodney W. Grimes 			 */
1800ff9b006dSJulien Charbon 			if (ti_locked == TI_RLOCKED)
1801ff9b006dSJulien Charbon 				INP_INFO_RUNLOCK(&V_tcbinfo);
1802252ca428SRobert Watson 			ti_locked = TI_UNLOCKED;
1803252ca428SRobert Watson 
18046d90faf3SPaul Saab 			/* Clean receiver SACK report if present */
18053529149eSAndre Oppermann 			if ((tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks)
18066d90faf3SPaul Saab 				tcp_clean_sackreport(tp);
180778b50714SRobert Watson 			TCPSTAT_INC(tcps_preddat);
1808fb59c426SYoshinobu Inoue 			tp->rcv_nxt += tlen;
18091645d090SJeff Roberson 			/*
18101645d090SJeff Roberson 			 * Pull snd_wl1 up to prevent seq wrap relative to
18111645d090SJeff Roberson 			 * th_seq.
18121645d090SJeff Roberson 			 */
181360ee3bb2SAndre Oppermann 			tp->snd_wl1 = th->th_seq;
18141645d090SJeff Roberson 			/*
18151645d090SJeff Roberson 			 * Pull rcv_up up to prevent seq wrap relative to
18161645d090SJeff Roberson 			 * rcv_nxt.
18171645d090SJeff Roberson 			 */
18181645d090SJeff Roberson 			tp->rcv_up = tp->rcv_nxt;
181978b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpack);
182078b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
18213c653157SHartmut Brandt #ifdef TCPDEBUG
18223c653157SHartmut Brandt 			if (so->so_options & SO_DEBUG)
18233c653157SHartmut Brandt 				tcp_trace(TA_INPUT, ostate, tp,
18243c653157SHartmut Brandt 				    (void *)tcp_saveipgen, &tcp_savetcp, 0);
18253c653157SHartmut Brandt #endif
18265d06879aSGeorge V. Neville-Neil 			TCP_PROBE3(debug__input, tp, th, mtod(m, const char *));
18275d06879aSGeorge V. Neville-Neil 
18286741ecf5SAndre Oppermann 		/*
18296741ecf5SAndre Oppermann 		 * Automatic sizing of receive socket buffer.  Often the send
18306741ecf5SAndre Oppermann 		 * buffer size is not optimally adjusted to the actual network
18316741ecf5SAndre Oppermann 		 * conditions at hand (delay bandwidth product).  Setting the
18326741ecf5SAndre Oppermann 		 * buffer size too small limits throughput on links with high
18336741ecf5SAndre Oppermann 		 * bandwidth and high delay (eg. trans-continental/oceanic links).
18346741ecf5SAndre Oppermann 		 *
18356741ecf5SAndre Oppermann 		 * On the receive side the socket buffer memory is only rarely
18366741ecf5SAndre Oppermann 		 * used to any significant extent.  This allows us to be much
18376741ecf5SAndre Oppermann 		 * more aggressive in scaling the receive socket buffer.  For
18386741ecf5SAndre Oppermann 		 * the case that the buffer space is actually used to a large
18396741ecf5SAndre Oppermann 		 * extent and we run out of kernel memory we can simply drop
18406741ecf5SAndre Oppermann 		 * the new segments; TCP on the sender will just retransmit it
18416741ecf5SAndre Oppermann 		 * later.  Setting the buffer size too big may only consume too
18426741ecf5SAndre Oppermann 		 * much kernel memory if the application doesn't read() from
18436741ecf5SAndre Oppermann 		 * the socket or packet loss or reordering makes use of the
18446741ecf5SAndre Oppermann 		 * reassembly queue.
18456741ecf5SAndre Oppermann 		 *
18466741ecf5SAndre Oppermann 		 * The criteria to step up the receive buffer one notch are:
1847f7469d3eSHiren Panchasara 		 *  1. Application has not set receive buffer size with
1848f7469d3eSHiren Panchasara 		 *     SO_RCVBUF. Setting SO_RCVBUF clears SB_AUTOSIZE.
1849f7469d3eSHiren Panchasara 		 *  2. the number of bytes received during the time it takes
18506741ecf5SAndre Oppermann 		 *     one timestamp to be reflected back to us (the RTT);
1851f7469d3eSHiren Panchasara 		 *  3. received bytes per RTT is within seven eighth of the
18526741ecf5SAndre Oppermann 		 *     current socket buffer size;
1853f7469d3eSHiren Panchasara 		 *  4. receive buffer size has not hit maximal automatic size;
18546741ecf5SAndre Oppermann 		 *
18556741ecf5SAndre Oppermann 		 * This algorithm does one step per RTT at most and only if
18566741ecf5SAndre Oppermann 		 * we receive a bulk stream w/o packet losses or reorderings.
18576741ecf5SAndre Oppermann 		 * Shrinking the buffer during idle times is not necessary as
18586741ecf5SAndre Oppermann 		 * it doesn't consume any memory when idle.
18596741ecf5SAndre Oppermann 		 *
18606741ecf5SAndre Oppermann 		 * TODO: Only step up if the application is actually serving
18616741ecf5SAndre Oppermann 		 * the buffer to better manage the socket buffer resources.
1862df8bae1dSRodney W. Grimes 		 */
1863603724d3SBjoern A. Zeeb 			if (V_tcp_do_autorcvbuf &&
1864d57724fdSPatrick Kelsey 			    (to.to_flags & TOF_TS) &&
18656741ecf5SAndre Oppermann 			    to.to_tsecr &&
18666741ecf5SAndre Oppermann 			    (so->so_rcv.sb_flags & SB_AUTOSIZE)) {
18678502ec25SAndre Oppermann 				if (TSTMP_GT(to.to_tsecr, tp->rfbuf_ts) &&
18686741ecf5SAndre Oppermann 				    to.to_tsecr - tp->rfbuf_ts < hz) {
18696741ecf5SAndre Oppermann 					if (tp->rfbuf_cnt >
18706741ecf5SAndre Oppermann 					    (so->so_rcv.sb_hiwat / 8 * 7) &&
18716741ecf5SAndre Oppermann 					    so->so_rcv.sb_hiwat <
1872603724d3SBjoern A. Zeeb 					    V_tcp_autorcvbuf_max) {
18736741ecf5SAndre Oppermann 						newsize =
18746741ecf5SAndre Oppermann 						    min(so->so_rcv.sb_hiwat +
1875603724d3SBjoern A. Zeeb 						    V_tcp_autorcvbuf_inc,
1876603724d3SBjoern A. Zeeb 						    V_tcp_autorcvbuf_max);
18776741ecf5SAndre Oppermann 					}
18786741ecf5SAndre Oppermann 					/* Start over with next RTT. */
18796741ecf5SAndre Oppermann 					tp->rfbuf_ts = 0;
18806741ecf5SAndre Oppermann 					tp->rfbuf_cnt = 0;
18816741ecf5SAndre Oppermann 				} else
18826741ecf5SAndre Oppermann 					tp->rfbuf_cnt += tlen;	/* add up */
18836741ecf5SAndre Oppermann 			}
18846741ecf5SAndre Oppermann 
18856741ecf5SAndre Oppermann 			/* Add data to socket buffer. */
18861e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
1887c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
1888c1c36a2cSMike Silbersack 				m_freem(m);
1889c1c36a2cSMike Silbersack 			} else {
18906741ecf5SAndre Oppermann 				/*
18916741ecf5SAndre Oppermann 				 * Set new socket buffer size.
18926741ecf5SAndre Oppermann 				 * Give up when limit is reached.
18936741ecf5SAndre Oppermann 				 */
18946741ecf5SAndre Oppermann 				if (newsize)
18956741ecf5SAndre Oppermann 					if (!sbreserve_locked(&so->so_rcv,
18966c8286e4SRobert Watson 					    newsize, so, NULL))
18976741ecf5SAndre Oppermann 						so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
1898fb59c426SYoshinobu Inoue 				m_adj(m, drop_hdrlen);	/* delayed header drop */
1899651e4e6aSGleb Smirnoff 				sbappendstream_locked(&so->so_rcv, m, 0);
1900c1c36a2cSMike Silbersack 			}
1901e8949f74SAndre Oppermann 			/* NB: sorwakeup_locked() does an implicit unlock. */
19021e4d7da7SRobert Watson 			sorwakeup_locked(so);
1903c1e5a6e5SAndre Oppermann 			if (DELAY_ACK(tp, tlen)) {
19043bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
1905f498eeeeSDavid Greenman 			} else {
1906e612a582SDavid Greenman 				tp->t_flags |= TF_ACKNOW;
190755bceb1eSRandall Stewart 				tp->t_fb->tfb_tcp_output(tp);
1908e612a582SDavid Greenman 			}
1909a14c749fSJonathan Lemon 			goto check_delack;
1910df8bae1dSRodney W. Grimes 		}
1911df8bae1dSRodney W. Grimes 	}
1912df8bae1dSRodney W. Grimes 
1913df8bae1dSRodney W. Grimes 	/*
1914df8bae1dSRodney W. Grimes 	 * Calculate amount of space in receive window,
1915df8bae1dSRodney W. Grimes 	 * and then do TCP input processing.
1916df8bae1dSRodney W. Grimes 	 * Receive window is amount of space in rcv queue,
1917df8bae1dSRodney W. Grimes 	 * but not less than advertised window.
1918df8bae1dSRodney W. Grimes 	 */
1919df8bae1dSRodney W. Grimes 	win = sbspace(&so->so_rcv);
1920df8bae1dSRodney W. Grimes 	if (win < 0)
1921df8bae1dSRodney W. Grimes 		win = 0;
192266e39adcSJohn Polstra 	tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1923df8bae1dSRodney W. Grimes 
19246741ecf5SAndre Oppermann 	/* Reset receive buffer auto scaling when not in bulk receive mode. */
19256741ecf5SAndre Oppermann 	tp->rfbuf_ts = 0;
19266741ecf5SAndre Oppermann 	tp->rfbuf_cnt = 0;
19276741ecf5SAndre Oppermann 
1928df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
1929df8bae1dSRodney W. Grimes 
1930df8bae1dSRodney W. Grimes 	/*
1931764d8cefSBill Fenner 	 * If the state is SYN_RECEIVED:
1932764d8cefSBill Fenner 	 *	if seg contains an ACK, but not for our SYN/ACK, send a RST.
1933764d8cefSBill Fenner 	 */
1934764d8cefSBill Fenner 	case TCPS_SYN_RECEIVED:
1935fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1936fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1937a57815efSBosko Milekic 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1938a57815efSBosko Milekic 				rstreason = BANDLIM_RST_OPENPORT;
1939a57815efSBosko Milekic 				goto dropwithreset;
1940a57815efSBosko Milekic 		}
1941281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
1942281a0fd4SPatrick Kelsey 		if (tp->t_flags & TF_FASTOPEN) {
1943281a0fd4SPatrick Kelsey 			/*
1944281a0fd4SPatrick Kelsey 			 * When a TFO connection is in SYN_RECEIVED, the
1945281a0fd4SPatrick Kelsey 			 * only valid packets are the initial SYN, a
1946281a0fd4SPatrick Kelsey 			 * retransmit/copy of the initial SYN (possibly with
1947281a0fd4SPatrick Kelsey 			 * a subset of the original data), a valid ACK, a
1948281a0fd4SPatrick Kelsey 			 * FIN, or a RST.
1949281a0fd4SPatrick Kelsey 			 */
1950281a0fd4SPatrick Kelsey 			if ((thflags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK)) {
1951281a0fd4SPatrick Kelsey 				rstreason = BANDLIM_RST_OPENPORT;
1952281a0fd4SPatrick Kelsey 				goto dropwithreset;
1953281a0fd4SPatrick Kelsey 			} else if (thflags & TH_SYN) {
1954281a0fd4SPatrick Kelsey 				/* non-initial SYN is ignored */
1955281a0fd4SPatrick Kelsey 				if ((tcp_timer_active(tp, TT_DELACK) ||
1956281a0fd4SPatrick Kelsey 				     tcp_timer_active(tp, TT_REXMT)))
1957281a0fd4SPatrick Kelsey 					goto drop;
1958281a0fd4SPatrick Kelsey 			} else if (!(thflags & (TH_ACK|TH_FIN|TH_RST))) {
1959281a0fd4SPatrick Kelsey 				goto drop;
1960281a0fd4SPatrick Kelsey 			}
1961281a0fd4SPatrick Kelsey 		}
1962281a0fd4SPatrick Kelsey #endif
1963764d8cefSBill Fenner 		break;
1964764d8cefSBill Fenner 
1965764d8cefSBill Fenner 	/*
1966df8bae1dSRodney W. Grimes 	 * If the state is SYN_SENT:
1967df8bae1dSRodney W. Grimes 	 *	if seg contains an ACK, but not for our SYN, drop the input.
1968df8bae1dSRodney W. Grimes 	 *	if seg contains a RST, then drop the connection.
1969df8bae1dSRodney W. Grimes 	 *	if seg does not contain SYN, then drop it.
1970df8bae1dSRodney W. Grimes 	 * Otherwise this is an acceptable SYN segment
1971df8bae1dSRodney W. Grimes 	 *	initialize tp->rcv_nxt and tp->irs
1972df8bae1dSRodney W. Grimes 	 *	if seg contains ack then advance tp->snd_una
1973f2512ba1SRui Paulo 	 *	if seg contains an ECE and ECN support is enabled, the stream
1974f2512ba1SRui Paulo 	 *	    is ECN capable.
1975df8bae1dSRodney W. Grimes 	 *	if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1976df8bae1dSRodney W. Grimes 	 *	arrange for segment to be acked (eventually)
1977df8bae1dSRodney W. Grimes 	 *	continue processing rest of data/controls, beginning with URG
1978df8bae1dSRodney W. Grimes 	 */
1979df8bae1dSRodney W. Grimes 	case TCPS_SYN_SENT:
1980fb59c426SYoshinobu Inoue 		if ((thflags & TH_ACK) &&
1981fb59c426SYoshinobu Inoue 		    (SEQ_LEQ(th->th_ack, tp->iss) ||
1982fb59c426SYoshinobu Inoue 		     SEQ_GT(th->th_ack, tp->snd_max))) {
1983a57815efSBosko Milekic 			rstreason = BANDLIM_UNLIMITED;
1984a0292f23SGarrett Wollman 			goto dropwithreset;
1985a0292f23SGarrett Wollman 		}
198657f60867SMark Johnston 		if ((thflags & (TH_ACK|TH_RST)) == (TH_ACK|TH_RST)) {
1987d9fae5abSAndriy Gapon 			TCP_PROBE5(connect__refused, NULL, tp,
1988fa22ce15SAdrian Chadd 			    mtod(m, const char *), tp, th);
1989df8bae1dSRodney W. Grimes 			tp = tcp_drop(tp, ECONNREFUSED);
199057f60867SMark Johnston 		}
19910ca3f933SAndre Oppermann 		if (thflags & TH_RST)
1992df8bae1dSRodney W. Grimes 			goto drop;
19930ca3f933SAndre Oppermann 		if (!(thflags & TH_SYN))
1994df8bae1dSRodney W. Grimes 			goto drop;
1995464fcfbcSAndre Oppermann 
1996fb59c426SYoshinobu Inoue 		tp->irs = th->th_seq;
1997df8bae1dSRodney W. Grimes 		tcp_rcvseqinit(tp);
1998fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK) {
199978b50714SRobert Watson 			TCPSTAT_INC(tcps_connects);
2000845799c1SAndras Olah 			soisconnected(so);
2001c488362eSRobert Watson #ifdef MAC
200230d239bcSRobert Watson 			mac_socketpeer_set_from_mbuf(m, so);
2003c488362eSRobert Watson #endif
2004845799c1SAndras Olah 			/* Do window scaling on this connection? */
2005845799c1SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2006845799c1SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
2007845799c1SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2008845799c1SAndras Olah 			}
2009766282cbSJohn Baldwin 			tp->rcv_adv += imin(tp->rcv_wnd,
2010766282cbSJohn Baldwin 			    TCP_MAXWIN << tp->rcv_scale);
2011a0292f23SGarrett Wollman 			tp->snd_una++;		/* SYN is acked */
2012a0292f23SGarrett Wollman 			/*
2013a0292f23SGarrett Wollman 			 * If there's data, delay ACK; if there's also a FIN
2014a0292f23SGarrett Wollman 			 * ACKNOW will be turned on later.
2015a0292f23SGarrett Wollman 			 */
2016c1e5a6e5SAndre Oppermann 			if (DELAY_ACK(tp, tlen) && tlen != 0)
2017b8152ba7SAndre Oppermann 				tcp_timer_activate(tp, TT_DELACK,
2018b8152ba7SAndre Oppermann 				    tcp_delacktime);
2019a0292f23SGarrett Wollman 			else
2020a0292f23SGarrett Wollman 				tp->t_flags |= TF_ACKNOW;
2021f2512ba1SRui Paulo 
2022603724d3SBjoern A. Zeeb 			if ((thflags & TH_ECE) && V_tcp_do_ecn) {
2023f2512ba1SRui Paulo 				tp->t_flags |= TF_ECN_PERMIT;
202478b50714SRobert Watson 				TCPSTAT_INC(tcps_ecn_shs);
2025f2512ba1SRui Paulo 			}
2026f2512ba1SRui Paulo 
2027a0292f23SGarrett Wollman 			/*
2028a0292f23SGarrett Wollman 			 * Received <SYN,ACK> in SYN_SENT[*] state.
2029a0292f23SGarrett Wollman 			 * Transitions:
2030a0292f23SGarrett Wollman 			 *	SYN_SENT  --> ESTABLISHED
2031a0292f23SGarrett Wollman 			 *	SYN_SENT* --> FIN_WAIT_1
2032a0292f23SGarrett Wollman 			 */
20339b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
2034a0292f23SGarrett Wollman 			if (tp->t_flags & TF_NEEDFIN) {
203557f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_1);
2036a0292f23SGarrett Wollman 				tp->t_flags &= ~TF_NEEDFIN;
2037fb59c426SYoshinobu Inoue 				thflags &= ~TH_SYN;
20387ff19458SPaul Traina 			} else {
203957f60867SMark Johnston 				tcp_state_change(tp, TCPS_ESTABLISHED);
2040d9fae5abSAndriy Gapon 				TCP_PROBE5(connect__established, NULL, tp,
2041fa22ce15SAdrian Chadd 				    mtod(m, const char *), tp, th);
2042dbc42409SLawrence Stewart 				cc_conn_init(tp);
20439077f387SGleb Smirnoff 				tcp_timer_activate(tp, TT_KEEP,
20449077f387SGleb Smirnoff 				    TP_KEEPIDLE(tp));
20457ff19458SPaul Traina 			}
2046a0292f23SGarrett Wollman 		} else {
2047a0292f23SGarrett Wollman 			/*
2048c068736aSJeffrey Hsu 			 * Received initial SYN in SYN-SENT[*] state =>
2049153edc50SHiren Panchasara 			 * simultaneous open.
2050c068736aSJeffrey Hsu 			 * If it succeeds, connection is * half-synchronized.
2051c068736aSJeffrey Hsu 			 * Otherwise, do 3-way handshake:
2052a0292f23SGarrett Wollman 			 *        SYN-SENT -> SYN-RECEIVED
2053a0292f23SGarrett Wollman 			 *        SYN-SENT* -> SYN-RECEIVED*
2054a0292f23SGarrett Wollman 			 */
20554b8e98d6SQing Li 			tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
2056b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
205757f60867SMark Johnston 			tcp_state_change(tp, TCPS_SYN_RECEIVED);
2058a0292f23SGarrett Wollman 		}
2059df8bae1dSRodney W. Grimes 
2060ff9b006dSJulien Charbon 		KASSERT(ti_locked == TI_RLOCKED, ("%s: trimthenstep6: "
2061252ca428SRobert Watson 		    "ti_locked %d", __func__, ti_locked));
2062ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
20638501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
20647cfc6904SRobert Watson 
2065df8bae1dSRodney W. Grimes 		/*
2066fb59c426SYoshinobu Inoue 		 * Advance th->th_seq to correspond to first data byte.
2067df8bae1dSRodney W. Grimes 		 * If data, trim to stay within window,
2068df8bae1dSRodney W. Grimes 		 * dropping FIN if necessary.
2069df8bae1dSRodney W. Grimes 		 */
2070fb59c426SYoshinobu Inoue 		th->th_seq++;
2071fb59c426SYoshinobu Inoue 		if (tlen > tp->rcv_wnd) {
2072fb59c426SYoshinobu Inoue 			todrop = tlen - tp->rcv_wnd;
2073df8bae1dSRodney W. Grimes 			m_adj(m, -todrop);
2074fb59c426SYoshinobu Inoue 			tlen = tp->rcv_wnd;
2075fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
207678b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpackafterwin);
207778b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2078df8bae1dSRodney W. Grimes 		}
2079fb59c426SYoshinobu Inoue 		tp->snd_wl1 = th->th_seq - 1;
2080fb59c426SYoshinobu Inoue 		tp->rcv_up = th->th_seq;
2081a0292f23SGarrett Wollman 		/*
2082a0292f23SGarrett Wollman 		 * Client side of transaction: already sent SYN and data.
2083a0292f23SGarrett Wollman 		 * If the remote host used T/TCP to validate the SYN,
2084a0292f23SGarrett Wollman 		 * our data will be ACK'd; if so, enter normal data segment
2085a0292f23SGarrett Wollman 		 * processing in the middle of step 5, ack processing.
2086a0292f23SGarrett Wollman 		 * Otherwise, goto step 6.
2087a0292f23SGarrett Wollman 		 */
2088fb59c426SYoshinobu Inoue 		if (thflags & TH_ACK)
2089a0292f23SGarrett Wollman 			goto process_ACK;
2090c068736aSJeffrey Hsu 
2091df8bae1dSRodney W. Grimes 		goto step6;
2092c068736aSJeffrey Hsu 
2093a0292f23SGarrett Wollman 	/*
2094a0292f23SGarrett Wollman 	 * If the state is LAST_ACK or CLOSING or TIME_WAIT:
2095c94c54e4SAndre Oppermann 	 *      do normal processing.
2096a0292f23SGarrett Wollman 	 *
2097c94c54e4SAndre Oppermann 	 * NB: Leftover from RFC1644 T/TCP.  Cases to be reused later.
2098a0292f23SGarrett Wollman 	 */
2099a0292f23SGarrett Wollman 	case TCPS_LAST_ACK:
2100a0292f23SGarrett Wollman 	case TCPS_CLOSING:
2101a0292f23SGarrett Wollman 		break;  /* continue normal processing */
2102df8bae1dSRodney W. Grimes 	}
2103df8bae1dSRodney W. Grimes 
2104df8bae1dSRodney W. Grimes 	/*
2105df8bae1dSRodney W. Grimes 	 * States other than LISTEN or SYN_SENT.
210680ab7c0eSGarrett Wollman 	 * First check the RST flag and sequence number since reset segments
210780ab7c0eSGarrett Wollman 	 * are exempt from the timestamp and connection count tests.  This
210880ab7c0eSGarrett Wollman 	 * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
210980ab7c0eSGarrett Wollman 	 * below which allowed reset segments in half the sequence space
211080ab7c0eSGarrett Wollman 	 * to fall though and be processed (which gives forged reset
211180ab7c0eSGarrett Wollman 	 * segments with a random sequence number a 50 percent chance of
211280ab7c0eSGarrett Wollman 	 * killing a connection).
211380ab7c0eSGarrett Wollman 	 * Then check timestamp, if present.
2114a0292f23SGarrett Wollman 	 * Then check the connection count, if present.
2115df8bae1dSRodney W. Grimes 	 * Then check that at least some bytes of segment are within
2116df8bae1dSRodney W. Grimes 	 * receive window.  If segment begins before rcv_nxt,
2117df8bae1dSRodney W. Grimes 	 * drop leading data (and SYN); if nothing left, just ack.
211880ab7c0eSGarrett Wollman 	 */
2119fb59c426SYoshinobu Inoue 	if (thflags & TH_RST) {
21203220a212SGleb Smirnoff 		/*
21213220a212SGleb Smirnoff 		 * RFC5961 Section 3.2
21223220a212SGleb Smirnoff 		 *
21233220a212SGleb Smirnoff 		 * - RST drops connection only if SEG.SEQ == RCV.NXT.
21243220a212SGleb Smirnoff 		 * - If RST is in window, we send challenge ACK.
21253220a212SGleb Smirnoff 		 *
21263220a212SGleb Smirnoff 		 * Note: to take into account delayed ACKs, we should
21273220a212SGleb Smirnoff 		 *   test against last_ack_sent instead of rcv_nxt.
21283220a212SGleb Smirnoff 		 * Note 2: we handle special case of closed window, not
21293220a212SGleb Smirnoff 		 *   covered by the RFC.
21303220a212SGleb Smirnoff 		 */
21313220a212SGleb Smirnoff 		if ((SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
21323220a212SGleb Smirnoff 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) ||
21333220a212SGleb Smirnoff 		    (tp->rcv_wnd == 0 && tp->last_ack_sent == th->th_seq)) {
213480ab7c0eSGarrett Wollman 
2135ff9b006dSJulien Charbon 			INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2136ff9b006dSJulien Charbon 			KASSERT(ti_locked == TI_RLOCKED,
21373220a212SGleb Smirnoff 			    ("%s: TH_RST ti_locked %d, th %p tp %p",
21383220a212SGleb Smirnoff 			    __func__, ti_locked, th, tp));
21393220a212SGleb Smirnoff 			KASSERT(tp->t_state != TCPS_SYN_SENT,
21403220a212SGleb Smirnoff 			    ("%s: TH_RST for TCPS_SYN_SENT th %p tp %p",
21413220a212SGleb Smirnoff 			    __func__, th, tp));
21423220a212SGleb Smirnoff 
21433220a212SGleb Smirnoff 			if (V_tcp_insecure_rst ||
21443220a212SGleb Smirnoff 			    tp->last_ack_sent == th->th_seq) {
21453220a212SGleb Smirnoff 				TCPSTAT_INC(tcps_drops);
21463220a212SGleb Smirnoff 				/* Drop the connection. */
21473220a212SGleb Smirnoff 				switch (tp->t_state) {
214880ab7c0eSGarrett Wollman 				case TCPS_SYN_RECEIVED:
214980ab7c0eSGarrett Wollman 					so->so_error = ECONNREFUSED;
215080ab7c0eSGarrett Wollman 					goto close;
215180ab7c0eSGarrett Wollman 				case TCPS_ESTABLISHED:
215280ab7c0eSGarrett Wollman 				case TCPS_FIN_WAIT_1:
215380ab7c0eSGarrett Wollman 				case TCPS_FIN_WAIT_2:
215480ab7c0eSGarrett Wollman 				case TCPS_CLOSE_WAIT:
215580ab7c0eSGarrett Wollman 					so->so_error = ECONNRESET;
215680ab7c0eSGarrett Wollman 				close:
215757f60867SMark Johnston 					tcp_state_change(tp, TCPS_CLOSED);
21583220a212SGleb Smirnoff 					/* FALLTHROUGH */
21593220a212SGleb Smirnoff 				default:
216080ab7c0eSGarrett Wollman 					tp = tcp_close(tp);
21613220a212SGleb Smirnoff 				}
21623220a212SGleb Smirnoff 			} else {
21633220a212SGleb Smirnoff 				TCPSTAT_INC(tcps_badrst);
21643220a212SGleb Smirnoff 				/* Send challenge ACK. */
21653220a212SGleb Smirnoff 				tcp_respond(tp, mtod(m, void *), th, m,
21663220a212SGleb Smirnoff 				    tp->rcv_nxt, tp->snd_nxt, TH_ACK);
21673220a212SGleb Smirnoff 				tp->last_ack_sent = tp->rcv_nxt;
21683220a212SGleb Smirnoff 				m = NULL;
21693220a212SGleb Smirnoff 			}
21703220a212SGleb Smirnoff 		}
21713220a212SGleb Smirnoff 		goto drop;
21723220a212SGleb Smirnoff 	}
217380ab7c0eSGarrett Wollman 
21743220a212SGleb Smirnoff 	/*
21753220a212SGleb Smirnoff 	 * RFC5961 Section 4.2
21763220a212SGleb Smirnoff 	 * Send challenge ACK for any SYN in synchronized state.
21773220a212SGleb Smirnoff 	 */
2178281a0fd4SPatrick Kelsey 	if ((thflags & TH_SYN) && tp->t_state != TCPS_SYN_SENT &&
2179281a0fd4SPatrick Kelsey 	    tp->t_state != TCPS_SYN_RECEIVED) {
2180ff9b006dSJulien Charbon 		KASSERT(ti_locked == TI_RLOCKED,
21813220a212SGleb Smirnoff 		    ("tcp_do_segment: TH_SYN ti_locked %d", ti_locked));
2182ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2183252ca428SRobert Watson 
21843220a212SGleb Smirnoff 		TCPSTAT_INC(tcps_badsyn);
21853220a212SGleb Smirnoff 		if (V_tcp_insecure_syn &&
21863220a212SGleb Smirnoff 		    SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
21873220a212SGleb Smirnoff 		    SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
21883220a212SGleb Smirnoff 			tp = tcp_drop(tp, ECONNRESET);
21893220a212SGleb Smirnoff 			rstreason = BANDLIM_UNLIMITED;
21903220a212SGleb Smirnoff 		} else {
21913220a212SGleb Smirnoff 			/* Send challenge ACK. */
21923220a212SGleb Smirnoff 			tcp_respond(tp, mtod(m, void *), th, m, tp->rcv_nxt,
21933220a212SGleb Smirnoff 			    tp->snd_nxt, TH_ACK);
21943220a212SGleb Smirnoff 			tp->last_ack_sent = tp->rcv_nxt;
21953220a212SGleb Smirnoff 			m = NULL;
219680ab7c0eSGarrett Wollman 		}
219780ab7c0eSGarrett Wollman 		goto drop;
219880ab7c0eSGarrett Wollman 	}
219980ab7c0eSGarrett Wollman 
220080ab7c0eSGarrett Wollman 	/*
2201df8bae1dSRodney W. Grimes 	 * RFC 1323 PAWS: If we have a timestamp reply on this segment
2202df8bae1dSRodney W. Grimes 	 * and it's less than ts_recent, drop it.
2203df8bae1dSRodney W. Grimes 	 */
2204be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
220580ab7c0eSGarrett Wollman 	    TSTMP_LT(to.to_tsval, tp->ts_recent)) {
2206df8bae1dSRodney W. Grimes 
2207df8bae1dSRodney W. Grimes 		/* Check to see if ts_recent is over 24 days old.  */
2208d8951c8aSBjoern A. Zeeb 		if (tcp_ts_getticks() - tp->ts_recent_age > TCP_PAWS_IDLE) {
2209df8bae1dSRodney W. Grimes 			/*
2210df8bae1dSRodney W. Grimes 			 * Invalidate ts_recent.  If this segment updates
2211df8bae1dSRodney W. Grimes 			 * ts_recent, the age will be reset later and ts_recent
2212df8bae1dSRodney W. Grimes 			 * will get a valid value.  If it does not, setting
2213df8bae1dSRodney W. Grimes 			 * ts_recent to zero will at least satisfy the
2214df8bae1dSRodney W. Grimes 			 * requirement that zero be placed in the timestamp
2215df8bae1dSRodney W. Grimes 			 * echo reply when ts_recent isn't valid.  The
2216df8bae1dSRodney W. Grimes 			 * age isn't reset until we get a valid ts_recent
2217df8bae1dSRodney W. Grimes 			 * because we don't want out-of-order segments to be
2218df8bae1dSRodney W. Grimes 			 * dropped when ts_recent is old.
2219df8bae1dSRodney W. Grimes 			 */
2220df8bae1dSRodney W. Grimes 			tp->ts_recent = 0;
2221df8bae1dSRodney W. Grimes 		} else {
222278b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
222378b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, tlen);
222478b50714SRobert Watson 			TCPSTAT_INC(tcps_pawsdrop);
222507fd333dSMatthew Dillon 			if (tlen)
2226df8bae1dSRodney W. Grimes 				goto dropafterack;
22271ab4789dSMatthew Dillon 			goto drop;
22281ab4789dSMatthew Dillon 		}
2229df8bae1dSRodney W. Grimes 	}
2230df8bae1dSRodney W. Grimes 
2231a0292f23SGarrett Wollman 	/*
223280ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, validate that the packet belongs to
223380ab7c0eSGarrett Wollman 	 * this connection before trimming the data to fit the receive
223480ab7c0eSGarrett Wollman 	 * window.  Check the sequence number versus IRS since we know
223580ab7c0eSGarrett Wollman 	 * the sequence numbers haven't wrapped.  This is a partial fix
223680ab7c0eSGarrett Wollman 	 * for the "LAND" DoS attack.
223780ab7c0eSGarrett Wollman 	 */
2238a57815efSBosko Milekic 	if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
2239a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
2240a57815efSBosko Milekic 		goto dropwithreset;
2241a57815efSBosko Milekic 	}
224280ab7c0eSGarrett Wollman 
2243fb59c426SYoshinobu Inoue 	todrop = tp->rcv_nxt - th->th_seq;
2244df8bae1dSRodney W. Grimes 	if (todrop > 0) {
2245831ad37eSXin LI 		if (thflags & TH_SYN) {
2246fb59c426SYoshinobu Inoue 			thflags &= ~TH_SYN;
2247fb59c426SYoshinobu Inoue 			th->th_seq++;
2248fb59c426SYoshinobu Inoue 			if (th->th_urp > 1)
2249fb59c426SYoshinobu Inoue 				th->th_urp--;
2250df8bae1dSRodney W. Grimes 			else
2251fb59c426SYoshinobu Inoue 				thflags &= ~TH_URG;
2252df8bae1dSRodney W. Grimes 			todrop--;
2253df8bae1dSRodney W. Grimes 		}
2254df8bae1dSRodney W. Grimes 		/*
2255dac20301SGarrett Wollman 		 * Following if statement from Stevens, vol. 2, p. 960.
2256df8bae1dSRodney W. Grimes 		 */
2257fb59c426SYoshinobu Inoue 		if (todrop > tlen
2258fb59c426SYoshinobu Inoue 		    || (todrop == tlen && (thflags & TH_FIN) == 0)) {
2259dac20301SGarrett Wollman 			/*
2260dac20301SGarrett Wollman 			 * Any valid FIN must be to the left of the window.
2261dac20301SGarrett Wollman 			 * At this point the FIN must be a duplicate or out
2262dac20301SGarrett Wollman 			 * of sequence; drop it.
2263dac20301SGarrett Wollman 			 */
2264fb59c426SYoshinobu Inoue 			thflags &= ~TH_FIN;
2265dac20301SGarrett Wollman 
2266df8bae1dSRodney W. Grimes 			/*
2267dac20301SGarrett Wollman 			 * Send an ACK to resynchronize and drop any data.
2268dac20301SGarrett Wollman 			 * But keep on processing for RST or ACK.
2269df8bae1dSRodney W. Grimes 			 */
2270dac20301SGarrett Wollman 			tp->t_flags |= TF_ACKNOW;
2271fb59c426SYoshinobu Inoue 			todrop = tlen;
227278b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvduppack);
227378b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvdupbyte, todrop);
2274df8bae1dSRodney W. Grimes 		} else {
227578b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpartduppack);
227678b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvpartdupbyte, todrop);
2277df8bae1dSRodney W. Grimes 		}
2278fb59c426SYoshinobu Inoue 		drop_hdrlen += todrop;	/* drop from the top afterwards */
2279fb59c426SYoshinobu Inoue 		th->th_seq += todrop;
2280fb59c426SYoshinobu Inoue 		tlen -= todrop;
2281fb59c426SYoshinobu Inoue 		if (th->th_urp > todrop)
2282fb59c426SYoshinobu Inoue 			th->th_urp -= todrop;
2283df8bae1dSRodney W. Grimes 		else {
2284fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;
2285fb59c426SYoshinobu Inoue 			th->th_urp = 0;
2286df8bae1dSRodney W. Grimes 		}
2287df8bae1dSRodney W. Grimes 	}
2288df8bae1dSRodney W. Grimes 
2289df8bae1dSRodney W. Grimes 	/*
2290df8bae1dSRodney W. Grimes 	 * If new data are received on a connection after the
2291df8bae1dSRodney W. Grimes 	 * user processes are gone, then RST the other end.
2292df8bae1dSRodney W. Grimes 	 */
2293df8bae1dSRodney W. Grimes 	if ((so->so_state & SS_NOFDREF) &&
2294fb59c426SYoshinobu Inoue 	    tp->t_state > TCPS_CLOSE_WAIT && tlen) {
2295ff9b006dSJulien Charbon 		KASSERT(ti_locked == TI_RLOCKED, ("%s: SS_NOFDEREF && "
2296252ca428SRobert Watson 		    "CLOSE_WAIT && tlen ti_locked %d", __func__, ti_locked));
2297ff9b006dSJulien Charbon 		INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2298252ca428SRobert Watson 
229907dacf03SAndre Oppermann 		if ((s = tcp_log_addrs(inc, th, NULL, NULL))) {
230007dacf03SAndre Oppermann 			log(LOG_DEBUG, "%s; %s: %s: Received %d bytes of data "
230107dacf03SAndre Oppermann 			    "after socket was closed, "
230207dacf03SAndre Oppermann 			    "sending RST and removing tcpcb\n",
2303e31d8aa3SMike Silbersack 			    s, __func__, tcpstates[tp->t_state], tlen);
2304773673c1SAndre Oppermann 			free(s, M_TCPLOG);
2305773673c1SAndre Oppermann 		}
2306df8bae1dSRodney W. Grimes 		tp = tcp_close(tp);
230778b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvafterclose);
2308a57815efSBosko Milekic 		rstreason = BANDLIM_UNLIMITED;
2309df8bae1dSRodney W. Grimes 		goto dropwithreset;
2310df8bae1dSRodney W. Grimes 	}
2311df8bae1dSRodney W. Grimes 
2312df8bae1dSRodney W. Grimes 	/*
2313df8bae1dSRodney W. Grimes 	 * If segment ends after window, drop trailing data
2314df8bae1dSRodney W. Grimes 	 * (and PUSH and FIN); if nothing left, just ACK.
2315df8bae1dSRodney W. Grimes 	 */
2316fb59c426SYoshinobu Inoue 	todrop = (th->th_seq + tlen) - (tp->rcv_nxt + tp->rcv_wnd);
2317df8bae1dSRodney W. Grimes 	if (todrop > 0) {
231878b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvpackafterwin);
2319fb59c426SYoshinobu Inoue 		if (todrop >= tlen) {
232078b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, tlen);
2321df8bae1dSRodney W. Grimes 			/*
2322df8bae1dSRodney W. Grimes 			 * If window is closed can only take segments at
2323df8bae1dSRodney W. Grimes 			 * window edge, and have to drop data and PUSH from
2324df8bae1dSRodney W. Grimes 			 * incoming segments.  Continue processing, but
2325df8bae1dSRodney W. Grimes 			 * remember to ack.  Otherwise, drop segment
2326df8bae1dSRodney W. Grimes 			 * and ack.
2327df8bae1dSRodney W. Grimes 			 */
2328fb59c426SYoshinobu Inoue 			if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
2329df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
233078b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvwinprobe);
2331df8bae1dSRodney W. Grimes 			} else
2332df8bae1dSRodney W. Grimes 				goto dropafterack;
2333df8bae1dSRodney W. Grimes 		} else
233478b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyteafterwin, todrop);
2335df8bae1dSRodney W. Grimes 		m_adj(m, -todrop);
2336fb59c426SYoshinobu Inoue 		tlen -= todrop;
2337fb59c426SYoshinobu Inoue 		thflags &= ~(TH_PUSH|TH_FIN);
2338df8bae1dSRodney W. Grimes 	}
2339df8bae1dSRodney W. Grimes 
2340df8bae1dSRodney W. Grimes 	/*
2341df8bae1dSRodney W. Grimes 	 * If last ACK falls within this segment's sequence numbers,
2342df8bae1dSRodney W. Grimes 	 * record its timestamp.
2343cf09195bSPaul Saab 	 * NOTE:
2344cf09195bSPaul Saab 	 * 1) That the test incorporates suggestions from the latest
2345a0292f23SGarrett Wollman 	 *    proposal of the tcplw@cray.com list (Braden 1993/04/26).
2346cf09195bSPaul Saab 	 * 2) That updating only on newer timestamps interferes with
2347cf09195bSPaul Saab 	 *    our earlier PAWS tests, so this check should be solely
2348cf09195bSPaul Saab 	 *    predicated on the sequence space of this segment.
2349cf09195bSPaul Saab 	 * 3) That we modify the segment boundary check to be
2350cf09195bSPaul Saab 	 *        Last.ACK.Sent <= SEG.SEQ + SEG.Len
2351cf09195bSPaul Saab 	 *    instead of RFC1323's
2352cf09195bSPaul Saab 	 *        Last.ACK.Sent < SEG.SEQ + SEG.Len,
2353cf09195bSPaul Saab 	 *    This modified check allows us to overcome RFC1323's
2354cf09195bSPaul Saab 	 *    limitations as described in Stevens TCP/IP Illustrated
2355cf09195bSPaul Saab 	 *    Vol. 2 p.869. In such cases, we can still calculate the
2356cf09195bSPaul Saab 	 *    RTT correctly when RCV.NXT == Last.ACK.Sent.
2357df8bae1dSRodney W. Grimes 	 */
2358be2ac88cSJonathan Lemon 	if ((to.to_flags & TOF_TS) != 0 &&
2359cf09195bSPaul Saab 	    SEQ_LEQ(th->th_seq, tp->last_ack_sent) &&
2360cf09195bSPaul Saab 	    SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
2361cf09195bSPaul Saab 		((thflags & (TH_SYN|TH_FIN)) != 0))) {
2362d8951c8aSBjoern A. Zeeb 		tp->ts_recent_age = tcp_ts_getticks();
2363a0292f23SGarrett Wollman 		tp->ts_recent = to.to_tsval;
2364df8bae1dSRodney W. Grimes 	}
2365df8bae1dSRodney W. Grimes 
2366df8bae1dSRodney W. Grimes 	/*
2367a0292f23SGarrett Wollman 	 * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
2368a0292f23SGarrett Wollman 	 * flag is on (half-synchronized state), then queue data for
2369a0292f23SGarrett Wollman 	 * later processing; else drop segment and return.
2370a0292f23SGarrett Wollman 	 */
2371fb59c426SYoshinobu Inoue 	if ((thflags & TH_ACK) == 0) {
2372a0292f23SGarrett Wollman 		if (tp->t_state == TCPS_SYN_RECEIVED ||
2373281a0fd4SPatrick Kelsey 		    (tp->t_flags & TF_NEEDSYN)) {
2374281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
2375281a0fd4SPatrick Kelsey 			if (tp->t_state == TCPS_SYN_RECEIVED &&
2376281a0fd4SPatrick Kelsey 			    tp->t_flags & TF_FASTOPEN) {
2377281a0fd4SPatrick Kelsey 				tp->snd_wnd = tiwin;
2378281a0fd4SPatrick Kelsey 				cc_conn_init(tp);
2379281a0fd4SPatrick Kelsey 			}
2380281a0fd4SPatrick Kelsey #endif
2381a0292f23SGarrett Wollman 			goto step6;
2382281a0fd4SPatrick Kelsey 		} else if (tp->t_flags & TF_ACKNOW)
23835e1aa279SDavid Malone 			goto dropafterack;
2384a0292f23SGarrett Wollman 		else
2385a0292f23SGarrett Wollman 			goto drop;
2386a0292f23SGarrett Wollman 	}
2387df8bae1dSRodney W. Grimes 
2388df8bae1dSRodney W. Grimes 	/*
2389df8bae1dSRodney W. Grimes 	 * Ack processing.
2390df8bae1dSRodney W. Grimes 	 */
2391df8bae1dSRodney W. Grimes 	switch (tp->t_state) {
2392df8bae1dSRodney W. Grimes 
2393df8bae1dSRodney W. Grimes 	/*
2394764d8cefSBill Fenner 	 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
2395764d8cefSBill Fenner 	 * ESTABLISHED state and continue processing.
2396764d8cefSBill Fenner 	 * The ACK was checked above.
2397df8bae1dSRodney W. Grimes 	 */
2398df8bae1dSRodney W. Grimes 	case TCPS_SYN_RECEIVED:
2399a0292f23SGarrett Wollman 
240078b50714SRobert Watson 		TCPSTAT_INC(tcps_connects);
2401df8bae1dSRodney W. Grimes 		soisconnected(so);
2402df8bae1dSRodney W. Grimes 		/* Do window scaling? */
2403df8bae1dSRodney W. Grimes 		if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
2404df8bae1dSRodney W. Grimes 			(TF_RCVD_SCALE|TF_REQ_SCALE)) {
2405df8bae1dSRodney W. Grimes 			tp->rcv_scale = tp->request_r_scale;
2406464fcfbcSAndre Oppermann 			tp->snd_wnd = tiwin;
2407df8bae1dSRodney W. Grimes 		}
2408a0292f23SGarrett Wollman 		/*
2409a0292f23SGarrett Wollman 		 * Make transitions:
2410a0292f23SGarrett Wollman 		 *      SYN-RECEIVED  -> ESTABLISHED
2411a0292f23SGarrett Wollman 		 *      SYN-RECEIVED* -> FIN-WAIT-1
2412a0292f23SGarrett Wollman 		 */
24139b8b58e0SJonathan Lemon 		tp->t_starttime = ticks;
2414a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDFIN) {
241557f60867SMark Johnston 			tcp_state_change(tp, TCPS_FIN_WAIT_1);
2416a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDFIN;
24177ff19458SPaul Traina 		} else {
241857f60867SMark Johnston 			tcp_state_change(tp, TCPS_ESTABLISHED);
2419d9fae5abSAndriy Gapon 			TCP_PROBE5(accept__established, NULL, tp,
2420fa22ce15SAdrian Chadd 			    mtod(m, const char *), tp, th);
2421281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
2422281a0fd4SPatrick Kelsey 			if (tp->t_tfo_pending) {
2423281a0fd4SPatrick Kelsey 				tcp_fastopen_decrement_counter(tp->t_tfo_pending);
2424281a0fd4SPatrick Kelsey 				tp->t_tfo_pending = NULL;
2425281a0fd4SPatrick Kelsey 
2426281a0fd4SPatrick Kelsey 				/*
2427281a0fd4SPatrick Kelsey 				 * Account for the ACK of our SYN prior to
2428281a0fd4SPatrick Kelsey 				 * regular ACK processing below.
2429281a0fd4SPatrick Kelsey 				 */
2430281a0fd4SPatrick Kelsey 				tp->snd_una++;
2431281a0fd4SPatrick Kelsey 			}
2432281a0fd4SPatrick Kelsey 			/*
2433281a0fd4SPatrick Kelsey 			 * TFO connections call cc_conn_init() during SYN
2434281a0fd4SPatrick Kelsey 			 * processing.  Calling it again here for such
2435281a0fd4SPatrick Kelsey 			 * connections is not harmless as it would undo the
2436281a0fd4SPatrick Kelsey 			 * snd_cwnd reduction that occurs when a TFO SYN|ACK
2437281a0fd4SPatrick Kelsey 			 * is retransmitted.
2438281a0fd4SPatrick Kelsey 			 */
2439281a0fd4SPatrick Kelsey 			if (!(tp->t_flags & TF_FASTOPEN))
2440281a0fd4SPatrick Kelsey #endif
2441dbc42409SLawrence Stewart 				cc_conn_init(tp);
24429077f387SGleb Smirnoff 			tcp_timer_activate(tp, TT_KEEP, TP_KEEPIDLE(tp));
24437ff19458SPaul Traina 		}
2444a0292f23SGarrett Wollman 		/*
2445a0292f23SGarrett Wollman 		 * If segment contains data or ACK, will call tcp_reass()
2446a0292f23SGarrett Wollman 		 * later; if not, do so now to pass queued data to user.
2447a0292f23SGarrett Wollman 		 */
2448fb59c426SYoshinobu Inoue 		if (tlen == 0 && (thflags & TH_FIN) == 0)
2449fb59c426SYoshinobu Inoue 			(void) tcp_reass(tp, (struct tcphdr *)0, 0,
2450a0292f23SGarrett Wollman 			    (struct mbuf *)0);
245160ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq - 1;
245293b0017fSPhilippe Charnier 		/* FALLTHROUGH */
2453df8bae1dSRodney W. Grimes 
2454df8bae1dSRodney W. Grimes 	/*
2455df8bae1dSRodney W. Grimes 	 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
2456df8bae1dSRodney W. Grimes 	 * ACKs.  If the ack is in the range
2457fb59c426SYoshinobu Inoue 	 *	tp->snd_una < th->th_ack <= tp->snd_max
2458fb59c426SYoshinobu Inoue 	 * then advance tp->snd_una to th->th_ack and drop
2459df8bae1dSRodney W. Grimes 	 * data from the retransmission queue.  If this ACK reflects
2460df8bae1dSRodney W. Grimes 	 * more up to date window information we update our window information.
2461df8bae1dSRodney W. Grimes 	 */
2462df8bae1dSRodney W. Grimes 	case TCPS_ESTABLISHED:
2463df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_1:
2464df8bae1dSRodney W. Grimes 	case TCPS_FIN_WAIT_2:
2465df8bae1dSRodney W. Grimes 	case TCPS_CLOSE_WAIT:
2466df8bae1dSRodney W. Grimes 	case TCPS_CLOSING:
2467df8bae1dSRodney W. Grimes 	case TCPS_LAST_ACK:
24685a53ca16SPaul Saab 		if (SEQ_GT(th->th_ack, tp->snd_max)) {
246978b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvacktoomuch);
24705a53ca16SPaul Saab 			goto dropafterack;
24715a53ca16SPaul Saab 		}
24723529149eSAndre Oppermann 		if ((tp->t_flags & TF_SACK_PERMIT) &&
2473fc30a251SAndre Oppermann 		    ((to.to_flags & TOF_SACK) ||
2474fc30a251SAndre Oppermann 		     !TAILQ_EMPTY(&tp->snd_holes)))
2475021eaf79SHiren Panchasara 			sack_changed = tcp_sack_doack(tp, &to, th->th_ack);
247612eeb81fSHiren Panchasara 		else
247712eeb81fSHiren Panchasara 			/*
247812eeb81fSHiren Panchasara 			 * Reset the value so that previous (valid) value
247912eeb81fSHiren Panchasara 			 * from the last ack with SACK doesn't get used.
248012eeb81fSHiren Panchasara 			 */
248112eeb81fSHiren Panchasara 			tp->sackhint.sacked_bytes = 0;
248239bc9de5SLawrence Stewart 
248339bc9de5SLawrence Stewart 		/* Run HHOOK_TCP_ESTABLISHED_IN helper hooks. */
248439bc9de5SLawrence Stewart 		hhook_run_tcp_est_in(tp, th, &to);
248539bc9de5SLawrence Stewart 
2486fb59c426SYoshinobu Inoue 		if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
24870c39d38dSGleb Smirnoff 			u_int maxseg;
24880c39d38dSGleb Smirnoff 
24890c39d38dSGleb Smirnoff 			maxseg = tcp_maxseg(tp);
2490021eaf79SHiren Panchasara 			if (tlen == 0 &&
2491021eaf79SHiren Panchasara 			    (tiwin == tp->snd_wnd ||
2492021eaf79SHiren Panchasara 			    (tp->t_flags & TF_SACK_PERMIT))) {
249362b90589SPeter Wemm 				/*
249462b90589SPeter Wemm 				 * If this is the first time we've seen a
249562b90589SPeter Wemm 				 * FIN from the remote, this is not a
249662b90589SPeter Wemm 				 * duplicate and it needs to be processed
249762b90589SPeter Wemm 				 * normally.  This happens during a
249862b90589SPeter Wemm 				 * simultaneous close.
249962b90589SPeter Wemm 				 */
250062b90589SPeter Wemm 				if ((thflags & TH_FIN) &&
250162b90589SPeter Wemm 				    (TCPS_HAVERCVDFIN(tp->t_state) == 0)) {
250262b90589SPeter Wemm 					tp->t_dupacks = 0;
250362b90589SPeter Wemm 					break;
250462b90589SPeter Wemm 				}
250578b50714SRobert Watson 				TCPSTAT_INC(tcps_rcvdupack);
2506df8bae1dSRodney W. Grimes 				/*
2507df8bae1dSRodney W. Grimes 				 * If we have outstanding data (other than
2508df8bae1dSRodney W. Grimes 				 * a window probe), this is a completely
2509df8bae1dSRodney W. Grimes 				 * duplicate ack (ie, window info didn't
25105f30ec9bSEitan Adler 				 * change and FIN isn't set),
25115f30ec9bSEitan Adler 				 * the ack is the biggest we've
2512df8bae1dSRodney W. Grimes 				 * seen and we've seen exactly our rexmt
2513df8bae1dSRodney W. Grimes 				 * threshhold of them, assume a packet
2514df8bae1dSRodney W. Grimes 				 * has been dropped and retransmit it.
2515df8bae1dSRodney W. Grimes 				 * Kludge snd_nxt & the congestion
2516df8bae1dSRodney W. Grimes 				 * window so we send only this one
2517df8bae1dSRodney W. Grimes 				 * packet.
2518df8bae1dSRodney W. Grimes 				 *
2519df8bae1dSRodney W. Grimes 				 * We know we're losing at the current
2520df8bae1dSRodney W. Grimes 				 * window size so do congestion avoidance
2521df8bae1dSRodney W. Grimes 				 * (set ssthresh to half the current window
2522df8bae1dSRodney W. Grimes 				 * and pull our congestion window back to
2523df8bae1dSRodney W. Grimes 				 * the new ssthresh).
2524df8bae1dSRodney W. Grimes 				 *
2525df8bae1dSRodney W. Grimes 				 * Dup acks mean that packets have left the
2526df8bae1dSRodney W. Grimes 				 * network (they're now cached at the receiver)
2527df8bae1dSRodney W. Grimes 				 * so bump cwnd by the amount in the receiver
2528df8bae1dSRodney W. Grimes 				 * to keep a constant cwnd packets in the
2529df8bae1dSRodney W. Grimes 				 * network.
2530f2512ba1SRui Paulo 				 *
2531f2512ba1SRui Paulo 				 * When using TCP ECN, notify the peer that
2532f2512ba1SRui Paulo 				 * we reduced the cwnd.
2533df8bae1dSRodney W. Grimes 				 */
2534021eaf79SHiren Panchasara 				/*
2535021eaf79SHiren Panchasara 				 * Following 2 kinds of acks should not affect
2536021eaf79SHiren Panchasara 				 * dupack counting:
2537021eaf79SHiren Panchasara 				 * 1) Old acks
2538021eaf79SHiren Panchasara 				 * 2) Acks with SACK but without any new SACK
2539021eaf79SHiren Panchasara 				 * information in them. These could result from
2540021eaf79SHiren Panchasara 				 * any anomaly in the network like a switch
2541021eaf79SHiren Panchasara 				 * duplicating packets or a possible DoS attack.
2542021eaf79SHiren Panchasara 				 */
2543021eaf79SHiren Panchasara 				if (th->th_ack != tp->snd_una ||
2544021eaf79SHiren Panchasara 				    ((tp->t_flags & TF_SACK_PERMIT) &&
2545021eaf79SHiren Panchasara 				    !sack_changed))
2546021eaf79SHiren Panchasara 					break;
2547021eaf79SHiren Panchasara 				else if (!tcp_timer_active(tp, TT_REXMT))
2548df8bae1dSRodney W. Grimes 					tp->t_dupacks = 0;
2549cb942153SJeffrey Hsu 				else if (++tp->t_dupacks > tcprexmtthresh ||
2550dbc42409SLawrence Stewart 				     IN_FASTRECOVERY(tp->t_flags)) {
2551dbc42409SLawrence Stewart 					cc_ack_received(tp, th, CC_DUPACK);
25523529149eSAndre Oppermann 					if ((tp->t_flags & TF_SACK_PERMIT) &&
2553dbc42409SLawrence Stewart 					    IN_FASTRECOVERY(tp->t_flags)) {
2554808f11b7SPaul Saab 						int awnd;
255525e6f9edSPaul Saab 
255625e6f9edSPaul Saab 						/*
255725e6f9edSPaul Saab 						 * Compute the amount of data in flight first.
25584c3972f0SHiren Panchasara 						 * We can inject new data into the pipe iff
255925e6f9edSPaul Saab 						 * we have less than 1/2 the original window's
256025e6f9edSPaul Saab 						 * worth of data in flight.
256125e6f9edSPaul Saab 						 */
256212eeb81fSHiren Panchasara 						if (V_tcp_do_rfc6675_pipe)
256312eeb81fSHiren Panchasara 							awnd = tcp_compute_pipe(tp);
256412eeb81fSHiren Panchasara 						else
2565808f11b7SPaul Saab 							awnd = (tp->snd_nxt - tp->snd_fack) +
25660077b016SPaul Saab 								tp->sackhint.sack_bytes_rexmit;
256712eeb81fSHiren Panchasara 
2568808f11b7SPaul Saab 						if (awnd < tp->snd_ssthresh) {
25690c39d38dSGleb Smirnoff 							tp->snd_cwnd += maxseg;
257025e6f9edSPaul Saab 							if (tp->snd_cwnd > tp->snd_ssthresh)
257125e6f9edSPaul Saab 								tp->snd_cwnd = tp->snd_ssthresh;
257225e6f9edSPaul Saab 						}
257325e6f9edSPaul Saab 					} else
25740c39d38dSGleb Smirnoff 						tp->snd_cwnd += maxseg;
257555bceb1eSRandall Stewart 					(void) tp->t_fb->tfb_tcp_output(tp);
257646f58482SJonathan Lemon 					goto drop;
2577cb942153SJeffrey Hsu 				} else if (tp->t_dupacks == tcprexmtthresh) {
2578cb942153SJeffrey Hsu 					tcp_seq onxt = tp->snd_nxt;
2579a0445c2eSJayanth Vijayaraghavan 
2580a0445c2eSJayanth Vijayaraghavan 					/*
2581a0445c2eSJayanth Vijayaraghavan 					 * If we're doing sack, check to
2582a0445c2eSJayanth Vijayaraghavan 					 * see if we're already in sack
2583a0445c2eSJayanth Vijayaraghavan 					 * recovery. If we're not doing sack,
2584a0445c2eSJayanth Vijayaraghavan 					 * check to see if we're in newreno
2585a0445c2eSJayanth Vijayaraghavan 					 * recovery.
2586a0445c2eSJayanth Vijayaraghavan 					 */
25873529149eSAndre Oppermann 					if (tp->t_flags & TF_SACK_PERMIT) {
2588dbc42409SLawrence Stewart 						if (IN_FASTRECOVERY(tp->t_flags)) {
2589a0445c2eSJayanth Vijayaraghavan 							tp->t_dupacks = 0;
2590a0445c2eSJayanth Vijayaraghavan 							break;
2591a0445c2eSJayanth Vijayaraghavan 						}
2592dbc42409SLawrence Stewart 					} else {
2593a0445c2eSJayanth Vijayaraghavan 						if (SEQ_LEQ(th->th_ack,
25949d11646dSJeffrey Hsu 						    tp->snd_recover)) {
2595cb942153SJeffrey Hsu 							tp->t_dupacks = 0;
2596cb942153SJeffrey Hsu 							break;
259746f58482SJonathan Lemon 						}
2598a0445c2eSJayanth Vijayaraghavan 					}
2599dbc42409SLawrence Stewart 					/* Congestion signal before ack. */
2600dbc42409SLawrence Stewart 					cc_cong_signal(tp, th, CC_NDUPACK);
2601dbc42409SLawrence Stewart 					cc_ack_received(tp, th, CC_DUPACK);
2602b8152ba7SAndre Oppermann 					tcp_timer_activate(tp, TT_REXMT, 0);
26039b8b58e0SJonathan Lemon 					tp->t_rtttime = 0;
26043529149eSAndre Oppermann 					if (tp->t_flags & TF_SACK_PERMIT) {
260578b50714SRobert Watson 						TCPSTAT_INC(
260678b50714SRobert Watson 						    tcps_sack_recovery_episode);
2607a55db2b6SPaul Saab 						tp->sack_newdata = tp->snd_nxt;
26080c39d38dSGleb Smirnoff 						tp->snd_cwnd = maxseg;
260955bceb1eSRandall Stewart 						(void) tp->t_fb->tfb_tcp_output(tp);
26106d90faf3SPaul Saab 						goto drop;
26116d90faf3SPaul Saab 					}
2612fb59c426SYoshinobu Inoue 					tp->snd_nxt = th->th_ack;
26130c39d38dSGleb Smirnoff 					tp->snd_cwnd = maxseg;
261455bceb1eSRandall Stewart 					(void) tp->t_fb->tfb_tcp_output(tp);
261548d2549cSJeffrey Hsu 					KASSERT(tp->snd_limited <= 2,
2616302ce8d6SAndre Oppermann 					    ("%s: tp->snd_limited too big",
2617302ce8d6SAndre Oppermann 					    __func__));
2618df8bae1dSRodney W. Grimes 					tp->snd_cwnd = tp->snd_ssthresh +
26190c39d38dSGleb Smirnoff 					     maxseg *
262048d2549cSJeffrey Hsu 					     (tp->t_dupacks - tp->snd_limited);
2621df8bae1dSRodney W. Grimes 					if (SEQ_GT(onxt, tp->snd_nxt))
2622df8bae1dSRodney W. Grimes 						tp->snd_nxt = onxt;
2623df8bae1dSRodney W. Grimes 					goto drop;
2624603724d3SBjoern A. Zeeb 				} else if (V_tcp_do_rfc3042) {
262562d4443fSHiren Panchasara 					/*
262662d4443fSHiren Panchasara 					 * Process first and second duplicate
262762d4443fSHiren Panchasara 					 * ACKs. Each indicates a segment
262862d4443fSHiren Panchasara 					 * leaving the network, creating room
262962d4443fSHiren Panchasara 					 * for more. Make sure we can send a
263062d4443fSHiren Panchasara 					 * packet on reception of each duplicate
263162d4443fSHiren Panchasara 					 * ACK by increasing snd_cwnd by one
263262d4443fSHiren Panchasara 					 * segment. Restore the original
263362d4443fSHiren Panchasara 					 * snd_cwnd after packet transmission.
263462d4443fSHiren Panchasara 					 */
2635dbc42409SLawrence Stewart 					cc_ack_received(tp, th, CC_DUPACK);
2636582a954bSJeffrey Hsu 					u_long oldcwnd = tp->snd_cwnd;
263748d2549cSJeffrey Hsu 					tcp_seq oldsndmax = tp->snd_max;
263848d2549cSJeffrey Hsu 					u_int sent;
26395628dd08SAndre Oppermann 					int avail;
264089c02376SJeffrey Hsu 
2641582a954bSJeffrey Hsu 					KASSERT(tp->t_dupacks == 1 ||
2642582a954bSJeffrey Hsu 					    tp->t_dupacks == 2,
2643302ce8d6SAndre Oppermann 					    ("%s: dupacks not 1 or 2",
2644302ce8d6SAndre Oppermann 					    __func__));
264561a36e3dSJeffrey Hsu 					if (tp->t_dupacks == 1)
264648d2549cSJeffrey Hsu 						tp->snd_limited = 0;
264761a36e3dSJeffrey Hsu 					tp->snd_cwnd =
264861a36e3dSJeffrey Hsu 					    (tp->snd_nxt - tp->snd_una) +
264961a36e3dSJeffrey Hsu 					    (tp->t_dupacks - tp->snd_limited) *
26500c39d38dSGleb Smirnoff 					    maxseg;
26515628dd08SAndre Oppermann 					/*
26525628dd08SAndre Oppermann 					 * Only call tcp_output when there
26535628dd08SAndre Oppermann 					 * is new data available to be sent.
26545628dd08SAndre Oppermann 					 * Otherwise we would send pure ACKs.
26555628dd08SAndre Oppermann 					 */
26565628dd08SAndre Oppermann 					SOCKBUF_LOCK(&so->so_snd);
2657cfa6009eSGleb Smirnoff 					avail = sbavail(&so->so_snd) -
26585628dd08SAndre Oppermann 					    (tp->snd_nxt - tp->snd_una);
26595628dd08SAndre Oppermann 					SOCKBUF_UNLOCK(&so->so_snd);
26605628dd08SAndre Oppermann 					if (avail > 0)
266155bceb1eSRandall Stewart 						(void) tp->t_fb->tfb_tcp_output(tp);
266248d2549cSJeffrey Hsu 					sent = tp->snd_max - oldsndmax;
26630c39d38dSGleb Smirnoff 					if (sent > maxseg) {
266489c02376SJeffrey Hsu 						KASSERT((tp->t_dupacks == 2 &&
266589c02376SJeffrey Hsu 						    tp->snd_limited == 0) ||
26660c39d38dSGleb Smirnoff 						   (sent == maxseg + 1 &&
266789c02376SJeffrey Hsu 						    tp->t_flags & TF_SENTFIN),
2668302ce8d6SAndre Oppermann 						    ("%s: sent too much",
2669302ce8d6SAndre Oppermann 						    __func__));
267048d2549cSJeffrey Hsu 						tp->snd_limited = 2;
267148d2549cSJeffrey Hsu 					} else if (sent > 0)
267248d2549cSJeffrey Hsu 						++tp->snd_limited;
2673582a954bSJeffrey Hsu 					tp->snd_cwnd = oldcwnd;
2674582a954bSJeffrey Hsu 					goto drop;
2675df8bae1dSRodney W. Grimes 				}
2676021eaf79SHiren Panchasara 			}
2677df8bae1dSRodney W. Grimes 			break;
2678021eaf79SHiren Panchasara 		} else {
2679021eaf79SHiren Panchasara 			/*
2680021eaf79SHiren Panchasara 			 * This ack is advancing the left edge, reset the
2681021eaf79SHiren Panchasara 			 * counter.
2682021eaf79SHiren Panchasara 			 */
2683021eaf79SHiren Panchasara 			tp->t_dupacks = 0;
2684021eaf79SHiren Panchasara 			/*
2685021eaf79SHiren Panchasara 			 * If this ack also has new SACK info, increment the
2686021eaf79SHiren Panchasara 			 * counter as per rfc6675.
2687021eaf79SHiren Panchasara 			 */
2688021eaf79SHiren Panchasara 			if ((tp->t_flags & TF_SACK_PERMIT) && sack_changed)
2689021eaf79SHiren Panchasara 				tp->t_dupacks++;
2690df8bae1dSRodney W. Grimes 		}
2691cb942153SJeffrey Hsu 
2692302ce8d6SAndre Oppermann 		KASSERT(SEQ_GT(th->th_ack, tp->snd_una),
2693302ce8d6SAndre Oppermann 		    ("%s: th_ack <= snd_una", __func__));
2694cb942153SJeffrey Hsu 
2695df8bae1dSRodney W. Grimes 		/*
2696df8bae1dSRodney W. Grimes 		 * If the congestion window was inflated to account
2697df8bae1dSRodney W. Grimes 		 * for the other side's cached packets, retract it.
2698df8bae1dSRodney W. Grimes 		 */
2699dbc42409SLawrence Stewart 		if (IN_FASTRECOVERY(tp->t_flags)) {
2700cb942153SJeffrey Hsu 			if (SEQ_LT(th->th_ack, tp->snd_recover)) {
27013529149eSAndre Oppermann 				if (tp->t_flags & TF_SACK_PERMIT)
27026d90faf3SPaul Saab 					tcp_sack_partialack(tp, th);
27036d90faf3SPaul Saab 				else
2704c068736aSJeffrey Hsu 					tcp_newreno_partial_ack(tp, th);
2705dbc42409SLawrence Stewart 			} else
2706dbc42409SLawrence Stewart 				cc_post_recovery(tp, th);
270746f58482SJonathan Lemon 		}
2708a0292f23SGarrett Wollman 		/*
2709a0292f23SGarrett Wollman 		 * If we reach this point, ACK is not a duplicate,
2710a0292f23SGarrett Wollman 		 *     i.e., it ACKs something we sent.
2711a0292f23SGarrett Wollman 		 */
2712a0292f23SGarrett Wollman 		if (tp->t_flags & TF_NEEDSYN) {
2713a0292f23SGarrett Wollman 			/*
2714a0292f23SGarrett Wollman 			 * T/TCP: Connection was half-synchronized, and our
2715a0292f23SGarrett Wollman 			 * SYN has been ACK'd (so connection is now fully
271607e43e10SAndras Olah 			 * synchronized).  Go to non-starred state,
271707e43e10SAndras Olah 			 * increment snd_una for ACK of SYN, and check if
271807e43e10SAndras Olah 			 * we can do window scaling.
2719a0292f23SGarrett Wollman 			 */
2720a0292f23SGarrett Wollman 			tp->t_flags &= ~TF_NEEDSYN;
2721a0292f23SGarrett Wollman 			tp->snd_una++;
272207e43e10SAndras Olah 			/* Do window scaling? */
272307e43e10SAndras Olah 			if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
272407e43e10SAndras Olah 				(TF_RCVD_SCALE|TF_REQ_SCALE)) {
272507e43e10SAndras Olah 				tp->rcv_scale = tp->request_r_scale;
2726464fcfbcSAndre Oppermann 				/* Send window already scaled. */
272707e43e10SAndras Olah 			}
2728a0292f23SGarrett Wollman 		}
2729a0292f23SGarrett Wollman 
2730a0292f23SGarrett Wollman process_ACK:
27318501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
27327cfc6904SRobert Watson 
2733dbc42409SLawrence Stewart 		acked = BYTES_THIS_ACK(tp, th);
273478b50714SRobert Watson 		TCPSTAT_INC(tcps_rcvackpack);
273578b50714SRobert Watson 		TCPSTAT_ADD(tcps_rcvackbyte, acked);
2736df8bae1dSRodney W. Grimes 
2737df8bae1dSRodney W. Grimes 		/*
27389b8b58e0SJonathan Lemon 		 * If we just performed our first retransmit, and the ACK
27399b8b58e0SJonathan Lemon 		 * arrives within our recovery window, then it was a mistake
27409b8b58e0SJonathan Lemon 		 * to do the retransmit in the first place.  Recover our
27419b8b58e0SJonathan Lemon 		 * original cwnd and ssthresh, and proceed to transmit where
27429b8b58e0SJonathan Lemon 		 * we left off.
27439b8b58e0SJonathan Lemon 		 */
2744672dc4aeSJohn Baldwin 		if (tp->t_rxtshift == 1 && tp->t_flags & TF_PREVVALID &&
2745672dc4aeSJohn Baldwin 		    (int)(ticks - tp->t_badrxtwin) < 0)
2746dbc42409SLawrence Stewart 			cc_cong_signal(tp, th, CC_RTO_ERR);
27479b8b58e0SJonathan Lemon 
27489b8b58e0SJonathan Lemon 		/*
2749df8bae1dSRodney W. Grimes 		 * If we have a timestamp reply, update smoothed
2750df8bae1dSRodney W. Grimes 		 * round trip time.  If no timestamp is present but
2751df8bae1dSRodney W. Grimes 		 * transmit timer is running and timed sequence
2752df8bae1dSRodney W. Grimes 		 * number was acked, update smoothed round trip time.
2753df8bae1dSRodney W. Grimes 		 * Since we now have an rtt measurement, cancel the
2754df8bae1dSRodney W. Grimes 		 * timer backoff (cf., Phil Karn's retransmit alg.).
2755df8bae1dSRodney W. Grimes 		 * Recompute the initial retransmit timer.
2756fa55172bSMatthew Dillon 		 *
2757fa55172bSMatthew Dillon 		 * Some boxes send broken timestamp replies
2758fa55172bSMatthew Dillon 		 * during the SYN+ACK phase, ignore
2759fa55172bSMatthew Dillon 		 * timestamps of 0 or we could calculate a
2760fa55172bSMatthew Dillon 		 * huge RTT and blow up the retransmit timer.
2761df8bae1dSRodney W. Grimes 		 */
2762d8951c8aSBjoern A. Zeeb 		if ((to.to_flags & TOF_TS) != 0 && to.to_tsecr) {
2763d8951c8aSBjoern A. Zeeb 			u_int t;
2764d8951c8aSBjoern A. Zeeb 
2765d8951c8aSBjoern A. Zeeb 			t = tcp_ts_getticks() - to.to_tsecr;
2766d8951c8aSBjoern A. Zeeb 			if (!tp->t_rttlow || tp->t_rttlow > t)
2767d8951c8aSBjoern A. Zeeb 				tp->t_rttlow = t;
2768d8951c8aSBjoern A. Zeeb 			tcp_xmit_timer(tp, TCP_TS_TO_TICKS(t) + 1);
2769fa55172bSMatthew Dillon 		} else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
2770eaf80179SAndre Oppermann 			if (!tp->t_rttlow || tp->t_rttlow > ticks - tp->t_rtttime)
2771eaf80179SAndre Oppermann 				tp->t_rttlow = ticks - tp->t_rtttime;
27729b8b58e0SJonathan Lemon 			tcp_xmit_timer(tp, ticks - tp->t_rtttime);
2773fa55172bSMatthew Dillon 		}
2774df8bae1dSRodney W. Grimes 
2775df8bae1dSRodney W. Grimes 		/*
2776df8bae1dSRodney W. Grimes 		 * If all outstanding data is acked, stop retransmit
2777df8bae1dSRodney W. Grimes 		 * timer and remember to restart (more output or persist).
2778df8bae1dSRodney W. Grimes 		 * If there is more data to be acked, restart retransmit
2779df8bae1dSRodney W. Grimes 		 * timer, using current (possibly backed-off) value.
2780df8bae1dSRodney W. Grimes 		 */
2781fb59c426SYoshinobu Inoue 		if (th->th_ack == tp->snd_max) {
2782b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, 0);
2783df8bae1dSRodney W. Grimes 			needoutput = 1;
2784b8152ba7SAndre Oppermann 		} else if (!tcp_timer_active(tp, TT_PERSIST))
2785b8152ba7SAndre Oppermann 			tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur);
2786a0292f23SGarrett Wollman 
2787a0292f23SGarrett Wollman 		/*
2788a0292f23SGarrett Wollman 		 * If no data (only SYN) was ACK'd,
2789a0292f23SGarrett Wollman 		 *    skip rest of ACK processing.
2790a0292f23SGarrett Wollman 		 */
2791a0292f23SGarrett Wollman 		if (acked == 0)
2792a0292f23SGarrett Wollman 			goto step6;
2793a0292f23SGarrett Wollman 
2794df8bae1dSRodney W. Grimes 		/*
2795dbc42409SLawrence Stewart 		 * Let the congestion control algorithm update congestion
2796dbc42409SLawrence Stewart 		 * control related information. This typically means increasing
2797dbc42409SLawrence Stewart 		 * the congestion window.
2798df8bae1dSRodney W. Grimes 		 */
2799dbc42409SLawrence Stewart 		cc_ack_received(tp, th, CC_ACK);
2800dbc42409SLawrence Stewart 
28015905999bSRobert Watson 		SOCKBUF_LOCK(&so->so_snd);
2802cfa6009eSGleb Smirnoff 		if (acked > sbavail(&so->so_snd)) {
2803cfa6009eSGleb Smirnoff 			tp->snd_wnd -= sbavail(&so->so_snd);
2804c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd,
2805cfa6009eSGleb Smirnoff 			    (int)sbavail(&so->so_snd));
2806df8bae1dSRodney W. Grimes 			ourfinisacked = 1;
2807df8bae1dSRodney W. Grimes 		} else {
2808c11a15bfSGleb Smirnoff 			mfree = sbcut_locked(&so->so_snd, acked);
280960ee3bb2SAndre Oppermann 			tp->snd_wnd -= acked;
2810df8bae1dSRodney W. Grimes 			ourfinisacked = 0;
2811df8bae1dSRodney W. Grimes 		}
2812564aab1fSAndre Oppermann 		/* NB: sowwakeup_locked() does an implicit unlock. */
28131e4d7da7SRobert Watson 		sowwakeup_locked(so);
2814c11a15bfSGleb Smirnoff 		m_freem(mfree);
2815e8949f74SAndre Oppermann 		/* Detect una wraparound. */
2816dbc42409SLawrence Stewart 		if (!IN_RECOVERY(tp->t_flags) &&
28179d11646dSJeffrey Hsu 		    SEQ_GT(tp->snd_una, tp->snd_recover) &&
28189d11646dSJeffrey Hsu 		    SEQ_LEQ(th->th_ack, tp->snd_recover))
28199d11646dSJeffrey Hsu 			tp->snd_recover = th->th_ack - 1;
2820dbc42409SLawrence Stewart 		/* XXXLAS: Can this be moved up into cc_post_recovery? */
2821dbc42409SLawrence Stewart 		if (IN_RECOVERY(tp->t_flags) &&
282224cb0f22SLawrence Stewart 		    SEQ_GEQ(th->th_ack, tp->snd_recover)) {
2823dbc42409SLawrence Stewart 			EXIT_RECOVERY(tp->t_flags);
282424cb0f22SLawrence Stewart 		}
2825fb59c426SYoshinobu Inoue 		tp->snd_una = th->th_ack;
28263529149eSAndre Oppermann 		if (tp->t_flags & TF_SACK_PERMIT) {
28276d90faf3SPaul Saab 			if (SEQ_GT(tp->snd_una, tp->snd_recover))
28286d90faf3SPaul Saab 				tp->snd_recover = tp->snd_una;
28296d90faf3SPaul Saab 		}
2830df8bae1dSRodney W. Grimes 		if (SEQ_LT(tp->snd_nxt, tp->snd_una))
2831df8bae1dSRodney W. Grimes 			tp->snd_nxt = tp->snd_una;
2832df8bae1dSRodney W. Grimes 
2833df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
2834df8bae1dSRodney W. Grimes 
2835df8bae1dSRodney W. Grimes 		/*
2836df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_1 STATE in addition to the processing
2837df8bae1dSRodney W. Grimes 		 * for the ESTABLISHED state if our FIN is now acknowledged
2838df8bae1dSRodney W. Grimes 		 * then enter FIN_WAIT_2.
2839df8bae1dSRodney W. Grimes 		 */
2840df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
2841df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2842df8bae1dSRodney W. Grimes 				/*
2843df8bae1dSRodney W. Grimes 				 * If we can't receive any more
2844df8bae1dSRodney W. Grimes 				 * data, then closing user can proceed.
2845df8bae1dSRodney W. Grimes 				 * Starting the timer is contrary to the
2846df8bae1dSRodney W. Grimes 				 * specification, but if we don't get a FIN
2847df8bae1dSRodney W. Grimes 				 * we'll hang forever.
2848e8949f74SAndre Oppermann 				 *
2849e8949f74SAndre Oppermann 				 * XXXjl:
2850340c35deSJonathan Lemon 				 * we should release the tp also, and use a
2851340c35deSJonathan Lemon 				 * compressed state.
2852340c35deSJonathan Lemon 				 */
2853c0b99ffaSRobert Watson 				if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
285403e49181SSeigo Tanimura 					soisdisconnected(so);
28559077f387SGleb Smirnoff 					tcp_timer_activate(tp, TT_2MSL,
28569077f387SGleb Smirnoff 					    (tcp_fast_finwait2_recycle ?
28579077f387SGleb Smirnoff 					    tcp_finwait2_timeout :
28589077f387SGleb Smirnoff 					    TP_MAXIDLE(tp)));
28594cc20ab1SSeigo Tanimura 				}
286057f60867SMark Johnston 				tcp_state_change(tp, TCPS_FIN_WAIT_2);
2861df8bae1dSRodney W. Grimes 			}
2862df8bae1dSRodney W. Grimes 			break;
2863df8bae1dSRodney W. Grimes 
2864df8bae1dSRodney W. Grimes 		/*
2865df8bae1dSRodney W. Grimes 		 * In CLOSING STATE in addition to the processing for
2866df8bae1dSRodney W. Grimes 		 * the ESTABLISHED state if the ACK acknowledges our FIN
2867df8bae1dSRodney W. Grimes 		 * then enter the TIME-WAIT state, otherwise ignore
2868df8bae1dSRodney W. Grimes 		 * the segment.
2869df8bae1dSRodney W. Grimes 		 */
2870df8bae1dSRodney W. Grimes 		case TCPS_CLOSING:
2871df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2872ff9b006dSJulien Charbon 				INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
287311a20fb8SJeffrey Hsu 				tcp_twstart(tp);
2874ff9b006dSJulien Charbon 				INP_INFO_RUNLOCK(&V_tcbinfo);
2875340c35deSJonathan Lemon 				m_freem(m);
2876679d9708SAndre Oppermann 				return;
2877df8bae1dSRodney W. Grimes 			}
2878df8bae1dSRodney W. Grimes 			break;
2879df8bae1dSRodney W. Grimes 
2880df8bae1dSRodney W. Grimes 		/*
2881df8bae1dSRodney W. Grimes 		 * In LAST_ACK, we may still be waiting for data to drain
2882df8bae1dSRodney W. Grimes 		 * and/or to be acked, as well as for the ack of our FIN.
2883df8bae1dSRodney W. Grimes 		 * If our FIN is now acknowledged, delete the TCB,
2884df8bae1dSRodney W. Grimes 		 * enter the closed state and return.
2885df8bae1dSRodney W. Grimes 		 */
2886df8bae1dSRodney W. Grimes 		case TCPS_LAST_ACK:
2887df8bae1dSRodney W. Grimes 			if (ourfinisacked) {
2888ff9b006dSJulien Charbon 				INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
2889df8bae1dSRodney W. Grimes 				tp = tcp_close(tp);
2890df8bae1dSRodney W. Grimes 				goto drop;
2891df8bae1dSRodney W. Grimes 			}
2892df8bae1dSRodney W. Grimes 			break;
2893df8bae1dSRodney W. Grimes 		}
2894df8bae1dSRodney W. Grimes 	}
2895df8bae1dSRodney W. Grimes 
2896df8bae1dSRodney W. Grimes step6:
28978501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
28987cfc6904SRobert Watson 
2899df8bae1dSRodney W. Grimes 	/*
290060ee3bb2SAndre Oppermann 	 * Update window information.
290160ee3bb2SAndre Oppermann 	 * Don't look at window if no ACK: TAC's send garbage on first SYN.
2902df8bae1dSRodney W. Grimes 	 */
290360ee3bb2SAndre Oppermann 	if ((thflags & TH_ACK) &&
290460ee3bb2SAndre Oppermann 	    (SEQ_LT(tp->snd_wl1, th->th_seq) ||
290560ee3bb2SAndre Oppermann 	    (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
290660ee3bb2SAndre Oppermann 	     (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
290760ee3bb2SAndre Oppermann 		/* keep track of pure window updates */
290860ee3bb2SAndre Oppermann 		if (tlen == 0 &&
290960ee3bb2SAndre Oppermann 		    tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
291078b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvwinupd);
2911df8bae1dSRodney W. Grimes 		tp->snd_wnd = tiwin;
291260ee3bb2SAndre Oppermann 		tp->snd_wl1 = th->th_seq;
291360ee3bb2SAndre Oppermann 		tp->snd_wl2 = th->th_ack;
2914df8bae1dSRodney W. Grimes 		if (tp->snd_wnd > tp->max_sndwnd)
2915df8bae1dSRodney W. Grimes 			tp->max_sndwnd = tp->snd_wnd;
291660ee3bb2SAndre Oppermann 		needoutput = 1;
2917df8bae1dSRodney W. Grimes 	}
2918df8bae1dSRodney W. Grimes 
2919df8bae1dSRodney W. Grimes 	/*
2920df8bae1dSRodney W. Grimes 	 * Process segments with URG.
2921df8bae1dSRodney W. Grimes 	 */
2922fb59c426SYoshinobu Inoue 	if ((thflags & TH_URG) && th->th_urp &&
2923df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2924df8bae1dSRodney W. Grimes 		/*
2925df8bae1dSRodney W. Grimes 		 * This is a kludge, but if we receive and accept
2926df8bae1dSRodney W. Grimes 		 * random urgent pointers, we'll crash in
2927df8bae1dSRodney W. Grimes 		 * soreceive.  It's hard to imagine someone
2928df8bae1dSRodney W. Grimes 		 * actually wanting to send this much urgent data.
2929df8bae1dSRodney W. Grimes 		 */
293018ad5842SRobert Watson 		SOCKBUF_LOCK(&so->so_rcv);
2931cfa6009eSGleb Smirnoff 		if (th->th_urp + sbavail(&so->so_rcv) > sb_max) {
2932fb59c426SYoshinobu Inoue 			th->th_urp = 0;			/* XXX */
2933fb59c426SYoshinobu Inoue 			thflags &= ~TH_URG;		/* XXX */
293418ad5842SRobert Watson 			SOCKBUF_UNLOCK(&so->so_rcv);	/* XXX */
2935df8bae1dSRodney W. Grimes 			goto dodata;			/* XXX */
2936df8bae1dSRodney W. Grimes 		}
2937df8bae1dSRodney W. Grimes 		/*
2938df8bae1dSRodney W. Grimes 		 * If this segment advances the known urgent pointer,
2939df8bae1dSRodney W. Grimes 		 * then mark the data stream.  This should not happen
2940df8bae1dSRodney W. Grimes 		 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2941df8bae1dSRodney W. Grimes 		 * a FIN has been received from the remote side.
2942df8bae1dSRodney W. Grimes 		 * In these states we ignore the URG.
2943df8bae1dSRodney W. Grimes 		 *
2944df8bae1dSRodney W. Grimes 		 * According to RFC961 (Assigned Protocols),
2945df8bae1dSRodney W. Grimes 		 * the urgent pointer points to the last octet
2946df8bae1dSRodney W. Grimes 		 * of urgent data.  We continue, however,
2947df8bae1dSRodney W. Grimes 		 * to consider it to indicate the first octet
2948df8bae1dSRodney W. Grimes 		 * of data past the urgent section as the original
2949df8bae1dSRodney W. Grimes 		 * spec states (in one of two places).
2950df8bae1dSRodney W. Grimes 		 */
2951fb59c426SYoshinobu Inoue 		if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
2952fb59c426SYoshinobu Inoue 			tp->rcv_up = th->th_seq + th->th_urp;
2953cfa6009eSGleb Smirnoff 			so->so_oobmark = sbavail(&so->so_rcv) +
2954df8bae1dSRodney W. Grimes 			    (tp->rcv_up - tp->rcv_nxt) - 1;
2955927c5ceaSRobert Watson 			if (so->so_oobmark == 0)
2956c0b99ffaSRobert Watson 				so->so_rcv.sb_state |= SBS_RCVATMARK;
2957df8bae1dSRodney W. Grimes 			sohasoutofband(so);
2958df8bae1dSRodney W. Grimes 			tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2959df8bae1dSRodney W. Grimes 		}
296018ad5842SRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
2961df8bae1dSRodney W. Grimes 		/*
2962df8bae1dSRodney W. Grimes 		 * Remove out of band data so doesn't get presented to user.
2963df8bae1dSRodney W. Grimes 		 * This can happen independent of advancing the URG pointer,
2964df8bae1dSRodney W. Grimes 		 * but if two URG's are pending at once, some out-of-band
2965df8bae1dSRodney W. Grimes 		 * data may creep in... ick.
2966df8bae1dSRodney W. Grimes 		 */
296730613f56SJeffrey Hsu 		if (th->th_urp <= (u_long)tlen &&
296830613f56SJeffrey Hsu 		    !(so->so_options & SO_OOBINLINE)) {
296930613f56SJeffrey Hsu 			/* hdr drop is delayed */
297030613f56SJeffrey Hsu 			tcp_pulloutofband(so, th, m, drop_hdrlen);
297130613f56SJeffrey Hsu 		}
2972c068736aSJeffrey Hsu 	} else {
2973df8bae1dSRodney W. Grimes 		/*
2974df8bae1dSRodney W. Grimes 		 * If no out of band data is expected,
2975df8bae1dSRodney W. Grimes 		 * pull receive urgent pointer along
2976df8bae1dSRodney W. Grimes 		 * with the receive window.
2977df8bae1dSRodney W. Grimes 		 */
2978df8bae1dSRodney W. Grimes 		if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
2979df8bae1dSRodney W. Grimes 			tp->rcv_up = tp->rcv_nxt;
2980c068736aSJeffrey Hsu 	}
2981df8bae1dSRodney W. Grimes dodata:							/* XXX */
29828501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
29837cfc6904SRobert Watson 
2984df8bae1dSRodney W. Grimes 	/*
2985df8bae1dSRodney W. Grimes 	 * Process the segment text, merging it into the TCP sequencing queue,
2986df8bae1dSRodney W. Grimes 	 * and arranging for acknowledgment of receipt if necessary.
2987df8bae1dSRodney W. Grimes 	 * This process logically involves adjusting tp->rcv_wnd as data
2988df8bae1dSRodney W. Grimes 	 * is presented to the user (this happens in tcp_usrreq.c,
2989df8bae1dSRodney W. Grimes 	 * case PRU_RCVD).  If a FIN has already been received on this
2990df8bae1dSRodney W. Grimes 	 * connection then we just ignore the text.
2991df8bae1dSRodney W. Grimes 	 */
2992281a0fd4SPatrick Kelsey 	tfo_syn = ((tp->t_state == TCPS_SYN_RECEIVED) &&
2993281a0fd4SPatrick Kelsey 		   (tp->t_flags & TF_FASTOPEN));
2994281a0fd4SPatrick Kelsey 	if ((tlen || (thflags & TH_FIN) || tfo_syn) &&
2995df8bae1dSRodney W. Grimes 	    TCPS_HAVERCVDFIN(tp->t_state) == 0) {
299669e03620SPaul Saab 		tcp_seq save_start = th->th_seq;
2997fb59c426SYoshinobu Inoue 		m_adj(m, drop_hdrlen);	/* delayed header drop */
2998e4b64281SJesper Skriver 		/*
2999c068736aSJeffrey Hsu 		 * Insert segment which includes th into TCP reassembly queue
3000c068736aSJeffrey Hsu 		 * with control block tp.  Set thflags to whether reassembly now
3001c068736aSJeffrey Hsu 		 * includes a segment with FIN.  This handles the common case
3002c068736aSJeffrey Hsu 		 * inline (segment is the next to be received on an established
3003c068736aSJeffrey Hsu 		 * connection, and the queue is empty), avoiding linkage into
3004c068736aSJeffrey Hsu 		 * and removal from the queue and repetition of various
3005c068736aSJeffrey Hsu 		 * conversions.
3006c068736aSJeffrey Hsu 		 * Set DELACK for segments received in order, but ack
3007c068736aSJeffrey Hsu 		 * immediately when segments are out of order (so
3008c068736aSJeffrey Hsu 		 * fast retransmit can work).
3009e4b64281SJesper Skriver 		 */
30104741bfcbSPatrick Kelsey 		if (th->th_seq == tp->rcv_nxt &&
30114741bfcbSPatrick Kelsey 		    LIST_EMPTY(&tp->t_segq) &&
3012281a0fd4SPatrick Kelsey 		    (TCPS_HAVEESTABLISHED(tp->t_state) ||
3013281a0fd4SPatrick Kelsey 		     tfo_syn)) {
3014281a0fd4SPatrick Kelsey 			if (DELAY_ACK(tp, tlen) || tfo_syn)
30153bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
3016e4b64281SJesper Skriver 			else
3017e4b64281SJesper Skriver 				tp->t_flags |= TF_ACKNOW;
3018e4b64281SJesper Skriver 			tp->rcv_nxt += tlen;
3019e4b64281SJesper Skriver 			thflags = th->th_flags & TH_FIN;
302078b50714SRobert Watson 			TCPSTAT_INC(tcps_rcvpack);
302178b50714SRobert Watson 			TCPSTAT_ADD(tcps_rcvbyte, tlen);
30221e4d7da7SRobert Watson 			SOCKBUF_LOCK(&so->so_rcv);
3023c0b99ffaSRobert Watson 			if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
3024c1c36a2cSMike Silbersack 				m_freem(m);
3025c1c36a2cSMike Silbersack 			else
3026651e4e6aSGleb Smirnoff 				sbappendstream_locked(&so->so_rcv, m, 0);
3027e8949f74SAndre Oppermann 			/* NB: sorwakeup_locked() does an implicit unlock. */
30281e4d7da7SRobert Watson 			sorwakeup_locked(so);
3029e4b64281SJesper Skriver 		} else {
3030e8949f74SAndre Oppermann 			/*
3031e8949f74SAndre Oppermann 			 * XXX: Due to the header drop above "th" is
3032e8949f74SAndre Oppermann 			 * theoretically invalid by now.  Fortunately
3033e8949f74SAndre Oppermann 			 * m_adj() doesn't actually frees any mbufs
3034e8949f74SAndre Oppermann 			 * when trimming from the head.
3035e8949f74SAndre Oppermann 			 */
3036e4b64281SJesper Skriver 			thflags = tcp_reass(tp, th, &tlen, m);
3037e4b64281SJesper Skriver 			tp->t_flags |= TF_ACKNOW;
3038e4b64281SJesper Skriver 		}
30393529149eSAndre Oppermann 		if (tlen > 0 && (tp->t_flags & TF_SACK_PERMIT))
3040f194524fSAndre Oppermann 			tcp_update_sack_list(tp, save_start, save_start + tlen);
3041302ce8d6SAndre Oppermann #if 0
3042df8bae1dSRodney W. Grimes 		/*
3043df8bae1dSRodney W. Grimes 		 * Note the amount of data that peer has sent into
3044df8bae1dSRodney W. Grimes 		 * our window, in order to estimate the sender's
3045df8bae1dSRodney W. Grimes 		 * buffer size.
3046302ce8d6SAndre Oppermann 		 * XXX: Unused.
3047df8bae1dSRodney W. Grimes 		 */
3048f701e30dSJohn Baldwin 		if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt))
3049df8bae1dSRodney W. Grimes 			len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
3050f701e30dSJohn Baldwin 		else
3051f701e30dSJohn Baldwin 			len = so->so_rcv.sb_hiwat;
3052302ce8d6SAndre Oppermann #endif
3053df8bae1dSRodney W. Grimes 	} else {
3054df8bae1dSRodney W. Grimes 		m_freem(m);
3055fb59c426SYoshinobu Inoue 		thflags &= ~TH_FIN;
3056df8bae1dSRodney W. Grimes 	}
3057df8bae1dSRodney W. Grimes 
3058df8bae1dSRodney W. Grimes 	/*
3059df8bae1dSRodney W. Grimes 	 * If FIN is received ACK the FIN and let the user know
3060df8bae1dSRodney W. Grimes 	 * that the connection is closing.
3061df8bae1dSRodney W. Grimes 	 */
3062fb59c426SYoshinobu Inoue 	if (thflags & TH_FIN) {
3063df8bae1dSRodney W. Grimes 		if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
3064df8bae1dSRodney W. Grimes 			socantrcvmore(so);
3065a0292f23SGarrett Wollman 			/*
3066a0292f23SGarrett Wollman 			 * If connection is half-synchronized
3067d3eede9dSAndras Olah 			 * (ie NEEDSYN flag on) then delay ACK,
3068a0292f23SGarrett Wollman 			 * so it may be piggybacked when SYN is sent.
3069a0292f23SGarrett Wollman 			 * Otherwise, since we received a FIN then no
3070a0292f23SGarrett Wollman 			 * more input can be expected, send ACK now.
3071a0292f23SGarrett Wollman 			 */
30723bfd6421SJonathan Lemon 			if (tp->t_flags & TF_NEEDSYN)
30733bfd6421SJonathan Lemon 				tp->t_flags |= TF_DELACK;
3074a0292f23SGarrett Wollman 			else
3075df8bae1dSRodney W. Grimes 				tp->t_flags |= TF_ACKNOW;
3076df8bae1dSRodney W. Grimes 			tp->rcv_nxt++;
3077df8bae1dSRodney W. Grimes 		}
3078df8bae1dSRodney W. Grimes 		switch (tp->t_state) {
3079df8bae1dSRodney W. Grimes 
3080df8bae1dSRodney W. Grimes 		/*
3081df8bae1dSRodney W. Grimes 		 * In SYN_RECEIVED and ESTABLISHED STATES
3082df8bae1dSRodney W. Grimes 		 * enter the CLOSE_WAIT state.
3083df8bae1dSRodney W. Grimes 		 */
3084df8bae1dSRodney W. Grimes 		case TCPS_SYN_RECEIVED:
30859b8b58e0SJonathan Lemon 			tp->t_starttime = ticks;
30869b8b58e0SJonathan Lemon 			/* FALLTHROUGH */
3087df8bae1dSRodney W. Grimes 		case TCPS_ESTABLISHED:
308857f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSE_WAIT);
3089df8bae1dSRodney W. Grimes 			break;
3090df8bae1dSRodney W. Grimes 
3091df8bae1dSRodney W. Grimes 		/*
3092df8bae1dSRodney W. Grimes 		 * If still in FIN_WAIT_1 STATE FIN has not been acked so
3093df8bae1dSRodney W. Grimes 		 * enter the CLOSING state.
3094df8bae1dSRodney W. Grimes 		 */
3095df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_1:
309657f60867SMark Johnston 			tcp_state_change(tp, TCPS_CLOSING);
3097df8bae1dSRodney W. Grimes 			break;
3098df8bae1dSRodney W. Grimes 
3099df8bae1dSRodney W. Grimes 		/*
3100df8bae1dSRodney W. Grimes 		 * In FIN_WAIT_2 state enter the TIME_WAIT state,
3101df8bae1dSRodney W. Grimes 		 * starting the time-wait timer, turning off the other
3102df8bae1dSRodney W. Grimes 		 * standard timers.
3103df8bae1dSRodney W. Grimes 		 */
3104df8bae1dSRodney W. Grimes 		case TCPS_FIN_WAIT_2:
3105ff9b006dSJulien Charbon 			INP_INFO_RLOCK_ASSERT(&V_tcbinfo);
3106ff9b006dSJulien Charbon 			KASSERT(ti_locked == TI_RLOCKED, ("%s: dodata "
3107252ca428SRobert Watson 			    "TCP_FIN_WAIT_2 ti_locked: %d", __func__,
3108252ca428SRobert Watson 			    ti_locked));
3109252ca428SRobert Watson 
3110340c35deSJonathan Lemon 			tcp_twstart(tp);
3111ff9b006dSJulien Charbon 			INP_INFO_RUNLOCK(&V_tcbinfo);
3112679d9708SAndre Oppermann 			return;
3113df8bae1dSRodney W. Grimes 		}
3114df8bae1dSRodney W. Grimes 	}
3115ff9b006dSJulien Charbon 	if (ti_locked == TI_RLOCKED)
3116ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
3117252ca428SRobert Watson 	ti_locked = TI_UNLOCKED;
3118252ca428SRobert Watson 
3119610ee2f9SDavid Greenman #ifdef TCPDEBUG
31204cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
3121fb59c426SYoshinobu Inoue 		tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
3122fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3123610ee2f9SDavid Greenman #endif
31245d06879aSGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, mtod(m, const char *));
3125df8bae1dSRodney W. Grimes 
3126df8bae1dSRodney W. Grimes 	/*
3127df8bae1dSRodney W. Grimes 	 * Return any desired output.
3128df8bae1dSRodney W. Grimes 	 */
3129df8bae1dSRodney W. Grimes 	if (needoutput || (tp->t_flags & TF_ACKNOW))
313055bceb1eSRandall Stewart 		(void) tp->t_fb->tfb_tcp_output(tp);
31317792ea27SJeffrey Hsu 
3132a14c749fSJonathan Lemon check_delack:
3133252ca428SRobert Watson 	KASSERT(ti_locked == TI_UNLOCKED, ("%s: check_delack ti_locked %d",
3134252ca428SRobert Watson 	    __func__, ti_locked));
3135603724d3SBjoern A. Zeeb 	INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
31368501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
3137252ca428SRobert Watson 
3138a14c749fSJonathan Lemon 	if (tp->t_flags & TF_DELACK) {
31393bfd6421SJonathan Lemon 		tp->t_flags &= ~TF_DELACK;
3140b8152ba7SAndre Oppermann 		tcp_timer_activate(tp, TT_DELACK, tcp_delacktime);
31413bfd6421SJonathan Lemon 	}
31428501a69cSRobert Watson 	INP_WUNLOCK(tp->t_inpcb);
3143679d9708SAndre Oppermann 	return;
3144df8bae1dSRodney W. Grimes 
3145df8bae1dSRodney W. Grimes dropafterack:
3146df8bae1dSRodney W. Grimes 	/*
3147df8bae1dSRodney W. Grimes 	 * Generate an ACK dropping incoming segment if it occupies
3148df8bae1dSRodney W. Grimes 	 * sequence space, where the ACK reflects our state.
314980ab7c0eSGarrett Wollman 	 *
315080ab7c0eSGarrett Wollman 	 * We can now skip the test for the RST flag since all
315180ab7c0eSGarrett Wollman 	 * paths to this code happen after packets containing
315280ab7c0eSGarrett Wollman 	 * RST have been dropped.
315380ab7c0eSGarrett Wollman 	 *
315480ab7c0eSGarrett Wollman 	 * In the SYN-RECEIVED state, don't send an ACK unless the
315580ab7c0eSGarrett Wollman 	 * segment we received passes the SYN-RECEIVED ACK test.
315680ab7c0eSGarrett Wollman 	 * If it fails send a RST.  This breaks the loop in the
315780ab7c0eSGarrett Wollman 	 * "LAND" DoS attack, and also prevents an ACK storm
315880ab7c0eSGarrett Wollman 	 * between two listening ports that have been sent forged
315980ab7c0eSGarrett Wollman 	 * SYN segments, each with the source address of the other.
3160df8bae1dSRodney W. Grimes 	 */
3161fb59c426SYoshinobu Inoue 	if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
3162fb59c426SYoshinobu Inoue 	    (SEQ_GT(tp->snd_una, th->th_ack) ||
3163a57815efSBosko Milekic 	     SEQ_GT(th->th_ack, tp->snd_max)) ) {
3164a57815efSBosko Milekic 		rstreason = BANDLIM_RST_OPENPORT;
3165a57815efSBosko Milekic 		goto dropwithreset;
3166a57815efSBosko Milekic 	}
3167a0292f23SGarrett Wollman #ifdef TCPDEBUG
31684cc20ab1SSeigo Tanimura 	if (so->so_options & SO_DEBUG)
3169fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
3170fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3171a0292f23SGarrett Wollman #endif
31725d06879aSGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, mtod(m, const char *));
3173ff9b006dSJulien Charbon 	if (ti_locked == TI_RLOCKED)
3174ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
3175252ca428SRobert Watson 	ti_locked = TI_UNLOCKED;
3176252ca428SRobert Watson 
3177df8bae1dSRodney W. Grimes 	tp->t_flags |= TF_ACKNOW;
317855bceb1eSRandall Stewart 	(void) tp->t_fb->tfb_tcp_output(tp);
31798501a69cSRobert Watson 	INP_WUNLOCK(tp->t_inpcb);
3180d6915262SRobert Watson 	m_freem(m);
3181679d9708SAndre Oppermann 	return;
3182df8bae1dSRodney W. Grimes 
3183df8bae1dSRodney W. Grimes dropwithreset:
3184ff9b006dSJulien Charbon 	if (ti_locked == TI_RLOCKED)
3185ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
3186252ca428SRobert Watson 	ti_locked = TI_UNLOCKED;
3187a57815efSBosko Milekic 
3188a0ca0871SRobert Watson 	if (tp != NULL) {
3189302ce8d6SAndre Oppermann 		tcp_dropwithreset(m, th, tp, tlen, rstreason);
31908501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
3191dd8ac7f9SRobert Watson 	} else
3192a0ca0871SRobert Watson 		tcp_dropwithreset(m, th, NULL, tlen, rstreason);
3193679d9708SAndre Oppermann 	return;
3194df8bae1dSRodney W. Grimes 
3195df8bae1dSRodney W. Grimes drop:
3196ff9b006dSJulien Charbon 	if (ti_locked == TI_RLOCKED) {
3197ff9b006dSJulien Charbon 		INP_INFO_RUNLOCK(&V_tcbinfo);
3198fa046d87SRobert Watson 		ti_locked = TI_UNLOCKED;
3199fa046d87SRobert Watson 	}
3200252ca428SRobert Watson #ifdef INVARIANTS
3201252ca428SRobert Watson 	else
3202252ca428SRobert Watson 		INP_INFO_UNLOCK_ASSERT(&V_tcbinfo);
3203252ca428SRobert Watson #endif
3204252ca428SRobert Watson 
3205df8bae1dSRodney W. Grimes 	/*
3206df8bae1dSRodney W. Grimes 	 * Drop space held by incoming segment and return.
3207df8bae1dSRodney W. Grimes 	 */
3208610ee2f9SDavid Greenman #ifdef TCPDEBUG
32091c53f806SRobert Watson 	if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
3210fb59c426SYoshinobu Inoue 		tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
3211fb59c426SYoshinobu Inoue 			  &tcp_savetcp, 0);
3212a0292f23SGarrett Wollman #endif
32135d06879aSGeorge V. Neville-Neil 	TCP_PROBE3(debug__input, tp, th, mtod(m, const char *));
32141c53f806SRobert Watson 	if (tp != NULL)
32158501a69cSRobert Watson 		INP_WUNLOCK(tp->t_inpcb);
3216d6915262SRobert Watson 	m_freem(m);
3217302ce8d6SAndre Oppermann }
3218302ce8d6SAndre Oppermann 
3219302ce8d6SAndre Oppermann /*
32200ca3f933SAndre Oppermann  * Issue RST and make ACK acceptable to originator of segment.
32210ca3f933SAndre Oppermann  * The mbuf must still include the original packet header.
32220ca3f933SAndre Oppermann  * tp may be NULL.
3223302ce8d6SAndre Oppermann  */
322455bceb1eSRandall Stewart void
3225302ce8d6SAndre Oppermann tcp_dropwithreset(struct mbuf *m, struct tcphdr *th, struct tcpcb *tp,
3226302ce8d6SAndre Oppermann     int tlen, int rstreason)
3227302ce8d6SAndre Oppermann {
3228b287c6c7SBjoern A. Zeeb #ifdef INET
3229302ce8d6SAndre Oppermann 	struct ip *ip;
3230b287c6c7SBjoern A. Zeeb #endif
3231302ce8d6SAndre Oppermann #ifdef INET6
3232302ce8d6SAndre Oppermann 	struct ip6_hdr *ip6;
3233302ce8d6SAndre Oppermann #endif
32347a3244ccSRobert Watson 
32357a3244ccSRobert Watson 	if (tp != NULL) {
32368501a69cSRobert Watson 		INP_WLOCK_ASSERT(tp->t_inpcb);
32377a3244ccSRobert Watson 	}
32387a3244ccSRobert Watson 
32390ca3f933SAndre Oppermann 	/* Don't bother if destination was broadcast/multicast. */
3240302ce8d6SAndre Oppermann 	if ((th->th_flags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
3241302ce8d6SAndre Oppermann 		goto drop;
3242302ce8d6SAndre Oppermann #ifdef INET6
3243302ce8d6SAndre Oppermann 	if (mtod(m, struct ip *)->ip_v == 6) {
3244302ce8d6SAndre Oppermann 		ip6 = mtod(m, struct ip6_hdr *);
3245302ce8d6SAndre Oppermann 		if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
3246302ce8d6SAndre Oppermann 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
3247302ce8d6SAndre Oppermann 			goto drop;
3248302ce8d6SAndre Oppermann 		/* IPv6 anycast check is done at tcp6_input() */
3249b287c6c7SBjoern A. Zeeb 	}
3250302ce8d6SAndre Oppermann #endif
3251b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3252b287c6c7SBjoern A. Zeeb 	else
3253b287c6c7SBjoern A. Zeeb #endif
3254b287c6c7SBjoern A. Zeeb #ifdef INET
3255302ce8d6SAndre Oppermann 	{
3256302ce8d6SAndre Oppermann 		ip = mtod(m, struct ip *);
3257302ce8d6SAndre Oppermann 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
3258302ce8d6SAndre Oppermann 		    IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
3259302ce8d6SAndre Oppermann 		    ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
3260302ce8d6SAndre Oppermann 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
3261302ce8d6SAndre Oppermann 			goto drop;
3262302ce8d6SAndre Oppermann 	}
3263b287c6c7SBjoern A. Zeeb #endif
3264302ce8d6SAndre Oppermann 
3265302ce8d6SAndre Oppermann 	/* Perform bandwidth limiting. */
3266302ce8d6SAndre Oppermann 	if (badport_bandlim(rstreason) < 0)
3267302ce8d6SAndre Oppermann 		goto drop;
3268302ce8d6SAndre Oppermann 
3269302ce8d6SAndre Oppermann 	/* tcp_respond consumes the mbuf chain. */
3270302ce8d6SAndre Oppermann 	if (th->th_flags & TH_ACK) {
3271302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0,
3272302ce8d6SAndre Oppermann 		    th->th_ack, TH_RST);
3273302ce8d6SAndre Oppermann 	} else {
3274302ce8d6SAndre Oppermann 		if (th->th_flags & TH_SYN)
3275302ce8d6SAndre Oppermann 			tlen++;
3276302ce8d6SAndre Oppermann 		tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
3277302ce8d6SAndre Oppermann 		    (tcp_seq)0, TH_RST|TH_ACK);
3278302ce8d6SAndre Oppermann 	}
3279302ce8d6SAndre Oppermann 	return;
3280302ce8d6SAndre Oppermann drop:
3281302ce8d6SAndre Oppermann 	m_freem(m);
3282df8bae1dSRodney W. Grimes }
3283df8bae1dSRodney W. Grimes 
3284be2ac88cSJonathan Lemon /*
3285be2ac88cSJonathan Lemon  * Parse TCP options and place in tcpopt.
3286be2ac88cSJonathan Lemon  */
328755bceb1eSRandall Stewart void
3288ad3f9ab3SAndre Oppermann tcp_dooptions(struct tcpopt *to, u_char *cp, int cnt, int flags)
3289df8bae1dSRodney W. Grimes {
3290df8bae1dSRodney W. Grimes 	int opt, optlen;
3291df8bae1dSRodney W. Grimes 
3292be2ac88cSJonathan Lemon 	to->to_flags = 0;
3293df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
3294df8bae1dSRodney W. Grimes 		opt = cp[0];
3295df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_EOL)
3296df8bae1dSRodney W. Grimes 			break;
3297df8bae1dSRodney W. Grimes 		if (opt == TCPOPT_NOP)
3298df8bae1dSRodney W. Grimes 			optlen = 1;
3299df8bae1dSRodney W. Grimes 		else {
3300b474779fSJun-ichiro itojun Hagino 			if (cnt < 2)
3301b474779fSJun-ichiro itojun Hagino 				break;
3302df8bae1dSRodney W. Grimes 			optlen = cp[1];
3303b474779fSJun-ichiro itojun Hagino 			if (optlen < 2 || optlen > cnt)
3304df8bae1dSRodney W. Grimes 				break;
3305df8bae1dSRodney W. Grimes 		}
3306df8bae1dSRodney W. Grimes 		switch (opt) {
3307df8bae1dSRodney W. Grimes 		case TCPOPT_MAXSEG:
3308df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_MAXSEG)
3309df8bae1dSRodney W. Grimes 				continue;
3310f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3311df8bae1dSRodney W. Grimes 				continue;
3312be2ac88cSJonathan Lemon 			to->to_flags |= TOF_MSS;
3313be2ac88cSJonathan Lemon 			bcopy((char *)cp + 2,
3314be2ac88cSJonathan Lemon 			    (char *)&to->to_mss, sizeof(to->to_mss));
3315fd8e4ebcSMike Barcroft 			to->to_mss = ntohs(to->to_mss);
3316df8bae1dSRodney W. Grimes 			break;
3317df8bae1dSRodney W. Grimes 		case TCPOPT_WINDOW:
3318df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_WINDOW)
3319df8bae1dSRodney W. Grimes 				continue;
3320f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3321df8bae1dSRodney W. Grimes 				continue;
3322be2ac88cSJonathan Lemon 			to->to_flags |= TOF_SCALE;
332302a1a643SAndre Oppermann 			to->to_wscale = min(cp[2], TCP_MAX_WINSHIFT);
3324df8bae1dSRodney W. Grimes 			break;
3325df8bae1dSRodney W. Grimes 		case TCPOPT_TIMESTAMP:
3326df8bae1dSRodney W. Grimes 			if (optlen != TCPOLEN_TIMESTAMP)
3327df8bae1dSRodney W. Grimes 				continue;
3328be2ac88cSJonathan Lemon 			to->to_flags |= TOF_TS;
3329a0292f23SGarrett Wollman 			bcopy((char *)cp + 2,
3330a0292f23SGarrett Wollman 			    (char *)&to->to_tsval, sizeof(to->to_tsval));
3331fd8e4ebcSMike Barcroft 			to->to_tsval = ntohl(to->to_tsval);
3332a0292f23SGarrett Wollman 			bcopy((char *)cp + 6,
3333a0292f23SGarrett Wollman 			    (char *)&to->to_tsecr, sizeof(to->to_tsecr));
3334fd8e4ebcSMike Barcroft 			to->to_tsecr = ntohl(to->to_tsecr);
3335df8bae1dSRodney W. Grimes 			break;
33361cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE
33371cfd4b53SBruce M Simpson 		/*
33381cfd4b53SBruce M Simpson 		 * XXX In order to reply to a host which has set the
33391cfd4b53SBruce M Simpson 		 * TCP_SIGNATURE option in its initial SYN, we have to
33401cfd4b53SBruce M Simpson 		 * record the fact that the option was observed here
33411cfd4b53SBruce M Simpson 		 * for the syncache code to perform the correct response.
33421cfd4b53SBruce M Simpson 		 */
33431cfd4b53SBruce M Simpson 		case TCPOPT_SIGNATURE:
33441cfd4b53SBruce M Simpson 			if (optlen != TCPOLEN_SIGNATURE)
33451cfd4b53SBruce M Simpson 				continue;
3346df47e437SAndre Oppermann 			to->to_flags |= TOF_SIGNATURE;
3347df47e437SAndre Oppermann 			to->to_signature = cp + 2;
33481cfd4b53SBruce M Simpson 			break;
3349265ed012SBruce M Simpson #endif
33506d90faf3SPaul Saab 		case TCPOPT_SACK_PERMITTED:
3351f72167f4SAndre Oppermann 			if (optlen != TCPOLEN_SACK_PERMITTED)
33526d90faf3SPaul Saab 				continue;
3353f72167f4SAndre Oppermann 			if (!(flags & TO_SYN))
3354f72167f4SAndre Oppermann 				continue;
3355603724d3SBjoern A. Zeeb 			if (!V_tcp_do_sack)
3356f72167f4SAndre Oppermann 				continue;
3357fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACKPERM;
33586d90faf3SPaul Saab 			break;
33596d90faf3SPaul Saab 		case TCPOPT_SACK:
33605a53ca16SPaul Saab 			if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
33616d90faf3SPaul Saab 				continue;
3362b728e902SAndre Oppermann 			if (flags & TO_SYN)
3363b728e902SAndre Oppermann 				continue;
3364fc30a251SAndre Oppermann 			to->to_flags |= TOF_SACK;
33655a53ca16SPaul Saab 			to->to_nsacks = (optlen - 2) / TCPOLEN_SACK;
33665a53ca16SPaul Saab 			to->to_sacks = cp + 2;
336778b50714SRobert Watson 			TCPSTAT_INC(tcps_sack_rcv_blocks);
33686d90faf3SPaul Saab 			break;
3369281a0fd4SPatrick Kelsey #ifdef TCP_RFC7413
3370281a0fd4SPatrick Kelsey 		case TCPOPT_FAST_OPEN:
3371281a0fd4SPatrick Kelsey 			if ((optlen != TCPOLEN_FAST_OPEN_EMPTY) &&
3372281a0fd4SPatrick Kelsey 			    (optlen < TCPOLEN_FAST_OPEN_MIN) &&
3373281a0fd4SPatrick Kelsey 			    (optlen > TCPOLEN_FAST_OPEN_MAX))
3374281a0fd4SPatrick Kelsey 				continue;
3375281a0fd4SPatrick Kelsey 			if (!(flags & TO_SYN))
3376281a0fd4SPatrick Kelsey 				continue;
3377281a0fd4SPatrick Kelsey 			if (!V_tcp_fastopen_enabled)
3378281a0fd4SPatrick Kelsey 				continue;
3379281a0fd4SPatrick Kelsey 			to->to_flags |= TOF_FASTOPEN;
3380281a0fd4SPatrick Kelsey 			to->to_tfo_len = optlen - 2;
3381281a0fd4SPatrick Kelsey 			to->to_tfo_cookie = to->to_tfo_len ? cp + 2 : NULL;
3382281a0fd4SPatrick Kelsey 			break;
3383281a0fd4SPatrick Kelsey #endif
3384be2ac88cSJonathan Lemon 		default:
3385be2ac88cSJonathan Lemon 			continue;
3386df8bae1dSRodney W. Grimes 		}
3387df8bae1dSRodney W. Grimes 	}
3388df8bae1dSRodney W. Grimes }
3389df8bae1dSRodney W. Grimes 
3390df8bae1dSRodney W. Grimes /*
3391df8bae1dSRodney W. Grimes  * Pull out of band byte out of a segment so
3392df8bae1dSRodney W. Grimes  * it doesn't appear in the user's data queue.
3393df8bae1dSRodney W. Grimes  * It is still reflected in the segment length for
3394df8bae1dSRodney W. Grimes  * sequencing purposes.
3395df8bae1dSRodney W. Grimes  */
339655bceb1eSRandall Stewart void
3397ad3f9ab3SAndre Oppermann tcp_pulloutofband(struct socket *so, struct tcphdr *th, struct mbuf *m,
3398ad3f9ab3SAndre Oppermann     int off)
3399df8bae1dSRodney W. Grimes {
3400fb59c426SYoshinobu Inoue 	int cnt = off + th->th_urp - 1;
3401df8bae1dSRodney W. Grimes 
3402df8bae1dSRodney W. Grimes 	while (cnt >= 0) {
3403df8bae1dSRodney W. Grimes 		if (m->m_len > cnt) {
3404df8bae1dSRodney W. Grimes 			char *cp = mtod(m, caddr_t) + cnt;
3405df8bae1dSRodney W. Grimes 			struct tcpcb *tp = sototcpcb(so);
3406df8bae1dSRodney W. Grimes 
34078501a69cSRobert Watson 			INP_WLOCK_ASSERT(tp->t_inpcb);
34087a3244ccSRobert Watson 
3409df8bae1dSRodney W. Grimes 			tp->t_iobc = *cp;
3410df8bae1dSRodney W. Grimes 			tp->t_oobflags |= TCPOOB_HAVEDATA;
3411df8bae1dSRodney W. Grimes 			bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
3412df8bae1dSRodney W. Grimes 			m->m_len--;
341369a34685SYoshinobu Inoue 			if (m->m_flags & M_PKTHDR)
341469a34685SYoshinobu Inoue 				m->m_pkthdr.len--;
3415df8bae1dSRodney W. Grimes 			return;
3416df8bae1dSRodney W. Grimes 		}
3417df8bae1dSRodney W. Grimes 		cnt -= m->m_len;
3418df8bae1dSRodney W. Grimes 		m = m->m_next;
34194dfdffe9SAndre Oppermann 		if (m == NULL)
3420df8bae1dSRodney W. Grimes 			break;
3421df8bae1dSRodney W. Grimes 	}
3422df8bae1dSRodney W. Grimes 	panic("tcp_pulloutofband");
3423df8bae1dSRodney W. Grimes }
3424df8bae1dSRodney W. Grimes 
3425df8bae1dSRodney W. Grimes /*
3426df8bae1dSRodney W. Grimes  * Collect new round-trip time estimate
3427df8bae1dSRodney W. Grimes  * and update averages and current timeout.
3428df8bae1dSRodney W. Grimes  */
342955bceb1eSRandall Stewart void
3430ad3f9ab3SAndre Oppermann tcp_xmit_timer(struct tcpcb *tp, int rtt)
3431df8bae1dSRodney W. Grimes {
3432ad3f9ab3SAndre Oppermann 	int delta;
3433233e8c18SGarrett Wollman 
34348501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
34352be3bf22SRobert Watson 
343678b50714SRobert Watson 	TCPSTAT_INC(tcps_rttupdated);
3437233e8c18SGarrett Wollman 	tp->t_rttupdated++;
3438233e8c18SGarrett Wollman 	if (tp->t_srtt != 0) {
3439233e8c18SGarrett Wollman 		/*
3440233e8c18SGarrett Wollman 		 * srtt is stored as fixed point with 5 bits after the
3441233e8c18SGarrett Wollman 		 * binary point (i.e., scaled by 8).  The following magic
3442233e8c18SGarrett Wollman 		 * is equivalent to the smoothing algorithm in rfc793 with
3443233e8c18SGarrett Wollman 		 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
3444233e8c18SGarrett Wollman 		 * point).  Adjust rtt to origin 0.
3445233e8c18SGarrett Wollman 		 */
3446233e8c18SGarrett Wollman 		delta = ((rtt - 1) << TCP_DELTA_SHIFT)
3447233e8c18SGarrett Wollman 			- (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
3448233e8c18SGarrett Wollman 
3449233e8c18SGarrett Wollman 		if ((tp->t_srtt += delta) <= 0)
3450233e8c18SGarrett Wollman 			tp->t_srtt = 1;
3451233e8c18SGarrett Wollman 
3452233e8c18SGarrett Wollman 		/*
3453233e8c18SGarrett Wollman 		 * We accumulate a smoothed rtt variance (actually, a
3454233e8c18SGarrett Wollman 		 * smoothed mean difference), then set the retransmit
3455233e8c18SGarrett Wollman 		 * timer to smoothed rtt + 4 times the smoothed variance.
3456233e8c18SGarrett Wollman 		 * rttvar is stored as fixed point with 4 bits after the
3457233e8c18SGarrett Wollman 		 * binary point (scaled by 16).  The following is
3458233e8c18SGarrett Wollman 		 * equivalent to rfc793 smoothing with an alpha of .75
3459233e8c18SGarrett Wollman 		 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
3460233e8c18SGarrett Wollman 		 * rfc793's wired-in beta.
3461233e8c18SGarrett Wollman 		 */
3462233e8c18SGarrett Wollman 		if (delta < 0)
3463233e8c18SGarrett Wollman 			delta = -delta;
3464233e8c18SGarrett Wollman 		delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
3465233e8c18SGarrett Wollman 		if ((tp->t_rttvar += delta) <= 0)
3466233e8c18SGarrett Wollman 			tp->t_rttvar = 1;
34671fcc99b5SMatthew Dillon 		if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
34681fcc99b5SMatthew Dillon 		    tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3469233e8c18SGarrett Wollman 	} else {
3470233e8c18SGarrett Wollman 		/*
3471233e8c18SGarrett Wollman 		 * No rtt measurement yet - use the unsmoothed rtt.
3472233e8c18SGarrett Wollman 		 * Set the variance to half the rtt (so our first
3473233e8c18SGarrett Wollman 		 * retransmit happens at 3*rtt).
3474233e8c18SGarrett Wollman 		 */
3475233e8c18SGarrett Wollman 		tp->t_srtt = rtt << TCP_RTT_SHIFT;
3476233e8c18SGarrett Wollman 		tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
34771fcc99b5SMatthew Dillon 		tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
3478233e8c18SGarrett Wollman 	}
34799b8b58e0SJonathan Lemon 	tp->t_rtttime = 0;
3480df8bae1dSRodney W. Grimes 	tp->t_rxtshift = 0;
3481df8bae1dSRodney W. Grimes 
3482df8bae1dSRodney W. Grimes 	/*
3483df8bae1dSRodney W. Grimes 	 * the retransmit should happen at rtt + 4 * rttvar.
3484df8bae1dSRodney W. Grimes 	 * Because of the way we do the smoothing, srtt and rttvar
3485df8bae1dSRodney W. Grimes 	 * will each average +1/2 tick of bias.  When we compute
3486df8bae1dSRodney W. Grimes 	 * the retransmit timer, we want 1/2 tick of rounding and
3487df8bae1dSRodney W. Grimes 	 * 1 extra tick because of +-1/2 tick uncertainty in the
3488df8bae1dSRodney W. Grimes 	 * firing of the timer.  The bias will give us exactly the
3489df8bae1dSRodney W. Grimes 	 * 1.5 tick we need.  But, because the bias is
3490df8bae1dSRodney W. Grimes 	 * statistical, we have to test that we don't drop below
3491df8bae1dSRodney W. Grimes 	 * the minimum feasible timer (which is 2 ticks).
3492df8bae1dSRodney W. Grimes 	 */
3493233e8c18SGarrett Wollman 	TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
34949e2874b0SGarrett Wollman 		      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
3495df8bae1dSRodney W. Grimes 
3496df8bae1dSRodney W. Grimes 	/*
3497df8bae1dSRodney W. Grimes 	 * We received an ack for a packet that wasn't retransmitted;
3498df8bae1dSRodney W. Grimes 	 * it is probably safe to discard any error indications we've
3499df8bae1dSRodney W. Grimes 	 * received recently.  This isn't quite right, but close enough
3500df8bae1dSRodney W. Grimes 	 * for now (a route might have failed after we sent a segment,
3501df8bae1dSRodney W. Grimes 	 * and the return path might not be symmetrical).
3502df8bae1dSRodney W. Grimes 	 */
3503df8bae1dSRodney W. Grimes 	tp->t_softerror = 0;
3504df8bae1dSRodney W. Grimes }
3505df8bae1dSRodney W. Grimes 
3506df8bae1dSRodney W. Grimes /*
3507df8bae1dSRodney W. Grimes  * Determine a reasonable value for maxseg size.
3508df8bae1dSRodney W. Grimes  * If the route is known, check route for mtu.
35094faaea55SAndre Oppermann  * If none, use an mss that can be handled on the outgoing interface
35104faaea55SAndre Oppermann  * without forcing IP to fragment.  If no route is found, route has no mtu,
3511df8bae1dSRodney W. Grimes  * or the destination isn't local, use a default, hopefully conservative
3512df8bae1dSRodney W. Grimes  * size (usually 512 or the default IP max size, but no more than the mtu
3513df8bae1dSRodney W. Grimes  * of the interface), as we can't discover anything about intervening
3514df8bae1dSRodney W. Grimes  * gateways or networks.  We also initialize the congestion/slow start
3515df8bae1dSRodney W. Grimes  * window to be a single segment if the destination isn't local.
3516df8bae1dSRodney W. Grimes  * While looking at the routing entry, we also initialize other path-dependent
3517df8bae1dSRodney W. Grimes  * parameters from pre-set or cached values in the routing entry.
3518a0292f23SGarrett Wollman  *
35190c39d38dSGleb Smirnoff  * NOTE that resulting t_maxseg doesn't include space for TCP options or
35200c39d38dSGleb Smirnoff  * IP options, e.g. IPSEC data, since length of this data may vary, and
35210c39d38dSGleb Smirnoff  * thus it is calculated for every segment separately in tcp_output().
3522a0292f23SGarrett Wollman  *
352397d8d152SAndre Oppermann  * NOTE that this routine is only called when we process an incoming
3524ef341ee1SGleb Smirnoff  * segment, or an ICMP need fragmentation datagram. Outgoing SYN/ACK MSS
3525ef341ee1SGleb Smirnoff  * settings are handled in tcp_mssopt().
3526df8bae1dSRodney W. Grimes  */
3527a0292f23SGarrett Wollman void
3528ef341ee1SGleb Smirnoff tcp_mss_update(struct tcpcb *tp, int offer, int mtuoffer,
35293c914c54SAndre Oppermann     struct hc_metrics_lite *metricptr, struct tcp_ifcap *cap)
3530df8bae1dSRodney W. Grimes {
3531b287c6c7SBjoern A. Zeeb 	int mss = 0;
3532b287c6c7SBjoern A. Zeeb 	u_long maxmtu = 0;
3533c068736aSJeffrey Hsu 	struct inpcb *inp = tp->t_inpcb;
353497d8d152SAndre Oppermann 	struct hc_metrics_lite metrics;
3535fb59c426SYoshinobu Inoue #ifdef INET6
3536c068736aSJeffrey Hsu 	int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
3537c068736aSJeffrey Hsu 	size_t min_protoh = isipv6 ?
3538c068736aSJeffrey Hsu 			    sizeof (struct ip6_hdr) + sizeof (struct tcphdr) :
3539c068736aSJeffrey Hsu 			    sizeof (struct tcpiphdr);
3540c068736aSJeffrey Hsu #else
3541c068736aSJeffrey Hsu 	const size_t min_protoh = sizeof(struct tcpiphdr);
3542fb59c426SYoshinobu Inoue #endif
3543df8bae1dSRodney W. Grimes 
35448501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
35457a3244ccSRobert Watson 
3546ef341ee1SGleb Smirnoff 	if (mtuoffer != -1) {
3547ef341ee1SGleb Smirnoff 		KASSERT(offer == -1, ("%s: conflict", __func__));
3548ef341ee1SGleb Smirnoff 		offer = mtuoffer - min_protoh;
3549ef341ee1SGleb Smirnoff 	}
3550ef341ee1SGleb Smirnoff 
3551e8949f74SAndre Oppermann 	/* Initialize. */
355297d8d152SAndre Oppermann #ifdef INET6
355397d8d152SAndre Oppermann 	if (isipv6) {
35543c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu6(&inp->inp_inc, cap);
35550c39d38dSGleb Smirnoff 		tp->t_maxseg = V_tcp_v6mssdflt;
3556b287c6c7SBjoern A. Zeeb 	}
355797d8d152SAndre Oppermann #endif
3558b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3559b287c6c7SBjoern A. Zeeb 	else
3560b287c6c7SBjoern A. Zeeb #endif
3561b287c6c7SBjoern A. Zeeb #ifdef INET
356297d8d152SAndre Oppermann 	{
35633c914c54SAndre Oppermann 		maxmtu = tcp_maxmtu(&inp->inp_inc, cap);
35640c39d38dSGleb Smirnoff 		tp->t_maxseg = V_tcp_mssdflt;
3565df8bae1dSRodney W. Grimes 	}
3566b287c6c7SBjoern A. Zeeb #endif
3567df8bae1dSRodney W. Grimes 
356897d8d152SAndre Oppermann 	/*
3569e8949f74SAndre Oppermann 	 * No route to sender, stay with default mss and return.
357097d8d152SAndre Oppermann 	 */
35716f01cac6SBjoern A. Zeeb 	if (maxmtu == 0) {
35726f01cac6SBjoern A. Zeeb 		/*
35738e5c87f4SBjoern A. Zeeb 		 * In case we return early we need to initialize metrics
35746f01cac6SBjoern A. Zeeb 		 * to a defined state as tcp_hc_get() would do for us
35756f01cac6SBjoern A. Zeeb 		 * if there was no cache hit.
35766f01cac6SBjoern A. Zeeb 		 */
35776f01cac6SBjoern A. Zeeb 		if (metricptr != NULL)
35786f01cac6SBjoern A. Zeeb 			bzero(metricptr, sizeof(struct hc_metrics_lite));
357997d8d152SAndre Oppermann 		return;
35806f01cac6SBjoern A. Zeeb 	}
358197d8d152SAndre Oppermann 
3582e8949f74SAndre Oppermann 	/* What have we got? */
358397d8d152SAndre Oppermann 	switch (offer) {
358497d8d152SAndre Oppermann 		case 0:
358597d8d152SAndre Oppermann 			/*
358697d8d152SAndre Oppermann 			 * Offer == 0 means that there was no MSS on the SYN
3587c3b02504SBjoern A. Zeeb 			 * segment, in this case we use tcp_mssdflt as
35880c39d38dSGleb Smirnoff 			 * already assigned to t_maxseg above.
358997d8d152SAndre Oppermann 			 */
35900c39d38dSGleb Smirnoff 			offer = tp->t_maxseg;
359197d8d152SAndre Oppermann 			break;
359297d8d152SAndre Oppermann 
359397d8d152SAndre Oppermann 		case -1:
3594a0292f23SGarrett Wollman 			/*
3595c94c54e4SAndre Oppermann 			 * Offer == -1 means that we didn't receive SYN yet.
3596a0292f23SGarrett Wollman 			 */
359797d8d152SAndre Oppermann 			/* FALLTHROUGH */
359897d8d152SAndre Oppermann 
359997d8d152SAndre Oppermann 		default:
3600a0292f23SGarrett Wollman 			/*
360153369ac9SAndre Oppermann 			 * Prevent DoS attack with too small MSS. Round up
360253369ac9SAndre Oppermann 			 * to at least minmss.
360353369ac9SAndre Oppermann 			 */
3604603724d3SBjoern A. Zeeb 			offer = max(offer, V_tcp_minmss);
360597d8d152SAndre Oppermann 	}
3606a0292f23SGarrett Wollman 
3607df8bae1dSRodney W. Grimes 	/*
3608e8949f74SAndre Oppermann 	 * rmx information is now retrieved from tcp_hostcache.
3609df8bae1dSRodney W. Grimes 	 */
361097d8d152SAndre Oppermann 	tcp_hc_get(&inp->inp_inc, &metrics);
36113cee92e0SBjoern A. Zeeb 	if (metricptr != NULL)
36123cee92e0SBjoern A. Zeeb 		bcopy(&metrics, metricptr, sizeof(struct hc_metrics_lite));
361397d8d152SAndre Oppermann 
3614df8bae1dSRodney W. Grimes 	/*
3615cc412412SHiren Panchasara 	 * If there's a discovered mtu in tcp hostcache, use it.
3616cc412412SHiren Panchasara 	 * Else, use the link mtu.
3617df8bae1dSRodney W. Grimes 	 */
361897d8d152SAndre Oppermann 	if (metrics.rmx_mtu)
36192d166c02SAndre Oppermann 		mss = min(metrics.rmx_mtu, maxmtu) - min_protoh;
3620c068736aSJeffrey Hsu 	else {
362131b3783cSHajimu UMEMOTO #ifdef INET6
3622fb59c426SYoshinobu Inoue 		if (isipv6) {
362397d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3624603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
362597d8d152SAndre Oppermann 			    !in6_localaddr(&inp->in6p_faddr))
3626603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_v6mssdflt);
3627b287c6c7SBjoern A. Zeeb 		}
3628b3399803SHajimu UMEMOTO #endif
3629b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3630b287c6c7SBjoern A. Zeeb 		else
3631b287c6c7SBjoern A. Zeeb #endif
3632b287c6c7SBjoern A. Zeeb #ifdef INET
363397d8d152SAndre Oppermann 		{
363497d8d152SAndre Oppermann 			mss = maxmtu - min_protoh;
3635603724d3SBjoern A. Zeeb 			if (!V_path_mtu_discovery &&
363697d8d152SAndre Oppermann 			    !in_localaddr(inp->inp_faddr))
3637603724d3SBjoern A. Zeeb 				mss = min(mss, V_tcp_mssdflt);
3638a0292f23SGarrett Wollman 		}
3639b287c6c7SBjoern A. Zeeb #endif
36403cee92e0SBjoern A. Zeeb 		/*
36413cee92e0SBjoern A. Zeeb 		 * XXX - The above conditional (mss = maxmtu - min_protoh)
36423cee92e0SBjoern A. Zeeb 		 * probably violates the TCP spec.
36433cee92e0SBjoern A. Zeeb 		 * The problem is that, since we don't know the
36443cee92e0SBjoern A. Zeeb 		 * other end's MSS, we are supposed to use a conservative
36453cee92e0SBjoern A. Zeeb 		 * default.  But, if we do that, then MTU discovery will
36463cee92e0SBjoern A. Zeeb 		 * never actually take place, because the conservative
36473cee92e0SBjoern A. Zeeb 		 * default is much less than the MTUs typically seen
36483cee92e0SBjoern A. Zeeb 		 * on the Internet today.  For the moment, we'll sweep
36493cee92e0SBjoern A. Zeeb 		 * this under the carpet.
36503cee92e0SBjoern A. Zeeb 		 *
36513cee92e0SBjoern A. Zeeb 		 * The conservative default might not actually be a problem
36523cee92e0SBjoern A. Zeeb 		 * if the only case this occurs is when sending an initial
36533cee92e0SBjoern A. Zeeb 		 * SYN with options and data to a host we've never talked
36543cee92e0SBjoern A. Zeeb 		 * to before.  Then, they will reply with an MSS value which
36553cee92e0SBjoern A. Zeeb 		 * will get recorded and the new parameters should get
36563cee92e0SBjoern A. Zeeb 		 * recomputed.  For Further Study.
36573cee92e0SBjoern A. Zeeb 		 */
365897d8d152SAndre Oppermann 	}
3659a0292f23SGarrett Wollman 	mss = min(mss, offer);
366097d8d152SAndre Oppermann 
3661a0292f23SGarrett Wollman 	/*
36620c39d38dSGleb Smirnoff 	 * Sanity check: make sure that maxseg will be large
36633cee92e0SBjoern A. Zeeb 	 * enough to allow some data on segments even if the
36643cee92e0SBjoern A. Zeeb 	 * all the option space is used (40bytes).  Otherwise
36653cee92e0SBjoern A. Zeeb 	 * funny things may happen in tcp_output.
36660c39d38dSGleb Smirnoff 	 *
36670c39d38dSGleb Smirnoff 	 * XXXGL: shouldn't we reserve space for IP/IPv6 options?
36683cee92e0SBjoern A. Zeeb 	 */
36693cee92e0SBjoern A. Zeeb 	mss = max(mss, 64);
36703cee92e0SBjoern A. Zeeb 
367197d8d152SAndre Oppermann 	tp->t_maxseg = mss;
36723cee92e0SBjoern A. Zeeb }
36733cee92e0SBjoern A. Zeeb 
36743cee92e0SBjoern A. Zeeb void
36753cee92e0SBjoern A. Zeeb tcp_mss(struct tcpcb *tp, int offer)
36763cee92e0SBjoern A. Zeeb {
3677dbc42409SLawrence Stewart 	int mss;
36783cee92e0SBjoern A. Zeeb 	u_long bufsize;
36793cee92e0SBjoern A. Zeeb 	struct inpcb *inp;
36803cee92e0SBjoern A. Zeeb 	struct socket *so;
36813cee92e0SBjoern A. Zeeb 	struct hc_metrics_lite metrics;
36823c914c54SAndre Oppermann 	struct tcp_ifcap cap;
3683dbc42409SLawrence Stewart 
36843cee92e0SBjoern A. Zeeb 	KASSERT(tp != NULL, ("%s: tp == NULL", __func__));
36853cee92e0SBjoern A. Zeeb 
36863c914c54SAndre Oppermann 	bzero(&cap, sizeof(cap));
36873c914c54SAndre Oppermann 	tcp_mss_update(tp, offer, -1, &metrics, &cap);
36883cee92e0SBjoern A. Zeeb 
36893cee92e0SBjoern A. Zeeb 	mss = tp->t_maxseg;
36903cee92e0SBjoern A. Zeeb 	inp = tp->t_inpcb;
369197d8d152SAndre Oppermann 
3692df8bae1dSRodney W. Grimes 	/*
369397d8d152SAndre Oppermann 	 * If there's a pipesize, change the socket buffer to that size,
369497d8d152SAndre Oppermann 	 * don't change if sb_hiwat is different than default (then it
369597d8d152SAndre Oppermann 	 * has been changed on purpose with setsockopt).
369697d8d152SAndre Oppermann 	 * Make the socket buffers an integral number of mss units;
369797d8d152SAndre Oppermann 	 * if the mss is larger than the socket buffer, decrease the mss.
3698df8bae1dSRodney W. Grimes 	 */
3699c3b02504SBjoern A. Zeeb 	so = inp->inp_socket;
37003f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_snd);
3701e233e2acSAndre Oppermann 	if ((so->so_snd.sb_hiwat == V_tcp_sendspace) && metrics.rmx_sendpipe)
370297d8d152SAndre Oppermann 		bufsize = metrics.rmx_sendpipe;
370397d8d152SAndre Oppermann 	else
3704df8bae1dSRodney W. Grimes 		bufsize = so->so_snd.sb_hiwat;
3705df8bae1dSRodney W. Grimes 	if (bufsize < mss)
3706df8bae1dSRodney W. Grimes 		mss = bufsize;
3707df8bae1dSRodney W. Grimes 	else {
3708df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3709df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3710df8bae1dSRodney W. Grimes 			bufsize = sb_max;
371188c39af3SRuslan Ermilov 		if (bufsize > so->so_snd.sb_hiwat)
37123f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_snd, bufsize, so, NULL);
3713df8bae1dSRodney W. Grimes 	}
37143f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_snd);
3715df8bae1dSRodney W. Grimes 	tp->t_maxseg = mss;
3716df8bae1dSRodney W. Grimes 
37173f11a2f3SRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
3718e233e2acSAndre Oppermann 	if ((so->so_rcv.sb_hiwat == V_tcp_recvspace) && metrics.rmx_recvpipe)
371997d8d152SAndre Oppermann 		bufsize = metrics.rmx_recvpipe;
372097d8d152SAndre Oppermann 	else
3721df8bae1dSRodney W. Grimes 		bufsize = so->so_rcv.sb_hiwat;
3722df8bae1dSRodney W. Grimes 	if (bufsize > mss) {
3723df8bae1dSRodney W. Grimes 		bufsize = roundup(bufsize, mss);
3724df8bae1dSRodney W. Grimes 		if (bufsize > sb_max)
3725df8bae1dSRodney W. Grimes 			bufsize = sb_max;
372688c39af3SRuslan Ermilov 		if (bufsize > so->so_rcv.sb_hiwat)
37273f11a2f3SRobert Watson 			(void)sbreserve_locked(&so->so_rcv, bufsize, so, NULL);
3728df8bae1dSRodney W. Grimes 	}
37293f11a2f3SRobert Watson 	SOCKBUF_UNLOCK(&so->so_rcv);
373091d6cfa6SBjoern A. Zeeb 
373191d6cfa6SBjoern A. Zeeb 	/* Check the interface for TSO capabilities. */
37323c914c54SAndre Oppermann 	if (cap.ifcap & CSUM_TSO) {
373391d6cfa6SBjoern A. Zeeb 		tp->t_flags |= TF_TSO;
37343c914c54SAndre Oppermann 		tp->t_tsomax = cap.tsomax;
37359fd573c3SHans Petter Selasky 		tp->t_tsomaxsegcount = cap.tsomaxsegcount;
37369fd573c3SHans Petter Selasky 		tp->t_tsomaxsegsize = cap.tsomaxsegsize;
37373c914c54SAndre Oppermann 	}
3738a0292f23SGarrett Wollman }
3739a0292f23SGarrett Wollman 
3740a0292f23SGarrett Wollman /*
3741a0292f23SGarrett Wollman  * Determine the MSS option to send on an outgoing SYN.
3742a0292f23SGarrett Wollman  */
3743a0292f23SGarrett Wollman int
3744ad3f9ab3SAndre Oppermann tcp_mssopt(struct in_conninfo *inc)
3745a0292f23SGarrett Wollman {
374697d8d152SAndre Oppermann 	int mss = 0;
374797d8d152SAndre Oppermann 	u_long maxmtu = 0;
374897d8d152SAndre Oppermann 	u_long thcmtu = 0;
374997d8d152SAndre Oppermann 	size_t min_protoh;
3750a0292f23SGarrett Wollman 
375197d8d152SAndre Oppermann 	KASSERT(inc != NULL, ("tcp_mssopt with NULL in_conninfo pointer"));
3752a0292f23SGarrett Wollman 
375331b3783cSHajimu UMEMOTO #ifdef INET6
3754dcdb4371SBjoern A. Zeeb 	if (inc->inc_flags & INC_ISIPV6) {
3755603724d3SBjoern A. Zeeb 		mss = V_tcp_v6mssdflt;
3756233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu6(inc, NULL);
375797d8d152SAndre Oppermann 		min_protoh = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
3758b287c6c7SBjoern A. Zeeb 	}
375931b3783cSHajimu UMEMOTO #endif
3760b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6)
3761b287c6c7SBjoern A. Zeeb 	else
3762b287c6c7SBjoern A. Zeeb #endif
3763b287c6c7SBjoern A. Zeeb #ifdef INET
376497d8d152SAndre Oppermann 	{
3765603724d3SBjoern A. Zeeb 		mss = V_tcp_mssdflt;
3766233dcce1SAndre Oppermann 		maxmtu = tcp_maxmtu(inc, NULL);
376797d8d152SAndre Oppermann 		min_protoh = sizeof(struct tcpiphdr);
376897d8d152SAndre Oppermann 	}
3769b287c6c7SBjoern A. Zeeb #endif
37703a9391deSBjoern A. Zeeb #if defined(INET6) || defined(INET)
37713a9391deSBjoern A. Zeeb 	thcmtu = tcp_hc_getmtu(inc); /* IPv4 and IPv6 */
37723a9391deSBjoern A. Zeeb #endif
37733a9391deSBjoern A. Zeeb 
377497d8d152SAndre Oppermann 	if (maxmtu && thcmtu)
377597d8d152SAndre Oppermann 		mss = min(maxmtu, thcmtu) - min_protoh;
377697d8d152SAndre Oppermann 	else if (maxmtu || thcmtu)
377797d8d152SAndre Oppermann 		mss = max(maxmtu, thcmtu) - min_protoh;
377897d8d152SAndre Oppermann 
377997d8d152SAndre Oppermann 	return (mss);
3780df8bae1dSRodney W. Grimes }
378146f58482SJonathan Lemon 
378246f58482SJonathan Lemon 
378346f58482SJonathan Lemon /*
3784c068736aSJeffrey Hsu  * On a partial ack arrives, force the retransmission of the
3785c068736aSJeffrey Hsu  * next unacknowledged segment.  Do not clear tp->t_dupacks.
3786c068736aSJeffrey Hsu  * By setting snd_nxt to ti_ack, this forces retransmission timer to
3787c068736aSJeffrey Hsu  * be started again.
378846f58482SJonathan Lemon  */
378955bceb1eSRandall Stewart void
3790ad3f9ab3SAndre Oppermann tcp_newreno_partial_ack(struct tcpcb *tp, struct tcphdr *th)
379146f58482SJonathan Lemon {
379246f58482SJonathan Lemon 	tcp_seq onxt = tp->snd_nxt;
379346f58482SJonathan Lemon 	u_long ocwnd = tp->snd_cwnd;
37940c39d38dSGleb Smirnoff 	u_int maxseg = tcp_maxseg(tp);
379546f58482SJonathan Lemon 
37968501a69cSRobert Watson 	INP_WLOCK_ASSERT(tp->t_inpcb);
37977a3244ccSRobert Watson 
3798b8152ba7SAndre Oppermann 	tcp_timer_activate(tp, TT_REXMT, 0);
379946f58482SJonathan Lemon 	tp->t_rtttime = 0;
380046f58482SJonathan Lemon 	tp->snd_nxt = th->th_ack;
38016b2a5f92SJayanth Vijayaraghavan 	/*
3802c068736aSJeffrey Hsu 	 * Set snd_cwnd to one segment beyond acknowledged offset.
3803c068736aSJeffrey Hsu 	 * (tp->snd_una has not yet been updated when this function is called.)
38046b2a5f92SJayanth Vijayaraghavan 	 */
38050c39d38dSGleb Smirnoff 	tp->snd_cwnd = maxseg + BYTES_THIS_ACK(tp, th);
3806a84db8f4SMatthew Dillon 	tp->t_flags |= TF_ACKNOW;
380755bceb1eSRandall Stewart 	(void) tp->t_fb->tfb_tcp_output(tp);
380846f58482SJonathan Lemon 	tp->snd_cwnd = ocwnd;
380946f58482SJonathan Lemon 	if (SEQ_GT(onxt, tp->snd_nxt))
381046f58482SJonathan Lemon 		tp->snd_nxt = onxt;
381146f58482SJonathan Lemon 	/*
381246f58482SJonathan Lemon 	 * Partial window deflation.  Relies on fact that tp->snd_una
381346f58482SJonathan Lemon 	 * not updated yet.
381446f58482SJonathan Lemon 	 */
3815dbc42409SLawrence Stewart 	if (tp->snd_cwnd > BYTES_THIS_ACK(tp, th))
3816dbc42409SLawrence Stewart 		tp->snd_cwnd -= BYTES_THIS_ACK(tp, th);
3817d7587117SPaul Saab 	else
3818d7587117SPaul Saab 		tp->snd_cwnd = 0;
38190c39d38dSGleb Smirnoff 	tp->snd_cwnd += maxseg;
382046f58482SJonathan Lemon }
382112eeb81fSHiren Panchasara 
382212eeb81fSHiren Panchasara int
382312eeb81fSHiren Panchasara tcp_compute_pipe(struct tcpcb *tp)
382412eeb81fSHiren Panchasara {
382512eeb81fSHiren Panchasara 	return (tp->snd_max - tp->snd_una +
382612eeb81fSHiren Panchasara 		tp->sackhint.sack_bytes_rexmit -
382712eeb81fSHiren Panchasara 		tp->sackhint.sacked_bytes);
382812eeb81fSHiren Panchasara }
3829